gdb: Add support for tracking the DWARF line table is-stmt field
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "bfd.h"
41 #include "elf-bfd.h"
42 #include "symtab.h"
43 #include "gdbtypes.h"
44 #include "objfiles.h"
45 #include "dwarf2.h"
46 #include "buildsym.h"
47 #include "demangle.h"
48 #include "gdb-demangle.h"
49 #include "filenames.h" /* for DOSish file names */
50 #include "macrotab.h"
51 #include "language.h"
52 #include "complaints.h"
53 #include "dwarf2/expr.h"
54 #include "dwarf2/loc.h"
55 #include "cp-support.h"
56 #include "hashtab.h"
57 #include "command.h"
58 #include "gdbcmd.h"
59 #include "block.h"
60 #include "addrmap.h"
61 #include "typeprint.h"
62 #include "psympriv.h"
63 #include "c-lang.h"
64 #include "go-lang.h"
65 #include "valprint.h"
66 #include "gdbcore.h" /* for gnutarget */
67 #include "gdb/gdb-index.h"
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71 #include "build-id.h"
72 #include "namespace.h"
73 #include "gdbsupport/function-view.h"
74 #include "gdbsupport/gdb_optional.h"
75 #include "gdbsupport/underlying.h"
76 #include "gdbsupport/hash_enum.h"
77 #include "filename-seen-cache.h"
78 #include "producer.h"
79 #include <fcntl.h>
80 #include <algorithm>
81 #include <unordered_map>
82 #include "gdbsupport/selftest.h"
83 #include "rust-lang.h"
84 #include "gdbsupport/pathstuff.h"
85 #include "count-one-bits.h"
86 #include "debuginfod-support.h"
87
88 /* When == 1, print basic high level tracing messages.
89 When > 1, be more verbose.
90 This is in contrast to the low level DIE reading of dwarf_die_debug. */
91 static unsigned int dwarf_read_debug = 0;
92
93 /* When non-zero, dump DIEs after they are read in. */
94 static unsigned int dwarf_die_debug = 0;
95
96 /* When non-zero, dump line number entries as they are read in. */
97 unsigned int dwarf_line_debug = 0;
98
99 /* When true, cross-check physname against demangler. */
100 static bool check_physname = false;
101
102 /* When true, do not reject deprecated .gdb_index sections. */
103 static bool use_deprecated_index_sections = false;
104
105 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
106
107 /* The "aclass" indices for various kinds of computed DWARF symbols. */
108
109 static int dwarf2_locexpr_index;
110 static int dwarf2_loclist_index;
111 static int dwarf2_locexpr_block_index;
112 static int dwarf2_loclist_block_index;
113
114 /* An index into a (C++) symbol name component in a symbol name as
115 recorded in the mapped_index's symbol table. For each C++ symbol
116 in the symbol table, we record one entry for the start of each
117 component in the symbol in a table of name components, and then
118 sort the table, in order to be able to binary search symbol names,
119 ignoring leading namespaces, both completion and regular look up.
120 For example, for symbol "A::B::C", we'll have an entry that points
121 to "A::B::C", another that points to "B::C", and another for "C".
122 Note that function symbols in GDB index have no parameter
123 information, just the function/method names. You can convert a
124 name_component to a "const char *" using the
125 'mapped_index::symbol_name_at(offset_type)' method. */
126
127 struct name_component
128 {
129 /* Offset in the symbol name where the component starts. Stored as
130 a (32-bit) offset instead of a pointer to save memory and improve
131 locality on 64-bit architectures. */
132 offset_type name_offset;
133
134 /* The symbol's index in the symbol and constant pool tables of a
135 mapped_index. */
136 offset_type idx;
137 };
138
139 /* Base class containing bits shared by both .gdb_index and
140 .debug_name indexes. */
141
142 struct mapped_index_base
143 {
144 mapped_index_base () = default;
145 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
146
147 /* The name_component table (a sorted vector). See name_component's
148 description above. */
149 std::vector<name_component> name_components;
150
151 /* How NAME_COMPONENTS is sorted. */
152 enum case_sensitivity name_components_casing;
153
154 /* Return the number of names in the symbol table. */
155 virtual size_t symbol_name_count () const = 0;
156
157 /* Get the name of the symbol at IDX in the symbol table. */
158 virtual const char *symbol_name_at (offset_type idx) const = 0;
159
160 /* Return whether the name at IDX in the symbol table should be
161 ignored. */
162 virtual bool symbol_name_slot_invalid (offset_type idx) const
163 {
164 return false;
165 }
166
167 /* Build the symbol name component sorted vector, if we haven't
168 yet. */
169 void build_name_components ();
170
171 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
172 possible matches for LN_NO_PARAMS in the name component
173 vector. */
174 std::pair<std::vector<name_component>::const_iterator,
175 std::vector<name_component>::const_iterator>
176 find_name_components_bounds (const lookup_name_info &ln_no_params,
177 enum language lang) const;
178
179 /* Prevent deleting/destroying via a base class pointer. */
180 protected:
181 ~mapped_index_base() = default;
182 };
183
184 /* A description of the mapped index. The file format is described in
185 a comment by the code that writes the index. */
186 struct mapped_index final : public mapped_index_base
187 {
188 /* A slot/bucket in the symbol table hash. */
189 struct symbol_table_slot
190 {
191 const offset_type name;
192 const offset_type vec;
193 };
194
195 /* Index data format version. */
196 int version = 0;
197
198 /* The address table data. */
199 gdb::array_view<const gdb_byte> address_table;
200
201 /* The symbol table, implemented as a hash table. */
202 gdb::array_view<symbol_table_slot> symbol_table;
203
204 /* A pointer to the constant pool. */
205 const char *constant_pool = nullptr;
206
207 bool symbol_name_slot_invalid (offset_type idx) const override
208 {
209 const auto &bucket = this->symbol_table[idx];
210 return bucket.name == 0 && bucket.vec == 0;
211 }
212
213 /* Convenience method to get at the name of the symbol at IDX in the
214 symbol table. */
215 const char *symbol_name_at (offset_type idx) const override
216 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
217
218 size_t symbol_name_count () const override
219 { return this->symbol_table.size (); }
220 };
221
222 /* A description of the mapped .debug_names.
223 Uninitialized map has CU_COUNT 0. */
224 struct mapped_debug_names final : public mapped_index_base
225 {
226 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
227 : dwarf2_per_objfile (dwarf2_per_objfile_)
228 {}
229
230 struct dwarf2_per_objfile *dwarf2_per_objfile;
231 bfd_endian dwarf5_byte_order;
232 bool dwarf5_is_dwarf64;
233 bool augmentation_is_gdb;
234 uint8_t offset_size;
235 uint32_t cu_count = 0;
236 uint32_t tu_count, bucket_count, name_count;
237 const gdb_byte *cu_table_reordered, *tu_table_reordered;
238 const uint32_t *bucket_table_reordered, *hash_table_reordered;
239 const gdb_byte *name_table_string_offs_reordered;
240 const gdb_byte *name_table_entry_offs_reordered;
241 const gdb_byte *entry_pool;
242
243 struct index_val
244 {
245 ULONGEST dwarf_tag;
246 struct attr
247 {
248 /* Attribute name DW_IDX_*. */
249 ULONGEST dw_idx;
250
251 /* Attribute form DW_FORM_*. */
252 ULONGEST form;
253
254 /* Value if FORM is DW_FORM_implicit_const. */
255 LONGEST implicit_const;
256 };
257 std::vector<attr> attr_vec;
258 };
259
260 std::unordered_map<ULONGEST, index_val> abbrev_map;
261
262 const char *namei_to_name (uint32_t namei) const;
263
264 /* Implementation of the mapped_index_base virtual interface, for
265 the name_components cache. */
266
267 const char *symbol_name_at (offset_type idx) const override
268 { return namei_to_name (idx); }
269
270 size_t symbol_name_count () const override
271 { return this->name_count; }
272 };
273
274 /* See dwarf2read.h. */
275
276 dwarf2_per_objfile *
277 get_dwarf2_per_objfile (struct objfile *objfile)
278 {
279 return dwarf2_objfile_data_key.get (objfile);
280 }
281
282 /* Default names of the debugging sections. */
283
284 /* Note that if the debugging section has been compressed, it might
285 have a name like .zdebug_info. */
286
287 static const struct dwarf2_debug_sections dwarf2_elf_names =
288 {
289 { ".debug_info", ".zdebug_info" },
290 { ".debug_abbrev", ".zdebug_abbrev" },
291 { ".debug_line", ".zdebug_line" },
292 { ".debug_loc", ".zdebug_loc" },
293 { ".debug_loclists", ".zdebug_loclists" },
294 { ".debug_macinfo", ".zdebug_macinfo" },
295 { ".debug_macro", ".zdebug_macro" },
296 { ".debug_str", ".zdebug_str" },
297 { ".debug_str_offsets", ".zdebug_str_offsets" },
298 { ".debug_line_str", ".zdebug_line_str" },
299 { ".debug_ranges", ".zdebug_ranges" },
300 { ".debug_rnglists", ".zdebug_rnglists" },
301 { ".debug_types", ".zdebug_types" },
302 { ".debug_addr", ".zdebug_addr" },
303 { ".debug_frame", ".zdebug_frame" },
304 { ".eh_frame", NULL },
305 { ".gdb_index", ".zgdb_index" },
306 { ".debug_names", ".zdebug_names" },
307 { ".debug_aranges", ".zdebug_aranges" },
308 23
309 };
310
311 /* List of DWO/DWP sections. */
312
313 static const struct dwop_section_names
314 {
315 struct dwarf2_section_names abbrev_dwo;
316 struct dwarf2_section_names info_dwo;
317 struct dwarf2_section_names line_dwo;
318 struct dwarf2_section_names loc_dwo;
319 struct dwarf2_section_names loclists_dwo;
320 struct dwarf2_section_names macinfo_dwo;
321 struct dwarf2_section_names macro_dwo;
322 struct dwarf2_section_names str_dwo;
323 struct dwarf2_section_names str_offsets_dwo;
324 struct dwarf2_section_names types_dwo;
325 struct dwarf2_section_names cu_index;
326 struct dwarf2_section_names tu_index;
327 }
328 dwop_section_names =
329 {
330 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
331 { ".debug_info.dwo", ".zdebug_info.dwo" },
332 { ".debug_line.dwo", ".zdebug_line.dwo" },
333 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
334 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
335 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
336 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
337 { ".debug_str.dwo", ".zdebug_str.dwo" },
338 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
339 { ".debug_types.dwo", ".zdebug_types.dwo" },
340 { ".debug_cu_index", ".zdebug_cu_index" },
341 { ".debug_tu_index", ".zdebug_tu_index" },
342 };
343
344 /* local data types */
345
346 /* Type used for delaying computation of method physnames.
347 See comments for compute_delayed_physnames. */
348 struct delayed_method_info
349 {
350 /* The type to which the method is attached, i.e., its parent class. */
351 struct type *type;
352
353 /* The index of the method in the type's function fieldlists. */
354 int fnfield_index;
355
356 /* The index of the method in the fieldlist. */
357 int index;
358
359 /* The name of the DIE. */
360 const char *name;
361
362 /* The DIE associated with this method. */
363 struct die_info *die;
364 };
365
366 /* Internal state when decoding a particular compilation unit. */
367 struct dwarf2_cu
368 {
369 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
370 ~dwarf2_cu ();
371
372 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
373
374 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
375 Create the set of symtabs used by this TU, or if this TU is sharing
376 symtabs with another TU and the symtabs have already been created
377 then restore those symtabs in the line header.
378 We don't need the pc/line-number mapping for type units. */
379 void setup_type_unit_groups (struct die_info *die);
380
381 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
382 buildsym_compunit constructor. */
383 struct compunit_symtab *start_symtab (const char *name,
384 const char *comp_dir,
385 CORE_ADDR low_pc);
386
387 /* Reset the builder. */
388 void reset_builder () { m_builder.reset (); }
389
390 /* The header of the compilation unit. */
391 struct comp_unit_head header {};
392
393 /* Base address of this compilation unit. */
394 CORE_ADDR base_address = 0;
395
396 /* Non-zero if base_address has been set. */
397 int base_known = 0;
398
399 /* The language we are debugging. */
400 enum language language = language_unknown;
401 const struct language_defn *language_defn = nullptr;
402
403 const char *producer = nullptr;
404
405 private:
406 /* The symtab builder for this CU. This is only non-NULL when full
407 symbols are being read. */
408 std::unique_ptr<buildsym_compunit> m_builder;
409
410 public:
411 /* The generic symbol table building routines have separate lists for
412 file scope symbols and all all other scopes (local scopes). So
413 we need to select the right one to pass to add_symbol_to_list().
414 We do it by keeping a pointer to the correct list in list_in_scope.
415
416 FIXME: The original dwarf code just treated the file scope as the
417 first local scope, and all other local scopes as nested local
418 scopes, and worked fine. Check to see if we really need to
419 distinguish these in buildsym.c. */
420 struct pending **list_in_scope = nullptr;
421
422 /* Hash table holding all the loaded partial DIEs
423 with partial_die->offset.SECT_OFF as hash. */
424 htab_t partial_dies = nullptr;
425
426 /* Storage for things with the same lifetime as this read-in compilation
427 unit, including partial DIEs. */
428 auto_obstack comp_unit_obstack;
429
430 /* When multiple dwarf2_cu structures are living in memory, this field
431 chains them all together, so that they can be released efficiently.
432 We will probably also want a generation counter so that most-recently-used
433 compilation units are cached... */
434 struct dwarf2_per_cu_data *read_in_chain = nullptr;
435
436 /* Backlink to our per_cu entry. */
437 struct dwarf2_per_cu_data *per_cu;
438
439 /* How many compilation units ago was this CU last referenced? */
440 int last_used = 0;
441
442 /* A hash table of DIE cu_offset for following references with
443 die_info->offset.sect_off as hash. */
444 htab_t die_hash = nullptr;
445
446 /* Full DIEs if read in. */
447 struct die_info *dies = nullptr;
448
449 /* A set of pointers to dwarf2_per_cu_data objects for compilation
450 units referenced by this one. Only set during full symbol processing;
451 partial symbol tables do not have dependencies. */
452 htab_t dependencies = nullptr;
453
454 /* Header data from the line table, during full symbol processing. */
455 struct line_header *line_header = nullptr;
456 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
457 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
458 this is the DW_TAG_compile_unit die for this CU. We'll hold on
459 to the line header as long as this DIE is being processed. See
460 process_die_scope. */
461 die_info *line_header_die_owner = nullptr;
462
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
465 std::vector<delayed_method_info> method_list;
466
467 /* To be copied to symtab->call_site_htab. */
468 htab_t call_site_htab = nullptr;
469
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
474 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
478 is non-NULL). */
479 struct dwo_unit *dwo_unit = nullptr;
480
481 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
482 Note this value comes from the Fission stub CU/TU's DIE. */
483 gdb::optional<ULONGEST> addr_base;
484
485 /* The DW_AT_rnglists_base attribute if present.
486 Note this value comes from the Fission stub CU/TU's DIE.
487 Also note that the value is zero in the non-DWO case so this value can
488 be used without needing to know whether DWO files are in use or not.
489 N.B. This does not apply to DW_AT_ranges appearing in
490 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
491 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
492 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
493 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
494 ULONGEST ranges_base = 0;
495
496 /* When reading debug info generated by older versions of rustc, we
497 have to rewrite some union types to be struct types with a
498 variant part. This rewriting must be done after the CU is fully
499 read in, because otherwise at the point of rewriting some struct
500 type might not have been fully processed. So, we keep a list of
501 all such types here and process them after expansion. */
502 std::vector<struct type *> rust_unions;
503
504 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
505 files, the value is implicitly zero. For DWARF 5 version DWO files, the
506 value is often implicit and is the size of the header of
507 .debug_str_offsets section (8 or 4, depending on the address size). */
508 gdb::optional<ULONGEST> str_offsets_base;
509
510 /* Mark used when releasing cached dies. */
511 bool mark : 1;
512
513 /* This CU references .debug_loc. See the symtab->locations_valid field.
514 This test is imperfect as there may exist optimized debug code not using
515 any location list and still facing inlining issues if handled as
516 unoptimized code. For a future better test see GCC PR other/32998. */
517 bool has_loclist : 1;
518
519 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
520 if all the producer_is_* fields are valid. This information is cached
521 because profiling CU expansion showed excessive time spent in
522 producer_is_gxx_lt_4_6. */
523 bool checked_producer : 1;
524 bool producer_is_gxx_lt_4_6 : 1;
525 bool producer_is_gcc_lt_4_3 : 1;
526 bool producer_is_icc : 1;
527 bool producer_is_icc_lt_14 : 1;
528 bool producer_is_codewarrior : 1;
529
530 /* When true, the file that we're processing is known to have
531 debugging info for C++ namespaces. GCC 3.3.x did not produce
532 this information, but later versions do. */
533
534 bool processing_has_namespace_info : 1;
535
536 struct partial_die_info *find_partial_die (sect_offset sect_off);
537
538 /* If this CU was inherited by another CU (via specification,
539 abstract_origin, etc), this is the ancestor CU. */
540 dwarf2_cu *ancestor;
541
542 /* Get the buildsym_compunit for this CU. */
543 buildsym_compunit *get_builder ()
544 {
545 /* If this CU has a builder associated with it, use that. */
546 if (m_builder != nullptr)
547 return m_builder.get ();
548
549 /* Otherwise, search ancestors for a valid builder. */
550 if (ancestor != nullptr)
551 return ancestor->get_builder ();
552
553 return nullptr;
554 }
555 };
556
557 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
558 This includes type_unit_group and quick_file_names. */
559
560 struct stmt_list_hash
561 {
562 /* The DWO unit this table is from or NULL if there is none. */
563 struct dwo_unit *dwo_unit;
564
565 /* Offset in .debug_line or .debug_line.dwo. */
566 sect_offset line_sect_off;
567 };
568
569 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
570 an object of this type. */
571
572 struct type_unit_group
573 {
574 /* dwarf2read.c's main "handle" on a TU symtab.
575 To simplify things we create an artificial CU that "includes" all the
576 type units using this stmt_list so that the rest of the code still has
577 a "per_cu" handle on the symtab. */
578 struct dwarf2_per_cu_data per_cu;
579
580 /* The TUs that share this DW_AT_stmt_list entry.
581 This is added to while parsing type units to build partial symtabs,
582 and is deleted afterwards and not used again. */
583 std::vector<signatured_type *> *tus;
584
585 /* The compunit symtab.
586 Type units in a group needn't all be defined in the same source file,
587 so we create an essentially anonymous symtab as the compunit symtab. */
588 struct compunit_symtab *compunit_symtab;
589
590 /* The data used to construct the hash key. */
591 struct stmt_list_hash hash;
592
593 /* The symbol tables for this TU (obtained from the files listed in
594 DW_AT_stmt_list).
595 WARNING: The order of entries here must match the order of entries
596 in the line header. After the first TU using this type_unit_group, the
597 line header for the subsequent TUs is recreated from this. This is done
598 because we need to use the same symtabs for each TU using the same
599 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
600 there's no guarantee the line header doesn't have duplicate entries. */
601 struct symtab **symtabs;
602 };
603
604 /* These sections are what may appear in a (real or virtual) DWO file. */
605
606 struct dwo_sections
607 {
608 struct dwarf2_section_info abbrev;
609 struct dwarf2_section_info line;
610 struct dwarf2_section_info loc;
611 struct dwarf2_section_info loclists;
612 struct dwarf2_section_info macinfo;
613 struct dwarf2_section_info macro;
614 struct dwarf2_section_info str;
615 struct dwarf2_section_info str_offsets;
616 /* In the case of a virtual DWO file, these two are unused. */
617 struct dwarf2_section_info info;
618 std::vector<dwarf2_section_info> types;
619 };
620
621 /* CUs/TUs in DWP/DWO files. */
622
623 struct dwo_unit
624 {
625 /* Backlink to the containing struct dwo_file. */
626 struct dwo_file *dwo_file;
627
628 /* The "id" that distinguishes this CU/TU.
629 .debug_info calls this "dwo_id", .debug_types calls this "signature".
630 Since signatures came first, we stick with it for consistency. */
631 ULONGEST signature;
632
633 /* The section this CU/TU lives in, in the DWO file. */
634 struct dwarf2_section_info *section;
635
636 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
637 sect_offset sect_off;
638 unsigned int length;
639
640 /* For types, offset in the type's DIE of the type defined by this TU. */
641 cu_offset type_offset_in_tu;
642 };
643
644 /* include/dwarf2.h defines the DWP section codes.
645 It defines a max value but it doesn't define a min value, which we
646 use for error checking, so provide one. */
647
648 enum dwp_v2_section_ids
649 {
650 DW_SECT_MIN = 1
651 };
652
653 /* Data for one DWO file.
654
655 This includes virtual DWO files (a virtual DWO file is a DWO file as it
656 appears in a DWP file). DWP files don't really have DWO files per se -
657 comdat folding of types "loses" the DWO file they came from, and from
658 a high level view DWP files appear to contain a mass of random types.
659 However, to maintain consistency with the non-DWP case we pretend DWP
660 files contain virtual DWO files, and we assign each TU with one virtual
661 DWO file (generally based on the line and abbrev section offsets -
662 a heuristic that seems to work in practice). */
663
664 struct dwo_file
665 {
666 dwo_file () = default;
667 DISABLE_COPY_AND_ASSIGN (dwo_file);
668
669 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
670 For virtual DWO files the name is constructed from the section offsets
671 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
672 from related CU+TUs. */
673 const char *dwo_name = nullptr;
674
675 /* The DW_AT_comp_dir attribute. */
676 const char *comp_dir = nullptr;
677
678 /* The bfd, when the file is open. Otherwise this is NULL.
679 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
680 gdb_bfd_ref_ptr dbfd;
681
682 /* The sections that make up this DWO file.
683 Remember that for virtual DWO files in DWP V2, these are virtual
684 sections (for lack of a better name). */
685 struct dwo_sections sections {};
686
687 /* The CUs in the file.
688 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
689 an extension to handle LLVM's Link Time Optimization output (where
690 multiple source files may be compiled into a single object/dwo pair). */
691 htab_up cus;
692
693 /* Table of TUs in the file.
694 Each element is a struct dwo_unit. */
695 htab_up tus;
696 };
697
698 /* These sections are what may appear in a DWP file. */
699
700 struct dwp_sections
701 {
702 /* These are used by both DWP version 1 and 2. */
703 struct dwarf2_section_info str;
704 struct dwarf2_section_info cu_index;
705 struct dwarf2_section_info tu_index;
706
707 /* These are only used by DWP version 2 files.
708 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
709 sections are referenced by section number, and are not recorded here.
710 In DWP version 2 there is at most one copy of all these sections, each
711 section being (effectively) comprised of the concatenation of all of the
712 individual sections that exist in the version 1 format.
713 To keep the code simple we treat each of these concatenated pieces as a
714 section itself (a virtual section?). */
715 struct dwarf2_section_info abbrev;
716 struct dwarf2_section_info info;
717 struct dwarf2_section_info line;
718 struct dwarf2_section_info loc;
719 struct dwarf2_section_info macinfo;
720 struct dwarf2_section_info macro;
721 struct dwarf2_section_info str_offsets;
722 struct dwarf2_section_info types;
723 };
724
725 /* These sections are what may appear in a virtual DWO file in DWP version 1.
726 A virtual DWO file is a DWO file as it appears in a DWP file. */
727
728 struct virtual_v1_dwo_sections
729 {
730 struct dwarf2_section_info abbrev;
731 struct dwarf2_section_info line;
732 struct dwarf2_section_info loc;
733 struct dwarf2_section_info macinfo;
734 struct dwarf2_section_info macro;
735 struct dwarf2_section_info str_offsets;
736 /* Each DWP hash table entry records one CU or one TU.
737 That is recorded here, and copied to dwo_unit.section. */
738 struct dwarf2_section_info info_or_types;
739 };
740
741 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
742 In version 2, the sections of the DWO files are concatenated together
743 and stored in one section of that name. Thus each ELF section contains
744 several "virtual" sections. */
745
746 struct virtual_v2_dwo_sections
747 {
748 bfd_size_type abbrev_offset;
749 bfd_size_type abbrev_size;
750
751 bfd_size_type line_offset;
752 bfd_size_type line_size;
753
754 bfd_size_type loc_offset;
755 bfd_size_type loc_size;
756
757 bfd_size_type macinfo_offset;
758 bfd_size_type macinfo_size;
759
760 bfd_size_type macro_offset;
761 bfd_size_type macro_size;
762
763 bfd_size_type str_offsets_offset;
764 bfd_size_type str_offsets_size;
765
766 /* Each DWP hash table entry records one CU or one TU.
767 That is recorded here, and copied to dwo_unit.section. */
768 bfd_size_type info_or_types_offset;
769 bfd_size_type info_or_types_size;
770 };
771
772 /* Contents of DWP hash tables. */
773
774 struct dwp_hash_table
775 {
776 uint32_t version, nr_columns;
777 uint32_t nr_units, nr_slots;
778 const gdb_byte *hash_table, *unit_table;
779 union
780 {
781 struct
782 {
783 const gdb_byte *indices;
784 } v1;
785 struct
786 {
787 /* This is indexed by column number and gives the id of the section
788 in that column. */
789 #define MAX_NR_V2_DWO_SECTIONS \
790 (1 /* .debug_info or .debug_types */ \
791 + 1 /* .debug_abbrev */ \
792 + 1 /* .debug_line */ \
793 + 1 /* .debug_loc */ \
794 + 1 /* .debug_str_offsets */ \
795 + 1 /* .debug_macro or .debug_macinfo */)
796 int section_ids[MAX_NR_V2_DWO_SECTIONS];
797 const gdb_byte *offsets;
798 const gdb_byte *sizes;
799 } v2;
800 } section_pool;
801 };
802
803 /* Data for one DWP file. */
804
805 struct dwp_file
806 {
807 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
808 : name (name_),
809 dbfd (std::move (abfd))
810 {
811 }
812
813 /* Name of the file. */
814 const char *name;
815
816 /* File format version. */
817 int version = 0;
818
819 /* The bfd. */
820 gdb_bfd_ref_ptr dbfd;
821
822 /* Section info for this file. */
823 struct dwp_sections sections {};
824
825 /* Table of CUs in the file. */
826 const struct dwp_hash_table *cus = nullptr;
827
828 /* Table of TUs in the file. */
829 const struct dwp_hash_table *tus = nullptr;
830
831 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
832 htab_up loaded_cus;
833 htab_up loaded_tus;
834
835 /* Table to map ELF section numbers to their sections.
836 This is only needed for the DWP V1 file format. */
837 unsigned int num_sections = 0;
838 asection **elf_sections = nullptr;
839 };
840
841 /* Struct used to pass misc. parameters to read_die_and_children, et
842 al. which are used for both .debug_info and .debug_types dies.
843 All parameters here are unchanging for the life of the call. This
844 struct exists to abstract away the constant parameters of die reading. */
845
846 struct die_reader_specs
847 {
848 /* The bfd of die_section. */
849 bfd* abfd;
850
851 /* The CU of the DIE we are parsing. */
852 struct dwarf2_cu *cu;
853
854 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
855 struct dwo_file *dwo_file;
856
857 /* The section the die comes from.
858 This is either .debug_info or .debug_types, or the .dwo variants. */
859 struct dwarf2_section_info *die_section;
860
861 /* die_section->buffer. */
862 const gdb_byte *buffer;
863
864 /* The end of the buffer. */
865 const gdb_byte *buffer_end;
866
867 /* The abbreviation table to use when reading the DIEs. */
868 struct abbrev_table *abbrev_table;
869 };
870
871 /* A subclass of die_reader_specs that holds storage and has complex
872 constructor and destructor behavior. */
873
874 class cutu_reader : public die_reader_specs
875 {
876 public:
877
878 cutu_reader (struct dwarf2_per_cu_data *this_cu,
879 struct abbrev_table *abbrev_table,
880 int use_existing_cu,
881 bool skip_partial);
882
883 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
884 struct dwarf2_cu *parent_cu = nullptr,
885 struct dwo_file *dwo_file = nullptr);
886
887 DISABLE_COPY_AND_ASSIGN (cutu_reader);
888
889 const gdb_byte *info_ptr = nullptr;
890 struct die_info *comp_unit_die = nullptr;
891 bool dummy_p = false;
892
893 /* Release the new CU, putting it on the chain. This cannot be done
894 for dummy CUs. */
895 void keep ();
896
897 private:
898 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
899 int use_existing_cu);
900
901 struct dwarf2_per_cu_data *m_this_cu;
902 std::unique_ptr<dwarf2_cu> m_new_cu;
903
904 /* The ordinary abbreviation table. */
905 abbrev_table_up m_abbrev_table_holder;
906
907 /* The DWO abbreviation table. */
908 abbrev_table_up m_dwo_abbrev_table;
909 };
910
911 /* When we construct a partial symbol table entry we only
912 need this much information. */
913 struct partial_die_info : public allocate_on_obstack
914 {
915 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
916
917 /* Disable assign but still keep copy ctor, which is needed
918 load_partial_dies. */
919 partial_die_info& operator=(const partial_die_info& rhs) = delete;
920
921 /* Adjust the partial die before generating a symbol for it. This
922 function may set the is_external flag or change the DIE's
923 name. */
924 void fixup (struct dwarf2_cu *cu);
925
926 /* Read a minimal amount of information into the minimal die
927 structure. */
928 const gdb_byte *read (const struct die_reader_specs *reader,
929 const struct abbrev_info &abbrev,
930 const gdb_byte *info_ptr);
931
932 /* Offset of this DIE. */
933 const sect_offset sect_off;
934
935 /* DWARF-2 tag for this DIE. */
936 const ENUM_BITFIELD(dwarf_tag) tag : 16;
937
938 /* Assorted flags describing the data found in this DIE. */
939 const unsigned int has_children : 1;
940
941 unsigned int is_external : 1;
942 unsigned int is_declaration : 1;
943 unsigned int has_type : 1;
944 unsigned int has_specification : 1;
945 unsigned int has_pc_info : 1;
946 unsigned int may_be_inlined : 1;
947
948 /* This DIE has been marked DW_AT_main_subprogram. */
949 unsigned int main_subprogram : 1;
950
951 /* Flag set if the SCOPE field of this structure has been
952 computed. */
953 unsigned int scope_set : 1;
954
955 /* Flag set if the DIE has a byte_size attribute. */
956 unsigned int has_byte_size : 1;
957
958 /* Flag set if the DIE has a DW_AT_const_value attribute. */
959 unsigned int has_const_value : 1;
960
961 /* Flag set if any of the DIE's children are template arguments. */
962 unsigned int has_template_arguments : 1;
963
964 /* Flag set if fixup has been called on this die. */
965 unsigned int fixup_called : 1;
966
967 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
968 unsigned int is_dwz : 1;
969
970 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
971 unsigned int spec_is_dwz : 1;
972
973 /* The name of this DIE. Normally the value of DW_AT_name, but
974 sometimes a default name for unnamed DIEs. */
975 const char *name = nullptr;
976
977 /* The linkage name, if present. */
978 const char *linkage_name = nullptr;
979
980 /* The scope to prepend to our children. This is generally
981 allocated on the comp_unit_obstack, so will disappear
982 when this compilation unit leaves the cache. */
983 const char *scope = nullptr;
984
985 /* Some data associated with the partial DIE. The tag determines
986 which field is live. */
987 union
988 {
989 /* The location description associated with this DIE, if any. */
990 struct dwarf_block *locdesc;
991 /* The offset of an import, for DW_TAG_imported_unit. */
992 sect_offset sect_off;
993 } d {};
994
995 /* If HAS_PC_INFO, the PC range associated with this DIE. */
996 CORE_ADDR lowpc = 0;
997 CORE_ADDR highpc = 0;
998
999 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1000 DW_AT_sibling, if any. */
1001 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1002 could return DW_AT_sibling values to its caller load_partial_dies. */
1003 const gdb_byte *sibling = nullptr;
1004
1005 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1006 DW_AT_specification (or DW_AT_abstract_origin or
1007 DW_AT_extension). */
1008 sect_offset spec_offset {};
1009
1010 /* Pointers to this DIE's parent, first child, and next sibling,
1011 if any. */
1012 struct partial_die_info *die_parent = nullptr;
1013 struct partial_die_info *die_child = nullptr;
1014 struct partial_die_info *die_sibling = nullptr;
1015
1016 friend struct partial_die_info *
1017 dwarf2_cu::find_partial_die (sect_offset sect_off);
1018
1019 private:
1020 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1021 partial_die_info (sect_offset sect_off)
1022 : partial_die_info (sect_off, DW_TAG_padding, 0)
1023 {
1024 }
1025
1026 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1027 int has_children_)
1028 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1029 {
1030 is_external = 0;
1031 is_declaration = 0;
1032 has_type = 0;
1033 has_specification = 0;
1034 has_pc_info = 0;
1035 may_be_inlined = 0;
1036 main_subprogram = 0;
1037 scope_set = 0;
1038 has_byte_size = 0;
1039 has_const_value = 0;
1040 has_template_arguments = 0;
1041 fixup_called = 0;
1042 is_dwz = 0;
1043 spec_is_dwz = 0;
1044 }
1045 };
1046
1047 /* This data structure holds a complete die structure. */
1048 struct die_info
1049 {
1050 /* DWARF-2 tag for this DIE. */
1051 ENUM_BITFIELD(dwarf_tag) tag : 16;
1052
1053 /* Number of attributes */
1054 unsigned char num_attrs;
1055
1056 /* True if we're presently building the full type name for the
1057 type derived from this DIE. */
1058 unsigned char building_fullname : 1;
1059
1060 /* True if this die is in process. PR 16581. */
1061 unsigned char in_process : 1;
1062
1063 /* True if this DIE has children. */
1064 unsigned char has_children : 1;
1065
1066 /* Abbrev number */
1067 unsigned int abbrev;
1068
1069 /* Offset in .debug_info or .debug_types section. */
1070 sect_offset sect_off;
1071
1072 /* The dies in a compilation unit form an n-ary tree. PARENT
1073 points to this die's parent; CHILD points to the first child of
1074 this node; and all the children of a given node are chained
1075 together via their SIBLING fields. */
1076 struct die_info *child; /* Its first child, if any. */
1077 struct die_info *sibling; /* Its next sibling, if any. */
1078 struct die_info *parent; /* Its parent, if any. */
1079
1080 /* An array of attributes, with NUM_ATTRS elements. There may be
1081 zero, but it's not common and zero-sized arrays are not
1082 sufficiently portable C. */
1083 struct attribute attrs[1];
1084 };
1085
1086 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1087 but this would require a corresponding change in unpack_field_as_long
1088 and friends. */
1089 static int bits_per_byte = 8;
1090
1091 /* When reading a variant or variant part, we track a bit more
1092 information about the field, and store it in an object of this
1093 type. */
1094
1095 struct variant_field
1096 {
1097 /* If we see a DW_TAG_variant, then this will be the discriminant
1098 value. */
1099 ULONGEST discriminant_value;
1100 /* If we see a DW_TAG_variant, then this will be set if this is the
1101 default branch. */
1102 bool default_branch;
1103 /* While reading a DW_TAG_variant_part, this will be set if this
1104 field is the discriminant. */
1105 bool is_discriminant;
1106 };
1107
1108 struct nextfield
1109 {
1110 int accessibility = 0;
1111 int virtuality = 0;
1112 /* Extra information to describe a variant or variant part. */
1113 struct variant_field variant {};
1114 struct field field {};
1115 };
1116
1117 struct fnfieldlist
1118 {
1119 const char *name = nullptr;
1120 std::vector<struct fn_field> fnfields;
1121 };
1122
1123 /* The routines that read and process dies for a C struct or C++ class
1124 pass lists of data member fields and lists of member function fields
1125 in an instance of a field_info structure, as defined below. */
1126 struct field_info
1127 {
1128 /* List of data member and baseclasses fields. */
1129 std::vector<struct nextfield> fields;
1130 std::vector<struct nextfield> baseclasses;
1131
1132 /* Set if the accessibility of one of the fields is not public. */
1133 int non_public_fields = 0;
1134
1135 /* Member function fieldlist array, contains name of possibly overloaded
1136 member function, number of overloaded member functions and a pointer
1137 to the head of the member function field chain. */
1138 std::vector<struct fnfieldlist> fnfieldlists;
1139
1140 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1141 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1142 std::vector<struct decl_field> typedef_field_list;
1143
1144 /* Nested types defined by this class and the number of elements in this
1145 list. */
1146 std::vector<struct decl_field> nested_types_list;
1147
1148 /* Return the total number of fields (including baseclasses). */
1149 int nfields () const
1150 {
1151 return fields.size () + baseclasses.size ();
1152 }
1153 };
1154
1155 /* Loaded secondary compilation units are kept in memory until they
1156 have not been referenced for the processing of this many
1157 compilation units. Set this to zero to disable caching. Cache
1158 sizes of up to at least twenty will improve startup time for
1159 typical inter-CU-reference binaries, at an obvious memory cost. */
1160 static int dwarf_max_cache_age = 5;
1161 static void
1162 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1163 struct cmd_list_element *c, const char *value)
1164 {
1165 fprintf_filtered (file, _("The upper bound on the age of cached "
1166 "DWARF compilation units is %s.\n"),
1167 value);
1168 }
1169 \f
1170 /* local function prototypes */
1171
1172 static void dwarf2_find_base_address (struct die_info *die,
1173 struct dwarf2_cu *cu);
1174
1175 static dwarf2_psymtab *create_partial_symtab
1176 (struct dwarf2_per_cu_data *per_cu, const char *name);
1177
1178 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1179 const gdb_byte *info_ptr,
1180 struct die_info *type_unit_die);
1181
1182 static void dwarf2_build_psymtabs_hard
1183 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1184
1185 static void scan_partial_symbols (struct partial_die_info *,
1186 CORE_ADDR *, CORE_ADDR *,
1187 int, struct dwarf2_cu *);
1188
1189 static void add_partial_symbol (struct partial_die_info *,
1190 struct dwarf2_cu *);
1191
1192 static void add_partial_namespace (struct partial_die_info *pdi,
1193 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1194 int set_addrmap, struct dwarf2_cu *cu);
1195
1196 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1197 CORE_ADDR *highpc, int set_addrmap,
1198 struct dwarf2_cu *cu);
1199
1200 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1201 struct dwarf2_cu *cu);
1202
1203 static void add_partial_subprogram (struct partial_die_info *pdi,
1204 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1205 int need_pc, struct dwarf2_cu *cu);
1206
1207 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1208
1209 static struct partial_die_info *load_partial_dies
1210 (const struct die_reader_specs *, const gdb_byte *, int);
1211
1212 /* A pair of partial_die_info and compilation unit. */
1213 struct cu_partial_die_info
1214 {
1215 /* The compilation unit of the partial_die_info. */
1216 struct dwarf2_cu *cu;
1217 /* A partial_die_info. */
1218 struct partial_die_info *pdi;
1219
1220 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1221 : cu (cu),
1222 pdi (pdi)
1223 { /* Nothing. */ }
1224
1225 private:
1226 cu_partial_die_info () = delete;
1227 };
1228
1229 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1230 struct dwarf2_cu *);
1231
1232 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1233 struct attribute *, struct attr_abbrev *,
1234 const gdb_byte *, bool *need_reprocess);
1235
1236 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1237 struct attribute *attr);
1238
1239 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1240
1241 static LONGEST read_checked_initial_length_and_offset
1242 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1243 unsigned int *, unsigned int *);
1244
1245 static sect_offset read_abbrev_offset
1246 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1247 struct dwarf2_section_info *, sect_offset);
1248
1249 static const char *read_indirect_string
1250 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1251 const struct comp_unit_head *, unsigned int *);
1252
1253 static const char *read_indirect_line_string
1254 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1255 const struct comp_unit_head *, unsigned int *);
1256
1257 static const char *read_indirect_string_at_offset
1258 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1259 LONGEST str_offset);
1260
1261 static const char *read_indirect_string_from_dwz
1262 (struct objfile *objfile, struct dwz_file *, LONGEST);
1263
1264 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1265 const gdb_byte *,
1266 unsigned int *);
1267
1268 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1269 ULONGEST str_index);
1270
1271 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1272 ULONGEST str_index);
1273
1274 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1275
1276 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1277 struct dwarf2_cu *);
1278
1279 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1280 unsigned int);
1281
1282 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1283 struct dwarf2_cu *cu);
1284
1285 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1286
1287 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1288 struct dwarf2_cu *cu);
1289
1290 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1291
1292 static struct die_info *die_specification (struct die_info *die,
1293 struct dwarf2_cu **);
1294
1295 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1296 struct dwarf2_cu *cu);
1297
1298 static void dwarf_decode_lines (struct line_header *, const char *,
1299 struct dwarf2_cu *, dwarf2_psymtab *,
1300 CORE_ADDR, int decode_mapping);
1301
1302 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1303 const char *);
1304
1305 static struct symbol *new_symbol (struct die_info *, struct type *,
1306 struct dwarf2_cu *, struct symbol * = NULL);
1307
1308 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1309 struct dwarf2_cu *);
1310
1311 static void dwarf2_const_value_attr (const struct attribute *attr,
1312 struct type *type,
1313 const char *name,
1314 struct obstack *obstack,
1315 struct dwarf2_cu *cu, LONGEST *value,
1316 const gdb_byte **bytes,
1317 struct dwarf2_locexpr_baton **baton);
1318
1319 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1320
1321 static int need_gnat_info (struct dwarf2_cu *);
1322
1323 static struct type *die_descriptive_type (struct die_info *,
1324 struct dwarf2_cu *);
1325
1326 static void set_descriptive_type (struct type *, struct die_info *,
1327 struct dwarf2_cu *);
1328
1329 static struct type *die_containing_type (struct die_info *,
1330 struct dwarf2_cu *);
1331
1332 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1333 struct dwarf2_cu *);
1334
1335 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1336
1337 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1338
1339 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1340
1341 static char *typename_concat (struct obstack *obs, const char *prefix,
1342 const char *suffix, int physname,
1343 struct dwarf2_cu *cu);
1344
1345 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1346
1347 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1348
1349 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1350
1351 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1352
1353 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1354
1355 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1356
1357 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1358 struct dwarf2_cu *, dwarf2_psymtab *);
1359
1360 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1361 values. Keep the items ordered with increasing constraints compliance. */
1362 enum pc_bounds_kind
1363 {
1364 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1365 PC_BOUNDS_NOT_PRESENT,
1366
1367 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1368 were present but they do not form a valid range of PC addresses. */
1369 PC_BOUNDS_INVALID,
1370
1371 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1372 PC_BOUNDS_RANGES,
1373
1374 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1375 PC_BOUNDS_HIGH_LOW,
1376 };
1377
1378 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1379 CORE_ADDR *, CORE_ADDR *,
1380 struct dwarf2_cu *,
1381 dwarf2_psymtab *);
1382
1383 static void get_scope_pc_bounds (struct die_info *,
1384 CORE_ADDR *, CORE_ADDR *,
1385 struct dwarf2_cu *);
1386
1387 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1388 CORE_ADDR, struct dwarf2_cu *);
1389
1390 static void dwarf2_add_field (struct field_info *, struct die_info *,
1391 struct dwarf2_cu *);
1392
1393 static void dwarf2_attach_fields_to_type (struct field_info *,
1394 struct type *, struct dwarf2_cu *);
1395
1396 static void dwarf2_add_member_fn (struct field_info *,
1397 struct die_info *, struct type *,
1398 struct dwarf2_cu *);
1399
1400 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1401 struct type *,
1402 struct dwarf2_cu *);
1403
1404 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1405
1406 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1407
1408 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1409
1410 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1411
1412 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1413
1414 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1415
1416 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1417
1418 static struct type *read_module_type (struct die_info *die,
1419 struct dwarf2_cu *cu);
1420
1421 static const char *namespace_name (struct die_info *die,
1422 int *is_anonymous, struct dwarf2_cu *);
1423
1424 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1425
1426 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1427
1428 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1429 struct dwarf2_cu *);
1430
1431 static struct die_info *read_die_and_siblings_1
1432 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1433 struct die_info *);
1434
1435 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1436 const gdb_byte *info_ptr,
1437 const gdb_byte **new_info_ptr,
1438 struct die_info *parent);
1439
1440 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1441 struct die_info **, const gdb_byte *,
1442 int);
1443
1444 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1445 struct die_info **, const gdb_byte *);
1446
1447 static void process_die (struct die_info *, struct dwarf2_cu *);
1448
1449 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1450 struct objfile *);
1451
1452 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1453
1454 static const char *dwarf2_full_name (const char *name,
1455 struct die_info *die,
1456 struct dwarf2_cu *cu);
1457
1458 static const char *dwarf2_physname (const char *name, struct die_info *die,
1459 struct dwarf2_cu *cu);
1460
1461 static struct die_info *dwarf2_extension (struct die_info *die,
1462 struct dwarf2_cu **);
1463
1464 static const char *dwarf_tag_name (unsigned int);
1465
1466 static const char *dwarf_attr_name (unsigned int);
1467
1468 static const char *dwarf_form_name (unsigned int);
1469
1470 static const char *dwarf_bool_name (unsigned int);
1471
1472 static const char *dwarf_type_encoding_name (unsigned int);
1473
1474 static struct die_info *sibling_die (struct die_info *);
1475
1476 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1477
1478 static void dump_die_for_error (struct die_info *);
1479
1480 static void dump_die_1 (struct ui_file *, int level, int max_level,
1481 struct die_info *);
1482
1483 /*static*/ void dump_die (struct die_info *, int max_level);
1484
1485 static void store_in_ref_table (struct die_info *,
1486 struct dwarf2_cu *);
1487
1488 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1489
1490 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1491
1492 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1493 const struct attribute *,
1494 struct dwarf2_cu **);
1495
1496 static struct die_info *follow_die_ref (struct die_info *,
1497 const struct attribute *,
1498 struct dwarf2_cu **);
1499
1500 static struct die_info *follow_die_sig (struct die_info *,
1501 const struct attribute *,
1502 struct dwarf2_cu **);
1503
1504 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1505 struct dwarf2_cu *);
1506
1507 static struct type *get_DW_AT_signature_type (struct die_info *,
1508 const struct attribute *,
1509 struct dwarf2_cu *);
1510
1511 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1512
1513 static void read_signatured_type (struct signatured_type *);
1514
1515 static int attr_to_dynamic_prop (const struct attribute *attr,
1516 struct die_info *die, struct dwarf2_cu *cu,
1517 struct dynamic_prop *prop, struct type *type);
1518
1519 /* memory allocation interface */
1520
1521 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1522
1523 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1524
1525 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1526
1527 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1528 struct dwarf2_loclist_baton *baton,
1529 const struct attribute *attr);
1530
1531 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1532 struct symbol *sym,
1533 struct dwarf2_cu *cu,
1534 int is_block);
1535
1536 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1537 const gdb_byte *info_ptr,
1538 struct abbrev_info *abbrev);
1539
1540 static hashval_t partial_die_hash (const void *item);
1541
1542 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1543
1544 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1545 (sect_offset sect_off, unsigned int offset_in_dwz,
1546 struct dwarf2_per_objfile *dwarf2_per_objfile);
1547
1548 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1549 struct die_info *comp_unit_die,
1550 enum language pretend_language);
1551
1552 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1553
1554 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1555
1556 static struct type *set_die_type (struct die_info *, struct type *,
1557 struct dwarf2_cu *);
1558
1559 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1560
1561 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1562
1563 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1564 enum language);
1565
1566 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1567 enum language);
1568
1569 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1570 enum language);
1571
1572 static void dwarf2_add_dependence (struct dwarf2_cu *,
1573 struct dwarf2_per_cu_data *);
1574
1575 static void dwarf2_mark (struct dwarf2_cu *);
1576
1577 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1578
1579 static struct type *get_die_type_at_offset (sect_offset,
1580 struct dwarf2_per_cu_data *);
1581
1582 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1583
1584 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1585 enum language pretend_language);
1586
1587 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1588
1589 /* Class, the destructor of which frees all allocated queue entries. This
1590 will only have work to do if an error was thrown while processing the
1591 dwarf. If no error was thrown then the queue entries should have all
1592 been processed, and freed, as we went along. */
1593
1594 class dwarf2_queue_guard
1595 {
1596 public:
1597 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1598 : m_per_objfile (per_objfile)
1599 {
1600 }
1601
1602 /* Free any entries remaining on the queue. There should only be
1603 entries left if we hit an error while processing the dwarf. */
1604 ~dwarf2_queue_guard ()
1605 {
1606 /* Ensure that no memory is allocated by the queue. */
1607 std::queue<dwarf2_queue_item> empty;
1608 std::swap (m_per_objfile->queue, empty);
1609 }
1610
1611 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1612
1613 private:
1614 dwarf2_per_objfile *m_per_objfile;
1615 };
1616
1617 dwarf2_queue_item::~dwarf2_queue_item ()
1618 {
1619 /* Anything still marked queued is likely to be in an
1620 inconsistent state, so discard it. */
1621 if (per_cu->queued)
1622 {
1623 if (per_cu->cu != NULL)
1624 free_one_cached_comp_unit (per_cu);
1625 per_cu->queued = 0;
1626 }
1627 }
1628
1629 /* The return type of find_file_and_directory. Note, the enclosed
1630 string pointers are only valid while this object is valid. */
1631
1632 struct file_and_directory
1633 {
1634 /* The filename. This is never NULL. */
1635 const char *name;
1636
1637 /* The compilation directory. NULL if not known. If we needed to
1638 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1639 points directly to the DW_AT_comp_dir string attribute owned by
1640 the obstack that owns the DIE. */
1641 const char *comp_dir;
1642
1643 /* If we needed to build a new string for comp_dir, this is what
1644 owns the storage. */
1645 std::string comp_dir_storage;
1646 };
1647
1648 static file_and_directory find_file_and_directory (struct die_info *die,
1649 struct dwarf2_cu *cu);
1650
1651 static htab_up allocate_signatured_type_table ();
1652
1653 static htab_up allocate_dwo_unit_table ();
1654
1655 static struct dwo_unit *lookup_dwo_unit_in_dwp
1656 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1657 struct dwp_file *dwp_file, const char *comp_dir,
1658 ULONGEST signature, int is_debug_types);
1659
1660 static struct dwp_file *get_dwp_file
1661 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1662
1663 static struct dwo_unit *lookup_dwo_comp_unit
1664 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1665
1666 static struct dwo_unit *lookup_dwo_type_unit
1667 (struct signatured_type *, const char *, const char *);
1668
1669 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1670
1671 /* A unique pointer to a dwo_file. */
1672
1673 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1674
1675 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1676
1677 static void check_producer (struct dwarf2_cu *cu);
1678
1679 static void free_line_header_voidp (void *arg);
1680 \f
1681 /* Various complaints about symbol reading that don't abort the process. */
1682
1683 static void
1684 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1685 {
1686 complaint (_("statement list doesn't fit in .debug_line section"));
1687 }
1688
1689 static void
1690 dwarf2_debug_line_missing_file_complaint (void)
1691 {
1692 complaint (_(".debug_line section has line data without a file"));
1693 }
1694
1695 static void
1696 dwarf2_debug_line_missing_end_sequence_complaint (void)
1697 {
1698 complaint (_(".debug_line section has line "
1699 "program sequence without an end"));
1700 }
1701
1702 static void
1703 dwarf2_complex_location_expr_complaint (void)
1704 {
1705 complaint (_("location expression too complex"));
1706 }
1707
1708 static void
1709 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1710 int arg3)
1711 {
1712 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1713 arg1, arg2, arg3);
1714 }
1715
1716 static void
1717 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1718 {
1719 complaint (_("debug info runs off end of %s section"
1720 " [in module %s]"),
1721 section->get_name (),
1722 section->get_file_name ());
1723 }
1724
1725 static void
1726 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1727 {
1728 complaint (_("macro debug info contains a "
1729 "malformed macro definition:\n`%s'"),
1730 arg1);
1731 }
1732
1733 static void
1734 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1735 {
1736 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1737 arg1, arg2);
1738 }
1739
1740 /* Hash function for line_header_hash. */
1741
1742 static hashval_t
1743 line_header_hash (const struct line_header *ofs)
1744 {
1745 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1746 }
1747
1748 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1749
1750 static hashval_t
1751 line_header_hash_voidp (const void *item)
1752 {
1753 const struct line_header *ofs = (const struct line_header *) item;
1754
1755 return line_header_hash (ofs);
1756 }
1757
1758 /* Equality function for line_header_hash. */
1759
1760 static int
1761 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1762 {
1763 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1764 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1765
1766 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1767 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1768 }
1769
1770 \f
1771
1772 /* See declaration. */
1773
1774 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1775 const dwarf2_debug_sections *names,
1776 bool can_copy_)
1777 : objfile (objfile_),
1778 can_copy (can_copy_)
1779 {
1780 if (names == NULL)
1781 names = &dwarf2_elf_names;
1782
1783 bfd *obfd = objfile->obfd;
1784
1785 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1786 locate_sections (obfd, sec, *names);
1787 }
1788
1789 dwarf2_per_objfile::~dwarf2_per_objfile ()
1790 {
1791 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1792 free_cached_comp_units ();
1793
1794 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1795 per_cu->imported_symtabs_free ();
1796
1797 for (signatured_type *sig_type : all_type_units)
1798 sig_type->per_cu.imported_symtabs_free ();
1799
1800 /* Everything else should be on the objfile obstack. */
1801 }
1802
1803 /* See declaration. */
1804
1805 void
1806 dwarf2_per_objfile::free_cached_comp_units ()
1807 {
1808 dwarf2_per_cu_data *per_cu = read_in_chain;
1809 dwarf2_per_cu_data **last_chain = &read_in_chain;
1810 while (per_cu != NULL)
1811 {
1812 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1813
1814 delete per_cu->cu;
1815 *last_chain = next_cu;
1816 per_cu = next_cu;
1817 }
1818 }
1819
1820 /* A helper class that calls free_cached_comp_units on
1821 destruction. */
1822
1823 class free_cached_comp_units
1824 {
1825 public:
1826
1827 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1828 : m_per_objfile (per_objfile)
1829 {
1830 }
1831
1832 ~free_cached_comp_units ()
1833 {
1834 m_per_objfile->free_cached_comp_units ();
1835 }
1836
1837 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1838
1839 private:
1840
1841 dwarf2_per_objfile *m_per_objfile;
1842 };
1843
1844 /* Try to locate the sections we need for DWARF 2 debugging
1845 information and return true if we have enough to do something.
1846 NAMES points to the dwarf2 section names, or is NULL if the standard
1847 ELF names are used. CAN_COPY is true for formats where symbol
1848 interposition is possible and so symbol values must follow copy
1849 relocation rules. */
1850
1851 int
1852 dwarf2_has_info (struct objfile *objfile,
1853 const struct dwarf2_debug_sections *names,
1854 bool can_copy)
1855 {
1856 if (objfile->flags & OBJF_READNEVER)
1857 return 0;
1858
1859 struct dwarf2_per_objfile *dwarf2_per_objfile
1860 = get_dwarf2_per_objfile (objfile);
1861
1862 if (dwarf2_per_objfile == NULL)
1863 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1864 names,
1865 can_copy);
1866
1867 return (!dwarf2_per_objfile->info.is_virtual
1868 && dwarf2_per_objfile->info.s.section != NULL
1869 && !dwarf2_per_objfile->abbrev.is_virtual
1870 && dwarf2_per_objfile->abbrev.s.section != NULL);
1871 }
1872
1873 /* When loading sections, we look either for uncompressed section or for
1874 compressed section names. */
1875
1876 static int
1877 section_is_p (const char *section_name,
1878 const struct dwarf2_section_names *names)
1879 {
1880 if (names->normal != NULL
1881 && strcmp (section_name, names->normal) == 0)
1882 return 1;
1883 if (names->compressed != NULL
1884 && strcmp (section_name, names->compressed) == 0)
1885 return 1;
1886 return 0;
1887 }
1888
1889 /* See declaration. */
1890
1891 void
1892 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1893 const dwarf2_debug_sections &names)
1894 {
1895 flagword aflag = bfd_section_flags (sectp);
1896
1897 if ((aflag & SEC_HAS_CONTENTS) == 0)
1898 {
1899 }
1900 else if (elf_section_data (sectp)->this_hdr.sh_size
1901 > bfd_get_file_size (abfd))
1902 {
1903 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1904 warning (_("Discarding section %s which has a section size (%s"
1905 ") larger than the file size [in module %s]"),
1906 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1907 bfd_get_filename (abfd));
1908 }
1909 else if (section_is_p (sectp->name, &names.info))
1910 {
1911 this->info.s.section = sectp;
1912 this->info.size = bfd_section_size (sectp);
1913 }
1914 else if (section_is_p (sectp->name, &names.abbrev))
1915 {
1916 this->abbrev.s.section = sectp;
1917 this->abbrev.size = bfd_section_size (sectp);
1918 }
1919 else if (section_is_p (sectp->name, &names.line))
1920 {
1921 this->line.s.section = sectp;
1922 this->line.size = bfd_section_size (sectp);
1923 }
1924 else if (section_is_p (sectp->name, &names.loc))
1925 {
1926 this->loc.s.section = sectp;
1927 this->loc.size = bfd_section_size (sectp);
1928 }
1929 else if (section_is_p (sectp->name, &names.loclists))
1930 {
1931 this->loclists.s.section = sectp;
1932 this->loclists.size = bfd_section_size (sectp);
1933 }
1934 else if (section_is_p (sectp->name, &names.macinfo))
1935 {
1936 this->macinfo.s.section = sectp;
1937 this->macinfo.size = bfd_section_size (sectp);
1938 }
1939 else if (section_is_p (sectp->name, &names.macro))
1940 {
1941 this->macro.s.section = sectp;
1942 this->macro.size = bfd_section_size (sectp);
1943 }
1944 else if (section_is_p (sectp->name, &names.str))
1945 {
1946 this->str.s.section = sectp;
1947 this->str.size = bfd_section_size (sectp);
1948 }
1949 else if (section_is_p (sectp->name, &names.str_offsets))
1950 {
1951 this->str_offsets.s.section = sectp;
1952 this->str_offsets.size = bfd_section_size (sectp);
1953 }
1954 else if (section_is_p (sectp->name, &names.line_str))
1955 {
1956 this->line_str.s.section = sectp;
1957 this->line_str.size = bfd_section_size (sectp);
1958 }
1959 else if (section_is_p (sectp->name, &names.addr))
1960 {
1961 this->addr.s.section = sectp;
1962 this->addr.size = bfd_section_size (sectp);
1963 }
1964 else if (section_is_p (sectp->name, &names.frame))
1965 {
1966 this->frame.s.section = sectp;
1967 this->frame.size = bfd_section_size (sectp);
1968 }
1969 else if (section_is_p (sectp->name, &names.eh_frame))
1970 {
1971 this->eh_frame.s.section = sectp;
1972 this->eh_frame.size = bfd_section_size (sectp);
1973 }
1974 else if (section_is_p (sectp->name, &names.ranges))
1975 {
1976 this->ranges.s.section = sectp;
1977 this->ranges.size = bfd_section_size (sectp);
1978 }
1979 else if (section_is_p (sectp->name, &names.rnglists))
1980 {
1981 this->rnglists.s.section = sectp;
1982 this->rnglists.size = bfd_section_size (sectp);
1983 }
1984 else if (section_is_p (sectp->name, &names.types))
1985 {
1986 struct dwarf2_section_info type_section;
1987
1988 memset (&type_section, 0, sizeof (type_section));
1989 type_section.s.section = sectp;
1990 type_section.size = bfd_section_size (sectp);
1991
1992 this->types.push_back (type_section);
1993 }
1994 else if (section_is_p (sectp->name, &names.gdb_index))
1995 {
1996 this->gdb_index.s.section = sectp;
1997 this->gdb_index.size = bfd_section_size (sectp);
1998 }
1999 else if (section_is_p (sectp->name, &names.debug_names))
2000 {
2001 this->debug_names.s.section = sectp;
2002 this->debug_names.size = bfd_section_size (sectp);
2003 }
2004 else if (section_is_p (sectp->name, &names.debug_aranges))
2005 {
2006 this->debug_aranges.s.section = sectp;
2007 this->debug_aranges.size = bfd_section_size (sectp);
2008 }
2009
2010 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2011 && bfd_section_vma (sectp) == 0)
2012 this->has_section_at_zero = true;
2013 }
2014
2015 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2016 SECTION_NAME. */
2017
2018 void
2019 dwarf2_get_section_info (struct objfile *objfile,
2020 enum dwarf2_section_enum sect,
2021 asection **sectp, const gdb_byte **bufp,
2022 bfd_size_type *sizep)
2023 {
2024 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2025 struct dwarf2_section_info *info;
2026
2027 /* We may see an objfile without any DWARF, in which case we just
2028 return nothing. */
2029 if (data == NULL)
2030 {
2031 *sectp = NULL;
2032 *bufp = NULL;
2033 *sizep = 0;
2034 return;
2035 }
2036 switch (sect)
2037 {
2038 case DWARF2_DEBUG_FRAME:
2039 info = &data->frame;
2040 break;
2041 case DWARF2_EH_FRAME:
2042 info = &data->eh_frame;
2043 break;
2044 default:
2045 gdb_assert_not_reached ("unexpected section");
2046 }
2047
2048 info->read (objfile);
2049
2050 *sectp = info->get_bfd_section ();
2051 *bufp = info->buffer;
2052 *sizep = info->size;
2053 }
2054
2055 /* A helper function to find the sections for a .dwz file. */
2056
2057 static void
2058 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2059 {
2060 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2061
2062 /* Note that we only support the standard ELF names, because .dwz
2063 is ELF-only (at the time of writing). */
2064 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2065 {
2066 dwz_file->abbrev.s.section = sectp;
2067 dwz_file->abbrev.size = bfd_section_size (sectp);
2068 }
2069 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2070 {
2071 dwz_file->info.s.section = sectp;
2072 dwz_file->info.size = bfd_section_size (sectp);
2073 }
2074 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2075 {
2076 dwz_file->str.s.section = sectp;
2077 dwz_file->str.size = bfd_section_size (sectp);
2078 }
2079 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2080 {
2081 dwz_file->line.s.section = sectp;
2082 dwz_file->line.size = bfd_section_size (sectp);
2083 }
2084 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2085 {
2086 dwz_file->macro.s.section = sectp;
2087 dwz_file->macro.size = bfd_section_size (sectp);
2088 }
2089 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2090 {
2091 dwz_file->gdb_index.s.section = sectp;
2092 dwz_file->gdb_index.size = bfd_section_size (sectp);
2093 }
2094 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2095 {
2096 dwz_file->debug_names.s.section = sectp;
2097 dwz_file->debug_names.size = bfd_section_size (sectp);
2098 }
2099 }
2100
2101 /* See dwarf2read.h. */
2102
2103 struct dwz_file *
2104 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2105 {
2106 const char *filename;
2107 bfd_size_type buildid_len_arg;
2108 size_t buildid_len;
2109 bfd_byte *buildid;
2110
2111 if (dwarf2_per_objfile->dwz_file != NULL)
2112 return dwarf2_per_objfile->dwz_file.get ();
2113
2114 bfd_set_error (bfd_error_no_error);
2115 gdb::unique_xmalloc_ptr<char> data
2116 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2117 &buildid_len_arg, &buildid));
2118 if (data == NULL)
2119 {
2120 if (bfd_get_error () == bfd_error_no_error)
2121 return NULL;
2122 error (_("could not read '.gnu_debugaltlink' section: %s"),
2123 bfd_errmsg (bfd_get_error ()));
2124 }
2125
2126 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2127
2128 buildid_len = (size_t) buildid_len_arg;
2129
2130 filename = data.get ();
2131
2132 std::string abs_storage;
2133 if (!IS_ABSOLUTE_PATH (filename))
2134 {
2135 gdb::unique_xmalloc_ptr<char> abs
2136 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2137
2138 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2139 filename = abs_storage.c_str ();
2140 }
2141
2142 /* First try the file name given in the section. If that doesn't
2143 work, try to use the build-id instead. */
2144 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2145 if (dwz_bfd != NULL)
2146 {
2147 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2148 dwz_bfd.reset (nullptr);
2149 }
2150
2151 if (dwz_bfd == NULL)
2152 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2153
2154 if (dwz_bfd == nullptr)
2155 {
2156 gdb::unique_xmalloc_ptr<char> alt_filename;
2157 const char *origname = dwarf2_per_objfile->objfile->original_name;
2158
2159 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2160 buildid_len,
2161 origname,
2162 &alt_filename));
2163
2164 if (fd.get () >= 0)
2165 {
2166 /* File successfully retrieved from server. */
2167 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2168
2169 if (dwz_bfd == nullptr)
2170 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2171 alt_filename.get ());
2172 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2173 dwz_bfd.reset (nullptr);
2174 }
2175 }
2176
2177 if (dwz_bfd == NULL)
2178 error (_("could not find '.gnu_debugaltlink' file for %s"),
2179 objfile_name (dwarf2_per_objfile->objfile));
2180
2181 std::unique_ptr<struct dwz_file> result
2182 (new struct dwz_file (std::move (dwz_bfd)));
2183
2184 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2185 result.get ());
2186
2187 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2188 result->dwz_bfd.get ());
2189 dwarf2_per_objfile->dwz_file = std::move (result);
2190 return dwarf2_per_objfile->dwz_file.get ();
2191 }
2192 \f
2193 /* DWARF quick_symbols_functions support. */
2194
2195 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2196 unique line tables, so we maintain a separate table of all .debug_line
2197 derived entries to support the sharing.
2198 All the quick functions need is the list of file names. We discard the
2199 line_header when we're done and don't need to record it here. */
2200 struct quick_file_names
2201 {
2202 /* The data used to construct the hash key. */
2203 struct stmt_list_hash hash;
2204
2205 /* The number of entries in file_names, real_names. */
2206 unsigned int num_file_names;
2207
2208 /* The file names from the line table, after being run through
2209 file_full_name. */
2210 const char **file_names;
2211
2212 /* The file names from the line table after being run through
2213 gdb_realpath. These are computed lazily. */
2214 const char **real_names;
2215 };
2216
2217 /* When using the index (and thus not using psymtabs), each CU has an
2218 object of this type. This is used to hold information needed by
2219 the various "quick" methods. */
2220 struct dwarf2_per_cu_quick_data
2221 {
2222 /* The file table. This can be NULL if there was no file table
2223 or it's currently not read in.
2224 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2225 struct quick_file_names *file_names;
2226
2227 /* The corresponding symbol table. This is NULL if symbols for this
2228 CU have not yet been read. */
2229 struct compunit_symtab *compunit_symtab;
2230
2231 /* A temporary mark bit used when iterating over all CUs in
2232 expand_symtabs_matching. */
2233 unsigned int mark : 1;
2234
2235 /* True if we've tried to read the file table and found there isn't one.
2236 There will be no point in trying to read it again next time. */
2237 unsigned int no_file_data : 1;
2238 };
2239
2240 /* Utility hash function for a stmt_list_hash. */
2241
2242 static hashval_t
2243 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2244 {
2245 hashval_t v = 0;
2246
2247 if (stmt_list_hash->dwo_unit != NULL)
2248 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2249 v += to_underlying (stmt_list_hash->line_sect_off);
2250 return v;
2251 }
2252
2253 /* Utility equality function for a stmt_list_hash. */
2254
2255 static int
2256 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2257 const struct stmt_list_hash *rhs)
2258 {
2259 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2260 return 0;
2261 if (lhs->dwo_unit != NULL
2262 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2263 return 0;
2264
2265 return lhs->line_sect_off == rhs->line_sect_off;
2266 }
2267
2268 /* Hash function for a quick_file_names. */
2269
2270 static hashval_t
2271 hash_file_name_entry (const void *e)
2272 {
2273 const struct quick_file_names *file_data
2274 = (const struct quick_file_names *) e;
2275
2276 return hash_stmt_list_entry (&file_data->hash);
2277 }
2278
2279 /* Equality function for a quick_file_names. */
2280
2281 static int
2282 eq_file_name_entry (const void *a, const void *b)
2283 {
2284 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2285 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2286
2287 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2288 }
2289
2290 /* Delete function for a quick_file_names. */
2291
2292 static void
2293 delete_file_name_entry (void *e)
2294 {
2295 struct quick_file_names *file_data = (struct quick_file_names *) e;
2296 int i;
2297
2298 for (i = 0; i < file_data->num_file_names; ++i)
2299 {
2300 xfree ((void*) file_data->file_names[i]);
2301 if (file_data->real_names)
2302 xfree ((void*) file_data->real_names[i]);
2303 }
2304
2305 /* The space for the struct itself lives on objfile_obstack,
2306 so we don't free it here. */
2307 }
2308
2309 /* Create a quick_file_names hash table. */
2310
2311 static htab_up
2312 create_quick_file_names_table (unsigned int nr_initial_entries)
2313 {
2314 return htab_up (htab_create_alloc (nr_initial_entries,
2315 hash_file_name_entry, eq_file_name_entry,
2316 delete_file_name_entry, xcalloc, xfree));
2317 }
2318
2319 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2320 have to be created afterwards. You should call age_cached_comp_units after
2321 processing PER_CU->CU. dw2_setup must have been already called. */
2322
2323 static void
2324 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2325 {
2326 if (per_cu->is_debug_types)
2327 load_full_type_unit (per_cu);
2328 else
2329 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2330
2331 if (per_cu->cu == NULL)
2332 return; /* Dummy CU. */
2333
2334 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2335 }
2336
2337 /* Read in the symbols for PER_CU. */
2338
2339 static void
2340 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2341 {
2342 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2343
2344 /* Skip type_unit_groups, reading the type units they contain
2345 is handled elsewhere. */
2346 if (per_cu->type_unit_group_p ())
2347 return;
2348
2349 /* The destructor of dwarf2_queue_guard frees any entries left on
2350 the queue. After this point we're guaranteed to leave this function
2351 with the dwarf queue empty. */
2352 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2353
2354 if (dwarf2_per_objfile->using_index
2355 ? per_cu->v.quick->compunit_symtab == NULL
2356 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2357 {
2358 queue_comp_unit (per_cu, language_minimal);
2359 load_cu (per_cu, skip_partial);
2360
2361 /* If we just loaded a CU from a DWO, and we're working with an index
2362 that may badly handle TUs, load all the TUs in that DWO as well.
2363 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2364 if (!per_cu->is_debug_types
2365 && per_cu->cu != NULL
2366 && per_cu->cu->dwo_unit != NULL
2367 && dwarf2_per_objfile->index_table != NULL
2368 && dwarf2_per_objfile->index_table->version <= 7
2369 /* DWP files aren't supported yet. */
2370 && get_dwp_file (dwarf2_per_objfile) == NULL)
2371 queue_and_load_all_dwo_tus (per_cu);
2372 }
2373
2374 process_queue (dwarf2_per_objfile);
2375
2376 /* Age the cache, releasing compilation units that have not
2377 been used recently. */
2378 age_cached_comp_units (dwarf2_per_objfile);
2379 }
2380
2381 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2382 the objfile from which this CU came. Returns the resulting symbol
2383 table. */
2384
2385 static struct compunit_symtab *
2386 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2387 {
2388 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2389
2390 gdb_assert (dwarf2_per_objfile->using_index);
2391 if (!per_cu->v.quick->compunit_symtab)
2392 {
2393 free_cached_comp_units freer (dwarf2_per_objfile);
2394 scoped_restore decrementer = increment_reading_symtab ();
2395 dw2_do_instantiate_symtab (per_cu, skip_partial);
2396 process_cu_includes (dwarf2_per_objfile);
2397 }
2398
2399 return per_cu->v.quick->compunit_symtab;
2400 }
2401
2402 /* See declaration. */
2403
2404 dwarf2_per_cu_data *
2405 dwarf2_per_objfile::get_cutu (int index)
2406 {
2407 if (index >= this->all_comp_units.size ())
2408 {
2409 index -= this->all_comp_units.size ();
2410 gdb_assert (index < this->all_type_units.size ());
2411 return &this->all_type_units[index]->per_cu;
2412 }
2413
2414 return this->all_comp_units[index];
2415 }
2416
2417 /* See declaration. */
2418
2419 dwarf2_per_cu_data *
2420 dwarf2_per_objfile::get_cu (int index)
2421 {
2422 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2423
2424 return this->all_comp_units[index];
2425 }
2426
2427 /* See declaration. */
2428
2429 signatured_type *
2430 dwarf2_per_objfile::get_tu (int index)
2431 {
2432 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2433
2434 return this->all_type_units[index];
2435 }
2436
2437 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2438 objfile_obstack, and constructed with the specified field
2439 values. */
2440
2441 static dwarf2_per_cu_data *
2442 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2443 struct dwarf2_section_info *section,
2444 int is_dwz,
2445 sect_offset sect_off, ULONGEST length)
2446 {
2447 struct objfile *objfile = dwarf2_per_objfile->objfile;
2448 dwarf2_per_cu_data *the_cu
2449 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2450 struct dwarf2_per_cu_data);
2451 the_cu->sect_off = sect_off;
2452 the_cu->length = length;
2453 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2454 the_cu->section = section;
2455 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2456 struct dwarf2_per_cu_quick_data);
2457 the_cu->is_dwz = is_dwz;
2458 return the_cu;
2459 }
2460
2461 /* A helper for create_cus_from_index that handles a given list of
2462 CUs. */
2463
2464 static void
2465 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2466 const gdb_byte *cu_list, offset_type n_elements,
2467 struct dwarf2_section_info *section,
2468 int is_dwz)
2469 {
2470 for (offset_type i = 0; i < n_elements; i += 2)
2471 {
2472 gdb_static_assert (sizeof (ULONGEST) >= 8);
2473
2474 sect_offset sect_off
2475 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2476 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2477 cu_list += 2 * 8;
2478
2479 dwarf2_per_cu_data *per_cu
2480 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2481 sect_off, length);
2482 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2483 }
2484 }
2485
2486 /* Read the CU list from the mapped index, and use it to create all
2487 the CU objects for this objfile. */
2488
2489 static void
2490 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2491 const gdb_byte *cu_list, offset_type cu_list_elements,
2492 const gdb_byte *dwz_list, offset_type dwz_elements)
2493 {
2494 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2495 dwarf2_per_objfile->all_comp_units.reserve
2496 ((cu_list_elements + dwz_elements) / 2);
2497
2498 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2499 &dwarf2_per_objfile->info, 0);
2500
2501 if (dwz_elements == 0)
2502 return;
2503
2504 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2505 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2506 &dwz->info, 1);
2507 }
2508
2509 /* Create the signatured type hash table from the index. */
2510
2511 static void
2512 create_signatured_type_table_from_index
2513 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2514 struct dwarf2_section_info *section,
2515 const gdb_byte *bytes,
2516 offset_type elements)
2517 {
2518 struct objfile *objfile = dwarf2_per_objfile->objfile;
2519
2520 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2521 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2522
2523 htab_up sig_types_hash = allocate_signatured_type_table ();
2524
2525 for (offset_type i = 0; i < elements; i += 3)
2526 {
2527 struct signatured_type *sig_type;
2528 ULONGEST signature;
2529 void **slot;
2530 cu_offset type_offset_in_tu;
2531
2532 gdb_static_assert (sizeof (ULONGEST) >= 8);
2533 sect_offset sect_off
2534 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2535 type_offset_in_tu
2536 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2537 BFD_ENDIAN_LITTLE);
2538 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2539 bytes += 3 * 8;
2540
2541 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2542 struct signatured_type);
2543 sig_type->signature = signature;
2544 sig_type->type_offset_in_tu = type_offset_in_tu;
2545 sig_type->per_cu.is_debug_types = 1;
2546 sig_type->per_cu.section = section;
2547 sig_type->per_cu.sect_off = sect_off;
2548 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2549 sig_type->per_cu.v.quick
2550 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2551 struct dwarf2_per_cu_quick_data);
2552
2553 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2554 *slot = sig_type;
2555
2556 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2557 }
2558
2559 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2560 }
2561
2562 /* Create the signatured type hash table from .debug_names. */
2563
2564 static void
2565 create_signatured_type_table_from_debug_names
2566 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2567 const mapped_debug_names &map,
2568 struct dwarf2_section_info *section,
2569 struct dwarf2_section_info *abbrev_section)
2570 {
2571 struct objfile *objfile = dwarf2_per_objfile->objfile;
2572
2573 section->read (objfile);
2574 abbrev_section->read (objfile);
2575
2576 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2577 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2578
2579 htab_up sig_types_hash = allocate_signatured_type_table ();
2580
2581 for (uint32_t i = 0; i < map.tu_count; ++i)
2582 {
2583 struct signatured_type *sig_type;
2584 void **slot;
2585
2586 sect_offset sect_off
2587 = (sect_offset) (extract_unsigned_integer
2588 (map.tu_table_reordered + i * map.offset_size,
2589 map.offset_size,
2590 map.dwarf5_byte_order));
2591
2592 comp_unit_head cu_header;
2593 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2594 abbrev_section,
2595 section->buffer + to_underlying (sect_off),
2596 rcuh_kind::TYPE);
2597
2598 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2599 struct signatured_type);
2600 sig_type->signature = cu_header.signature;
2601 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2602 sig_type->per_cu.is_debug_types = 1;
2603 sig_type->per_cu.section = section;
2604 sig_type->per_cu.sect_off = sect_off;
2605 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2606 sig_type->per_cu.v.quick
2607 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2608 struct dwarf2_per_cu_quick_data);
2609
2610 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2611 *slot = sig_type;
2612
2613 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2614 }
2615
2616 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2617 }
2618
2619 /* Read the address map data from the mapped index, and use it to
2620 populate the objfile's psymtabs_addrmap. */
2621
2622 static void
2623 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2624 struct mapped_index *index)
2625 {
2626 struct objfile *objfile = dwarf2_per_objfile->objfile;
2627 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2628 const gdb_byte *iter, *end;
2629 struct addrmap *mutable_map;
2630 CORE_ADDR baseaddr;
2631
2632 auto_obstack temp_obstack;
2633
2634 mutable_map = addrmap_create_mutable (&temp_obstack);
2635
2636 iter = index->address_table.data ();
2637 end = iter + index->address_table.size ();
2638
2639 baseaddr = objfile->text_section_offset ();
2640
2641 while (iter < end)
2642 {
2643 ULONGEST hi, lo, cu_index;
2644 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2645 iter += 8;
2646 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2647 iter += 8;
2648 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2649 iter += 4;
2650
2651 if (lo > hi)
2652 {
2653 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2654 hex_string (lo), hex_string (hi));
2655 continue;
2656 }
2657
2658 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2659 {
2660 complaint (_(".gdb_index address table has invalid CU number %u"),
2661 (unsigned) cu_index);
2662 continue;
2663 }
2664
2665 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2666 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2667 addrmap_set_empty (mutable_map, lo, hi - 1,
2668 dwarf2_per_objfile->get_cu (cu_index));
2669 }
2670
2671 objfile->partial_symtabs->psymtabs_addrmap
2672 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2673 }
2674
2675 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2676 populate the objfile's psymtabs_addrmap. */
2677
2678 static void
2679 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2680 struct dwarf2_section_info *section)
2681 {
2682 struct objfile *objfile = dwarf2_per_objfile->objfile;
2683 bfd *abfd = objfile->obfd;
2684 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2685 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2686
2687 auto_obstack temp_obstack;
2688 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2689
2690 std::unordered_map<sect_offset,
2691 dwarf2_per_cu_data *,
2692 gdb::hash_enum<sect_offset>>
2693 debug_info_offset_to_per_cu;
2694 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2695 {
2696 const auto insertpair
2697 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2698 if (!insertpair.second)
2699 {
2700 warning (_("Section .debug_aranges in %s has duplicate "
2701 "debug_info_offset %s, ignoring .debug_aranges."),
2702 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2703 return;
2704 }
2705 }
2706
2707 section->read (objfile);
2708
2709 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2710
2711 const gdb_byte *addr = section->buffer;
2712
2713 while (addr < section->buffer + section->size)
2714 {
2715 const gdb_byte *const entry_addr = addr;
2716 unsigned int bytes_read;
2717
2718 const LONGEST entry_length = read_initial_length (abfd, addr,
2719 &bytes_read);
2720 addr += bytes_read;
2721
2722 const gdb_byte *const entry_end = addr + entry_length;
2723 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2724 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2725 if (addr + entry_length > section->buffer + section->size)
2726 {
2727 warning (_("Section .debug_aranges in %s entry at offset %s "
2728 "length %s exceeds section length %s, "
2729 "ignoring .debug_aranges."),
2730 objfile_name (objfile),
2731 plongest (entry_addr - section->buffer),
2732 plongest (bytes_read + entry_length),
2733 pulongest (section->size));
2734 return;
2735 }
2736
2737 /* The version number. */
2738 const uint16_t version = read_2_bytes (abfd, addr);
2739 addr += 2;
2740 if (version != 2)
2741 {
2742 warning (_("Section .debug_aranges in %s entry at offset %s "
2743 "has unsupported version %d, ignoring .debug_aranges."),
2744 objfile_name (objfile),
2745 plongest (entry_addr - section->buffer), version);
2746 return;
2747 }
2748
2749 const uint64_t debug_info_offset
2750 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2751 addr += offset_size;
2752 const auto per_cu_it
2753 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2754 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2755 {
2756 warning (_("Section .debug_aranges in %s entry at offset %s "
2757 "debug_info_offset %s does not exists, "
2758 "ignoring .debug_aranges."),
2759 objfile_name (objfile),
2760 plongest (entry_addr - section->buffer),
2761 pulongest (debug_info_offset));
2762 return;
2763 }
2764 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2765
2766 const uint8_t address_size = *addr++;
2767 if (address_size < 1 || address_size > 8)
2768 {
2769 warning (_("Section .debug_aranges in %s entry at offset %s "
2770 "address_size %u is invalid, ignoring .debug_aranges."),
2771 objfile_name (objfile),
2772 plongest (entry_addr - section->buffer), address_size);
2773 return;
2774 }
2775
2776 const uint8_t segment_selector_size = *addr++;
2777 if (segment_selector_size != 0)
2778 {
2779 warning (_("Section .debug_aranges in %s entry at offset %s "
2780 "segment_selector_size %u is not supported, "
2781 "ignoring .debug_aranges."),
2782 objfile_name (objfile),
2783 plongest (entry_addr - section->buffer),
2784 segment_selector_size);
2785 return;
2786 }
2787
2788 /* Must pad to an alignment boundary that is twice the address
2789 size. It is undocumented by the DWARF standard but GCC does
2790 use it. */
2791 for (size_t padding = ((-(addr - section->buffer))
2792 & (2 * address_size - 1));
2793 padding > 0; padding--)
2794 if (*addr++ != 0)
2795 {
2796 warning (_("Section .debug_aranges in %s entry at offset %s "
2797 "padding is not zero, ignoring .debug_aranges."),
2798 objfile_name (objfile),
2799 plongest (entry_addr - section->buffer));
2800 return;
2801 }
2802
2803 for (;;)
2804 {
2805 if (addr + 2 * address_size > entry_end)
2806 {
2807 warning (_("Section .debug_aranges in %s entry at offset %s "
2808 "address list is not properly terminated, "
2809 "ignoring .debug_aranges."),
2810 objfile_name (objfile),
2811 plongest (entry_addr - section->buffer));
2812 return;
2813 }
2814 ULONGEST start = extract_unsigned_integer (addr, address_size,
2815 dwarf5_byte_order);
2816 addr += address_size;
2817 ULONGEST length = extract_unsigned_integer (addr, address_size,
2818 dwarf5_byte_order);
2819 addr += address_size;
2820 if (start == 0 && length == 0)
2821 break;
2822 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2823 {
2824 /* Symbol was eliminated due to a COMDAT group. */
2825 continue;
2826 }
2827 ULONGEST end = start + length;
2828 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2829 - baseaddr);
2830 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2831 - baseaddr);
2832 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2833 }
2834 }
2835
2836 objfile->partial_symtabs->psymtabs_addrmap
2837 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2838 }
2839
2840 /* Find a slot in the mapped index INDEX for the object named NAME.
2841 If NAME is found, set *VEC_OUT to point to the CU vector in the
2842 constant pool and return true. If NAME cannot be found, return
2843 false. */
2844
2845 static bool
2846 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2847 offset_type **vec_out)
2848 {
2849 offset_type hash;
2850 offset_type slot, step;
2851 int (*cmp) (const char *, const char *);
2852
2853 gdb::unique_xmalloc_ptr<char> without_params;
2854 if (current_language->la_language == language_cplus
2855 || current_language->la_language == language_fortran
2856 || current_language->la_language == language_d)
2857 {
2858 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2859 not contain any. */
2860
2861 if (strchr (name, '(') != NULL)
2862 {
2863 without_params = cp_remove_params (name);
2864
2865 if (without_params != NULL)
2866 name = without_params.get ();
2867 }
2868 }
2869
2870 /* Index version 4 did not support case insensitive searches. But the
2871 indices for case insensitive languages are built in lowercase, therefore
2872 simulate our NAME being searched is also lowercased. */
2873 hash = mapped_index_string_hash ((index->version == 4
2874 && case_sensitivity == case_sensitive_off
2875 ? 5 : index->version),
2876 name);
2877
2878 slot = hash & (index->symbol_table.size () - 1);
2879 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2880 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2881
2882 for (;;)
2883 {
2884 const char *str;
2885
2886 const auto &bucket = index->symbol_table[slot];
2887 if (bucket.name == 0 && bucket.vec == 0)
2888 return false;
2889
2890 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2891 if (!cmp (name, str))
2892 {
2893 *vec_out = (offset_type *) (index->constant_pool
2894 + MAYBE_SWAP (bucket.vec));
2895 return true;
2896 }
2897
2898 slot = (slot + step) & (index->symbol_table.size () - 1);
2899 }
2900 }
2901
2902 /* A helper function that reads the .gdb_index from BUFFER and fills
2903 in MAP. FILENAME is the name of the file containing the data;
2904 it is used for error reporting. DEPRECATED_OK is true if it is
2905 ok to use deprecated sections.
2906
2907 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2908 out parameters that are filled in with information about the CU and
2909 TU lists in the section.
2910
2911 Returns true if all went well, false otherwise. */
2912
2913 static bool
2914 read_gdb_index_from_buffer (struct objfile *objfile,
2915 const char *filename,
2916 bool deprecated_ok,
2917 gdb::array_view<const gdb_byte> buffer,
2918 struct mapped_index *map,
2919 const gdb_byte **cu_list,
2920 offset_type *cu_list_elements,
2921 const gdb_byte **types_list,
2922 offset_type *types_list_elements)
2923 {
2924 const gdb_byte *addr = &buffer[0];
2925
2926 /* Version check. */
2927 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2928 /* Versions earlier than 3 emitted every copy of a psymbol. This
2929 causes the index to behave very poorly for certain requests. Version 3
2930 contained incomplete addrmap. So, it seems better to just ignore such
2931 indices. */
2932 if (version < 4)
2933 {
2934 static int warning_printed = 0;
2935 if (!warning_printed)
2936 {
2937 warning (_("Skipping obsolete .gdb_index section in %s."),
2938 filename);
2939 warning_printed = 1;
2940 }
2941 return 0;
2942 }
2943 /* Index version 4 uses a different hash function than index version
2944 5 and later.
2945
2946 Versions earlier than 6 did not emit psymbols for inlined
2947 functions. Using these files will cause GDB not to be able to
2948 set breakpoints on inlined functions by name, so we ignore these
2949 indices unless the user has done
2950 "set use-deprecated-index-sections on". */
2951 if (version < 6 && !deprecated_ok)
2952 {
2953 static int warning_printed = 0;
2954 if (!warning_printed)
2955 {
2956 warning (_("\
2957 Skipping deprecated .gdb_index section in %s.\n\
2958 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2959 to use the section anyway."),
2960 filename);
2961 warning_printed = 1;
2962 }
2963 return 0;
2964 }
2965 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2966 of the TU (for symbols coming from TUs),
2967 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2968 Plus gold-generated indices can have duplicate entries for global symbols,
2969 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2970 These are just performance bugs, and we can't distinguish gdb-generated
2971 indices from gold-generated ones, so issue no warning here. */
2972
2973 /* Indexes with higher version than the one supported by GDB may be no
2974 longer backward compatible. */
2975 if (version > 8)
2976 return 0;
2977
2978 map->version = version;
2979
2980 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2981
2982 int i = 0;
2983 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2984 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2985 / 8);
2986 ++i;
2987
2988 *types_list = addr + MAYBE_SWAP (metadata[i]);
2989 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2990 - MAYBE_SWAP (metadata[i]))
2991 / 8);
2992 ++i;
2993
2994 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2995 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2996 map->address_table
2997 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2998 ++i;
2999
3000 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3001 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3002 map->symbol_table
3003 = gdb::array_view<mapped_index::symbol_table_slot>
3004 ((mapped_index::symbol_table_slot *) symbol_table,
3005 (mapped_index::symbol_table_slot *) symbol_table_end);
3006
3007 ++i;
3008 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3009
3010 return 1;
3011 }
3012
3013 /* Callback types for dwarf2_read_gdb_index. */
3014
3015 typedef gdb::function_view
3016 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3017 get_gdb_index_contents_ftype;
3018 typedef gdb::function_view
3019 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3020 get_gdb_index_contents_dwz_ftype;
3021
3022 /* Read .gdb_index. If everything went ok, initialize the "quick"
3023 elements of all the CUs and return 1. Otherwise, return 0. */
3024
3025 static int
3026 dwarf2_read_gdb_index
3027 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3028 get_gdb_index_contents_ftype get_gdb_index_contents,
3029 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3030 {
3031 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3032 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3033 struct dwz_file *dwz;
3034 struct objfile *objfile = dwarf2_per_objfile->objfile;
3035
3036 gdb::array_view<const gdb_byte> main_index_contents
3037 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3038
3039 if (main_index_contents.empty ())
3040 return 0;
3041
3042 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3043 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3044 use_deprecated_index_sections,
3045 main_index_contents, map.get (), &cu_list,
3046 &cu_list_elements, &types_list,
3047 &types_list_elements))
3048 return 0;
3049
3050 /* Don't use the index if it's empty. */
3051 if (map->symbol_table.empty ())
3052 return 0;
3053
3054 /* If there is a .dwz file, read it so we can get its CU list as
3055 well. */
3056 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3057 if (dwz != NULL)
3058 {
3059 struct mapped_index dwz_map;
3060 const gdb_byte *dwz_types_ignore;
3061 offset_type dwz_types_elements_ignore;
3062
3063 gdb::array_view<const gdb_byte> dwz_index_content
3064 = get_gdb_index_contents_dwz (objfile, dwz);
3065
3066 if (dwz_index_content.empty ())
3067 return 0;
3068
3069 if (!read_gdb_index_from_buffer (objfile,
3070 bfd_get_filename (dwz->dwz_bfd.get ()),
3071 1, dwz_index_content, &dwz_map,
3072 &dwz_list, &dwz_list_elements,
3073 &dwz_types_ignore,
3074 &dwz_types_elements_ignore))
3075 {
3076 warning (_("could not read '.gdb_index' section from %s; skipping"),
3077 bfd_get_filename (dwz->dwz_bfd.get ()));
3078 return 0;
3079 }
3080 }
3081
3082 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3083 dwz_list, dwz_list_elements);
3084
3085 if (types_list_elements)
3086 {
3087 /* We can only handle a single .debug_types when we have an
3088 index. */
3089 if (dwarf2_per_objfile->types.size () != 1)
3090 return 0;
3091
3092 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3093
3094 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3095 types_list, types_list_elements);
3096 }
3097
3098 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3099
3100 dwarf2_per_objfile->index_table = std::move (map);
3101 dwarf2_per_objfile->using_index = 1;
3102 dwarf2_per_objfile->quick_file_names_table =
3103 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3104
3105 return 1;
3106 }
3107
3108 /* die_reader_func for dw2_get_file_names. */
3109
3110 static void
3111 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3112 const gdb_byte *info_ptr,
3113 struct die_info *comp_unit_die)
3114 {
3115 struct dwarf2_cu *cu = reader->cu;
3116 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3117 struct dwarf2_per_objfile *dwarf2_per_objfile
3118 = cu->per_cu->dwarf2_per_objfile;
3119 struct objfile *objfile = dwarf2_per_objfile->objfile;
3120 struct dwarf2_per_cu_data *lh_cu;
3121 struct attribute *attr;
3122 void **slot;
3123 struct quick_file_names *qfn;
3124
3125 gdb_assert (! this_cu->is_debug_types);
3126
3127 /* Our callers never want to match partial units -- instead they
3128 will match the enclosing full CU. */
3129 if (comp_unit_die->tag == DW_TAG_partial_unit)
3130 {
3131 this_cu->v.quick->no_file_data = 1;
3132 return;
3133 }
3134
3135 lh_cu = this_cu;
3136 slot = NULL;
3137
3138 line_header_up lh;
3139 sect_offset line_offset {};
3140
3141 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3142 if (attr != nullptr)
3143 {
3144 struct quick_file_names find_entry;
3145
3146 line_offset = (sect_offset) DW_UNSND (attr);
3147
3148 /* We may have already read in this line header (TU line header sharing).
3149 If we have we're done. */
3150 find_entry.hash.dwo_unit = cu->dwo_unit;
3151 find_entry.hash.line_sect_off = line_offset;
3152 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3153 &find_entry, INSERT);
3154 if (*slot != NULL)
3155 {
3156 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3157 return;
3158 }
3159
3160 lh = dwarf_decode_line_header (line_offset, cu);
3161 }
3162 if (lh == NULL)
3163 {
3164 lh_cu->v.quick->no_file_data = 1;
3165 return;
3166 }
3167
3168 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3169 qfn->hash.dwo_unit = cu->dwo_unit;
3170 qfn->hash.line_sect_off = line_offset;
3171 gdb_assert (slot != NULL);
3172 *slot = qfn;
3173
3174 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3175
3176 int offset = 0;
3177 if (strcmp (fnd.name, "<unknown>") != 0)
3178 ++offset;
3179
3180 qfn->num_file_names = offset + lh->file_names_size ();
3181 qfn->file_names =
3182 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3183 if (offset != 0)
3184 qfn->file_names[0] = xstrdup (fnd.name);
3185 for (int i = 0; i < lh->file_names_size (); ++i)
3186 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3187 fnd.comp_dir).release ();
3188 qfn->real_names = NULL;
3189
3190 lh_cu->v.quick->file_names = qfn;
3191 }
3192
3193 /* A helper for the "quick" functions which attempts to read the line
3194 table for THIS_CU. */
3195
3196 static struct quick_file_names *
3197 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3198 {
3199 /* This should never be called for TUs. */
3200 gdb_assert (! this_cu->is_debug_types);
3201 /* Nor type unit groups. */
3202 gdb_assert (! this_cu->type_unit_group_p ());
3203
3204 if (this_cu->v.quick->file_names != NULL)
3205 return this_cu->v.quick->file_names;
3206 /* If we know there is no line data, no point in looking again. */
3207 if (this_cu->v.quick->no_file_data)
3208 return NULL;
3209
3210 cutu_reader reader (this_cu);
3211 if (!reader.dummy_p)
3212 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3213
3214 if (this_cu->v.quick->no_file_data)
3215 return NULL;
3216 return this_cu->v.quick->file_names;
3217 }
3218
3219 /* A helper for the "quick" functions which computes and caches the
3220 real path for a given file name from the line table. */
3221
3222 static const char *
3223 dw2_get_real_path (struct objfile *objfile,
3224 struct quick_file_names *qfn, int index)
3225 {
3226 if (qfn->real_names == NULL)
3227 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3228 qfn->num_file_names, const char *);
3229
3230 if (qfn->real_names[index] == NULL)
3231 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3232
3233 return qfn->real_names[index];
3234 }
3235
3236 static struct symtab *
3237 dw2_find_last_source_symtab (struct objfile *objfile)
3238 {
3239 struct dwarf2_per_objfile *dwarf2_per_objfile
3240 = get_dwarf2_per_objfile (objfile);
3241 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3242 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3243
3244 if (cust == NULL)
3245 return NULL;
3246
3247 return compunit_primary_filetab (cust);
3248 }
3249
3250 /* Traversal function for dw2_forget_cached_source_info. */
3251
3252 static int
3253 dw2_free_cached_file_names (void **slot, void *info)
3254 {
3255 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3256
3257 if (file_data->real_names)
3258 {
3259 int i;
3260
3261 for (i = 0; i < file_data->num_file_names; ++i)
3262 {
3263 xfree ((void*) file_data->real_names[i]);
3264 file_data->real_names[i] = NULL;
3265 }
3266 }
3267
3268 return 1;
3269 }
3270
3271 static void
3272 dw2_forget_cached_source_info (struct objfile *objfile)
3273 {
3274 struct dwarf2_per_objfile *dwarf2_per_objfile
3275 = get_dwarf2_per_objfile (objfile);
3276
3277 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3278 dw2_free_cached_file_names, NULL);
3279 }
3280
3281 /* Helper function for dw2_map_symtabs_matching_filename that expands
3282 the symtabs and calls the iterator. */
3283
3284 static int
3285 dw2_map_expand_apply (struct objfile *objfile,
3286 struct dwarf2_per_cu_data *per_cu,
3287 const char *name, const char *real_path,
3288 gdb::function_view<bool (symtab *)> callback)
3289 {
3290 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3291
3292 /* Don't visit already-expanded CUs. */
3293 if (per_cu->v.quick->compunit_symtab)
3294 return 0;
3295
3296 /* This may expand more than one symtab, and we want to iterate over
3297 all of them. */
3298 dw2_instantiate_symtab (per_cu, false);
3299
3300 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3301 last_made, callback);
3302 }
3303
3304 /* Implementation of the map_symtabs_matching_filename method. */
3305
3306 static bool
3307 dw2_map_symtabs_matching_filename
3308 (struct objfile *objfile, const char *name, const char *real_path,
3309 gdb::function_view<bool (symtab *)> callback)
3310 {
3311 const char *name_basename = lbasename (name);
3312 struct dwarf2_per_objfile *dwarf2_per_objfile
3313 = get_dwarf2_per_objfile (objfile);
3314
3315 /* The rule is CUs specify all the files, including those used by
3316 any TU, so there's no need to scan TUs here. */
3317
3318 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3319 {
3320 /* We only need to look at symtabs not already expanded. */
3321 if (per_cu->v.quick->compunit_symtab)
3322 continue;
3323
3324 quick_file_names *file_data = dw2_get_file_names (per_cu);
3325 if (file_data == NULL)
3326 continue;
3327
3328 for (int j = 0; j < file_data->num_file_names; ++j)
3329 {
3330 const char *this_name = file_data->file_names[j];
3331 const char *this_real_name;
3332
3333 if (compare_filenames_for_search (this_name, name))
3334 {
3335 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3336 callback))
3337 return true;
3338 continue;
3339 }
3340
3341 /* Before we invoke realpath, which can get expensive when many
3342 files are involved, do a quick comparison of the basenames. */
3343 if (! basenames_may_differ
3344 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3345 continue;
3346
3347 this_real_name = dw2_get_real_path (objfile, file_data, j);
3348 if (compare_filenames_for_search (this_real_name, name))
3349 {
3350 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3351 callback))
3352 return true;
3353 continue;
3354 }
3355
3356 if (real_path != NULL)
3357 {
3358 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3359 gdb_assert (IS_ABSOLUTE_PATH (name));
3360 if (this_real_name != NULL
3361 && FILENAME_CMP (real_path, this_real_name) == 0)
3362 {
3363 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3364 callback))
3365 return true;
3366 continue;
3367 }
3368 }
3369 }
3370 }
3371
3372 return false;
3373 }
3374
3375 /* Struct used to manage iterating over all CUs looking for a symbol. */
3376
3377 struct dw2_symtab_iterator
3378 {
3379 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3380 struct dwarf2_per_objfile *dwarf2_per_objfile;
3381 /* If set, only look for symbols that match that block. Valid values are
3382 GLOBAL_BLOCK and STATIC_BLOCK. */
3383 gdb::optional<block_enum> block_index;
3384 /* The kind of symbol we're looking for. */
3385 domain_enum domain;
3386 /* The list of CUs from the index entry of the symbol,
3387 or NULL if not found. */
3388 offset_type *vec;
3389 /* The next element in VEC to look at. */
3390 int next;
3391 /* The number of elements in VEC, or zero if there is no match. */
3392 int length;
3393 /* Have we seen a global version of the symbol?
3394 If so we can ignore all further global instances.
3395 This is to work around gold/15646, inefficient gold-generated
3396 indices. */
3397 int global_seen;
3398 };
3399
3400 /* Initialize the index symtab iterator ITER. */
3401
3402 static void
3403 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3404 struct dwarf2_per_objfile *dwarf2_per_objfile,
3405 gdb::optional<block_enum> block_index,
3406 domain_enum domain,
3407 const char *name)
3408 {
3409 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3410 iter->block_index = block_index;
3411 iter->domain = domain;
3412 iter->next = 0;
3413 iter->global_seen = 0;
3414
3415 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3416
3417 /* index is NULL if OBJF_READNOW. */
3418 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3419 iter->length = MAYBE_SWAP (*iter->vec);
3420 else
3421 {
3422 iter->vec = NULL;
3423 iter->length = 0;
3424 }
3425 }
3426
3427 /* Return the next matching CU or NULL if there are no more. */
3428
3429 static struct dwarf2_per_cu_data *
3430 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3431 {
3432 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3433
3434 for ( ; iter->next < iter->length; ++iter->next)
3435 {
3436 offset_type cu_index_and_attrs =
3437 MAYBE_SWAP (iter->vec[iter->next + 1]);
3438 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3439 gdb_index_symbol_kind symbol_kind =
3440 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3441 /* Only check the symbol attributes if they're present.
3442 Indices prior to version 7 don't record them,
3443 and indices >= 7 may elide them for certain symbols
3444 (gold does this). */
3445 int attrs_valid =
3446 (dwarf2_per_objfile->index_table->version >= 7
3447 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3448
3449 /* Don't crash on bad data. */
3450 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3451 + dwarf2_per_objfile->all_type_units.size ()))
3452 {
3453 complaint (_(".gdb_index entry has bad CU index"
3454 " [in module %s]"),
3455 objfile_name (dwarf2_per_objfile->objfile));
3456 continue;
3457 }
3458
3459 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3460
3461 /* Skip if already read in. */
3462 if (per_cu->v.quick->compunit_symtab)
3463 continue;
3464
3465 /* Check static vs global. */
3466 if (attrs_valid)
3467 {
3468 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3469
3470 if (iter->block_index.has_value ())
3471 {
3472 bool want_static = *iter->block_index == STATIC_BLOCK;
3473
3474 if (is_static != want_static)
3475 continue;
3476 }
3477
3478 /* Work around gold/15646. */
3479 if (!is_static && iter->global_seen)
3480 continue;
3481 if (!is_static)
3482 iter->global_seen = 1;
3483 }
3484
3485 /* Only check the symbol's kind if it has one. */
3486 if (attrs_valid)
3487 {
3488 switch (iter->domain)
3489 {
3490 case VAR_DOMAIN:
3491 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3492 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3493 /* Some types are also in VAR_DOMAIN. */
3494 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3495 continue;
3496 break;
3497 case STRUCT_DOMAIN:
3498 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3499 continue;
3500 break;
3501 case LABEL_DOMAIN:
3502 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3503 continue;
3504 break;
3505 case MODULE_DOMAIN:
3506 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3507 continue;
3508 break;
3509 default:
3510 break;
3511 }
3512 }
3513
3514 ++iter->next;
3515 return per_cu;
3516 }
3517
3518 return NULL;
3519 }
3520
3521 static struct compunit_symtab *
3522 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3523 const char *name, domain_enum domain)
3524 {
3525 struct compunit_symtab *stab_best = NULL;
3526 struct dwarf2_per_objfile *dwarf2_per_objfile
3527 = get_dwarf2_per_objfile (objfile);
3528
3529 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3530
3531 struct dw2_symtab_iterator iter;
3532 struct dwarf2_per_cu_data *per_cu;
3533
3534 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3535
3536 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3537 {
3538 struct symbol *sym, *with_opaque = NULL;
3539 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3540 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3541 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3542
3543 sym = block_find_symbol (block, name, domain,
3544 block_find_non_opaque_type_preferred,
3545 &with_opaque);
3546
3547 /* Some caution must be observed with overloaded functions
3548 and methods, since the index will not contain any overload
3549 information (but NAME might contain it). */
3550
3551 if (sym != NULL
3552 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3553 return stab;
3554 if (with_opaque != NULL
3555 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3556 stab_best = stab;
3557
3558 /* Keep looking through other CUs. */
3559 }
3560
3561 return stab_best;
3562 }
3563
3564 static void
3565 dw2_print_stats (struct objfile *objfile)
3566 {
3567 struct dwarf2_per_objfile *dwarf2_per_objfile
3568 = get_dwarf2_per_objfile (objfile);
3569 int total = (dwarf2_per_objfile->all_comp_units.size ()
3570 + dwarf2_per_objfile->all_type_units.size ());
3571 int count = 0;
3572
3573 for (int i = 0; i < total; ++i)
3574 {
3575 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3576
3577 if (!per_cu->v.quick->compunit_symtab)
3578 ++count;
3579 }
3580 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3581 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3582 }
3583
3584 /* This dumps minimal information about the index.
3585 It is called via "mt print objfiles".
3586 One use is to verify .gdb_index has been loaded by the
3587 gdb.dwarf2/gdb-index.exp testcase. */
3588
3589 static void
3590 dw2_dump (struct objfile *objfile)
3591 {
3592 struct dwarf2_per_objfile *dwarf2_per_objfile
3593 = get_dwarf2_per_objfile (objfile);
3594
3595 gdb_assert (dwarf2_per_objfile->using_index);
3596 printf_filtered (".gdb_index:");
3597 if (dwarf2_per_objfile->index_table != NULL)
3598 {
3599 printf_filtered (" version %d\n",
3600 dwarf2_per_objfile->index_table->version);
3601 }
3602 else
3603 printf_filtered (" faked for \"readnow\"\n");
3604 printf_filtered ("\n");
3605 }
3606
3607 static void
3608 dw2_expand_symtabs_for_function (struct objfile *objfile,
3609 const char *func_name)
3610 {
3611 struct dwarf2_per_objfile *dwarf2_per_objfile
3612 = get_dwarf2_per_objfile (objfile);
3613
3614 struct dw2_symtab_iterator iter;
3615 struct dwarf2_per_cu_data *per_cu;
3616
3617 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3618
3619 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3620 dw2_instantiate_symtab (per_cu, false);
3621
3622 }
3623
3624 static void
3625 dw2_expand_all_symtabs (struct objfile *objfile)
3626 {
3627 struct dwarf2_per_objfile *dwarf2_per_objfile
3628 = get_dwarf2_per_objfile (objfile);
3629 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3630 + dwarf2_per_objfile->all_type_units.size ());
3631
3632 for (int i = 0; i < total_units; ++i)
3633 {
3634 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3635
3636 /* We don't want to directly expand a partial CU, because if we
3637 read it with the wrong language, then assertion failures can
3638 be triggered later on. See PR symtab/23010. So, tell
3639 dw2_instantiate_symtab to skip partial CUs -- any important
3640 partial CU will be read via DW_TAG_imported_unit anyway. */
3641 dw2_instantiate_symtab (per_cu, true);
3642 }
3643 }
3644
3645 static void
3646 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3647 const char *fullname)
3648 {
3649 struct dwarf2_per_objfile *dwarf2_per_objfile
3650 = get_dwarf2_per_objfile (objfile);
3651
3652 /* We don't need to consider type units here.
3653 This is only called for examining code, e.g. expand_line_sal.
3654 There can be an order of magnitude (or more) more type units
3655 than comp units, and we avoid them if we can. */
3656
3657 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3658 {
3659 /* We only need to look at symtabs not already expanded. */
3660 if (per_cu->v.quick->compunit_symtab)
3661 continue;
3662
3663 quick_file_names *file_data = dw2_get_file_names (per_cu);
3664 if (file_data == NULL)
3665 continue;
3666
3667 for (int j = 0; j < file_data->num_file_names; ++j)
3668 {
3669 const char *this_fullname = file_data->file_names[j];
3670
3671 if (filename_cmp (this_fullname, fullname) == 0)
3672 {
3673 dw2_instantiate_symtab (per_cu, false);
3674 break;
3675 }
3676 }
3677 }
3678 }
3679
3680 static void
3681 dw2_map_matching_symbols
3682 (struct objfile *objfile,
3683 const lookup_name_info &name, domain_enum domain,
3684 int global,
3685 gdb::function_view<symbol_found_callback_ftype> callback,
3686 symbol_compare_ftype *ordered_compare)
3687 {
3688 /* Currently unimplemented; used for Ada. The function can be called if the
3689 current language is Ada for a non-Ada objfile using GNU index. As Ada
3690 does not look for non-Ada symbols this function should just return. */
3691 }
3692
3693 /* Starting from a search name, return the string that finds the upper
3694 bound of all strings that start with SEARCH_NAME in a sorted name
3695 list. Returns the empty string to indicate that the upper bound is
3696 the end of the list. */
3697
3698 static std::string
3699 make_sort_after_prefix_name (const char *search_name)
3700 {
3701 /* When looking to complete "func", we find the upper bound of all
3702 symbols that start with "func" by looking for where we'd insert
3703 the closest string that would follow "func" in lexicographical
3704 order. Usually, that's "func"-with-last-character-incremented,
3705 i.e. "fund". Mind non-ASCII characters, though. Usually those
3706 will be UTF-8 multi-byte sequences, but we can't be certain.
3707 Especially mind the 0xff character, which is a valid character in
3708 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3709 rule out compilers allowing it in identifiers. Note that
3710 conveniently, strcmp/strcasecmp are specified to compare
3711 characters interpreted as unsigned char. So what we do is treat
3712 the whole string as a base 256 number composed of a sequence of
3713 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3714 to 0, and carries 1 to the following more-significant position.
3715 If the very first character in SEARCH_NAME ends up incremented
3716 and carries/overflows, then the upper bound is the end of the
3717 list. The string after the empty string is also the empty
3718 string.
3719
3720 Some examples of this operation:
3721
3722 SEARCH_NAME => "+1" RESULT
3723
3724 "abc" => "abd"
3725 "ab\xff" => "ac"
3726 "\xff" "a" "\xff" => "\xff" "b"
3727 "\xff" => ""
3728 "\xff\xff" => ""
3729 "" => ""
3730
3731 Then, with these symbols for example:
3732
3733 func
3734 func1
3735 fund
3736
3737 completing "func" looks for symbols between "func" and
3738 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3739 which finds "func" and "func1", but not "fund".
3740
3741 And with:
3742
3743 funcÿ (Latin1 'ÿ' [0xff])
3744 funcÿ1
3745 fund
3746
3747 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3748 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3749
3750 And with:
3751
3752 ÿÿ (Latin1 'ÿ' [0xff])
3753 ÿÿ1
3754
3755 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3756 the end of the list.
3757 */
3758 std::string after = search_name;
3759 while (!after.empty () && (unsigned char) after.back () == 0xff)
3760 after.pop_back ();
3761 if (!after.empty ())
3762 after.back () = (unsigned char) after.back () + 1;
3763 return after;
3764 }
3765
3766 /* See declaration. */
3767
3768 std::pair<std::vector<name_component>::const_iterator,
3769 std::vector<name_component>::const_iterator>
3770 mapped_index_base::find_name_components_bounds
3771 (const lookup_name_info &lookup_name_without_params, language lang) const
3772 {
3773 auto *name_cmp
3774 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3775
3776 const char *lang_name
3777 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3778
3779 /* Comparison function object for lower_bound that matches against a
3780 given symbol name. */
3781 auto lookup_compare_lower = [&] (const name_component &elem,
3782 const char *name)
3783 {
3784 const char *elem_qualified = this->symbol_name_at (elem.idx);
3785 const char *elem_name = elem_qualified + elem.name_offset;
3786 return name_cmp (elem_name, name) < 0;
3787 };
3788
3789 /* Comparison function object for upper_bound that matches against a
3790 given symbol name. */
3791 auto lookup_compare_upper = [&] (const char *name,
3792 const name_component &elem)
3793 {
3794 const char *elem_qualified = this->symbol_name_at (elem.idx);
3795 const char *elem_name = elem_qualified + elem.name_offset;
3796 return name_cmp (name, elem_name) < 0;
3797 };
3798
3799 auto begin = this->name_components.begin ();
3800 auto end = this->name_components.end ();
3801
3802 /* Find the lower bound. */
3803 auto lower = [&] ()
3804 {
3805 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3806 return begin;
3807 else
3808 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3809 } ();
3810
3811 /* Find the upper bound. */
3812 auto upper = [&] ()
3813 {
3814 if (lookup_name_without_params.completion_mode ())
3815 {
3816 /* In completion mode, we want UPPER to point past all
3817 symbols names that have the same prefix. I.e., with
3818 these symbols, and completing "func":
3819
3820 function << lower bound
3821 function1
3822 other_function << upper bound
3823
3824 We find the upper bound by looking for the insertion
3825 point of "func"-with-last-character-incremented,
3826 i.e. "fund". */
3827 std::string after = make_sort_after_prefix_name (lang_name);
3828 if (after.empty ())
3829 return end;
3830 return std::lower_bound (lower, end, after.c_str (),
3831 lookup_compare_lower);
3832 }
3833 else
3834 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3835 } ();
3836
3837 return {lower, upper};
3838 }
3839
3840 /* See declaration. */
3841
3842 void
3843 mapped_index_base::build_name_components ()
3844 {
3845 if (!this->name_components.empty ())
3846 return;
3847
3848 this->name_components_casing = case_sensitivity;
3849 auto *name_cmp
3850 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3851
3852 /* The code below only knows how to break apart components of C++
3853 symbol names (and other languages that use '::' as
3854 namespace/module separator) and Ada symbol names. */
3855 auto count = this->symbol_name_count ();
3856 for (offset_type idx = 0; idx < count; idx++)
3857 {
3858 if (this->symbol_name_slot_invalid (idx))
3859 continue;
3860
3861 const char *name = this->symbol_name_at (idx);
3862
3863 /* Add each name component to the name component table. */
3864 unsigned int previous_len = 0;
3865
3866 if (strstr (name, "::") != nullptr)
3867 {
3868 for (unsigned int current_len = cp_find_first_component (name);
3869 name[current_len] != '\0';
3870 current_len += cp_find_first_component (name + current_len))
3871 {
3872 gdb_assert (name[current_len] == ':');
3873 this->name_components.push_back ({previous_len, idx});
3874 /* Skip the '::'. */
3875 current_len += 2;
3876 previous_len = current_len;
3877 }
3878 }
3879 else
3880 {
3881 /* Handle the Ada encoded (aka mangled) form here. */
3882 for (const char *iter = strstr (name, "__");
3883 iter != nullptr;
3884 iter = strstr (iter, "__"))
3885 {
3886 this->name_components.push_back ({previous_len, idx});
3887 iter += 2;
3888 previous_len = iter - name;
3889 }
3890 }
3891
3892 this->name_components.push_back ({previous_len, idx});
3893 }
3894
3895 /* Sort name_components elements by name. */
3896 auto name_comp_compare = [&] (const name_component &left,
3897 const name_component &right)
3898 {
3899 const char *left_qualified = this->symbol_name_at (left.idx);
3900 const char *right_qualified = this->symbol_name_at (right.idx);
3901
3902 const char *left_name = left_qualified + left.name_offset;
3903 const char *right_name = right_qualified + right.name_offset;
3904
3905 return name_cmp (left_name, right_name) < 0;
3906 };
3907
3908 std::sort (this->name_components.begin (),
3909 this->name_components.end (),
3910 name_comp_compare);
3911 }
3912
3913 /* Helper for dw2_expand_symtabs_matching that works with a
3914 mapped_index_base instead of the containing objfile. This is split
3915 to a separate function in order to be able to unit test the
3916 name_components matching using a mock mapped_index_base. For each
3917 symbol name that matches, calls MATCH_CALLBACK, passing it the
3918 symbol's index in the mapped_index_base symbol table. */
3919
3920 static void
3921 dw2_expand_symtabs_matching_symbol
3922 (mapped_index_base &index,
3923 const lookup_name_info &lookup_name_in,
3924 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3925 enum search_domain kind,
3926 gdb::function_view<bool (offset_type)> match_callback)
3927 {
3928 lookup_name_info lookup_name_without_params
3929 = lookup_name_in.make_ignore_params ();
3930
3931 /* Build the symbol name component sorted vector, if we haven't
3932 yet. */
3933 index.build_name_components ();
3934
3935 /* The same symbol may appear more than once in the range though.
3936 E.g., if we're looking for symbols that complete "w", and we have
3937 a symbol named "w1::w2", we'll find the two name components for
3938 that same symbol in the range. To be sure we only call the
3939 callback once per symbol, we first collect the symbol name
3940 indexes that matched in a temporary vector and ignore
3941 duplicates. */
3942 std::vector<offset_type> matches;
3943
3944 struct name_and_matcher
3945 {
3946 symbol_name_matcher_ftype *matcher;
3947 const std::string &name;
3948
3949 bool operator== (const name_and_matcher &other) const
3950 {
3951 return matcher == other.matcher && name == other.name;
3952 }
3953 };
3954
3955 /* A vector holding all the different symbol name matchers, for all
3956 languages. */
3957 std::vector<name_and_matcher> matchers;
3958
3959 for (int i = 0; i < nr_languages; i++)
3960 {
3961 enum language lang_e = (enum language) i;
3962
3963 const language_defn *lang = language_def (lang_e);
3964 symbol_name_matcher_ftype *name_matcher
3965 = get_symbol_name_matcher (lang, lookup_name_without_params);
3966
3967 name_and_matcher key {
3968 name_matcher,
3969 lookup_name_without_params.language_lookup_name (lang_e)
3970 };
3971
3972 /* Don't insert the same comparison routine more than once.
3973 Note that we do this linear walk. This is not a problem in
3974 practice because the number of supported languages is
3975 low. */
3976 if (std::find (matchers.begin (), matchers.end (), key)
3977 != matchers.end ())
3978 continue;
3979 matchers.push_back (std::move (key));
3980
3981 auto bounds
3982 = index.find_name_components_bounds (lookup_name_without_params,
3983 lang_e);
3984
3985 /* Now for each symbol name in range, check to see if we have a name
3986 match, and if so, call the MATCH_CALLBACK callback. */
3987
3988 for (; bounds.first != bounds.second; ++bounds.first)
3989 {
3990 const char *qualified = index.symbol_name_at (bounds.first->idx);
3991
3992 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3993 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3994 continue;
3995
3996 matches.push_back (bounds.first->idx);
3997 }
3998 }
3999
4000 std::sort (matches.begin (), matches.end ());
4001
4002 /* Finally call the callback, once per match. */
4003 ULONGEST prev = -1;
4004 for (offset_type idx : matches)
4005 {
4006 if (prev != idx)
4007 {
4008 if (!match_callback (idx))
4009 break;
4010 prev = idx;
4011 }
4012 }
4013
4014 /* Above we use a type wider than idx's for 'prev', since 0 and
4015 (offset_type)-1 are both possible values. */
4016 static_assert (sizeof (prev) > sizeof (offset_type), "");
4017 }
4018
4019 #if GDB_SELF_TEST
4020
4021 namespace selftests { namespace dw2_expand_symtabs_matching {
4022
4023 /* A mock .gdb_index/.debug_names-like name index table, enough to
4024 exercise dw2_expand_symtabs_matching_symbol, which works with the
4025 mapped_index_base interface. Builds an index from the symbol list
4026 passed as parameter to the constructor. */
4027 class mock_mapped_index : public mapped_index_base
4028 {
4029 public:
4030 mock_mapped_index (gdb::array_view<const char *> symbols)
4031 : m_symbol_table (symbols)
4032 {}
4033
4034 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4035
4036 /* Return the number of names in the symbol table. */
4037 size_t symbol_name_count () const override
4038 {
4039 return m_symbol_table.size ();
4040 }
4041
4042 /* Get the name of the symbol at IDX in the symbol table. */
4043 const char *symbol_name_at (offset_type idx) const override
4044 {
4045 return m_symbol_table[idx];
4046 }
4047
4048 private:
4049 gdb::array_view<const char *> m_symbol_table;
4050 };
4051
4052 /* Convenience function that converts a NULL pointer to a "<null>"
4053 string, to pass to print routines. */
4054
4055 static const char *
4056 string_or_null (const char *str)
4057 {
4058 return str != NULL ? str : "<null>";
4059 }
4060
4061 /* Check if a lookup_name_info built from
4062 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4063 index. EXPECTED_LIST is the list of expected matches, in expected
4064 matching order. If no match expected, then an empty list is
4065 specified. Returns true on success. On failure prints a warning
4066 indicating the file:line that failed, and returns false. */
4067
4068 static bool
4069 check_match (const char *file, int line,
4070 mock_mapped_index &mock_index,
4071 const char *name, symbol_name_match_type match_type,
4072 bool completion_mode,
4073 std::initializer_list<const char *> expected_list)
4074 {
4075 lookup_name_info lookup_name (name, match_type, completion_mode);
4076
4077 bool matched = true;
4078
4079 auto mismatch = [&] (const char *expected_str,
4080 const char *got)
4081 {
4082 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4083 "expected=\"%s\", got=\"%s\"\n"),
4084 file, line,
4085 (match_type == symbol_name_match_type::FULL
4086 ? "FULL" : "WILD"),
4087 name, string_or_null (expected_str), string_or_null (got));
4088 matched = false;
4089 };
4090
4091 auto expected_it = expected_list.begin ();
4092 auto expected_end = expected_list.end ();
4093
4094 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4095 NULL, ALL_DOMAIN,
4096 [&] (offset_type idx)
4097 {
4098 const char *matched_name = mock_index.symbol_name_at (idx);
4099 const char *expected_str
4100 = expected_it == expected_end ? NULL : *expected_it++;
4101
4102 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4103 mismatch (expected_str, matched_name);
4104 return true;
4105 });
4106
4107 const char *expected_str
4108 = expected_it == expected_end ? NULL : *expected_it++;
4109 if (expected_str != NULL)
4110 mismatch (expected_str, NULL);
4111
4112 return matched;
4113 }
4114
4115 /* The symbols added to the mock mapped_index for testing (in
4116 canonical form). */
4117 static const char *test_symbols[] = {
4118 "function",
4119 "std::bar",
4120 "std::zfunction",
4121 "std::zfunction2",
4122 "w1::w2",
4123 "ns::foo<char*>",
4124 "ns::foo<int>",
4125 "ns::foo<long>",
4126 "ns2::tmpl<int>::foo2",
4127 "(anonymous namespace)::A::B::C",
4128
4129 /* These are used to check that the increment-last-char in the
4130 matching algorithm for completion doesn't match "t1_fund" when
4131 completing "t1_func". */
4132 "t1_func",
4133 "t1_func1",
4134 "t1_fund",
4135 "t1_fund1",
4136
4137 /* A UTF-8 name with multi-byte sequences to make sure that
4138 cp-name-parser understands this as a single identifier ("função"
4139 is "function" in PT). */
4140 u8"u8função",
4141
4142 /* \377 (0xff) is Latin1 'ÿ'. */
4143 "yfunc\377",
4144
4145 /* \377 (0xff) is Latin1 'ÿ'. */
4146 "\377",
4147 "\377\377123",
4148
4149 /* A name with all sorts of complications. Starts with "z" to make
4150 it easier for the completion tests below. */
4151 #define Z_SYM_NAME \
4152 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4153 "::tuple<(anonymous namespace)::ui*, " \
4154 "std::default_delete<(anonymous namespace)::ui>, void>"
4155
4156 Z_SYM_NAME
4157 };
4158
4159 /* Returns true if the mapped_index_base::find_name_component_bounds
4160 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4161 in completion mode. */
4162
4163 static bool
4164 check_find_bounds_finds (mapped_index_base &index,
4165 const char *search_name,
4166 gdb::array_view<const char *> expected_syms)
4167 {
4168 lookup_name_info lookup_name (search_name,
4169 symbol_name_match_type::FULL, true);
4170
4171 auto bounds = index.find_name_components_bounds (lookup_name,
4172 language_cplus);
4173
4174 size_t distance = std::distance (bounds.first, bounds.second);
4175 if (distance != expected_syms.size ())
4176 return false;
4177
4178 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4179 {
4180 auto nc_elem = bounds.first + exp_elem;
4181 const char *qualified = index.symbol_name_at (nc_elem->idx);
4182 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4183 return false;
4184 }
4185
4186 return true;
4187 }
4188
4189 /* Test the lower-level mapped_index::find_name_component_bounds
4190 method. */
4191
4192 static void
4193 test_mapped_index_find_name_component_bounds ()
4194 {
4195 mock_mapped_index mock_index (test_symbols);
4196
4197 mock_index.build_name_components ();
4198
4199 /* Test the lower-level mapped_index::find_name_component_bounds
4200 method in completion mode. */
4201 {
4202 static const char *expected_syms[] = {
4203 "t1_func",
4204 "t1_func1",
4205 };
4206
4207 SELF_CHECK (check_find_bounds_finds (mock_index,
4208 "t1_func", expected_syms));
4209 }
4210
4211 /* Check that the increment-last-char in the name matching algorithm
4212 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4213 {
4214 static const char *expected_syms1[] = {
4215 "\377",
4216 "\377\377123",
4217 };
4218 SELF_CHECK (check_find_bounds_finds (mock_index,
4219 "\377", expected_syms1));
4220
4221 static const char *expected_syms2[] = {
4222 "\377\377123",
4223 };
4224 SELF_CHECK (check_find_bounds_finds (mock_index,
4225 "\377\377", expected_syms2));
4226 }
4227 }
4228
4229 /* Test dw2_expand_symtabs_matching_symbol. */
4230
4231 static void
4232 test_dw2_expand_symtabs_matching_symbol ()
4233 {
4234 mock_mapped_index mock_index (test_symbols);
4235
4236 /* We let all tests run until the end even if some fails, for debug
4237 convenience. */
4238 bool any_mismatch = false;
4239
4240 /* Create the expected symbols list (an initializer_list). Needed
4241 because lists have commas, and we need to pass them to CHECK,
4242 which is a macro. */
4243 #define EXPECT(...) { __VA_ARGS__ }
4244
4245 /* Wrapper for check_match that passes down the current
4246 __FILE__/__LINE__. */
4247 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4248 any_mismatch |= !check_match (__FILE__, __LINE__, \
4249 mock_index, \
4250 NAME, MATCH_TYPE, COMPLETION_MODE, \
4251 EXPECTED_LIST)
4252
4253 /* Identity checks. */
4254 for (const char *sym : test_symbols)
4255 {
4256 /* Should be able to match all existing symbols. */
4257 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4258 EXPECT (sym));
4259
4260 /* Should be able to match all existing symbols with
4261 parameters. */
4262 std::string with_params = std::string (sym) + "(int)";
4263 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4264 EXPECT (sym));
4265
4266 /* Should be able to match all existing symbols with
4267 parameters and qualifiers. */
4268 with_params = std::string (sym) + " ( int ) const";
4269 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4270 EXPECT (sym));
4271
4272 /* This should really find sym, but cp-name-parser.y doesn't
4273 know about lvalue/rvalue qualifiers yet. */
4274 with_params = std::string (sym) + " ( int ) &&";
4275 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4276 {});
4277 }
4278
4279 /* Check that the name matching algorithm for completion doesn't get
4280 confused with Latin1 'ÿ' / 0xff. */
4281 {
4282 static const char str[] = "\377";
4283 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4284 EXPECT ("\377", "\377\377123"));
4285 }
4286
4287 /* Check that the increment-last-char in the matching algorithm for
4288 completion doesn't match "t1_fund" when completing "t1_func". */
4289 {
4290 static const char str[] = "t1_func";
4291 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4292 EXPECT ("t1_func", "t1_func1"));
4293 }
4294
4295 /* Check that completion mode works at each prefix of the expected
4296 symbol name. */
4297 {
4298 static const char str[] = "function(int)";
4299 size_t len = strlen (str);
4300 std::string lookup;
4301
4302 for (size_t i = 1; i < len; i++)
4303 {
4304 lookup.assign (str, i);
4305 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4306 EXPECT ("function"));
4307 }
4308 }
4309
4310 /* While "w" is a prefix of both components, the match function
4311 should still only be called once. */
4312 {
4313 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4314 EXPECT ("w1::w2"));
4315 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4316 EXPECT ("w1::w2"));
4317 }
4318
4319 /* Same, with a "complicated" symbol. */
4320 {
4321 static const char str[] = Z_SYM_NAME;
4322 size_t len = strlen (str);
4323 std::string lookup;
4324
4325 for (size_t i = 1; i < len; i++)
4326 {
4327 lookup.assign (str, i);
4328 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4329 EXPECT (Z_SYM_NAME));
4330 }
4331 }
4332
4333 /* In FULL mode, an incomplete symbol doesn't match. */
4334 {
4335 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4336 {});
4337 }
4338
4339 /* A complete symbol with parameters matches any overload, since the
4340 index has no overload info. */
4341 {
4342 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4343 EXPECT ("std::zfunction", "std::zfunction2"));
4344 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4345 EXPECT ("std::zfunction", "std::zfunction2"));
4346 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4347 EXPECT ("std::zfunction", "std::zfunction2"));
4348 }
4349
4350 /* Check that whitespace is ignored appropriately. A symbol with a
4351 template argument list. */
4352 {
4353 static const char expected[] = "ns::foo<int>";
4354 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4355 EXPECT (expected));
4356 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4357 EXPECT (expected));
4358 }
4359
4360 /* Check that whitespace is ignored appropriately. A symbol with a
4361 template argument list that includes a pointer. */
4362 {
4363 static const char expected[] = "ns::foo<char*>";
4364 /* Try both completion and non-completion modes. */
4365 static const bool completion_mode[2] = {false, true};
4366 for (size_t i = 0; i < 2; i++)
4367 {
4368 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4369 completion_mode[i], EXPECT (expected));
4370 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4371 completion_mode[i], EXPECT (expected));
4372
4373 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4374 completion_mode[i], EXPECT (expected));
4375 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4376 completion_mode[i], EXPECT (expected));
4377 }
4378 }
4379
4380 {
4381 /* Check method qualifiers are ignored. */
4382 static const char expected[] = "ns::foo<char*>";
4383 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4384 symbol_name_match_type::FULL, true, EXPECT (expected));
4385 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4386 symbol_name_match_type::FULL, true, EXPECT (expected));
4387 CHECK_MATCH ("foo < char * > ( int ) const",
4388 symbol_name_match_type::WILD, true, EXPECT (expected));
4389 CHECK_MATCH ("foo < char * > ( int ) &&",
4390 symbol_name_match_type::WILD, true, EXPECT (expected));
4391 }
4392
4393 /* Test lookup names that don't match anything. */
4394 {
4395 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4396 {});
4397
4398 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4399 {});
4400 }
4401
4402 /* Some wild matching tests, exercising "(anonymous namespace)",
4403 which should not be confused with a parameter list. */
4404 {
4405 static const char *syms[] = {
4406 "A::B::C",
4407 "B::C",
4408 "C",
4409 "A :: B :: C ( int )",
4410 "B :: C ( int )",
4411 "C ( int )",
4412 };
4413
4414 for (const char *s : syms)
4415 {
4416 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4417 EXPECT ("(anonymous namespace)::A::B::C"));
4418 }
4419 }
4420
4421 {
4422 static const char expected[] = "ns2::tmpl<int>::foo2";
4423 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4424 EXPECT (expected));
4425 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4426 EXPECT (expected));
4427 }
4428
4429 SELF_CHECK (!any_mismatch);
4430
4431 #undef EXPECT
4432 #undef CHECK_MATCH
4433 }
4434
4435 static void
4436 run_test ()
4437 {
4438 test_mapped_index_find_name_component_bounds ();
4439 test_dw2_expand_symtabs_matching_symbol ();
4440 }
4441
4442 }} // namespace selftests::dw2_expand_symtabs_matching
4443
4444 #endif /* GDB_SELF_TEST */
4445
4446 /* If FILE_MATCHER is NULL or if PER_CU has
4447 dwarf2_per_cu_quick_data::MARK set (see
4448 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4449 EXPANSION_NOTIFY on it. */
4450
4451 static void
4452 dw2_expand_symtabs_matching_one
4453 (struct dwarf2_per_cu_data *per_cu,
4454 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4455 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4456 {
4457 if (file_matcher == NULL || per_cu->v.quick->mark)
4458 {
4459 bool symtab_was_null
4460 = (per_cu->v.quick->compunit_symtab == NULL);
4461
4462 dw2_instantiate_symtab (per_cu, false);
4463
4464 if (expansion_notify != NULL
4465 && symtab_was_null
4466 && per_cu->v.quick->compunit_symtab != NULL)
4467 expansion_notify (per_cu->v.quick->compunit_symtab);
4468 }
4469 }
4470
4471 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4472 matched, to expand corresponding CUs that were marked. IDX is the
4473 index of the symbol name that matched. */
4474
4475 static void
4476 dw2_expand_marked_cus
4477 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4478 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4479 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4480 search_domain kind)
4481 {
4482 offset_type *vec, vec_len, vec_idx;
4483 bool global_seen = false;
4484 mapped_index &index = *dwarf2_per_objfile->index_table;
4485
4486 vec = (offset_type *) (index.constant_pool
4487 + MAYBE_SWAP (index.symbol_table[idx].vec));
4488 vec_len = MAYBE_SWAP (vec[0]);
4489 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4490 {
4491 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4492 /* This value is only valid for index versions >= 7. */
4493 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4494 gdb_index_symbol_kind symbol_kind =
4495 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4496 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4497 /* Only check the symbol attributes if they're present.
4498 Indices prior to version 7 don't record them,
4499 and indices >= 7 may elide them for certain symbols
4500 (gold does this). */
4501 int attrs_valid =
4502 (index.version >= 7
4503 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4504
4505 /* Work around gold/15646. */
4506 if (attrs_valid)
4507 {
4508 if (!is_static && global_seen)
4509 continue;
4510 if (!is_static)
4511 global_seen = true;
4512 }
4513
4514 /* Only check the symbol's kind if it has one. */
4515 if (attrs_valid)
4516 {
4517 switch (kind)
4518 {
4519 case VARIABLES_DOMAIN:
4520 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4521 continue;
4522 break;
4523 case FUNCTIONS_DOMAIN:
4524 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4525 continue;
4526 break;
4527 case TYPES_DOMAIN:
4528 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4529 continue;
4530 break;
4531 case MODULES_DOMAIN:
4532 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4533 continue;
4534 break;
4535 default:
4536 break;
4537 }
4538 }
4539
4540 /* Don't crash on bad data. */
4541 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4542 + dwarf2_per_objfile->all_type_units.size ()))
4543 {
4544 complaint (_(".gdb_index entry has bad CU index"
4545 " [in module %s]"),
4546 objfile_name (dwarf2_per_objfile->objfile));
4547 continue;
4548 }
4549
4550 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4551 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4552 expansion_notify);
4553 }
4554 }
4555
4556 /* If FILE_MATCHER is non-NULL, set all the
4557 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4558 that match FILE_MATCHER. */
4559
4560 static void
4561 dw_expand_symtabs_matching_file_matcher
4562 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4563 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4564 {
4565 if (file_matcher == NULL)
4566 return;
4567
4568 objfile *const objfile = dwarf2_per_objfile->objfile;
4569
4570 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4571 htab_eq_pointer,
4572 NULL, xcalloc, xfree));
4573 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4574 htab_eq_pointer,
4575 NULL, xcalloc, xfree));
4576
4577 /* The rule is CUs specify all the files, including those used by
4578 any TU, so there's no need to scan TUs here. */
4579
4580 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4581 {
4582 QUIT;
4583
4584 per_cu->v.quick->mark = 0;
4585
4586 /* We only need to look at symtabs not already expanded. */
4587 if (per_cu->v.quick->compunit_symtab)
4588 continue;
4589
4590 quick_file_names *file_data = dw2_get_file_names (per_cu);
4591 if (file_data == NULL)
4592 continue;
4593
4594 if (htab_find (visited_not_found.get (), file_data) != NULL)
4595 continue;
4596 else if (htab_find (visited_found.get (), file_data) != NULL)
4597 {
4598 per_cu->v.quick->mark = 1;
4599 continue;
4600 }
4601
4602 for (int j = 0; j < file_data->num_file_names; ++j)
4603 {
4604 const char *this_real_name;
4605
4606 if (file_matcher (file_data->file_names[j], false))
4607 {
4608 per_cu->v.quick->mark = 1;
4609 break;
4610 }
4611
4612 /* Before we invoke realpath, which can get expensive when many
4613 files are involved, do a quick comparison of the basenames. */
4614 if (!basenames_may_differ
4615 && !file_matcher (lbasename (file_data->file_names[j]),
4616 true))
4617 continue;
4618
4619 this_real_name = dw2_get_real_path (objfile, file_data, j);
4620 if (file_matcher (this_real_name, false))
4621 {
4622 per_cu->v.quick->mark = 1;
4623 break;
4624 }
4625 }
4626
4627 void **slot = htab_find_slot (per_cu->v.quick->mark
4628 ? visited_found.get ()
4629 : visited_not_found.get (),
4630 file_data, INSERT);
4631 *slot = file_data;
4632 }
4633 }
4634
4635 static void
4636 dw2_expand_symtabs_matching
4637 (struct objfile *objfile,
4638 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4639 const lookup_name_info &lookup_name,
4640 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4641 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4642 enum search_domain kind)
4643 {
4644 struct dwarf2_per_objfile *dwarf2_per_objfile
4645 = get_dwarf2_per_objfile (objfile);
4646
4647 /* index_table is NULL if OBJF_READNOW. */
4648 if (!dwarf2_per_objfile->index_table)
4649 return;
4650
4651 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4652
4653 mapped_index &index = *dwarf2_per_objfile->index_table;
4654
4655 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4656 symbol_matcher,
4657 kind, [&] (offset_type idx)
4658 {
4659 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4660 expansion_notify, kind);
4661 return true;
4662 });
4663 }
4664
4665 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4666 symtab. */
4667
4668 static struct compunit_symtab *
4669 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4670 CORE_ADDR pc)
4671 {
4672 int i;
4673
4674 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4675 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4676 return cust;
4677
4678 if (cust->includes == NULL)
4679 return NULL;
4680
4681 for (i = 0; cust->includes[i]; ++i)
4682 {
4683 struct compunit_symtab *s = cust->includes[i];
4684
4685 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4686 if (s != NULL)
4687 return s;
4688 }
4689
4690 return NULL;
4691 }
4692
4693 static struct compunit_symtab *
4694 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4695 struct bound_minimal_symbol msymbol,
4696 CORE_ADDR pc,
4697 struct obj_section *section,
4698 int warn_if_readin)
4699 {
4700 struct dwarf2_per_cu_data *data;
4701 struct compunit_symtab *result;
4702
4703 if (!objfile->partial_symtabs->psymtabs_addrmap)
4704 return NULL;
4705
4706 CORE_ADDR baseaddr = objfile->text_section_offset ();
4707 data = (struct dwarf2_per_cu_data *) addrmap_find
4708 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4709 if (!data)
4710 return NULL;
4711
4712 if (warn_if_readin && data->v.quick->compunit_symtab)
4713 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4714 paddress (get_objfile_arch (objfile), pc));
4715
4716 result
4717 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4718 false),
4719 pc);
4720 gdb_assert (result != NULL);
4721 return result;
4722 }
4723
4724 static void
4725 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4726 void *data, int need_fullname)
4727 {
4728 struct dwarf2_per_objfile *dwarf2_per_objfile
4729 = get_dwarf2_per_objfile (objfile);
4730
4731 if (!dwarf2_per_objfile->filenames_cache)
4732 {
4733 dwarf2_per_objfile->filenames_cache.emplace ();
4734
4735 htab_up visited (htab_create_alloc (10,
4736 htab_hash_pointer, htab_eq_pointer,
4737 NULL, xcalloc, xfree));
4738
4739 /* The rule is CUs specify all the files, including those used
4740 by any TU, so there's no need to scan TUs here. We can
4741 ignore file names coming from already-expanded CUs. */
4742
4743 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4744 {
4745 if (per_cu->v.quick->compunit_symtab)
4746 {
4747 void **slot = htab_find_slot (visited.get (),
4748 per_cu->v.quick->file_names,
4749 INSERT);
4750
4751 *slot = per_cu->v.quick->file_names;
4752 }
4753 }
4754
4755 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4756 {
4757 /* We only need to look at symtabs not already expanded. */
4758 if (per_cu->v.quick->compunit_symtab)
4759 continue;
4760
4761 quick_file_names *file_data = dw2_get_file_names (per_cu);
4762 if (file_data == NULL)
4763 continue;
4764
4765 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4766 if (*slot)
4767 {
4768 /* Already visited. */
4769 continue;
4770 }
4771 *slot = file_data;
4772
4773 for (int j = 0; j < file_data->num_file_names; ++j)
4774 {
4775 const char *filename = file_data->file_names[j];
4776 dwarf2_per_objfile->filenames_cache->seen (filename);
4777 }
4778 }
4779 }
4780
4781 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4782 {
4783 gdb::unique_xmalloc_ptr<char> this_real_name;
4784
4785 if (need_fullname)
4786 this_real_name = gdb_realpath (filename);
4787 (*fun) (filename, this_real_name.get (), data);
4788 });
4789 }
4790
4791 static int
4792 dw2_has_symbols (struct objfile *objfile)
4793 {
4794 return 1;
4795 }
4796
4797 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4798 {
4799 dw2_has_symbols,
4800 dw2_find_last_source_symtab,
4801 dw2_forget_cached_source_info,
4802 dw2_map_symtabs_matching_filename,
4803 dw2_lookup_symbol,
4804 dw2_print_stats,
4805 dw2_dump,
4806 dw2_expand_symtabs_for_function,
4807 dw2_expand_all_symtabs,
4808 dw2_expand_symtabs_with_fullname,
4809 dw2_map_matching_symbols,
4810 dw2_expand_symtabs_matching,
4811 dw2_find_pc_sect_compunit_symtab,
4812 NULL,
4813 dw2_map_symbol_filenames
4814 };
4815
4816 /* DWARF-5 debug_names reader. */
4817
4818 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4819 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4820
4821 /* A helper function that reads the .debug_names section in SECTION
4822 and fills in MAP. FILENAME is the name of the file containing the
4823 section; it is used for error reporting.
4824
4825 Returns true if all went well, false otherwise. */
4826
4827 static bool
4828 read_debug_names_from_section (struct objfile *objfile,
4829 const char *filename,
4830 struct dwarf2_section_info *section,
4831 mapped_debug_names &map)
4832 {
4833 if (section->empty ())
4834 return false;
4835
4836 /* Older elfutils strip versions could keep the section in the main
4837 executable while splitting it for the separate debug info file. */
4838 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4839 return false;
4840
4841 section->read (objfile);
4842
4843 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4844
4845 const gdb_byte *addr = section->buffer;
4846
4847 bfd *const abfd = section->get_bfd_owner ();
4848
4849 unsigned int bytes_read;
4850 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4851 addr += bytes_read;
4852
4853 map.dwarf5_is_dwarf64 = bytes_read != 4;
4854 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4855 if (bytes_read + length != section->size)
4856 {
4857 /* There may be multiple per-CU indices. */
4858 warning (_("Section .debug_names in %s length %s does not match "
4859 "section length %s, ignoring .debug_names."),
4860 filename, plongest (bytes_read + length),
4861 pulongest (section->size));
4862 return false;
4863 }
4864
4865 /* The version number. */
4866 uint16_t version = read_2_bytes (abfd, addr);
4867 addr += 2;
4868 if (version != 5)
4869 {
4870 warning (_("Section .debug_names in %s has unsupported version %d, "
4871 "ignoring .debug_names."),
4872 filename, version);
4873 return false;
4874 }
4875
4876 /* Padding. */
4877 uint16_t padding = read_2_bytes (abfd, addr);
4878 addr += 2;
4879 if (padding != 0)
4880 {
4881 warning (_("Section .debug_names in %s has unsupported padding %d, "
4882 "ignoring .debug_names."),
4883 filename, padding);
4884 return false;
4885 }
4886
4887 /* comp_unit_count - The number of CUs in the CU list. */
4888 map.cu_count = read_4_bytes (abfd, addr);
4889 addr += 4;
4890
4891 /* local_type_unit_count - The number of TUs in the local TU
4892 list. */
4893 map.tu_count = read_4_bytes (abfd, addr);
4894 addr += 4;
4895
4896 /* foreign_type_unit_count - The number of TUs in the foreign TU
4897 list. */
4898 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4899 addr += 4;
4900 if (foreign_tu_count != 0)
4901 {
4902 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4903 "ignoring .debug_names."),
4904 filename, static_cast<unsigned long> (foreign_tu_count));
4905 return false;
4906 }
4907
4908 /* bucket_count - The number of hash buckets in the hash lookup
4909 table. */
4910 map.bucket_count = read_4_bytes (abfd, addr);
4911 addr += 4;
4912
4913 /* name_count - The number of unique names in the index. */
4914 map.name_count = read_4_bytes (abfd, addr);
4915 addr += 4;
4916
4917 /* abbrev_table_size - The size in bytes of the abbreviations
4918 table. */
4919 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4920 addr += 4;
4921
4922 /* augmentation_string_size - The size in bytes of the augmentation
4923 string. This value is rounded up to a multiple of 4. */
4924 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4925 addr += 4;
4926 map.augmentation_is_gdb = ((augmentation_string_size
4927 == sizeof (dwarf5_augmentation))
4928 && memcmp (addr, dwarf5_augmentation,
4929 sizeof (dwarf5_augmentation)) == 0);
4930 augmentation_string_size += (-augmentation_string_size) & 3;
4931 addr += augmentation_string_size;
4932
4933 /* List of CUs */
4934 map.cu_table_reordered = addr;
4935 addr += map.cu_count * map.offset_size;
4936
4937 /* List of Local TUs */
4938 map.tu_table_reordered = addr;
4939 addr += map.tu_count * map.offset_size;
4940
4941 /* Hash Lookup Table */
4942 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4943 addr += map.bucket_count * 4;
4944 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4945 addr += map.name_count * 4;
4946
4947 /* Name Table */
4948 map.name_table_string_offs_reordered = addr;
4949 addr += map.name_count * map.offset_size;
4950 map.name_table_entry_offs_reordered = addr;
4951 addr += map.name_count * map.offset_size;
4952
4953 const gdb_byte *abbrev_table_start = addr;
4954 for (;;)
4955 {
4956 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4957 addr += bytes_read;
4958 if (index_num == 0)
4959 break;
4960
4961 const auto insertpair
4962 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4963 if (!insertpair.second)
4964 {
4965 warning (_("Section .debug_names in %s has duplicate index %s, "
4966 "ignoring .debug_names."),
4967 filename, pulongest (index_num));
4968 return false;
4969 }
4970 mapped_debug_names::index_val &indexval = insertpair.first->second;
4971 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4972 addr += bytes_read;
4973
4974 for (;;)
4975 {
4976 mapped_debug_names::index_val::attr attr;
4977 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4978 addr += bytes_read;
4979 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4980 addr += bytes_read;
4981 if (attr.form == DW_FORM_implicit_const)
4982 {
4983 attr.implicit_const = read_signed_leb128 (abfd, addr,
4984 &bytes_read);
4985 addr += bytes_read;
4986 }
4987 if (attr.dw_idx == 0 && attr.form == 0)
4988 break;
4989 indexval.attr_vec.push_back (std::move (attr));
4990 }
4991 }
4992 if (addr != abbrev_table_start + abbrev_table_size)
4993 {
4994 warning (_("Section .debug_names in %s has abbreviation_table "
4995 "of size %s vs. written as %u, ignoring .debug_names."),
4996 filename, plongest (addr - abbrev_table_start),
4997 abbrev_table_size);
4998 return false;
4999 }
5000 map.entry_pool = addr;
5001
5002 return true;
5003 }
5004
5005 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5006 list. */
5007
5008 static void
5009 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5010 const mapped_debug_names &map,
5011 dwarf2_section_info &section,
5012 bool is_dwz)
5013 {
5014 sect_offset sect_off_prev;
5015 for (uint32_t i = 0; i <= map.cu_count; ++i)
5016 {
5017 sect_offset sect_off_next;
5018 if (i < map.cu_count)
5019 {
5020 sect_off_next
5021 = (sect_offset) (extract_unsigned_integer
5022 (map.cu_table_reordered + i * map.offset_size,
5023 map.offset_size,
5024 map.dwarf5_byte_order));
5025 }
5026 else
5027 sect_off_next = (sect_offset) section.size;
5028 if (i >= 1)
5029 {
5030 const ULONGEST length = sect_off_next - sect_off_prev;
5031 dwarf2_per_cu_data *per_cu
5032 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5033 sect_off_prev, length);
5034 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5035 }
5036 sect_off_prev = sect_off_next;
5037 }
5038 }
5039
5040 /* Read the CU list from the mapped index, and use it to create all
5041 the CU objects for this dwarf2_per_objfile. */
5042
5043 static void
5044 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5045 const mapped_debug_names &map,
5046 const mapped_debug_names &dwz_map)
5047 {
5048 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5049 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5050
5051 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5052 dwarf2_per_objfile->info,
5053 false /* is_dwz */);
5054
5055 if (dwz_map.cu_count == 0)
5056 return;
5057
5058 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5059 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5060 true /* is_dwz */);
5061 }
5062
5063 /* Read .debug_names. If everything went ok, initialize the "quick"
5064 elements of all the CUs and return true. Otherwise, return false. */
5065
5066 static bool
5067 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5068 {
5069 std::unique_ptr<mapped_debug_names> map
5070 (new mapped_debug_names (dwarf2_per_objfile));
5071 mapped_debug_names dwz_map (dwarf2_per_objfile);
5072 struct objfile *objfile = dwarf2_per_objfile->objfile;
5073
5074 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5075 &dwarf2_per_objfile->debug_names,
5076 *map))
5077 return false;
5078
5079 /* Don't use the index if it's empty. */
5080 if (map->name_count == 0)
5081 return false;
5082
5083 /* If there is a .dwz file, read it so we can get its CU list as
5084 well. */
5085 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5086 if (dwz != NULL)
5087 {
5088 if (!read_debug_names_from_section (objfile,
5089 bfd_get_filename (dwz->dwz_bfd.get ()),
5090 &dwz->debug_names, dwz_map))
5091 {
5092 warning (_("could not read '.debug_names' section from %s; skipping"),
5093 bfd_get_filename (dwz->dwz_bfd.get ()));
5094 return false;
5095 }
5096 }
5097
5098 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5099
5100 if (map->tu_count != 0)
5101 {
5102 /* We can only handle a single .debug_types when we have an
5103 index. */
5104 if (dwarf2_per_objfile->types.size () != 1)
5105 return false;
5106
5107 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5108
5109 create_signatured_type_table_from_debug_names
5110 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5111 }
5112
5113 create_addrmap_from_aranges (dwarf2_per_objfile,
5114 &dwarf2_per_objfile->debug_aranges);
5115
5116 dwarf2_per_objfile->debug_names_table = std::move (map);
5117 dwarf2_per_objfile->using_index = 1;
5118 dwarf2_per_objfile->quick_file_names_table =
5119 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5120
5121 return true;
5122 }
5123
5124 /* Type used to manage iterating over all CUs looking for a symbol for
5125 .debug_names. */
5126
5127 class dw2_debug_names_iterator
5128 {
5129 public:
5130 dw2_debug_names_iterator (const mapped_debug_names &map,
5131 gdb::optional<block_enum> block_index,
5132 domain_enum domain,
5133 const char *name)
5134 : m_map (map), m_block_index (block_index), m_domain (domain),
5135 m_addr (find_vec_in_debug_names (map, name))
5136 {}
5137
5138 dw2_debug_names_iterator (const mapped_debug_names &map,
5139 search_domain search, uint32_t namei)
5140 : m_map (map),
5141 m_search (search),
5142 m_addr (find_vec_in_debug_names (map, namei))
5143 {}
5144
5145 dw2_debug_names_iterator (const mapped_debug_names &map,
5146 block_enum block_index, domain_enum domain,
5147 uint32_t namei)
5148 : m_map (map), m_block_index (block_index), m_domain (domain),
5149 m_addr (find_vec_in_debug_names (map, namei))
5150 {}
5151
5152 /* Return the next matching CU or NULL if there are no more. */
5153 dwarf2_per_cu_data *next ();
5154
5155 private:
5156 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5157 const char *name);
5158 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5159 uint32_t namei);
5160
5161 /* The internalized form of .debug_names. */
5162 const mapped_debug_names &m_map;
5163
5164 /* If set, only look for symbols that match that block. Valid values are
5165 GLOBAL_BLOCK and STATIC_BLOCK. */
5166 const gdb::optional<block_enum> m_block_index;
5167
5168 /* The kind of symbol we're looking for. */
5169 const domain_enum m_domain = UNDEF_DOMAIN;
5170 const search_domain m_search = ALL_DOMAIN;
5171
5172 /* The list of CUs from the index entry of the symbol, or NULL if
5173 not found. */
5174 const gdb_byte *m_addr;
5175 };
5176
5177 const char *
5178 mapped_debug_names::namei_to_name (uint32_t namei) const
5179 {
5180 const ULONGEST namei_string_offs
5181 = extract_unsigned_integer ((name_table_string_offs_reordered
5182 + namei * offset_size),
5183 offset_size,
5184 dwarf5_byte_order);
5185 return read_indirect_string_at_offset
5186 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5187 }
5188
5189 /* Find a slot in .debug_names for the object named NAME. If NAME is
5190 found, return pointer to its pool data. If NAME cannot be found,
5191 return NULL. */
5192
5193 const gdb_byte *
5194 dw2_debug_names_iterator::find_vec_in_debug_names
5195 (const mapped_debug_names &map, const char *name)
5196 {
5197 int (*cmp) (const char *, const char *);
5198
5199 gdb::unique_xmalloc_ptr<char> without_params;
5200 if (current_language->la_language == language_cplus
5201 || current_language->la_language == language_fortran
5202 || current_language->la_language == language_d)
5203 {
5204 /* NAME is already canonical. Drop any qualifiers as
5205 .debug_names does not contain any. */
5206
5207 if (strchr (name, '(') != NULL)
5208 {
5209 without_params = cp_remove_params (name);
5210 if (without_params != NULL)
5211 name = without_params.get ();
5212 }
5213 }
5214
5215 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5216
5217 const uint32_t full_hash = dwarf5_djb_hash (name);
5218 uint32_t namei
5219 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5220 (map.bucket_table_reordered
5221 + (full_hash % map.bucket_count)), 4,
5222 map.dwarf5_byte_order);
5223 if (namei == 0)
5224 return NULL;
5225 --namei;
5226 if (namei >= map.name_count)
5227 {
5228 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5229 "[in module %s]"),
5230 namei, map.name_count,
5231 objfile_name (map.dwarf2_per_objfile->objfile));
5232 return NULL;
5233 }
5234
5235 for (;;)
5236 {
5237 const uint32_t namei_full_hash
5238 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5239 (map.hash_table_reordered + namei), 4,
5240 map.dwarf5_byte_order);
5241 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5242 return NULL;
5243
5244 if (full_hash == namei_full_hash)
5245 {
5246 const char *const namei_string = map.namei_to_name (namei);
5247
5248 #if 0 /* An expensive sanity check. */
5249 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5250 {
5251 complaint (_("Wrong .debug_names hash for string at index %u "
5252 "[in module %s]"),
5253 namei, objfile_name (dwarf2_per_objfile->objfile));
5254 return NULL;
5255 }
5256 #endif
5257
5258 if (cmp (namei_string, name) == 0)
5259 {
5260 const ULONGEST namei_entry_offs
5261 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5262 + namei * map.offset_size),
5263 map.offset_size, map.dwarf5_byte_order);
5264 return map.entry_pool + namei_entry_offs;
5265 }
5266 }
5267
5268 ++namei;
5269 if (namei >= map.name_count)
5270 return NULL;
5271 }
5272 }
5273
5274 const gdb_byte *
5275 dw2_debug_names_iterator::find_vec_in_debug_names
5276 (const mapped_debug_names &map, uint32_t namei)
5277 {
5278 if (namei >= map.name_count)
5279 {
5280 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5281 "[in module %s]"),
5282 namei, map.name_count,
5283 objfile_name (map.dwarf2_per_objfile->objfile));
5284 return NULL;
5285 }
5286
5287 const ULONGEST namei_entry_offs
5288 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5289 + namei * map.offset_size),
5290 map.offset_size, map.dwarf5_byte_order);
5291 return map.entry_pool + namei_entry_offs;
5292 }
5293
5294 /* See dw2_debug_names_iterator. */
5295
5296 dwarf2_per_cu_data *
5297 dw2_debug_names_iterator::next ()
5298 {
5299 if (m_addr == NULL)
5300 return NULL;
5301
5302 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5303 struct objfile *objfile = dwarf2_per_objfile->objfile;
5304 bfd *const abfd = objfile->obfd;
5305
5306 again:
5307
5308 unsigned int bytes_read;
5309 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5310 m_addr += bytes_read;
5311 if (abbrev == 0)
5312 return NULL;
5313
5314 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5315 if (indexval_it == m_map.abbrev_map.cend ())
5316 {
5317 complaint (_("Wrong .debug_names undefined abbrev code %s "
5318 "[in module %s]"),
5319 pulongest (abbrev), objfile_name (objfile));
5320 return NULL;
5321 }
5322 const mapped_debug_names::index_val &indexval = indexval_it->second;
5323 enum class symbol_linkage {
5324 unknown,
5325 static_,
5326 extern_,
5327 } symbol_linkage_ = symbol_linkage::unknown;
5328 dwarf2_per_cu_data *per_cu = NULL;
5329 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5330 {
5331 ULONGEST ull;
5332 switch (attr.form)
5333 {
5334 case DW_FORM_implicit_const:
5335 ull = attr.implicit_const;
5336 break;
5337 case DW_FORM_flag_present:
5338 ull = 1;
5339 break;
5340 case DW_FORM_udata:
5341 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5342 m_addr += bytes_read;
5343 break;
5344 default:
5345 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5346 dwarf_form_name (attr.form),
5347 objfile_name (objfile));
5348 return NULL;
5349 }
5350 switch (attr.dw_idx)
5351 {
5352 case DW_IDX_compile_unit:
5353 /* Don't crash on bad data. */
5354 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5355 {
5356 complaint (_(".debug_names entry has bad CU index %s"
5357 " [in module %s]"),
5358 pulongest (ull),
5359 objfile_name (dwarf2_per_objfile->objfile));
5360 continue;
5361 }
5362 per_cu = dwarf2_per_objfile->get_cutu (ull);
5363 break;
5364 case DW_IDX_type_unit:
5365 /* Don't crash on bad data. */
5366 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5367 {
5368 complaint (_(".debug_names entry has bad TU index %s"
5369 " [in module %s]"),
5370 pulongest (ull),
5371 objfile_name (dwarf2_per_objfile->objfile));
5372 continue;
5373 }
5374 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5375 break;
5376 case DW_IDX_GNU_internal:
5377 if (!m_map.augmentation_is_gdb)
5378 break;
5379 symbol_linkage_ = symbol_linkage::static_;
5380 break;
5381 case DW_IDX_GNU_external:
5382 if (!m_map.augmentation_is_gdb)
5383 break;
5384 symbol_linkage_ = symbol_linkage::extern_;
5385 break;
5386 }
5387 }
5388
5389 /* Skip if already read in. */
5390 if (per_cu->v.quick->compunit_symtab)
5391 goto again;
5392
5393 /* Check static vs global. */
5394 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5395 {
5396 const bool want_static = *m_block_index == STATIC_BLOCK;
5397 const bool symbol_is_static =
5398 symbol_linkage_ == symbol_linkage::static_;
5399 if (want_static != symbol_is_static)
5400 goto again;
5401 }
5402
5403 /* Match dw2_symtab_iter_next, symbol_kind
5404 and debug_names::psymbol_tag. */
5405 switch (m_domain)
5406 {
5407 case VAR_DOMAIN:
5408 switch (indexval.dwarf_tag)
5409 {
5410 case DW_TAG_variable:
5411 case DW_TAG_subprogram:
5412 /* Some types are also in VAR_DOMAIN. */
5413 case DW_TAG_typedef:
5414 case DW_TAG_structure_type:
5415 break;
5416 default:
5417 goto again;
5418 }
5419 break;
5420 case STRUCT_DOMAIN:
5421 switch (indexval.dwarf_tag)
5422 {
5423 case DW_TAG_typedef:
5424 case DW_TAG_structure_type:
5425 break;
5426 default:
5427 goto again;
5428 }
5429 break;
5430 case LABEL_DOMAIN:
5431 switch (indexval.dwarf_tag)
5432 {
5433 case 0:
5434 case DW_TAG_variable:
5435 break;
5436 default:
5437 goto again;
5438 }
5439 break;
5440 case MODULE_DOMAIN:
5441 switch (indexval.dwarf_tag)
5442 {
5443 case DW_TAG_module:
5444 break;
5445 default:
5446 goto again;
5447 }
5448 break;
5449 default:
5450 break;
5451 }
5452
5453 /* Match dw2_expand_symtabs_matching, symbol_kind and
5454 debug_names::psymbol_tag. */
5455 switch (m_search)
5456 {
5457 case VARIABLES_DOMAIN:
5458 switch (indexval.dwarf_tag)
5459 {
5460 case DW_TAG_variable:
5461 break;
5462 default:
5463 goto again;
5464 }
5465 break;
5466 case FUNCTIONS_DOMAIN:
5467 switch (indexval.dwarf_tag)
5468 {
5469 case DW_TAG_subprogram:
5470 break;
5471 default:
5472 goto again;
5473 }
5474 break;
5475 case TYPES_DOMAIN:
5476 switch (indexval.dwarf_tag)
5477 {
5478 case DW_TAG_typedef:
5479 case DW_TAG_structure_type:
5480 break;
5481 default:
5482 goto again;
5483 }
5484 break;
5485 case MODULES_DOMAIN:
5486 switch (indexval.dwarf_tag)
5487 {
5488 case DW_TAG_module:
5489 break;
5490 default:
5491 goto again;
5492 }
5493 default:
5494 break;
5495 }
5496
5497 return per_cu;
5498 }
5499
5500 static struct compunit_symtab *
5501 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5502 const char *name, domain_enum domain)
5503 {
5504 struct dwarf2_per_objfile *dwarf2_per_objfile
5505 = get_dwarf2_per_objfile (objfile);
5506
5507 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5508 if (!mapp)
5509 {
5510 /* index is NULL if OBJF_READNOW. */
5511 return NULL;
5512 }
5513 const auto &map = *mapp;
5514
5515 dw2_debug_names_iterator iter (map, block_index, domain, name);
5516
5517 struct compunit_symtab *stab_best = NULL;
5518 struct dwarf2_per_cu_data *per_cu;
5519 while ((per_cu = iter.next ()) != NULL)
5520 {
5521 struct symbol *sym, *with_opaque = NULL;
5522 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5523 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5524 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5525
5526 sym = block_find_symbol (block, name, domain,
5527 block_find_non_opaque_type_preferred,
5528 &with_opaque);
5529
5530 /* Some caution must be observed with overloaded functions and
5531 methods, since the index will not contain any overload
5532 information (but NAME might contain it). */
5533
5534 if (sym != NULL
5535 && strcmp_iw (sym->search_name (), name) == 0)
5536 return stab;
5537 if (with_opaque != NULL
5538 && strcmp_iw (with_opaque->search_name (), name) == 0)
5539 stab_best = stab;
5540
5541 /* Keep looking through other CUs. */
5542 }
5543
5544 return stab_best;
5545 }
5546
5547 /* This dumps minimal information about .debug_names. It is called
5548 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5549 uses this to verify that .debug_names has been loaded. */
5550
5551 static void
5552 dw2_debug_names_dump (struct objfile *objfile)
5553 {
5554 struct dwarf2_per_objfile *dwarf2_per_objfile
5555 = get_dwarf2_per_objfile (objfile);
5556
5557 gdb_assert (dwarf2_per_objfile->using_index);
5558 printf_filtered (".debug_names:");
5559 if (dwarf2_per_objfile->debug_names_table)
5560 printf_filtered (" exists\n");
5561 else
5562 printf_filtered (" faked for \"readnow\"\n");
5563 printf_filtered ("\n");
5564 }
5565
5566 static void
5567 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5568 const char *func_name)
5569 {
5570 struct dwarf2_per_objfile *dwarf2_per_objfile
5571 = get_dwarf2_per_objfile (objfile);
5572
5573 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5574 if (dwarf2_per_objfile->debug_names_table)
5575 {
5576 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5577
5578 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5579
5580 struct dwarf2_per_cu_data *per_cu;
5581 while ((per_cu = iter.next ()) != NULL)
5582 dw2_instantiate_symtab (per_cu, false);
5583 }
5584 }
5585
5586 static void
5587 dw2_debug_names_map_matching_symbols
5588 (struct objfile *objfile,
5589 const lookup_name_info &name, domain_enum domain,
5590 int global,
5591 gdb::function_view<symbol_found_callback_ftype> callback,
5592 symbol_compare_ftype *ordered_compare)
5593 {
5594 struct dwarf2_per_objfile *dwarf2_per_objfile
5595 = get_dwarf2_per_objfile (objfile);
5596
5597 /* debug_names_table is NULL if OBJF_READNOW. */
5598 if (!dwarf2_per_objfile->debug_names_table)
5599 return;
5600
5601 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5602 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5603
5604 const char *match_name = name.ada ().lookup_name ().c_str ();
5605 auto matcher = [&] (const char *symname)
5606 {
5607 if (ordered_compare == nullptr)
5608 return true;
5609 return ordered_compare (symname, match_name) == 0;
5610 };
5611
5612 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5613 [&] (offset_type namei)
5614 {
5615 /* The name was matched, now expand corresponding CUs that were
5616 marked. */
5617 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5618
5619 struct dwarf2_per_cu_data *per_cu;
5620 while ((per_cu = iter.next ()) != NULL)
5621 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5622 return true;
5623 });
5624
5625 /* It's a shame we couldn't do this inside the
5626 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5627 that have already been expanded. Instead, this loop matches what
5628 the psymtab code does. */
5629 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5630 {
5631 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5632 if (cust != nullptr)
5633 {
5634 const struct block *block
5635 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5636 if (!iterate_over_symbols_terminated (block, name,
5637 domain, callback))
5638 break;
5639 }
5640 }
5641 }
5642
5643 static void
5644 dw2_debug_names_expand_symtabs_matching
5645 (struct objfile *objfile,
5646 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5647 const lookup_name_info &lookup_name,
5648 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5649 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5650 enum search_domain kind)
5651 {
5652 struct dwarf2_per_objfile *dwarf2_per_objfile
5653 = get_dwarf2_per_objfile (objfile);
5654
5655 /* debug_names_table is NULL if OBJF_READNOW. */
5656 if (!dwarf2_per_objfile->debug_names_table)
5657 return;
5658
5659 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5660
5661 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5662
5663 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5664 symbol_matcher,
5665 kind, [&] (offset_type namei)
5666 {
5667 /* The name was matched, now expand corresponding CUs that were
5668 marked. */
5669 dw2_debug_names_iterator iter (map, kind, namei);
5670
5671 struct dwarf2_per_cu_data *per_cu;
5672 while ((per_cu = iter.next ()) != NULL)
5673 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5674 expansion_notify);
5675 return true;
5676 });
5677 }
5678
5679 const struct quick_symbol_functions dwarf2_debug_names_functions =
5680 {
5681 dw2_has_symbols,
5682 dw2_find_last_source_symtab,
5683 dw2_forget_cached_source_info,
5684 dw2_map_symtabs_matching_filename,
5685 dw2_debug_names_lookup_symbol,
5686 dw2_print_stats,
5687 dw2_debug_names_dump,
5688 dw2_debug_names_expand_symtabs_for_function,
5689 dw2_expand_all_symtabs,
5690 dw2_expand_symtabs_with_fullname,
5691 dw2_debug_names_map_matching_symbols,
5692 dw2_debug_names_expand_symtabs_matching,
5693 dw2_find_pc_sect_compunit_symtab,
5694 NULL,
5695 dw2_map_symbol_filenames
5696 };
5697
5698 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5699 to either a dwarf2_per_objfile or dwz_file object. */
5700
5701 template <typename T>
5702 static gdb::array_view<const gdb_byte>
5703 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5704 {
5705 dwarf2_section_info *section = &section_owner->gdb_index;
5706
5707 if (section->empty ())
5708 return {};
5709
5710 /* Older elfutils strip versions could keep the section in the main
5711 executable while splitting it for the separate debug info file. */
5712 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5713 return {};
5714
5715 section->read (obj);
5716
5717 /* dwarf2_section_info::size is a bfd_size_type, while
5718 gdb::array_view works with size_t. On 32-bit hosts, with
5719 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5720 is 32-bit. So we need an explicit narrowing conversion here.
5721 This is fine, because it's impossible to allocate or mmap an
5722 array/buffer larger than what size_t can represent. */
5723 return gdb::make_array_view (section->buffer, section->size);
5724 }
5725
5726 /* Lookup the index cache for the contents of the index associated to
5727 DWARF2_OBJ. */
5728
5729 static gdb::array_view<const gdb_byte>
5730 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5731 {
5732 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5733 if (build_id == nullptr)
5734 return {};
5735
5736 return global_index_cache.lookup_gdb_index (build_id,
5737 &dwarf2_obj->index_cache_res);
5738 }
5739
5740 /* Same as the above, but for DWZ. */
5741
5742 static gdb::array_view<const gdb_byte>
5743 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5744 {
5745 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5746 if (build_id == nullptr)
5747 return {};
5748
5749 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5750 }
5751
5752 /* See symfile.h. */
5753
5754 bool
5755 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5756 {
5757 struct dwarf2_per_objfile *dwarf2_per_objfile
5758 = get_dwarf2_per_objfile (objfile);
5759
5760 /* If we're about to read full symbols, don't bother with the
5761 indices. In this case we also don't care if some other debug
5762 format is making psymtabs, because they are all about to be
5763 expanded anyway. */
5764 if ((objfile->flags & OBJF_READNOW))
5765 {
5766 dwarf2_per_objfile->using_index = 1;
5767 create_all_comp_units (dwarf2_per_objfile);
5768 create_all_type_units (dwarf2_per_objfile);
5769 dwarf2_per_objfile->quick_file_names_table
5770 = create_quick_file_names_table
5771 (dwarf2_per_objfile->all_comp_units.size ());
5772
5773 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5774 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5775 {
5776 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5777
5778 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5779 struct dwarf2_per_cu_quick_data);
5780 }
5781
5782 /* Return 1 so that gdb sees the "quick" functions. However,
5783 these functions will be no-ops because we will have expanded
5784 all symtabs. */
5785 *index_kind = dw_index_kind::GDB_INDEX;
5786 return true;
5787 }
5788
5789 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5790 {
5791 *index_kind = dw_index_kind::DEBUG_NAMES;
5792 return true;
5793 }
5794
5795 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5796 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5797 get_gdb_index_contents_from_section<dwz_file>))
5798 {
5799 *index_kind = dw_index_kind::GDB_INDEX;
5800 return true;
5801 }
5802
5803 /* ... otherwise, try to find the index in the index cache. */
5804 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5805 get_gdb_index_contents_from_cache,
5806 get_gdb_index_contents_from_cache_dwz))
5807 {
5808 global_index_cache.hit ();
5809 *index_kind = dw_index_kind::GDB_INDEX;
5810 return true;
5811 }
5812
5813 global_index_cache.miss ();
5814 return false;
5815 }
5816
5817 \f
5818
5819 /* Build a partial symbol table. */
5820
5821 void
5822 dwarf2_build_psymtabs (struct objfile *objfile)
5823 {
5824 struct dwarf2_per_objfile *dwarf2_per_objfile
5825 = get_dwarf2_per_objfile (objfile);
5826
5827 init_psymbol_list (objfile, 1024);
5828
5829 try
5830 {
5831 /* This isn't really ideal: all the data we allocate on the
5832 objfile's obstack is still uselessly kept around. However,
5833 freeing it seems unsafe. */
5834 psymtab_discarder psymtabs (objfile);
5835 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5836 psymtabs.keep ();
5837
5838 /* (maybe) store an index in the cache. */
5839 global_index_cache.store (dwarf2_per_objfile);
5840 }
5841 catch (const gdb_exception_error &except)
5842 {
5843 exception_print (gdb_stderr, except);
5844 }
5845 }
5846
5847 /* Find the base address of the compilation unit for range lists and
5848 location lists. It will normally be specified by DW_AT_low_pc.
5849 In DWARF-3 draft 4, the base address could be overridden by
5850 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5851 compilation units with discontinuous ranges. */
5852
5853 static void
5854 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5855 {
5856 struct attribute *attr;
5857
5858 cu->base_known = 0;
5859 cu->base_address = 0;
5860
5861 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5862 if (attr != nullptr)
5863 {
5864 cu->base_address = attr->value_as_address ();
5865 cu->base_known = 1;
5866 }
5867 else
5868 {
5869 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5870 if (attr != nullptr)
5871 {
5872 cu->base_address = attr->value_as_address ();
5873 cu->base_known = 1;
5874 }
5875 }
5876 }
5877
5878 /* Helper function that returns the proper abbrev section for
5879 THIS_CU. */
5880
5881 static struct dwarf2_section_info *
5882 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5883 {
5884 struct dwarf2_section_info *abbrev;
5885 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5886
5887 if (this_cu->is_dwz)
5888 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5889 else
5890 abbrev = &dwarf2_per_objfile->abbrev;
5891
5892 return abbrev;
5893 }
5894
5895 /* Fetch the abbreviation table offset from a comp or type unit header. */
5896
5897 static sect_offset
5898 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5899 struct dwarf2_section_info *section,
5900 sect_offset sect_off)
5901 {
5902 bfd *abfd = section->get_bfd_owner ();
5903 const gdb_byte *info_ptr;
5904 unsigned int initial_length_size, offset_size;
5905 uint16_t version;
5906
5907 section->read (dwarf2_per_objfile->objfile);
5908 info_ptr = section->buffer + to_underlying (sect_off);
5909 read_initial_length (abfd, info_ptr, &initial_length_size);
5910 offset_size = initial_length_size == 4 ? 4 : 8;
5911 info_ptr += initial_length_size;
5912
5913 version = read_2_bytes (abfd, info_ptr);
5914 info_ptr += 2;
5915 if (version >= 5)
5916 {
5917 /* Skip unit type and address size. */
5918 info_ptr += 2;
5919 }
5920
5921 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5922 }
5923
5924 /* A partial symtab that is used only for include files. */
5925 struct dwarf2_include_psymtab : public partial_symtab
5926 {
5927 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5928 : partial_symtab (filename, objfile)
5929 {
5930 }
5931
5932 void read_symtab (struct objfile *objfile) override
5933 {
5934 expand_psymtab (objfile);
5935 }
5936
5937 void expand_psymtab (struct objfile *objfile) override
5938 {
5939 if (m_readin)
5940 return;
5941 /* It's an include file, no symbols to read for it.
5942 Everything is in the parent symtab. */
5943 read_dependencies (objfile);
5944 m_readin = true;
5945 }
5946
5947 bool readin_p () const override
5948 {
5949 return m_readin;
5950 }
5951
5952 struct compunit_symtab *get_compunit_symtab () const override
5953 {
5954 return nullptr;
5955 }
5956
5957 private:
5958
5959 bool m_readin = false;
5960 };
5961
5962 /* Allocate a new partial symtab for file named NAME and mark this new
5963 partial symtab as being an include of PST. */
5964
5965 static void
5966 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5967 struct objfile *objfile)
5968 {
5969 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5970
5971 if (!IS_ABSOLUTE_PATH (subpst->filename))
5972 {
5973 /* It shares objfile->objfile_obstack. */
5974 subpst->dirname = pst->dirname;
5975 }
5976
5977 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5978 subpst->dependencies[0] = pst;
5979 subpst->number_of_dependencies = 1;
5980 }
5981
5982 /* Read the Line Number Program data and extract the list of files
5983 included by the source file represented by PST. Build an include
5984 partial symtab for each of these included files. */
5985
5986 static void
5987 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5988 struct die_info *die,
5989 dwarf2_psymtab *pst)
5990 {
5991 line_header_up lh;
5992 struct attribute *attr;
5993
5994 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5995 if (attr != nullptr)
5996 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5997 if (lh == NULL)
5998 return; /* No linetable, so no includes. */
5999
6000 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6001 that we pass in the raw text_low here; that is ok because we're
6002 only decoding the line table to make include partial symtabs, and
6003 so the addresses aren't really used. */
6004 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6005 pst->raw_text_low (), 1);
6006 }
6007
6008 static hashval_t
6009 hash_signatured_type (const void *item)
6010 {
6011 const struct signatured_type *sig_type
6012 = (const struct signatured_type *) item;
6013
6014 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6015 return sig_type->signature;
6016 }
6017
6018 static int
6019 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6020 {
6021 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6022 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6023
6024 return lhs->signature == rhs->signature;
6025 }
6026
6027 /* Allocate a hash table for signatured types. */
6028
6029 static htab_up
6030 allocate_signatured_type_table ()
6031 {
6032 return htab_up (htab_create_alloc (41,
6033 hash_signatured_type,
6034 eq_signatured_type,
6035 NULL, xcalloc, xfree));
6036 }
6037
6038 /* A helper function to add a signatured type CU to a table. */
6039
6040 static int
6041 add_signatured_type_cu_to_table (void **slot, void *datum)
6042 {
6043 struct signatured_type *sigt = (struct signatured_type *) *slot;
6044 std::vector<signatured_type *> *all_type_units
6045 = (std::vector<signatured_type *> *) datum;
6046
6047 all_type_units->push_back (sigt);
6048
6049 return 1;
6050 }
6051
6052 /* A helper for create_debug_types_hash_table. Read types from SECTION
6053 and fill them into TYPES_HTAB. It will process only type units,
6054 therefore DW_UT_type. */
6055
6056 static void
6057 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6058 struct dwo_file *dwo_file,
6059 dwarf2_section_info *section, htab_up &types_htab,
6060 rcuh_kind section_kind)
6061 {
6062 struct objfile *objfile = dwarf2_per_objfile->objfile;
6063 struct dwarf2_section_info *abbrev_section;
6064 bfd *abfd;
6065 const gdb_byte *info_ptr, *end_ptr;
6066
6067 abbrev_section = (dwo_file != NULL
6068 ? &dwo_file->sections.abbrev
6069 : &dwarf2_per_objfile->abbrev);
6070
6071 if (dwarf_read_debug)
6072 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6073 section->get_name (),
6074 abbrev_section->get_file_name ());
6075
6076 section->read (objfile);
6077 info_ptr = section->buffer;
6078
6079 if (info_ptr == NULL)
6080 return;
6081
6082 /* We can't set abfd until now because the section may be empty or
6083 not present, in which case the bfd is unknown. */
6084 abfd = section->get_bfd_owner ();
6085
6086 /* We don't use cutu_reader here because we don't need to read
6087 any dies: the signature is in the header. */
6088
6089 end_ptr = info_ptr + section->size;
6090 while (info_ptr < end_ptr)
6091 {
6092 struct signatured_type *sig_type;
6093 struct dwo_unit *dwo_tu;
6094 void **slot;
6095 const gdb_byte *ptr = info_ptr;
6096 struct comp_unit_head header;
6097 unsigned int length;
6098
6099 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6100
6101 /* Initialize it due to a false compiler warning. */
6102 header.signature = -1;
6103 header.type_cu_offset_in_tu = (cu_offset) -1;
6104
6105 /* We need to read the type's signature in order to build the hash
6106 table, but we don't need anything else just yet. */
6107
6108 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6109 abbrev_section, ptr, section_kind);
6110
6111 length = header.get_length ();
6112
6113 /* Skip dummy type units. */
6114 if (ptr >= info_ptr + length
6115 || peek_abbrev_code (abfd, ptr) == 0
6116 || header.unit_type != DW_UT_type)
6117 {
6118 info_ptr += length;
6119 continue;
6120 }
6121
6122 if (types_htab == NULL)
6123 {
6124 if (dwo_file)
6125 types_htab = allocate_dwo_unit_table ();
6126 else
6127 types_htab = allocate_signatured_type_table ();
6128 }
6129
6130 if (dwo_file)
6131 {
6132 sig_type = NULL;
6133 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6134 struct dwo_unit);
6135 dwo_tu->dwo_file = dwo_file;
6136 dwo_tu->signature = header.signature;
6137 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6138 dwo_tu->section = section;
6139 dwo_tu->sect_off = sect_off;
6140 dwo_tu->length = length;
6141 }
6142 else
6143 {
6144 /* N.B.: type_offset is not usable if this type uses a DWO file.
6145 The real type_offset is in the DWO file. */
6146 dwo_tu = NULL;
6147 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6148 struct signatured_type);
6149 sig_type->signature = header.signature;
6150 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6151 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6152 sig_type->per_cu.is_debug_types = 1;
6153 sig_type->per_cu.section = section;
6154 sig_type->per_cu.sect_off = sect_off;
6155 sig_type->per_cu.length = length;
6156 }
6157
6158 slot = htab_find_slot (types_htab.get (),
6159 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6160 INSERT);
6161 gdb_assert (slot != NULL);
6162 if (*slot != NULL)
6163 {
6164 sect_offset dup_sect_off;
6165
6166 if (dwo_file)
6167 {
6168 const struct dwo_unit *dup_tu
6169 = (const struct dwo_unit *) *slot;
6170
6171 dup_sect_off = dup_tu->sect_off;
6172 }
6173 else
6174 {
6175 const struct signatured_type *dup_tu
6176 = (const struct signatured_type *) *slot;
6177
6178 dup_sect_off = dup_tu->per_cu.sect_off;
6179 }
6180
6181 complaint (_("debug type entry at offset %s is duplicate to"
6182 " the entry at offset %s, signature %s"),
6183 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6184 hex_string (header.signature));
6185 }
6186 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6187
6188 if (dwarf_read_debug > 1)
6189 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6190 sect_offset_str (sect_off),
6191 hex_string (header.signature));
6192
6193 info_ptr += length;
6194 }
6195 }
6196
6197 /* Create the hash table of all entries in the .debug_types
6198 (or .debug_types.dwo) section(s).
6199 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6200 otherwise it is NULL.
6201
6202 The result is a pointer to the hash table or NULL if there are no types.
6203
6204 Note: This function processes DWO files only, not DWP files. */
6205
6206 static void
6207 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6208 struct dwo_file *dwo_file,
6209 gdb::array_view<dwarf2_section_info> type_sections,
6210 htab_up &types_htab)
6211 {
6212 for (dwarf2_section_info &section : type_sections)
6213 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6214 types_htab, rcuh_kind::TYPE);
6215 }
6216
6217 /* Create the hash table of all entries in the .debug_types section,
6218 and initialize all_type_units.
6219 The result is zero if there is an error (e.g. missing .debug_types section),
6220 otherwise non-zero. */
6221
6222 static int
6223 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6224 {
6225 htab_up types_htab;
6226
6227 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6228 &dwarf2_per_objfile->info, types_htab,
6229 rcuh_kind::COMPILE);
6230 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6231 dwarf2_per_objfile->types, types_htab);
6232 if (types_htab == NULL)
6233 {
6234 dwarf2_per_objfile->signatured_types = NULL;
6235 return 0;
6236 }
6237
6238 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6239
6240 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6241 dwarf2_per_objfile->all_type_units.reserve
6242 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6243
6244 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6245 add_signatured_type_cu_to_table,
6246 &dwarf2_per_objfile->all_type_units);
6247
6248 return 1;
6249 }
6250
6251 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6252 If SLOT is non-NULL, it is the entry to use in the hash table.
6253 Otherwise we find one. */
6254
6255 static struct signatured_type *
6256 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6257 void **slot)
6258 {
6259 struct objfile *objfile = dwarf2_per_objfile->objfile;
6260
6261 if (dwarf2_per_objfile->all_type_units.size ()
6262 == dwarf2_per_objfile->all_type_units.capacity ())
6263 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6264
6265 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6266 struct signatured_type);
6267
6268 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6269 sig_type->signature = sig;
6270 sig_type->per_cu.is_debug_types = 1;
6271 if (dwarf2_per_objfile->using_index)
6272 {
6273 sig_type->per_cu.v.quick =
6274 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6275 struct dwarf2_per_cu_quick_data);
6276 }
6277
6278 if (slot == NULL)
6279 {
6280 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6281 sig_type, INSERT);
6282 }
6283 gdb_assert (*slot == NULL);
6284 *slot = sig_type;
6285 /* The rest of sig_type must be filled in by the caller. */
6286 return sig_type;
6287 }
6288
6289 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6290 Fill in SIG_ENTRY with DWO_ENTRY. */
6291
6292 static void
6293 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6294 struct signatured_type *sig_entry,
6295 struct dwo_unit *dwo_entry)
6296 {
6297 /* Make sure we're not clobbering something we don't expect to. */
6298 gdb_assert (! sig_entry->per_cu.queued);
6299 gdb_assert (sig_entry->per_cu.cu == NULL);
6300 if (dwarf2_per_objfile->using_index)
6301 {
6302 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6303 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6304 }
6305 else
6306 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6307 gdb_assert (sig_entry->signature == dwo_entry->signature);
6308 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6309 gdb_assert (sig_entry->type_unit_group == NULL);
6310 gdb_assert (sig_entry->dwo_unit == NULL);
6311
6312 sig_entry->per_cu.section = dwo_entry->section;
6313 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6314 sig_entry->per_cu.length = dwo_entry->length;
6315 sig_entry->per_cu.reading_dwo_directly = 1;
6316 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6317 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6318 sig_entry->dwo_unit = dwo_entry;
6319 }
6320
6321 /* Subroutine of lookup_signatured_type.
6322 If we haven't read the TU yet, create the signatured_type data structure
6323 for a TU to be read in directly from a DWO file, bypassing the stub.
6324 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6325 using .gdb_index, then when reading a CU we want to stay in the DWO file
6326 containing that CU. Otherwise we could end up reading several other DWO
6327 files (due to comdat folding) to process the transitive closure of all the
6328 mentioned TUs, and that can be slow. The current DWO file will have every
6329 type signature that it needs.
6330 We only do this for .gdb_index because in the psymtab case we already have
6331 to read all the DWOs to build the type unit groups. */
6332
6333 static struct signatured_type *
6334 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6335 {
6336 struct dwarf2_per_objfile *dwarf2_per_objfile
6337 = cu->per_cu->dwarf2_per_objfile;
6338 struct dwo_file *dwo_file;
6339 struct dwo_unit find_dwo_entry, *dwo_entry;
6340 struct signatured_type find_sig_entry, *sig_entry;
6341 void **slot;
6342
6343 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6344
6345 /* If TU skeletons have been removed then we may not have read in any
6346 TUs yet. */
6347 if (dwarf2_per_objfile->signatured_types == NULL)
6348 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6349
6350 /* We only ever need to read in one copy of a signatured type.
6351 Use the global signatured_types array to do our own comdat-folding
6352 of types. If this is the first time we're reading this TU, and
6353 the TU has an entry in .gdb_index, replace the recorded data from
6354 .gdb_index with this TU. */
6355
6356 find_sig_entry.signature = sig;
6357 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6358 &find_sig_entry, INSERT);
6359 sig_entry = (struct signatured_type *) *slot;
6360
6361 /* We can get here with the TU already read, *or* in the process of being
6362 read. Don't reassign the global entry to point to this DWO if that's
6363 the case. Also note that if the TU is already being read, it may not
6364 have come from a DWO, the program may be a mix of Fission-compiled
6365 code and non-Fission-compiled code. */
6366
6367 /* Have we already tried to read this TU?
6368 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6369 needn't exist in the global table yet). */
6370 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6371 return sig_entry;
6372
6373 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6374 dwo_unit of the TU itself. */
6375 dwo_file = cu->dwo_unit->dwo_file;
6376
6377 /* Ok, this is the first time we're reading this TU. */
6378 if (dwo_file->tus == NULL)
6379 return NULL;
6380 find_dwo_entry.signature = sig;
6381 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6382 &find_dwo_entry);
6383 if (dwo_entry == NULL)
6384 return NULL;
6385
6386 /* If the global table doesn't have an entry for this TU, add one. */
6387 if (sig_entry == NULL)
6388 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6389
6390 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6391 sig_entry->per_cu.tu_read = 1;
6392 return sig_entry;
6393 }
6394
6395 /* Subroutine of lookup_signatured_type.
6396 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6397 then try the DWP file. If the TU stub (skeleton) has been removed then
6398 it won't be in .gdb_index. */
6399
6400 static struct signatured_type *
6401 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6402 {
6403 struct dwarf2_per_objfile *dwarf2_per_objfile
6404 = cu->per_cu->dwarf2_per_objfile;
6405 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6406 struct dwo_unit *dwo_entry;
6407 struct signatured_type find_sig_entry, *sig_entry;
6408 void **slot;
6409
6410 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6411 gdb_assert (dwp_file != NULL);
6412
6413 /* If TU skeletons have been removed then we may not have read in any
6414 TUs yet. */
6415 if (dwarf2_per_objfile->signatured_types == NULL)
6416 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6417
6418 find_sig_entry.signature = sig;
6419 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6420 &find_sig_entry, INSERT);
6421 sig_entry = (struct signatured_type *) *slot;
6422
6423 /* Have we already tried to read this TU?
6424 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6425 needn't exist in the global table yet). */
6426 if (sig_entry != NULL)
6427 return sig_entry;
6428
6429 if (dwp_file->tus == NULL)
6430 return NULL;
6431 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6432 sig, 1 /* is_debug_types */);
6433 if (dwo_entry == NULL)
6434 return NULL;
6435
6436 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6437 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6438
6439 return sig_entry;
6440 }
6441
6442 /* Lookup a signature based type for DW_FORM_ref_sig8.
6443 Returns NULL if signature SIG is not present in the table.
6444 It is up to the caller to complain about this. */
6445
6446 static struct signatured_type *
6447 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6448 {
6449 struct dwarf2_per_objfile *dwarf2_per_objfile
6450 = cu->per_cu->dwarf2_per_objfile;
6451
6452 if (cu->dwo_unit
6453 && dwarf2_per_objfile->using_index)
6454 {
6455 /* We're in a DWO/DWP file, and we're using .gdb_index.
6456 These cases require special processing. */
6457 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6458 return lookup_dwo_signatured_type (cu, sig);
6459 else
6460 return lookup_dwp_signatured_type (cu, sig);
6461 }
6462 else
6463 {
6464 struct signatured_type find_entry, *entry;
6465
6466 if (dwarf2_per_objfile->signatured_types == NULL)
6467 return NULL;
6468 find_entry.signature = sig;
6469 entry = ((struct signatured_type *)
6470 htab_find (dwarf2_per_objfile->signatured_types.get (),
6471 &find_entry));
6472 return entry;
6473 }
6474 }
6475
6476 /* Return the address base of the compile unit, which, if exists, is stored
6477 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6478 static gdb::optional<ULONGEST>
6479 lookup_addr_base (struct die_info *comp_unit_die)
6480 {
6481 struct attribute *attr;
6482 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6483 if (attr == nullptr)
6484 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6485 if (attr == nullptr)
6486 return gdb::optional<ULONGEST> ();
6487 return DW_UNSND (attr);
6488 }
6489
6490 /* Return range lists base of the compile unit, which, if exists, is stored
6491 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6492 static ULONGEST
6493 lookup_ranges_base (struct die_info *comp_unit_die)
6494 {
6495 struct attribute *attr;
6496 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6497 if (attr == nullptr)
6498 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6499 if (attr == nullptr)
6500 return 0;
6501 return DW_UNSND (attr);
6502 }
6503
6504 /* Low level DIE reading support. */
6505
6506 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6507
6508 static void
6509 init_cu_die_reader (struct die_reader_specs *reader,
6510 struct dwarf2_cu *cu,
6511 struct dwarf2_section_info *section,
6512 struct dwo_file *dwo_file,
6513 struct abbrev_table *abbrev_table)
6514 {
6515 gdb_assert (section->readin && section->buffer != NULL);
6516 reader->abfd = section->get_bfd_owner ();
6517 reader->cu = cu;
6518 reader->dwo_file = dwo_file;
6519 reader->die_section = section;
6520 reader->buffer = section->buffer;
6521 reader->buffer_end = section->buffer + section->size;
6522 reader->abbrev_table = abbrev_table;
6523 }
6524
6525 /* Subroutine of cutu_reader to simplify it.
6526 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6527 There's just a lot of work to do, and cutu_reader is big enough
6528 already.
6529
6530 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6531 from it to the DIE in the DWO. If NULL we are skipping the stub.
6532 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6533 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6534 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6535 STUB_COMP_DIR may be non-NULL.
6536 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6537 are filled in with the info of the DIE from the DWO file.
6538 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6539 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6540 kept around for at least as long as *RESULT_READER.
6541
6542 The result is non-zero if a valid (non-dummy) DIE was found. */
6543
6544 static int
6545 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6546 struct dwo_unit *dwo_unit,
6547 struct die_info *stub_comp_unit_die,
6548 const char *stub_comp_dir,
6549 struct die_reader_specs *result_reader,
6550 const gdb_byte **result_info_ptr,
6551 struct die_info **result_comp_unit_die,
6552 abbrev_table_up *result_dwo_abbrev_table)
6553 {
6554 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6555 struct objfile *objfile = dwarf2_per_objfile->objfile;
6556 struct dwarf2_cu *cu = this_cu->cu;
6557 bfd *abfd;
6558 const gdb_byte *begin_info_ptr, *info_ptr;
6559 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6560 int i,num_extra_attrs;
6561 struct dwarf2_section_info *dwo_abbrev_section;
6562 struct die_info *comp_unit_die;
6563
6564 /* At most one of these may be provided. */
6565 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6566
6567 /* These attributes aren't processed until later:
6568 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6569 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6570 referenced later. However, these attributes are found in the stub
6571 which we won't have later. In order to not impose this complication
6572 on the rest of the code, we read them here and copy them to the
6573 DWO CU/TU die. */
6574
6575 stmt_list = NULL;
6576 low_pc = NULL;
6577 high_pc = NULL;
6578 ranges = NULL;
6579 comp_dir = NULL;
6580
6581 if (stub_comp_unit_die != NULL)
6582 {
6583 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6584 DWO file. */
6585 if (! this_cu->is_debug_types)
6586 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6587 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6588 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6589 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6590 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6591
6592 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6593
6594 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6595 here (if needed). We need the value before we can process
6596 DW_AT_ranges. */
6597 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6598 }
6599 else if (stub_comp_dir != NULL)
6600 {
6601 /* Reconstruct the comp_dir attribute to simplify the code below. */
6602 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6603 comp_dir->name = DW_AT_comp_dir;
6604 comp_dir->form = DW_FORM_string;
6605 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6606 DW_STRING (comp_dir) = stub_comp_dir;
6607 }
6608
6609 /* Set up for reading the DWO CU/TU. */
6610 cu->dwo_unit = dwo_unit;
6611 dwarf2_section_info *section = dwo_unit->section;
6612 section->read (objfile);
6613 abfd = section->get_bfd_owner ();
6614 begin_info_ptr = info_ptr = (section->buffer
6615 + to_underlying (dwo_unit->sect_off));
6616 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6617
6618 if (this_cu->is_debug_types)
6619 {
6620 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6621
6622 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6623 &cu->header, section,
6624 dwo_abbrev_section,
6625 info_ptr, rcuh_kind::TYPE);
6626 /* This is not an assert because it can be caused by bad debug info. */
6627 if (sig_type->signature != cu->header.signature)
6628 {
6629 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6630 " TU at offset %s [in module %s]"),
6631 hex_string (sig_type->signature),
6632 hex_string (cu->header.signature),
6633 sect_offset_str (dwo_unit->sect_off),
6634 bfd_get_filename (abfd));
6635 }
6636 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6637 /* For DWOs coming from DWP files, we don't know the CU length
6638 nor the type's offset in the TU until now. */
6639 dwo_unit->length = cu->header.get_length ();
6640 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6641
6642 /* Establish the type offset that can be used to lookup the type.
6643 For DWO files, we don't know it until now. */
6644 sig_type->type_offset_in_section
6645 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6646 }
6647 else
6648 {
6649 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6650 &cu->header, section,
6651 dwo_abbrev_section,
6652 info_ptr, rcuh_kind::COMPILE);
6653 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6654 /* For DWOs coming from DWP files, we don't know the CU length
6655 until now. */
6656 dwo_unit->length = cu->header.get_length ();
6657 }
6658
6659 *result_dwo_abbrev_table
6660 = abbrev_table::read (objfile, dwo_abbrev_section,
6661 cu->header.abbrev_sect_off);
6662 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6663 result_dwo_abbrev_table->get ());
6664
6665 /* Read in the die, but leave space to copy over the attributes
6666 from the stub. This has the benefit of simplifying the rest of
6667 the code - all the work to maintain the illusion of a single
6668 DW_TAG_{compile,type}_unit DIE is done here. */
6669 num_extra_attrs = ((stmt_list != NULL)
6670 + (low_pc != NULL)
6671 + (high_pc != NULL)
6672 + (ranges != NULL)
6673 + (comp_dir != NULL));
6674 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6675 num_extra_attrs);
6676
6677 /* Copy over the attributes from the stub to the DIE we just read in. */
6678 comp_unit_die = *result_comp_unit_die;
6679 i = comp_unit_die->num_attrs;
6680 if (stmt_list != NULL)
6681 comp_unit_die->attrs[i++] = *stmt_list;
6682 if (low_pc != NULL)
6683 comp_unit_die->attrs[i++] = *low_pc;
6684 if (high_pc != NULL)
6685 comp_unit_die->attrs[i++] = *high_pc;
6686 if (ranges != NULL)
6687 comp_unit_die->attrs[i++] = *ranges;
6688 if (comp_dir != NULL)
6689 comp_unit_die->attrs[i++] = *comp_dir;
6690 comp_unit_die->num_attrs += num_extra_attrs;
6691
6692 if (dwarf_die_debug)
6693 {
6694 fprintf_unfiltered (gdb_stdlog,
6695 "Read die from %s@0x%x of %s:\n",
6696 section->get_name (),
6697 (unsigned) (begin_info_ptr - section->buffer),
6698 bfd_get_filename (abfd));
6699 dump_die (comp_unit_die, dwarf_die_debug);
6700 }
6701
6702 /* Skip dummy compilation units. */
6703 if (info_ptr >= begin_info_ptr + dwo_unit->length
6704 || peek_abbrev_code (abfd, info_ptr) == 0)
6705 return 0;
6706
6707 *result_info_ptr = info_ptr;
6708 return 1;
6709 }
6710
6711 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6712 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6713 signature is part of the header. */
6714 static gdb::optional<ULONGEST>
6715 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6716 {
6717 if (cu->header.version >= 5)
6718 return cu->header.signature;
6719 struct attribute *attr;
6720 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6721 if (attr == nullptr)
6722 return gdb::optional<ULONGEST> ();
6723 return DW_UNSND (attr);
6724 }
6725
6726 /* Subroutine of cutu_reader to simplify it.
6727 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6728 Returns NULL if the specified DWO unit cannot be found. */
6729
6730 static struct dwo_unit *
6731 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6732 struct die_info *comp_unit_die,
6733 const char *dwo_name)
6734 {
6735 struct dwarf2_cu *cu = this_cu->cu;
6736 struct dwo_unit *dwo_unit;
6737 const char *comp_dir;
6738
6739 gdb_assert (cu != NULL);
6740
6741 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6742 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6743 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6744
6745 if (this_cu->is_debug_types)
6746 {
6747 struct signatured_type *sig_type;
6748
6749 /* Since this_cu is the first member of struct signatured_type,
6750 we can go from a pointer to one to a pointer to the other. */
6751 sig_type = (struct signatured_type *) this_cu;
6752 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6753 }
6754 else
6755 {
6756 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6757 if (!signature.has_value ())
6758 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6759 " [in module %s]"),
6760 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6761 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6762 *signature);
6763 }
6764
6765 return dwo_unit;
6766 }
6767
6768 /* Subroutine of cutu_reader to simplify it.
6769 See it for a description of the parameters.
6770 Read a TU directly from a DWO file, bypassing the stub. */
6771
6772 void
6773 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6774 int use_existing_cu)
6775 {
6776 struct signatured_type *sig_type;
6777
6778 /* Verify we can do the following downcast, and that we have the
6779 data we need. */
6780 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6781 sig_type = (struct signatured_type *) this_cu;
6782 gdb_assert (sig_type->dwo_unit != NULL);
6783
6784 if (use_existing_cu && this_cu->cu != NULL)
6785 {
6786 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6787 /* There's no need to do the rereading_dwo_cu handling that
6788 cutu_reader does since we don't read the stub. */
6789 }
6790 else
6791 {
6792 /* If !use_existing_cu, this_cu->cu must be NULL. */
6793 gdb_assert (this_cu->cu == NULL);
6794 m_new_cu.reset (new dwarf2_cu (this_cu));
6795 }
6796
6797 /* A future optimization, if needed, would be to use an existing
6798 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6799 could share abbrev tables. */
6800
6801 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6802 NULL /* stub_comp_unit_die */,
6803 sig_type->dwo_unit->dwo_file->comp_dir,
6804 this, &info_ptr,
6805 &comp_unit_die,
6806 &m_dwo_abbrev_table) == 0)
6807 {
6808 /* Dummy die. */
6809 dummy_p = true;
6810 }
6811 }
6812
6813 /* Initialize a CU (or TU) and read its DIEs.
6814 If the CU defers to a DWO file, read the DWO file as well.
6815
6816 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6817 Otherwise the table specified in the comp unit header is read in and used.
6818 This is an optimization for when we already have the abbrev table.
6819
6820 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6821 Otherwise, a new CU is allocated with xmalloc. */
6822
6823 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6824 struct abbrev_table *abbrev_table,
6825 int use_existing_cu,
6826 bool skip_partial)
6827 : die_reader_specs {},
6828 m_this_cu (this_cu)
6829 {
6830 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6831 struct objfile *objfile = dwarf2_per_objfile->objfile;
6832 struct dwarf2_section_info *section = this_cu->section;
6833 bfd *abfd = section->get_bfd_owner ();
6834 struct dwarf2_cu *cu;
6835 const gdb_byte *begin_info_ptr;
6836 struct signatured_type *sig_type = NULL;
6837 struct dwarf2_section_info *abbrev_section;
6838 /* Non-zero if CU currently points to a DWO file and we need to
6839 reread it. When this happens we need to reread the skeleton die
6840 before we can reread the DWO file (this only applies to CUs, not TUs). */
6841 int rereading_dwo_cu = 0;
6842
6843 if (dwarf_die_debug)
6844 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6845 this_cu->is_debug_types ? "type" : "comp",
6846 sect_offset_str (this_cu->sect_off));
6847
6848 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6849 file (instead of going through the stub), short-circuit all of this. */
6850 if (this_cu->reading_dwo_directly)
6851 {
6852 /* Narrow down the scope of possibilities to have to understand. */
6853 gdb_assert (this_cu->is_debug_types);
6854 gdb_assert (abbrev_table == NULL);
6855 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6856 return;
6857 }
6858
6859 /* This is cheap if the section is already read in. */
6860 section->read (objfile);
6861
6862 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6863
6864 abbrev_section = get_abbrev_section_for_cu (this_cu);
6865
6866 if (use_existing_cu && this_cu->cu != NULL)
6867 {
6868 cu = this_cu->cu;
6869 /* If this CU is from a DWO file we need to start over, we need to
6870 refetch the attributes from the skeleton CU.
6871 This could be optimized by retrieving those attributes from when we
6872 were here the first time: the previous comp_unit_die was stored in
6873 comp_unit_obstack. But there's no data yet that we need this
6874 optimization. */
6875 if (cu->dwo_unit != NULL)
6876 rereading_dwo_cu = 1;
6877 }
6878 else
6879 {
6880 /* If !use_existing_cu, this_cu->cu must be NULL. */
6881 gdb_assert (this_cu->cu == NULL);
6882 m_new_cu.reset (new dwarf2_cu (this_cu));
6883 cu = m_new_cu.get ();
6884 }
6885
6886 /* Get the header. */
6887 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6888 {
6889 /* We already have the header, there's no need to read it in again. */
6890 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6891 }
6892 else
6893 {
6894 if (this_cu->is_debug_types)
6895 {
6896 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6897 &cu->header, section,
6898 abbrev_section, info_ptr,
6899 rcuh_kind::TYPE);
6900
6901 /* Since per_cu is the first member of struct signatured_type,
6902 we can go from a pointer to one to a pointer to the other. */
6903 sig_type = (struct signatured_type *) this_cu;
6904 gdb_assert (sig_type->signature == cu->header.signature);
6905 gdb_assert (sig_type->type_offset_in_tu
6906 == cu->header.type_cu_offset_in_tu);
6907 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6908
6909 /* LENGTH has not been set yet for type units if we're
6910 using .gdb_index. */
6911 this_cu->length = cu->header.get_length ();
6912
6913 /* Establish the type offset that can be used to lookup the type. */
6914 sig_type->type_offset_in_section =
6915 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6916
6917 this_cu->dwarf_version = cu->header.version;
6918 }
6919 else
6920 {
6921 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6922 &cu->header, section,
6923 abbrev_section,
6924 info_ptr,
6925 rcuh_kind::COMPILE);
6926
6927 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6928 gdb_assert (this_cu->length == cu->header.get_length ());
6929 this_cu->dwarf_version = cu->header.version;
6930 }
6931 }
6932
6933 /* Skip dummy compilation units. */
6934 if (info_ptr >= begin_info_ptr + this_cu->length
6935 || peek_abbrev_code (abfd, info_ptr) == 0)
6936 {
6937 dummy_p = true;
6938 return;
6939 }
6940
6941 /* If we don't have them yet, read the abbrevs for this compilation unit.
6942 And if we need to read them now, make sure they're freed when we're
6943 done. */
6944 if (abbrev_table != NULL)
6945 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6946 else
6947 {
6948 m_abbrev_table_holder
6949 = abbrev_table::read (objfile, abbrev_section,
6950 cu->header.abbrev_sect_off);
6951 abbrev_table = m_abbrev_table_holder.get ();
6952 }
6953
6954 /* Read the top level CU/TU die. */
6955 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6956 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6957
6958 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6959 {
6960 dummy_p = true;
6961 return;
6962 }
6963
6964 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6965 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6966 table from the DWO file and pass the ownership over to us. It will be
6967 referenced from READER, so we must make sure to free it after we're done
6968 with READER.
6969
6970 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6971 DWO CU, that this test will fail (the attribute will not be present). */
6972 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6973 if (dwo_name != nullptr)
6974 {
6975 struct dwo_unit *dwo_unit;
6976 struct die_info *dwo_comp_unit_die;
6977
6978 if (comp_unit_die->has_children)
6979 {
6980 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6981 " has children (offset %s) [in module %s]"),
6982 sect_offset_str (this_cu->sect_off),
6983 bfd_get_filename (abfd));
6984 }
6985 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6986 if (dwo_unit != NULL)
6987 {
6988 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6989 comp_unit_die, NULL,
6990 this, &info_ptr,
6991 &dwo_comp_unit_die,
6992 &m_dwo_abbrev_table) == 0)
6993 {
6994 /* Dummy die. */
6995 dummy_p = true;
6996 return;
6997 }
6998 comp_unit_die = dwo_comp_unit_die;
6999 }
7000 else
7001 {
7002 /* Yikes, we couldn't find the rest of the DIE, we only have
7003 the stub. A complaint has already been logged. There's
7004 not much more we can do except pass on the stub DIE to
7005 die_reader_func. We don't want to throw an error on bad
7006 debug info. */
7007 }
7008 }
7009 }
7010
7011 void
7012 cutu_reader::keep ()
7013 {
7014 /* Done, clean up. */
7015 gdb_assert (!dummy_p);
7016 if (m_new_cu != NULL)
7017 {
7018 struct dwarf2_per_objfile *dwarf2_per_objfile
7019 = m_this_cu->dwarf2_per_objfile;
7020 /* Link this CU into read_in_chain. */
7021 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7022 dwarf2_per_objfile->read_in_chain = m_this_cu;
7023 /* The chain owns it now. */
7024 m_new_cu.release ();
7025 }
7026 }
7027
7028 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7029 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7030 assumed to have already done the lookup to find the DWO file).
7031
7032 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7033 THIS_CU->is_debug_types, but nothing else.
7034
7035 We fill in THIS_CU->length.
7036
7037 THIS_CU->cu is always freed when done.
7038 This is done in order to not leave THIS_CU->cu in a state where we have
7039 to care whether it refers to the "main" CU or the DWO CU.
7040
7041 When parent_cu is passed, it is used to provide a default value for
7042 str_offsets_base and addr_base from the parent. */
7043
7044 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7045 struct dwarf2_cu *parent_cu,
7046 struct dwo_file *dwo_file)
7047 : die_reader_specs {},
7048 m_this_cu (this_cu)
7049 {
7050 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7051 struct objfile *objfile = dwarf2_per_objfile->objfile;
7052 struct dwarf2_section_info *section = this_cu->section;
7053 bfd *abfd = section->get_bfd_owner ();
7054 struct dwarf2_section_info *abbrev_section;
7055 const gdb_byte *begin_info_ptr, *info_ptr;
7056
7057 if (dwarf_die_debug)
7058 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7059 this_cu->is_debug_types ? "type" : "comp",
7060 sect_offset_str (this_cu->sect_off));
7061
7062 gdb_assert (this_cu->cu == NULL);
7063
7064 abbrev_section = (dwo_file != NULL
7065 ? &dwo_file->sections.abbrev
7066 : get_abbrev_section_for_cu (this_cu));
7067
7068 /* This is cheap if the section is already read in. */
7069 section->read (objfile);
7070
7071 m_new_cu.reset (new dwarf2_cu (this_cu));
7072
7073 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7074 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7075 &m_new_cu->header, section,
7076 abbrev_section, info_ptr,
7077 (this_cu->is_debug_types
7078 ? rcuh_kind::TYPE
7079 : rcuh_kind::COMPILE));
7080
7081 if (parent_cu != nullptr)
7082 {
7083 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7084 m_new_cu->addr_base = parent_cu->addr_base;
7085 }
7086 this_cu->length = m_new_cu->header.get_length ();
7087
7088 /* Skip dummy compilation units. */
7089 if (info_ptr >= begin_info_ptr + this_cu->length
7090 || peek_abbrev_code (abfd, info_ptr) == 0)
7091 {
7092 dummy_p = true;
7093 return;
7094 }
7095
7096 m_abbrev_table_holder
7097 = abbrev_table::read (objfile, abbrev_section,
7098 m_new_cu->header.abbrev_sect_off);
7099
7100 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7101 m_abbrev_table_holder.get ());
7102 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7103 }
7104
7105 \f
7106 /* Type Unit Groups.
7107
7108 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7109 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7110 so that all types coming from the same compilation (.o file) are grouped
7111 together. A future step could be to put the types in the same symtab as
7112 the CU the types ultimately came from. */
7113
7114 static hashval_t
7115 hash_type_unit_group (const void *item)
7116 {
7117 const struct type_unit_group *tu_group
7118 = (const struct type_unit_group *) item;
7119
7120 return hash_stmt_list_entry (&tu_group->hash);
7121 }
7122
7123 static int
7124 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7125 {
7126 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7127 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7128
7129 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7130 }
7131
7132 /* Allocate a hash table for type unit groups. */
7133
7134 static htab_up
7135 allocate_type_unit_groups_table ()
7136 {
7137 return htab_up (htab_create_alloc (3,
7138 hash_type_unit_group,
7139 eq_type_unit_group,
7140 NULL, xcalloc, xfree));
7141 }
7142
7143 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7144 partial symtabs. We combine several TUs per psymtab to not let the size
7145 of any one psymtab grow too big. */
7146 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7147 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7148
7149 /* Helper routine for get_type_unit_group.
7150 Create the type_unit_group object used to hold one or more TUs. */
7151
7152 static struct type_unit_group *
7153 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7154 {
7155 struct dwarf2_per_objfile *dwarf2_per_objfile
7156 = cu->per_cu->dwarf2_per_objfile;
7157 struct objfile *objfile = dwarf2_per_objfile->objfile;
7158 struct dwarf2_per_cu_data *per_cu;
7159 struct type_unit_group *tu_group;
7160
7161 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7162 struct type_unit_group);
7163 per_cu = &tu_group->per_cu;
7164 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7165
7166 if (dwarf2_per_objfile->using_index)
7167 {
7168 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7169 struct dwarf2_per_cu_quick_data);
7170 }
7171 else
7172 {
7173 unsigned int line_offset = to_underlying (line_offset_struct);
7174 dwarf2_psymtab *pst;
7175 std::string name;
7176
7177 /* Give the symtab a useful name for debug purposes. */
7178 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7179 name = string_printf ("<type_units_%d>",
7180 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7181 else
7182 name = string_printf ("<type_units_at_0x%x>", line_offset);
7183
7184 pst = create_partial_symtab (per_cu, name.c_str ());
7185 pst->anonymous = true;
7186 }
7187
7188 tu_group->hash.dwo_unit = cu->dwo_unit;
7189 tu_group->hash.line_sect_off = line_offset_struct;
7190
7191 return tu_group;
7192 }
7193
7194 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7195 STMT_LIST is a DW_AT_stmt_list attribute. */
7196
7197 static struct type_unit_group *
7198 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7199 {
7200 struct dwarf2_per_objfile *dwarf2_per_objfile
7201 = cu->per_cu->dwarf2_per_objfile;
7202 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7203 struct type_unit_group *tu_group;
7204 void **slot;
7205 unsigned int line_offset;
7206 struct type_unit_group type_unit_group_for_lookup;
7207
7208 if (dwarf2_per_objfile->type_unit_groups == NULL)
7209 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7210
7211 /* Do we need to create a new group, or can we use an existing one? */
7212
7213 if (stmt_list)
7214 {
7215 line_offset = DW_UNSND (stmt_list);
7216 ++tu_stats->nr_symtab_sharers;
7217 }
7218 else
7219 {
7220 /* Ugh, no stmt_list. Rare, but we have to handle it.
7221 We can do various things here like create one group per TU or
7222 spread them over multiple groups to split up the expansion work.
7223 To avoid worst case scenarios (too many groups or too large groups)
7224 we, umm, group them in bunches. */
7225 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7226 | (tu_stats->nr_stmt_less_type_units
7227 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7228 ++tu_stats->nr_stmt_less_type_units;
7229 }
7230
7231 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7232 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7233 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7234 &type_unit_group_for_lookup, INSERT);
7235 if (*slot != NULL)
7236 {
7237 tu_group = (struct type_unit_group *) *slot;
7238 gdb_assert (tu_group != NULL);
7239 }
7240 else
7241 {
7242 sect_offset line_offset_struct = (sect_offset) line_offset;
7243 tu_group = create_type_unit_group (cu, line_offset_struct);
7244 *slot = tu_group;
7245 ++tu_stats->nr_symtabs;
7246 }
7247
7248 return tu_group;
7249 }
7250 \f
7251 /* Partial symbol tables. */
7252
7253 /* Create a psymtab named NAME and assign it to PER_CU.
7254
7255 The caller must fill in the following details:
7256 dirname, textlow, texthigh. */
7257
7258 static dwarf2_psymtab *
7259 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7260 {
7261 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7262 dwarf2_psymtab *pst;
7263
7264 pst = new dwarf2_psymtab (name, objfile, 0);
7265
7266 pst->psymtabs_addrmap_supported = true;
7267
7268 /* This is the glue that links PST into GDB's symbol API. */
7269 pst->per_cu_data = per_cu;
7270 per_cu->v.psymtab = pst;
7271
7272 return pst;
7273 }
7274
7275 /* DIE reader function for process_psymtab_comp_unit. */
7276
7277 static void
7278 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7279 const gdb_byte *info_ptr,
7280 struct die_info *comp_unit_die,
7281 enum language pretend_language)
7282 {
7283 struct dwarf2_cu *cu = reader->cu;
7284 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7285 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7286 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7287 CORE_ADDR baseaddr;
7288 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7289 dwarf2_psymtab *pst;
7290 enum pc_bounds_kind cu_bounds_kind;
7291 const char *filename;
7292
7293 gdb_assert (! per_cu->is_debug_types);
7294
7295 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7296
7297 /* Allocate a new partial symbol table structure. */
7298 gdb::unique_xmalloc_ptr<char> debug_filename;
7299 static const char artificial[] = "<artificial>";
7300 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7301 if (filename == NULL)
7302 filename = "";
7303 else if (strcmp (filename, artificial) == 0)
7304 {
7305 debug_filename.reset (concat (artificial, "@",
7306 sect_offset_str (per_cu->sect_off),
7307 (char *) NULL));
7308 filename = debug_filename.get ();
7309 }
7310
7311 pst = create_partial_symtab (per_cu, filename);
7312
7313 /* This must be done before calling dwarf2_build_include_psymtabs. */
7314 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7315
7316 baseaddr = objfile->text_section_offset ();
7317
7318 dwarf2_find_base_address (comp_unit_die, cu);
7319
7320 /* Possibly set the default values of LOWPC and HIGHPC from
7321 `DW_AT_ranges'. */
7322 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7323 &best_highpc, cu, pst);
7324 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7325 {
7326 CORE_ADDR low
7327 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7328 - baseaddr);
7329 CORE_ADDR high
7330 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7331 - baseaddr - 1);
7332 /* Store the contiguous range if it is not empty; it can be
7333 empty for CUs with no code. */
7334 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7335 low, high, pst);
7336 }
7337
7338 /* Check if comp unit has_children.
7339 If so, read the rest of the partial symbols from this comp unit.
7340 If not, there's no more debug_info for this comp unit. */
7341 if (comp_unit_die->has_children)
7342 {
7343 struct partial_die_info *first_die;
7344 CORE_ADDR lowpc, highpc;
7345
7346 lowpc = ((CORE_ADDR) -1);
7347 highpc = ((CORE_ADDR) 0);
7348
7349 first_die = load_partial_dies (reader, info_ptr, 1);
7350
7351 scan_partial_symbols (first_die, &lowpc, &highpc,
7352 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7353
7354 /* If we didn't find a lowpc, set it to highpc to avoid
7355 complaints from `maint check'. */
7356 if (lowpc == ((CORE_ADDR) -1))
7357 lowpc = highpc;
7358
7359 /* If the compilation unit didn't have an explicit address range,
7360 then use the information extracted from its child dies. */
7361 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7362 {
7363 best_lowpc = lowpc;
7364 best_highpc = highpc;
7365 }
7366 }
7367 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7368 best_lowpc + baseaddr)
7369 - baseaddr);
7370 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7371 best_highpc + baseaddr)
7372 - baseaddr);
7373
7374 end_psymtab_common (objfile, pst);
7375
7376 if (!cu->per_cu->imported_symtabs_empty ())
7377 {
7378 int i;
7379 int len = cu->per_cu->imported_symtabs_size ();
7380
7381 /* Fill in 'dependencies' here; we fill in 'users' in a
7382 post-pass. */
7383 pst->number_of_dependencies = len;
7384 pst->dependencies
7385 = objfile->partial_symtabs->allocate_dependencies (len);
7386 for (i = 0; i < len; ++i)
7387 {
7388 pst->dependencies[i]
7389 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7390 }
7391
7392 cu->per_cu->imported_symtabs_free ();
7393 }
7394
7395 /* Get the list of files included in the current compilation unit,
7396 and build a psymtab for each of them. */
7397 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7398
7399 if (dwarf_read_debug)
7400 fprintf_unfiltered (gdb_stdlog,
7401 "Psymtab for %s unit @%s: %s - %s"
7402 ", %d global, %d static syms\n",
7403 per_cu->is_debug_types ? "type" : "comp",
7404 sect_offset_str (per_cu->sect_off),
7405 paddress (gdbarch, pst->text_low (objfile)),
7406 paddress (gdbarch, pst->text_high (objfile)),
7407 pst->n_global_syms, pst->n_static_syms);
7408 }
7409
7410 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7411 Process compilation unit THIS_CU for a psymtab. */
7412
7413 static void
7414 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7415 bool want_partial_unit,
7416 enum language pretend_language)
7417 {
7418 /* If this compilation unit was already read in, free the
7419 cached copy in order to read it in again. This is
7420 necessary because we skipped some symbols when we first
7421 read in the compilation unit (see load_partial_dies).
7422 This problem could be avoided, but the benefit is unclear. */
7423 if (this_cu->cu != NULL)
7424 free_one_cached_comp_unit (this_cu);
7425
7426 cutu_reader reader (this_cu, NULL, 0, false);
7427
7428 if (reader.dummy_p)
7429 {
7430 /* Nothing. */
7431 }
7432 else if (this_cu->is_debug_types)
7433 build_type_psymtabs_reader (&reader, reader.info_ptr,
7434 reader.comp_unit_die);
7435 else if (want_partial_unit
7436 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7437 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7438 reader.comp_unit_die,
7439 pretend_language);
7440
7441 /* Age out any secondary CUs. */
7442 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7443 }
7444
7445 /* Reader function for build_type_psymtabs. */
7446
7447 static void
7448 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7449 const gdb_byte *info_ptr,
7450 struct die_info *type_unit_die)
7451 {
7452 struct dwarf2_per_objfile *dwarf2_per_objfile
7453 = reader->cu->per_cu->dwarf2_per_objfile;
7454 struct objfile *objfile = dwarf2_per_objfile->objfile;
7455 struct dwarf2_cu *cu = reader->cu;
7456 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7457 struct signatured_type *sig_type;
7458 struct type_unit_group *tu_group;
7459 struct attribute *attr;
7460 struct partial_die_info *first_die;
7461 CORE_ADDR lowpc, highpc;
7462 dwarf2_psymtab *pst;
7463
7464 gdb_assert (per_cu->is_debug_types);
7465 sig_type = (struct signatured_type *) per_cu;
7466
7467 if (! type_unit_die->has_children)
7468 return;
7469
7470 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7471 tu_group = get_type_unit_group (cu, attr);
7472
7473 if (tu_group->tus == nullptr)
7474 tu_group->tus = new std::vector<signatured_type *>;
7475 tu_group->tus->push_back (sig_type);
7476
7477 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7478 pst = create_partial_symtab (per_cu, "");
7479 pst->anonymous = true;
7480
7481 first_die = load_partial_dies (reader, info_ptr, 1);
7482
7483 lowpc = (CORE_ADDR) -1;
7484 highpc = (CORE_ADDR) 0;
7485 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7486
7487 end_psymtab_common (objfile, pst);
7488 }
7489
7490 /* Struct used to sort TUs by their abbreviation table offset. */
7491
7492 struct tu_abbrev_offset
7493 {
7494 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7495 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7496 {}
7497
7498 signatured_type *sig_type;
7499 sect_offset abbrev_offset;
7500 };
7501
7502 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7503
7504 static bool
7505 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7506 const struct tu_abbrev_offset &b)
7507 {
7508 return a.abbrev_offset < b.abbrev_offset;
7509 }
7510
7511 /* Efficiently read all the type units.
7512 This does the bulk of the work for build_type_psymtabs.
7513
7514 The efficiency is because we sort TUs by the abbrev table they use and
7515 only read each abbrev table once. In one program there are 200K TUs
7516 sharing 8K abbrev tables.
7517
7518 The main purpose of this function is to support building the
7519 dwarf2_per_objfile->type_unit_groups table.
7520 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7521 can collapse the search space by grouping them by stmt_list.
7522 The savings can be significant, in the same program from above the 200K TUs
7523 share 8K stmt_list tables.
7524
7525 FUNC is expected to call get_type_unit_group, which will create the
7526 struct type_unit_group if necessary and add it to
7527 dwarf2_per_objfile->type_unit_groups. */
7528
7529 static void
7530 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7531 {
7532 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7533 abbrev_table_up abbrev_table;
7534 sect_offset abbrev_offset;
7535
7536 /* It's up to the caller to not call us multiple times. */
7537 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7538
7539 if (dwarf2_per_objfile->all_type_units.empty ())
7540 return;
7541
7542 /* TUs typically share abbrev tables, and there can be way more TUs than
7543 abbrev tables. Sort by abbrev table to reduce the number of times we
7544 read each abbrev table in.
7545 Alternatives are to punt or to maintain a cache of abbrev tables.
7546 This is simpler and efficient enough for now.
7547
7548 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7549 symtab to use). Typically TUs with the same abbrev offset have the same
7550 stmt_list value too so in practice this should work well.
7551
7552 The basic algorithm here is:
7553
7554 sort TUs by abbrev table
7555 for each TU with same abbrev table:
7556 read abbrev table if first user
7557 read TU top level DIE
7558 [IWBN if DWO skeletons had DW_AT_stmt_list]
7559 call FUNC */
7560
7561 if (dwarf_read_debug)
7562 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7563
7564 /* Sort in a separate table to maintain the order of all_type_units
7565 for .gdb_index: TU indices directly index all_type_units. */
7566 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7567 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7568
7569 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7570 sorted_by_abbrev.emplace_back
7571 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7572 sig_type->per_cu.section,
7573 sig_type->per_cu.sect_off));
7574
7575 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7576 sort_tu_by_abbrev_offset);
7577
7578 abbrev_offset = (sect_offset) ~(unsigned) 0;
7579
7580 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7581 {
7582 /* Switch to the next abbrev table if necessary. */
7583 if (abbrev_table == NULL
7584 || tu.abbrev_offset != abbrev_offset)
7585 {
7586 abbrev_offset = tu.abbrev_offset;
7587 abbrev_table =
7588 abbrev_table::read (dwarf2_per_objfile->objfile,
7589 &dwarf2_per_objfile->abbrev,
7590 abbrev_offset);
7591 ++tu_stats->nr_uniq_abbrev_tables;
7592 }
7593
7594 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7595 0, false);
7596 if (!reader.dummy_p)
7597 build_type_psymtabs_reader (&reader, reader.info_ptr,
7598 reader.comp_unit_die);
7599 }
7600 }
7601
7602 /* Print collected type unit statistics. */
7603
7604 static void
7605 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7606 {
7607 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7608
7609 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7610 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7611 dwarf2_per_objfile->all_type_units.size ());
7612 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7613 tu_stats->nr_uniq_abbrev_tables);
7614 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7615 tu_stats->nr_symtabs);
7616 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7617 tu_stats->nr_symtab_sharers);
7618 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7619 tu_stats->nr_stmt_less_type_units);
7620 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7621 tu_stats->nr_all_type_units_reallocs);
7622 }
7623
7624 /* Traversal function for build_type_psymtabs. */
7625
7626 static int
7627 build_type_psymtab_dependencies (void **slot, void *info)
7628 {
7629 struct dwarf2_per_objfile *dwarf2_per_objfile
7630 = (struct dwarf2_per_objfile *) info;
7631 struct objfile *objfile = dwarf2_per_objfile->objfile;
7632 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7633 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7634 dwarf2_psymtab *pst = per_cu->v.psymtab;
7635 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7636 int i;
7637
7638 gdb_assert (len > 0);
7639 gdb_assert (per_cu->type_unit_group_p ());
7640
7641 pst->number_of_dependencies = len;
7642 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7643 for (i = 0; i < len; ++i)
7644 {
7645 struct signatured_type *iter = tu_group->tus->at (i);
7646 gdb_assert (iter->per_cu.is_debug_types);
7647 pst->dependencies[i] = iter->per_cu.v.psymtab;
7648 iter->type_unit_group = tu_group;
7649 }
7650
7651 delete tu_group->tus;
7652 tu_group->tus = nullptr;
7653
7654 return 1;
7655 }
7656
7657 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7658 Build partial symbol tables for the .debug_types comp-units. */
7659
7660 static void
7661 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7662 {
7663 if (! create_all_type_units (dwarf2_per_objfile))
7664 return;
7665
7666 build_type_psymtabs_1 (dwarf2_per_objfile);
7667 }
7668
7669 /* Traversal function for process_skeletonless_type_unit.
7670 Read a TU in a DWO file and build partial symbols for it. */
7671
7672 static int
7673 process_skeletonless_type_unit (void **slot, void *info)
7674 {
7675 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7676 struct dwarf2_per_objfile *dwarf2_per_objfile
7677 = (struct dwarf2_per_objfile *) info;
7678 struct signatured_type find_entry, *entry;
7679
7680 /* If this TU doesn't exist in the global table, add it and read it in. */
7681
7682 if (dwarf2_per_objfile->signatured_types == NULL)
7683 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7684
7685 find_entry.signature = dwo_unit->signature;
7686 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7687 &find_entry, INSERT);
7688 /* If we've already seen this type there's nothing to do. What's happening
7689 is we're doing our own version of comdat-folding here. */
7690 if (*slot != NULL)
7691 return 1;
7692
7693 /* This does the job that create_all_type_units would have done for
7694 this TU. */
7695 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7696 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7697 *slot = entry;
7698
7699 /* This does the job that build_type_psymtabs_1 would have done. */
7700 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7701 if (!reader.dummy_p)
7702 build_type_psymtabs_reader (&reader, reader.info_ptr,
7703 reader.comp_unit_die);
7704
7705 return 1;
7706 }
7707
7708 /* Traversal function for process_skeletonless_type_units. */
7709
7710 static int
7711 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7712 {
7713 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7714
7715 if (dwo_file->tus != NULL)
7716 htab_traverse_noresize (dwo_file->tus.get (),
7717 process_skeletonless_type_unit, info);
7718
7719 return 1;
7720 }
7721
7722 /* Scan all TUs of DWO files, verifying we've processed them.
7723 This is needed in case a TU was emitted without its skeleton.
7724 Note: This can't be done until we know what all the DWO files are. */
7725
7726 static void
7727 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7728 {
7729 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7730 if (get_dwp_file (dwarf2_per_objfile) == NULL
7731 && dwarf2_per_objfile->dwo_files != NULL)
7732 {
7733 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7734 process_dwo_file_for_skeletonless_type_units,
7735 dwarf2_per_objfile);
7736 }
7737 }
7738
7739 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7740
7741 static void
7742 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7743 {
7744 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7745 {
7746 dwarf2_psymtab *pst = per_cu->v.psymtab;
7747
7748 if (pst == NULL)
7749 continue;
7750
7751 for (int j = 0; j < pst->number_of_dependencies; ++j)
7752 {
7753 /* Set the 'user' field only if it is not already set. */
7754 if (pst->dependencies[j]->user == NULL)
7755 pst->dependencies[j]->user = pst;
7756 }
7757 }
7758 }
7759
7760 /* Build the partial symbol table by doing a quick pass through the
7761 .debug_info and .debug_abbrev sections. */
7762
7763 static void
7764 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7765 {
7766 struct objfile *objfile = dwarf2_per_objfile->objfile;
7767
7768 if (dwarf_read_debug)
7769 {
7770 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7771 objfile_name (objfile));
7772 }
7773
7774 scoped_restore restore_reading_psyms
7775 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7776 true);
7777
7778 dwarf2_per_objfile->info.read (objfile);
7779
7780 /* Any cached compilation units will be linked by the per-objfile
7781 read_in_chain. Make sure to free them when we're done. */
7782 free_cached_comp_units freer (dwarf2_per_objfile);
7783
7784 build_type_psymtabs (dwarf2_per_objfile);
7785
7786 create_all_comp_units (dwarf2_per_objfile);
7787
7788 /* Create a temporary address map on a temporary obstack. We later
7789 copy this to the final obstack. */
7790 auto_obstack temp_obstack;
7791
7792 scoped_restore save_psymtabs_addrmap
7793 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7794 addrmap_create_mutable (&temp_obstack));
7795
7796 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7797 process_psymtab_comp_unit (per_cu, false, language_minimal);
7798
7799 /* This has to wait until we read the CUs, we need the list of DWOs. */
7800 process_skeletonless_type_units (dwarf2_per_objfile);
7801
7802 /* Now that all TUs have been processed we can fill in the dependencies. */
7803 if (dwarf2_per_objfile->type_unit_groups != NULL)
7804 {
7805 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7806 build_type_psymtab_dependencies, dwarf2_per_objfile);
7807 }
7808
7809 if (dwarf_read_debug)
7810 print_tu_stats (dwarf2_per_objfile);
7811
7812 set_partial_user (dwarf2_per_objfile);
7813
7814 objfile->partial_symtabs->psymtabs_addrmap
7815 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7816 objfile->partial_symtabs->obstack ());
7817 /* At this point we want to keep the address map. */
7818 save_psymtabs_addrmap.release ();
7819
7820 if (dwarf_read_debug)
7821 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7822 objfile_name (objfile));
7823 }
7824
7825 /* Load the partial DIEs for a secondary CU into memory.
7826 This is also used when rereading a primary CU with load_all_dies. */
7827
7828 static void
7829 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7830 {
7831 cutu_reader reader (this_cu, NULL, 1, false);
7832
7833 if (!reader.dummy_p)
7834 {
7835 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7836 language_minimal);
7837
7838 /* Check if comp unit has_children.
7839 If so, read the rest of the partial symbols from this comp unit.
7840 If not, there's no more debug_info for this comp unit. */
7841 if (reader.comp_unit_die->has_children)
7842 load_partial_dies (&reader, reader.info_ptr, 0);
7843
7844 reader.keep ();
7845 }
7846 }
7847
7848 static void
7849 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7850 struct dwarf2_section_info *section,
7851 struct dwarf2_section_info *abbrev_section,
7852 unsigned int is_dwz)
7853 {
7854 const gdb_byte *info_ptr;
7855 struct objfile *objfile = dwarf2_per_objfile->objfile;
7856
7857 if (dwarf_read_debug)
7858 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7859 section->get_name (),
7860 section->get_file_name ());
7861
7862 section->read (objfile);
7863
7864 info_ptr = section->buffer;
7865
7866 while (info_ptr < section->buffer + section->size)
7867 {
7868 struct dwarf2_per_cu_data *this_cu;
7869
7870 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7871
7872 comp_unit_head cu_header;
7873 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7874 abbrev_section, info_ptr,
7875 rcuh_kind::COMPILE);
7876
7877 /* Save the compilation unit for later lookup. */
7878 if (cu_header.unit_type != DW_UT_type)
7879 {
7880 this_cu = XOBNEW (&objfile->objfile_obstack,
7881 struct dwarf2_per_cu_data);
7882 memset (this_cu, 0, sizeof (*this_cu));
7883 }
7884 else
7885 {
7886 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7887 struct signatured_type);
7888 memset (sig_type, 0, sizeof (*sig_type));
7889 sig_type->signature = cu_header.signature;
7890 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7891 this_cu = &sig_type->per_cu;
7892 }
7893 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7894 this_cu->sect_off = sect_off;
7895 this_cu->length = cu_header.length + cu_header.initial_length_size;
7896 this_cu->is_dwz = is_dwz;
7897 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7898 this_cu->section = section;
7899
7900 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7901
7902 info_ptr = info_ptr + this_cu->length;
7903 }
7904 }
7905
7906 /* Create a list of all compilation units in OBJFILE.
7907 This is only done for -readnow and building partial symtabs. */
7908
7909 static void
7910 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7911 {
7912 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7913 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7914 &dwarf2_per_objfile->abbrev, 0);
7915
7916 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7917 if (dwz != NULL)
7918 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7919 1);
7920 }
7921
7922 /* Process all loaded DIEs for compilation unit CU, starting at
7923 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7924 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7925 DW_AT_ranges). See the comments of add_partial_subprogram on how
7926 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7927
7928 static void
7929 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7930 CORE_ADDR *highpc, int set_addrmap,
7931 struct dwarf2_cu *cu)
7932 {
7933 struct partial_die_info *pdi;
7934
7935 /* Now, march along the PDI's, descending into ones which have
7936 interesting children but skipping the children of the other ones,
7937 until we reach the end of the compilation unit. */
7938
7939 pdi = first_die;
7940
7941 while (pdi != NULL)
7942 {
7943 pdi->fixup (cu);
7944
7945 /* Anonymous namespaces or modules have no name but have interesting
7946 children, so we need to look at them. Ditto for anonymous
7947 enums. */
7948
7949 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7950 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7951 || pdi->tag == DW_TAG_imported_unit
7952 || pdi->tag == DW_TAG_inlined_subroutine)
7953 {
7954 switch (pdi->tag)
7955 {
7956 case DW_TAG_subprogram:
7957 case DW_TAG_inlined_subroutine:
7958 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7959 break;
7960 case DW_TAG_constant:
7961 case DW_TAG_variable:
7962 case DW_TAG_typedef:
7963 case DW_TAG_union_type:
7964 if (!pdi->is_declaration)
7965 {
7966 add_partial_symbol (pdi, cu);
7967 }
7968 break;
7969 case DW_TAG_class_type:
7970 case DW_TAG_interface_type:
7971 case DW_TAG_structure_type:
7972 if (!pdi->is_declaration)
7973 {
7974 add_partial_symbol (pdi, cu);
7975 }
7976 if ((cu->language == language_rust
7977 || cu->language == language_cplus) && pdi->has_children)
7978 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7979 set_addrmap, cu);
7980 break;
7981 case DW_TAG_enumeration_type:
7982 if (!pdi->is_declaration)
7983 add_partial_enumeration (pdi, cu);
7984 break;
7985 case DW_TAG_base_type:
7986 case DW_TAG_subrange_type:
7987 /* File scope base type definitions are added to the partial
7988 symbol table. */
7989 add_partial_symbol (pdi, cu);
7990 break;
7991 case DW_TAG_namespace:
7992 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7993 break;
7994 case DW_TAG_module:
7995 if (!pdi->is_declaration)
7996 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7997 break;
7998 case DW_TAG_imported_unit:
7999 {
8000 struct dwarf2_per_cu_data *per_cu;
8001
8002 /* For now we don't handle imported units in type units. */
8003 if (cu->per_cu->is_debug_types)
8004 {
8005 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8006 " supported in type units [in module %s]"),
8007 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8008 }
8009
8010 per_cu = dwarf2_find_containing_comp_unit
8011 (pdi->d.sect_off, pdi->is_dwz,
8012 cu->per_cu->dwarf2_per_objfile);
8013
8014 /* Go read the partial unit, if needed. */
8015 if (per_cu->v.psymtab == NULL)
8016 process_psymtab_comp_unit (per_cu, true, cu->language);
8017
8018 cu->per_cu->imported_symtabs_push (per_cu);
8019 }
8020 break;
8021 case DW_TAG_imported_declaration:
8022 add_partial_symbol (pdi, cu);
8023 break;
8024 default:
8025 break;
8026 }
8027 }
8028
8029 /* If the die has a sibling, skip to the sibling. */
8030
8031 pdi = pdi->die_sibling;
8032 }
8033 }
8034
8035 /* Functions used to compute the fully scoped name of a partial DIE.
8036
8037 Normally, this is simple. For C++, the parent DIE's fully scoped
8038 name is concatenated with "::" and the partial DIE's name.
8039 Enumerators are an exception; they use the scope of their parent
8040 enumeration type, i.e. the name of the enumeration type is not
8041 prepended to the enumerator.
8042
8043 There are two complexities. One is DW_AT_specification; in this
8044 case "parent" means the parent of the target of the specification,
8045 instead of the direct parent of the DIE. The other is compilers
8046 which do not emit DW_TAG_namespace; in this case we try to guess
8047 the fully qualified name of structure types from their members'
8048 linkage names. This must be done using the DIE's children rather
8049 than the children of any DW_AT_specification target. We only need
8050 to do this for structures at the top level, i.e. if the target of
8051 any DW_AT_specification (if any; otherwise the DIE itself) does not
8052 have a parent. */
8053
8054 /* Compute the scope prefix associated with PDI's parent, in
8055 compilation unit CU. The result will be allocated on CU's
8056 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8057 field. NULL is returned if no prefix is necessary. */
8058 static const char *
8059 partial_die_parent_scope (struct partial_die_info *pdi,
8060 struct dwarf2_cu *cu)
8061 {
8062 const char *grandparent_scope;
8063 struct partial_die_info *parent, *real_pdi;
8064
8065 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8066 then this means the parent of the specification DIE. */
8067
8068 real_pdi = pdi;
8069 while (real_pdi->has_specification)
8070 {
8071 auto res = find_partial_die (real_pdi->spec_offset,
8072 real_pdi->spec_is_dwz, cu);
8073 real_pdi = res.pdi;
8074 cu = res.cu;
8075 }
8076
8077 parent = real_pdi->die_parent;
8078 if (parent == NULL)
8079 return NULL;
8080
8081 if (parent->scope_set)
8082 return parent->scope;
8083
8084 parent->fixup (cu);
8085
8086 grandparent_scope = partial_die_parent_scope (parent, cu);
8087
8088 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8089 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8090 Work around this problem here. */
8091 if (cu->language == language_cplus
8092 && parent->tag == DW_TAG_namespace
8093 && strcmp (parent->name, "::") == 0
8094 && grandparent_scope == NULL)
8095 {
8096 parent->scope = NULL;
8097 parent->scope_set = 1;
8098 return NULL;
8099 }
8100
8101 /* Nested subroutines in Fortran get a prefix. */
8102 if (pdi->tag == DW_TAG_enumerator)
8103 /* Enumerators should not get the name of the enumeration as a prefix. */
8104 parent->scope = grandparent_scope;
8105 else if (parent->tag == DW_TAG_namespace
8106 || parent->tag == DW_TAG_module
8107 || parent->tag == DW_TAG_structure_type
8108 || parent->tag == DW_TAG_class_type
8109 || parent->tag == DW_TAG_interface_type
8110 || parent->tag == DW_TAG_union_type
8111 || parent->tag == DW_TAG_enumeration_type
8112 || (cu->language == language_fortran
8113 && parent->tag == DW_TAG_subprogram
8114 && pdi->tag == DW_TAG_subprogram))
8115 {
8116 if (grandparent_scope == NULL)
8117 parent->scope = parent->name;
8118 else
8119 parent->scope = typename_concat (&cu->comp_unit_obstack,
8120 grandparent_scope,
8121 parent->name, 0, cu);
8122 }
8123 else
8124 {
8125 /* FIXME drow/2004-04-01: What should we be doing with
8126 function-local names? For partial symbols, we should probably be
8127 ignoring them. */
8128 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8129 dwarf_tag_name (parent->tag),
8130 sect_offset_str (pdi->sect_off));
8131 parent->scope = grandparent_scope;
8132 }
8133
8134 parent->scope_set = 1;
8135 return parent->scope;
8136 }
8137
8138 /* Return the fully scoped name associated with PDI, from compilation unit
8139 CU. The result will be allocated with malloc. */
8140
8141 static gdb::unique_xmalloc_ptr<char>
8142 partial_die_full_name (struct partial_die_info *pdi,
8143 struct dwarf2_cu *cu)
8144 {
8145 const char *parent_scope;
8146
8147 /* If this is a template instantiation, we can not work out the
8148 template arguments from partial DIEs. So, unfortunately, we have
8149 to go through the full DIEs. At least any work we do building
8150 types here will be reused if full symbols are loaded later. */
8151 if (pdi->has_template_arguments)
8152 {
8153 pdi->fixup (cu);
8154
8155 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8156 {
8157 struct die_info *die;
8158 struct attribute attr;
8159 struct dwarf2_cu *ref_cu = cu;
8160
8161 /* DW_FORM_ref_addr is using section offset. */
8162 attr.name = (enum dwarf_attribute) 0;
8163 attr.form = DW_FORM_ref_addr;
8164 attr.u.unsnd = to_underlying (pdi->sect_off);
8165 die = follow_die_ref (NULL, &attr, &ref_cu);
8166
8167 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8168 }
8169 }
8170
8171 parent_scope = partial_die_parent_scope (pdi, cu);
8172 if (parent_scope == NULL)
8173 return NULL;
8174 else
8175 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8176 pdi->name, 0, cu));
8177 }
8178
8179 static void
8180 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8181 {
8182 struct dwarf2_per_objfile *dwarf2_per_objfile
8183 = cu->per_cu->dwarf2_per_objfile;
8184 struct objfile *objfile = dwarf2_per_objfile->objfile;
8185 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8186 CORE_ADDR addr = 0;
8187 const char *actual_name = NULL;
8188 CORE_ADDR baseaddr;
8189
8190 baseaddr = objfile->text_section_offset ();
8191
8192 gdb::unique_xmalloc_ptr<char> built_actual_name
8193 = partial_die_full_name (pdi, cu);
8194 if (built_actual_name != NULL)
8195 actual_name = built_actual_name.get ();
8196
8197 if (actual_name == NULL)
8198 actual_name = pdi->name;
8199
8200 switch (pdi->tag)
8201 {
8202 case DW_TAG_inlined_subroutine:
8203 case DW_TAG_subprogram:
8204 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8205 - baseaddr);
8206 if (pdi->is_external
8207 || cu->language == language_ada
8208 || (cu->language == language_fortran
8209 && pdi->die_parent != NULL
8210 && pdi->die_parent->tag == DW_TAG_subprogram))
8211 {
8212 /* Normally, only "external" DIEs are part of the global scope.
8213 But in Ada and Fortran, we want to be able to access nested
8214 procedures globally. So all Ada and Fortran subprograms are
8215 stored in the global scope. */
8216 add_psymbol_to_list (actual_name,
8217 built_actual_name != NULL,
8218 VAR_DOMAIN, LOC_BLOCK,
8219 SECT_OFF_TEXT (objfile),
8220 psymbol_placement::GLOBAL,
8221 addr,
8222 cu->language, objfile);
8223 }
8224 else
8225 {
8226 add_psymbol_to_list (actual_name,
8227 built_actual_name != NULL,
8228 VAR_DOMAIN, LOC_BLOCK,
8229 SECT_OFF_TEXT (objfile),
8230 psymbol_placement::STATIC,
8231 addr, cu->language, objfile);
8232 }
8233
8234 if (pdi->main_subprogram && actual_name != NULL)
8235 set_objfile_main_name (objfile, actual_name, cu->language);
8236 break;
8237 case DW_TAG_constant:
8238 add_psymbol_to_list (actual_name,
8239 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8240 -1, (pdi->is_external
8241 ? psymbol_placement::GLOBAL
8242 : psymbol_placement::STATIC),
8243 0, cu->language, objfile);
8244 break;
8245 case DW_TAG_variable:
8246 if (pdi->d.locdesc)
8247 addr = decode_locdesc (pdi->d.locdesc, cu);
8248
8249 if (pdi->d.locdesc
8250 && addr == 0
8251 && !dwarf2_per_objfile->has_section_at_zero)
8252 {
8253 /* A global or static variable may also have been stripped
8254 out by the linker if unused, in which case its address
8255 will be nullified; do not add such variables into partial
8256 symbol table then. */
8257 }
8258 else if (pdi->is_external)
8259 {
8260 /* Global Variable.
8261 Don't enter into the minimal symbol tables as there is
8262 a minimal symbol table entry from the ELF symbols already.
8263 Enter into partial symbol table if it has a location
8264 descriptor or a type.
8265 If the location descriptor is missing, new_symbol will create
8266 a LOC_UNRESOLVED symbol, the address of the variable will then
8267 be determined from the minimal symbol table whenever the variable
8268 is referenced.
8269 The address for the partial symbol table entry is not
8270 used by GDB, but it comes in handy for debugging partial symbol
8271 table building. */
8272
8273 if (pdi->d.locdesc || pdi->has_type)
8274 add_psymbol_to_list (actual_name,
8275 built_actual_name != NULL,
8276 VAR_DOMAIN, LOC_STATIC,
8277 SECT_OFF_TEXT (objfile),
8278 psymbol_placement::GLOBAL,
8279 addr, cu->language, objfile);
8280 }
8281 else
8282 {
8283 int has_loc = pdi->d.locdesc != NULL;
8284
8285 /* Static Variable. Skip symbols whose value we cannot know (those
8286 without location descriptors or constant values). */
8287 if (!has_loc && !pdi->has_const_value)
8288 return;
8289
8290 add_psymbol_to_list (actual_name,
8291 built_actual_name != NULL,
8292 VAR_DOMAIN, LOC_STATIC,
8293 SECT_OFF_TEXT (objfile),
8294 psymbol_placement::STATIC,
8295 has_loc ? addr : 0,
8296 cu->language, objfile);
8297 }
8298 break;
8299 case DW_TAG_typedef:
8300 case DW_TAG_base_type:
8301 case DW_TAG_subrange_type:
8302 add_psymbol_to_list (actual_name,
8303 built_actual_name != NULL,
8304 VAR_DOMAIN, LOC_TYPEDEF, -1,
8305 psymbol_placement::STATIC,
8306 0, cu->language, objfile);
8307 break;
8308 case DW_TAG_imported_declaration:
8309 case DW_TAG_namespace:
8310 add_psymbol_to_list (actual_name,
8311 built_actual_name != NULL,
8312 VAR_DOMAIN, LOC_TYPEDEF, -1,
8313 psymbol_placement::GLOBAL,
8314 0, cu->language, objfile);
8315 break;
8316 case DW_TAG_module:
8317 /* With Fortran 77 there might be a "BLOCK DATA" module
8318 available without any name. If so, we skip the module as it
8319 doesn't bring any value. */
8320 if (actual_name != nullptr)
8321 add_psymbol_to_list (actual_name,
8322 built_actual_name != NULL,
8323 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8324 psymbol_placement::GLOBAL,
8325 0, cu->language, objfile);
8326 break;
8327 case DW_TAG_class_type:
8328 case DW_TAG_interface_type:
8329 case DW_TAG_structure_type:
8330 case DW_TAG_union_type:
8331 case DW_TAG_enumeration_type:
8332 /* Skip external references. The DWARF standard says in the section
8333 about "Structure, Union, and Class Type Entries": "An incomplete
8334 structure, union or class type is represented by a structure,
8335 union or class entry that does not have a byte size attribute
8336 and that has a DW_AT_declaration attribute." */
8337 if (!pdi->has_byte_size && pdi->is_declaration)
8338 return;
8339
8340 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8341 static vs. global. */
8342 add_psymbol_to_list (actual_name,
8343 built_actual_name != NULL,
8344 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8345 cu->language == language_cplus
8346 ? psymbol_placement::GLOBAL
8347 : psymbol_placement::STATIC,
8348 0, cu->language, objfile);
8349
8350 break;
8351 case DW_TAG_enumerator:
8352 add_psymbol_to_list (actual_name,
8353 built_actual_name != NULL,
8354 VAR_DOMAIN, LOC_CONST, -1,
8355 cu->language == language_cplus
8356 ? psymbol_placement::GLOBAL
8357 : psymbol_placement::STATIC,
8358 0, cu->language, objfile);
8359 break;
8360 default:
8361 break;
8362 }
8363 }
8364
8365 /* Read a partial die corresponding to a namespace; also, add a symbol
8366 corresponding to that namespace to the symbol table. NAMESPACE is
8367 the name of the enclosing namespace. */
8368
8369 static void
8370 add_partial_namespace (struct partial_die_info *pdi,
8371 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8372 int set_addrmap, struct dwarf2_cu *cu)
8373 {
8374 /* Add a symbol for the namespace. */
8375
8376 add_partial_symbol (pdi, cu);
8377
8378 /* Now scan partial symbols in that namespace. */
8379
8380 if (pdi->has_children)
8381 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8382 }
8383
8384 /* Read a partial die corresponding to a Fortran module. */
8385
8386 static void
8387 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8388 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8389 {
8390 /* Add a symbol for the namespace. */
8391
8392 add_partial_symbol (pdi, cu);
8393
8394 /* Now scan partial symbols in that module. */
8395
8396 if (pdi->has_children)
8397 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8398 }
8399
8400 /* Read a partial die corresponding to a subprogram or an inlined
8401 subprogram and create a partial symbol for that subprogram.
8402 When the CU language allows it, this routine also defines a partial
8403 symbol for each nested subprogram that this subprogram contains.
8404 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8405 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8406
8407 PDI may also be a lexical block, in which case we simply search
8408 recursively for subprograms defined inside that lexical block.
8409 Again, this is only performed when the CU language allows this
8410 type of definitions. */
8411
8412 static void
8413 add_partial_subprogram (struct partial_die_info *pdi,
8414 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8415 int set_addrmap, struct dwarf2_cu *cu)
8416 {
8417 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8418 {
8419 if (pdi->has_pc_info)
8420 {
8421 if (pdi->lowpc < *lowpc)
8422 *lowpc = pdi->lowpc;
8423 if (pdi->highpc > *highpc)
8424 *highpc = pdi->highpc;
8425 if (set_addrmap)
8426 {
8427 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8428 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8429 CORE_ADDR baseaddr;
8430 CORE_ADDR this_highpc;
8431 CORE_ADDR this_lowpc;
8432
8433 baseaddr = objfile->text_section_offset ();
8434 this_lowpc
8435 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8436 pdi->lowpc + baseaddr)
8437 - baseaddr);
8438 this_highpc
8439 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8440 pdi->highpc + baseaddr)
8441 - baseaddr);
8442 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8443 this_lowpc, this_highpc - 1,
8444 cu->per_cu->v.psymtab);
8445 }
8446 }
8447
8448 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8449 {
8450 if (!pdi->is_declaration)
8451 /* Ignore subprogram DIEs that do not have a name, they are
8452 illegal. Do not emit a complaint at this point, we will
8453 do so when we convert this psymtab into a symtab. */
8454 if (pdi->name)
8455 add_partial_symbol (pdi, cu);
8456 }
8457 }
8458
8459 if (! pdi->has_children)
8460 return;
8461
8462 if (cu->language == language_ada || cu->language == language_fortran)
8463 {
8464 pdi = pdi->die_child;
8465 while (pdi != NULL)
8466 {
8467 pdi->fixup (cu);
8468 if (pdi->tag == DW_TAG_subprogram
8469 || pdi->tag == DW_TAG_inlined_subroutine
8470 || pdi->tag == DW_TAG_lexical_block)
8471 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8472 pdi = pdi->die_sibling;
8473 }
8474 }
8475 }
8476
8477 /* Read a partial die corresponding to an enumeration type. */
8478
8479 static void
8480 add_partial_enumeration (struct partial_die_info *enum_pdi,
8481 struct dwarf2_cu *cu)
8482 {
8483 struct partial_die_info *pdi;
8484
8485 if (enum_pdi->name != NULL)
8486 add_partial_symbol (enum_pdi, cu);
8487
8488 pdi = enum_pdi->die_child;
8489 while (pdi)
8490 {
8491 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8492 complaint (_("malformed enumerator DIE ignored"));
8493 else
8494 add_partial_symbol (pdi, cu);
8495 pdi = pdi->die_sibling;
8496 }
8497 }
8498
8499 /* Return the initial uleb128 in the die at INFO_PTR. */
8500
8501 static unsigned int
8502 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8503 {
8504 unsigned int bytes_read;
8505
8506 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8507 }
8508
8509 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8510 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8511
8512 Return the corresponding abbrev, or NULL if the number is zero (indicating
8513 an empty DIE). In either case *BYTES_READ will be set to the length of
8514 the initial number. */
8515
8516 static struct abbrev_info *
8517 peek_die_abbrev (const die_reader_specs &reader,
8518 const gdb_byte *info_ptr, unsigned int *bytes_read)
8519 {
8520 dwarf2_cu *cu = reader.cu;
8521 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8522 unsigned int abbrev_number
8523 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8524
8525 if (abbrev_number == 0)
8526 return NULL;
8527
8528 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8529 if (!abbrev)
8530 {
8531 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8532 " at offset %s [in module %s]"),
8533 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8534 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8535 }
8536
8537 return abbrev;
8538 }
8539
8540 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8541 Returns a pointer to the end of a series of DIEs, terminated by an empty
8542 DIE. Any children of the skipped DIEs will also be skipped. */
8543
8544 static const gdb_byte *
8545 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8546 {
8547 while (1)
8548 {
8549 unsigned int bytes_read;
8550 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8551
8552 if (abbrev == NULL)
8553 return info_ptr + bytes_read;
8554 else
8555 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8556 }
8557 }
8558
8559 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8560 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8561 abbrev corresponding to that skipped uleb128 should be passed in
8562 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8563 children. */
8564
8565 static const gdb_byte *
8566 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8567 struct abbrev_info *abbrev)
8568 {
8569 unsigned int bytes_read;
8570 struct attribute attr;
8571 bfd *abfd = reader->abfd;
8572 struct dwarf2_cu *cu = reader->cu;
8573 const gdb_byte *buffer = reader->buffer;
8574 const gdb_byte *buffer_end = reader->buffer_end;
8575 unsigned int form, i;
8576
8577 for (i = 0; i < abbrev->num_attrs; i++)
8578 {
8579 /* The only abbrev we care about is DW_AT_sibling. */
8580 if (abbrev->attrs[i].name == DW_AT_sibling)
8581 {
8582 bool ignored;
8583 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8584 &ignored);
8585 if (attr.form == DW_FORM_ref_addr)
8586 complaint (_("ignoring absolute DW_AT_sibling"));
8587 else
8588 {
8589 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8590 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8591
8592 if (sibling_ptr < info_ptr)
8593 complaint (_("DW_AT_sibling points backwards"));
8594 else if (sibling_ptr > reader->buffer_end)
8595 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8596 else
8597 return sibling_ptr;
8598 }
8599 }
8600
8601 /* If it isn't DW_AT_sibling, skip this attribute. */
8602 form = abbrev->attrs[i].form;
8603 skip_attribute:
8604 switch (form)
8605 {
8606 case DW_FORM_ref_addr:
8607 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8608 and later it is offset sized. */
8609 if (cu->header.version == 2)
8610 info_ptr += cu->header.addr_size;
8611 else
8612 info_ptr += cu->header.offset_size;
8613 break;
8614 case DW_FORM_GNU_ref_alt:
8615 info_ptr += cu->header.offset_size;
8616 break;
8617 case DW_FORM_addr:
8618 info_ptr += cu->header.addr_size;
8619 break;
8620 case DW_FORM_data1:
8621 case DW_FORM_ref1:
8622 case DW_FORM_flag:
8623 case DW_FORM_strx1:
8624 info_ptr += 1;
8625 break;
8626 case DW_FORM_flag_present:
8627 case DW_FORM_implicit_const:
8628 break;
8629 case DW_FORM_data2:
8630 case DW_FORM_ref2:
8631 case DW_FORM_strx2:
8632 info_ptr += 2;
8633 break;
8634 case DW_FORM_strx3:
8635 info_ptr += 3;
8636 break;
8637 case DW_FORM_data4:
8638 case DW_FORM_ref4:
8639 case DW_FORM_strx4:
8640 info_ptr += 4;
8641 break;
8642 case DW_FORM_data8:
8643 case DW_FORM_ref8:
8644 case DW_FORM_ref_sig8:
8645 info_ptr += 8;
8646 break;
8647 case DW_FORM_data16:
8648 info_ptr += 16;
8649 break;
8650 case DW_FORM_string:
8651 read_direct_string (abfd, info_ptr, &bytes_read);
8652 info_ptr += bytes_read;
8653 break;
8654 case DW_FORM_sec_offset:
8655 case DW_FORM_strp:
8656 case DW_FORM_GNU_strp_alt:
8657 info_ptr += cu->header.offset_size;
8658 break;
8659 case DW_FORM_exprloc:
8660 case DW_FORM_block:
8661 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8662 info_ptr += bytes_read;
8663 break;
8664 case DW_FORM_block1:
8665 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8666 break;
8667 case DW_FORM_block2:
8668 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8669 break;
8670 case DW_FORM_block4:
8671 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8672 break;
8673 case DW_FORM_addrx:
8674 case DW_FORM_strx:
8675 case DW_FORM_sdata:
8676 case DW_FORM_udata:
8677 case DW_FORM_ref_udata:
8678 case DW_FORM_GNU_addr_index:
8679 case DW_FORM_GNU_str_index:
8680 case DW_FORM_rnglistx:
8681 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8682 break;
8683 case DW_FORM_indirect:
8684 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8685 info_ptr += bytes_read;
8686 /* We need to continue parsing from here, so just go back to
8687 the top. */
8688 goto skip_attribute;
8689
8690 default:
8691 error (_("Dwarf Error: Cannot handle %s "
8692 "in DWARF reader [in module %s]"),
8693 dwarf_form_name (form),
8694 bfd_get_filename (abfd));
8695 }
8696 }
8697
8698 if (abbrev->has_children)
8699 return skip_children (reader, info_ptr);
8700 else
8701 return info_ptr;
8702 }
8703
8704 /* Locate ORIG_PDI's sibling.
8705 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8706
8707 static const gdb_byte *
8708 locate_pdi_sibling (const struct die_reader_specs *reader,
8709 struct partial_die_info *orig_pdi,
8710 const gdb_byte *info_ptr)
8711 {
8712 /* Do we know the sibling already? */
8713
8714 if (orig_pdi->sibling)
8715 return orig_pdi->sibling;
8716
8717 /* Are there any children to deal with? */
8718
8719 if (!orig_pdi->has_children)
8720 return info_ptr;
8721
8722 /* Skip the children the long way. */
8723
8724 return skip_children (reader, info_ptr);
8725 }
8726
8727 /* Expand this partial symbol table into a full symbol table. SELF is
8728 not NULL. */
8729
8730 void
8731 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8732 {
8733 struct dwarf2_per_objfile *dwarf2_per_objfile
8734 = get_dwarf2_per_objfile (objfile);
8735
8736 gdb_assert (!readin);
8737 /* If this psymtab is constructed from a debug-only objfile, the
8738 has_section_at_zero flag will not necessarily be correct. We
8739 can get the correct value for this flag by looking at the data
8740 associated with the (presumably stripped) associated objfile. */
8741 if (objfile->separate_debug_objfile_backlink)
8742 {
8743 struct dwarf2_per_objfile *dpo_backlink
8744 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8745
8746 dwarf2_per_objfile->has_section_at_zero
8747 = dpo_backlink->has_section_at_zero;
8748 }
8749
8750 expand_psymtab (objfile);
8751
8752 process_cu_includes (dwarf2_per_objfile);
8753 }
8754 \f
8755 /* Reading in full CUs. */
8756
8757 /* Add PER_CU to the queue. */
8758
8759 static void
8760 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8761 enum language pretend_language)
8762 {
8763 per_cu->queued = 1;
8764 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8765 }
8766
8767 /* If PER_CU is not yet queued, add it to the queue.
8768 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8769 dependency.
8770 The result is non-zero if PER_CU was queued, otherwise the result is zero
8771 meaning either PER_CU is already queued or it is already loaded.
8772
8773 N.B. There is an invariant here that if a CU is queued then it is loaded.
8774 The caller is required to load PER_CU if we return non-zero. */
8775
8776 static int
8777 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8778 struct dwarf2_per_cu_data *per_cu,
8779 enum language pretend_language)
8780 {
8781 /* We may arrive here during partial symbol reading, if we need full
8782 DIEs to process an unusual case (e.g. template arguments). Do
8783 not queue PER_CU, just tell our caller to load its DIEs. */
8784 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8785 {
8786 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8787 return 1;
8788 return 0;
8789 }
8790
8791 /* Mark the dependence relation so that we don't flush PER_CU
8792 too early. */
8793 if (dependent_cu != NULL)
8794 dwarf2_add_dependence (dependent_cu, per_cu);
8795
8796 /* If it's already on the queue, we have nothing to do. */
8797 if (per_cu->queued)
8798 return 0;
8799
8800 /* If the compilation unit is already loaded, just mark it as
8801 used. */
8802 if (per_cu->cu != NULL)
8803 {
8804 per_cu->cu->last_used = 0;
8805 return 0;
8806 }
8807
8808 /* Add it to the queue. */
8809 queue_comp_unit (per_cu, pretend_language);
8810
8811 return 1;
8812 }
8813
8814 /* Process the queue. */
8815
8816 static void
8817 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8818 {
8819 if (dwarf_read_debug)
8820 {
8821 fprintf_unfiltered (gdb_stdlog,
8822 "Expanding one or more symtabs of objfile %s ...\n",
8823 objfile_name (dwarf2_per_objfile->objfile));
8824 }
8825
8826 /* The queue starts out with one item, but following a DIE reference
8827 may load a new CU, adding it to the end of the queue. */
8828 while (!dwarf2_per_objfile->queue.empty ())
8829 {
8830 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8831
8832 if ((dwarf2_per_objfile->using_index
8833 ? !item.per_cu->v.quick->compunit_symtab
8834 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8835 /* Skip dummy CUs. */
8836 && item.per_cu->cu != NULL)
8837 {
8838 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8839 unsigned int debug_print_threshold;
8840 char buf[100];
8841
8842 if (per_cu->is_debug_types)
8843 {
8844 struct signatured_type *sig_type =
8845 (struct signatured_type *) per_cu;
8846
8847 sprintf (buf, "TU %s at offset %s",
8848 hex_string (sig_type->signature),
8849 sect_offset_str (per_cu->sect_off));
8850 /* There can be 100s of TUs.
8851 Only print them in verbose mode. */
8852 debug_print_threshold = 2;
8853 }
8854 else
8855 {
8856 sprintf (buf, "CU at offset %s",
8857 sect_offset_str (per_cu->sect_off));
8858 debug_print_threshold = 1;
8859 }
8860
8861 if (dwarf_read_debug >= debug_print_threshold)
8862 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8863
8864 if (per_cu->is_debug_types)
8865 process_full_type_unit (per_cu, item.pretend_language);
8866 else
8867 process_full_comp_unit (per_cu, item.pretend_language);
8868
8869 if (dwarf_read_debug >= debug_print_threshold)
8870 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8871 }
8872
8873 item.per_cu->queued = 0;
8874 dwarf2_per_objfile->queue.pop ();
8875 }
8876
8877 if (dwarf_read_debug)
8878 {
8879 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8880 objfile_name (dwarf2_per_objfile->objfile));
8881 }
8882 }
8883
8884 /* Read in full symbols for PST, and anything it depends on. */
8885
8886 void
8887 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8888 {
8889 if (readin)
8890 return;
8891
8892 read_dependencies (objfile);
8893
8894 dw2_do_instantiate_symtab (per_cu_data, false);
8895 gdb_assert (get_compunit_symtab () != nullptr);
8896 }
8897
8898 /* Trivial hash function for die_info: the hash value of a DIE
8899 is its offset in .debug_info for this objfile. */
8900
8901 static hashval_t
8902 die_hash (const void *item)
8903 {
8904 const struct die_info *die = (const struct die_info *) item;
8905
8906 return to_underlying (die->sect_off);
8907 }
8908
8909 /* Trivial comparison function for die_info structures: two DIEs
8910 are equal if they have the same offset. */
8911
8912 static int
8913 die_eq (const void *item_lhs, const void *item_rhs)
8914 {
8915 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8916 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8917
8918 return die_lhs->sect_off == die_rhs->sect_off;
8919 }
8920
8921 /* Load the DIEs associated with PER_CU into memory. */
8922
8923 static void
8924 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8925 bool skip_partial,
8926 enum language pretend_language)
8927 {
8928 gdb_assert (! this_cu->is_debug_types);
8929
8930 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8931 if (reader.dummy_p)
8932 return;
8933
8934 struct dwarf2_cu *cu = reader.cu;
8935 const gdb_byte *info_ptr = reader.info_ptr;
8936
8937 gdb_assert (cu->die_hash == NULL);
8938 cu->die_hash =
8939 htab_create_alloc_ex (cu->header.length / 12,
8940 die_hash,
8941 die_eq,
8942 NULL,
8943 &cu->comp_unit_obstack,
8944 hashtab_obstack_allocate,
8945 dummy_obstack_deallocate);
8946
8947 if (reader.comp_unit_die->has_children)
8948 reader.comp_unit_die->child
8949 = read_die_and_siblings (&reader, reader.info_ptr,
8950 &info_ptr, reader.comp_unit_die);
8951 cu->dies = reader.comp_unit_die;
8952 /* comp_unit_die is not stored in die_hash, no need. */
8953
8954 /* We try not to read any attributes in this function, because not
8955 all CUs needed for references have been loaded yet, and symbol
8956 table processing isn't initialized. But we have to set the CU language,
8957 or we won't be able to build types correctly.
8958 Similarly, if we do not read the producer, we can not apply
8959 producer-specific interpretation. */
8960 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8961
8962 reader.keep ();
8963 }
8964
8965 /* Add a DIE to the delayed physname list. */
8966
8967 static void
8968 add_to_method_list (struct type *type, int fnfield_index, int index,
8969 const char *name, struct die_info *die,
8970 struct dwarf2_cu *cu)
8971 {
8972 struct delayed_method_info mi;
8973 mi.type = type;
8974 mi.fnfield_index = fnfield_index;
8975 mi.index = index;
8976 mi.name = name;
8977 mi.die = die;
8978 cu->method_list.push_back (mi);
8979 }
8980
8981 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8982 "const" / "volatile". If so, decrements LEN by the length of the
8983 modifier and return true. Otherwise return false. */
8984
8985 template<size_t N>
8986 static bool
8987 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8988 {
8989 size_t mod_len = sizeof (mod) - 1;
8990 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8991 {
8992 len -= mod_len;
8993 return true;
8994 }
8995 return false;
8996 }
8997
8998 /* Compute the physnames of any methods on the CU's method list.
8999
9000 The computation of method physnames is delayed in order to avoid the
9001 (bad) condition that one of the method's formal parameters is of an as yet
9002 incomplete type. */
9003
9004 static void
9005 compute_delayed_physnames (struct dwarf2_cu *cu)
9006 {
9007 /* Only C++ delays computing physnames. */
9008 if (cu->method_list.empty ())
9009 return;
9010 gdb_assert (cu->language == language_cplus);
9011
9012 for (const delayed_method_info &mi : cu->method_list)
9013 {
9014 const char *physname;
9015 struct fn_fieldlist *fn_flp
9016 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9017 physname = dwarf2_physname (mi.name, mi.die, cu);
9018 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9019 = physname ? physname : "";
9020
9021 /* Since there's no tag to indicate whether a method is a
9022 const/volatile overload, extract that information out of the
9023 demangled name. */
9024 if (physname != NULL)
9025 {
9026 size_t len = strlen (physname);
9027
9028 while (1)
9029 {
9030 if (physname[len] == ')') /* shortcut */
9031 break;
9032 else if (check_modifier (physname, len, " const"))
9033 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9034 else if (check_modifier (physname, len, " volatile"))
9035 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9036 else
9037 break;
9038 }
9039 }
9040 }
9041
9042 /* The list is no longer needed. */
9043 cu->method_list.clear ();
9044 }
9045
9046 /* Go objects should be embedded in a DW_TAG_module DIE,
9047 and it's not clear if/how imported objects will appear.
9048 To keep Go support simple until that's worked out,
9049 go back through what we've read and create something usable.
9050 We could do this while processing each DIE, and feels kinda cleaner,
9051 but that way is more invasive.
9052 This is to, for example, allow the user to type "p var" or "b main"
9053 without having to specify the package name, and allow lookups
9054 of module.object to work in contexts that use the expression
9055 parser. */
9056
9057 static void
9058 fixup_go_packaging (struct dwarf2_cu *cu)
9059 {
9060 gdb::unique_xmalloc_ptr<char> package_name;
9061 struct pending *list;
9062 int i;
9063
9064 for (list = *cu->get_builder ()->get_global_symbols ();
9065 list != NULL;
9066 list = list->next)
9067 {
9068 for (i = 0; i < list->nsyms; ++i)
9069 {
9070 struct symbol *sym = list->symbol[i];
9071
9072 if (sym->language () == language_go
9073 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9074 {
9075 gdb::unique_xmalloc_ptr<char> this_package_name
9076 (go_symbol_package_name (sym));
9077
9078 if (this_package_name == NULL)
9079 continue;
9080 if (package_name == NULL)
9081 package_name = std::move (this_package_name);
9082 else
9083 {
9084 struct objfile *objfile
9085 = cu->per_cu->dwarf2_per_objfile->objfile;
9086 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9087 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9088 (symbol_symtab (sym) != NULL
9089 ? symtab_to_filename_for_display
9090 (symbol_symtab (sym))
9091 : objfile_name (objfile)),
9092 this_package_name.get (), package_name.get ());
9093 }
9094 }
9095 }
9096 }
9097
9098 if (package_name != NULL)
9099 {
9100 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9101 const char *saved_package_name = objfile->intern (package_name.get ());
9102 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9103 saved_package_name);
9104 struct symbol *sym;
9105
9106 sym = allocate_symbol (objfile);
9107 sym->set_language (language_go, &objfile->objfile_obstack);
9108 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9109 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9110 e.g., "main" finds the "main" module and not C's main(). */
9111 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9112 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9113 SYMBOL_TYPE (sym) = type;
9114
9115 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9116 }
9117 }
9118
9119 /* Allocate a fully-qualified name consisting of the two parts on the
9120 obstack. */
9121
9122 static const char *
9123 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9124 {
9125 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9126 }
9127
9128 /* A helper that allocates a struct discriminant_info to attach to a
9129 union type. */
9130
9131 static struct discriminant_info *
9132 alloc_discriminant_info (struct type *type, int discriminant_index,
9133 int default_index)
9134 {
9135 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9136 gdb_assert (discriminant_index == -1
9137 || (discriminant_index >= 0
9138 && discriminant_index < TYPE_NFIELDS (type)));
9139 gdb_assert (default_index == -1
9140 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9141
9142 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9143
9144 struct discriminant_info *disc
9145 = ((struct discriminant_info *)
9146 TYPE_ZALLOC (type,
9147 offsetof (struct discriminant_info, discriminants)
9148 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9149 disc->default_index = default_index;
9150 disc->discriminant_index = discriminant_index;
9151
9152 struct dynamic_prop prop;
9153 prop.kind = PROP_UNDEFINED;
9154 prop.data.baton = disc;
9155
9156 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9157
9158 return disc;
9159 }
9160
9161 /* Some versions of rustc emitted enums in an unusual way.
9162
9163 Ordinary enums were emitted as unions. The first element of each
9164 structure in the union was named "RUST$ENUM$DISR". This element
9165 held the discriminant.
9166
9167 These versions of Rust also implemented the "non-zero"
9168 optimization. When the enum had two values, and one is empty and
9169 the other holds a pointer that cannot be zero, the pointer is used
9170 as the discriminant, with a zero value meaning the empty variant.
9171 Here, the union's first member is of the form
9172 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9173 where the fieldnos are the indices of the fields that should be
9174 traversed in order to find the field (which may be several fields deep)
9175 and the variantname is the name of the variant of the case when the
9176 field is zero.
9177
9178 This function recognizes whether TYPE is of one of these forms,
9179 and, if so, smashes it to be a variant type. */
9180
9181 static void
9182 quirk_rust_enum (struct type *type, struct objfile *objfile)
9183 {
9184 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9185
9186 /* We don't need to deal with empty enums. */
9187 if (TYPE_NFIELDS (type) == 0)
9188 return;
9189
9190 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9191 if (TYPE_NFIELDS (type) == 1
9192 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9193 {
9194 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9195
9196 /* Decode the field name to find the offset of the
9197 discriminant. */
9198 ULONGEST bit_offset = 0;
9199 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9200 while (name[0] >= '0' && name[0] <= '9')
9201 {
9202 char *tail;
9203 unsigned long index = strtoul (name, &tail, 10);
9204 name = tail;
9205 if (*name != '$'
9206 || index >= TYPE_NFIELDS (field_type)
9207 || (TYPE_FIELD_LOC_KIND (field_type, index)
9208 != FIELD_LOC_KIND_BITPOS))
9209 {
9210 complaint (_("Could not parse Rust enum encoding string \"%s\""
9211 "[in module %s]"),
9212 TYPE_FIELD_NAME (type, 0),
9213 objfile_name (objfile));
9214 return;
9215 }
9216 ++name;
9217
9218 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9219 field_type = TYPE_FIELD_TYPE (field_type, index);
9220 }
9221
9222 /* Make a union to hold the variants. */
9223 struct type *union_type = alloc_type (objfile);
9224 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9225 TYPE_NFIELDS (union_type) = 3;
9226 TYPE_FIELDS (union_type)
9227 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9228 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9229 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9230
9231 /* Put the discriminant must at index 0. */
9232 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9233 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9234 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9235 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9236
9237 /* The order of fields doesn't really matter, so put the real
9238 field at index 1 and the data-less field at index 2. */
9239 struct discriminant_info *disc
9240 = alloc_discriminant_info (union_type, 0, 1);
9241 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9242 TYPE_FIELD_NAME (union_type, 1)
9243 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9244 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9245 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9246 TYPE_FIELD_NAME (union_type, 1));
9247
9248 const char *dataless_name
9249 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9250 name);
9251 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9252 dataless_name);
9253 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9254 /* NAME points into the original discriminant name, which
9255 already has the correct lifetime. */
9256 TYPE_FIELD_NAME (union_type, 2) = name;
9257 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9258 disc->discriminants[2] = 0;
9259
9260 /* Smash this type to be a structure type. We have to do this
9261 because the type has already been recorded. */
9262 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9263 TYPE_NFIELDS (type) = 1;
9264 TYPE_FIELDS (type)
9265 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9266
9267 /* Install the variant part. */
9268 TYPE_FIELD_TYPE (type, 0) = union_type;
9269 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9270 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9271 }
9272 /* A union with a single anonymous field is probably an old-style
9273 univariant enum. */
9274 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9275 {
9276 /* Smash this type to be a structure type. We have to do this
9277 because the type has already been recorded. */
9278 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9279
9280 /* Make a union to hold the variants. */
9281 struct type *union_type = alloc_type (objfile);
9282 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9283 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9284 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9285 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9286 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9287
9288 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9289 const char *variant_name
9290 = rust_last_path_segment (TYPE_NAME (field_type));
9291 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9292 TYPE_NAME (field_type)
9293 = rust_fully_qualify (&objfile->objfile_obstack,
9294 TYPE_NAME (type), variant_name);
9295
9296 /* Install the union in the outer struct type. */
9297 TYPE_NFIELDS (type) = 1;
9298 TYPE_FIELDS (type)
9299 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9300 TYPE_FIELD_TYPE (type, 0) = union_type;
9301 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9302 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9303
9304 alloc_discriminant_info (union_type, -1, 0);
9305 }
9306 else
9307 {
9308 struct type *disr_type = nullptr;
9309 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9310 {
9311 disr_type = TYPE_FIELD_TYPE (type, i);
9312
9313 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9314 {
9315 /* All fields of a true enum will be structs. */
9316 return;
9317 }
9318 else if (TYPE_NFIELDS (disr_type) == 0)
9319 {
9320 /* Could be data-less variant, so keep going. */
9321 disr_type = nullptr;
9322 }
9323 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9324 "RUST$ENUM$DISR") != 0)
9325 {
9326 /* Not a Rust enum. */
9327 return;
9328 }
9329 else
9330 {
9331 /* Found one. */
9332 break;
9333 }
9334 }
9335
9336 /* If we got here without a discriminant, then it's probably
9337 just a union. */
9338 if (disr_type == nullptr)
9339 return;
9340
9341 /* Smash this type to be a structure type. We have to do this
9342 because the type has already been recorded. */
9343 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9344
9345 /* Make a union to hold the variants. */
9346 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9347 struct type *union_type = alloc_type (objfile);
9348 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9349 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9350 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9351 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9352 TYPE_FIELDS (union_type)
9353 = (struct field *) TYPE_ZALLOC (union_type,
9354 (TYPE_NFIELDS (union_type)
9355 * sizeof (struct field)));
9356
9357 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9358 TYPE_NFIELDS (type) * sizeof (struct field));
9359
9360 /* Install the discriminant at index 0 in the union. */
9361 TYPE_FIELD (union_type, 0) = *disr_field;
9362 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9363 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9364
9365 /* Install the union in the outer struct type. */
9366 TYPE_FIELD_TYPE (type, 0) = union_type;
9367 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9368 TYPE_NFIELDS (type) = 1;
9369
9370 /* Set the size and offset of the union type. */
9371 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9372
9373 /* We need a way to find the correct discriminant given a
9374 variant name. For convenience we build a map here. */
9375 struct type *enum_type = FIELD_TYPE (*disr_field);
9376 std::unordered_map<std::string, ULONGEST> discriminant_map;
9377 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9378 {
9379 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9380 {
9381 const char *name
9382 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9383 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9384 }
9385 }
9386
9387 int n_fields = TYPE_NFIELDS (union_type);
9388 struct discriminant_info *disc
9389 = alloc_discriminant_info (union_type, 0, -1);
9390 /* Skip the discriminant here. */
9391 for (int i = 1; i < n_fields; ++i)
9392 {
9393 /* Find the final word in the name of this variant's type.
9394 That name can be used to look up the correct
9395 discriminant. */
9396 const char *variant_name
9397 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9398 i)));
9399
9400 auto iter = discriminant_map.find (variant_name);
9401 if (iter != discriminant_map.end ())
9402 disc->discriminants[i] = iter->second;
9403
9404 /* Remove the discriminant field, if it exists. */
9405 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9406 if (TYPE_NFIELDS (sub_type) > 0)
9407 {
9408 --TYPE_NFIELDS (sub_type);
9409 ++TYPE_FIELDS (sub_type);
9410 }
9411 TYPE_FIELD_NAME (union_type, i) = variant_name;
9412 TYPE_NAME (sub_type)
9413 = rust_fully_qualify (&objfile->objfile_obstack,
9414 TYPE_NAME (type), variant_name);
9415 }
9416 }
9417 }
9418
9419 /* Rewrite some Rust unions to be structures with variants parts. */
9420
9421 static void
9422 rust_union_quirks (struct dwarf2_cu *cu)
9423 {
9424 gdb_assert (cu->language == language_rust);
9425 for (type *type_ : cu->rust_unions)
9426 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9427 /* We don't need this any more. */
9428 cu->rust_unions.clear ();
9429 }
9430
9431 /* Return the symtab for PER_CU. This works properly regardless of
9432 whether we're using the index or psymtabs. */
9433
9434 static struct compunit_symtab *
9435 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9436 {
9437 return (per_cu->dwarf2_per_objfile->using_index
9438 ? per_cu->v.quick->compunit_symtab
9439 : per_cu->v.psymtab->compunit_symtab);
9440 }
9441
9442 /* A helper function for computing the list of all symbol tables
9443 included by PER_CU. */
9444
9445 static void
9446 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9447 htab_t all_children, htab_t all_type_symtabs,
9448 struct dwarf2_per_cu_data *per_cu,
9449 struct compunit_symtab *immediate_parent)
9450 {
9451 void **slot;
9452 struct compunit_symtab *cust;
9453
9454 slot = htab_find_slot (all_children, per_cu, INSERT);
9455 if (*slot != NULL)
9456 {
9457 /* This inclusion and its children have been processed. */
9458 return;
9459 }
9460
9461 *slot = per_cu;
9462 /* Only add a CU if it has a symbol table. */
9463 cust = get_compunit_symtab (per_cu);
9464 if (cust != NULL)
9465 {
9466 /* If this is a type unit only add its symbol table if we haven't
9467 seen it yet (type unit per_cu's can share symtabs). */
9468 if (per_cu->is_debug_types)
9469 {
9470 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9471 if (*slot == NULL)
9472 {
9473 *slot = cust;
9474 result->push_back (cust);
9475 if (cust->user == NULL)
9476 cust->user = immediate_parent;
9477 }
9478 }
9479 else
9480 {
9481 result->push_back (cust);
9482 if (cust->user == NULL)
9483 cust->user = immediate_parent;
9484 }
9485 }
9486
9487 if (!per_cu->imported_symtabs_empty ())
9488 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9489 {
9490 recursively_compute_inclusions (result, all_children,
9491 all_type_symtabs, ptr, cust);
9492 }
9493 }
9494
9495 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9496 PER_CU. */
9497
9498 static void
9499 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9500 {
9501 gdb_assert (! per_cu->is_debug_types);
9502
9503 if (!per_cu->imported_symtabs_empty ())
9504 {
9505 int len;
9506 std::vector<compunit_symtab *> result_symtabs;
9507 htab_t all_children, all_type_symtabs;
9508 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9509
9510 /* If we don't have a symtab, we can just skip this case. */
9511 if (cust == NULL)
9512 return;
9513
9514 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9515 NULL, xcalloc, xfree);
9516 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9517 NULL, xcalloc, xfree);
9518
9519 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9520 {
9521 recursively_compute_inclusions (&result_symtabs, all_children,
9522 all_type_symtabs, ptr, cust);
9523 }
9524
9525 /* Now we have a transitive closure of all the included symtabs. */
9526 len = result_symtabs.size ();
9527 cust->includes
9528 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9529 struct compunit_symtab *, len + 1);
9530 memcpy (cust->includes, result_symtabs.data (),
9531 len * sizeof (compunit_symtab *));
9532 cust->includes[len] = NULL;
9533
9534 htab_delete (all_children);
9535 htab_delete (all_type_symtabs);
9536 }
9537 }
9538
9539 /* Compute the 'includes' field for the symtabs of all the CUs we just
9540 read. */
9541
9542 static void
9543 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9544 {
9545 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9546 {
9547 if (! iter->is_debug_types)
9548 compute_compunit_symtab_includes (iter);
9549 }
9550
9551 dwarf2_per_objfile->just_read_cus.clear ();
9552 }
9553
9554 /* Generate full symbol information for PER_CU, whose DIEs have
9555 already been loaded into memory. */
9556
9557 static void
9558 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9559 enum language pretend_language)
9560 {
9561 struct dwarf2_cu *cu = per_cu->cu;
9562 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9563 struct objfile *objfile = dwarf2_per_objfile->objfile;
9564 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9565 CORE_ADDR lowpc, highpc;
9566 struct compunit_symtab *cust;
9567 CORE_ADDR baseaddr;
9568 struct block *static_block;
9569 CORE_ADDR addr;
9570
9571 baseaddr = objfile->text_section_offset ();
9572
9573 /* Clear the list here in case something was left over. */
9574 cu->method_list.clear ();
9575
9576 cu->language = pretend_language;
9577 cu->language_defn = language_def (cu->language);
9578
9579 /* Do line number decoding in read_file_scope () */
9580 process_die (cu->dies, cu);
9581
9582 /* For now fudge the Go package. */
9583 if (cu->language == language_go)
9584 fixup_go_packaging (cu);
9585
9586 /* Now that we have processed all the DIEs in the CU, all the types
9587 should be complete, and it should now be safe to compute all of the
9588 physnames. */
9589 compute_delayed_physnames (cu);
9590
9591 if (cu->language == language_rust)
9592 rust_union_quirks (cu);
9593
9594 /* Some compilers don't define a DW_AT_high_pc attribute for the
9595 compilation unit. If the DW_AT_high_pc is missing, synthesize
9596 it, by scanning the DIE's below the compilation unit. */
9597 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9598
9599 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9600 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9601
9602 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9603 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9604 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9605 addrmap to help ensure it has an accurate map of pc values belonging to
9606 this comp unit. */
9607 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9608
9609 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9610 SECT_OFF_TEXT (objfile),
9611 0);
9612
9613 if (cust != NULL)
9614 {
9615 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9616
9617 /* Set symtab language to language from DW_AT_language. If the
9618 compilation is from a C file generated by language preprocessors, do
9619 not set the language if it was already deduced by start_subfile. */
9620 if (!(cu->language == language_c
9621 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9622 COMPUNIT_FILETABS (cust)->language = cu->language;
9623
9624 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9625 produce DW_AT_location with location lists but it can be possibly
9626 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9627 there were bugs in prologue debug info, fixed later in GCC-4.5
9628 by "unwind info for epilogues" patch (which is not directly related).
9629
9630 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9631 needed, it would be wrong due to missing DW_AT_producer there.
9632
9633 Still one can confuse GDB by using non-standard GCC compilation
9634 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9635 */
9636 if (cu->has_loclist && gcc_4_minor >= 5)
9637 cust->locations_valid = 1;
9638
9639 if (gcc_4_minor >= 5)
9640 cust->epilogue_unwind_valid = 1;
9641
9642 cust->call_site_htab = cu->call_site_htab;
9643 }
9644
9645 if (dwarf2_per_objfile->using_index)
9646 per_cu->v.quick->compunit_symtab = cust;
9647 else
9648 {
9649 dwarf2_psymtab *pst = per_cu->v.psymtab;
9650 pst->compunit_symtab = cust;
9651 pst->readin = true;
9652 }
9653
9654 /* Push it for inclusion processing later. */
9655 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9656
9657 /* Not needed any more. */
9658 cu->reset_builder ();
9659 }
9660
9661 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9662 already been loaded into memory. */
9663
9664 static void
9665 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9666 enum language pretend_language)
9667 {
9668 struct dwarf2_cu *cu = per_cu->cu;
9669 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9670 struct objfile *objfile = dwarf2_per_objfile->objfile;
9671 struct compunit_symtab *cust;
9672 struct signatured_type *sig_type;
9673
9674 gdb_assert (per_cu->is_debug_types);
9675 sig_type = (struct signatured_type *) per_cu;
9676
9677 /* Clear the list here in case something was left over. */
9678 cu->method_list.clear ();
9679
9680 cu->language = pretend_language;
9681 cu->language_defn = language_def (cu->language);
9682
9683 /* The symbol tables are set up in read_type_unit_scope. */
9684 process_die (cu->dies, cu);
9685
9686 /* For now fudge the Go package. */
9687 if (cu->language == language_go)
9688 fixup_go_packaging (cu);
9689
9690 /* Now that we have processed all the DIEs in the CU, all the types
9691 should be complete, and it should now be safe to compute all of the
9692 physnames. */
9693 compute_delayed_physnames (cu);
9694
9695 if (cu->language == language_rust)
9696 rust_union_quirks (cu);
9697
9698 /* TUs share symbol tables.
9699 If this is the first TU to use this symtab, complete the construction
9700 of it with end_expandable_symtab. Otherwise, complete the addition of
9701 this TU's symbols to the existing symtab. */
9702 if (sig_type->type_unit_group->compunit_symtab == NULL)
9703 {
9704 buildsym_compunit *builder = cu->get_builder ();
9705 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9706 sig_type->type_unit_group->compunit_symtab = cust;
9707
9708 if (cust != NULL)
9709 {
9710 /* Set symtab language to language from DW_AT_language. If the
9711 compilation is from a C file generated by language preprocessors,
9712 do not set the language if it was already deduced by
9713 start_subfile. */
9714 if (!(cu->language == language_c
9715 && COMPUNIT_FILETABS (cust)->language != language_c))
9716 COMPUNIT_FILETABS (cust)->language = cu->language;
9717 }
9718 }
9719 else
9720 {
9721 cu->get_builder ()->augment_type_symtab ();
9722 cust = sig_type->type_unit_group->compunit_symtab;
9723 }
9724
9725 if (dwarf2_per_objfile->using_index)
9726 per_cu->v.quick->compunit_symtab = cust;
9727 else
9728 {
9729 dwarf2_psymtab *pst = per_cu->v.psymtab;
9730 pst->compunit_symtab = cust;
9731 pst->readin = true;
9732 }
9733
9734 /* Not needed any more. */
9735 cu->reset_builder ();
9736 }
9737
9738 /* Process an imported unit DIE. */
9739
9740 static void
9741 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9742 {
9743 struct attribute *attr;
9744
9745 /* For now we don't handle imported units in type units. */
9746 if (cu->per_cu->is_debug_types)
9747 {
9748 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9749 " supported in type units [in module %s]"),
9750 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9751 }
9752
9753 attr = dwarf2_attr (die, DW_AT_import, cu);
9754 if (attr != NULL)
9755 {
9756 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9757 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9758 dwarf2_per_cu_data *per_cu
9759 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9760 cu->per_cu->dwarf2_per_objfile);
9761
9762 /* If necessary, add it to the queue and load its DIEs. */
9763 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9764 load_full_comp_unit (per_cu, false, cu->language);
9765
9766 cu->per_cu->imported_symtabs_push (per_cu);
9767 }
9768 }
9769
9770 /* RAII object that represents a process_die scope: i.e.,
9771 starts/finishes processing a DIE. */
9772 class process_die_scope
9773 {
9774 public:
9775 process_die_scope (die_info *die, dwarf2_cu *cu)
9776 : m_die (die), m_cu (cu)
9777 {
9778 /* We should only be processing DIEs not already in process. */
9779 gdb_assert (!m_die->in_process);
9780 m_die->in_process = true;
9781 }
9782
9783 ~process_die_scope ()
9784 {
9785 m_die->in_process = false;
9786
9787 /* If we're done processing the DIE for the CU that owns the line
9788 header, we don't need the line header anymore. */
9789 if (m_cu->line_header_die_owner == m_die)
9790 {
9791 delete m_cu->line_header;
9792 m_cu->line_header = NULL;
9793 m_cu->line_header_die_owner = NULL;
9794 }
9795 }
9796
9797 private:
9798 die_info *m_die;
9799 dwarf2_cu *m_cu;
9800 };
9801
9802 /* Process a die and its children. */
9803
9804 static void
9805 process_die (struct die_info *die, struct dwarf2_cu *cu)
9806 {
9807 process_die_scope scope (die, cu);
9808
9809 switch (die->tag)
9810 {
9811 case DW_TAG_padding:
9812 break;
9813 case DW_TAG_compile_unit:
9814 case DW_TAG_partial_unit:
9815 read_file_scope (die, cu);
9816 break;
9817 case DW_TAG_type_unit:
9818 read_type_unit_scope (die, cu);
9819 break;
9820 case DW_TAG_subprogram:
9821 /* Nested subprograms in Fortran get a prefix. */
9822 if (cu->language == language_fortran
9823 && die->parent != NULL
9824 && die->parent->tag == DW_TAG_subprogram)
9825 cu->processing_has_namespace_info = true;
9826 /* Fall through. */
9827 case DW_TAG_inlined_subroutine:
9828 read_func_scope (die, cu);
9829 break;
9830 case DW_TAG_lexical_block:
9831 case DW_TAG_try_block:
9832 case DW_TAG_catch_block:
9833 read_lexical_block_scope (die, cu);
9834 break;
9835 case DW_TAG_call_site:
9836 case DW_TAG_GNU_call_site:
9837 read_call_site_scope (die, cu);
9838 break;
9839 case DW_TAG_class_type:
9840 case DW_TAG_interface_type:
9841 case DW_TAG_structure_type:
9842 case DW_TAG_union_type:
9843 process_structure_scope (die, cu);
9844 break;
9845 case DW_TAG_enumeration_type:
9846 process_enumeration_scope (die, cu);
9847 break;
9848
9849 /* These dies have a type, but processing them does not create
9850 a symbol or recurse to process the children. Therefore we can
9851 read them on-demand through read_type_die. */
9852 case DW_TAG_subroutine_type:
9853 case DW_TAG_set_type:
9854 case DW_TAG_array_type:
9855 case DW_TAG_pointer_type:
9856 case DW_TAG_ptr_to_member_type:
9857 case DW_TAG_reference_type:
9858 case DW_TAG_rvalue_reference_type:
9859 case DW_TAG_string_type:
9860 break;
9861
9862 case DW_TAG_base_type:
9863 case DW_TAG_subrange_type:
9864 case DW_TAG_typedef:
9865 /* Add a typedef symbol for the type definition, if it has a
9866 DW_AT_name. */
9867 new_symbol (die, read_type_die (die, cu), cu);
9868 break;
9869 case DW_TAG_common_block:
9870 read_common_block (die, cu);
9871 break;
9872 case DW_TAG_common_inclusion:
9873 break;
9874 case DW_TAG_namespace:
9875 cu->processing_has_namespace_info = true;
9876 read_namespace (die, cu);
9877 break;
9878 case DW_TAG_module:
9879 cu->processing_has_namespace_info = true;
9880 read_module (die, cu);
9881 break;
9882 case DW_TAG_imported_declaration:
9883 cu->processing_has_namespace_info = true;
9884 if (read_namespace_alias (die, cu))
9885 break;
9886 /* The declaration is not a global namespace alias. */
9887 /* Fall through. */
9888 case DW_TAG_imported_module:
9889 cu->processing_has_namespace_info = true;
9890 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9891 || cu->language != language_fortran))
9892 complaint (_("Tag '%s' has unexpected children"),
9893 dwarf_tag_name (die->tag));
9894 read_import_statement (die, cu);
9895 break;
9896
9897 case DW_TAG_imported_unit:
9898 process_imported_unit_die (die, cu);
9899 break;
9900
9901 case DW_TAG_variable:
9902 read_variable (die, cu);
9903 break;
9904
9905 default:
9906 new_symbol (die, NULL, cu);
9907 break;
9908 }
9909 }
9910 \f
9911 /* DWARF name computation. */
9912
9913 /* A helper function for dwarf2_compute_name which determines whether DIE
9914 needs to have the name of the scope prepended to the name listed in the
9915 die. */
9916
9917 static int
9918 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9919 {
9920 struct attribute *attr;
9921
9922 switch (die->tag)
9923 {
9924 case DW_TAG_namespace:
9925 case DW_TAG_typedef:
9926 case DW_TAG_class_type:
9927 case DW_TAG_interface_type:
9928 case DW_TAG_structure_type:
9929 case DW_TAG_union_type:
9930 case DW_TAG_enumeration_type:
9931 case DW_TAG_enumerator:
9932 case DW_TAG_subprogram:
9933 case DW_TAG_inlined_subroutine:
9934 case DW_TAG_member:
9935 case DW_TAG_imported_declaration:
9936 return 1;
9937
9938 case DW_TAG_variable:
9939 case DW_TAG_constant:
9940 /* We only need to prefix "globally" visible variables. These include
9941 any variable marked with DW_AT_external or any variable that
9942 lives in a namespace. [Variables in anonymous namespaces
9943 require prefixing, but they are not DW_AT_external.] */
9944
9945 if (dwarf2_attr (die, DW_AT_specification, cu))
9946 {
9947 struct dwarf2_cu *spec_cu = cu;
9948
9949 return die_needs_namespace (die_specification (die, &spec_cu),
9950 spec_cu);
9951 }
9952
9953 attr = dwarf2_attr (die, DW_AT_external, cu);
9954 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9955 && die->parent->tag != DW_TAG_module)
9956 return 0;
9957 /* A variable in a lexical block of some kind does not need a
9958 namespace, even though in C++ such variables may be external
9959 and have a mangled name. */
9960 if (die->parent->tag == DW_TAG_lexical_block
9961 || die->parent->tag == DW_TAG_try_block
9962 || die->parent->tag == DW_TAG_catch_block
9963 || die->parent->tag == DW_TAG_subprogram)
9964 return 0;
9965 return 1;
9966
9967 default:
9968 return 0;
9969 }
9970 }
9971
9972 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9973 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9974 defined for the given DIE. */
9975
9976 static struct attribute *
9977 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9978 {
9979 struct attribute *attr;
9980
9981 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9982 if (attr == NULL)
9983 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9984
9985 return attr;
9986 }
9987
9988 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9989 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9990 defined for the given DIE. */
9991
9992 static const char *
9993 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9994 {
9995 const char *linkage_name;
9996
9997 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9998 if (linkage_name == NULL)
9999 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10000
10001 return linkage_name;
10002 }
10003
10004 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10005 compute the physname for the object, which include a method's:
10006 - formal parameters (C++),
10007 - receiver type (Go),
10008
10009 The term "physname" is a bit confusing.
10010 For C++, for example, it is the demangled name.
10011 For Go, for example, it's the mangled name.
10012
10013 For Ada, return the DIE's linkage name rather than the fully qualified
10014 name. PHYSNAME is ignored..
10015
10016 The result is allocated on the objfile_obstack and canonicalized. */
10017
10018 static const char *
10019 dwarf2_compute_name (const char *name,
10020 struct die_info *die, struct dwarf2_cu *cu,
10021 int physname)
10022 {
10023 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10024
10025 if (name == NULL)
10026 name = dwarf2_name (die, cu);
10027
10028 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10029 but otherwise compute it by typename_concat inside GDB.
10030 FIXME: Actually this is not really true, or at least not always true.
10031 It's all very confusing. compute_and_set_names doesn't try to demangle
10032 Fortran names because there is no mangling standard. So new_symbol
10033 will set the demangled name to the result of dwarf2_full_name, and it is
10034 the demangled name that GDB uses if it exists. */
10035 if (cu->language == language_ada
10036 || (cu->language == language_fortran && physname))
10037 {
10038 /* For Ada unit, we prefer the linkage name over the name, as
10039 the former contains the exported name, which the user expects
10040 to be able to reference. Ideally, we want the user to be able
10041 to reference this entity using either natural or linkage name,
10042 but we haven't started looking at this enhancement yet. */
10043 const char *linkage_name = dw2_linkage_name (die, cu);
10044
10045 if (linkage_name != NULL)
10046 return linkage_name;
10047 }
10048
10049 /* These are the only languages we know how to qualify names in. */
10050 if (name != NULL
10051 && (cu->language == language_cplus
10052 || cu->language == language_fortran || cu->language == language_d
10053 || cu->language == language_rust))
10054 {
10055 if (die_needs_namespace (die, cu))
10056 {
10057 const char *prefix;
10058 const char *canonical_name = NULL;
10059
10060 string_file buf;
10061
10062 prefix = determine_prefix (die, cu);
10063 if (*prefix != '\0')
10064 {
10065 gdb::unique_xmalloc_ptr<char> prefixed_name
10066 (typename_concat (NULL, prefix, name, physname, cu));
10067
10068 buf.puts (prefixed_name.get ());
10069 }
10070 else
10071 buf.puts (name);
10072
10073 /* Template parameters may be specified in the DIE's DW_AT_name, or
10074 as children with DW_TAG_template_type_param or
10075 DW_TAG_value_type_param. If the latter, add them to the name
10076 here. If the name already has template parameters, then
10077 skip this step; some versions of GCC emit both, and
10078 it is more efficient to use the pre-computed name.
10079
10080 Something to keep in mind about this process: it is very
10081 unlikely, or in some cases downright impossible, to produce
10082 something that will match the mangled name of a function.
10083 If the definition of the function has the same debug info,
10084 we should be able to match up with it anyway. But fallbacks
10085 using the minimal symbol, for instance to find a method
10086 implemented in a stripped copy of libstdc++, will not work.
10087 If we do not have debug info for the definition, we will have to
10088 match them up some other way.
10089
10090 When we do name matching there is a related problem with function
10091 templates; two instantiated function templates are allowed to
10092 differ only by their return types, which we do not add here. */
10093
10094 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10095 {
10096 struct attribute *attr;
10097 struct die_info *child;
10098 int first = 1;
10099
10100 die->building_fullname = 1;
10101
10102 for (child = die->child; child != NULL; child = child->sibling)
10103 {
10104 struct type *type;
10105 LONGEST value;
10106 const gdb_byte *bytes;
10107 struct dwarf2_locexpr_baton *baton;
10108 struct value *v;
10109
10110 if (child->tag != DW_TAG_template_type_param
10111 && child->tag != DW_TAG_template_value_param)
10112 continue;
10113
10114 if (first)
10115 {
10116 buf.puts ("<");
10117 first = 0;
10118 }
10119 else
10120 buf.puts (", ");
10121
10122 attr = dwarf2_attr (child, DW_AT_type, cu);
10123 if (attr == NULL)
10124 {
10125 complaint (_("template parameter missing DW_AT_type"));
10126 buf.puts ("UNKNOWN_TYPE");
10127 continue;
10128 }
10129 type = die_type (child, cu);
10130
10131 if (child->tag == DW_TAG_template_type_param)
10132 {
10133 c_print_type (type, "", &buf, -1, 0, cu->language,
10134 &type_print_raw_options);
10135 continue;
10136 }
10137
10138 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10139 if (attr == NULL)
10140 {
10141 complaint (_("template parameter missing "
10142 "DW_AT_const_value"));
10143 buf.puts ("UNKNOWN_VALUE");
10144 continue;
10145 }
10146
10147 dwarf2_const_value_attr (attr, type, name,
10148 &cu->comp_unit_obstack, cu,
10149 &value, &bytes, &baton);
10150
10151 if (TYPE_NOSIGN (type))
10152 /* GDB prints characters as NUMBER 'CHAR'. If that's
10153 changed, this can use value_print instead. */
10154 c_printchar (value, type, &buf);
10155 else
10156 {
10157 struct value_print_options opts;
10158
10159 if (baton != NULL)
10160 v = dwarf2_evaluate_loc_desc (type, NULL,
10161 baton->data,
10162 baton->size,
10163 baton->per_cu);
10164 else if (bytes != NULL)
10165 {
10166 v = allocate_value (type);
10167 memcpy (value_contents_writeable (v), bytes,
10168 TYPE_LENGTH (type));
10169 }
10170 else
10171 v = value_from_longest (type, value);
10172
10173 /* Specify decimal so that we do not depend on
10174 the radix. */
10175 get_formatted_print_options (&opts, 'd');
10176 opts.raw = 1;
10177 value_print (v, &buf, &opts);
10178 release_value (v);
10179 }
10180 }
10181
10182 die->building_fullname = 0;
10183
10184 if (!first)
10185 {
10186 /* Close the argument list, with a space if necessary
10187 (nested templates). */
10188 if (!buf.empty () && buf.string ().back () == '>')
10189 buf.puts (" >");
10190 else
10191 buf.puts (">");
10192 }
10193 }
10194
10195 /* For C++ methods, append formal parameter type
10196 information, if PHYSNAME. */
10197
10198 if (physname && die->tag == DW_TAG_subprogram
10199 && cu->language == language_cplus)
10200 {
10201 struct type *type = read_type_die (die, cu);
10202
10203 c_type_print_args (type, &buf, 1, cu->language,
10204 &type_print_raw_options);
10205
10206 if (cu->language == language_cplus)
10207 {
10208 /* Assume that an artificial first parameter is
10209 "this", but do not crash if it is not. RealView
10210 marks unnamed (and thus unused) parameters as
10211 artificial; there is no way to differentiate
10212 the two cases. */
10213 if (TYPE_NFIELDS (type) > 0
10214 && TYPE_FIELD_ARTIFICIAL (type, 0)
10215 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10216 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10217 0))))
10218 buf.puts (" const");
10219 }
10220 }
10221
10222 const std::string &intermediate_name = buf.string ();
10223
10224 if (cu->language == language_cplus)
10225 canonical_name
10226 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10227 objfile);
10228
10229 /* If we only computed INTERMEDIATE_NAME, or if
10230 INTERMEDIATE_NAME is already canonical, then we need to
10231 intern it. */
10232 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10233 name = objfile->intern (intermediate_name);
10234 else
10235 name = canonical_name;
10236 }
10237 }
10238
10239 return name;
10240 }
10241
10242 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10243 If scope qualifiers are appropriate they will be added. The result
10244 will be allocated on the storage_obstack, or NULL if the DIE does
10245 not have a name. NAME may either be from a previous call to
10246 dwarf2_name or NULL.
10247
10248 The output string will be canonicalized (if C++). */
10249
10250 static const char *
10251 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10252 {
10253 return dwarf2_compute_name (name, die, cu, 0);
10254 }
10255
10256 /* Construct a physname for the given DIE in CU. NAME may either be
10257 from a previous call to dwarf2_name or NULL. The result will be
10258 allocated on the objfile_objstack or NULL if the DIE does not have a
10259 name.
10260
10261 The output string will be canonicalized (if C++). */
10262
10263 static const char *
10264 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10265 {
10266 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10267 const char *retval, *mangled = NULL, *canon = NULL;
10268 int need_copy = 1;
10269
10270 /* In this case dwarf2_compute_name is just a shortcut not building anything
10271 on its own. */
10272 if (!die_needs_namespace (die, cu))
10273 return dwarf2_compute_name (name, die, cu, 1);
10274
10275 mangled = dw2_linkage_name (die, cu);
10276
10277 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10278 See https://github.com/rust-lang/rust/issues/32925. */
10279 if (cu->language == language_rust && mangled != NULL
10280 && strchr (mangled, '{') != NULL)
10281 mangled = NULL;
10282
10283 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10284 has computed. */
10285 gdb::unique_xmalloc_ptr<char> demangled;
10286 if (mangled != NULL)
10287 {
10288
10289 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10290 {
10291 /* Do nothing (do not demangle the symbol name). */
10292 }
10293 else if (cu->language == language_go)
10294 {
10295 /* This is a lie, but we already lie to the caller new_symbol.
10296 new_symbol assumes we return the mangled name.
10297 This just undoes that lie until things are cleaned up. */
10298 }
10299 else
10300 {
10301 /* Use DMGL_RET_DROP for C++ template functions to suppress
10302 their return type. It is easier for GDB users to search
10303 for such functions as `name(params)' than `long name(params)'.
10304 In such case the minimal symbol names do not match the full
10305 symbol names but for template functions there is never a need
10306 to look up their definition from their declaration so
10307 the only disadvantage remains the minimal symbol variant
10308 `long name(params)' does not have the proper inferior type. */
10309 demangled.reset (gdb_demangle (mangled,
10310 (DMGL_PARAMS | DMGL_ANSI
10311 | DMGL_RET_DROP)));
10312 }
10313 if (demangled)
10314 canon = demangled.get ();
10315 else
10316 {
10317 canon = mangled;
10318 need_copy = 0;
10319 }
10320 }
10321
10322 if (canon == NULL || check_physname)
10323 {
10324 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10325
10326 if (canon != NULL && strcmp (physname, canon) != 0)
10327 {
10328 /* It may not mean a bug in GDB. The compiler could also
10329 compute DW_AT_linkage_name incorrectly. But in such case
10330 GDB would need to be bug-to-bug compatible. */
10331
10332 complaint (_("Computed physname <%s> does not match demangled <%s> "
10333 "(from linkage <%s>) - DIE at %s [in module %s]"),
10334 physname, canon, mangled, sect_offset_str (die->sect_off),
10335 objfile_name (objfile));
10336
10337 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10338 is available here - over computed PHYSNAME. It is safer
10339 against both buggy GDB and buggy compilers. */
10340
10341 retval = canon;
10342 }
10343 else
10344 {
10345 retval = physname;
10346 need_copy = 0;
10347 }
10348 }
10349 else
10350 retval = canon;
10351
10352 if (need_copy)
10353 retval = objfile->intern (retval);
10354
10355 return retval;
10356 }
10357
10358 /* Inspect DIE in CU for a namespace alias. If one exists, record
10359 a new symbol for it.
10360
10361 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10362
10363 static int
10364 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10365 {
10366 struct attribute *attr;
10367
10368 /* If the die does not have a name, this is not a namespace
10369 alias. */
10370 attr = dwarf2_attr (die, DW_AT_name, cu);
10371 if (attr != NULL)
10372 {
10373 int num;
10374 struct die_info *d = die;
10375 struct dwarf2_cu *imported_cu = cu;
10376
10377 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10378 keep inspecting DIEs until we hit the underlying import. */
10379 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10380 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10381 {
10382 attr = dwarf2_attr (d, DW_AT_import, cu);
10383 if (attr == NULL)
10384 break;
10385
10386 d = follow_die_ref (d, attr, &imported_cu);
10387 if (d->tag != DW_TAG_imported_declaration)
10388 break;
10389 }
10390
10391 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10392 {
10393 complaint (_("DIE at %s has too many recursively imported "
10394 "declarations"), sect_offset_str (d->sect_off));
10395 return 0;
10396 }
10397
10398 if (attr != NULL)
10399 {
10400 struct type *type;
10401 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10402
10403 type = get_die_type_at_offset (sect_off, cu->per_cu);
10404 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10405 {
10406 /* This declaration is a global namespace alias. Add
10407 a symbol for it whose type is the aliased namespace. */
10408 new_symbol (die, type, cu);
10409 return 1;
10410 }
10411 }
10412 }
10413
10414 return 0;
10415 }
10416
10417 /* Return the using directives repository (global or local?) to use in the
10418 current context for CU.
10419
10420 For Ada, imported declarations can materialize renamings, which *may* be
10421 global. However it is impossible (for now?) in DWARF to distinguish
10422 "external" imported declarations and "static" ones. As all imported
10423 declarations seem to be static in all other languages, make them all CU-wide
10424 global only in Ada. */
10425
10426 static struct using_direct **
10427 using_directives (struct dwarf2_cu *cu)
10428 {
10429 if (cu->language == language_ada
10430 && cu->get_builder ()->outermost_context_p ())
10431 return cu->get_builder ()->get_global_using_directives ();
10432 else
10433 return cu->get_builder ()->get_local_using_directives ();
10434 }
10435
10436 /* Read the import statement specified by the given die and record it. */
10437
10438 static void
10439 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10440 {
10441 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10442 struct attribute *import_attr;
10443 struct die_info *imported_die, *child_die;
10444 struct dwarf2_cu *imported_cu;
10445 const char *imported_name;
10446 const char *imported_name_prefix;
10447 const char *canonical_name;
10448 const char *import_alias;
10449 const char *imported_declaration = NULL;
10450 const char *import_prefix;
10451 std::vector<const char *> excludes;
10452
10453 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10454 if (import_attr == NULL)
10455 {
10456 complaint (_("Tag '%s' has no DW_AT_import"),
10457 dwarf_tag_name (die->tag));
10458 return;
10459 }
10460
10461 imported_cu = cu;
10462 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10463 imported_name = dwarf2_name (imported_die, imported_cu);
10464 if (imported_name == NULL)
10465 {
10466 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10467
10468 The import in the following code:
10469 namespace A
10470 {
10471 typedef int B;
10472 }
10473
10474 int main ()
10475 {
10476 using A::B;
10477 B b;
10478 return b;
10479 }
10480
10481 ...
10482 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10483 <52> DW_AT_decl_file : 1
10484 <53> DW_AT_decl_line : 6
10485 <54> DW_AT_import : <0x75>
10486 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10487 <59> DW_AT_name : B
10488 <5b> DW_AT_decl_file : 1
10489 <5c> DW_AT_decl_line : 2
10490 <5d> DW_AT_type : <0x6e>
10491 ...
10492 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10493 <76> DW_AT_byte_size : 4
10494 <77> DW_AT_encoding : 5 (signed)
10495
10496 imports the wrong die ( 0x75 instead of 0x58 ).
10497 This case will be ignored until the gcc bug is fixed. */
10498 return;
10499 }
10500
10501 /* Figure out the local name after import. */
10502 import_alias = dwarf2_name (die, cu);
10503
10504 /* Figure out where the statement is being imported to. */
10505 import_prefix = determine_prefix (die, cu);
10506
10507 /* Figure out what the scope of the imported die is and prepend it
10508 to the name of the imported die. */
10509 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10510
10511 if (imported_die->tag != DW_TAG_namespace
10512 && imported_die->tag != DW_TAG_module)
10513 {
10514 imported_declaration = imported_name;
10515 canonical_name = imported_name_prefix;
10516 }
10517 else if (strlen (imported_name_prefix) > 0)
10518 canonical_name = obconcat (&objfile->objfile_obstack,
10519 imported_name_prefix,
10520 (cu->language == language_d ? "." : "::"),
10521 imported_name, (char *) NULL);
10522 else
10523 canonical_name = imported_name;
10524
10525 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10526 for (child_die = die->child; child_die && child_die->tag;
10527 child_die = sibling_die (child_die))
10528 {
10529 /* DWARF-4: A Fortran use statement with a “rename list” may be
10530 represented by an imported module entry with an import attribute
10531 referring to the module and owned entries corresponding to those
10532 entities that are renamed as part of being imported. */
10533
10534 if (child_die->tag != DW_TAG_imported_declaration)
10535 {
10536 complaint (_("child DW_TAG_imported_declaration expected "
10537 "- DIE at %s [in module %s]"),
10538 sect_offset_str (child_die->sect_off),
10539 objfile_name (objfile));
10540 continue;
10541 }
10542
10543 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10544 if (import_attr == NULL)
10545 {
10546 complaint (_("Tag '%s' has no DW_AT_import"),
10547 dwarf_tag_name (child_die->tag));
10548 continue;
10549 }
10550
10551 imported_cu = cu;
10552 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10553 &imported_cu);
10554 imported_name = dwarf2_name (imported_die, imported_cu);
10555 if (imported_name == NULL)
10556 {
10557 complaint (_("child DW_TAG_imported_declaration has unknown "
10558 "imported name - DIE at %s [in module %s]"),
10559 sect_offset_str (child_die->sect_off),
10560 objfile_name (objfile));
10561 continue;
10562 }
10563
10564 excludes.push_back (imported_name);
10565
10566 process_die (child_die, cu);
10567 }
10568
10569 add_using_directive (using_directives (cu),
10570 import_prefix,
10571 canonical_name,
10572 import_alias,
10573 imported_declaration,
10574 excludes,
10575 0,
10576 &objfile->objfile_obstack);
10577 }
10578
10579 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10580 types, but gives them a size of zero. Starting with version 14,
10581 ICC is compatible with GCC. */
10582
10583 static bool
10584 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10585 {
10586 if (!cu->checked_producer)
10587 check_producer (cu);
10588
10589 return cu->producer_is_icc_lt_14;
10590 }
10591
10592 /* ICC generates a DW_AT_type for C void functions. This was observed on
10593 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10594 which says that void functions should not have a DW_AT_type. */
10595
10596 static bool
10597 producer_is_icc (struct dwarf2_cu *cu)
10598 {
10599 if (!cu->checked_producer)
10600 check_producer (cu);
10601
10602 return cu->producer_is_icc;
10603 }
10604
10605 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10606 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10607 this, it was first present in GCC release 4.3.0. */
10608
10609 static bool
10610 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10611 {
10612 if (!cu->checked_producer)
10613 check_producer (cu);
10614
10615 return cu->producer_is_gcc_lt_4_3;
10616 }
10617
10618 static file_and_directory
10619 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10620 {
10621 file_and_directory res;
10622
10623 /* Find the filename. Do not use dwarf2_name here, since the filename
10624 is not a source language identifier. */
10625 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10626 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10627
10628 if (res.comp_dir == NULL
10629 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10630 && IS_ABSOLUTE_PATH (res.name))
10631 {
10632 res.comp_dir_storage = ldirname (res.name);
10633 if (!res.comp_dir_storage.empty ())
10634 res.comp_dir = res.comp_dir_storage.c_str ();
10635 }
10636 if (res.comp_dir != NULL)
10637 {
10638 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10639 directory, get rid of it. */
10640 const char *cp = strchr (res.comp_dir, ':');
10641
10642 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10643 res.comp_dir = cp + 1;
10644 }
10645
10646 if (res.name == NULL)
10647 res.name = "<unknown>";
10648
10649 return res;
10650 }
10651
10652 /* Handle DW_AT_stmt_list for a compilation unit.
10653 DIE is the DW_TAG_compile_unit die for CU.
10654 COMP_DIR is the compilation directory. LOWPC is passed to
10655 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10656
10657 static void
10658 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10659 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10660 {
10661 struct dwarf2_per_objfile *dwarf2_per_objfile
10662 = cu->per_cu->dwarf2_per_objfile;
10663 struct attribute *attr;
10664 struct line_header line_header_local;
10665 hashval_t line_header_local_hash;
10666 void **slot;
10667 int decode_mapping;
10668
10669 gdb_assert (! cu->per_cu->is_debug_types);
10670
10671 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10672 if (attr == NULL)
10673 return;
10674
10675 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10676
10677 /* The line header hash table is only created if needed (it exists to
10678 prevent redundant reading of the line table for partial_units).
10679 If we're given a partial_unit, we'll need it. If we're given a
10680 compile_unit, then use the line header hash table if it's already
10681 created, but don't create one just yet. */
10682
10683 if (dwarf2_per_objfile->line_header_hash == NULL
10684 && die->tag == DW_TAG_partial_unit)
10685 {
10686 dwarf2_per_objfile->line_header_hash
10687 .reset (htab_create_alloc (127, line_header_hash_voidp,
10688 line_header_eq_voidp,
10689 free_line_header_voidp,
10690 xcalloc, xfree));
10691 }
10692
10693 line_header_local.sect_off = line_offset;
10694 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10695 line_header_local_hash = line_header_hash (&line_header_local);
10696 if (dwarf2_per_objfile->line_header_hash != NULL)
10697 {
10698 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10699 &line_header_local,
10700 line_header_local_hash, NO_INSERT);
10701
10702 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10703 is not present in *SLOT (since if there is something in *SLOT then
10704 it will be for a partial_unit). */
10705 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10706 {
10707 gdb_assert (*slot != NULL);
10708 cu->line_header = (struct line_header *) *slot;
10709 return;
10710 }
10711 }
10712
10713 /* dwarf_decode_line_header does not yet provide sufficient information.
10714 We always have to call also dwarf_decode_lines for it. */
10715 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10716 if (lh == NULL)
10717 return;
10718
10719 cu->line_header = lh.release ();
10720 cu->line_header_die_owner = die;
10721
10722 if (dwarf2_per_objfile->line_header_hash == NULL)
10723 slot = NULL;
10724 else
10725 {
10726 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10727 &line_header_local,
10728 line_header_local_hash, INSERT);
10729 gdb_assert (slot != NULL);
10730 }
10731 if (slot != NULL && *slot == NULL)
10732 {
10733 /* This newly decoded line number information unit will be owned
10734 by line_header_hash hash table. */
10735 *slot = cu->line_header;
10736 cu->line_header_die_owner = NULL;
10737 }
10738 else
10739 {
10740 /* We cannot free any current entry in (*slot) as that struct line_header
10741 may be already used by multiple CUs. Create only temporary decoded
10742 line_header for this CU - it may happen at most once for each line
10743 number information unit. And if we're not using line_header_hash
10744 then this is what we want as well. */
10745 gdb_assert (die->tag != DW_TAG_partial_unit);
10746 }
10747 decode_mapping = (die->tag != DW_TAG_partial_unit);
10748 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10749 decode_mapping);
10750
10751 }
10752
10753 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10754
10755 static void
10756 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10757 {
10758 struct dwarf2_per_objfile *dwarf2_per_objfile
10759 = cu->per_cu->dwarf2_per_objfile;
10760 struct objfile *objfile = dwarf2_per_objfile->objfile;
10761 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10762 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10763 CORE_ADDR highpc = ((CORE_ADDR) 0);
10764 struct attribute *attr;
10765 struct die_info *child_die;
10766 CORE_ADDR baseaddr;
10767
10768 prepare_one_comp_unit (cu, die, cu->language);
10769 baseaddr = objfile->text_section_offset ();
10770
10771 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10772
10773 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10774 from finish_block. */
10775 if (lowpc == ((CORE_ADDR) -1))
10776 lowpc = highpc;
10777 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10778
10779 file_and_directory fnd = find_file_and_directory (die, cu);
10780
10781 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10782 standardised yet. As a workaround for the language detection we fall
10783 back to the DW_AT_producer string. */
10784 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10785 cu->language = language_opencl;
10786
10787 /* Similar hack for Go. */
10788 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10789 set_cu_language (DW_LANG_Go, cu);
10790
10791 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10792
10793 /* Decode line number information if present. We do this before
10794 processing child DIEs, so that the line header table is available
10795 for DW_AT_decl_file. */
10796 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10797
10798 /* Process all dies in compilation unit. */
10799 if (die->child != NULL)
10800 {
10801 child_die = die->child;
10802 while (child_die && child_die->tag)
10803 {
10804 process_die (child_die, cu);
10805 child_die = sibling_die (child_die);
10806 }
10807 }
10808
10809 /* Decode macro information, if present. Dwarf 2 macro information
10810 refers to information in the line number info statement program
10811 header, so we can only read it if we've read the header
10812 successfully. */
10813 attr = dwarf2_attr (die, DW_AT_macros, cu);
10814 if (attr == NULL)
10815 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10816 if (attr && cu->line_header)
10817 {
10818 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10819 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10820
10821 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10822 }
10823 else
10824 {
10825 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10826 if (attr && cu->line_header)
10827 {
10828 unsigned int macro_offset = DW_UNSND (attr);
10829
10830 dwarf_decode_macros (cu, macro_offset, 0);
10831 }
10832 }
10833 }
10834
10835 void
10836 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10837 {
10838 struct type_unit_group *tu_group;
10839 int first_time;
10840 struct attribute *attr;
10841 unsigned int i;
10842 struct signatured_type *sig_type;
10843
10844 gdb_assert (per_cu->is_debug_types);
10845 sig_type = (struct signatured_type *) per_cu;
10846
10847 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10848
10849 /* If we're using .gdb_index (includes -readnow) then
10850 per_cu->type_unit_group may not have been set up yet. */
10851 if (sig_type->type_unit_group == NULL)
10852 sig_type->type_unit_group = get_type_unit_group (this, attr);
10853 tu_group = sig_type->type_unit_group;
10854
10855 /* If we've already processed this stmt_list there's no real need to
10856 do it again, we could fake it and just recreate the part we need
10857 (file name,index -> symtab mapping). If data shows this optimization
10858 is useful we can do it then. */
10859 first_time = tu_group->compunit_symtab == NULL;
10860
10861 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10862 debug info. */
10863 line_header_up lh;
10864 if (attr != NULL)
10865 {
10866 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10867 lh = dwarf_decode_line_header (line_offset, this);
10868 }
10869 if (lh == NULL)
10870 {
10871 if (first_time)
10872 start_symtab ("", NULL, 0);
10873 else
10874 {
10875 gdb_assert (tu_group->symtabs == NULL);
10876 gdb_assert (m_builder == nullptr);
10877 struct compunit_symtab *cust = tu_group->compunit_symtab;
10878 m_builder.reset (new struct buildsym_compunit
10879 (COMPUNIT_OBJFILE (cust), "",
10880 COMPUNIT_DIRNAME (cust),
10881 compunit_language (cust),
10882 0, cust));
10883 }
10884 return;
10885 }
10886
10887 line_header = lh.release ();
10888 line_header_die_owner = die;
10889
10890 if (first_time)
10891 {
10892 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10893
10894 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10895 still initializing it, and our caller (a few levels up)
10896 process_full_type_unit still needs to know if this is the first
10897 time. */
10898
10899 tu_group->symtabs
10900 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10901 struct symtab *, line_header->file_names_size ());
10902
10903 auto &file_names = line_header->file_names ();
10904 for (i = 0; i < file_names.size (); ++i)
10905 {
10906 file_entry &fe = file_names[i];
10907 dwarf2_start_subfile (this, fe.name,
10908 fe.include_dir (line_header));
10909 buildsym_compunit *b = get_builder ();
10910 if (b->get_current_subfile ()->symtab == NULL)
10911 {
10912 /* NOTE: start_subfile will recognize when it's been
10913 passed a file it has already seen. So we can't
10914 assume there's a simple mapping from
10915 cu->line_header->file_names to subfiles, plus
10916 cu->line_header->file_names may contain dups. */
10917 b->get_current_subfile ()->symtab
10918 = allocate_symtab (cust, b->get_current_subfile ()->name);
10919 }
10920
10921 fe.symtab = b->get_current_subfile ()->symtab;
10922 tu_group->symtabs[i] = fe.symtab;
10923 }
10924 }
10925 else
10926 {
10927 gdb_assert (m_builder == nullptr);
10928 struct compunit_symtab *cust = tu_group->compunit_symtab;
10929 m_builder.reset (new struct buildsym_compunit
10930 (COMPUNIT_OBJFILE (cust), "",
10931 COMPUNIT_DIRNAME (cust),
10932 compunit_language (cust),
10933 0, cust));
10934
10935 auto &file_names = line_header->file_names ();
10936 for (i = 0; i < file_names.size (); ++i)
10937 {
10938 file_entry &fe = file_names[i];
10939 fe.symtab = tu_group->symtabs[i];
10940 }
10941 }
10942
10943 /* The main symtab is allocated last. Type units don't have DW_AT_name
10944 so they don't have a "real" (so to speak) symtab anyway.
10945 There is later code that will assign the main symtab to all symbols
10946 that don't have one. We need to handle the case of a symbol with a
10947 missing symtab (DW_AT_decl_file) anyway. */
10948 }
10949
10950 /* Process DW_TAG_type_unit.
10951 For TUs we want to skip the first top level sibling if it's not the
10952 actual type being defined by this TU. In this case the first top
10953 level sibling is there to provide context only. */
10954
10955 static void
10956 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10957 {
10958 struct die_info *child_die;
10959
10960 prepare_one_comp_unit (cu, die, language_minimal);
10961
10962 /* Initialize (or reinitialize) the machinery for building symtabs.
10963 We do this before processing child DIEs, so that the line header table
10964 is available for DW_AT_decl_file. */
10965 cu->setup_type_unit_groups (die);
10966
10967 if (die->child != NULL)
10968 {
10969 child_die = die->child;
10970 while (child_die && child_die->tag)
10971 {
10972 process_die (child_die, cu);
10973 child_die = sibling_die (child_die);
10974 }
10975 }
10976 }
10977 \f
10978 /* DWO/DWP files.
10979
10980 http://gcc.gnu.org/wiki/DebugFission
10981 http://gcc.gnu.org/wiki/DebugFissionDWP
10982
10983 To simplify handling of both DWO files ("object" files with the DWARF info)
10984 and DWP files (a file with the DWOs packaged up into one file), we treat
10985 DWP files as having a collection of virtual DWO files. */
10986
10987 static hashval_t
10988 hash_dwo_file (const void *item)
10989 {
10990 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10991 hashval_t hash;
10992
10993 hash = htab_hash_string (dwo_file->dwo_name);
10994 if (dwo_file->comp_dir != NULL)
10995 hash += htab_hash_string (dwo_file->comp_dir);
10996 return hash;
10997 }
10998
10999 static int
11000 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11001 {
11002 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11003 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11004
11005 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11006 return 0;
11007 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11008 return lhs->comp_dir == rhs->comp_dir;
11009 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11010 }
11011
11012 /* Allocate a hash table for DWO files. */
11013
11014 static htab_up
11015 allocate_dwo_file_hash_table ()
11016 {
11017 auto delete_dwo_file = [] (void *item)
11018 {
11019 struct dwo_file *dwo_file = (struct dwo_file *) item;
11020
11021 delete dwo_file;
11022 };
11023
11024 return htab_up (htab_create_alloc (41,
11025 hash_dwo_file,
11026 eq_dwo_file,
11027 delete_dwo_file,
11028 xcalloc, xfree));
11029 }
11030
11031 /* Lookup DWO file DWO_NAME. */
11032
11033 static void **
11034 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11035 const char *dwo_name,
11036 const char *comp_dir)
11037 {
11038 struct dwo_file find_entry;
11039 void **slot;
11040
11041 if (dwarf2_per_objfile->dwo_files == NULL)
11042 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11043
11044 find_entry.dwo_name = dwo_name;
11045 find_entry.comp_dir = comp_dir;
11046 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11047 INSERT);
11048
11049 return slot;
11050 }
11051
11052 static hashval_t
11053 hash_dwo_unit (const void *item)
11054 {
11055 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11056
11057 /* This drops the top 32 bits of the id, but is ok for a hash. */
11058 return dwo_unit->signature;
11059 }
11060
11061 static int
11062 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11063 {
11064 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11065 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11066
11067 /* The signature is assumed to be unique within the DWO file.
11068 So while object file CU dwo_id's always have the value zero,
11069 that's OK, assuming each object file DWO file has only one CU,
11070 and that's the rule for now. */
11071 return lhs->signature == rhs->signature;
11072 }
11073
11074 /* Allocate a hash table for DWO CUs,TUs.
11075 There is one of these tables for each of CUs,TUs for each DWO file. */
11076
11077 static htab_up
11078 allocate_dwo_unit_table ()
11079 {
11080 /* Start out with a pretty small number.
11081 Generally DWO files contain only one CU and maybe some TUs. */
11082 return htab_up (htab_create_alloc (3,
11083 hash_dwo_unit,
11084 eq_dwo_unit,
11085 NULL, xcalloc, xfree));
11086 }
11087
11088 /* die_reader_func for create_dwo_cu. */
11089
11090 static void
11091 create_dwo_cu_reader (const struct die_reader_specs *reader,
11092 const gdb_byte *info_ptr,
11093 struct die_info *comp_unit_die,
11094 struct dwo_file *dwo_file,
11095 struct dwo_unit *dwo_unit)
11096 {
11097 struct dwarf2_cu *cu = reader->cu;
11098 sect_offset sect_off = cu->per_cu->sect_off;
11099 struct dwarf2_section_info *section = cu->per_cu->section;
11100
11101 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11102 if (!signature.has_value ())
11103 {
11104 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11105 " its dwo_id [in module %s]"),
11106 sect_offset_str (sect_off), dwo_file->dwo_name);
11107 return;
11108 }
11109
11110 dwo_unit->dwo_file = dwo_file;
11111 dwo_unit->signature = *signature;
11112 dwo_unit->section = section;
11113 dwo_unit->sect_off = sect_off;
11114 dwo_unit->length = cu->per_cu->length;
11115
11116 if (dwarf_read_debug)
11117 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11118 sect_offset_str (sect_off),
11119 hex_string (dwo_unit->signature));
11120 }
11121
11122 /* Create the dwo_units for the CUs in a DWO_FILE.
11123 Note: This function processes DWO files only, not DWP files. */
11124
11125 static void
11126 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11127 dwarf2_cu *cu, struct dwo_file &dwo_file,
11128 dwarf2_section_info &section, htab_up &cus_htab)
11129 {
11130 struct objfile *objfile = dwarf2_per_objfile->objfile;
11131 const gdb_byte *info_ptr, *end_ptr;
11132
11133 section.read (objfile);
11134 info_ptr = section.buffer;
11135
11136 if (info_ptr == NULL)
11137 return;
11138
11139 if (dwarf_read_debug)
11140 {
11141 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11142 section.get_name (),
11143 section.get_file_name ());
11144 }
11145
11146 end_ptr = info_ptr + section.size;
11147 while (info_ptr < end_ptr)
11148 {
11149 struct dwarf2_per_cu_data per_cu;
11150 struct dwo_unit read_unit {};
11151 struct dwo_unit *dwo_unit;
11152 void **slot;
11153 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11154
11155 memset (&per_cu, 0, sizeof (per_cu));
11156 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11157 per_cu.is_debug_types = 0;
11158 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11159 per_cu.section = &section;
11160
11161 cutu_reader reader (&per_cu, cu, &dwo_file);
11162 if (!reader.dummy_p)
11163 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11164 &dwo_file, &read_unit);
11165 info_ptr += per_cu.length;
11166
11167 // If the unit could not be parsed, skip it.
11168 if (read_unit.dwo_file == NULL)
11169 continue;
11170
11171 if (cus_htab == NULL)
11172 cus_htab = allocate_dwo_unit_table ();
11173
11174 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11175 *dwo_unit = read_unit;
11176 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11177 gdb_assert (slot != NULL);
11178 if (*slot != NULL)
11179 {
11180 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11181 sect_offset dup_sect_off = dup_cu->sect_off;
11182
11183 complaint (_("debug cu entry at offset %s is duplicate to"
11184 " the entry at offset %s, signature %s"),
11185 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11186 hex_string (dwo_unit->signature));
11187 }
11188 *slot = (void *)dwo_unit;
11189 }
11190 }
11191
11192 /* DWP file .debug_{cu,tu}_index section format:
11193 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11194
11195 DWP Version 1:
11196
11197 Both index sections have the same format, and serve to map a 64-bit
11198 signature to a set of section numbers. Each section begins with a header,
11199 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11200 indexes, and a pool of 32-bit section numbers. The index sections will be
11201 aligned at 8-byte boundaries in the file.
11202
11203 The index section header consists of:
11204
11205 V, 32 bit version number
11206 -, 32 bits unused
11207 N, 32 bit number of compilation units or type units in the index
11208 M, 32 bit number of slots in the hash table
11209
11210 Numbers are recorded using the byte order of the application binary.
11211
11212 The hash table begins at offset 16 in the section, and consists of an array
11213 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11214 order of the application binary). Unused slots in the hash table are 0.
11215 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11216
11217 The parallel table begins immediately after the hash table
11218 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11219 array of 32-bit indexes (using the byte order of the application binary),
11220 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11221 table contains a 32-bit index into the pool of section numbers. For unused
11222 hash table slots, the corresponding entry in the parallel table will be 0.
11223
11224 The pool of section numbers begins immediately following the hash table
11225 (at offset 16 + 12 * M from the beginning of the section). The pool of
11226 section numbers consists of an array of 32-bit words (using the byte order
11227 of the application binary). Each item in the array is indexed starting
11228 from 0. The hash table entry provides the index of the first section
11229 number in the set. Additional section numbers in the set follow, and the
11230 set is terminated by a 0 entry (section number 0 is not used in ELF).
11231
11232 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11233 section must be the first entry in the set, and the .debug_abbrev.dwo must
11234 be the second entry. Other members of the set may follow in any order.
11235
11236 ---
11237
11238 DWP Version 2:
11239
11240 DWP Version 2 combines all the .debug_info, etc. sections into one,
11241 and the entries in the index tables are now offsets into these sections.
11242 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11243 section.
11244
11245 Index Section Contents:
11246 Header
11247 Hash Table of Signatures dwp_hash_table.hash_table
11248 Parallel Table of Indices dwp_hash_table.unit_table
11249 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11250 Table of Section Sizes dwp_hash_table.v2.sizes
11251
11252 The index section header consists of:
11253
11254 V, 32 bit version number
11255 L, 32 bit number of columns in the table of section offsets
11256 N, 32 bit number of compilation units or type units in the index
11257 M, 32 bit number of slots in the hash table
11258
11259 Numbers are recorded using the byte order of the application binary.
11260
11261 The hash table has the same format as version 1.
11262 The parallel table of indices has the same format as version 1,
11263 except that the entries are origin-1 indices into the table of sections
11264 offsets and the table of section sizes.
11265
11266 The table of offsets begins immediately following the parallel table
11267 (at offset 16 + 12 * M from the beginning of the section). The table is
11268 a two-dimensional array of 32-bit words (using the byte order of the
11269 application binary), with L columns and N+1 rows, in row-major order.
11270 Each row in the array is indexed starting from 0. The first row provides
11271 a key to the remaining rows: each column in this row provides an identifier
11272 for a debug section, and the offsets in the same column of subsequent rows
11273 refer to that section. The section identifiers are:
11274
11275 DW_SECT_INFO 1 .debug_info.dwo
11276 DW_SECT_TYPES 2 .debug_types.dwo
11277 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11278 DW_SECT_LINE 4 .debug_line.dwo
11279 DW_SECT_LOC 5 .debug_loc.dwo
11280 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11281 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11282 DW_SECT_MACRO 8 .debug_macro.dwo
11283
11284 The offsets provided by the CU and TU index sections are the base offsets
11285 for the contributions made by each CU or TU to the corresponding section
11286 in the package file. Each CU and TU header contains an abbrev_offset
11287 field, used to find the abbreviations table for that CU or TU within the
11288 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11289 be interpreted as relative to the base offset given in the index section.
11290 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11291 should be interpreted as relative to the base offset for .debug_line.dwo,
11292 and offsets into other debug sections obtained from DWARF attributes should
11293 also be interpreted as relative to the corresponding base offset.
11294
11295 The table of sizes begins immediately following the table of offsets.
11296 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11297 with L columns and N rows, in row-major order. Each row in the array is
11298 indexed starting from 1 (row 0 is shared by the two tables).
11299
11300 ---
11301
11302 Hash table lookup is handled the same in version 1 and 2:
11303
11304 We assume that N and M will not exceed 2^32 - 1.
11305 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11306
11307 Given a 64-bit compilation unit signature or a type signature S, an entry
11308 in the hash table is located as follows:
11309
11310 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11311 the low-order k bits all set to 1.
11312
11313 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11314
11315 3) If the hash table entry at index H matches the signature, use that
11316 entry. If the hash table entry at index H is unused (all zeroes),
11317 terminate the search: the signature is not present in the table.
11318
11319 4) Let H = (H + H') modulo M. Repeat at Step 3.
11320
11321 Because M > N and H' and M are relatively prime, the search is guaranteed
11322 to stop at an unused slot or find the match. */
11323
11324 /* Create a hash table to map DWO IDs to their CU/TU entry in
11325 .debug_{info,types}.dwo in DWP_FILE.
11326 Returns NULL if there isn't one.
11327 Note: This function processes DWP files only, not DWO files. */
11328
11329 static struct dwp_hash_table *
11330 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11331 struct dwp_file *dwp_file, int is_debug_types)
11332 {
11333 struct objfile *objfile = dwarf2_per_objfile->objfile;
11334 bfd *dbfd = dwp_file->dbfd.get ();
11335 const gdb_byte *index_ptr, *index_end;
11336 struct dwarf2_section_info *index;
11337 uint32_t version, nr_columns, nr_units, nr_slots;
11338 struct dwp_hash_table *htab;
11339
11340 if (is_debug_types)
11341 index = &dwp_file->sections.tu_index;
11342 else
11343 index = &dwp_file->sections.cu_index;
11344
11345 if (index->empty ())
11346 return NULL;
11347 index->read (objfile);
11348
11349 index_ptr = index->buffer;
11350 index_end = index_ptr + index->size;
11351
11352 version = read_4_bytes (dbfd, index_ptr);
11353 index_ptr += 4;
11354 if (version == 2)
11355 nr_columns = read_4_bytes (dbfd, index_ptr);
11356 else
11357 nr_columns = 0;
11358 index_ptr += 4;
11359 nr_units = read_4_bytes (dbfd, index_ptr);
11360 index_ptr += 4;
11361 nr_slots = read_4_bytes (dbfd, index_ptr);
11362 index_ptr += 4;
11363
11364 if (version != 1 && version != 2)
11365 {
11366 error (_("Dwarf Error: unsupported DWP file version (%s)"
11367 " [in module %s]"),
11368 pulongest (version), dwp_file->name);
11369 }
11370 if (nr_slots != (nr_slots & -nr_slots))
11371 {
11372 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11373 " is not power of 2 [in module %s]"),
11374 pulongest (nr_slots), dwp_file->name);
11375 }
11376
11377 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11378 htab->version = version;
11379 htab->nr_columns = nr_columns;
11380 htab->nr_units = nr_units;
11381 htab->nr_slots = nr_slots;
11382 htab->hash_table = index_ptr;
11383 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11384
11385 /* Exit early if the table is empty. */
11386 if (nr_slots == 0 || nr_units == 0
11387 || (version == 2 && nr_columns == 0))
11388 {
11389 /* All must be zero. */
11390 if (nr_slots != 0 || nr_units != 0
11391 || (version == 2 && nr_columns != 0))
11392 {
11393 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11394 " all zero [in modules %s]"),
11395 dwp_file->name);
11396 }
11397 return htab;
11398 }
11399
11400 if (version == 1)
11401 {
11402 htab->section_pool.v1.indices =
11403 htab->unit_table + sizeof (uint32_t) * nr_slots;
11404 /* It's harder to decide whether the section is too small in v1.
11405 V1 is deprecated anyway so we punt. */
11406 }
11407 else
11408 {
11409 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11410 int *ids = htab->section_pool.v2.section_ids;
11411 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11412 /* Reverse map for error checking. */
11413 int ids_seen[DW_SECT_MAX + 1];
11414 int i;
11415
11416 if (nr_columns < 2)
11417 {
11418 error (_("Dwarf Error: bad DWP hash table, too few columns"
11419 " in section table [in module %s]"),
11420 dwp_file->name);
11421 }
11422 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11423 {
11424 error (_("Dwarf Error: bad DWP hash table, too many columns"
11425 " in section table [in module %s]"),
11426 dwp_file->name);
11427 }
11428 memset (ids, 255, sizeof_ids);
11429 memset (ids_seen, 255, sizeof (ids_seen));
11430 for (i = 0; i < nr_columns; ++i)
11431 {
11432 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11433
11434 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11435 {
11436 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11437 " in section table [in module %s]"),
11438 id, dwp_file->name);
11439 }
11440 if (ids_seen[id] != -1)
11441 {
11442 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11443 " id %d in section table [in module %s]"),
11444 id, dwp_file->name);
11445 }
11446 ids_seen[id] = i;
11447 ids[i] = id;
11448 }
11449 /* Must have exactly one info or types section. */
11450 if (((ids_seen[DW_SECT_INFO] != -1)
11451 + (ids_seen[DW_SECT_TYPES] != -1))
11452 != 1)
11453 {
11454 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11455 " DWO info/types section [in module %s]"),
11456 dwp_file->name);
11457 }
11458 /* Must have an abbrev section. */
11459 if (ids_seen[DW_SECT_ABBREV] == -1)
11460 {
11461 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11462 " section [in module %s]"),
11463 dwp_file->name);
11464 }
11465 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11466 htab->section_pool.v2.sizes =
11467 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11468 * nr_units * nr_columns);
11469 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11470 * nr_units * nr_columns))
11471 > index_end)
11472 {
11473 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11474 " [in module %s]"),
11475 dwp_file->name);
11476 }
11477 }
11478
11479 return htab;
11480 }
11481
11482 /* Update SECTIONS with the data from SECTP.
11483
11484 This function is like the other "locate" section routines that are
11485 passed to bfd_map_over_sections, but in this context the sections to
11486 read comes from the DWP V1 hash table, not the full ELF section table.
11487
11488 The result is non-zero for success, or zero if an error was found. */
11489
11490 static int
11491 locate_v1_virtual_dwo_sections (asection *sectp,
11492 struct virtual_v1_dwo_sections *sections)
11493 {
11494 const struct dwop_section_names *names = &dwop_section_names;
11495
11496 if (section_is_p (sectp->name, &names->abbrev_dwo))
11497 {
11498 /* There can be only one. */
11499 if (sections->abbrev.s.section != NULL)
11500 return 0;
11501 sections->abbrev.s.section = sectp;
11502 sections->abbrev.size = bfd_section_size (sectp);
11503 }
11504 else if (section_is_p (sectp->name, &names->info_dwo)
11505 || section_is_p (sectp->name, &names->types_dwo))
11506 {
11507 /* There can be only one. */
11508 if (sections->info_or_types.s.section != NULL)
11509 return 0;
11510 sections->info_or_types.s.section = sectp;
11511 sections->info_or_types.size = bfd_section_size (sectp);
11512 }
11513 else if (section_is_p (sectp->name, &names->line_dwo))
11514 {
11515 /* There can be only one. */
11516 if (sections->line.s.section != NULL)
11517 return 0;
11518 sections->line.s.section = sectp;
11519 sections->line.size = bfd_section_size (sectp);
11520 }
11521 else if (section_is_p (sectp->name, &names->loc_dwo))
11522 {
11523 /* There can be only one. */
11524 if (sections->loc.s.section != NULL)
11525 return 0;
11526 sections->loc.s.section = sectp;
11527 sections->loc.size = bfd_section_size (sectp);
11528 }
11529 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11530 {
11531 /* There can be only one. */
11532 if (sections->macinfo.s.section != NULL)
11533 return 0;
11534 sections->macinfo.s.section = sectp;
11535 sections->macinfo.size = bfd_section_size (sectp);
11536 }
11537 else if (section_is_p (sectp->name, &names->macro_dwo))
11538 {
11539 /* There can be only one. */
11540 if (sections->macro.s.section != NULL)
11541 return 0;
11542 sections->macro.s.section = sectp;
11543 sections->macro.size = bfd_section_size (sectp);
11544 }
11545 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11546 {
11547 /* There can be only one. */
11548 if (sections->str_offsets.s.section != NULL)
11549 return 0;
11550 sections->str_offsets.s.section = sectp;
11551 sections->str_offsets.size = bfd_section_size (sectp);
11552 }
11553 else
11554 {
11555 /* No other kind of section is valid. */
11556 return 0;
11557 }
11558
11559 return 1;
11560 }
11561
11562 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11563 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11564 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11565 This is for DWP version 1 files. */
11566
11567 static struct dwo_unit *
11568 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11569 struct dwp_file *dwp_file,
11570 uint32_t unit_index,
11571 const char *comp_dir,
11572 ULONGEST signature, int is_debug_types)
11573 {
11574 struct objfile *objfile = dwarf2_per_objfile->objfile;
11575 const struct dwp_hash_table *dwp_htab =
11576 is_debug_types ? dwp_file->tus : dwp_file->cus;
11577 bfd *dbfd = dwp_file->dbfd.get ();
11578 const char *kind = is_debug_types ? "TU" : "CU";
11579 struct dwo_file *dwo_file;
11580 struct dwo_unit *dwo_unit;
11581 struct virtual_v1_dwo_sections sections;
11582 void **dwo_file_slot;
11583 int i;
11584
11585 gdb_assert (dwp_file->version == 1);
11586
11587 if (dwarf_read_debug)
11588 {
11589 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11590 kind,
11591 pulongest (unit_index), hex_string (signature),
11592 dwp_file->name);
11593 }
11594
11595 /* Fetch the sections of this DWO unit.
11596 Put a limit on the number of sections we look for so that bad data
11597 doesn't cause us to loop forever. */
11598
11599 #define MAX_NR_V1_DWO_SECTIONS \
11600 (1 /* .debug_info or .debug_types */ \
11601 + 1 /* .debug_abbrev */ \
11602 + 1 /* .debug_line */ \
11603 + 1 /* .debug_loc */ \
11604 + 1 /* .debug_str_offsets */ \
11605 + 1 /* .debug_macro or .debug_macinfo */ \
11606 + 1 /* trailing zero */)
11607
11608 memset (&sections, 0, sizeof (sections));
11609
11610 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11611 {
11612 asection *sectp;
11613 uint32_t section_nr =
11614 read_4_bytes (dbfd,
11615 dwp_htab->section_pool.v1.indices
11616 + (unit_index + i) * sizeof (uint32_t));
11617
11618 if (section_nr == 0)
11619 break;
11620 if (section_nr >= dwp_file->num_sections)
11621 {
11622 error (_("Dwarf Error: bad DWP hash table, section number too large"
11623 " [in module %s]"),
11624 dwp_file->name);
11625 }
11626
11627 sectp = dwp_file->elf_sections[section_nr];
11628 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11629 {
11630 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11631 " [in module %s]"),
11632 dwp_file->name);
11633 }
11634 }
11635
11636 if (i < 2
11637 || sections.info_or_types.empty ()
11638 || sections.abbrev.empty ())
11639 {
11640 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11641 " [in module %s]"),
11642 dwp_file->name);
11643 }
11644 if (i == MAX_NR_V1_DWO_SECTIONS)
11645 {
11646 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11647 " [in module %s]"),
11648 dwp_file->name);
11649 }
11650
11651 /* It's easier for the rest of the code if we fake a struct dwo_file and
11652 have dwo_unit "live" in that. At least for now.
11653
11654 The DWP file can be made up of a random collection of CUs and TUs.
11655 However, for each CU + set of TUs that came from the same original DWO
11656 file, we can combine them back into a virtual DWO file to save space
11657 (fewer struct dwo_file objects to allocate). Remember that for really
11658 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11659
11660 std::string virtual_dwo_name =
11661 string_printf ("virtual-dwo/%d-%d-%d-%d",
11662 sections.abbrev.get_id (),
11663 sections.line.get_id (),
11664 sections.loc.get_id (),
11665 sections.str_offsets.get_id ());
11666 /* Can we use an existing virtual DWO file? */
11667 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11668 virtual_dwo_name.c_str (),
11669 comp_dir);
11670 /* Create one if necessary. */
11671 if (*dwo_file_slot == NULL)
11672 {
11673 if (dwarf_read_debug)
11674 {
11675 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11676 virtual_dwo_name.c_str ());
11677 }
11678 dwo_file = new struct dwo_file;
11679 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11680 dwo_file->comp_dir = comp_dir;
11681 dwo_file->sections.abbrev = sections.abbrev;
11682 dwo_file->sections.line = sections.line;
11683 dwo_file->sections.loc = sections.loc;
11684 dwo_file->sections.macinfo = sections.macinfo;
11685 dwo_file->sections.macro = sections.macro;
11686 dwo_file->sections.str_offsets = sections.str_offsets;
11687 /* The "str" section is global to the entire DWP file. */
11688 dwo_file->sections.str = dwp_file->sections.str;
11689 /* The info or types section is assigned below to dwo_unit,
11690 there's no need to record it in dwo_file.
11691 Also, we can't simply record type sections in dwo_file because
11692 we record a pointer into the vector in dwo_unit. As we collect more
11693 types we'll grow the vector and eventually have to reallocate space
11694 for it, invalidating all copies of pointers into the previous
11695 contents. */
11696 *dwo_file_slot = dwo_file;
11697 }
11698 else
11699 {
11700 if (dwarf_read_debug)
11701 {
11702 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11703 virtual_dwo_name.c_str ());
11704 }
11705 dwo_file = (struct dwo_file *) *dwo_file_slot;
11706 }
11707
11708 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11709 dwo_unit->dwo_file = dwo_file;
11710 dwo_unit->signature = signature;
11711 dwo_unit->section =
11712 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11713 *dwo_unit->section = sections.info_or_types;
11714 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11715
11716 return dwo_unit;
11717 }
11718
11719 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11720 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11721 piece within that section used by a TU/CU, return a virtual section
11722 of just that piece. */
11723
11724 static struct dwarf2_section_info
11725 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11726 struct dwarf2_section_info *section,
11727 bfd_size_type offset, bfd_size_type size)
11728 {
11729 struct dwarf2_section_info result;
11730 asection *sectp;
11731
11732 gdb_assert (section != NULL);
11733 gdb_assert (!section->is_virtual);
11734
11735 memset (&result, 0, sizeof (result));
11736 result.s.containing_section = section;
11737 result.is_virtual = true;
11738
11739 if (size == 0)
11740 return result;
11741
11742 sectp = section->get_bfd_section ();
11743
11744 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11745 bounds of the real section. This is a pretty-rare event, so just
11746 flag an error (easier) instead of a warning and trying to cope. */
11747 if (sectp == NULL
11748 || offset + size > bfd_section_size (sectp))
11749 {
11750 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11751 " in section %s [in module %s]"),
11752 sectp ? bfd_section_name (sectp) : "<unknown>",
11753 objfile_name (dwarf2_per_objfile->objfile));
11754 }
11755
11756 result.virtual_offset = offset;
11757 result.size = size;
11758 return result;
11759 }
11760
11761 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11762 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11763 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11764 This is for DWP version 2 files. */
11765
11766 static struct dwo_unit *
11767 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11768 struct dwp_file *dwp_file,
11769 uint32_t unit_index,
11770 const char *comp_dir,
11771 ULONGEST signature, int is_debug_types)
11772 {
11773 struct objfile *objfile = dwarf2_per_objfile->objfile;
11774 const struct dwp_hash_table *dwp_htab =
11775 is_debug_types ? dwp_file->tus : dwp_file->cus;
11776 bfd *dbfd = dwp_file->dbfd.get ();
11777 const char *kind = is_debug_types ? "TU" : "CU";
11778 struct dwo_file *dwo_file;
11779 struct dwo_unit *dwo_unit;
11780 struct virtual_v2_dwo_sections sections;
11781 void **dwo_file_slot;
11782 int i;
11783
11784 gdb_assert (dwp_file->version == 2);
11785
11786 if (dwarf_read_debug)
11787 {
11788 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11789 kind,
11790 pulongest (unit_index), hex_string (signature),
11791 dwp_file->name);
11792 }
11793
11794 /* Fetch the section offsets of this DWO unit. */
11795
11796 memset (&sections, 0, sizeof (sections));
11797
11798 for (i = 0; i < dwp_htab->nr_columns; ++i)
11799 {
11800 uint32_t offset = read_4_bytes (dbfd,
11801 dwp_htab->section_pool.v2.offsets
11802 + (((unit_index - 1) * dwp_htab->nr_columns
11803 + i)
11804 * sizeof (uint32_t)));
11805 uint32_t size = read_4_bytes (dbfd,
11806 dwp_htab->section_pool.v2.sizes
11807 + (((unit_index - 1) * dwp_htab->nr_columns
11808 + i)
11809 * sizeof (uint32_t)));
11810
11811 switch (dwp_htab->section_pool.v2.section_ids[i])
11812 {
11813 case DW_SECT_INFO:
11814 case DW_SECT_TYPES:
11815 sections.info_or_types_offset = offset;
11816 sections.info_or_types_size = size;
11817 break;
11818 case DW_SECT_ABBREV:
11819 sections.abbrev_offset = offset;
11820 sections.abbrev_size = size;
11821 break;
11822 case DW_SECT_LINE:
11823 sections.line_offset = offset;
11824 sections.line_size = size;
11825 break;
11826 case DW_SECT_LOC:
11827 sections.loc_offset = offset;
11828 sections.loc_size = size;
11829 break;
11830 case DW_SECT_STR_OFFSETS:
11831 sections.str_offsets_offset = offset;
11832 sections.str_offsets_size = size;
11833 break;
11834 case DW_SECT_MACINFO:
11835 sections.macinfo_offset = offset;
11836 sections.macinfo_size = size;
11837 break;
11838 case DW_SECT_MACRO:
11839 sections.macro_offset = offset;
11840 sections.macro_size = size;
11841 break;
11842 }
11843 }
11844
11845 /* It's easier for the rest of the code if we fake a struct dwo_file and
11846 have dwo_unit "live" in that. At least for now.
11847
11848 The DWP file can be made up of a random collection of CUs and TUs.
11849 However, for each CU + set of TUs that came from the same original DWO
11850 file, we can combine them back into a virtual DWO file to save space
11851 (fewer struct dwo_file objects to allocate). Remember that for really
11852 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11853
11854 std::string virtual_dwo_name =
11855 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11856 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11857 (long) (sections.line_size ? sections.line_offset : 0),
11858 (long) (sections.loc_size ? sections.loc_offset : 0),
11859 (long) (sections.str_offsets_size
11860 ? sections.str_offsets_offset : 0));
11861 /* Can we use an existing virtual DWO file? */
11862 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11863 virtual_dwo_name.c_str (),
11864 comp_dir);
11865 /* Create one if necessary. */
11866 if (*dwo_file_slot == NULL)
11867 {
11868 if (dwarf_read_debug)
11869 {
11870 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11871 virtual_dwo_name.c_str ());
11872 }
11873 dwo_file = new struct dwo_file;
11874 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11875 dwo_file->comp_dir = comp_dir;
11876 dwo_file->sections.abbrev =
11877 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11878 sections.abbrev_offset, sections.abbrev_size);
11879 dwo_file->sections.line =
11880 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11881 sections.line_offset, sections.line_size);
11882 dwo_file->sections.loc =
11883 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11884 sections.loc_offset, sections.loc_size);
11885 dwo_file->sections.macinfo =
11886 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11887 sections.macinfo_offset, sections.macinfo_size);
11888 dwo_file->sections.macro =
11889 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11890 sections.macro_offset, sections.macro_size);
11891 dwo_file->sections.str_offsets =
11892 create_dwp_v2_section (dwarf2_per_objfile,
11893 &dwp_file->sections.str_offsets,
11894 sections.str_offsets_offset,
11895 sections.str_offsets_size);
11896 /* The "str" section is global to the entire DWP file. */
11897 dwo_file->sections.str = dwp_file->sections.str;
11898 /* The info or types section is assigned below to dwo_unit,
11899 there's no need to record it in dwo_file.
11900 Also, we can't simply record type sections in dwo_file because
11901 we record a pointer into the vector in dwo_unit. As we collect more
11902 types we'll grow the vector and eventually have to reallocate space
11903 for it, invalidating all copies of pointers into the previous
11904 contents. */
11905 *dwo_file_slot = dwo_file;
11906 }
11907 else
11908 {
11909 if (dwarf_read_debug)
11910 {
11911 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11912 virtual_dwo_name.c_str ());
11913 }
11914 dwo_file = (struct dwo_file *) *dwo_file_slot;
11915 }
11916
11917 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11918 dwo_unit->dwo_file = dwo_file;
11919 dwo_unit->signature = signature;
11920 dwo_unit->section =
11921 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11922 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11923 is_debug_types
11924 ? &dwp_file->sections.types
11925 : &dwp_file->sections.info,
11926 sections.info_or_types_offset,
11927 sections.info_or_types_size);
11928 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11929
11930 return dwo_unit;
11931 }
11932
11933 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11934 Returns NULL if the signature isn't found. */
11935
11936 static struct dwo_unit *
11937 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11938 struct dwp_file *dwp_file, const char *comp_dir,
11939 ULONGEST signature, int is_debug_types)
11940 {
11941 const struct dwp_hash_table *dwp_htab =
11942 is_debug_types ? dwp_file->tus : dwp_file->cus;
11943 bfd *dbfd = dwp_file->dbfd.get ();
11944 uint32_t mask = dwp_htab->nr_slots - 1;
11945 uint32_t hash = signature & mask;
11946 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11947 unsigned int i;
11948 void **slot;
11949 struct dwo_unit find_dwo_cu;
11950
11951 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11952 find_dwo_cu.signature = signature;
11953 slot = htab_find_slot (is_debug_types
11954 ? dwp_file->loaded_tus.get ()
11955 : dwp_file->loaded_cus.get (),
11956 &find_dwo_cu, INSERT);
11957
11958 if (*slot != NULL)
11959 return (struct dwo_unit *) *slot;
11960
11961 /* Use a for loop so that we don't loop forever on bad debug info. */
11962 for (i = 0; i < dwp_htab->nr_slots; ++i)
11963 {
11964 ULONGEST signature_in_table;
11965
11966 signature_in_table =
11967 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11968 if (signature_in_table == signature)
11969 {
11970 uint32_t unit_index =
11971 read_4_bytes (dbfd,
11972 dwp_htab->unit_table + hash * sizeof (uint32_t));
11973
11974 if (dwp_file->version == 1)
11975 {
11976 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11977 dwp_file, unit_index,
11978 comp_dir, signature,
11979 is_debug_types);
11980 }
11981 else
11982 {
11983 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11984 dwp_file, unit_index,
11985 comp_dir, signature,
11986 is_debug_types);
11987 }
11988 return (struct dwo_unit *) *slot;
11989 }
11990 if (signature_in_table == 0)
11991 return NULL;
11992 hash = (hash + hash2) & mask;
11993 }
11994
11995 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11996 " [in module %s]"),
11997 dwp_file->name);
11998 }
11999
12000 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12001 Open the file specified by FILE_NAME and hand it off to BFD for
12002 preliminary analysis. Return a newly initialized bfd *, which
12003 includes a canonicalized copy of FILE_NAME.
12004 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12005 SEARCH_CWD is true if the current directory is to be searched.
12006 It will be searched before debug-file-directory.
12007 If successful, the file is added to the bfd include table of the
12008 objfile's bfd (see gdb_bfd_record_inclusion).
12009 If unable to find/open the file, return NULL.
12010 NOTE: This function is derived from symfile_bfd_open. */
12011
12012 static gdb_bfd_ref_ptr
12013 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12014 const char *file_name, int is_dwp, int search_cwd)
12015 {
12016 int desc;
12017 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12018 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12019 to debug_file_directory. */
12020 const char *search_path;
12021 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12022
12023 gdb::unique_xmalloc_ptr<char> search_path_holder;
12024 if (search_cwd)
12025 {
12026 if (*debug_file_directory != '\0')
12027 {
12028 search_path_holder.reset (concat (".", dirname_separator_string,
12029 debug_file_directory,
12030 (char *) NULL));
12031 search_path = search_path_holder.get ();
12032 }
12033 else
12034 search_path = ".";
12035 }
12036 else
12037 search_path = debug_file_directory;
12038
12039 openp_flags flags = OPF_RETURN_REALPATH;
12040 if (is_dwp)
12041 flags |= OPF_SEARCH_IN_PATH;
12042
12043 gdb::unique_xmalloc_ptr<char> absolute_name;
12044 desc = openp (search_path, flags, file_name,
12045 O_RDONLY | O_BINARY, &absolute_name);
12046 if (desc < 0)
12047 return NULL;
12048
12049 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12050 gnutarget, desc));
12051 if (sym_bfd == NULL)
12052 return NULL;
12053 bfd_set_cacheable (sym_bfd.get (), 1);
12054
12055 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12056 return NULL;
12057
12058 /* Success. Record the bfd as having been included by the objfile's bfd.
12059 This is important because things like demangled_names_hash lives in the
12060 objfile's per_bfd space and may have references to things like symbol
12061 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12062 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12063
12064 return sym_bfd;
12065 }
12066
12067 /* Try to open DWO file FILE_NAME.
12068 COMP_DIR is the DW_AT_comp_dir attribute.
12069 The result is the bfd handle of the file.
12070 If there is a problem finding or opening the file, return NULL.
12071 Upon success, the canonicalized path of the file is stored in the bfd,
12072 same as symfile_bfd_open. */
12073
12074 static gdb_bfd_ref_ptr
12075 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12076 const char *file_name, const char *comp_dir)
12077 {
12078 if (IS_ABSOLUTE_PATH (file_name))
12079 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12080 0 /*is_dwp*/, 0 /*search_cwd*/);
12081
12082 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12083
12084 if (comp_dir != NULL)
12085 {
12086 gdb::unique_xmalloc_ptr<char> path_to_try
12087 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12088
12089 /* NOTE: If comp_dir is a relative path, this will also try the
12090 search path, which seems useful. */
12091 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12092 path_to_try.get (),
12093 0 /*is_dwp*/,
12094 1 /*search_cwd*/));
12095 if (abfd != NULL)
12096 return abfd;
12097 }
12098
12099 /* That didn't work, try debug-file-directory, which, despite its name,
12100 is a list of paths. */
12101
12102 if (*debug_file_directory == '\0')
12103 return NULL;
12104
12105 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12106 0 /*is_dwp*/, 1 /*search_cwd*/);
12107 }
12108
12109 /* This function is mapped across the sections and remembers the offset and
12110 size of each of the DWO debugging sections we are interested in. */
12111
12112 static void
12113 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12114 {
12115 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12116 const struct dwop_section_names *names = &dwop_section_names;
12117
12118 if (section_is_p (sectp->name, &names->abbrev_dwo))
12119 {
12120 dwo_sections->abbrev.s.section = sectp;
12121 dwo_sections->abbrev.size = bfd_section_size (sectp);
12122 }
12123 else if (section_is_p (sectp->name, &names->info_dwo))
12124 {
12125 dwo_sections->info.s.section = sectp;
12126 dwo_sections->info.size = bfd_section_size (sectp);
12127 }
12128 else if (section_is_p (sectp->name, &names->line_dwo))
12129 {
12130 dwo_sections->line.s.section = sectp;
12131 dwo_sections->line.size = bfd_section_size (sectp);
12132 }
12133 else if (section_is_p (sectp->name, &names->loc_dwo))
12134 {
12135 dwo_sections->loc.s.section = sectp;
12136 dwo_sections->loc.size = bfd_section_size (sectp);
12137 }
12138 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12139 {
12140 dwo_sections->macinfo.s.section = sectp;
12141 dwo_sections->macinfo.size = bfd_section_size (sectp);
12142 }
12143 else if (section_is_p (sectp->name, &names->macro_dwo))
12144 {
12145 dwo_sections->macro.s.section = sectp;
12146 dwo_sections->macro.size = bfd_section_size (sectp);
12147 }
12148 else if (section_is_p (sectp->name, &names->str_dwo))
12149 {
12150 dwo_sections->str.s.section = sectp;
12151 dwo_sections->str.size = bfd_section_size (sectp);
12152 }
12153 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12154 {
12155 dwo_sections->str_offsets.s.section = sectp;
12156 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12157 }
12158 else if (section_is_p (sectp->name, &names->types_dwo))
12159 {
12160 struct dwarf2_section_info type_section;
12161
12162 memset (&type_section, 0, sizeof (type_section));
12163 type_section.s.section = sectp;
12164 type_section.size = bfd_section_size (sectp);
12165 dwo_sections->types.push_back (type_section);
12166 }
12167 }
12168
12169 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12170 by PER_CU. This is for the non-DWP case.
12171 The result is NULL if DWO_NAME can't be found. */
12172
12173 static struct dwo_file *
12174 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12175 const char *dwo_name, const char *comp_dir)
12176 {
12177 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12178
12179 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12180 if (dbfd == NULL)
12181 {
12182 if (dwarf_read_debug)
12183 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12184 return NULL;
12185 }
12186
12187 dwo_file_up dwo_file (new struct dwo_file);
12188 dwo_file->dwo_name = dwo_name;
12189 dwo_file->comp_dir = comp_dir;
12190 dwo_file->dbfd = std::move (dbfd);
12191
12192 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12193 &dwo_file->sections);
12194
12195 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12196 dwo_file->sections.info, dwo_file->cus);
12197
12198 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12199 dwo_file->sections.types, dwo_file->tus);
12200
12201 if (dwarf_read_debug)
12202 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12203
12204 return dwo_file.release ();
12205 }
12206
12207 /* This function is mapped across the sections and remembers the offset and
12208 size of each of the DWP debugging sections common to version 1 and 2 that
12209 we are interested in. */
12210
12211 static void
12212 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12213 void *dwp_file_ptr)
12214 {
12215 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12216 const struct dwop_section_names *names = &dwop_section_names;
12217 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12218
12219 /* Record the ELF section number for later lookup: this is what the
12220 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12221 gdb_assert (elf_section_nr < dwp_file->num_sections);
12222 dwp_file->elf_sections[elf_section_nr] = sectp;
12223
12224 /* Look for specific sections that we need. */
12225 if (section_is_p (sectp->name, &names->str_dwo))
12226 {
12227 dwp_file->sections.str.s.section = sectp;
12228 dwp_file->sections.str.size = bfd_section_size (sectp);
12229 }
12230 else if (section_is_p (sectp->name, &names->cu_index))
12231 {
12232 dwp_file->sections.cu_index.s.section = sectp;
12233 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12234 }
12235 else if (section_is_p (sectp->name, &names->tu_index))
12236 {
12237 dwp_file->sections.tu_index.s.section = sectp;
12238 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12239 }
12240 }
12241
12242 /* This function is mapped across the sections and remembers the offset and
12243 size of each of the DWP version 2 debugging sections that we are interested
12244 in. This is split into a separate function because we don't know if we
12245 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12246
12247 static void
12248 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12249 {
12250 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12251 const struct dwop_section_names *names = &dwop_section_names;
12252 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12253
12254 /* Record the ELF section number for later lookup: this is what the
12255 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12256 gdb_assert (elf_section_nr < dwp_file->num_sections);
12257 dwp_file->elf_sections[elf_section_nr] = sectp;
12258
12259 /* Look for specific sections that we need. */
12260 if (section_is_p (sectp->name, &names->abbrev_dwo))
12261 {
12262 dwp_file->sections.abbrev.s.section = sectp;
12263 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12264 }
12265 else if (section_is_p (sectp->name, &names->info_dwo))
12266 {
12267 dwp_file->sections.info.s.section = sectp;
12268 dwp_file->sections.info.size = bfd_section_size (sectp);
12269 }
12270 else if (section_is_p (sectp->name, &names->line_dwo))
12271 {
12272 dwp_file->sections.line.s.section = sectp;
12273 dwp_file->sections.line.size = bfd_section_size (sectp);
12274 }
12275 else if (section_is_p (sectp->name, &names->loc_dwo))
12276 {
12277 dwp_file->sections.loc.s.section = sectp;
12278 dwp_file->sections.loc.size = bfd_section_size (sectp);
12279 }
12280 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12281 {
12282 dwp_file->sections.macinfo.s.section = sectp;
12283 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12284 }
12285 else if (section_is_p (sectp->name, &names->macro_dwo))
12286 {
12287 dwp_file->sections.macro.s.section = sectp;
12288 dwp_file->sections.macro.size = bfd_section_size (sectp);
12289 }
12290 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12291 {
12292 dwp_file->sections.str_offsets.s.section = sectp;
12293 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12294 }
12295 else if (section_is_p (sectp->name, &names->types_dwo))
12296 {
12297 dwp_file->sections.types.s.section = sectp;
12298 dwp_file->sections.types.size = bfd_section_size (sectp);
12299 }
12300 }
12301
12302 /* Hash function for dwp_file loaded CUs/TUs. */
12303
12304 static hashval_t
12305 hash_dwp_loaded_cutus (const void *item)
12306 {
12307 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12308
12309 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12310 return dwo_unit->signature;
12311 }
12312
12313 /* Equality function for dwp_file loaded CUs/TUs. */
12314
12315 static int
12316 eq_dwp_loaded_cutus (const void *a, const void *b)
12317 {
12318 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12319 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12320
12321 return dua->signature == dub->signature;
12322 }
12323
12324 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12325
12326 static htab_up
12327 allocate_dwp_loaded_cutus_table ()
12328 {
12329 return htab_up (htab_create_alloc (3,
12330 hash_dwp_loaded_cutus,
12331 eq_dwp_loaded_cutus,
12332 NULL, xcalloc, xfree));
12333 }
12334
12335 /* Try to open DWP file FILE_NAME.
12336 The result is the bfd handle of the file.
12337 If there is a problem finding or opening the file, return NULL.
12338 Upon success, the canonicalized path of the file is stored in the bfd,
12339 same as symfile_bfd_open. */
12340
12341 static gdb_bfd_ref_ptr
12342 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12343 const char *file_name)
12344 {
12345 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12346 1 /*is_dwp*/,
12347 1 /*search_cwd*/));
12348 if (abfd != NULL)
12349 return abfd;
12350
12351 /* Work around upstream bug 15652.
12352 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12353 [Whether that's a "bug" is debatable, but it is getting in our way.]
12354 We have no real idea where the dwp file is, because gdb's realpath-ing
12355 of the executable's path may have discarded the needed info.
12356 [IWBN if the dwp file name was recorded in the executable, akin to
12357 .gnu_debuglink, but that doesn't exist yet.]
12358 Strip the directory from FILE_NAME and search again. */
12359 if (*debug_file_directory != '\0')
12360 {
12361 /* Don't implicitly search the current directory here.
12362 If the user wants to search "." to handle this case,
12363 it must be added to debug-file-directory. */
12364 return try_open_dwop_file (dwarf2_per_objfile,
12365 lbasename (file_name), 1 /*is_dwp*/,
12366 0 /*search_cwd*/);
12367 }
12368
12369 return NULL;
12370 }
12371
12372 /* Initialize the use of the DWP file for the current objfile.
12373 By convention the name of the DWP file is ${objfile}.dwp.
12374 The result is NULL if it can't be found. */
12375
12376 static std::unique_ptr<struct dwp_file>
12377 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12378 {
12379 struct objfile *objfile = dwarf2_per_objfile->objfile;
12380
12381 /* Try to find first .dwp for the binary file before any symbolic links
12382 resolving. */
12383
12384 /* If the objfile is a debug file, find the name of the real binary
12385 file and get the name of dwp file from there. */
12386 std::string dwp_name;
12387 if (objfile->separate_debug_objfile_backlink != NULL)
12388 {
12389 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12390 const char *backlink_basename = lbasename (backlink->original_name);
12391
12392 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12393 }
12394 else
12395 dwp_name = objfile->original_name;
12396
12397 dwp_name += ".dwp";
12398
12399 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12400 if (dbfd == NULL
12401 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12402 {
12403 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12404 dwp_name = objfile_name (objfile);
12405 dwp_name += ".dwp";
12406 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12407 }
12408
12409 if (dbfd == NULL)
12410 {
12411 if (dwarf_read_debug)
12412 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12413 return std::unique_ptr<dwp_file> ();
12414 }
12415
12416 const char *name = bfd_get_filename (dbfd.get ());
12417 std::unique_ptr<struct dwp_file> dwp_file
12418 (new struct dwp_file (name, std::move (dbfd)));
12419
12420 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12421 dwp_file->elf_sections =
12422 OBSTACK_CALLOC (&objfile->objfile_obstack,
12423 dwp_file->num_sections, asection *);
12424
12425 bfd_map_over_sections (dwp_file->dbfd.get (),
12426 dwarf2_locate_common_dwp_sections,
12427 dwp_file.get ());
12428
12429 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12430 0);
12431
12432 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12433 1);
12434
12435 /* The DWP file version is stored in the hash table. Oh well. */
12436 if (dwp_file->cus && dwp_file->tus
12437 && dwp_file->cus->version != dwp_file->tus->version)
12438 {
12439 /* Technically speaking, we should try to limp along, but this is
12440 pretty bizarre. We use pulongest here because that's the established
12441 portability solution (e.g, we cannot use %u for uint32_t). */
12442 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12443 " TU version %s [in DWP file %s]"),
12444 pulongest (dwp_file->cus->version),
12445 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12446 }
12447
12448 if (dwp_file->cus)
12449 dwp_file->version = dwp_file->cus->version;
12450 else if (dwp_file->tus)
12451 dwp_file->version = dwp_file->tus->version;
12452 else
12453 dwp_file->version = 2;
12454
12455 if (dwp_file->version == 2)
12456 bfd_map_over_sections (dwp_file->dbfd.get (),
12457 dwarf2_locate_v2_dwp_sections,
12458 dwp_file.get ());
12459
12460 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12461 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12462
12463 if (dwarf_read_debug)
12464 {
12465 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12466 fprintf_unfiltered (gdb_stdlog,
12467 " %s CUs, %s TUs\n",
12468 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12469 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12470 }
12471
12472 return dwp_file;
12473 }
12474
12475 /* Wrapper around open_and_init_dwp_file, only open it once. */
12476
12477 static struct dwp_file *
12478 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12479 {
12480 if (! dwarf2_per_objfile->dwp_checked)
12481 {
12482 dwarf2_per_objfile->dwp_file
12483 = open_and_init_dwp_file (dwarf2_per_objfile);
12484 dwarf2_per_objfile->dwp_checked = 1;
12485 }
12486 return dwarf2_per_objfile->dwp_file.get ();
12487 }
12488
12489 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12490 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12491 or in the DWP file for the objfile, referenced by THIS_UNIT.
12492 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12493 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12494
12495 This is called, for example, when wanting to read a variable with a
12496 complex location. Therefore we don't want to do file i/o for every call.
12497 Therefore we don't want to look for a DWO file on every call.
12498 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12499 then we check if we've already seen DWO_NAME, and only THEN do we check
12500 for a DWO file.
12501
12502 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12503 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12504
12505 static struct dwo_unit *
12506 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12507 const char *dwo_name, const char *comp_dir,
12508 ULONGEST signature, int is_debug_types)
12509 {
12510 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12511 struct objfile *objfile = dwarf2_per_objfile->objfile;
12512 const char *kind = is_debug_types ? "TU" : "CU";
12513 void **dwo_file_slot;
12514 struct dwo_file *dwo_file;
12515 struct dwp_file *dwp_file;
12516
12517 /* First see if there's a DWP file.
12518 If we have a DWP file but didn't find the DWO inside it, don't
12519 look for the original DWO file. It makes gdb behave differently
12520 depending on whether one is debugging in the build tree. */
12521
12522 dwp_file = get_dwp_file (dwarf2_per_objfile);
12523 if (dwp_file != NULL)
12524 {
12525 const struct dwp_hash_table *dwp_htab =
12526 is_debug_types ? dwp_file->tus : dwp_file->cus;
12527
12528 if (dwp_htab != NULL)
12529 {
12530 struct dwo_unit *dwo_cutu =
12531 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12532 signature, is_debug_types);
12533
12534 if (dwo_cutu != NULL)
12535 {
12536 if (dwarf_read_debug)
12537 {
12538 fprintf_unfiltered (gdb_stdlog,
12539 "Virtual DWO %s %s found: @%s\n",
12540 kind, hex_string (signature),
12541 host_address_to_string (dwo_cutu));
12542 }
12543 return dwo_cutu;
12544 }
12545 }
12546 }
12547 else
12548 {
12549 /* No DWP file, look for the DWO file. */
12550
12551 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12552 dwo_name, comp_dir);
12553 if (*dwo_file_slot == NULL)
12554 {
12555 /* Read in the file and build a table of the CUs/TUs it contains. */
12556 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12557 }
12558 /* NOTE: This will be NULL if unable to open the file. */
12559 dwo_file = (struct dwo_file *) *dwo_file_slot;
12560
12561 if (dwo_file != NULL)
12562 {
12563 struct dwo_unit *dwo_cutu = NULL;
12564
12565 if (is_debug_types && dwo_file->tus)
12566 {
12567 struct dwo_unit find_dwo_cutu;
12568
12569 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12570 find_dwo_cutu.signature = signature;
12571 dwo_cutu
12572 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12573 &find_dwo_cutu);
12574 }
12575 else if (!is_debug_types && dwo_file->cus)
12576 {
12577 struct dwo_unit find_dwo_cutu;
12578
12579 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12580 find_dwo_cutu.signature = signature;
12581 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12582 &find_dwo_cutu);
12583 }
12584
12585 if (dwo_cutu != NULL)
12586 {
12587 if (dwarf_read_debug)
12588 {
12589 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12590 kind, dwo_name, hex_string (signature),
12591 host_address_to_string (dwo_cutu));
12592 }
12593 return dwo_cutu;
12594 }
12595 }
12596 }
12597
12598 /* We didn't find it. This could mean a dwo_id mismatch, or
12599 someone deleted the DWO/DWP file, or the search path isn't set up
12600 correctly to find the file. */
12601
12602 if (dwarf_read_debug)
12603 {
12604 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12605 kind, dwo_name, hex_string (signature));
12606 }
12607
12608 /* This is a warning and not a complaint because it can be caused by
12609 pilot error (e.g., user accidentally deleting the DWO). */
12610 {
12611 /* Print the name of the DWP file if we looked there, helps the user
12612 better diagnose the problem. */
12613 std::string dwp_text;
12614
12615 if (dwp_file != NULL)
12616 dwp_text = string_printf (" [in DWP file %s]",
12617 lbasename (dwp_file->name));
12618
12619 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12620 " [in module %s]"),
12621 kind, dwo_name, hex_string (signature),
12622 dwp_text.c_str (),
12623 this_unit->is_debug_types ? "TU" : "CU",
12624 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12625 }
12626 return NULL;
12627 }
12628
12629 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12630 See lookup_dwo_cutu_unit for details. */
12631
12632 static struct dwo_unit *
12633 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12634 const char *dwo_name, const char *comp_dir,
12635 ULONGEST signature)
12636 {
12637 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12638 }
12639
12640 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12641 See lookup_dwo_cutu_unit for details. */
12642
12643 static struct dwo_unit *
12644 lookup_dwo_type_unit (struct signatured_type *this_tu,
12645 const char *dwo_name, const char *comp_dir)
12646 {
12647 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12648 }
12649
12650 /* Traversal function for queue_and_load_all_dwo_tus. */
12651
12652 static int
12653 queue_and_load_dwo_tu (void **slot, void *info)
12654 {
12655 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12656 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12657 ULONGEST signature = dwo_unit->signature;
12658 struct signatured_type *sig_type =
12659 lookup_dwo_signatured_type (per_cu->cu, signature);
12660
12661 if (sig_type != NULL)
12662 {
12663 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12664
12665 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12666 a real dependency of PER_CU on SIG_TYPE. That is detected later
12667 while processing PER_CU. */
12668 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12669 load_full_type_unit (sig_cu);
12670 per_cu->imported_symtabs_push (sig_cu);
12671 }
12672
12673 return 1;
12674 }
12675
12676 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12677 The DWO may have the only definition of the type, though it may not be
12678 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12679 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12680
12681 static void
12682 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12683 {
12684 struct dwo_unit *dwo_unit;
12685 struct dwo_file *dwo_file;
12686
12687 gdb_assert (!per_cu->is_debug_types);
12688 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12689 gdb_assert (per_cu->cu != NULL);
12690
12691 dwo_unit = per_cu->cu->dwo_unit;
12692 gdb_assert (dwo_unit != NULL);
12693
12694 dwo_file = dwo_unit->dwo_file;
12695 if (dwo_file->tus != NULL)
12696 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12697 per_cu);
12698 }
12699
12700 /* Read in various DIEs. */
12701
12702 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12703 Inherit only the children of the DW_AT_abstract_origin DIE not being
12704 already referenced by DW_AT_abstract_origin from the children of the
12705 current DIE. */
12706
12707 static void
12708 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12709 {
12710 struct die_info *child_die;
12711 sect_offset *offsetp;
12712 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12713 struct die_info *origin_die;
12714 /* Iterator of the ORIGIN_DIE children. */
12715 struct die_info *origin_child_die;
12716 struct attribute *attr;
12717 struct dwarf2_cu *origin_cu;
12718 struct pending **origin_previous_list_in_scope;
12719
12720 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12721 if (!attr)
12722 return;
12723
12724 /* Note that following die references may follow to a die in a
12725 different cu. */
12726
12727 origin_cu = cu;
12728 origin_die = follow_die_ref (die, attr, &origin_cu);
12729
12730 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12731 symbols in. */
12732 origin_previous_list_in_scope = origin_cu->list_in_scope;
12733 origin_cu->list_in_scope = cu->list_in_scope;
12734
12735 if (die->tag != origin_die->tag
12736 && !(die->tag == DW_TAG_inlined_subroutine
12737 && origin_die->tag == DW_TAG_subprogram))
12738 complaint (_("DIE %s and its abstract origin %s have different tags"),
12739 sect_offset_str (die->sect_off),
12740 sect_offset_str (origin_die->sect_off));
12741
12742 std::vector<sect_offset> offsets;
12743
12744 for (child_die = die->child;
12745 child_die && child_die->tag;
12746 child_die = sibling_die (child_die))
12747 {
12748 struct die_info *child_origin_die;
12749 struct dwarf2_cu *child_origin_cu;
12750
12751 /* We are trying to process concrete instance entries:
12752 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12753 it's not relevant to our analysis here. i.e. detecting DIEs that are
12754 present in the abstract instance but not referenced in the concrete
12755 one. */
12756 if (child_die->tag == DW_TAG_call_site
12757 || child_die->tag == DW_TAG_GNU_call_site)
12758 continue;
12759
12760 /* For each CHILD_DIE, find the corresponding child of
12761 ORIGIN_DIE. If there is more than one layer of
12762 DW_AT_abstract_origin, follow them all; there shouldn't be,
12763 but GCC versions at least through 4.4 generate this (GCC PR
12764 40573). */
12765 child_origin_die = child_die;
12766 child_origin_cu = cu;
12767 while (1)
12768 {
12769 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12770 child_origin_cu);
12771 if (attr == NULL)
12772 break;
12773 child_origin_die = follow_die_ref (child_origin_die, attr,
12774 &child_origin_cu);
12775 }
12776
12777 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12778 counterpart may exist. */
12779 if (child_origin_die != child_die)
12780 {
12781 if (child_die->tag != child_origin_die->tag
12782 && !(child_die->tag == DW_TAG_inlined_subroutine
12783 && child_origin_die->tag == DW_TAG_subprogram))
12784 complaint (_("Child DIE %s and its abstract origin %s have "
12785 "different tags"),
12786 sect_offset_str (child_die->sect_off),
12787 sect_offset_str (child_origin_die->sect_off));
12788 if (child_origin_die->parent != origin_die)
12789 complaint (_("Child DIE %s and its abstract origin %s have "
12790 "different parents"),
12791 sect_offset_str (child_die->sect_off),
12792 sect_offset_str (child_origin_die->sect_off));
12793 else
12794 offsets.push_back (child_origin_die->sect_off);
12795 }
12796 }
12797 std::sort (offsets.begin (), offsets.end ());
12798 sect_offset *offsets_end = offsets.data () + offsets.size ();
12799 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12800 if (offsetp[-1] == *offsetp)
12801 complaint (_("Multiple children of DIE %s refer "
12802 "to DIE %s as their abstract origin"),
12803 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12804
12805 offsetp = offsets.data ();
12806 origin_child_die = origin_die->child;
12807 while (origin_child_die && origin_child_die->tag)
12808 {
12809 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12810 while (offsetp < offsets_end
12811 && *offsetp < origin_child_die->sect_off)
12812 offsetp++;
12813 if (offsetp >= offsets_end
12814 || *offsetp > origin_child_die->sect_off)
12815 {
12816 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12817 Check whether we're already processing ORIGIN_CHILD_DIE.
12818 This can happen with mutually referenced abstract_origins.
12819 PR 16581. */
12820 if (!origin_child_die->in_process)
12821 process_die (origin_child_die, origin_cu);
12822 }
12823 origin_child_die = sibling_die (origin_child_die);
12824 }
12825 origin_cu->list_in_scope = origin_previous_list_in_scope;
12826
12827 if (cu != origin_cu)
12828 compute_delayed_physnames (origin_cu);
12829 }
12830
12831 static void
12832 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12833 {
12834 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12835 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12836 struct context_stack *newobj;
12837 CORE_ADDR lowpc;
12838 CORE_ADDR highpc;
12839 struct die_info *child_die;
12840 struct attribute *attr, *call_line, *call_file;
12841 const char *name;
12842 CORE_ADDR baseaddr;
12843 struct block *block;
12844 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12845 std::vector<struct symbol *> template_args;
12846 struct template_symbol *templ_func = NULL;
12847
12848 if (inlined_func)
12849 {
12850 /* If we do not have call site information, we can't show the
12851 caller of this inlined function. That's too confusing, so
12852 only use the scope for local variables. */
12853 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12854 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12855 if (call_line == NULL || call_file == NULL)
12856 {
12857 read_lexical_block_scope (die, cu);
12858 return;
12859 }
12860 }
12861
12862 baseaddr = objfile->text_section_offset ();
12863
12864 name = dwarf2_name (die, cu);
12865
12866 /* Ignore functions with missing or empty names. These are actually
12867 illegal according to the DWARF standard. */
12868 if (name == NULL)
12869 {
12870 complaint (_("missing name for subprogram DIE at %s"),
12871 sect_offset_str (die->sect_off));
12872 return;
12873 }
12874
12875 /* Ignore functions with missing or invalid low and high pc attributes. */
12876 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12877 <= PC_BOUNDS_INVALID)
12878 {
12879 attr = dwarf2_attr (die, DW_AT_external, cu);
12880 if (!attr || !DW_UNSND (attr))
12881 complaint (_("cannot get low and high bounds "
12882 "for subprogram DIE at %s"),
12883 sect_offset_str (die->sect_off));
12884 return;
12885 }
12886
12887 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12888 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12889
12890 /* If we have any template arguments, then we must allocate a
12891 different sort of symbol. */
12892 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12893 {
12894 if (child_die->tag == DW_TAG_template_type_param
12895 || child_die->tag == DW_TAG_template_value_param)
12896 {
12897 templ_func = allocate_template_symbol (objfile);
12898 templ_func->subclass = SYMBOL_TEMPLATE;
12899 break;
12900 }
12901 }
12902
12903 newobj = cu->get_builder ()->push_context (0, lowpc);
12904 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12905 (struct symbol *) templ_func);
12906
12907 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12908 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12909 cu->language);
12910
12911 /* If there is a location expression for DW_AT_frame_base, record
12912 it. */
12913 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12914 if (attr != nullptr)
12915 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12916
12917 /* If there is a location for the static link, record it. */
12918 newobj->static_link = NULL;
12919 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12920 if (attr != nullptr)
12921 {
12922 newobj->static_link
12923 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12924 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12925 cu->per_cu->addr_type ());
12926 }
12927
12928 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12929
12930 if (die->child != NULL)
12931 {
12932 child_die = die->child;
12933 while (child_die && child_die->tag)
12934 {
12935 if (child_die->tag == DW_TAG_template_type_param
12936 || child_die->tag == DW_TAG_template_value_param)
12937 {
12938 struct symbol *arg = new_symbol (child_die, NULL, cu);
12939
12940 if (arg != NULL)
12941 template_args.push_back (arg);
12942 }
12943 else
12944 process_die (child_die, cu);
12945 child_die = sibling_die (child_die);
12946 }
12947 }
12948
12949 inherit_abstract_dies (die, cu);
12950
12951 /* If we have a DW_AT_specification, we might need to import using
12952 directives from the context of the specification DIE. See the
12953 comment in determine_prefix. */
12954 if (cu->language == language_cplus
12955 && dwarf2_attr (die, DW_AT_specification, cu))
12956 {
12957 struct dwarf2_cu *spec_cu = cu;
12958 struct die_info *spec_die = die_specification (die, &spec_cu);
12959
12960 while (spec_die)
12961 {
12962 child_die = spec_die->child;
12963 while (child_die && child_die->tag)
12964 {
12965 if (child_die->tag == DW_TAG_imported_module)
12966 process_die (child_die, spec_cu);
12967 child_die = sibling_die (child_die);
12968 }
12969
12970 /* In some cases, GCC generates specification DIEs that
12971 themselves contain DW_AT_specification attributes. */
12972 spec_die = die_specification (spec_die, &spec_cu);
12973 }
12974 }
12975
12976 struct context_stack cstk = cu->get_builder ()->pop_context ();
12977 /* Make a block for the local symbols within. */
12978 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12979 cstk.static_link, lowpc, highpc);
12980
12981 /* For C++, set the block's scope. */
12982 if ((cu->language == language_cplus
12983 || cu->language == language_fortran
12984 || cu->language == language_d
12985 || cu->language == language_rust)
12986 && cu->processing_has_namespace_info)
12987 block_set_scope (block, determine_prefix (die, cu),
12988 &objfile->objfile_obstack);
12989
12990 /* If we have address ranges, record them. */
12991 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12992
12993 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12994
12995 /* Attach template arguments to function. */
12996 if (!template_args.empty ())
12997 {
12998 gdb_assert (templ_func != NULL);
12999
13000 templ_func->n_template_arguments = template_args.size ();
13001 templ_func->template_arguments
13002 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13003 templ_func->n_template_arguments);
13004 memcpy (templ_func->template_arguments,
13005 template_args.data (),
13006 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13007
13008 /* Make sure that the symtab is set on the new symbols. Even
13009 though they don't appear in this symtab directly, other parts
13010 of gdb assume that symbols do, and this is reasonably
13011 true. */
13012 for (symbol *sym : template_args)
13013 symbol_set_symtab (sym, symbol_symtab (templ_func));
13014 }
13015
13016 /* In C++, we can have functions nested inside functions (e.g., when
13017 a function declares a class that has methods). This means that
13018 when we finish processing a function scope, we may need to go
13019 back to building a containing block's symbol lists. */
13020 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13021 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13022
13023 /* If we've finished processing a top-level function, subsequent
13024 symbols go in the file symbol list. */
13025 if (cu->get_builder ()->outermost_context_p ())
13026 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13027 }
13028
13029 /* Process all the DIES contained within a lexical block scope. Start
13030 a new scope, process the dies, and then close the scope. */
13031
13032 static void
13033 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13034 {
13035 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13036 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13037 CORE_ADDR lowpc, highpc;
13038 struct die_info *child_die;
13039 CORE_ADDR baseaddr;
13040
13041 baseaddr = objfile->text_section_offset ();
13042
13043 /* Ignore blocks with missing or invalid low and high pc attributes. */
13044 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13045 as multiple lexical blocks? Handling children in a sane way would
13046 be nasty. Might be easier to properly extend generic blocks to
13047 describe ranges. */
13048 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13049 {
13050 case PC_BOUNDS_NOT_PRESENT:
13051 /* DW_TAG_lexical_block has no attributes, process its children as if
13052 there was no wrapping by that DW_TAG_lexical_block.
13053 GCC does no longer produces such DWARF since GCC r224161. */
13054 for (child_die = die->child;
13055 child_die != NULL && child_die->tag;
13056 child_die = sibling_die (child_die))
13057 process_die (child_die, cu);
13058 return;
13059 case PC_BOUNDS_INVALID:
13060 return;
13061 }
13062 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13063 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13064
13065 cu->get_builder ()->push_context (0, lowpc);
13066 if (die->child != NULL)
13067 {
13068 child_die = die->child;
13069 while (child_die && child_die->tag)
13070 {
13071 process_die (child_die, cu);
13072 child_die = sibling_die (child_die);
13073 }
13074 }
13075 inherit_abstract_dies (die, cu);
13076 struct context_stack cstk = cu->get_builder ()->pop_context ();
13077
13078 if (*cu->get_builder ()->get_local_symbols () != NULL
13079 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13080 {
13081 struct block *block
13082 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13083 cstk.start_addr, highpc);
13084
13085 /* Note that recording ranges after traversing children, as we
13086 do here, means that recording a parent's ranges entails
13087 walking across all its children's ranges as they appear in
13088 the address map, which is quadratic behavior.
13089
13090 It would be nicer to record the parent's ranges before
13091 traversing its children, simply overriding whatever you find
13092 there. But since we don't even decide whether to create a
13093 block until after we've traversed its children, that's hard
13094 to do. */
13095 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13096 }
13097 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13098 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13099 }
13100
13101 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13102
13103 static void
13104 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13105 {
13106 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13107 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13108 CORE_ADDR pc, baseaddr;
13109 struct attribute *attr;
13110 struct call_site *call_site, call_site_local;
13111 void **slot;
13112 int nparams;
13113 struct die_info *child_die;
13114
13115 baseaddr = objfile->text_section_offset ();
13116
13117 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13118 if (attr == NULL)
13119 {
13120 /* This was a pre-DWARF-5 GNU extension alias
13121 for DW_AT_call_return_pc. */
13122 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13123 }
13124 if (!attr)
13125 {
13126 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13127 "DIE %s [in module %s]"),
13128 sect_offset_str (die->sect_off), objfile_name (objfile));
13129 return;
13130 }
13131 pc = attr->value_as_address () + baseaddr;
13132 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13133
13134 if (cu->call_site_htab == NULL)
13135 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13136 NULL, &objfile->objfile_obstack,
13137 hashtab_obstack_allocate, NULL);
13138 call_site_local.pc = pc;
13139 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13140 if (*slot != NULL)
13141 {
13142 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13143 "DIE %s [in module %s]"),
13144 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13145 objfile_name (objfile));
13146 return;
13147 }
13148
13149 /* Count parameters at the caller. */
13150
13151 nparams = 0;
13152 for (child_die = die->child; child_die && child_die->tag;
13153 child_die = sibling_die (child_die))
13154 {
13155 if (child_die->tag != DW_TAG_call_site_parameter
13156 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13157 {
13158 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13159 "DW_TAG_call_site child DIE %s [in module %s]"),
13160 child_die->tag, sect_offset_str (child_die->sect_off),
13161 objfile_name (objfile));
13162 continue;
13163 }
13164
13165 nparams++;
13166 }
13167
13168 call_site
13169 = ((struct call_site *)
13170 obstack_alloc (&objfile->objfile_obstack,
13171 sizeof (*call_site)
13172 + (sizeof (*call_site->parameter) * (nparams - 1))));
13173 *slot = call_site;
13174 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13175 call_site->pc = pc;
13176
13177 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13178 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13179 {
13180 struct die_info *func_die;
13181
13182 /* Skip also over DW_TAG_inlined_subroutine. */
13183 for (func_die = die->parent;
13184 func_die && func_die->tag != DW_TAG_subprogram
13185 && func_die->tag != DW_TAG_subroutine_type;
13186 func_die = func_die->parent);
13187
13188 /* DW_AT_call_all_calls is a superset
13189 of DW_AT_call_all_tail_calls. */
13190 if (func_die
13191 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13192 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13193 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13194 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13195 {
13196 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13197 not complete. But keep CALL_SITE for look ups via call_site_htab,
13198 both the initial caller containing the real return address PC and
13199 the final callee containing the current PC of a chain of tail
13200 calls do not need to have the tail call list complete. But any
13201 function candidate for a virtual tail call frame searched via
13202 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13203 determined unambiguously. */
13204 }
13205 else
13206 {
13207 struct type *func_type = NULL;
13208
13209 if (func_die)
13210 func_type = get_die_type (func_die, cu);
13211 if (func_type != NULL)
13212 {
13213 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13214
13215 /* Enlist this call site to the function. */
13216 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13217 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13218 }
13219 else
13220 complaint (_("Cannot find function owning DW_TAG_call_site "
13221 "DIE %s [in module %s]"),
13222 sect_offset_str (die->sect_off), objfile_name (objfile));
13223 }
13224 }
13225
13226 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13227 if (attr == NULL)
13228 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13229 if (attr == NULL)
13230 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13231 if (attr == NULL)
13232 {
13233 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13234 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13235 }
13236 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13237 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13238 /* Keep NULL DWARF_BLOCK. */;
13239 else if (attr->form_is_block ())
13240 {
13241 struct dwarf2_locexpr_baton *dlbaton;
13242
13243 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13244 dlbaton->data = DW_BLOCK (attr)->data;
13245 dlbaton->size = DW_BLOCK (attr)->size;
13246 dlbaton->per_cu = cu->per_cu;
13247
13248 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13249 }
13250 else if (attr->form_is_ref ())
13251 {
13252 struct dwarf2_cu *target_cu = cu;
13253 struct die_info *target_die;
13254
13255 target_die = follow_die_ref (die, attr, &target_cu);
13256 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13257 if (die_is_declaration (target_die, target_cu))
13258 {
13259 const char *target_physname;
13260
13261 /* Prefer the mangled name; otherwise compute the demangled one. */
13262 target_physname = dw2_linkage_name (target_die, target_cu);
13263 if (target_physname == NULL)
13264 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13265 if (target_physname == NULL)
13266 complaint (_("DW_AT_call_target target DIE has invalid "
13267 "physname, for referencing DIE %s [in module %s]"),
13268 sect_offset_str (die->sect_off), objfile_name (objfile));
13269 else
13270 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13271 }
13272 else
13273 {
13274 CORE_ADDR lowpc;
13275
13276 /* DW_AT_entry_pc should be preferred. */
13277 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13278 <= PC_BOUNDS_INVALID)
13279 complaint (_("DW_AT_call_target target DIE has invalid "
13280 "low pc, for referencing DIE %s [in module %s]"),
13281 sect_offset_str (die->sect_off), objfile_name (objfile));
13282 else
13283 {
13284 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13285 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13286 }
13287 }
13288 }
13289 else
13290 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13291 "block nor reference, for DIE %s [in module %s]"),
13292 sect_offset_str (die->sect_off), objfile_name (objfile));
13293
13294 call_site->per_cu = cu->per_cu;
13295
13296 for (child_die = die->child;
13297 child_die && child_die->tag;
13298 child_die = sibling_die (child_die))
13299 {
13300 struct call_site_parameter *parameter;
13301 struct attribute *loc, *origin;
13302
13303 if (child_die->tag != DW_TAG_call_site_parameter
13304 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13305 {
13306 /* Already printed the complaint above. */
13307 continue;
13308 }
13309
13310 gdb_assert (call_site->parameter_count < nparams);
13311 parameter = &call_site->parameter[call_site->parameter_count];
13312
13313 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13314 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13315 register is contained in DW_AT_call_value. */
13316
13317 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13318 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13319 if (origin == NULL)
13320 {
13321 /* This was a pre-DWARF-5 GNU extension alias
13322 for DW_AT_call_parameter. */
13323 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13324 }
13325 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13326 {
13327 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13328
13329 sect_offset sect_off
13330 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13331 if (!cu->header.offset_in_cu_p (sect_off))
13332 {
13333 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13334 binding can be done only inside one CU. Such referenced DIE
13335 therefore cannot be even moved to DW_TAG_partial_unit. */
13336 complaint (_("DW_AT_call_parameter offset is not in CU for "
13337 "DW_TAG_call_site child DIE %s [in module %s]"),
13338 sect_offset_str (child_die->sect_off),
13339 objfile_name (objfile));
13340 continue;
13341 }
13342 parameter->u.param_cu_off
13343 = (cu_offset) (sect_off - cu->header.sect_off);
13344 }
13345 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13346 {
13347 complaint (_("No DW_FORM_block* DW_AT_location for "
13348 "DW_TAG_call_site child DIE %s [in module %s]"),
13349 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13350 continue;
13351 }
13352 else
13353 {
13354 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13355 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13356 if (parameter->u.dwarf_reg != -1)
13357 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13358 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13359 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13360 &parameter->u.fb_offset))
13361 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13362 else
13363 {
13364 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13365 "for DW_FORM_block* DW_AT_location is supported for "
13366 "DW_TAG_call_site child DIE %s "
13367 "[in module %s]"),
13368 sect_offset_str (child_die->sect_off),
13369 objfile_name (objfile));
13370 continue;
13371 }
13372 }
13373
13374 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13375 if (attr == NULL)
13376 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13377 if (attr == NULL || !attr->form_is_block ())
13378 {
13379 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13380 "DW_TAG_call_site child DIE %s [in module %s]"),
13381 sect_offset_str (child_die->sect_off),
13382 objfile_name (objfile));
13383 continue;
13384 }
13385 parameter->value = DW_BLOCK (attr)->data;
13386 parameter->value_size = DW_BLOCK (attr)->size;
13387
13388 /* Parameters are not pre-cleared by memset above. */
13389 parameter->data_value = NULL;
13390 parameter->data_value_size = 0;
13391 call_site->parameter_count++;
13392
13393 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13394 if (attr == NULL)
13395 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13396 if (attr != nullptr)
13397 {
13398 if (!attr->form_is_block ())
13399 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13400 "DW_TAG_call_site child DIE %s [in module %s]"),
13401 sect_offset_str (child_die->sect_off),
13402 objfile_name (objfile));
13403 else
13404 {
13405 parameter->data_value = DW_BLOCK (attr)->data;
13406 parameter->data_value_size = DW_BLOCK (attr)->size;
13407 }
13408 }
13409 }
13410 }
13411
13412 /* Helper function for read_variable. If DIE represents a virtual
13413 table, then return the type of the concrete object that is
13414 associated with the virtual table. Otherwise, return NULL. */
13415
13416 static struct type *
13417 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13418 {
13419 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13420 if (attr == NULL)
13421 return NULL;
13422
13423 /* Find the type DIE. */
13424 struct die_info *type_die = NULL;
13425 struct dwarf2_cu *type_cu = cu;
13426
13427 if (attr->form_is_ref ())
13428 type_die = follow_die_ref (die, attr, &type_cu);
13429 if (type_die == NULL)
13430 return NULL;
13431
13432 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13433 return NULL;
13434 return die_containing_type (type_die, type_cu);
13435 }
13436
13437 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13438
13439 static void
13440 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13441 {
13442 struct rust_vtable_symbol *storage = NULL;
13443
13444 if (cu->language == language_rust)
13445 {
13446 struct type *containing_type = rust_containing_type (die, cu);
13447
13448 if (containing_type != NULL)
13449 {
13450 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13451
13452 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13453 initialize_objfile_symbol (storage);
13454 storage->concrete_type = containing_type;
13455 storage->subclass = SYMBOL_RUST_VTABLE;
13456 }
13457 }
13458
13459 struct symbol *res = new_symbol (die, NULL, cu, storage);
13460 struct attribute *abstract_origin
13461 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13462 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13463 if (res == NULL && loc && abstract_origin)
13464 {
13465 /* We have a variable without a name, but with a location and an abstract
13466 origin. This may be a concrete instance of an abstract variable
13467 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13468 later. */
13469 struct dwarf2_cu *origin_cu = cu;
13470 struct die_info *origin_die
13471 = follow_die_ref (die, abstract_origin, &origin_cu);
13472 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13473 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13474 }
13475 }
13476
13477 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13478 reading .debug_rnglists.
13479 Callback's type should be:
13480 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13481 Return true if the attributes are present and valid, otherwise,
13482 return false. */
13483
13484 template <typename Callback>
13485 static bool
13486 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13487 Callback &&callback)
13488 {
13489 struct dwarf2_per_objfile *dwarf2_per_objfile
13490 = cu->per_cu->dwarf2_per_objfile;
13491 struct objfile *objfile = dwarf2_per_objfile->objfile;
13492 bfd *obfd = objfile->obfd;
13493 /* Base address selection entry. */
13494 CORE_ADDR base;
13495 int found_base;
13496 const gdb_byte *buffer;
13497 CORE_ADDR baseaddr;
13498 bool overflow = false;
13499
13500 found_base = cu->base_known;
13501 base = cu->base_address;
13502
13503 dwarf2_per_objfile->rnglists.read (objfile);
13504 if (offset >= dwarf2_per_objfile->rnglists.size)
13505 {
13506 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13507 offset);
13508 return false;
13509 }
13510 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13511
13512 baseaddr = objfile->text_section_offset ();
13513
13514 while (1)
13515 {
13516 /* Initialize it due to a false compiler warning. */
13517 CORE_ADDR range_beginning = 0, range_end = 0;
13518 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13519 + dwarf2_per_objfile->rnglists.size);
13520 unsigned int bytes_read;
13521
13522 if (buffer == buf_end)
13523 {
13524 overflow = true;
13525 break;
13526 }
13527 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13528 switch (rlet)
13529 {
13530 case DW_RLE_end_of_list:
13531 break;
13532 case DW_RLE_base_address:
13533 if (buffer + cu->header.addr_size > buf_end)
13534 {
13535 overflow = true;
13536 break;
13537 }
13538 base = cu->header.read_address (obfd, buffer, &bytes_read);
13539 found_base = 1;
13540 buffer += bytes_read;
13541 break;
13542 case DW_RLE_start_length:
13543 if (buffer + cu->header.addr_size > buf_end)
13544 {
13545 overflow = true;
13546 break;
13547 }
13548 range_beginning = cu->header.read_address (obfd, buffer,
13549 &bytes_read);
13550 buffer += bytes_read;
13551 range_end = (range_beginning
13552 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13553 buffer += bytes_read;
13554 if (buffer > buf_end)
13555 {
13556 overflow = true;
13557 break;
13558 }
13559 break;
13560 case DW_RLE_offset_pair:
13561 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13562 buffer += bytes_read;
13563 if (buffer > buf_end)
13564 {
13565 overflow = true;
13566 break;
13567 }
13568 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13569 buffer += bytes_read;
13570 if (buffer > buf_end)
13571 {
13572 overflow = true;
13573 break;
13574 }
13575 break;
13576 case DW_RLE_start_end:
13577 if (buffer + 2 * cu->header.addr_size > buf_end)
13578 {
13579 overflow = true;
13580 break;
13581 }
13582 range_beginning = cu->header.read_address (obfd, buffer,
13583 &bytes_read);
13584 buffer += bytes_read;
13585 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13586 buffer += bytes_read;
13587 break;
13588 default:
13589 complaint (_("Invalid .debug_rnglists data (no base address)"));
13590 return false;
13591 }
13592 if (rlet == DW_RLE_end_of_list || overflow)
13593 break;
13594 if (rlet == DW_RLE_base_address)
13595 continue;
13596
13597 if (!found_base)
13598 {
13599 /* We have no valid base address for the ranges
13600 data. */
13601 complaint (_("Invalid .debug_rnglists data (no base address)"));
13602 return false;
13603 }
13604
13605 if (range_beginning > range_end)
13606 {
13607 /* Inverted range entries are invalid. */
13608 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13609 return false;
13610 }
13611
13612 /* Empty range entries have no effect. */
13613 if (range_beginning == range_end)
13614 continue;
13615
13616 range_beginning += base;
13617 range_end += base;
13618
13619 /* A not-uncommon case of bad debug info.
13620 Don't pollute the addrmap with bad data. */
13621 if (range_beginning + baseaddr == 0
13622 && !dwarf2_per_objfile->has_section_at_zero)
13623 {
13624 complaint (_(".debug_rnglists entry has start address of zero"
13625 " [in module %s]"), objfile_name (objfile));
13626 continue;
13627 }
13628
13629 callback (range_beginning, range_end);
13630 }
13631
13632 if (overflow)
13633 {
13634 complaint (_("Offset %d is not terminated "
13635 "for DW_AT_ranges attribute"),
13636 offset);
13637 return false;
13638 }
13639
13640 return true;
13641 }
13642
13643 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13644 Callback's type should be:
13645 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13646 Return 1 if the attributes are present and valid, otherwise, return 0. */
13647
13648 template <typename Callback>
13649 static int
13650 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13651 Callback &&callback)
13652 {
13653 struct dwarf2_per_objfile *dwarf2_per_objfile
13654 = cu->per_cu->dwarf2_per_objfile;
13655 struct objfile *objfile = dwarf2_per_objfile->objfile;
13656 struct comp_unit_head *cu_header = &cu->header;
13657 bfd *obfd = objfile->obfd;
13658 unsigned int addr_size = cu_header->addr_size;
13659 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13660 /* Base address selection entry. */
13661 CORE_ADDR base;
13662 int found_base;
13663 unsigned int dummy;
13664 const gdb_byte *buffer;
13665 CORE_ADDR baseaddr;
13666
13667 if (cu_header->version >= 5)
13668 return dwarf2_rnglists_process (offset, cu, callback);
13669
13670 found_base = cu->base_known;
13671 base = cu->base_address;
13672
13673 dwarf2_per_objfile->ranges.read (objfile);
13674 if (offset >= dwarf2_per_objfile->ranges.size)
13675 {
13676 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13677 offset);
13678 return 0;
13679 }
13680 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13681
13682 baseaddr = objfile->text_section_offset ();
13683
13684 while (1)
13685 {
13686 CORE_ADDR range_beginning, range_end;
13687
13688 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13689 buffer += addr_size;
13690 range_end = cu->header.read_address (obfd, buffer, &dummy);
13691 buffer += addr_size;
13692 offset += 2 * addr_size;
13693
13694 /* An end of list marker is a pair of zero addresses. */
13695 if (range_beginning == 0 && range_end == 0)
13696 /* Found the end of list entry. */
13697 break;
13698
13699 /* Each base address selection entry is a pair of 2 values.
13700 The first is the largest possible address, the second is
13701 the base address. Check for a base address here. */
13702 if ((range_beginning & mask) == mask)
13703 {
13704 /* If we found the largest possible address, then we already
13705 have the base address in range_end. */
13706 base = range_end;
13707 found_base = 1;
13708 continue;
13709 }
13710
13711 if (!found_base)
13712 {
13713 /* We have no valid base address for the ranges
13714 data. */
13715 complaint (_("Invalid .debug_ranges data (no base address)"));
13716 return 0;
13717 }
13718
13719 if (range_beginning > range_end)
13720 {
13721 /* Inverted range entries are invalid. */
13722 complaint (_("Invalid .debug_ranges data (inverted range)"));
13723 return 0;
13724 }
13725
13726 /* Empty range entries have no effect. */
13727 if (range_beginning == range_end)
13728 continue;
13729
13730 range_beginning += base;
13731 range_end += base;
13732
13733 /* A not-uncommon case of bad debug info.
13734 Don't pollute the addrmap with bad data. */
13735 if (range_beginning + baseaddr == 0
13736 && !dwarf2_per_objfile->has_section_at_zero)
13737 {
13738 complaint (_(".debug_ranges entry has start address of zero"
13739 " [in module %s]"), objfile_name (objfile));
13740 continue;
13741 }
13742
13743 callback (range_beginning, range_end);
13744 }
13745
13746 return 1;
13747 }
13748
13749 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13750 Return 1 if the attributes are present and valid, otherwise, return 0.
13751 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13752
13753 static int
13754 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13755 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13756 dwarf2_psymtab *ranges_pst)
13757 {
13758 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13759 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13760 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13761 int low_set = 0;
13762 CORE_ADDR low = 0;
13763 CORE_ADDR high = 0;
13764 int retval;
13765
13766 retval = dwarf2_ranges_process (offset, cu,
13767 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13768 {
13769 if (ranges_pst != NULL)
13770 {
13771 CORE_ADDR lowpc;
13772 CORE_ADDR highpc;
13773
13774 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13775 range_beginning + baseaddr)
13776 - baseaddr);
13777 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13778 range_end + baseaddr)
13779 - baseaddr);
13780 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13781 lowpc, highpc - 1, ranges_pst);
13782 }
13783
13784 /* FIXME: This is recording everything as a low-high
13785 segment of consecutive addresses. We should have a
13786 data structure for discontiguous block ranges
13787 instead. */
13788 if (! low_set)
13789 {
13790 low = range_beginning;
13791 high = range_end;
13792 low_set = 1;
13793 }
13794 else
13795 {
13796 if (range_beginning < low)
13797 low = range_beginning;
13798 if (range_end > high)
13799 high = range_end;
13800 }
13801 });
13802 if (!retval)
13803 return 0;
13804
13805 if (! low_set)
13806 /* If the first entry is an end-of-list marker, the range
13807 describes an empty scope, i.e. no instructions. */
13808 return 0;
13809
13810 if (low_return)
13811 *low_return = low;
13812 if (high_return)
13813 *high_return = high;
13814 return 1;
13815 }
13816
13817 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13818 definition for the return value. *LOWPC and *HIGHPC are set iff
13819 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13820
13821 static enum pc_bounds_kind
13822 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13823 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13824 dwarf2_psymtab *pst)
13825 {
13826 struct dwarf2_per_objfile *dwarf2_per_objfile
13827 = cu->per_cu->dwarf2_per_objfile;
13828 struct attribute *attr;
13829 struct attribute *attr_high;
13830 CORE_ADDR low = 0;
13831 CORE_ADDR high = 0;
13832 enum pc_bounds_kind ret;
13833
13834 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13835 if (attr_high)
13836 {
13837 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13838 if (attr != nullptr)
13839 {
13840 low = attr->value_as_address ();
13841 high = attr_high->value_as_address ();
13842 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13843 high += low;
13844 }
13845 else
13846 /* Found high w/o low attribute. */
13847 return PC_BOUNDS_INVALID;
13848
13849 /* Found consecutive range of addresses. */
13850 ret = PC_BOUNDS_HIGH_LOW;
13851 }
13852 else
13853 {
13854 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13855 if (attr != NULL)
13856 {
13857 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13858 We take advantage of the fact that DW_AT_ranges does not appear
13859 in DW_TAG_compile_unit of DWO files. */
13860 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13861 unsigned int ranges_offset = (DW_UNSND (attr)
13862 + (need_ranges_base
13863 ? cu->ranges_base
13864 : 0));
13865
13866 /* Value of the DW_AT_ranges attribute is the offset in the
13867 .debug_ranges section. */
13868 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13869 return PC_BOUNDS_INVALID;
13870 /* Found discontinuous range of addresses. */
13871 ret = PC_BOUNDS_RANGES;
13872 }
13873 else
13874 return PC_BOUNDS_NOT_PRESENT;
13875 }
13876
13877 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13878 if (high <= low)
13879 return PC_BOUNDS_INVALID;
13880
13881 /* When using the GNU linker, .gnu.linkonce. sections are used to
13882 eliminate duplicate copies of functions and vtables and such.
13883 The linker will arbitrarily choose one and discard the others.
13884 The AT_*_pc values for such functions refer to local labels in
13885 these sections. If the section from that file was discarded, the
13886 labels are not in the output, so the relocs get a value of 0.
13887 If this is a discarded function, mark the pc bounds as invalid,
13888 so that GDB will ignore it. */
13889 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13890 return PC_BOUNDS_INVALID;
13891
13892 *lowpc = low;
13893 if (highpc)
13894 *highpc = high;
13895 return ret;
13896 }
13897
13898 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13899 its low and high PC addresses. Do nothing if these addresses could not
13900 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13901 and HIGHPC to the high address if greater than HIGHPC. */
13902
13903 static void
13904 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13905 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13906 struct dwarf2_cu *cu)
13907 {
13908 CORE_ADDR low, high;
13909 struct die_info *child = die->child;
13910
13911 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13912 {
13913 *lowpc = std::min (*lowpc, low);
13914 *highpc = std::max (*highpc, high);
13915 }
13916
13917 /* If the language does not allow nested subprograms (either inside
13918 subprograms or lexical blocks), we're done. */
13919 if (cu->language != language_ada)
13920 return;
13921
13922 /* Check all the children of the given DIE. If it contains nested
13923 subprograms, then check their pc bounds. Likewise, we need to
13924 check lexical blocks as well, as they may also contain subprogram
13925 definitions. */
13926 while (child && child->tag)
13927 {
13928 if (child->tag == DW_TAG_subprogram
13929 || child->tag == DW_TAG_lexical_block)
13930 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13931 child = sibling_die (child);
13932 }
13933 }
13934
13935 /* Get the low and high pc's represented by the scope DIE, and store
13936 them in *LOWPC and *HIGHPC. If the correct values can't be
13937 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13938
13939 static void
13940 get_scope_pc_bounds (struct die_info *die,
13941 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13942 struct dwarf2_cu *cu)
13943 {
13944 CORE_ADDR best_low = (CORE_ADDR) -1;
13945 CORE_ADDR best_high = (CORE_ADDR) 0;
13946 CORE_ADDR current_low, current_high;
13947
13948 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13949 >= PC_BOUNDS_RANGES)
13950 {
13951 best_low = current_low;
13952 best_high = current_high;
13953 }
13954 else
13955 {
13956 struct die_info *child = die->child;
13957
13958 while (child && child->tag)
13959 {
13960 switch (child->tag) {
13961 case DW_TAG_subprogram:
13962 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13963 break;
13964 case DW_TAG_namespace:
13965 case DW_TAG_module:
13966 /* FIXME: carlton/2004-01-16: Should we do this for
13967 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13968 that current GCC's always emit the DIEs corresponding
13969 to definitions of methods of classes as children of a
13970 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13971 the DIEs giving the declarations, which could be
13972 anywhere). But I don't see any reason why the
13973 standards says that they have to be there. */
13974 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13975
13976 if (current_low != ((CORE_ADDR) -1))
13977 {
13978 best_low = std::min (best_low, current_low);
13979 best_high = std::max (best_high, current_high);
13980 }
13981 break;
13982 default:
13983 /* Ignore. */
13984 break;
13985 }
13986
13987 child = sibling_die (child);
13988 }
13989 }
13990
13991 *lowpc = best_low;
13992 *highpc = best_high;
13993 }
13994
13995 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13996 in DIE. */
13997
13998 static void
13999 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14000 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14001 {
14002 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14003 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14004 struct attribute *attr;
14005 struct attribute *attr_high;
14006
14007 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14008 if (attr_high)
14009 {
14010 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14011 if (attr != nullptr)
14012 {
14013 CORE_ADDR low = attr->value_as_address ();
14014 CORE_ADDR high = attr_high->value_as_address ();
14015
14016 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14017 high += low;
14018
14019 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14020 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14021 cu->get_builder ()->record_block_range (block, low, high - 1);
14022 }
14023 }
14024
14025 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14026 if (attr != nullptr)
14027 {
14028 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14029 We take advantage of the fact that DW_AT_ranges does not appear
14030 in DW_TAG_compile_unit of DWO files. */
14031 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14032
14033 /* The value of the DW_AT_ranges attribute is the offset of the
14034 address range list in the .debug_ranges section. */
14035 unsigned long offset = (DW_UNSND (attr)
14036 + (need_ranges_base ? cu->ranges_base : 0));
14037
14038 std::vector<blockrange> blockvec;
14039 dwarf2_ranges_process (offset, cu,
14040 [&] (CORE_ADDR start, CORE_ADDR end)
14041 {
14042 start += baseaddr;
14043 end += baseaddr;
14044 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14045 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14046 cu->get_builder ()->record_block_range (block, start, end - 1);
14047 blockvec.emplace_back (start, end);
14048 });
14049
14050 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14051 }
14052 }
14053
14054 /* Check whether the producer field indicates either of GCC < 4.6, or the
14055 Intel C/C++ compiler, and cache the result in CU. */
14056
14057 static void
14058 check_producer (struct dwarf2_cu *cu)
14059 {
14060 int major, minor;
14061
14062 if (cu->producer == NULL)
14063 {
14064 /* For unknown compilers expect their behavior is DWARF version
14065 compliant.
14066
14067 GCC started to support .debug_types sections by -gdwarf-4 since
14068 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14069 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14070 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14071 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14072 }
14073 else if (producer_is_gcc (cu->producer, &major, &minor))
14074 {
14075 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14076 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14077 }
14078 else if (producer_is_icc (cu->producer, &major, &minor))
14079 {
14080 cu->producer_is_icc = true;
14081 cu->producer_is_icc_lt_14 = major < 14;
14082 }
14083 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14084 cu->producer_is_codewarrior = true;
14085 else
14086 {
14087 /* For other non-GCC compilers, expect their behavior is DWARF version
14088 compliant. */
14089 }
14090
14091 cu->checked_producer = true;
14092 }
14093
14094 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14095 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14096 during 4.6.0 experimental. */
14097
14098 static bool
14099 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14100 {
14101 if (!cu->checked_producer)
14102 check_producer (cu);
14103
14104 return cu->producer_is_gxx_lt_4_6;
14105 }
14106
14107
14108 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14109 with incorrect is_stmt attributes. */
14110
14111 static bool
14112 producer_is_codewarrior (struct dwarf2_cu *cu)
14113 {
14114 if (!cu->checked_producer)
14115 check_producer (cu);
14116
14117 return cu->producer_is_codewarrior;
14118 }
14119
14120 /* Return the default accessibility type if it is not overridden by
14121 DW_AT_accessibility. */
14122
14123 static enum dwarf_access_attribute
14124 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14125 {
14126 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14127 {
14128 /* The default DWARF 2 accessibility for members is public, the default
14129 accessibility for inheritance is private. */
14130
14131 if (die->tag != DW_TAG_inheritance)
14132 return DW_ACCESS_public;
14133 else
14134 return DW_ACCESS_private;
14135 }
14136 else
14137 {
14138 /* DWARF 3+ defines the default accessibility a different way. The same
14139 rules apply now for DW_TAG_inheritance as for the members and it only
14140 depends on the container kind. */
14141
14142 if (die->parent->tag == DW_TAG_class_type)
14143 return DW_ACCESS_private;
14144 else
14145 return DW_ACCESS_public;
14146 }
14147 }
14148
14149 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14150 offset. If the attribute was not found return 0, otherwise return
14151 1. If it was found but could not properly be handled, set *OFFSET
14152 to 0. */
14153
14154 static int
14155 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14156 LONGEST *offset)
14157 {
14158 struct attribute *attr;
14159
14160 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14161 if (attr != NULL)
14162 {
14163 *offset = 0;
14164
14165 /* Note that we do not check for a section offset first here.
14166 This is because DW_AT_data_member_location is new in DWARF 4,
14167 so if we see it, we can assume that a constant form is really
14168 a constant and not a section offset. */
14169 if (attr->form_is_constant ())
14170 *offset = dwarf2_get_attr_constant_value (attr, 0);
14171 else if (attr->form_is_section_offset ())
14172 dwarf2_complex_location_expr_complaint ();
14173 else if (attr->form_is_block ())
14174 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14175 else
14176 dwarf2_complex_location_expr_complaint ();
14177
14178 return 1;
14179 }
14180
14181 return 0;
14182 }
14183
14184 /* Add an aggregate field to the field list. */
14185
14186 static void
14187 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14188 struct dwarf2_cu *cu)
14189 {
14190 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14191 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14192 struct nextfield *new_field;
14193 struct attribute *attr;
14194 struct field *fp;
14195 const char *fieldname = "";
14196
14197 if (die->tag == DW_TAG_inheritance)
14198 {
14199 fip->baseclasses.emplace_back ();
14200 new_field = &fip->baseclasses.back ();
14201 }
14202 else
14203 {
14204 fip->fields.emplace_back ();
14205 new_field = &fip->fields.back ();
14206 }
14207
14208 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14209 if (attr != nullptr)
14210 new_field->accessibility = DW_UNSND (attr);
14211 else
14212 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14213 if (new_field->accessibility != DW_ACCESS_public)
14214 fip->non_public_fields = 1;
14215
14216 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14217 if (attr != nullptr)
14218 new_field->virtuality = DW_UNSND (attr);
14219 else
14220 new_field->virtuality = DW_VIRTUALITY_none;
14221
14222 fp = &new_field->field;
14223
14224 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14225 {
14226 LONGEST offset;
14227
14228 /* Data member other than a C++ static data member. */
14229
14230 /* Get type of field. */
14231 fp->type = die_type (die, cu);
14232
14233 SET_FIELD_BITPOS (*fp, 0);
14234
14235 /* Get bit size of field (zero if none). */
14236 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14237 if (attr != nullptr)
14238 {
14239 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14240 }
14241 else
14242 {
14243 FIELD_BITSIZE (*fp) = 0;
14244 }
14245
14246 /* Get bit offset of field. */
14247 if (handle_data_member_location (die, cu, &offset))
14248 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14249 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14250 if (attr != nullptr)
14251 {
14252 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14253 {
14254 /* For big endian bits, the DW_AT_bit_offset gives the
14255 additional bit offset from the MSB of the containing
14256 anonymous object to the MSB of the field. We don't
14257 have to do anything special since we don't need to
14258 know the size of the anonymous object. */
14259 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14260 }
14261 else
14262 {
14263 /* For little endian bits, compute the bit offset to the
14264 MSB of the anonymous object, subtract off the number of
14265 bits from the MSB of the field to the MSB of the
14266 object, and then subtract off the number of bits of
14267 the field itself. The result is the bit offset of
14268 the LSB of the field. */
14269 int anonymous_size;
14270 int bit_offset = DW_UNSND (attr);
14271
14272 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14273 if (attr != nullptr)
14274 {
14275 /* The size of the anonymous object containing
14276 the bit field is explicit, so use the
14277 indicated size (in bytes). */
14278 anonymous_size = DW_UNSND (attr);
14279 }
14280 else
14281 {
14282 /* The size of the anonymous object containing
14283 the bit field must be inferred from the type
14284 attribute of the data member containing the
14285 bit field. */
14286 anonymous_size = TYPE_LENGTH (fp->type);
14287 }
14288 SET_FIELD_BITPOS (*fp,
14289 (FIELD_BITPOS (*fp)
14290 + anonymous_size * bits_per_byte
14291 - bit_offset - FIELD_BITSIZE (*fp)));
14292 }
14293 }
14294 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14295 if (attr != NULL)
14296 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14297 + dwarf2_get_attr_constant_value (attr, 0)));
14298
14299 /* Get name of field. */
14300 fieldname = dwarf2_name (die, cu);
14301 if (fieldname == NULL)
14302 fieldname = "";
14303
14304 /* The name is already allocated along with this objfile, so we don't
14305 need to duplicate it for the type. */
14306 fp->name = fieldname;
14307
14308 /* Change accessibility for artificial fields (e.g. virtual table
14309 pointer or virtual base class pointer) to private. */
14310 if (dwarf2_attr (die, DW_AT_artificial, cu))
14311 {
14312 FIELD_ARTIFICIAL (*fp) = 1;
14313 new_field->accessibility = DW_ACCESS_private;
14314 fip->non_public_fields = 1;
14315 }
14316 }
14317 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14318 {
14319 /* C++ static member. */
14320
14321 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14322 is a declaration, but all versions of G++ as of this writing
14323 (so through at least 3.2.1) incorrectly generate
14324 DW_TAG_variable tags. */
14325
14326 const char *physname;
14327
14328 /* Get name of field. */
14329 fieldname = dwarf2_name (die, cu);
14330 if (fieldname == NULL)
14331 return;
14332
14333 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14334 if (attr
14335 /* Only create a symbol if this is an external value.
14336 new_symbol checks this and puts the value in the global symbol
14337 table, which we want. If it is not external, new_symbol
14338 will try to put the value in cu->list_in_scope which is wrong. */
14339 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14340 {
14341 /* A static const member, not much different than an enum as far as
14342 we're concerned, except that we can support more types. */
14343 new_symbol (die, NULL, cu);
14344 }
14345
14346 /* Get physical name. */
14347 physname = dwarf2_physname (fieldname, die, cu);
14348
14349 /* The name is already allocated along with this objfile, so we don't
14350 need to duplicate it for the type. */
14351 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14352 FIELD_TYPE (*fp) = die_type (die, cu);
14353 FIELD_NAME (*fp) = fieldname;
14354 }
14355 else if (die->tag == DW_TAG_inheritance)
14356 {
14357 LONGEST offset;
14358
14359 /* C++ base class field. */
14360 if (handle_data_member_location (die, cu, &offset))
14361 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14362 FIELD_BITSIZE (*fp) = 0;
14363 FIELD_TYPE (*fp) = die_type (die, cu);
14364 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14365 }
14366 else if (die->tag == DW_TAG_variant_part)
14367 {
14368 /* process_structure_scope will treat this DIE as a union. */
14369 process_structure_scope (die, cu);
14370
14371 /* The variant part is relative to the start of the enclosing
14372 structure. */
14373 SET_FIELD_BITPOS (*fp, 0);
14374 fp->type = get_die_type (die, cu);
14375 fp->artificial = 1;
14376 fp->name = "<<variant>>";
14377
14378 /* Normally a DW_TAG_variant_part won't have a size, but our
14379 representation requires one, so set it to the maximum of the
14380 child sizes, being sure to account for the offset at which
14381 each child is seen. */
14382 if (TYPE_LENGTH (fp->type) == 0)
14383 {
14384 unsigned max = 0;
14385 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14386 {
14387 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14388 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14389 if (len > max)
14390 max = len;
14391 }
14392 TYPE_LENGTH (fp->type) = max;
14393 }
14394 }
14395 else
14396 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14397 }
14398
14399 /* Can the type given by DIE define another type? */
14400
14401 static bool
14402 type_can_define_types (const struct die_info *die)
14403 {
14404 switch (die->tag)
14405 {
14406 case DW_TAG_typedef:
14407 case DW_TAG_class_type:
14408 case DW_TAG_structure_type:
14409 case DW_TAG_union_type:
14410 case DW_TAG_enumeration_type:
14411 return true;
14412
14413 default:
14414 return false;
14415 }
14416 }
14417
14418 /* Add a type definition defined in the scope of the FIP's class. */
14419
14420 static void
14421 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14422 struct dwarf2_cu *cu)
14423 {
14424 struct decl_field fp;
14425 memset (&fp, 0, sizeof (fp));
14426
14427 gdb_assert (type_can_define_types (die));
14428
14429 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14430 fp.name = dwarf2_name (die, cu);
14431 fp.type = read_type_die (die, cu);
14432
14433 /* Save accessibility. */
14434 enum dwarf_access_attribute accessibility;
14435 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14436 if (attr != NULL)
14437 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14438 else
14439 accessibility = dwarf2_default_access_attribute (die, cu);
14440 switch (accessibility)
14441 {
14442 case DW_ACCESS_public:
14443 /* The assumed value if neither private nor protected. */
14444 break;
14445 case DW_ACCESS_private:
14446 fp.is_private = 1;
14447 break;
14448 case DW_ACCESS_protected:
14449 fp.is_protected = 1;
14450 break;
14451 default:
14452 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14453 }
14454
14455 if (die->tag == DW_TAG_typedef)
14456 fip->typedef_field_list.push_back (fp);
14457 else
14458 fip->nested_types_list.push_back (fp);
14459 }
14460
14461 /* Create the vector of fields, and attach it to the type. */
14462
14463 static void
14464 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14465 struct dwarf2_cu *cu)
14466 {
14467 int nfields = fip->nfields ();
14468
14469 /* Record the field count, allocate space for the array of fields,
14470 and create blank accessibility bitfields if necessary. */
14471 TYPE_NFIELDS (type) = nfields;
14472 TYPE_FIELDS (type) = (struct field *)
14473 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14474
14475 if (fip->non_public_fields && cu->language != language_ada)
14476 {
14477 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14478
14479 TYPE_FIELD_PRIVATE_BITS (type) =
14480 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14481 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14482
14483 TYPE_FIELD_PROTECTED_BITS (type) =
14484 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14485 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14486
14487 TYPE_FIELD_IGNORE_BITS (type) =
14488 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14489 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14490 }
14491
14492 /* If the type has baseclasses, allocate and clear a bit vector for
14493 TYPE_FIELD_VIRTUAL_BITS. */
14494 if (!fip->baseclasses.empty () && cu->language != language_ada)
14495 {
14496 int num_bytes = B_BYTES (fip->baseclasses.size ());
14497 unsigned char *pointer;
14498
14499 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14500 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14501 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14502 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14503 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14504 }
14505
14506 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14507 {
14508 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14509
14510 for (int index = 0; index < nfields; ++index)
14511 {
14512 struct nextfield &field = fip->fields[index];
14513
14514 if (field.variant.is_discriminant)
14515 di->discriminant_index = index;
14516 else if (field.variant.default_branch)
14517 di->default_index = index;
14518 else
14519 di->discriminants[index] = field.variant.discriminant_value;
14520 }
14521 }
14522
14523 /* Copy the saved-up fields into the field vector. */
14524 for (int i = 0; i < nfields; ++i)
14525 {
14526 struct nextfield &field
14527 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14528 : fip->fields[i - fip->baseclasses.size ()]);
14529
14530 TYPE_FIELD (type, i) = field.field;
14531 switch (field.accessibility)
14532 {
14533 case DW_ACCESS_private:
14534 if (cu->language != language_ada)
14535 SET_TYPE_FIELD_PRIVATE (type, i);
14536 break;
14537
14538 case DW_ACCESS_protected:
14539 if (cu->language != language_ada)
14540 SET_TYPE_FIELD_PROTECTED (type, i);
14541 break;
14542
14543 case DW_ACCESS_public:
14544 break;
14545
14546 default:
14547 /* Unknown accessibility. Complain and treat it as public. */
14548 {
14549 complaint (_("unsupported accessibility %d"),
14550 field.accessibility);
14551 }
14552 break;
14553 }
14554 if (i < fip->baseclasses.size ())
14555 {
14556 switch (field.virtuality)
14557 {
14558 case DW_VIRTUALITY_virtual:
14559 case DW_VIRTUALITY_pure_virtual:
14560 if (cu->language == language_ada)
14561 error (_("unexpected virtuality in component of Ada type"));
14562 SET_TYPE_FIELD_VIRTUAL (type, i);
14563 break;
14564 }
14565 }
14566 }
14567 }
14568
14569 /* Return true if this member function is a constructor, false
14570 otherwise. */
14571
14572 static int
14573 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14574 {
14575 const char *fieldname;
14576 const char *type_name;
14577 int len;
14578
14579 if (die->parent == NULL)
14580 return 0;
14581
14582 if (die->parent->tag != DW_TAG_structure_type
14583 && die->parent->tag != DW_TAG_union_type
14584 && die->parent->tag != DW_TAG_class_type)
14585 return 0;
14586
14587 fieldname = dwarf2_name (die, cu);
14588 type_name = dwarf2_name (die->parent, cu);
14589 if (fieldname == NULL || type_name == NULL)
14590 return 0;
14591
14592 len = strlen (fieldname);
14593 return (strncmp (fieldname, type_name, len) == 0
14594 && (type_name[len] == '\0' || type_name[len] == '<'));
14595 }
14596
14597 /* Check if the given VALUE is a recognized enum
14598 dwarf_defaulted_attribute constant according to DWARF5 spec,
14599 Table 7.24. */
14600
14601 static bool
14602 is_valid_DW_AT_defaulted (ULONGEST value)
14603 {
14604 switch (value)
14605 {
14606 case DW_DEFAULTED_no:
14607 case DW_DEFAULTED_in_class:
14608 case DW_DEFAULTED_out_of_class:
14609 return true;
14610 }
14611
14612 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14613 return false;
14614 }
14615
14616 /* Add a member function to the proper fieldlist. */
14617
14618 static void
14619 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14620 struct type *type, struct dwarf2_cu *cu)
14621 {
14622 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14623 struct attribute *attr;
14624 int i;
14625 struct fnfieldlist *flp = nullptr;
14626 struct fn_field *fnp;
14627 const char *fieldname;
14628 struct type *this_type;
14629 enum dwarf_access_attribute accessibility;
14630
14631 if (cu->language == language_ada)
14632 error (_("unexpected member function in Ada type"));
14633
14634 /* Get name of member function. */
14635 fieldname = dwarf2_name (die, cu);
14636 if (fieldname == NULL)
14637 return;
14638
14639 /* Look up member function name in fieldlist. */
14640 for (i = 0; i < fip->fnfieldlists.size (); i++)
14641 {
14642 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14643 {
14644 flp = &fip->fnfieldlists[i];
14645 break;
14646 }
14647 }
14648
14649 /* Create a new fnfieldlist if necessary. */
14650 if (flp == nullptr)
14651 {
14652 fip->fnfieldlists.emplace_back ();
14653 flp = &fip->fnfieldlists.back ();
14654 flp->name = fieldname;
14655 i = fip->fnfieldlists.size () - 1;
14656 }
14657
14658 /* Create a new member function field and add it to the vector of
14659 fnfieldlists. */
14660 flp->fnfields.emplace_back ();
14661 fnp = &flp->fnfields.back ();
14662
14663 /* Delay processing of the physname until later. */
14664 if (cu->language == language_cplus)
14665 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14666 die, cu);
14667 else
14668 {
14669 const char *physname = dwarf2_physname (fieldname, die, cu);
14670 fnp->physname = physname ? physname : "";
14671 }
14672
14673 fnp->type = alloc_type (objfile);
14674 this_type = read_type_die (die, cu);
14675 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14676 {
14677 int nparams = TYPE_NFIELDS (this_type);
14678
14679 /* TYPE is the domain of this method, and THIS_TYPE is the type
14680 of the method itself (TYPE_CODE_METHOD). */
14681 smash_to_method_type (fnp->type, type,
14682 TYPE_TARGET_TYPE (this_type),
14683 TYPE_FIELDS (this_type),
14684 TYPE_NFIELDS (this_type),
14685 TYPE_VARARGS (this_type));
14686
14687 /* Handle static member functions.
14688 Dwarf2 has no clean way to discern C++ static and non-static
14689 member functions. G++ helps GDB by marking the first
14690 parameter for non-static member functions (which is the this
14691 pointer) as artificial. We obtain this information from
14692 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14693 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14694 fnp->voffset = VOFFSET_STATIC;
14695 }
14696 else
14697 complaint (_("member function type missing for '%s'"),
14698 dwarf2_full_name (fieldname, die, cu));
14699
14700 /* Get fcontext from DW_AT_containing_type if present. */
14701 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14702 fnp->fcontext = die_containing_type (die, cu);
14703
14704 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14705 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14706
14707 /* Get accessibility. */
14708 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14709 if (attr != nullptr)
14710 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14711 else
14712 accessibility = dwarf2_default_access_attribute (die, cu);
14713 switch (accessibility)
14714 {
14715 case DW_ACCESS_private:
14716 fnp->is_private = 1;
14717 break;
14718 case DW_ACCESS_protected:
14719 fnp->is_protected = 1;
14720 break;
14721 }
14722
14723 /* Check for artificial methods. */
14724 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14725 if (attr && DW_UNSND (attr) != 0)
14726 fnp->is_artificial = 1;
14727
14728 /* Check for defaulted methods. */
14729 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14730 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14731 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14732
14733 /* Check for deleted methods. */
14734 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14735 if (attr != nullptr && DW_UNSND (attr) != 0)
14736 fnp->is_deleted = 1;
14737
14738 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14739
14740 /* Get index in virtual function table if it is a virtual member
14741 function. For older versions of GCC, this is an offset in the
14742 appropriate virtual table, as specified by DW_AT_containing_type.
14743 For everyone else, it is an expression to be evaluated relative
14744 to the object address. */
14745
14746 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14747 if (attr != nullptr)
14748 {
14749 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14750 {
14751 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14752 {
14753 /* Old-style GCC. */
14754 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14755 }
14756 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14757 || (DW_BLOCK (attr)->size > 1
14758 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14759 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14760 {
14761 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14762 if ((fnp->voffset % cu->header.addr_size) != 0)
14763 dwarf2_complex_location_expr_complaint ();
14764 else
14765 fnp->voffset /= cu->header.addr_size;
14766 fnp->voffset += 2;
14767 }
14768 else
14769 dwarf2_complex_location_expr_complaint ();
14770
14771 if (!fnp->fcontext)
14772 {
14773 /* If there is no `this' field and no DW_AT_containing_type,
14774 we cannot actually find a base class context for the
14775 vtable! */
14776 if (TYPE_NFIELDS (this_type) == 0
14777 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14778 {
14779 complaint (_("cannot determine context for virtual member "
14780 "function \"%s\" (offset %s)"),
14781 fieldname, sect_offset_str (die->sect_off));
14782 }
14783 else
14784 {
14785 fnp->fcontext
14786 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14787 }
14788 }
14789 }
14790 else if (attr->form_is_section_offset ())
14791 {
14792 dwarf2_complex_location_expr_complaint ();
14793 }
14794 else
14795 {
14796 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14797 fieldname);
14798 }
14799 }
14800 else
14801 {
14802 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14803 if (attr && DW_UNSND (attr))
14804 {
14805 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14806 complaint (_("Member function \"%s\" (offset %s) is virtual "
14807 "but the vtable offset is not specified"),
14808 fieldname, sect_offset_str (die->sect_off));
14809 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14810 TYPE_CPLUS_DYNAMIC (type) = 1;
14811 }
14812 }
14813 }
14814
14815 /* Create the vector of member function fields, and attach it to the type. */
14816
14817 static void
14818 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14819 struct dwarf2_cu *cu)
14820 {
14821 if (cu->language == language_ada)
14822 error (_("unexpected member functions in Ada type"));
14823
14824 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14825 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14826 TYPE_ALLOC (type,
14827 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14828
14829 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14830 {
14831 struct fnfieldlist &nf = fip->fnfieldlists[i];
14832 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14833
14834 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14835 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14836 fn_flp->fn_fields = (struct fn_field *)
14837 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14838
14839 for (int k = 0; k < nf.fnfields.size (); ++k)
14840 fn_flp->fn_fields[k] = nf.fnfields[k];
14841 }
14842
14843 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14844 }
14845
14846 /* Returns non-zero if NAME is the name of a vtable member in CU's
14847 language, zero otherwise. */
14848 static int
14849 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14850 {
14851 static const char vptr[] = "_vptr";
14852
14853 /* Look for the C++ form of the vtable. */
14854 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14855 return 1;
14856
14857 return 0;
14858 }
14859
14860 /* GCC outputs unnamed structures that are really pointers to member
14861 functions, with the ABI-specified layout. If TYPE describes
14862 such a structure, smash it into a member function type.
14863
14864 GCC shouldn't do this; it should just output pointer to member DIEs.
14865 This is GCC PR debug/28767. */
14866
14867 static void
14868 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14869 {
14870 struct type *pfn_type, *self_type, *new_type;
14871
14872 /* Check for a structure with no name and two children. */
14873 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14874 return;
14875
14876 /* Check for __pfn and __delta members. */
14877 if (TYPE_FIELD_NAME (type, 0) == NULL
14878 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14879 || TYPE_FIELD_NAME (type, 1) == NULL
14880 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14881 return;
14882
14883 /* Find the type of the method. */
14884 pfn_type = TYPE_FIELD_TYPE (type, 0);
14885 if (pfn_type == NULL
14886 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14887 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14888 return;
14889
14890 /* Look for the "this" argument. */
14891 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14892 if (TYPE_NFIELDS (pfn_type) == 0
14893 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14894 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14895 return;
14896
14897 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14898 new_type = alloc_type (objfile);
14899 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14900 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14901 TYPE_VARARGS (pfn_type));
14902 smash_to_methodptr_type (type, new_type);
14903 }
14904
14905 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14906 appropriate error checking and issuing complaints if there is a
14907 problem. */
14908
14909 static ULONGEST
14910 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14911 {
14912 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14913
14914 if (attr == nullptr)
14915 return 0;
14916
14917 if (!attr->form_is_constant ())
14918 {
14919 complaint (_("DW_AT_alignment must have constant form"
14920 " - DIE at %s [in module %s]"),
14921 sect_offset_str (die->sect_off),
14922 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14923 return 0;
14924 }
14925
14926 ULONGEST align;
14927 if (attr->form == DW_FORM_sdata)
14928 {
14929 LONGEST val = DW_SND (attr);
14930 if (val < 0)
14931 {
14932 complaint (_("DW_AT_alignment value must not be negative"
14933 " - DIE at %s [in module %s]"),
14934 sect_offset_str (die->sect_off),
14935 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14936 return 0;
14937 }
14938 align = val;
14939 }
14940 else
14941 align = DW_UNSND (attr);
14942
14943 if (align == 0)
14944 {
14945 complaint (_("DW_AT_alignment value must not be zero"
14946 " - DIE at %s [in module %s]"),
14947 sect_offset_str (die->sect_off),
14948 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14949 return 0;
14950 }
14951 if ((align & (align - 1)) != 0)
14952 {
14953 complaint (_("DW_AT_alignment value must be a power of 2"
14954 " - DIE at %s [in module %s]"),
14955 sect_offset_str (die->sect_off),
14956 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14957 return 0;
14958 }
14959
14960 return align;
14961 }
14962
14963 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14964 the alignment for TYPE. */
14965
14966 static void
14967 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14968 struct type *type)
14969 {
14970 if (!set_type_align (type, get_alignment (cu, die)))
14971 complaint (_("DW_AT_alignment value too large"
14972 " - DIE at %s [in module %s]"),
14973 sect_offset_str (die->sect_off),
14974 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14975 }
14976
14977 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14978 constant for a type, according to DWARF5 spec, Table 5.5. */
14979
14980 static bool
14981 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14982 {
14983 switch (value)
14984 {
14985 case DW_CC_normal:
14986 case DW_CC_pass_by_reference:
14987 case DW_CC_pass_by_value:
14988 return true;
14989
14990 default:
14991 complaint (_("unrecognized DW_AT_calling_convention value "
14992 "(%s) for a type"), pulongest (value));
14993 return false;
14994 }
14995 }
14996
14997 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14998 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14999 also according to GNU-specific values (see include/dwarf2.h). */
15000
15001 static bool
15002 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15003 {
15004 switch (value)
15005 {
15006 case DW_CC_normal:
15007 case DW_CC_program:
15008 case DW_CC_nocall:
15009 return true;
15010
15011 case DW_CC_GNU_renesas_sh:
15012 case DW_CC_GNU_borland_fastcall_i386:
15013 case DW_CC_GDB_IBM_OpenCL:
15014 return true;
15015
15016 default:
15017 complaint (_("unrecognized DW_AT_calling_convention value "
15018 "(%s) for a subroutine"), pulongest (value));
15019 return false;
15020 }
15021 }
15022
15023 /* Called when we find the DIE that starts a structure or union scope
15024 (definition) to create a type for the structure or union. Fill in
15025 the type's name and general properties; the members will not be
15026 processed until process_structure_scope. A symbol table entry for
15027 the type will also not be done until process_structure_scope (assuming
15028 the type has a name).
15029
15030 NOTE: we need to call these functions regardless of whether or not the
15031 DIE has a DW_AT_name attribute, since it might be an anonymous
15032 structure or union. This gets the type entered into our set of
15033 user defined types. */
15034
15035 static struct type *
15036 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15037 {
15038 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15039 struct type *type;
15040 struct attribute *attr;
15041 const char *name;
15042
15043 /* If the definition of this type lives in .debug_types, read that type.
15044 Don't follow DW_AT_specification though, that will take us back up
15045 the chain and we want to go down. */
15046 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15047 if (attr != nullptr)
15048 {
15049 type = get_DW_AT_signature_type (die, attr, cu);
15050
15051 /* The type's CU may not be the same as CU.
15052 Ensure TYPE is recorded with CU in die_type_hash. */
15053 return set_die_type (die, type, cu);
15054 }
15055
15056 type = alloc_type (objfile);
15057 INIT_CPLUS_SPECIFIC (type);
15058
15059 name = dwarf2_name (die, cu);
15060 if (name != NULL)
15061 {
15062 if (cu->language == language_cplus
15063 || cu->language == language_d
15064 || cu->language == language_rust)
15065 {
15066 const char *full_name = dwarf2_full_name (name, die, cu);
15067
15068 /* dwarf2_full_name might have already finished building the DIE's
15069 type. If so, there is no need to continue. */
15070 if (get_die_type (die, cu) != NULL)
15071 return get_die_type (die, cu);
15072
15073 TYPE_NAME (type) = full_name;
15074 }
15075 else
15076 {
15077 /* The name is already allocated along with this objfile, so
15078 we don't need to duplicate it for the type. */
15079 TYPE_NAME (type) = name;
15080 }
15081 }
15082
15083 if (die->tag == DW_TAG_structure_type)
15084 {
15085 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15086 }
15087 else if (die->tag == DW_TAG_union_type)
15088 {
15089 TYPE_CODE (type) = TYPE_CODE_UNION;
15090 }
15091 else if (die->tag == DW_TAG_variant_part)
15092 {
15093 TYPE_CODE (type) = TYPE_CODE_UNION;
15094 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15095 }
15096 else
15097 {
15098 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15099 }
15100
15101 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15102 TYPE_DECLARED_CLASS (type) = 1;
15103
15104 /* Store the calling convention in the type if it's available in
15105 the die. Otherwise the calling convention remains set to
15106 the default value DW_CC_normal. */
15107 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15108 if (attr != nullptr
15109 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15110 {
15111 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15112 TYPE_CPLUS_CALLING_CONVENTION (type)
15113 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15114 }
15115
15116 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15117 if (attr != nullptr)
15118 {
15119 if (attr->form_is_constant ())
15120 TYPE_LENGTH (type) = DW_UNSND (attr);
15121 else
15122 {
15123 /* For the moment, dynamic type sizes are not supported
15124 by GDB's struct type. The actual size is determined
15125 on-demand when resolving the type of a given object,
15126 so set the type's length to zero for now. Otherwise,
15127 we record an expression as the length, and that expression
15128 could lead to a very large value, which could eventually
15129 lead to us trying to allocate that much memory when creating
15130 a value of that type. */
15131 TYPE_LENGTH (type) = 0;
15132 }
15133 }
15134 else
15135 {
15136 TYPE_LENGTH (type) = 0;
15137 }
15138
15139 maybe_set_alignment (cu, die, type);
15140
15141 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15142 {
15143 /* ICC<14 does not output the required DW_AT_declaration on
15144 incomplete types, but gives them a size of zero. */
15145 TYPE_STUB (type) = 1;
15146 }
15147 else
15148 TYPE_STUB_SUPPORTED (type) = 1;
15149
15150 if (die_is_declaration (die, cu))
15151 TYPE_STUB (type) = 1;
15152 else if (attr == NULL && die->child == NULL
15153 && producer_is_realview (cu->producer))
15154 /* RealView does not output the required DW_AT_declaration
15155 on incomplete types. */
15156 TYPE_STUB (type) = 1;
15157
15158 /* We need to add the type field to the die immediately so we don't
15159 infinitely recurse when dealing with pointers to the structure
15160 type within the structure itself. */
15161 set_die_type (die, type, cu);
15162
15163 /* set_die_type should be already done. */
15164 set_descriptive_type (type, die, cu);
15165
15166 return type;
15167 }
15168
15169 /* A helper for process_structure_scope that handles a single member
15170 DIE. */
15171
15172 static void
15173 handle_struct_member_die (struct die_info *child_die, struct type *type,
15174 struct field_info *fi,
15175 std::vector<struct symbol *> *template_args,
15176 struct dwarf2_cu *cu)
15177 {
15178 if (child_die->tag == DW_TAG_member
15179 || child_die->tag == DW_TAG_variable
15180 || child_die->tag == DW_TAG_variant_part)
15181 {
15182 /* NOTE: carlton/2002-11-05: A C++ static data member
15183 should be a DW_TAG_member that is a declaration, but
15184 all versions of G++ as of this writing (so through at
15185 least 3.2.1) incorrectly generate DW_TAG_variable
15186 tags for them instead. */
15187 dwarf2_add_field (fi, child_die, cu);
15188 }
15189 else if (child_die->tag == DW_TAG_subprogram)
15190 {
15191 /* Rust doesn't have member functions in the C++ sense.
15192 However, it does emit ordinary functions as children
15193 of a struct DIE. */
15194 if (cu->language == language_rust)
15195 read_func_scope (child_die, cu);
15196 else
15197 {
15198 /* C++ member function. */
15199 dwarf2_add_member_fn (fi, child_die, type, cu);
15200 }
15201 }
15202 else if (child_die->tag == DW_TAG_inheritance)
15203 {
15204 /* C++ base class field. */
15205 dwarf2_add_field (fi, child_die, cu);
15206 }
15207 else if (type_can_define_types (child_die))
15208 dwarf2_add_type_defn (fi, child_die, cu);
15209 else if (child_die->tag == DW_TAG_template_type_param
15210 || child_die->tag == DW_TAG_template_value_param)
15211 {
15212 struct symbol *arg = new_symbol (child_die, NULL, cu);
15213
15214 if (arg != NULL)
15215 template_args->push_back (arg);
15216 }
15217 else if (child_die->tag == DW_TAG_variant)
15218 {
15219 /* In a variant we want to get the discriminant and also add a
15220 field for our sole member child. */
15221 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15222
15223 for (die_info *variant_child = child_die->child;
15224 variant_child != NULL;
15225 variant_child = sibling_die (variant_child))
15226 {
15227 if (variant_child->tag == DW_TAG_member)
15228 {
15229 handle_struct_member_die (variant_child, type, fi,
15230 template_args, cu);
15231 /* Only handle the one. */
15232 break;
15233 }
15234 }
15235
15236 /* We don't handle this but we might as well report it if we see
15237 it. */
15238 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15239 complaint (_("DW_AT_discr_list is not supported yet"
15240 " - DIE at %s [in module %s]"),
15241 sect_offset_str (child_die->sect_off),
15242 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15243
15244 /* The first field was just added, so we can stash the
15245 discriminant there. */
15246 gdb_assert (!fi->fields.empty ());
15247 if (discr == NULL)
15248 fi->fields.back ().variant.default_branch = true;
15249 else
15250 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15251 }
15252 }
15253
15254 /* Finish creating a structure or union type, including filling in
15255 its members and creating a symbol for it. */
15256
15257 static void
15258 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15259 {
15260 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15261 struct die_info *child_die;
15262 struct type *type;
15263
15264 type = get_die_type (die, cu);
15265 if (type == NULL)
15266 type = read_structure_type (die, cu);
15267
15268 /* When reading a DW_TAG_variant_part, we need to notice when we
15269 read the discriminant member, so we can record it later in the
15270 discriminant_info. */
15271 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15272 sect_offset discr_offset {};
15273 bool has_template_parameters = false;
15274
15275 if (is_variant_part)
15276 {
15277 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15278 if (discr == NULL)
15279 {
15280 /* Maybe it's a univariant form, an extension we support.
15281 In this case arrange not to check the offset. */
15282 is_variant_part = false;
15283 }
15284 else if (discr->form_is_ref ())
15285 {
15286 struct dwarf2_cu *target_cu = cu;
15287 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15288
15289 discr_offset = target_die->sect_off;
15290 }
15291 else
15292 {
15293 complaint (_("DW_AT_discr does not have DIE reference form"
15294 " - DIE at %s [in module %s]"),
15295 sect_offset_str (die->sect_off),
15296 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15297 is_variant_part = false;
15298 }
15299 }
15300
15301 if (die->child != NULL && ! die_is_declaration (die, cu))
15302 {
15303 struct field_info fi;
15304 std::vector<struct symbol *> template_args;
15305
15306 child_die = die->child;
15307
15308 while (child_die && child_die->tag)
15309 {
15310 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15311
15312 if (is_variant_part && discr_offset == child_die->sect_off)
15313 fi.fields.back ().variant.is_discriminant = true;
15314
15315 child_die = sibling_die (child_die);
15316 }
15317
15318 /* Attach template arguments to type. */
15319 if (!template_args.empty ())
15320 {
15321 has_template_parameters = true;
15322 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15323 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15324 TYPE_TEMPLATE_ARGUMENTS (type)
15325 = XOBNEWVEC (&objfile->objfile_obstack,
15326 struct symbol *,
15327 TYPE_N_TEMPLATE_ARGUMENTS (type));
15328 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15329 template_args.data (),
15330 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15331 * sizeof (struct symbol *)));
15332 }
15333
15334 /* Attach fields and member functions to the type. */
15335 if (fi.nfields () > 0)
15336 dwarf2_attach_fields_to_type (&fi, type, cu);
15337 if (!fi.fnfieldlists.empty ())
15338 {
15339 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15340
15341 /* Get the type which refers to the base class (possibly this
15342 class itself) which contains the vtable pointer for the current
15343 class from the DW_AT_containing_type attribute. This use of
15344 DW_AT_containing_type is a GNU extension. */
15345
15346 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15347 {
15348 struct type *t = die_containing_type (die, cu);
15349
15350 set_type_vptr_basetype (type, t);
15351 if (type == t)
15352 {
15353 int i;
15354
15355 /* Our own class provides vtbl ptr. */
15356 for (i = TYPE_NFIELDS (t) - 1;
15357 i >= TYPE_N_BASECLASSES (t);
15358 --i)
15359 {
15360 const char *fieldname = TYPE_FIELD_NAME (t, i);
15361
15362 if (is_vtable_name (fieldname, cu))
15363 {
15364 set_type_vptr_fieldno (type, i);
15365 break;
15366 }
15367 }
15368
15369 /* Complain if virtual function table field not found. */
15370 if (i < TYPE_N_BASECLASSES (t))
15371 complaint (_("virtual function table pointer "
15372 "not found when defining class '%s'"),
15373 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15374 }
15375 else
15376 {
15377 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15378 }
15379 }
15380 else if (cu->producer
15381 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15382 {
15383 /* The IBM XLC compiler does not provide direct indication
15384 of the containing type, but the vtable pointer is
15385 always named __vfp. */
15386
15387 int i;
15388
15389 for (i = TYPE_NFIELDS (type) - 1;
15390 i >= TYPE_N_BASECLASSES (type);
15391 --i)
15392 {
15393 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15394 {
15395 set_type_vptr_fieldno (type, i);
15396 set_type_vptr_basetype (type, type);
15397 break;
15398 }
15399 }
15400 }
15401 }
15402
15403 /* Copy fi.typedef_field_list linked list elements content into the
15404 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15405 if (!fi.typedef_field_list.empty ())
15406 {
15407 int count = fi.typedef_field_list.size ();
15408
15409 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15410 TYPE_TYPEDEF_FIELD_ARRAY (type)
15411 = ((struct decl_field *)
15412 TYPE_ALLOC (type,
15413 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15414 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15415
15416 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15417 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15418 }
15419
15420 /* Copy fi.nested_types_list linked list elements content into the
15421 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15422 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15423 {
15424 int count = fi.nested_types_list.size ();
15425
15426 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15427 TYPE_NESTED_TYPES_ARRAY (type)
15428 = ((struct decl_field *)
15429 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15430 TYPE_NESTED_TYPES_COUNT (type) = count;
15431
15432 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15433 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15434 }
15435 }
15436
15437 quirk_gcc_member_function_pointer (type, objfile);
15438 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15439 cu->rust_unions.push_back (type);
15440
15441 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15442 snapshots) has been known to create a die giving a declaration
15443 for a class that has, as a child, a die giving a definition for a
15444 nested class. So we have to process our children even if the
15445 current die is a declaration. Normally, of course, a declaration
15446 won't have any children at all. */
15447
15448 child_die = die->child;
15449
15450 while (child_die != NULL && child_die->tag)
15451 {
15452 if (child_die->tag == DW_TAG_member
15453 || child_die->tag == DW_TAG_variable
15454 || child_die->tag == DW_TAG_inheritance
15455 || child_die->tag == DW_TAG_template_value_param
15456 || child_die->tag == DW_TAG_template_type_param)
15457 {
15458 /* Do nothing. */
15459 }
15460 else
15461 process_die (child_die, cu);
15462
15463 child_die = sibling_die (child_die);
15464 }
15465
15466 /* Do not consider external references. According to the DWARF standard,
15467 these DIEs are identified by the fact that they have no byte_size
15468 attribute, and a declaration attribute. */
15469 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15470 || !die_is_declaration (die, cu))
15471 {
15472 struct symbol *sym = new_symbol (die, type, cu);
15473
15474 if (has_template_parameters)
15475 {
15476 struct symtab *symtab;
15477 if (sym != nullptr)
15478 symtab = symbol_symtab (sym);
15479 else if (cu->line_header != nullptr)
15480 {
15481 /* Any related symtab will do. */
15482 symtab
15483 = cu->line_header->file_names ()[0].symtab;
15484 }
15485 else
15486 {
15487 symtab = nullptr;
15488 complaint (_("could not find suitable "
15489 "symtab for template parameter"
15490 " - DIE at %s [in module %s]"),
15491 sect_offset_str (die->sect_off),
15492 objfile_name (objfile));
15493 }
15494
15495 if (symtab != nullptr)
15496 {
15497 /* Make sure that the symtab is set on the new symbols.
15498 Even though they don't appear in this symtab directly,
15499 other parts of gdb assume that symbols do, and this is
15500 reasonably true. */
15501 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15502 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15503 }
15504 }
15505 }
15506 }
15507
15508 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15509 update TYPE using some information only available in DIE's children. */
15510
15511 static void
15512 update_enumeration_type_from_children (struct die_info *die,
15513 struct type *type,
15514 struct dwarf2_cu *cu)
15515 {
15516 struct die_info *child_die;
15517 int unsigned_enum = 1;
15518 int flag_enum = 1;
15519
15520 auto_obstack obstack;
15521
15522 for (child_die = die->child;
15523 child_die != NULL && child_die->tag;
15524 child_die = sibling_die (child_die))
15525 {
15526 struct attribute *attr;
15527 LONGEST value;
15528 const gdb_byte *bytes;
15529 struct dwarf2_locexpr_baton *baton;
15530 const char *name;
15531
15532 if (child_die->tag != DW_TAG_enumerator)
15533 continue;
15534
15535 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15536 if (attr == NULL)
15537 continue;
15538
15539 name = dwarf2_name (child_die, cu);
15540 if (name == NULL)
15541 name = "<anonymous enumerator>";
15542
15543 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15544 &value, &bytes, &baton);
15545 if (value < 0)
15546 {
15547 unsigned_enum = 0;
15548 flag_enum = 0;
15549 }
15550 else
15551 {
15552 if (count_one_bits_ll (value) >= 2)
15553 flag_enum = 0;
15554 }
15555
15556 /* If we already know that the enum type is neither unsigned, nor
15557 a flag type, no need to look at the rest of the enumerates. */
15558 if (!unsigned_enum && !flag_enum)
15559 break;
15560 }
15561
15562 if (unsigned_enum)
15563 TYPE_UNSIGNED (type) = 1;
15564 if (flag_enum)
15565 TYPE_FLAG_ENUM (type) = 1;
15566 }
15567
15568 /* Given a DW_AT_enumeration_type die, set its type. We do not
15569 complete the type's fields yet, or create any symbols. */
15570
15571 static struct type *
15572 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15573 {
15574 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15575 struct type *type;
15576 struct attribute *attr;
15577 const char *name;
15578
15579 /* If the definition of this type lives in .debug_types, read that type.
15580 Don't follow DW_AT_specification though, that will take us back up
15581 the chain and we want to go down. */
15582 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15583 if (attr != nullptr)
15584 {
15585 type = get_DW_AT_signature_type (die, attr, cu);
15586
15587 /* The type's CU may not be the same as CU.
15588 Ensure TYPE is recorded with CU in die_type_hash. */
15589 return set_die_type (die, type, cu);
15590 }
15591
15592 type = alloc_type (objfile);
15593
15594 TYPE_CODE (type) = TYPE_CODE_ENUM;
15595 name = dwarf2_full_name (NULL, die, cu);
15596 if (name != NULL)
15597 TYPE_NAME (type) = name;
15598
15599 attr = dwarf2_attr (die, DW_AT_type, cu);
15600 if (attr != NULL)
15601 {
15602 struct type *underlying_type = die_type (die, cu);
15603
15604 TYPE_TARGET_TYPE (type) = underlying_type;
15605 }
15606
15607 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15608 if (attr != nullptr)
15609 {
15610 TYPE_LENGTH (type) = DW_UNSND (attr);
15611 }
15612 else
15613 {
15614 TYPE_LENGTH (type) = 0;
15615 }
15616
15617 maybe_set_alignment (cu, die, type);
15618
15619 /* The enumeration DIE can be incomplete. In Ada, any type can be
15620 declared as private in the package spec, and then defined only
15621 inside the package body. Such types are known as Taft Amendment
15622 Types. When another package uses such a type, an incomplete DIE
15623 may be generated by the compiler. */
15624 if (die_is_declaration (die, cu))
15625 TYPE_STUB (type) = 1;
15626
15627 /* Finish the creation of this type by using the enum's children.
15628 We must call this even when the underlying type has been provided
15629 so that we can determine if we're looking at a "flag" enum. */
15630 update_enumeration_type_from_children (die, type, cu);
15631
15632 /* If this type has an underlying type that is not a stub, then we
15633 may use its attributes. We always use the "unsigned" attribute
15634 in this situation, because ordinarily we guess whether the type
15635 is unsigned -- but the guess can be wrong and the underlying type
15636 can tell us the reality. However, we defer to a local size
15637 attribute if one exists, because this lets the compiler override
15638 the underlying type if needed. */
15639 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15640 {
15641 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15642 if (TYPE_LENGTH (type) == 0)
15643 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15644 if (TYPE_RAW_ALIGN (type) == 0
15645 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15646 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15647 }
15648
15649 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15650
15651 return set_die_type (die, type, cu);
15652 }
15653
15654 /* Given a pointer to a die which begins an enumeration, process all
15655 the dies that define the members of the enumeration, and create the
15656 symbol for the enumeration type.
15657
15658 NOTE: We reverse the order of the element list. */
15659
15660 static void
15661 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15662 {
15663 struct type *this_type;
15664
15665 this_type = get_die_type (die, cu);
15666 if (this_type == NULL)
15667 this_type = read_enumeration_type (die, cu);
15668
15669 if (die->child != NULL)
15670 {
15671 struct die_info *child_die;
15672 struct symbol *sym;
15673 std::vector<struct field> fields;
15674 const char *name;
15675
15676 child_die = die->child;
15677 while (child_die && child_die->tag)
15678 {
15679 if (child_die->tag != DW_TAG_enumerator)
15680 {
15681 process_die (child_die, cu);
15682 }
15683 else
15684 {
15685 name = dwarf2_name (child_die, cu);
15686 if (name)
15687 {
15688 sym = new_symbol (child_die, this_type, cu);
15689
15690 fields.emplace_back ();
15691 struct field &field = fields.back ();
15692
15693 FIELD_NAME (field) = sym->linkage_name ();
15694 FIELD_TYPE (field) = NULL;
15695 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15696 FIELD_BITSIZE (field) = 0;
15697 }
15698 }
15699
15700 child_die = sibling_die (child_die);
15701 }
15702
15703 if (!fields.empty ())
15704 {
15705 TYPE_NFIELDS (this_type) = fields.size ();
15706 TYPE_FIELDS (this_type) = (struct field *)
15707 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15708 memcpy (TYPE_FIELDS (this_type), fields.data (),
15709 sizeof (struct field) * fields.size ());
15710 }
15711 }
15712
15713 /* If we are reading an enum from a .debug_types unit, and the enum
15714 is a declaration, and the enum is not the signatured type in the
15715 unit, then we do not want to add a symbol for it. Adding a
15716 symbol would in some cases obscure the true definition of the
15717 enum, giving users an incomplete type when the definition is
15718 actually available. Note that we do not want to do this for all
15719 enums which are just declarations, because C++0x allows forward
15720 enum declarations. */
15721 if (cu->per_cu->is_debug_types
15722 && die_is_declaration (die, cu))
15723 {
15724 struct signatured_type *sig_type;
15725
15726 sig_type = (struct signatured_type *) cu->per_cu;
15727 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15728 if (sig_type->type_offset_in_section != die->sect_off)
15729 return;
15730 }
15731
15732 new_symbol (die, this_type, cu);
15733 }
15734
15735 /* Extract all information from a DW_TAG_array_type DIE and put it in
15736 the DIE's type field. For now, this only handles one dimensional
15737 arrays. */
15738
15739 static struct type *
15740 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15741 {
15742 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15743 struct die_info *child_die;
15744 struct type *type;
15745 struct type *element_type, *range_type, *index_type;
15746 struct attribute *attr;
15747 const char *name;
15748 struct dynamic_prop *byte_stride_prop = NULL;
15749 unsigned int bit_stride = 0;
15750
15751 element_type = die_type (die, cu);
15752
15753 /* The die_type call above may have already set the type for this DIE. */
15754 type = get_die_type (die, cu);
15755 if (type)
15756 return type;
15757
15758 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15759 if (attr != NULL)
15760 {
15761 int stride_ok;
15762 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15763
15764 byte_stride_prop
15765 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15766 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15767 prop_type);
15768 if (!stride_ok)
15769 {
15770 complaint (_("unable to read array DW_AT_byte_stride "
15771 " - DIE at %s [in module %s]"),
15772 sect_offset_str (die->sect_off),
15773 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15774 /* Ignore this attribute. We will likely not be able to print
15775 arrays of this type correctly, but there is little we can do
15776 to help if we cannot read the attribute's value. */
15777 byte_stride_prop = NULL;
15778 }
15779 }
15780
15781 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15782 if (attr != NULL)
15783 bit_stride = DW_UNSND (attr);
15784
15785 /* Irix 6.2 native cc creates array types without children for
15786 arrays with unspecified length. */
15787 if (die->child == NULL)
15788 {
15789 index_type = objfile_type (objfile)->builtin_int;
15790 range_type = create_static_range_type (NULL, index_type, 0, -1);
15791 type = create_array_type_with_stride (NULL, element_type, range_type,
15792 byte_stride_prop, bit_stride);
15793 return set_die_type (die, type, cu);
15794 }
15795
15796 std::vector<struct type *> range_types;
15797 child_die = die->child;
15798 while (child_die && child_die->tag)
15799 {
15800 if (child_die->tag == DW_TAG_subrange_type)
15801 {
15802 struct type *child_type = read_type_die (child_die, cu);
15803
15804 if (child_type != NULL)
15805 {
15806 /* The range type was succesfully read. Save it for the
15807 array type creation. */
15808 range_types.push_back (child_type);
15809 }
15810 }
15811 child_die = sibling_die (child_die);
15812 }
15813
15814 /* Dwarf2 dimensions are output from left to right, create the
15815 necessary array types in backwards order. */
15816
15817 type = element_type;
15818
15819 if (read_array_order (die, cu) == DW_ORD_col_major)
15820 {
15821 int i = 0;
15822
15823 while (i < range_types.size ())
15824 type = create_array_type_with_stride (NULL, type, range_types[i++],
15825 byte_stride_prop, bit_stride);
15826 }
15827 else
15828 {
15829 size_t ndim = range_types.size ();
15830 while (ndim-- > 0)
15831 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15832 byte_stride_prop, bit_stride);
15833 }
15834
15835 /* Understand Dwarf2 support for vector types (like they occur on
15836 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15837 array type. This is not part of the Dwarf2/3 standard yet, but a
15838 custom vendor extension. The main difference between a regular
15839 array and the vector variant is that vectors are passed by value
15840 to functions. */
15841 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15842 if (attr != nullptr)
15843 make_vector_type (type);
15844
15845 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15846 implementation may choose to implement triple vectors using this
15847 attribute. */
15848 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15849 if (attr != nullptr)
15850 {
15851 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15852 TYPE_LENGTH (type) = DW_UNSND (attr);
15853 else
15854 complaint (_("DW_AT_byte_size for array type smaller "
15855 "than the total size of elements"));
15856 }
15857
15858 name = dwarf2_name (die, cu);
15859 if (name)
15860 TYPE_NAME (type) = name;
15861
15862 maybe_set_alignment (cu, die, type);
15863
15864 /* Install the type in the die. */
15865 set_die_type (die, type, cu);
15866
15867 /* set_die_type should be already done. */
15868 set_descriptive_type (type, die, cu);
15869
15870 return type;
15871 }
15872
15873 static enum dwarf_array_dim_ordering
15874 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15875 {
15876 struct attribute *attr;
15877
15878 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15879
15880 if (attr != nullptr)
15881 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15882
15883 /* GNU F77 is a special case, as at 08/2004 array type info is the
15884 opposite order to the dwarf2 specification, but data is still
15885 laid out as per normal fortran.
15886
15887 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15888 version checking. */
15889
15890 if (cu->language == language_fortran
15891 && cu->producer && strstr (cu->producer, "GNU F77"))
15892 {
15893 return DW_ORD_row_major;
15894 }
15895
15896 switch (cu->language_defn->la_array_ordering)
15897 {
15898 case array_column_major:
15899 return DW_ORD_col_major;
15900 case array_row_major:
15901 default:
15902 return DW_ORD_row_major;
15903 };
15904 }
15905
15906 /* Extract all information from a DW_TAG_set_type DIE and put it in
15907 the DIE's type field. */
15908
15909 static struct type *
15910 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15911 {
15912 struct type *domain_type, *set_type;
15913 struct attribute *attr;
15914
15915 domain_type = die_type (die, cu);
15916
15917 /* The die_type call above may have already set the type for this DIE. */
15918 set_type = get_die_type (die, cu);
15919 if (set_type)
15920 return set_type;
15921
15922 set_type = create_set_type (NULL, domain_type);
15923
15924 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15925 if (attr != nullptr)
15926 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15927
15928 maybe_set_alignment (cu, die, set_type);
15929
15930 return set_die_type (die, set_type, cu);
15931 }
15932
15933 /* A helper for read_common_block that creates a locexpr baton.
15934 SYM is the symbol which we are marking as computed.
15935 COMMON_DIE is the DIE for the common block.
15936 COMMON_LOC is the location expression attribute for the common
15937 block itself.
15938 MEMBER_LOC is the location expression attribute for the particular
15939 member of the common block that we are processing.
15940 CU is the CU from which the above come. */
15941
15942 static void
15943 mark_common_block_symbol_computed (struct symbol *sym,
15944 struct die_info *common_die,
15945 struct attribute *common_loc,
15946 struct attribute *member_loc,
15947 struct dwarf2_cu *cu)
15948 {
15949 struct dwarf2_per_objfile *dwarf2_per_objfile
15950 = cu->per_cu->dwarf2_per_objfile;
15951 struct objfile *objfile = dwarf2_per_objfile->objfile;
15952 struct dwarf2_locexpr_baton *baton;
15953 gdb_byte *ptr;
15954 unsigned int cu_off;
15955 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15956 LONGEST offset = 0;
15957
15958 gdb_assert (common_loc && member_loc);
15959 gdb_assert (common_loc->form_is_block ());
15960 gdb_assert (member_loc->form_is_block ()
15961 || member_loc->form_is_constant ());
15962
15963 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15964 baton->per_cu = cu->per_cu;
15965 gdb_assert (baton->per_cu);
15966
15967 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15968
15969 if (member_loc->form_is_constant ())
15970 {
15971 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15972 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15973 }
15974 else
15975 baton->size += DW_BLOCK (member_loc)->size;
15976
15977 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15978 baton->data = ptr;
15979
15980 *ptr++ = DW_OP_call4;
15981 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15982 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15983 ptr += 4;
15984
15985 if (member_loc->form_is_constant ())
15986 {
15987 *ptr++ = DW_OP_addr;
15988 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15989 ptr += cu->header.addr_size;
15990 }
15991 else
15992 {
15993 /* We have to copy the data here, because DW_OP_call4 will only
15994 use a DW_AT_location attribute. */
15995 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15996 ptr += DW_BLOCK (member_loc)->size;
15997 }
15998
15999 *ptr++ = DW_OP_plus;
16000 gdb_assert (ptr - baton->data == baton->size);
16001
16002 SYMBOL_LOCATION_BATON (sym) = baton;
16003 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16004 }
16005
16006 /* Create appropriate locally-scoped variables for all the
16007 DW_TAG_common_block entries. Also create a struct common_block
16008 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16009 is used to separate the common blocks name namespace from regular
16010 variable names. */
16011
16012 static void
16013 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16014 {
16015 struct attribute *attr;
16016
16017 attr = dwarf2_attr (die, DW_AT_location, cu);
16018 if (attr != nullptr)
16019 {
16020 /* Support the .debug_loc offsets. */
16021 if (attr->form_is_block ())
16022 {
16023 /* Ok. */
16024 }
16025 else if (attr->form_is_section_offset ())
16026 {
16027 dwarf2_complex_location_expr_complaint ();
16028 attr = NULL;
16029 }
16030 else
16031 {
16032 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16033 "common block member");
16034 attr = NULL;
16035 }
16036 }
16037
16038 if (die->child != NULL)
16039 {
16040 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16041 struct die_info *child_die;
16042 size_t n_entries = 0, size;
16043 struct common_block *common_block;
16044 struct symbol *sym;
16045
16046 for (child_die = die->child;
16047 child_die && child_die->tag;
16048 child_die = sibling_die (child_die))
16049 ++n_entries;
16050
16051 size = (sizeof (struct common_block)
16052 + (n_entries - 1) * sizeof (struct symbol *));
16053 common_block
16054 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16055 size);
16056 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16057 common_block->n_entries = 0;
16058
16059 for (child_die = die->child;
16060 child_die && child_die->tag;
16061 child_die = sibling_die (child_die))
16062 {
16063 /* Create the symbol in the DW_TAG_common_block block in the current
16064 symbol scope. */
16065 sym = new_symbol (child_die, NULL, cu);
16066 if (sym != NULL)
16067 {
16068 struct attribute *member_loc;
16069
16070 common_block->contents[common_block->n_entries++] = sym;
16071
16072 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16073 cu);
16074 if (member_loc)
16075 {
16076 /* GDB has handled this for a long time, but it is
16077 not specified by DWARF. It seems to have been
16078 emitted by gfortran at least as recently as:
16079 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16080 complaint (_("Variable in common block has "
16081 "DW_AT_data_member_location "
16082 "- DIE at %s [in module %s]"),
16083 sect_offset_str (child_die->sect_off),
16084 objfile_name (objfile));
16085
16086 if (member_loc->form_is_section_offset ())
16087 dwarf2_complex_location_expr_complaint ();
16088 else if (member_loc->form_is_constant ()
16089 || member_loc->form_is_block ())
16090 {
16091 if (attr != nullptr)
16092 mark_common_block_symbol_computed (sym, die, attr,
16093 member_loc, cu);
16094 }
16095 else
16096 dwarf2_complex_location_expr_complaint ();
16097 }
16098 }
16099 }
16100
16101 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16102 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16103 }
16104 }
16105
16106 /* Create a type for a C++ namespace. */
16107
16108 static struct type *
16109 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16110 {
16111 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16112 const char *previous_prefix, *name;
16113 int is_anonymous;
16114 struct type *type;
16115
16116 /* For extensions, reuse the type of the original namespace. */
16117 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16118 {
16119 struct die_info *ext_die;
16120 struct dwarf2_cu *ext_cu = cu;
16121
16122 ext_die = dwarf2_extension (die, &ext_cu);
16123 type = read_type_die (ext_die, ext_cu);
16124
16125 /* EXT_CU may not be the same as CU.
16126 Ensure TYPE is recorded with CU in die_type_hash. */
16127 return set_die_type (die, type, cu);
16128 }
16129
16130 name = namespace_name (die, &is_anonymous, cu);
16131
16132 /* Now build the name of the current namespace. */
16133
16134 previous_prefix = determine_prefix (die, cu);
16135 if (previous_prefix[0] != '\0')
16136 name = typename_concat (&objfile->objfile_obstack,
16137 previous_prefix, name, 0, cu);
16138
16139 /* Create the type. */
16140 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16141
16142 return set_die_type (die, type, cu);
16143 }
16144
16145 /* Read a namespace scope. */
16146
16147 static void
16148 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16149 {
16150 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16151 int is_anonymous;
16152
16153 /* Add a symbol associated to this if we haven't seen the namespace
16154 before. Also, add a using directive if it's an anonymous
16155 namespace. */
16156
16157 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16158 {
16159 struct type *type;
16160
16161 type = read_type_die (die, cu);
16162 new_symbol (die, type, cu);
16163
16164 namespace_name (die, &is_anonymous, cu);
16165 if (is_anonymous)
16166 {
16167 const char *previous_prefix = determine_prefix (die, cu);
16168
16169 std::vector<const char *> excludes;
16170 add_using_directive (using_directives (cu),
16171 previous_prefix, TYPE_NAME (type), NULL,
16172 NULL, excludes, 0, &objfile->objfile_obstack);
16173 }
16174 }
16175
16176 if (die->child != NULL)
16177 {
16178 struct die_info *child_die = die->child;
16179
16180 while (child_die && child_die->tag)
16181 {
16182 process_die (child_die, cu);
16183 child_die = sibling_die (child_die);
16184 }
16185 }
16186 }
16187
16188 /* Read a Fortran module as type. This DIE can be only a declaration used for
16189 imported module. Still we need that type as local Fortran "use ... only"
16190 declaration imports depend on the created type in determine_prefix. */
16191
16192 static struct type *
16193 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16194 {
16195 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16196 const char *module_name;
16197 struct type *type;
16198
16199 module_name = dwarf2_name (die, cu);
16200 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16201
16202 return set_die_type (die, type, cu);
16203 }
16204
16205 /* Read a Fortran module. */
16206
16207 static void
16208 read_module (struct die_info *die, struct dwarf2_cu *cu)
16209 {
16210 struct die_info *child_die = die->child;
16211 struct type *type;
16212
16213 type = read_type_die (die, cu);
16214 new_symbol (die, type, cu);
16215
16216 while (child_die && child_die->tag)
16217 {
16218 process_die (child_die, cu);
16219 child_die = sibling_die (child_die);
16220 }
16221 }
16222
16223 /* Return the name of the namespace represented by DIE. Set
16224 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16225 namespace. */
16226
16227 static const char *
16228 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16229 {
16230 struct die_info *current_die;
16231 const char *name = NULL;
16232
16233 /* Loop through the extensions until we find a name. */
16234
16235 for (current_die = die;
16236 current_die != NULL;
16237 current_die = dwarf2_extension (die, &cu))
16238 {
16239 /* We don't use dwarf2_name here so that we can detect the absence
16240 of a name -> anonymous namespace. */
16241 name = dwarf2_string_attr (die, DW_AT_name, cu);
16242
16243 if (name != NULL)
16244 break;
16245 }
16246
16247 /* Is it an anonymous namespace? */
16248
16249 *is_anonymous = (name == NULL);
16250 if (*is_anonymous)
16251 name = CP_ANONYMOUS_NAMESPACE_STR;
16252
16253 return name;
16254 }
16255
16256 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16257 the user defined type vector. */
16258
16259 static struct type *
16260 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16261 {
16262 struct gdbarch *gdbarch
16263 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16264 struct comp_unit_head *cu_header = &cu->header;
16265 struct type *type;
16266 struct attribute *attr_byte_size;
16267 struct attribute *attr_address_class;
16268 int byte_size, addr_class;
16269 struct type *target_type;
16270
16271 target_type = die_type (die, cu);
16272
16273 /* The die_type call above may have already set the type for this DIE. */
16274 type = get_die_type (die, cu);
16275 if (type)
16276 return type;
16277
16278 type = lookup_pointer_type (target_type);
16279
16280 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16281 if (attr_byte_size)
16282 byte_size = DW_UNSND (attr_byte_size);
16283 else
16284 byte_size = cu_header->addr_size;
16285
16286 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16287 if (attr_address_class)
16288 addr_class = DW_UNSND (attr_address_class);
16289 else
16290 addr_class = DW_ADDR_none;
16291
16292 ULONGEST alignment = get_alignment (cu, die);
16293
16294 /* If the pointer size, alignment, or address class is different
16295 than the default, create a type variant marked as such and set
16296 the length accordingly. */
16297 if (TYPE_LENGTH (type) != byte_size
16298 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16299 && alignment != TYPE_RAW_ALIGN (type))
16300 || addr_class != DW_ADDR_none)
16301 {
16302 if (gdbarch_address_class_type_flags_p (gdbarch))
16303 {
16304 int type_flags;
16305
16306 type_flags = gdbarch_address_class_type_flags
16307 (gdbarch, byte_size, addr_class);
16308 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16309 == 0);
16310 type = make_type_with_address_space (type, type_flags);
16311 }
16312 else if (TYPE_LENGTH (type) != byte_size)
16313 {
16314 complaint (_("invalid pointer size %d"), byte_size);
16315 }
16316 else if (TYPE_RAW_ALIGN (type) != alignment)
16317 {
16318 complaint (_("Invalid DW_AT_alignment"
16319 " - DIE at %s [in module %s]"),
16320 sect_offset_str (die->sect_off),
16321 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16322 }
16323 else
16324 {
16325 /* Should we also complain about unhandled address classes? */
16326 }
16327 }
16328
16329 TYPE_LENGTH (type) = byte_size;
16330 set_type_align (type, alignment);
16331 return set_die_type (die, type, cu);
16332 }
16333
16334 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16335 the user defined type vector. */
16336
16337 static struct type *
16338 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16339 {
16340 struct type *type;
16341 struct type *to_type;
16342 struct type *domain;
16343
16344 to_type = die_type (die, cu);
16345 domain = die_containing_type (die, cu);
16346
16347 /* The calls above may have already set the type for this DIE. */
16348 type = get_die_type (die, cu);
16349 if (type)
16350 return type;
16351
16352 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16353 type = lookup_methodptr_type (to_type);
16354 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16355 {
16356 struct type *new_type
16357 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16358
16359 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16360 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16361 TYPE_VARARGS (to_type));
16362 type = lookup_methodptr_type (new_type);
16363 }
16364 else
16365 type = lookup_memberptr_type (to_type, domain);
16366
16367 return set_die_type (die, type, cu);
16368 }
16369
16370 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16371 the user defined type vector. */
16372
16373 static struct type *
16374 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16375 enum type_code refcode)
16376 {
16377 struct comp_unit_head *cu_header = &cu->header;
16378 struct type *type, *target_type;
16379 struct attribute *attr;
16380
16381 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16382
16383 target_type = die_type (die, cu);
16384
16385 /* The die_type call above may have already set the type for this DIE. */
16386 type = get_die_type (die, cu);
16387 if (type)
16388 return type;
16389
16390 type = lookup_reference_type (target_type, refcode);
16391 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16392 if (attr != nullptr)
16393 {
16394 TYPE_LENGTH (type) = DW_UNSND (attr);
16395 }
16396 else
16397 {
16398 TYPE_LENGTH (type) = cu_header->addr_size;
16399 }
16400 maybe_set_alignment (cu, die, type);
16401 return set_die_type (die, type, cu);
16402 }
16403
16404 /* Add the given cv-qualifiers to the element type of the array. GCC
16405 outputs DWARF type qualifiers that apply to an array, not the
16406 element type. But GDB relies on the array element type to carry
16407 the cv-qualifiers. This mimics section 6.7.3 of the C99
16408 specification. */
16409
16410 static struct type *
16411 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16412 struct type *base_type, int cnst, int voltl)
16413 {
16414 struct type *el_type, *inner_array;
16415
16416 base_type = copy_type (base_type);
16417 inner_array = base_type;
16418
16419 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16420 {
16421 TYPE_TARGET_TYPE (inner_array) =
16422 copy_type (TYPE_TARGET_TYPE (inner_array));
16423 inner_array = TYPE_TARGET_TYPE (inner_array);
16424 }
16425
16426 el_type = TYPE_TARGET_TYPE (inner_array);
16427 cnst |= TYPE_CONST (el_type);
16428 voltl |= TYPE_VOLATILE (el_type);
16429 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16430
16431 return set_die_type (die, base_type, cu);
16432 }
16433
16434 static struct type *
16435 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16436 {
16437 struct type *base_type, *cv_type;
16438
16439 base_type = die_type (die, cu);
16440
16441 /* The die_type call above may have already set the type for this DIE. */
16442 cv_type = get_die_type (die, cu);
16443 if (cv_type)
16444 return cv_type;
16445
16446 /* In case the const qualifier is applied to an array type, the element type
16447 is so qualified, not the array type (section 6.7.3 of C99). */
16448 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16449 return add_array_cv_type (die, cu, base_type, 1, 0);
16450
16451 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16452 return set_die_type (die, cv_type, cu);
16453 }
16454
16455 static struct type *
16456 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16457 {
16458 struct type *base_type, *cv_type;
16459
16460 base_type = die_type (die, cu);
16461
16462 /* The die_type call above may have already set the type for this DIE. */
16463 cv_type = get_die_type (die, cu);
16464 if (cv_type)
16465 return cv_type;
16466
16467 /* In case the volatile qualifier is applied to an array type, the
16468 element type is so qualified, not the array type (section 6.7.3
16469 of C99). */
16470 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16471 return add_array_cv_type (die, cu, base_type, 0, 1);
16472
16473 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16474 return set_die_type (die, cv_type, cu);
16475 }
16476
16477 /* Handle DW_TAG_restrict_type. */
16478
16479 static struct type *
16480 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16481 {
16482 struct type *base_type, *cv_type;
16483
16484 base_type = die_type (die, cu);
16485
16486 /* The die_type call above may have already set the type for this DIE. */
16487 cv_type = get_die_type (die, cu);
16488 if (cv_type)
16489 return cv_type;
16490
16491 cv_type = make_restrict_type (base_type);
16492 return set_die_type (die, cv_type, cu);
16493 }
16494
16495 /* Handle DW_TAG_atomic_type. */
16496
16497 static struct type *
16498 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16499 {
16500 struct type *base_type, *cv_type;
16501
16502 base_type = die_type (die, cu);
16503
16504 /* The die_type call above may have already set the type for this DIE. */
16505 cv_type = get_die_type (die, cu);
16506 if (cv_type)
16507 return cv_type;
16508
16509 cv_type = make_atomic_type (base_type);
16510 return set_die_type (die, cv_type, cu);
16511 }
16512
16513 /* Extract all information from a DW_TAG_string_type DIE and add to
16514 the user defined type vector. It isn't really a user defined type,
16515 but it behaves like one, with other DIE's using an AT_user_def_type
16516 attribute to reference it. */
16517
16518 static struct type *
16519 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16520 {
16521 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16522 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16523 struct type *type, *range_type, *index_type, *char_type;
16524 struct attribute *attr;
16525 struct dynamic_prop prop;
16526 bool length_is_constant = true;
16527 LONGEST length;
16528
16529 /* There are a couple of places where bit sizes might be made use of
16530 when parsing a DW_TAG_string_type, however, no producer that we know
16531 of make use of these. Handling bit sizes that are a multiple of the
16532 byte size is easy enough, but what about other bit sizes? Lets deal
16533 with that problem when we have to. Warn about these attributes being
16534 unsupported, then parse the type and ignore them like we always
16535 have. */
16536 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16537 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16538 {
16539 static bool warning_printed = false;
16540 if (!warning_printed)
16541 {
16542 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16543 "currently supported on DW_TAG_string_type."));
16544 warning_printed = true;
16545 }
16546 }
16547
16548 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16549 if (attr != nullptr && !attr->form_is_constant ())
16550 {
16551 /* The string length describes the location at which the length of
16552 the string can be found. The size of the length field can be
16553 specified with one of the attributes below. */
16554 struct type *prop_type;
16555 struct attribute *len
16556 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16557 if (len == nullptr)
16558 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16559 if (len != nullptr && len->form_is_constant ())
16560 {
16561 /* Pass 0 as the default as we know this attribute is constant
16562 and the default value will not be returned. */
16563 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16564 prop_type = cu->per_cu->int_type (sz, true);
16565 }
16566 else
16567 {
16568 /* If the size is not specified then we assume it is the size of
16569 an address on this target. */
16570 prop_type = cu->per_cu->addr_sized_int_type (true);
16571 }
16572
16573 /* Convert the attribute into a dynamic property. */
16574 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16575 length = 1;
16576 else
16577 length_is_constant = false;
16578 }
16579 else if (attr != nullptr)
16580 {
16581 /* This DW_AT_string_length just contains the length with no
16582 indirection. There's no need to create a dynamic property in this
16583 case. Pass 0 for the default value as we know it will not be
16584 returned in this case. */
16585 length = dwarf2_get_attr_constant_value (attr, 0);
16586 }
16587 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16588 {
16589 /* We don't currently support non-constant byte sizes for strings. */
16590 length = dwarf2_get_attr_constant_value (attr, 1);
16591 }
16592 else
16593 {
16594 /* Use 1 as a fallback length if we have nothing else. */
16595 length = 1;
16596 }
16597
16598 index_type = objfile_type (objfile)->builtin_int;
16599 if (length_is_constant)
16600 range_type = create_static_range_type (NULL, index_type, 1, length);
16601 else
16602 {
16603 struct dynamic_prop low_bound;
16604
16605 low_bound.kind = PROP_CONST;
16606 low_bound.data.const_val = 1;
16607 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16608 }
16609 char_type = language_string_char_type (cu->language_defn, gdbarch);
16610 type = create_string_type (NULL, char_type, range_type);
16611
16612 return set_die_type (die, type, cu);
16613 }
16614
16615 /* Assuming that DIE corresponds to a function, returns nonzero
16616 if the function is prototyped. */
16617
16618 static int
16619 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16620 {
16621 struct attribute *attr;
16622
16623 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16624 if (attr && (DW_UNSND (attr) != 0))
16625 return 1;
16626
16627 /* The DWARF standard implies that the DW_AT_prototyped attribute
16628 is only meaningful for C, but the concept also extends to other
16629 languages that allow unprototyped functions (Eg: Objective C).
16630 For all other languages, assume that functions are always
16631 prototyped. */
16632 if (cu->language != language_c
16633 && cu->language != language_objc
16634 && cu->language != language_opencl)
16635 return 1;
16636
16637 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16638 prototyped and unprototyped functions; default to prototyped,
16639 since that is more common in modern code (and RealView warns
16640 about unprototyped functions). */
16641 if (producer_is_realview (cu->producer))
16642 return 1;
16643
16644 return 0;
16645 }
16646
16647 /* Handle DIES due to C code like:
16648
16649 struct foo
16650 {
16651 int (*funcp)(int a, long l);
16652 int b;
16653 };
16654
16655 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16656
16657 static struct type *
16658 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16659 {
16660 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16661 struct type *type; /* Type that this function returns. */
16662 struct type *ftype; /* Function that returns above type. */
16663 struct attribute *attr;
16664
16665 type = die_type (die, cu);
16666
16667 /* The die_type call above may have already set the type for this DIE. */
16668 ftype = get_die_type (die, cu);
16669 if (ftype)
16670 return ftype;
16671
16672 ftype = lookup_function_type (type);
16673
16674 if (prototyped_function_p (die, cu))
16675 TYPE_PROTOTYPED (ftype) = 1;
16676
16677 /* Store the calling convention in the type if it's available in
16678 the subroutine die. Otherwise set the calling convention to
16679 the default value DW_CC_normal. */
16680 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16681 if (attr != nullptr
16682 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16683 TYPE_CALLING_CONVENTION (ftype)
16684 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16685 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16686 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16687 else
16688 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16689
16690 /* Record whether the function returns normally to its caller or not
16691 if the DWARF producer set that information. */
16692 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16693 if (attr && (DW_UNSND (attr) != 0))
16694 TYPE_NO_RETURN (ftype) = 1;
16695
16696 /* We need to add the subroutine type to the die immediately so
16697 we don't infinitely recurse when dealing with parameters
16698 declared as the same subroutine type. */
16699 set_die_type (die, ftype, cu);
16700
16701 if (die->child != NULL)
16702 {
16703 struct type *void_type = objfile_type (objfile)->builtin_void;
16704 struct die_info *child_die;
16705 int nparams, iparams;
16706
16707 /* Count the number of parameters.
16708 FIXME: GDB currently ignores vararg functions, but knows about
16709 vararg member functions. */
16710 nparams = 0;
16711 child_die = die->child;
16712 while (child_die && child_die->tag)
16713 {
16714 if (child_die->tag == DW_TAG_formal_parameter)
16715 nparams++;
16716 else if (child_die->tag == DW_TAG_unspecified_parameters)
16717 TYPE_VARARGS (ftype) = 1;
16718 child_die = sibling_die (child_die);
16719 }
16720
16721 /* Allocate storage for parameters and fill them in. */
16722 TYPE_NFIELDS (ftype) = nparams;
16723 TYPE_FIELDS (ftype) = (struct field *)
16724 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16725
16726 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16727 even if we error out during the parameters reading below. */
16728 for (iparams = 0; iparams < nparams; iparams++)
16729 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16730
16731 iparams = 0;
16732 child_die = die->child;
16733 while (child_die && child_die->tag)
16734 {
16735 if (child_die->tag == DW_TAG_formal_parameter)
16736 {
16737 struct type *arg_type;
16738
16739 /* DWARF version 2 has no clean way to discern C++
16740 static and non-static member functions. G++ helps
16741 GDB by marking the first parameter for non-static
16742 member functions (which is the this pointer) as
16743 artificial. We pass this information to
16744 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16745
16746 DWARF version 3 added DW_AT_object_pointer, which GCC
16747 4.5 does not yet generate. */
16748 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16749 if (attr != nullptr)
16750 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16751 else
16752 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16753 arg_type = die_type (child_die, cu);
16754
16755 /* RealView does not mark THIS as const, which the testsuite
16756 expects. GCC marks THIS as const in method definitions,
16757 but not in the class specifications (GCC PR 43053). */
16758 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16759 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16760 {
16761 int is_this = 0;
16762 struct dwarf2_cu *arg_cu = cu;
16763 const char *name = dwarf2_name (child_die, cu);
16764
16765 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16766 if (attr != nullptr)
16767 {
16768 /* If the compiler emits this, use it. */
16769 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16770 is_this = 1;
16771 }
16772 else if (name && strcmp (name, "this") == 0)
16773 /* Function definitions will have the argument names. */
16774 is_this = 1;
16775 else if (name == NULL && iparams == 0)
16776 /* Declarations may not have the names, so like
16777 elsewhere in GDB, assume an artificial first
16778 argument is "this". */
16779 is_this = 1;
16780
16781 if (is_this)
16782 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16783 arg_type, 0);
16784 }
16785
16786 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16787 iparams++;
16788 }
16789 child_die = sibling_die (child_die);
16790 }
16791 }
16792
16793 return ftype;
16794 }
16795
16796 static struct type *
16797 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16798 {
16799 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16800 const char *name = NULL;
16801 struct type *this_type, *target_type;
16802
16803 name = dwarf2_full_name (NULL, die, cu);
16804 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16805 TYPE_TARGET_STUB (this_type) = 1;
16806 set_die_type (die, this_type, cu);
16807 target_type = die_type (die, cu);
16808 if (target_type != this_type)
16809 TYPE_TARGET_TYPE (this_type) = target_type;
16810 else
16811 {
16812 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16813 spec and cause infinite loops in GDB. */
16814 complaint (_("Self-referential DW_TAG_typedef "
16815 "- DIE at %s [in module %s]"),
16816 sect_offset_str (die->sect_off), objfile_name (objfile));
16817 TYPE_TARGET_TYPE (this_type) = NULL;
16818 }
16819 if (name == NULL)
16820 {
16821 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16822 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16823 Handle these by just returning the target type, rather than
16824 constructing an anonymous typedef type and trying to handle this
16825 elsewhere. */
16826 set_die_type (die, target_type, cu);
16827 return target_type;
16828 }
16829 return this_type;
16830 }
16831
16832 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16833 (which may be different from NAME) to the architecture back-end to allow
16834 it to guess the correct format if necessary. */
16835
16836 static struct type *
16837 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16838 const char *name_hint, enum bfd_endian byte_order)
16839 {
16840 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16841 const struct floatformat **format;
16842 struct type *type;
16843
16844 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16845 if (format)
16846 type = init_float_type (objfile, bits, name, format, byte_order);
16847 else
16848 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16849
16850 return type;
16851 }
16852
16853 /* Allocate an integer type of size BITS and name NAME. */
16854
16855 static struct type *
16856 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16857 int bits, int unsigned_p, const char *name)
16858 {
16859 struct type *type;
16860
16861 /* Versions of Intel's C Compiler generate an integer type called "void"
16862 instead of using DW_TAG_unspecified_type. This has been seen on
16863 at least versions 14, 17, and 18. */
16864 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16865 && strcmp (name, "void") == 0)
16866 type = objfile_type (objfile)->builtin_void;
16867 else
16868 type = init_integer_type (objfile, bits, unsigned_p, name);
16869
16870 return type;
16871 }
16872
16873 /* Initialise and return a floating point type of size BITS suitable for
16874 use as a component of a complex number. The NAME_HINT is passed through
16875 when initialising the floating point type and is the name of the complex
16876 type.
16877
16878 As DWARF doesn't currently provide an explicit name for the components
16879 of a complex number, but it can be helpful to have these components
16880 named, we try to select a suitable name based on the size of the
16881 component. */
16882 static struct type *
16883 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16884 struct objfile *objfile,
16885 int bits, const char *name_hint,
16886 enum bfd_endian byte_order)
16887 {
16888 gdbarch *gdbarch = get_objfile_arch (objfile);
16889 struct type *tt = nullptr;
16890
16891 /* Try to find a suitable floating point builtin type of size BITS.
16892 We're going to use the name of this type as the name for the complex
16893 target type that we are about to create. */
16894 switch (cu->language)
16895 {
16896 case language_fortran:
16897 switch (bits)
16898 {
16899 case 32:
16900 tt = builtin_f_type (gdbarch)->builtin_real;
16901 break;
16902 case 64:
16903 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16904 break;
16905 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16906 case 128:
16907 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16908 break;
16909 }
16910 break;
16911 default:
16912 switch (bits)
16913 {
16914 case 32:
16915 tt = builtin_type (gdbarch)->builtin_float;
16916 break;
16917 case 64:
16918 tt = builtin_type (gdbarch)->builtin_double;
16919 break;
16920 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16921 case 128:
16922 tt = builtin_type (gdbarch)->builtin_long_double;
16923 break;
16924 }
16925 break;
16926 }
16927
16928 /* If the type we found doesn't match the size we were looking for, then
16929 pretend we didn't find a type at all, the complex target type we
16930 create will then be nameless. */
16931 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16932 tt = nullptr;
16933
16934 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16935 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16936 }
16937
16938 /* Find a representation of a given base type and install
16939 it in the TYPE field of the die. */
16940
16941 static struct type *
16942 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16943 {
16944 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16945 struct type *type;
16946 struct attribute *attr;
16947 int encoding = 0, bits = 0;
16948 const char *name;
16949 gdbarch *arch;
16950
16951 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16952 if (attr != nullptr)
16953 encoding = DW_UNSND (attr);
16954 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16955 if (attr != nullptr)
16956 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16957 name = dwarf2_name (die, cu);
16958 if (!name)
16959 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16960
16961 arch = get_objfile_arch (objfile);
16962 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16963
16964 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16965 if (attr)
16966 {
16967 int endianity = DW_UNSND (attr);
16968
16969 switch (endianity)
16970 {
16971 case DW_END_big:
16972 byte_order = BFD_ENDIAN_BIG;
16973 break;
16974 case DW_END_little:
16975 byte_order = BFD_ENDIAN_LITTLE;
16976 break;
16977 default:
16978 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16979 break;
16980 }
16981 }
16982
16983 switch (encoding)
16984 {
16985 case DW_ATE_address:
16986 /* Turn DW_ATE_address into a void * pointer. */
16987 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16988 type = init_pointer_type (objfile, bits, name, type);
16989 break;
16990 case DW_ATE_boolean:
16991 type = init_boolean_type (objfile, bits, 1, name);
16992 break;
16993 case DW_ATE_complex_float:
16994 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16995 byte_order);
16996 type = init_complex_type (objfile, name, type);
16997 break;
16998 case DW_ATE_decimal_float:
16999 type = init_decfloat_type (objfile, bits, name);
17000 break;
17001 case DW_ATE_float:
17002 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17003 break;
17004 case DW_ATE_signed:
17005 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17006 break;
17007 case DW_ATE_unsigned:
17008 if (cu->language == language_fortran
17009 && name
17010 && startswith (name, "character("))
17011 type = init_character_type (objfile, bits, 1, name);
17012 else
17013 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17014 break;
17015 case DW_ATE_signed_char:
17016 if (cu->language == language_ada || cu->language == language_m2
17017 || cu->language == language_pascal
17018 || cu->language == language_fortran)
17019 type = init_character_type (objfile, bits, 0, name);
17020 else
17021 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17022 break;
17023 case DW_ATE_unsigned_char:
17024 if (cu->language == language_ada || cu->language == language_m2
17025 || cu->language == language_pascal
17026 || cu->language == language_fortran
17027 || cu->language == language_rust)
17028 type = init_character_type (objfile, bits, 1, name);
17029 else
17030 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17031 break;
17032 case DW_ATE_UTF:
17033 {
17034 if (bits == 16)
17035 type = builtin_type (arch)->builtin_char16;
17036 else if (bits == 32)
17037 type = builtin_type (arch)->builtin_char32;
17038 else
17039 {
17040 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17041 bits);
17042 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17043 }
17044 return set_die_type (die, type, cu);
17045 }
17046 break;
17047
17048 default:
17049 complaint (_("unsupported DW_AT_encoding: '%s'"),
17050 dwarf_type_encoding_name (encoding));
17051 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17052 break;
17053 }
17054
17055 if (name && strcmp (name, "char") == 0)
17056 TYPE_NOSIGN (type) = 1;
17057
17058 maybe_set_alignment (cu, die, type);
17059
17060 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17061
17062 return set_die_type (die, type, cu);
17063 }
17064
17065 /* Parse dwarf attribute if it's a block, reference or constant and put the
17066 resulting value of the attribute into struct bound_prop.
17067 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17068
17069 static int
17070 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17071 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17072 struct type *default_type)
17073 {
17074 struct dwarf2_property_baton *baton;
17075 struct obstack *obstack
17076 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17077
17078 gdb_assert (default_type != NULL);
17079
17080 if (attr == NULL || prop == NULL)
17081 return 0;
17082
17083 if (attr->form_is_block ())
17084 {
17085 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17086 baton->property_type = default_type;
17087 baton->locexpr.per_cu = cu->per_cu;
17088 baton->locexpr.size = DW_BLOCK (attr)->size;
17089 baton->locexpr.data = DW_BLOCK (attr)->data;
17090 switch (attr->name)
17091 {
17092 case DW_AT_string_length:
17093 baton->locexpr.is_reference = true;
17094 break;
17095 default:
17096 baton->locexpr.is_reference = false;
17097 break;
17098 }
17099 prop->data.baton = baton;
17100 prop->kind = PROP_LOCEXPR;
17101 gdb_assert (prop->data.baton != NULL);
17102 }
17103 else if (attr->form_is_ref ())
17104 {
17105 struct dwarf2_cu *target_cu = cu;
17106 struct die_info *target_die;
17107 struct attribute *target_attr;
17108
17109 target_die = follow_die_ref (die, attr, &target_cu);
17110 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17111 if (target_attr == NULL)
17112 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17113 target_cu);
17114 if (target_attr == NULL)
17115 return 0;
17116
17117 switch (target_attr->name)
17118 {
17119 case DW_AT_location:
17120 if (target_attr->form_is_section_offset ())
17121 {
17122 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17123 baton->property_type = die_type (target_die, target_cu);
17124 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17125 prop->data.baton = baton;
17126 prop->kind = PROP_LOCLIST;
17127 gdb_assert (prop->data.baton != NULL);
17128 }
17129 else if (target_attr->form_is_block ())
17130 {
17131 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17132 baton->property_type = die_type (target_die, target_cu);
17133 baton->locexpr.per_cu = cu->per_cu;
17134 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17135 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17136 baton->locexpr.is_reference = true;
17137 prop->data.baton = baton;
17138 prop->kind = PROP_LOCEXPR;
17139 gdb_assert (prop->data.baton != NULL);
17140 }
17141 else
17142 {
17143 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17144 "dynamic property");
17145 return 0;
17146 }
17147 break;
17148 case DW_AT_data_member_location:
17149 {
17150 LONGEST offset;
17151
17152 if (!handle_data_member_location (target_die, target_cu,
17153 &offset))
17154 return 0;
17155
17156 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17157 baton->property_type = read_type_die (target_die->parent,
17158 target_cu);
17159 baton->offset_info.offset = offset;
17160 baton->offset_info.type = die_type (target_die, target_cu);
17161 prop->data.baton = baton;
17162 prop->kind = PROP_ADDR_OFFSET;
17163 break;
17164 }
17165 }
17166 }
17167 else if (attr->form_is_constant ())
17168 {
17169 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17170 prop->kind = PROP_CONST;
17171 }
17172 else
17173 {
17174 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17175 dwarf2_name (die, cu));
17176 return 0;
17177 }
17178
17179 return 1;
17180 }
17181
17182 /* See read.h. */
17183
17184 struct type *
17185 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17186 {
17187 struct objfile *objfile = dwarf2_per_objfile->objfile;
17188 struct type *int_type;
17189
17190 /* Helper macro to examine the various builtin types. */
17191 #define TRY_TYPE(F) \
17192 int_type = (unsigned_p \
17193 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17194 : objfile_type (objfile)->builtin_ ## F); \
17195 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17196 return int_type
17197
17198 TRY_TYPE (char);
17199 TRY_TYPE (short);
17200 TRY_TYPE (int);
17201 TRY_TYPE (long);
17202 TRY_TYPE (long_long);
17203
17204 #undef TRY_TYPE
17205
17206 gdb_assert_not_reached ("unable to find suitable integer type");
17207 }
17208
17209 /* See read.h. */
17210
17211 struct type *
17212 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17213 {
17214 int addr_size = this->addr_size ();
17215 return int_type (addr_size, unsigned_p);
17216 }
17217
17218 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17219 present (which is valid) then compute the default type based on the
17220 compilation units address size. */
17221
17222 static struct type *
17223 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17224 {
17225 struct type *index_type = die_type (die, cu);
17226
17227 /* Dwarf-2 specifications explicitly allows to create subrange types
17228 without specifying a base type.
17229 In that case, the base type must be set to the type of
17230 the lower bound, upper bound or count, in that order, if any of these
17231 three attributes references an object that has a type.
17232 If no base type is found, the Dwarf-2 specifications say that
17233 a signed integer type of size equal to the size of an address should
17234 be used.
17235 For the following C code: `extern char gdb_int [];'
17236 GCC produces an empty range DIE.
17237 FIXME: muller/2010-05-28: Possible references to object for low bound,
17238 high bound or count are not yet handled by this code. */
17239 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17240 index_type = cu->per_cu->addr_sized_int_type (false);
17241
17242 return index_type;
17243 }
17244
17245 /* Read the given DW_AT_subrange DIE. */
17246
17247 static struct type *
17248 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17249 {
17250 struct type *base_type, *orig_base_type;
17251 struct type *range_type;
17252 struct attribute *attr;
17253 struct dynamic_prop low, high;
17254 int low_default_is_valid;
17255 int high_bound_is_count = 0;
17256 const char *name;
17257 ULONGEST negative_mask;
17258
17259 orig_base_type = read_subrange_index_type (die, cu);
17260
17261 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17262 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17263 creating the range type, but we use the result of check_typedef
17264 when examining properties of the type. */
17265 base_type = check_typedef (orig_base_type);
17266
17267 /* The die_type call above may have already set the type for this DIE. */
17268 range_type = get_die_type (die, cu);
17269 if (range_type)
17270 return range_type;
17271
17272 low.kind = PROP_CONST;
17273 high.kind = PROP_CONST;
17274 high.data.const_val = 0;
17275
17276 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17277 omitting DW_AT_lower_bound. */
17278 switch (cu->language)
17279 {
17280 case language_c:
17281 case language_cplus:
17282 low.data.const_val = 0;
17283 low_default_is_valid = 1;
17284 break;
17285 case language_fortran:
17286 low.data.const_val = 1;
17287 low_default_is_valid = 1;
17288 break;
17289 case language_d:
17290 case language_objc:
17291 case language_rust:
17292 low.data.const_val = 0;
17293 low_default_is_valid = (cu->header.version >= 4);
17294 break;
17295 case language_ada:
17296 case language_m2:
17297 case language_pascal:
17298 low.data.const_val = 1;
17299 low_default_is_valid = (cu->header.version >= 4);
17300 break;
17301 default:
17302 low.data.const_val = 0;
17303 low_default_is_valid = 0;
17304 break;
17305 }
17306
17307 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17308 if (attr != nullptr)
17309 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17310 else if (!low_default_is_valid)
17311 complaint (_("Missing DW_AT_lower_bound "
17312 "- DIE at %s [in module %s]"),
17313 sect_offset_str (die->sect_off),
17314 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17315
17316 struct attribute *attr_ub, *attr_count;
17317 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17318 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17319 {
17320 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17321 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17322 {
17323 /* If bounds are constant do the final calculation here. */
17324 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17325 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17326 else
17327 high_bound_is_count = 1;
17328 }
17329 else
17330 {
17331 if (attr_ub != NULL)
17332 complaint (_("Unresolved DW_AT_upper_bound "
17333 "- DIE at %s [in module %s]"),
17334 sect_offset_str (die->sect_off),
17335 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17336 if (attr_count != NULL)
17337 complaint (_("Unresolved DW_AT_count "
17338 "- DIE at %s [in module %s]"),
17339 sect_offset_str (die->sect_off),
17340 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17341 }
17342 }
17343
17344 LONGEST bias = 0;
17345 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17346 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17347 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17348
17349 /* Normally, the DWARF producers are expected to use a signed
17350 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17351 But this is unfortunately not always the case, as witnessed
17352 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17353 is used instead. To work around that ambiguity, we treat
17354 the bounds as signed, and thus sign-extend their values, when
17355 the base type is signed. */
17356 negative_mask =
17357 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17358 if (low.kind == PROP_CONST
17359 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17360 low.data.const_val |= negative_mask;
17361 if (high.kind == PROP_CONST
17362 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17363 high.data.const_val |= negative_mask;
17364
17365 /* Check for bit and byte strides. */
17366 struct dynamic_prop byte_stride_prop;
17367 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17368 if (attr_byte_stride != nullptr)
17369 {
17370 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17371 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17372 prop_type);
17373 }
17374
17375 struct dynamic_prop bit_stride_prop;
17376 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17377 if (attr_bit_stride != nullptr)
17378 {
17379 /* It only makes sense to have either a bit or byte stride. */
17380 if (attr_byte_stride != nullptr)
17381 {
17382 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17383 "- DIE at %s [in module %s]"),
17384 sect_offset_str (die->sect_off),
17385 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17386 attr_bit_stride = nullptr;
17387 }
17388 else
17389 {
17390 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17391 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17392 prop_type);
17393 }
17394 }
17395
17396 if (attr_byte_stride != nullptr
17397 || attr_bit_stride != nullptr)
17398 {
17399 bool byte_stride_p = (attr_byte_stride != nullptr);
17400 struct dynamic_prop *stride
17401 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17402
17403 range_type
17404 = create_range_type_with_stride (NULL, orig_base_type, &low,
17405 &high, bias, stride, byte_stride_p);
17406 }
17407 else
17408 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17409
17410 if (high_bound_is_count)
17411 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17412
17413 /* Ada expects an empty array on no boundary attributes. */
17414 if (attr == NULL && cu->language != language_ada)
17415 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17416
17417 name = dwarf2_name (die, cu);
17418 if (name)
17419 TYPE_NAME (range_type) = name;
17420
17421 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17422 if (attr != nullptr)
17423 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17424
17425 maybe_set_alignment (cu, die, range_type);
17426
17427 set_die_type (die, range_type, cu);
17428
17429 /* set_die_type should be already done. */
17430 set_descriptive_type (range_type, die, cu);
17431
17432 return range_type;
17433 }
17434
17435 static struct type *
17436 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17437 {
17438 struct type *type;
17439
17440 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17441 NULL);
17442 TYPE_NAME (type) = dwarf2_name (die, cu);
17443
17444 /* In Ada, an unspecified type is typically used when the description
17445 of the type is deferred to a different unit. When encountering
17446 such a type, we treat it as a stub, and try to resolve it later on,
17447 when needed. */
17448 if (cu->language == language_ada)
17449 TYPE_STUB (type) = 1;
17450
17451 return set_die_type (die, type, cu);
17452 }
17453
17454 /* Read a single die and all its descendents. Set the die's sibling
17455 field to NULL; set other fields in the die correctly, and set all
17456 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17457 location of the info_ptr after reading all of those dies. PARENT
17458 is the parent of the die in question. */
17459
17460 static struct die_info *
17461 read_die_and_children (const struct die_reader_specs *reader,
17462 const gdb_byte *info_ptr,
17463 const gdb_byte **new_info_ptr,
17464 struct die_info *parent)
17465 {
17466 struct die_info *die;
17467 const gdb_byte *cur_ptr;
17468
17469 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17470 if (die == NULL)
17471 {
17472 *new_info_ptr = cur_ptr;
17473 return NULL;
17474 }
17475 store_in_ref_table (die, reader->cu);
17476
17477 if (die->has_children)
17478 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17479 else
17480 {
17481 die->child = NULL;
17482 *new_info_ptr = cur_ptr;
17483 }
17484
17485 die->sibling = NULL;
17486 die->parent = parent;
17487 return die;
17488 }
17489
17490 /* Read a die, all of its descendents, and all of its siblings; set
17491 all of the fields of all of the dies correctly. Arguments are as
17492 in read_die_and_children. */
17493
17494 static struct die_info *
17495 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17496 const gdb_byte *info_ptr,
17497 const gdb_byte **new_info_ptr,
17498 struct die_info *parent)
17499 {
17500 struct die_info *first_die, *last_sibling;
17501 const gdb_byte *cur_ptr;
17502
17503 cur_ptr = info_ptr;
17504 first_die = last_sibling = NULL;
17505
17506 while (1)
17507 {
17508 struct die_info *die
17509 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17510
17511 if (die == NULL)
17512 {
17513 *new_info_ptr = cur_ptr;
17514 return first_die;
17515 }
17516
17517 if (!first_die)
17518 first_die = die;
17519 else
17520 last_sibling->sibling = die;
17521
17522 last_sibling = die;
17523 }
17524 }
17525
17526 /* Read a die, all of its descendents, and all of its siblings; set
17527 all of the fields of all of the dies correctly. Arguments are as
17528 in read_die_and_children.
17529 This the main entry point for reading a DIE and all its children. */
17530
17531 static struct die_info *
17532 read_die_and_siblings (const struct die_reader_specs *reader,
17533 const gdb_byte *info_ptr,
17534 const gdb_byte **new_info_ptr,
17535 struct die_info *parent)
17536 {
17537 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17538 new_info_ptr, parent);
17539
17540 if (dwarf_die_debug)
17541 {
17542 fprintf_unfiltered (gdb_stdlog,
17543 "Read die from %s@0x%x of %s:\n",
17544 reader->die_section->get_name (),
17545 (unsigned) (info_ptr - reader->die_section->buffer),
17546 bfd_get_filename (reader->abfd));
17547 dump_die (die, dwarf_die_debug);
17548 }
17549
17550 return die;
17551 }
17552
17553 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17554 attributes.
17555 The caller is responsible for filling in the extra attributes
17556 and updating (*DIEP)->num_attrs.
17557 Set DIEP to point to a newly allocated die with its information,
17558 except for its child, sibling, and parent fields. */
17559
17560 static const gdb_byte *
17561 read_full_die_1 (const struct die_reader_specs *reader,
17562 struct die_info **diep, const gdb_byte *info_ptr,
17563 int num_extra_attrs)
17564 {
17565 unsigned int abbrev_number, bytes_read, i;
17566 struct abbrev_info *abbrev;
17567 struct die_info *die;
17568 struct dwarf2_cu *cu = reader->cu;
17569 bfd *abfd = reader->abfd;
17570
17571 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17572 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17573 info_ptr += bytes_read;
17574 if (!abbrev_number)
17575 {
17576 *diep = NULL;
17577 return info_ptr;
17578 }
17579
17580 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17581 if (!abbrev)
17582 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17583 abbrev_number,
17584 bfd_get_filename (abfd));
17585
17586 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17587 die->sect_off = sect_off;
17588 die->tag = abbrev->tag;
17589 die->abbrev = abbrev_number;
17590 die->has_children = abbrev->has_children;
17591
17592 /* Make the result usable.
17593 The caller needs to update num_attrs after adding the extra
17594 attributes. */
17595 die->num_attrs = abbrev->num_attrs;
17596
17597 std::vector<int> indexes_that_need_reprocess;
17598 for (i = 0; i < abbrev->num_attrs; ++i)
17599 {
17600 bool need_reprocess;
17601 info_ptr =
17602 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17603 info_ptr, &need_reprocess);
17604 if (need_reprocess)
17605 indexes_that_need_reprocess.push_back (i);
17606 }
17607
17608 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17609 if (attr != nullptr)
17610 cu->str_offsets_base = DW_UNSND (attr);
17611
17612 auto maybe_addr_base = lookup_addr_base(die);
17613 if (maybe_addr_base.has_value ())
17614 cu->addr_base = *maybe_addr_base;
17615 for (int index : indexes_that_need_reprocess)
17616 read_attribute_reprocess (reader, &die->attrs[index]);
17617 *diep = die;
17618 return info_ptr;
17619 }
17620
17621 /* Read a die and all its attributes.
17622 Set DIEP to point to a newly allocated die with its information,
17623 except for its child, sibling, and parent fields. */
17624
17625 static const gdb_byte *
17626 read_full_die (const struct die_reader_specs *reader,
17627 struct die_info **diep, const gdb_byte *info_ptr)
17628 {
17629 const gdb_byte *result;
17630
17631 result = read_full_die_1 (reader, diep, info_ptr, 0);
17632
17633 if (dwarf_die_debug)
17634 {
17635 fprintf_unfiltered (gdb_stdlog,
17636 "Read die from %s@0x%x of %s:\n",
17637 reader->die_section->get_name (),
17638 (unsigned) (info_ptr - reader->die_section->buffer),
17639 bfd_get_filename (reader->abfd));
17640 dump_die (*diep, dwarf_die_debug);
17641 }
17642
17643 return result;
17644 }
17645 \f
17646
17647 /* Returns nonzero if TAG represents a type that we might generate a partial
17648 symbol for. */
17649
17650 static int
17651 is_type_tag_for_partial (int tag)
17652 {
17653 switch (tag)
17654 {
17655 #if 0
17656 /* Some types that would be reasonable to generate partial symbols for,
17657 that we don't at present. */
17658 case DW_TAG_array_type:
17659 case DW_TAG_file_type:
17660 case DW_TAG_ptr_to_member_type:
17661 case DW_TAG_set_type:
17662 case DW_TAG_string_type:
17663 case DW_TAG_subroutine_type:
17664 #endif
17665 case DW_TAG_base_type:
17666 case DW_TAG_class_type:
17667 case DW_TAG_interface_type:
17668 case DW_TAG_enumeration_type:
17669 case DW_TAG_structure_type:
17670 case DW_TAG_subrange_type:
17671 case DW_TAG_typedef:
17672 case DW_TAG_union_type:
17673 return 1;
17674 default:
17675 return 0;
17676 }
17677 }
17678
17679 /* Load all DIEs that are interesting for partial symbols into memory. */
17680
17681 static struct partial_die_info *
17682 load_partial_dies (const struct die_reader_specs *reader,
17683 const gdb_byte *info_ptr, int building_psymtab)
17684 {
17685 struct dwarf2_cu *cu = reader->cu;
17686 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17687 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17688 unsigned int bytes_read;
17689 unsigned int load_all = 0;
17690 int nesting_level = 1;
17691
17692 parent_die = NULL;
17693 last_die = NULL;
17694
17695 gdb_assert (cu->per_cu != NULL);
17696 if (cu->per_cu->load_all_dies)
17697 load_all = 1;
17698
17699 cu->partial_dies
17700 = htab_create_alloc_ex (cu->header.length / 12,
17701 partial_die_hash,
17702 partial_die_eq,
17703 NULL,
17704 &cu->comp_unit_obstack,
17705 hashtab_obstack_allocate,
17706 dummy_obstack_deallocate);
17707
17708 while (1)
17709 {
17710 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17711
17712 /* A NULL abbrev means the end of a series of children. */
17713 if (abbrev == NULL)
17714 {
17715 if (--nesting_level == 0)
17716 return first_die;
17717
17718 info_ptr += bytes_read;
17719 last_die = parent_die;
17720 parent_die = parent_die->die_parent;
17721 continue;
17722 }
17723
17724 /* Check for template arguments. We never save these; if
17725 they're seen, we just mark the parent, and go on our way. */
17726 if (parent_die != NULL
17727 && cu->language == language_cplus
17728 && (abbrev->tag == DW_TAG_template_type_param
17729 || abbrev->tag == DW_TAG_template_value_param))
17730 {
17731 parent_die->has_template_arguments = 1;
17732
17733 if (!load_all)
17734 {
17735 /* We don't need a partial DIE for the template argument. */
17736 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17737 continue;
17738 }
17739 }
17740
17741 /* We only recurse into c++ subprograms looking for template arguments.
17742 Skip their other children. */
17743 if (!load_all
17744 && cu->language == language_cplus
17745 && parent_die != NULL
17746 && parent_die->tag == DW_TAG_subprogram)
17747 {
17748 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17749 continue;
17750 }
17751
17752 /* Check whether this DIE is interesting enough to save. Normally
17753 we would not be interested in members here, but there may be
17754 later variables referencing them via DW_AT_specification (for
17755 static members). */
17756 if (!load_all
17757 && !is_type_tag_for_partial (abbrev->tag)
17758 && abbrev->tag != DW_TAG_constant
17759 && abbrev->tag != DW_TAG_enumerator
17760 && abbrev->tag != DW_TAG_subprogram
17761 && abbrev->tag != DW_TAG_inlined_subroutine
17762 && abbrev->tag != DW_TAG_lexical_block
17763 && abbrev->tag != DW_TAG_variable
17764 && abbrev->tag != DW_TAG_namespace
17765 && abbrev->tag != DW_TAG_module
17766 && abbrev->tag != DW_TAG_member
17767 && abbrev->tag != DW_TAG_imported_unit
17768 && abbrev->tag != DW_TAG_imported_declaration)
17769 {
17770 /* Otherwise we skip to the next sibling, if any. */
17771 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17772 continue;
17773 }
17774
17775 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17776 abbrev);
17777
17778 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17779
17780 /* This two-pass algorithm for processing partial symbols has a
17781 high cost in cache pressure. Thus, handle some simple cases
17782 here which cover the majority of C partial symbols. DIEs
17783 which neither have specification tags in them, nor could have
17784 specification tags elsewhere pointing at them, can simply be
17785 processed and discarded.
17786
17787 This segment is also optional; scan_partial_symbols and
17788 add_partial_symbol will handle these DIEs if we chain
17789 them in normally. When compilers which do not emit large
17790 quantities of duplicate debug information are more common,
17791 this code can probably be removed. */
17792
17793 /* Any complete simple types at the top level (pretty much all
17794 of them, for a language without namespaces), can be processed
17795 directly. */
17796 if (parent_die == NULL
17797 && pdi.has_specification == 0
17798 && pdi.is_declaration == 0
17799 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17800 || pdi.tag == DW_TAG_base_type
17801 || pdi.tag == DW_TAG_subrange_type))
17802 {
17803 if (building_psymtab && pdi.name != NULL)
17804 add_psymbol_to_list (pdi.name, false,
17805 VAR_DOMAIN, LOC_TYPEDEF, -1,
17806 psymbol_placement::STATIC,
17807 0, cu->language, objfile);
17808 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17809 continue;
17810 }
17811
17812 /* The exception for DW_TAG_typedef with has_children above is
17813 a workaround of GCC PR debug/47510. In the case of this complaint
17814 type_name_or_error will error on such types later.
17815
17816 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17817 it could not find the child DIEs referenced later, this is checked
17818 above. In correct DWARF DW_TAG_typedef should have no children. */
17819
17820 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17821 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17822 "- DIE at %s [in module %s]"),
17823 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17824
17825 /* If we're at the second level, and we're an enumerator, and
17826 our parent has no specification (meaning possibly lives in a
17827 namespace elsewhere), then we can add the partial symbol now
17828 instead of queueing it. */
17829 if (pdi.tag == DW_TAG_enumerator
17830 && parent_die != NULL
17831 && parent_die->die_parent == NULL
17832 && parent_die->tag == DW_TAG_enumeration_type
17833 && parent_die->has_specification == 0)
17834 {
17835 if (pdi.name == NULL)
17836 complaint (_("malformed enumerator DIE ignored"));
17837 else if (building_psymtab)
17838 add_psymbol_to_list (pdi.name, false,
17839 VAR_DOMAIN, LOC_CONST, -1,
17840 cu->language == language_cplus
17841 ? psymbol_placement::GLOBAL
17842 : psymbol_placement::STATIC,
17843 0, cu->language, objfile);
17844
17845 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17846 continue;
17847 }
17848
17849 struct partial_die_info *part_die
17850 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17851
17852 /* We'll save this DIE so link it in. */
17853 part_die->die_parent = parent_die;
17854 part_die->die_sibling = NULL;
17855 part_die->die_child = NULL;
17856
17857 if (last_die && last_die == parent_die)
17858 last_die->die_child = part_die;
17859 else if (last_die)
17860 last_die->die_sibling = part_die;
17861
17862 last_die = part_die;
17863
17864 if (first_die == NULL)
17865 first_die = part_die;
17866
17867 /* Maybe add the DIE to the hash table. Not all DIEs that we
17868 find interesting need to be in the hash table, because we
17869 also have the parent/sibling/child chains; only those that we
17870 might refer to by offset later during partial symbol reading.
17871
17872 For now this means things that might have be the target of a
17873 DW_AT_specification, DW_AT_abstract_origin, or
17874 DW_AT_extension. DW_AT_extension will refer only to
17875 namespaces; DW_AT_abstract_origin refers to functions (and
17876 many things under the function DIE, but we do not recurse
17877 into function DIEs during partial symbol reading) and
17878 possibly variables as well; DW_AT_specification refers to
17879 declarations. Declarations ought to have the DW_AT_declaration
17880 flag. It happens that GCC forgets to put it in sometimes, but
17881 only for functions, not for types.
17882
17883 Adding more things than necessary to the hash table is harmless
17884 except for the performance cost. Adding too few will result in
17885 wasted time in find_partial_die, when we reread the compilation
17886 unit with load_all_dies set. */
17887
17888 if (load_all
17889 || abbrev->tag == DW_TAG_constant
17890 || abbrev->tag == DW_TAG_subprogram
17891 || abbrev->tag == DW_TAG_variable
17892 || abbrev->tag == DW_TAG_namespace
17893 || part_die->is_declaration)
17894 {
17895 void **slot;
17896
17897 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17898 to_underlying (part_die->sect_off),
17899 INSERT);
17900 *slot = part_die;
17901 }
17902
17903 /* For some DIEs we want to follow their children (if any). For C
17904 we have no reason to follow the children of structures; for other
17905 languages we have to, so that we can get at method physnames
17906 to infer fully qualified class names, for DW_AT_specification,
17907 and for C++ template arguments. For C++, we also look one level
17908 inside functions to find template arguments (if the name of the
17909 function does not already contain the template arguments).
17910
17911 For Ada and Fortran, we need to scan the children of subprograms
17912 and lexical blocks as well because these languages allow the
17913 definition of nested entities that could be interesting for the
17914 debugger, such as nested subprograms for instance. */
17915 if (last_die->has_children
17916 && (load_all
17917 || last_die->tag == DW_TAG_namespace
17918 || last_die->tag == DW_TAG_module
17919 || last_die->tag == DW_TAG_enumeration_type
17920 || (cu->language == language_cplus
17921 && last_die->tag == DW_TAG_subprogram
17922 && (last_die->name == NULL
17923 || strchr (last_die->name, '<') == NULL))
17924 || (cu->language != language_c
17925 && (last_die->tag == DW_TAG_class_type
17926 || last_die->tag == DW_TAG_interface_type
17927 || last_die->tag == DW_TAG_structure_type
17928 || last_die->tag == DW_TAG_union_type))
17929 || ((cu->language == language_ada
17930 || cu->language == language_fortran)
17931 && (last_die->tag == DW_TAG_subprogram
17932 || last_die->tag == DW_TAG_lexical_block))))
17933 {
17934 nesting_level++;
17935 parent_die = last_die;
17936 continue;
17937 }
17938
17939 /* Otherwise we skip to the next sibling, if any. */
17940 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17941
17942 /* Back to the top, do it again. */
17943 }
17944 }
17945
17946 partial_die_info::partial_die_info (sect_offset sect_off_,
17947 struct abbrev_info *abbrev)
17948 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17949 {
17950 }
17951
17952 /* Read a minimal amount of information into the minimal die structure.
17953 INFO_PTR should point just after the initial uleb128 of a DIE. */
17954
17955 const gdb_byte *
17956 partial_die_info::read (const struct die_reader_specs *reader,
17957 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17958 {
17959 struct dwarf2_cu *cu = reader->cu;
17960 struct dwarf2_per_objfile *dwarf2_per_objfile
17961 = cu->per_cu->dwarf2_per_objfile;
17962 unsigned int i;
17963 int has_low_pc_attr = 0;
17964 int has_high_pc_attr = 0;
17965 int high_pc_relative = 0;
17966
17967 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17968 for (i = 0; i < abbrev.num_attrs; ++i)
17969 {
17970 bool need_reprocess;
17971 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17972 info_ptr, &need_reprocess);
17973 /* String and address offsets that need to do the reprocessing have
17974 already been read at this point, so there is no need to wait until
17975 the loop terminates to do the reprocessing. */
17976 if (need_reprocess)
17977 read_attribute_reprocess (reader, &attr_vec[i]);
17978 attribute &attr = attr_vec[i];
17979 /* Store the data if it is of an attribute we want to keep in a
17980 partial symbol table. */
17981 switch (attr.name)
17982 {
17983 case DW_AT_name:
17984 switch (tag)
17985 {
17986 case DW_TAG_compile_unit:
17987 case DW_TAG_partial_unit:
17988 case DW_TAG_type_unit:
17989 /* Compilation units have a DW_AT_name that is a filename, not
17990 a source language identifier. */
17991 case DW_TAG_enumeration_type:
17992 case DW_TAG_enumerator:
17993 /* These tags always have simple identifiers already; no need
17994 to canonicalize them. */
17995 name = DW_STRING (&attr);
17996 break;
17997 default:
17998 {
17999 struct objfile *objfile = dwarf2_per_objfile->objfile;
18000
18001 name
18002 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18003 }
18004 break;
18005 }
18006 break;
18007 case DW_AT_linkage_name:
18008 case DW_AT_MIPS_linkage_name:
18009 /* Note that both forms of linkage name might appear. We
18010 assume they will be the same, and we only store the last
18011 one we see. */
18012 linkage_name = DW_STRING (&attr);
18013 break;
18014 case DW_AT_low_pc:
18015 has_low_pc_attr = 1;
18016 lowpc = attr.value_as_address ();
18017 break;
18018 case DW_AT_high_pc:
18019 has_high_pc_attr = 1;
18020 highpc = attr.value_as_address ();
18021 if (cu->header.version >= 4 && attr.form_is_constant ())
18022 high_pc_relative = 1;
18023 break;
18024 case DW_AT_location:
18025 /* Support the .debug_loc offsets. */
18026 if (attr.form_is_block ())
18027 {
18028 d.locdesc = DW_BLOCK (&attr);
18029 }
18030 else if (attr.form_is_section_offset ())
18031 {
18032 dwarf2_complex_location_expr_complaint ();
18033 }
18034 else
18035 {
18036 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18037 "partial symbol information");
18038 }
18039 break;
18040 case DW_AT_external:
18041 is_external = DW_UNSND (&attr);
18042 break;
18043 case DW_AT_declaration:
18044 is_declaration = DW_UNSND (&attr);
18045 break;
18046 case DW_AT_type:
18047 has_type = 1;
18048 break;
18049 case DW_AT_abstract_origin:
18050 case DW_AT_specification:
18051 case DW_AT_extension:
18052 has_specification = 1;
18053 spec_offset = dwarf2_get_ref_die_offset (&attr);
18054 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18055 || cu->per_cu->is_dwz);
18056 break;
18057 case DW_AT_sibling:
18058 /* Ignore absolute siblings, they might point outside of
18059 the current compile unit. */
18060 if (attr.form == DW_FORM_ref_addr)
18061 complaint (_("ignoring absolute DW_AT_sibling"));
18062 else
18063 {
18064 const gdb_byte *buffer = reader->buffer;
18065 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18066 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18067
18068 if (sibling_ptr < info_ptr)
18069 complaint (_("DW_AT_sibling points backwards"));
18070 else if (sibling_ptr > reader->buffer_end)
18071 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18072 else
18073 sibling = sibling_ptr;
18074 }
18075 break;
18076 case DW_AT_byte_size:
18077 has_byte_size = 1;
18078 break;
18079 case DW_AT_const_value:
18080 has_const_value = 1;
18081 break;
18082 case DW_AT_calling_convention:
18083 /* DWARF doesn't provide a way to identify a program's source-level
18084 entry point. DW_AT_calling_convention attributes are only meant
18085 to describe functions' calling conventions.
18086
18087 However, because it's a necessary piece of information in
18088 Fortran, and before DWARF 4 DW_CC_program was the only
18089 piece of debugging information whose definition refers to
18090 a 'main program' at all, several compilers marked Fortran
18091 main programs with DW_CC_program --- even when those
18092 functions use the standard calling conventions.
18093
18094 Although DWARF now specifies a way to provide this
18095 information, we support this practice for backward
18096 compatibility. */
18097 if (DW_UNSND (&attr) == DW_CC_program
18098 && cu->language == language_fortran)
18099 main_subprogram = 1;
18100 break;
18101 case DW_AT_inline:
18102 if (DW_UNSND (&attr) == DW_INL_inlined
18103 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18104 may_be_inlined = 1;
18105 break;
18106
18107 case DW_AT_import:
18108 if (tag == DW_TAG_imported_unit)
18109 {
18110 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18111 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18112 || cu->per_cu->is_dwz);
18113 }
18114 break;
18115
18116 case DW_AT_main_subprogram:
18117 main_subprogram = DW_UNSND (&attr);
18118 break;
18119
18120 case DW_AT_ranges:
18121 {
18122 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18123 but that requires a full DIE, so instead we just
18124 reimplement it. */
18125 int need_ranges_base = tag != DW_TAG_compile_unit;
18126 unsigned int ranges_offset = (DW_UNSND (&attr)
18127 + (need_ranges_base
18128 ? cu->ranges_base
18129 : 0));
18130
18131 /* Value of the DW_AT_ranges attribute is the offset in the
18132 .debug_ranges section. */
18133 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18134 nullptr))
18135 has_pc_info = 1;
18136 }
18137 break;
18138
18139 default:
18140 break;
18141 }
18142 }
18143
18144 /* For Ada, if both the name and the linkage name appear, we prefer
18145 the latter. This lets "catch exception" work better, regardless
18146 of the order in which the name and linkage name were emitted.
18147 Really, though, this is just a workaround for the fact that gdb
18148 doesn't store both the name and the linkage name. */
18149 if (cu->language == language_ada && linkage_name != nullptr)
18150 name = linkage_name;
18151
18152 if (high_pc_relative)
18153 highpc += lowpc;
18154
18155 if (has_low_pc_attr && has_high_pc_attr)
18156 {
18157 /* When using the GNU linker, .gnu.linkonce. sections are used to
18158 eliminate duplicate copies of functions and vtables and such.
18159 The linker will arbitrarily choose one and discard the others.
18160 The AT_*_pc values for such functions refer to local labels in
18161 these sections. If the section from that file was discarded, the
18162 labels are not in the output, so the relocs get a value of 0.
18163 If this is a discarded function, mark the pc bounds as invalid,
18164 so that GDB will ignore it. */
18165 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18166 {
18167 struct objfile *objfile = dwarf2_per_objfile->objfile;
18168 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18169
18170 complaint (_("DW_AT_low_pc %s is zero "
18171 "for DIE at %s [in module %s]"),
18172 paddress (gdbarch, lowpc),
18173 sect_offset_str (sect_off),
18174 objfile_name (objfile));
18175 }
18176 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18177 else if (lowpc >= highpc)
18178 {
18179 struct objfile *objfile = dwarf2_per_objfile->objfile;
18180 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18181
18182 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18183 "for DIE at %s [in module %s]"),
18184 paddress (gdbarch, lowpc),
18185 paddress (gdbarch, highpc),
18186 sect_offset_str (sect_off),
18187 objfile_name (objfile));
18188 }
18189 else
18190 has_pc_info = 1;
18191 }
18192
18193 return info_ptr;
18194 }
18195
18196 /* Find a cached partial DIE at OFFSET in CU. */
18197
18198 struct partial_die_info *
18199 dwarf2_cu::find_partial_die (sect_offset sect_off)
18200 {
18201 struct partial_die_info *lookup_die = NULL;
18202 struct partial_die_info part_die (sect_off);
18203
18204 lookup_die = ((struct partial_die_info *)
18205 htab_find_with_hash (partial_dies, &part_die,
18206 to_underlying (sect_off)));
18207
18208 return lookup_die;
18209 }
18210
18211 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18212 except in the case of .debug_types DIEs which do not reference
18213 outside their CU (they do however referencing other types via
18214 DW_FORM_ref_sig8). */
18215
18216 static const struct cu_partial_die_info
18217 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18218 {
18219 struct dwarf2_per_objfile *dwarf2_per_objfile
18220 = cu->per_cu->dwarf2_per_objfile;
18221 struct objfile *objfile = dwarf2_per_objfile->objfile;
18222 struct dwarf2_per_cu_data *per_cu = NULL;
18223 struct partial_die_info *pd = NULL;
18224
18225 if (offset_in_dwz == cu->per_cu->is_dwz
18226 && cu->header.offset_in_cu_p (sect_off))
18227 {
18228 pd = cu->find_partial_die (sect_off);
18229 if (pd != NULL)
18230 return { cu, pd };
18231 /* We missed recording what we needed.
18232 Load all dies and try again. */
18233 per_cu = cu->per_cu;
18234 }
18235 else
18236 {
18237 /* TUs don't reference other CUs/TUs (except via type signatures). */
18238 if (cu->per_cu->is_debug_types)
18239 {
18240 error (_("Dwarf Error: Type Unit at offset %s contains"
18241 " external reference to offset %s [in module %s].\n"),
18242 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18243 bfd_get_filename (objfile->obfd));
18244 }
18245 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18246 dwarf2_per_objfile);
18247
18248 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18249 load_partial_comp_unit (per_cu);
18250
18251 per_cu->cu->last_used = 0;
18252 pd = per_cu->cu->find_partial_die (sect_off);
18253 }
18254
18255 /* If we didn't find it, and not all dies have been loaded,
18256 load them all and try again. */
18257
18258 if (pd == NULL && per_cu->load_all_dies == 0)
18259 {
18260 per_cu->load_all_dies = 1;
18261
18262 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18263 THIS_CU->cu may already be in use. So we can't just free it and
18264 replace its DIEs with the ones we read in. Instead, we leave those
18265 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18266 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18267 set. */
18268 load_partial_comp_unit (per_cu);
18269
18270 pd = per_cu->cu->find_partial_die (sect_off);
18271 }
18272
18273 if (pd == NULL)
18274 internal_error (__FILE__, __LINE__,
18275 _("could not find partial DIE %s "
18276 "in cache [from module %s]\n"),
18277 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18278 return { per_cu->cu, pd };
18279 }
18280
18281 /* See if we can figure out if the class lives in a namespace. We do
18282 this by looking for a member function; its demangled name will
18283 contain namespace info, if there is any. */
18284
18285 static void
18286 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18287 struct dwarf2_cu *cu)
18288 {
18289 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18290 what template types look like, because the demangler
18291 frequently doesn't give the same name as the debug info. We
18292 could fix this by only using the demangled name to get the
18293 prefix (but see comment in read_structure_type). */
18294
18295 struct partial_die_info *real_pdi;
18296 struct partial_die_info *child_pdi;
18297
18298 /* If this DIE (this DIE's specification, if any) has a parent, then
18299 we should not do this. We'll prepend the parent's fully qualified
18300 name when we create the partial symbol. */
18301
18302 real_pdi = struct_pdi;
18303 while (real_pdi->has_specification)
18304 {
18305 auto res = find_partial_die (real_pdi->spec_offset,
18306 real_pdi->spec_is_dwz, cu);
18307 real_pdi = res.pdi;
18308 cu = res.cu;
18309 }
18310
18311 if (real_pdi->die_parent != NULL)
18312 return;
18313
18314 for (child_pdi = struct_pdi->die_child;
18315 child_pdi != NULL;
18316 child_pdi = child_pdi->die_sibling)
18317 {
18318 if (child_pdi->tag == DW_TAG_subprogram
18319 && child_pdi->linkage_name != NULL)
18320 {
18321 gdb::unique_xmalloc_ptr<char> actual_class_name
18322 (language_class_name_from_physname (cu->language_defn,
18323 child_pdi->linkage_name));
18324 if (actual_class_name != NULL)
18325 {
18326 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18327 struct_pdi->name = objfile->intern (actual_class_name.get ());
18328 }
18329 break;
18330 }
18331 }
18332 }
18333
18334 void
18335 partial_die_info::fixup (struct dwarf2_cu *cu)
18336 {
18337 /* Once we've fixed up a die, there's no point in doing so again.
18338 This also avoids a memory leak if we were to call
18339 guess_partial_die_structure_name multiple times. */
18340 if (fixup_called)
18341 return;
18342
18343 /* If we found a reference attribute and the DIE has no name, try
18344 to find a name in the referred to DIE. */
18345
18346 if (name == NULL && has_specification)
18347 {
18348 struct partial_die_info *spec_die;
18349
18350 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18351 spec_die = res.pdi;
18352 cu = res.cu;
18353
18354 spec_die->fixup (cu);
18355
18356 if (spec_die->name)
18357 {
18358 name = spec_die->name;
18359
18360 /* Copy DW_AT_external attribute if it is set. */
18361 if (spec_die->is_external)
18362 is_external = spec_die->is_external;
18363 }
18364 }
18365
18366 /* Set default names for some unnamed DIEs. */
18367
18368 if (name == NULL && tag == DW_TAG_namespace)
18369 name = CP_ANONYMOUS_NAMESPACE_STR;
18370
18371 /* If there is no parent die to provide a namespace, and there are
18372 children, see if we can determine the namespace from their linkage
18373 name. */
18374 if (cu->language == language_cplus
18375 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18376 && die_parent == NULL
18377 && has_children
18378 && (tag == DW_TAG_class_type
18379 || tag == DW_TAG_structure_type
18380 || tag == DW_TAG_union_type))
18381 guess_partial_die_structure_name (this, cu);
18382
18383 /* GCC might emit a nameless struct or union that has a linkage
18384 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18385 if (name == NULL
18386 && (tag == DW_TAG_class_type
18387 || tag == DW_TAG_interface_type
18388 || tag == DW_TAG_structure_type
18389 || tag == DW_TAG_union_type)
18390 && linkage_name != NULL)
18391 {
18392 gdb::unique_xmalloc_ptr<char> demangled
18393 (gdb_demangle (linkage_name, DMGL_TYPES));
18394 if (demangled != nullptr)
18395 {
18396 const char *base;
18397
18398 /* Strip any leading namespaces/classes, keep only the base name.
18399 DW_AT_name for named DIEs does not contain the prefixes. */
18400 base = strrchr (demangled.get (), ':');
18401 if (base && base > demangled.get () && base[-1] == ':')
18402 base++;
18403 else
18404 base = demangled.get ();
18405
18406 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18407 name = objfile->intern (base);
18408 }
18409 }
18410
18411 fixup_called = 1;
18412 }
18413
18414 /* Process the attributes that had to be skipped in the first round. These
18415 attributes are the ones that need str_offsets_base or addr_base attributes.
18416 They could not have been processed in the first round, because at the time
18417 the values of str_offsets_base or addr_base may not have been known. */
18418 void read_attribute_reprocess (const struct die_reader_specs *reader,
18419 struct attribute *attr)
18420 {
18421 struct dwarf2_cu *cu = reader->cu;
18422 switch (attr->form)
18423 {
18424 case DW_FORM_addrx:
18425 case DW_FORM_GNU_addr_index:
18426 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18427 break;
18428 case DW_FORM_strx:
18429 case DW_FORM_strx1:
18430 case DW_FORM_strx2:
18431 case DW_FORM_strx3:
18432 case DW_FORM_strx4:
18433 case DW_FORM_GNU_str_index:
18434 {
18435 unsigned int str_index = DW_UNSND (attr);
18436 if (reader->dwo_file != NULL)
18437 {
18438 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18439 DW_STRING_IS_CANONICAL (attr) = 0;
18440 }
18441 else
18442 {
18443 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18444 DW_STRING_IS_CANONICAL (attr) = 0;
18445 }
18446 break;
18447 }
18448 default:
18449 gdb_assert_not_reached (_("Unexpected DWARF form."));
18450 }
18451 }
18452
18453 /* Read an attribute value described by an attribute form. */
18454
18455 static const gdb_byte *
18456 read_attribute_value (const struct die_reader_specs *reader,
18457 struct attribute *attr, unsigned form,
18458 LONGEST implicit_const, const gdb_byte *info_ptr,
18459 bool *need_reprocess)
18460 {
18461 struct dwarf2_cu *cu = reader->cu;
18462 struct dwarf2_per_objfile *dwarf2_per_objfile
18463 = cu->per_cu->dwarf2_per_objfile;
18464 struct objfile *objfile = dwarf2_per_objfile->objfile;
18465 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18466 bfd *abfd = reader->abfd;
18467 struct comp_unit_head *cu_header = &cu->header;
18468 unsigned int bytes_read;
18469 struct dwarf_block *blk;
18470 *need_reprocess = false;
18471
18472 attr->form = (enum dwarf_form) form;
18473 switch (form)
18474 {
18475 case DW_FORM_ref_addr:
18476 if (cu->header.version == 2)
18477 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18478 &bytes_read);
18479 else
18480 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18481 &bytes_read);
18482 info_ptr += bytes_read;
18483 break;
18484 case DW_FORM_GNU_ref_alt:
18485 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18486 info_ptr += bytes_read;
18487 break;
18488 case DW_FORM_addr:
18489 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18490 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18491 info_ptr += bytes_read;
18492 break;
18493 case DW_FORM_block2:
18494 blk = dwarf_alloc_block (cu);
18495 blk->size = read_2_bytes (abfd, info_ptr);
18496 info_ptr += 2;
18497 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18498 info_ptr += blk->size;
18499 DW_BLOCK (attr) = blk;
18500 break;
18501 case DW_FORM_block4:
18502 blk = dwarf_alloc_block (cu);
18503 blk->size = read_4_bytes (abfd, info_ptr);
18504 info_ptr += 4;
18505 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18506 info_ptr += blk->size;
18507 DW_BLOCK (attr) = blk;
18508 break;
18509 case DW_FORM_data2:
18510 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18511 info_ptr += 2;
18512 break;
18513 case DW_FORM_data4:
18514 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18515 info_ptr += 4;
18516 break;
18517 case DW_FORM_data8:
18518 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18519 info_ptr += 8;
18520 break;
18521 case DW_FORM_data16:
18522 blk = dwarf_alloc_block (cu);
18523 blk->size = 16;
18524 blk->data = read_n_bytes (abfd, info_ptr, 16);
18525 info_ptr += 16;
18526 DW_BLOCK (attr) = blk;
18527 break;
18528 case DW_FORM_sec_offset:
18529 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18530 info_ptr += bytes_read;
18531 break;
18532 case DW_FORM_string:
18533 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18534 DW_STRING_IS_CANONICAL (attr) = 0;
18535 info_ptr += bytes_read;
18536 break;
18537 case DW_FORM_strp:
18538 if (!cu->per_cu->is_dwz)
18539 {
18540 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18541 abfd, info_ptr, cu_header,
18542 &bytes_read);
18543 DW_STRING_IS_CANONICAL (attr) = 0;
18544 info_ptr += bytes_read;
18545 break;
18546 }
18547 /* FALLTHROUGH */
18548 case DW_FORM_line_strp:
18549 if (!cu->per_cu->is_dwz)
18550 {
18551 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18552 abfd, info_ptr,
18553 cu_header, &bytes_read);
18554 DW_STRING_IS_CANONICAL (attr) = 0;
18555 info_ptr += bytes_read;
18556 break;
18557 }
18558 /* FALLTHROUGH */
18559 case DW_FORM_GNU_strp_alt:
18560 {
18561 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18562 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18563 &bytes_read);
18564
18565 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18566 dwz, str_offset);
18567 DW_STRING_IS_CANONICAL (attr) = 0;
18568 info_ptr += bytes_read;
18569 }
18570 break;
18571 case DW_FORM_exprloc:
18572 case DW_FORM_block:
18573 blk = dwarf_alloc_block (cu);
18574 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18575 info_ptr += bytes_read;
18576 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18577 info_ptr += blk->size;
18578 DW_BLOCK (attr) = blk;
18579 break;
18580 case DW_FORM_block1:
18581 blk = dwarf_alloc_block (cu);
18582 blk->size = read_1_byte (abfd, info_ptr);
18583 info_ptr += 1;
18584 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18585 info_ptr += blk->size;
18586 DW_BLOCK (attr) = blk;
18587 break;
18588 case DW_FORM_data1:
18589 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18590 info_ptr += 1;
18591 break;
18592 case DW_FORM_flag:
18593 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18594 info_ptr += 1;
18595 break;
18596 case DW_FORM_flag_present:
18597 DW_UNSND (attr) = 1;
18598 break;
18599 case DW_FORM_sdata:
18600 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18601 info_ptr += bytes_read;
18602 break;
18603 case DW_FORM_udata:
18604 case DW_FORM_rnglistx:
18605 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18606 info_ptr += bytes_read;
18607 break;
18608 case DW_FORM_ref1:
18609 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18610 + read_1_byte (abfd, info_ptr));
18611 info_ptr += 1;
18612 break;
18613 case DW_FORM_ref2:
18614 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18615 + read_2_bytes (abfd, info_ptr));
18616 info_ptr += 2;
18617 break;
18618 case DW_FORM_ref4:
18619 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18620 + read_4_bytes (abfd, info_ptr));
18621 info_ptr += 4;
18622 break;
18623 case DW_FORM_ref8:
18624 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18625 + read_8_bytes (abfd, info_ptr));
18626 info_ptr += 8;
18627 break;
18628 case DW_FORM_ref_sig8:
18629 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18630 info_ptr += 8;
18631 break;
18632 case DW_FORM_ref_udata:
18633 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18634 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18635 info_ptr += bytes_read;
18636 break;
18637 case DW_FORM_indirect:
18638 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18639 info_ptr += bytes_read;
18640 if (form == DW_FORM_implicit_const)
18641 {
18642 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18643 info_ptr += bytes_read;
18644 }
18645 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18646 info_ptr, need_reprocess);
18647 break;
18648 case DW_FORM_implicit_const:
18649 DW_SND (attr) = implicit_const;
18650 break;
18651 case DW_FORM_addrx:
18652 case DW_FORM_GNU_addr_index:
18653 *need_reprocess = true;
18654 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18655 info_ptr += bytes_read;
18656 break;
18657 case DW_FORM_strx:
18658 case DW_FORM_strx1:
18659 case DW_FORM_strx2:
18660 case DW_FORM_strx3:
18661 case DW_FORM_strx4:
18662 case DW_FORM_GNU_str_index:
18663 {
18664 ULONGEST str_index;
18665 if (form == DW_FORM_strx1)
18666 {
18667 str_index = read_1_byte (abfd, info_ptr);
18668 info_ptr += 1;
18669 }
18670 else if (form == DW_FORM_strx2)
18671 {
18672 str_index = read_2_bytes (abfd, info_ptr);
18673 info_ptr += 2;
18674 }
18675 else if (form == DW_FORM_strx3)
18676 {
18677 str_index = read_3_bytes (abfd, info_ptr);
18678 info_ptr += 3;
18679 }
18680 else if (form == DW_FORM_strx4)
18681 {
18682 str_index = read_4_bytes (abfd, info_ptr);
18683 info_ptr += 4;
18684 }
18685 else
18686 {
18687 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18688 info_ptr += bytes_read;
18689 }
18690 *need_reprocess = true;
18691 DW_UNSND (attr) = str_index;
18692 }
18693 break;
18694 default:
18695 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18696 dwarf_form_name (form),
18697 bfd_get_filename (abfd));
18698 }
18699
18700 /* Super hack. */
18701 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18702 attr->form = DW_FORM_GNU_ref_alt;
18703
18704 /* We have seen instances where the compiler tried to emit a byte
18705 size attribute of -1 which ended up being encoded as an unsigned
18706 0xffffffff. Although 0xffffffff is technically a valid size value,
18707 an object of this size seems pretty unlikely so we can relatively
18708 safely treat these cases as if the size attribute was invalid and
18709 treat them as zero by default. */
18710 if (attr->name == DW_AT_byte_size
18711 && form == DW_FORM_data4
18712 && DW_UNSND (attr) >= 0xffffffff)
18713 {
18714 complaint
18715 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18716 hex_string (DW_UNSND (attr)));
18717 DW_UNSND (attr) = 0;
18718 }
18719
18720 return info_ptr;
18721 }
18722
18723 /* Read an attribute described by an abbreviated attribute. */
18724
18725 static const gdb_byte *
18726 read_attribute (const struct die_reader_specs *reader,
18727 struct attribute *attr, struct attr_abbrev *abbrev,
18728 const gdb_byte *info_ptr, bool *need_reprocess)
18729 {
18730 attr->name = abbrev->name;
18731 return read_attribute_value (reader, attr, abbrev->form,
18732 abbrev->implicit_const, info_ptr,
18733 need_reprocess);
18734 }
18735
18736 /* Cover function for read_initial_length.
18737 Returns the length of the object at BUF, and stores the size of the
18738 initial length in *BYTES_READ and stores the size that offsets will be in
18739 *OFFSET_SIZE.
18740 If the initial length size is not equivalent to that specified in
18741 CU_HEADER then issue a complaint.
18742 This is useful when reading non-comp-unit headers. */
18743
18744 static LONGEST
18745 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
18746 const struct comp_unit_head *cu_header,
18747 unsigned int *bytes_read,
18748 unsigned int *offset_size)
18749 {
18750 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18751
18752 gdb_assert (cu_header->initial_length_size == 4
18753 || cu_header->initial_length_size == 8
18754 || cu_header->initial_length_size == 12);
18755
18756 if (cu_header->initial_length_size != *bytes_read)
18757 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
18758
18759 *offset_size = (*bytes_read == 4) ? 4 : 8;
18760 return length;
18761 }
18762
18763 /* Return pointer to string at section SECT offset STR_OFFSET with error
18764 reporting strings FORM_NAME and SECT_NAME. */
18765
18766 static const char *
18767 read_indirect_string_at_offset_from (struct objfile *objfile,
18768 bfd *abfd, LONGEST str_offset,
18769 struct dwarf2_section_info *sect,
18770 const char *form_name,
18771 const char *sect_name)
18772 {
18773 sect->read (objfile);
18774 if (sect->buffer == NULL)
18775 error (_("%s used without %s section [in module %s]"),
18776 form_name, sect_name, bfd_get_filename (abfd));
18777 if (str_offset >= sect->size)
18778 error (_("%s pointing outside of %s section [in module %s]"),
18779 form_name, sect_name, bfd_get_filename (abfd));
18780 gdb_assert (HOST_CHAR_BIT == 8);
18781 if (sect->buffer[str_offset] == '\0')
18782 return NULL;
18783 return (const char *) (sect->buffer + str_offset);
18784 }
18785
18786 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18787
18788 static const char *
18789 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18790 bfd *abfd, LONGEST str_offset)
18791 {
18792 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18793 abfd, str_offset,
18794 &dwarf2_per_objfile->str,
18795 "DW_FORM_strp", ".debug_str");
18796 }
18797
18798 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18799
18800 static const char *
18801 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18802 bfd *abfd, LONGEST str_offset)
18803 {
18804 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18805 abfd, str_offset,
18806 &dwarf2_per_objfile->line_str,
18807 "DW_FORM_line_strp",
18808 ".debug_line_str");
18809 }
18810
18811 /* Read a string at offset STR_OFFSET in the .debug_str section from
18812 the .dwz file DWZ. Throw an error if the offset is too large. If
18813 the string consists of a single NUL byte, return NULL; otherwise
18814 return a pointer to the string. */
18815
18816 static const char *
18817 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
18818 LONGEST str_offset)
18819 {
18820 dwz->str.read (objfile);
18821
18822 if (dwz->str.buffer == NULL)
18823 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18824 "section [in module %s]"),
18825 bfd_get_filename (dwz->dwz_bfd.get ()));
18826 if (str_offset >= dwz->str.size)
18827 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18828 ".debug_str section [in module %s]"),
18829 bfd_get_filename (dwz->dwz_bfd.get ()));
18830 gdb_assert (HOST_CHAR_BIT == 8);
18831 if (dwz->str.buffer[str_offset] == '\0')
18832 return NULL;
18833 return (const char *) (dwz->str.buffer + str_offset);
18834 }
18835
18836 /* Return pointer to string at .debug_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_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18842 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_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
18849 }
18850
18851 /* Return pointer to string at .debug_line_str offset as read from BUF.
18852 BUF is assumed to be in a compilation unit described by CU_HEADER.
18853 Return *BYTES_READ_PTR count of bytes read from BUF. */
18854
18855 static const char *
18856 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18857 bfd *abfd, const gdb_byte *buf,
18858 const struct comp_unit_head *cu_header,
18859 unsigned int *bytes_read_ptr)
18860 {
18861 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18862
18863 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18864 str_offset);
18865 }
18866
18867 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18868 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18869 ADDR_SIZE is the size of addresses from the CU header. */
18870
18871 static CORE_ADDR
18872 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18873 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18874 int addr_size)
18875 {
18876 struct objfile *objfile = dwarf2_per_objfile->objfile;
18877 bfd *abfd = objfile->obfd;
18878 const gdb_byte *info_ptr;
18879 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18880
18881 dwarf2_per_objfile->addr.read (objfile);
18882 if (dwarf2_per_objfile->addr.buffer == NULL)
18883 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18884 objfile_name (objfile));
18885 if (addr_base_or_zero + addr_index * addr_size
18886 >= dwarf2_per_objfile->addr.size)
18887 error (_("DW_FORM_addr_index pointing outside of "
18888 ".debug_addr section [in module %s]"),
18889 objfile_name (objfile));
18890 info_ptr = (dwarf2_per_objfile->addr.buffer
18891 + addr_base_or_zero + addr_index * addr_size);
18892 if (addr_size == 4)
18893 return bfd_get_32 (abfd, info_ptr);
18894 else
18895 return bfd_get_64 (abfd, info_ptr);
18896 }
18897
18898 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18899
18900 static CORE_ADDR
18901 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18902 {
18903 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18904 cu->addr_base, cu->header.addr_size);
18905 }
18906
18907 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18908
18909 static CORE_ADDR
18910 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18911 unsigned int *bytes_read)
18912 {
18913 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18914 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18915
18916 return read_addr_index (cu, addr_index);
18917 }
18918
18919 /* See read.h. */
18920
18921 CORE_ADDR
18922 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18923 {
18924 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18925 struct dwarf2_cu *cu = per_cu->cu;
18926 gdb::optional<ULONGEST> addr_base;
18927 int addr_size;
18928
18929 /* We need addr_base and addr_size.
18930 If we don't have PER_CU->cu, we have to get it.
18931 Nasty, but the alternative is storing the needed info in PER_CU,
18932 which at this point doesn't seem justified: it's not clear how frequently
18933 it would get used and it would increase the size of every PER_CU.
18934 Entry points like dwarf2_per_cu_addr_size do a similar thing
18935 so we're not in uncharted territory here.
18936 Alas we need to be a bit more complicated as addr_base is contained
18937 in the DIE.
18938
18939 We don't need to read the entire CU(/TU).
18940 We just need the header and top level die.
18941
18942 IWBN to use the aging mechanism to let us lazily later discard the CU.
18943 For now we skip this optimization. */
18944
18945 if (cu != NULL)
18946 {
18947 addr_base = cu->addr_base;
18948 addr_size = cu->header.addr_size;
18949 }
18950 else
18951 {
18952 cutu_reader reader (per_cu, NULL, 0, false);
18953 addr_base = reader.cu->addr_base;
18954 addr_size = reader.cu->header.addr_size;
18955 }
18956
18957 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18958 addr_size);
18959 }
18960
18961 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18962 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18963 DWO file. */
18964
18965 static const char *
18966 read_str_index (struct dwarf2_cu *cu,
18967 struct dwarf2_section_info *str_section,
18968 struct dwarf2_section_info *str_offsets_section,
18969 ULONGEST str_offsets_base, ULONGEST str_index)
18970 {
18971 struct dwarf2_per_objfile *dwarf2_per_objfile
18972 = cu->per_cu->dwarf2_per_objfile;
18973 struct objfile *objfile = dwarf2_per_objfile->objfile;
18974 const char *objf_name = objfile_name (objfile);
18975 bfd *abfd = objfile->obfd;
18976 const gdb_byte *info_ptr;
18977 ULONGEST str_offset;
18978 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18979
18980 str_section->read (objfile);
18981 str_offsets_section->read (objfile);
18982 if (str_section->buffer == NULL)
18983 error (_("%s used without %s section"
18984 " in CU at offset %s [in module %s]"),
18985 form_name, str_section->get_name (),
18986 sect_offset_str (cu->header.sect_off), objf_name);
18987 if (str_offsets_section->buffer == NULL)
18988 error (_("%s used without %s section"
18989 " in CU at offset %s [in module %s]"),
18990 form_name, str_section->get_name (),
18991 sect_offset_str (cu->header.sect_off), objf_name);
18992 info_ptr = (str_offsets_section->buffer
18993 + str_offsets_base
18994 + str_index * cu->header.offset_size);
18995 if (cu->header.offset_size == 4)
18996 str_offset = bfd_get_32 (abfd, info_ptr);
18997 else
18998 str_offset = bfd_get_64 (abfd, info_ptr);
18999 if (str_offset >= str_section->size)
19000 error (_("Offset from %s pointing outside of"
19001 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19002 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19003 return (const char *) (str_section->buffer + str_offset);
19004 }
19005
19006 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19007
19008 static const char *
19009 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19010 {
19011 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19012 ? reader->cu->header.addr_size : 0;
19013 return read_str_index (reader->cu,
19014 &reader->dwo_file->sections.str,
19015 &reader->dwo_file->sections.str_offsets,
19016 str_offsets_base, str_index);
19017 }
19018
19019 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19020
19021 static const char *
19022 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19023 {
19024 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19025 const char *objf_name = objfile_name (objfile);
19026 static const char form_name[] = "DW_FORM_GNU_str_index";
19027 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19028
19029 if (!cu->str_offsets_base.has_value ())
19030 error (_("%s used in Fission stub without %s"
19031 " in CU at offset 0x%lx [in module %s]"),
19032 form_name, str_offsets_attr_name,
19033 (long) cu->header.offset_size, objf_name);
19034
19035 return read_str_index (cu,
19036 &cu->per_cu->dwarf2_per_objfile->str,
19037 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19038 *cu->str_offsets_base, str_index);
19039 }
19040
19041 /* Return the length of an LEB128 number in BUF. */
19042
19043 static int
19044 leb128_size (const gdb_byte *buf)
19045 {
19046 const gdb_byte *begin = buf;
19047 gdb_byte byte;
19048
19049 while (1)
19050 {
19051 byte = *buf++;
19052 if ((byte & 128) == 0)
19053 return buf - begin;
19054 }
19055 }
19056
19057 static void
19058 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19059 {
19060 switch (lang)
19061 {
19062 case DW_LANG_C89:
19063 case DW_LANG_C99:
19064 case DW_LANG_C11:
19065 case DW_LANG_C:
19066 case DW_LANG_UPC:
19067 cu->language = language_c;
19068 break;
19069 case DW_LANG_Java:
19070 case DW_LANG_C_plus_plus:
19071 case DW_LANG_C_plus_plus_11:
19072 case DW_LANG_C_plus_plus_14:
19073 cu->language = language_cplus;
19074 break;
19075 case DW_LANG_D:
19076 cu->language = language_d;
19077 break;
19078 case DW_LANG_Fortran77:
19079 case DW_LANG_Fortran90:
19080 case DW_LANG_Fortran95:
19081 case DW_LANG_Fortran03:
19082 case DW_LANG_Fortran08:
19083 cu->language = language_fortran;
19084 break;
19085 case DW_LANG_Go:
19086 cu->language = language_go;
19087 break;
19088 case DW_LANG_Mips_Assembler:
19089 cu->language = language_asm;
19090 break;
19091 case DW_LANG_Ada83:
19092 case DW_LANG_Ada95:
19093 cu->language = language_ada;
19094 break;
19095 case DW_LANG_Modula2:
19096 cu->language = language_m2;
19097 break;
19098 case DW_LANG_Pascal83:
19099 cu->language = language_pascal;
19100 break;
19101 case DW_LANG_ObjC:
19102 cu->language = language_objc;
19103 break;
19104 case DW_LANG_Rust:
19105 case DW_LANG_Rust_old:
19106 cu->language = language_rust;
19107 break;
19108 case DW_LANG_Cobol74:
19109 case DW_LANG_Cobol85:
19110 default:
19111 cu->language = language_minimal;
19112 break;
19113 }
19114 cu->language_defn = language_def (cu->language);
19115 }
19116
19117 /* Return the named attribute or NULL if not there. */
19118
19119 static struct attribute *
19120 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19121 {
19122 for (;;)
19123 {
19124 unsigned int i;
19125 struct attribute *spec = NULL;
19126
19127 for (i = 0; i < die->num_attrs; ++i)
19128 {
19129 if (die->attrs[i].name == name)
19130 return &die->attrs[i];
19131 if (die->attrs[i].name == DW_AT_specification
19132 || die->attrs[i].name == DW_AT_abstract_origin)
19133 spec = &die->attrs[i];
19134 }
19135
19136 if (!spec)
19137 break;
19138
19139 die = follow_die_ref (die, spec, &cu);
19140 }
19141
19142 return NULL;
19143 }
19144
19145 /* Return the named attribute or NULL if not there,
19146 but do not follow DW_AT_specification, etc.
19147 This is for use in contexts where we're reading .debug_types dies.
19148 Following DW_AT_specification, DW_AT_abstract_origin will take us
19149 back up the chain, and we want to go down. */
19150
19151 static struct attribute *
19152 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19153 {
19154 unsigned int i;
19155
19156 for (i = 0; i < die->num_attrs; ++i)
19157 if (die->attrs[i].name == name)
19158 return &die->attrs[i];
19159
19160 return NULL;
19161 }
19162
19163 /* Return the string associated with a string-typed attribute, or NULL if it
19164 is either not found or is of an incorrect type. */
19165
19166 static const char *
19167 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19168 {
19169 struct attribute *attr;
19170 const char *str = NULL;
19171
19172 attr = dwarf2_attr (die, name, cu);
19173
19174 if (attr != NULL)
19175 {
19176 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19177 || attr->form == DW_FORM_string
19178 || attr->form == DW_FORM_strx
19179 || attr->form == DW_FORM_strx1
19180 || attr->form == DW_FORM_strx2
19181 || attr->form == DW_FORM_strx3
19182 || attr->form == DW_FORM_strx4
19183 || attr->form == DW_FORM_GNU_str_index
19184 || attr->form == DW_FORM_GNU_strp_alt)
19185 str = DW_STRING (attr);
19186 else
19187 complaint (_("string type expected for attribute %s for "
19188 "DIE at %s in module %s"),
19189 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19190 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19191 }
19192
19193 return str;
19194 }
19195
19196 /* Return the dwo name or NULL if not present. If present, it is in either
19197 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19198 static const char *
19199 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19200 {
19201 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19202 if (dwo_name == nullptr)
19203 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19204 return dwo_name;
19205 }
19206
19207 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19208 and holds a non-zero value. This function should only be used for
19209 DW_FORM_flag or DW_FORM_flag_present attributes. */
19210
19211 static int
19212 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19213 {
19214 struct attribute *attr = dwarf2_attr (die, name, cu);
19215
19216 return (attr && DW_UNSND (attr));
19217 }
19218
19219 static int
19220 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19221 {
19222 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19223 which value is non-zero. However, we have to be careful with
19224 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19225 (via dwarf2_flag_true_p) follows this attribute. So we may
19226 end up accidently finding a declaration attribute that belongs
19227 to a different DIE referenced by the specification attribute,
19228 even though the given DIE does not have a declaration attribute. */
19229 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19230 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19231 }
19232
19233 /* Return the die giving the specification for DIE, if there is
19234 one. *SPEC_CU is the CU containing DIE on input, and the CU
19235 containing the return value on output. If there is no
19236 specification, but there is an abstract origin, that is
19237 returned. */
19238
19239 static struct die_info *
19240 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19241 {
19242 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19243 *spec_cu);
19244
19245 if (spec_attr == NULL)
19246 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19247
19248 if (spec_attr == NULL)
19249 return NULL;
19250 else
19251 return follow_die_ref (die, spec_attr, spec_cu);
19252 }
19253
19254 /* Stub for free_line_header to match void * callback types. */
19255
19256 static void
19257 free_line_header_voidp (void *arg)
19258 {
19259 struct line_header *lh = (struct line_header *) arg;
19260
19261 delete lh;
19262 }
19263
19264 /* A convenience function to find the proper .debug_line section for a CU. */
19265
19266 static struct dwarf2_section_info *
19267 get_debug_line_section (struct dwarf2_cu *cu)
19268 {
19269 struct dwarf2_section_info *section;
19270 struct dwarf2_per_objfile *dwarf2_per_objfile
19271 = cu->per_cu->dwarf2_per_objfile;
19272
19273 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19274 DWO file. */
19275 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19276 section = &cu->dwo_unit->dwo_file->sections.line;
19277 else if (cu->per_cu->is_dwz)
19278 {
19279 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19280
19281 section = &dwz->line;
19282 }
19283 else
19284 section = &dwarf2_per_objfile->line;
19285
19286 return section;
19287 }
19288
19289 /* Read directory or file name entry format, starting with byte of
19290 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19291 entries count and the entries themselves in the described entry
19292 format. */
19293
19294 static void
19295 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19296 bfd *abfd, const gdb_byte **bufp,
19297 struct line_header *lh,
19298 const struct comp_unit_head *cu_header,
19299 void (*callback) (struct line_header *lh,
19300 const char *name,
19301 dir_index d_index,
19302 unsigned int mod_time,
19303 unsigned int length))
19304 {
19305 gdb_byte format_count, formati;
19306 ULONGEST data_count, datai;
19307 const gdb_byte *buf = *bufp;
19308 const gdb_byte *format_header_data;
19309 unsigned int bytes_read;
19310
19311 format_count = read_1_byte (abfd, buf);
19312 buf += 1;
19313 format_header_data = buf;
19314 for (formati = 0; formati < format_count; formati++)
19315 {
19316 read_unsigned_leb128 (abfd, buf, &bytes_read);
19317 buf += bytes_read;
19318 read_unsigned_leb128 (abfd, buf, &bytes_read);
19319 buf += bytes_read;
19320 }
19321
19322 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19323 buf += bytes_read;
19324 for (datai = 0; datai < data_count; datai++)
19325 {
19326 const gdb_byte *format = format_header_data;
19327 struct file_entry fe;
19328
19329 for (formati = 0; formati < format_count; formati++)
19330 {
19331 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19332 format += bytes_read;
19333
19334 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19335 format += bytes_read;
19336
19337 gdb::optional<const char *> string;
19338 gdb::optional<unsigned int> uint;
19339
19340 switch (form)
19341 {
19342 case DW_FORM_string:
19343 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19344 buf += bytes_read;
19345 break;
19346
19347 case DW_FORM_line_strp:
19348 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19349 abfd, buf,
19350 cu_header,
19351 &bytes_read));
19352 buf += bytes_read;
19353 break;
19354
19355 case DW_FORM_data1:
19356 uint.emplace (read_1_byte (abfd, buf));
19357 buf += 1;
19358 break;
19359
19360 case DW_FORM_data2:
19361 uint.emplace (read_2_bytes (abfd, buf));
19362 buf += 2;
19363 break;
19364
19365 case DW_FORM_data4:
19366 uint.emplace (read_4_bytes (abfd, buf));
19367 buf += 4;
19368 break;
19369
19370 case DW_FORM_data8:
19371 uint.emplace (read_8_bytes (abfd, buf));
19372 buf += 8;
19373 break;
19374
19375 case DW_FORM_data16:
19376 /* This is used for MD5, but file_entry does not record MD5s. */
19377 buf += 16;
19378 break;
19379
19380 case DW_FORM_udata:
19381 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19382 buf += bytes_read;
19383 break;
19384
19385 case DW_FORM_block:
19386 /* It is valid only for DW_LNCT_timestamp which is ignored by
19387 current GDB. */
19388 break;
19389 }
19390
19391 switch (content_type)
19392 {
19393 case DW_LNCT_path:
19394 if (string.has_value ())
19395 fe.name = *string;
19396 break;
19397 case DW_LNCT_directory_index:
19398 if (uint.has_value ())
19399 fe.d_index = (dir_index) *uint;
19400 break;
19401 case DW_LNCT_timestamp:
19402 if (uint.has_value ())
19403 fe.mod_time = *uint;
19404 break;
19405 case DW_LNCT_size:
19406 if (uint.has_value ())
19407 fe.length = *uint;
19408 break;
19409 case DW_LNCT_MD5:
19410 break;
19411 default:
19412 complaint (_("Unknown format content type %s"),
19413 pulongest (content_type));
19414 }
19415 }
19416
19417 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
19418 }
19419
19420 *bufp = buf;
19421 }
19422
19423 /* Read the statement program header starting at OFFSET in
19424 .debug_line, or .debug_line.dwo. Return a pointer
19425 to a struct line_header, allocated using xmalloc.
19426 Returns NULL if there is a problem reading the header, e.g., if it
19427 has a version we don't understand.
19428
19429 NOTE: the strings in the include directory and file name tables of
19430 the returned object point into the dwarf line section buffer,
19431 and must not be freed. */
19432
19433 static line_header_up
19434 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19435 {
19436 const gdb_byte *line_ptr;
19437 unsigned int bytes_read, offset_size;
19438 int i;
19439 const char *cur_dir, *cur_file;
19440 struct dwarf2_section_info *section;
19441 bfd *abfd;
19442 struct dwarf2_per_objfile *dwarf2_per_objfile
19443 = cu->per_cu->dwarf2_per_objfile;
19444
19445 section = get_debug_line_section (cu);
19446 section->read (dwarf2_per_objfile->objfile);
19447 if (section->buffer == NULL)
19448 {
19449 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19450 complaint (_("missing .debug_line.dwo section"));
19451 else
19452 complaint (_("missing .debug_line section"));
19453 return 0;
19454 }
19455
19456 /* We can't do this until we know the section is non-empty.
19457 Only then do we know we have such a section. */
19458 abfd = section->get_bfd_owner ();
19459
19460 /* Make sure that at least there's room for the total_length field.
19461 That could be 12 bytes long, but we're just going to fudge that. */
19462 if (to_underlying (sect_off) + 4 >= section->size)
19463 {
19464 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19465 return 0;
19466 }
19467
19468 line_header_up lh (new line_header ());
19469
19470 lh->sect_off = sect_off;
19471 lh->offset_in_dwz = cu->per_cu->is_dwz;
19472
19473 line_ptr = section->buffer + to_underlying (sect_off);
19474
19475 /* Read in the header. */
19476 lh->total_length =
19477 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19478 &bytes_read, &offset_size);
19479 line_ptr += bytes_read;
19480
19481 const gdb_byte *start_here = line_ptr;
19482
19483 if (line_ptr + lh->total_length > (section->buffer + section->size))
19484 {
19485 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19486 return 0;
19487 }
19488 lh->statement_program_end = start_here + lh->total_length;
19489 lh->version = read_2_bytes (abfd, line_ptr);
19490 line_ptr += 2;
19491 if (lh->version > 5)
19492 {
19493 /* This is a version we don't understand. The format could have
19494 changed in ways we don't handle properly so just punt. */
19495 complaint (_("unsupported version in .debug_line section"));
19496 return NULL;
19497 }
19498 if (lh->version >= 5)
19499 {
19500 gdb_byte segment_selector_size;
19501
19502 /* Skip address size. */
19503 read_1_byte (abfd, line_ptr);
19504 line_ptr += 1;
19505
19506 segment_selector_size = read_1_byte (abfd, line_ptr);
19507 line_ptr += 1;
19508 if (segment_selector_size != 0)
19509 {
19510 complaint (_("unsupported segment selector size %u "
19511 "in .debug_line section"),
19512 segment_selector_size);
19513 return NULL;
19514 }
19515 }
19516 lh->header_length = read_offset (abfd, line_ptr, offset_size);
19517 line_ptr += offset_size;
19518 lh->statement_program_start = line_ptr + lh->header_length;
19519 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19520 line_ptr += 1;
19521 if (lh->version >= 4)
19522 {
19523 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19524 line_ptr += 1;
19525 }
19526 else
19527 lh->maximum_ops_per_instruction = 1;
19528
19529 if (lh->maximum_ops_per_instruction == 0)
19530 {
19531 lh->maximum_ops_per_instruction = 1;
19532 complaint (_("invalid maximum_ops_per_instruction "
19533 "in `.debug_line' section"));
19534 }
19535
19536 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19537 line_ptr += 1;
19538 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19539 line_ptr += 1;
19540 lh->line_range = read_1_byte (abfd, line_ptr);
19541 line_ptr += 1;
19542 lh->opcode_base = read_1_byte (abfd, line_ptr);
19543 line_ptr += 1;
19544 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
19545
19546 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19547 for (i = 1; i < lh->opcode_base; ++i)
19548 {
19549 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19550 line_ptr += 1;
19551 }
19552
19553 if (lh->version >= 5)
19554 {
19555 /* Read directory table. */
19556 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19557 &cu->header,
19558 [] (struct line_header *header, const char *name,
19559 dir_index d_index, unsigned int mod_time,
19560 unsigned int length)
19561 {
19562 header->add_include_dir (name);
19563 });
19564
19565 /* Read file name table. */
19566 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19567 &cu->header,
19568 [] (struct line_header *header, const char *name,
19569 dir_index d_index, unsigned int mod_time,
19570 unsigned int length)
19571 {
19572 header->add_file_name (name, d_index, mod_time, length);
19573 });
19574 }
19575 else
19576 {
19577 /* Read directory table. */
19578 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19579 {
19580 line_ptr += bytes_read;
19581 lh->add_include_dir (cur_dir);
19582 }
19583 line_ptr += bytes_read;
19584
19585 /* Read file name table. */
19586 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19587 {
19588 unsigned int mod_time, length;
19589 dir_index d_index;
19590
19591 line_ptr += bytes_read;
19592 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19593 line_ptr += bytes_read;
19594 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19595 line_ptr += bytes_read;
19596 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19597 line_ptr += bytes_read;
19598
19599 lh->add_file_name (cur_file, d_index, mod_time, length);
19600 }
19601 line_ptr += bytes_read;
19602 }
19603
19604 if (line_ptr > (section->buffer + section->size))
19605 complaint (_("line number info header doesn't "
19606 "fit in `.debug_line' section"));
19607
19608 return lh;
19609 }
19610
19611 /* Subroutine of dwarf_decode_lines to simplify it.
19612 Return the file name of the psymtab for the given file_entry.
19613 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19614 If space for the result is malloc'd, *NAME_HOLDER will be set.
19615 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19616
19617 static const char *
19618 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19619 const dwarf2_psymtab *pst,
19620 const char *comp_dir,
19621 gdb::unique_xmalloc_ptr<char> *name_holder)
19622 {
19623 const char *include_name = fe.name;
19624 const char *include_name_to_compare = include_name;
19625 const char *pst_filename;
19626 int file_is_pst;
19627
19628 const char *dir_name = fe.include_dir (lh);
19629
19630 gdb::unique_xmalloc_ptr<char> hold_compare;
19631 if (!IS_ABSOLUTE_PATH (include_name)
19632 && (dir_name != NULL || comp_dir != NULL))
19633 {
19634 /* Avoid creating a duplicate psymtab for PST.
19635 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19636 Before we do the comparison, however, we need to account
19637 for DIR_NAME and COMP_DIR.
19638 First prepend dir_name (if non-NULL). If we still don't
19639 have an absolute path prepend comp_dir (if non-NULL).
19640 However, the directory we record in the include-file's
19641 psymtab does not contain COMP_DIR (to match the
19642 corresponding symtab(s)).
19643
19644 Example:
19645
19646 bash$ cd /tmp
19647 bash$ gcc -g ./hello.c
19648 include_name = "hello.c"
19649 dir_name = "."
19650 DW_AT_comp_dir = comp_dir = "/tmp"
19651 DW_AT_name = "./hello.c"
19652
19653 */
19654
19655 if (dir_name != NULL)
19656 {
19657 name_holder->reset (concat (dir_name, SLASH_STRING,
19658 include_name, (char *) NULL));
19659 include_name = name_holder->get ();
19660 include_name_to_compare = include_name;
19661 }
19662 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19663 {
19664 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19665 include_name, (char *) NULL));
19666 include_name_to_compare = hold_compare.get ();
19667 }
19668 }
19669
19670 pst_filename = pst->filename;
19671 gdb::unique_xmalloc_ptr<char> copied_name;
19672 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19673 {
19674 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19675 pst_filename, (char *) NULL));
19676 pst_filename = copied_name.get ();
19677 }
19678
19679 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19680
19681 if (file_is_pst)
19682 return NULL;
19683 return include_name;
19684 }
19685
19686 /* State machine to track the state of the line number program. */
19687
19688 class lnp_state_machine
19689 {
19690 public:
19691 /* Initialize a machine state for the start of a line number
19692 program. */
19693 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19694 bool record_lines_p);
19695
19696 file_entry *current_file ()
19697 {
19698 /* lh->file_names is 0-based, but the file name numbers in the
19699 statement program are 1-based. */
19700 return m_line_header->file_name_at (m_file);
19701 }
19702
19703 /* Record the line in the state machine. END_SEQUENCE is true if
19704 we're processing the end of a sequence. */
19705 void record_line (bool end_sequence);
19706
19707 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19708 nop-out rest of the lines in this sequence. */
19709 void check_line_address (struct dwarf2_cu *cu,
19710 const gdb_byte *line_ptr,
19711 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19712
19713 void handle_set_discriminator (unsigned int discriminator)
19714 {
19715 m_discriminator = discriminator;
19716 m_line_has_non_zero_discriminator |= discriminator != 0;
19717 }
19718
19719 /* Handle DW_LNE_set_address. */
19720 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19721 {
19722 m_op_index = 0;
19723 address += baseaddr;
19724 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19725 }
19726
19727 /* Handle DW_LNS_advance_pc. */
19728 void handle_advance_pc (CORE_ADDR adjust);
19729
19730 /* Handle a special opcode. */
19731 void handle_special_opcode (unsigned char op_code);
19732
19733 /* Handle DW_LNS_advance_line. */
19734 void handle_advance_line (int line_delta)
19735 {
19736 advance_line (line_delta);
19737 }
19738
19739 /* Handle DW_LNS_set_file. */
19740 void handle_set_file (file_name_index file);
19741
19742 /* Handle DW_LNS_negate_stmt. */
19743 void handle_negate_stmt ()
19744 {
19745 m_is_stmt = !m_is_stmt;
19746 }
19747
19748 /* Handle DW_LNS_const_add_pc. */
19749 void handle_const_add_pc ();
19750
19751 /* Handle DW_LNS_fixed_advance_pc. */
19752 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19753 {
19754 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19755 m_op_index = 0;
19756 }
19757
19758 /* Handle DW_LNS_copy. */
19759 void handle_copy ()
19760 {
19761 record_line (false);
19762 m_discriminator = 0;
19763 }
19764
19765 /* Handle DW_LNE_end_sequence. */
19766 void handle_end_sequence ()
19767 {
19768 m_currently_recording_lines = true;
19769 }
19770
19771 private:
19772 /* Advance the line by LINE_DELTA. */
19773 void advance_line (int line_delta)
19774 {
19775 m_line += line_delta;
19776
19777 if (line_delta != 0)
19778 m_line_has_non_zero_discriminator = m_discriminator != 0;
19779 }
19780
19781 struct dwarf2_cu *m_cu;
19782
19783 gdbarch *m_gdbarch;
19784
19785 /* True if we're recording lines.
19786 Otherwise we're building partial symtabs and are just interested in
19787 finding include files mentioned by the line number program. */
19788 bool m_record_lines_p;
19789
19790 /* The line number header. */
19791 line_header *m_line_header;
19792
19793 /* These are part of the standard DWARF line number state machine,
19794 and initialized according to the DWARF spec. */
19795
19796 unsigned char m_op_index = 0;
19797 /* The line table index of the current file. */
19798 file_name_index m_file = 1;
19799 unsigned int m_line = 1;
19800
19801 /* These are initialized in the constructor. */
19802
19803 CORE_ADDR m_address;
19804 bool m_is_stmt;
19805 unsigned int m_discriminator;
19806
19807 /* Additional bits of state we need to track. */
19808
19809 /* The last file that we called dwarf2_start_subfile for.
19810 This is only used for TLLs. */
19811 unsigned int m_last_file = 0;
19812 /* The last file a line number was recorded for. */
19813 struct subfile *m_last_subfile = NULL;
19814
19815 /* When true, record the lines we decode. */
19816 bool m_currently_recording_lines = false;
19817
19818 /* The last line number that was recorded, used to coalesce
19819 consecutive entries for the same line. This can happen, for
19820 example, when discriminators are present. PR 17276. */
19821 unsigned int m_last_line = 0;
19822 bool m_line_has_non_zero_discriminator = false;
19823 };
19824
19825 void
19826 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19827 {
19828 CORE_ADDR addr_adj = (((m_op_index + adjust)
19829 / m_line_header->maximum_ops_per_instruction)
19830 * m_line_header->minimum_instruction_length);
19831 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19832 m_op_index = ((m_op_index + adjust)
19833 % m_line_header->maximum_ops_per_instruction);
19834 }
19835
19836 void
19837 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19838 {
19839 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19840 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19841 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19842 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19843 / m_line_header->maximum_ops_per_instruction)
19844 * m_line_header->minimum_instruction_length);
19845 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19846 m_op_index = ((m_op_index + adj_opcode_d)
19847 % m_line_header->maximum_ops_per_instruction);
19848
19849 int line_delta = m_line_header->line_base + adj_opcode_r;
19850 advance_line (line_delta);
19851 record_line (false);
19852 m_discriminator = 0;
19853 }
19854
19855 void
19856 lnp_state_machine::handle_set_file (file_name_index file)
19857 {
19858 m_file = file;
19859
19860 const file_entry *fe = current_file ();
19861 if (fe == NULL)
19862 dwarf2_debug_line_missing_file_complaint ();
19863 else if (m_record_lines_p)
19864 {
19865 const char *dir = fe->include_dir (m_line_header);
19866
19867 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19868 m_line_has_non_zero_discriminator = m_discriminator != 0;
19869 dwarf2_start_subfile (m_cu, fe->name, dir);
19870 }
19871 }
19872
19873 void
19874 lnp_state_machine::handle_const_add_pc ()
19875 {
19876 CORE_ADDR adjust
19877 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19878
19879 CORE_ADDR addr_adj
19880 = (((m_op_index + adjust)
19881 / m_line_header->maximum_ops_per_instruction)
19882 * m_line_header->minimum_instruction_length);
19883
19884 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19885 m_op_index = ((m_op_index + adjust)
19886 % m_line_header->maximum_ops_per_instruction);
19887 }
19888
19889 /* Return non-zero if we should add LINE to the line number table.
19890 LINE is the line to add, LAST_LINE is the last line that was added,
19891 LAST_SUBFILE is the subfile for LAST_LINE.
19892 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19893 had a non-zero discriminator.
19894
19895 We have to be careful in the presence of discriminators.
19896 E.g., for this line:
19897
19898 for (i = 0; i < 100000; i++);
19899
19900 clang can emit four line number entries for that one line,
19901 each with a different discriminator.
19902 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19903
19904 However, we want gdb to coalesce all four entries into one.
19905 Otherwise the user could stepi into the middle of the line and
19906 gdb would get confused about whether the pc really was in the
19907 middle of the line.
19908
19909 Things are further complicated by the fact that two consecutive
19910 line number entries for the same line is a heuristic used by gcc
19911 to denote the end of the prologue. So we can't just discard duplicate
19912 entries, we have to be selective about it. The heuristic we use is
19913 that we only collapse consecutive entries for the same line if at least
19914 one of those entries has a non-zero discriminator. PR 17276.
19915
19916 Note: Addresses in the line number state machine can never go backwards
19917 within one sequence, thus this coalescing is ok. */
19918
19919 static int
19920 dwarf_record_line_p (struct dwarf2_cu *cu,
19921 unsigned int line, unsigned int last_line,
19922 int line_has_non_zero_discriminator,
19923 struct subfile *last_subfile)
19924 {
19925 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19926 return 1;
19927 if (line != last_line)
19928 return 1;
19929 /* Same line for the same file that we've seen already.
19930 As a last check, for pr 17276, only record the line if the line
19931 has never had a non-zero discriminator. */
19932 if (!line_has_non_zero_discriminator)
19933 return 1;
19934 return 0;
19935 }
19936
19937 /* Use the CU's builder to record line number LINE beginning at
19938 address ADDRESS in the line table of subfile SUBFILE. */
19939
19940 static void
19941 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19942 unsigned int line, CORE_ADDR address, bool is_stmt,
19943 struct dwarf2_cu *cu)
19944 {
19945 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19946
19947 if (dwarf_line_debug)
19948 {
19949 fprintf_unfiltered (gdb_stdlog,
19950 "Recording line %u, file %s, address %s\n",
19951 line, lbasename (subfile->name),
19952 paddress (gdbarch, address));
19953 }
19954
19955 if (cu != nullptr)
19956 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19957 }
19958
19959 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19960 Mark the end of a set of line number records.
19961 The arguments are the same as for dwarf_record_line_1.
19962 If SUBFILE is NULL the request is ignored. */
19963
19964 static void
19965 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19966 CORE_ADDR address, struct dwarf2_cu *cu)
19967 {
19968 if (subfile == NULL)
19969 return;
19970
19971 if (dwarf_line_debug)
19972 {
19973 fprintf_unfiltered (gdb_stdlog,
19974 "Finishing current line, file %s, address %s\n",
19975 lbasename (subfile->name),
19976 paddress (gdbarch, address));
19977 }
19978
19979 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19980 }
19981
19982 void
19983 lnp_state_machine::record_line (bool end_sequence)
19984 {
19985 if (dwarf_line_debug)
19986 {
19987 fprintf_unfiltered (gdb_stdlog,
19988 "Processing actual line %u: file %u,"
19989 " address %s, is_stmt %u, discrim %u%s\n",
19990 m_line, m_file,
19991 paddress (m_gdbarch, m_address),
19992 m_is_stmt, m_discriminator,
19993 (end_sequence ? "\t(end sequence)" : ""));
19994 }
19995
19996 file_entry *fe = current_file ();
19997
19998 if (fe == NULL)
19999 dwarf2_debug_line_missing_file_complaint ();
20000 /* For now we ignore lines not starting on an instruction boundary.
20001 But not when processing end_sequence for compatibility with the
20002 previous version of the code. */
20003 else if (m_op_index == 0 || end_sequence)
20004 {
20005 fe->included_p = 1;
20006 if (m_record_lines_p)
20007 {
20008 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20009 || end_sequence)
20010 {
20011 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20012 m_currently_recording_lines ? m_cu : nullptr);
20013 }
20014
20015 if (!end_sequence)
20016 {
20017 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20018
20019 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20020 m_line_has_non_zero_discriminator,
20021 m_last_subfile))
20022 {
20023 buildsym_compunit *builder = m_cu->get_builder ();
20024 dwarf_record_line_1 (m_gdbarch,
20025 builder->get_current_subfile (),
20026 m_line, m_address, is_stmt,
20027 m_currently_recording_lines ? m_cu : nullptr);
20028 }
20029 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20030 m_last_line = m_line;
20031 }
20032 }
20033 }
20034 }
20035
20036 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20037 line_header *lh, bool record_lines_p)
20038 {
20039 m_cu = cu;
20040 m_gdbarch = arch;
20041 m_record_lines_p = record_lines_p;
20042 m_line_header = lh;
20043
20044 m_currently_recording_lines = true;
20045
20046 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20047 was a line entry for it so that the backend has a chance to adjust it
20048 and also record it in case it needs it. This is currently used by MIPS
20049 code, cf. `mips_adjust_dwarf2_line'. */
20050 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20051 m_is_stmt = lh->default_is_stmt;
20052 m_discriminator = 0;
20053 }
20054
20055 void
20056 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20057 const gdb_byte *line_ptr,
20058 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20059 {
20060 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20061 the pc range of the CU. However, we restrict the test to only ADDRESS
20062 values of zero to preserve GDB's previous behaviour which is to handle
20063 the specific case of a function being GC'd by the linker. */
20064
20065 if (address == 0 && address < unrelocated_lowpc)
20066 {
20067 /* This line table is for a function which has been
20068 GCd by the linker. Ignore it. PR gdb/12528 */
20069
20070 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20071 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20072
20073 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20074 line_offset, objfile_name (objfile));
20075 m_currently_recording_lines = false;
20076 /* Note: m_currently_recording_lines is left as false until we see
20077 DW_LNE_end_sequence. */
20078 }
20079 }
20080
20081 /* Subroutine of dwarf_decode_lines to simplify it.
20082 Process the line number information in LH.
20083 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20084 program in order to set included_p for every referenced header. */
20085
20086 static void
20087 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20088 const int decode_for_pst_p, CORE_ADDR lowpc)
20089 {
20090 const gdb_byte *line_ptr, *extended_end;
20091 const gdb_byte *line_end;
20092 unsigned int bytes_read, extended_len;
20093 unsigned char op_code, extended_op;
20094 CORE_ADDR baseaddr;
20095 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20096 bfd *abfd = objfile->obfd;
20097 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20098 /* True if we're recording line info (as opposed to building partial
20099 symtabs and just interested in finding include files mentioned by
20100 the line number program). */
20101 bool record_lines_p = !decode_for_pst_p;
20102
20103 baseaddr = objfile->text_section_offset ();
20104
20105 line_ptr = lh->statement_program_start;
20106 line_end = lh->statement_program_end;
20107
20108 /* Read the statement sequences until there's nothing left. */
20109 while (line_ptr < line_end)
20110 {
20111 /* The DWARF line number program state machine. Reset the state
20112 machine at the start of each sequence. */
20113 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20114 bool end_sequence = false;
20115
20116 if (record_lines_p)
20117 {
20118 /* Start a subfile for the current file of the state
20119 machine. */
20120 const file_entry *fe = state_machine.current_file ();
20121
20122 if (fe != NULL)
20123 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20124 }
20125
20126 /* Decode the table. */
20127 while (line_ptr < line_end && !end_sequence)
20128 {
20129 op_code = read_1_byte (abfd, line_ptr);
20130 line_ptr += 1;
20131
20132 if (op_code >= lh->opcode_base)
20133 {
20134 /* Special opcode. */
20135 state_machine.handle_special_opcode (op_code);
20136 }
20137 else switch (op_code)
20138 {
20139 case DW_LNS_extended_op:
20140 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20141 &bytes_read);
20142 line_ptr += bytes_read;
20143 extended_end = line_ptr + extended_len;
20144 extended_op = read_1_byte (abfd, line_ptr);
20145 line_ptr += 1;
20146 switch (extended_op)
20147 {
20148 case DW_LNE_end_sequence:
20149 state_machine.handle_end_sequence ();
20150 end_sequence = true;
20151 break;
20152 case DW_LNE_set_address:
20153 {
20154 CORE_ADDR address
20155 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20156 line_ptr += bytes_read;
20157
20158 state_machine.check_line_address (cu, line_ptr,
20159 lowpc - baseaddr, address);
20160 state_machine.handle_set_address (baseaddr, address);
20161 }
20162 break;
20163 case DW_LNE_define_file:
20164 {
20165 const char *cur_file;
20166 unsigned int mod_time, length;
20167 dir_index dindex;
20168
20169 cur_file = read_direct_string (abfd, line_ptr,
20170 &bytes_read);
20171 line_ptr += bytes_read;
20172 dindex = (dir_index)
20173 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20174 line_ptr += bytes_read;
20175 mod_time =
20176 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20177 line_ptr += bytes_read;
20178 length =
20179 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20180 line_ptr += bytes_read;
20181 lh->add_file_name (cur_file, dindex, mod_time, length);
20182 }
20183 break;
20184 case DW_LNE_set_discriminator:
20185 {
20186 /* The discriminator is not interesting to the
20187 debugger; just ignore it. We still need to
20188 check its value though:
20189 if there are consecutive entries for the same
20190 (non-prologue) line we want to coalesce them.
20191 PR 17276. */
20192 unsigned int discr
20193 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20194 line_ptr += bytes_read;
20195
20196 state_machine.handle_set_discriminator (discr);
20197 }
20198 break;
20199 default:
20200 complaint (_("mangled .debug_line section"));
20201 return;
20202 }
20203 /* Make sure that we parsed the extended op correctly. If e.g.
20204 we expected a different address size than the producer used,
20205 we may have read the wrong number of bytes. */
20206 if (line_ptr != extended_end)
20207 {
20208 complaint (_("mangled .debug_line section"));
20209 return;
20210 }
20211 break;
20212 case DW_LNS_copy:
20213 state_machine.handle_copy ();
20214 break;
20215 case DW_LNS_advance_pc:
20216 {
20217 CORE_ADDR adjust
20218 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20219 line_ptr += bytes_read;
20220
20221 state_machine.handle_advance_pc (adjust);
20222 }
20223 break;
20224 case DW_LNS_advance_line:
20225 {
20226 int line_delta
20227 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20228 line_ptr += bytes_read;
20229
20230 state_machine.handle_advance_line (line_delta);
20231 }
20232 break;
20233 case DW_LNS_set_file:
20234 {
20235 file_name_index file
20236 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20237 &bytes_read);
20238 line_ptr += bytes_read;
20239
20240 state_machine.handle_set_file (file);
20241 }
20242 break;
20243 case DW_LNS_set_column:
20244 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20245 line_ptr += bytes_read;
20246 break;
20247 case DW_LNS_negate_stmt:
20248 state_machine.handle_negate_stmt ();
20249 break;
20250 case DW_LNS_set_basic_block:
20251 break;
20252 /* Add to the address register of the state machine the
20253 address increment value corresponding to special opcode
20254 255. I.e., this value is scaled by the minimum
20255 instruction length since special opcode 255 would have
20256 scaled the increment. */
20257 case DW_LNS_const_add_pc:
20258 state_machine.handle_const_add_pc ();
20259 break;
20260 case DW_LNS_fixed_advance_pc:
20261 {
20262 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20263 line_ptr += 2;
20264
20265 state_machine.handle_fixed_advance_pc (addr_adj);
20266 }
20267 break;
20268 default:
20269 {
20270 /* Unknown standard opcode, ignore it. */
20271 int i;
20272
20273 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20274 {
20275 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20276 line_ptr += bytes_read;
20277 }
20278 }
20279 }
20280 }
20281
20282 if (!end_sequence)
20283 dwarf2_debug_line_missing_end_sequence_complaint ();
20284
20285 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20286 in which case we still finish recording the last line). */
20287 state_machine.record_line (true);
20288 }
20289 }
20290
20291 /* Decode the Line Number Program (LNP) for the given line_header
20292 structure and CU. The actual information extracted and the type
20293 of structures created from the LNP depends on the value of PST.
20294
20295 1. If PST is NULL, then this procedure uses the data from the program
20296 to create all necessary symbol tables, and their linetables.
20297
20298 2. If PST is not NULL, this procedure reads the program to determine
20299 the list of files included by the unit represented by PST, and
20300 builds all the associated partial symbol tables.
20301
20302 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20303 It is used for relative paths in the line table.
20304 NOTE: When processing partial symtabs (pst != NULL),
20305 comp_dir == pst->dirname.
20306
20307 NOTE: It is important that psymtabs have the same file name (via strcmp)
20308 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20309 symtab we don't use it in the name of the psymtabs we create.
20310 E.g. expand_line_sal requires this when finding psymtabs to expand.
20311 A good testcase for this is mb-inline.exp.
20312
20313 LOWPC is the lowest address in CU (or 0 if not known).
20314
20315 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20316 for its PC<->lines mapping information. Otherwise only the filename
20317 table is read in. */
20318
20319 static void
20320 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20321 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20322 CORE_ADDR lowpc, int decode_mapping)
20323 {
20324 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20325 const int decode_for_pst_p = (pst != NULL);
20326
20327 if (decode_mapping)
20328 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20329
20330 if (decode_for_pst_p)
20331 {
20332 /* Now that we're done scanning the Line Header Program, we can
20333 create the psymtab of each included file. */
20334 for (auto &file_entry : lh->file_names ())
20335 if (file_entry.included_p == 1)
20336 {
20337 gdb::unique_xmalloc_ptr<char> name_holder;
20338 const char *include_name =
20339 psymtab_include_file_name (lh, file_entry, pst,
20340 comp_dir, &name_holder);
20341 if (include_name != NULL)
20342 dwarf2_create_include_psymtab (include_name, pst, objfile);
20343 }
20344 }
20345 else
20346 {
20347 /* Make sure a symtab is created for every file, even files
20348 which contain only variables (i.e. no code with associated
20349 line numbers). */
20350 buildsym_compunit *builder = cu->get_builder ();
20351 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20352
20353 for (auto &fe : lh->file_names ())
20354 {
20355 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20356 if (builder->get_current_subfile ()->symtab == NULL)
20357 {
20358 builder->get_current_subfile ()->symtab
20359 = allocate_symtab (cust,
20360 builder->get_current_subfile ()->name);
20361 }
20362 fe.symtab = builder->get_current_subfile ()->symtab;
20363 }
20364 }
20365 }
20366
20367 /* Start a subfile for DWARF. FILENAME is the name of the file and
20368 DIRNAME the name of the source directory which contains FILENAME
20369 or NULL if not known.
20370 This routine tries to keep line numbers from identical absolute and
20371 relative file names in a common subfile.
20372
20373 Using the `list' example from the GDB testsuite, which resides in
20374 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20375 of /srcdir/list0.c yields the following debugging information for list0.c:
20376
20377 DW_AT_name: /srcdir/list0.c
20378 DW_AT_comp_dir: /compdir
20379 files.files[0].name: list0.h
20380 files.files[0].dir: /srcdir
20381 files.files[1].name: list0.c
20382 files.files[1].dir: /srcdir
20383
20384 The line number information for list0.c has to end up in a single
20385 subfile, so that `break /srcdir/list0.c:1' works as expected.
20386 start_subfile will ensure that this happens provided that we pass the
20387 concatenation of files.files[1].dir and files.files[1].name as the
20388 subfile's name. */
20389
20390 static void
20391 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20392 const char *dirname)
20393 {
20394 gdb::unique_xmalloc_ptr<char> copy;
20395
20396 /* In order not to lose the line information directory,
20397 we concatenate it to the filename when it makes sense.
20398 Note that the Dwarf3 standard says (speaking of filenames in line
20399 information): ``The directory index is ignored for file names
20400 that represent full path names''. Thus ignoring dirname in the
20401 `else' branch below isn't an issue. */
20402
20403 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20404 {
20405 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20406 filename = copy.get ();
20407 }
20408
20409 cu->get_builder ()->start_subfile (filename);
20410 }
20411
20412 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20413 buildsym_compunit constructor. */
20414
20415 struct compunit_symtab *
20416 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20417 CORE_ADDR low_pc)
20418 {
20419 gdb_assert (m_builder == nullptr);
20420
20421 m_builder.reset (new struct buildsym_compunit
20422 (per_cu->dwarf2_per_objfile->objfile,
20423 name, comp_dir, language, low_pc));
20424
20425 list_in_scope = get_builder ()->get_file_symbols ();
20426
20427 get_builder ()->record_debugformat ("DWARF 2");
20428 get_builder ()->record_producer (producer);
20429
20430 processing_has_namespace_info = false;
20431
20432 return get_builder ()->get_compunit_symtab ();
20433 }
20434
20435 static void
20436 var_decode_location (struct attribute *attr, struct symbol *sym,
20437 struct dwarf2_cu *cu)
20438 {
20439 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20440 struct comp_unit_head *cu_header = &cu->header;
20441
20442 /* NOTE drow/2003-01-30: There used to be a comment and some special
20443 code here to turn a symbol with DW_AT_external and a
20444 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20445 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20446 with some versions of binutils) where shared libraries could have
20447 relocations against symbols in their debug information - the
20448 minimal symbol would have the right address, but the debug info
20449 would not. It's no longer necessary, because we will explicitly
20450 apply relocations when we read in the debug information now. */
20451
20452 /* A DW_AT_location attribute with no contents indicates that a
20453 variable has been optimized away. */
20454 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20455 {
20456 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20457 return;
20458 }
20459
20460 /* Handle one degenerate form of location expression specially, to
20461 preserve GDB's previous behavior when section offsets are
20462 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20463 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20464
20465 if (attr->form_is_block ()
20466 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20467 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20468 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20469 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20470 && (DW_BLOCK (attr)->size
20471 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20472 {
20473 unsigned int dummy;
20474
20475 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20476 SET_SYMBOL_VALUE_ADDRESS
20477 (sym, cu->header.read_address (objfile->obfd,
20478 DW_BLOCK (attr)->data + 1,
20479 &dummy));
20480 else
20481 SET_SYMBOL_VALUE_ADDRESS
20482 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20483 &dummy));
20484 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20485 fixup_symbol_section (sym, objfile);
20486 SET_SYMBOL_VALUE_ADDRESS
20487 (sym,
20488 SYMBOL_VALUE_ADDRESS (sym)
20489 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20490 return;
20491 }
20492
20493 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20494 expression evaluator, and use LOC_COMPUTED only when necessary
20495 (i.e. when the value of a register or memory location is
20496 referenced, or a thread-local block, etc.). Then again, it might
20497 not be worthwhile. I'm assuming that it isn't unless performance
20498 or memory numbers show me otherwise. */
20499
20500 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20501
20502 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20503 cu->has_loclist = true;
20504 }
20505
20506 /* Given a pointer to a DWARF information entry, figure out if we need
20507 to make a symbol table entry for it, and if so, create a new entry
20508 and return a pointer to it.
20509 If TYPE is NULL, determine symbol type from the die, otherwise
20510 used the passed type.
20511 If SPACE is not NULL, use it to hold the new symbol. If it is
20512 NULL, allocate a new symbol on the objfile's obstack. */
20513
20514 static struct symbol *
20515 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20516 struct symbol *space)
20517 {
20518 struct dwarf2_per_objfile *dwarf2_per_objfile
20519 = cu->per_cu->dwarf2_per_objfile;
20520 struct objfile *objfile = dwarf2_per_objfile->objfile;
20521 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20522 struct symbol *sym = NULL;
20523 const char *name;
20524 struct attribute *attr = NULL;
20525 struct attribute *attr2 = NULL;
20526 CORE_ADDR baseaddr;
20527 struct pending **list_to_add = NULL;
20528
20529 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20530
20531 baseaddr = objfile->text_section_offset ();
20532
20533 name = dwarf2_name (die, cu);
20534 if (name)
20535 {
20536 const char *linkagename;
20537 int suppress_add = 0;
20538
20539 if (space)
20540 sym = space;
20541 else
20542 sym = allocate_symbol (objfile);
20543 OBJSTAT (objfile, n_syms++);
20544
20545 /* Cache this symbol's name and the name's demangled form (if any). */
20546 sym->set_language (cu->language, &objfile->objfile_obstack);
20547 linkagename = dwarf2_physname (name, die, cu);
20548 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20549
20550 /* Fortran does not have mangling standard and the mangling does differ
20551 between gfortran, iFort etc. */
20552 if (cu->language == language_fortran
20553 && symbol_get_demangled_name (sym) == NULL)
20554 symbol_set_demangled_name (sym,
20555 dwarf2_full_name (name, die, cu),
20556 NULL);
20557
20558 /* Default assumptions.
20559 Use the passed type or decode it from the die. */
20560 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20561 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20562 if (type != NULL)
20563 SYMBOL_TYPE (sym) = type;
20564 else
20565 SYMBOL_TYPE (sym) = die_type (die, cu);
20566 attr = dwarf2_attr (die,
20567 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20568 cu);
20569 if (attr != nullptr)
20570 {
20571 SYMBOL_LINE (sym) = DW_UNSND (attr);
20572 }
20573
20574 attr = dwarf2_attr (die,
20575 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20576 cu);
20577 if (attr != nullptr)
20578 {
20579 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20580 struct file_entry *fe;
20581
20582 if (cu->line_header != NULL)
20583 fe = cu->line_header->file_name_at (file_index);
20584 else
20585 fe = NULL;
20586
20587 if (fe == NULL)
20588 complaint (_("file index out of range"));
20589 else
20590 symbol_set_symtab (sym, fe->symtab);
20591 }
20592
20593 switch (die->tag)
20594 {
20595 case DW_TAG_label:
20596 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20597 if (attr != nullptr)
20598 {
20599 CORE_ADDR addr;
20600
20601 addr = attr->value_as_address ();
20602 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20603 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20604 }
20605 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20606 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20607 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20608 add_symbol_to_list (sym, cu->list_in_scope);
20609 break;
20610 case DW_TAG_subprogram:
20611 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20612 finish_block. */
20613 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20614 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20615 if ((attr2 && (DW_UNSND (attr2) != 0))
20616 || cu->language == language_ada
20617 || cu->language == language_fortran)
20618 {
20619 /* Subprograms marked external are stored as a global symbol.
20620 Ada and Fortran subprograms, whether marked external or
20621 not, are always stored as a global symbol, because we want
20622 to be able to access them globally. For instance, we want
20623 to be able to break on a nested subprogram without having
20624 to specify the context. */
20625 list_to_add = cu->get_builder ()->get_global_symbols ();
20626 }
20627 else
20628 {
20629 list_to_add = cu->list_in_scope;
20630 }
20631 break;
20632 case DW_TAG_inlined_subroutine:
20633 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20634 finish_block. */
20635 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20636 SYMBOL_INLINED (sym) = 1;
20637 list_to_add = cu->list_in_scope;
20638 break;
20639 case DW_TAG_template_value_param:
20640 suppress_add = 1;
20641 /* Fall through. */
20642 case DW_TAG_constant:
20643 case DW_TAG_variable:
20644 case DW_TAG_member:
20645 /* Compilation with minimal debug info may result in
20646 variables with missing type entries. Change the
20647 misleading `void' type to something sensible. */
20648 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20649 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20650
20651 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20652 /* In the case of DW_TAG_member, we should only be called for
20653 static const members. */
20654 if (die->tag == DW_TAG_member)
20655 {
20656 /* dwarf2_add_field uses die_is_declaration,
20657 so we do the same. */
20658 gdb_assert (die_is_declaration (die, cu));
20659 gdb_assert (attr);
20660 }
20661 if (attr != nullptr)
20662 {
20663 dwarf2_const_value (attr, sym, cu);
20664 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20665 if (!suppress_add)
20666 {
20667 if (attr2 && (DW_UNSND (attr2) != 0))
20668 list_to_add = cu->get_builder ()->get_global_symbols ();
20669 else
20670 list_to_add = cu->list_in_scope;
20671 }
20672 break;
20673 }
20674 attr = dwarf2_attr (die, DW_AT_location, cu);
20675 if (attr != nullptr)
20676 {
20677 var_decode_location (attr, sym, cu);
20678 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20679
20680 /* Fortran explicitly imports any global symbols to the local
20681 scope by DW_TAG_common_block. */
20682 if (cu->language == language_fortran && die->parent
20683 && die->parent->tag == DW_TAG_common_block)
20684 attr2 = NULL;
20685
20686 if (SYMBOL_CLASS (sym) == LOC_STATIC
20687 && SYMBOL_VALUE_ADDRESS (sym) == 0
20688 && !dwarf2_per_objfile->has_section_at_zero)
20689 {
20690 /* When a static variable is eliminated by the linker,
20691 the corresponding debug information is not stripped
20692 out, but the variable address is set to null;
20693 do not add such variables into symbol table. */
20694 }
20695 else if (attr2 && (DW_UNSND (attr2) != 0))
20696 {
20697 if (SYMBOL_CLASS (sym) == LOC_STATIC
20698 && (objfile->flags & OBJF_MAINLINE) == 0
20699 && dwarf2_per_objfile->can_copy)
20700 {
20701 /* A global static variable might be subject to
20702 copy relocation. We first check for a local
20703 minsym, though, because maybe the symbol was
20704 marked hidden, in which case this would not
20705 apply. */
20706 bound_minimal_symbol found
20707 = (lookup_minimal_symbol_linkage
20708 (sym->linkage_name (), objfile));
20709 if (found.minsym != nullptr)
20710 sym->maybe_copied = 1;
20711 }
20712
20713 /* A variable with DW_AT_external is never static,
20714 but it may be block-scoped. */
20715 list_to_add
20716 = ((cu->list_in_scope
20717 == cu->get_builder ()->get_file_symbols ())
20718 ? cu->get_builder ()->get_global_symbols ()
20719 : cu->list_in_scope);
20720 }
20721 else
20722 list_to_add = cu->list_in_scope;
20723 }
20724 else
20725 {
20726 /* We do not know the address of this symbol.
20727 If it is an external symbol and we have type information
20728 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20729 The address of the variable will then be determined from
20730 the minimal symbol table whenever the variable is
20731 referenced. */
20732 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20733
20734 /* Fortran explicitly imports any global symbols to the local
20735 scope by DW_TAG_common_block. */
20736 if (cu->language == language_fortran && die->parent
20737 && die->parent->tag == DW_TAG_common_block)
20738 {
20739 /* SYMBOL_CLASS doesn't matter here because
20740 read_common_block is going to reset it. */
20741 if (!suppress_add)
20742 list_to_add = cu->list_in_scope;
20743 }
20744 else if (attr2 && (DW_UNSND (attr2) != 0)
20745 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20746 {
20747 /* A variable with DW_AT_external is never static, but it
20748 may be block-scoped. */
20749 list_to_add
20750 = ((cu->list_in_scope
20751 == cu->get_builder ()->get_file_symbols ())
20752 ? cu->get_builder ()->get_global_symbols ()
20753 : cu->list_in_scope);
20754
20755 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20756 }
20757 else if (!die_is_declaration (die, cu))
20758 {
20759 /* Use the default LOC_OPTIMIZED_OUT class. */
20760 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20761 if (!suppress_add)
20762 list_to_add = cu->list_in_scope;
20763 }
20764 }
20765 break;
20766 case DW_TAG_formal_parameter:
20767 {
20768 /* If we are inside a function, mark this as an argument. If
20769 not, we might be looking at an argument to an inlined function
20770 when we do not have enough information to show inlined frames;
20771 pretend it's a local variable in that case so that the user can
20772 still see it. */
20773 struct context_stack *curr
20774 = cu->get_builder ()->get_current_context_stack ();
20775 if (curr != nullptr && curr->name != nullptr)
20776 SYMBOL_IS_ARGUMENT (sym) = 1;
20777 attr = dwarf2_attr (die, DW_AT_location, cu);
20778 if (attr != nullptr)
20779 {
20780 var_decode_location (attr, sym, cu);
20781 }
20782 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20783 if (attr != nullptr)
20784 {
20785 dwarf2_const_value (attr, sym, cu);
20786 }
20787
20788 list_to_add = cu->list_in_scope;
20789 }
20790 break;
20791 case DW_TAG_unspecified_parameters:
20792 /* From varargs functions; gdb doesn't seem to have any
20793 interest in this information, so just ignore it for now.
20794 (FIXME?) */
20795 break;
20796 case DW_TAG_template_type_param:
20797 suppress_add = 1;
20798 /* Fall through. */
20799 case DW_TAG_class_type:
20800 case DW_TAG_interface_type:
20801 case DW_TAG_structure_type:
20802 case DW_TAG_union_type:
20803 case DW_TAG_set_type:
20804 case DW_TAG_enumeration_type:
20805 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20806 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20807
20808 {
20809 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20810 really ever be static objects: otherwise, if you try
20811 to, say, break of a class's method and you're in a file
20812 which doesn't mention that class, it won't work unless
20813 the check for all static symbols in lookup_symbol_aux
20814 saves you. See the OtherFileClass tests in
20815 gdb.c++/namespace.exp. */
20816
20817 if (!suppress_add)
20818 {
20819 buildsym_compunit *builder = cu->get_builder ();
20820 list_to_add
20821 = (cu->list_in_scope == builder->get_file_symbols ()
20822 && cu->language == language_cplus
20823 ? builder->get_global_symbols ()
20824 : cu->list_in_scope);
20825
20826 /* The semantics of C++ state that "struct foo {
20827 ... }" also defines a typedef for "foo". */
20828 if (cu->language == language_cplus
20829 || cu->language == language_ada
20830 || cu->language == language_d
20831 || cu->language == language_rust)
20832 {
20833 /* The symbol's name is already allocated along
20834 with this objfile, so we don't need to
20835 duplicate it for the type. */
20836 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20837 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20838 }
20839 }
20840 }
20841 break;
20842 case DW_TAG_typedef:
20843 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20844 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20845 list_to_add = cu->list_in_scope;
20846 break;
20847 case DW_TAG_base_type:
20848 case DW_TAG_subrange_type:
20849 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20850 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20851 list_to_add = cu->list_in_scope;
20852 break;
20853 case DW_TAG_enumerator:
20854 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20855 if (attr != nullptr)
20856 {
20857 dwarf2_const_value (attr, sym, cu);
20858 }
20859 {
20860 /* NOTE: carlton/2003-11-10: See comment above in the
20861 DW_TAG_class_type, etc. block. */
20862
20863 list_to_add
20864 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20865 && cu->language == language_cplus
20866 ? cu->get_builder ()->get_global_symbols ()
20867 : cu->list_in_scope);
20868 }
20869 break;
20870 case DW_TAG_imported_declaration:
20871 case DW_TAG_namespace:
20872 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20873 list_to_add = cu->get_builder ()->get_global_symbols ();
20874 break;
20875 case DW_TAG_module:
20876 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20877 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20878 list_to_add = cu->get_builder ()->get_global_symbols ();
20879 break;
20880 case DW_TAG_common_block:
20881 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20882 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20883 add_symbol_to_list (sym, cu->list_in_scope);
20884 break;
20885 default:
20886 /* Not a tag we recognize. Hopefully we aren't processing
20887 trash data, but since we must specifically ignore things
20888 we don't recognize, there is nothing else we should do at
20889 this point. */
20890 complaint (_("unsupported tag: '%s'"),
20891 dwarf_tag_name (die->tag));
20892 break;
20893 }
20894
20895 if (suppress_add)
20896 {
20897 sym->hash_next = objfile->template_symbols;
20898 objfile->template_symbols = sym;
20899 list_to_add = NULL;
20900 }
20901
20902 if (list_to_add != NULL)
20903 add_symbol_to_list (sym, list_to_add);
20904
20905 /* For the benefit of old versions of GCC, check for anonymous
20906 namespaces based on the demangled name. */
20907 if (!cu->processing_has_namespace_info
20908 && cu->language == language_cplus)
20909 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20910 }
20911 return (sym);
20912 }
20913
20914 /* Given an attr with a DW_FORM_dataN value in host byte order,
20915 zero-extend it as appropriate for the symbol's type. The DWARF
20916 standard (v4) is not entirely clear about the meaning of using
20917 DW_FORM_dataN for a constant with a signed type, where the type is
20918 wider than the data. The conclusion of a discussion on the DWARF
20919 list was that this is unspecified. We choose to always zero-extend
20920 because that is the interpretation long in use by GCC. */
20921
20922 static gdb_byte *
20923 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20924 struct dwarf2_cu *cu, LONGEST *value, int bits)
20925 {
20926 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20927 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20928 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20929 LONGEST l = DW_UNSND (attr);
20930
20931 if (bits < sizeof (*value) * 8)
20932 {
20933 l &= ((LONGEST) 1 << bits) - 1;
20934 *value = l;
20935 }
20936 else if (bits == sizeof (*value) * 8)
20937 *value = l;
20938 else
20939 {
20940 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20941 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20942 return bytes;
20943 }
20944
20945 return NULL;
20946 }
20947
20948 /* Read a constant value from an attribute. Either set *VALUE, or if
20949 the value does not fit in *VALUE, set *BYTES - either already
20950 allocated on the objfile obstack, or newly allocated on OBSTACK,
20951 or, set *BATON, if we translated the constant to a location
20952 expression. */
20953
20954 static void
20955 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20956 const char *name, struct obstack *obstack,
20957 struct dwarf2_cu *cu,
20958 LONGEST *value, const gdb_byte **bytes,
20959 struct dwarf2_locexpr_baton **baton)
20960 {
20961 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20962 struct comp_unit_head *cu_header = &cu->header;
20963 struct dwarf_block *blk;
20964 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20965 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20966
20967 *value = 0;
20968 *bytes = NULL;
20969 *baton = NULL;
20970
20971 switch (attr->form)
20972 {
20973 case DW_FORM_addr:
20974 case DW_FORM_addrx:
20975 case DW_FORM_GNU_addr_index:
20976 {
20977 gdb_byte *data;
20978
20979 if (TYPE_LENGTH (type) != cu_header->addr_size)
20980 dwarf2_const_value_length_mismatch_complaint (name,
20981 cu_header->addr_size,
20982 TYPE_LENGTH (type));
20983 /* Symbols of this form are reasonably rare, so we just
20984 piggyback on the existing location code rather than writing
20985 a new implementation of symbol_computed_ops. */
20986 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20987 (*baton)->per_cu = cu->per_cu;
20988 gdb_assert ((*baton)->per_cu);
20989
20990 (*baton)->size = 2 + cu_header->addr_size;
20991 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20992 (*baton)->data = data;
20993
20994 data[0] = DW_OP_addr;
20995 store_unsigned_integer (&data[1], cu_header->addr_size,
20996 byte_order, DW_ADDR (attr));
20997 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20998 }
20999 break;
21000 case DW_FORM_string:
21001 case DW_FORM_strp:
21002 case DW_FORM_strx:
21003 case DW_FORM_GNU_str_index:
21004 case DW_FORM_GNU_strp_alt:
21005 /* DW_STRING is already allocated on the objfile obstack, point
21006 directly to it. */
21007 *bytes = (const gdb_byte *) DW_STRING (attr);
21008 break;
21009 case DW_FORM_block1:
21010 case DW_FORM_block2:
21011 case DW_FORM_block4:
21012 case DW_FORM_block:
21013 case DW_FORM_exprloc:
21014 case DW_FORM_data16:
21015 blk = DW_BLOCK (attr);
21016 if (TYPE_LENGTH (type) != blk->size)
21017 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21018 TYPE_LENGTH (type));
21019 *bytes = blk->data;
21020 break;
21021
21022 /* The DW_AT_const_value attributes are supposed to carry the
21023 symbol's value "represented as it would be on the target
21024 architecture." By the time we get here, it's already been
21025 converted to host endianness, so we just need to sign- or
21026 zero-extend it as appropriate. */
21027 case DW_FORM_data1:
21028 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21029 break;
21030 case DW_FORM_data2:
21031 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21032 break;
21033 case DW_FORM_data4:
21034 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21035 break;
21036 case DW_FORM_data8:
21037 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21038 break;
21039
21040 case DW_FORM_sdata:
21041 case DW_FORM_implicit_const:
21042 *value = DW_SND (attr);
21043 break;
21044
21045 case DW_FORM_udata:
21046 *value = DW_UNSND (attr);
21047 break;
21048
21049 default:
21050 complaint (_("unsupported const value attribute form: '%s'"),
21051 dwarf_form_name (attr->form));
21052 *value = 0;
21053 break;
21054 }
21055 }
21056
21057
21058 /* Copy constant value from an attribute to a symbol. */
21059
21060 static void
21061 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21062 struct dwarf2_cu *cu)
21063 {
21064 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21065 LONGEST value;
21066 const gdb_byte *bytes;
21067 struct dwarf2_locexpr_baton *baton;
21068
21069 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21070 sym->print_name (),
21071 &objfile->objfile_obstack, cu,
21072 &value, &bytes, &baton);
21073
21074 if (baton != NULL)
21075 {
21076 SYMBOL_LOCATION_BATON (sym) = baton;
21077 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21078 }
21079 else if (bytes != NULL)
21080 {
21081 SYMBOL_VALUE_BYTES (sym) = bytes;
21082 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21083 }
21084 else
21085 {
21086 SYMBOL_VALUE (sym) = value;
21087 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21088 }
21089 }
21090
21091 /* Return the type of the die in question using its DW_AT_type attribute. */
21092
21093 static struct type *
21094 die_type (struct die_info *die, struct dwarf2_cu *cu)
21095 {
21096 struct attribute *type_attr;
21097
21098 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21099 if (!type_attr)
21100 {
21101 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21102 /* A missing DW_AT_type represents a void type. */
21103 return objfile_type (objfile)->builtin_void;
21104 }
21105
21106 return lookup_die_type (die, type_attr, cu);
21107 }
21108
21109 /* True iff CU's producer generates GNAT Ada auxiliary information
21110 that allows to find parallel types through that information instead
21111 of having to do expensive parallel lookups by type name. */
21112
21113 static int
21114 need_gnat_info (struct dwarf2_cu *cu)
21115 {
21116 /* Assume that the Ada compiler was GNAT, which always produces
21117 the auxiliary information. */
21118 return (cu->language == language_ada);
21119 }
21120
21121 /* Return the auxiliary type of the die in question using its
21122 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21123 attribute is not present. */
21124
21125 static struct type *
21126 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21127 {
21128 struct attribute *type_attr;
21129
21130 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21131 if (!type_attr)
21132 return NULL;
21133
21134 return lookup_die_type (die, type_attr, cu);
21135 }
21136
21137 /* If DIE has a descriptive_type attribute, then set the TYPE's
21138 descriptive type accordingly. */
21139
21140 static void
21141 set_descriptive_type (struct type *type, struct die_info *die,
21142 struct dwarf2_cu *cu)
21143 {
21144 struct type *descriptive_type = die_descriptive_type (die, cu);
21145
21146 if (descriptive_type)
21147 {
21148 ALLOCATE_GNAT_AUX_TYPE (type);
21149 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21150 }
21151 }
21152
21153 /* Return the containing type of the die in question using its
21154 DW_AT_containing_type attribute. */
21155
21156 static struct type *
21157 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21158 {
21159 struct attribute *type_attr;
21160 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21161
21162 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21163 if (!type_attr)
21164 error (_("Dwarf Error: Problem turning containing type into gdb type "
21165 "[in module %s]"), objfile_name (objfile));
21166
21167 return lookup_die_type (die, type_attr, cu);
21168 }
21169
21170 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21171
21172 static struct type *
21173 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21174 {
21175 struct dwarf2_per_objfile *dwarf2_per_objfile
21176 = cu->per_cu->dwarf2_per_objfile;
21177 struct objfile *objfile = dwarf2_per_objfile->objfile;
21178 char *saved;
21179
21180 std::string message
21181 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21182 objfile_name (objfile),
21183 sect_offset_str (cu->header.sect_off),
21184 sect_offset_str (die->sect_off));
21185 saved = obstack_strdup (&objfile->objfile_obstack, message);
21186
21187 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21188 }
21189
21190 /* Look up the type of DIE in CU using its type attribute ATTR.
21191 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21192 DW_AT_containing_type.
21193 If there is no type substitute an error marker. */
21194
21195 static struct type *
21196 lookup_die_type (struct die_info *die, const struct attribute *attr,
21197 struct dwarf2_cu *cu)
21198 {
21199 struct dwarf2_per_objfile *dwarf2_per_objfile
21200 = cu->per_cu->dwarf2_per_objfile;
21201 struct objfile *objfile = dwarf2_per_objfile->objfile;
21202 struct type *this_type;
21203
21204 gdb_assert (attr->name == DW_AT_type
21205 || attr->name == DW_AT_GNAT_descriptive_type
21206 || attr->name == DW_AT_containing_type);
21207
21208 /* First see if we have it cached. */
21209
21210 if (attr->form == DW_FORM_GNU_ref_alt)
21211 {
21212 struct dwarf2_per_cu_data *per_cu;
21213 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21214
21215 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21216 dwarf2_per_objfile);
21217 this_type = get_die_type_at_offset (sect_off, per_cu);
21218 }
21219 else if (attr->form_is_ref ())
21220 {
21221 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21222
21223 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21224 }
21225 else if (attr->form == DW_FORM_ref_sig8)
21226 {
21227 ULONGEST signature = DW_SIGNATURE (attr);
21228
21229 return get_signatured_type (die, signature, cu);
21230 }
21231 else
21232 {
21233 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21234 " at %s [in module %s]"),
21235 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21236 objfile_name (objfile));
21237 return build_error_marker_type (cu, die);
21238 }
21239
21240 /* If not cached we need to read it in. */
21241
21242 if (this_type == NULL)
21243 {
21244 struct die_info *type_die = NULL;
21245 struct dwarf2_cu *type_cu = cu;
21246
21247 if (attr->form_is_ref ())
21248 type_die = follow_die_ref (die, attr, &type_cu);
21249 if (type_die == NULL)
21250 return build_error_marker_type (cu, die);
21251 /* If we find the type now, it's probably because the type came
21252 from an inter-CU reference and the type's CU got expanded before
21253 ours. */
21254 this_type = read_type_die (type_die, type_cu);
21255 }
21256
21257 /* If we still don't have a type use an error marker. */
21258
21259 if (this_type == NULL)
21260 return build_error_marker_type (cu, die);
21261
21262 return this_type;
21263 }
21264
21265 /* Return the type in DIE, CU.
21266 Returns NULL for invalid types.
21267
21268 This first does a lookup in die_type_hash,
21269 and only reads the die in if necessary.
21270
21271 NOTE: This can be called when reading in partial or full symbols. */
21272
21273 static struct type *
21274 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21275 {
21276 struct type *this_type;
21277
21278 this_type = get_die_type (die, cu);
21279 if (this_type)
21280 return this_type;
21281
21282 return read_type_die_1 (die, cu);
21283 }
21284
21285 /* Read the type in DIE, CU.
21286 Returns NULL for invalid types. */
21287
21288 static struct type *
21289 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21290 {
21291 struct type *this_type = NULL;
21292
21293 switch (die->tag)
21294 {
21295 case DW_TAG_class_type:
21296 case DW_TAG_interface_type:
21297 case DW_TAG_structure_type:
21298 case DW_TAG_union_type:
21299 this_type = read_structure_type (die, cu);
21300 break;
21301 case DW_TAG_enumeration_type:
21302 this_type = read_enumeration_type (die, cu);
21303 break;
21304 case DW_TAG_subprogram:
21305 case DW_TAG_subroutine_type:
21306 case DW_TAG_inlined_subroutine:
21307 this_type = read_subroutine_type (die, cu);
21308 break;
21309 case DW_TAG_array_type:
21310 this_type = read_array_type (die, cu);
21311 break;
21312 case DW_TAG_set_type:
21313 this_type = read_set_type (die, cu);
21314 break;
21315 case DW_TAG_pointer_type:
21316 this_type = read_tag_pointer_type (die, cu);
21317 break;
21318 case DW_TAG_ptr_to_member_type:
21319 this_type = read_tag_ptr_to_member_type (die, cu);
21320 break;
21321 case DW_TAG_reference_type:
21322 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21323 break;
21324 case DW_TAG_rvalue_reference_type:
21325 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21326 break;
21327 case DW_TAG_const_type:
21328 this_type = read_tag_const_type (die, cu);
21329 break;
21330 case DW_TAG_volatile_type:
21331 this_type = read_tag_volatile_type (die, cu);
21332 break;
21333 case DW_TAG_restrict_type:
21334 this_type = read_tag_restrict_type (die, cu);
21335 break;
21336 case DW_TAG_string_type:
21337 this_type = read_tag_string_type (die, cu);
21338 break;
21339 case DW_TAG_typedef:
21340 this_type = read_typedef (die, cu);
21341 break;
21342 case DW_TAG_subrange_type:
21343 this_type = read_subrange_type (die, cu);
21344 break;
21345 case DW_TAG_base_type:
21346 this_type = read_base_type (die, cu);
21347 break;
21348 case DW_TAG_unspecified_type:
21349 this_type = read_unspecified_type (die, cu);
21350 break;
21351 case DW_TAG_namespace:
21352 this_type = read_namespace_type (die, cu);
21353 break;
21354 case DW_TAG_module:
21355 this_type = read_module_type (die, cu);
21356 break;
21357 case DW_TAG_atomic_type:
21358 this_type = read_tag_atomic_type (die, cu);
21359 break;
21360 default:
21361 complaint (_("unexpected tag in read_type_die: '%s'"),
21362 dwarf_tag_name (die->tag));
21363 break;
21364 }
21365
21366 return this_type;
21367 }
21368
21369 /* See if we can figure out if the class lives in a namespace. We do
21370 this by looking for a member function; its demangled name will
21371 contain namespace info, if there is any.
21372 Return the computed name or NULL.
21373 Space for the result is allocated on the objfile's obstack.
21374 This is the full-die version of guess_partial_die_structure_name.
21375 In this case we know DIE has no useful parent. */
21376
21377 static const char *
21378 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21379 {
21380 struct die_info *spec_die;
21381 struct dwarf2_cu *spec_cu;
21382 struct die_info *child;
21383 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21384
21385 spec_cu = cu;
21386 spec_die = die_specification (die, &spec_cu);
21387 if (spec_die != NULL)
21388 {
21389 die = spec_die;
21390 cu = spec_cu;
21391 }
21392
21393 for (child = die->child;
21394 child != NULL;
21395 child = child->sibling)
21396 {
21397 if (child->tag == DW_TAG_subprogram)
21398 {
21399 const char *linkage_name = dw2_linkage_name (child, cu);
21400
21401 if (linkage_name != NULL)
21402 {
21403 gdb::unique_xmalloc_ptr<char> actual_name
21404 (language_class_name_from_physname (cu->language_defn,
21405 linkage_name));
21406 const char *name = NULL;
21407
21408 if (actual_name != NULL)
21409 {
21410 const char *die_name = dwarf2_name (die, cu);
21411
21412 if (die_name != NULL
21413 && strcmp (die_name, actual_name.get ()) != 0)
21414 {
21415 /* Strip off the class name from the full name.
21416 We want the prefix. */
21417 int die_name_len = strlen (die_name);
21418 int actual_name_len = strlen (actual_name.get ());
21419 const char *ptr = actual_name.get ();
21420
21421 /* Test for '::' as a sanity check. */
21422 if (actual_name_len > die_name_len + 2
21423 && ptr[actual_name_len - die_name_len - 1] == ':')
21424 name = obstack_strndup (
21425 &objfile->per_bfd->storage_obstack,
21426 ptr, actual_name_len - die_name_len - 2);
21427 }
21428 }
21429 return name;
21430 }
21431 }
21432 }
21433
21434 return NULL;
21435 }
21436
21437 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21438 prefix part in such case. See
21439 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21440
21441 static const char *
21442 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21443 {
21444 struct attribute *attr;
21445 const char *base;
21446
21447 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21448 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21449 return NULL;
21450
21451 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21452 return NULL;
21453
21454 attr = dw2_linkage_name_attr (die, cu);
21455 if (attr == NULL || DW_STRING (attr) == NULL)
21456 return NULL;
21457
21458 /* dwarf2_name had to be already called. */
21459 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21460
21461 /* Strip the base name, keep any leading namespaces/classes. */
21462 base = strrchr (DW_STRING (attr), ':');
21463 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21464 return "";
21465
21466 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21467 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21468 DW_STRING (attr),
21469 &base[-1] - DW_STRING (attr));
21470 }
21471
21472 /* Return the name of the namespace/class that DIE is defined within,
21473 or "" if we can't tell. The caller should not xfree the result.
21474
21475 For example, if we're within the method foo() in the following
21476 code:
21477
21478 namespace N {
21479 class C {
21480 void foo () {
21481 }
21482 };
21483 }
21484
21485 then determine_prefix on foo's die will return "N::C". */
21486
21487 static const char *
21488 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21489 {
21490 struct dwarf2_per_objfile *dwarf2_per_objfile
21491 = cu->per_cu->dwarf2_per_objfile;
21492 struct die_info *parent, *spec_die;
21493 struct dwarf2_cu *spec_cu;
21494 struct type *parent_type;
21495 const char *retval;
21496
21497 if (cu->language != language_cplus
21498 && cu->language != language_fortran && cu->language != language_d
21499 && cu->language != language_rust)
21500 return "";
21501
21502 retval = anonymous_struct_prefix (die, cu);
21503 if (retval)
21504 return retval;
21505
21506 /* We have to be careful in the presence of DW_AT_specification.
21507 For example, with GCC 3.4, given the code
21508
21509 namespace N {
21510 void foo() {
21511 // Definition of N::foo.
21512 }
21513 }
21514
21515 then we'll have a tree of DIEs like this:
21516
21517 1: DW_TAG_compile_unit
21518 2: DW_TAG_namespace // N
21519 3: DW_TAG_subprogram // declaration of N::foo
21520 4: DW_TAG_subprogram // definition of N::foo
21521 DW_AT_specification // refers to die #3
21522
21523 Thus, when processing die #4, we have to pretend that we're in
21524 the context of its DW_AT_specification, namely the contex of die
21525 #3. */
21526 spec_cu = cu;
21527 spec_die = die_specification (die, &spec_cu);
21528 if (spec_die == NULL)
21529 parent = die->parent;
21530 else
21531 {
21532 parent = spec_die->parent;
21533 cu = spec_cu;
21534 }
21535
21536 if (parent == NULL)
21537 return "";
21538 else if (parent->building_fullname)
21539 {
21540 const char *name;
21541 const char *parent_name;
21542
21543 /* It has been seen on RealView 2.2 built binaries,
21544 DW_TAG_template_type_param types actually _defined_ as
21545 children of the parent class:
21546
21547 enum E {};
21548 template class <class Enum> Class{};
21549 Class<enum E> class_e;
21550
21551 1: DW_TAG_class_type (Class)
21552 2: DW_TAG_enumeration_type (E)
21553 3: DW_TAG_enumerator (enum1:0)
21554 3: DW_TAG_enumerator (enum2:1)
21555 ...
21556 2: DW_TAG_template_type_param
21557 DW_AT_type DW_FORM_ref_udata (E)
21558
21559 Besides being broken debug info, it can put GDB into an
21560 infinite loop. Consider:
21561
21562 When we're building the full name for Class<E>, we'll start
21563 at Class, and go look over its template type parameters,
21564 finding E. We'll then try to build the full name of E, and
21565 reach here. We're now trying to build the full name of E,
21566 and look over the parent DIE for containing scope. In the
21567 broken case, if we followed the parent DIE of E, we'd again
21568 find Class, and once again go look at its template type
21569 arguments, etc., etc. Simply don't consider such parent die
21570 as source-level parent of this die (it can't be, the language
21571 doesn't allow it), and break the loop here. */
21572 name = dwarf2_name (die, cu);
21573 parent_name = dwarf2_name (parent, cu);
21574 complaint (_("template param type '%s' defined within parent '%s'"),
21575 name ? name : "<unknown>",
21576 parent_name ? parent_name : "<unknown>");
21577 return "";
21578 }
21579 else
21580 switch (parent->tag)
21581 {
21582 case DW_TAG_namespace:
21583 parent_type = read_type_die (parent, cu);
21584 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21585 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21586 Work around this problem here. */
21587 if (cu->language == language_cplus
21588 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21589 return "";
21590 /* We give a name to even anonymous namespaces. */
21591 return TYPE_NAME (parent_type);
21592 case DW_TAG_class_type:
21593 case DW_TAG_interface_type:
21594 case DW_TAG_structure_type:
21595 case DW_TAG_union_type:
21596 case DW_TAG_module:
21597 parent_type = read_type_die (parent, cu);
21598 if (TYPE_NAME (parent_type) != NULL)
21599 return TYPE_NAME (parent_type);
21600 else
21601 /* An anonymous structure is only allowed non-static data
21602 members; no typedefs, no member functions, et cetera.
21603 So it does not need a prefix. */
21604 return "";
21605 case DW_TAG_compile_unit:
21606 case DW_TAG_partial_unit:
21607 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21608 if (cu->language == language_cplus
21609 && !dwarf2_per_objfile->types.empty ()
21610 && die->child != NULL
21611 && (die->tag == DW_TAG_class_type
21612 || die->tag == DW_TAG_structure_type
21613 || die->tag == DW_TAG_union_type))
21614 {
21615 const char *name = guess_full_die_structure_name (die, cu);
21616 if (name != NULL)
21617 return name;
21618 }
21619 return "";
21620 case DW_TAG_subprogram:
21621 /* Nested subroutines in Fortran get a prefix with the name
21622 of the parent's subroutine. */
21623 if (cu->language == language_fortran)
21624 {
21625 if ((die->tag == DW_TAG_subprogram)
21626 && (dwarf2_name (parent, cu) != NULL))
21627 return dwarf2_name (parent, cu);
21628 }
21629 return determine_prefix (parent, cu);
21630 case DW_TAG_enumeration_type:
21631 parent_type = read_type_die (parent, cu);
21632 if (TYPE_DECLARED_CLASS (parent_type))
21633 {
21634 if (TYPE_NAME (parent_type) != NULL)
21635 return TYPE_NAME (parent_type);
21636 return "";
21637 }
21638 /* Fall through. */
21639 default:
21640 return determine_prefix (parent, cu);
21641 }
21642 }
21643
21644 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21645 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21646 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21647 an obconcat, otherwise allocate storage for the result. The CU argument is
21648 used to determine the language and hence, the appropriate separator. */
21649
21650 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21651
21652 static char *
21653 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21654 int physname, struct dwarf2_cu *cu)
21655 {
21656 const char *lead = "";
21657 const char *sep;
21658
21659 if (suffix == NULL || suffix[0] == '\0'
21660 || prefix == NULL || prefix[0] == '\0')
21661 sep = "";
21662 else if (cu->language == language_d)
21663 {
21664 /* For D, the 'main' function could be defined in any module, but it
21665 should never be prefixed. */
21666 if (strcmp (suffix, "D main") == 0)
21667 {
21668 prefix = "";
21669 sep = "";
21670 }
21671 else
21672 sep = ".";
21673 }
21674 else if (cu->language == language_fortran && physname)
21675 {
21676 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21677 DW_AT_MIPS_linkage_name is preferred and used instead. */
21678
21679 lead = "__";
21680 sep = "_MOD_";
21681 }
21682 else
21683 sep = "::";
21684
21685 if (prefix == NULL)
21686 prefix = "";
21687 if (suffix == NULL)
21688 suffix = "";
21689
21690 if (obs == NULL)
21691 {
21692 char *retval
21693 = ((char *)
21694 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21695
21696 strcpy (retval, lead);
21697 strcat (retval, prefix);
21698 strcat (retval, sep);
21699 strcat (retval, suffix);
21700 return retval;
21701 }
21702 else
21703 {
21704 /* We have an obstack. */
21705 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21706 }
21707 }
21708
21709 /* Return sibling of die, NULL if no sibling. */
21710
21711 static struct die_info *
21712 sibling_die (struct die_info *die)
21713 {
21714 return die->sibling;
21715 }
21716
21717 /* Get name of a die, return NULL if not found. */
21718
21719 static const char *
21720 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21721 struct objfile *objfile)
21722 {
21723 if (name && cu->language == language_cplus)
21724 {
21725 std::string canon_name = cp_canonicalize_string (name);
21726
21727 if (!canon_name.empty ())
21728 {
21729 if (canon_name != name)
21730 name = objfile->intern (canon_name);
21731 }
21732 }
21733
21734 return name;
21735 }
21736
21737 /* Get name of a die, return NULL if not found.
21738 Anonymous namespaces are converted to their magic string. */
21739
21740 static const char *
21741 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21742 {
21743 struct attribute *attr;
21744 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21745
21746 attr = dwarf2_attr (die, DW_AT_name, cu);
21747 if ((!attr || !DW_STRING (attr))
21748 && die->tag != DW_TAG_namespace
21749 && die->tag != DW_TAG_class_type
21750 && die->tag != DW_TAG_interface_type
21751 && die->tag != DW_TAG_structure_type
21752 && die->tag != DW_TAG_union_type)
21753 return NULL;
21754
21755 switch (die->tag)
21756 {
21757 case DW_TAG_compile_unit:
21758 case DW_TAG_partial_unit:
21759 /* Compilation units have a DW_AT_name that is a filename, not
21760 a source language identifier. */
21761 case DW_TAG_enumeration_type:
21762 case DW_TAG_enumerator:
21763 /* These tags always have simple identifiers already; no need
21764 to canonicalize them. */
21765 return DW_STRING (attr);
21766
21767 case DW_TAG_namespace:
21768 if (attr != NULL && DW_STRING (attr) != NULL)
21769 return DW_STRING (attr);
21770 return CP_ANONYMOUS_NAMESPACE_STR;
21771
21772 case DW_TAG_class_type:
21773 case DW_TAG_interface_type:
21774 case DW_TAG_structure_type:
21775 case DW_TAG_union_type:
21776 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21777 structures or unions. These were of the form "._%d" in GCC 4.1,
21778 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21779 and GCC 4.4. We work around this problem by ignoring these. */
21780 if (attr && DW_STRING (attr)
21781 && (startswith (DW_STRING (attr), "._")
21782 || startswith (DW_STRING (attr), "<anonymous")))
21783 return NULL;
21784
21785 /* GCC might emit a nameless typedef that has a linkage name. See
21786 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21787 if (!attr || DW_STRING (attr) == NULL)
21788 {
21789 attr = dw2_linkage_name_attr (die, cu);
21790 if (attr == NULL || DW_STRING (attr) == NULL)
21791 return NULL;
21792
21793 /* Avoid demangling DW_STRING (attr) the second time on a second
21794 call for the same DIE. */
21795 if (!DW_STRING_IS_CANONICAL (attr))
21796 {
21797 gdb::unique_xmalloc_ptr<char> demangled
21798 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21799 if (demangled == nullptr)
21800 return nullptr;
21801
21802 const char *base;
21803
21804 DW_STRING (attr) = objfile->intern (demangled.get ());
21805 DW_STRING_IS_CANONICAL (attr) = 1;
21806
21807 /* Strip any leading namespaces/classes, keep only the base name.
21808 DW_AT_name for named DIEs does not contain the prefixes. */
21809 base = strrchr (DW_STRING (attr), ':');
21810 if (base && base > DW_STRING (attr) && base[-1] == ':')
21811 return &base[1];
21812 else
21813 return DW_STRING (attr);
21814 }
21815 }
21816 break;
21817
21818 default:
21819 break;
21820 }
21821
21822 if (!DW_STRING_IS_CANONICAL (attr))
21823 {
21824 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21825 objfile);
21826 DW_STRING_IS_CANONICAL (attr) = 1;
21827 }
21828 return DW_STRING (attr);
21829 }
21830
21831 /* Return the die that this die in an extension of, or NULL if there
21832 is none. *EXT_CU is the CU containing DIE on input, and the CU
21833 containing the return value on output. */
21834
21835 static struct die_info *
21836 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21837 {
21838 struct attribute *attr;
21839
21840 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21841 if (attr == NULL)
21842 return NULL;
21843
21844 return follow_die_ref (die, attr, ext_cu);
21845 }
21846
21847 /* A convenience function that returns an "unknown" DWARF name,
21848 including the value of V. STR is the name of the entity being
21849 printed, e.g., "TAG". */
21850
21851 static const char *
21852 dwarf_unknown (const char *str, unsigned v)
21853 {
21854 char *cell = get_print_cell ();
21855 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21856 return cell;
21857 }
21858
21859 /* Convert a DIE tag into its string name. */
21860
21861 static const char *
21862 dwarf_tag_name (unsigned tag)
21863 {
21864 const char *name = get_DW_TAG_name (tag);
21865
21866 if (name == NULL)
21867 return dwarf_unknown ("TAG", tag);
21868
21869 return name;
21870 }
21871
21872 /* Convert a DWARF attribute code into its string name. */
21873
21874 static const char *
21875 dwarf_attr_name (unsigned attr)
21876 {
21877 const char *name;
21878
21879 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21880 if (attr == DW_AT_MIPS_fde)
21881 return "DW_AT_MIPS_fde";
21882 #else
21883 if (attr == DW_AT_HP_block_index)
21884 return "DW_AT_HP_block_index";
21885 #endif
21886
21887 name = get_DW_AT_name (attr);
21888
21889 if (name == NULL)
21890 return dwarf_unknown ("AT", attr);
21891
21892 return name;
21893 }
21894
21895 /* Convert a DWARF value form code into its string name. */
21896
21897 static const char *
21898 dwarf_form_name (unsigned form)
21899 {
21900 const char *name = get_DW_FORM_name (form);
21901
21902 if (name == NULL)
21903 return dwarf_unknown ("FORM", form);
21904
21905 return name;
21906 }
21907
21908 static const char *
21909 dwarf_bool_name (unsigned mybool)
21910 {
21911 if (mybool)
21912 return "TRUE";
21913 else
21914 return "FALSE";
21915 }
21916
21917 /* Convert a DWARF type code into its string name. */
21918
21919 static const char *
21920 dwarf_type_encoding_name (unsigned enc)
21921 {
21922 const char *name = get_DW_ATE_name (enc);
21923
21924 if (name == NULL)
21925 return dwarf_unknown ("ATE", enc);
21926
21927 return name;
21928 }
21929
21930 static void
21931 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21932 {
21933 unsigned int i;
21934
21935 print_spaces (indent, f);
21936 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21937 dwarf_tag_name (die->tag), die->abbrev,
21938 sect_offset_str (die->sect_off));
21939
21940 if (die->parent != NULL)
21941 {
21942 print_spaces (indent, f);
21943 fprintf_unfiltered (f, " parent at offset: %s\n",
21944 sect_offset_str (die->parent->sect_off));
21945 }
21946
21947 print_spaces (indent, f);
21948 fprintf_unfiltered (f, " has children: %s\n",
21949 dwarf_bool_name (die->child != NULL));
21950
21951 print_spaces (indent, f);
21952 fprintf_unfiltered (f, " attributes:\n");
21953
21954 for (i = 0; i < die->num_attrs; ++i)
21955 {
21956 print_spaces (indent, f);
21957 fprintf_unfiltered (f, " %s (%s) ",
21958 dwarf_attr_name (die->attrs[i].name),
21959 dwarf_form_name (die->attrs[i].form));
21960
21961 switch (die->attrs[i].form)
21962 {
21963 case DW_FORM_addr:
21964 case DW_FORM_addrx:
21965 case DW_FORM_GNU_addr_index:
21966 fprintf_unfiltered (f, "address: ");
21967 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21968 break;
21969 case DW_FORM_block2:
21970 case DW_FORM_block4:
21971 case DW_FORM_block:
21972 case DW_FORM_block1:
21973 fprintf_unfiltered (f, "block: size %s",
21974 pulongest (DW_BLOCK (&die->attrs[i])->size));
21975 break;
21976 case DW_FORM_exprloc:
21977 fprintf_unfiltered (f, "expression: size %s",
21978 pulongest (DW_BLOCK (&die->attrs[i])->size));
21979 break;
21980 case DW_FORM_data16:
21981 fprintf_unfiltered (f, "constant of 16 bytes");
21982 break;
21983 case DW_FORM_ref_addr:
21984 fprintf_unfiltered (f, "ref address: ");
21985 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21986 break;
21987 case DW_FORM_GNU_ref_alt:
21988 fprintf_unfiltered (f, "alt ref address: ");
21989 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21990 break;
21991 case DW_FORM_ref1:
21992 case DW_FORM_ref2:
21993 case DW_FORM_ref4:
21994 case DW_FORM_ref8:
21995 case DW_FORM_ref_udata:
21996 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21997 (long) (DW_UNSND (&die->attrs[i])));
21998 break;
21999 case DW_FORM_data1:
22000 case DW_FORM_data2:
22001 case DW_FORM_data4:
22002 case DW_FORM_data8:
22003 case DW_FORM_udata:
22004 case DW_FORM_sdata:
22005 fprintf_unfiltered (f, "constant: %s",
22006 pulongest (DW_UNSND (&die->attrs[i])));
22007 break;
22008 case DW_FORM_sec_offset:
22009 fprintf_unfiltered (f, "section offset: %s",
22010 pulongest (DW_UNSND (&die->attrs[i])));
22011 break;
22012 case DW_FORM_ref_sig8:
22013 fprintf_unfiltered (f, "signature: %s",
22014 hex_string (DW_SIGNATURE (&die->attrs[i])));
22015 break;
22016 case DW_FORM_string:
22017 case DW_FORM_strp:
22018 case DW_FORM_line_strp:
22019 case DW_FORM_strx:
22020 case DW_FORM_GNU_str_index:
22021 case DW_FORM_GNU_strp_alt:
22022 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22023 DW_STRING (&die->attrs[i])
22024 ? DW_STRING (&die->attrs[i]) : "",
22025 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22026 break;
22027 case DW_FORM_flag:
22028 if (DW_UNSND (&die->attrs[i]))
22029 fprintf_unfiltered (f, "flag: TRUE");
22030 else
22031 fprintf_unfiltered (f, "flag: FALSE");
22032 break;
22033 case DW_FORM_flag_present:
22034 fprintf_unfiltered (f, "flag: TRUE");
22035 break;
22036 case DW_FORM_indirect:
22037 /* The reader will have reduced the indirect form to
22038 the "base form" so this form should not occur. */
22039 fprintf_unfiltered (f,
22040 "unexpected attribute form: DW_FORM_indirect");
22041 break;
22042 case DW_FORM_implicit_const:
22043 fprintf_unfiltered (f, "constant: %s",
22044 plongest (DW_SND (&die->attrs[i])));
22045 break;
22046 default:
22047 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22048 die->attrs[i].form);
22049 break;
22050 }
22051 fprintf_unfiltered (f, "\n");
22052 }
22053 }
22054
22055 static void
22056 dump_die_for_error (struct die_info *die)
22057 {
22058 dump_die_shallow (gdb_stderr, 0, die);
22059 }
22060
22061 static void
22062 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22063 {
22064 int indent = level * 4;
22065
22066 gdb_assert (die != NULL);
22067
22068 if (level >= max_level)
22069 return;
22070
22071 dump_die_shallow (f, indent, die);
22072
22073 if (die->child != NULL)
22074 {
22075 print_spaces (indent, f);
22076 fprintf_unfiltered (f, " Children:");
22077 if (level + 1 < max_level)
22078 {
22079 fprintf_unfiltered (f, "\n");
22080 dump_die_1 (f, level + 1, max_level, die->child);
22081 }
22082 else
22083 {
22084 fprintf_unfiltered (f,
22085 " [not printed, max nesting level reached]\n");
22086 }
22087 }
22088
22089 if (die->sibling != NULL && level > 0)
22090 {
22091 dump_die_1 (f, level, max_level, die->sibling);
22092 }
22093 }
22094
22095 /* This is called from the pdie macro in gdbinit.in.
22096 It's not static so gcc will keep a copy callable from gdb. */
22097
22098 void
22099 dump_die (struct die_info *die, int max_level)
22100 {
22101 dump_die_1 (gdb_stdlog, 0, max_level, die);
22102 }
22103
22104 static void
22105 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22106 {
22107 void **slot;
22108
22109 slot = htab_find_slot_with_hash (cu->die_hash, die,
22110 to_underlying (die->sect_off),
22111 INSERT);
22112
22113 *slot = die;
22114 }
22115
22116 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22117 required kind. */
22118
22119 static sect_offset
22120 dwarf2_get_ref_die_offset (const struct attribute *attr)
22121 {
22122 if (attr->form_is_ref ())
22123 return (sect_offset) DW_UNSND (attr);
22124
22125 complaint (_("unsupported die ref attribute form: '%s'"),
22126 dwarf_form_name (attr->form));
22127 return {};
22128 }
22129
22130 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22131 * the value held by the attribute is not constant. */
22132
22133 static LONGEST
22134 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22135 {
22136 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22137 return DW_SND (attr);
22138 else if (attr->form == DW_FORM_udata
22139 || attr->form == DW_FORM_data1
22140 || attr->form == DW_FORM_data2
22141 || attr->form == DW_FORM_data4
22142 || attr->form == DW_FORM_data8)
22143 return DW_UNSND (attr);
22144 else
22145 {
22146 /* For DW_FORM_data16 see attribute::form_is_constant. */
22147 complaint (_("Attribute value is not a constant (%s)"),
22148 dwarf_form_name (attr->form));
22149 return default_value;
22150 }
22151 }
22152
22153 /* Follow reference or signature attribute ATTR of SRC_DIE.
22154 On entry *REF_CU is the CU of SRC_DIE.
22155 On exit *REF_CU is the CU of the result. */
22156
22157 static struct die_info *
22158 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22159 struct dwarf2_cu **ref_cu)
22160 {
22161 struct die_info *die;
22162
22163 if (attr->form_is_ref ())
22164 die = follow_die_ref (src_die, attr, ref_cu);
22165 else if (attr->form == DW_FORM_ref_sig8)
22166 die = follow_die_sig (src_die, attr, ref_cu);
22167 else
22168 {
22169 dump_die_for_error (src_die);
22170 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22171 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22172 }
22173
22174 return die;
22175 }
22176
22177 /* Follow reference OFFSET.
22178 On entry *REF_CU is the CU of the source die referencing OFFSET.
22179 On exit *REF_CU is the CU of the result.
22180 Returns NULL if OFFSET is invalid. */
22181
22182 static struct die_info *
22183 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22184 struct dwarf2_cu **ref_cu)
22185 {
22186 struct die_info temp_die;
22187 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22188 struct dwarf2_per_objfile *dwarf2_per_objfile
22189 = cu->per_cu->dwarf2_per_objfile;
22190
22191 gdb_assert (cu->per_cu != NULL);
22192
22193 target_cu = cu;
22194
22195 if (cu->per_cu->is_debug_types)
22196 {
22197 /* .debug_types CUs cannot reference anything outside their CU.
22198 If they need to, they have to reference a signatured type via
22199 DW_FORM_ref_sig8. */
22200 if (!cu->header.offset_in_cu_p (sect_off))
22201 return NULL;
22202 }
22203 else if (offset_in_dwz != cu->per_cu->is_dwz
22204 || !cu->header.offset_in_cu_p (sect_off))
22205 {
22206 struct dwarf2_per_cu_data *per_cu;
22207
22208 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22209 dwarf2_per_objfile);
22210
22211 /* If necessary, add it to the queue and load its DIEs. */
22212 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22213 load_full_comp_unit (per_cu, false, cu->language);
22214
22215 target_cu = per_cu->cu;
22216 }
22217 else if (cu->dies == NULL)
22218 {
22219 /* We're loading full DIEs during partial symbol reading. */
22220 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22221 load_full_comp_unit (cu->per_cu, false, language_minimal);
22222 }
22223
22224 *ref_cu = target_cu;
22225 temp_die.sect_off = sect_off;
22226
22227 if (target_cu != cu)
22228 target_cu->ancestor = cu;
22229
22230 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22231 &temp_die,
22232 to_underlying (sect_off));
22233 }
22234
22235 /* Follow reference attribute ATTR of SRC_DIE.
22236 On entry *REF_CU is the CU of SRC_DIE.
22237 On exit *REF_CU is the CU of the result. */
22238
22239 static struct die_info *
22240 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22241 struct dwarf2_cu **ref_cu)
22242 {
22243 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22244 struct dwarf2_cu *cu = *ref_cu;
22245 struct die_info *die;
22246
22247 die = follow_die_offset (sect_off,
22248 (attr->form == DW_FORM_GNU_ref_alt
22249 || cu->per_cu->is_dwz),
22250 ref_cu);
22251 if (!die)
22252 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22253 "at %s [in module %s]"),
22254 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22255 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22256
22257 return die;
22258 }
22259
22260 /* See read.h. */
22261
22262 struct dwarf2_locexpr_baton
22263 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22264 dwarf2_per_cu_data *per_cu,
22265 CORE_ADDR (*get_frame_pc) (void *baton),
22266 void *baton, bool resolve_abstract_p)
22267 {
22268 struct dwarf2_cu *cu;
22269 struct die_info *die;
22270 struct attribute *attr;
22271 struct dwarf2_locexpr_baton retval;
22272 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22273 struct objfile *objfile = dwarf2_per_objfile->objfile;
22274
22275 if (per_cu->cu == NULL)
22276 load_cu (per_cu, false);
22277 cu = per_cu->cu;
22278 if (cu == NULL)
22279 {
22280 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22281 Instead just throw an error, not much else we can do. */
22282 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22283 sect_offset_str (sect_off), objfile_name (objfile));
22284 }
22285
22286 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22287 if (!die)
22288 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22289 sect_offset_str (sect_off), objfile_name (objfile));
22290
22291 attr = dwarf2_attr (die, DW_AT_location, cu);
22292 if (!attr && resolve_abstract_p
22293 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22294 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22295 {
22296 CORE_ADDR pc = (*get_frame_pc) (baton);
22297 CORE_ADDR baseaddr = objfile->text_section_offset ();
22298 struct gdbarch *gdbarch = get_objfile_arch (objfile);
22299
22300 for (const auto &cand_off
22301 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22302 {
22303 struct dwarf2_cu *cand_cu = cu;
22304 struct die_info *cand
22305 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22306 if (!cand
22307 || !cand->parent
22308 || cand->parent->tag != DW_TAG_subprogram)
22309 continue;
22310
22311 CORE_ADDR pc_low, pc_high;
22312 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22313 if (pc_low == ((CORE_ADDR) -1))
22314 continue;
22315 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22316 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22317 if (!(pc_low <= pc && pc < pc_high))
22318 continue;
22319
22320 die = cand;
22321 attr = dwarf2_attr (die, DW_AT_location, cu);
22322 break;
22323 }
22324 }
22325
22326 if (!attr)
22327 {
22328 /* DWARF: "If there is no such attribute, then there is no effect.".
22329 DATA is ignored if SIZE is 0. */
22330
22331 retval.data = NULL;
22332 retval.size = 0;
22333 }
22334 else if (attr->form_is_section_offset ())
22335 {
22336 struct dwarf2_loclist_baton loclist_baton;
22337 CORE_ADDR pc = (*get_frame_pc) (baton);
22338 size_t size;
22339
22340 fill_in_loclist_baton (cu, &loclist_baton, attr);
22341
22342 retval.data = dwarf2_find_location_expression (&loclist_baton,
22343 &size, pc);
22344 retval.size = size;
22345 }
22346 else
22347 {
22348 if (!attr->form_is_block ())
22349 error (_("Dwarf Error: DIE at %s referenced in module %s "
22350 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22351 sect_offset_str (sect_off), objfile_name (objfile));
22352
22353 retval.data = DW_BLOCK (attr)->data;
22354 retval.size = DW_BLOCK (attr)->size;
22355 }
22356 retval.per_cu = cu->per_cu;
22357
22358 age_cached_comp_units (dwarf2_per_objfile);
22359
22360 return retval;
22361 }
22362
22363 /* See read.h. */
22364
22365 struct dwarf2_locexpr_baton
22366 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22367 dwarf2_per_cu_data *per_cu,
22368 CORE_ADDR (*get_frame_pc) (void *baton),
22369 void *baton)
22370 {
22371 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22372
22373 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22374 }
22375
22376 /* Write a constant of a given type as target-ordered bytes into
22377 OBSTACK. */
22378
22379 static const gdb_byte *
22380 write_constant_as_bytes (struct obstack *obstack,
22381 enum bfd_endian byte_order,
22382 struct type *type,
22383 ULONGEST value,
22384 LONGEST *len)
22385 {
22386 gdb_byte *result;
22387
22388 *len = TYPE_LENGTH (type);
22389 result = (gdb_byte *) obstack_alloc (obstack, *len);
22390 store_unsigned_integer (result, *len, byte_order, value);
22391
22392 return result;
22393 }
22394
22395 /* See read.h. */
22396
22397 const gdb_byte *
22398 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22399 dwarf2_per_cu_data *per_cu,
22400 obstack *obstack,
22401 LONGEST *len)
22402 {
22403 struct dwarf2_cu *cu;
22404 struct die_info *die;
22405 struct attribute *attr;
22406 const gdb_byte *result = NULL;
22407 struct type *type;
22408 LONGEST value;
22409 enum bfd_endian byte_order;
22410 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22411
22412 if (per_cu->cu == NULL)
22413 load_cu (per_cu, false);
22414 cu = per_cu->cu;
22415 if (cu == NULL)
22416 {
22417 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22418 Instead just throw an error, not much else we can do. */
22419 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22420 sect_offset_str (sect_off), objfile_name (objfile));
22421 }
22422
22423 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22424 if (!die)
22425 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22426 sect_offset_str (sect_off), objfile_name (objfile));
22427
22428 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22429 if (attr == NULL)
22430 return NULL;
22431
22432 byte_order = (bfd_big_endian (objfile->obfd)
22433 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22434
22435 switch (attr->form)
22436 {
22437 case DW_FORM_addr:
22438 case DW_FORM_addrx:
22439 case DW_FORM_GNU_addr_index:
22440 {
22441 gdb_byte *tem;
22442
22443 *len = cu->header.addr_size;
22444 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22445 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22446 result = tem;
22447 }
22448 break;
22449 case DW_FORM_string:
22450 case DW_FORM_strp:
22451 case DW_FORM_strx:
22452 case DW_FORM_GNU_str_index:
22453 case DW_FORM_GNU_strp_alt:
22454 /* DW_STRING is already allocated on the objfile obstack, point
22455 directly to it. */
22456 result = (const gdb_byte *) DW_STRING (attr);
22457 *len = strlen (DW_STRING (attr));
22458 break;
22459 case DW_FORM_block1:
22460 case DW_FORM_block2:
22461 case DW_FORM_block4:
22462 case DW_FORM_block:
22463 case DW_FORM_exprloc:
22464 case DW_FORM_data16:
22465 result = DW_BLOCK (attr)->data;
22466 *len = DW_BLOCK (attr)->size;
22467 break;
22468
22469 /* The DW_AT_const_value attributes are supposed to carry the
22470 symbol's value "represented as it would be on the target
22471 architecture." By the time we get here, it's already been
22472 converted to host endianness, so we just need to sign- or
22473 zero-extend it as appropriate. */
22474 case DW_FORM_data1:
22475 type = die_type (die, cu);
22476 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22477 if (result == NULL)
22478 result = write_constant_as_bytes (obstack, byte_order,
22479 type, value, len);
22480 break;
22481 case DW_FORM_data2:
22482 type = die_type (die, cu);
22483 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22484 if (result == NULL)
22485 result = write_constant_as_bytes (obstack, byte_order,
22486 type, value, len);
22487 break;
22488 case DW_FORM_data4:
22489 type = die_type (die, cu);
22490 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22491 if (result == NULL)
22492 result = write_constant_as_bytes (obstack, byte_order,
22493 type, value, len);
22494 break;
22495 case DW_FORM_data8:
22496 type = die_type (die, cu);
22497 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22498 if (result == NULL)
22499 result = write_constant_as_bytes (obstack, byte_order,
22500 type, value, len);
22501 break;
22502
22503 case DW_FORM_sdata:
22504 case DW_FORM_implicit_const:
22505 type = die_type (die, cu);
22506 result = write_constant_as_bytes (obstack, byte_order,
22507 type, DW_SND (attr), len);
22508 break;
22509
22510 case DW_FORM_udata:
22511 type = die_type (die, cu);
22512 result = write_constant_as_bytes (obstack, byte_order,
22513 type, DW_UNSND (attr), len);
22514 break;
22515
22516 default:
22517 complaint (_("unsupported const value attribute form: '%s'"),
22518 dwarf_form_name (attr->form));
22519 break;
22520 }
22521
22522 return result;
22523 }
22524
22525 /* See read.h. */
22526
22527 struct type *
22528 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22529 dwarf2_per_cu_data *per_cu)
22530 {
22531 struct dwarf2_cu *cu;
22532 struct die_info *die;
22533
22534 if (per_cu->cu == NULL)
22535 load_cu (per_cu, false);
22536 cu = per_cu->cu;
22537 if (!cu)
22538 return NULL;
22539
22540 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22541 if (!die)
22542 return NULL;
22543
22544 return die_type (die, cu);
22545 }
22546
22547 /* See read.h. */
22548
22549 struct type *
22550 dwarf2_get_die_type (cu_offset die_offset,
22551 struct dwarf2_per_cu_data *per_cu)
22552 {
22553 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22554 return get_die_type_at_offset (die_offset_sect, per_cu);
22555 }
22556
22557 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22558 On entry *REF_CU is the CU of SRC_DIE.
22559 On exit *REF_CU is the CU of the result.
22560 Returns NULL if the referenced DIE isn't found. */
22561
22562 static struct die_info *
22563 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22564 struct dwarf2_cu **ref_cu)
22565 {
22566 struct die_info temp_die;
22567 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22568 struct die_info *die;
22569
22570 /* While it might be nice to assert sig_type->type == NULL here,
22571 we can get here for DW_AT_imported_declaration where we need
22572 the DIE not the type. */
22573
22574 /* If necessary, add it to the queue and load its DIEs. */
22575
22576 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22577 read_signatured_type (sig_type);
22578
22579 sig_cu = sig_type->per_cu.cu;
22580 gdb_assert (sig_cu != NULL);
22581 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22582 temp_die.sect_off = sig_type->type_offset_in_section;
22583 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22584 to_underlying (temp_die.sect_off));
22585 if (die)
22586 {
22587 struct dwarf2_per_objfile *dwarf2_per_objfile
22588 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22589
22590 /* For .gdb_index version 7 keep track of included TUs.
22591 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22592 if (dwarf2_per_objfile->index_table != NULL
22593 && dwarf2_per_objfile->index_table->version <= 7)
22594 {
22595 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22596 }
22597
22598 *ref_cu = sig_cu;
22599 if (sig_cu != cu)
22600 sig_cu->ancestor = cu;
22601
22602 return die;
22603 }
22604
22605 return NULL;
22606 }
22607
22608 /* Follow signatured type referenced by ATTR in SRC_DIE.
22609 On entry *REF_CU is the CU of SRC_DIE.
22610 On exit *REF_CU is the CU of the result.
22611 The result is the DIE of the type.
22612 If the referenced type cannot be found an error is thrown. */
22613
22614 static struct die_info *
22615 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22616 struct dwarf2_cu **ref_cu)
22617 {
22618 ULONGEST signature = DW_SIGNATURE (attr);
22619 struct signatured_type *sig_type;
22620 struct die_info *die;
22621
22622 gdb_assert (attr->form == DW_FORM_ref_sig8);
22623
22624 sig_type = lookup_signatured_type (*ref_cu, signature);
22625 /* sig_type will be NULL if the signatured type is missing from
22626 the debug info. */
22627 if (sig_type == NULL)
22628 {
22629 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22630 " from DIE at %s [in module %s]"),
22631 hex_string (signature), sect_offset_str (src_die->sect_off),
22632 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22633 }
22634
22635 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22636 if (die == NULL)
22637 {
22638 dump_die_for_error (src_die);
22639 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22640 " from DIE at %s [in module %s]"),
22641 hex_string (signature), sect_offset_str (src_die->sect_off),
22642 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22643 }
22644
22645 return die;
22646 }
22647
22648 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22649 reading in and processing the type unit if necessary. */
22650
22651 static struct type *
22652 get_signatured_type (struct die_info *die, ULONGEST signature,
22653 struct dwarf2_cu *cu)
22654 {
22655 struct dwarf2_per_objfile *dwarf2_per_objfile
22656 = cu->per_cu->dwarf2_per_objfile;
22657 struct signatured_type *sig_type;
22658 struct dwarf2_cu *type_cu;
22659 struct die_info *type_die;
22660 struct type *type;
22661
22662 sig_type = lookup_signatured_type (cu, signature);
22663 /* sig_type will be NULL if the signatured type is missing from
22664 the debug info. */
22665 if (sig_type == NULL)
22666 {
22667 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22668 " from DIE at %s [in module %s]"),
22669 hex_string (signature), sect_offset_str (die->sect_off),
22670 objfile_name (dwarf2_per_objfile->objfile));
22671 return build_error_marker_type (cu, die);
22672 }
22673
22674 /* If we already know the type we're done. */
22675 if (sig_type->type != NULL)
22676 return sig_type->type;
22677
22678 type_cu = cu;
22679 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22680 if (type_die != NULL)
22681 {
22682 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22683 is created. This is important, for example, because for c++ classes
22684 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22685 type = read_type_die (type_die, type_cu);
22686 if (type == NULL)
22687 {
22688 complaint (_("Dwarf Error: Cannot build signatured type %s"
22689 " referenced from DIE at %s [in module %s]"),
22690 hex_string (signature), sect_offset_str (die->sect_off),
22691 objfile_name (dwarf2_per_objfile->objfile));
22692 type = build_error_marker_type (cu, die);
22693 }
22694 }
22695 else
22696 {
22697 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22698 " from DIE at %s [in module %s]"),
22699 hex_string (signature), sect_offset_str (die->sect_off),
22700 objfile_name (dwarf2_per_objfile->objfile));
22701 type = build_error_marker_type (cu, die);
22702 }
22703 sig_type->type = type;
22704
22705 return type;
22706 }
22707
22708 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22709 reading in and processing the type unit if necessary. */
22710
22711 static struct type *
22712 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22713 struct dwarf2_cu *cu) /* ARI: editCase function */
22714 {
22715 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22716 if (attr->form_is_ref ())
22717 {
22718 struct dwarf2_cu *type_cu = cu;
22719 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22720
22721 return read_type_die (type_die, type_cu);
22722 }
22723 else if (attr->form == DW_FORM_ref_sig8)
22724 {
22725 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22726 }
22727 else
22728 {
22729 struct dwarf2_per_objfile *dwarf2_per_objfile
22730 = cu->per_cu->dwarf2_per_objfile;
22731
22732 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22733 " at %s [in module %s]"),
22734 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22735 objfile_name (dwarf2_per_objfile->objfile));
22736 return build_error_marker_type (cu, die);
22737 }
22738 }
22739
22740 /* Load the DIEs associated with type unit PER_CU into memory. */
22741
22742 static void
22743 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22744 {
22745 struct signatured_type *sig_type;
22746
22747 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22748 gdb_assert (! per_cu->type_unit_group_p ());
22749
22750 /* We have the per_cu, but we need the signatured_type.
22751 Fortunately this is an easy translation. */
22752 gdb_assert (per_cu->is_debug_types);
22753 sig_type = (struct signatured_type *) per_cu;
22754
22755 gdb_assert (per_cu->cu == NULL);
22756
22757 read_signatured_type (sig_type);
22758
22759 gdb_assert (per_cu->cu != NULL);
22760 }
22761
22762 /* Read in a signatured type and build its CU and DIEs.
22763 If the type is a stub for the real type in a DWO file,
22764 read in the real type from the DWO file as well. */
22765
22766 static void
22767 read_signatured_type (struct signatured_type *sig_type)
22768 {
22769 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22770
22771 gdb_assert (per_cu->is_debug_types);
22772 gdb_assert (per_cu->cu == NULL);
22773
22774 cutu_reader reader (per_cu, NULL, 0, false);
22775
22776 if (!reader.dummy_p)
22777 {
22778 struct dwarf2_cu *cu = reader.cu;
22779 const gdb_byte *info_ptr = reader.info_ptr;
22780
22781 gdb_assert (cu->die_hash == NULL);
22782 cu->die_hash =
22783 htab_create_alloc_ex (cu->header.length / 12,
22784 die_hash,
22785 die_eq,
22786 NULL,
22787 &cu->comp_unit_obstack,
22788 hashtab_obstack_allocate,
22789 dummy_obstack_deallocate);
22790
22791 if (reader.comp_unit_die->has_children)
22792 reader.comp_unit_die->child
22793 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22794 reader.comp_unit_die);
22795 cu->dies = reader.comp_unit_die;
22796 /* comp_unit_die is not stored in die_hash, no need. */
22797
22798 /* We try not to read any attributes in this function, because
22799 not all CUs needed for references have been loaded yet, and
22800 symbol table processing isn't initialized. But we have to
22801 set the CU language, or we won't be able to build types
22802 correctly. Similarly, if we do not read the producer, we can
22803 not apply producer-specific interpretation. */
22804 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22805
22806 reader.keep ();
22807 }
22808
22809 sig_type->per_cu.tu_read = 1;
22810 }
22811
22812 /* Decode simple location descriptions.
22813 Given a pointer to a dwarf block that defines a location, compute
22814 the location and return the value.
22815
22816 NOTE drow/2003-11-18: This function is called in two situations
22817 now: for the address of static or global variables (partial symbols
22818 only) and for offsets into structures which are expected to be
22819 (more or less) constant. The partial symbol case should go away,
22820 and only the constant case should remain. That will let this
22821 function complain more accurately. A few special modes are allowed
22822 without complaint for global variables (for instance, global
22823 register values and thread-local values).
22824
22825 A location description containing no operations indicates that the
22826 object is optimized out. The return value is 0 for that case.
22827 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22828 callers will only want a very basic result and this can become a
22829 complaint.
22830
22831 Note that stack[0] is unused except as a default error return. */
22832
22833 static CORE_ADDR
22834 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22835 {
22836 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22837 size_t i;
22838 size_t size = blk->size;
22839 const gdb_byte *data = blk->data;
22840 CORE_ADDR stack[64];
22841 int stacki;
22842 unsigned int bytes_read, unsnd;
22843 gdb_byte op;
22844
22845 i = 0;
22846 stacki = 0;
22847 stack[stacki] = 0;
22848 stack[++stacki] = 0;
22849
22850 while (i < size)
22851 {
22852 op = data[i++];
22853 switch (op)
22854 {
22855 case DW_OP_lit0:
22856 case DW_OP_lit1:
22857 case DW_OP_lit2:
22858 case DW_OP_lit3:
22859 case DW_OP_lit4:
22860 case DW_OP_lit5:
22861 case DW_OP_lit6:
22862 case DW_OP_lit7:
22863 case DW_OP_lit8:
22864 case DW_OP_lit9:
22865 case DW_OP_lit10:
22866 case DW_OP_lit11:
22867 case DW_OP_lit12:
22868 case DW_OP_lit13:
22869 case DW_OP_lit14:
22870 case DW_OP_lit15:
22871 case DW_OP_lit16:
22872 case DW_OP_lit17:
22873 case DW_OP_lit18:
22874 case DW_OP_lit19:
22875 case DW_OP_lit20:
22876 case DW_OP_lit21:
22877 case DW_OP_lit22:
22878 case DW_OP_lit23:
22879 case DW_OP_lit24:
22880 case DW_OP_lit25:
22881 case DW_OP_lit26:
22882 case DW_OP_lit27:
22883 case DW_OP_lit28:
22884 case DW_OP_lit29:
22885 case DW_OP_lit30:
22886 case DW_OP_lit31:
22887 stack[++stacki] = op - DW_OP_lit0;
22888 break;
22889
22890 case DW_OP_reg0:
22891 case DW_OP_reg1:
22892 case DW_OP_reg2:
22893 case DW_OP_reg3:
22894 case DW_OP_reg4:
22895 case DW_OP_reg5:
22896 case DW_OP_reg6:
22897 case DW_OP_reg7:
22898 case DW_OP_reg8:
22899 case DW_OP_reg9:
22900 case DW_OP_reg10:
22901 case DW_OP_reg11:
22902 case DW_OP_reg12:
22903 case DW_OP_reg13:
22904 case DW_OP_reg14:
22905 case DW_OP_reg15:
22906 case DW_OP_reg16:
22907 case DW_OP_reg17:
22908 case DW_OP_reg18:
22909 case DW_OP_reg19:
22910 case DW_OP_reg20:
22911 case DW_OP_reg21:
22912 case DW_OP_reg22:
22913 case DW_OP_reg23:
22914 case DW_OP_reg24:
22915 case DW_OP_reg25:
22916 case DW_OP_reg26:
22917 case DW_OP_reg27:
22918 case DW_OP_reg28:
22919 case DW_OP_reg29:
22920 case DW_OP_reg30:
22921 case DW_OP_reg31:
22922 stack[++stacki] = op - DW_OP_reg0;
22923 if (i < size)
22924 dwarf2_complex_location_expr_complaint ();
22925 break;
22926
22927 case DW_OP_regx:
22928 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22929 i += bytes_read;
22930 stack[++stacki] = unsnd;
22931 if (i < size)
22932 dwarf2_complex_location_expr_complaint ();
22933 break;
22934
22935 case DW_OP_addr:
22936 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22937 &bytes_read);
22938 i += bytes_read;
22939 break;
22940
22941 case DW_OP_const1u:
22942 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22943 i += 1;
22944 break;
22945
22946 case DW_OP_const1s:
22947 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22948 i += 1;
22949 break;
22950
22951 case DW_OP_const2u:
22952 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22953 i += 2;
22954 break;
22955
22956 case DW_OP_const2s:
22957 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22958 i += 2;
22959 break;
22960
22961 case DW_OP_const4u:
22962 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22963 i += 4;
22964 break;
22965
22966 case DW_OP_const4s:
22967 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22968 i += 4;
22969 break;
22970
22971 case DW_OP_const8u:
22972 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22973 i += 8;
22974 break;
22975
22976 case DW_OP_constu:
22977 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22978 &bytes_read);
22979 i += bytes_read;
22980 break;
22981
22982 case DW_OP_consts:
22983 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22984 i += bytes_read;
22985 break;
22986
22987 case DW_OP_dup:
22988 stack[stacki + 1] = stack[stacki];
22989 stacki++;
22990 break;
22991
22992 case DW_OP_plus:
22993 stack[stacki - 1] += stack[stacki];
22994 stacki--;
22995 break;
22996
22997 case DW_OP_plus_uconst:
22998 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22999 &bytes_read);
23000 i += bytes_read;
23001 break;
23002
23003 case DW_OP_minus:
23004 stack[stacki - 1] -= stack[stacki];
23005 stacki--;
23006 break;
23007
23008 case DW_OP_deref:
23009 /* If we're not the last op, then we definitely can't encode
23010 this using GDB's address_class enum. This is valid for partial
23011 global symbols, although the variable's address will be bogus
23012 in the psymtab. */
23013 if (i < size)
23014 dwarf2_complex_location_expr_complaint ();
23015 break;
23016
23017 case DW_OP_GNU_push_tls_address:
23018 case DW_OP_form_tls_address:
23019 /* The top of the stack has the offset from the beginning
23020 of the thread control block at which the variable is located. */
23021 /* Nothing should follow this operator, so the top of stack would
23022 be returned. */
23023 /* This is valid for partial global symbols, but the variable's
23024 address will be bogus in the psymtab. Make it always at least
23025 non-zero to not look as a variable garbage collected by linker
23026 which have DW_OP_addr 0. */
23027 if (i < size)
23028 dwarf2_complex_location_expr_complaint ();
23029 stack[stacki]++;
23030 break;
23031
23032 case DW_OP_GNU_uninit:
23033 break;
23034
23035 case DW_OP_addrx:
23036 case DW_OP_GNU_addr_index:
23037 case DW_OP_GNU_const_index:
23038 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23039 &bytes_read);
23040 i += bytes_read;
23041 break;
23042
23043 default:
23044 {
23045 const char *name = get_DW_OP_name (op);
23046
23047 if (name)
23048 complaint (_("unsupported stack op: '%s'"),
23049 name);
23050 else
23051 complaint (_("unsupported stack op: '%02x'"),
23052 op);
23053 }
23054
23055 return (stack[stacki]);
23056 }
23057
23058 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23059 outside of the allocated space. Also enforce minimum>0. */
23060 if (stacki >= ARRAY_SIZE (stack) - 1)
23061 {
23062 complaint (_("location description stack overflow"));
23063 return 0;
23064 }
23065
23066 if (stacki <= 0)
23067 {
23068 complaint (_("location description stack underflow"));
23069 return 0;
23070 }
23071 }
23072 return (stack[stacki]);
23073 }
23074
23075 /* memory allocation interface */
23076
23077 static struct dwarf_block *
23078 dwarf_alloc_block (struct dwarf2_cu *cu)
23079 {
23080 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23081 }
23082
23083 static struct die_info *
23084 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23085 {
23086 struct die_info *die;
23087 size_t size = sizeof (struct die_info);
23088
23089 if (num_attrs > 1)
23090 size += (num_attrs - 1) * sizeof (struct attribute);
23091
23092 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23093 memset (die, 0, sizeof (struct die_info));
23094 return (die);
23095 }
23096
23097 \f
23098 /* Macro support. */
23099
23100 static struct macro_source_file *
23101 macro_start_file (struct dwarf2_cu *cu,
23102 int file, int line,
23103 struct macro_source_file *current_file,
23104 struct line_header *lh)
23105 {
23106 /* File name relative to the compilation directory of this source file. */
23107 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
23108
23109 if (! current_file)
23110 {
23111 /* Note: We don't create a macro table for this compilation unit
23112 at all until we actually get a filename. */
23113 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
23114
23115 /* If we have no current file, then this must be the start_file
23116 directive for the compilation unit's main source file. */
23117 current_file = macro_set_main (macro_table, file_name.get ());
23118 macro_define_special (macro_table);
23119 }
23120 else
23121 current_file = macro_include (current_file, line, file_name.get ());
23122
23123 return current_file;
23124 }
23125
23126 static const char *
23127 consume_improper_spaces (const char *p, const char *body)
23128 {
23129 if (*p == ' ')
23130 {
23131 complaint (_("macro definition contains spaces "
23132 "in formal argument list:\n`%s'"),
23133 body);
23134
23135 while (*p == ' ')
23136 p++;
23137 }
23138
23139 return p;
23140 }
23141
23142
23143 static void
23144 parse_macro_definition (struct macro_source_file *file, int line,
23145 const char *body)
23146 {
23147 const char *p;
23148
23149 /* The body string takes one of two forms. For object-like macro
23150 definitions, it should be:
23151
23152 <macro name> " " <definition>
23153
23154 For function-like macro definitions, it should be:
23155
23156 <macro name> "() " <definition>
23157 or
23158 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23159
23160 Spaces may appear only where explicitly indicated, and in the
23161 <definition>.
23162
23163 The Dwarf 2 spec says that an object-like macro's name is always
23164 followed by a space, but versions of GCC around March 2002 omit
23165 the space when the macro's definition is the empty string.
23166
23167 The Dwarf 2 spec says that there should be no spaces between the
23168 formal arguments in a function-like macro's formal argument list,
23169 but versions of GCC around March 2002 include spaces after the
23170 commas. */
23171
23172
23173 /* Find the extent of the macro name. The macro name is terminated
23174 by either a space or null character (for an object-like macro) or
23175 an opening paren (for a function-like macro). */
23176 for (p = body; *p; p++)
23177 if (*p == ' ' || *p == '(')
23178 break;
23179
23180 if (*p == ' ' || *p == '\0')
23181 {
23182 /* It's an object-like macro. */
23183 int name_len = p - body;
23184 std::string name (body, name_len);
23185 const char *replacement;
23186
23187 if (*p == ' ')
23188 replacement = body + name_len + 1;
23189 else
23190 {
23191 dwarf2_macro_malformed_definition_complaint (body);
23192 replacement = body + name_len;
23193 }
23194
23195 macro_define_object (file, line, name.c_str (), replacement);
23196 }
23197 else if (*p == '(')
23198 {
23199 /* It's a function-like macro. */
23200 std::string name (body, p - body);
23201 int argc = 0;
23202 int argv_size = 1;
23203 char **argv = XNEWVEC (char *, argv_size);
23204
23205 p++;
23206
23207 p = consume_improper_spaces (p, body);
23208
23209 /* Parse the formal argument list. */
23210 while (*p && *p != ')')
23211 {
23212 /* Find the extent of the current argument name. */
23213 const char *arg_start = p;
23214
23215 while (*p && *p != ',' && *p != ')' && *p != ' ')
23216 p++;
23217
23218 if (! *p || p == arg_start)
23219 dwarf2_macro_malformed_definition_complaint (body);
23220 else
23221 {
23222 /* Make sure argv has room for the new argument. */
23223 if (argc >= argv_size)
23224 {
23225 argv_size *= 2;
23226 argv = XRESIZEVEC (char *, argv, argv_size);
23227 }
23228
23229 argv[argc++] = savestring (arg_start, p - arg_start);
23230 }
23231
23232 p = consume_improper_spaces (p, body);
23233
23234 /* Consume the comma, if present. */
23235 if (*p == ',')
23236 {
23237 p++;
23238
23239 p = consume_improper_spaces (p, body);
23240 }
23241 }
23242
23243 if (*p == ')')
23244 {
23245 p++;
23246
23247 if (*p == ' ')
23248 /* Perfectly formed definition, no complaints. */
23249 macro_define_function (file, line, name.c_str (),
23250 argc, (const char **) argv,
23251 p + 1);
23252 else if (*p == '\0')
23253 {
23254 /* Complain, but do define it. */
23255 dwarf2_macro_malformed_definition_complaint (body);
23256 macro_define_function (file, line, name.c_str (),
23257 argc, (const char **) argv,
23258 p);
23259 }
23260 else
23261 /* Just complain. */
23262 dwarf2_macro_malformed_definition_complaint (body);
23263 }
23264 else
23265 /* Just complain. */
23266 dwarf2_macro_malformed_definition_complaint (body);
23267
23268 {
23269 int i;
23270
23271 for (i = 0; i < argc; i++)
23272 xfree (argv[i]);
23273 }
23274 xfree (argv);
23275 }
23276 else
23277 dwarf2_macro_malformed_definition_complaint (body);
23278 }
23279
23280 /* Skip some bytes from BYTES according to the form given in FORM.
23281 Returns the new pointer. */
23282
23283 static const gdb_byte *
23284 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23285 enum dwarf_form form,
23286 unsigned int offset_size,
23287 struct dwarf2_section_info *section)
23288 {
23289 unsigned int bytes_read;
23290
23291 switch (form)
23292 {
23293 case DW_FORM_data1:
23294 case DW_FORM_flag:
23295 ++bytes;
23296 break;
23297
23298 case DW_FORM_data2:
23299 bytes += 2;
23300 break;
23301
23302 case DW_FORM_data4:
23303 bytes += 4;
23304 break;
23305
23306 case DW_FORM_data8:
23307 bytes += 8;
23308 break;
23309
23310 case DW_FORM_data16:
23311 bytes += 16;
23312 break;
23313
23314 case DW_FORM_string:
23315 read_direct_string (abfd, bytes, &bytes_read);
23316 bytes += bytes_read;
23317 break;
23318
23319 case DW_FORM_sec_offset:
23320 case DW_FORM_strp:
23321 case DW_FORM_GNU_strp_alt:
23322 bytes += offset_size;
23323 break;
23324
23325 case DW_FORM_block:
23326 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23327 bytes += bytes_read;
23328 break;
23329
23330 case DW_FORM_block1:
23331 bytes += 1 + read_1_byte (abfd, bytes);
23332 break;
23333 case DW_FORM_block2:
23334 bytes += 2 + read_2_bytes (abfd, bytes);
23335 break;
23336 case DW_FORM_block4:
23337 bytes += 4 + read_4_bytes (abfd, bytes);
23338 break;
23339
23340 case DW_FORM_addrx:
23341 case DW_FORM_sdata:
23342 case DW_FORM_strx:
23343 case DW_FORM_udata:
23344 case DW_FORM_GNU_addr_index:
23345 case DW_FORM_GNU_str_index:
23346 bytes = gdb_skip_leb128 (bytes, buffer_end);
23347 if (bytes == NULL)
23348 {
23349 dwarf2_section_buffer_overflow_complaint (section);
23350 return NULL;
23351 }
23352 break;
23353
23354 case DW_FORM_implicit_const:
23355 break;
23356
23357 default:
23358 {
23359 complaint (_("invalid form 0x%x in `%s'"),
23360 form, section->get_name ());
23361 return NULL;
23362 }
23363 }
23364
23365 return bytes;
23366 }
23367
23368 /* A helper for dwarf_decode_macros that handles skipping an unknown
23369 opcode. Returns an updated pointer to the macro data buffer; or,
23370 on error, issues a complaint and returns NULL. */
23371
23372 static const gdb_byte *
23373 skip_unknown_opcode (unsigned int opcode,
23374 const gdb_byte **opcode_definitions,
23375 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23376 bfd *abfd,
23377 unsigned int offset_size,
23378 struct dwarf2_section_info *section)
23379 {
23380 unsigned int bytes_read, i;
23381 unsigned long arg;
23382 const gdb_byte *defn;
23383
23384 if (opcode_definitions[opcode] == NULL)
23385 {
23386 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
23387 opcode);
23388 return NULL;
23389 }
23390
23391 defn = opcode_definitions[opcode];
23392 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23393 defn += bytes_read;
23394
23395 for (i = 0; i < arg; ++i)
23396 {
23397 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23398 (enum dwarf_form) defn[i], offset_size,
23399 section);
23400 if (mac_ptr == NULL)
23401 {
23402 /* skip_form_bytes already issued the complaint. */
23403 return NULL;
23404 }
23405 }
23406
23407 return mac_ptr;
23408 }
23409
23410 /* A helper function which parses the header of a macro section.
23411 If the macro section is the extended (for now called "GNU") type,
23412 then this updates *OFFSET_SIZE. Returns a pointer to just after
23413 the header, or issues a complaint and returns NULL on error. */
23414
23415 static const gdb_byte *
23416 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
23417 bfd *abfd,
23418 const gdb_byte *mac_ptr,
23419 unsigned int *offset_size,
23420 int section_is_gnu)
23421 {
23422 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
23423
23424 if (section_is_gnu)
23425 {
23426 unsigned int version, flags;
23427
23428 version = read_2_bytes (abfd, mac_ptr);
23429 if (version != 4 && version != 5)
23430 {
23431 complaint (_("unrecognized version `%d' in .debug_macro section"),
23432 version);
23433 return NULL;
23434 }
23435 mac_ptr += 2;
23436
23437 flags = read_1_byte (abfd, mac_ptr);
23438 ++mac_ptr;
23439 *offset_size = (flags & 1) ? 8 : 4;
23440
23441 if ((flags & 2) != 0)
23442 /* We don't need the line table offset. */
23443 mac_ptr += *offset_size;
23444
23445 /* Vendor opcode descriptions. */
23446 if ((flags & 4) != 0)
23447 {
23448 unsigned int i, count;
23449
23450 count = read_1_byte (abfd, mac_ptr);
23451 ++mac_ptr;
23452 for (i = 0; i < count; ++i)
23453 {
23454 unsigned int opcode, bytes_read;
23455 unsigned long arg;
23456
23457 opcode = read_1_byte (abfd, mac_ptr);
23458 ++mac_ptr;
23459 opcode_definitions[opcode] = mac_ptr;
23460 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23461 mac_ptr += bytes_read;
23462 mac_ptr += arg;
23463 }
23464 }
23465 }
23466
23467 return mac_ptr;
23468 }
23469
23470 /* A helper for dwarf_decode_macros that handles the GNU extensions,
23471 including DW_MACRO_import. */
23472
23473 static void
23474 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
23475 bfd *abfd,
23476 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23477 struct macro_source_file *current_file,
23478 struct line_header *lh,
23479 struct dwarf2_section_info *section,
23480 int section_is_gnu, int section_is_dwz,
23481 unsigned int offset_size,
23482 htab_t include_hash)
23483 {
23484 struct dwarf2_per_objfile *dwarf2_per_objfile
23485 = cu->per_cu->dwarf2_per_objfile;
23486 struct objfile *objfile = dwarf2_per_objfile->objfile;
23487 enum dwarf_macro_record_type macinfo_type;
23488 int at_commandline;
23489 const gdb_byte *opcode_definitions[256];
23490
23491 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23492 &offset_size, section_is_gnu);
23493 if (mac_ptr == NULL)
23494 {
23495 /* We already issued a complaint. */
23496 return;
23497 }
23498
23499 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23500 GDB is still reading the definitions from command line. First
23501 DW_MACINFO_start_file will need to be ignored as it was already executed
23502 to create CURRENT_FILE for the main source holding also the command line
23503 definitions. On first met DW_MACINFO_start_file this flag is reset to
23504 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23505
23506 at_commandline = 1;
23507
23508 do
23509 {
23510 /* Do we at least have room for a macinfo type byte? */
23511 if (mac_ptr >= mac_end)
23512 {
23513 dwarf2_section_buffer_overflow_complaint (section);
23514 break;
23515 }
23516
23517 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23518 mac_ptr++;
23519
23520 /* Note that we rely on the fact that the corresponding GNU and
23521 DWARF constants are the same. */
23522 DIAGNOSTIC_PUSH
23523 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23524 switch (macinfo_type)
23525 {
23526 /* A zero macinfo type indicates the end of the macro
23527 information. */
23528 case 0:
23529 break;
23530
23531 case DW_MACRO_define:
23532 case DW_MACRO_undef:
23533 case DW_MACRO_define_strp:
23534 case DW_MACRO_undef_strp:
23535 case DW_MACRO_define_sup:
23536 case DW_MACRO_undef_sup:
23537 {
23538 unsigned int bytes_read;
23539 int line;
23540 const char *body;
23541 int is_define;
23542
23543 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23544 mac_ptr += bytes_read;
23545
23546 if (macinfo_type == DW_MACRO_define
23547 || macinfo_type == DW_MACRO_undef)
23548 {
23549 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23550 mac_ptr += bytes_read;
23551 }
23552 else
23553 {
23554 LONGEST str_offset;
23555
23556 str_offset = read_offset (abfd, mac_ptr, offset_size);
23557 mac_ptr += offset_size;
23558
23559 if (macinfo_type == DW_MACRO_define_sup
23560 || macinfo_type == DW_MACRO_undef_sup
23561 || section_is_dwz)
23562 {
23563 struct dwz_file *dwz
23564 = dwarf2_get_dwz_file (dwarf2_per_objfile);
23565
23566 body = read_indirect_string_from_dwz (objfile,
23567 dwz, str_offset);
23568 }
23569 else
23570 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23571 abfd, str_offset);
23572 }
23573
23574 is_define = (macinfo_type == DW_MACRO_define
23575 || macinfo_type == DW_MACRO_define_strp
23576 || macinfo_type == DW_MACRO_define_sup);
23577 if (! current_file)
23578 {
23579 /* DWARF violation as no main source is present. */
23580 complaint (_("debug info with no main source gives macro %s "
23581 "on line %d: %s"),
23582 is_define ? _("definition") : _("undefinition"),
23583 line, body);
23584 break;
23585 }
23586 if ((line == 0 && !at_commandline)
23587 || (line != 0 && at_commandline))
23588 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
23589 at_commandline ? _("command-line") : _("in-file"),
23590 is_define ? _("definition") : _("undefinition"),
23591 line == 0 ? _("zero") : _("non-zero"), line, body);
23592
23593 if (body == NULL)
23594 {
23595 /* Fedora's rpm-build's "debugedit" binary
23596 corrupted .debug_macro sections.
23597
23598 For more info, see
23599 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23600 complaint (_("debug info gives %s invalid macro %s "
23601 "without body (corrupted?) at line %d "
23602 "on file %s"),
23603 at_commandline ? _("command-line") : _("in-file"),
23604 is_define ? _("definition") : _("undefinition"),
23605 line, current_file->filename);
23606 }
23607 else if (is_define)
23608 parse_macro_definition (current_file, line, body);
23609 else
23610 {
23611 gdb_assert (macinfo_type == DW_MACRO_undef
23612 || macinfo_type == DW_MACRO_undef_strp
23613 || macinfo_type == DW_MACRO_undef_sup);
23614 macro_undef (current_file, line, body);
23615 }
23616 }
23617 break;
23618
23619 case DW_MACRO_start_file:
23620 {
23621 unsigned int bytes_read;
23622 int line, file;
23623
23624 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23625 mac_ptr += bytes_read;
23626 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23627 mac_ptr += bytes_read;
23628
23629 if ((line == 0 && !at_commandline)
23630 || (line != 0 && at_commandline))
23631 complaint (_("debug info gives source %d included "
23632 "from %s at %s line %d"),
23633 file, at_commandline ? _("command-line") : _("file"),
23634 line == 0 ? _("zero") : _("non-zero"), line);
23635
23636 if (at_commandline)
23637 {
23638 /* This DW_MACRO_start_file was executed in the
23639 pass one. */
23640 at_commandline = 0;
23641 }
23642 else
23643 current_file = macro_start_file (cu, file, line, current_file,
23644 lh);
23645 }
23646 break;
23647
23648 case DW_MACRO_end_file:
23649 if (! current_file)
23650 complaint (_("macro debug info has an unmatched "
23651 "`close_file' directive"));
23652 else
23653 {
23654 current_file = current_file->included_by;
23655 if (! current_file)
23656 {
23657 enum dwarf_macro_record_type next_type;
23658
23659 /* GCC circa March 2002 doesn't produce the zero
23660 type byte marking the end of the compilation
23661 unit. Complain if it's not there, but exit no
23662 matter what. */
23663
23664 /* Do we at least have room for a macinfo type byte? */
23665 if (mac_ptr >= mac_end)
23666 {
23667 dwarf2_section_buffer_overflow_complaint (section);
23668 return;
23669 }
23670
23671 /* We don't increment mac_ptr here, so this is just
23672 a look-ahead. */
23673 next_type
23674 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23675 mac_ptr);
23676 if (next_type != 0)
23677 complaint (_("no terminating 0-type entry for "
23678 "macros in `.debug_macinfo' section"));
23679
23680 return;
23681 }
23682 }
23683 break;
23684
23685 case DW_MACRO_import:
23686 case DW_MACRO_import_sup:
23687 {
23688 LONGEST offset;
23689 void **slot;
23690 bfd *include_bfd = abfd;
23691 struct dwarf2_section_info *include_section = section;
23692 const gdb_byte *include_mac_end = mac_end;
23693 int is_dwz = section_is_dwz;
23694 const gdb_byte *new_mac_ptr;
23695
23696 offset = read_offset (abfd, mac_ptr, offset_size);
23697 mac_ptr += offset_size;
23698
23699 if (macinfo_type == DW_MACRO_import_sup)
23700 {
23701 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
23702
23703 dwz->macro.read (objfile);
23704
23705 include_section = &dwz->macro;
23706 include_bfd = include_section->get_bfd_owner ();
23707 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23708 is_dwz = 1;
23709 }
23710
23711 new_mac_ptr = include_section->buffer + offset;
23712 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23713
23714 if (*slot != NULL)
23715 {
23716 /* This has actually happened; see
23717 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23718 complaint (_("recursive DW_MACRO_import in "
23719 ".debug_macro section"));
23720 }
23721 else
23722 {
23723 *slot = (void *) new_mac_ptr;
23724
23725 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
23726 include_mac_end, current_file, lh,
23727 section, section_is_gnu, is_dwz,
23728 offset_size, include_hash);
23729
23730 htab_remove_elt (include_hash, (void *) new_mac_ptr);
23731 }
23732 }
23733 break;
23734
23735 case DW_MACINFO_vendor_ext:
23736 if (!section_is_gnu)
23737 {
23738 unsigned int bytes_read;
23739
23740 /* This reads the constant, but since we don't recognize
23741 any vendor extensions, we ignore it. */
23742 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23743 mac_ptr += bytes_read;
23744 read_direct_string (abfd, mac_ptr, &bytes_read);
23745 mac_ptr += bytes_read;
23746
23747 /* We don't recognize any vendor extensions. */
23748 break;
23749 }
23750 /* FALLTHROUGH */
23751
23752 default:
23753 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23754 mac_ptr, mac_end, abfd, offset_size,
23755 section);
23756 if (mac_ptr == NULL)
23757 return;
23758 break;
23759 }
23760 DIAGNOSTIC_POP
23761 } while (macinfo_type != 0);
23762 }
23763
23764 static void
23765 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23766 int section_is_gnu)
23767 {
23768 struct dwarf2_per_objfile *dwarf2_per_objfile
23769 = cu->per_cu->dwarf2_per_objfile;
23770 struct objfile *objfile = dwarf2_per_objfile->objfile;
23771 struct line_header *lh = cu->line_header;
23772 bfd *abfd;
23773 const gdb_byte *mac_ptr, *mac_end;
23774 struct macro_source_file *current_file = 0;
23775 enum dwarf_macro_record_type macinfo_type;
23776 unsigned int offset_size = cu->header.offset_size;
23777 const gdb_byte *opcode_definitions[256];
23778 void **slot;
23779 struct dwarf2_section_info *section;
23780 const char *section_name;
23781
23782 if (cu->dwo_unit != NULL)
23783 {
23784 if (section_is_gnu)
23785 {
23786 section = &cu->dwo_unit->dwo_file->sections.macro;
23787 section_name = ".debug_macro.dwo";
23788 }
23789 else
23790 {
23791 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23792 section_name = ".debug_macinfo.dwo";
23793 }
23794 }
23795 else
23796 {
23797 if (section_is_gnu)
23798 {
23799 section = &dwarf2_per_objfile->macro;
23800 section_name = ".debug_macro";
23801 }
23802 else
23803 {
23804 section = &dwarf2_per_objfile->macinfo;
23805 section_name = ".debug_macinfo";
23806 }
23807 }
23808
23809 section->read (objfile);
23810 if (section->buffer == NULL)
23811 {
23812 complaint (_("missing %s section"), section_name);
23813 return;
23814 }
23815 abfd = section->get_bfd_owner ();
23816
23817 /* First pass: Find the name of the base filename.
23818 This filename is needed in order to process all macros whose definition
23819 (or undefinition) comes from the command line. These macros are defined
23820 before the first DW_MACINFO_start_file entry, and yet still need to be
23821 associated to the base file.
23822
23823 To determine the base file name, we scan the macro definitions until we
23824 reach the first DW_MACINFO_start_file entry. We then initialize
23825 CURRENT_FILE accordingly so that any macro definition found before the
23826 first DW_MACINFO_start_file can still be associated to the base file. */
23827
23828 mac_ptr = section->buffer + offset;
23829 mac_end = section->buffer + section->size;
23830
23831 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23832 &offset_size, section_is_gnu);
23833 if (mac_ptr == NULL)
23834 {
23835 /* We already issued a complaint. */
23836 return;
23837 }
23838
23839 do
23840 {
23841 /* Do we at least have room for a macinfo type byte? */
23842 if (mac_ptr >= mac_end)
23843 {
23844 /* Complaint is printed during the second pass as GDB will probably
23845 stop the first pass earlier upon finding
23846 DW_MACINFO_start_file. */
23847 break;
23848 }
23849
23850 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23851 mac_ptr++;
23852
23853 /* Note that we rely on the fact that the corresponding GNU and
23854 DWARF constants are the same. */
23855 DIAGNOSTIC_PUSH
23856 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23857 switch (macinfo_type)
23858 {
23859 /* A zero macinfo type indicates the end of the macro
23860 information. */
23861 case 0:
23862 break;
23863
23864 case DW_MACRO_define:
23865 case DW_MACRO_undef:
23866 /* Only skip the data by MAC_PTR. */
23867 {
23868 unsigned int bytes_read;
23869
23870 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23871 mac_ptr += bytes_read;
23872 read_direct_string (abfd, mac_ptr, &bytes_read);
23873 mac_ptr += bytes_read;
23874 }
23875 break;
23876
23877 case DW_MACRO_start_file:
23878 {
23879 unsigned int bytes_read;
23880 int line, file;
23881
23882 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23883 mac_ptr += bytes_read;
23884 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23885 mac_ptr += bytes_read;
23886
23887 current_file = macro_start_file (cu, file, line, current_file, lh);
23888 }
23889 break;
23890
23891 case DW_MACRO_end_file:
23892 /* No data to skip by MAC_PTR. */
23893 break;
23894
23895 case DW_MACRO_define_strp:
23896 case DW_MACRO_undef_strp:
23897 case DW_MACRO_define_sup:
23898 case DW_MACRO_undef_sup:
23899 {
23900 unsigned int bytes_read;
23901
23902 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23903 mac_ptr += bytes_read;
23904 mac_ptr += offset_size;
23905 }
23906 break;
23907
23908 case DW_MACRO_import:
23909 case DW_MACRO_import_sup:
23910 /* Note that, according to the spec, a transparent include
23911 chain cannot call DW_MACRO_start_file. So, we can just
23912 skip this opcode. */
23913 mac_ptr += offset_size;
23914 break;
23915
23916 case DW_MACINFO_vendor_ext:
23917 /* Only skip the data by MAC_PTR. */
23918 if (!section_is_gnu)
23919 {
23920 unsigned int bytes_read;
23921
23922 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23923 mac_ptr += bytes_read;
23924 read_direct_string (abfd, mac_ptr, &bytes_read);
23925 mac_ptr += bytes_read;
23926 }
23927 /* FALLTHROUGH */
23928
23929 default:
23930 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23931 mac_ptr, mac_end, abfd, offset_size,
23932 section);
23933 if (mac_ptr == NULL)
23934 return;
23935 break;
23936 }
23937 DIAGNOSTIC_POP
23938 } while (macinfo_type != 0 && current_file == NULL);
23939
23940 /* Second pass: Process all entries.
23941
23942 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23943 command-line macro definitions/undefinitions. This flag is unset when we
23944 reach the first DW_MACINFO_start_file entry. */
23945
23946 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23947 htab_eq_pointer,
23948 NULL, xcalloc, xfree));
23949 mac_ptr = section->buffer + offset;
23950 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
23951 *slot = (void *) mac_ptr;
23952 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
23953 current_file, lh, section,
23954 section_is_gnu, 0, offset_size,
23955 include_hash.get ());
23956 }
23957
23958 /* Return the .debug_loc section to use for CU.
23959 For DWO files use .debug_loc.dwo. */
23960
23961 static struct dwarf2_section_info *
23962 cu_debug_loc_section (struct dwarf2_cu *cu)
23963 {
23964 struct dwarf2_per_objfile *dwarf2_per_objfile
23965 = cu->per_cu->dwarf2_per_objfile;
23966
23967 if (cu->dwo_unit)
23968 {
23969 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23970
23971 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23972 }
23973 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23974 : &dwarf2_per_objfile->loc);
23975 }
23976
23977 /* A helper function that fills in a dwarf2_loclist_baton. */
23978
23979 static void
23980 fill_in_loclist_baton (struct dwarf2_cu *cu,
23981 struct dwarf2_loclist_baton *baton,
23982 const struct attribute *attr)
23983 {
23984 struct dwarf2_per_objfile *dwarf2_per_objfile
23985 = cu->per_cu->dwarf2_per_objfile;
23986 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23987
23988 section->read (dwarf2_per_objfile->objfile);
23989
23990 baton->per_cu = cu->per_cu;
23991 gdb_assert (baton->per_cu);
23992 /* We don't know how long the location list is, but make sure we
23993 don't run off the edge of the section. */
23994 baton->size = section->size - DW_UNSND (attr);
23995 baton->data = section->buffer + DW_UNSND (attr);
23996 baton->base_address = cu->base_address;
23997 baton->from_dwo = cu->dwo_unit != NULL;
23998 }
23999
24000 static void
24001 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24002 struct dwarf2_cu *cu, int is_block)
24003 {
24004 struct dwarf2_per_objfile *dwarf2_per_objfile
24005 = cu->per_cu->dwarf2_per_objfile;
24006 struct objfile *objfile = dwarf2_per_objfile->objfile;
24007 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24008
24009 if (attr->form_is_section_offset ()
24010 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24011 the section. If so, fall through to the complaint in the
24012 other branch. */
24013 && DW_UNSND (attr) < section->get_size (objfile))
24014 {
24015 struct dwarf2_loclist_baton *baton;
24016
24017 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24018
24019 fill_in_loclist_baton (cu, baton, attr);
24020
24021 if (cu->base_known == 0)
24022 complaint (_("Location list used without "
24023 "specifying the CU base address."));
24024
24025 SYMBOL_ACLASS_INDEX (sym) = (is_block
24026 ? dwarf2_loclist_block_index
24027 : dwarf2_loclist_index);
24028 SYMBOL_LOCATION_BATON (sym) = baton;
24029 }
24030 else
24031 {
24032 struct dwarf2_locexpr_baton *baton;
24033
24034 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24035 baton->per_cu = cu->per_cu;
24036 gdb_assert (baton->per_cu);
24037
24038 if (attr->form_is_block ())
24039 {
24040 /* Note that we're just copying the block's data pointer
24041 here, not the actual data. We're still pointing into the
24042 info_buffer for SYM's objfile; right now we never release
24043 that buffer, but when we do clean up properly this may
24044 need to change. */
24045 baton->size = DW_BLOCK (attr)->size;
24046 baton->data = DW_BLOCK (attr)->data;
24047 }
24048 else
24049 {
24050 dwarf2_invalid_attrib_class_complaint ("location description",
24051 sym->natural_name ());
24052 baton->size = 0;
24053 }
24054
24055 SYMBOL_ACLASS_INDEX (sym) = (is_block
24056 ? dwarf2_locexpr_block_index
24057 : dwarf2_locexpr_index);
24058 SYMBOL_LOCATION_BATON (sym) = baton;
24059 }
24060 }
24061
24062 /* See read.h. */
24063
24064 struct objfile *
24065 dwarf2_per_cu_data::objfile () const
24066 {
24067 struct objfile *objfile = dwarf2_per_objfile->objfile;
24068
24069 /* Return the master objfile, so that we can report and look up the
24070 correct file containing this variable. */
24071 if (objfile->separate_debug_objfile_backlink)
24072 objfile = objfile->separate_debug_objfile_backlink;
24073
24074 return objfile;
24075 }
24076
24077 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24078 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24079 CU_HEADERP first. */
24080
24081 static const struct comp_unit_head *
24082 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24083 const struct dwarf2_per_cu_data *per_cu)
24084 {
24085 const gdb_byte *info_ptr;
24086
24087 if (per_cu->cu)
24088 return &per_cu->cu->header;
24089
24090 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24091
24092 memset (cu_headerp, 0, sizeof (*cu_headerp));
24093 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24094 rcuh_kind::COMPILE);
24095
24096 return cu_headerp;
24097 }
24098
24099 /* See read.h. */
24100
24101 int
24102 dwarf2_per_cu_data::addr_size () const
24103 {
24104 struct comp_unit_head cu_header_local;
24105 const struct comp_unit_head *cu_headerp;
24106
24107 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24108
24109 return cu_headerp->addr_size;
24110 }
24111
24112 /* See read.h. */
24113
24114 int
24115 dwarf2_per_cu_data::offset_size () const
24116 {
24117 struct comp_unit_head cu_header_local;
24118 const struct comp_unit_head *cu_headerp;
24119
24120 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24121
24122 return cu_headerp->offset_size;
24123 }
24124
24125 /* See read.h. */
24126
24127 int
24128 dwarf2_per_cu_data::ref_addr_size () const
24129 {
24130 struct comp_unit_head cu_header_local;
24131 const struct comp_unit_head *cu_headerp;
24132
24133 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24134
24135 if (cu_headerp->version == 2)
24136 return cu_headerp->addr_size;
24137 else
24138 return cu_headerp->offset_size;
24139 }
24140
24141 /* See read.h. */
24142
24143 CORE_ADDR
24144 dwarf2_per_cu_data::text_offset () const
24145 {
24146 struct objfile *objfile = dwarf2_per_objfile->objfile;
24147
24148 return objfile->text_section_offset ();
24149 }
24150
24151 /* See read.h. */
24152
24153 struct type *
24154 dwarf2_per_cu_data::addr_type () const
24155 {
24156 struct objfile *objfile = dwarf2_per_objfile->objfile;
24157 struct type *void_type = objfile_type (objfile)->builtin_void;
24158 struct type *addr_type = lookup_pointer_type (void_type);
24159 int addr_size = this->addr_size ();
24160
24161 if (TYPE_LENGTH (addr_type) == addr_size)
24162 return addr_type;
24163
24164 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
24165 return addr_type;
24166 }
24167
24168 /* A helper function for dwarf2_find_containing_comp_unit that returns
24169 the index of the result, and that searches a vector. It will
24170 return a result even if the offset in question does not actually
24171 occur in any CU. This is separate so that it can be unit
24172 tested. */
24173
24174 static int
24175 dwarf2_find_containing_comp_unit
24176 (sect_offset sect_off,
24177 unsigned int offset_in_dwz,
24178 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24179 {
24180 int low, high;
24181
24182 low = 0;
24183 high = all_comp_units.size () - 1;
24184 while (high > low)
24185 {
24186 struct dwarf2_per_cu_data *mid_cu;
24187 int mid = low + (high - low) / 2;
24188
24189 mid_cu = all_comp_units[mid];
24190 if (mid_cu->is_dwz > offset_in_dwz
24191 || (mid_cu->is_dwz == offset_in_dwz
24192 && mid_cu->sect_off + mid_cu->length > sect_off))
24193 high = mid;
24194 else
24195 low = mid + 1;
24196 }
24197 gdb_assert (low == high);
24198 return low;
24199 }
24200
24201 /* Locate the .debug_info compilation unit from CU's objfile which contains
24202 the DIE at OFFSET. Raises an error on failure. */
24203
24204 static struct dwarf2_per_cu_data *
24205 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24206 unsigned int offset_in_dwz,
24207 struct dwarf2_per_objfile *dwarf2_per_objfile)
24208 {
24209 int low
24210 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
24211 dwarf2_per_objfile->all_comp_units);
24212 struct dwarf2_per_cu_data *this_cu
24213 = dwarf2_per_objfile->all_comp_units[low];
24214
24215 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24216 {
24217 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24218 error (_("Dwarf Error: could not find partial DIE containing "
24219 "offset %s [in module %s]"),
24220 sect_offset_str (sect_off),
24221 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24222
24223 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24224 <= sect_off);
24225 return dwarf2_per_objfile->all_comp_units[low-1];
24226 }
24227 else
24228 {
24229 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24230 && sect_off >= this_cu->sect_off + this_cu->length)
24231 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24232 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24233 return this_cu;
24234 }
24235 }
24236
24237 #if GDB_SELF_TEST
24238
24239 namespace selftests {
24240 namespace find_containing_comp_unit {
24241
24242 static void
24243 run_test ()
24244 {
24245 struct dwarf2_per_cu_data one {};
24246 struct dwarf2_per_cu_data two {};
24247 struct dwarf2_per_cu_data three {};
24248 struct dwarf2_per_cu_data four {};
24249
24250 one.length = 5;
24251 two.sect_off = sect_offset (one.length);
24252 two.length = 7;
24253
24254 three.length = 5;
24255 three.is_dwz = 1;
24256 four.sect_off = sect_offset (three.length);
24257 four.length = 7;
24258 four.is_dwz = 1;
24259
24260 std::vector<dwarf2_per_cu_data *> units;
24261 units.push_back (&one);
24262 units.push_back (&two);
24263 units.push_back (&three);
24264 units.push_back (&four);
24265
24266 int result;
24267
24268 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24269 SELF_CHECK (units[result] == &one);
24270 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24271 SELF_CHECK (units[result] == &one);
24272 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24273 SELF_CHECK (units[result] == &two);
24274
24275 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24276 SELF_CHECK (units[result] == &three);
24277 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24278 SELF_CHECK (units[result] == &three);
24279 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24280 SELF_CHECK (units[result] == &four);
24281 }
24282
24283 }
24284 }
24285
24286 #endif /* GDB_SELF_TEST */
24287
24288 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24289
24290 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24291 : per_cu (per_cu_),
24292 mark (false),
24293 has_loclist (false),
24294 checked_producer (false),
24295 producer_is_gxx_lt_4_6 (false),
24296 producer_is_gcc_lt_4_3 (false),
24297 producer_is_icc (false),
24298 producer_is_icc_lt_14 (false),
24299 producer_is_codewarrior (false),
24300 processing_has_namespace_info (false)
24301 {
24302 per_cu->cu = this;
24303 }
24304
24305 /* Destroy a dwarf2_cu. */
24306
24307 dwarf2_cu::~dwarf2_cu ()
24308 {
24309 per_cu->cu = NULL;
24310 }
24311
24312 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24313
24314 static void
24315 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24316 enum language pretend_language)
24317 {
24318 struct attribute *attr;
24319
24320 /* Set the language we're debugging. */
24321 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24322 if (attr != nullptr)
24323 set_cu_language (DW_UNSND (attr), cu);
24324 else
24325 {
24326 cu->language = pretend_language;
24327 cu->language_defn = language_def (cu->language);
24328 }
24329
24330 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24331 }
24332
24333 /* Increase the age counter on each cached compilation unit, and free
24334 any that are too old. */
24335
24336 static void
24337 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
24338 {
24339 struct dwarf2_per_cu_data *per_cu, **last_chain;
24340
24341 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24342 per_cu = dwarf2_per_objfile->read_in_chain;
24343 while (per_cu != NULL)
24344 {
24345 per_cu->cu->last_used ++;
24346 if (per_cu->cu->last_used <= dwarf_max_cache_age)
24347 dwarf2_mark (per_cu->cu);
24348 per_cu = per_cu->cu->read_in_chain;
24349 }
24350
24351 per_cu = dwarf2_per_objfile->read_in_chain;
24352 last_chain = &dwarf2_per_objfile->read_in_chain;
24353 while (per_cu != NULL)
24354 {
24355 struct dwarf2_per_cu_data *next_cu;
24356
24357 next_cu = per_cu->cu->read_in_chain;
24358
24359 if (!per_cu->cu->mark)
24360 {
24361 delete per_cu->cu;
24362 *last_chain = next_cu;
24363 }
24364 else
24365 last_chain = &per_cu->cu->read_in_chain;
24366
24367 per_cu = next_cu;
24368 }
24369 }
24370
24371 /* Remove a single compilation unit from the cache. */
24372
24373 static void
24374 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
24375 {
24376 struct dwarf2_per_cu_data *per_cu, **last_chain;
24377 struct dwarf2_per_objfile *dwarf2_per_objfile
24378 = target_per_cu->dwarf2_per_objfile;
24379
24380 per_cu = dwarf2_per_objfile->read_in_chain;
24381 last_chain = &dwarf2_per_objfile->read_in_chain;
24382 while (per_cu != NULL)
24383 {
24384 struct dwarf2_per_cu_data *next_cu;
24385
24386 next_cu = per_cu->cu->read_in_chain;
24387
24388 if (per_cu == target_per_cu)
24389 {
24390 delete per_cu->cu;
24391 per_cu->cu = NULL;
24392 *last_chain = next_cu;
24393 break;
24394 }
24395 else
24396 last_chain = &per_cu->cu->read_in_chain;
24397
24398 per_cu = next_cu;
24399 }
24400 }
24401
24402 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24403 We store these in a hash table separate from the DIEs, and preserve them
24404 when the DIEs are flushed out of cache.
24405
24406 The CU "per_cu" pointer is needed because offset alone is not enough to
24407 uniquely identify the type. A file may have multiple .debug_types sections,
24408 or the type may come from a DWO file. Furthermore, while it's more logical
24409 to use per_cu->section+offset, with Fission the section with the data is in
24410 the DWO file but we don't know that section at the point we need it.
24411 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24412 because we can enter the lookup routine, get_die_type_at_offset, from
24413 outside this file, and thus won't necessarily have PER_CU->cu.
24414 Fortunately, PER_CU is stable for the life of the objfile. */
24415
24416 struct dwarf2_per_cu_offset_and_type
24417 {
24418 const struct dwarf2_per_cu_data *per_cu;
24419 sect_offset sect_off;
24420 struct type *type;
24421 };
24422
24423 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24424
24425 static hashval_t
24426 per_cu_offset_and_type_hash (const void *item)
24427 {
24428 const struct dwarf2_per_cu_offset_and_type *ofs
24429 = (const struct dwarf2_per_cu_offset_and_type *) item;
24430
24431 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24432 }
24433
24434 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24435
24436 static int
24437 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24438 {
24439 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24440 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24441 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24442 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24443
24444 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24445 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24446 }
24447
24448 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24449 table if necessary. For convenience, return TYPE.
24450
24451 The DIEs reading must have careful ordering to:
24452 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24453 reading current DIE.
24454 * Not trying to dereference contents of still incompletely read in types
24455 while reading in other DIEs.
24456 * Enable referencing still incompletely read in types just by a pointer to
24457 the type without accessing its fields.
24458
24459 Therefore caller should follow these rules:
24460 * Try to fetch any prerequisite types we may need to build this DIE type
24461 before building the type and calling set_die_type.
24462 * After building type call set_die_type for current DIE as soon as
24463 possible before fetching more types to complete the current type.
24464 * Make the type as complete as possible before fetching more types. */
24465
24466 static struct type *
24467 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24468 {
24469 struct dwarf2_per_objfile *dwarf2_per_objfile
24470 = cu->per_cu->dwarf2_per_objfile;
24471 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24472 struct objfile *objfile = dwarf2_per_objfile->objfile;
24473 struct attribute *attr;
24474 struct dynamic_prop prop;
24475
24476 /* For Ada types, make sure that the gnat-specific data is always
24477 initialized (if not already set). There are a few types where
24478 we should not be doing so, because the type-specific area is
24479 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24480 where the type-specific area is used to store the floatformat).
24481 But this is not a problem, because the gnat-specific information
24482 is actually not needed for these types. */
24483 if (need_gnat_info (cu)
24484 && TYPE_CODE (type) != TYPE_CODE_FUNC
24485 && TYPE_CODE (type) != TYPE_CODE_FLT
24486 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24487 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24488 && TYPE_CODE (type) != TYPE_CODE_METHOD
24489 && !HAVE_GNAT_AUX_INFO (type))
24490 INIT_GNAT_SPECIFIC (type);
24491
24492 /* Read DW_AT_allocated and set in type. */
24493 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24494 if (attr != NULL && attr->form_is_block ())
24495 {
24496 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24497 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24498 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
24499 }
24500 else if (attr != NULL)
24501 {
24502 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
24503 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24504 sect_offset_str (die->sect_off));
24505 }
24506
24507 /* Read DW_AT_associated and set in type. */
24508 attr = dwarf2_attr (die, DW_AT_associated, cu);
24509 if (attr != NULL && attr->form_is_block ())
24510 {
24511 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24512 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24513 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
24514 }
24515 else if (attr != NULL)
24516 {
24517 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
24518 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24519 sect_offset_str (die->sect_off));
24520 }
24521
24522 /* Read DW_AT_data_location and set in type. */
24523 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24524 if (attr_to_dynamic_prop (attr, die, cu, &prop,
24525 cu->per_cu->addr_type ()))
24526 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
24527
24528 if (dwarf2_per_objfile->die_type_hash == NULL)
24529 dwarf2_per_objfile->die_type_hash
24530 = htab_up (htab_create_alloc (127,
24531 per_cu_offset_and_type_hash,
24532 per_cu_offset_and_type_eq,
24533 NULL, xcalloc, xfree));
24534
24535 ofs.per_cu = cu->per_cu;
24536 ofs.sect_off = die->sect_off;
24537 ofs.type = type;
24538 slot = (struct dwarf2_per_cu_offset_and_type **)
24539 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
24540 if (*slot)
24541 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24542 sect_offset_str (die->sect_off));
24543 *slot = XOBNEW (&objfile->objfile_obstack,
24544 struct dwarf2_per_cu_offset_and_type);
24545 **slot = ofs;
24546 return type;
24547 }
24548
24549 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24550 or return NULL if the die does not have a saved type. */
24551
24552 static struct type *
24553 get_die_type_at_offset (sect_offset sect_off,
24554 struct dwarf2_per_cu_data *per_cu)
24555 {
24556 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24557 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
24558
24559 if (dwarf2_per_objfile->die_type_hash == NULL)
24560 return NULL;
24561
24562 ofs.per_cu = per_cu;
24563 ofs.sect_off = sect_off;
24564 slot = ((struct dwarf2_per_cu_offset_and_type *)
24565 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
24566 if (slot)
24567 return slot->type;
24568 else
24569 return NULL;
24570 }
24571
24572 /* Look up the type for DIE in CU in die_type_hash,
24573 or return NULL if DIE does not have a saved type. */
24574
24575 static struct type *
24576 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24577 {
24578 return get_die_type_at_offset (die->sect_off, cu->per_cu);
24579 }
24580
24581 /* Add a dependence relationship from CU to REF_PER_CU. */
24582
24583 static void
24584 dwarf2_add_dependence (struct dwarf2_cu *cu,
24585 struct dwarf2_per_cu_data *ref_per_cu)
24586 {
24587 void **slot;
24588
24589 if (cu->dependencies == NULL)
24590 cu->dependencies
24591 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24592 NULL, &cu->comp_unit_obstack,
24593 hashtab_obstack_allocate,
24594 dummy_obstack_deallocate);
24595
24596 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24597 if (*slot == NULL)
24598 *slot = ref_per_cu;
24599 }
24600
24601 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24602 Set the mark field in every compilation unit in the
24603 cache that we must keep because we are keeping CU. */
24604
24605 static int
24606 dwarf2_mark_helper (void **slot, void *data)
24607 {
24608 struct dwarf2_per_cu_data *per_cu;
24609
24610 per_cu = (struct dwarf2_per_cu_data *) *slot;
24611
24612 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24613 reading of the chain. As such dependencies remain valid it is not much
24614 useful to track and undo them during QUIT cleanups. */
24615 if (per_cu->cu == NULL)
24616 return 1;
24617
24618 if (per_cu->cu->mark)
24619 return 1;
24620 per_cu->cu->mark = true;
24621
24622 if (per_cu->cu->dependencies != NULL)
24623 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24624
24625 return 1;
24626 }
24627
24628 /* Set the mark field in CU and in every other compilation unit in the
24629 cache that we must keep because we are keeping CU. */
24630
24631 static void
24632 dwarf2_mark (struct dwarf2_cu *cu)
24633 {
24634 if (cu->mark)
24635 return;
24636 cu->mark = true;
24637 if (cu->dependencies != NULL)
24638 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
24639 }
24640
24641 static void
24642 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24643 {
24644 while (per_cu)
24645 {
24646 per_cu->cu->mark = false;
24647 per_cu = per_cu->cu->read_in_chain;
24648 }
24649 }
24650
24651 /* Trivial hash function for partial_die_info: the hash value of a DIE
24652 is its offset in .debug_info for this objfile. */
24653
24654 static hashval_t
24655 partial_die_hash (const void *item)
24656 {
24657 const struct partial_die_info *part_die
24658 = (const struct partial_die_info *) item;
24659
24660 return to_underlying (part_die->sect_off);
24661 }
24662
24663 /* Trivial comparison function for partial_die_info structures: two DIEs
24664 are equal if they have the same offset. */
24665
24666 static int
24667 partial_die_eq (const void *item_lhs, const void *item_rhs)
24668 {
24669 const struct partial_die_info *part_die_lhs
24670 = (const struct partial_die_info *) item_lhs;
24671 const struct partial_die_info *part_die_rhs
24672 = (const struct partial_die_info *) item_rhs;
24673
24674 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24675 }
24676
24677 struct cmd_list_element *set_dwarf_cmdlist;
24678 struct cmd_list_element *show_dwarf_cmdlist;
24679
24680 static void
24681 set_dwarf_cmd (const char *args, int from_tty)
24682 {
24683 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
24684 gdb_stdout);
24685 }
24686
24687 static void
24688 show_dwarf_cmd (const char *args, int from_tty)
24689 {
24690 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
24691 }
24692
24693 static void
24694 show_check_physname (struct ui_file *file, int from_tty,
24695 struct cmd_list_element *c, const char *value)
24696 {
24697 fprintf_filtered (file,
24698 _("Whether to check \"physname\" is %s.\n"),
24699 value);
24700 }
24701
24702 void _initialize_dwarf2_read ();
24703 void
24704 _initialize_dwarf2_read ()
24705 {
24706 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24707 Set DWARF specific variables.\n\
24708 Configure DWARF variables such as the cache size."),
24709 &set_dwarf_cmdlist, "maintenance set dwarf ",
24710 0/*allow-unknown*/, &maintenance_set_cmdlist);
24711
24712 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24713 Show DWARF specific variables.\n\
24714 Show DWARF variables such as the cache size."),
24715 &show_dwarf_cmdlist, "maintenance show dwarf ",
24716 0/*allow-unknown*/, &maintenance_show_cmdlist);
24717
24718 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24719 &dwarf_max_cache_age, _("\
24720 Set the upper bound on the age of cached DWARF compilation units."), _("\
24721 Show the upper bound on the age of cached DWARF compilation units."), _("\
24722 A higher limit means that cached compilation units will be stored\n\
24723 in memory longer, and more total memory will be used. Zero disables\n\
24724 caching, which can slow down startup."),
24725 NULL,
24726 show_dwarf_max_cache_age,
24727 &set_dwarf_cmdlist,
24728 &show_dwarf_cmdlist);
24729
24730 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24731 Set debugging of the DWARF reader."), _("\
24732 Show debugging of the DWARF reader."), _("\
24733 When enabled (non-zero), debugging messages are printed during DWARF\n\
24734 reading and symtab expansion. A value of 1 (one) provides basic\n\
24735 information. A value greater than 1 provides more verbose information."),
24736 NULL,
24737 NULL,
24738 &setdebuglist, &showdebuglist);
24739
24740 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24741 Set debugging of the DWARF DIE reader."), _("\
24742 Show debugging of the DWARF DIE reader."), _("\
24743 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24744 The value is the maximum depth to print."),
24745 NULL,
24746 NULL,
24747 &setdebuglist, &showdebuglist);
24748
24749 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24750 Set debugging of the dwarf line reader."), _("\
24751 Show debugging of the dwarf line reader."), _("\
24752 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24753 A value of 1 (one) provides basic information.\n\
24754 A value greater than 1 provides more verbose information."),
24755 NULL,
24756 NULL,
24757 &setdebuglist, &showdebuglist);
24758
24759 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24760 Set cross-checking of \"physname\" code against demangler."), _("\
24761 Show cross-checking of \"physname\" code against demangler."), _("\
24762 When enabled, GDB's internal \"physname\" code is checked against\n\
24763 the demangler."),
24764 NULL, show_check_physname,
24765 &setdebuglist, &showdebuglist);
24766
24767 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24768 no_class, &use_deprecated_index_sections, _("\
24769 Set whether to use deprecated gdb_index sections."), _("\
24770 Show whether to use deprecated gdb_index sections."), _("\
24771 When enabled, deprecated .gdb_index sections are used anyway.\n\
24772 Normally they are ignored either because of a missing feature or\n\
24773 performance issue.\n\
24774 Warning: This option must be enabled before gdb reads the file."),
24775 NULL,
24776 NULL,
24777 &setlist, &showlist);
24778
24779 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24780 &dwarf2_locexpr_funcs);
24781 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24782 &dwarf2_loclist_funcs);
24783
24784 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24785 &dwarf2_block_frame_base_locexpr_funcs);
24786 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24787 &dwarf2_block_frame_base_loclist_funcs);
24788
24789 #if GDB_SELF_TEST
24790 selftests::register_test ("dw2_expand_symtabs_matching",
24791 selftests::dw2_expand_symtabs_matching::run_test);
24792 selftests::register_test ("dwarf2_find_containing_comp_unit",
24793 selftests::find_containing_comp_unit::run_test);
24794 #endif
24795 }
This page took 0.503272 seconds and 5 git commands to generate.