Remove dwarf2_cu::base_known
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "bfd.h"
44 #include "elf-bfd.h"
45 #include "symtab.h"
46 #include "gdbtypes.h"
47 #include "objfiles.h"
48 #include "dwarf2.h"
49 #include "buildsym.h"
50 #include "demangle.h"
51 #include "gdb-demangle.h"
52 #include "filenames.h" /* for DOSish file names */
53 #include "language.h"
54 #include "complaints.h"
55 #include "dwarf2/expr.h"
56 #include "dwarf2/loc.h"
57 #include "cp-support.h"
58 #include "hashtab.h"
59 #include "command.h"
60 #include "gdbcmd.h"
61 #include "block.h"
62 #include "addrmap.h"
63 #include "typeprint.h"
64 #include "psympriv.h"
65 #include "c-lang.h"
66 #include "go-lang.h"
67 #include "valprint.h"
68 #include "gdbcore.h" /* for gnutarget */
69 #include "gdb/gdb-index.h"
70 #include "gdb_bfd.h"
71 #include "f-lang.h"
72 #include "source.h"
73 #include "build-id.h"
74 #include "namespace.h"
75 #include "gdbsupport/function-view.h"
76 #include "gdbsupport/gdb_optional.h"
77 #include "gdbsupport/underlying.h"
78 #include "gdbsupport/hash_enum.h"
79 #include "filename-seen-cache.h"
80 #include "producer.h"
81 #include <fcntl.h>
82 #include <algorithm>
83 #include <unordered_map>
84 #include "gdbsupport/selftest.h"
85 #include "rust-lang.h"
86 #include "gdbsupport/pathstuff.h"
87 #include "count-one-bits.h"
88 #include "debuginfod-support.h"
89
90 /* When == 1, print basic high level tracing messages.
91 When > 1, be more verbose.
92 This is in contrast to the low level DIE reading of dwarf_die_debug. */
93 static unsigned int dwarf_read_debug = 0;
94
95 /* When non-zero, dump DIEs after they are read in. */
96 static unsigned int dwarf_die_debug = 0;
97
98 /* When non-zero, dump line number entries as they are read in. */
99 unsigned int dwarf_line_debug = 0;
100
101 /* When true, cross-check physname against demangler. */
102 static bool check_physname = false;
103
104 /* When true, do not reject deprecated .gdb_index sections. */
105 static bool use_deprecated_index_sections = false;
106
107 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
108
109 /* The "aclass" indices for various kinds of computed DWARF symbols. */
110
111 static int dwarf2_locexpr_index;
112 static int dwarf2_loclist_index;
113 static int dwarf2_locexpr_block_index;
114 static int dwarf2_loclist_block_index;
115
116 /* An index into a (C++) symbol name component in a symbol name as
117 recorded in the mapped_index's symbol table. For each C++ symbol
118 in the symbol table, we record one entry for the start of each
119 component in the symbol in a table of name components, and then
120 sort the table, in order to be able to binary search symbol names,
121 ignoring leading namespaces, both completion and regular look up.
122 For example, for symbol "A::B::C", we'll have an entry that points
123 to "A::B::C", another that points to "B::C", and another for "C".
124 Note that function symbols in GDB index have no parameter
125 information, just the function/method names. You can convert a
126 name_component to a "const char *" using the
127 'mapped_index::symbol_name_at(offset_type)' method. */
128
129 struct name_component
130 {
131 /* Offset in the symbol name where the component starts. Stored as
132 a (32-bit) offset instead of a pointer to save memory and improve
133 locality on 64-bit architectures. */
134 offset_type name_offset;
135
136 /* The symbol's index in the symbol and constant pool tables of a
137 mapped_index. */
138 offset_type idx;
139 };
140
141 /* Base class containing bits shared by both .gdb_index and
142 .debug_name indexes. */
143
144 struct mapped_index_base
145 {
146 mapped_index_base () = default;
147 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
148
149 /* The name_component table (a sorted vector). See name_component's
150 description above. */
151 std::vector<name_component> name_components;
152
153 /* How NAME_COMPONENTS is sorted. */
154 enum case_sensitivity name_components_casing;
155
156 /* Return the number of names in the symbol table. */
157 virtual size_t symbol_name_count () const = 0;
158
159 /* Get the name of the symbol at IDX in the symbol table. */
160 virtual const char *symbol_name_at (offset_type idx) const = 0;
161
162 /* Return whether the name at IDX in the symbol table should be
163 ignored. */
164 virtual bool symbol_name_slot_invalid (offset_type idx) const
165 {
166 return false;
167 }
168
169 /* Build the symbol name component sorted vector, if we haven't
170 yet. */
171 void build_name_components ();
172
173 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
174 possible matches for LN_NO_PARAMS in the name component
175 vector. */
176 std::pair<std::vector<name_component>::const_iterator,
177 std::vector<name_component>::const_iterator>
178 find_name_components_bounds (const lookup_name_info &ln_no_params,
179 enum language lang) const;
180
181 /* Prevent deleting/destroying via a base class pointer. */
182 protected:
183 ~mapped_index_base() = default;
184 };
185
186 /* A description of the mapped index. The file format is described in
187 a comment by the code that writes the index. */
188 struct mapped_index final : public mapped_index_base
189 {
190 /* A slot/bucket in the symbol table hash. */
191 struct symbol_table_slot
192 {
193 const offset_type name;
194 const offset_type vec;
195 };
196
197 /* Index data format version. */
198 int version = 0;
199
200 /* The address table data. */
201 gdb::array_view<const gdb_byte> address_table;
202
203 /* The symbol table, implemented as a hash table. */
204 gdb::array_view<symbol_table_slot> symbol_table;
205
206 /* A pointer to the constant pool. */
207 const char *constant_pool = nullptr;
208
209 bool symbol_name_slot_invalid (offset_type idx) const override
210 {
211 const auto &bucket = this->symbol_table[idx];
212 return bucket.name == 0 && bucket.vec == 0;
213 }
214
215 /* Convenience method to get at the name of the symbol at IDX in the
216 symbol table. */
217 const char *symbol_name_at (offset_type idx) const override
218 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
219
220 size_t symbol_name_count () const override
221 { return this->symbol_table.size (); }
222 };
223
224 /* A description of the mapped .debug_names.
225 Uninitialized map has CU_COUNT 0. */
226 struct mapped_debug_names final : public mapped_index_base
227 {
228 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
229 : dwarf2_per_objfile (dwarf2_per_objfile_)
230 {}
231
232 struct dwarf2_per_objfile *dwarf2_per_objfile;
233 bfd_endian dwarf5_byte_order;
234 bool dwarf5_is_dwarf64;
235 bool augmentation_is_gdb;
236 uint8_t offset_size;
237 uint32_t cu_count = 0;
238 uint32_t tu_count, bucket_count, name_count;
239 const gdb_byte *cu_table_reordered, *tu_table_reordered;
240 const uint32_t *bucket_table_reordered, *hash_table_reordered;
241 const gdb_byte *name_table_string_offs_reordered;
242 const gdb_byte *name_table_entry_offs_reordered;
243 const gdb_byte *entry_pool;
244
245 struct index_val
246 {
247 ULONGEST dwarf_tag;
248 struct attr
249 {
250 /* Attribute name DW_IDX_*. */
251 ULONGEST dw_idx;
252
253 /* Attribute form DW_FORM_*. */
254 ULONGEST form;
255
256 /* Value if FORM is DW_FORM_implicit_const. */
257 LONGEST implicit_const;
258 };
259 std::vector<attr> attr_vec;
260 };
261
262 std::unordered_map<ULONGEST, index_val> abbrev_map;
263
264 const char *namei_to_name (uint32_t namei) const;
265
266 /* Implementation of the mapped_index_base virtual interface, for
267 the name_components cache. */
268
269 const char *symbol_name_at (offset_type idx) const override
270 { return namei_to_name (idx); }
271
272 size_t symbol_name_count () const override
273 { return this->name_count; }
274 };
275
276 /* See dwarf2read.h. */
277
278 dwarf2_per_objfile *
279 get_dwarf2_per_objfile (struct objfile *objfile)
280 {
281 return dwarf2_objfile_data_key.get (objfile);
282 }
283
284 /* Default names of the debugging sections. */
285
286 /* Note that if the debugging section has been compressed, it might
287 have a name like .zdebug_info. */
288
289 static const struct dwarf2_debug_sections dwarf2_elf_names =
290 {
291 { ".debug_info", ".zdebug_info" },
292 { ".debug_abbrev", ".zdebug_abbrev" },
293 { ".debug_line", ".zdebug_line" },
294 { ".debug_loc", ".zdebug_loc" },
295 { ".debug_loclists", ".zdebug_loclists" },
296 { ".debug_macinfo", ".zdebug_macinfo" },
297 { ".debug_macro", ".zdebug_macro" },
298 { ".debug_str", ".zdebug_str" },
299 { ".debug_str_offsets", ".zdebug_str_offsets" },
300 { ".debug_line_str", ".zdebug_line_str" },
301 { ".debug_ranges", ".zdebug_ranges" },
302 { ".debug_rnglists", ".zdebug_rnglists" },
303 { ".debug_types", ".zdebug_types" },
304 { ".debug_addr", ".zdebug_addr" },
305 { ".debug_frame", ".zdebug_frame" },
306 { ".eh_frame", NULL },
307 { ".gdb_index", ".zgdb_index" },
308 { ".debug_names", ".zdebug_names" },
309 { ".debug_aranges", ".zdebug_aranges" },
310 23
311 };
312
313 /* List of DWO/DWP sections. */
314
315 static const struct dwop_section_names
316 {
317 struct dwarf2_section_names abbrev_dwo;
318 struct dwarf2_section_names info_dwo;
319 struct dwarf2_section_names line_dwo;
320 struct dwarf2_section_names loc_dwo;
321 struct dwarf2_section_names loclists_dwo;
322 struct dwarf2_section_names macinfo_dwo;
323 struct dwarf2_section_names macro_dwo;
324 struct dwarf2_section_names str_dwo;
325 struct dwarf2_section_names str_offsets_dwo;
326 struct dwarf2_section_names types_dwo;
327 struct dwarf2_section_names cu_index;
328 struct dwarf2_section_names tu_index;
329 }
330 dwop_section_names =
331 {
332 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
333 { ".debug_info.dwo", ".zdebug_info.dwo" },
334 { ".debug_line.dwo", ".zdebug_line.dwo" },
335 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
336 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
337 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
338 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
339 { ".debug_str.dwo", ".zdebug_str.dwo" },
340 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
341 { ".debug_types.dwo", ".zdebug_types.dwo" },
342 { ".debug_cu_index", ".zdebug_cu_index" },
343 { ".debug_tu_index", ".zdebug_tu_index" },
344 };
345
346 /* local data types */
347
348 /* Type used for delaying computation of method physnames.
349 See comments for compute_delayed_physnames. */
350 struct delayed_method_info
351 {
352 /* The type to which the method is attached, i.e., its parent class. */
353 struct type *type;
354
355 /* The index of the method in the type's function fieldlists. */
356 int fnfield_index;
357
358 /* The index of the method in the fieldlist. */
359 int index;
360
361 /* The name of the DIE. */
362 const char *name;
363
364 /* The DIE associated with this method. */
365 struct die_info *die;
366 };
367
368 /* Internal state when decoding a particular compilation unit. */
369 struct dwarf2_cu
370 {
371 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
372 ~dwarf2_cu ();
373
374 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
375
376 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
377 Create the set of symtabs used by this TU, or if this TU is sharing
378 symtabs with another TU and the symtabs have already been created
379 then restore those symtabs in the line header.
380 We don't need the pc/line-number mapping for type units. */
381 void setup_type_unit_groups (struct die_info *die);
382
383 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
384 buildsym_compunit constructor. */
385 struct compunit_symtab *start_symtab (const char *name,
386 const char *comp_dir,
387 CORE_ADDR low_pc);
388
389 /* Reset the builder. */
390 void reset_builder () { m_builder.reset (); }
391
392 /* The header of the compilation unit. */
393 struct comp_unit_head header {};
394
395 /* Base address of this compilation unit. */
396 gdb::optional<CORE_ADDR> base_address;
397
398 /* The language we are debugging. */
399 enum language language = language_unknown;
400 const struct language_defn *language_defn = nullptr;
401
402 const char *producer = nullptr;
403
404 private:
405 /* The symtab builder for this CU. This is only non-NULL when full
406 symbols are being read. */
407 std::unique_ptr<buildsym_compunit> m_builder;
408
409 public:
410 /* The generic symbol table building routines have separate lists for
411 file scope symbols and all all other scopes (local scopes). So
412 we need to select the right one to pass to add_symbol_to_list().
413 We do it by keeping a pointer to the correct list in list_in_scope.
414
415 FIXME: The original dwarf code just treated the file scope as the
416 first local scope, and all other local scopes as nested local
417 scopes, and worked fine. Check to see if we really need to
418 distinguish these in buildsym.c. */
419 struct pending **list_in_scope = nullptr;
420
421 /* Hash table holding all the loaded partial DIEs
422 with partial_die->offset.SECT_OFF as hash. */
423 htab_t partial_dies = nullptr;
424
425 /* Storage for things with the same lifetime as this read-in compilation
426 unit, including partial DIEs. */
427 auto_obstack comp_unit_obstack;
428
429 /* When multiple dwarf2_cu structures are living in memory, this field
430 chains them all together, so that they can be released efficiently.
431 We will probably also want a generation counter so that most-recently-used
432 compilation units are cached... */
433 struct dwarf2_per_cu_data *read_in_chain = nullptr;
434
435 /* Backlink to our per_cu entry. */
436 struct dwarf2_per_cu_data *per_cu;
437
438 /* How many compilation units ago was this CU last referenced? */
439 int last_used = 0;
440
441 /* A hash table of DIE cu_offset for following references with
442 die_info->offset.sect_off as hash. */
443 htab_t die_hash = nullptr;
444
445 /* Full DIEs if read in. */
446 struct die_info *dies = nullptr;
447
448 /* A set of pointers to dwarf2_per_cu_data objects for compilation
449 units referenced by this one. Only set during full symbol processing;
450 partial symbol tables do not have dependencies. */
451 htab_t dependencies = nullptr;
452
453 /* Header data from the line table, during full symbol processing. */
454 struct line_header *line_header = nullptr;
455 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
456 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
457 this is the DW_TAG_compile_unit die for this CU. We'll hold on
458 to the line header as long as this DIE is being processed. See
459 process_die_scope. */
460 die_info *line_header_die_owner = nullptr;
461
462 /* A list of methods which need to have physnames computed
463 after all type information has been read. */
464 std::vector<delayed_method_info> method_list;
465
466 /* To be copied to symtab->call_site_htab. */
467 htab_t call_site_htab = nullptr;
468
469 /* Non-NULL if this CU came from a DWO file.
470 There is an invariant here that is important to remember:
471 Except for attributes copied from the top level DIE in the "main"
472 (or "stub") file in preparation for reading the DWO file
473 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
474 Either there isn't a DWO file (in which case this is NULL and the point
475 is moot), or there is and either we're not going to read it (in which
476 case this is NULL) or there is and we are reading it (in which case this
477 is non-NULL). */
478 struct dwo_unit *dwo_unit = nullptr;
479
480 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
481 Note this value comes from the Fission stub CU/TU's DIE. */
482 gdb::optional<ULONGEST> addr_base;
483
484 /* The DW_AT_rnglists_base attribute if present.
485 Note this value comes from the Fission stub CU/TU's DIE.
486 Also note that the value is zero in the non-DWO case so this value can
487 be used without needing to know whether DWO files are in use or not.
488 N.B. This does not apply to DW_AT_ranges appearing in
489 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
490 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
491 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
492 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
493 ULONGEST ranges_base = 0;
494
495 /* When reading debug info generated by older versions of rustc, we
496 have to rewrite some union types to be struct types with a
497 variant part. This rewriting must be done after the CU is fully
498 read in, because otherwise at the point of rewriting some struct
499 type might not have been fully processed. So, we keep a list of
500 all such types here and process them after expansion. */
501 std::vector<struct type *> rust_unions;
502
503 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
504 files, the value is implicitly zero. For DWARF 5 version DWO files, the
505 value is often implicit and is the size of the header of
506 .debug_str_offsets section (8 or 4, depending on the address size). */
507 gdb::optional<ULONGEST> str_offsets_base;
508
509 /* Mark used when releasing cached dies. */
510 bool mark : 1;
511
512 /* This CU references .debug_loc. See the symtab->locations_valid field.
513 This test is imperfect as there may exist optimized debug code not using
514 any location list and still facing inlining issues if handled as
515 unoptimized code. For a future better test see GCC PR other/32998. */
516 bool has_loclist : 1;
517
518 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
519 if all the producer_is_* fields are valid. This information is cached
520 because profiling CU expansion showed excessive time spent in
521 producer_is_gxx_lt_4_6. */
522 bool checked_producer : 1;
523 bool producer_is_gxx_lt_4_6 : 1;
524 bool producer_is_gcc_lt_4_3 : 1;
525 bool producer_is_icc : 1;
526 bool producer_is_icc_lt_14 : 1;
527 bool producer_is_codewarrior : 1;
528
529 /* When true, the file that we're processing is known to have
530 debugging info for C++ namespaces. GCC 3.3.x did not produce
531 this information, but later versions do. */
532
533 bool processing_has_namespace_info : 1;
534
535 struct partial_die_info *find_partial_die (sect_offset sect_off);
536
537 /* If this CU was inherited by another CU (via specification,
538 abstract_origin, etc), this is the ancestor CU. */
539 dwarf2_cu *ancestor;
540
541 /* Get the buildsym_compunit for this CU. */
542 buildsym_compunit *get_builder ()
543 {
544 /* If this CU has a builder associated with it, use that. */
545 if (m_builder != nullptr)
546 return m_builder.get ();
547
548 /* Otherwise, search ancestors for a valid builder. */
549 if (ancestor != nullptr)
550 return ancestor->get_builder ();
551
552 return nullptr;
553 }
554 };
555
556 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
557 This includes type_unit_group and quick_file_names. */
558
559 struct stmt_list_hash
560 {
561 /* The DWO unit this table is from or NULL if there is none. */
562 struct dwo_unit *dwo_unit;
563
564 /* Offset in .debug_line or .debug_line.dwo. */
565 sect_offset line_sect_off;
566 };
567
568 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
569 an object of this type. */
570
571 struct type_unit_group
572 {
573 /* dwarf2read.c's main "handle" on a TU symtab.
574 To simplify things we create an artificial CU that "includes" all the
575 type units using this stmt_list so that the rest of the code still has
576 a "per_cu" handle on the symtab. */
577 struct dwarf2_per_cu_data per_cu;
578
579 /* The TUs that share this DW_AT_stmt_list entry.
580 This is added to while parsing type units to build partial symtabs,
581 and is deleted afterwards and not used again. */
582 std::vector<signatured_type *> *tus;
583
584 /* The compunit symtab.
585 Type units in a group needn't all be defined in the same source file,
586 so we create an essentially anonymous symtab as the compunit symtab. */
587 struct compunit_symtab *compunit_symtab;
588
589 /* The data used to construct the hash key. */
590 struct stmt_list_hash hash;
591
592 /* The symbol tables for this TU (obtained from the files listed in
593 DW_AT_stmt_list).
594 WARNING: The order of entries here must match the order of entries
595 in the line header. After the first TU using this type_unit_group, the
596 line header for the subsequent TUs is recreated from this. This is done
597 because we need to use the same symtabs for each TU using the same
598 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
599 there's no guarantee the line header doesn't have duplicate entries. */
600 struct symtab **symtabs;
601 };
602
603 /* These sections are what may appear in a (real or virtual) DWO file. */
604
605 struct dwo_sections
606 {
607 struct dwarf2_section_info abbrev;
608 struct dwarf2_section_info line;
609 struct dwarf2_section_info loc;
610 struct dwarf2_section_info loclists;
611 struct dwarf2_section_info macinfo;
612 struct dwarf2_section_info macro;
613 struct dwarf2_section_info str;
614 struct dwarf2_section_info str_offsets;
615 /* In the case of a virtual DWO file, these two are unused. */
616 struct dwarf2_section_info info;
617 std::vector<dwarf2_section_info> types;
618 };
619
620 /* CUs/TUs in DWP/DWO files. */
621
622 struct dwo_unit
623 {
624 /* Backlink to the containing struct dwo_file. */
625 struct dwo_file *dwo_file;
626
627 /* The "id" that distinguishes this CU/TU.
628 .debug_info calls this "dwo_id", .debug_types calls this "signature".
629 Since signatures came first, we stick with it for consistency. */
630 ULONGEST signature;
631
632 /* The section this CU/TU lives in, in the DWO file. */
633 struct dwarf2_section_info *section;
634
635 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
636 sect_offset sect_off;
637 unsigned int length;
638
639 /* For types, offset in the type's DIE of the type defined by this TU. */
640 cu_offset type_offset_in_tu;
641 };
642
643 /* include/dwarf2.h defines the DWP section codes.
644 It defines a max value but it doesn't define a min value, which we
645 use for error checking, so provide one. */
646
647 enum dwp_v2_section_ids
648 {
649 DW_SECT_MIN = 1
650 };
651
652 /* Data for one DWO file.
653
654 This includes virtual DWO files (a virtual DWO file is a DWO file as it
655 appears in a DWP file). DWP files don't really have DWO files per se -
656 comdat folding of types "loses" the DWO file they came from, and from
657 a high level view DWP files appear to contain a mass of random types.
658 However, to maintain consistency with the non-DWP case we pretend DWP
659 files contain virtual DWO files, and we assign each TU with one virtual
660 DWO file (generally based on the line and abbrev section offsets -
661 a heuristic that seems to work in practice). */
662
663 struct dwo_file
664 {
665 dwo_file () = default;
666 DISABLE_COPY_AND_ASSIGN (dwo_file);
667
668 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
669 For virtual DWO files the name is constructed from the section offsets
670 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
671 from related CU+TUs. */
672 const char *dwo_name = nullptr;
673
674 /* The DW_AT_comp_dir attribute. */
675 const char *comp_dir = nullptr;
676
677 /* The bfd, when the file is open. Otherwise this is NULL.
678 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
679 gdb_bfd_ref_ptr dbfd;
680
681 /* The sections that make up this DWO file.
682 Remember that for virtual DWO files in DWP V2, these are virtual
683 sections (for lack of a better name). */
684 struct dwo_sections sections {};
685
686 /* The CUs in the file.
687 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
688 an extension to handle LLVM's Link Time Optimization output (where
689 multiple source files may be compiled into a single object/dwo pair). */
690 htab_up cus;
691
692 /* Table of TUs in the file.
693 Each element is a struct dwo_unit. */
694 htab_up tus;
695 };
696
697 /* These sections are what may appear in a DWP file. */
698
699 struct dwp_sections
700 {
701 /* These are used by both DWP version 1 and 2. */
702 struct dwarf2_section_info str;
703 struct dwarf2_section_info cu_index;
704 struct dwarf2_section_info tu_index;
705
706 /* These are only used by DWP version 2 files.
707 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
708 sections are referenced by section number, and are not recorded here.
709 In DWP version 2 there is at most one copy of all these sections, each
710 section being (effectively) comprised of the concatenation of all of the
711 individual sections that exist in the version 1 format.
712 To keep the code simple we treat each of these concatenated pieces as a
713 section itself (a virtual section?). */
714 struct dwarf2_section_info abbrev;
715 struct dwarf2_section_info info;
716 struct dwarf2_section_info line;
717 struct dwarf2_section_info loc;
718 struct dwarf2_section_info macinfo;
719 struct dwarf2_section_info macro;
720 struct dwarf2_section_info str_offsets;
721 struct dwarf2_section_info types;
722 };
723
724 /* These sections are what may appear in a virtual DWO file in DWP version 1.
725 A virtual DWO file is a DWO file as it appears in a DWP file. */
726
727 struct virtual_v1_dwo_sections
728 {
729 struct dwarf2_section_info abbrev;
730 struct dwarf2_section_info line;
731 struct dwarf2_section_info loc;
732 struct dwarf2_section_info macinfo;
733 struct dwarf2_section_info macro;
734 struct dwarf2_section_info str_offsets;
735 /* Each DWP hash table entry records one CU or one TU.
736 That is recorded here, and copied to dwo_unit.section. */
737 struct dwarf2_section_info info_or_types;
738 };
739
740 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
741 In version 2, the sections of the DWO files are concatenated together
742 and stored in one section of that name. Thus each ELF section contains
743 several "virtual" sections. */
744
745 struct virtual_v2_dwo_sections
746 {
747 bfd_size_type abbrev_offset;
748 bfd_size_type abbrev_size;
749
750 bfd_size_type line_offset;
751 bfd_size_type line_size;
752
753 bfd_size_type loc_offset;
754 bfd_size_type loc_size;
755
756 bfd_size_type macinfo_offset;
757 bfd_size_type macinfo_size;
758
759 bfd_size_type macro_offset;
760 bfd_size_type macro_size;
761
762 bfd_size_type str_offsets_offset;
763 bfd_size_type str_offsets_size;
764
765 /* Each DWP hash table entry records one CU or one TU.
766 That is recorded here, and copied to dwo_unit.section. */
767 bfd_size_type info_or_types_offset;
768 bfd_size_type info_or_types_size;
769 };
770
771 /* Contents of DWP hash tables. */
772
773 struct dwp_hash_table
774 {
775 uint32_t version, nr_columns;
776 uint32_t nr_units, nr_slots;
777 const gdb_byte *hash_table, *unit_table;
778 union
779 {
780 struct
781 {
782 const gdb_byte *indices;
783 } v1;
784 struct
785 {
786 /* This is indexed by column number and gives the id of the section
787 in that column. */
788 #define MAX_NR_V2_DWO_SECTIONS \
789 (1 /* .debug_info or .debug_types */ \
790 + 1 /* .debug_abbrev */ \
791 + 1 /* .debug_line */ \
792 + 1 /* .debug_loc */ \
793 + 1 /* .debug_str_offsets */ \
794 + 1 /* .debug_macro or .debug_macinfo */)
795 int section_ids[MAX_NR_V2_DWO_SECTIONS];
796 const gdb_byte *offsets;
797 const gdb_byte *sizes;
798 } v2;
799 } section_pool;
800 };
801
802 /* Data for one DWP file. */
803
804 struct dwp_file
805 {
806 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
807 : name (name_),
808 dbfd (std::move (abfd))
809 {
810 }
811
812 /* Name of the file. */
813 const char *name;
814
815 /* File format version. */
816 int version = 0;
817
818 /* The bfd. */
819 gdb_bfd_ref_ptr dbfd;
820
821 /* Section info for this file. */
822 struct dwp_sections sections {};
823
824 /* Table of CUs in the file. */
825 const struct dwp_hash_table *cus = nullptr;
826
827 /* Table of TUs in the file. */
828 const struct dwp_hash_table *tus = nullptr;
829
830 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
831 htab_up loaded_cus;
832 htab_up loaded_tus;
833
834 /* Table to map ELF section numbers to their sections.
835 This is only needed for the DWP V1 file format. */
836 unsigned int num_sections = 0;
837 asection **elf_sections = nullptr;
838 };
839
840 /* Struct used to pass misc. parameters to read_die_and_children, et
841 al. which are used for both .debug_info and .debug_types dies.
842 All parameters here are unchanging for the life of the call. This
843 struct exists to abstract away the constant parameters of die reading. */
844
845 struct die_reader_specs
846 {
847 /* The bfd of die_section. */
848 bfd* abfd;
849
850 /* The CU of the DIE we are parsing. */
851 struct dwarf2_cu *cu;
852
853 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
854 struct dwo_file *dwo_file;
855
856 /* The section the die comes from.
857 This is either .debug_info or .debug_types, or the .dwo variants. */
858 struct dwarf2_section_info *die_section;
859
860 /* die_section->buffer. */
861 const gdb_byte *buffer;
862
863 /* The end of the buffer. */
864 const gdb_byte *buffer_end;
865
866 /* The abbreviation table to use when reading the DIEs. */
867 struct abbrev_table *abbrev_table;
868 };
869
870 /* A subclass of die_reader_specs that holds storage and has complex
871 constructor and destructor behavior. */
872
873 class cutu_reader : public die_reader_specs
874 {
875 public:
876
877 cutu_reader (struct dwarf2_per_cu_data *this_cu,
878 struct abbrev_table *abbrev_table,
879 int use_existing_cu,
880 bool skip_partial);
881
882 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
883 struct dwarf2_cu *parent_cu = nullptr,
884 struct dwo_file *dwo_file = nullptr);
885
886 DISABLE_COPY_AND_ASSIGN (cutu_reader);
887
888 const gdb_byte *info_ptr = nullptr;
889 struct die_info *comp_unit_die = nullptr;
890 bool dummy_p = false;
891
892 /* Release the new CU, putting it on the chain. This cannot be done
893 for dummy CUs. */
894 void keep ();
895
896 private:
897 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
898 int use_existing_cu);
899
900 struct dwarf2_per_cu_data *m_this_cu;
901 std::unique_ptr<dwarf2_cu> m_new_cu;
902
903 /* The ordinary abbreviation table. */
904 abbrev_table_up m_abbrev_table_holder;
905
906 /* The DWO abbreviation table. */
907 abbrev_table_up m_dwo_abbrev_table;
908 };
909
910 /* When we construct a partial symbol table entry we only
911 need this much information. */
912 struct partial_die_info : public allocate_on_obstack
913 {
914 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
915
916 /* Disable assign but still keep copy ctor, which is needed
917 load_partial_dies. */
918 partial_die_info& operator=(const partial_die_info& rhs) = delete;
919
920 /* Adjust the partial die before generating a symbol for it. This
921 function may set the is_external flag or change the DIE's
922 name. */
923 void fixup (struct dwarf2_cu *cu);
924
925 /* Read a minimal amount of information into the minimal die
926 structure. */
927 const gdb_byte *read (const struct die_reader_specs *reader,
928 const struct abbrev_info &abbrev,
929 const gdb_byte *info_ptr);
930
931 /* Offset of this DIE. */
932 const sect_offset sect_off;
933
934 /* DWARF-2 tag for this DIE. */
935 const ENUM_BITFIELD(dwarf_tag) tag : 16;
936
937 /* Assorted flags describing the data found in this DIE. */
938 const unsigned int has_children : 1;
939
940 unsigned int is_external : 1;
941 unsigned int is_declaration : 1;
942 unsigned int has_type : 1;
943 unsigned int has_specification : 1;
944 unsigned int has_pc_info : 1;
945 unsigned int may_be_inlined : 1;
946
947 /* This DIE has been marked DW_AT_main_subprogram. */
948 unsigned int main_subprogram : 1;
949
950 /* Flag set if the SCOPE field of this structure has been
951 computed. */
952 unsigned int scope_set : 1;
953
954 /* Flag set if the DIE has a byte_size attribute. */
955 unsigned int has_byte_size : 1;
956
957 /* Flag set if the DIE has a DW_AT_const_value attribute. */
958 unsigned int has_const_value : 1;
959
960 /* Flag set if any of the DIE's children are template arguments. */
961 unsigned int has_template_arguments : 1;
962
963 /* Flag set if fixup has been called on this die. */
964 unsigned int fixup_called : 1;
965
966 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
967 unsigned int is_dwz : 1;
968
969 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
970 unsigned int spec_is_dwz : 1;
971
972 /* The name of this DIE. Normally the value of DW_AT_name, but
973 sometimes a default name for unnamed DIEs. */
974 const char *name = nullptr;
975
976 /* The linkage name, if present. */
977 const char *linkage_name = nullptr;
978
979 /* The scope to prepend to our children. This is generally
980 allocated on the comp_unit_obstack, so will disappear
981 when this compilation unit leaves the cache. */
982 const char *scope = nullptr;
983
984 /* Some data associated with the partial DIE. The tag determines
985 which field is live. */
986 union
987 {
988 /* The location description associated with this DIE, if any. */
989 struct dwarf_block *locdesc;
990 /* The offset of an import, for DW_TAG_imported_unit. */
991 sect_offset sect_off;
992 } d {};
993
994 /* If HAS_PC_INFO, the PC range associated with this DIE. */
995 CORE_ADDR lowpc = 0;
996 CORE_ADDR highpc = 0;
997
998 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
999 DW_AT_sibling, if any. */
1000 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1001 could return DW_AT_sibling values to its caller load_partial_dies. */
1002 const gdb_byte *sibling = nullptr;
1003
1004 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1005 DW_AT_specification (or DW_AT_abstract_origin or
1006 DW_AT_extension). */
1007 sect_offset spec_offset {};
1008
1009 /* Pointers to this DIE's parent, first child, and next sibling,
1010 if any. */
1011 struct partial_die_info *die_parent = nullptr;
1012 struct partial_die_info *die_child = nullptr;
1013 struct partial_die_info *die_sibling = nullptr;
1014
1015 friend struct partial_die_info *
1016 dwarf2_cu::find_partial_die (sect_offset sect_off);
1017
1018 private:
1019 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1020 partial_die_info (sect_offset sect_off)
1021 : partial_die_info (sect_off, DW_TAG_padding, 0)
1022 {
1023 }
1024
1025 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1026 int has_children_)
1027 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1028 {
1029 is_external = 0;
1030 is_declaration = 0;
1031 has_type = 0;
1032 has_specification = 0;
1033 has_pc_info = 0;
1034 may_be_inlined = 0;
1035 main_subprogram = 0;
1036 scope_set = 0;
1037 has_byte_size = 0;
1038 has_const_value = 0;
1039 has_template_arguments = 0;
1040 fixup_called = 0;
1041 is_dwz = 0;
1042 spec_is_dwz = 0;
1043 }
1044 };
1045
1046 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1047 but this would require a corresponding change in unpack_field_as_long
1048 and friends. */
1049 static int bits_per_byte = 8;
1050
1051 /* When reading a variant or variant part, we track a bit more
1052 information about the field, and store it in an object of this
1053 type. */
1054
1055 struct variant_field
1056 {
1057 /* If we see a DW_TAG_variant, then this will be the discriminant
1058 value. */
1059 ULONGEST discriminant_value;
1060 /* If we see a DW_TAG_variant, then this will be set if this is the
1061 default branch. */
1062 bool default_branch;
1063 /* While reading a DW_TAG_variant_part, this will be set if this
1064 field is the discriminant. */
1065 bool is_discriminant;
1066 };
1067
1068 struct nextfield
1069 {
1070 int accessibility = 0;
1071 int virtuality = 0;
1072 /* Extra information to describe a variant or variant part. */
1073 struct variant_field variant {};
1074 struct field field {};
1075 };
1076
1077 struct fnfieldlist
1078 {
1079 const char *name = nullptr;
1080 std::vector<struct fn_field> fnfields;
1081 };
1082
1083 /* The routines that read and process dies for a C struct or C++ class
1084 pass lists of data member fields and lists of member function fields
1085 in an instance of a field_info structure, as defined below. */
1086 struct field_info
1087 {
1088 /* List of data member and baseclasses fields. */
1089 std::vector<struct nextfield> fields;
1090 std::vector<struct nextfield> baseclasses;
1091
1092 /* Set if the accessibility of one of the fields is not public. */
1093 int non_public_fields = 0;
1094
1095 /* Member function fieldlist array, contains name of possibly overloaded
1096 member function, number of overloaded member functions and a pointer
1097 to the head of the member function field chain. */
1098 std::vector<struct fnfieldlist> fnfieldlists;
1099
1100 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1101 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1102 std::vector<struct decl_field> typedef_field_list;
1103
1104 /* Nested types defined by this class and the number of elements in this
1105 list. */
1106 std::vector<struct decl_field> nested_types_list;
1107
1108 /* Return the total number of fields (including baseclasses). */
1109 int nfields () const
1110 {
1111 return fields.size () + baseclasses.size ();
1112 }
1113 };
1114
1115 /* Loaded secondary compilation units are kept in memory until they
1116 have not been referenced for the processing of this many
1117 compilation units. Set this to zero to disable caching. Cache
1118 sizes of up to at least twenty will improve startup time for
1119 typical inter-CU-reference binaries, at an obvious memory cost. */
1120 static int dwarf_max_cache_age = 5;
1121 static void
1122 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1123 struct cmd_list_element *c, const char *value)
1124 {
1125 fprintf_filtered (file, _("The upper bound on the age of cached "
1126 "DWARF compilation units is %s.\n"),
1127 value);
1128 }
1129 \f
1130 /* local function prototypes */
1131
1132 static void dwarf2_find_base_address (struct die_info *die,
1133 struct dwarf2_cu *cu);
1134
1135 static dwarf2_psymtab *create_partial_symtab
1136 (struct dwarf2_per_cu_data *per_cu, const char *name);
1137
1138 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1139 const gdb_byte *info_ptr,
1140 struct die_info *type_unit_die);
1141
1142 static void dwarf2_build_psymtabs_hard
1143 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1144
1145 static void scan_partial_symbols (struct partial_die_info *,
1146 CORE_ADDR *, CORE_ADDR *,
1147 int, struct dwarf2_cu *);
1148
1149 static void add_partial_symbol (struct partial_die_info *,
1150 struct dwarf2_cu *);
1151
1152 static void add_partial_namespace (struct partial_die_info *pdi,
1153 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1154 int set_addrmap, struct dwarf2_cu *cu);
1155
1156 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1157 CORE_ADDR *highpc, int set_addrmap,
1158 struct dwarf2_cu *cu);
1159
1160 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1161 struct dwarf2_cu *cu);
1162
1163 static void add_partial_subprogram (struct partial_die_info *pdi,
1164 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1165 int need_pc, struct dwarf2_cu *cu);
1166
1167 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1168
1169 static struct partial_die_info *load_partial_dies
1170 (const struct die_reader_specs *, const gdb_byte *, int);
1171
1172 /* A pair of partial_die_info and compilation unit. */
1173 struct cu_partial_die_info
1174 {
1175 /* The compilation unit of the partial_die_info. */
1176 struct dwarf2_cu *cu;
1177 /* A partial_die_info. */
1178 struct partial_die_info *pdi;
1179
1180 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1181 : cu (cu),
1182 pdi (pdi)
1183 { /* Nothing. */ }
1184
1185 private:
1186 cu_partial_die_info () = delete;
1187 };
1188
1189 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1190 struct dwarf2_cu *);
1191
1192 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1193 struct attribute *, struct attr_abbrev *,
1194 const gdb_byte *, bool *need_reprocess);
1195
1196 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1197 struct attribute *attr);
1198
1199 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1200
1201 static sect_offset read_abbrev_offset
1202 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1203 struct dwarf2_section_info *, sect_offset);
1204
1205 static const char *read_indirect_string
1206 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1207 const struct comp_unit_head *, unsigned int *);
1208
1209 static const char *read_indirect_string_at_offset
1210 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1211
1212 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1213 const gdb_byte *,
1214 unsigned int *);
1215
1216 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1217 ULONGEST str_index);
1218
1219 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1220 ULONGEST str_index);
1221
1222 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1223
1224 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1225 struct dwarf2_cu *);
1226
1227 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1228 unsigned int);
1229
1230 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1231 struct dwarf2_cu *cu);
1232
1233 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1234
1235 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1236 struct dwarf2_cu *cu);
1237
1238 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1239
1240 static struct die_info *die_specification (struct die_info *die,
1241 struct dwarf2_cu **);
1242
1243 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1244 struct dwarf2_cu *cu);
1245
1246 static void dwarf_decode_lines (struct line_header *, const char *,
1247 struct dwarf2_cu *, dwarf2_psymtab *,
1248 CORE_ADDR, int decode_mapping);
1249
1250 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1251 const char *);
1252
1253 static struct symbol *new_symbol (struct die_info *, struct type *,
1254 struct dwarf2_cu *, struct symbol * = NULL);
1255
1256 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1257 struct dwarf2_cu *);
1258
1259 static void dwarf2_const_value_attr (const struct attribute *attr,
1260 struct type *type,
1261 const char *name,
1262 struct obstack *obstack,
1263 struct dwarf2_cu *cu, LONGEST *value,
1264 const gdb_byte **bytes,
1265 struct dwarf2_locexpr_baton **baton);
1266
1267 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1268
1269 static int need_gnat_info (struct dwarf2_cu *);
1270
1271 static struct type *die_descriptive_type (struct die_info *,
1272 struct dwarf2_cu *);
1273
1274 static void set_descriptive_type (struct type *, struct die_info *,
1275 struct dwarf2_cu *);
1276
1277 static struct type *die_containing_type (struct die_info *,
1278 struct dwarf2_cu *);
1279
1280 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1281 struct dwarf2_cu *);
1282
1283 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1284
1285 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1286
1287 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1288
1289 static char *typename_concat (struct obstack *obs, const char *prefix,
1290 const char *suffix, int physname,
1291 struct dwarf2_cu *cu);
1292
1293 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1294
1295 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1296
1297 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1298
1299 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1300
1301 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1302
1303 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1304
1305 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1306 struct dwarf2_cu *, dwarf2_psymtab *);
1307
1308 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1309 values. Keep the items ordered with increasing constraints compliance. */
1310 enum pc_bounds_kind
1311 {
1312 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1313 PC_BOUNDS_NOT_PRESENT,
1314
1315 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1316 were present but they do not form a valid range of PC addresses. */
1317 PC_BOUNDS_INVALID,
1318
1319 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1320 PC_BOUNDS_RANGES,
1321
1322 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1323 PC_BOUNDS_HIGH_LOW,
1324 };
1325
1326 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1327 CORE_ADDR *, CORE_ADDR *,
1328 struct dwarf2_cu *,
1329 dwarf2_psymtab *);
1330
1331 static void get_scope_pc_bounds (struct die_info *,
1332 CORE_ADDR *, CORE_ADDR *,
1333 struct dwarf2_cu *);
1334
1335 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1336 CORE_ADDR, struct dwarf2_cu *);
1337
1338 static void dwarf2_add_field (struct field_info *, struct die_info *,
1339 struct dwarf2_cu *);
1340
1341 static void dwarf2_attach_fields_to_type (struct field_info *,
1342 struct type *, struct dwarf2_cu *);
1343
1344 static void dwarf2_add_member_fn (struct field_info *,
1345 struct die_info *, struct type *,
1346 struct dwarf2_cu *);
1347
1348 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1349 struct type *,
1350 struct dwarf2_cu *);
1351
1352 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1353
1354 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1355
1356 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1357
1358 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1359
1360 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1361
1362 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1363
1364 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1365
1366 static struct type *read_module_type (struct die_info *die,
1367 struct dwarf2_cu *cu);
1368
1369 static const char *namespace_name (struct die_info *die,
1370 int *is_anonymous, struct dwarf2_cu *);
1371
1372 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1373
1374 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1375
1376 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1377 struct dwarf2_cu *);
1378
1379 static struct die_info *read_die_and_siblings_1
1380 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1381 struct die_info *);
1382
1383 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1384 const gdb_byte *info_ptr,
1385 const gdb_byte **new_info_ptr,
1386 struct die_info *parent);
1387
1388 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1389 struct die_info **, const gdb_byte *,
1390 int);
1391
1392 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1393 struct die_info **, const gdb_byte *);
1394
1395 static void process_die (struct die_info *, struct dwarf2_cu *);
1396
1397 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1398 struct objfile *);
1399
1400 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1401
1402 static const char *dwarf2_full_name (const char *name,
1403 struct die_info *die,
1404 struct dwarf2_cu *cu);
1405
1406 static const char *dwarf2_physname (const char *name, struct die_info *die,
1407 struct dwarf2_cu *cu);
1408
1409 static struct die_info *dwarf2_extension (struct die_info *die,
1410 struct dwarf2_cu **);
1411
1412 static const char *dwarf_tag_name (unsigned int);
1413
1414 static const char *dwarf_attr_name (unsigned int);
1415
1416 static const char *dwarf_form_name (unsigned int);
1417
1418 static const char *dwarf_bool_name (unsigned int);
1419
1420 static const char *dwarf_type_encoding_name (unsigned int);
1421
1422 static struct die_info *sibling_die (struct die_info *);
1423
1424 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1425
1426 static void dump_die_for_error (struct die_info *);
1427
1428 static void dump_die_1 (struct ui_file *, int level, int max_level,
1429 struct die_info *);
1430
1431 /*static*/ void dump_die (struct die_info *, int max_level);
1432
1433 static void store_in_ref_table (struct die_info *,
1434 struct dwarf2_cu *);
1435
1436 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1437
1438 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1439
1440 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1441 const struct attribute *,
1442 struct dwarf2_cu **);
1443
1444 static struct die_info *follow_die_ref (struct die_info *,
1445 const struct attribute *,
1446 struct dwarf2_cu **);
1447
1448 static struct die_info *follow_die_sig (struct die_info *,
1449 const struct attribute *,
1450 struct dwarf2_cu **);
1451
1452 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1453 struct dwarf2_cu *);
1454
1455 static struct type *get_DW_AT_signature_type (struct die_info *,
1456 const struct attribute *,
1457 struct dwarf2_cu *);
1458
1459 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1460
1461 static void read_signatured_type (struct signatured_type *);
1462
1463 static int attr_to_dynamic_prop (const struct attribute *attr,
1464 struct die_info *die, struct dwarf2_cu *cu,
1465 struct dynamic_prop *prop, struct type *type);
1466
1467 /* memory allocation interface */
1468
1469 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1470
1471 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1472
1473 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1474
1475 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1476 struct dwarf2_loclist_baton *baton,
1477 const struct attribute *attr);
1478
1479 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1480 struct symbol *sym,
1481 struct dwarf2_cu *cu,
1482 int is_block);
1483
1484 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1485 const gdb_byte *info_ptr,
1486 struct abbrev_info *abbrev);
1487
1488 static hashval_t partial_die_hash (const void *item);
1489
1490 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1491
1492 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1493 (sect_offset sect_off, unsigned int offset_in_dwz,
1494 struct dwarf2_per_objfile *dwarf2_per_objfile);
1495
1496 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1497 struct die_info *comp_unit_die,
1498 enum language pretend_language);
1499
1500 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1501
1502 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1503
1504 static struct type *set_die_type (struct die_info *, struct type *,
1505 struct dwarf2_cu *);
1506
1507 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1508
1509 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1510
1511 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1512 enum language);
1513
1514 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1515 enum language);
1516
1517 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1518 enum language);
1519
1520 static void dwarf2_add_dependence (struct dwarf2_cu *,
1521 struct dwarf2_per_cu_data *);
1522
1523 static void dwarf2_mark (struct dwarf2_cu *);
1524
1525 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1526
1527 static struct type *get_die_type_at_offset (sect_offset,
1528 struct dwarf2_per_cu_data *);
1529
1530 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1531
1532 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1533 enum language pretend_language);
1534
1535 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1536
1537 /* Class, the destructor of which frees all allocated queue entries. This
1538 will only have work to do if an error was thrown while processing the
1539 dwarf. If no error was thrown then the queue entries should have all
1540 been processed, and freed, as we went along. */
1541
1542 class dwarf2_queue_guard
1543 {
1544 public:
1545 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1546 : m_per_objfile (per_objfile)
1547 {
1548 }
1549
1550 /* Free any entries remaining on the queue. There should only be
1551 entries left if we hit an error while processing the dwarf. */
1552 ~dwarf2_queue_guard ()
1553 {
1554 /* Ensure that no memory is allocated by the queue. */
1555 std::queue<dwarf2_queue_item> empty;
1556 std::swap (m_per_objfile->queue, empty);
1557 }
1558
1559 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1560
1561 private:
1562 dwarf2_per_objfile *m_per_objfile;
1563 };
1564
1565 dwarf2_queue_item::~dwarf2_queue_item ()
1566 {
1567 /* Anything still marked queued is likely to be in an
1568 inconsistent state, so discard it. */
1569 if (per_cu->queued)
1570 {
1571 if (per_cu->cu != NULL)
1572 free_one_cached_comp_unit (per_cu);
1573 per_cu->queued = 0;
1574 }
1575 }
1576
1577 /* The return type of find_file_and_directory. Note, the enclosed
1578 string pointers are only valid while this object is valid. */
1579
1580 struct file_and_directory
1581 {
1582 /* The filename. This is never NULL. */
1583 const char *name;
1584
1585 /* The compilation directory. NULL if not known. If we needed to
1586 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1587 points directly to the DW_AT_comp_dir string attribute owned by
1588 the obstack that owns the DIE. */
1589 const char *comp_dir;
1590
1591 /* If we needed to build a new string for comp_dir, this is what
1592 owns the storage. */
1593 std::string comp_dir_storage;
1594 };
1595
1596 static file_and_directory find_file_and_directory (struct die_info *die,
1597 struct dwarf2_cu *cu);
1598
1599 static htab_up allocate_signatured_type_table ();
1600
1601 static htab_up allocate_dwo_unit_table ();
1602
1603 static struct dwo_unit *lookup_dwo_unit_in_dwp
1604 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1605 struct dwp_file *dwp_file, const char *comp_dir,
1606 ULONGEST signature, int is_debug_types);
1607
1608 static struct dwp_file *get_dwp_file
1609 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1610
1611 static struct dwo_unit *lookup_dwo_comp_unit
1612 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1613
1614 static struct dwo_unit *lookup_dwo_type_unit
1615 (struct signatured_type *, const char *, const char *);
1616
1617 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1618
1619 /* A unique pointer to a dwo_file. */
1620
1621 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1622
1623 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1624
1625 static void check_producer (struct dwarf2_cu *cu);
1626
1627 static void free_line_header_voidp (void *arg);
1628 \f
1629 /* Various complaints about symbol reading that don't abort the process. */
1630
1631 static void
1632 dwarf2_debug_line_missing_file_complaint (void)
1633 {
1634 complaint (_(".debug_line section has line data without a file"));
1635 }
1636
1637 static void
1638 dwarf2_debug_line_missing_end_sequence_complaint (void)
1639 {
1640 complaint (_(".debug_line section has line "
1641 "program sequence without an end"));
1642 }
1643
1644 static void
1645 dwarf2_complex_location_expr_complaint (void)
1646 {
1647 complaint (_("location expression too complex"));
1648 }
1649
1650 static void
1651 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1652 int arg3)
1653 {
1654 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1655 arg1, arg2, arg3);
1656 }
1657
1658 static void
1659 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1660 {
1661 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1662 arg1, arg2);
1663 }
1664
1665 /* Hash function for line_header_hash. */
1666
1667 static hashval_t
1668 line_header_hash (const struct line_header *ofs)
1669 {
1670 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1671 }
1672
1673 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1674
1675 static hashval_t
1676 line_header_hash_voidp (const void *item)
1677 {
1678 const struct line_header *ofs = (const struct line_header *) item;
1679
1680 return line_header_hash (ofs);
1681 }
1682
1683 /* Equality function for line_header_hash. */
1684
1685 static int
1686 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1687 {
1688 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1689 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1690
1691 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1692 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1693 }
1694
1695 \f
1696
1697 /* See declaration. */
1698
1699 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1700 const dwarf2_debug_sections *names,
1701 bool can_copy_)
1702 : objfile (objfile_),
1703 can_copy (can_copy_)
1704 {
1705 if (names == NULL)
1706 names = &dwarf2_elf_names;
1707
1708 bfd *obfd = objfile->obfd;
1709
1710 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1711 locate_sections (obfd, sec, *names);
1712 }
1713
1714 dwarf2_per_objfile::~dwarf2_per_objfile ()
1715 {
1716 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1717 free_cached_comp_units ();
1718
1719 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1720 per_cu->imported_symtabs_free ();
1721
1722 for (signatured_type *sig_type : all_type_units)
1723 sig_type->per_cu.imported_symtabs_free ();
1724
1725 /* Everything else should be on the objfile obstack. */
1726 }
1727
1728 /* See declaration. */
1729
1730 void
1731 dwarf2_per_objfile::free_cached_comp_units ()
1732 {
1733 dwarf2_per_cu_data *per_cu = read_in_chain;
1734 dwarf2_per_cu_data **last_chain = &read_in_chain;
1735 while (per_cu != NULL)
1736 {
1737 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1738
1739 delete per_cu->cu;
1740 *last_chain = next_cu;
1741 per_cu = next_cu;
1742 }
1743 }
1744
1745 /* A helper class that calls free_cached_comp_units on
1746 destruction. */
1747
1748 class free_cached_comp_units
1749 {
1750 public:
1751
1752 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1753 : m_per_objfile (per_objfile)
1754 {
1755 }
1756
1757 ~free_cached_comp_units ()
1758 {
1759 m_per_objfile->free_cached_comp_units ();
1760 }
1761
1762 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1763
1764 private:
1765
1766 dwarf2_per_objfile *m_per_objfile;
1767 };
1768
1769 /* Try to locate the sections we need for DWARF 2 debugging
1770 information and return true if we have enough to do something.
1771 NAMES points to the dwarf2 section names, or is NULL if the standard
1772 ELF names are used. CAN_COPY is true for formats where symbol
1773 interposition is possible and so symbol values must follow copy
1774 relocation rules. */
1775
1776 int
1777 dwarf2_has_info (struct objfile *objfile,
1778 const struct dwarf2_debug_sections *names,
1779 bool can_copy)
1780 {
1781 if (objfile->flags & OBJF_READNEVER)
1782 return 0;
1783
1784 struct dwarf2_per_objfile *dwarf2_per_objfile
1785 = get_dwarf2_per_objfile (objfile);
1786
1787 if (dwarf2_per_objfile == NULL)
1788 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1789 names,
1790 can_copy);
1791
1792 return (!dwarf2_per_objfile->info.is_virtual
1793 && dwarf2_per_objfile->info.s.section != NULL
1794 && !dwarf2_per_objfile->abbrev.is_virtual
1795 && dwarf2_per_objfile->abbrev.s.section != NULL);
1796 }
1797
1798 /* When loading sections, we look either for uncompressed section or for
1799 compressed section names. */
1800
1801 static int
1802 section_is_p (const char *section_name,
1803 const struct dwarf2_section_names *names)
1804 {
1805 if (names->normal != NULL
1806 && strcmp (section_name, names->normal) == 0)
1807 return 1;
1808 if (names->compressed != NULL
1809 && strcmp (section_name, names->compressed) == 0)
1810 return 1;
1811 return 0;
1812 }
1813
1814 /* See declaration. */
1815
1816 void
1817 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1818 const dwarf2_debug_sections &names)
1819 {
1820 flagword aflag = bfd_section_flags (sectp);
1821
1822 if ((aflag & SEC_HAS_CONTENTS) == 0)
1823 {
1824 }
1825 else if (elf_section_data (sectp)->this_hdr.sh_size
1826 > bfd_get_file_size (abfd))
1827 {
1828 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1829 warning (_("Discarding section %s which has a section size (%s"
1830 ") larger than the file size [in module %s]"),
1831 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1832 bfd_get_filename (abfd));
1833 }
1834 else if (section_is_p (sectp->name, &names.info))
1835 {
1836 this->info.s.section = sectp;
1837 this->info.size = bfd_section_size (sectp);
1838 }
1839 else if (section_is_p (sectp->name, &names.abbrev))
1840 {
1841 this->abbrev.s.section = sectp;
1842 this->abbrev.size = bfd_section_size (sectp);
1843 }
1844 else if (section_is_p (sectp->name, &names.line))
1845 {
1846 this->line.s.section = sectp;
1847 this->line.size = bfd_section_size (sectp);
1848 }
1849 else if (section_is_p (sectp->name, &names.loc))
1850 {
1851 this->loc.s.section = sectp;
1852 this->loc.size = bfd_section_size (sectp);
1853 }
1854 else if (section_is_p (sectp->name, &names.loclists))
1855 {
1856 this->loclists.s.section = sectp;
1857 this->loclists.size = bfd_section_size (sectp);
1858 }
1859 else if (section_is_p (sectp->name, &names.macinfo))
1860 {
1861 this->macinfo.s.section = sectp;
1862 this->macinfo.size = bfd_section_size (sectp);
1863 }
1864 else if (section_is_p (sectp->name, &names.macro))
1865 {
1866 this->macro.s.section = sectp;
1867 this->macro.size = bfd_section_size (sectp);
1868 }
1869 else if (section_is_p (sectp->name, &names.str))
1870 {
1871 this->str.s.section = sectp;
1872 this->str.size = bfd_section_size (sectp);
1873 }
1874 else if (section_is_p (sectp->name, &names.str_offsets))
1875 {
1876 this->str_offsets.s.section = sectp;
1877 this->str_offsets.size = bfd_section_size (sectp);
1878 }
1879 else if (section_is_p (sectp->name, &names.line_str))
1880 {
1881 this->line_str.s.section = sectp;
1882 this->line_str.size = bfd_section_size (sectp);
1883 }
1884 else if (section_is_p (sectp->name, &names.addr))
1885 {
1886 this->addr.s.section = sectp;
1887 this->addr.size = bfd_section_size (sectp);
1888 }
1889 else if (section_is_p (sectp->name, &names.frame))
1890 {
1891 this->frame.s.section = sectp;
1892 this->frame.size = bfd_section_size (sectp);
1893 }
1894 else if (section_is_p (sectp->name, &names.eh_frame))
1895 {
1896 this->eh_frame.s.section = sectp;
1897 this->eh_frame.size = bfd_section_size (sectp);
1898 }
1899 else if (section_is_p (sectp->name, &names.ranges))
1900 {
1901 this->ranges.s.section = sectp;
1902 this->ranges.size = bfd_section_size (sectp);
1903 }
1904 else if (section_is_p (sectp->name, &names.rnglists))
1905 {
1906 this->rnglists.s.section = sectp;
1907 this->rnglists.size = bfd_section_size (sectp);
1908 }
1909 else if (section_is_p (sectp->name, &names.types))
1910 {
1911 struct dwarf2_section_info type_section;
1912
1913 memset (&type_section, 0, sizeof (type_section));
1914 type_section.s.section = sectp;
1915 type_section.size = bfd_section_size (sectp);
1916
1917 this->types.push_back (type_section);
1918 }
1919 else if (section_is_p (sectp->name, &names.gdb_index))
1920 {
1921 this->gdb_index.s.section = sectp;
1922 this->gdb_index.size = bfd_section_size (sectp);
1923 }
1924 else if (section_is_p (sectp->name, &names.debug_names))
1925 {
1926 this->debug_names.s.section = sectp;
1927 this->debug_names.size = bfd_section_size (sectp);
1928 }
1929 else if (section_is_p (sectp->name, &names.debug_aranges))
1930 {
1931 this->debug_aranges.s.section = sectp;
1932 this->debug_aranges.size = bfd_section_size (sectp);
1933 }
1934
1935 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1936 && bfd_section_vma (sectp) == 0)
1937 this->has_section_at_zero = true;
1938 }
1939
1940 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1941 SECTION_NAME. */
1942
1943 void
1944 dwarf2_get_section_info (struct objfile *objfile,
1945 enum dwarf2_section_enum sect,
1946 asection **sectp, const gdb_byte **bufp,
1947 bfd_size_type *sizep)
1948 {
1949 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
1950 struct dwarf2_section_info *info;
1951
1952 /* We may see an objfile without any DWARF, in which case we just
1953 return nothing. */
1954 if (data == NULL)
1955 {
1956 *sectp = NULL;
1957 *bufp = NULL;
1958 *sizep = 0;
1959 return;
1960 }
1961 switch (sect)
1962 {
1963 case DWARF2_DEBUG_FRAME:
1964 info = &data->frame;
1965 break;
1966 case DWARF2_EH_FRAME:
1967 info = &data->eh_frame;
1968 break;
1969 default:
1970 gdb_assert_not_reached ("unexpected section");
1971 }
1972
1973 info->read (objfile);
1974
1975 *sectp = info->get_bfd_section ();
1976 *bufp = info->buffer;
1977 *sizep = info->size;
1978 }
1979
1980 /* A helper function to find the sections for a .dwz file. */
1981
1982 static void
1983 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1984 {
1985 struct dwz_file *dwz_file = (struct dwz_file *) arg;
1986
1987 /* Note that we only support the standard ELF names, because .dwz
1988 is ELF-only (at the time of writing). */
1989 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1990 {
1991 dwz_file->abbrev.s.section = sectp;
1992 dwz_file->abbrev.size = bfd_section_size (sectp);
1993 }
1994 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1995 {
1996 dwz_file->info.s.section = sectp;
1997 dwz_file->info.size = bfd_section_size (sectp);
1998 }
1999 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2000 {
2001 dwz_file->str.s.section = sectp;
2002 dwz_file->str.size = bfd_section_size (sectp);
2003 }
2004 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2005 {
2006 dwz_file->line.s.section = sectp;
2007 dwz_file->line.size = bfd_section_size (sectp);
2008 }
2009 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2010 {
2011 dwz_file->macro.s.section = sectp;
2012 dwz_file->macro.size = bfd_section_size (sectp);
2013 }
2014 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2015 {
2016 dwz_file->gdb_index.s.section = sectp;
2017 dwz_file->gdb_index.size = bfd_section_size (sectp);
2018 }
2019 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2020 {
2021 dwz_file->debug_names.s.section = sectp;
2022 dwz_file->debug_names.size = bfd_section_size (sectp);
2023 }
2024 }
2025
2026 /* See dwarf2read.h. */
2027
2028 struct dwz_file *
2029 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2030 {
2031 const char *filename;
2032 bfd_size_type buildid_len_arg;
2033 size_t buildid_len;
2034 bfd_byte *buildid;
2035
2036 if (dwarf2_per_objfile->dwz_file != NULL)
2037 return dwarf2_per_objfile->dwz_file.get ();
2038
2039 bfd_set_error (bfd_error_no_error);
2040 gdb::unique_xmalloc_ptr<char> data
2041 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2042 &buildid_len_arg, &buildid));
2043 if (data == NULL)
2044 {
2045 if (bfd_get_error () == bfd_error_no_error)
2046 return NULL;
2047 error (_("could not read '.gnu_debugaltlink' section: %s"),
2048 bfd_errmsg (bfd_get_error ()));
2049 }
2050
2051 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2052
2053 buildid_len = (size_t) buildid_len_arg;
2054
2055 filename = data.get ();
2056
2057 std::string abs_storage;
2058 if (!IS_ABSOLUTE_PATH (filename))
2059 {
2060 gdb::unique_xmalloc_ptr<char> abs
2061 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2062
2063 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2064 filename = abs_storage.c_str ();
2065 }
2066
2067 /* First try the file name given in the section. If that doesn't
2068 work, try to use the build-id instead. */
2069 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2070 if (dwz_bfd != NULL)
2071 {
2072 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2073 dwz_bfd.reset (nullptr);
2074 }
2075
2076 if (dwz_bfd == NULL)
2077 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2078
2079 if (dwz_bfd == nullptr)
2080 {
2081 gdb::unique_xmalloc_ptr<char> alt_filename;
2082 const char *origname = dwarf2_per_objfile->objfile->original_name;
2083
2084 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2085 buildid_len,
2086 origname,
2087 &alt_filename));
2088
2089 if (fd.get () >= 0)
2090 {
2091 /* File successfully retrieved from server. */
2092 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2093
2094 if (dwz_bfd == nullptr)
2095 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2096 alt_filename.get ());
2097 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2098 dwz_bfd.reset (nullptr);
2099 }
2100 }
2101
2102 if (dwz_bfd == NULL)
2103 error (_("could not find '.gnu_debugaltlink' file for %s"),
2104 objfile_name (dwarf2_per_objfile->objfile));
2105
2106 std::unique_ptr<struct dwz_file> result
2107 (new struct dwz_file (std::move (dwz_bfd)));
2108
2109 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2110 result.get ());
2111
2112 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2113 result->dwz_bfd.get ());
2114 dwarf2_per_objfile->dwz_file = std::move (result);
2115 return dwarf2_per_objfile->dwz_file.get ();
2116 }
2117 \f
2118 /* DWARF quick_symbols_functions support. */
2119
2120 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2121 unique line tables, so we maintain a separate table of all .debug_line
2122 derived entries to support the sharing.
2123 All the quick functions need is the list of file names. We discard the
2124 line_header when we're done and don't need to record it here. */
2125 struct quick_file_names
2126 {
2127 /* The data used to construct the hash key. */
2128 struct stmt_list_hash hash;
2129
2130 /* The number of entries in file_names, real_names. */
2131 unsigned int num_file_names;
2132
2133 /* The file names from the line table, after being run through
2134 file_full_name. */
2135 const char **file_names;
2136
2137 /* The file names from the line table after being run through
2138 gdb_realpath. These are computed lazily. */
2139 const char **real_names;
2140 };
2141
2142 /* When using the index (and thus not using psymtabs), each CU has an
2143 object of this type. This is used to hold information needed by
2144 the various "quick" methods. */
2145 struct dwarf2_per_cu_quick_data
2146 {
2147 /* The file table. This can be NULL if there was no file table
2148 or it's currently not read in.
2149 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2150 struct quick_file_names *file_names;
2151
2152 /* The corresponding symbol table. This is NULL if symbols for this
2153 CU have not yet been read. */
2154 struct compunit_symtab *compunit_symtab;
2155
2156 /* A temporary mark bit used when iterating over all CUs in
2157 expand_symtabs_matching. */
2158 unsigned int mark : 1;
2159
2160 /* True if we've tried to read the file table and found there isn't one.
2161 There will be no point in trying to read it again next time. */
2162 unsigned int no_file_data : 1;
2163 };
2164
2165 /* Utility hash function for a stmt_list_hash. */
2166
2167 static hashval_t
2168 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2169 {
2170 hashval_t v = 0;
2171
2172 if (stmt_list_hash->dwo_unit != NULL)
2173 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2174 v += to_underlying (stmt_list_hash->line_sect_off);
2175 return v;
2176 }
2177
2178 /* Utility equality function for a stmt_list_hash. */
2179
2180 static int
2181 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2182 const struct stmt_list_hash *rhs)
2183 {
2184 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2185 return 0;
2186 if (lhs->dwo_unit != NULL
2187 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2188 return 0;
2189
2190 return lhs->line_sect_off == rhs->line_sect_off;
2191 }
2192
2193 /* Hash function for a quick_file_names. */
2194
2195 static hashval_t
2196 hash_file_name_entry (const void *e)
2197 {
2198 const struct quick_file_names *file_data
2199 = (const struct quick_file_names *) e;
2200
2201 return hash_stmt_list_entry (&file_data->hash);
2202 }
2203
2204 /* Equality function for a quick_file_names. */
2205
2206 static int
2207 eq_file_name_entry (const void *a, const void *b)
2208 {
2209 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2210 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2211
2212 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2213 }
2214
2215 /* Delete function for a quick_file_names. */
2216
2217 static void
2218 delete_file_name_entry (void *e)
2219 {
2220 struct quick_file_names *file_data = (struct quick_file_names *) e;
2221 int i;
2222
2223 for (i = 0; i < file_data->num_file_names; ++i)
2224 {
2225 xfree ((void*) file_data->file_names[i]);
2226 if (file_data->real_names)
2227 xfree ((void*) file_data->real_names[i]);
2228 }
2229
2230 /* The space for the struct itself lives on objfile_obstack,
2231 so we don't free it here. */
2232 }
2233
2234 /* Create a quick_file_names hash table. */
2235
2236 static htab_up
2237 create_quick_file_names_table (unsigned int nr_initial_entries)
2238 {
2239 return htab_up (htab_create_alloc (nr_initial_entries,
2240 hash_file_name_entry, eq_file_name_entry,
2241 delete_file_name_entry, xcalloc, xfree));
2242 }
2243
2244 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2245 have to be created afterwards. You should call age_cached_comp_units after
2246 processing PER_CU->CU. dw2_setup must have been already called. */
2247
2248 static void
2249 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2250 {
2251 if (per_cu->is_debug_types)
2252 load_full_type_unit (per_cu);
2253 else
2254 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2255
2256 if (per_cu->cu == NULL)
2257 return; /* Dummy CU. */
2258
2259 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2260 }
2261
2262 /* Read in the symbols for PER_CU. */
2263
2264 static void
2265 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2266 {
2267 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2268
2269 /* Skip type_unit_groups, reading the type units they contain
2270 is handled elsewhere. */
2271 if (per_cu->type_unit_group_p ())
2272 return;
2273
2274 /* The destructor of dwarf2_queue_guard frees any entries left on
2275 the queue. After this point we're guaranteed to leave this function
2276 with the dwarf queue empty. */
2277 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2278
2279 if (dwarf2_per_objfile->using_index
2280 ? per_cu->v.quick->compunit_symtab == NULL
2281 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2282 {
2283 queue_comp_unit (per_cu, language_minimal);
2284 load_cu (per_cu, skip_partial);
2285
2286 /* If we just loaded a CU from a DWO, and we're working with an index
2287 that may badly handle TUs, load all the TUs in that DWO as well.
2288 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2289 if (!per_cu->is_debug_types
2290 && per_cu->cu != NULL
2291 && per_cu->cu->dwo_unit != NULL
2292 && dwarf2_per_objfile->index_table != NULL
2293 && dwarf2_per_objfile->index_table->version <= 7
2294 /* DWP files aren't supported yet. */
2295 && get_dwp_file (dwarf2_per_objfile) == NULL)
2296 queue_and_load_all_dwo_tus (per_cu);
2297 }
2298
2299 process_queue (dwarf2_per_objfile);
2300
2301 /* Age the cache, releasing compilation units that have not
2302 been used recently. */
2303 age_cached_comp_units (dwarf2_per_objfile);
2304 }
2305
2306 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2307 the objfile from which this CU came. Returns the resulting symbol
2308 table. */
2309
2310 static struct compunit_symtab *
2311 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2312 {
2313 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2314
2315 gdb_assert (dwarf2_per_objfile->using_index);
2316 if (!per_cu->v.quick->compunit_symtab)
2317 {
2318 free_cached_comp_units freer (dwarf2_per_objfile);
2319 scoped_restore decrementer = increment_reading_symtab ();
2320 dw2_do_instantiate_symtab (per_cu, skip_partial);
2321 process_cu_includes (dwarf2_per_objfile);
2322 }
2323
2324 return per_cu->v.quick->compunit_symtab;
2325 }
2326
2327 /* See declaration. */
2328
2329 dwarf2_per_cu_data *
2330 dwarf2_per_objfile::get_cutu (int index)
2331 {
2332 if (index >= this->all_comp_units.size ())
2333 {
2334 index -= this->all_comp_units.size ();
2335 gdb_assert (index < this->all_type_units.size ());
2336 return &this->all_type_units[index]->per_cu;
2337 }
2338
2339 return this->all_comp_units[index];
2340 }
2341
2342 /* See declaration. */
2343
2344 dwarf2_per_cu_data *
2345 dwarf2_per_objfile::get_cu (int index)
2346 {
2347 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2348
2349 return this->all_comp_units[index];
2350 }
2351
2352 /* See declaration. */
2353
2354 signatured_type *
2355 dwarf2_per_objfile::get_tu (int index)
2356 {
2357 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2358
2359 return this->all_type_units[index];
2360 }
2361
2362 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2363 objfile_obstack, and constructed with the specified field
2364 values. */
2365
2366 static dwarf2_per_cu_data *
2367 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2368 struct dwarf2_section_info *section,
2369 int is_dwz,
2370 sect_offset sect_off, ULONGEST length)
2371 {
2372 struct objfile *objfile = dwarf2_per_objfile->objfile;
2373 dwarf2_per_cu_data *the_cu
2374 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2375 struct dwarf2_per_cu_data);
2376 the_cu->sect_off = sect_off;
2377 the_cu->length = length;
2378 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2379 the_cu->section = section;
2380 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2381 struct dwarf2_per_cu_quick_data);
2382 the_cu->is_dwz = is_dwz;
2383 return the_cu;
2384 }
2385
2386 /* A helper for create_cus_from_index that handles a given list of
2387 CUs. */
2388
2389 static void
2390 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2391 const gdb_byte *cu_list, offset_type n_elements,
2392 struct dwarf2_section_info *section,
2393 int is_dwz)
2394 {
2395 for (offset_type i = 0; i < n_elements; i += 2)
2396 {
2397 gdb_static_assert (sizeof (ULONGEST) >= 8);
2398
2399 sect_offset sect_off
2400 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2401 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2402 cu_list += 2 * 8;
2403
2404 dwarf2_per_cu_data *per_cu
2405 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2406 sect_off, length);
2407 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2408 }
2409 }
2410
2411 /* Read the CU list from the mapped index, and use it to create all
2412 the CU objects for this objfile. */
2413
2414 static void
2415 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2416 const gdb_byte *cu_list, offset_type cu_list_elements,
2417 const gdb_byte *dwz_list, offset_type dwz_elements)
2418 {
2419 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2420 dwarf2_per_objfile->all_comp_units.reserve
2421 ((cu_list_elements + dwz_elements) / 2);
2422
2423 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2424 &dwarf2_per_objfile->info, 0);
2425
2426 if (dwz_elements == 0)
2427 return;
2428
2429 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2430 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2431 &dwz->info, 1);
2432 }
2433
2434 /* Create the signatured type hash table from the index. */
2435
2436 static void
2437 create_signatured_type_table_from_index
2438 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2439 struct dwarf2_section_info *section,
2440 const gdb_byte *bytes,
2441 offset_type elements)
2442 {
2443 struct objfile *objfile = dwarf2_per_objfile->objfile;
2444
2445 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2446 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2447
2448 htab_up sig_types_hash = allocate_signatured_type_table ();
2449
2450 for (offset_type i = 0; i < elements; i += 3)
2451 {
2452 struct signatured_type *sig_type;
2453 ULONGEST signature;
2454 void **slot;
2455 cu_offset type_offset_in_tu;
2456
2457 gdb_static_assert (sizeof (ULONGEST) >= 8);
2458 sect_offset sect_off
2459 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2460 type_offset_in_tu
2461 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2462 BFD_ENDIAN_LITTLE);
2463 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2464 bytes += 3 * 8;
2465
2466 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2467 struct signatured_type);
2468 sig_type->signature = signature;
2469 sig_type->type_offset_in_tu = type_offset_in_tu;
2470 sig_type->per_cu.is_debug_types = 1;
2471 sig_type->per_cu.section = section;
2472 sig_type->per_cu.sect_off = sect_off;
2473 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2474 sig_type->per_cu.v.quick
2475 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2476 struct dwarf2_per_cu_quick_data);
2477
2478 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2479 *slot = sig_type;
2480
2481 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2482 }
2483
2484 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2485 }
2486
2487 /* Create the signatured type hash table from .debug_names. */
2488
2489 static void
2490 create_signatured_type_table_from_debug_names
2491 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2492 const mapped_debug_names &map,
2493 struct dwarf2_section_info *section,
2494 struct dwarf2_section_info *abbrev_section)
2495 {
2496 struct objfile *objfile = dwarf2_per_objfile->objfile;
2497
2498 section->read (objfile);
2499 abbrev_section->read (objfile);
2500
2501 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2502 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2503
2504 htab_up sig_types_hash = allocate_signatured_type_table ();
2505
2506 for (uint32_t i = 0; i < map.tu_count; ++i)
2507 {
2508 struct signatured_type *sig_type;
2509 void **slot;
2510
2511 sect_offset sect_off
2512 = (sect_offset) (extract_unsigned_integer
2513 (map.tu_table_reordered + i * map.offset_size,
2514 map.offset_size,
2515 map.dwarf5_byte_order));
2516
2517 comp_unit_head cu_header;
2518 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2519 abbrev_section,
2520 section->buffer + to_underlying (sect_off),
2521 rcuh_kind::TYPE);
2522
2523 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2524 struct signatured_type);
2525 sig_type->signature = cu_header.signature;
2526 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2527 sig_type->per_cu.is_debug_types = 1;
2528 sig_type->per_cu.section = section;
2529 sig_type->per_cu.sect_off = sect_off;
2530 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2531 sig_type->per_cu.v.quick
2532 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2533 struct dwarf2_per_cu_quick_data);
2534
2535 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2536 *slot = sig_type;
2537
2538 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2539 }
2540
2541 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2542 }
2543
2544 /* Read the address map data from the mapped index, and use it to
2545 populate the objfile's psymtabs_addrmap. */
2546
2547 static void
2548 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2549 struct mapped_index *index)
2550 {
2551 struct objfile *objfile = dwarf2_per_objfile->objfile;
2552 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2553 const gdb_byte *iter, *end;
2554 struct addrmap *mutable_map;
2555 CORE_ADDR baseaddr;
2556
2557 auto_obstack temp_obstack;
2558
2559 mutable_map = addrmap_create_mutable (&temp_obstack);
2560
2561 iter = index->address_table.data ();
2562 end = iter + index->address_table.size ();
2563
2564 baseaddr = objfile->text_section_offset ();
2565
2566 while (iter < end)
2567 {
2568 ULONGEST hi, lo, cu_index;
2569 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2570 iter += 8;
2571 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2572 iter += 8;
2573 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2574 iter += 4;
2575
2576 if (lo > hi)
2577 {
2578 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2579 hex_string (lo), hex_string (hi));
2580 continue;
2581 }
2582
2583 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2584 {
2585 complaint (_(".gdb_index address table has invalid CU number %u"),
2586 (unsigned) cu_index);
2587 continue;
2588 }
2589
2590 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2591 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2592 addrmap_set_empty (mutable_map, lo, hi - 1,
2593 dwarf2_per_objfile->get_cu (cu_index));
2594 }
2595
2596 objfile->partial_symtabs->psymtabs_addrmap
2597 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2598 }
2599
2600 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2601 populate the objfile's psymtabs_addrmap. */
2602
2603 static void
2604 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2605 struct dwarf2_section_info *section)
2606 {
2607 struct objfile *objfile = dwarf2_per_objfile->objfile;
2608 bfd *abfd = objfile->obfd;
2609 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2610 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2611
2612 auto_obstack temp_obstack;
2613 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2614
2615 std::unordered_map<sect_offset,
2616 dwarf2_per_cu_data *,
2617 gdb::hash_enum<sect_offset>>
2618 debug_info_offset_to_per_cu;
2619 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2620 {
2621 const auto insertpair
2622 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2623 if (!insertpair.second)
2624 {
2625 warning (_("Section .debug_aranges in %s has duplicate "
2626 "debug_info_offset %s, ignoring .debug_aranges."),
2627 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2628 return;
2629 }
2630 }
2631
2632 section->read (objfile);
2633
2634 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2635
2636 const gdb_byte *addr = section->buffer;
2637
2638 while (addr < section->buffer + section->size)
2639 {
2640 const gdb_byte *const entry_addr = addr;
2641 unsigned int bytes_read;
2642
2643 const LONGEST entry_length = read_initial_length (abfd, addr,
2644 &bytes_read);
2645 addr += bytes_read;
2646
2647 const gdb_byte *const entry_end = addr + entry_length;
2648 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2649 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2650 if (addr + entry_length > section->buffer + section->size)
2651 {
2652 warning (_("Section .debug_aranges in %s entry at offset %s "
2653 "length %s exceeds section length %s, "
2654 "ignoring .debug_aranges."),
2655 objfile_name (objfile),
2656 plongest (entry_addr - section->buffer),
2657 plongest (bytes_read + entry_length),
2658 pulongest (section->size));
2659 return;
2660 }
2661
2662 /* The version number. */
2663 const uint16_t version = read_2_bytes (abfd, addr);
2664 addr += 2;
2665 if (version != 2)
2666 {
2667 warning (_("Section .debug_aranges in %s entry at offset %s "
2668 "has unsupported version %d, ignoring .debug_aranges."),
2669 objfile_name (objfile),
2670 plongest (entry_addr - section->buffer), version);
2671 return;
2672 }
2673
2674 const uint64_t debug_info_offset
2675 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2676 addr += offset_size;
2677 const auto per_cu_it
2678 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2679 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2680 {
2681 warning (_("Section .debug_aranges in %s entry at offset %s "
2682 "debug_info_offset %s does not exists, "
2683 "ignoring .debug_aranges."),
2684 objfile_name (objfile),
2685 plongest (entry_addr - section->buffer),
2686 pulongest (debug_info_offset));
2687 return;
2688 }
2689 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2690
2691 const uint8_t address_size = *addr++;
2692 if (address_size < 1 || address_size > 8)
2693 {
2694 warning (_("Section .debug_aranges in %s entry at offset %s "
2695 "address_size %u is invalid, ignoring .debug_aranges."),
2696 objfile_name (objfile),
2697 plongest (entry_addr - section->buffer), address_size);
2698 return;
2699 }
2700
2701 const uint8_t segment_selector_size = *addr++;
2702 if (segment_selector_size != 0)
2703 {
2704 warning (_("Section .debug_aranges in %s entry at offset %s "
2705 "segment_selector_size %u is not supported, "
2706 "ignoring .debug_aranges."),
2707 objfile_name (objfile),
2708 plongest (entry_addr - section->buffer),
2709 segment_selector_size);
2710 return;
2711 }
2712
2713 /* Must pad to an alignment boundary that is twice the address
2714 size. It is undocumented by the DWARF standard but GCC does
2715 use it. */
2716 for (size_t padding = ((-(addr - section->buffer))
2717 & (2 * address_size - 1));
2718 padding > 0; padding--)
2719 if (*addr++ != 0)
2720 {
2721 warning (_("Section .debug_aranges in %s entry at offset %s "
2722 "padding is not zero, ignoring .debug_aranges."),
2723 objfile_name (objfile),
2724 plongest (entry_addr - section->buffer));
2725 return;
2726 }
2727
2728 for (;;)
2729 {
2730 if (addr + 2 * address_size > entry_end)
2731 {
2732 warning (_("Section .debug_aranges in %s entry at offset %s "
2733 "address list is not properly terminated, "
2734 "ignoring .debug_aranges."),
2735 objfile_name (objfile),
2736 plongest (entry_addr - section->buffer));
2737 return;
2738 }
2739 ULONGEST start = extract_unsigned_integer (addr, address_size,
2740 dwarf5_byte_order);
2741 addr += address_size;
2742 ULONGEST length = extract_unsigned_integer (addr, address_size,
2743 dwarf5_byte_order);
2744 addr += address_size;
2745 if (start == 0 && length == 0)
2746 break;
2747 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2748 {
2749 /* Symbol was eliminated due to a COMDAT group. */
2750 continue;
2751 }
2752 ULONGEST end = start + length;
2753 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2754 - baseaddr);
2755 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2756 - baseaddr);
2757 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2758 }
2759 }
2760
2761 objfile->partial_symtabs->psymtabs_addrmap
2762 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2763 }
2764
2765 /* Find a slot in the mapped index INDEX for the object named NAME.
2766 If NAME is found, set *VEC_OUT to point to the CU vector in the
2767 constant pool and return true. If NAME cannot be found, return
2768 false. */
2769
2770 static bool
2771 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2772 offset_type **vec_out)
2773 {
2774 offset_type hash;
2775 offset_type slot, step;
2776 int (*cmp) (const char *, const char *);
2777
2778 gdb::unique_xmalloc_ptr<char> without_params;
2779 if (current_language->la_language == language_cplus
2780 || current_language->la_language == language_fortran
2781 || current_language->la_language == language_d)
2782 {
2783 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2784 not contain any. */
2785
2786 if (strchr (name, '(') != NULL)
2787 {
2788 without_params = cp_remove_params (name);
2789
2790 if (without_params != NULL)
2791 name = without_params.get ();
2792 }
2793 }
2794
2795 /* Index version 4 did not support case insensitive searches. But the
2796 indices for case insensitive languages are built in lowercase, therefore
2797 simulate our NAME being searched is also lowercased. */
2798 hash = mapped_index_string_hash ((index->version == 4
2799 && case_sensitivity == case_sensitive_off
2800 ? 5 : index->version),
2801 name);
2802
2803 slot = hash & (index->symbol_table.size () - 1);
2804 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2805 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2806
2807 for (;;)
2808 {
2809 const char *str;
2810
2811 const auto &bucket = index->symbol_table[slot];
2812 if (bucket.name == 0 && bucket.vec == 0)
2813 return false;
2814
2815 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2816 if (!cmp (name, str))
2817 {
2818 *vec_out = (offset_type *) (index->constant_pool
2819 + MAYBE_SWAP (bucket.vec));
2820 return true;
2821 }
2822
2823 slot = (slot + step) & (index->symbol_table.size () - 1);
2824 }
2825 }
2826
2827 /* A helper function that reads the .gdb_index from BUFFER and fills
2828 in MAP. FILENAME is the name of the file containing the data;
2829 it is used for error reporting. DEPRECATED_OK is true if it is
2830 ok to use deprecated sections.
2831
2832 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2833 out parameters that are filled in with information about the CU and
2834 TU lists in the section.
2835
2836 Returns true if all went well, false otherwise. */
2837
2838 static bool
2839 read_gdb_index_from_buffer (struct objfile *objfile,
2840 const char *filename,
2841 bool deprecated_ok,
2842 gdb::array_view<const gdb_byte> buffer,
2843 struct mapped_index *map,
2844 const gdb_byte **cu_list,
2845 offset_type *cu_list_elements,
2846 const gdb_byte **types_list,
2847 offset_type *types_list_elements)
2848 {
2849 const gdb_byte *addr = &buffer[0];
2850
2851 /* Version check. */
2852 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2853 /* Versions earlier than 3 emitted every copy of a psymbol. This
2854 causes the index to behave very poorly for certain requests. Version 3
2855 contained incomplete addrmap. So, it seems better to just ignore such
2856 indices. */
2857 if (version < 4)
2858 {
2859 static int warning_printed = 0;
2860 if (!warning_printed)
2861 {
2862 warning (_("Skipping obsolete .gdb_index section in %s."),
2863 filename);
2864 warning_printed = 1;
2865 }
2866 return 0;
2867 }
2868 /* Index version 4 uses a different hash function than index version
2869 5 and later.
2870
2871 Versions earlier than 6 did not emit psymbols for inlined
2872 functions. Using these files will cause GDB not to be able to
2873 set breakpoints on inlined functions by name, so we ignore these
2874 indices unless the user has done
2875 "set use-deprecated-index-sections on". */
2876 if (version < 6 && !deprecated_ok)
2877 {
2878 static int warning_printed = 0;
2879 if (!warning_printed)
2880 {
2881 warning (_("\
2882 Skipping deprecated .gdb_index section in %s.\n\
2883 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2884 to use the section anyway."),
2885 filename);
2886 warning_printed = 1;
2887 }
2888 return 0;
2889 }
2890 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2891 of the TU (for symbols coming from TUs),
2892 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2893 Plus gold-generated indices can have duplicate entries for global symbols,
2894 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2895 These are just performance bugs, and we can't distinguish gdb-generated
2896 indices from gold-generated ones, so issue no warning here. */
2897
2898 /* Indexes with higher version than the one supported by GDB may be no
2899 longer backward compatible. */
2900 if (version > 8)
2901 return 0;
2902
2903 map->version = version;
2904
2905 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2906
2907 int i = 0;
2908 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2909 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2910 / 8);
2911 ++i;
2912
2913 *types_list = addr + MAYBE_SWAP (metadata[i]);
2914 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2915 - MAYBE_SWAP (metadata[i]))
2916 / 8);
2917 ++i;
2918
2919 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2920 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2921 map->address_table
2922 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2923 ++i;
2924
2925 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2926 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2927 map->symbol_table
2928 = gdb::array_view<mapped_index::symbol_table_slot>
2929 ((mapped_index::symbol_table_slot *) symbol_table,
2930 (mapped_index::symbol_table_slot *) symbol_table_end);
2931
2932 ++i;
2933 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2934
2935 return 1;
2936 }
2937
2938 /* Callback types for dwarf2_read_gdb_index. */
2939
2940 typedef gdb::function_view
2941 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2942 get_gdb_index_contents_ftype;
2943 typedef gdb::function_view
2944 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2945 get_gdb_index_contents_dwz_ftype;
2946
2947 /* Read .gdb_index. If everything went ok, initialize the "quick"
2948 elements of all the CUs and return 1. Otherwise, return 0. */
2949
2950 static int
2951 dwarf2_read_gdb_index
2952 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2953 get_gdb_index_contents_ftype get_gdb_index_contents,
2954 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2955 {
2956 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2957 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2958 struct dwz_file *dwz;
2959 struct objfile *objfile = dwarf2_per_objfile->objfile;
2960
2961 gdb::array_view<const gdb_byte> main_index_contents
2962 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
2963
2964 if (main_index_contents.empty ())
2965 return 0;
2966
2967 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2968 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
2969 use_deprecated_index_sections,
2970 main_index_contents, map.get (), &cu_list,
2971 &cu_list_elements, &types_list,
2972 &types_list_elements))
2973 return 0;
2974
2975 /* Don't use the index if it's empty. */
2976 if (map->symbol_table.empty ())
2977 return 0;
2978
2979 /* If there is a .dwz file, read it so we can get its CU list as
2980 well. */
2981 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2982 if (dwz != NULL)
2983 {
2984 struct mapped_index dwz_map;
2985 const gdb_byte *dwz_types_ignore;
2986 offset_type dwz_types_elements_ignore;
2987
2988 gdb::array_view<const gdb_byte> dwz_index_content
2989 = get_gdb_index_contents_dwz (objfile, dwz);
2990
2991 if (dwz_index_content.empty ())
2992 return 0;
2993
2994 if (!read_gdb_index_from_buffer (objfile,
2995 bfd_get_filename (dwz->dwz_bfd.get ()),
2996 1, dwz_index_content, &dwz_map,
2997 &dwz_list, &dwz_list_elements,
2998 &dwz_types_ignore,
2999 &dwz_types_elements_ignore))
3000 {
3001 warning (_("could not read '.gdb_index' section from %s; skipping"),
3002 bfd_get_filename (dwz->dwz_bfd.get ()));
3003 return 0;
3004 }
3005 }
3006
3007 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3008 dwz_list, dwz_list_elements);
3009
3010 if (types_list_elements)
3011 {
3012 /* We can only handle a single .debug_types when we have an
3013 index. */
3014 if (dwarf2_per_objfile->types.size () != 1)
3015 return 0;
3016
3017 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3018
3019 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3020 types_list, types_list_elements);
3021 }
3022
3023 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3024
3025 dwarf2_per_objfile->index_table = std::move (map);
3026 dwarf2_per_objfile->using_index = 1;
3027 dwarf2_per_objfile->quick_file_names_table =
3028 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3029
3030 return 1;
3031 }
3032
3033 /* die_reader_func for dw2_get_file_names. */
3034
3035 static void
3036 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3037 const gdb_byte *info_ptr,
3038 struct die_info *comp_unit_die)
3039 {
3040 struct dwarf2_cu *cu = reader->cu;
3041 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3042 struct dwarf2_per_objfile *dwarf2_per_objfile
3043 = cu->per_cu->dwarf2_per_objfile;
3044 struct objfile *objfile = dwarf2_per_objfile->objfile;
3045 struct dwarf2_per_cu_data *lh_cu;
3046 struct attribute *attr;
3047 void **slot;
3048 struct quick_file_names *qfn;
3049
3050 gdb_assert (! this_cu->is_debug_types);
3051
3052 /* Our callers never want to match partial units -- instead they
3053 will match the enclosing full CU. */
3054 if (comp_unit_die->tag == DW_TAG_partial_unit)
3055 {
3056 this_cu->v.quick->no_file_data = 1;
3057 return;
3058 }
3059
3060 lh_cu = this_cu;
3061 slot = NULL;
3062
3063 line_header_up lh;
3064 sect_offset line_offset {};
3065
3066 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3067 if (attr != nullptr)
3068 {
3069 struct quick_file_names find_entry;
3070
3071 line_offset = (sect_offset) DW_UNSND (attr);
3072
3073 /* We may have already read in this line header (TU line header sharing).
3074 If we have we're done. */
3075 find_entry.hash.dwo_unit = cu->dwo_unit;
3076 find_entry.hash.line_sect_off = line_offset;
3077 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3078 &find_entry, INSERT);
3079 if (*slot != NULL)
3080 {
3081 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3082 return;
3083 }
3084
3085 lh = dwarf_decode_line_header (line_offset, cu);
3086 }
3087 if (lh == NULL)
3088 {
3089 lh_cu->v.quick->no_file_data = 1;
3090 return;
3091 }
3092
3093 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3094 qfn->hash.dwo_unit = cu->dwo_unit;
3095 qfn->hash.line_sect_off = line_offset;
3096 gdb_assert (slot != NULL);
3097 *slot = qfn;
3098
3099 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3100
3101 int offset = 0;
3102 if (strcmp (fnd.name, "<unknown>") != 0)
3103 ++offset;
3104
3105 qfn->num_file_names = offset + lh->file_names_size ();
3106 qfn->file_names =
3107 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3108 if (offset != 0)
3109 qfn->file_names[0] = xstrdup (fnd.name);
3110 for (int i = 0; i < lh->file_names_size (); ++i)
3111 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3112 fnd.comp_dir).release ();
3113 qfn->real_names = NULL;
3114
3115 lh_cu->v.quick->file_names = qfn;
3116 }
3117
3118 /* A helper for the "quick" functions which attempts to read the line
3119 table for THIS_CU. */
3120
3121 static struct quick_file_names *
3122 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3123 {
3124 /* This should never be called for TUs. */
3125 gdb_assert (! this_cu->is_debug_types);
3126 /* Nor type unit groups. */
3127 gdb_assert (! this_cu->type_unit_group_p ());
3128
3129 if (this_cu->v.quick->file_names != NULL)
3130 return this_cu->v.quick->file_names;
3131 /* If we know there is no line data, no point in looking again. */
3132 if (this_cu->v.quick->no_file_data)
3133 return NULL;
3134
3135 cutu_reader reader (this_cu);
3136 if (!reader.dummy_p)
3137 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3138
3139 if (this_cu->v.quick->no_file_data)
3140 return NULL;
3141 return this_cu->v.quick->file_names;
3142 }
3143
3144 /* A helper for the "quick" functions which computes and caches the
3145 real path for a given file name from the line table. */
3146
3147 static const char *
3148 dw2_get_real_path (struct objfile *objfile,
3149 struct quick_file_names *qfn, int index)
3150 {
3151 if (qfn->real_names == NULL)
3152 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3153 qfn->num_file_names, const char *);
3154
3155 if (qfn->real_names[index] == NULL)
3156 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3157
3158 return qfn->real_names[index];
3159 }
3160
3161 static struct symtab *
3162 dw2_find_last_source_symtab (struct objfile *objfile)
3163 {
3164 struct dwarf2_per_objfile *dwarf2_per_objfile
3165 = get_dwarf2_per_objfile (objfile);
3166 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3167 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3168
3169 if (cust == NULL)
3170 return NULL;
3171
3172 return compunit_primary_filetab (cust);
3173 }
3174
3175 /* Traversal function for dw2_forget_cached_source_info. */
3176
3177 static int
3178 dw2_free_cached_file_names (void **slot, void *info)
3179 {
3180 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3181
3182 if (file_data->real_names)
3183 {
3184 int i;
3185
3186 for (i = 0; i < file_data->num_file_names; ++i)
3187 {
3188 xfree ((void*) file_data->real_names[i]);
3189 file_data->real_names[i] = NULL;
3190 }
3191 }
3192
3193 return 1;
3194 }
3195
3196 static void
3197 dw2_forget_cached_source_info (struct objfile *objfile)
3198 {
3199 struct dwarf2_per_objfile *dwarf2_per_objfile
3200 = get_dwarf2_per_objfile (objfile);
3201
3202 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3203 dw2_free_cached_file_names, NULL);
3204 }
3205
3206 /* Helper function for dw2_map_symtabs_matching_filename that expands
3207 the symtabs and calls the iterator. */
3208
3209 static int
3210 dw2_map_expand_apply (struct objfile *objfile,
3211 struct dwarf2_per_cu_data *per_cu,
3212 const char *name, const char *real_path,
3213 gdb::function_view<bool (symtab *)> callback)
3214 {
3215 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3216
3217 /* Don't visit already-expanded CUs. */
3218 if (per_cu->v.quick->compunit_symtab)
3219 return 0;
3220
3221 /* This may expand more than one symtab, and we want to iterate over
3222 all of them. */
3223 dw2_instantiate_symtab (per_cu, false);
3224
3225 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3226 last_made, callback);
3227 }
3228
3229 /* Implementation of the map_symtabs_matching_filename method. */
3230
3231 static bool
3232 dw2_map_symtabs_matching_filename
3233 (struct objfile *objfile, const char *name, const char *real_path,
3234 gdb::function_view<bool (symtab *)> callback)
3235 {
3236 const char *name_basename = lbasename (name);
3237 struct dwarf2_per_objfile *dwarf2_per_objfile
3238 = get_dwarf2_per_objfile (objfile);
3239
3240 /* The rule is CUs specify all the files, including those used by
3241 any TU, so there's no need to scan TUs here. */
3242
3243 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3244 {
3245 /* We only need to look at symtabs not already expanded. */
3246 if (per_cu->v.quick->compunit_symtab)
3247 continue;
3248
3249 quick_file_names *file_data = dw2_get_file_names (per_cu);
3250 if (file_data == NULL)
3251 continue;
3252
3253 for (int j = 0; j < file_data->num_file_names; ++j)
3254 {
3255 const char *this_name = file_data->file_names[j];
3256 const char *this_real_name;
3257
3258 if (compare_filenames_for_search (this_name, name))
3259 {
3260 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3261 callback))
3262 return true;
3263 continue;
3264 }
3265
3266 /* Before we invoke realpath, which can get expensive when many
3267 files are involved, do a quick comparison of the basenames. */
3268 if (! basenames_may_differ
3269 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3270 continue;
3271
3272 this_real_name = dw2_get_real_path (objfile, file_data, j);
3273 if (compare_filenames_for_search (this_real_name, name))
3274 {
3275 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3276 callback))
3277 return true;
3278 continue;
3279 }
3280
3281 if (real_path != NULL)
3282 {
3283 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3284 gdb_assert (IS_ABSOLUTE_PATH (name));
3285 if (this_real_name != NULL
3286 && FILENAME_CMP (real_path, this_real_name) == 0)
3287 {
3288 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3289 callback))
3290 return true;
3291 continue;
3292 }
3293 }
3294 }
3295 }
3296
3297 return false;
3298 }
3299
3300 /* Struct used to manage iterating over all CUs looking for a symbol. */
3301
3302 struct dw2_symtab_iterator
3303 {
3304 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3305 struct dwarf2_per_objfile *dwarf2_per_objfile;
3306 /* If set, only look for symbols that match that block. Valid values are
3307 GLOBAL_BLOCK and STATIC_BLOCK. */
3308 gdb::optional<block_enum> block_index;
3309 /* The kind of symbol we're looking for. */
3310 domain_enum domain;
3311 /* The list of CUs from the index entry of the symbol,
3312 or NULL if not found. */
3313 offset_type *vec;
3314 /* The next element in VEC to look at. */
3315 int next;
3316 /* The number of elements in VEC, or zero if there is no match. */
3317 int length;
3318 /* Have we seen a global version of the symbol?
3319 If so we can ignore all further global instances.
3320 This is to work around gold/15646, inefficient gold-generated
3321 indices. */
3322 int global_seen;
3323 };
3324
3325 /* Initialize the index symtab iterator ITER. */
3326
3327 static void
3328 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3329 struct dwarf2_per_objfile *dwarf2_per_objfile,
3330 gdb::optional<block_enum> block_index,
3331 domain_enum domain,
3332 const char *name)
3333 {
3334 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3335 iter->block_index = block_index;
3336 iter->domain = domain;
3337 iter->next = 0;
3338 iter->global_seen = 0;
3339
3340 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3341
3342 /* index is NULL if OBJF_READNOW. */
3343 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3344 iter->length = MAYBE_SWAP (*iter->vec);
3345 else
3346 {
3347 iter->vec = NULL;
3348 iter->length = 0;
3349 }
3350 }
3351
3352 /* Return the next matching CU or NULL if there are no more. */
3353
3354 static struct dwarf2_per_cu_data *
3355 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3356 {
3357 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3358
3359 for ( ; iter->next < iter->length; ++iter->next)
3360 {
3361 offset_type cu_index_and_attrs =
3362 MAYBE_SWAP (iter->vec[iter->next + 1]);
3363 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3364 gdb_index_symbol_kind symbol_kind =
3365 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3366 /* Only check the symbol attributes if they're present.
3367 Indices prior to version 7 don't record them,
3368 and indices >= 7 may elide them for certain symbols
3369 (gold does this). */
3370 int attrs_valid =
3371 (dwarf2_per_objfile->index_table->version >= 7
3372 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3373
3374 /* Don't crash on bad data. */
3375 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3376 + dwarf2_per_objfile->all_type_units.size ()))
3377 {
3378 complaint (_(".gdb_index entry has bad CU index"
3379 " [in module %s]"),
3380 objfile_name (dwarf2_per_objfile->objfile));
3381 continue;
3382 }
3383
3384 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3385
3386 /* Skip if already read in. */
3387 if (per_cu->v.quick->compunit_symtab)
3388 continue;
3389
3390 /* Check static vs global. */
3391 if (attrs_valid)
3392 {
3393 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3394
3395 if (iter->block_index.has_value ())
3396 {
3397 bool want_static = *iter->block_index == STATIC_BLOCK;
3398
3399 if (is_static != want_static)
3400 continue;
3401 }
3402
3403 /* Work around gold/15646. */
3404 if (!is_static && iter->global_seen)
3405 continue;
3406 if (!is_static)
3407 iter->global_seen = 1;
3408 }
3409
3410 /* Only check the symbol's kind if it has one. */
3411 if (attrs_valid)
3412 {
3413 switch (iter->domain)
3414 {
3415 case VAR_DOMAIN:
3416 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3417 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3418 /* Some types are also in VAR_DOMAIN. */
3419 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3420 continue;
3421 break;
3422 case STRUCT_DOMAIN:
3423 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3424 continue;
3425 break;
3426 case LABEL_DOMAIN:
3427 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3428 continue;
3429 break;
3430 case MODULE_DOMAIN:
3431 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3432 continue;
3433 break;
3434 default:
3435 break;
3436 }
3437 }
3438
3439 ++iter->next;
3440 return per_cu;
3441 }
3442
3443 return NULL;
3444 }
3445
3446 static struct compunit_symtab *
3447 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3448 const char *name, domain_enum domain)
3449 {
3450 struct compunit_symtab *stab_best = NULL;
3451 struct dwarf2_per_objfile *dwarf2_per_objfile
3452 = get_dwarf2_per_objfile (objfile);
3453
3454 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3455
3456 struct dw2_symtab_iterator iter;
3457 struct dwarf2_per_cu_data *per_cu;
3458
3459 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3460
3461 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3462 {
3463 struct symbol *sym, *with_opaque = NULL;
3464 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3465 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3466 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3467
3468 sym = block_find_symbol (block, name, domain,
3469 block_find_non_opaque_type_preferred,
3470 &with_opaque);
3471
3472 /* Some caution must be observed with overloaded functions
3473 and methods, since the index will not contain any overload
3474 information (but NAME might contain it). */
3475
3476 if (sym != NULL
3477 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3478 return stab;
3479 if (with_opaque != NULL
3480 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3481 stab_best = stab;
3482
3483 /* Keep looking through other CUs. */
3484 }
3485
3486 return stab_best;
3487 }
3488
3489 static void
3490 dw2_print_stats (struct objfile *objfile)
3491 {
3492 struct dwarf2_per_objfile *dwarf2_per_objfile
3493 = get_dwarf2_per_objfile (objfile);
3494 int total = (dwarf2_per_objfile->all_comp_units.size ()
3495 + dwarf2_per_objfile->all_type_units.size ());
3496 int count = 0;
3497
3498 for (int i = 0; i < total; ++i)
3499 {
3500 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3501
3502 if (!per_cu->v.quick->compunit_symtab)
3503 ++count;
3504 }
3505 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3506 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3507 }
3508
3509 /* This dumps minimal information about the index.
3510 It is called via "mt print objfiles".
3511 One use is to verify .gdb_index has been loaded by the
3512 gdb.dwarf2/gdb-index.exp testcase. */
3513
3514 static void
3515 dw2_dump (struct objfile *objfile)
3516 {
3517 struct dwarf2_per_objfile *dwarf2_per_objfile
3518 = get_dwarf2_per_objfile (objfile);
3519
3520 gdb_assert (dwarf2_per_objfile->using_index);
3521 printf_filtered (".gdb_index:");
3522 if (dwarf2_per_objfile->index_table != NULL)
3523 {
3524 printf_filtered (" version %d\n",
3525 dwarf2_per_objfile->index_table->version);
3526 }
3527 else
3528 printf_filtered (" faked for \"readnow\"\n");
3529 printf_filtered ("\n");
3530 }
3531
3532 static void
3533 dw2_expand_symtabs_for_function (struct objfile *objfile,
3534 const char *func_name)
3535 {
3536 struct dwarf2_per_objfile *dwarf2_per_objfile
3537 = get_dwarf2_per_objfile (objfile);
3538
3539 struct dw2_symtab_iterator iter;
3540 struct dwarf2_per_cu_data *per_cu;
3541
3542 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3543
3544 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3545 dw2_instantiate_symtab (per_cu, false);
3546
3547 }
3548
3549 static void
3550 dw2_expand_all_symtabs (struct objfile *objfile)
3551 {
3552 struct dwarf2_per_objfile *dwarf2_per_objfile
3553 = get_dwarf2_per_objfile (objfile);
3554 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3555 + dwarf2_per_objfile->all_type_units.size ());
3556
3557 for (int i = 0; i < total_units; ++i)
3558 {
3559 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3560
3561 /* We don't want to directly expand a partial CU, because if we
3562 read it with the wrong language, then assertion failures can
3563 be triggered later on. See PR symtab/23010. So, tell
3564 dw2_instantiate_symtab to skip partial CUs -- any important
3565 partial CU will be read via DW_TAG_imported_unit anyway. */
3566 dw2_instantiate_symtab (per_cu, true);
3567 }
3568 }
3569
3570 static void
3571 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3572 const char *fullname)
3573 {
3574 struct dwarf2_per_objfile *dwarf2_per_objfile
3575 = get_dwarf2_per_objfile (objfile);
3576
3577 /* We don't need to consider type units here.
3578 This is only called for examining code, e.g. expand_line_sal.
3579 There can be an order of magnitude (or more) more type units
3580 than comp units, and we avoid them if we can. */
3581
3582 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3583 {
3584 /* We only need to look at symtabs not already expanded. */
3585 if (per_cu->v.quick->compunit_symtab)
3586 continue;
3587
3588 quick_file_names *file_data = dw2_get_file_names (per_cu);
3589 if (file_data == NULL)
3590 continue;
3591
3592 for (int j = 0; j < file_data->num_file_names; ++j)
3593 {
3594 const char *this_fullname = file_data->file_names[j];
3595
3596 if (filename_cmp (this_fullname, fullname) == 0)
3597 {
3598 dw2_instantiate_symtab (per_cu, false);
3599 break;
3600 }
3601 }
3602 }
3603 }
3604
3605 static void
3606 dw2_map_matching_symbols
3607 (struct objfile *objfile,
3608 const lookup_name_info &name, domain_enum domain,
3609 int global,
3610 gdb::function_view<symbol_found_callback_ftype> callback,
3611 symbol_compare_ftype *ordered_compare)
3612 {
3613 /* Currently unimplemented; used for Ada. The function can be called if the
3614 current language is Ada for a non-Ada objfile using GNU index. As Ada
3615 does not look for non-Ada symbols this function should just return. */
3616 }
3617
3618 /* Starting from a search name, return the string that finds the upper
3619 bound of all strings that start with SEARCH_NAME in a sorted name
3620 list. Returns the empty string to indicate that the upper bound is
3621 the end of the list. */
3622
3623 static std::string
3624 make_sort_after_prefix_name (const char *search_name)
3625 {
3626 /* When looking to complete "func", we find the upper bound of all
3627 symbols that start with "func" by looking for where we'd insert
3628 the closest string that would follow "func" in lexicographical
3629 order. Usually, that's "func"-with-last-character-incremented,
3630 i.e. "fund". Mind non-ASCII characters, though. Usually those
3631 will be UTF-8 multi-byte sequences, but we can't be certain.
3632 Especially mind the 0xff character, which is a valid character in
3633 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3634 rule out compilers allowing it in identifiers. Note that
3635 conveniently, strcmp/strcasecmp are specified to compare
3636 characters interpreted as unsigned char. So what we do is treat
3637 the whole string as a base 256 number composed of a sequence of
3638 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3639 to 0, and carries 1 to the following more-significant position.
3640 If the very first character in SEARCH_NAME ends up incremented
3641 and carries/overflows, then the upper bound is the end of the
3642 list. The string after the empty string is also the empty
3643 string.
3644
3645 Some examples of this operation:
3646
3647 SEARCH_NAME => "+1" RESULT
3648
3649 "abc" => "abd"
3650 "ab\xff" => "ac"
3651 "\xff" "a" "\xff" => "\xff" "b"
3652 "\xff" => ""
3653 "\xff\xff" => ""
3654 "" => ""
3655
3656 Then, with these symbols for example:
3657
3658 func
3659 func1
3660 fund
3661
3662 completing "func" looks for symbols between "func" and
3663 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3664 which finds "func" and "func1", but not "fund".
3665
3666 And with:
3667
3668 funcÿ (Latin1 'ÿ' [0xff])
3669 funcÿ1
3670 fund
3671
3672 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3673 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3674
3675 And with:
3676
3677 ÿÿ (Latin1 'ÿ' [0xff])
3678 ÿÿ1
3679
3680 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3681 the end of the list.
3682 */
3683 std::string after = search_name;
3684 while (!after.empty () && (unsigned char) after.back () == 0xff)
3685 after.pop_back ();
3686 if (!after.empty ())
3687 after.back () = (unsigned char) after.back () + 1;
3688 return after;
3689 }
3690
3691 /* See declaration. */
3692
3693 std::pair<std::vector<name_component>::const_iterator,
3694 std::vector<name_component>::const_iterator>
3695 mapped_index_base::find_name_components_bounds
3696 (const lookup_name_info &lookup_name_without_params, language lang) const
3697 {
3698 auto *name_cmp
3699 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3700
3701 const char *lang_name
3702 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3703
3704 /* Comparison function object for lower_bound that matches against a
3705 given symbol name. */
3706 auto lookup_compare_lower = [&] (const name_component &elem,
3707 const char *name)
3708 {
3709 const char *elem_qualified = this->symbol_name_at (elem.idx);
3710 const char *elem_name = elem_qualified + elem.name_offset;
3711 return name_cmp (elem_name, name) < 0;
3712 };
3713
3714 /* Comparison function object for upper_bound that matches against a
3715 given symbol name. */
3716 auto lookup_compare_upper = [&] (const char *name,
3717 const name_component &elem)
3718 {
3719 const char *elem_qualified = this->symbol_name_at (elem.idx);
3720 const char *elem_name = elem_qualified + elem.name_offset;
3721 return name_cmp (name, elem_name) < 0;
3722 };
3723
3724 auto begin = this->name_components.begin ();
3725 auto end = this->name_components.end ();
3726
3727 /* Find the lower bound. */
3728 auto lower = [&] ()
3729 {
3730 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3731 return begin;
3732 else
3733 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3734 } ();
3735
3736 /* Find the upper bound. */
3737 auto upper = [&] ()
3738 {
3739 if (lookup_name_without_params.completion_mode ())
3740 {
3741 /* In completion mode, we want UPPER to point past all
3742 symbols names that have the same prefix. I.e., with
3743 these symbols, and completing "func":
3744
3745 function << lower bound
3746 function1
3747 other_function << upper bound
3748
3749 We find the upper bound by looking for the insertion
3750 point of "func"-with-last-character-incremented,
3751 i.e. "fund". */
3752 std::string after = make_sort_after_prefix_name (lang_name);
3753 if (after.empty ())
3754 return end;
3755 return std::lower_bound (lower, end, after.c_str (),
3756 lookup_compare_lower);
3757 }
3758 else
3759 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3760 } ();
3761
3762 return {lower, upper};
3763 }
3764
3765 /* See declaration. */
3766
3767 void
3768 mapped_index_base::build_name_components ()
3769 {
3770 if (!this->name_components.empty ())
3771 return;
3772
3773 this->name_components_casing = case_sensitivity;
3774 auto *name_cmp
3775 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3776
3777 /* The code below only knows how to break apart components of C++
3778 symbol names (and other languages that use '::' as
3779 namespace/module separator) and Ada symbol names. */
3780 auto count = this->symbol_name_count ();
3781 for (offset_type idx = 0; idx < count; idx++)
3782 {
3783 if (this->symbol_name_slot_invalid (idx))
3784 continue;
3785
3786 const char *name = this->symbol_name_at (idx);
3787
3788 /* Add each name component to the name component table. */
3789 unsigned int previous_len = 0;
3790
3791 if (strstr (name, "::") != nullptr)
3792 {
3793 for (unsigned int current_len = cp_find_first_component (name);
3794 name[current_len] != '\0';
3795 current_len += cp_find_first_component (name + current_len))
3796 {
3797 gdb_assert (name[current_len] == ':');
3798 this->name_components.push_back ({previous_len, idx});
3799 /* Skip the '::'. */
3800 current_len += 2;
3801 previous_len = current_len;
3802 }
3803 }
3804 else
3805 {
3806 /* Handle the Ada encoded (aka mangled) form here. */
3807 for (const char *iter = strstr (name, "__");
3808 iter != nullptr;
3809 iter = strstr (iter, "__"))
3810 {
3811 this->name_components.push_back ({previous_len, idx});
3812 iter += 2;
3813 previous_len = iter - name;
3814 }
3815 }
3816
3817 this->name_components.push_back ({previous_len, idx});
3818 }
3819
3820 /* Sort name_components elements by name. */
3821 auto name_comp_compare = [&] (const name_component &left,
3822 const name_component &right)
3823 {
3824 const char *left_qualified = this->symbol_name_at (left.idx);
3825 const char *right_qualified = this->symbol_name_at (right.idx);
3826
3827 const char *left_name = left_qualified + left.name_offset;
3828 const char *right_name = right_qualified + right.name_offset;
3829
3830 return name_cmp (left_name, right_name) < 0;
3831 };
3832
3833 std::sort (this->name_components.begin (),
3834 this->name_components.end (),
3835 name_comp_compare);
3836 }
3837
3838 /* Helper for dw2_expand_symtabs_matching that works with a
3839 mapped_index_base instead of the containing objfile. This is split
3840 to a separate function in order to be able to unit test the
3841 name_components matching using a mock mapped_index_base. For each
3842 symbol name that matches, calls MATCH_CALLBACK, passing it the
3843 symbol's index in the mapped_index_base symbol table. */
3844
3845 static void
3846 dw2_expand_symtabs_matching_symbol
3847 (mapped_index_base &index,
3848 const lookup_name_info &lookup_name_in,
3849 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3850 enum search_domain kind,
3851 gdb::function_view<bool (offset_type)> match_callback)
3852 {
3853 lookup_name_info lookup_name_without_params
3854 = lookup_name_in.make_ignore_params ();
3855
3856 /* Build the symbol name component sorted vector, if we haven't
3857 yet. */
3858 index.build_name_components ();
3859
3860 /* The same symbol may appear more than once in the range though.
3861 E.g., if we're looking for symbols that complete "w", and we have
3862 a symbol named "w1::w2", we'll find the two name components for
3863 that same symbol in the range. To be sure we only call the
3864 callback once per symbol, we first collect the symbol name
3865 indexes that matched in a temporary vector and ignore
3866 duplicates. */
3867 std::vector<offset_type> matches;
3868
3869 struct name_and_matcher
3870 {
3871 symbol_name_matcher_ftype *matcher;
3872 const std::string &name;
3873
3874 bool operator== (const name_and_matcher &other) const
3875 {
3876 return matcher == other.matcher && name == other.name;
3877 }
3878 };
3879
3880 /* A vector holding all the different symbol name matchers, for all
3881 languages. */
3882 std::vector<name_and_matcher> matchers;
3883
3884 for (int i = 0; i < nr_languages; i++)
3885 {
3886 enum language lang_e = (enum language) i;
3887
3888 const language_defn *lang = language_def (lang_e);
3889 symbol_name_matcher_ftype *name_matcher
3890 = get_symbol_name_matcher (lang, lookup_name_without_params);
3891
3892 name_and_matcher key {
3893 name_matcher,
3894 lookup_name_without_params.language_lookup_name (lang_e)
3895 };
3896
3897 /* Don't insert the same comparison routine more than once.
3898 Note that we do this linear walk. This is not a problem in
3899 practice because the number of supported languages is
3900 low. */
3901 if (std::find (matchers.begin (), matchers.end (), key)
3902 != matchers.end ())
3903 continue;
3904 matchers.push_back (std::move (key));
3905
3906 auto bounds
3907 = index.find_name_components_bounds (lookup_name_without_params,
3908 lang_e);
3909
3910 /* Now for each symbol name in range, check to see if we have a name
3911 match, and if so, call the MATCH_CALLBACK callback. */
3912
3913 for (; bounds.first != bounds.second; ++bounds.first)
3914 {
3915 const char *qualified = index.symbol_name_at (bounds.first->idx);
3916
3917 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3918 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3919 continue;
3920
3921 matches.push_back (bounds.first->idx);
3922 }
3923 }
3924
3925 std::sort (matches.begin (), matches.end ());
3926
3927 /* Finally call the callback, once per match. */
3928 ULONGEST prev = -1;
3929 for (offset_type idx : matches)
3930 {
3931 if (prev != idx)
3932 {
3933 if (!match_callback (idx))
3934 break;
3935 prev = idx;
3936 }
3937 }
3938
3939 /* Above we use a type wider than idx's for 'prev', since 0 and
3940 (offset_type)-1 are both possible values. */
3941 static_assert (sizeof (prev) > sizeof (offset_type), "");
3942 }
3943
3944 #if GDB_SELF_TEST
3945
3946 namespace selftests { namespace dw2_expand_symtabs_matching {
3947
3948 /* A mock .gdb_index/.debug_names-like name index table, enough to
3949 exercise dw2_expand_symtabs_matching_symbol, which works with the
3950 mapped_index_base interface. Builds an index from the symbol list
3951 passed as parameter to the constructor. */
3952 class mock_mapped_index : public mapped_index_base
3953 {
3954 public:
3955 mock_mapped_index (gdb::array_view<const char *> symbols)
3956 : m_symbol_table (symbols)
3957 {}
3958
3959 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3960
3961 /* Return the number of names in the symbol table. */
3962 size_t symbol_name_count () const override
3963 {
3964 return m_symbol_table.size ();
3965 }
3966
3967 /* Get the name of the symbol at IDX in the symbol table. */
3968 const char *symbol_name_at (offset_type idx) const override
3969 {
3970 return m_symbol_table[idx];
3971 }
3972
3973 private:
3974 gdb::array_view<const char *> m_symbol_table;
3975 };
3976
3977 /* Convenience function that converts a NULL pointer to a "<null>"
3978 string, to pass to print routines. */
3979
3980 static const char *
3981 string_or_null (const char *str)
3982 {
3983 return str != NULL ? str : "<null>";
3984 }
3985
3986 /* Check if a lookup_name_info built from
3987 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3988 index. EXPECTED_LIST is the list of expected matches, in expected
3989 matching order. If no match expected, then an empty list is
3990 specified. Returns true on success. On failure prints a warning
3991 indicating the file:line that failed, and returns false. */
3992
3993 static bool
3994 check_match (const char *file, int line,
3995 mock_mapped_index &mock_index,
3996 const char *name, symbol_name_match_type match_type,
3997 bool completion_mode,
3998 std::initializer_list<const char *> expected_list)
3999 {
4000 lookup_name_info lookup_name (name, match_type, completion_mode);
4001
4002 bool matched = true;
4003
4004 auto mismatch = [&] (const char *expected_str,
4005 const char *got)
4006 {
4007 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4008 "expected=\"%s\", got=\"%s\"\n"),
4009 file, line,
4010 (match_type == symbol_name_match_type::FULL
4011 ? "FULL" : "WILD"),
4012 name, string_or_null (expected_str), string_or_null (got));
4013 matched = false;
4014 };
4015
4016 auto expected_it = expected_list.begin ();
4017 auto expected_end = expected_list.end ();
4018
4019 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4020 NULL, ALL_DOMAIN,
4021 [&] (offset_type idx)
4022 {
4023 const char *matched_name = mock_index.symbol_name_at (idx);
4024 const char *expected_str
4025 = expected_it == expected_end ? NULL : *expected_it++;
4026
4027 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4028 mismatch (expected_str, matched_name);
4029 return true;
4030 });
4031
4032 const char *expected_str
4033 = expected_it == expected_end ? NULL : *expected_it++;
4034 if (expected_str != NULL)
4035 mismatch (expected_str, NULL);
4036
4037 return matched;
4038 }
4039
4040 /* The symbols added to the mock mapped_index for testing (in
4041 canonical form). */
4042 static const char *test_symbols[] = {
4043 "function",
4044 "std::bar",
4045 "std::zfunction",
4046 "std::zfunction2",
4047 "w1::w2",
4048 "ns::foo<char*>",
4049 "ns::foo<int>",
4050 "ns::foo<long>",
4051 "ns2::tmpl<int>::foo2",
4052 "(anonymous namespace)::A::B::C",
4053
4054 /* These are used to check that the increment-last-char in the
4055 matching algorithm for completion doesn't match "t1_fund" when
4056 completing "t1_func". */
4057 "t1_func",
4058 "t1_func1",
4059 "t1_fund",
4060 "t1_fund1",
4061
4062 /* A UTF-8 name with multi-byte sequences to make sure that
4063 cp-name-parser understands this as a single identifier ("função"
4064 is "function" in PT). */
4065 u8"u8função",
4066
4067 /* \377 (0xff) is Latin1 'ÿ'. */
4068 "yfunc\377",
4069
4070 /* \377 (0xff) is Latin1 'ÿ'. */
4071 "\377",
4072 "\377\377123",
4073
4074 /* A name with all sorts of complications. Starts with "z" to make
4075 it easier for the completion tests below. */
4076 #define Z_SYM_NAME \
4077 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4078 "::tuple<(anonymous namespace)::ui*, " \
4079 "std::default_delete<(anonymous namespace)::ui>, void>"
4080
4081 Z_SYM_NAME
4082 };
4083
4084 /* Returns true if the mapped_index_base::find_name_component_bounds
4085 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4086 in completion mode. */
4087
4088 static bool
4089 check_find_bounds_finds (mapped_index_base &index,
4090 const char *search_name,
4091 gdb::array_view<const char *> expected_syms)
4092 {
4093 lookup_name_info lookup_name (search_name,
4094 symbol_name_match_type::FULL, true);
4095
4096 auto bounds = index.find_name_components_bounds (lookup_name,
4097 language_cplus);
4098
4099 size_t distance = std::distance (bounds.first, bounds.second);
4100 if (distance != expected_syms.size ())
4101 return false;
4102
4103 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4104 {
4105 auto nc_elem = bounds.first + exp_elem;
4106 const char *qualified = index.symbol_name_at (nc_elem->idx);
4107 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4108 return false;
4109 }
4110
4111 return true;
4112 }
4113
4114 /* Test the lower-level mapped_index::find_name_component_bounds
4115 method. */
4116
4117 static void
4118 test_mapped_index_find_name_component_bounds ()
4119 {
4120 mock_mapped_index mock_index (test_symbols);
4121
4122 mock_index.build_name_components ();
4123
4124 /* Test the lower-level mapped_index::find_name_component_bounds
4125 method in completion mode. */
4126 {
4127 static const char *expected_syms[] = {
4128 "t1_func",
4129 "t1_func1",
4130 };
4131
4132 SELF_CHECK (check_find_bounds_finds (mock_index,
4133 "t1_func", expected_syms));
4134 }
4135
4136 /* Check that the increment-last-char in the name matching algorithm
4137 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4138 {
4139 static const char *expected_syms1[] = {
4140 "\377",
4141 "\377\377123",
4142 };
4143 SELF_CHECK (check_find_bounds_finds (mock_index,
4144 "\377", expected_syms1));
4145
4146 static const char *expected_syms2[] = {
4147 "\377\377123",
4148 };
4149 SELF_CHECK (check_find_bounds_finds (mock_index,
4150 "\377\377", expected_syms2));
4151 }
4152 }
4153
4154 /* Test dw2_expand_symtabs_matching_symbol. */
4155
4156 static void
4157 test_dw2_expand_symtabs_matching_symbol ()
4158 {
4159 mock_mapped_index mock_index (test_symbols);
4160
4161 /* We let all tests run until the end even if some fails, for debug
4162 convenience. */
4163 bool any_mismatch = false;
4164
4165 /* Create the expected symbols list (an initializer_list). Needed
4166 because lists have commas, and we need to pass them to CHECK,
4167 which is a macro. */
4168 #define EXPECT(...) { __VA_ARGS__ }
4169
4170 /* Wrapper for check_match that passes down the current
4171 __FILE__/__LINE__. */
4172 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4173 any_mismatch |= !check_match (__FILE__, __LINE__, \
4174 mock_index, \
4175 NAME, MATCH_TYPE, COMPLETION_MODE, \
4176 EXPECTED_LIST)
4177
4178 /* Identity checks. */
4179 for (const char *sym : test_symbols)
4180 {
4181 /* Should be able to match all existing symbols. */
4182 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4183 EXPECT (sym));
4184
4185 /* Should be able to match all existing symbols with
4186 parameters. */
4187 std::string with_params = std::string (sym) + "(int)";
4188 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4189 EXPECT (sym));
4190
4191 /* Should be able to match all existing symbols with
4192 parameters and qualifiers. */
4193 with_params = std::string (sym) + " ( int ) const";
4194 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4195 EXPECT (sym));
4196
4197 /* This should really find sym, but cp-name-parser.y doesn't
4198 know about lvalue/rvalue qualifiers yet. */
4199 with_params = std::string (sym) + " ( int ) &&";
4200 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4201 {});
4202 }
4203
4204 /* Check that the name matching algorithm for completion doesn't get
4205 confused with Latin1 'ÿ' / 0xff. */
4206 {
4207 static const char str[] = "\377";
4208 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4209 EXPECT ("\377", "\377\377123"));
4210 }
4211
4212 /* Check that the increment-last-char in the matching algorithm for
4213 completion doesn't match "t1_fund" when completing "t1_func". */
4214 {
4215 static const char str[] = "t1_func";
4216 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4217 EXPECT ("t1_func", "t1_func1"));
4218 }
4219
4220 /* Check that completion mode works at each prefix of the expected
4221 symbol name. */
4222 {
4223 static const char str[] = "function(int)";
4224 size_t len = strlen (str);
4225 std::string lookup;
4226
4227 for (size_t i = 1; i < len; i++)
4228 {
4229 lookup.assign (str, i);
4230 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4231 EXPECT ("function"));
4232 }
4233 }
4234
4235 /* While "w" is a prefix of both components, the match function
4236 should still only be called once. */
4237 {
4238 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4239 EXPECT ("w1::w2"));
4240 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4241 EXPECT ("w1::w2"));
4242 }
4243
4244 /* Same, with a "complicated" symbol. */
4245 {
4246 static const char str[] = Z_SYM_NAME;
4247 size_t len = strlen (str);
4248 std::string lookup;
4249
4250 for (size_t i = 1; i < len; i++)
4251 {
4252 lookup.assign (str, i);
4253 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4254 EXPECT (Z_SYM_NAME));
4255 }
4256 }
4257
4258 /* In FULL mode, an incomplete symbol doesn't match. */
4259 {
4260 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4261 {});
4262 }
4263
4264 /* A complete symbol with parameters matches any overload, since the
4265 index has no overload info. */
4266 {
4267 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4268 EXPECT ("std::zfunction", "std::zfunction2"));
4269 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4270 EXPECT ("std::zfunction", "std::zfunction2"));
4271 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4272 EXPECT ("std::zfunction", "std::zfunction2"));
4273 }
4274
4275 /* Check that whitespace is ignored appropriately. A symbol with a
4276 template argument list. */
4277 {
4278 static const char expected[] = "ns::foo<int>";
4279 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4280 EXPECT (expected));
4281 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4282 EXPECT (expected));
4283 }
4284
4285 /* Check that whitespace is ignored appropriately. A symbol with a
4286 template argument list that includes a pointer. */
4287 {
4288 static const char expected[] = "ns::foo<char*>";
4289 /* Try both completion and non-completion modes. */
4290 static const bool completion_mode[2] = {false, true};
4291 for (size_t i = 0; i < 2; i++)
4292 {
4293 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4294 completion_mode[i], EXPECT (expected));
4295 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4296 completion_mode[i], EXPECT (expected));
4297
4298 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4299 completion_mode[i], EXPECT (expected));
4300 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4301 completion_mode[i], EXPECT (expected));
4302 }
4303 }
4304
4305 {
4306 /* Check method qualifiers are ignored. */
4307 static const char expected[] = "ns::foo<char*>";
4308 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4309 symbol_name_match_type::FULL, true, EXPECT (expected));
4310 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4311 symbol_name_match_type::FULL, true, EXPECT (expected));
4312 CHECK_MATCH ("foo < char * > ( int ) const",
4313 symbol_name_match_type::WILD, true, EXPECT (expected));
4314 CHECK_MATCH ("foo < char * > ( int ) &&",
4315 symbol_name_match_type::WILD, true, EXPECT (expected));
4316 }
4317
4318 /* Test lookup names that don't match anything. */
4319 {
4320 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4321 {});
4322
4323 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4324 {});
4325 }
4326
4327 /* Some wild matching tests, exercising "(anonymous namespace)",
4328 which should not be confused with a parameter list. */
4329 {
4330 static const char *syms[] = {
4331 "A::B::C",
4332 "B::C",
4333 "C",
4334 "A :: B :: C ( int )",
4335 "B :: C ( int )",
4336 "C ( int )",
4337 };
4338
4339 for (const char *s : syms)
4340 {
4341 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4342 EXPECT ("(anonymous namespace)::A::B::C"));
4343 }
4344 }
4345
4346 {
4347 static const char expected[] = "ns2::tmpl<int>::foo2";
4348 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4349 EXPECT (expected));
4350 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4351 EXPECT (expected));
4352 }
4353
4354 SELF_CHECK (!any_mismatch);
4355
4356 #undef EXPECT
4357 #undef CHECK_MATCH
4358 }
4359
4360 static void
4361 run_test ()
4362 {
4363 test_mapped_index_find_name_component_bounds ();
4364 test_dw2_expand_symtabs_matching_symbol ();
4365 }
4366
4367 }} // namespace selftests::dw2_expand_symtabs_matching
4368
4369 #endif /* GDB_SELF_TEST */
4370
4371 /* If FILE_MATCHER is NULL or if PER_CU has
4372 dwarf2_per_cu_quick_data::MARK set (see
4373 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4374 EXPANSION_NOTIFY on it. */
4375
4376 static void
4377 dw2_expand_symtabs_matching_one
4378 (struct dwarf2_per_cu_data *per_cu,
4379 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4380 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4381 {
4382 if (file_matcher == NULL || per_cu->v.quick->mark)
4383 {
4384 bool symtab_was_null
4385 = (per_cu->v.quick->compunit_symtab == NULL);
4386
4387 dw2_instantiate_symtab (per_cu, false);
4388
4389 if (expansion_notify != NULL
4390 && symtab_was_null
4391 && per_cu->v.quick->compunit_symtab != NULL)
4392 expansion_notify (per_cu->v.quick->compunit_symtab);
4393 }
4394 }
4395
4396 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4397 matched, to expand corresponding CUs that were marked. IDX is the
4398 index of the symbol name that matched. */
4399
4400 static void
4401 dw2_expand_marked_cus
4402 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4403 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4404 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4405 search_domain kind)
4406 {
4407 offset_type *vec, vec_len, vec_idx;
4408 bool global_seen = false;
4409 mapped_index &index = *dwarf2_per_objfile->index_table;
4410
4411 vec = (offset_type *) (index.constant_pool
4412 + MAYBE_SWAP (index.symbol_table[idx].vec));
4413 vec_len = MAYBE_SWAP (vec[0]);
4414 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4415 {
4416 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4417 /* This value is only valid for index versions >= 7. */
4418 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4419 gdb_index_symbol_kind symbol_kind =
4420 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4421 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4422 /* Only check the symbol attributes if they're present.
4423 Indices prior to version 7 don't record them,
4424 and indices >= 7 may elide them for certain symbols
4425 (gold does this). */
4426 int attrs_valid =
4427 (index.version >= 7
4428 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4429
4430 /* Work around gold/15646. */
4431 if (attrs_valid)
4432 {
4433 if (!is_static && global_seen)
4434 continue;
4435 if (!is_static)
4436 global_seen = true;
4437 }
4438
4439 /* Only check the symbol's kind if it has one. */
4440 if (attrs_valid)
4441 {
4442 switch (kind)
4443 {
4444 case VARIABLES_DOMAIN:
4445 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4446 continue;
4447 break;
4448 case FUNCTIONS_DOMAIN:
4449 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4450 continue;
4451 break;
4452 case TYPES_DOMAIN:
4453 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4454 continue;
4455 break;
4456 case MODULES_DOMAIN:
4457 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4458 continue;
4459 break;
4460 default:
4461 break;
4462 }
4463 }
4464
4465 /* Don't crash on bad data. */
4466 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4467 + dwarf2_per_objfile->all_type_units.size ()))
4468 {
4469 complaint (_(".gdb_index entry has bad CU index"
4470 " [in module %s]"),
4471 objfile_name (dwarf2_per_objfile->objfile));
4472 continue;
4473 }
4474
4475 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4476 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4477 expansion_notify);
4478 }
4479 }
4480
4481 /* If FILE_MATCHER is non-NULL, set all the
4482 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4483 that match FILE_MATCHER. */
4484
4485 static void
4486 dw_expand_symtabs_matching_file_matcher
4487 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4488 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4489 {
4490 if (file_matcher == NULL)
4491 return;
4492
4493 objfile *const objfile = dwarf2_per_objfile->objfile;
4494
4495 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4496 htab_eq_pointer,
4497 NULL, xcalloc, xfree));
4498 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4499 htab_eq_pointer,
4500 NULL, xcalloc, xfree));
4501
4502 /* The rule is CUs specify all the files, including those used by
4503 any TU, so there's no need to scan TUs here. */
4504
4505 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4506 {
4507 QUIT;
4508
4509 per_cu->v.quick->mark = 0;
4510
4511 /* We only need to look at symtabs not already expanded. */
4512 if (per_cu->v.quick->compunit_symtab)
4513 continue;
4514
4515 quick_file_names *file_data = dw2_get_file_names (per_cu);
4516 if (file_data == NULL)
4517 continue;
4518
4519 if (htab_find (visited_not_found.get (), file_data) != NULL)
4520 continue;
4521 else if (htab_find (visited_found.get (), file_data) != NULL)
4522 {
4523 per_cu->v.quick->mark = 1;
4524 continue;
4525 }
4526
4527 for (int j = 0; j < file_data->num_file_names; ++j)
4528 {
4529 const char *this_real_name;
4530
4531 if (file_matcher (file_data->file_names[j], false))
4532 {
4533 per_cu->v.quick->mark = 1;
4534 break;
4535 }
4536
4537 /* Before we invoke realpath, which can get expensive when many
4538 files are involved, do a quick comparison of the basenames. */
4539 if (!basenames_may_differ
4540 && !file_matcher (lbasename (file_data->file_names[j]),
4541 true))
4542 continue;
4543
4544 this_real_name = dw2_get_real_path (objfile, file_data, j);
4545 if (file_matcher (this_real_name, false))
4546 {
4547 per_cu->v.quick->mark = 1;
4548 break;
4549 }
4550 }
4551
4552 void **slot = htab_find_slot (per_cu->v.quick->mark
4553 ? visited_found.get ()
4554 : visited_not_found.get (),
4555 file_data, INSERT);
4556 *slot = file_data;
4557 }
4558 }
4559
4560 static void
4561 dw2_expand_symtabs_matching
4562 (struct objfile *objfile,
4563 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4564 const lookup_name_info &lookup_name,
4565 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4566 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4567 enum search_domain kind)
4568 {
4569 struct dwarf2_per_objfile *dwarf2_per_objfile
4570 = get_dwarf2_per_objfile (objfile);
4571
4572 /* index_table is NULL if OBJF_READNOW. */
4573 if (!dwarf2_per_objfile->index_table)
4574 return;
4575
4576 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4577
4578 mapped_index &index = *dwarf2_per_objfile->index_table;
4579
4580 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4581 symbol_matcher,
4582 kind, [&] (offset_type idx)
4583 {
4584 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4585 expansion_notify, kind);
4586 return true;
4587 });
4588 }
4589
4590 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4591 symtab. */
4592
4593 static struct compunit_symtab *
4594 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4595 CORE_ADDR pc)
4596 {
4597 int i;
4598
4599 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4600 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4601 return cust;
4602
4603 if (cust->includes == NULL)
4604 return NULL;
4605
4606 for (i = 0; cust->includes[i]; ++i)
4607 {
4608 struct compunit_symtab *s = cust->includes[i];
4609
4610 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4611 if (s != NULL)
4612 return s;
4613 }
4614
4615 return NULL;
4616 }
4617
4618 static struct compunit_symtab *
4619 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4620 struct bound_minimal_symbol msymbol,
4621 CORE_ADDR pc,
4622 struct obj_section *section,
4623 int warn_if_readin)
4624 {
4625 struct dwarf2_per_cu_data *data;
4626 struct compunit_symtab *result;
4627
4628 if (!objfile->partial_symtabs->psymtabs_addrmap)
4629 return NULL;
4630
4631 CORE_ADDR baseaddr = objfile->text_section_offset ();
4632 data = (struct dwarf2_per_cu_data *) addrmap_find
4633 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4634 if (!data)
4635 return NULL;
4636
4637 if (warn_if_readin && data->v.quick->compunit_symtab)
4638 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4639 paddress (get_objfile_arch (objfile), pc));
4640
4641 result
4642 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4643 false),
4644 pc);
4645 gdb_assert (result != NULL);
4646 return result;
4647 }
4648
4649 static void
4650 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4651 void *data, int need_fullname)
4652 {
4653 struct dwarf2_per_objfile *dwarf2_per_objfile
4654 = get_dwarf2_per_objfile (objfile);
4655
4656 if (!dwarf2_per_objfile->filenames_cache)
4657 {
4658 dwarf2_per_objfile->filenames_cache.emplace ();
4659
4660 htab_up visited (htab_create_alloc (10,
4661 htab_hash_pointer, htab_eq_pointer,
4662 NULL, xcalloc, xfree));
4663
4664 /* The rule is CUs specify all the files, including those used
4665 by any TU, so there's no need to scan TUs here. We can
4666 ignore file names coming from already-expanded CUs. */
4667
4668 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4669 {
4670 if (per_cu->v.quick->compunit_symtab)
4671 {
4672 void **slot = htab_find_slot (visited.get (),
4673 per_cu->v.quick->file_names,
4674 INSERT);
4675
4676 *slot = per_cu->v.quick->file_names;
4677 }
4678 }
4679
4680 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4681 {
4682 /* We only need to look at symtabs not already expanded. */
4683 if (per_cu->v.quick->compunit_symtab)
4684 continue;
4685
4686 quick_file_names *file_data = dw2_get_file_names (per_cu);
4687 if (file_data == NULL)
4688 continue;
4689
4690 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4691 if (*slot)
4692 {
4693 /* Already visited. */
4694 continue;
4695 }
4696 *slot = file_data;
4697
4698 for (int j = 0; j < file_data->num_file_names; ++j)
4699 {
4700 const char *filename = file_data->file_names[j];
4701 dwarf2_per_objfile->filenames_cache->seen (filename);
4702 }
4703 }
4704 }
4705
4706 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4707 {
4708 gdb::unique_xmalloc_ptr<char> this_real_name;
4709
4710 if (need_fullname)
4711 this_real_name = gdb_realpath (filename);
4712 (*fun) (filename, this_real_name.get (), data);
4713 });
4714 }
4715
4716 static int
4717 dw2_has_symbols (struct objfile *objfile)
4718 {
4719 return 1;
4720 }
4721
4722 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4723 {
4724 dw2_has_symbols,
4725 dw2_find_last_source_symtab,
4726 dw2_forget_cached_source_info,
4727 dw2_map_symtabs_matching_filename,
4728 dw2_lookup_symbol,
4729 dw2_print_stats,
4730 dw2_dump,
4731 dw2_expand_symtabs_for_function,
4732 dw2_expand_all_symtabs,
4733 dw2_expand_symtabs_with_fullname,
4734 dw2_map_matching_symbols,
4735 dw2_expand_symtabs_matching,
4736 dw2_find_pc_sect_compunit_symtab,
4737 NULL,
4738 dw2_map_symbol_filenames
4739 };
4740
4741 /* DWARF-5 debug_names reader. */
4742
4743 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4744 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4745
4746 /* A helper function that reads the .debug_names section in SECTION
4747 and fills in MAP. FILENAME is the name of the file containing the
4748 section; it is used for error reporting.
4749
4750 Returns true if all went well, false otherwise. */
4751
4752 static bool
4753 read_debug_names_from_section (struct objfile *objfile,
4754 const char *filename,
4755 struct dwarf2_section_info *section,
4756 mapped_debug_names &map)
4757 {
4758 if (section->empty ())
4759 return false;
4760
4761 /* Older elfutils strip versions could keep the section in the main
4762 executable while splitting it for the separate debug info file. */
4763 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4764 return false;
4765
4766 section->read (objfile);
4767
4768 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4769
4770 const gdb_byte *addr = section->buffer;
4771
4772 bfd *const abfd = section->get_bfd_owner ();
4773
4774 unsigned int bytes_read;
4775 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4776 addr += bytes_read;
4777
4778 map.dwarf5_is_dwarf64 = bytes_read != 4;
4779 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4780 if (bytes_read + length != section->size)
4781 {
4782 /* There may be multiple per-CU indices. */
4783 warning (_("Section .debug_names in %s length %s does not match "
4784 "section length %s, ignoring .debug_names."),
4785 filename, plongest (bytes_read + length),
4786 pulongest (section->size));
4787 return false;
4788 }
4789
4790 /* The version number. */
4791 uint16_t version = read_2_bytes (abfd, addr);
4792 addr += 2;
4793 if (version != 5)
4794 {
4795 warning (_("Section .debug_names in %s has unsupported version %d, "
4796 "ignoring .debug_names."),
4797 filename, version);
4798 return false;
4799 }
4800
4801 /* Padding. */
4802 uint16_t padding = read_2_bytes (abfd, addr);
4803 addr += 2;
4804 if (padding != 0)
4805 {
4806 warning (_("Section .debug_names in %s has unsupported padding %d, "
4807 "ignoring .debug_names."),
4808 filename, padding);
4809 return false;
4810 }
4811
4812 /* comp_unit_count - The number of CUs in the CU list. */
4813 map.cu_count = read_4_bytes (abfd, addr);
4814 addr += 4;
4815
4816 /* local_type_unit_count - The number of TUs in the local TU
4817 list. */
4818 map.tu_count = read_4_bytes (abfd, addr);
4819 addr += 4;
4820
4821 /* foreign_type_unit_count - The number of TUs in the foreign TU
4822 list. */
4823 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4824 addr += 4;
4825 if (foreign_tu_count != 0)
4826 {
4827 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4828 "ignoring .debug_names."),
4829 filename, static_cast<unsigned long> (foreign_tu_count));
4830 return false;
4831 }
4832
4833 /* bucket_count - The number of hash buckets in the hash lookup
4834 table. */
4835 map.bucket_count = read_4_bytes (abfd, addr);
4836 addr += 4;
4837
4838 /* name_count - The number of unique names in the index. */
4839 map.name_count = read_4_bytes (abfd, addr);
4840 addr += 4;
4841
4842 /* abbrev_table_size - The size in bytes of the abbreviations
4843 table. */
4844 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4845 addr += 4;
4846
4847 /* augmentation_string_size - The size in bytes of the augmentation
4848 string. This value is rounded up to a multiple of 4. */
4849 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4850 addr += 4;
4851 map.augmentation_is_gdb = ((augmentation_string_size
4852 == sizeof (dwarf5_augmentation))
4853 && memcmp (addr, dwarf5_augmentation,
4854 sizeof (dwarf5_augmentation)) == 0);
4855 augmentation_string_size += (-augmentation_string_size) & 3;
4856 addr += augmentation_string_size;
4857
4858 /* List of CUs */
4859 map.cu_table_reordered = addr;
4860 addr += map.cu_count * map.offset_size;
4861
4862 /* List of Local TUs */
4863 map.tu_table_reordered = addr;
4864 addr += map.tu_count * map.offset_size;
4865
4866 /* Hash Lookup Table */
4867 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4868 addr += map.bucket_count * 4;
4869 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4870 addr += map.name_count * 4;
4871
4872 /* Name Table */
4873 map.name_table_string_offs_reordered = addr;
4874 addr += map.name_count * map.offset_size;
4875 map.name_table_entry_offs_reordered = addr;
4876 addr += map.name_count * map.offset_size;
4877
4878 const gdb_byte *abbrev_table_start = addr;
4879 for (;;)
4880 {
4881 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4882 addr += bytes_read;
4883 if (index_num == 0)
4884 break;
4885
4886 const auto insertpair
4887 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4888 if (!insertpair.second)
4889 {
4890 warning (_("Section .debug_names in %s has duplicate index %s, "
4891 "ignoring .debug_names."),
4892 filename, pulongest (index_num));
4893 return false;
4894 }
4895 mapped_debug_names::index_val &indexval = insertpair.first->second;
4896 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4897 addr += bytes_read;
4898
4899 for (;;)
4900 {
4901 mapped_debug_names::index_val::attr attr;
4902 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4903 addr += bytes_read;
4904 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4905 addr += bytes_read;
4906 if (attr.form == DW_FORM_implicit_const)
4907 {
4908 attr.implicit_const = read_signed_leb128 (abfd, addr,
4909 &bytes_read);
4910 addr += bytes_read;
4911 }
4912 if (attr.dw_idx == 0 && attr.form == 0)
4913 break;
4914 indexval.attr_vec.push_back (std::move (attr));
4915 }
4916 }
4917 if (addr != abbrev_table_start + abbrev_table_size)
4918 {
4919 warning (_("Section .debug_names in %s has abbreviation_table "
4920 "of size %s vs. written as %u, ignoring .debug_names."),
4921 filename, plongest (addr - abbrev_table_start),
4922 abbrev_table_size);
4923 return false;
4924 }
4925 map.entry_pool = addr;
4926
4927 return true;
4928 }
4929
4930 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4931 list. */
4932
4933 static void
4934 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4935 const mapped_debug_names &map,
4936 dwarf2_section_info &section,
4937 bool is_dwz)
4938 {
4939 sect_offset sect_off_prev;
4940 for (uint32_t i = 0; i <= map.cu_count; ++i)
4941 {
4942 sect_offset sect_off_next;
4943 if (i < map.cu_count)
4944 {
4945 sect_off_next
4946 = (sect_offset) (extract_unsigned_integer
4947 (map.cu_table_reordered + i * map.offset_size,
4948 map.offset_size,
4949 map.dwarf5_byte_order));
4950 }
4951 else
4952 sect_off_next = (sect_offset) section.size;
4953 if (i >= 1)
4954 {
4955 const ULONGEST length = sect_off_next - sect_off_prev;
4956 dwarf2_per_cu_data *per_cu
4957 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
4958 sect_off_prev, length);
4959 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
4960 }
4961 sect_off_prev = sect_off_next;
4962 }
4963 }
4964
4965 /* Read the CU list from the mapped index, and use it to create all
4966 the CU objects for this dwarf2_per_objfile. */
4967
4968 static void
4969 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
4970 const mapped_debug_names &map,
4971 const mapped_debug_names &dwz_map)
4972 {
4973 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
4974 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4975
4976 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
4977 dwarf2_per_objfile->info,
4978 false /* is_dwz */);
4979
4980 if (dwz_map.cu_count == 0)
4981 return;
4982
4983 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4984 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
4985 true /* is_dwz */);
4986 }
4987
4988 /* Read .debug_names. If everything went ok, initialize the "quick"
4989 elements of all the CUs and return true. Otherwise, return false. */
4990
4991 static bool
4992 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
4993 {
4994 std::unique_ptr<mapped_debug_names> map
4995 (new mapped_debug_names (dwarf2_per_objfile));
4996 mapped_debug_names dwz_map (dwarf2_per_objfile);
4997 struct objfile *objfile = dwarf2_per_objfile->objfile;
4998
4999 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5000 &dwarf2_per_objfile->debug_names,
5001 *map))
5002 return false;
5003
5004 /* Don't use the index if it's empty. */
5005 if (map->name_count == 0)
5006 return false;
5007
5008 /* If there is a .dwz file, read it so we can get its CU list as
5009 well. */
5010 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5011 if (dwz != NULL)
5012 {
5013 if (!read_debug_names_from_section (objfile,
5014 bfd_get_filename (dwz->dwz_bfd.get ()),
5015 &dwz->debug_names, dwz_map))
5016 {
5017 warning (_("could not read '.debug_names' section from %s; skipping"),
5018 bfd_get_filename (dwz->dwz_bfd.get ()));
5019 return false;
5020 }
5021 }
5022
5023 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5024
5025 if (map->tu_count != 0)
5026 {
5027 /* We can only handle a single .debug_types when we have an
5028 index. */
5029 if (dwarf2_per_objfile->types.size () != 1)
5030 return false;
5031
5032 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5033
5034 create_signatured_type_table_from_debug_names
5035 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5036 }
5037
5038 create_addrmap_from_aranges (dwarf2_per_objfile,
5039 &dwarf2_per_objfile->debug_aranges);
5040
5041 dwarf2_per_objfile->debug_names_table = std::move (map);
5042 dwarf2_per_objfile->using_index = 1;
5043 dwarf2_per_objfile->quick_file_names_table =
5044 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5045
5046 return true;
5047 }
5048
5049 /* Type used to manage iterating over all CUs looking for a symbol for
5050 .debug_names. */
5051
5052 class dw2_debug_names_iterator
5053 {
5054 public:
5055 dw2_debug_names_iterator (const mapped_debug_names &map,
5056 gdb::optional<block_enum> block_index,
5057 domain_enum domain,
5058 const char *name)
5059 : m_map (map), m_block_index (block_index), m_domain (domain),
5060 m_addr (find_vec_in_debug_names (map, name))
5061 {}
5062
5063 dw2_debug_names_iterator (const mapped_debug_names &map,
5064 search_domain search, uint32_t namei)
5065 : m_map (map),
5066 m_search (search),
5067 m_addr (find_vec_in_debug_names (map, namei))
5068 {}
5069
5070 dw2_debug_names_iterator (const mapped_debug_names &map,
5071 block_enum block_index, domain_enum domain,
5072 uint32_t namei)
5073 : m_map (map), m_block_index (block_index), m_domain (domain),
5074 m_addr (find_vec_in_debug_names (map, namei))
5075 {}
5076
5077 /* Return the next matching CU or NULL if there are no more. */
5078 dwarf2_per_cu_data *next ();
5079
5080 private:
5081 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5082 const char *name);
5083 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5084 uint32_t namei);
5085
5086 /* The internalized form of .debug_names. */
5087 const mapped_debug_names &m_map;
5088
5089 /* If set, only look for symbols that match that block. Valid values are
5090 GLOBAL_BLOCK and STATIC_BLOCK. */
5091 const gdb::optional<block_enum> m_block_index;
5092
5093 /* The kind of symbol we're looking for. */
5094 const domain_enum m_domain = UNDEF_DOMAIN;
5095 const search_domain m_search = ALL_DOMAIN;
5096
5097 /* The list of CUs from the index entry of the symbol, or NULL if
5098 not found. */
5099 const gdb_byte *m_addr;
5100 };
5101
5102 const char *
5103 mapped_debug_names::namei_to_name (uint32_t namei) const
5104 {
5105 const ULONGEST namei_string_offs
5106 = extract_unsigned_integer ((name_table_string_offs_reordered
5107 + namei * offset_size),
5108 offset_size,
5109 dwarf5_byte_order);
5110 return read_indirect_string_at_offset (dwarf2_per_objfile,
5111 namei_string_offs);
5112 }
5113
5114 /* Find a slot in .debug_names for the object named NAME. If NAME is
5115 found, return pointer to its pool data. If NAME cannot be found,
5116 return NULL. */
5117
5118 const gdb_byte *
5119 dw2_debug_names_iterator::find_vec_in_debug_names
5120 (const mapped_debug_names &map, const char *name)
5121 {
5122 int (*cmp) (const char *, const char *);
5123
5124 gdb::unique_xmalloc_ptr<char> without_params;
5125 if (current_language->la_language == language_cplus
5126 || current_language->la_language == language_fortran
5127 || current_language->la_language == language_d)
5128 {
5129 /* NAME is already canonical. Drop any qualifiers as
5130 .debug_names does not contain any. */
5131
5132 if (strchr (name, '(') != NULL)
5133 {
5134 without_params = cp_remove_params (name);
5135 if (without_params != NULL)
5136 name = without_params.get ();
5137 }
5138 }
5139
5140 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5141
5142 const uint32_t full_hash = dwarf5_djb_hash (name);
5143 uint32_t namei
5144 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5145 (map.bucket_table_reordered
5146 + (full_hash % map.bucket_count)), 4,
5147 map.dwarf5_byte_order);
5148 if (namei == 0)
5149 return NULL;
5150 --namei;
5151 if (namei >= map.name_count)
5152 {
5153 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5154 "[in module %s]"),
5155 namei, map.name_count,
5156 objfile_name (map.dwarf2_per_objfile->objfile));
5157 return NULL;
5158 }
5159
5160 for (;;)
5161 {
5162 const uint32_t namei_full_hash
5163 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5164 (map.hash_table_reordered + namei), 4,
5165 map.dwarf5_byte_order);
5166 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5167 return NULL;
5168
5169 if (full_hash == namei_full_hash)
5170 {
5171 const char *const namei_string = map.namei_to_name (namei);
5172
5173 #if 0 /* An expensive sanity check. */
5174 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5175 {
5176 complaint (_("Wrong .debug_names hash for string at index %u "
5177 "[in module %s]"),
5178 namei, objfile_name (dwarf2_per_objfile->objfile));
5179 return NULL;
5180 }
5181 #endif
5182
5183 if (cmp (namei_string, name) == 0)
5184 {
5185 const ULONGEST namei_entry_offs
5186 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5187 + namei * map.offset_size),
5188 map.offset_size, map.dwarf5_byte_order);
5189 return map.entry_pool + namei_entry_offs;
5190 }
5191 }
5192
5193 ++namei;
5194 if (namei >= map.name_count)
5195 return NULL;
5196 }
5197 }
5198
5199 const gdb_byte *
5200 dw2_debug_names_iterator::find_vec_in_debug_names
5201 (const mapped_debug_names &map, uint32_t namei)
5202 {
5203 if (namei >= map.name_count)
5204 {
5205 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5206 "[in module %s]"),
5207 namei, map.name_count,
5208 objfile_name (map.dwarf2_per_objfile->objfile));
5209 return NULL;
5210 }
5211
5212 const ULONGEST namei_entry_offs
5213 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5214 + namei * map.offset_size),
5215 map.offset_size, map.dwarf5_byte_order);
5216 return map.entry_pool + namei_entry_offs;
5217 }
5218
5219 /* See dw2_debug_names_iterator. */
5220
5221 dwarf2_per_cu_data *
5222 dw2_debug_names_iterator::next ()
5223 {
5224 if (m_addr == NULL)
5225 return NULL;
5226
5227 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5228 struct objfile *objfile = dwarf2_per_objfile->objfile;
5229 bfd *const abfd = objfile->obfd;
5230
5231 again:
5232
5233 unsigned int bytes_read;
5234 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5235 m_addr += bytes_read;
5236 if (abbrev == 0)
5237 return NULL;
5238
5239 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5240 if (indexval_it == m_map.abbrev_map.cend ())
5241 {
5242 complaint (_("Wrong .debug_names undefined abbrev code %s "
5243 "[in module %s]"),
5244 pulongest (abbrev), objfile_name (objfile));
5245 return NULL;
5246 }
5247 const mapped_debug_names::index_val &indexval = indexval_it->second;
5248 enum class symbol_linkage {
5249 unknown,
5250 static_,
5251 extern_,
5252 } symbol_linkage_ = symbol_linkage::unknown;
5253 dwarf2_per_cu_data *per_cu = NULL;
5254 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5255 {
5256 ULONGEST ull;
5257 switch (attr.form)
5258 {
5259 case DW_FORM_implicit_const:
5260 ull = attr.implicit_const;
5261 break;
5262 case DW_FORM_flag_present:
5263 ull = 1;
5264 break;
5265 case DW_FORM_udata:
5266 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5267 m_addr += bytes_read;
5268 break;
5269 default:
5270 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5271 dwarf_form_name (attr.form),
5272 objfile_name (objfile));
5273 return NULL;
5274 }
5275 switch (attr.dw_idx)
5276 {
5277 case DW_IDX_compile_unit:
5278 /* Don't crash on bad data. */
5279 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5280 {
5281 complaint (_(".debug_names entry has bad CU index %s"
5282 " [in module %s]"),
5283 pulongest (ull),
5284 objfile_name (dwarf2_per_objfile->objfile));
5285 continue;
5286 }
5287 per_cu = dwarf2_per_objfile->get_cutu (ull);
5288 break;
5289 case DW_IDX_type_unit:
5290 /* Don't crash on bad data. */
5291 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5292 {
5293 complaint (_(".debug_names entry has bad TU index %s"
5294 " [in module %s]"),
5295 pulongest (ull),
5296 objfile_name (dwarf2_per_objfile->objfile));
5297 continue;
5298 }
5299 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5300 break;
5301 case DW_IDX_GNU_internal:
5302 if (!m_map.augmentation_is_gdb)
5303 break;
5304 symbol_linkage_ = symbol_linkage::static_;
5305 break;
5306 case DW_IDX_GNU_external:
5307 if (!m_map.augmentation_is_gdb)
5308 break;
5309 symbol_linkage_ = symbol_linkage::extern_;
5310 break;
5311 }
5312 }
5313
5314 /* Skip if already read in. */
5315 if (per_cu->v.quick->compunit_symtab)
5316 goto again;
5317
5318 /* Check static vs global. */
5319 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5320 {
5321 const bool want_static = *m_block_index == STATIC_BLOCK;
5322 const bool symbol_is_static =
5323 symbol_linkage_ == symbol_linkage::static_;
5324 if (want_static != symbol_is_static)
5325 goto again;
5326 }
5327
5328 /* Match dw2_symtab_iter_next, symbol_kind
5329 and debug_names::psymbol_tag. */
5330 switch (m_domain)
5331 {
5332 case VAR_DOMAIN:
5333 switch (indexval.dwarf_tag)
5334 {
5335 case DW_TAG_variable:
5336 case DW_TAG_subprogram:
5337 /* Some types are also in VAR_DOMAIN. */
5338 case DW_TAG_typedef:
5339 case DW_TAG_structure_type:
5340 break;
5341 default:
5342 goto again;
5343 }
5344 break;
5345 case STRUCT_DOMAIN:
5346 switch (indexval.dwarf_tag)
5347 {
5348 case DW_TAG_typedef:
5349 case DW_TAG_structure_type:
5350 break;
5351 default:
5352 goto again;
5353 }
5354 break;
5355 case LABEL_DOMAIN:
5356 switch (indexval.dwarf_tag)
5357 {
5358 case 0:
5359 case DW_TAG_variable:
5360 break;
5361 default:
5362 goto again;
5363 }
5364 break;
5365 case MODULE_DOMAIN:
5366 switch (indexval.dwarf_tag)
5367 {
5368 case DW_TAG_module:
5369 break;
5370 default:
5371 goto again;
5372 }
5373 break;
5374 default:
5375 break;
5376 }
5377
5378 /* Match dw2_expand_symtabs_matching, symbol_kind and
5379 debug_names::psymbol_tag. */
5380 switch (m_search)
5381 {
5382 case VARIABLES_DOMAIN:
5383 switch (indexval.dwarf_tag)
5384 {
5385 case DW_TAG_variable:
5386 break;
5387 default:
5388 goto again;
5389 }
5390 break;
5391 case FUNCTIONS_DOMAIN:
5392 switch (indexval.dwarf_tag)
5393 {
5394 case DW_TAG_subprogram:
5395 break;
5396 default:
5397 goto again;
5398 }
5399 break;
5400 case TYPES_DOMAIN:
5401 switch (indexval.dwarf_tag)
5402 {
5403 case DW_TAG_typedef:
5404 case DW_TAG_structure_type:
5405 break;
5406 default:
5407 goto again;
5408 }
5409 break;
5410 case MODULES_DOMAIN:
5411 switch (indexval.dwarf_tag)
5412 {
5413 case DW_TAG_module:
5414 break;
5415 default:
5416 goto again;
5417 }
5418 default:
5419 break;
5420 }
5421
5422 return per_cu;
5423 }
5424
5425 static struct compunit_symtab *
5426 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5427 const char *name, domain_enum domain)
5428 {
5429 struct dwarf2_per_objfile *dwarf2_per_objfile
5430 = get_dwarf2_per_objfile (objfile);
5431
5432 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5433 if (!mapp)
5434 {
5435 /* index is NULL if OBJF_READNOW. */
5436 return NULL;
5437 }
5438 const auto &map = *mapp;
5439
5440 dw2_debug_names_iterator iter (map, block_index, domain, name);
5441
5442 struct compunit_symtab *stab_best = NULL;
5443 struct dwarf2_per_cu_data *per_cu;
5444 while ((per_cu = iter.next ()) != NULL)
5445 {
5446 struct symbol *sym, *with_opaque = NULL;
5447 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5448 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5449 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5450
5451 sym = block_find_symbol (block, name, domain,
5452 block_find_non_opaque_type_preferred,
5453 &with_opaque);
5454
5455 /* Some caution must be observed with overloaded functions and
5456 methods, since the index will not contain any overload
5457 information (but NAME might contain it). */
5458
5459 if (sym != NULL
5460 && strcmp_iw (sym->search_name (), name) == 0)
5461 return stab;
5462 if (with_opaque != NULL
5463 && strcmp_iw (with_opaque->search_name (), name) == 0)
5464 stab_best = stab;
5465
5466 /* Keep looking through other CUs. */
5467 }
5468
5469 return stab_best;
5470 }
5471
5472 /* This dumps minimal information about .debug_names. It is called
5473 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5474 uses this to verify that .debug_names has been loaded. */
5475
5476 static void
5477 dw2_debug_names_dump (struct objfile *objfile)
5478 {
5479 struct dwarf2_per_objfile *dwarf2_per_objfile
5480 = get_dwarf2_per_objfile (objfile);
5481
5482 gdb_assert (dwarf2_per_objfile->using_index);
5483 printf_filtered (".debug_names:");
5484 if (dwarf2_per_objfile->debug_names_table)
5485 printf_filtered (" exists\n");
5486 else
5487 printf_filtered (" faked for \"readnow\"\n");
5488 printf_filtered ("\n");
5489 }
5490
5491 static void
5492 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5493 const char *func_name)
5494 {
5495 struct dwarf2_per_objfile *dwarf2_per_objfile
5496 = get_dwarf2_per_objfile (objfile);
5497
5498 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5499 if (dwarf2_per_objfile->debug_names_table)
5500 {
5501 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5502
5503 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5504
5505 struct dwarf2_per_cu_data *per_cu;
5506 while ((per_cu = iter.next ()) != NULL)
5507 dw2_instantiate_symtab (per_cu, false);
5508 }
5509 }
5510
5511 static void
5512 dw2_debug_names_map_matching_symbols
5513 (struct objfile *objfile,
5514 const lookup_name_info &name, domain_enum domain,
5515 int global,
5516 gdb::function_view<symbol_found_callback_ftype> callback,
5517 symbol_compare_ftype *ordered_compare)
5518 {
5519 struct dwarf2_per_objfile *dwarf2_per_objfile
5520 = get_dwarf2_per_objfile (objfile);
5521
5522 /* debug_names_table is NULL if OBJF_READNOW. */
5523 if (!dwarf2_per_objfile->debug_names_table)
5524 return;
5525
5526 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5527 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5528
5529 const char *match_name = name.ada ().lookup_name ().c_str ();
5530 auto matcher = [&] (const char *symname)
5531 {
5532 if (ordered_compare == nullptr)
5533 return true;
5534 return ordered_compare (symname, match_name) == 0;
5535 };
5536
5537 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5538 [&] (offset_type namei)
5539 {
5540 /* The name was matched, now expand corresponding CUs that were
5541 marked. */
5542 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5543
5544 struct dwarf2_per_cu_data *per_cu;
5545 while ((per_cu = iter.next ()) != NULL)
5546 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5547 return true;
5548 });
5549
5550 /* It's a shame we couldn't do this inside the
5551 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5552 that have already been expanded. Instead, this loop matches what
5553 the psymtab code does. */
5554 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5555 {
5556 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5557 if (cust != nullptr)
5558 {
5559 const struct block *block
5560 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5561 if (!iterate_over_symbols_terminated (block, name,
5562 domain, callback))
5563 break;
5564 }
5565 }
5566 }
5567
5568 static void
5569 dw2_debug_names_expand_symtabs_matching
5570 (struct objfile *objfile,
5571 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5572 const lookup_name_info &lookup_name,
5573 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5574 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5575 enum search_domain kind)
5576 {
5577 struct dwarf2_per_objfile *dwarf2_per_objfile
5578 = get_dwarf2_per_objfile (objfile);
5579
5580 /* debug_names_table is NULL if OBJF_READNOW. */
5581 if (!dwarf2_per_objfile->debug_names_table)
5582 return;
5583
5584 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5585
5586 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5587
5588 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5589 symbol_matcher,
5590 kind, [&] (offset_type namei)
5591 {
5592 /* The name was matched, now expand corresponding CUs that were
5593 marked. */
5594 dw2_debug_names_iterator iter (map, kind, namei);
5595
5596 struct dwarf2_per_cu_data *per_cu;
5597 while ((per_cu = iter.next ()) != NULL)
5598 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5599 expansion_notify);
5600 return true;
5601 });
5602 }
5603
5604 const struct quick_symbol_functions dwarf2_debug_names_functions =
5605 {
5606 dw2_has_symbols,
5607 dw2_find_last_source_symtab,
5608 dw2_forget_cached_source_info,
5609 dw2_map_symtabs_matching_filename,
5610 dw2_debug_names_lookup_symbol,
5611 dw2_print_stats,
5612 dw2_debug_names_dump,
5613 dw2_debug_names_expand_symtabs_for_function,
5614 dw2_expand_all_symtabs,
5615 dw2_expand_symtabs_with_fullname,
5616 dw2_debug_names_map_matching_symbols,
5617 dw2_debug_names_expand_symtabs_matching,
5618 dw2_find_pc_sect_compunit_symtab,
5619 NULL,
5620 dw2_map_symbol_filenames
5621 };
5622
5623 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5624 to either a dwarf2_per_objfile or dwz_file object. */
5625
5626 template <typename T>
5627 static gdb::array_view<const gdb_byte>
5628 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5629 {
5630 dwarf2_section_info *section = &section_owner->gdb_index;
5631
5632 if (section->empty ())
5633 return {};
5634
5635 /* Older elfutils strip versions could keep the section in the main
5636 executable while splitting it for the separate debug info file. */
5637 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5638 return {};
5639
5640 section->read (obj);
5641
5642 /* dwarf2_section_info::size is a bfd_size_type, while
5643 gdb::array_view works with size_t. On 32-bit hosts, with
5644 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5645 is 32-bit. So we need an explicit narrowing conversion here.
5646 This is fine, because it's impossible to allocate or mmap an
5647 array/buffer larger than what size_t can represent. */
5648 return gdb::make_array_view (section->buffer, section->size);
5649 }
5650
5651 /* Lookup the index cache for the contents of the index associated to
5652 DWARF2_OBJ. */
5653
5654 static gdb::array_view<const gdb_byte>
5655 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5656 {
5657 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5658 if (build_id == nullptr)
5659 return {};
5660
5661 return global_index_cache.lookup_gdb_index (build_id,
5662 &dwarf2_obj->index_cache_res);
5663 }
5664
5665 /* Same as the above, but for DWZ. */
5666
5667 static gdb::array_view<const gdb_byte>
5668 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5669 {
5670 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5671 if (build_id == nullptr)
5672 return {};
5673
5674 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5675 }
5676
5677 /* See symfile.h. */
5678
5679 bool
5680 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5681 {
5682 struct dwarf2_per_objfile *dwarf2_per_objfile
5683 = get_dwarf2_per_objfile (objfile);
5684
5685 /* If we're about to read full symbols, don't bother with the
5686 indices. In this case we also don't care if some other debug
5687 format is making psymtabs, because they are all about to be
5688 expanded anyway. */
5689 if ((objfile->flags & OBJF_READNOW))
5690 {
5691 dwarf2_per_objfile->using_index = 1;
5692 create_all_comp_units (dwarf2_per_objfile);
5693 create_all_type_units (dwarf2_per_objfile);
5694 dwarf2_per_objfile->quick_file_names_table
5695 = create_quick_file_names_table
5696 (dwarf2_per_objfile->all_comp_units.size ());
5697
5698 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5699 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5700 {
5701 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5702
5703 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5704 struct dwarf2_per_cu_quick_data);
5705 }
5706
5707 /* Return 1 so that gdb sees the "quick" functions. However,
5708 these functions will be no-ops because we will have expanded
5709 all symtabs. */
5710 *index_kind = dw_index_kind::GDB_INDEX;
5711 return true;
5712 }
5713
5714 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5715 {
5716 *index_kind = dw_index_kind::DEBUG_NAMES;
5717 return true;
5718 }
5719
5720 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5721 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5722 get_gdb_index_contents_from_section<dwz_file>))
5723 {
5724 *index_kind = dw_index_kind::GDB_INDEX;
5725 return true;
5726 }
5727
5728 /* ... otherwise, try to find the index in the index cache. */
5729 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5730 get_gdb_index_contents_from_cache,
5731 get_gdb_index_contents_from_cache_dwz))
5732 {
5733 global_index_cache.hit ();
5734 *index_kind = dw_index_kind::GDB_INDEX;
5735 return true;
5736 }
5737
5738 global_index_cache.miss ();
5739 return false;
5740 }
5741
5742 \f
5743
5744 /* Build a partial symbol table. */
5745
5746 void
5747 dwarf2_build_psymtabs (struct objfile *objfile)
5748 {
5749 struct dwarf2_per_objfile *dwarf2_per_objfile
5750 = get_dwarf2_per_objfile (objfile);
5751
5752 init_psymbol_list (objfile, 1024);
5753
5754 try
5755 {
5756 /* This isn't really ideal: all the data we allocate on the
5757 objfile's obstack is still uselessly kept around. However,
5758 freeing it seems unsafe. */
5759 psymtab_discarder psymtabs (objfile);
5760 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5761 psymtabs.keep ();
5762
5763 /* (maybe) store an index in the cache. */
5764 global_index_cache.store (dwarf2_per_objfile);
5765 }
5766 catch (const gdb_exception_error &except)
5767 {
5768 exception_print (gdb_stderr, except);
5769 }
5770 }
5771
5772 /* Find the base address of the compilation unit for range lists and
5773 location lists. It will normally be specified by DW_AT_low_pc.
5774 In DWARF-3 draft 4, the base address could be overridden by
5775 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5776 compilation units with discontinuous ranges. */
5777
5778 static void
5779 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5780 {
5781 struct attribute *attr;
5782
5783 cu->base_address.reset ();
5784
5785 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5786 if (attr != nullptr)
5787 cu->base_address = attr->value_as_address ();
5788 else
5789 {
5790 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5791 if (attr != nullptr)
5792 cu->base_address = attr->value_as_address ();
5793 }
5794 }
5795
5796 /* Helper function that returns the proper abbrev section for
5797 THIS_CU. */
5798
5799 static struct dwarf2_section_info *
5800 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5801 {
5802 struct dwarf2_section_info *abbrev;
5803 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5804
5805 if (this_cu->is_dwz)
5806 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5807 else
5808 abbrev = &dwarf2_per_objfile->abbrev;
5809
5810 return abbrev;
5811 }
5812
5813 /* Fetch the abbreviation table offset from a comp or type unit header. */
5814
5815 static sect_offset
5816 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5817 struct dwarf2_section_info *section,
5818 sect_offset sect_off)
5819 {
5820 bfd *abfd = section->get_bfd_owner ();
5821 const gdb_byte *info_ptr;
5822 unsigned int initial_length_size, offset_size;
5823 uint16_t version;
5824
5825 section->read (dwarf2_per_objfile->objfile);
5826 info_ptr = section->buffer + to_underlying (sect_off);
5827 read_initial_length (abfd, info_ptr, &initial_length_size);
5828 offset_size = initial_length_size == 4 ? 4 : 8;
5829 info_ptr += initial_length_size;
5830
5831 version = read_2_bytes (abfd, info_ptr);
5832 info_ptr += 2;
5833 if (version >= 5)
5834 {
5835 /* Skip unit type and address size. */
5836 info_ptr += 2;
5837 }
5838
5839 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5840 }
5841
5842 /* A partial symtab that is used only for include files. */
5843 struct dwarf2_include_psymtab : public partial_symtab
5844 {
5845 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5846 : partial_symtab (filename, objfile)
5847 {
5848 }
5849
5850 void read_symtab (struct objfile *objfile) override
5851 {
5852 expand_psymtab (objfile);
5853 }
5854
5855 void expand_psymtab (struct objfile *objfile) override
5856 {
5857 if (m_readin)
5858 return;
5859 /* It's an include file, no symbols to read for it.
5860 Everything is in the parent symtab. */
5861 read_dependencies (objfile);
5862 m_readin = true;
5863 }
5864
5865 bool readin_p () const override
5866 {
5867 return m_readin;
5868 }
5869
5870 struct compunit_symtab *get_compunit_symtab () const override
5871 {
5872 return nullptr;
5873 }
5874
5875 private:
5876
5877 bool m_readin = false;
5878 };
5879
5880 /* Allocate a new partial symtab for file named NAME and mark this new
5881 partial symtab as being an include of PST. */
5882
5883 static void
5884 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5885 struct objfile *objfile)
5886 {
5887 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5888
5889 if (!IS_ABSOLUTE_PATH (subpst->filename))
5890 {
5891 /* It shares objfile->objfile_obstack. */
5892 subpst->dirname = pst->dirname;
5893 }
5894
5895 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5896 subpst->dependencies[0] = pst;
5897 subpst->number_of_dependencies = 1;
5898 }
5899
5900 /* Read the Line Number Program data and extract the list of files
5901 included by the source file represented by PST. Build an include
5902 partial symtab for each of these included files. */
5903
5904 static void
5905 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5906 struct die_info *die,
5907 dwarf2_psymtab *pst)
5908 {
5909 line_header_up lh;
5910 struct attribute *attr;
5911
5912 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5913 if (attr != nullptr)
5914 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5915 if (lh == NULL)
5916 return; /* No linetable, so no includes. */
5917
5918 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5919 that we pass in the raw text_low here; that is ok because we're
5920 only decoding the line table to make include partial symtabs, and
5921 so the addresses aren't really used. */
5922 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5923 pst->raw_text_low (), 1);
5924 }
5925
5926 static hashval_t
5927 hash_signatured_type (const void *item)
5928 {
5929 const struct signatured_type *sig_type
5930 = (const struct signatured_type *) item;
5931
5932 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5933 return sig_type->signature;
5934 }
5935
5936 static int
5937 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5938 {
5939 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5940 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5941
5942 return lhs->signature == rhs->signature;
5943 }
5944
5945 /* Allocate a hash table for signatured types. */
5946
5947 static htab_up
5948 allocate_signatured_type_table ()
5949 {
5950 return htab_up (htab_create_alloc (41,
5951 hash_signatured_type,
5952 eq_signatured_type,
5953 NULL, xcalloc, xfree));
5954 }
5955
5956 /* A helper function to add a signatured type CU to a table. */
5957
5958 static int
5959 add_signatured_type_cu_to_table (void **slot, void *datum)
5960 {
5961 struct signatured_type *sigt = (struct signatured_type *) *slot;
5962 std::vector<signatured_type *> *all_type_units
5963 = (std::vector<signatured_type *> *) datum;
5964
5965 all_type_units->push_back (sigt);
5966
5967 return 1;
5968 }
5969
5970 /* A helper for create_debug_types_hash_table. Read types from SECTION
5971 and fill them into TYPES_HTAB. It will process only type units,
5972 therefore DW_UT_type. */
5973
5974 static void
5975 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
5976 struct dwo_file *dwo_file,
5977 dwarf2_section_info *section, htab_up &types_htab,
5978 rcuh_kind section_kind)
5979 {
5980 struct objfile *objfile = dwarf2_per_objfile->objfile;
5981 struct dwarf2_section_info *abbrev_section;
5982 bfd *abfd;
5983 const gdb_byte *info_ptr, *end_ptr;
5984
5985 abbrev_section = (dwo_file != NULL
5986 ? &dwo_file->sections.abbrev
5987 : &dwarf2_per_objfile->abbrev);
5988
5989 if (dwarf_read_debug)
5990 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
5991 section->get_name (),
5992 abbrev_section->get_file_name ());
5993
5994 section->read (objfile);
5995 info_ptr = section->buffer;
5996
5997 if (info_ptr == NULL)
5998 return;
5999
6000 /* We can't set abfd until now because the section may be empty or
6001 not present, in which case the bfd is unknown. */
6002 abfd = section->get_bfd_owner ();
6003
6004 /* We don't use cutu_reader here because we don't need to read
6005 any dies: the signature is in the header. */
6006
6007 end_ptr = info_ptr + section->size;
6008 while (info_ptr < end_ptr)
6009 {
6010 struct signatured_type *sig_type;
6011 struct dwo_unit *dwo_tu;
6012 void **slot;
6013 const gdb_byte *ptr = info_ptr;
6014 struct comp_unit_head header;
6015 unsigned int length;
6016
6017 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6018
6019 /* Initialize it due to a false compiler warning. */
6020 header.signature = -1;
6021 header.type_cu_offset_in_tu = (cu_offset) -1;
6022
6023 /* We need to read the type's signature in order to build the hash
6024 table, but we don't need anything else just yet. */
6025
6026 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6027 abbrev_section, ptr, section_kind);
6028
6029 length = header.get_length ();
6030
6031 /* Skip dummy type units. */
6032 if (ptr >= info_ptr + length
6033 || peek_abbrev_code (abfd, ptr) == 0
6034 || header.unit_type != DW_UT_type)
6035 {
6036 info_ptr += length;
6037 continue;
6038 }
6039
6040 if (types_htab == NULL)
6041 {
6042 if (dwo_file)
6043 types_htab = allocate_dwo_unit_table ();
6044 else
6045 types_htab = allocate_signatured_type_table ();
6046 }
6047
6048 if (dwo_file)
6049 {
6050 sig_type = NULL;
6051 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6052 struct dwo_unit);
6053 dwo_tu->dwo_file = dwo_file;
6054 dwo_tu->signature = header.signature;
6055 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6056 dwo_tu->section = section;
6057 dwo_tu->sect_off = sect_off;
6058 dwo_tu->length = length;
6059 }
6060 else
6061 {
6062 /* N.B.: type_offset is not usable if this type uses a DWO file.
6063 The real type_offset is in the DWO file. */
6064 dwo_tu = NULL;
6065 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6066 struct signatured_type);
6067 sig_type->signature = header.signature;
6068 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6069 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6070 sig_type->per_cu.is_debug_types = 1;
6071 sig_type->per_cu.section = section;
6072 sig_type->per_cu.sect_off = sect_off;
6073 sig_type->per_cu.length = length;
6074 }
6075
6076 slot = htab_find_slot (types_htab.get (),
6077 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6078 INSERT);
6079 gdb_assert (slot != NULL);
6080 if (*slot != NULL)
6081 {
6082 sect_offset dup_sect_off;
6083
6084 if (dwo_file)
6085 {
6086 const struct dwo_unit *dup_tu
6087 = (const struct dwo_unit *) *slot;
6088
6089 dup_sect_off = dup_tu->sect_off;
6090 }
6091 else
6092 {
6093 const struct signatured_type *dup_tu
6094 = (const struct signatured_type *) *slot;
6095
6096 dup_sect_off = dup_tu->per_cu.sect_off;
6097 }
6098
6099 complaint (_("debug type entry at offset %s is duplicate to"
6100 " the entry at offset %s, signature %s"),
6101 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6102 hex_string (header.signature));
6103 }
6104 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6105
6106 if (dwarf_read_debug > 1)
6107 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6108 sect_offset_str (sect_off),
6109 hex_string (header.signature));
6110
6111 info_ptr += length;
6112 }
6113 }
6114
6115 /* Create the hash table of all entries in the .debug_types
6116 (or .debug_types.dwo) section(s).
6117 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6118 otherwise it is NULL.
6119
6120 The result is a pointer to the hash table or NULL if there are no types.
6121
6122 Note: This function processes DWO files only, not DWP files. */
6123
6124 static void
6125 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6126 struct dwo_file *dwo_file,
6127 gdb::array_view<dwarf2_section_info> type_sections,
6128 htab_up &types_htab)
6129 {
6130 for (dwarf2_section_info &section : type_sections)
6131 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6132 types_htab, rcuh_kind::TYPE);
6133 }
6134
6135 /* Create the hash table of all entries in the .debug_types section,
6136 and initialize all_type_units.
6137 The result is zero if there is an error (e.g. missing .debug_types section),
6138 otherwise non-zero. */
6139
6140 static int
6141 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6142 {
6143 htab_up types_htab;
6144
6145 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6146 &dwarf2_per_objfile->info, types_htab,
6147 rcuh_kind::COMPILE);
6148 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6149 dwarf2_per_objfile->types, types_htab);
6150 if (types_htab == NULL)
6151 {
6152 dwarf2_per_objfile->signatured_types = NULL;
6153 return 0;
6154 }
6155
6156 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6157
6158 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6159 dwarf2_per_objfile->all_type_units.reserve
6160 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6161
6162 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6163 add_signatured_type_cu_to_table,
6164 &dwarf2_per_objfile->all_type_units);
6165
6166 return 1;
6167 }
6168
6169 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6170 If SLOT is non-NULL, it is the entry to use in the hash table.
6171 Otherwise we find one. */
6172
6173 static struct signatured_type *
6174 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6175 void **slot)
6176 {
6177 struct objfile *objfile = dwarf2_per_objfile->objfile;
6178
6179 if (dwarf2_per_objfile->all_type_units.size ()
6180 == dwarf2_per_objfile->all_type_units.capacity ())
6181 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6182
6183 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6184 struct signatured_type);
6185
6186 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6187 sig_type->signature = sig;
6188 sig_type->per_cu.is_debug_types = 1;
6189 if (dwarf2_per_objfile->using_index)
6190 {
6191 sig_type->per_cu.v.quick =
6192 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6193 struct dwarf2_per_cu_quick_data);
6194 }
6195
6196 if (slot == NULL)
6197 {
6198 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6199 sig_type, INSERT);
6200 }
6201 gdb_assert (*slot == NULL);
6202 *slot = sig_type;
6203 /* The rest of sig_type must be filled in by the caller. */
6204 return sig_type;
6205 }
6206
6207 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6208 Fill in SIG_ENTRY with DWO_ENTRY. */
6209
6210 static void
6211 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6212 struct signatured_type *sig_entry,
6213 struct dwo_unit *dwo_entry)
6214 {
6215 /* Make sure we're not clobbering something we don't expect to. */
6216 gdb_assert (! sig_entry->per_cu.queued);
6217 gdb_assert (sig_entry->per_cu.cu == NULL);
6218 if (dwarf2_per_objfile->using_index)
6219 {
6220 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6221 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6222 }
6223 else
6224 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6225 gdb_assert (sig_entry->signature == dwo_entry->signature);
6226 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6227 gdb_assert (sig_entry->type_unit_group == NULL);
6228 gdb_assert (sig_entry->dwo_unit == NULL);
6229
6230 sig_entry->per_cu.section = dwo_entry->section;
6231 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6232 sig_entry->per_cu.length = dwo_entry->length;
6233 sig_entry->per_cu.reading_dwo_directly = 1;
6234 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6235 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6236 sig_entry->dwo_unit = dwo_entry;
6237 }
6238
6239 /* Subroutine of lookup_signatured_type.
6240 If we haven't read the TU yet, create the signatured_type data structure
6241 for a TU to be read in directly from a DWO file, bypassing the stub.
6242 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6243 using .gdb_index, then when reading a CU we want to stay in the DWO file
6244 containing that CU. Otherwise we could end up reading several other DWO
6245 files (due to comdat folding) to process the transitive closure of all the
6246 mentioned TUs, and that can be slow. The current DWO file will have every
6247 type signature that it needs.
6248 We only do this for .gdb_index because in the psymtab case we already have
6249 to read all the DWOs to build the type unit groups. */
6250
6251 static struct signatured_type *
6252 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6253 {
6254 struct dwarf2_per_objfile *dwarf2_per_objfile
6255 = cu->per_cu->dwarf2_per_objfile;
6256 struct dwo_file *dwo_file;
6257 struct dwo_unit find_dwo_entry, *dwo_entry;
6258 struct signatured_type find_sig_entry, *sig_entry;
6259 void **slot;
6260
6261 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6262
6263 /* If TU skeletons have been removed then we may not have read in any
6264 TUs yet. */
6265 if (dwarf2_per_objfile->signatured_types == NULL)
6266 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6267
6268 /* We only ever need to read in one copy of a signatured type.
6269 Use the global signatured_types array to do our own comdat-folding
6270 of types. If this is the first time we're reading this TU, and
6271 the TU has an entry in .gdb_index, replace the recorded data from
6272 .gdb_index with this TU. */
6273
6274 find_sig_entry.signature = sig;
6275 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6276 &find_sig_entry, INSERT);
6277 sig_entry = (struct signatured_type *) *slot;
6278
6279 /* We can get here with the TU already read, *or* in the process of being
6280 read. Don't reassign the global entry to point to this DWO if that's
6281 the case. Also note that if the TU is already being read, it may not
6282 have come from a DWO, the program may be a mix of Fission-compiled
6283 code and non-Fission-compiled code. */
6284
6285 /* Have we already tried to read this TU?
6286 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6287 needn't exist in the global table yet). */
6288 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6289 return sig_entry;
6290
6291 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6292 dwo_unit of the TU itself. */
6293 dwo_file = cu->dwo_unit->dwo_file;
6294
6295 /* Ok, this is the first time we're reading this TU. */
6296 if (dwo_file->tus == NULL)
6297 return NULL;
6298 find_dwo_entry.signature = sig;
6299 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6300 &find_dwo_entry);
6301 if (dwo_entry == NULL)
6302 return NULL;
6303
6304 /* If the global table doesn't have an entry for this TU, add one. */
6305 if (sig_entry == NULL)
6306 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6307
6308 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6309 sig_entry->per_cu.tu_read = 1;
6310 return sig_entry;
6311 }
6312
6313 /* Subroutine of lookup_signatured_type.
6314 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6315 then try the DWP file. If the TU stub (skeleton) has been removed then
6316 it won't be in .gdb_index. */
6317
6318 static struct signatured_type *
6319 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6320 {
6321 struct dwarf2_per_objfile *dwarf2_per_objfile
6322 = cu->per_cu->dwarf2_per_objfile;
6323 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6324 struct dwo_unit *dwo_entry;
6325 struct signatured_type find_sig_entry, *sig_entry;
6326 void **slot;
6327
6328 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6329 gdb_assert (dwp_file != NULL);
6330
6331 /* If TU skeletons have been removed then we may not have read in any
6332 TUs yet. */
6333 if (dwarf2_per_objfile->signatured_types == NULL)
6334 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6335
6336 find_sig_entry.signature = sig;
6337 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6338 &find_sig_entry, INSERT);
6339 sig_entry = (struct signatured_type *) *slot;
6340
6341 /* Have we already tried to read this TU?
6342 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6343 needn't exist in the global table yet). */
6344 if (sig_entry != NULL)
6345 return sig_entry;
6346
6347 if (dwp_file->tus == NULL)
6348 return NULL;
6349 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6350 sig, 1 /* is_debug_types */);
6351 if (dwo_entry == NULL)
6352 return NULL;
6353
6354 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6355 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6356
6357 return sig_entry;
6358 }
6359
6360 /* Lookup a signature based type for DW_FORM_ref_sig8.
6361 Returns NULL if signature SIG is not present in the table.
6362 It is up to the caller to complain about this. */
6363
6364 static struct signatured_type *
6365 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6366 {
6367 struct dwarf2_per_objfile *dwarf2_per_objfile
6368 = cu->per_cu->dwarf2_per_objfile;
6369
6370 if (cu->dwo_unit
6371 && dwarf2_per_objfile->using_index)
6372 {
6373 /* We're in a DWO/DWP file, and we're using .gdb_index.
6374 These cases require special processing. */
6375 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6376 return lookup_dwo_signatured_type (cu, sig);
6377 else
6378 return lookup_dwp_signatured_type (cu, sig);
6379 }
6380 else
6381 {
6382 struct signatured_type find_entry, *entry;
6383
6384 if (dwarf2_per_objfile->signatured_types == NULL)
6385 return NULL;
6386 find_entry.signature = sig;
6387 entry = ((struct signatured_type *)
6388 htab_find (dwarf2_per_objfile->signatured_types.get (),
6389 &find_entry));
6390 return entry;
6391 }
6392 }
6393
6394 /* Return the address base of the compile unit, which, if exists, is stored
6395 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6396 static gdb::optional<ULONGEST>
6397 lookup_addr_base (struct die_info *comp_unit_die)
6398 {
6399 struct attribute *attr;
6400 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6401 if (attr == nullptr)
6402 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6403 if (attr == nullptr)
6404 return gdb::optional<ULONGEST> ();
6405 return DW_UNSND (attr);
6406 }
6407
6408 /* Return range lists base of the compile unit, which, if exists, is stored
6409 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6410 static ULONGEST
6411 lookup_ranges_base (struct die_info *comp_unit_die)
6412 {
6413 struct attribute *attr;
6414 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6415 if (attr == nullptr)
6416 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6417 if (attr == nullptr)
6418 return 0;
6419 return DW_UNSND (attr);
6420 }
6421
6422 /* Low level DIE reading support. */
6423
6424 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6425
6426 static void
6427 init_cu_die_reader (struct die_reader_specs *reader,
6428 struct dwarf2_cu *cu,
6429 struct dwarf2_section_info *section,
6430 struct dwo_file *dwo_file,
6431 struct abbrev_table *abbrev_table)
6432 {
6433 gdb_assert (section->readin && section->buffer != NULL);
6434 reader->abfd = section->get_bfd_owner ();
6435 reader->cu = cu;
6436 reader->dwo_file = dwo_file;
6437 reader->die_section = section;
6438 reader->buffer = section->buffer;
6439 reader->buffer_end = section->buffer + section->size;
6440 reader->abbrev_table = abbrev_table;
6441 }
6442
6443 /* Subroutine of cutu_reader to simplify it.
6444 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6445 There's just a lot of work to do, and cutu_reader is big enough
6446 already.
6447
6448 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6449 from it to the DIE in the DWO. If NULL we are skipping the stub.
6450 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6451 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6452 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6453 STUB_COMP_DIR may be non-NULL.
6454 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6455 are filled in with the info of the DIE from the DWO file.
6456 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6457 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6458 kept around for at least as long as *RESULT_READER.
6459
6460 The result is non-zero if a valid (non-dummy) DIE was found. */
6461
6462 static int
6463 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6464 struct dwo_unit *dwo_unit,
6465 struct die_info *stub_comp_unit_die,
6466 const char *stub_comp_dir,
6467 struct die_reader_specs *result_reader,
6468 const gdb_byte **result_info_ptr,
6469 struct die_info **result_comp_unit_die,
6470 abbrev_table_up *result_dwo_abbrev_table)
6471 {
6472 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6473 struct objfile *objfile = dwarf2_per_objfile->objfile;
6474 struct dwarf2_cu *cu = this_cu->cu;
6475 bfd *abfd;
6476 const gdb_byte *begin_info_ptr, *info_ptr;
6477 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6478 int i,num_extra_attrs;
6479 struct dwarf2_section_info *dwo_abbrev_section;
6480 struct die_info *comp_unit_die;
6481
6482 /* At most one of these may be provided. */
6483 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6484
6485 /* These attributes aren't processed until later:
6486 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6487 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6488 referenced later. However, these attributes are found in the stub
6489 which we won't have later. In order to not impose this complication
6490 on the rest of the code, we read them here and copy them to the
6491 DWO CU/TU die. */
6492
6493 stmt_list = NULL;
6494 low_pc = NULL;
6495 high_pc = NULL;
6496 ranges = NULL;
6497 comp_dir = NULL;
6498
6499 if (stub_comp_unit_die != NULL)
6500 {
6501 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6502 DWO file. */
6503 if (! this_cu->is_debug_types)
6504 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6505 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6506 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6507 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6508 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6509
6510 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6511
6512 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6513 here (if needed). We need the value before we can process
6514 DW_AT_ranges. */
6515 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6516 }
6517 else if (stub_comp_dir != NULL)
6518 {
6519 /* Reconstruct the comp_dir attribute to simplify the code below. */
6520 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6521 comp_dir->name = DW_AT_comp_dir;
6522 comp_dir->form = DW_FORM_string;
6523 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6524 DW_STRING (comp_dir) = stub_comp_dir;
6525 }
6526
6527 /* Set up for reading the DWO CU/TU. */
6528 cu->dwo_unit = dwo_unit;
6529 dwarf2_section_info *section = dwo_unit->section;
6530 section->read (objfile);
6531 abfd = section->get_bfd_owner ();
6532 begin_info_ptr = info_ptr = (section->buffer
6533 + to_underlying (dwo_unit->sect_off));
6534 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6535
6536 if (this_cu->is_debug_types)
6537 {
6538 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6539
6540 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6541 &cu->header, section,
6542 dwo_abbrev_section,
6543 info_ptr, rcuh_kind::TYPE);
6544 /* This is not an assert because it can be caused by bad debug info. */
6545 if (sig_type->signature != cu->header.signature)
6546 {
6547 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6548 " TU at offset %s [in module %s]"),
6549 hex_string (sig_type->signature),
6550 hex_string (cu->header.signature),
6551 sect_offset_str (dwo_unit->sect_off),
6552 bfd_get_filename (abfd));
6553 }
6554 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6555 /* For DWOs coming from DWP files, we don't know the CU length
6556 nor the type's offset in the TU until now. */
6557 dwo_unit->length = cu->header.get_length ();
6558 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6559
6560 /* Establish the type offset that can be used to lookup the type.
6561 For DWO files, we don't know it until now. */
6562 sig_type->type_offset_in_section
6563 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6564 }
6565 else
6566 {
6567 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6568 &cu->header, section,
6569 dwo_abbrev_section,
6570 info_ptr, rcuh_kind::COMPILE);
6571 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6572 /* For DWOs coming from DWP files, we don't know the CU length
6573 until now. */
6574 dwo_unit->length = cu->header.get_length ();
6575 }
6576
6577 *result_dwo_abbrev_table
6578 = abbrev_table::read (objfile, dwo_abbrev_section,
6579 cu->header.abbrev_sect_off);
6580 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6581 result_dwo_abbrev_table->get ());
6582
6583 /* Read in the die, but leave space to copy over the attributes
6584 from the stub. This has the benefit of simplifying the rest of
6585 the code - all the work to maintain the illusion of a single
6586 DW_TAG_{compile,type}_unit DIE is done here. */
6587 num_extra_attrs = ((stmt_list != NULL)
6588 + (low_pc != NULL)
6589 + (high_pc != NULL)
6590 + (ranges != NULL)
6591 + (comp_dir != NULL));
6592 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6593 num_extra_attrs);
6594
6595 /* Copy over the attributes from the stub to the DIE we just read in. */
6596 comp_unit_die = *result_comp_unit_die;
6597 i = comp_unit_die->num_attrs;
6598 if (stmt_list != NULL)
6599 comp_unit_die->attrs[i++] = *stmt_list;
6600 if (low_pc != NULL)
6601 comp_unit_die->attrs[i++] = *low_pc;
6602 if (high_pc != NULL)
6603 comp_unit_die->attrs[i++] = *high_pc;
6604 if (ranges != NULL)
6605 comp_unit_die->attrs[i++] = *ranges;
6606 if (comp_dir != NULL)
6607 comp_unit_die->attrs[i++] = *comp_dir;
6608 comp_unit_die->num_attrs += num_extra_attrs;
6609
6610 if (dwarf_die_debug)
6611 {
6612 fprintf_unfiltered (gdb_stdlog,
6613 "Read die from %s@0x%x of %s:\n",
6614 section->get_name (),
6615 (unsigned) (begin_info_ptr - section->buffer),
6616 bfd_get_filename (abfd));
6617 dump_die (comp_unit_die, dwarf_die_debug);
6618 }
6619
6620 /* Skip dummy compilation units. */
6621 if (info_ptr >= begin_info_ptr + dwo_unit->length
6622 || peek_abbrev_code (abfd, info_ptr) == 0)
6623 return 0;
6624
6625 *result_info_ptr = info_ptr;
6626 return 1;
6627 }
6628
6629 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6630 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6631 signature is part of the header. */
6632 static gdb::optional<ULONGEST>
6633 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6634 {
6635 if (cu->header.version >= 5)
6636 return cu->header.signature;
6637 struct attribute *attr;
6638 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6639 if (attr == nullptr)
6640 return gdb::optional<ULONGEST> ();
6641 return DW_UNSND (attr);
6642 }
6643
6644 /* Subroutine of cutu_reader to simplify it.
6645 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6646 Returns NULL if the specified DWO unit cannot be found. */
6647
6648 static struct dwo_unit *
6649 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6650 struct die_info *comp_unit_die,
6651 const char *dwo_name)
6652 {
6653 struct dwarf2_cu *cu = this_cu->cu;
6654 struct dwo_unit *dwo_unit;
6655 const char *comp_dir;
6656
6657 gdb_assert (cu != NULL);
6658
6659 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6660 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6661 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6662
6663 if (this_cu->is_debug_types)
6664 {
6665 struct signatured_type *sig_type;
6666
6667 /* Since this_cu is the first member of struct signatured_type,
6668 we can go from a pointer to one to a pointer to the other. */
6669 sig_type = (struct signatured_type *) this_cu;
6670 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6671 }
6672 else
6673 {
6674 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6675 if (!signature.has_value ())
6676 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6677 " [in module %s]"),
6678 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6679 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6680 *signature);
6681 }
6682
6683 return dwo_unit;
6684 }
6685
6686 /* Subroutine of cutu_reader to simplify it.
6687 See it for a description of the parameters.
6688 Read a TU directly from a DWO file, bypassing the stub. */
6689
6690 void
6691 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6692 int use_existing_cu)
6693 {
6694 struct signatured_type *sig_type;
6695
6696 /* Verify we can do the following downcast, and that we have the
6697 data we need. */
6698 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6699 sig_type = (struct signatured_type *) this_cu;
6700 gdb_assert (sig_type->dwo_unit != NULL);
6701
6702 if (use_existing_cu && this_cu->cu != NULL)
6703 {
6704 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6705 /* There's no need to do the rereading_dwo_cu handling that
6706 cutu_reader does since we don't read the stub. */
6707 }
6708 else
6709 {
6710 /* If !use_existing_cu, this_cu->cu must be NULL. */
6711 gdb_assert (this_cu->cu == NULL);
6712 m_new_cu.reset (new dwarf2_cu (this_cu));
6713 }
6714
6715 /* A future optimization, if needed, would be to use an existing
6716 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6717 could share abbrev tables. */
6718
6719 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6720 NULL /* stub_comp_unit_die */,
6721 sig_type->dwo_unit->dwo_file->comp_dir,
6722 this, &info_ptr,
6723 &comp_unit_die,
6724 &m_dwo_abbrev_table) == 0)
6725 {
6726 /* Dummy die. */
6727 dummy_p = true;
6728 }
6729 }
6730
6731 /* Initialize a CU (or TU) and read its DIEs.
6732 If the CU defers to a DWO file, read the DWO file as well.
6733
6734 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6735 Otherwise the table specified in the comp unit header is read in and used.
6736 This is an optimization for when we already have the abbrev table.
6737
6738 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6739 Otherwise, a new CU is allocated with xmalloc. */
6740
6741 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6742 struct abbrev_table *abbrev_table,
6743 int use_existing_cu,
6744 bool skip_partial)
6745 : die_reader_specs {},
6746 m_this_cu (this_cu)
6747 {
6748 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6749 struct objfile *objfile = dwarf2_per_objfile->objfile;
6750 struct dwarf2_section_info *section = this_cu->section;
6751 bfd *abfd = section->get_bfd_owner ();
6752 struct dwarf2_cu *cu;
6753 const gdb_byte *begin_info_ptr;
6754 struct signatured_type *sig_type = NULL;
6755 struct dwarf2_section_info *abbrev_section;
6756 /* Non-zero if CU currently points to a DWO file and we need to
6757 reread it. When this happens we need to reread the skeleton die
6758 before we can reread the DWO file (this only applies to CUs, not TUs). */
6759 int rereading_dwo_cu = 0;
6760
6761 if (dwarf_die_debug)
6762 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6763 this_cu->is_debug_types ? "type" : "comp",
6764 sect_offset_str (this_cu->sect_off));
6765
6766 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6767 file (instead of going through the stub), short-circuit all of this. */
6768 if (this_cu->reading_dwo_directly)
6769 {
6770 /* Narrow down the scope of possibilities to have to understand. */
6771 gdb_assert (this_cu->is_debug_types);
6772 gdb_assert (abbrev_table == NULL);
6773 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6774 return;
6775 }
6776
6777 /* This is cheap if the section is already read in. */
6778 section->read (objfile);
6779
6780 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6781
6782 abbrev_section = get_abbrev_section_for_cu (this_cu);
6783
6784 if (use_existing_cu && this_cu->cu != NULL)
6785 {
6786 cu = this_cu->cu;
6787 /* If this CU is from a DWO file we need to start over, we need to
6788 refetch the attributes from the skeleton CU.
6789 This could be optimized by retrieving those attributes from when we
6790 were here the first time: the previous comp_unit_die was stored in
6791 comp_unit_obstack. But there's no data yet that we need this
6792 optimization. */
6793 if (cu->dwo_unit != NULL)
6794 rereading_dwo_cu = 1;
6795 }
6796 else
6797 {
6798 /* If !use_existing_cu, this_cu->cu must be NULL. */
6799 gdb_assert (this_cu->cu == NULL);
6800 m_new_cu.reset (new dwarf2_cu (this_cu));
6801 cu = m_new_cu.get ();
6802 }
6803
6804 /* Get the header. */
6805 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6806 {
6807 /* We already have the header, there's no need to read it in again. */
6808 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6809 }
6810 else
6811 {
6812 if (this_cu->is_debug_types)
6813 {
6814 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6815 &cu->header, section,
6816 abbrev_section, info_ptr,
6817 rcuh_kind::TYPE);
6818
6819 /* Since per_cu is the first member of struct signatured_type,
6820 we can go from a pointer to one to a pointer to the other. */
6821 sig_type = (struct signatured_type *) this_cu;
6822 gdb_assert (sig_type->signature == cu->header.signature);
6823 gdb_assert (sig_type->type_offset_in_tu
6824 == cu->header.type_cu_offset_in_tu);
6825 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6826
6827 /* LENGTH has not been set yet for type units if we're
6828 using .gdb_index. */
6829 this_cu->length = cu->header.get_length ();
6830
6831 /* Establish the type offset that can be used to lookup the type. */
6832 sig_type->type_offset_in_section =
6833 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6834
6835 this_cu->dwarf_version = cu->header.version;
6836 }
6837 else
6838 {
6839 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6840 &cu->header, section,
6841 abbrev_section,
6842 info_ptr,
6843 rcuh_kind::COMPILE);
6844
6845 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6846 gdb_assert (this_cu->length == cu->header.get_length ());
6847 this_cu->dwarf_version = cu->header.version;
6848 }
6849 }
6850
6851 /* Skip dummy compilation units. */
6852 if (info_ptr >= begin_info_ptr + this_cu->length
6853 || peek_abbrev_code (abfd, info_ptr) == 0)
6854 {
6855 dummy_p = true;
6856 return;
6857 }
6858
6859 /* If we don't have them yet, read the abbrevs for this compilation unit.
6860 And if we need to read them now, make sure they're freed when we're
6861 done. */
6862 if (abbrev_table != NULL)
6863 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6864 else
6865 {
6866 m_abbrev_table_holder
6867 = abbrev_table::read (objfile, abbrev_section,
6868 cu->header.abbrev_sect_off);
6869 abbrev_table = m_abbrev_table_holder.get ();
6870 }
6871
6872 /* Read the top level CU/TU die. */
6873 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6874 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6875
6876 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6877 {
6878 dummy_p = true;
6879 return;
6880 }
6881
6882 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6883 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6884 table from the DWO file and pass the ownership over to us. It will be
6885 referenced from READER, so we must make sure to free it after we're done
6886 with READER.
6887
6888 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6889 DWO CU, that this test will fail (the attribute will not be present). */
6890 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6891 if (dwo_name != nullptr)
6892 {
6893 struct dwo_unit *dwo_unit;
6894 struct die_info *dwo_comp_unit_die;
6895
6896 if (comp_unit_die->has_children)
6897 {
6898 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6899 " has children (offset %s) [in module %s]"),
6900 sect_offset_str (this_cu->sect_off),
6901 bfd_get_filename (abfd));
6902 }
6903 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6904 if (dwo_unit != NULL)
6905 {
6906 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6907 comp_unit_die, NULL,
6908 this, &info_ptr,
6909 &dwo_comp_unit_die,
6910 &m_dwo_abbrev_table) == 0)
6911 {
6912 /* Dummy die. */
6913 dummy_p = true;
6914 return;
6915 }
6916 comp_unit_die = dwo_comp_unit_die;
6917 }
6918 else
6919 {
6920 /* Yikes, we couldn't find the rest of the DIE, we only have
6921 the stub. A complaint has already been logged. There's
6922 not much more we can do except pass on the stub DIE to
6923 die_reader_func. We don't want to throw an error on bad
6924 debug info. */
6925 }
6926 }
6927 }
6928
6929 void
6930 cutu_reader::keep ()
6931 {
6932 /* Done, clean up. */
6933 gdb_assert (!dummy_p);
6934 if (m_new_cu != NULL)
6935 {
6936 struct dwarf2_per_objfile *dwarf2_per_objfile
6937 = m_this_cu->dwarf2_per_objfile;
6938 /* Link this CU into read_in_chain. */
6939 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6940 dwarf2_per_objfile->read_in_chain = m_this_cu;
6941 /* The chain owns it now. */
6942 m_new_cu.release ();
6943 }
6944 }
6945
6946 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6947 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6948 assumed to have already done the lookup to find the DWO file).
6949
6950 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6951 THIS_CU->is_debug_types, but nothing else.
6952
6953 We fill in THIS_CU->length.
6954
6955 THIS_CU->cu is always freed when done.
6956 This is done in order to not leave THIS_CU->cu in a state where we have
6957 to care whether it refers to the "main" CU or the DWO CU.
6958
6959 When parent_cu is passed, it is used to provide a default value for
6960 str_offsets_base and addr_base from the parent. */
6961
6962 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6963 struct dwarf2_cu *parent_cu,
6964 struct dwo_file *dwo_file)
6965 : die_reader_specs {},
6966 m_this_cu (this_cu)
6967 {
6968 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6969 struct objfile *objfile = dwarf2_per_objfile->objfile;
6970 struct dwarf2_section_info *section = this_cu->section;
6971 bfd *abfd = section->get_bfd_owner ();
6972 struct dwarf2_section_info *abbrev_section;
6973 const gdb_byte *begin_info_ptr, *info_ptr;
6974
6975 if (dwarf_die_debug)
6976 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6977 this_cu->is_debug_types ? "type" : "comp",
6978 sect_offset_str (this_cu->sect_off));
6979
6980 gdb_assert (this_cu->cu == NULL);
6981
6982 abbrev_section = (dwo_file != NULL
6983 ? &dwo_file->sections.abbrev
6984 : get_abbrev_section_for_cu (this_cu));
6985
6986 /* This is cheap if the section is already read in. */
6987 section->read (objfile);
6988
6989 m_new_cu.reset (new dwarf2_cu (this_cu));
6990
6991 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6992 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6993 &m_new_cu->header, section,
6994 abbrev_section, info_ptr,
6995 (this_cu->is_debug_types
6996 ? rcuh_kind::TYPE
6997 : rcuh_kind::COMPILE));
6998
6999 if (parent_cu != nullptr)
7000 {
7001 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7002 m_new_cu->addr_base = parent_cu->addr_base;
7003 }
7004 this_cu->length = m_new_cu->header.get_length ();
7005
7006 /* Skip dummy compilation units. */
7007 if (info_ptr >= begin_info_ptr + this_cu->length
7008 || peek_abbrev_code (abfd, info_ptr) == 0)
7009 {
7010 dummy_p = true;
7011 return;
7012 }
7013
7014 m_abbrev_table_holder
7015 = abbrev_table::read (objfile, abbrev_section,
7016 m_new_cu->header.abbrev_sect_off);
7017
7018 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7019 m_abbrev_table_holder.get ());
7020 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7021 }
7022
7023 \f
7024 /* Type Unit Groups.
7025
7026 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7027 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7028 so that all types coming from the same compilation (.o file) are grouped
7029 together. A future step could be to put the types in the same symtab as
7030 the CU the types ultimately came from. */
7031
7032 static hashval_t
7033 hash_type_unit_group (const void *item)
7034 {
7035 const struct type_unit_group *tu_group
7036 = (const struct type_unit_group *) item;
7037
7038 return hash_stmt_list_entry (&tu_group->hash);
7039 }
7040
7041 static int
7042 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7043 {
7044 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7045 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7046
7047 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7048 }
7049
7050 /* Allocate a hash table for type unit groups. */
7051
7052 static htab_up
7053 allocate_type_unit_groups_table ()
7054 {
7055 return htab_up (htab_create_alloc (3,
7056 hash_type_unit_group,
7057 eq_type_unit_group,
7058 NULL, xcalloc, xfree));
7059 }
7060
7061 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7062 partial symtabs. We combine several TUs per psymtab to not let the size
7063 of any one psymtab grow too big. */
7064 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7065 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7066
7067 /* Helper routine for get_type_unit_group.
7068 Create the type_unit_group object used to hold one or more TUs. */
7069
7070 static struct type_unit_group *
7071 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7072 {
7073 struct dwarf2_per_objfile *dwarf2_per_objfile
7074 = cu->per_cu->dwarf2_per_objfile;
7075 struct objfile *objfile = dwarf2_per_objfile->objfile;
7076 struct dwarf2_per_cu_data *per_cu;
7077 struct type_unit_group *tu_group;
7078
7079 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7080 struct type_unit_group);
7081 per_cu = &tu_group->per_cu;
7082 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7083
7084 if (dwarf2_per_objfile->using_index)
7085 {
7086 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7087 struct dwarf2_per_cu_quick_data);
7088 }
7089 else
7090 {
7091 unsigned int line_offset = to_underlying (line_offset_struct);
7092 dwarf2_psymtab *pst;
7093 std::string name;
7094
7095 /* Give the symtab a useful name for debug purposes. */
7096 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7097 name = string_printf ("<type_units_%d>",
7098 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7099 else
7100 name = string_printf ("<type_units_at_0x%x>", line_offset);
7101
7102 pst = create_partial_symtab (per_cu, name.c_str ());
7103 pst->anonymous = true;
7104 }
7105
7106 tu_group->hash.dwo_unit = cu->dwo_unit;
7107 tu_group->hash.line_sect_off = line_offset_struct;
7108
7109 return tu_group;
7110 }
7111
7112 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7113 STMT_LIST is a DW_AT_stmt_list attribute. */
7114
7115 static struct type_unit_group *
7116 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7117 {
7118 struct dwarf2_per_objfile *dwarf2_per_objfile
7119 = cu->per_cu->dwarf2_per_objfile;
7120 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7121 struct type_unit_group *tu_group;
7122 void **slot;
7123 unsigned int line_offset;
7124 struct type_unit_group type_unit_group_for_lookup;
7125
7126 if (dwarf2_per_objfile->type_unit_groups == NULL)
7127 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7128
7129 /* Do we need to create a new group, or can we use an existing one? */
7130
7131 if (stmt_list)
7132 {
7133 line_offset = DW_UNSND (stmt_list);
7134 ++tu_stats->nr_symtab_sharers;
7135 }
7136 else
7137 {
7138 /* Ugh, no stmt_list. Rare, but we have to handle it.
7139 We can do various things here like create one group per TU or
7140 spread them over multiple groups to split up the expansion work.
7141 To avoid worst case scenarios (too many groups or too large groups)
7142 we, umm, group them in bunches. */
7143 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7144 | (tu_stats->nr_stmt_less_type_units
7145 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7146 ++tu_stats->nr_stmt_less_type_units;
7147 }
7148
7149 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7150 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7151 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7152 &type_unit_group_for_lookup, INSERT);
7153 if (*slot != NULL)
7154 {
7155 tu_group = (struct type_unit_group *) *slot;
7156 gdb_assert (tu_group != NULL);
7157 }
7158 else
7159 {
7160 sect_offset line_offset_struct = (sect_offset) line_offset;
7161 tu_group = create_type_unit_group (cu, line_offset_struct);
7162 *slot = tu_group;
7163 ++tu_stats->nr_symtabs;
7164 }
7165
7166 return tu_group;
7167 }
7168 \f
7169 /* Partial symbol tables. */
7170
7171 /* Create a psymtab named NAME and assign it to PER_CU.
7172
7173 The caller must fill in the following details:
7174 dirname, textlow, texthigh. */
7175
7176 static dwarf2_psymtab *
7177 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7178 {
7179 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7180 dwarf2_psymtab *pst;
7181
7182 pst = new dwarf2_psymtab (name, objfile, 0);
7183
7184 pst->psymtabs_addrmap_supported = true;
7185
7186 /* This is the glue that links PST into GDB's symbol API. */
7187 pst->per_cu_data = per_cu;
7188 per_cu->v.psymtab = pst;
7189
7190 return pst;
7191 }
7192
7193 /* DIE reader function for process_psymtab_comp_unit. */
7194
7195 static void
7196 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7197 const gdb_byte *info_ptr,
7198 struct die_info *comp_unit_die,
7199 enum language pretend_language)
7200 {
7201 struct dwarf2_cu *cu = reader->cu;
7202 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7203 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7204 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7205 CORE_ADDR baseaddr;
7206 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7207 dwarf2_psymtab *pst;
7208 enum pc_bounds_kind cu_bounds_kind;
7209 const char *filename;
7210
7211 gdb_assert (! per_cu->is_debug_types);
7212
7213 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7214
7215 /* Allocate a new partial symbol table structure. */
7216 gdb::unique_xmalloc_ptr<char> debug_filename;
7217 static const char artificial[] = "<artificial>";
7218 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7219 if (filename == NULL)
7220 filename = "";
7221 else if (strcmp (filename, artificial) == 0)
7222 {
7223 debug_filename.reset (concat (artificial, "@",
7224 sect_offset_str (per_cu->sect_off),
7225 (char *) NULL));
7226 filename = debug_filename.get ();
7227 }
7228
7229 pst = create_partial_symtab (per_cu, filename);
7230
7231 /* This must be done before calling dwarf2_build_include_psymtabs. */
7232 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7233
7234 baseaddr = objfile->text_section_offset ();
7235
7236 dwarf2_find_base_address (comp_unit_die, cu);
7237
7238 /* Possibly set the default values of LOWPC and HIGHPC from
7239 `DW_AT_ranges'. */
7240 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7241 &best_highpc, cu, pst);
7242 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7243 {
7244 CORE_ADDR low
7245 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7246 - baseaddr);
7247 CORE_ADDR high
7248 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7249 - baseaddr - 1);
7250 /* Store the contiguous range if it is not empty; it can be
7251 empty for CUs with no code. */
7252 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7253 low, high, pst);
7254 }
7255
7256 /* Check if comp unit has_children.
7257 If so, read the rest of the partial symbols from this comp unit.
7258 If not, there's no more debug_info for this comp unit. */
7259 if (comp_unit_die->has_children)
7260 {
7261 struct partial_die_info *first_die;
7262 CORE_ADDR lowpc, highpc;
7263
7264 lowpc = ((CORE_ADDR) -1);
7265 highpc = ((CORE_ADDR) 0);
7266
7267 first_die = load_partial_dies (reader, info_ptr, 1);
7268
7269 scan_partial_symbols (first_die, &lowpc, &highpc,
7270 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7271
7272 /* If we didn't find a lowpc, set it to highpc to avoid
7273 complaints from `maint check'. */
7274 if (lowpc == ((CORE_ADDR) -1))
7275 lowpc = highpc;
7276
7277 /* If the compilation unit didn't have an explicit address range,
7278 then use the information extracted from its child dies. */
7279 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7280 {
7281 best_lowpc = lowpc;
7282 best_highpc = highpc;
7283 }
7284 }
7285 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7286 best_lowpc + baseaddr)
7287 - baseaddr);
7288 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7289 best_highpc + baseaddr)
7290 - baseaddr);
7291
7292 end_psymtab_common (objfile, pst);
7293
7294 if (!cu->per_cu->imported_symtabs_empty ())
7295 {
7296 int i;
7297 int len = cu->per_cu->imported_symtabs_size ();
7298
7299 /* Fill in 'dependencies' here; we fill in 'users' in a
7300 post-pass. */
7301 pst->number_of_dependencies = len;
7302 pst->dependencies
7303 = objfile->partial_symtabs->allocate_dependencies (len);
7304 for (i = 0; i < len; ++i)
7305 {
7306 pst->dependencies[i]
7307 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7308 }
7309
7310 cu->per_cu->imported_symtabs_free ();
7311 }
7312
7313 /* Get the list of files included in the current compilation unit,
7314 and build a psymtab for each of them. */
7315 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7316
7317 if (dwarf_read_debug)
7318 fprintf_unfiltered (gdb_stdlog,
7319 "Psymtab for %s unit @%s: %s - %s"
7320 ", %d global, %d static syms\n",
7321 per_cu->is_debug_types ? "type" : "comp",
7322 sect_offset_str (per_cu->sect_off),
7323 paddress (gdbarch, pst->text_low (objfile)),
7324 paddress (gdbarch, pst->text_high (objfile)),
7325 pst->n_global_syms, pst->n_static_syms);
7326 }
7327
7328 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7329 Process compilation unit THIS_CU for a psymtab. */
7330
7331 static void
7332 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7333 bool want_partial_unit,
7334 enum language pretend_language)
7335 {
7336 /* If this compilation unit was already read in, free the
7337 cached copy in order to read it in again. This is
7338 necessary because we skipped some symbols when we first
7339 read in the compilation unit (see load_partial_dies).
7340 This problem could be avoided, but the benefit is unclear. */
7341 if (this_cu->cu != NULL)
7342 free_one_cached_comp_unit (this_cu);
7343
7344 cutu_reader reader (this_cu, NULL, 0, false);
7345
7346 switch (reader.comp_unit_die->tag)
7347 {
7348 case DW_TAG_compile_unit:
7349 this_cu->unit_type = DW_UT_compile;
7350 break;
7351 case DW_TAG_partial_unit:
7352 this_cu->unit_type = DW_UT_partial;
7353 break;
7354 default:
7355 abort ();
7356 }
7357
7358 if (reader.dummy_p)
7359 {
7360 /* Nothing. */
7361 }
7362 else if (this_cu->is_debug_types)
7363 build_type_psymtabs_reader (&reader, reader.info_ptr,
7364 reader.comp_unit_die);
7365 else if (want_partial_unit
7366 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7367 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7368 reader.comp_unit_die,
7369 pretend_language);
7370
7371 this_cu->lang = this_cu->cu->language;
7372
7373 /* Age out any secondary CUs. */
7374 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7375 }
7376
7377 /* Reader function for build_type_psymtabs. */
7378
7379 static void
7380 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7381 const gdb_byte *info_ptr,
7382 struct die_info *type_unit_die)
7383 {
7384 struct dwarf2_per_objfile *dwarf2_per_objfile
7385 = reader->cu->per_cu->dwarf2_per_objfile;
7386 struct objfile *objfile = dwarf2_per_objfile->objfile;
7387 struct dwarf2_cu *cu = reader->cu;
7388 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7389 struct signatured_type *sig_type;
7390 struct type_unit_group *tu_group;
7391 struct attribute *attr;
7392 struct partial_die_info *first_die;
7393 CORE_ADDR lowpc, highpc;
7394 dwarf2_psymtab *pst;
7395
7396 gdb_assert (per_cu->is_debug_types);
7397 sig_type = (struct signatured_type *) per_cu;
7398
7399 if (! type_unit_die->has_children)
7400 return;
7401
7402 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7403 tu_group = get_type_unit_group (cu, attr);
7404
7405 if (tu_group->tus == nullptr)
7406 tu_group->tus = new std::vector<signatured_type *>;
7407 tu_group->tus->push_back (sig_type);
7408
7409 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7410 pst = create_partial_symtab (per_cu, "");
7411 pst->anonymous = true;
7412
7413 first_die = load_partial_dies (reader, info_ptr, 1);
7414
7415 lowpc = (CORE_ADDR) -1;
7416 highpc = (CORE_ADDR) 0;
7417 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7418
7419 end_psymtab_common (objfile, pst);
7420 }
7421
7422 /* Struct used to sort TUs by their abbreviation table offset. */
7423
7424 struct tu_abbrev_offset
7425 {
7426 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7427 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7428 {}
7429
7430 signatured_type *sig_type;
7431 sect_offset abbrev_offset;
7432 };
7433
7434 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7435
7436 static bool
7437 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7438 const struct tu_abbrev_offset &b)
7439 {
7440 return a.abbrev_offset < b.abbrev_offset;
7441 }
7442
7443 /* Efficiently read all the type units.
7444 This does the bulk of the work for build_type_psymtabs.
7445
7446 The efficiency is because we sort TUs by the abbrev table they use and
7447 only read each abbrev table once. In one program there are 200K TUs
7448 sharing 8K abbrev tables.
7449
7450 The main purpose of this function is to support building the
7451 dwarf2_per_objfile->type_unit_groups table.
7452 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7453 can collapse the search space by grouping them by stmt_list.
7454 The savings can be significant, in the same program from above the 200K TUs
7455 share 8K stmt_list tables.
7456
7457 FUNC is expected to call get_type_unit_group, which will create the
7458 struct type_unit_group if necessary and add it to
7459 dwarf2_per_objfile->type_unit_groups. */
7460
7461 static void
7462 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7463 {
7464 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7465 abbrev_table_up abbrev_table;
7466 sect_offset abbrev_offset;
7467
7468 /* It's up to the caller to not call us multiple times. */
7469 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7470
7471 if (dwarf2_per_objfile->all_type_units.empty ())
7472 return;
7473
7474 /* TUs typically share abbrev tables, and there can be way more TUs than
7475 abbrev tables. Sort by abbrev table to reduce the number of times we
7476 read each abbrev table in.
7477 Alternatives are to punt or to maintain a cache of abbrev tables.
7478 This is simpler and efficient enough for now.
7479
7480 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7481 symtab to use). Typically TUs with the same abbrev offset have the same
7482 stmt_list value too so in practice this should work well.
7483
7484 The basic algorithm here is:
7485
7486 sort TUs by abbrev table
7487 for each TU with same abbrev table:
7488 read abbrev table if first user
7489 read TU top level DIE
7490 [IWBN if DWO skeletons had DW_AT_stmt_list]
7491 call FUNC */
7492
7493 if (dwarf_read_debug)
7494 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7495
7496 /* Sort in a separate table to maintain the order of all_type_units
7497 for .gdb_index: TU indices directly index all_type_units. */
7498 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7499 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7500
7501 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7502 sorted_by_abbrev.emplace_back
7503 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7504 sig_type->per_cu.section,
7505 sig_type->per_cu.sect_off));
7506
7507 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7508 sort_tu_by_abbrev_offset);
7509
7510 abbrev_offset = (sect_offset) ~(unsigned) 0;
7511
7512 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7513 {
7514 /* Switch to the next abbrev table if necessary. */
7515 if (abbrev_table == NULL
7516 || tu.abbrev_offset != abbrev_offset)
7517 {
7518 abbrev_offset = tu.abbrev_offset;
7519 abbrev_table =
7520 abbrev_table::read (dwarf2_per_objfile->objfile,
7521 &dwarf2_per_objfile->abbrev,
7522 abbrev_offset);
7523 ++tu_stats->nr_uniq_abbrev_tables;
7524 }
7525
7526 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7527 0, false);
7528 if (!reader.dummy_p)
7529 build_type_psymtabs_reader (&reader, reader.info_ptr,
7530 reader.comp_unit_die);
7531 }
7532 }
7533
7534 /* Print collected type unit statistics. */
7535
7536 static void
7537 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7538 {
7539 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7540
7541 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7542 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7543 dwarf2_per_objfile->all_type_units.size ());
7544 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7545 tu_stats->nr_uniq_abbrev_tables);
7546 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7547 tu_stats->nr_symtabs);
7548 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7549 tu_stats->nr_symtab_sharers);
7550 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7551 tu_stats->nr_stmt_less_type_units);
7552 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7553 tu_stats->nr_all_type_units_reallocs);
7554 }
7555
7556 /* Traversal function for build_type_psymtabs. */
7557
7558 static int
7559 build_type_psymtab_dependencies (void **slot, void *info)
7560 {
7561 struct dwarf2_per_objfile *dwarf2_per_objfile
7562 = (struct dwarf2_per_objfile *) info;
7563 struct objfile *objfile = dwarf2_per_objfile->objfile;
7564 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7565 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7566 dwarf2_psymtab *pst = per_cu->v.psymtab;
7567 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7568 int i;
7569
7570 gdb_assert (len > 0);
7571 gdb_assert (per_cu->type_unit_group_p ());
7572
7573 pst->number_of_dependencies = len;
7574 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7575 for (i = 0; i < len; ++i)
7576 {
7577 struct signatured_type *iter = tu_group->tus->at (i);
7578 gdb_assert (iter->per_cu.is_debug_types);
7579 pst->dependencies[i] = iter->per_cu.v.psymtab;
7580 iter->type_unit_group = tu_group;
7581 }
7582
7583 delete tu_group->tus;
7584 tu_group->tus = nullptr;
7585
7586 return 1;
7587 }
7588
7589 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7590 Build partial symbol tables for the .debug_types comp-units. */
7591
7592 static void
7593 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7594 {
7595 if (! create_all_type_units (dwarf2_per_objfile))
7596 return;
7597
7598 build_type_psymtabs_1 (dwarf2_per_objfile);
7599 }
7600
7601 /* Traversal function for process_skeletonless_type_unit.
7602 Read a TU in a DWO file and build partial symbols for it. */
7603
7604 static int
7605 process_skeletonless_type_unit (void **slot, void *info)
7606 {
7607 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7608 struct dwarf2_per_objfile *dwarf2_per_objfile
7609 = (struct dwarf2_per_objfile *) info;
7610 struct signatured_type find_entry, *entry;
7611
7612 /* If this TU doesn't exist in the global table, add it and read it in. */
7613
7614 if (dwarf2_per_objfile->signatured_types == NULL)
7615 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7616
7617 find_entry.signature = dwo_unit->signature;
7618 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7619 &find_entry, INSERT);
7620 /* If we've already seen this type there's nothing to do. What's happening
7621 is we're doing our own version of comdat-folding here. */
7622 if (*slot != NULL)
7623 return 1;
7624
7625 /* This does the job that create_all_type_units would have done for
7626 this TU. */
7627 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7628 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7629 *slot = entry;
7630
7631 /* This does the job that build_type_psymtabs_1 would have done. */
7632 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7633 if (!reader.dummy_p)
7634 build_type_psymtabs_reader (&reader, reader.info_ptr,
7635 reader.comp_unit_die);
7636
7637 return 1;
7638 }
7639
7640 /* Traversal function for process_skeletonless_type_units. */
7641
7642 static int
7643 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7644 {
7645 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7646
7647 if (dwo_file->tus != NULL)
7648 htab_traverse_noresize (dwo_file->tus.get (),
7649 process_skeletonless_type_unit, info);
7650
7651 return 1;
7652 }
7653
7654 /* Scan all TUs of DWO files, verifying we've processed them.
7655 This is needed in case a TU was emitted without its skeleton.
7656 Note: This can't be done until we know what all the DWO files are. */
7657
7658 static void
7659 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7660 {
7661 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7662 if (get_dwp_file (dwarf2_per_objfile) == NULL
7663 && dwarf2_per_objfile->dwo_files != NULL)
7664 {
7665 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7666 process_dwo_file_for_skeletonless_type_units,
7667 dwarf2_per_objfile);
7668 }
7669 }
7670
7671 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7672
7673 static void
7674 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7675 {
7676 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7677 {
7678 dwarf2_psymtab *pst = per_cu->v.psymtab;
7679
7680 if (pst == NULL)
7681 continue;
7682
7683 for (int j = 0; j < pst->number_of_dependencies; ++j)
7684 {
7685 /* Set the 'user' field only if it is not already set. */
7686 if (pst->dependencies[j]->user == NULL)
7687 pst->dependencies[j]->user = pst;
7688 }
7689 }
7690 }
7691
7692 /* Build the partial symbol table by doing a quick pass through the
7693 .debug_info and .debug_abbrev sections. */
7694
7695 static void
7696 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7697 {
7698 struct objfile *objfile = dwarf2_per_objfile->objfile;
7699
7700 if (dwarf_read_debug)
7701 {
7702 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7703 objfile_name (objfile));
7704 }
7705
7706 scoped_restore restore_reading_psyms
7707 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7708 true);
7709
7710 dwarf2_per_objfile->info.read (objfile);
7711
7712 /* Any cached compilation units will be linked by the per-objfile
7713 read_in_chain. Make sure to free them when we're done. */
7714 free_cached_comp_units freer (dwarf2_per_objfile);
7715
7716 build_type_psymtabs (dwarf2_per_objfile);
7717
7718 create_all_comp_units (dwarf2_per_objfile);
7719
7720 /* Create a temporary address map on a temporary obstack. We later
7721 copy this to the final obstack. */
7722 auto_obstack temp_obstack;
7723
7724 scoped_restore save_psymtabs_addrmap
7725 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7726 addrmap_create_mutable (&temp_obstack));
7727
7728 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7729 process_psymtab_comp_unit (per_cu, false, language_minimal);
7730
7731 /* This has to wait until we read the CUs, we need the list of DWOs. */
7732 process_skeletonless_type_units (dwarf2_per_objfile);
7733
7734 /* Now that all TUs have been processed we can fill in the dependencies. */
7735 if (dwarf2_per_objfile->type_unit_groups != NULL)
7736 {
7737 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7738 build_type_psymtab_dependencies, dwarf2_per_objfile);
7739 }
7740
7741 if (dwarf_read_debug)
7742 print_tu_stats (dwarf2_per_objfile);
7743
7744 set_partial_user (dwarf2_per_objfile);
7745
7746 objfile->partial_symtabs->psymtabs_addrmap
7747 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7748 objfile->partial_symtabs->obstack ());
7749 /* At this point we want to keep the address map. */
7750 save_psymtabs_addrmap.release ();
7751
7752 if (dwarf_read_debug)
7753 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7754 objfile_name (objfile));
7755 }
7756
7757 /* Load the partial DIEs for a secondary CU into memory.
7758 This is also used when rereading a primary CU with load_all_dies. */
7759
7760 static void
7761 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7762 {
7763 cutu_reader reader (this_cu, NULL, 1, false);
7764
7765 if (!reader.dummy_p)
7766 {
7767 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7768 language_minimal);
7769
7770 /* Check if comp unit has_children.
7771 If so, read the rest of the partial symbols from this comp unit.
7772 If not, there's no more debug_info for this comp unit. */
7773 if (reader.comp_unit_die->has_children)
7774 load_partial_dies (&reader, reader.info_ptr, 0);
7775
7776 reader.keep ();
7777 }
7778 }
7779
7780 static void
7781 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7782 struct dwarf2_section_info *section,
7783 struct dwarf2_section_info *abbrev_section,
7784 unsigned int is_dwz)
7785 {
7786 const gdb_byte *info_ptr;
7787 struct objfile *objfile = dwarf2_per_objfile->objfile;
7788
7789 if (dwarf_read_debug)
7790 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7791 section->get_name (),
7792 section->get_file_name ());
7793
7794 section->read (objfile);
7795
7796 info_ptr = section->buffer;
7797
7798 while (info_ptr < section->buffer + section->size)
7799 {
7800 struct dwarf2_per_cu_data *this_cu;
7801
7802 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7803
7804 comp_unit_head cu_header;
7805 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7806 abbrev_section, info_ptr,
7807 rcuh_kind::COMPILE);
7808
7809 /* Save the compilation unit for later lookup. */
7810 if (cu_header.unit_type != DW_UT_type)
7811 {
7812 this_cu = XOBNEW (&objfile->objfile_obstack,
7813 struct dwarf2_per_cu_data);
7814 memset (this_cu, 0, sizeof (*this_cu));
7815 }
7816 else
7817 {
7818 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7819 struct signatured_type);
7820 memset (sig_type, 0, sizeof (*sig_type));
7821 sig_type->signature = cu_header.signature;
7822 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7823 this_cu = &sig_type->per_cu;
7824 }
7825 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7826 this_cu->sect_off = sect_off;
7827 this_cu->length = cu_header.length + cu_header.initial_length_size;
7828 this_cu->is_dwz = is_dwz;
7829 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7830 this_cu->section = section;
7831
7832 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7833
7834 info_ptr = info_ptr + this_cu->length;
7835 }
7836 }
7837
7838 /* Create a list of all compilation units in OBJFILE.
7839 This is only done for -readnow and building partial symtabs. */
7840
7841 static void
7842 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7843 {
7844 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7845 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7846 &dwarf2_per_objfile->abbrev, 0);
7847
7848 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7849 if (dwz != NULL)
7850 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7851 1);
7852 }
7853
7854 /* Process all loaded DIEs for compilation unit CU, starting at
7855 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7856 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7857 DW_AT_ranges). See the comments of add_partial_subprogram on how
7858 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7859
7860 static void
7861 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7862 CORE_ADDR *highpc, int set_addrmap,
7863 struct dwarf2_cu *cu)
7864 {
7865 struct partial_die_info *pdi;
7866
7867 /* Now, march along the PDI's, descending into ones which have
7868 interesting children but skipping the children of the other ones,
7869 until we reach the end of the compilation unit. */
7870
7871 pdi = first_die;
7872
7873 while (pdi != NULL)
7874 {
7875 pdi->fixup (cu);
7876
7877 /* Anonymous namespaces or modules have no name but have interesting
7878 children, so we need to look at them. Ditto for anonymous
7879 enums. */
7880
7881 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7882 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7883 || pdi->tag == DW_TAG_imported_unit
7884 || pdi->tag == DW_TAG_inlined_subroutine)
7885 {
7886 switch (pdi->tag)
7887 {
7888 case DW_TAG_subprogram:
7889 case DW_TAG_inlined_subroutine:
7890 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7891 break;
7892 case DW_TAG_constant:
7893 case DW_TAG_variable:
7894 case DW_TAG_typedef:
7895 case DW_TAG_union_type:
7896 if (!pdi->is_declaration)
7897 {
7898 add_partial_symbol (pdi, cu);
7899 }
7900 break;
7901 case DW_TAG_class_type:
7902 case DW_TAG_interface_type:
7903 case DW_TAG_structure_type:
7904 if (!pdi->is_declaration)
7905 {
7906 add_partial_symbol (pdi, cu);
7907 }
7908 if ((cu->language == language_rust
7909 || cu->language == language_cplus) && pdi->has_children)
7910 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7911 set_addrmap, cu);
7912 break;
7913 case DW_TAG_enumeration_type:
7914 if (!pdi->is_declaration)
7915 add_partial_enumeration (pdi, cu);
7916 break;
7917 case DW_TAG_base_type:
7918 case DW_TAG_subrange_type:
7919 /* File scope base type definitions are added to the partial
7920 symbol table. */
7921 add_partial_symbol (pdi, cu);
7922 break;
7923 case DW_TAG_namespace:
7924 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7925 break;
7926 case DW_TAG_module:
7927 if (!pdi->is_declaration)
7928 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7929 break;
7930 case DW_TAG_imported_unit:
7931 {
7932 struct dwarf2_per_cu_data *per_cu;
7933
7934 /* For now we don't handle imported units in type units. */
7935 if (cu->per_cu->is_debug_types)
7936 {
7937 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7938 " supported in type units [in module %s]"),
7939 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7940 }
7941
7942 per_cu = dwarf2_find_containing_comp_unit
7943 (pdi->d.sect_off, pdi->is_dwz,
7944 cu->per_cu->dwarf2_per_objfile);
7945
7946 /* Go read the partial unit, if needed. */
7947 if (per_cu->v.psymtab == NULL)
7948 process_psymtab_comp_unit (per_cu, true, cu->language);
7949
7950 cu->per_cu->imported_symtabs_push (per_cu);
7951 }
7952 break;
7953 case DW_TAG_imported_declaration:
7954 add_partial_symbol (pdi, cu);
7955 break;
7956 default:
7957 break;
7958 }
7959 }
7960
7961 /* If the die has a sibling, skip to the sibling. */
7962
7963 pdi = pdi->die_sibling;
7964 }
7965 }
7966
7967 /* Functions used to compute the fully scoped name of a partial DIE.
7968
7969 Normally, this is simple. For C++, the parent DIE's fully scoped
7970 name is concatenated with "::" and the partial DIE's name.
7971 Enumerators are an exception; they use the scope of their parent
7972 enumeration type, i.e. the name of the enumeration type is not
7973 prepended to the enumerator.
7974
7975 There are two complexities. One is DW_AT_specification; in this
7976 case "parent" means the parent of the target of the specification,
7977 instead of the direct parent of the DIE. The other is compilers
7978 which do not emit DW_TAG_namespace; in this case we try to guess
7979 the fully qualified name of structure types from their members'
7980 linkage names. This must be done using the DIE's children rather
7981 than the children of any DW_AT_specification target. We only need
7982 to do this for structures at the top level, i.e. if the target of
7983 any DW_AT_specification (if any; otherwise the DIE itself) does not
7984 have a parent. */
7985
7986 /* Compute the scope prefix associated with PDI's parent, in
7987 compilation unit CU. The result will be allocated on CU's
7988 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7989 field. NULL is returned if no prefix is necessary. */
7990 static const char *
7991 partial_die_parent_scope (struct partial_die_info *pdi,
7992 struct dwarf2_cu *cu)
7993 {
7994 const char *grandparent_scope;
7995 struct partial_die_info *parent, *real_pdi;
7996
7997 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7998 then this means the parent of the specification DIE. */
7999
8000 real_pdi = pdi;
8001 while (real_pdi->has_specification)
8002 {
8003 auto res = find_partial_die (real_pdi->spec_offset,
8004 real_pdi->spec_is_dwz, cu);
8005 real_pdi = res.pdi;
8006 cu = res.cu;
8007 }
8008
8009 parent = real_pdi->die_parent;
8010 if (parent == NULL)
8011 return NULL;
8012
8013 if (parent->scope_set)
8014 return parent->scope;
8015
8016 parent->fixup (cu);
8017
8018 grandparent_scope = partial_die_parent_scope (parent, cu);
8019
8020 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8021 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8022 Work around this problem here. */
8023 if (cu->language == language_cplus
8024 && parent->tag == DW_TAG_namespace
8025 && strcmp (parent->name, "::") == 0
8026 && grandparent_scope == NULL)
8027 {
8028 parent->scope = NULL;
8029 parent->scope_set = 1;
8030 return NULL;
8031 }
8032
8033 /* Nested subroutines in Fortran get a prefix. */
8034 if (pdi->tag == DW_TAG_enumerator)
8035 /* Enumerators should not get the name of the enumeration as a prefix. */
8036 parent->scope = grandparent_scope;
8037 else if (parent->tag == DW_TAG_namespace
8038 || parent->tag == DW_TAG_module
8039 || parent->tag == DW_TAG_structure_type
8040 || parent->tag == DW_TAG_class_type
8041 || parent->tag == DW_TAG_interface_type
8042 || parent->tag == DW_TAG_union_type
8043 || parent->tag == DW_TAG_enumeration_type
8044 || (cu->language == language_fortran
8045 && parent->tag == DW_TAG_subprogram
8046 && pdi->tag == DW_TAG_subprogram))
8047 {
8048 if (grandparent_scope == NULL)
8049 parent->scope = parent->name;
8050 else
8051 parent->scope = typename_concat (&cu->comp_unit_obstack,
8052 grandparent_scope,
8053 parent->name, 0, cu);
8054 }
8055 else
8056 {
8057 /* FIXME drow/2004-04-01: What should we be doing with
8058 function-local names? For partial symbols, we should probably be
8059 ignoring them. */
8060 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8061 dwarf_tag_name (parent->tag),
8062 sect_offset_str (pdi->sect_off));
8063 parent->scope = grandparent_scope;
8064 }
8065
8066 parent->scope_set = 1;
8067 return parent->scope;
8068 }
8069
8070 /* Return the fully scoped name associated with PDI, from compilation unit
8071 CU. The result will be allocated with malloc. */
8072
8073 static gdb::unique_xmalloc_ptr<char>
8074 partial_die_full_name (struct partial_die_info *pdi,
8075 struct dwarf2_cu *cu)
8076 {
8077 const char *parent_scope;
8078
8079 /* If this is a template instantiation, we can not work out the
8080 template arguments from partial DIEs. So, unfortunately, we have
8081 to go through the full DIEs. At least any work we do building
8082 types here will be reused if full symbols are loaded later. */
8083 if (pdi->has_template_arguments)
8084 {
8085 pdi->fixup (cu);
8086
8087 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8088 {
8089 struct die_info *die;
8090 struct attribute attr;
8091 struct dwarf2_cu *ref_cu = cu;
8092
8093 /* DW_FORM_ref_addr is using section offset. */
8094 attr.name = (enum dwarf_attribute) 0;
8095 attr.form = DW_FORM_ref_addr;
8096 attr.u.unsnd = to_underlying (pdi->sect_off);
8097 die = follow_die_ref (NULL, &attr, &ref_cu);
8098
8099 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8100 }
8101 }
8102
8103 parent_scope = partial_die_parent_scope (pdi, cu);
8104 if (parent_scope == NULL)
8105 return NULL;
8106 else
8107 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8108 pdi->name, 0, cu));
8109 }
8110
8111 static void
8112 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8113 {
8114 struct dwarf2_per_objfile *dwarf2_per_objfile
8115 = cu->per_cu->dwarf2_per_objfile;
8116 struct objfile *objfile = dwarf2_per_objfile->objfile;
8117 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8118 CORE_ADDR addr = 0;
8119 const char *actual_name = NULL;
8120 CORE_ADDR baseaddr;
8121
8122 baseaddr = objfile->text_section_offset ();
8123
8124 gdb::unique_xmalloc_ptr<char> built_actual_name
8125 = partial_die_full_name (pdi, cu);
8126 if (built_actual_name != NULL)
8127 actual_name = built_actual_name.get ();
8128
8129 if (actual_name == NULL)
8130 actual_name = pdi->name;
8131
8132 switch (pdi->tag)
8133 {
8134 case DW_TAG_inlined_subroutine:
8135 case DW_TAG_subprogram:
8136 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8137 - baseaddr);
8138 if (pdi->is_external
8139 || cu->language == language_ada
8140 || (cu->language == language_fortran
8141 && pdi->die_parent != NULL
8142 && pdi->die_parent->tag == DW_TAG_subprogram))
8143 {
8144 /* Normally, only "external" DIEs are part of the global scope.
8145 But in Ada and Fortran, we want to be able to access nested
8146 procedures globally. So all Ada and Fortran subprograms are
8147 stored in the global scope. */
8148 add_psymbol_to_list (actual_name,
8149 built_actual_name != NULL,
8150 VAR_DOMAIN, LOC_BLOCK,
8151 SECT_OFF_TEXT (objfile),
8152 psymbol_placement::GLOBAL,
8153 addr,
8154 cu->language, objfile);
8155 }
8156 else
8157 {
8158 add_psymbol_to_list (actual_name,
8159 built_actual_name != NULL,
8160 VAR_DOMAIN, LOC_BLOCK,
8161 SECT_OFF_TEXT (objfile),
8162 psymbol_placement::STATIC,
8163 addr, cu->language, objfile);
8164 }
8165
8166 if (pdi->main_subprogram && actual_name != NULL)
8167 set_objfile_main_name (objfile, actual_name, cu->language);
8168 break;
8169 case DW_TAG_constant:
8170 add_psymbol_to_list (actual_name,
8171 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8172 -1, (pdi->is_external
8173 ? psymbol_placement::GLOBAL
8174 : psymbol_placement::STATIC),
8175 0, cu->language, objfile);
8176 break;
8177 case DW_TAG_variable:
8178 if (pdi->d.locdesc)
8179 addr = decode_locdesc (pdi->d.locdesc, cu);
8180
8181 if (pdi->d.locdesc
8182 && addr == 0
8183 && !dwarf2_per_objfile->has_section_at_zero)
8184 {
8185 /* A global or static variable may also have been stripped
8186 out by the linker if unused, in which case its address
8187 will be nullified; do not add such variables into partial
8188 symbol table then. */
8189 }
8190 else if (pdi->is_external)
8191 {
8192 /* Global Variable.
8193 Don't enter into the minimal symbol tables as there is
8194 a minimal symbol table entry from the ELF symbols already.
8195 Enter into partial symbol table if it has a location
8196 descriptor or a type.
8197 If the location descriptor is missing, new_symbol will create
8198 a LOC_UNRESOLVED symbol, the address of the variable will then
8199 be determined from the minimal symbol table whenever the variable
8200 is referenced.
8201 The address for the partial symbol table entry is not
8202 used by GDB, but it comes in handy for debugging partial symbol
8203 table building. */
8204
8205 if (pdi->d.locdesc || pdi->has_type)
8206 add_psymbol_to_list (actual_name,
8207 built_actual_name != NULL,
8208 VAR_DOMAIN, LOC_STATIC,
8209 SECT_OFF_TEXT (objfile),
8210 psymbol_placement::GLOBAL,
8211 addr, cu->language, objfile);
8212 }
8213 else
8214 {
8215 int has_loc = pdi->d.locdesc != NULL;
8216
8217 /* Static Variable. Skip symbols whose value we cannot know (those
8218 without location descriptors or constant values). */
8219 if (!has_loc && !pdi->has_const_value)
8220 return;
8221
8222 add_psymbol_to_list (actual_name,
8223 built_actual_name != NULL,
8224 VAR_DOMAIN, LOC_STATIC,
8225 SECT_OFF_TEXT (objfile),
8226 psymbol_placement::STATIC,
8227 has_loc ? addr : 0,
8228 cu->language, objfile);
8229 }
8230 break;
8231 case DW_TAG_typedef:
8232 case DW_TAG_base_type:
8233 case DW_TAG_subrange_type:
8234 add_psymbol_to_list (actual_name,
8235 built_actual_name != NULL,
8236 VAR_DOMAIN, LOC_TYPEDEF, -1,
8237 psymbol_placement::STATIC,
8238 0, cu->language, objfile);
8239 break;
8240 case DW_TAG_imported_declaration:
8241 case DW_TAG_namespace:
8242 add_psymbol_to_list (actual_name,
8243 built_actual_name != NULL,
8244 VAR_DOMAIN, LOC_TYPEDEF, -1,
8245 psymbol_placement::GLOBAL,
8246 0, cu->language, objfile);
8247 break;
8248 case DW_TAG_module:
8249 /* With Fortran 77 there might be a "BLOCK DATA" module
8250 available without any name. If so, we skip the module as it
8251 doesn't bring any value. */
8252 if (actual_name != nullptr)
8253 add_psymbol_to_list (actual_name,
8254 built_actual_name != NULL,
8255 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8256 psymbol_placement::GLOBAL,
8257 0, cu->language, objfile);
8258 break;
8259 case DW_TAG_class_type:
8260 case DW_TAG_interface_type:
8261 case DW_TAG_structure_type:
8262 case DW_TAG_union_type:
8263 case DW_TAG_enumeration_type:
8264 /* Skip external references. The DWARF standard says in the section
8265 about "Structure, Union, and Class Type Entries": "An incomplete
8266 structure, union or class type is represented by a structure,
8267 union or class entry that does not have a byte size attribute
8268 and that has a DW_AT_declaration attribute." */
8269 if (!pdi->has_byte_size && pdi->is_declaration)
8270 return;
8271
8272 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8273 static vs. global. */
8274 add_psymbol_to_list (actual_name,
8275 built_actual_name != NULL,
8276 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8277 cu->language == language_cplus
8278 ? psymbol_placement::GLOBAL
8279 : psymbol_placement::STATIC,
8280 0, cu->language, objfile);
8281
8282 break;
8283 case DW_TAG_enumerator:
8284 add_psymbol_to_list (actual_name,
8285 built_actual_name != NULL,
8286 VAR_DOMAIN, LOC_CONST, -1,
8287 cu->language == language_cplus
8288 ? psymbol_placement::GLOBAL
8289 : psymbol_placement::STATIC,
8290 0, cu->language, objfile);
8291 break;
8292 default:
8293 break;
8294 }
8295 }
8296
8297 /* Read a partial die corresponding to a namespace; also, add a symbol
8298 corresponding to that namespace to the symbol table. NAMESPACE is
8299 the name of the enclosing namespace. */
8300
8301 static void
8302 add_partial_namespace (struct partial_die_info *pdi,
8303 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8304 int set_addrmap, struct dwarf2_cu *cu)
8305 {
8306 /* Add a symbol for the namespace. */
8307
8308 add_partial_symbol (pdi, cu);
8309
8310 /* Now scan partial symbols in that namespace. */
8311
8312 if (pdi->has_children)
8313 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8314 }
8315
8316 /* Read a partial die corresponding to a Fortran module. */
8317
8318 static void
8319 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8320 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8321 {
8322 /* Add a symbol for the namespace. */
8323
8324 add_partial_symbol (pdi, cu);
8325
8326 /* Now scan partial symbols in that module. */
8327
8328 if (pdi->has_children)
8329 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8330 }
8331
8332 /* Read a partial die corresponding to a subprogram or an inlined
8333 subprogram and create a partial symbol for that subprogram.
8334 When the CU language allows it, this routine also defines a partial
8335 symbol for each nested subprogram that this subprogram contains.
8336 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8337 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8338
8339 PDI may also be a lexical block, in which case we simply search
8340 recursively for subprograms defined inside that lexical block.
8341 Again, this is only performed when the CU language allows this
8342 type of definitions. */
8343
8344 static void
8345 add_partial_subprogram (struct partial_die_info *pdi,
8346 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8347 int set_addrmap, struct dwarf2_cu *cu)
8348 {
8349 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8350 {
8351 if (pdi->has_pc_info)
8352 {
8353 if (pdi->lowpc < *lowpc)
8354 *lowpc = pdi->lowpc;
8355 if (pdi->highpc > *highpc)
8356 *highpc = pdi->highpc;
8357 if (set_addrmap)
8358 {
8359 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8360 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8361 CORE_ADDR baseaddr;
8362 CORE_ADDR this_highpc;
8363 CORE_ADDR this_lowpc;
8364
8365 baseaddr = objfile->text_section_offset ();
8366 this_lowpc
8367 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8368 pdi->lowpc + baseaddr)
8369 - baseaddr);
8370 this_highpc
8371 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8372 pdi->highpc + baseaddr)
8373 - baseaddr);
8374 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8375 this_lowpc, this_highpc - 1,
8376 cu->per_cu->v.psymtab);
8377 }
8378 }
8379
8380 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8381 {
8382 if (!pdi->is_declaration)
8383 /* Ignore subprogram DIEs that do not have a name, they are
8384 illegal. Do not emit a complaint at this point, we will
8385 do so when we convert this psymtab into a symtab. */
8386 if (pdi->name)
8387 add_partial_symbol (pdi, cu);
8388 }
8389 }
8390
8391 if (! pdi->has_children)
8392 return;
8393
8394 if (cu->language == language_ada || cu->language == language_fortran)
8395 {
8396 pdi = pdi->die_child;
8397 while (pdi != NULL)
8398 {
8399 pdi->fixup (cu);
8400 if (pdi->tag == DW_TAG_subprogram
8401 || pdi->tag == DW_TAG_inlined_subroutine
8402 || pdi->tag == DW_TAG_lexical_block)
8403 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8404 pdi = pdi->die_sibling;
8405 }
8406 }
8407 }
8408
8409 /* Read a partial die corresponding to an enumeration type. */
8410
8411 static void
8412 add_partial_enumeration (struct partial_die_info *enum_pdi,
8413 struct dwarf2_cu *cu)
8414 {
8415 struct partial_die_info *pdi;
8416
8417 if (enum_pdi->name != NULL)
8418 add_partial_symbol (enum_pdi, cu);
8419
8420 pdi = enum_pdi->die_child;
8421 while (pdi)
8422 {
8423 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8424 complaint (_("malformed enumerator DIE ignored"));
8425 else
8426 add_partial_symbol (pdi, cu);
8427 pdi = pdi->die_sibling;
8428 }
8429 }
8430
8431 /* Return the initial uleb128 in the die at INFO_PTR. */
8432
8433 static unsigned int
8434 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8435 {
8436 unsigned int bytes_read;
8437
8438 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8439 }
8440
8441 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8442 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8443
8444 Return the corresponding abbrev, or NULL if the number is zero (indicating
8445 an empty DIE). In either case *BYTES_READ will be set to the length of
8446 the initial number. */
8447
8448 static struct abbrev_info *
8449 peek_die_abbrev (const die_reader_specs &reader,
8450 const gdb_byte *info_ptr, unsigned int *bytes_read)
8451 {
8452 dwarf2_cu *cu = reader.cu;
8453 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8454 unsigned int abbrev_number
8455 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8456
8457 if (abbrev_number == 0)
8458 return NULL;
8459
8460 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8461 if (!abbrev)
8462 {
8463 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8464 " at offset %s [in module %s]"),
8465 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8466 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8467 }
8468
8469 return abbrev;
8470 }
8471
8472 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8473 Returns a pointer to the end of a series of DIEs, terminated by an empty
8474 DIE. Any children of the skipped DIEs will also be skipped. */
8475
8476 static const gdb_byte *
8477 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8478 {
8479 while (1)
8480 {
8481 unsigned int bytes_read;
8482 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8483
8484 if (abbrev == NULL)
8485 return info_ptr + bytes_read;
8486 else
8487 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8488 }
8489 }
8490
8491 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8492 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8493 abbrev corresponding to that skipped uleb128 should be passed in
8494 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8495 children. */
8496
8497 static const gdb_byte *
8498 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8499 struct abbrev_info *abbrev)
8500 {
8501 unsigned int bytes_read;
8502 struct attribute attr;
8503 bfd *abfd = reader->abfd;
8504 struct dwarf2_cu *cu = reader->cu;
8505 const gdb_byte *buffer = reader->buffer;
8506 const gdb_byte *buffer_end = reader->buffer_end;
8507 unsigned int form, i;
8508
8509 for (i = 0; i < abbrev->num_attrs; i++)
8510 {
8511 /* The only abbrev we care about is DW_AT_sibling. */
8512 if (abbrev->attrs[i].name == DW_AT_sibling)
8513 {
8514 bool ignored;
8515 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8516 &ignored);
8517 if (attr.form == DW_FORM_ref_addr)
8518 complaint (_("ignoring absolute DW_AT_sibling"));
8519 else
8520 {
8521 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8522 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8523
8524 if (sibling_ptr < info_ptr)
8525 complaint (_("DW_AT_sibling points backwards"));
8526 else if (sibling_ptr > reader->buffer_end)
8527 reader->die_section->overflow_complaint ();
8528 else
8529 return sibling_ptr;
8530 }
8531 }
8532
8533 /* If it isn't DW_AT_sibling, skip this attribute. */
8534 form = abbrev->attrs[i].form;
8535 skip_attribute:
8536 switch (form)
8537 {
8538 case DW_FORM_ref_addr:
8539 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8540 and later it is offset sized. */
8541 if (cu->header.version == 2)
8542 info_ptr += cu->header.addr_size;
8543 else
8544 info_ptr += cu->header.offset_size;
8545 break;
8546 case DW_FORM_GNU_ref_alt:
8547 info_ptr += cu->header.offset_size;
8548 break;
8549 case DW_FORM_addr:
8550 info_ptr += cu->header.addr_size;
8551 break;
8552 case DW_FORM_data1:
8553 case DW_FORM_ref1:
8554 case DW_FORM_flag:
8555 case DW_FORM_strx1:
8556 info_ptr += 1;
8557 break;
8558 case DW_FORM_flag_present:
8559 case DW_FORM_implicit_const:
8560 break;
8561 case DW_FORM_data2:
8562 case DW_FORM_ref2:
8563 case DW_FORM_strx2:
8564 info_ptr += 2;
8565 break;
8566 case DW_FORM_strx3:
8567 info_ptr += 3;
8568 break;
8569 case DW_FORM_data4:
8570 case DW_FORM_ref4:
8571 case DW_FORM_strx4:
8572 info_ptr += 4;
8573 break;
8574 case DW_FORM_data8:
8575 case DW_FORM_ref8:
8576 case DW_FORM_ref_sig8:
8577 info_ptr += 8;
8578 break;
8579 case DW_FORM_data16:
8580 info_ptr += 16;
8581 break;
8582 case DW_FORM_string:
8583 read_direct_string (abfd, info_ptr, &bytes_read);
8584 info_ptr += bytes_read;
8585 break;
8586 case DW_FORM_sec_offset:
8587 case DW_FORM_strp:
8588 case DW_FORM_GNU_strp_alt:
8589 info_ptr += cu->header.offset_size;
8590 break;
8591 case DW_FORM_exprloc:
8592 case DW_FORM_block:
8593 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8594 info_ptr += bytes_read;
8595 break;
8596 case DW_FORM_block1:
8597 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8598 break;
8599 case DW_FORM_block2:
8600 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8601 break;
8602 case DW_FORM_block4:
8603 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8604 break;
8605 case DW_FORM_addrx:
8606 case DW_FORM_strx:
8607 case DW_FORM_sdata:
8608 case DW_FORM_udata:
8609 case DW_FORM_ref_udata:
8610 case DW_FORM_GNU_addr_index:
8611 case DW_FORM_GNU_str_index:
8612 case DW_FORM_rnglistx:
8613 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8614 break;
8615 case DW_FORM_indirect:
8616 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8617 info_ptr += bytes_read;
8618 /* We need to continue parsing from here, so just go back to
8619 the top. */
8620 goto skip_attribute;
8621
8622 default:
8623 error (_("Dwarf Error: Cannot handle %s "
8624 "in DWARF reader [in module %s]"),
8625 dwarf_form_name (form),
8626 bfd_get_filename (abfd));
8627 }
8628 }
8629
8630 if (abbrev->has_children)
8631 return skip_children (reader, info_ptr);
8632 else
8633 return info_ptr;
8634 }
8635
8636 /* Locate ORIG_PDI's sibling.
8637 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8638
8639 static const gdb_byte *
8640 locate_pdi_sibling (const struct die_reader_specs *reader,
8641 struct partial_die_info *orig_pdi,
8642 const gdb_byte *info_ptr)
8643 {
8644 /* Do we know the sibling already? */
8645
8646 if (orig_pdi->sibling)
8647 return orig_pdi->sibling;
8648
8649 /* Are there any children to deal with? */
8650
8651 if (!orig_pdi->has_children)
8652 return info_ptr;
8653
8654 /* Skip the children the long way. */
8655
8656 return skip_children (reader, info_ptr);
8657 }
8658
8659 /* Expand this partial symbol table into a full symbol table. SELF is
8660 not NULL. */
8661
8662 void
8663 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8664 {
8665 struct dwarf2_per_objfile *dwarf2_per_objfile
8666 = get_dwarf2_per_objfile (objfile);
8667
8668 gdb_assert (!readin);
8669 /* If this psymtab is constructed from a debug-only objfile, the
8670 has_section_at_zero flag will not necessarily be correct. We
8671 can get the correct value for this flag by looking at the data
8672 associated with the (presumably stripped) associated objfile. */
8673 if (objfile->separate_debug_objfile_backlink)
8674 {
8675 struct dwarf2_per_objfile *dpo_backlink
8676 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8677
8678 dwarf2_per_objfile->has_section_at_zero
8679 = dpo_backlink->has_section_at_zero;
8680 }
8681
8682 expand_psymtab (objfile);
8683
8684 process_cu_includes (dwarf2_per_objfile);
8685 }
8686 \f
8687 /* Reading in full CUs. */
8688
8689 /* Add PER_CU to the queue. */
8690
8691 static void
8692 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8693 enum language pretend_language)
8694 {
8695 per_cu->queued = 1;
8696 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8697 }
8698
8699 /* If PER_CU is not yet queued, add it to the queue.
8700 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8701 dependency.
8702 The result is non-zero if PER_CU was queued, otherwise the result is zero
8703 meaning either PER_CU is already queued or it is already loaded.
8704
8705 N.B. There is an invariant here that if a CU is queued then it is loaded.
8706 The caller is required to load PER_CU if we return non-zero. */
8707
8708 static int
8709 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8710 struct dwarf2_per_cu_data *per_cu,
8711 enum language pretend_language)
8712 {
8713 /* We may arrive here during partial symbol reading, if we need full
8714 DIEs to process an unusual case (e.g. template arguments). Do
8715 not queue PER_CU, just tell our caller to load its DIEs. */
8716 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8717 {
8718 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8719 return 1;
8720 return 0;
8721 }
8722
8723 /* Mark the dependence relation so that we don't flush PER_CU
8724 too early. */
8725 if (dependent_cu != NULL)
8726 dwarf2_add_dependence (dependent_cu, per_cu);
8727
8728 /* If it's already on the queue, we have nothing to do. */
8729 if (per_cu->queued)
8730 return 0;
8731
8732 /* If the compilation unit is already loaded, just mark it as
8733 used. */
8734 if (per_cu->cu != NULL)
8735 {
8736 per_cu->cu->last_used = 0;
8737 return 0;
8738 }
8739
8740 /* Add it to the queue. */
8741 queue_comp_unit (per_cu, pretend_language);
8742
8743 return 1;
8744 }
8745
8746 /* Process the queue. */
8747
8748 static void
8749 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8750 {
8751 if (dwarf_read_debug)
8752 {
8753 fprintf_unfiltered (gdb_stdlog,
8754 "Expanding one or more symtabs of objfile %s ...\n",
8755 objfile_name (dwarf2_per_objfile->objfile));
8756 }
8757
8758 /* The queue starts out with one item, but following a DIE reference
8759 may load a new CU, adding it to the end of the queue. */
8760 while (!dwarf2_per_objfile->queue.empty ())
8761 {
8762 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8763
8764 if ((dwarf2_per_objfile->using_index
8765 ? !item.per_cu->v.quick->compunit_symtab
8766 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8767 /* Skip dummy CUs. */
8768 && item.per_cu->cu != NULL)
8769 {
8770 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8771 unsigned int debug_print_threshold;
8772 char buf[100];
8773
8774 if (per_cu->is_debug_types)
8775 {
8776 struct signatured_type *sig_type =
8777 (struct signatured_type *) per_cu;
8778
8779 sprintf (buf, "TU %s at offset %s",
8780 hex_string (sig_type->signature),
8781 sect_offset_str (per_cu->sect_off));
8782 /* There can be 100s of TUs.
8783 Only print them in verbose mode. */
8784 debug_print_threshold = 2;
8785 }
8786 else
8787 {
8788 sprintf (buf, "CU at offset %s",
8789 sect_offset_str (per_cu->sect_off));
8790 debug_print_threshold = 1;
8791 }
8792
8793 if (dwarf_read_debug >= debug_print_threshold)
8794 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8795
8796 if (per_cu->is_debug_types)
8797 process_full_type_unit (per_cu, item.pretend_language);
8798 else
8799 process_full_comp_unit (per_cu, item.pretend_language);
8800
8801 if (dwarf_read_debug >= debug_print_threshold)
8802 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8803 }
8804
8805 item.per_cu->queued = 0;
8806 dwarf2_per_objfile->queue.pop ();
8807 }
8808
8809 if (dwarf_read_debug)
8810 {
8811 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8812 objfile_name (dwarf2_per_objfile->objfile));
8813 }
8814 }
8815
8816 /* Read in full symbols for PST, and anything it depends on. */
8817
8818 void
8819 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8820 {
8821 if (readin)
8822 return;
8823
8824 read_dependencies (objfile);
8825
8826 dw2_do_instantiate_symtab (per_cu_data, false);
8827 gdb_assert (get_compunit_symtab () != nullptr);
8828 }
8829
8830 /* Trivial hash function for die_info: the hash value of a DIE
8831 is its offset in .debug_info for this objfile. */
8832
8833 static hashval_t
8834 die_hash (const void *item)
8835 {
8836 const struct die_info *die = (const struct die_info *) item;
8837
8838 return to_underlying (die->sect_off);
8839 }
8840
8841 /* Trivial comparison function for die_info structures: two DIEs
8842 are equal if they have the same offset. */
8843
8844 static int
8845 die_eq (const void *item_lhs, const void *item_rhs)
8846 {
8847 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8848 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8849
8850 return die_lhs->sect_off == die_rhs->sect_off;
8851 }
8852
8853 /* Load the DIEs associated with PER_CU into memory. */
8854
8855 static void
8856 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8857 bool skip_partial,
8858 enum language pretend_language)
8859 {
8860 gdb_assert (! this_cu->is_debug_types);
8861
8862 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8863 if (reader.dummy_p)
8864 return;
8865
8866 struct dwarf2_cu *cu = reader.cu;
8867 const gdb_byte *info_ptr = reader.info_ptr;
8868
8869 gdb_assert (cu->die_hash == NULL);
8870 cu->die_hash =
8871 htab_create_alloc_ex (cu->header.length / 12,
8872 die_hash,
8873 die_eq,
8874 NULL,
8875 &cu->comp_unit_obstack,
8876 hashtab_obstack_allocate,
8877 dummy_obstack_deallocate);
8878
8879 if (reader.comp_unit_die->has_children)
8880 reader.comp_unit_die->child
8881 = read_die_and_siblings (&reader, reader.info_ptr,
8882 &info_ptr, reader.comp_unit_die);
8883 cu->dies = reader.comp_unit_die;
8884 /* comp_unit_die is not stored in die_hash, no need. */
8885
8886 /* We try not to read any attributes in this function, because not
8887 all CUs needed for references have been loaded yet, and symbol
8888 table processing isn't initialized. But we have to set the CU language,
8889 or we won't be able to build types correctly.
8890 Similarly, if we do not read the producer, we can not apply
8891 producer-specific interpretation. */
8892 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8893
8894 reader.keep ();
8895 }
8896
8897 /* Add a DIE to the delayed physname list. */
8898
8899 static void
8900 add_to_method_list (struct type *type, int fnfield_index, int index,
8901 const char *name, struct die_info *die,
8902 struct dwarf2_cu *cu)
8903 {
8904 struct delayed_method_info mi;
8905 mi.type = type;
8906 mi.fnfield_index = fnfield_index;
8907 mi.index = index;
8908 mi.name = name;
8909 mi.die = die;
8910 cu->method_list.push_back (mi);
8911 }
8912
8913 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8914 "const" / "volatile". If so, decrements LEN by the length of the
8915 modifier and return true. Otherwise return false. */
8916
8917 template<size_t N>
8918 static bool
8919 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8920 {
8921 size_t mod_len = sizeof (mod) - 1;
8922 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8923 {
8924 len -= mod_len;
8925 return true;
8926 }
8927 return false;
8928 }
8929
8930 /* Compute the physnames of any methods on the CU's method list.
8931
8932 The computation of method physnames is delayed in order to avoid the
8933 (bad) condition that one of the method's formal parameters is of an as yet
8934 incomplete type. */
8935
8936 static void
8937 compute_delayed_physnames (struct dwarf2_cu *cu)
8938 {
8939 /* Only C++ delays computing physnames. */
8940 if (cu->method_list.empty ())
8941 return;
8942 gdb_assert (cu->language == language_cplus);
8943
8944 for (const delayed_method_info &mi : cu->method_list)
8945 {
8946 const char *physname;
8947 struct fn_fieldlist *fn_flp
8948 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8949 physname = dwarf2_physname (mi.name, mi.die, cu);
8950 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8951 = physname ? physname : "";
8952
8953 /* Since there's no tag to indicate whether a method is a
8954 const/volatile overload, extract that information out of the
8955 demangled name. */
8956 if (physname != NULL)
8957 {
8958 size_t len = strlen (physname);
8959
8960 while (1)
8961 {
8962 if (physname[len] == ')') /* shortcut */
8963 break;
8964 else if (check_modifier (physname, len, " const"))
8965 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8966 else if (check_modifier (physname, len, " volatile"))
8967 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8968 else
8969 break;
8970 }
8971 }
8972 }
8973
8974 /* The list is no longer needed. */
8975 cu->method_list.clear ();
8976 }
8977
8978 /* Go objects should be embedded in a DW_TAG_module DIE,
8979 and it's not clear if/how imported objects will appear.
8980 To keep Go support simple until that's worked out,
8981 go back through what we've read and create something usable.
8982 We could do this while processing each DIE, and feels kinda cleaner,
8983 but that way is more invasive.
8984 This is to, for example, allow the user to type "p var" or "b main"
8985 without having to specify the package name, and allow lookups
8986 of module.object to work in contexts that use the expression
8987 parser. */
8988
8989 static void
8990 fixup_go_packaging (struct dwarf2_cu *cu)
8991 {
8992 gdb::unique_xmalloc_ptr<char> package_name;
8993 struct pending *list;
8994 int i;
8995
8996 for (list = *cu->get_builder ()->get_global_symbols ();
8997 list != NULL;
8998 list = list->next)
8999 {
9000 for (i = 0; i < list->nsyms; ++i)
9001 {
9002 struct symbol *sym = list->symbol[i];
9003
9004 if (sym->language () == language_go
9005 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9006 {
9007 gdb::unique_xmalloc_ptr<char> this_package_name
9008 (go_symbol_package_name (sym));
9009
9010 if (this_package_name == NULL)
9011 continue;
9012 if (package_name == NULL)
9013 package_name = std::move (this_package_name);
9014 else
9015 {
9016 struct objfile *objfile
9017 = cu->per_cu->dwarf2_per_objfile->objfile;
9018 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9019 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9020 (symbol_symtab (sym) != NULL
9021 ? symtab_to_filename_for_display
9022 (symbol_symtab (sym))
9023 : objfile_name (objfile)),
9024 this_package_name.get (), package_name.get ());
9025 }
9026 }
9027 }
9028 }
9029
9030 if (package_name != NULL)
9031 {
9032 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9033 const char *saved_package_name = objfile->intern (package_name.get ());
9034 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9035 saved_package_name);
9036 struct symbol *sym;
9037
9038 sym = allocate_symbol (objfile);
9039 sym->set_language (language_go, &objfile->objfile_obstack);
9040 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9041 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9042 e.g., "main" finds the "main" module and not C's main(). */
9043 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9044 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9045 SYMBOL_TYPE (sym) = type;
9046
9047 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9048 }
9049 }
9050
9051 /* Allocate a fully-qualified name consisting of the two parts on the
9052 obstack. */
9053
9054 static const char *
9055 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9056 {
9057 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9058 }
9059
9060 /* A helper that allocates a struct discriminant_info to attach to a
9061 union type. */
9062
9063 static struct discriminant_info *
9064 alloc_discriminant_info (struct type *type, int discriminant_index,
9065 int default_index)
9066 {
9067 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9068 gdb_assert (discriminant_index == -1
9069 || (discriminant_index >= 0
9070 && discriminant_index < TYPE_NFIELDS (type)));
9071 gdb_assert (default_index == -1
9072 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9073
9074 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9075
9076 struct discriminant_info *disc
9077 = ((struct discriminant_info *)
9078 TYPE_ZALLOC (type,
9079 offsetof (struct discriminant_info, discriminants)
9080 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9081 disc->default_index = default_index;
9082 disc->discriminant_index = discriminant_index;
9083
9084 struct dynamic_prop prop;
9085 prop.kind = PROP_UNDEFINED;
9086 prop.data.baton = disc;
9087
9088 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9089
9090 return disc;
9091 }
9092
9093 /* Some versions of rustc emitted enums in an unusual way.
9094
9095 Ordinary enums were emitted as unions. The first element of each
9096 structure in the union was named "RUST$ENUM$DISR". This element
9097 held the discriminant.
9098
9099 These versions of Rust also implemented the "non-zero"
9100 optimization. When the enum had two values, and one is empty and
9101 the other holds a pointer that cannot be zero, the pointer is used
9102 as the discriminant, with a zero value meaning the empty variant.
9103 Here, the union's first member is of the form
9104 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9105 where the fieldnos are the indices of the fields that should be
9106 traversed in order to find the field (which may be several fields deep)
9107 and the variantname is the name of the variant of the case when the
9108 field is zero.
9109
9110 This function recognizes whether TYPE is of one of these forms,
9111 and, if so, smashes it to be a variant type. */
9112
9113 static void
9114 quirk_rust_enum (struct type *type, struct objfile *objfile)
9115 {
9116 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9117
9118 /* We don't need to deal with empty enums. */
9119 if (TYPE_NFIELDS (type) == 0)
9120 return;
9121
9122 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9123 if (TYPE_NFIELDS (type) == 1
9124 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9125 {
9126 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9127
9128 /* Decode the field name to find the offset of the
9129 discriminant. */
9130 ULONGEST bit_offset = 0;
9131 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9132 while (name[0] >= '0' && name[0] <= '9')
9133 {
9134 char *tail;
9135 unsigned long index = strtoul (name, &tail, 10);
9136 name = tail;
9137 if (*name != '$'
9138 || index >= TYPE_NFIELDS (field_type)
9139 || (TYPE_FIELD_LOC_KIND (field_type, index)
9140 != FIELD_LOC_KIND_BITPOS))
9141 {
9142 complaint (_("Could not parse Rust enum encoding string \"%s\""
9143 "[in module %s]"),
9144 TYPE_FIELD_NAME (type, 0),
9145 objfile_name (objfile));
9146 return;
9147 }
9148 ++name;
9149
9150 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9151 field_type = TYPE_FIELD_TYPE (field_type, index);
9152 }
9153
9154 /* Make a union to hold the variants. */
9155 struct type *union_type = alloc_type (objfile);
9156 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9157 TYPE_NFIELDS (union_type) = 3;
9158 TYPE_FIELDS (union_type)
9159 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9160 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9161 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9162
9163 /* Put the discriminant must at index 0. */
9164 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9165 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9166 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9167 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9168
9169 /* The order of fields doesn't really matter, so put the real
9170 field at index 1 and the data-less field at index 2. */
9171 struct discriminant_info *disc
9172 = alloc_discriminant_info (union_type, 0, 1);
9173 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9174 TYPE_FIELD_NAME (union_type, 1)
9175 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9176 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9177 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9178 TYPE_FIELD_NAME (union_type, 1));
9179
9180 const char *dataless_name
9181 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9182 name);
9183 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9184 dataless_name);
9185 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9186 /* NAME points into the original discriminant name, which
9187 already has the correct lifetime. */
9188 TYPE_FIELD_NAME (union_type, 2) = name;
9189 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9190 disc->discriminants[2] = 0;
9191
9192 /* Smash this type to be a structure type. We have to do this
9193 because the type has already been recorded. */
9194 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9195 TYPE_NFIELDS (type) = 1;
9196 TYPE_FIELDS (type)
9197 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9198
9199 /* Install the variant part. */
9200 TYPE_FIELD_TYPE (type, 0) = union_type;
9201 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9202 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9203 }
9204 /* A union with a single anonymous field is probably an old-style
9205 univariant enum. */
9206 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9207 {
9208 /* Smash this type to be a structure type. We have to do this
9209 because the type has already been recorded. */
9210 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9211
9212 /* Make a union to hold the variants. */
9213 struct type *union_type = alloc_type (objfile);
9214 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9215 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9216 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9217 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9218 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9219
9220 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9221 const char *variant_name
9222 = rust_last_path_segment (TYPE_NAME (field_type));
9223 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9224 TYPE_NAME (field_type)
9225 = rust_fully_qualify (&objfile->objfile_obstack,
9226 TYPE_NAME (type), variant_name);
9227
9228 /* Install the union in the outer struct type. */
9229 TYPE_NFIELDS (type) = 1;
9230 TYPE_FIELDS (type)
9231 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9232 TYPE_FIELD_TYPE (type, 0) = union_type;
9233 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9234 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9235
9236 alloc_discriminant_info (union_type, -1, 0);
9237 }
9238 else
9239 {
9240 struct type *disr_type = nullptr;
9241 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9242 {
9243 disr_type = TYPE_FIELD_TYPE (type, i);
9244
9245 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9246 {
9247 /* All fields of a true enum will be structs. */
9248 return;
9249 }
9250 else if (TYPE_NFIELDS (disr_type) == 0)
9251 {
9252 /* Could be data-less variant, so keep going. */
9253 disr_type = nullptr;
9254 }
9255 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9256 "RUST$ENUM$DISR") != 0)
9257 {
9258 /* Not a Rust enum. */
9259 return;
9260 }
9261 else
9262 {
9263 /* Found one. */
9264 break;
9265 }
9266 }
9267
9268 /* If we got here without a discriminant, then it's probably
9269 just a union. */
9270 if (disr_type == nullptr)
9271 return;
9272
9273 /* Smash this type to be a structure type. We have to do this
9274 because the type has already been recorded. */
9275 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9276
9277 /* Make a union to hold the variants. */
9278 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9279 struct type *union_type = alloc_type (objfile);
9280 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9281 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9282 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9283 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9284 TYPE_FIELDS (union_type)
9285 = (struct field *) TYPE_ZALLOC (union_type,
9286 (TYPE_NFIELDS (union_type)
9287 * sizeof (struct field)));
9288
9289 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9290 TYPE_NFIELDS (type) * sizeof (struct field));
9291
9292 /* Install the discriminant at index 0 in the union. */
9293 TYPE_FIELD (union_type, 0) = *disr_field;
9294 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9295 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9296
9297 /* Install the union in the outer struct type. */
9298 TYPE_FIELD_TYPE (type, 0) = union_type;
9299 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9300 TYPE_NFIELDS (type) = 1;
9301
9302 /* Set the size and offset of the union type. */
9303 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9304
9305 /* We need a way to find the correct discriminant given a
9306 variant name. For convenience we build a map here. */
9307 struct type *enum_type = FIELD_TYPE (*disr_field);
9308 std::unordered_map<std::string, ULONGEST> discriminant_map;
9309 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9310 {
9311 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9312 {
9313 const char *name
9314 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9315 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9316 }
9317 }
9318
9319 int n_fields = TYPE_NFIELDS (union_type);
9320 struct discriminant_info *disc
9321 = alloc_discriminant_info (union_type, 0, -1);
9322 /* Skip the discriminant here. */
9323 for (int i = 1; i < n_fields; ++i)
9324 {
9325 /* Find the final word in the name of this variant's type.
9326 That name can be used to look up the correct
9327 discriminant. */
9328 const char *variant_name
9329 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9330 i)));
9331
9332 auto iter = discriminant_map.find (variant_name);
9333 if (iter != discriminant_map.end ())
9334 disc->discriminants[i] = iter->second;
9335
9336 /* Remove the discriminant field, if it exists. */
9337 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9338 if (TYPE_NFIELDS (sub_type) > 0)
9339 {
9340 --TYPE_NFIELDS (sub_type);
9341 ++TYPE_FIELDS (sub_type);
9342 }
9343 TYPE_FIELD_NAME (union_type, i) = variant_name;
9344 TYPE_NAME (sub_type)
9345 = rust_fully_qualify (&objfile->objfile_obstack,
9346 TYPE_NAME (type), variant_name);
9347 }
9348 }
9349 }
9350
9351 /* Rewrite some Rust unions to be structures with variants parts. */
9352
9353 static void
9354 rust_union_quirks (struct dwarf2_cu *cu)
9355 {
9356 gdb_assert (cu->language == language_rust);
9357 for (type *type_ : cu->rust_unions)
9358 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9359 /* We don't need this any more. */
9360 cu->rust_unions.clear ();
9361 }
9362
9363 /* Return the symtab for PER_CU. This works properly regardless of
9364 whether we're using the index or psymtabs. */
9365
9366 static struct compunit_symtab *
9367 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9368 {
9369 return (per_cu->dwarf2_per_objfile->using_index
9370 ? per_cu->v.quick->compunit_symtab
9371 : per_cu->v.psymtab->compunit_symtab);
9372 }
9373
9374 /* A helper function for computing the list of all symbol tables
9375 included by PER_CU. */
9376
9377 static void
9378 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9379 htab_t all_children, htab_t all_type_symtabs,
9380 struct dwarf2_per_cu_data *per_cu,
9381 struct compunit_symtab *immediate_parent)
9382 {
9383 void **slot;
9384 struct compunit_symtab *cust;
9385
9386 slot = htab_find_slot (all_children, per_cu, INSERT);
9387 if (*slot != NULL)
9388 {
9389 /* This inclusion and its children have been processed. */
9390 return;
9391 }
9392
9393 *slot = per_cu;
9394 /* Only add a CU if it has a symbol table. */
9395 cust = get_compunit_symtab (per_cu);
9396 if (cust != NULL)
9397 {
9398 /* If this is a type unit only add its symbol table if we haven't
9399 seen it yet (type unit per_cu's can share symtabs). */
9400 if (per_cu->is_debug_types)
9401 {
9402 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9403 if (*slot == NULL)
9404 {
9405 *slot = cust;
9406 result->push_back (cust);
9407 if (cust->user == NULL)
9408 cust->user = immediate_parent;
9409 }
9410 }
9411 else
9412 {
9413 result->push_back (cust);
9414 if (cust->user == NULL)
9415 cust->user = immediate_parent;
9416 }
9417 }
9418
9419 if (!per_cu->imported_symtabs_empty ())
9420 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9421 {
9422 recursively_compute_inclusions (result, all_children,
9423 all_type_symtabs, ptr, cust);
9424 }
9425 }
9426
9427 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9428 PER_CU. */
9429
9430 static void
9431 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9432 {
9433 gdb_assert (! per_cu->is_debug_types);
9434
9435 if (!per_cu->imported_symtabs_empty ())
9436 {
9437 int len;
9438 std::vector<compunit_symtab *> result_symtabs;
9439 htab_t all_children, all_type_symtabs;
9440 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9441
9442 /* If we don't have a symtab, we can just skip this case. */
9443 if (cust == NULL)
9444 return;
9445
9446 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9447 NULL, xcalloc, xfree);
9448 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9449 NULL, xcalloc, xfree);
9450
9451 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9452 {
9453 recursively_compute_inclusions (&result_symtabs, all_children,
9454 all_type_symtabs, ptr, cust);
9455 }
9456
9457 /* Now we have a transitive closure of all the included symtabs. */
9458 len = result_symtabs.size ();
9459 cust->includes
9460 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9461 struct compunit_symtab *, len + 1);
9462 memcpy (cust->includes, result_symtabs.data (),
9463 len * sizeof (compunit_symtab *));
9464 cust->includes[len] = NULL;
9465
9466 htab_delete (all_children);
9467 htab_delete (all_type_symtabs);
9468 }
9469 }
9470
9471 /* Compute the 'includes' field for the symtabs of all the CUs we just
9472 read. */
9473
9474 static void
9475 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9476 {
9477 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9478 {
9479 if (! iter->is_debug_types)
9480 compute_compunit_symtab_includes (iter);
9481 }
9482
9483 dwarf2_per_objfile->just_read_cus.clear ();
9484 }
9485
9486 /* Generate full symbol information for PER_CU, whose DIEs have
9487 already been loaded into memory. */
9488
9489 static void
9490 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9491 enum language pretend_language)
9492 {
9493 struct dwarf2_cu *cu = per_cu->cu;
9494 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9495 struct objfile *objfile = dwarf2_per_objfile->objfile;
9496 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9497 CORE_ADDR lowpc, highpc;
9498 struct compunit_symtab *cust;
9499 CORE_ADDR baseaddr;
9500 struct block *static_block;
9501 CORE_ADDR addr;
9502
9503 baseaddr = objfile->text_section_offset ();
9504
9505 /* Clear the list here in case something was left over. */
9506 cu->method_list.clear ();
9507
9508 cu->language = pretend_language;
9509 cu->language_defn = language_def (cu->language);
9510
9511 /* Do line number decoding in read_file_scope () */
9512 process_die (cu->dies, cu);
9513
9514 /* For now fudge the Go package. */
9515 if (cu->language == language_go)
9516 fixup_go_packaging (cu);
9517
9518 /* Now that we have processed all the DIEs in the CU, all the types
9519 should be complete, and it should now be safe to compute all of the
9520 physnames. */
9521 compute_delayed_physnames (cu);
9522
9523 if (cu->language == language_rust)
9524 rust_union_quirks (cu);
9525
9526 /* Some compilers don't define a DW_AT_high_pc attribute for the
9527 compilation unit. If the DW_AT_high_pc is missing, synthesize
9528 it, by scanning the DIE's below the compilation unit. */
9529 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9530
9531 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9532 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9533
9534 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9535 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9536 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9537 addrmap to help ensure it has an accurate map of pc values belonging to
9538 this comp unit. */
9539 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9540
9541 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9542 SECT_OFF_TEXT (objfile),
9543 0);
9544
9545 if (cust != NULL)
9546 {
9547 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9548
9549 /* Set symtab language to language from DW_AT_language. If the
9550 compilation is from a C file generated by language preprocessors, do
9551 not set the language if it was already deduced by start_subfile. */
9552 if (!(cu->language == language_c
9553 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9554 COMPUNIT_FILETABS (cust)->language = cu->language;
9555
9556 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9557 produce DW_AT_location with location lists but it can be possibly
9558 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9559 there were bugs in prologue debug info, fixed later in GCC-4.5
9560 by "unwind info for epilogues" patch (which is not directly related).
9561
9562 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9563 needed, it would be wrong due to missing DW_AT_producer there.
9564
9565 Still one can confuse GDB by using non-standard GCC compilation
9566 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9567 */
9568 if (cu->has_loclist && gcc_4_minor >= 5)
9569 cust->locations_valid = 1;
9570
9571 if (gcc_4_minor >= 5)
9572 cust->epilogue_unwind_valid = 1;
9573
9574 cust->call_site_htab = cu->call_site_htab;
9575 }
9576
9577 if (dwarf2_per_objfile->using_index)
9578 per_cu->v.quick->compunit_symtab = cust;
9579 else
9580 {
9581 dwarf2_psymtab *pst = per_cu->v.psymtab;
9582 pst->compunit_symtab = cust;
9583 pst->readin = true;
9584 }
9585
9586 /* Push it for inclusion processing later. */
9587 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9588
9589 /* Not needed any more. */
9590 cu->reset_builder ();
9591 }
9592
9593 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9594 already been loaded into memory. */
9595
9596 static void
9597 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9598 enum language pretend_language)
9599 {
9600 struct dwarf2_cu *cu = per_cu->cu;
9601 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9602 struct objfile *objfile = dwarf2_per_objfile->objfile;
9603 struct compunit_symtab *cust;
9604 struct signatured_type *sig_type;
9605
9606 gdb_assert (per_cu->is_debug_types);
9607 sig_type = (struct signatured_type *) per_cu;
9608
9609 /* Clear the list here in case something was left over. */
9610 cu->method_list.clear ();
9611
9612 cu->language = pretend_language;
9613 cu->language_defn = language_def (cu->language);
9614
9615 /* The symbol tables are set up in read_type_unit_scope. */
9616 process_die (cu->dies, cu);
9617
9618 /* For now fudge the Go package. */
9619 if (cu->language == language_go)
9620 fixup_go_packaging (cu);
9621
9622 /* Now that we have processed all the DIEs in the CU, all the types
9623 should be complete, and it should now be safe to compute all of the
9624 physnames. */
9625 compute_delayed_physnames (cu);
9626
9627 if (cu->language == language_rust)
9628 rust_union_quirks (cu);
9629
9630 /* TUs share symbol tables.
9631 If this is the first TU to use this symtab, complete the construction
9632 of it with end_expandable_symtab. Otherwise, complete the addition of
9633 this TU's symbols to the existing symtab. */
9634 if (sig_type->type_unit_group->compunit_symtab == NULL)
9635 {
9636 buildsym_compunit *builder = cu->get_builder ();
9637 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9638 sig_type->type_unit_group->compunit_symtab = cust;
9639
9640 if (cust != NULL)
9641 {
9642 /* Set symtab language to language from DW_AT_language. If the
9643 compilation is from a C file generated by language preprocessors,
9644 do not set the language if it was already deduced by
9645 start_subfile. */
9646 if (!(cu->language == language_c
9647 && COMPUNIT_FILETABS (cust)->language != language_c))
9648 COMPUNIT_FILETABS (cust)->language = cu->language;
9649 }
9650 }
9651 else
9652 {
9653 cu->get_builder ()->augment_type_symtab ();
9654 cust = sig_type->type_unit_group->compunit_symtab;
9655 }
9656
9657 if (dwarf2_per_objfile->using_index)
9658 per_cu->v.quick->compunit_symtab = cust;
9659 else
9660 {
9661 dwarf2_psymtab *pst = per_cu->v.psymtab;
9662 pst->compunit_symtab = cust;
9663 pst->readin = true;
9664 }
9665
9666 /* Not needed any more. */
9667 cu->reset_builder ();
9668 }
9669
9670 /* Process an imported unit DIE. */
9671
9672 static void
9673 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9674 {
9675 struct attribute *attr;
9676
9677 /* For now we don't handle imported units in type units. */
9678 if (cu->per_cu->is_debug_types)
9679 {
9680 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9681 " supported in type units [in module %s]"),
9682 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9683 }
9684
9685 attr = dwarf2_attr (die, DW_AT_import, cu);
9686 if (attr != NULL)
9687 {
9688 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9689 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9690 dwarf2_per_cu_data *per_cu
9691 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9692 cu->per_cu->dwarf2_per_objfile);
9693
9694 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9695 into another compilation unit, at root level. Regard this as a hint,
9696 and ignore it. */
9697 if (die->parent && die->parent->parent == NULL
9698 && per_cu->unit_type == DW_UT_compile
9699 && per_cu->lang == language_cplus)
9700 return;
9701
9702 /* If necessary, add it to the queue and load its DIEs. */
9703 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9704 load_full_comp_unit (per_cu, false, cu->language);
9705
9706 cu->per_cu->imported_symtabs_push (per_cu);
9707 }
9708 }
9709
9710 /* RAII object that represents a process_die scope: i.e.,
9711 starts/finishes processing a DIE. */
9712 class process_die_scope
9713 {
9714 public:
9715 process_die_scope (die_info *die, dwarf2_cu *cu)
9716 : m_die (die), m_cu (cu)
9717 {
9718 /* We should only be processing DIEs not already in process. */
9719 gdb_assert (!m_die->in_process);
9720 m_die->in_process = true;
9721 }
9722
9723 ~process_die_scope ()
9724 {
9725 m_die->in_process = false;
9726
9727 /* If we're done processing the DIE for the CU that owns the line
9728 header, we don't need the line header anymore. */
9729 if (m_cu->line_header_die_owner == m_die)
9730 {
9731 delete m_cu->line_header;
9732 m_cu->line_header = NULL;
9733 m_cu->line_header_die_owner = NULL;
9734 }
9735 }
9736
9737 private:
9738 die_info *m_die;
9739 dwarf2_cu *m_cu;
9740 };
9741
9742 /* Process a die and its children. */
9743
9744 static void
9745 process_die (struct die_info *die, struct dwarf2_cu *cu)
9746 {
9747 process_die_scope scope (die, cu);
9748
9749 switch (die->tag)
9750 {
9751 case DW_TAG_padding:
9752 break;
9753 case DW_TAG_compile_unit:
9754 case DW_TAG_partial_unit:
9755 read_file_scope (die, cu);
9756 break;
9757 case DW_TAG_type_unit:
9758 read_type_unit_scope (die, cu);
9759 break;
9760 case DW_TAG_subprogram:
9761 /* Nested subprograms in Fortran get a prefix. */
9762 if (cu->language == language_fortran
9763 && die->parent != NULL
9764 && die->parent->tag == DW_TAG_subprogram)
9765 cu->processing_has_namespace_info = true;
9766 /* Fall through. */
9767 case DW_TAG_inlined_subroutine:
9768 read_func_scope (die, cu);
9769 break;
9770 case DW_TAG_lexical_block:
9771 case DW_TAG_try_block:
9772 case DW_TAG_catch_block:
9773 read_lexical_block_scope (die, cu);
9774 break;
9775 case DW_TAG_call_site:
9776 case DW_TAG_GNU_call_site:
9777 read_call_site_scope (die, cu);
9778 break;
9779 case DW_TAG_class_type:
9780 case DW_TAG_interface_type:
9781 case DW_TAG_structure_type:
9782 case DW_TAG_union_type:
9783 process_structure_scope (die, cu);
9784 break;
9785 case DW_TAG_enumeration_type:
9786 process_enumeration_scope (die, cu);
9787 break;
9788
9789 /* These dies have a type, but processing them does not create
9790 a symbol or recurse to process the children. Therefore we can
9791 read them on-demand through read_type_die. */
9792 case DW_TAG_subroutine_type:
9793 case DW_TAG_set_type:
9794 case DW_TAG_array_type:
9795 case DW_TAG_pointer_type:
9796 case DW_TAG_ptr_to_member_type:
9797 case DW_TAG_reference_type:
9798 case DW_TAG_rvalue_reference_type:
9799 case DW_TAG_string_type:
9800 break;
9801
9802 case DW_TAG_base_type:
9803 case DW_TAG_subrange_type:
9804 case DW_TAG_typedef:
9805 /* Add a typedef symbol for the type definition, if it has a
9806 DW_AT_name. */
9807 new_symbol (die, read_type_die (die, cu), cu);
9808 break;
9809 case DW_TAG_common_block:
9810 read_common_block (die, cu);
9811 break;
9812 case DW_TAG_common_inclusion:
9813 break;
9814 case DW_TAG_namespace:
9815 cu->processing_has_namespace_info = true;
9816 read_namespace (die, cu);
9817 break;
9818 case DW_TAG_module:
9819 cu->processing_has_namespace_info = true;
9820 read_module (die, cu);
9821 break;
9822 case DW_TAG_imported_declaration:
9823 cu->processing_has_namespace_info = true;
9824 if (read_namespace_alias (die, cu))
9825 break;
9826 /* The declaration is not a global namespace alias. */
9827 /* Fall through. */
9828 case DW_TAG_imported_module:
9829 cu->processing_has_namespace_info = true;
9830 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9831 || cu->language != language_fortran))
9832 complaint (_("Tag '%s' has unexpected children"),
9833 dwarf_tag_name (die->tag));
9834 read_import_statement (die, cu);
9835 break;
9836
9837 case DW_TAG_imported_unit:
9838 process_imported_unit_die (die, cu);
9839 break;
9840
9841 case DW_TAG_variable:
9842 read_variable (die, cu);
9843 break;
9844
9845 default:
9846 new_symbol (die, NULL, cu);
9847 break;
9848 }
9849 }
9850 \f
9851 /* DWARF name computation. */
9852
9853 /* A helper function for dwarf2_compute_name which determines whether DIE
9854 needs to have the name of the scope prepended to the name listed in the
9855 die. */
9856
9857 static int
9858 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9859 {
9860 struct attribute *attr;
9861
9862 switch (die->tag)
9863 {
9864 case DW_TAG_namespace:
9865 case DW_TAG_typedef:
9866 case DW_TAG_class_type:
9867 case DW_TAG_interface_type:
9868 case DW_TAG_structure_type:
9869 case DW_TAG_union_type:
9870 case DW_TAG_enumeration_type:
9871 case DW_TAG_enumerator:
9872 case DW_TAG_subprogram:
9873 case DW_TAG_inlined_subroutine:
9874 case DW_TAG_member:
9875 case DW_TAG_imported_declaration:
9876 return 1;
9877
9878 case DW_TAG_variable:
9879 case DW_TAG_constant:
9880 /* We only need to prefix "globally" visible variables. These include
9881 any variable marked with DW_AT_external or any variable that
9882 lives in a namespace. [Variables in anonymous namespaces
9883 require prefixing, but they are not DW_AT_external.] */
9884
9885 if (dwarf2_attr (die, DW_AT_specification, cu))
9886 {
9887 struct dwarf2_cu *spec_cu = cu;
9888
9889 return die_needs_namespace (die_specification (die, &spec_cu),
9890 spec_cu);
9891 }
9892
9893 attr = dwarf2_attr (die, DW_AT_external, cu);
9894 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9895 && die->parent->tag != DW_TAG_module)
9896 return 0;
9897 /* A variable in a lexical block of some kind does not need a
9898 namespace, even though in C++ such variables may be external
9899 and have a mangled name. */
9900 if (die->parent->tag == DW_TAG_lexical_block
9901 || die->parent->tag == DW_TAG_try_block
9902 || die->parent->tag == DW_TAG_catch_block
9903 || die->parent->tag == DW_TAG_subprogram)
9904 return 0;
9905 return 1;
9906
9907 default:
9908 return 0;
9909 }
9910 }
9911
9912 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9913 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9914 defined for the given DIE. */
9915
9916 static struct attribute *
9917 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9918 {
9919 struct attribute *attr;
9920
9921 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9922 if (attr == NULL)
9923 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9924
9925 return attr;
9926 }
9927
9928 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9929 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9930 defined for the given DIE. */
9931
9932 static const char *
9933 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9934 {
9935 const char *linkage_name;
9936
9937 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9938 if (linkage_name == NULL)
9939 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9940
9941 return linkage_name;
9942 }
9943
9944 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9945 compute the physname for the object, which include a method's:
9946 - formal parameters (C++),
9947 - receiver type (Go),
9948
9949 The term "physname" is a bit confusing.
9950 For C++, for example, it is the demangled name.
9951 For Go, for example, it's the mangled name.
9952
9953 For Ada, return the DIE's linkage name rather than the fully qualified
9954 name. PHYSNAME is ignored..
9955
9956 The result is allocated on the objfile_obstack and canonicalized. */
9957
9958 static const char *
9959 dwarf2_compute_name (const char *name,
9960 struct die_info *die, struct dwarf2_cu *cu,
9961 int physname)
9962 {
9963 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9964
9965 if (name == NULL)
9966 name = dwarf2_name (die, cu);
9967
9968 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9969 but otherwise compute it by typename_concat inside GDB.
9970 FIXME: Actually this is not really true, or at least not always true.
9971 It's all very confusing. compute_and_set_names doesn't try to demangle
9972 Fortran names because there is no mangling standard. So new_symbol
9973 will set the demangled name to the result of dwarf2_full_name, and it is
9974 the demangled name that GDB uses if it exists. */
9975 if (cu->language == language_ada
9976 || (cu->language == language_fortran && physname))
9977 {
9978 /* For Ada unit, we prefer the linkage name over the name, as
9979 the former contains the exported name, which the user expects
9980 to be able to reference. Ideally, we want the user to be able
9981 to reference this entity using either natural or linkage name,
9982 but we haven't started looking at this enhancement yet. */
9983 const char *linkage_name = dw2_linkage_name (die, cu);
9984
9985 if (linkage_name != NULL)
9986 return linkage_name;
9987 }
9988
9989 /* These are the only languages we know how to qualify names in. */
9990 if (name != NULL
9991 && (cu->language == language_cplus
9992 || cu->language == language_fortran || cu->language == language_d
9993 || cu->language == language_rust))
9994 {
9995 if (die_needs_namespace (die, cu))
9996 {
9997 const char *prefix;
9998 const char *canonical_name = NULL;
9999
10000 string_file buf;
10001
10002 prefix = determine_prefix (die, cu);
10003 if (*prefix != '\0')
10004 {
10005 gdb::unique_xmalloc_ptr<char> prefixed_name
10006 (typename_concat (NULL, prefix, name, physname, cu));
10007
10008 buf.puts (prefixed_name.get ());
10009 }
10010 else
10011 buf.puts (name);
10012
10013 /* Template parameters may be specified in the DIE's DW_AT_name, or
10014 as children with DW_TAG_template_type_param or
10015 DW_TAG_value_type_param. If the latter, add them to the name
10016 here. If the name already has template parameters, then
10017 skip this step; some versions of GCC emit both, and
10018 it is more efficient to use the pre-computed name.
10019
10020 Something to keep in mind about this process: it is very
10021 unlikely, or in some cases downright impossible, to produce
10022 something that will match the mangled name of a function.
10023 If the definition of the function has the same debug info,
10024 we should be able to match up with it anyway. But fallbacks
10025 using the minimal symbol, for instance to find a method
10026 implemented in a stripped copy of libstdc++, will not work.
10027 If we do not have debug info for the definition, we will have to
10028 match them up some other way.
10029
10030 When we do name matching there is a related problem with function
10031 templates; two instantiated function templates are allowed to
10032 differ only by their return types, which we do not add here. */
10033
10034 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10035 {
10036 struct attribute *attr;
10037 struct die_info *child;
10038 int first = 1;
10039
10040 die->building_fullname = 1;
10041
10042 for (child = die->child; child != NULL; child = child->sibling)
10043 {
10044 struct type *type;
10045 LONGEST value;
10046 const gdb_byte *bytes;
10047 struct dwarf2_locexpr_baton *baton;
10048 struct value *v;
10049
10050 if (child->tag != DW_TAG_template_type_param
10051 && child->tag != DW_TAG_template_value_param)
10052 continue;
10053
10054 if (first)
10055 {
10056 buf.puts ("<");
10057 first = 0;
10058 }
10059 else
10060 buf.puts (", ");
10061
10062 attr = dwarf2_attr (child, DW_AT_type, cu);
10063 if (attr == NULL)
10064 {
10065 complaint (_("template parameter missing DW_AT_type"));
10066 buf.puts ("UNKNOWN_TYPE");
10067 continue;
10068 }
10069 type = die_type (child, cu);
10070
10071 if (child->tag == DW_TAG_template_type_param)
10072 {
10073 c_print_type (type, "", &buf, -1, 0, cu->language,
10074 &type_print_raw_options);
10075 continue;
10076 }
10077
10078 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10079 if (attr == NULL)
10080 {
10081 complaint (_("template parameter missing "
10082 "DW_AT_const_value"));
10083 buf.puts ("UNKNOWN_VALUE");
10084 continue;
10085 }
10086
10087 dwarf2_const_value_attr (attr, type, name,
10088 &cu->comp_unit_obstack, cu,
10089 &value, &bytes, &baton);
10090
10091 if (TYPE_NOSIGN (type))
10092 /* GDB prints characters as NUMBER 'CHAR'. If that's
10093 changed, this can use value_print instead. */
10094 c_printchar (value, type, &buf);
10095 else
10096 {
10097 struct value_print_options opts;
10098
10099 if (baton != NULL)
10100 v = dwarf2_evaluate_loc_desc (type, NULL,
10101 baton->data,
10102 baton->size,
10103 baton->per_cu);
10104 else if (bytes != NULL)
10105 {
10106 v = allocate_value (type);
10107 memcpy (value_contents_writeable (v), bytes,
10108 TYPE_LENGTH (type));
10109 }
10110 else
10111 v = value_from_longest (type, value);
10112
10113 /* Specify decimal so that we do not depend on
10114 the radix. */
10115 get_formatted_print_options (&opts, 'd');
10116 opts.raw = 1;
10117 value_print (v, &buf, &opts);
10118 release_value (v);
10119 }
10120 }
10121
10122 die->building_fullname = 0;
10123
10124 if (!first)
10125 {
10126 /* Close the argument list, with a space if necessary
10127 (nested templates). */
10128 if (!buf.empty () && buf.string ().back () == '>')
10129 buf.puts (" >");
10130 else
10131 buf.puts (">");
10132 }
10133 }
10134
10135 /* For C++ methods, append formal parameter type
10136 information, if PHYSNAME. */
10137
10138 if (physname && die->tag == DW_TAG_subprogram
10139 && cu->language == language_cplus)
10140 {
10141 struct type *type = read_type_die (die, cu);
10142
10143 c_type_print_args (type, &buf, 1, cu->language,
10144 &type_print_raw_options);
10145
10146 if (cu->language == language_cplus)
10147 {
10148 /* Assume that an artificial first parameter is
10149 "this", but do not crash if it is not. RealView
10150 marks unnamed (and thus unused) parameters as
10151 artificial; there is no way to differentiate
10152 the two cases. */
10153 if (TYPE_NFIELDS (type) > 0
10154 && TYPE_FIELD_ARTIFICIAL (type, 0)
10155 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10156 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10157 0))))
10158 buf.puts (" const");
10159 }
10160 }
10161
10162 const std::string &intermediate_name = buf.string ();
10163
10164 if (cu->language == language_cplus)
10165 canonical_name
10166 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10167 objfile);
10168
10169 /* If we only computed INTERMEDIATE_NAME, or if
10170 INTERMEDIATE_NAME is already canonical, then we need to
10171 intern it. */
10172 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10173 name = objfile->intern (intermediate_name);
10174 else
10175 name = canonical_name;
10176 }
10177 }
10178
10179 return name;
10180 }
10181
10182 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10183 If scope qualifiers are appropriate they will be added. The result
10184 will be allocated on the storage_obstack, or NULL if the DIE does
10185 not have a name. NAME may either be from a previous call to
10186 dwarf2_name or NULL.
10187
10188 The output string will be canonicalized (if C++). */
10189
10190 static const char *
10191 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10192 {
10193 return dwarf2_compute_name (name, die, cu, 0);
10194 }
10195
10196 /* Construct a physname for the given DIE in CU. NAME may either be
10197 from a previous call to dwarf2_name or NULL. The result will be
10198 allocated on the objfile_objstack or NULL if the DIE does not have a
10199 name.
10200
10201 The output string will be canonicalized (if C++). */
10202
10203 static const char *
10204 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10205 {
10206 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10207 const char *retval, *mangled = NULL, *canon = NULL;
10208 int need_copy = 1;
10209
10210 /* In this case dwarf2_compute_name is just a shortcut not building anything
10211 on its own. */
10212 if (!die_needs_namespace (die, cu))
10213 return dwarf2_compute_name (name, die, cu, 1);
10214
10215 mangled = dw2_linkage_name (die, cu);
10216
10217 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10218 See https://github.com/rust-lang/rust/issues/32925. */
10219 if (cu->language == language_rust && mangled != NULL
10220 && strchr (mangled, '{') != NULL)
10221 mangled = NULL;
10222
10223 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10224 has computed. */
10225 gdb::unique_xmalloc_ptr<char> demangled;
10226 if (mangled != NULL)
10227 {
10228
10229 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10230 {
10231 /* Do nothing (do not demangle the symbol name). */
10232 }
10233 else if (cu->language == language_go)
10234 {
10235 /* This is a lie, but we already lie to the caller new_symbol.
10236 new_symbol assumes we return the mangled name.
10237 This just undoes that lie until things are cleaned up. */
10238 }
10239 else
10240 {
10241 /* Use DMGL_RET_DROP for C++ template functions to suppress
10242 their return type. It is easier for GDB users to search
10243 for such functions as `name(params)' than `long name(params)'.
10244 In such case the minimal symbol names do not match the full
10245 symbol names but for template functions there is never a need
10246 to look up their definition from their declaration so
10247 the only disadvantage remains the minimal symbol variant
10248 `long name(params)' does not have the proper inferior type. */
10249 demangled.reset (gdb_demangle (mangled,
10250 (DMGL_PARAMS | DMGL_ANSI
10251 | DMGL_RET_DROP)));
10252 }
10253 if (demangled)
10254 canon = demangled.get ();
10255 else
10256 {
10257 canon = mangled;
10258 need_copy = 0;
10259 }
10260 }
10261
10262 if (canon == NULL || check_physname)
10263 {
10264 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10265
10266 if (canon != NULL && strcmp (physname, canon) != 0)
10267 {
10268 /* It may not mean a bug in GDB. The compiler could also
10269 compute DW_AT_linkage_name incorrectly. But in such case
10270 GDB would need to be bug-to-bug compatible. */
10271
10272 complaint (_("Computed physname <%s> does not match demangled <%s> "
10273 "(from linkage <%s>) - DIE at %s [in module %s]"),
10274 physname, canon, mangled, sect_offset_str (die->sect_off),
10275 objfile_name (objfile));
10276
10277 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10278 is available here - over computed PHYSNAME. It is safer
10279 against both buggy GDB and buggy compilers. */
10280
10281 retval = canon;
10282 }
10283 else
10284 {
10285 retval = physname;
10286 need_copy = 0;
10287 }
10288 }
10289 else
10290 retval = canon;
10291
10292 if (need_copy)
10293 retval = objfile->intern (retval);
10294
10295 return retval;
10296 }
10297
10298 /* Inspect DIE in CU for a namespace alias. If one exists, record
10299 a new symbol for it.
10300
10301 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10302
10303 static int
10304 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10305 {
10306 struct attribute *attr;
10307
10308 /* If the die does not have a name, this is not a namespace
10309 alias. */
10310 attr = dwarf2_attr (die, DW_AT_name, cu);
10311 if (attr != NULL)
10312 {
10313 int num;
10314 struct die_info *d = die;
10315 struct dwarf2_cu *imported_cu = cu;
10316
10317 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10318 keep inspecting DIEs until we hit the underlying import. */
10319 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10320 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10321 {
10322 attr = dwarf2_attr (d, DW_AT_import, cu);
10323 if (attr == NULL)
10324 break;
10325
10326 d = follow_die_ref (d, attr, &imported_cu);
10327 if (d->tag != DW_TAG_imported_declaration)
10328 break;
10329 }
10330
10331 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10332 {
10333 complaint (_("DIE at %s has too many recursively imported "
10334 "declarations"), sect_offset_str (d->sect_off));
10335 return 0;
10336 }
10337
10338 if (attr != NULL)
10339 {
10340 struct type *type;
10341 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10342
10343 type = get_die_type_at_offset (sect_off, cu->per_cu);
10344 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10345 {
10346 /* This declaration is a global namespace alias. Add
10347 a symbol for it whose type is the aliased namespace. */
10348 new_symbol (die, type, cu);
10349 return 1;
10350 }
10351 }
10352 }
10353
10354 return 0;
10355 }
10356
10357 /* Return the using directives repository (global or local?) to use in the
10358 current context for CU.
10359
10360 For Ada, imported declarations can materialize renamings, which *may* be
10361 global. However it is impossible (for now?) in DWARF to distinguish
10362 "external" imported declarations and "static" ones. As all imported
10363 declarations seem to be static in all other languages, make them all CU-wide
10364 global only in Ada. */
10365
10366 static struct using_direct **
10367 using_directives (struct dwarf2_cu *cu)
10368 {
10369 if (cu->language == language_ada
10370 && cu->get_builder ()->outermost_context_p ())
10371 return cu->get_builder ()->get_global_using_directives ();
10372 else
10373 return cu->get_builder ()->get_local_using_directives ();
10374 }
10375
10376 /* Read the import statement specified by the given die and record it. */
10377
10378 static void
10379 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10380 {
10381 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10382 struct attribute *import_attr;
10383 struct die_info *imported_die, *child_die;
10384 struct dwarf2_cu *imported_cu;
10385 const char *imported_name;
10386 const char *imported_name_prefix;
10387 const char *canonical_name;
10388 const char *import_alias;
10389 const char *imported_declaration = NULL;
10390 const char *import_prefix;
10391 std::vector<const char *> excludes;
10392
10393 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10394 if (import_attr == NULL)
10395 {
10396 complaint (_("Tag '%s' has no DW_AT_import"),
10397 dwarf_tag_name (die->tag));
10398 return;
10399 }
10400
10401 imported_cu = cu;
10402 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10403 imported_name = dwarf2_name (imported_die, imported_cu);
10404 if (imported_name == NULL)
10405 {
10406 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10407
10408 The import in the following code:
10409 namespace A
10410 {
10411 typedef int B;
10412 }
10413
10414 int main ()
10415 {
10416 using A::B;
10417 B b;
10418 return b;
10419 }
10420
10421 ...
10422 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10423 <52> DW_AT_decl_file : 1
10424 <53> DW_AT_decl_line : 6
10425 <54> DW_AT_import : <0x75>
10426 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10427 <59> DW_AT_name : B
10428 <5b> DW_AT_decl_file : 1
10429 <5c> DW_AT_decl_line : 2
10430 <5d> DW_AT_type : <0x6e>
10431 ...
10432 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10433 <76> DW_AT_byte_size : 4
10434 <77> DW_AT_encoding : 5 (signed)
10435
10436 imports the wrong die ( 0x75 instead of 0x58 ).
10437 This case will be ignored until the gcc bug is fixed. */
10438 return;
10439 }
10440
10441 /* Figure out the local name after import. */
10442 import_alias = dwarf2_name (die, cu);
10443
10444 /* Figure out where the statement is being imported to. */
10445 import_prefix = determine_prefix (die, cu);
10446
10447 /* Figure out what the scope of the imported die is and prepend it
10448 to the name of the imported die. */
10449 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10450
10451 if (imported_die->tag != DW_TAG_namespace
10452 && imported_die->tag != DW_TAG_module)
10453 {
10454 imported_declaration = imported_name;
10455 canonical_name = imported_name_prefix;
10456 }
10457 else if (strlen (imported_name_prefix) > 0)
10458 canonical_name = obconcat (&objfile->objfile_obstack,
10459 imported_name_prefix,
10460 (cu->language == language_d ? "." : "::"),
10461 imported_name, (char *) NULL);
10462 else
10463 canonical_name = imported_name;
10464
10465 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10466 for (child_die = die->child; child_die && child_die->tag;
10467 child_die = sibling_die (child_die))
10468 {
10469 /* DWARF-4: A Fortran use statement with a “rename list” may be
10470 represented by an imported module entry with an import attribute
10471 referring to the module and owned entries corresponding to those
10472 entities that are renamed as part of being imported. */
10473
10474 if (child_die->tag != DW_TAG_imported_declaration)
10475 {
10476 complaint (_("child DW_TAG_imported_declaration expected "
10477 "- DIE at %s [in module %s]"),
10478 sect_offset_str (child_die->sect_off),
10479 objfile_name (objfile));
10480 continue;
10481 }
10482
10483 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10484 if (import_attr == NULL)
10485 {
10486 complaint (_("Tag '%s' has no DW_AT_import"),
10487 dwarf_tag_name (child_die->tag));
10488 continue;
10489 }
10490
10491 imported_cu = cu;
10492 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10493 &imported_cu);
10494 imported_name = dwarf2_name (imported_die, imported_cu);
10495 if (imported_name == NULL)
10496 {
10497 complaint (_("child DW_TAG_imported_declaration has unknown "
10498 "imported name - DIE at %s [in module %s]"),
10499 sect_offset_str (child_die->sect_off),
10500 objfile_name (objfile));
10501 continue;
10502 }
10503
10504 excludes.push_back (imported_name);
10505
10506 process_die (child_die, cu);
10507 }
10508
10509 add_using_directive (using_directives (cu),
10510 import_prefix,
10511 canonical_name,
10512 import_alias,
10513 imported_declaration,
10514 excludes,
10515 0,
10516 &objfile->objfile_obstack);
10517 }
10518
10519 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10520 types, but gives them a size of zero. Starting with version 14,
10521 ICC is compatible with GCC. */
10522
10523 static bool
10524 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10525 {
10526 if (!cu->checked_producer)
10527 check_producer (cu);
10528
10529 return cu->producer_is_icc_lt_14;
10530 }
10531
10532 /* ICC generates a DW_AT_type for C void functions. This was observed on
10533 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10534 which says that void functions should not have a DW_AT_type. */
10535
10536 static bool
10537 producer_is_icc (struct dwarf2_cu *cu)
10538 {
10539 if (!cu->checked_producer)
10540 check_producer (cu);
10541
10542 return cu->producer_is_icc;
10543 }
10544
10545 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10546 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10547 this, it was first present in GCC release 4.3.0. */
10548
10549 static bool
10550 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10551 {
10552 if (!cu->checked_producer)
10553 check_producer (cu);
10554
10555 return cu->producer_is_gcc_lt_4_3;
10556 }
10557
10558 static file_and_directory
10559 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10560 {
10561 file_and_directory res;
10562
10563 /* Find the filename. Do not use dwarf2_name here, since the filename
10564 is not a source language identifier. */
10565 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10566 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10567
10568 if (res.comp_dir == NULL
10569 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10570 && IS_ABSOLUTE_PATH (res.name))
10571 {
10572 res.comp_dir_storage = ldirname (res.name);
10573 if (!res.comp_dir_storage.empty ())
10574 res.comp_dir = res.comp_dir_storage.c_str ();
10575 }
10576 if (res.comp_dir != NULL)
10577 {
10578 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10579 directory, get rid of it. */
10580 const char *cp = strchr (res.comp_dir, ':');
10581
10582 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10583 res.comp_dir = cp + 1;
10584 }
10585
10586 if (res.name == NULL)
10587 res.name = "<unknown>";
10588
10589 return res;
10590 }
10591
10592 /* Handle DW_AT_stmt_list for a compilation unit.
10593 DIE is the DW_TAG_compile_unit die for CU.
10594 COMP_DIR is the compilation directory. LOWPC is passed to
10595 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10596
10597 static void
10598 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10599 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10600 {
10601 struct dwarf2_per_objfile *dwarf2_per_objfile
10602 = cu->per_cu->dwarf2_per_objfile;
10603 struct attribute *attr;
10604 struct line_header line_header_local;
10605 hashval_t line_header_local_hash;
10606 void **slot;
10607 int decode_mapping;
10608
10609 gdb_assert (! cu->per_cu->is_debug_types);
10610
10611 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10612 if (attr == NULL)
10613 return;
10614
10615 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10616
10617 /* The line header hash table is only created if needed (it exists to
10618 prevent redundant reading of the line table for partial_units).
10619 If we're given a partial_unit, we'll need it. If we're given a
10620 compile_unit, then use the line header hash table if it's already
10621 created, but don't create one just yet. */
10622
10623 if (dwarf2_per_objfile->line_header_hash == NULL
10624 && die->tag == DW_TAG_partial_unit)
10625 {
10626 dwarf2_per_objfile->line_header_hash
10627 .reset (htab_create_alloc (127, line_header_hash_voidp,
10628 line_header_eq_voidp,
10629 free_line_header_voidp,
10630 xcalloc, xfree));
10631 }
10632
10633 line_header_local.sect_off = line_offset;
10634 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10635 line_header_local_hash = line_header_hash (&line_header_local);
10636 if (dwarf2_per_objfile->line_header_hash != NULL)
10637 {
10638 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10639 &line_header_local,
10640 line_header_local_hash, NO_INSERT);
10641
10642 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10643 is not present in *SLOT (since if there is something in *SLOT then
10644 it will be for a partial_unit). */
10645 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10646 {
10647 gdb_assert (*slot != NULL);
10648 cu->line_header = (struct line_header *) *slot;
10649 return;
10650 }
10651 }
10652
10653 /* dwarf_decode_line_header does not yet provide sufficient information.
10654 We always have to call also dwarf_decode_lines for it. */
10655 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10656 if (lh == NULL)
10657 return;
10658
10659 cu->line_header = lh.release ();
10660 cu->line_header_die_owner = die;
10661
10662 if (dwarf2_per_objfile->line_header_hash == NULL)
10663 slot = NULL;
10664 else
10665 {
10666 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10667 &line_header_local,
10668 line_header_local_hash, INSERT);
10669 gdb_assert (slot != NULL);
10670 }
10671 if (slot != NULL && *slot == NULL)
10672 {
10673 /* This newly decoded line number information unit will be owned
10674 by line_header_hash hash table. */
10675 *slot = cu->line_header;
10676 cu->line_header_die_owner = NULL;
10677 }
10678 else
10679 {
10680 /* We cannot free any current entry in (*slot) as that struct line_header
10681 may be already used by multiple CUs. Create only temporary decoded
10682 line_header for this CU - it may happen at most once for each line
10683 number information unit. And if we're not using line_header_hash
10684 then this is what we want as well. */
10685 gdb_assert (die->tag != DW_TAG_partial_unit);
10686 }
10687 decode_mapping = (die->tag != DW_TAG_partial_unit);
10688 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10689 decode_mapping);
10690
10691 }
10692
10693 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10694
10695 static void
10696 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10697 {
10698 struct dwarf2_per_objfile *dwarf2_per_objfile
10699 = cu->per_cu->dwarf2_per_objfile;
10700 struct objfile *objfile = dwarf2_per_objfile->objfile;
10701 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10702 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10703 CORE_ADDR highpc = ((CORE_ADDR) 0);
10704 struct attribute *attr;
10705 struct die_info *child_die;
10706 CORE_ADDR baseaddr;
10707
10708 prepare_one_comp_unit (cu, die, cu->language);
10709 baseaddr = objfile->text_section_offset ();
10710
10711 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10712
10713 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10714 from finish_block. */
10715 if (lowpc == ((CORE_ADDR) -1))
10716 lowpc = highpc;
10717 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10718
10719 file_and_directory fnd = find_file_and_directory (die, cu);
10720
10721 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10722 standardised yet. As a workaround for the language detection we fall
10723 back to the DW_AT_producer string. */
10724 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10725 cu->language = language_opencl;
10726
10727 /* Similar hack for Go. */
10728 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10729 set_cu_language (DW_LANG_Go, cu);
10730
10731 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10732
10733 /* Decode line number information if present. We do this before
10734 processing child DIEs, so that the line header table is available
10735 for DW_AT_decl_file. */
10736 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10737
10738 /* Process all dies in compilation unit. */
10739 if (die->child != NULL)
10740 {
10741 child_die = die->child;
10742 while (child_die && child_die->tag)
10743 {
10744 process_die (child_die, cu);
10745 child_die = sibling_die (child_die);
10746 }
10747 }
10748
10749 /* Decode macro information, if present. Dwarf 2 macro information
10750 refers to information in the line number info statement program
10751 header, so we can only read it if we've read the header
10752 successfully. */
10753 attr = dwarf2_attr (die, DW_AT_macros, cu);
10754 if (attr == NULL)
10755 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10756 if (attr && cu->line_header)
10757 {
10758 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10759 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10760
10761 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10762 }
10763 else
10764 {
10765 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10766 if (attr && cu->line_header)
10767 {
10768 unsigned int macro_offset = DW_UNSND (attr);
10769
10770 dwarf_decode_macros (cu, macro_offset, 0);
10771 }
10772 }
10773 }
10774
10775 void
10776 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10777 {
10778 struct type_unit_group *tu_group;
10779 int first_time;
10780 struct attribute *attr;
10781 unsigned int i;
10782 struct signatured_type *sig_type;
10783
10784 gdb_assert (per_cu->is_debug_types);
10785 sig_type = (struct signatured_type *) per_cu;
10786
10787 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10788
10789 /* If we're using .gdb_index (includes -readnow) then
10790 per_cu->type_unit_group may not have been set up yet. */
10791 if (sig_type->type_unit_group == NULL)
10792 sig_type->type_unit_group = get_type_unit_group (this, attr);
10793 tu_group = sig_type->type_unit_group;
10794
10795 /* If we've already processed this stmt_list there's no real need to
10796 do it again, we could fake it and just recreate the part we need
10797 (file name,index -> symtab mapping). If data shows this optimization
10798 is useful we can do it then. */
10799 first_time = tu_group->compunit_symtab == NULL;
10800
10801 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10802 debug info. */
10803 line_header_up lh;
10804 if (attr != NULL)
10805 {
10806 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10807 lh = dwarf_decode_line_header (line_offset, this);
10808 }
10809 if (lh == NULL)
10810 {
10811 if (first_time)
10812 start_symtab ("", NULL, 0);
10813 else
10814 {
10815 gdb_assert (tu_group->symtabs == NULL);
10816 gdb_assert (m_builder == nullptr);
10817 struct compunit_symtab *cust = tu_group->compunit_symtab;
10818 m_builder.reset (new struct buildsym_compunit
10819 (COMPUNIT_OBJFILE (cust), "",
10820 COMPUNIT_DIRNAME (cust),
10821 compunit_language (cust),
10822 0, cust));
10823 }
10824 return;
10825 }
10826
10827 line_header = lh.release ();
10828 line_header_die_owner = die;
10829
10830 if (first_time)
10831 {
10832 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10833
10834 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10835 still initializing it, and our caller (a few levels up)
10836 process_full_type_unit still needs to know if this is the first
10837 time. */
10838
10839 tu_group->symtabs
10840 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10841 struct symtab *, line_header->file_names_size ());
10842
10843 auto &file_names = line_header->file_names ();
10844 for (i = 0; i < file_names.size (); ++i)
10845 {
10846 file_entry &fe = file_names[i];
10847 dwarf2_start_subfile (this, fe.name,
10848 fe.include_dir (line_header));
10849 buildsym_compunit *b = get_builder ();
10850 if (b->get_current_subfile ()->symtab == NULL)
10851 {
10852 /* NOTE: start_subfile will recognize when it's been
10853 passed a file it has already seen. So we can't
10854 assume there's a simple mapping from
10855 cu->line_header->file_names to subfiles, plus
10856 cu->line_header->file_names may contain dups. */
10857 b->get_current_subfile ()->symtab
10858 = allocate_symtab (cust, b->get_current_subfile ()->name);
10859 }
10860
10861 fe.symtab = b->get_current_subfile ()->symtab;
10862 tu_group->symtabs[i] = fe.symtab;
10863 }
10864 }
10865 else
10866 {
10867 gdb_assert (m_builder == nullptr);
10868 struct compunit_symtab *cust = tu_group->compunit_symtab;
10869 m_builder.reset (new struct buildsym_compunit
10870 (COMPUNIT_OBJFILE (cust), "",
10871 COMPUNIT_DIRNAME (cust),
10872 compunit_language (cust),
10873 0, cust));
10874
10875 auto &file_names = line_header->file_names ();
10876 for (i = 0; i < file_names.size (); ++i)
10877 {
10878 file_entry &fe = file_names[i];
10879 fe.symtab = tu_group->symtabs[i];
10880 }
10881 }
10882
10883 /* The main symtab is allocated last. Type units don't have DW_AT_name
10884 so they don't have a "real" (so to speak) symtab anyway.
10885 There is later code that will assign the main symtab to all symbols
10886 that don't have one. We need to handle the case of a symbol with a
10887 missing symtab (DW_AT_decl_file) anyway. */
10888 }
10889
10890 /* Process DW_TAG_type_unit.
10891 For TUs we want to skip the first top level sibling if it's not the
10892 actual type being defined by this TU. In this case the first top
10893 level sibling is there to provide context only. */
10894
10895 static void
10896 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10897 {
10898 struct die_info *child_die;
10899
10900 prepare_one_comp_unit (cu, die, language_minimal);
10901
10902 /* Initialize (or reinitialize) the machinery for building symtabs.
10903 We do this before processing child DIEs, so that the line header table
10904 is available for DW_AT_decl_file. */
10905 cu->setup_type_unit_groups (die);
10906
10907 if (die->child != NULL)
10908 {
10909 child_die = die->child;
10910 while (child_die && child_die->tag)
10911 {
10912 process_die (child_die, cu);
10913 child_die = sibling_die (child_die);
10914 }
10915 }
10916 }
10917 \f
10918 /* DWO/DWP files.
10919
10920 http://gcc.gnu.org/wiki/DebugFission
10921 http://gcc.gnu.org/wiki/DebugFissionDWP
10922
10923 To simplify handling of both DWO files ("object" files with the DWARF info)
10924 and DWP files (a file with the DWOs packaged up into one file), we treat
10925 DWP files as having a collection of virtual DWO files. */
10926
10927 static hashval_t
10928 hash_dwo_file (const void *item)
10929 {
10930 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10931 hashval_t hash;
10932
10933 hash = htab_hash_string (dwo_file->dwo_name);
10934 if (dwo_file->comp_dir != NULL)
10935 hash += htab_hash_string (dwo_file->comp_dir);
10936 return hash;
10937 }
10938
10939 static int
10940 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10941 {
10942 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10943 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10944
10945 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10946 return 0;
10947 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10948 return lhs->comp_dir == rhs->comp_dir;
10949 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10950 }
10951
10952 /* Allocate a hash table for DWO files. */
10953
10954 static htab_up
10955 allocate_dwo_file_hash_table ()
10956 {
10957 auto delete_dwo_file = [] (void *item)
10958 {
10959 struct dwo_file *dwo_file = (struct dwo_file *) item;
10960
10961 delete dwo_file;
10962 };
10963
10964 return htab_up (htab_create_alloc (41,
10965 hash_dwo_file,
10966 eq_dwo_file,
10967 delete_dwo_file,
10968 xcalloc, xfree));
10969 }
10970
10971 /* Lookup DWO file DWO_NAME. */
10972
10973 static void **
10974 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10975 const char *dwo_name,
10976 const char *comp_dir)
10977 {
10978 struct dwo_file find_entry;
10979 void **slot;
10980
10981 if (dwarf2_per_objfile->dwo_files == NULL)
10982 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10983
10984 find_entry.dwo_name = dwo_name;
10985 find_entry.comp_dir = comp_dir;
10986 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
10987 INSERT);
10988
10989 return slot;
10990 }
10991
10992 static hashval_t
10993 hash_dwo_unit (const void *item)
10994 {
10995 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10996
10997 /* This drops the top 32 bits of the id, but is ok for a hash. */
10998 return dwo_unit->signature;
10999 }
11000
11001 static int
11002 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11003 {
11004 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11005 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11006
11007 /* The signature is assumed to be unique within the DWO file.
11008 So while object file CU dwo_id's always have the value zero,
11009 that's OK, assuming each object file DWO file has only one CU,
11010 and that's the rule for now. */
11011 return lhs->signature == rhs->signature;
11012 }
11013
11014 /* Allocate a hash table for DWO CUs,TUs.
11015 There is one of these tables for each of CUs,TUs for each DWO file. */
11016
11017 static htab_up
11018 allocate_dwo_unit_table ()
11019 {
11020 /* Start out with a pretty small number.
11021 Generally DWO files contain only one CU and maybe some TUs. */
11022 return htab_up (htab_create_alloc (3,
11023 hash_dwo_unit,
11024 eq_dwo_unit,
11025 NULL, xcalloc, xfree));
11026 }
11027
11028 /* die_reader_func for create_dwo_cu. */
11029
11030 static void
11031 create_dwo_cu_reader (const struct die_reader_specs *reader,
11032 const gdb_byte *info_ptr,
11033 struct die_info *comp_unit_die,
11034 struct dwo_file *dwo_file,
11035 struct dwo_unit *dwo_unit)
11036 {
11037 struct dwarf2_cu *cu = reader->cu;
11038 sect_offset sect_off = cu->per_cu->sect_off;
11039 struct dwarf2_section_info *section = cu->per_cu->section;
11040
11041 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11042 if (!signature.has_value ())
11043 {
11044 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11045 " its dwo_id [in module %s]"),
11046 sect_offset_str (sect_off), dwo_file->dwo_name);
11047 return;
11048 }
11049
11050 dwo_unit->dwo_file = dwo_file;
11051 dwo_unit->signature = *signature;
11052 dwo_unit->section = section;
11053 dwo_unit->sect_off = sect_off;
11054 dwo_unit->length = cu->per_cu->length;
11055
11056 if (dwarf_read_debug)
11057 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11058 sect_offset_str (sect_off),
11059 hex_string (dwo_unit->signature));
11060 }
11061
11062 /* Create the dwo_units for the CUs in a DWO_FILE.
11063 Note: This function processes DWO files only, not DWP files. */
11064
11065 static void
11066 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11067 dwarf2_cu *cu, struct dwo_file &dwo_file,
11068 dwarf2_section_info &section, htab_up &cus_htab)
11069 {
11070 struct objfile *objfile = dwarf2_per_objfile->objfile;
11071 const gdb_byte *info_ptr, *end_ptr;
11072
11073 section.read (objfile);
11074 info_ptr = section.buffer;
11075
11076 if (info_ptr == NULL)
11077 return;
11078
11079 if (dwarf_read_debug)
11080 {
11081 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11082 section.get_name (),
11083 section.get_file_name ());
11084 }
11085
11086 end_ptr = info_ptr + section.size;
11087 while (info_ptr < end_ptr)
11088 {
11089 struct dwarf2_per_cu_data per_cu;
11090 struct dwo_unit read_unit {};
11091 struct dwo_unit *dwo_unit;
11092 void **slot;
11093 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11094
11095 memset (&per_cu, 0, sizeof (per_cu));
11096 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11097 per_cu.is_debug_types = 0;
11098 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11099 per_cu.section = &section;
11100
11101 cutu_reader reader (&per_cu, cu, &dwo_file);
11102 if (!reader.dummy_p)
11103 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11104 &dwo_file, &read_unit);
11105 info_ptr += per_cu.length;
11106
11107 // If the unit could not be parsed, skip it.
11108 if (read_unit.dwo_file == NULL)
11109 continue;
11110
11111 if (cus_htab == NULL)
11112 cus_htab = allocate_dwo_unit_table ();
11113
11114 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11115 *dwo_unit = read_unit;
11116 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11117 gdb_assert (slot != NULL);
11118 if (*slot != NULL)
11119 {
11120 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11121 sect_offset dup_sect_off = dup_cu->sect_off;
11122
11123 complaint (_("debug cu entry at offset %s is duplicate to"
11124 " the entry at offset %s, signature %s"),
11125 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11126 hex_string (dwo_unit->signature));
11127 }
11128 *slot = (void *)dwo_unit;
11129 }
11130 }
11131
11132 /* DWP file .debug_{cu,tu}_index section format:
11133 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11134
11135 DWP Version 1:
11136
11137 Both index sections have the same format, and serve to map a 64-bit
11138 signature to a set of section numbers. Each section begins with a header,
11139 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11140 indexes, and a pool of 32-bit section numbers. The index sections will be
11141 aligned at 8-byte boundaries in the file.
11142
11143 The index section header consists of:
11144
11145 V, 32 bit version number
11146 -, 32 bits unused
11147 N, 32 bit number of compilation units or type units in the index
11148 M, 32 bit number of slots in the hash table
11149
11150 Numbers are recorded using the byte order of the application binary.
11151
11152 The hash table begins at offset 16 in the section, and consists of an array
11153 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11154 order of the application binary). Unused slots in the hash table are 0.
11155 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11156
11157 The parallel table begins immediately after the hash table
11158 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11159 array of 32-bit indexes (using the byte order of the application binary),
11160 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11161 table contains a 32-bit index into the pool of section numbers. For unused
11162 hash table slots, the corresponding entry in the parallel table will be 0.
11163
11164 The pool of section numbers begins immediately following the hash table
11165 (at offset 16 + 12 * M from the beginning of the section). The pool of
11166 section numbers consists of an array of 32-bit words (using the byte order
11167 of the application binary). Each item in the array is indexed starting
11168 from 0. The hash table entry provides the index of the first section
11169 number in the set. Additional section numbers in the set follow, and the
11170 set is terminated by a 0 entry (section number 0 is not used in ELF).
11171
11172 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11173 section must be the first entry in the set, and the .debug_abbrev.dwo must
11174 be the second entry. Other members of the set may follow in any order.
11175
11176 ---
11177
11178 DWP Version 2:
11179
11180 DWP Version 2 combines all the .debug_info, etc. sections into one,
11181 and the entries in the index tables are now offsets into these sections.
11182 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11183 section.
11184
11185 Index Section Contents:
11186 Header
11187 Hash Table of Signatures dwp_hash_table.hash_table
11188 Parallel Table of Indices dwp_hash_table.unit_table
11189 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11190 Table of Section Sizes dwp_hash_table.v2.sizes
11191
11192 The index section header consists of:
11193
11194 V, 32 bit version number
11195 L, 32 bit number of columns in the table of section offsets
11196 N, 32 bit number of compilation units or type units in the index
11197 M, 32 bit number of slots in the hash table
11198
11199 Numbers are recorded using the byte order of the application binary.
11200
11201 The hash table has the same format as version 1.
11202 The parallel table of indices has the same format as version 1,
11203 except that the entries are origin-1 indices into the table of sections
11204 offsets and the table of section sizes.
11205
11206 The table of offsets begins immediately following the parallel table
11207 (at offset 16 + 12 * M from the beginning of the section). The table is
11208 a two-dimensional array of 32-bit words (using the byte order of the
11209 application binary), with L columns and N+1 rows, in row-major order.
11210 Each row in the array is indexed starting from 0. The first row provides
11211 a key to the remaining rows: each column in this row provides an identifier
11212 for a debug section, and the offsets in the same column of subsequent rows
11213 refer to that section. The section identifiers are:
11214
11215 DW_SECT_INFO 1 .debug_info.dwo
11216 DW_SECT_TYPES 2 .debug_types.dwo
11217 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11218 DW_SECT_LINE 4 .debug_line.dwo
11219 DW_SECT_LOC 5 .debug_loc.dwo
11220 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11221 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11222 DW_SECT_MACRO 8 .debug_macro.dwo
11223
11224 The offsets provided by the CU and TU index sections are the base offsets
11225 for the contributions made by each CU or TU to the corresponding section
11226 in the package file. Each CU and TU header contains an abbrev_offset
11227 field, used to find the abbreviations table for that CU or TU within the
11228 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11229 be interpreted as relative to the base offset given in the index section.
11230 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11231 should be interpreted as relative to the base offset for .debug_line.dwo,
11232 and offsets into other debug sections obtained from DWARF attributes should
11233 also be interpreted as relative to the corresponding base offset.
11234
11235 The table of sizes begins immediately following the table of offsets.
11236 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11237 with L columns and N rows, in row-major order. Each row in the array is
11238 indexed starting from 1 (row 0 is shared by the two tables).
11239
11240 ---
11241
11242 Hash table lookup is handled the same in version 1 and 2:
11243
11244 We assume that N and M will not exceed 2^32 - 1.
11245 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11246
11247 Given a 64-bit compilation unit signature or a type signature S, an entry
11248 in the hash table is located as follows:
11249
11250 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11251 the low-order k bits all set to 1.
11252
11253 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11254
11255 3) If the hash table entry at index H matches the signature, use that
11256 entry. If the hash table entry at index H is unused (all zeroes),
11257 terminate the search: the signature is not present in the table.
11258
11259 4) Let H = (H + H') modulo M. Repeat at Step 3.
11260
11261 Because M > N and H' and M are relatively prime, the search is guaranteed
11262 to stop at an unused slot or find the match. */
11263
11264 /* Create a hash table to map DWO IDs to their CU/TU entry in
11265 .debug_{info,types}.dwo in DWP_FILE.
11266 Returns NULL if there isn't one.
11267 Note: This function processes DWP files only, not DWO files. */
11268
11269 static struct dwp_hash_table *
11270 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11271 struct dwp_file *dwp_file, int is_debug_types)
11272 {
11273 struct objfile *objfile = dwarf2_per_objfile->objfile;
11274 bfd *dbfd = dwp_file->dbfd.get ();
11275 const gdb_byte *index_ptr, *index_end;
11276 struct dwarf2_section_info *index;
11277 uint32_t version, nr_columns, nr_units, nr_slots;
11278 struct dwp_hash_table *htab;
11279
11280 if (is_debug_types)
11281 index = &dwp_file->sections.tu_index;
11282 else
11283 index = &dwp_file->sections.cu_index;
11284
11285 if (index->empty ())
11286 return NULL;
11287 index->read (objfile);
11288
11289 index_ptr = index->buffer;
11290 index_end = index_ptr + index->size;
11291
11292 version = read_4_bytes (dbfd, index_ptr);
11293 index_ptr += 4;
11294 if (version == 2)
11295 nr_columns = read_4_bytes (dbfd, index_ptr);
11296 else
11297 nr_columns = 0;
11298 index_ptr += 4;
11299 nr_units = read_4_bytes (dbfd, index_ptr);
11300 index_ptr += 4;
11301 nr_slots = read_4_bytes (dbfd, index_ptr);
11302 index_ptr += 4;
11303
11304 if (version != 1 && version != 2)
11305 {
11306 error (_("Dwarf Error: unsupported DWP file version (%s)"
11307 " [in module %s]"),
11308 pulongest (version), dwp_file->name);
11309 }
11310 if (nr_slots != (nr_slots & -nr_slots))
11311 {
11312 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11313 " is not power of 2 [in module %s]"),
11314 pulongest (nr_slots), dwp_file->name);
11315 }
11316
11317 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11318 htab->version = version;
11319 htab->nr_columns = nr_columns;
11320 htab->nr_units = nr_units;
11321 htab->nr_slots = nr_slots;
11322 htab->hash_table = index_ptr;
11323 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11324
11325 /* Exit early if the table is empty. */
11326 if (nr_slots == 0 || nr_units == 0
11327 || (version == 2 && nr_columns == 0))
11328 {
11329 /* All must be zero. */
11330 if (nr_slots != 0 || nr_units != 0
11331 || (version == 2 && nr_columns != 0))
11332 {
11333 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11334 " all zero [in modules %s]"),
11335 dwp_file->name);
11336 }
11337 return htab;
11338 }
11339
11340 if (version == 1)
11341 {
11342 htab->section_pool.v1.indices =
11343 htab->unit_table + sizeof (uint32_t) * nr_slots;
11344 /* It's harder to decide whether the section is too small in v1.
11345 V1 is deprecated anyway so we punt. */
11346 }
11347 else
11348 {
11349 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11350 int *ids = htab->section_pool.v2.section_ids;
11351 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11352 /* Reverse map for error checking. */
11353 int ids_seen[DW_SECT_MAX + 1];
11354 int i;
11355
11356 if (nr_columns < 2)
11357 {
11358 error (_("Dwarf Error: bad DWP hash table, too few columns"
11359 " in section table [in module %s]"),
11360 dwp_file->name);
11361 }
11362 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11363 {
11364 error (_("Dwarf Error: bad DWP hash table, too many columns"
11365 " in section table [in module %s]"),
11366 dwp_file->name);
11367 }
11368 memset (ids, 255, sizeof_ids);
11369 memset (ids_seen, 255, sizeof (ids_seen));
11370 for (i = 0; i < nr_columns; ++i)
11371 {
11372 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11373
11374 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11375 {
11376 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11377 " in section table [in module %s]"),
11378 id, dwp_file->name);
11379 }
11380 if (ids_seen[id] != -1)
11381 {
11382 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11383 " id %d in section table [in module %s]"),
11384 id, dwp_file->name);
11385 }
11386 ids_seen[id] = i;
11387 ids[i] = id;
11388 }
11389 /* Must have exactly one info or types section. */
11390 if (((ids_seen[DW_SECT_INFO] != -1)
11391 + (ids_seen[DW_SECT_TYPES] != -1))
11392 != 1)
11393 {
11394 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11395 " DWO info/types section [in module %s]"),
11396 dwp_file->name);
11397 }
11398 /* Must have an abbrev section. */
11399 if (ids_seen[DW_SECT_ABBREV] == -1)
11400 {
11401 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11402 " section [in module %s]"),
11403 dwp_file->name);
11404 }
11405 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11406 htab->section_pool.v2.sizes =
11407 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11408 * nr_units * nr_columns);
11409 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11410 * nr_units * nr_columns))
11411 > index_end)
11412 {
11413 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11414 " [in module %s]"),
11415 dwp_file->name);
11416 }
11417 }
11418
11419 return htab;
11420 }
11421
11422 /* Update SECTIONS with the data from SECTP.
11423
11424 This function is like the other "locate" section routines that are
11425 passed to bfd_map_over_sections, but in this context the sections to
11426 read comes from the DWP V1 hash table, not the full ELF section table.
11427
11428 The result is non-zero for success, or zero if an error was found. */
11429
11430 static int
11431 locate_v1_virtual_dwo_sections (asection *sectp,
11432 struct virtual_v1_dwo_sections *sections)
11433 {
11434 const struct dwop_section_names *names = &dwop_section_names;
11435
11436 if (section_is_p (sectp->name, &names->abbrev_dwo))
11437 {
11438 /* There can be only one. */
11439 if (sections->abbrev.s.section != NULL)
11440 return 0;
11441 sections->abbrev.s.section = sectp;
11442 sections->abbrev.size = bfd_section_size (sectp);
11443 }
11444 else if (section_is_p (sectp->name, &names->info_dwo)
11445 || section_is_p (sectp->name, &names->types_dwo))
11446 {
11447 /* There can be only one. */
11448 if (sections->info_or_types.s.section != NULL)
11449 return 0;
11450 sections->info_or_types.s.section = sectp;
11451 sections->info_or_types.size = bfd_section_size (sectp);
11452 }
11453 else if (section_is_p (sectp->name, &names->line_dwo))
11454 {
11455 /* There can be only one. */
11456 if (sections->line.s.section != NULL)
11457 return 0;
11458 sections->line.s.section = sectp;
11459 sections->line.size = bfd_section_size (sectp);
11460 }
11461 else if (section_is_p (sectp->name, &names->loc_dwo))
11462 {
11463 /* There can be only one. */
11464 if (sections->loc.s.section != NULL)
11465 return 0;
11466 sections->loc.s.section = sectp;
11467 sections->loc.size = bfd_section_size (sectp);
11468 }
11469 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11470 {
11471 /* There can be only one. */
11472 if (sections->macinfo.s.section != NULL)
11473 return 0;
11474 sections->macinfo.s.section = sectp;
11475 sections->macinfo.size = bfd_section_size (sectp);
11476 }
11477 else if (section_is_p (sectp->name, &names->macro_dwo))
11478 {
11479 /* There can be only one. */
11480 if (sections->macro.s.section != NULL)
11481 return 0;
11482 sections->macro.s.section = sectp;
11483 sections->macro.size = bfd_section_size (sectp);
11484 }
11485 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11486 {
11487 /* There can be only one. */
11488 if (sections->str_offsets.s.section != NULL)
11489 return 0;
11490 sections->str_offsets.s.section = sectp;
11491 sections->str_offsets.size = bfd_section_size (sectp);
11492 }
11493 else
11494 {
11495 /* No other kind of section is valid. */
11496 return 0;
11497 }
11498
11499 return 1;
11500 }
11501
11502 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11503 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11504 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11505 This is for DWP version 1 files. */
11506
11507 static struct dwo_unit *
11508 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11509 struct dwp_file *dwp_file,
11510 uint32_t unit_index,
11511 const char *comp_dir,
11512 ULONGEST signature, int is_debug_types)
11513 {
11514 struct objfile *objfile = dwarf2_per_objfile->objfile;
11515 const struct dwp_hash_table *dwp_htab =
11516 is_debug_types ? dwp_file->tus : dwp_file->cus;
11517 bfd *dbfd = dwp_file->dbfd.get ();
11518 const char *kind = is_debug_types ? "TU" : "CU";
11519 struct dwo_file *dwo_file;
11520 struct dwo_unit *dwo_unit;
11521 struct virtual_v1_dwo_sections sections;
11522 void **dwo_file_slot;
11523 int i;
11524
11525 gdb_assert (dwp_file->version == 1);
11526
11527 if (dwarf_read_debug)
11528 {
11529 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11530 kind,
11531 pulongest (unit_index), hex_string (signature),
11532 dwp_file->name);
11533 }
11534
11535 /* Fetch the sections of this DWO unit.
11536 Put a limit on the number of sections we look for so that bad data
11537 doesn't cause us to loop forever. */
11538
11539 #define MAX_NR_V1_DWO_SECTIONS \
11540 (1 /* .debug_info or .debug_types */ \
11541 + 1 /* .debug_abbrev */ \
11542 + 1 /* .debug_line */ \
11543 + 1 /* .debug_loc */ \
11544 + 1 /* .debug_str_offsets */ \
11545 + 1 /* .debug_macro or .debug_macinfo */ \
11546 + 1 /* trailing zero */)
11547
11548 memset (&sections, 0, sizeof (sections));
11549
11550 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11551 {
11552 asection *sectp;
11553 uint32_t section_nr =
11554 read_4_bytes (dbfd,
11555 dwp_htab->section_pool.v1.indices
11556 + (unit_index + i) * sizeof (uint32_t));
11557
11558 if (section_nr == 0)
11559 break;
11560 if (section_nr >= dwp_file->num_sections)
11561 {
11562 error (_("Dwarf Error: bad DWP hash table, section number too large"
11563 " [in module %s]"),
11564 dwp_file->name);
11565 }
11566
11567 sectp = dwp_file->elf_sections[section_nr];
11568 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11569 {
11570 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11571 " [in module %s]"),
11572 dwp_file->name);
11573 }
11574 }
11575
11576 if (i < 2
11577 || sections.info_or_types.empty ()
11578 || sections.abbrev.empty ())
11579 {
11580 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11581 " [in module %s]"),
11582 dwp_file->name);
11583 }
11584 if (i == MAX_NR_V1_DWO_SECTIONS)
11585 {
11586 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11587 " [in module %s]"),
11588 dwp_file->name);
11589 }
11590
11591 /* It's easier for the rest of the code if we fake a struct dwo_file and
11592 have dwo_unit "live" in that. At least for now.
11593
11594 The DWP file can be made up of a random collection of CUs and TUs.
11595 However, for each CU + set of TUs that came from the same original DWO
11596 file, we can combine them back into a virtual DWO file to save space
11597 (fewer struct dwo_file objects to allocate). Remember that for really
11598 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11599
11600 std::string virtual_dwo_name =
11601 string_printf ("virtual-dwo/%d-%d-%d-%d",
11602 sections.abbrev.get_id (),
11603 sections.line.get_id (),
11604 sections.loc.get_id (),
11605 sections.str_offsets.get_id ());
11606 /* Can we use an existing virtual DWO file? */
11607 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11608 virtual_dwo_name.c_str (),
11609 comp_dir);
11610 /* Create one if necessary. */
11611 if (*dwo_file_slot == NULL)
11612 {
11613 if (dwarf_read_debug)
11614 {
11615 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11616 virtual_dwo_name.c_str ());
11617 }
11618 dwo_file = new struct dwo_file;
11619 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11620 dwo_file->comp_dir = comp_dir;
11621 dwo_file->sections.abbrev = sections.abbrev;
11622 dwo_file->sections.line = sections.line;
11623 dwo_file->sections.loc = sections.loc;
11624 dwo_file->sections.macinfo = sections.macinfo;
11625 dwo_file->sections.macro = sections.macro;
11626 dwo_file->sections.str_offsets = sections.str_offsets;
11627 /* The "str" section is global to the entire DWP file. */
11628 dwo_file->sections.str = dwp_file->sections.str;
11629 /* The info or types section is assigned below to dwo_unit,
11630 there's no need to record it in dwo_file.
11631 Also, we can't simply record type sections in dwo_file because
11632 we record a pointer into the vector in dwo_unit. As we collect more
11633 types we'll grow the vector and eventually have to reallocate space
11634 for it, invalidating all copies of pointers into the previous
11635 contents. */
11636 *dwo_file_slot = dwo_file;
11637 }
11638 else
11639 {
11640 if (dwarf_read_debug)
11641 {
11642 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11643 virtual_dwo_name.c_str ());
11644 }
11645 dwo_file = (struct dwo_file *) *dwo_file_slot;
11646 }
11647
11648 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11649 dwo_unit->dwo_file = dwo_file;
11650 dwo_unit->signature = signature;
11651 dwo_unit->section =
11652 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11653 *dwo_unit->section = sections.info_or_types;
11654 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11655
11656 return dwo_unit;
11657 }
11658
11659 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11660 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11661 piece within that section used by a TU/CU, return a virtual section
11662 of just that piece. */
11663
11664 static struct dwarf2_section_info
11665 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11666 struct dwarf2_section_info *section,
11667 bfd_size_type offset, bfd_size_type size)
11668 {
11669 struct dwarf2_section_info result;
11670 asection *sectp;
11671
11672 gdb_assert (section != NULL);
11673 gdb_assert (!section->is_virtual);
11674
11675 memset (&result, 0, sizeof (result));
11676 result.s.containing_section = section;
11677 result.is_virtual = true;
11678
11679 if (size == 0)
11680 return result;
11681
11682 sectp = section->get_bfd_section ();
11683
11684 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11685 bounds of the real section. This is a pretty-rare event, so just
11686 flag an error (easier) instead of a warning and trying to cope. */
11687 if (sectp == NULL
11688 || offset + size > bfd_section_size (sectp))
11689 {
11690 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11691 " in section %s [in module %s]"),
11692 sectp ? bfd_section_name (sectp) : "<unknown>",
11693 objfile_name (dwarf2_per_objfile->objfile));
11694 }
11695
11696 result.virtual_offset = offset;
11697 result.size = size;
11698 return result;
11699 }
11700
11701 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11702 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11703 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11704 This is for DWP version 2 files. */
11705
11706 static struct dwo_unit *
11707 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11708 struct dwp_file *dwp_file,
11709 uint32_t unit_index,
11710 const char *comp_dir,
11711 ULONGEST signature, int is_debug_types)
11712 {
11713 struct objfile *objfile = dwarf2_per_objfile->objfile;
11714 const struct dwp_hash_table *dwp_htab =
11715 is_debug_types ? dwp_file->tus : dwp_file->cus;
11716 bfd *dbfd = dwp_file->dbfd.get ();
11717 const char *kind = is_debug_types ? "TU" : "CU";
11718 struct dwo_file *dwo_file;
11719 struct dwo_unit *dwo_unit;
11720 struct virtual_v2_dwo_sections sections;
11721 void **dwo_file_slot;
11722 int i;
11723
11724 gdb_assert (dwp_file->version == 2);
11725
11726 if (dwarf_read_debug)
11727 {
11728 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11729 kind,
11730 pulongest (unit_index), hex_string (signature),
11731 dwp_file->name);
11732 }
11733
11734 /* Fetch the section offsets of this DWO unit. */
11735
11736 memset (&sections, 0, sizeof (sections));
11737
11738 for (i = 0; i < dwp_htab->nr_columns; ++i)
11739 {
11740 uint32_t offset = read_4_bytes (dbfd,
11741 dwp_htab->section_pool.v2.offsets
11742 + (((unit_index - 1) * dwp_htab->nr_columns
11743 + i)
11744 * sizeof (uint32_t)));
11745 uint32_t size = read_4_bytes (dbfd,
11746 dwp_htab->section_pool.v2.sizes
11747 + (((unit_index - 1) * dwp_htab->nr_columns
11748 + i)
11749 * sizeof (uint32_t)));
11750
11751 switch (dwp_htab->section_pool.v2.section_ids[i])
11752 {
11753 case DW_SECT_INFO:
11754 case DW_SECT_TYPES:
11755 sections.info_or_types_offset = offset;
11756 sections.info_or_types_size = size;
11757 break;
11758 case DW_SECT_ABBREV:
11759 sections.abbrev_offset = offset;
11760 sections.abbrev_size = size;
11761 break;
11762 case DW_SECT_LINE:
11763 sections.line_offset = offset;
11764 sections.line_size = size;
11765 break;
11766 case DW_SECT_LOC:
11767 sections.loc_offset = offset;
11768 sections.loc_size = size;
11769 break;
11770 case DW_SECT_STR_OFFSETS:
11771 sections.str_offsets_offset = offset;
11772 sections.str_offsets_size = size;
11773 break;
11774 case DW_SECT_MACINFO:
11775 sections.macinfo_offset = offset;
11776 sections.macinfo_size = size;
11777 break;
11778 case DW_SECT_MACRO:
11779 sections.macro_offset = offset;
11780 sections.macro_size = size;
11781 break;
11782 }
11783 }
11784
11785 /* It's easier for the rest of the code if we fake a struct dwo_file and
11786 have dwo_unit "live" in that. At least for now.
11787
11788 The DWP file can be made up of a random collection of CUs and TUs.
11789 However, for each CU + set of TUs that came from the same original DWO
11790 file, we can combine them back into a virtual DWO file to save space
11791 (fewer struct dwo_file objects to allocate). Remember that for really
11792 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11793
11794 std::string virtual_dwo_name =
11795 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11796 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11797 (long) (sections.line_size ? sections.line_offset : 0),
11798 (long) (sections.loc_size ? sections.loc_offset : 0),
11799 (long) (sections.str_offsets_size
11800 ? sections.str_offsets_offset : 0));
11801 /* Can we use an existing virtual DWO file? */
11802 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11803 virtual_dwo_name.c_str (),
11804 comp_dir);
11805 /* Create one if necessary. */
11806 if (*dwo_file_slot == NULL)
11807 {
11808 if (dwarf_read_debug)
11809 {
11810 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11811 virtual_dwo_name.c_str ());
11812 }
11813 dwo_file = new struct dwo_file;
11814 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11815 dwo_file->comp_dir = comp_dir;
11816 dwo_file->sections.abbrev =
11817 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11818 sections.abbrev_offset, sections.abbrev_size);
11819 dwo_file->sections.line =
11820 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11821 sections.line_offset, sections.line_size);
11822 dwo_file->sections.loc =
11823 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11824 sections.loc_offset, sections.loc_size);
11825 dwo_file->sections.macinfo =
11826 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11827 sections.macinfo_offset, sections.macinfo_size);
11828 dwo_file->sections.macro =
11829 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11830 sections.macro_offset, sections.macro_size);
11831 dwo_file->sections.str_offsets =
11832 create_dwp_v2_section (dwarf2_per_objfile,
11833 &dwp_file->sections.str_offsets,
11834 sections.str_offsets_offset,
11835 sections.str_offsets_size);
11836 /* The "str" section is global to the entire DWP file. */
11837 dwo_file->sections.str = dwp_file->sections.str;
11838 /* The info or types section is assigned below to dwo_unit,
11839 there's no need to record it in dwo_file.
11840 Also, we can't simply record type sections in dwo_file because
11841 we record a pointer into the vector in dwo_unit. As we collect more
11842 types we'll grow the vector and eventually have to reallocate space
11843 for it, invalidating all copies of pointers into the previous
11844 contents. */
11845 *dwo_file_slot = dwo_file;
11846 }
11847 else
11848 {
11849 if (dwarf_read_debug)
11850 {
11851 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11852 virtual_dwo_name.c_str ());
11853 }
11854 dwo_file = (struct dwo_file *) *dwo_file_slot;
11855 }
11856
11857 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11858 dwo_unit->dwo_file = dwo_file;
11859 dwo_unit->signature = signature;
11860 dwo_unit->section =
11861 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11862 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11863 is_debug_types
11864 ? &dwp_file->sections.types
11865 : &dwp_file->sections.info,
11866 sections.info_or_types_offset,
11867 sections.info_or_types_size);
11868 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11869
11870 return dwo_unit;
11871 }
11872
11873 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11874 Returns NULL if the signature isn't found. */
11875
11876 static struct dwo_unit *
11877 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11878 struct dwp_file *dwp_file, const char *comp_dir,
11879 ULONGEST signature, int is_debug_types)
11880 {
11881 const struct dwp_hash_table *dwp_htab =
11882 is_debug_types ? dwp_file->tus : dwp_file->cus;
11883 bfd *dbfd = dwp_file->dbfd.get ();
11884 uint32_t mask = dwp_htab->nr_slots - 1;
11885 uint32_t hash = signature & mask;
11886 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11887 unsigned int i;
11888 void **slot;
11889 struct dwo_unit find_dwo_cu;
11890
11891 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11892 find_dwo_cu.signature = signature;
11893 slot = htab_find_slot (is_debug_types
11894 ? dwp_file->loaded_tus.get ()
11895 : dwp_file->loaded_cus.get (),
11896 &find_dwo_cu, INSERT);
11897
11898 if (*slot != NULL)
11899 return (struct dwo_unit *) *slot;
11900
11901 /* Use a for loop so that we don't loop forever on bad debug info. */
11902 for (i = 0; i < dwp_htab->nr_slots; ++i)
11903 {
11904 ULONGEST signature_in_table;
11905
11906 signature_in_table =
11907 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11908 if (signature_in_table == signature)
11909 {
11910 uint32_t unit_index =
11911 read_4_bytes (dbfd,
11912 dwp_htab->unit_table + hash * sizeof (uint32_t));
11913
11914 if (dwp_file->version == 1)
11915 {
11916 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11917 dwp_file, unit_index,
11918 comp_dir, signature,
11919 is_debug_types);
11920 }
11921 else
11922 {
11923 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11924 dwp_file, unit_index,
11925 comp_dir, signature,
11926 is_debug_types);
11927 }
11928 return (struct dwo_unit *) *slot;
11929 }
11930 if (signature_in_table == 0)
11931 return NULL;
11932 hash = (hash + hash2) & mask;
11933 }
11934
11935 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11936 " [in module %s]"),
11937 dwp_file->name);
11938 }
11939
11940 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11941 Open the file specified by FILE_NAME and hand it off to BFD for
11942 preliminary analysis. Return a newly initialized bfd *, which
11943 includes a canonicalized copy of FILE_NAME.
11944 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11945 SEARCH_CWD is true if the current directory is to be searched.
11946 It will be searched before debug-file-directory.
11947 If successful, the file is added to the bfd include table of the
11948 objfile's bfd (see gdb_bfd_record_inclusion).
11949 If unable to find/open the file, return NULL.
11950 NOTE: This function is derived from symfile_bfd_open. */
11951
11952 static gdb_bfd_ref_ptr
11953 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11954 const char *file_name, int is_dwp, int search_cwd)
11955 {
11956 int desc;
11957 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11958 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11959 to debug_file_directory. */
11960 const char *search_path;
11961 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11962
11963 gdb::unique_xmalloc_ptr<char> search_path_holder;
11964 if (search_cwd)
11965 {
11966 if (*debug_file_directory != '\0')
11967 {
11968 search_path_holder.reset (concat (".", dirname_separator_string,
11969 debug_file_directory,
11970 (char *) NULL));
11971 search_path = search_path_holder.get ();
11972 }
11973 else
11974 search_path = ".";
11975 }
11976 else
11977 search_path = debug_file_directory;
11978
11979 openp_flags flags = OPF_RETURN_REALPATH;
11980 if (is_dwp)
11981 flags |= OPF_SEARCH_IN_PATH;
11982
11983 gdb::unique_xmalloc_ptr<char> absolute_name;
11984 desc = openp (search_path, flags, file_name,
11985 O_RDONLY | O_BINARY, &absolute_name);
11986 if (desc < 0)
11987 return NULL;
11988
11989 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11990 gnutarget, desc));
11991 if (sym_bfd == NULL)
11992 return NULL;
11993 bfd_set_cacheable (sym_bfd.get (), 1);
11994
11995 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11996 return NULL;
11997
11998 /* Success. Record the bfd as having been included by the objfile's bfd.
11999 This is important because things like demangled_names_hash lives in the
12000 objfile's per_bfd space and may have references to things like symbol
12001 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12002 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12003
12004 return sym_bfd;
12005 }
12006
12007 /* Try to open DWO file FILE_NAME.
12008 COMP_DIR is the DW_AT_comp_dir attribute.
12009 The result is the bfd handle of the file.
12010 If there is a problem finding or opening the file, return NULL.
12011 Upon success, the canonicalized path of the file is stored in the bfd,
12012 same as symfile_bfd_open. */
12013
12014 static gdb_bfd_ref_ptr
12015 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12016 const char *file_name, const char *comp_dir)
12017 {
12018 if (IS_ABSOLUTE_PATH (file_name))
12019 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12020 0 /*is_dwp*/, 0 /*search_cwd*/);
12021
12022 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12023
12024 if (comp_dir != NULL)
12025 {
12026 gdb::unique_xmalloc_ptr<char> path_to_try
12027 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12028
12029 /* NOTE: If comp_dir is a relative path, this will also try the
12030 search path, which seems useful. */
12031 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12032 path_to_try.get (),
12033 0 /*is_dwp*/,
12034 1 /*search_cwd*/));
12035 if (abfd != NULL)
12036 return abfd;
12037 }
12038
12039 /* That didn't work, try debug-file-directory, which, despite its name,
12040 is a list of paths. */
12041
12042 if (*debug_file_directory == '\0')
12043 return NULL;
12044
12045 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12046 0 /*is_dwp*/, 1 /*search_cwd*/);
12047 }
12048
12049 /* This function is mapped across the sections and remembers the offset and
12050 size of each of the DWO debugging sections we are interested in. */
12051
12052 static void
12053 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12054 {
12055 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12056 const struct dwop_section_names *names = &dwop_section_names;
12057
12058 if (section_is_p (sectp->name, &names->abbrev_dwo))
12059 {
12060 dwo_sections->abbrev.s.section = sectp;
12061 dwo_sections->abbrev.size = bfd_section_size (sectp);
12062 }
12063 else if (section_is_p (sectp->name, &names->info_dwo))
12064 {
12065 dwo_sections->info.s.section = sectp;
12066 dwo_sections->info.size = bfd_section_size (sectp);
12067 }
12068 else if (section_is_p (sectp->name, &names->line_dwo))
12069 {
12070 dwo_sections->line.s.section = sectp;
12071 dwo_sections->line.size = bfd_section_size (sectp);
12072 }
12073 else if (section_is_p (sectp->name, &names->loc_dwo))
12074 {
12075 dwo_sections->loc.s.section = sectp;
12076 dwo_sections->loc.size = bfd_section_size (sectp);
12077 }
12078 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12079 {
12080 dwo_sections->macinfo.s.section = sectp;
12081 dwo_sections->macinfo.size = bfd_section_size (sectp);
12082 }
12083 else if (section_is_p (sectp->name, &names->macro_dwo))
12084 {
12085 dwo_sections->macro.s.section = sectp;
12086 dwo_sections->macro.size = bfd_section_size (sectp);
12087 }
12088 else if (section_is_p (sectp->name, &names->str_dwo))
12089 {
12090 dwo_sections->str.s.section = sectp;
12091 dwo_sections->str.size = bfd_section_size (sectp);
12092 }
12093 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12094 {
12095 dwo_sections->str_offsets.s.section = sectp;
12096 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12097 }
12098 else if (section_is_p (sectp->name, &names->types_dwo))
12099 {
12100 struct dwarf2_section_info type_section;
12101
12102 memset (&type_section, 0, sizeof (type_section));
12103 type_section.s.section = sectp;
12104 type_section.size = bfd_section_size (sectp);
12105 dwo_sections->types.push_back (type_section);
12106 }
12107 }
12108
12109 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12110 by PER_CU. This is for the non-DWP case.
12111 The result is NULL if DWO_NAME can't be found. */
12112
12113 static struct dwo_file *
12114 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12115 const char *dwo_name, const char *comp_dir)
12116 {
12117 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12118
12119 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12120 if (dbfd == NULL)
12121 {
12122 if (dwarf_read_debug)
12123 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12124 return NULL;
12125 }
12126
12127 dwo_file_up dwo_file (new struct dwo_file);
12128 dwo_file->dwo_name = dwo_name;
12129 dwo_file->comp_dir = comp_dir;
12130 dwo_file->dbfd = std::move (dbfd);
12131
12132 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12133 &dwo_file->sections);
12134
12135 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12136 dwo_file->sections.info, dwo_file->cus);
12137
12138 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12139 dwo_file->sections.types, dwo_file->tus);
12140
12141 if (dwarf_read_debug)
12142 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12143
12144 return dwo_file.release ();
12145 }
12146
12147 /* This function is mapped across the sections and remembers the offset and
12148 size of each of the DWP debugging sections common to version 1 and 2 that
12149 we are interested in. */
12150
12151 static void
12152 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12153 void *dwp_file_ptr)
12154 {
12155 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12156 const struct dwop_section_names *names = &dwop_section_names;
12157 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12158
12159 /* Record the ELF section number for later lookup: this is what the
12160 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12161 gdb_assert (elf_section_nr < dwp_file->num_sections);
12162 dwp_file->elf_sections[elf_section_nr] = sectp;
12163
12164 /* Look for specific sections that we need. */
12165 if (section_is_p (sectp->name, &names->str_dwo))
12166 {
12167 dwp_file->sections.str.s.section = sectp;
12168 dwp_file->sections.str.size = bfd_section_size (sectp);
12169 }
12170 else if (section_is_p (sectp->name, &names->cu_index))
12171 {
12172 dwp_file->sections.cu_index.s.section = sectp;
12173 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12174 }
12175 else if (section_is_p (sectp->name, &names->tu_index))
12176 {
12177 dwp_file->sections.tu_index.s.section = sectp;
12178 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12179 }
12180 }
12181
12182 /* This function is mapped across the sections and remembers the offset and
12183 size of each of the DWP version 2 debugging sections that we are interested
12184 in. This is split into a separate function because we don't know if we
12185 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12186
12187 static void
12188 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12189 {
12190 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12191 const struct dwop_section_names *names = &dwop_section_names;
12192 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12193
12194 /* Record the ELF section number for later lookup: this is what the
12195 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12196 gdb_assert (elf_section_nr < dwp_file->num_sections);
12197 dwp_file->elf_sections[elf_section_nr] = sectp;
12198
12199 /* Look for specific sections that we need. */
12200 if (section_is_p (sectp->name, &names->abbrev_dwo))
12201 {
12202 dwp_file->sections.abbrev.s.section = sectp;
12203 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12204 }
12205 else if (section_is_p (sectp->name, &names->info_dwo))
12206 {
12207 dwp_file->sections.info.s.section = sectp;
12208 dwp_file->sections.info.size = bfd_section_size (sectp);
12209 }
12210 else if (section_is_p (sectp->name, &names->line_dwo))
12211 {
12212 dwp_file->sections.line.s.section = sectp;
12213 dwp_file->sections.line.size = bfd_section_size (sectp);
12214 }
12215 else if (section_is_p (sectp->name, &names->loc_dwo))
12216 {
12217 dwp_file->sections.loc.s.section = sectp;
12218 dwp_file->sections.loc.size = bfd_section_size (sectp);
12219 }
12220 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12221 {
12222 dwp_file->sections.macinfo.s.section = sectp;
12223 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12224 }
12225 else if (section_is_p (sectp->name, &names->macro_dwo))
12226 {
12227 dwp_file->sections.macro.s.section = sectp;
12228 dwp_file->sections.macro.size = bfd_section_size (sectp);
12229 }
12230 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12231 {
12232 dwp_file->sections.str_offsets.s.section = sectp;
12233 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12234 }
12235 else if (section_is_p (sectp->name, &names->types_dwo))
12236 {
12237 dwp_file->sections.types.s.section = sectp;
12238 dwp_file->sections.types.size = bfd_section_size (sectp);
12239 }
12240 }
12241
12242 /* Hash function for dwp_file loaded CUs/TUs. */
12243
12244 static hashval_t
12245 hash_dwp_loaded_cutus (const void *item)
12246 {
12247 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12248
12249 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12250 return dwo_unit->signature;
12251 }
12252
12253 /* Equality function for dwp_file loaded CUs/TUs. */
12254
12255 static int
12256 eq_dwp_loaded_cutus (const void *a, const void *b)
12257 {
12258 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12259 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12260
12261 return dua->signature == dub->signature;
12262 }
12263
12264 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12265
12266 static htab_up
12267 allocate_dwp_loaded_cutus_table ()
12268 {
12269 return htab_up (htab_create_alloc (3,
12270 hash_dwp_loaded_cutus,
12271 eq_dwp_loaded_cutus,
12272 NULL, xcalloc, xfree));
12273 }
12274
12275 /* Try to open DWP file FILE_NAME.
12276 The result is the bfd handle of the file.
12277 If there is a problem finding or opening the file, return NULL.
12278 Upon success, the canonicalized path of the file is stored in the bfd,
12279 same as symfile_bfd_open. */
12280
12281 static gdb_bfd_ref_ptr
12282 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12283 const char *file_name)
12284 {
12285 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12286 1 /*is_dwp*/,
12287 1 /*search_cwd*/));
12288 if (abfd != NULL)
12289 return abfd;
12290
12291 /* Work around upstream bug 15652.
12292 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12293 [Whether that's a "bug" is debatable, but it is getting in our way.]
12294 We have no real idea where the dwp file is, because gdb's realpath-ing
12295 of the executable's path may have discarded the needed info.
12296 [IWBN if the dwp file name was recorded in the executable, akin to
12297 .gnu_debuglink, but that doesn't exist yet.]
12298 Strip the directory from FILE_NAME and search again. */
12299 if (*debug_file_directory != '\0')
12300 {
12301 /* Don't implicitly search the current directory here.
12302 If the user wants to search "." to handle this case,
12303 it must be added to debug-file-directory. */
12304 return try_open_dwop_file (dwarf2_per_objfile,
12305 lbasename (file_name), 1 /*is_dwp*/,
12306 0 /*search_cwd*/);
12307 }
12308
12309 return NULL;
12310 }
12311
12312 /* Initialize the use of the DWP file for the current objfile.
12313 By convention the name of the DWP file is ${objfile}.dwp.
12314 The result is NULL if it can't be found. */
12315
12316 static std::unique_ptr<struct dwp_file>
12317 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12318 {
12319 struct objfile *objfile = dwarf2_per_objfile->objfile;
12320
12321 /* Try to find first .dwp for the binary file before any symbolic links
12322 resolving. */
12323
12324 /* If the objfile is a debug file, find the name of the real binary
12325 file and get the name of dwp file from there. */
12326 std::string dwp_name;
12327 if (objfile->separate_debug_objfile_backlink != NULL)
12328 {
12329 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12330 const char *backlink_basename = lbasename (backlink->original_name);
12331
12332 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12333 }
12334 else
12335 dwp_name = objfile->original_name;
12336
12337 dwp_name += ".dwp";
12338
12339 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12340 if (dbfd == NULL
12341 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12342 {
12343 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12344 dwp_name = objfile_name (objfile);
12345 dwp_name += ".dwp";
12346 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12347 }
12348
12349 if (dbfd == NULL)
12350 {
12351 if (dwarf_read_debug)
12352 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12353 return std::unique_ptr<dwp_file> ();
12354 }
12355
12356 const char *name = bfd_get_filename (dbfd.get ());
12357 std::unique_ptr<struct dwp_file> dwp_file
12358 (new struct dwp_file (name, std::move (dbfd)));
12359
12360 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12361 dwp_file->elf_sections =
12362 OBSTACK_CALLOC (&objfile->objfile_obstack,
12363 dwp_file->num_sections, asection *);
12364
12365 bfd_map_over_sections (dwp_file->dbfd.get (),
12366 dwarf2_locate_common_dwp_sections,
12367 dwp_file.get ());
12368
12369 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12370 0);
12371
12372 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12373 1);
12374
12375 /* The DWP file version is stored in the hash table. Oh well. */
12376 if (dwp_file->cus && dwp_file->tus
12377 && dwp_file->cus->version != dwp_file->tus->version)
12378 {
12379 /* Technically speaking, we should try to limp along, but this is
12380 pretty bizarre. We use pulongest here because that's the established
12381 portability solution (e.g, we cannot use %u for uint32_t). */
12382 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12383 " TU version %s [in DWP file %s]"),
12384 pulongest (dwp_file->cus->version),
12385 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12386 }
12387
12388 if (dwp_file->cus)
12389 dwp_file->version = dwp_file->cus->version;
12390 else if (dwp_file->tus)
12391 dwp_file->version = dwp_file->tus->version;
12392 else
12393 dwp_file->version = 2;
12394
12395 if (dwp_file->version == 2)
12396 bfd_map_over_sections (dwp_file->dbfd.get (),
12397 dwarf2_locate_v2_dwp_sections,
12398 dwp_file.get ());
12399
12400 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12401 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12402
12403 if (dwarf_read_debug)
12404 {
12405 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12406 fprintf_unfiltered (gdb_stdlog,
12407 " %s CUs, %s TUs\n",
12408 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12409 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12410 }
12411
12412 return dwp_file;
12413 }
12414
12415 /* Wrapper around open_and_init_dwp_file, only open it once. */
12416
12417 static struct dwp_file *
12418 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12419 {
12420 if (! dwarf2_per_objfile->dwp_checked)
12421 {
12422 dwarf2_per_objfile->dwp_file
12423 = open_and_init_dwp_file (dwarf2_per_objfile);
12424 dwarf2_per_objfile->dwp_checked = 1;
12425 }
12426 return dwarf2_per_objfile->dwp_file.get ();
12427 }
12428
12429 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12430 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12431 or in the DWP file for the objfile, referenced by THIS_UNIT.
12432 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12433 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12434
12435 This is called, for example, when wanting to read a variable with a
12436 complex location. Therefore we don't want to do file i/o for every call.
12437 Therefore we don't want to look for a DWO file on every call.
12438 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12439 then we check if we've already seen DWO_NAME, and only THEN do we check
12440 for a DWO file.
12441
12442 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12443 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12444
12445 static struct dwo_unit *
12446 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12447 const char *dwo_name, const char *comp_dir,
12448 ULONGEST signature, int is_debug_types)
12449 {
12450 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12451 struct objfile *objfile = dwarf2_per_objfile->objfile;
12452 const char *kind = is_debug_types ? "TU" : "CU";
12453 void **dwo_file_slot;
12454 struct dwo_file *dwo_file;
12455 struct dwp_file *dwp_file;
12456
12457 /* First see if there's a DWP file.
12458 If we have a DWP file but didn't find the DWO inside it, don't
12459 look for the original DWO file. It makes gdb behave differently
12460 depending on whether one is debugging in the build tree. */
12461
12462 dwp_file = get_dwp_file (dwarf2_per_objfile);
12463 if (dwp_file != NULL)
12464 {
12465 const struct dwp_hash_table *dwp_htab =
12466 is_debug_types ? dwp_file->tus : dwp_file->cus;
12467
12468 if (dwp_htab != NULL)
12469 {
12470 struct dwo_unit *dwo_cutu =
12471 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12472 signature, is_debug_types);
12473
12474 if (dwo_cutu != NULL)
12475 {
12476 if (dwarf_read_debug)
12477 {
12478 fprintf_unfiltered (gdb_stdlog,
12479 "Virtual DWO %s %s found: @%s\n",
12480 kind, hex_string (signature),
12481 host_address_to_string (dwo_cutu));
12482 }
12483 return dwo_cutu;
12484 }
12485 }
12486 }
12487 else
12488 {
12489 /* No DWP file, look for the DWO file. */
12490
12491 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12492 dwo_name, comp_dir);
12493 if (*dwo_file_slot == NULL)
12494 {
12495 /* Read in the file and build a table of the CUs/TUs it contains. */
12496 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12497 }
12498 /* NOTE: This will be NULL if unable to open the file. */
12499 dwo_file = (struct dwo_file *) *dwo_file_slot;
12500
12501 if (dwo_file != NULL)
12502 {
12503 struct dwo_unit *dwo_cutu = NULL;
12504
12505 if (is_debug_types && dwo_file->tus)
12506 {
12507 struct dwo_unit find_dwo_cutu;
12508
12509 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12510 find_dwo_cutu.signature = signature;
12511 dwo_cutu
12512 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12513 &find_dwo_cutu);
12514 }
12515 else if (!is_debug_types && dwo_file->cus)
12516 {
12517 struct dwo_unit find_dwo_cutu;
12518
12519 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12520 find_dwo_cutu.signature = signature;
12521 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12522 &find_dwo_cutu);
12523 }
12524
12525 if (dwo_cutu != NULL)
12526 {
12527 if (dwarf_read_debug)
12528 {
12529 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12530 kind, dwo_name, hex_string (signature),
12531 host_address_to_string (dwo_cutu));
12532 }
12533 return dwo_cutu;
12534 }
12535 }
12536 }
12537
12538 /* We didn't find it. This could mean a dwo_id mismatch, or
12539 someone deleted the DWO/DWP file, or the search path isn't set up
12540 correctly to find the file. */
12541
12542 if (dwarf_read_debug)
12543 {
12544 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12545 kind, dwo_name, hex_string (signature));
12546 }
12547
12548 /* This is a warning and not a complaint because it can be caused by
12549 pilot error (e.g., user accidentally deleting the DWO). */
12550 {
12551 /* Print the name of the DWP file if we looked there, helps the user
12552 better diagnose the problem. */
12553 std::string dwp_text;
12554
12555 if (dwp_file != NULL)
12556 dwp_text = string_printf (" [in DWP file %s]",
12557 lbasename (dwp_file->name));
12558
12559 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12560 " [in module %s]"),
12561 kind, dwo_name, hex_string (signature),
12562 dwp_text.c_str (),
12563 this_unit->is_debug_types ? "TU" : "CU",
12564 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12565 }
12566 return NULL;
12567 }
12568
12569 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12570 See lookup_dwo_cutu_unit for details. */
12571
12572 static struct dwo_unit *
12573 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12574 const char *dwo_name, const char *comp_dir,
12575 ULONGEST signature)
12576 {
12577 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12578 }
12579
12580 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12581 See lookup_dwo_cutu_unit for details. */
12582
12583 static struct dwo_unit *
12584 lookup_dwo_type_unit (struct signatured_type *this_tu,
12585 const char *dwo_name, const char *comp_dir)
12586 {
12587 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12588 }
12589
12590 /* Traversal function for queue_and_load_all_dwo_tus. */
12591
12592 static int
12593 queue_and_load_dwo_tu (void **slot, void *info)
12594 {
12595 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12596 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12597 ULONGEST signature = dwo_unit->signature;
12598 struct signatured_type *sig_type =
12599 lookup_dwo_signatured_type (per_cu->cu, signature);
12600
12601 if (sig_type != NULL)
12602 {
12603 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12604
12605 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12606 a real dependency of PER_CU on SIG_TYPE. That is detected later
12607 while processing PER_CU. */
12608 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12609 load_full_type_unit (sig_cu);
12610 per_cu->imported_symtabs_push (sig_cu);
12611 }
12612
12613 return 1;
12614 }
12615
12616 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12617 The DWO may have the only definition of the type, though it may not be
12618 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12619 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12620
12621 static void
12622 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12623 {
12624 struct dwo_unit *dwo_unit;
12625 struct dwo_file *dwo_file;
12626
12627 gdb_assert (!per_cu->is_debug_types);
12628 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12629 gdb_assert (per_cu->cu != NULL);
12630
12631 dwo_unit = per_cu->cu->dwo_unit;
12632 gdb_assert (dwo_unit != NULL);
12633
12634 dwo_file = dwo_unit->dwo_file;
12635 if (dwo_file->tus != NULL)
12636 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12637 per_cu);
12638 }
12639
12640 /* Read in various DIEs. */
12641
12642 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12643 Inherit only the children of the DW_AT_abstract_origin DIE not being
12644 already referenced by DW_AT_abstract_origin from the children of the
12645 current DIE. */
12646
12647 static void
12648 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12649 {
12650 struct die_info *child_die;
12651 sect_offset *offsetp;
12652 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12653 struct die_info *origin_die;
12654 /* Iterator of the ORIGIN_DIE children. */
12655 struct die_info *origin_child_die;
12656 struct attribute *attr;
12657 struct dwarf2_cu *origin_cu;
12658 struct pending **origin_previous_list_in_scope;
12659
12660 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12661 if (!attr)
12662 return;
12663
12664 /* Note that following die references may follow to a die in a
12665 different cu. */
12666
12667 origin_cu = cu;
12668 origin_die = follow_die_ref (die, attr, &origin_cu);
12669
12670 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12671 symbols in. */
12672 origin_previous_list_in_scope = origin_cu->list_in_scope;
12673 origin_cu->list_in_scope = cu->list_in_scope;
12674
12675 if (die->tag != origin_die->tag
12676 && !(die->tag == DW_TAG_inlined_subroutine
12677 && origin_die->tag == DW_TAG_subprogram))
12678 complaint (_("DIE %s and its abstract origin %s have different tags"),
12679 sect_offset_str (die->sect_off),
12680 sect_offset_str (origin_die->sect_off));
12681
12682 std::vector<sect_offset> offsets;
12683
12684 for (child_die = die->child;
12685 child_die && child_die->tag;
12686 child_die = sibling_die (child_die))
12687 {
12688 struct die_info *child_origin_die;
12689 struct dwarf2_cu *child_origin_cu;
12690
12691 /* We are trying to process concrete instance entries:
12692 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12693 it's not relevant to our analysis here. i.e. detecting DIEs that are
12694 present in the abstract instance but not referenced in the concrete
12695 one. */
12696 if (child_die->tag == DW_TAG_call_site
12697 || child_die->tag == DW_TAG_GNU_call_site)
12698 continue;
12699
12700 /* For each CHILD_DIE, find the corresponding child of
12701 ORIGIN_DIE. If there is more than one layer of
12702 DW_AT_abstract_origin, follow them all; there shouldn't be,
12703 but GCC versions at least through 4.4 generate this (GCC PR
12704 40573). */
12705 child_origin_die = child_die;
12706 child_origin_cu = cu;
12707 while (1)
12708 {
12709 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12710 child_origin_cu);
12711 if (attr == NULL)
12712 break;
12713 child_origin_die = follow_die_ref (child_origin_die, attr,
12714 &child_origin_cu);
12715 }
12716
12717 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12718 counterpart may exist. */
12719 if (child_origin_die != child_die)
12720 {
12721 if (child_die->tag != child_origin_die->tag
12722 && !(child_die->tag == DW_TAG_inlined_subroutine
12723 && child_origin_die->tag == DW_TAG_subprogram))
12724 complaint (_("Child DIE %s and its abstract origin %s have "
12725 "different tags"),
12726 sect_offset_str (child_die->sect_off),
12727 sect_offset_str (child_origin_die->sect_off));
12728 if (child_origin_die->parent != origin_die)
12729 complaint (_("Child DIE %s and its abstract origin %s have "
12730 "different parents"),
12731 sect_offset_str (child_die->sect_off),
12732 sect_offset_str (child_origin_die->sect_off));
12733 else
12734 offsets.push_back (child_origin_die->sect_off);
12735 }
12736 }
12737 std::sort (offsets.begin (), offsets.end ());
12738 sect_offset *offsets_end = offsets.data () + offsets.size ();
12739 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12740 if (offsetp[-1] == *offsetp)
12741 complaint (_("Multiple children of DIE %s refer "
12742 "to DIE %s as their abstract origin"),
12743 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12744
12745 offsetp = offsets.data ();
12746 origin_child_die = origin_die->child;
12747 while (origin_child_die && origin_child_die->tag)
12748 {
12749 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12750 while (offsetp < offsets_end
12751 && *offsetp < origin_child_die->sect_off)
12752 offsetp++;
12753 if (offsetp >= offsets_end
12754 || *offsetp > origin_child_die->sect_off)
12755 {
12756 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12757 Check whether we're already processing ORIGIN_CHILD_DIE.
12758 This can happen with mutually referenced abstract_origins.
12759 PR 16581. */
12760 if (!origin_child_die->in_process)
12761 process_die (origin_child_die, origin_cu);
12762 }
12763 origin_child_die = sibling_die (origin_child_die);
12764 }
12765 origin_cu->list_in_scope = origin_previous_list_in_scope;
12766
12767 if (cu != origin_cu)
12768 compute_delayed_physnames (origin_cu);
12769 }
12770
12771 static void
12772 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12773 {
12774 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12775 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12776 struct context_stack *newobj;
12777 CORE_ADDR lowpc;
12778 CORE_ADDR highpc;
12779 struct die_info *child_die;
12780 struct attribute *attr, *call_line, *call_file;
12781 const char *name;
12782 CORE_ADDR baseaddr;
12783 struct block *block;
12784 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12785 std::vector<struct symbol *> template_args;
12786 struct template_symbol *templ_func = NULL;
12787
12788 if (inlined_func)
12789 {
12790 /* If we do not have call site information, we can't show the
12791 caller of this inlined function. That's too confusing, so
12792 only use the scope for local variables. */
12793 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12794 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12795 if (call_line == NULL || call_file == NULL)
12796 {
12797 read_lexical_block_scope (die, cu);
12798 return;
12799 }
12800 }
12801
12802 baseaddr = objfile->text_section_offset ();
12803
12804 name = dwarf2_name (die, cu);
12805
12806 /* Ignore functions with missing or empty names. These are actually
12807 illegal according to the DWARF standard. */
12808 if (name == NULL)
12809 {
12810 complaint (_("missing name for subprogram DIE at %s"),
12811 sect_offset_str (die->sect_off));
12812 return;
12813 }
12814
12815 /* Ignore functions with missing or invalid low and high pc attributes. */
12816 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12817 <= PC_BOUNDS_INVALID)
12818 {
12819 attr = dwarf2_attr (die, DW_AT_external, cu);
12820 if (!attr || !DW_UNSND (attr))
12821 complaint (_("cannot get low and high bounds "
12822 "for subprogram DIE at %s"),
12823 sect_offset_str (die->sect_off));
12824 return;
12825 }
12826
12827 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12828 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12829
12830 /* If we have any template arguments, then we must allocate a
12831 different sort of symbol. */
12832 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12833 {
12834 if (child_die->tag == DW_TAG_template_type_param
12835 || child_die->tag == DW_TAG_template_value_param)
12836 {
12837 templ_func = allocate_template_symbol (objfile);
12838 templ_func->subclass = SYMBOL_TEMPLATE;
12839 break;
12840 }
12841 }
12842
12843 newobj = cu->get_builder ()->push_context (0, lowpc);
12844 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12845 (struct symbol *) templ_func);
12846
12847 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12848 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12849 cu->language);
12850
12851 /* If there is a location expression for DW_AT_frame_base, record
12852 it. */
12853 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12854 if (attr != nullptr)
12855 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12856
12857 /* If there is a location for the static link, record it. */
12858 newobj->static_link = NULL;
12859 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12860 if (attr != nullptr)
12861 {
12862 newobj->static_link
12863 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12864 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12865 cu->per_cu->addr_type ());
12866 }
12867
12868 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12869
12870 if (die->child != NULL)
12871 {
12872 child_die = die->child;
12873 while (child_die && child_die->tag)
12874 {
12875 if (child_die->tag == DW_TAG_template_type_param
12876 || child_die->tag == DW_TAG_template_value_param)
12877 {
12878 struct symbol *arg = new_symbol (child_die, NULL, cu);
12879
12880 if (arg != NULL)
12881 template_args.push_back (arg);
12882 }
12883 else
12884 process_die (child_die, cu);
12885 child_die = sibling_die (child_die);
12886 }
12887 }
12888
12889 inherit_abstract_dies (die, cu);
12890
12891 /* If we have a DW_AT_specification, we might need to import using
12892 directives from the context of the specification DIE. See the
12893 comment in determine_prefix. */
12894 if (cu->language == language_cplus
12895 && dwarf2_attr (die, DW_AT_specification, cu))
12896 {
12897 struct dwarf2_cu *spec_cu = cu;
12898 struct die_info *spec_die = die_specification (die, &spec_cu);
12899
12900 while (spec_die)
12901 {
12902 child_die = spec_die->child;
12903 while (child_die && child_die->tag)
12904 {
12905 if (child_die->tag == DW_TAG_imported_module)
12906 process_die (child_die, spec_cu);
12907 child_die = sibling_die (child_die);
12908 }
12909
12910 /* In some cases, GCC generates specification DIEs that
12911 themselves contain DW_AT_specification attributes. */
12912 spec_die = die_specification (spec_die, &spec_cu);
12913 }
12914 }
12915
12916 struct context_stack cstk = cu->get_builder ()->pop_context ();
12917 /* Make a block for the local symbols within. */
12918 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12919 cstk.static_link, lowpc, highpc);
12920
12921 /* For C++, set the block's scope. */
12922 if ((cu->language == language_cplus
12923 || cu->language == language_fortran
12924 || cu->language == language_d
12925 || cu->language == language_rust)
12926 && cu->processing_has_namespace_info)
12927 block_set_scope (block, determine_prefix (die, cu),
12928 &objfile->objfile_obstack);
12929
12930 /* If we have address ranges, record them. */
12931 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12932
12933 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12934
12935 /* Attach template arguments to function. */
12936 if (!template_args.empty ())
12937 {
12938 gdb_assert (templ_func != NULL);
12939
12940 templ_func->n_template_arguments = template_args.size ();
12941 templ_func->template_arguments
12942 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12943 templ_func->n_template_arguments);
12944 memcpy (templ_func->template_arguments,
12945 template_args.data (),
12946 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12947
12948 /* Make sure that the symtab is set on the new symbols. Even
12949 though they don't appear in this symtab directly, other parts
12950 of gdb assume that symbols do, and this is reasonably
12951 true. */
12952 for (symbol *sym : template_args)
12953 symbol_set_symtab (sym, symbol_symtab (templ_func));
12954 }
12955
12956 /* In C++, we can have functions nested inside functions (e.g., when
12957 a function declares a class that has methods). This means that
12958 when we finish processing a function scope, we may need to go
12959 back to building a containing block's symbol lists. */
12960 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12961 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12962
12963 /* If we've finished processing a top-level function, subsequent
12964 symbols go in the file symbol list. */
12965 if (cu->get_builder ()->outermost_context_p ())
12966 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12967 }
12968
12969 /* Process all the DIES contained within a lexical block scope. Start
12970 a new scope, process the dies, and then close the scope. */
12971
12972 static void
12973 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12974 {
12975 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12976 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12977 CORE_ADDR lowpc, highpc;
12978 struct die_info *child_die;
12979 CORE_ADDR baseaddr;
12980
12981 baseaddr = objfile->text_section_offset ();
12982
12983 /* Ignore blocks with missing or invalid low and high pc attributes. */
12984 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12985 as multiple lexical blocks? Handling children in a sane way would
12986 be nasty. Might be easier to properly extend generic blocks to
12987 describe ranges. */
12988 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12989 {
12990 case PC_BOUNDS_NOT_PRESENT:
12991 /* DW_TAG_lexical_block has no attributes, process its children as if
12992 there was no wrapping by that DW_TAG_lexical_block.
12993 GCC does no longer produces such DWARF since GCC r224161. */
12994 for (child_die = die->child;
12995 child_die != NULL && child_die->tag;
12996 child_die = sibling_die (child_die))
12997 process_die (child_die, cu);
12998 return;
12999 case PC_BOUNDS_INVALID:
13000 return;
13001 }
13002 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13003 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13004
13005 cu->get_builder ()->push_context (0, lowpc);
13006 if (die->child != NULL)
13007 {
13008 child_die = die->child;
13009 while (child_die && child_die->tag)
13010 {
13011 process_die (child_die, cu);
13012 child_die = sibling_die (child_die);
13013 }
13014 }
13015 inherit_abstract_dies (die, cu);
13016 struct context_stack cstk = cu->get_builder ()->pop_context ();
13017
13018 if (*cu->get_builder ()->get_local_symbols () != NULL
13019 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13020 {
13021 struct block *block
13022 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13023 cstk.start_addr, highpc);
13024
13025 /* Note that recording ranges after traversing children, as we
13026 do here, means that recording a parent's ranges entails
13027 walking across all its children's ranges as they appear in
13028 the address map, which is quadratic behavior.
13029
13030 It would be nicer to record the parent's ranges before
13031 traversing its children, simply overriding whatever you find
13032 there. But since we don't even decide whether to create a
13033 block until after we've traversed its children, that's hard
13034 to do. */
13035 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13036 }
13037 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13038 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13039 }
13040
13041 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13042
13043 static void
13044 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13045 {
13046 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13047 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13048 CORE_ADDR pc, baseaddr;
13049 struct attribute *attr;
13050 struct call_site *call_site, call_site_local;
13051 void **slot;
13052 int nparams;
13053 struct die_info *child_die;
13054
13055 baseaddr = objfile->text_section_offset ();
13056
13057 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13058 if (attr == NULL)
13059 {
13060 /* This was a pre-DWARF-5 GNU extension alias
13061 for DW_AT_call_return_pc. */
13062 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13063 }
13064 if (!attr)
13065 {
13066 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13067 "DIE %s [in module %s]"),
13068 sect_offset_str (die->sect_off), objfile_name (objfile));
13069 return;
13070 }
13071 pc = attr->value_as_address () + baseaddr;
13072 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13073
13074 if (cu->call_site_htab == NULL)
13075 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13076 NULL, &objfile->objfile_obstack,
13077 hashtab_obstack_allocate, NULL);
13078 call_site_local.pc = pc;
13079 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13080 if (*slot != NULL)
13081 {
13082 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13083 "DIE %s [in module %s]"),
13084 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13085 objfile_name (objfile));
13086 return;
13087 }
13088
13089 /* Count parameters at the caller. */
13090
13091 nparams = 0;
13092 for (child_die = die->child; child_die && child_die->tag;
13093 child_die = sibling_die (child_die))
13094 {
13095 if (child_die->tag != DW_TAG_call_site_parameter
13096 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13097 {
13098 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13099 "DW_TAG_call_site child DIE %s [in module %s]"),
13100 child_die->tag, sect_offset_str (child_die->sect_off),
13101 objfile_name (objfile));
13102 continue;
13103 }
13104
13105 nparams++;
13106 }
13107
13108 call_site
13109 = ((struct call_site *)
13110 obstack_alloc (&objfile->objfile_obstack,
13111 sizeof (*call_site)
13112 + (sizeof (*call_site->parameter) * (nparams - 1))));
13113 *slot = call_site;
13114 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13115 call_site->pc = pc;
13116
13117 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13118 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13119 {
13120 struct die_info *func_die;
13121
13122 /* Skip also over DW_TAG_inlined_subroutine. */
13123 for (func_die = die->parent;
13124 func_die && func_die->tag != DW_TAG_subprogram
13125 && func_die->tag != DW_TAG_subroutine_type;
13126 func_die = func_die->parent);
13127
13128 /* DW_AT_call_all_calls is a superset
13129 of DW_AT_call_all_tail_calls. */
13130 if (func_die
13131 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13132 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13133 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13134 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13135 {
13136 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13137 not complete. But keep CALL_SITE for look ups via call_site_htab,
13138 both the initial caller containing the real return address PC and
13139 the final callee containing the current PC of a chain of tail
13140 calls do not need to have the tail call list complete. But any
13141 function candidate for a virtual tail call frame searched via
13142 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13143 determined unambiguously. */
13144 }
13145 else
13146 {
13147 struct type *func_type = NULL;
13148
13149 if (func_die)
13150 func_type = get_die_type (func_die, cu);
13151 if (func_type != NULL)
13152 {
13153 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13154
13155 /* Enlist this call site to the function. */
13156 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13157 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13158 }
13159 else
13160 complaint (_("Cannot find function owning DW_TAG_call_site "
13161 "DIE %s [in module %s]"),
13162 sect_offset_str (die->sect_off), objfile_name (objfile));
13163 }
13164 }
13165
13166 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13167 if (attr == NULL)
13168 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13169 if (attr == NULL)
13170 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13171 if (attr == NULL)
13172 {
13173 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13174 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13175 }
13176 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13177 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13178 /* Keep NULL DWARF_BLOCK. */;
13179 else if (attr->form_is_block ())
13180 {
13181 struct dwarf2_locexpr_baton *dlbaton;
13182
13183 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13184 dlbaton->data = DW_BLOCK (attr)->data;
13185 dlbaton->size = DW_BLOCK (attr)->size;
13186 dlbaton->per_cu = cu->per_cu;
13187
13188 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13189 }
13190 else if (attr->form_is_ref ())
13191 {
13192 struct dwarf2_cu *target_cu = cu;
13193 struct die_info *target_die;
13194
13195 target_die = follow_die_ref (die, attr, &target_cu);
13196 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13197 if (die_is_declaration (target_die, target_cu))
13198 {
13199 const char *target_physname;
13200
13201 /* Prefer the mangled name; otherwise compute the demangled one. */
13202 target_physname = dw2_linkage_name (target_die, target_cu);
13203 if (target_physname == NULL)
13204 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13205 if (target_physname == NULL)
13206 complaint (_("DW_AT_call_target target DIE has invalid "
13207 "physname, for referencing DIE %s [in module %s]"),
13208 sect_offset_str (die->sect_off), objfile_name (objfile));
13209 else
13210 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13211 }
13212 else
13213 {
13214 CORE_ADDR lowpc;
13215
13216 /* DW_AT_entry_pc should be preferred. */
13217 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13218 <= PC_BOUNDS_INVALID)
13219 complaint (_("DW_AT_call_target target DIE has invalid "
13220 "low pc, for referencing DIE %s [in module %s]"),
13221 sect_offset_str (die->sect_off), objfile_name (objfile));
13222 else
13223 {
13224 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13225 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13226 }
13227 }
13228 }
13229 else
13230 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13231 "block nor reference, for DIE %s [in module %s]"),
13232 sect_offset_str (die->sect_off), objfile_name (objfile));
13233
13234 call_site->per_cu = cu->per_cu;
13235
13236 for (child_die = die->child;
13237 child_die && child_die->tag;
13238 child_die = sibling_die (child_die))
13239 {
13240 struct call_site_parameter *parameter;
13241 struct attribute *loc, *origin;
13242
13243 if (child_die->tag != DW_TAG_call_site_parameter
13244 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13245 {
13246 /* Already printed the complaint above. */
13247 continue;
13248 }
13249
13250 gdb_assert (call_site->parameter_count < nparams);
13251 parameter = &call_site->parameter[call_site->parameter_count];
13252
13253 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13254 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13255 register is contained in DW_AT_call_value. */
13256
13257 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13258 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13259 if (origin == NULL)
13260 {
13261 /* This was a pre-DWARF-5 GNU extension alias
13262 for DW_AT_call_parameter. */
13263 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13264 }
13265 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13266 {
13267 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13268
13269 sect_offset sect_off
13270 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13271 if (!cu->header.offset_in_cu_p (sect_off))
13272 {
13273 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13274 binding can be done only inside one CU. Such referenced DIE
13275 therefore cannot be even moved to DW_TAG_partial_unit. */
13276 complaint (_("DW_AT_call_parameter offset is not in CU for "
13277 "DW_TAG_call_site child DIE %s [in module %s]"),
13278 sect_offset_str (child_die->sect_off),
13279 objfile_name (objfile));
13280 continue;
13281 }
13282 parameter->u.param_cu_off
13283 = (cu_offset) (sect_off - cu->header.sect_off);
13284 }
13285 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13286 {
13287 complaint (_("No DW_FORM_block* DW_AT_location for "
13288 "DW_TAG_call_site child DIE %s [in module %s]"),
13289 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13290 continue;
13291 }
13292 else
13293 {
13294 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13295 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13296 if (parameter->u.dwarf_reg != -1)
13297 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13298 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13299 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13300 &parameter->u.fb_offset))
13301 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13302 else
13303 {
13304 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13305 "for DW_FORM_block* DW_AT_location is supported for "
13306 "DW_TAG_call_site child DIE %s "
13307 "[in module %s]"),
13308 sect_offset_str (child_die->sect_off),
13309 objfile_name (objfile));
13310 continue;
13311 }
13312 }
13313
13314 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13315 if (attr == NULL)
13316 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13317 if (attr == NULL || !attr->form_is_block ())
13318 {
13319 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13320 "DW_TAG_call_site child DIE %s [in module %s]"),
13321 sect_offset_str (child_die->sect_off),
13322 objfile_name (objfile));
13323 continue;
13324 }
13325 parameter->value = DW_BLOCK (attr)->data;
13326 parameter->value_size = DW_BLOCK (attr)->size;
13327
13328 /* Parameters are not pre-cleared by memset above. */
13329 parameter->data_value = NULL;
13330 parameter->data_value_size = 0;
13331 call_site->parameter_count++;
13332
13333 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13334 if (attr == NULL)
13335 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13336 if (attr != nullptr)
13337 {
13338 if (!attr->form_is_block ())
13339 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13340 "DW_TAG_call_site child DIE %s [in module %s]"),
13341 sect_offset_str (child_die->sect_off),
13342 objfile_name (objfile));
13343 else
13344 {
13345 parameter->data_value = DW_BLOCK (attr)->data;
13346 parameter->data_value_size = DW_BLOCK (attr)->size;
13347 }
13348 }
13349 }
13350 }
13351
13352 /* Helper function for read_variable. If DIE represents a virtual
13353 table, then return the type of the concrete object that is
13354 associated with the virtual table. Otherwise, return NULL. */
13355
13356 static struct type *
13357 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13358 {
13359 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13360 if (attr == NULL)
13361 return NULL;
13362
13363 /* Find the type DIE. */
13364 struct die_info *type_die = NULL;
13365 struct dwarf2_cu *type_cu = cu;
13366
13367 if (attr->form_is_ref ())
13368 type_die = follow_die_ref (die, attr, &type_cu);
13369 if (type_die == NULL)
13370 return NULL;
13371
13372 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13373 return NULL;
13374 return die_containing_type (type_die, type_cu);
13375 }
13376
13377 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13378
13379 static void
13380 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13381 {
13382 struct rust_vtable_symbol *storage = NULL;
13383
13384 if (cu->language == language_rust)
13385 {
13386 struct type *containing_type = rust_containing_type (die, cu);
13387
13388 if (containing_type != NULL)
13389 {
13390 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13391
13392 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13393 initialize_objfile_symbol (storage);
13394 storage->concrete_type = containing_type;
13395 storage->subclass = SYMBOL_RUST_VTABLE;
13396 }
13397 }
13398
13399 struct symbol *res = new_symbol (die, NULL, cu, storage);
13400 struct attribute *abstract_origin
13401 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13402 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13403 if (res == NULL && loc && abstract_origin)
13404 {
13405 /* We have a variable without a name, but with a location and an abstract
13406 origin. This may be a concrete instance of an abstract variable
13407 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13408 later. */
13409 struct dwarf2_cu *origin_cu = cu;
13410 struct die_info *origin_die
13411 = follow_die_ref (die, abstract_origin, &origin_cu);
13412 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13413 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13414 }
13415 }
13416
13417 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13418 reading .debug_rnglists.
13419 Callback's type should be:
13420 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13421 Return true if the attributes are present and valid, otherwise,
13422 return false. */
13423
13424 template <typename Callback>
13425 static bool
13426 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13427 Callback &&callback)
13428 {
13429 struct dwarf2_per_objfile *dwarf2_per_objfile
13430 = cu->per_cu->dwarf2_per_objfile;
13431 struct objfile *objfile = dwarf2_per_objfile->objfile;
13432 bfd *obfd = objfile->obfd;
13433 /* Base address selection entry. */
13434 gdb::optional<CORE_ADDR> base;
13435 const gdb_byte *buffer;
13436 CORE_ADDR baseaddr;
13437 bool overflow = false;
13438
13439 base = cu->base_address;
13440
13441 dwarf2_per_objfile->rnglists.read (objfile);
13442 if (offset >= dwarf2_per_objfile->rnglists.size)
13443 {
13444 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13445 offset);
13446 return false;
13447 }
13448 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13449
13450 baseaddr = objfile->text_section_offset ();
13451
13452 while (1)
13453 {
13454 /* Initialize it due to a false compiler warning. */
13455 CORE_ADDR range_beginning = 0, range_end = 0;
13456 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13457 + dwarf2_per_objfile->rnglists.size);
13458 unsigned int bytes_read;
13459
13460 if (buffer == buf_end)
13461 {
13462 overflow = true;
13463 break;
13464 }
13465 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13466 switch (rlet)
13467 {
13468 case DW_RLE_end_of_list:
13469 break;
13470 case DW_RLE_base_address:
13471 if (buffer + cu->header.addr_size > buf_end)
13472 {
13473 overflow = true;
13474 break;
13475 }
13476 base = cu->header.read_address (obfd, buffer, &bytes_read);
13477 buffer += bytes_read;
13478 break;
13479 case DW_RLE_start_length:
13480 if (buffer + cu->header.addr_size > buf_end)
13481 {
13482 overflow = true;
13483 break;
13484 }
13485 range_beginning = cu->header.read_address (obfd, buffer,
13486 &bytes_read);
13487 buffer += bytes_read;
13488 range_end = (range_beginning
13489 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13490 buffer += bytes_read;
13491 if (buffer > buf_end)
13492 {
13493 overflow = true;
13494 break;
13495 }
13496 break;
13497 case DW_RLE_offset_pair:
13498 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13499 buffer += bytes_read;
13500 if (buffer > buf_end)
13501 {
13502 overflow = true;
13503 break;
13504 }
13505 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13506 buffer += bytes_read;
13507 if (buffer > buf_end)
13508 {
13509 overflow = true;
13510 break;
13511 }
13512 break;
13513 case DW_RLE_start_end:
13514 if (buffer + 2 * cu->header.addr_size > buf_end)
13515 {
13516 overflow = true;
13517 break;
13518 }
13519 range_beginning = cu->header.read_address (obfd, buffer,
13520 &bytes_read);
13521 buffer += bytes_read;
13522 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13523 buffer += bytes_read;
13524 break;
13525 default:
13526 complaint (_("Invalid .debug_rnglists data (no base address)"));
13527 return false;
13528 }
13529 if (rlet == DW_RLE_end_of_list || overflow)
13530 break;
13531 if (rlet == DW_RLE_base_address)
13532 continue;
13533
13534 if (!base.has_value ())
13535 {
13536 /* We have no valid base address for the ranges
13537 data. */
13538 complaint (_("Invalid .debug_rnglists data (no base address)"));
13539 return false;
13540 }
13541
13542 if (range_beginning > range_end)
13543 {
13544 /* Inverted range entries are invalid. */
13545 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13546 return false;
13547 }
13548
13549 /* Empty range entries have no effect. */
13550 if (range_beginning == range_end)
13551 continue;
13552
13553 range_beginning += *base;
13554 range_end += *base;
13555
13556 /* A not-uncommon case of bad debug info.
13557 Don't pollute the addrmap with bad data. */
13558 if (range_beginning + baseaddr == 0
13559 && !dwarf2_per_objfile->has_section_at_zero)
13560 {
13561 complaint (_(".debug_rnglists entry has start address of zero"
13562 " [in module %s]"), objfile_name (objfile));
13563 continue;
13564 }
13565
13566 callback (range_beginning, range_end);
13567 }
13568
13569 if (overflow)
13570 {
13571 complaint (_("Offset %d is not terminated "
13572 "for DW_AT_ranges attribute"),
13573 offset);
13574 return false;
13575 }
13576
13577 return true;
13578 }
13579
13580 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13581 Callback's type should be:
13582 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13583 Return 1 if the attributes are present and valid, otherwise, return 0. */
13584
13585 template <typename Callback>
13586 static int
13587 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13588 Callback &&callback)
13589 {
13590 struct dwarf2_per_objfile *dwarf2_per_objfile
13591 = cu->per_cu->dwarf2_per_objfile;
13592 struct objfile *objfile = dwarf2_per_objfile->objfile;
13593 struct comp_unit_head *cu_header = &cu->header;
13594 bfd *obfd = objfile->obfd;
13595 unsigned int addr_size = cu_header->addr_size;
13596 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13597 /* Base address selection entry. */
13598 gdb::optional<CORE_ADDR> base;
13599 unsigned int dummy;
13600 const gdb_byte *buffer;
13601 CORE_ADDR baseaddr;
13602
13603 if (cu_header->version >= 5)
13604 return dwarf2_rnglists_process (offset, cu, callback);
13605
13606 base = cu->base_address;
13607
13608 dwarf2_per_objfile->ranges.read (objfile);
13609 if (offset >= dwarf2_per_objfile->ranges.size)
13610 {
13611 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13612 offset);
13613 return 0;
13614 }
13615 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13616
13617 baseaddr = objfile->text_section_offset ();
13618
13619 while (1)
13620 {
13621 CORE_ADDR range_beginning, range_end;
13622
13623 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13624 buffer += addr_size;
13625 range_end = cu->header.read_address (obfd, buffer, &dummy);
13626 buffer += addr_size;
13627 offset += 2 * addr_size;
13628
13629 /* An end of list marker is a pair of zero addresses. */
13630 if (range_beginning == 0 && range_end == 0)
13631 /* Found the end of list entry. */
13632 break;
13633
13634 /* Each base address selection entry is a pair of 2 values.
13635 The first is the largest possible address, the second is
13636 the base address. Check for a base address here. */
13637 if ((range_beginning & mask) == mask)
13638 {
13639 /* If we found the largest possible address, then we already
13640 have the base address in range_end. */
13641 base = range_end;
13642 continue;
13643 }
13644
13645 if (!base.has_value ())
13646 {
13647 /* We have no valid base address for the ranges
13648 data. */
13649 complaint (_("Invalid .debug_ranges data (no base address)"));
13650 return 0;
13651 }
13652
13653 if (range_beginning > range_end)
13654 {
13655 /* Inverted range entries are invalid. */
13656 complaint (_("Invalid .debug_ranges data (inverted range)"));
13657 return 0;
13658 }
13659
13660 /* Empty range entries have no effect. */
13661 if (range_beginning == range_end)
13662 continue;
13663
13664 range_beginning += *base;
13665 range_end += *base;
13666
13667 /* A not-uncommon case of bad debug info.
13668 Don't pollute the addrmap with bad data. */
13669 if (range_beginning + baseaddr == 0
13670 && !dwarf2_per_objfile->has_section_at_zero)
13671 {
13672 complaint (_(".debug_ranges entry has start address of zero"
13673 " [in module %s]"), objfile_name (objfile));
13674 continue;
13675 }
13676
13677 callback (range_beginning, range_end);
13678 }
13679
13680 return 1;
13681 }
13682
13683 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13684 Return 1 if the attributes are present and valid, otherwise, return 0.
13685 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13686
13687 static int
13688 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13689 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13690 dwarf2_psymtab *ranges_pst)
13691 {
13692 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13693 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13694 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13695 int low_set = 0;
13696 CORE_ADDR low = 0;
13697 CORE_ADDR high = 0;
13698 int retval;
13699
13700 retval = dwarf2_ranges_process (offset, cu,
13701 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13702 {
13703 if (ranges_pst != NULL)
13704 {
13705 CORE_ADDR lowpc;
13706 CORE_ADDR highpc;
13707
13708 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13709 range_beginning + baseaddr)
13710 - baseaddr);
13711 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13712 range_end + baseaddr)
13713 - baseaddr);
13714 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13715 lowpc, highpc - 1, ranges_pst);
13716 }
13717
13718 /* FIXME: This is recording everything as a low-high
13719 segment of consecutive addresses. We should have a
13720 data structure for discontiguous block ranges
13721 instead. */
13722 if (! low_set)
13723 {
13724 low = range_beginning;
13725 high = range_end;
13726 low_set = 1;
13727 }
13728 else
13729 {
13730 if (range_beginning < low)
13731 low = range_beginning;
13732 if (range_end > high)
13733 high = range_end;
13734 }
13735 });
13736 if (!retval)
13737 return 0;
13738
13739 if (! low_set)
13740 /* If the first entry is an end-of-list marker, the range
13741 describes an empty scope, i.e. no instructions. */
13742 return 0;
13743
13744 if (low_return)
13745 *low_return = low;
13746 if (high_return)
13747 *high_return = high;
13748 return 1;
13749 }
13750
13751 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13752 definition for the return value. *LOWPC and *HIGHPC are set iff
13753 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13754
13755 static enum pc_bounds_kind
13756 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13757 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13758 dwarf2_psymtab *pst)
13759 {
13760 struct dwarf2_per_objfile *dwarf2_per_objfile
13761 = cu->per_cu->dwarf2_per_objfile;
13762 struct attribute *attr;
13763 struct attribute *attr_high;
13764 CORE_ADDR low = 0;
13765 CORE_ADDR high = 0;
13766 enum pc_bounds_kind ret;
13767
13768 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13769 if (attr_high)
13770 {
13771 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13772 if (attr != nullptr)
13773 {
13774 low = attr->value_as_address ();
13775 high = attr_high->value_as_address ();
13776 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13777 high += low;
13778 }
13779 else
13780 /* Found high w/o low attribute. */
13781 return PC_BOUNDS_INVALID;
13782
13783 /* Found consecutive range of addresses. */
13784 ret = PC_BOUNDS_HIGH_LOW;
13785 }
13786 else
13787 {
13788 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13789 if (attr != NULL)
13790 {
13791 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13792 We take advantage of the fact that DW_AT_ranges does not appear
13793 in DW_TAG_compile_unit of DWO files. */
13794 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13795 unsigned int ranges_offset = (DW_UNSND (attr)
13796 + (need_ranges_base
13797 ? cu->ranges_base
13798 : 0));
13799
13800 /* Value of the DW_AT_ranges attribute is the offset in the
13801 .debug_ranges section. */
13802 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13803 return PC_BOUNDS_INVALID;
13804 /* Found discontinuous range of addresses. */
13805 ret = PC_BOUNDS_RANGES;
13806 }
13807 else
13808 return PC_BOUNDS_NOT_PRESENT;
13809 }
13810
13811 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13812 if (high <= low)
13813 return PC_BOUNDS_INVALID;
13814
13815 /* When using the GNU linker, .gnu.linkonce. sections are used to
13816 eliminate duplicate copies of functions and vtables and such.
13817 The linker will arbitrarily choose one and discard the others.
13818 The AT_*_pc values for such functions refer to local labels in
13819 these sections. If the section from that file was discarded, the
13820 labels are not in the output, so the relocs get a value of 0.
13821 If this is a discarded function, mark the pc bounds as invalid,
13822 so that GDB will ignore it. */
13823 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13824 return PC_BOUNDS_INVALID;
13825
13826 *lowpc = low;
13827 if (highpc)
13828 *highpc = high;
13829 return ret;
13830 }
13831
13832 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13833 its low and high PC addresses. Do nothing if these addresses could not
13834 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13835 and HIGHPC to the high address if greater than HIGHPC. */
13836
13837 static void
13838 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13839 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13840 struct dwarf2_cu *cu)
13841 {
13842 CORE_ADDR low, high;
13843 struct die_info *child = die->child;
13844
13845 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13846 {
13847 *lowpc = std::min (*lowpc, low);
13848 *highpc = std::max (*highpc, high);
13849 }
13850
13851 /* If the language does not allow nested subprograms (either inside
13852 subprograms or lexical blocks), we're done. */
13853 if (cu->language != language_ada)
13854 return;
13855
13856 /* Check all the children of the given DIE. If it contains nested
13857 subprograms, then check their pc bounds. Likewise, we need to
13858 check lexical blocks as well, as they may also contain subprogram
13859 definitions. */
13860 while (child && child->tag)
13861 {
13862 if (child->tag == DW_TAG_subprogram
13863 || child->tag == DW_TAG_lexical_block)
13864 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13865 child = sibling_die (child);
13866 }
13867 }
13868
13869 /* Get the low and high pc's represented by the scope DIE, and store
13870 them in *LOWPC and *HIGHPC. If the correct values can't be
13871 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13872
13873 static void
13874 get_scope_pc_bounds (struct die_info *die,
13875 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13876 struct dwarf2_cu *cu)
13877 {
13878 CORE_ADDR best_low = (CORE_ADDR) -1;
13879 CORE_ADDR best_high = (CORE_ADDR) 0;
13880 CORE_ADDR current_low, current_high;
13881
13882 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13883 >= PC_BOUNDS_RANGES)
13884 {
13885 best_low = current_low;
13886 best_high = current_high;
13887 }
13888 else
13889 {
13890 struct die_info *child = die->child;
13891
13892 while (child && child->tag)
13893 {
13894 switch (child->tag) {
13895 case DW_TAG_subprogram:
13896 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13897 break;
13898 case DW_TAG_namespace:
13899 case DW_TAG_module:
13900 /* FIXME: carlton/2004-01-16: Should we do this for
13901 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13902 that current GCC's always emit the DIEs corresponding
13903 to definitions of methods of classes as children of a
13904 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13905 the DIEs giving the declarations, which could be
13906 anywhere). But I don't see any reason why the
13907 standards says that they have to be there. */
13908 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13909
13910 if (current_low != ((CORE_ADDR) -1))
13911 {
13912 best_low = std::min (best_low, current_low);
13913 best_high = std::max (best_high, current_high);
13914 }
13915 break;
13916 default:
13917 /* Ignore. */
13918 break;
13919 }
13920
13921 child = sibling_die (child);
13922 }
13923 }
13924
13925 *lowpc = best_low;
13926 *highpc = best_high;
13927 }
13928
13929 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13930 in DIE. */
13931
13932 static void
13933 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13934 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13935 {
13936 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13937 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13938 struct attribute *attr;
13939 struct attribute *attr_high;
13940
13941 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13942 if (attr_high)
13943 {
13944 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13945 if (attr != nullptr)
13946 {
13947 CORE_ADDR low = attr->value_as_address ();
13948 CORE_ADDR high = attr_high->value_as_address ();
13949
13950 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13951 high += low;
13952
13953 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13954 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13955 cu->get_builder ()->record_block_range (block, low, high - 1);
13956 }
13957 }
13958
13959 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13960 if (attr != nullptr)
13961 {
13962 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13963 We take advantage of the fact that DW_AT_ranges does not appear
13964 in DW_TAG_compile_unit of DWO files. */
13965 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13966
13967 /* The value of the DW_AT_ranges attribute is the offset of the
13968 address range list in the .debug_ranges section. */
13969 unsigned long offset = (DW_UNSND (attr)
13970 + (need_ranges_base ? cu->ranges_base : 0));
13971
13972 std::vector<blockrange> blockvec;
13973 dwarf2_ranges_process (offset, cu,
13974 [&] (CORE_ADDR start, CORE_ADDR end)
13975 {
13976 start += baseaddr;
13977 end += baseaddr;
13978 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13979 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13980 cu->get_builder ()->record_block_range (block, start, end - 1);
13981 blockvec.emplace_back (start, end);
13982 });
13983
13984 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
13985 }
13986 }
13987
13988 /* Check whether the producer field indicates either of GCC < 4.6, or the
13989 Intel C/C++ compiler, and cache the result in CU. */
13990
13991 static void
13992 check_producer (struct dwarf2_cu *cu)
13993 {
13994 int major, minor;
13995
13996 if (cu->producer == NULL)
13997 {
13998 /* For unknown compilers expect their behavior is DWARF version
13999 compliant.
14000
14001 GCC started to support .debug_types sections by -gdwarf-4 since
14002 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14003 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14004 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14005 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14006 }
14007 else if (producer_is_gcc (cu->producer, &major, &minor))
14008 {
14009 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14010 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14011 }
14012 else if (producer_is_icc (cu->producer, &major, &minor))
14013 {
14014 cu->producer_is_icc = true;
14015 cu->producer_is_icc_lt_14 = major < 14;
14016 }
14017 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14018 cu->producer_is_codewarrior = true;
14019 else
14020 {
14021 /* For other non-GCC compilers, expect their behavior is DWARF version
14022 compliant. */
14023 }
14024
14025 cu->checked_producer = true;
14026 }
14027
14028 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14029 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14030 during 4.6.0 experimental. */
14031
14032 static bool
14033 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14034 {
14035 if (!cu->checked_producer)
14036 check_producer (cu);
14037
14038 return cu->producer_is_gxx_lt_4_6;
14039 }
14040
14041
14042 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14043 with incorrect is_stmt attributes. */
14044
14045 static bool
14046 producer_is_codewarrior (struct dwarf2_cu *cu)
14047 {
14048 if (!cu->checked_producer)
14049 check_producer (cu);
14050
14051 return cu->producer_is_codewarrior;
14052 }
14053
14054 /* Return the default accessibility type if it is not overridden by
14055 DW_AT_accessibility. */
14056
14057 static enum dwarf_access_attribute
14058 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14059 {
14060 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14061 {
14062 /* The default DWARF 2 accessibility for members is public, the default
14063 accessibility for inheritance is private. */
14064
14065 if (die->tag != DW_TAG_inheritance)
14066 return DW_ACCESS_public;
14067 else
14068 return DW_ACCESS_private;
14069 }
14070 else
14071 {
14072 /* DWARF 3+ defines the default accessibility a different way. The same
14073 rules apply now for DW_TAG_inheritance as for the members and it only
14074 depends on the container kind. */
14075
14076 if (die->parent->tag == DW_TAG_class_type)
14077 return DW_ACCESS_private;
14078 else
14079 return DW_ACCESS_public;
14080 }
14081 }
14082
14083 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14084 offset. If the attribute was not found return 0, otherwise return
14085 1. If it was found but could not properly be handled, set *OFFSET
14086 to 0. */
14087
14088 static int
14089 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14090 LONGEST *offset)
14091 {
14092 struct attribute *attr;
14093
14094 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14095 if (attr != NULL)
14096 {
14097 *offset = 0;
14098
14099 /* Note that we do not check for a section offset first here.
14100 This is because DW_AT_data_member_location is new in DWARF 4,
14101 so if we see it, we can assume that a constant form is really
14102 a constant and not a section offset. */
14103 if (attr->form_is_constant ())
14104 *offset = dwarf2_get_attr_constant_value (attr, 0);
14105 else if (attr->form_is_section_offset ())
14106 dwarf2_complex_location_expr_complaint ();
14107 else if (attr->form_is_block ())
14108 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14109 else
14110 dwarf2_complex_location_expr_complaint ();
14111
14112 return 1;
14113 }
14114
14115 return 0;
14116 }
14117
14118 /* Add an aggregate field to the field list. */
14119
14120 static void
14121 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14122 struct dwarf2_cu *cu)
14123 {
14124 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14125 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14126 struct nextfield *new_field;
14127 struct attribute *attr;
14128 struct field *fp;
14129 const char *fieldname = "";
14130
14131 if (die->tag == DW_TAG_inheritance)
14132 {
14133 fip->baseclasses.emplace_back ();
14134 new_field = &fip->baseclasses.back ();
14135 }
14136 else
14137 {
14138 fip->fields.emplace_back ();
14139 new_field = &fip->fields.back ();
14140 }
14141
14142 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14143 if (attr != nullptr)
14144 new_field->accessibility = DW_UNSND (attr);
14145 else
14146 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14147 if (new_field->accessibility != DW_ACCESS_public)
14148 fip->non_public_fields = 1;
14149
14150 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14151 if (attr != nullptr)
14152 new_field->virtuality = DW_UNSND (attr);
14153 else
14154 new_field->virtuality = DW_VIRTUALITY_none;
14155
14156 fp = &new_field->field;
14157
14158 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14159 {
14160 LONGEST offset;
14161
14162 /* Data member other than a C++ static data member. */
14163
14164 /* Get type of field. */
14165 fp->type = die_type (die, cu);
14166
14167 SET_FIELD_BITPOS (*fp, 0);
14168
14169 /* Get bit size of field (zero if none). */
14170 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14171 if (attr != nullptr)
14172 {
14173 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14174 }
14175 else
14176 {
14177 FIELD_BITSIZE (*fp) = 0;
14178 }
14179
14180 /* Get bit offset of field. */
14181 if (handle_data_member_location (die, cu, &offset))
14182 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14183 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14184 if (attr != nullptr)
14185 {
14186 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14187 {
14188 /* For big endian bits, the DW_AT_bit_offset gives the
14189 additional bit offset from the MSB of the containing
14190 anonymous object to the MSB of the field. We don't
14191 have to do anything special since we don't need to
14192 know the size of the anonymous object. */
14193 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14194 }
14195 else
14196 {
14197 /* For little endian bits, compute the bit offset to the
14198 MSB of the anonymous object, subtract off the number of
14199 bits from the MSB of the field to the MSB of the
14200 object, and then subtract off the number of bits of
14201 the field itself. The result is the bit offset of
14202 the LSB of the field. */
14203 int anonymous_size;
14204 int bit_offset = DW_UNSND (attr);
14205
14206 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14207 if (attr != nullptr)
14208 {
14209 /* The size of the anonymous object containing
14210 the bit field is explicit, so use the
14211 indicated size (in bytes). */
14212 anonymous_size = DW_UNSND (attr);
14213 }
14214 else
14215 {
14216 /* The size of the anonymous object containing
14217 the bit field must be inferred from the type
14218 attribute of the data member containing the
14219 bit field. */
14220 anonymous_size = TYPE_LENGTH (fp->type);
14221 }
14222 SET_FIELD_BITPOS (*fp,
14223 (FIELD_BITPOS (*fp)
14224 + anonymous_size * bits_per_byte
14225 - bit_offset - FIELD_BITSIZE (*fp)));
14226 }
14227 }
14228 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14229 if (attr != NULL)
14230 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14231 + dwarf2_get_attr_constant_value (attr, 0)));
14232
14233 /* Get name of field. */
14234 fieldname = dwarf2_name (die, cu);
14235 if (fieldname == NULL)
14236 fieldname = "";
14237
14238 /* The name is already allocated along with this objfile, so we don't
14239 need to duplicate it for the type. */
14240 fp->name = fieldname;
14241
14242 /* Change accessibility for artificial fields (e.g. virtual table
14243 pointer or virtual base class pointer) to private. */
14244 if (dwarf2_attr (die, DW_AT_artificial, cu))
14245 {
14246 FIELD_ARTIFICIAL (*fp) = 1;
14247 new_field->accessibility = DW_ACCESS_private;
14248 fip->non_public_fields = 1;
14249 }
14250 }
14251 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14252 {
14253 /* C++ static member. */
14254
14255 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14256 is a declaration, but all versions of G++ as of this writing
14257 (so through at least 3.2.1) incorrectly generate
14258 DW_TAG_variable tags. */
14259
14260 const char *physname;
14261
14262 /* Get name of field. */
14263 fieldname = dwarf2_name (die, cu);
14264 if (fieldname == NULL)
14265 return;
14266
14267 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14268 if (attr
14269 /* Only create a symbol if this is an external value.
14270 new_symbol checks this and puts the value in the global symbol
14271 table, which we want. If it is not external, new_symbol
14272 will try to put the value in cu->list_in_scope which is wrong. */
14273 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14274 {
14275 /* A static const member, not much different than an enum as far as
14276 we're concerned, except that we can support more types. */
14277 new_symbol (die, NULL, cu);
14278 }
14279
14280 /* Get physical name. */
14281 physname = dwarf2_physname (fieldname, die, cu);
14282
14283 /* The name is already allocated along with this objfile, so we don't
14284 need to duplicate it for the type. */
14285 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14286 FIELD_TYPE (*fp) = die_type (die, cu);
14287 FIELD_NAME (*fp) = fieldname;
14288 }
14289 else if (die->tag == DW_TAG_inheritance)
14290 {
14291 LONGEST offset;
14292
14293 /* C++ base class field. */
14294 if (handle_data_member_location (die, cu, &offset))
14295 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14296 FIELD_BITSIZE (*fp) = 0;
14297 FIELD_TYPE (*fp) = die_type (die, cu);
14298 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14299 }
14300 else if (die->tag == DW_TAG_variant_part)
14301 {
14302 /* process_structure_scope will treat this DIE as a union. */
14303 process_structure_scope (die, cu);
14304
14305 /* The variant part is relative to the start of the enclosing
14306 structure. */
14307 SET_FIELD_BITPOS (*fp, 0);
14308 fp->type = get_die_type (die, cu);
14309 fp->artificial = 1;
14310 fp->name = "<<variant>>";
14311
14312 /* Normally a DW_TAG_variant_part won't have a size, but our
14313 representation requires one, so set it to the maximum of the
14314 child sizes, being sure to account for the offset at which
14315 each child is seen. */
14316 if (TYPE_LENGTH (fp->type) == 0)
14317 {
14318 unsigned max = 0;
14319 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14320 {
14321 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14322 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14323 if (len > max)
14324 max = len;
14325 }
14326 TYPE_LENGTH (fp->type) = max;
14327 }
14328 }
14329 else
14330 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14331 }
14332
14333 /* Can the type given by DIE define another type? */
14334
14335 static bool
14336 type_can_define_types (const struct die_info *die)
14337 {
14338 switch (die->tag)
14339 {
14340 case DW_TAG_typedef:
14341 case DW_TAG_class_type:
14342 case DW_TAG_structure_type:
14343 case DW_TAG_union_type:
14344 case DW_TAG_enumeration_type:
14345 return true;
14346
14347 default:
14348 return false;
14349 }
14350 }
14351
14352 /* Add a type definition defined in the scope of the FIP's class. */
14353
14354 static void
14355 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14356 struct dwarf2_cu *cu)
14357 {
14358 struct decl_field fp;
14359 memset (&fp, 0, sizeof (fp));
14360
14361 gdb_assert (type_can_define_types (die));
14362
14363 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14364 fp.name = dwarf2_name (die, cu);
14365 fp.type = read_type_die (die, cu);
14366
14367 /* Save accessibility. */
14368 enum dwarf_access_attribute accessibility;
14369 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14370 if (attr != NULL)
14371 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14372 else
14373 accessibility = dwarf2_default_access_attribute (die, cu);
14374 switch (accessibility)
14375 {
14376 case DW_ACCESS_public:
14377 /* The assumed value if neither private nor protected. */
14378 break;
14379 case DW_ACCESS_private:
14380 fp.is_private = 1;
14381 break;
14382 case DW_ACCESS_protected:
14383 fp.is_protected = 1;
14384 break;
14385 default:
14386 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14387 }
14388
14389 if (die->tag == DW_TAG_typedef)
14390 fip->typedef_field_list.push_back (fp);
14391 else
14392 fip->nested_types_list.push_back (fp);
14393 }
14394
14395 /* Create the vector of fields, and attach it to the type. */
14396
14397 static void
14398 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14399 struct dwarf2_cu *cu)
14400 {
14401 int nfields = fip->nfields ();
14402
14403 /* Record the field count, allocate space for the array of fields,
14404 and create blank accessibility bitfields if necessary. */
14405 TYPE_NFIELDS (type) = nfields;
14406 TYPE_FIELDS (type) = (struct field *)
14407 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14408
14409 if (fip->non_public_fields && cu->language != language_ada)
14410 {
14411 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14412
14413 TYPE_FIELD_PRIVATE_BITS (type) =
14414 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14415 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14416
14417 TYPE_FIELD_PROTECTED_BITS (type) =
14418 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14419 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14420
14421 TYPE_FIELD_IGNORE_BITS (type) =
14422 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14423 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14424 }
14425
14426 /* If the type has baseclasses, allocate and clear a bit vector for
14427 TYPE_FIELD_VIRTUAL_BITS. */
14428 if (!fip->baseclasses.empty () && cu->language != language_ada)
14429 {
14430 int num_bytes = B_BYTES (fip->baseclasses.size ());
14431 unsigned char *pointer;
14432
14433 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14434 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14435 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14436 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14437 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14438 }
14439
14440 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14441 {
14442 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14443
14444 for (int index = 0; index < nfields; ++index)
14445 {
14446 struct nextfield &field = fip->fields[index];
14447
14448 if (field.variant.is_discriminant)
14449 di->discriminant_index = index;
14450 else if (field.variant.default_branch)
14451 di->default_index = index;
14452 else
14453 di->discriminants[index] = field.variant.discriminant_value;
14454 }
14455 }
14456
14457 /* Copy the saved-up fields into the field vector. */
14458 for (int i = 0; i < nfields; ++i)
14459 {
14460 struct nextfield &field
14461 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14462 : fip->fields[i - fip->baseclasses.size ()]);
14463
14464 TYPE_FIELD (type, i) = field.field;
14465 switch (field.accessibility)
14466 {
14467 case DW_ACCESS_private:
14468 if (cu->language != language_ada)
14469 SET_TYPE_FIELD_PRIVATE (type, i);
14470 break;
14471
14472 case DW_ACCESS_protected:
14473 if (cu->language != language_ada)
14474 SET_TYPE_FIELD_PROTECTED (type, i);
14475 break;
14476
14477 case DW_ACCESS_public:
14478 break;
14479
14480 default:
14481 /* Unknown accessibility. Complain and treat it as public. */
14482 {
14483 complaint (_("unsupported accessibility %d"),
14484 field.accessibility);
14485 }
14486 break;
14487 }
14488 if (i < fip->baseclasses.size ())
14489 {
14490 switch (field.virtuality)
14491 {
14492 case DW_VIRTUALITY_virtual:
14493 case DW_VIRTUALITY_pure_virtual:
14494 if (cu->language == language_ada)
14495 error (_("unexpected virtuality in component of Ada type"));
14496 SET_TYPE_FIELD_VIRTUAL (type, i);
14497 break;
14498 }
14499 }
14500 }
14501 }
14502
14503 /* Return true if this member function is a constructor, false
14504 otherwise. */
14505
14506 static int
14507 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14508 {
14509 const char *fieldname;
14510 const char *type_name;
14511 int len;
14512
14513 if (die->parent == NULL)
14514 return 0;
14515
14516 if (die->parent->tag != DW_TAG_structure_type
14517 && die->parent->tag != DW_TAG_union_type
14518 && die->parent->tag != DW_TAG_class_type)
14519 return 0;
14520
14521 fieldname = dwarf2_name (die, cu);
14522 type_name = dwarf2_name (die->parent, cu);
14523 if (fieldname == NULL || type_name == NULL)
14524 return 0;
14525
14526 len = strlen (fieldname);
14527 return (strncmp (fieldname, type_name, len) == 0
14528 && (type_name[len] == '\0' || type_name[len] == '<'));
14529 }
14530
14531 /* Check if the given VALUE is a recognized enum
14532 dwarf_defaulted_attribute constant according to DWARF5 spec,
14533 Table 7.24. */
14534
14535 static bool
14536 is_valid_DW_AT_defaulted (ULONGEST value)
14537 {
14538 switch (value)
14539 {
14540 case DW_DEFAULTED_no:
14541 case DW_DEFAULTED_in_class:
14542 case DW_DEFAULTED_out_of_class:
14543 return true;
14544 }
14545
14546 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14547 return false;
14548 }
14549
14550 /* Add a member function to the proper fieldlist. */
14551
14552 static void
14553 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14554 struct type *type, struct dwarf2_cu *cu)
14555 {
14556 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14557 struct attribute *attr;
14558 int i;
14559 struct fnfieldlist *flp = nullptr;
14560 struct fn_field *fnp;
14561 const char *fieldname;
14562 struct type *this_type;
14563 enum dwarf_access_attribute accessibility;
14564
14565 if (cu->language == language_ada)
14566 error (_("unexpected member function in Ada type"));
14567
14568 /* Get name of member function. */
14569 fieldname = dwarf2_name (die, cu);
14570 if (fieldname == NULL)
14571 return;
14572
14573 /* Look up member function name in fieldlist. */
14574 for (i = 0; i < fip->fnfieldlists.size (); i++)
14575 {
14576 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14577 {
14578 flp = &fip->fnfieldlists[i];
14579 break;
14580 }
14581 }
14582
14583 /* Create a new fnfieldlist if necessary. */
14584 if (flp == nullptr)
14585 {
14586 fip->fnfieldlists.emplace_back ();
14587 flp = &fip->fnfieldlists.back ();
14588 flp->name = fieldname;
14589 i = fip->fnfieldlists.size () - 1;
14590 }
14591
14592 /* Create a new member function field and add it to the vector of
14593 fnfieldlists. */
14594 flp->fnfields.emplace_back ();
14595 fnp = &flp->fnfields.back ();
14596
14597 /* Delay processing of the physname until later. */
14598 if (cu->language == language_cplus)
14599 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14600 die, cu);
14601 else
14602 {
14603 const char *physname = dwarf2_physname (fieldname, die, cu);
14604 fnp->physname = physname ? physname : "";
14605 }
14606
14607 fnp->type = alloc_type (objfile);
14608 this_type = read_type_die (die, cu);
14609 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14610 {
14611 int nparams = TYPE_NFIELDS (this_type);
14612
14613 /* TYPE is the domain of this method, and THIS_TYPE is the type
14614 of the method itself (TYPE_CODE_METHOD). */
14615 smash_to_method_type (fnp->type, type,
14616 TYPE_TARGET_TYPE (this_type),
14617 TYPE_FIELDS (this_type),
14618 TYPE_NFIELDS (this_type),
14619 TYPE_VARARGS (this_type));
14620
14621 /* Handle static member functions.
14622 Dwarf2 has no clean way to discern C++ static and non-static
14623 member functions. G++ helps GDB by marking the first
14624 parameter for non-static member functions (which is the this
14625 pointer) as artificial. We obtain this information from
14626 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14627 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14628 fnp->voffset = VOFFSET_STATIC;
14629 }
14630 else
14631 complaint (_("member function type missing for '%s'"),
14632 dwarf2_full_name (fieldname, die, cu));
14633
14634 /* Get fcontext from DW_AT_containing_type if present. */
14635 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14636 fnp->fcontext = die_containing_type (die, cu);
14637
14638 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14639 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14640
14641 /* Get accessibility. */
14642 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14643 if (attr != nullptr)
14644 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14645 else
14646 accessibility = dwarf2_default_access_attribute (die, cu);
14647 switch (accessibility)
14648 {
14649 case DW_ACCESS_private:
14650 fnp->is_private = 1;
14651 break;
14652 case DW_ACCESS_protected:
14653 fnp->is_protected = 1;
14654 break;
14655 }
14656
14657 /* Check for artificial methods. */
14658 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14659 if (attr && DW_UNSND (attr) != 0)
14660 fnp->is_artificial = 1;
14661
14662 /* Check for defaulted methods. */
14663 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14664 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14665 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14666
14667 /* Check for deleted methods. */
14668 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14669 if (attr != nullptr && DW_UNSND (attr) != 0)
14670 fnp->is_deleted = 1;
14671
14672 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14673
14674 /* Get index in virtual function table if it is a virtual member
14675 function. For older versions of GCC, this is an offset in the
14676 appropriate virtual table, as specified by DW_AT_containing_type.
14677 For everyone else, it is an expression to be evaluated relative
14678 to the object address. */
14679
14680 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14681 if (attr != nullptr)
14682 {
14683 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14684 {
14685 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14686 {
14687 /* Old-style GCC. */
14688 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14689 }
14690 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14691 || (DW_BLOCK (attr)->size > 1
14692 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14693 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14694 {
14695 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14696 if ((fnp->voffset % cu->header.addr_size) != 0)
14697 dwarf2_complex_location_expr_complaint ();
14698 else
14699 fnp->voffset /= cu->header.addr_size;
14700 fnp->voffset += 2;
14701 }
14702 else
14703 dwarf2_complex_location_expr_complaint ();
14704
14705 if (!fnp->fcontext)
14706 {
14707 /* If there is no `this' field and no DW_AT_containing_type,
14708 we cannot actually find a base class context for the
14709 vtable! */
14710 if (TYPE_NFIELDS (this_type) == 0
14711 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14712 {
14713 complaint (_("cannot determine context for virtual member "
14714 "function \"%s\" (offset %s)"),
14715 fieldname, sect_offset_str (die->sect_off));
14716 }
14717 else
14718 {
14719 fnp->fcontext
14720 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14721 }
14722 }
14723 }
14724 else if (attr->form_is_section_offset ())
14725 {
14726 dwarf2_complex_location_expr_complaint ();
14727 }
14728 else
14729 {
14730 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14731 fieldname);
14732 }
14733 }
14734 else
14735 {
14736 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14737 if (attr && DW_UNSND (attr))
14738 {
14739 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14740 complaint (_("Member function \"%s\" (offset %s) is virtual "
14741 "but the vtable offset is not specified"),
14742 fieldname, sect_offset_str (die->sect_off));
14743 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14744 TYPE_CPLUS_DYNAMIC (type) = 1;
14745 }
14746 }
14747 }
14748
14749 /* Create the vector of member function fields, and attach it to the type. */
14750
14751 static void
14752 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14753 struct dwarf2_cu *cu)
14754 {
14755 if (cu->language == language_ada)
14756 error (_("unexpected member functions in Ada type"));
14757
14758 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14759 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14760 TYPE_ALLOC (type,
14761 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14762
14763 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14764 {
14765 struct fnfieldlist &nf = fip->fnfieldlists[i];
14766 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14767
14768 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14769 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14770 fn_flp->fn_fields = (struct fn_field *)
14771 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14772
14773 for (int k = 0; k < nf.fnfields.size (); ++k)
14774 fn_flp->fn_fields[k] = nf.fnfields[k];
14775 }
14776
14777 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14778 }
14779
14780 /* Returns non-zero if NAME is the name of a vtable member in CU's
14781 language, zero otherwise. */
14782 static int
14783 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14784 {
14785 static const char vptr[] = "_vptr";
14786
14787 /* Look for the C++ form of the vtable. */
14788 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14789 return 1;
14790
14791 return 0;
14792 }
14793
14794 /* GCC outputs unnamed structures that are really pointers to member
14795 functions, with the ABI-specified layout. If TYPE describes
14796 such a structure, smash it into a member function type.
14797
14798 GCC shouldn't do this; it should just output pointer to member DIEs.
14799 This is GCC PR debug/28767. */
14800
14801 static void
14802 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14803 {
14804 struct type *pfn_type, *self_type, *new_type;
14805
14806 /* Check for a structure with no name and two children. */
14807 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14808 return;
14809
14810 /* Check for __pfn and __delta members. */
14811 if (TYPE_FIELD_NAME (type, 0) == NULL
14812 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14813 || TYPE_FIELD_NAME (type, 1) == NULL
14814 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14815 return;
14816
14817 /* Find the type of the method. */
14818 pfn_type = TYPE_FIELD_TYPE (type, 0);
14819 if (pfn_type == NULL
14820 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14821 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14822 return;
14823
14824 /* Look for the "this" argument. */
14825 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14826 if (TYPE_NFIELDS (pfn_type) == 0
14827 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14828 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14829 return;
14830
14831 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14832 new_type = alloc_type (objfile);
14833 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14834 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14835 TYPE_VARARGS (pfn_type));
14836 smash_to_methodptr_type (type, new_type);
14837 }
14838
14839 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14840 appropriate error checking and issuing complaints if there is a
14841 problem. */
14842
14843 static ULONGEST
14844 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14845 {
14846 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14847
14848 if (attr == nullptr)
14849 return 0;
14850
14851 if (!attr->form_is_constant ())
14852 {
14853 complaint (_("DW_AT_alignment must have constant form"
14854 " - DIE at %s [in module %s]"),
14855 sect_offset_str (die->sect_off),
14856 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14857 return 0;
14858 }
14859
14860 ULONGEST align;
14861 if (attr->form == DW_FORM_sdata)
14862 {
14863 LONGEST val = DW_SND (attr);
14864 if (val < 0)
14865 {
14866 complaint (_("DW_AT_alignment value must not be negative"
14867 " - DIE at %s [in module %s]"),
14868 sect_offset_str (die->sect_off),
14869 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14870 return 0;
14871 }
14872 align = val;
14873 }
14874 else
14875 align = DW_UNSND (attr);
14876
14877 if (align == 0)
14878 {
14879 complaint (_("DW_AT_alignment value must not be zero"
14880 " - DIE at %s [in module %s]"),
14881 sect_offset_str (die->sect_off),
14882 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14883 return 0;
14884 }
14885 if ((align & (align - 1)) != 0)
14886 {
14887 complaint (_("DW_AT_alignment value must be a power of 2"
14888 " - DIE at %s [in module %s]"),
14889 sect_offset_str (die->sect_off),
14890 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14891 return 0;
14892 }
14893
14894 return align;
14895 }
14896
14897 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14898 the alignment for TYPE. */
14899
14900 static void
14901 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14902 struct type *type)
14903 {
14904 if (!set_type_align (type, get_alignment (cu, die)))
14905 complaint (_("DW_AT_alignment value too large"
14906 " - DIE at %s [in module %s]"),
14907 sect_offset_str (die->sect_off),
14908 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14909 }
14910
14911 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14912 constant for a type, according to DWARF5 spec, Table 5.5. */
14913
14914 static bool
14915 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14916 {
14917 switch (value)
14918 {
14919 case DW_CC_normal:
14920 case DW_CC_pass_by_reference:
14921 case DW_CC_pass_by_value:
14922 return true;
14923
14924 default:
14925 complaint (_("unrecognized DW_AT_calling_convention value "
14926 "(%s) for a type"), pulongest (value));
14927 return false;
14928 }
14929 }
14930
14931 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14932 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14933 also according to GNU-specific values (see include/dwarf2.h). */
14934
14935 static bool
14936 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14937 {
14938 switch (value)
14939 {
14940 case DW_CC_normal:
14941 case DW_CC_program:
14942 case DW_CC_nocall:
14943 return true;
14944
14945 case DW_CC_GNU_renesas_sh:
14946 case DW_CC_GNU_borland_fastcall_i386:
14947 case DW_CC_GDB_IBM_OpenCL:
14948 return true;
14949
14950 default:
14951 complaint (_("unrecognized DW_AT_calling_convention value "
14952 "(%s) for a subroutine"), pulongest (value));
14953 return false;
14954 }
14955 }
14956
14957 /* Called when we find the DIE that starts a structure or union scope
14958 (definition) to create a type for the structure or union. Fill in
14959 the type's name and general properties; the members will not be
14960 processed until process_structure_scope. A symbol table entry for
14961 the type will also not be done until process_structure_scope (assuming
14962 the type has a name).
14963
14964 NOTE: we need to call these functions regardless of whether or not the
14965 DIE has a DW_AT_name attribute, since it might be an anonymous
14966 structure or union. This gets the type entered into our set of
14967 user defined types. */
14968
14969 static struct type *
14970 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14971 {
14972 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14973 struct type *type;
14974 struct attribute *attr;
14975 const char *name;
14976
14977 /* If the definition of this type lives in .debug_types, read that type.
14978 Don't follow DW_AT_specification though, that will take us back up
14979 the chain and we want to go down. */
14980 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
14981 if (attr != nullptr)
14982 {
14983 type = get_DW_AT_signature_type (die, attr, cu);
14984
14985 /* The type's CU may not be the same as CU.
14986 Ensure TYPE is recorded with CU in die_type_hash. */
14987 return set_die_type (die, type, cu);
14988 }
14989
14990 type = alloc_type (objfile);
14991 INIT_CPLUS_SPECIFIC (type);
14992
14993 name = dwarf2_name (die, cu);
14994 if (name != NULL)
14995 {
14996 if (cu->language == language_cplus
14997 || cu->language == language_d
14998 || cu->language == language_rust)
14999 {
15000 const char *full_name = dwarf2_full_name (name, die, cu);
15001
15002 /* dwarf2_full_name might have already finished building the DIE's
15003 type. If so, there is no need to continue. */
15004 if (get_die_type (die, cu) != NULL)
15005 return get_die_type (die, cu);
15006
15007 TYPE_NAME (type) = full_name;
15008 }
15009 else
15010 {
15011 /* The name is already allocated along with this objfile, so
15012 we don't need to duplicate it for the type. */
15013 TYPE_NAME (type) = name;
15014 }
15015 }
15016
15017 if (die->tag == DW_TAG_structure_type)
15018 {
15019 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15020 }
15021 else if (die->tag == DW_TAG_union_type)
15022 {
15023 TYPE_CODE (type) = TYPE_CODE_UNION;
15024 }
15025 else if (die->tag == DW_TAG_variant_part)
15026 {
15027 TYPE_CODE (type) = TYPE_CODE_UNION;
15028 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15029 }
15030 else
15031 {
15032 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15033 }
15034
15035 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15036 TYPE_DECLARED_CLASS (type) = 1;
15037
15038 /* Store the calling convention in the type if it's available in
15039 the die. Otherwise the calling convention remains set to
15040 the default value DW_CC_normal. */
15041 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15042 if (attr != nullptr
15043 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15044 {
15045 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15046 TYPE_CPLUS_CALLING_CONVENTION (type)
15047 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15048 }
15049
15050 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15051 if (attr != nullptr)
15052 {
15053 if (attr->form_is_constant ())
15054 TYPE_LENGTH (type) = DW_UNSND (attr);
15055 else
15056 {
15057 /* For the moment, dynamic type sizes are not supported
15058 by GDB's struct type. The actual size is determined
15059 on-demand when resolving the type of a given object,
15060 so set the type's length to zero for now. Otherwise,
15061 we record an expression as the length, and that expression
15062 could lead to a very large value, which could eventually
15063 lead to us trying to allocate that much memory when creating
15064 a value of that type. */
15065 TYPE_LENGTH (type) = 0;
15066 }
15067 }
15068 else
15069 {
15070 TYPE_LENGTH (type) = 0;
15071 }
15072
15073 maybe_set_alignment (cu, die, type);
15074
15075 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15076 {
15077 /* ICC<14 does not output the required DW_AT_declaration on
15078 incomplete types, but gives them a size of zero. */
15079 TYPE_STUB (type) = 1;
15080 }
15081 else
15082 TYPE_STUB_SUPPORTED (type) = 1;
15083
15084 if (die_is_declaration (die, cu))
15085 TYPE_STUB (type) = 1;
15086 else if (attr == NULL && die->child == NULL
15087 && producer_is_realview (cu->producer))
15088 /* RealView does not output the required DW_AT_declaration
15089 on incomplete types. */
15090 TYPE_STUB (type) = 1;
15091
15092 /* We need to add the type field to the die immediately so we don't
15093 infinitely recurse when dealing with pointers to the structure
15094 type within the structure itself. */
15095 set_die_type (die, type, cu);
15096
15097 /* set_die_type should be already done. */
15098 set_descriptive_type (type, die, cu);
15099
15100 return type;
15101 }
15102
15103 /* A helper for process_structure_scope that handles a single member
15104 DIE. */
15105
15106 static void
15107 handle_struct_member_die (struct die_info *child_die, struct type *type,
15108 struct field_info *fi,
15109 std::vector<struct symbol *> *template_args,
15110 struct dwarf2_cu *cu)
15111 {
15112 if (child_die->tag == DW_TAG_member
15113 || child_die->tag == DW_TAG_variable
15114 || child_die->tag == DW_TAG_variant_part)
15115 {
15116 /* NOTE: carlton/2002-11-05: A C++ static data member
15117 should be a DW_TAG_member that is a declaration, but
15118 all versions of G++ as of this writing (so through at
15119 least 3.2.1) incorrectly generate DW_TAG_variable
15120 tags for them instead. */
15121 dwarf2_add_field (fi, child_die, cu);
15122 }
15123 else if (child_die->tag == DW_TAG_subprogram)
15124 {
15125 /* Rust doesn't have member functions in the C++ sense.
15126 However, it does emit ordinary functions as children
15127 of a struct DIE. */
15128 if (cu->language == language_rust)
15129 read_func_scope (child_die, cu);
15130 else
15131 {
15132 /* C++ member function. */
15133 dwarf2_add_member_fn (fi, child_die, type, cu);
15134 }
15135 }
15136 else if (child_die->tag == DW_TAG_inheritance)
15137 {
15138 /* C++ base class field. */
15139 dwarf2_add_field (fi, child_die, cu);
15140 }
15141 else if (type_can_define_types (child_die))
15142 dwarf2_add_type_defn (fi, child_die, cu);
15143 else if (child_die->tag == DW_TAG_template_type_param
15144 || child_die->tag == DW_TAG_template_value_param)
15145 {
15146 struct symbol *arg = new_symbol (child_die, NULL, cu);
15147
15148 if (arg != NULL)
15149 template_args->push_back (arg);
15150 }
15151 else if (child_die->tag == DW_TAG_variant)
15152 {
15153 /* In a variant we want to get the discriminant and also add a
15154 field for our sole member child. */
15155 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15156
15157 for (die_info *variant_child = child_die->child;
15158 variant_child != NULL;
15159 variant_child = sibling_die (variant_child))
15160 {
15161 if (variant_child->tag == DW_TAG_member)
15162 {
15163 handle_struct_member_die (variant_child, type, fi,
15164 template_args, cu);
15165 /* Only handle the one. */
15166 break;
15167 }
15168 }
15169
15170 /* We don't handle this but we might as well report it if we see
15171 it. */
15172 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15173 complaint (_("DW_AT_discr_list is not supported yet"
15174 " - DIE at %s [in module %s]"),
15175 sect_offset_str (child_die->sect_off),
15176 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15177
15178 /* The first field was just added, so we can stash the
15179 discriminant there. */
15180 gdb_assert (!fi->fields.empty ());
15181 if (discr == NULL)
15182 fi->fields.back ().variant.default_branch = true;
15183 else
15184 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15185 }
15186 }
15187
15188 /* Finish creating a structure or union type, including filling in
15189 its members and creating a symbol for it. */
15190
15191 static void
15192 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15193 {
15194 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15195 struct die_info *child_die;
15196 struct type *type;
15197
15198 type = get_die_type (die, cu);
15199 if (type == NULL)
15200 type = read_structure_type (die, cu);
15201
15202 /* When reading a DW_TAG_variant_part, we need to notice when we
15203 read the discriminant member, so we can record it later in the
15204 discriminant_info. */
15205 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15206 sect_offset discr_offset {};
15207 bool has_template_parameters = false;
15208
15209 if (is_variant_part)
15210 {
15211 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15212 if (discr == NULL)
15213 {
15214 /* Maybe it's a univariant form, an extension we support.
15215 In this case arrange not to check the offset. */
15216 is_variant_part = false;
15217 }
15218 else if (discr->form_is_ref ())
15219 {
15220 struct dwarf2_cu *target_cu = cu;
15221 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15222
15223 discr_offset = target_die->sect_off;
15224 }
15225 else
15226 {
15227 complaint (_("DW_AT_discr does not have DIE reference form"
15228 " - DIE at %s [in module %s]"),
15229 sect_offset_str (die->sect_off),
15230 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15231 is_variant_part = false;
15232 }
15233 }
15234
15235 if (die->child != NULL && ! die_is_declaration (die, cu))
15236 {
15237 struct field_info fi;
15238 std::vector<struct symbol *> template_args;
15239
15240 child_die = die->child;
15241
15242 while (child_die && child_die->tag)
15243 {
15244 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15245
15246 if (is_variant_part && discr_offset == child_die->sect_off)
15247 fi.fields.back ().variant.is_discriminant = true;
15248
15249 child_die = sibling_die (child_die);
15250 }
15251
15252 /* Attach template arguments to type. */
15253 if (!template_args.empty ())
15254 {
15255 has_template_parameters = true;
15256 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15257 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15258 TYPE_TEMPLATE_ARGUMENTS (type)
15259 = XOBNEWVEC (&objfile->objfile_obstack,
15260 struct symbol *,
15261 TYPE_N_TEMPLATE_ARGUMENTS (type));
15262 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15263 template_args.data (),
15264 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15265 * sizeof (struct symbol *)));
15266 }
15267
15268 /* Attach fields and member functions to the type. */
15269 if (fi.nfields () > 0)
15270 dwarf2_attach_fields_to_type (&fi, type, cu);
15271 if (!fi.fnfieldlists.empty ())
15272 {
15273 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15274
15275 /* Get the type which refers to the base class (possibly this
15276 class itself) which contains the vtable pointer for the current
15277 class from the DW_AT_containing_type attribute. This use of
15278 DW_AT_containing_type is a GNU extension. */
15279
15280 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15281 {
15282 struct type *t = die_containing_type (die, cu);
15283
15284 set_type_vptr_basetype (type, t);
15285 if (type == t)
15286 {
15287 int i;
15288
15289 /* Our own class provides vtbl ptr. */
15290 for (i = TYPE_NFIELDS (t) - 1;
15291 i >= TYPE_N_BASECLASSES (t);
15292 --i)
15293 {
15294 const char *fieldname = TYPE_FIELD_NAME (t, i);
15295
15296 if (is_vtable_name (fieldname, cu))
15297 {
15298 set_type_vptr_fieldno (type, i);
15299 break;
15300 }
15301 }
15302
15303 /* Complain if virtual function table field not found. */
15304 if (i < TYPE_N_BASECLASSES (t))
15305 complaint (_("virtual function table pointer "
15306 "not found when defining class '%s'"),
15307 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15308 }
15309 else
15310 {
15311 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15312 }
15313 }
15314 else if (cu->producer
15315 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15316 {
15317 /* The IBM XLC compiler does not provide direct indication
15318 of the containing type, but the vtable pointer is
15319 always named __vfp. */
15320
15321 int i;
15322
15323 for (i = TYPE_NFIELDS (type) - 1;
15324 i >= TYPE_N_BASECLASSES (type);
15325 --i)
15326 {
15327 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15328 {
15329 set_type_vptr_fieldno (type, i);
15330 set_type_vptr_basetype (type, type);
15331 break;
15332 }
15333 }
15334 }
15335 }
15336
15337 /* Copy fi.typedef_field_list linked list elements content into the
15338 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15339 if (!fi.typedef_field_list.empty ())
15340 {
15341 int count = fi.typedef_field_list.size ();
15342
15343 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15344 TYPE_TYPEDEF_FIELD_ARRAY (type)
15345 = ((struct decl_field *)
15346 TYPE_ALLOC (type,
15347 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15348 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15349
15350 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15351 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15352 }
15353
15354 /* Copy fi.nested_types_list linked list elements content into the
15355 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15356 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15357 {
15358 int count = fi.nested_types_list.size ();
15359
15360 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15361 TYPE_NESTED_TYPES_ARRAY (type)
15362 = ((struct decl_field *)
15363 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15364 TYPE_NESTED_TYPES_COUNT (type) = count;
15365
15366 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15367 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15368 }
15369 }
15370
15371 quirk_gcc_member_function_pointer (type, objfile);
15372 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15373 cu->rust_unions.push_back (type);
15374
15375 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15376 snapshots) has been known to create a die giving a declaration
15377 for a class that has, as a child, a die giving a definition for a
15378 nested class. So we have to process our children even if the
15379 current die is a declaration. Normally, of course, a declaration
15380 won't have any children at all. */
15381
15382 child_die = die->child;
15383
15384 while (child_die != NULL && child_die->tag)
15385 {
15386 if (child_die->tag == DW_TAG_member
15387 || child_die->tag == DW_TAG_variable
15388 || child_die->tag == DW_TAG_inheritance
15389 || child_die->tag == DW_TAG_template_value_param
15390 || child_die->tag == DW_TAG_template_type_param)
15391 {
15392 /* Do nothing. */
15393 }
15394 else
15395 process_die (child_die, cu);
15396
15397 child_die = sibling_die (child_die);
15398 }
15399
15400 /* Do not consider external references. According to the DWARF standard,
15401 these DIEs are identified by the fact that they have no byte_size
15402 attribute, and a declaration attribute. */
15403 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15404 || !die_is_declaration (die, cu))
15405 {
15406 struct symbol *sym = new_symbol (die, type, cu);
15407
15408 if (has_template_parameters)
15409 {
15410 struct symtab *symtab;
15411 if (sym != nullptr)
15412 symtab = symbol_symtab (sym);
15413 else if (cu->line_header != nullptr)
15414 {
15415 /* Any related symtab will do. */
15416 symtab
15417 = cu->line_header->file_names ()[0].symtab;
15418 }
15419 else
15420 {
15421 symtab = nullptr;
15422 complaint (_("could not find suitable "
15423 "symtab for template parameter"
15424 " - DIE at %s [in module %s]"),
15425 sect_offset_str (die->sect_off),
15426 objfile_name (objfile));
15427 }
15428
15429 if (symtab != nullptr)
15430 {
15431 /* Make sure that the symtab is set on the new symbols.
15432 Even though they don't appear in this symtab directly,
15433 other parts of gdb assume that symbols do, and this is
15434 reasonably true. */
15435 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15436 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15437 }
15438 }
15439 }
15440 }
15441
15442 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15443 update TYPE using some information only available in DIE's children. */
15444
15445 static void
15446 update_enumeration_type_from_children (struct die_info *die,
15447 struct type *type,
15448 struct dwarf2_cu *cu)
15449 {
15450 struct die_info *child_die;
15451 int unsigned_enum = 1;
15452 int flag_enum = 1;
15453
15454 auto_obstack obstack;
15455
15456 for (child_die = die->child;
15457 child_die != NULL && child_die->tag;
15458 child_die = sibling_die (child_die))
15459 {
15460 struct attribute *attr;
15461 LONGEST value;
15462 const gdb_byte *bytes;
15463 struct dwarf2_locexpr_baton *baton;
15464 const char *name;
15465
15466 if (child_die->tag != DW_TAG_enumerator)
15467 continue;
15468
15469 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15470 if (attr == NULL)
15471 continue;
15472
15473 name = dwarf2_name (child_die, cu);
15474 if (name == NULL)
15475 name = "<anonymous enumerator>";
15476
15477 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15478 &value, &bytes, &baton);
15479 if (value < 0)
15480 {
15481 unsigned_enum = 0;
15482 flag_enum = 0;
15483 }
15484 else
15485 {
15486 if (count_one_bits_ll (value) >= 2)
15487 flag_enum = 0;
15488 }
15489
15490 /* If we already know that the enum type is neither unsigned, nor
15491 a flag type, no need to look at the rest of the enumerates. */
15492 if (!unsigned_enum && !flag_enum)
15493 break;
15494 }
15495
15496 if (unsigned_enum)
15497 TYPE_UNSIGNED (type) = 1;
15498 if (flag_enum)
15499 TYPE_FLAG_ENUM (type) = 1;
15500 }
15501
15502 /* Given a DW_AT_enumeration_type die, set its type. We do not
15503 complete the type's fields yet, or create any symbols. */
15504
15505 static struct type *
15506 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15507 {
15508 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15509 struct type *type;
15510 struct attribute *attr;
15511 const char *name;
15512
15513 /* If the definition of this type lives in .debug_types, read that type.
15514 Don't follow DW_AT_specification though, that will take us back up
15515 the chain and we want to go down. */
15516 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15517 if (attr != nullptr)
15518 {
15519 type = get_DW_AT_signature_type (die, attr, cu);
15520
15521 /* The type's CU may not be the same as CU.
15522 Ensure TYPE is recorded with CU in die_type_hash. */
15523 return set_die_type (die, type, cu);
15524 }
15525
15526 type = alloc_type (objfile);
15527
15528 TYPE_CODE (type) = TYPE_CODE_ENUM;
15529 name = dwarf2_full_name (NULL, die, cu);
15530 if (name != NULL)
15531 TYPE_NAME (type) = name;
15532
15533 attr = dwarf2_attr (die, DW_AT_type, cu);
15534 if (attr != NULL)
15535 {
15536 struct type *underlying_type = die_type (die, cu);
15537
15538 TYPE_TARGET_TYPE (type) = underlying_type;
15539 }
15540
15541 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15542 if (attr != nullptr)
15543 {
15544 TYPE_LENGTH (type) = DW_UNSND (attr);
15545 }
15546 else
15547 {
15548 TYPE_LENGTH (type) = 0;
15549 }
15550
15551 maybe_set_alignment (cu, die, type);
15552
15553 /* The enumeration DIE can be incomplete. In Ada, any type can be
15554 declared as private in the package spec, and then defined only
15555 inside the package body. Such types are known as Taft Amendment
15556 Types. When another package uses such a type, an incomplete DIE
15557 may be generated by the compiler. */
15558 if (die_is_declaration (die, cu))
15559 TYPE_STUB (type) = 1;
15560
15561 /* Finish the creation of this type by using the enum's children.
15562 We must call this even when the underlying type has been provided
15563 so that we can determine if we're looking at a "flag" enum. */
15564 update_enumeration_type_from_children (die, type, cu);
15565
15566 /* If this type has an underlying type that is not a stub, then we
15567 may use its attributes. We always use the "unsigned" attribute
15568 in this situation, because ordinarily we guess whether the type
15569 is unsigned -- but the guess can be wrong and the underlying type
15570 can tell us the reality. However, we defer to a local size
15571 attribute if one exists, because this lets the compiler override
15572 the underlying type if needed. */
15573 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15574 {
15575 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15576 if (TYPE_LENGTH (type) == 0)
15577 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15578 if (TYPE_RAW_ALIGN (type) == 0
15579 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15580 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15581 }
15582
15583 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15584
15585 return set_die_type (die, type, cu);
15586 }
15587
15588 /* Given a pointer to a die which begins an enumeration, process all
15589 the dies that define the members of the enumeration, and create the
15590 symbol for the enumeration type.
15591
15592 NOTE: We reverse the order of the element list. */
15593
15594 static void
15595 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15596 {
15597 struct type *this_type;
15598
15599 this_type = get_die_type (die, cu);
15600 if (this_type == NULL)
15601 this_type = read_enumeration_type (die, cu);
15602
15603 if (die->child != NULL)
15604 {
15605 struct die_info *child_die;
15606 struct symbol *sym;
15607 std::vector<struct field> fields;
15608 const char *name;
15609
15610 child_die = die->child;
15611 while (child_die && child_die->tag)
15612 {
15613 if (child_die->tag != DW_TAG_enumerator)
15614 {
15615 process_die (child_die, cu);
15616 }
15617 else
15618 {
15619 name = dwarf2_name (child_die, cu);
15620 if (name)
15621 {
15622 sym = new_symbol (child_die, this_type, cu);
15623
15624 fields.emplace_back ();
15625 struct field &field = fields.back ();
15626
15627 FIELD_NAME (field) = sym->linkage_name ();
15628 FIELD_TYPE (field) = NULL;
15629 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15630 FIELD_BITSIZE (field) = 0;
15631 }
15632 }
15633
15634 child_die = sibling_die (child_die);
15635 }
15636
15637 if (!fields.empty ())
15638 {
15639 TYPE_NFIELDS (this_type) = fields.size ();
15640 TYPE_FIELDS (this_type) = (struct field *)
15641 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15642 memcpy (TYPE_FIELDS (this_type), fields.data (),
15643 sizeof (struct field) * fields.size ());
15644 }
15645 }
15646
15647 /* If we are reading an enum from a .debug_types unit, and the enum
15648 is a declaration, and the enum is not the signatured type in the
15649 unit, then we do not want to add a symbol for it. Adding a
15650 symbol would in some cases obscure the true definition of the
15651 enum, giving users an incomplete type when the definition is
15652 actually available. Note that we do not want to do this for all
15653 enums which are just declarations, because C++0x allows forward
15654 enum declarations. */
15655 if (cu->per_cu->is_debug_types
15656 && die_is_declaration (die, cu))
15657 {
15658 struct signatured_type *sig_type;
15659
15660 sig_type = (struct signatured_type *) cu->per_cu;
15661 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15662 if (sig_type->type_offset_in_section != die->sect_off)
15663 return;
15664 }
15665
15666 new_symbol (die, this_type, cu);
15667 }
15668
15669 /* Extract all information from a DW_TAG_array_type DIE and put it in
15670 the DIE's type field. For now, this only handles one dimensional
15671 arrays. */
15672
15673 static struct type *
15674 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15675 {
15676 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15677 struct die_info *child_die;
15678 struct type *type;
15679 struct type *element_type, *range_type, *index_type;
15680 struct attribute *attr;
15681 const char *name;
15682 struct dynamic_prop *byte_stride_prop = NULL;
15683 unsigned int bit_stride = 0;
15684
15685 element_type = die_type (die, cu);
15686
15687 /* The die_type call above may have already set the type for this DIE. */
15688 type = get_die_type (die, cu);
15689 if (type)
15690 return type;
15691
15692 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15693 if (attr != NULL)
15694 {
15695 int stride_ok;
15696 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15697
15698 byte_stride_prop
15699 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15700 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15701 prop_type);
15702 if (!stride_ok)
15703 {
15704 complaint (_("unable to read array DW_AT_byte_stride "
15705 " - DIE at %s [in module %s]"),
15706 sect_offset_str (die->sect_off),
15707 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15708 /* Ignore this attribute. We will likely not be able to print
15709 arrays of this type correctly, but there is little we can do
15710 to help if we cannot read the attribute's value. */
15711 byte_stride_prop = NULL;
15712 }
15713 }
15714
15715 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15716 if (attr != NULL)
15717 bit_stride = DW_UNSND (attr);
15718
15719 /* Irix 6.2 native cc creates array types without children for
15720 arrays with unspecified length. */
15721 if (die->child == NULL)
15722 {
15723 index_type = objfile_type (objfile)->builtin_int;
15724 range_type = create_static_range_type (NULL, index_type, 0, -1);
15725 type = create_array_type_with_stride (NULL, element_type, range_type,
15726 byte_stride_prop, bit_stride);
15727 return set_die_type (die, type, cu);
15728 }
15729
15730 std::vector<struct type *> range_types;
15731 child_die = die->child;
15732 while (child_die && child_die->tag)
15733 {
15734 if (child_die->tag == DW_TAG_subrange_type)
15735 {
15736 struct type *child_type = read_type_die (child_die, cu);
15737
15738 if (child_type != NULL)
15739 {
15740 /* The range type was succesfully read. Save it for the
15741 array type creation. */
15742 range_types.push_back (child_type);
15743 }
15744 }
15745 child_die = sibling_die (child_die);
15746 }
15747
15748 /* Dwarf2 dimensions are output from left to right, create the
15749 necessary array types in backwards order. */
15750
15751 type = element_type;
15752
15753 if (read_array_order (die, cu) == DW_ORD_col_major)
15754 {
15755 int i = 0;
15756
15757 while (i < range_types.size ())
15758 type = create_array_type_with_stride (NULL, type, range_types[i++],
15759 byte_stride_prop, bit_stride);
15760 }
15761 else
15762 {
15763 size_t ndim = range_types.size ();
15764 while (ndim-- > 0)
15765 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15766 byte_stride_prop, bit_stride);
15767 }
15768
15769 /* Understand Dwarf2 support for vector types (like they occur on
15770 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15771 array type. This is not part of the Dwarf2/3 standard yet, but a
15772 custom vendor extension. The main difference between a regular
15773 array and the vector variant is that vectors are passed by value
15774 to functions. */
15775 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15776 if (attr != nullptr)
15777 make_vector_type (type);
15778
15779 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15780 implementation may choose to implement triple vectors using this
15781 attribute. */
15782 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15783 if (attr != nullptr)
15784 {
15785 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15786 TYPE_LENGTH (type) = DW_UNSND (attr);
15787 else
15788 complaint (_("DW_AT_byte_size for array type smaller "
15789 "than the total size of elements"));
15790 }
15791
15792 name = dwarf2_name (die, cu);
15793 if (name)
15794 TYPE_NAME (type) = name;
15795
15796 maybe_set_alignment (cu, die, type);
15797
15798 /* Install the type in the die. */
15799 set_die_type (die, type, cu);
15800
15801 /* set_die_type should be already done. */
15802 set_descriptive_type (type, die, cu);
15803
15804 return type;
15805 }
15806
15807 static enum dwarf_array_dim_ordering
15808 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15809 {
15810 struct attribute *attr;
15811
15812 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15813
15814 if (attr != nullptr)
15815 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15816
15817 /* GNU F77 is a special case, as at 08/2004 array type info is the
15818 opposite order to the dwarf2 specification, but data is still
15819 laid out as per normal fortran.
15820
15821 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15822 version checking. */
15823
15824 if (cu->language == language_fortran
15825 && cu->producer && strstr (cu->producer, "GNU F77"))
15826 {
15827 return DW_ORD_row_major;
15828 }
15829
15830 switch (cu->language_defn->la_array_ordering)
15831 {
15832 case array_column_major:
15833 return DW_ORD_col_major;
15834 case array_row_major:
15835 default:
15836 return DW_ORD_row_major;
15837 };
15838 }
15839
15840 /* Extract all information from a DW_TAG_set_type DIE and put it in
15841 the DIE's type field. */
15842
15843 static struct type *
15844 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15845 {
15846 struct type *domain_type, *set_type;
15847 struct attribute *attr;
15848
15849 domain_type = die_type (die, cu);
15850
15851 /* The die_type call above may have already set the type for this DIE. */
15852 set_type = get_die_type (die, cu);
15853 if (set_type)
15854 return set_type;
15855
15856 set_type = create_set_type (NULL, domain_type);
15857
15858 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15859 if (attr != nullptr)
15860 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15861
15862 maybe_set_alignment (cu, die, set_type);
15863
15864 return set_die_type (die, set_type, cu);
15865 }
15866
15867 /* A helper for read_common_block that creates a locexpr baton.
15868 SYM is the symbol which we are marking as computed.
15869 COMMON_DIE is the DIE for the common block.
15870 COMMON_LOC is the location expression attribute for the common
15871 block itself.
15872 MEMBER_LOC is the location expression attribute for the particular
15873 member of the common block that we are processing.
15874 CU is the CU from which the above come. */
15875
15876 static void
15877 mark_common_block_symbol_computed (struct symbol *sym,
15878 struct die_info *common_die,
15879 struct attribute *common_loc,
15880 struct attribute *member_loc,
15881 struct dwarf2_cu *cu)
15882 {
15883 struct dwarf2_per_objfile *dwarf2_per_objfile
15884 = cu->per_cu->dwarf2_per_objfile;
15885 struct objfile *objfile = dwarf2_per_objfile->objfile;
15886 struct dwarf2_locexpr_baton *baton;
15887 gdb_byte *ptr;
15888 unsigned int cu_off;
15889 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15890 LONGEST offset = 0;
15891
15892 gdb_assert (common_loc && member_loc);
15893 gdb_assert (common_loc->form_is_block ());
15894 gdb_assert (member_loc->form_is_block ()
15895 || member_loc->form_is_constant ());
15896
15897 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15898 baton->per_cu = cu->per_cu;
15899 gdb_assert (baton->per_cu);
15900
15901 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15902
15903 if (member_loc->form_is_constant ())
15904 {
15905 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15906 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15907 }
15908 else
15909 baton->size += DW_BLOCK (member_loc)->size;
15910
15911 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15912 baton->data = ptr;
15913
15914 *ptr++ = DW_OP_call4;
15915 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15916 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15917 ptr += 4;
15918
15919 if (member_loc->form_is_constant ())
15920 {
15921 *ptr++ = DW_OP_addr;
15922 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15923 ptr += cu->header.addr_size;
15924 }
15925 else
15926 {
15927 /* We have to copy the data here, because DW_OP_call4 will only
15928 use a DW_AT_location attribute. */
15929 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15930 ptr += DW_BLOCK (member_loc)->size;
15931 }
15932
15933 *ptr++ = DW_OP_plus;
15934 gdb_assert (ptr - baton->data == baton->size);
15935
15936 SYMBOL_LOCATION_BATON (sym) = baton;
15937 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15938 }
15939
15940 /* Create appropriate locally-scoped variables for all the
15941 DW_TAG_common_block entries. Also create a struct common_block
15942 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15943 is used to separate the common blocks name namespace from regular
15944 variable names. */
15945
15946 static void
15947 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15948 {
15949 struct attribute *attr;
15950
15951 attr = dwarf2_attr (die, DW_AT_location, cu);
15952 if (attr != nullptr)
15953 {
15954 /* Support the .debug_loc offsets. */
15955 if (attr->form_is_block ())
15956 {
15957 /* Ok. */
15958 }
15959 else if (attr->form_is_section_offset ())
15960 {
15961 dwarf2_complex_location_expr_complaint ();
15962 attr = NULL;
15963 }
15964 else
15965 {
15966 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15967 "common block member");
15968 attr = NULL;
15969 }
15970 }
15971
15972 if (die->child != NULL)
15973 {
15974 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15975 struct die_info *child_die;
15976 size_t n_entries = 0, size;
15977 struct common_block *common_block;
15978 struct symbol *sym;
15979
15980 for (child_die = die->child;
15981 child_die && child_die->tag;
15982 child_die = sibling_die (child_die))
15983 ++n_entries;
15984
15985 size = (sizeof (struct common_block)
15986 + (n_entries - 1) * sizeof (struct symbol *));
15987 common_block
15988 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15989 size);
15990 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15991 common_block->n_entries = 0;
15992
15993 for (child_die = die->child;
15994 child_die && child_die->tag;
15995 child_die = sibling_die (child_die))
15996 {
15997 /* Create the symbol in the DW_TAG_common_block block in the current
15998 symbol scope. */
15999 sym = new_symbol (child_die, NULL, cu);
16000 if (sym != NULL)
16001 {
16002 struct attribute *member_loc;
16003
16004 common_block->contents[common_block->n_entries++] = sym;
16005
16006 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16007 cu);
16008 if (member_loc)
16009 {
16010 /* GDB has handled this for a long time, but it is
16011 not specified by DWARF. It seems to have been
16012 emitted by gfortran at least as recently as:
16013 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16014 complaint (_("Variable in common block has "
16015 "DW_AT_data_member_location "
16016 "- DIE at %s [in module %s]"),
16017 sect_offset_str (child_die->sect_off),
16018 objfile_name (objfile));
16019
16020 if (member_loc->form_is_section_offset ())
16021 dwarf2_complex_location_expr_complaint ();
16022 else if (member_loc->form_is_constant ()
16023 || member_loc->form_is_block ())
16024 {
16025 if (attr != nullptr)
16026 mark_common_block_symbol_computed (sym, die, attr,
16027 member_loc, cu);
16028 }
16029 else
16030 dwarf2_complex_location_expr_complaint ();
16031 }
16032 }
16033 }
16034
16035 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16036 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16037 }
16038 }
16039
16040 /* Create a type for a C++ namespace. */
16041
16042 static struct type *
16043 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16044 {
16045 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16046 const char *previous_prefix, *name;
16047 int is_anonymous;
16048 struct type *type;
16049
16050 /* For extensions, reuse the type of the original namespace. */
16051 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16052 {
16053 struct die_info *ext_die;
16054 struct dwarf2_cu *ext_cu = cu;
16055
16056 ext_die = dwarf2_extension (die, &ext_cu);
16057 type = read_type_die (ext_die, ext_cu);
16058
16059 /* EXT_CU may not be the same as CU.
16060 Ensure TYPE is recorded with CU in die_type_hash. */
16061 return set_die_type (die, type, cu);
16062 }
16063
16064 name = namespace_name (die, &is_anonymous, cu);
16065
16066 /* Now build the name of the current namespace. */
16067
16068 previous_prefix = determine_prefix (die, cu);
16069 if (previous_prefix[0] != '\0')
16070 name = typename_concat (&objfile->objfile_obstack,
16071 previous_prefix, name, 0, cu);
16072
16073 /* Create the type. */
16074 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16075
16076 return set_die_type (die, type, cu);
16077 }
16078
16079 /* Read a namespace scope. */
16080
16081 static void
16082 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16083 {
16084 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16085 int is_anonymous;
16086
16087 /* Add a symbol associated to this if we haven't seen the namespace
16088 before. Also, add a using directive if it's an anonymous
16089 namespace. */
16090
16091 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16092 {
16093 struct type *type;
16094
16095 type = read_type_die (die, cu);
16096 new_symbol (die, type, cu);
16097
16098 namespace_name (die, &is_anonymous, cu);
16099 if (is_anonymous)
16100 {
16101 const char *previous_prefix = determine_prefix (die, cu);
16102
16103 std::vector<const char *> excludes;
16104 add_using_directive (using_directives (cu),
16105 previous_prefix, TYPE_NAME (type), NULL,
16106 NULL, excludes, 0, &objfile->objfile_obstack);
16107 }
16108 }
16109
16110 if (die->child != NULL)
16111 {
16112 struct die_info *child_die = die->child;
16113
16114 while (child_die && child_die->tag)
16115 {
16116 process_die (child_die, cu);
16117 child_die = sibling_die (child_die);
16118 }
16119 }
16120 }
16121
16122 /* Read a Fortran module as type. This DIE can be only a declaration used for
16123 imported module. Still we need that type as local Fortran "use ... only"
16124 declaration imports depend on the created type in determine_prefix. */
16125
16126 static struct type *
16127 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16128 {
16129 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16130 const char *module_name;
16131 struct type *type;
16132
16133 module_name = dwarf2_name (die, cu);
16134 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16135
16136 return set_die_type (die, type, cu);
16137 }
16138
16139 /* Read a Fortran module. */
16140
16141 static void
16142 read_module (struct die_info *die, struct dwarf2_cu *cu)
16143 {
16144 struct die_info *child_die = die->child;
16145 struct type *type;
16146
16147 type = read_type_die (die, cu);
16148 new_symbol (die, type, cu);
16149
16150 while (child_die && child_die->tag)
16151 {
16152 process_die (child_die, cu);
16153 child_die = sibling_die (child_die);
16154 }
16155 }
16156
16157 /* Return the name of the namespace represented by DIE. Set
16158 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16159 namespace. */
16160
16161 static const char *
16162 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16163 {
16164 struct die_info *current_die;
16165 const char *name = NULL;
16166
16167 /* Loop through the extensions until we find a name. */
16168
16169 for (current_die = die;
16170 current_die != NULL;
16171 current_die = dwarf2_extension (die, &cu))
16172 {
16173 /* We don't use dwarf2_name here so that we can detect the absence
16174 of a name -> anonymous namespace. */
16175 name = dwarf2_string_attr (die, DW_AT_name, cu);
16176
16177 if (name != NULL)
16178 break;
16179 }
16180
16181 /* Is it an anonymous namespace? */
16182
16183 *is_anonymous = (name == NULL);
16184 if (*is_anonymous)
16185 name = CP_ANONYMOUS_NAMESPACE_STR;
16186
16187 return name;
16188 }
16189
16190 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16191 the user defined type vector. */
16192
16193 static struct type *
16194 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16195 {
16196 struct gdbarch *gdbarch
16197 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16198 struct comp_unit_head *cu_header = &cu->header;
16199 struct type *type;
16200 struct attribute *attr_byte_size;
16201 struct attribute *attr_address_class;
16202 int byte_size, addr_class;
16203 struct type *target_type;
16204
16205 target_type = die_type (die, cu);
16206
16207 /* The die_type call above may have already set the type for this DIE. */
16208 type = get_die_type (die, cu);
16209 if (type)
16210 return type;
16211
16212 type = lookup_pointer_type (target_type);
16213
16214 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16215 if (attr_byte_size)
16216 byte_size = DW_UNSND (attr_byte_size);
16217 else
16218 byte_size = cu_header->addr_size;
16219
16220 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16221 if (attr_address_class)
16222 addr_class = DW_UNSND (attr_address_class);
16223 else
16224 addr_class = DW_ADDR_none;
16225
16226 ULONGEST alignment = get_alignment (cu, die);
16227
16228 /* If the pointer size, alignment, or address class is different
16229 than the default, create a type variant marked as such and set
16230 the length accordingly. */
16231 if (TYPE_LENGTH (type) != byte_size
16232 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16233 && alignment != TYPE_RAW_ALIGN (type))
16234 || addr_class != DW_ADDR_none)
16235 {
16236 if (gdbarch_address_class_type_flags_p (gdbarch))
16237 {
16238 int type_flags;
16239
16240 type_flags = gdbarch_address_class_type_flags
16241 (gdbarch, byte_size, addr_class);
16242 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16243 == 0);
16244 type = make_type_with_address_space (type, type_flags);
16245 }
16246 else if (TYPE_LENGTH (type) != byte_size)
16247 {
16248 complaint (_("invalid pointer size %d"), byte_size);
16249 }
16250 else if (TYPE_RAW_ALIGN (type) != alignment)
16251 {
16252 complaint (_("Invalid DW_AT_alignment"
16253 " - DIE at %s [in module %s]"),
16254 sect_offset_str (die->sect_off),
16255 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16256 }
16257 else
16258 {
16259 /* Should we also complain about unhandled address classes? */
16260 }
16261 }
16262
16263 TYPE_LENGTH (type) = byte_size;
16264 set_type_align (type, alignment);
16265 return set_die_type (die, type, cu);
16266 }
16267
16268 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16269 the user defined type vector. */
16270
16271 static struct type *
16272 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16273 {
16274 struct type *type;
16275 struct type *to_type;
16276 struct type *domain;
16277
16278 to_type = die_type (die, cu);
16279 domain = die_containing_type (die, cu);
16280
16281 /* The calls above may have already set the type for this DIE. */
16282 type = get_die_type (die, cu);
16283 if (type)
16284 return type;
16285
16286 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16287 type = lookup_methodptr_type (to_type);
16288 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16289 {
16290 struct type *new_type
16291 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16292
16293 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16294 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16295 TYPE_VARARGS (to_type));
16296 type = lookup_methodptr_type (new_type);
16297 }
16298 else
16299 type = lookup_memberptr_type (to_type, domain);
16300
16301 return set_die_type (die, type, cu);
16302 }
16303
16304 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16305 the user defined type vector. */
16306
16307 static struct type *
16308 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16309 enum type_code refcode)
16310 {
16311 struct comp_unit_head *cu_header = &cu->header;
16312 struct type *type, *target_type;
16313 struct attribute *attr;
16314
16315 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16316
16317 target_type = die_type (die, cu);
16318
16319 /* The die_type call above may have already set the type for this DIE. */
16320 type = get_die_type (die, cu);
16321 if (type)
16322 return type;
16323
16324 type = lookup_reference_type (target_type, refcode);
16325 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16326 if (attr != nullptr)
16327 {
16328 TYPE_LENGTH (type) = DW_UNSND (attr);
16329 }
16330 else
16331 {
16332 TYPE_LENGTH (type) = cu_header->addr_size;
16333 }
16334 maybe_set_alignment (cu, die, type);
16335 return set_die_type (die, type, cu);
16336 }
16337
16338 /* Add the given cv-qualifiers to the element type of the array. GCC
16339 outputs DWARF type qualifiers that apply to an array, not the
16340 element type. But GDB relies on the array element type to carry
16341 the cv-qualifiers. This mimics section 6.7.3 of the C99
16342 specification. */
16343
16344 static struct type *
16345 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16346 struct type *base_type, int cnst, int voltl)
16347 {
16348 struct type *el_type, *inner_array;
16349
16350 base_type = copy_type (base_type);
16351 inner_array = base_type;
16352
16353 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16354 {
16355 TYPE_TARGET_TYPE (inner_array) =
16356 copy_type (TYPE_TARGET_TYPE (inner_array));
16357 inner_array = TYPE_TARGET_TYPE (inner_array);
16358 }
16359
16360 el_type = TYPE_TARGET_TYPE (inner_array);
16361 cnst |= TYPE_CONST (el_type);
16362 voltl |= TYPE_VOLATILE (el_type);
16363 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16364
16365 return set_die_type (die, base_type, cu);
16366 }
16367
16368 static struct type *
16369 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16370 {
16371 struct type *base_type, *cv_type;
16372
16373 base_type = die_type (die, cu);
16374
16375 /* The die_type call above may have already set the type for this DIE. */
16376 cv_type = get_die_type (die, cu);
16377 if (cv_type)
16378 return cv_type;
16379
16380 /* In case the const qualifier is applied to an array type, the element type
16381 is so qualified, not the array type (section 6.7.3 of C99). */
16382 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16383 return add_array_cv_type (die, cu, base_type, 1, 0);
16384
16385 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16386 return set_die_type (die, cv_type, cu);
16387 }
16388
16389 static struct type *
16390 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16391 {
16392 struct type *base_type, *cv_type;
16393
16394 base_type = die_type (die, cu);
16395
16396 /* The die_type call above may have already set the type for this DIE. */
16397 cv_type = get_die_type (die, cu);
16398 if (cv_type)
16399 return cv_type;
16400
16401 /* In case the volatile qualifier is applied to an array type, the
16402 element type is so qualified, not the array type (section 6.7.3
16403 of C99). */
16404 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16405 return add_array_cv_type (die, cu, base_type, 0, 1);
16406
16407 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16408 return set_die_type (die, cv_type, cu);
16409 }
16410
16411 /* Handle DW_TAG_restrict_type. */
16412
16413 static struct type *
16414 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16415 {
16416 struct type *base_type, *cv_type;
16417
16418 base_type = die_type (die, cu);
16419
16420 /* The die_type call above may have already set the type for this DIE. */
16421 cv_type = get_die_type (die, cu);
16422 if (cv_type)
16423 return cv_type;
16424
16425 cv_type = make_restrict_type (base_type);
16426 return set_die_type (die, cv_type, cu);
16427 }
16428
16429 /* Handle DW_TAG_atomic_type. */
16430
16431 static struct type *
16432 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16433 {
16434 struct type *base_type, *cv_type;
16435
16436 base_type = die_type (die, cu);
16437
16438 /* The die_type call above may have already set the type for this DIE. */
16439 cv_type = get_die_type (die, cu);
16440 if (cv_type)
16441 return cv_type;
16442
16443 cv_type = make_atomic_type (base_type);
16444 return set_die_type (die, cv_type, cu);
16445 }
16446
16447 /* Extract all information from a DW_TAG_string_type DIE and add to
16448 the user defined type vector. It isn't really a user defined type,
16449 but it behaves like one, with other DIE's using an AT_user_def_type
16450 attribute to reference it. */
16451
16452 static struct type *
16453 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16454 {
16455 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16456 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16457 struct type *type, *range_type, *index_type, *char_type;
16458 struct attribute *attr;
16459 struct dynamic_prop prop;
16460 bool length_is_constant = true;
16461 LONGEST length;
16462
16463 /* There are a couple of places where bit sizes might be made use of
16464 when parsing a DW_TAG_string_type, however, no producer that we know
16465 of make use of these. Handling bit sizes that are a multiple of the
16466 byte size is easy enough, but what about other bit sizes? Lets deal
16467 with that problem when we have to. Warn about these attributes being
16468 unsupported, then parse the type and ignore them like we always
16469 have. */
16470 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16471 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16472 {
16473 static bool warning_printed = false;
16474 if (!warning_printed)
16475 {
16476 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16477 "currently supported on DW_TAG_string_type."));
16478 warning_printed = true;
16479 }
16480 }
16481
16482 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16483 if (attr != nullptr && !attr->form_is_constant ())
16484 {
16485 /* The string length describes the location at which the length of
16486 the string can be found. The size of the length field can be
16487 specified with one of the attributes below. */
16488 struct type *prop_type;
16489 struct attribute *len
16490 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16491 if (len == nullptr)
16492 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16493 if (len != nullptr && len->form_is_constant ())
16494 {
16495 /* Pass 0 as the default as we know this attribute is constant
16496 and the default value will not be returned. */
16497 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16498 prop_type = cu->per_cu->int_type (sz, true);
16499 }
16500 else
16501 {
16502 /* If the size is not specified then we assume it is the size of
16503 an address on this target. */
16504 prop_type = cu->per_cu->addr_sized_int_type (true);
16505 }
16506
16507 /* Convert the attribute into a dynamic property. */
16508 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16509 length = 1;
16510 else
16511 length_is_constant = false;
16512 }
16513 else if (attr != nullptr)
16514 {
16515 /* This DW_AT_string_length just contains the length with no
16516 indirection. There's no need to create a dynamic property in this
16517 case. Pass 0 for the default value as we know it will not be
16518 returned in this case. */
16519 length = dwarf2_get_attr_constant_value (attr, 0);
16520 }
16521 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16522 {
16523 /* We don't currently support non-constant byte sizes for strings. */
16524 length = dwarf2_get_attr_constant_value (attr, 1);
16525 }
16526 else
16527 {
16528 /* Use 1 as a fallback length if we have nothing else. */
16529 length = 1;
16530 }
16531
16532 index_type = objfile_type (objfile)->builtin_int;
16533 if (length_is_constant)
16534 range_type = create_static_range_type (NULL, index_type, 1, length);
16535 else
16536 {
16537 struct dynamic_prop low_bound;
16538
16539 low_bound.kind = PROP_CONST;
16540 low_bound.data.const_val = 1;
16541 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16542 }
16543 char_type = language_string_char_type (cu->language_defn, gdbarch);
16544 type = create_string_type (NULL, char_type, range_type);
16545
16546 return set_die_type (die, type, cu);
16547 }
16548
16549 /* Assuming that DIE corresponds to a function, returns nonzero
16550 if the function is prototyped. */
16551
16552 static int
16553 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16554 {
16555 struct attribute *attr;
16556
16557 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16558 if (attr && (DW_UNSND (attr) != 0))
16559 return 1;
16560
16561 /* The DWARF standard implies that the DW_AT_prototyped attribute
16562 is only meaningful for C, but the concept also extends to other
16563 languages that allow unprototyped functions (Eg: Objective C).
16564 For all other languages, assume that functions are always
16565 prototyped. */
16566 if (cu->language != language_c
16567 && cu->language != language_objc
16568 && cu->language != language_opencl)
16569 return 1;
16570
16571 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16572 prototyped and unprototyped functions; default to prototyped,
16573 since that is more common in modern code (and RealView warns
16574 about unprototyped functions). */
16575 if (producer_is_realview (cu->producer))
16576 return 1;
16577
16578 return 0;
16579 }
16580
16581 /* Handle DIES due to C code like:
16582
16583 struct foo
16584 {
16585 int (*funcp)(int a, long l);
16586 int b;
16587 };
16588
16589 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16590
16591 static struct type *
16592 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16593 {
16594 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16595 struct type *type; /* Type that this function returns. */
16596 struct type *ftype; /* Function that returns above type. */
16597 struct attribute *attr;
16598
16599 type = die_type (die, cu);
16600
16601 /* The die_type call above may have already set the type for this DIE. */
16602 ftype = get_die_type (die, cu);
16603 if (ftype)
16604 return ftype;
16605
16606 ftype = lookup_function_type (type);
16607
16608 if (prototyped_function_p (die, cu))
16609 TYPE_PROTOTYPED (ftype) = 1;
16610
16611 /* Store the calling convention in the type if it's available in
16612 the subroutine die. Otherwise set the calling convention to
16613 the default value DW_CC_normal. */
16614 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16615 if (attr != nullptr
16616 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16617 TYPE_CALLING_CONVENTION (ftype)
16618 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16619 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16620 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16621 else
16622 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16623
16624 /* Record whether the function returns normally to its caller or not
16625 if the DWARF producer set that information. */
16626 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16627 if (attr && (DW_UNSND (attr) != 0))
16628 TYPE_NO_RETURN (ftype) = 1;
16629
16630 /* We need to add the subroutine type to the die immediately so
16631 we don't infinitely recurse when dealing with parameters
16632 declared as the same subroutine type. */
16633 set_die_type (die, ftype, cu);
16634
16635 if (die->child != NULL)
16636 {
16637 struct type *void_type = objfile_type (objfile)->builtin_void;
16638 struct die_info *child_die;
16639 int nparams, iparams;
16640
16641 /* Count the number of parameters.
16642 FIXME: GDB currently ignores vararg functions, but knows about
16643 vararg member functions. */
16644 nparams = 0;
16645 child_die = die->child;
16646 while (child_die && child_die->tag)
16647 {
16648 if (child_die->tag == DW_TAG_formal_parameter)
16649 nparams++;
16650 else if (child_die->tag == DW_TAG_unspecified_parameters)
16651 TYPE_VARARGS (ftype) = 1;
16652 child_die = sibling_die (child_die);
16653 }
16654
16655 /* Allocate storage for parameters and fill them in. */
16656 TYPE_NFIELDS (ftype) = nparams;
16657 TYPE_FIELDS (ftype) = (struct field *)
16658 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16659
16660 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16661 even if we error out during the parameters reading below. */
16662 for (iparams = 0; iparams < nparams; iparams++)
16663 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16664
16665 iparams = 0;
16666 child_die = die->child;
16667 while (child_die && child_die->tag)
16668 {
16669 if (child_die->tag == DW_TAG_formal_parameter)
16670 {
16671 struct type *arg_type;
16672
16673 /* DWARF version 2 has no clean way to discern C++
16674 static and non-static member functions. G++ helps
16675 GDB by marking the first parameter for non-static
16676 member functions (which is the this pointer) as
16677 artificial. We pass this information to
16678 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16679
16680 DWARF version 3 added DW_AT_object_pointer, which GCC
16681 4.5 does not yet generate. */
16682 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16683 if (attr != nullptr)
16684 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16685 else
16686 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16687 arg_type = die_type (child_die, cu);
16688
16689 /* RealView does not mark THIS as const, which the testsuite
16690 expects. GCC marks THIS as const in method definitions,
16691 but not in the class specifications (GCC PR 43053). */
16692 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16693 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16694 {
16695 int is_this = 0;
16696 struct dwarf2_cu *arg_cu = cu;
16697 const char *name = dwarf2_name (child_die, cu);
16698
16699 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16700 if (attr != nullptr)
16701 {
16702 /* If the compiler emits this, use it. */
16703 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16704 is_this = 1;
16705 }
16706 else if (name && strcmp (name, "this") == 0)
16707 /* Function definitions will have the argument names. */
16708 is_this = 1;
16709 else if (name == NULL && iparams == 0)
16710 /* Declarations may not have the names, so like
16711 elsewhere in GDB, assume an artificial first
16712 argument is "this". */
16713 is_this = 1;
16714
16715 if (is_this)
16716 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16717 arg_type, 0);
16718 }
16719
16720 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16721 iparams++;
16722 }
16723 child_die = sibling_die (child_die);
16724 }
16725 }
16726
16727 return ftype;
16728 }
16729
16730 static struct type *
16731 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16732 {
16733 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16734 const char *name = NULL;
16735 struct type *this_type, *target_type;
16736
16737 name = dwarf2_full_name (NULL, die, cu);
16738 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16739 TYPE_TARGET_STUB (this_type) = 1;
16740 set_die_type (die, this_type, cu);
16741 target_type = die_type (die, cu);
16742 if (target_type != this_type)
16743 TYPE_TARGET_TYPE (this_type) = target_type;
16744 else
16745 {
16746 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16747 spec and cause infinite loops in GDB. */
16748 complaint (_("Self-referential DW_TAG_typedef "
16749 "- DIE at %s [in module %s]"),
16750 sect_offset_str (die->sect_off), objfile_name (objfile));
16751 TYPE_TARGET_TYPE (this_type) = NULL;
16752 }
16753 if (name == NULL)
16754 {
16755 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16756 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16757 Handle these by just returning the target type, rather than
16758 constructing an anonymous typedef type and trying to handle this
16759 elsewhere. */
16760 set_die_type (die, target_type, cu);
16761 return target_type;
16762 }
16763 return this_type;
16764 }
16765
16766 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16767 (which may be different from NAME) to the architecture back-end to allow
16768 it to guess the correct format if necessary. */
16769
16770 static struct type *
16771 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16772 const char *name_hint, enum bfd_endian byte_order)
16773 {
16774 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16775 const struct floatformat **format;
16776 struct type *type;
16777
16778 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16779 if (format)
16780 type = init_float_type (objfile, bits, name, format, byte_order);
16781 else
16782 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16783
16784 return type;
16785 }
16786
16787 /* Allocate an integer type of size BITS and name NAME. */
16788
16789 static struct type *
16790 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16791 int bits, int unsigned_p, const char *name)
16792 {
16793 struct type *type;
16794
16795 /* Versions of Intel's C Compiler generate an integer type called "void"
16796 instead of using DW_TAG_unspecified_type. This has been seen on
16797 at least versions 14, 17, and 18. */
16798 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16799 && strcmp (name, "void") == 0)
16800 type = objfile_type (objfile)->builtin_void;
16801 else
16802 type = init_integer_type (objfile, bits, unsigned_p, name);
16803
16804 return type;
16805 }
16806
16807 /* Initialise and return a floating point type of size BITS suitable for
16808 use as a component of a complex number. The NAME_HINT is passed through
16809 when initialising the floating point type and is the name of the complex
16810 type.
16811
16812 As DWARF doesn't currently provide an explicit name for the components
16813 of a complex number, but it can be helpful to have these components
16814 named, we try to select a suitable name based on the size of the
16815 component. */
16816 static struct type *
16817 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16818 struct objfile *objfile,
16819 int bits, const char *name_hint,
16820 enum bfd_endian byte_order)
16821 {
16822 gdbarch *gdbarch = get_objfile_arch (objfile);
16823 struct type *tt = nullptr;
16824
16825 /* Try to find a suitable floating point builtin type of size BITS.
16826 We're going to use the name of this type as the name for the complex
16827 target type that we are about to create. */
16828 switch (cu->language)
16829 {
16830 case language_fortran:
16831 switch (bits)
16832 {
16833 case 32:
16834 tt = builtin_f_type (gdbarch)->builtin_real;
16835 break;
16836 case 64:
16837 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16838 break;
16839 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16840 case 128:
16841 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16842 break;
16843 }
16844 break;
16845 default:
16846 switch (bits)
16847 {
16848 case 32:
16849 tt = builtin_type (gdbarch)->builtin_float;
16850 break;
16851 case 64:
16852 tt = builtin_type (gdbarch)->builtin_double;
16853 break;
16854 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16855 case 128:
16856 tt = builtin_type (gdbarch)->builtin_long_double;
16857 break;
16858 }
16859 break;
16860 }
16861
16862 /* If the type we found doesn't match the size we were looking for, then
16863 pretend we didn't find a type at all, the complex target type we
16864 create will then be nameless. */
16865 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16866 tt = nullptr;
16867
16868 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16869 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16870 }
16871
16872 /* Find a representation of a given base type and install
16873 it in the TYPE field of the die. */
16874
16875 static struct type *
16876 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16877 {
16878 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16879 struct type *type;
16880 struct attribute *attr;
16881 int encoding = 0, bits = 0;
16882 const char *name;
16883 gdbarch *arch;
16884
16885 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16886 if (attr != nullptr)
16887 encoding = DW_UNSND (attr);
16888 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16889 if (attr != nullptr)
16890 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16891 name = dwarf2_name (die, cu);
16892 if (!name)
16893 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16894
16895 arch = get_objfile_arch (objfile);
16896 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16897
16898 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16899 if (attr)
16900 {
16901 int endianity = DW_UNSND (attr);
16902
16903 switch (endianity)
16904 {
16905 case DW_END_big:
16906 byte_order = BFD_ENDIAN_BIG;
16907 break;
16908 case DW_END_little:
16909 byte_order = BFD_ENDIAN_LITTLE;
16910 break;
16911 default:
16912 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16913 break;
16914 }
16915 }
16916
16917 switch (encoding)
16918 {
16919 case DW_ATE_address:
16920 /* Turn DW_ATE_address into a void * pointer. */
16921 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16922 type = init_pointer_type (objfile, bits, name, type);
16923 break;
16924 case DW_ATE_boolean:
16925 type = init_boolean_type (objfile, bits, 1, name);
16926 break;
16927 case DW_ATE_complex_float:
16928 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16929 byte_order);
16930 type = init_complex_type (objfile, name, type);
16931 break;
16932 case DW_ATE_decimal_float:
16933 type = init_decfloat_type (objfile, bits, name);
16934 break;
16935 case DW_ATE_float:
16936 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16937 break;
16938 case DW_ATE_signed:
16939 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16940 break;
16941 case DW_ATE_unsigned:
16942 if (cu->language == language_fortran
16943 && name
16944 && startswith (name, "character("))
16945 type = init_character_type (objfile, bits, 1, name);
16946 else
16947 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16948 break;
16949 case DW_ATE_signed_char:
16950 if (cu->language == language_ada || cu->language == language_m2
16951 || cu->language == language_pascal
16952 || cu->language == language_fortran)
16953 type = init_character_type (objfile, bits, 0, name);
16954 else
16955 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16956 break;
16957 case DW_ATE_unsigned_char:
16958 if (cu->language == language_ada || cu->language == language_m2
16959 || cu->language == language_pascal
16960 || cu->language == language_fortran
16961 || cu->language == language_rust)
16962 type = init_character_type (objfile, bits, 1, name);
16963 else
16964 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16965 break;
16966 case DW_ATE_UTF:
16967 {
16968 if (bits == 16)
16969 type = builtin_type (arch)->builtin_char16;
16970 else if (bits == 32)
16971 type = builtin_type (arch)->builtin_char32;
16972 else
16973 {
16974 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
16975 bits);
16976 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16977 }
16978 return set_die_type (die, type, cu);
16979 }
16980 break;
16981
16982 default:
16983 complaint (_("unsupported DW_AT_encoding: '%s'"),
16984 dwarf_type_encoding_name (encoding));
16985 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16986 break;
16987 }
16988
16989 if (name && strcmp (name, "char") == 0)
16990 TYPE_NOSIGN (type) = 1;
16991
16992 maybe_set_alignment (cu, die, type);
16993
16994 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
16995
16996 return set_die_type (die, type, cu);
16997 }
16998
16999 /* Parse dwarf attribute if it's a block, reference or constant and put the
17000 resulting value of the attribute into struct bound_prop.
17001 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17002
17003 static int
17004 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17005 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17006 struct type *default_type)
17007 {
17008 struct dwarf2_property_baton *baton;
17009 struct obstack *obstack
17010 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17011
17012 gdb_assert (default_type != NULL);
17013
17014 if (attr == NULL || prop == NULL)
17015 return 0;
17016
17017 if (attr->form_is_block ())
17018 {
17019 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17020 baton->property_type = default_type;
17021 baton->locexpr.per_cu = cu->per_cu;
17022 baton->locexpr.size = DW_BLOCK (attr)->size;
17023 baton->locexpr.data = DW_BLOCK (attr)->data;
17024 switch (attr->name)
17025 {
17026 case DW_AT_string_length:
17027 baton->locexpr.is_reference = true;
17028 break;
17029 default:
17030 baton->locexpr.is_reference = false;
17031 break;
17032 }
17033 prop->data.baton = baton;
17034 prop->kind = PROP_LOCEXPR;
17035 gdb_assert (prop->data.baton != NULL);
17036 }
17037 else if (attr->form_is_ref ())
17038 {
17039 struct dwarf2_cu *target_cu = cu;
17040 struct die_info *target_die;
17041 struct attribute *target_attr;
17042
17043 target_die = follow_die_ref (die, attr, &target_cu);
17044 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17045 if (target_attr == NULL)
17046 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17047 target_cu);
17048 if (target_attr == NULL)
17049 return 0;
17050
17051 switch (target_attr->name)
17052 {
17053 case DW_AT_location:
17054 if (target_attr->form_is_section_offset ())
17055 {
17056 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17057 baton->property_type = die_type (target_die, target_cu);
17058 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17059 prop->data.baton = baton;
17060 prop->kind = PROP_LOCLIST;
17061 gdb_assert (prop->data.baton != NULL);
17062 }
17063 else if (target_attr->form_is_block ())
17064 {
17065 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17066 baton->property_type = die_type (target_die, target_cu);
17067 baton->locexpr.per_cu = cu->per_cu;
17068 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17069 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17070 baton->locexpr.is_reference = true;
17071 prop->data.baton = baton;
17072 prop->kind = PROP_LOCEXPR;
17073 gdb_assert (prop->data.baton != NULL);
17074 }
17075 else
17076 {
17077 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17078 "dynamic property");
17079 return 0;
17080 }
17081 break;
17082 case DW_AT_data_member_location:
17083 {
17084 LONGEST offset;
17085
17086 if (!handle_data_member_location (target_die, target_cu,
17087 &offset))
17088 return 0;
17089
17090 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17091 baton->property_type = read_type_die (target_die->parent,
17092 target_cu);
17093 baton->offset_info.offset = offset;
17094 baton->offset_info.type = die_type (target_die, target_cu);
17095 prop->data.baton = baton;
17096 prop->kind = PROP_ADDR_OFFSET;
17097 break;
17098 }
17099 }
17100 }
17101 else if (attr->form_is_constant ())
17102 {
17103 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17104 prop->kind = PROP_CONST;
17105 }
17106 else
17107 {
17108 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17109 dwarf2_name (die, cu));
17110 return 0;
17111 }
17112
17113 return 1;
17114 }
17115
17116 /* See read.h. */
17117
17118 struct type *
17119 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17120 {
17121 struct objfile *objfile = dwarf2_per_objfile->objfile;
17122 struct type *int_type;
17123
17124 /* Helper macro to examine the various builtin types. */
17125 #define TRY_TYPE(F) \
17126 int_type = (unsigned_p \
17127 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17128 : objfile_type (objfile)->builtin_ ## F); \
17129 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17130 return int_type
17131
17132 TRY_TYPE (char);
17133 TRY_TYPE (short);
17134 TRY_TYPE (int);
17135 TRY_TYPE (long);
17136 TRY_TYPE (long_long);
17137
17138 #undef TRY_TYPE
17139
17140 gdb_assert_not_reached ("unable to find suitable integer type");
17141 }
17142
17143 /* See read.h. */
17144
17145 struct type *
17146 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17147 {
17148 int addr_size = this->addr_size ();
17149 return int_type (addr_size, unsigned_p);
17150 }
17151
17152 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17153 present (which is valid) then compute the default type based on the
17154 compilation units address size. */
17155
17156 static struct type *
17157 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17158 {
17159 struct type *index_type = die_type (die, cu);
17160
17161 /* Dwarf-2 specifications explicitly allows to create subrange types
17162 without specifying a base type.
17163 In that case, the base type must be set to the type of
17164 the lower bound, upper bound or count, in that order, if any of these
17165 three attributes references an object that has a type.
17166 If no base type is found, the Dwarf-2 specifications say that
17167 a signed integer type of size equal to the size of an address should
17168 be used.
17169 For the following C code: `extern char gdb_int [];'
17170 GCC produces an empty range DIE.
17171 FIXME: muller/2010-05-28: Possible references to object for low bound,
17172 high bound or count are not yet handled by this code. */
17173 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17174 index_type = cu->per_cu->addr_sized_int_type (false);
17175
17176 return index_type;
17177 }
17178
17179 /* Read the given DW_AT_subrange DIE. */
17180
17181 static struct type *
17182 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17183 {
17184 struct type *base_type, *orig_base_type;
17185 struct type *range_type;
17186 struct attribute *attr;
17187 struct dynamic_prop low, high;
17188 int low_default_is_valid;
17189 int high_bound_is_count = 0;
17190 const char *name;
17191 ULONGEST negative_mask;
17192
17193 orig_base_type = read_subrange_index_type (die, cu);
17194
17195 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17196 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17197 creating the range type, but we use the result of check_typedef
17198 when examining properties of the type. */
17199 base_type = check_typedef (orig_base_type);
17200
17201 /* The die_type call above may have already set the type for this DIE. */
17202 range_type = get_die_type (die, cu);
17203 if (range_type)
17204 return range_type;
17205
17206 low.kind = PROP_CONST;
17207 high.kind = PROP_CONST;
17208 high.data.const_val = 0;
17209
17210 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17211 omitting DW_AT_lower_bound. */
17212 switch (cu->language)
17213 {
17214 case language_c:
17215 case language_cplus:
17216 low.data.const_val = 0;
17217 low_default_is_valid = 1;
17218 break;
17219 case language_fortran:
17220 low.data.const_val = 1;
17221 low_default_is_valid = 1;
17222 break;
17223 case language_d:
17224 case language_objc:
17225 case language_rust:
17226 low.data.const_val = 0;
17227 low_default_is_valid = (cu->header.version >= 4);
17228 break;
17229 case language_ada:
17230 case language_m2:
17231 case language_pascal:
17232 low.data.const_val = 1;
17233 low_default_is_valid = (cu->header.version >= 4);
17234 break;
17235 default:
17236 low.data.const_val = 0;
17237 low_default_is_valid = 0;
17238 break;
17239 }
17240
17241 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17242 if (attr != nullptr)
17243 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17244 else if (!low_default_is_valid)
17245 complaint (_("Missing DW_AT_lower_bound "
17246 "- DIE at %s [in module %s]"),
17247 sect_offset_str (die->sect_off),
17248 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17249
17250 struct attribute *attr_ub, *attr_count;
17251 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17252 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17253 {
17254 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17255 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17256 {
17257 /* If bounds are constant do the final calculation here. */
17258 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17259 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17260 else
17261 high_bound_is_count = 1;
17262 }
17263 else
17264 {
17265 if (attr_ub != NULL)
17266 complaint (_("Unresolved DW_AT_upper_bound "
17267 "- DIE at %s [in module %s]"),
17268 sect_offset_str (die->sect_off),
17269 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17270 if (attr_count != NULL)
17271 complaint (_("Unresolved DW_AT_count "
17272 "- DIE at %s [in module %s]"),
17273 sect_offset_str (die->sect_off),
17274 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17275 }
17276 }
17277
17278 LONGEST bias = 0;
17279 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17280 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17281 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17282
17283 /* Normally, the DWARF producers are expected to use a signed
17284 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17285 But this is unfortunately not always the case, as witnessed
17286 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17287 is used instead. To work around that ambiguity, we treat
17288 the bounds as signed, and thus sign-extend their values, when
17289 the base type is signed. */
17290 negative_mask =
17291 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17292 if (low.kind == PROP_CONST
17293 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17294 low.data.const_val |= negative_mask;
17295 if (high.kind == PROP_CONST
17296 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17297 high.data.const_val |= negative_mask;
17298
17299 /* Check for bit and byte strides. */
17300 struct dynamic_prop byte_stride_prop;
17301 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17302 if (attr_byte_stride != nullptr)
17303 {
17304 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17305 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17306 prop_type);
17307 }
17308
17309 struct dynamic_prop bit_stride_prop;
17310 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17311 if (attr_bit_stride != nullptr)
17312 {
17313 /* It only makes sense to have either a bit or byte stride. */
17314 if (attr_byte_stride != nullptr)
17315 {
17316 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17317 "- DIE at %s [in module %s]"),
17318 sect_offset_str (die->sect_off),
17319 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17320 attr_bit_stride = nullptr;
17321 }
17322 else
17323 {
17324 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17325 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17326 prop_type);
17327 }
17328 }
17329
17330 if (attr_byte_stride != nullptr
17331 || attr_bit_stride != nullptr)
17332 {
17333 bool byte_stride_p = (attr_byte_stride != nullptr);
17334 struct dynamic_prop *stride
17335 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17336
17337 range_type
17338 = create_range_type_with_stride (NULL, orig_base_type, &low,
17339 &high, bias, stride, byte_stride_p);
17340 }
17341 else
17342 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17343
17344 if (high_bound_is_count)
17345 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17346
17347 /* Ada expects an empty array on no boundary attributes. */
17348 if (attr == NULL && cu->language != language_ada)
17349 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17350
17351 name = dwarf2_name (die, cu);
17352 if (name)
17353 TYPE_NAME (range_type) = name;
17354
17355 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17356 if (attr != nullptr)
17357 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17358
17359 maybe_set_alignment (cu, die, range_type);
17360
17361 set_die_type (die, range_type, cu);
17362
17363 /* set_die_type should be already done. */
17364 set_descriptive_type (range_type, die, cu);
17365
17366 return range_type;
17367 }
17368
17369 static struct type *
17370 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17371 {
17372 struct type *type;
17373
17374 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17375 NULL);
17376 TYPE_NAME (type) = dwarf2_name (die, cu);
17377
17378 /* In Ada, an unspecified type is typically used when the description
17379 of the type is deferred to a different unit. When encountering
17380 such a type, we treat it as a stub, and try to resolve it later on,
17381 when needed. */
17382 if (cu->language == language_ada)
17383 TYPE_STUB (type) = 1;
17384
17385 return set_die_type (die, type, cu);
17386 }
17387
17388 /* Read a single die and all its descendents. Set the die's sibling
17389 field to NULL; set other fields in the die correctly, and set all
17390 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17391 location of the info_ptr after reading all of those dies. PARENT
17392 is the parent of the die in question. */
17393
17394 static struct die_info *
17395 read_die_and_children (const struct die_reader_specs *reader,
17396 const gdb_byte *info_ptr,
17397 const gdb_byte **new_info_ptr,
17398 struct die_info *parent)
17399 {
17400 struct die_info *die;
17401 const gdb_byte *cur_ptr;
17402
17403 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17404 if (die == NULL)
17405 {
17406 *new_info_ptr = cur_ptr;
17407 return NULL;
17408 }
17409 store_in_ref_table (die, reader->cu);
17410
17411 if (die->has_children)
17412 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17413 else
17414 {
17415 die->child = NULL;
17416 *new_info_ptr = cur_ptr;
17417 }
17418
17419 die->sibling = NULL;
17420 die->parent = parent;
17421 return die;
17422 }
17423
17424 /* Read a die, all of its descendents, and all of its siblings; set
17425 all of the fields of all of the dies correctly. Arguments are as
17426 in read_die_and_children. */
17427
17428 static struct die_info *
17429 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17430 const gdb_byte *info_ptr,
17431 const gdb_byte **new_info_ptr,
17432 struct die_info *parent)
17433 {
17434 struct die_info *first_die, *last_sibling;
17435 const gdb_byte *cur_ptr;
17436
17437 cur_ptr = info_ptr;
17438 first_die = last_sibling = NULL;
17439
17440 while (1)
17441 {
17442 struct die_info *die
17443 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17444
17445 if (die == NULL)
17446 {
17447 *new_info_ptr = cur_ptr;
17448 return first_die;
17449 }
17450
17451 if (!first_die)
17452 first_die = die;
17453 else
17454 last_sibling->sibling = die;
17455
17456 last_sibling = die;
17457 }
17458 }
17459
17460 /* Read a die, all of its descendents, and all of its siblings; set
17461 all of the fields of all of the dies correctly. Arguments are as
17462 in read_die_and_children.
17463 This the main entry point for reading a DIE and all its children. */
17464
17465 static struct die_info *
17466 read_die_and_siblings (const struct die_reader_specs *reader,
17467 const gdb_byte *info_ptr,
17468 const gdb_byte **new_info_ptr,
17469 struct die_info *parent)
17470 {
17471 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17472 new_info_ptr, parent);
17473
17474 if (dwarf_die_debug)
17475 {
17476 fprintf_unfiltered (gdb_stdlog,
17477 "Read die from %s@0x%x of %s:\n",
17478 reader->die_section->get_name (),
17479 (unsigned) (info_ptr - reader->die_section->buffer),
17480 bfd_get_filename (reader->abfd));
17481 dump_die (die, dwarf_die_debug);
17482 }
17483
17484 return die;
17485 }
17486
17487 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17488 attributes.
17489 The caller is responsible for filling in the extra attributes
17490 and updating (*DIEP)->num_attrs.
17491 Set DIEP to point to a newly allocated die with its information,
17492 except for its child, sibling, and parent fields. */
17493
17494 static const gdb_byte *
17495 read_full_die_1 (const struct die_reader_specs *reader,
17496 struct die_info **diep, const gdb_byte *info_ptr,
17497 int num_extra_attrs)
17498 {
17499 unsigned int abbrev_number, bytes_read, i;
17500 struct abbrev_info *abbrev;
17501 struct die_info *die;
17502 struct dwarf2_cu *cu = reader->cu;
17503 bfd *abfd = reader->abfd;
17504
17505 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17506 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17507 info_ptr += bytes_read;
17508 if (!abbrev_number)
17509 {
17510 *diep = NULL;
17511 return info_ptr;
17512 }
17513
17514 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17515 if (!abbrev)
17516 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17517 abbrev_number,
17518 bfd_get_filename (abfd));
17519
17520 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17521 die->sect_off = sect_off;
17522 die->tag = abbrev->tag;
17523 die->abbrev = abbrev_number;
17524 die->has_children = abbrev->has_children;
17525
17526 /* Make the result usable.
17527 The caller needs to update num_attrs after adding the extra
17528 attributes. */
17529 die->num_attrs = abbrev->num_attrs;
17530
17531 std::vector<int> indexes_that_need_reprocess;
17532 for (i = 0; i < abbrev->num_attrs; ++i)
17533 {
17534 bool need_reprocess;
17535 info_ptr =
17536 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17537 info_ptr, &need_reprocess);
17538 if (need_reprocess)
17539 indexes_that_need_reprocess.push_back (i);
17540 }
17541
17542 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17543 if (attr != nullptr)
17544 cu->str_offsets_base = DW_UNSND (attr);
17545
17546 auto maybe_addr_base = lookup_addr_base(die);
17547 if (maybe_addr_base.has_value ())
17548 cu->addr_base = *maybe_addr_base;
17549 for (int index : indexes_that_need_reprocess)
17550 read_attribute_reprocess (reader, &die->attrs[index]);
17551 *diep = die;
17552 return info_ptr;
17553 }
17554
17555 /* Read a die and all its attributes.
17556 Set DIEP to point to a newly allocated die with its information,
17557 except for its child, sibling, and parent fields. */
17558
17559 static const gdb_byte *
17560 read_full_die (const struct die_reader_specs *reader,
17561 struct die_info **diep, const gdb_byte *info_ptr)
17562 {
17563 const gdb_byte *result;
17564
17565 result = read_full_die_1 (reader, diep, info_ptr, 0);
17566
17567 if (dwarf_die_debug)
17568 {
17569 fprintf_unfiltered (gdb_stdlog,
17570 "Read die from %s@0x%x of %s:\n",
17571 reader->die_section->get_name (),
17572 (unsigned) (info_ptr - reader->die_section->buffer),
17573 bfd_get_filename (reader->abfd));
17574 dump_die (*diep, dwarf_die_debug);
17575 }
17576
17577 return result;
17578 }
17579 \f
17580
17581 /* Returns nonzero if TAG represents a type that we might generate a partial
17582 symbol for. */
17583
17584 static int
17585 is_type_tag_for_partial (int tag)
17586 {
17587 switch (tag)
17588 {
17589 #if 0
17590 /* Some types that would be reasonable to generate partial symbols for,
17591 that we don't at present. */
17592 case DW_TAG_array_type:
17593 case DW_TAG_file_type:
17594 case DW_TAG_ptr_to_member_type:
17595 case DW_TAG_set_type:
17596 case DW_TAG_string_type:
17597 case DW_TAG_subroutine_type:
17598 #endif
17599 case DW_TAG_base_type:
17600 case DW_TAG_class_type:
17601 case DW_TAG_interface_type:
17602 case DW_TAG_enumeration_type:
17603 case DW_TAG_structure_type:
17604 case DW_TAG_subrange_type:
17605 case DW_TAG_typedef:
17606 case DW_TAG_union_type:
17607 return 1;
17608 default:
17609 return 0;
17610 }
17611 }
17612
17613 /* Load all DIEs that are interesting for partial symbols into memory. */
17614
17615 static struct partial_die_info *
17616 load_partial_dies (const struct die_reader_specs *reader,
17617 const gdb_byte *info_ptr, int building_psymtab)
17618 {
17619 struct dwarf2_cu *cu = reader->cu;
17620 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17621 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17622 unsigned int bytes_read;
17623 unsigned int load_all = 0;
17624 int nesting_level = 1;
17625
17626 parent_die = NULL;
17627 last_die = NULL;
17628
17629 gdb_assert (cu->per_cu != NULL);
17630 if (cu->per_cu->load_all_dies)
17631 load_all = 1;
17632
17633 cu->partial_dies
17634 = htab_create_alloc_ex (cu->header.length / 12,
17635 partial_die_hash,
17636 partial_die_eq,
17637 NULL,
17638 &cu->comp_unit_obstack,
17639 hashtab_obstack_allocate,
17640 dummy_obstack_deallocate);
17641
17642 while (1)
17643 {
17644 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17645
17646 /* A NULL abbrev means the end of a series of children. */
17647 if (abbrev == NULL)
17648 {
17649 if (--nesting_level == 0)
17650 return first_die;
17651
17652 info_ptr += bytes_read;
17653 last_die = parent_die;
17654 parent_die = parent_die->die_parent;
17655 continue;
17656 }
17657
17658 /* Check for template arguments. We never save these; if
17659 they're seen, we just mark the parent, and go on our way. */
17660 if (parent_die != NULL
17661 && cu->language == language_cplus
17662 && (abbrev->tag == DW_TAG_template_type_param
17663 || abbrev->tag == DW_TAG_template_value_param))
17664 {
17665 parent_die->has_template_arguments = 1;
17666
17667 if (!load_all)
17668 {
17669 /* We don't need a partial DIE for the template argument. */
17670 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17671 continue;
17672 }
17673 }
17674
17675 /* We only recurse into c++ subprograms looking for template arguments.
17676 Skip their other children. */
17677 if (!load_all
17678 && cu->language == language_cplus
17679 && parent_die != NULL
17680 && parent_die->tag == DW_TAG_subprogram)
17681 {
17682 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17683 continue;
17684 }
17685
17686 /* Check whether this DIE is interesting enough to save. Normally
17687 we would not be interested in members here, but there may be
17688 later variables referencing them via DW_AT_specification (for
17689 static members). */
17690 if (!load_all
17691 && !is_type_tag_for_partial (abbrev->tag)
17692 && abbrev->tag != DW_TAG_constant
17693 && abbrev->tag != DW_TAG_enumerator
17694 && abbrev->tag != DW_TAG_subprogram
17695 && abbrev->tag != DW_TAG_inlined_subroutine
17696 && abbrev->tag != DW_TAG_lexical_block
17697 && abbrev->tag != DW_TAG_variable
17698 && abbrev->tag != DW_TAG_namespace
17699 && abbrev->tag != DW_TAG_module
17700 && abbrev->tag != DW_TAG_member
17701 && abbrev->tag != DW_TAG_imported_unit
17702 && abbrev->tag != DW_TAG_imported_declaration)
17703 {
17704 /* Otherwise we skip to the next sibling, if any. */
17705 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17706 continue;
17707 }
17708
17709 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17710 abbrev);
17711
17712 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17713
17714 /* This two-pass algorithm for processing partial symbols has a
17715 high cost in cache pressure. Thus, handle some simple cases
17716 here which cover the majority of C partial symbols. DIEs
17717 which neither have specification tags in them, nor could have
17718 specification tags elsewhere pointing at them, can simply be
17719 processed and discarded.
17720
17721 This segment is also optional; scan_partial_symbols and
17722 add_partial_symbol will handle these DIEs if we chain
17723 them in normally. When compilers which do not emit large
17724 quantities of duplicate debug information are more common,
17725 this code can probably be removed. */
17726
17727 /* Any complete simple types at the top level (pretty much all
17728 of them, for a language without namespaces), can be processed
17729 directly. */
17730 if (parent_die == NULL
17731 && pdi.has_specification == 0
17732 && pdi.is_declaration == 0
17733 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17734 || pdi.tag == DW_TAG_base_type
17735 || pdi.tag == DW_TAG_subrange_type))
17736 {
17737 if (building_psymtab && pdi.name != NULL)
17738 add_psymbol_to_list (pdi.name, false,
17739 VAR_DOMAIN, LOC_TYPEDEF, -1,
17740 psymbol_placement::STATIC,
17741 0, cu->language, objfile);
17742 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17743 continue;
17744 }
17745
17746 /* The exception for DW_TAG_typedef with has_children above is
17747 a workaround of GCC PR debug/47510. In the case of this complaint
17748 type_name_or_error will error on such types later.
17749
17750 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17751 it could not find the child DIEs referenced later, this is checked
17752 above. In correct DWARF DW_TAG_typedef should have no children. */
17753
17754 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17755 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17756 "- DIE at %s [in module %s]"),
17757 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17758
17759 /* If we're at the second level, and we're an enumerator, and
17760 our parent has no specification (meaning possibly lives in a
17761 namespace elsewhere), then we can add the partial symbol now
17762 instead of queueing it. */
17763 if (pdi.tag == DW_TAG_enumerator
17764 && parent_die != NULL
17765 && parent_die->die_parent == NULL
17766 && parent_die->tag == DW_TAG_enumeration_type
17767 && parent_die->has_specification == 0)
17768 {
17769 if (pdi.name == NULL)
17770 complaint (_("malformed enumerator DIE ignored"));
17771 else if (building_psymtab)
17772 add_psymbol_to_list (pdi.name, false,
17773 VAR_DOMAIN, LOC_CONST, -1,
17774 cu->language == language_cplus
17775 ? psymbol_placement::GLOBAL
17776 : psymbol_placement::STATIC,
17777 0, cu->language, objfile);
17778
17779 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17780 continue;
17781 }
17782
17783 struct partial_die_info *part_die
17784 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17785
17786 /* We'll save this DIE so link it in. */
17787 part_die->die_parent = parent_die;
17788 part_die->die_sibling = NULL;
17789 part_die->die_child = NULL;
17790
17791 if (last_die && last_die == parent_die)
17792 last_die->die_child = part_die;
17793 else if (last_die)
17794 last_die->die_sibling = part_die;
17795
17796 last_die = part_die;
17797
17798 if (first_die == NULL)
17799 first_die = part_die;
17800
17801 /* Maybe add the DIE to the hash table. Not all DIEs that we
17802 find interesting need to be in the hash table, because we
17803 also have the parent/sibling/child chains; only those that we
17804 might refer to by offset later during partial symbol reading.
17805
17806 For now this means things that might have be the target of a
17807 DW_AT_specification, DW_AT_abstract_origin, or
17808 DW_AT_extension. DW_AT_extension will refer only to
17809 namespaces; DW_AT_abstract_origin refers to functions (and
17810 many things under the function DIE, but we do not recurse
17811 into function DIEs during partial symbol reading) and
17812 possibly variables as well; DW_AT_specification refers to
17813 declarations. Declarations ought to have the DW_AT_declaration
17814 flag. It happens that GCC forgets to put it in sometimes, but
17815 only for functions, not for types.
17816
17817 Adding more things than necessary to the hash table is harmless
17818 except for the performance cost. Adding too few will result in
17819 wasted time in find_partial_die, when we reread the compilation
17820 unit with load_all_dies set. */
17821
17822 if (load_all
17823 || abbrev->tag == DW_TAG_constant
17824 || abbrev->tag == DW_TAG_subprogram
17825 || abbrev->tag == DW_TAG_variable
17826 || abbrev->tag == DW_TAG_namespace
17827 || part_die->is_declaration)
17828 {
17829 void **slot;
17830
17831 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17832 to_underlying (part_die->sect_off),
17833 INSERT);
17834 *slot = part_die;
17835 }
17836
17837 /* For some DIEs we want to follow their children (if any). For C
17838 we have no reason to follow the children of structures; for other
17839 languages we have to, so that we can get at method physnames
17840 to infer fully qualified class names, for DW_AT_specification,
17841 and for C++ template arguments. For C++, we also look one level
17842 inside functions to find template arguments (if the name of the
17843 function does not already contain the template arguments).
17844
17845 For Ada and Fortran, we need to scan the children of subprograms
17846 and lexical blocks as well because these languages allow the
17847 definition of nested entities that could be interesting for the
17848 debugger, such as nested subprograms for instance. */
17849 if (last_die->has_children
17850 && (load_all
17851 || last_die->tag == DW_TAG_namespace
17852 || last_die->tag == DW_TAG_module
17853 || last_die->tag == DW_TAG_enumeration_type
17854 || (cu->language == language_cplus
17855 && last_die->tag == DW_TAG_subprogram
17856 && (last_die->name == NULL
17857 || strchr (last_die->name, '<') == NULL))
17858 || (cu->language != language_c
17859 && (last_die->tag == DW_TAG_class_type
17860 || last_die->tag == DW_TAG_interface_type
17861 || last_die->tag == DW_TAG_structure_type
17862 || last_die->tag == DW_TAG_union_type))
17863 || ((cu->language == language_ada
17864 || cu->language == language_fortran)
17865 && (last_die->tag == DW_TAG_subprogram
17866 || last_die->tag == DW_TAG_lexical_block))))
17867 {
17868 nesting_level++;
17869 parent_die = last_die;
17870 continue;
17871 }
17872
17873 /* Otherwise we skip to the next sibling, if any. */
17874 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17875
17876 /* Back to the top, do it again. */
17877 }
17878 }
17879
17880 partial_die_info::partial_die_info (sect_offset sect_off_,
17881 struct abbrev_info *abbrev)
17882 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17883 {
17884 }
17885
17886 /* Read a minimal amount of information into the minimal die structure.
17887 INFO_PTR should point just after the initial uleb128 of a DIE. */
17888
17889 const gdb_byte *
17890 partial_die_info::read (const struct die_reader_specs *reader,
17891 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17892 {
17893 struct dwarf2_cu *cu = reader->cu;
17894 struct dwarf2_per_objfile *dwarf2_per_objfile
17895 = cu->per_cu->dwarf2_per_objfile;
17896 unsigned int i;
17897 int has_low_pc_attr = 0;
17898 int has_high_pc_attr = 0;
17899 int high_pc_relative = 0;
17900
17901 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17902 for (i = 0; i < abbrev.num_attrs; ++i)
17903 {
17904 bool need_reprocess;
17905 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17906 info_ptr, &need_reprocess);
17907 /* String and address offsets that need to do the reprocessing have
17908 already been read at this point, so there is no need to wait until
17909 the loop terminates to do the reprocessing. */
17910 if (need_reprocess)
17911 read_attribute_reprocess (reader, &attr_vec[i]);
17912 attribute &attr = attr_vec[i];
17913 /* Store the data if it is of an attribute we want to keep in a
17914 partial symbol table. */
17915 switch (attr.name)
17916 {
17917 case DW_AT_name:
17918 switch (tag)
17919 {
17920 case DW_TAG_compile_unit:
17921 case DW_TAG_partial_unit:
17922 case DW_TAG_type_unit:
17923 /* Compilation units have a DW_AT_name that is a filename, not
17924 a source language identifier. */
17925 case DW_TAG_enumeration_type:
17926 case DW_TAG_enumerator:
17927 /* These tags always have simple identifiers already; no need
17928 to canonicalize them. */
17929 name = DW_STRING (&attr);
17930 break;
17931 default:
17932 {
17933 struct objfile *objfile = dwarf2_per_objfile->objfile;
17934
17935 name
17936 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
17937 }
17938 break;
17939 }
17940 break;
17941 case DW_AT_linkage_name:
17942 case DW_AT_MIPS_linkage_name:
17943 /* Note that both forms of linkage name might appear. We
17944 assume they will be the same, and we only store the last
17945 one we see. */
17946 linkage_name = DW_STRING (&attr);
17947 break;
17948 case DW_AT_low_pc:
17949 has_low_pc_attr = 1;
17950 lowpc = attr.value_as_address ();
17951 break;
17952 case DW_AT_high_pc:
17953 has_high_pc_attr = 1;
17954 highpc = attr.value_as_address ();
17955 if (cu->header.version >= 4 && attr.form_is_constant ())
17956 high_pc_relative = 1;
17957 break;
17958 case DW_AT_location:
17959 /* Support the .debug_loc offsets. */
17960 if (attr.form_is_block ())
17961 {
17962 d.locdesc = DW_BLOCK (&attr);
17963 }
17964 else if (attr.form_is_section_offset ())
17965 {
17966 dwarf2_complex_location_expr_complaint ();
17967 }
17968 else
17969 {
17970 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17971 "partial symbol information");
17972 }
17973 break;
17974 case DW_AT_external:
17975 is_external = DW_UNSND (&attr);
17976 break;
17977 case DW_AT_declaration:
17978 is_declaration = DW_UNSND (&attr);
17979 break;
17980 case DW_AT_type:
17981 has_type = 1;
17982 break;
17983 case DW_AT_abstract_origin:
17984 case DW_AT_specification:
17985 case DW_AT_extension:
17986 has_specification = 1;
17987 spec_offset = dwarf2_get_ref_die_offset (&attr);
17988 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17989 || cu->per_cu->is_dwz);
17990 break;
17991 case DW_AT_sibling:
17992 /* Ignore absolute siblings, they might point outside of
17993 the current compile unit. */
17994 if (attr.form == DW_FORM_ref_addr)
17995 complaint (_("ignoring absolute DW_AT_sibling"));
17996 else
17997 {
17998 const gdb_byte *buffer = reader->buffer;
17999 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18000 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18001
18002 if (sibling_ptr < info_ptr)
18003 complaint (_("DW_AT_sibling points backwards"));
18004 else if (sibling_ptr > reader->buffer_end)
18005 reader->die_section->overflow_complaint ();
18006 else
18007 sibling = sibling_ptr;
18008 }
18009 break;
18010 case DW_AT_byte_size:
18011 has_byte_size = 1;
18012 break;
18013 case DW_AT_const_value:
18014 has_const_value = 1;
18015 break;
18016 case DW_AT_calling_convention:
18017 /* DWARF doesn't provide a way to identify a program's source-level
18018 entry point. DW_AT_calling_convention attributes are only meant
18019 to describe functions' calling conventions.
18020
18021 However, because it's a necessary piece of information in
18022 Fortran, and before DWARF 4 DW_CC_program was the only
18023 piece of debugging information whose definition refers to
18024 a 'main program' at all, several compilers marked Fortran
18025 main programs with DW_CC_program --- even when those
18026 functions use the standard calling conventions.
18027
18028 Although DWARF now specifies a way to provide this
18029 information, we support this practice for backward
18030 compatibility. */
18031 if (DW_UNSND (&attr) == DW_CC_program
18032 && cu->language == language_fortran)
18033 main_subprogram = 1;
18034 break;
18035 case DW_AT_inline:
18036 if (DW_UNSND (&attr) == DW_INL_inlined
18037 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18038 may_be_inlined = 1;
18039 break;
18040
18041 case DW_AT_import:
18042 if (tag == DW_TAG_imported_unit)
18043 {
18044 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18045 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18046 || cu->per_cu->is_dwz);
18047 }
18048 break;
18049
18050 case DW_AT_main_subprogram:
18051 main_subprogram = DW_UNSND (&attr);
18052 break;
18053
18054 case DW_AT_ranges:
18055 {
18056 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18057 but that requires a full DIE, so instead we just
18058 reimplement it. */
18059 int need_ranges_base = tag != DW_TAG_compile_unit;
18060 unsigned int ranges_offset = (DW_UNSND (&attr)
18061 + (need_ranges_base
18062 ? cu->ranges_base
18063 : 0));
18064
18065 /* Value of the DW_AT_ranges attribute is the offset in the
18066 .debug_ranges section. */
18067 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18068 nullptr))
18069 has_pc_info = 1;
18070 }
18071 break;
18072
18073 default:
18074 break;
18075 }
18076 }
18077
18078 /* For Ada, if both the name and the linkage name appear, we prefer
18079 the latter. This lets "catch exception" work better, regardless
18080 of the order in which the name and linkage name were emitted.
18081 Really, though, this is just a workaround for the fact that gdb
18082 doesn't store both the name and the linkage name. */
18083 if (cu->language == language_ada && linkage_name != nullptr)
18084 name = linkage_name;
18085
18086 if (high_pc_relative)
18087 highpc += lowpc;
18088
18089 if (has_low_pc_attr && has_high_pc_attr)
18090 {
18091 /* When using the GNU linker, .gnu.linkonce. sections are used to
18092 eliminate duplicate copies of functions and vtables and such.
18093 The linker will arbitrarily choose one and discard the others.
18094 The AT_*_pc values for such functions refer to local labels in
18095 these sections. If the section from that file was discarded, the
18096 labels are not in the output, so the relocs get a value of 0.
18097 If this is a discarded function, mark the pc bounds as invalid,
18098 so that GDB will ignore it. */
18099 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18100 {
18101 struct objfile *objfile = dwarf2_per_objfile->objfile;
18102 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18103
18104 complaint (_("DW_AT_low_pc %s is zero "
18105 "for DIE at %s [in module %s]"),
18106 paddress (gdbarch, lowpc),
18107 sect_offset_str (sect_off),
18108 objfile_name (objfile));
18109 }
18110 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18111 else if (lowpc >= highpc)
18112 {
18113 struct objfile *objfile = dwarf2_per_objfile->objfile;
18114 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18115
18116 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18117 "for DIE at %s [in module %s]"),
18118 paddress (gdbarch, lowpc),
18119 paddress (gdbarch, highpc),
18120 sect_offset_str (sect_off),
18121 objfile_name (objfile));
18122 }
18123 else
18124 has_pc_info = 1;
18125 }
18126
18127 return info_ptr;
18128 }
18129
18130 /* Find a cached partial DIE at OFFSET in CU. */
18131
18132 struct partial_die_info *
18133 dwarf2_cu::find_partial_die (sect_offset sect_off)
18134 {
18135 struct partial_die_info *lookup_die = NULL;
18136 struct partial_die_info part_die (sect_off);
18137
18138 lookup_die = ((struct partial_die_info *)
18139 htab_find_with_hash (partial_dies, &part_die,
18140 to_underlying (sect_off)));
18141
18142 return lookup_die;
18143 }
18144
18145 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18146 except in the case of .debug_types DIEs which do not reference
18147 outside their CU (they do however referencing other types via
18148 DW_FORM_ref_sig8). */
18149
18150 static const struct cu_partial_die_info
18151 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18152 {
18153 struct dwarf2_per_objfile *dwarf2_per_objfile
18154 = cu->per_cu->dwarf2_per_objfile;
18155 struct objfile *objfile = dwarf2_per_objfile->objfile;
18156 struct dwarf2_per_cu_data *per_cu = NULL;
18157 struct partial_die_info *pd = NULL;
18158
18159 if (offset_in_dwz == cu->per_cu->is_dwz
18160 && cu->header.offset_in_cu_p (sect_off))
18161 {
18162 pd = cu->find_partial_die (sect_off);
18163 if (pd != NULL)
18164 return { cu, pd };
18165 /* We missed recording what we needed.
18166 Load all dies and try again. */
18167 per_cu = cu->per_cu;
18168 }
18169 else
18170 {
18171 /* TUs don't reference other CUs/TUs (except via type signatures). */
18172 if (cu->per_cu->is_debug_types)
18173 {
18174 error (_("Dwarf Error: Type Unit at offset %s contains"
18175 " external reference to offset %s [in module %s].\n"),
18176 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18177 bfd_get_filename (objfile->obfd));
18178 }
18179 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18180 dwarf2_per_objfile);
18181
18182 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18183 load_partial_comp_unit (per_cu);
18184
18185 per_cu->cu->last_used = 0;
18186 pd = per_cu->cu->find_partial_die (sect_off);
18187 }
18188
18189 /* If we didn't find it, and not all dies have been loaded,
18190 load them all and try again. */
18191
18192 if (pd == NULL && per_cu->load_all_dies == 0)
18193 {
18194 per_cu->load_all_dies = 1;
18195
18196 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18197 THIS_CU->cu may already be in use. So we can't just free it and
18198 replace its DIEs with the ones we read in. Instead, we leave those
18199 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18200 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18201 set. */
18202 load_partial_comp_unit (per_cu);
18203
18204 pd = per_cu->cu->find_partial_die (sect_off);
18205 }
18206
18207 if (pd == NULL)
18208 internal_error (__FILE__, __LINE__,
18209 _("could not find partial DIE %s "
18210 "in cache [from module %s]\n"),
18211 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18212 return { per_cu->cu, pd };
18213 }
18214
18215 /* See if we can figure out if the class lives in a namespace. We do
18216 this by looking for a member function; its demangled name will
18217 contain namespace info, if there is any. */
18218
18219 static void
18220 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18221 struct dwarf2_cu *cu)
18222 {
18223 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18224 what template types look like, because the demangler
18225 frequently doesn't give the same name as the debug info. We
18226 could fix this by only using the demangled name to get the
18227 prefix (but see comment in read_structure_type). */
18228
18229 struct partial_die_info *real_pdi;
18230 struct partial_die_info *child_pdi;
18231
18232 /* If this DIE (this DIE's specification, if any) has a parent, then
18233 we should not do this. We'll prepend the parent's fully qualified
18234 name when we create the partial symbol. */
18235
18236 real_pdi = struct_pdi;
18237 while (real_pdi->has_specification)
18238 {
18239 auto res = find_partial_die (real_pdi->spec_offset,
18240 real_pdi->spec_is_dwz, cu);
18241 real_pdi = res.pdi;
18242 cu = res.cu;
18243 }
18244
18245 if (real_pdi->die_parent != NULL)
18246 return;
18247
18248 for (child_pdi = struct_pdi->die_child;
18249 child_pdi != NULL;
18250 child_pdi = child_pdi->die_sibling)
18251 {
18252 if (child_pdi->tag == DW_TAG_subprogram
18253 && child_pdi->linkage_name != NULL)
18254 {
18255 gdb::unique_xmalloc_ptr<char> actual_class_name
18256 (language_class_name_from_physname (cu->language_defn,
18257 child_pdi->linkage_name));
18258 if (actual_class_name != NULL)
18259 {
18260 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18261 struct_pdi->name = objfile->intern (actual_class_name.get ());
18262 }
18263 break;
18264 }
18265 }
18266 }
18267
18268 void
18269 partial_die_info::fixup (struct dwarf2_cu *cu)
18270 {
18271 /* Once we've fixed up a die, there's no point in doing so again.
18272 This also avoids a memory leak if we were to call
18273 guess_partial_die_structure_name multiple times. */
18274 if (fixup_called)
18275 return;
18276
18277 /* If we found a reference attribute and the DIE has no name, try
18278 to find a name in the referred to DIE. */
18279
18280 if (name == NULL && has_specification)
18281 {
18282 struct partial_die_info *spec_die;
18283
18284 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18285 spec_die = res.pdi;
18286 cu = res.cu;
18287
18288 spec_die->fixup (cu);
18289
18290 if (spec_die->name)
18291 {
18292 name = spec_die->name;
18293
18294 /* Copy DW_AT_external attribute if it is set. */
18295 if (spec_die->is_external)
18296 is_external = spec_die->is_external;
18297 }
18298 }
18299
18300 /* Set default names for some unnamed DIEs. */
18301
18302 if (name == NULL && tag == DW_TAG_namespace)
18303 name = CP_ANONYMOUS_NAMESPACE_STR;
18304
18305 /* If there is no parent die to provide a namespace, and there are
18306 children, see if we can determine the namespace from their linkage
18307 name. */
18308 if (cu->language == language_cplus
18309 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18310 && die_parent == NULL
18311 && has_children
18312 && (tag == DW_TAG_class_type
18313 || tag == DW_TAG_structure_type
18314 || tag == DW_TAG_union_type))
18315 guess_partial_die_structure_name (this, cu);
18316
18317 /* GCC might emit a nameless struct or union that has a linkage
18318 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18319 if (name == NULL
18320 && (tag == DW_TAG_class_type
18321 || tag == DW_TAG_interface_type
18322 || tag == DW_TAG_structure_type
18323 || tag == DW_TAG_union_type)
18324 && linkage_name != NULL)
18325 {
18326 gdb::unique_xmalloc_ptr<char> demangled
18327 (gdb_demangle (linkage_name, DMGL_TYPES));
18328 if (demangled != nullptr)
18329 {
18330 const char *base;
18331
18332 /* Strip any leading namespaces/classes, keep only the base name.
18333 DW_AT_name for named DIEs does not contain the prefixes. */
18334 base = strrchr (demangled.get (), ':');
18335 if (base && base > demangled.get () && base[-1] == ':')
18336 base++;
18337 else
18338 base = demangled.get ();
18339
18340 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18341 name = objfile->intern (base);
18342 }
18343 }
18344
18345 fixup_called = 1;
18346 }
18347
18348 /* Process the attributes that had to be skipped in the first round. These
18349 attributes are the ones that need str_offsets_base or addr_base attributes.
18350 They could not have been processed in the first round, because at the time
18351 the values of str_offsets_base or addr_base may not have been known. */
18352 void read_attribute_reprocess (const struct die_reader_specs *reader,
18353 struct attribute *attr)
18354 {
18355 struct dwarf2_cu *cu = reader->cu;
18356 switch (attr->form)
18357 {
18358 case DW_FORM_addrx:
18359 case DW_FORM_GNU_addr_index:
18360 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18361 break;
18362 case DW_FORM_strx:
18363 case DW_FORM_strx1:
18364 case DW_FORM_strx2:
18365 case DW_FORM_strx3:
18366 case DW_FORM_strx4:
18367 case DW_FORM_GNU_str_index:
18368 {
18369 unsigned int str_index = DW_UNSND (attr);
18370 if (reader->dwo_file != NULL)
18371 {
18372 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18373 DW_STRING_IS_CANONICAL (attr) = 0;
18374 }
18375 else
18376 {
18377 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18378 DW_STRING_IS_CANONICAL (attr) = 0;
18379 }
18380 break;
18381 }
18382 default:
18383 gdb_assert_not_reached (_("Unexpected DWARF form."));
18384 }
18385 }
18386
18387 /* Read an attribute value described by an attribute form. */
18388
18389 static const gdb_byte *
18390 read_attribute_value (const struct die_reader_specs *reader,
18391 struct attribute *attr, unsigned form,
18392 LONGEST implicit_const, const gdb_byte *info_ptr,
18393 bool *need_reprocess)
18394 {
18395 struct dwarf2_cu *cu = reader->cu;
18396 struct dwarf2_per_objfile *dwarf2_per_objfile
18397 = cu->per_cu->dwarf2_per_objfile;
18398 struct objfile *objfile = dwarf2_per_objfile->objfile;
18399 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18400 bfd *abfd = reader->abfd;
18401 struct comp_unit_head *cu_header = &cu->header;
18402 unsigned int bytes_read;
18403 struct dwarf_block *blk;
18404 *need_reprocess = false;
18405
18406 attr->form = (enum dwarf_form) form;
18407 switch (form)
18408 {
18409 case DW_FORM_ref_addr:
18410 if (cu->header.version == 2)
18411 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18412 &bytes_read);
18413 else
18414 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18415 &bytes_read);
18416 info_ptr += bytes_read;
18417 break;
18418 case DW_FORM_GNU_ref_alt:
18419 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18420 info_ptr += bytes_read;
18421 break;
18422 case DW_FORM_addr:
18423 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18424 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18425 info_ptr += bytes_read;
18426 break;
18427 case DW_FORM_block2:
18428 blk = dwarf_alloc_block (cu);
18429 blk->size = read_2_bytes (abfd, info_ptr);
18430 info_ptr += 2;
18431 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18432 info_ptr += blk->size;
18433 DW_BLOCK (attr) = blk;
18434 break;
18435 case DW_FORM_block4:
18436 blk = dwarf_alloc_block (cu);
18437 blk->size = read_4_bytes (abfd, info_ptr);
18438 info_ptr += 4;
18439 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18440 info_ptr += blk->size;
18441 DW_BLOCK (attr) = blk;
18442 break;
18443 case DW_FORM_data2:
18444 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18445 info_ptr += 2;
18446 break;
18447 case DW_FORM_data4:
18448 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18449 info_ptr += 4;
18450 break;
18451 case DW_FORM_data8:
18452 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18453 info_ptr += 8;
18454 break;
18455 case DW_FORM_data16:
18456 blk = dwarf_alloc_block (cu);
18457 blk->size = 16;
18458 blk->data = read_n_bytes (abfd, info_ptr, 16);
18459 info_ptr += 16;
18460 DW_BLOCK (attr) = blk;
18461 break;
18462 case DW_FORM_sec_offset:
18463 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18464 info_ptr += bytes_read;
18465 break;
18466 case DW_FORM_string:
18467 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18468 DW_STRING_IS_CANONICAL (attr) = 0;
18469 info_ptr += bytes_read;
18470 break;
18471 case DW_FORM_strp:
18472 if (!cu->per_cu->is_dwz)
18473 {
18474 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18475 abfd, info_ptr, cu_header,
18476 &bytes_read);
18477 DW_STRING_IS_CANONICAL (attr) = 0;
18478 info_ptr += bytes_read;
18479 break;
18480 }
18481 /* FALLTHROUGH */
18482 case DW_FORM_line_strp:
18483 if (!cu->per_cu->is_dwz)
18484 {
18485 DW_STRING (attr)
18486 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18487 &bytes_read);
18488 DW_STRING_IS_CANONICAL (attr) = 0;
18489 info_ptr += bytes_read;
18490 break;
18491 }
18492 /* FALLTHROUGH */
18493 case DW_FORM_GNU_strp_alt:
18494 {
18495 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18496 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18497 &bytes_read);
18498
18499 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18500 DW_STRING_IS_CANONICAL (attr) = 0;
18501 info_ptr += bytes_read;
18502 }
18503 break;
18504 case DW_FORM_exprloc:
18505 case DW_FORM_block:
18506 blk = dwarf_alloc_block (cu);
18507 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18508 info_ptr += bytes_read;
18509 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18510 info_ptr += blk->size;
18511 DW_BLOCK (attr) = blk;
18512 break;
18513 case DW_FORM_block1:
18514 blk = dwarf_alloc_block (cu);
18515 blk->size = read_1_byte (abfd, info_ptr);
18516 info_ptr += 1;
18517 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18518 info_ptr += blk->size;
18519 DW_BLOCK (attr) = blk;
18520 break;
18521 case DW_FORM_data1:
18522 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18523 info_ptr += 1;
18524 break;
18525 case DW_FORM_flag:
18526 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18527 info_ptr += 1;
18528 break;
18529 case DW_FORM_flag_present:
18530 DW_UNSND (attr) = 1;
18531 break;
18532 case DW_FORM_sdata:
18533 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18534 info_ptr += bytes_read;
18535 break;
18536 case DW_FORM_udata:
18537 case DW_FORM_rnglistx:
18538 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18539 info_ptr += bytes_read;
18540 break;
18541 case DW_FORM_ref1:
18542 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18543 + read_1_byte (abfd, info_ptr));
18544 info_ptr += 1;
18545 break;
18546 case DW_FORM_ref2:
18547 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18548 + read_2_bytes (abfd, info_ptr));
18549 info_ptr += 2;
18550 break;
18551 case DW_FORM_ref4:
18552 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18553 + read_4_bytes (abfd, info_ptr));
18554 info_ptr += 4;
18555 break;
18556 case DW_FORM_ref8:
18557 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18558 + read_8_bytes (abfd, info_ptr));
18559 info_ptr += 8;
18560 break;
18561 case DW_FORM_ref_sig8:
18562 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18563 info_ptr += 8;
18564 break;
18565 case DW_FORM_ref_udata:
18566 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18567 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18568 info_ptr += bytes_read;
18569 break;
18570 case DW_FORM_indirect:
18571 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18572 info_ptr += bytes_read;
18573 if (form == DW_FORM_implicit_const)
18574 {
18575 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18576 info_ptr += bytes_read;
18577 }
18578 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18579 info_ptr, need_reprocess);
18580 break;
18581 case DW_FORM_implicit_const:
18582 DW_SND (attr) = implicit_const;
18583 break;
18584 case DW_FORM_addrx:
18585 case DW_FORM_GNU_addr_index:
18586 *need_reprocess = true;
18587 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18588 info_ptr += bytes_read;
18589 break;
18590 case DW_FORM_strx:
18591 case DW_FORM_strx1:
18592 case DW_FORM_strx2:
18593 case DW_FORM_strx3:
18594 case DW_FORM_strx4:
18595 case DW_FORM_GNU_str_index:
18596 {
18597 ULONGEST str_index;
18598 if (form == DW_FORM_strx1)
18599 {
18600 str_index = read_1_byte (abfd, info_ptr);
18601 info_ptr += 1;
18602 }
18603 else if (form == DW_FORM_strx2)
18604 {
18605 str_index = read_2_bytes (abfd, info_ptr);
18606 info_ptr += 2;
18607 }
18608 else if (form == DW_FORM_strx3)
18609 {
18610 str_index = read_3_bytes (abfd, info_ptr);
18611 info_ptr += 3;
18612 }
18613 else if (form == DW_FORM_strx4)
18614 {
18615 str_index = read_4_bytes (abfd, info_ptr);
18616 info_ptr += 4;
18617 }
18618 else
18619 {
18620 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18621 info_ptr += bytes_read;
18622 }
18623 *need_reprocess = true;
18624 DW_UNSND (attr) = str_index;
18625 }
18626 break;
18627 default:
18628 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18629 dwarf_form_name (form),
18630 bfd_get_filename (abfd));
18631 }
18632
18633 /* Super hack. */
18634 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18635 attr->form = DW_FORM_GNU_ref_alt;
18636
18637 /* We have seen instances where the compiler tried to emit a byte
18638 size attribute of -1 which ended up being encoded as an unsigned
18639 0xffffffff. Although 0xffffffff is technically a valid size value,
18640 an object of this size seems pretty unlikely so we can relatively
18641 safely treat these cases as if the size attribute was invalid and
18642 treat them as zero by default. */
18643 if (attr->name == DW_AT_byte_size
18644 && form == DW_FORM_data4
18645 && DW_UNSND (attr) >= 0xffffffff)
18646 {
18647 complaint
18648 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18649 hex_string (DW_UNSND (attr)));
18650 DW_UNSND (attr) = 0;
18651 }
18652
18653 return info_ptr;
18654 }
18655
18656 /* Read an attribute described by an abbreviated attribute. */
18657
18658 static const gdb_byte *
18659 read_attribute (const struct die_reader_specs *reader,
18660 struct attribute *attr, struct attr_abbrev *abbrev,
18661 const gdb_byte *info_ptr, bool *need_reprocess)
18662 {
18663 attr->name = abbrev->name;
18664 return read_attribute_value (reader, attr, abbrev->form,
18665 abbrev->implicit_const, info_ptr,
18666 need_reprocess);
18667 }
18668
18669 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18670
18671 static const char *
18672 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18673 LONGEST str_offset)
18674 {
18675 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18676 str_offset, "DW_FORM_strp");
18677 }
18678
18679 /* Return pointer to string at .debug_str offset as read from BUF.
18680 BUF is assumed to be in a compilation unit described by CU_HEADER.
18681 Return *BYTES_READ_PTR count of bytes read from BUF. */
18682
18683 static const char *
18684 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18685 const gdb_byte *buf,
18686 const struct comp_unit_head *cu_header,
18687 unsigned int *bytes_read_ptr)
18688 {
18689 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18690
18691 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
18692 }
18693
18694 /* See read.h. */
18695
18696 const char *
18697 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
18698 const struct comp_unit_head *cu_header,
18699 unsigned int *bytes_read_ptr)
18700 {
18701 bfd *abfd = objfile->obfd;
18702 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18703
18704 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
18705 }
18706
18707 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18708 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18709 ADDR_SIZE is the size of addresses from the CU header. */
18710
18711 static CORE_ADDR
18712 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18713 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18714 int addr_size)
18715 {
18716 struct objfile *objfile = dwarf2_per_objfile->objfile;
18717 bfd *abfd = objfile->obfd;
18718 const gdb_byte *info_ptr;
18719 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18720
18721 dwarf2_per_objfile->addr.read (objfile);
18722 if (dwarf2_per_objfile->addr.buffer == NULL)
18723 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18724 objfile_name (objfile));
18725 if (addr_base_or_zero + addr_index * addr_size
18726 >= dwarf2_per_objfile->addr.size)
18727 error (_("DW_FORM_addr_index pointing outside of "
18728 ".debug_addr section [in module %s]"),
18729 objfile_name (objfile));
18730 info_ptr = (dwarf2_per_objfile->addr.buffer
18731 + addr_base_or_zero + addr_index * addr_size);
18732 if (addr_size == 4)
18733 return bfd_get_32 (abfd, info_ptr);
18734 else
18735 return bfd_get_64 (abfd, info_ptr);
18736 }
18737
18738 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18739
18740 static CORE_ADDR
18741 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18742 {
18743 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18744 cu->addr_base, cu->header.addr_size);
18745 }
18746
18747 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18748
18749 static CORE_ADDR
18750 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18751 unsigned int *bytes_read)
18752 {
18753 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18754 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18755
18756 return read_addr_index (cu, addr_index);
18757 }
18758
18759 /* See read.h. */
18760
18761 CORE_ADDR
18762 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18763 {
18764 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18765 struct dwarf2_cu *cu = per_cu->cu;
18766 gdb::optional<ULONGEST> addr_base;
18767 int addr_size;
18768
18769 /* We need addr_base and addr_size.
18770 If we don't have PER_CU->cu, we have to get it.
18771 Nasty, but the alternative is storing the needed info in PER_CU,
18772 which at this point doesn't seem justified: it's not clear how frequently
18773 it would get used and it would increase the size of every PER_CU.
18774 Entry points like dwarf2_per_cu_addr_size do a similar thing
18775 so we're not in uncharted territory here.
18776 Alas we need to be a bit more complicated as addr_base is contained
18777 in the DIE.
18778
18779 We don't need to read the entire CU(/TU).
18780 We just need the header and top level die.
18781
18782 IWBN to use the aging mechanism to let us lazily later discard the CU.
18783 For now we skip this optimization. */
18784
18785 if (cu != NULL)
18786 {
18787 addr_base = cu->addr_base;
18788 addr_size = cu->header.addr_size;
18789 }
18790 else
18791 {
18792 cutu_reader reader (per_cu, NULL, 0, false);
18793 addr_base = reader.cu->addr_base;
18794 addr_size = reader.cu->header.addr_size;
18795 }
18796
18797 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18798 addr_size);
18799 }
18800
18801 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18802 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18803 DWO file. */
18804
18805 static const char *
18806 read_str_index (struct dwarf2_cu *cu,
18807 struct dwarf2_section_info *str_section,
18808 struct dwarf2_section_info *str_offsets_section,
18809 ULONGEST str_offsets_base, ULONGEST str_index)
18810 {
18811 struct dwarf2_per_objfile *dwarf2_per_objfile
18812 = cu->per_cu->dwarf2_per_objfile;
18813 struct objfile *objfile = dwarf2_per_objfile->objfile;
18814 const char *objf_name = objfile_name (objfile);
18815 bfd *abfd = objfile->obfd;
18816 const gdb_byte *info_ptr;
18817 ULONGEST str_offset;
18818 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18819
18820 str_section->read (objfile);
18821 str_offsets_section->read (objfile);
18822 if (str_section->buffer == NULL)
18823 error (_("%s used without %s section"
18824 " in CU at offset %s [in module %s]"),
18825 form_name, str_section->get_name (),
18826 sect_offset_str (cu->header.sect_off), objf_name);
18827 if (str_offsets_section->buffer == NULL)
18828 error (_("%s used without %s section"
18829 " in CU at offset %s [in module %s]"),
18830 form_name, str_section->get_name (),
18831 sect_offset_str (cu->header.sect_off), objf_name);
18832 info_ptr = (str_offsets_section->buffer
18833 + str_offsets_base
18834 + str_index * cu->header.offset_size);
18835 if (cu->header.offset_size == 4)
18836 str_offset = bfd_get_32 (abfd, info_ptr);
18837 else
18838 str_offset = bfd_get_64 (abfd, info_ptr);
18839 if (str_offset >= str_section->size)
18840 error (_("Offset from %s pointing outside of"
18841 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18842 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18843 return (const char *) (str_section->buffer + str_offset);
18844 }
18845
18846 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18847
18848 static const char *
18849 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18850 {
18851 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18852 ? reader->cu->header.addr_size : 0;
18853 return read_str_index (reader->cu,
18854 &reader->dwo_file->sections.str,
18855 &reader->dwo_file->sections.str_offsets,
18856 str_offsets_base, str_index);
18857 }
18858
18859 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18860
18861 static const char *
18862 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18863 {
18864 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18865 const char *objf_name = objfile_name (objfile);
18866 static const char form_name[] = "DW_FORM_GNU_str_index";
18867 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18868
18869 if (!cu->str_offsets_base.has_value ())
18870 error (_("%s used in Fission stub without %s"
18871 " in CU at offset 0x%lx [in module %s]"),
18872 form_name, str_offsets_attr_name,
18873 (long) cu->header.offset_size, objf_name);
18874
18875 return read_str_index (cu,
18876 &cu->per_cu->dwarf2_per_objfile->str,
18877 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18878 *cu->str_offsets_base, str_index);
18879 }
18880
18881 /* Return the length of an LEB128 number in BUF. */
18882
18883 static int
18884 leb128_size (const gdb_byte *buf)
18885 {
18886 const gdb_byte *begin = buf;
18887 gdb_byte byte;
18888
18889 while (1)
18890 {
18891 byte = *buf++;
18892 if ((byte & 128) == 0)
18893 return buf - begin;
18894 }
18895 }
18896
18897 static void
18898 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18899 {
18900 switch (lang)
18901 {
18902 case DW_LANG_C89:
18903 case DW_LANG_C99:
18904 case DW_LANG_C11:
18905 case DW_LANG_C:
18906 case DW_LANG_UPC:
18907 cu->language = language_c;
18908 break;
18909 case DW_LANG_Java:
18910 case DW_LANG_C_plus_plus:
18911 case DW_LANG_C_plus_plus_11:
18912 case DW_LANG_C_plus_plus_14:
18913 cu->language = language_cplus;
18914 break;
18915 case DW_LANG_D:
18916 cu->language = language_d;
18917 break;
18918 case DW_LANG_Fortran77:
18919 case DW_LANG_Fortran90:
18920 case DW_LANG_Fortran95:
18921 case DW_LANG_Fortran03:
18922 case DW_LANG_Fortran08:
18923 cu->language = language_fortran;
18924 break;
18925 case DW_LANG_Go:
18926 cu->language = language_go;
18927 break;
18928 case DW_LANG_Mips_Assembler:
18929 cu->language = language_asm;
18930 break;
18931 case DW_LANG_Ada83:
18932 case DW_LANG_Ada95:
18933 cu->language = language_ada;
18934 break;
18935 case DW_LANG_Modula2:
18936 cu->language = language_m2;
18937 break;
18938 case DW_LANG_Pascal83:
18939 cu->language = language_pascal;
18940 break;
18941 case DW_LANG_ObjC:
18942 cu->language = language_objc;
18943 break;
18944 case DW_LANG_Rust:
18945 case DW_LANG_Rust_old:
18946 cu->language = language_rust;
18947 break;
18948 case DW_LANG_Cobol74:
18949 case DW_LANG_Cobol85:
18950 default:
18951 cu->language = language_minimal;
18952 break;
18953 }
18954 cu->language_defn = language_def (cu->language);
18955 }
18956
18957 /* Return the named attribute or NULL if not there. */
18958
18959 static struct attribute *
18960 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18961 {
18962 for (;;)
18963 {
18964 unsigned int i;
18965 struct attribute *spec = NULL;
18966
18967 for (i = 0; i < die->num_attrs; ++i)
18968 {
18969 if (die->attrs[i].name == name)
18970 return &die->attrs[i];
18971 if (die->attrs[i].name == DW_AT_specification
18972 || die->attrs[i].name == DW_AT_abstract_origin)
18973 spec = &die->attrs[i];
18974 }
18975
18976 if (!spec)
18977 break;
18978
18979 die = follow_die_ref (die, spec, &cu);
18980 }
18981
18982 return NULL;
18983 }
18984
18985 /* Return the named attribute or NULL if not there,
18986 but do not follow DW_AT_specification, etc.
18987 This is for use in contexts where we're reading .debug_types dies.
18988 Following DW_AT_specification, DW_AT_abstract_origin will take us
18989 back up the chain, and we want to go down. */
18990
18991 static struct attribute *
18992 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
18993 {
18994 unsigned int i;
18995
18996 for (i = 0; i < die->num_attrs; ++i)
18997 if (die->attrs[i].name == name)
18998 return &die->attrs[i];
18999
19000 return NULL;
19001 }
19002
19003 /* Return the string associated with a string-typed attribute, or NULL if it
19004 is either not found or is of an incorrect type. */
19005
19006 static const char *
19007 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19008 {
19009 struct attribute *attr;
19010 const char *str = NULL;
19011
19012 attr = dwarf2_attr (die, name, cu);
19013
19014 if (attr != NULL)
19015 {
19016 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19017 || attr->form == DW_FORM_string
19018 || attr->form == DW_FORM_strx
19019 || attr->form == DW_FORM_strx1
19020 || attr->form == DW_FORM_strx2
19021 || attr->form == DW_FORM_strx3
19022 || attr->form == DW_FORM_strx4
19023 || attr->form == DW_FORM_GNU_str_index
19024 || attr->form == DW_FORM_GNU_strp_alt)
19025 str = DW_STRING (attr);
19026 else
19027 complaint (_("string type expected for attribute %s for "
19028 "DIE at %s in module %s"),
19029 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19030 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19031 }
19032
19033 return str;
19034 }
19035
19036 /* Return the dwo name or NULL if not present. If present, it is in either
19037 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19038 static const char *
19039 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19040 {
19041 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19042 if (dwo_name == nullptr)
19043 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19044 return dwo_name;
19045 }
19046
19047 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19048 and holds a non-zero value. This function should only be used for
19049 DW_FORM_flag or DW_FORM_flag_present attributes. */
19050
19051 static int
19052 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19053 {
19054 struct attribute *attr = dwarf2_attr (die, name, cu);
19055
19056 return (attr && DW_UNSND (attr));
19057 }
19058
19059 static int
19060 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19061 {
19062 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19063 which value is non-zero. However, we have to be careful with
19064 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19065 (via dwarf2_flag_true_p) follows this attribute. So we may
19066 end up accidently finding a declaration attribute that belongs
19067 to a different DIE referenced by the specification attribute,
19068 even though the given DIE does not have a declaration attribute. */
19069 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19070 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19071 }
19072
19073 /* Return the die giving the specification for DIE, if there is
19074 one. *SPEC_CU is the CU containing DIE on input, and the CU
19075 containing the return value on output. If there is no
19076 specification, but there is an abstract origin, that is
19077 returned. */
19078
19079 static struct die_info *
19080 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19081 {
19082 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19083 *spec_cu);
19084
19085 if (spec_attr == NULL)
19086 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19087
19088 if (spec_attr == NULL)
19089 return NULL;
19090 else
19091 return follow_die_ref (die, spec_attr, spec_cu);
19092 }
19093
19094 /* Stub for free_line_header to match void * callback types. */
19095
19096 static void
19097 free_line_header_voidp (void *arg)
19098 {
19099 struct line_header *lh = (struct line_header *) arg;
19100
19101 delete lh;
19102 }
19103
19104 /* A convenience function to find the proper .debug_line section for a CU. */
19105
19106 static struct dwarf2_section_info *
19107 get_debug_line_section (struct dwarf2_cu *cu)
19108 {
19109 struct dwarf2_section_info *section;
19110 struct dwarf2_per_objfile *dwarf2_per_objfile
19111 = cu->per_cu->dwarf2_per_objfile;
19112
19113 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19114 DWO file. */
19115 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19116 section = &cu->dwo_unit->dwo_file->sections.line;
19117 else if (cu->per_cu->is_dwz)
19118 {
19119 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19120
19121 section = &dwz->line;
19122 }
19123 else
19124 section = &dwarf2_per_objfile->line;
19125
19126 return section;
19127 }
19128
19129 /* Read the statement program header starting at OFFSET in
19130 .debug_line, or .debug_line.dwo. Return a pointer
19131 to a struct line_header, allocated using xmalloc.
19132 Returns NULL if there is a problem reading the header, e.g., if it
19133 has a version we don't understand.
19134
19135 NOTE: the strings in the include directory and file name tables of
19136 the returned object point into the dwarf line section buffer,
19137 and must not be freed. */
19138
19139 static line_header_up
19140 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19141 {
19142 struct dwarf2_section_info *section;
19143 struct dwarf2_per_objfile *dwarf2_per_objfile
19144 = cu->per_cu->dwarf2_per_objfile;
19145
19146 section = get_debug_line_section (cu);
19147 section->read (dwarf2_per_objfile->objfile);
19148 if (section->buffer == NULL)
19149 {
19150 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19151 complaint (_("missing .debug_line.dwo section"));
19152 else
19153 complaint (_("missing .debug_line section"));
19154 return 0;
19155 }
19156
19157 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19158 dwarf2_per_objfile, section,
19159 &cu->header);
19160 }
19161
19162 /* Subroutine of dwarf_decode_lines to simplify it.
19163 Return the file name of the psymtab for the given file_entry.
19164 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19165 If space for the result is malloc'd, *NAME_HOLDER will be set.
19166 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19167
19168 static const char *
19169 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19170 const dwarf2_psymtab *pst,
19171 const char *comp_dir,
19172 gdb::unique_xmalloc_ptr<char> *name_holder)
19173 {
19174 const char *include_name = fe.name;
19175 const char *include_name_to_compare = include_name;
19176 const char *pst_filename;
19177 int file_is_pst;
19178
19179 const char *dir_name = fe.include_dir (lh);
19180
19181 gdb::unique_xmalloc_ptr<char> hold_compare;
19182 if (!IS_ABSOLUTE_PATH (include_name)
19183 && (dir_name != NULL || comp_dir != NULL))
19184 {
19185 /* Avoid creating a duplicate psymtab for PST.
19186 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19187 Before we do the comparison, however, we need to account
19188 for DIR_NAME and COMP_DIR.
19189 First prepend dir_name (if non-NULL). If we still don't
19190 have an absolute path prepend comp_dir (if non-NULL).
19191 However, the directory we record in the include-file's
19192 psymtab does not contain COMP_DIR (to match the
19193 corresponding symtab(s)).
19194
19195 Example:
19196
19197 bash$ cd /tmp
19198 bash$ gcc -g ./hello.c
19199 include_name = "hello.c"
19200 dir_name = "."
19201 DW_AT_comp_dir = comp_dir = "/tmp"
19202 DW_AT_name = "./hello.c"
19203
19204 */
19205
19206 if (dir_name != NULL)
19207 {
19208 name_holder->reset (concat (dir_name, SLASH_STRING,
19209 include_name, (char *) NULL));
19210 include_name = name_holder->get ();
19211 include_name_to_compare = include_name;
19212 }
19213 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19214 {
19215 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19216 include_name, (char *) NULL));
19217 include_name_to_compare = hold_compare.get ();
19218 }
19219 }
19220
19221 pst_filename = pst->filename;
19222 gdb::unique_xmalloc_ptr<char> copied_name;
19223 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19224 {
19225 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19226 pst_filename, (char *) NULL));
19227 pst_filename = copied_name.get ();
19228 }
19229
19230 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19231
19232 if (file_is_pst)
19233 return NULL;
19234 return include_name;
19235 }
19236
19237 /* State machine to track the state of the line number program. */
19238
19239 class lnp_state_machine
19240 {
19241 public:
19242 /* Initialize a machine state for the start of a line number
19243 program. */
19244 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19245 bool record_lines_p);
19246
19247 file_entry *current_file ()
19248 {
19249 /* lh->file_names is 0-based, but the file name numbers in the
19250 statement program are 1-based. */
19251 return m_line_header->file_name_at (m_file);
19252 }
19253
19254 /* Record the line in the state machine. END_SEQUENCE is true if
19255 we're processing the end of a sequence. */
19256 void record_line (bool end_sequence);
19257
19258 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19259 nop-out rest of the lines in this sequence. */
19260 void check_line_address (struct dwarf2_cu *cu,
19261 const gdb_byte *line_ptr,
19262 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19263
19264 void handle_set_discriminator (unsigned int discriminator)
19265 {
19266 m_discriminator = discriminator;
19267 m_line_has_non_zero_discriminator |= discriminator != 0;
19268 }
19269
19270 /* Handle DW_LNE_set_address. */
19271 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19272 {
19273 m_op_index = 0;
19274 address += baseaddr;
19275 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19276 }
19277
19278 /* Handle DW_LNS_advance_pc. */
19279 void handle_advance_pc (CORE_ADDR adjust);
19280
19281 /* Handle a special opcode. */
19282 void handle_special_opcode (unsigned char op_code);
19283
19284 /* Handle DW_LNS_advance_line. */
19285 void handle_advance_line (int line_delta)
19286 {
19287 advance_line (line_delta);
19288 }
19289
19290 /* Handle DW_LNS_set_file. */
19291 void handle_set_file (file_name_index file);
19292
19293 /* Handle DW_LNS_negate_stmt. */
19294 void handle_negate_stmt ()
19295 {
19296 m_is_stmt = !m_is_stmt;
19297 }
19298
19299 /* Handle DW_LNS_const_add_pc. */
19300 void handle_const_add_pc ();
19301
19302 /* Handle DW_LNS_fixed_advance_pc. */
19303 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19304 {
19305 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19306 m_op_index = 0;
19307 }
19308
19309 /* Handle DW_LNS_copy. */
19310 void handle_copy ()
19311 {
19312 record_line (false);
19313 m_discriminator = 0;
19314 }
19315
19316 /* Handle DW_LNE_end_sequence. */
19317 void handle_end_sequence ()
19318 {
19319 m_currently_recording_lines = true;
19320 }
19321
19322 private:
19323 /* Advance the line by LINE_DELTA. */
19324 void advance_line (int line_delta)
19325 {
19326 m_line += line_delta;
19327
19328 if (line_delta != 0)
19329 m_line_has_non_zero_discriminator = m_discriminator != 0;
19330 }
19331
19332 struct dwarf2_cu *m_cu;
19333
19334 gdbarch *m_gdbarch;
19335
19336 /* True if we're recording lines.
19337 Otherwise we're building partial symtabs and are just interested in
19338 finding include files mentioned by the line number program. */
19339 bool m_record_lines_p;
19340
19341 /* The line number header. */
19342 line_header *m_line_header;
19343
19344 /* These are part of the standard DWARF line number state machine,
19345 and initialized according to the DWARF spec. */
19346
19347 unsigned char m_op_index = 0;
19348 /* The line table index of the current file. */
19349 file_name_index m_file = 1;
19350 unsigned int m_line = 1;
19351
19352 /* These are initialized in the constructor. */
19353
19354 CORE_ADDR m_address;
19355 bool m_is_stmt;
19356 unsigned int m_discriminator;
19357
19358 /* Additional bits of state we need to track. */
19359
19360 /* The last file that we called dwarf2_start_subfile for.
19361 This is only used for TLLs. */
19362 unsigned int m_last_file = 0;
19363 /* The last file a line number was recorded for. */
19364 struct subfile *m_last_subfile = NULL;
19365
19366 /* When true, record the lines we decode. */
19367 bool m_currently_recording_lines = false;
19368
19369 /* The last line number that was recorded, used to coalesce
19370 consecutive entries for the same line. This can happen, for
19371 example, when discriminators are present. PR 17276. */
19372 unsigned int m_last_line = 0;
19373 bool m_line_has_non_zero_discriminator = false;
19374 };
19375
19376 void
19377 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19378 {
19379 CORE_ADDR addr_adj = (((m_op_index + adjust)
19380 / m_line_header->maximum_ops_per_instruction)
19381 * m_line_header->minimum_instruction_length);
19382 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19383 m_op_index = ((m_op_index + adjust)
19384 % m_line_header->maximum_ops_per_instruction);
19385 }
19386
19387 void
19388 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19389 {
19390 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19391 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19392 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19393 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19394 / m_line_header->maximum_ops_per_instruction)
19395 * m_line_header->minimum_instruction_length);
19396 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19397 m_op_index = ((m_op_index + adj_opcode_d)
19398 % m_line_header->maximum_ops_per_instruction);
19399
19400 int line_delta = m_line_header->line_base + adj_opcode_r;
19401 advance_line (line_delta);
19402 record_line (false);
19403 m_discriminator = 0;
19404 }
19405
19406 void
19407 lnp_state_machine::handle_set_file (file_name_index file)
19408 {
19409 m_file = file;
19410
19411 const file_entry *fe = current_file ();
19412 if (fe == NULL)
19413 dwarf2_debug_line_missing_file_complaint ();
19414 else if (m_record_lines_p)
19415 {
19416 const char *dir = fe->include_dir (m_line_header);
19417
19418 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19419 m_line_has_non_zero_discriminator = m_discriminator != 0;
19420 dwarf2_start_subfile (m_cu, fe->name, dir);
19421 }
19422 }
19423
19424 void
19425 lnp_state_machine::handle_const_add_pc ()
19426 {
19427 CORE_ADDR adjust
19428 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19429
19430 CORE_ADDR addr_adj
19431 = (((m_op_index + adjust)
19432 / m_line_header->maximum_ops_per_instruction)
19433 * m_line_header->minimum_instruction_length);
19434
19435 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19436 m_op_index = ((m_op_index + adjust)
19437 % m_line_header->maximum_ops_per_instruction);
19438 }
19439
19440 /* Return non-zero if we should add LINE to the line number table.
19441 LINE is the line to add, LAST_LINE is the last line that was added,
19442 LAST_SUBFILE is the subfile for LAST_LINE.
19443 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19444 had a non-zero discriminator.
19445
19446 We have to be careful in the presence of discriminators.
19447 E.g., for this line:
19448
19449 for (i = 0; i < 100000; i++);
19450
19451 clang can emit four line number entries for that one line,
19452 each with a different discriminator.
19453 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19454
19455 However, we want gdb to coalesce all four entries into one.
19456 Otherwise the user could stepi into the middle of the line and
19457 gdb would get confused about whether the pc really was in the
19458 middle of the line.
19459
19460 Things are further complicated by the fact that two consecutive
19461 line number entries for the same line is a heuristic used by gcc
19462 to denote the end of the prologue. So we can't just discard duplicate
19463 entries, we have to be selective about it. The heuristic we use is
19464 that we only collapse consecutive entries for the same line if at least
19465 one of those entries has a non-zero discriminator. PR 17276.
19466
19467 Note: Addresses in the line number state machine can never go backwards
19468 within one sequence, thus this coalescing is ok. */
19469
19470 static int
19471 dwarf_record_line_p (struct dwarf2_cu *cu,
19472 unsigned int line, unsigned int last_line,
19473 int line_has_non_zero_discriminator,
19474 struct subfile *last_subfile)
19475 {
19476 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19477 return 1;
19478 if (line != last_line)
19479 return 1;
19480 /* Same line for the same file that we've seen already.
19481 As a last check, for pr 17276, only record the line if the line
19482 has never had a non-zero discriminator. */
19483 if (!line_has_non_zero_discriminator)
19484 return 1;
19485 return 0;
19486 }
19487
19488 /* Use the CU's builder to record line number LINE beginning at
19489 address ADDRESS in the line table of subfile SUBFILE. */
19490
19491 static void
19492 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19493 unsigned int line, CORE_ADDR address, bool is_stmt,
19494 struct dwarf2_cu *cu)
19495 {
19496 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19497
19498 if (dwarf_line_debug)
19499 {
19500 fprintf_unfiltered (gdb_stdlog,
19501 "Recording line %u, file %s, address %s\n",
19502 line, lbasename (subfile->name),
19503 paddress (gdbarch, address));
19504 }
19505
19506 if (cu != nullptr)
19507 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19508 }
19509
19510 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19511 Mark the end of a set of line number records.
19512 The arguments are the same as for dwarf_record_line_1.
19513 If SUBFILE is NULL the request is ignored. */
19514
19515 static void
19516 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19517 CORE_ADDR address, struct dwarf2_cu *cu)
19518 {
19519 if (subfile == NULL)
19520 return;
19521
19522 if (dwarf_line_debug)
19523 {
19524 fprintf_unfiltered (gdb_stdlog,
19525 "Finishing current line, file %s, address %s\n",
19526 lbasename (subfile->name),
19527 paddress (gdbarch, address));
19528 }
19529
19530 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19531 }
19532
19533 void
19534 lnp_state_machine::record_line (bool end_sequence)
19535 {
19536 if (dwarf_line_debug)
19537 {
19538 fprintf_unfiltered (gdb_stdlog,
19539 "Processing actual line %u: file %u,"
19540 " address %s, is_stmt %u, discrim %u%s\n",
19541 m_line, m_file,
19542 paddress (m_gdbarch, m_address),
19543 m_is_stmt, m_discriminator,
19544 (end_sequence ? "\t(end sequence)" : ""));
19545 }
19546
19547 file_entry *fe = current_file ();
19548
19549 if (fe == NULL)
19550 dwarf2_debug_line_missing_file_complaint ();
19551 /* For now we ignore lines not starting on an instruction boundary.
19552 But not when processing end_sequence for compatibility with the
19553 previous version of the code. */
19554 else if (m_op_index == 0 || end_sequence)
19555 {
19556 fe->included_p = 1;
19557 if (m_record_lines_p)
19558 {
19559 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19560 || end_sequence)
19561 {
19562 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19563 m_currently_recording_lines ? m_cu : nullptr);
19564 }
19565
19566 if (!end_sequence)
19567 {
19568 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19569
19570 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19571 m_line_has_non_zero_discriminator,
19572 m_last_subfile))
19573 {
19574 buildsym_compunit *builder = m_cu->get_builder ();
19575 dwarf_record_line_1 (m_gdbarch,
19576 builder->get_current_subfile (),
19577 m_line, m_address, is_stmt,
19578 m_currently_recording_lines ? m_cu : nullptr);
19579 }
19580 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19581 m_last_line = m_line;
19582 }
19583 }
19584 }
19585 }
19586
19587 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19588 line_header *lh, bool record_lines_p)
19589 {
19590 m_cu = cu;
19591 m_gdbarch = arch;
19592 m_record_lines_p = record_lines_p;
19593 m_line_header = lh;
19594
19595 m_currently_recording_lines = true;
19596
19597 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19598 was a line entry for it so that the backend has a chance to adjust it
19599 and also record it in case it needs it. This is currently used by MIPS
19600 code, cf. `mips_adjust_dwarf2_line'. */
19601 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19602 m_is_stmt = lh->default_is_stmt;
19603 m_discriminator = 0;
19604 }
19605
19606 void
19607 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19608 const gdb_byte *line_ptr,
19609 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
19610 {
19611 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19612 the pc range of the CU. However, we restrict the test to only ADDRESS
19613 values of zero to preserve GDB's previous behaviour which is to handle
19614 the specific case of a function being GC'd by the linker. */
19615
19616 if (address == 0 && address < unrelocated_lowpc)
19617 {
19618 /* This line table is for a function which has been
19619 GCd by the linker. Ignore it. PR gdb/12528 */
19620
19621 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19622 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19623
19624 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19625 line_offset, objfile_name (objfile));
19626 m_currently_recording_lines = false;
19627 /* Note: m_currently_recording_lines is left as false until we see
19628 DW_LNE_end_sequence. */
19629 }
19630 }
19631
19632 /* Subroutine of dwarf_decode_lines to simplify it.
19633 Process the line number information in LH.
19634 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19635 program in order to set included_p for every referenced header. */
19636
19637 static void
19638 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19639 const int decode_for_pst_p, CORE_ADDR lowpc)
19640 {
19641 const gdb_byte *line_ptr, *extended_end;
19642 const gdb_byte *line_end;
19643 unsigned int bytes_read, extended_len;
19644 unsigned char op_code, extended_op;
19645 CORE_ADDR baseaddr;
19646 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19647 bfd *abfd = objfile->obfd;
19648 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19649 /* True if we're recording line info (as opposed to building partial
19650 symtabs and just interested in finding include files mentioned by
19651 the line number program). */
19652 bool record_lines_p = !decode_for_pst_p;
19653
19654 baseaddr = objfile->text_section_offset ();
19655
19656 line_ptr = lh->statement_program_start;
19657 line_end = lh->statement_program_end;
19658
19659 /* Read the statement sequences until there's nothing left. */
19660 while (line_ptr < line_end)
19661 {
19662 /* The DWARF line number program state machine. Reset the state
19663 machine at the start of each sequence. */
19664 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
19665 bool end_sequence = false;
19666
19667 if (record_lines_p)
19668 {
19669 /* Start a subfile for the current file of the state
19670 machine. */
19671 const file_entry *fe = state_machine.current_file ();
19672
19673 if (fe != NULL)
19674 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
19675 }
19676
19677 /* Decode the table. */
19678 while (line_ptr < line_end && !end_sequence)
19679 {
19680 op_code = read_1_byte (abfd, line_ptr);
19681 line_ptr += 1;
19682
19683 if (op_code >= lh->opcode_base)
19684 {
19685 /* Special opcode. */
19686 state_machine.handle_special_opcode (op_code);
19687 }
19688 else switch (op_code)
19689 {
19690 case DW_LNS_extended_op:
19691 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19692 &bytes_read);
19693 line_ptr += bytes_read;
19694 extended_end = line_ptr + extended_len;
19695 extended_op = read_1_byte (abfd, line_ptr);
19696 line_ptr += 1;
19697 switch (extended_op)
19698 {
19699 case DW_LNE_end_sequence:
19700 state_machine.handle_end_sequence ();
19701 end_sequence = true;
19702 break;
19703 case DW_LNE_set_address:
19704 {
19705 CORE_ADDR address
19706 = cu->header.read_address (abfd, line_ptr, &bytes_read);
19707 line_ptr += bytes_read;
19708
19709 state_machine.check_line_address (cu, line_ptr,
19710 lowpc - baseaddr, address);
19711 state_machine.handle_set_address (baseaddr, address);
19712 }
19713 break;
19714 case DW_LNE_define_file:
19715 {
19716 const char *cur_file;
19717 unsigned int mod_time, length;
19718 dir_index dindex;
19719
19720 cur_file = read_direct_string (abfd, line_ptr,
19721 &bytes_read);
19722 line_ptr += bytes_read;
19723 dindex = (dir_index)
19724 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19725 line_ptr += bytes_read;
19726 mod_time =
19727 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19728 line_ptr += bytes_read;
19729 length =
19730 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19731 line_ptr += bytes_read;
19732 lh->add_file_name (cur_file, dindex, mod_time, length);
19733 }
19734 break;
19735 case DW_LNE_set_discriminator:
19736 {
19737 /* The discriminator is not interesting to the
19738 debugger; just ignore it. We still need to
19739 check its value though:
19740 if there are consecutive entries for the same
19741 (non-prologue) line we want to coalesce them.
19742 PR 17276. */
19743 unsigned int discr
19744 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19745 line_ptr += bytes_read;
19746
19747 state_machine.handle_set_discriminator (discr);
19748 }
19749 break;
19750 default:
19751 complaint (_("mangled .debug_line section"));
19752 return;
19753 }
19754 /* Make sure that we parsed the extended op correctly. If e.g.
19755 we expected a different address size than the producer used,
19756 we may have read the wrong number of bytes. */
19757 if (line_ptr != extended_end)
19758 {
19759 complaint (_("mangled .debug_line section"));
19760 return;
19761 }
19762 break;
19763 case DW_LNS_copy:
19764 state_machine.handle_copy ();
19765 break;
19766 case DW_LNS_advance_pc:
19767 {
19768 CORE_ADDR adjust
19769 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19770 line_ptr += bytes_read;
19771
19772 state_machine.handle_advance_pc (adjust);
19773 }
19774 break;
19775 case DW_LNS_advance_line:
19776 {
19777 int line_delta
19778 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19779 line_ptr += bytes_read;
19780
19781 state_machine.handle_advance_line (line_delta);
19782 }
19783 break;
19784 case DW_LNS_set_file:
19785 {
19786 file_name_index file
19787 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19788 &bytes_read);
19789 line_ptr += bytes_read;
19790
19791 state_machine.handle_set_file (file);
19792 }
19793 break;
19794 case DW_LNS_set_column:
19795 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19796 line_ptr += bytes_read;
19797 break;
19798 case DW_LNS_negate_stmt:
19799 state_machine.handle_negate_stmt ();
19800 break;
19801 case DW_LNS_set_basic_block:
19802 break;
19803 /* Add to the address register of the state machine the
19804 address increment value corresponding to special opcode
19805 255. I.e., this value is scaled by the minimum
19806 instruction length since special opcode 255 would have
19807 scaled the increment. */
19808 case DW_LNS_const_add_pc:
19809 state_machine.handle_const_add_pc ();
19810 break;
19811 case DW_LNS_fixed_advance_pc:
19812 {
19813 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19814 line_ptr += 2;
19815
19816 state_machine.handle_fixed_advance_pc (addr_adj);
19817 }
19818 break;
19819 default:
19820 {
19821 /* Unknown standard opcode, ignore it. */
19822 int i;
19823
19824 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19825 {
19826 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19827 line_ptr += bytes_read;
19828 }
19829 }
19830 }
19831 }
19832
19833 if (!end_sequence)
19834 dwarf2_debug_line_missing_end_sequence_complaint ();
19835
19836 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19837 in which case we still finish recording the last line). */
19838 state_machine.record_line (true);
19839 }
19840 }
19841
19842 /* Decode the Line Number Program (LNP) for the given line_header
19843 structure and CU. The actual information extracted and the type
19844 of structures created from the LNP depends on the value of PST.
19845
19846 1. If PST is NULL, then this procedure uses the data from the program
19847 to create all necessary symbol tables, and their linetables.
19848
19849 2. If PST is not NULL, this procedure reads the program to determine
19850 the list of files included by the unit represented by PST, and
19851 builds all the associated partial symbol tables.
19852
19853 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19854 It is used for relative paths in the line table.
19855 NOTE: When processing partial symtabs (pst != NULL),
19856 comp_dir == pst->dirname.
19857
19858 NOTE: It is important that psymtabs have the same file name (via strcmp)
19859 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19860 symtab we don't use it in the name of the psymtabs we create.
19861 E.g. expand_line_sal requires this when finding psymtabs to expand.
19862 A good testcase for this is mb-inline.exp.
19863
19864 LOWPC is the lowest address in CU (or 0 if not known).
19865
19866 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19867 for its PC<->lines mapping information. Otherwise only the filename
19868 table is read in. */
19869
19870 static void
19871 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
19872 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
19873 CORE_ADDR lowpc, int decode_mapping)
19874 {
19875 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19876 const int decode_for_pst_p = (pst != NULL);
19877
19878 if (decode_mapping)
19879 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
19880
19881 if (decode_for_pst_p)
19882 {
19883 /* Now that we're done scanning the Line Header Program, we can
19884 create the psymtab of each included file. */
19885 for (auto &file_entry : lh->file_names ())
19886 if (file_entry.included_p == 1)
19887 {
19888 gdb::unique_xmalloc_ptr<char> name_holder;
19889 const char *include_name =
19890 psymtab_include_file_name (lh, file_entry, pst,
19891 comp_dir, &name_holder);
19892 if (include_name != NULL)
19893 dwarf2_create_include_psymtab (include_name, pst, objfile);
19894 }
19895 }
19896 else
19897 {
19898 /* Make sure a symtab is created for every file, even files
19899 which contain only variables (i.e. no code with associated
19900 line numbers). */
19901 buildsym_compunit *builder = cu->get_builder ();
19902 struct compunit_symtab *cust = builder->get_compunit_symtab ();
19903
19904 for (auto &fe : lh->file_names ())
19905 {
19906 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
19907 if (builder->get_current_subfile ()->symtab == NULL)
19908 {
19909 builder->get_current_subfile ()->symtab
19910 = allocate_symtab (cust,
19911 builder->get_current_subfile ()->name);
19912 }
19913 fe.symtab = builder->get_current_subfile ()->symtab;
19914 }
19915 }
19916 }
19917
19918 /* Start a subfile for DWARF. FILENAME is the name of the file and
19919 DIRNAME the name of the source directory which contains FILENAME
19920 or NULL if not known.
19921 This routine tries to keep line numbers from identical absolute and
19922 relative file names in a common subfile.
19923
19924 Using the `list' example from the GDB testsuite, which resides in
19925 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19926 of /srcdir/list0.c yields the following debugging information for list0.c:
19927
19928 DW_AT_name: /srcdir/list0.c
19929 DW_AT_comp_dir: /compdir
19930 files.files[0].name: list0.h
19931 files.files[0].dir: /srcdir
19932 files.files[1].name: list0.c
19933 files.files[1].dir: /srcdir
19934
19935 The line number information for list0.c has to end up in a single
19936 subfile, so that `break /srcdir/list0.c:1' works as expected.
19937 start_subfile will ensure that this happens provided that we pass the
19938 concatenation of files.files[1].dir and files.files[1].name as the
19939 subfile's name. */
19940
19941 static void
19942 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
19943 const char *dirname)
19944 {
19945 gdb::unique_xmalloc_ptr<char> copy;
19946
19947 /* In order not to lose the line information directory,
19948 we concatenate it to the filename when it makes sense.
19949 Note that the Dwarf3 standard says (speaking of filenames in line
19950 information): ``The directory index is ignored for file names
19951 that represent full path names''. Thus ignoring dirname in the
19952 `else' branch below isn't an issue. */
19953
19954 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
19955 {
19956 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
19957 filename = copy.get ();
19958 }
19959
19960 cu->get_builder ()->start_subfile (filename);
19961 }
19962
19963 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
19964 buildsym_compunit constructor. */
19965
19966 struct compunit_symtab *
19967 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
19968 CORE_ADDR low_pc)
19969 {
19970 gdb_assert (m_builder == nullptr);
19971
19972 m_builder.reset (new struct buildsym_compunit
19973 (per_cu->dwarf2_per_objfile->objfile,
19974 name, comp_dir, language, low_pc));
19975
19976 list_in_scope = get_builder ()->get_file_symbols ();
19977
19978 get_builder ()->record_debugformat ("DWARF 2");
19979 get_builder ()->record_producer (producer);
19980
19981 processing_has_namespace_info = false;
19982
19983 return get_builder ()->get_compunit_symtab ();
19984 }
19985
19986 static void
19987 var_decode_location (struct attribute *attr, struct symbol *sym,
19988 struct dwarf2_cu *cu)
19989 {
19990 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19991 struct comp_unit_head *cu_header = &cu->header;
19992
19993 /* NOTE drow/2003-01-30: There used to be a comment and some special
19994 code here to turn a symbol with DW_AT_external and a
19995 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19996 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19997 with some versions of binutils) where shared libraries could have
19998 relocations against symbols in their debug information - the
19999 minimal symbol would have the right address, but the debug info
20000 would not. It's no longer necessary, because we will explicitly
20001 apply relocations when we read in the debug information now. */
20002
20003 /* A DW_AT_location attribute with no contents indicates that a
20004 variable has been optimized away. */
20005 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20006 {
20007 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20008 return;
20009 }
20010
20011 /* Handle one degenerate form of location expression specially, to
20012 preserve GDB's previous behavior when section offsets are
20013 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20014 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20015
20016 if (attr->form_is_block ()
20017 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20018 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20019 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20020 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20021 && (DW_BLOCK (attr)->size
20022 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20023 {
20024 unsigned int dummy;
20025
20026 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20027 SET_SYMBOL_VALUE_ADDRESS
20028 (sym, cu->header.read_address (objfile->obfd,
20029 DW_BLOCK (attr)->data + 1,
20030 &dummy));
20031 else
20032 SET_SYMBOL_VALUE_ADDRESS
20033 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20034 &dummy));
20035 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20036 fixup_symbol_section (sym, objfile);
20037 SET_SYMBOL_VALUE_ADDRESS
20038 (sym,
20039 SYMBOL_VALUE_ADDRESS (sym)
20040 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20041 return;
20042 }
20043
20044 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20045 expression evaluator, and use LOC_COMPUTED only when necessary
20046 (i.e. when the value of a register or memory location is
20047 referenced, or a thread-local block, etc.). Then again, it might
20048 not be worthwhile. I'm assuming that it isn't unless performance
20049 or memory numbers show me otherwise. */
20050
20051 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20052
20053 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20054 cu->has_loclist = true;
20055 }
20056
20057 /* Given a pointer to a DWARF information entry, figure out if we need
20058 to make a symbol table entry for it, and if so, create a new entry
20059 and return a pointer to it.
20060 If TYPE is NULL, determine symbol type from the die, otherwise
20061 used the passed type.
20062 If SPACE is not NULL, use it to hold the new symbol. If it is
20063 NULL, allocate a new symbol on the objfile's obstack. */
20064
20065 static struct symbol *
20066 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20067 struct symbol *space)
20068 {
20069 struct dwarf2_per_objfile *dwarf2_per_objfile
20070 = cu->per_cu->dwarf2_per_objfile;
20071 struct objfile *objfile = dwarf2_per_objfile->objfile;
20072 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20073 struct symbol *sym = NULL;
20074 const char *name;
20075 struct attribute *attr = NULL;
20076 struct attribute *attr2 = NULL;
20077 CORE_ADDR baseaddr;
20078 struct pending **list_to_add = NULL;
20079
20080 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20081
20082 baseaddr = objfile->text_section_offset ();
20083
20084 name = dwarf2_name (die, cu);
20085 if (name)
20086 {
20087 const char *linkagename;
20088 int suppress_add = 0;
20089
20090 if (space)
20091 sym = space;
20092 else
20093 sym = allocate_symbol (objfile);
20094 OBJSTAT (objfile, n_syms++);
20095
20096 /* Cache this symbol's name and the name's demangled form (if any). */
20097 sym->set_language (cu->language, &objfile->objfile_obstack);
20098 linkagename = dwarf2_physname (name, die, cu);
20099 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20100
20101 /* Fortran does not have mangling standard and the mangling does differ
20102 between gfortran, iFort etc. */
20103 if (cu->language == language_fortran
20104 && symbol_get_demangled_name (sym) == NULL)
20105 symbol_set_demangled_name (sym,
20106 dwarf2_full_name (name, die, cu),
20107 NULL);
20108
20109 /* Default assumptions.
20110 Use the passed type or decode it from the die. */
20111 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20112 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20113 if (type != NULL)
20114 SYMBOL_TYPE (sym) = type;
20115 else
20116 SYMBOL_TYPE (sym) = die_type (die, cu);
20117 attr = dwarf2_attr (die,
20118 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20119 cu);
20120 if (attr != nullptr)
20121 {
20122 SYMBOL_LINE (sym) = DW_UNSND (attr);
20123 }
20124
20125 attr = dwarf2_attr (die,
20126 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20127 cu);
20128 if (attr != nullptr)
20129 {
20130 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20131 struct file_entry *fe;
20132
20133 if (cu->line_header != NULL)
20134 fe = cu->line_header->file_name_at (file_index);
20135 else
20136 fe = NULL;
20137
20138 if (fe == NULL)
20139 complaint (_("file index out of range"));
20140 else
20141 symbol_set_symtab (sym, fe->symtab);
20142 }
20143
20144 switch (die->tag)
20145 {
20146 case DW_TAG_label:
20147 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20148 if (attr != nullptr)
20149 {
20150 CORE_ADDR addr;
20151
20152 addr = attr->value_as_address ();
20153 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20154 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20155 }
20156 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20157 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20158 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20159 add_symbol_to_list (sym, cu->list_in_scope);
20160 break;
20161 case DW_TAG_subprogram:
20162 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20163 finish_block. */
20164 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20165 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20166 if ((attr2 && (DW_UNSND (attr2) != 0))
20167 || cu->language == language_ada
20168 || cu->language == language_fortran)
20169 {
20170 /* Subprograms marked external are stored as a global symbol.
20171 Ada and Fortran subprograms, whether marked external or
20172 not, are always stored as a global symbol, because we want
20173 to be able to access them globally. For instance, we want
20174 to be able to break on a nested subprogram without having
20175 to specify the context. */
20176 list_to_add = cu->get_builder ()->get_global_symbols ();
20177 }
20178 else
20179 {
20180 list_to_add = cu->list_in_scope;
20181 }
20182 break;
20183 case DW_TAG_inlined_subroutine:
20184 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20185 finish_block. */
20186 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20187 SYMBOL_INLINED (sym) = 1;
20188 list_to_add = cu->list_in_scope;
20189 break;
20190 case DW_TAG_template_value_param:
20191 suppress_add = 1;
20192 /* Fall through. */
20193 case DW_TAG_constant:
20194 case DW_TAG_variable:
20195 case DW_TAG_member:
20196 /* Compilation with minimal debug info may result in
20197 variables with missing type entries. Change the
20198 misleading `void' type to something sensible. */
20199 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20200 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20201
20202 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20203 /* In the case of DW_TAG_member, we should only be called for
20204 static const members. */
20205 if (die->tag == DW_TAG_member)
20206 {
20207 /* dwarf2_add_field uses die_is_declaration,
20208 so we do the same. */
20209 gdb_assert (die_is_declaration (die, cu));
20210 gdb_assert (attr);
20211 }
20212 if (attr != nullptr)
20213 {
20214 dwarf2_const_value (attr, sym, cu);
20215 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20216 if (!suppress_add)
20217 {
20218 if (attr2 && (DW_UNSND (attr2) != 0))
20219 list_to_add = cu->get_builder ()->get_global_symbols ();
20220 else
20221 list_to_add = cu->list_in_scope;
20222 }
20223 break;
20224 }
20225 attr = dwarf2_attr (die, DW_AT_location, cu);
20226 if (attr != nullptr)
20227 {
20228 var_decode_location (attr, sym, cu);
20229 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20230
20231 /* Fortran explicitly imports any global symbols to the local
20232 scope by DW_TAG_common_block. */
20233 if (cu->language == language_fortran && die->parent
20234 && die->parent->tag == DW_TAG_common_block)
20235 attr2 = NULL;
20236
20237 if (SYMBOL_CLASS (sym) == LOC_STATIC
20238 && SYMBOL_VALUE_ADDRESS (sym) == 0
20239 && !dwarf2_per_objfile->has_section_at_zero)
20240 {
20241 /* When a static variable is eliminated by the linker,
20242 the corresponding debug information is not stripped
20243 out, but the variable address is set to null;
20244 do not add such variables into symbol table. */
20245 }
20246 else if (attr2 && (DW_UNSND (attr2) != 0))
20247 {
20248 if (SYMBOL_CLASS (sym) == LOC_STATIC
20249 && (objfile->flags & OBJF_MAINLINE) == 0
20250 && dwarf2_per_objfile->can_copy)
20251 {
20252 /* A global static variable might be subject to
20253 copy relocation. We first check for a local
20254 minsym, though, because maybe the symbol was
20255 marked hidden, in which case this would not
20256 apply. */
20257 bound_minimal_symbol found
20258 = (lookup_minimal_symbol_linkage
20259 (sym->linkage_name (), objfile));
20260 if (found.minsym != nullptr)
20261 sym->maybe_copied = 1;
20262 }
20263
20264 /* A variable with DW_AT_external is never static,
20265 but it may be block-scoped. */
20266 list_to_add
20267 = ((cu->list_in_scope
20268 == cu->get_builder ()->get_file_symbols ())
20269 ? cu->get_builder ()->get_global_symbols ()
20270 : cu->list_in_scope);
20271 }
20272 else
20273 list_to_add = cu->list_in_scope;
20274 }
20275 else
20276 {
20277 /* We do not know the address of this symbol.
20278 If it is an external symbol and we have type information
20279 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20280 The address of the variable will then be determined from
20281 the minimal symbol table whenever the variable is
20282 referenced. */
20283 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20284
20285 /* Fortran explicitly imports any global symbols to the local
20286 scope by DW_TAG_common_block. */
20287 if (cu->language == language_fortran && die->parent
20288 && die->parent->tag == DW_TAG_common_block)
20289 {
20290 /* SYMBOL_CLASS doesn't matter here because
20291 read_common_block is going to reset it. */
20292 if (!suppress_add)
20293 list_to_add = cu->list_in_scope;
20294 }
20295 else if (attr2 && (DW_UNSND (attr2) != 0)
20296 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20297 {
20298 /* A variable with DW_AT_external is never static, but it
20299 may be block-scoped. */
20300 list_to_add
20301 = ((cu->list_in_scope
20302 == cu->get_builder ()->get_file_symbols ())
20303 ? cu->get_builder ()->get_global_symbols ()
20304 : cu->list_in_scope);
20305
20306 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20307 }
20308 else if (!die_is_declaration (die, cu))
20309 {
20310 /* Use the default LOC_OPTIMIZED_OUT class. */
20311 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20312 if (!suppress_add)
20313 list_to_add = cu->list_in_scope;
20314 }
20315 }
20316 break;
20317 case DW_TAG_formal_parameter:
20318 {
20319 /* If we are inside a function, mark this as an argument. If
20320 not, we might be looking at an argument to an inlined function
20321 when we do not have enough information to show inlined frames;
20322 pretend it's a local variable in that case so that the user can
20323 still see it. */
20324 struct context_stack *curr
20325 = cu->get_builder ()->get_current_context_stack ();
20326 if (curr != nullptr && curr->name != nullptr)
20327 SYMBOL_IS_ARGUMENT (sym) = 1;
20328 attr = dwarf2_attr (die, DW_AT_location, cu);
20329 if (attr != nullptr)
20330 {
20331 var_decode_location (attr, sym, cu);
20332 }
20333 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20334 if (attr != nullptr)
20335 {
20336 dwarf2_const_value (attr, sym, cu);
20337 }
20338
20339 list_to_add = cu->list_in_scope;
20340 }
20341 break;
20342 case DW_TAG_unspecified_parameters:
20343 /* From varargs functions; gdb doesn't seem to have any
20344 interest in this information, so just ignore it for now.
20345 (FIXME?) */
20346 break;
20347 case DW_TAG_template_type_param:
20348 suppress_add = 1;
20349 /* Fall through. */
20350 case DW_TAG_class_type:
20351 case DW_TAG_interface_type:
20352 case DW_TAG_structure_type:
20353 case DW_TAG_union_type:
20354 case DW_TAG_set_type:
20355 case DW_TAG_enumeration_type:
20356 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20357 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20358
20359 {
20360 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20361 really ever be static objects: otherwise, if you try
20362 to, say, break of a class's method and you're in a file
20363 which doesn't mention that class, it won't work unless
20364 the check for all static symbols in lookup_symbol_aux
20365 saves you. See the OtherFileClass tests in
20366 gdb.c++/namespace.exp. */
20367
20368 if (!suppress_add)
20369 {
20370 buildsym_compunit *builder = cu->get_builder ();
20371 list_to_add
20372 = (cu->list_in_scope == builder->get_file_symbols ()
20373 && cu->language == language_cplus
20374 ? builder->get_global_symbols ()
20375 : cu->list_in_scope);
20376
20377 /* The semantics of C++ state that "struct foo {
20378 ... }" also defines a typedef for "foo". */
20379 if (cu->language == language_cplus
20380 || cu->language == language_ada
20381 || cu->language == language_d
20382 || cu->language == language_rust)
20383 {
20384 /* The symbol's name is already allocated along
20385 with this objfile, so we don't need to
20386 duplicate it for the type. */
20387 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20388 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20389 }
20390 }
20391 }
20392 break;
20393 case DW_TAG_typedef:
20394 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20395 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20396 list_to_add = cu->list_in_scope;
20397 break;
20398 case DW_TAG_base_type:
20399 case DW_TAG_subrange_type:
20400 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20401 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20402 list_to_add = cu->list_in_scope;
20403 break;
20404 case DW_TAG_enumerator:
20405 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20406 if (attr != nullptr)
20407 {
20408 dwarf2_const_value (attr, sym, cu);
20409 }
20410 {
20411 /* NOTE: carlton/2003-11-10: See comment above in the
20412 DW_TAG_class_type, etc. block. */
20413
20414 list_to_add
20415 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20416 && cu->language == language_cplus
20417 ? cu->get_builder ()->get_global_symbols ()
20418 : cu->list_in_scope);
20419 }
20420 break;
20421 case DW_TAG_imported_declaration:
20422 case DW_TAG_namespace:
20423 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20424 list_to_add = cu->get_builder ()->get_global_symbols ();
20425 break;
20426 case DW_TAG_module:
20427 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20428 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20429 list_to_add = cu->get_builder ()->get_global_symbols ();
20430 break;
20431 case DW_TAG_common_block:
20432 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20433 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20434 add_symbol_to_list (sym, cu->list_in_scope);
20435 break;
20436 default:
20437 /* Not a tag we recognize. Hopefully we aren't processing
20438 trash data, but since we must specifically ignore things
20439 we don't recognize, there is nothing else we should do at
20440 this point. */
20441 complaint (_("unsupported tag: '%s'"),
20442 dwarf_tag_name (die->tag));
20443 break;
20444 }
20445
20446 if (suppress_add)
20447 {
20448 sym->hash_next = objfile->template_symbols;
20449 objfile->template_symbols = sym;
20450 list_to_add = NULL;
20451 }
20452
20453 if (list_to_add != NULL)
20454 add_symbol_to_list (sym, list_to_add);
20455
20456 /* For the benefit of old versions of GCC, check for anonymous
20457 namespaces based on the demangled name. */
20458 if (!cu->processing_has_namespace_info
20459 && cu->language == language_cplus)
20460 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20461 }
20462 return (sym);
20463 }
20464
20465 /* Given an attr with a DW_FORM_dataN value in host byte order,
20466 zero-extend it as appropriate for the symbol's type. The DWARF
20467 standard (v4) is not entirely clear about the meaning of using
20468 DW_FORM_dataN for a constant with a signed type, where the type is
20469 wider than the data. The conclusion of a discussion on the DWARF
20470 list was that this is unspecified. We choose to always zero-extend
20471 because that is the interpretation long in use by GCC. */
20472
20473 static gdb_byte *
20474 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20475 struct dwarf2_cu *cu, LONGEST *value, int bits)
20476 {
20477 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20478 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20479 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20480 LONGEST l = DW_UNSND (attr);
20481
20482 if (bits < sizeof (*value) * 8)
20483 {
20484 l &= ((LONGEST) 1 << bits) - 1;
20485 *value = l;
20486 }
20487 else if (bits == sizeof (*value) * 8)
20488 *value = l;
20489 else
20490 {
20491 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20492 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20493 return bytes;
20494 }
20495
20496 return NULL;
20497 }
20498
20499 /* Read a constant value from an attribute. Either set *VALUE, or if
20500 the value does not fit in *VALUE, set *BYTES - either already
20501 allocated on the objfile obstack, or newly allocated on OBSTACK,
20502 or, set *BATON, if we translated the constant to a location
20503 expression. */
20504
20505 static void
20506 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20507 const char *name, struct obstack *obstack,
20508 struct dwarf2_cu *cu,
20509 LONGEST *value, const gdb_byte **bytes,
20510 struct dwarf2_locexpr_baton **baton)
20511 {
20512 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20513 struct comp_unit_head *cu_header = &cu->header;
20514 struct dwarf_block *blk;
20515 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20516 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20517
20518 *value = 0;
20519 *bytes = NULL;
20520 *baton = NULL;
20521
20522 switch (attr->form)
20523 {
20524 case DW_FORM_addr:
20525 case DW_FORM_addrx:
20526 case DW_FORM_GNU_addr_index:
20527 {
20528 gdb_byte *data;
20529
20530 if (TYPE_LENGTH (type) != cu_header->addr_size)
20531 dwarf2_const_value_length_mismatch_complaint (name,
20532 cu_header->addr_size,
20533 TYPE_LENGTH (type));
20534 /* Symbols of this form are reasonably rare, so we just
20535 piggyback on the existing location code rather than writing
20536 a new implementation of symbol_computed_ops. */
20537 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20538 (*baton)->per_cu = cu->per_cu;
20539 gdb_assert ((*baton)->per_cu);
20540
20541 (*baton)->size = 2 + cu_header->addr_size;
20542 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20543 (*baton)->data = data;
20544
20545 data[0] = DW_OP_addr;
20546 store_unsigned_integer (&data[1], cu_header->addr_size,
20547 byte_order, DW_ADDR (attr));
20548 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20549 }
20550 break;
20551 case DW_FORM_string:
20552 case DW_FORM_strp:
20553 case DW_FORM_strx:
20554 case DW_FORM_GNU_str_index:
20555 case DW_FORM_GNU_strp_alt:
20556 /* DW_STRING is already allocated on the objfile obstack, point
20557 directly to it. */
20558 *bytes = (const gdb_byte *) DW_STRING (attr);
20559 break;
20560 case DW_FORM_block1:
20561 case DW_FORM_block2:
20562 case DW_FORM_block4:
20563 case DW_FORM_block:
20564 case DW_FORM_exprloc:
20565 case DW_FORM_data16:
20566 blk = DW_BLOCK (attr);
20567 if (TYPE_LENGTH (type) != blk->size)
20568 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20569 TYPE_LENGTH (type));
20570 *bytes = blk->data;
20571 break;
20572
20573 /* The DW_AT_const_value attributes are supposed to carry the
20574 symbol's value "represented as it would be on the target
20575 architecture." By the time we get here, it's already been
20576 converted to host endianness, so we just need to sign- or
20577 zero-extend it as appropriate. */
20578 case DW_FORM_data1:
20579 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20580 break;
20581 case DW_FORM_data2:
20582 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20583 break;
20584 case DW_FORM_data4:
20585 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20586 break;
20587 case DW_FORM_data8:
20588 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20589 break;
20590
20591 case DW_FORM_sdata:
20592 case DW_FORM_implicit_const:
20593 *value = DW_SND (attr);
20594 break;
20595
20596 case DW_FORM_udata:
20597 *value = DW_UNSND (attr);
20598 break;
20599
20600 default:
20601 complaint (_("unsupported const value attribute form: '%s'"),
20602 dwarf_form_name (attr->form));
20603 *value = 0;
20604 break;
20605 }
20606 }
20607
20608
20609 /* Copy constant value from an attribute to a symbol. */
20610
20611 static void
20612 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20613 struct dwarf2_cu *cu)
20614 {
20615 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20616 LONGEST value;
20617 const gdb_byte *bytes;
20618 struct dwarf2_locexpr_baton *baton;
20619
20620 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20621 sym->print_name (),
20622 &objfile->objfile_obstack, cu,
20623 &value, &bytes, &baton);
20624
20625 if (baton != NULL)
20626 {
20627 SYMBOL_LOCATION_BATON (sym) = baton;
20628 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20629 }
20630 else if (bytes != NULL)
20631 {
20632 SYMBOL_VALUE_BYTES (sym) = bytes;
20633 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20634 }
20635 else
20636 {
20637 SYMBOL_VALUE (sym) = value;
20638 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20639 }
20640 }
20641
20642 /* Return the type of the die in question using its DW_AT_type attribute. */
20643
20644 static struct type *
20645 die_type (struct die_info *die, struct dwarf2_cu *cu)
20646 {
20647 struct attribute *type_attr;
20648
20649 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20650 if (!type_attr)
20651 {
20652 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20653 /* A missing DW_AT_type represents a void type. */
20654 return objfile_type (objfile)->builtin_void;
20655 }
20656
20657 return lookup_die_type (die, type_attr, cu);
20658 }
20659
20660 /* True iff CU's producer generates GNAT Ada auxiliary information
20661 that allows to find parallel types through that information instead
20662 of having to do expensive parallel lookups by type name. */
20663
20664 static int
20665 need_gnat_info (struct dwarf2_cu *cu)
20666 {
20667 /* Assume that the Ada compiler was GNAT, which always produces
20668 the auxiliary information. */
20669 return (cu->language == language_ada);
20670 }
20671
20672 /* Return the auxiliary type of the die in question using its
20673 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20674 attribute is not present. */
20675
20676 static struct type *
20677 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20678 {
20679 struct attribute *type_attr;
20680
20681 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20682 if (!type_attr)
20683 return NULL;
20684
20685 return lookup_die_type (die, type_attr, cu);
20686 }
20687
20688 /* If DIE has a descriptive_type attribute, then set the TYPE's
20689 descriptive type accordingly. */
20690
20691 static void
20692 set_descriptive_type (struct type *type, struct die_info *die,
20693 struct dwarf2_cu *cu)
20694 {
20695 struct type *descriptive_type = die_descriptive_type (die, cu);
20696
20697 if (descriptive_type)
20698 {
20699 ALLOCATE_GNAT_AUX_TYPE (type);
20700 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20701 }
20702 }
20703
20704 /* Return the containing type of the die in question using its
20705 DW_AT_containing_type attribute. */
20706
20707 static struct type *
20708 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20709 {
20710 struct attribute *type_attr;
20711 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20712
20713 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20714 if (!type_attr)
20715 error (_("Dwarf Error: Problem turning containing type into gdb type "
20716 "[in module %s]"), objfile_name (objfile));
20717
20718 return lookup_die_type (die, type_attr, cu);
20719 }
20720
20721 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20722
20723 static struct type *
20724 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20725 {
20726 struct dwarf2_per_objfile *dwarf2_per_objfile
20727 = cu->per_cu->dwarf2_per_objfile;
20728 struct objfile *objfile = dwarf2_per_objfile->objfile;
20729 char *saved;
20730
20731 std::string message
20732 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20733 objfile_name (objfile),
20734 sect_offset_str (cu->header.sect_off),
20735 sect_offset_str (die->sect_off));
20736 saved = obstack_strdup (&objfile->objfile_obstack, message);
20737
20738 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20739 }
20740
20741 /* Look up the type of DIE in CU using its type attribute ATTR.
20742 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20743 DW_AT_containing_type.
20744 If there is no type substitute an error marker. */
20745
20746 static struct type *
20747 lookup_die_type (struct die_info *die, const struct attribute *attr,
20748 struct dwarf2_cu *cu)
20749 {
20750 struct dwarf2_per_objfile *dwarf2_per_objfile
20751 = cu->per_cu->dwarf2_per_objfile;
20752 struct objfile *objfile = dwarf2_per_objfile->objfile;
20753 struct type *this_type;
20754
20755 gdb_assert (attr->name == DW_AT_type
20756 || attr->name == DW_AT_GNAT_descriptive_type
20757 || attr->name == DW_AT_containing_type);
20758
20759 /* First see if we have it cached. */
20760
20761 if (attr->form == DW_FORM_GNU_ref_alt)
20762 {
20763 struct dwarf2_per_cu_data *per_cu;
20764 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20765
20766 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20767 dwarf2_per_objfile);
20768 this_type = get_die_type_at_offset (sect_off, per_cu);
20769 }
20770 else if (attr->form_is_ref ())
20771 {
20772 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20773
20774 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20775 }
20776 else if (attr->form == DW_FORM_ref_sig8)
20777 {
20778 ULONGEST signature = DW_SIGNATURE (attr);
20779
20780 return get_signatured_type (die, signature, cu);
20781 }
20782 else
20783 {
20784 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
20785 " at %s [in module %s]"),
20786 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
20787 objfile_name (objfile));
20788 return build_error_marker_type (cu, die);
20789 }
20790
20791 /* If not cached we need to read it in. */
20792
20793 if (this_type == NULL)
20794 {
20795 struct die_info *type_die = NULL;
20796 struct dwarf2_cu *type_cu = cu;
20797
20798 if (attr->form_is_ref ())
20799 type_die = follow_die_ref (die, attr, &type_cu);
20800 if (type_die == NULL)
20801 return build_error_marker_type (cu, die);
20802 /* If we find the type now, it's probably because the type came
20803 from an inter-CU reference and the type's CU got expanded before
20804 ours. */
20805 this_type = read_type_die (type_die, type_cu);
20806 }
20807
20808 /* If we still don't have a type use an error marker. */
20809
20810 if (this_type == NULL)
20811 return build_error_marker_type (cu, die);
20812
20813 return this_type;
20814 }
20815
20816 /* Return the type in DIE, CU.
20817 Returns NULL for invalid types.
20818
20819 This first does a lookup in die_type_hash,
20820 and only reads the die in if necessary.
20821
20822 NOTE: This can be called when reading in partial or full symbols. */
20823
20824 static struct type *
20825 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20826 {
20827 struct type *this_type;
20828
20829 this_type = get_die_type (die, cu);
20830 if (this_type)
20831 return this_type;
20832
20833 return read_type_die_1 (die, cu);
20834 }
20835
20836 /* Read the type in DIE, CU.
20837 Returns NULL for invalid types. */
20838
20839 static struct type *
20840 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20841 {
20842 struct type *this_type = NULL;
20843
20844 switch (die->tag)
20845 {
20846 case DW_TAG_class_type:
20847 case DW_TAG_interface_type:
20848 case DW_TAG_structure_type:
20849 case DW_TAG_union_type:
20850 this_type = read_structure_type (die, cu);
20851 break;
20852 case DW_TAG_enumeration_type:
20853 this_type = read_enumeration_type (die, cu);
20854 break;
20855 case DW_TAG_subprogram:
20856 case DW_TAG_subroutine_type:
20857 case DW_TAG_inlined_subroutine:
20858 this_type = read_subroutine_type (die, cu);
20859 break;
20860 case DW_TAG_array_type:
20861 this_type = read_array_type (die, cu);
20862 break;
20863 case DW_TAG_set_type:
20864 this_type = read_set_type (die, cu);
20865 break;
20866 case DW_TAG_pointer_type:
20867 this_type = read_tag_pointer_type (die, cu);
20868 break;
20869 case DW_TAG_ptr_to_member_type:
20870 this_type = read_tag_ptr_to_member_type (die, cu);
20871 break;
20872 case DW_TAG_reference_type:
20873 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20874 break;
20875 case DW_TAG_rvalue_reference_type:
20876 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
20877 break;
20878 case DW_TAG_const_type:
20879 this_type = read_tag_const_type (die, cu);
20880 break;
20881 case DW_TAG_volatile_type:
20882 this_type = read_tag_volatile_type (die, cu);
20883 break;
20884 case DW_TAG_restrict_type:
20885 this_type = read_tag_restrict_type (die, cu);
20886 break;
20887 case DW_TAG_string_type:
20888 this_type = read_tag_string_type (die, cu);
20889 break;
20890 case DW_TAG_typedef:
20891 this_type = read_typedef (die, cu);
20892 break;
20893 case DW_TAG_subrange_type:
20894 this_type = read_subrange_type (die, cu);
20895 break;
20896 case DW_TAG_base_type:
20897 this_type = read_base_type (die, cu);
20898 break;
20899 case DW_TAG_unspecified_type:
20900 this_type = read_unspecified_type (die, cu);
20901 break;
20902 case DW_TAG_namespace:
20903 this_type = read_namespace_type (die, cu);
20904 break;
20905 case DW_TAG_module:
20906 this_type = read_module_type (die, cu);
20907 break;
20908 case DW_TAG_atomic_type:
20909 this_type = read_tag_atomic_type (die, cu);
20910 break;
20911 default:
20912 complaint (_("unexpected tag in read_type_die: '%s'"),
20913 dwarf_tag_name (die->tag));
20914 break;
20915 }
20916
20917 return this_type;
20918 }
20919
20920 /* See if we can figure out if the class lives in a namespace. We do
20921 this by looking for a member function; its demangled name will
20922 contain namespace info, if there is any.
20923 Return the computed name or NULL.
20924 Space for the result is allocated on the objfile's obstack.
20925 This is the full-die version of guess_partial_die_structure_name.
20926 In this case we know DIE has no useful parent. */
20927
20928 static const char *
20929 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20930 {
20931 struct die_info *spec_die;
20932 struct dwarf2_cu *spec_cu;
20933 struct die_info *child;
20934 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20935
20936 spec_cu = cu;
20937 spec_die = die_specification (die, &spec_cu);
20938 if (spec_die != NULL)
20939 {
20940 die = spec_die;
20941 cu = spec_cu;
20942 }
20943
20944 for (child = die->child;
20945 child != NULL;
20946 child = child->sibling)
20947 {
20948 if (child->tag == DW_TAG_subprogram)
20949 {
20950 const char *linkage_name = dw2_linkage_name (child, cu);
20951
20952 if (linkage_name != NULL)
20953 {
20954 gdb::unique_xmalloc_ptr<char> actual_name
20955 (language_class_name_from_physname (cu->language_defn,
20956 linkage_name));
20957 const char *name = NULL;
20958
20959 if (actual_name != NULL)
20960 {
20961 const char *die_name = dwarf2_name (die, cu);
20962
20963 if (die_name != NULL
20964 && strcmp (die_name, actual_name.get ()) != 0)
20965 {
20966 /* Strip off the class name from the full name.
20967 We want the prefix. */
20968 int die_name_len = strlen (die_name);
20969 int actual_name_len = strlen (actual_name.get ());
20970 const char *ptr = actual_name.get ();
20971
20972 /* Test for '::' as a sanity check. */
20973 if (actual_name_len > die_name_len + 2
20974 && ptr[actual_name_len - die_name_len - 1] == ':')
20975 name = obstack_strndup (
20976 &objfile->per_bfd->storage_obstack,
20977 ptr, actual_name_len - die_name_len - 2);
20978 }
20979 }
20980 return name;
20981 }
20982 }
20983 }
20984
20985 return NULL;
20986 }
20987
20988 /* GCC might emit a nameless typedef that has a linkage name. Determine the
20989 prefix part in such case. See
20990 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20991
20992 static const char *
20993 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20994 {
20995 struct attribute *attr;
20996 const char *base;
20997
20998 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20999 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21000 return NULL;
21001
21002 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21003 return NULL;
21004
21005 attr = dw2_linkage_name_attr (die, cu);
21006 if (attr == NULL || DW_STRING (attr) == NULL)
21007 return NULL;
21008
21009 /* dwarf2_name had to be already called. */
21010 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21011
21012 /* Strip the base name, keep any leading namespaces/classes. */
21013 base = strrchr (DW_STRING (attr), ':');
21014 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21015 return "";
21016
21017 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21018 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21019 DW_STRING (attr),
21020 &base[-1] - DW_STRING (attr));
21021 }
21022
21023 /* Return the name of the namespace/class that DIE is defined within,
21024 or "" if we can't tell. The caller should not xfree the result.
21025
21026 For example, if we're within the method foo() in the following
21027 code:
21028
21029 namespace N {
21030 class C {
21031 void foo () {
21032 }
21033 };
21034 }
21035
21036 then determine_prefix on foo's die will return "N::C". */
21037
21038 static const char *
21039 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21040 {
21041 struct dwarf2_per_objfile *dwarf2_per_objfile
21042 = cu->per_cu->dwarf2_per_objfile;
21043 struct die_info *parent, *spec_die;
21044 struct dwarf2_cu *spec_cu;
21045 struct type *parent_type;
21046 const char *retval;
21047
21048 if (cu->language != language_cplus
21049 && cu->language != language_fortran && cu->language != language_d
21050 && cu->language != language_rust)
21051 return "";
21052
21053 retval = anonymous_struct_prefix (die, cu);
21054 if (retval)
21055 return retval;
21056
21057 /* We have to be careful in the presence of DW_AT_specification.
21058 For example, with GCC 3.4, given the code
21059
21060 namespace N {
21061 void foo() {
21062 // Definition of N::foo.
21063 }
21064 }
21065
21066 then we'll have a tree of DIEs like this:
21067
21068 1: DW_TAG_compile_unit
21069 2: DW_TAG_namespace // N
21070 3: DW_TAG_subprogram // declaration of N::foo
21071 4: DW_TAG_subprogram // definition of N::foo
21072 DW_AT_specification // refers to die #3
21073
21074 Thus, when processing die #4, we have to pretend that we're in
21075 the context of its DW_AT_specification, namely the contex of die
21076 #3. */
21077 spec_cu = cu;
21078 spec_die = die_specification (die, &spec_cu);
21079 if (spec_die == NULL)
21080 parent = die->parent;
21081 else
21082 {
21083 parent = spec_die->parent;
21084 cu = spec_cu;
21085 }
21086
21087 if (parent == NULL)
21088 return "";
21089 else if (parent->building_fullname)
21090 {
21091 const char *name;
21092 const char *parent_name;
21093
21094 /* It has been seen on RealView 2.2 built binaries,
21095 DW_TAG_template_type_param types actually _defined_ as
21096 children of the parent class:
21097
21098 enum E {};
21099 template class <class Enum> Class{};
21100 Class<enum E> class_e;
21101
21102 1: DW_TAG_class_type (Class)
21103 2: DW_TAG_enumeration_type (E)
21104 3: DW_TAG_enumerator (enum1:0)
21105 3: DW_TAG_enumerator (enum2:1)
21106 ...
21107 2: DW_TAG_template_type_param
21108 DW_AT_type DW_FORM_ref_udata (E)
21109
21110 Besides being broken debug info, it can put GDB into an
21111 infinite loop. Consider:
21112
21113 When we're building the full name for Class<E>, we'll start
21114 at Class, and go look over its template type parameters,
21115 finding E. We'll then try to build the full name of E, and
21116 reach here. We're now trying to build the full name of E,
21117 and look over the parent DIE for containing scope. In the
21118 broken case, if we followed the parent DIE of E, we'd again
21119 find Class, and once again go look at its template type
21120 arguments, etc., etc. Simply don't consider such parent die
21121 as source-level parent of this die (it can't be, the language
21122 doesn't allow it), and break the loop here. */
21123 name = dwarf2_name (die, cu);
21124 parent_name = dwarf2_name (parent, cu);
21125 complaint (_("template param type '%s' defined within parent '%s'"),
21126 name ? name : "<unknown>",
21127 parent_name ? parent_name : "<unknown>");
21128 return "";
21129 }
21130 else
21131 switch (parent->tag)
21132 {
21133 case DW_TAG_namespace:
21134 parent_type = read_type_die (parent, cu);
21135 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21136 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21137 Work around this problem here. */
21138 if (cu->language == language_cplus
21139 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21140 return "";
21141 /* We give a name to even anonymous namespaces. */
21142 return TYPE_NAME (parent_type);
21143 case DW_TAG_class_type:
21144 case DW_TAG_interface_type:
21145 case DW_TAG_structure_type:
21146 case DW_TAG_union_type:
21147 case DW_TAG_module:
21148 parent_type = read_type_die (parent, cu);
21149 if (TYPE_NAME (parent_type) != NULL)
21150 return TYPE_NAME (parent_type);
21151 else
21152 /* An anonymous structure is only allowed non-static data
21153 members; no typedefs, no member functions, et cetera.
21154 So it does not need a prefix. */
21155 return "";
21156 case DW_TAG_compile_unit:
21157 case DW_TAG_partial_unit:
21158 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21159 if (cu->language == language_cplus
21160 && !dwarf2_per_objfile->types.empty ()
21161 && die->child != NULL
21162 && (die->tag == DW_TAG_class_type
21163 || die->tag == DW_TAG_structure_type
21164 || die->tag == DW_TAG_union_type))
21165 {
21166 const char *name = guess_full_die_structure_name (die, cu);
21167 if (name != NULL)
21168 return name;
21169 }
21170 return "";
21171 case DW_TAG_subprogram:
21172 /* Nested subroutines in Fortran get a prefix with the name
21173 of the parent's subroutine. */
21174 if (cu->language == language_fortran)
21175 {
21176 if ((die->tag == DW_TAG_subprogram)
21177 && (dwarf2_name (parent, cu) != NULL))
21178 return dwarf2_name (parent, cu);
21179 }
21180 return determine_prefix (parent, cu);
21181 case DW_TAG_enumeration_type:
21182 parent_type = read_type_die (parent, cu);
21183 if (TYPE_DECLARED_CLASS (parent_type))
21184 {
21185 if (TYPE_NAME (parent_type) != NULL)
21186 return TYPE_NAME (parent_type);
21187 return "";
21188 }
21189 /* Fall through. */
21190 default:
21191 return determine_prefix (parent, cu);
21192 }
21193 }
21194
21195 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21196 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21197 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21198 an obconcat, otherwise allocate storage for the result. The CU argument is
21199 used to determine the language and hence, the appropriate separator. */
21200
21201 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21202
21203 static char *
21204 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21205 int physname, struct dwarf2_cu *cu)
21206 {
21207 const char *lead = "";
21208 const char *sep;
21209
21210 if (suffix == NULL || suffix[0] == '\0'
21211 || prefix == NULL || prefix[0] == '\0')
21212 sep = "";
21213 else if (cu->language == language_d)
21214 {
21215 /* For D, the 'main' function could be defined in any module, but it
21216 should never be prefixed. */
21217 if (strcmp (suffix, "D main") == 0)
21218 {
21219 prefix = "";
21220 sep = "";
21221 }
21222 else
21223 sep = ".";
21224 }
21225 else if (cu->language == language_fortran && physname)
21226 {
21227 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21228 DW_AT_MIPS_linkage_name is preferred and used instead. */
21229
21230 lead = "__";
21231 sep = "_MOD_";
21232 }
21233 else
21234 sep = "::";
21235
21236 if (prefix == NULL)
21237 prefix = "";
21238 if (suffix == NULL)
21239 suffix = "";
21240
21241 if (obs == NULL)
21242 {
21243 char *retval
21244 = ((char *)
21245 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21246
21247 strcpy (retval, lead);
21248 strcat (retval, prefix);
21249 strcat (retval, sep);
21250 strcat (retval, suffix);
21251 return retval;
21252 }
21253 else
21254 {
21255 /* We have an obstack. */
21256 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21257 }
21258 }
21259
21260 /* Return sibling of die, NULL if no sibling. */
21261
21262 static struct die_info *
21263 sibling_die (struct die_info *die)
21264 {
21265 return die->sibling;
21266 }
21267
21268 /* Get name of a die, return NULL if not found. */
21269
21270 static const char *
21271 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21272 struct objfile *objfile)
21273 {
21274 if (name && cu->language == language_cplus)
21275 {
21276 std::string canon_name = cp_canonicalize_string (name);
21277
21278 if (!canon_name.empty ())
21279 {
21280 if (canon_name != name)
21281 name = objfile->intern (canon_name);
21282 }
21283 }
21284
21285 return name;
21286 }
21287
21288 /* Get name of a die, return NULL if not found.
21289 Anonymous namespaces are converted to their magic string. */
21290
21291 static const char *
21292 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21293 {
21294 struct attribute *attr;
21295 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21296
21297 attr = dwarf2_attr (die, DW_AT_name, cu);
21298 if ((!attr || !DW_STRING (attr))
21299 && die->tag != DW_TAG_namespace
21300 && die->tag != DW_TAG_class_type
21301 && die->tag != DW_TAG_interface_type
21302 && die->tag != DW_TAG_structure_type
21303 && die->tag != DW_TAG_union_type)
21304 return NULL;
21305
21306 switch (die->tag)
21307 {
21308 case DW_TAG_compile_unit:
21309 case DW_TAG_partial_unit:
21310 /* Compilation units have a DW_AT_name that is a filename, not
21311 a source language identifier. */
21312 case DW_TAG_enumeration_type:
21313 case DW_TAG_enumerator:
21314 /* These tags always have simple identifiers already; no need
21315 to canonicalize them. */
21316 return DW_STRING (attr);
21317
21318 case DW_TAG_namespace:
21319 if (attr != NULL && DW_STRING (attr) != NULL)
21320 return DW_STRING (attr);
21321 return CP_ANONYMOUS_NAMESPACE_STR;
21322
21323 case DW_TAG_class_type:
21324 case DW_TAG_interface_type:
21325 case DW_TAG_structure_type:
21326 case DW_TAG_union_type:
21327 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21328 structures or unions. These were of the form "._%d" in GCC 4.1,
21329 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21330 and GCC 4.4. We work around this problem by ignoring these. */
21331 if (attr && DW_STRING (attr)
21332 && (startswith (DW_STRING (attr), "._")
21333 || startswith (DW_STRING (attr), "<anonymous")))
21334 return NULL;
21335
21336 /* GCC might emit a nameless typedef that has a linkage name. See
21337 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21338 if (!attr || DW_STRING (attr) == NULL)
21339 {
21340 attr = dw2_linkage_name_attr (die, cu);
21341 if (attr == NULL || DW_STRING (attr) == NULL)
21342 return NULL;
21343
21344 /* Avoid demangling DW_STRING (attr) the second time on a second
21345 call for the same DIE. */
21346 if (!DW_STRING_IS_CANONICAL (attr))
21347 {
21348 gdb::unique_xmalloc_ptr<char> demangled
21349 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21350 if (demangled == nullptr)
21351 return nullptr;
21352
21353 DW_STRING (attr) = objfile->intern (demangled.get ());
21354 DW_STRING_IS_CANONICAL (attr) = 1;
21355 }
21356
21357 /* Strip any leading namespaces/classes, keep only the base name.
21358 DW_AT_name for named DIEs does not contain the prefixes. */
21359 const char *base = strrchr (DW_STRING (attr), ':');
21360 if (base && base > DW_STRING (attr) && base[-1] == ':')
21361 return &base[1];
21362 else
21363 return DW_STRING (attr);
21364 }
21365 break;
21366
21367 default:
21368 break;
21369 }
21370
21371 if (!DW_STRING_IS_CANONICAL (attr))
21372 {
21373 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21374 objfile);
21375 DW_STRING_IS_CANONICAL (attr) = 1;
21376 }
21377 return DW_STRING (attr);
21378 }
21379
21380 /* Return the die that this die in an extension of, or NULL if there
21381 is none. *EXT_CU is the CU containing DIE on input, and the CU
21382 containing the return value on output. */
21383
21384 static struct die_info *
21385 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21386 {
21387 struct attribute *attr;
21388
21389 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21390 if (attr == NULL)
21391 return NULL;
21392
21393 return follow_die_ref (die, attr, ext_cu);
21394 }
21395
21396 /* A convenience function that returns an "unknown" DWARF name,
21397 including the value of V. STR is the name of the entity being
21398 printed, e.g., "TAG". */
21399
21400 static const char *
21401 dwarf_unknown (const char *str, unsigned v)
21402 {
21403 char *cell = get_print_cell ();
21404 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21405 return cell;
21406 }
21407
21408 /* Convert a DIE tag into its string name. */
21409
21410 static const char *
21411 dwarf_tag_name (unsigned tag)
21412 {
21413 const char *name = get_DW_TAG_name (tag);
21414
21415 if (name == NULL)
21416 return dwarf_unknown ("TAG", tag);
21417
21418 return name;
21419 }
21420
21421 /* Convert a DWARF attribute code into its string name. */
21422
21423 static const char *
21424 dwarf_attr_name (unsigned attr)
21425 {
21426 const char *name;
21427
21428 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21429 if (attr == DW_AT_MIPS_fde)
21430 return "DW_AT_MIPS_fde";
21431 #else
21432 if (attr == DW_AT_HP_block_index)
21433 return "DW_AT_HP_block_index";
21434 #endif
21435
21436 name = get_DW_AT_name (attr);
21437
21438 if (name == NULL)
21439 return dwarf_unknown ("AT", attr);
21440
21441 return name;
21442 }
21443
21444 /* Convert a DWARF value form code into its string name. */
21445
21446 static const char *
21447 dwarf_form_name (unsigned form)
21448 {
21449 const char *name = get_DW_FORM_name (form);
21450
21451 if (name == NULL)
21452 return dwarf_unknown ("FORM", form);
21453
21454 return name;
21455 }
21456
21457 static const char *
21458 dwarf_bool_name (unsigned mybool)
21459 {
21460 if (mybool)
21461 return "TRUE";
21462 else
21463 return "FALSE";
21464 }
21465
21466 /* Convert a DWARF type code into its string name. */
21467
21468 static const char *
21469 dwarf_type_encoding_name (unsigned enc)
21470 {
21471 const char *name = get_DW_ATE_name (enc);
21472
21473 if (name == NULL)
21474 return dwarf_unknown ("ATE", enc);
21475
21476 return name;
21477 }
21478
21479 static void
21480 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21481 {
21482 unsigned int i;
21483
21484 print_spaces (indent, f);
21485 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21486 dwarf_tag_name (die->tag), die->abbrev,
21487 sect_offset_str (die->sect_off));
21488
21489 if (die->parent != NULL)
21490 {
21491 print_spaces (indent, f);
21492 fprintf_unfiltered (f, " parent at offset: %s\n",
21493 sect_offset_str (die->parent->sect_off));
21494 }
21495
21496 print_spaces (indent, f);
21497 fprintf_unfiltered (f, " has children: %s\n",
21498 dwarf_bool_name (die->child != NULL));
21499
21500 print_spaces (indent, f);
21501 fprintf_unfiltered (f, " attributes:\n");
21502
21503 for (i = 0; i < die->num_attrs; ++i)
21504 {
21505 print_spaces (indent, f);
21506 fprintf_unfiltered (f, " %s (%s) ",
21507 dwarf_attr_name (die->attrs[i].name),
21508 dwarf_form_name (die->attrs[i].form));
21509
21510 switch (die->attrs[i].form)
21511 {
21512 case DW_FORM_addr:
21513 case DW_FORM_addrx:
21514 case DW_FORM_GNU_addr_index:
21515 fprintf_unfiltered (f, "address: ");
21516 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21517 break;
21518 case DW_FORM_block2:
21519 case DW_FORM_block4:
21520 case DW_FORM_block:
21521 case DW_FORM_block1:
21522 fprintf_unfiltered (f, "block: size %s",
21523 pulongest (DW_BLOCK (&die->attrs[i])->size));
21524 break;
21525 case DW_FORM_exprloc:
21526 fprintf_unfiltered (f, "expression: size %s",
21527 pulongest (DW_BLOCK (&die->attrs[i])->size));
21528 break;
21529 case DW_FORM_data16:
21530 fprintf_unfiltered (f, "constant of 16 bytes");
21531 break;
21532 case DW_FORM_ref_addr:
21533 fprintf_unfiltered (f, "ref address: ");
21534 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21535 break;
21536 case DW_FORM_GNU_ref_alt:
21537 fprintf_unfiltered (f, "alt ref address: ");
21538 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21539 break;
21540 case DW_FORM_ref1:
21541 case DW_FORM_ref2:
21542 case DW_FORM_ref4:
21543 case DW_FORM_ref8:
21544 case DW_FORM_ref_udata:
21545 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21546 (long) (DW_UNSND (&die->attrs[i])));
21547 break;
21548 case DW_FORM_data1:
21549 case DW_FORM_data2:
21550 case DW_FORM_data4:
21551 case DW_FORM_data8:
21552 case DW_FORM_udata:
21553 case DW_FORM_sdata:
21554 fprintf_unfiltered (f, "constant: %s",
21555 pulongest (DW_UNSND (&die->attrs[i])));
21556 break;
21557 case DW_FORM_sec_offset:
21558 fprintf_unfiltered (f, "section offset: %s",
21559 pulongest (DW_UNSND (&die->attrs[i])));
21560 break;
21561 case DW_FORM_ref_sig8:
21562 fprintf_unfiltered (f, "signature: %s",
21563 hex_string (DW_SIGNATURE (&die->attrs[i])));
21564 break;
21565 case DW_FORM_string:
21566 case DW_FORM_strp:
21567 case DW_FORM_line_strp:
21568 case DW_FORM_strx:
21569 case DW_FORM_GNU_str_index:
21570 case DW_FORM_GNU_strp_alt:
21571 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21572 DW_STRING (&die->attrs[i])
21573 ? DW_STRING (&die->attrs[i]) : "",
21574 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21575 break;
21576 case DW_FORM_flag:
21577 if (DW_UNSND (&die->attrs[i]))
21578 fprintf_unfiltered (f, "flag: TRUE");
21579 else
21580 fprintf_unfiltered (f, "flag: FALSE");
21581 break;
21582 case DW_FORM_flag_present:
21583 fprintf_unfiltered (f, "flag: TRUE");
21584 break;
21585 case DW_FORM_indirect:
21586 /* The reader will have reduced the indirect form to
21587 the "base form" so this form should not occur. */
21588 fprintf_unfiltered (f,
21589 "unexpected attribute form: DW_FORM_indirect");
21590 break;
21591 case DW_FORM_implicit_const:
21592 fprintf_unfiltered (f, "constant: %s",
21593 plongest (DW_SND (&die->attrs[i])));
21594 break;
21595 default:
21596 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21597 die->attrs[i].form);
21598 break;
21599 }
21600 fprintf_unfiltered (f, "\n");
21601 }
21602 }
21603
21604 static void
21605 dump_die_for_error (struct die_info *die)
21606 {
21607 dump_die_shallow (gdb_stderr, 0, die);
21608 }
21609
21610 static void
21611 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21612 {
21613 int indent = level * 4;
21614
21615 gdb_assert (die != NULL);
21616
21617 if (level >= max_level)
21618 return;
21619
21620 dump_die_shallow (f, indent, die);
21621
21622 if (die->child != NULL)
21623 {
21624 print_spaces (indent, f);
21625 fprintf_unfiltered (f, " Children:");
21626 if (level + 1 < max_level)
21627 {
21628 fprintf_unfiltered (f, "\n");
21629 dump_die_1 (f, level + 1, max_level, die->child);
21630 }
21631 else
21632 {
21633 fprintf_unfiltered (f,
21634 " [not printed, max nesting level reached]\n");
21635 }
21636 }
21637
21638 if (die->sibling != NULL && level > 0)
21639 {
21640 dump_die_1 (f, level, max_level, die->sibling);
21641 }
21642 }
21643
21644 /* This is called from the pdie macro in gdbinit.in.
21645 It's not static so gcc will keep a copy callable from gdb. */
21646
21647 void
21648 dump_die (struct die_info *die, int max_level)
21649 {
21650 dump_die_1 (gdb_stdlog, 0, max_level, die);
21651 }
21652
21653 static void
21654 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21655 {
21656 void **slot;
21657
21658 slot = htab_find_slot_with_hash (cu->die_hash, die,
21659 to_underlying (die->sect_off),
21660 INSERT);
21661
21662 *slot = die;
21663 }
21664
21665 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21666 required kind. */
21667
21668 static sect_offset
21669 dwarf2_get_ref_die_offset (const struct attribute *attr)
21670 {
21671 if (attr->form_is_ref ())
21672 return (sect_offset) DW_UNSND (attr);
21673
21674 complaint (_("unsupported die ref attribute form: '%s'"),
21675 dwarf_form_name (attr->form));
21676 return {};
21677 }
21678
21679 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21680 * the value held by the attribute is not constant. */
21681
21682 static LONGEST
21683 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
21684 {
21685 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
21686 return DW_SND (attr);
21687 else if (attr->form == DW_FORM_udata
21688 || attr->form == DW_FORM_data1
21689 || attr->form == DW_FORM_data2
21690 || attr->form == DW_FORM_data4
21691 || attr->form == DW_FORM_data8)
21692 return DW_UNSND (attr);
21693 else
21694 {
21695 /* For DW_FORM_data16 see attribute::form_is_constant. */
21696 complaint (_("Attribute value is not a constant (%s)"),
21697 dwarf_form_name (attr->form));
21698 return default_value;
21699 }
21700 }
21701
21702 /* Follow reference or signature attribute ATTR of SRC_DIE.
21703 On entry *REF_CU is the CU of SRC_DIE.
21704 On exit *REF_CU is the CU of the result. */
21705
21706 static struct die_info *
21707 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21708 struct dwarf2_cu **ref_cu)
21709 {
21710 struct die_info *die;
21711
21712 if (attr->form_is_ref ())
21713 die = follow_die_ref (src_die, attr, ref_cu);
21714 else if (attr->form == DW_FORM_ref_sig8)
21715 die = follow_die_sig (src_die, attr, ref_cu);
21716 else
21717 {
21718 dump_die_for_error (src_die);
21719 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21720 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
21721 }
21722
21723 return die;
21724 }
21725
21726 /* Follow reference OFFSET.
21727 On entry *REF_CU is the CU of the source die referencing OFFSET.
21728 On exit *REF_CU is the CU of the result.
21729 Returns NULL if OFFSET is invalid. */
21730
21731 static struct die_info *
21732 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21733 struct dwarf2_cu **ref_cu)
21734 {
21735 struct die_info temp_die;
21736 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21737 struct dwarf2_per_objfile *dwarf2_per_objfile
21738 = cu->per_cu->dwarf2_per_objfile;
21739
21740 gdb_assert (cu->per_cu != NULL);
21741
21742 target_cu = cu;
21743
21744 if (cu->per_cu->is_debug_types)
21745 {
21746 /* .debug_types CUs cannot reference anything outside their CU.
21747 If they need to, they have to reference a signatured type via
21748 DW_FORM_ref_sig8. */
21749 if (!cu->header.offset_in_cu_p (sect_off))
21750 return NULL;
21751 }
21752 else if (offset_in_dwz != cu->per_cu->is_dwz
21753 || !cu->header.offset_in_cu_p (sect_off))
21754 {
21755 struct dwarf2_per_cu_data *per_cu;
21756
21757 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21758 dwarf2_per_objfile);
21759
21760 /* If necessary, add it to the queue and load its DIEs. */
21761 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21762 load_full_comp_unit (per_cu, false, cu->language);
21763
21764 target_cu = per_cu->cu;
21765 }
21766 else if (cu->dies == NULL)
21767 {
21768 /* We're loading full DIEs during partial symbol reading. */
21769 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21770 load_full_comp_unit (cu->per_cu, false, language_minimal);
21771 }
21772
21773 *ref_cu = target_cu;
21774 temp_die.sect_off = sect_off;
21775
21776 if (target_cu != cu)
21777 target_cu->ancestor = cu;
21778
21779 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21780 &temp_die,
21781 to_underlying (sect_off));
21782 }
21783
21784 /* Follow reference attribute ATTR of SRC_DIE.
21785 On entry *REF_CU is the CU of SRC_DIE.
21786 On exit *REF_CU is the CU of the result. */
21787
21788 static struct die_info *
21789 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21790 struct dwarf2_cu **ref_cu)
21791 {
21792 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21793 struct dwarf2_cu *cu = *ref_cu;
21794 struct die_info *die;
21795
21796 die = follow_die_offset (sect_off,
21797 (attr->form == DW_FORM_GNU_ref_alt
21798 || cu->per_cu->is_dwz),
21799 ref_cu);
21800 if (!die)
21801 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21802 "at %s [in module %s]"),
21803 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
21804 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
21805
21806 return die;
21807 }
21808
21809 /* See read.h. */
21810
21811 struct dwarf2_locexpr_baton
21812 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21813 dwarf2_per_cu_data *per_cu,
21814 CORE_ADDR (*get_frame_pc) (void *baton),
21815 void *baton, bool resolve_abstract_p)
21816 {
21817 struct dwarf2_cu *cu;
21818 struct die_info *die;
21819 struct attribute *attr;
21820 struct dwarf2_locexpr_baton retval;
21821 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21822 struct objfile *objfile = dwarf2_per_objfile->objfile;
21823
21824 if (per_cu->cu == NULL)
21825 load_cu (per_cu, false);
21826 cu = per_cu->cu;
21827 if (cu == NULL)
21828 {
21829 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21830 Instead just throw an error, not much else we can do. */
21831 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21832 sect_offset_str (sect_off), objfile_name (objfile));
21833 }
21834
21835 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21836 if (!die)
21837 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21838 sect_offset_str (sect_off), objfile_name (objfile));
21839
21840 attr = dwarf2_attr (die, DW_AT_location, cu);
21841 if (!attr && resolve_abstract_p
21842 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
21843 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21844 {
21845 CORE_ADDR pc = (*get_frame_pc) (baton);
21846 CORE_ADDR baseaddr = objfile->text_section_offset ();
21847 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21848
21849 for (const auto &cand_off
21850 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
21851 {
21852 struct dwarf2_cu *cand_cu = cu;
21853 struct die_info *cand
21854 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21855 if (!cand
21856 || !cand->parent
21857 || cand->parent->tag != DW_TAG_subprogram)
21858 continue;
21859
21860 CORE_ADDR pc_low, pc_high;
21861 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
21862 if (pc_low == ((CORE_ADDR) -1))
21863 continue;
21864 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21865 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21866 if (!(pc_low <= pc && pc < pc_high))
21867 continue;
21868
21869 die = cand;
21870 attr = dwarf2_attr (die, DW_AT_location, cu);
21871 break;
21872 }
21873 }
21874
21875 if (!attr)
21876 {
21877 /* DWARF: "If there is no such attribute, then there is no effect.".
21878 DATA is ignored if SIZE is 0. */
21879
21880 retval.data = NULL;
21881 retval.size = 0;
21882 }
21883 else if (attr->form_is_section_offset ())
21884 {
21885 struct dwarf2_loclist_baton loclist_baton;
21886 CORE_ADDR pc = (*get_frame_pc) (baton);
21887 size_t size;
21888
21889 fill_in_loclist_baton (cu, &loclist_baton, attr);
21890
21891 retval.data = dwarf2_find_location_expression (&loclist_baton,
21892 &size, pc);
21893 retval.size = size;
21894 }
21895 else
21896 {
21897 if (!attr->form_is_block ())
21898 error (_("Dwarf Error: DIE at %s referenced in module %s "
21899 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21900 sect_offset_str (sect_off), objfile_name (objfile));
21901
21902 retval.data = DW_BLOCK (attr)->data;
21903 retval.size = DW_BLOCK (attr)->size;
21904 }
21905 retval.per_cu = cu->per_cu;
21906
21907 age_cached_comp_units (dwarf2_per_objfile);
21908
21909 return retval;
21910 }
21911
21912 /* See read.h. */
21913
21914 struct dwarf2_locexpr_baton
21915 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21916 dwarf2_per_cu_data *per_cu,
21917 CORE_ADDR (*get_frame_pc) (void *baton),
21918 void *baton)
21919 {
21920 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21921
21922 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21923 }
21924
21925 /* Write a constant of a given type as target-ordered bytes into
21926 OBSTACK. */
21927
21928 static const gdb_byte *
21929 write_constant_as_bytes (struct obstack *obstack,
21930 enum bfd_endian byte_order,
21931 struct type *type,
21932 ULONGEST value,
21933 LONGEST *len)
21934 {
21935 gdb_byte *result;
21936
21937 *len = TYPE_LENGTH (type);
21938 result = (gdb_byte *) obstack_alloc (obstack, *len);
21939 store_unsigned_integer (result, *len, byte_order, value);
21940
21941 return result;
21942 }
21943
21944 /* See read.h. */
21945
21946 const gdb_byte *
21947 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21948 dwarf2_per_cu_data *per_cu,
21949 obstack *obstack,
21950 LONGEST *len)
21951 {
21952 struct dwarf2_cu *cu;
21953 struct die_info *die;
21954 struct attribute *attr;
21955 const gdb_byte *result = NULL;
21956 struct type *type;
21957 LONGEST value;
21958 enum bfd_endian byte_order;
21959 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
21960
21961 if (per_cu->cu == NULL)
21962 load_cu (per_cu, false);
21963 cu = per_cu->cu;
21964 if (cu == NULL)
21965 {
21966 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21967 Instead just throw an error, not much else we can do. */
21968 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21969 sect_offset_str (sect_off), objfile_name (objfile));
21970 }
21971
21972 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21973 if (!die)
21974 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21975 sect_offset_str (sect_off), objfile_name (objfile));
21976
21977 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21978 if (attr == NULL)
21979 return NULL;
21980
21981 byte_order = (bfd_big_endian (objfile->obfd)
21982 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21983
21984 switch (attr->form)
21985 {
21986 case DW_FORM_addr:
21987 case DW_FORM_addrx:
21988 case DW_FORM_GNU_addr_index:
21989 {
21990 gdb_byte *tem;
21991
21992 *len = cu->header.addr_size;
21993 tem = (gdb_byte *) obstack_alloc (obstack, *len);
21994 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21995 result = tem;
21996 }
21997 break;
21998 case DW_FORM_string:
21999 case DW_FORM_strp:
22000 case DW_FORM_strx:
22001 case DW_FORM_GNU_str_index:
22002 case DW_FORM_GNU_strp_alt:
22003 /* DW_STRING is already allocated on the objfile obstack, point
22004 directly to it. */
22005 result = (const gdb_byte *) DW_STRING (attr);
22006 *len = strlen (DW_STRING (attr));
22007 break;
22008 case DW_FORM_block1:
22009 case DW_FORM_block2:
22010 case DW_FORM_block4:
22011 case DW_FORM_block:
22012 case DW_FORM_exprloc:
22013 case DW_FORM_data16:
22014 result = DW_BLOCK (attr)->data;
22015 *len = DW_BLOCK (attr)->size;
22016 break;
22017
22018 /* The DW_AT_const_value attributes are supposed to carry the
22019 symbol's value "represented as it would be on the target
22020 architecture." By the time we get here, it's already been
22021 converted to host endianness, so we just need to sign- or
22022 zero-extend it as appropriate. */
22023 case DW_FORM_data1:
22024 type = die_type (die, cu);
22025 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22026 if (result == NULL)
22027 result = write_constant_as_bytes (obstack, byte_order,
22028 type, value, len);
22029 break;
22030 case DW_FORM_data2:
22031 type = die_type (die, cu);
22032 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22033 if (result == NULL)
22034 result = write_constant_as_bytes (obstack, byte_order,
22035 type, value, len);
22036 break;
22037 case DW_FORM_data4:
22038 type = die_type (die, cu);
22039 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22040 if (result == NULL)
22041 result = write_constant_as_bytes (obstack, byte_order,
22042 type, value, len);
22043 break;
22044 case DW_FORM_data8:
22045 type = die_type (die, cu);
22046 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22047 if (result == NULL)
22048 result = write_constant_as_bytes (obstack, byte_order,
22049 type, value, len);
22050 break;
22051
22052 case DW_FORM_sdata:
22053 case DW_FORM_implicit_const:
22054 type = die_type (die, cu);
22055 result = write_constant_as_bytes (obstack, byte_order,
22056 type, DW_SND (attr), len);
22057 break;
22058
22059 case DW_FORM_udata:
22060 type = die_type (die, cu);
22061 result = write_constant_as_bytes (obstack, byte_order,
22062 type, DW_UNSND (attr), len);
22063 break;
22064
22065 default:
22066 complaint (_("unsupported const value attribute form: '%s'"),
22067 dwarf_form_name (attr->form));
22068 break;
22069 }
22070
22071 return result;
22072 }
22073
22074 /* See read.h. */
22075
22076 struct type *
22077 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22078 dwarf2_per_cu_data *per_cu)
22079 {
22080 struct dwarf2_cu *cu;
22081 struct die_info *die;
22082
22083 if (per_cu->cu == NULL)
22084 load_cu (per_cu, false);
22085 cu = per_cu->cu;
22086 if (!cu)
22087 return NULL;
22088
22089 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22090 if (!die)
22091 return NULL;
22092
22093 return die_type (die, cu);
22094 }
22095
22096 /* See read.h. */
22097
22098 struct type *
22099 dwarf2_get_die_type (cu_offset die_offset,
22100 struct dwarf2_per_cu_data *per_cu)
22101 {
22102 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22103 return get_die_type_at_offset (die_offset_sect, per_cu);
22104 }
22105
22106 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22107 On entry *REF_CU is the CU of SRC_DIE.
22108 On exit *REF_CU is the CU of the result.
22109 Returns NULL if the referenced DIE isn't found. */
22110
22111 static struct die_info *
22112 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22113 struct dwarf2_cu **ref_cu)
22114 {
22115 struct die_info temp_die;
22116 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22117 struct die_info *die;
22118
22119 /* While it might be nice to assert sig_type->type == NULL here,
22120 we can get here for DW_AT_imported_declaration where we need
22121 the DIE not the type. */
22122
22123 /* If necessary, add it to the queue and load its DIEs. */
22124
22125 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22126 read_signatured_type (sig_type);
22127
22128 sig_cu = sig_type->per_cu.cu;
22129 gdb_assert (sig_cu != NULL);
22130 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22131 temp_die.sect_off = sig_type->type_offset_in_section;
22132 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22133 to_underlying (temp_die.sect_off));
22134 if (die)
22135 {
22136 struct dwarf2_per_objfile *dwarf2_per_objfile
22137 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22138
22139 /* For .gdb_index version 7 keep track of included TUs.
22140 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22141 if (dwarf2_per_objfile->index_table != NULL
22142 && dwarf2_per_objfile->index_table->version <= 7)
22143 {
22144 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22145 }
22146
22147 *ref_cu = sig_cu;
22148 if (sig_cu != cu)
22149 sig_cu->ancestor = cu;
22150
22151 return die;
22152 }
22153
22154 return NULL;
22155 }
22156
22157 /* Follow signatured type referenced by ATTR in SRC_DIE.
22158 On entry *REF_CU is the CU of SRC_DIE.
22159 On exit *REF_CU is the CU of the result.
22160 The result is the DIE of the type.
22161 If the referenced type cannot be found an error is thrown. */
22162
22163 static struct die_info *
22164 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22165 struct dwarf2_cu **ref_cu)
22166 {
22167 ULONGEST signature = DW_SIGNATURE (attr);
22168 struct signatured_type *sig_type;
22169 struct die_info *die;
22170
22171 gdb_assert (attr->form == DW_FORM_ref_sig8);
22172
22173 sig_type = lookup_signatured_type (*ref_cu, signature);
22174 /* sig_type will be NULL if the signatured type is missing from
22175 the debug info. */
22176 if (sig_type == NULL)
22177 {
22178 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22179 " from DIE at %s [in module %s]"),
22180 hex_string (signature), sect_offset_str (src_die->sect_off),
22181 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22182 }
22183
22184 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22185 if (die == NULL)
22186 {
22187 dump_die_for_error (src_die);
22188 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22189 " from DIE at %s [in module %s]"),
22190 hex_string (signature), sect_offset_str (src_die->sect_off),
22191 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22192 }
22193
22194 return die;
22195 }
22196
22197 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22198 reading in and processing the type unit if necessary. */
22199
22200 static struct type *
22201 get_signatured_type (struct die_info *die, ULONGEST signature,
22202 struct dwarf2_cu *cu)
22203 {
22204 struct dwarf2_per_objfile *dwarf2_per_objfile
22205 = cu->per_cu->dwarf2_per_objfile;
22206 struct signatured_type *sig_type;
22207 struct dwarf2_cu *type_cu;
22208 struct die_info *type_die;
22209 struct type *type;
22210
22211 sig_type = lookup_signatured_type (cu, signature);
22212 /* sig_type will be NULL if the signatured type is missing from
22213 the debug info. */
22214 if (sig_type == NULL)
22215 {
22216 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22217 " from DIE at %s [in module %s]"),
22218 hex_string (signature), sect_offset_str (die->sect_off),
22219 objfile_name (dwarf2_per_objfile->objfile));
22220 return build_error_marker_type (cu, die);
22221 }
22222
22223 /* If we already know the type we're done. */
22224 if (sig_type->type != NULL)
22225 return sig_type->type;
22226
22227 type_cu = cu;
22228 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22229 if (type_die != NULL)
22230 {
22231 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22232 is created. This is important, for example, because for c++ classes
22233 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22234 type = read_type_die (type_die, type_cu);
22235 if (type == NULL)
22236 {
22237 complaint (_("Dwarf Error: Cannot build signatured type %s"
22238 " referenced from DIE at %s [in module %s]"),
22239 hex_string (signature), sect_offset_str (die->sect_off),
22240 objfile_name (dwarf2_per_objfile->objfile));
22241 type = build_error_marker_type (cu, die);
22242 }
22243 }
22244 else
22245 {
22246 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22247 " from DIE at %s [in module %s]"),
22248 hex_string (signature), sect_offset_str (die->sect_off),
22249 objfile_name (dwarf2_per_objfile->objfile));
22250 type = build_error_marker_type (cu, die);
22251 }
22252 sig_type->type = type;
22253
22254 return type;
22255 }
22256
22257 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22258 reading in and processing the type unit if necessary. */
22259
22260 static struct type *
22261 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22262 struct dwarf2_cu *cu) /* ARI: editCase function */
22263 {
22264 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22265 if (attr->form_is_ref ())
22266 {
22267 struct dwarf2_cu *type_cu = cu;
22268 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22269
22270 return read_type_die (type_die, type_cu);
22271 }
22272 else if (attr->form == DW_FORM_ref_sig8)
22273 {
22274 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22275 }
22276 else
22277 {
22278 struct dwarf2_per_objfile *dwarf2_per_objfile
22279 = cu->per_cu->dwarf2_per_objfile;
22280
22281 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22282 " at %s [in module %s]"),
22283 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22284 objfile_name (dwarf2_per_objfile->objfile));
22285 return build_error_marker_type (cu, die);
22286 }
22287 }
22288
22289 /* Load the DIEs associated with type unit PER_CU into memory. */
22290
22291 static void
22292 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22293 {
22294 struct signatured_type *sig_type;
22295
22296 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22297 gdb_assert (! per_cu->type_unit_group_p ());
22298
22299 /* We have the per_cu, but we need the signatured_type.
22300 Fortunately this is an easy translation. */
22301 gdb_assert (per_cu->is_debug_types);
22302 sig_type = (struct signatured_type *) per_cu;
22303
22304 gdb_assert (per_cu->cu == NULL);
22305
22306 read_signatured_type (sig_type);
22307
22308 gdb_assert (per_cu->cu != NULL);
22309 }
22310
22311 /* Read in a signatured type and build its CU and DIEs.
22312 If the type is a stub for the real type in a DWO file,
22313 read in the real type from the DWO file as well. */
22314
22315 static void
22316 read_signatured_type (struct signatured_type *sig_type)
22317 {
22318 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22319
22320 gdb_assert (per_cu->is_debug_types);
22321 gdb_assert (per_cu->cu == NULL);
22322
22323 cutu_reader reader (per_cu, NULL, 0, false);
22324
22325 if (!reader.dummy_p)
22326 {
22327 struct dwarf2_cu *cu = reader.cu;
22328 const gdb_byte *info_ptr = reader.info_ptr;
22329
22330 gdb_assert (cu->die_hash == NULL);
22331 cu->die_hash =
22332 htab_create_alloc_ex (cu->header.length / 12,
22333 die_hash,
22334 die_eq,
22335 NULL,
22336 &cu->comp_unit_obstack,
22337 hashtab_obstack_allocate,
22338 dummy_obstack_deallocate);
22339
22340 if (reader.comp_unit_die->has_children)
22341 reader.comp_unit_die->child
22342 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22343 reader.comp_unit_die);
22344 cu->dies = reader.comp_unit_die;
22345 /* comp_unit_die is not stored in die_hash, no need. */
22346
22347 /* We try not to read any attributes in this function, because
22348 not all CUs needed for references have been loaded yet, and
22349 symbol table processing isn't initialized. But we have to
22350 set the CU language, or we won't be able to build types
22351 correctly. Similarly, if we do not read the producer, we can
22352 not apply producer-specific interpretation. */
22353 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22354
22355 reader.keep ();
22356 }
22357
22358 sig_type->per_cu.tu_read = 1;
22359 }
22360
22361 /* Decode simple location descriptions.
22362 Given a pointer to a dwarf block that defines a location, compute
22363 the location and return the value.
22364
22365 NOTE drow/2003-11-18: This function is called in two situations
22366 now: for the address of static or global variables (partial symbols
22367 only) and for offsets into structures which are expected to be
22368 (more or less) constant. The partial symbol case should go away,
22369 and only the constant case should remain. That will let this
22370 function complain more accurately. A few special modes are allowed
22371 without complaint for global variables (for instance, global
22372 register values and thread-local values).
22373
22374 A location description containing no operations indicates that the
22375 object is optimized out. The return value is 0 for that case.
22376 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22377 callers will only want a very basic result and this can become a
22378 complaint.
22379
22380 Note that stack[0] is unused except as a default error return. */
22381
22382 static CORE_ADDR
22383 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22384 {
22385 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22386 size_t i;
22387 size_t size = blk->size;
22388 const gdb_byte *data = blk->data;
22389 CORE_ADDR stack[64];
22390 int stacki;
22391 unsigned int bytes_read, unsnd;
22392 gdb_byte op;
22393
22394 i = 0;
22395 stacki = 0;
22396 stack[stacki] = 0;
22397 stack[++stacki] = 0;
22398
22399 while (i < size)
22400 {
22401 op = data[i++];
22402 switch (op)
22403 {
22404 case DW_OP_lit0:
22405 case DW_OP_lit1:
22406 case DW_OP_lit2:
22407 case DW_OP_lit3:
22408 case DW_OP_lit4:
22409 case DW_OP_lit5:
22410 case DW_OP_lit6:
22411 case DW_OP_lit7:
22412 case DW_OP_lit8:
22413 case DW_OP_lit9:
22414 case DW_OP_lit10:
22415 case DW_OP_lit11:
22416 case DW_OP_lit12:
22417 case DW_OP_lit13:
22418 case DW_OP_lit14:
22419 case DW_OP_lit15:
22420 case DW_OP_lit16:
22421 case DW_OP_lit17:
22422 case DW_OP_lit18:
22423 case DW_OP_lit19:
22424 case DW_OP_lit20:
22425 case DW_OP_lit21:
22426 case DW_OP_lit22:
22427 case DW_OP_lit23:
22428 case DW_OP_lit24:
22429 case DW_OP_lit25:
22430 case DW_OP_lit26:
22431 case DW_OP_lit27:
22432 case DW_OP_lit28:
22433 case DW_OP_lit29:
22434 case DW_OP_lit30:
22435 case DW_OP_lit31:
22436 stack[++stacki] = op - DW_OP_lit0;
22437 break;
22438
22439 case DW_OP_reg0:
22440 case DW_OP_reg1:
22441 case DW_OP_reg2:
22442 case DW_OP_reg3:
22443 case DW_OP_reg4:
22444 case DW_OP_reg5:
22445 case DW_OP_reg6:
22446 case DW_OP_reg7:
22447 case DW_OP_reg8:
22448 case DW_OP_reg9:
22449 case DW_OP_reg10:
22450 case DW_OP_reg11:
22451 case DW_OP_reg12:
22452 case DW_OP_reg13:
22453 case DW_OP_reg14:
22454 case DW_OP_reg15:
22455 case DW_OP_reg16:
22456 case DW_OP_reg17:
22457 case DW_OP_reg18:
22458 case DW_OP_reg19:
22459 case DW_OP_reg20:
22460 case DW_OP_reg21:
22461 case DW_OP_reg22:
22462 case DW_OP_reg23:
22463 case DW_OP_reg24:
22464 case DW_OP_reg25:
22465 case DW_OP_reg26:
22466 case DW_OP_reg27:
22467 case DW_OP_reg28:
22468 case DW_OP_reg29:
22469 case DW_OP_reg30:
22470 case DW_OP_reg31:
22471 stack[++stacki] = op - DW_OP_reg0;
22472 if (i < size)
22473 dwarf2_complex_location_expr_complaint ();
22474 break;
22475
22476 case DW_OP_regx:
22477 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22478 i += bytes_read;
22479 stack[++stacki] = unsnd;
22480 if (i < size)
22481 dwarf2_complex_location_expr_complaint ();
22482 break;
22483
22484 case DW_OP_addr:
22485 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22486 &bytes_read);
22487 i += bytes_read;
22488 break;
22489
22490 case DW_OP_const1u:
22491 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22492 i += 1;
22493 break;
22494
22495 case DW_OP_const1s:
22496 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22497 i += 1;
22498 break;
22499
22500 case DW_OP_const2u:
22501 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22502 i += 2;
22503 break;
22504
22505 case DW_OP_const2s:
22506 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22507 i += 2;
22508 break;
22509
22510 case DW_OP_const4u:
22511 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22512 i += 4;
22513 break;
22514
22515 case DW_OP_const4s:
22516 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22517 i += 4;
22518 break;
22519
22520 case DW_OP_const8u:
22521 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22522 i += 8;
22523 break;
22524
22525 case DW_OP_constu:
22526 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22527 &bytes_read);
22528 i += bytes_read;
22529 break;
22530
22531 case DW_OP_consts:
22532 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22533 i += bytes_read;
22534 break;
22535
22536 case DW_OP_dup:
22537 stack[stacki + 1] = stack[stacki];
22538 stacki++;
22539 break;
22540
22541 case DW_OP_plus:
22542 stack[stacki - 1] += stack[stacki];
22543 stacki--;
22544 break;
22545
22546 case DW_OP_plus_uconst:
22547 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22548 &bytes_read);
22549 i += bytes_read;
22550 break;
22551
22552 case DW_OP_minus:
22553 stack[stacki - 1] -= stack[stacki];
22554 stacki--;
22555 break;
22556
22557 case DW_OP_deref:
22558 /* If we're not the last op, then we definitely can't encode
22559 this using GDB's address_class enum. This is valid for partial
22560 global symbols, although the variable's address will be bogus
22561 in the psymtab. */
22562 if (i < size)
22563 dwarf2_complex_location_expr_complaint ();
22564 break;
22565
22566 case DW_OP_GNU_push_tls_address:
22567 case DW_OP_form_tls_address:
22568 /* The top of the stack has the offset from the beginning
22569 of the thread control block at which the variable is located. */
22570 /* Nothing should follow this operator, so the top of stack would
22571 be returned. */
22572 /* This is valid for partial global symbols, but the variable's
22573 address will be bogus in the psymtab. Make it always at least
22574 non-zero to not look as a variable garbage collected by linker
22575 which have DW_OP_addr 0. */
22576 if (i < size)
22577 dwarf2_complex_location_expr_complaint ();
22578 stack[stacki]++;
22579 break;
22580
22581 case DW_OP_GNU_uninit:
22582 break;
22583
22584 case DW_OP_addrx:
22585 case DW_OP_GNU_addr_index:
22586 case DW_OP_GNU_const_index:
22587 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22588 &bytes_read);
22589 i += bytes_read;
22590 break;
22591
22592 default:
22593 {
22594 const char *name = get_DW_OP_name (op);
22595
22596 if (name)
22597 complaint (_("unsupported stack op: '%s'"),
22598 name);
22599 else
22600 complaint (_("unsupported stack op: '%02x'"),
22601 op);
22602 }
22603
22604 return (stack[stacki]);
22605 }
22606
22607 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22608 outside of the allocated space. Also enforce minimum>0. */
22609 if (stacki >= ARRAY_SIZE (stack) - 1)
22610 {
22611 complaint (_("location description stack overflow"));
22612 return 0;
22613 }
22614
22615 if (stacki <= 0)
22616 {
22617 complaint (_("location description stack underflow"));
22618 return 0;
22619 }
22620 }
22621 return (stack[stacki]);
22622 }
22623
22624 /* memory allocation interface */
22625
22626 static struct dwarf_block *
22627 dwarf_alloc_block (struct dwarf2_cu *cu)
22628 {
22629 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22630 }
22631
22632 static struct die_info *
22633 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22634 {
22635 struct die_info *die;
22636 size_t size = sizeof (struct die_info);
22637
22638 if (num_attrs > 1)
22639 size += (num_attrs - 1) * sizeof (struct attribute);
22640
22641 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22642 memset (die, 0, sizeof (struct die_info));
22643 return (die);
22644 }
22645
22646 \f
22647
22648 /* Macro support. */
22649
22650 /* An overload of dwarf_decode_macros that finds the correct section
22651 and ensures it is read in before calling the other overload. */
22652
22653 static void
22654 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22655 int section_is_gnu)
22656 {
22657 struct dwarf2_per_objfile *dwarf2_per_objfile
22658 = cu->per_cu->dwarf2_per_objfile;
22659 struct objfile *objfile = dwarf2_per_objfile->objfile;
22660 const struct line_header *lh = cu->line_header;
22661 unsigned int offset_size = cu->header.offset_size;
22662 struct dwarf2_section_info *section;
22663 const char *section_name;
22664
22665 if (cu->dwo_unit != nullptr)
22666 {
22667 if (section_is_gnu)
22668 {
22669 section = &cu->dwo_unit->dwo_file->sections.macro;
22670 section_name = ".debug_macro.dwo";
22671 }
22672 else
22673 {
22674 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22675 section_name = ".debug_macinfo.dwo";
22676 }
22677 }
22678 else
22679 {
22680 if (section_is_gnu)
22681 {
22682 section = &dwarf2_per_objfile->macro;
22683 section_name = ".debug_macro";
22684 }
22685 else
22686 {
22687 section = &dwarf2_per_objfile->macinfo;
22688 section_name = ".debug_macinfo";
22689 }
22690 }
22691
22692 section->read (objfile);
22693 if (section->buffer == nullptr)
22694 {
22695 complaint (_("missing %s section"), section_name);
22696 return;
22697 }
22698
22699 buildsym_compunit *builder = cu->get_builder ();
22700
22701 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22702 offset_size, offset, section_is_gnu);
22703 }
22704
22705 /* Return the .debug_loc section to use for CU.
22706 For DWO files use .debug_loc.dwo. */
22707
22708 static struct dwarf2_section_info *
22709 cu_debug_loc_section (struct dwarf2_cu *cu)
22710 {
22711 struct dwarf2_per_objfile *dwarf2_per_objfile
22712 = cu->per_cu->dwarf2_per_objfile;
22713
22714 if (cu->dwo_unit)
22715 {
22716 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22717
22718 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22719 }
22720 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22721 : &dwarf2_per_objfile->loc);
22722 }
22723
22724 /* A helper function that fills in a dwarf2_loclist_baton. */
22725
22726 static void
22727 fill_in_loclist_baton (struct dwarf2_cu *cu,
22728 struct dwarf2_loclist_baton *baton,
22729 const struct attribute *attr)
22730 {
22731 struct dwarf2_per_objfile *dwarf2_per_objfile
22732 = cu->per_cu->dwarf2_per_objfile;
22733 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22734
22735 section->read (dwarf2_per_objfile->objfile);
22736
22737 baton->per_cu = cu->per_cu;
22738 gdb_assert (baton->per_cu);
22739 /* We don't know how long the location list is, but make sure we
22740 don't run off the edge of the section. */
22741 baton->size = section->size - DW_UNSND (attr);
22742 baton->data = section->buffer + DW_UNSND (attr);
22743 if (cu->base_address.has_value ())
22744 baton->base_address = *cu->base_address;
22745 else
22746 baton->base_address = 0;
22747 baton->from_dwo = cu->dwo_unit != NULL;
22748 }
22749
22750 static void
22751 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22752 struct dwarf2_cu *cu, int is_block)
22753 {
22754 struct dwarf2_per_objfile *dwarf2_per_objfile
22755 = cu->per_cu->dwarf2_per_objfile;
22756 struct objfile *objfile = dwarf2_per_objfile->objfile;
22757 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22758
22759 if (attr->form_is_section_offset ()
22760 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22761 the section. If so, fall through to the complaint in the
22762 other branch. */
22763 && DW_UNSND (attr) < section->get_size (objfile))
22764 {
22765 struct dwarf2_loclist_baton *baton;
22766
22767 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22768
22769 fill_in_loclist_baton (cu, baton, attr);
22770
22771 if (!cu->base_address.has_value ())
22772 complaint (_("Location list used without "
22773 "specifying the CU base address."));
22774
22775 SYMBOL_ACLASS_INDEX (sym) = (is_block
22776 ? dwarf2_loclist_block_index
22777 : dwarf2_loclist_index);
22778 SYMBOL_LOCATION_BATON (sym) = baton;
22779 }
22780 else
22781 {
22782 struct dwarf2_locexpr_baton *baton;
22783
22784 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22785 baton->per_cu = cu->per_cu;
22786 gdb_assert (baton->per_cu);
22787
22788 if (attr->form_is_block ())
22789 {
22790 /* Note that we're just copying the block's data pointer
22791 here, not the actual data. We're still pointing into the
22792 info_buffer for SYM's objfile; right now we never release
22793 that buffer, but when we do clean up properly this may
22794 need to change. */
22795 baton->size = DW_BLOCK (attr)->size;
22796 baton->data = DW_BLOCK (attr)->data;
22797 }
22798 else
22799 {
22800 dwarf2_invalid_attrib_class_complaint ("location description",
22801 sym->natural_name ());
22802 baton->size = 0;
22803 }
22804
22805 SYMBOL_ACLASS_INDEX (sym) = (is_block
22806 ? dwarf2_locexpr_block_index
22807 : dwarf2_locexpr_index);
22808 SYMBOL_LOCATION_BATON (sym) = baton;
22809 }
22810 }
22811
22812 /* See read.h. */
22813
22814 struct objfile *
22815 dwarf2_per_cu_data::objfile () const
22816 {
22817 struct objfile *objfile = dwarf2_per_objfile->objfile;
22818
22819 /* Return the master objfile, so that we can report and look up the
22820 correct file containing this variable. */
22821 if (objfile->separate_debug_objfile_backlink)
22822 objfile = objfile->separate_debug_objfile_backlink;
22823
22824 return objfile;
22825 }
22826
22827 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22828 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22829 CU_HEADERP first. */
22830
22831 static const struct comp_unit_head *
22832 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22833 const struct dwarf2_per_cu_data *per_cu)
22834 {
22835 const gdb_byte *info_ptr;
22836
22837 if (per_cu->cu)
22838 return &per_cu->cu->header;
22839
22840 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22841
22842 memset (cu_headerp, 0, sizeof (*cu_headerp));
22843 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22844 rcuh_kind::COMPILE);
22845
22846 return cu_headerp;
22847 }
22848
22849 /* See read.h. */
22850
22851 int
22852 dwarf2_per_cu_data::addr_size () const
22853 {
22854 struct comp_unit_head cu_header_local;
22855 const struct comp_unit_head *cu_headerp;
22856
22857 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22858
22859 return cu_headerp->addr_size;
22860 }
22861
22862 /* See read.h. */
22863
22864 int
22865 dwarf2_per_cu_data::offset_size () const
22866 {
22867 struct comp_unit_head cu_header_local;
22868 const struct comp_unit_head *cu_headerp;
22869
22870 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22871
22872 return cu_headerp->offset_size;
22873 }
22874
22875 /* See read.h. */
22876
22877 int
22878 dwarf2_per_cu_data::ref_addr_size () const
22879 {
22880 struct comp_unit_head cu_header_local;
22881 const struct comp_unit_head *cu_headerp;
22882
22883 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22884
22885 if (cu_headerp->version == 2)
22886 return cu_headerp->addr_size;
22887 else
22888 return cu_headerp->offset_size;
22889 }
22890
22891 /* See read.h. */
22892
22893 CORE_ADDR
22894 dwarf2_per_cu_data::text_offset () const
22895 {
22896 struct objfile *objfile = dwarf2_per_objfile->objfile;
22897
22898 return objfile->text_section_offset ();
22899 }
22900
22901 /* See read.h. */
22902
22903 struct type *
22904 dwarf2_per_cu_data::addr_type () const
22905 {
22906 struct objfile *objfile = dwarf2_per_objfile->objfile;
22907 struct type *void_type = objfile_type (objfile)->builtin_void;
22908 struct type *addr_type = lookup_pointer_type (void_type);
22909 int addr_size = this->addr_size ();
22910
22911 if (TYPE_LENGTH (addr_type) == addr_size)
22912 return addr_type;
22913
22914 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
22915 return addr_type;
22916 }
22917
22918 /* A helper function for dwarf2_find_containing_comp_unit that returns
22919 the index of the result, and that searches a vector. It will
22920 return a result even if the offset in question does not actually
22921 occur in any CU. This is separate so that it can be unit
22922 tested. */
22923
22924 static int
22925 dwarf2_find_containing_comp_unit
22926 (sect_offset sect_off,
22927 unsigned int offset_in_dwz,
22928 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
22929 {
22930 int low, high;
22931
22932 low = 0;
22933 high = all_comp_units.size () - 1;
22934 while (high > low)
22935 {
22936 struct dwarf2_per_cu_data *mid_cu;
22937 int mid = low + (high - low) / 2;
22938
22939 mid_cu = all_comp_units[mid];
22940 if (mid_cu->is_dwz > offset_in_dwz
22941 || (mid_cu->is_dwz == offset_in_dwz
22942 && mid_cu->sect_off + mid_cu->length > sect_off))
22943 high = mid;
22944 else
22945 low = mid + 1;
22946 }
22947 gdb_assert (low == high);
22948 return low;
22949 }
22950
22951 /* Locate the .debug_info compilation unit from CU's objfile which contains
22952 the DIE at OFFSET. Raises an error on failure. */
22953
22954 static struct dwarf2_per_cu_data *
22955 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22956 unsigned int offset_in_dwz,
22957 struct dwarf2_per_objfile *dwarf2_per_objfile)
22958 {
22959 int low
22960 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22961 dwarf2_per_objfile->all_comp_units);
22962 struct dwarf2_per_cu_data *this_cu
22963 = dwarf2_per_objfile->all_comp_units[low];
22964
22965 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
22966 {
22967 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22968 error (_("Dwarf Error: could not find partial DIE containing "
22969 "offset %s [in module %s]"),
22970 sect_offset_str (sect_off),
22971 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
22972
22973 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22974 <= sect_off);
22975 return dwarf2_per_objfile->all_comp_units[low-1];
22976 }
22977 else
22978 {
22979 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
22980 && sect_off >= this_cu->sect_off + this_cu->length)
22981 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
22982 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
22983 return this_cu;
22984 }
22985 }
22986
22987 #if GDB_SELF_TEST
22988
22989 namespace selftests {
22990 namespace find_containing_comp_unit {
22991
22992 static void
22993 run_test ()
22994 {
22995 struct dwarf2_per_cu_data one {};
22996 struct dwarf2_per_cu_data two {};
22997 struct dwarf2_per_cu_data three {};
22998 struct dwarf2_per_cu_data four {};
22999
23000 one.length = 5;
23001 two.sect_off = sect_offset (one.length);
23002 two.length = 7;
23003
23004 three.length = 5;
23005 three.is_dwz = 1;
23006 four.sect_off = sect_offset (three.length);
23007 four.length = 7;
23008 four.is_dwz = 1;
23009
23010 std::vector<dwarf2_per_cu_data *> units;
23011 units.push_back (&one);
23012 units.push_back (&two);
23013 units.push_back (&three);
23014 units.push_back (&four);
23015
23016 int result;
23017
23018 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23019 SELF_CHECK (units[result] == &one);
23020 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23021 SELF_CHECK (units[result] == &one);
23022 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23023 SELF_CHECK (units[result] == &two);
23024
23025 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23026 SELF_CHECK (units[result] == &three);
23027 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23028 SELF_CHECK (units[result] == &three);
23029 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23030 SELF_CHECK (units[result] == &four);
23031 }
23032
23033 }
23034 }
23035
23036 #endif /* GDB_SELF_TEST */
23037
23038 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23039
23040 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23041 : per_cu (per_cu_),
23042 mark (false),
23043 has_loclist (false),
23044 checked_producer (false),
23045 producer_is_gxx_lt_4_6 (false),
23046 producer_is_gcc_lt_4_3 (false),
23047 producer_is_icc (false),
23048 producer_is_icc_lt_14 (false),
23049 producer_is_codewarrior (false),
23050 processing_has_namespace_info (false)
23051 {
23052 per_cu->cu = this;
23053 }
23054
23055 /* Destroy a dwarf2_cu. */
23056
23057 dwarf2_cu::~dwarf2_cu ()
23058 {
23059 per_cu->cu = NULL;
23060 }
23061
23062 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23063
23064 static void
23065 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23066 enum language pretend_language)
23067 {
23068 struct attribute *attr;
23069
23070 /* Set the language we're debugging. */
23071 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23072 if (attr != nullptr)
23073 set_cu_language (DW_UNSND (attr), cu);
23074 else
23075 {
23076 cu->language = pretend_language;
23077 cu->language_defn = language_def (cu->language);
23078 }
23079
23080 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23081 }
23082
23083 /* Increase the age counter on each cached compilation unit, and free
23084 any that are too old. */
23085
23086 static void
23087 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23088 {
23089 struct dwarf2_per_cu_data *per_cu, **last_chain;
23090
23091 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23092 per_cu = dwarf2_per_objfile->read_in_chain;
23093 while (per_cu != NULL)
23094 {
23095 per_cu->cu->last_used ++;
23096 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23097 dwarf2_mark (per_cu->cu);
23098 per_cu = per_cu->cu->read_in_chain;
23099 }
23100
23101 per_cu = dwarf2_per_objfile->read_in_chain;
23102 last_chain = &dwarf2_per_objfile->read_in_chain;
23103 while (per_cu != NULL)
23104 {
23105 struct dwarf2_per_cu_data *next_cu;
23106
23107 next_cu = per_cu->cu->read_in_chain;
23108
23109 if (!per_cu->cu->mark)
23110 {
23111 delete per_cu->cu;
23112 *last_chain = next_cu;
23113 }
23114 else
23115 last_chain = &per_cu->cu->read_in_chain;
23116
23117 per_cu = next_cu;
23118 }
23119 }
23120
23121 /* Remove a single compilation unit from the cache. */
23122
23123 static void
23124 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23125 {
23126 struct dwarf2_per_cu_data *per_cu, **last_chain;
23127 struct dwarf2_per_objfile *dwarf2_per_objfile
23128 = target_per_cu->dwarf2_per_objfile;
23129
23130 per_cu = dwarf2_per_objfile->read_in_chain;
23131 last_chain = &dwarf2_per_objfile->read_in_chain;
23132 while (per_cu != NULL)
23133 {
23134 struct dwarf2_per_cu_data *next_cu;
23135
23136 next_cu = per_cu->cu->read_in_chain;
23137
23138 if (per_cu == target_per_cu)
23139 {
23140 delete per_cu->cu;
23141 per_cu->cu = NULL;
23142 *last_chain = next_cu;
23143 break;
23144 }
23145 else
23146 last_chain = &per_cu->cu->read_in_chain;
23147
23148 per_cu = next_cu;
23149 }
23150 }
23151
23152 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23153 We store these in a hash table separate from the DIEs, and preserve them
23154 when the DIEs are flushed out of cache.
23155
23156 The CU "per_cu" pointer is needed because offset alone is not enough to
23157 uniquely identify the type. A file may have multiple .debug_types sections,
23158 or the type may come from a DWO file. Furthermore, while it's more logical
23159 to use per_cu->section+offset, with Fission the section with the data is in
23160 the DWO file but we don't know that section at the point we need it.
23161 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23162 because we can enter the lookup routine, get_die_type_at_offset, from
23163 outside this file, and thus won't necessarily have PER_CU->cu.
23164 Fortunately, PER_CU is stable for the life of the objfile. */
23165
23166 struct dwarf2_per_cu_offset_and_type
23167 {
23168 const struct dwarf2_per_cu_data *per_cu;
23169 sect_offset sect_off;
23170 struct type *type;
23171 };
23172
23173 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23174
23175 static hashval_t
23176 per_cu_offset_and_type_hash (const void *item)
23177 {
23178 const struct dwarf2_per_cu_offset_and_type *ofs
23179 = (const struct dwarf2_per_cu_offset_and_type *) item;
23180
23181 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23182 }
23183
23184 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23185
23186 static int
23187 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23188 {
23189 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23190 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23191 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23192 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23193
23194 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23195 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23196 }
23197
23198 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23199 table if necessary. For convenience, return TYPE.
23200
23201 The DIEs reading must have careful ordering to:
23202 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23203 reading current DIE.
23204 * Not trying to dereference contents of still incompletely read in types
23205 while reading in other DIEs.
23206 * Enable referencing still incompletely read in types just by a pointer to
23207 the type without accessing its fields.
23208
23209 Therefore caller should follow these rules:
23210 * Try to fetch any prerequisite types we may need to build this DIE type
23211 before building the type and calling set_die_type.
23212 * After building type call set_die_type for current DIE as soon as
23213 possible before fetching more types to complete the current type.
23214 * Make the type as complete as possible before fetching more types. */
23215
23216 static struct type *
23217 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23218 {
23219 struct dwarf2_per_objfile *dwarf2_per_objfile
23220 = cu->per_cu->dwarf2_per_objfile;
23221 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23222 struct objfile *objfile = dwarf2_per_objfile->objfile;
23223 struct attribute *attr;
23224 struct dynamic_prop prop;
23225
23226 /* For Ada types, make sure that the gnat-specific data is always
23227 initialized (if not already set). There are a few types where
23228 we should not be doing so, because the type-specific area is
23229 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23230 where the type-specific area is used to store the floatformat).
23231 But this is not a problem, because the gnat-specific information
23232 is actually not needed for these types. */
23233 if (need_gnat_info (cu)
23234 && TYPE_CODE (type) != TYPE_CODE_FUNC
23235 && TYPE_CODE (type) != TYPE_CODE_FLT
23236 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23237 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23238 && TYPE_CODE (type) != TYPE_CODE_METHOD
23239 && !HAVE_GNAT_AUX_INFO (type))
23240 INIT_GNAT_SPECIFIC (type);
23241
23242 /* Read DW_AT_allocated and set in type. */
23243 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23244 if (attr != NULL && attr->form_is_block ())
23245 {
23246 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23247 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23248 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23249 }
23250 else if (attr != NULL)
23251 {
23252 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23253 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23254 sect_offset_str (die->sect_off));
23255 }
23256
23257 /* Read DW_AT_associated and set in type. */
23258 attr = dwarf2_attr (die, DW_AT_associated, cu);
23259 if (attr != NULL && attr->form_is_block ())
23260 {
23261 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23262 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23263 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23264 }
23265 else if (attr != NULL)
23266 {
23267 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23268 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23269 sect_offset_str (die->sect_off));
23270 }
23271
23272 /* Read DW_AT_data_location and set in type. */
23273 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23274 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23275 cu->per_cu->addr_type ()))
23276 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23277
23278 if (dwarf2_per_objfile->die_type_hash == NULL)
23279 dwarf2_per_objfile->die_type_hash
23280 = htab_up (htab_create_alloc (127,
23281 per_cu_offset_and_type_hash,
23282 per_cu_offset_and_type_eq,
23283 NULL, xcalloc, xfree));
23284
23285 ofs.per_cu = cu->per_cu;
23286 ofs.sect_off = die->sect_off;
23287 ofs.type = type;
23288 slot = (struct dwarf2_per_cu_offset_and_type **)
23289 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23290 if (*slot)
23291 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23292 sect_offset_str (die->sect_off));
23293 *slot = XOBNEW (&objfile->objfile_obstack,
23294 struct dwarf2_per_cu_offset_and_type);
23295 **slot = ofs;
23296 return type;
23297 }
23298
23299 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23300 or return NULL if the die does not have a saved type. */
23301
23302 static struct type *
23303 get_die_type_at_offset (sect_offset sect_off,
23304 struct dwarf2_per_cu_data *per_cu)
23305 {
23306 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23307 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23308
23309 if (dwarf2_per_objfile->die_type_hash == NULL)
23310 return NULL;
23311
23312 ofs.per_cu = per_cu;
23313 ofs.sect_off = sect_off;
23314 slot = ((struct dwarf2_per_cu_offset_and_type *)
23315 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23316 if (slot)
23317 return slot->type;
23318 else
23319 return NULL;
23320 }
23321
23322 /* Look up the type for DIE in CU in die_type_hash,
23323 or return NULL if DIE does not have a saved type. */
23324
23325 static struct type *
23326 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23327 {
23328 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23329 }
23330
23331 /* Add a dependence relationship from CU to REF_PER_CU. */
23332
23333 static void
23334 dwarf2_add_dependence (struct dwarf2_cu *cu,
23335 struct dwarf2_per_cu_data *ref_per_cu)
23336 {
23337 void **slot;
23338
23339 if (cu->dependencies == NULL)
23340 cu->dependencies
23341 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23342 NULL, &cu->comp_unit_obstack,
23343 hashtab_obstack_allocate,
23344 dummy_obstack_deallocate);
23345
23346 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23347 if (*slot == NULL)
23348 *slot = ref_per_cu;
23349 }
23350
23351 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23352 Set the mark field in every compilation unit in the
23353 cache that we must keep because we are keeping CU. */
23354
23355 static int
23356 dwarf2_mark_helper (void **slot, void *data)
23357 {
23358 struct dwarf2_per_cu_data *per_cu;
23359
23360 per_cu = (struct dwarf2_per_cu_data *) *slot;
23361
23362 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23363 reading of the chain. As such dependencies remain valid it is not much
23364 useful to track and undo them during QUIT cleanups. */
23365 if (per_cu->cu == NULL)
23366 return 1;
23367
23368 if (per_cu->cu->mark)
23369 return 1;
23370 per_cu->cu->mark = true;
23371
23372 if (per_cu->cu->dependencies != NULL)
23373 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23374
23375 return 1;
23376 }
23377
23378 /* Set the mark field in CU and in every other compilation unit in the
23379 cache that we must keep because we are keeping CU. */
23380
23381 static void
23382 dwarf2_mark (struct dwarf2_cu *cu)
23383 {
23384 if (cu->mark)
23385 return;
23386 cu->mark = true;
23387 if (cu->dependencies != NULL)
23388 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23389 }
23390
23391 static void
23392 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23393 {
23394 while (per_cu)
23395 {
23396 per_cu->cu->mark = false;
23397 per_cu = per_cu->cu->read_in_chain;
23398 }
23399 }
23400
23401 /* Trivial hash function for partial_die_info: the hash value of a DIE
23402 is its offset in .debug_info for this objfile. */
23403
23404 static hashval_t
23405 partial_die_hash (const void *item)
23406 {
23407 const struct partial_die_info *part_die
23408 = (const struct partial_die_info *) item;
23409
23410 return to_underlying (part_die->sect_off);
23411 }
23412
23413 /* Trivial comparison function for partial_die_info structures: two DIEs
23414 are equal if they have the same offset. */
23415
23416 static int
23417 partial_die_eq (const void *item_lhs, const void *item_rhs)
23418 {
23419 const struct partial_die_info *part_die_lhs
23420 = (const struct partial_die_info *) item_lhs;
23421 const struct partial_die_info *part_die_rhs
23422 = (const struct partial_die_info *) item_rhs;
23423
23424 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23425 }
23426
23427 struct cmd_list_element *set_dwarf_cmdlist;
23428 struct cmd_list_element *show_dwarf_cmdlist;
23429
23430 static void
23431 set_dwarf_cmd (const char *args, int from_tty)
23432 {
23433 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23434 gdb_stdout);
23435 }
23436
23437 static void
23438 show_dwarf_cmd (const char *args, int from_tty)
23439 {
23440 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23441 }
23442
23443 static void
23444 show_check_physname (struct ui_file *file, int from_tty,
23445 struct cmd_list_element *c, const char *value)
23446 {
23447 fprintf_filtered (file,
23448 _("Whether to check \"physname\" is %s.\n"),
23449 value);
23450 }
23451
23452 void _initialize_dwarf2_read ();
23453 void
23454 _initialize_dwarf2_read ()
23455 {
23456 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23457 Set DWARF specific variables.\n\
23458 Configure DWARF variables such as the cache size."),
23459 &set_dwarf_cmdlist, "maintenance set dwarf ",
23460 0/*allow-unknown*/, &maintenance_set_cmdlist);
23461
23462 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23463 Show DWARF specific variables.\n\
23464 Show DWARF variables such as the cache size."),
23465 &show_dwarf_cmdlist, "maintenance show dwarf ",
23466 0/*allow-unknown*/, &maintenance_show_cmdlist);
23467
23468 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23469 &dwarf_max_cache_age, _("\
23470 Set the upper bound on the age of cached DWARF compilation units."), _("\
23471 Show the upper bound on the age of cached DWARF compilation units."), _("\
23472 A higher limit means that cached compilation units will be stored\n\
23473 in memory longer, and more total memory will be used. Zero disables\n\
23474 caching, which can slow down startup."),
23475 NULL,
23476 show_dwarf_max_cache_age,
23477 &set_dwarf_cmdlist,
23478 &show_dwarf_cmdlist);
23479
23480 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23481 Set debugging of the DWARF reader."), _("\
23482 Show debugging of the DWARF reader."), _("\
23483 When enabled (non-zero), debugging messages are printed during DWARF\n\
23484 reading and symtab expansion. A value of 1 (one) provides basic\n\
23485 information. A value greater than 1 provides more verbose information."),
23486 NULL,
23487 NULL,
23488 &setdebuglist, &showdebuglist);
23489
23490 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23491 Set debugging of the DWARF DIE reader."), _("\
23492 Show debugging of the DWARF DIE reader."), _("\
23493 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23494 The value is the maximum depth to print."),
23495 NULL,
23496 NULL,
23497 &setdebuglist, &showdebuglist);
23498
23499 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23500 Set debugging of the dwarf line reader."), _("\
23501 Show debugging of the dwarf line reader."), _("\
23502 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23503 A value of 1 (one) provides basic information.\n\
23504 A value greater than 1 provides more verbose information."),
23505 NULL,
23506 NULL,
23507 &setdebuglist, &showdebuglist);
23508
23509 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23510 Set cross-checking of \"physname\" code against demangler."), _("\
23511 Show cross-checking of \"physname\" code against demangler."), _("\
23512 When enabled, GDB's internal \"physname\" code is checked against\n\
23513 the demangler."),
23514 NULL, show_check_physname,
23515 &setdebuglist, &showdebuglist);
23516
23517 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23518 no_class, &use_deprecated_index_sections, _("\
23519 Set whether to use deprecated gdb_index sections."), _("\
23520 Show whether to use deprecated gdb_index sections."), _("\
23521 When enabled, deprecated .gdb_index sections are used anyway.\n\
23522 Normally they are ignored either because of a missing feature or\n\
23523 performance issue.\n\
23524 Warning: This option must be enabled before gdb reads the file."),
23525 NULL,
23526 NULL,
23527 &setlist, &showlist);
23528
23529 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23530 &dwarf2_locexpr_funcs);
23531 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23532 &dwarf2_loclist_funcs);
23533
23534 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23535 &dwarf2_block_frame_base_locexpr_funcs);
23536 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23537 &dwarf2_block_frame_base_loclist_funcs);
23538
23539 #if GDB_SELF_TEST
23540 selftests::register_test ("dw2_expand_symtabs_matching",
23541 selftests::dw2_expand_symtabs_matching::run_test);
23542 selftests::register_test ("dwarf2_find_containing_comp_unit",
23543 selftests::find_containing_comp_unit::run_test);
23544 #endif
23545 }
This page took 0.493659 seconds and 5 git commands to generate.