Convert dwarf2_section_buffer_overflow_complaint to a method
[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 "bfd.h"
42 #include "elf-bfd.h"
43 #include "symtab.h"
44 #include "gdbtypes.h"
45 #include "objfiles.h"
46 #include "dwarf2.h"
47 #include "buildsym.h"
48 #include "demangle.h"
49 #include "gdb-demangle.h"
50 #include "filenames.h" /* for DOSish file names */
51 #include "macrotab.h"
52 #include "language.h"
53 #include "complaints.h"
54 #include "dwarf2/expr.h"
55 #include "dwarf2/loc.h"
56 #include "cp-support.h"
57 #include "hashtab.h"
58 #include "command.h"
59 #include "gdbcmd.h"
60 #include "block.h"
61 #include "addrmap.h"
62 #include "typeprint.h"
63 #include "psympriv.h"
64 #include "c-lang.h"
65 #include "go-lang.h"
66 #include "valprint.h"
67 #include "gdbcore.h" /* for gnutarget */
68 #include "gdb/gdb-index.h"
69 #include "gdb_bfd.h"
70 #include "f-lang.h"
71 #include "source.h"
72 #include "build-id.h"
73 #include "namespace.h"
74 #include "gdbsupport/function-view.h"
75 #include "gdbsupport/gdb_optional.h"
76 #include "gdbsupport/underlying.h"
77 #include "gdbsupport/hash_enum.h"
78 #include "filename-seen-cache.h"
79 #include "producer.h"
80 #include <fcntl.h>
81 #include <algorithm>
82 #include <unordered_map>
83 #include "gdbsupport/selftest.h"
84 #include "rust-lang.h"
85 #include "gdbsupport/pathstuff.h"
86 #include "count-one-bits.h"
87 #include "debuginfod-support.h"
88
89 /* When == 1, print basic high level tracing messages.
90 When > 1, be more verbose.
91 This is in contrast to the low level DIE reading of dwarf_die_debug. */
92 static unsigned int dwarf_read_debug = 0;
93
94 /* When non-zero, dump DIEs after they are read in. */
95 static unsigned int dwarf_die_debug = 0;
96
97 /* When non-zero, dump line number entries as they are read in. */
98 unsigned int dwarf_line_debug = 0;
99
100 /* When true, cross-check physname against demangler. */
101 static bool check_physname = false;
102
103 /* When true, do not reject deprecated .gdb_index sections. */
104 static bool use_deprecated_index_sections = false;
105
106 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
107
108 /* The "aclass" indices for various kinds of computed DWARF symbols. */
109
110 static int dwarf2_locexpr_index;
111 static int dwarf2_loclist_index;
112 static int dwarf2_locexpr_block_index;
113 static int dwarf2_loclist_block_index;
114
115 /* An index into a (C++) symbol name component in a symbol name as
116 recorded in the mapped_index's symbol table. For each C++ symbol
117 in the symbol table, we record one entry for the start of each
118 component in the symbol in a table of name components, and then
119 sort the table, in order to be able to binary search symbol names,
120 ignoring leading namespaces, both completion and regular look up.
121 For example, for symbol "A::B::C", we'll have an entry that points
122 to "A::B::C", another that points to "B::C", and another for "C".
123 Note that function symbols in GDB index have no parameter
124 information, just the function/method names. You can convert a
125 name_component to a "const char *" using the
126 'mapped_index::symbol_name_at(offset_type)' method. */
127
128 struct name_component
129 {
130 /* Offset in the symbol name where the component starts. Stored as
131 a (32-bit) offset instead of a pointer to save memory and improve
132 locality on 64-bit architectures. */
133 offset_type name_offset;
134
135 /* The symbol's index in the symbol and constant pool tables of a
136 mapped_index. */
137 offset_type idx;
138 };
139
140 /* Base class containing bits shared by both .gdb_index and
141 .debug_name indexes. */
142
143 struct mapped_index_base
144 {
145 mapped_index_base () = default;
146 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
147
148 /* The name_component table (a sorted vector). See name_component's
149 description above. */
150 std::vector<name_component> name_components;
151
152 /* How NAME_COMPONENTS is sorted. */
153 enum case_sensitivity name_components_casing;
154
155 /* Return the number of names in the symbol table. */
156 virtual size_t symbol_name_count () const = 0;
157
158 /* Get the name of the symbol at IDX in the symbol table. */
159 virtual const char *symbol_name_at (offset_type idx) const = 0;
160
161 /* Return whether the name at IDX in the symbol table should be
162 ignored. */
163 virtual bool symbol_name_slot_invalid (offset_type idx) const
164 {
165 return false;
166 }
167
168 /* Build the symbol name component sorted vector, if we haven't
169 yet. */
170 void build_name_components ();
171
172 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
173 possible matches for LN_NO_PARAMS in the name component
174 vector. */
175 std::pair<std::vector<name_component>::const_iterator,
176 std::vector<name_component>::const_iterator>
177 find_name_components_bounds (const lookup_name_info &ln_no_params,
178 enum language lang) const;
179
180 /* Prevent deleting/destroying via a base class pointer. */
181 protected:
182 ~mapped_index_base() = default;
183 };
184
185 /* A description of the mapped index. The file format is described in
186 a comment by the code that writes the index. */
187 struct mapped_index final : public mapped_index_base
188 {
189 /* A slot/bucket in the symbol table hash. */
190 struct symbol_table_slot
191 {
192 const offset_type name;
193 const offset_type vec;
194 };
195
196 /* Index data format version. */
197 int version = 0;
198
199 /* The address table data. */
200 gdb::array_view<const gdb_byte> address_table;
201
202 /* The symbol table, implemented as a hash table. */
203 gdb::array_view<symbol_table_slot> symbol_table;
204
205 /* A pointer to the constant pool. */
206 const char *constant_pool = nullptr;
207
208 bool symbol_name_slot_invalid (offset_type idx) const override
209 {
210 const auto &bucket = this->symbol_table[idx];
211 return bucket.name == 0 && bucket.vec == 0;
212 }
213
214 /* Convenience method to get at the name of the symbol at IDX in the
215 symbol table. */
216 const char *symbol_name_at (offset_type idx) const override
217 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
218
219 size_t symbol_name_count () const override
220 { return this->symbol_table.size (); }
221 };
222
223 /* A description of the mapped .debug_names.
224 Uninitialized map has CU_COUNT 0. */
225 struct mapped_debug_names final : public mapped_index_base
226 {
227 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
228 : dwarf2_per_objfile (dwarf2_per_objfile_)
229 {}
230
231 struct dwarf2_per_objfile *dwarf2_per_objfile;
232 bfd_endian dwarf5_byte_order;
233 bool dwarf5_is_dwarf64;
234 bool augmentation_is_gdb;
235 uint8_t offset_size;
236 uint32_t cu_count = 0;
237 uint32_t tu_count, bucket_count, name_count;
238 const gdb_byte *cu_table_reordered, *tu_table_reordered;
239 const uint32_t *bucket_table_reordered, *hash_table_reordered;
240 const gdb_byte *name_table_string_offs_reordered;
241 const gdb_byte *name_table_entry_offs_reordered;
242 const gdb_byte *entry_pool;
243
244 struct index_val
245 {
246 ULONGEST dwarf_tag;
247 struct attr
248 {
249 /* Attribute name DW_IDX_*. */
250 ULONGEST dw_idx;
251
252 /* Attribute form DW_FORM_*. */
253 ULONGEST form;
254
255 /* Value if FORM is DW_FORM_implicit_const. */
256 LONGEST implicit_const;
257 };
258 std::vector<attr> attr_vec;
259 };
260
261 std::unordered_map<ULONGEST, index_val> abbrev_map;
262
263 const char *namei_to_name (uint32_t namei) const;
264
265 /* Implementation of the mapped_index_base virtual interface, for
266 the name_components cache. */
267
268 const char *symbol_name_at (offset_type idx) const override
269 { return namei_to_name (idx); }
270
271 size_t symbol_name_count () const override
272 { return this->name_count; }
273 };
274
275 /* See dwarf2read.h. */
276
277 dwarf2_per_objfile *
278 get_dwarf2_per_objfile (struct objfile *objfile)
279 {
280 return dwarf2_objfile_data_key.get (objfile);
281 }
282
283 /* Default names of the debugging sections. */
284
285 /* Note that if the debugging section has been compressed, it might
286 have a name like .zdebug_info. */
287
288 static const struct dwarf2_debug_sections dwarf2_elf_names =
289 {
290 { ".debug_info", ".zdebug_info" },
291 { ".debug_abbrev", ".zdebug_abbrev" },
292 { ".debug_line", ".zdebug_line" },
293 { ".debug_loc", ".zdebug_loc" },
294 { ".debug_loclists", ".zdebug_loclists" },
295 { ".debug_macinfo", ".zdebug_macinfo" },
296 { ".debug_macro", ".zdebug_macro" },
297 { ".debug_str", ".zdebug_str" },
298 { ".debug_str_offsets", ".zdebug_str_offsets" },
299 { ".debug_line_str", ".zdebug_line_str" },
300 { ".debug_ranges", ".zdebug_ranges" },
301 { ".debug_rnglists", ".zdebug_rnglists" },
302 { ".debug_types", ".zdebug_types" },
303 { ".debug_addr", ".zdebug_addr" },
304 { ".debug_frame", ".zdebug_frame" },
305 { ".eh_frame", NULL },
306 { ".gdb_index", ".zgdb_index" },
307 { ".debug_names", ".zdebug_names" },
308 { ".debug_aranges", ".zdebug_aranges" },
309 23
310 };
311
312 /* List of DWO/DWP sections. */
313
314 static const struct dwop_section_names
315 {
316 struct dwarf2_section_names abbrev_dwo;
317 struct dwarf2_section_names info_dwo;
318 struct dwarf2_section_names line_dwo;
319 struct dwarf2_section_names loc_dwo;
320 struct dwarf2_section_names loclists_dwo;
321 struct dwarf2_section_names macinfo_dwo;
322 struct dwarf2_section_names macro_dwo;
323 struct dwarf2_section_names str_dwo;
324 struct dwarf2_section_names str_offsets_dwo;
325 struct dwarf2_section_names types_dwo;
326 struct dwarf2_section_names cu_index;
327 struct dwarf2_section_names tu_index;
328 }
329 dwop_section_names =
330 {
331 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
332 { ".debug_info.dwo", ".zdebug_info.dwo" },
333 { ".debug_line.dwo", ".zdebug_line.dwo" },
334 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
335 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
336 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
337 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
338 { ".debug_str.dwo", ".zdebug_str.dwo" },
339 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
340 { ".debug_types.dwo", ".zdebug_types.dwo" },
341 { ".debug_cu_index", ".zdebug_cu_index" },
342 { ".debug_tu_index", ".zdebug_tu_index" },
343 };
344
345 /* local data types */
346
347 /* Type used for delaying computation of method physnames.
348 See comments for compute_delayed_physnames. */
349 struct delayed_method_info
350 {
351 /* The type to which the method is attached, i.e., its parent class. */
352 struct type *type;
353
354 /* The index of the method in the type's function fieldlists. */
355 int fnfield_index;
356
357 /* The index of the method in the fieldlist. */
358 int index;
359
360 /* The name of the DIE. */
361 const char *name;
362
363 /* The DIE associated with this method. */
364 struct die_info *die;
365 };
366
367 /* Internal state when decoding a particular compilation unit. */
368 struct dwarf2_cu
369 {
370 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
371 ~dwarf2_cu ();
372
373 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
374
375 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
376 Create the set of symtabs used by this TU, or if this TU is sharing
377 symtabs with another TU and the symtabs have already been created
378 then restore those symtabs in the line header.
379 We don't need the pc/line-number mapping for type units. */
380 void setup_type_unit_groups (struct die_info *die);
381
382 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
383 buildsym_compunit constructor. */
384 struct compunit_symtab *start_symtab (const char *name,
385 const char *comp_dir,
386 CORE_ADDR low_pc);
387
388 /* Reset the builder. */
389 void reset_builder () { m_builder.reset (); }
390
391 /* The header of the compilation unit. */
392 struct comp_unit_head header {};
393
394 /* Base address of this compilation unit. */
395 CORE_ADDR base_address = 0;
396
397 /* Non-zero if base_address has been set. */
398 int base_known = 0;
399
400 /* The language we are debugging. */
401 enum language language = language_unknown;
402 const struct language_defn *language_defn = nullptr;
403
404 const char *producer = nullptr;
405
406 private:
407 /* The symtab builder for this CU. This is only non-NULL when full
408 symbols are being read. */
409 std::unique_ptr<buildsym_compunit> m_builder;
410
411 public:
412 /* The generic symbol table building routines have separate lists for
413 file scope symbols and all all other scopes (local scopes). So
414 we need to select the right one to pass to add_symbol_to_list().
415 We do it by keeping a pointer to the correct list in list_in_scope.
416
417 FIXME: The original dwarf code just treated the file scope as the
418 first local scope, and all other local scopes as nested local
419 scopes, and worked fine. Check to see if we really need to
420 distinguish these in buildsym.c. */
421 struct pending **list_in_scope = nullptr;
422
423 /* Hash table holding all the loaded partial DIEs
424 with partial_die->offset.SECT_OFF as hash. */
425 htab_t partial_dies = nullptr;
426
427 /* Storage for things with the same lifetime as this read-in compilation
428 unit, including partial DIEs. */
429 auto_obstack comp_unit_obstack;
430
431 /* When multiple dwarf2_cu structures are living in memory, this field
432 chains them all together, so that they can be released efficiently.
433 We will probably also want a generation counter so that most-recently-used
434 compilation units are cached... */
435 struct dwarf2_per_cu_data *read_in_chain = nullptr;
436
437 /* Backlink to our per_cu entry. */
438 struct dwarf2_per_cu_data *per_cu;
439
440 /* How many compilation units ago was this CU last referenced? */
441 int last_used = 0;
442
443 /* A hash table of DIE cu_offset for following references with
444 die_info->offset.sect_off as hash. */
445 htab_t die_hash = nullptr;
446
447 /* Full DIEs if read in. */
448 struct die_info *dies = nullptr;
449
450 /* A set of pointers to dwarf2_per_cu_data objects for compilation
451 units referenced by this one. Only set during full symbol processing;
452 partial symbol tables do not have dependencies. */
453 htab_t dependencies = nullptr;
454
455 /* Header data from the line table, during full symbol processing. */
456 struct line_header *line_header = nullptr;
457 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
458 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
459 this is the DW_TAG_compile_unit die for this CU. We'll hold on
460 to the line header as long as this DIE is being processed. See
461 process_die_scope. */
462 die_info *line_header_die_owner = nullptr;
463
464 /* A list of methods which need to have physnames computed
465 after all type information has been read. */
466 std::vector<delayed_method_info> method_list;
467
468 /* To be copied to symtab->call_site_htab. */
469 htab_t call_site_htab = nullptr;
470
471 /* Non-NULL if this CU came from a DWO file.
472 There is an invariant here that is important to remember:
473 Except for attributes copied from the top level DIE in the "main"
474 (or "stub") file in preparation for reading the DWO file
475 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
476 Either there isn't a DWO file (in which case this is NULL and the point
477 is moot), or there is and either we're not going to read it (in which
478 case this is NULL) or there is and we are reading it (in which case this
479 is non-NULL). */
480 struct dwo_unit *dwo_unit = nullptr;
481
482 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
483 Note this value comes from the Fission stub CU/TU's DIE. */
484 gdb::optional<ULONGEST> addr_base;
485
486 /* The DW_AT_rnglists_base attribute if present.
487 Note this value comes from the Fission stub CU/TU's DIE.
488 Also note that the value is zero in the non-DWO case so this value can
489 be used without needing to know whether DWO files are in use or not.
490 N.B. This does not apply to DW_AT_ranges appearing in
491 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
492 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
493 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
494 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
495 ULONGEST ranges_base = 0;
496
497 /* When reading debug info generated by older versions of rustc, we
498 have to rewrite some union types to be struct types with a
499 variant part. This rewriting must be done after the CU is fully
500 read in, because otherwise at the point of rewriting some struct
501 type might not have been fully processed. So, we keep a list of
502 all such types here and process them after expansion. */
503 std::vector<struct type *> rust_unions;
504
505 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
506 files, the value is implicitly zero. For DWARF 5 version DWO files, the
507 value is often implicit and is the size of the header of
508 .debug_str_offsets section (8 or 4, depending on the address size). */
509 gdb::optional<ULONGEST> str_offsets_base;
510
511 /* Mark used when releasing cached dies. */
512 bool mark : 1;
513
514 /* This CU references .debug_loc. See the symtab->locations_valid field.
515 This test is imperfect as there may exist optimized debug code not using
516 any location list and still facing inlining issues if handled as
517 unoptimized code. For a future better test see GCC PR other/32998. */
518 bool has_loclist : 1;
519
520 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
521 if all the producer_is_* fields are valid. This information is cached
522 because profiling CU expansion showed excessive time spent in
523 producer_is_gxx_lt_4_6. */
524 bool checked_producer : 1;
525 bool producer_is_gxx_lt_4_6 : 1;
526 bool producer_is_gcc_lt_4_3 : 1;
527 bool producer_is_icc : 1;
528 bool producer_is_icc_lt_14 : 1;
529 bool producer_is_codewarrior : 1;
530
531 /* When true, the file that we're processing is known to have
532 debugging info for C++ namespaces. GCC 3.3.x did not produce
533 this information, but later versions do. */
534
535 bool processing_has_namespace_info : 1;
536
537 struct partial_die_info *find_partial_die (sect_offset sect_off);
538
539 /* If this CU was inherited by another CU (via specification,
540 abstract_origin, etc), this is the ancestor CU. */
541 dwarf2_cu *ancestor;
542
543 /* Get the buildsym_compunit for this CU. */
544 buildsym_compunit *get_builder ()
545 {
546 /* If this CU has a builder associated with it, use that. */
547 if (m_builder != nullptr)
548 return m_builder.get ();
549
550 /* Otherwise, search ancestors for a valid builder. */
551 if (ancestor != nullptr)
552 return ancestor->get_builder ();
553
554 return nullptr;
555 }
556 };
557
558 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
559 This includes type_unit_group and quick_file_names. */
560
561 struct stmt_list_hash
562 {
563 /* The DWO unit this table is from or NULL if there is none. */
564 struct dwo_unit *dwo_unit;
565
566 /* Offset in .debug_line or .debug_line.dwo. */
567 sect_offset line_sect_off;
568 };
569
570 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
571 an object of this type. */
572
573 struct type_unit_group
574 {
575 /* dwarf2read.c's main "handle" on a TU symtab.
576 To simplify things we create an artificial CU that "includes" all the
577 type units using this stmt_list so that the rest of the code still has
578 a "per_cu" handle on the symtab. */
579 struct dwarf2_per_cu_data per_cu;
580
581 /* The TUs that share this DW_AT_stmt_list entry.
582 This is added to while parsing type units to build partial symtabs,
583 and is deleted afterwards and not used again. */
584 std::vector<signatured_type *> *tus;
585
586 /* The compunit symtab.
587 Type units in a group needn't all be defined in the same source file,
588 so we create an essentially anonymous symtab as the compunit symtab. */
589 struct compunit_symtab *compunit_symtab;
590
591 /* The data used to construct the hash key. */
592 struct stmt_list_hash hash;
593
594 /* The symbol tables for this TU (obtained from the files listed in
595 DW_AT_stmt_list).
596 WARNING: The order of entries here must match the order of entries
597 in the line header. After the first TU using this type_unit_group, the
598 line header for the subsequent TUs is recreated from this. This is done
599 because we need to use the same symtabs for each TU using the same
600 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
601 there's no guarantee the line header doesn't have duplicate entries. */
602 struct symtab **symtabs;
603 };
604
605 /* These sections are what may appear in a (real or virtual) DWO file. */
606
607 struct dwo_sections
608 {
609 struct dwarf2_section_info abbrev;
610 struct dwarf2_section_info line;
611 struct dwarf2_section_info loc;
612 struct dwarf2_section_info loclists;
613 struct dwarf2_section_info macinfo;
614 struct dwarf2_section_info macro;
615 struct dwarf2_section_info str;
616 struct dwarf2_section_info str_offsets;
617 /* In the case of a virtual DWO file, these two are unused. */
618 struct dwarf2_section_info info;
619 std::vector<dwarf2_section_info> types;
620 };
621
622 /* CUs/TUs in DWP/DWO files. */
623
624 struct dwo_unit
625 {
626 /* Backlink to the containing struct dwo_file. */
627 struct dwo_file *dwo_file;
628
629 /* The "id" that distinguishes this CU/TU.
630 .debug_info calls this "dwo_id", .debug_types calls this "signature".
631 Since signatures came first, we stick with it for consistency. */
632 ULONGEST signature;
633
634 /* The section this CU/TU lives in, in the DWO file. */
635 struct dwarf2_section_info *section;
636
637 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
638 sect_offset sect_off;
639 unsigned int length;
640
641 /* For types, offset in the type's DIE of the type defined by this TU. */
642 cu_offset type_offset_in_tu;
643 };
644
645 /* include/dwarf2.h defines the DWP section codes.
646 It defines a max value but it doesn't define a min value, which we
647 use for error checking, so provide one. */
648
649 enum dwp_v2_section_ids
650 {
651 DW_SECT_MIN = 1
652 };
653
654 /* Data for one DWO file.
655
656 This includes virtual DWO files (a virtual DWO file is a DWO file as it
657 appears in a DWP file). DWP files don't really have DWO files per se -
658 comdat folding of types "loses" the DWO file they came from, and from
659 a high level view DWP files appear to contain a mass of random types.
660 However, to maintain consistency with the non-DWP case we pretend DWP
661 files contain virtual DWO files, and we assign each TU with one virtual
662 DWO file (generally based on the line and abbrev section offsets -
663 a heuristic that seems to work in practice). */
664
665 struct dwo_file
666 {
667 dwo_file () = default;
668 DISABLE_COPY_AND_ASSIGN (dwo_file);
669
670 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
671 For virtual DWO files the name is constructed from the section offsets
672 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
673 from related CU+TUs. */
674 const char *dwo_name = nullptr;
675
676 /* The DW_AT_comp_dir attribute. */
677 const char *comp_dir = nullptr;
678
679 /* The bfd, when the file is open. Otherwise this is NULL.
680 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
681 gdb_bfd_ref_ptr dbfd;
682
683 /* The sections that make up this DWO file.
684 Remember that for virtual DWO files in DWP V2, these are virtual
685 sections (for lack of a better name). */
686 struct dwo_sections sections {};
687
688 /* The CUs in the file.
689 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
690 an extension to handle LLVM's Link Time Optimization output (where
691 multiple source files may be compiled into a single object/dwo pair). */
692 htab_up cus;
693
694 /* Table of TUs in the file.
695 Each element is a struct dwo_unit. */
696 htab_up tus;
697 };
698
699 /* These sections are what may appear in a DWP file. */
700
701 struct dwp_sections
702 {
703 /* These are used by both DWP version 1 and 2. */
704 struct dwarf2_section_info str;
705 struct dwarf2_section_info cu_index;
706 struct dwarf2_section_info tu_index;
707
708 /* These are only used by DWP version 2 files.
709 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
710 sections are referenced by section number, and are not recorded here.
711 In DWP version 2 there is at most one copy of all these sections, each
712 section being (effectively) comprised of the concatenation of all of the
713 individual sections that exist in the version 1 format.
714 To keep the code simple we treat each of these concatenated pieces as a
715 section itself (a virtual section?). */
716 struct dwarf2_section_info abbrev;
717 struct dwarf2_section_info info;
718 struct dwarf2_section_info line;
719 struct dwarf2_section_info loc;
720 struct dwarf2_section_info macinfo;
721 struct dwarf2_section_info macro;
722 struct dwarf2_section_info str_offsets;
723 struct dwarf2_section_info types;
724 };
725
726 /* These sections are what may appear in a virtual DWO file in DWP version 1.
727 A virtual DWO file is a DWO file as it appears in a DWP file. */
728
729 struct virtual_v1_dwo_sections
730 {
731 struct dwarf2_section_info abbrev;
732 struct dwarf2_section_info line;
733 struct dwarf2_section_info loc;
734 struct dwarf2_section_info macinfo;
735 struct dwarf2_section_info macro;
736 struct dwarf2_section_info str_offsets;
737 /* Each DWP hash table entry records one CU or one TU.
738 That is recorded here, and copied to dwo_unit.section. */
739 struct dwarf2_section_info info_or_types;
740 };
741
742 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
743 In version 2, the sections of the DWO files are concatenated together
744 and stored in one section of that name. Thus each ELF section contains
745 several "virtual" sections. */
746
747 struct virtual_v2_dwo_sections
748 {
749 bfd_size_type abbrev_offset;
750 bfd_size_type abbrev_size;
751
752 bfd_size_type line_offset;
753 bfd_size_type line_size;
754
755 bfd_size_type loc_offset;
756 bfd_size_type loc_size;
757
758 bfd_size_type macinfo_offset;
759 bfd_size_type macinfo_size;
760
761 bfd_size_type macro_offset;
762 bfd_size_type macro_size;
763
764 bfd_size_type str_offsets_offset;
765 bfd_size_type str_offsets_size;
766
767 /* Each DWP hash table entry records one CU or one TU.
768 That is recorded here, and copied to dwo_unit.section. */
769 bfd_size_type info_or_types_offset;
770 bfd_size_type info_or_types_size;
771 };
772
773 /* Contents of DWP hash tables. */
774
775 struct dwp_hash_table
776 {
777 uint32_t version, nr_columns;
778 uint32_t nr_units, nr_slots;
779 const gdb_byte *hash_table, *unit_table;
780 union
781 {
782 struct
783 {
784 const gdb_byte *indices;
785 } v1;
786 struct
787 {
788 /* This is indexed by column number and gives the id of the section
789 in that column. */
790 #define MAX_NR_V2_DWO_SECTIONS \
791 (1 /* .debug_info or .debug_types */ \
792 + 1 /* .debug_abbrev */ \
793 + 1 /* .debug_line */ \
794 + 1 /* .debug_loc */ \
795 + 1 /* .debug_str_offsets */ \
796 + 1 /* .debug_macro or .debug_macinfo */)
797 int section_ids[MAX_NR_V2_DWO_SECTIONS];
798 const gdb_byte *offsets;
799 const gdb_byte *sizes;
800 } v2;
801 } section_pool;
802 };
803
804 /* Data for one DWP file. */
805
806 struct dwp_file
807 {
808 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
809 : name (name_),
810 dbfd (std::move (abfd))
811 {
812 }
813
814 /* Name of the file. */
815 const char *name;
816
817 /* File format version. */
818 int version = 0;
819
820 /* The bfd. */
821 gdb_bfd_ref_ptr dbfd;
822
823 /* Section info for this file. */
824 struct dwp_sections sections {};
825
826 /* Table of CUs in the file. */
827 const struct dwp_hash_table *cus = nullptr;
828
829 /* Table of TUs in the file. */
830 const struct dwp_hash_table *tus = nullptr;
831
832 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
833 htab_up loaded_cus;
834 htab_up loaded_tus;
835
836 /* Table to map ELF section numbers to their sections.
837 This is only needed for the DWP V1 file format. */
838 unsigned int num_sections = 0;
839 asection **elf_sections = nullptr;
840 };
841
842 /* Struct used to pass misc. parameters to read_die_and_children, et
843 al. which are used for both .debug_info and .debug_types dies.
844 All parameters here are unchanging for the life of the call. This
845 struct exists to abstract away the constant parameters of die reading. */
846
847 struct die_reader_specs
848 {
849 /* The bfd of die_section. */
850 bfd* abfd;
851
852 /* The CU of the DIE we are parsing. */
853 struct dwarf2_cu *cu;
854
855 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
856 struct dwo_file *dwo_file;
857
858 /* The section the die comes from.
859 This is either .debug_info or .debug_types, or the .dwo variants. */
860 struct dwarf2_section_info *die_section;
861
862 /* die_section->buffer. */
863 const gdb_byte *buffer;
864
865 /* The end of the buffer. */
866 const gdb_byte *buffer_end;
867
868 /* The abbreviation table to use when reading the DIEs. */
869 struct abbrev_table *abbrev_table;
870 };
871
872 /* A subclass of die_reader_specs that holds storage and has complex
873 constructor and destructor behavior. */
874
875 class cutu_reader : public die_reader_specs
876 {
877 public:
878
879 cutu_reader (struct dwarf2_per_cu_data *this_cu,
880 struct abbrev_table *abbrev_table,
881 int use_existing_cu,
882 bool skip_partial);
883
884 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
885 struct dwarf2_cu *parent_cu = nullptr,
886 struct dwo_file *dwo_file = nullptr);
887
888 DISABLE_COPY_AND_ASSIGN (cutu_reader);
889
890 const gdb_byte *info_ptr = nullptr;
891 struct die_info *comp_unit_die = nullptr;
892 bool dummy_p = false;
893
894 /* Release the new CU, putting it on the chain. This cannot be done
895 for dummy CUs. */
896 void keep ();
897
898 private:
899 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
900 int use_existing_cu);
901
902 struct dwarf2_per_cu_data *m_this_cu;
903 std::unique_ptr<dwarf2_cu> m_new_cu;
904
905 /* The ordinary abbreviation table. */
906 abbrev_table_up m_abbrev_table_holder;
907
908 /* The DWO abbreviation table. */
909 abbrev_table_up m_dwo_abbrev_table;
910 };
911
912 /* When we construct a partial symbol table entry we only
913 need this much information. */
914 struct partial_die_info : public allocate_on_obstack
915 {
916 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
917
918 /* Disable assign but still keep copy ctor, which is needed
919 load_partial_dies. */
920 partial_die_info& operator=(const partial_die_info& rhs) = delete;
921
922 /* Adjust the partial die before generating a symbol for it. This
923 function may set the is_external flag or change the DIE's
924 name. */
925 void fixup (struct dwarf2_cu *cu);
926
927 /* Read a minimal amount of information into the minimal die
928 structure. */
929 const gdb_byte *read (const struct die_reader_specs *reader,
930 const struct abbrev_info &abbrev,
931 const gdb_byte *info_ptr);
932
933 /* Offset of this DIE. */
934 const sect_offset sect_off;
935
936 /* DWARF-2 tag for this DIE. */
937 const ENUM_BITFIELD(dwarf_tag) tag : 16;
938
939 /* Assorted flags describing the data found in this DIE. */
940 const unsigned int has_children : 1;
941
942 unsigned int is_external : 1;
943 unsigned int is_declaration : 1;
944 unsigned int has_type : 1;
945 unsigned int has_specification : 1;
946 unsigned int has_pc_info : 1;
947 unsigned int may_be_inlined : 1;
948
949 /* This DIE has been marked DW_AT_main_subprogram. */
950 unsigned int main_subprogram : 1;
951
952 /* Flag set if the SCOPE field of this structure has been
953 computed. */
954 unsigned int scope_set : 1;
955
956 /* Flag set if the DIE has a byte_size attribute. */
957 unsigned int has_byte_size : 1;
958
959 /* Flag set if the DIE has a DW_AT_const_value attribute. */
960 unsigned int has_const_value : 1;
961
962 /* Flag set if any of the DIE's children are template arguments. */
963 unsigned int has_template_arguments : 1;
964
965 /* Flag set if fixup has been called on this die. */
966 unsigned int fixup_called : 1;
967
968 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
969 unsigned int is_dwz : 1;
970
971 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
972 unsigned int spec_is_dwz : 1;
973
974 /* The name of this DIE. Normally the value of DW_AT_name, but
975 sometimes a default name for unnamed DIEs. */
976 const char *name = nullptr;
977
978 /* The linkage name, if present. */
979 const char *linkage_name = nullptr;
980
981 /* The scope to prepend to our children. This is generally
982 allocated on the comp_unit_obstack, so will disappear
983 when this compilation unit leaves the cache. */
984 const char *scope = nullptr;
985
986 /* Some data associated with the partial DIE. The tag determines
987 which field is live. */
988 union
989 {
990 /* The location description associated with this DIE, if any. */
991 struct dwarf_block *locdesc;
992 /* The offset of an import, for DW_TAG_imported_unit. */
993 sect_offset sect_off;
994 } d {};
995
996 /* If HAS_PC_INFO, the PC range associated with this DIE. */
997 CORE_ADDR lowpc = 0;
998 CORE_ADDR highpc = 0;
999
1000 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1001 DW_AT_sibling, if any. */
1002 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1003 could return DW_AT_sibling values to its caller load_partial_dies. */
1004 const gdb_byte *sibling = nullptr;
1005
1006 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1007 DW_AT_specification (or DW_AT_abstract_origin or
1008 DW_AT_extension). */
1009 sect_offset spec_offset {};
1010
1011 /* Pointers to this DIE's parent, first child, and next sibling,
1012 if any. */
1013 struct partial_die_info *die_parent = nullptr;
1014 struct partial_die_info *die_child = nullptr;
1015 struct partial_die_info *die_sibling = nullptr;
1016
1017 friend struct partial_die_info *
1018 dwarf2_cu::find_partial_die (sect_offset sect_off);
1019
1020 private:
1021 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1022 partial_die_info (sect_offset sect_off)
1023 : partial_die_info (sect_off, DW_TAG_padding, 0)
1024 {
1025 }
1026
1027 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1028 int has_children_)
1029 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1030 {
1031 is_external = 0;
1032 is_declaration = 0;
1033 has_type = 0;
1034 has_specification = 0;
1035 has_pc_info = 0;
1036 may_be_inlined = 0;
1037 main_subprogram = 0;
1038 scope_set = 0;
1039 has_byte_size = 0;
1040 has_const_value = 0;
1041 has_template_arguments = 0;
1042 fixup_called = 0;
1043 is_dwz = 0;
1044 spec_is_dwz = 0;
1045 }
1046 };
1047
1048 /* This data structure holds a complete die structure. */
1049 struct die_info
1050 {
1051 /* DWARF-2 tag for this DIE. */
1052 ENUM_BITFIELD(dwarf_tag) tag : 16;
1053
1054 /* Number of attributes */
1055 unsigned char num_attrs;
1056
1057 /* True if we're presently building the full type name for the
1058 type derived from this DIE. */
1059 unsigned char building_fullname : 1;
1060
1061 /* True if this die is in process. PR 16581. */
1062 unsigned char in_process : 1;
1063
1064 /* True if this DIE has children. */
1065 unsigned char has_children : 1;
1066
1067 /* Abbrev number */
1068 unsigned int abbrev;
1069
1070 /* Offset in .debug_info or .debug_types section. */
1071 sect_offset sect_off;
1072
1073 /* The dies in a compilation unit form an n-ary tree. PARENT
1074 points to this die's parent; CHILD points to the first child of
1075 this node; and all the children of a given node are chained
1076 together via their SIBLING fields. */
1077 struct die_info *child; /* Its first child, if any. */
1078 struct die_info *sibling; /* Its next sibling, if any. */
1079 struct die_info *parent; /* Its parent, if any. */
1080
1081 /* An array of attributes, with NUM_ATTRS elements. There may be
1082 zero, but it's not common and zero-sized arrays are not
1083 sufficiently portable C. */
1084 struct attribute attrs[1];
1085 };
1086
1087 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1088 but this would require a corresponding change in unpack_field_as_long
1089 and friends. */
1090 static int bits_per_byte = 8;
1091
1092 /* When reading a variant or variant part, we track a bit more
1093 information about the field, and store it in an object of this
1094 type. */
1095
1096 struct variant_field
1097 {
1098 /* If we see a DW_TAG_variant, then this will be the discriminant
1099 value. */
1100 ULONGEST discriminant_value;
1101 /* If we see a DW_TAG_variant, then this will be set if this is the
1102 default branch. */
1103 bool default_branch;
1104 /* While reading a DW_TAG_variant_part, this will be set if this
1105 field is the discriminant. */
1106 bool is_discriminant;
1107 };
1108
1109 struct nextfield
1110 {
1111 int accessibility = 0;
1112 int virtuality = 0;
1113 /* Extra information to describe a variant or variant part. */
1114 struct variant_field variant {};
1115 struct field field {};
1116 };
1117
1118 struct fnfieldlist
1119 {
1120 const char *name = nullptr;
1121 std::vector<struct fn_field> fnfields;
1122 };
1123
1124 /* The routines that read and process dies for a C struct or C++ class
1125 pass lists of data member fields and lists of member function fields
1126 in an instance of a field_info structure, as defined below. */
1127 struct field_info
1128 {
1129 /* List of data member and baseclasses fields. */
1130 std::vector<struct nextfield> fields;
1131 std::vector<struct nextfield> baseclasses;
1132
1133 /* Set if the accessibility of one of the fields is not public. */
1134 int non_public_fields = 0;
1135
1136 /* Member function fieldlist array, contains name of possibly overloaded
1137 member function, number of overloaded member functions and a pointer
1138 to the head of the member function field chain. */
1139 std::vector<struct fnfieldlist> fnfieldlists;
1140
1141 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1142 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1143 std::vector<struct decl_field> typedef_field_list;
1144
1145 /* Nested types defined by this class and the number of elements in this
1146 list. */
1147 std::vector<struct decl_field> nested_types_list;
1148
1149 /* Return the total number of fields (including baseclasses). */
1150 int nfields () const
1151 {
1152 return fields.size () + baseclasses.size ();
1153 }
1154 };
1155
1156 /* Loaded secondary compilation units are kept in memory until they
1157 have not been referenced for the processing of this many
1158 compilation units. Set this to zero to disable caching. Cache
1159 sizes of up to at least twenty will improve startup time for
1160 typical inter-CU-reference binaries, at an obvious memory cost. */
1161 static int dwarf_max_cache_age = 5;
1162 static void
1163 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1164 struct cmd_list_element *c, const char *value)
1165 {
1166 fprintf_filtered (file, _("The upper bound on the age of cached "
1167 "DWARF compilation units is %s.\n"),
1168 value);
1169 }
1170 \f
1171 /* local function prototypes */
1172
1173 static void dwarf2_find_base_address (struct die_info *die,
1174 struct dwarf2_cu *cu);
1175
1176 static dwarf2_psymtab *create_partial_symtab
1177 (struct dwarf2_per_cu_data *per_cu, const char *name);
1178
1179 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1180 const gdb_byte *info_ptr,
1181 struct die_info *type_unit_die);
1182
1183 static void dwarf2_build_psymtabs_hard
1184 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1185
1186 static void scan_partial_symbols (struct partial_die_info *,
1187 CORE_ADDR *, CORE_ADDR *,
1188 int, struct dwarf2_cu *);
1189
1190 static void add_partial_symbol (struct partial_die_info *,
1191 struct dwarf2_cu *);
1192
1193 static void add_partial_namespace (struct partial_die_info *pdi,
1194 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1195 int set_addrmap, struct dwarf2_cu *cu);
1196
1197 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1198 CORE_ADDR *highpc, int set_addrmap,
1199 struct dwarf2_cu *cu);
1200
1201 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1202 struct dwarf2_cu *cu);
1203
1204 static void add_partial_subprogram (struct partial_die_info *pdi,
1205 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1206 int need_pc, struct dwarf2_cu *cu);
1207
1208 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1209
1210 static struct partial_die_info *load_partial_dies
1211 (const struct die_reader_specs *, const gdb_byte *, int);
1212
1213 /* A pair of partial_die_info and compilation unit. */
1214 struct cu_partial_die_info
1215 {
1216 /* The compilation unit of the partial_die_info. */
1217 struct dwarf2_cu *cu;
1218 /* A partial_die_info. */
1219 struct partial_die_info *pdi;
1220
1221 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1222 : cu (cu),
1223 pdi (pdi)
1224 { /* Nothing. */ }
1225
1226 private:
1227 cu_partial_die_info () = delete;
1228 };
1229
1230 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1231 struct dwarf2_cu *);
1232
1233 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1234 struct attribute *, struct attr_abbrev *,
1235 const gdb_byte *, bool *need_reprocess);
1236
1237 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1238 struct attribute *attr);
1239
1240 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1241
1242 static LONGEST read_checked_initial_length_and_offset
1243 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1244 unsigned int *, unsigned int *);
1245
1246 static sect_offset read_abbrev_offset
1247 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1248 struct dwarf2_section_info *, sect_offset);
1249
1250 static const char *read_indirect_string
1251 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1252 const struct comp_unit_head *, unsigned int *);
1253
1254 static const char *read_indirect_line_string
1255 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1256 const struct comp_unit_head *, unsigned int *);
1257
1258 static const char *read_indirect_string_at_offset
1259 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1260 LONGEST str_offset);
1261
1262 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1263 const gdb_byte *,
1264 unsigned int *);
1265
1266 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1267 ULONGEST str_index);
1268
1269 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1270 ULONGEST str_index);
1271
1272 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1273
1274 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1275 struct dwarf2_cu *);
1276
1277 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1278 unsigned int);
1279
1280 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1281 struct dwarf2_cu *cu);
1282
1283 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1284
1285 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1286 struct dwarf2_cu *cu);
1287
1288 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1289
1290 static struct die_info *die_specification (struct die_info *die,
1291 struct dwarf2_cu **);
1292
1293 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1294 struct dwarf2_cu *cu);
1295
1296 static void dwarf_decode_lines (struct line_header *, const char *,
1297 struct dwarf2_cu *, dwarf2_psymtab *,
1298 CORE_ADDR, int decode_mapping);
1299
1300 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1301 const char *);
1302
1303 static struct symbol *new_symbol (struct die_info *, struct type *,
1304 struct dwarf2_cu *, struct symbol * = NULL);
1305
1306 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1307 struct dwarf2_cu *);
1308
1309 static void dwarf2_const_value_attr (const struct attribute *attr,
1310 struct type *type,
1311 const char *name,
1312 struct obstack *obstack,
1313 struct dwarf2_cu *cu, LONGEST *value,
1314 const gdb_byte **bytes,
1315 struct dwarf2_locexpr_baton **baton);
1316
1317 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1318
1319 static int need_gnat_info (struct dwarf2_cu *);
1320
1321 static struct type *die_descriptive_type (struct die_info *,
1322 struct dwarf2_cu *);
1323
1324 static void set_descriptive_type (struct type *, struct die_info *,
1325 struct dwarf2_cu *);
1326
1327 static struct type *die_containing_type (struct die_info *,
1328 struct dwarf2_cu *);
1329
1330 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1331 struct dwarf2_cu *);
1332
1333 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1334
1335 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1336
1337 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1338
1339 static char *typename_concat (struct obstack *obs, const char *prefix,
1340 const char *suffix, int physname,
1341 struct dwarf2_cu *cu);
1342
1343 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1344
1345 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1346
1347 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1348
1349 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1350
1351 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1352
1353 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1354
1355 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1356 struct dwarf2_cu *, dwarf2_psymtab *);
1357
1358 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1359 values. Keep the items ordered with increasing constraints compliance. */
1360 enum pc_bounds_kind
1361 {
1362 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1363 PC_BOUNDS_NOT_PRESENT,
1364
1365 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1366 were present but they do not form a valid range of PC addresses. */
1367 PC_BOUNDS_INVALID,
1368
1369 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1370 PC_BOUNDS_RANGES,
1371
1372 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1373 PC_BOUNDS_HIGH_LOW,
1374 };
1375
1376 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1377 CORE_ADDR *, CORE_ADDR *,
1378 struct dwarf2_cu *,
1379 dwarf2_psymtab *);
1380
1381 static void get_scope_pc_bounds (struct die_info *,
1382 CORE_ADDR *, CORE_ADDR *,
1383 struct dwarf2_cu *);
1384
1385 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1386 CORE_ADDR, struct dwarf2_cu *);
1387
1388 static void dwarf2_add_field (struct field_info *, struct die_info *,
1389 struct dwarf2_cu *);
1390
1391 static void dwarf2_attach_fields_to_type (struct field_info *,
1392 struct type *, struct dwarf2_cu *);
1393
1394 static void dwarf2_add_member_fn (struct field_info *,
1395 struct die_info *, struct type *,
1396 struct dwarf2_cu *);
1397
1398 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1399 struct type *,
1400 struct dwarf2_cu *);
1401
1402 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1403
1404 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1405
1406 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1407
1408 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1409
1410 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1411
1412 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1413
1414 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1415
1416 static struct type *read_module_type (struct die_info *die,
1417 struct dwarf2_cu *cu);
1418
1419 static const char *namespace_name (struct die_info *die,
1420 int *is_anonymous, struct dwarf2_cu *);
1421
1422 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1423
1424 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1425
1426 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1427 struct dwarf2_cu *);
1428
1429 static struct die_info *read_die_and_siblings_1
1430 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1431 struct die_info *);
1432
1433 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1434 const gdb_byte *info_ptr,
1435 const gdb_byte **new_info_ptr,
1436 struct die_info *parent);
1437
1438 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1439 struct die_info **, const gdb_byte *,
1440 int);
1441
1442 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1443 struct die_info **, const gdb_byte *);
1444
1445 static void process_die (struct die_info *, struct dwarf2_cu *);
1446
1447 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1448 struct objfile *);
1449
1450 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1451
1452 static const char *dwarf2_full_name (const char *name,
1453 struct die_info *die,
1454 struct dwarf2_cu *cu);
1455
1456 static const char *dwarf2_physname (const char *name, struct die_info *die,
1457 struct dwarf2_cu *cu);
1458
1459 static struct die_info *dwarf2_extension (struct die_info *die,
1460 struct dwarf2_cu **);
1461
1462 static const char *dwarf_tag_name (unsigned int);
1463
1464 static const char *dwarf_attr_name (unsigned int);
1465
1466 static const char *dwarf_form_name (unsigned int);
1467
1468 static const char *dwarf_bool_name (unsigned int);
1469
1470 static const char *dwarf_type_encoding_name (unsigned int);
1471
1472 static struct die_info *sibling_die (struct die_info *);
1473
1474 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1475
1476 static void dump_die_for_error (struct die_info *);
1477
1478 static void dump_die_1 (struct ui_file *, int level, int max_level,
1479 struct die_info *);
1480
1481 /*static*/ void dump_die (struct die_info *, int max_level);
1482
1483 static void store_in_ref_table (struct die_info *,
1484 struct dwarf2_cu *);
1485
1486 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1487
1488 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1489
1490 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1491 const struct attribute *,
1492 struct dwarf2_cu **);
1493
1494 static struct die_info *follow_die_ref (struct die_info *,
1495 const struct attribute *,
1496 struct dwarf2_cu **);
1497
1498 static struct die_info *follow_die_sig (struct die_info *,
1499 const struct attribute *,
1500 struct dwarf2_cu **);
1501
1502 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1503 struct dwarf2_cu *);
1504
1505 static struct type *get_DW_AT_signature_type (struct die_info *,
1506 const struct attribute *,
1507 struct dwarf2_cu *);
1508
1509 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1510
1511 static void read_signatured_type (struct signatured_type *);
1512
1513 static int attr_to_dynamic_prop (const struct attribute *attr,
1514 struct die_info *die, struct dwarf2_cu *cu,
1515 struct dynamic_prop *prop, struct type *type);
1516
1517 /* memory allocation interface */
1518
1519 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1520
1521 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1522
1523 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1524
1525 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1526 struct dwarf2_loclist_baton *baton,
1527 const struct attribute *attr);
1528
1529 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1530 struct symbol *sym,
1531 struct dwarf2_cu *cu,
1532 int is_block);
1533
1534 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1535 const gdb_byte *info_ptr,
1536 struct abbrev_info *abbrev);
1537
1538 static hashval_t partial_die_hash (const void *item);
1539
1540 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1541
1542 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1543 (sect_offset sect_off, unsigned int offset_in_dwz,
1544 struct dwarf2_per_objfile *dwarf2_per_objfile);
1545
1546 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1547 struct die_info *comp_unit_die,
1548 enum language pretend_language);
1549
1550 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1551
1552 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1553
1554 static struct type *set_die_type (struct die_info *, struct type *,
1555 struct dwarf2_cu *);
1556
1557 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1558
1559 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1560
1561 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1562 enum language);
1563
1564 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1565 enum language);
1566
1567 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1568 enum language);
1569
1570 static void dwarf2_add_dependence (struct dwarf2_cu *,
1571 struct dwarf2_per_cu_data *);
1572
1573 static void dwarf2_mark (struct dwarf2_cu *);
1574
1575 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1576
1577 static struct type *get_die_type_at_offset (sect_offset,
1578 struct dwarf2_per_cu_data *);
1579
1580 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1581
1582 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1583 enum language pretend_language);
1584
1585 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1586
1587 /* Class, the destructor of which frees all allocated queue entries. This
1588 will only have work to do if an error was thrown while processing the
1589 dwarf. If no error was thrown then the queue entries should have all
1590 been processed, and freed, as we went along. */
1591
1592 class dwarf2_queue_guard
1593 {
1594 public:
1595 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1596 : m_per_objfile (per_objfile)
1597 {
1598 }
1599
1600 /* Free any entries remaining on the queue. There should only be
1601 entries left if we hit an error while processing the dwarf. */
1602 ~dwarf2_queue_guard ()
1603 {
1604 /* Ensure that no memory is allocated by the queue. */
1605 std::queue<dwarf2_queue_item> empty;
1606 std::swap (m_per_objfile->queue, empty);
1607 }
1608
1609 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1610
1611 private:
1612 dwarf2_per_objfile *m_per_objfile;
1613 };
1614
1615 dwarf2_queue_item::~dwarf2_queue_item ()
1616 {
1617 /* Anything still marked queued is likely to be in an
1618 inconsistent state, so discard it. */
1619 if (per_cu->queued)
1620 {
1621 if (per_cu->cu != NULL)
1622 free_one_cached_comp_unit (per_cu);
1623 per_cu->queued = 0;
1624 }
1625 }
1626
1627 /* The return type of find_file_and_directory. Note, the enclosed
1628 string pointers are only valid while this object is valid. */
1629
1630 struct file_and_directory
1631 {
1632 /* The filename. This is never NULL. */
1633 const char *name;
1634
1635 /* The compilation directory. NULL if not known. If we needed to
1636 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1637 points directly to the DW_AT_comp_dir string attribute owned by
1638 the obstack that owns the DIE. */
1639 const char *comp_dir;
1640
1641 /* If we needed to build a new string for comp_dir, this is what
1642 owns the storage. */
1643 std::string comp_dir_storage;
1644 };
1645
1646 static file_and_directory find_file_and_directory (struct die_info *die,
1647 struct dwarf2_cu *cu);
1648
1649 static htab_up allocate_signatured_type_table ();
1650
1651 static htab_up allocate_dwo_unit_table ();
1652
1653 static struct dwo_unit *lookup_dwo_unit_in_dwp
1654 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1655 struct dwp_file *dwp_file, const char *comp_dir,
1656 ULONGEST signature, int is_debug_types);
1657
1658 static struct dwp_file *get_dwp_file
1659 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1660
1661 static struct dwo_unit *lookup_dwo_comp_unit
1662 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1663
1664 static struct dwo_unit *lookup_dwo_type_unit
1665 (struct signatured_type *, const char *, const char *);
1666
1667 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1668
1669 /* A unique pointer to a dwo_file. */
1670
1671 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1672
1673 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1674
1675 static void check_producer (struct dwarf2_cu *cu);
1676
1677 static void free_line_header_voidp (void *arg);
1678 \f
1679 /* Various complaints about symbol reading that don't abort the process. */
1680
1681 static void
1682 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1683 {
1684 complaint (_("statement list doesn't fit in .debug_line section"));
1685 }
1686
1687 static void
1688 dwarf2_debug_line_missing_file_complaint (void)
1689 {
1690 complaint (_(".debug_line section has line data without a file"));
1691 }
1692
1693 static void
1694 dwarf2_debug_line_missing_end_sequence_complaint (void)
1695 {
1696 complaint (_(".debug_line section has line "
1697 "program sequence without an end"));
1698 }
1699
1700 static void
1701 dwarf2_complex_location_expr_complaint (void)
1702 {
1703 complaint (_("location expression too complex"));
1704 }
1705
1706 static void
1707 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1708 int arg3)
1709 {
1710 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1711 arg1, arg2, arg3);
1712 }
1713
1714 static void
1715 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1716 {
1717 complaint (_("macro debug info contains a "
1718 "malformed macro definition:\n`%s'"),
1719 arg1);
1720 }
1721
1722 static void
1723 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1724 {
1725 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1726 arg1, arg2);
1727 }
1728
1729 /* Hash function for line_header_hash. */
1730
1731 static hashval_t
1732 line_header_hash (const struct line_header *ofs)
1733 {
1734 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1735 }
1736
1737 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1738
1739 static hashval_t
1740 line_header_hash_voidp (const void *item)
1741 {
1742 const struct line_header *ofs = (const struct line_header *) item;
1743
1744 return line_header_hash (ofs);
1745 }
1746
1747 /* Equality function for line_header_hash. */
1748
1749 static int
1750 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1751 {
1752 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1753 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1754
1755 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1756 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1757 }
1758
1759 \f
1760
1761 /* See declaration. */
1762
1763 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1764 const dwarf2_debug_sections *names,
1765 bool can_copy_)
1766 : objfile (objfile_),
1767 can_copy (can_copy_)
1768 {
1769 if (names == NULL)
1770 names = &dwarf2_elf_names;
1771
1772 bfd *obfd = objfile->obfd;
1773
1774 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1775 locate_sections (obfd, sec, *names);
1776 }
1777
1778 dwarf2_per_objfile::~dwarf2_per_objfile ()
1779 {
1780 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1781 free_cached_comp_units ();
1782
1783 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1784 per_cu->imported_symtabs_free ();
1785
1786 for (signatured_type *sig_type : all_type_units)
1787 sig_type->per_cu.imported_symtabs_free ();
1788
1789 /* Everything else should be on the objfile obstack. */
1790 }
1791
1792 /* See declaration. */
1793
1794 void
1795 dwarf2_per_objfile::free_cached_comp_units ()
1796 {
1797 dwarf2_per_cu_data *per_cu = read_in_chain;
1798 dwarf2_per_cu_data **last_chain = &read_in_chain;
1799 while (per_cu != NULL)
1800 {
1801 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1802
1803 delete per_cu->cu;
1804 *last_chain = next_cu;
1805 per_cu = next_cu;
1806 }
1807 }
1808
1809 /* A helper class that calls free_cached_comp_units on
1810 destruction. */
1811
1812 class free_cached_comp_units
1813 {
1814 public:
1815
1816 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1817 : m_per_objfile (per_objfile)
1818 {
1819 }
1820
1821 ~free_cached_comp_units ()
1822 {
1823 m_per_objfile->free_cached_comp_units ();
1824 }
1825
1826 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1827
1828 private:
1829
1830 dwarf2_per_objfile *m_per_objfile;
1831 };
1832
1833 /* Try to locate the sections we need for DWARF 2 debugging
1834 information and return true if we have enough to do something.
1835 NAMES points to the dwarf2 section names, or is NULL if the standard
1836 ELF names are used. CAN_COPY is true for formats where symbol
1837 interposition is possible and so symbol values must follow copy
1838 relocation rules. */
1839
1840 int
1841 dwarf2_has_info (struct objfile *objfile,
1842 const struct dwarf2_debug_sections *names,
1843 bool can_copy)
1844 {
1845 if (objfile->flags & OBJF_READNEVER)
1846 return 0;
1847
1848 struct dwarf2_per_objfile *dwarf2_per_objfile
1849 = get_dwarf2_per_objfile (objfile);
1850
1851 if (dwarf2_per_objfile == NULL)
1852 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1853 names,
1854 can_copy);
1855
1856 return (!dwarf2_per_objfile->info.is_virtual
1857 && dwarf2_per_objfile->info.s.section != NULL
1858 && !dwarf2_per_objfile->abbrev.is_virtual
1859 && dwarf2_per_objfile->abbrev.s.section != NULL);
1860 }
1861
1862 /* When loading sections, we look either for uncompressed section or for
1863 compressed section names. */
1864
1865 static int
1866 section_is_p (const char *section_name,
1867 const struct dwarf2_section_names *names)
1868 {
1869 if (names->normal != NULL
1870 && strcmp (section_name, names->normal) == 0)
1871 return 1;
1872 if (names->compressed != NULL
1873 && strcmp (section_name, names->compressed) == 0)
1874 return 1;
1875 return 0;
1876 }
1877
1878 /* See declaration. */
1879
1880 void
1881 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1882 const dwarf2_debug_sections &names)
1883 {
1884 flagword aflag = bfd_section_flags (sectp);
1885
1886 if ((aflag & SEC_HAS_CONTENTS) == 0)
1887 {
1888 }
1889 else if (elf_section_data (sectp)->this_hdr.sh_size
1890 > bfd_get_file_size (abfd))
1891 {
1892 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1893 warning (_("Discarding section %s which has a section size (%s"
1894 ") larger than the file size [in module %s]"),
1895 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1896 bfd_get_filename (abfd));
1897 }
1898 else if (section_is_p (sectp->name, &names.info))
1899 {
1900 this->info.s.section = sectp;
1901 this->info.size = bfd_section_size (sectp);
1902 }
1903 else if (section_is_p (sectp->name, &names.abbrev))
1904 {
1905 this->abbrev.s.section = sectp;
1906 this->abbrev.size = bfd_section_size (sectp);
1907 }
1908 else if (section_is_p (sectp->name, &names.line))
1909 {
1910 this->line.s.section = sectp;
1911 this->line.size = bfd_section_size (sectp);
1912 }
1913 else if (section_is_p (sectp->name, &names.loc))
1914 {
1915 this->loc.s.section = sectp;
1916 this->loc.size = bfd_section_size (sectp);
1917 }
1918 else if (section_is_p (sectp->name, &names.loclists))
1919 {
1920 this->loclists.s.section = sectp;
1921 this->loclists.size = bfd_section_size (sectp);
1922 }
1923 else if (section_is_p (sectp->name, &names.macinfo))
1924 {
1925 this->macinfo.s.section = sectp;
1926 this->macinfo.size = bfd_section_size (sectp);
1927 }
1928 else if (section_is_p (sectp->name, &names.macro))
1929 {
1930 this->macro.s.section = sectp;
1931 this->macro.size = bfd_section_size (sectp);
1932 }
1933 else if (section_is_p (sectp->name, &names.str))
1934 {
1935 this->str.s.section = sectp;
1936 this->str.size = bfd_section_size (sectp);
1937 }
1938 else if (section_is_p (sectp->name, &names.str_offsets))
1939 {
1940 this->str_offsets.s.section = sectp;
1941 this->str_offsets.size = bfd_section_size (sectp);
1942 }
1943 else if (section_is_p (sectp->name, &names.line_str))
1944 {
1945 this->line_str.s.section = sectp;
1946 this->line_str.size = bfd_section_size (sectp);
1947 }
1948 else if (section_is_p (sectp->name, &names.addr))
1949 {
1950 this->addr.s.section = sectp;
1951 this->addr.size = bfd_section_size (sectp);
1952 }
1953 else if (section_is_p (sectp->name, &names.frame))
1954 {
1955 this->frame.s.section = sectp;
1956 this->frame.size = bfd_section_size (sectp);
1957 }
1958 else if (section_is_p (sectp->name, &names.eh_frame))
1959 {
1960 this->eh_frame.s.section = sectp;
1961 this->eh_frame.size = bfd_section_size (sectp);
1962 }
1963 else if (section_is_p (sectp->name, &names.ranges))
1964 {
1965 this->ranges.s.section = sectp;
1966 this->ranges.size = bfd_section_size (sectp);
1967 }
1968 else if (section_is_p (sectp->name, &names.rnglists))
1969 {
1970 this->rnglists.s.section = sectp;
1971 this->rnglists.size = bfd_section_size (sectp);
1972 }
1973 else if (section_is_p (sectp->name, &names.types))
1974 {
1975 struct dwarf2_section_info type_section;
1976
1977 memset (&type_section, 0, sizeof (type_section));
1978 type_section.s.section = sectp;
1979 type_section.size = bfd_section_size (sectp);
1980
1981 this->types.push_back (type_section);
1982 }
1983 else if (section_is_p (sectp->name, &names.gdb_index))
1984 {
1985 this->gdb_index.s.section = sectp;
1986 this->gdb_index.size = bfd_section_size (sectp);
1987 }
1988 else if (section_is_p (sectp->name, &names.debug_names))
1989 {
1990 this->debug_names.s.section = sectp;
1991 this->debug_names.size = bfd_section_size (sectp);
1992 }
1993 else if (section_is_p (sectp->name, &names.debug_aranges))
1994 {
1995 this->debug_aranges.s.section = sectp;
1996 this->debug_aranges.size = bfd_section_size (sectp);
1997 }
1998
1999 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2000 && bfd_section_vma (sectp) == 0)
2001 this->has_section_at_zero = true;
2002 }
2003
2004 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2005 SECTION_NAME. */
2006
2007 void
2008 dwarf2_get_section_info (struct objfile *objfile,
2009 enum dwarf2_section_enum sect,
2010 asection **sectp, const gdb_byte **bufp,
2011 bfd_size_type *sizep)
2012 {
2013 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2014 struct dwarf2_section_info *info;
2015
2016 /* We may see an objfile without any DWARF, in which case we just
2017 return nothing. */
2018 if (data == NULL)
2019 {
2020 *sectp = NULL;
2021 *bufp = NULL;
2022 *sizep = 0;
2023 return;
2024 }
2025 switch (sect)
2026 {
2027 case DWARF2_DEBUG_FRAME:
2028 info = &data->frame;
2029 break;
2030 case DWARF2_EH_FRAME:
2031 info = &data->eh_frame;
2032 break;
2033 default:
2034 gdb_assert_not_reached ("unexpected section");
2035 }
2036
2037 info->read (objfile);
2038
2039 *sectp = info->get_bfd_section ();
2040 *bufp = info->buffer;
2041 *sizep = info->size;
2042 }
2043
2044 /* A helper function to find the sections for a .dwz file. */
2045
2046 static void
2047 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2048 {
2049 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2050
2051 /* Note that we only support the standard ELF names, because .dwz
2052 is ELF-only (at the time of writing). */
2053 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2054 {
2055 dwz_file->abbrev.s.section = sectp;
2056 dwz_file->abbrev.size = bfd_section_size (sectp);
2057 }
2058 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2059 {
2060 dwz_file->info.s.section = sectp;
2061 dwz_file->info.size = bfd_section_size (sectp);
2062 }
2063 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2064 {
2065 dwz_file->str.s.section = sectp;
2066 dwz_file->str.size = bfd_section_size (sectp);
2067 }
2068 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2069 {
2070 dwz_file->line.s.section = sectp;
2071 dwz_file->line.size = bfd_section_size (sectp);
2072 }
2073 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2074 {
2075 dwz_file->macro.s.section = sectp;
2076 dwz_file->macro.size = bfd_section_size (sectp);
2077 }
2078 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2079 {
2080 dwz_file->gdb_index.s.section = sectp;
2081 dwz_file->gdb_index.size = bfd_section_size (sectp);
2082 }
2083 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2084 {
2085 dwz_file->debug_names.s.section = sectp;
2086 dwz_file->debug_names.size = bfd_section_size (sectp);
2087 }
2088 }
2089
2090 /* See dwarf2read.h. */
2091
2092 struct dwz_file *
2093 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2094 {
2095 const char *filename;
2096 bfd_size_type buildid_len_arg;
2097 size_t buildid_len;
2098 bfd_byte *buildid;
2099
2100 if (dwarf2_per_objfile->dwz_file != NULL)
2101 return dwarf2_per_objfile->dwz_file.get ();
2102
2103 bfd_set_error (bfd_error_no_error);
2104 gdb::unique_xmalloc_ptr<char> data
2105 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2106 &buildid_len_arg, &buildid));
2107 if (data == NULL)
2108 {
2109 if (bfd_get_error () == bfd_error_no_error)
2110 return NULL;
2111 error (_("could not read '.gnu_debugaltlink' section: %s"),
2112 bfd_errmsg (bfd_get_error ()));
2113 }
2114
2115 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2116
2117 buildid_len = (size_t) buildid_len_arg;
2118
2119 filename = data.get ();
2120
2121 std::string abs_storage;
2122 if (!IS_ABSOLUTE_PATH (filename))
2123 {
2124 gdb::unique_xmalloc_ptr<char> abs
2125 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2126
2127 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2128 filename = abs_storage.c_str ();
2129 }
2130
2131 /* First try the file name given in the section. If that doesn't
2132 work, try to use the build-id instead. */
2133 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2134 if (dwz_bfd != NULL)
2135 {
2136 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2137 dwz_bfd.reset (nullptr);
2138 }
2139
2140 if (dwz_bfd == NULL)
2141 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2142
2143 if (dwz_bfd == nullptr)
2144 {
2145 gdb::unique_xmalloc_ptr<char> alt_filename;
2146 const char *origname = dwarf2_per_objfile->objfile->original_name;
2147
2148 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2149 buildid_len,
2150 origname,
2151 &alt_filename));
2152
2153 if (fd.get () >= 0)
2154 {
2155 /* File successfully retrieved from server. */
2156 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2157
2158 if (dwz_bfd == nullptr)
2159 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2160 alt_filename.get ());
2161 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2162 dwz_bfd.reset (nullptr);
2163 }
2164 }
2165
2166 if (dwz_bfd == NULL)
2167 error (_("could not find '.gnu_debugaltlink' file for %s"),
2168 objfile_name (dwarf2_per_objfile->objfile));
2169
2170 std::unique_ptr<struct dwz_file> result
2171 (new struct dwz_file (std::move (dwz_bfd)));
2172
2173 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2174 result.get ());
2175
2176 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2177 result->dwz_bfd.get ());
2178 dwarf2_per_objfile->dwz_file = std::move (result);
2179 return dwarf2_per_objfile->dwz_file.get ();
2180 }
2181 \f
2182 /* DWARF quick_symbols_functions support. */
2183
2184 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2185 unique line tables, so we maintain a separate table of all .debug_line
2186 derived entries to support the sharing.
2187 All the quick functions need is the list of file names. We discard the
2188 line_header when we're done and don't need to record it here. */
2189 struct quick_file_names
2190 {
2191 /* The data used to construct the hash key. */
2192 struct stmt_list_hash hash;
2193
2194 /* The number of entries in file_names, real_names. */
2195 unsigned int num_file_names;
2196
2197 /* The file names from the line table, after being run through
2198 file_full_name. */
2199 const char **file_names;
2200
2201 /* The file names from the line table after being run through
2202 gdb_realpath. These are computed lazily. */
2203 const char **real_names;
2204 };
2205
2206 /* When using the index (and thus not using psymtabs), each CU has an
2207 object of this type. This is used to hold information needed by
2208 the various "quick" methods. */
2209 struct dwarf2_per_cu_quick_data
2210 {
2211 /* The file table. This can be NULL if there was no file table
2212 or it's currently not read in.
2213 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2214 struct quick_file_names *file_names;
2215
2216 /* The corresponding symbol table. This is NULL if symbols for this
2217 CU have not yet been read. */
2218 struct compunit_symtab *compunit_symtab;
2219
2220 /* A temporary mark bit used when iterating over all CUs in
2221 expand_symtabs_matching. */
2222 unsigned int mark : 1;
2223
2224 /* True if we've tried to read the file table and found there isn't one.
2225 There will be no point in trying to read it again next time. */
2226 unsigned int no_file_data : 1;
2227 };
2228
2229 /* Utility hash function for a stmt_list_hash. */
2230
2231 static hashval_t
2232 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2233 {
2234 hashval_t v = 0;
2235
2236 if (stmt_list_hash->dwo_unit != NULL)
2237 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2238 v += to_underlying (stmt_list_hash->line_sect_off);
2239 return v;
2240 }
2241
2242 /* Utility equality function for a stmt_list_hash. */
2243
2244 static int
2245 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2246 const struct stmt_list_hash *rhs)
2247 {
2248 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2249 return 0;
2250 if (lhs->dwo_unit != NULL
2251 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2252 return 0;
2253
2254 return lhs->line_sect_off == rhs->line_sect_off;
2255 }
2256
2257 /* Hash function for a quick_file_names. */
2258
2259 static hashval_t
2260 hash_file_name_entry (const void *e)
2261 {
2262 const struct quick_file_names *file_data
2263 = (const struct quick_file_names *) e;
2264
2265 return hash_stmt_list_entry (&file_data->hash);
2266 }
2267
2268 /* Equality function for a quick_file_names. */
2269
2270 static int
2271 eq_file_name_entry (const void *a, const void *b)
2272 {
2273 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2274 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2275
2276 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2277 }
2278
2279 /* Delete function for a quick_file_names. */
2280
2281 static void
2282 delete_file_name_entry (void *e)
2283 {
2284 struct quick_file_names *file_data = (struct quick_file_names *) e;
2285 int i;
2286
2287 for (i = 0; i < file_data->num_file_names; ++i)
2288 {
2289 xfree ((void*) file_data->file_names[i]);
2290 if (file_data->real_names)
2291 xfree ((void*) file_data->real_names[i]);
2292 }
2293
2294 /* The space for the struct itself lives on objfile_obstack,
2295 so we don't free it here. */
2296 }
2297
2298 /* Create a quick_file_names hash table. */
2299
2300 static htab_up
2301 create_quick_file_names_table (unsigned int nr_initial_entries)
2302 {
2303 return htab_up (htab_create_alloc (nr_initial_entries,
2304 hash_file_name_entry, eq_file_name_entry,
2305 delete_file_name_entry, xcalloc, xfree));
2306 }
2307
2308 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2309 have to be created afterwards. You should call age_cached_comp_units after
2310 processing PER_CU->CU. dw2_setup must have been already called. */
2311
2312 static void
2313 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2314 {
2315 if (per_cu->is_debug_types)
2316 load_full_type_unit (per_cu);
2317 else
2318 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2319
2320 if (per_cu->cu == NULL)
2321 return; /* Dummy CU. */
2322
2323 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2324 }
2325
2326 /* Read in the symbols for PER_CU. */
2327
2328 static void
2329 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2330 {
2331 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2332
2333 /* Skip type_unit_groups, reading the type units they contain
2334 is handled elsewhere. */
2335 if (per_cu->type_unit_group_p ())
2336 return;
2337
2338 /* The destructor of dwarf2_queue_guard frees any entries left on
2339 the queue. After this point we're guaranteed to leave this function
2340 with the dwarf queue empty. */
2341 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2342
2343 if (dwarf2_per_objfile->using_index
2344 ? per_cu->v.quick->compunit_symtab == NULL
2345 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2346 {
2347 queue_comp_unit (per_cu, language_minimal);
2348 load_cu (per_cu, skip_partial);
2349
2350 /* If we just loaded a CU from a DWO, and we're working with an index
2351 that may badly handle TUs, load all the TUs in that DWO as well.
2352 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2353 if (!per_cu->is_debug_types
2354 && per_cu->cu != NULL
2355 && per_cu->cu->dwo_unit != NULL
2356 && dwarf2_per_objfile->index_table != NULL
2357 && dwarf2_per_objfile->index_table->version <= 7
2358 /* DWP files aren't supported yet. */
2359 && get_dwp_file (dwarf2_per_objfile) == NULL)
2360 queue_and_load_all_dwo_tus (per_cu);
2361 }
2362
2363 process_queue (dwarf2_per_objfile);
2364
2365 /* Age the cache, releasing compilation units that have not
2366 been used recently. */
2367 age_cached_comp_units (dwarf2_per_objfile);
2368 }
2369
2370 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2371 the objfile from which this CU came. Returns the resulting symbol
2372 table. */
2373
2374 static struct compunit_symtab *
2375 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2376 {
2377 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2378
2379 gdb_assert (dwarf2_per_objfile->using_index);
2380 if (!per_cu->v.quick->compunit_symtab)
2381 {
2382 free_cached_comp_units freer (dwarf2_per_objfile);
2383 scoped_restore decrementer = increment_reading_symtab ();
2384 dw2_do_instantiate_symtab (per_cu, skip_partial);
2385 process_cu_includes (dwarf2_per_objfile);
2386 }
2387
2388 return per_cu->v.quick->compunit_symtab;
2389 }
2390
2391 /* See declaration. */
2392
2393 dwarf2_per_cu_data *
2394 dwarf2_per_objfile::get_cutu (int index)
2395 {
2396 if (index >= this->all_comp_units.size ())
2397 {
2398 index -= this->all_comp_units.size ();
2399 gdb_assert (index < this->all_type_units.size ());
2400 return &this->all_type_units[index]->per_cu;
2401 }
2402
2403 return this->all_comp_units[index];
2404 }
2405
2406 /* See declaration. */
2407
2408 dwarf2_per_cu_data *
2409 dwarf2_per_objfile::get_cu (int index)
2410 {
2411 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2412
2413 return this->all_comp_units[index];
2414 }
2415
2416 /* See declaration. */
2417
2418 signatured_type *
2419 dwarf2_per_objfile::get_tu (int index)
2420 {
2421 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2422
2423 return this->all_type_units[index];
2424 }
2425
2426 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2427 objfile_obstack, and constructed with the specified field
2428 values. */
2429
2430 static dwarf2_per_cu_data *
2431 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2432 struct dwarf2_section_info *section,
2433 int is_dwz,
2434 sect_offset sect_off, ULONGEST length)
2435 {
2436 struct objfile *objfile = dwarf2_per_objfile->objfile;
2437 dwarf2_per_cu_data *the_cu
2438 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2439 struct dwarf2_per_cu_data);
2440 the_cu->sect_off = sect_off;
2441 the_cu->length = length;
2442 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2443 the_cu->section = section;
2444 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2445 struct dwarf2_per_cu_quick_data);
2446 the_cu->is_dwz = is_dwz;
2447 return the_cu;
2448 }
2449
2450 /* A helper for create_cus_from_index that handles a given list of
2451 CUs. */
2452
2453 static void
2454 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2455 const gdb_byte *cu_list, offset_type n_elements,
2456 struct dwarf2_section_info *section,
2457 int is_dwz)
2458 {
2459 for (offset_type i = 0; i < n_elements; i += 2)
2460 {
2461 gdb_static_assert (sizeof (ULONGEST) >= 8);
2462
2463 sect_offset sect_off
2464 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2465 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2466 cu_list += 2 * 8;
2467
2468 dwarf2_per_cu_data *per_cu
2469 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2470 sect_off, length);
2471 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2472 }
2473 }
2474
2475 /* Read the CU list from the mapped index, and use it to create all
2476 the CU objects for this objfile. */
2477
2478 static void
2479 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2480 const gdb_byte *cu_list, offset_type cu_list_elements,
2481 const gdb_byte *dwz_list, offset_type dwz_elements)
2482 {
2483 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2484 dwarf2_per_objfile->all_comp_units.reserve
2485 ((cu_list_elements + dwz_elements) / 2);
2486
2487 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2488 &dwarf2_per_objfile->info, 0);
2489
2490 if (dwz_elements == 0)
2491 return;
2492
2493 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2494 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2495 &dwz->info, 1);
2496 }
2497
2498 /* Create the signatured type hash table from the index. */
2499
2500 static void
2501 create_signatured_type_table_from_index
2502 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2503 struct dwarf2_section_info *section,
2504 const gdb_byte *bytes,
2505 offset_type elements)
2506 {
2507 struct objfile *objfile = dwarf2_per_objfile->objfile;
2508
2509 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2510 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2511
2512 htab_up sig_types_hash = allocate_signatured_type_table ();
2513
2514 for (offset_type i = 0; i < elements; i += 3)
2515 {
2516 struct signatured_type *sig_type;
2517 ULONGEST signature;
2518 void **slot;
2519 cu_offset type_offset_in_tu;
2520
2521 gdb_static_assert (sizeof (ULONGEST) >= 8);
2522 sect_offset sect_off
2523 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2524 type_offset_in_tu
2525 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2526 BFD_ENDIAN_LITTLE);
2527 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2528 bytes += 3 * 8;
2529
2530 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2531 struct signatured_type);
2532 sig_type->signature = signature;
2533 sig_type->type_offset_in_tu = type_offset_in_tu;
2534 sig_type->per_cu.is_debug_types = 1;
2535 sig_type->per_cu.section = section;
2536 sig_type->per_cu.sect_off = sect_off;
2537 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2538 sig_type->per_cu.v.quick
2539 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2540 struct dwarf2_per_cu_quick_data);
2541
2542 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2543 *slot = sig_type;
2544
2545 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2546 }
2547
2548 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2549 }
2550
2551 /* Create the signatured type hash table from .debug_names. */
2552
2553 static void
2554 create_signatured_type_table_from_debug_names
2555 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2556 const mapped_debug_names &map,
2557 struct dwarf2_section_info *section,
2558 struct dwarf2_section_info *abbrev_section)
2559 {
2560 struct objfile *objfile = dwarf2_per_objfile->objfile;
2561
2562 section->read (objfile);
2563 abbrev_section->read (objfile);
2564
2565 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2566 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2567
2568 htab_up sig_types_hash = allocate_signatured_type_table ();
2569
2570 for (uint32_t i = 0; i < map.tu_count; ++i)
2571 {
2572 struct signatured_type *sig_type;
2573 void **slot;
2574
2575 sect_offset sect_off
2576 = (sect_offset) (extract_unsigned_integer
2577 (map.tu_table_reordered + i * map.offset_size,
2578 map.offset_size,
2579 map.dwarf5_byte_order));
2580
2581 comp_unit_head cu_header;
2582 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2583 abbrev_section,
2584 section->buffer + to_underlying (sect_off),
2585 rcuh_kind::TYPE);
2586
2587 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2588 struct signatured_type);
2589 sig_type->signature = cu_header.signature;
2590 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2591 sig_type->per_cu.is_debug_types = 1;
2592 sig_type->per_cu.section = section;
2593 sig_type->per_cu.sect_off = sect_off;
2594 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2595 sig_type->per_cu.v.quick
2596 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2597 struct dwarf2_per_cu_quick_data);
2598
2599 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2600 *slot = sig_type;
2601
2602 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2603 }
2604
2605 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2606 }
2607
2608 /* Read the address map data from the mapped index, and use it to
2609 populate the objfile's psymtabs_addrmap. */
2610
2611 static void
2612 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2613 struct mapped_index *index)
2614 {
2615 struct objfile *objfile = dwarf2_per_objfile->objfile;
2616 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2617 const gdb_byte *iter, *end;
2618 struct addrmap *mutable_map;
2619 CORE_ADDR baseaddr;
2620
2621 auto_obstack temp_obstack;
2622
2623 mutable_map = addrmap_create_mutable (&temp_obstack);
2624
2625 iter = index->address_table.data ();
2626 end = iter + index->address_table.size ();
2627
2628 baseaddr = objfile->text_section_offset ();
2629
2630 while (iter < end)
2631 {
2632 ULONGEST hi, lo, cu_index;
2633 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2634 iter += 8;
2635 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2636 iter += 8;
2637 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2638 iter += 4;
2639
2640 if (lo > hi)
2641 {
2642 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2643 hex_string (lo), hex_string (hi));
2644 continue;
2645 }
2646
2647 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2648 {
2649 complaint (_(".gdb_index address table has invalid CU number %u"),
2650 (unsigned) cu_index);
2651 continue;
2652 }
2653
2654 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2655 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2656 addrmap_set_empty (mutable_map, lo, hi - 1,
2657 dwarf2_per_objfile->get_cu (cu_index));
2658 }
2659
2660 objfile->partial_symtabs->psymtabs_addrmap
2661 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2662 }
2663
2664 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2665 populate the objfile's psymtabs_addrmap. */
2666
2667 static void
2668 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2669 struct dwarf2_section_info *section)
2670 {
2671 struct objfile *objfile = dwarf2_per_objfile->objfile;
2672 bfd *abfd = objfile->obfd;
2673 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2674 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2675
2676 auto_obstack temp_obstack;
2677 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2678
2679 std::unordered_map<sect_offset,
2680 dwarf2_per_cu_data *,
2681 gdb::hash_enum<sect_offset>>
2682 debug_info_offset_to_per_cu;
2683 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2684 {
2685 const auto insertpair
2686 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2687 if (!insertpair.second)
2688 {
2689 warning (_("Section .debug_aranges in %s has duplicate "
2690 "debug_info_offset %s, ignoring .debug_aranges."),
2691 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2692 return;
2693 }
2694 }
2695
2696 section->read (objfile);
2697
2698 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2699
2700 const gdb_byte *addr = section->buffer;
2701
2702 while (addr < section->buffer + section->size)
2703 {
2704 const gdb_byte *const entry_addr = addr;
2705 unsigned int bytes_read;
2706
2707 const LONGEST entry_length = read_initial_length (abfd, addr,
2708 &bytes_read);
2709 addr += bytes_read;
2710
2711 const gdb_byte *const entry_end = addr + entry_length;
2712 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2713 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2714 if (addr + entry_length > section->buffer + section->size)
2715 {
2716 warning (_("Section .debug_aranges in %s entry at offset %s "
2717 "length %s exceeds section length %s, "
2718 "ignoring .debug_aranges."),
2719 objfile_name (objfile),
2720 plongest (entry_addr - section->buffer),
2721 plongest (bytes_read + entry_length),
2722 pulongest (section->size));
2723 return;
2724 }
2725
2726 /* The version number. */
2727 const uint16_t version = read_2_bytes (abfd, addr);
2728 addr += 2;
2729 if (version != 2)
2730 {
2731 warning (_("Section .debug_aranges in %s entry at offset %s "
2732 "has unsupported version %d, ignoring .debug_aranges."),
2733 objfile_name (objfile),
2734 plongest (entry_addr - section->buffer), version);
2735 return;
2736 }
2737
2738 const uint64_t debug_info_offset
2739 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2740 addr += offset_size;
2741 const auto per_cu_it
2742 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2743 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2744 {
2745 warning (_("Section .debug_aranges in %s entry at offset %s "
2746 "debug_info_offset %s does not exists, "
2747 "ignoring .debug_aranges."),
2748 objfile_name (objfile),
2749 plongest (entry_addr - section->buffer),
2750 pulongest (debug_info_offset));
2751 return;
2752 }
2753 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2754
2755 const uint8_t address_size = *addr++;
2756 if (address_size < 1 || address_size > 8)
2757 {
2758 warning (_("Section .debug_aranges in %s entry at offset %s "
2759 "address_size %u is invalid, ignoring .debug_aranges."),
2760 objfile_name (objfile),
2761 plongest (entry_addr - section->buffer), address_size);
2762 return;
2763 }
2764
2765 const uint8_t segment_selector_size = *addr++;
2766 if (segment_selector_size != 0)
2767 {
2768 warning (_("Section .debug_aranges in %s entry at offset %s "
2769 "segment_selector_size %u is not supported, "
2770 "ignoring .debug_aranges."),
2771 objfile_name (objfile),
2772 plongest (entry_addr - section->buffer),
2773 segment_selector_size);
2774 return;
2775 }
2776
2777 /* Must pad to an alignment boundary that is twice the address
2778 size. It is undocumented by the DWARF standard but GCC does
2779 use it. */
2780 for (size_t padding = ((-(addr - section->buffer))
2781 & (2 * address_size - 1));
2782 padding > 0; padding--)
2783 if (*addr++ != 0)
2784 {
2785 warning (_("Section .debug_aranges in %s entry at offset %s "
2786 "padding is not zero, ignoring .debug_aranges."),
2787 objfile_name (objfile),
2788 plongest (entry_addr - section->buffer));
2789 return;
2790 }
2791
2792 for (;;)
2793 {
2794 if (addr + 2 * address_size > entry_end)
2795 {
2796 warning (_("Section .debug_aranges in %s entry at offset %s "
2797 "address list is not properly terminated, "
2798 "ignoring .debug_aranges."),
2799 objfile_name (objfile),
2800 plongest (entry_addr - section->buffer));
2801 return;
2802 }
2803 ULONGEST start = extract_unsigned_integer (addr, address_size,
2804 dwarf5_byte_order);
2805 addr += address_size;
2806 ULONGEST length = extract_unsigned_integer (addr, address_size,
2807 dwarf5_byte_order);
2808 addr += address_size;
2809 if (start == 0 && length == 0)
2810 break;
2811 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2812 {
2813 /* Symbol was eliminated due to a COMDAT group. */
2814 continue;
2815 }
2816 ULONGEST end = start + length;
2817 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2818 - baseaddr);
2819 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2820 - baseaddr);
2821 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2822 }
2823 }
2824
2825 objfile->partial_symtabs->psymtabs_addrmap
2826 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2827 }
2828
2829 /* Find a slot in the mapped index INDEX for the object named NAME.
2830 If NAME is found, set *VEC_OUT to point to the CU vector in the
2831 constant pool and return true. If NAME cannot be found, return
2832 false. */
2833
2834 static bool
2835 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2836 offset_type **vec_out)
2837 {
2838 offset_type hash;
2839 offset_type slot, step;
2840 int (*cmp) (const char *, const char *);
2841
2842 gdb::unique_xmalloc_ptr<char> without_params;
2843 if (current_language->la_language == language_cplus
2844 || current_language->la_language == language_fortran
2845 || current_language->la_language == language_d)
2846 {
2847 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2848 not contain any. */
2849
2850 if (strchr (name, '(') != NULL)
2851 {
2852 without_params = cp_remove_params (name);
2853
2854 if (without_params != NULL)
2855 name = without_params.get ();
2856 }
2857 }
2858
2859 /* Index version 4 did not support case insensitive searches. But the
2860 indices for case insensitive languages are built in lowercase, therefore
2861 simulate our NAME being searched is also lowercased. */
2862 hash = mapped_index_string_hash ((index->version == 4
2863 && case_sensitivity == case_sensitive_off
2864 ? 5 : index->version),
2865 name);
2866
2867 slot = hash & (index->symbol_table.size () - 1);
2868 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2869 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2870
2871 for (;;)
2872 {
2873 const char *str;
2874
2875 const auto &bucket = index->symbol_table[slot];
2876 if (bucket.name == 0 && bucket.vec == 0)
2877 return false;
2878
2879 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2880 if (!cmp (name, str))
2881 {
2882 *vec_out = (offset_type *) (index->constant_pool
2883 + MAYBE_SWAP (bucket.vec));
2884 return true;
2885 }
2886
2887 slot = (slot + step) & (index->symbol_table.size () - 1);
2888 }
2889 }
2890
2891 /* A helper function that reads the .gdb_index from BUFFER and fills
2892 in MAP. FILENAME is the name of the file containing the data;
2893 it is used for error reporting. DEPRECATED_OK is true if it is
2894 ok to use deprecated sections.
2895
2896 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2897 out parameters that are filled in with information about the CU and
2898 TU lists in the section.
2899
2900 Returns true if all went well, false otherwise. */
2901
2902 static bool
2903 read_gdb_index_from_buffer (struct objfile *objfile,
2904 const char *filename,
2905 bool deprecated_ok,
2906 gdb::array_view<const gdb_byte> buffer,
2907 struct mapped_index *map,
2908 const gdb_byte **cu_list,
2909 offset_type *cu_list_elements,
2910 const gdb_byte **types_list,
2911 offset_type *types_list_elements)
2912 {
2913 const gdb_byte *addr = &buffer[0];
2914
2915 /* Version check. */
2916 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2917 /* Versions earlier than 3 emitted every copy of a psymbol. This
2918 causes the index to behave very poorly for certain requests. Version 3
2919 contained incomplete addrmap. So, it seems better to just ignore such
2920 indices. */
2921 if (version < 4)
2922 {
2923 static int warning_printed = 0;
2924 if (!warning_printed)
2925 {
2926 warning (_("Skipping obsolete .gdb_index section in %s."),
2927 filename);
2928 warning_printed = 1;
2929 }
2930 return 0;
2931 }
2932 /* Index version 4 uses a different hash function than index version
2933 5 and later.
2934
2935 Versions earlier than 6 did not emit psymbols for inlined
2936 functions. Using these files will cause GDB not to be able to
2937 set breakpoints on inlined functions by name, so we ignore these
2938 indices unless the user has done
2939 "set use-deprecated-index-sections on". */
2940 if (version < 6 && !deprecated_ok)
2941 {
2942 static int warning_printed = 0;
2943 if (!warning_printed)
2944 {
2945 warning (_("\
2946 Skipping deprecated .gdb_index section in %s.\n\
2947 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2948 to use the section anyway."),
2949 filename);
2950 warning_printed = 1;
2951 }
2952 return 0;
2953 }
2954 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2955 of the TU (for symbols coming from TUs),
2956 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2957 Plus gold-generated indices can have duplicate entries for global symbols,
2958 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2959 These are just performance bugs, and we can't distinguish gdb-generated
2960 indices from gold-generated ones, so issue no warning here. */
2961
2962 /* Indexes with higher version than the one supported by GDB may be no
2963 longer backward compatible. */
2964 if (version > 8)
2965 return 0;
2966
2967 map->version = version;
2968
2969 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2970
2971 int i = 0;
2972 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2973 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2974 / 8);
2975 ++i;
2976
2977 *types_list = addr + MAYBE_SWAP (metadata[i]);
2978 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2979 - MAYBE_SWAP (metadata[i]))
2980 / 8);
2981 ++i;
2982
2983 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2984 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2985 map->address_table
2986 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2987 ++i;
2988
2989 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2990 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2991 map->symbol_table
2992 = gdb::array_view<mapped_index::symbol_table_slot>
2993 ((mapped_index::symbol_table_slot *) symbol_table,
2994 (mapped_index::symbol_table_slot *) symbol_table_end);
2995
2996 ++i;
2997 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2998
2999 return 1;
3000 }
3001
3002 /* Callback types for dwarf2_read_gdb_index. */
3003
3004 typedef gdb::function_view
3005 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3006 get_gdb_index_contents_ftype;
3007 typedef gdb::function_view
3008 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3009 get_gdb_index_contents_dwz_ftype;
3010
3011 /* Read .gdb_index. If everything went ok, initialize the "quick"
3012 elements of all the CUs and return 1. Otherwise, return 0. */
3013
3014 static int
3015 dwarf2_read_gdb_index
3016 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3017 get_gdb_index_contents_ftype get_gdb_index_contents,
3018 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3019 {
3020 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3021 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3022 struct dwz_file *dwz;
3023 struct objfile *objfile = dwarf2_per_objfile->objfile;
3024
3025 gdb::array_view<const gdb_byte> main_index_contents
3026 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3027
3028 if (main_index_contents.empty ())
3029 return 0;
3030
3031 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3032 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3033 use_deprecated_index_sections,
3034 main_index_contents, map.get (), &cu_list,
3035 &cu_list_elements, &types_list,
3036 &types_list_elements))
3037 return 0;
3038
3039 /* Don't use the index if it's empty. */
3040 if (map->symbol_table.empty ())
3041 return 0;
3042
3043 /* If there is a .dwz file, read it so we can get its CU list as
3044 well. */
3045 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3046 if (dwz != NULL)
3047 {
3048 struct mapped_index dwz_map;
3049 const gdb_byte *dwz_types_ignore;
3050 offset_type dwz_types_elements_ignore;
3051
3052 gdb::array_view<const gdb_byte> dwz_index_content
3053 = get_gdb_index_contents_dwz (objfile, dwz);
3054
3055 if (dwz_index_content.empty ())
3056 return 0;
3057
3058 if (!read_gdb_index_from_buffer (objfile,
3059 bfd_get_filename (dwz->dwz_bfd.get ()),
3060 1, dwz_index_content, &dwz_map,
3061 &dwz_list, &dwz_list_elements,
3062 &dwz_types_ignore,
3063 &dwz_types_elements_ignore))
3064 {
3065 warning (_("could not read '.gdb_index' section from %s; skipping"),
3066 bfd_get_filename (dwz->dwz_bfd.get ()));
3067 return 0;
3068 }
3069 }
3070
3071 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3072 dwz_list, dwz_list_elements);
3073
3074 if (types_list_elements)
3075 {
3076 /* We can only handle a single .debug_types when we have an
3077 index. */
3078 if (dwarf2_per_objfile->types.size () != 1)
3079 return 0;
3080
3081 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3082
3083 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3084 types_list, types_list_elements);
3085 }
3086
3087 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3088
3089 dwarf2_per_objfile->index_table = std::move (map);
3090 dwarf2_per_objfile->using_index = 1;
3091 dwarf2_per_objfile->quick_file_names_table =
3092 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3093
3094 return 1;
3095 }
3096
3097 /* die_reader_func for dw2_get_file_names. */
3098
3099 static void
3100 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3101 const gdb_byte *info_ptr,
3102 struct die_info *comp_unit_die)
3103 {
3104 struct dwarf2_cu *cu = reader->cu;
3105 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3106 struct dwarf2_per_objfile *dwarf2_per_objfile
3107 = cu->per_cu->dwarf2_per_objfile;
3108 struct objfile *objfile = dwarf2_per_objfile->objfile;
3109 struct dwarf2_per_cu_data *lh_cu;
3110 struct attribute *attr;
3111 void **slot;
3112 struct quick_file_names *qfn;
3113
3114 gdb_assert (! this_cu->is_debug_types);
3115
3116 /* Our callers never want to match partial units -- instead they
3117 will match the enclosing full CU. */
3118 if (comp_unit_die->tag == DW_TAG_partial_unit)
3119 {
3120 this_cu->v.quick->no_file_data = 1;
3121 return;
3122 }
3123
3124 lh_cu = this_cu;
3125 slot = NULL;
3126
3127 line_header_up lh;
3128 sect_offset line_offset {};
3129
3130 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3131 if (attr != nullptr)
3132 {
3133 struct quick_file_names find_entry;
3134
3135 line_offset = (sect_offset) DW_UNSND (attr);
3136
3137 /* We may have already read in this line header (TU line header sharing).
3138 If we have we're done. */
3139 find_entry.hash.dwo_unit = cu->dwo_unit;
3140 find_entry.hash.line_sect_off = line_offset;
3141 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3142 &find_entry, INSERT);
3143 if (*slot != NULL)
3144 {
3145 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3146 return;
3147 }
3148
3149 lh = dwarf_decode_line_header (line_offset, cu);
3150 }
3151 if (lh == NULL)
3152 {
3153 lh_cu->v.quick->no_file_data = 1;
3154 return;
3155 }
3156
3157 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3158 qfn->hash.dwo_unit = cu->dwo_unit;
3159 qfn->hash.line_sect_off = line_offset;
3160 gdb_assert (slot != NULL);
3161 *slot = qfn;
3162
3163 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3164
3165 int offset = 0;
3166 if (strcmp (fnd.name, "<unknown>") != 0)
3167 ++offset;
3168
3169 qfn->num_file_names = offset + lh->file_names_size ();
3170 qfn->file_names =
3171 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3172 if (offset != 0)
3173 qfn->file_names[0] = xstrdup (fnd.name);
3174 for (int i = 0; i < lh->file_names_size (); ++i)
3175 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3176 fnd.comp_dir).release ();
3177 qfn->real_names = NULL;
3178
3179 lh_cu->v.quick->file_names = qfn;
3180 }
3181
3182 /* A helper for the "quick" functions which attempts to read the line
3183 table for THIS_CU. */
3184
3185 static struct quick_file_names *
3186 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3187 {
3188 /* This should never be called for TUs. */
3189 gdb_assert (! this_cu->is_debug_types);
3190 /* Nor type unit groups. */
3191 gdb_assert (! this_cu->type_unit_group_p ());
3192
3193 if (this_cu->v.quick->file_names != NULL)
3194 return this_cu->v.quick->file_names;
3195 /* If we know there is no line data, no point in looking again. */
3196 if (this_cu->v.quick->no_file_data)
3197 return NULL;
3198
3199 cutu_reader reader (this_cu);
3200 if (!reader.dummy_p)
3201 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3202
3203 if (this_cu->v.quick->no_file_data)
3204 return NULL;
3205 return this_cu->v.quick->file_names;
3206 }
3207
3208 /* A helper for the "quick" functions which computes and caches the
3209 real path for a given file name from the line table. */
3210
3211 static const char *
3212 dw2_get_real_path (struct objfile *objfile,
3213 struct quick_file_names *qfn, int index)
3214 {
3215 if (qfn->real_names == NULL)
3216 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3217 qfn->num_file_names, const char *);
3218
3219 if (qfn->real_names[index] == NULL)
3220 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3221
3222 return qfn->real_names[index];
3223 }
3224
3225 static struct symtab *
3226 dw2_find_last_source_symtab (struct objfile *objfile)
3227 {
3228 struct dwarf2_per_objfile *dwarf2_per_objfile
3229 = get_dwarf2_per_objfile (objfile);
3230 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3231 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3232
3233 if (cust == NULL)
3234 return NULL;
3235
3236 return compunit_primary_filetab (cust);
3237 }
3238
3239 /* Traversal function for dw2_forget_cached_source_info. */
3240
3241 static int
3242 dw2_free_cached_file_names (void **slot, void *info)
3243 {
3244 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3245
3246 if (file_data->real_names)
3247 {
3248 int i;
3249
3250 for (i = 0; i < file_data->num_file_names; ++i)
3251 {
3252 xfree ((void*) file_data->real_names[i]);
3253 file_data->real_names[i] = NULL;
3254 }
3255 }
3256
3257 return 1;
3258 }
3259
3260 static void
3261 dw2_forget_cached_source_info (struct objfile *objfile)
3262 {
3263 struct dwarf2_per_objfile *dwarf2_per_objfile
3264 = get_dwarf2_per_objfile (objfile);
3265
3266 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3267 dw2_free_cached_file_names, NULL);
3268 }
3269
3270 /* Helper function for dw2_map_symtabs_matching_filename that expands
3271 the symtabs and calls the iterator. */
3272
3273 static int
3274 dw2_map_expand_apply (struct objfile *objfile,
3275 struct dwarf2_per_cu_data *per_cu,
3276 const char *name, const char *real_path,
3277 gdb::function_view<bool (symtab *)> callback)
3278 {
3279 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3280
3281 /* Don't visit already-expanded CUs. */
3282 if (per_cu->v.quick->compunit_symtab)
3283 return 0;
3284
3285 /* This may expand more than one symtab, and we want to iterate over
3286 all of them. */
3287 dw2_instantiate_symtab (per_cu, false);
3288
3289 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3290 last_made, callback);
3291 }
3292
3293 /* Implementation of the map_symtabs_matching_filename method. */
3294
3295 static bool
3296 dw2_map_symtabs_matching_filename
3297 (struct objfile *objfile, const char *name, const char *real_path,
3298 gdb::function_view<bool (symtab *)> callback)
3299 {
3300 const char *name_basename = lbasename (name);
3301 struct dwarf2_per_objfile *dwarf2_per_objfile
3302 = get_dwarf2_per_objfile (objfile);
3303
3304 /* The rule is CUs specify all the files, including those used by
3305 any TU, so there's no need to scan TUs here. */
3306
3307 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3308 {
3309 /* We only need to look at symtabs not already expanded. */
3310 if (per_cu->v.quick->compunit_symtab)
3311 continue;
3312
3313 quick_file_names *file_data = dw2_get_file_names (per_cu);
3314 if (file_data == NULL)
3315 continue;
3316
3317 for (int j = 0; j < file_data->num_file_names; ++j)
3318 {
3319 const char *this_name = file_data->file_names[j];
3320 const char *this_real_name;
3321
3322 if (compare_filenames_for_search (this_name, name))
3323 {
3324 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3325 callback))
3326 return true;
3327 continue;
3328 }
3329
3330 /* Before we invoke realpath, which can get expensive when many
3331 files are involved, do a quick comparison of the basenames. */
3332 if (! basenames_may_differ
3333 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3334 continue;
3335
3336 this_real_name = dw2_get_real_path (objfile, file_data, j);
3337 if (compare_filenames_for_search (this_real_name, name))
3338 {
3339 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3340 callback))
3341 return true;
3342 continue;
3343 }
3344
3345 if (real_path != NULL)
3346 {
3347 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3348 gdb_assert (IS_ABSOLUTE_PATH (name));
3349 if (this_real_name != NULL
3350 && FILENAME_CMP (real_path, this_real_name) == 0)
3351 {
3352 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3353 callback))
3354 return true;
3355 continue;
3356 }
3357 }
3358 }
3359 }
3360
3361 return false;
3362 }
3363
3364 /* Struct used to manage iterating over all CUs looking for a symbol. */
3365
3366 struct dw2_symtab_iterator
3367 {
3368 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3369 struct dwarf2_per_objfile *dwarf2_per_objfile;
3370 /* If set, only look for symbols that match that block. Valid values are
3371 GLOBAL_BLOCK and STATIC_BLOCK. */
3372 gdb::optional<block_enum> block_index;
3373 /* The kind of symbol we're looking for. */
3374 domain_enum domain;
3375 /* The list of CUs from the index entry of the symbol,
3376 or NULL if not found. */
3377 offset_type *vec;
3378 /* The next element in VEC to look at. */
3379 int next;
3380 /* The number of elements in VEC, or zero if there is no match. */
3381 int length;
3382 /* Have we seen a global version of the symbol?
3383 If so we can ignore all further global instances.
3384 This is to work around gold/15646, inefficient gold-generated
3385 indices. */
3386 int global_seen;
3387 };
3388
3389 /* Initialize the index symtab iterator ITER. */
3390
3391 static void
3392 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3393 struct dwarf2_per_objfile *dwarf2_per_objfile,
3394 gdb::optional<block_enum> block_index,
3395 domain_enum domain,
3396 const char *name)
3397 {
3398 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3399 iter->block_index = block_index;
3400 iter->domain = domain;
3401 iter->next = 0;
3402 iter->global_seen = 0;
3403
3404 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3405
3406 /* index is NULL if OBJF_READNOW. */
3407 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3408 iter->length = MAYBE_SWAP (*iter->vec);
3409 else
3410 {
3411 iter->vec = NULL;
3412 iter->length = 0;
3413 }
3414 }
3415
3416 /* Return the next matching CU or NULL if there are no more. */
3417
3418 static struct dwarf2_per_cu_data *
3419 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3420 {
3421 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3422
3423 for ( ; iter->next < iter->length; ++iter->next)
3424 {
3425 offset_type cu_index_and_attrs =
3426 MAYBE_SWAP (iter->vec[iter->next + 1]);
3427 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3428 gdb_index_symbol_kind symbol_kind =
3429 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3430 /* Only check the symbol attributes if they're present.
3431 Indices prior to version 7 don't record them,
3432 and indices >= 7 may elide them for certain symbols
3433 (gold does this). */
3434 int attrs_valid =
3435 (dwarf2_per_objfile->index_table->version >= 7
3436 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3437
3438 /* Don't crash on bad data. */
3439 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3440 + dwarf2_per_objfile->all_type_units.size ()))
3441 {
3442 complaint (_(".gdb_index entry has bad CU index"
3443 " [in module %s]"),
3444 objfile_name (dwarf2_per_objfile->objfile));
3445 continue;
3446 }
3447
3448 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3449
3450 /* Skip if already read in. */
3451 if (per_cu->v.quick->compunit_symtab)
3452 continue;
3453
3454 /* Check static vs global. */
3455 if (attrs_valid)
3456 {
3457 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3458
3459 if (iter->block_index.has_value ())
3460 {
3461 bool want_static = *iter->block_index == STATIC_BLOCK;
3462
3463 if (is_static != want_static)
3464 continue;
3465 }
3466
3467 /* Work around gold/15646. */
3468 if (!is_static && iter->global_seen)
3469 continue;
3470 if (!is_static)
3471 iter->global_seen = 1;
3472 }
3473
3474 /* Only check the symbol's kind if it has one. */
3475 if (attrs_valid)
3476 {
3477 switch (iter->domain)
3478 {
3479 case VAR_DOMAIN:
3480 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3481 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3482 /* Some types are also in VAR_DOMAIN. */
3483 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3484 continue;
3485 break;
3486 case STRUCT_DOMAIN:
3487 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3488 continue;
3489 break;
3490 case LABEL_DOMAIN:
3491 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3492 continue;
3493 break;
3494 case MODULE_DOMAIN:
3495 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3496 continue;
3497 break;
3498 default:
3499 break;
3500 }
3501 }
3502
3503 ++iter->next;
3504 return per_cu;
3505 }
3506
3507 return NULL;
3508 }
3509
3510 static struct compunit_symtab *
3511 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3512 const char *name, domain_enum domain)
3513 {
3514 struct compunit_symtab *stab_best = NULL;
3515 struct dwarf2_per_objfile *dwarf2_per_objfile
3516 = get_dwarf2_per_objfile (objfile);
3517
3518 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3519
3520 struct dw2_symtab_iterator iter;
3521 struct dwarf2_per_cu_data *per_cu;
3522
3523 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3524
3525 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3526 {
3527 struct symbol *sym, *with_opaque = NULL;
3528 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3529 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3530 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3531
3532 sym = block_find_symbol (block, name, domain,
3533 block_find_non_opaque_type_preferred,
3534 &with_opaque);
3535
3536 /* Some caution must be observed with overloaded functions
3537 and methods, since the index will not contain any overload
3538 information (but NAME might contain it). */
3539
3540 if (sym != NULL
3541 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3542 return stab;
3543 if (with_opaque != NULL
3544 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3545 stab_best = stab;
3546
3547 /* Keep looking through other CUs. */
3548 }
3549
3550 return stab_best;
3551 }
3552
3553 static void
3554 dw2_print_stats (struct objfile *objfile)
3555 {
3556 struct dwarf2_per_objfile *dwarf2_per_objfile
3557 = get_dwarf2_per_objfile (objfile);
3558 int total = (dwarf2_per_objfile->all_comp_units.size ()
3559 + dwarf2_per_objfile->all_type_units.size ());
3560 int count = 0;
3561
3562 for (int i = 0; i < total; ++i)
3563 {
3564 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3565
3566 if (!per_cu->v.quick->compunit_symtab)
3567 ++count;
3568 }
3569 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3570 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3571 }
3572
3573 /* This dumps minimal information about the index.
3574 It is called via "mt print objfiles".
3575 One use is to verify .gdb_index has been loaded by the
3576 gdb.dwarf2/gdb-index.exp testcase. */
3577
3578 static void
3579 dw2_dump (struct objfile *objfile)
3580 {
3581 struct dwarf2_per_objfile *dwarf2_per_objfile
3582 = get_dwarf2_per_objfile (objfile);
3583
3584 gdb_assert (dwarf2_per_objfile->using_index);
3585 printf_filtered (".gdb_index:");
3586 if (dwarf2_per_objfile->index_table != NULL)
3587 {
3588 printf_filtered (" version %d\n",
3589 dwarf2_per_objfile->index_table->version);
3590 }
3591 else
3592 printf_filtered (" faked for \"readnow\"\n");
3593 printf_filtered ("\n");
3594 }
3595
3596 static void
3597 dw2_expand_symtabs_for_function (struct objfile *objfile,
3598 const char *func_name)
3599 {
3600 struct dwarf2_per_objfile *dwarf2_per_objfile
3601 = get_dwarf2_per_objfile (objfile);
3602
3603 struct dw2_symtab_iterator iter;
3604 struct dwarf2_per_cu_data *per_cu;
3605
3606 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3607
3608 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3609 dw2_instantiate_symtab (per_cu, false);
3610
3611 }
3612
3613 static void
3614 dw2_expand_all_symtabs (struct objfile *objfile)
3615 {
3616 struct dwarf2_per_objfile *dwarf2_per_objfile
3617 = get_dwarf2_per_objfile (objfile);
3618 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3619 + dwarf2_per_objfile->all_type_units.size ());
3620
3621 for (int i = 0; i < total_units; ++i)
3622 {
3623 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3624
3625 /* We don't want to directly expand a partial CU, because if we
3626 read it with the wrong language, then assertion failures can
3627 be triggered later on. See PR symtab/23010. So, tell
3628 dw2_instantiate_symtab to skip partial CUs -- any important
3629 partial CU will be read via DW_TAG_imported_unit anyway. */
3630 dw2_instantiate_symtab (per_cu, true);
3631 }
3632 }
3633
3634 static void
3635 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3636 const char *fullname)
3637 {
3638 struct dwarf2_per_objfile *dwarf2_per_objfile
3639 = get_dwarf2_per_objfile (objfile);
3640
3641 /* We don't need to consider type units here.
3642 This is only called for examining code, e.g. expand_line_sal.
3643 There can be an order of magnitude (or more) more type units
3644 than comp units, and we avoid them if we can. */
3645
3646 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3647 {
3648 /* We only need to look at symtabs not already expanded. */
3649 if (per_cu->v.quick->compunit_symtab)
3650 continue;
3651
3652 quick_file_names *file_data = dw2_get_file_names (per_cu);
3653 if (file_data == NULL)
3654 continue;
3655
3656 for (int j = 0; j < file_data->num_file_names; ++j)
3657 {
3658 const char *this_fullname = file_data->file_names[j];
3659
3660 if (filename_cmp (this_fullname, fullname) == 0)
3661 {
3662 dw2_instantiate_symtab (per_cu, false);
3663 break;
3664 }
3665 }
3666 }
3667 }
3668
3669 static void
3670 dw2_map_matching_symbols
3671 (struct objfile *objfile,
3672 const lookup_name_info &name, domain_enum domain,
3673 int global,
3674 gdb::function_view<symbol_found_callback_ftype> callback,
3675 symbol_compare_ftype *ordered_compare)
3676 {
3677 /* Currently unimplemented; used for Ada. The function can be called if the
3678 current language is Ada for a non-Ada objfile using GNU index. As Ada
3679 does not look for non-Ada symbols this function should just return. */
3680 }
3681
3682 /* Starting from a search name, return the string that finds the upper
3683 bound of all strings that start with SEARCH_NAME in a sorted name
3684 list. Returns the empty string to indicate that the upper bound is
3685 the end of the list. */
3686
3687 static std::string
3688 make_sort_after_prefix_name (const char *search_name)
3689 {
3690 /* When looking to complete "func", we find the upper bound of all
3691 symbols that start with "func" by looking for where we'd insert
3692 the closest string that would follow "func" in lexicographical
3693 order. Usually, that's "func"-with-last-character-incremented,
3694 i.e. "fund". Mind non-ASCII characters, though. Usually those
3695 will be UTF-8 multi-byte sequences, but we can't be certain.
3696 Especially mind the 0xff character, which is a valid character in
3697 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3698 rule out compilers allowing it in identifiers. Note that
3699 conveniently, strcmp/strcasecmp are specified to compare
3700 characters interpreted as unsigned char. So what we do is treat
3701 the whole string as a base 256 number composed of a sequence of
3702 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3703 to 0, and carries 1 to the following more-significant position.
3704 If the very first character in SEARCH_NAME ends up incremented
3705 and carries/overflows, then the upper bound is the end of the
3706 list. The string after the empty string is also the empty
3707 string.
3708
3709 Some examples of this operation:
3710
3711 SEARCH_NAME => "+1" RESULT
3712
3713 "abc" => "abd"
3714 "ab\xff" => "ac"
3715 "\xff" "a" "\xff" => "\xff" "b"
3716 "\xff" => ""
3717 "\xff\xff" => ""
3718 "" => ""
3719
3720 Then, with these symbols for example:
3721
3722 func
3723 func1
3724 fund
3725
3726 completing "func" looks for symbols between "func" and
3727 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3728 which finds "func" and "func1", but not "fund".
3729
3730 And with:
3731
3732 funcÿ (Latin1 'ÿ' [0xff])
3733 funcÿ1
3734 fund
3735
3736 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3737 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3738
3739 And with:
3740
3741 ÿÿ (Latin1 'ÿ' [0xff])
3742 ÿÿ1
3743
3744 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3745 the end of the list.
3746 */
3747 std::string after = search_name;
3748 while (!after.empty () && (unsigned char) after.back () == 0xff)
3749 after.pop_back ();
3750 if (!after.empty ())
3751 after.back () = (unsigned char) after.back () + 1;
3752 return after;
3753 }
3754
3755 /* See declaration. */
3756
3757 std::pair<std::vector<name_component>::const_iterator,
3758 std::vector<name_component>::const_iterator>
3759 mapped_index_base::find_name_components_bounds
3760 (const lookup_name_info &lookup_name_without_params, language lang) const
3761 {
3762 auto *name_cmp
3763 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3764
3765 const char *lang_name
3766 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3767
3768 /* Comparison function object for lower_bound that matches against a
3769 given symbol name. */
3770 auto lookup_compare_lower = [&] (const name_component &elem,
3771 const char *name)
3772 {
3773 const char *elem_qualified = this->symbol_name_at (elem.idx);
3774 const char *elem_name = elem_qualified + elem.name_offset;
3775 return name_cmp (elem_name, name) < 0;
3776 };
3777
3778 /* Comparison function object for upper_bound that matches against a
3779 given symbol name. */
3780 auto lookup_compare_upper = [&] (const char *name,
3781 const name_component &elem)
3782 {
3783 const char *elem_qualified = this->symbol_name_at (elem.idx);
3784 const char *elem_name = elem_qualified + elem.name_offset;
3785 return name_cmp (name, elem_name) < 0;
3786 };
3787
3788 auto begin = this->name_components.begin ();
3789 auto end = this->name_components.end ();
3790
3791 /* Find the lower bound. */
3792 auto lower = [&] ()
3793 {
3794 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3795 return begin;
3796 else
3797 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3798 } ();
3799
3800 /* Find the upper bound. */
3801 auto upper = [&] ()
3802 {
3803 if (lookup_name_without_params.completion_mode ())
3804 {
3805 /* In completion mode, we want UPPER to point past all
3806 symbols names that have the same prefix. I.e., with
3807 these symbols, and completing "func":
3808
3809 function << lower bound
3810 function1
3811 other_function << upper bound
3812
3813 We find the upper bound by looking for the insertion
3814 point of "func"-with-last-character-incremented,
3815 i.e. "fund". */
3816 std::string after = make_sort_after_prefix_name (lang_name);
3817 if (after.empty ())
3818 return end;
3819 return std::lower_bound (lower, end, after.c_str (),
3820 lookup_compare_lower);
3821 }
3822 else
3823 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3824 } ();
3825
3826 return {lower, upper};
3827 }
3828
3829 /* See declaration. */
3830
3831 void
3832 mapped_index_base::build_name_components ()
3833 {
3834 if (!this->name_components.empty ())
3835 return;
3836
3837 this->name_components_casing = case_sensitivity;
3838 auto *name_cmp
3839 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3840
3841 /* The code below only knows how to break apart components of C++
3842 symbol names (and other languages that use '::' as
3843 namespace/module separator) and Ada symbol names. */
3844 auto count = this->symbol_name_count ();
3845 for (offset_type idx = 0; idx < count; idx++)
3846 {
3847 if (this->symbol_name_slot_invalid (idx))
3848 continue;
3849
3850 const char *name = this->symbol_name_at (idx);
3851
3852 /* Add each name component to the name component table. */
3853 unsigned int previous_len = 0;
3854
3855 if (strstr (name, "::") != nullptr)
3856 {
3857 for (unsigned int current_len = cp_find_first_component (name);
3858 name[current_len] != '\0';
3859 current_len += cp_find_first_component (name + current_len))
3860 {
3861 gdb_assert (name[current_len] == ':');
3862 this->name_components.push_back ({previous_len, idx});
3863 /* Skip the '::'. */
3864 current_len += 2;
3865 previous_len = current_len;
3866 }
3867 }
3868 else
3869 {
3870 /* Handle the Ada encoded (aka mangled) form here. */
3871 for (const char *iter = strstr (name, "__");
3872 iter != nullptr;
3873 iter = strstr (iter, "__"))
3874 {
3875 this->name_components.push_back ({previous_len, idx});
3876 iter += 2;
3877 previous_len = iter - name;
3878 }
3879 }
3880
3881 this->name_components.push_back ({previous_len, idx});
3882 }
3883
3884 /* Sort name_components elements by name. */
3885 auto name_comp_compare = [&] (const name_component &left,
3886 const name_component &right)
3887 {
3888 const char *left_qualified = this->symbol_name_at (left.idx);
3889 const char *right_qualified = this->symbol_name_at (right.idx);
3890
3891 const char *left_name = left_qualified + left.name_offset;
3892 const char *right_name = right_qualified + right.name_offset;
3893
3894 return name_cmp (left_name, right_name) < 0;
3895 };
3896
3897 std::sort (this->name_components.begin (),
3898 this->name_components.end (),
3899 name_comp_compare);
3900 }
3901
3902 /* Helper for dw2_expand_symtabs_matching that works with a
3903 mapped_index_base instead of the containing objfile. This is split
3904 to a separate function in order to be able to unit test the
3905 name_components matching using a mock mapped_index_base. For each
3906 symbol name that matches, calls MATCH_CALLBACK, passing it the
3907 symbol's index in the mapped_index_base symbol table. */
3908
3909 static void
3910 dw2_expand_symtabs_matching_symbol
3911 (mapped_index_base &index,
3912 const lookup_name_info &lookup_name_in,
3913 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3914 enum search_domain kind,
3915 gdb::function_view<bool (offset_type)> match_callback)
3916 {
3917 lookup_name_info lookup_name_without_params
3918 = lookup_name_in.make_ignore_params ();
3919
3920 /* Build the symbol name component sorted vector, if we haven't
3921 yet. */
3922 index.build_name_components ();
3923
3924 /* The same symbol may appear more than once in the range though.
3925 E.g., if we're looking for symbols that complete "w", and we have
3926 a symbol named "w1::w2", we'll find the two name components for
3927 that same symbol in the range. To be sure we only call the
3928 callback once per symbol, we first collect the symbol name
3929 indexes that matched in a temporary vector and ignore
3930 duplicates. */
3931 std::vector<offset_type> matches;
3932
3933 struct name_and_matcher
3934 {
3935 symbol_name_matcher_ftype *matcher;
3936 const std::string &name;
3937
3938 bool operator== (const name_and_matcher &other) const
3939 {
3940 return matcher == other.matcher && name == other.name;
3941 }
3942 };
3943
3944 /* A vector holding all the different symbol name matchers, for all
3945 languages. */
3946 std::vector<name_and_matcher> matchers;
3947
3948 for (int i = 0; i < nr_languages; i++)
3949 {
3950 enum language lang_e = (enum language) i;
3951
3952 const language_defn *lang = language_def (lang_e);
3953 symbol_name_matcher_ftype *name_matcher
3954 = get_symbol_name_matcher (lang, lookup_name_without_params);
3955
3956 name_and_matcher key {
3957 name_matcher,
3958 lookup_name_without_params.language_lookup_name (lang_e)
3959 };
3960
3961 /* Don't insert the same comparison routine more than once.
3962 Note that we do this linear walk. This is not a problem in
3963 practice because the number of supported languages is
3964 low. */
3965 if (std::find (matchers.begin (), matchers.end (), key)
3966 != matchers.end ())
3967 continue;
3968 matchers.push_back (std::move (key));
3969
3970 auto bounds
3971 = index.find_name_components_bounds (lookup_name_without_params,
3972 lang_e);
3973
3974 /* Now for each symbol name in range, check to see if we have a name
3975 match, and if so, call the MATCH_CALLBACK callback. */
3976
3977 for (; bounds.first != bounds.second; ++bounds.first)
3978 {
3979 const char *qualified = index.symbol_name_at (bounds.first->idx);
3980
3981 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3982 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3983 continue;
3984
3985 matches.push_back (bounds.first->idx);
3986 }
3987 }
3988
3989 std::sort (matches.begin (), matches.end ());
3990
3991 /* Finally call the callback, once per match. */
3992 ULONGEST prev = -1;
3993 for (offset_type idx : matches)
3994 {
3995 if (prev != idx)
3996 {
3997 if (!match_callback (idx))
3998 break;
3999 prev = idx;
4000 }
4001 }
4002
4003 /* Above we use a type wider than idx's for 'prev', since 0 and
4004 (offset_type)-1 are both possible values. */
4005 static_assert (sizeof (prev) > sizeof (offset_type), "");
4006 }
4007
4008 #if GDB_SELF_TEST
4009
4010 namespace selftests { namespace dw2_expand_symtabs_matching {
4011
4012 /* A mock .gdb_index/.debug_names-like name index table, enough to
4013 exercise dw2_expand_symtabs_matching_symbol, which works with the
4014 mapped_index_base interface. Builds an index from the symbol list
4015 passed as parameter to the constructor. */
4016 class mock_mapped_index : public mapped_index_base
4017 {
4018 public:
4019 mock_mapped_index (gdb::array_view<const char *> symbols)
4020 : m_symbol_table (symbols)
4021 {}
4022
4023 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4024
4025 /* Return the number of names in the symbol table. */
4026 size_t symbol_name_count () const override
4027 {
4028 return m_symbol_table.size ();
4029 }
4030
4031 /* Get the name of the symbol at IDX in the symbol table. */
4032 const char *symbol_name_at (offset_type idx) const override
4033 {
4034 return m_symbol_table[idx];
4035 }
4036
4037 private:
4038 gdb::array_view<const char *> m_symbol_table;
4039 };
4040
4041 /* Convenience function that converts a NULL pointer to a "<null>"
4042 string, to pass to print routines. */
4043
4044 static const char *
4045 string_or_null (const char *str)
4046 {
4047 return str != NULL ? str : "<null>";
4048 }
4049
4050 /* Check if a lookup_name_info built from
4051 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4052 index. EXPECTED_LIST is the list of expected matches, in expected
4053 matching order. If no match expected, then an empty list is
4054 specified. Returns true on success. On failure prints a warning
4055 indicating the file:line that failed, and returns false. */
4056
4057 static bool
4058 check_match (const char *file, int line,
4059 mock_mapped_index &mock_index,
4060 const char *name, symbol_name_match_type match_type,
4061 bool completion_mode,
4062 std::initializer_list<const char *> expected_list)
4063 {
4064 lookup_name_info lookup_name (name, match_type, completion_mode);
4065
4066 bool matched = true;
4067
4068 auto mismatch = [&] (const char *expected_str,
4069 const char *got)
4070 {
4071 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4072 "expected=\"%s\", got=\"%s\"\n"),
4073 file, line,
4074 (match_type == symbol_name_match_type::FULL
4075 ? "FULL" : "WILD"),
4076 name, string_or_null (expected_str), string_or_null (got));
4077 matched = false;
4078 };
4079
4080 auto expected_it = expected_list.begin ();
4081 auto expected_end = expected_list.end ();
4082
4083 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4084 NULL, ALL_DOMAIN,
4085 [&] (offset_type idx)
4086 {
4087 const char *matched_name = mock_index.symbol_name_at (idx);
4088 const char *expected_str
4089 = expected_it == expected_end ? NULL : *expected_it++;
4090
4091 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4092 mismatch (expected_str, matched_name);
4093 return true;
4094 });
4095
4096 const char *expected_str
4097 = expected_it == expected_end ? NULL : *expected_it++;
4098 if (expected_str != NULL)
4099 mismatch (expected_str, NULL);
4100
4101 return matched;
4102 }
4103
4104 /* The symbols added to the mock mapped_index for testing (in
4105 canonical form). */
4106 static const char *test_symbols[] = {
4107 "function",
4108 "std::bar",
4109 "std::zfunction",
4110 "std::zfunction2",
4111 "w1::w2",
4112 "ns::foo<char*>",
4113 "ns::foo<int>",
4114 "ns::foo<long>",
4115 "ns2::tmpl<int>::foo2",
4116 "(anonymous namespace)::A::B::C",
4117
4118 /* These are used to check that the increment-last-char in the
4119 matching algorithm for completion doesn't match "t1_fund" when
4120 completing "t1_func". */
4121 "t1_func",
4122 "t1_func1",
4123 "t1_fund",
4124 "t1_fund1",
4125
4126 /* A UTF-8 name with multi-byte sequences to make sure that
4127 cp-name-parser understands this as a single identifier ("função"
4128 is "function" in PT). */
4129 u8"u8função",
4130
4131 /* \377 (0xff) is Latin1 'ÿ'. */
4132 "yfunc\377",
4133
4134 /* \377 (0xff) is Latin1 'ÿ'. */
4135 "\377",
4136 "\377\377123",
4137
4138 /* A name with all sorts of complications. Starts with "z" to make
4139 it easier for the completion tests below. */
4140 #define Z_SYM_NAME \
4141 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4142 "::tuple<(anonymous namespace)::ui*, " \
4143 "std::default_delete<(anonymous namespace)::ui>, void>"
4144
4145 Z_SYM_NAME
4146 };
4147
4148 /* Returns true if the mapped_index_base::find_name_component_bounds
4149 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4150 in completion mode. */
4151
4152 static bool
4153 check_find_bounds_finds (mapped_index_base &index,
4154 const char *search_name,
4155 gdb::array_view<const char *> expected_syms)
4156 {
4157 lookup_name_info lookup_name (search_name,
4158 symbol_name_match_type::FULL, true);
4159
4160 auto bounds = index.find_name_components_bounds (lookup_name,
4161 language_cplus);
4162
4163 size_t distance = std::distance (bounds.first, bounds.second);
4164 if (distance != expected_syms.size ())
4165 return false;
4166
4167 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4168 {
4169 auto nc_elem = bounds.first + exp_elem;
4170 const char *qualified = index.symbol_name_at (nc_elem->idx);
4171 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4172 return false;
4173 }
4174
4175 return true;
4176 }
4177
4178 /* Test the lower-level mapped_index::find_name_component_bounds
4179 method. */
4180
4181 static void
4182 test_mapped_index_find_name_component_bounds ()
4183 {
4184 mock_mapped_index mock_index (test_symbols);
4185
4186 mock_index.build_name_components ();
4187
4188 /* Test the lower-level mapped_index::find_name_component_bounds
4189 method in completion mode. */
4190 {
4191 static const char *expected_syms[] = {
4192 "t1_func",
4193 "t1_func1",
4194 };
4195
4196 SELF_CHECK (check_find_bounds_finds (mock_index,
4197 "t1_func", expected_syms));
4198 }
4199
4200 /* Check that the increment-last-char in the name matching algorithm
4201 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4202 {
4203 static const char *expected_syms1[] = {
4204 "\377",
4205 "\377\377123",
4206 };
4207 SELF_CHECK (check_find_bounds_finds (mock_index,
4208 "\377", expected_syms1));
4209
4210 static const char *expected_syms2[] = {
4211 "\377\377123",
4212 };
4213 SELF_CHECK (check_find_bounds_finds (mock_index,
4214 "\377\377", expected_syms2));
4215 }
4216 }
4217
4218 /* Test dw2_expand_symtabs_matching_symbol. */
4219
4220 static void
4221 test_dw2_expand_symtabs_matching_symbol ()
4222 {
4223 mock_mapped_index mock_index (test_symbols);
4224
4225 /* We let all tests run until the end even if some fails, for debug
4226 convenience. */
4227 bool any_mismatch = false;
4228
4229 /* Create the expected symbols list (an initializer_list). Needed
4230 because lists have commas, and we need to pass them to CHECK,
4231 which is a macro. */
4232 #define EXPECT(...) { __VA_ARGS__ }
4233
4234 /* Wrapper for check_match that passes down the current
4235 __FILE__/__LINE__. */
4236 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4237 any_mismatch |= !check_match (__FILE__, __LINE__, \
4238 mock_index, \
4239 NAME, MATCH_TYPE, COMPLETION_MODE, \
4240 EXPECTED_LIST)
4241
4242 /* Identity checks. */
4243 for (const char *sym : test_symbols)
4244 {
4245 /* Should be able to match all existing symbols. */
4246 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4247 EXPECT (sym));
4248
4249 /* Should be able to match all existing symbols with
4250 parameters. */
4251 std::string with_params = std::string (sym) + "(int)";
4252 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4253 EXPECT (sym));
4254
4255 /* Should be able to match all existing symbols with
4256 parameters and qualifiers. */
4257 with_params = std::string (sym) + " ( int ) const";
4258 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4259 EXPECT (sym));
4260
4261 /* This should really find sym, but cp-name-parser.y doesn't
4262 know about lvalue/rvalue qualifiers yet. */
4263 with_params = std::string (sym) + " ( int ) &&";
4264 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4265 {});
4266 }
4267
4268 /* Check that the name matching algorithm for completion doesn't get
4269 confused with Latin1 'ÿ' / 0xff. */
4270 {
4271 static const char str[] = "\377";
4272 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4273 EXPECT ("\377", "\377\377123"));
4274 }
4275
4276 /* Check that the increment-last-char in the matching algorithm for
4277 completion doesn't match "t1_fund" when completing "t1_func". */
4278 {
4279 static const char str[] = "t1_func";
4280 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4281 EXPECT ("t1_func", "t1_func1"));
4282 }
4283
4284 /* Check that completion mode works at each prefix of the expected
4285 symbol name. */
4286 {
4287 static const char str[] = "function(int)";
4288 size_t len = strlen (str);
4289 std::string lookup;
4290
4291 for (size_t i = 1; i < len; i++)
4292 {
4293 lookup.assign (str, i);
4294 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4295 EXPECT ("function"));
4296 }
4297 }
4298
4299 /* While "w" is a prefix of both components, the match function
4300 should still only be called once. */
4301 {
4302 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4303 EXPECT ("w1::w2"));
4304 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4305 EXPECT ("w1::w2"));
4306 }
4307
4308 /* Same, with a "complicated" symbol. */
4309 {
4310 static const char str[] = Z_SYM_NAME;
4311 size_t len = strlen (str);
4312 std::string lookup;
4313
4314 for (size_t i = 1; i < len; i++)
4315 {
4316 lookup.assign (str, i);
4317 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4318 EXPECT (Z_SYM_NAME));
4319 }
4320 }
4321
4322 /* In FULL mode, an incomplete symbol doesn't match. */
4323 {
4324 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4325 {});
4326 }
4327
4328 /* A complete symbol with parameters matches any overload, since the
4329 index has no overload info. */
4330 {
4331 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4332 EXPECT ("std::zfunction", "std::zfunction2"));
4333 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4334 EXPECT ("std::zfunction", "std::zfunction2"));
4335 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4336 EXPECT ("std::zfunction", "std::zfunction2"));
4337 }
4338
4339 /* Check that whitespace is ignored appropriately. A symbol with a
4340 template argument list. */
4341 {
4342 static const char expected[] = "ns::foo<int>";
4343 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4344 EXPECT (expected));
4345 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4346 EXPECT (expected));
4347 }
4348
4349 /* Check that whitespace is ignored appropriately. A symbol with a
4350 template argument list that includes a pointer. */
4351 {
4352 static const char expected[] = "ns::foo<char*>";
4353 /* Try both completion and non-completion modes. */
4354 static const bool completion_mode[2] = {false, true};
4355 for (size_t i = 0; i < 2; i++)
4356 {
4357 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4358 completion_mode[i], EXPECT (expected));
4359 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4360 completion_mode[i], EXPECT (expected));
4361
4362 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4363 completion_mode[i], EXPECT (expected));
4364 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4365 completion_mode[i], EXPECT (expected));
4366 }
4367 }
4368
4369 {
4370 /* Check method qualifiers are ignored. */
4371 static const char expected[] = "ns::foo<char*>";
4372 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4373 symbol_name_match_type::FULL, true, EXPECT (expected));
4374 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4375 symbol_name_match_type::FULL, true, EXPECT (expected));
4376 CHECK_MATCH ("foo < char * > ( int ) const",
4377 symbol_name_match_type::WILD, true, EXPECT (expected));
4378 CHECK_MATCH ("foo < char * > ( int ) &&",
4379 symbol_name_match_type::WILD, true, EXPECT (expected));
4380 }
4381
4382 /* Test lookup names that don't match anything. */
4383 {
4384 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4385 {});
4386
4387 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4388 {});
4389 }
4390
4391 /* Some wild matching tests, exercising "(anonymous namespace)",
4392 which should not be confused with a parameter list. */
4393 {
4394 static const char *syms[] = {
4395 "A::B::C",
4396 "B::C",
4397 "C",
4398 "A :: B :: C ( int )",
4399 "B :: C ( int )",
4400 "C ( int )",
4401 };
4402
4403 for (const char *s : syms)
4404 {
4405 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4406 EXPECT ("(anonymous namespace)::A::B::C"));
4407 }
4408 }
4409
4410 {
4411 static const char expected[] = "ns2::tmpl<int>::foo2";
4412 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4413 EXPECT (expected));
4414 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4415 EXPECT (expected));
4416 }
4417
4418 SELF_CHECK (!any_mismatch);
4419
4420 #undef EXPECT
4421 #undef CHECK_MATCH
4422 }
4423
4424 static void
4425 run_test ()
4426 {
4427 test_mapped_index_find_name_component_bounds ();
4428 test_dw2_expand_symtabs_matching_symbol ();
4429 }
4430
4431 }} // namespace selftests::dw2_expand_symtabs_matching
4432
4433 #endif /* GDB_SELF_TEST */
4434
4435 /* If FILE_MATCHER is NULL or if PER_CU has
4436 dwarf2_per_cu_quick_data::MARK set (see
4437 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4438 EXPANSION_NOTIFY on it. */
4439
4440 static void
4441 dw2_expand_symtabs_matching_one
4442 (struct dwarf2_per_cu_data *per_cu,
4443 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4444 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4445 {
4446 if (file_matcher == NULL || per_cu->v.quick->mark)
4447 {
4448 bool symtab_was_null
4449 = (per_cu->v.quick->compunit_symtab == NULL);
4450
4451 dw2_instantiate_symtab (per_cu, false);
4452
4453 if (expansion_notify != NULL
4454 && symtab_was_null
4455 && per_cu->v.quick->compunit_symtab != NULL)
4456 expansion_notify (per_cu->v.quick->compunit_symtab);
4457 }
4458 }
4459
4460 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4461 matched, to expand corresponding CUs that were marked. IDX is the
4462 index of the symbol name that matched. */
4463
4464 static void
4465 dw2_expand_marked_cus
4466 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4467 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4468 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4469 search_domain kind)
4470 {
4471 offset_type *vec, vec_len, vec_idx;
4472 bool global_seen = false;
4473 mapped_index &index = *dwarf2_per_objfile->index_table;
4474
4475 vec = (offset_type *) (index.constant_pool
4476 + MAYBE_SWAP (index.symbol_table[idx].vec));
4477 vec_len = MAYBE_SWAP (vec[0]);
4478 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4479 {
4480 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4481 /* This value is only valid for index versions >= 7. */
4482 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4483 gdb_index_symbol_kind symbol_kind =
4484 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4485 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4486 /* Only check the symbol attributes if they're present.
4487 Indices prior to version 7 don't record them,
4488 and indices >= 7 may elide them for certain symbols
4489 (gold does this). */
4490 int attrs_valid =
4491 (index.version >= 7
4492 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4493
4494 /* Work around gold/15646. */
4495 if (attrs_valid)
4496 {
4497 if (!is_static && global_seen)
4498 continue;
4499 if (!is_static)
4500 global_seen = true;
4501 }
4502
4503 /* Only check the symbol's kind if it has one. */
4504 if (attrs_valid)
4505 {
4506 switch (kind)
4507 {
4508 case VARIABLES_DOMAIN:
4509 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4510 continue;
4511 break;
4512 case FUNCTIONS_DOMAIN:
4513 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4514 continue;
4515 break;
4516 case TYPES_DOMAIN:
4517 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4518 continue;
4519 break;
4520 case MODULES_DOMAIN:
4521 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4522 continue;
4523 break;
4524 default:
4525 break;
4526 }
4527 }
4528
4529 /* Don't crash on bad data. */
4530 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4531 + dwarf2_per_objfile->all_type_units.size ()))
4532 {
4533 complaint (_(".gdb_index entry has bad CU index"
4534 " [in module %s]"),
4535 objfile_name (dwarf2_per_objfile->objfile));
4536 continue;
4537 }
4538
4539 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4540 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4541 expansion_notify);
4542 }
4543 }
4544
4545 /* If FILE_MATCHER is non-NULL, set all the
4546 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4547 that match FILE_MATCHER. */
4548
4549 static void
4550 dw_expand_symtabs_matching_file_matcher
4551 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4552 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4553 {
4554 if (file_matcher == NULL)
4555 return;
4556
4557 objfile *const objfile = dwarf2_per_objfile->objfile;
4558
4559 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4560 htab_eq_pointer,
4561 NULL, xcalloc, xfree));
4562 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4563 htab_eq_pointer,
4564 NULL, xcalloc, xfree));
4565
4566 /* The rule is CUs specify all the files, including those used by
4567 any TU, so there's no need to scan TUs here. */
4568
4569 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4570 {
4571 QUIT;
4572
4573 per_cu->v.quick->mark = 0;
4574
4575 /* We only need to look at symtabs not already expanded. */
4576 if (per_cu->v.quick->compunit_symtab)
4577 continue;
4578
4579 quick_file_names *file_data = dw2_get_file_names (per_cu);
4580 if (file_data == NULL)
4581 continue;
4582
4583 if (htab_find (visited_not_found.get (), file_data) != NULL)
4584 continue;
4585 else if (htab_find (visited_found.get (), file_data) != NULL)
4586 {
4587 per_cu->v.quick->mark = 1;
4588 continue;
4589 }
4590
4591 for (int j = 0; j < file_data->num_file_names; ++j)
4592 {
4593 const char *this_real_name;
4594
4595 if (file_matcher (file_data->file_names[j], false))
4596 {
4597 per_cu->v.quick->mark = 1;
4598 break;
4599 }
4600
4601 /* Before we invoke realpath, which can get expensive when many
4602 files are involved, do a quick comparison of the basenames. */
4603 if (!basenames_may_differ
4604 && !file_matcher (lbasename (file_data->file_names[j]),
4605 true))
4606 continue;
4607
4608 this_real_name = dw2_get_real_path (objfile, file_data, j);
4609 if (file_matcher (this_real_name, false))
4610 {
4611 per_cu->v.quick->mark = 1;
4612 break;
4613 }
4614 }
4615
4616 void **slot = htab_find_slot (per_cu->v.quick->mark
4617 ? visited_found.get ()
4618 : visited_not_found.get (),
4619 file_data, INSERT);
4620 *slot = file_data;
4621 }
4622 }
4623
4624 static void
4625 dw2_expand_symtabs_matching
4626 (struct objfile *objfile,
4627 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4628 const lookup_name_info &lookup_name,
4629 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4630 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4631 enum search_domain kind)
4632 {
4633 struct dwarf2_per_objfile *dwarf2_per_objfile
4634 = get_dwarf2_per_objfile (objfile);
4635
4636 /* index_table is NULL if OBJF_READNOW. */
4637 if (!dwarf2_per_objfile->index_table)
4638 return;
4639
4640 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4641
4642 mapped_index &index = *dwarf2_per_objfile->index_table;
4643
4644 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4645 symbol_matcher,
4646 kind, [&] (offset_type idx)
4647 {
4648 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4649 expansion_notify, kind);
4650 return true;
4651 });
4652 }
4653
4654 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4655 symtab. */
4656
4657 static struct compunit_symtab *
4658 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4659 CORE_ADDR pc)
4660 {
4661 int i;
4662
4663 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4664 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4665 return cust;
4666
4667 if (cust->includes == NULL)
4668 return NULL;
4669
4670 for (i = 0; cust->includes[i]; ++i)
4671 {
4672 struct compunit_symtab *s = cust->includes[i];
4673
4674 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4675 if (s != NULL)
4676 return s;
4677 }
4678
4679 return NULL;
4680 }
4681
4682 static struct compunit_symtab *
4683 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4684 struct bound_minimal_symbol msymbol,
4685 CORE_ADDR pc,
4686 struct obj_section *section,
4687 int warn_if_readin)
4688 {
4689 struct dwarf2_per_cu_data *data;
4690 struct compunit_symtab *result;
4691
4692 if (!objfile->partial_symtabs->psymtabs_addrmap)
4693 return NULL;
4694
4695 CORE_ADDR baseaddr = objfile->text_section_offset ();
4696 data = (struct dwarf2_per_cu_data *) addrmap_find
4697 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4698 if (!data)
4699 return NULL;
4700
4701 if (warn_if_readin && data->v.quick->compunit_symtab)
4702 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4703 paddress (get_objfile_arch (objfile), pc));
4704
4705 result
4706 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4707 false),
4708 pc);
4709 gdb_assert (result != NULL);
4710 return result;
4711 }
4712
4713 static void
4714 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4715 void *data, int need_fullname)
4716 {
4717 struct dwarf2_per_objfile *dwarf2_per_objfile
4718 = get_dwarf2_per_objfile (objfile);
4719
4720 if (!dwarf2_per_objfile->filenames_cache)
4721 {
4722 dwarf2_per_objfile->filenames_cache.emplace ();
4723
4724 htab_up visited (htab_create_alloc (10,
4725 htab_hash_pointer, htab_eq_pointer,
4726 NULL, xcalloc, xfree));
4727
4728 /* The rule is CUs specify all the files, including those used
4729 by any TU, so there's no need to scan TUs here. We can
4730 ignore file names coming from already-expanded CUs. */
4731
4732 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4733 {
4734 if (per_cu->v.quick->compunit_symtab)
4735 {
4736 void **slot = htab_find_slot (visited.get (),
4737 per_cu->v.quick->file_names,
4738 INSERT);
4739
4740 *slot = per_cu->v.quick->file_names;
4741 }
4742 }
4743
4744 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4745 {
4746 /* We only need to look at symtabs not already expanded. */
4747 if (per_cu->v.quick->compunit_symtab)
4748 continue;
4749
4750 quick_file_names *file_data = dw2_get_file_names (per_cu);
4751 if (file_data == NULL)
4752 continue;
4753
4754 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4755 if (*slot)
4756 {
4757 /* Already visited. */
4758 continue;
4759 }
4760 *slot = file_data;
4761
4762 for (int j = 0; j < file_data->num_file_names; ++j)
4763 {
4764 const char *filename = file_data->file_names[j];
4765 dwarf2_per_objfile->filenames_cache->seen (filename);
4766 }
4767 }
4768 }
4769
4770 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4771 {
4772 gdb::unique_xmalloc_ptr<char> this_real_name;
4773
4774 if (need_fullname)
4775 this_real_name = gdb_realpath (filename);
4776 (*fun) (filename, this_real_name.get (), data);
4777 });
4778 }
4779
4780 static int
4781 dw2_has_symbols (struct objfile *objfile)
4782 {
4783 return 1;
4784 }
4785
4786 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4787 {
4788 dw2_has_symbols,
4789 dw2_find_last_source_symtab,
4790 dw2_forget_cached_source_info,
4791 dw2_map_symtabs_matching_filename,
4792 dw2_lookup_symbol,
4793 dw2_print_stats,
4794 dw2_dump,
4795 dw2_expand_symtabs_for_function,
4796 dw2_expand_all_symtabs,
4797 dw2_expand_symtabs_with_fullname,
4798 dw2_map_matching_symbols,
4799 dw2_expand_symtabs_matching,
4800 dw2_find_pc_sect_compunit_symtab,
4801 NULL,
4802 dw2_map_symbol_filenames
4803 };
4804
4805 /* DWARF-5 debug_names reader. */
4806
4807 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4808 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4809
4810 /* A helper function that reads the .debug_names section in SECTION
4811 and fills in MAP. FILENAME is the name of the file containing the
4812 section; it is used for error reporting.
4813
4814 Returns true if all went well, false otherwise. */
4815
4816 static bool
4817 read_debug_names_from_section (struct objfile *objfile,
4818 const char *filename,
4819 struct dwarf2_section_info *section,
4820 mapped_debug_names &map)
4821 {
4822 if (section->empty ())
4823 return false;
4824
4825 /* Older elfutils strip versions could keep the section in the main
4826 executable while splitting it for the separate debug info file. */
4827 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4828 return false;
4829
4830 section->read (objfile);
4831
4832 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4833
4834 const gdb_byte *addr = section->buffer;
4835
4836 bfd *const abfd = section->get_bfd_owner ();
4837
4838 unsigned int bytes_read;
4839 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4840 addr += bytes_read;
4841
4842 map.dwarf5_is_dwarf64 = bytes_read != 4;
4843 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4844 if (bytes_read + length != section->size)
4845 {
4846 /* There may be multiple per-CU indices. */
4847 warning (_("Section .debug_names in %s length %s does not match "
4848 "section length %s, ignoring .debug_names."),
4849 filename, plongest (bytes_read + length),
4850 pulongest (section->size));
4851 return false;
4852 }
4853
4854 /* The version number. */
4855 uint16_t version = read_2_bytes (abfd, addr);
4856 addr += 2;
4857 if (version != 5)
4858 {
4859 warning (_("Section .debug_names in %s has unsupported version %d, "
4860 "ignoring .debug_names."),
4861 filename, version);
4862 return false;
4863 }
4864
4865 /* Padding. */
4866 uint16_t padding = read_2_bytes (abfd, addr);
4867 addr += 2;
4868 if (padding != 0)
4869 {
4870 warning (_("Section .debug_names in %s has unsupported padding %d, "
4871 "ignoring .debug_names."),
4872 filename, padding);
4873 return false;
4874 }
4875
4876 /* comp_unit_count - The number of CUs in the CU list. */
4877 map.cu_count = read_4_bytes (abfd, addr);
4878 addr += 4;
4879
4880 /* local_type_unit_count - The number of TUs in the local TU
4881 list. */
4882 map.tu_count = read_4_bytes (abfd, addr);
4883 addr += 4;
4884
4885 /* foreign_type_unit_count - The number of TUs in the foreign TU
4886 list. */
4887 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4888 addr += 4;
4889 if (foreign_tu_count != 0)
4890 {
4891 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4892 "ignoring .debug_names."),
4893 filename, static_cast<unsigned long> (foreign_tu_count));
4894 return false;
4895 }
4896
4897 /* bucket_count - The number of hash buckets in the hash lookup
4898 table. */
4899 map.bucket_count = read_4_bytes (abfd, addr);
4900 addr += 4;
4901
4902 /* name_count - The number of unique names in the index. */
4903 map.name_count = read_4_bytes (abfd, addr);
4904 addr += 4;
4905
4906 /* abbrev_table_size - The size in bytes of the abbreviations
4907 table. */
4908 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4909 addr += 4;
4910
4911 /* augmentation_string_size - The size in bytes of the augmentation
4912 string. This value is rounded up to a multiple of 4. */
4913 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4914 addr += 4;
4915 map.augmentation_is_gdb = ((augmentation_string_size
4916 == sizeof (dwarf5_augmentation))
4917 && memcmp (addr, dwarf5_augmentation,
4918 sizeof (dwarf5_augmentation)) == 0);
4919 augmentation_string_size += (-augmentation_string_size) & 3;
4920 addr += augmentation_string_size;
4921
4922 /* List of CUs */
4923 map.cu_table_reordered = addr;
4924 addr += map.cu_count * map.offset_size;
4925
4926 /* List of Local TUs */
4927 map.tu_table_reordered = addr;
4928 addr += map.tu_count * map.offset_size;
4929
4930 /* Hash Lookup Table */
4931 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4932 addr += map.bucket_count * 4;
4933 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4934 addr += map.name_count * 4;
4935
4936 /* Name Table */
4937 map.name_table_string_offs_reordered = addr;
4938 addr += map.name_count * map.offset_size;
4939 map.name_table_entry_offs_reordered = addr;
4940 addr += map.name_count * map.offset_size;
4941
4942 const gdb_byte *abbrev_table_start = addr;
4943 for (;;)
4944 {
4945 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4946 addr += bytes_read;
4947 if (index_num == 0)
4948 break;
4949
4950 const auto insertpair
4951 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4952 if (!insertpair.second)
4953 {
4954 warning (_("Section .debug_names in %s has duplicate index %s, "
4955 "ignoring .debug_names."),
4956 filename, pulongest (index_num));
4957 return false;
4958 }
4959 mapped_debug_names::index_val &indexval = insertpair.first->second;
4960 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4961 addr += bytes_read;
4962
4963 for (;;)
4964 {
4965 mapped_debug_names::index_val::attr attr;
4966 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4967 addr += bytes_read;
4968 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4969 addr += bytes_read;
4970 if (attr.form == DW_FORM_implicit_const)
4971 {
4972 attr.implicit_const = read_signed_leb128 (abfd, addr,
4973 &bytes_read);
4974 addr += bytes_read;
4975 }
4976 if (attr.dw_idx == 0 && attr.form == 0)
4977 break;
4978 indexval.attr_vec.push_back (std::move (attr));
4979 }
4980 }
4981 if (addr != abbrev_table_start + abbrev_table_size)
4982 {
4983 warning (_("Section .debug_names in %s has abbreviation_table "
4984 "of size %s vs. written as %u, ignoring .debug_names."),
4985 filename, plongest (addr - abbrev_table_start),
4986 abbrev_table_size);
4987 return false;
4988 }
4989 map.entry_pool = addr;
4990
4991 return true;
4992 }
4993
4994 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4995 list. */
4996
4997 static void
4998 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4999 const mapped_debug_names &map,
5000 dwarf2_section_info &section,
5001 bool is_dwz)
5002 {
5003 sect_offset sect_off_prev;
5004 for (uint32_t i = 0; i <= map.cu_count; ++i)
5005 {
5006 sect_offset sect_off_next;
5007 if (i < map.cu_count)
5008 {
5009 sect_off_next
5010 = (sect_offset) (extract_unsigned_integer
5011 (map.cu_table_reordered + i * map.offset_size,
5012 map.offset_size,
5013 map.dwarf5_byte_order));
5014 }
5015 else
5016 sect_off_next = (sect_offset) section.size;
5017 if (i >= 1)
5018 {
5019 const ULONGEST length = sect_off_next - sect_off_prev;
5020 dwarf2_per_cu_data *per_cu
5021 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5022 sect_off_prev, length);
5023 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5024 }
5025 sect_off_prev = sect_off_next;
5026 }
5027 }
5028
5029 /* Read the CU list from the mapped index, and use it to create all
5030 the CU objects for this dwarf2_per_objfile. */
5031
5032 static void
5033 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5034 const mapped_debug_names &map,
5035 const mapped_debug_names &dwz_map)
5036 {
5037 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5038 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5039
5040 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5041 dwarf2_per_objfile->info,
5042 false /* is_dwz */);
5043
5044 if (dwz_map.cu_count == 0)
5045 return;
5046
5047 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5048 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5049 true /* is_dwz */);
5050 }
5051
5052 /* Read .debug_names. If everything went ok, initialize the "quick"
5053 elements of all the CUs and return true. Otherwise, return false. */
5054
5055 static bool
5056 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5057 {
5058 std::unique_ptr<mapped_debug_names> map
5059 (new mapped_debug_names (dwarf2_per_objfile));
5060 mapped_debug_names dwz_map (dwarf2_per_objfile);
5061 struct objfile *objfile = dwarf2_per_objfile->objfile;
5062
5063 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5064 &dwarf2_per_objfile->debug_names,
5065 *map))
5066 return false;
5067
5068 /* Don't use the index if it's empty. */
5069 if (map->name_count == 0)
5070 return false;
5071
5072 /* If there is a .dwz file, read it so we can get its CU list as
5073 well. */
5074 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5075 if (dwz != NULL)
5076 {
5077 if (!read_debug_names_from_section (objfile,
5078 bfd_get_filename (dwz->dwz_bfd.get ()),
5079 &dwz->debug_names, dwz_map))
5080 {
5081 warning (_("could not read '.debug_names' section from %s; skipping"),
5082 bfd_get_filename (dwz->dwz_bfd.get ()));
5083 return false;
5084 }
5085 }
5086
5087 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5088
5089 if (map->tu_count != 0)
5090 {
5091 /* We can only handle a single .debug_types when we have an
5092 index. */
5093 if (dwarf2_per_objfile->types.size () != 1)
5094 return false;
5095
5096 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5097
5098 create_signatured_type_table_from_debug_names
5099 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5100 }
5101
5102 create_addrmap_from_aranges (dwarf2_per_objfile,
5103 &dwarf2_per_objfile->debug_aranges);
5104
5105 dwarf2_per_objfile->debug_names_table = std::move (map);
5106 dwarf2_per_objfile->using_index = 1;
5107 dwarf2_per_objfile->quick_file_names_table =
5108 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5109
5110 return true;
5111 }
5112
5113 /* Type used to manage iterating over all CUs looking for a symbol for
5114 .debug_names. */
5115
5116 class dw2_debug_names_iterator
5117 {
5118 public:
5119 dw2_debug_names_iterator (const mapped_debug_names &map,
5120 gdb::optional<block_enum> block_index,
5121 domain_enum domain,
5122 const char *name)
5123 : m_map (map), m_block_index (block_index), m_domain (domain),
5124 m_addr (find_vec_in_debug_names (map, name))
5125 {}
5126
5127 dw2_debug_names_iterator (const mapped_debug_names &map,
5128 search_domain search, uint32_t namei)
5129 : m_map (map),
5130 m_search (search),
5131 m_addr (find_vec_in_debug_names (map, namei))
5132 {}
5133
5134 dw2_debug_names_iterator (const mapped_debug_names &map,
5135 block_enum block_index, domain_enum domain,
5136 uint32_t namei)
5137 : m_map (map), m_block_index (block_index), m_domain (domain),
5138 m_addr (find_vec_in_debug_names (map, namei))
5139 {}
5140
5141 /* Return the next matching CU or NULL if there are no more. */
5142 dwarf2_per_cu_data *next ();
5143
5144 private:
5145 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5146 const char *name);
5147 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5148 uint32_t namei);
5149
5150 /* The internalized form of .debug_names. */
5151 const mapped_debug_names &m_map;
5152
5153 /* If set, only look for symbols that match that block. Valid values are
5154 GLOBAL_BLOCK and STATIC_BLOCK. */
5155 const gdb::optional<block_enum> m_block_index;
5156
5157 /* The kind of symbol we're looking for. */
5158 const domain_enum m_domain = UNDEF_DOMAIN;
5159 const search_domain m_search = ALL_DOMAIN;
5160
5161 /* The list of CUs from the index entry of the symbol, or NULL if
5162 not found. */
5163 const gdb_byte *m_addr;
5164 };
5165
5166 const char *
5167 mapped_debug_names::namei_to_name (uint32_t namei) const
5168 {
5169 const ULONGEST namei_string_offs
5170 = extract_unsigned_integer ((name_table_string_offs_reordered
5171 + namei * offset_size),
5172 offset_size,
5173 dwarf5_byte_order);
5174 return read_indirect_string_at_offset
5175 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5176 }
5177
5178 /* Find a slot in .debug_names for the object named NAME. If NAME is
5179 found, return pointer to its pool data. If NAME cannot be found,
5180 return NULL. */
5181
5182 const gdb_byte *
5183 dw2_debug_names_iterator::find_vec_in_debug_names
5184 (const mapped_debug_names &map, const char *name)
5185 {
5186 int (*cmp) (const char *, const char *);
5187
5188 gdb::unique_xmalloc_ptr<char> without_params;
5189 if (current_language->la_language == language_cplus
5190 || current_language->la_language == language_fortran
5191 || current_language->la_language == language_d)
5192 {
5193 /* NAME is already canonical. Drop any qualifiers as
5194 .debug_names does not contain any. */
5195
5196 if (strchr (name, '(') != NULL)
5197 {
5198 without_params = cp_remove_params (name);
5199 if (without_params != NULL)
5200 name = without_params.get ();
5201 }
5202 }
5203
5204 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5205
5206 const uint32_t full_hash = dwarf5_djb_hash (name);
5207 uint32_t namei
5208 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5209 (map.bucket_table_reordered
5210 + (full_hash % map.bucket_count)), 4,
5211 map.dwarf5_byte_order);
5212 if (namei == 0)
5213 return NULL;
5214 --namei;
5215 if (namei >= map.name_count)
5216 {
5217 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5218 "[in module %s]"),
5219 namei, map.name_count,
5220 objfile_name (map.dwarf2_per_objfile->objfile));
5221 return NULL;
5222 }
5223
5224 for (;;)
5225 {
5226 const uint32_t namei_full_hash
5227 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5228 (map.hash_table_reordered + namei), 4,
5229 map.dwarf5_byte_order);
5230 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5231 return NULL;
5232
5233 if (full_hash == namei_full_hash)
5234 {
5235 const char *const namei_string = map.namei_to_name (namei);
5236
5237 #if 0 /* An expensive sanity check. */
5238 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5239 {
5240 complaint (_("Wrong .debug_names hash for string at index %u "
5241 "[in module %s]"),
5242 namei, objfile_name (dwarf2_per_objfile->objfile));
5243 return NULL;
5244 }
5245 #endif
5246
5247 if (cmp (namei_string, name) == 0)
5248 {
5249 const ULONGEST namei_entry_offs
5250 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5251 + namei * map.offset_size),
5252 map.offset_size, map.dwarf5_byte_order);
5253 return map.entry_pool + namei_entry_offs;
5254 }
5255 }
5256
5257 ++namei;
5258 if (namei >= map.name_count)
5259 return NULL;
5260 }
5261 }
5262
5263 const gdb_byte *
5264 dw2_debug_names_iterator::find_vec_in_debug_names
5265 (const mapped_debug_names &map, uint32_t namei)
5266 {
5267 if (namei >= map.name_count)
5268 {
5269 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5270 "[in module %s]"),
5271 namei, map.name_count,
5272 objfile_name (map.dwarf2_per_objfile->objfile));
5273 return NULL;
5274 }
5275
5276 const ULONGEST namei_entry_offs
5277 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5278 + namei * map.offset_size),
5279 map.offset_size, map.dwarf5_byte_order);
5280 return map.entry_pool + namei_entry_offs;
5281 }
5282
5283 /* See dw2_debug_names_iterator. */
5284
5285 dwarf2_per_cu_data *
5286 dw2_debug_names_iterator::next ()
5287 {
5288 if (m_addr == NULL)
5289 return NULL;
5290
5291 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5292 struct objfile *objfile = dwarf2_per_objfile->objfile;
5293 bfd *const abfd = objfile->obfd;
5294
5295 again:
5296
5297 unsigned int bytes_read;
5298 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5299 m_addr += bytes_read;
5300 if (abbrev == 0)
5301 return NULL;
5302
5303 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5304 if (indexval_it == m_map.abbrev_map.cend ())
5305 {
5306 complaint (_("Wrong .debug_names undefined abbrev code %s "
5307 "[in module %s]"),
5308 pulongest (abbrev), objfile_name (objfile));
5309 return NULL;
5310 }
5311 const mapped_debug_names::index_val &indexval = indexval_it->second;
5312 enum class symbol_linkage {
5313 unknown,
5314 static_,
5315 extern_,
5316 } symbol_linkage_ = symbol_linkage::unknown;
5317 dwarf2_per_cu_data *per_cu = NULL;
5318 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5319 {
5320 ULONGEST ull;
5321 switch (attr.form)
5322 {
5323 case DW_FORM_implicit_const:
5324 ull = attr.implicit_const;
5325 break;
5326 case DW_FORM_flag_present:
5327 ull = 1;
5328 break;
5329 case DW_FORM_udata:
5330 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5331 m_addr += bytes_read;
5332 break;
5333 default:
5334 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5335 dwarf_form_name (attr.form),
5336 objfile_name (objfile));
5337 return NULL;
5338 }
5339 switch (attr.dw_idx)
5340 {
5341 case DW_IDX_compile_unit:
5342 /* Don't crash on bad data. */
5343 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5344 {
5345 complaint (_(".debug_names entry has bad CU index %s"
5346 " [in module %s]"),
5347 pulongest (ull),
5348 objfile_name (dwarf2_per_objfile->objfile));
5349 continue;
5350 }
5351 per_cu = dwarf2_per_objfile->get_cutu (ull);
5352 break;
5353 case DW_IDX_type_unit:
5354 /* Don't crash on bad data. */
5355 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5356 {
5357 complaint (_(".debug_names entry has bad TU index %s"
5358 " [in module %s]"),
5359 pulongest (ull),
5360 objfile_name (dwarf2_per_objfile->objfile));
5361 continue;
5362 }
5363 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5364 break;
5365 case DW_IDX_GNU_internal:
5366 if (!m_map.augmentation_is_gdb)
5367 break;
5368 symbol_linkage_ = symbol_linkage::static_;
5369 break;
5370 case DW_IDX_GNU_external:
5371 if (!m_map.augmentation_is_gdb)
5372 break;
5373 symbol_linkage_ = symbol_linkage::extern_;
5374 break;
5375 }
5376 }
5377
5378 /* Skip if already read in. */
5379 if (per_cu->v.quick->compunit_symtab)
5380 goto again;
5381
5382 /* Check static vs global. */
5383 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5384 {
5385 const bool want_static = *m_block_index == STATIC_BLOCK;
5386 const bool symbol_is_static =
5387 symbol_linkage_ == symbol_linkage::static_;
5388 if (want_static != symbol_is_static)
5389 goto again;
5390 }
5391
5392 /* Match dw2_symtab_iter_next, symbol_kind
5393 and debug_names::psymbol_tag. */
5394 switch (m_domain)
5395 {
5396 case VAR_DOMAIN:
5397 switch (indexval.dwarf_tag)
5398 {
5399 case DW_TAG_variable:
5400 case DW_TAG_subprogram:
5401 /* Some types are also in VAR_DOMAIN. */
5402 case DW_TAG_typedef:
5403 case DW_TAG_structure_type:
5404 break;
5405 default:
5406 goto again;
5407 }
5408 break;
5409 case STRUCT_DOMAIN:
5410 switch (indexval.dwarf_tag)
5411 {
5412 case DW_TAG_typedef:
5413 case DW_TAG_structure_type:
5414 break;
5415 default:
5416 goto again;
5417 }
5418 break;
5419 case LABEL_DOMAIN:
5420 switch (indexval.dwarf_tag)
5421 {
5422 case 0:
5423 case DW_TAG_variable:
5424 break;
5425 default:
5426 goto again;
5427 }
5428 break;
5429 case MODULE_DOMAIN:
5430 switch (indexval.dwarf_tag)
5431 {
5432 case DW_TAG_module:
5433 break;
5434 default:
5435 goto again;
5436 }
5437 break;
5438 default:
5439 break;
5440 }
5441
5442 /* Match dw2_expand_symtabs_matching, symbol_kind and
5443 debug_names::psymbol_tag. */
5444 switch (m_search)
5445 {
5446 case VARIABLES_DOMAIN:
5447 switch (indexval.dwarf_tag)
5448 {
5449 case DW_TAG_variable:
5450 break;
5451 default:
5452 goto again;
5453 }
5454 break;
5455 case FUNCTIONS_DOMAIN:
5456 switch (indexval.dwarf_tag)
5457 {
5458 case DW_TAG_subprogram:
5459 break;
5460 default:
5461 goto again;
5462 }
5463 break;
5464 case TYPES_DOMAIN:
5465 switch (indexval.dwarf_tag)
5466 {
5467 case DW_TAG_typedef:
5468 case DW_TAG_structure_type:
5469 break;
5470 default:
5471 goto again;
5472 }
5473 break;
5474 case MODULES_DOMAIN:
5475 switch (indexval.dwarf_tag)
5476 {
5477 case DW_TAG_module:
5478 break;
5479 default:
5480 goto again;
5481 }
5482 default:
5483 break;
5484 }
5485
5486 return per_cu;
5487 }
5488
5489 static struct compunit_symtab *
5490 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5491 const char *name, domain_enum domain)
5492 {
5493 struct dwarf2_per_objfile *dwarf2_per_objfile
5494 = get_dwarf2_per_objfile (objfile);
5495
5496 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5497 if (!mapp)
5498 {
5499 /* index is NULL if OBJF_READNOW. */
5500 return NULL;
5501 }
5502 const auto &map = *mapp;
5503
5504 dw2_debug_names_iterator iter (map, block_index, domain, name);
5505
5506 struct compunit_symtab *stab_best = NULL;
5507 struct dwarf2_per_cu_data *per_cu;
5508 while ((per_cu = iter.next ()) != NULL)
5509 {
5510 struct symbol *sym, *with_opaque = NULL;
5511 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5512 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5513 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5514
5515 sym = block_find_symbol (block, name, domain,
5516 block_find_non_opaque_type_preferred,
5517 &with_opaque);
5518
5519 /* Some caution must be observed with overloaded functions and
5520 methods, since the index will not contain any overload
5521 information (but NAME might contain it). */
5522
5523 if (sym != NULL
5524 && strcmp_iw (sym->search_name (), name) == 0)
5525 return stab;
5526 if (with_opaque != NULL
5527 && strcmp_iw (with_opaque->search_name (), name) == 0)
5528 stab_best = stab;
5529
5530 /* Keep looking through other CUs. */
5531 }
5532
5533 return stab_best;
5534 }
5535
5536 /* This dumps minimal information about .debug_names. It is called
5537 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5538 uses this to verify that .debug_names has been loaded. */
5539
5540 static void
5541 dw2_debug_names_dump (struct objfile *objfile)
5542 {
5543 struct dwarf2_per_objfile *dwarf2_per_objfile
5544 = get_dwarf2_per_objfile (objfile);
5545
5546 gdb_assert (dwarf2_per_objfile->using_index);
5547 printf_filtered (".debug_names:");
5548 if (dwarf2_per_objfile->debug_names_table)
5549 printf_filtered (" exists\n");
5550 else
5551 printf_filtered (" faked for \"readnow\"\n");
5552 printf_filtered ("\n");
5553 }
5554
5555 static void
5556 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5557 const char *func_name)
5558 {
5559 struct dwarf2_per_objfile *dwarf2_per_objfile
5560 = get_dwarf2_per_objfile (objfile);
5561
5562 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5563 if (dwarf2_per_objfile->debug_names_table)
5564 {
5565 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5566
5567 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5568
5569 struct dwarf2_per_cu_data *per_cu;
5570 while ((per_cu = iter.next ()) != NULL)
5571 dw2_instantiate_symtab (per_cu, false);
5572 }
5573 }
5574
5575 static void
5576 dw2_debug_names_map_matching_symbols
5577 (struct objfile *objfile,
5578 const lookup_name_info &name, domain_enum domain,
5579 int global,
5580 gdb::function_view<symbol_found_callback_ftype> callback,
5581 symbol_compare_ftype *ordered_compare)
5582 {
5583 struct dwarf2_per_objfile *dwarf2_per_objfile
5584 = get_dwarf2_per_objfile (objfile);
5585
5586 /* debug_names_table is NULL if OBJF_READNOW. */
5587 if (!dwarf2_per_objfile->debug_names_table)
5588 return;
5589
5590 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5591 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5592
5593 const char *match_name = name.ada ().lookup_name ().c_str ();
5594 auto matcher = [&] (const char *symname)
5595 {
5596 if (ordered_compare == nullptr)
5597 return true;
5598 return ordered_compare (symname, match_name) == 0;
5599 };
5600
5601 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5602 [&] (offset_type namei)
5603 {
5604 /* The name was matched, now expand corresponding CUs that were
5605 marked. */
5606 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5607
5608 struct dwarf2_per_cu_data *per_cu;
5609 while ((per_cu = iter.next ()) != NULL)
5610 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5611 return true;
5612 });
5613
5614 /* It's a shame we couldn't do this inside the
5615 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5616 that have already been expanded. Instead, this loop matches what
5617 the psymtab code does. */
5618 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5619 {
5620 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5621 if (cust != nullptr)
5622 {
5623 const struct block *block
5624 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5625 if (!iterate_over_symbols_terminated (block, name,
5626 domain, callback))
5627 break;
5628 }
5629 }
5630 }
5631
5632 static void
5633 dw2_debug_names_expand_symtabs_matching
5634 (struct objfile *objfile,
5635 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5636 const lookup_name_info &lookup_name,
5637 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5638 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5639 enum search_domain kind)
5640 {
5641 struct dwarf2_per_objfile *dwarf2_per_objfile
5642 = get_dwarf2_per_objfile (objfile);
5643
5644 /* debug_names_table is NULL if OBJF_READNOW. */
5645 if (!dwarf2_per_objfile->debug_names_table)
5646 return;
5647
5648 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5649
5650 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5651
5652 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5653 symbol_matcher,
5654 kind, [&] (offset_type namei)
5655 {
5656 /* The name was matched, now expand corresponding CUs that were
5657 marked. */
5658 dw2_debug_names_iterator iter (map, kind, namei);
5659
5660 struct dwarf2_per_cu_data *per_cu;
5661 while ((per_cu = iter.next ()) != NULL)
5662 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5663 expansion_notify);
5664 return true;
5665 });
5666 }
5667
5668 const struct quick_symbol_functions dwarf2_debug_names_functions =
5669 {
5670 dw2_has_symbols,
5671 dw2_find_last_source_symtab,
5672 dw2_forget_cached_source_info,
5673 dw2_map_symtabs_matching_filename,
5674 dw2_debug_names_lookup_symbol,
5675 dw2_print_stats,
5676 dw2_debug_names_dump,
5677 dw2_debug_names_expand_symtabs_for_function,
5678 dw2_expand_all_symtabs,
5679 dw2_expand_symtabs_with_fullname,
5680 dw2_debug_names_map_matching_symbols,
5681 dw2_debug_names_expand_symtabs_matching,
5682 dw2_find_pc_sect_compunit_symtab,
5683 NULL,
5684 dw2_map_symbol_filenames
5685 };
5686
5687 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5688 to either a dwarf2_per_objfile or dwz_file object. */
5689
5690 template <typename T>
5691 static gdb::array_view<const gdb_byte>
5692 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5693 {
5694 dwarf2_section_info *section = &section_owner->gdb_index;
5695
5696 if (section->empty ())
5697 return {};
5698
5699 /* Older elfutils strip versions could keep the section in the main
5700 executable while splitting it for the separate debug info file. */
5701 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5702 return {};
5703
5704 section->read (obj);
5705
5706 /* dwarf2_section_info::size is a bfd_size_type, while
5707 gdb::array_view works with size_t. On 32-bit hosts, with
5708 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5709 is 32-bit. So we need an explicit narrowing conversion here.
5710 This is fine, because it's impossible to allocate or mmap an
5711 array/buffer larger than what size_t can represent. */
5712 return gdb::make_array_view (section->buffer, section->size);
5713 }
5714
5715 /* Lookup the index cache for the contents of the index associated to
5716 DWARF2_OBJ. */
5717
5718 static gdb::array_view<const gdb_byte>
5719 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5720 {
5721 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5722 if (build_id == nullptr)
5723 return {};
5724
5725 return global_index_cache.lookup_gdb_index (build_id,
5726 &dwarf2_obj->index_cache_res);
5727 }
5728
5729 /* Same as the above, but for DWZ. */
5730
5731 static gdb::array_view<const gdb_byte>
5732 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5733 {
5734 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5735 if (build_id == nullptr)
5736 return {};
5737
5738 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5739 }
5740
5741 /* See symfile.h. */
5742
5743 bool
5744 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5745 {
5746 struct dwarf2_per_objfile *dwarf2_per_objfile
5747 = get_dwarf2_per_objfile (objfile);
5748
5749 /* If we're about to read full symbols, don't bother with the
5750 indices. In this case we also don't care if some other debug
5751 format is making psymtabs, because they are all about to be
5752 expanded anyway. */
5753 if ((objfile->flags & OBJF_READNOW))
5754 {
5755 dwarf2_per_objfile->using_index = 1;
5756 create_all_comp_units (dwarf2_per_objfile);
5757 create_all_type_units (dwarf2_per_objfile);
5758 dwarf2_per_objfile->quick_file_names_table
5759 = create_quick_file_names_table
5760 (dwarf2_per_objfile->all_comp_units.size ());
5761
5762 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5763 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5764 {
5765 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5766
5767 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5768 struct dwarf2_per_cu_quick_data);
5769 }
5770
5771 /* Return 1 so that gdb sees the "quick" functions. However,
5772 these functions will be no-ops because we will have expanded
5773 all symtabs. */
5774 *index_kind = dw_index_kind::GDB_INDEX;
5775 return true;
5776 }
5777
5778 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5779 {
5780 *index_kind = dw_index_kind::DEBUG_NAMES;
5781 return true;
5782 }
5783
5784 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5785 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5786 get_gdb_index_contents_from_section<dwz_file>))
5787 {
5788 *index_kind = dw_index_kind::GDB_INDEX;
5789 return true;
5790 }
5791
5792 /* ... otherwise, try to find the index in the index cache. */
5793 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5794 get_gdb_index_contents_from_cache,
5795 get_gdb_index_contents_from_cache_dwz))
5796 {
5797 global_index_cache.hit ();
5798 *index_kind = dw_index_kind::GDB_INDEX;
5799 return true;
5800 }
5801
5802 global_index_cache.miss ();
5803 return false;
5804 }
5805
5806 \f
5807
5808 /* Build a partial symbol table. */
5809
5810 void
5811 dwarf2_build_psymtabs (struct objfile *objfile)
5812 {
5813 struct dwarf2_per_objfile *dwarf2_per_objfile
5814 = get_dwarf2_per_objfile (objfile);
5815
5816 init_psymbol_list (objfile, 1024);
5817
5818 try
5819 {
5820 /* This isn't really ideal: all the data we allocate on the
5821 objfile's obstack is still uselessly kept around. However,
5822 freeing it seems unsafe. */
5823 psymtab_discarder psymtabs (objfile);
5824 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5825 psymtabs.keep ();
5826
5827 /* (maybe) store an index in the cache. */
5828 global_index_cache.store (dwarf2_per_objfile);
5829 }
5830 catch (const gdb_exception_error &except)
5831 {
5832 exception_print (gdb_stderr, except);
5833 }
5834 }
5835
5836 /* Find the base address of the compilation unit for range lists and
5837 location lists. It will normally be specified by DW_AT_low_pc.
5838 In DWARF-3 draft 4, the base address could be overridden by
5839 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5840 compilation units with discontinuous ranges. */
5841
5842 static void
5843 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5844 {
5845 struct attribute *attr;
5846
5847 cu->base_known = 0;
5848 cu->base_address = 0;
5849
5850 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5851 if (attr != nullptr)
5852 {
5853 cu->base_address = attr->value_as_address ();
5854 cu->base_known = 1;
5855 }
5856 else
5857 {
5858 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5859 if (attr != nullptr)
5860 {
5861 cu->base_address = attr->value_as_address ();
5862 cu->base_known = 1;
5863 }
5864 }
5865 }
5866
5867 /* Helper function that returns the proper abbrev section for
5868 THIS_CU. */
5869
5870 static struct dwarf2_section_info *
5871 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5872 {
5873 struct dwarf2_section_info *abbrev;
5874 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5875
5876 if (this_cu->is_dwz)
5877 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5878 else
5879 abbrev = &dwarf2_per_objfile->abbrev;
5880
5881 return abbrev;
5882 }
5883
5884 /* Fetch the abbreviation table offset from a comp or type unit header. */
5885
5886 static sect_offset
5887 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5888 struct dwarf2_section_info *section,
5889 sect_offset sect_off)
5890 {
5891 bfd *abfd = section->get_bfd_owner ();
5892 const gdb_byte *info_ptr;
5893 unsigned int initial_length_size, offset_size;
5894 uint16_t version;
5895
5896 section->read (dwarf2_per_objfile->objfile);
5897 info_ptr = section->buffer + to_underlying (sect_off);
5898 read_initial_length (abfd, info_ptr, &initial_length_size);
5899 offset_size = initial_length_size == 4 ? 4 : 8;
5900 info_ptr += initial_length_size;
5901
5902 version = read_2_bytes (abfd, info_ptr);
5903 info_ptr += 2;
5904 if (version >= 5)
5905 {
5906 /* Skip unit type and address size. */
5907 info_ptr += 2;
5908 }
5909
5910 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5911 }
5912
5913 /* A partial symtab that is used only for include files. */
5914 struct dwarf2_include_psymtab : public partial_symtab
5915 {
5916 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5917 : partial_symtab (filename, objfile)
5918 {
5919 }
5920
5921 void read_symtab (struct objfile *objfile) override
5922 {
5923 expand_psymtab (objfile);
5924 }
5925
5926 void expand_psymtab (struct objfile *objfile) override
5927 {
5928 if (m_readin)
5929 return;
5930 /* It's an include file, no symbols to read for it.
5931 Everything is in the parent symtab. */
5932 read_dependencies (objfile);
5933 m_readin = true;
5934 }
5935
5936 bool readin_p () const override
5937 {
5938 return m_readin;
5939 }
5940
5941 struct compunit_symtab *get_compunit_symtab () const override
5942 {
5943 return nullptr;
5944 }
5945
5946 private:
5947
5948 bool m_readin = false;
5949 };
5950
5951 /* Allocate a new partial symtab for file named NAME and mark this new
5952 partial symtab as being an include of PST. */
5953
5954 static void
5955 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5956 struct objfile *objfile)
5957 {
5958 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5959
5960 if (!IS_ABSOLUTE_PATH (subpst->filename))
5961 {
5962 /* It shares objfile->objfile_obstack. */
5963 subpst->dirname = pst->dirname;
5964 }
5965
5966 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5967 subpst->dependencies[0] = pst;
5968 subpst->number_of_dependencies = 1;
5969 }
5970
5971 /* Read the Line Number Program data and extract the list of files
5972 included by the source file represented by PST. Build an include
5973 partial symtab for each of these included files. */
5974
5975 static void
5976 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5977 struct die_info *die,
5978 dwarf2_psymtab *pst)
5979 {
5980 line_header_up lh;
5981 struct attribute *attr;
5982
5983 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5984 if (attr != nullptr)
5985 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5986 if (lh == NULL)
5987 return; /* No linetable, so no includes. */
5988
5989 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5990 that we pass in the raw text_low here; that is ok because we're
5991 only decoding the line table to make include partial symtabs, and
5992 so the addresses aren't really used. */
5993 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5994 pst->raw_text_low (), 1);
5995 }
5996
5997 static hashval_t
5998 hash_signatured_type (const void *item)
5999 {
6000 const struct signatured_type *sig_type
6001 = (const struct signatured_type *) item;
6002
6003 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6004 return sig_type->signature;
6005 }
6006
6007 static int
6008 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6009 {
6010 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6011 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6012
6013 return lhs->signature == rhs->signature;
6014 }
6015
6016 /* Allocate a hash table for signatured types. */
6017
6018 static htab_up
6019 allocate_signatured_type_table ()
6020 {
6021 return htab_up (htab_create_alloc (41,
6022 hash_signatured_type,
6023 eq_signatured_type,
6024 NULL, xcalloc, xfree));
6025 }
6026
6027 /* A helper function to add a signatured type CU to a table. */
6028
6029 static int
6030 add_signatured_type_cu_to_table (void **slot, void *datum)
6031 {
6032 struct signatured_type *sigt = (struct signatured_type *) *slot;
6033 std::vector<signatured_type *> *all_type_units
6034 = (std::vector<signatured_type *> *) datum;
6035
6036 all_type_units->push_back (sigt);
6037
6038 return 1;
6039 }
6040
6041 /* A helper for create_debug_types_hash_table. Read types from SECTION
6042 and fill them into TYPES_HTAB. It will process only type units,
6043 therefore DW_UT_type. */
6044
6045 static void
6046 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6047 struct dwo_file *dwo_file,
6048 dwarf2_section_info *section, htab_up &types_htab,
6049 rcuh_kind section_kind)
6050 {
6051 struct objfile *objfile = dwarf2_per_objfile->objfile;
6052 struct dwarf2_section_info *abbrev_section;
6053 bfd *abfd;
6054 const gdb_byte *info_ptr, *end_ptr;
6055
6056 abbrev_section = (dwo_file != NULL
6057 ? &dwo_file->sections.abbrev
6058 : &dwarf2_per_objfile->abbrev);
6059
6060 if (dwarf_read_debug)
6061 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6062 section->get_name (),
6063 abbrev_section->get_file_name ());
6064
6065 section->read (objfile);
6066 info_ptr = section->buffer;
6067
6068 if (info_ptr == NULL)
6069 return;
6070
6071 /* We can't set abfd until now because the section may be empty or
6072 not present, in which case the bfd is unknown. */
6073 abfd = section->get_bfd_owner ();
6074
6075 /* We don't use cutu_reader here because we don't need to read
6076 any dies: the signature is in the header. */
6077
6078 end_ptr = info_ptr + section->size;
6079 while (info_ptr < end_ptr)
6080 {
6081 struct signatured_type *sig_type;
6082 struct dwo_unit *dwo_tu;
6083 void **slot;
6084 const gdb_byte *ptr = info_ptr;
6085 struct comp_unit_head header;
6086 unsigned int length;
6087
6088 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6089
6090 /* Initialize it due to a false compiler warning. */
6091 header.signature = -1;
6092 header.type_cu_offset_in_tu = (cu_offset) -1;
6093
6094 /* We need to read the type's signature in order to build the hash
6095 table, but we don't need anything else just yet. */
6096
6097 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6098 abbrev_section, ptr, section_kind);
6099
6100 length = header.get_length ();
6101
6102 /* Skip dummy type units. */
6103 if (ptr >= info_ptr + length
6104 || peek_abbrev_code (abfd, ptr) == 0
6105 || header.unit_type != DW_UT_type)
6106 {
6107 info_ptr += length;
6108 continue;
6109 }
6110
6111 if (types_htab == NULL)
6112 {
6113 if (dwo_file)
6114 types_htab = allocate_dwo_unit_table ();
6115 else
6116 types_htab = allocate_signatured_type_table ();
6117 }
6118
6119 if (dwo_file)
6120 {
6121 sig_type = NULL;
6122 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6123 struct dwo_unit);
6124 dwo_tu->dwo_file = dwo_file;
6125 dwo_tu->signature = header.signature;
6126 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6127 dwo_tu->section = section;
6128 dwo_tu->sect_off = sect_off;
6129 dwo_tu->length = length;
6130 }
6131 else
6132 {
6133 /* N.B.: type_offset is not usable if this type uses a DWO file.
6134 The real type_offset is in the DWO file. */
6135 dwo_tu = NULL;
6136 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6137 struct signatured_type);
6138 sig_type->signature = header.signature;
6139 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6140 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6141 sig_type->per_cu.is_debug_types = 1;
6142 sig_type->per_cu.section = section;
6143 sig_type->per_cu.sect_off = sect_off;
6144 sig_type->per_cu.length = length;
6145 }
6146
6147 slot = htab_find_slot (types_htab.get (),
6148 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6149 INSERT);
6150 gdb_assert (slot != NULL);
6151 if (*slot != NULL)
6152 {
6153 sect_offset dup_sect_off;
6154
6155 if (dwo_file)
6156 {
6157 const struct dwo_unit *dup_tu
6158 = (const struct dwo_unit *) *slot;
6159
6160 dup_sect_off = dup_tu->sect_off;
6161 }
6162 else
6163 {
6164 const struct signatured_type *dup_tu
6165 = (const struct signatured_type *) *slot;
6166
6167 dup_sect_off = dup_tu->per_cu.sect_off;
6168 }
6169
6170 complaint (_("debug type entry at offset %s is duplicate to"
6171 " the entry at offset %s, signature %s"),
6172 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6173 hex_string (header.signature));
6174 }
6175 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6176
6177 if (dwarf_read_debug > 1)
6178 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6179 sect_offset_str (sect_off),
6180 hex_string (header.signature));
6181
6182 info_ptr += length;
6183 }
6184 }
6185
6186 /* Create the hash table of all entries in the .debug_types
6187 (or .debug_types.dwo) section(s).
6188 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6189 otherwise it is NULL.
6190
6191 The result is a pointer to the hash table or NULL if there are no types.
6192
6193 Note: This function processes DWO files only, not DWP files. */
6194
6195 static void
6196 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6197 struct dwo_file *dwo_file,
6198 gdb::array_view<dwarf2_section_info> type_sections,
6199 htab_up &types_htab)
6200 {
6201 for (dwarf2_section_info &section : type_sections)
6202 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6203 types_htab, rcuh_kind::TYPE);
6204 }
6205
6206 /* Create the hash table of all entries in the .debug_types section,
6207 and initialize all_type_units.
6208 The result is zero if there is an error (e.g. missing .debug_types section),
6209 otherwise non-zero. */
6210
6211 static int
6212 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6213 {
6214 htab_up types_htab;
6215
6216 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6217 &dwarf2_per_objfile->info, types_htab,
6218 rcuh_kind::COMPILE);
6219 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6220 dwarf2_per_objfile->types, types_htab);
6221 if (types_htab == NULL)
6222 {
6223 dwarf2_per_objfile->signatured_types = NULL;
6224 return 0;
6225 }
6226
6227 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6228
6229 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6230 dwarf2_per_objfile->all_type_units.reserve
6231 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6232
6233 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6234 add_signatured_type_cu_to_table,
6235 &dwarf2_per_objfile->all_type_units);
6236
6237 return 1;
6238 }
6239
6240 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6241 If SLOT is non-NULL, it is the entry to use in the hash table.
6242 Otherwise we find one. */
6243
6244 static struct signatured_type *
6245 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6246 void **slot)
6247 {
6248 struct objfile *objfile = dwarf2_per_objfile->objfile;
6249
6250 if (dwarf2_per_objfile->all_type_units.size ()
6251 == dwarf2_per_objfile->all_type_units.capacity ())
6252 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6253
6254 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6255 struct signatured_type);
6256
6257 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6258 sig_type->signature = sig;
6259 sig_type->per_cu.is_debug_types = 1;
6260 if (dwarf2_per_objfile->using_index)
6261 {
6262 sig_type->per_cu.v.quick =
6263 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6264 struct dwarf2_per_cu_quick_data);
6265 }
6266
6267 if (slot == NULL)
6268 {
6269 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6270 sig_type, INSERT);
6271 }
6272 gdb_assert (*slot == NULL);
6273 *slot = sig_type;
6274 /* The rest of sig_type must be filled in by the caller. */
6275 return sig_type;
6276 }
6277
6278 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6279 Fill in SIG_ENTRY with DWO_ENTRY. */
6280
6281 static void
6282 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6283 struct signatured_type *sig_entry,
6284 struct dwo_unit *dwo_entry)
6285 {
6286 /* Make sure we're not clobbering something we don't expect to. */
6287 gdb_assert (! sig_entry->per_cu.queued);
6288 gdb_assert (sig_entry->per_cu.cu == NULL);
6289 if (dwarf2_per_objfile->using_index)
6290 {
6291 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6292 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6293 }
6294 else
6295 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6296 gdb_assert (sig_entry->signature == dwo_entry->signature);
6297 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6298 gdb_assert (sig_entry->type_unit_group == NULL);
6299 gdb_assert (sig_entry->dwo_unit == NULL);
6300
6301 sig_entry->per_cu.section = dwo_entry->section;
6302 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6303 sig_entry->per_cu.length = dwo_entry->length;
6304 sig_entry->per_cu.reading_dwo_directly = 1;
6305 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6306 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6307 sig_entry->dwo_unit = dwo_entry;
6308 }
6309
6310 /* Subroutine of lookup_signatured_type.
6311 If we haven't read the TU yet, create the signatured_type data structure
6312 for a TU to be read in directly from a DWO file, bypassing the stub.
6313 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6314 using .gdb_index, then when reading a CU we want to stay in the DWO file
6315 containing that CU. Otherwise we could end up reading several other DWO
6316 files (due to comdat folding) to process the transitive closure of all the
6317 mentioned TUs, and that can be slow. The current DWO file will have every
6318 type signature that it needs.
6319 We only do this for .gdb_index because in the psymtab case we already have
6320 to read all the DWOs to build the type unit groups. */
6321
6322 static struct signatured_type *
6323 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6324 {
6325 struct dwarf2_per_objfile *dwarf2_per_objfile
6326 = cu->per_cu->dwarf2_per_objfile;
6327 struct dwo_file *dwo_file;
6328 struct dwo_unit find_dwo_entry, *dwo_entry;
6329 struct signatured_type find_sig_entry, *sig_entry;
6330 void **slot;
6331
6332 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6333
6334 /* If TU skeletons have been removed then we may not have read in any
6335 TUs yet. */
6336 if (dwarf2_per_objfile->signatured_types == NULL)
6337 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6338
6339 /* We only ever need to read in one copy of a signatured type.
6340 Use the global signatured_types array to do our own comdat-folding
6341 of types. If this is the first time we're reading this TU, and
6342 the TU has an entry in .gdb_index, replace the recorded data from
6343 .gdb_index with this TU. */
6344
6345 find_sig_entry.signature = sig;
6346 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6347 &find_sig_entry, INSERT);
6348 sig_entry = (struct signatured_type *) *slot;
6349
6350 /* We can get here with the TU already read, *or* in the process of being
6351 read. Don't reassign the global entry to point to this DWO if that's
6352 the case. Also note that if the TU is already being read, it may not
6353 have come from a DWO, the program may be a mix of Fission-compiled
6354 code and non-Fission-compiled code. */
6355
6356 /* Have we already tried to read this TU?
6357 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6358 needn't exist in the global table yet). */
6359 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6360 return sig_entry;
6361
6362 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6363 dwo_unit of the TU itself. */
6364 dwo_file = cu->dwo_unit->dwo_file;
6365
6366 /* Ok, this is the first time we're reading this TU. */
6367 if (dwo_file->tus == NULL)
6368 return NULL;
6369 find_dwo_entry.signature = sig;
6370 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6371 &find_dwo_entry);
6372 if (dwo_entry == NULL)
6373 return NULL;
6374
6375 /* If the global table doesn't have an entry for this TU, add one. */
6376 if (sig_entry == NULL)
6377 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6378
6379 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6380 sig_entry->per_cu.tu_read = 1;
6381 return sig_entry;
6382 }
6383
6384 /* Subroutine of lookup_signatured_type.
6385 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6386 then try the DWP file. If the TU stub (skeleton) has been removed then
6387 it won't be in .gdb_index. */
6388
6389 static struct signatured_type *
6390 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6391 {
6392 struct dwarf2_per_objfile *dwarf2_per_objfile
6393 = cu->per_cu->dwarf2_per_objfile;
6394 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6395 struct dwo_unit *dwo_entry;
6396 struct signatured_type find_sig_entry, *sig_entry;
6397 void **slot;
6398
6399 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6400 gdb_assert (dwp_file != NULL);
6401
6402 /* If TU skeletons have been removed then we may not have read in any
6403 TUs yet. */
6404 if (dwarf2_per_objfile->signatured_types == NULL)
6405 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6406
6407 find_sig_entry.signature = sig;
6408 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6409 &find_sig_entry, INSERT);
6410 sig_entry = (struct signatured_type *) *slot;
6411
6412 /* Have we already tried to read this TU?
6413 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6414 needn't exist in the global table yet). */
6415 if (sig_entry != NULL)
6416 return sig_entry;
6417
6418 if (dwp_file->tus == NULL)
6419 return NULL;
6420 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6421 sig, 1 /* is_debug_types */);
6422 if (dwo_entry == NULL)
6423 return NULL;
6424
6425 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6426 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6427
6428 return sig_entry;
6429 }
6430
6431 /* Lookup a signature based type for DW_FORM_ref_sig8.
6432 Returns NULL if signature SIG is not present in the table.
6433 It is up to the caller to complain about this. */
6434
6435 static struct signatured_type *
6436 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6437 {
6438 struct dwarf2_per_objfile *dwarf2_per_objfile
6439 = cu->per_cu->dwarf2_per_objfile;
6440
6441 if (cu->dwo_unit
6442 && dwarf2_per_objfile->using_index)
6443 {
6444 /* We're in a DWO/DWP file, and we're using .gdb_index.
6445 These cases require special processing. */
6446 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6447 return lookup_dwo_signatured_type (cu, sig);
6448 else
6449 return lookup_dwp_signatured_type (cu, sig);
6450 }
6451 else
6452 {
6453 struct signatured_type find_entry, *entry;
6454
6455 if (dwarf2_per_objfile->signatured_types == NULL)
6456 return NULL;
6457 find_entry.signature = sig;
6458 entry = ((struct signatured_type *)
6459 htab_find (dwarf2_per_objfile->signatured_types.get (),
6460 &find_entry));
6461 return entry;
6462 }
6463 }
6464
6465 /* Return the address base of the compile unit, which, if exists, is stored
6466 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6467 static gdb::optional<ULONGEST>
6468 lookup_addr_base (struct die_info *comp_unit_die)
6469 {
6470 struct attribute *attr;
6471 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6472 if (attr == nullptr)
6473 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6474 if (attr == nullptr)
6475 return gdb::optional<ULONGEST> ();
6476 return DW_UNSND (attr);
6477 }
6478
6479 /* Return range lists base of the compile unit, which, if exists, is stored
6480 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6481 static ULONGEST
6482 lookup_ranges_base (struct die_info *comp_unit_die)
6483 {
6484 struct attribute *attr;
6485 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6486 if (attr == nullptr)
6487 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6488 if (attr == nullptr)
6489 return 0;
6490 return DW_UNSND (attr);
6491 }
6492
6493 /* Low level DIE reading support. */
6494
6495 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6496
6497 static void
6498 init_cu_die_reader (struct die_reader_specs *reader,
6499 struct dwarf2_cu *cu,
6500 struct dwarf2_section_info *section,
6501 struct dwo_file *dwo_file,
6502 struct abbrev_table *abbrev_table)
6503 {
6504 gdb_assert (section->readin && section->buffer != NULL);
6505 reader->abfd = section->get_bfd_owner ();
6506 reader->cu = cu;
6507 reader->dwo_file = dwo_file;
6508 reader->die_section = section;
6509 reader->buffer = section->buffer;
6510 reader->buffer_end = section->buffer + section->size;
6511 reader->abbrev_table = abbrev_table;
6512 }
6513
6514 /* Subroutine of cutu_reader to simplify it.
6515 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6516 There's just a lot of work to do, and cutu_reader is big enough
6517 already.
6518
6519 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6520 from it to the DIE in the DWO. If NULL we are skipping the stub.
6521 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6522 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6523 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6524 STUB_COMP_DIR may be non-NULL.
6525 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6526 are filled in with the info of the DIE from the DWO file.
6527 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6528 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6529 kept around for at least as long as *RESULT_READER.
6530
6531 The result is non-zero if a valid (non-dummy) DIE was found. */
6532
6533 static int
6534 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6535 struct dwo_unit *dwo_unit,
6536 struct die_info *stub_comp_unit_die,
6537 const char *stub_comp_dir,
6538 struct die_reader_specs *result_reader,
6539 const gdb_byte **result_info_ptr,
6540 struct die_info **result_comp_unit_die,
6541 abbrev_table_up *result_dwo_abbrev_table)
6542 {
6543 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6544 struct objfile *objfile = dwarf2_per_objfile->objfile;
6545 struct dwarf2_cu *cu = this_cu->cu;
6546 bfd *abfd;
6547 const gdb_byte *begin_info_ptr, *info_ptr;
6548 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6549 int i,num_extra_attrs;
6550 struct dwarf2_section_info *dwo_abbrev_section;
6551 struct die_info *comp_unit_die;
6552
6553 /* At most one of these may be provided. */
6554 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6555
6556 /* These attributes aren't processed until later:
6557 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6558 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6559 referenced later. However, these attributes are found in the stub
6560 which we won't have later. In order to not impose this complication
6561 on the rest of the code, we read them here and copy them to the
6562 DWO CU/TU die. */
6563
6564 stmt_list = NULL;
6565 low_pc = NULL;
6566 high_pc = NULL;
6567 ranges = NULL;
6568 comp_dir = NULL;
6569
6570 if (stub_comp_unit_die != NULL)
6571 {
6572 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6573 DWO file. */
6574 if (! this_cu->is_debug_types)
6575 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6576 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6577 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6578 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6579 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6580
6581 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6582
6583 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6584 here (if needed). We need the value before we can process
6585 DW_AT_ranges. */
6586 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6587 }
6588 else if (stub_comp_dir != NULL)
6589 {
6590 /* Reconstruct the comp_dir attribute to simplify the code below. */
6591 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6592 comp_dir->name = DW_AT_comp_dir;
6593 comp_dir->form = DW_FORM_string;
6594 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6595 DW_STRING (comp_dir) = stub_comp_dir;
6596 }
6597
6598 /* Set up for reading the DWO CU/TU. */
6599 cu->dwo_unit = dwo_unit;
6600 dwarf2_section_info *section = dwo_unit->section;
6601 section->read (objfile);
6602 abfd = section->get_bfd_owner ();
6603 begin_info_ptr = info_ptr = (section->buffer
6604 + to_underlying (dwo_unit->sect_off));
6605 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6606
6607 if (this_cu->is_debug_types)
6608 {
6609 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6610
6611 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6612 &cu->header, section,
6613 dwo_abbrev_section,
6614 info_ptr, rcuh_kind::TYPE);
6615 /* This is not an assert because it can be caused by bad debug info. */
6616 if (sig_type->signature != cu->header.signature)
6617 {
6618 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6619 " TU at offset %s [in module %s]"),
6620 hex_string (sig_type->signature),
6621 hex_string (cu->header.signature),
6622 sect_offset_str (dwo_unit->sect_off),
6623 bfd_get_filename (abfd));
6624 }
6625 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6626 /* For DWOs coming from DWP files, we don't know the CU length
6627 nor the type's offset in the TU until now. */
6628 dwo_unit->length = cu->header.get_length ();
6629 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6630
6631 /* Establish the type offset that can be used to lookup the type.
6632 For DWO files, we don't know it until now. */
6633 sig_type->type_offset_in_section
6634 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6635 }
6636 else
6637 {
6638 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6639 &cu->header, section,
6640 dwo_abbrev_section,
6641 info_ptr, rcuh_kind::COMPILE);
6642 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6643 /* For DWOs coming from DWP files, we don't know the CU length
6644 until now. */
6645 dwo_unit->length = cu->header.get_length ();
6646 }
6647
6648 *result_dwo_abbrev_table
6649 = abbrev_table::read (objfile, dwo_abbrev_section,
6650 cu->header.abbrev_sect_off);
6651 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6652 result_dwo_abbrev_table->get ());
6653
6654 /* Read in the die, but leave space to copy over the attributes
6655 from the stub. This has the benefit of simplifying the rest of
6656 the code - all the work to maintain the illusion of a single
6657 DW_TAG_{compile,type}_unit DIE is done here. */
6658 num_extra_attrs = ((stmt_list != NULL)
6659 + (low_pc != NULL)
6660 + (high_pc != NULL)
6661 + (ranges != NULL)
6662 + (comp_dir != NULL));
6663 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6664 num_extra_attrs);
6665
6666 /* Copy over the attributes from the stub to the DIE we just read in. */
6667 comp_unit_die = *result_comp_unit_die;
6668 i = comp_unit_die->num_attrs;
6669 if (stmt_list != NULL)
6670 comp_unit_die->attrs[i++] = *stmt_list;
6671 if (low_pc != NULL)
6672 comp_unit_die->attrs[i++] = *low_pc;
6673 if (high_pc != NULL)
6674 comp_unit_die->attrs[i++] = *high_pc;
6675 if (ranges != NULL)
6676 comp_unit_die->attrs[i++] = *ranges;
6677 if (comp_dir != NULL)
6678 comp_unit_die->attrs[i++] = *comp_dir;
6679 comp_unit_die->num_attrs += num_extra_attrs;
6680
6681 if (dwarf_die_debug)
6682 {
6683 fprintf_unfiltered (gdb_stdlog,
6684 "Read die from %s@0x%x of %s:\n",
6685 section->get_name (),
6686 (unsigned) (begin_info_ptr - section->buffer),
6687 bfd_get_filename (abfd));
6688 dump_die (comp_unit_die, dwarf_die_debug);
6689 }
6690
6691 /* Skip dummy compilation units. */
6692 if (info_ptr >= begin_info_ptr + dwo_unit->length
6693 || peek_abbrev_code (abfd, info_ptr) == 0)
6694 return 0;
6695
6696 *result_info_ptr = info_ptr;
6697 return 1;
6698 }
6699
6700 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6701 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6702 signature is part of the header. */
6703 static gdb::optional<ULONGEST>
6704 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6705 {
6706 if (cu->header.version >= 5)
6707 return cu->header.signature;
6708 struct attribute *attr;
6709 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6710 if (attr == nullptr)
6711 return gdb::optional<ULONGEST> ();
6712 return DW_UNSND (attr);
6713 }
6714
6715 /* Subroutine of cutu_reader to simplify it.
6716 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6717 Returns NULL if the specified DWO unit cannot be found. */
6718
6719 static struct dwo_unit *
6720 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6721 struct die_info *comp_unit_die,
6722 const char *dwo_name)
6723 {
6724 struct dwarf2_cu *cu = this_cu->cu;
6725 struct dwo_unit *dwo_unit;
6726 const char *comp_dir;
6727
6728 gdb_assert (cu != NULL);
6729
6730 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6731 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6732 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6733
6734 if (this_cu->is_debug_types)
6735 {
6736 struct signatured_type *sig_type;
6737
6738 /* Since this_cu is the first member of struct signatured_type,
6739 we can go from a pointer to one to a pointer to the other. */
6740 sig_type = (struct signatured_type *) this_cu;
6741 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6742 }
6743 else
6744 {
6745 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6746 if (!signature.has_value ())
6747 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6748 " [in module %s]"),
6749 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6750 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6751 *signature);
6752 }
6753
6754 return dwo_unit;
6755 }
6756
6757 /* Subroutine of cutu_reader to simplify it.
6758 See it for a description of the parameters.
6759 Read a TU directly from a DWO file, bypassing the stub. */
6760
6761 void
6762 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6763 int use_existing_cu)
6764 {
6765 struct signatured_type *sig_type;
6766
6767 /* Verify we can do the following downcast, and that we have the
6768 data we need. */
6769 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6770 sig_type = (struct signatured_type *) this_cu;
6771 gdb_assert (sig_type->dwo_unit != NULL);
6772
6773 if (use_existing_cu && this_cu->cu != NULL)
6774 {
6775 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6776 /* There's no need to do the rereading_dwo_cu handling that
6777 cutu_reader does since we don't read the stub. */
6778 }
6779 else
6780 {
6781 /* If !use_existing_cu, this_cu->cu must be NULL. */
6782 gdb_assert (this_cu->cu == NULL);
6783 m_new_cu.reset (new dwarf2_cu (this_cu));
6784 }
6785
6786 /* A future optimization, if needed, would be to use an existing
6787 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6788 could share abbrev tables. */
6789
6790 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6791 NULL /* stub_comp_unit_die */,
6792 sig_type->dwo_unit->dwo_file->comp_dir,
6793 this, &info_ptr,
6794 &comp_unit_die,
6795 &m_dwo_abbrev_table) == 0)
6796 {
6797 /* Dummy die. */
6798 dummy_p = true;
6799 }
6800 }
6801
6802 /* Initialize a CU (or TU) and read its DIEs.
6803 If the CU defers to a DWO file, read the DWO file as well.
6804
6805 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6806 Otherwise the table specified in the comp unit header is read in and used.
6807 This is an optimization for when we already have the abbrev table.
6808
6809 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6810 Otherwise, a new CU is allocated with xmalloc. */
6811
6812 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6813 struct abbrev_table *abbrev_table,
6814 int use_existing_cu,
6815 bool skip_partial)
6816 : die_reader_specs {},
6817 m_this_cu (this_cu)
6818 {
6819 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6820 struct objfile *objfile = dwarf2_per_objfile->objfile;
6821 struct dwarf2_section_info *section = this_cu->section;
6822 bfd *abfd = section->get_bfd_owner ();
6823 struct dwarf2_cu *cu;
6824 const gdb_byte *begin_info_ptr;
6825 struct signatured_type *sig_type = NULL;
6826 struct dwarf2_section_info *abbrev_section;
6827 /* Non-zero if CU currently points to a DWO file and we need to
6828 reread it. When this happens we need to reread the skeleton die
6829 before we can reread the DWO file (this only applies to CUs, not TUs). */
6830 int rereading_dwo_cu = 0;
6831
6832 if (dwarf_die_debug)
6833 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6834 this_cu->is_debug_types ? "type" : "comp",
6835 sect_offset_str (this_cu->sect_off));
6836
6837 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6838 file (instead of going through the stub), short-circuit all of this. */
6839 if (this_cu->reading_dwo_directly)
6840 {
6841 /* Narrow down the scope of possibilities to have to understand. */
6842 gdb_assert (this_cu->is_debug_types);
6843 gdb_assert (abbrev_table == NULL);
6844 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6845 return;
6846 }
6847
6848 /* This is cheap if the section is already read in. */
6849 section->read (objfile);
6850
6851 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6852
6853 abbrev_section = get_abbrev_section_for_cu (this_cu);
6854
6855 if (use_existing_cu && this_cu->cu != NULL)
6856 {
6857 cu = this_cu->cu;
6858 /* If this CU is from a DWO file we need to start over, we need to
6859 refetch the attributes from the skeleton CU.
6860 This could be optimized by retrieving those attributes from when we
6861 were here the first time: the previous comp_unit_die was stored in
6862 comp_unit_obstack. But there's no data yet that we need this
6863 optimization. */
6864 if (cu->dwo_unit != NULL)
6865 rereading_dwo_cu = 1;
6866 }
6867 else
6868 {
6869 /* If !use_existing_cu, this_cu->cu must be NULL. */
6870 gdb_assert (this_cu->cu == NULL);
6871 m_new_cu.reset (new dwarf2_cu (this_cu));
6872 cu = m_new_cu.get ();
6873 }
6874
6875 /* Get the header. */
6876 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6877 {
6878 /* We already have the header, there's no need to read it in again. */
6879 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6880 }
6881 else
6882 {
6883 if (this_cu->is_debug_types)
6884 {
6885 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6886 &cu->header, section,
6887 abbrev_section, info_ptr,
6888 rcuh_kind::TYPE);
6889
6890 /* Since per_cu is the first member of struct signatured_type,
6891 we can go from a pointer to one to a pointer to the other. */
6892 sig_type = (struct signatured_type *) this_cu;
6893 gdb_assert (sig_type->signature == cu->header.signature);
6894 gdb_assert (sig_type->type_offset_in_tu
6895 == cu->header.type_cu_offset_in_tu);
6896 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6897
6898 /* LENGTH has not been set yet for type units if we're
6899 using .gdb_index. */
6900 this_cu->length = cu->header.get_length ();
6901
6902 /* Establish the type offset that can be used to lookup the type. */
6903 sig_type->type_offset_in_section =
6904 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6905
6906 this_cu->dwarf_version = cu->header.version;
6907 }
6908 else
6909 {
6910 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6911 &cu->header, section,
6912 abbrev_section,
6913 info_ptr,
6914 rcuh_kind::COMPILE);
6915
6916 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6917 gdb_assert (this_cu->length == cu->header.get_length ());
6918 this_cu->dwarf_version = cu->header.version;
6919 }
6920 }
6921
6922 /* Skip dummy compilation units. */
6923 if (info_ptr >= begin_info_ptr + this_cu->length
6924 || peek_abbrev_code (abfd, info_ptr) == 0)
6925 {
6926 dummy_p = true;
6927 return;
6928 }
6929
6930 /* If we don't have them yet, read the abbrevs for this compilation unit.
6931 And if we need to read them now, make sure they're freed when we're
6932 done. */
6933 if (abbrev_table != NULL)
6934 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6935 else
6936 {
6937 m_abbrev_table_holder
6938 = abbrev_table::read (objfile, abbrev_section,
6939 cu->header.abbrev_sect_off);
6940 abbrev_table = m_abbrev_table_holder.get ();
6941 }
6942
6943 /* Read the top level CU/TU die. */
6944 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6945 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6946
6947 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6948 {
6949 dummy_p = true;
6950 return;
6951 }
6952
6953 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6954 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6955 table from the DWO file and pass the ownership over to us. It will be
6956 referenced from READER, so we must make sure to free it after we're done
6957 with READER.
6958
6959 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6960 DWO CU, that this test will fail (the attribute will not be present). */
6961 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6962 if (dwo_name != nullptr)
6963 {
6964 struct dwo_unit *dwo_unit;
6965 struct die_info *dwo_comp_unit_die;
6966
6967 if (comp_unit_die->has_children)
6968 {
6969 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6970 " has children (offset %s) [in module %s]"),
6971 sect_offset_str (this_cu->sect_off),
6972 bfd_get_filename (abfd));
6973 }
6974 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6975 if (dwo_unit != NULL)
6976 {
6977 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6978 comp_unit_die, NULL,
6979 this, &info_ptr,
6980 &dwo_comp_unit_die,
6981 &m_dwo_abbrev_table) == 0)
6982 {
6983 /* Dummy die. */
6984 dummy_p = true;
6985 return;
6986 }
6987 comp_unit_die = dwo_comp_unit_die;
6988 }
6989 else
6990 {
6991 /* Yikes, we couldn't find the rest of the DIE, we only have
6992 the stub. A complaint has already been logged. There's
6993 not much more we can do except pass on the stub DIE to
6994 die_reader_func. We don't want to throw an error on bad
6995 debug info. */
6996 }
6997 }
6998 }
6999
7000 void
7001 cutu_reader::keep ()
7002 {
7003 /* Done, clean up. */
7004 gdb_assert (!dummy_p);
7005 if (m_new_cu != NULL)
7006 {
7007 struct dwarf2_per_objfile *dwarf2_per_objfile
7008 = m_this_cu->dwarf2_per_objfile;
7009 /* Link this CU into read_in_chain. */
7010 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7011 dwarf2_per_objfile->read_in_chain = m_this_cu;
7012 /* The chain owns it now. */
7013 m_new_cu.release ();
7014 }
7015 }
7016
7017 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7018 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7019 assumed to have already done the lookup to find the DWO file).
7020
7021 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7022 THIS_CU->is_debug_types, but nothing else.
7023
7024 We fill in THIS_CU->length.
7025
7026 THIS_CU->cu is always freed when done.
7027 This is done in order to not leave THIS_CU->cu in a state where we have
7028 to care whether it refers to the "main" CU or the DWO CU.
7029
7030 When parent_cu is passed, it is used to provide a default value for
7031 str_offsets_base and addr_base from the parent. */
7032
7033 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7034 struct dwarf2_cu *parent_cu,
7035 struct dwo_file *dwo_file)
7036 : die_reader_specs {},
7037 m_this_cu (this_cu)
7038 {
7039 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7040 struct objfile *objfile = dwarf2_per_objfile->objfile;
7041 struct dwarf2_section_info *section = this_cu->section;
7042 bfd *abfd = section->get_bfd_owner ();
7043 struct dwarf2_section_info *abbrev_section;
7044 const gdb_byte *begin_info_ptr, *info_ptr;
7045
7046 if (dwarf_die_debug)
7047 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7048 this_cu->is_debug_types ? "type" : "comp",
7049 sect_offset_str (this_cu->sect_off));
7050
7051 gdb_assert (this_cu->cu == NULL);
7052
7053 abbrev_section = (dwo_file != NULL
7054 ? &dwo_file->sections.abbrev
7055 : get_abbrev_section_for_cu (this_cu));
7056
7057 /* This is cheap if the section is already read in. */
7058 section->read (objfile);
7059
7060 m_new_cu.reset (new dwarf2_cu (this_cu));
7061
7062 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7063 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7064 &m_new_cu->header, section,
7065 abbrev_section, info_ptr,
7066 (this_cu->is_debug_types
7067 ? rcuh_kind::TYPE
7068 : rcuh_kind::COMPILE));
7069
7070 if (parent_cu != nullptr)
7071 {
7072 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7073 m_new_cu->addr_base = parent_cu->addr_base;
7074 }
7075 this_cu->length = m_new_cu->header.get_length ();
7076
7077 /* Skip dummy compilation units. */
7078 if (info_ptr >= begin_info_ptr + this_cu->length
7079 || peek_abbrev_code (abfd, info_ptr) == 0)
7080 {
7081 dummy_p = true;
7082 return;
7083 }
7084
7085 m_abbrev_table_holder
7086 = abbrev_table::read (objfile, abbrev_section,
7087 m_new_cu->header.abbrev_sect_off);
7088
7089 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7090 m_abbrev_table_holder.get ());
7091 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7092 }
7093
7094 \f
7095 /* Type Unit Groups.
7096
7097 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7098 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7099 so that all types coming from the same compilation (.o file) are grouped
7100 together. A future step could be to put the types in the same symtab as
7101 the CU the types ultimately came from. */
7102
7103 static hashval_t
7104 hash_type_unit_group (const void *item)
7105 {
7106 const struct type_unit_group *tu_group
7107 = (const struct type_unit_group *) item;
7108
7109 return hash_stmt_list_entry (&tu_group->hash);
7110 }
7111
7112 static int
7113 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7114 {
7115 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7116 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7117
7118 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7119 }
7120
7121 /* Allocate a hash table for type unit groups. */
7122
7123 static htab_up
7124 allocate_type_unit_groups_table ()
7125 {
7126 return htab_up (htab_create_alloc (3,
7127 hash_type_unit_group,
7128 eq_type_unit_group,
7129 NULL, xcalloc, xfree));
7130 }
7131
7132 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7133 partial symtabs. We combine several TUs per psymtab to not let the size
7134 of any one psymtab grow too big. */
7135 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7136 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7137
7138 /* Helper routine for get_type_unit_group.
7139 Create the type_unit_group object used to hold one or more TUs. */
7140
7141 static struct type_unit_group *
7142 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7143 {
7144 struct dwarf2_per_objfile *dwarf2_per_objfile
7145 = cu->per_cu->dwarf2_per_objfile;
7146 struct objfile *objfile = dwarf2_per_objfile->objfile;
7147 struct dwarf2_per_cu_data *per_cu;
7148 struct type_unit_group *tu_group;
7149
7150 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7151 struct type_unit_group);
7152 per_cu = &tu_group->per_cu;
7153 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7154
7155 if (dwarf2_per_objfile->using_index)
7156 {
7157 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7158 struct dwarf2_per_cu_quick_data);
7159 }
7160 else
7161 {
7162 unsigned int line_offset = to_underlying (line_offset_struct);
7163 dwarf2_psymtab *pst;
7164 std::string name;
7165
7166 /* Give the symtab a useful name for debug purposes. */
7167 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7168 name = string_printf ("<type_units_%d>",
7169 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7170 else
7171 name = string_printf ("<type_units_at_0x%x>", line_offset);
7172
7173 pst = create_partial_symtab (per_cu, name.c_str ());
7174 pst->anonymous = true;
7175 }
7176
7177 tu_group->hash.dwo_unit = cu->dwo_unit;
7178 tu_group->hash.line_sect_off = line_offset_struct;
7179
7180 return tu_group;
7181 }
7182
7183 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7184 STMT_LIST is a DW_AT_stmt_list attribute. */
7185
7186 static struct type_unit_group *
7187 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7188 {
7189 struct dwarf2_per_objfile *dwarf2_per_objfile
7190 = cu->per_cu->dwarf2_per_objfile;
7191 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7192 struct type_unit_group *tu_group;
7193 void **slot;
7194 unsigned int line_offset;
7195 struct type_unit_group type_unit_group_for_lookup;
7196
7197 if (dwarf2_per_objfile->type_unit_groups == NULL)
7198 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7199
7200 /* Do we need to create a new group, or can we use an existing one? */
7201
7202 if (stmt_list)
7203 {
7204 line_offset = DW_UNSND (stmt_list);
7205 ++tu_stats->nr_symtab_sharers;
7206 }
7207 else
7208 {
7209 /* Ugh, no stmt_list. Rare, but we have to handle it.
7210 We can do various things here like create one group per TU or
7211 spread them over multiple groups to split up the expansion work.
7212 To avoid worst case scenarios (too many groups or too large groups)
7213 we, umm, group them in bunches. */
7214 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7215 | (tu_stats->nr_stmt_less_type_units
7216 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7217 ++tu_stats->nr_stmt_less_type_units;
7218 }
7219
7220 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7221 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7222 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7223 &type_unit_group_for_lookup, INSERT);
7224 if (*slot != NULL)
7225 {
7226 tu_group = (struct type_unit_group *) *slot;
7227 gdb_assert (tu_group != NULL);
7228 }
7229 else
7230 {
7231 sect_offset line_offset_struct = (sect_offset) line_offset;
7232 tu_group = create_type_unit_group (cu, line_offset_struct);
7233 *slot = tu_group;
7234 ++tu_stats->nr_symtabs;
7235 }
7236
7237 return tu_group;
7238 }
7239 \f
7240 /* Partial symbol tables. */
7241
7242 /* Create a psymtab named NAME and assign it to PER_CU.
7243
7244 The caller must fill in the following details:
7245 dirname, textlow, texthigh. */
7246
7247 static dwarf2_psymtab *
7248 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7249 {
7250 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7251 dwarf2_psymtab *pst;
7252
7253 pst = new dwarf2_psymtab (name, objfile, 0);
7254
7255 pst->psymtabs_addrmap_supported = true;
7256
7257 /* This is the glue that links PST into GDB's symbol API. */
7258 pst->per_cu_data = per_cu;
7259 per_cu->v.psymtab = pst;
7260
7261 return pst;
7262 }
7263
7264 /* DIE reader function for process_psymtab_comp_unit. */
7265
7266 static void
7267 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7268 const gdb_byte *info_ptr,
7269 struct die_info *comp_unit_die,
7270 enum language pretend_language)
7271 {
7272 struct dwarf2_cu *cu = reader->cu;
7273 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7274 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7275 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7276 CORE_ADDR baseaddr;
7277 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7278 dwarf2_psymtab *pst;
7279 enum pc_bounds_kind cu_bounds_kind;
7280 const char *filename;
7281
7282 gdb_assert (! per_cu->is_debug_types);
7283
7284 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7285
7286 /* Allocate a new partial symbol table structure. */
7287 gdb::unique_xmalloc_ptr<char> debug_filename;
7288 static const char artificial[] = "<artificial>";
7289 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7290 if (filename == NULL)
7291 filename = "";
7292 else if (strcmp (filename, artificial) == 0)
7293 {
7294 debug_filename.reset (concat (artificial, "@",
7295 sect_offset_str (per_cu->sect_off),
7296 (char *) NULL));
7297 filename = debug_filename.get ();
7298 }
7299
7300 pst = create_partial_symtab (per_cu, filename);
7301
7302 /* This must be done before calling dwarf2_build_include_psymtabs. */
7303 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7304
7305 baseaddr = objfile->text_section_offset ();
7306
7307 dwarf2_find_base_address (comp_unit_die, cu);
7308
7309 /* Possibly set the default values of LOWPC and HIGHPC from
7310 `DW_AT_ranges'. */
7311 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7312 &best_highpc, cu, pst);
7313 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7314 {
7315 CORE_ADDR low
7316 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7317 - baseaddr);
7318 CORE_ADDR high
7319 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7320 - baseaddr - 1);
7321 /* Store the contiguous range if it is not empty; it can be
7322 empty for CUs with no code. */
7323 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7324 low, high, pst);
7325 }
7326
7327 /* Check if comp unit has_children.
7328 If so, read the rest of the partial symbols from this comp unit.
7329 If not, there's no more debug_info for this comp unit. */
7330 if (comp_unit_die->has_children)
7331 {
7332 struct partial_die_info *first_die;
7333 CORE_ADDR lowpc, highpc;
7334
7335 lowpc = ((CORE_ADDR) -1);
7336 highpc = ((CORE_ADDR) 0);
7337
7338 first_die = load_partial_dies (reader, info_ptr, 1);
7339
7340 scan_partial_symbols (first_die, &lowpc, &highpc,
7341 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7342
7343 /* If we didn't find a lowpc, set it to highpc to avoid
7344 complaints from `maint check'. */
7345 if (lowpc == ((CORE_ADDR) -1))
7346 lowpc = highpc;
7347
7348 /* If the compilation unit didn't have an explicit address range,
7349 then use the information extracted from its child dies. */
7350 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7351 {
7352 best_lowpc = lowpc;
7353 best_highpc = highpc;
7354 }
7355 }
7356 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7357 best_lowpc + baseaddr)
7358 - baseaddr);
7359 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7360 best_highpc + baseaddr)
7361 - baseaddr);
7362
7363 end_psymtab_common (objfile, pst);
7364
7365 if (!cu->per_cu->imported_symtabs_empty ())
7366 {
7367 int i;
7368 int len = cu->per_cu->imported_symtabs_size ();
7369
7370 /* Fill in 'dependencies' here; we fill in 'users' in a
7371 post-pass. */
7372 pst->number_of_dependencies = len;
7373 pst->dependencies
7374 = objfile->partial_symtabs->allocate_dependencies (len);
7375 for (i = 0; i < len; ++i)
7376 {
7377 pst->dependencies[i]
7378 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7379 }
7380
7381 cu->per_cu->imported_symtabs_free ();
7382 }
7383
7384 /* Get the list of files included in the current compilation unit,
7385 and build a psymtab for each of them. */
7386 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7387
7388 if (dwarf_read_debug)
7389 fprintf_unfiltered (gdb_stdlog,
7390 "Psymtab for %s unit @%s: %s - %s"
7391 ", %d global, %d static syms\n",
7392 per_cu->is_debug_types ? "type" : "comp",
7393 sect_offset_str (per_cu->sect_off),
7394 paddress (gdbarch, pst->text_low (objfile)),
7395 paddress (gdbarch, pst->text_high (objfile)),
7396 pst->n_global_syms, pst->n_static_syms);
7397 }
7398
7399 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7400 Process compilation unit THIS_CU for a psymtab. */
7401
7402 static void
7403 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7404 bool want_partial_unit,
7405 enum language pretend_language)
7406 {
7407 /* If this compilation unit was already read in, free the
7408 cached copy in order to read it in again. This is
7409 necessary because we skipped some symbols when we first
7410 read in the compilation unit (see load_partial_dies).
7411 This problem could be avoided, but the benefit is unclear. */
7412 if (this_cu->cu != NULL)
7413 free_one_cached_comp_unit (this_cu);
7414
7415 cutu_reader reader (this_cu, NULL, 0, false);
7416
7417 switch (reader.comp_unit_die->tag)
7418 {
7419 case DW_TAG_compile_unit:
7420 this_cu->unit_type = DW_UT_compile;
7421 break;
7422 case DW_TAG_partial_unit:
7423 this_cu->unit_type = DW_UT_partial;
7424 break;
7425 default:
7426 abort ();
7427 }
7428
7429 if (reader.dummy_p)
7430 {
7431 /* Nothing. */
7432 }
7433 else if (this_cu->is_debug_types)
7434 build_type_psymtabs_reader (&reader, reader.info_ptr,
7435 reader.comp_unit_die);
7436 else if (want_partial_unit
7437 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7438 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7439 reader.comp_unit_die,
7440 pretend_language);
7441
7442 this_cu->lang = this_cu->cu->language;
7443
7444 /* Age out any secondary CUs. */
7445 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7446 }
7447
7448 /* Reader function for build_type_psymtabs. */
7449
7450 static void
7451 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7452 const gdb_byte *info_ptr,
7453 struct die_info *type_unit_die)
7454 {
7455 struct dwarf2_per_objfile *dwarf2_per_objfile
7456 = reader->cu->per_cu->dwarf2_per_objfile;
7457 struct objfile *objfile = dwarf2_per_objfile->objfile;
7458 struct dwarf2_cu *cu = reader->cu;
7459 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7460 struct signatured_type *sig_type;
7461 struct type_unit_group *tu_group;
7462 struct attribute *attr;
7463 struct partial_die_info *first_die;
7464 CORE_ADDR lowpc, highpc;
7465 dwarf2_psymtab *pst;
7466
7467 gdb_assert (per_cu->is_debug_types);
7468 sig_type = (struct signatured_type *) per_cu;
7469
7470 if (! type_unit_die->has_children)
7471 return;
7472
7473 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7474 tu_group = get_type_unit_group (cu, attr);
7475
7476 if (tu_group->tus == nullptr)
7477 tu_group->tus = new std::vector<signatured_type *>;
7478 tu_group->tus->push_back (sig_type);
7479
7480 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7481 pst = create_partial_symtab (per_cu, "");
7482 pst->anonymous = true;
7483
7484 first_die = load_partial_dies (reader, info_ptr, 1);
7485
7486 lowpc = (CORE_ADDR) -1;
7487 highpc = (CORE_ADDR) 0;
7488 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7489
7490 end_psymtab_common (objfile, pst);
7491 }
7492
7493 /* Struct used to sort TUs by their abbreviation table offset. */
7494
7495 struct tu_abbrev_offset
7496 {
7497 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7498 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7499 {}
7500
7501 signatured_type *sig_type;
7502 sect_offset abbrev_offset;
7503 };
7504
7505 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7506
7507 static bool
7508 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7509 const struct tu_abbrev_offset &b)
7510 {
7511 return a.abbrev_offset < b.abbrev_offset;
7512 }
7513
7514 /* Efficiently read all the type units.
7515 This does the bulk of the work for build_type_psymtabs.
7516
7517 The efficiency is because we sort TUs by the abbrev table they use and
7518 only read each abbrev table once. In one program there are 200K TUs
7519 sharing 8K abbrev tables.
7520
7521 The main purpose of this function is to support building the
7522 dwarf2_per_objfile->type_unit_groups table.
7523 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7524 can collapse the search space by grouping them by stmt_list.
7525 The savings can be significant, in the same program from above the 200K TUs
7526 share 8K stmt_list tables.
7527
7528 FUNC is expected to call get_type_unit_group, which will create the
7529 struct type_unit_group if necessary and add it to
7530 dwarf2_per_objfile->type_unit_groups. */
7531
7532 static void
7533 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7534 {
7535 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7536 abbrev_table_up abbrev_table;
7537 sect_offset abbrev_offset;
7538
7539 /* It's up to the caller to not call us multiple times. */
7540 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7541
7542 if (dwarf2_per_objfile->all_type_units.empty ())
7543 return;
7544
7545 /* TUs typically share abbrev tables, and there can be way more TUs than
7546 abbrev tables. Sort by abbrev table to reduce the number of times we
7547 read each abbrev table in.
7548 Alternatives are to punt or to maintain a cache of abbrev tables.
7549 This is simpler and efficient enough for now.
7550
7551 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7552 symtab to use). Typically TUs with the same abbrev offset have the same
7553 stmt_list value too so in practice this should work well.
7554
7555 The basic algorithm here is:
7556
7557 sort TUs by abbrev table
7558 for each TU with same abbrev table:
7559 read abbrev table if first user
7560 read TU top level DIE
7561 [IWBN if DWO skeletons had DW_AT_stmt_list]
7562 call FUNC */
7563
7564 if (dwarf_read_debug)
7565 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7566
7567 /* Sort in a separate table to maintain the order of all_type_units
7568 for .gdb_index: TU indices directly index all_type_units. */
7569 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7570 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7571
7572 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7573 sorted_by_abbrev.emplace_back
7574 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7575 sig_type->per_cu.section,
7576 sig_type->per_cu.sect_off));
7577
7578 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7579 sort_tu_by_abbrev_offset);
7580
7581 abbrev_offset = (sect_offset) ~(unsigned) 0;
7582
7583 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7584 {
7585 /* Switch to the next abbrev table if necessary. */
7586 if (abbrev_table == NULL
7587 || tu.abbrev_offset != abbrev_offset)
7588 {
7589 abbrev_offset = tu.abbrev_offset;
7590 abbrev_table =
7591 abbrev_table::read (dwarf2_per_objfile->objfile,
7592 &dwarf2_per_objfile->abbrev,
7593 abbrev_offset);
7594 ++tu_stats->nr_uniq_abbrev_tables;
7595 }
7596
7597 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7598 0, false);
7599 if (!reader.dummy_p)
7600 build_type_psymtabs_reader (&reader, reader.info_ptr,
7601 reader.comp_unit_die);
7602 }
7603 }
7604
7605 /* Print collected type unit statistics. */
7606
7607 static void
7608 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7609 {
7610 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7611
7612 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7613 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7614 dwarf2_per_objfile->all_type_units.size ());
7615 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7616 tu_stats->nr_uniq_abbrev_tables);
7617 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7618 tu_stats->nr_symtabs);
7619 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7620 tu_stats->nr_symtab_sharers);
7621 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7622 tu_stats->nr_stmt_less_type_units);
7623 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7624 tu_stats->nr_all_type_units_reallocs);
7625 }
7626
7627 /* Traversal function for build_type_psymtabs. */
7628
7629 static int
7630 build_type_psymtab_dependencies (void **slot, void *info)
7631 {
7632 struct dwarf2_per_objfile *dwarf2_per_objfile
7633 = (struct dwarf2_per_objfile *) info;
7634 struct objfile *objfile = dwarf2_per_objfile->objfile;
7635 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7636 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7637 dwarf2_psymtab *pst = per_cu->v.psymtab;
7638 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7639 int i;
7640
7641 gdb_assert (len > 0);
7642 gdb_assert (per_cu->type_unit_group_p ());
7643
7644 pst->number_of_dependencies = len;
7645 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7646 for (i = 0; i < len; ++i)
7647 {
7648 struct signatured_type *iter = tu_group->tus->at (i);
7649 gdb_assert (iter->per_cu.is_debug_types);
7650 pst->dependencies[i] = iter->per_cu.v.psymtab;
7651 iter->type_unit_group = tu_group;
7652 }
7653
7654 delete tu_group->tus;
7655 tu_group->tus = nullptr;
7656
7657 return 1;
7658 }
7659
7660 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7661 Build partial symbol tables for the .debug_types comp-units. */
7662
7663 static void
7664 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7665 {
7666 if (! create_all_type_units (dwarf2_per_objfile))
7667 return;
7668
7669 build_type_psymtabs_1 (dwarf2_per_objfile);
7670 }
7671
7672 /* Traversal function for process_skeletonless_type_unit.
7673 Read a TU in a DWO file and build partial symbols for it. */
7674
7675 static int
7676 process_skeletonless_type_unit (void **slot, void *info)
7677 {
7678 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7679 struct dwarf2_per_objfile *dwarf2_per_objfile
7680 = (struct dwarf2_per_objfile *) info;
7681 struct signatured_type find_entry, *entry;
7682
7683 /* If this TU doesn't exist in the global table, add it and read it in. */
7684
7685 if (dwarf2_per_objfile->signatured_types == NULL)
7686 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7687
7688 find_entry.signature = dwo_unit->signature;
7689 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7690 &find_entry, INSERT);
7691 /* If we've already seen this type there's nothing to do. What's happening
7692 is we're doing our own version of comdat-folding here. */
7693 if (*slot != NULL)
7694 return 1;
7695
7696 /* This does the job that create_all_type_units would have done for
7697 this TU. */
7698 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7699 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7700 *slot = entry;
7701
7702 /* This does the job that build_type_psymtabs_1 would have done. */
7703 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7704 if (!reader.dummy_p)
7705 build_type_psymtabs_reader (&reader, reader.info_ptr,
7706 reader.comp_unit_die);
7707
7708 return 1;
7709 }
7710
7711 /* Traversal function for process_skeletonless_type_units. */
7712
7713 static int
7714 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7715 {
7716 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7717
7718 if (dwo_file->tus != NULL)
7719 htab_traverse_noresize (dwo_file->tus.get (),
7720 process_skeletonless_type_unit, info);
7721
7722 return 1;
7723 }
7724
7725 /* Scan all TUs of DWO files, verifying we've processed them.
7726 This is needed in case a TU was emitted without its skeleton.
7727 Note: This can't be done until we know what all the DWO files are. */
7728
7729 static void
7730 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7731 {
7732 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7733 if (get_dwp_file (dwarf2_per_objfile) == NULL
7734 && dwarf2_per_objfile->dwo_files != NULL)
7735 {
7736 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7737 process_dwo_file_for_skeletonless_type_units,
7738 dwarf2_per_objfile);
7739 }
7740 }
7741
7742 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7743
7744 static void
7745 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7746 {
7747 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7748 {
7749 dwarf2_psymtab *pst = per_cu->v.psymtab;
7750
7751 if (pst == NULL)
7752 continue;
7753
7754 for (int j = 0; j < pst->number_of_dependencies; ++j)
7755 {
7756 /* Set the 'user' field only if it is not already set. */
7757 if (pst->dependencies[j]->user == NULL)
7758 pst->dependencies[j]->user = pst;
7759 }
7760 }
7761 }
7762
7763 /* Build the partial symbol table by doing a quick pass through the
7764 .debug_info and .debug_abbrev sections. */
7765
7766 static void
7767 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7768 {
7769 struct objfile *objfile = dwarf2_per_objfile->objfile;
7770
7771 if (dwarf_read_debug)
7772 {
7773 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7774 objfile_name (objfile));
7775 }
7776
7777 scoped_restore restore_reading_psyms
7778 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7779 true);
7780
7781 dwarf2_per_objfile->info.read (objfile);
7782
7783 /* Any cached compilation units will be linked by the per-objfile
7784 read_in_chain. Make sure to free them when we're done. */
7785 free_cached_comp_units freer (dwarf2_per_objfile);
7786
7787 build_type_psymtabs (dwarf2_per_objfile);
7788
7789 create_all_comp_units (dwarf2_per_objfile);
7790
7791 /* Create a temporary address map on a temporary obstack. We later
7792 copy this to the final obstack. */
7793 auto_obstack temp_obstack;
7794
7795 scoped_restore save_psymtabs_addrmap
7796 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7797 addrmap_create_mutable (&temp_obstack));
7798
7799 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7800 process_psymtab_comp_unit (per_cu, false, language_minimal);
7801
7802 /* This has to wait until we read the CUs, we need the list of DWOs. */
7803 process_skeletonless_type_units (dwarf2_per_objfile);
7804
7805 /* Now that all TUs have been processed we can fill in the dependencies. */
7806 if (dwarf2_per_objfile->type_unit_groups != NULL)
7807 {
7808 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7809 build_type_psymtab_dependencies, dwarf2_per_objfile);
7810 }
7811
7812 if (dwarf_read_debug)
7813 print_tu_stats (dwarf2_per_objfile);
7814
7815 set_partial_user (dwarf2_per_objfile);
7816
7817 objfile->partial_symtabs->psymtabs_addrmap
7818 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7819 objfile->partial_symtabs->obstack ());
7820 /* At this point we want to keep the address map. */
7821 save_psymtabs_addrmap.release ();
7822
7823 if (dwarf_read_debug)
7824 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7825 objfile_name (objfile));
7826 }
7827
7828 /* Load the partial DIEs for a secondary CU into memory.
7829 This is also used when rereading a primary CU with load_all_dies. */
7830
7831 static void
7832 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7833 {
7834 cutu_reader reader (this_cu, NULL, 1, false);
7835
7836 if (!reader.dummy_p)
7837 {
7838 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7839 language_minimal);
7840
7841 /* Check if comp unit has_children.
7842 If so, read the rest of the partial symbols from this comp unit.
7843 If not, there's no more debug_info for this comp unit. */
7844 if (reader.comp_unit_die->has_children)
7845 load_partial_dies (&reader, reader.info_ptr, 0);
7846
7847 reader.keep ();
7848 }
7849 }
7850
7851 static void
7852 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7853 struct dwarf2_section_info *section,
7854 struct dwarf2_section_info *abbrev_section,
7855 unsigned int is_dwz)
7856 {
7857 const gdb_byte *info_ptr;
7858 struct objfile *objfile = dwarf2_per_objfile->objfile;
7859
7860 if (dwarf_read_debug)
7861 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7862 section->get_name (),
7863 section->get_file_name ());
7864
7865 section->read (objfile);
7866
7867 info_ptr = section->buffer;
7868
7869 while (info_ptr < section->buffer + section->size)
7870 {
7871 struct dwarf2_per_cu_data *this_cu;
7872
7873 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7874
7875 comp_unit_head cu_header;
7876 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7877 abbrev_section, info_ptr,
7878 rcuh_kind::COMPILE);
7879
7880 /* Save the compilation unit for later lookup. */
7881 if (cu_header.unit_type != DW_UT_type)
7882 {
7883 this_cu = XOBNEW (&objfile->objfile_obstack,
7884 struct dwarf2_per_cu_data);
7885 memset (this_cu, 0, sizeof (*this_cu));
7886 }
7887 else
7888 {
7889 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7890 struct signatured_type);
7891 memset (sig_type, 0, sizeof (*sig_type));
7892 sig_type->signature = cu_header.signature;
7893 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7894 this_cu = &sig_type->per_cu;
7895 }
7896 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7897 this_cu->sect_off = sect_off;
7898 this_cu->length = cu_header.length + cu_header.initial_length_size;
7899 this_cu->is_dwz = is_dwz;
7900 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7901 this_cu->section = section;
7902
7903 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7904
7905 info_ptr = info_ptr + this_cu->length;
7906 }
7907 }
7908
7909 /* Create a list of all compilation units in OBJFILE.
7910 This is only done for -readnow and building partial symtabs. */
7911
7912 static void
7913 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7914 {
7915 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7916 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7917 &dwarf2_per_objfile->abbrev, 0);
7918
7919 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7920 if (dwz != NULL)
7921 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7922 1);
7923 }
7924
7925 /* Process all loaded DIEs for compilation unit CU, starting at
7926 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7927 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7928 DW_AT_ranges). See the comments of add_partial_subprogram on how
7929 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7930
7931 static void
7932 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7933 CORE_ADDR *highpc, int set_addrmap,
7934 struct dwarf2_cu *cu)
7935 {
7936 struct partial_die_info *pdi;
7937
7938 /* Now, march along the PDI's, descending into ones which have
7939 interesting children but skipping the children of the other ones,
7940 until we reach the end of the compilation unit. */
7941
7942 pdi = first_die;
7943
7944 while (pdi != NULL)
7945 {
7946 pdi->fixup (cu);
7947
7948 /* Anonymous namespaces or modules have no name but have interesting
7949 children, so we need to look at them. Ditto for anonymous
7950 enums. */
7951
7952 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7953 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7954 || pdi->tag == DW_TAG_imported_unit
7955 || pdi->tag == DW_TAG_inlined_subroutine)
7956 {
7957 switch (pdi->tag)
7958 {
7959 case DW_TAG_subprogram:
7960 case DW_TAG_inlined_subroutine:
7961 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7962 break;
7963 case DW_TAG_constant:
7964 case DW_TAG_variable:
7965 case DW_TAG_typedef:
7966 case DW_TAG_union_type:
7967 if (!pdi->is_declaration)
7968 {
7969 add_partial_symbol (pdi, cu);
7970 }
7971 break;
7972 case DW_TAG_class_type:
7973 case DW_TAG_interface_type:
7974 case DW_TAG_structure_type:
7975 if (!pdi->is_declaration)
7976 {
7977 add_partial_symbol (pdi, cu);
7978 }
7979 if ((cu->language == language_rust
7980 || cu->language == language_cplus) && pdi->has_children)
7981 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7982 set_addrmap, cu);
7983 break;
7984 case DW_TAG_enumeration_type:
7985 if (!pdi->is_declaration)
7986 add_partial_enumeration (pdi, cu);
7987 break;
7988 case DW_TAG_base_type:
7989 case DW_TAG_subrange_type:
7990 /* File scope base type definitions are added to the partial
7991 symbol table. */
7992 add_partial_symbol (pdi, cu);
7993 break;
7994 case DW_TAG_namespace:
7995 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7996 break;
7997 case DW_TAG_module:
7998 if (!pdi->is_declaration)
7999 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8000 break;
8001 case DW_TAG_imported_unit:
8002 {
8003 struct dwarf2_per_cu_data *per_cu;
8004
8005 /* For now we don't handle imported units in type units. */
8006 if (cu->per_cu->is_debug_types)
8007 {
8008 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8009 " supported in type units [in module %s]"),
8010 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8011 }
8012
8013 per_cu = dwarf2_find_containing_comp_unit
8014 (pdi->d.sect_off, pdi->is_dwz,
8015 cu->per_cu->dwarf2_per_objfile);
8016
8017 /* Go read the partial unit, if needed. */
8018 if (per_cu->v.psymtab == NULL)
8019 process_psymtab_comp_unit (per_cu, true, cu->language);
8020
8021 cu->per_cu->imported_symtabs_push (per_cu);
8022 }
8023 break;
8024 case DW_TAG_imported_declaration:
8025 add_partial_symbol (pdi, cu);
8026 break;
8027 default:
8028 break;
8029 }
8030 }
8031
8032 /* If the die has a sibling, skip to the sibling. */
8033
8034 pdi = pdi->die_sibling;
8035 }
8036 }
8037
8038 /* Functions used to compute the fully scoped name of a partial DIE.
8039
8040 Normally, this is simple. For C++, the parent DIE's fully scoped
8041 name is concatenated with "::" and the partial DIE's name.
8042 Enumerators are an exception; they use the scope of their parent
8043 enumeration type, i.e. the name of the enumeration type is not
8044 prepended to the enumerator.
8045
8046 There are two complexities. One is DW_AT_specification; in this
8047 case "parent" means the parent of the target of the specification,
8048 instead of the direct parent of the DIE. The other is compilers
8049 which do not emit DW_TAG_namespace; in this case we try to guess
8050 the fully qualified name of structure types from their members'
8051 linkage names. This must be done using the DIE's children rather
8052 than the children of any DW_AT_specification target. We only need
8053 to do this for structures at the top level, i.e. if the target of
8054 any DW_AT_specification (if any; otherwise the DIE itself) does not
8055 have a parent. */
8056
8057 /* Compute the scope prefix associated with PDI's parent, in
8058 compilation unit CU. The result will be allocated on CU's
8059 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8060 field. NULL is returned if no prefix is necessary. */
8061 static const char *
8062 partial_die_parent_scope (struct partial_die_info *pdi,
8063 struct dwarf2_cu *cu)
8064 {
8065 const char *grandparent_scope;
8066 struct partial_die_info *parent, *real_pdi;
8067
8068 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8069 then this means the parent of the specification DIE. */
8070
8071 real_pdi = pdi;
8072 while (real_pdi->has_specification)
8073 {
8074 auto res = find_partial_die (real_pdi->spec_offset,
8075 real_pdi->spec_is_dwz, cu);
8076 real_pdi = res.pdi;
8077 cu = res.cu;
8078 }
8079
8080 parent = real_pdi->die_parent;
8081 if (parent == NULL)
8082 return NULL;
8083
8084 if (parent->scope_set)
8085 return parent->scope;
8086
8087 parent->fixup (cu);
8088
8089 grandparent_scope = partial_die_parent_scope (parent, cu);
8090
8091 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8092 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8093 Work around this problem here. */
8094 if (cu->language == language_cplus
8095 && parent->tag == DW_TAG_namespace
8096 && strcmp (parent->name, "::") == 0
8097 && grandparent_scope == NULL)
8098 {
8099 parent->scope = NULL;
8100 parent->scope_set = 1;
8101 return NULL;
8102 }
8103
8104 /* Nested subroutines in Fortran get a prefix. */
8105 if (pdi->tag == DW_TAG_enumerator)
8106 /* Enumerators should not get the name of the enumeration as a prefix. */
8107 parent->scope = grandparent_scope;
8108 else if (parent->tag == DW_TAG_namespace
8109 || parent->tag == DW_TAG_module
8110 || parent->tag == DW_TAG_structure_type
8111 || parent->tag == DW_TAG_class_type
8112 || parent->tag == DW_TAG_interface_type
8113 || parent->tag == DW_TAG_union_type
8114 || parent->tag == DW_TAG_enumeration_type
8115 || (cu->language == language_fortran
8116 && parent->tag == DW_TAG_subprogram
8117 && pdi->tag == DW_TAG_subprogram))
8118 {
8119 if (grandparent_scope == NULL)
8120 parent->scope = parent->name;
8121 else
8122 parent->scope = typename_concat (&cu->comp_unit_obstack,
8123 grandparent_scope,
8124 parent->name, 0, cu);
8125 }
8126 else
8127 {
8128 /* FIXME drow/2004-04-01: What should we be doing with
8129 function-local names? For partial symbols, we should probably be
8130 ignoring them. */
8131 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8132 dwarf_tag_name (parent->tag),
8133 sect_offset_str (pdi->sect_off));
8134 parent->scope = grandparent_scope;
8135 }
8136
8137 parent->scope_set = 1;
8138 return parent->scope;
8139 }
8140
8141 /* Return the fully scoped name associated with PDI, from compilation unit
8142 CU. The result will be allocated with malloc. */
8143
8144 static gdb::unique_xmalloc_ptr<char>
8145 partial_die_full_name (struct partial_die_info *pdi,
8146 struct dwarf2_cu *cu)
8147 {
8148 const char *parent_scope;
8149
8150 /* If this is a template instantiation, we can not work out the
8151 template arguments from partial DIEs. So, unfortunately, we have
8152 to go through the full DIEs. At least any work we do building
8153 types here will be reused if full symbols are loaded later. */
8154 if (pdi->has_template_arguments)
8155 {
8156 pdi->fixup (cu);
8157
8158 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8159 {
8160 struct die_info *die;
8161 struct attribute attr;
8162 struct dwarf2_cu *ref_cu = cu;
8163
8164 /* DW_FORM_ref_addr is using section offset. */
8165 attr.name = (enum dwarf_attribute) 0;
8166 attr.form = DW_FORM_ref_addr;
8167 attr.u.unsnd = to_underlying (pdi->sect_off);
8168 die = follow_die_ref (NULL, &attr, &ref_cu);
8169
8170 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8171 }
8172 }
8173
8174 parent_scope = partial_die_parent_scope (pdi, cu);
8175 if (parent_scope == NULL)
8176 return NULL;
8177 else
8178 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8179 pdi->name, 0, cu));
8180 }
8181
8182 static void
8183 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8184 {
8185 struct dwarf2_per_objfile *dwarf2_per_objfile
8186 = cu->per_cu->dwarf2_per_objfile;
8187 struct objfile *objfile = dwarf2_per_objfile->objfile;
8188 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8189 CORE_ADDR addr = 0;
8190 const char *actual_name = NULL;
8191 CORE_ADDR baseaddr;
8192
8193 baseaddr = objfile->text_section_offset ();
8194
8195 gdb::unique_xmalloc_ptr<char> built_actual_name
8196 = partial_die_full_name (pdi, cu);
8197 if (built_actual_name != NULL)
8198 actual_name = built_actual_name.get ();
8199
8200 if (actual_name == NULL)
8201 actual_name = pdi->name;
8202
8203 switch (pdi->tag)
8204 {
8205 case DW_TAG_inlined_subroutine:
8206 case DW_TAG_subprogram:
8207 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8208 - baseaddr);
8209 if (pdi->is_external
8210 || cu->language == language_ada
8211 || (cu->language == language_fortran
8212 && pdi->die_parent != NULL
8213 && pdi->die_parent->tag == DW_TAG_subprogram))
8214 {
8215 /* Normally, only "external" DIEs are part of the global scope.
8216 But in Ada and Fortran, we want to be able to access nested
8217 procedures globally. So all Ada and Fortran subprograms are
8218 stored in the global scope. */
8219 add_psymbol_to_list (actual_name,
8220 built_actual_name != NULL,
8221 VAR_DOMAIN, LOC_BLOCK,
8222 SECT_OFF_TEXT (objfile),
8223 psymbol_placement::GLOBAL,
8224 addr,
8225 cu->language, objfile);
8226 }
8227 else
8228 {
8229 add_psymbol_to_list (actual_name,
8230 built_actual_name != NULL,
8231 VAR_DOMAIN, LOC_BLOCK,
8232 SECT_OFF_TEXT (objfile),
8233 psymbol_placement::STATIC,
8234 addr, cu->language, objfile);
8235 }
8236
8237 if (pdi->main_subprogram && actual_name != NULL)
8238 set_objfile_main_name (objfile, actual_name, cu->language);
8239 break;
8240 case DW_TAG_constant:
8241 add_psymbol_to_list (actual_name,
8242 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8243 -1, (pdi->is_external
8244 ? psymbol_placement::GLOBAL
8245 : psymbol_placement::STATIC),
8246 0, cu->language, objfile);
8247 break;
8248 case DW_TAG_variable:
8249 if (pdi->d.locdesc)
8250 addr = decode_locdesc (pdi->d.locdesc, cu);
8251
8252 if (pdi->d.locdesc
8253 && addr == 0
8254 && !dwarf2_per_objfile->has_section_at_zero)
8255 {
8256 /* A global or static variable may also have been stripped
8257 out by the linker if unused, in which case its address
8258 will be nullified; do not add such variables into partial
8259 symbol table then. */
8260 }
8261 else if (pdi->is_external)
8262 {
8263 /* Global Variable.
8264 Don't enter into the minimal symbol tables as there is
8265 a minimal symbol table entry from the ELF symbols already.
8266 Enter into partial symbol table if it has a location
8267 descriptor or a type.
8268 If the location descriptor is missing, new_symbol will create
8269 a LOC_UNRESOLVED symbol, the address of the variable will then
8270 be determined from the minimal symbol table whenever the variable
8271 is referenced.
8272 The address for the partial symbol table entry is not
8273 used by GDB, but it comes in handy for debugging partial symbol
8274 table building. */
8275
8276 if (pdi->d.locdesc || pdi->has_type)
8277 add_psymbol_to_list (actual_name,
8278 built_actual_name != NULL,
8279 VAR_DOMAIN, LOC_STATIC,
8280 SECT_OFF_TEXT (objfile),
8281 psymbol_placement::GLOBAL,
8282 addr, cu->language, objfile);
8283 }
8284 else
8285 {
8286 int has_loc = pdi->d.locdesc != NULL;
8287
8288 /* Static Variable. Skip symbols whose value we cannot know (those
8289 without location descriptors or constant values). */
8290 if (!has_loc && !pdi->has_const_value)
8291 return;
8292
8293 add_psymbol_to_list (actual_name,
8294 built_actual_name != NULL,
8295 VAR_DOMAIN, LOC_STATIC,
8296 SECT_OFF_TEXT (objfile),
8297 psymbol_placement::STATIC,
8298 has_loc ? addr : 0,
8299 cu->language, objfile);
8300 }
8301 break;
8302 case DW_TAG_typedef:
8303 case DW_TAG_base_type:
8304 case DW_TAG_subrange_type:
8305 add_psymbol_to_list (actual_name,
8306 built_actual_name != NULL,
8307 VAR_DOMAIN, LOC_TYPEDEF, -1,
8308 psymbol_placement::STATIC,
8309 0, cu->language, objfile);
8310 break;
8311 case DW_TAG_imported_declaration:
8312 case DW_TAG_namespace:
8313 add_psymbol_to_list (actual_name,
8314 built_actual_name != NULL,
8315 VAR_DOMAIN, LOC_TYPEDEF, -1,
8316 psymbol_placement::GLOBAL,
8317 0, cu->language, objfile);
8318 break;
8319 case DW_TAG_module:
8320 /* With Fortran 77 there might be a "BLOCK DATA" module
8321 available without any name. If so, we skip the module as it
8322 doesn't bring any value. */
8323 if (actual_name != nullptr)
8324 add_psymbol_to_list (actual_name,
8325 built_actual_name != NULL,
8326 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8327 psymbol_placement::GLOBAL,
8328 0, cu->language, objfile);
8329 break;
8330 case DW_TAG_class_type:
8331 case DW_TAG_interface_type:
8332 case DW_TAG_structure_type:
8333 case DW_TAG_union_type:
8334 case DW_TAG_enumeration_type:
8335 /* Skip external references. The DWARF standard says in the section
8336 about "Structure, Union, and Class Type Entries": "An incomplete
8337 structure, union or class type is represented by a structure,
8338 union or class entry that does not have a byte size attribute
8339 and that has a DW_AT_declaration attribute." */
8340 if (!pdi->has_byte_size && pdi->is_declaration)
8341 return;
8342
8343 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8344 static vs. global. */
8345 add_psymbol_to_list (actual_name,
8346 built_actual_name != NULL,
8347 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8348 cu->language == language_cplus
8349 ? psymbol_placement::GLOBAL
8350 : psymbol_placement::STATIC,
8351 0, cu->language, objfile);
8352
8353 break;
8354 case DW_TAG_enumerator:
8355 add_psymbol_to_list (actual_name,
8356 built_actual_name != NULL,
8357 VAR_DOMAIN, LOC_CONST, -1,
8358 cu->language == language_cplus
8359 ? psymbol_placement::GLOBAL
8360 : psymbol_placement::STATIC,
8361 0, cu->language, objfile);
8362 break;
8363 default:
8364 break;
8365 }
8366 }
8367
8368 /* Read a partial die corresponding to a namespace; also, add a symbol
8369 corresponding to that namespace to the symbol table. NAMESPACE is
8370 the name of the enclosing namespace. */
8371
8372 static void
8373 add_partial_namespace (struct partial_die_info *pdi,
8374 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8375 int set_addrmap, struct dwarf2_cu *cu)
8376 {
8377 /* Add a symbol for the namespace. */
8378
8379 add_partial_symbol (pdi, cu);
8380
8381 /* Now scan partial symbols in that namespace. */
8382
8383 if (pdi->has_children)
8384 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8385 }
8386
8387 /* Read a partial die corresponding to a Fortran module. */
8388
8389 static void
8390 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8391 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8392 {
8393 /* Add a symbol for the namespace. */
8394
8395 add_partial_symbol (pdi, cu);
8396
8397 /* Now scan partial symbols in that module. */
8398
8399 if (pdi->has_children)
8400 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8401 }
8402
8403 /* Read a partial die corresponding to a subprogram or an inlined
8404 subprogram and create a partial symbol for that subprogram.
8405 When the CU language allows it, this routine also defines a partial
8406 symbol for each nested subprogram that this subprogram contains.
8407 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8408 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8409
8410 PDI may also be a lexical block, in which case we simply search
8411 recursively for subprograms defined inside that lexical block.
8412 Again, this is only performed when the CU language allows this
8413 type of definitions. */
8414
8415 static void
8416 add_partial_subprogram (struct partial_die_info *pdi,
8417 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8418 int set_addrmap, struct dwarf2_cu *cu)
8419 {
8420 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8421 {
8422 if (pdi->has_pc_info)
8423 {
8424 if (pdi->lowpc < *lowpc)
8425 *lowpc = pdi->lowpc;
8426 if (pdi->highpc > *highpc)
8427 *highpc = pdi->highpc;
8428 if (set_addrmap)
8429 {
8430 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8431 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8432 CORE_ADDR baseaddr;
8433 CORE_ADDR this_highpc;
8434 CORE_ADDR this_lowpc;
8435
8436 baseaddr = objfile->text_section_offset ();
8437 this_lowpc
8438 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8439 pdi->lowpc + baseaddr)
8440 - baseaddr);
8441 this_highpc
8442 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8443 pdi->highpc + baseaddr)
8444 - baseaddr);
8445 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8446 this_lowpc, this_highpc - 1,
8447 cu->per_cu->v.psymtab);
8448 }
8449 }
8450
8451 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8452 {
8453 if (!pdi->is_declaration)
8454 /* Ignore subprogram DIEs that do not have a name, they are
8455 illegal. Do not emit a complaint at this point, we will
8456 do so when we convert this psymtab into a symtab. */
8457 if (pdi->name)
8458 add_partial_symbol (pdi, cu);
8459 }
8460 }
8461
8462 if (! pdi->has_children)
8463 return;
8464
8465 if (cu->language == language_ada || cu->language == language_fortran)
8466 {
8467 pdi = pdi->die_child;
8468 while (pdi != NULL)
8469 {
8470 pdi->fixup (cu);
8471 if (pdi->tag == DW_TAG_subprogram
8472 || pdi->tag == DW_TAG_inlined_subroutine
8473 || pdi->tag == DW_TAG_lexical_block)
8474 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8475 pdi = pdi->die_sibling;
8476 }
8477 }
8478 }
8479
8480 /* Read a partial die corresponding to an enumeration type. */
8481
8482 static void
8483 add_partial_enumeration (struct partial_die_info *enum_pdi,
8484 struct dwarf2_cu *cu)
8485 {
8486 struct partial_die_info *pdi;
8487
8488 if (enum_pdi->name != NULL)
8489 add_partial_symbol (enum_pdi, cu);
8490
8491 pdi = enum_pdi->die_child;
8492 while (pdi)
8493 {
8494 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8495 complaint (_("malformed enumerator DIE ignored"));
8496 else
8497 add_partial_symbol (pdi, cu);
8498 pdi = pdi->die_sibling;
8499 }
8500 }
8501
8502 /* Return the initial uleb128 in the die at INFO_PTR. */
8503
8504 static unsigned int
8505 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8506 {
8507 unsigned int bytes_read;
8508
8509 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8510 }
8511
8512 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8513 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8514
8515 Return the corresponding abbrev, or NULL if the number is zero (indicating
8516 an empty DIE). In either case *BYTES_READ will be set to the length of
8517 the initial number. */
8518
8519 static struct abbrev_info *
8520 peek_die_abbrev (const die_reader_specs &reader,
8521 const gdb_byte *info_ptr, unsigned int *bytes_read)
8522 {
8523 dwarf2_cu *cu = reader.cu;
8524 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8525 unsigned int abbrev_number
8526 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8527
8528 if (abbrev_number == 0)
8529 return NULL;
8530
8531 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8532 if (!abbrev)
8533 {
8534 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8535 " at offset %s [in module %s]"),
8536 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8537 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8538 }
8539
8540 return abbrev;
8541 }
8542
8543 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8544 Returns a pointer to the end of a series of DIEs, terminated by an empty
8545 DIE. Any children of the skipped DIEs will also be skipped. */
8546
8547 static const gdb_byte *
8548 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8549 {
8550 while (1)
8551 {
8552 unsigned int bytes_read;
8553 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8554
8555 if (abbrev == NULL)
8556 return info_ptr + bytes_read;
8557 else
8558 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8559 }
8560 }
8561
8562 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8563 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8564 abbrev corresponding to that skipped uleb128 should be passed in
8565 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8566 children. */
8567
8568 static const gdb_byte *
8569 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8570 struct abbrev_info *abbrev)
8571 {
8572 unsigned int bytes_read;
8573 struct attribute attr;
8574 bfd *abfd = reader->abfd;
8575 struct dwarf2_cu *cu = reader->cu;
8576 const gdb_byte *buffer = reader->buffer;
8577 const gdb_byte *buffer_end = reader->buffer_end;
8578 unsigned int form, i;
8579
8580 for (i = 0; i < abbrev->num_attrs; i++)
8581 {
8582 /* The only abbrev we care about is DW_AT_sibling. */
8583 if (abbrev->attrs[i].name == DW_AT_sibling)
8584 {
8585 bool ignored;
8586 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8587 &ignored);
8588 if (attr.form == DW_FORM_ref_addr)
8589 complaint (_("ignoring absolute DW_AT_sibling"));
8590 else
8591 {
8592 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8593 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8594
8595 if (sibling_ptr < info_ptr)
8596 complaint (_("DW_AT_sibling points backwards"));
8597 else if (sibling_ptr > reader->buffer_end)
8598 reader->die_section->overflow_complaint ();
8599 else
8600 return sibling_ptr;
8601 }
8602 }
8603
8604 /* If it isn't DW_AT_sibling, skip this attribute. */
8605 form = abbrev->attrs[i].form;
8606 skip_attribute:
8607 switch (form)
8608 {
8609 case DW_FORM_ref_addr:
8610 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8611 and later it is offset sized. */
8612 if (cu->header.version == 2)
8613 info_ptr += cu->header.addr_size;
8614 else
8615 info_ptr += cu->header.offset_size;
8616 break;
8617 case DW_FORM_GNU_ref_alt:
8618 info_ptr += cu->header.offset_size;
8619 break;
8620 case DW_FORM_addr:
8621 info_ptr += cu->header.addr_size;
8622 break;
8623 case DW_FORM_data1:
8624 case DW_FORM_ref1:
8625 case DW_FORM_flag:
8626 case DW_FORM_strx1:
8627 info_ptr += 1;
8628 break;
8629 case DW_FORM_flag_present:
8630 case DW_FORM_implicit_const:
8631 break;
8632 case DW_FORM_data2:
8633 case DW_FORM_ref2:
8634 case DW_FORM_strx2:
8635 info_ptr += 2;
8636 break;
8637 case DW_FORM_strx3:
8638 info_ptr += 3;
8639 break;
8640 case DW_FORM_data4:
8641 case DW_FORM_ref4:
8642 case DW_FORM_strx4:
8643 info_ptr += 4;
8644 break;
8645 case DW_FORM_data8:
8646 case DW_FORM_ref8:
8647 case DW_FORM_ref_sig8:
8648 info_ptr += 8;
8649 break;
8650 case DW_FORM_data16:
8651 info_ptr += 16;
8652 break;
8653 case DW_FORM_string:
8654 read_direct_string (abfd, info_ptr, &bytes_read);
8655 info_ptr += bytes_read;
8656 break;
8657 case DW_FORM_sec_offset:
8658 case DW_FORM_strp:
8659 case DW_FORM_GNU_strp_alt:
8660 info_ptr += cu->header.offset_size;
8661 break;
8662 case DW_FORM_exprloc:
8663 case DW_FORM_block:
8664 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8665 info_ptr += bytes_read;
8666 break;
8667 case DW_FORM_block1:
8668 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8669 break;
8670 case DW_FORM_block2:
8671 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8672 break;
8673 case DW_FORM_block4:
8674 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8675 break;
8676 case DW_FORM_addrx:
8677 case DW_FORM_strx:
8678 case DW_FORM_sdata:
8679 case DW_FORM_udata:
8680 case DW_FORM_ref_udata:
8681 case DW_FORM_GNU_addr_index:
8682 case DW_FORM_GNU_str_index:
8683 case DW_FORM_rnglistx:
8684 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8685 break;
8686 case DW_FORM_indirect:
8687 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8688 info_ptr += bytes_read;
8689 /* We need to continue parsing from here, so just go back to
8690 the top. */
8691 goto skip_attribute;
8692
8693 default:
8694 error (_("Dwarf Error: Cannot handle %s "
8695 "in DWARF reader [in module %s]"),
8696 dwarf_form_name (form),
8697 bfd_get_filename (abfd));
8698 }
8699 }
8700
8701 if (abbrev->has_children)
8702 return skip_children (reader, info_ptr);
8703 else
8704 return info_ptr;
8705 }
8706
8707 /* Locate ORIG_PDI's sibling.
8708 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8709
8710 static const gdb_byte *
8711 locate_pdi_sibling (const struct die_reader_specs *reader,
8712 struct partial_die_info *orig_pdi,
8713 const gdb_byte *info_ptr)
8714 {
8715 /* Do we know the sibling already? */
8716
8717 if (orig_pdi->sibling)
8718 return orig_pdi->sibling;
8719
8720 /* Are there any children to deal with? */
8721
8722 if (!orig_pdi->has_children)
8723 return info_ptr;
8724
8725 /* Skip the children the long way. */
8726
8727 return skip_children (reader, info_ptr);
8728 }
8729
8730 /* Expand this partial symbol table into a full symbol table. SELF is
8731 not NULL. */
8732
8733 void
8734 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8735 {
8736 struct dwarf2_per_objfile *dwarf2_per_objfile
8737 = get_dwarf2_per_objfile (objfile);
8738
8739 gdb_assert (!readin);
8740 /* If this psymtab is constructed from a debug-only objfile, the
8741 has_section_at_zero flag will not necessarily be correct. We
8742 can get the correct value for this flag by looking at the data
8743 associated with the (presumably stripped) associated objfile. */
8744 if (objfile->separate_debug_objfile_backlink)
8745 {
8746 struct dwarf2_per_objfile *dpo_backlink
8747 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8748
8749 dwarf2_per_objfile->has_section_at_zero
8750 = dpo_backlink->has_section_at_zero;
8751 }
8752
8753 expand_psymtab (objfile);
8754
8755 process_cu_includes (dwarf2_per_objfile);
8756 }
8757 \f
8758 /* Reading in full CUs. */
8759
8760 /* Add PER_CU to the queue. */
8761
8762 static void
8763 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8764 enum language pretend_language)
8765 {
8766 per_cu->queued = 1;
8767 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8768 }
8769
8770 /* If PER_CU is not yet queued, add it to the queue.
8771 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8772 dependency.
8773 The result is non-zero if PER_CU was queued, otherwise the result is zero
8774 meaning either PER_CU is already queued or it is already loaded.
8775
8776 N.B. There is an invariant here that if a CU is queued then it is loaded.
8777 The caller is required to load PER_CU if we return non-zero. */
8778
8779 static int
8780 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8781 struct dwarf2_per_cu_data *per_cu,
8782 enum language pretend_language)
8783 {
8784 /* We may arrive here during partial symbol reading, if we need full
8785 DIEs to process an unusual case (e.g. template arguments). Do
8786 not queue PER_CU, just tell our caller to load its DIEs. */
8787 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8788 {
8789 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8790 return 1;
8791 return 0;
8792 }
8793
8794 /* Mark the dependence relation so that we don't flush PER_CU
8795 too early. */
8796 if (dependent_cu != NULL)
8797 dwarf2_add_dependence (dependent_cu, per_cu);
8798
8799 /* If it's already on the queue, we have nothing to do. */
8800 if (per_cu->queued)
8801 return 0;
8802
8803 /* If the compilation unit is already loaded, just mark it as
8804 used. */
8805 if (per_cu->cu != NULL)
8806 {
8807 per_cu->cu->last_used = 0;
8808 return 0;
8809 }
8810
8811 /* Add it to the queue. */
8812 queue_comp_unit (per_cu, pretend_language);
8813
8814 return 1;
8815 }
8816
8817 /* Process the queue. */
8818
8819 static void
8820 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8821 {
8822 if (dwarf_read_debug)
8823 {
8824 fprintf_unfiltered (gdb_stdlog,
8825 "Expanding one or more symtabs of objfile %s ...\n",
8826 objfile_name (dwarf2_per_objfile->objfile));
8827 }
8828
8829 /* The queue starts out with one item, but following a DIE reference
8830 may load a new CU, adding it to the end of the queue. */
8831 while (!dwarf2_per_objfile->queue.empty ())
8832 {
8833 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8834
8835 if ((dwarf2_per_objfile->using_index
8836 ? !item.per_cu->v.quick->compunit_symtab
8837 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8838 /* Skip dummy CUs. */
8839 && item.per_cu->cu != NULL)
8840 {
8841 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8842 unsigned int debug_print_threshold;
8843 char buf[100];
8844
8845 if (per_cu->is_debug_types)
8846 {
8847 struct signatured_type *sig_type =
8848 (struct signatured_type *) per_cu;
8849
8850 sprintf (buf, "TU %s at offset %s",
8851 hex_string (sig_type->signature),
8852 sect_offset_str (per_cu->sect_off));
8853 /* There can be 100s of TUs.
8854 Only print them in verbose mode. */
8855 debug_print_threshold = 2;
8856 }
8857 else
8858 {
8859 sprintf (buf, "CU at offset %s",
8860 sect_offset_str (per_cu->sect_off));
8861 debug_print_threshold = 1;
8862 }
8863
8864 if (dwarf_read_debug >= debug_print_threshold)
8865 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8866
8867 if (per_cu->is_debug_types)
8868 process_full_type_unit (per_cu, item.pretend_language);
8869 else
8870 process_full_comp_unit (per_cu, item.pretend_language);
8871
8872 if (dwarf_read_debug >= debug_print_threshold)
8873 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8874 }
8875
8876 item.per_cu->queued = 0;
8877 dwarf2_per_objfile->queue.pop ();
8878 }
8879
8880 if (dwarf_read_debug)
8881 {
8882 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8883 objfile_name (dwarf2_per_objfile->objfile));
8884 }
8885 }
8886
8887 /* Read in full symbols for PST, and anything it depends on. */
8888
8889 void
8890 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8891 {
8892 if (readin)
8893 return;
8894
8895 read_dependencies (objfile);
8896
8897 dw2_do_instantiate_symtab (per_cu_data, false);
8898 gdb_assert (get_compunit_symtab () != nullptr);
8899 }
8900
8901 /* Trivial hash function for die_info: the hash value of a DIE
8902 is its offset in .debug_info for this objfile. */
8903
8904 static hashval_t
8905 die_hash (const void *item)
8906 {
8907 const struct die_info *die = (const struct die_info *) item;
8908
8909 return to_underlying (die->sect_off);
8910 }
8911
8912 /* Trivial comparison function for die_info structures: two DIEs
8913 are equal if they have the same offset. */
8914
8915 static int
8916 die_eq (const void *item_lhs, const void *item_rhs)
8917 {
8918 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8919 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8920
8921 return die_lhs->sect_off == die_rhs->sect_off;
8922 }
8923
8924 /* Load the DIEs associated with PER_CU into memory. */
8925
8926 static void
8927 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8928 bool skip_partial,
8929 enum language pretend_language)
8930 {
8931 gdb_assert (! this_cu->is_debug_types);
8932
8933 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8934 if (reader.dummy_p)
8935 return;
8936
8937 struct dwarf2_cu *cu = reader.cu;
8938 const gdb_byte *info_ptr = reader.info_ptr;
8939
8940 gdb_assert (cu->die_hash == NULL);
8941 cu->die_hash =
8942 htab_create_alloc_ex (cu->header.length / 12,
8943 die_hash,
8944 die_eq,
8945 NULL,
8946 &cu->comp_unit_obstack,
8947 hashtab_obstack_allocate,
8948 dummy_obstack_deallocate);
8949
8950 if (reader.comp_unit_die->has_children)
8951 reader.comp_unit_die->child
8952 = read_die_and_siblings (&reader, reader.info_ptr,
8953 &info_ptr, reader.comp_unit_die);
8954 cu->dies = reader.comp_unit_die;
8955 /* comp_unit_die is not stored in die_hash, no need. */
8956
8957 /* We try not to read any attributes in this function, because not
8958 all CUs needed for references have been loaded yet, and symbol
8959 table processing isn't initialized. But we have to set the CU language,
8960 or we won't be able to build types correctly.
8961 Similarly, if we do not read the producer, we can not apply
8962 producer-specific interpretation. */
8963 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8964
8965 reader.keep ();
8966 }
8967
8968 /* Add a DIE to the delayed physname list. */
8969
8970 static void
8971 add_to_method_list (struct type *type, int fnfield_index, int index,
8972 const char *name, struct die_info *die,
8973 struct dwarf2_cu *cu)
8974 {
8975 struct delayed_method_info mi;
8976 mi.type = type;
8977 mi.fnfield_index = fnfield_index;
8978 mi.index = index;
8979 mi.name = name;
8980 mi.die = die;
8981 cu->method_list.push_back (mi);
8982 }
8983
8984 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8985 "const" / "volatile". If so, decrements LEN by the length of the
8986 modifier and return true. Otherwise return false. */
8987
8988 template<size_t N>
8989 static bool
8990 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8991 {
8992 size_t mod_len = sizeof (mod) - 1;
8993 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8994 {
8995 len -= mod_len;
8996 return true;
8997 }
8998 return false;
8999 }
9000
9001 /* Compute the physnames of any methods on the CU's method list.
9002
9003 The computation of method physnames is delayed in order to avoid the
9004 (bad) condition that one of the method's formal parameters is of an as yet
9005 incomplete type. */
9006
9007 static void
9008 compute_delayed_physnames (struct dwarf2_cu *cu)
9009 {
9010 /* Only C++ delays computing physnames. */
9011 if (cu->method_list.empty ())
9012 return;
9013 gdb_assert (cu->language == language_cplus);
9014
9015 for (const delayed_method_info &mi : cu->method_list)
9016 {
9017 const char *physname;
9018 struct fn_fieldlist *fn_flp
9019 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9020 physname = dwarf2_physname (mi.name, mi.die, cu);
9021 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9022 = physname ? physname : "";
9023
9024 /* Since there's no tag to indicate whether a method is a
9025 const/volatile overload, extract that information out of the
9026 demangled name. */
9027 if (physname != NULL)
9028 {
9029 size_t len = strlen (physname);
9030
9031 while (1)
9032 {
9033 if (physname[len] == ')') /* shortcut */
9034 break;
9035 else if (check_modifier (physname, len, " const"))
9036 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9037 else if (check_modifier (physname, len, " volatile"))
9038 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9039 else
9040 break;
9041 }
9042 }
9043 }
9044
9045 /* The list is no longer needed. */
9046 cu->method_list.clear ();
9047 }
9048
9049 /* Go objects should be embedded in a DW_TAG_module DIE,
9050 and it's not clear if/how imported objects will appear.
9051 To keep Go support simple until that's worked out,
9052 go back through what we've read and create something usable.
9053 We could do this while processing each DIE, and feels kinda cleaner,
9054 but that way is more invasive.
9055 This is to, for example, allow the user to type "p var" or "b main"
9056 without having to specify the package name, and allow lookups
9057 of module.object to work in contexts that use the expression
9058 parser. */
9059
9060 static void
9061 fixup_go_packaging (struct dwarf2_cu *cu)
9062 {
9063 gdb::unique_xmalloc_ptr<char> package_name;
9064 struct pending *list;
9065 int i;
9066
9067 for (list = *cu->get_builder ()->get_global_symbols ();
9068 list != NULL;
9069 list = list->next)
9070 {
9071 for (i = 0; i < list->nsyms; ++i)
9072 {
9073 struct symbol *sym = list->symbol[i];
9074
9075 if (sym->language () == language_go
9076 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9077 {
9078 gdb::unique_xmalloc_ptr<char> this_package_name
9079 (go_symbol_package_name (sym));
9080
9081 if (this_package_name == NULL)
9082 continue;
9083 if (package_name == NULL)
9084 package_name = std::move (this_package_name);
9085 else
9086 {
9087 struct objfile *objfile
9088 = cu->per_cu->dwarf2_per_objfile->objfile;
9089 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9090 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9091 (symbol_symtab (sym) != NULL
9092 ? symtab_to_filename_for_display
9093 (symbol_symtab (sym))
9094 : objfile_name (objfile)),
9095 this_package_name.get (), package_name.get ());
9096 }
9097 }
9098 }
9099 }
9100
9101 if (package_name != NULL)
9102 {
9103 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9104 const char *saved_package_name = objfile->intern (package_name.get ());
9105 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9106 saved_package_name);
9107 struct symbol *sym;
9108
9109 sym = allocate_symbol (objfile);
9110 sym->set_language (language_go, &objfile->objfile_obstack);
9111 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9112 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9113 e.g., "main" finds the "main" module and not C's main(). */
9114 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9115 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9116 SYMBOL_TYPE (sym) = type;
9117
9118 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9119 }
9120 }
9121
9122 /* Allocate a fully-qualified name consisting of the two parts on the
9123 obstack. */
9124
9125 static const char *
9126 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9127 {
9128 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9129 }
9130
9131 /* A helper that allocates a struct discriminant_info to attach to a
9132 union type. */
9133
9134 static struct discriminant_info *
9135 alloc_discriminant_info (struct type *type, int discriminant_index,
9136 int default_index)
9137 {
9138 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9139 gdb_assert (discriminant_index == -1
9140 || (discriminant_index >= 0
9141 && discriminant_index < TYPE_NFIELDS (type)));
9142 gdb_assert (default_index == -1
9143 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9144
9145 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9146
9147 struct discriminant_info *disc
9148 = ((struct discriminant_info *)
9149 TYPE_ZALLOC (type,
9150 offsetof (struct discriminant_info, discriminants)
9151 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9152 disc->default_index = default_index;
9153 disc->discriminant_index = discriminant_index;
9154
9155 struct dynamic_prop prop;
9156 prop.kind = PROP_UNDEFINED;
9157 prop.data.baton = disc;
9158
9159 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9160
9161 return disc;
9162 }
9163
9164 /* Some versions of rustc emitted enums in an unusual way.
9165
9166 Ordinary enums were emitted as unions. The first element of each
9167 structure in the union was named "RUST$ENUM$DISR". This element
9168 held the discriminant.
9169
9170 These versions of Rust also implemented the "non-zero"
9171 optimization. When the enum had two values, and one is empty and
9172 the other holds a pointer that cannot be zero, the pointer is used
9173 as the discriminant, with a zero value meaning the empty variant.
9174 Here, the union's first member is of the form
9175 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9176 where the fieldnos are the indices of the fields that should be
9177 traversed in order to find the field (which may be several fields deep)
9178 and the variantname is the name of the variant of the case when the
9179 field is zero.
9180
9181 This function recognizes whether TYPE is of one of these forms,
9182 and, if so, smashes it to be a variant type. */
9183
9184 static void
9185 quirk_rust_enum (struct type *type, struct objfile *objfile)
9186 {
9187 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9188
9189 /* We don't need to deal with empty enums. */
9190 if (TYPE_NFIELDS (type) == 0)
9191 return;
9192
9193 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9194 if (TYPE_NFIELDS (type) == 1
9195 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9196 {
9197 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9198
9199 /* Decode the field name to find the offset of the
9200 discriminant. */
9201 ULONGEST bit_offset = 0;
9202 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9203 while (name[0] >= '0' && name[0] <= '9')
9204 {
9205 char *tail;
9206 unsigned long index = strtoul (name, &tail, 10);
9207 name = tail;
9208 if (*name != '$'
9209 || index >= TYPE_NFIELDS (field_type)
9210 || (TYPE_FIELD_LOC_KIND (field_type, index)
9211 != FIELD_LOC_KIND_BITPOS))
9212 {
9213 complaint (_("Could not parse Rust enum encoding string \"%s\""
9214 "[in module %s]"),
9215 TYPE_FIELD_NAME (type, 0),
9216 objfile_name (objfile));
9217 return;
9218 }
9219 ++name;
9220
9221 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9222 field_type = TYPE_FIELD_TYPE (field_type, index);
9223 }
9224
9225 /* Make a union to hold the variants. */
9226 struct type *union_type = alloc_type (objfile);
9227 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9228 TYPE_NFIELDS (union_type) = 3;
9229 TYPE_FIELDS (union_type)
9230 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9231 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9232 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9233
9234 /* Put the discriminant must at index 0. */
9235 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9236 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9237 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9238 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9239
9240 /* The order of fields doesn't really matter, so put the real
9241 field at index 1 and the data-less field at index 2. */
9242 struct discriminant_info *disc
9243 = alloc_discriminant_info (union_type, 0, 1);
9244 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9245 TYPE_FIELD_NAME (union_type, 1)
9246 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9247 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9248 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9249 TYPE_FIELD_NAME (union_type, 1));
9250
9251 const char *dataless_name
9252 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9253 name);
9254 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9255 dataless_name);
9256 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9257 /* NAME points into the original discriminant name, which
9258 already has the correct lifetime. */
9259 TYPE_FIELD_NAME (union_type, 2) = name;
9260 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9261 disc->discriminants[2] = 0;
9262
9263 /* Smash this type to be a structure type. We have to do this
9264 because the type has already been recorded. */
9265 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9266 TYPE_NFIELDS (type) = 1;
9267 TYPE_FIELDS (type)
9268 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9269
9270 /* Install the variant part. */
9271 TYPE_FIELD_TYPE (type, 0) = union_type;
9272 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9273 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9274 }
9275 /* A union with a single anonymous field is probably an old-style
9276 univariant enum. */
9277 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9278 {
9279 /* Smash this type to be a structure type. We have to do this
9280 because the type has already been recorded. */
9281 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9282
9283 /* Make a union to hold the variants. */
9284 struct type *union_type = alloc_type (objfile);
9285 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9286 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9287 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9288 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9289 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9290
9291 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9292 const char *variant_name
9293 = rust_last_path_segment (TYPE_NAME (field_type));
9294 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9295 TYPE_NAME (field_type)
9296 = rust_fully_qualify (&objfile->objfile_obstack,
9297 TYPE_NAME (type), variant_name);
9298
9299 /* Install the union in the outer struct type. */
9300 TYPE_NFIELDS (type) = 1;
9301 TYPE_FIELDS (type)
9302 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9303 TYPE_FIELD_TYPE (type, 0) = union_type;
9304 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9305 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9306
9307 alloc_discriminant_info (union_type, -1, 0);
9308 }
9309 else
9310 {
9311 struct type *disr_type = nullptr;
9312 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9313 {
9314 disr_type = TYPE_FIELD_TYPE (type, i);
9315
9316 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9317 {
9318 /* All fields of a true enum will be structs. */
9319 return;
9320 }
9321 else if (TYPE_NFIELDS (disr_type) == 0)
9322 {
9323 /* Could be data-less variant, so keep going. */
9324 disr_type = nullptr;
9325 }
9326 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9327 "RUST$ENUM$DISR") != 0)
9328 {
9329 /* Not a Rust enum. */
9330 return;
9331 }
9332 else
9333 {
9334 /* Found one. */
9335 break;
9336 }
9337 }
9338
9339 /* If we got here without a discriminant, then it's probably
9340 just a union. */
9341 if (disr_type == nullptr)
9342 return;
9343
9344 /* Smash this type to be a structure type. We have to do this
9345 because the type has already been recorded. */
9346 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9347
9348 /* Make a union to hold the variants. */
9349 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9350 struct type *union_type = alloc_type (objfile);
9351 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9352 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9353 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9354 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9355 TYPE_FIELDS (union_type)
9356 = (struct field *) TYPE_ZALLOC (union_type,
9357 (TYPE_NFIELDS (union_type)
9358 * sizeof (struct field)));
9359
9360 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9361 TYPE_NFIELDS (type) * sizeof (struct field));
9362
9363 /* Install the discriminant at index 0 in the union. */
9364 TYPE_FIELD (union_type, 0) = *disr_field;
9365 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9366 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9367
9368 /* Install the union in the outer struct type. */
9369 TYPE_FIELD_TYPE (type, 0) = union_type;
9370 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9371 TYPE_NFIELDS (type) = 1;
9372
9373 /* Set the size and offset of the union type. */
9374 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9375
9376 /* We need a way to find the correct discriminant given a
9377 variant name. For convenience we build a map here. */
9378 struct type *enum_type = FIELD_TYPE (*disr_field);
9379 std::unordered_map<std::string, ULONGEST> discriminant_map;
9380 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9381 {
9382 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9383 {
9384 const char *name
9385 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9386 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9387 }
9388 }
9389
9390 int n_fields = TYPE_NFIELDS (union_type);
9391 struct discriminant_info *disc
9392 = alloc_discriminant_info (union_type, 0, -1);
9393 /* Skip the discriminant here. */
9394 for (int i = 1; i < n_fields; ++i)
9395 {
9396 /* Find the final word in the name of this variant's type.
9397 That name can be used to look up the correct
9398 discriminant. */
9399 const char *variant_name
9400 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9401 i)));
9402
9403 auto iter = discriminant_map.find (variant_name);
9404 if (iter != discriminant_map.end ())
9405 disc->discriminants[i] = iter->second;
9406
9407 /* Remove the discriminant field, if it exists. */
9408 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9409 if (TYPE_NFIELDS (sub_type) > 0)
9410 {
9411 --TYPE_NFIELDS (sub_type);
9412 ++TYPE_FIELDS (sub_type);
9413 }
9414 TYPE_FIELD_NAME (union_type, i) = variant_name;
9415 TYPE_NAME (sub_type)
9416 = rust_fully_qualify (&objfile->objfile_obstack,
9417 TYPE_NAME (type), variant_name);
9418 }
9419 }
9420 }
9421
9422 /* Rewrite some Rust unions to be structures with variants parts. */
9423
9424 static void
9425 rust_union_quirks (struct dwarf2_cu *cu)
9426 {
9427 gdb_assert (cu->language == language_rust);
9428 for (type *type_ : cu->rust_unions)
9429 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9430 /* We don't need this any more. */
9431 cu->rust_unions.clear ();
9432 }
9433
9434 /* Return the symtab for PER_CU. This works properly regardless of
9435 whether we're using the index or psymtabs. */
9436
9437 static struct compunit_symtab *
9438 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9439 {
9440 return (per_cu->dwarf2_per_objfile->using_index
9441 ? per_cu->v.quick->compunit_symtab
9442 : per_cu->v.psymtab->compunit_symtab);
9443 }
9444
9445 /* A helper function for computing the list of all symbol tables
9446 included by PER_CU. */
9447
9448 static void
9449 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9450 htab_t all_children, htab_t all_type_symtabs,
9451 struct dwarf2_per_cu_data *per_cu,
9452 struct compunit_symtab *immediate_parent)
9453 {
9454 void **slot;
9455 struct compunit_symtab *cust;
9456
9457 slot = htab_find_slot (all_children, per_cu, INSERT);
9458 if (*slot != NULL)
9459 {
9460 /* This inclusion and its children have been processed. */
9461 return;
9462 }
9463
9464 *slot = per_cu;
9465 /* Only add a CU if it has a symbol table. */
9466 cust = get_compunit_symtab (per_cu);
9467 if (cust != NULL)
9468 {
9469 /* If this is a type unit only add its symbol table if we haven't
9470 seen it yet (type unit per_cu's can share symtabs). */
9471 if (per_cu->is_debug_types)
9472 {
9473 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9474 if (*slot == NULL)
9475 {
9476 *slot = cust;
9477 result->push_back (cust);
9478 if (cust->user == NULL)
9479 cust->user = immediate_parent;
9480 }
9481 }
9482 else
9483 {
9484 result->push_back (cust);
9485 if (cust->user == NULL)
9486 cust->user = immediate_parent;
9487 }
9488 }
9489
9490 if (!per_cu->imported_symtabs_empty ())
9491 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9492 {
9493 recursively_compute_inclusions (result, all_children,
9494 all_type_symtabs, ptr, cust);
9495 }
9496 }
9497
9498 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9499 PER_CU. */
9500
9501 static void
9502 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9503 {
9504 gdb_assert (! per_cu->is_debug_types);
9505
9506 if (!per_cu->imported_symtabs_empty ())
9507 {
9508 int len;
9509 std::vector<compunit_symtab *> result_symtabs;
9510 htab_t all_children, all_type_symtabs;
9511 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9512
9513 /* If we don't have a symtab, we can just skip this case. */
9514 if (cust == NULL)
9515 return;
9516
9517 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9518 NULL, xcalloc, xfree);
9519 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9520 NULL, xcalloc, xfree);
9521
9522 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9523 {
9524 recursively_compute_inclusions (&result_symtabs, all_children,
9525 all_type_symtabs, ptr, cust);
9526 }
9527
9528 /* Now we have a transitive closure of all the included symtabs. */
9529 len = result_symtabs.size ();
9530 cust->includes
9531 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9532 struct compunit_symtab *, len + 1);
9533 memcpy (cust->includes, result_symtabs.data (),
9534 len * sizeof (compunit_symtab *));
9535 cust->includes[len] = NULL;
9536
9537 htab_delete (all_children);
9538 htab_delete (all_type_symtabs);
9539 }
9540 }
9541
9542 /* Compute the 'includes' field for the symtabs of all the CUs we just
9543 read. */
9544
9545 static void
9546 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9547 {
9548 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9549 {
9550 if (! iter->is_debug_types)
9551 compute_compunit_symtab_includes (iter);
9552 }
9553
9554 dwarf2_per_objfile->just_read_cus.clear ();
9555 }
9556
9557 /* Generate full symbol information for PER_CU, whose DIEs have
9558 already been loaded into memory. */
9559
9560 static void
9561 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9562 enum language pretend_language)
9563 {
9564 struct dwarf2_cu *cu = per_cu->cu;
9565 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9566 struct objfile *objfile = dwarf2_per_objfile->objfile;
9567 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9568 CORE_ADDR lowpc, highpc;
9569 struct compunit_symtab *cust;
9570 CORE_ADDR baseaddr;
9571 struct block *static_block;
9572 CORE_ADDR addr;
9573
9574 baseaddr = objfile->text_section_offset ();
9575
9576 /* Clear the list here in case something was left over. */
9577 cu->method_list.clear ();
9578
9579 cu->language = pretend_language;
9580 cu->language_defn = language_def (cu->language);
9581
9582 /* Do line number decoding in read_file_scope () */
9583 process_die (cu->dies, cu);
9584
9585 /* For now fudge the Go package. */
9586 if (cu->language == language_go)
9587 fixup_go_packaging (cu);
9588
9589 /* Now that we have processed all the DIEs in the CU, all the types
9590 should be complete, and it should now be safe to compute all of the
9591 physnames. */
9592 compute_delayed_physnames (cu);
9593
9594 if (cu->language == language_rust)
9595 rust_union_quirks (cu);
9596
9597 /* Some compilers don't define a DW_AT_high_pc attribute for the
9598 compilation unit. If the DW_AT_high_pc is missing, synthesize
9599 it, by scanning the DIE's below the compilation unit. */
9600 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9601
9602 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9603 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9604
9605 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9606 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9607 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9608 addrmap to help ensure it has an accurate map of pc values belonging to
9609 this comp unit. */
9610 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9611
9612 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9613 SECT_OFF_TEXT (objfile),
9614 0);
9615
9616 if (cust != NULL)
9617 {
9618 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9619
9620 /* Set symtab language to language from DW_AT_language. If the
9621 compilation is from a C file generated by language preprocessors, do
9622 not set the language if it was already deduced by start_subfile. */
9623 if (!(cu->language == language_c
9624 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9625 COMPUNIT_FILETABS (cust)->language = cu->language;
9626
9627 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9628 produce DW_AT_location with location lists but it can be possibly
9629 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9630 there were bugs in prologue debug info, fixed later in GCC-4.5
9631 by "unwind info for epilogues" patch (which is not directly related).
9632
9633 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9634 needed, it would be wrong due to missing DW_AT_producer there.
9635
9636 Still one can confuse GDB by using non-standard GCC compilation
9637 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9638 */
9639 if (cu->has_loclist && gcc_4_minor >= 5)
9640 cust->locations_valid = 1;
9641
9642 if (gcc_4_minor >= 5)
9643 cust->epilogue_unwind_valid = 1;
9644
9645 cust->call_site_htab = cu->call_site_htab;
9646 }
9647
9648 if (dwarf2_per_objfile->using_index)
9649 per_cu->v.quick->compunit_symtab = cust;
9650 else
9651 {
9652 dwarf2_psymtab *pst = per_cu->v.psymtab;
9653 pst->compunit_symtab = cust;
9654 pst->readin = true;
9655 }
9656
9657 /* Push it for inclusion processing later. */
9658 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9659
9660 /* Not needed any more. */
9661 cu->reset_builder ();
9662 }
9663
9664 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9665 already been loaded into memory. */
9666
9667 static void
9668 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9669 enum language pretend_language)
9670 {
9671 struct dwarf2_cu *cu = per_cu->cu;
9672 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9673 struct objfile *objfile = dwarf2_per_objfile->objfile;
9674 struct compunit_symtab *cust;
9675 struct signatured_type *sig_type;
9676
9677 gdb_assert (per_cu->is_debug_types);
9678 sig_type = (struct signatured_type *) per_cu;
9679
9680 /* Clear the list here in case something was left over. */
9681 cu->method_list.clear ();
9682
9683 cu->language = pretend_language;
9684 cu->language_defn = language_def (cu->language);
9685
9686 /* The symbol tables are set up in read_type_unit_scope. */
9687 process_die (cu->dies, cu);
9688
9689 /* For now fudge the Go package. */
9690 if (cu->language == language_go)
9691 fixup_go_packaging (cu);
9692
9693 /* Now that we have processed all the DIEs in the CU, all the types
9694 should be complete, and it should now be safe to compute all of the
9695 physnames. */
9696 compute_delayed_physnames (cu);
9697
9698 if (cu->language == language_rust)
9699 rust_union_quirks (cu);
9700
9701 /* TUs share symbol tables.
9702 If this is the first TU to use this symtab, complete the construction
9703 of it with end_expandable_symtab. Otherwise, complete the addition of
9704 this TU's symbols to the existing symtab. */
9705 if (sig_type->type_unit_group->compunit_symtab == NULL)
9706 {
9707 buildsym_compunit *builder = cu->get_builder ();
9708 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9709 sig_type->type_unit_group->compunit_symtab = cust;
9710
9711 if (cust != NULL)
9712 {
9713 /* Set symtab language to language from DW_AT_language. If the
9714 compilation is from a C file generated by language preprocessors,
9715 do not set the language if it was already deduced by
9716 start_subfile. */
9717 if (!(cu->language == language_c
9718 && COMPUNIT_FILETABS (cust)->language != language_c))
9719 COMPUNIT_FILETABS (cust)->language = cu->language;
9720 }
9721 }
9722 else
9723 {
9724 cu->get_builder ()->augment_type_symtab ();
9725 cust = sig_type->type_unit_group->compunit_symtab;
9726 }
9727
9728 if (dwarf2_per_objfile->using_index)
9729 per_cu->v.quick->compunit_symtab = cust;
9730 else
9731 {
9732 dwarf2_psymtab *pst = per_cu->v.psymtab;
9733 pst->compunit_symtab = cust;
9734 pst->readin = true;
9735 }
9736
9737 /* Not needed any more. */
9738 cu->reset_builder ();
9739 }
9740
9741 /* Process an imported unit DIE. */
9742
9743 static void
9744 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9745 {
9746 struct attribute *attr;
9747
9748 /* For now we don't handle imported units in type units. */
9749 if (cu->per_cu->is_debug_types)
9750 {
9751 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9752 " supported in type units [in module %s]"),
9753 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9754 }
9755
9756 attr = dwarf2_attr (die, DW_AT_import, cu);
9757 if (attr != NULL)
9758 {
9759 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9760 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9761 dwarf2_per_cu_data *per_cu
9762 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9763 cu->per_cu->dwarf2_per_objfile);
9764
9765 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9766 into another compilation unit, at root level. Regard this as a hint,
9767 and ignore it. */
9768 if (die->parent && die->parent->parent == NULL
9769 && per_cu->unit_type == DW_UT_compile
9770 && per_cu->lang == language_cplus)
9771 return;
9772
9773 /* If necessary, add it to the queue and load its DIEs. */
9774 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9775 load_full_comp_unit (per_cu, false, cu->language);
9776
9777 cu->per_cu->imported_symtabs_push (per_cu);
9778 }
9779 }
9780
9781 /* RAII object that represents a process_die scope: i.e.,
9782 starts/finishes processing a DIE. */
9783 class process_die_scope
9784 {
9785 public:
9786 process_die_scope (die_info *die, dwarf2_cu *cu)
9787 : m_die (die), m_cu (cu)
9788 {
9789 /* We should only be processing DIEs not already in process. */
9790 gdb_assert (!m_die->in_process);
9791 m_die->in_process = true;
9792 }
9793
9794 ~process_die_scope ()
9795 {
9796 m_die->in_process = false;
9797
9798 /* If we're done processing the DIE for the CU that owns the line
9799 header, we don't need the line header anymore. */
9800 if (m_cu->line_header_die_owner == m_die)
9801 {
9802 delete m_cu->line_header;
9803 m_cu->line_header = NULL;
9804 m_cu->line_header_die_owner = NULL;
9805 }
9806 }
9807
9808 private:
9809 die_info *m_die;
9810 dwarf2_cu *m_cu;
9811 };
9812
9813 /* Process a die and its children. */
9814
9815 static void
9816 process_die (struct die_info *die, struct dwarf2_cu *cu)
9817 {
9818 process_die_scope scope (die, cu);
9819
9820 switch (die->tag)
9821 {
9822 case DW_TAG_padding:
9823 break;
9824 case DW_TAG_compile_unit:
9825 case DW_TAG_partial_unit:
9826 read_file_scope (die, cu);
9827 break;
9828 case DW_TAG_type_unit:
9829 read_type_unit_scope (die, cu);
9830 break;
9831 case DW_TAG_subprogram:
9832 /* Nested subprograms in Fortran get a prefix. */
9833 if (cu->language == language_fortran
9834 && die->parent != NULL
9835 && die->parent->tag == DW_TAG_subprogram)
9836 cu->processing_has_namespace_info = true;
9837 /* Fall through. */
9838 case DW_TAG_inlined_subroutine:
9839 read_func_scope (die, cu);
9840 break;
9841 case DW_TAG_lexical_block:
9842 case DW_TAG_try_block:
9843 case DW_TAG_catch_block:
9844 read_lexical_block_scope (die, cu);
9845 break;
9846 case DW_TAG_call_site:
9847 case DW_TAG_GNU_call_site:
9848 read_call_site_scope (die, cu);
9849 break;
9850 case DW_TAG_class_type:
9851 case DW_TAG_interface_type:
9852 case DW_TAG_structure_type:
9853 case DW_TAG_union_type:
9854 process_structure_scope (die, cu);
9855 break;
9856 case DW_TAG_enumeration_type:
9857 process_enumeration_scope (die, cu);
9858 break;
9859
9860 /* These dies have a type, but processing them does not create
9861 a symbol or recurse to process the children. Therefore we can
9862 read them on-demand through read_type_die. */
9863 case DW_TAG_subroutine_type:
9864 case DW_TAG_set_type:
9865 case DW_TAG_array_type:
9866 case DW_TAG_pointer_type:
9867 case DW_TAG_ptr_to_member_type:
9868 case DW_TAG_reference_type:
9869 case DW_TAG_rvalue_reference_type:
9870 case DW_TAG_string_type:
9871 break;
9872
9873 case DW_TAG_base_type:
9874 case DW_TAG_subrange_type:
9875 case DW_TAG_typedef:
9876 /* Add a typedef symbol for the type definition, if it has a
9877 DW_AT_name. */
9878 new_symbol (die, read_type_die (die, cu), cu);
9879 break;
9880 case DW_TAG_common_block:
9881 read_common_block (die, cu);
9882 break;
9883 case DW_TAG_common_inclusion:
9884 break;
9885 case DW_TAG_namespace:
9886 cu->processing_has_namespace_info = true;
9887 read_namespace (die, cu);
9888 break;
9889 case DW_TAG_module:
9890 cu->processing_has_namespace_info = true;
9891 read_module (die, cu);
9892 break;
9893 case DW_TAG_imported_declaration:
9894 cu->processing_has_namespace_info = true;
9895 if (read_namespace_alias (die, cu))
9896 break;
9897 /* The declaration is not a global namespace alias. */
9898 /* Fall through. */
9899 case DW_TAG_imported_module:
9900 cu->processing_has_namespace_info = true;
9901 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9902 || cu->language != language_fortran))
9903 complaint (_("Tag '%s' has unexpected children"),
9904 dwarf_tag_name (die->tag));
9905 read_import_statement (die, cu);
9906 break;
9907
9908 case DW_TAG_imported_unit:
9909 process_imported_unit_die (die, cu);
9910 break;
9911
9912 case DW_TAG_variable:
9913 read_variable (die, cu);
9914 break;
9915
9916 default:
9917 new_symbol (die, NULL, cu);
9918 break;
9919 }
9920 }
9921 \f
9922 /* DWARF name computation. */
9923
9924 /* A helper function for dwarf2_compute_name which determines whether DIE
9925 needs to have the name of the scope prepended to the name listed in the
9926 die. */
9927
9928 static int
9929 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9930 {
9931 struct attribute *attr;
9932
9933 switch (die->tag)
9934 {
9935 case DW_TAG_namespace:
9936 case DW_TAG_typedef:
9937 case DW_TAG_class_type:
9938 case DW_TAG_interface_type:
9939 case DW_TAG_structure_type:
9940 case DW_TAG_union_type:
9941 case DW_TAG_enumeration_type:
9942 case DW_TAG_enumerator:
9943 case DW_TAG_subprogram:
9944 case DW_TAG_inlined_subroutine:
9945 case DW_TAG_member:
9946 case DW_TAG_imported_declaration:
9947 return 1;
9948
9949 case DW_TAG_variable:
9950 case DW_TAG_constant:
9951 /* We only need to prefix "globally" visible variables. These include
9952 any variable marked with DW_AT_external or any variable that
9953 lives in a namespace. [Variables in anonymous namespaces
9954 require prefixing, but they are not DW_AT_external.] */
9955
9956 if (dwarf2_attr (die, DW_AT_specification, cu))
9957 {
9958 struct dwarf2_cu *spec_cu = cu;
9959
9960 return die_needs_namespace (die_specification (die, &spec_cu),
9961 spec_cu);
9962 }
9963
9964 attr = dwarf2_attr (die, DW_AT_external, cu);
9965 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9966 && die->parent->tag != DW_TAG_module)
9967 return 0;
9968 /* A variable in a lexical block of some kind does not need a
9969 namespace, even though in C++ such variables may be external
9970 and have a mangled name. */
9971 if (die->parent->tag == DW_TAG_lexical_block
9972 || die->parent->tag == DW_TAG_try_block
9973 || die->parent->tag == DW_TAG_catch_block
9974 || die->parent->tag == DW_TAG_subprogram)
9975 return 0;
9976 return 1;
9977
9978 default:
9979 return 0;
9980 }
9981 }
9982
9983 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9984 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9985 defined for the given DIE. */
9986
9987 static struct attribute *
9988 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9989 {
9990 struct attribute *attr;
9991
9992 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9993 if (attr == NULL)
9994 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9995
9996 return attr;
9997 }
9998
9999 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10000 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10001 defined for the given DIE. */
10002
10003 static const char *
10004 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10005 {
10006 const char *linkage_name;
10007
10008 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10009 if (linkage_name == NULL)
10010 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10011
10012 return linkage_name;
10013 }
10014
10015 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10016 compute the physname for the object, which include a method's:
10017 - formal parameters (C++),
10018 - receiver type (Go),
10019
10020 The term "physname" is a bit confusing.
10021 For C++, for example, it is the demangled name.
10022 For Go, for example, it's the mangled name.
10023
10024 For Ada, return the DIE's linkage name rather than the fully qualified
10025 name. PHYSNAME is ignored..
10026
10027 The result is allocated on the objfile_obstack and canonicalized. */
10028
10029 static const char *
10030 dwarf2_compute_name (const char *name,
10031 struct die_info *die, struct dwarf2_cu *cu,
10032 int physname)
10033 {
10034 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10035
10036 if (name == NULL)
10037 name = dwarf2_name (die, cu);
10038
10039 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10040 but otherwise compute it by typename_concat inside GDB.
10041 FIXME: Actually this is not really true, or at least not always true.
10042 It's all very confusing. compute_and_set_names doesn't try to demangle
10043 Fortran names because there is no mangling standard. So new_symbol
10044 will set the demangled name to the result of dwarf2_full_name, and it is
10045 the demangled name that GDB uses if it exists. */
10046 if (cu->language == language_ada
10047 || (cu->language == language_fortran && physname))
10048 {
10049 /* For Ada unit, we prefer the linkage name over the name, as
10050 the former contains the exported name, which the user expects
10051 to be able to reference. Ideally, we want the user to be able
10052 to reference this entity using either natural or linkage name,
10053 but we haven't started looking at this enhancement yet. */
10054 const char *linkage_name = dw2_linkage_name (die, cu);
10055
10056 if (linkage_name != NULL)
10057 return linkage_name;
10058 }
10059
10060 /* These are the only languages we know how to qualify names in. */
10061 if (name != NULL
10062 && (cu->language == language_cplus
10063 || cu->language == language_fortran || cu->language == language_d
10064 || cu->language == language_rust))
10065 {
10066 if (die_needs_namespace (die, cu))
10067 {
10068 const char *prefix;
10069 const char *canonical_name = NULL;
10070
10071 string_file buf;
10072
10073 prefix = determine_prefix (die, cu);
10074 if (*prefix != '\0')
10075 {
10076 gdb::unique_xmalloc_ptr<char> prefixed_name
10077 (typename_concat (NULL, prefix, name, physname, cu));
10078
10079 buf.puts (prefixed_name.get ());
10080 }
10081 else
10082 buf.puts (name);
10083
10084 /* Template parameters may be specified in the DIE's DW_AT_name, or
10085 as children with DW_TAG_template_type_param or
10086 DW_TAG_value_type_param. If the latter, add them to the name
10087 here. If the name already has template parameters, then
10088 skip this step; some versions of GCC emit both, and
10089 it is more efficient to use the pre-computed name.
10090
10091 Something to keep in mind about this process: it is very
10092 unlikely, or in some cases downright impossible, to produce
10093 something that will match the mangled name of a function.
10094 If the definition of the function has the same debug info,
10095 we should be able to match up with it anyway. But fallbacks
10096 using the minimal symbol, for instance to find a method
10097 implemented in a stripped copy of libstdc++, will not work.
10098 If we do not have debug info for the definition, we will have to
10099 match them up some other way.
10100
10101 When we do name matching there is a related problem with function
10102 templates; two instantiated function templates are allowed to
10103 differ only by their return types, which we do not add here. */
10104
10105 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10106 {
10107 struct attribute *attr;
10108 struct die_info *child;
10109 int first = 1;
10110
10111 die->building_fullname = 1;
10112
10113 for (child = die->child; child != NULL; child = child->sibling)
10114 {
10115 struct type *type;
10116 LONGEST value;
10117 const gdb_byte *bytes;
10118 struct dwarf2_locexpr_baton *baton;
10119 struct value *v;
10120
10121 if (child->tag != DW_TAG_template_type_param
10122 && child->tag != DW_TAG_template_value_param)
10123 continue;
10124
10125 if (first)
10126 {
10127 buf.puts ("<");
10128 first = 0;
10129 }
10130 else
10131 buf.puts (", ");
10132
10133 attr = dwarf2_attr (child, DW_AT_type, cu);
10134 if (attr == NULL)
10135 {
10136 complaint (_("template parameter missing DW_AT_type"));
10137 buf.puts ("UNKNOWN_TYPE");
10138 continue;
10139 }
10140 type = die_type (child, cu);
10141
10142 if (child->tag == DW_TAG_template_type_param)
10143 {
10144 c_print_type (type, "", &buf, -1, 0, cu->language,
10145 &type_print_raw_options);
10146 continue;
10147 }
10148
10149 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10150 if (attr == NULL)
10151 {
10152 complaint (_("template parameter missing "
10153 "DW_AT_const_value"));
10154 buf.puts ("UNKNOWN_VALUE");
10155 continue;
10156 }
10157
10158 dwarf2_const_value_attr (attr, type, name,
10159 &cu->comp_unit_obstack, cu,
10160 &value, &bytes, &baton);
10161
10162 if (TYPE_NOSIGN (type))
10163 /* GDB prints characters as NUMBER 'CHAR'. If that's
10164 changed, this can use value_print instead. */
10165 c_printchar (value, type, &buf);
10166 else
10167 {
10168 struct value_print_options opts;
10169
10170 if (baton != NULL)
10171 v = dwarf2_evaluate_loc_desc (type, NULL,
10172 baton->data,
10173 baton->size,
10174 baton->per_cu);
10175 else if (bytes != NULL)
10176 {
10177 v = allocate_value (type);
10178 memcpy (value_contents_writeable (v), bytes,
10179 TYPE_LENGTH (type));
10180 }
10181 else
10182 v = value_from_longest (type, value);
10183
10184 /* Specify decimal so that we do not depend on
10185 the radix. */
10186 get_formatted_print_options (&opts, 'd');
10187 opts.raw = 1;
10188 value_print (v, &buf, &opts);
10189 release_value (v);
10190 }
10191 }
10192
10193 die->building_fullname = 0;
10194
10195 if (!first)
10196 {
10197 /* Close the argument list, with a space if necessary
10198 (nested templates). */
10199 if (!buf.empty () && buf.string ().back () == '>')
10200 buf.puts (" >");
10201 else
10202 buf.puts (">");
10203 }
10204 }
10205
10206 /* For C++ methods, append formal parameter type
10207 information, if PHYSNAME. */
10208
10209 if (physname && die->tag == DW_TAG_subprogram
10210 && cu->language == language_cplus)
10211 {
10212 struct type *type = read_type_die (die, cu);
10213
10214 c_type_print_args (type, &buf, 1, cu->language,
10215 &type_print_raw_options);
10216
10217 if (cu->language == language_cplus)
10218 {
10219 /* Assume that an artificial first parameter is
10220 "this", but do not crash if it is not. RealView
10221 marks unnamed (and thus unused) parameters as
10222 artificial; there is no way to differentiate
10223 the two cases. */
10224 if (TYPE_NFIELDS (type) > 0
10225 && TYPE_FIELD_ARTIFICIAL (type, 0)
10226 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10227 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10228 0))))
10229 buf.puts (" const");
10230 }
10231 }
10232
10233 const std::string &intermediate_name = buf.string ();
10234
10235 if (cu->language == language_cplus)
10236 canonical_name
10237 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10238 objfile);
10239
10240 /* If we only computed INTERMEDIATE_NAME, or if
10241 INTERMEDIATE_NAME is already canonical, then we need to
10242 intern it. */
10243 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10244 name = objfile->intern (intermediate_name);
10245 else
10246 name = canonical_name;
10247 }
10248 }
10249
10250 return name;
10251 }
10252
10253 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10254 If scope qualifiers are appropriate they will be added. The result
10255 will be allocated on the storage_obstack, or NULL if the DIE does
10256 not have a name. NAME may either be from a previous call to
10257 dwarf2_name or NULL.
10258
10259 The output string will be canonicalized (if C++). */
10260
10261 static const char *
10262 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10263 {
10264 return dwarf2_compute_name (name, die, cu, 0);
10265 }
10266
10267 /* Construct a physname for the given DIE in CU. NAME may either be
10268 from a previous call to dwarf2_name or NULL. The result will be
10269 allocated on the objfile_objstack or NULL if the DIE does not have a
10270 name.
10271
10272 The output string will be canonicalized (if C++). */
10273
10274 static const char *
10275 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10276 {
10277 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10278 const char *retval, *mangled = NULL, *canon = NULL;
10279 int need_copy = 1;
10280
10281 /* In this case dwarf2_compute_name is just a shortcut not building anything
10282 on its own. */
10283 if (!die_needs_namespace (die, cu))
10284 return dwarf2_compute_name (name, die, cu, 1);
10285
10286 mangled = dw2_linkage_name (die, cu);
10287
10288 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10289 See https://github.com/rust-lang/rust/issues/32925. */
10290 if (cu->language == language_rust && mangled != NULL
10291 && strchr (mangled, '{') != NULL)
10292 mangled = NULL;
10293
10294 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10295 has computed. */
10296 gdb::unique_xmalloc_ptr<char> demangled;
10297 if (mangled != NULL)
10298 {
10299
10300 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10301 {
10302 /* Do nothing (do not demangle the symbol name). */
10303 }
10304 else if (cu->language == language_go)
10305 {
10306 /* This is a lie, but we already lie to the caller new_symbol.
10307 new_symbol assumes we return the mangled name.
10308 This just undoes that lie until things are cleaned up. */
10309 }
10310 else
10311 {
10312 /* Use DMGL_RET_DROP for C++ template functions to suppress
10313 their return type. It is easier for GDB users to search
10314 for such functions as `name(params)' than `long name(params)'.
10315 In such case the minimal symbol names do not match the full
10316 symbol names but for template functions there is never a need
10317 to look up their definition from their declaration so
10318 the only disadvantage remains the minimal symbol variant
10319 `long name(params)' does not have the proper inferior type. */
10320 demangled.reset (gdb_demangle (mangled,
10321 (DMGL_PARAMS | DMGL_ANSI
10322 | DMGL_RET_DROP)));
10323 }
10324 if (demangled)
10325 canon = demangled.get ();
10326 else
10327 {
10328 canon = mangled;
10329 need_copy = 0;
10330 }
10331 }
10332
10333 if (canon == NULL || check_physname)
10334 {
10335 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10336
10337 if (canon != NULL && strcmp (physname, canon) != 0)
10338 {
10339 /* It may not mean a bug in GDB. The compiler could also
10340 compute DW_AT_linkage_name incorrectly. But in such case
10341 GDB would need to be bug-to-bug compatible. */
10342
10343 complaint (_("Computed physname <%s> does not match demangled <%s> "
10344 "(from linkage <%s>) - DIE at %s [in module %s]"),
10345 physname, canon, mangled, sect_offset_str (die->sect_off),
10346 objfile_name (objfile));
10347
10348 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10349 is available here - over computed PHYSNAME. It is safer
10350 against both buggy GDB and buggy compilers. */
10351
10352 retval = canon;
10353 }
10354 else
10355 {
10356 retval = physname;
10357 need_copy = 0;
10358 }
10359 }
10360 else
10361 retval = canon;
10362
10363 if (need_copy)
10364 retval = objfile->intern (retval);
10365
10366 return retval;
10367 }
10368
10369 /* Inspect DIE in CU for a namespace alias. If one exists, record
10370 a new symbol for it.
10371
10372 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10373
10374 static int
10375 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10376 {
10377 struct attribute *attr;
10378
10379 /* If the die does not have a name, this is not a namespace
10380 alias. */
10381 attr = dwarf2_attr (die, DW_AT_name, cu);
10382 if (attr != NULL)
10383 {
10384 int num;
10385 struct die_info *d = die;
10386 struct dwarf2_cu *imported_cu = cu;
10387
10388 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10389 keep inspecting DIEs until we hit the underlying import. */
10390 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10391 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10392 {
10393 attr = dwarf2_attr (d, DW_AT_import, cu);
10394 if (attr == NULL)
10395 break;
10396
10397 d = follow_die_ref (d, attr, &imported_cu);
10398 if (d->tag != DW_TAG_imported_declaration)
10399 break;
10400 }
10401
10402 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10403 {
10404 complaint (_("DIE at %s has too many recursively imported "
10405 "declarations"), sect_offset_str (d->sect_off));
10406 return 0;
10407 }
10408
10409 if (attr != NULL)
10410 {
10411 struct type *type;
10412 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10413
10414 type = get_die_type_at_offset (sect_off, cu->per_cu);
10415 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10416 {
10417 /* This declaration is a global namespace alias. Add
10418 a symbol for it whose type is the aliased namespace. */
10419 new_symbol (die, type, cu);
10420 return 1;
10421 }
10422 }
10423 }
10424
10425 return 0;
10426 }
10427
10428 /* Return the using directives repository (global or local?) to use in the
10429 current context for CU.
10430
10431 For Ada, imported declarations can materialize renamings, which *may* be
10432 global. However it is impossible (for now?) in DWARF to distinguish
10433 "external" imported declarations and "static" ones. As all imported
10434 declarations seem to be static in all other languages, make them all CU-wide
10435 global only in Ada. */
10436
10437 static struct using_direct **
10438 using_directives (struct dwarf2_cu *cu)
10439 {
10440 if (cu->language == language_ada
10441 && cu->get_builder ()->outermost_context_p ())
10442 return cu->get_builder ()->get_global_using_directives ();
10443 else
10444 return cu->get_builder ()->get_local_using_directives ();
10445 }
10446
10447 /* Read the import statement specified by the given die and record it. */
10448
10449 static void
10450 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10451 {
10452 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10453 struct attribute *import_attr;
10454 struct die_info *imported_die, *child_die;
10455 struct dwarf2_cu *imported_cu;
10456 const char *imported_name;
10457 const char *imported_name_prefix;
10458 const char *canonical_name;
10459 const char *import_alias;
10460 const char *imported_declaration = NULL;
10461 const char *import_prefix;
10462 std::vector<const char *> excludes;
10463
10464 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10465 if (import_attr == NULL)
10466 {
10467 complaint (_("Tag '%s' has no DW_AT_import"),
10468 dwarf_tag_name (die->tag));
10469 return;
10470 }
10471
10472 imported_cu = cu;
10473 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10474 imported_name = dwarf2_name (imported_die, imported_cu);
10475 if (imported_name == NULL)
10476 {
10477 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10478
10479 The import in the following code:
10480 namespace A
10481 {
10482 typedef int B;
10483 }
10484
10485 int main ()
10486 {
10487 using A::B;
10488 B b;
10489 return b;
10490 }
10491
10492 ...
10493 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10494 <52> DW_AT_decl_file : 1
10495 <53> DW_AT_decl_line : 6
10496 <54> DW_AT_import : <0x75>
10497 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10498 <59> DW_AT_name : B
10499 <5b> DW_AT_decl_file : 1
10500 <5c> DW_AT_decl_line : 2
10501 <5d> DW_AT_type : <0x6e>
10502 ...
10503 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10504 <76> DW_AT_byte_size : 4
10505 <77> DW_AT_encoding : 5 (signed)
10506
10507 imports the wrong die ( 0x75 instead of 0x58 ).
10508 This case will be ignored until the gcc bug is fixed. */
10509 return;
10510 }
10511
10512 /* Figure out the local name after import. */
10513 import_alias = dwarf2_name (die, cu);
10514
10515 /* Figure out where the statement is being imported to. */
10516 import_prefix = determine_prefix (die, cu);
10517
10518 /* Figure out what the scope of the imported die is and prepend it
10519 to the name of the imported die. */
10520 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10521
10522 if (imported_die->tag != DW_TAG_namespace
10523 && imported_die->tag != DW_TAG_module)
10524 {
10525 imported_declaration = imported_name;
10526 canonical_name = imported_name_prefix;
10527 }
10528 else if (strlen (imported_name_prefix) > 0)
10529 canonical_name = obconcat (&objfile->objfile_obstack,
10530 imported_name_prefix,
10531 (cu->language == language_d ? "." : "::"),
10532 imported_name, (char *) NULL);
10533 else
10534 canonical_name = imported_name;
10535
10536 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10537 for (child_die = die->child; child_die && child_die->tag;
10538 child_die = sibling_die (child_die))
10539 {
10540 /* DWARF-4: A Fortran use statement with a “rename list” may be
10541 represented by an imported module entry with an import attribute
10542 referring to the module and owned entries corresponding to those
10543 entities that are renamed as part of being imported. */
10544
10545 if (child_die->tag != DW_TAG_imported_declaration)
10546 {
10547 complaint (_("child DW_TAG_imported_declaration expected "
10548 "- DIE at %s [in module %s]"),
10549 sect_offset_str (child_die->sect_off),
10550 objfile_name (objfile));
10551 continue;
10552 }
10553
10554 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10555 if (import_attr == NULL)
10556 {
10557 complaint (_("Tag '%s' has no DW_AT_import"),
10558 dwarf_tag_name (child_die->tag));
10559 continue;
10560 }
10561
10562 imported_cu = cu;
10563 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10564 &imported_cu);
10565 imported_name = dwarf2_name (imported_die, imported_cu);
10566 if (imported_name == NULL)
10567 {
10568 complaint (_("child DW_TAG_imported_declaration has unknown "
10569 "imported name - DIE at %s [in module %s]"),
10570 sect_offset_str (child_die->sect_off),
10571 objfile_name (objfile));
10572 continue;
10573 }
10574
10575 excludes.push_back (imported_name);
10576
10577 process_die (child_die, cu);
10578 }
10579
10580 add_using_directive (using_directives (cu),
10581 import_prefix,
10582 canonical_name,
10583 import_alias,
10584 imported_declaration,
10585 excludes,
10586 0,
10587 &objfile->objfile_obstack);
10588 }
10589
10590 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10591 types, but gives them a size of zero. Starting with version 14,
10592 ICC is compatible with GCC. */
10593
10594 static bool
10595 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10596 {
10597 if (!cu->checked_producer)
10598 check_producer (cu);
10599
10600 return cu->producer_is_icc_lt_14;
10601 }
10602
10603 /* ICC generates a DW_AT_type for C void functions. This was observed on
10604 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10605 which says that void functions should not have a DW_AT_type. */
10606
10607 static bool
10608 producer_is_icc (struct dwarf2_cu *cu)
10609 {
10610 if (!cu->checked_producer)
10611 check_producer (cu);
10612
10613 return cu->producer_is_icc;
10614 }
10615
10616 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10617 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10618 this, it was first present in GCC release 4.3.0. */
10619
10620 static bool
10621 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10622 {
10623 if (!cu->checked_producer)
10624 check_producer (cu);
10625
10626 return cu->producer_is_gcc_lt_4_3;
10627 }
10628
10629 static file_and_directory
10630 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10631 {
10632 file_and_directory res;
10633
10634 /* Find the filename. Do not use dwarf2_name here, since the filename
10635 is not a source language identifier. */
10636 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10637 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10638
10639 if (res.comp_dir == NULL
10640 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10641 && IS_ABSOLUTE_PATH (res.name))
10642 {
10643 res.comp_dir_storage = ldirname (res.name);
10644 if (!res.comp_dir_storage.empty ())
10645 res.comp_dir = res.comp_dir_storage.c_str ();
10646 }
10647 if (res.comp_dir != NULL)
10648 {
10649 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10650 directory, get rid of it. */
10651 const char *cp = strchr (res.comp_dir, ':');
10652
10653 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10654 res.comp_dir = cp + 1;
10655 }
10656
10657 if (res.name == NULL)
10658 res.name = "<unknown>";
10659
10660 return res;
10661 }
10662
10663 /* Handle DW_AT_stmt_list for a compilation unit.
10664 DIE is the DW_TAG_compile_unit die for CU.
10665 COMP_DIR is the compilation directory. LOWPC is passed to
10666 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10667
10668 static void
10669 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10670 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10671 {
10672 struct dwarf2_per_objfile *dwarf2_per_objfile
10673 = cu->per_cu->dwarf2_per_objfile;
10674 struct attribute *attr;
10675 struct line_header line_header_local;
10676 hashval_t line_header_local_hash;
10677 void **slot;
10678 int decode_mapping;
10679
10680 gdb_assert (! cu->per_cu->is_debug_types);
10681
10682 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10683 if (attr == NULL)
10684 return;
10685
10686 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10687
10688 /* The line header hash table is only created if needed (it exists to
10689 prevent redundant reading of the line table for partial_units).
10690 If we're given a partial_unit, we'll need it. If we're given a
10691 compile_unit, then use the line header hash table if it's already
10692 created, but don't create one just yet. */
10693
10694 if (dwarf2_per_objfile->line_header_hash == NULL
10695 && die->tag == DW_TAG_partial_unit)
10696 {
10697 dwarf2_per_objfile->line_header_hash
10698 .reset (htab_create_alloc (127, line_header_hash_voidp,
10699 line_header_eq_voidp,
10700 free_line_header_voidp,
10701 xcalloc, xfree));
10702 }
10703
10704 line_header_local.sect_off = line_offset;
10705 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10706 line_header_local_hash = line_header_hash (&line_header_local);
10707 if (dwarf2_per_objfile->line_header_hash != NULL)
10708 {
10709 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10710 &line_header_local,
10711 line_header_local_hash, NO_INSERT);
10712
10713 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10714 is not present in *SLOT (since if there is something in *SLOT then
10715 it will be for a partial_unit). */
10716 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10717 {
10718 gdb_assert (*slot != NULL);
10719 cu->line_header = (struct line_header *) *slot;
10720 return;
10721 }
10722 }
10723
10724 /* dwarf_decode_line_header does not yet provide sufficient information.
10725 We always have to call also dwarf_decode_lines for it. */
10726 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10727 if (lh == NULL)
10728 return;
10729
10730 cu->line_header = lh.release ();
10731 cu->line_header_die_owner = die;
10732
10733 if (dwarf2_per_objfile->line_header_hash == NULL)
10734 slot = NULL;
10735 else
10736 {
10737 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10738 &line_header_local,
10739 line_header_local_hash, INSERT);
10740 gdb_assert (slot != NULL);
10741 }
10742 if (slot != NULL && *slot == NULL)
10743 {
10744 /* This newly decoded line number information unit will be owned
10745 by line_header_hash hash table. */
10746 *slot = cu->line_header;
10747 cu->line_header_die_owner = NULL;
10748 }
10749 else
10750 {
10751 /* We cannot free any current entry in (*slot) as that struct line_header
10752 may be already used by multiple CUs. Create only temporary decoded
10753 line_header for this CU - it may happen at most once for each line
10754 number information unit. And if we're not using line_header_hash
10755 then this is what we want as well. */
10756 gdb_assert (die->tag != DW_TAG_partial_unit);
10757 }
10758 decode_mapping = (die->tag != DW_TAG_partial_unit);
10759 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10760 decode_mapping);
10761
10762 }
10763
10764 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10765
10766 static void
10767 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10768 {
10769 struct dwarf2_per_objfile *dwarf2_per_objfile
10770 = cu->per_cu->dwarf2_per_objfile;
10771 struct objfile *objfile = dwarf2_per_objfile->objfile;
10772 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10773 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10774 CORE_ADDR highpc = ((CORE_ADDR) 0);
10775 struct attribute *attr;
10776 struct die_info *child_die;
10777 CORE_ADDR baseaddr;
10778
10779 prepare_one_comp_unit (cu, die, cu->language);
10780 baseaddr = objfile->text_section_offset ();
10781
10782 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10783
10784 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10785 from finish_block. */
10786 if (lowpc == ((CORE_ADDR) -1))
10787 lowpc = highpc;
10788 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10789
10790 file_and_directory fnd = find_file_and_directory (die, cu);
10791
10792 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10793 standardised yet. As a workaround for the language detection we fall
10794 back to the DW_AT_producer string. */
10795 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10796 cu->language = language_opencl;
10797
10798 /* Similar hack for Go. */
10799 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10800 set_cu_language (DW_LANG_Go, cu);
10801
10802 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10803
10804 /* Decode line number information if present. We do this before
10805 processing child DIEs, so that the line header table is available
10806 for DW_AT_decl_file. */
10807 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10808
10809 /* Process all dies in compilation unit. */
10810 if (die->child != NULL)
10811 {
10812 child_die = die->child;
10813 while (child_die && child_die->tag)
10814 {
10815 process_die (child_die, cu);
10816 child_die = sibling_die (child_die);
10817 }
10818 }
10819
10820 /* Decode macro information, if present. Dwarf 2 macro information
10821 refers to information in the line number info statement program
10822 header, so we can only read it if we've read the header
10823 successfully. */
10824 attr = dwarf2_attr (die, DW_AT_macros, cu);
10825 if (attr == NULL)
10826 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10827 if (attr && cu->line_header)
10828 {
10829 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10830 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10831
10832 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10833 }
10834 else
10835 {
10836 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10837 if (attr && cu->line_header)
10838 {
10839 unsigned int macro_offset = DW_UNSND (attr);
10840
10841 dwarf_decode_macros (cu, macro_offset, 0);
10842 }
10843 }
10844 }
10845
10846 void
10847 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10848 {
10849 struct type_unit_group *tu_group;
10850 int first_time;
10851 struct attribute *attr;
10852 unsigned int i;
10853 struct signatured_type *sig_type;
10854
10855 gdb_assert (per_cu->is_debug_types);
10856 sig_type = (struct signatured_type *) per_cu;
10857
10858 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10859
10860 /* If we're using .gdb_index (includes -readnow) then
10861 per_cu->type_unit_group may not have been set up yet. */
10862 if (sig_type->type_unit_group == NULL)
10863 sig_type->type_unit_group = get_type_unit_group (this, attr);
10864 tu_group = sig_type->type_unit_group;
10865
10866 /* If we've already processed this stmt_list there's no real need to
10867 do it again, we could fake it and just recreate the part we need
10868 (file name,index -> symtab mapping). If data shows this optimization
10869 is useful we can do it then. */
10870 first_time = tu_group->compunit_symtab == NULL;
10871
10872 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10873 debug info. */
10874 line_header_up lh;
10875 if (attr != NULL)
10876 {
10877 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10878 lh = dwarf_decode_line_header (line_offset, this);
10879 }
10880 if (lh == NULL)
10881 {
10882 if (first_time)
10883 start_symtab ("", NULL, 0);
10884 else
10885 {
10886 gdb_assert (tu_group->symtabs == NULL);
10887 gdb_assert (m_builder == nullptr);
10888 struct compunit_symtab *cust = tu_group->compunit_symtab;
10889 m_builder.reset (new struct buildsym_compunit
10890 (COMPUNIT_OBJFILE (cust), "",
10891 COMPUNIT_DIRNAME (cust),
10892 compunit_language (cust),
10893 0, cust));
10894 }
10895 return;
10896 }
10897
10898 line_header = lh.release ();
10899 line_header_die_owner = die;
10900
10901 if (first_time)
10902 {
10903 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10904
10905 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10906 still initializing it, and our caller (a few levels up)
10907 process_full_type_unit still needs to know if this is the first
10908 time. */
10909
10910 tu_group->symtabs
10911 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10912 struct symtab *, line_header->file_names_size ());
10913
10914 auto &file_names = line_header->file_names ();
10915 for (i = 0; i < file_names.size (); ++i)
10916 {
10917 file_entry &fe = file_names[i];
10918 dwarf2_start_subfile (this, fe.name,
10919 fe.include_dir (line_header));
10920 buildsym_compunit *b = get_builder ();
10921 if (b->get_current_subfile ()->symtab == NULL)
10922 {
10923 /* NOTE: start_subfile will recognize when it's been
10924 passed a file it has already seen. So we can't
10925 assume there's a simple mapping from
10926 cu->line_header->file_names to subfiles, plus
10927 cu->line_header->file_names may contain dups. */
10928 b->get_current_subfile ()->symtab
10929 = allocate_symtab (cust, b->get_current_subfile ()->name);
10930 }
10931
10932 fe.symtab = b->get_current_subfile ()->symtab;
10933 tu_group->symtabs[i] = fe.symtab;
10934 }
10935 }
10936 else
10937 {
10938 gdb_assert (m_builder == nullptr);
10939 struct compunit_symtab *cust = tu_group->compunit_symtab;
10940 m_builder.reset (new struct buildsym_compunit
10941 (COMPUNIT_OBJFILE (cust), "",
10942 COMPUNIT_DIRNAME (cust),
10943 compunit_language (cust),
10944 0, cust));
10945
10946 auto &file_names = line_header->file_names ();
10947 for (i = 0; i < file_names.size (); ++i)
10948 {
10949 file_entry &fe = file_names[i];
10950 fe.symtab = tu_group->symtabs[i];
10951 }
10952 }
10953
10954 /* The main symtab is allocated last. Type units don't have DW_AT_name
10955 so they don't have a "real" (so to speak) symtab anyway.
10956 There is later code that will assign the main symtab to all symbols
10957 that don't have one. We need to handle the case of a symbol with a
10958 missing symtab (DW_AT_decl_file) anyway. */
10959 }
10960
10961 /* Process DW_TAG_type_unit.
10962 For TUs we want to skip the first top level sibling if it's not the
10963 actual type being defined by this TU. In this case the first top
10964 level sibling is there to provide context only. */
10965
10966 static void
10967 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10968 {
10969 struct die_info *child_die;
10970
10971 prepare_one_comp_unit (cu, die, language_minimal);
10972
10973 /* Initialize (or reinitialize) the machinery for building symtabs.
10974 We do this before processing child DIEs, so that the line header table
10975 is available for DW_AT_decl_file. */
10976 cu->setup_type_unit_groups (die);
10977
10978 if (die->child != NULL)
10979 {
10980 child_die = die->child;
10981 while (child_die && child_die->tag)
10982 {
10983 process_die (child_die, cu);
10984 child_die = sibling_die (child_die);
10985 }
10986 }
10987 }
10988 \f
10989 /* DWO/DWP files.
10990
10991 http://gcc.gnu.org/wiki/DebugFission
10992 http://gcc.gnu.org/wiki/DebugFissionDWP
10993
10994 To simplify handling of both DWO files ("object" files with the DWARF info)
10995 and DWP files (a file with the DWOs packaged up into one file), we treat
10996 DWP files as having a collection of virtual DWO files. */
10997
10998 static hashval_t
10999 hash_dwo_file (const void *item)
11000 {
11001 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11002 hashval_t hash;
11003
11004 hash = htab_hash_string (dwo_file->dwo_name);
11005 if (dwo_file->comp_dir != NULL)
11006 hash += htab_hash_string (dwo_file->comp_dir);
11007 return hash;
11008 }
11009
11010 static int
11011 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11012 {
11013 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11014 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11015
11016 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11017 return 0;
11018 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11019 return lhs->comp_dir == rhs->comp_dir;
11020 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11021 }
11022
11023 /* Allocate a hash table for DWO files. */
11024
11025 static htab_up
11026 allocate_dwo_file_hash_table ()
11027 {
11028 auto delete_dwo_file = [] (void *item)
11029 {
11030 struct dwo_file *dwo_file = (struct dwo_file *) item;
11031
11032 delete dwo_file;
11033 };
11034
11035 return htab_up (htab_create_alloc (41,
11036 hash_dwo_file,
11037 eq_dwo_file,
11038 delete_dwo_file,
11039 xcalloc, xfree));
11040 }
11041
11042 /* Lookup DWO file DWO_NAME. */
11043
11044 static void **
11045 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11046 const char *dwo_name,
11047 const char *comp_dir)
11048 {
11049 struct dwo_file find_entry;
11050 void **slot;
11051
11052 if (dwarf2_per_objfile->dwo_files == NULL)
11053 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11054
11055 find_entry.dwo_name = dwo_name;
11056 find_entry.comp_dir = comp_dir;
11057 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11058 INSERT);
11059
11060 return slot;
11061 }
11062
11063 static hashval_t
11064 hash_dwo_unit (const void *item)
11065 {
11066 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11067
11068 /* This drops the top 32 bits of the id, but is ok for a hash. */
11069 return dwo_unit->signature;
11070 }
11071
11072 static int
11073 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11074 {
11075 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11076 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11077
11078 /* The signature is assumed to be unique within the DWO file.
11079 So while object file CU dwo_id's always have the value zero,
11080 that's OK, assuming each object file DWO file has only one CU,
11081 and that's the rule for now. */
11082 return lhs->signature == rhs->signature;
11083 }
11084
11085 /* Allocate a hash table for DWO CUs,TUs.
11086 There is one of these tables for each of CUs,TUs for each DWO file. */
11087
11088 static htab_up
11089 allocate_dwo_unit_table ()
11090 {
11091 /* Start out with a pretty small number.
11092 Generally DWO files contain only one CU and maybe some TUs. */
11093 return htab_up (htab_create_alloc (3,
11094 hash_dwo_unit,
11095 eq_dwo_unit,
11096 NULL, xcalloc, xfree));
11097 }
11098
11099 /* die_reader_func for create_dwo_cu. */
11100
11101 static void
11102 create_dwo_cu_reader (const struct die_reader_specs *reader,
11103 const gdb_byte *info_ptr,
11104 struct die_info *comp_unit_die,
11105 struct dwo_file *dwo_file,
11106 struct dwo_unit *dwo_unit)
11107 {
11108 struct dwarf2_cu *cu = reader->cu;
11109 sect_offset sect_off = cu->per_cu->sect_off;
11110 struct dwarf2_section_info *section = cu->per_cu->section;
11111
11112 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11113 if (!signature.has_value ())
11114 {
11115 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11116 " its dwo_id [in module %s]"),
11117 sect_offset_str (sect_off), dwo_file->dwo_name);
11118 return;
11119 }
11120
11121 dwo_unit->dwo_file = dwo_file;
11122 dwo_unit->signature = *signature;
11123 dwo_unit->section = section;
11124 dwo_unit->sect_off = sect_off;
11125 dwo_unit->length = cu->per_cu->length;
11126
11127 if (dwarf_read_debug)
11128 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11129 sect_offset_str (sect_off),
11130 hex_string (dwo_unit->signature));
11131 }
11132
11133 /* Create the dwo_units for the CUs in a DWO_FILE.
11134 Note: This function processes DWO files only, not DWP files. */
11135
11136 static void
11137 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11138 dwarf2_cu *cu, struct dwo_file &dwo_file,
11139 dwarf2_section_info &section, htab_up &cus_htab)
11140 {
11141 struct objfile *objfile = dwarf2_per_objfile->objfile;
11142 const gdb_byte *info_ptr, *end_ptr;
11143
11144 section.read (objfile);
11145 info_ptr = section.buffer;
11146
11147 if (info_ptr == NULL)
11148 return;
11149
11150 if (dwarf_read_debug)
11151 {
11152 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11153 section.get_name (),
11154 section.get_file_name ());
11155 }
11156
11157 end_ptr = info_ptr + section.size;
11158 while (info_ptr < end_ptr)
11159 {
11160 struct dwarf2_per_cu_data per_cu;
11161 struct dwo_unit read_unit {};
11162 struct dwo_unit *dwo_unit;
11163 void **slot;
11164 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11165
11166 memset (&per_cu, 0, sizeof (per_cu));
11167 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11168 per_cu.is_debug_types = 0;
11169 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11170 per_cu.section = &section;
11171
11172 cutu_reader reader (&per_cu, cu, &dwo_file);
11173 if (!reader.dummy_p)
11174 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11175 &dwo_file, &read_unit);
11176 info_ptr += per_cu.length;
11177
11178 // If the unit could not be parsed, skip it.
11179 if (read_unit.dwo_file == NULL)
11180 continue;
11181
11182 if (cus_htab == NULL)
11183 cus_htab = allocate_dwo_unit_table ();
11184
11185 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11186 *dwo_unit = read_unit;
11187 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11188 gdb_assert (slot != NULL);
11189 if (*slot != NULL)
11190 {
11191 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11192 sect_offset dup_sect_off = dup_cu->sect_off;
11193
11194 complaint (_("debug cu entry at offset %s is duplicate to"
11195 " the entry at offset %s, signature %s"),
11196 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11197 hex_string (dwo_unit->signature));
11198 }
11199 *slot = (void *)dwo_unit;
11200 }
11201 }
11202
11203 /* DWP file .debug_{cu,tu}_index section format:
11204 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11205
11206 DWP Version 1:
11207
11208 Both index sections have the same format, and serve to map a 64-bit
11209 signature to a set of section numbers. Each section begins with a header,
11210 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11211 indexes, and a pool of 32-bit section numbers. The index sections will be
11212 aligned at 8-byte boundaries in the file.
11213
11214 The index section header consists of:
11215
11216 V, 32 bit version number
11217 -, 32 bits unused
11218 N, 32 bit number of compilation units or type units in the index
11219 M, 32 bit number of slots in the hash table
11220
11221 Numbers are recorded using the byte order of the application binary.
11222
11223 The hash table begins at offset 16 in the section, and consists of an array
11224 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11225 order of the application binary). Unused slots in the hash table are 0.
11226 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11227
11228 The parallel table begins immediately after the hash table
11229 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11230 array of 32-bit indexes (using the byte order of the application binary),
11231 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11232 table contains a 32-bit index into the pool of section numbers. For unused
11233 hash table slots, the corresponding entry in the parallel table will be 0.
11234
11235 The pool of section numbers begins immediately following the hash table
11236 (at offset 16 + 12 * M from the beginning of the section). The pool of
11237 section numbers consists of an array of 32-bit words (using the byte order
11238 of the application binary). Each item in the array is indexed starting
11239 from 0. The hash table entry provides the index of the first section
11240 number in the set. Additional section numbers in the set follow, and the
11241 set is terminated by a 0 entry (section number 0 is not used in ELF).
11242
11243 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11244 section must be the first entry in the set, and the .debug_abbrev.dwo must
11245 be the second entry. Other members of the set may follow in any order.
11246
11247 ---
11248
11249 DWP Version 2:
11250
11251 DWP Version 2 combines all the .debug_info, etc. sections into one,
11252 and the entries in the index tables are now offsets into these sections.
11253 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11254 section.
11255
11256 Index Section Contents:
11257 Header
11258 Hash Table of Signatures dwp_hash_table.hash_table
11259 Parallel Table of Indices dwp_hash_table.unit_table
11260 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11261 Table of Section Sizes dwp_hash_table.v2.sizes
11262
11263 The index section header consists of:
11264
11265 V, 32 bit version number
11266 L, 32 bit number of columns in the table of section offsets
11267 N, 32 bit number of compilation units or type units in the index
11268 M, 32 bit number of slots in the hash table
11269
11270 Numbers are recorded using the byte order of the application binary.
11271
11272 The hash table has the same format as version 1.
11273 The parallel table of indices has the same format as version 1,
11274 except that the entries are origin-1 indices into the table of sections
11275 offsets and the table of section sizes.
11276
11277 The table of offsets begins immediately following the parallel table
11278 (at offset 16 + 12 * M from the beginning of the section). The table is
11279 a two-dimensional array of 32-bit words (using the byte order of the
11280 application binary), with L columns and N+1 rows, in row-major order.
11281 Each row in the array is indexed starting from 0. The first row provides
11282 a key to the remaining rows: each column in this row provides an identifier
11283 for a debug section, and the offsets in the same column of subsequent rows
11284 refer to that section. The section identifiers are:
11285
11286 DW_SECT_INFO 1 .debug_info.dwo
11287 DW_SECT_TYPES 2 .debug_types.dwo
11288 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11289 DW_SECT_LINE 4 .debug_line.dwo
11290 DW_SECT_LOC 5 .debug_loc.dwo
11291 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11292 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11293 DW_SECT_MACRO 8 .debug_macro.dwo
11294
11295 The offsets provided by the CU and TU index sections are the base offsets
11296 for the contributions made by each CU or TU to the corresponding section
11297 in the package file. Each CU and TU header contains an abbrev_offset
11298 field, used to find the abbreviations table for that CU or TU within the
11299 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11300 be interpreted as relative to the base offset given in the index section.
11301 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11302 should be interpreted as relative to the base offset for .debug_line.dwo,
11303 and offsets into other debug sections obtained from DWARF attributes should
11304 also be interpreted as relative to the corresponding base offset.
11305
11306 The table of sizes begins immediately following the table of offsets.
11307 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11308 with L columns and N rows, in row-major order. Each row in the array is
11309 indexed starting from 1 (row 0 is shared by the two tables).
11310
11311 ---
11312
11313 Hash table lookup is handled the same in version 1 and 2:
11314
11315 We assume that N and M will not exceed 2^32 - 1.
11316 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11317
11318 Given a 64-bit compilation unit signature or a type signature S, an entry
11319 in the hash table is located as follows:
11320
11321 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11322 the low-order k bits all set to 1.
11323
11324 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11325
11326 3) If the hash table entry at index H matches the signature, use that
11327 entry. If the hash table entry at index H is unused (all zeroes),
11328 terminate the search: the signature is not present in the table.
11329
11330 4) Let H = (H + H') modulo M. Repeat at Step 3.
11331
11332 Because M > N and H' and M are relatively prime, the search is guaranteed
11333 to stop at an unused slot or find the match. */
11334
11335 /* Create a hash table to map DWO IDs to their CU/TU entry in
11336 .debug_{info,types}.dwo in DWP_FILE.
11337 Returns NULL if there isn't one.
11338 Note: This function processes DWP files only, not DWO files. */
11339
11340 static struct dwp_hash_table *
11341 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11342 struct dwp_file *dwp_file, int is_debug_types)
11343 {
11344 struct objfile *objfile = dwarf2_per_objfile->objfile;
11345 bfd *dbfd = dwp_file->dbfd.get ();
11346 const gdb_byte *index_ptr, *index_end;
11347 struct dwarf2_section_info *index;
11348 uint32_t version, nr_columns, nr_units, nr_slots;
11349 struct dwp_hash_table *htab;
11350
11351 if (is_debug_types)
11352 index = &dwp_file->sections.tu_index;
11353 else
11354 index = &dwp_file->sections.cu_index;
11355
11356 if (index->empty ())
11357 return NULL;
11358 index->read (objfile);
11359
11360 index_ptr = index->buffer;
11361 index_end = index_ptr + index->size;
11362
11363 version = read_4_bytes (dbfd, index_ptr);
11364 index_ptr += 4;
11365 if (version == 2)
11366 nr_columns = read_4_bytes (dbfd, index_ptr);
11367 else
11368 nr_columns = 0;
11369 index_ptr += 4;
11370 nr_units = read_4_bytes (dbfd, index_ptr);
11371 index_ptr += 4;
11372 nr_slots = read_4_bytes (dbfd, index_ptr);
11373 index_ptr += 4;
11374
11375 if (version != 1 && version != 2)
11376 {
11377 error (_("Dwarf Error: unsupported DWP file version (%s)"
11378 " [in module %s]"),
11379 pulongest (version), dwp_file->name);
11380 }
11381 if (nr_slots != (nr_slots & -nr_slots))
11382 {
11383 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11384 " is not power of 2 [in module %s]"),
11385 pulongest (nr_slots), dwp_file->name);
11386 }
11387
11388 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11389 htab->version = version;
11390 htab->nr_columns = nr_columns;
11391 htab->nr_units = nr_units;
11392 htab->nr_slots = nr_slots;
11393 htab->hash_table = index_ptr;
11394 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11395
11396 /* Exit early if the table is empty. */
11397 if (nr_slots == 0 || nr_units == 0
11398 || (version == 2 && nr_columns == 0))
11399 {
11400 /* All must be zero. */
11401 if (nr_slots != 0 || nr_units != 0
11402 || (version == 2 && nr_columns != 0))
11403 {
11404 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11405 " all zero [in modules %s]"),
11406 dwp_file->name);
11407 }
11408 return htab;
11409 }
11410
11411 if (version == 1)
11412 {
11413 htab->section_pool.v1.indices =
11414 htab->unit_table + sizeof (uint32_t) * nr_slots;
11415 /* It's harder to decide whether the section is too small in v1.
11416 V1 is deprecated anyway so we punt. */
11417 }
11418 else
11419 {
11420 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11421 int *ids = htab->section_pool.v2.section_ids;
11422 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11423 /* Reverse map for error checking. */
11424 int ids_seen[DW_SECT_MAX + 1];
11425 int i;
11426
11427 if (nr_columns < 2)
11428 {
11429 error (_("Dwarf Error: bad DWP hash table, too few columns"
11430 " in section table [in module %s]"),
11431 dwp_file->name);
11432 }
11433 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11434 {
11435 error (_("Dwarf Error: bad DWP hash table, too many columns"
11436 " in section table [in module %s]"),
11437 dwp_file->name);
11438 }
11439 memset (ids, 255, sizeof_ids);
11440 memset (ids_seen, 255, sizeof (ids_seen));
11441 for (i = 0; i < nr_columns; ++i)
11442 {
11443 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11444
11445 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11446 {
11447 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11448 " in section table [in module %s]"),
11449 id, dwp_file->name);
11450 }
11451 if (ids_seen[id] != -1)
11452 {
11453 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11454 " id %d in section table [in module %s]"),
11455 id, dwp_file->name);
11456 }
11457 ids_seen[id] = i;
11458 ids[i] = id;
11459 }
11460 /* Must have exactly one info or types section. */
11461 if (((ids_seen[DW_SECT_INFO] != -1)
11462 + (ids_seen[DW_SECT_TYPES] != -1))
11463 != 1)
11464 {
11465 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11466 " DWO info/types section [in module %s]"),
11467 dwp_file->name);
11468 }
11469 /* Must have an abbrev section. */
11470 if (ids_seen[DW_SECT_ABBREV] == -1)
11471 {
11472 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11473 " section [in module %s]"),
11474 dwp_file->name);
11475 }
11476 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11477 htab->section_pool.v2.sizes =
11478 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11479 * nr_units * nr_columns);
11480 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11481 * nr_units * nr_columns))
11482 > index_end)
11483 {
11484 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11485 " [in module %s]"),
11486 dwp_file->name);
11487 }
11488 }
11489
11490 return htab;
11491 }
11492
11493 /* Update SECTIONS with the data from SECTP.
11494
11495 This function is like the other "locate" section routines that are
11496 passed to bfd_map_over_sections, but in this context the sections to
11497 read comes from the DWP V1 hash table, not the full ELF section table.
11498
11499 The result is non-zero for success, or zero if an error was found. */
11500
11501 static int
11502 locate_v1_virtual_dwo_sections (asection *sectp,
11503 struct virtual_v1_dwo_sections *sections)
11504 {
11505 const struct dwop_section_names *names = &dwop_section_names;
11506
11507 if (section_is_p (sectp->name, &names->abbrev_dwo))
11508 {
11509 /* There can be only one. */
11510 if (sections->abbrev.s.section != NULL)
11511 return 0;
11512 sections->abbrev.s.section = sectp;
11513 sections->abbrev.size = bfd_section_size (sectp);
11514 }
11515 else if (section_is_p (sectp->name, &names->info_dwo)
11516 || section_is_p (sectp->name, &names->types_dwo))
11517 {
11518 /* There can be only one. */
11519 if (sections->info_or_types.s.section != NULL)
11520 return 0;
11521 sections->info_or_types.s.section = sectp;
11522 sections->info_or_types.size = bfd_section_size (sectp);
11523 }
11524 else if (section_is_p (sectp->name, &names->line_dwo))
11525 {
11526 /* There can be only one. */
11527 if (sections->line.s.section != NULL)
11528 return 0;
11529 sections->line.s.section = sectp;
11530 sections->line.size = bfd_section_size (sectp);
11531 }
11532 else if (section_is_p (sectp->name, &names->loc_dwo))
11533 {
11534 /* There can be only one. */
11535 if (sections->loc.s.section != NULL)
11536 return 0;
11537 sections->loc.s.section = sectp;
11538 sections->loc.size = bfd_section_size (sectp);
11539 }
11540 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11541 {
11542 /* There can be only one. */
11543 if (sections->macinfo.s.section != NULL)
11544 return 0;
11545 sections->macinfo.s.section = sectp;
11546 sections->macinfo.size = bfd_section_size (sectp);
11547 }
11548 else if (section_is_p (sectp->name, &names->macro_dwo))
11549 {
11550 /* There can be only one. */
11551 if (sections->macro.s.section != NULL)
11552 return 0;
11553 sections->macro.s.section = sectp;
11554 sections->macro.size = bfd_section_size (sectp);
11555 }
11556 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11557 {
11558 /* There can be only one. */
11559 if (sections->str_offsets.s.section != NULL)
11560 return 0;
11561 sections->str_offsets.s.section = sectp;
11562 sections->str_offsets.size = bfd_section_size (sectp);
11563 }
11564 else
11565 {
11566 /* No other kind of section is valid. */
11567 return 0;
11568 }
11569
11570 return 1;
11571 }
11572
11573 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11574 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11575 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11576 This is for DWP version 1 files. */
11577
11578 static struct dwo_unit *
11579 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11580 struct dwp_file *dwp_file,
11581 uint32_t unit_index,
11582 const char *comp_dir,
11583 ULONGEST signature, int is_debug_types)
11584 {
11585 struct objfile *objfile = dwarf2_per_objfile->objfile;
11586 const struct dwp_hash_table *dwp_htab =
11587 is_debug_types ? dwp_file->tus : dwp_file->cus;
11588 bfd *dbfd = dwp_file->dbfd.get ();
11589 const char *kind = is_debug_types ? "TU" : "CU";
11590 struct dwo_file *dwo_file;
11591 struct dwo_unit *dwo_unit;
11592 struct virtual_v1_dwo_sections sections;
11593 void **dwo_file_slot;
11594 int i;
11595
11596 gdb_assert (dwp_file->version == 1);
11597
11598 if (dwarf_read_debug)
11599 {
11600 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11601 kind,
11602 pulongest (unit_index), hex_string (signature),
11603 dwp_file->name);
11604 }
11605
11606 /* Fetch the sections of this DWO unit.
11607 Put a limit on the number of sections we look for so that bad data
11608 doesn't cause us to loop forever. */
11609
11610 #define MAX_NR_V1_DWO_SECTIONS \
11611 (1 /* .debug_info or .debug_types */ \
11612 + 1 /* .debug_abbrev */ \
11613 + 1 /* .debug_line */ \
11614 + 1 /* .debug_loc */ \
11615 + 1 /* .debug_str_offsets */ \
11616 + 1 /* .debug_macro or .debug_macinfo */ \
11617 + 1 /* trailing zero */)
11618
11619 memset (&sections, 0, sizeof (sections));
11620
11621 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11622 {
11623 asection *sectp;
11624 uint32_t section_nr =
11625 read_4_bytes (dbfd,
11626 dwp_htab->section_pool.v1.indices
11627 + (unit_index + i) * sizeof (uint32_t));
11628
11629 if (section_nr == 0)
11630 break;
11631 if (section_nr >= dwp_file->num_sections)
11632 {
11633 error (_("Dwarf Error: bad DWP hash table, section number too large"
11634 " [in module %s]"),
11635 dwp_file->name);
11636 }
11637
11638 sectp = dwp_file->elf_sections[section_nr];
11639 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11640 {
11641 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11642 " [in module %s]"),
11643 dwp_file->name);
11644 }
11645 }
11646
11647 if (i < 2
11648 || sections.info_or_types.empty ()
11649 || sections.abbrev.empty ())
11650 {
11651 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11652 " [in module %s]"),
11653 dwp_file->name);
11654 }
11655 if (i == MAX_NR_V1_DWO_SECTIONS)
11656 {
11657 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11658 " [in module %s]"),
11659 dwp_file->name);
11660 }
11661
11662 /* It's easier for the rest of the code if we fake a struct dwo_file and
11663 have dwo_unit "live" in that. At least for now.
11664
11665 The DWP file can be made up of a random collection of CUs and TUs.
11666 However, for each CU + set of TUs that came from the same original DWO
11667 file, we can combine them back into a virtual DWO file to save space
11668 (fewer struct dwo_file objects to allocate). Remember that for really
11669 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11670
11671 std::string virtual_dwo_name =
11672 string_printf ("virtual-dwo/%d-%d-%d-%d",
11673 sections.abbrev.get_id (),
11674 sections.line.get_id (),
11675 sections.loc.get_id (),
11676 sections.str_offsets.get_id ());
11677 /* Can we use an existing virtual DWO file? */
11678 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11679 virtual_dwo_name.c_str (),
11680 comp_dir);
11681 /* Create one if necessary. */
11682 if (*dwo_file_slot == NULL)
11683 {
11684 if (dwarf_read_debug)
11685 {
11686 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11687 virtual_dwo_name.c_str ());
11688 }
11689 dwo_file = new struct dwo_file;
11690 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11691 dwo_file->comp_dir = comp_dir;
11692 dwo_file->sections.abbrev = sections.abbrev;
11693 dwo_file->sections.line = sections.line;
11694 dwo_file->sections.loc = sections.loc;
11695 dwo_file->sections.macinfo = sections.macinfo;
11696 dwo_file->sections.macro = sections.macro;
11697 dwo_file->sections.str_offsets = sections.str_offsets;
11698 /* The "str" section is global to the entire DWP file. */
11699 dwo_file->sections.str = dwp_file->sections.str;
11700 /* The info or types section is assigned below to dwo_unit,
11701 there's no need to record it in dwo_file.
11702 Also, we can't simply record type sections in dwo_file because
11703 we record a pointer into the vector in dwo_unit. As we collect more
11704 types we'll grow the vector and eventually have to reallocate space
11705 for it, invalidating all copies of pointers into the previous
11706 contents. */
11707 *dwo_file_slot = dwo_file;
11708 }
11709 else
11710 {
11711 if (dwarf_read_debug)
11712 {
11713 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11714 virtual_dwo_name.c_str ());
11715 }
11716 dwo_file = (struct dwo_file *) *dwo_file_slot;
11717 }
11718
11719 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11720 dwo_unit->dwo_file = dwo_file;
11721 dwo_unit->signature = signature;
11722 dwo_unit->section =
11723 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11724 *dwo_unit->section = sections.info_or_types;
11725 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11726
11727 return dwo_unit;
11728 }
11729
11730 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11731 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11732 piece within that section used by a TU/CU, return a virtual section
11733 of just that piece. */
11734
11735 static struct dwarf2_section_info
11736 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11737 struct dwarf2_section_info *section,
11738 bfd_size_type offset, bfd_size_type size)
11739 {
11740 struct dwarf2_section_info result;
11741 asection *sectp;
11742
11743 gdb_assert (section != NULL);
11744 gdb_assert (!section->is_virtual);
11745
11746 memset (&result, 0, sizeof (result));
11747 result.s.containing_section = section;
11748 result.is_virtual = true;
11749
11750 if (size == 0)
11751 return result;
11752
11753 sectp = section->get_bfd_section ();
11754
11755 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11756 bounds of the real section. This is a pretty-rare event, so just
11757 flag an error (easier) instead of a warning and trying to cope. */
11758 if (sectp == NULL
11759 || offset + size > bfd_section_size (sectp))
11760 {
11761 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11762 " in section %s [in module %s]"),
11763 sectp ? bfd_section_name (sectp) : "<unknown>",
11764 objfile_name (dwarf2_per_objfile->objfile));
11765 }
11766
11767 result.virtual_offset = offset;
11768 result.size = size;
11769 return result;
11770 }
11771
11772 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11773 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11774 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11775 This is for DWP version 2 files. */
11776
11777 static struct dwo_unit *
11778 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11779 struct dwp_file *dwp_file,
11780 uint32_t unit_index,
11781 const char *comp_dir,
11782 ULONGEST signature, int is_debug_types)
11783 {
11784 struct objfile *objfile = dwarf2_per_objfile->objfile;
11785 const struct dwp_hash_table *dwp_htab =
11786 is_debug_types ? dwp_file->tus : dwp_file->cus;
11787 bfd *dbfd = dwp_file->dbfd.get ();
11788 const char *kind = is_debug_types ? "TU" : "CU";
11789 struct dwo_file *dwo_file;
11790 struct dwo_unit *dwo_unit;
11791 struct virtual_v2_dwo_sections sections;
11792 void **dwo_file_slot;
11793 int i;
11794
11795 gdb_assert (dwp_file->version == 2);
11796
11797 if (dwarf_read_debug)
11798 {
11799 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11800 kind,
11801 pulongest (unit_index), hex_string (signature),
11802 dwp_file->name);
11803 }
11804
11805 /* Fetch the section offsets of this DWO unit. */
11806
11807 memset (&sections, 0, sizeof (sections));
11808
11809 for (i = 0; i < dwp_htab->nr_columns; ++i)
11810 {
11811 uint32_t offset = read_4_bytes (dbfd,
11812 dwp_htab->section_pool.v2.offsets
11813 + (((unit_index - 1) * dwp_htab->nr_columns
11814 + i)
11815 * sizeof (uint32_t)));
11816 uint32_t size = read_4_bytes (dbfd,
11817 dwp_htab->section_pool.v2.sizes
11818 + (((unit_index - 1) * dwp_htab->nr_columns
11819 + i)
11820 * sizeof (uint32_t)));
11821
11822 switch (dwp_htab->section_pool.v2.section_ids[i])
11823 {
11824 case DW_SECT_INFO:
11825 case DW_SECT_TYPES:
11826 sections.info_or_types_offset = offset;
11827 sections.info_or_types_size = size;
11828 break;
11829 case DW_SECT_ABBREV:
11830 sections.abbrev_offset = offset;
11831 sections.abbrev_size = size;
11832 break;
11833 case DW_SECT_LINE:
11834 sections.line_offset = offset;
11835 sections.line_size = size;
11836 break;
11837 case DW_SECT_LOC:
11838 sections.loc_offset = offset;
11839 sections.loc_size = size;
11840 break;
11841 case DW_SECT_STR_OFFSETS:
11842 sections.str_offsets_offset = offset;
11843 sections.str_offsets_size = size;
11844 break;
11845 case DW_SECT_MACINFO:
11846 sections.macinfo_offset = offset;
11847 sections.macinfo_size = size;
11848 break;
11849 case DW_SECT_MACRO:
11850 sections.macro_offset = offset;
11851 sections.macro_size = size;
11852 break;
11853 }
11854 }
11855
11856 /* It's easier for the rest of the code if we fake a struct dwo_file and
11857 have dwo_unit "live" in that. At least for now.
11858
11859 The DWP file can be made up of a random collection of CUs and TUs.
11860 However, for each CU + set of TUs that came from the same original DWO
11861 file, we can combine them back into a virtual DWO file to save space
11862 (fewer struct dwo_file objects to allocate). Remember that for really
11863 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11864
11865 std::string virtual_dwo_name =
11866 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11867 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11868 (long) (sections.line_size ? sections.line_offset : 0),
11869 (long) (sections.loc_size ? sections.loc_offset : 0),
11870 (long) (sections.str_offsets_size
11871 ? sections.str_offsets_offset : 0));
11872 /* Can we use an existing virtual DWO file? */
11873 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11874 virtual_dwo_name.c_str (),
11875 comp_dir);
11876 /* Create one if necessary. */
11877 if (*dwo_file_slot == NULL)
11878 {
11879 if (dwarf_read_debug)
11880 {
11881 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11882 virtual_dwo_name.c_str ());
11883 }
11884 dwo_file = new struct dwo_file;
11885 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11886 dwo_file->comp_dir = comp_dir;
11887 dwo_file->sections.abbrev =
11888 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11889 sections.abbrev_offset, sections.abbrev_size);
11890 dwo_file->sections.line =
11891 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11892 sections.line_offset, sections.line_size);
11893 dwo_file->sections.loc =
11894 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11895 sections.loc_offset, sections.loc_size);
11896 dwo_file->sections.macinfo =
11897 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11898 sections.macinfo_offset, sections.macinfo_size);
11899 dwo_file->sections.macro =
11900 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11901 sections.macro_offset, sections.macro_size);
11902 dwo_file->sections.str_offsets =
11903 create_dwp_v2_section (dwarf2_per_objfile,
11904 &dwp_file->sections.str_offsets,
11905 sections.str_offsets_offset,
11906 sections.str_offsets_size);
11907 /* The "str" section is global to the entire DWP file. */
11908 dwo_file->sections.str = dwp_file->sections.str;
11909 /* The info or types section is assigned below to dwo_unit,
11910 there's no need to record it in dwo_file.
11911 Also, we can't simply record type sections in dwo_file because
11912 we record a pointer into the vector in dwo_unit. As we collect more
11913 types we'll grow the vector and eventually have to reallocate space
11914 for it, invalidating all copies of pointers into the previous
11915 contents. */
11916 *dwo_file_slot = dwo_file;
11917 }
11918 else
11919 {
11920 if (dwarf_read_debug)
11921 {
11922 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11923 virtual_dwo_name.c_str ());
11924 }
11925 dwo_file = (struct dwo_file *) *dwo_file_slot;
11926 }
11927
11928 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11929 dwo_unit->dwo_file = dwo_file;
11930 dwo_unit->signature = signature;
11931 dwo_unit->section =
11932 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11933 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11934 is_debug_types
11935 ? &dwp_file->sections.types
11936 : &dwp_file->sections.info,
11937 sections.info_or_types_offset,
11938 sections.info_or_types_size);
11939 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11940
11941 return dwo_unit;
11942 }
11943
11944 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11945 Returns NULL if the signature isn't found. */
11946
11947 static struct dwo_unit *
11948 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11949 struct dwp_file *dwp_file, const char *comp_dir,
11950 ULONGEST signature, int is_debug_types)
11951 {
11952 const struct dwp_hash_table *dwp_htab =
11953 is_debug_types ? dwp_file->tus : dwp_file->cus;
11954 bfd *dbfd = dwp_file->dbfd.get ();
11955 uint32_t mask = dwp_htab->nr_slots - 1;
11956 uint32_t hash = signature & mask;
11957 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11958 unsigned int i;
11959 void **slot;
11960 struct dwo_unit find_dwo_cu;
11961
11962 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11963 find_dwo_cu.signature = signature;
11964 slot = htab_find_slot (is_debug_types
11965 ? dwp_file->loaded_tus.get ()
11966 : dwp_file->loaded_cus.get (),
11967 &find_dwo_cu, INSERT);
11968
11969 if (*slot != NULL)
11970 return (struct dwo_unit *) *slot;
11971
11972 /* Use a for loop so that we don't loop forever on bad debug info. */
11973 for (i = 0; i < dwp_htab->nr_slots; ++i)
11974 {
11975 ULONGEST signature_in_table;
11976
11977 signature_in_table =
11978 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11979 if (signature_in_table == signature)
11980 {
11981 uint32_t unit_index =
11982 read_4_bytes (dbfd,
11983 dwp_htab->unit_table + hash * sizeof (uint32_t));
11984
11985 if (dwp_file->version == 1)
11986 {
11987 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11988 dwp_file, unit_index,
11989 comp_dir, signature,
11990 is_debug_types);
11991 }
11992 else
11993 {
11994 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11995 dwp_file, unit_index,
11996 comp_dir, signature,
11997 is_debug_types);
11998 }
11999 return (struct dwo_unit *) *slot;
12000 }
12001 if (signature_in_table == 0)
12002 return NULL;
12003 hash = (hash + hash2) & mask;
12004 }
12005
12006 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12007 " [in module %s]"),
12008 dwp_file->name);
12009 }
12010
12011 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12012 Open the file specified by FILE_NAME and hand it off to BFD for
12013 preliminary analysis. Return a newly initialized bfd *, which
12014 includes a canonicalized copy of FILE_NAME.
12015 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12016 SEARCH_CWD is true if the current directory is to be searched.
12017 It will be searched before debug-file-directory.
12018 If successful, the file is added to the bfd include table of the
12019 objfile's bfd (see gdb_bfd_record_inclusion).
12020 If unable to find/open the file, return NULL.
12021 NOTE: This function is derived from symfile_bfd_open. */
12022
12023 static gdb_bfd_ref_ptr
12024 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12025 const char *file_name, int is_dwp, int search_cwd)
12026 {
12027 int desc;
12028 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12029 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12030 to debug_file_directory. */
12031 const char *search_path;
12032 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12033
12034 gdb::unique_xmalloc_ptr<char> search_path_holder;
12035 if (search_cwd)
12036 {
12037 if (*debug_file_directory != '\0')
12038 {
12039 search_path_holder.reset (concat (".", dirname_separator_string,
12040 debug_file_directory,
12041 (char *) NULL));
12042 search_path = search_path_holder.get ();
12043 }
12044 else
12045 search_path = ".";
12046 }
12047 else
12048 search_path = debug_file_directory;
12049
12050 openp_flags flags = OPF_RETURN_REALPATH;
12051 if (is_dwp)
12052 flags |= OPF_SEARCH_IN_PATH;
12053
12054 gdb::unique_xmalloc_ptr<char> absolute_name;
12055 desc = openp (search_path, flags, file_name,
12056 O_RDONLY | O_BINARY, &absolute_name);
12057 if (desc < 0)
12058 return NULL;
12059
12060 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12061 gnutarget, desc));
12062 if (sym_bfd == NULL)
12063 return NULL;
12064 bfd_set_cacheable (sym_bfd.get (), 1);
12065
12066 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12067 return NULL;
12068
12069 /* Success. Record the bfd as having been included by the objfile's bfd.
12070 This is important because things like demangled_names_hash lives in the
12071 objfile's per_bfd space and may have references to things like symbol
12072 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12073 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12074
12075 return sym_bfd;
12076 }
12077
12078 /* Try to open DWO file FILE_NAME.
12079 COMP_DIR is the DW_AT_comp_dir attribute.
12080 The result is the bfd handle of the file.
12081 If there is a problem finding or opening the file, return NULL.
12082 Upon success, the canonicalized path of the file is stored in the bfd,
12083 same as symfile_bfd_open. */
12084
12085 static gdb_bfd_ref_ptr
12086 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12087 const char *file_name, const char *comp_dir)
12088 {
12089 if (IS_ABSOLUTE_PATH (file_name))
12090 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12091 0 /*is_dwp*/, 0 /*search_cwd*/);
12092
12093 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12094
12095 if (comp_dir != NULL)
12096 {
12097 gdb::unique_xmalloc_ptr<char> path_to_try
12098 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12099
12100 /* NOTE: If comp_dir is a relative path, this will also try the
12101 search path, which seems useful. */
12102 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12103 path_to_try.get (),
12104 0 /*is_dwp*/,
12105 1 /*search_cwd*/));
12106 if (abfd != NULL)
12107 return abfd;
12108 }
12109
12110 /* That didn't work, try debug-file-directory, which, despite its name,
12111 is a list of paths. */
12112
12113 if (*debug_file_directory == '\0')
12114 return NULL;
12115
12116 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12117 0 /*is_dwp*/, 1 /*search_cwd*/);
12118 }
12119
12120 /* This function is mapped across the sections and remembers the offset and
12121 size of each of the DWO debugging sections we are interested in. */
12122
12123 static void
12124 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12125 {
12126 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12127 const struct dwop_section_names *names = &dwop_section_names;
12128
12129 if (section_is_p (sectp->name, &names->abbrev_dwo))
12130 {
12131 dwo_sections->abbrev.s.section = sectp;
12132 dwo_sections->abbrev.size = bfd_section_size (sectp);
12133 }
12134 else if (section_is_p (sectp->name, &names->info_dwo))
12135 {
12136 dwo_sections->info.s.section = sectp;
12137 dwo_sections->info.size = bfd_section_size (sectp);
12138 }
12139 else if (section_is_p (sectp->name, &names->line_dwo))
12140 {
12141 dwo_sections->line.s.section = sectp;
12142 dwo_sections->line.size = bfd_section_size (sectp);
12143 }
12144 else if (section_is_p (sectp->name, &names->loc_dwo))
12145 {
12146 dwo_sections->loc.s.section = sectp;
12147 dwo_sections->loc.size = bfd_section_size (sectp);
12148 }
12149 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12150 {
12151 dwo_sections->macinfo.s.section = sectp;
12152 dwo_sections->macinfo.size = bfd_section_size (sectp);
12153 }
12154 else if (section_is_p (sectp->name, &names->macro_dwo))
12155 {
12156 dwo_sections->macro.s.section = sectp;
12157 dwo_sections->macro.size = bfd_section_size (sectp);
12158 }
12159 else if (section_is_p (sectp->name, &names->str_dwo))
12160 {
12161 dwo_sections->str.s.section = sectp;
12162 dwo_sections->str.size = bfd_section_size (sectp);
12163 }
12164 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12165 {
12166 dwo_sections->str_offsets.s.section = sectp;
12167 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12168 }
12169 else if (section_is_p (sectp->name, &names->types_dwo))
12170 {
12171 struct dwarf2_section_info type_section;
12172
12173 memset (&type_section, 0, sizeof (type_section));
12174 type_section.s.section = sectp;
12175 type_section.size = bfd_section_size (sectp);
12176 dwo_sections->types.push_back (type_section);
12177 }
12178 }
12179
12180 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12181 by PER_CU. This is for the non-DWP case.
12182 The result is NULL if DWO_NAME can't be found. */
12183
12184 static struct dwo_file *
12185 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12186 const char *dwo_name, const char *comp_dir)
12187 {
12188 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12189
12190 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12191 if (dbfd == NULL)
12192 {
12193 if (dwarf_read_debug)
12194 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12195 return NULL;
12196 }
12197
12198 dwo_file_up dwo_file (new struct dwo_file);
12199 dwo_file->dwo_name = dwo_name;
12200 dwo_file->comp_dir = comp_dir;
12201 dwo_file->dbfd = std::move (dbfd);
12202
12203 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12204 &dwo_file->sections);
12205
12206 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12207 dwo_file->sections.info, dwo_file->cus);
12208
12209 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12210 dwo_file->sections.types, dwo_file->tus);
12211
12212 if (dwarf_read_debug)
12213 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12214
12215 return dwo_file.release ();
12216 }
12217
12218 /* This function is mapped across the sections and remembers the offset and
12219 size of each of the DWP debugging sections common to version 1 and 2 that
12220 we are interested in. */
12221
12222 static void
12223 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12224 void *dwp_file_ptr)
12225 {
12226 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12227 const struct dwop_section_names *names = &dwop_section_names;
12228 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12229
12230 /* Record the ELF section number for later lookup: this is what the
12231 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12232 gdb_assert (elf_section_nr < dwp_file->num_sections);
12233 dwp_file->elf_sections[elf_section_nr] = sectp;
12234
12235 /* Look for specific sections that we need. */
12236 if (section_is_p (sectp->name, &names->str_dwo))
12237 {
12238 dwp_file->sections.str.s.section = sectp;
12239 dwp_file->sections.str.size = bfd_section_size (sectp);
12240 }
12241 else if (section_is_p (sectp->name, &names->cu_index))
12242 {
12243 dwp_file->sections.cu_index.s.section = sectp;
12244 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12245 }
12246 else if (section_is_p (sectp->name, &names->tu_index))
12247 {
12248 dwp_file->sections.tu_index.s.section = sectp;
12249 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12250 }
12251 }
12252
12253 /* This function is mapped across the sections and remembers the offset and
12254 size of each of the DWP version 2 debugging sections that we are interested
12255 in. This is split into a separate function because we don't know if we
12256 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12257
12258 static void
12259 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12260 {
12261 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12262 const struct dwop_section_names *names = &dwop_section_names;
12263 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12264
12265 /* Record the ELF section number for later lookup: this is what the
12266 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12267 gdb_assert (elf_section_nr < dwp_file->num_sections);
12268 dwp_file->elf_sections[elf_section_nr] = sectp;
12269
12270 /* Look for specific sections that we need. */
12271 if (section_is_p (sectp->name, &names->abbrev_dwo))
12272 {
12273 dwp_file->sections.abbrev.s.section = sectp;
12274 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12275 }
12276 else if (section_is_p (sectp->name, &names->info_dwo))
12277 {
12278 dwp_file->sections.info.s.section = sectp;
12279 dwp_file->sections.info.size = bfd_section_size (sectp);
12280 }
12281 else if (section_is_p (sectp->name, &names->line_dwo))
12282 {
12283 dwp_file->sections.line.s.section = sectp;
12284 dwp_file->sections.line.size = bfd_section_size (sectp);
12285 }
12286 else if (section_is_p (sectp->name, &names->loc_dwo))
12287 {
12288 dwp_file->sections.loc.s.section = sectp;
12289 dwp_file->sections.loc.size = bfd_section_size (sectp);
12290 }
12291 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12292 {
12293 dwp_file->sections.macinfo.s.section = sectp;
12294 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12295 }
12296 else if (section_is_p (sectp->name, &names->macro_dwo))
12297 {
12298 dwp_file->sections.macro.s.section = sectp;
12299 dwp_file->sections.macro.size = bfd_section_size (sectp);
12300 }
12301 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12302 {
12303 dwp_file->sections.str_offsets.s.section = sectp;
12304 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12305 }
12306 else if (section_is_p (sectp->name, &names->types_dwo))
12307 {
12308 dwp_file->sections.types.s.section = sectp;
12309 dwp_file->sections.types.size = bfd_section_size (sectp);
12310 }
12311 }
12312
12313 /* Hash function for dwp_file loaded CUs/TUs. */
12314
12315 static hashval_t
12316 hash_dwp_loaded_cutus (const void *item)
12317 {
12318 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12319
12320 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12321 return dwo_unit->signature;
12322 }
12323
12324 /* Equality function for dwp_file loaded CUs/TUs. */
12325
12326 static int
12327 eq_dwp_loaded_cutus (const void *a, const void *b)
12328 {
12329 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12330 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12331
12332 return dua->signature == dub->signature;
12333 }
12334
12335 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12336
12337 static htab_up
12338 allocate_dwp_loaded_cutus_table ()
12339 {
12340 return htab_up (htab_create_alloc (3,
12341 hash_dwp_loaded_cutus,
12342 eq_dwp_loaded_cutus,
12343 NULL, xcalloc, xfree));
12344 }
12345
12346 /* Try to open DWP file FILE_NAME.
12347 The result is the bfd handle of the file.
12348 If there is a problem finding or opening the file, return NULL.
12349 Upon success, the canonicalized path of the file is stored in the bfd,
12350 same as symfile_bfd_open. */
12351
12352 static gdb_bfd_ref_ptr
12353 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12354 const char *file_name)
12355 {
12356 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12357 1 /*is_dwp*/,
12358 1 /*search_cwd*/));
12359 if (abfd != NULL)
12360 return abfd;
12361
12362 /* Work around upstream bug 15652.
12363 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12364 [Whether that's a "bug" is debatable, but it is getting in our way.]
12365 We have no real idea where the dwp file is, because gdb's realpath-ing
12366 of the executable's path may have discarded the needed info.
12367 [IWBN if the dwp file name was recorded in the executable, akin to
12368 .gnu_debuglink, but that doesn't exist yet.]
12369 Strip the directory from FILE_NAME and search again. */
12370 if (*debug_file_directory != '\0')
12371 {
12372 /* Don't implicitly search the current directory here.
12373 If the user wants to search "." to handle this case,
12374 it must be added to debug-file-directory. */
12375 return try_open_dwop_file (dwarf2_per_objfile,
12376 lbasename (file_name), 1 /*is_dwp*/,
12377 0 /*search_cwd*/);
12378 }
12379
12380 return NULL;
12381 }
12382
12383 /* Initialize the use of the DWP file for the current objfile.
12384 By convention the name of the DWP file is ${objfile}.dwp.
12385 The result is NULL if it can't be found. */
12386
12387 static std::unique_ptr<struct dwp_file>
12388 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12389 {
12390 struct objfile *objfile = dwarf2_per_objfile->objfile;
12391
12392 /* Try to find first .dwp for the binary file before any symbolic links
12393 resolving. */
12394
12395 /* If the objfile is a debug file, find the name of the real binary
12396 file and get the name of dwp file from there. */
12397 std::string dwp_name;
12398 if (objfile->separate_debug_objfile_backlink != NULL)
12399 {
12400 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12401 const char *backlink_basename = lbasename (backlink->original_name);
12402
12403 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12404 }
12405 else
12406 dwp_name = objfile->original_name;
12407
12408 dwp_name += ".dwp";
12409
12410 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12411 if (dbfd == NULL
12412 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12413 {
12414 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12415 dwp_name = objfile_name (objfile);
12416 dwp_name += ".dwp";
12417 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12418 }
12419
12420 if (dbfd == NULL)
12421 {
12422 if (dwarf_read_debug)
12423 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12424 return std::unique_ptr<dwp_file> ();
12425 }
12426
12427 const char *name = bfd_get_filename (dbfd.get ());
12428 std::unique_ptr<struct dwp_file> dwp_file
12429 (new struct dwp_file (name, std::move (dbfd)));
12430
12431 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12432 dwp_file->elf_sections =
12433 OBSTACK_CALLOC (&objfile->objfile_obstack,
12434 dwp_file->num_sections, asection *);
12435
12436 bfd_map_over_sections (dwp_file->dbfd.get (),
12437 dwarf2_locate_common_dwp_sections,
12438 dwp_file.get ());
12439
12440 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12441 0);
12442
12443 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12444 1);
12445
12446 /* The DWP file version is stored in the hash table. Oh well. */
12447 if (dwp_file->cus && dwp_file->tus
12448 && dwp_file->cus->version != dwp_file->tus->version)
12449 {
12450 /* Technically speaking, we should try to limp along, but this is
12451 pretty bizarre. We use pulongest here because that's the established
12452 portability solution (e.g, we cannot use %u for uint32_t). */
12453 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12454 " TU version %s [in DWP file %s]"),
12455 pulongest (dwp_file->cus->version),
12456 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12457 }
12458
12459 if (dwp_file->cus)
12460 dwp_file->version = dwp_file->cus->version;
12461 else if (dwp_file->tus)
12462 dwp_file->version = dwp_file->tus->version;
12463 else
12464 dwp_file->version = 2;
12465
12466 if (dwp_file->version == 2)
12467 bfd_map_over_sections (dwp_file->dbfd.get (),
12468 dwarf2_locate_v2_dwp_sections,
12469 dwp_file.get ());
12470
12471 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12472 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12473
12474 if (dwarf_read_debug)
12475 {
12476 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12477 fprintf_unfiltered (gdb_stdlog,
12478 " %s CUs, %s TUs\n",
12479 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12480 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12481 }
12482
12483 return dwp_file;
12484 }
12485
12486 /* Wrapper around open_and_init_dwp_file, only open it once. */
12487
12488 static struct dwp_file *
12489 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12490 {
12491 if (! dwarf2_per_objfile->dwp_checked)
12492 {
12493 dwarf2_per_objfile->dwp_file
12494 = open_and_init_dwp_file (dwarf2_per_objfile);
12495 dwarf2_per_objfile->dwp_checked = 1;
12496 }
12497 return dwarf2_per_objfile->dwp_file.get ();
12498 }
12499
12500 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12501 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12502 or in the DWP file for the objfile, referenced by THIS_UNIT.
12503 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12504 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12505
12506 This is called, for example, when wanting to read a variable with a
12507 complex location. Therefore we don't want to do file i/o for every call.
12508 Therefore we don't want to look for a DWO file on every call.
12509 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12510 then we check if we've already seen DWO_NAME, and only THEN do we check
12511 for a DWO file.
12512
12513 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12514 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12515
12516 static struct dwo_unit *
12517 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12518 const char *dwo_name, const char *comp_dir,
12519 ULONGEST signature, int is_debug_types)
12520 {
12521 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12522 struct objfile *objfile = dwarf2_per_objfile->objfile;
12523 const char *kind = is_debug_types ? "TU" : "CU";
12524 void **dwo_file_slot;
12525 struct dwo_file *dwo_file;
12526 struct dwp_file *dwp_file;
12527
12528 /* First see if there's a DWP file.
12529 If we have a DWP file but didn't find the DWO inside it, don't
12530 look for the original DWO file. It makes gdb behave differently
12531 depending on whether one is debugging in the build tree. */
12532
12533 dwp_file = get_dwp_file (dwarf2_per_objfile);
12534 if (dwp_file != NULL)
12535 {
12536 const struct dwp_hash_table *dwp_htab =
12537 is_debug_types ? dwp_file->tus : dwp_file->cus;
12538
12539 if (dwp_htab != NULL)
12540 {
12541 struct dwo_unit *dwo_cutu =
12542 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12543 signature, is_debug_types);
12544
12545 if (dwo_cutu != NULL)
12546 {
12547 if (dwarf_read_debug)
12548 {
12549 fprintf_unfiltered (gdb_stdlog,
12550 "Virtual DWO %s %s found: @%s\n",
12551 kind, hex_string (signature),
12552 host_address_to_string (dwo_cutu));
12553 }
12554 return dwo_cutu;
12555 }
12556 }
12557 }
12558 else
12559 {
12560 /* No DWP file, look for the DWO file. */
12561
12562 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12563 dwo_name, comp_dir);
12564 if (*dwo_file_slot == NULL)
12565 {
12566 /* Read in the file and build a table of the CUs/TUs it contains. */
12567 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12568 }
12569 /* NOTE: This will be NULL if unable to open the file. */
12570 dwo_file = (struct dwo_file *) *dwo_file_slot;
12571
12572 if (dwo_file != NULL)
12573 {
12574 struct dwo_unit *dwo_cutu = NULL;
12575
12576 if (is_debug_types && dwo_file->tus)
12577 {
12578 struct dwo_unit find_dwo_cutu;
12579
12580 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12581 find_dwo_cutu.signature = signature;
12582 dwo_cutu
12583 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12584 &find_dwo_cutu);
12585 }
12586 else if (!is_debug_types && dwo_file->cus)
12587 {
12588 struct dwo_unit find_dwo_cutu;
12589
12590 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12591 find_dwo_cutu.signature = signature;
12592 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12593 &find_dwo_cutu);
12594 }
12595
12596 if (dwo_cutu != NULL)
12597 {
12598 if (dwarf_read_debug)
12599 {
12600 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12601 kind, dwo_name, hex_string (signature),
12602 host_address_to_string (dwo_cutu));
12603 }
12604 return dwo_cutu;
12605 }
12606 }
12607 }
12608
12609 /* We didn't find it. This could mean a dwo_id mismatch, or
12610 someone deleted the DWO/DWP file, or the search path isn't set up
12611 correctly to find the file. */
12612
12613 if (dwarf_read_debug)
12614 {
12615 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12616 kind, dwo_name, hex_string (signature));
12617 }
12618
12619 /* This is a warning and not a complaint because it can be caused by
12620 pilot error (e.g., user accidentally deleting the DWO). */
12621 {
12622 /* Print the name of the DWP file if we looked there, helps the user
12623 better diagnose the problem. */
12624 std::string dwp_text;
12625
12626 if (dwp_file != NULL)
12627 dwp_text = string_printf (" [in DWP file %s]",
12628 lbasename (dwp_file->name));
12629
12630 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12631 " [in module %s]"),
12632 kind, dwo_name, hex_string (signature),
12633 dwp_text.c_str (),
12634 this_unit->is_debug_types ? "TU" : "CU",
12635 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12636 }
12637 return NULL;
12638 }
12639
12640 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12641 See lookup_dwo_cutu_unit for details. */
12642
12643 static struct dwo_unit *
12644 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12645 const char *dwo_name, const char *comp_dir,
12646 ULONGEST signature)
12647 {
12648 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12649 }
12650
12651 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12652 See lookup_dwo_cutu_unit for details. */
12653
12654 static struct dwo_unit *
12655 lookup_dwo_type_unit (struct signatured_type *this_tu,
12656 const char *dwo_name, const char *comp_dir)
12657 {
12658 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12659 }
12660
12661 /* Traversal function for queue_and_load_all_dwo_tus. */
12662
12663 static int
12664 queue_and_load_dwo_tu (void **slot, void *info)
12665 {
12666 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12667 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12668 ULONGEST signature = dwo_unit->signature;
12669 struct signatured_type *sig_type =
12670 lookup_dwo_signatured_type (per_cu->cu, signature);
12671
12672 if (sig_type != NULL)
12673 {
12674 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12675
12676 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12677 a real dependency of PER_CU on SIG_TYPE. That is detected later
12678 while processing PER_CU. */
12679 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12680 load_full_type_unit (sig_cu);
12681 per_cu->imported_symtabs_push (sig_cu);
12682 }
12683
12684 return 1;
12685 }
12686
12687 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12688 The DWO may have the only definition of the type, though it may not be
12689 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12690 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12691
12692 static void
12693 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12694 {
12695 struct dwo_unit *dwo_unit;
12696 struct dwo_file *dwo_file;
12697
12698 gdb_assert (!per_cu->is_debug_types);
12699 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12700 gdb_assert (per_cu->cu != NULL);
12701
12702 dwo_unit = per_cu->cu->dwo_unit;
12703 gdb_assert (dwo_unit != NULL);
12704
12705 dwo_file = dwo_unit->dwo_file;
12706 if (dwo_file->tus != NULL)
12707 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12708 per_cu);
12709 }
12710
12711 /* Read in various DIEs. */
12712
12713 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12714 Inherit only the children of the DW_AT_abstract_origin DIE not being
12715 already referenced by DW_AT_abstract_origin from the children of the
12716 current DIE. */
12717
12718 static void
12719 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12720 {
12721 struct die_info *child_die;
12722 sect_offset *offsetp;
12723 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12724 struct die_info *origin_die;
12725 /* Iterator of the ORIGIN_DIE children. */
12726 struct die_info *origin_child_die;
12727 struct attribute *attr;
12728 struct dwarf2_cu *origin_cu;
12729 struct pending **origin_previous_list_in_scope;
12730
12731 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12732 if (!attr)
12733 return;
12734
12735 /* Note that following die references may follow to a die in a
12736 different cu. */
12737
12738 origin_cu = cu;
12739 origin_die = follow_die_ref (die, attr, &origin_cu);
12740
12741 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12742 symbols in. */
12743 origin_previous_list_in_scope = origin_cu->list_in_scope;
12744 origin_cu->list_in_scope = cu->list_in_scope;
12745
12746 if (die->tag != origin_die->tag
12747 && !(die->tag == DW_TAG_inlined_subroutine
12748 && origin_die->tag == DW_TAG_subprogram))
12749 complaint (_("DIE %s and its abstract origin %s have different tags"),
12750 sect_offset_str (die->sect_off),
12751 sect_offset_str (origin_die->sect_off));
12752
12753 std::vector<sect_offset> offsets;
12754
12755 for (child_die = die->child;
12756 child_die && child_die->tag;
12757 child_die = sibling_die (child_die))
12758 {
12759 struct die_info *child_origin_die;
12760 struct dwarf2_cu *child_origin_cu;
12761
12762 /* We are trying to process concrete instance entries:
12763 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12764 it's not relevant to our analysis here. i.e. detecting DIEs that are
12765 present in the abstract instance but not referenced in the concrete
12766 one. */
12767 if (child_die->tag == DW_TAG_call_site
12768 || child_die->tag == DW_TAG_GNU_call_site)
12769 continue;
12770
12771 /* For each CHILD_DIE, find the corresponding child of
12772 ORIGIN_DIE. If there is more than one layer of
12773 DW_AT_abstract_origin, follow them all; there shouldn't be,
12774 but GCC versions at least through 4.4 generate this (GCC PR
12775 40573). */
12776 child_origin_die = child_die;
12777 child_origin_cu = cu;
12778 while (1)
12779 {
12780 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12781 child_origin_cu);
12782 if (attr == NULL)
12783 break;
12784 child_origin_die = follow_die_ref (child_origin_die, attr,
12785 &child_origin_cu);
12786 }
12787
12788 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12789 counterpart may exist. */
12790 if (child_origin_die != child_die)
12791 {
12792 if (child_die->tag != child_origin_die->tag
12793 && !(child_die->tag == DW_TAG_inlined_subroutine
12794 && child_origin_die->tag == DW_TAG_subprogram))
12795 complaint (_("Child DIE %s and its abstract origin %s have "
12796 "different tags"),
12797 sect_offset_str (child_die->sect_off),
12798 sect_offset_str (child_origin_die->sect_off));
12799 if (child_origin_die->parent != origin_die)
12800 complaint (_("Child DIE %s and its abstract origin %s have "
12801 "different parents"),
12802 sect_offset_str (child_die->sect_off),
12803 sect_offset_str (child_origin_die->sect_off));
12804 else
12805 offsets.push_back (child_origin_die->sect_off);
12806 }
12807 }
12808 std::sort (offsets.begin (), offsets.end ());
12809 sect_offset *offsets_end = offsets.data () + offsets.size ();
12810 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12811 if (offsetp[-1] == *offsetp)
12812 complaint (_("Multiple children of DIE %s refer "
12813 "to DIE %s as their abstract origin"),
12814 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12815
12816 offsetp = offsets.data ();
12817 origin_child_die = origin_die->child;
12818 while (origin_child_die && origin_child_die->tag)
12819 {
12820 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12821 while (offsetp < offsets_end
12822 && *offsetp < origin_child_die->sect_off)
12823 offsetp++;
12824 if (offsetp >= offsets_end
12825 || *offsetp > origin_child_die->sect_off)
12826 {
12827 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12828 Check whether we're already processing ORIGIN_CHILD_DIE.
12829 This can happen with mutually referenced abstract_origins.
12830 PR 16581. */
12831 if (!origin_child_die->in_process)
12832 process_die (origin_child_die, origin_cu);
12833 }
12834 origin_child_die = sibling_die (origin_child_die);
12835 }
12836 origin_cu->list_in_scope = origin_previous_list_in_scope;
12837
12838 if (cu != origin_cu)
12839 compute_delayed_physnames (origin_cu);
12840 }
12841
12842 static void
12843 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12844 {
12845 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12846 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12847 struct context_stack *newobj;
12848 CORE_ADDR lowpc;
12849 CORE_ADDR highpc;
12850 struct die_info *child_die;
12851 struct attribute *attr, *call_line, *call_file;
12852 const char *name;
12853 CORE_ADDR baseaddr;
12854 struct block *block;
12855 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12856 std::vector<struct symbol *> template_args;
12857 struct template_symbol *templ_func = NULL;
12858
12859 if (inlined_func)
12860 {
12861 /* If we do not have call site information, we can't show the
12862 caller of this inlined function. That's too confusing, so
12863 only use the scope for local variables. */
12864 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12865 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12866 if (call_line == NULL || call_file == NULL)
12867 {
12868 read_lexical_block_scope (die, cu);
12869 return;
12870 }
12871 }
12872
12873 baseaddr = objfile->text_section_offset ();
12874
12875 name = dwarf2_name (die, cu);
12876
12877 /* Ignore functions with missing or empty names. These are actually
12878 illegal according to the DWARF standard. */
12879 if (name == NULL)
12880 {
12881 complaint (_("missing name for subprogram DIE at %s"),
12882 sect_offset_str (die->sect_off));
12883 return;
12884 }
12885
12886 /* Ignore functions with missing or invalid low and high pc attributes. */
12887 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12888 <= PC_BOUNDS_INVALID)
12889 {
12890 attr = dwarf2_attr (die, DW_AT_external, cu);
12891 if (!attr || !DW_UNSND (attr))
12892 complaint (_("cannot get low and high bounds "
12893 "for subprogram DIE at %s"),
12894 sect_offset_str (die->sect_off));
12895 return;
12896 }
12897
12898 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12899 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12900
12901 /* If we have any template arguments, then we must allocate a
12902 different sort of symbol. */
12903 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12904 {
12905 if (child_die->tag == DW_TAG_template_type_param
12906 || child_die->tag == DW_TAG_template_value_param)
12907 {
12908 templ_func = allocate_template_symbol (objfile);
12909 templ_func->subclass = SYMBOL_TEMPLATE;
12910 break;
12911 }
12912 }
12913
12914 newobj = cu->get_builder ()->push_context (0, lowpc);
12915 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12916 (struct symbol *) templ_func);
12917
12918 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12919 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12920 cu->language);
12921
12922 /* If there is a location expression for DW_AT_frame_base, record
12923 it. */
12924 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12925 if (attr != nullptr)
12926 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12927
12928 /* If there is a location for the static link, record it. */
12929 newobj->static_link = NULL;
12930 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12931 if (attr != nullptr)
12932 {
12933 newobj->static_link
12934 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12935 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12936 cu->per_cu->addr_type ());
12937 }
12938
12939 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12940
12941 if (die->child != NULL)
12942 {
12943 child_die = die->child;
12944 while (child_die && child_die->tag)
12945 {
12946 if (child_die->tag == DW_TAG_template_type_param
12947 || child_die->tag == DW_TAG_template_value_param)
12948 {
12949 struct symbol *arg = new_symbol (child_die, NULL, cu);
12950
12951 if (arg != NULL)
12952 template_args.push_back (arg);
12953 }
12954 else
12955 process_die (child_die, cu);
12956 child_die = sibling_die (child_die);
12957 }
12958 }
12959
12960 inherit_abstract_dies (die, cu);
12961
12962 /* If we have a DW_AT_specification, we might need to import using
12963 directives from the context of the specification DIE. See the
12964 comment in determine_prefix. */
12965 if (cu->language == language_cplus
12966 && dwarf2_attr (die, DW_AT_specification, cu))
12967 {
12968 struct dwarf2_cu *spec_cu = cu;
12969 struct die_info *spec_die = die_specification (die, &spec_cu);
12970
12971 while (spec_die)
12972 {
12973 child_die = spec_die->child;
12974 while (child_die && child_die->tag)
12975 {
12976 if (child_die->tag == DW_TAG_imported_module)
12977 process_die (child_die, spec_cu);
12978 child_die = sibling_die (child_die);
12979 }
12980
12981 /* In some cases, GCC generates specification DIEs that
12982 themselves contain DW_AT_specification attributes. */
12983 spec_die = die_specification (spec_die, &spec_cu);
12984 }
12985 }
12986
12987 struct context_stack cstk = cu->get_builder ()->pop_context ();
12988 /* Make a block for the local symbols within. */
12989 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12990 cstk.static_link, lowpc, highpc);
12991
12992 /* For C++, set the block's scope. */
12993 if ((cu->language == language_cplus
12994 || cu->language == language_fortran
12995 || cu->language == language_d
12996 || cu->language == language_rust)
12997 && cu->processing_has_namespace_info)
12998 block_set_scope (block, determine_prefix (die, cu),
12999 &objfile->objfile_obstack);
13000
13001 /* If we have address ranges, record them. */
13002 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13003
13004 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13005
13006 /* Attach template arguments to function. */
13007 if (!template_args.empty ())
13008 {
13009 gdb_assert (templ_func != NULL);
13010
13011 templ_func->n_template_arguments = template_args.size ();
13012 templ_func->template_arguments
13013 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13014 templ_func->n_template_arguments);
13015 memcpy (templ_func->template_arguments,
13016 template_args.data (),
13017 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13018
13019 /* Make sure that the symtab is set on the new symbols. Even
13020 though they don't appear in this symtab directly, other parts
13021 of gdb assume that symbols do, and this is reasonably
13022 true. */
13023 for (symbol *sym : template_args)
13024 symbol_set_symtab (sym, symbol_symtab (templ_func));
13025 }
13026
13027 /* In C++, we can have functions nested inside functions (e.g., when
13028 a function declares a class that has methods). This means that
13029 when we finish processing a function scope, we may need to go
13030 back to building a containing block's symbol lists. */
13031 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13032 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13033
13034 /* If we've finished processing a top-level function, subsequent
13035 symbols go in the file symbol list. */
13036 if (cu->get_builder ()->outermost_context_p ())
13037 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13038 }
13039
13040 /* Process all the DIES contained within a lexical block scope. Start
13041 a new scope, process the dies, and then close the scope. */
13042
13043 static void
13044 read_lexical_block_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 lowpc, highpc;
13049 struct die_info *child_die;
13050 CORE_ADDR baseaddr;
13051
13052 baseaddr = objfile->text_section_offset ();
13053
13054 /* Ignore blocks with missing or invalid low and high pc attributes. */
13055 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13056 as multiple lexical blocks? Handling children in a sane way would
13057 be nasty. Might be easier to properly extend generic blocks to
13058 describe ranges. */
13059 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13060 {
13061 case PC_BOUNDS_NOT_PRESENT:
13062 /* DW_TAG_lexical_block has no attributes, process its children as if
13063 there was no wrapping by that DW_TAG_lexical_block.
13064 GCC does no longer produces such DWARF since GCC r224161. */
13065 for (child_die = die->child;
13066 child_die != NULL && child_die->tag;
13067 child_die = sibling_die (child_die))
13068 process_die (child_die, cu);
13069 return;
13070 case PC_BOUNDS_INVALID:
13071 return;
13072 }
13073 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13074 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13075
13076 cu->get_builder ()->push_context (0, lowpc);
13077 if (die->child != NULL)
13078 {
13079 child_die = die->child;
13080 while (child_die && child_die->tag)
13081 {
13082 process_die (child_die, cu);
13083 child_die = sibling_die (child_die);
13084 }
13085 }
13086 inherit_abstract_dies (die, cu);
13087 struct context_stack cstk = cu->get_builder ()->pop_context ();
13088
13089 if (*cu->get_builder ()->get_local_symbols () != NULL
13090 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13091 {
13092 struct block *block
13093 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13094 cstk.start_addr, highpc);
13095
13096 /* Note that recording ranges after traversing children, as we
13097 do here, means that recording a parent's ranges entails
13098 walking across all its children's ranges as they appear in
13099 the address map, which is quadratic behavior.
13100
13101 It would be nicer to record the parent's ranges before
13102 traversing its children, simply overriding whatever you find
13103 there. But since we don't even decide whether to create a
13104 block until after we've traversed its children, that's hard
13105 to do. */
13106 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13107 }
13108 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13109 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13110 }
13111
13112 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13113
13114 static void
13115 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13116 {
13117 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13118 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13119 CORE_ADDR pc, baseaddr;
13120 struct attribute *attr;
13121 struct call_site *call_site, call_site_local;
13122 void **slot;
13123 int nparams;
13124 struct die_info *child_die;
13125
13126 baseaddr = objfile->text_section_offset ();
13127
13128 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13129 if (attr == NULL)
13130 {
13131 /* This was a pre-DWARF-5 GNU extension alias
13132 for DW_AT_call_return_pc. */
13133 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13134 }
13135 if (!attr)
13136 {
13137 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13138 "DIE %s [in module %s]"),
13139 sect_offset_str (die->sect_off), objfile_name (objfile));
13140 return;
13141 }
13142 pc = attr->value_as_address () + baseaddr;
13143 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13144
13145 if (cu->call_site_htab == NULL)
13146 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13147 NULL, &objfile->objfile_obstack,
13148 hashtab_obstack_allocate, NULL);
13149 call_site_local.pc = pc;
13150 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13151 if (*slot != NULL)
13152 {
13153 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13154 "DIE %s [in module %s]"),
13155 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13156 objfile_name (objfile));
13157 return;
13158 }
13159
13160 /* Count parameters at the caller. */
13161
13162 nparams = 0;
13163 for (child_die = die->child; child_die && child_die->tag;
13164 child_die = sibling_die (child_die))
13165 {
13166 if (child_die->tag != DW_TAG_call_site_parameter
13167 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13168 {
13169 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13170 "DW_TAG_call_site child DIE %s [in module %s]"),
13171 child_die->tag, sect_offset_str (child_die->sect_off),
13172 objfile_name (objfile));
13173 continue;
13174 }
13175
13176 nparams++;
13177 }
13178
13179 call_site
13180 = ((struct call_site *)
13181 obstack_alloc (&objfile->objfile_obstack,
13182 sizeof (*call_site)
13183 + (sizeof (*call_site->parameter) * (nparams - 1))));
13184 *slot = call_site;
13185 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13186 call_site->pc = pc;
13187
13188 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13189 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13190 {
13191 struct die_info *func_die;
13192
13193 /* Skip also over DW_TAG_inlined_subroutine. */
13194 for (func_die = die->parent;
13195 func_die && func_die->tag != DW_TAG_subprogram
13196 && func_die->tag != DW_TAG_subroutine_type;
13197 func_die = func_die->parent);
13198
13199 /* DW_AT_call_all_calls is a superset
13200 of DW_AT_call_all_tail_calls. */
13201 if (func_die
13202 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13203 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13204 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13205 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13206 {
13207 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13208 not complete. But keep CALL_SITE for look ups via call_site_htab,
13209 both the initial caller containing the real return address PC and
13210 the final callee containing the current PC of a chain of tail
13211 calls do not need to have the tail call list complete. But any
13212 function candidate for a virtual tail call frame searched via
13213 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13214 determined unambiguously. */
13215 }
13216 else
13217 {
13218 struct type *func_type = NULL;
13219
13220 if (func_die)
13221 func_type = get_die_type (func_die, cu);
13222 if (func_type != NULL)
13223 {
13224 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13225
13226 /* Enlist this call site to the function. */
13227 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13228 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13229 }
13230 else
13231 complaint (_("Cannot find function owning DW_TAG_call_site "
13232 "DIE %s [in module %s]"),
13233 sect_offset_str (die->sect_off), objfile_name (objfile));
13234 }
13235 }
13236
13237 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13238 if (attr == NULL)
13239 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13240 if (attr == NULL)
13241 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13242 if (attr == NULL)
13243 {
13244 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13245 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13246 }
13247 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13248 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13249 /* Keep NULL DWARF_BLOCK. */;
13250 else if (attr->form_is_block ())
13251 {
13252 struct dwarf2_locexpr_baton *dlbaton;
13253
13254 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13255 dlbaton->data = DW_BLOCK (attr)->data;
13256 dlbaton->size = DW_BLOCK (attr)->size;
13257 dlbaton->per_cu = cu->per_cu;
13258
13259 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13260 }
13261 else if (attr->form_is_ref ())
13262 {
13263 struct dwarf2_cu *target_cu = cu;
13264 struct die_info *target_die;
13265
13266 target_die = follow_die_ref (die, attr, &target_cu);
13267 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13268 if (die_is_declaration (target_die, target_cu))
13269 {
13270 const char *target_physname;
13271
13272 /* Prefer the mangled name; otherwise compute the demangled one. */
13273 target_physname = dw2_linkage_name (target_die, target_cu);
13274 if (target_physname == NULL)
13275 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13276 if (target_physname == NULL)
13277 complaint (_("DW_AT_call_target target DIE has invalid "
13278 "physname, for referencing DIE %s [in module %s]"),
13279 sect_offset_str (die->sect_off), objfile_name (objfile));
13280 else
13281 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13282 }
13283 else
13284 {
13285 CORE_ADDR lowpc;
13286
13287 /* DW_AT_entry_pc should be preferred. */
13288 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13289 <= PC_BOUNDS_INVALID)
13290 complaint (_("DW_AT_call_target target DIE has invalid "
13291 "low pc, for referencing DIE %s [in module %s]"),
13292 sect_offset_str (die->sect_off), objfile_name (objfile));
13293 else
13294 {
13295 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13296 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13297 }
13298 }
13299 }
13300 else
13301 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13302 "block nor reference, for DIE %s [in module %s]"),
13303 sect_offset_str (die->sect_off), objfile_name (objfile));
13304
13305 call_site->per_cu = cu->per_cu;
13306
13307 for (child_die = die->child;
13308 child_die && child_die->tag;
13309 child_die = sibling_die (child_die))
13310 {
13311 struct call_site_parameter *parameter;
13312 struct attribute *loc, *origin;
13313
13314 if (child_die->tag != DW_TAG_call_site_parameter
13315 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13316 {
13317 /* Already printed the complaint above. */
13318 continue;
13319 }
13320
13321 gdb_assert (call_site->parameter_count < nparams);
13322 parameter = &call_site->parameter[call_site->parameter_count];
13323
13324 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13325 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13326 register is contained in DW_AT_call_value. */
13327
13328 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13329 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13330 if (origin == NULL)
13331 {
13332 /* This was a pre-DWARF-5 GNU extension alias
13333 for DW_AT_call_parameter. */
13334 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13335 }
13336 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13337 {
13338 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13339
13340 sect_offset sect_off
13341 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13342 if (!cu->header.offset_in_cu_p (sect_off))
13343 {
13344 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13345 binding can be done only inside one CU. Such referenced DIE
13346 therefore cannot be even moved to DW_TAG_partial_unit. */
13347 complaint (_("DW_AT_call_parameter offset is not in CU for "
13348 "DW_TAG_call_site child DIE %s [in module %s]"),
13349 sect_offset_str (child_die->sect_off),
13350 objfile_name (objfile));
13351 continue;
13352 }
13353 parameter->u.param_cu_off
13354 = (cu_offset) (sect_off - cu->header.sect_off);
13355 }
13356 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13357 {
13358 complaint (_("No DW_FORM_block* DW_AT_location for "
13359 "DW_TAG_call_site child DIE %s [in module %s]"),
13360 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13361 continue;
13362 }
13363 else
13364 {
13365 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13366 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13367 if (parameter->u.dwarf_reg != -1)
13368 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13369 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13370 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13371 &parameter->u.fb_offset))
13372 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13373 else
13374 {
13375 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13376 "for DW_FORM_block* DW_AT_location is supported for "
13377 "DW_TAG_call_site child DIE %s "
13378 "[in module %s]"),
13379 sect_offset_str (child_die->sect_off),
13380 objfile_name (objfile));
13381 continue;
13382 }
13383 }
13384
13385 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13386 if (attr == NULL)
13387 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13388 if (attr == NULL || !attr->form_is_block ())
13389 {
13390 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13391 "DW_TAG_call_site child DIE %s [in module %s]"),
13392 sect_offset_str (child_die->sect_off),
13393 objfile_name (objfile));
13394 continue;
13395 }
13396 parameter->value = DW_BLOCK (attr)->data;
13397 parameter->value_size = DW_BLOCK (attr)->size;
13398
13399 /* Parameters are not pre-cleared by memset above. */
13400 parameter->data_value = NULL;
13401 parameter->data_value_size = 0;
13402 call_site->parameter_count++;
13403
13404 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13405 if (attr == NULL)
13406 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13407 if (attr != nullptr)
13408 {
13409 if (!attr->form_is_block ())
13410 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13411 "DW_TAG_call_site child DIE %s [in module %s]"),
13412 sect_offset_str (child_die->sect_off),
13413 objfile_name (objfile));
13414 else
13415 {
13416 parameter->data_value = DW_BLOCK (attr)->data;
13417 parameter->data_value_size = DW_BLOCK (attr)->size;
13418 }
13419 }
13420 }
13421 }
13422
13423 /* Helper function for read_variable. If DIE represents a virtual
13424 table, then return the type of the concrete object that is
13425 associated with the virtual table. Otherwise, return NULL. */
13426
13427 static struct type *
13428 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13429 {
13430 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13431 if (attr == NULL)
13432 return NULL;
13433
13434 /* Find the type DIE. */
13435 struct die_info *type_die = NULL;
13436 struct dwarf2_cu *type_cu = cu;
13437
13438 if (attr->form_is_ref ())
13439 type_die = follow_die_ref (die, attr, &type_cu);
13440 if (type_die == NULL)
13441 return NULL;
13442
13443 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13444 return NULL;
13445 return die_containing_type (type_die, type_cu);
13446 }
13447
13448 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13449
13450 static void
13451 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13452 {
13453 struct rust_vtable_symbol *storage = NULL;
13454
13455 if (cu->language == language_rust)
13456 {
13457 struct type *containing_type = rust_containing_type (die, cu);
13458
13459 if (containing_type != NULL)
13460 {
13461 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13462
13463 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13464 initialize_objfile_symbol (storage);
13465 storage->concrete_type = containing_type;
13466 storage->subclass = SYMBOL_RUST_VTABLE;
13467 }
13468 }
13469
13470 struct symbol *res = new_symbol (die, NULL, cu, storage);
13471 struct attribute *abstract_origin
13472 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13473 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13474 if (res == NULL && loc && abstract_origin)
13475 {
13476 /* We have a variable without a name, but with a location and an abstract
13477 origin. This may be a concrete instance of an abstract variable
13478 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13479 later. */
13480 struct dwarf2_cu *origin_cu = cu;
13481 struct die_info *origin_die
13482 = follow_die_ref (die, abstract_origin, &origin_cu);
13483 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13484 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13485 }
13486 }
13487
13488 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13489 reading .debug_rnglists.
13490 Callback's type should be:
13491 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13492 Return true if the attributes are present and valid, otherwise,
13493 return false. */
13494
13495 template <typename Callback>
13496 static bool
13497 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13498 Callback &&callback)
13499 {
13500 struct dwarf2_per_objfile *dwarf2_per_objfile
13501 = cu->per_cu->dwarf2_per_objfile;
13502 struct objfile *objfile = dwarf2_per_objfile->objfile;
13503 bfd *obfd = objfile->obfd;
13504 /* Base address selection entry. */
13505 CORE_ADDR base;
13506 int found_base;
13507 const gdb_byte *buffer;
13508 CORE_ADDR baseaddr;
13509 bool overflow = false;
13510
13511 found_base = cu->base_known;
13512 base = cu->base_address;
13513
13514 dwarf2_per_objfile->rnglists.read (objfile);
13515 if (offset >= dwarf2_per_objfile->rnglists.size)
13516 {
13517 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13518 offset);
13519 return false;
13520 }
13521 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13522
13523 baseaddr = objfile->text_section_offset ();
13524
13525 while (1)
13526 {
13527 /* Initialize it due to a false compiler warning. */
13528 CORE_ADDR range_beginning = 0, range_end = 0;
13529 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13530 + dwarf2_per_objfile->rnglists.size);
13531 unsigned int bytes_read;
13532
13533 if (buffer == buf_end)
13534 {
13535 overflow = true;
13536 break;
13537 }
13538 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13539 switch (rlet)
13540 {
13541 case DW_RLE_end_of_list:
13542 break;
13543 case DW_RLE_base_address:
13544 if (buffer + cu->header.addr_size > buf_end)
13545 {
13546 overflow = true;
13547 break;
13548 }
13549 base = cu->header.read_address (obfd, buffer, &bytes_read);
13550 found_base = 1;
13551 buffer += bytes_read;
13552 break;
13553 case DW_RLE_start_length:
13554 if (buffer + cu->header.addr_size > buf_end)
13555 {
13556 overflow = true;
13557 break;
13558 }
13559 range_beginning = cu->header.read_address (obfd, buffer,
13560 &bytes_read);
13561 buffer += bytes_read;
13562 range_end = (range_beginning
13563 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13564 buffer += bytes_read;
13565 if (buffer > buf_end)
13566 {
13567 overflow = true;
13568 break;
13569 }
13570 break;
13571 case DW_RLE_offset_pair:
13572 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13573 buffer += bytes_read;
13574 if (buffer > buf_end)
13575 {
13576 overflow = true;
13577 break;
13578 }
13579 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13580 buffer += bytes_read;
13581 if (buffer > buf_end)
13582 {
13583 overflow = true;
13584 break;
13585 }
13586 break;
13587 case DW_RLE_start_end:
13588 if (buffer + 2 * cu->header.addr_size > buf_end)
13589 {
13590 overflow = true;
13591 break;
13592 }
13593 range_beginning = cu->header.read_address (obfd, buffer,
13594 &bytes_read);
13595 buffer += bytes_read;
13596 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13597 buffer += bytes_read;
13598 break;
13599 default:
13600 complaint (_("Invalid .debug_rnglists data (no base address)"));
13601 return false;
13602 }
13603 if (rlet == DW_RLE_end_of_list || overflow)
13604 break;
13605 if (rlet == DW_RLE_base_address)
13606 continue;
13607
13608 if (!found_base)
13609 {
13610 /* We have no valid base address for the ranges
13611 data. */
13612 complaint (_("Invalid .debug_rnglists data (no base address)"));
13613 return false;
13614 }
13615
13616 if (range_beginning > range_end)
13617 {
13618 /* Inverted range entries are invalid. */
13619 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13620 return false;
13621 }
13622
13623 /* Empty range entries have no effect. */
13624 if (range_beginning == range_end)
13625 continue;
13626
13627 range_beginning += base;
13628 range_end += base;
13629
13630 /* A not-uncommon case of bad debug info.
13631 Don't pollute the addrmap with bad data. */
13632 if (range_beginning + baseaddr == 0
13633 && !dwarf2_per_objfile->has_section_at_zero)
13634 {
13635 complaint (_(".debug_rnglists entry has start address of zero"
13636 " [in module %s]"), objfile_name (objfile));
13637 continue;
13638 }
13639
13640 callback (range_beginning, range_end);
13641 }
13642
13643 if (overflow)
13644 {
13645 complaint (_("Offset %d is not terminated "
13646 "for DW_AT_ranges attribute"),
13647 offset);
13648 return false;
13649 }
13650
13651 return true;
13652 }
13653
13654 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13655 Callback's type should be:
13656 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13657 Return 1 if the attributes are present and valid, otherwise, return 0. */
13658
13659 template <typename Callback>
13660 static int
13661 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13662 Callback &&callback)
13663 {
13664 struct dwarf2_per_objfile *dwarf2_per_objfile
13665 = cu->per_cu->dwarf2_per_objfile;
13666 struct objfile *objfile = dwarf2_per_objfile->objfile;
13667 struct comp_unit_head *cu_header = &cu->header;
13668 bfd *obfd = objfile->obfd;
13669 unsigned int addr_size = cu_header->addr_size;
13670 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13671 /* Base address selection entry. */
13672 CORE_ADDR base;
13673 int found_base;
13674 unsigned int dummy;
13675 const gdb_byte *buffer;
13676 CORE_ADDR baseaddr;
13677
13678 if (cu_header->version >= 5)
13679 return dwarf2_rnglists_process (offset, cu, callback);
13680
13681 found_base = cu->base_known;
13682 base = cu->base_address;
13683
13684 dwarf2_per_objfile->ranges.read (objfile);
13685 if (offset >= dwarf2_per_objfile->ranges.size)
13686 {
13687 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13688 offset);
13689 return 0;
13690 }
13691 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13692
13693 baseaddr = objfile->text_section_offset ();
13694
13695 while (1)
13696 {
13697 CORE_ADDR range_beginning, range_end;
13698
13699 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13700 buffer += addr_size;
13701 range_end = cu->header.read_address (obfd, buffer, &dummy);
13702 buffer += addr_size;
13703 offset += 2 * addr_size;
13704
13705 /* An end of list marker is a pair of zero addresses. */
13706 if (range_beginning == 0 && range_end == 0)
13707 /* Found the end of list entry. */
13708 break;
13709
13710 /* Each base address selection entry is a pair of 2 values.
13711 The first is the largest possible address, the second is
13712 the base address. Check for a base address here. */
13713 if ((range_beginning & mask) == mask)
13714 {
13715 /* If we found the largest possible address, then we already
13716 have the base address in range_end. */
13717 base = range_end;
13718 found_base = 1;
13719 continue;
13720 }
13721
13722 if (!found_base)
13723 {
13724 /* We have no valid base address for the ranges
13725 data. */
13726 complaint (_("Invalid .debug_ranges data (no base address)"));
13727 return 0;
13728 }
13729
13730 if (range_beginning > range_end)
13731 {
13732 /* Inverted range entries are invalid. */
13733 complaint (_("Invalid .debug_ranges data (inverted range)"));
13734 return 0;
13735 }
13736
13737 /* Empty range entries have no effect. */
13738 if (range_beginning == range_end)
13739 continue;
13740
13741 range_beginning += base;
13742 range_end += base;
13743
13744 /* A not-uncommon case of bad debug info.
13745 Don't pollute the addrmap with bad data. */
13746 if (range_beginning + baseaddr == 0
13747 && !dwarf2_per_objfile->has_section_at_zero)
13748 {
13749 complaint (_(".debug_ranges entry has start address of zero"
13750 " [in module %s]"), objfile_name (objfile));
13751 continue;
13752 }
13753
13754 callback (range_beginning, range_end);
13755 }
13756
13757 return 1;
13758 }
13759
13760 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13761 Return 1 if the attributes are present and valid, otherwise, return 0.
13762 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13763
13764 static int
13765 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13766 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13767 dwarf2_psymtab *ranges_pst)
13768 {
13769 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13770 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13771 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13772 int low_set = 0;
13773 CORE_ADDR low = 0;
13774 CORE_ADDR high = 0;
13775 int retval;
13776
13777 retval = dwarf2_ranges_process (offset, cu,
13778 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13779 {
13780 if (ranges_pst != NULL)
13781 {
13782 CORE_ADDR lowpc;
13783 CORE_ADDR highpc;
13784
13785 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13786 range_beginning + baseaddr)
13787 - baseaddr);
13788 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13789 range_end + baseaddr)
13790 - baseaddr);
13791 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13792 lowpc, highpc - 1, ranges_pst);
13793 }
13794
13795 /* FIXME: This is recording everything as a low-high
13796 segment of consecutive addresses. We should have a
13797 data structure for discontiguous block ranges
13798 instead. */
13799 if (! low_set)
13800 {
13801 low = range_beginning;
13802 high = range_end;
13803 low_set = 1;
13804 }
13805 else
13806 {
13807 if (range_beginning < low)
13808 low = range_beginning;
13809 if (range_end > high)
13810 high = range_end;
13811 }
13812 });
13813 if (!retval)
13814 return 0;
13815
13816 if (! low_set)
13817 /* If the first entry is an end-of-list marker, the range
13818 describes an empty scope, i.e. no instructions. */
13819 return 0;
13820
13821 if (low_return)
13822 *low_return = low;
13823 if (high_return)
13824 *high_return = high;
13825 return 1;
13826 }
13827
13828 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13829 definition for the return value. *LOWPC and *HIGHPC are set iff
13830 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13831
13832 static enum pc_bounds_kind
13833 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13834 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13835 dwarf2_psymtab *pst)
13836 {
13837 struct dwarf2_per_objfile *dwarf2_per_objfile
13838 = cu->per_cu->dwarf2_per_objfile;
13839 struct attribute *attr;
13840 struct attribute *attr_high;
13841 CORE_ADDR low = 0;
13842 CORE_ADDR high = 0;
13843 enum pc_bounds_kind ret;
13844
13845 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13846 if (attr_high)
13847 {
13848 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13849 if (attr != nullptr)
13850 {
13851 low = attr->value_as_address ();
13852 high = attr_high->value_as_address ();
13853 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13854 high += low;
13855 }
13856 else
13857 /* Found high w/o low attribute. */
13858 return PC_BOUNDS_INVALID;
13859
13860 /* Found consecutive range of addresses. */
13861 ret = PC_BOUNDS_HIGH_LOW;
13862 }
13863 else
13864 {
13865 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13866 if (attr != NULL)
13867 {
13868 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13869 We take advantage of the fact that DW_AT_ranges does not appear
13870 in DW_TAG_compile_unit of DWO files. */
13871 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13872 unsigned int ranges_offset = (DW_UNSND (attr)
13873 + (need_ranges_base
13874 ? cu->ranges_base
13875 : 0));
13876
13877 /* Value of the DW_AT_ranges attribute is the offset in the
13878 .debug_ranges section. */
13879 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13880 return PC_BOUNDS_INVALID;
13881 /* Found discontinuous range of addresses. */
13882 ret = PC_BOUNDS_RANGES;
13883 }
13884 else
13885 return PC_BOUNDS_NOT_PRESENT;
13886 }
13887
13888 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13889 if (high <= low)
13890 return PC_BOUNDS_INVALID;
13891
13892 /* When using the GNU linker, .gnu.linkonce. sections are used to
13893 eliminate duplicate copies of functions and vtables and such.
13894 The linker will arbitrarily choose one and discard the others.
13895 The AT_*_pc values for such functions refer to local labels in
13896 these sections. If the section from that file was discarded, the
13897 labels are not in the output, so the relocs get a value of 0.
13898 If this is a discarded function, mark the pc bounds as invalid,
13899 so that GDB will ignore it. */
13900 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13901 return PC_BOUNDS_INVALID;
13902
13903 *lowpc = low;
13904 if (highpc)
13905 *highpc = high;
13906 return ret;
13907 }
13908
13909 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13910 its low and high PC addresses. Do nothing if these addresses could not
13911 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13912 and HIGHPC to the high address if greater than HIGHPC. */
13913
13914 static void
13915 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13916 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13917 struct dwarf2_cu *cu)
13918 {
13919 CORE_ADDR low, high;
13920 struct die_info *child = die->child;
13921
13922 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13923 {
13924 *lowpc = std::min (*lowpc, low);
13925 *highpc = std::max (*highpc, high);
13926 }
13927
13928 /* If the language does not allow nested subprograms (either inside
13929 subprograms or lexical blocks), we're done. */
13930 if (cu->language != language_ada)
13931 return;
13932
13933 /* Check all the children of the given DIE. If it contains nested
13934 subprograms, then check their pc bounds. Likewise, we need to
13935 check lexical blocks as well, as they may also contain subprogram
13936 definitions. */
13937 while (child && child->tag)
13938 {
13939 if (child->tag == DW_TAG_subprogram
13940 || child->tag == DW_TAG_lexical_block)
13941 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13942 child = sibling_die (child);
13943 }
13944 }
13945
13946 /* Get the low and high pc's represented by the scope DIE, and store
13947 them in *LOWPC and *HIGHPC. If the correct values can't be
13948 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13949
13950 static void
13951 get_scope_pc_bounds (struct die_info *die,
13952 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13953 struct dwarf2_cu *cu)
13954 {
13955 CORE_ADDR best_low = (CORE_ADDR) -1;
13956 CORE_ADDR best_high = (CORE_ADDR) 0;
13957 CORE_ADDR current_low, current_high;
13958
13959 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13960 >= PC_BOUNDS_RANGES)
13961 {
13962 best_low = current_low;
13963 best_high = current_high;
13964 }
13965 else
13966 {
13967 struct die_info *child = die->child;
13968
13969 while (child && child->tag)
13970 {
13971 switch (child->tag) {
13972 case DW_TAG_subprogram:
13973 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13974 break;
13975 case DW_TAG_namespace:
13976 case DW_TAG_module:
13977 /* FIXME: carlton/2004-01-16: Should we do this for
13978 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13979 that current GCC's always emit the DIEs corresponding
13980 to definitions of methods of classes as children of a
13981 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13982 the DIEs giving the declarations, which could be
13983 anywhere). But I don't see any reason why the
13984 standards says that they have to be there. */
13985 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13986
13987 if (current_low != ((CORE_ADDR) -1))
13988 {
13989 best_low = std::min (best_low, current_low);
13990 best_high = std::max (best_high, current_high);
13991 }
13992 break;
13993 default:
13994 /* Ignore. */
13995 break;
13996 }
13997
13998 child = sibling_die (child);
13999 }
14000 }
14001
14002 *lowpc = best_low;
14003 *highpc = best_high;
14004 }
14005
14006 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14007 in DIE. */
14008
14009 static void
14010 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14011 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14012 {
14013 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14014 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14015 struct attribute *attr;
14016 struct attribute *attr_high;
14017
14018 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14019 if (attr_high)
14020 {
14021 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14022 if (attr != nullptr)
14023 {
14024 CORE_ADDR low = attr->value_as_address ();
14025 CORE_ADDR high = attr_high->value_as_address ();
14026
14027 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14028 high += low;
14029
14030 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14031 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14032 cu->get_builder ()->record_block_range (block, low, high - 1);
14033 }
14034 }
14035
14036 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14037 if (attr != nullptr)
14038 {
14039 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14040 We take advantage of the fact that DW_AT_ranges does not appear
14041 in DW_TAG_compile_unit of DWO files. */
14042 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14043
14044 /* The value of the DW_AT_ranges attribute is the offset of the
14045 address range list in the .debug_ranges section. */
14046 unsigned long offset = (DW_UNSND (attr)
14047 + (need_ranges_base ? cu->ranges_base : 0));
14048
14049 std::vector<blockrange> blockvec;
14050 dwarf2_ranges_process (offset, cu,
14051 [&] (CORE_ADDR start, CORE_ADDR end)
14052 {
14053 start += baseaddr;
14054 end += baseaddr;
14055 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14056 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14057 cu->get_builder ()->record_block_range (block, start, end - 1);
14058 blockvec.emplace_back (start, end);
14059 });
14060
14061 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14062 }
14063 }
14064
14065 /* Check whether the producer field indicates either of GCC < 4.6, or the
14066 Intel C/C++ compiler, and cache the result in CU. */
14067
14068 static void
14069 check_producer (struct dwarf2_cu *cu)
14070 {
14071 int major, minor;
14072
14073 if (cu->producer == NULL)
14074 {
14075 /* For unknown compilers expect their behavior is DWARF version
14076 compliant.
14077
14078 GCC started to support .debug_types sections by -gdwarf-4 since
14079 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14080 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14081 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14082 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14083 }
14084 else if (producer_is_gcc (cu->producer, &major, &minor))
14085 {
14086 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14087 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14088 }
14089 else if (producer_is_icc (cu->producer, &major, &minor))
14090 {
14091 cu->producer_is_icc = true;
14092 cu->producer_is_icc_lt_14 = major < 14;
14093 }
14094 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14095 cu->producer_is_codewarrior = true;
14096 else
14097 {
14098 /* For other non-GCC compilers, expect their behavior is DWARF version
14099 compliant. */
14100 }
14101
14102 cu->checked_producer = true;
14103 }
14104
14105 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14106 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14107 during 4.6.0 experimental. */
14108
14109 static bool
14110 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14111 {
14112 if (!cu->checked_producer)
14113 check_producer (cu);
14114
14115 return cu->producer_is_gxx_lt_4_6;
14116 }
14117
14118
14119 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14120 with incorrect is_stmt attributes. */
14121
14122 static bool
14123 producer_is_codewarrior (struct dwarf2_cu *cu)
14124 {
14125 if (!cu->checked_producer)
14126 check_producer (cu);
14127
14128 return cu->producer_is_codewarrior;
14129 }
14130
14131 /* Return the default accessibility type if it is not overridden by
14132 DW_AT_accessibility. */
14133
14134 static enum dwarf_access_attribute
14135 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14136 {
14137 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14138 {
14139 /* The default DWARF 2 accessibility for members is public, the default
14140 accessibility for inheritance is private. */
14141
14142 if (die->tag != DW_TAG_inheritance)
14143 return DW_ACCESS_public;
14144 else
14145 return DW_ACCESS_private;
14146 }
14147 else
14148 {
14149 /* DWARF 3+ defines the default accessibility a different way. The same
14150 rules apply now for DW_TAG_inheritance as for the members and it only
14151 depends on the container kind. */
14152
14153 if (die->parent->tag == DW_TAG_class_type)
14154 return DW_ACCESS_private;
14155 else
14156 return DW_ACCESS_public;
14157 }
14158 }
14159
14160 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14161 offset. If the attribute was not found return 0, otherwise return
14162 1. If it was found but could not properly be handled, set *OFFSET
14163 to 0. */
14164
14165 static int
14166 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14167 LONGEST *offset)
14168 {
14169 struct attribute *attr;
14170
14171 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14172 if (attr != NULL)
14173 {
14174 *offset = 0;
14175
14176 /* Note that we do not check for a section offset first here.
14177 This is because DW_AT_data_member_location is new in DWARF 4,
14178 so if we see it, we can assume that a constant form is really
14179 a constant and not a section offset. */
14180 if (attr->form_is_constant ())
14181 *offset = dwarf2_get_attr_constant_value (attr, 0);
14182 else if (attr->form_is_section_offset ())
14183 dwarf2_complex_location_expr_complaint ();
14184 else if (attr->form_is_block ())
14185 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14186 else
14187 dwarf2_complex_location_expr_complaint ();
14188
14189 return 1;
14190 }
14191
14192 return 0;
14193 }
14194
14195 /* Add an aggregate field to the field list. */
14196
14197 static void
14198 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14199 struct dwarf2_cu *cu)
14200 {
14201 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14202 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14203 struct nextfield *new_field;
14204 struct attribute *attr;
14205 struct field *fp;
14206 const char *fieldname = "";
14207
14208 if (die->tag == DW_TAG_inheritance)
14209 {
14210 fip->baseclasses.emplace_back ();
14211 new_field = &fip->baseclasses.back ();
14212 }
14213 else
14214 {
14215 fip->fields.emplace_back ();
14216 new_field = &fip->fields.back ();
14217 }
14218
14219 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14220 if (attr != nullptr)
14221 new_field->accessibility = DW_UNSND (attr);
14222 else
14223 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14224 if (new_field->accessibility != DW_ACCESS_public)
14225 fip->non_public_fields = 1;
14226
14227 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14228 if (attr != nullptr)
14229 new_field->virtuality = DW_UNSND (attr);
14230 else
14231 new_field->virtuality = DW_VIRTUALITY_none;
14232
14233 fp = &new_field->field;
14234
14235 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14236 {
14237 LONGEST offset;
14238
14239 /* Data member other than a C++ static data member. */
14240
14241 /* Get type of field. */
14242 fp->type = die_type (die, cu);
14243
14244 SET_FIELD_BITPOS (*fp, 0);
14245
14246 /* Get bit size of field (zero if none). */
14247 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14248 if (attr != nullptr)
14249 {
14250 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14251 }
14252 else
14253 {
14254 FIELD_BITSIZE (*fp) = 0;
14255 }
14256
14257 /* Get bit offset of field. */
14258 if (handle_data_member_location (die, cu, &offset))
14259 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14260 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14261 if (attr != nullptr)
14262 {
14263 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14264 {
14265 /* For big endian bits, the DW_AT_bit_offset gives the
14266 additional bit offset from the MSB of the containing
14267 anonymous object to the MSB of the field. We don't
14268 have to do anything special since we don't need to
14269 know the size of the anonymous object. */
14270 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14271 }
14272 else
14273 {
14274 /* For little endian bits, compute the bit offset to the
14275 MSB of the anonymous object, subtract off the number of
14276 bits from the MSB of the field to the MSB of the
14277 object, and then subtract off the number of bits of
14278 the field itself. The result is the bit offset of
14279 the LSB of the field. */
14280 int anonymous_size;
14281 int bit_offset = DW_UNSND (attr);
14282
14283 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14284 if (attr != nullptr)
14285 {
14286 /* The size of the anonymous object containing
14287 the bit field is explicit, so use the
14288 indicated size (in bytes). */
14289 anonymous_size = DW_UNSND (attr);
14290 }
14291 else
14292 {
14293 /* The size of the anonymous object containing
14294 the bit field must be inferred from the type
14295 attribute of the data member containing the
14296 bit field. */
14297 anonymous_size = TYPE_LENGTH (fp->type);
14298 }
14299 SET_FIELD_BITPOS (*fp,
14300 (FIELD_BITPOS (*fp)
14301 + anonymous_size * bits_per_byte
14302 - bit_offset - FIELD_BITSIZE (*fp)));
14303 }
14304 }
14305 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14306 if (attr != NULL)
14307 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14308 + dwarf2_get_attr_constant_value (attr, 0)));
14309
14310 /* Get name of field. */
14311 fieldname = dwarf2_name (die, cu);
14312 if (fieldname == NULL)
14313 fieldname = "";
14314
14315 /* The name is already allocated along with this objfile, so we don't
14316 need to duplicate it for the type. */
14317 fp->name = fieldname;
14318
14319 /* Change accessibility for artificial fields (e.g. virtual table
14320 pointer or virtual base class pointer) to private. */
14321 if (dwarf2_attr (die, DW_AT_artificial, cu))
14322 {
14323 FIELD_ARTIFICIAL (*fp) = 1;
14324 new_field->accessibility = DW_ACCESS_private;
14325 fip->non_public_fields = 1;
14326 }
14327 }
14328 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14329 {
14330 /* C++ static member. */
14331
14332 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14333 is a declaration, but all versions of G++ as of this writing
14334 (so through at least 3.2.1) incorrectly generate
14335 DW_TAG_variable tags. */
14336
14337 const char *physname;
14338
14339 /* Get name of field. */
14340 fieldname = dwarf2_name (die, cu);
14341 if (fieldname == NULL)
14342 return;
14343
14344 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14345 if (attr
14346 /* Only create a symbol if this is an external value.
14347 new_symbol checks this and puts the value in the global symbol
14348 table, which we want. If it is not external, new_symbol
14349 will try to put the value in cu->list_in_scope which is wrong. */
14350 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14351 {
14352 /* A static const member, not much different than an enum as far as
14353 we're concerned, except that we can support more types. */
14354 new_symbol (die, NULL, cu);
14355 }
14356
14357 /* Get physical name. */
14358 physname = dwarf2_physname (fieldname, die, cu);
14359
14360 /* The name is already allocated along with this objfile, so we don't
14361 need to duplicate it for the type. */
14362 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14363 FIELD_TYPE (*fp) = die_type (die, cu);
14364 FIELD_NAME (*fp) = fieldname;
14365 }
14366 else if (die->tag == DW_TAG_inheritance)
14367 {
14368 LONGEST offset;
14369
14370 /* C++ base class field. */
14371 if (handle_data_member_location (die, cu, &offset))
14372 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14373 FIELD_BITSIZE (*fp) = 0;
14374 FIELD_TYPE (*fp) = die_type (die, cu);
14375 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14376 }
14377 else if (die->tag == DW_TAG_variant_part)
14378 {
14379 /* process_structure_scope will treat this DIE as a union. */
14380 process_structure_scope (die, cu);
14381
14382 /* The variant part is relative to the start of the enclosing
14383 structure. */
14384 SET_FIELD_BITPOS (*fp, 0);
14385 fp->type = get_die_type (die, cu);
14386 fp->artificial = 1;
14387 fp->name = "<<variant>>";
14388
14389 /* Normally a DW_TAG_variant_part won't have a size, but our
14390 representation requires one, so set it to the maximum of the
14391 child sizes, being sure to account for the offset at which
14392 each child is seen. */
14393 if (TYPE_LENGTH (fp->type) == 0)
14394 {
14395 unsigned max = 0;
14396 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14397 {
14398 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14399 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14400 if (len > max)
14401 max = len;
14402 }
14403 TYPE_LENGTH (fp->type) = max;
14404 }
14405 }
14406 else
14407 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14408 }
14409
14410 /* Can the type given by DIE define another type? */
14411
14412 static bool
14413 type_can_define_types (const struct die_info *die)
14414 {
14415 switch (die->tag)
14416 {
14417 case DW_TAG_typedef:
14418 case DW_TAG_class_type:
14419 case DW_TAG_structure_type:
14420 case DW_TAG_union_type:
14421 case DW_TAG_enumeration_type:
14422 return true;
14423
14424 default:
14425 return false;
14426 }
14427 }
14428
14429 /* Add a type definition defined in the scope of the FIP's class. */
14430
14431 static void
14432 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14433 struct dwarf2_cu *cu)
14434 {
14435 struct decl_field fp;
14436 memset (&fp, 0, sizeof (fp));
14437
14438 gdb_assert (type_can_define_types (die));
14439
14440 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14441 fp.name = dwarf2_name (die, cu);
14442 fp.type = read_type_die (die, cu);
14443
14444 /* Save accessibility. */
14445 enum dwarf_access_attribute accessibility;
14446 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14447 if (attr != NULL)
14448 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14449 else
14450 accessibility = dwarf2_default_access_attribute (die, cu);
14451 switch (accessibility)
14452 {
14453 case DW_ACCESS_public:
14454 /* The assumed value if neither private nor protected. */
14455 break;
14456 case DW_ACCESS_private:
14457 fp.is_private = 1;
14458 break;
14459 case DW_ACCESS_protected:
14460 fp.is_protected = 1;
14461 break;
14462 default:
14463 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14464 }
14465
14466 if (die->tag == DW_TAG_typedef)
14467 fip->typedef_field_list.push_back (fp);
14468 else
14469 fip->nested_types_list.push_back (fp);
14470 }
14471
14472 /* Create the vector of fields, and attach it to the type. */
14473
14474 static void
14475 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14476 struct dwarf2_cu *cu)
14477 {
14478 int nfields = fip->nfields ();
14479
14480 /* Record the field count, allocate space for the array of fields,
14481 and create blank accessibility bitfields if necessary. */
14482 TYPE_NFIELDS (type) = nfields;
14483 TYPE_FIELDS (type) = (struct field *)
14484 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14485
14486 if (fip->non_public_fields && cu->language != language_ada)
14487 {
14488 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14489
14490 TYPE_FIELD_PRIVATE_BITS (type) =
14491 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14492 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14493
14494 TYPE_FIELD_PROTECTED_BITS (type) =
14495 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14496 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14497
14498 TYPE_FIELD_IGNORE_BITS (type) =
14499 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14500 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14501 }
14502
14503 /* If the type has baseclasses, allocate and clear a bit vector for
14504 TYPE_FIELD_VIRTUAL_BITS. */
14505 if (!fip->baseclasses.empty () && cu->language != language_ada)
14506 {
14507 int num_bytes = B_BYTES (fip->baseclasses.size ());
14508 unsigned char *pointer;
14509
14510 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14511 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14512 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14513 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14514 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14515 }
14516
14517 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14518 {
14519 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14520
14521 for (int index = 0; index < nfields; ++index)
14522 {
14523 struct nextfield &field = fip->fields[index];
14524
14525 if (field.variant.is_discriminant)
14526 di->discriminant_index = index;
14527 else if (field.variant.default_branch)
14528 di->default_index = index;
14529 else
14530 di->discriminants[index] = field.variant.discriminant_value;
14531 }
14532 }
14533
14534 /* Copy the saved-up fields into the field vector. */
14535 for (int i = 0; i < nfields; ++i)
14536 {
14537 struct nextfield &field
14538 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14539 : fip->fields[i - fip->baseclasses.size ()]);
14540
14541 TYPE_FIELD (type, i) = field.field;
14542 switch (field.accessibility)
14543 {
14544 case DW_ACCESS_private:
14545 if (cu->language != language_ada)
14546 SET_TYPE_FIELD_PRIVATE (type, i);
14547 break;
14548
14549 case DW_ACCESS_protected:
14550 if (cu->language != language_ada)
14551 SET_TYPE_FIELD_PROTECTED (type, i);
14552 break;
14553
14554 case DW_ACCESS_public:
14555 break;
14556
14557 default:
14558 /* Unknown accessibility. Complain and treat it as public. */
14559 {
14560 complaint (_("unsupported accessibility %d"),
14561 field.accessibility);
14562 }
14563 break;
14564 }
14565 if (i < fip->baseclasses.size ())
14566 {
14567 switch (field.virtuality)
14568 {
14569 case DW_VIRTUALITY_virtual:
14570 case DW_VIRTUALITY_pure_virtual:
14571 if (cu->language == language_ada)
14572 error (_("unexpected virtuality in component of Ada type"));
14573 SET_TYPE_FIELD_VIRTUAL (type, i);
14574 break;
14575 }
14576 }
14577 }
14578 }
14579
14580 /* Return true if this member function is a constructor, false
14581 otherwise. */
14582
14583 static int
14584 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14585 {
14586 const char *fieldname;
14587 const char *type_name;
14588 int len;
14589
14590 if (die->parent == NULL)
14591 return 0;
14592
14593 if (die->parent->tag != DW_TAG_structure_type
14594 && die->parent->tag != DW_TAG_union_type
14595 && die->parent->tag != DW_TAG_class_type)
14596 return 0;
14597
14598 fieldname = dwarf2_name (die, cu);
14599 type_name = dwarf2_name (die->parent, cu);
14600 if (fieldname == NULL || type_name == NULL)
14601 return 0;
14602
14603 len = strlen (fieldname);
14604 return (strncmp (fieldname, type_name, len) == 0
14605 && (type_name[len] == '\0' || type_name[len] == '<'));
14606 }
14607
14608 /* Check if the given VALUE is a recognized enum
14609 dwarf_defaulted_attribute constant according to DWARF5 spec,
14610 Table 7.24. */
14611
14612 static bool
14613 is_valid_DW_AT_defaulted (ULONGEST value)
14614 {
14615 switch (value)
14616 {
14617 case DW_DEFAULTED_no:
14618 case DW_DEFAULTED_in_class:
14619 case DW_DEFAULTED_out_of_class:
14620 return true;
14621 }
14622
14623 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14624 return false;
14625 }
14626
14627 /* Add a member function to the proper fieldlist. */
14628
14629 static void
14630 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14631 struct type *type, struct dwarf2_cu *cu)
14632 {
14633 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14634 struct attribute *attr;
14635 int i;
14636 struct fnfieldlist *flp = nullptr;
14637 struct fn_field *fnp;
14638 const char *fieldname;
14639 struct type *this_type;
14640 enum dwarf_access_attribute accessibility;
14641
14642 if (cu->language == language_ada)
14643 error (_("unexpected member function in Ada type"));
14644
14645 /* Get name of member function. */
14646 fieldname = dwarf2_name (die, cu);
14647 if (fieldname == NULL)
14648 return;
14649
14650 /* Look up member function name in fieldlist. */
14651 for (i = 0; i < fip->fnfieldlists.size (); i++)
14652 {
14653 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14654 {
14655 flp = &fip->fnfieldlists[i];
14656 break;
14657 }
14658 }
14659
14660 /* Create a new fnfieldlist if necessary. */
14661 if (flp == nullptr)
14662 {
14663 fip->fnfieldlists.emplace_back ();
14664 flp = &fip->fnfieldlists.back ();
14665 flp->name = fieldname;
14666 i = fip->fnfieldlists.size () - 1;
14667 }
14668
14669 /* Create a new member function field and add it to the vector of
14670 fnfieldlists. */
14671 flp->fnfields.emplace_back ();
14672 fnp = &flp->fnfields.back ();
14673
14674 /* Delay processing of the physname until later. */
14675 if (cu->language == language_cplus)
14676 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14677 die, cu);
14678 else
14679 {
14680 const char *physname = dwarf2_physname (fieldname, die, cu);
14681 fnp->physname = physname ? physname : "";
14682 }
14683
14684 fnp->type = alloc_type (objfile);
14685 this_type = read_type_die (die, cu);
14686 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14687 {
14688 int nparams = TYPE_NFIELDS (this_type);
14689
14690 /* TYPE is the domain of this method, and THIS_TYPE is the type
14691 of the method itself (TYPE_CODE_METHOD). */
14692 smash_to_method_type (fnp->type, type,
14693 TYPE_TARGET_TYPE (this_type),
14694 TYPE_FIELDS (this_type),
14695 TYPE_NFIELDS (this_type),
14696 TYPE_VARARGS (this_type));
14697
14698 /* Handle static member functions.
14699 Dwarf2 has no clean way to discern C++ static and non-static
14700 member functions. G++ helps GDB by marking the first
14701 parameter for non-static member functions (which is the this
14702 pointer) as artificial. We obtain this information from
14703 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14704 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14705 fnp->voffset = VOFFSET_STATIC;
14706 }
14707 else
14708 complaint (_("member function type missing for '%s'"),
14709 dwarf2_full_name (fieldname, die, cu));
14710
14711 /* Get fcontext from DW_AT_containing_type if present. */
14712 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14713 fnp->fcontext = die_containing_type (die, cu);
14714
14715 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14716 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14717
14718 /* Get accessibility. */
14719 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14720 if (attr != nullptr)
14721 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14722 else
14723 accessibility = dwarf2_default_access_attribute (die, cu);
14724 switch (accessibility)
14725 {
14726 case DW_ACCESS_private:
14727 fnp->is_private = 1;
14728 break;
14729 case DW_ACCESS_protected:
14730 fnp->is_protected = 1;
14731 break;
14732 }
14733
14734 /* Check for artificial methods. */
14735 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14736 if (attr && DW_UNSND (attr) != 0)
14737 fnp->is_artificial = 1;
14738
14739 /* Check for defaulted methods. */
14740 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14741 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14742 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14743
14744 /* Check for deleted methods. */
14745 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14746 if (attr != nullptr && DW_UNSND (attr) != 0)
14747 fnp->is_deleted = 1;
14748
14749 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14750
14751 /* Get index in virtual function table if it is a virtual member
14752 function. For older versions of GCC, this is an offset in the
14753 appropriate virtual table, as specified by DW_AT_containing_type.
14754 For everyone else, it is an expression to be evaluated relative
14755 to the object address. */
14756
14757 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14758 if (attr != nullptr)
14759 {
14760 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14761 {
14762 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14763 {
14764 /* Old-style GCC. */
14765 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14766 }
14767 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14768 || (DW_BLOCK (attr)->size > 1
14769 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14770 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14771 {
14772 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14773 if ((fnp->voffset % cu->header.addr_size) != 0)
14774 dwarf2_complex_location_expr_complaint ();
14775 else
14776 fnp->voffset /= cu->header.addr_size;
14777 fnp->voffset += 2;
14778 }
14779 else
14780 dwarf2_complex_location_expr_complaint ();
14781
14782 if (!fnp->fcontext)
14783 {
14784 /* If there is no `this' field and no DW_AT_containing_type,
14785 we cannot actually find a base class context for the
14786 vtable! */
14787 if (TYPE_NFIELDS (this_type) == 0
14788 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14789 {
14790 complaint (_("cannot determine context for virtual member "
14791 "function \"%s\" (offset %s)"),
14792 fieldname, sect_offset_str (die->sect_off));
14793 }
14794 else
14795 {
14796 fnp->fcontext
14797 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14798 }
14799 }
14800 }
14801 else if (attr->form_is_section_offset ())
14802 {
14803 dwarf2_complex_location_expr_complaint ();
14804 }
14805 else
14806 {
14807 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14808 fieldname);
14809 }
14810 }
14811 else
14812 {
14813 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14814 if (attr && DW_UNSND (attr))
14815 {
14816 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14817 complaint (_("Member function \"%s\" (offset %s) is virtual "
14818 "but the vtable offset is not specified"),
14819 fieldname, sect_offset_str (die->sect_off));
14820 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14821 TYPE_CPLUS_DYNAMIC (type) = 1;
14822 }
14823 }
14824 }
14825
14826 /* Create the vector of member function fields, and attach it to the type. */
14827
14828 static void
14829 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14830 struct dwarf2_cu *cu)
14831 {
14832 if (cu->language == language_ada)
14833 error (_("unexpected member functions in Ada type"));
14834
14835 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14836 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14837 TYPE_ALLOC (type,
14838 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14839
14840 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14841 {
14842 struct fnfieldlist &nf = fip->fnfieldlists[i];
14843 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14844
14845 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14846 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14847 fn_flp->fn_fields = (struct fn_field *)
14848 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14849
14850 for (int k = 0; k < nf.fnfields.size (); ++k)
14851 fn_flp->fn_fields[k] = nf.fnfields[k];
14852 }
14853
14854 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14855 }
14856
14857 /* Returns non-zero if NAME is the name of a vtable member in CU's
14858 language, zero otherwise. */
14859 static int
14860 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14861 {
14862 static const char vptr[] = "_vptr";
14863
14864 /* Look for the C++ form of the vtable. */
14865 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14866 return 1;
14867
14868 return 0;
14869 }
14870
14871 /* GCC outputs unnamed structures that are really pointers to member
14872 functions, with the ABI-specified layout. If TYPE describes
14873 such a structure, smash it into a member function type.
14874
14875 GCC shouldn't do this; it should just output pointer to member DIEs.
14876 This is GCC PR debug/28767. */
14877
14878 static void
14879 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14880 {
14881 struct type *pfn_type, *self_type, *new_type;
14882
14883 /* Check for a structure with no name and two children. */
14884 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14885 return;
14886
14887 /* Check for __pfn and __delta members. */
14888 if (TYPE_FIELD_NAME (type, 0) == NULL
14889 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14890 || TYPE_FIELD_NAME (type, 1) == NULL
14891 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14892 return;
14893
14894 /* Find the type of the method. */
14895 pfn_type = TYPE_FIELD_TYPE (type, 0);
14896 if (pfn_type == NULL
14897 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14898 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14899 return;
14900
14901 /* Look for the "this" argument. */
14902 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14903 if (TYPE_NFIELDS (pfn_type) == 0
14904 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14905 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14906 return;
14907
14908 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14909 new_type = alloc_type (objfile);
14910 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14911 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14912 TYPE_VARARGS (pfn_type));
14913 smash_to_methodptr_type (type, new_type);
14914 }
14915
14916 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14917 appropriate error checking and issuing complaints if there is a
14918 problem. */
14919
14920 static ULONGEST
14921 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14922 {
14923 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14924
14925 if (attr == nullptr)
14926 return 0;
14927
14928 if (!attr->form_is_constant ())
14929 {
14930 complaint (_("DW_AT_alignment must have constant form"
14931 " - DIE at %s [in module %s]"),
14932 sect_offset_str (die->sect_off),
14933 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14934 return 0;
14935 }
14936
14937 ULONGEST align;
14938 if (attr->form == DW_FORM_sdata)
14939 {
14940 LONGEST val = DW_SND (attr);
14941 if (val < 0)
14942 {
14943 complaint (_("DW_AT_alignment value must not be negative"
14944 " - DIE at %s [in module %s]"),
14945 sect_offset_str (die->sect_off),
14946 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14947 return 0;
14948 }
14949 align = val;
14950 }
14951 else
14952 align = DW_UNSND (attr);
14953
14954 if (align == 0)
14955 {
14956 complaint (_("DW_AT_alignment value must not be zero"
14957 " - DIE at %s [in module %s]"),
14958 sect_offset_str (die->sect_off),
14959 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14960 return 0;
14961 }
14962 if ((align & (align - 1)) != 0)
14963 {
14964 complaint (_("DW_AT_alignment value must be a power of 2"
14965 " - DIE at %s [in module %s]"),
14966 sect_offset_str (die->sect_off),
14967 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14968 return 0;
14969 }
14970
14971 return align;
14972 }
14973
14974 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14975 the alignment for TYPE. */
14976
14977 static void
14978 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14979 struct type *type)
14980 {
14981 if (!set_type_align (type, get_alignment (cu, die)))
14982 complaint (_("DW_AT_alignment value too large"
14983 " - DIE at %s [in module %s]"),
14984 sect_offset_str (die->sect_off),
14985 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14986 }
14987
14988 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14989 constant for a type, according to DWARF5 spec, Table 5.5. */
14990
14991 static bool
14992 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14993 {
14994 switch (value)
14995 {
14996 case DW_CC_normal:
14997 case DW_CC_pass_by_reference:
14998 case DW_CC_pass_by_value:
14999 return true;
15000
15001 default:
15002 complaint (_("unrecognized DW_AT_calling_convention value "
15003 "(%s) for a type"), pulongest (value));
15004 return false;
15005 }
15006 }
15007
15008 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15009 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15010 also according to GNU-specific values (see include/dwarf2.h). */
15011
15012 static bool
15013 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15014 {
15015 switch (value)
15016 {
15017 case DW_CC_normal:
15018 case DW_CC_program:
15019 case DW_CC_nocall:
15020 return true;
15021
15022 case DW_CC_GNU_renesas_sh:
15023 case DW_CC_GNU_borland_fastcall_i386:
15024 case DW_CC_GDB_IBM_OpenCL:
15025 return true;
15026
15027 default:
15028 complaint (_("unrecognized DW_AT_calling_convention value "
15029 "(%s) for a subroutine"), pulongest (value));
15030 return false;
15031 }
15032 }
15033
15034 /* Called when we find the DIE that starts a structure or union scope
15035 (definition) to create a type for the structure or union. Fill in
15036 the type's name and general properties; the members will not be
15037 processed until process_structure_scope. A symbol table entry for
15038 the type will also not be done until process_structure_scope (assuming
15039 the type has a name).
15040
15041 NOTE: we need to call these functions regardless of whether or not the
15042 DIE has a DW_AT_name attribute, since it might be an anonymous
15043 structure or union. This gets the type entered into our set of
15044 user defined types. */
15045
15046 static struct type *
15047 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15048 {
15049 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15050 struct type *type;
15051 struct attribute *attr;
15052 const char *name;
15053
15054 /* If the definition of this type lives in .debug_types, read that type.
15055 Don't follow DW_AT_specification though, that will take us back up
15056 the chain and we want to go down. */
15057 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15058 if (attr != nullptr)
15059 {
15060 type = get_DW_AT_signature_type (die, attr, cu);
15061
15062 /* The type's CU may not be the same as CU.
15063 Ensure TYPE is recorded with CU in die_type_hash. */
15064 return set_die_type (die, type, cu);
15065 }
15066
15067 type = alloc_type (objfile);
15068 INIT_CPLUS_SPECIFIC (type);
15069
15070 name = dwarf2_name (die, cu);
15071 if (name != NULL)
15072 {
15073 if (cu->language == language_cplus
15074 || cu->language == language_d
15075 || cu->language == language_rust)
15076 {
15077 const char *full_name = dwarf2_full_name (name, die, cu);
15078
15079 /* dwarf2_full_name might have already finished building the DIE's
15080 type. If so, there is no need to continue. */
15081 if (get_die_type (die, cu) != NULL)
15082 return get_die_type (die, cu);
15083
15084 TYPE_NAME (type) = full_name;
15085 }
15086 else
15087 {
15088 /* The name is already allocated along with this objfile, so
15089 we don't need to duplicate it for the type. */
15090 TYPE_NAME (type) = name;
15091 }
15092 }
15093
15094 if (die->tag == DW_TAG_structure_type)
15095 {
15096 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15097 }
15098 else if (die->tag == DW_TAG_union_type)
15099 {
15100 TYPE_CODE (type) = TYPE_CODE_UNION;
15101 }
15102 else if (die->tag == DW_TAG_variant_part)
15103 {
15104 TYPE_CODE (type) = TYPE_CODE_UNION;
15105 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15106 }
15107 else
15108 {
15109 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15110 }
15111
15112 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15113 TYPE_DECLARED_CLASS (type) = 1;
15114
15115 /* Store the calling convention in the type if it's available in
15116 the die. Otherwise the calling convention remains set to
15117 the default value DW_CC_normal. */
15118 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15119 if (attr != nullptr
15120 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15121 {
15122 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15123 TYPE_CPLUS_CALLING_CONVENTION (type)
15124 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15125 }
15126
15127 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15128 if (attr != nullptr)
15129 {
15130 if (attr->form_is_constant ())
15131 TYPE_LENGTH (type) = DW_UNSND (attr);
15132 else
15133 {
15134 /* For the moment, dynamic type sizes are not supported
15135 by GDB's struct type. The actual size is determined
15136 on-demand when resolving the type of a given object,
15137 so set the type's length to zero for now. Otherwise,
15138 we record an expression as the length, and that expression
15139 could lead to a very large value, which could eventually
15140 lead to us trying to allocate that much memory when creating
15141 a value of that type. */
15142 TYPE_LENGTH (type) = 0;
15143 }
15144 }
15145 else
15146 {
15147 TYPE_LENGTH (type) = 0;
15148 }
15149
15150 maybe_set_alignment (cu, die, type);
15151
15152 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15153 {
15154 /* ICC<14 does not output the required DW_AT_declaration on
15155 incomplete types, but gives them a size of zero. */
15156 TYPE_STUB (type) = 1;
15157 }
15158 else
15159 TYPE_STUB_SUPPORTED (type) = 1;
15160
15161 if (die_is_declaration (die, cu))
15162 TYPE_STUB (type) = 1;
15163 else if (attr == NULL && die->child == NULL
15164 && producer_is_realview (cu->producer))
15165 /* RealView does not output the required DW_AT_declaration
15166 on incomplete types. */
15167 TYPE_STUB (type) = 1;
15168
15169 /* We need to add the type field to the die immediately so we don't
15170 infinitely recurse when dealing with pointers to the structure
15171 type within the structure itself. */
15172 set_die_type (die, type, cu);
15173
15174 /* set_die_type should be already done. */
15175 set_descriptive_type (type, die, cu);
15176
15177 return type;
15178 }
15179
15180 /* A helper for process_structure_scope that handles a single member
15181 DIE. */
15182
15183 static void
15184 handle_struct_member_die (struct die_info *child_die, struct type *type,
15185 struct field_info *fi,
15186 std::vector<struct symbol *> *template_args,
15187 struct dwarf2_cu *cu)
15188 {
15189 if (child_die->tag == DW_TAG_member
15190 || child_die->tag == DW_TAG_variable
15191 || child_die->tag == DW_TAG_variant_part)
15192 {
15193 /* NOTE: carlton/2002-11-05: A C++ static data member
15194 should be a DW_TAG_member that is a declaration, but
15195 all versions of G++ as of this writing (so through at
15196 least 3.2.1) incorrectly generate DW_TAG_variable
15197 tags for them instead. */
15198 dwarf2_add_field (fi, child_die, cu);
15199 }
15200 else if (child_die->tag == DW_TAG_subprogram)
15201 {
15202 /* Rust doesn't have member functions in the C++ sense.
15203 However, it does emit ordinary functions as children
15204 of a struct DIE. */
15205 if (cu->language == language_rust)
15206 read_func_scope (child_die, cu);
15207 else
15208 {
15209 /* C++ member function. */
15210 dwarf2_add_member_fn (fi, child_die, type, cu);
15211 }
15212 }
15213 else if (child_die->tag == DW_TAG_inheritance)
15214 {
15215 /* C++ base class field. */
15216 dwarf2_add_field (fi, child_die, cu);
15217 }
15218 else if (type_can_define_types (child_die))
15219 dwarf2_add_type_defn (fi, child_die, cu);
15220 else if (child_die->tag == DW_TAG_template_type_param
15221 || child_die->tag == DW_TAG_template_value_param)
15222 {
15223 struct symbol *arg = new_symbol (child_die, NULL, cu);
15224
15225 if (arg != NULL)
15226 template_args->push_back (arg);
15227 }
15228 else if (child_die->tag == DW_TAG_variant)
15229 {
15230 /* In a variant we want to get the discriminant and also add a
15231 field for our sole member child. */
15232 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15233
15234 for (die_info *variant_child = child_die->child;
15235 variant_child != NULL;
15236 variant_child = sibling_die (variant_child))
15237 {
15238 if (variant_child->tag == DW_TAG_member)
15239 {
15240 handle_struct_member_die (variant_child, type, fi,
15241 template_args, cu);
15242 /* Only handle the one. */
15243 break;
15244 }
15245 }
15246
15247 /* We don't handle this but we might as well report it if we see
15248 it. */
15249 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15250 complaint (_("DW_AT_discr_list is not supported yet"
15251 " - DIE at %s [in module %s]"),
15252 sect_offset_str (child_die->sect_off),
15253 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15254
15255 /* The first field was just added, so we can stash the
15256 discriminant there. */
15257 gdb_assert (!fi->fields.empty ());
15258 if (discr == NULL)
15259 fi->fields.back ().variant.default_branch = true;
15260 else
15261 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15262 }
15263 }
15264
15265 /* Finish creating a structure or union type, including filling in
15266 its members and creating a symbol for it. */
15267
15268 static void
15269 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15270 {
15271 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15272 struct die_info *child_die;
15273 struct type *type;
15274
15275 type = get_die_type (die, cu);
15276 if (type == NULL)
15277 type = read_structure_type (die, cu);
15278
15279 /* When reading a DW_TAG_variant_part, we need to notice when we
15280 read the discriminant member, so we can record it later in the
15281 discriminant_info. */
15282 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15283 sect_offset discr_offset {};
15284 bool has_template_parameters = false;
15285
15286 if (is_variant_part)
15287 {
15288 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15289 if (discr == NULL)
15290 {
15291 /* Maybe it's a univariant form, an extension we support.
15292 In this case arrange not to check the offset. */
15293 is_variant_part = false;
15294 }
15295 else if (discr->form_is_ref ())
15296 {
15297 struct dwarf2_cu *target_cu = cu;
15298 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15299
15300 discr_offset = target_die->sect_off;
15301 }
15302 else
15303 {
15304 complaint (_("DW_AT_discr does not have DIE reference form"
15305 " - DIE at %s [in module %s]"),
15306 sect_offset_str (die->sect_off),
15307 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15308 is_variant_part = false;
15309 }
15310 }
15311
15312 if (die->child != NULL && ! die_is_declaration (die, cu))
15313 {
15314 struct field_info fi;
15315 std::vector<struct symbol *> template_args;
15316
15317 child_die = die->child;
15318
15319 while (child_die && child_die->tag)
15320 {
15321 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15322
15323 if (is_variant_part && discr_offset == child_die->sect_off)
15324 fi.fields.back ().variant.is_discriminant = true;
15325
15326 child_die = sibling_die (child_die);
15327 }
15328
15329 /* Attach template arguments to type. */
15330 if (!template_args.empty ())
15331 {
15332 has_template_parameters = true;
15333 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15334 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15335 TYPE_TEMPLATE_ARGUMENTS (type)
15336 = XOBNEWVEC (&objfile->objfile_obstack,
15337 struct symbol *,
15338 TYPE_N_TEMPLATE_ARGUMENTS (type));
15339 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15340 template_args.data (),
15341 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15342 * sizeof (struct symbol *)));
15343 }
15344
15345 /* Attach fields and member functions to the type. */
15346 if (fi.nfields () > 0)
15347 dwarf2_attach_fields_to_type (&fi, type, cu);
15348 if (!fi.fnfieldlists.empty ())
15349 {
15350 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15351
15352 /* Get the type which refers to the base class (possibly this
15353 class itself) which contains the vtable pointer for the current
15354 class from the DW_AT_containing_type attribute. This use of
15355 DW_AT_containing_type is a GNU extension. */
15356
15357 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15358 {
15359 struct type *t = die_containing_type (die, cu);
15360
15361 set_type_vptr_basetype (type, t);
15362 if (type == t)
15363 {
15364 int i;
15365
15366 /* Our own class provides vtbl ptr. */
15367 for (i = TYPE_NFIELDS (t) - 1;
15368 i >= TYPE_N_BASECLASSES (t);
15369 --i)
15370 {
15371 const char *fieldname = TYPE_FIELD_NAME (t, i);
15372
15373 if (is_vtable_name (fieldname, cu))
15374 {
15375 set_type_vptr_fieldno (type, i);
15376 break;
15377 }
15378 }
15379
15380 /* Complain if virtual function table field not found. */
15381 if (i < TYPE_N_BASECLASSES (t))
15382 complaint (_("virtual function table pointer "
15383 "not found when defining class '%s'"),
15384 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15385 }
15386 else
15387 {
15388 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15389 }
15390 }
15391 else if (cu->producer
15392 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15393 {
15394 /* The IBM XLC compiler does not provide direct indication
15395 of the containing type, but the vtable pointer is
15396 always named __vfp. */
15397
15398 int i;
15399
15400 for (i = TYPE_NFIELDS (type) - 1;
15401 i >= TYPE_N_BASECLASSES (type);
15402 --i)
15403 {
15404 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15405 {
15406 set_type_vptr_fieldno (type, i);
15407 set_type_vptr_basetype (type, type);
15408 break;
15409 }
15410 }
15411 }
15412 }
15413
15414 /* Copy fi.typedef_field_list linked list elements content into the
15415 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15416 if (!fi.typedef_field_list.empty ())
15417 {
15418 int count = fi.typedef_field_list.size ();
15419
15420 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15421 TYPE_TYPEDEF_FIELD_ARRAY (type)
15422 = ((struct decl_field *)
15423 TYPE_ALLOC (type,
15424 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15425 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15426
15427 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15428 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15429 }
15430
15431 /* Copy fi.nested_types_list linked list elements content into the
15432 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15433 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15434 {
15435 int count = fi.nested_types_list.size ();
15436
15437 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15438 TYPE_NESTED_TYPES_ARRAY (type)
15439 = ((struct decl_field *)
15440 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15441 TYPE_NESTED_TYPES_COUNT (type) = count;
15442
15443 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15444 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15445 }
15446 }
15447
15448 quirk_gcc_member_function_pointer (type, objfile);
15449 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15450 cu->rust_unions.push_back (type);
15451
15452 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15453 snapshots) has been known to create a die giving a declaration
15454 for a class that has, as a child, a die giving a definition for a
15455 nested class. So we have to process our children even if the
15456 current die is a declaration. Normally, of course, a declaration
15457 won't have any children at all. */
15458
15459 child_die = die->child;
15460
15461 while (child_die != NULL && child_die->tag)
15462 {
15463 if (child_die->tag == DW_TAG_member
15464 || child_die->tag == DW_TAG_variable
15465 || child_die->tag == DW_TAG_inheritance
15466 || child_die->tag == DW_TAG_template_value_param
15467 || child_die->tag == DW_TAG_template_type_param)
15468 {
15469 /* Do nothing. */
15470 }
15471 else
15472 process_die (child_die, cu);
15473
15474 child_die = sibling_die (child_die);
15475 }
15476
15477 /* Do not consider external references. According to the DWARF standard,
15478 these DIEs are identified by the fact that they have no byte_size
15479 attribute, and a declaration attribute. */
15480 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15481 || !die_is_declaration (die, cu))
15482 {
15483 struct symbol *sym = new_symbol (die, type, cu);
15484
15485 if (has_template_parameters)
15486 {
15487 struct symtab *symtab;
15488 if (sym != nullptr)
15489 symtab = symbol_symtab (sym);
15490 else if (cu->line_header != nullptr)
15491 {
15492 /* Any related symtab will do. */
15493 symtab
15494 = cu->line_header->file_names ()[0].symtab;
15495 }
15496 else
15497 {
15498 symtab = nullptr;
15499 complaint (_("could not find suitable "
15500 "symtab for template parameter"
15501 " - DIE at %s [in module %s]"),
15502 sect_offset_str (die->sect_off),
15503 objfile_name (objfile));
15504 }
15505
15506 if (symtab != nullptr)
15507 {
15508 /* Make sure that the symtab is set on the new symbols.
15509 Even though they don't appear in this symtab directly,
15510 other parts of gdb assume that symbols do, and this is
15511 reasonably true. */
15512 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15513 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15514 }
15515 }
15516 }
15517 }
15518
15519 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15520 update TYPE using some information only available in DIE's children. */
15521
15522 static void
15523 update_enumeration_type_from_children (struct die_info *die,
15524 struct type *type,
15525 struct dwarf2_cu *cu)
15526 {
15527 struct die_info *child_die;
15528 int unsigned_enum = 1;
15529 int flag_enum = 1;
15530
15531 auto_obstack obstack;
15532
15533 for (child_die = die->child;
15534 child_die != NULL && child_die->tag;
15535 child_die = sibling_die (child_die))
15536 {
15537 struct attribute *attr;
15538 LONGEST value;
15539 const gdb_byte *bytes;
15540 struct dwarf2_locexpr_baton *baton;
15541 const char *name;
15542
15543 if (child_die->tag != DW_TAG_enumerator)
15544 continue;
15545
15546 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15547 if (attr == NULL)
15548 continue;
15549
15550 name = dwarf2_name (child_die, cu);
15551 if (name == NULL)
15552 name = "<anonymous enumerator>";
15553
15554 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15555 &value, &bytes, &baton);
15556 if (value < 0)
15557 {
15558 unsigned_enum = 0;
15559 flag_enum = 0;
15560 }
15561 else
15562 {
15563 if (count_one_bits_ll (value) >= 2)
15564 flag_enum = 0;
15565 }
15566
15567 /* If we already know that the enum type is neither unsigned, nor
15568 a flag type, no need to look at the rest of the enumerates. */
15569 if (!unsigned_enum && !flag_enum)
15570 break;
15571 }
15572
15573 if (unsigned_enum)
15574 TYPE_UNSIGNED (type) = 1;
15575 if (flag_enum)
15576 TYPE_FLAG_ENUM (type) = 1;
15577 }
15578
15579 /* Given a DW_AT_enumeration_type die, set its type. We do not
15580 complete the type's fields yet, or create any symbols. */
15581
15582 static struct type *
15583 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15584 {
15585 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15586 struct type *type;
15587 struct attribute *attr;
15588 const char *name;
15589
15590 /* If the definition of this type lives in .debug_types, read that type.
15591 Don't follow DW_AT_specification though, that will take us back up
15592 the chain and we want to go down. */
15593 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15594 if (attr != nullptr)
15595 {
15596 type = get_DW_AT_signature_type (die, attr, cu);
15597
15598 /* The type's CU may not be the same as CU.
15599 Ensure TYPE is recorded with CU in die_type_hash. */
15600 return set_die_type (die, type, cu);
15601 }
15602
15603 type = alloc_type (objfile);
15604
15605 TYPE_CODE (type) = TYPE_CODE_ENUM;
15606 name = dwarf2_full_name (NULL, die, cu);
15607 if (name != NULL)
15608 TYPE_NAME (type) = name;
15609
15610 attr = dwarf2_attr (die, DW_AT_type, cu);
15611 if (attr != NULL)
15612 {
15613 struct type *underlying_type = die_type (die, cu);
15614
15615 TYPE_TARGET_TYPE (type) = underlying_type;
15616 }
15617
15618 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15619 if (attr != nullptr)
15620 {
15621 TYPE_LENGTH (type) = DW_UNSND (attr);
15622 }
15623 else
15624 {
15625 TYPE_LENGTH (type) = 0;
15626 }
15627
15628 maybe_set_alignment (cu, die, type);
15629
15630 /* The enumeration DIE can be incomplete. In Ada, any type can be
15631 declared as private in the package spec, and then defined only
15632 inside the package body. Such types are known as Taft Amendment
15633 Types. When another package uses such a type, an incomplete DIE
15634 may be generated by the compiler. */
15635 if (die_is_declaration (die, cu))
15636 TYPE_STUB (type) = 1;
15637
15638 /* Finish the creation of this type by using the enum's children.
15639 We must call this even when the underlying type has been provided
15640 so that we can determine if we're looking at a "flag" enum. */
15641 update_enumeration_type_from_children (die, type, cu);
15642
15643 /* If this type has an underlying type that is not a stub, then we
15644 may use its attributes. We always use the "unsigned" attribute
15645 in this situation, because ordinarily we guess whether the type
15646 is unsigned -- but the guess can be wrong and the underlying type
15647 can tell us the reality. However, we defer to a local size
15648 attribute if one exists, because this lets the compiler override
15649 the underlying type if needed. */
15650 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15651 {
15652 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15653 if (TYPE_LENGTH (type) == 0)
15654 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15655 if (TYPE_RAW_ALIGN (type) == 0
15656 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15657 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15658 }
15659
15660 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15661
15662 return set_die_type (die, type, cu);
15663 }
15664
15665 /* Given a pointer to a die which begins an enumeration, process all
15666 the dies that define the members of the enumeration, and create the
15667 symbol for the enumeration type.
15668
15669 NOTE: We reverse the order of the element list. */
15670
15671 static void
15672 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15673 {
15674 struct type *this_type;
15675
15676 this_type = get_die_type (die, cu);
15677 if (this_type == NULL)
15678 this_type = read_enumeration_type (die, cu);
15679
15680 if (die->child != NULL)
15681 {
15682 struct die_info *child_die;
15683 struct symbol *sym;
15684 std::vector<struct field> fields;
15685 const char *name;
15686
15687 child_die = die->child;
15688 while (child_die && child_die->tag)
15689 {
15690 if (child_die->tag != DW_TAG_enumerator)
15691 {
15692 process_die (child_die, cu);
15693 }
15694 else
15695 {
15696 name = dwarf2_name (child_die, cu);
15697 if (name)
15698 {
15699 sym = new_symbol (child_die, this_type, cu);
15700
15701 fields.emplace_back ();
15702 struct field &field = fields.back ();
15703
15704 FIELD_NAME (field) = sym->linkage_name ();
15705 FIELD_TYPE (field) = NULL;
15706 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15707 FIELD_BITSIZE (field) = 0;
15708 }
15709 }
15710
15711 child_die = sibling_die (child_die);
15712 }
15713
15714 if (!fields.empty ())
15715 {
15716 TYPE_NFIELDS (this_type) = fields.size ();
15717 TYPE_FIELDS (this_type) = (struct field *)
15718 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15719 memcpy (TYPE_FIELDS (this_type), fields.data (),
15720 sizeof (struct field) * fields.size ());
15721 }
15722 }
15723
15724 /* If we are reading an enum from a .debug_types unit, and the enum
15725 is a declaration, and the enum is not the signatured type in the
15726 unit, then we do not want to add a symbol for it. Adding a
15727 symbol would in some cases obscure the true definition of the
15728 enum, giving users an incomplete type when the definition is
15729 actually available. Note that we do not want to do this for all
15730 enums which are just declarations, because C++0x allows forward
15731 enum declarations. */
15732 if (cu->per_cu->is_debug_types
15733 && die_is_declaration (die, cu))
15734 {
15735 struct signatured_type *sig_type;
15736
15737 sig_type = (struct signatured_type *) cu->per_cu;
15738 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15739 if (sig_type->type_offset_in_section != die->sect_off)
15740 return;
15741 }
15742
15743 new_symbol (die, this_type, cu);
15744 }
15745
15746 /* Extract all information from a DW_TAG_array_type DIE and put it in
15747 the DIE's type field. For now, this only handles one dimensional
15748 arrays. */
15749
15750 static struct type *
15751 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15752 {
15753 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15754 struct die_info *child_die;
15755 struct type *type;
15756 struct type *element_type, *range_type, *index_type;
15757 struct attribute *attr;
15758 const char *name;
15759 struct dynamic_prop *byte_stride_prop = NULL;
15760 unsigned int bit_stride = 0;
15761
15762 element_type = die_type (die, cu);
15763
15764 /* The die_type call above may have already set the type for this DIE. */
15765 type = get_die_type (die, cu);
15766 if (type)
15767 return type;
15768
15769 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15770 if (attr != NULL)
15771 {
15772 int stride_ok;
15773 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15774
15775 byte_stride_prop
15776 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15777 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15778 prop_type);
15779 if (!stride_ok)
15780 {
15781 complaint (_("unable to read array DW_AT_byte_stride "
15782 " - DIE at %s [in module %s]"),
15783 sect_offset_str (die->sect_off),
15784 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15785 /* Ignore this attribute. We will likely not be able to print
15786 arrays of this type correctly, but there is little we can do
15787 to help if we cannot read the attribute's value. */
15788 byte_stride_prop = NULL;
15789 }
15790 }
15791
15792 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15793 if (attr != NULL)
15794 bit_stride = DW_UNSND (attr);
15795
15796 /* Irix 6.2 native cc creates array types without children for
15797 arrays with unspecified length. */
15798 if (die->child == NULL)
15799 {
15800 index_type = objfile_type (objfile)->builtin_int;
15801 range_type = create_static_range_type (NULL, index_type, 0, -1);
15802 type = create_array_type_with_stride (NULL, element_type, range_type,
15803 byte_stride_prop, bit_stride);
15804 return set_die_type (die, type, cu);
15805 }
15806
15807 std::vector<struct type *> range_types;
15808 child_die = die->child;
15809 while (child_die && child_die->tag)
15810 {
15811 if (child_die->tag == DW_TAG_subrange_type)
15812 {
15813 struct type *child_type = read_type_die (child_die, cu);
15814
15815 if (child_type != NULL)
15816 {
15817 /* The range type was succesfully read. Save it for the
15818 array type creation. */
15819 range_types.push_back (child_type);
15820 }
15821 }
15822 child_die = sibling_die (child_die);
15823 }
15824
15825 /* Dwarf2 dimensions are output from left to right, create the
15826 necessary array types in backwards order. */
15827
15828 type = element_type;
15829
15830 if (read_array_order (die, cu) == DW_ORD_col_major)
15831 {
15832 int i = 0;
15833
15834 while (i < range_types.size ())
15835 type = create_array_type_with_stride (NULL, type, range_types[i++],
15836 byte_stride_prop, bit_stride);
15837 }
15838 else
15839 {
15840 size_t ndim = range_types.size ();
15841 while (ndim-- > 0)
15842 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15843 byte_stride_prop, bit_stride);
15844 }
15845
15846 /* Understand Dwarf2 support for vector types (like they occur on
15847 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15848 array type. This is not part of the Dwarf2/3 standard yet, but a
15849 custom vendor extension. The main difference between a regular
15850 array and the vector variant is that vectors are passed by value
15851 to functions. */
15852 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15853 if (attr != nullptr)
15854 make_vector_type (type);
15855
15856 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15857 implementation may choose to implement triple vectors using this
15858 attribute. */
15859 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15860 if (attr != nullptr)
15861 {
15862 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15863 TYPE_LENGTH (type) = DW_UNSND (attr);
15864 else
15865 complaint (_("DW_AT_byte_size for array type smaller "
15866 "than the total size of elements"));
15867 }
15868
15869 name = dwarf2_name (die, cu);
15870 if (name)
15871 TYPE_NAME (type) = name;
15872
15873 maybe_set_alignment (cu, die, type);
15874
15875 /* Install the type in the die. */
15876 set_die_type (die, type, cu);
15877
15878 /* set_die_type should be already done. */
15879 set_descriptive_type (type, die, cu);
15880
15881 return type;
15882 }
15883
15884 static enum dwarf_array_dim_ordering
15885 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15886 {
15887 struct attribute *attr;
15888
15889 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15890
15891 if (attr != nullptr)
15892 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15893
15894 /* GNU F77 is a special case, as at 08/2004 array type info is the
15895 opposite order to the dwarf2 specification, but data is still
15896 laid out as per normal fortran.
15897
15898 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15899 version checking. */
15900
15901 if (cu->language == language_fortran
15902 && cu->producer && strstr (cu->producer, "GNU F77"))
15903 {
15904 return DW_ORD_row_major;
15905 }
15906
15907 switch (cu->language_defn->la_array_ordering)
15908 {
15909 case array_column_major:
15910 return DW_ORD_col_major;
15911 case array_row_major:
15912 default:
15913 return DW_ORD_row_major;
15914 };
15915 }
15916
15917 /* Extract all information from a DW_TAG_set_type DIE and put it in
15918 the DIE's type field. */
15919
15920 static struct type *
15921 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15922 {
15923 struct type *domain_type, *set_type;
15924 struct attribute *attr;
15925
15926 domain_type = die_type (die, cu);
15927
15928 /* The die_type call above may have already set the type for this DIE. */
15929 set_type = get_die_type (die, cu);
15930 if (set_type)
15931 return set_type;
15932
15933 set_type = create_set_type (NULL, domain_type);
15934
15935 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15936 if (attr != nullptr)
15937 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15938
15939 maybe_set_alignment (cu, die, set_type);
15940
15941 return set_die_type (die, set_type, cu);
15942 }
15943
15944 /* A helper for read_common_block that creates a locexpr baton.
15945 SYM is the symbol which we are marking as computed.
15946 COMMON_DIE is the DIE for the common block.
15947 COMMON_LOC is the location expression attribute for the common
15948 block itself.
15949 MEMBER_LOC is the location expression attribute for the particular
15950 member of the common block that we are processing.
15951 CU is the CU from which the above come. */
15952
15953 static void
15954 mark_common_block_symbol_computed (struct symbol *sym,
15955 struct die_info *common_die,
15956 struct attribute *common_loc,
15957 struct attribute *member_loc,
15958 struct dwarf2_cu *cu)
15959 {
15960 struct dwarf2_per_objfile *dwarf2_per_objfile
15961 = cu->per_cu->dwarf2_per_objfile;
15962 struct objfile *objfile = dwarf2_per_objfile->objfile;
15963 struct dwarf2_locexpr_baton *baton;
15964 gdb_byte *ptr;
15965 unsigned int cu_off;
15966 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15967 LONGEST offset = 0;
15968
15969 gdb_assert (common_loc && member_loc);
15970 gdb_assert (common_loc->form_is_block ());
15971 gdb_assert (member_loc->form_is_block ()
15972 || member_loc->form_is_constant ());
15973
15974 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15975 baton->per_cu = cu->per_cu;
15976 gdb_assert (baton->per_cu);
15977
15978 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15979
15980 if (member_loc->form_is_constant ())
15981 {
15982 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15983 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15984 }
15985 else
15986 baton->size += DW_BLOCK (member_loc)->size;
15987
15988 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15989 baton->data = ptr;
15990
15991 *ptr++ = DW_OP_call4;
15992 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15993 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15994 ptr += 4;
15995
15996 if (member_loc->form_is_constant ())
15997 {
15998 *ptr++ = DW_OP_addr;
15999 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16000 ptr += cu->header.addr_size;
16001 }
16002 else
16003 {
16004 /* We have to copy the data here, because DW_OP_call4 will only
16005 use a DW_AT_location attribute. */
16006 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16007 ptr += DW_BLOCK (member_loc)->size;
16008 }
16009
16010 *ptr++ = DW_OP_plus;
16011 gdb_assert (ptr - baton->data == baton->size);
16012
16013 SYMBOL_LOCATION_BATON (sym) = baton;
16014 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16015 }
16016
16017 /* Create appropriate locally-scoped variables for all the
16018 DW_TAG_common_block entries. Also create a struct common_block
16019 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16020 is used to separate the common blocks name namespace from regular
16021 variable names. */
16022
16023 static void
16024 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16025 {
16026 struct attribute *attr;
16027
16028 attr = dwarf2_attr (die, DW_AT_location, cu);
16029 if (attr != nullptr)
16030 {
16031 /* Support the .debug_loc offsets. */
16032 if (attr->form_is_block ())
16033 {
16034 /* Ok. */
16035 }
16036 else if (attr->form_is_section_offset ())
16037 {
16038 dwarf2_complex_location_expr_complaint ();
16039 attr = NULL;
16040 }
16041 else
16042 {
16043 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16044 "common block member");
16045 attr = NULL;
16046 }
16047 }
16048
16049 if (die->child != NULL)
16050 {
16051 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16052 struct die_info *child_die;
16053 size_t n_entries = 0, size;
16054 struct common_block *common_block;
16055 struct symbol *sym;
16056
16057 for (child_die = die->child;
16058 child_die && child_die->tag;
16059 child_die = sibling_die (child_die))
16060 ++n_entries;
16061
16062 size = (sizeof (struct common_block)
16063 + (n_entries - 1) * sizeof (struct symbol *));
16064 common_block
16065 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16066 size);
16067 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16068 common_block->n_entries = 0;
16069
16070 for (child_die = die->child;
16071 child_die && child_die->tag;
16072 child_die = sibling_die (child_die))
16073 {
16074 /* Create the symbol in the DW_TAG_common_block block in the current
16075 symbol scope. */
16076 sym = new_symbol (child_die, NULL, cu);
16077 if (sym != NULL)
16078 {
16079 struct attribute *member_loc;
16080
16081 common_block->contents[common_block->n_entries++] = sym;
16082
16083 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16084 cu);
16085 if (member_loc)
16086 {
16087 /* GDB has handled this for a long time, but it is
16088 not specified by DWARF. It seems to have been
16089 emitted by gfortran at least as recently as:
16090 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16091 complaint (_("Variable in common block has "
16092 "DW_AT_data_member_location "
16093 "- DIE at %s [in module %s]"),
16094 sect_offset_str (child_die->sect_off),
16095 objfile_name (objfile));
16096
16097 if (member_loc->form_is_section_offset ())
16098 dwarf2_complex_location_expr_complaint ();
16099 else if (member_loc->form_is_constant ()
16100 || member_loc->form_is_block ())
16101 {
16102 if (attr != nullptr)
16103 mark_common_block_symbol_computed (sym, die, attr,
16104 member_loc, cu);
16105 }
16106 else
16107 dwarf2_complex_location_expr_complaint ();
16108 }
16109 }
16110 }
16111
16112 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16113 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16114 }
16115 }
16116
16117 /* Create a type for a C++ namespace. */
16118
16119 static struct type *
16120 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16121 {
16122 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16123 const char *previous_prefix, *name;
16124 int is_anonymous;
16125 struct type *type;
16126
16127 /* For extensions, reuse the type of the original namespace. */
16128 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16129 {
16130 struct die_info *ext_die;
16131 struct dwarf2_cu *ext_cu = cu;
16132
16133 ext_die = dwarf2_extension (die, &ext_cu);
16134 type = read_type_die (ext_die, ext_cu);
16135
16136 /* EXT_CU may not be the same as CU.
16137 Ensure TYPE is recorded with CU in die_type_hash. */
16138 return set_die_type (die, type, cu);
16139 }
16140
16141 name = namespace_name (die, &is_anonymous, cu);
16142
16143 /* Now build the name of the current namespace. */
16144
16145 previous_prefix = determine_prefix (die, cu);
16146 if (previous_prefix[0] != '\0')
16147 name = typename_concat (&objfile->objfile_obstack,
16148 previous_prefix, name, 0, cu);
16149
16150 /* Create the type. */
16151 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16152
16153 return set_die_type (die, type, cu);
16154 }
16155
16156 /* Read a namespace scope. */
16157
16158 static void
16159 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16160 {
16161 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16162 int is_anonymous;
16163
16164 /* Add a symbol associated to this if we haven't seen the namespace
16165 before. Also, add a using directive if it's an anonymous
16166 namespace. */
16167
16168 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16169 {
16170 struct type *type;
16171
16172 type = read_type_die (die, cu);
16173 new_symbol (die, type, cu);
16174
16175 namespace_name (die, &is_anonymous, cu);
16176 if (is_anonymous)
16177 {
16178 const char *previous_prefix = determine_prefix (die, cu);
16179
16180 std::vector<const char *> excludes;
16181 add_using_directive (using_directives (cu),
16182 previous_prefix, TYPE_NAME (type), NULL,
16183 NULL, excludes, 0, &objfile->objfile_obstack);
16184 }
16185 }
16186
16187 if (die->child != NULL)
16188 {
16189 struct die_info *child_die = die->child;
16190
16191 while (child_die && child_die->tag)
16192 {
16193 process_die (child_die, cu);
16194 child_die = sibling_die (child_die);
16195 }
16196 }
16197 }
16198
16199 /* Read a Fortran module as type. This DIE can be only a declaration used for
16200 imported module. Still we need that type as local Fortran "use ... only"
16201 declaration imports depend on the created type in determine_prefix. */
16202
16203 static struct type *
16204 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16205 {
16206 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16207 const char *module_name;
16208 struct type *type;
16209
16210 module_name = dwarf2_name (die, cu);
16211 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16212
16213 return set_die_type (die, type, cu);
16214 }
16215
16216 /* Read a Fortran module. */
16217
16218 static void
16219 read_module (struct die_info *die, struct dwarf2_cu *cu)
16220 {
16221 struct die_info *child_die = die->child;
16222 struct type *type;
16223
16224 type = read_type_die (die, cu);
16225 new_symbol (die, type, cu);
16226
16227 while (child_die && child_die->tag)
16228 {
16229 process_die (child_die, cu);
16230 child_die = sibling_die (child_die);
16231 }
16232 }
16233
16234 /* Return the name of the namespace represented by DIE. Set
16235 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16236 namespace. */
16237
16238 static const char *
16239 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16240 {
16241 struct die_info *current_die;
16242 const char *name = NULL;
16243
16244 /* Loop through the extensions until we find a name. */
16245
16246 for (current_die = die;
16247 current_die != NULL;
16248 current_die = dwarf2_extension (die, &cu))
16249 {
16250 /* We don't use dwarf2_name here so that we can detect the absence
16251 of a name -> anonymous namespace. */
16252 name = dwarf2_string_attr (die, DW_AT_name, cu);
16253
16254 if (name != NULL)
16255 break;
16256 }
16257
16258 /* Is it an anonymous namespace? */
16259
16260 *is_anonymous = (name == NULL);
16261 if (*is_anonymous)
16262 name = CP_ANONYMOUS_NAMESPACE_STR;
16263
16264 return name;
16265 }
16266
16267 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16268 the user defined type vector. */
16269
16270 static struct type *
16271 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16272 {
16273 struct gdbarch *gdbarch
16274 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16275 struct comp_unit_head *cu_header = &cu->header;
16276 struct type *type;
16277 struct attribute *attr_byte_size;
16278 struct attribute *attr_address_class;
16279 int byte_size, addr_class;
16280 struct type *target_type;
16281
16282 target_type = die_type (die, cu);
16283
16284 /* The die_type call above may have already set the type for this DIE. */
16285 type = get_die_type (die, cu);
16286 if (type)
16287 return type;
16288
16289 type = lookup_pointer_type (target_type);
16290
16291 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16292 if (attr_byte_size)
16293 byte_size = DW_UNSND (attr_byte_size);
16294 else
16295 byte_size = cu_header->addr_size;
16296
16297 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16298 if (attr_address_class)
16299 addr_class = DW_UNSND (attr_address_class);
16300 else
16301 addr_class = DW_ADDR_none;
16302
16303 ULONGEST alignment = get_alignment (cu, die);
16304
16305 /* If the pointer size, alignment, or address class is different
16306 than the default, create a type variant marked as such and set
16307 the length accordingly. */
16308 if (TYPE_LENGTH (type) != byte_size
16309 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16310 && alignment != TYPE_RAW_ALIGN (type))
16311 || addr_class != DW_ADDR_none)
16312 {
16313 if (gdbarch_address_class_type_flags_p (gdbarch))
16314 {
16315 int type_flags;
16316
16317 type_flags = gdbarch_address_class_type_flags
16318 (gdbarch, byte_size, addr_class);
16319 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16320 == 0);
16321 type = make_type_with_address_space (type, type_flags);
16322 }
16323 else if (TYPE_LENGTH (type) != byte_size)
16324 {
16325 complaint (_("invalid pointer size %d"), byte_size);
16326 }
16327 else if (TYPE_RAW_ALIGN (type) != alignment)
16328 {
16329 complaint (_("Invalid DW_AT_alignment"
16330 " - DIE at %s [in module %s]"),
16331 sect_offset_str (die->sect_off),
16332 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16333 }
16334 else
16335 {
16336 /* Should we also complain about unhandled address classes? */
16337 }
16338 }
16339
16340 TYPE_LENGTH (type) = byte_size;
16341 set_type_align (type, alignment);
16342 return set_die_type (die, type, cu);
16343 }
16344
16345 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16346 the user defined type vector. */
16347
16348 static struct type *
16349 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16350 {
16351 struct type *type;
16352 struct type *to_type;
16353 struct type *domain;
16354
16355 to_type = die_type (die, cu);
16356 domain = die_containing_type (die, cu);
16357
16358 /* The calls above may have already set the type for this DIE. */
16359 type = get_die_type (die, cu);
16360 if (type)
16361 return type;
16362
16363 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16364 type = lookup_methodptr_type (to_type);
16365 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16366 {
16367 struct type *new_type
16368 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16369
16370 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16371 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16372 TYPE_VARARGS (to_type));
16373 type = lookup_methodptr_type (new_type);
16374 }
16375 else
16376 type = lookup_memberptr_type (to_type, domain);
16377
16378 return set_die_type (die, type, cu);
16379 }
16380
16381 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16382 the user defined type vector. */
16383
16384 static struct type *
16385 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16386 enum type_code refcode)
16387 {
16388 struct comp_unit_head *cu_header = &cu->header;
16389 struct type *type, *target_type;
16390 struct attribute *attr;
16391
16392 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16393
16394 target_type = die_type (die, cu);
16395
16396 /* The die_type call above may have already set the type for this DIE. */
16397 type = get_die_type (die, cu);
16398 if (type)
16399 return type;
16400
16401 type = lookup_reference_type (target_type, refcode);
16402 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16403 if (attr != nullptr)
16404 {
16405 TYPE_LENGTH (type) = DW_UNSND (attr);
16406 }
16407 else
16408 {
16409 TYPE_LENGTH (type) = cu_header->addr_size;
16410 }
16411 maybe_set_alignment (cu, die, type);
16412 return set_die_type (die, type, cu);
16413 }
16414
16415 /* Add the given cv-qualifiers to the element type of the array. GCC
16416 outputs DWARF type qualifiers that apply to an array, not the
16417 element type. But GDB relies on the array element type to carry
16418 the cv-qualifiers. This mimics section 6.7.3 of the C99
16419 specification. */
16420
16421 static struct type *
16422 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16423 struct type *base_type, int cnst, int voltl)
16424 {
16425 struct type *el_type, *inner_array;
16426
16427 base_type = copy_type (base_type);
16428 inner_array = base_type;
16429
16430 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16431 {
16432 TYPE_TARGET_TYPE (inner_array) =
16433 copy_type (TYPE_TARGET_TYPE (inner_array));
16434 inner_array = TYPE_TARGET_TYPE (inner_array);
16435 }
16436
16437 el_type = TYPE_TARGET_TYPE (inner_array);
16438 cnst |= TYPE_CONST (el_type);
16439 voltl |= TYPE_VOLATILE (el_type);
16440 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16441
16442 return set_die_type (die, base_type, cu);
16443 }
16444
16445 static struct type *
16446 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16447 {
16448 struct type *base_type, *cv_type;
16449
16450 base_type = die_type (die, cu);
16451
16452 /* The die_type call above may have already set the type for this DIE. */
16453 cv_type = get_die_type (die, cu);
16454 if (cv_type)
16455 return cv_type;
16456
16457 /* In case the const qualifier is applied to an array type, the element type
16458 is so qualified, not the array type (section 6.7.3 of C99). */
16459 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16460 return add_array_cv_type (die, cu, base_type, 1, 0);
16461
16462 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16463 return set_die_type (die, cv_type, cu);
16464 }
16465
16466 static struct type *
16467 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16468 {
16469 struct type *base_type, *cv_type;
16470
16471 base_type = die_type (die, cu);
16472
16473 /* The die_type call above may have already set the type for this DIE. */
16474 cv_type = get_die_type (die, cu);
16475 if (cv_type)
16476 return cv_type;
16477
16478 /* In case the volatile qualifier is applied to an array type, the
16479 element type is so qualified, not the array type (section 6.7.3
16480 of C99). */
16481 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16482 return add_array_cv_type (die, cu, base_type, 0, 1);
16483
16484 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16485 return set_die_type (die, cv_type, cu);
16486 }
16487
16488 /* Handle DW_TAG_restrict_type. */
16489
16490 static struct type *
16491 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16492 {
16493 struct type *base_type, *cv_type;
16494
16495 base_type = die_type (die, cu);
16496
16497 /* The die_type call above may have already set the type for this DIE. */
16498 cv_type = get_die_type (die, cu);
16499 if (cv_type)
16500 return cv_type;
16501
16502 cv_type = make_restrict_type (base_type);
16503 return set_die_type (die, cv_type, cu);
16504 }
16505
16506 /* Handle DW_TAG_atomic_type. */
16507
16508 static struct type *
16509 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16510 {
16511 struct type *base_type, *cv_type;
16512
16513 base_type = die_type (die, cu);
16514
16515 /* The die_type call above may have already set the type for this DIE. */
16516 cv_type = get_die_type (die, cu);
16517 if (cv_type)
16518 return cv_type;
16519
16520 cv_type = make_atomic_type (base_type);
16521 return set_die_type (die, cv_type, cu);
16522 }
16523
16524 /* Extract all information from a DW_TAG_string_type DIE and add to
16525 the user defined type vector. It isn't really a user defined type,
16526 but it behaves like one, with other DIE's using an AT_user_def_type
16527 attribute to reference it. */
16528
16529 static struct type *
16530 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16531 {
16532 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16533 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16534 struct type *type, *range_type, *index_type, *char_type;
16535 struct attribute *attr;
16536 struct dynamic_prop prop;
16537 bool length_is_constant = true;
16538 LONGEST length;
16539
16540 /* There are a couple of places where bit sizes might be made use of
16541 when parsing a DW_TAG_string_type, however, no producer that we know
16542 of make use of these. Handling bit sizes that are a multiple of the
16543 byte size is easy enough, but what about other bit sizes? Lets deal
16544 with that problem when we have to. Warn about these attributes being
16545 unsupported, then parse the type and ignore them like we always
16546 have. */
16547 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16548 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16549 {
16550 static bool warning_printed = false;
16551 if (!warning_printed)
16552 {
16553 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16554 "currently supported on DW_TAG_string_type."));
16555 warning_printed = true;
16556 }
16557 }
16558
16559 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16560 if (attr != nullptr && !attr->form_is_constant ())
16561 {
16562 /* The string length describes the location at which the length of
16563 the string can be found. The size of the length field can be
16564 specified with one of the attributes below. */
16565 struct type *prop_type;
16566 struct attribute *len
16567 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16568 if (len == nullptr)
16569 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16570 if (len != nullptr && len->form_is_constant ())
16571 {
16572 /* Pass 0 as the default as we know this attribute is constant
16573 and the default value will not be returned. */
16574 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16575 prop_type = cu->per_cu->int_type (sz, true);
16576 }
16577 else
16578 {
16579 /* If the size is not specified then we assume it is the size of
16580 an address on this target. */
16581 prop_type = cu->per_cu->addr_sized_int_type (true);
16582 }
16583
16584 /* Convert the attribute into a dynamic property. */
16585 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16586 length = 1;
16587 else
16588 length_is_constant = false;
16589 }
16590 else if (attr != nullptr)
16591 {
16592 /* This DW_AT_string_length just contains the length with no
16593 indirection. There's no need to create a dynamic property in this
16594 case. Pass 0 for the default value as we know it will not be
16595 returned in this case. */
16596 length = dwarf2_get_attr_constant_value (attr, 0);
16597 }
16598 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16599 {
16600 /* We don't currently support non-constant byte sizes for strings. */
16601 length = dwarf2_get_attr_constant_value (attr, 1);
16602 }
16603 else
16604 {
16605 /* Use 1 as a fallback length if we have nothing else. */
16606 length = 1;
16607 }
16608
16609 index_type = objfile_type (objfile)->builtin_int;
16610 if (length_is_constant)
16611 range_type = create_static_range_type (NULL, index_type, 1, length);
16612 else
16613 {
16614 struct dynamic_prop low_bound;
16615
16616 low_bound.kind = PROP_CONST;
16617 low_bound.data.const_val = 1;
16618 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16619 }
16620 char_type = language_string_char_type (cu->language_defn, gdbarch);
16621 type = create_string_type (NULL, char_type, range_type);
16622
16623 return set_die_type (die, type, cu);
16624 }
16625
16626 /* Assuming that DIE corresponds to a function, returns nonzero
16627 if the function is prototyped. */
16628
16629 static int
16630 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16631 {
16632 struct attribute *attr;
16633
16634 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16635 if (attr && (DW_UNSND (attr) != 0))
16636 return 1;
16637
16638 /* The DWARF standard implies that the DW_AT_prototyped attribute
16639 is only meaningful for C, but the concept also extends to other
16640 languages that allow unprototyped functions (Eg: Objective C).
16641 For all other languages, assume that functions are always
16642 prototyped. */
16643 if (cu->language != language_c
16644 && cu->language != language_objc
16645 && cu->language != language_opencl)
16646 return 1;
16647
16648 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16649 prototyped and unprototyped functions; default to prototyped,
16650 since that is more common in modern code (and RealView warns
16651 about unprototyped functions). */
16652 if (producer_is_realview (cu->producer))
16653 return 1;
16654
16655 return 0;
16656 }
16657
16658 /* Handle DIES due to C code like:
16659
16660 struct foo
16661 {
16662 int (*funcp)(int a, long l);
16663 int b;
16664 };
16665
16666 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16667
16668 static struct type *
16669 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16670 {
16671 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16672 struct type *type; /* Type that this function returns. */
16673 struct type *ftype; /* Function that returns above type. */
16674 struct attribute *attr;
16675
16676 type = die_type (die, cu);
16677
16678 /* The die_type call above may have already set the type for this DIE. */
16679 ftype = get_die_type (die, cu);
16680 if (ftype)
16681 return ftype;
16682
16683 ftype = lookup_function_type (type);
16684
16685 if (prototyped_function_p (die, cu))
16686 TYPE_PROTOTYPED (ftype) = 1;
16687
16688 /* Store the calling convention in the type if it's available in
16689 the subroutine die. Otherwise set the calling convention to
16690 the default value DW_CC_normal. */
16691 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16692 if (attr != nullptr
16693 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16694 TYPE_CALLING_CONVENTION (ftype)
16695 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16696 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16697 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16698 else
16699 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16700
16701 /* Record whether the function returns normally to its caller or not
16702 if the DWARF producer set that information. */
16703 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16704 if (attr && (DW_UNSND (attr) != 0))
16705 TYPE_NO_RETURN (ftype) = 1;
16706
16707 /* We need to add the subroutine type to the die immediately so
16708 we don't infinitely recurse when dealing with parameters
16709 declared as the same subroutine type. */
16710 set_die_type (die, ftype, cu);
16711
16712 if (die->child != NULL)
16713 {
16714 struct type *void_type = objfile_type (objfile)->builtin_void;
16715 struct die_info *child_die;
16716 int nparams, iparams;
16717
16718 /* Count the number of parameters.
16719 FIXME: GDB currently ignores vararg functions, but knows about
16720 vararg member functions. */
16721 nparams = 0;
16722 child_die = die->child;
16723 while (child_die && child_die->tag)
16724 {
16725 if (child_die->tag == DW_TAG_formal_parameter)
16726 nparams++;
16727 else if (child_die->tag == DW_TAG_unspecified_parameters)
16728 TYPE_VARARGS (ftype) = 1;
16729 child_die = sibling_die (child_die);
16730 }
16731
16732 /* Allocate storage for parameters and fill them in. */
16733 TYPE_NFIELDS (ftype) = nparams;
16734 TYPE_FIELDS (ftype) = (struct field *)
16735 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16736
16737 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16738 even if we error out during the parameters reading below. */
16739 for (iparams = 0; iparams < nparams; iparams++)
16740 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16741
16742 iparams = 0;
16743 child_die = die->child;
16744 while (child_die && child_die->tag)
16745 {
16746 if (child_die->tag == DW_TAG_formal_parameter)
16747 {
16748 struct type *arg_type;
16749
16750 /* DWARF version 2 has no clean way to discern C++
16751 static and non-static member functions. G++ helps
16752 GDB by marking the first parameter for non-static
16753 member functions (which is the this pointer) as
16754 artificial. We pass this information to
16755 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16756
16757 DWARF version 3 added DW_AT_object_pointer, which GCC
16758 4.5 does not yet generate. */
16759 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16760 if (attr != nullptr)
16761 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16762 else
16763 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16764 arg_type = die_type (child_die, cu);
16765
16766 /* RealView does not mark THIS as const, which the testsuite
16767 expects. GCC marks THIS as const in method definitions,
16768 but not in the class specifications (GCC PR 43053). */
16769 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16770 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16771 {
16772 int is_this = 0;
16773 struct dwarf2_cu *arg_cu = cu;
16774 const char *name = dwarf2_name (child_die, cu);
16775
16776 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16777 if (attr != nullptr)
16778 {
16779 /* If the compiler emits this, use it. */
16780 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16781 is_this = 1;
16782 }
16783 else if (name && strcmp (name, "this") == 0)
16784 /* Function definitions will have the argument names. */
16785 is_this = 1;
16786 else if (name == NULL && iparams == 0)
16787 /* Declarations may not have the names, so like
16788 elsewhere in GDB, assume an artificial first
16789 argument is "this". */
16790 is_this = 1;
16791
16792 if (is_this)
16793 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16794 arg_type, 0);
16795 }
16796
16797 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16798 iparams++;
16799 }
16800 child_die = sibling_die (child_die);
16801 }
16802 }
16803
16804 return ftype;
16805 }
16806
16807 static struct type *
16808 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16809 {
16810 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16811 const char *name = NULL;
16812 struct type *this_type, *target_type;
16813
16814 name = dwarf2_full_name (NULL, die, cu);
16815 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16816 TYPE_TARGET_STUB (this_type) = 1;
16817 set_die_type (die, this_type, cu);
16818 target_type = die_type (die, cu);
16819 if (target_type != this_type)
16820 TYPE_TARGET_TYPE (this_type) = target_type;
16821 else
16822 {
16823 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16824 spec and cause infinite loops in GDB. */
16825 complaint (_("Self-referential DW_TAG_typedef "
16826 "- DIE at %s [in module %s]"),
16827 sect_offset_str (die->sect_off), objfile_name (objfile));
16828 TYPE_TARGET_TYPE (this_type) = NULL;
16829 }
16830 if (name == NULL)
16831 {
16832 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16833 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16834 Handle these by just returning the target type, rather than
16835 constructing an anonymous typedef type and trying to handle this
16836 elsewhere. */
16837 set_die_type (die, target_type, cu);
16838 return target_type;
16839 }
16840 return this_type;
16841 }
16842
16843 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16844 (which may be different from NAME) to the architecture back-end to allow
16845 it to guess the correct format if necessary. */
16846
16847 static struct type *
16848 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16849 const char *name_hint, enum bfd_endian byte_order)
16850 {
16851 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16852 const struct floatformat **format;
16853 struct type *type;
16854
16855 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16856 if (format)
16857 type = init_float_type (objfile, bits, name, format, byte_order);
16858 else
16859 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16860
16861 return type;
16862 }
16863
16864 /* Allocate an integer type of size BITS and name NAME. */
16865
16866 static struct type *
16867 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16868 int bits, int unsigned_p, const char *name)
16869 {
16870 struct type *type;
16871
16872 /* Versions of Intel's C Compiler generate an integer type called "void"
16873 instead of using DW_TAG_unspecified_type. This has been seen on
16874 at least versions 14, 17, and 18. */
16875 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16876 && strcmp (name, "void") == 0)
16877 type = objfile_type (objfile)->builtin_void;
16878 else
16879 type = init_integer_type (objfile, bits, unsigned_p, name);
16880
16881 return type;
16882 }
16883
16884 /* Initialise and return a floating point type of size BITS suitable for
16885 use as a component of a complex number. The NAME_HINT is passed through
16886 when initialising the floating point type and is the name of the complex
16887 type.
16888
16889 As DWARF doesn't currently provide an explicit name for the components
16890 of a complex number, but it can be helpful to have these components
16891 named, we try to select a suitable name based on the size of the
16892 component. */
16893 static struct type *
16894 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16895 struct objfile *objfile,
16896 int bits, const char *name_hint,
16897 enum bfd_endian byte_order)
16898 {
16899 gdbarch *gdbarch = get_objfile_arch (objfile);
16900 struct type *tt = nullptr;
16901
16902 /* Try to find a suitable floating point builtin type of size BITS.
16903 We're going to use the name of this type as the name for the complex
16904 target type that we are about to create. */
16905 switch (cu->language)
16906 {
16907 case language_fortran:
16908 switch (bits)
16909 {
16910 case 32:
16911 tt = builtin_f_type (gdbarch)->builtin_real;
16912 break;
16913 case 64:
16914 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16915 break;
16916 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16917 case 128:
16918 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16919 break;
16920 }
16921 break;
16922 default:
16923 switch (bits)
16924 {
16925 case 32:
16926 tt = builtin_type (gdbarch)->builtin_float;
16927 break;
16928 case 64:
16929 tt = builtin_type (gdbarch)->builtin_double;
16930 break;
16931 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16932 case 128:
16933 tt = builtin_type (gdbarch)->builtin_long_double;
16934 break;
16935 }
16936 break;
16937 }
16938
16939 /* If the type we found doesn't match the size we were looking for, then
16940 pretend we didn't find a type at all, the complex target type we
16941 create will then be nameless. */
16942 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16943 tt = nullptr;
16944
16945 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16946 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16947 }
16948
16949 /* Find a representation of a given base type and install
16950 it in the TYPE field of the die. */
16951
16952 static struct type *
16953 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16954 {
16955 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16956 struct type *type;
16957 struct attribute *attr;
16958 int encoding = 0, bits = 0;
16959 const char *name;
16960 gdbarch *arch;
16961
16962 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16963 if (attr != nullptr)
16964 encoding = DW_UNSND (attr);
16965 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16966 if (attr != nullptr)
16967 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16968 name = dwarf2_name (die, cu);
16969 if (!name)
16970 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16971
16972 arch = get_objfile_arch (objfile);
16973 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16974
16975 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16976 if (attr)
16977 {
16978 int endianity = DW_UNSND (attr);
16979
16980 switch (endianity)
16981 {
16982 case DW_END_big:
16983 byte_order = BFD_ENDIAN_BIG;
16984 break;
16985 case DW_END_little:
16986 byte_order = BFD_ENDIAN_LITTLE;
16987 break;
16988 default:
16989 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16990 break;
16991 }
16992 }
16993
16994 switch (encoding)
16995 {
16996 case DW_ATE_address:
16997 /* Turn DW_ATE_address into a void * pointer. */
16998 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16999 type = init_pointer_type (objfile, bits, name, type);
17000 break;
17001 case DW_ATE_boolean:
17002 type = init_boolean_type (objfile, bits, 1, name);
17003 break;
17004 case DW_ATE_complex_float:
17005 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17006 byte_order);
17007 type = init_complex_type (objfile, name, type);
17008 break;
17009 case DW_ATE_decimal_float:
17010 type = init_decfloat_type (objfile, bits, name);
17011 break;
17012 case DW_ATE_float:
17013 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17014 break;
17015 case DW_ATE_signed:
17016 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17017 break;
17018 case DW_ATE_unsigned:
17019 if (cu->language == language_fortran
17020 && name
17021 && startswith (name, "character("))
17022 type = init_character_type (objfile, bits, 1, name);
17023 else
17024 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17025 break;
17026 case DW_ATE_signed_char:
17027 if (cu->language == language_ada || cu->language == language_m2
17028 || cu->language == language_pascal
17029 || cu->language == language_fortran)
17030 type = init_character_type (objfile, bits, 0, name);
17031 else
17032 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17033 break;
17034 case DW_ATE_unsigned_char:
17035 if (cu->language == language_ada || cu->language == language_m2
17036 || cu->language == language_pascal
17037 || cu->language == language_fortran
17038 || cu->language == language_rust)
17039 type = init_character_type (objfile, bits, 1, name);
17040 else
17041 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17042 break;
17043 case DW_ATE_UTF:
17044 {
17045 if (bits == 16)
17046 type = builtin_type (arch)->builtin_char16;
17047 else if (bits == 32)
17048 type = builtin_type (arch)->builtin_char32;
17049 else
17050 {
17051 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17052 bits);
17053 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17054 }
17055 return set_die_type (die, type, cu);
17056 }
17057 break;
17058
17059 default:
17060 complaint (_("unsupported DW_AT_encoding: '%s'"),
17061 dwarf_type_encoding_name (encoding));
17062 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17063 break;
17064 }
17065
17066 if (name && strcmp (name, "char") == 0)
17067 TYPE_NOSIGN (type) = 1;
17068
17069 maybe_set_alignment (cu, die, type);
17070
17071 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17072
17073 return set_die_type (die, type, cu);
17074 }
17075
17076 /* Parse dwarf attribute if it's a block, reference or constant and put the
17077 resulting value of the attribute into struct bound_prop.
17078 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17079
17080 static int
17081 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17082 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17083 struct type *default_type)
17084 {
17085 struct dwarf2_property_baton *baton;
17086 struct obstack *obstack
17087 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17088
17089 gdb_assert (default_type != NULL);
17090
17091 if (attr == NULL || prop == NULL)
17092 return 0;
17093
17094 if (attr->form_is_block ())
17095 {
17096 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17097 baton->property_type = default_type;
17098 baton->locexpr.per_cu = cu->per_cu;
17099 baton->locexpr.size = DW_BLOCK (attr)->size;
17100 baton->locexpr.data = DW_BLOCK (attr)->data;
17101 switch (attr->name)
17102 {
17103 case DW_AT_string_length:
17104 baton->locexpr.is_reference = true;
17105 break;
17106 default:
17107 baton->locexpr.is_reference = false;
17108 break;
17109 }
17110 prop->data.baton = baton;
17111 prop->kind = PROP_LOCEXPR;
17112 gdb_assert (prop->data.baton != NULL);
17113 }
17114 else if (attr->form_is_ref ())
17115 {
17116 struct dwarf2_cu *target_cu = cu;
17117 struct die_info *target_die;
17118 struct attribute *target_attr;
17119
17120 target_die = follow_die_ref (die, attr, &target_cu);
17121 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17122 if (target_attr == NULL)
17123 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17124 target_cu);
17125 if (target_attr == NULL)
17126 return 0;
17127
17128 switch (target_attr->name)
17129 {
17130 case DW_AT_location:
17131 if (target_attr->form_is_section_offset ())
17132 {
17133 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17134 baton->property_type = die_type (target_die, target_cu);
17135 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17136 prop->data.baton = baton;
17137 prop->kind = PROP_LOCLIST;
17138 gdb_assert (prop->data.baton != NULL);
17139 }
17140 else if (target_attr->form_is_block ())
17141 {
17142 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17143 baton->property_type = die_type (target_die, target_cu);
17144 baton->locexpr.per_cu = cu->per_cu;
17145 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17146 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17147 baton->locexpr.is_reference = true;
17148 prop->data.baton = baton;
17149 prop->kind = PROP_LOCEXPR;
17150 gdb_assert (prop->data.baton != NULL);
17151 }
17152 else
17153 {
17154 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17155 "dynamic property");
17156 return 0;
17157 }
17158 break;
17159 case DW_AT_data_member_location:
17160 {
17161 LONGEST offset;
17162
17163 if (!handle_data_member_location (target_die, target_cu,
17164 &offset))
17165 return 0;
17166
17167 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17168 baton->property_type = read_type_die (target_die->parent,
17169 target_cu);
17170 baton->offset_info.offset = offset;
17171 baton->offset_info.type = die_type (target_die, target_cu);
17172 prop->data.baton = baton;
17173 prop->kind = PROP_ADDR_OFFSET;
17174 break;
17175 }
17176 }
17177 }
17178 else if (attr->form_is_constant ())
17179 {
17180 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17181 prop->kind = PROP_CONST;
17182 }
17183 else
17184 {
17185 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17186 dwarf2_name (die, cu));
17187 return 0;
17188 }
17189
17190 return 1;
17191 }
17192
17193 /* See read.h. */
17194
17195 struct type *
17196 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17197 {
17198 struct objfile *objfile = dwarf2_per_objfile->objfile;
17199 struct type *int_type;
17200
17201 /* Helper macro to examine the various builtin types. */
17202 #define TRY_TYPE(F) \
17203 int_type = (unsigned_p \
17204 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17205 : objfile_type (objfile)->builtin_ ## F); \
17206 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17207 return int_type
17208
17209 TRY_TYPE (char);
17210 TRY_TYPE (short);
17211 TRY_TYPE (int);
17212 TRY_TYPE (long);
17213 TRY_TYPE (long_long);
17214
17215 #undef TRY_TYPE
17216
17217 gdb_assert_not_reached ("unable to find suitable integer type");
17218 }
17219
17220 /* See read.h. */
17221
17222 struct type *
17223 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17224 {
17225 int addr_size = this->addr_size ();
17226 return int_type (addr_size, unsigned_p);
17227 }
17228
17229 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17230 present (which is valid) then compute the default type based on the
17231 compilation units address size. */
17232
17233 static struct type *
17234 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17235 {
17236 struct type *index_type = die_type (die, cu);
17237
17238 /* Dwarf-2 specifications explicitly allows to create subrange types
17239 without specifying a base type.
17240 In that case, the base type must be set to the type of
17241 the lower bound, upper bound or count, in that order, if any of these
17242 three attributes references an object that has a type.
17243 If no base type is found, the Dwarf-2 specifications say that
17244 a signed integer type of size equal to the size of an address should
17245 be used.
17246 For the following C code: `extern char gdb_int [];'
17247 GCC produces an empty range DIE.
17248 FIXME: muller/2010-05-28: Possible references to object for low bound,
17249 high bound or count are not yet handled by this code. */
17250 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17251 index_type = cu->per_cu->addr_sized_int_type (false);
17252
17253 return index_type;
17254 }
17255
17256 /* Read the given DW_AT_subrange DIE. */
17257
17258 static struct type *
17259 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17260 {
17261 struct type *base_type, *orig_base_type;
17262 struct type *range_type;
17263 struct attribute *attr;
17264 struct dynamic_prop low, high;
17265 int low_default_is_valid;
17266 int high_bound_is_count = 0;
17267 const char *name;
17268 ULONGEST negative_mask;
17269
17270 orig_base_type = read_subrange_index_type (die, cu);
17271
17272 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17273 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17274 creating the range type, but we use the result of check_typedef
17275 when examining properties of the type. */
17276 base_type = check_typedef (orig_base_type);
17277
17278 /* The die_type call above may have already set the type for this DIE. */
17279 range_type = get_die_type (die, cu);
17280 if (range_type)
17281 return range_type;
17282
17283 low.kind = PROP_CONST;
17284 high.kind = PROP_CONST;
17285 high.data.const_val = 0;
17286
17287 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17288 omitting DW_AT_lower_bound. */
17289 switch (cu->language)
17290 {
17291 case language_c:
17292 case language_cplus:
17293 low.data.const_val = 0;
17294 low_default_is_valid = 1;
17295 break;
17296 case language_fortran:
17297 low.data.const_val = 1;
17298 low_default_is_valid = 1;
17299 break;
17300 case language_d:
17301 case language_objc:
17302 case language_rust:
17303 low.data.const_val = 0;
17304 low_default_is_valid = (cu->header.version >= 4);
17305 break;
17306 case language_ada:
17307 case language_m2:
17308 case language_pascal:
17309 low.data.const_val = 1;
17310 low_default_is_valid = (cu->header.version >= 4);
17311 break;
17312 default:
17313 low.data.const_val = 0;
17314 low_default_is_valid = 0;
17315 break;
17316 }
17317
17318 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17319 if (attr != nullptr)
17320 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17321 else if (!low_default_is_valid)
17322 complaint (_("Missing DW_AT_lower_bound "
17323 "- DIE at %s [in module %s]"),
17324 sect_offset_str (die->sect_off),
17325 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17326
17327 struct attribute *attr_ub, *attr_count;
17328 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17329 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17330 {
17331 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17332 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17333 {
17334 /* If bounds are constant do the final calculation here. */
17335 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17336 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17337 else
17338 high_bound_is_count = 1;
17339 }
17340 else
17341 {
17342 if (attr_ub != NULL)
17343 complaint (_("Unresolved DW_AT_upper_bound "
17344 "- DIE at %s [in module %s]"),
17345 sect_offset_str (die->sect_off),
17346 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17347 if (attr_count != NULL)
17348 complaint (_("Unresolved DW_AT_count "
17349 "- DIE at %s [in module %s]"),
17350 sect_offset_str (die->sect_off),
17351 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17352 }
17353 }
17354
17355 LONGEST bias = 0;
17356 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17357 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17358 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17359
17360 /* Normally, the DWARF producers are expected to use a signed
17361 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17362 But this is unfortunately not always the case, as witnessed
17363 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17364 is used instead. To work around that ambiguity, we treat
17365 the bounds as signed, and thus sign-extend their values, when
17366 the base type is signed. */
17367 negative_mask =
17368 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17369 if (low.kind == PROP_CONST
17370 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17371 low.data.const_val |= negative_mask;
17372 if (high.kind == PROP_CONST
17373 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17374 high.data.const_val |= negative_mask;
17375
17376 /* Check for bit and byte strides. */
17377 struct dynamic_prop byte_stride_prop;
17378 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17379 if (attr_byte_stride != nullptr)
17380 {
17381 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17382 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17383 prop_type);
17384 }
17385
17386 struct dynamic_prop bit_stride_prop;
17387 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17388 if (attr_bit_stride != nullptr)
17389 {
17390 /* It only makes sense to have either a bit or byte stride. */
17391 if (attr_byte_stride != nullptr)
17392 {
17393 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17394 "- DIE at %s [in module %s]"),
17395 sect_offset_str (die->sect_off),
17396 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17397 attr_bit_stride = nullptr;
17398 }
17399 else
17400 {
17401 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17402 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17403 prop_type);
17404 }
17405 }
17406
17407 if (attr_byte_stride != nullptr
17408 || attr_bit_stride != nullptr)
17409 {
17410 bool byte_stride_p = (attr_byte_stride != nullptr);
17411 struct dynamic_prop *stride
17412 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17413
17414 range_type
17415 = create_range_type_with_stride (NULL, orig_base_type, &low,
17416 &high, bias, stride, byte_stride_p);
17417 }
17418 else
17419 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17420
17421 if (high_bound_is_count)
17422 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17423
17424 /* Ada expects an empty array on no boundary attributes. */
17425 if (attr == NULL && cu->language != language_ada)
17426 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17427
17428 name = dwarf2_name (die, cu);
17429 if (name)
17430 TYPE_NAME (range_type) = name;
17431
17432 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17433 if (attr != nullptr)
17434 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17435
17436 maybe_set_alignment (cu, die, range_type);
17437
17438 set_die_type (die, range_type, cu);
17439
17440 /* set_die_type should be already done. */
17441 set_descriptive_type (range_type, die, cu);
17442
17443 return range_type;
17444 }
17445
17446 static struct type *
17447 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17448 {
17449 struct type *type;
17450
17451 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17452 NULL);
17453 TYPE_NAME (type) = dwarf2_name (die, cu);
17454
17455 /* In Ada, an unspecified type is typically used when the description
17456 of the type is deferred to a different unit. When encountering
17457 such a type, we treat it as a stub, and try to resolve it later on,
17458 when needed. */
17459 if (cu->language == language_ada)
17460 TYPE_STUB (type) = 1;
17461
17462 return set_die_type (die, type, cu);
17463 }
17464
17465 /* Read a single die and all its descendents. Set the die's sibling
17466 field to NULL; set other fields in the die correctly, and set all
17467 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17468 location of the info_ptr after reading all of those dies. PARENT
17469 is the parent of the die in question. */
17470
17471 static struct die_info *
17472 read_die_and_children (const struct die_reader_specs *reader,
17473 const gdb_byte *info_ptr,
17474 const gdb_byte **new_info_ptr,
17475 struct die_info *parent)
17476 {
17477 struct die_info *die;
17478 const gdb_byte *cur_ptr;
17479
17480 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17481 if (die == NULL)
17482 {
17483 *new_info_ptr = cur_ptr;
17484 return NULL;
17485 }
17486 store_in_ref_table (die, reader->cu);
17487
17488 if (die->has_children)
17489 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17490 else
17491 {
17492 die->child = NULL;
17493 *new_info_ptr = cur_ptr;
17494 }
17495
17496 die->sibling = NULL;
17497 die->parent = parent;
17498 return die;
17499 }
17500
17501 /* Read a die, all of its descendents, and all of its siblings; set
17502 all of the fields of all of the dies correctly. Arguments are as
17503 in read_die_and_children. */
17504
17505 static struct die_info *
17506 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17507 const gdb_byte *info_ptr,
17508 const gdb_byte **new_info_ptr,
17509 struct die_info *parent)
17510 {
17511 struct die_info *first_die, *last_sibling;
17512 const gdb_byte *cur_ptr;
17513
17514 cur_ptr = info_ptr;
17515 first_die = last_sibling = NULL;
17516
17517 while (1)
17518 {
17519 struct die_info *die
17520 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17521
17522 if (die == NULL)
17523 {
17524 *new_info_ptr = cur_ptr;
17525 return first_die;
17526 }
17527
17528 if (!first_die)
17529 first_die = die;
17530 else
17531 last_sibling->sibling = die;
17532
17533 last_sibling = die;
17534 }
17535 }
17536
17537 /* Read a die, all of its descendents, and all of its siblings; set
17538 all of the fields of all of the dies correctly. Arguments are as
17539 in read_die_and_children.
17540 This the main entry point for reading a DIE and all its children. */
17541
17542 static struct die_info *
17543 read_die_and_siblings (const struct die_reader_specs *reader,
17544 const gdb_byte *info_ptr,
17545 const gdb_byte **new_info_ptr,
17546 struct die_info *parent)
17547 {
17548 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17549 new_info_ptr, parent);
17550
17551 if (dwarf_die_debug)
17552 {
17553 fprintf_unfiltered (gdb_stdlog,
17554 "Read die from %s@0x%x of %s:\n",
17555 reader->die_section->get_name (),
17556 (unsigned) (info_ptr - reader->die_section->buffer),
17557 bfd_get_filename (reader->abfd));
17558 dump_die (die, dwarf_die_debug);
17559 }
17560
17561 return die;
17562 }
17563
17564 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17565 attributes.
17566 The caller is responsible for filling in the extra attributes
17567 and updating (*DIEP)->num_attrs.
17568 Set DIEP to point to a newly allocated die with its information,
17569 except for its child, sibling, and parent fields. */
17570
17571 static const gdb_byte *
17572 read_full_die_1 (const struct die_reader_specs *reader,
17573 struct die_info **diep, const gdb_byte *info_ptr,
17574 int num_extra_attrs)
17575 {
17576 unsigned int abbrev_number, bytes_read, i;
17577 struct abbrev_info *abbrev;
17578 struct die_info *die;
17579 struct dwarf2_cu *cu = reader->cu;
17580 bfd *abfd = reader->abfd;
17581
17582 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17583 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17584 info_ptr += bytes_read;
17585 if (!abbrev_number)
17586 {
17587 *diep = NULL;
17588 return info_ptr;
17589 }
17590
17591 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17592 if (!abbrev)
17593 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17594 abbrev_number,
17595 bfd_get_filename (abfd));
17596
17597 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17598 die->sect_off = sect_off;
17599 die->tag = abbrev->tag;
17600 die->abbrev = abbrev_number;
17601 die->has_children = abbrev->has_children;
17602
17603 /* Make the result usable.
17604 The caller needs to update num_attrs after adding the extra
17605 attributes. */
17606 die->num_attrs = abbrev->num_attrs;
17607
17608 std::vector<int> indexes_that_need_reprocess;
17609 for (i = 0; i < abbrev->num_attrs; ++i)
17610 {
17611 bool need_reprocess;
17612 info_ptr =
17613 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17614 info_ptr, &need_reprocess);
17615 if (need_reprocess)
17616 indexes_that_need_reprocess.push_back (i);
17617 }
17618
17619 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17620 if (attr != nullptr)
17621 cu->str_offsets_base = DW_UNSND (attr);
17622
17623 auto maybe_addr_base = lookup_addr_base(die);
17624 if (maybe_addr_base.has_value ())
17625 cu->addr_base = *maybe_addr_base;
17626 for (int index : indexes_that_need_reprocess)
17627 read_attribute_reprocess (reader, &die->attrs[index]);
17628 *diep = die;
17629 return info_ptr;
17630 }
17631
17632 /* Read a die and all its attributes.
17633 Set DIEP to point to a newly allocated die with its information,
17634 except for its child, sibling, and parent fields. */
17635
17636 static const gdb_byte *
17637 read_full_die (const struct die_reader_specs *reader,
17638 struct die_info **diep, const gdb_byte *info_ptr)
17639 {
17640 const gdb_byte *result;
17641
17642 result = read_full_die_1 (reader, diep, info_ptr, 0);
17643
17644 if (dwarf_die_debug)
17645 {
17646 fprintf_unfiltered (gdb_stdlog,
17647 "Read die from %s@0x%x of %s:\n",
17648 reader->die_section->get_name (),
17649 (unsigned) (info_ptr - reader->die_section->buffer),
17650 bfd_get_filename (reader->abfd));
17651 dump_die (*diep, dwarf_die_debug);
17652 }
17653
17654 return result;
17655 }
17656 \f
17657
17658 /* Returns nonzero if TAG represents a type that we might generate a partial
17659 symbol for. */
17660
17661 static int
17662 is_type_tag_for_partial (int tag)
17663 {
17664 switch (tag)
17665 {
17666 #if 0
17667 /* Some types that would be reasonable to generate partial symbols for,
17668 that we don't at present. */
17669 case DW_TAG_array_type:
17670 case DW_TAG_file_type:
17671 case DW_TAG_ptr_to_member_type:
17672 case DW_TAG_set_type:
17673 case DW_TAG_string_type:
17674 case DW_TAG_subroutine_type:
17675 #endif
17676 case DW_TAG_base_type:
17677 case DW_TAG_class_type:
17678 case DW_TAG_interface_type:
17679 case DW_TAG_enumeration_type:
17680 case DW_TAG_structure_type:
17681 case DW_TAG_subrange_type:
17682 case DW_TAG_typedef:
17683 case DW_TAG_union_type:
17684 return 1;
17685 default:
17686 return 0;
17687 }
17688 }
17689
17690 /* Load all DIEs that are interesting for partial symbols into memory. */
17691
17692 static struct partial_die_info *
17693 load_partial_dies (const struct die_reader_specs *reader,
17694 const gdb_byte *info_ptr, int building_psymtab)
17695 {
17696 struct dwarf2_cu *cu = reader->cu;
17697 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17698 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17699 unsigned int bytes_read;
17700 unsigned int load_all = 0;
17701 int nesting_level = 1;
17702
17703 parent_die = NULL;
17704 last_die = NULL;
17705
17706 gdb_assert (cu->per_cu != NULL);
17707 if (cu->per_cu->load_all_dies)
17708 load_all = 1;
17709
17710 cu->partial_dies
17711 = htab_create_alloc_ex (cu->header.length / 12,
17712 partial_die_hash,
17713 partial_die_eq,
17714 NULL,
17715 &cu->comp_unit_obstack,
17716 hashtab_obstack_allocate,
17717 dummy_obstack_deallocate);
17718
17719 while (1)
17720 {
17721 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17722
17723 /* A NULL abbrev means the end of a series of children. */
17724 if (abbrev == NULL)
17725 {
17726 if (--nesting_level == 0)
17727 return first_die;
17728
17729 info_ptr += bytes_read;
17730 last_die = parent_die;
17731 parent_die = parent_die->die_parent;
17732 continue;
17733 }
17734
17735 /* Check for template arguments. We never save these; if
17736 they're seen, we just mark the parent, and go on our way. */
17737 if (parent_die != NULL
17738 && cu->language == language_cplus
17739 && (abbrev->tag == DW_TAG_template_type_param
17740 || abbrev->tag == DW_TAG_template_value_param))
17741 {
17742 parent_die->has_template_arguments = 1;
17743
17744 if (!load_all)
17745 {
17746 /* We don't need a partial DIE for the template argument. */
17747 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17748 continue;
17749 }
17750 }
17751
17752 /* We only recurse into c++ subprograms looking for template arguments.
17753 Skip their other children. */
17754 if (!load_all
17755 && cu->language == language_cplus
17756 && parent_die != NULL
17757 && parent_die->tag == DW_TAG_subprogram)
17758 {
17759 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17760 continue;
17761 }
17762
17763 /* Check whether this DIE is interesting enough to save. Normally
17764 we would not be interested in members here, but there may be
17765 later variables referencing them via DW_AT_specification (for
17766 static members). */
17767 if (!load_all
17768 && !is_type_tag_for_partial (abbrev->tag)
17769 && abbrev->tag != DW_TAG_constant
17770 && abbrev->tag != DW_TAG_enumerator
17771 && abbrev->tag != DW_TAG_subprogram
17772 && abbrev->tag != DW_TAG_inlined_subroutine
17773 && abbrev->tag != DW_TAG_lexical_block
17774 && abbrev->tag != DW_TAG_variable
17775 && abbrev->tag != DW_TAG_namespace
17776 && abbrev->tag != DW_TAG_module
17777 && abbrev->tag != DW_TAG_member
17778 && abbrev->tag != DW_TAG_imported_unit
17779 && abbrev->tag != DW_TAG_imported_declaration)
17780 {
17781 /* Otherwise we skip to the next sibling, if any. */
17782 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17783 continue;
17784 }
17785
17786 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17787 abbrev);
17788
17789 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17790
17791 /* This two-pass algorithm for processing partial symbols has a
17792 high cost in cache pressure. Thus, handle some simple cases
17793 here which cover the majority of C partial symbols. DIEs
17794 which neither have specification tags in them, nor could have
17795 specification tags elsewhere pointing at them, can simply be
17796 processed and discarded.
17797
17798 This segment is also optional; scan_partial_symbols and
17799 add_partial_symbol will handle these DIEs if we chain
17800 them in normally. When compilers which do not emit large
17801 quantities of duplicate debug information are more common,
17802 this code can probably be removed. */
17803
17804 /* Any complete simple types at the top level (pretty much all
17805 of them, for a language without namespaces), can be processed
17806 directly. */
17807 if (parent_die == NULL
17808 && pdi.has_specification == 0
17809 && pdi.is_declaration == 0
17810 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17811 || pdi.tag == DW_TAG_base_type
17812 || pdi.tag == DW_TAG_subrange_type))
17813 {
17814 if (building_psymtab && pdi.name != NULL)
17815 add_psymbol_to_list (pdi.name, false,
17816 VAR_DOMAIN, LOC_TYPEDEF, -1,
17817 psymbol_placement::STATIC,
17818 0, cu->language, objfile);
17819 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17820 continue;
17821 }
17822
17823 /* The exception for DW_TAG_typedef with has_children above is
17824 a workaround of GCC PR debug/47510. In the case of this complaint
17825 type_name_or_error will error on such types later.
17826
17827 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17828 it could not find the child DIEs referenced later, this is checked
17829 above. In correct DWARF DW_TAG_typedef should have no children. */
17830
17831 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17832 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17833 "- DIE at %s [in module %s]"),
17834 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17835
17836 /* If we're at the second level, and we're an enumerator, and
17837 our parent has no specification (meaning possibly lives in a
17838 namespace elsewhere), then we can add the partial symbol now
17839 instead of queueing it. */
17840 if (pdi.tag == DW_TAG_enumerator
17841 && parent_die != NULL
17842 && parent_die->die_parent == NULL
17843 && parent_die->tag == DW_TAG_enumeration_type
17844 && parent_die->has_specification == 0)
17845 {
17846 if (pdi.name == NULL)
17847 complaint (_("malformed enumerator DIE ignored"));
17848 else if (building_psymtab)
17849 add_psymbol_to_list (pdi.name, false,
17850 VAR_DOMAIN, LOC_CONST, -1,
17851 cu->language == language_cplus
17852 ? psymbol_placement::GLOBAL
17853 : psymbol_placement::STATIC,
17854 0, cu->language, objfile);
17855
17856 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17857 continue;
17858 }
17859
17860 struct partial_die_info *part_die
17861 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17862
17863 /* We'll save this DIE so link it in. */
17864 part_die->die_parent = parent_die;
17865 part_die->die_sibling = NULL;
17866 part_die->die_child = NULL;
17867
17868 if (last_die && last_die == parent_die)
17869 last_die->die_child = part_die;
17870 else if (last_die)
17871 last_die->die_sibling = part_die;
17872
17873 last_die = part_die;
17874
17875 if (first_die == NULL)
17876 first_die = part_die;
17877
17878 /* Maybe add the DIE to the hash table. Not all DIEs that we
17879 find interesting need to be in the hash table, because we
17880 also have the parent/sibling/child chains; only those that we
17881 might refer to by offset later during partial symbol reading.
17882
17883 For now this means things that might have be the target of a
17884 DW_AT_specification, DW_AT_abstract_origin, or
17885 DW_AT_extension. DW_AT_extension will refer only to
17886 namespaces; DW_AT_abstract_origin refers to functions (and
17887 many things under the function DIE, but we do not recurse
17888 into function DIEs during partial symbol reading) and
17889 possibly variables as well; DW_AT_specification refers to
17890 declarations. Declarations ought to have the DW_AT_declaration
17891 flag. It happens that GCC forgets to put it in sometimes, but
17892 only for functions, not for types.
17893
17894 Adding more things than necessary to the hash table is harmless
17895 except for the performance cost. Adding too few will result in
17896 wasted time in find_partial_die, when we reread the compilation
17897 unit with load_all_dies set. */
17898
17899 if (load_all
17900 || abbrev->tag == DW_TAG_constant
17901 || abbrev->tag == DW_TAG_subprogram
17902 || abbrev->tag == DW_TAG_variable
17903 || abbrev->tag == DW_TAG_namespace
17904 || part_die->is_declaration)
17905 {
17906 void **slot;
17907
17908 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17909 to_underlying (part_die->sect_off),
17910 INSERT);
17911 *slot = part_die;
17912 }
17913
17914 /* For some DIEs we want to follow their children (if any). For C
17915 we have no reason to follow the children of structures; for other
17916 languages we have to, so that we can get at method physnames
17917 to infer fully qualified class names, for DW_AT_specification,
17918 and for C++ template arguments. For C++, we also look one level
17919 inside functions to find template arguments (if the name of the
17920 function does not already contain the template arguments).
17921
17922 For Ada and Fortran, we need to scan the children of subprograms
17923 and lexical blocks as well because these languages allow the
17924 definition of nested entities that could be interesting for the
17925 debugger, such as nested subprograms for instance. */
17926 if (last_die->has_children
17927 && (load_all
17928 || last_die->tag == DW_TAG_namespace
17929 || last_die->tag == DW_TAG_module
17930 || last_die->tag == DW_TAG_enumeration_type
17931 || (cu->language == language_cplus
17932 && last_die->tag == DW_TAG_subprogram
17933 && (last_die->name == NULL
17934 || strchr (last_die->name, '<') == NULL))
17935 || (cu->language != language_c
17936 && (last_die->tag == DW_TAG_class_type
17937 || last_die->tag == DW_TAG_interface_type
17938 || last_die->tag == DW_TAG_structure_type
17939 || last_die->tag == DW_TAG_union_type))
17940 || ((cu->language == language_ada
17941 || cu->language == language_fortran)
17942 && (last_die->tag == DW_TAG_subprogram
17943 || last_die->tag == DW_TAG_lexical_block))))
17944 {
17945 nesting_level++;
17946 parent_die = last_die;
17947 continue;
17948 }
17949
17950 /* Otherwise we skip to the next sibling, if any. */
17951 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17952
17953 /* Back to the top, do it again. */
17954 }
17955 }
17956
17957 partial_die_info::partial_die_info (sect_offset sect_off_,
17958 struct abbrev_info *abbrev)
17959 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17960 {
17961 }
17962
17963 /* Read a minimal amount of information into the minimal die structure.
17964 INFO_PTR should point just after the initial uleb128 of a DIE. */
17965
17966 const gdb_byte *
17967 partial_die_info::read (const struct die_reader_specs *reader,
17968 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17969 {
17970 struct dwarf2_cu *cu = reader->cu;
17971 struct dwarf2_per_objfile *dwarf2_per_objfile
17972 = cu->per_cu->dwarf2_per_objfile;
17973 unsigned int i;
17974 int has_low_pc_attr = 0;
17975 int has_high_pc_attr = 0;
17976 int high_pc_relative = 0;
17977
17978 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17979 for (i = 0; i < abbrev.num_attrs; ++i)
17980 {
17981 bool need_reprocess;
17982 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17983 info_ptr, &need_reprocess);
17984 /* String and address offsets that need to do the reprocessing have
17985 already been read at this point, so there is no need to wait until
17986 the loop terminates to do the reprocessing. */
17987 if (need_reprocess)
17988 read_attribute_reprocess (reader, &attr_vec[i]);
17989 attribute &attr = attr_vec[i];
17990 /* Store the data if it is of an attribute we want to keep in a
17991 partial symbol table. */
17992 switch (attr.name)
17993 {
17994 case DW_AT_name:
17995 switch (tag)
17996 {
17997 case DW_TAG_compile_unit:
17998 case DW_TAG_partial_unit:
17999 case DW_TAG_type_unit:
18000 /* Compilation units have a DW_AT_name that is a filename, not
18001 a source language identifier. */
18002 case DW_TAG_enumeration_type:
18003 case DW_TAG_enumerator:
18004 /* These tags always have simple identifiers already; no need
18005 to canonicalize them. */
18006 name = DW_STRING (&attr);
18007 break;
18008 default:
18009 {
18010 struct objfile *objfile = dwarf2_per_objfile->objfile;
18011
18012 name
18013 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18014 }
18015 break;
18016 }
18017 break;
18018 case DW_AT_linkage_name:
18019 case DW_AT_MIPS_linkage_name:
18020 /* Note that both forms of linkage name might appear. We
18021 assume they will be the same, and we only store the last
18022 one we see. */
18023 linkage_name = DW_STRING (&attr);
18024 break;
18025 case DW_AT_low_pc:
18026 has_low_pc_attr = 1;
18027 lowpc = attr.value_as_address ();
18028 break;
18029 case DW_AT_high_pc:
18030 has_high_pc_attr = 1;
18031 highpc = attr.value_as_address ();
18032 if (cu->header.version >= 4 && attr.form_is_constant ())
18033 high_pc_relative = 1;
18034 break;
18035 case DW_AT_location:
18036 /* Support the .debug_loc offsets. */
18037 if (attr.form_is_block ())
18038 {
18039 d.locdesc = DW_BLOCK (&attr);
18040 }
18041 else if (attr.form_is_section_offset ())
18042 {
18043 dwarf2_complex_location_expr_complaint ();
18044 }
18045 else
18046 {
18047 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18048 "partial symbol information");
18049 }
18050 break;
18051 case DW_AT_external:
18052 is_external = DW_UNSND (&attr);
18053 break;
18054 case DW_AT_declaration:
18055 is_declaration = DW_UNSND (&attr);
18056 break;
18057 case DW_AT_type:
18058 has_type = 1;
18059 break;
18060 case DW_AT_abstract_origin:
18061 case DW_AT_specification:
18062 case DW_AT_extension:
18063 has_specification = 1;
18064 spec_offset = dwarf2_get_ref_die_offset (&attr);
18065 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18066 || cu->per_cu->is_dwz);
18067 break;
18068 case DW_AT_sibling:
18069 /* Ignore absolute siblings, they might point outside of
18070 the current compile unit. */
18071 if (attr.form == DW_FORM_ref_addr)
18072 complaint (_("ignoring absolute DW_AT_sibling"));
18073 else
18074 {
18075 const gdb_byte *buffer = reader->buffer;
18076 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18077 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18078
18079 if (sibling_ptr < info_ptr)
18080 complaint (_("DW_AT_sibling points backwards"));
18081 else if (sibling_ptr > reader->buffer_end)
18082 reader->die_section->overflow_complaint ();
18083 else
18084 sibling = sibling_ptr;
18085 }
18086 break;
18087 case DW_AT_byte_size:
18088 has_byte_size = 1;
18089 break;
18090 case DW_AT_const_value:
18091 has_const_value = 1;
18092 break;
18093 case DW_AT_calling_convention:
18094 /* DWARF doesn't provide a way to identify a program's source-level
18095 entry point. DW_AT_calling_convention attributes are only meant
18096 to describe functions' calling conventions.
18097
18098 However, because it's a necessary piece of information in
18099 Fortran, and before DWARF 4 DW_CC_program was the only
18100 piece of debugging information whose definition refers to
18101 a 'main program' at all, several compilers marked Fortran
18102 main programs with DW_CC_program --- even when those
18103 functions use the standard calling conventions.
18104
18105 Although DWARF now specifies a way to provide this
18106 information, we support this practice for backward
18107 compatibility. */
18108 if (DW_UNSND (&attr) == DW_CC_program
18109 && cu->language == language_fortran)
18110 main_subprogram = 1;
18111 break;
18112 case DW_AT_inline:
18113 if (DW_UNSND (&attr) == DW_INL_inlined
18114 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18115 may_be_inlined = 1;
18116 break;
18117
18118 case DW_AT_import:
18119 if (tag == DW_TAG_imported_unit)
18120 {
18121 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18122 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18123 || cu->per_cu->is_dwz);
18124 }
18125 break;
18126
18127 case DW_AT_main_subprogram:
18128 main_subprogram = DW_UNSND (&attr);
18129 break;
18130
18131 case DW_AT_ranges:
18132 {
18133 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18134 but that requires a full DIE, so instead we just
18135 reimplement it. */
18136 int need_ranges_base = tag != DW_TAG_compile_unit;
18137 unsigned int ranges_offset = (DW_UNSND (&attr)
18138 + (need_ranges_base
18139 ? cu->ranges_base
18140 : 0));
18141
18142 /* Value of the DW_AT_ranges attribute is the offset in the
18143 .debug_ranges section. */
18144 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18145 nullptr))
18146 has_pc_info = 1;
18147 }
18148 break;
18149
18150 default:
18151 break;
18152 }
18153 }
18154
18155 /* For Ada, if both the name and the linkage name appear, we prefer
18156 the latter. This lets "catch exception" work better, regardless
18157 of the order in which the name and linkage name were emitted.
18158 Really, though, this is just a workaround for the fact that gdb
18159 doesn't store both the name and the linkage name. */
18160 if (cu->language == language_ada && linkage_name != nullptr)
18161 name = linkage_name;
18162
18163 if (high_pc_relative)
18164 highpc += lowpc;
18165
18166 if (has_low_pc_attr && has_high_pc_attr)
18167 {
18168 /* When using the GNU linker, .gnu.linkonce. sections are used to
18169 eliminate duplicate copies of functions and vtables and such.
18170 The linker will arbitrarily choose one and discard the others.
18171 The AT_*_pc values for such functions refer to local labels in
18172 these sections. If the section from that file was discarded, the
18173 labels are not in the output, so the relocs get a value of 0.
18174 If this is a discarded function, mark the pc bounds as invalid,
18175 so that GDB will ignore it. */
18176 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18177 {
18178 struct objfile *objfile = dwarf2_per_objfile->objfile;
18179 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18180
18181 complaint (_("DW_AT_low_pc %s is zero "
18182 "for DIE at %s [in module %s]"),
18183 paddress (gdbarch, lowpc),
18184 sect_offset_str (sect_off),
18185 objfile_name (objfile));
18186 }
18187 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18188 else if (lowpc >= highpc)
18189 {
18190 struct objfile *objfile = dwarf2_per_objfile->objfile;
18191 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18192
18193 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18194 "for DIE at %s [in module %s]"),
18195 paddress (gdbarch, lowpc),
18196 paddress (gdbarch, highpc),
18197 sect_offset_str (sect_off),
18198 objfile_name (objfile));
18199 }
18200 else
18201 has_pc_info = 1;
18202 }
18203
18204 return info_ptr;
18205 }
18206
18207 /* Find a cached partial DIE at OFFSET in CU. */
18208
18209 struct partial_die_info *
18210 dwarf2_cu::find_partial_die (sect_offset sect_off)
18211 {
18212 struct partial_die_info *lookup_die = NULL;
18213 struct partial_die_info part_die (sect_off);
18214
18215 lookup_die = ((struct partial_die_info *)
18216 htab_find_with_hash (partial_dies, &part_die,
18217 to_underlying (sect_off)));
18218
18219 return lookup_die;
18220 }
18221
18222 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18223 except in the case of .debug_types DIEs which do not reference
18224 outside their CU (they do however referencing other types via
18225 DW_FORM_ref_sig8). */
18226
18227 static const struct cu_partial_die_info
18228 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18229 {
18230 struct dwarf2_per_objfile *dwarf2_per_objfile
18231 = cu->per_cu->dwarf2_per_objfile;
18232 struct objfile *objfile = dwarf2_per_objfile->objfile;
18233 struct dwarf2_per_cu_data *per_cu = NULL;
18234 struct partial_die_info *pd = NULL;
18235
18236 if (offset_in_dwz == cu->per_cu->is_dwz
18237 && cu->header.offset_in_cu_p (sect_off))
18238 {
18239 pd = cu->find_partial_die (sect_off);
18240 if (pd != NULL)
18241 return { cu, pd };
18242 /* We missed recording what we needed.
18243 Load all dies and try again. */
18244 per_cu = cu->per_cu;
18245 }
18246 else
18247 {
18248 /* TUs don't reference other CUs/TUs (except via type signatures). */
18249 if (cu->per_cu->is_debug_types)
18250 {
18251 error (_("Dwarf Error: Type Unit at offset %s contains"
18252 " external reference to offset %s [in module %s].\n"),
18253 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18254 bfd_get_filename (objfile->obfd));
18255 }
18256 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18257 dwarf2_per_objfile);
18258
18259 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18260 load_partial_comp_unit (per_cu);
18261
18262 per_cu->cu->last_used = 0;
18263 pd = per_cu->cu->find_partial_die (sect_off);
18264 }
18265
18266 /* If we didn't find it, and not all dies have been loaded,
18267 load them all and try again. */
18268
18269 if (pd == NULL && per_cu->load_all_dies == 0)
18270 {
18271 per_cu->load_all_dies = 1;
18272
18273 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18274 THIS_CU->cu may already be in use. So we can't just free it and
18275 replace its DIEs with the ones we read in. Instead, we leave those
18276 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18277 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18278 set. */
18279 load_partial_comp_unit (per_cu);
18280
18281 pd = per_cu->cu->find_partial_die (sect_off);
18282 }
18283
18284 if (pd == NULL)
18285 internal_error (__FILE__, __LINE__,
18286 _("could not find partial DIE %s "
18287 "in cache [from module %s]\n"),
18288 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18289 return { per_cu->cu, pd };
18290 }
18291
18292 /* See if we can figure out if the class lives in a namespace. We do
18293 this by looking for a member function; its demangled name will
18294 contain namespace info, if there is any. */
18295
18296 static void
18297 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18298 struct dwarf2_cu *cu)
18299 {
18300 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18301 what template types look like, because the demangler
18302 frequently doesn't give the same name as the debug info. We
18303 could fix this by only using the demangled name to get the
18304 prefix (but see comment in read_structure_type). */
18305
18306 struct partial_die_info *real_pdi;
18307 struct partial_die_info *child_pdi;
18308
18309 /* If this DIE (this DIE's specification, if any) has a parent, then
18310 we should not do this. We'll prepend the parent's fully qualified
18311 name when we create the partial symbol. */
18312
18313 real_pdi = struct_pdi;
18314 while (real_pdi->has_specification)
18315 {
18316 auto res = find_partial_die (real_pdi->spec_offset,
18317 real_pdi->spec_is_dwz, cu);
18318 real_pdi = res.pdi;
18319 cu = res.cu;
18320 }
18321
18322 if (real_pdi->die_parent != NULL)
18323 return;
18324
18325 for (child_pdi = struct_pdi->die_child;
18326 child_pdi != NULL;
18327 child_pdi = child_pdi->die_sibling)
18328 {
18329 if (child_pdi->tag == DW_TAG_subprogram
18330 && child_pdi->linkage_name != NULL)
18331 {
18332 gdb::unique_xmalloc_ptr<char> actual_class_name
18333 (language_class_name_from_physname (cu->language_defn,
18334 child_pdi->linkage_name));
18335 if (actual_class_name != NULL)
18336 {
18337 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18338 struct_pdi->name = objfile->intern (actual_class_name.get ());
18339 }
18340 break;
18341 }
18342 }
18343 }
18344
18345 void
18346 partial_die_info::fixup (struct dwarf2_cu *cu)
18347 {
18348 /* Once we've fixed up a die, there's no point in doing so again.
18349 This also avoids a memory leak if we were to call
18350 guess_partial_die_structure_name multiple times. */
18351 if (fixup_called)
18352 return;
18353
18354 /* If we found a reference attribute and the DIE has no name, try
18355 to find a name in the referred to DIE. */
18356
18357 if (name == NULL && has_specification)
18358 {
18359 struct partial_die_info *spec_die;
18360
18361 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18362 spec_die = res.pdi;
18363 cu = res.cu;
18364
18365 spec_die->fixup (cu);
18366
18367 if (spec_die->name)
18368 {
18369 name = spec_die->name;
18370
18371 /* Copy DW_AT_external attribute if it is set. */
18372 if (spec_die->is_external)
18373 is_external = spec_die->is_external;
18374 }
18375 }
18376
18377 /* Set default names for some unnamed DIEs. */
18378
18379 if (name == NULL && tag == DW_TAG_namespace)
18380 name = CP_ANONYMOUS_NAMESPACE_STR;
18381
18382 /* If there is no parent die to provide a namespace, and there are
18383 children, see if we can determine the namespace from their linkage
18384 name. */
18385 if (cu->language == language_cplus
18386 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18387 && die_parent == NULL
18388 && has_children
18389 && (tag == DW_TAG_class_type
18390 || tag == DW_TAG_structure_type
18391 || tag == DW_TAG_union_type))
18392 guess_partial_die_structure_name (this, cu);
18393
18394 /* GCC might emit a nameless struct or union that has a linkage
18395 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18396 if (name == NULL
18397 && (tag == DW_TAG_class_type
18398 || tag == DW_TAG_interface_type
18399 || tag == DW_TAG_structure_type
18400 || tag == DW_TAG_union_type)
18401 && linkage_name != NULL)
18402 {
18403 gdb::unique_xmalloc_ptr<char> demangled
18404 (gdb_demangle (linkage_name, DMGL_TYPES));
18405 if (demangled != nullptr)
18406 {
18407 const char *base;
18408
18409 /* Strip any leading namespaces/classes, keep only the base name.
18410 DW_AT_name for named DIEs does not contain the prefixes. */
18411 base = strrchr (demangled.get (), ':');
18412 if (base && base > demangled.get () && base[-1] == ':')
18413 base++;
18414 else
18415 base = demangled.get ();
18416
18417 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18418 name = objfile->intern (base);
18419 }
18420 }
18421
18422 fixup_called = 1;
18423 }
18424
18425 /* Process the attributes that had to be skipped in the first round. These
18426 attributes are the ones that need str_offsets_base or addr_base attributes.
18427 They could not have been processed in the first round, because at the time
18428 the values of str_offsets_base or addr_base may not have been known. */
18429 void read_attribute_reprocess (const struct die_reader_specs *reader,
18430 struct attribute *attr)
18431 {
18432 struct dwarf2_cu *cu = reader->cu;
18433 switch (attr->form)
18434 {
18435 case DW_FORM_addrx:
18436 case DW_FORM_GNU_addr_index:
18437 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18438 break;
18439 case DW_FORM_strx:
18440 case DW_FORM_strx1:
18441 case DW_FORM_strx2:
18442 case DW_FORM_strx3:
18443 case DW_FORM_strx4:
18444 case DW_FORM_GNU_str_index:
18445 {
18446 unsigned int str_index = DW_UNSND (attr);
18447 if (reader->dwo_file != NULL)
18448 {
18449 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18450 DW_STRING_IS_CANONICAL (attr) = 0;
18451 }
18452 else
18453 {
18454 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18455 DW_STRING_IS_CANONICAL (attr) = 0;
18456 }
18457 break;
18458 }
18459 default:
18460 gdb_assert_not_reached (_("Unexpected DWARF form."));
18461 }
18462 }
18463
18464 /* Read an attribute value described by an attribute form. */
18465
18466 static const gdb_byte *
18467 read_attribute_value (const struct die_reader_specs *reader,
18468 struct attribute *attr, unsigned form,
18469 LONGEST implicit_const, const gdb_byte *info_ptr,
18470 bool *need_reprocess)
18471 {
18472 struct dwarf2_cu *cu = reader->cu;
18473 struct dwarf2_per_objfile *dwarf2_per_objfile
18474 = cu->per_cu->dwarf2_per_objfile;
18475 struct objfile *objfile = dwarf2_per_objfile->objfile;
18476 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18477 bfd *abfd = reader->abfd;
18478 struct comp_unit_head *cu_header = &cu->header;
18479 unsigned int bytes_read;
18480 struct dwarf_block *blk;
18481 *need_reprocess = false;
18482
18483 attr->form = (enum dwarf_form) form;
18484 switch (form)
18485 {
18486 case DW_FORM_ref_addr:
18487 if (cu->header.version == 2)
18488 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18489 &bytes_read);
18490 else
18491 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18492 &bytes_read);
18493 info_ptr += bytes_read;
18494 break;
18495 case DW_FORM_GNU_ref_alt:
18496 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18497 info_ptr += bytes_read;
18498 break;
18499 case DW_FORM_addr:
18500 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18501 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18502 info_ptr += bytes_read;
18503 break;
18504 case DW_FORM_block2:
18505 blk = dwarf_alloc_block (cu);
18506 blk->size = read_2_bytes (abfd, info_ptr);
18507 info_ptr += 2;
18508 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18509 info_ptr += blk->size;
18510 DW_BLOCK (attr) = blk;
18511 break;
18512 case DW_FORM_block4:
18513 blk = dwarf_alloc_block (cu);
18514 blk->size = read_4_bytes (abfd, info_ptr);
18515 info_ptr += 4;
18516 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18517 info_ptr += blk->size;
18518 DW_BLOCK (attr) = blk;
18519 break;
18520 case DW_FORM_data2:
18521 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18522 info_ptr += 2;
18523 break;
18524 case DW_FORM_data4:
18525 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18526 info_ptr += 4;
18527 break;
18528 case DW_FORM_data8:
18529 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18530 info_ptr += 8;
18531 break;
18532 case DW_FORM_data16:
18533 blk = dwarf_alloc_block (cu);
18534 blk->size = 16;
18535 blk->data = read_n_bytes (abfd, info_ptr, 16);
18536 info_ptr += 16;
18537 DW_BLOCK (attr) = blk;
18538 break;
18539 case DW_FORM_sec_offset:
18540 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18541 info_ptr += bytes_read;
18542 break;
18543 case DW_FORM_string:
18544 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18545 DW_STRING_IS_CANONICAL (attr) = 0;
18546 info_ptr += bytes_read;
18547 break;
18548 case DW_FORM_strp:
18549 if (!cu->per_cu->is_dwz)
18550 {
18551 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18552 abfd, info_ptr, cu_header,
18553 &bytes_read);
18554 DW_STRING_IS_CANONICAL (attr) = 0;
18555 info_ptr += bytes_read;
18556 break;
18557 }
18558 /* FALLTHROUGH */
18559 case DW_FORM_line_strp:
18560 if (!cu->per_cu->is_dwz)
18561 {
18562 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18563 abfd, info_ptr,
18564 cu_header, &bytes_read);
18565 DW_STRING_IS_CANONICAL (attr) = 0;
18566 info_ptr += bytes_read;
18567 break;
18568 }
18569 /* FALLTHROUGH */
18570 case DW_FORM_GNU_strp_alt:
18571 {
18572 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18573 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18574 &bytes_read);
18575
18576 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18577 DW_STRING_IS_CANONICAL (attr) = 0;
18578 info_ptr += bytes_read;
18579 }
18580 break;
18581 case DW_FORM_exprloc:
18582 case DW_FORM_block:
18583 blk = dwarf_alloc_block (cu);
18584 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18585 info_ptr += bytes_read;
18586 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18587 info_ptr += blk->size;
18588 DW_BLOCK (attr) = blk;
18589 break;
18590 case DW_FORM_block1:
18591 blk = dwarf_alloc_block (cu);
18592 blk->size = read_1_byte (abfd, info_ptr);
18593 info_ptr += 1;
18594 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18595 info_ptr += blk->size;
18596 DW_BLOCK (attr) = blk;
18597 break;
18598 case DW_FORM_data1:
18599 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18600 info_ptr += 1;
18601 break;
18602 case DW_FORM_flag:
18603 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18604 info_ptr += 1;
18605 break;
18606 case DW_FORM_flag_present:
18607 DW_UNSND (attr) = 1;
18608 break;
18609 case DW_FORM_sdata:
18610 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18611 info_ptr += bytes_read;
18612 break;
18613 case DW_FORM_udata:
18614 case DW_FORM_rnglistx:
18615 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18616 info_ptr += bytes_read;
18617 break;
18618 case DW_FORM_ref1:
18619 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18620 + read_1_byte (abfd, info_ptr));
18621 info_ptr += 1;
18622 break;
18623 case DW_FORM_ref2:
18624 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18625 + read_2_bytes (abfd, info_ptr));
18626 info_ptr += 2;
18627 break;
18628 case DW_FORM_ref4:
18629 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18630 + read_4_bytes (abfd, info_ptr));
18631 info_ptr += 4;
18632 break;
18633 case DW_FORM_ref8:
18634 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18635 + read_8_bytes (abfd, info_ptr));
18636 info_ptr += 8;
18637 break;
18638 case DW_FORM_ref_sig8:
18639 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18640 info_ptr += 8;
18641 break;
18642 case DW_FORM_ref_udata:
18643 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18644 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18645 info_ptr += bytes_read;
18646 break;
18647 case DW_FORM_indirect:
18648 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18649 info_ptr += bytes_read;
18650 if (form == DW_FORM_implicit_const)
18651 {
18652 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18653 info_ptr += bytes_read;
18654 }
18655 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18656 info_ptr, need_reprocess);
18657 break;
18658 case DW_FORM_implicit_const:
18659 DW_SND (attr) = implicit_const;
18660 break;
18661 case DW_FORM_addrx:
18662 case DW_FORM_GNU_addr_index:
18663 *need_reprocess = true;
18664 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18665 info_ptr += bytes_read;
18666 break;
18667 case DW_FORM_strx:
18668 case DW_FORM_strx1:
18669 case DW_FORM_strx2:
18670 case DW_FORM_strx3:
18671 case DW_FORM_strx4:
18672 case DW_FORM_GNU_str_index:
18673 {
18674 ULONGEST str_index;
18675 if (form == DW_FORM_strx1)
18676 {
18677 str_index = read_1_byte (abfd, info_ptr);
18678 info_ptr += 1;
18679 }
18680 else if (form == DW_FORM_strx2)
18681 {
18682 str_index = read_2_bytes (abfd, info_ptr);
18683 info_ptr += 2;
18684 }
18685 else if (form == DW_FORM_strx3)
18686 {
18687 str_index = read_3_bytes (abfd, info_ptr);
18688 info_ptr += 3;
18689 }
18690 else if (form == DW_FORM_strx4)
18691 {
18692 str_index = read_4_bytes (abfd, info_ptr);
18693 info_ptr += 4;
18694 }
18695 else
18696 {
18697 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18698 info_ptr += bytes_read;
18699 }
18700 *need_reprocess = true;
18701 DW_UNSND (attr) = str_index;
18702 }
18703 break;
18704 default:
18705 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18706 dwarf_form_name (form),
18707 bfd_get_filename (abfd));
18708 }
18709
18710 /* Super hack. */
18711 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18712 attr->form = DW_FORM_GNU_ref_alt;
18713
18714 /* We have seen instances where the compiler tried to emit a byte
18715 size attribute of -1 which ended up being encoded as an unsigned
18716 0xffffffff. Although 0xffffffff is technically a valid size value,
18717 an object of this size seems pretty unlikely so we can relatively
18718 safely treat these cases as if the size attribute was invalid and
18719 treat them as zero by default. */
18720 if (attr->name == DW_AT_byte_size
18721 && form == DW_FORM_data4
18722 && DW_UNSND (attr) >= 0xffffffff)
18723 {
18724 complaint
18725 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18726 hex_string (DW_UNSND (attr)));
18727 DW_UNSND (attr) = 0;
18728 }
18729
18730 return info_ptr;
18731 }
18732
18733 /* Read an attribute described by an abbreviated attribute. */
18734
18735 static const gdb_byte *
18736 read_attribute (const struct die_reader_specs *reader,
18737 struct attribute *attr, struct attr_abbrev *abbrev,
18738 const gdb_byte *info_ptr, bool *need_reprocess)
18739 {
18740 attr->name = abbrev->name;
18741 return read_attribute_value (reader, attr, abbrev->form,
18742 abbrev->implicit_const, info_ptr,
18743 need_reprocess);
18744 }
18745
18746 /* Cover function for read_initial_length.
18747 Returns the length of the object at BUF, and stores the size of the
18748 initial length in *BYTES_READ and stores the size that offsets will be in
18749 *OFFSET_SIZE.
18750 If the initial length size is not equivalent to that specified in
18751 CU_HEADER then issue a complaint.
18752 This is useful when reading non-comp-unit headers. */
18753
18754 static LONGEST
18755 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
18756 const struct comp_unit_head *cu_header,
18757 unsigned int *bytes_read,
18758 unsigned int *offset_size)
18759 {
18760 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18761
18762 gdb_assert (cu_header->initial_length_size == 4
18763 || cu_header->initial_length_size == 8
18764 || cu_header->initial_length_size == 12);
18765
18766 if (cu_header->initial_length_size != *bytes_read)
18767 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
18768
18769 *offset_size = (*bytes_read == 4) ? 4 : 8;
18770 return length;
18771 }
18772
18773 /* Return pointer to string at section SECT offset STR_OFFSET with error
18774 reporting strings FORM_NAME and SECT_NAME. */
18775
18776 static const char *
18777 read_indirect_string_at_offset_from (struct objfile *objfile,
18778 bfd *abfd, LONGEST str_offset,
18779 struct dwarf2_section_info *sect,
18780 const char *form_name,
18781 const char *sect_name)
18782 {
18783 sect->read (objfile);
18784 if (sect->buffer == NULL)
18785 error (_("%s used without %s section [in module %s]"),
18786 form_name, sect_name, bfd_get_filename (abfd));
18787 if (str_offset >= sect->size)
18788 error (_("%s pointing outside of %s section [in module %s]"),
18789 form_name, sect_name, bfd_get_filename (abfd));
18790 gdb_assert (HOST_CHAR_BIT == 8);
18791 if (sect->buffer[str_offset] == '\0')
18792 return NULL;
18793 return (const char *) (sect->buffer + str_offset);
18794 }
18795
18796 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18797
18798 static const char *
18799 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18800 bfd *abfd, LONGEST str_offset)
18801 {
18802 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18803 abfd, str_offset,
18804 &dwarf2_per_objfile->str,
18805 "DW_FORM_strp", ".debug_str");
18806 }
18807
18808 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18809
18810 static const char *
18811 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18812 bfd *abfd, LONGEST str_offset)
18813 {
18814 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18815 abfd, str_offset,
18816 &dwarf2_per_objfile->line_str,
18817 "DW_FORM_line_strp",
18818 ".debug_line_str");
18819 }
18820
18821 /* Return pointer to string at .debug_str offset as read from BUF.
18822 BUF is assumed to be in a compilation unit described by CU_HEADER.
18823 Return *BYTES_READ_PTR count of bytes read from BUF. */
18824
18825 static const char *
18826 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18827 const gdb_byte *buf,
18828 const struct comp_unit_head *cu_header,
18829 unsigned int *bytes_read_ptr)
18830 {
18831 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18832
18833 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
18834 }
18835
18836 /* Return pointer to string at .debug_line_str offset as read from BUF.
18837 BUF is assumed to be in a compilation unit described by CU_HEADER.
18838 Return *BYTES_READ_PTR count of bytes read from BUF. */
18839
18840 static const char *
18841 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18842 bfd *abfd, const gdb_byte *buf,
18843 const struct comp_unit_head *cu_header,
18844 unsigned int *bytes_read_ptr)
18845 {
18846 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18847
18848 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18849 str_offset);
18850 }
18851
18852 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18853 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18854 ADDR_SIZE is the size of addresses from the CU header. */
18855
18856 static CORE_ADDR
18857 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18858 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18859 int addr_size)
18860 {
18861 struct objfile *objfile = dwarf2_per_objfile->objfile;
18862 bfd *abfd = objfile->obfd;
18863 const gdb_byte *info_ptr;
18864 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18865
18866 dwarf2_per_objfile->addr.read (objfile);
18867 if (dwarf2_per_objfile->addr.buffer == NULL)
18868 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18869 objfile_name (objfile));
18870 if (addr_base_or_zero + addr_index * addr_size
18871 >= dwarf2_per_objfile->addr.size)
18872 error (_("DW_FORM_addr_index pointing outside of "
18873 ".debug_addr section [in module %s]"),
18874 objfile_name (objfile));
18875 info_ptr = (dwarf2_per_objfile->addr.buffer
18876 + addr_base_or_zero + addr_index * addr_size);
18877 if (addr_size == 4)
18878 return bfd_get_32 (abfd, info_ptr);
18879 else
18880 return bfd_get_64 (abfd, info_ptr);
18881 }
18882
18883 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18884
18885 static CORE_ADDR
18886 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18887 {
18888 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18889 cu->addr_base, cu->header.addr_size);
18890 }
18891
18892 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18893
18894 static CORE_ADDR
18895 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18896 unsigned int *bytes_read)
18897 {
18898 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18899 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18900
18901 return read_addr_index (cu, addr_index);
18902 }
18903
18904 /* See read.h. */
18905
18906 CORE_ADDR
18907 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18908 {
18909 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18910 struct dwarf2_cu *cu = per_cu->cu;
18911 gdb::optional<ULONGEST> addr_base;
18912 int addr_size;
18913
18914 /* We need addr_base and addr_size.
18915 If we don't have PER_CU->cu, we have to get it.
18916 Nasty, but the alternative is storing the needed info in PER_CU,
18917 which at this point doesn't seem justified: it's not clear how frequently
18918 it would get used and it would increase the size of every PER_CU.
18919 Entry points like dwarf2_per_cu_addr_size do a similar thing
18920 so we're not in uncharted territory here.
18921 Alas we need to be a bit more complicated as addr_base is contained
18922 in the DIE.
18923
18924 We don't need to read the entire CU(/TU).
18925 We just need the header and top level die.
18926
18927 IWBN to use the aging mechanism to let us lazily later discard the CU.
18928 For now we skip this optimization. */
18929
18930 if (cu != NULL)
18931 {
18932 addr_base = cu->addr_base;
18933 addr_size = cu->header.addr_size;
18934 }
18935 else
18936 {
18937 cutu_reader reader (per_cu, NULL, 0, false);
18938 addr_base = reader.cu->addr_base;
18939 addr_size = reader.cu->header.addr_size;
18940 }
18941
18942 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18943 addr_size);
18944 }
18945
18946 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18947 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18948 DWO file. */
18949
18950 static const char *
18951 read_str_index (struct dwarf2_cu *cu,
18952 struct dwarf2_section_info *str_section,
18953 struct dwarf2_section_info *str_offsets_section,
18954 ULONGEST str_offsets_base, ULONGEST str_index)
18955 {
18956 struct dwarf2_per_objfile *dwarf2_per_objfile
18957 = cu->per_cu->dwarf2_per_objfile;
18958 struct objfile *objfile = dwarf2_per_objfile->objfile;
18959 const char *objf_name = objfile_name (objfile);
18960 bfd *abfd = objfile->obfd;
18961 const gdb_byte *info_ptr;
18962 ULONGEST str_offset;
18963 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18964
18965 str_section->read (objfile);
18966 str_offsets_section->read (objfile);
18967 if (str_section->buffer == NULL)
18968 error (_("%s used without %s section"
18969 " in CU at offset %s [in module %s]"),
18970 form_name, str_section->get_name (),
18971 sect_offset_str (cu->header.sect_off), objf_name);
18972 if (str_offsets_section->buffer == NULL)
18973 error (_("%s used without %s section"
18974 " in CU at offset %s [in module %s]"),
18975 form_name, str_section->get_name (),
18976 sect_offset_str (cu->header.sect_off), objf_name);
18977 info_ptr = (str_offsets_section->buffer
18978 + str_offsets_base
18979 + str_index * cu->header.offset_size);
18980 if (cu->header.offset_size == 4)
18981 str_offset = bfd_get_32 (abfd, info_ptr);
18982 else
18983 str_offset = bfd_get_64 (abfd, info_ptr);
18984 if (str_offset >= str_section->size)
18985 error (_("Offset from %s pointing outside of"
18986 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18987 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18988 return (const char *) (str_section->buffer + str_offset);
18989 }
18990
18991 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18992
18993 static const char *
18994 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18995 {
18996 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18997 ? reader->cu->header.addr_size : 0;
18998 return read_str_index (reader->cu,
18999 &reader->dwo_file->sections.str,
19000 &reader->dwo_file->sections.str_offsets,
19001 str_offsets_base, str_index);
19002 }
19003
19004 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19005
19006 static const char *
19007 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19008 {
19009 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19010 const char *objf_name = objfile_name (objfile);
19011 static const char form_name[] = "DW_FORM_GNU_str_index";
19012 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19013
19014 if (!cu->str_offsets_base.has_value ())
19015 error (_("%s used in Fission stub without %s"
19016 " in CU at offset 0x%lx [in module %s]"),
19017 form_name, str_offsets_attr_name,
19018 (long) cu->header.offset_size, objf_name);
19019
19020 return read_str_index (cu,
19021 &cu->per_cu->dwarf2_per_objfile->str,
19022 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19023 *cu->str_offsets_base, str_index);
19024 }
19025
19026 /* Return the length of an LEB128 number in BUF. */
19027
19028 static int
19029 leb128_size (const gdb_byte *buf)
19030 {
19031 const gdb_byte *begin = buf;
19032 gdb_byte byte;
19033
19034 while (1)
19035 {
19036 byte = *buf++;
19037 if ((byte & 128) == 0)
19038 return buf - begin;
19039 }
19040 }
19041
19042 static void
19043 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19044 {
19045 switch (lang)
19046 {
19047 case DW_LANG_C89:
19048 case DW_LANG_C99:
19049 case DW_LANG_C11:
19050 case DW_LANG_C:
19051 case DW_LANG_UPC:
19052 cu->language = language_c;
19053 break;
19054 case DW_LANG_Java:
19055 case DW_LANG_C_plus_plus:
19056 case DW_LANG_C_plus_plus_11:
19057 case DW_LANG_C_plus_plus_14:
19058 cu->language = language_cplus;
19059 break;
19060 case DW_LANG_D:
19061 cu->language = language_d;
19062 break;
19063 case DW_LANG_Fortran77:
19064 case DW_LANG_Fortran90:
19065 case DW_LANG_Fortran95:
19066 case DW_LANG_Fortran03:
19067 case DW_LANG_Fortran08:
19068 cu->language = language_fortran;
19069 break;
19070 case DW_LANG_Go:
19071 cu->language = language_go;
19072 break;
19073 case DW_LANG_Mips_Assembler:
19074 cu->language = language_asm;
19075 break;
19076 case DW_LANG_Ada83:
19077 case DW_LANG_Ada95:
19078 cu->language = language_ada;
19079 break;
19080 case DW_LANG_Modula2:
19081 cu->language = language_m2;
19082 break;
19083 case DW_LANG_Pascal83:
19084 cu->language = language_pascal;
19085 break;
19086 case DW_LANG_ObjC:
19087 cu->language = language_objc;
19088 break;
19089 case DW_LANG_Rust:
19090 case DW_LANG_Rust_old:
19091 cu->language = language_rust;
19092 break;
19093 case DW_LANG_Cobol74:
19094 case DW_LANG_Cobol85:
19095 default:
19096 cu->language = language_minimal;
19097 break;
19098 }
19099 cu->language_defn = language_def (cu->language);
19100 }
19101
19102 /* Return the named attribute or NULL if not there. */
19103
19104 static struct attribute *
19105 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19106 {
19107 for (;;)
19108 {
19109 unsigned int i;
19110 struct attribute *spec = NULL;
19111
19112 for (i = 0; i < die->num_attrs; ++i)
19113 {
19114 if (die->attrs[i].name == name)
19115 return &die->attrs[i];
19116 if (die->attrs[i].name == DW_AT_specification
19117 || die->attrs[i].name == DW_AT_abstract_origin)
19118 spec = &die->attrs[i];
19119 }
19120
19121 if (!spec)
19122 break;
19123
19124 die = follow_die_ref (die, spec, &cu);
19125 }
19126
19127 return NULL;
19128 }
19129
19130 /* Return the named attribute or NULL if not there,
19131 but do not follow DW_AT_specification, etc.
19132 This is for use in contexts where we're reading .debug_types dies.
19133 Following DW_AT_specification, DW_AT_abstract_origin will take us
19134 back up the chain, and we want to go down. */
19135
19136 static struct attribute *
19137 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19138 {
19139 unsigned int i;
19140
19141 for (i = 0; i < die->num_attrs; ++i)
19142 if (die->attrs[i].name == name)
19143 return &die->attrs[i];
19144
19145 return NULL;
19146 }
19147
19148 /* Return the string associated with a string-typed attribute, or NULL if it
19149 is either not found or is of an incorrect type. */
19150
19151 static const char *
19152 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19153 {
19154 struct attribute *attr;
19155 const char *str = NULL;
19156
19157 attr = dwarf2_attr (die, name, cu);
19158
19159 if (attr != NULL)
19160 {
19161 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19162 || attr->form == DW_FORM_string
19163 || attr->form == DW_FORM_strx
19164 || attr->form == DW_FORM_strx1
19165 || attr->form == DW_FORM_strx2
19166 || attr->form == DW_FORM_strx3
19167 || attr->form == DW_FORM_strx4
19168 || attr->form == DW_FORM_GNU_str_index
19169 || attr->form == DW_FORM_GNU_strp_alt)
19170 str = DW_STRING (attr);
19171 else
19172 complaint (_("string type expected for attribute %s for "
19173 "DIE at %s in module %s"),
19174 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19175 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19176 }
19177
19178 return str;
19179 }
19180
19181 /* Return the dwo name or NULL if not present. If present, it is in either
19182 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19183 static const char *
19184 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19185 {
19186 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19187 if (dwo_name == nullptr)
19188 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19189 return dwo_name;
19190 }
19191
19192 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19193 and holds a non-zero value. This function should only be used for
19194 DW_FORM_flag or DW_FORM_flag_present attributes. */
19195
19196 static int
19197 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19198 {
19199 struct attribute *attr = dwarf2_attr (die, name, cu);
19200
19201 return (attr && DW_UNSND (attr));
19202 }
19203
19204 static int
19205 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19206 {
19207 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19208 which value is non-zero. However, we have to be careful with
19209 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19210 (via dwarf2_flag_true_p) follows this attribute. So we may
19211 end up accidently finding a declaration attribute that belongs
19212 to a different DIE referenced by the specification attribute,
19213 even though the given DIE does not have a declaration attribute. */
19214 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19215 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19216 }
19217
19218 /* Return the die giving the specification for DIE, if there is
19219 one. *SPEC_CU is the CU containing DIE on input, and the CU
19220 containing the return value on output. If there is no
19221 specification, but there is an abstract origin, that is
19222 returned. */
19223
19224 static struct die_info *
19225 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19226 {
19227 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19228 *spec_cu);
19229
19230 if (spec_attr == NULL)
19231 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19232
19233 if (spec_attr == NULL)
19234 return NULL;
19235 else
19236 return follow_die_ref (die, spec_attr, spec_cu);
19237 }
19238
19239 /* Stub for free_line_header to match void * callback types. */
19240
19241 static void
19242 free_line_header_voidp (void *arg)
19243 {
19244 struct line_header *lh = (struct line_header *) arg;
19245
19246 delete lh;
19247 }
19248
19249 /* A convenience function to find the proper .debug_line section for a CU. */
19250
19251 static struct dwarf2_section_info *
19252 get_debug_line_section (struct dwarf2_cu *cu)
19253 {
19254 struct dwarf2_section_info *section;
19255 struct dwarf2_per_objfile *dwarf2_per_objfile
19256 = cu->per_cu->dwarf2_per_objfile;
19257
19258 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19259 DWO file. */
19260 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19261 section = &cu->dwo_unit->dwo_file->sections.line;
19262 else if (cu->per_cu->is_dwz)
19263 {
19264 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19265
19266 section = &dwz->line;
19267 }
19268 else
19269 section = &dwarf2_per_objfile->line;
19270
19271 return section;
19272 }
19273
19274 /* Read directory or file name entry format, starting with byte of
19275 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19276 entries count and the entries themselves in the described entry
19277 format. */
19278
19279 static void
19280 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19281 bfd *abfd, const gdb_byte **bufp,
19282 struct line_header *lh,
19283 const struct comp_unit_head *cu_header,
19284 void (*callback) (struct line_header *lh,
19285 const char *name,
19286 dir_index d_index,
19287 unsigned int mod_time,
19288 unsigned int length))
19289 {
19290 gdb_byte format_count, formati;
19291 ULONGEST data_count, datai;
19292 const gdb_byte *buf = *bufp;
19293 const gdb_byte *format_header_data;
19294 unsigned int bytes_read;
19295
19296 format_count = read_1_byte (abfd, buf);
19297 buf += 1;
19298 format_header_data = buf;
19299 for (formati = 0; formati < format_count; formati++)
19300 {
19301 read_unsigned_leb128 (abfd, buf, &bytes_read);
19302 buf += bytes_read;
19303 read_unsigned_leb128 (abfd, buf, &bytes_read);
19304 buf += bytes_read;
19305 }
19306
19307 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19308 buf += bytes_read;
19309 for (datai = 0; datai < data_count; datai++)
19310 {
19311 const gdb_byte *format = format_header_data;
19312 struct file_entry fe;
19313
19314 for (formati = 0; formati < format_count; formati++)
19315 {
19316 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19317 format += bytes_read;
19318
19319 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19320 format += bytes_read;
19321
19322 gdb::optional<const char *> string;
19323 gdb::optional<unsigned int> uint;
19324
19325 switch (form)
19326 {
19327 case DW_FORM_string:
19328 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19329 buf += bytes_read;
19330 break;
19331
19332 case DW_FORM_line_strp:
19333 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19334 abfd, buf,
19335 cu_header,
19336 &bytes_read));
19337 buf += bytes_read;
19338 break;
19339
19340 case DW_FORM_data1:
19341 uint.emplace (read_1_byte (abfd, buf));
19342 buf += 1;
19343 break;
19344
19345 case DW_FORM_data2:
19346 uint.emplace (read_2_bytes (abfd, buf));
19347 buf += 2;
19348 break;
19349
19350 case DW_FORM_data4:
19351 uint.emplace (read_4_bytes (abfd, buf));
19352 buf += 4;
19353 break;
19354
19355 case DW_FORM_data8:
19356 uint.emplace (read_8_bytes (abfd, buf));
19357 buf += 8;
19358 break;
19359
19360 case DW_FORM_data16:
19361 /* This is used for MD5, but file_entry does not record MD5s. */
19362 buf += 16;
19363 break;
19364
19365 case DW_FORM_udata:
19366 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19367 buf += bytes_read;
19368 break;
19369
19370 case DW_FORM_block:
19371 /* It is valid only for DW_LNCT_timestamp which is ignored by
19372 current GDB. */
19373 break;
19374 }
19375
19376 switch (content_type)
19377 {
19378 case DW_LNCT_path:
19379 if (string.has_value ())
19380 fe.name = *string;
19381 break;
19382 case DW_LNCT_directory_index:
19383 if (uint.has_value ())
19384 fe.d_index = (dir_index) *uint;
19385 break;
19386 case DW_LNCT_timestamp:
19387 if (uint.has_value ())
19388 fe.mod_time = *uint;
19389 break;
19390 case DW_LNCT_size:
19391 if (uint.has_value ())
19392 fe.length = *uint;
19393 break;
19394 case DW_LNCT_MD5:
19395 break;
19396 default:
19397 complaint (_("Unknown format content type %s"),
19398 pulongest (content_type));
19399 }
19400 }
19401
19402 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
19403 }
19404
19405 *bufp = buf;
19406 }
19407
19408 /* Read the statement program header starting at OFFSET in
19409 .debug_line, or .debug_line.dwo. Return a pointer
19410 to a struct line_header, allocated using xmalloc.
19411 Returns NULL if there is a problem reading the header, e.g., if it
19412 has a version we don't understand.
19413
19414 NOTE: the strings in the include directory and file name tables of
19415 the returned object point into the dwarf line section buffer,
19416 and must not be freed. */
19417
19418 static line_header_up
19419 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19420 {
19421 const gdb_byte *line_ptr;
19422 unsigned int bytes_read, offset_size;
19423 int i;
19424 const char *cur_dir, *cur_file;
19425 struct dwarf2_section_info *section;
19426 bfd *abfd;
19427 struct dwarf2_per_objfile *dwarf2_per_objfile
19428 = cu->per_cu->dwarf2_per_objfile;
19429
19430 section = get_debug_line_section (cu);
19431 section->read (dwarf2_per_objfile->objfile);
19432 if (section->buffer == NULL)
19433 {
19434 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19435 complaint (_("missing .debug_line.dwo section"));
19436 else
19437 complaint (_("missing .debug_line section"));
19438 return 0;
19439 }
19440
19441 /* We can't do this until we know the section is non-empty.
19442 Only then do we know we have such a section. */
19443 abfd = section->get_bfd_owner ();
19444
19445 /* Make sure that at least there's room for the total_length field.
19446 That could be 12 bytes long, but we're just going to fudge that. */
19447 if (to_underlying (sect_off) + 4 >= section->size)
19448 {
19449 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19450 return 0;
19451 }
19452
19453 line_header_up lh (new line_header ());
19454
19455 lh->sect_off = sect_off;
19456 lh->offset_in_dwz = cu->per_cu->is_dwz;
19457
19458 line_ptr = section->buffer + to_underlying (sect_off);
19459
19460 /* Read in the header. */
19461 lh->total_length =
19462 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19463 &bytes_read, &offset_size);
19464 line_ptr += bytes_read;
19465
19466 const gdb_byte *start_here = line_ptr;
19467
19468 if (line_ptr + lh->total_length > (section->buffer + section->size))
19469 {
19470 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19471 return 0;
19472 }
19473 lh->statement_program_end = start_here + lh->total_length;
19474 lh->version = read_2_bytes (abfd, line_ptr);
19475 line_ptr += 2;
19476 if (lh->version > 5)
19477 {
19478 /* This is a version we don't understand. The format could have
19479 changed in ways we don't handle properly so just punt. */
19480 complaint (_("unsupported version in .debug_line section"));
19481 return NULL;
19482 }
19483 if (lh->version >= 5)
19484 {
19485 gdb_byte segment_selector_size;
19486
19487 /* Skip address size. */
19488 read_1_byte (abfd, line_ptr);
19489 line_ptr += 1;
19490
19491 segment_selector_size = read_1_byte (abfd, line_ptr);
19492 line_ptr += 1;
19493 if (segment_selector_size != 0)
19494 {
19495 complaint (_("unsupported segment selector size %u "
19496 "in .debug_line section"),
19497 segment_selector_size);
19498 return NULL;
19499 }
19500 }
19501 lh->header_length = read_offset (abfd, line_ptr, offset_size);
19502 line_ptr += offset_size;
19503 lh->statement_program_start = line_ptr + lh->header_length;
19504 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19505 line_ptr += 1;
19506 if (lh->version >= 4)
19507 {
19508 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19509 line_ptr += 1;
19510 }
19511 else
19512 lh->maximum_ops_per_instruction = 1;
19513
19514 if (lh->maximum_ops_per_instruction == 0)
19515 {
19516 lh->maximum_ops_per_instruction = 1;
19517 complaint (_("invalid maximum_ops_per_instruction "
19518 "in `.debug_line' section"));
19519 }
19520
19521 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19522 line_ptr += 1;
19523 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19524 line_ptr += 1;
19525 lh->line_range = read_1_byte (abfd, line_ptr);
19526 line_ptr += 1;
19527 lh->opcode_base = read_1_byte (abfd, line_ptr);
19528 line_ptr += 1;
19529 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
19530
19531 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19532 for (i = 1; i < lh->opcode_base; ++i)
19533 {
19534 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19535 line_ptr += 1;
19536 }
19537
19538 if (lh->version >= 5)
19539 {
19540 /* Read directory table. */
19541 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19542 &cu->header,
19543 [] (struct line_header *header, const char *name,
19544 dir_index d_index, unsigned int mod_time,
19545 unsigned int length)
19546 {
19547 header->add_include_dir (name);
19548 });
19549
19550 /* Read file name table. */
19551 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19552 &cu->header,
19553 [] (struct line_header *header, const char *name,
19554 dir_index d_index, unsigned int mod_time,
19555 unsigned int length)
19556 {
19557 header->add_file_name (name, d_index, mod_time, length);
19558 });
19559 }
19560 else
19561 {
19562 /* Read directory table. */
19563 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19564 {
19565 line_ptr += bytes_read;
19566 lh->add_include_dir (cur_dir);
19567 }
19568 line_ptr += bytes_read;
19569
19570 /* Read file name table. */
19571 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19572 {
19573 unsigned int mod_time, length;
19574 dir_index d_index;
19575
19576 line_ptr += bytes_read;
19577 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19578 line_ptr += bytes_read;
19579 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19580 line_ptr += bytes_read;
19581 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19582 line_ptr += bytes_read;
19583
19584 lh->add_file_name (cur_file, d_index, mod_time, length);
19585 }
19586 line_ptr += bytes_read;
19587 }
19588
19589 if (line_ptr > (section->buffer + section->size))
19590 complaint (_("line number info header doesn't "
19591 "fit in `.debug_line' section"));
19592
19593 return lh;
19594 }
19595
19596 /* Subroutine of dwarf_decode_lines to simplify it.
19597 Return the file name of the psymtab for the given file_entry.
19598 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19599 If space for the result is malloc'd, *NAME_HOLDER will be set.
19600 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19601
19602 static const char *
19603 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19604 const dwarf2_psymtab *pst,
19605 const char *comp_dir,
19606 gdb::unique_xmalloc_ptr<char> *name_holder)
19607 {
19608 const char *include_name = fe.name;
19609 const char *include_name_to_compare = include_name;
19610 const char *pst_filename;
19611 int file_is_pst;
19612
19613 const char *dir_name = fe.include_dir (lh);
19614
19615 gdb::unique_xmalloc_ptr<char> hold_compare;
19616 if (!IS_ABSOLUTE_PATH (include_name)
19617 && (dir_name != NULL || comp_dir != NULL))
19618 {
19619 /* Avoid creating a duplicate psymtab for PST.
19620 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19621 Before we do the comparison, however, we need to account
19622 for DIR_NAME and COMP_DIR.
19623 First prepend dir_name (if non-NULL). If we still don't
19624 have an absolute path prepend comp_dir (if non-NULL).
19625 However, the directory we record in the include-file's
19626 psymtab does not contain COMP_DIR (to match the
19627 corresponding symtab(s)).
19628
19629 Example:
19630
19631 bash$ cd /tmp
19632 bash$ gcc -g ./hello.c
19633 include_name = "hello.c"
19634 dir_name = "."
19635 DW_AT_comp_dir = comp_dir = "/tmp"
19636 DW_AT_name = "./hello.c"
19637
19638 */
19639
19640 if (dir_name != NULL)
19641 {
19642 name_holder->reset (concat (dir_name, SLASH_STRING,
19643 include_name, (char *) NULL));
19644 include_name = name_holder->get ();
19645 include_name_to_compare = include_name;
19646 }
19647 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19648 {
19649 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19650 include_name, (char *) NULL));
19651 include_name_to_compare = hold_compare.get ();
19652 }
19653 }
19654
19655 pst_filename = pst->filename;
19656 gdb::unique_xmalloc_ptr<char> copied_name;
19657 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19658 {
19659 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19660 pst_filename, (char *) NULL));
19661 pst_filename = copied_name.get ();
19662 }
19663
19664 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19665
19666 if (file_is_pst)
19667 return NULL;
19668 return include_name;
19669 }
19670
19671 /* State machine to track the state of the line number program. */
19672
19673 class lnp_state_machine
19674 {
19675 public:
19676 /* Initialize a machine state for the start of a line number
19677 program. */
19678 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19679 bool record_lines_p);
19680
19681 file_entry *current_file ()
19682 {
19683 /* lh->file_names is 0-based, but the file name numbers in the
19684 statement program are 1-based. */
19685 return m_line_header->file_name_at (m_file);
19686 }
19687
19688 /* Record the line in the state machine. END_SEQUENCE is true if
19689 we're processing the end of a sequence. */
19690 void record_line (bool end_sequence);
19691
19692 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19693 nop-out rest of the lines in this sequence. */
19694 void check_line_address (struct dwarf2_cu *cu,
19695 const gdb_byte *line_ptr,
19696 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19697
19698 void handle_set_discriminator (unsigned int discriminator)
19699 {
19700 m_discriminator = discriminator;
19701 m_line_has_non_zero_discriminator |= discriminator != 0;
19702 }
19703
19704 /* Handle DW_LNE_set_address. */
19705 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19706 {
19707 m_op_index = 0;
19708 address += baseaddr;
19709 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19710 }
19711
19712 /* Handle DW_LNS_advance_pc. */
19713 void handle_advance_pc (CORE_ADDR adjust);
19714
19715 /* Handle a special opcode. */
19716 void handle_special_opcode (unsigned char op_code);
19717
19718 /* Handle DW_LNS_advance_line. */
19719 void handle_advance_line (int line_delta)
19720 {
19721 advance_line (line_delta);
19722 }
19723
19724 /* Handle DW_LNS_set_file. */
19725 void handle_set_file (file_name_index file);
19726
19727 /* Handle DW_LNS_negate_stmt. */
19728 void handle_negate_stmt ()
19729 {
19730 m_is_stmt = !m_is_stmt;
19731 }
19732
19733 /* Handle DW_LNS_const_add_pc. */
19734 void handle_const_add_pc ();
19735
19736 /* Handle DW_LNS_fixed_advance_pc. */
19737 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19738 {
19739 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19740 m_op_index = 0;
19741 }
19742
19743 /* Handle DW_LNS_copy. */
19744 void handle_copy ()
19745 {
19746 record_line (false);
19747 m_discriminator = 0;
19748 }
19749
19750 /* Handle DW_LNE_end_sequence. */
19751 void handle_end_sequence ()
19752 {
19753 m_currently_recording_lines = true;
19754 }
19755
19756 private:
19757 /* Advance the line by LINE_DELTA. */
19758 void advance_line (int line_delta)
19759 {
19760 m_line += line_delta;
19761
19762 if (line_delta != 0)
19763 m_line_has_non_zero_discriminator = m_discriminator != 0;
19764 }
19765
19766 struct dwarf2_cu *m_cu;
19767
19768 gdbarch *m_gdbarch;
19769
19770 /* True if we're recording lines.
19771 Otherwise we're building partial symtabs and are just interested in
19772 finding include files mentioned by the line number program. */
19773 bool m_record_lines_p;
19774
19775 /* The line number header. */
19776 line_header *m_line_header;
19777
19778 /* These are part of the standard DWARF line number state machine,
19779 and initialized according to the DWARF spec. */
19780
19781 unsigned char m_op_index = 0;
19782 /* The line table index of the current file. */
19783 file_name_index m_file = 1;
19784 unsigned int m_line = 1;
19785
19786 /* These are initialized in the constructor. */
19787
19788 CORE_ADDR m_address;
19789 bool m_is_stmt;
19790 unsigned int m_discriminator;
19791
19792 /* Additional bits of state we need to track. */
19793
19794 /* The last file that we called dwarf2_start_subfile for.
19795 This is only used for TLLs. */
19796 unsigned int m_last_file = 0;
19797 /* The last file a line number was recorded for. */
19798 struct subfile *m_last_subfile = NULL;
19799
19800 /* When true, record the lines we decode. */
19801 bool m_currently_recording_lines = false;
19802
19803 /* The last line number that was recorded, used to coalesce
19804 consecutive entries for the same line. This can happen, for
19805 example, when discriminators are present. PR 17276. */
19806 unsigned int m_last_line = 0;
19807 bool m_line_has_non_zero_discriminator = false;
19808 };
19809
19810 void
19811 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19812 {
19813 CORE_ADDR addr_adj = (((m_op_index + adjust)
19814 / m_line_header->maximum_ops_per_instruction)
19815 * m_line_header->minimum_instruction_length);
19816 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19817 m_op_index = ((m_op_index + adjust)
19818 % m_line_header->maximum_ops_per_instruction);
19819 }
19820
19821 void
19822 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19823 {
19824 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19825 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19826 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19827 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19828 / m_line_header->maximum_ops_per_instruction)
19829 * m_line_header->minimum_instruction_length);
19830 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19831 m_op_index = ((m_op_index + adj_opcode_d)
19832 % m_line_header->maximum_ops_per_instruction);
19833
19834 int line_delta = m_line_header->line_base + adj_opcode_r;
19835 advance_line (line_delta);
19836 record_line (false);
19837 m_discriminator = 0;
19838 }
19839
19840 void
19841 lnp_state_machine::handle_set_file (file_name_index file)
19842 {
19843 m_file = file;
19844
19845 const file_entry *fe = current_file ();
19846 if (fe == NULL)
19847 dwarf2_debug_line_missing_file_complaint ();
19848 else if (m_record_lines_p)
19849 {
19850 const char *dir = fe->include_dir (m_line_header);
19851
19852 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19853 m_line_has_non_zero_discriminator = m_discriminator != 0;
19854 dwarf2_start_subfile (m_cu, fe->name, dir);
19855 }
19856 }
19857
19858 void
19859 lnp_state_machine::handle_const_add_pc ()
19860 {
19861 CORE_ADDR adjust
19862 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19863
19864 CORE_ADDR addr_adj
19865 = (((m_op_index + adjust)
19866 / m_line_header->maximum_ops_per_instruction)
19867 * m_line_header->minimum_instruction_length);
19868
19869 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19870 m_op_index = ((m_op_index + adjust)
19871 % m_line_header->maximum_ops_per_instruction);
19872 }
19873
19874 /* Return non-zero if we should add LINE to the line number table.
19875 LINE is the line to add, LAST_LINE is the last line that was added,
19876 LAST_SUBFILE is the subfile for LAST_LINE.
19877 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19878 had a non-zero discriminator.
19879
19880 We have to be careful in the presence of discriminators.
19881 E.g., for this line:
19882
19883 for (i = 0; i < 100000; i++);
19884
19885 clang can emit four line number entries for that one line,
19886 each with a different discriminator.
19887 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19888
19889 However, we want gdb to coalesce all four entries into one.
19890 Otherwise the user could stepi into the middle of the line and
19891 gdb would get confused about whether the pc really was in the
19892 middle of the line.
19893
19894 Things are further complicated by the fact that two consecutive
19895 line number entries for the same line is a heuristic used by gcc
19896 to denote the end of the prologue. So we can't just discard duplicate
19897 entries, we have to be selective about it. The heuristic we use is
19898 that we only collapse consecutive entries for the same line if at least
19899 one of those entries has a non-zero discriminator. PR 17276.
19900
19901 Note: Addresses in the line number state machine can never go backwards
19902 within one sequence, thus this coalescing is ok. */
19903
19904 static int
19905 dwarf_record_line_p (struct dwarf2_cu *cu,
19906 unsigned int line, unsigned int last_line,
19907 int line_has_non_zero_discriminator,
19908 struct subfile *last_subfile)
19909 {
19910 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19911 return 1;
19912 if (line != last_line)
19913 return 1;
19914 /* Same line for the same file that we've seen already.
19915 As a last check, for pr 17276, only record the line if the line
19916 has never had a non-zero discriminator. */
19917 if (!line_has_non_zero_discriminator)
19918 return 1;
19919 return 0;
19920 }
19921
19922 /* Use the CU's builder to record line number LINE beginning at
19923 address ADDRESS in the line table of subfile SUBFILE. */
19924
19925 static void
19926 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19927 unsigned int line, CORE_ADDR address, bool is_stmt,
19928 struct dwarf2_cu *cu)
19929 {
19930 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19931
19932 if (dwarf_line_debug)
19933 {
19934 fprintf_unfiltered (gdb_stdlog,
19935 "Recording line %u, file %s, address %s\n",
19936 line, lbasename (subfile->name),
19937 paddress (gdbarch, address));
19938 }
19939
19940 if (cu != nullptr)
19941 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19942 }
19943
19944 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19945 Mark the end of a set of line number records.
19946 The arguments are the same as for dwarf_record_line_1.
19947 If SUBFILE is NULL the request is ignored. */
19948
19949 static void
19950 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19951 CORE_ADDR address, struct dwarf2_cu *cu)
19952 {
19953 if (subfile == NULL)
19954 return;
19955
19956 if (dwarf_line_debug)
19957 {
19958 fprintf_unfiltered (gdb_stdlog,
19959 "Finishing current line, file %s, address %s\n",
19960 lbasename (subfile->name),
19961 paddress (gdbarch, address));
19962 }
19963
19964 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19965 }
19966
19967 void
19968 lnp_state_machine::record_line (bool end_sequence)
19969 {
19970 if (dwarf_line_debug)
19971 {
19972 fprintf_unfiltered (gdb_stdlog,
19973 "Processing actual line %u: file %u,"
19974 " address %s, is_stmt %u, discrim %u%s\n",
19975 m_line, m_file,
19976 paddress (m_gdbarch, m_address),
19977 m_is_stmt, m_discriminator,
19978 (end_sequence ? "\t(end sequence)" : ""));
19979 }
19980
19981 file_entry *fe = current_file ();
19982
19983 if (fe == NULL)
19984 dwarf2_debug_line_missing_file_complaint ();
19985 /* For now we ignore lines not starting on an instruction boundary.
19986 But not when processing end_sequence for compatibility with the
19987 previous version of the code. */
19988 else if (m_op_index == 0 || end_sequence)
19989 {
19990 fe->included_p = 1;
19991 if (m_record_lines_p)
19992 {
19993 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19994 || end_sequence)
19995 {
19996 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19997 m_currently_recording_lines ? m_cu : nullptr);
19998 }
19999
20000 if (!end_sequence)
20001 {
20002 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20003
20004 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20005 m_line_has_non_zero_discriminator,
20006 m_last_subfile))
20007 {
20008 buildsym_compunit *builder = m_cu->get_builder ();
20009 dwarf_record_line_1 (m_gdbarch,
20010 builder->get_current_subfile (),
20011 m_line, m_address, is_stmt,
20012 m_currently_recording_lines ? m_cu : nullptr);
20013 }
20014 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20015 m_last_line = m_line;
20016 }
20017 }
20018 }
20019 }
20020
20021 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20022 line_header *lh, bool record_lines_p)
20023 {
20024 m_cu = cu;
20025 m_gdbarch = arch;
20026 m_record_lines_p = record_lines_p;
20027 m_line_header = lh;
20028
20029 m_currently_recording_lines = true;
20030
20031 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20032 was a line entry for it so that the backend has a chance to adjust it
20033 and also record it in case it needs it. This is currently used by MIPS
20034 code, cf. `mips_adjust_dwarf2_line'. */
20035 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20036 m_is_stmt = lh->default_is_stmt;
20037 m_discriminator = 0;
20038 }
20039
20040 void
20041 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20042 const gdb_byte *line_ptr,
20043 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20044 {
20045 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20046 the pc range of the CU. However, we restrict the test to only ADDRESS
20047 values of zero to preserve GDB's previous behaviour which is to handle
20048 the specific case of a function being GC'd by the linker. */
20049
20050 if (address == 0 && address < unrelocated_lowpc)
20051 {
20052 /* This line table is for a function which has been
20053 GCd by the linker. Ignore it. PR gdb/12528 */
20054
20055 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20056 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20057
20058 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20059 line_offset, objfile_name (objfile));
20060 m_currently_recording_lines = false;
20061 /* Note: m_currently_recording_lines is left as false until we see
20062 DW_LNE_end_sequence. */
20063 }
20064 }
20065
20066 /* Subroutine of dwarf_decode_lines to simplify it.
20067 Process the line number information in LH.
20068 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20069 program in order to set included_p for every referenced header. */
20070
20071 static void
20072 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20073 const int decode_for_pst_p, CORE_ADDR lowpc)
20074 {
20075 const gdb_byte *line_ptr, *extended_end;
20076 const gdb_byte *line_end;
20077 unsigned int bytes_read, extended_len;
20078 unsigned char op_code, extended_op;
20079 CORE_ADDR baseaddr;
20080 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20081 bfd *abfd = objfile->obfd;
20082 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20083 /* True if we're recording line info (as opposed to building partial
20084 symtabs and just interested in finding include files mentioned by
20085 the line number program). */
20086 bool record_lines_p = !decode_for_pst_p;
20087
20088 baseaddr = objfile->text_section_offset ();
20089
20090 line_ptr = lh->statement_program_start;
20091 line_end = lh->statement_program_end;
20092
20093 /* Read the statement sequences until there's nothing left. */
20094 while (line_ptr < line_end)
20095 {
20096 /* The DWARF line number program state machine. Reset the state
20097 machine at the start of each sequence. */
20098 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20099 bool end_sequence = false;
20100
20101 if (record_lines_p)
20102 {
20103 /* Start a subfile for the current file of the state
20104 machine. */
20105 const file_entry *fe = state_machine.current_file ();
20106
20107 if (fe != NULL)
20108 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20109 }
20110
20111 /* Decode the table. */
20112 while (line_ptr < line_end && !end_sequence)
20113 {
20114 op_code = read_1_byte (abfd, line_ptr);
20115 line_ptr += 1;
20116
20117 if (op_code >= lh->opcode_base)
20118 {
20119 /* Special opcode. */
20120 state_machine.handle_special_opcode (op_code);
20121 }
20122 else switch (op_code)
20123 {
20124 case DW_LNS_extended_op:
20125 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20126 &bytes_read);
20127 line_ptr += bytes_read;
20128 extended_end = line_ptr + extended_len;
20129 extended_op = read_1_byte (abfd, line_ptr);
20130 line_ptr += 1;
20131 switch (extended_op)
20132 {
20133 case DW_LNE_end_sequence:
20134 state_machine.handle_end_sequence ();
20135 end_sequence = true;
20136 break;
20137 case DW_LNE_set_address:
20138 {
20139 CORE_ADDR address
20140 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20141 line_ptr += bytes_read;
20142
20143 state_machine.check_line_address (cu, line_ptr,
20144 lowpc - baseaddr, address);
20145 state_machine.handle_set_address (baseaddr, address);
20146 }
20147 break;
20148 case DW_LNE_define_file:
20149 {
20150 const char *cur_file;
20151 unsigned int mod_time, length;
20152 dir_index dindex;
20153
20154 cur_file = read_direct_string (abfd, line_ptr,
20155 &bytes_read);
20156 line_ptr += bytes_read;
20157 dindex = (dir_index)
20158 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20159 line_ptr += bytes_read;
20160 mod_time =
20161 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20162 line_ptr += bytes_read;
20163 length =
20164 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20165 line_ptr += bytes_read;
20166 lh->add_file_name (cur_file, dindex, mod_time, length);
20167 }
20168 break;
20169 case DW_LNE_set_discriminator:
20170 {
20171 /* The discriminator is not interesting to the
20172 debugger; just ignore it. We still need to
20173 check its value though:
20174 if there are consecutive entries for the same
20175 (non-prologue) line we want to coalesce them.
20176 PR 17276. */
20177 unsigned int discr
20178 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20179 line_ptr += bytes_read;
20180
20181 state_machine.handle_set_discriminator (discr);
20182 }
20183 break;
20184 default:
20185 complaint (_("mangled .debug_line section"));
20186 return;
20187 }
20188 /* Make sure that we parsed the extended op correctly. If e.g.
20189 we expected a different address size than the producer used,
20190 we may have read the wrong number of bytes. */
20191 if (line_ptr != extended_end)
20192 {
20193 complaint (_("mangled .debug_line section"));
20194 return;
20195 }
20196 break;
20197 case DW_LNS_copy:
20198 state_machine.handle_copy ();
20199 break;
20200 case DW_LNS_advance_pc:
20201 {
20202 CORE_ADDR adjust
20203 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20204 line_ptr += bytes_read;
20205
20206 state_machine.handle_advance_pc (adjust);
20207 }
20208 break;
20209 case DW_LNS_advance_line:
20210 {
20211 int line_delta
20212 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20213 line_ptr += bytes_read;
20214
20215 state_machine.handle_advance_line (line_delta);
20216 }
20217 break;
20218 case DW_LNS_set_file:
20219 {
20220 file_name_index file
20221 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20222 &bytes_read);
20223 line_ptr += bytes_read;
20224
20225 state_machine.handle_set_file (file);
20226 }
20227 break;
20228 case DW_LNS_set_column:
20229 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20230 line_ptr += bytes_read;
20231 break;
20232 case DW_LNS_negate_stmt:
20233 state_machine.handle_negate_stmt ();
20234 break;
20235 case DW_LNS_set_basic_block:
20236 break;
20237 /* Add to the address register of the state machine the
20238 address increment value corresponding to special opcode
20239 255. I.e., this value is scaled by the minimum
20240 instruction length since special opcode 255 would have
20241 scaled the increment. */
20242 case DW_LNS_const_add_pc:
20243 state_machine.handle_const_add_pc ();
20244 break;
20245 case DW_LNS_fixed_advance_pc:
20246 {
20247 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20248 line_ptr += 2;
20249
20250 state_machine.handle_fixed_advance_pc (addr_adj);
20251 }
20252 break;
20253 default:
20254 {
20255 /* Unknown standard opcode, ignore it. */
20256 int i;
20257
20258 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20259 {
20260 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20261 line_ptr += bytes_read;
20262 }
20263 }
20264 }
20265 }
20266
20267 if (!end_sequence)
20268 dwarf2_debug_line_missing_end_sequence_complaint ();
20269
20270 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20271 in which case we still finish recording the last line). */
20272 state_machine.record_line (true);
20273 }
20274 }
20275
20276 /* Decode the Line Number Program (LNP) for the given line_header
20277 structure and CU. The actual information extracted and the type
20278 of structures created from the LNP depends on the value of PST.
20279
20280 1. If PST is NULL, then this procedure uses the data from the program
20281 to create all necessary symbol tables, and their linetables.
20282
20283 2. If PST is not NULL, this procedure reads the program to determine
20284 the list of files included by the unit represented by PST, and
20285 builds all the associated partial symbol tables.
20286
20287 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20288 It is used for relative paths in the line table.
20289 NOTE: When processing partial symtabs (pst != NULL),
20290 comp_dir == pst->dirname.
20291
20292 NOTE: It is important that psymtabs have the same file name (via strcmp)
20293 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20294 symtab we don't use it in the name of the psymtabs we create.
20295 E.g. expand_line_sal requires this when finding psymtabs to expand.
20296 A good testcase for this is mb-inline.exp.
20297
20298 LOWPC is the lowest address in CU (or 0 if not known).
20299
20300 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20301 for its PC<->lines mapping information. Otherwise only the filename
20302 table is read in. */
20303
20304 static void
20305 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20306 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20307 CORE_ADDR lowpc, int decode_mapping)
20308 {
20309 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20310 const int decode_for_pst_p = (pst != NULL);
20311
20312 if (decode_mapping)
20313 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20314
20315 if (decode_for_pst_p)
20316 {
20317 /* Now that we're done scanning the Line Header Program, we can
20318 create the psymtab of each included file. */
20319 for (auto &file_entry : lh->file_names ())
20320 if (file_entry.included_p == 1)
20321 {
20322 gdb::unique_xmalloc_ptr<char> name_holder;
20323 const char *include_name =
20324 psymtab_include_file_name (lh, file_entry, pst,
20325 comp_dir, &name_holder);
20326 if (include_name != NULL)
20327 dwarf2_create_include_psymtab (include_name, pst, objfile);
20328 }
20329 }
20330 else
20331 {
20332 /* Make sure a symtab is created for every file, even files
20333 which contain only variables (i.e. no code with associated
20334 line numbers). */
20335 buildsym_compunit *builder = cu->get_builder ();
20336 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20337
20338 for (auto &fe : lh->file_names ())
20339 {
20340 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20341 if (builder->get_current_subfile ()->symtab == NULL)
20342 {
20343 builder->get_current_subfile ()->symtab
20344 = allocate_symtab (cust,
20345 builder->get_current_subfile ()->name);
20346 }
20347 fe.symtab = builder->get_current_subfile ()->symtab;
20348 }
20349 }
20350 }
20351
20352 /* Start a subfile for DWARF. FILENAME is the name of the file and
20353 DIRNAME the name of the source directory which contains FILENAME
20354 or NULL if not known.
20355 This routine tries to keep line numbers from identical absolute and
20356 relative file names in a common subfile.
20357
20358 Using the `list' example from the GDB testsuite, which resides in
20359 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20360 of /srcdir/list0.c yields the following debugging information for list0.c:
20361
20362 DW_AT_name: /srcdir/list0.c
20363 DW_AT_comp_dir: /compdir
20364 files.files[0].name: list0.h
20365 files.files[0].dir: /srcdir
20366 files.files[1].name: list0.c
20367 files.files[1].dir: /srcdir
20368
20369 The line number information for list0.c has to end up in a single
20370 subfile, so that `break /srcdir/list0.c:1' works as expected.
20371 start_subfile will ensure that this happens provided that we pass the
20372 concatenation of files.files[1].dir and files.files[1].name as the
20373 subfile's name. */
20374
20375 static void
20376 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20377 const char *dirname)
20378 {
20379 gdb::unique_xmalloc_ptr<char> copy;
20380
20381 /* In order not to lose the line information directory,
20382 we concatenate it to the filename when it makes sense.
20383 Note that the Dwarf3 standard says (speaking of filenames in line
20384 information): ``The directory index is ignored for file names
20385 that represent full path names''. Thus ignoring dirname in the
20386 `else' branch below isn't an issue. */
20387
20388 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20389 {
20390 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20391 filename = copy.get ();
20392 }
20393
20394 cu->get_builder ()->start_subfile (filename);
20395 }
20396
20397 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20398 buildsym_compunit constructor. */
20399
20400 struct compunit_symtab *
20401 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20402 CORE_ADDR low_pc)
20403 {
20404 gdb_assert (m_builder == nullptr);
20405
20406 m_builder.reset (new struct buildsym_compunit
20407 (per_cu->dwarf2_per_objfile->objfile,
20408 name, comp_dir, language, low_pc));
20409
20410 list_in_scope = get_builder ()->get_file_symbols ();
20411
20412 get_builder ()->record_debugformat ("DWARF 2");
20413 get_builder ()->record_producer (producer);
20414
20415 processing_has_namespace_info = false;
20416
20417 return get_builder ()->get_compunit_symtab ();
20418 }
20419
20420 static void
20421 var_decode_location (struct attribute *attr, struct symbol *sym,
20422 struct dwarf2_cu *cu)
20423 {
20424 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20425 struct comp_unit_head *cu_header = &cu->header;
20426
20427 /* NOTE drow/2003-01-30: There used to be a comment and some special
20428 code here to turn a symbol with DW_AT_external and a
20429 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20430 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20431 with some versions of binutils) where shared libraries could have
20432 relocations against symbols in their debug information - the
20433 minimal symbol would have the right address, but the debug info
20434 would not. It's no longer necessary, because we will explicitly
20435 apply relocations when we read in the debug information now. */
20436
20437 /* A DW_AT_location attribute with no contents indicates that a
20438 variable has been optimized away. */
20439 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20440 {
20441 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20442 return;
20443 }
20444
20445 /* Handle one degenerate form of location expression specially, to
20446 preserve GDB's previous behavior when section offsets are
20447 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20448 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20449
20450 if (attr->form_is_block ()
20451 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20452 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20453 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20454 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20455 && (DW_BLOCK (attr)->size
20456 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20457 {
20458 unsigned int dummy;
20459
20460 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20461 SET_SYMBOL_VALUE_ADDRESS
20462 (sym, cu->header.read_address (objfile->obfd,
20463 DW_BLOCK (attr)->data + 1,
20464 &dummy));
20465 else
20466 SET_SYMBOL_VALUE_ADDRESS
20467 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20468 &dummy));
20469 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20470 fixup_symbol_section (sym, objfile);
20471 SET_SYMBOL_VALUE_ADDRESS
20472 (sym,
20473 SYMBOL_VALUE_ADDRESS (sym)
20474 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20475 return;
20476 }
20477
20478 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20479 expression evaluator, and use LOC_COMPUTED only when necessary
20480 (i.e. when the value of a register or memory location is
20481 referenced, or a thread-local block, etc.). Then again, it might
20482 not be worthwhile. I'm assuming that it isn't unless performance
20483 or memory numbers show me otherwise. */
20484
20485 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20486
20487 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20488 cu->has_loclist = true;
20489 }
20490
20491 /* Given a pointer to a DWARF information entry, figure out if we need
20492 to make a symbol table entry for it, and if so, create a new entry
20493 and return a pointer to it.
20494 If TYPE is NULL, determine symbol type from the die, otherwise
20495 used the passed type.
20496 If SPACE is not NULL, use it to hold the new symbol. If it is
20497 NULL, allocate a new symbol on the objfile's obstack. */
20498
20499 static struct symbol *
20500 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20501 struct symbol *space)
20502 {
20503 struct dwarf2_per_objfile *dwarf2_per_objfile
20504 = cu->per_cu->dwarf2_per_objfile;
20505 struct objfile *objfile = dwarf2_per_objfile->objfile;
20506 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20507 struct symbol *sym = NULL;
20508 const char *name;
20509 struct attribute *attr = NULL;
20510 struct attribute *attr2 = NULL;
20511 CORE_ADDR baseaddr;
20512 struct pending **list_to_add = NULL;
20513
20514 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20515
20516 baseaddr = objfile->text_section_offset ();
20517
20518 name = dwarf2_name (die, cu);
20519 if (name)
20520 {
20521 const char *linkagename;
20522 int suppress_add = 0;
20523
20524 if (space)
20525 sym = space;
20526 else
20527 sym = allocate_symbol (objfile);
20528 OBJSTAT (objfile, n_syms++);
20529
20530 /* Cache this symbol's name and the name's demangled form (if any). */
20531 sym->set_language (cu->language, &objfile->objfile_obstack);
20532 linkagename = dwarf2_physname (name, die, cu);
20533 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20534
20535 /* Fortran does not have mangling standard and the mangling does differ
20536 between gfortran, iFort etc. */
20537 if (cu->language == language_fortran
20538 && symbol_get_demangled_name (sym) == NULL)
20539 symbol_set_demangled_name (sym,
20540 dwarf2_full_name (name, die, cu),
20541 NULL);
20542
20543 /* Default assumptions.
20544 Use the passed type or decode it from the die. */
20545 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20546 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20547 if (type != NULL)
20548 SYMBOL_TYPE (sym) = type;
20549 else
20550 SYMBOL_TYPE (sym) = die_type (die, cu);
20551 attr = dwarf2_attr (die,
20552 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20553 cu);
20554 if (attr != nullptr)
20555 {
20556 SYMBOL_LINE (sym) = DW_UNSND (attr);
20557 }
20558
20559 attr = dwarf2_attr (die,
20560 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20561 cu);
20562 if (attr != nullptr)
20563 {
20564 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20565 struct file_entry *fe;
20566
20567 if (cu->line_header != NULL)
20568 fe = cu->line_header->file_name_at (file_index);
20569 else
20570 fe = NULL;
20571
20572 if (fe == NULL)
20573 complaint (_("file index out of range"));
20574 else
20575 symbol_set_symtab (sym, fe->symtab);
20576 }
20577
20578 switch (die->tag)
20579 {
20580 case DW_TAG_label:
20581 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20582 if (attr != nullptr)
20583 {
20584 CORE_ADDR addr;
20585
20586 addr = attr->value_as_address ();
20587 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20588 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20589 }
20590 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20591 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20592 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20593 add_symbol_to_list (sym, cu->list_in_scope);
20594 break;
20595 case DW_TAG_subprogram:
20596 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20597 finish_block. */
20598 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20599 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20600 if ((attr2 && (DW_UNSND (attr2) != 0))
20601 || cu->language == language_ada
20602 || cu->language == language_fortran)
20603 {
20604 /* Subprograms marked external are stored as a global symbol.
20605 Ada and Fortran subprograms, whether marked external or
20606 not, are always stored as a global symbol, because we want
20607 to be able to access them globally. For instance, we want
20608 to be able to break on a nested subprogram without having
20609 to specify the context. */
20610 list_to_add = cu->get_builder ()->get_global_symbols ();
20611 }
20612 else
20613 {
20614 list_to_add = cu->list_in_scope;
20615 }
20616 break;
20617 case DW_TAG_inlined_subroutine:
20618 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20619 finish_block. */
20620 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20621 SYMBOL_INLINED (sym) = 1;
20622 list_to_add = cu->list_in_scope;
20623 break;
20624 case DW_TAG_template_value_param:
20625 suppress_add = 1;
20626 /* Fall through. */
20627 case DW_TAG_constant:
20628 case DW_TAG_variable:
20629 case DW_TAG_member:
20630 /* Compilation with minimal debug info may result in
20631 variables with missing type entries. Change the
20632 misleading `void' type to something sensible. */
20633 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20634 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20635
20636 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20637 /* In the case of DW_TAG_member, we should only be called for
20638 static const members. */
20639 if (die->tag == DW_TAG_member)
20640 {
20641 /* dwarf2_add_field uses die_is_declaration,
20642 so we do the same. */
20643 gdb_assert (die_is_declaration (die, cu));
20644 gdb_assert (attr);
20645 }
20646 if (attr != nullptr)
20647 {
20648 dwarf2_const_value (attr, sym, cu);
20649 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20650 if (!suppress_add)
20651 {
20652 if (attr2 && (DW_UNSND (attr2) != 0))
20653 list_to_add = cu->get_builder ()->get_global_symbols ();
20654 else
20655 list_to_add = cu->list_in_scope;
20656 }
20657 break;
20658 }
20659 attr = dwarf2_attr (die, DW_AT_location, cu);
20660 if (attr != nullptr)
20661 {
20662 var_decode_location (attr, sym, cu);
20663 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20664
20665 /* Fortran explicitly imports any global symbols to the local
20666 scope by DW_TAG_common_block. */
20667 if (cu->language == language_fortran && die->parent
20668 && die->parent->tag == DW_TAG_common_block)
20669 attr2 = NULL;
20670
20671 if (SYMBOL_CLASS (sym) == LOC_STATIC
20672 && SYMBOL_VALUE_ADDRESS (sym) == 0
20673 && !dwarf2_per_objfile->has_section_at_zero)
20674 {
20675 /* When a static variable is eliminated by the linker,
20676 the corresponding debug information is not stripped
20677 out, but the variable address is set to null;
20678 do not add such variables into symbol table. */
20679 }
20680 else if (attr2 && (DW_UNSND (attr2) != 0))
20681 {
20682 if (SYMBOL_CLASS (sym) == LOC_STATIC
20683 && (objfile->flags & OBJF_MAINLINE) == 0
20684 && dwarf2_per_objfile->can_copy)
20685 {
20686 /* A global static variable might be subject to
20687 copy relocation. We first check for a local
20688 minsym, though, because maybe the symbol was
20689 marked hidden, in which case this would not
20690 apply. */
20691 bound_minimal_symbol found
20692 = (lookup_minimal_symbol_linkage
20693 (sym->linkage_name (), objfile));
20694 if (found.minsym != nullptr)
20695 sym->maybe_copied = 1;
20696 }
20697
20698 /* A variable with DW_AT_external is never static,
20699 but it may be block-scoped. */
20700 list_to_add
20701 = ((cu->list_in_scope
20702 == cu->get_builder ()->get_file_symbols ())
20703 ? cu->get_builder ()->get_global_symbols ()
20704 : cu->list_in_scope);
20705 }
20706 else
20707 list_to_add = cu->list_in_scope;
20708 }
20709 else
20710 {
20711 /* We do not know the address of this symbol.
20712 If it is an external symbol and we have type information
20713 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20714 The address of the variable will then be determined from
20715 the minimal symbol table whenever the variable is
20716 referenced. */
20717 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20718
20719 /* Fortran explicitly imports any global symbols to the local
20720 scope by DW_TAG_common_block. */
20721 if (cu->language == language_fortran && die->parent
20722 && die->parent->tag == DW_TAG_common_block)
20723 {
20724 /* SYMBOL_CLASS doesn't matter here because
20725 read_common_block is going to reset it. */
20726 if (!suppress_add)
20727 list_to_add = cu->list_in_scope;
20728 }
20729 else if (attr2 && (DW_UNSND (attr2) != 0)
20730 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20731 {
20732 /* A variable with DW_AT_external is never static, but it
20733 may be block-scoped. */
20734 list_to_add
20735 = ((cu->list_in_scope
20736 == cu->get_builder ()->get_file_symbols ())
20737 ? cu->get_builder ()->get_global_symbols ()
20738 : cu->list_in_scope);
20739
20740 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20741 }
20742 else if (!die_is_declaration (die, cu))
20743 {
20744 /* Use the default LOC_OPTIMIZED_OUT class. */
20745 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20746 if (!suppress_add)
20747 list_to_add = cu->list_in_scope;
20748 }
20749 }
20750 break;
20751 case DW_TAG_formal_parameter:
20752 {
20753 /* If we are inside a function, mark this as an argument. If
20754 not, we might be looking at an argument to an inlined function
20755 when we do not have enough information to show inlined frames;
20756 pretend it's a local variable in that case so that the user can
20757 still see it. */
20758 struct context_stack *curr
20759 = cu->get_builder ()->get_current_context_stack ();
20760 if (curr != nullptr && curr->name != nullptr)
20761 SYMBOL_IS_ARGUMENT (sym) = 1;
20762 attr = dwarf2_attr (die, DW_AT_location, cu);
20763 if (attr != nullptr)
20764 {
20765 var_decode_location (attr, sym, cu);
20766 }
20767 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20768 if (attr != nullptr)
20769 {
20770 dwarf2_const_value (attr, sym, cu);
20771 }
20772
20773 list_to_add = cu->list_in_scope;
20774 }
20775 break;
20776 case DW_TAG_unspecified_parameters:
20777 /* From varargs functions; gdb doesn't seem to have any
20778 interest in this information, so just ignore it for now.
20779 (FIXME?) */
20780 break;
20781 case DW_TAG_template_type_param:
20782 suppress_add = 1;
20783 /* Fall through. */
20784 case DW_TAG_class_type:
20785 case DW_TAG_interface_type:
20786 case DW_TAG_structure_type:
20787 case DW_TAG_union_type:
20788 case DW_TAG_set_type:
20789 case DW_TAG_enumeration_type:
20790 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20791 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20792
20793 {
20794 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20795 really ever be static objects: otherwise, if you try
20796 to, say, break of a class's method and you're in a file
20797 which doesn't mention that class, it won't work unless
20798 the check for all static symbols in lookup_symbol_aux
20799 saves you. See the OtherFileClass tests in
20800 gdb.c++/namespace.exp. */
20801
20802 if (!suppress_add)
20803 {
20804 buildsym_compunit *builder = cu->get_builder ();
20805 list_to_add
20806 = (cu->list_in_scope == builder->get_file_symbols ()
20807 && cu->language == language_cplus
20808 ? builder->get_global_symbols ()
20809 : cu->list_in_scope);
20810
20811 /* The semantics of C++ state that "struct foo {
20812 ... }" also defines a typedef for "foo". */
20813 if (cu->language == language_cplus
20814 || cu->language == language_ada
20815 || cu->language == language_d
20816 || cu->language == language_rust)
20817 {
20818 /* The symbol's name is already allocated along
20819 with this objfile, so we don't need to
20820 duplicate it for the type. */
20821 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20822 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20823 }
20824 }
20825 }
20826 break;
20827 case DW_TAG_typedef:
20828 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20829 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20830 list_to_add = cu->list_in_scope;
20831 break;
20832 case DW_TAG_base_type:
20833 case DW_TAG_subrange_type:
20834 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20835 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20836 list_to_add = cu->list_in_scope;
20837 break;
20838 case DW_TAG_enumerator:
20839 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20840 if (attr != nullptr)
20841 {
20842 dwarf2_const_value (attr, sym, cu);
20843 }
20844 {
20845 /* NOTE: carlton/2003-11-10: See comment above in the
20846 DW_TAG_class_type, etc. block. */
20847
20848 list_to_add
20849 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20850 && cu->language == language_cplus
20851 ? cu->get_builder ()->get_global_symbols ()
20852 : cu->list_in_scope);
20853 }
20854 break;
20855 case DW_TAG_imported_declaration:
20856 case DW_TAG_namespace:
20857 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20858 list_to_add = cu->get_builder ()->get_global_symbols ();
20859 break;
20860 case DW_TAG_module:
20861 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20862 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20863 list_to_add = cu->get_builder ()->get_global_symbols ();
20864 break;
20865 case DW_TAG_common_block:
20866 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20867 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20868 add_symbol_to_list (sym, cu->list_in_scope);
20869 break;
20870 default:
20871 /* Not a tag we recognize. Hopefully we aren't processing
20872 trash data, but since we must specifically ignore things
20873 we don't recognize, there is nothing else we should do at
20874 this point. */
20875 complaint (_("unsupported tag: '%s'"),
20876 dwarf_tag_name (die->tag));
20877 break;
20878 }
20879
20880 if (suppress_add)
20881 {
20882 sym->hash_next = objfile->template_symbols;
20883 objfile->template_symbols = sym;
20884 list_to_add = NULL;
20885 }
20886
20887 if (list_to_add != NULL)
20888 add_symbol_to_list (sym, list_to_add);
20889
20890 /* For the benefit of old versions of GCC, check for anonymous
20891 namespaces based on the demangled name. */
20892 if (!cu->processing_has_namespace_info
20893 && cu->language == language_cplus)
20894 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20895 }
20896 return (sym);
20897 }
20898
20899 /* Given an attr with a DW_FORM_dataN value in host byte order,
20900 zero-extend it as appropriate for the symbol's type. The DWARF
20901 standard (v4) is not entirely clear about the meaning of using
20902 DW_FORM_dataN for a constant with a signed type, where the type is
20903 wider than the data. The conclusion of a discussion on the DWARF
20904 list was that this is unspecified. We choose to always zero-extend
20905 because that is the interpretation long in use by GCC. */
20906
20907 static gdb_byte *
20908 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20909 struct dwarf2_cu *cu, LONGEST *value, int bits)
20910 {
20911 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20912 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20913 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20914 LONGEST l = DW_UNSND (attr);
20915
20916 if (bits < sizeof (*value) * 8)
20917 {
20918 l &= ((LONGEST) 1 << bits) - 1;
20919 *value = l;
20920 }
20921 else if (bits == sizeof (*value) * 8)
20922 *value = l;
20923 else
20924 {
20925 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20926 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20927 return bytes;
20928 }
20929
20930 return NULL;
20931 }
20932
20933 /* Read a constant value from an attribute. Either set *VALUE, or if
20934 the value does not fit in *VALUE, set *BYTES - either already
20935 allocated on the objfile obstack, or newly allocated on OBSTACK,
20936 or, set *BATON, if we translated the constant to a location
20937 expression. */
20938
20939 static void
20940 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20941 const char *name, struct obstack *obstack,
20942 struct dwarf2_cu *cu,
20943 LONGEST *value, const gdb_byte **bytes,
20944 struct dwarf2_locexpr_baton **baton)
20945 {
20946 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20947 struct comp_unit_head *cu_header = &cu->header;
20948 struct dwarf_block *blk;
20949 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20950 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20951
20952 *value = 0;
20953 *bytes = NULL;
20954 *baton = NULL;
20955
20956 switch (attr->form)
20957 {
20958 case DW_FORM_addr:
20959 case DW_FORM_addrx:
20960 case DW_FORM_GNU_addr_index:
20961 {
20962 gdb_byte *data;
20963
20964 if (TYPE_LENGTH (type) != cu_header->addr_size)
20965 dwarf2_const_value_length_mismatch_complaint (name,
20966 cu_header->addr_size,
20967 TYPE_LENGTH (type));
20968 /* Symbols of this form are reasonably rare, so we just
20969 piggyback on the existing location code rather than writing
20970 a new implementation of symbol_computed_ops. */
20971 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20972 (*baton)->per_cu = cu->per_cu;
20973 gdb_assert ((*baton)->per_cu);
20974
20975 (*baton)->size = 2 + cu_header->addr_size;
20976 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20977 (*baton)->data = data;
20978
20979 data[0] = DW_OP_addr;
20980 store_unsigned_integer (&data[1], cu_header->addr_size,
20981 byte_order, DW_ADDR (attr));
20982 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20983 }
20984 break;
20985 case DW_FORM_string:
20986 case DW_FORM_strp:
20987 case DW_FORM_strx:
20988 case DW_FORM_GNU_str_index:
20989 case DW_FORM_GNU_strp_alt:
20990 /* DW_STRING is already allocated on the objfile obstack, point
20991 directly to it. */
20992 *bytes = (const gdb_byte *) DW_STRING (attr);
20993 break;
20994 case DW_FORM_block1:
20995 case DW_FORM_block2:
20996 case DW_FORM_block4:
20997 case DW_FORM_block:
20998 case DW_FORM_exprloc:
20999 case DW_FORM_data16:
21000 blk = DW_BLOCK (attr);
21001 if (TYPE_LENGTH (type) != blk->size)
21002 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21003 TYPE_LENGTH (type));
21004 *bytes = blk->data;
21005 break;
21006
21007 /* The DW_AT_const_value attributes are supposed to carry the
21008 symbol's value "represented as it would be on the target
21009 architecture." By the time we get here, it's already been
21010 converted to host endianness, so we just need to sign- or
21011 zero-extend it as appropriate. */
21012 case DW_FORM_data1:
21013 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21014 break;
21015 case DW_FORM_data2:
21016 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21017 break;
21018 case DW_FORM_data4:
21019 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21020 break;
21021 case DW_FORM_data8:
21022 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21023 break;
21024
21025 case DW_FORM_sdata:
21026 case DW_FORM_implicit_const:
21027 *value = DW_SND (attr);
21028 break;
21029
21030 case DW_FORM_udata:
21031 *value = DW_UNSND (attr);
21032 break;
21033
21034 default:
21035 complaint (_("unsupported const value attribute form: '%s'"),
21036 dwarf_form_name (attr->form));
21037 *value = 0;
21038 break;
21039 }
21040 }
21041
21042
21043 /* Copy constant value from an attribute to a symbol. */
21044
21045 static void
21046 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21047 struct dwarf2_cu *cu)
21048 {
21049 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21050 LONGEST value;
21051 const gdb_byte *bytes;
21052 struct dwarf2_locexpr_baton *baton;
21053
21054 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21055 sym->print_name (),
21056 &objfile->objfile_obstack, cu,
21057 &value, &bytes, &baton);
21058
21059 if (baton != NULL)
21060 {
21061 SYMBOL_LOCATION_BATON (sym) = baton;
21062 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21063 }
21064 else if (bytes != NULL)
21065 {
21066 SYMBOL_VALUE_BYTES (sym) = bytes;
21067 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21068 }
21069 else
21070 {
21071 SYMBOL_VALUE (sym) = value;
21072 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21073 }
21074 }
21075
21076 /* Return the type of the die in question using its DW_AT_type attribute. */
21077
21078 static struct type *
21079 die_type (struct die_info *die, struct dwarf2_cu *cu)
21080 {
21081 struct attribute *type_attr;
21082
21083 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21084 if (!type_attr)
21085 {
21086 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21087 /* A missing DW_AT_type represents a void type. */
21088 return objfile_type (objfile)->builtin_void;
21089 }
21090
21091 return lookup_die_type (die, type_attr, cu);
21092 }
21093
21094 /* True iff CU's producer generates GNAT Ada auxiliary information
21095 that allows to find parallel types through that information instead
21096 of having to do expensive parallel lookups by type name. */
21097
21098 static int
21099 need_gnat_info (struct dwarf2_cu *cu)
21100 {
21101 /* Assume that the Ada compiler was GNAT, which always produces
21102 the auxiliary information. */
21103 return (cu->language == language_ada);
21104 }
21105
21106 /* Return the auxiliary type of the die in question using its
21107 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21108 attribute is not present. */
21109
21110 static struct type *
21111 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21112 {
21113 struct attribute *type_attr;
21114
21115 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21116 if (!type_attr)
21117 return NULL;
21118
21119 return lookup_die_type (die, type_attr, cu);
21120 }
21121
21122 /* If DIE has a descriptive_type attribute, then set the TYPE's
21123 descriptive type accordingly. */
21124
21125 static void
21126 set_descriptive_type (struct type *type, struct die_info *die,
21127 struct dwarf2_cu *cu)
21128 {
21129 struct type *descriptive_type = die_descriptive_type (die, cu);
21130
21131 if (descriptive_type)
21132 {
21133 ALLOCATE_GNAT_AUX_TYPE (type);
21134 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21135 }
21136 }
21137
21138 /* Return the containing type of the die in question using its
21139 DW_AT_containing_type attribute. */
21140
21141 static struct type *
21142 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21143 {
21144 struct attribute *type_attr;
21145 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21146
21147 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21148 if (!type_attr)
21149 error (_("Dwarf Error: Problem turning containing type into gdb type "
21150 "[in module %s]"), objfile_name (objfile));
21151
21152 return lookup_die_type (die, type_attr, cu);
21153 }
21154
21155 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21156
21157 static struct type *
21158 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21159 {
21160 struct dwarf2_per_objfile *dwarf2_per_objfile
21161 = cu->per_cu->dwarf2_per_objfile;
21162 struct objfile *objfile = dwarf2_per_objfile->objfile;
21163 char *saved;
21164
21165 std::string message
21166 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21167 objfile_name (objfile),
21168 sect_offset_str (cu->header.sect_off),
21169 sect_offset_str (die->sect_off));
21170 saved = obstack_strdup (&objfile->objfile_obstack, message);
21171
21172 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21173 }
21174
21175 /* Look up the type of DIE in CU using its type attribute ATTR.
21176 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21177 DW_AT_containing_type.
21178 If there is no type substitute an error marker. */
21179
21180 static struct type *
21181 lookup_die_type (struct die_info *die, const struct attribute *attr,
21182 struct dwarf2_cu *cu)
21183 {
21184 struct dwarf2_per_objfile *dwarf2_per_objfile
21185 = cu->per_cu->dwarf2_per_objfile;
21186 struct objfile *objfile = dwarf2_per_objfile->objfile;
21187 struct type *this_type;
21188
21189 gdb_assert (attr->name == DW_AT_type
21190 || attr->name == DW_AT_GNAT_descriptive_type
21191 || attr->name == DW_AT_containing_type);
21192
21193 /* First see if we have it cached. */
21194
21195 if (attr->form == DW_FORM_GNU_ref_alt)
21196 {
21197 struct dwarf2_per_cu_data *per_cu;
21198 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21199
21200 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21201 dwarf2_per_objfile);
21202 this_type = get_die_type_at_offset (sect_off, per_cu);
21203 }
21204 else if (attr->form_is_ref ())
21205 {
21206 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21207
21208 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21209 }
21210 else if (attr->form == DW_FORM_ref_sig8)
21211 {
21212 ULONGEST signature = DW_SIGNATURE (attr);
21213
21214 return get_signatured_type (die, signature, cu);
21215 }
21216 else
21217 {
21218 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21219 " at %s [in module %s]"),
21220 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21221 objfile_name (objfile));
21222 return build_error_marker_type (cu, die);
21223 }
21224
21225 /* If not cached we need to read it in. */
21226
21227 if (this_type == NULL)
21228 {
21229 struct die_info *type_die = NULL;
21230 struct dwarf2_cu *type_cu = cu;
21231
21232 if (attr->form_is_ref ())
21233 type_die = follow_die_ref (die, attr, &type_cu);
21234 if (type_die == NULL)
21235 return build_error_marker_type (cu, die);
21236 /* If we find the type now, it's probably because the type came
21237 from an inter-CU reference and the type's CU got expanded before
21238 ours. */
21239 this_type = read_type_die (type_die, type_cu);
21240 }
21241
21242 /* If we still don't have a type use an error marker. */
21243
21244 if (this_type == NULL)
21245 return build_error_marker_type (cu, die);
21246
21247 return this_type;
21248 }
21249
21250 /* Return the type in DIE, CU.
21251 Returns NULL for invalid types.
21252
21253 This first does a lookup in die_type_hash,
21254 and only reads the die in if necessary.
21255
21256 NOTE: This can be called when reading in partial or full symbols. */
21257
21258 static struct type *
21259 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21260 {
21261 struct type *this_type;
21262
21263 this_type = get_die_type (die, cu);
21264 if (this_type)
21265 return this_type;
21266
21267 return read_type_die_1 (die, cu);
21268 }
21269
21270 /* Read the type in DIE, CU.
21271 Returns NULL for invalid types. */
21272
21273 static struct type *
21274 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21275 {
21276 struct type *this_type = NULL;
21277
21278 switch (die->tag)
21279 {
21280 case DW_TAG_class_type:
21281 case DW_TAG_interface_type:
21282 case DW_TAG_structure_type:
21283 case DW_TAG_union_type:
21284 this_type = read_structure_type (die, cu);
21285 break;
21286 case DW_TAG_enumeration_type:
21287 this_type = read_enumeration_type (die, cu);
21288 break;
21289 case DW_TAG_subprogram:
21290 case DW_TAG_subroutine_type:
21291 case DW_TAG_inlined_subroutine:
21292 this_type = read_subroutine_type (die, cu);
21293 break;
21294 case DW_TAG_array_type:
21295 this_type = read_array_type (die, cu);
21296 break;
21297 case DW_TAG_set_type:
21298 this_type = read_set_type (die, cu);
21299 break;
21300 case DW_TAG_pointer_type:
21301 this_type = read_tag_pointer_type (die, cu);
21302 break;
21303 case DW_TAG_ptr_to_member_type:
21304 this_type = read_tag_ptr_to_member_type (die, cu);
21305 break;
21306 case DW_TAG_reference_type:
21307 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21308 break;
21309 case DW_TAG_rvalue_reference_type:
21310 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21311 break;
21312 case DW_TAG_const_type:
21313 this_type = read_tag_const_type (die, cu);
21314 break;
21315 case DW_TAG_volatile_type:
21316 this_type = read_tag_volatile_type (die, cu);
21317 break;
21318 case DW_TAG_restrict_type:
21319 this_type = read_tag_restrict_type (die, cu);
21320 break;
21321 case DW_TAG_string_type:
21322 this_type = read_tag_string_type (die, cu);
21323 break;
21324 case DW_TAG_typedef:
21325 this_type = read_typedef (die, cu);
21326 break;
21327 case DW_TAG_subrange_type:
21328 this_type = read_subrange_type (die, cu);
21329 break;
21330 case DW_TAG_base_type:
21331 this_type = read_base_type (die, cu);
21332 break;
21333 case DW_TAG_unspecified_type:
21334 this_type = read_unspecified_type (die, cu);
21335 break;
21336 case DW_TAG_namespace:
21337 this_type = read_namespace_type (die, cu);
21338 break;
21339 case DW_TAG_module:
21340 this_type = read_module_type (die, cu);
21341 break;
21342 case DW_TAG_atomic_type:
21343 this_type = read_tag_atomic_type (die, cu);
21344 break;
21345 default:
21346 complaint (_("unexpected tag in read_type_die: '%s'"),
21347 dwarf_tag_name (die->tag));
21348 break;
21349 }
21350
21351 return this_type;
21352 }
21353
21354 /* See if we can figure out if the class lives in a namespace. We do
21355 this by looking for a member function; its demangled name will
21356 contain namespace info, if there is any.
21357 Return the computed name or NULL.
21358 Space for the result is allocated on the objfile's obstack.
21359 This is the full-die version of guess_partial_die_structure_name.
21360 In this case we know DIE has no useful parent. */
21361
21362 static const char *
21363 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21364 {
21365 struct die_info *spec_die;
21366 struct dwarf2_cu *spec_cu;
21367 struct die_info *child;
21368 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21369
21370 spec_cu = cu;
21371 spec_die = die_specification (die, &spec_cu);
21372 if (spec_die != NULL)
21373 {
21374 die = spec_die;
21375 cu = spec_cu;
21376 }
21377
21378 for (child = die->child;
21379 child != NULL;
21380 child = child->sibling)
21381 {
21382 if (child->tag == DW_TAG_subprogram)
21383 {
21384 const char *linkage_name = dw2_linkage_name (child, cu);
21385
21386 if (linkage_name != NULL)
21387 {
21388 gdb::unique_xmalloc_ptr<char> actual_name
21389 (language_class_name_from_physname (cu->language_defn,
21390 linkage_name));
21391 const char *name = NULL;
21392
21393 if (actual_name != NULL)
21394 {
21395 const char *die_name = dwarf2_name (die, cu);
21396
21397 if (die_name != NULL
21398 && strcmp (die_name, actual_name.get ()) != 0)
21399 {
21400 /* Strip off the class name from the full name.
21401 We want the prefix. */
21402 int die_name_len = strlen (die_name);
21403 int actual_name_len = strlen (actual_name.get ());
21404 const char *ptr = actual_name.get ();
21405
21406 /* Test for '::' as a sanity check. */
21407 if (actual_name_len > die_name_len + 2
21408 && ptr[actual_name_len - die_name_len - 1] == ':')
21409 name = obstack_strndup (
21410 &objfile->per_bfd->storage_obstack,
21411 ptr, actual_name_len - die_name_len - 2);
21412 }
21413 }
21414 return name;
21415 }
21416 }
21417 }
21418
21419 return NULL;
21420 }
21421
21422 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21423 prefix part in such case. See
21424 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21425
21426 static const char *
21427 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21428 {
21429 struct attribute *attr;
21430 const char *base;
21431
21432 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21433 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21434 return NULL;
21435
21436 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21437 return NULL;
21438
21439 attr = dw2_linkage_name_attr (die, cu);
21440 if (attr == NULL || DW_STRING (attr) == NULL)
21441 return NULL;
21442
21443 /* dwarf2_name had to be already called. */
21444 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21445
21446 /* Strip the base name, keep any leading namespaces/classes. */
21447 base = strrchr (DW_STRING (attr), ':');
21448 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21449 return "";
21450
21451 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21452 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21453 DW_STRING (attr),
21454 &base[-1] - DW_STRING (attr));
21455 }
21456
21457 /* Return the name of the namespace/class that DIE is defined within,
21458 or "" if we can't tell. The caller should not xfree the result.
21459
21460 For example, if we're within the method foo() in the following
21461 code:
21462
21463 namespace N {
21464 class C {
21465 void foo () {
21466 }
21467 };
21468 }
21469
21470 then determine_prefix on foo's die will return "N::C". */
21471
21472 static const char *
21473 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21474 {
21475 struct dwarf2_per_objfile *dwarf2_per_objfile
21476 = cu->per_cu->dwarf2_per_objfile;
21477 struct die_info *parent, *spec_die;
21478 struct dwarf2_cu *spec_cu;
21479 struct type *parent_type;
21480 const char *retval;
21481
21482 if (cu->language != language_cplus
21483 && cu->language != language_fortran && cu->language != language_d
21484 && cu->language != language_rust)
21485 return "";
21486
21487 retval = anonymous_struct_prefix (die, cu);
21488 if (retval)
21489 return retval;
21490
21491 /* We have to be careful in the presence of DW_AT_specification.
21492 For example, with GCC 3.4, given the code
21493
21494 namespace N {
21495 void foo() {
21496 // Definition of N::foo.
21497 }
21498 }
21499
21500 then we'll have a tree of DIEs like this:
21501
21502 1: DW_TAG_compile_unit
21503 2: DW_TAG_namespace // N
21504 3: DW_TAG_subprogram // declaration of N::foo
21505 4: DW_TAG_subprogram // definition of N::foo
21506 DW_AT_specification // refers to die #3
21507
21508 Thus, when processing die #4, we have to pretend that we're in
21509 the context of its DW_AT_specification, namely the contex of die
21510 #3. */
21511 spec_cu = cu;
21512 spec_die = die_specification (die, &spec_cu);
21513 if (spec_die == NULL)
21514 parent = die->parent;
21515 else
21516 {
21517 parent = spec_die->parent;
21518 cu = spec_cu;
21519 }
21520
21521 if (parent == NULL)
21522 return "";
21523 else if (parent->building_fullname)
21524 {
21525 const char *name;
21526 const char *parent_name;
21527
21528 /* It has been seen on RealView 2.2 built binaries,
21529 DW_TAG_template_type_param types actually _defined_ as
21530 children of the parent class:
21531
21532 enum E {};
21533 template class <class Enum> Class{};
21534 Class<enum E> class_e;
21535
21536 1: DW_TAG_class_type (Class)
21537 2: DW_TAG_enumeration_type (E)
21538 3: DW_TAG_enumerator (enum1:0)
21539 3: DW_TAG_enumerator (enum2:1)
21540 ...
21541 2: DW_TAG_template_type_param
21542 DW_AT_type DW_FORM_ref_udata (E)
21543
21544 Besides being broken debug info, it can put GDB into an
21545 infinite loop. Consider:
21546
21547 When we're building the full name for Class<E>, we'll start
21548 at Class, and go look over its template type parameters,
21549 finding E. We'll then try to build the full name of E, and
21550 reach here. We're now trying to build the full name of E,
21551 and look over the parent DIE for containing scope. In the
21552 broken case, if we followed the parent DIE of E, we'd again
21553 find Class, and once again go look at its template type
21554 arguments, etc., etc. Simply don't consider such parent die
21555 as source-level parent of this die (it can't be, the language
21556 doesn't allow it), and break the loop here. */
21557 name = dwarf2_name (die, cu);
21558 parent_name = dwarf2_name (parent, cu);
21559 complaint (_("template param type '%s' defined within parent '%s'"),
21560 name ? name : "<unknown>",
21561 parent_name ? parent_name : "<unknown>");
21562 return "";
21563 }
21564 else
21565 switch (parent->tag)
21566 {
21567 case DW_TAG_namespace:
21568 parent_type = read_type_die (parent, cu);
21569 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21570 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21571 Work around this problem here. */
21572 if (cu->language == language_cplus
21573 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21574 return "";
21575 /* We give a name to even anonymous namespaces. */
21576 return TYPE_NAME (parent_type);
21577 case DW_TAG_class_type:
21578 case DW_TAG_interface_type:
21579 case DW_TAG_structure_type:
21580 case DW_TAG_union_type:
21581 case DW_TAG_module:
21582 parent_type = read_type_die (parent, cu);
21583 if (TYPE_NAME (parent_type) != NULL)
21584 return TYPE_NAME (parent_type);
21585 else
21586 /* An anonymous structure is only allowed non-static data
21587 members; no typedefs, no member functions, et cetera.
21588 So it does not need a prefix. */
21589 return "";
21590 case DW_TAG_compile_unit:
21591 case DW_TAG_partial_unit:
21592 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21593 if (cu->language == language_cplus
21594 && !dwarf2_per_objfile->types.empty ()
21595 && die->child != NULL
21596 && (die->tag == DW_TAG_class_type
21597 || die->tag == DW_TAG_structure_type
21598 || die->tag == DW_TAG_union_type))
21599 {
21600 const char *name = guess_full_die_structure_name (die, cu);
21601 if (name != NULL)
21602 return name;
21603 }
21604 return "";
21605 case DW_TAG_subprogram:
21606 /* Nested subroutines in Fortran get a prefix with the name
21607 of the parent's subroutine. */
21608 if (cu->language == language_fortran)
21609 {
21610 if ((die->tag == DW_TAG_subprogram)
21611 && (dwarf2_name (parent, cu) != NULL))
21612 return dwarf2_name (parent, cu);
21613 }
21614 return determine_prefix (parent, cu);
21615 case DW_TAG_enumeration_type:
21616 parent_type = read_type_die (parent, cu);
21617 if (TYPE_DECLARED_CLASS (parent_type))
21618 {
21619 if (TYPE_NAME (parent_type) != NULL)
21620 return TYPE_NAME (parent_type);
21621 return "";
21622 }
21623 /* Fall through. */
21624 default:
21625 return determine_prefix (parent, cu);
21626 }
21627 }
21628
21629 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21630 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21631 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21632 an obconcat, otherwise allocate storage for the result. The CU argument is
21633 used to determine the language and hence, the appropriate separator. */
21634
21635 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21636
21637 static char *
21638 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21639 int physname, struct dwarf2_cu *cu)
21640 {
21641 const char *lead = "";
21642 const char *sep;
21643
21644 if (suffix == NULL || suffix[0] == '\0'
21645 || prefix == NULL || prefix[0] == '\0')
21646 sep = "";
21647 else if (cu->language == language_d)
21648 {
21649 /* For D, the 'main' function could be defined in any module, but it
21650 should never be prefixed. */
21651 if (strcmp (suffix, "D main") == 0)
21652 {
21653 prefix = "";
21654 sep = "";
21655 }
21656 else
21657 sep = ".";
21658 }
21659 else if (cu->language == language_fortran && physname)
21660 {
21661 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21662 DW_AT_MIPS_linkage_name is preferred and used instead. */
21663
21664 lead = "__";
21665 sep = "_MOD_";
21666 }
21667 else
21668 sep = "::";
21669
21670 if (prefix == NULL)
21671 prefix = "";
21672 if (suffix == NULL)
21673 suffix = "";
21674
21675 if (obs == NULL)
21676 {
21677 char *retval
21678 = ((char *)
21679 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21680
21681 strcpy (retval, lead);
21682 strcat (retval, prefix);
21683 strcat (retval, sep);
21684 strcat (retval, suffix);
21685 return retval;
21686 }
21687 else
21688 {
21689 /* We have an obstack. */
21690 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21691 }
21692 }
21693
21694 /* Return sibling of die, NULL if no sibling. */
21695
21696 static struct die_info *
21697 sibling_die (struct die_info *die)
21698 {
21699 return die->sibling;
21700 }
21701
21702 /* Get name of a die, return NULL if not found. */
21703
21704 static const char *
21705 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21706 struct objfile *objfile)
21707 {
21708 if (name && cu->language == language_cplus)
21709 {
21710 std::string canon_name = cp_canonicalize_string (name);
21711
21712 if (!canon_name.empty ())
21713 {
21714 if (canon_name != name)
21715 name = objfile->intern (canon_name);
21716 }
21717 }
21718
21719 return name;
21720 }
21721
21722 /* Get name of a die, return NULL if not found.
21723 Anonymous namespaces are converted to their magic string. */
21724
21725 static const char *
21726 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21727 {
21728 struct attribute *attr;
21729 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21730
21731 attr = dwarf2_attr (die, DW_AT_name, cu);
21732 if ((!attr || !DW_STRING (attr))
21733 && die->tag != DW_TAG_namespace
21734 && die->tag != DW_TAG_class_type
21735 && die->tag != DW_TAG_interface_type
21736 && die->tag != DW_TAG_structure_type
21737 && die->tag != DW_TAG_union_type)
21738 return NULL;
21739
21740 switch (die->tag)
21741 {
21742 case DW_TAG_compile_unit:
21743 case DW_TAG_partial_unit:
21744 /* Compilation units have a DW_AT_name that is a filename, not
21745 a source language identifier. */
21746 case DW_TAG_enumeration_type:
21747 case DW_TAG_enumerator:
21748 /* These tags always have simple identifiers already; no need
21749 to canonicalize them. */
21750 return DW_STRING (attr);
21751
21752 case DW_TAG_namespace:
21753 if (attr != NULL && DW_STRING (attr) != NULL)
21754 return DW_STRING (attr);
21755 return CP_ANONYMOUS_NAMESPACE_STR;
21756
21757 case DW_TAG_class_type:
21758 case DW_TAG_interface_type:
21759 case DW_TAG_structure_type:
21760 case DW_TAG_union_type:
21761 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21762 structures or unions. These were of the form "._%d" in GCC 4.1,
21763 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21764 and GCC 4.4. We work around this problem by ignoring these. */
21765 if (attr && DW_STRING (attr)
21766 && (startswith (DW_STRING (attr), "._")
21767 || startswith (DW_STRING (attr), "<anonymous")))
21768 return NULL;
21769
21770 /* GCC might emit a nameless typedef that has a linkage name. See
21771 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21772 if (!attr || DW_STRING (attr) == NULL)
21773 {
21774 attr = dw2_linkage_name_attr (die, cu);
21775 if (attr == NULL || DW_STRING (attr) == NULL)
21776 return NULL;
21777
21778 /* Avoid demangling DW_STRING (attr) the second time on a second
21779 call for the same DIE. */
21780 if (!DW_STRING_IS_CANONICAL (attr))
21781 {
21782 gdb::unique_xmalloc_ptr<char> demangled
21783 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21784 if (demangled == nullptr)
21785 return nullptr;
21786
21787 DW_STRING (attr) = objfile->intern (demangled.get ());
21788 DW_STRING_IS_CANONICAL (attr) = 1;
21789 }
21790
21791 /* Strip any leading namespaces/classes, keep only the base name.
21792 DW_AT_name for named DIEs does not contain the prefixes. */
21793 const char *base = strrchr (DW_STRING (attr), ':');
21794 if (base && base > DW_STRING (attr) && base[-1] == ':')
21795 return &base[1];
21796 else
21797 return DW_STRING (attr);
21798 }
21799 break;
21800
21801 default:
21802 break;
21803 }
21804
21805 if (!DW_STRING_IS_CANONICAL (attr))
21806 {
21807 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21808 objfile);
21809 DW_STRING_IS_CANONICAL (attr) = 1;
21810 }
21811 return DW_STRING (attr);
21812 }
21813
21814 /* Return the die that this die in an extension of, or NULL if there
21815 is none. *EXT_CU is the CU containing DIE on input, and the CU
21816 containing the return value on output. */
21817
21818 static struct die_info *
21819 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21820 {
21821 struct attribute *attr;
21822
21823 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21824 if (attr == NULL)
21825 return NULL;
21826
21827 return follow_die_ref (die, attr, ext_cu);
21828 }
21829
21830 /* A convenience function that returns an "unknown" DWARF name,
21831 including the value of V. STR is the name of the entity being
21832 printed, e.g., "TAG". */
21833
21834 static const char *
21835 dwarf_unknown (const char *str, unsigned v)
21836 {
21837 char *cell = get_print_cell ();
21838 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21839 return cell;
21840 }
21841
21842 /* Convert a DIE tag into its string name. */
21843
21844 static const char *
21845 dwarf_tag_name (unsigned tag)
21846 {
21847 const char *name = get_DW_TAG_name (tag);
21848
21849 if (name == NULL)
21850 return dwarf_unknown ("TAG", tag);
21851
21852 return name;
21853 }
21854
21855 /* Convert a DWARF attribute code into its string name. */
21856
21857 static const char *
21858 dwarf_attr_name (unsigned attr)
21859 {
21860 const char *name;
21861
21862 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21863 if (attr == DW_AT_MIPS_fde)
21864 return "DW_AT_MIPS_fde";
21865 #else
21866 if (attr == DW_AT_HP_block_index)
21867 return "DW_AT_HP_block_index";
21868 #endif
21869
21870 name = get_DW_AT_name (attr);
21871
21872 if (name == NULL)
21873 return dwarf_unknown ("AT", attr);
21874
21875 return name;
21876 }
21877
21878 /* Convert a DWARF value form code into its string name. */
21879
21880 static const char *
21881 dwarf_form_name (unsigned form)
21882 {
21883 const char *name = get_DW_FORM_name (form);
21884
21885 if (name == NULL)
21886 return dwarf_unknown ("FORM", form);
21887
21888 return name;
21889 }
21890
21891 static const char *
21892 dwarf_bool_name (unsigned mybool)
21893 {
21894 if (mybool)
21895 return "TRUE";
21896 else
21897 return "FALSE";
21898 }
21899
21900 /* Convert a DWARF type code into its string name. */
21901
21902 static const char *
21903 dwarf_type_encoding_name (unsigned enc)
21904 {
21905 const char *name = get_DW_ATE_name (enc);
21906
21907 if (name == NULL)
21908 return dwarf_unknown ("ATE", enc);
21909
21910 return name;
21911 }
21912
21913 static void
21914 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21915 {
21916 unsigned int i;
21917
21918 print_spaces (indent, f);
21919 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21920 dwarf_tag_name (die->tag), die->abbrev,
21921 sect_offset_str (die->sect_off));
21922
21923 if (die->parent != NULL)
21924 {
21925 print_spaces (indent, f);
21926 fprintf_unfiltered (f, " parent at offset: %s\n",
21927 sect_offset_str (die->parent->sect_off));
21928 }
21929
21930 print_spaces (indent, f);
21931 fprintf_unfiltered (f, " has children: %s\n",
21932 dwarf_bool_name (die->child != NULL));
21933
21934 print_spaces (indent, f);
21935 fprintf_unfiltered (f, " attributes:\n");
21936
21937 for (i = 0; i < die->num_attrs; ++i)
21938 {
21939 print_spaces (indent, f);
21940 fprintf_unfiltered (f, " %s (%s) ",
21941 dwarf_attr_name (die->attrs[i].name),
21942 dwarf_form_name (die->attrs[i].form));
21943
21944 switch (die->attrs[i].form)
21945 {
21946 case DW_FORM_addr:
21947 case DW_FORM_addrx:
21948 case DW_FORM_GNU_addr_index:
21949 fprintf_unfiltered (f, "address: ");
21950 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21951 break;
21952 case DW_FORM_block2:
21953 case DW_FORM_block4:
21954 case DW_FORM_block:
21955 case DW_FORM_block1:
21956 fprintf_unfiltered (f, "block: size %s",
21957 pulongest (DW_BLOCK (&die->attrs[i])->size));
21958 break;
21959 case DW_FORM_exprloc:
21960 fprintf_unfiltered (f, "expression: size %s",
21961 pulongest (DW_BLOCK (&die->attrs[i])->size));
21962 break;
21963 case DW_FORM_data16:
21964 fprintf_unfiltered (f, "constant of 16 bytes");
21965 break;
21966 case DW_FORM_ref_addr:
21967 fprintf_unfiltered (f, "ref address: ");
21968 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21969 break;
21970 case DW_FORM_GNU_ref_alt:
21971 fprintf_unfiltered (f, "alt ref address: ");
21972 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21973 break;
21974 case DW_FORM_ref1:
21975 case DW_FORM_ref2:
21976 case DW_FORM_ref4:
21977 case DW_FORM_ref8:
21978 case DW_FORM_ref_udata:
21979 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21980 (long) (DW_UNSND (&die->attrs[i])));
21981 break;
21982 case DW_FORM_data1:
21983 case DW_FORM_data2:
21984 case DW_FORM_data4:
21985 case DW_FORM_data8:
21986 case DW_FORM_udata:
21987 case DW_FORM_sdata:
21988 fprintf_unfiltered (f, "constant: %s",
21989 pulongest (DW_UNSND (&die->attrs[i])));
21990 break;
21991 case DW_FORM_sec_offset:
21992 fprintf_unfiltered (f, "section offset: %s",
21993 pulongest (DW_UNSND (&die->attrs[i])));
21994 break;
21995 case DW_FORM_ref_sig8:
21996 fprintf_unfiltered (f, "signature: %s",
21997 hex_string (DW_SIGNATURE (&die->attrs[i])));
21998 break;
21999 case DW_FORM_string:
22000 case DW_FORM_strp:
22001 case DW_FORM_line_strp:
22002 case DW_FORM_strx:
22003 case DW_FORM_GNU_str_index:
22004 case DW_FORM_GNU_strp_alt:
22005 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22006 DW_STRING (&die->attrs[i])
22007 ? DW_STRING (&die->attrs[i]) : "",
22008 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22009 break;
22010 case DW_FORM_flag:
22011 if (DW_UNSND (&die->attrs[i]))
22012 fprintf_unfiltered (f, "flag: TRUE");
22013 else
22014 fprintf_unfiltered (f, "flag: FALSE");
22015 break;
22016 case DW_FORM_flag_present:
22017 fprintf_unfiltered (f, "flag: TRUE");
22018 break;
22019 case DW_FORM_indirect:
22020 /* The reader will have reduced the indirect form to
22021 the "base form" so this form should not occur. */
22022 fprintf_unfiltered (f,
22023 "unexpected attribute form: DW_FORM_indirect");
22024 break;
22025 case DW_FORM_implicit_const:
22026 fprintf_unfiltered (f, "constant: %s",
22027 plongest (DW_SND (&die->attrs[i])));
22028 break;
22029 default:
22030 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22031 die->attrs[i].form);
22032 break;
22033 }
22034 fprintf_unfiltered (f, "\n");
22035 }
22036 }
22037
22038 static void
22039 dump_die_for_error (struct die_info *die)
22040 {
22041 dump_die_shallow (gdb_stderr, 0, die);
22042 }
22043
22044 static void
22045 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22046 {
22047 int indent = level * 4;
22048
22049 gdb_assert (die != NULL);
22050
22051 if (level >= max_level)
22052 return;
22053
22054 dump_die_shallow (f, indent, die);
22055
22056 if (die->child != NULL)
22057 {
22058 print_spaces (indent, f);
22059 fprintf_unfiltered (f, " Children:");
22060 if (level + 1 < max_level)
22061 {
22062 fprintf_unfiltered (f, "\n");
22063 dump_die_1 (f, level + 1, max_level, die->child);
22064 }
22065 else
22066 {
22067 fprintf_unfiltered (f,
22068 " [not printed, max nesting level reached]\n");
22069 }
22070 }
22071
22072 if (die->sibling != NULL && level > 0)
22073 {
22074 dump_die_1 (f, level, max_level, die->sibling);
22075 }
22076 }
22077
22078 /* This is called from the pdie macro in gdbinit.in.
22079 It's not static so gcc will keep a copy callable from gdb. */
22080
22081 void
22082 dump_die (struct die_info *die, int max_level)
22083 {
22084 dump_die_1 (gdb_stdlog, 0, max_level, die);
22085 }
22086
22087 static void
22088 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22089 {
22090 void **slot;
22091
22092 slot = htab_find_slot_with_hash (cu->die_hash, die,
22093 to_underlying (die->sect_off),
22094 INSERT);
22095
22096 *slot = die;
22097 }
22098
22099 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22100 required kind. */
22101
22102 static sect_offset
22103 dwarf2_get_ref_die_offset (const struct attribute *attr)
22104 {
22105 if (attr->form_is_ref ())
22106 return (sect_offset) DW_UNSND (attr);
22107
22108 complaint (_("unsupported die ref attribute form: '%s'"),
22109 dwarf_form_name (attr->form));
22110 return {};
22111 }
22112
22113 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22114 * the value held by the attribute is not constant. */
22115
22116 static LONGEST
22117 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22118 {
22119 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22120 return DW_SND (attr);
22121 else if (attr->form == DW_FORM_udata
22122 || attr->form == DW_FORM_data1
22123 || attr->form == DW_FORM_data2
22124 || attr->form == DW_FORM_data4
22125 || attr->form == DW_FORM_data8)
22126 return DW_UNSND (attr);
22127 else
22128 {
22129 /* For DW_FORM_data16 see attribute::form_is_constant. */
22130 complaint (_("Attribute value is not a constant (%s)"),
22131 dwarf_form_name (attr->form));
22132 return default_value;
22133 }
22134 }
22135
22136 /* Follow reference or signature attribute ATTR of SRC_DIE.
22137 On entry *REF_CU is the CU of SRC_DIE.
22138 On exit *REF_CU is the CU of the result. */
22139
22140 static struct die_info *
22141 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22142 struct dwarf2_cu **ref_cu)
22143 {
22144 struct die_info *die;
22145
22146 if (attr->form_is_ref ())
22147 die = follow_die_ref (src_die, attr, ref_cu);
22148 else if (attr->form == DW_FORM_ref_sig8)
22149 die = follow_die_sig (src_die, attr, ref_cu);
22150 else
22151 {
22152 dump_die_for_error (src_die);
22153 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22154 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22155 }
22156
22157 return die;
22158 }
22159
22160 /* Follow reference OFFSET.
22161 On entry *REF_CU is the CU of the source die referencing OFFSET.
22162 On exit *REF_CU is the CU of the result.
22163 Returns NULL if OFFSET is invalid. */
22164
22165 static struct die_info *
22166 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22167 struct dwarf2_cu **ref_cu)
22168 {
22169 struct die_info temp_die;
22170 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22171 struct dwarf2_per_objfile *dwarf2_per_objfile
22172 = cu->per_cu->dwarf2_per_objfile;
22173
22174 gdb_assert (cu->per_cu != NULL);
22175
22176 target_cu = cu;
22177
22178 if (cu->per_cu->is_debug_types)
22179 {
22180 /* .debug_types CUs cannot reference anything outside their CU.
22181 If they need to, they have to reference a signatured type via
22182 DW_FORM_ref_sig8. */
22183 if (!cu->header.offset_in_cu_p (sect_off))
22184 return NULL;
22185 }
22186 else if (offset_in_dwz != cu->per_cu->is_dwz
22187 || !cu->header.offset_in_cu_p (sect_off))
22188 {
22189 struct dwarf2_per_cu_data *per_cu;
22190
22191 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22192 dwarf2_per_objfile);
22193
22194 /* If necessary, add it to the queue and load its DIEs. */
22195 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22196 load_full_comp_unit (per_cu, false, cu->language);
22197
22198 target_cu = per_cu->cu;
22199 }
22200 else if (cu->dies == NULL)
22201 {
22202 /* We're loading full DIEs during partial symbol reading. */
22203 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22204 load_full_comp_unit (cu->per_cu, false, language_minimal);
22205 }
22206
22207 *ref_cu = target_cu;
22208 temp_die.sect_off = sect_off;
22209
22210 if (target_cu != cu)
22211 target_cu->ancestor = cu;
22212
22213 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22214 &temp_die,
22215 to_underlying (sect_off));
22216 }
22217
22218 /* Follow reference attribute ATTR of SRC_DIE.
22219 On entry *REF_CU is the CU of SRC_DIE.
22220 On exit *REF_CU is the CU of the result. */
22221
22222 static struct die_info *
22223 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22224 struct dwarf2_cu **ref_cu)
22225 {
22226 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22227 struct dwarf2_cu *cu = *ref_cu;
22228 struct die_info *die;
22229
22230 die = follow_die_offset (sect_off,
22231 (attr->form == DW_FORM_GNU_ref_alt
22232 || cu->per_cu->is_dwz),
22233 ref_cu);
22234 if (!die)
22235 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22236 "at %s [in module %s]"),
22237 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22238 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22239
22240 return die;
22241 }
22242
22243 /* See read.h. */
22244
22245 struct dwarf2_locexpr_baton
22246 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22247 dwarf2_per_cu_data *per_cu,
22248 CORE_ADDR (*get_frame_pc) (void *baton),
22249 void *baton, bool resolve_abstract_p)
22250 {
22251 struct dwarf2_cu *cu;
22252 struct die_info *die;
22253 struct attribute *attr;
22254 struct dwarf2_locexpr_baton retval;
22255 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22256 struct objfile *objfile = dwarf2_per_objfile->objfile;
22257
22258 if (per_cu->cu == NULL)
22259 load_cu (per_cu, false);
22260 cu = per_cu->cu;
22261 if (cu == NULL)
22262 {
22263 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22264 Instead just throw an error, not much else we can do. */
22265 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22266 sect_offset_str (sect_off), objfile_name (objfile));
22267 }
22268
22269 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22270 if (!die)
22271 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22272 sect_offset_str (sect_off), objfile_name (objfile));
22273
22274 attr = dwarf2_attr (die, DW_AT_location, cu);
22275 if (!attr && resolve_abstract_p
22276 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22277 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22278 {
22279 CORE_ADDR pc = (*get_frame_pc) (baton);
22280 CORE_ADDR baseaddr = objfile->text_section_offset ();
22281 struct gdbarch *gdbarch = get_objfile_arch (objfile);
22282
22283 for (const auto &cand_off
22284 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22285 {
22286 struct dwarf2_cu *cand_cu = cu;
22287 struct die_info *cand
22288 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22289 if (!cand
22290 || !cand->parent
22291 || cand->parent->tag != DW_TAG_subprogram)
22292 continue;
22293
22294 CORE_ADDR pc_low, pc_high;
22295 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22296 if (pc_low == ((CORE_ADDR) -1))
22297 continue;
22298 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22299 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22300 if (!(pc_low <= pc && pc < pc_high))
22301 continue;
22302
22303 die = cand;
22304 attr = dwarf2_attr (die, DW_AT_location, cu);
22305 break;
22306 }
22307 }
22308
22309 if (!attr)
22310 {
22311 /* DWARF: "If there is no such attribute, then there is no effect.".
22312 DATA is ignored if SIZE is 0. */
22313
22314 retval.data = NULL;
22315 retval.size = 0;
22316 }
22317 else if (attr->form_is_section_offset ())
22318 {
22319 struct dwarf2_loclist_baton loclist_baton;
22320 CORE_ADDR pc = (*get_frame_pc) (baton);
22321 size_t size;
22322
22323 fill_in_loclist_baton (cu, &loclist_baton, attr);
22324
22325 retval.data = dwarf2_find_location_expression (&loclist_baton,
22326 &size, pc);
22327 retval.size = size;
22328 }
22329 else
22330 {
22331 if (!attr->form_is_block ())
22332 error (_("Dwarf Error: DIE at %s referenced in module %s "
22333 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22334 sect_offset_str (sect_off), objfile_name (objfile));
22335
22336 retval.data = DW_BLOCK (attr)->data;
22337 retval.size = DW_BLOCK (attr)->size;
22338 }
22339 retval.per_cu = cu->per_cu;
22340
22341 age_cached_comp_units (dwarf2_per_objfile);
22342
22343 return retval;
22344 }
22345
22346 /* See read.h. */
22347
22348 struct dwarf2_locexpr_baton
22349 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22350 dwarf2_per_cu_data *per_cu,
22351 CORE_ADDR (*get_frame_pc) (void *baton),
22352 void *baton)
22353 {
22354 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22355
22356 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22357 }
22358
22359 /* Write a constant of a given type as target-ordered bytes into
22360 OBSTACK. */
22361
22362 static const gdb_byte *
22363 write_constant_as_bytes (struct obstack *obstack,
22364 enum bfd_endian byte_order,
22365 struct type *type,
22366 ULONGEST value,
22367 LONGEST *len)
22368 {
22369 gdb_byte *result;
22370
22371 *len = TYPE_LENGTH (type);
22372 result = (gdb_byte *) obstack_alloc (obstack, *len);
22373 store_unsigned_integer (result, *len, byte_order, value);
22374
22375 return result;
22376 }
22377
22378 /* See read.h. */
22379
22380 const gdb_byte *
22381 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22382 dwarf2_per_cu_data *per_cu,
22383 obstack *obstack,
22384 LONGEST *len)
22385 {
22386 struct dwarf2_cu *cu;
22387 struct die_info *die;
22388 struct attribute *attr;
22389 const gdb_byte *result = NULL;
22390 struct type *type;
22391 LONGEST value;
22392 enum bfd_endian byte_order;
22393 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22394
22395 if (per_cu->cu == NULL)
22396 load_cu (per_cu, false);
22397 cu = per_cu->cu;
22398 if (cu == NULL)
22399 {
22400 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22401 Instead just throw an error, not much else we can do. */
22402 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22403 sect_offset_str (sect_off), objfile_name (objfile));
22404 }
22405
22406 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22407 if (!die)
22408 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22409 sect_offset_str (sect_off), objfile_name (objfile));
22410
22411 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22412 if (attr == NULL)
22413 return NULL;
22414
22415 byte_order = (bfd_big_endian (objfile->obfd)
22416 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22417
22418 switch (attr->form)
22419 {
22420 case DW_FORM_addr:
22421 case DW_FORM_addrx:
22422 case DW_FORM_GNU_addr_index:
22423 {
22424 gdb_byte *tem;
22425
22426 *len = cu->header.addr_size;
22427 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22428 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22429 result = tem;
22430 }
22431 break;
22432 case DW_FORM_string:
22433 case DW_FORM_strp:
22434 case DW_FORM_strx:
22435 case DW_FORM_GNU_str_index:
22436 case DW_FORM_GNU_strp_alt:
22437 /* DW_STRING is already allocated on the objfile obstack, point
22438 directly to it. */
22439 result = (const gdb_byte *) DW_STRING (attr);
22440 *len = strlen (DW_STRING (attr));
22441 break;
22442 case DW_FORM_block1:
22443 case DW_FORM_block2:
22444 case DW_FORM_block4:
22445 case DW_FORM_block:
22446 case DW_FORM_exprloc:
22447 case DW_FORM_data16:
22448 result = DW_BLOCK (attr)->data;
22449 *len = DW_BLOCK (attr)->size;
22450 break;
22451
22452 /* The DW_AT_const_value attributes are supposed to carry the
22453 symbol's value "represented as it would be on the target
22454 architecture." By the time we get here, it's already been
22455 converted to host endianness, so we just need to sign- or
22456 zero-extend it as appropriate. */
22457 case DW_FORM_data1:
22458 type = die_type (die, cu);
22459 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22460 if (result == NULL)
22461 result = write_constant_as_bytes (obstack, byte_order,
22462 type, value, len);
22463 break;
22464 case DW_FORM_data2:
22465 type = die_type (die, cu);
22466 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22467 if (result == NULL)
22468 result = write_constant_as_bytes (obstack, byte_order,
22469 type, value, len);
22470 break;
22471 case DW_FORM_data4:
22472 type = die_type (die, cu);
22473 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22474 if (result == NULL)
22475 result = write_constant_as_bytes (obstack, byte_order,
22476 type, value, len);
22477 break;
22478 case DW_FORM_data8:
22479 type = die_type (die, cu);
22480 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22481 if (result == NULL)
22482 result = write_constant_as_bytes (obstack, byte_order,
22483 type, value, len);
22484 break;
22485
22486 case DW_FORM_sdata:
22487 case DW_FORM_implicit_const:
22488 type = die_type (die, cu);
22489 result = write_constant_as_bytes (obstack, byte_order,
22490 type, DW_SND (attr), len);
22491 break;
22492
22493 case DW_FORM_udata:
22494 type = die_type (die, cu);
22495 result = write_constant_as_bytes (obstack, byte_order,
22496 type, DW_UNSND (attr), len);
22497 break;
22498
22499 default:
22500 complaint (_("unsupported const value attribute form: '%s'"),
22501 dwarf_form_name (attr->form));
22502 break;
22503 }
22504
22505 return result;
22506 }
22507
22508 /* See read.h. */
22509
22510 struct type *
22511 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22512 dwarf2_per_cu_data *per_cu)
22513 {
22514 struct dwarf2_cu *cu;
22515 struct die_info *die;
22516
22517 if (per_cu->cu == NULL)
22518 load_cu (per_cu, false);
22519 cu = per_cu->cu;
22520 if (!cu)
22521 return NULL;
22522
22523 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22524 if (!die)
22525 return NULL;
22526
22527 return die_type (die, cu);
22528 }
22529
22530 /* See read.h. */
22531
22532 struct type *
22533 dwarf2_get_die_type (cu_offset die_offset,
22534 struct dwarf2_per_cu_data *per_cu)
22535 {
22536 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22537 return get_die_type_at_offset (die_offset_sect, per_cu);
22538 }
22539
22540 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22541 On entry *REF_CU is the CU of SRC_DIE.
22542 On exit *REF_CU is the CU of the result.
22543 Returns NULL if the referenced DIE isn't found. */
22544
22545 static struct die_info *
22546 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22547 struct dwarf2_cu **ref_cu)
22548 {
22549 struct die_info temp_die;
22550 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22551 struct die_info *die;
22552
22553 /* While it might be nice to assert sig_type->type == NULL here,
22554 we can get here for DW_AT_imported_declaration where we need
22555 the DIE not the type. */
22556
22557 /* If necessary, add it to the queue and load its DIEs. */
22558
22559 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22560 read_signatured_type (sig_type);
22561
22562 sig_cu = sig_type->per_cu.cu;
22563 gdb_assert (sig_cu != NULL);
22564 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22565 temp_die.sect_off = sig_type->type_offset_in_section;
22566 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22567 to_underlying (temp_die.sect_off));
22568 if (die)
22569 {
22570 struct dwarf2_per_objfile *dwarf2_per_objfile
22571 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22572
22573 /* For .gdb_index version 7 keep track of included TUs.
22574 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22575 if (dwarf2_per_objfile->index_table != NULL
22576 && dwarf2_per_objfile->index_table->version <= 7)
22577 {
22578 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22579 }
22580
22581 *ref_cu = sig_cu;
22582 if (sig_cu != cu)
22583 sig_cu->ancestor = cu;
22584
22585 return die;
22586 }
22587
22588 return NULL;
22589 }
22590
22591 /* Follow signatured type referenced by ATTR in SRC_DIE.
22592 On entry *REF_CU is the CU of SRC_DIE.
22593 On exit *REF_CU is the CU of the result.
22594 The result is the DIE of the type.
22595 If the referenced type cannot be found an error is thrown. */
22596
22597 static struct die_info *
22598 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22599 struct dwarf2_cu **ref_cu)
22600 {
22601 ULONGEST signature = DW_SIGNATURE (attr);
22602 struct signatured_type *sig_type;
22603 struct die_info *die;
22604
22605 gdb_assert (attr->form == DW_FORM_ref_sig8);
22606
22607 sig_type = lookup_signatured_type (*ref_cu, signature);
22608 /* sig_type will be NULL if the signatured type is missing from
22609 the debug info. */
22610 if (sig_type == NULL)
22611 {
22612 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22613 " from DIE at %s [in module %s]"),
22614 hex_string (signature), sect_offset_str (src_die->sect_off),
22615 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22616 }
22617
22618 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22619 if (die == NULL)
22620 {
22621 dump_die_for_error (src_die);
22622 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22623 " from DIE at %s [in module %s]"),
22624 hex_string (signature), sect_offset_str (src_die->sect_off),
22625 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22626 }
22627
22628 return die;
22629 }
22630
22631 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22632 reading in and processing the type unit if necessary. */
22633
22634 static struct type *
22635 get_signatured_type (struct die_info *die, ULONGEST signature,
22636 struct dwarf2_cu *cu)
22637 {
22638 struct dwarf2_per_objfile *dwarf2_per_objfile
22639 = cu->per_cu->dwarf2_per_objfile;
22640 struct signatured_type *sig_type;
22641 struct dwarf2_cu *type_cu;
22642 struct die_info *type_die;
22643 struct type *type;
22644
22645 sig_type = lookup_signatured_type (cu, signature);
22646 /* sig_type will be NULL if the signatured type is missing from
22647 the debug info. */
22648 if (sig_type == NULL)
22649 {
22650 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22651 " from DIE at %s [in module %s]"),
22652 hex_string (signature), sect_offset_str (die->sect_off),
22653 objfile_name (dwarf2_per_objfile->objfile));
22654 return build_error_marker_type (cu, die);
22655 }
22656
22657 /* If we already know the type we're done. */
22658 if (sig_type->type != NULL)
22659 return sig_type->type;
22660
22661 type_cu = cu;
22662 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22663 if (type_die != NULL)
22664 {
22665 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22666 is created. This is important, for example, because for c++ classes
22667 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22668 type = read_type_die (type_die, type_cu);
22669 if (type == NULL)
22670 {
22671 complaint (_("Dwarf Error: Cannot build signatured type %s"
22672 " referenced from DIE at %s [in module %s]"),
22673 hex_string (signature), sect_offset_str (die->sect_off),
22674 objfile_name (dwarf2_per_objfile->objfile));
22675 type = build_error_marker_type (cu, die);
22676 }
22677 }
22678 else
22679 {
22680 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22681 " from DIE at %s [in module %s]"),
22682 hex_string (signature), sect_offset_str (die->sect_off),
22683 objfile_name (dwarf2_per_objfile->objfile));
22684 type = build_error_marker_type (cu, die);
22685 }
22686 sig_type->type = type;
22687
22688 return type;
22689 }
22690
22691 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22692 reading in and processing the type unit if necessary. */
22693
22694 static struct type *
22695 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22696 struct dwarf2_cu *cu) /* ARI: editCase function */
22697 {
22698 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22699 if (attr->form_is_ref ())
22700 {
22701 struct dwarf2_cu *type_cu = cu;
22702 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22703
22704 return read_type_die (type_die, type_cu);
22705 }
22706 else if (attr->form == DW_FORM_ref_sig8)
22707 {
22708 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22709 }
22710 else
22711 {
22712 struct dwarf2_per_objfile *dwarf2_per_objfile
22713 = cu->per_cu->dwarf2_per_objfile;
22714
22715 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22716 " at %s [in module %s]"),
22717 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22718 objfile_name (dwarf2_per_objfile->objfile));
22719 return build_error_marker_type (cu, die);
22720 }
22721 }
22722
22723 /* Load the DIEs associated with type unit PER_CU into memory. */
22724
22725 static void
22726 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22727 {
22728 struct signatured_type *sig_type;
22729
22730 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22731 gdb_assert (! per_cu->type_unit_group_p ());
22732
22733 /* We have the per_cu, but we need the signatured_type.
22734 Fortunately this is an easy translation. */
22735 gdb_assert (per_cu->is_debug_types);
22736 sig_type = (struct signatured_type *) per_cu;
22737
22738 gdb_assert (per_cu->cu == NULL);
22739
22740 read_signatured_type (sig_type);
22741
22742 gdb_assert (per_cu->cu != NULL);
22743 }
22744
22745 /* Read in a signatured type and build its CU and DIEs.
22746 If the type is a stub for the real type in a DWO file,
22747 read in the real type from the DWO file as well. */
22748
22749 static void
22750 read_signatured_type (struct signatured_type *sig_type)
22751 {
22752 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22753
22754 gdb_assert (per_cu->is_debug_types);
22755 gdb_assert (per_cu->cu == NULL);
22756
22757 cutu_reader reader (per_cu, NULL, 0, false);
22758
22759 if (!reader.dummy_p)
22760 {
22761 struct dwarf2_cu *cu = reader.cu;
22762 const gdb_byte *info_ptr = reader.info_ptr;
22763
22764 gdb_assert (cu->die_hash == NULL);
22765 cu->die_hash =
22766 htab_create_alloc_ex (cu->header.length / 12,
22767 die_hash,
22768 die_eq,
22769 NULL,
22770 &cu->comp_unit_obstack,
22771 hashtab_obstack_allocate,
22772 dummy_obstack_deallocate);
22773
22774 if (reader.comp_unit_die->has_children)
22775 reader.comp_unit_die->child
22776 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22777 reader.comp_unit_die);
22778 cu->dies = reader.comp_unit_die;
22779 /* comp_unit_die is not stored in die_hash, no need. */
22780
22781 /* We try not to read any attributes in this function, because
22782 not all CUs needed for references have been loaded yet, and
22783 symbol table processing isn't initialized. But we have to
22784 set the CU language, or we won't be able to build types
22785 correctly. Similarly, if we do not read the producer, we can
22786 not apply producer-specific interpretation. */
22787 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22788
22789 reader.keep ();
22790 }
22791
22792 sig_type->per_cu.tu_read = 1;
22793 }
22794
22795 /* Decode simple location descriptions.
22796 Given a pointer to a dwarf block that defines a location, compute
22797 the location and return the value.
22798
22799 NOTE drow/2003-11-18: This function is called in two situations
22800 now: for the address of static or global variables (partial symbols
22801 only) and for offsets into structures which are expected to be
22802 (more or less) constant. The partial symbol case should go away,
22803 and only the constant case should remain. That will let this
22804 function complain more accurately. A few special modes are allowed
22805 without complaint for global variables (for instance, global
22806 register values and thread-local values).
22807
22808 A location description containing no operations indicates that the
22809 object is optimized out. The return value is 0 for that case.
22810 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22811 callers will only want a very basic result and this can become a
22812 complaint.
22813
22814 Note that stack[0] is unused except as a default error return. */
22815
22816 static CORE_ADDR
22817 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22818 {
22819 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22820 size_t i;
22821 size_t size = blk->size;
22822 const gdb_byte *data = blk->data;
22823 CORE_ADDR stack[64];
22824 int stacki;
22825 unsigned int bytes_read, unsnd;
22826 gdb_byte op;
22827
22828 i = 0;
22829 stacki = 0;
22830 stack[stacki] = 0;
22831 stack[++stacki] = 0;
22832
22833 while (i < size)
22834 {
22835 op = data[i++];
22836 switch (op)
22837 {
22838 case DW_OP_lit0:
22839 case DW_OP_lit1:
22840 case DW_OP_lit2:
22841 case DW_OP_lit3:
22842 case DW_OP_lit4:
22843 case DW_OP_lit5:
22844 case DW_OP_lit6:
22845 case DW_OP_lit7:
22846 case DW_OP_lit8:
22847 case DW_OP_lit9:
22848 case DW_OP_lit10:
22849 case DW_OP_lit11:
22850 case DW_OP_lit12:
22851 case DW_OP_lit13:
22852 case DW_OP_lit14:
22853 case DW_OP_lit15:
22854 case DW_OP_lit16:
22855 case DW_OP_lit17:
22856 case DW_OP_lit18:
22857 case DW_OP_lit19:
22858 case DW_OP_lit20:
22859 case DW_OP_lit21:
22860 case DW_OP_lit22:
22861 case DW_OP_lit23:
22862 case DW_OP_lit24:
22863 case DW_OP_lit25:
22864 case DW_OP_lit26:
22865 case DW_OP_lit27:
22866 case DW_OP_lit28:
22867 case DW_OP_lit29:
22868 case DW_OP_lit30:
22869 case DW_OP_lit31:
22870 stack[++stacki] = op - DW_OP_lit0;
22871 break;
22872
22873 case DW_OP_reg0:
22874 case DW_OP_reg1:
22875 case DW_OP_reg2:
22876 case DW_OP_reg3:
22877 case DW_OP_reg4:
22878 case DW_OP_reg5:
22879 case DW_OP_reg6:
22880 case DW_OP_reg7:
22881 case DW_OP_reg8:
22882 case DW_OP_reg9:
22883 case DW_OP_reg10:
22884 case DW_OP_reg11:
22885 case DW_OP_reg12:
22886 case DW_OP_reg13:
22887 case DW_OP_reg14:
22888 case DW_OP_reg15:
22889 case DW_OP_reg16:
22890 case DW_OP_reg17:
22891 case DW_OP_reg18:
22892 case DW_OP_reg19:
22893 case DW_OP_reg20:
22894 case DW_OP_reg21:
22895 case DW_OP_reg22:
22896 case DW_OP_reg23:
22897 case DW_OP_reg24:
22898 case DW_OP_reg25:
22899 case DW_OP_reg26:
22900 case DW_OP_reg27:
22901 case DW_OP_reg28:
22902 case DW_OP_reg29:
22903 case DW_OP_reg30:
22904 case DW_OP_reg31:
22905 stack[++stacki] = op - DW_OP_reg0;
22906 if (i < size)
22907 dwarf2_complex_location_expr_complaint ();
22908 break;
22909
22910 case DW_OP_regx:
22911 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22912 i += bytes_read;
22913 stack[++stacki] = unsnd;
22914 if (i < size)
22915 dwarf2_complex_location_expr_complaint ();
22916 break;
22917
22918 case DW_OP_addr:
22919 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22920 &bytes_read);
22921 i += bytes_read;
22922 break;
22923
22924 case DW_OP_const1u:
22925 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22926 i += 1;
22927 break;
22928
22929 case DW_OP_const1s:
22930 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22931 i += 1;
22932 break;
22933
22934 case DW_OP_const2u:
22935 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22936 i += 2;
22937 break;
22938
22939 case DW_OP_const2s:
22940 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22941 i += 2;
22942 break;
22943
22944 case DW_OP_const4u:
22945 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22946 i += 4;
22947 break;
22948
22949 case DW_OP_const4s:
22950 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22951 i += 4;
22952 break;
22953
22954 case DW_OP_const8u:
22955 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22956 i += 8;
22957 break;
22958
22959 case DW_OP_constu:
22960 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22961 &bytes_read);
22962 i += bytes_read;
22963 break;
22964
22965 case DW_OP_consts:
22966 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22967 i += bytes_read;
22968 break;
22969
22970 case DW_OP_dup:
22971 stack[stacki + 1] = stack[stacki];
22972 stacki++;
22973 break;
22974
22975 case DW_OP_plus:
22976 stack[stacki - 1] += stack[stacki];
22977 stacki--;
22978 break;
22979
22980 case DW_OP_plus_uconst:
22981 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22982 &bytes_read);
22983 i += bytes_read;
22984 break;
22985
22986 case DW_OP_minus:
22987 stack[stacki - 1] -= stack[stacki];
22988 stacki--;
22989 break;
22990
22991 case DW_OP_deref:
22992 /* If we're not the last op, then we definitely can't encode
22993 this using GDB's address_class enum. This is valid for partial
22994 global symbols, although the variable's address will be bogus
22995 in the psymtab. */
22996 if (i < size)
22997 dwarf2_complex_location_expr_complaint ();
22998 break;
22999
23000 case DW_OP_GNU_push_tls_address:
23001 case DW_OP_form_tls_address:
23002 /* The top of the stack has the offset from the beginning
23003 of the thread control block at which the variable is located. */
23004 /* Nothing should follow this operator, so the top of stack would
23005 be returned. */
23006 /* This is valid for partial global symbols, but the variable's
23007 address will be bogus in the psymtab. Make it always at least
23008 non-zero to not look as a variable garbage collected by linker
23009 which have DW_OP_addr 0. */
23010 if (i < size)
23011 dwarf2_complex_location_expr_complaint ();
23012 stack[stacki]++;
23013 break;
23014
23015 case DW_OP_GNU_uninit:
23016 break;
23017
23018 case DW_OP_addrx:
23019 case DW_OP_GNU_addr_index:
23020 case DW_OP_GNU_const_index:
23021 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23022 &bytes_read);
23023 i += bytes_read;
23024 break;
23025
23026 default:
23027 {
23028 const char *name = get_DW_OP_name (op);
23029
23030 if (name)
23031 complaint (_("unsupported stack op: '%s'"),
23032 name);
23033 else
23034 complaint (_("unsupported stack op: '%02x'"),
23035 op);
23036 }
23037
23038 return (stack[stacki]);
23039 }
23040
23041 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23042 outside of the allocated space. Also enforce minimum>0. */
23043 if (stacki >= ARRAY_SIZE (stack) - 1)
23044 {
23045 complaint (_("location description stack overflow"));
23046 return 0;
23047 }
23048
23049 if (stacki <= 0)
23050 {
23051 complaint (_("location description stack underflow"));
23052 return 0;
23053 }
23054 }
23055 return (stack[stacki]);
23056 }
23057
23058 /* memory allocation interface */
23059
23060 static struct dwarf_block *
23061 dwarf_alloc_block (struct dwarf2_cu *cu)
23062 {
23063 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23064 }
23065
23066 static struct die_info *
23067 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23068 {
23069 struct die_info *die;
23070 size_t size = sizeof (struct die_info);
23071
23072 if (num_attrs > 1)
23073 size += (num_attrs - 1) * sizeof (struct attribute);
23074
23075 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23076 memset (die, 0, sizeof (struct die_info));
23077 return (die);
23078 }
23079
23080 \f
23081 /* Macro support. */
23082
23083 static struct macro_source_file *
23084 macro_start_file (buildsym_compunit *builder,
23085 int file, int line,
23086 struct macro_source_file *current_file,
23087 struct line_header *lh)
23088 {
23089 /* File name relative to the compilation directory of this source file. */
23090 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
23091
23092 if (! current_file)
23093 {
23094 /* Note: We don't create a macro table for this compilation unit
23095 at all until we actually get a filename. */
23096 struct macro_table *macro_table = builder->get_macro_table ();
23097
23098 /* If we have no current file, then this must be the start_file
23099 directive for the compilation unit's main source file. */
23100 current_file = macro_set_main (macro_table, file_name.get ());
23101 macro_define_special (macro_table);
23102 }
23103 else
23104 current_file = macro_include (current_file, line, file_name.get ());
23105
23106 return current_file;
23107 }
23108
23109 static const char *
23110 consume_improper_spaces (const char *p, const char *body)
23111 {
23112 if (*p == ' ')
23113 {
23114 complaint (_("macro definition contains spaces "
23115 "in formal argument list:\n`%s'"),
23116 body);
23117
23118 while (*p == ' ')
23119 p++;
23120 }
23121
23122 return p;
23123 }
23124
23125
23126 static void
23127 parse_macro_definition (struct macro_source_file *file, int line,
23128 const char *body)
23129 {
23130 const char *p;
23131
23132 /* The body string takes one of two forms. For object-like macro
23133 definitions, it should be:
23134
23135 <macro name> " " <definition>
23136
23137 For function-like macro definitions, it should be:
23138
23139 <macro name> "() " <definition>
23140 or
23141 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23142
23143 Spaces may appear only where explicitly indicated, and in the
23144 <definition>.
23145
23146 The Dwarf 2 spec says that an object-like macro's name is always
23147 followed by a space, but versions of GCC around March 2002 omit
23148 the space when the macro's definition is the empty string.
23149
23150 The Dwarf 2 spec says that there should be no spaces between the
23151 formal arguments in a function-like macro's formal argument list,
23152 but versions of GCC around March 2002 include spaces after the
23153 commas. */
23154
23155
23156 /* Find the extent of the macro name. The macro name is terminated
23157 by either a space or null character (for an object-like macro) or
23158 an opening paren (for a function-like macro). */
23159 for (p = body; *p; p++)
23160 if (*p == ' ' || *p == '(')
23161 break;
23162
23163 if (*p == ' ' || *p == '\0')
23164 {
23165 /* It's an object-like macro. */
23166 int name_len = p - body;
23167 std::string name (body, name_len);
23168 const char *replacement;
23169
23170 if (*p == ' ')
23171 replacement = body + name_len + 1;
23172 else
23173 {
23174 dwarf2_macro_malformed_definition_complaint (body);
23175 replacement = body + name_len;
23176 }
23177
23178 macro_define_object (file, line, name.c_str (), replacement);
23179 }
23180 else if (*p == '(')
23181 {
23182 /* It's a function-like macro. */
23183 std::string name (body, p - body);
23184 int argc = 0;
23185 int argv_size = 1;
23186 char **argv = XNEWVEC (char *, argv_size);
23187
23188 p++;
23189
23190 p = consume_improper_spaces (p, body);
23191
23192 /* Parse the formal argument list. */
23193 while (*p && *p != ')')
23194 {
23195 /* Find the extent of the current argument name. */
23196 const char *arg_start = p;
23197
23198 while (*p && *p != ',' && *p != ')' && *p != ' ')
23199 p++;
23200
23201 if (! *p || p == arg_start)
23202 dwarf2_macro_malformed_definition_complaint (body);
23203 else
23204 {
23205 /* Make sure argv has room for the new argument. */
23206 if (argc >= argv_size)
23207 {
23208 argv_size *= 2;
23209 argv = XRESIZEVEC (char *, argv, argv_size);
23210 }
23211
23212 argv[argc++] = savestring (arg_start, p - arg_start);
23213 }
23214
23215 p = consume_improper_spaces (p, body);
23216
23217 /* Consume the comma, if present. */
23218 if (*p == ',')
23219 {
23220 p++;
23221
23222 p = consume_improper_spaces (p, body);
23223 }
23224 }
23225
23226 if (*p == ')')
23227 {
23228 p++;
23229
23230 if (*p == ' ')
23231 /* Perfectly formed definition, no complaints. */
23232 macro_define_function (file, line, name.c_str (),
23233 argc, (const char **) argv,
23234 p + 1);
23235 else if (*p == '\0')
23236 {
23237 /* Complain, but do define it. */
23238 dwarf2_macro_malformed_definition_complaint (body);
23239 macro_define_function (file, line, name.c_str (),
23240 argc, (const char **) argv,
23241 p);
23242 }
23243 else
23244 /* Just complain. */
23245 dwarf2_macro_malformed_definition_complaint (body);
23246 }
23247 else
23248 /* Just complain. */
23249 dwarf2_macro_malformed_definition_complaint (body);
23250
23251 {
23252 int i;
23253
23254 for (i = 0; i < argc; i++)
23255 xfree (argv[i]);
23256 }
23257 xfree (argv);
23258 }
23259 else
23260 dwarf2_macro_malformed_definition_complaint (body);
23261 }
23262
23263 /* Skip some bytes from BYTES according to the form given in FORM.
23264 Returns the new pointer. */
23265
23266 static const gdb_byte *
23267 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23268 enum dwarf_form form,
23269 unsigned int offset_size,
23270 struct dwarf2_section_info *section)
23271 {
23272 unsigned int bytes_read;
23273
23274 switch (form)
23275 {
23276 case DW_FORM_data1:
23277 case DW_FORM_flag:
23278 ++bytes;
23279 break;
23280
23281 case DW_FORM_data2:
23282 bytes += 2;
23283 break;
23284
23285 case DW_FORM_data4:
23286 bytes += 4;
23287 break;
23288
23289 case DW_FORM_data8:
23290 bytes += 8;
23291 break;
23292
23293 case DW_FORM_data16:
23294 bytes += 16;
23295 break;
23296
23297 case DW_FORM_string:
23298 read_direct_string (abfd, bytes, &bytes_read);
23299 bytes += bytes_read;
23300 break;
23301
23302 case DW_FORM_sec_offset:
23303 case DW_FORM_strp:
23304 case DW_FORM_GNU_strp_alt:
23305 bytes += offset_size;
23306 break;
23307
23308 case DW_FORM_block:
23309 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23310 bytes += bytes_read;
23311 break;
23312
23313 case DW_FORM_block1:
23314 bytes += 1 + read_1_byte (abfd, bytes);
23315 break;
23316 case DW_FORM_block2:
23317 bytes += 2 + read_2_bytes (abfd, bytes);
23318 break;
23319 case DW_FORM_block4:
23320 bytes += 4 + read_4_bytes (abfd, bytes);
23321 break;
23322
23323 case DW_FORM_addrx:
23324 case DW_FORM_sdata:
23325 case DW_FORM_strx:
23326 case DW_FORM_udata:
23327 case DW_FORM_GNU_addr_index:
23328 case DW_FORM_GNU_str_index:
23329 bytes = gdb_skip_leb128 (bytes, buffer_end);
23330 if (bytes == NULL)
23331 {
23332 section->overflow_complaint ();
23333 return NULL;
23334 }
23335 break;
23336
23337 case DW_FORM_implicit_const:
23338 break;
23339
23340 default:
23341 {
23342 complaint (_("invalid form 0x%x in `%s'"),
23343 form, section->get_name ());
23344 return NULL;
23345 }
23346 }
23347
23348 return bytes;
23349 }
23350
23351 /* A helper for dwarf_decode_macros that handles skipping an unknown
23352 opcode. Returns an updated pointer to the macro data buffer; or,
23353 on error, issues a complaint and returns NULL. */
23354
23355 static const gdb_byte *
23356 skip_unknown_opcode (unsigned int opcode,
23357 const gdb_byte **opcode_definitions,
23358 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23359 bfd *abfd,
23360 unsigned int offset_size,
23361 struct dwarf2_section_info *section)
23362 {
23363 unsigned int bytes_read, i;
23364 unsigned long arg;
23365 const gdb_byte *defn;
23366
23367 if (opcode_definitions[opcode] == NULL)
23368 {
23369 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
23370 opcode);
23371 return NULL;
23372 }
23373
23374 defn = opcode_definitions[opcode];
23375 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23376 defn += bytes_read;
23377
23378 for (i = 0; i < arg; ++i)
23379 {
23380 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23381 (enum dwarf_form) defn[i], offset_size,
23382 section);
23383 if (mac_ptr == NULL)
23384 {
23385 /* skip_form_bytes already issued the complaint. */
23386 return NULL;
23387 }
23388 }
23389
23390 return mac_ptr;
23391 }
23392
23393 /* A helper function which parses the header of a macro section.
23394 If the macro section is the extended (for now called "GNU") type,
23395 then this updates *OFFSET_SIZE. Returns a pointer to just after
23396 the header, or issues a complaint and returns NULL on error. */
23397
23398 static const gdb_byte *
23399 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
23400 bfd *abfd,
23401 const gdb_byte *mac_ptr,
23402 unsigned int *offset_size,
23403 int section_is_gnu)
23404 {
23405 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
23406
23407 if (section_is_gnu)
23408 {
23409 unsigned int version, flags;
23410
23411 version = read_2_bytes (abfd, mac_ptr);
23412 if (version != 4 && version != 5)
23413 {
23414 complaint (_("unrecognized version `%d' in .debug_macro section"),
23415 version);
23416 return NULL;
23417 }
23418 mac_ptr += 2;
23419
23420 flags = read_1_byte (abfd, mac_ptr);
23421 ++mac_ptr;
23422 *offset_size = (flags & 1) ? 8 : 4;
23423
23424 if ((flags & 2) != 0)
23425 /* We don't need the line table offset. */
23426 mac_ptr += *offset_size;
23427
23428 /* Vendor opcode descriptions. */
23429 if ((flags & 4) != 0)
23430 {
23431 unsigned int i, count;
23432
23433 count = read_1_byte (abfd, mac_ptr);
23434 ++mac_ptr;
23435 for (i = 0; i < count; ++i)
23436 {
23437 unsigned int opcode, bytes_read;
23438 unsigned long arg;
23439
23440 opcode = read_1_byte (abfd, mac_ptr);
23441 ++mac_ptr;
23442 opcode_definitions[opcode] = mac_ptr;
23443 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23444 mac_ptr += bytes_read;
23445 mac_ptr += arg;
23446 }
23447 }
23448 }
23449
23450 return mac_ptr;
23451 }
23452
23453 /* A helper for dwarf_decode_macros that handles the GNU extensions,
23454 including DW_MACRO_import. */
23455
23456 static void
23457 dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
23458 buildsym_compunit *builder,
23459 bfd *abfd,
23460 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23461 struct macro_source_file *current_file,
23462 struct line_header *lh,
23463 struct dwarf2_section_info *section,
23464 int section_is_gnu, int section_is_dwz,
23465 unsigned int offset_size,
23466 htab_t include_hash)
23467 {
23468 struct objfile *objfile = dwarf2_per_objfile->objfile;
23469 enum dwarf_macro_record_type macinfo_type;
23470 int at_commandline;
23471 const gdb_byte *opcode_definitions[256];
23472
23473 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23474 &offset_size, section_is_gnu);
23475 if (mac_ptr == NULL)
23476 {
23477 /* We already issued a complaint. */
23478 return;
23479 }
23480
23481 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23482 GDB is still reading the definitions from command line. First
23483 DW_MACINFO_start_file will need to be ignored as it was already executed
23484 to create CURRENT_FILE for the main source holding also the command line
23485 definitions. On first met DW_MACINFO_start_file this flag is reset to
23486 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23487
23488 at_commandline = 1;
23489
23490 do
23491 {
23492 /* Do we at least have room for a macinfo type byte? */
23493 if (mac_ptr >= mac_end)
23494 {
23495 section->overflow_complaint ();
23496 break;
23497 }
23498
23499 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23500 mac_ptr++;
23501
23502 /* Note that we rely on the fact that the corresponding GNU and
23503 DWARF constants are the same. */
23504 DIAGNOSTIC_PUSH
23505 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23506 switch (macinfo_type)
23507 {
23508 /* A zero macinfo type indicates the end of the macro
23509 information. */
23510 case 0:
23511 break;
23512
23513 case DW_MACRO_define:
23514 case DW_MACRO_undef:
23515 case DW_MACRO_define_strp:
23516 case DW_MACRO_undef_strp:
23517 case DW_MACRO_define_sup:
23518 case DW_MACRO_undef_sup:
23519 {
23520 unsigned int bytes_read;
23521 int line;
23522 const char *body;
23523 int is_define;
23524
23525 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23526 mac_ptr += bytes_read;
23527
23528 if (macinfo_type == DW_MACRO_define
23529 || macinfo_type == DW_MACRO_undef)
23530 {
23531 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23532 mac_ptr += bytes_read;
23533 }
23534 else
23535 {
23536 LONGEST str_offset;
23537
23538 str_offset = read_offset (abfd, mac_ptr, offset_size);
23539 mac_ptr += offset_size;
23540
23541 if (macinfo_type == DW_MACRO_define_sup
23542 || macinfo_type == DW_MACRO_undef_sup
23543 || section_is_dwz)
23544 {
23545 struct dwz_file *dwz
23546 = dwarf2_get_dwz_file (dwarf2_per_objfile);
23547
23548 body = dwz->read_string (objfile, str_offset);
23549 }
23550 else
23551 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23552 abfd, str_offset);
23553 }
23554
23555 is_define = (macinfo_type == DW_MACRO_define
23556 || macinfo_type == DW_MACRO_define_strp
23557 || macinfo_type == DW_MACRO_define_sup);
23558 if (! current_file)
23559 {
23560 /* DWARF violation as no main source is present. */
23561 complaint (_("debug info with no main source gives macro %s "
23562 "on line %d: %s"),
23563 is_define ? _("definition") : _("undefinition"),
23564 line, body);
23565 break;
23566 }
23567 if ((line == 0 && !at_commandline)
23568 || (line != 0 && at_commandline))
23569 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
23570 at_commandline ? _("command-line") : _("in-file"),
23571 is_define ? _("definition") : _("undefinition"),
23572 line == 0 ? _("zero") : _("non-zero"), line, body);
23573
23574 if (body == NULL)
23575 {
23576 /* Fedora's rpm-build's "debugedit" binary
23577 corrupted .debug_macro sections.
23578
23579 For more info, see
23580 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23581 complaint (_("debug info gives %s invalid macro %s "
23582 "without body (corrupted?) at line %d "
23583 "on file %s"),
23584 at_commandline ? _("command-line") : _("in-file"),
23585 is_define ? _("definition") : _("undefinition"),
23586 line, current_file->filename);
23587 }
23588 else if (is_define)
23589 parse_macro_definition (current_file, line, body);
23590 else
23591 {
23592 gdb_assert (macinfo_type == DW_MACRO_undef
23593 || macinfo_type == DW_MACRO_undef_strp
23594 || macinfo_type == DW_MACRO_undef_sup);
23595 macro_undef (current_file, line, body);
23596 }
23597 }
23598 break;
23599
23600 case DW_MACRO_start_file:
23601 {
23602 unsigned int bytes_read;
23603 int line, file;
23604
23605 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23606 mac_ptr += bytes_read;
23607 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23608 mac_ptr += bytes_read;
23609
23610 if ((line == 0 && !at_commandline)
23611 || (line != 0 && at_commandline))
23612 complaint (_("debug info gives source %d included "
23613 "from %s at %s line %d"),
23614 file, at_commandline ? _("command-line") : _("file"),
23615 line == 0 ? _("zero") : _("non-zero"), line);
23616
23617 if (at_commandline)
23618 {
23619 /* This DW_MACRO_start_file was executed in the
23620 pass one. */
23621 at_commandline = 0;
23622 }
23623 else
23624 current_file = macro_start_file (builder, file, line,
23625 current_file, lh);
23626 }
23627 break;
23628
23629 case DW_MACRO_end_file:
23630 if (! current_file)
23631 complaint (_("macro debug info has an unmatched "
23632 "`close_file' directive"));
23633 else
23634 {
23635 current_file = current_file->included_by;
23636 if (! current_file)
23637 {
23638 enum dwarf_macro_record_type next_type;
23639
23640 /* GCC circa March 2002 doesn't produce the zero
23641 type byte marking the end of the compilation
23642 unit. Complain if it's not there, but exit no
23643 matter what. */
23644
23645 /* Do we at least have room for a macinfo type byte? */
23646 if (mac_ptr >= mac_end)
23647 {
23648 section->overflow_complaint ();
23649 return;
23650 }
23651
23652 /* We don't increment mac_ptr here, so this is just
23653 a look-ahead. */
23654 next_type
23655 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23656 mac_ptr);
23657 if (next_type != 0)
23658 complaint (_("no terminating 0-type entry for "
23659 "macros in `.debug_macinfo' section"));
23660
23661 return;
23662 }
23663 }
23664 break;
23665
23666 case DW_MACRO_import:
23667 case DW_MACRO_import_sup:
23668 {
23669 LONGEST offset;
23670 void **slot;
23671 bfd *include_bfd = abfd;
23672 struct dwarf2_section_info *include_section = section;
23673 const gdb_byte *include_mac_end = mac_end;
23674 int is_dwz = section_is_dwz;
23675 const gdb_byte *new_mac_ptr;
23676
23677 offset = read_offset (abfd, mac_ptr, offset_size);
23678 mac_ptr += offset_size;
23679
23680 if (macinfo_type == DW_MACRO_import_sup)
23681 {
23682 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
23683
23684 dwz->macro.read (objfile);
23685
23686 include_section = &dwz->macro;
23687 include_bfd = include_section->get_bfd_owner ();
23688 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23689 is_dwz = 1;
23690 }
23691
23692 new_mac_ptr = include_section->buffer + offset;
23693 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23694
23695 if (*slot != NULL)
23696 {
23697 /* This has actually happened; see
23698 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23699 complaint (_("recursive DW_MACRO_import in "
23700 ".debug_macro section"));
23701 }
23702 else
23703 {
23704 *slot = (void *) new_mac_ptr;
23705
23706 dwarf_decode_macro_bytes (dwarf2_per_objfile, builder,
23707 include_bfd, new_mac_ptr,
23708 include_mac_end, current_file, lh,
23709 section, section_is_gnu, is_dwz,
23710 offset_size, include_hash);
23711
23712 htab_remove_elt (include_hash, (void *) new_mac_ptr);
23713 }
23714 }
23715 break;
23716
23717 case DW_MACINFO_vendor_ext:
23718 if (!section_is_gnu)
23719 {
23720 unsigned int bytes_read;
23721
23722 /* This reads the constant, but since we don't recognize
23723 any vendor extensions, we ignore it. */
23724 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23725 mac_ptr += bytes_read;
23726 read_direct_string (abfd, mac_ptr, &bytes_read);
23727 mac_ptr += bytes_read;
23728
23729 /* We don't recognize any vendor extensions. */
23730 break;
23731 }
23732 /* FALLTHROUGH */
23733
23734 default:
23735 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23736 mac_ptr, mac_end, abfd, offset_size,
23737 section);
23738 if (mac_ptr == NULL)
23739 return;
23740 break;
23741 }
23742 DIAGNOSTIC_POP
23743 } while (macinfo_type != 0);
23744 }
23745
23746 static void
23747 dwarf_decode_macros (struct dwarf2_per_objfile *dwarf2_per_objfile,
23748 buildsym_compunit *builder, dwarf2_section_info *section,
23749 struct line_header *lh, unsigned int offset_size,
23750 unsigned int offset, int section_is_gnu)
23751 {
23752 bfd *abfd;
23753 const gdb_byte *mac_ptr, *mac_end;
23754 struct macro_source_file *current_file = 0;
23755 enum dwarf_macro_record_type macinfo_type;
23756 const gdb_byte *opcode_definitions[256];
23757 void **slot;
23758
23759 abfd = section->get_bfd_owner ();
23760
23761 /* First pass: Find the name of the base filename.
23762 This filename is needed in order to process all macros whose definition
23763 (or undefinition) comes from the command line. These macros are defined
23764 before the first DW_MACINFO_start_file entry, and yet still need to be
23765 associated to the base file.
23766
23767 To determine the base file name, we scan the macro definitions until we
23768 reach the first DW_MACINFO_start_file entry. We then initialize
23769 CURRENT_FILE accordingly so that any macro definition found before the
23770 first DW_MACINFO_start_file can still be associated to the base file. */
23771
23772 mac_ptr = section->buffer + offset;
23773 mac_end = section->buffer + section->size;
23774
23775 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23776 &offset_size, section_is_gnu);
23777 if (mac_ptr == NULL)
23778 {
23779 /* We already issued a complaint. */
23780 return;
23781 }
23782
23783 do
23784 {
23785 /* Do we at least have room for a macinfo type byte? */
23786 if (mac_ptr >= mac_end)
23787 {
23788 /* Complaint is printed during the second pass as GDB will probably
23789 stop the first pass earlier upon finding
23790 DW_MACINFO_start_file. */
23791 break;
23792 }
23793
23794 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23795 mac_ptr++;
23796
23797 /* Note that we rely on the fact that the corresponding GNU and
23798 DWARF constants are the same. */
23799 DIAGNOSTIC_PUSH
23800 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23801 switch (macinfo_type)
23802 {
23803 /* A zero macinfo type indicates the end of the macro
23804 information. */
23805 case 0:
23806 break;
23807
23808 case DW_MACRO_define:
23809 case DW_MACRO_undef:
23810 /* Only skip the data by MAC_PTR. */
23811 {
23812 unsigned int bytes_read;
23813
23814 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23815 mac_ptr += bytes_read;
23816 read_direct_string (abfd, mac_ptr, &bytes_read);
23817 mac_ptr += bytes_read;
23818 }
23819 break;
23820
23821 case DW_MACRO_start_file:
23822 {
23823 unsigned int bytes_read;
23824 int line, file;
23825
23826 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23827 mac_ptr += bytes_read;
23828 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23829 mac_ptr += bytes_read;
23830
23831 current_file = macro_start_file (builder, file, line,
23832 current_file, lh);
23833 }
23834 break;
23835
23836 case DW_MACRO_end_file:
23837 /* No data to skip by MAC_PTR. */
23838 break;
23839
23840 case DW_MACRO_define_strp:
23841 case DW_MACRO_undef_strp:
23842 case DW_MACRO_define_sup:
23843 case DW_MACRO_undef_sup:
23844 {
23845 unsigned int bytes_read;
23846
23847 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23848 mac_ptr += bytes_read;
23849 mac_ptr += offset_size;
23850 }
23851 break;
23852
23853 case DW_MACRO_import:
23854 case DW_MACRO_import_sup:
23855 /* Note that, according to the spec, a transparent include
23856 chain cannot call DW_MACRO_start_file. So, we can just
23857 skip this opcode. */
23858 mac_ptr += offset_size;
23859 break;
23860
23861 case DW_MACINFO_vendor_ext:
23862 /* Only skip the data by MAC_PTR. */
23863 if (!section_is_gnu)
23864 {
23865 unsigned int bytes_read;
23866
23867 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23868 mac_ptr += bytes_read;
23869 read_direct_string (abfd, mac_ptr, &bytes_read);
23870 mac_ptr += bytes_read;
23871 }
23872 /* FALLTHROUGH */
23873
23874 default:
23875 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23876 mac_ptr, mac_end, abfd, offset_size,
23877 section);
23878 if (mac_ptr == NULL)
23879 return;
23880 break;
23881 }
23882 DIAGNOSTIC_POP
23883 } while (macinfo_type != 0 && current_file == NULL);
23884
23885 /* Second pass: Process all entries.
23886
23887 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23888 command-line macro definitions/undefinitions. This flag is unset when we
23889 reach the first DW_MACINFO_start_file entry. */
23890
23891 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23892 htab_eq_pointer,
23893 NULL, xcalloc, xfree));
23894 mac_ptr = section->buffer + offset;
23895 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
23896 *slot = (void *) mac_ptr;
23897 dwarf_decode_macro_bytes (dwarf2_per_objfile, builder,
23898 abfd, mac_ptr, mac_end,
23899 current_file, lh, section,
23900 section_is_gnu, 0, offset_size,
23901 include_hash.get ());
23902 }
23903
23904 /* An overload of dwarf_decode_macros that finds the correct section
23905 and ensures it is read in before calling the other overload. */
23906
23907 static void
23908 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23909 int section_is_gnu)
23910 {
23911 struct dwarf2_per_objfile *dwarf2_per_objfile
23912 = cu->per_cu->dwarf2_per_objfile;
23913 struct objfile *objfile = dwarf2_per_objfile->objfile;
23914 struct line_header *lh = cu->line_header;
23915 unsigned int offset_size = cu->header.offset_size;
23916 struct dwarf2_section_info *section;
23917 const char *section_name;
23918
23919 if (cu->dwo_unit != nullptr)
23920 {
23921 if (section_is_gnu)
23922 {
23923 section = &cu->dwo_unit->dwo_file->sections.macro;
23924 section_name = ".debug_macro.dwo";
23925 }
23926 else
23927 {
23928 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23929 section_name = ".debug_macinfo.dwo";
23930 }
23931 }
23932 else
23933 {
23934 if (section_is_gnu)
23935 {
23936 section = &dwarf2_per_objfile->macro;
23937 section_name = ".debug_macro";
23938 }
23939 else
23940 {
23941 section = &dwarf2_per_objfile->macinfo;
23942 section_name = ".debug_macinfo";
23943 }
23944 }
23945
23946 section->read (objfile);
23947 if (section->buffer == nullptr)
23948 {
23949 complaint (_("missing %s section"), section_name);
23950 return;
23951 }
23952
23953 buildsym_compunit *builder = cu->get_builder ();
23954
23955 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23956 offset_size, offset, section_is_gnu);
23957 }
23958
23959 /* Return the .debug_loc section to use for CU.
23960 For DWO files use .debug_loc.dwo. */
23961
23962 static struct dwarf2_section_info *
23963 cu_debug_loc_section (struct dwarf2_cu *cu)
23964 {
23965 struct dwarf2_per_objfile *dwarf2_per_objfile
23966 = cu->per_cu->dwarf2_per_objfile;
23967
23968 if (cu->dwo_unit)
23969 {
23970 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23971
23972 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23973 }
23974 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23975 : &dwarf2_per_objfile->loc);
23976 }
23977
23978 /* A helper function that fills in a dwarf2_loclist_baton. */
23979
23980 static void
23981 fill_in_loclist_baton (struct dwarf2_cu *cu,
23982 struct dwarf2_loclist_baton *baton,
23983 const struct attribute *attr)
23984 {
23985 struct dwarf2_per_objfile *dwarf2_per_objfile
23986 = cu->per_cu->dwarf2_per_objfile;
23987 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23988
23989 section->read (dwarf2_per_objfile->objfile);
23990
23991 baton->per_cu = cu->per_cu;
23992 gdb_assert (baton->per_cu);
23993 /* We don't know how long the location list is, but make sure we
23994 don't run off the edge of the section. */
23995 baton->size = section->size - DW_UNSND (attr);
23996 baton->data = section->buffer + DW_UNSND (attr);
23997 baton->base_address = cu->base_address;
23998 baton->from_dwo = cu->dwo_unit != NULL;
23999 }
24000
24001 static void
24002 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24003 struct dwarf2_cu *cu, int is_block)
24004 {
24005 struct dwarf2_per_objfile *dwarf2_per_objfile
24006 = cu->per_cu->dwarf2_per_objfile;
24007 struct objfile *objfile = dwarf2_per_objfile->objfile;
24008 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24009
24010 if (attr->form_is_section_offset ()
24011 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24012 the section. If so, fall through to the complaint in the
24013 other branch. */
24014 && DW_UNSND (attr) < section->get_size (objfile))
24015 {
24016 struct dwarf2_loclist_baton *baton;
24017
24018 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24019
24020 fill_in_loclist_baton (cu, baton, attr);
24021
24022 if (cu->base_known == 0)
24023 complaint (_("Location list used without "
24024 "specifying the CU base address."));
24025
24026 SYMBOL_ACLASS_INDEX (sym) = (is_block
24027 ? dwarf2_loclist_block_index
24028 : dwarf2_loclist_index);
24029 SYMBOL_LOCATION_BATON (sym) = baton;
24030 }
24031 else
24032 {
24033 struct dwarf2_locexpr_baton *baton;
24034
24035 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24036 baton->per_cu = cu->per_cu;
24037 gdb_assert (baton->per_cu);
24038
24039 if (attr->form_is_block ())
24040 {
24041 /* Note that we're just copying the block's data pointer
24042 here, not the actual data. We're still pointing into the
24043 info_buffer for SYM's objfile; right now we never release
24044 that buffer, but when we do clean up properly this may
24045 need to change. */
24046 baton->size = DW_BLOCK (attr)->size;
24047 baton->data = DW_BLOCK (attr)->data;
24048 }
24049 else
24050 {
24051 dwarf2_invalid_attrib_class_complaint ("location description",
24052 sym->natural_name ());
24053 baton->size = 0;
24054 }
24055
24056 SYMBOL_ACLASS_INDEX (sym) = (is_block
24057 ? dwarf2_locexpr_block_index
24058 : dwarf2_locexpr_index);
24059 SYMBOL_LOCATION_BATON (sym) = baton;
24060 }
24061 }
24062
24063 /* See read.h. */
24064
24065 struct objfile *
24066 dwarf2_per_cu_data::objfile () const
24067 {
24068 struct objfile *objfile = dwarf2_per_objfile->objfile;
24069
24070 /* Return the master objfile, so that we can report and look up the
24071 correct file containing this variable. */
24072 if (objfile->separate_debug_objfile_backlink)
24073 objfile = objfile->separate_debug_objfile_backlink;
24074
24075 return objfile;
24076 }
24077
24078 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24079 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24080 CU_HEADERP first. */
24081
24082 static const struct comp_unit_head *
24083 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24084 const struct dwarf2_per_cu_data *per_cu)
24085 {
24086 const gdb_byte *info_ptr;
24087
24088 if (per_cu->cu)
24089 return &per_cu->cu->header;
24090
24091 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24092
24093 memset (cu_headerp, 0, sizeof (*cu_headerp));
24094 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24095 rcuh_kind::COMPILE);
24096
24097 return cu_headerp;
24098 }
24099
24100 /* See read.h. */
24101
24102 int
24103 dwarf2_per_cu_data::addr_size () const
24104 {
24105 struct comp_unit_head cu_header_local;
24106 const struct comp_unit_head *cu_headerp;
24107
24108 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24109
24110 return cu_headerp->addr_size;
24111 }
24112
24113 /* See read.h. */
24114
24115 int
24116 dwarf2_per_cu_data::offset_size () const
24117 {
24118 struct comp_unit_head cu_header_local;
24119 const struct comp_unit_head *cu_headerp;
24120
24121 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24122
24123 return cu_headerp->offset_size;
24124 }
24125
24126 /* See read.h. */
24127
24128 int
24129 dwarf2_per_cu_data::ref_addr_size () const
24130 {
24131 struct comp_unit_head cu_header_local;
24132 const struct comp_unit_head *cu_headerp;
24133
24134 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24135
24136 if (cu_headerp->version == 2)
24137 return cu_headerp->addr_size;
24138 else
24139 return cu_headerp->offset_size;
24140 }
24141
24142 /* See read.h. */
24143
24144 CORE_ADDR
24145 dwarf2_per_cu_data::text_offset () const
24146 {
24147 struct objfile *objfile = dwarf2_per_objfile->objfile;
24148
24149 return objfile->text_section_offset ();
24150 }
24151
24152 /* See read.h. */
24153
24154 struct type *
24155 dwarf2_per_cu_data::addr_type () const
24156 {
24157 struct objfile *objfile = dwarf2_per_objfile->objfile;
24158 struct type *void_type = objfile_type (objfile)->builtin_void;
24159 struct type *addr_type = lookup_pointer_type (void_type);
24160 int addr_size = this->addr_size ();
24161
24162 if (TYPE_LENGTH (addr_type) == addr_size)
24163 return addr_type;
24164
24165 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
24166 return addr_type;
24167 }
24168
24169 /* A helper function for dwarf2_find_containing_comp_unit that returns
24170 the index of the result, and that searches a vector. It will
24171 return a result even if the offset in question does not actually
24172 occur in any CU. This is separate so that it can be unit
24173 tested. */
24174
24175 static int
24176 dwarf2_find_containing_comp_unit
24177 (sect_offset sect_off,
24178 unsigned int offset_in_dwz,
24179 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24180 {
24181 int low, high;
24182
24183 low = 0;
24184 high = all_comp_units.size () - 1;
24185 while (high > low)
24186 {
24187 struct dwarf2_per_cu_data *mid_cu;
24188 int mid = low + (high - low) / 2;
24189
24190 mid_cu = all_comp_units[mid];
24191 if (mid_cu->is_dwz > offset_in_dwz
24192 || (mid_cu->is_dwz == offset_in_dwz
24193 && mid_cu->sect_off + mid_cu->length > sect_off))
24194 high = mid;
24195 else
24196 low = mid + 1;
24197 }
24198 gdb_assert (low == high);
24199 return low;
24200 }
24201
24202 /* Locate the .debug_info compilation unit from CU's objfile which contains
24203 the DIE at OFFSET. Raises an error on failure. */
24204
24205 static struct dwarf2_per_cu_data *
24206 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24207 unsigned int offset_in_dwz,
24208 struct dwarf2_per_objfile *dwarf2_per_objfile)
24209 {
24210 int low
24211 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
24212 dwarf2_per_objfile->all_comp_units);
24213 struct dwarf2_per_cu_data *this_cu
24214 = dwarf2_per_objfile->all_comp_units[low];
24215
24216 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24217 {
24218 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24219 error (_("Dwarf Error: could not find partial DIE containing "
24220 "offset %s [in module %s]"),
24221 sect_offset_str (sect_off),
24222 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24223
24224 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24225 <= sect_off);
24226 return dwarf2_per_objfile->all_comp_units[low-1];
24227 }
24228 else
24229 {
24230 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24231 && sect_off >= this_cu->sect_off + this_cu->length)
24232 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24233 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24234 return this_cu;
24235 }
24236 }
24237
24238 #if GDB_SELF_TEST
24239
24240 namespace selftests {
24241 namespace find_containing_comp_unit {
24242
24243 static void
24244 run_test ()
24245 {
24246 struct dwarf2_per_cu_data one {};
24247 struct dwarf2_per_cu_data two {};
24248 struct dwarf2_per_cu_data three {};
24249 struct dwarf2_per_cu_data four {};
24250
24251 one.length = 5;
24252 two.sect_off = sect_offset (one.length);
24253 two.length = 7;
24254
24255 three.length = 5;
24256 three.is_dwz = 1;
24257 four.sect_off = sect_offset (three.length);
24258 four.length = 7;
24259 four.is_dwz = 1;
24260
24261 std::vector<dwarf2_per_cu_data *> units;
24262 units.push_back (&one);
24263 units.push_back (&two);
24264 units.push_back (&three);
24265 units.push_back (&four);
24266
24267 int result;
24268
24269 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24270 SELF_CHECK (units[result] == &one);
24271 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24272 SELF_CHECK (units[result] == &one);
24273 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24274 SELF_CHECK (units[result] == &two);
24275
24276 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24277 SELF_CHECK (units[result] == &three);
24278 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24279 SELF_CHECK (units[result] == &three);
24280 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24281 SELF_CHECK (units[result] == &four);
24282 }
24283
24284 }
24285 }
24286
24287 #endif /* GDB_SELF_TEST */
24288
24289 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24290
24291 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24292 : per_cu (per_cu_),
24293 mark (false),
24294 has_loclist (false),
24295 checked_producer (false),
24296 producer_is_gxx_lt_4_6 (false),
24297 producer_is_gcc_lt_4_3 (false),
24298 producer_is_icc (false),
24299 producer_is_icc_lt_14 (false),
24300 producer_is_codewarrior (false),
24301 processing_has_namespace_info (false)
24302 {
24303 per_cu->cu = this;
24304 }
24305
24306 /* Destroy a dwarf2_cu. */
24307
24308 dwarf2_cu::~dwarf2_cu ()
24309 {
24310 per_cu->cu = NULL;
24311 }
24312
24313 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24314
24315 static void
24316 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24317 enum language pretend_language)
24318 {
24319 struct attribute *attr;
24320
24321 /* Set the language we're debugging. */
24322 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24323 if (attr != nullptr)
24324 set_cu_language (DW_UNSND (attr), cu);
24325 else
24326 {
24327 cu->language = pretend_language;
24328 cu->language_defn = language_def (cu->language);
24329 }
24330
24331 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24332 }
24333
24334 /* Increase the age counter on each cached compilation unit, and free
24335 any that are too old. */
24336
24337 static void
24338 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
24339 {
24340 struct dwarf2_per_cu_data *per_cu, **last_chain;
24341
24342 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24343 per_cu = dwarf2_per_objfile->read_in_chain;
24344 while (per_cu != NULL)
24345 {
24346 per_cu->cu->last_used ++;
24347 if (per_cu->cu->last_used <= dwarf_max_cache_age)
24348 dwarf2_mark (per_cu->cu);
24349 per_cu = per_cu->cu->read_in_chain;
24350 }
24351
24352 per_cu = dwarf2_per_objfile->read_in_chain;
24353 last_chain = &dwarf2_per_objfile->read_in_chain;
24354 while (per_cu != NULL)
24355 {
24356 struct dwarf2_per_cu_data *next_cu;
24357
24358 next_cu = per_cu->cu->read_in_chain;
24359
24360 if (!per_cu->cu->mark)
24361 {
24362 delete per_cu->cu;
24363 *last_chain = next_cu;
24364 }
24365 else
24366 last_chain = &per_cu->cu->read_in_chain;
24367
24368 per_cu = next_cu;
24369 }
24370 }
24371
24372 /* Remove a single compilation unit from the cache. */
24373
24374 static void
24375 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
24376 {
24377 struct dwarf2_per_cu_data *per_cu, **last_chain;
24378 struct dwarf2_per_objfile *dwarf2_per_objfile
24379 = target_per_cu->dwarf2_per_objfile;
24380
24381 per_cu = dwarf2_per_objfile->read_in_chain;
24382 last_chain = &dwarf2_per_objfile->read_in_chain;
24383 while (per_cu != NULL)
24384 {
24385 struct dwarf2_per_cu_data *next_cu;
24386
24387 next_cu = per_cu->cu->read_in_chain;
24388
24389 if (per_cu == target_per_cu)
24390 {
24391 delete per_cu->cu;
24392 per_cu->cu = NULL;
24393 *last_chain = next_cu;
24394 break;
24395 }
24396 else
24397 last_chain = &per_cu->cu->read_in_chain;
24398
24399 per_cu = next_cu;
24400 }
24401 }
24402
24403 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24404 We store these in a hash table separate from the DIEs, and preserve them
24405 when the DIEs are flushed out of cache.
24406
24407 The CU "per_cu" pointer is needed because offset alone is not enough to
24408 uniquely identify the type. A file may have multiple .debug_types sections,
24409 or the type may come from a DWO file. Furthermore, while it's more logical
24410 to use per_cu->section+offset, with Fission the section with the data is in
24411 the DWO file but we don't know that section at the point we need it.
24412 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24413 because we can enter the lookup routine, get_die_type_at_offset, from
24414 outside this file, and thus won't necessarily have PER_CU->cu.
24415 Fortunately, PER_CU is stable for the life of the objfile. */
24416
24417 struct dwarf2_per_cu_offset_and_type
24418 {
24419 const struct dwarf2_per_cu_data *per_cu;
24420 sect_offset sect_off;
24421 struct type *type;
24422 };
24423
24424 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24425
24426 static hashval_t
24427 per_cu_offset_and_type_hash (const void *item)
24428 {
24429 const struct dwarf2_per_cu_offset_and_type *ofs
24430 = (const struct dwarf2_per_cu_offset_and_type *) item;
24431
24432 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24433 }
24434
24435 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24436
24437 static int
24438 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24439 {
24440 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24441 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24442 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24443 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24444
24445 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24446 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24447 }
24448
24449 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24450 table if necessary. For convenience, return TYPE.
24451
24452 The DIEs reading must have careful ordering to:
24453 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24454 reading current DIE.
24455 * Not trying to dereference contents of still incompletely read in types
24456 while reading in other DIEs.
24457 * Enable referencing still incompletely read in types just by a pointer to
24458 the type without accessing its fields.
24459
24460 Therefore caller should follow these rules:
24461 * Try to fetch any prerequisite types we may need to build this DIE type
24462 before building the type and calling set_die_type.
24463 * After building type call set_die_type for current DIE as soon as
24464 possible before fetching more types to complete the current type.
24465 * Make the type as complete as possible before fetching more types. */
24466
24467 static struct type *
24468 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24469 {
24470 struct dwarf2_per_objfile *dwarf2_per_objfile
24471 = cu->per_cu->dwarf2_per_objfile;
24472 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24473 struct objfile *objfile = dwarf2_per_objfile->objfile;
24474 struct attribute *attr;
24475 struct dynamic_prop prop;
24476
24477 /* For Ada types, make sure that the gnat-specific data is always
24478 initialized (if not already set). There are a few types where
24479 we should not be doing so, because the type-specific area is
24480 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24481 where the type-specific area is used to store the floatformat).
24482 But this is not a problem, because the gnat-specific information
24483 is actually not needed for these types. */
24484 if (need_gnat_info (cu)
24485 && TYPE_CODE (type) != TYPE_CODE_FUNC
24486 && TYPE_CODE (type) != TYPE_CODE_FLT
24487 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24488 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24489 && TYPE_CODE (type) != TYPE_CODE_METHOD
24490 && !HAVE_GNAT_AUX_INFO (type))
24491 INIT_GNAT_SPECIFIC (type);
24492
24493 /* Read DW_AT_allocated and set in type. */
24494 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24495 if (attr != NULL && attr->form_is_block ())
24496 {
24497 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24498 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24499 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
24500 }
24501 else if (attr != NULL)
24502 {
24503 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
24504 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24505 sect_offset_str (die->sect_off));
24506 }
24507
24508 /* Read DW_AT_associated and set in type. */
24509 attr = dwarf2_attr (die, DW_AT_associated, cu);
24510 if (attr != NULL && attr->form_is_block ())
24511 {
24512 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24513 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24514 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
24515 }
24516 else if (attr != NULL)
24517 {
24518 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
24519 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24520 sect_offset_str (die->sect_off));
24521 }
24522
24523 /* Read DW_AT_data_location and set in type. */
24524 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24525 if (attr_to_dynamic_prop (attr, die, cu, &prop,
24526 cu->per_cu->addr_type ()))
24527 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
24528
24529 if (dwarf2_per_objfile->die_type_hash == NULL)
24530 dwarf2_per_objfile->die_type_hash
24531 = htab_up (htab_create_alloc (127,
24532 per_cu_offset_and_type_hash,
24533 per_cu_offset_and_type_eq,
24534 NULL, xcalloc, xfree));
24535
24536 ofs.per_cu = cu->per_cu;
24537 ofs.sect_off = die->sect_off;
24538 ofs.type = type;
24539 slot = (struct dwarf2_per_cu_offset_and_type **)
24540 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
24541 if (*slot)
24542 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24543 sect_offset_str (die->sect_off));
24544 *slot = XOBNEW (&objfile->objfile_obstack,
24545 struct dwarf2_per_cu_offset_and_type);
24546 **slot = ofs;
24547 return type;
24548 }
24549
24550 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24551 or return NULL if the die does not have a saved type. */
24552
24553 static struct type *
24554 get_die_type_at_offset (sect_offset sect_off,
24555 struct dwarf2_per_cu_data *per_cu)
24556 {
24557 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24558 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
24559
24560 if (dwarf2_per_objfile->die_type_hash == NULL)
24561 return NULL;
24562
24563 ofs.per_cu = per_cu;
24564 ofs.sect_off = sect_off;
24565 slot = ((struct dwarf2_per_cu_offset_and_type *)
24566 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
24567 if (slot)
24568 return slot->type;
24569 else
24570 return NULL;
24571 }
24572
24573 /* Look up the type for DIE in CU in die_type_hash,
24574 or return NULL if DIE does not have a saved type. */
24575
24576 static struct type *
24577 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24578 {
24579 return get_die_type_at_offset (die->sect_off, cu->per_cu);
24580 }
24581
24582 /* Add a dependence relationship from CU to REF_PER_CU. */
24583
24584 static void
24585 dwarf2_add_dependence (struct dwarf2_cu *cu,
24586 struct dwarf2_per_cu_data *ref_per_cu)
24587 {
24588 void **slot;
24589
24590 if (cu->dependencies == NULL)
24591 cu->dependencies
24592 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24593 NULL, &cu->comp_unit_obstack,
24594 hashtab_obstack_allocate,
24595 dummy_obstack_deallocate);
24596
24597 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24598 if (*slot == NULL)
24599 *slot = ref_per_cu;
24600 }
24601
24602 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24603 Set the mark field in every compilation unit in the
24604 cache that we must keep because we are keeping CU. */
24605
24606 static int
24607 dwarf2_mark_helper (void **slot, void *data)
24608 {
24609 struct dwarf2_per_cu_data *per_cu;
24610
24611 per_cu = (struct dwarf2_per_cu_data *) *slot;
24612
24613 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24614 reading of the chain. As such dependencies remain valid it is not much
24615 useful to track and undo them during QUIT cleanups. */
24616 if (per_cu->cu == NULL)
24617 return 1;
24618
24619 if (per_cu->cu->mark)
24620 return 1;
24621 per_cu->cu->mark = true;
24622
24623 if (per_cu->cu->dependencies != NULL)
24624 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24625
24626 return 1;
24627 }
24628
24629 /* Set the mark field in CU and in every other compilation unit in the
24630 cache that we must keep because we are keeping CU. */
24631
24632 static void
24633 dwarf2_mark (struct dwarf2_cu *cu)
24634 {
24635 if (cu->mark)
24636 return;
24637 cu->mark = true;
24638 if (cu->dependencies != NULL)
24639 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
24640 }
24641
24642 static void
24643 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24644 {
24645 while (per_cu)
24646 {
24647 per_cu->cu->mark = false;
24648 per_cu = per_cu->cu->read_in_chain;
24649 }
24650 }
24651
24652 /* Trivial hash function for partial_die_info: the hash value of a DIE
24653 is its offset in .debug_info for this objfile. */
24654
24655 static hashval_t
24656 partial_die_hash (const void *item)
24657 {
24658 const struct partial_die_info *part_die
24659 = (const struct partial_die_info *) item;
24660
24661 return to_underlying (part_die->sect_off);
24662 }
24663
24664 /* Trivial comparison function for partial_die_info structures: two DIEs
24665 are equal if they have the same offset. */
24666
24667 static int
24668 partial_die_eq (const void *item_lhs, const void *item_rhs)
24669 {
24670 const struct partial_die_info *part_die_lhs
24671 = (const struct partial_die_info *) item_lhs;
24672 const struct partial_die_info *part_die_rhs
24673 = (const struct partial_die_info *) item_rhs;
24674
24675 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24676 }
24677
24678 struct cmd_list_element *set_dwarf_cmdlist;
24679 struct cmd_list_element *show_dwarf_cmdlist;
24680
24681 static void
24682 set_dwarf_cmd (const char *args, int from_tty)
24683 {
24684 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
24685 gdb_stdout);
24686 }
24687
24688 static void
24689 show_dwarf_cmd (const char *args, int from_tty)
24690 {
24691 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
24692 }
24693
24694 static void
24695 show_check_physname (struct ui_file *file, int from_tty,
24696 struct cmd_list_element *c, const char *value)
24697 {
24698 fprintf_filtered (file,
24699 _("Whether to check \"physname\" is %s.\n"),
24700 value);
24701 }
24702
24703 void _initialize_dwarf2_read ();
24704 void
24705 _initialize_dwarf2_read ()
24706 {
24707 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24708 Set DWARF specific variables.\n\
24709 Configure DWARF variables such as the cache size."),
24710 &set_dwarf_cmdlist, "maintenance set dwarf ",
24711 0/*allow-unknown*/, &maintenance_set_cmdlist);
24712
24713 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24714 Show DWARF specific variables.\n\
24715 Show DWARF variables such as the cache size."),
24716 &show_dwarf_cmdlist, "maintenance show dwarf ",
24717 0/*allow-unknown*/, &maintenance_show_cmdlist);
24718
24719 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24720 &dwarf_max_cache_age, _("\
24721 Set the upper bound on the age of cached DWARF compilation units."), _("\
24722 Show the upper bound on the age of cached DWARF compilation units."), _("\
24723 A higher limit means that cached compilation units will be stored\n\
24724 in memory longer, and more total memory will be used. Zero disables\n\
24725 caching, which can slow down startup."),
24726 NULL,
24727 show_dwarf_max_cache_age,
24728 &set_dwarf_cmdlist,
24729 &show_dwarf_cmdlist);
24730
24731 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24732 Set debugging of the DWARF reader."), _("\
24733 Show debugging of the DWARF reader."), _("\
24734 When enabled (non-zero), debugging messages are printed during DWARF\n\
24735 reading and symtab expansion. A value of 1 (one) provides basic\n\
24736 information. A value greater than 1 provides more verbose information."),
24737 NULL,
24738 NULL,
24739 &setdebuglist, &showdebuglist);
24740
24741 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24742 Set debugging of the DWARF DIE reader."), _("\
24743 Show debugging of the DWARF DIE reader."), _("\
24744 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24745 The value is the maximum depth to print."),
24746 NULL,
24747 NULL,
24748 &setdebuglist, &showdebuglist);
24749
24750 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24751 Set debugging of the dwarf line reader."), _("\
24752 Show debugging of the dwarf line reader."), _("\
24753 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24754 A value of 1 (one) provides basic information.\n\
24755 A value greater than 1 provides more verbose information."),
24756 NULL,
24757 NULL,
24758 &setdebuglist, &showdebuglist);
24759
24760 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24761 Set cross-checking of \"physname\" code against demangler."), _("\
24762 Show cross-checking of \"physname\" code against demangler."), _("\
24763 When enabled, GDB's internal \"physname\" code is checked against\n\
24764 the demangler."),
24765 NULL, show_check_physname,
24766 &setdebuglist, &showdebuglist);
24767
24768 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24769 no_class, &use_deprecated_index_sections, _("\
24770 Set whether to use deprecated gdb_index sections."), _("\
24771 Show whether to use deprecated gdb_index sections."), _("\
24772 When enabled, deprecated .gdb_index sections are used anyway.\n\
24773 Normally they are ignored either because of a missing feature or\n\
24774 performance issue.\n\
24775 Warning: This option must be enabled before gdb reads the file."),
24776 NULL,
24777 NULL,
24778 &setlist, &showlist);
24779
24780 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24781 &dwarf2_locexpr_funcs);
24782 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24783 &dwarf2_loclist_funcs);
24784
24785 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24786 &dwarf2_block_frame_base_locexpr_funcs);
24787 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24788 &dwarf2_block_frame_base_loclist_funcs);
24789
24790 #if GDB_SELF_TEST
24791 selftests::register_test ("dw2_expand_symtabs_matching",
24792 selftests::dw2_expand_symtabs_matching::run_test);
24793 selftests::register_test ("dwarf2_find_containing_comp_unit",
24794 selftests::find_containing_comp_unit::run_test);
24795 #endif
24796 }
This page took 0.519579 seconds and 5 git commands to generate.