]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Move two more functions to dwarf2/leb.h
[thirdparty/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "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
86 /* When == 1, print basic high level tracing messages.
87 When > 1, be more verbose.
88 This is in contrast to the low level DIE reading of dwarf_die_debug. */
89 static unsigned int dwarf_read_debug = 0;
90
91 /* When non-zero, dump DIEs after they are read in. */
92 static unsigned int dwarf_die_debug = 0;
93
94 /* When non-zero, dump line number entries as they are read in. */
95 unsigned int dwarf_line_debug = 0;
96
97 /* When true, cross-check physname against demangler. */
98 static bool check_physname = false;
99
100 /* When true, do not reject deprecated .gdb_index sections. */
101 static bool use_deprecated_index_sections = false;
102
103 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
104
105 /* The "aclass" indices for various kinds of computed DWARF symbols. */
106
107 static int dwarf2_locexpr_index;
108 static int dwarf2_loclist_index;
109 static int dwarf2_locexpr_block_index;
110 static int dwarf2_loclist_block_index;
111
112 /* An index into a (C++) symbol name component in a symbol name as
113 recorded in the mapped_index's symbol table. For each C++ symbol
114 in the symbol table, we record one entry for the start of each
115 component in the symbol in a table of name components, and then
116 sort the table, in order to be able to binary search symbol names,
117 ignoring leading namespaces, both completion and regular look up.
118 For example, for symbol "A::B::C", we'll have an entry that points
119 to "A::B::C", another that points to "B::C", and another for "C".
120 Note that function symbols in GDB index have no parameter
121 information, just the function/method names. You can convert a
122 name_component to a "const char *" using the
123 'mapped_index::symbol_name_at(offset_type)' method. */
124
125 struct name_component
126 {
127 /* Offset in the symbol name where the component starts. Stored as
128 a (32-bit) offset instead of a pointer to save memory and improve
129 locality on 64-bit architectures. */
130 offset_type name_offset;
131
132 /* The symbol's index in the symbol and constant pool tables of a
133 mapped_index. */
134 offset_type idx;
135 };
136
137 /* Base class containing bits shared by both .gdb_index and
138 .debug_name indexes. */
139
140 struct mapped_index_base
141 {
142 mapped_index_base () = default;
143 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
144
145 /* The name_component table (a sorted vector). See name_component's
146 description above. */
147 std::vector<name_component> name_components;
148
149 /* How NAME_COMPONENTS is sorted. */
150 enum case_sensitivity name_components_casing;
151
152 /* Return the number of names in the symbol table. */
153 virtual size_t symbol_name_count () const = 0;
154
155 /* Get the name of the symbol at IDX in the symbol table. */
156 virtual const char *symbol_name_at (offset_type idx) const = 0;
157
158 /* Return whether the name at IDX in the symbol table should be
159 ignored. */
160 virtual bool symbol_name_slot_invalid (offset_type idx) const
161 {
162 return false;
163 }
164
165 /* Build the symbol name component sorted vector, if we haven't
166 yet. */
167 void build_name_components ();
168
169 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
170 possible matches for LN_NO_PARAMS in the name component
171 vector. */
172 std::pair<std::vector<name_component>::const_iterator,
173 std::vector<name_component>::const_iterator>
174 find_name_components_bounds (const lookup_name_info &ln_no_params,
175 enum language lang) const;
176
177 /* Prevent deleting/destroying via a base class pointer. */
178 protected:
179 ~mapped_index_base() = default;
180 };
181
182 /* A description of the mapped index. The file format is described in
183 a comment by the code that writes the index. */
184 struct mapped_index final : public mapped_index_base
185 {
186 /* A slot/bucket in the symbol table hash. */
187 struct symbol_table_slot
188 {
189 const offset_type name;
190 const offset_type vec;
191 };
192
193 /* Index data format version. */
194 int version = 0;
195
196 /* The address table data. */
197 gdb::array_view<const gdb_byte> address_table;
198
199 /* The symbol table, implemented as a hash table. */
200 gdb::array_view<symbol_table_slot> symbol_table;
201
202 /* A pointer to the constant pool. */
203 const char *constant_pool = nullptr;
204
205 bool symbol_name_slot_invalid (offset_type idx) const override
206 {
207 const auto &bucket = this->symbol_table[idx];
208 return bucket.name == 0 && bucket.vec == 0;
209 }
210
211 /* Convenience method to get at the name of the symbol at IDX in the
212 symbol table. */
213 const char *symbol_name_at (offset_type idx) const override
214 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
215
216 size_t symbol_name_count () const override
217 { return this->symbol_table.size (); }
218 };
219
220 /* A description of the mapped .debug_names.
221 Uninitialized map has CU_COUNT 0. */
222 struct mapped_debug_names final : public mapped_index_base
223 {
224 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
225 : dwarf2_per_objfile (dwarf2_per_objfile_)
226 {}
227
228 struct dwarf2_per_objfile *dwarf2_per_objfile;
229 bfd_endian dwarf5_byte_order;
230 bool dwarf5_is_dwarf64;
231 bool augmentation_is_gdb;
232 uint8_t offset_size;
233 uint32_t cu_count = 0;
234 uint32_t tu_count, bucket_count, name_count;
235 const gdb_byte *cu_table_reordered, *tu_table_reordered;
236 const uint32_t *bucket_table_reordered, *hash_table_reordered;
237 const gdb_byte *name_table_string_offs_reordered;
238 const gdb_byte *name_table_entry_offs_reordered;
239 const gdb_byte *entry_pool;
240
241 struct index_val
242 {
243 ULONGEST dwarf_tag;
244 struct attr
245 {
246 /* Attribute name DW_IDX_*. */
247 ULONGEST dw_idx;
248
249 /* Attribute form DW_FORM_*. */
250 ULONGEST form;
251
252 /* Value if FORM is DW_FORM_implicit_const. */
253 LONGEST implicit_const;
254 };
255 std::vector<attr> attr_vec;
256 };
257
258 std::unordered_map<ULONGEST, index_val> abbrev_map;
259
260 const char *namei_to_name (uint32_t namei) const;
261
262 /* Implementation of the mapped_index_base virtual interface, for
263 the name_components cache. */
264
265 const char *symbol_name_at (offset_type idx) const override
266 { return namei_to_name (idx); }
267
268 size_t symbol_name_count () const override
269 { return this->name_count; }
270 };
271
272 /* See dwarf2read.h. */
273
274 dwarf2_per_objfile *
275 get_dwarf2_per_objfile (struct objfile *objfile)
276 {
277 return dwarf2_objfile_data_key.get (objfile);
278 }
279
280 /* Default names of the debugging sections. */
281
282 /* Note that if the debugging section has been compressed, it might
283 have a name like .zdebug_info. */
284
285 static const struct dwarf2_debug_sections dwarf2_elf_names =
286 {
287 { ".debug_info", ".zdebug_info" },
288 { ".debug_abbrev", ".zdebug_abbrev" },
289 { ".debug_line", ".zdebug_line" },
290 { ".debug_loc", ".zdebug_loc" },
291 { ".debug_loclists", ".zdebug_loclists" },
292 { ".debug_macinfo", ".zdebug_macinfo" },
293 { ".debug_macro", ".zdebug_macro" },
294 { ".debug_str", ".zdebug_str" },
295 { ".debug_str_offsets", ".zdebug_str_offsets" },
296 { ".debug_line_str", ".zdebug_line_str" },
297 { ".debug_ranges", ".zdebug_ranges" },
298 { ".debug_rnglists", ".zdebug_rnglists" },
299 { ".debug_types", ".zdebug_types" },
300 { ".debug_addr", ".zdebug_addr" },
301 { ".debug_frame", ".zdebug_frame" },
302 { ".eh_frame", NULL },
303 { ".gdb_index", ".zgdb_index" },
304 { ".debug_names", ".zdebug_names" },
305 { ".debug_aranges", ".zdebug_aranges" },
306 23
307 };
308
309 /* List of DWO/DWP sections. */
310
311 static const struct dwop_section_names
312 {
313 struct dwarf2_section_names abbrev_dwo;
314 struct dwarf2_section_names info_dwo;
315 struct dwarf2_section_names line_dwo;
316 struct dwarf2_section_names loc_dwo;
317 struct dwarf2_section_names loclists_dwo;
318 struct dwarf2_section_names macinfo_dwo;
319 struct dwarf2_section_names macro_dwo;
320 struct dwarf2_section_names str_dwo;
321 struct dwarf2_section_names str_offsets_dwo;
322 struct dwarf2_section_names types_dwo;
323 struct dwarf2_section_names cu_index;
324 struct dwarf2_section_names tu_index;
325 }
326 dwop_section_names =
327 {
328 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
329 { ".debug_info.dwo", ".zdebug_info.dwo" },
330 { ".debug_line.dwo", ".zdebug_line.dwo" },
331 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
332 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
333 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
334 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
335 { ".debug_str.dwo", ".zdebug_str.dwo" },
336 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
337 { ".debug_types.dwo", ".zdebug_types.dwo" },
338 { ".debug_cu_index", ".zdebug_cu_index" },
339 { ".debug_tu_index", ".zdebug_tu_index" },
340 };
341
342 /* local data types */
343
344 /* Type used for delaying computation of method physnames.
345 See comments for compute_delayed_physnames. */
346 struct delayed_method_info
347 {
348 /* The type to which the method is attached, i.e., its parent class. */
349 struct type *type;
350
351 /* The index of the method in the type's function fieldlists. */
352 int fnfield_index;
353
354 /* The index of the method in the fieldlist. */
355 int index;
356
357 /* The name of the DIE. */
358 const char *name;
359
360 /* The DIE associated with this method. */
361 struct die_info *die;
362 };
363
364 /* Internal state when decoding a particular compilation unit. */
365 struct dwarf2_cu
366 {
367 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
368 ~dwarf2_cu ();
369
370 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
371
372 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
373 Create the set of symtabs used by this TU, or if this TU is sharing
374 symtabs with another TU and the symtabs have already been created
375 then restore those symtabs in the line header.
376 We don't need the pc/line-number mapping for type units. */
377 void setup_type_unit_groups (struct die_info *die);
378
379 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
380 buildsym_compunit constructor. */
381 struct compunit_symtab *start_symtab (const char *name,
382 const char *comp_dir,
383 CORE_ADDR low_pc);
384
385 /* Reset the builder. */
386 void reset_builder () { m_builder.reset (); }
387
388 /* The header of the compilation unit. */
389 struct comp_unit_head header {};
390
391 /* Base address of this compilation unit. */
392 CORE_ADDR base_address = 0;
393
394 /* Non-zero if base_address has been set. */
395 int base_known = 0;
396
397 /* The language we are debugging. */
398 enum language language = language_unknown;
399 const struct language_defn *language_defn = nullptr;
400
401 const char *producer = nullptr;
402
403 private:
404 /* The symtab builder for this CU. This is only non-NULL when full
405 symbols are being read. */
406 std::unique_ptr<buildsym_compunit> m_builder;
407
408 public:
409 /* The generic symbol table building routines have separate lists for
410 file scope symbols and all all other scopes (local scopes). So
411 we need to select the right one to pass to add_symbol_to_list().
412 We do it by keeping a pointer to the correct list in list_in_scope.
413
414 FIXME: The original dwarf code just treated the file scope as the
415 first local scope, and all other local scopes as nested local
416 scopes, and worked fine. Check to see if we really need to
417 distinguish these in buildsym.c. */
418 struct pending **list_in_scope = nullptr;
419
420 /* Hash table holding all the loaded partial DIEs
421 with partial_die->offset.SECT_OFF as hash. */
422 htab_t partial_dies = nullptr;
423
424 /* Storage for things with the same lifetime as this read-in compilation
425 unit, including partial DIEs. */
426 auto_obstack comp_unit_obstack;
427
428 /* When multiple dwarf2_cu structures are living in memory, this field
429 chains them all together, so that they can be released efficiently.
430 We will probably also want a generation counter so that most-recently-used
431 compilation units are cached... */
432 struct dwarf2_per_cu_data *read_in_chain = nullptr;
433
434 /* Backlink to our per_cu entry. */
435 struct dwarf2_per_cu_data *per_cu;
436
437 /* How many compilation units ago was this CU last referenced? */
438 int last_used = 0;
439
440 /* A hash table of DIE cu_offset for following references with
441 die_info->offset.sect_off as hash. */
442 htab_t die_hash = nullptr;
443
444 /* Full DIEs if read in. */
445 struct die_info *dies = nullptr;
446
447 /* A set of pointers to dwarf2_per_cu_data objects for compilation
448 units referenced by this one. Only set during full symbol processing;
449 partial symbol tables do not have dependencies. */
450 htab_t dependencies = nullptr;
451
452 /* Header data from the line table, during full symbol processing. */
453 struct line_header *line_header = nullptr;
454 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
455 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
456 this is the DW_TAG_compile_unit die for this CU. We'll hold on
457 to the line header as long as this DIE is being processed. See
458 process_die_scope. */
459 die_info *line_header_die_owner = nullptr;
460
461 /* A list of methods which need to have physnames computed
462 after all type information has been read. */
463 std::vector<delayed_method_info> method_list;
464
465 /* To be copied to symtab->call_site_htab. */
466 htab_t call_site_htab = nullptr;
467
468 /* Non-NULL if this CU came from a DWO file.
469 There is an invariant here that is important to remember:
470 Except for attributes copied from the top level DIE in the "main"
471 (or "stub") file in preparation for reading the DWO file
472 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
473 Either there isn't a DWO file (in which case this is NULL and the point
474 is moot), or there is and either we're not going to read it (in which
475 case this is NULL) or there is and we are reading it (in which case this
476 is non-NULL). */
477 struct dwo_unit *dwo_unit = nullptr;
478
479 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
480 Note this value comes from the Fission stub CU/TU's DIE. */
481 gdb::optional<ULONGEST> addr_base;
482
483 /* The DW_AT_rnglists_base attribute if present.
484 Note this value comes from the Fission stub CU/TU's DIE.
485 Also note that the value is zero in the non-DWO case so this value can
486 be used without needing to know whether DWO files are in use or not.
487 N.B. This does not apply to DW_AT_ranges appearing in
488 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
489 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
490 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
491 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
492 ULONGEST ranges_base = 0;
493
494 /* When reading debug info generated by older versions of rustc, we
495 have to rewrite some union types to be struct types with a
496 variant part. This rewriting must be done after the CU is fully
497 read in, because otherwise at the point of rewriting some struct
498 type might not have been fully processed. So, we keep a list of
499 all such types here and process them after expansion. */
500 std::vector<struct type *> rust_unions;
501
502 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
503 files, the value is implicitly zero. For DWARF 5 version DWO files, the
504 value is often implicit and is the size of the header of
505 .debug_str_offsets section (8 or 4, depending on the address size). */
506 gdb::optional<ULONGEST> str_offsets_base;
507
508 /* Mark used when releasing cached dies. */
509 bool mark : 1;
510
511 /* This CU references .debug_loc. See the symtab->locations_valid field.
512 This test is imperfect as there may exist optimized debug code not using
513 any location list and still facing inlining issues if handled as
514 unoptimized code. For a future better test see GCC PR other/32998. */
515 bool has_loclist : 1;
516
517 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
518 if all the producer_is_* fields are valid. This information is cached
519 because profiling CU expansion showed excessive time spent in
520 producer_is_gxx_lt_4_6. */
521 bool checked_producer : 1;
522 bool producer_is_gxx_lt_4_6 : 1;
523 bool producer_is_gcc_lt_4_3 : 1;
524 bool producer_is_icc : 1;
525 bool producer_is_icc_lt_14 : 1;
526 bool producer_is_codewarrior : 1;
527
528 /* When true, the file that we're processing is known to have
529 debugging info for C++ namespaces. GCC 3.3.x did not produce
530 this information, but later versions do. */
531
532 bool processing_has_namespace_info : 1;
533
534 struct partial_die_info *find_partial_die (sect_offset sect_off);
535
536 /* If this CU was inherited by another CU (via specification,
537 abstract_origin, etc), this is the ancestor CU. */
538 dwarf2_cu *ancestor;
539
540 /* Get the buildsym_compunit for this CU. */
541 buildsym_compunit *get_builder ()
542 {
543 /* If this CU has a builder associated with it, use that. */
544 if (m_builder != nullptr)
545 return m_builder.get ();
546
547 /* Otherwise, search ancestors for a valid builder. */
548 if (ancestor != nullptr)
549 return ancestor->get_builder ();
550
551 return nullptr;
552 }
553 };
554
555 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
556 This includes type_unit_group and quick_file_names. */
557
558 struct stmt_list_hash
559 {
560 /* The DWO unit this table is from or NULL if there is none. */
561 struct dwo_unit *dwo_unit;
562
563 /* Offset in .debug_line or .debug_line.dwo. */
564 sect_offset line_sect_off;
565 };
566
567 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
568 an object of this type. */
569
570 struct type_unit_group
571 {
572 /* dwarf2read.c's main "handle" on a TU symtab.
573 To simplify things we create an artificial CU that "includes" all the
574 type units using this stmt_list so that the rest of the code still has
575 a "per_cu" handle on the symtab.
576 This PER_CU is recognized by having no section. */
577 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
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 number of symtabs from the line header.
594 The value here must match line_header.num_file_names. */
595 unsigned int num_symtabs;
596
597 /* The symbol tables for this TU (obtained from the files listed in
598 DW_AT_stmt_list).
599 WARNING: The order of entries here must match the order of entries
600 in the line header. After the first TU using this type_unit_group, the
601 line header for the subsequent TUs is recreated from this. This is done
602 because we need to use the same symtabs for each TU using the same
603 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
604 there's no guarantee the line header doesn't have duplicate entries. */
605 struct symtab **symtabs;
606 };
607
608 /* These sections are what may appear in a (real or virtual) DWO file. */
609
610 struct dwo_sections
611 {
612 struct dwarf2_section_info abbrev;
613 struct dwarf2_section_info line;
614 struct dwarf2_section_info loc;
615 struct dwarf2_section_info loclists;
616 struct dwarf2_section_info macinfo;
617 struct dwarf2_section_info macro;
618 struct dwarf2_section_info str;
619 struct dwarf2_section_info str_offsets;
620 /* In the case of a virtual DWO file, these two are unused. */
621 struct dwarf2_section_info info;
622 std::vector<dwarf2_section_info> types;
623 };
624
625 /* CUs/TUs in DWP/DWO files. */
626
627 struct dwo_unit
628 {
629 /* Backlink to the containing struct dwo_file. */
630 struct dwo_file *dwo_file;
631
632 /* The "id" that distinguishes this CU/TU.
633 .debug_info calls this "dwo_id", .debug_types calls this "signature".
634 Since signatures came first, we stick with it for consistency. */
635 ULONGEST signature;
636
637 /* The section this CU/TU lives in, in the DWO file. */
638 struct dwarf2_section_info *section;
639
640 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
641 sect_offset sect_off;
642 unsigned int length;
643
644 /* For types, offset in the type's DIE of the type defined by this TU. */
645 cu_offset type_offset_in_tu;
646 };
647
648 /* include/dwarf2.h defines the DWP section codes.
649 It defines a max value but it doesn't define a min value, which we
650 use for error checking, so provide one. */
651
652 enum dwp_v2_section_ids
653 {
654 DW_SECT_MIN = 1
655 };
656
657 /* Data for one DWO file.
658
659 This includes virtual DWO files (a virtual DWO file is a DWO file as it
660 appears in a DWP file). DWP files don't really have DWO files per se -
661 comdat folding of types "loses" the DWO file they came from, and from
662 a high level view DWP files appear to contain a mass of random types.
663 However, to maintain consistency with the non-DWP case we pretend DWP
664 files contain virtual DWO files, and we assign each TU with one virtual
665 DWO file (generally based on the line and abbrev section offsets -
666 a heuristic that seems to work in practice). */
667
668 struct dwo_file
669 {
670 dwo_file () = default;
671 DISABLE_COPY_AND_ASSIGN (dwo_file);
672
673 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
674 For virtual DWO files the name is constructed from the section offsets
675 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
676 from related CU+TUs. */
677 const char *dwo_name = nullptr;
678
679 /* The DW_AT_comp_dir attribute. */
680 const char *comp_dir = nullptr;
681
682 /* The bfd, when the file is open. Otherwise this is NULL.
683 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
684 gdb_bfd_ref_ptr dbfd;
685
686 /* The sections that make up this DWO file.
687 Remember that for virtual DWO files in DWP V2, these are virtual
688 sections (for lack of a better name). */
689 struct dwo_sections sections {};
690
691 /* The CUs in the file.
692 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
693 an extension to handle LLVM's Link Time Optimization output (where
694 multiple source files may be compiled into a single object/dwo pair). */
695 htab_up cus;
696
697 /* Table of TUs in the file.
698 Each element is a struct dwo_unit. */
699 htab_up tus;
700 };
701
702 /* These sections are what may appear in a DWP file. */
703
704 struct dwp_sections
705 {
706 /* These are used by both DWP version 1 and 2. */
707 struct dwarf2_section_info str;
708 struct dwarf2_section_info cu_index;
709 struct dwarf2_section_info tu_index;
710
711 /* These are only used by DWP version 2 files.
712 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
713 sections are referenced by section number, and are not recorded here.
714 In DWP version 2 there is at most one copy of all these sections, each
715 section being (effectively) comprised of the concatenation of all of the
716 individual sections that exist in the version 1 format.
717 To keep the code simple we treat each of these concatenated pieces as a
718 section itself (a virtual section?). */
719 struct dwarf2_section_info abbrev;
720 struct dwarf2_section_info info;
721 struct dwarf2_section_info line;
722 struct dwarf2_section_info loc;
723 struct dwarf2_section_info macinfo;
724 struct dwarf2_section_info macro;
725 struct dwarf2_section_info str_offsets;
726 struct dwarf2_section_info types;
727 };
728
729 /* These sections are what may appear in a virtual DWO file in DWP version 1.
730 A virtual DWO file is a DWO file as it appears in a DWP file. */
731
732 struct virtual_v1_dwo_sections
733 {
734 struct dwarf2_section_info abbrev;
735 struct dwarf2_section_info line;
736 struct dwarf2_section_info loc;
737 struct dwarf2_section_info macinfo;
738 struct dwarf2_section_info macro;
739 struct dwarf2_section_info str_offsets;
740 /* Each DWP hash table entry records one CU or one TU.
741 That is recorded here, and copied to dwo_unit.section. */
742 struct dwarf2_section_info info_or_types;
743 };
744
745 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
746 In version 2, the sections of the DWO files are concatenated together
747 and stored in one section of that name. Thus each ELF section contains
748 several "virtual" sections. */
749
750 struct virtual_v2_dwo_sections
751 {
752 bfd_size_type abbrev_offset;
753 bfd_size_type abbrev_size;
754
755 bfd_size_type line_offset;
756 bfd_size_type line_size;
757
758 bfd_size_type loc_offset;
759 bfd_size_type loc_size;
760
761 bfd_size_type macinfo_offset;
762 bfd_size_type macinfo_size;
763
764 bfd_size_type macro_offset;
765 bfd_size_type macro_size;
766
767 bfd_size_type str_offsets_offset;
768 bfd_size_type str_offsets_size;
769
770 /* Each DWP hash table entry records one CU or one TU.
771 That is recorded here, and copied to dwo_unit.section. */
772 bfd_size_type info_or_types_offset;
773 bfd_size_type info_or_types_size;
774 };
775
776 /* Contents of DWP hash tables. */
777
778 struct dwp_hash_table
779 {
780 uint32_t version, nr_columns;
781 uint32_t nr_units, nr_slots;
782 const gdb_byte *hash_table, *unit_table;
783 union
784 {
785 struct
786 {
787 const gdb_byte *indices;
788 } v1;
789 struct
790 {
791 /* This is indexed by column number and gives the id of the section
792 in that column. */
793 #define MAX_NR_V2_DWO_SECTIONS \
794 (1 /* .debug_info or .debug_types */ \
795 + 1 /* .debug_abbrev */ \
796 + 1 /* .debug_line */ \
797 + 1 /* .debug_loc */ \
798 + 1 /* .debug_str_offsets */ \
799 + 1 /* .debug_macro or .debug_macinfo */)
800 int section_ids[MAX_NR_V2_DWO_SECTIONS];
801 const gdb_byte *offsets;
802 const gdb_byte *sizes;
803 } v2;
804 } section_pool;
805 };
806
807 /* Data for one DWP file. */
808
809 struct dwp_file
810 {
811 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
812 : name (name_),
813 dbfd (std::move (abfd))
814 {
815 }
816
817 /* Name of the file. */
818 const char *name;
819
820 /* File format version. */
821 int version = 0;
822
823 /* The bfd. */
824 gdb_bfd_ref_ptr dbfd;
825
826 /* Section info for this file. */
827 struct dwp_sections sections {};
828
829 /* Table of CUs in the file. */
830 const struct dwp_hash_table *cus = nullptr;
831
832 /* Table of TUs in the file. */
833 const struct dwp_hash_table *tus = nullptr;
834
835 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
836 htab_up loaded_cus;
837 htab_up loaded_tus;
838
839 /* Table to map ELF section numbers to their sections.
840 This is only needed for the DWP V1 file format. */
841 unsigned int num_sections = 0;
842 asection **elf_sections = nullptr;
843 };
844
845 /* Struct used to pass misc. parameters to read_die_and_children, et
846 al. which are used for both .debug_info and .debug_types dies.
847 All parameters here are unchanging for the life of the call. This
848 struct exists to abstract away the constant parameters of die reading. */
849
850 struct die_reader_specs
851 {
852 /* The bfd of die_section. */
853 bfd* abfd;
854
855 /* The CU of the DIE we are parsing. */
856 struct dwarf2_cu *cu;
857
858 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
859 struct dwo_file *dwo_file;
860
861 /* The section the die comes from.
862 This is either .debug_info or .debug_types, or the .dwo variants. */
863 struct dwarf2_section_info *die_section;
864
865 /* die_section->buffer. */
866 const gdb_byte *buffer;
867
868 /* The end of the buffer. */
869 const gdb_byte *buffer_end;
870
871 /* The abbreviation table to use when reading the DIEs. */
872 struct abbrev_table *abbrev_table;
873 };
874
875 /* A subclass of die_reader_specs that holds storage and has complex
876 constructor and destructor behavior. */
877
878 class cutu_reader : public die_reader_specs
879 {
880 public:
881
882 cutu_reader (struct dwarf2_per_cu_data *this_cu,
883 struct abbrev_table *abbrev_table,
884 int use_existing_cu, int keep,
885 bool skip_partial);
886
887 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
888 struct dwarf2_cu *parent_cu = nullptr,
889 struct dwo_file *dwo_file = nullptr);
890
891 ~cutu_reader ();
892
893 DISABLE_COPY_AND_ASSIGN (cutu_reader);
894
895 const gdb_byte *info_ptr = nullptr;
896 struct die_info *comp_unit_die = nullptr;
897 bool dummy_p = false;
898
899 private:
900 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
901 int use_existing_cu, int keep);
902
903 struct dwarf2_per_cu_data *m_this_cu;
904 int m_keep = 0;
905 std::unique_ptr<dwarf2_cu> m_new_cu;
906
907 /* The ordinary abbreviation table. */
908 abbrev_table_up m_abbrev_table_holder;
909
910 /* The DWO abbreviation table. */
911 abbrev_table_up m_dwo_abbrev_table;
912 };
913
914 /* When we construct a partial symbol table entry we only
915 need this much information. */
916 struct partial_die_info : public allocate_on_obstack
917 {
918 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
919
920 /* Disable assign but still keep copy ctor, which is needed
921 load_partial_dies. */
922 partial_die_info& operator=(const partial_die_info& rhs) = delete;
923
924 /* Adjust the partial die before generating a symbol for it. This
925 function may set the is_external flag or change the DIE's
926 name. */
927 void fixup (struct dwarf2_cu *cu);
928
929 /* Read a minimal amount of information into the minimal die
930 structure. */
931 const gdb_byte *read (const struct die_reader_specs *reader,
932 const struct abbrev_info &abbrev,
933 const gdb_byte *info_ptr);
934
935 /* Offset of this DIE. */
936 const sect_offset sect_off;
937
938 /* DWARF-2 tag for this DIE. */
939 const ENUM_BITFIELD(dwarf_tag) tag : 16;
940
941 /* Assorted flags describing the data found in this DIE. */
942 const unsigned int has_children : 1;
943
944 unsigned int is_external : 1;
945 unsigned int is_declaration : 1;
946 unsigned int has_type : 1;
947 unsigned int has_specification : 1;
948 unsigned int has_pc_info : 1;
949 unsigned int may_be_inlined : 1;
950
951 /* This DIE has been marked DW_AT_main_subprogram. */
952 unsigned int main_subprogram : 1;
953
954 /* Flag set if the SCOPE field of this structure has been
955 computed. */
956 unsigned int scope_set : 1;
957
958 /* Flag set if the DIE has a byte_size attribute. */
959 unsigned int has_byte_size : 1;
960
961 /* Flag set if the DIE has a DW_AT_const_value attribute. */
962 unsigned int has_const_value : 1;
963
964 /* Flag set if any of the DIE's children are template arguments. */
965 unsigned int has_template_arguments : 1;
966
967 /* Flag set if fixup has been called on this die. */
968 unsigned int fixup_called : 1;
969
970 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
971 unsigned int is_dwz : 1;
972
973 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
974 unsigned int spec_is_dwz : 1;
975
976 /* The name of this DIE. Normally the value of DW_AT_name, but
977 sometimes a default name for unnamed DIEs. */
978 const char *name = nullptr;
979
980 /* The linkage name, if present. */
981 const char *linkage_name = nullptr;
982
983 /* The scope to prepend to our children. This is generally
984 allocated on the comp_unit_obstack, so will disappear
985 when this compilation unit leaves the cache. */
986 const char *scope = nullptr;
987
988 /* Some data associated with the partial DIE. The tag determines
989 which field is live. */
990 union
991 {
992 /* The location description associated with this DIE, if any. */
993 struct dwarf_block *locdesc;
994 /* The offset of an import, for DW_TAG_imported_unit. */
995 sect_offset sect_off;
996 } d {};
997
998 /* If HAS_PC_INFO, the PC range associated with this DIE. */
999 CORE_ADDR lowpc = 0;
1000 CORE_ADDR highpc = 0;
1001
1002 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1003 DW_AT_sibling, if any. */
1004 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1005 could return DW_AT_sibling values to its caller load_partial_dies. */
1006 const gdb_byte *sibling = nullptr;
1007
1008 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1009 DW_AT_specification (or DW_AT_abstract_origin or
1010 DW_AT_extension). */
1011 sect_offset spec_offset {};
1012
1013 /* Pointers to this DIE's parent, first child, and next sibling,
1014 if any. */
1015 struct partial_die_info *die_parent = nullptr;
1016 struct partial_die_info *die_child = nullptr;
1017 struct partial_die_info *die_sibling = nullptr;
1018
1019 friend struct partial_die_info *
1020 dwarf2_cu::find_partial_die (sect_offset sect_off);
1021
1022 private:
1023 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1024 partial_die_info (sect_offset sect_off)
1025 : partial_die_info (sect_off, DW_TAG_padding, 0)
1026 {
1027 }
1028
1029 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1030 int has_children_)
1031 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1032 {
1033 is_external = 0;
1034 is_declaration = 0;
1035 has_type = 0;
1036 has_specification = 0;
1037 has_pc_info = 0;
1038 may_be_inlined = 0;
1039 main_subprogram = 0;
1040 scope_set = 0;
1041 has_byte_size = 0;
1042 has_const_value = 0;
1043 has_template_arguments = 0;
1044 fixup_called = 0;
1045 is_dwz = 0;
1046 spec_is_dwz = 0;
1047 }
1048 };
1049
1050 /* This data structure holds a complete die structure. */
1051 struct die_info
1052 {
1053 /* DWARF-2 tag for this DIE. */
1054 ENUM_BITFIELD(dwarf_tag) tag : 16;
1055
1056 /* Number of attributes */
1057 unsigned char num_attrs;
1058
1059 /* True if we're presently building the full type name for the
1060 type derived from this DIE. */
1061 unsigned char building_fullname : 1;
1062
1063 /* True if this die is in process. PR 16581. */
1064 unsigned char in_process : 1;
1065
1066 /* True if this DIE has children. */
1067 unsigned char has_children : 1;
1068
1069 /* Abbrev number */
1070 unsigned int abbrev;
1071
1072 /* Offset in .debug_info or .debug_types section. */
1073 sect_offset sect_off;
1074
1075 /* The dies in a compilation unit form an n-ary tree. PARENT
1076 points to this die's parent; CHILD points to the first child of
1077 this node; and all the children of a given node are chained
1078 together via their SIBLING fields. */
1079 struct die_info *child; /* Its first child, if any. */
1080 struct die_info *sibling; /* Its next sibling, if any. */
1081 struct die_info *parent; /* Its parent, if any. */
1082
1083 /* An array of attributes, with NUM_ATTRS elements. There may be
1084 zero, but it's not common and zero-sized arrays are not
1085 sufficiently portable C. */
1086 struct attribute attrs[1];
1087 };
1088
1089 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1090 but this would require a corresponding change in unpack_field_as_long
1091 and friends. */
1092 static int bits_per_byte = 8;
1093
1094 /* When reading a variant or variant part, we track a bit more
1095 information about the field, and store it in an object of this
1096 type. */
1097
1098 struct variant_field
1099 {
1100 /* If we see a DW_TAG_variant, then this will be the discriminant
1101 value. */
1102 ULONGEST discriminant_value;
1103 /* If we see a DW_TAG_variant, then this will be set if this is the
1104 default branch. */
1105 bool default_branch;
1106 /* While reading a DW_TAG_variant_part, this will be set if this
1107 field is the discriminant. */
1108 bool is_discriminant;
1109 };
1110
1111 struct nextfield
1112 {
1113 int accessibility = 0;
1114 int virtuality = 0;
1115 /* Extra information to describe a variant or variant part. */
1116 struct variant_field variant {};
1117 struct field field {};
1118 };
1119
1120 struct fnfieldlist
1121 {
1122 const char *name = nullptr;
1123 std::vector<struct fn_field> fnfields;
1124 };
1125
1126 /* The routines that read and process dies for a C struct or C++ class
1127 pass lists of data member fields and lists of member function fields
1128 in an instance of a field_info structure, as defined below. */
1129 struct field_info
1130 {
1131 /* List of data member and baseclasses fields. */
1132 std::vector<struct nextfield> fields;
1133 std::vector<struct nextfield> baseclasses;
1134
1135 /* Number of fields (including baseclasses). */
1136 int nfields = 0;
1137
1138 /* Set if the accessibility of one of the fields is not public. */
1139 int non_public_fields = 0;
1140
1141 /* Member function fieldlist array, contains name of possibly overloaded
1142 member function, number of overloaded member functions and a pointer
1143 to the head of the member function field chain. */
1144 std::vector<struct fnfieldlist> fnfieldlists;
1145
1146 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1147 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1148 std::vector<struct decl_field> typedef_field_list;
1149
1150 /* Nested types defined by this class and the number of elements in this
1151 list. */
1152 std::vector<struct decl_field> nested_types_list;
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 obstack *);
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 (struct objfile *objfile);
1652
1653 static htab_up allocate_dwo_unit_table (struct objfile *objfile);
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 == NULL)
2155 error (_("could not find '.gnu_debugaltlink' file for %s"),
2156 objfile_name (dwarf2_per_objfile->objfile));
2157
2158 std::unique_ptr<struct dwz_file> result
2159 (new struct dwz_file (std::move (dwz_bfd)));
2160
2161 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2162 result.get ());
2163
2164 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2165 result->dwz_bfd.get ());
2166 dwarf2_per_objfile->dwz_file = std::move (result);
2167 return dwarf2_per_objfile->dwz_file.get ();
2168 }
2169 \f
2170 /* DWARF quick_symbols_functions support. */
2171
2172 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2173 unique line tables, so we maintain a separate table of all .debug_line
2174 derived entries to support the sharing.
2175 All the quick functions need is the list of file names. We discard the
2176 line_header when we're done and don't need to record it here. */
2177 struct quick_file_names
2178 {
2179 /* The data used to construct the hash key. */
2180 struct stmt_list_hash hash;
2181
2182 /* The number of entries in file_names, real_names. */
2183 unsigned int num_file_names;
2184
2185 /* The file names from the line table, after being run through
2186 file_full_name. */
2187 const char **file_names;
2188
2189 /* The file names from the line table after being run through
2190 gdb_realpath. These are computed lazily. */
2191 const char **real_names;
2192 };
2193
2194 /* When using the index (and thus not using psymtabs), each CU has an
2195 object of this type. This is used to hold information needed by
2196 the various "quick" methods. */
2197 struct dwarf2_per_cu_quick_data
2198 {
2199 /* The file table. This can be NULL if there was no file table
2200 or it's currently not read in.
2201 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2202 struct quick_file_names *file_names;
2203
2204 /* The corresponding symbol table. This is NULL if symbols for this
2205 CU have not yet been read. */
2206 struct compunit_symtab *compunit_symtab;
2207
2208 /* A temporary mark bit used when iterating over all CUs in
2209 expand_symtabs_matching. */
2210 unsigned int mark : 1;
2211
2212 /* True if we've tried to read the file table and found there isn't one.
2213 There will be no point in trying to read it again next time. */
2214 unsigned int no_file_data : 1;
2215 };
2216
2217 /* Utility hash function for a stmt_list_hash. */
2218
2219 static hashval_t
2220 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2221 {
2222 hashval_t v = 0;
2223
2224 if (stmt_list_hash->dwo_unit != NULL)
2225 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2226 v += to_underlying (stmt_list_hash->line_sect_off);
2227 return v;
2228 }
2229
2230 /* Utility equality function for a stmt_list_hash. */
2231
2232 static int
2233 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2234 const struct stmt_list_hash *rhs)
2235 {
2236 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2237 return 0;
2238 if (lhs->dwo_unit != NULL
2239 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2240 return 0;
2241
2242 return lhs->line_sect_off == rhs->line_sect_off;
2243 }
2244
2245 /* Hash function for a quick_file_names. */
2246
2247 static hashval_t
2248 hash_file_name_entry (const void *e)
2249 {
2250 const struct quick_file_names *file_data
2251 = (const struct quick_file_names *) e;
2252
2253 return hash_stmt_list_entry (&file_data->hash);
2254 }
2255
2256 /* Equality function for a quick_file_names. */
2257
2258 static int
2259 eq_file_name_entry (const void *a, const void *b)
2260 {
2261 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2262 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2263
2264 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2265 }
2266
2267 /* Delete function for a quick_file_names. */
2268
2269 static void
2270 delete_file_name_entry (void *e)
2271 {
2272 struct quick_file_names *file_data = (struct quick_file_names *) e;
2273 int i;
2274
2275 for (i = 0; i < file_data->num_file_names; ++i)
2276 {
2277 xfree ((void*) file_data->file_names[i]);
2278 if (file_data->real_names)
2279 xfree ((void*) file_data->real_names[i]);
2280 }
2281
2282 /* The space for the struct itself lives on objfile_obstack,
2283 so we don't free it here. */
2284 }
2285
2286 /* Create a quick_file_names hash table. */
2287
2288 static htab_up
2289 create_quick_file_names_table (unsigned int nr_initial_entries)
2290 {
2291 return htab_up (htab_create_alloc (nr_initial_entries,
2292 hash_file_name_entry, eq_file_name_entry,
2293 delete_file_name_entry, xcalloc, xfree));
2294 }
2295
2296 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2297 have to be created afterwards. You should call age_cached_comp_units after
2298 processing PER_CU->CU. dw2_setup must have been already called. */
2299
2300 static void
2301 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2302 {
2303 if (per_cu->is_debug_types)
2304 load_full_type_unit (per_cu);
2305 else
2306 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2307
2308 if (per_cu->cu == NULL)
2309 return; /* Dummy CU. */
2310
2311 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2312 }
2313
2314 /* Read in the symbols for PER_CU. */
2315
2316 static void
2317 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2318 {
2319 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2320
2321 /* Skip type_unit_groups, reading the type units they contain
2322 is handled elsewhere. */
2323 if (IS_TYPE_UNIT_GROUP (per_cu))
2324 return;
2325
2326 /* The destructor of dwarf2_queue_guard frees any entries left on
2327 the queue. After this point we're guaranteed to leave this function
2328 with the dwarf queue empty. */
2329 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2330
2331 if (dwarf2_per_objfile->using_index
2332 ? per_cu->v.quick->compunit_symtab == NULL
2333 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2334 {
2335 queue_comp_unit (per_cu, language_minimal);
2336 load_cu (per_cu, skip_partial);
2337
2338 /* If we just loaded a CU from a DWO, and we're working with an index
2339 that may badly handle TUs, load all the TUs in that DWO as well.
2340 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2341 if (!per_cu->is_debug_types
2342 && per_cu->cu != NULL
2343 && per_cu->cu->dwo_unit != NULL
2344 && dwarf2_per_objfile->index_table != NULL
2345 && dwarf2_per_objfile->index_table->version <= 7
2346 /* DWP files aren't supported yet. */
2347 && get_dwp_file (dwarf2_per_objfile) == NULL)
2348 queue_and_load_all_dwo_tus (per_cu);
2349 }
2350
2351 process_queue (dwarf2_per_objfile);
2352
2353 /* Age the cache, releasing compilation units that have not
2354 been used recently. */
2355 age_cached_comp_units (dwarf2_per_objfile);
2356 }
2357
2358 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2359 the objfile from which this CU came. Returns the resulting symbol
2360 table. */
2361
2362 static struct compunit_symtab *
2363 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2364 {
2365 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2366
2367 gdb_assert (dwarf2_per_objfile->using_index);
2368 if (!per_cu->v.quick->compunit_symtab)
2369 {
2370 free_cached_comp_units freer (dwarf2_per_objfile);
2371 scoped_restore decrementer = increment_reading_symtab ();
2372 dw2_do_instantiate_symtab (per_cu, skip_partial);
2373 process_cu_includes (dwarf2_per_objfile);
2374 }
2375
2376 return per_cu->v.quick->compunit_symtab;
2377 }
2378
2379 /* See declaration. */
2380
2381 dwarf2_per_cu_data *
2382 dwarf2_per_objfile::get_cutu (int index)
2383 {
2384 if (index >= this->all_comp_units.size ())
2385 {
2386 index -= this->all_comp_units.size ();
2387 gdb_assert (index < this->all_type_units.size ());
2388 return &this->all_type_units[index]->per_cu;
2389 }
2390
2391 return this->all_comp_units[index];
2392 }
2393
2394 /* See declaration. */
2395
2396 dwarf2_per_cu_data *
2397 dwarf2_per_objfile::get_cu (int index)
2398 {
2399 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2400
2401 return this->all_comp_units[index];
2402 }
2403
2404 /* See declaration. */
2405
2406 signatured_type *
2407 dwarf2_per_objfile::get_tu (int index)
2408 {
2409 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2410
2411 return this->all_type_units[index];
2412 }
2413
2414 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2415 objfile_obstack, and constructed with the specified field
2416 values. */
2417
2418 static dwarf2_per_cu_data *
2419 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2420 struct dwarf2_section_info *section,
2421 int is_dwz,
2422 sect_offset sect_off, ULONGEST length)
2423 {
2424 struct objfile *objfile = dwarf2_per_objfile->objfile;
2425 dwarf2_per_cu_data *the_cu
2426 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2427 struct dwarf2_per_cu_data);
2428 the_cu->sect_off = sect_off;
2429 the_cu->length = length;
2430 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2431 the_cu->section = section;
2432 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2433 struct dwarf2_per_cu_quick_data);
2434 the_cu->is_dwz = is_dwz;
2435 return the_cu;
2436 }
2437
2438 /* A helper for create_cus_from_index that handles a given list of
2439 CUs. */
2440
2441 static void
2442 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2443 const gdb_byte *cu_list, offset_type n_elements,
2444 struct dwarf2_section_info *section,
2445 int is_dwz)
2446 {
2447 for (offset_type i = 0; i < n_elements; i += 2)
2448 {
2449 gdb_static_assert (sizeof (ULONGEST) >= 8);
2450
2451 sect_offset sect_off
2452 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2453 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2454 cu_list += 2 * 8;
2455
2456 dwarf2_per_cu_data *per_cu
2457 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2458 sect_off, length);
2459 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2460 }
2461 }
2462
2463 /* Read the CU list from the mapped index, and use it to create all
2464 the CU objects for this objfile. */
2465
2466 static void
2467 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2468 const gdb_byte *cu_list, offset_type cu_list_elements,
2469 const gdb_byte *dwz_list, offset_type dwz_elements)
2470 {
2471 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2472 dwarf2_per_objfile->all_comp_units.reserve
2473 ((cu_list_elements + dwz_elements) / 2);
2474
2475 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2476 &dwarf2_per_objfile->info, 0);
2477
2478 if (dwz_elements == 0)
2479 return;
2480
2481 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2482 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2483 &dwz->info, 1);
2484 }
2485
2486 /* Create the signatured type hash table from the index. */
2487
2488 static void
2489 create_signatured_type_table_from_index
2490 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2491 struct dwarf2_section_info *section,
2492 const gdb_byte *bytes,
2493 offset_type elements)
2494 {
2495 struct objfile *objfile = dwarf2_per_objfile->objfile;
2496
2497 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2498 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2499
2500 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
2501
2502 for (offset_type i = 0; i < elements; i += 3)
2503 {
2504 struct signatured_type *sig_type;
2505 ULONGEST signature;
2506 void **slot;
2507 cu_offset type_offset_in_tu;
2508
2509 gdb_static_assert (sizeof (ULONGEST) >= 8);
2510 sect_offset sect_off
2511 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2512 type_offset_in_tu
2513 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2514 BFD_ENDIAN_LITTLE);
2515 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2516 bytes += 3 * 8;
2517
2518 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2519 struct signatured_type);
2520 sig_type->signature = signature;
2521 sig_type->type_offset_in_tu = type_offset_in_tu;
2522 sig_type->per_cu.is_debug_types = 1;
2523 sig_type->per_cu.section = section;
2524 sig_type->per_cu.sect_off = sect_off;
2525 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2526 sig_type->per_cu.v.quick
2527 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2528 struct dwarf2_per_cu_quick_data);
2529
2530 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2531 *slot = sig_type;
2532
2533 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2534 }
2535
2536 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2537 }
2538
2539 /* Create the signatured type hash table from .debug_names. */
2540
2541 static void
2542 create_signatured_type_table_from_debug_names
2543 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2544 const mapped_debug_names &map,
2545 struct dwarf2_section_info *section,
2546 struct dwarf2_section_info *abbrev_section)
2547 {
2548 struct objfile *objfile = dwarf2_per_objfile->objfile;
2549
2550 section->read (objfile);
2551 abbrev_section->read (objfile);
2552
2553 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2554 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2555
2556 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
2557
2558 for (uint32_t i = 0; i < map.tu_count; ++i)
2559 {
2560 struct signatured_type *sig_type;
2561 void **slot;
2562
2563 sect_offset sect_off
2564 = (sect_offset) (extract_unsigned_integer
2565 (map.tu_table_reordered + i * map.offset_size,
2566 map.offset_size,
2567 map.dwarf5_byte_order));
2568
2569 comp_unit_head cu_header;
2570 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2571 abbrev_section,
2572 section->buffer + to_underlying (sect_off),
2573 rcuh_kind::TYPE);
2574
2575 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2576 struct signatured_type);
2577 sig_type->signature = cu_header.signature;
2578 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2579 sig_type->per_cu.is_debug_types = 1;
2580 sig_type->per_cu.section = section;
2581 sig_type->per_cu.sect_off = sect_off;
2582 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2583 sig_type->per_cu.v.quick
2584 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2585 struct dwarf2_per_cu_quick_data);
2586
2587 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2588 *slot = sig_type;
2589
2590 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2591 }
2592
2593 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2594 }
2595
2596 /* Read the address map data from the mapped index, and use it to
2597 populate the objfile's psymtabs_addrmap. */
2598
2599 static void
2600 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2601 struct mapped_index *index)
2602 {
2603 struct objfile *objfile = dwarf2_per_objfile->objfile;
2604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2605 const gdb_byte *iter, *end;
2606 struct addrmap *mutable_map;
2607 CORE_ADDR baseaddr;
2608
2609 auto_obstack temp_obstack;
2610
2611 mutable_map = addrmap_create_mutable (&temp_obstack);
2612
2613 iter = index->address_table.data ();
2614 end = iter + index->address_table.size ();
2615
2616 baseaddr = objfile->text_section_offset ();
2617
2618 while (iter < end)
2619 {
2620 ULONGEST hi, lo, cu_index;
2621 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2622 iter += 8;
2623 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2624 iter += 8;
2625 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2626 iter += 4;
2627
2628 if (lo > hi)
2629 {
2630 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2631 hex_string (lo), hex_string (hi));
2632 continue;
2633 }
2634
2635 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2636 {
2637 complaint (_(".gdb_index address table has invalid CU number %u"),
2638 (unsigned) cu_index);
2639 continue;
2640 }
2641
2642 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2643 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2644 addrmap_set_empty (mutable_map, lo, hi - 1,
2645 dwarf2_per_objfile->get_cu (cu_index));
2646 }
2647
2648 objfile->partial_symtabs->psymtabs_addrmap
2649 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2650 }
2651
2652 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2653 populate the objfile's psymtabs_addrmap. */
2654
2655 static void
2656 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2657 struct dwarf2_section_info *section)
2658 {
2659 struct objfile *objfile = dwarf2_per_objfile->objfile;
2660 bfd *abfd = objfile->obfd;
2661 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2662 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2663
2664 auto_obstack temp_obstack;
2665 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2666
2667 std::unordered_map<sect_offset,
2668 dwarf2_per_cu_data *,
2669 gdb::hash_enum<sect_offset>>
2670 debug_info_offset_to_per_cu;
2671 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2672 {
2673 const auto insertpair
2674 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2675 if (!insertpair.second)
2676 {
2677 warning (_("Section .debug_aranges in %s has duplicate "
2678 "debug_info_offset %s, ignoring .debug_aranges."),
2679 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2680 return;
2681 }
2682 }
2683
2684 section->read (objfile);
2685
2686 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2687
2688 const gdb_byte *addr = section->buffer;
2689
2690 while (addr < section->buffer + section->size)
2691 {
2692 const gdb_byte *const entry_addr = addr;
2693 unsigned int bytes_read;
2694
2695 const LONGEST entry_length = read_initial_length (abfd, addr,
2696 &bytes_read);
2697 addr += bytes_read;
2698
2699 const gdb_byte *const entry_end = addr + entry_length;
2700 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2701 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2702 if (addr + entry_length > section->buffer + section->size)
2703 {
2704 warning (_("Section .debug_aranges in %s entry at offset %s "
2705 "length %s exceeds section length %s, "
2706 "ignoring .debug_aranges."),
2707 objfile_name (objfile),
2708 plongest (entry_addr - section->buffer),
2709 plongest (bytes_read + entry_length),
2710 pulongest (section->size));
2711 return;
2712 }
2713
2714 /* The version number. */
2715 const uint16_t version = read_2_bytes (abfd, addr);
2716 addr += 2;
2717 if (version != 2)
2718 {
2719 warning (_("Section .debug_aranges in %s entry at offset %s "
2720 "has unsupported version %d, ignoring .debug_aranges."),
2721 objfile_name (objfile),
2722 plongest (entry_addr - section->buffer), version);
2723 return;
2724 }
2725
2726 const uint64_t debug_info_offset
2727 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2728 addr += offset_size;
2729 const auto per_cu_it
2730 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2731 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2732 {
2733 warning (_("Section .debug_aranges in %s entry at offset %s "
2734 "debug_info_offset %s does not exists, "
2735 "ignoring .debug_aranges."),
2736 objfile_name (objfile),
2737 plongest (entry_addr - section->buffer),
2738 pulongest (debug_info_offset));
2739 return;
2740 }
2741 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2742
2743 const uint8_t address_size = *addr++;
2744 if (address_size < 1 || address_size > 8)
2745 {
2746 warning (_("Section .debug_aranges in %s entry at offset %s "
2747 "address_size %u is invalid, ignoring .debug_aranges."),
2748 objfile_name (objfile),
2749 plongest (entry_addr - section->buffer), address_size);
2750 return;
2751 }
2752
2753 const uint8_t segment_selector_size = *addr++;
2754 if (segment_selector_size != 0)
2755 {
2756 warning (_("Section .debug_aranges in %s entry at offset %s "
2757 "segment_selector_size %u is not supported, "
2758 "ignoring .debug_aranges."),
2759 objfile_name (objfile),
2760 plongest (entry_addr - section->buffer),
2761 segment_selector_size);
2762 return;
2763 }
2764
2765 /* Must pad to an alignment boundary that is twice the address
2766 size. It is undocumented by the DWARF standard but GCC does
2767 use it. */
2768 for (size_t padding = ((-(addr - section->buffer))
2769 & (2 * address_size - 1));
2770 padding > 0; padding--)
2771 if (*addr++ != 0)
2772 {
2773 warning (_("Section .debug_aranges in %s entry at offset %s "
2774 "padding is not zero, ignoring .debug_aranges."),
2775 objfile_name (objfile),
2776 plongest (entry_addr - section->buffer));
2777 return;
2778 }
2779
2780 for (;;)
2781 {
2782 if (addr + 2 * address_size > entry_end)
2783 {
2784 warning (_("Section .debug_aranges in %s entry at offset %s "
2785 "address list is not properly terminated, "
2786 "ignoring .debug_aranges."),
2787 objfile_name (objfile),
2788 plongest (entry_addr - section->buffer));
2789 return;
2790 }
2791 ULONGEST start = extract_unsigned_integer (addr, address_size,
2792 dwarf5_byte_order);
2793 addr += address_size;
2794 ULONGEST length = extract_unsigned_integer (addr, address_size,
2795 dwarf5_byte_order);
2796 addr += address_size;
2797 if (start == 0 && length == 0)
2798 break;
2799 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2800 {
2801 /* Symbol was eliminated due to a COMDAT group. */
2802 continue;
2803 }
2804 ULONGEST end = start + length;
2805 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2806 - baseaddr);
2807 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2808 - baseaddr);
2809 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2810 }
2811 }
2812
2813 objfile->partial_symtabs->psymtabs_addrmap
2814 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2815 }
2816
2817 /* Find a slot in the mapped index INDEX for the object named NAME.
2818 If NAME is found, set *VEC_OUT to point to the CU vector in the
2819 constant pool and return true. If NAME cannot be found, return
2820 false. */
2821
2822 static bool
2823 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2824 offset_type **vec_out)
2825 {
2826 offset_type hash;
2827 offset_type slot, step;
2828 int (*cmp) (const char *, const char *);
2829
2830 gdb::unique_xmalloc_ptr<char> without_params;
2831 if (current_language->la_language == language_cplus
2832 || current_language->la_language == language_fortran
2833 || current_language->la_language == language_d)
2834 {
2835 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2836 not contain any. */
2837
2838 if (strchr (name, '(') != NULL)
2839 {
2840 without_params = cp_remove_params (name);
2841
2842 if (without_params != NULL)
2843 name = without_params.get ();
2844 }
2845 }
2846
2847 /* Index version 4 did not support case insensitive searches. But the
2848 indices for case insensitive languages are built in lowercase, therefore
2849 simulate our NAME being searched is also lowercased. */
2850 hash = mapped_index_string_hash ((index->version == 4
2851 && case_sensitivity == case_sensitive_off
2852 ? 5 : index->version),
2853 name);
2854
2855 slot = hash & (index->symbol_table.size () - 1);
2856 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2857 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2858
2859 for (;;)
2860 {
2861 const char *str;
2862
2863 const auto &bucket = index->symbol_table[slot];
2864 if (bucket.name == 0 && bucket.vec == 0)
2865 return false;
2866
2867 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2868 if (!cmp (name, str))
2869 {
2870 *vec_out = (offset_type *) (index->constant_pool
2871 + MAYBE_SWAP (bucket.vec));
2872 return true;
2873 }
2874
2875 slot = (slot + step) & (index->symbol_table.size () - 1);
2876 }
2877 }
2878
2879 /* A helper function that reads the .gdb_index from BUFFER and fills
2880 in MAP. FILENAME is the name of the file containing the data;
2881 it is used for error reporting. DEPRECATED_OK is true if it is
2882 ok to use deprecated sections.
2883
2884 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2885 out parameters that are filled in with information about the CU and
2886 TU lists in the section.
2887
2888 Returns true if all went well, false otherwise. */
2889
2890 static bool
2891 read_gdb_index_from_buffer (struct objfile *objfile,
2892 const char *filename,
2893 bool deprecated_ok,
2894 gdb::array_view<const gdb_byte> buffer,
2895 struct mapped_index *map,
2896 const gdb_byte **cu_list,
2897 offset_type *cu_list_elements,
2898 const gdb_byte **types_list,
2899 offset_type *types_list_elements)
2900 {
2901 const gdb_byte *addr = &buffer[0];
2902
2903 /* Version check. */
2904 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2905 /* Versions earlier than 3 emitted every copy of a psymbol. This
2906 causes the index to behave very poorly for certain requests. Version 3
2907 contained incomplete addrmap. So, it seems better to just ignore such
2908 indices. */
2909 if (version < 4)
2910 {
2911 static int warning_printed = 0;
2912 if (!warning_printed)
2913 {
2914 warning (_("Skipping obsolete .gdb_index section in %s."),
2915 filename);
2916 warning_printed = 1;
2917 }
2918 return 0;
2919 }
2920 /* Index version 4 uses a different hash function than index version
2921 5 and later.
2922
2923 Versions earlier than 6 did not emit psymbols for inlined
2924 functions. Using these files will cause GDB not to be able to
2925 set breakpoints on inlined functions by name, so we ignore these
2926 indices unless the user has done
2927 "set use-deprecated-index-sections on". */
2928 if (version < 6 && !deprecated_ok)
2929 {
2930 static int warning_printed = 0;
2931 if (!warning_printed)
2932 {
2933 warning (_("\
2934 Skipping deprecated .gdb_index section in %s.\n\
2935 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2936 to use the section anyway."),
2937 filename);
2938 warning_printed = 1;
2939 }
2940 return 0;
2941 }
2942 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2943 of the TU (for symbols coming from TUs),
2944 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2945 Plus gold-generated indices can have duplicate entries for global symbols,
2946 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2947 These are just performance bugs, and we can't distinguish gdb-generated
2948 indices from gold-generated ones, so issue no warning here. */
2949
2950 /* Indexes with higher version than the one supported by GDB may be no
2951 longer backward compatible. */
2952 if (version > 8)
2953 return 0;
2954
2955 map->version = version;
2956
2957 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2958
2959 int i = 0;
2960 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2961 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2962 / 8);
2963 ++i;
2964
2965 *types_list = addr + MAYBE_SWAP (metadata[i]);
2966 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2967 - MAYBE_SWAP (metadata[i]))
2968 / 8);
2969 ++i;
2970
2971 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2972 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2973 map->address_table
2974 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2975 ++i;
2976
2977 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2978 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2979 map->symbol_table
2980 = gdb::array_view<mapped_index::symbol_table_slot>
2981 ((mapped_index::symbol_table_slot *) symbol_table,
2982 (mapped_index::symbol_table_slot *) symbol_table_end);
2983
2984 ++i;
2985 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2986
2987 return 1;
2988 }
2989
2990 /* Callback types for dwarf2_read_gdb_index. */
2991
2992 typedef gdb::function_view
2993 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2994 get_gdb_index_contents_ftype;
2995 typedef gdb::function_view
2996 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2997 get_gdb_index_contents_dwz_ftype;
2998
2999 /* Read .gdb_index. If everything went ok, initialize the "quick"
3000 elements of all the CUs and return 1. Otherwise, return 0. */
3001
3002 static int
3003 dwarf2_read_gdb_index
3004 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3005 get_gdb_index_contents_ftype get_gdb_index_contents,
3006 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3007 {
3008 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3009 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3010 struct dwz_file *dwz;
3011 struct objfile *objfile = dwarf2_per_objfile->objfile;
3012
3013 gdb::array_view<const gdb_byte> main_index_contents
3014 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3015
3016 if (main_index_contents.empty ())
3017 return 0;
3018
3019 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3020 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3021 use_deprecated_index_sections,
3022 main_index_contents, map.get (), &cu_list,
3023 &cu_list_elements, &types_list,
3024 &types_list_elements))
3025 return 0;
3026
3027 /* Don't use the index if it's empty. */
3028 if (map->symbol_table.empty ())
3029 return 0;
3030
3031 /* If there is a .dwz file, read it so we can get its CU list as
3032 well. */
3033 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3034 if (dwz != NULL)
3035 {
3036 struct mapped_index dwz_map;
3037 const gdb_byte *dwz_types_ignore;
3038 offset_type dwz_types_elements_ignore;
3039
3040 gdb::array_view<const gdb_byte> dwz_index_content
3041 = get_gdb_index_contents_dwz (objfile, dwz);
3042
3043 if (dwz_index_content.empty ())
3044 return 0;
3045
3046 if (!read_gdb_index_from_buffer (objfile,
3047 bfd_get_filename (dwz->dwz_bfd.get ()),
3048 1, dwz_index_content, &dwz_map,
3049 &dwz_list, &dwz_list_elements,
3050 &dwz_types_ignore,
3051 &dwz_types_elements_ignore))
3052 {
3053 warning (_("could not read '.gdb_index' section from %s; skipping"),
3054 bfd_get_filename (dwz->dwz_bfd.get ()));
3055 return 0;
3056 }
3057 }
3058
3059 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3060 dwz_list, dwz_list_elements);
3061
3062 if (types_list_elements)
3063 {
3064 /* We can only handle a single .debug_types when we have an
3065 index. */
3066 if (dwarf2_per_objfile->types.size () != 1)
3067 return 0;
3068
3069 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3070
3071 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3072 types_list, types_list_elements);
3073 }
3074
3075 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3076
3077 dwarf2_per_objfile->index_table = std::move (map);
3078 dwarf2_per_objfile->using_index = 1;
3079 dwarf2_per_objfile->quick_file_names_table =
3080 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3081
3082 return 1;
3083 }
3084
3085 /* die_reader_func for dw2_get_file_names. */
3086
3087 static void
3088 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3089 const gdb_byte *info_ptr,
3090 struct die_info *comp_unit_die)
3091 {
3092 struct dwarf2_cu *cu = reader->cu;
3093 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3094 struct dwarf2_per_objfile *dwarf2_per_objfile
3095 = cu->per_cu->dwarf2_per_objfile;
3096 struct objfile *objfile = dwarf2_per_objfile->objfile;
3097 struct dwarf2_per_cu_data *lh_cu;
3098 struct attribute *attr;
3099 void **slot;
3100 struct quick_file_names *qfn;
3101
3102 gdb_assert (! this_cu->is_debug_types);
3103
3104 /* Our callers never want to match partial units -- instead they
3105 will match the enclosing full CU. */
3106 if (comp_unit_die->tag == DW_TAG_partial_unit)
3107 {
3108 this_cu->v.quick->no_file_data = 1;
3109 return;
3110 }
3111
3112 lh_cu = this_cu;
3113 slot = NULL;
3114
3115 line_header_up lh;
3116 sect_offset line_offset {};
3117
3118 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3119 if (attr != nullptr)
3120 {
3121 struct quick_file_names find_entry;
3122
3123 line_offset = (sect_offset) DW_UNSND (attr);
3124
3125 /* We may have already read in this line header (TU line header sharing).
3126 If we have we're done. */
3127 find_entry.hash.dwo_unit = cu->dwo_unit;
3128 find_entry.hash.line_sect_off = line_offset;
3129 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3130 &find_entry, INSERT);
3131 if (*slot != NULL)
3132 {
3133 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3134 return;
3135 }
3136
3137 lh = dwarf_decode_line_header (line_offset, cu);
3138 }
3139 if (lh == NULL)
3140 {
3141 lh_cu->v.quick->no_file_data = 1;
3142 return;
3143 }
3144
3145 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3146 qfn->hash.dwo_unit = cu->dwo_unit;
3147 qfn->hash.line_sect_off = line_offset;
3148 gdb_assert (slot != NULL);
3149 *slot = qfn;
3150
3151 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3152
3153 int offset = 0;
3154 if (strcmp (fnd.name, "<unknown>") != 0)
3155 ++offset;
3156
3157 qfn->num_file_names = offset + lh->file_names_size ();
3158 qfn->file_names =
3159 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3160 if (offset != 0)
3161 qfn->file_names[0] = xstrdup (fnd.name);
3162 for (int i = 0; i < lh->file_names_size (); ++i)
3163 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3164 fnd.comp_dir).release ();
3165 qfn->real_names = NULL;
3166
3167 lh_cu->v.quick->file_names = qfn;
3168 }
3169
3170 /* A helper for the "quick" functions which attempts to read the line
3171 table for THIS_CU. */
3172
3173 static struct quick_file_names *
3174 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3175 {
3176 /* This should never be called for TUs. */
3177 gdb_assert (! this_cu->is_debug_types);
3178 /* Nor type unit groups. */
3179 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3180
3181 if (this_cu->v.quick->file_names != NULL)
3182 return this_cu->v.quick->file_names;
3183 /* If we know there is no line data, no point in looking again. */
3184 if (this_cu->v.quick->no_file_data)
3185 return NULL;
3186
3187 cutu_reader reader (this_cu);
3188 if (!reader.dummy_p)
3189 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3190
3191 if (this_cu->v.quick->no_file_data)
3192 return NULL;
3193 return this_cu->v.quick->file_names;
3194 }
3195
3196 /* A helper for the "quick" functions which computes and caches the
3197 real path for a given file name from the line table. */
3198
3199 static const char *
3200 dw2_get_real_path (struct objfile *objfile,
3201 struct quick_file_names *qfn, int index)
3202 {
3203 if (qfn->real_names == NULL)
3204 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3205 qfn->num_file_names, const char *);
3206
3207 if (qfn->real_names[index] == NULL)
3208 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3209
3210 return qfn->real_names[index];
3211 }
3212
3213 static struct symtab *
3214 dw2_find_last_source_symtab (struct objfile *objfile)
3215 {
3216 struct dwarf2_per_objfile *dwarf2_per_objfile
3217 = get_dwarf2_per_objfile (objfile);
3218 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3219 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3220
3221 if (cust == NULL)
3222 return NULL;
3223
3224 return compunit_primary_filetab (cust);
3225 }
3226
3227 /* Traversal function for dw2_forget_cached_source_info. */
3228
3229 static int
3230 dw2_free_cached_file_names (void **slot, void *info)
3231 {
3232 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3233
3234 if (file_data->real_names)
3235 {
3236 int i;
3237
3238 for (i = 0; i < file_data->num_file_names; ++i)
3239 {
3240 xfree ((void*) file_data->real_names[i]);
3241 file_data->real_names[i] = NULL;
3242 }
3243 }
3244
3245 return 1;
3246 }
3247
3248 static void
3249 dw2_forget_cached_source_info (struct objfile *objfile)
3250 {
3251 struct dwarf2_per_objfile *dwarf2_per_objfile
3252 = get_dwarf2_per_objfile (objfile);
3253
3254 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3255 dw2_free_cached_file_names, NULL);
3256 }
3257
3258 /* Helper function for dw2_map_symtabs_matching_filename that expands
3259 the symtabs and calls the iterator. */
3260
3261 static int
3262 dw2_map_expand_apply (struct objfile *objfile,
3263 struct dwarf2_per_cu_data *per_cu,
3264 const char *name, const char *real_path,
3265 gdb::function_view<bool (symtab *)> callback)
3266 {
3267 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3268
3269 /* Don't visit already-expanded CUs. */
3270 if (per_cu->v.quick->compunit_symtab)
3271 return 0;
3272
3273 /* This may expand more than one symtab, and we want to iterate over
3274 all of them. */
3275 dw2_instantiate_symtab (per_cu, false);
3276
3277 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3278 last_made, callback);
3279 }
3280
3281 /* Implementation of the map_symtabs_matching_filename method. */
3282
3283 static bool
3284 dw2_map_symtabs_matching_filename
3285 (struct objfile *objfile, const char *name, const char *real_path,
3286 gdb::function_view<bool (symtab *)> callback)
3287 {
3288 const char *name_basename = lbasename (name);
3289 struct dwarf2_per_objfile *dwarf2_per_objfile
3290 = get_dwarf2_per_objfile (objfile);
3291
3292 /* The rule is CUs specify all the files, including those used by
3293 any TU, so there's no need to scan TUs here. */
3294
3295 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3296 {
3297 /* We only need to look at symtabs not already expanded. */
3298 if (per_cu->v.quick->compunit_symtab)
3299 continue;
3300
3301 quick_file_names *file_data = dw2_get_file_names (per_cu);
3302 if (file_data == NULL)
3303 continue;
3304
3305 for (int j = 0; j < file_data->num_file_names; ++j)
3306 {
3307 const char *this_name = file_data->file_names[j];
3308 const char *this_real_name;
3309
3310 if (compare_filenames_for_search (this_name, name))
3311 {
3312 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3313 callback))
3314 return true;
3315 continue;
3316 }
3317
3318 /* Before we invoke realpath, which can get expensive when many
3319 files are involved, do a quick comparison of the basenames. */
3320 if (! basenames_may_differ
3321 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3322 continue;
3323
3324 this_real_name = dw2_get_real_path (objfile, file_data, j);
3325 if (compare_filenames_for_search (this_real_name, name))
3326 {
3327 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3328 callback))
3329 return true;
3330 continue;
3331 }
3332
3333 if (real_path != NULL)
3334 {
3335 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3336 gdb_assert (IS_ABSOLUTE_PATH (name));
3337 if (this_real_name != NULL
3338 && FILENAME_CMP (real_path, this_real_name) == 0)
3339 {
3340 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3341 callback))
3342 return true;
3343 continue;
3344 }
3345 }
3346 }
3347 }
3348
3349 return false;
3350 }
3351
3352 /* Struct used to manage iterating over all CUs looking for a symbol. */
3353
3354 struct dw2_symtab_iterator
3355 {
3356 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3357 struct dwarf2_per_objfile *dwarf2_per_objfile;
3358 /* If set, only look for symbols that match that block. Valid values are
3359 GLOBAL_BLOCK and STATIC_BLOCK. */
3360 gdb::optional<block_enum> block_index;
3361 /* The kind of symbol we're looking for. */
3362 domain_enum domain;
3363 /* The list of CUs from the index entry of the symbol,
3364 or NULL if not found. */
3365 offset_type *vec;
3366 /* The next element in VEC to look at. */
3367 int next;
3368 /* The number of elements in VEC, or zero if there is no match. */
3369 int length;
3370 /* Have we seen a global version of the symbol?
3371 If so we can ignore all further global instances.
3372 This is to work around gold/15646, inefficient gold-generated
3373 indices. */
3374 int global_seen;
3375 };
3376
3377 /* Initialize the index symtab iterator ITER. */
3378
3379 static void
3380 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3381 struct dwarf2_per_objfile *dwarf2_per_objfile,
3382 gdb::optional<block_enum> block_index,
3383 domain_enum domain,
3384 const char *name)
3385 {
3386 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3387 iter->block_index = block_index;
3388 iter->domain = domain;
3389 iter->next = 0;
3390 iter->global_seen = 0;
3391
3392 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3393
3394 /* index is NULL if OBJF_READNOW. */
3395 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3396 iter->length = MAYBE_SWAP (*iter->vec);
3397 else
3398 {
3399 iter->vec = NULL;
3400 iter->length = 0;
3401 }
3402 }
3403
3404 /* Return the next matching CU or NULL if there are no more. */
3405
3406 static struct dwarf2_per_cu_data *
3407 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3408 {
3409 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3410
3411 for ( ; iter->next < iter->length; ++iter->next)
3412 {
3413 offset_type cu_index_and_attrs =
3414 MAYBE_SWAP (iter->vec[iter->next + 1]);
3415 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3416 gdb_index_symbol_kind symbol_kind =
3417 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3418 /* Only check the symbol attributes if they're present.
3419 Indices prior to version 7 don't record them,
3420 and indices >= 7 may elide them for certain symbols
3421 (gold does this). */
3422 int attrs_valid =
3423 (dwarf2_per_objfile->index_table->version >= 7
3424 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3425
3426 /* Don't crash on bad data. */
3427 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3428 + dwarf2_per_objfile->all_type_units.size ()))
3429 {
3430 complaint (_(".gdb_index entry has bad CU index"
3431 " [in module %s]"),
3432 objfile_name (dwarf2_per_objfile->objfile));
3433 continue;
3434 }
3435
3436 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3437
3438 /* Skip if already read in. */
3439 if (per_cu->v.quick->compunit_symtab)
3440 continue;
3441
3442 /* Check static vs global. */
3443 if (attrs_valid)
3444 {
3445 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3446
3447 if (iter->block_index.has_value ())
3448 {
3449 bool want_static = *iter->block_index == STATIC_BLOCK;
3450
3451 if (is_static != want_static)
3452 continue;
3453 }
3454
3455 /* Work around gold/15646. */
3456 if (!is_static && iter->global_seen)
3457 continue;
3458 if (!is_static)
3459 iter->global_seen = 1;
3460 }
3461
3462 /* Only check the symbol's kind if it has one. */
3463 if (attrs_valid)
3464 {
3465 switch (iter->domain)
3466 {
3467 case VAR_DOMAIN:
3468 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3469 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3470 /* Some types are also in VAR_DOMAIN. */
3471 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3472 continue;
3473 break;
3474 case STRUCT_DOMAIN:
3475 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3476 continue;
3477 break;
3478 case LABEL_DOMAIN:
3479 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3480 continue;
3481 break;
3482 case MODULE_DOMAIN:
3483 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3484 continue;
3485 break;
3486 default:
3487 break;
3488 }
3489 }
3490
3491 ++iter->next;
3492 return per_cu;
3493 }
3494
3495 return NULL;
3496 }
3497
3498 static struct compunit_symtab *
3499 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3500 const char *name, domain_enum domain)
3501 {
3502 struct compunit_symtab *stab_best = NULL;
3503 struct dwarf2_per_objfile *dwarf2_per_objfile
3504 = get_dwarf2_per_objfile (objfile);
3505
3506 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3507
3508 struct dw2_symtab_iterator iter;
3509 struct dwarf2_per_cu_data *per_cu;
3510
3511 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3512
3513 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3514 {
3515 struct symbol *sym, *with_opaque = NULL;
3516 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3517 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3518 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3519
3520 sym = block_find_symbol (block, name, domain,
3521 block_find_non_opaque_type_preferred,
3522 &with_opaque);
3523
3524 /* Some caution must be observed with overloaded functions
3525 and methods, since the index will not contain any overload
3526 information (but NAME might contain it). */
3527
3528 if (sym != NULL
3529 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3530 return stab;
3531 if (with_opaque != NULL
3532 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3533 stab_best = stab;
3534
3535 /* Keep looking through other CUs. */
3536 }
3537
3538 return stab_best;
3539 }
3540
3541 static void
3542 dw2_print_stats (struct objfile *objfile)
3543 {
3544 struct dwarf2_per_objfile *dwarf2_per_objfile
3545 = get_dwarf2_per_objfile (objfile);
3546 int total = (dwarf2_per_objfile->all_comp_units.size ()
3547 + dwarf2_per_objfile->all_type_units.size ());
3548 int count = 0;
3549
3550 for (int i = 0; i < total; ++i)
3551 {
3552 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3553
3554 if (!per_cu->v.quick->compunit_symtab)
3555 ++count;
3556 }
3557 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3558 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3559 }
3560
3561 /* This dumps minimal information about the index.
3562 It is called via "mt print objfiles".
3563 One use is to verify .gdb_index has been loaded by the
3564 gdb.dwarf2/gdb-index.exp testcase. */
3565
3566 static void
3567 dw2_dump (struct objfile *objfile)
3568 {
3569 struct dwarf2_per_objfile *dwarf2_per_objfile
3570 = get_dwarf2_per_objfile (objfile);
3571
3572 gdb_assert (dwarf2_per_objfile->using_index);
3573 printf_filtered (".gdb_index:");
3574 if (dwarf2_per_objfile->index_table != NULL)
3575 {
3576 printf_filtered (" version %d\n",
3577 dwarf2_per_objfile->index_table->version);
3578 }
3579 else
3580 printf_filtered (" faked for \"readnow\"\n");
3581 printf_filtered ("\n");
3582 }
3583
3584 static void
3585 dw2_expand_symtabs_for_function (struct objfile *objfile,
3586 const char *func_name)
3587 {
3588 struct dwarf2_per_objfile *dwarf2_per_objfile
3589 = get_dwarf2_per_objfile (objfile);
3590
3591 struct dw2_symtab_iterator iter;
3592 struct dwarf2_per_cu_data *per_cu;
3593
3594 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3595
3596 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3597 dw2_instantiate_symtab (per_cu, false);
3598
3599 }
3600
3601 static void
3602 dw2_expand_all_symtabs (struct objfile *objfile)
3603 {
3604 struct dwarf2_per_objfile *dwarf2_per_objfile
3605 = get_dwarf2_per_objfile (objfile);
3606 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3607 + dwarf2_per_objfile->all_type_units.size ());
3608
3609 for (int i = 0; i < total_units; ++i)
3610 {
3611 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3612
3613 /* We don't want to directly expand a partial CU, because if we
3614 read it with the wrong language, then assertion failures can
3615 be triggered later on. See PR symtab/23010. So, tell
3616 dw2_instantiate_symtab to skip partial CUs -- any important
3617 partial CU will be read via DW_TAG_imported_unit anyway. */
3618 dw2_instantiate_symtab (per_cu, true);
3619 }
3620 }
3621
3622 static void
3623 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3624 const char *fullname)
3625 {
3626 struct dwarf2_per_objfile *dwarf2_per_objfile
3627 = get_dwarf2_per_objfile (objfile);
3628
3629 /* We don't need to consider type units here.
3630 This is only called for examining code, e.g. expand_line_sal.
3631 There can be an order of magnitude (or more) more type units
3632 than comp units, and we avoid them if we can. */
3633
3634 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3635 {
3636 /* We only need to look at symtabs not already expanded. */
3637 if (per_cu->v.quick->compunit_symtab)
3638 continue;
3639
3640 quick_file_names *file_data = dw2_get_file_names (per_cu);
3641 if (file_data == NULL)
3642 continue;
3643
3644 for (int j = 0; j < file_data->num_file_names; ++j)
3645 {
3646 const char *this_fullname = file_data->file_names[j];
3647
3648 if (filename_cmp (this_fullname, fullname) == 0)
3649 {
3650 dw2_instantiate_symtab (per_cu, false);
3651 break;
3652 }
3653 }
3654 }
3655 }
3656
3657 static void
3658 dw2_map_matching_symbols
3659 (struct objfile *objfile,
3660 const lookup_name_info &name, domain_enum domain,
3661 int global,
3662 gdb::function_view<symbol_found_callback_ftype> callback,
3663 symbol_compare_ftype *ordered_compare)
3664 {
3665 /* Currently unimplemented; used for Ada. The function can be called if the
3666 current language is Ada for a non-Ada objfile using GNU index. As Ada
3667 does not look for non-Ada symbols this function should just return. */
3668 }
3669
3670 /* Starting from a search name, return the string that finds the upper
3671 bound of all strings that start with SEARCH_NAME in a sorted name
3672 list. Returns the empty string to indicate that the upper bound is
3673 the end of the list. */
3674
3675 static std::string
3676 make_sort_after_prefix_name (const char *search_name)
3677 {
3678 /* When looking to complete "func", we find the upper bound of all
3679 symbols that start with "func" by looking for where we'd insert
3680 the closest string that would follow "func" in lexicographical
3681 order. Usually, that's "func"-with-last-character-incremented,
3682 i.e. "fund". Mind non-ASCII characters, though. Usually those
3683 will be UTF-8 multi-byte sequences, but we can't be certain.
3684 Especially mind the 0xff character, which is a valid character in
3685 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3686 rule out compilers allowing it in identifiers. Note that
3687 conveniently, strcmp/strcasecmp are specified to compare
3688 characters interpreted as unsigned char. So what we do is treat
3689 the whole string as a base 256 number composed of a sequence of
3690 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3691 to 0, and carries 1 to the following more-significant position.
3692 If the very first character in SEARCH_NAME ends up incremented
3693 and carries/overflows, then the upper bound is the end of the
3694 list. The string after the empty string is also the empty
3695 string.
3696
3697 Some examples of this operation:
3698
3699 SEARCH_NAME => "+1" RESULT
3700
3701 "abc" => "abd"
3702 "ab\xff" => "ac"
3703 "\xff" "a" "\xff" => "\xff" "b"
3704 "\xff" => ""
3705 "\xff\xff" => ""
3706 "" => ""
3707
3708 Then, with these symbols for example:
3709
3710 func
3711 func1
3712 fund
3713
3714 completing "func" looks for symbols between "func" and
3715 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3716 which finds "func" and "func1", but not "fund".
3717
3718 And with:
3719
3720 funcÿ (Latin1 'ÿ' [0xff])
3721 funcÿ1
3722 fund
3723
3724 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3725 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3726
3727 And with:
3728
3729 ÿÿ (Latin1 'ÿ' [0xff])
3730 ÿÿ1
3731
3732 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3733 the end of the list.
3734 */
3735 std::string after = search_name;
3736 while (!after.empty () && (unsigned char) after.back () == 0xff)
3737 after.pop_back ();
3738 if (!after.empty ())
3739 after.back () = (unsigned char) after.back () + 1;
3740 return after;
3741 }
3742
3743 /* See declaration. */
3744
3745 std::pair<std::vector<name_component>::const_iterator,
3746 std::vector<name_component>::const_iterator>
3747 mapped_index_base::find_name_components_bounds
3748 (const lookup_name_info &lookup_name_without_params, language lang) const
3749 {
3750 auto *name_cmp
3751 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3752
3753 const char *lang_name
3754 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3755
3756 /* Comparison function object for lower_bound that matches against a
3757 given symbol name. */
3758 auto lookup_compare_lower = [&] (const name_component &elem,
3759 const char *name)
3760 {
3761 const char *elem_qualified = this->symbol_name_at (elem.idx);
3762 const char *elem_name = elem_qualified + elem.name_offset;
3763 return name_cmp (elem_name, name) < 0;
3764 };
3765
3766 /* Comparison function object for upper_bound that matches against a
3767 given symbol name. */
3768 auto lookup_compare_upper = [&] (const char *name,
3769 const name_component &elem)
3770 {
3771 const char *elem_qualified = this->symbol_name_at (elem.idx);
3772 const char *elem_name = elem_qualified + elem.name_offset;
3773 return name_cmp (name, elem_name) < 0;
3774 };
3775
3776 auto begin = this->name_components.begin ();
3777 auto end = this->name_components.end ();
3778
3779 /* Find the lower bound. */
3780 auto lower = [&] ()
3781 {
3782 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3783 return begin;
3784 else
3785 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3786 } ();
3787
3788 /* Find the upper bound. */
3789 auto upper = [&] ()
3790 {
3791 if (lookup_name_without_params.completion_mode ())
3792 {
3793 /* In completion mode, we want UPPER to point past all
3794 symbols names that have the same prefix. I.e., with
3795 these symbols, and completing "func":
3796
3797 function << lower bound
3798 function1
3799 other_function << upper bound
3800
3801 We find the upper bound by looking for the insertion
3802 point of "func"-with-last-character-incremented,
3803 i.e. "fund". */
3804 std::string after = make_sort_after_prefix_name (lang_name);
3805 if (after.empty ())
3806 return end;
3807 return std::lower_bound (lower, end, after.c_str (),
3808 lookup_compare_lower);
3809 }
3810 else
3811 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3812 } ();
3813
3814 return {lower, upper};
3815 }
3816
3817 /* See declaration. */
3818
3819 void
3820 mapped_index_base::build_name_components ()
3821 {
3822 if (!this->name_components.empty ())
3823 return;
3824
3825 this->name_components_casing = case_sensitivity;
3826 auto *name_cmp
3827 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3828
3829 /* The code below only knows how to break apart components of C++
3830 symbol names (and other languages that use '::' as
3831 namespace/module separator) and Ada symbol names. */
3832 auto count = this->symbol_name_count ();
3833 for (offset_type idx = 0; idx < count; idx++)
3834 {
3835 if (this->symbol_name_slot_invalid (idx))
3836 continue;
3837
3838 const char *name = this->symbol_name_at (idx);
3839
3840 /* Add each name component to the name component table. */
3841 unsigned int previous_len = 0;
3842
3843 if (strstr (name, "::") != nullptr)
3844 {
3845 for (unsigned int current_len = cp_find_first_component (name);
3846 name[current_len] != '\0';
3847 current_len += cp_find_first_component (name + current_len))
3848 {
3849 gdb_assert (name[current_len] == ':');
3850 this->name_components.push_back ({previous_len, idx});
3851 /* Skip the '::'. */
3852 current_len += 2;
3853 previous_len = current_len;
3854 }
3855 }
3856 else
3857 {
3858 /* Handle the Ada encoded (aka mangled) form here. */
3859 for (const char *iter = strstr (name, "__");
3860 iter != nullptr;
3861 iter = strstr (iter, "__"))
3862 {
3863 this->name_components.push_back ({previous_len, idx});
3864 iter += 2;
3865 previous_len = iter - name;
3866 }
3867 }
3868
3869 this->name_components.push_back ({previous_len, idx});
3870 }
3871
3872 /* Sort name_components elements by name. */
3873 auto name_comp_compare = [&] (const name_component &left,
3874 const name_component &right)
3875 {
3876 const char *left_qualified = this->symbol_name_at (left.idx);
3877 const char *right_qualified = this->symbol_name_at (right.idx);
3878
3879 const char *left_name = left_qualified + left.name_offset;
3880 const char *right_name = right_qualified + right.name_offset;
3881
3882 return name_cmp (left_name, right_name) < 0;
3883 };
3884
3885 std::sort (this->name_components.begin (),
3886 this->name_components.end (),
3887 name_comp_compare);
3888 }
3889
3890 /* Helper for dw2_expand_symtabs_matching that works with a
3891 mapped_index_base instead of the containing objfile. This is split
3892 to a separate function in order to be able to unit test the
3893 name_components matching using a mock mapped_index_base. For each
3894 symbol name that matches, calls MATCH_CALLBACK, passing it the
3895 symbol's index in the mapped_index_base symbol table. */
3896
3897 static void
3898 dw2_expand_symtabs_matching_symbol
3899 (mapped_index_base &index,
3900 const lookup_name_info &lookup_name_in,
3901 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3902 enum search_domain kind,
3903 gdb::function_view<bool (offset_type)> match_callback)
3904 {
3905 lookup_name_info lookup_name_without_params
3906 = lookup_name_in.make_ignore_params ();
3907
3908 /* Build the symbol name component sorted vector, if we haven't
3909 yet. */
3910 index.build_name_components ();
3911
3912 /* The same symbol may appear more than once in the range though.
3913 E.g., if we're looking for symbols that complete "w", and we have
3914 a symbol named "w1::w2", we'll find the two name components for
3915 that same symbol in the range. To be sure we only call the
3916 callback once per symbol, we first collect the symbol name
3917 indexes that matched in a temporary vector and ignore
3918 duplicates. */
3919 std::vector<offset_type> matches;
3920
3921 struct name_and_matcher
3922 {
3923 symbol_name_matcher_ftype *matcher;
3924 const std::string &name;
3925
3926 bool operator== (const name_and_matcher &other) const
3927 {
3928 return matcher == other.matcher && name == other.name;
3929 }
3930 };
3931
3932 /* A vector holding all the different symbol name matchers, for all
3933 languages. */
3934 std::vector<name_and_matcher> matchers;
3935
3936 for (int i = 0; i < nr_languages; i++)
3937 {
3938 enum language lang_e = (enum language) i;
3939
3940 const language_defn *lang = language_def (lang_e);
3941 symbol_name_matcher_ftype *name_matcher
3942 = get_symbol_name_matcher (lang, lookup_name_without_params);
3943
3944 name_and_matcher key {
3945 name_matcher,
3946 lookup_name_without_params.language_lookup_name (lang_e)
3947 };
3948
3949 /* Don't insert the same comparison routine more than once.
3950 Note that we do this linear walk. This is not a problem in
3951 practice because the number of supported languages is
3952 low. */
3953 if (std::find (matchers.begin (), matchers.end (), key)
3954 != matchers.end ())
3955 continue;
3956 matchers.push_back (std::move (key));
3957
3958 auto bounds
3959 = index.find_name_components_bounds (lookup_name_without_params,
3960 lang_e);
3961
3962 /* Now for each symbol name in range, check to see if we have a name
3963 match, and if so, call the MATCH_CALLBACK callback. */
3964
3965 for (; bounds.first != bounds.second; ++bounds.first)
3966 {
3967 const char *qualified = index.symbol_name_at (bounds.first->idx);
3968
3969 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3970 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3971 continue;
3972
3973 matches.push_back (bounds.first->idx);
3974 }
3975 }
3976
3977 std::sort (matches.begin (), matches.end ());
3978
3979 /* Finally call the callback, once per match. */
3980 ULONGEST prev = -1;
3981 for (offset_type idx : matches)
3982 {
3983 if (prev != idx)
3984 {
3985 if (!match_callback (idx))
3986 break;
3987 prev = idx;
3988 }
3989 }
3990
3991 /* Above we use a type wider than idx's for 'prev', since 0 and
3992 (offset_type)-1 are both possible values. */
3993 static_assert (sizeof (prev) > sizeof (offset_type), "");
3994 }
3995
3996 #if GDB_SELF_TEST
3997
3998 namespace selftests { namespace dw2_expand_symtabs_matching {
3999
4000 /* A mock .gdb_index/.debug_names-like name index table, enough to
4001 exercise dw2_expand_symtabs_matching_symbol, which works with the
4002 mapped_index_base interface. Builds an index from the symbol list
4003 passed as parameter to the constructor. */
4004 class mock_mapped_index : public mapped_index_base
4005 {
4006 public:
4007 mock_mapped_index (gdb::array_view<const char *> symbols)
4008 : m_symbol_table (symbols)
4009 {}
4010
4011 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4012
4013 /* Return the number of names in the symbol table. */
4014 size_t symbol_name_count () const override
4015 {
4016 return m_symbol_table.size ();
4017 }
4018
4019 /* Get the name of the symbol at IDX in the symbol table. */
4020 const char *symbol_name_at (offset_type idx) const override
4021 {
4022 return m_symbol_table[idx];
4023 }
4024
4025 private:
4026 gdb::array_view<const char *> m_symbol_table;
4027 };
4028
4029 /* Convenience function that converts a NULL pointer to a "<null>"
4030 string, to pass to print routines. */
4031
4032 static const char *
4033 string_or_null (const char *str)
4034 {
4035 return str != NULL ? str : "<null>";
4036 }
4037
4038 /* Check if a lookup_name_info built from
4039 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4040 index. EXPECTED_LIST is the list of expected matches, in expected
4041 matching order. If no match expected, then an empty list is
4042 specified. Returns true on success. On failure prints a warning
4043 indicating the file:line that failed, and returns false. */
4044
4045 static bool
4046 check_match (const char *file, int line,
4047 mock_mapped_index &mock_index,
4048 const char *name, symbol_name_match_type match_type,
4049 bool completion_mode,
4050 std::initializer_list<const char *> expected_list)
4051 {
4052 lookup_name_info lookup_name (name, match_type, completion_mode);
4053
4054 bool matched = true;
4055
4056 auto mismatch = [&] (const char *expected_str,
4057 const char *got)
4058 {
4059 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4060 "expected=\"%s\", got=\"%s\"\n"),
4061 file, line,
4062 (match_type == symbol_name_match_type::FULL
4063 ? "FULL" : "WILD"),
4064 name, string_or_null (expected_str), string_or_null (got));
4065 matched = false;
4066 };
4067
4068 auto expected_it = expected_list.begin ();
4069 auto expected_end = expected_list.end ();
4070
4071 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4072 NULL, ALL_DOMAIN,
4073 [&] (offset_type idx)
4074 {
4075 const char *matched_name = mock_index.symbol_name_at (idx);
4076 const char *expected_str
4077 = expected_it == expected_end ? NULL : *expected_it++;
4078
4079 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4080 mismatch (expected_str, matched_name);
4081 return true;
4082 });
4083
4084 const char *expected_str
4085 = expected_it == expected_end ? NULL : *expected_it++;
4086 if (expected_str != NULL)
4087 mismatch (expected_str, NULL);
4088
4089 return matched;
4090 }
4091
4092 /* The symbols added to the mock mapped_index for testing (in
4093 canonical form). */
4094 static const char *test_symbols[] = {
4095 "function",
4096 "std::bar",
4097 "std::zfunction",
4098 "std::zfunction2",
4099 "w1::w2",
4100 "ns::foo<char*>",
4101 "ns::foo<int>",
4102 "ns::foo<long>",
4103 "ns2::tmpl<int>::foo2",
4104 "(anonymous namespace)::A::B::C",
4105
4106 /* These are used to check that the increment-last-char in the
4107 matching algorithm for completion doesn't match "t1_fund" when
4108 completing "t1_func". */
4109 "t1_func",
4110 "t1_func1",
4111 "t1_fund",
4112 "t1_fund1",
4113
4114 /* A UTF-8 name with multi-byte sequences to make sure that
4115 cp-name-parser understands this as a single identifier ("função"
4116 is "function" in PT). */
4117 u8"u8função",
4118
4119 /* \377 (0xff) is Latin1 'ÿ'. */
4120 "yfunc\377",
4121
4122 /* \377 (0xff) is Latin1 'ÿ'. */
4123 "\377",
4124 "\377\377123",
4125
4126 /* A name with all sorts of complications. Starts with "z" to make
4127 it easier for the completion tests below. */
4128 #define Z_SYM_NAME \
4129 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4130 "::tuple<(anonymous namespace)::ui*, " \
4131 "std::default_delete<(anonymous namespace)::ui>, void>"
4132
4133 Z_SYM_NAME
4134 };
4135
4136 /* Returns true if the mapped_index_base::find_name_component_bounds
4137 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4138 in completion mode. */
4139
4140 static bool
4141 check_find_bounds_finds (mapped_index_base &index,
4142 const char *search_name,
4143 gdb::array_view<const char *> expected_syms)
4144 {
4145 lookup_name_info lookup_name (search_name,
4146 symbol_name_match_type::FULL, true);
4147
4148 auto bounds = index.find_name_components_bounds (lookup_name,
4149 language_cplus);
4150
4151 size_t distance = std::distance (bounds.first, bounds.second);
4152 if (distance != expected_syms.size ())
4153 return false;
4154
4155 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4156 {
4157 auto nc_elem = bounds.first + exp_elem;
4158 const char *qualified = index.symbol_name_at (nc_elem->idx);
4159 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4160 return false;
4161 }
4162
4163 return true;
4164 }
4165
4166 /* Test the lower-level mapped_index::find_name_component_bounds
4167 method. */
4168
4169 static void
4170 test_mapped_index_find_name_component_bounds ()
4171 {
4172 mock_mapped_index mock_index (test_symbols);
4173
4174 mock_index.build_name_components ();
4175
4176 /* Test the lower-level mapped_index::find_name_component_bounds
4177 method in completion mode. */
4178 {
4179 static const char *expected_syms[] = {
4180 "t1_func",
4181 "t1_func1",
4182 };
4183
4184 SELF_CHECK (check_find_bounds_finds (mock_index,
4185 "t1_func", expected_syms));
4186 }
4187
4188 /* Check that the increment-last-char in the name matching algorithm
4189 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4190 {
4191 static const char *expected_syms1[] = {
4192 "\377",
4193 "\377\377123",
4194 };
4195 SELF_CHECK (check_find_bounds_finds (mock_index,
4196 "\377", expected_syms1));
4197
4198 static const char *expected_syms2[] = {
4199 "\377\377123",
4200 };
4201 SELF_CHECK (check_find_bounds_finds (mock_index,
4202 "\377\377", expected_syms2));
4203 }
4204 }
4205
4206 /* Test dw2_expand_symtabs_matching_symbol. */
4207
4208 static void
4209 test_dw2_expand_symtabs_matching_symbol ()
4210 {
4211 mock_mapped_index mock_index (test_symbols);
4212
4213 /* We let all tests run until the end even if some fails, for debug
4214 convenience. */
4215 bool any_mismatch = false;
4216
4217 /* Create the expected symbols list (an initializer_list). Needed
4218 because lists have commas, and we need to pass them to CHECK,
4219 which is a macro. */
4220 #define EXPECT(...) { __VA_ARGS__ }
4221
4222 /* Wrapper for check_match that passes down the current
4223 __FILE__/__LINE__. */
4224 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4225 any_mismatch |= !check_match (__FILE__, __LINE__, \
4226 mock_index, \
4227 NAME, MATCH_TYPE, COMPLETION_MODE, \
4228 EXPECTED_LIST)
4229
4230 /* Identity checks. */
4231 for (const char *sym : test_symbols)
4232 {
4233 /* Should be able to match all existing symbols. */
4234 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4235 EXPECT (sym));
4236
4237 /* Should be able to match all existing symbols with
4238 parameters. */
4239 std::string with_params = std::string (sym) + "(int)";
4240 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4241 EXPECT (sym));
4242
4243 /* Should be able to match all existing symbols with
4244 parameters and qualifiers. */
4245 with_params = std::string (sym) + " ( int ) const";
4246 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4247 EXPECT (sym));
4248
4249 /* This should really find sym, but cp-name-parser.y doesn't
4250 know about lvalue/rvalue qualifiers yet. */
4251 with_params = std::string (sym) + " ( int ) &&";
4252 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4253 {});
4254 }
4255
4256 /* Check that the name matching algorithm for completion doesn't get
4257 confused with Latin1 'ÿ' / 0xff. */
4258 {
4259 static const char str[] = "\377";
4260 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4261 EXPECT ("\377", "\377\377123"));
4262 }
4263
4264 /* Check that the increment-last-char in the matching algorithm for
4265 completion doesn't match "t1_fund" when completing "t1_func". */
4266 {
4267 static const char str[] = "t1_func";
4268 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4269 EXPECT ("t1_func", "t1_func1"));
4270 }
4271
4272 /* Check that completion mode works at each prefix of the expected
4273 symbol name. */
4274 {
4275 static const char str[] = "function(int)";
4276 size_t len = strlen (str);
4277 std::string lookup;
4278
4279 for (size_t i = 1; i < len; i++)
4280 {
4281 lookup.assign (str, i);
4282 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4283 EXPECT ("function"));
4284 }
4285 }
4286
4287 /* While "w" is a prefix of both components, the match function
4288 should still only be called once. */
4289 {
4290 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4291 EXPECT ("w1::w2"));
4292 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4293 EXPECT ("w1::w2"));
4294 }
4295
4296 /* Same, with a "complicated" symbol. */
4297 {
4298 static const char str[] = Z_SYM_NAME;
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 (Z_SYM_NAME));
4307 }
4308 }
4309
4310 /* In FULL mode, an incomplete symbol doesn't match. */
4311 {
4312 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4313 {});
4314 }
4315
4316 /* A complete symbol with parameters matches any overload, since the
4317 index has no overload info. */
4318 {
4319 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4320 EXPECT ("std::zfunction", "std::zfunction2"));
4321 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4322 EXPECT ("std::zfunction", "std::zfunction2"));
4323 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4324 EXPECT ("std::zfunction", "std::zfunction2"));
4325 }
4326
4327 /* Check that whitespace is ignored appropriately. A symbol with a
4328 template argument list. */
4329 {
4330 static const char expected[] = "ns::foo<int>";
4331 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4332 EXPECT (expected));
4333 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4334 EXPECT (expected));
4335 }
4336
4337 /* Check that whitespace is ignored appropriately. A symbol with a
4338 template argument list that includes a pointer. */
4339 {
4340 static const char expected[] = "ns::foo<char*>";
4341 /* Try both completion and non-completion modes. */
4342 static const bool completion_mode[2] = {false, true};
4343 for (size_t i = 0; i < 2; i++)
4344 {
4345 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4346 completion_mode[i], EXPECT (expected));
4347 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4348 completion_mode[i], EXPECT (expected));
4349
4350 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4351 completion_mode[i], EXPECT (expected));
4352 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4353 completion_mode[i], EXPECT (expected));
4354 }
4355 }
4356
4357 {
4358 /* Check method qualifiers are ignored. */
4359 static const char expected[] = "ns::foo<char*>";
4360 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4361 symbol_name_match_type::FULL, true, EXPECT (expected));
4362 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4363 symbol_name_match_type::FULL, true, EXPECT (expected));
4364 CHECK_MATCH ("foo < char * > ( int ) const",
4365 symbol_name_match_type::WILD, true, EXPECT (expected));
4366 CHECK_MATCH ("foo < char * > ( int ) &&",
4367 symbol_name_match_type::WILD, true, EXPECT (expected));
4368 }
4369
4370 /* Test lookup names that don't match anything. */
4371 {
4372 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4373 {});
4374
4375 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4376 {});
4377 }
4378
4379 /* Some wild matching tests, exercising "(anonymous namespace)",
4380 which should not be confused with a parameter list. */
4381 {
4382 static const char *syms[] = {
4383 "A::B::C",
4384 "B::C",
4385 "C",
4386 "A :: B :: C ( int )",
4387 "B :: C ( int )",
4388 "C ( int )",
4389 };
4390
4391 for (const char *s : syms)
4392 {
4393 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4394 EXPECT ("(anonymous namespace)::A::B::C"));
4395 }
4396 }
4397
4398 {
4399 static const char expected[] = "ns2::tmpl<int>::foo2";
4400 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4401 EXPECT (expected));
4402 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4403 EXPECT (expected));
4404 }
4405
4406 SELF_CHECK (!any_mismatch);
4407
4408 #undef EXPECT
4409 #undef CHECK_MATCH
4410 }
4411
4412 static void
4413 run_test ()
4414 {
4415 test_mapped_index_find_name_component_bounds ();
4416 test_dw2_expand_symtabs_matching_symbol ();
4417 }
4418
4419 }} // namespace selftests::dw2_expand_symtabs_matching
4420
4421 #endif /* GDB_SELF_TEST */
4422
4423 /* If FILE_MATCHER is NULL or if PER_CU has
4424 dwarf2_per_cu_quick_data::MARK set (see
4425 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4426 EXPANSION_NOTIFY on it. */
4427
4428 static void
4429 dw2_expand_symtabs_matching_one
4430 (struct dwarf2_per_cu_data *per_cu,
4431 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4432 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4433 {
4434 if (file_matcher == NULL || per_cu->v.quick->mark)
4435 {
4436 bool symtab_was_null
4437 = (per_cu->v.quick->compunit_symtab == NULL);
4438
4439 dw2_instantiate_symtab (per_cu, false);
4440
4441 if (expansion_notify != NULL
4442 && symtab_was_null
4443 && per_cu->v.quick->compunit_symtab != NULL)
4444 expansion_notify (per_cu->v.quick->compunit_symtab);
4445 }
4446 }
4447
4448 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4449 matched, to expand corresponding CUs that were marked. IDX is the
4450 index of the symbol name that matched. */
4451
4452 static void
4453 dw2_expand_marked_cus
4454 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4455 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4456 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4457 search_domain kind)
4458 {
4459 offset_type *vec, vec_len, vec_idx;
4460 bool global_seen = false;
4461 mapped_index &index = *dwarf2_per_objfile->index_table;
4462
4463 vec = (offset_type *) (index.constant_pool
4464 + MAYBE_SWAP (index.symbol_table[idx].vec));
4465 vec_len = MAYBE_SWAP (vec[0]);
4466 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4467 {
4468 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4469 /* This value is only valid for index versions >= 7. */
4470 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4471 gdb_index_symbol_kind symbol_kind =
4472 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4473 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4474 /* Only check the symbol attributes if they're present.
4475 Indices prior to version 7 don't record them,
4476 and indices >= 7 may elide them for certain symbols
4477 (gold does this). */
4478 int attrs_valid =
4479 (index.version >= 7
4480 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4481
4482 /* Work around gold/15646. */
4483 if (attrs_valid)
4484 {
4485 if (!is_static && global_seen)
4486 continue;
4487 if (!is_static)
4488 global_seen = true;
4489 }
4490
4491 /* Only check the symbol's kind if it has one. */
4492 if (attrs_valid)
4493 {
4494 switch (kind)
4495 {
4496 case VARIABLES_DOMAIN:
4497 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4498 continue;
4499 break;
4500 case FUNCTIONS_DOMAIN:
4501 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4502 continue;
4503 break;
4504 case TYPES_DOMAIN:
4505 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4506 continue;
4507 break;
4508 case MODULES_DOMAIN:
4509 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4510 continue;
4511 break;
4512 default:
4513 break;
4514 }
4515 }
4516
4517 /* Don't crash on bad data. */
4518 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4519 + dwarf2_per_objfile->all_type_units.size ()))
4520 {
4521 complaint (_(".gdb_index entry has bad CU index"
4522 " [in module %s]"),
4523 objfile_name (dwarf2_per_objfile->objfile));
4524 continue;
4525 }
4526
4527 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4528 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4529 expansion_notify);
4530 }
4531 }
4532
4533 /* If FILE_MATCHER is non-NULL, set all the
4534 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4535 that match FILE_MATCHER. */
4536
4537 static void
4538 dw_expand_symtabs_matching_file_matcher
4539 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4540 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4541 {
4542 if (file_matcher == NULL)
4543 return;
4544
4545 objfile *const objfile = dwarf2_per_objfile->objfile;
4546
4547 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4548 htab_eq_pointer,
4549 NULL, xcalloc, xfree));
4550 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4551 htab_eq_pointer,
4552 NULL, xcalloc, xfree));
4553
4554 /* The rule is CUs specify all the files, including those used by
4555 any TU, so there's no need to scan TUs here. */
4556
4557 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4558 {
4559 QUIT;
4560
4561 per_cu->v.quick->mark = 0;
4562
4563 /* We only need to look at symtabs not already expanded. */
4564 if (per_cu->v.quick->compunit_symtab)
4565 continue;
4566
4567 quick_file_names *file_data = dw2_get_file_names (per_cu);
4568 if (file_data == NULL)
4569 continue;
4570
4571 if (htab_find (visited_not_found.get (), file_data) != NULL)
4572 continue;
4573 else if (htab_find (visited_found.get (), file_data) != NULL)
4574 {
4575 per_cu->v.quick->mark = 1;
4576 continue;
4577 }
4578
4579 for (int j = 0; j < file_data->num_file_names; ++j)
4580 {
4581 const char *this_real_name;
4582
4583 if (file_matcher (file_data->file_names[j], false))
4584 {
4585 per_cu->v.quick->mark = 1;
4586 break;
4587 }
4588
4589 /* Before we invoke realpath, which can get expensive when many
4590 files are involved, do a quick comparison of the basenames. */
4591 if (!basenames_may_differ
4592 && !file_matcher (lbasename (file_data->file_names[j]),
4593 true))
4594 continue;
4595
4596 this_real_name = dw2_get_real_path (objfile, file_data, j);
4597 if (file_matcher (this_real_name, false))
4598 {
4599 per_cu->v.quick->mark = 1;
4600 break;
4601 }
4602 }
4603
4604 void **slot = htab_find_slot (per_cu->v.quick->mark
4605 ? visited_found.get ()
4606 : visited_not_found.get (),
4607 file_data, INSERT);
4608 *slot = file_data;
4609 }
4610 }
4611
4612 static void
4613 dw2_expand_symtabs_matching
4614 (struct objfile *objfile,
4615 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4616 const lookup_name_info &lookup_name,
4617 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4618 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4619 enum search_domain kind)
4620 {
4621 struct dwarf2_per_objfile *dwarf2_per_objfile
4622 = get_dwarf2_per_objfile (objfile);
4623
4624 /* index_table is NULL if OBJF_READNOW. */
4625 if (!dwarf2_per_objfile->index_table)
4626 return;
4627
4628 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4629
4630 mapped_index &index = *dwarf2_per_objfile->index_table;
4631
4632 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4633 symbol_matcher,
4634 kind, [&] (offset_type idx)
4635 {
4636 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4637 expansion_notify, kind);
4638 return true;
4639 });
4640 }
4641
4642 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4643 symtab. */
4644
4645 static struct compunit_symtab *
4646 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4647 CORE_ADDR pc)
4648 {
4649 int i;
4650
4651 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4652 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4653 return cust;
4654
4655 if (cust->includes == NULL)
4656 return NULL;
4657
4658 for (i = 0; cust->includes[i]; ++i)
4659 {
4660 struct compunit_symtab *s = cust->includes[i];
4661
4662 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4663 if (s != NULL)
4664 return s;
4665 }
4666
4667 return NULL;
4668 }
4669
4670 static struct compunit_symtab *
4671 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4672 struct bound_minimal_symbol msymbol,
4673 CORE_ADDR pc,
4674 struct obj_section *section,
4675 int warn_if_readin)
4676 {
4677 struct dwarf2_per_cu_data *data;
4678 struct compunit_symtab *result;
4679
4680 if (!objfile->partial_symtabs->psymtabs_addrmap)
4681 return NULL;
4682
4683 CORE_ADDR baseaddr = objfile->text_section_offset ();
4684 data = (struct dwarf2_per_cu_data *) addrmap_find
4685 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4686 if (!data)
4687 return NULL;
4688
4689 if (warn_if_readin && data->v.quick->compunit_symtab)
4690 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4691 paddress (get_objfile_arch (objfile), pc));
4692
4693 result
4694 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4695 false),
4696 pc);
4697 gdb_assert (result != NULL);
4698 return result;
4699 }
4700
4701 static void
4702 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4703 void *data, int need_fullname)
4704 {
4705 struct dwarf2_per_objfile *dwarf2_per_objfile
4706 = get_dwarf2_per_objfile (objfile);
4707
4708 if (!dwarf2_per_objfile->filenames_cache)
4709 {
4710 dwarf2_per_objfile->filenames_cache.emplace ();
4711
4712 htab_up visited (htab_create_alloc (10,
4713 htab_hash_pointer, htab_eq_pointer,
4714 NULL, xcalloc, xfree));
4715
4716 /* The rule is CUs specify all the files, including those used
4717 by any TU, so there's no need to scan TUs here. We can
4718 ignore file names coming from already-expanded CUs. */
4719
4720 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4721 {
4722 if (per_cu->v.quick->compunit_symtab)
4723 {
4724 void **slot = htab_find_slot (visited.get (),
4725 per_cu->v.quick->file_names,
4726 INSERT);
4727
4728 *slot = per_cu->v.quick->file_names;
4729 }
4730 }
4731
4732 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4733 {
4734 /* We only need to look at symtabs not already expanded. */
4735 if (per_cu->v.quick->compunit_symtab)
4736 continue;
4737
4738 quick_file_names *file_data = dw2_get_file_names (per_cu);
4739 if (file_data == NULL)
4740 continue;
4741
4742 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4743 if (*slot)
4744 {
4745 /* Already visited. */
4746 continue;
4747 }
4748 *slot = file_data;
4749
4750 for (int j = 0; j < file_data->num_file_names; ++j)
4751 {
4752 const char *filename = file_data->file_names[j];
4753 dwarf2_per_objfile->filenames_cache->seen (filename);
4754 }
4755 }
4756 }
4757
4758 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4759 {
4760 gdb::unique_xmalloc_ptr<char> this_real_name;
4761
4762 if (need_fullname)
4763 this_real_name = gdb_realpath (filename);
4764 (*fun) (filename, this_real_name.get (), data);
4765 });
4766 }
4767
4768 static int
4769 dw2_has_symbols (struct objfile *objfile)
4770 {
4771 return 1;
4772 }
4773
4774 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4775 {
4776 dw2_has_symbols,
4777 dw2_find_last_source_symtab,
4778 dw2_forget_cached_source_info,
4779 dw2_map_symtabs_matching_filename,
4780 dw2_lookup_symbol,
4781 dw2_print_stats,
4782 dw2_dump,
4783 dw2_expand_symtabs_for_function,
4784 dw2_expand_all_symtabs,
4785 dw2_expand_symtabs_with_fullname,
4786 dw2_map_matching_symbols,
4787 dw2_expand_symtabs_matching,
4788 dw2_find_pc_sect_compunit_symtab,
4789 NULL,
4790 dw2_map_symbol_filenames
4791 };
4792
4793 /* DWARF-5 debug_names reader. */
4794
4795 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4796 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4797
4798 /* A helper function that reads the .debug_names section in SECTION
4799 and fills in MAP. FILENAME is the name of the file containing the
4800 section; it is used for error reporting.
4801
4802 Returns true if all went well, false otherwise. */
4803
4804 static bool
4805 read_debug_names_from_section (struct objfile *objfile,
4806 const char *filename,
4807 struct dwarf2_section_info *section,
4808 mapped_debug_names &map)
4809 {
4810 if (section->empty ())
4811 return false;
4812
4813 /* Older elfutils strip versions could keep the section in the main
4814 executable while splitting it for the separate debug info file. */
4815 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4816 return false;
4817
4818 section->read (objfile);
4819
4820 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4821
4822 const gdb_byte *addr = section->buffer;
4823
4824 bfd *const abfd = section->get_bfd_owner ();
4825
4826 unsigned int bytes_read;
4827 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4828 addr += bytes_read;
4829
4830 map.dwarf5_is_dwarf64 = bytes_read != 4;
4831 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4832 if (bytes_read + length != section->size)
4833 {
4834 /* There may be multiple per-CU indices. */
4835 warning (_("Section .debug_names in %s length %s does not match "
4836 "section length %s, ignoring .debug_names."),
4837 filename, plongest (bytes_read + length),
4838 pulongest (section->size));
4839 return false;
4840 }
4841
4842 /* The version number. */
4843 uint16_t version = read_2_bytes (abfd, addr);
4844 addr += 2;
4845 if (version != 5)
4846 {
4847 warning (_("Section .debug_names in %s has unsupported version %d, "
4848 "ignoring .debug_names."),
4849 filename, version);
4850 return false;
4851 }
4852
4853 /* Padding. */
4854 uint16_t padding = read_2_bytes (abfd, addr);
4855 addr += 2;
4856 if (padding != 0)
4857 {
4858 warning (_("Section .debug_names in %s has unsupported padding %d, "
4859 "ignoring .debug_names."),
4860 filename, padding);
4861 return false;
4862 }
4863
4864 /* comp_unit_count - The number of CUs in the CU list. */
4865 map.cu_count = read_4_bytes (abfd, addr);
4866 addr += 4;
4867
4868 /* local_type_unit_count - The number of TUs in the local TU
4869 list. */
4870 map.tu_count = read_4_bytes (abfd, addr);
4871 addr += 4;
4872
4873 /* foreign_type_unit_count - The number of TUs in the foreign TU
4874 list. */
4875 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4876 addr += 4;
4877 if (foreign_tu_count != 0)
4878 {
4879 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4880 "ignoring .debug_names."),
4881 filename, static_cast<unsigned long> (foreign_tu_count));
4882 return false;
4883 }
4884
4885 /* bucket_count - The number of hash buckets in the hash lookup
4886 table. */
4887 map.bucket_count = read_4_bytes (abfd, addr);
4888 addr += 4;
4889
4890 /* name_count - The number of unique names in the index. */
4891 map.name_count = read_4_bytes (abfd, addr);
4892 addr += 4;
4893
4894 /* abbrev_table_size - The size in bytes of the abbreviations
4895 table. */
4896 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4897 addr += 4;
4898
4899 /* augmentation_string_size - The size in bytes of the augmentation
4900 string. This value is rounded up to a multiple of 4. */
4901 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4902 addr += 4;
4903 map.augmentation_is_gdb = ((augmentation_string_size
4904 == sizeof (dwarf5_augmentation))
4905 && memcmp (addr, dwarf5_augmentation,
4906 sizeof (dwarf5_augmentation)) == 0);
4907 augmentation_string_size += (-augmentation_string_size) & 3;
4908 addr += augmentation_string_size;
4909
4910 /* List of CUs */
4911 map.cu_table_reordered = addr;
4912 addr += map.cu_count * map.offset_size;
4913
4914 /* List of Local TUs */
4915 map.tu_table_reordered = addr;
4916 addr += map.tu_count * map.offset_size;
4917
4918 /* Hash Lookup Table */
4919 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4920 addr += map.bucket_count * 4;
4921 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4922 addr += map.name_count * 4;
4923
4924 /* Name Table */
4925 map.name_table_string_offs_reordered = addr;
4926 addr += map.name_count * map.offset_size;
4927 map.name_table_entry_offs_reordered = addr;
4928 addr += map.name_count * map.offset_size;
4929
4930 const gdb_byte *abbrev_table_start = addr;
4931 for (;;)
4932 {
4933 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4934 addr += bytes_read;
4935 if (index_num == 0)
4936 break;
4937
4938 const auto insertpair
4939 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4940 if (!insertpair.second)
4941 {
4942 warning (_("Section .debug_names in %s has duplicate index %s, "
4943 "ignoring .debug_names."),
4944 filename, pulongest (index_num));
4945 return false;
4946 }
4947 mapped_debug_names::index_val &indexval = insertpair.first->second;
4948 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4949 addr += bytes_read;
4950
4951 for (;;)
4952 {
4953 mapped_debug_names::index_val::attr attr;
4954 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4955 addr += bytes_read;
4956 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4957 addr += bytes_read;
4958 if (attr.form == DW_FORM_implicit_const)
4959 {
4960 attr.implicit_const = read_signed_leb128 (abfd, addr,
4961 &bytes_read);
4962 addr += bytes_read;
4963 }
4964 if (attr.dw_idx == 0 && attr.form == 0)
4965 break;
4966 indexval.attr_vec.push_back (std::move (attr));
4967 }
4968 }
4969 if (addr != abbrev_table_start + abbrev_table_size)
4970 {
4971 warning (_("Section .debug_names in %s has abbreviation_table "
4972 "of size %s vs. written as %u, ignoring .debug_names."),
4973 filename, plongest (addr - abbrev_table_start),
4974 abbrev_table_size);
4975 return false;
4976 }
4977 map.entry_pool = addr;
4978
4979 return true;
4980 }
4981
4982 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4983 list. */
4984
4985 static void
4986 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4987 const mapped_debug_names &map,
4988 dwarf2_section_info &section,
4989 bool is_dwz)
4990 {
4991 sect_offset sect_off_prev;
4992 for (uint32_t i = 0; i <= map.cu_count; ++i)
4993 {
4994 sect_offset sect_off_next;
4995 if (i < map.cu_count)
4996 {
4997 sect_off_next
4998 = (sect_offset) (extract_unsigned_integer
4999 (map.cu_table_reordered + i * map.offset_size,
5000 map.offset_size,
5001 map.dwarf5_byte_order));
5002 }
5003 else
5004 sect_off_next = (sect_offset) section.size;
5005 if (i >= 1)
5006 {
5007 const ULONGEST length = sect_off_next - sect_off_prev;
5008 dwarf2_per_cu_data *per_cu
5009 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5010 sect_off_prev, length);
5011 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5012 }
5013 sect_off_prev = sect_off_next;
5014 }
5015 }
5016
5017 /* Read the CU list from the mapped index, and use it to create all
5018 the CU objects for this dwarf2_per_objfile. */
5019
5020 static void
5021 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5022 const mapped_debug_names &map,
5023 const mapped_debug_names &dwz_map)
5024 {
5025 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5026 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5027
5028 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5029 dwarf2_per_objfile->info,
5030 false /* is_dwz */);
5031
5032 if (dwz_map.cu_count == 0)
5033 return;
5034
5035 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5036 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5037 true /* is_dwz */);
5038 }
5039
5040 /* Read .debug_names. If everything went ok, initialize the "quick"
5041 elements of all the CUs and return true. Otherwise, return false. */
5042
5043 static bool
5044 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5045 {
5046 std::unique_ptr<mapped_debug_names> map
5047 (new mapped_debug_names (dwarf2_per_objfile));
5048 mapped_debug_names dwz_map (dwarf2_per_objfile);
5049 struct objfile *objfile = dwarf2_per_objfile->objfile;
5050
5051 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5052 &dwarf2_per_objfile->debug_names,
5053 *map))
5054 return false;
5055
5056 /* Don't use the index if it's empty. */
5057 if (map->name_count == 0)
5058 return false;
5059
5060 /* If there is a .dwz file, read it so we can get its CU list as
5061 well. */
5062 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5063 if (dwz != NULL)
5064 {
5065 if (!read_debug_names_from_section (objfile,
5066 bfd_get_filename (dwz->dwz_bfd.get ()),
5067 &dwz->debug_names, dwz_map))
5068 {
5069 warning (_("could not read '.debug_names' section from %s; skipping"),
5070 bfd_get_filename (dwz->dwz_bfd.get ()));
5071 return false;
5072 }
5073 }
5074
5075 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5076
5077 if (map->tu_count != 0)
5078 {
5079 /* We can only handle a single .debug_types when we have an
5080 index. */
5081 if (dwarf2_per_objfile->types.size () != 1)
5082 return false;
5083
5084 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5085
5086 create_signatured_type_table_from_debug_names
5087 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5088 }
5089
5090 create_addrmap_from_aranges (dwarf2_per_objfile,
5091 &dwarf2_per_objfile->debug_aranges);
5092
5093 dwarf2_per_objfile->debug_names_table = std::move (map);
5094 dwarf2_per_objfile->using_index = 1;
5095 dwarf2_per_objfile->quick_file_names_table =
5096 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5097
5098 return true;
5099 }
5100
5101 /* Type used to manage iterating over all CUs looking for a symbol for
5102 .debug_names. */
5103
5104 class dw2_debug_names_iterator
5105 {
5106 public:
5107 dw2_debug_names_iterator (const mapped_debug_names &map,
5108 gdb::optional<block_enum> block_index,
5109 domain_enum domain,
5110 const char *name)
5111 : m_map (map), m_block_index (block_index), m_domain (domain),
5112 m_addr (find_vec_in_debug_names (map, name))
5113 {}
5114
5115 dw2_debug_names_iterator (const mapped_debug_names &map,
5116 search_domain search, uint32_t namei)
5117 : m_map (map),
5118 m_search (search),
5119 m_addr (find_vec_in_debug_names (map, namei))
5120 {}
5121
5122 dw2_debug_names_iterator (const mapped_debug_names &map,
5123 block_enum block_index, domain_enum domain,
5124 uint32_t namei)
5125 : m_map (map), m_block_index (block_index), m_domain (domain),
5126 m_addr (find_vec_in_debug_names (map, namei))
5127 {}
5128
5129 /* Return the next matching CU or NULL if there are no more. */
5130 dwarf2_per_cu_data *next ();
5131
5132 private:
5133 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5134 const char *name);
5135 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5136 uint32_t namei);
5137
5138 /* The internalized form of .debug_names. */
5139 const mapped_debug_names &m_map;
5140
5141 /* If set, only look for symbols that match that block. Valid values are
5142 GLOBAL_BLOCK and STATIC_BLOCK. */
5143 const gdb::optional<block_enum> m_block_index;
5144
5145 /* The kind of symbol we're looking for. */
5146 const domain_enum m_domain = UNDEF_DOMAIN;
5147 const search_domain m_search = ALL_DOMAIN;
5148
5149 /* The list of CUs from the index entry of the symbol, or NULL if
5150 not found. */
5151 const gdb_byte *m_addr;
5152 };
5153
5154 const char *
5155 mapped_debug_names::namei_to_name (uint32_t namei) const
5156 {
5157 const ULONGEST namei_string_offs
5158 = extract_unsigned_integer ((name_table_string_offs_reordered
5159 + namei * offset_size),
5160 offset_size,
5161 dwarf5_byte_order);
5162 return read_indirect_string_at_offset
5163 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5164 }
5165
5166 /* Find a slot in .debug_names for the object named NAME. If NAME is
5167 found, return pointer to its pool data. If NAME cannot be found,
5168 return NULL. */
5169
5170 const gdb_byte *
5171 dw2_debug_names_iterator::find_vec_in_debug_names
5172 (const mapped_debug_names &map, const char *name)
5173 {
5174 int (*cmp) (const char *, const char *);
5175
5176 gdb::unique_xmalloc_ptr<char> without_params;
5177 if (current_language->la_language == language_cplus
5178 || current_language->la_language == language_fortran
5179 || current_language->la_language == language_d)
5180 {
5181 /* NAME is already canonical. Drop any qualifiers as
5182 .debug_names does not contain any. */
5183
5184 if (strchr (name, '(') != NULL)
5185 {
5186 without_params = cp_remove_params (name);
5187 if (without_params != NULL)
5188 name = without_params.get ();
5189 }
5190 }
5191
5192 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5193
5194 const uint32_t full_hash = dwarf5_djb_hash (name);
5195 uint32_t namei
5196 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5197 (map.bucket_table_reordered
5198 + (full_hash % map.bucket_count)), 4,
5199 map.dwarf5_byte_order);
5200 if (namei == 0)
5201 return NULL;
5202 --namei;
5203 if (namei >= map.name_count)
5204 {
5205 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5206 "[in module %s]"),
5207 namei, map.name_count,
5208 objfile_name (map.dwarf2_per_objfile->objfile));
5209 return NULL;
5210 }
5211
5212 for (;;)
5213 {
5214 const uint32_t namei_full_hash
5215 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5216 (map.hash_table_reordered + namei), 4,
5217 map.dwarf5_byte_order);
5218 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5219 return NULL;
5220
5221 if (full_hash == namei_full_hash)
5222 {
5223 const char *const namei_string = map.namei_to_name (namei);
5224
5225 #if 0 /* An expensive sanity check. */
5226 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5227 {
5228 complaint (_("Wrong .debug_names hash for string at index %u "
5229 "[in module %s]"),
5230 namei, objfile_name (dwarf2_per_objfile->objfile));
5231 return NULL;
5232 }
5233 #endif
5234
5235 if (cmp (namei_string, name) == 0)
5236 {
5237 const ULONGEST namei_entry_offs
5238 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5239 + namei * map.offset_size),
5240 map.offset_size, map.dwarf5_byte_order);
5241 return map.entry_pool + namei_entry_offs;
5242 }
5243 }
5244
5245 ++namei;
5246 if (namei >= map.name_count)
5247 return NULL;
5248 }
5249 }
5250
5251 const gdb_byte *
5252 dw2_debug_names_iterator::find_vec_in_debug_names
5253 (const mapped_debug_names &map, uint32_t namei)
5254 {
5255 if (namei >= map.name_count)
5256 {
5257 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5258 "[in module %s]"),
5259 namei, map.name_count,
5260 objfile_name (map.dwarf2_per_objfile->objfile));
5261 return NULL;
5262 }
5263
5264 const ULONGEST namei_entry_offs
5265 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5266 + namei * map.offset_size),
5267 map.offset_size, map.dwarf5_byte_order);
5268 return map.entry_pool + namei_entry_offs;
5269 }
5270
5271 /* See dw2_debug_names_iterator. */
5272
5273 dwarf2_per_cu_data *
5274 dw2_debug_names_iterator::next ()
5275 {
5276 if (m_addr == NULL)
5277 return NULL;
5278
5279 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5280 struct objfile *objfile = dwarf2_per_objfile->objfile;
5281 bfd *const abfd = objfile->obfd;
5282
5283 again:
5284
5285 unsigned int bytes_read;
5286 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5287 m_addr += bytes_read;
5288 if (abbrev == 0)
5289 return NULL;
5290
5291 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5292 if (indexval_it == m_map.abbrev_map.cend ())
5293 {
5294 complaint (_("Wrong .debug_names undefined abbrev code %s "
5295 "[in module %s]"),
5296 pulongest (abbrev), objfile_name (objfile));
5297 return NULL;
5298 }
5299 const mapped_debug_names::index_val &indexval = indexval_it->second;
5300 enum class symbol_linkage {
5301 unknown,
5302 static_,
5303 extern_,
5304 } symbol_linkage_ = symbol_linkage::unknown;
5305 dwarf2_per_cu_data *per_cu = NULL;
5306 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5307 {
5308 ULONGEST ull;
5309 switch (attr.form)
5310 {
5311 case DW_FORM_implicit_const:
5312 ull = attr.implicit_const;
5313 break;
5314 case DW_FORM_flag_present:
5315 ull = 1;
5316 break;
5317 case DW_FORM_udata:
5318 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5319 m_addr += bytes_read;
5320 break;
5321 default:
5322 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5323 dwarf_form_name (attr.form),
5324 objfile_name (objfile));
5325 return NULL;
5326 }
5327 switch (attr.dw_idx)
5328 {
5329 case DW_IDX_compile_unit:
5330 /* Don't crash on bad data. */
5331 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5332 {
5333 complaint (_(".debug_names entry has bad CU index %s"
5334 " [in module %s]"),
5335 pulongest (ull),
5336 objfile_name (dwarf2_per_objfile->objfile));
5337 continue;
5338 }
5339 per_cu = dwarf2_per_objfile->get_cutu (ull);
5340 break;
5341 case DW_IDX_type_unit:
5342 /* Don't crash on bad data. */
5343 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5344 {
5345 complaint (_(".debug_names entry has bad TU index %s"
5346 " [in module %s]"),
5347 pulongest (ull),
5348 objfile_name (dwarf2_per_objfile->objfile));
5349 continue;
5350 }
5351 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5352 break;
5353 case DW_IDX_GNU_internal:
5354 if (!m_map.augmentation_is_gdb)
5355 break;
5356 symbol_linkage_ = symbol_linkage::static_;
5357 break;
5358 case DW_IDX_GNU_external:
5359 if (!m_map.augmentation_is_gdb)
5360 break;
5361 symbol_linkage_ = symbol_linkage::extern_;
5362 break;
5363 }
5364 }
5365
5366 /* Skip if already read in. */
5367 if (per_cu->v.quick->compunit_symtab)
5368 goto again;
5369
5370 /* Check static vs global. */
5371 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5372 {
5373 const bool want_static = *m_block_index == STATIC_BLOCK;
5374 const bool symbol_is_static =
5375 symbol_linkage_ == symbol_linkage::static_;
5376 if (want_static != symbol_is_static)
5377 goto again;
5378 }
5379
5380 /* Match dw2_symtab_iter_next, symbol_kind
5381 and debug_names::psymbol_tag. */
5382 switch (m_domain)
5383 {
5384 case VAR_DOMAIN:
5385 switch (indexval.dwarf_tag)
5386 {
5387 case DW_TAG_variable:
5388 case DW_TAG_subprogram:
5389 /* Some types are also in VAR_DOMAIN. */
5390 case DW_TAG_typedef:
5391 case DW_TAG_structure_type:
5392 break;
5393 default:
5394 goto again;
5395 }
5396 break;
5397 case STRUCT_DOMAIN:
5398 switch (indexval.dwarf_tag)
5399 {
5400 case DW_TAG_typedef:
5401 case DW_TAG_structure_type:
5402 break;
5403 default:
5404 goto again;
5405 }
5406 break;
5407 case LABEL_DOMAIN:
5408 switch (indexval.dwarf_tag)
5409 {
5410 case 0:
5411 case DW_TAG_variable:
5412 break;
5413 default:
5414 goto again;
5415 }
5416 break;
5417 case MODULE_DOMAIN:
5418 switch (indexval.dwarf_tag)
5419 {
5420 case DW_TAG_module:
5421 break;
5422 default:
5423 goto again;
5424 }
5425 break;
5426 default:
5427 break;
5428 }
5429
5430 /* Match dw2_expand_symtabs_matching, symbol_kind and
5431 debug_names::psymbol_tag. */
5432 switch (m_search)
5433 {
5434 case VARIABLES_DOMAIN:
5435 switch (indexval.dwarf_tag)
5436 {
5437 case DW_TAG_variable:
5438 break;
5439 default:
5440 goto again;
5441 }
5442 break;
5443 case FUNCTIONS_DOMAIN:
5444 switch (indexval.dwarf_tag)
5445 {
5446 case DW_TAG_subprogram:
5447 break;
5448 default:
5449 goto again;
5450 }
5451 break;
5452 case TYPES_DOMAIN:
5453 switch (indexval.dwarf_tag)
5454 {
5455 case DW_TAG_typedef:
5456 case DW_TAG_structure_type:
5457 break;
5458 default:
5459 goto again;
5460 }
5461 break;
5462 case MODULES_DOMAIN:
5463 switch (indexval.dwarf_tag)
5464 {
5465 case DW_TAG_module:
5466 break;
5467 default:
5468 goto again;
5469 }
5470 default:
5471 break;
5472 }
5473
5474 return per_cu;
5475 }
5476
5477 static struct compunit_symtab *
5478 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5479 const char *name, domain_enum domain)
5480 {
5481 struct dwarf2_per_objfile *dwarf2_per_objfile
5482 = get_dwarf2_per_objfile (objfile);
5483
5484 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5485 if (!mapp)
5486 {
5487 /* index is NULL if OBJF_READNOW. */
5488 return NULL;
5489 }
5490 const auto &map = *mapp;
5491
5492 dw2_debug_names_iterator iter (map, block_index, domain, name);
5493
5494 struct compunit_symtab *stab_best = NULL;
5495 struct dwarf2_per_cu_data *per_cu;
5496 while ((per_cu = iter.next ()) != NULL)
5497 {
5498 struct symbol *sym, *with_opaque = NULL;
5499 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5500 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5501 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5502
5503 sym = block_find_symbol (block, name, domain,
5504 block_find_non_opaque_type_preferred,
5505 &with_opaque);
5506
5507 /* Some caution must be observed with overloaded functions and
5508 methods, since the index will not contain any overload
5509 information (but NAME might contain it). */
5510
5511 if (sym != NULL
5512 && strcmp_iw (sym->search_name (), name) == 0)
5513 return stab;
5514 if (with_opaque != NULL
5515 && strcmp_iw (with_opaque->search_name (), name) == 0)
5516 stab_best = stab;
5517
5518 /* Keep looking through other CUs. */
5519 }
5520
5521 return stab_best;
5522 }
5523
5524 /* This dumps minimal information about .debug_names. It is called
5525 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5526 uses this to verify that .debug_names has been loaded. */
5527
5528 static void
5529 dw2_debug_names_dump (struct objfile *objfile)
5530 {
5531 struct dwarf2_per_objfile *dwarf2_per_objfile
5532 = get_dwarf2_per_objfile (objfile);
5533
5534 gdb_assert (dwarf2_per_objfile->using_index);
5535 printf_filtered (".debug_names:");
5536 if (dwarf2_per_objfile->debug_names_table)
5537 printf_filtered (" exists\n");
5538 else
5539 printf_filtered (" faked for \"readnow\"\n");
5540 printf_filtered ("\n");
5541 }
5542
5543 static void
5544 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5545 const char *func_name)
5546 {
5547 struct dwarf2_per_objfile *dwarf2_per_objfile
5548 = get_dwarf2_per_objfile (objfile);
5549
5550 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5551 if (dwarf2_per_objfile->debug_names_table)
5552 {
5553 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5554
5555 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5556
5557 struct dwarf2_per_cu_data *per_cu;
5558 while ((per_cu = iter.next ()) != NULL)
5559 dw2_instantiate_symtab (per_cu, false);
5560 }
5561 }
5562
5563 static void
5564 dw2_debug_names_map_matching_symbols
5565 (struct objfile *objfile,
5566 const lookup_name_info &name, domain_enum domain,
5567 int global,
5568 gdb::function_view<symbol_found_callback_ftype> callback,
5569 symbol_compare_ftype *ordered_compare)
5570 {
5571 struct dwarf2_per_objfile *dwarf2_per_objfile
5572 = get_dwarf2_per_objfile (objfile);
5573
5574 /* debug_names_table is NULL if OBJF_READNOW. */
5575 if (!dwarf2_per_objfile->debug_names_table)
5576 return;
5577
5578 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5579 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5580
5581 const char *match_name = name.ada ().lookup_name ().c_str ();
5582 auto matcher = [&] (const char *symname)
5583 {
5584 if (ordered_compare == nullptr)
5585 return true;
5586 return ordered_compare (symname, match_name) == 0;
5587 };
5588
5589 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5590 [&] (offset_type namei)
5591 {
5592 /* The name was matched, now expand corresponding CUs that were
5593 marked. */
5594 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5595
5596 struct dwarf2_per_cu_data *per_cu;
5597 while ((per_cu = iter.next ()) != NULL)
5598 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5599 return true;
5600 });
5601
5602 /* It's a shame we couldn't do this inside the
5603 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5604 that have already been expanded. Instead, this loop matches what
5605 the psymtab code does. */
5606 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5607 {
5608 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5609 if (cust != nullptr)
5610 {
5611 const struct block *block
5612 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5613 if (!iterate_over_symbols_terminated (block, name,
5614 domain, callback))
5615 break;
5616 }
5617 }
5618 }
5619
5620 static void
5621 dw2_debug_names_expand_symtabs_matching
5622 (struct objfile *objfile,
5623 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5624 const lookup_name_info &lookup_name,
5625 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5626 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5627 enum search_domain kind)
5628 {
5629 struct dwarf2_per_objfile *dwarf2_per_objfile
5630 = get_dwarf2_per_objfile (objfile);
5631
5632 /* debug_names_table is NULL if OBJF_READNOW. */
5633 if (!dwarf2_per_objfile->debug_names_table)
5634 return;
5635
5636 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5637
5638 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5639
5640 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5641 symbol_matcher,
5642 kind, [&] (offset_type namei)
5643 {
5644 /* The name was matched, now expand corresponding CUs that were
5645 marked. */
5646 dw2_debug_names_iterator iter (map, kind, namei);
5647
5648 struct dwarf2_per_cu_data *per_cu;
5649 while ((per_cu = iter.next ()) != NULL)
5650 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5651 expansion_notify);
5652 return true;
5653 });
5654 }
5655
5656 const struct quick_symbol_functions dwarf2_debug_names_functions =
5657 {
5658 dw2_has_symbols,
5659 dw2_find_last_source_symtab,
5660 dw2_forget_cached_source_info,
5661 dw2_map_symtabs_matching_filename,
5662 dw2_debug_names_lookup_symbol,
5663 dw2_print_stats,
5664 dw2_debug_names_dump,
5665 dw2_debug_names_expand_symtabs_for_function,
5666 dw2_expand_all_symtabs,
5667 dw2_expand_symtabs_with_fullname,
5668 dw2_debug_names_map_matching_symbols,
5669 dw2_debug_names_expand_symtabs_matching,
5670 dw2_find_pc_sect_compunit_symtab,
5671 NULL,
5672 dw2_map_symbol_filenames
5673 };
5674
5675 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5676 to either a dwarf2_per_objfile or dwz_file object. */
5677
5678 template <typename T>
5679 static gdb::array_view<const gdb_byte>
5680 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5681 {
5682 dwarf2_section_info *section = &section_owner->gdb_index;
5683
5684 if (section->empty ())
5685 return {};
5686
5687 /* Older elfutils strip versions could keep the section in the main
5688 executable while splitting it for the separate debug info file. */
5689 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5690 return {};
5691
5692 section->read (obj);
5693
5694 /* dwarf2_section_info::size is a bfd_size_type, while
5695 gdb::array_view works with size_t. On 32-bit hosts, with
5696 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5697 is 32-bit. So we need an explicit narrowing conversion here.
5698 This is fine, because it's impossible to allocate or mmap an
5699 array/buffer larger than what size_t can represent. */
5700 return gdb::make_array_view (section->buffer, section->size);
5701 }
5702
5703 /* Lookup the index cache for the contents of the index associated to
5704 DWARF2_OBJ. */
5705
5706 static gdb::array_view<const gdb_byte>
5707 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5708 {
5709 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5710 if (build_id == nullptr)
5711 return {};
5712
5713 return global_index_cache.lookup_gdb_index (build_id,
5714 &dwarf2_obj->index_cache_res);
5715 }
5716
5717 /* Same as the above, but for DWZ. */
5718
5719 static gdb::array_view<const gdb_byte>
5720 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5721 {
5722 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5723 if (build_id == nullptr)
5724 return {};
5725
5726 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5727 }
5728
5729 /* See symfile.h. */
5730
5731 bool
5732 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5733 {
5734 struct dwarf2_per_objfile *dwarf2_per_objfile
5735 = get_dwarf2_per_objfile (objfile);
5736
5737 /* If we're about to read full symbols, don't bother with the
5738 indices. In this case we also don't care if some other debug
5739 format is making psymtabs, because they are all about to be
5740 expanded anyway. */
5741 if ((objfile->flags & OBJF_READNOW))
5742 {
5743 dwarf2_per_objfile->using_index = 1;
5744 create_all_comp_units (dwarf2_per_objfile);
5745 create_all_type_units (dwarf2_per_objfile);
5746 dwarf2_per_objfile->quick_file_names_table
5747 = create_quick_file_names_table
5748 (dwarf2_per_objfile->all_comp_units.size ());
5749
5750 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5751 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5752 {
5753 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5754
5755 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5756 struct dwarf2_per_cu_quick_data);
5757 }
5758
5759 /* Return 1 so that gdb sees the "quick" functions. However,
5760 these functions will be no-ops because we will have expanded
5761 all symtabs. */
5762 *index_kind = dw_index_kind::GDB_INDEX;
5763 return true;
5764 }
5765
5766 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5767 {
5768 *index_kind = dw_index_kind::DEBUG_NAMES;
5769 return true;
5770 }
5771
5772 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5773 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5774 get_gdb_index_contents_from_section<dwz_file>))
5775 {
5776 *index_kind = dw_index_kind::GDB_INDEX;
5777 return true;
5778 }
5779
5780 /* ... otherwise, try to find the index in the index cache. */
5781 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5782 get_gdb_index_contents_from_cache,
5783 get_gdb_index_contents_from_cache_dwz))
5784 {
5785 global_index_cache.hit ();
5786 *index_kind = dw_index_kind::GDB_INDEX;
5787 return true;
5788 }
5789
5790 global_index_cache.miss ();
5791 return false;
5792 }
5793
5794 \f
5795
5796 /* Build a partial symbol table. */
5797
5798 void
5799 dwarf2_build_psymtabs (struct objfile *objfile)
5800 {
5801 struct dwarf2_per_objfile *dwarf2_per_objfile
5802 = get_dwarf2_per_objfile (objfile);
5803
5804 init_psymbol_list (objfile, 1024);
5805
5806 try
5807 {
5808 /* This isn't really ideal: all the data we allocate on the
5809 objfile's obstack is still uselessly kept around. However,
5810 freeing it seems unsafe. */
5811 psymtab_discarder psymtabs (objfile);
5812 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5813 psymtabs.keep ();
5814
5815 /* (maybe) store an index in the cache. */
5816 global_index_cache.store (dwarf2_per_objfile);
5817 }
5818 catch (const gdb_exception_error &except)
5819 {
5820 exception_print (gdb_stderr, except);
5821 }
5822 }
5823
5824 /* Find the base address of the compilation unit for range lists and
5825 location lists. It will normally be specified by DW_AT_low_pc.
5826 In DWARF-3 draft 4, the base address could be overridden by
5827 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5828 compilation units with discontinuous ranges. */
5829
5830 static void
5831 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5832 {
5833 struct attribute *attr;
5834
5835 cu->base_known = 0;
5836 cu->base_address = 0;
5837
5838 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5839 if (attr != nullptr)
5840 {
5841 cu->base_address = attr->value_as_address ();
5842 cu->base_known = 1;
5843 }
5844 else
5845 {
5846 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5847 if (attr != nullptr)
5848 {
5849 cu->base_address = attr->value_as_address ();
5850 cu->base_known = 1;
5851 }
5852 }
5853 }
5854
5855 /* Helper function that returns the proper abbrev section for
5856 THIS_CU. */
5857
5858 static struct dwarf2_section_info *
5859 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5860 {
5861 struct dwarf2_section_info *abbrev;
5862 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5863
5864 if (this_cu->is_dwz)
5865 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5866 else
5867 abbrev = &dwarf2_per_objfile->abbrev;
5868
5869 return abbrev;
5870 }
5871
5872 /* Fetch the abbreviation table offset from a comp or type unit header. */
5873
5874 static sect_offset
5875 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5876 struct dwarf2_section_info *section,
5877 sect_offset sect_off)
5878 {
5879 bfd *abfd = section->get_bfd_owner ();
5880 const gdb_byte *info_ptr;
5881 unsigned int initial_length_size, offset_size;
5882 uint16_t version;
5883
5884 section->read (dwarf2_per_objfile->objfile);
5885 info_ptr = section->buffer + to_underlying (sect_off);
5886 read_initial_length (abfd, info_ptr, &initial_length_size);
5887 offset_size = initial_length_size == 4 ? 4 : 8;
5888 info_ptr += initial_length_size;
5889
5890 version = read_2_bytes (abfd, info_ptr);
5891 info_ptr += 2;
5892 if (version >= 5)
5893 {
5894 /* Skip unit type and address size. */
5895 info_ptr += 2;
5896 }
5897
5898 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5899 }
5900
5901 /* Allocate a new partial symtab for file named NAME and mark this new
5902 partial symtab as being an include of PST. */
5903
5904 static void
5905 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5906 struct objfile *objfile)
5907 {
5908 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
5909
5910 if (!IS_ABSOLUTE_PATH (subpst->filename))
5911 {
5912 /* It shares objfile->objfile_obstack. */
5913 subpst->dirname = pst->dirname;
5914 }
5915
5916 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5917 subpst->dependencies[0] = pst;
5918 subpst->number_of_dependencies = 1;
5919
5920 /* No private part is necessary for include psymtabs. This property
5921 can be used to differentiate between such include psymtabs and
5922 the regular ones. */
5923 subpst->per_cu_data = nullptr;
5924 }
5925
5926 /* Read the Line Number Program data and extract the list of files
5927 included by the source file represented by PST. Build an include
5928 partial symtab for each of these included files. */
5929
5930 static void
5931 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5932 struct die_info *die,
5933 dwarf2_psymtab *pst)
5934 {
5935 line_header_up lh;
5936 struct attribute *attr;
5937
5938 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5939 if (attr != nullptr)
5940 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5941 if (lh == NULL)
5942 return; /* No linetable, so no includes. */
5943
5944 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5945 that we pass in the raw text_low here; that is ok because we're
5946 only decoding the line table to make include partial symtabs, and
5947 so the addresses aren't really used. */
5948 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5949 pst->raw_text_low (), 1);
5950 }
5951
5952 static hashval_t
5953 hash_signatured_type (const void *item)
5954 {
5955 const struct signatured_type *sig_type
5956 = (const struct signatured_type *) item;
5957
5958 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5959 return sig_type->signature;
5960 }
5961
5962 static int
5963 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5964 {
5965 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5966 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5967
5968 return lhs->signature == rhs->signature;
5969 }
5970
5971 /* Allocate a hash table for signatured types. */
5972
5973 static htab_up
5974 allocate_signatured_type_table (struct objfile *objfile)
5975 {
5976 return htab_up (htab_create_alloc (41,
5977 hash_signatured_type,
5978 eq_signatured_type,
5979 NULL, xcalloc, xfree));
5980 }
5981
5982 /* A helper function to add a signatured type CU to a table. */
5983
5984 static int
5985 add_signatured_type_cu_to_table (void **slot, void *datum)
5986 {
5987 struct signatured_type *sigt = (struct signatured_type *) *slot;
5988 std::vector<signatured_type *> *all_type_units
5989 = (std::vector<signatured_type *> *) datum;
5990
5991 all_type_units->push_back (sigt);
5992
5993 return 1;
5994 }
5995
5996 /* A helper for create_debug_types_hash_table. Read types from SECTION
5997 and fill them into TYPES_HTAB. It will process only type units,
5998 therefore DW_UT_type. */
5999
6000 static void
6001 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6002 struct dwo_file *dwo_file,
6003 dwarf2_section_info *section, htab_up &types_htab,
6004 rcuh_kind section_kind)
6005 {
6006 struct objfile *objfile = dwarf2_per_objfile->objfile;
6007 struct dwarf2_section_info *abbrev_section;
6008 bfd *abfd;
6009 const gdb_byte *info_ptr, *end_ptr;
6010
6011 abbrev_section = (dwo_file != NULL
6012 ? &dwo_file->sections.abbrev
6013 : &dwarf2_per_objfile->abbrev);
6014
6015 if (dwarf_read_debug)
6016 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6017 section->get_name (),
6018 abbrev_section->get_file_name ());
6019
6020 section->read (objfile);
6021 info_ptr = section->buffer;
6022
6023 if (info_ptr == NULL)
6024 return;
6025
6026 /* We can't set abfd until now because the section may be empty or
6027 not present, in which case the bfd is unknown. */
6028 abfd = section->get_bfd_owner ();
6029
6030 /* We don't use cutu_reader here because we don't need to read
6031 any dies: the signature is in the header. */
6032
6033 end_ptr = info_ptr + section->size;
6034 while (info_ptr < end_ptr)
6035 {
6036 struct signatured_type *sig_type;
6037 struct dwo_unit *dwo_tu;
6038 void **slot;
6039 const gdb_byte *ptr = info_ptr;
6040 struct comp_unit_head header;
6041 unsigned int length;
6042
6043 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6044
6045 /* Initialize it due to a false compiler warning. */
6046 header.signature = -1;
6047 header.type_cu_offset_in_tu = (cu_offset) -1;
6048
6049 /* We need to read the type's signature in order to build the hash
6050 table, but we don't need anything else just yet. */
6051
6052 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6053 abbrev_section, ptr, section_kind);
6054
6055 length = header.get_length ();
6056
6057 /* Skip dummy type units. */
6058 if (ptr >= info_ptr + length
6059 || peek_abbrev_code (abfd, ptr) == 0
6060 || header.unit_type != DW_UT_type)
6061 {
6062 info_ptr += length;
6063 continue;
6064 }
6065
6066 if (types_htab == NULL)
6067 {
6068 if (dwo_file)
6069 types_htab = allocate_dwo_unit_table (objfile);
6070 else
6071 types_htab = allocate_signatured_type_table (objfile);
6072 }
6073
6074 if (dwo_file)
6075 {
6076 sig_type = NULL;
6077 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6078 struct dwo_unit);
6079 dwo_tu->dwo_file = dwo_file;
6080 dwo_tu->signature = header.signature;
6081 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6082 dwo_tu->section = section;
6083 dwo_tu->sect_off = sect_off;
6084 dwo_tu->length = length;
6085 }
6086 else
6087 {
6088 /* N.B.: type_offset is not usable if this type uses a DWO file.
6089 The real type_offset is in the DWO file. */
6090 dwo_tu = NULL;
6091 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6092 struct signatured_type);
6093 sig_type->signature = header.signature;
6094 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6095 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6096 sig_type->per_cu.is_debug_types = 1;
6097 sig_type->per_cu.section = section;
6098 sig_type->per_cu.sect_off = sect_off;
6099 sig_type->per_cu.length = length;
6100 }
6101
6102 slot = htab_find_slot (types_htab.get (),
6103 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6104 INSERT);
6105 gdb_assert (slot != NULL);
6106 if (*slot != NULL)
6107 {
6108 sect_offset dup_sect_off;
6109
6110 if (dwo_file)
6111 {
6112 const struct dwo_unit *dup_tu
6113 = (const struct dwo_unit *) *slot;
6114
6115 dup_sect_off = dup_tu->sect_off;
6116 }
6117 else
6118 {
6119 const struct signatured_type *dup_tu
6120 = (const struct signatured_type *) *slot;
6121
6122 dup_sect_off = dup_tu->per_cu.sect_off;
6123 }
6124
6125 complaint (_("debug type entry at offset %s is duplicate to"
6126 " the entry at offset %s, signature %s"),
6127 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6128 hex_string (header.signature));
6129 }
6130 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6131
6132 if (dwarf_read_debug > 1)
6133 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6134 sect_offset_str (sect_off),
6135 hex_string (header.signature));
6136
6137 info_ptr += length;
6138 }
6139 }
6140
6141 /* Create the hash table of all entries in the .debug_types
6142 (or .debug_types.dwo) section(s).
6143 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6144 otherwise it is NULL.
6145
6146 The result is a pointer to the hash table or NULL if there are no types.
6147
6148 Note: This function processes DWO files only, not DWP files. */
6149
6150 static void
6151 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6152 struct dwo_file *dwo_file,
6153 gdb::array_view<dwarf2_section_info> type_sections,
6154 htab_up &types_htab)
6155 {
6156 for (dwarf2_section_info &section : type_sections)
6157 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6158 types_htab, rcuh_kind::TYPE);
6159 }
6160
6161 /* Create the hash table of all entries in the .debug_types section,
6162 and initialize all_type_units.
6163 The result is zero if there is an error (e.g. missing .debug_types section),
6164 otherwise non-zero. */
6165
6166 static int
6167 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6168 {
6169 htab_up types_htab;
6170
6171 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6172 &dwarf2_per_objfile->info, types_htab,
6173 rcuh_kind::COMPILE);
6174 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6175 dwarf2_per_objfile->types, types_htab);
6176 if (types_htab == NULL)
6177 {
6178 dwarf2_per_objfile->signatured_types = NULL;
6179 return 0;
6180 }
6181
6182 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6183
6184 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6185 dwarf2_per_objfile->all_type_units.reserve
6186 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6187
6188 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6189 add_signatured_type_cu_to_table,
6190 &dwarf2_per_objfile->all_type_units);
6191
6192 return 1;
6193 }
6194
6195 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6196 If SLOT is non-NULL, it is the entry to use in the hash table.
6197 Otherwise we find one. */
6198
6199 static struct signatured_type *
6200 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6201 void **slot)
6202 {
6203 struct objfile *objfile = dwarf2_per_objfile->objfile;
6204
6205 if (dwarf2_per_objfile->all_type_units.size ()
6206 == dwarf2_per_objfile->all_type_units.capacity ())
6207 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6208
6209 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6210 struct signatured_type);
6211
6212 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6213 sig_type->signature = sig;
6214 sig_type->per_cu.is_debug_types = 1;
6215 if (dwarf2_per_objfile->using_index)
6216 {
6217 sig_type->per_cu.v.quick =
6218 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6219 struct dwarf2_per_cu_quick_data);
6220 }
6221
6222 if (slot == NULL)
6223 {
6224 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6225 sig_type, INSERT);
6226 }
6227 gdb_assert (*slot == NULL);
6228 *slot = sig_type;
6229 /* The rest of sig_type must be filled in by the caller. */
6230 return sig_type;
6231 }
6232
6233 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6234 Fill in SIG_ENTRY with DWO_ENTRY. */
6235
6236 static void
6237 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6238 struct signatured_type *sig_entry,
6239 struct dwo_unit *dwo_entry)
6240 {
6241 /* Make sure we're not clobbering something we don't expect to. */
6242 gdb_assert (! sig_entry->per_cu.queued);
6243 gdb_assert (sig_entry->per_cu.cu == NULL);
6244 if (dwarf2_per_objfile->using_index)
6245 {
6246 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6247 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6248 }
6249 else
6250 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6251 gdb_assert (sig_entry->signature == dwo_entry->signature);
6252 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6253 gdb_assert (sig_entry->type_unit_group == NULL);
6254 gdb_assert (sig_entry->dwo_unit == NULL);
6255
6256 sig_entry->per_cu.section = dwo_entry->section;
6257 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6258 sig_entry->per_cu.length = dwo_entry->length;
6259 sig_entry->per_cu.reading_dwo_directly = 1;
6260 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6261 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6262 sig_entry->dwo_unit = dwo_entry;
6263 }
6264
6265 /* Subroutine of lookup_signatured_type.
6266 If we haven't read the TU yet, create the signatured_type data structure
6267 for a TU to be read in directly from a DWO file, bypassing the stub.
6268 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6269 using .gdb_index, then when reading a CU we want to stay in the DWO file
6270 containing that CU. Otherwise we could end up reading several other DWO
6271 files (due to comdat folding) to process the transitive closure of all the
6272 mentioned TUs, and that can be slow. The current DWO file will have every
6273 type signature that it needs.
6274 We only do this for .gdb_index because in the psymtab case we already have
6275 to read all the DWOs to build the type unit groups. */
6276
6277 static struct signatured_type *
6278 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6279 {
6280 struct dwarf2_per_objfile *dwarf2_per_objfile
6281 = cu->per_cu->dwarf2_per_objfile;
6282 struct objfile *objfile = dwarf2_per_objfile->objfile;
6283 struct dwo_file *dwo_file;
6284 struct dwo_unit find_dwo_entry, *dwo_entry;
6285 struct signatured_type find_sig_entry, *sig_entry;
6286 void **slot;
6287
6288 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6289
6290 /* If TU skeletons have been removed then we may not have read in any
6291 TUs yet. */
6292 if (dwarf2_per_objfile->signatured_types == NULL)
6293 {
6294 dwarf2_per_objfile->signatured_types
6295 = allocate_signatured_type_table (objfile);
6296 }
6297
6298 /* We only ever need to read in one copy of a signatured type.
6299 Use the global signatured_types array to do our own comdat-folding
6300 of types. If this is the first time we're reading this TU, and
6301 the TU has an entry in .gdb_index, replace the recorded data from
6302 .gdb_index with this TU. */
6303
6304 find_sig_entry.signature = sig;
6305 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6306 &find_sig_entry, INSERT);
6307 sig_entry = (struct signatured_type *) *slot;
6308
6309 /* We can get here with the TU already read, *or* in the process of being
6310 read. Don't reassign the global entry to point to this DWO if that's
6311 the case. Also note that if the TU is already being read, it may not
6312 have come from a DWO, the program may be a mix of Fission-compiled
6313 code and non-Fission-compiled code. */
6314
6315 /* Have we already tried to read this TU?
6316 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6317 needn't exist in the global table yet). */
6318 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6319 return sig_entry;
6320
6321 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6322 dwo_unit of the TU itself. */
6323 dwo_file = cu->dwo_unit->dwo_file;
6324
6325 /* Ok, this is the first time we're reading this TU. */
6326 if (dwo_file->tus == NULL)
6327 return NULL;
6328 find_dwo_entry.signature = sig;
6329 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6330 &find_dwo_entry);
6331 if (dwo_entry == NULL)
6332 return NULL;
6333
6334 /* If the global table doesn't have an entry for this TU, add one. */
6335 if (sig_entry == NULL)
6336 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6337
6338 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6339 sig_entry->per_cu.tu_read = 1;
6340 return sig_entry;
6341 }
6342
6343 /* Subroutine of lookup_signatured_type.
6344 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6345 then try the DWP file. If the TU stub (skeleton) has been removed then
6346 it won't be in .gdb_index. */
6347
6348 static struct signatured_type *
6349 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6350 {
6351 struct dwarf2_per_objfile *dwarf2_per_objfile
6352 = cu->per_cu->dwarf2_per_objfile;
6353 struct objfile *objfile = dwarf2_per_objfile->objfile;
6354 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6355 struct dwo_unit *dwo_entry;
6356 struct signatured_type find_sig_entry, *sig_entry;
6357 void **slot;
6358
6359 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6360 gdb_assert (dwp_file != NULL);
6361
6362 /* If TU skeletons have been removed then we may not have read in any
6363 TUs yet. */
6364 if (dwarf2_per_objfile->signatured_types == NULL)
6365 {
6366 dwarf2_per_objfile->signatured_types
6367 = allocate_signatured_type_table (objfile);
6368 }
6369
6370 find_sig_entry.signature = sig;
6371 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6372 &find_sig_entry, INSERT);
6373 sig_entry = (struct signatured_type *) *slot;
6374
6375 /* Have we already tried to read this TU?
6376 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6377 needn't exist in the global table yet). */
6378 if (sig_entry != NULL)
6379 return sig_entry;
6380
6381 if (dwp_file->tus == NULL)
6382 return NULL;
6383 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6384 sig, 1 /* is_debug_types */);
6385 if (dwo_entry == NULL)
6386 return NULL;
6387
6388 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6389 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6390
6391 return sig_entry;
6392 }
6393
6394 /* Lookup a signature based type for DW_FORM_ref_sig8.
6395 Returns NULL if signature SIG is not present in the table.
6396 It is up to the caller to complain about this. */
6397
6398 static struct signatured_type *
6399 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6400 {
6401 struct dwarf2_per_objfile *dwarf2_per_objfile
6402 = cu->per_cu->dwarf2_per_objfile;
6403
6404 if (cu->dwo_unit
6405 && dwarf2_per_objfile->using_index)
6406 {
6407 /* We're in a DWO/DWP file, and we're using .gdb_index.
6408 These cases require special processing. */
6409 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6410 return lookup_dwo_signatured_type (cu, sig);
6411 else
6412 return lookup_dwp_signatured_type (cu, sig);
6413 }
6414 else
6415 {
6416 struct signatured_type find_entry, *entry;
6417
6418 if (dwarf2_per_objfile->signatured_types == NULL)
6419 return NULL;
6420 find_entry.signature = sig;
6421 entry = ((struct signatured_type *)
6422 htab_find (dwarf2_per_objfile->signatured_types.get (),
6423 &find_entry));
6424 return entry;
6425 }
6426 }
6427
6428 /* Return the address base of the compile unit, which, if exists, is stored
6429 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6430 static gdb::optional<ULONGEST>
6431 lookup_addr_base (struct die_info *comp_unit_die)
6432 {
6433 struct attribute *attr;
6434 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6435 if (attr == nullptr)
6436 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6437 if (attr == nullptr)
6438 return gdb::optional<ULONGEST> ();
6439 return DW_UNSND (attr);
6440 }
6441
6442 /* Return range lists base of the compile unit, which, if exists, is stored
6443 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6444 static ULONGEST
6445 lookup_ranges_base (struct die_info *comp_unit_die)
6446 {
6447 struct attribute *attr;
6448 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6449 if (attr == nullptr)
6450 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6451 if (attr == nullptr)
6452 return 0;
6453 return DW_UNSND (attr);
6454 }
6455
6456 /* Low level DIE reading support. */
6457
6458 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6459
6460 static void
6461 init_cu_die_reader (struct die_reader_specs *reader,
6462 struct dwarf2_cu *cu,
6463 struct dwarf2_section_info *section,
6464 struct dwo_file *dwo_file,
6465 struct abbrev_table *abbrev_table)
6466 {
6467 gdb_assert (section->readin && section->buffer != NULL);
6468 reader->abfd = section->get_bfd_owner ();
6469 reader->cu = cu;
6470 reader->dwo_file = dwo_file;
6471 reader->die_section = section;
6472 reader->buffer = section->buffer;
6473 reader->buffer_end = section->buffer + section->size;
6474 reader->abbrev_table = abbrev_table;
6475 }
6476
6477 /* Subroutine of cutu_reader to simplify it.
6478 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6479 There's just a lot of work to do, and cutu_reader is big enough
6480 already.
6481
6482 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6483 from it to the DIE in the DWO. If NULL we are skipping the stub.
6484 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6485 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6486 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6487 STUB_COMP_DIR may be non-NULL.
6488 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6489 are filled in with the info of the DIE from the DWO file.
6490 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6491 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6492 kept around for at least as long as *RESULT_READER.
6493
6494 The result is non-zero if a valid (non-dummy) DIE was found. */
6495
6496 static int
6497 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6498 struct dwo_unit *dwo_unit,
6499 struct die_info *stub_comp_unit_die,
6500 const char *stub_comp_dir,
6501 struct die_reader_specs *result_reader,
6502 const gdb_byte **result_info_ptr,
6503 struct die_info **result_comp_unit_die,
6504 abbrev_table_up *result_dwo_abbrev_table)
6505 {
6506 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6507 struct objfile *objfile = dwarf2_per_objfile->objfile;
6508 struct dwarf2_cu *cu = this_cu->cu;
6509 bfd *abfd;
6510 const gdb_byte *begin_info_ptr, *info_ptr;
6511 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6512 int i,num_extra_attrs;
6513 struct dwarf2_section_info *dwo_abbrev_section;
6514 struct die_info *comp_unit_die;
6515
6516 /* At most one of these may be provided. */
6517 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6518
6519 /* These attributes aren't processed until later:
6520 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6521 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6522 referenced later. However, these attributes are found in the stub
6523 which we won't have later. In order to not impose this complication
6524 on the rest of the code, we read them here and copy them to the
6525 DWO CU/TU die. */
6526
6527 stmt_list = NULL;
6528 low_pc = NULL;
6529 high_pc = NULL;
6530 ranges = NULL;
6531 comp_dir = NULL;
6532
6533 if (stub_comp_unit_die != NULL)
6534 {
6535 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6536 DWO file. */
6537 if (! this_cu->is_debug_types)
6538 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6539 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6540 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6541 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6542 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6543
6544 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6545
6546 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6547 here (if needed). We need the value before we can process
6548 DW_AT_ranges. */
6549 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6550 }
6551 else if (stub_comp_dir != NULL)
6552 {
6553 /* Reconstruct the comp_dir attribute to simplify the code below. */
6554 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6555 comp_dir->name = DW_AT_comp_dir;
6556 comp_dir->form = DW_FORM_string;
6557 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6558 DW_STRING (comp_dir) = stub_comp_dir;
6559 }
6560
6561 /* Set up for reading the DWO CU/TU. */
6562 cu->dwo_unit = dwo_unit;
6563 dwarf2_section_info *section = dwo_unit->section;
6564 section->read (objfile);
6565 abfd = section->get_bfd_owner ();
6566 begin_info_ptr = info_ptr = (section->buffer
6567 + to_underlying (dwo_unit->sect_off));
6568 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6569
6570 if (this_cu->is_debug_types)
6571 {
6572 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6573
6574 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6575 &cu->header, section,
6576 dwo_abbrev_section,
6577 info_ptr, rcuh_kind::TYPE);
6578 /* This is not an assert because it can be caused by bad debug info. */
6579 if (sig_type->signature != cu->header.signature)
6580 {
6581 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6582 " TU at offset %s [in module %s]"),
6583 hex_string (sig_type->signature),
6584 hex_string (cu->header.signature),
6585 sect_offset_str (dwo_unit->sect_off),
6586 bfd_get_filename (abfd));
6587 }
6588 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6589 /* For DWOs coming from DWP files, we don't know the CU length
6590 nor the type's offset in the TU until now. */
6591 dwo_unit->length = cu->header.get_length ();
6592 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6593
6594 /* Establish the type offset that can be used to lookup the type.
6595 For DWO files, we don't know it until now. */
6596 sig_type->type_offset_in_section
6597 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6598 }
6599 else
6600 {
6601 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6602 &cu->header, section,
6603 dwo_abbrev_section,
6604 info_ptr, rcuh_kind::COMPILE);
6605 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6606 /* For DWOs coming from DWP files, we don't know the CU length
6607 until now. */
6608 dwo_unit->length = cu->header.get_length ();
6609 }
6610
6611 *result_dwo_abbrev_table
6612 = abbrev_table::read (objfile, dwo_abbrev_section,
6613 cu->header.abbrev_sect_off);
6614 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6615 result_dwo_abbrev_table->get ());
6616
6617 /* Read in the die, but leave space to copy over the attributes
6618 from the stub. This has the benefit of simplifying the rest of
6619 the code - all the work to maintain the illusion of a single
6620 DW_TAG_{compile,type}_unit DIE is done here. */
6621 num_extra_attrs = ((stmt_list != NULL)
6622 + (low_pc != NULL)
6623 + (high_pc != NULL)
6624 + (ranges != NULL)
6625 + (comp_dir != NULL));
6626 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6627 num_extra_attrs);
6628
6629 /* Copy over the attributes from the stub to the DIE we just read in. */
6630 comp_unit_die = *result_comp_unit_die;
6631 i = comp_unit_die->num_attrs;
6632 if (stmt_list != NULL)
6633 comp_unit_die->attrs[i++] = *stmt_list;
6634 if (low_pc != NULL)
6635 comp_unit_die->attrs[i++] = *low_pc;
6636 if (high_pc != NULL)
6637 comp_unit_die->attrs[i++] = *high_pc;
6638 if (ranges != NULL)
6639 comp_unit_die->attrs[i++] = *ranges;
6640 if (comp_dir != NULL)
6641 comp_unit_die->attrs[i++] = *comp_dir;
6642 comp_unit_die->num_attrs += num_extra_attrs;
6643
6644 if (dwarf_die_debug)
6645 {
6646 fprintf_unfiltered (gdb_stdlog,
6647 "Read die from %s@0x%x of %s:\n",
6648 section->get_name (),
6649 (unsigned) (begin_info_ptr - section->buffer),
6650 bfd_get_filename (abfd));
6651 dump_die (comp_unit_die, dwarf_die_debug);
6652 }
6653
6654 /* Skip dummy compilation units. */
6655 if (info_ptr >= begin_info_ptr + dwo_unit->length
6656 || peek_abbrev_code (abfd, info_ptr) == 0)
6657 return 0;
6658
6659 *result_info_ptr = info_ptr;
6660 return 1;
6661 }
6662
6663 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6664 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6665 signature is part of the header. */
6666 static gdb::optional<ULONGEST>
6667 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6668 {
6669 if (cu->header.version >= 5)
6670 return cu->header.signature;
6671 struct attribute *attr;
6672 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6673 if (attr == nullptr)
6674 return gdb::optional<ULONGEST> ();
6675 return DW_UNSND (attr);
6676 }
6677
6678 /* Subroutine of cutu_reader to simplify it.
6679 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6680 Returns NULL if the specified DWO unit cannot be found. */
6681
6682 static struct dwo_unit *
6683 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6684 struct die_info *comp_unit_die,
6685 const char *dwo_name)
6686 {
6687 struct dwarf2_cu *cu = this_cu->cu;
6688 struct dwo_unit *dwo_unit;
6689 const char *comp_dir;
6690
6691 gdb_assert (cu != NULL);
6692
6693 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6694 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6695 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6696
6697 if (this_cu->is_debug_types)
6698 {
6699 struct signatured_type *sig_type;
6700
6701 /* Since this_cu is the first member of struct signatured_type,
6702 we can go from a pointer to one to a pointer to the other. */
6703 sig_type = (struct signatured_type *) this_cu;
6704 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6705 }
6706 else
6707 {
6708 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6709 if (!signature.has_value ())
6710 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6711 " [in module %s]"),
6712 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6713 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6714 *signature);
6715 }
6716
6717 return dwo_unit;
6718 }
6719
6720 /* Subroutine of cutu_reader to simplify it.
6721 See it for a description of the parameters.
6722 Read a TU directly from a DWO file, bypassing the stub. */
6723
6724 void
6725 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6726 int use_existing_cu, int keep)
6727 {
6728 struct signatured_type *sig_type;
6729 struct die_reader_specs reader;
6730
6731 /* Verify we can do the following downcast, and that we have the
6732 data we need. */
6733 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6734 sig_type = (struct signatured_type *) this_cu;
6735 gdb_assert (sig_type->dwo_unit != NULL);
6736
6737 if (use_existing_cu && this_cu->cu != NULL)
6738 {
6739 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6740 /* There's no need to do the rereading_dwo_cu handling that
6741 cutu_reader does since we don't read the stub. */
6742 }
6743 else
6744 {
6745 /* If !use_existing_cu, this_cu->cu must be NULL. */
6746 gdb_assert (this_cu->cu == NULL);
6747 m_new_cu.reset (new dwarf2_cu (this_cu));
6748 }
6749
6750 /* A future optimization, if needed, would be to use an existing
6751 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6752 could share abbrev tables. */
6753
6754 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6755 NULL /* stub_comp_unit_die */,
6756 sig_type->dwo_unit->dwo_file->comp_dir,
6757 &reader, &info_ptr,
6758 &comp_unit_die,
6759 &m_dwo_abbrev_table) == 0)
6760 {
6761 /* Dummy die. */
6762 dummy_p = true;
6763 }
6764 }
6765
6766 /* Initialize a CU (or TU) and read its DIEs.
6767 If the CU defers to a DWO file, read the DWO file as well.
6768
6769 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6770 Otherwise the table specified in the comp unit header is read in and used.
6771 This is an optimization for when we already have the abbrev table.
6772
6773 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6774 Otherwise, a new CU is allocated with xmalloc.
6775
6776 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
6777 read_in_chain. Otherwise the dwarf2_cu data is freed at the
6778 end. */
6779
6780 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6781 struct abbrev_table *abbrev_table,
6782 int use_existing_cu, int keep,
6783 bool skip_partial)
6784 : die_reader_specs {},
6785 m_this_cu (this_cu),
6786 m_keep (keep)
6787 {
6788 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6789 struct objfile *objfile = dwarf2_per_objfile->objfile;
6790 struct dwarf2_section_info *section = this_cu->section;
6791 bfd *abfd = section->get_bfd_owner ();
6792 struct dwarf2_cu *cu;
6793 const gdb_byte *begin_info_ptr;
6794 struct signatured_type *sig_type = NULL;
6795 struct dwarf2_section_info *abbrev_section;
6796 /* Non-zero if CU currently points to a DWO file and we need to
6797 reread it. When this happens we need to reread the skeleton die
6798 before we can reread the DWO file (this only applies to CUs, not TUs). */
6799 int rereading_dwo_cu = 0;
6800
6801 if (dwarf_die_debug)
6802 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6803 this_cu->is_debug_types ? "type" : "comp",
6804 sect_offset_str (this_cu->sect_off));
6805
6806 if (use_existing_cu)
6807 gdb_assert (keep);
6808
6809 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6810 file (instead of going through the stub), short-circuit all of this. */
6811 if (this_cu->reading_dwo_directly)
6812 {
6813 /* Narrow down the scope of possibilities to have to understand. */
6814 gdb_assert (this_cu->is_debug_types);
6815 gdb_assert (abbrev_table == NULL);
6816 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
6817 return;
6818 }
6819
6820 /* This is cheap if the section is already read in. */
6821 section->read (objfile);
6822
6823 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6824
6825 abbrev_section = get_abbrev_section_for_cu (this_cu);
6826
6827 if (use_existing_cu && this_cu->cu != NULL)
6828 {
6829 cu = this_cu->cu;
6830 /* If this CU is from a DWO file we need to start over, we need to
6831 refetch the attributes from the skeleton CU.
6832 This could be optimized by retrieving those attributes from when we
6833 were here the first time: the previous comp_unit_die was stored in
6834 comp_unit_obstack. But there's no data yet that we need this
6835 optimization. */
6836 if (cu->dwo_unit != NULL)
6837 rereading_dwo_cu = 1;
6838 }
6839 else
6840 {
6841 /* If !use_existing_cu, this_cu->cu must be NULL. */
6842 gdb_assert (this_cu->cu == NULL);
6843 m_new_cu.reset (new dwarf2_cu (this_cu));
6844 cu = m_new_cu.get ();
6845 }
6846
6847 /* Get the header. */
6848 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6849 {
6850 /* We already have the header, there's no need to read it in again. */
6851 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6852 }
6853 else
6854 {
6855 if (this_cu->is_debug_types)
6856 {
6857 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6858 &cu->header, section,
6859 abbrev_section, info_ptr,
6860 rcuh_kind::TYPE);
6861
6862 /* Since per_cu is the first member of struct signatured_type,
6863 we can go from a pointer to one to a pointer to the other. */
6864 sig_type = (struct signatured_type *) this_cu;
6865 gdb_assert (sig_type->signature == cu->header.signature);
6866 gdb_assert (sig_type->type_offset_in_tu
6867 == cu->header.type_cu_offset_in_tu);
6868 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6869
6870 /* LENGTH has not been set yet for type units if we're
6871 using .gdb_index. */
6872 this_cu->length = cu->header.get_length ();
6873
6874 /* Establish the type offset that can be used to lookup the type. */
6875 sig_type->type_offset_in_section =
6876 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6877
6878 this_cu->dwarf_version = cu->header.version;
6879 }
6880 else
6881 {
6882 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6883 &cu->header, section,
6884 abbrev_section,
6885 info_ptr,
6886 rcuh_kind::COMPILE);
6887
6888 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6889 gdb_assert (this_cu->length == cu->header.get_length ());
6890 this_cu->dwarf_version = cu->header.version;
6891 }
6892 }
6893
6894 /* Skip dummy compilation units. */
6895 if (info_ptr >= begin_info_ptr + this_cu->length
6896 || peek_abbrev_code (abfd, info_ptr) == 0)
6897 {
6898 dummy_p = true;
6899 return;
6900 }
6901
6902 /* If we don't have them yet, read the abbrevs for this compilation unit.
6903 And if we need to read them now, make sure they're freed when we're
6904 done. */
6905 if (abbrev_table != NULL)
6906 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6907 else
6908 {
6909 m_abbrev_table_holder
6910 = abbrev_table::read (objfile, abbrev_section,
6911 cu->header.abbrev_sect_off);
6912 abbrev_table = m_abbrev_table_holder.get ();
6913 }
6914
6915 /* Read the top level CU/TU die. */
6916 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6917 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6918
6919 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6920 {
6921 dummy_p = true;
6922 return;
6923 }
6924
6925 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6926 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6927 table from the DWO file and pass the ownership over to us. It will be
6928 referenced from READER, so we must make sure to free it after we're done
6929 with READER.
6930
6931 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6932 DWO CU, that this test will fail (the attribute will not be present). */
6933 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6934 if (dwo_name != nullptr)
6935 {
6936 struct dwo_unit *dwo_unit;
6937 struct die_info *dwo_comp_unit_die;
6938
6939 if (comp_unit_die->has_children)
6940 {
6941 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6942 " has children (offset %s) [in module %s]"),
6943 sect_offset_str (this_cu->sect_off),
6944 bfd_get_filename (abfd));
6945 }
6946 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6947 if (dwo_unit != NULL)
6948 {
6949 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6950 comp_unit_die, NULL,
6951 this, &info_ptr,
6952 &dwo_comp_unit_die,
6953 &m_dwo_abbrev_table) == 0)
6954 {
6955 /* Dummy die. */
6956 dummy_p = true;
6957 return;
6958 }
6959 comp_unit_die = dwo_comp_unit_die;
6960 }
6961 else
6962 {
6963 /* Yikes, we couldn't find the rest of the DIE, we only have
6964 the stub. A complaint has already been logged. There's
6965 not much more we can do except pass on the stub DIE to
6966 die_reader_func. We don't want to throw an error on bad
6967 debug info. */
6968 }
6969 }
6970 }
6971
6972 cutu_reader::~cutu_reader ()
6973 {
6974 /* Done, clean up. */
6975 if (m_new_cu != NULL && m_keep && !dummy_p)
6976 {
6977 struct dwarf2_per_objfile *dwarf2_per_objfile
6978 = m_this_cu->dwarf2_per_objfile;
6979 /* Link this CU into read_in_chain. */
6980 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6981 dwarf2_per_objfile->read_in_chain = m_this_cu;
6982 /* The chain owns it now. */
6983 m_new_cu.release ();
6984 }
6985 }
6986
6987 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6988 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6989 assumed to have already done the lookup to find the DWO file).
6990
6991 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6992 THIS_CU->is_debug_types, but nothing else.
6993
6994 We fill in THIS_CU->length.
6995
6996 THIS_CU->cu is always freed when done.
6997 This is done in order to not leave THIS_CU->cu in a state where we have
6998 to care whether it refers to the "main" CU or the DWO CU.
6999
7000 When parent_cu is passed, it is used to provide a default value for
7001 str_offsets_base and addr_base from the parent. */
7002
7003 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7004 struct dwarf2_cu *parent_cu,
7005 struct dwo_file *dwo_file)
7006 : die_reader_specs {},
7007 m_this_cu (this_cu)
7008 {
7009 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7010 struct objfile *objfile = dwarf2_per_objfile->objfile;
7011 struct dwarf2_section_info *section = this_cu->section;
7012 bfd *abfd = section->get_bfd_owner ();
7013 struct dwarf2_section_info *abbrev_section;
7014 const gdb_byte *begin_info_ptr, *info_ptr;
7015
7016 if (dwarf_die_debug)
7017 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7018 this_cu->is_debug_types ? "type" : "comp",
7019 sect_offset_str (this_cu->sect_off));
7020
7021 gdb_assert (this_cu->cu == NULL);
7022
7023 abbrev_section = (dwo_file != NULL
7024 ? &dwo_file->sections.abbrev
7025 : get_abbrev_section_for_cu (this_cu));
7026
7027 /* This is cheap if the section is already read in. */
7028 section->read (objfile);
7029
7030 m_new_cu.reset (new dwarf2_cu (this_cu));
7031
7032 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7033 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7034 &m_new_cu->header, section,
7035 abbrev_section, info_ptr,
7036 (this_cu->is_debug_types
7037 ? rcuh_kind::TYPE
7038 : rcuh_kind::COMPILE));
7039
7040 if (parent_cu != nullptr)
7041 {
7042 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7043 m_new_cu->addr_base = parent_cu->addr_base;
7044 }
7045 this_cu->length = m_new_cu->header.get_length ();
7046
7047 /* Skip dummy compilation units. */
7048 if (info_ptr >= begin_info_ptr + this_cu->length
7049 || peek_abbrev_code (abfd, info_ptr) == 0)
7050 {
7051 dummy_p = true;
7052 return;
7053 }
7054
7055 m_abbrev_table_holder
7056 = abbrev_table::read (objfile, abbrev_section,
7057 m_new_cu->header.abbrev_sect_off);
7058
7059 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7060 m_abbrev_table_holder.get ());
7061 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7062 }
7063
7064 \f
7065 /* Type Unit Groups.
7066
7067 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7068 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7069 so that all types coming from the same compilation (.o file) are grouped
7070 together. A future step could be to put the types in the same symtab as
7071 the CU the types ultimately came from. */
7072
7073 static hashval_t
7074 hash_type_unit_group (const void *item)
7075 {
7076 const struct type_unit_group *tu_group
7077 = (const struct type_unit_group *) item;
7078
7079 return hash_stmt_list_entry (&tu_group->hash);
7080 }
7081
7082 static int
7083 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7084 {
7085 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7086 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7087
7088 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7089 }
7090
7091 /* Allocate a hash table for type unit groups. */
7092
7093 static htab_up
7094 allocate_type_unit_groups_table (struct objfile *objfile)
7095 {
7096 return htab_up (htab_create_alloc (3,
7097 hash_type_unit_group,
7098 eq_type_unit_group,
7099 NULL, xcalloc, xfree));
7100 }
7101
7102 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7103 partial symtabs. We combine several TUs per psymtab to not let the size
7104 of any one psymtab grow too big. */
7105 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7106 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7107
7108 /* Helper routine for get_type_unit_group.
7109 Create the type_unit_group object used to hold one or more TUs. */
7110
7111 static struct type_unit_group *
7112 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7113 {
7114 struct dwarf2_per_objfile *dwarf2_per_objfile
7115 = cu->per_cu->dwarf2_per_objfile;
7116 struct objfile *objfile = dwarf2_per_objfile->objfile;
7117 struct dwarf2_per_cu_data *per_cu;
7118 struct type_unit_group *tu_group;
7119
7120 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7121 struct type_unit_group);
7122 per_cu = &tu_group->per_cu;
7123 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7124
7125 if (dwarf2_per_objfile->using_index)
7126 {
7127 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7128 struct dwarf2_per_cu_quick_data);
7129 }
7130 else
7131 {
7132 unsigned int line_offset = to_underlying (line_offset_struct);
7133 dwarf2_psymtab *pst;
7134 std::string name;
7135
7136 /* Give the symtab a useful name for debug purposes. */
7137 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7138 name = string_printf ("<type_units_%d>",
7139 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7140 else
7141 name = string_printf ("<type_units_at_0x%x>", line_offset);
7142
7143 pst = create_partial_symtab (per_cu, name.c_str ());
7144 pst->anonymous = true;
7145 }
7146
7147 tu_group->hash.dwo_unit = cu->dwo_unit;
7148 tu_group->hash.line_sect_off = line_offset_struct;
7149
7150 return tu_group;
7151 }
7152
7153 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7154 STMT_LIST is a DW_AT_stmt_list attribute. */
7155
7156 static struct type_unit_group *
7157 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7158 {
7159 struct dwarf2_per_objfile *dwarf2_per_objfile
7160 = cu->per_cu->dwarf2_per_objfile;
7161 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7162 struct type_unit_group *tu_group;
7163 void **slot;
7164 unsigned int line_offset;
7165 struct type_unit_group type_unit_group_for_lookup;
7166
7167 if (dwarf2_per_objfile->type_unit_groups == NULL)
7168 {
7169 dwarf2_per_objfile->type_unit_groups =
7170 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7171 }
7172
7173 /* Do we need to create a new group, or can we use an existing one? */
7174
7175 if (stmt_list)
7176 {
7177 line_offset = DW_UNSND (stmt_list);
7178 ++tu_stats->nr_symtab_sharers;
7179 }
7180 else
7181 {
7182 /* Ugh, no stmt_list. Rare, but we have to handle it.
7183 We can do various things here like create one group per TU or
7184 spread them over multiple groups to split up the expansion work.
7185 To avoid worst case scenarios (too many groups or too large groups)
7186 we, umm, group them in bunches. */
7187 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7188 | (tu_stats->nr_stmt_less_type_units
7189 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7190 ++tu_stats->nr_stmt_less_type_units;
7191 }
7192
7193 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7194 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7195 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7196 &type_unit_group_for_lookup, INSERT);
7197 if (*slot != NULL)
7198 {
7199 tu_group = (struct type_unit_group *) *slot;
7200 gdb_assert (tu_group != NULL);
7201 }
7202 else
7203 {
7204 sect_offset line_offset_struct = (sect_offset) line_offset;
7205 tu_group = create_type_unit_group (cu, line_offset_struct);
7206 *slot = tu_group;
7207 ++tu_stats->nr_symtabs;
7208 }
7209
7210 return tu_group;
7211 }
7212 \f
7213 /* Partial symbol tables. */
7214
7215 /* Create a psymtab named NAME and assign it to PER_CU.
7216
7217 The caller must fill in the following details:
7218 dirname, textlow, texthigh. */
7219
7220 static dwarf2_psymtab *
7221 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7222 {
7223 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7224 dwarf2_psymtab *pst;
7225
7226 pst = new dwarf2_psymtab (name, objfile, 0);
7227
7228 pst->psymtabs_addrmap_supported = true;
7229
7230 /* This is the glue that links PST into GDB's symbol API. */
7231 pst->per_cu_data = per_cu;
7232 per_cu->v.psymtab = pst;
7233
7234 return pst;
7235 }
7236
7237 /* DIE reader function for process_psymtab_comp_unit. */
7238
7239 static void
7240 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7241 const gdb_byte *info_ptr,
7242 struct die_info *comp_unit_die,
7243 int want_partial_unit,
7244 enum language pretend_language)
7245 {
7246 struct dwarf2_cu *cu = reader->cu;
7247 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7248 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7249 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7250 CORE_ADDR baseaddr;
7251 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7252 dwarf2_psymtab *pst;
7253 enum pc_bounds_kind cu_bounds_kind;
7254 const char *filename;
7255
7256 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
7257 return;
7258
7259 gdb_assert (! per_cu->is_debug_types);
7260
7261 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7262
7263 /* Allocate a new partial symbol table structure. */
7264 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7265 if (filename == NULL)
7266 filename = "";
7267
7268 pst = create_partial_symtab (per_cu, filename);
7269
7270 /* This must be done before calling dwarf2_build_include_psymtabs. */
7271 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7272
7273 baseaddr = objfile->text_section_offset ();
7274
7275 dwarf2_find_base_address (comp_unit_die, cu);
7276
7277 /* Possibly set the default values of LOWPC and HIGHPC from
7278 `DW_AT_ranges'. */
7279 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7280 &best_highpc, cu, pst);
7281 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7282 {
7283 CORE_ADDR low
7284 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7285 - baseaddr);
7286 CORE_ADDR high
7287 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7288 - baseaddr - 1);
7289 /* Store the contiguous range if it is not empty; it can be
7290 empty for CUs with no code. */
7291 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7292 low, high, pst);
7293 }
7294
7295 /* Check if comp unit has_children.
7296 If so, read the rest of the partial symbols from this comp unit.
7297 If not, there's no more debug_info for this comp unit. */
7298 if (comp_unit_die->has_children)
7299 {
7300 struct partial_die_info *first_die;
7301 CORE_ADDR lowpc, highpc;
7302
7303 lowpc = ((CORE_ADDR) -1);
7304 highpc = ((CORE_ADDR) 0);
7305
7306 first_die = load_partial_dies (reader, info_ptr, 1);
7307
7308 scan_partial_symbols (first_die, &lowpc, &highpc,
7309 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7310
7311 /* If we didn't find a lowpc, set it to highpc to avoid
7312 complaints from `maint check'. */
7313 if (lowpc == ((CORE_ADDR) -1))
7314 lowpc = highpc;
7315
7316 /* If the compilation unit didn't have an explicit address range,
7317 then use the information extracted from its child dies. */
7318 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7319 {
7320 best_lowpc = lowpc;
7321 best_highpc = highpc;
7322 }
7323 }
7324 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7325 best_lowpc + baseaddr)
7326 - baseaddr);
7327 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7328 best_highpc + baseaddr)
7329 - baseaddr);
7330
7331 end_psymtab_common (objfile, pst);
7332
7333 if (!cu->per_cu->imported_symtabs_empty ())
7334 {
7335 int i;
7336 int len = cu->per_cu->imported_symtabs_size ();
7337
7338 /* Fill in 'dependencies' here; we fill in 'users' in a
7339 post-pass. */
7340 pst->number_of_dependencies = len;
7341 pst->dependencies
7342 = objfile->partial_symtabs->allocate_dependencies (len);
7343 for (i = 0; i < len; ++i)
7344 {
7345 pst->dependencies[i]
7346 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7347 }
7348
7349 cu->per_cu->imported_symtabs_free ();
7350 }
7351
7352 /* Get the list of files included in the current compilation unit,
7353 and build a psymtab for each of them. */
7354 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7355
7356 if (dwarf_read_debug)
7357 fprintf_unfiltered (gdb_stdlog,
7358 "Psymtab for %s unit @%s: %s - %s"
7359 ", %d global, %d static syms\n",
7360 per_cu->is_debug_types ? "type" : "comp",
7361 sect_offset_str (per_cu->sect_off),
7362 paddress (gdbarch, pst->text_low (objfile)),
7363 paddress (gdbarch, pst->text_high (objfile)),
7364 pst->n_global_syms, pst->n_static_syms);
7365 }
7366
7367 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7368 Process compilation unit THIS_CU for a psymtab. */
7369
7370 static void
7371 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7372 int want_partial_unit,
7373 enum language pretend_language)
7374 {
7375 /* If this compilation unit was already read in, free the
7376 cached copy in order to read it in again. This is
7377 necessary because we skipped some symbols when we first
7378 read in the compilation unit (see load_partial_dies).
7379 This problem could be avoided, but the benefit is unclear. */
7380 if (this_cu->cu != NULL)
7381 free_one_cached_comp_unit (this_cu);
7382
7383 cutu_reader reader (this_cu, NULL, 0, 0, false);
7384
7385 if (reader.dummy_p)
7386 {
7387 /* Nothing. */
7388 }
7389 else if (this_cu->is_debug_types)
7390 build_type_psymtabs_reader (&reader, reader.info_ptr,
7391 reader.comp_unit_die);
7392 else
7393 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7394 reader.comp_unit_die,
7395 want_partial_unit,
7396 pretend_language);
7397
7398 /* Age out any secondary CUs. */
7399 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7400 }
7401
7402 /* Reader function for build_type_psymtabs. */
7403
7404 static void
7405 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7406 const gdb_byte *info_ptr,
7407 struct die_info *type_unit_die)
7408 {
7409 struct dwarf2_per_objfile *dwarf2_per_objfile
7410 = reader->cu->per_cu->dwarf2_per_objfile;
7411 struct objfile *objfile = dwarf2_per_objfile->objfile;
7412 struct dwarf2_cu *cu = reader->cu;
7413 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7414 struct signatured_type *sig_type;
7415 struct type_unit_group *tu_group;
7416 struct attribute *attr;
7417 struct partial_die_info *first_die;
7418 CORE_ADDR lowpc, highpc;
7419 dwarf2_psymtab *pst;
7420
7421 gdb_assert (per_cu->is_debug_types);
7422 sig_type = (struct signatured_type *) per_cu;
7423
7424 if (! type_unit_die->has_children)
7425 return;
7426
7427 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7428 tu_group = get_type_unit_group (cu, attr);
7429
7430 if (tu_group->tus == nullptr)
7431 tu_group->tus = new std::vector<signatured_type *>;
7432 tu_group->tus->push_back (sig_type);
7433
7434 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7435 pst = create_partial_symtab (per_cu, "");
7436 pst->anonymous = true;
7437
7438 first_die = load_partial_dies (reader, info_ptr, 1);
7439
7440 lowpc = (CORE_ADDR) -1;
7441 highpc = (CORE_ADDR) 0;
7442 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7443
7444 end_psymtab_common (objfile, pst);
7445 }
7446
7447 /* Struct used to sort TUs by their abbreviation table offset. */
7448
7449 struct tu_abbrev_offset
7450 {
7451 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7452 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7453 {}
7454
7455 signatured_type *sig_type;
7456 sect_offset abbrev_offset;
7457 };
7458
7459 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7460
7461 static bool
7462 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7463 const struct tu_abbrev_offset &b)
7464 {
7465 return a.abbrev_offset < b.abbrev_offset;
7466 }
7467
7468 /* Efficiently read all the type units.
7469 This does the bulk of the work for build_type_psymtabs.
7470
7471 The efficiency is because we sort TUs by the abbrev table they use and
7472 only read each abbrev table once. In one program there are 200K TUs
7473 sharing 8K abbrev tables.
7474
7475 The main purpose of this function is to support building the
7476 dwarf2_per_objfile->type_unit_groups table.
7477 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7478 can collapse the search space by grouping them by stmt_list.
7479 The savings can be significant, in the same program from above the 200K TUs
7480 share 8K stmt_list tables.
7481
7482 FUNC is expected to call get_type_unit_group, which will create the
7483 struct type_unit_group if necessary and add it to
7484 dwarf2_per_objfile->type_unit_groups. */
7485
7486 static void
7487 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7488 {
7489 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7490 abbrev_table_up abbrev_table;
7491 sect_offset abbrev_offset;
7492
7493 /* It's up to the caller to not call us multiple times. */
7494 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7495
7496 if (dwarf2_per_objfile->all_type_units.empty ())
7497 return;
7498
7499 /* TUs typically share abbrev tables, and there can be way more TUs than
7500 abbrev tables. Sort by abbrev table to reduce the number of times we
7501 read each abbrev table in.
7502 Alternatives are to punt or to maintain a cache of abbrev tables.
7503 This is simpler and efficient enough for now.
7504
7505 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7506 symtab to use). Typically TUs with the same abbrev offset have the same
7507 stmt_list value too so in practice this should work well.
7508
7509 The basic algorithm here is:
7510
7511 sort TUs by abbrev table
7512 for each TU with same abbrev table:
7513 read abbrev table if first user
7514 read TU top level DIE
7515 [IWBN if DWO skeletons had DW_AT_stmt_list]
7516 call FUNC */
7517
7518 if (dwarf_read_debug)
7519 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7520
7521 /* Sort in a separate table to maintain the order of all_type_units
7522 for .gdb_index: TU indices directly index all_type_units. */
7523 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7524 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7525
7526 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7527 sorted_by_abbrev.emplace_back
7528 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7529 sig_type->per_cu.section,
7530 sig_type->per_cu.sect_off));
7531
7532 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7533 sort_tu_by_abbrev_offset);
7534
7535 abbrev_offset = (sect_offset) ~(unsigned) 0;
7536
7537 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7538 {
7539 /* Switch to the next abbrev table if necessary. */
7540 if (abbrev_table == NULL
7541 || tu.abbrev_offset != abbrev_offset)
7542 {
7543 abbrev_offset = tu.abbrev_offset;
7544 abbrev_table =
7545 abbrev_table::read (dwarf2_per_objfile->objfile,
7546 &dwarf2_per_objfile->abbrev,
7547 abbrev_offset);
7548 ++tu_stats->nr_uniq_abbrev_tables;
7549 }
7550
7551 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7552 0, 0, false);
7553 if (!reader.dummy_p)
7554 build_type_psymtabs_reader (&reader, reader.info_ptr,
7555 reader.comp_unit_die);
7556 }
7557 }
7558
7559 /* Print collected type unit statistics. */
7560
7561 static void
7562 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7563 {
7564 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7565
7566 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7567 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7568 dwarf2_per_objfile->all_type_units.size ());
7569 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7570 tu_stats->nr_uniq_abbrev_tables);
7571 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7572 tu_stats->nr_symtabs);
7573 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7574 tu_stats->nr_symtab_sharers);
7575 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7576 tu_stats->nr_stmt_less_type_units);
7577 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7578 tu_stats->nr_all_type_units_reallocs);
7579 }
7580
7581 /* Traversal function for build_type_psymtabs. */
7582
7583 static int
7584 build_type_psymtab_dependencies (void **slot, void *info)
7585 {
7586 struct dwarf2_per_objfile *dwarf2_per_objfile
7587 = (struct dwarf2_per_objfile *) info;
7588 struct objfile *objfile = dwarf2_per_objfile->objfile;
7589 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7590 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7591 dwarf2_psymtab *pst = per_cu->v.psymtab;
7592 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7593 int i;
7594
7595 gdb_assert (len > 0);
7596 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
7597
7598 pst->number_of_dependencies = len;
7599 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7600 for (i = 0; i < len; ++i)
7601 {
7602 struct signatured_type *iter = tu_group->tus->at (i);
7603 gdb_assert (iter->per_cu.is_debug_types);
7604 pst->dependencies[i] = iter->per_cu.v.psymtab;
7605 iter->type_unit_group = tu_group;
7606 }
7607
7608 delete tu_group->tus;
7609 tu_group->tus = nullptr;
7610
7611 return 1;
7612 }
7613
7614 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7615 Build partial symbol tables for the .debug_types comp-units. */
7616
7617 static void
7618 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7619 {
7620 if (! create_all_type_units (dwarf2_per_objfile))
7621 return;
7622
7623 build_type_psymtabs_1 (dwarf2_per_objfile);
7624 }
7625
7626 /* Traversal function for process_skeletonless_type_unit.
7627 Read a TU in a DWO file and build partial symbols for it. */
7628
7629 static int
7630 process_skeletonless_type_unit (void **slot, void *info)
7631 {
7632 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7633 struct dwarf2_per_objfile *dwarf2_per_objfile
7634 = (struct dwarf2_per_objfile *) info;
7635 struct signatured_type find_entry, *entry;
7636
7637 /* If this TU doesn't exist in the global table, add it and read it in. */
7638
7639 if (dwarf2_per_objfile->signatured_types == NULL)
7640 {
7641 dwarf2_per_objfile->signatured_types
7642 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
7643 }
7644
7645 find_entry.signature = dwo_unit->signature;
7646 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7647 &find_entry, INSERT);
7648 /* If we've already seen this type there's nothing to do. What's happening
7649 is we're doing our own version of comdat-folding here. */
7650 if (*slot != NULL)
7651 return 1;
7652
7653 /* This does the job that create_all_type_units would have done for
7654 this TU. */
7655 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7656 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7657 *slot = entry;
7658
7659 /* This does the job that build_type_psymtabs_1 would have done. */
7660 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
7661 if (!reader.dummy_p)
7662 build_type_psymtabs_reader (&reader, reader.info_ptr,
7663 reader.comp_unit_die);
7664
7665 return 1;
7666 }
7667
7668 /* Traversal function for process_skeletonless_type_units. */
7669
7670 static int
7671 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7672 {
7673 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7674
7675 if (dwo_file->tus != NULL)
7676 htab_traverse_noresize (dwo_file->tus.get (),
7677 process_skeletonless_type_unit, info);
7678
7679 return 1;
7680 }
7681
7682 /* Scan all TUs of DWO files, verifying we've processed them.
7683 This is needed in case a TU was emitted without its skeleton.
7684 Note: This can't be done until we know what all the DWO files are. */
7685
7686 static void
7687 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7688 {
7689 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7690 if (get_dwp_file (dwarf2_per_objfile) == NULL
7691 && dwarf2_per_objfile->dwo_files != NULL)
7692 {
7693 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7694 process_dwo_file_for_skeletonless_type_units,
7695 dwarf2_per_objfile);
7696 }
7697 }
7698
7699 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7700
7701 static void
7702 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7703 {
7704 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7705 {
7706 dwarf2_psymtab *pst = per_cu->v.psymtab;
7707
7708 if (pst == NULL)
7709 continue;
7710
7711 for (int j = 0; j < pst->number_of_dependencies; ++j)
7712 {
7713 /* Set the 'user' field only if it is not already set. */
7714 if (pst->dependencies[j]->user == NULL)
7715 pst->dependencies[j]->user = pst;
7716 }
7717 }
7718 }
7719
7720 /* Build the partial symbol table by doing a quick pass through the
7721 .debug_info and .debug_abbrev sections. */
7722
7723 static void
7724 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7725 {
7726 struct objfile *objfile = dwarf2_per_objfile->objfile;
7727
7728 if (dwarf_read_debug)
7729 {
7730 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7731 objfile_name (objfile));
7732 }
7733
7734 dwarf2_per_objfile->reading_partial_symbols = 1;
7735
7736 dwarf2_per_objfile->info.read (objfile);
7737
7738 /* Any cached compilation units will be linked by the per-objfile
7739 read_in_chain. Make sure to free them when we're done. */
7740 free_cached_comp_units freer (dwarf2_per_objfile);
7741
7742 build_type_psymtabs (dwarf2_per_objfile);
7743
7744 create_all_comp_units (dwarf2_per_objfile);
7745
7746 /* Create a temporary address map on a temporary obstack. We later
7747 copy this to the final obstack. */
7748 auto_obstack temp_obstack;
7749
7750 scoped_restore save_psymtabs_addrmap
7751 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7752 addrmap_create_mutable (&temp_obstack));
7753
7754 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7755 process_psymtab_comp_unit (per_cu, 0, language_minimal);
7756
7757 /* This has to wait until we read the CUs, we need the list of DWOs. */
7758 process_skeletonless_type_units (dwarf2_per_objfile);
7759
7760 /* Now that all TUs have been processed we can fill in the dependencies. */
7761 if (dwarf2_per_objfile->type_unit_groups != NULL)
7762 {
7763 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7764 build_type_psymtab_dependencies, dwarf2_per_objfile);
7765 }
7766
7767 if (dwarf_read_debug)
7768 print_tu_stats (dwarf2_per_objfile);
7769
7770 set_partial_user (dwarf2_per_objfile);
7771
7772 objfile->partial_symtabs->psymtabs_addrmap
7773 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7774 objfile->partial_symtabs->obstack ());
7775 /* At this point we want to keep the address map. */
7776 save_psymtabs_addrmap.release ();
7777
7778 if (dwarf_read_debug)
7779 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7780 objfile_name (objfile));
7781 }
7782
7783 /* Load the partial DIEs for a secondary CU into memory.
7784 This is also used when rereading a primary CU with load_all_dies. */
7785
7786 static void
7787 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7788 {
7789 cutu_reader reader (this_cu, NULL, 1, 1, false);
7790
7791 if (!reader.dummy_p)
7792 {
7793 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7794 language_minimal);
7795
7796 /* Check if comp unit has_children.
7797 If so, read the rest of the partial symbols from this comp unit.
7798 If not, there's no more debug_info for this comp unit. */
7799 if (reader.comp_unit_die->has_children)
7800 load_partial_dies (&reader, reader.info_ptr, 0);
7801 }
7802 }
7803
7804 static void
7805 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7806 struct dwarf2_section_info *section,
7807 struct dwarf2_section_info *abbrev_section,
7808 unsigned int is_dwz)
7809 {
7810 const gdb_byte *info_ptr;
7811 struct objfile *objfile = dwarf2_per_objfile->objfile;
7812
7813 if (dwarf_read_debug)
7814 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7815 section->get_name (),
7816 section->get_file_name ());
7817
7818 section->read (objfile);
7819
7820 info_ptr = section->buffer;
7821
7822 while (info_ptr < section->buffer + section->size)
7823 {
7824 struct dwarf2_per_cu_data *this_cu;
7825
7826 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7827
7828 comp_unit_head cu_header;
7829 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7830 abbrev_section, info_ptr,
7831 rcuh_kind::COMPILE);
7832
7833 /* Save the compilation unit for later lookup. */
7834 if (cu_header.unit_type != DW_UT_type)
7835 {
7836 this_cu = XOBNEW (&objfile->objfile_obstack,
7837 struct dwarf2_per_cu_data);
7838 memset (this_cu, 0, sizeof (*this_cu));
7839 }
7840 else
7841 {
7842 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7843 struct signatured_type);
7844 memset (sig_type, 0, sizeof (*sig_type));
7845 sig_type->signature = cu_header.signature;
7846 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7847 this_cu = &sig_type->per_cu;
7848 }
7849 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7850 this_cu->sect_off = sect_off;
7851 this_cu->length = cu_header.length + cu_header.initial_length_size;
7852 this_cu->is_dwz = is_dwz;
7853 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7854 this_cu->section = section;
7855
7856 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7857
7858 info_ptr = info_ptr + this_cu->length;
7859 }
7860 }
7861
7862 /* Create a list of all compilation units in OBJFILE.
7863 This is only done for -readnow and building partial symtabs. */
7864
7865 static void
7866 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7867 {
7868 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7869 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7870 &dwarf2_per_objfile->abbrev, 0);
7871
7872 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7873 if (dwz != NULL)
7874 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7875 1);
7876 }
7877
7878 /* Process all loaded DIEs for compilation unit CU, starting at
7879 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7880 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7881 DW_AT_ranges). See the comments of add_partial_subprogram on how
7882 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7883
7884 static void
7885 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7886 CORE_ADDR *highpc, int set_addrmap,
7887 struct dwarf2_cu *cu)
7888 {
7889 struct partial_die_info *pdi;
7890
7891 /* Now, march along the PDI's, descending into ones which have
7892 interesting children but skipping the children of the other ones,
7893 until we reach the end of the compilation unit. */
7894
7895 pdi = first_die;
7896
7897 while (pdi != NULL)
7898 {
7899 pdi->fixup (cu);
7900
7901 /* Anonymous namespaces or modules have no name but have interesting
7902 children, so we need to look at them. Ditto for anonymous
7903 enums. */
7904
7905 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7906 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7907 || pdi->tag == DW_TAG_imported_unit
7908 || pdi->tag == DW_TAG_inlined_subroutine)
7909 {
7910 switch (pdi->tag)
7911 {
7912 case DW_TAG_subprogram:
7913 case DW_TAG_inlined_subroutine:
7914 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7915 break;
7916 case DW_TAG_constant:
7917 case DW_TAG_variable:
7918 case DW_TAG_typedef:
7919 case DW_TAG_union_type:
7920 if (!pdi->is_declaration)
7921 {
7922 add_partial_symbol (pdi, cu);
7923 }
7924 break;
7925 case DW_TAG_class_type:
7926 case DW_TAG_interface_type:
7927 case DW_TAG_structure_type:
7928 if (!pdi->is_declaration)
7929 {
7930 add_partial_symbol (pdi, cu);
7931 }
7932 if ((cu->language == language_rust
7933 || cu->language == language_cplus) && pdi->has_children)
7934 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7935 set_addrmap, cu);
7936 break;
7937 case DW_TAG_enumeration_type:
7938 if (!pdi->is_declaration)
7939 add_partial_enumeration (pdi, cu);
7940 break;
7941 case DW_TAG_base_type:
7942 case DW_TAG_subrange_type:
7943 /* File scope base type definitions are added to the partial
7944 symbol table. */
7945 add_partial_symbol (pdi, cu);
7946 break;
7947 case DW_TAG_namespace:
7948 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7949 break;
7950 case DW_TAG_module:
7951 if (!pdi->is_declaration)
7952 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7953 break;
7954 case DW_TAG_imported_unit:
7955 {
7956 struct dwarf2_per_cu_data *per_cu;
7957
7958 /* For now we don't handle imported units in type units. */
7959 if (cu->per_cu->is_debug_types)
7960 {
7961 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7962 " supported in type units [in module %s]"),
7963 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7964 }
7965
7966 per_cu = dwarf2_find_containing_comp_unit
7967 (pdi->d.sect_off, pdi->is_dwz,
7968 cu->per_cu->dwarf2_per_objfile);
7969
7970 /* Go read the partial unit, if needed. */
7971 if (per_cu->v.psymtab == NULL)
7972 process_psymtab_comp_unit (per_cu, 1, cu->language);
7973
7974 cu->per_cu->imported_symtabs_push (per_cu);
7975 }
7976 break;
7977 case DW_TAG_imported_declaration:
7978 add_partial_symbol (pdi, cu);
7979 break;
7980 default:
7981 break;
7982 }
7983 }
7984
7985 /* If the die has a sibling, skip to the sibling. */
7986
7987 pdi = pdi->die_sibling;
7988 }
7989 }
7990
7991 /* Functions used to compute the fully scoped name of a partial DIE.
7992
7993 Normally, this is simple. For C++, the parent DIE's fully scoped
7994 name is concatenated with "::" and the partial DIE's name.
7995 Enumerators are an exception; they use the scope of their parent
7996 enumeration type, i.e. the name of the enumeration type is not
7997 prepended to the enumerator.
7998
7999 There are two complexities. One is DW_AT_specification; in this
8000 case "parent" means the parent of the target of the specification,
8001 instead of the direct parent of the DIE. The other is compilers
8002 which do not emit DW_TAG_namespace; in this case we try to guess
8003 the fully qualified name of structure types from their members'
8004 linkage names. This must be done using the DIE's children rather
8005 than the children of any DW_AT_specification target. We only need
8006 to do this for structures at the top level, i.e. if the target of
8007 any DW_AT_specification (if any; otherwise the DIE itself) does not
8008 have a parent. */
8009
8010 /* Compute the scope prefix associated with PDI's parent, in
8011 compilation unit CU. The result will be allocated on CU's
8012 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8013 field. NULL is returned if no prefix is necessary. */
8014 static const char *
8015 partial_die_parent_scope (struct partial_die_info *pdi,
8016 struct dwarf2_cu *cu)
8017 {
8018 const char *grandparent_scope;
8019 struct partial_die_info *parent, *real_pdi;
8020
8021 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8022 then this means the parent of the specification DIE. */
8023
8024 real_pdi = pdi;
8025 while (real_pdi->has_specification)
8026 {
8027 auto res = find_partial_die (real_pdi->spec_offset,
8028 real_pdi->spec_is_dwz, cu);
8029 real_pdi = res.pdi;
8030 cu = res.cu;
8031 }
8032
8033 parent = real_pdi->die_parent;
8034 if (parent == NULL)
8035 return NULL;
8036
8037 if (parent->scope_set)
8038 return parent->scope;
8039
8040 parent->fixup (cu);
8041
8042 grandparent_scope = partial_die_parent_scope (parent, cu);
8043
8044 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8045 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8046 Work around this problem here. */
8047 if (cu->language == language_cplus
8048 && parent->tag == DW_TAG_namespace
8049 && strcmp (parent->name, "::") == 0
8050 && grandparent_scope == NULL)
8051 {
8052 parent->scope = NULL;
8053 parent->scope_set = 1;
8054 return NULL;
8055 }
8056
8057 /* Nested subroutines in Fortran get a prefix. */
8058 if (pdi->tag == DW_TAG_enumerator)
8059 /* Enumerators should not get the name of the enumeration as a prefix. */
8060 parent->scope = grandparent_scope;
8061 else if (parent->tag == DW_TAG_namespace
8062 || parent->tag == DW_TAG_module
8063 || parent->tag == DW_TAG_structure_type
8064 || parent->tag == DW_TAG_class_type
8065 || parent->tag == DW_TAG_interface_type
8066 || parent->tag == DW_TAG_union_type
8067 || parent->tag == DW_TAG_enumeration_type
8068 || (cu->language == language_fortran
8069 && parent->tag == DW_TAG_subprogram
8070 && pdi->tag == DW_TAG_subprogram))
8071 {
8072 if (grandparent_scope == NULL)
8073 parent->scope = parent->name;
8074 else
8075 parent->scope = typename_concat (&cu->comp_unit_obstack,
8076 grandparent_scope,
8077 parent->name, 0, cu);
8078 }
8079 else
8080 {
8081 /* FIXME drow/2004-04-01: What should we be doing with
8082 function-local names? For partial symbols, we should probably be
8083 ignoring them. */
8084 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8085 dwarf_tag_name (parent->tag),
8086 sect_offset_str (pdi->sect_off));
8087 parent->scope = grandparent_scope;
8088 }
8089
8090 parent->scope_set = 1;
8091 return parent->scope;
8092 }
8093
8094 /* Return the fully scoped name associated with PDI, from compilation unit
8095 CU. The result will be allocated with malloc. */
8096
8097 static gdb::unique_xmalloc_ptr<char>
8098 partial_die_full_name (struct partial_die_info *pdi,
8099 struct dwarf2_cu *cu)
8100 {
8101 const char *parent_scope;
8102
8103 /* If this is a template instantiation, we can not work out the
8104 template arguments from partial DIEs. So, unfortunately, we have
8105 to go through the full DIEs. At least any work we do building
8106 types here will be reused if full symbols are loaded later. */
8107 if (pdi->has_template_arguments)
8108 {
8109 pdi->fixup (cu);
8110
8111 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8112 {
8113 struct die_info *die;
8114 struct attribute attr;
8115 struct dwarf2_cu *ref_cu = cu;
8116
8117 /* DW_FORM_ref_addr is using section offset. */
8118 attr.name = (enum dwarf_attribute) 0;
8119 attr.form = DW_FORM_ref_addr;
8120 attr.u.unsnd = to_underlying (pdi->sect_off);
8121 die = follow_die_ref (NULL, &attr, &ref_cu);
8122
8123 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8124 }
8125 }
8126
8127 parent_scope = partial_die_parent_scope (pdi, cu);
8128 if (parent_scope == NULL)
8129 return NULL;
8130 else
8131 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8132 pdi->name, 0, cu));
8133 }
8134
8135 static void
8136 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8137 {
8138 struct dwarf2_per_objfile *dwarf2_per_objfile
8139 = cu->per_cu->dwarf2_per_objfile;
8140 struct objfile *objfile = dwarf2_per_objfile->objfile;
8141 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8142 CORE_ADDR addr = 0;
8143 const char *actual_name = NULL;
8144 CORE_ADDR baseaddr;
8145
8146 baseaddr = objfile->text_section_offset ();
8147
8148 gdb::unique_xmalloc_ptr<char> built_actual_name
8149 = partial_die_full_name (pdi, cu);
8150 if (built_actual_name != NULL)
8151 actual_name = built_actual_name.get ();
8152
8153 if (actual_name == NULL)
8154 actual_name = pdi->name;
8155
8156 switch (pdi->tag)
8157 {
8158 case DW_TAG_inlined_subroutine:
8159 case DW_TAG_subprogram:
8160 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8161 - baseaddr);
8162 if (pdi->is_external
8163 || cu->language == language_ada
8164 || (cu->language == language_fortran
8165 && pdi->die_parent != NULL
8166 && pdi->die_parent->tag == DW_TAG_subprogram))
8167 {
8168 /* Normally, only "external" DIEs are part of the global scope.
8169 But in Ada and Fortran, we want to be able to access nested
8170 procedures globally. So all Ada and Fortran subprograms are
8171 stored in the global scope. */
8172 add_psymbol_to_list (actual_name,
8173 built_actual_name != NULL,
8174 VAR_DOMAIN, LOC_BLOCK,
8175 SECT_OFF_TEXT (objfile),
8176 psymbol_placement::GLOBAL,
8177 addr,
8178 cu->language, objfile);
8179 }
8180 else
8181 {
8182 add_psymbol_to_list (actual_name,
8183 built_actual_name != NULL,
8184 VAR_DOMAIN, LOC_BLOCK,
8185 SECT_OFF_TEXT (objfile),
8186 psymbol_placement::STATIC,
8187 addr, cu->language, objfile);
8188 }
8189
8190 if (pdi->main_subprogram && actual_name != NULL)
8191 set_objfile_main_name (objfile, actual_name, cu->language);
8192 break;
8193 case DW_TAG_constant:
8194 add_psymbol_to_list (actual_name,
8195 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8196 -1, (pdi->is_external
8197 ? psymbol_placement::GLOBAL
8198 : psymbol_placement::STATIC),
8199 0, cu->language, objfile);
8200 break;
8201 case DW_TAG_variable:
8202 if (pdi->d.locdesc)
8203 addr = decode_locdesc (pdi->d.locdesc, cu);
8204
8205 if (pdi->d.locdesc
8206 && addr == 0
8207 && !dwarf2_per_objfile->has_section_at_zero)
8208 {
8209 /* A global or static variable may also have been stripped
8210 out by the linker if unused, in which case its address
8211 will be nullified; do not add such variables into partial
8212 symbol table then. */
8213 }
8214 else if (pdi->is_external)
8215 {
8216 /* Global Variable.
8217 Don't enter into the minimal symbol tables as there is
8218 a minimal symbol table entry from the ELF symbols already.
8219 Enter into partial symbol table if it has a location
8220 descriptor or a type.
8221 If the location descriptor is missing, new_symbol will create
8222 a LOC_UNRESOLVED symbol, the address of the variable will then
8223 be determined from the minimal symbol table whenever the variable
8224 is referenced.
8225 The address for the partial symbol table entry is not
8226 used by GDB, but it comes in handy for debugging partial symbol
8227 table building. */
8228
8229 if (pdi->d.locdesc || pdi->has_type)
8230 add_psymbol_to_list (actual_name,
8231 built_actual_name != NULL,
8232 VAR_DOMAIN, LOC_STATIC,
8233 SECT_OFF_TEXT (objfile),
8234 psymbol_placement::GLOBAL,
8235 addr, cu->language, objfile);
8236 }
8237 else
8238 {
8239 int has_loc = pdi->d.locdesc != NULL;
8240
8241 /* Static Variable. Skip symbols whose value we cannot know (those
8242 without location descriptors or constant values). */
8243 if (!has_loc && !pdi->has_const_value)
8244 return;
8245
8246 add_psymbol_to_list (actual_name,
8247 built_actual_name != NULL,
8248 VAR_DOMAIN, LOC_STATIC,
8249 SECT_OFF_TEXT (objfile),
8250 psymbol_placement::STATIC,
8251 has_loc ? addr : 0,
8252 cu->language, objfile);
8253 }
8254 break;
8255 case DW_TAG_typedef:
8256 case DW_TAG_base_type:
8257 case DW_TAG_subrange_type:
8258 add_psymbol_to_list (actual_name,
8259 built_actual_name != NULL,
8260 VAR_DOMAIN, LOC_TYPEDEF, -1,
8261 psymbol_placement::STATIC,
8262 0, cu->language, objfile);
8263 break;
8264 case DW_TAG_imported_declaration:
8265 case DW_TAG_namespace:
8266 add_psymbol_to_list (actual_name,
8267 built_actual_name != NULL,
8268 VAR_DOMAIN, LOC_TYPEDEF, -1,
8269 psymbol_placement::GLOBAL,
8270 0, cu->language, objfile);
8271 break;
8272 case DW_TAG_module:
8273 /* With Fortran 77 there might be a "BLOCK DATA" module
8274 available without any name. If so, we skip the module as it
8275 doesn't bring any value. */
8276 if (actual_name != nullptr)
8277 add_psymbol_to_list (actual_name,
8278 built_actual_name != NULL,
8279 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8280 psymbol_placement::GLOBAL,
8281 0, cu->language, objfile);
8282 break;
8283 case DW_TAG_class_type:
8284 case DW_TAG_interface_type:
8285 case DW_TAG_structure_type:
8286 case DW_TAG_union_type:
8287 case DW_TAG_enumeration_type:
8288 /* Skip external references. The DWARF standard says in the section
8289 about "Structure, Union, and Class Type Entries": "An incomplete
8290 structure, union or class type is represented by a structure,
8291 union or class entry that does not have a byte size attribute
8292 and that has a DW_AT_declaration attribute." */
8293 if (!pdi->has_byte_size && pdi->is_declaration)
8294 return;
8295
8296 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8297 static vs. global. */
8298 add_psymbol_to_list (actual_name,
8299 built_actual_name != NULL,
8300 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8301 cu->language == language_cplus
8302 ? psymbol_placement::GLOBAL
8303 : psymbol_placement::STATIC,
8304 0, cu->language, objfile);
8305
8306 break;
8307 case DW_TAG_enumerator:
8308 add_psymbol_to_list (actual_name,
8309 built_actual_name != NULL,
8310 VAR_DOMAIN, LOC_CONST, -1,
8311 cu->language == language_cplus
8312 ? psymbol_placement::GLOBAL
8313 : psymbol_placement::STATIC,
8314 0, cu->language, objfile);
8315 break;
8316 default:
8317 break;
8318 }
8319 }
8320
8321 /* Read a partial die corresponding to a namespace; also, add a symbol
8322 corresponding to that namespace to the symbol table. NAMESPACE is
8323 the name of the enclosing namespace. */
8324
8325 static void
8326 add_partial_namespace (struct partial_die_info *pdi,
8327 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8328 int set_addrmap, struct dwarf2_cu *cu)
8329 {
8330 /* Add a symbol for the namespace. */
8331
8332 add_partial_symbol (pdi, cu);
8333
8334 /* Now scan partial symbols in that namespace. */
8335
8336 if (pdi->has_children)
8337 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8338 }
8339
8340 /* Read a partial die corresponding to a Fortran module. */
8341
8342 static void
8343 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8344 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8345 {
8346 /* Add a symbol for the namespace. */
8347
8348 add_partial_symbol (pdi, cu);
8349
8350 /* Now scan partial symbols in that module. */
8351
8352 if (pdi->has_children)
8353 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8354 }
8355
8356 /* Read a partial die corresponding to a subprogram or an inlined
8357 subprogram and create a partial symbol for that subprogram.
8358 When the CU language allows it, this routine also defines a partial
8359 symbol for each nested subprogram that this subprogram contains.
8360 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8361 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8362
8363 PDI may also be a lexical block, in which case we simply search
8364 recursively for subprograms defined inside that lexical block.
8365 Again, this is only performed when the CU language allows this
8366 type of definitions. */
8367
8368 static void
8369 add_partial_subprogram (struct partial_die_info *pdi,
8370 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8371 int set_addrmap, struct dwarf2_cu *cu)
8372 {
8373 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8374 {
8375 if (pdi->has_pc_info)
8376 {
8377 if (pdi->lowpc < *lowpc)
8378 *lowpc = pdi->lowpc;
8379 if (pdi->highpc > *highpc)
8380 *highpc = pdi->highpc;
8381 if (set_addrmap)
8382 {
8383 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8384 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8385 CORE_ADDR baseaddr;
8386 CORE_ADDR this_highpc;
8387 CORE_ADDR this_lowpc;
8388
8389 baseaddr = objfile->text_section_offset ();
8390 this_lowpc
8391 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8392 pdi->lowpc + baseaddr)
8393 - baseaddr);
8394 this_highpc
8395 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8396 pdi->highpc + baseaddr)
8397 - baseaddr);
8398 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8399 this_lowpc, this_highpc - 1,
8400 cu->per_cu->v.psymtab);
8401 }
8402 }
8403
8404 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8405 {
8406 if (!pdi->is_declaration)
8407 /* Ignore subprogram DIEs that do not have a name, they are
8408 illegal. Do not emit a complaint at this point, we will
8409 do so when we convert this psymtab into a symtab. */
8410 if (pdi->name)
8411 add_partial_symbol (pdi, cu);
8412 }
8413 }
8414
8415 if (! pdi->has_children)
8416 return;
8417
8418 if (cu->language == language_ada || cu->language == language_fortran)
8419 {
8420 pdi = pdi->die_child;
8421 while (pdi != NULL)
8422 {
8423 pdi->fixup (cu);
8424 if (pdi->tag == DW_TAG_subprogram
8425 || pdi->tag == DW_TAG_inlined_subroutine
8426 || pdi->tag == DW_TAG_lexical_block)
8427 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8428 pdi = pdi->die_sibling;
8429 }
8430 }
8431 }
8432
8433 /* Read a partial die corresponding to an enumeration type. */
8434
8435 static void
8436 add_partial_enumeration (struct partial_die_info *enum_pdi,
8437 struct dwarf2_cu *cu)
8438 {
8439 struct partial_die_info *pdi;
8440
8441 if (enum_pdi->name != NULL)
8442 add_partial_symbol (enum_pdi, cu);
8443
8444 pdi = enum_pdi->die_child;
8445 while (pdi)
8446 {
8447 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8448 complaint (_("malformed enumerator DIE ignored"));
8449 else
8450 add_partial_symbol (pdi, cu);
8451 pdi = pdi->die_sibling;
8452 }
8453 }
8454
8455 /* Return the initial uleb128 in the die at INFO_PTR. */
8456
8457 static unsigned int
8458 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8459 {
8460 unsigned int bytes_read;
8461
8462 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8463 }
8464
8465 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8466 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8467
8468 Return the corresponding abbrev, or NULL if the number is zero (indicating
8469 an empty DIE). In either case *BYTES_READ will be set to the length of
8470 the initial number. */
8471
8472 static struct abbrev_info *
8473 peek_die_abbrev (const die_reader_specs &reader,
8474 const gdb_byte *info_ptr, unsigned int *bytes_read)
8475 {
8476 dwarf2_cu *cu = reader.cu;
8477 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8478 unsigned int abbrev_number
8479 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8480
8481 if (abbrev_number == 0)
8482 return NULL;
8483
8484 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8485 if (!abbrev)
8486 {
8487 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8488 " at offset %s [in module %s]"),
8489 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8490 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8491 }
8492
8493 return abbrev;
8494 }
8495
8496 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8497 Returns a pointer to the end of a series of DIEs, terminated by an empty
8498 DIE. Any children of the skipped DIEs will also be skipped. */
8499
8500 static const gdb_byte *
8501 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8502 {
8503 while (1)
8504 {
8505 unsigned int bytes_read;
8506 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8507
8508 if (abbrev == NULL)
8509 return info_ptr + bytes_read;
8510 else
8511 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8512 }
8513 }
8514
8515 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8516 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8517 abbrev corresponding to that skipped uleb128 should be passed in
8518 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8519 children. */
8520
8521 static const gdb_byte *
8522 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8523 struct abbrev_info *abbrev)
8524 {
8525 unsigned int bytes_read;
8526 struct attribute attr;
8527 bfd *abfd = reader->abfd;
8528 struct dwarf2_cu *cu = reader->cu;
8529 const gdb_byte *buffer = reader->buffer;
8530 const gdb_byte *buffer_end = reader->buffer_end;
8531 unsigned int form, i;
8532
8533 for (i = 0; i < abbrev->num_attrs; i++)
8534 {
8535 /* The only abbrev we care about is DW_AT_sibling. */
8536 if (abbrev->attrs[i].name == DW_AT_sibling)
8537 {
8538 bool ignored;
8539 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8540 &ignored);
8541 if (attr.form == DW_FORM_ref_addr)
8542 complaint (_("ignoring absolute DW_AT_sibling"));
8543 else
8544 {
8545 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8546 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8547
8548 if (sibling_ptr < info_ptr)
8549 complaint (_("DW_AT_sibling points backwards"));
8550 else if (sibling_ptr > reader->buffer_end)
8551 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8552 else
8553 return sibling_ptr;
8554 }
8555 }
8556
8557 /* If it isn't DW_AT_sibling, skip this attribute. */
8558 form = abbrev->attrs[i].form;
8559 skip_attribute:
8560 switch (form)
8561 {
8562 case DW_FORM_ref_addr:
8563 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8564 and later it is offset sized. */
8565 if (cu->header.version == 2)
8566 info_ptr += cu->header.addr_size;
8567 else
8568 info_ptr += cu->header.offset_size;
8569 break;
8570 case DW_FORM_GNU_ref_alt:
8571 info_ptr += cu->header.offset_size;
8572 break;
8573 case DW_FORM_addr:
8574 info_ptr += cu->header.addr_size;
8575 break;
8576 case DW_FORM_data1:
8577 case DW_FORM_ref1:
8578 case DW_FORM_flag:
8579 case DW_FORM_strx1:
8580 info_ptr += 1;
8581 break;
8582 case DW_FORM_flag_present:
8583 case DW_FORM_implicit_const:
8584 break;
8585 case DW_FORM_data2:
8586 case DW_FORM_ref2:
8587 case DW_FORM_strx2:
8588 info_ptr += 2;
8589 break;
8590 case DW_FORM_strx3:
8591 info_ptr += 3;
8592 break;
8593 case DW_FORM_data4:
8594 case DW_FORM_ref4:
8595 case DW_FORM_strx4:
8596 info_ptr += 4;
8597 break;
8598 case DW_FORM_data8:
8599 case DW_FORM_ref8:
8600 case DW_FORM_ref_sig8:
8601 info_ptr += 8;
8602 break;
8603 case DW_FORM_data16:
8604 info_ptr += 16;
8605 break;
8606 case DW_FORM_string:
8607 read_direct_string (abfd, info_ptr, &bytes_read);
8608 info_ptr += bytes_read;
8609 break;
8610 case DW_FORM_sec_offset:
8611 case DW_FORM_strp:
8612 case DW_FORM_GNU_strp_alt:
8613 info_ptr += cu->header.offset_size;
8614 break;
8615 case DW_FORM_exprloc:
8616 case DW_FORM_block:
8617 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8618 info_ptr += bytes_read;
8619 break;
8620 case DW_FORM_block1:
8621 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8622 break;
8623 case DW_FORM_block2:
8624 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8625 break;
8626 case DW_FORM_block4:
8627 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8628 break;
8629 case DW_FORM_addrx:
8630 case DW_FORM_strx:
8631 case DW_FORM_sdata:
8632 case DW_FORM_udata:
8633 case DW_FORM_ref_udata:
8634 case DW_FORM_GNU_addr_index:
8635 case DW_FORM_GNU_str_index:
8636 case DW_FORM_rnglistx:
8637 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8638 break;
8639 case DW_FORM_indirect:
8640 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8641 info_ptr += bytes_read;
8642 /* We need to continue parsing from here, so just go back to
8643 the top. */
8644 goto skip_attribute;
8645
8646 default:
8647 error (_("Dwarf Error: Cannot handle %s "
8648 "in DWARF reader [in module %s]"),
8649 dwarf_form_name (form),
8650 bfd_get_filename (abfd));
8651 }
8652 }
8653
8654 if (abbrev->has_children)
8655 return skip_children (reader, info_ptr);
8656 else
8657 return info_ptr;
8658 }
8659
8660 /* Locate ORIG_PDI's sibling.
8661 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8662
8663 static const gdb_byte *
8664 locate_pdi_sibling (const struct die_reader_specs *reader,
8665 struct partial_die_info *orig_pdi,
8666 const gdb_byte *info_ptr)
8667 {
8668 /* Do we know the sibling already? */
8669
8670 if (orig_pdi->sibling)
8671 return orig_pdi->sibling;
8672
8673 /* Are there any children to deal with? */
8674
8675 if (!orig_pdi->has_children)
8676 return info_ptr;
8677
8678 /* Skip the children the long way. */
8679
8680 return skip_children (reader, info_ptr);
8681 }
8682
8683 /* Expand this partial symbol table into a full symbol table. SELF is
8684 not NULL. */
8685
8686 void
8687 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8688 {
8689 struct dwarf2_per_objfile *dwarf2_per_objfile
8690 = get_dwarf2_per_objfile (objfile);
8691
8692 gdb_assert (!readin);
8693 /* If this psymtab is constructed from a debug-only objfile, the
8694 has_section_at_zero flag will not necessarily be correct. We
8695 can get the correct value for this flag by looking at the data
8696 associated with the (presumably stripped) associated objfile. */
8697 if (objfile->separate_debug_objfile_backlink)
8698 {
8699 struct dwarf2_per_objfile *dpo_backlink
8700 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8701
8702 dwarf2_per_objfile->has_section_at_zero
8703 = dpo_backlink->has_section_at_zero;
8704 }
8705
8706 dwarf2_per_objfile->reading_partial_symbols = 0;
8707
8708 expand_psymtab (objfile);
8709
8710 process_cu_includes (dwarf2_per_objfile);
8711 }
8712 \f
8713 /* Reading in full CUs. */
8714
8715 /* Add PER_CU to the queue. */
8716
8717 static void
8718 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8719 enum language pretend_language)
8720 {
8721 per_cu->queued = 1;
8722 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8723 }
8724
8725 /* If PER_CU is not yet queued, add it to the queue.
8726 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8727 dependency.
8728 The result is non-zero if PER_CU was queued, otherwise the result is zero
8729 meaning either PER_CU is already queued or it is already loaded.
8730
8731 N.B. There is an invariant here that if a CU is queued then it is loaded.
8732 The caller is required to load PER_CU if we return non-zero. */
8733
8734 static int
8735 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8736 struct dwarf2_per_cu_data *per_cu,
8737 enum language pretend_language)
8738 {
8739 /* We may arrive here during partial symbol reading, if we need full
8740 DIEs to process an unusual case (e.g. template arguments). Do
8741 not queue PER_CU, just tell our caller to load its DIEs. */
8742 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8743 {
8744 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8745 return 1;
8746 return 0;
8747 }
8748
8749 /* Mark the dependence relation so that we don't flush PER_CU
8750 too early. */
8751 if (dependent_cu != NULL)
8752 dwarf2_add_dependence (dependent_cu, per_cu);
8753
8754 /* If it's already on the queue, we have nothing to do. */
8755 if (per_cu->queued)
8756 return 0;
8757
8758 /* If the compilation unit is already loaded, just mark it as
8759 used. */
8760 if (per_cu->cu != NULL)
8761 {
8762 per_cu->cu->last_used = 0;
8763 return 0;
8764 }
8765
8766 /* Add it to the queue. */
8767 queue_comp_unit (per_cu, pretend_language);
8768
8769 return 1;
8770 }
8771
8772 /* Process the queue. */
8773
8774 static void
8775 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8776 {
8777 if (dwarf_read_debug)
8778 {
8779 fprintf_unfiltered (gdb_stdlog,
8780 "Expanding one or more symtabs of objfile %s ...\n",
8781 objfile_name (dwarf2_per_objfile->objfile));
8782 }
8783
8784 /* The queue starts out with one item, but following a DIE reference
8785 may load a new CU, adding it to the end of the queue. */
8786 while (!dwarf2_per_objfile->queue.empty ())
8787 {
8788 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8789
8790 if ((dwarf2_per_objfile->using_index
8791 ? !item.per_cu->v.quick->compunit_symtab
8792 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8793 /* Skip dummy CUs. */
8794 && item.per_cu->cu != NULL)
8795 {
8796 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8797 unsigned int debug_print_threshold;
8798 char buf[100];
8799
8800 if (per_cu->is_debug_types)
8801 {
8802 struct signatured_type *sig_type =
8803 (struct signatured_type *) per_cu;
8804
8805 sprintf (buf, "TU %s at offset %s",
8806 hex_string (sig_type->signature),
8807 sect_offset_str (per_cu->sect_off));
8808 /* There can be 100s of TUs.
8809 Only print them in verbose mode. */
8810 debug_print_threshold = 2;
8811 }
8812 else
8813 {
8814 sprintf (buf, "CU at offset %s",
8815 sect_offset_str (per_cu->sect_off));
8816 debug_print_threshold = 1;
8817 }
8818
8819 if (dwarf_read_debug >= debug_print_threshold)
8820 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8821
8822 if (per_cu->is_debug_types)
8823 process_full_type_unit (per_cu, item.pretend_language);
8824 else
8825 process_full_comp_unit (per_cu, item.pretend_language);
8826
8827 if (dwarf_read_debug >= debug_print_threshold)
8828 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8829 }
8830
8831 item.per_cu->queued = 0;
8832 dwarf2_per_objfile->queue.pop ();
8833 }
8834
8835 if (dwarf_read_debug)
8836 {
8837 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8838 objfile_name (dwarf2_per_objfile->objfile));
8839 }
8840 }
8841
8842 /* Read in full symbols for PST, and anything it depends on. */
8843
8844 void
8845 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8846 {
8847 struct dwarf2_per_cu_data *per_cu;
8848
8849 if (readin)
8850 return;
8851
8852 read_dependencies (objfile);
8853
8854 per_cu = per_cu_data;
8855
8856 if (per_cu == NULL)
8857 {
8858 /* It's an include file, no symbols to read for it.
8859 Everything is in the parent symtab. */
8860 readin = true;
8861 return;
8862 }
8863
8864 dw2_do_instantiate_symtab (per_cu, false);
8865 }
8866
8867 /* Trivial hash function for die_info: the hash value of a DIE
8868 is its offset in .debug_info for this objfile. */
8869
8870 static hashval_t
8871 die_hash (const void *item)
8872 {
8873 const struct die_info *die = (const struct die_info *) item;
8874
8875 return to_underlying (die->sect_off);
8876 }
8877
8878 /* Trivial comparison function for die_info structures: two DIEs
8879 are equal if they have the same offset. */
8880
8881 static int
8882 die_eq (const void *item_lhs, const void *item_rhs)
8883 {
8884 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8885 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8886
8887 return die_lhs->sect_off == die_rhs->sect_off;
8888 }
8889
8890 /* Load the DIEs associated with PER_CU into memory. */
8891
8892 static void
8893 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8894 bool skip_partial,
8895 enum language pretend_language)
8896 {
8897 gdb_assert (! this_cu->is_debug_types);
8898
8899 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
8900 if (reader.dummy_p)
8901 return;
8902
8903 struct dwarf2_cu *cu = reader.cu;
8904 const gdb_byte *info_ptr = reader.info_ptr;
8905
8906 gdb_assert (cu->die_hash == NULL);
8907 cu->die_hash =
8908 htab_create_alloc_ex (cu->header.length / 12,
8909 die_hash,
8910 die_eq,
8911 NULL,
8912 &cu->comp_unit_obstack,
8913 hashtab_obstack_allocate,
8914 dummy_obstack_deallocate);
8915
8916 if (reader.comp_unit_die->has_children)
8917 reader.comp_unit_die->child
8918 = read_die_and_siblings (&reader, reader.info_ptr,
8919 &info_ptr, reader.comp_unit_die);
8920 cu->dies = reader.comp_unit_die;
8921 /* comp_unit_die is not stored in die_hash, no need. */
8922
8923 /* We try not to read any attributes in this function, because not
8924 all CUs needed for references have been loaded yet, and symbol
8925 table processing isn't initialized. But we have to set the CU language,
8926 or we won't be able to build types correctly.
8927 Similarly, if we do not read the producer, we can not apply
8928 producer-specific interpretation. */
8929 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8930 }
8931
8932 /* Add a DIE to the delayed physname list. */
8933
8934 static void
8935 add_to_method_list (struct type *type, int fnfield_index, int index,
8936 const char *name, struct die_info *die,
8937 struct dwarf2_cu *cu)
8938 {
8939 struct delayed_method_info mi;
8940 mi.type = type;
8941 mi.fnfield_index = fnfield_index;
8942 mi.index = index;
8943 mi.name = name;
8944 mi.die = die;
8945 cu->method_list.push_back (mi);
8946 }
8947
8948 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8949 "const" / "volatile". If so, decrements LEN by the length of the
8950 modifier and return true. Otherwise return false. */
8951
8952 template<size_t N>
8953 static bool
8954 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8955 {
8956 size_t mod_len = sizeof (mod) - 1;
8957 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8958 {
8959 len -= mod_len;
8960 return true;
8961 }
8962 return false;
8963 }
8964
8965 /* Compute the physnames of any methods on the CU's method list.
8966
8967 The computation of method physnames is delayed in order to avoid the
8968 (bad) condition that one of the method's formal parameters is of an as yet
8969 incomplete type. */
8970
8971 static void
8972 compute_delayed_physnames (struct dwarf2_cu *cu)
8973 {
8974 /* Only C++ delays computing physnames. */
8975 if (cu->method_list.empty ())
8976 return;
8977 gdb_assert (cu->language == language_cplus);
8978
8979 for (const delayed_method_info &mi : cu->method_list)
8980 {
8981 const char *physname;
8982 struct fn_fieldlist *fn_flp
8983 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8984 physname = dwarf2_physname (mi.name, mi.die, cu);
8985 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8986 = physname ? physname : "";
8987
8988 /* Since there's no tag to indicate whether a method is a
8989 const/volatile overload, extract that information out of the
8990 demangled name. */
8991 if (physname != NULL)
8992 {
8993 size_t len = strlen (physname);
8994
8995 while (1)
8996 {
8997 if (physname[len] == ')') /* shortcut */
8998 break;
8999 else if (check_modifier (physname, len, " const"))
9000 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9001 else if (check_modifier (physname, len, " volatile"))
9002 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9003 else
9004 break;
9005 }
9006 }
9007 }
9008
9009 /* The list is no longer needed. */
9010 cu->method_list.clear ();
9011 }
9012
9013 /* Go objects should be embedded in a DW_TAG_module DIE,
9014 and it's not clear if/how imported objects will appear.
9015 To keep Go support simple until that's worked out,
9016 go back through what we've read and create something usable.
9017 We could do this while processing each DIE, and feels kinda cleaner,
9018 but that way is more invasive.
9019 This is to, for example, allow the user to type "p var" or "b main"
9020 without having to specify the package name, and allow lookups
9021 of module.object to work in contexts that use the expression
9022 parser. */
9023
9024 static void
9025 fixup_go_packaging (struct dwarf2_cu *cu)
9026 {
9027 gdb::unique_xmalloc_ptr<char> package_name;
9028 struct pending *list;
9029 int i;
9030
9031 for (list = *cu->get_builder ()->get_global_symbols ();
9032 list != NULL;
9033 list = list->next)
9034 {
9035 for (i = 0; i < list->nsyms; ++i)
9036 {
9037 struct symbol *sym = list->symbol[i];
9038
9039 if (sym->language () == language_go
9040 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9041 {
9042 gdb::unique_xmalloc_ptr<char> this_package_name
9043 (go_symbol_package_name (sym));
9044
9045 if (this_package_name == NULL)
9046 continue;
9047 if (package_name == NULL)
9048 package_name = std::move (this_package_name);
9049 else
9050 {
9051 struct objfile *objfile
9052 = cu->per_cu->dwarf2_per_objfile->objfile;
9053 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9054 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9055 (symbol_symtab (sym) != NULL
9056 ? symtab_to_filename_for_display
9057 (symbol_symtab (sym))
9058 : objfile_name (objfile)),
9059 this_package_name.get (), package_name.get ());
9060 }
9061 }
9062 }
9063 }
9064
9065 if (package_name != NULL)
9066 {
9067 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9068 const char *saved_package_name
9069 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9070 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9071 saved_package_name);
9072 struct symbol *sym;
9073
9074 sym = allocate_symbol (objfile);
9075 sym->set_language (language_go, &objfile->objfile_obstack);
9076 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9077 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9078 e.g., "main" finds the "main" module and not C's main(). */
9079 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9080 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9081 SYMBOL_TYPE (sym) = type;
9082
9083 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9084 }
9085 }
9086
9087 /* Allocate a fully-qualified name consisting of the two parts on the
9088 obstack. */
9089
9090 static const char *
9091 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9092 {
9093 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9094 }
9095
9096 /* A helper that allocates a struct discriminant_info to attach to a
9097 union type. */
9098
9099 static struct discriminant_info *
9100 alloc_discriminant_info (struct type *type, int discriminant_index,
9101 int default_index)
9102 {
9103 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9104 gdb_assert (discriminant_index == -1
9105 || (discriminant_index >= 0
9106 && discriminant_index < TYPE_NFIELDS (type)));
9107 gdb_assert (default_index == -1
9108 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9109
9110 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9111
9112 struct discriminant_info *disc
9113 = ((struct discriminant_info *)
9114 TYPE_ZALLOC (type,
9115 offsetof (struct discriminant_info, discriminants)
9116 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9117 disc->default_index = default_index;
9118 disc->discriminant_index = discriminant_index;
9119
9120 struct dynamic_prop prop;
9121 prop.kind = PROP_UNDEFINED;
9122 prop.data.baton = disc;
9123
9124 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9125
9126 return disc;
9127 }
9128
9129 /* Some versions of rustc emitted enums in an unusual way.
9130
9131 Ordinary enums were emitted as unions. The first element of each
9132 structure in the union was named "RUST$ENUM$DISR". This element
9133 held the discriminant.
9134
9135 These versions of Rust also implemented the "non-zero"
9136 optimization. When the enum had two values, and one is empty and
9137 the other holds a pointer that cannot be zero, the pointer is used
9138 as the discriminant, with a zero value meaning the empty variant.
9139 Here, the union's first member is of the form
9140 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9141 where the fieldnos are the indices of the fields that should be
9142 traversed in order to find the field (which may be several fields deep)
9143 and the variantname is the name of the variant of the case when the
9144 field is zero.
9145
9146 This function recognizes whether TYPE is of one of these forms,
9147 and, if so, smashes it to be a variant type. */
9148
9149 static void
9150 quirk_rust_enum (struct type *type, struct objfile *objfile)
9151 {
9152 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9153
9154 /* We don't need to deal with empty enums. */
9155 if (TYPE_NFIELDS (type) == 0)
9156 return;
9157
9158 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9159 if (TYPE_NFIELDS (type) == 1
9160 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9161 {
9162 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9163
9164 /* Decode the field name to find the offset of the
9165 discriminant. */
9166 ULONGEST bit_offset = 0;
9167 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9168 while (name[0] >= '0' && name[0] <= '9')
9169 {
9170 char *tail;
9171 unsigned long index = strtoul (name, &tail, 10);
9172 name = tail;
9173 if (*name != '$'
9174 || index >= TYPE_NFIELDS (field_type)
9175 || (TYPE_FIELD_LOC_KIND (field_type, index)
9176 != FIELD_LOC_KIND_BITPOS))
9177 {
9178 complaint (_("Could not parse Rust enum encoding string \"%s\""
9179 "[in module %s]"),
9180 TYPE_FIELD_NAME (type, 0),
9181 objfile_name (objfile));
9182 return;
9183 }
9184 ++name;
9185
9186 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9187 field_type = TYPE_FIELD_TYPE (field_type, index);
9188 }
9189
9190 /* Make a union to hold the variants. */
9191 struct type *union_type = alloc_type (objfile);
9192 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9193 TYPE_NFIELDS (union_type) = 3;
9194 TYPE_FIELDS (union_type)
9195 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9196 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9197 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9198
9199 /* Put the discriminant must at index 0. */
9200 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9201 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9202 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9203 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9204
9205 /* The order of fields doesn't really matter, so put the real
9206 field at index 1 and the data-less field at index 2. */
9207 struct discriminant_info *disc
9208 = alloc_discriminant_info (union_type, 0, 1);
9209 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9210 TYPE_FIELD_NAME (union_type, 1)
9211 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9212 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9213 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9214 TYPE_FIELD_NAME (union_type, 1));
9215
9216 const char *dataless_name
9217 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9218 name);
9219 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9220 dataless_name);
9221 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9222 /* NAME points into the original discriminant name, which
9223 already has the correct lifetime. */
9224 TYPE_FIELD_NAME (union_type, 2) = name;
9225 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9226 disc->discriminants[2] = 0;
9227
9228 /* Smash this type to be a structure type. We have to do this
9229 because the type has already been recorded. */
9230 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9231 TYPE_NFIELDS (type) = 1;
9232 TYPE_FIELDS (type)
9233 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9234
9235 /* Install the variant part. */
9236 TYPE_FIELD_TYPE (type, 0) = union_type;
9237 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9238 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9239 }
9240 /* A union with a single anonymous field is probably an old-style
9241 univariant enum. */
9242 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9243 {
9244 /* Smash this type to be a structure type. We have to do this
9245 because the type has already been recorded. */
9246 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9247
9248 /* Make a union to hold the variants. */
9249 struct type *union_type = alloc_type (objfile);
9250 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9251 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9252 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9253 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9254 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9255
9256 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9257 const char *variant_name
9258 = rust_last_path_segment (TYPE_NAME (field_type));
9259 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9260 TYPE_NAME (field_type)
9261 = rust_fully_qualify (&objfile->objfile_obstack,
9262 TYPE_NAME (type), variant_name);
9263
9264 /* Install the union in the outer struct type. */
9265 TYPE_NFIELDS (type) = 1;
9266 TYPE_FIELDS (type)
9267 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9268 TYPE_FIELD_TYPE (type, 0) = union_type;
9269 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9270 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9271
9272 alloc_discriminant_info (union_type, -1, 0);
9273 }
9274 else
9275 {
9276 struct type *disr_type = nullptr;
9277 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9278 {
9279 disr_type = TYPE_FIELD_TYPE (type, i);
9280
9281 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9282 {
9283 /* All fields of a true enum will be structs. */
9284 return;
9285 }
9286 else if (TYPE_NFIELDS (disr_type) == 0)
9287 {
9288 /* Could be data-less variant, so keep going. */
9289 disr_type = nullptr;
9290 }
9291 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9292 "RUST$ENUM$DISR") != 0)
9293 {
9294 /* Not a Rust enum. */
9295 return;
9296 }
9297 else
9298 {
9299 /* Found one. */
9300 break;
9301 }
9302 }
9303
9304 /* If we got here without a discriminant, then it's probably
9305 just a union. */
9306 if (disr_type == nullptr)
9307 return;
9308
9309 /* Smash this type to be a structure type. We have to do this
9310 because the type has already been recorded. */
9311 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9312
9313 /* Make a union to hold the variants. */
9314 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9315 struct type *union_type = alloc_type (objfile);
9316 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9317 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9318 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9319 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9320 TYPE_FIELDS (union_type)
9321 = (struct field *) TYPE_ZALLOC (union_type,
9322 (TYPE_NFIELDS (union_type)
9323 * sizeof (struct field)));
9324
9325 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9326 TYPE_NFIELDS (type) * sizeof (struct field));
9327
9328 /* Install the discriminant at index 0 in the union. */
9329 TYPE_FIELD (union_type, 0) = *disr_field;
9330 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9331 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9332
9333 /* Install the union in the outer struct type. */
9334 TYPE_FIELD_TYPE (type, 0) = union_type;
9335 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9336 TYPE_NFIELDS (type) = 1;
9337
9338 /* Set the size and offset of the union type. */
9339 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9340
9341 /* We need a way to find the correct discriminant given a
9342 variant name. For convenience we build a map here. */
9343 struct type *enum_type = FIELD_TYPE (*disr_field);
9344 std::unordered_map<std::string, ULONGEST> discriminant_map;
9345 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9346 {
9347 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9348 {
9349 const char *name
9350 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9351 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9352 }
9353 }
9354
9355 int n_fields = TYPE_NFIELDS (union_type);
9356 struct discriminant_info *disc
9357 = alloc_discriminant_info (union_type, 0, -1);
9358 /* Skip the discriminant here. */
9359 for (int i = 1; i < n_fields; ++i)
9360 {
9361 /* Find the final word in the name of this variant's type.
9362 That name can be used to look up the correct
9363 discriminant. */
9364 const char *variant_name
9365 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9366 i)));
9367
9368 auto iter = discriminant_map.find (variant_name);
9369 if (iter != discriminant_map.end ())
9370 disc->discriminants[i] = iter->second;
9371
9372 /* Remove the discriminant field, if it exists. */
9373 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9374 if (TYPE_NFIELDS (sub_type) > 0)
9375 {
9376 --TYPE_NFIELDS (sub_type);
9377 ++TYPE_FIELDS (sub_type);
9378 }
9379 TYPE_FIELD_NAME (union_type, i) = variant_name;
9380 TYPE_NAME (sub_type)
9381 = rust_fully_qualify (&objfile->objfile_obstack,
9382 TYPE_NAME (type), variant_name);
9383 }
9384 }
9385 }
9386
9387 /* Rewrite some Rust unions to be structures with variants parts. */
9388
9389 static void
9390 rust_union_quirks (struct dwarf2_cu *cu)
9391 {
9392 gdb_assert (cu->language == language_rust);
9393 for (type *type_ : cu->rust_unions)
9394 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9395 /* We don't need this any more. */
9396 cu->rust_unions.clear ();
9397 }
9398
9399 /* Return the symtab for PER_CU. This works properly regardless of
9400 whether we're using the index or psymtabs. */
9401
9402 static struct compunit_symtab *
9403 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9404 {
9405 return (per_cu->dwarf2_per_objfile->using_index
9406 ? per_cu->v.quick->compunit_symtab
9407 : per_cu->v.psymtab->compunit_symtab);
9408 }
9409
9410 /* A helper function for computing the list of all symbol tables
9411 included by PER_CU. */
9412
9413 static void
9414 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9415 htab_t all_children, htab_t all_type_symtabs,
9416 struct dwarf2_per_cu_data *per_cu,
9417 struct compunit_symtab *immediate_parent)
9418 {
9419 void **slot;
9420 struct compunit_symtab *cust;
9421
9422 slot = htab_find_slot (all_children, per_cu, INSERT);
9423 if (*slot != NULL)
9424 {
9425 /* This inclusion and its children have been processed. */
9426 return;
9427 }
9428
9429 *slot = per_cu;
9430 /* Only add a CU if it has a symbol table. */
9431 cust = get_compunit_symtab (per_cu);
9432 if (cust != NULL)
9433 {
9434 /* If this is a type unit only add its symbol table if we haven't
9435 seen it yet (type unit per_cu's can share symtabs). */
9436 if (per_cu->is_debug_types)
9437 {
9438 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9439 if (*slot == NULL)
9440 {
9441 *slot = cust;
9442 result->push_back (cust);
9443 if (cust->user == NULL)
9444 cust->user = immediate_parent;
9445 }
9446 }
9447 else
9448 {
9449 result->push_back (cust);
9450 if (cust->user == NULL)
9451 cust->user = immediate_parent;
9452 }
9453 }
9454
9455 if (!per_cu->imported_symtabs_empty ())
9456 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9457 {
9458 recursively_compute_inclusions (result, all_children,
9459 all_type_symtabs, ptr, cust);
9460 }
9461 }
9462
9463 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9464 PER_CU. */
9465
9466 static void
9467 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9468 {
9469 gdb_assert (! per_cu->is_debug_types);
9470
9471 if (!per_cu->imported_symtabs_empty ())
9472 {
9473 int len;
9474 std::vector<compunit_symtab *> result_symtabs;
9475 htab_t all_children, all_type_symtabs;
9476 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9477
9478 /* If we don't have a symtab, we can just skip this case. */
9479 if (cust == NULL)
9480 return;
9481
9482 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9483 NULL, xcalloc, xfree);
9484 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9485 NULL, xcalloc, xfree);
9486
9487 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9488 {
9489 recursively_compute_inclusions (&result_symtabs, all_children,
9490 all_type_symtabs, ptr, cust);
9491 }
9492
9493 /* Now we have a transitive closure of all the included symtabs. */
9494 len = result_symtabs.size ();
9495 cust->includes
9496 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9497 struct compunit_symtab *, len + 1);
9498 memcpy (cust->includes, result_symtabs.data (),
9499 len * sizeof (compunit_symtab *));
9500 cust->includes[len] = NULL;
9501
9502 htab_delete (all_children);
9503 htab_delete (all_type_symtabs);
9504 }
9505 }
9506
9507 /* Compute the 'includes' field for the symtabs of all the CUs we just
9508 read. */
9509
9510 static void
9511 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9512 {
9513 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9514 {
9515 if (! iter->is_debug_types)
9516 compute_compunit_symtab_includes (iter);
9517 }
9518
9519 dwarf2_per_objfile->just_read_cus.clear ();
9520 }
9521
9522 /* Generate full symbol information for PER_CU, whose DIEs have
9523 already been loaded into memory. */
9524
9525 static void
9526 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9527 enum language pretend_language)
9528 {
9529 struct dwarf2_cu *cu = per_cu->cu;
9530 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9531 struct objfile *objfile = dwarf2_per_objfile->objfile;
9532 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9533 CORE_ADDR lowpc, highpc;
9534 struct compunit_symtab *cust;
9535 CORE_ADDR baseaddr;
9536 struct block *static_block;
9537 CORE_ADDR addr;
9538
9539 baseaddr = objfile->text_section_offset ();
9540
9541 /* Clear the list here in case something was left over. */
9542 cu->method_list.clear ();
9543
9544 cu->language = pretend_language;
9545 cu->language_defn = language_def (cu->language);
9546
9547 /* Do line number decoding in read_file_scope () */
9548 process_die (cu->dies, cu);
9549
9550 /* For now fudge the Go package. */
9551 if (cu->language == language_go)
9552 fixup_go_packaging (cu);
9553
9554 /* Now that we have processed all the DIEs in the CU, all the types
9555 should be complete, and it should now be safe to compute all of the
9556 physnames. */
9557 compute_delayed_physnames (cu);
9558
9559 if (cu->language == language_rust)
9560 rust_union_quirks (cu);
9561
9562 /* Some compilers don't define a DW_AT_high_pc attribute for the
9563 compilation unit. If the DW_AT_high_pc is missing, synthesize
9564 it, by scanning the DIE's below the compilation unit. */
9565 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9566
9567 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9568 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9569
9570 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9571 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9572 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9573 addrmap to help ensure it has an accurate map of pc values belonging to
9574 this comp unit. */
9575 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9576
9577 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9578 SECT_OFF_TEXT (objfile),
9579 0);
9580
9581 if (cust != NULL)
9582 {
9583 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9584
9585 /* Set symtab language to language from DW_AT_language. If the
9586 compilation is from a C file generated by language preprocessors, do
9587 not set the language if it was already deduced by start_subfile. */
9588 if (!(cu->language == language_c
9589 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9590 COMPUNIT_FILETABS (cust)->language = cu->language;
9591
9592 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9593 produce DW_AT_location with location lists but it can be possibly
9594 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9595 there were bugs in prologue debug info, fixed later in GCC-4.5
9596 by "unwind info for epilogues" patch (which is not directly related).
9597
9598 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9599 needed, it would be wrong due to missing DW_AT_producer there.
9600
9601 Still one can confuse GDB by using non-standard GCC compilation
9602 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9603 */
9604 if (cu->has_loclist && gcc_4_minor >= 5)
9605 cust->locations_valid = 1;
9606
9607 if (gcc_4_minor >= 5)
9608 cust->epilogue_unwind_valid = 1;
9609
9610 cust->call_site_htab = cu->call_site_htab;
9611 }
9612
9613 if (dwarf2_per_objfile->using_index)
9614 per_cu->v.quick->compunit_symtab = cust;
9615 else
9616 {
9617 dwarf2_psymtab *pst = per_cu->v.psymtab;
9618 pst->compunit_symtab = cust;
9619 pst->readin = true;
9620 }
9621
9622 /* Push it for inclusion processing later. */
9623 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9624
9625 /* Not needed any more. */
9626 cu->reset_builder ();
9627 }
9628
9629 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9630 already been loaded into memory. */
9631
9632 static void
9633 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9634 enum language pretend_language)
9635 {
9636 struct dwarf2_cu *cu = per_cu->cu;
9637 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9638 struct objfile *objfile = dwarf2_per_objfile->objfile;
9639 struct compunit_symtab *cust;
9640 struct signatured_type *sig_type;
9641
9642 gdb_assert (per_cu->is_debug_types);
9643 sig_type = (struct signatured_type *) per_cu;
9644
9645 /* Clear the list here in case something was left over. */
9646 cu->method_list.clear ();
9647
9648 cu->language = pretend_language;
9649 cu->language_defn = language_def (cu->language);
9650
9651 /* The symbol tables are set up in read_type_unit_scope. */
9652 process_die (cu->dies, cu);
9653
9654 /* For now fudge the Go package. */
9655 if (cu->language == language_go)
9656 fixup_go_packaging (cu);
9657
9658 /* Now that we have processed all the DIEs in the CU, all the types
9659 should be complete, and it should now be safe to compute all of the
9660 physnames. */
9661 compute_delayed_physnames (cu);
9662
9663 if (cu->language == language_rust)
9664 rust_union_quirks (cu);
9665
9666 /* TUs share symbol tables.
9667 If this is the first TU to use this symtab, complete the construction
9668 of it with end_expandable_symtab. Otherwise, complete the addition of
9669 this TU's symbols to the existing symtab. */
9670 if (sig_type->type_unit_group->compunit_symtab == NULL)
9671 {
9672 buildsym_compunit *builder = cu->get_builder ();
9673 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9674 sig_type->type_unit_group->compunit_symtab = cust;
9675
9676 if (cust != NULL)
9677 {
9678 /* Set symtab language to language from DW_AT_language. If the
9679 compilation is from a C file generated by language preprocessors,
9680 do not set the language if it was already deduced by
9681 start_subfile. */
9682 if (!(cu->language == language_c
9683 && COMPUNIT_FILETABS (cust)->language != language_c))
9684 COMPUNIT_FILETABS (cust)->language = cu->language;
9685 }
9686 }
9687 else
9688 {
9689 cu->get_builder ()->augment_type_symtab ();
9690 cust = sig_type->type_unit_group->compunit_symtab;
9691 }
9692
9693 if (dwarf2_per_objfile->using_index)
9694 per_cu->v.quick->compunit_symtab = cust;
9695 else
9696 {
9697 dwarf2_psymtab *pst = per_cu->v.psymtab;
9698 pst->compunit_symtab = cust;
9699 pst->readin = true;
9700 }
9701
9702 /* Not needed any more. */
9703 cu->reset_builder ();
9704 }
9705
9706 /* Process an imported unit DIE. */
9707
9708 static void
9709 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9710 {
9711 struct attribute *attr;
9712
9713 /* For now we don't handle imported units in type units. */
9714 if (cu->per_cu->is_debug_types)
9715 {
9716 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9717 " supported in type units [in module %s]"),
9718 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9719 }
9720
9721 attr = dwarf2_attr (die, DW_AT_import, cu);
9722 if (attr != NULL)
9723 {
9724 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9725 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9726 dwarf2_per_cu_data *per_cu
9727 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9728 cu->per_cu->dwarf2_per_objfile);
9729
9730 /* If necessary, add it to the queue and load its DIEs. */
9731 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9732 load_full_comp_unit (per_cu, false, cu->language);
9733
9734 cu->per_cu->imported_symtabs_push (per_cu);
9735 }
9736 }
9737
9738 /* RAII object that represents a process_die scope: i.e.,
9739 starts/finishes processing a DIE. */
9740 class process_die_scope
9741 {
9742 public:
9743 process_die_scope (die_info *die, dwarf2_cu *cu)
9744 : m_die (die), m_cu (cu)
9745 {
9746 /* We should only be processing DIEs not already in process. */
9747 gdb_assert (!m_die->in_process);
9748 m_die->in_process = true;
9749 }
9750
9751 ~process_die_scope ()
9752 {
9753 m_die->in_process = false;
9754
9755 /* If we're done processing the DIE for the CU that owns the line
9756 header, we don't need the line header anymore. */
9757 if (m_cu->line_header_die_owner == m_die)
9758 {
9759 delete m_cu->line_header;
9760 m_cu->line_header = NULL;
9761 m_cu->line_header_die_owner = NULL;
9762 }
9763 }
9764
9765 private:
9766 die_info *m_die;
9767 dwarf2_cu *m_cu;
9768 };
9769
9770 /* Process a die and its children. */
9771
9772 static void
9773 process_die (struct die_info *die, struct dwarf2_cu *cu)
9774 {
9775 process_die_scope scope (die, cu);
9776
9777 switch (die->tag)
9778 {
9779 case DW_TAG_padding:
9780 break;
9781 case DW_TAG_compile_unit:
9782 case DW_TAG_partial_unit:
9783 read_file_scope (die, cu);
9784 break;
9785 case DW_TAG_type_unit:
9786 read_type_unit_scope (die, cu);
9787 break;
9788 case DW_TAG_subprogram:
9789 /* Nested subprograms in Fortran get a prefix. */
9790 if (cu->language == language_fortran
9791 && die->parent != NULL
9792 && die->parent->tag == DW_TAG_subprogram)
9793 cu->processing_has_namespace_info = true;
9794 /* Fall through. */
9795 case DW_TAG_inlined_subroutine:
9796 read_func_scope (die, cu);
9797 break;
9798 case DW_TAG_lexical_block:
9799 case DW_TAG_try_block:
9800 case DW_TAG_catch_block:
9801 read_lexical_block_scope (die, cu);
9802 break;
9803 case DW_TAG_call_site:
9804 case DW_TAG_GNU_call_site:
9805 read_call_site_scope (die, cu);
9806 break;
9807 case DW_TAG_class_type:
9808 case DW_TAG_interface_type:
9809 case DW_TAG_structure_type:
9810 case DW_TAG_union_type:
9811 process_structure_scope (die, cu);
9812 break;
9813 case DW_TAG_enumeration_type:
9814 process_enumeration_scope (die, cu);
9815 break;
9816
9817 /* These dies have a type, but processing them does not create
9818 a symbol or recurse to process the children. Therefore we can
9819 read them on-demand through read_type_die. */
9820 case DW_TAG_subroutine_type:
9821 case DW_TAG_set_type:
9822 case DW_TAG_array_type:
9823 case DW_TAG_pointer_type:
9824 case DW_TAG_ptr_to_member_type:
9825 case DW_TAG_reference_type:
9826 case DW_TAG_rvalue_reference_type:
9827 case DW_TAG_string_type:
9828 break;
9829
9830 case DW_TAG_base_type:
9831 case DW_TAG_subrange_type:
9832 case DW_TAG_typedef:
9833 /* Add a typedef symbol for the type definition, if it has a
9834 DW_AT_name. */
9835 new_symbol (die, read_type_die (die, cu), cu);
9836 break;
9837 case DW_TAG_common_block:
9838 read_common_block (die, cu);
9839 break;
9840 case DW_TAG_common_inclusion:
9841 break;
9842 case DW_TAG_namespace:
9843 cu->processing_has_namespace_info = true;
9844 read_namespace (die, cu);
9845 break;
9846 case DW_TAG_module:
9847 cu->processing_has_namespace_info = true;
9848 read_module (die, cu);
9849 break;
9850 case DW_TAG_imported_declaration:
9851 cu->processing_has_namespace_info = true;
9852 if (read_namespace_alias (die, cu))
9853 break;
9854 /* The declaration is not a global namespace alias. */
9855 /* Fall through. */
9856 case DW_TAG_imported_module:
9857 cu->processing_has_namespace_info = true;
9858 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9859 || cu->language != language_fortran))
9860 complaint (_("Tag '%s' has unexpected children"),
9861 dwarf_tag_name (die->tag));
9862 read_import_statement (die, cu);
9863 break;
9864
9865 case DW_TAG_imported_unit:
9866 process_imported_unit_die (die, cu);
9867 break;
9868
9869 case DW_TAG_variable:
9870 read_variable (die, cu);
9871 break;
9872
9873 default:
9874 new_symbol (die, NULL, cu);
9875 break;
9876 }
9877 }
9878 \f
9879 /* DWARF name computation. */
9880
9881 /* A helper function for dwarf2_compute_name which determines whether DIE
9882 needs to have the name of the scope prepended to the name listed in the
9883 die. */
9884
9885 static int
9886 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9887 {
9888 struct attribute *attr;
9889
9890 switch (die->tag)
9891 {
9892 case DW_TAG_namespace:
9893 case DW_TAG_typedef:
9894 case DW_TAG_class_type:
9895 case DW_TAG_interface_type:
9896 case DW_TAG_structure_type:
9897 case DW_TAG_union_type:
9898 case DW_TAG_enumeration_type:
9899 case DW_TAG_enumerator:
9900 case DW_TAG_subprogram:
9901 case DW_TAG_inlined_subroutine:
9902 case DW_TAG_member:
9903 case DW_TAG_imported_declaration:
9904 return 1;
9905
9906 case DW_TAG_variable:
9907 case DW_TAG_constant:
9908 /* We only need to prefix "globally" visible variables. These include
9909 any variable marked with DW_AT_external or any variable that
9910 lives in a namespace. [Variables in anonymous namespaces
9911 require prefixing, but they are not DW_AT_external.] */
9912
9913 if (dwarf2_attr (die, DW_AT_specification, cu))
9914 {
9915 struct dwarf2_cu *spec_cu = cu;
9916
9917 return die_needs_namespace (die_specification (die, &spec_cu),
9918 spec_cu);
9919 }
9920
9921 attr = dwarf2_attr (die, DW_AT_external, cu);
9922 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9923 && die->parent->tag != DW_TAG_module)
9924 return 0;
9925 /* A variable in a lexical block of some kind does not need a
9926 namespace, even though in C++ such variables may be external
9927 and have a mangled name. */
9928 if (die->parent->tag == DW_TAG_lexical_block
9929 || die->parent->tag == DW_TAG_try_block
9930 || die->parent->tag == DW_TAG_catch_block
9931 || die->parent->tag == DW_TAG_subprogram)
9932 return 0;
9933 return 1;
9934
9935 default:
9936 return 0;
9937 }
9938 }
9939
9940 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9941 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9942 defined for the given DIE. */
9943
9944 static struct attribute *
9945 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9946 {
9947 struct attribute *attr;
9948
9949 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9950 if (attr == NULL)
9951 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9952
9953 return attr;
9954 }
9955
9956 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9957 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9958 defined for the given DIE. */
9959
9960 static const char *
9961 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9962 {
9963 const char *linkage_name;
9964
9965 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9966 if (linkage_name == NULL)
9967 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9968
9969 return linkage_name;
9970 }
9971
9972 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9973 compute the physname for the object, which include a method's:
9974 - formal parameters (C++),
9975 - receiver type (Go),
9976
9977 The term "physname" is a bit confusing.
9978 For C++, for example, it is the demangled name.
9979 For Go, for example, it's the mangled name.
9980
9981 For Ada, return the DIE's linkage name rather than the fully qualified
9982 name. PHYSNAME is ignored..
9983
9984 The result is allocated on the objfile_obstack and canonicalized. */
9985
9986 static const char *
9987 dwarf2_compute_name (const char *name,
9988 struct die_info *die, struct dwarf2_cu *cu,
9989 int physname)
9990 {
9991 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9992
9993 if (name == NULL)
9994 name = dwarf2_name (die, cu);
9995
9996 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9997 but otherwise compute it by typename_concat inside GDB.
9998 FIXME: Actually this is not really true, or at least not always true.
9999 It's all very confusing. compute_and_set_names doesn't try to demangle
10000 Fortran names because there is no mangling standard. So new_symbol
10001 will set the demangled name to the result of dwarf2_full_name, and it is
10002 the demangled name that GDB uses if it exists. */
10003 if (cu->language == language_ada
10004 || (cu->language == language_fortran && physname))
10005 {
10006 /* For Ada unit, we prefer the linkage name over the name, as
10007 the former contains the exported name, which the user expects
10008 to be able to reference. Ideally, we want the user to be able
10009 to reference this entity using either natural or linkage name,
10010 but we haven't started looking at this enhancement yet. */
10011 const char *linkage_name = dw2_linkage_name (die, cu);
10012
10013 if (linkage_name != NULL)
10014 return linkage_name;
10015 }
10016
10017 /* These are the only languages we know how to qualify names in. */
10018 if (name != NULL
10019 && (cu->language == language_cplus
10020 || cu->language == language_fortran || cu->language == language_d
10021 || cu->language == language_rust))
10022 {
10023 if (die_needs_namespace (die, cu))
10024 {
10025 const char *prefix;
10026 const char *canonical_name = NULL;
10027
10028 string_file buf;
10029
10030 prefix = determine_prefix (die, cu);
10031 if (*prefix != '\0')
10032 {
10033 gdb::unique_xmalloc_ptr<char> prefixed_name
10034 (typename_concat (NULL, prefix, name, physname, cu));
10035
10036 buf.puts (prefixed_name.get ());
10037 }
10038 else
10039 buf.puts (name);
10040
10041 /* Template parameters may be specified in the DIE's DW_AT_name, or
10042 as children with DW_TAG_template_type_param or
10043 DW_TAG_value_type_param. If the latter, add them to the name
10044 here. If the name already has template parameters, then
10045 skip this step; some versions of GCC emit both, and
10046 it is more efficient to use the pre-computed name.
10047
10048 Something to keep in mind about this process: it is very
10049 unlikely, or in some cases downright impossible, to produce
10050 something that will match the mangled name of a function.
10051 If the definition of the function has the same debug info,
10052 we should be able to match up with it anyway. But fallbacks
10053 using the minimal symbol, for instance to find a method
10054 implemented in a stripped copy of libstdc++, will not work.
10055 If we do not have debug info for the definition, we will have to
10056 match them up some other way.
10057
10058 When we do name matching there is a related problem with function
10059 templates; two instantiated function templates are allowed to
10060 differ only by their return types, which we do not add here. */
10061
10062 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10063 {
10064 struct attribute *attr;
10065 struct die_info *child;
10066 int first = 1;
10067
10068 die->building_fullname = 1;
10069
10070 for (child = die->child; child != NULL; child = child->sibling)
10071 {
10072 struct type *type;
10073 LONGEST value;
10074 const gdb_byte *bytes;
10075 struct dwarf2_locexpr_baton *baton;
10076 struct value *v;
10077
10078 if (child->tag != DW_TAG_template_type_param
10079 && child->tag != DW_TAG_template_value_param)
10080 continue;
10081
10082 if (first)
10083 {
10084 buf.puts ("<");
10085 first = 0;
10086 }
10087 else
10088 buf.puts (", ");
10089
10090 attr = dwarf2_attr (child, DW_AT_type, cu);
10091 if (attr == NULL)
10092 {
10093 complaint (_("template parameter missing DW_AT_type"));
10094 buf.puts ("UNKNOWN_TYPE");
10095 continue;
10096 }
10097 type = die_type (child, cu);
10098
10099 if (child->tag == DW_TAG_template_type_param)
10100 {
10101 c_print_type (type, "", &buf, -1, 0, cu->language,
10102 &type_print_raw_options);
10103 continue;
10104 }
10105
10106 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10107 if (attr == NULL)
10108 {
10109 complaint (_("template parameter missing "
10110 "DW_AT_const_value"));
10111 buf.puts ("UNKNOWN_VALUE");
10112 continue;
10113 }
10114
10115 dwarf2_const_value_attr (attr, type, name,
10116 &cu->comp_unit_obstack, cu,
10117 &value, &bytes, &baton);
10118
10119 if (TYPE_NOSIGN (type))
10120 /* GDB prints characters as NUMBER 'CHAR'. If that's
10121 changed, this can use value_print instead. */
10122 c_printchar (value, type, &buf);
10123 else
10124 {
10125 struct value_print_options opts;
10126
10127 if (baton != NULL)
10128 v = dwarf2_evaluate_loc_desc (type, NULL,
10129 baton->data,
10130 baton->size,
10131 baton->per_cu);
10132 else if (bytes != NULL)
10133 {
10134 v = allocate_value (type);
10135 memcpy (value_contents_writeable (v), bytes,
10136 TYPE_LENGTH (type));
10137 }
10138 else
10139 v = value_from_longest (type, value);
10140
10141 /* Specify decimal so that we do not depend on
10142 the radix. */
10143 get_formatted_print_options (&opts, 'd');
10144 opts.raw = 1;
10145 value_print (v, &buf, &opts);
10146 release_value (v);
10147 }
10148 }
10149
10150 die->building_fullname = 0;
10151
10152 if (!first)
10153 {
10154 /* Close the argument list, with a space if necessary
10155 (nested templates). */
10156 if (!buf.empty () && buf.string ().back () == '>')
10157 buf.puts (" >");
10158 else
10159 buf.puts (">");
10160 }
10161 }
10162
10163 /* For C++ methods, append formal parameter type
10164 information, if PHYSNAME. */
10165
10166 if (physname && die->tag == DW_TAG_subprogram
10167 && cu->language == language_cplus)
10168 {
10169 struct type *type = read_type_die (die, cu);
10170
10171 c_type_print_args (type, &buf, 1, cu->language,
10172 &type_print_raw_options);
10173
10174 if (cu->language == language_cplus)
10175 {
10176 /* Assume that an artificial first parameter is
10177 "this", but do not crash if it is not. RealView
10178 marks unnamed (and thus unused) parameters as
10179 artificial; there is no way to differentiate
10180 the two cases. */
10181 if (TYPE_NFIELDS (type) > 0
10182 && TYPE_FIELD_ARTIFICIAL (type, 0)
10183 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10184 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10185 0))))
10186 buf.puts (" const");
10187 }
10188 }
10189
10190 const std::string &intermediate_name = buf.string ();
10191
10192 if (cu->language == language_cplus)
10193 canonical_name
10194 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10195 &objfile->per_bfd->storage_obstack);
10196
10197 /* If we only computed INTERMEDIATE_NAME, or if
10198 INTERMEDIATE_NAME is already canonical, then we need to
10199 copy it to the appropriate obstack. */
10200 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10201 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10202 intermediate_name);
10203 else
10204 name = canonical_name;
10205 }
10206 }
10207
10208 return name;
10209 }
10210
10211 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10212 If scope qualifiers are appropriate they will be added. The result
10213 will be allocated on the storage_obstack, or NULL if the DIE does
10214 not have a name. NAME may either be from a previous call to
10215 dwarf2_name or NULL.
10216
10217 The output string will be canonicalized (if C++). */
10218
10219 static const char *
10220 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10221 {
10222 return dwarf2_compute_name (name, die, cu, 0);
10223 }
10224
10225 /* Construct a physname for the given DIE in CU. NAME may either be
10226 from a previous call to dwarf2_name or NULL. The result will be
10227 allocated on the objfile_objstack or NULL if the DIE does not have a
10228 name.
10229
10230 The output string will be canonicalized (if C++). */
10231
10232 static const char *
10233 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10234 {
10235 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10236 const char *retval, *mangled = NULL, *canon = NULL;
10237 int need_copy = 1;
10238
10239 /* In this case dwarf2_compute_name is just a shortcut not building anything
10240 on its own. */
10241 if (!die_needs_namespace (die, cu))
10242 return dwarf2_compute_name (name, die, cu, 1);
10243
10244 mangled = dw2_linkage_name (die, cu);
10245
10246 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10247 See https://github.com/rust-lang/rust/issues/32925. */
10248 if (cu->language == language_rust && mangled != NULL
10249 && strchr (mangled, '{') != NULL)
10250 mangled = NULL;
10251
10252 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10253 has computed. */
10254 gdb::unique_xmalloc_ptr<char> demangled;
10255 if (mangled != NULL)
10256 {
10257
10258 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10259 {
10260 /* Do nothing (do not demangle the symbol name). */
10261 }
10262 else if (cu->language == language_go)
10263 {
10264 /* This is a lie, but we already lie to the caller new_symbol.
10265 new_symbol assumes we return the mangled name.
10266 This just undoes that lie until things are cleaned up. */
10267 }
10268 else
10269 {
10270 /* Use DMGL_RET_DROP for C++ template functions to suppress
10271 their return type. It is easier for GDB users to search
10272 for such functions as `name(params)' than `long name(params)'.
10273 In such case the minimal symbol names do not match the full
10274 symbol names but for template functions there is never a need
10275 to look up their definition from their declaration so
10276 the only disadvantage remains the minimal symbol variant
10277 `long name(params)' does not have the proper inferior type. */
10278 demangled.reset (gdb_demangle (mangled,
10279 (DMGL_PARAMS | DMGL_ANSI
10280 | DMGL_RET_DROP)));
10281 }
10282 if (demangled)
10283 canon = demangled.get ();
10284 else
10285 {
10286 canon = mangled;
10287 need_copy = 0;
10288 }
10289 }
10290
10291 if (canon == NULL || check_physname)
10292 {
10293 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10294
10295 if (canon != NULL && strcmp (physname, canon) != 0)
10296 {
10297 /* It may not mean a bug in GDB. The compiler could also
10298 compute DW_AT_linkage_name incorrectly. But in such case
10299 GDB would need to be bug-to-bug compatible. */
10300
10301 complaint (_("Computed physname <%s> does not match demangled <%s> "
10302 "(from linkage <%s>) - DIE at %s [in module %s]"),
10303 physname, canon, mangled, sect_offset_str (die->sect_off),
10304 objfile_name (objfile));
10305
10306 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10307 is available here - over computed PHYSNAME. It is safer
10308 against both buggy GDB and buggy compilers. */
10309
10310 retval = canon;
10311 }
10312 else
10313 {
10314 retval = physname;
10315 need_copy = 0;
10316 }
10317 }
10318 else
10319 retval = canon;
10320
10321 if (need_copy)
10322 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
10323
10324 return retval;
10325 }
10326
10327 /* Inspect DIE in CU for a namespace alias. If one exists, record
10328 a new symbol for it.
10329
10330 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10331
10332 static int
10333 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10334 {
10335 struct attribute *attr;
10336
10337 /* If the die does not have a name, this is not a namespace
10338 alias. */
10339 attr = dwarf2_attr (die, DW_AT_name, cu);
10340 if (attr != NULL)
10341 {
10342 int num;
10343 struct die_info *d = die;
10344 struct dwarf2_cu *imported_cu = cu;
10345
10346 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10347 keep inspecting DIEs until we hit the underlying import. */
10348 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10349 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10350 {
10351 attr = dwarf2_attr (d, DW_AT_import, cu);
10352 if (attr == NULL)
10353 break;
10354
10355 d = follow_die_ref (d, attr, &imported_cu);
10356 if (d->tag != DW_TAG_imported_declaration)
10357 break;
10358 }
10359
10360 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10361 {
10362 complaint (_("DIE at %s has too many recursively imported "
10363 "declarations"), sect_offset_str (d->sect_off));
10364 return 0;
10365 }
10366
10367 if (attr != NULL)
10368 {
10369 struct type *type;
10370 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10371
10372 type = get_die_type_at_offset (sect_off, cu->per_cu);
10373 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10374 {
10375 /* This declaration is a global namespace alias. Add
10376 a symbol for it whose type is the aliased namespace. */
10377 new_symbol (die, type, cu);
10378 return 1;
10379 }
10380 }
10381 }
10382
10383 return 0;
10384 }
10385
10386 /* Return the using directives repository (global or local?) to use in the
10387 current context for CU.
10388
10389 For Ada, imported declarations can materialize renamings, which *may* be
10390 global. However it is impossible (for now?) in DWARF to distinguish
10391 "external" imported declarations and "static" ones. As all imported
10392 declarations seem to be static in all other languages, make them all CU-wide
10393 global only in Ada. */
10394
10395 static struct using_direct **
10396 using_directives (struct dwarf2_cu *cu)
10397 {
10398 if (cu->language == language_ada
10399 && cu->get_builder ()->outermost_context_p ())
10400 return cu->get_builder ()->get_global_using_directives ();
10401 else
10402 return cu->get_builder ()->get_local_using_directives ();
10403 }
10404
10405 /* Read the import statement specified by the given die and record it. */
10406
10407 static void
10408 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10409 {
10410 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10411 struct attribute *import_attr;
10412 struct die_info *imported_die, *child_die;
10413 struct dwarf2_cu *imported_cu;
10414 const char *imported_name;
10415 const char *imported_name_prefix;
10416 const char *canonical_name;
10417 const char *import_alias;
10418 const char *imported_declaration = NULL;
10419 const char *import_prefix;
10420 std::vector<const char *> excludes;
10421
10422 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10423 if (import_attr == NULL)
10424 {
10425 complaint (_("Tag '%s' has no DW_AT_import"),
10426 dwarf_tag_name (die->tag));
10427 return;
10428 }
10429
10430 imported_cu = cu;
10431 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10432 imported_name = dwarf2_name (imported_die, imported_cu);
10433 if (imported_name == NULL)
10434 {
10435 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10436
10437 The import in the following code:
10438 namespace A
10439 {
10440 typedef int B;
10441 }
10442
10443 int main ()
10444 {
10445 using A::B;
10446 B b;
10447 return b;
10448 }
10449
10450 ...
10451 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10452 <52> DW_AT_decl_file : 1
10453 <53> DW_AT_decl_line : 6
10454 <54> DW_AT_import : <0x75>
10455 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10456 <59> DW_AT_name : B
10457 <5b> DW_AT_decl_file : 1
10458 <5c> DW_AT_decl_line : 2
10459 <5d> DW_AT_type : <0x6e>
10460 ...
10461 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10462 <76> DW_AT_byte_size : 4
10463 <77> DW_AT_encoding : 5 (signed)
10464
10465 imports the wrong die ( 0x75 instead of 0x58 ).
10466 This case will be ignored until the gcc bug is fixed. */
10467 return;
10468 }
10469
10470 /* Figure out the local name after import. */
10471 import_alias = dwarf2_name (die, cu);
10472
10473 /* Figure out where the statement is being imported to. */
10474 import_prefix = determine_prefix (die, cu);
10475
10476 /* Figure out what the scope of the imported die is and prepend it
10477 to the name of the imported die. */
10478 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10479
10480 if (imported_die->tag != DW_TAG_namespace
10481 && imported_die->tag != DW_TAG_module)
10482 {
10483 imported_declaration = imported_name;
10484 canonical_name = imported_name_prefix;
10485 }
10486 else if (strlen (imported_name_prefix) > 0)
10487 canonical_name = obconcat (&objfile->objfile_obstack,
10488 imported_name_prefix,
10489 (cu->language == language_d ? "." : "::"),
10490 imported_name, (char *) NULL);
10491 else
10492 canonical_name = imported_name;
10493
10494 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10495 for (child_die = die->child; child_die && child_die->tag;
10496 child_die = sibling_die (child_die))
10497 {
10498 /* DWARF-4: A Fortran use statement with a “rename list” may be
10499 represented by an imported module entry with an import attribute
10500 referring to the module and owned entries corresponding to those
10501 entities that are renamed as part of being imported. */
10502
10503 if (child_die->tag != DW_TAG_imported_declaration)
10504 {
10505 complaint (_("child DW_TAG_imported_declaration expected "
10506 "- DIE at %s [in module %s]"),
10507 sect_offset_str (child_die->sect_off),
10508 objfile_name (objfile));
10509 continue;
10510 }
10511
10512 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10513 if (import_attr == NULL)
10514 {
10515 complaint (_("Tag '%s' has no DW_AT_import"),
10516 dwarf_tag_name (child_die->tag));
10517 continue;
10518 }
10519
10520 imported_cu = cu;
10521 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10522 &imported_cu);
10523 imported_name = dwarf2_name (imported_die, imported_cu);
10524 if (imported_name == NULL)
10525 {
10526 complaint (_("child DW_TAG_imported_declaration has unknown "
10527 "imported name - DIE at %s [in module %s]"),
10528 sect_offset_str (child_die->sect_off),
10529 objfile_name (objfile));
10530 continue;
10531 }
10532
10533 excludes.push_back (imported_name);
10534
10535 process_die (child_die, cu);
10536 }
10537
10538 add_using_directive (using_directives (cu),
10539 import_prefix,
10540 canonical_name,
10541 import_alias,
10542 imported_declaration,
10543 excludes,
10544 0,
10545 &objfile->objfile_obstack);
10546 }
10547
10548 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10549 types, but gives them a size of zero. Starting with version 14,
10550 ICC is compatible with GCC. */
10551
10552 static bool
10553 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10554 {
10555 if (!cu->checked_producer)
10556 check_producer (cu);
10557
10558 return cu->producer_is_icc_lt_14;
10559 }
10560
10561 /* ICC generates a DW_AT_type for C void functions. This was observed on
10562 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10563 which says that void functions should not have a DW_AT_type. */
10564
10565 static bool
10566 producer_is_icc (struct dwarf2_cu *cu)
10567 {
10568 if (!cu->checked_producer)
10569 check_producer (cu);
10570
10571 return cu->producer_is_icc;
10572 }
10573
10574 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10575 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10576 this, it was first present in GCC release 4.3.0. */
10577
10578 static bool
10579 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10580 {
10581 if (!cu->checked_producer)
10582 check_producer (cu);
10583
10584 return cu->producer_is_gcc_lt_4_3;
10585 }
10586
10587 static file_and_directory
10588 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10589 {
10590 file_and_directory res;
10591
10592 /* Find the filename. Do not use dwarf2_name here, since the filename
10593 is not a source language identifier. */
10594 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10595 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10596
10597 if (res.comp_dir == NULL
10598 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10599 && IS_ABSOLUTE_PATH (res.name))
10600 {
10601 res.comp_dir_storage = ldirname (res.name);
10602 if (!res.comp_dir_storage.empty ())
10603 res.comp_dir = res.comp_dir_storage.c_str ();
10604 }
10605 if (res.comp_dir != NULL)
10606 {
10607 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10608 directory, get rid of it. */
10609 const char *cp = strchr (res.comp_dir, ':');
10610
10611 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10612 res.comp_dir = cp + 1;
10613 }
10614
10615 if (res.name == NULL)
10616 res.name = "<unknown>";
10617
10618 return res;
10619 }
10620
10621 /* Handle DW_AT_stmt_list for a compilation unit.
10622 DIE is the DW_TAG_compile_unit die for CU.
10623 COMP_DIR is the compilation directory. LOWPC is passed to
10624 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10625
10626 static void
10627 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10628 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10629 {
10630 struct dwarf2_per_objfile *dwarf2_per_objfile
10631 = cu->per_cu->dwarf2_per_objfile;
10632 struct attribute *attr;
10633 struct line_header line_header_local;
10634 hashval_t line_header_local_hash;
10635 void **slot;
10636 int decode_mapping;
10637
10638 gdb_assert (! cu->per_cu->is_debug_types);
10639
10640 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10641 if (attr == NULL)
10642 return;
10643
10644 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10645
10646 /* The line header hash table is only created if needed (it exists to
10647 prevent redundant reading of the line table for partial_units).
10648 If we're given a partial_unit, we'll need it. If we're given a
10649 compile_unit, then use the line header hash table if it's already
10650 created, but don't create one just yet. */
10651
10652 if (dwarf2_per_objfile->line_header_hash == NULL
10653 && die->tag == DW_TAG_partial_unit)
10654 {
10655 dwarf2_per_objfile->line_header_hash
10656 .reset (htab_create_alloc (127, line_header_hash_voidp,
10657 line_header_eq_voidp,
10658 free_line_header_voidp,
10659 xcalloc, xfree));
10660 }
10661
10662 line_header_local.sect_off = line_offset;
10663 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10664 line_header_local_hash = line_header_hash (&line_header_local);
10665 if (dwarf2_per_objfile->line_header_hash != NULL)
10666 {
10667 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10668 &line_header_local,
10669 line_header_local_hash, NO_INSERT);
10670
10671 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10672 is not present in *SLOT (since if there is something in *SLOT then
10673 it will be for a partial_unit). */
10674 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10675 {
10676 gdb_assert (*slot != NULL);
10677 cu->line_header = (struct line_header *) *slot;
10678 return;
10679 }
10680 }
10681
10682 /* dwarf_decode_line_header does not yet provide sufficient information.
10683 We always have to call also dwarf_decode_lines for it. */
10684 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10685 if (lh == NULL)
10686 return;
10687
10688 cu->line_header = lh.release ();
10689 cu->line_header_die_owner = die;
10690
10691 if (dwarf2_per_objfile->line_header_hash == NULL)
10692 slot = NULL;
10693 else
10694 {
10695 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10696 &line_header_local,
10697 line_header_local_hash, INSERT);
10698 gdb_assert (slot != NULL);
10699 }
10700 if (slot != NULL && *slot == NULL)
10701 {
10702 /* This newly decoded line number information unit will be owned
10703 by line_header_hash hash table. */
10704 *slot = cu->line_header;
10705 cu->line_header_die_owner = NULL;
10706 }
10707 else
10708 {
10709 /* We cannot free any current entry in (*slot) as that struct line_header
10710 may be already used by multiple CUs. Create only temporary decoded
10711 line_header for this CU - it may happen at most once for each line
10712 number information unit. And if we're not using line_header_hash
10713 then this is what we want as well. */
10714 gdb_assert (die->tag != DW_TAG_partial_unit);
10715 }
10716 decode_mapping = (die->tag != DW_TAG_partial_unit);
10717 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10718 decode_mapping);
10719
10720 }
10721
10722 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10723
10724 static void
10725 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10726 {
10727 struct dwarf2_per_objfile *dwarf2_per_objfile
10728 = cu->per_cu->dwarf2_per_objfile;
10729 struct objfile *objfile = dwarf2_per_objfile->objfile;
10730 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10731 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10732 CORE_ADDR highpc = ((CORE_ADDR) 0);
10733 struct attribute *attr;
10734 struct die_info *child_die;
10735 CORE_ADDR baseaddr;
10736
10737 prepare_one_comp_unit (cu, die, cu->language);
10738 baseaddr = objfile->text_section_offset ();
10739
10740 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10741
10742 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10743 from finish_block. */
10744 if (lowpc == ((CORE_ADDR) -1))
10745 lowpc = highpc;
10746 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10747
10748 file_and_directory fnd = find_file_and_directory (die, cu);
10749
10750 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10751 standardised yet. As a workaround for the language detection we fall
10752 back to the DW_AT_producer string. */
10753 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10754 cu->language = language_opencl;
10755
10756 /* Similar hack for Go. */
10757 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10758 set_cu_language (DW_LANG_Go, cu);
10759
10760 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10761
10762 /* Decode line number information if present. We do this before
10763 processing child DIEs, so that the line header table is available
10764 for DW_AT_decl_file. */
10765 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10766
10767 /* Process all dies in compilation unit. */
10768 if (die->child != NULL)
10769 {
10770 child_die = die->child;
10771 while (child_die && child_die->tag)
10772 {
10773 process_die (child_die, cu);
10774 child_die = sibling_die (child_die);
10775 }
10776 }
10777
10778 /* Decode macro information, if present. Dwarf 2 macro information
10779 refers to information in the line number info statement program
10780 header, so we can only read it if we've read the header
10781 successfully. */
10782 attr = dwarf2_attr (die, DW_AT_macros, cu);
10783 if (attr == NULL)
10784 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10785 if (attr && cu->line_header)
10786 {
10787 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10788 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10789
10790 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10791 }
10792 else
10793 {
10794 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10795 if (attr && cu->line_header)
10796 {
10797 unsigned int macro_offset = DW_UNSND (attr);
10798
10799 dwarf_decode_macros (cu, macro_offset, 0);
10800 }
10801 }
10802 }
10803
10804 void
10805 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10806 {
10807 struct type_unit_group *tu_group;
10808 int first_time;
10809 struct attribute *attr;
10810 unsigned int i;
10811 struct signatured_type *sig_type;
10812
10813 gdb_assert (per_cu->is_debug_types);
10814 sig_type = (struct signatured_type *) per_cu;
10815
10816 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10817
10818 /* If we're using .gdb_index (includes -readnow) then
10819 per_cu->type_unit_group may not have been set up yet. */
10820 if (sig_type->type_unit_group == NULL)
10821 sig_type->type_unit_group = get_type_unit_group (this, attr);
10822 tu_group = sig_type->type_unit_group;
10823
10824 /* If we've already processed this stmt_list there's no real need to
10825 do it again, we could fake it and just recreate the part we need
10826 (file name,index -> symtab mapping). If data shows this optimization
10827 is useful we can do it then. */
10828 first_time = tu_group->compunit_symtab == NULL;
10829
10830 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10831 debug info. */
10832 line_header_up lh;
10833 if (attr != NULL)
10834 {
10835 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10836 lh = dwarf_decode_line_header (line_offset, this);
10837 }
10838 if (lh == NULL)
10839 {
10840 if (first_time)
10841 start_symtab ("", NULL, 0);
10842 else
10843 {
10844 gdb_assert (tu_group->symtabs == NULL);
10845 gdb_assert (m_builder == nullptr);
10846 struct compunit_symtab *cust = tu_group->compunit_symtab;
10847 m_builder.reset (new struct buildsym_compunit
10848 (COMPUNIT_OBJFILE (cust), "",
10849 COMPUNIT_DIRNAME (cust),
10850 compunit_language (cust),
10851 0, cust));
10852 }
10853 return;
10854 }
10855
10856 line_header = lh.release ();
10857 line_header_die_owner = die;
10858
10859 if (first_time)
10860 {
10861 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10862
10863 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10864 still initializing it, and our caller (a few levels up)
10865 process_full_type_unit still needs to know if this is the first
10866 time. */
10867
10868 tu_group->num_symtabs = line_header->file_names_size ();
10869 tu_group->symtabs = XNEWVEC (struct symtab *,
10870 line_header->file_names_size ());
10871
10872 auto &file_names = line_header->file_names ();
10873 for (i = 0; i < file_names.size (); ++i)
10874 {
10875 file_entry &fe = file_names[i];
10876 dwarf2_start_subfile (this, fe.name,
10877 fe.include_dir (line_header));
10878 buildsym_compunit *b = get_builder ();
10879 if (b->get_current_subfile ()->symtab == NULL)
10880 {
10881 /* NOTE: start_subfile will recognize when it's been
10882 passed a file it has already seen. So we can't
10883 assume there's a simple mapping from
10884 cu->line_header->file_names to subfiles, plus
10885 cu->line_header->file_names may contain dups. */
10886 b->get_current_subfile ()->symtab
10887 = allocate_symtab (cust, b->get_current_subfile ()->name);
10888 }
10889
10890 fe.symtab = b->get_current_subfile ()->symtab;
10891 tu_group->symtabs[i] = fe.symtab;
10892 }
10893 }
10894 else
10895 {
10896 gdb_assert (m_builder == nullptr);
10897 struct compunit_symtab *cust = tu_group->compunit_symtab;
10898 m_builder.reset (new struct buildsym_compunit
10899 (COMPUNIT_OBJFILE (cust), "",
10900 COMPUNIT_DIRNAME (cust),
10901 compunit_language (cust),
10902 0, cust));
10903
10904 auto &file_names = line_header->file_names ();
10905 for (i = 0; i < file_names.size (); ++i)
10906 {
10907 file_entry &fe = file_names[i];
10908 fe.symtab = tu_group->symtabs[i];
10909 }
10910 }
10911
10912 /* The main symtab is allocated last. Type units don't have DW_AT_name
10913 so they don't have a "real" (so to speak) symtab anyway.
10914 There is later code that will assign the main symtab to all symbols
10915 that don't have one. We need to handle the case of a symbol with a
10916 missing symtab (DW_AT_decl_file) anyway. */
10917 }
10918
10919 /* Process DW_TAG_type_unit.
10920 For TUs we want to skip the first top level sibling if it's not the
10921 actual type being defined by this TU. In this case the first top
10922 level sibling is there to provide context only. */
10923
10924 static void
10925 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10926 {
10927 struct die_info *child_die;
10928
10929 prepare_one_comp_unit (cu, die, language_minimal);
10930
10931 /* Initialize (or reinitialize) the machinery for building symtabs.
10932 We do this before processing child DIEs, so that the line header table
10933 is available for DW_AT_decl_file. */
10934 cu->setup_type_unit_groups (die);
10935
10936 if (die->child != NULL)
10937 {
10938 child_die = die->child;
10939 while (child_die && child_die->tag)
10940 {
10941 process_die (child_die, cu);
10942 child_die = sibling_die (child_die);
10943 }
10944 }
10945 }
10946 \f
10947 /* DWO/DWP files.
10948
10949 http://gcc.gnu.org/wiki/DebugFission
10950 http://gcc.gnu.org/wiki/DebugFissionDWP
10951
10952 To simplify handling of both DWO files ("object" files with the DWARF info)
10953 and DWP files (a file with the DWOs packaged up into one file), we treat
10954 DWP files as having a collection of virtual DWO files. */
10955
10956 static hashval_t
10957 hash_dwo_file (const void *item)
10958 {
10959 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10960 hashval_t hash;
10961
10962 hash = htab_hash_string (dwo_file->dwo_name);
10963 if (dwo_file->comp_dir != NULL)
10964 hash += htab_hash_string (dwo_file->comp_dir);
10965 return hash;
10966 }
10967
10968 static int
10969 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10970 {
10971 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10972 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10973
10974 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10975 return 0;
10976 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10977 return lhs->comp_dir == rhs->comp_dir;
10978 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10979 }
10980
10981 /* Allocate a hash table for DWO files. */
10982
10983 static htab_up
10984 allocate_dwo_file_hash_table (struct objfile *objfile)
10985 {
10986 auto delete_dwo_file = [] (void *item)
10987 {
10988 struct dwo_file *dwo_file = (struct dwo_file *) item;
10989
10990 delete dwo_file;
10991 };
10992
10993 return htab_up (htab_create_alloc (41,
10994 hash_dwo_file,
10995 eq_dwo_file,
10996 delete_dwo_file,
10997 xcalloc, xfree));
10998 }
10999
11000 /* Lookup DWO file DWO_NAME. */
11001
11002 static void **
11003 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11004 const char *dwo_name,
11005 const char *comp_dir)
11006 {
11007 struct dwo_file find_entry;
11008 void **slot;
11009
11010 if (dwarf2_per_objfile->dwo_files == NULL)
11011 dwarf2_per_objfile->dwo_files
11012 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11013
11014 find_entry.dwo_name = dwo_name;
11015 find_entry.comp_dir = comp_dir;
11016 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11017 INSERT);
11018
11019 return slot;
11020 }
11021
11022 static hashval_t
11023 hash_dwo_unit (const void *item)
11024 {
11025 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11026
11027 /* This drops the top 32 bits of the id, but is ok for a hash. */
11028 return dwo_unit->signature;
11029 }
11030
11031 static int
11032 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11033 {
11034 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11035 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11036
11037 /* The signature is assumed to be unique within the DWO file.
11038 So while object file CU dwo_id's always have the value zero,
11039 that's OK, assuming each object file DWO file has only one CU,
11040 and that's the rule for now. */
11041 return lhs->signature == rhs->signature;
11042 }
11043
11044 /* Allocate a hash table for DWO CUs,TUs.
11045 There is one of these tables for each of CUs,TUs for each DWO file. */
11046
11047 static htab_up
11048 allocate_dwo_unit_table (struct objfile *objfile)
11049 {
11050 /* Start out with a pretty small number.
11051 Generally DWO files contain only one CU and maybe some TUs. */
11052 return htab_up (htab_create_alloc (3,
11053 hash_dwo_unit,
11054 eq_dwo_unit,
11055 NULL, xcalloc, xfree));
11056 }
11057
11058 /* die_reader_func for create_dwo_cu. */
11059
11060 static void
11061 create_dwo_cu_reader (const struct die_reader_specs *reader,
11062 const gdb_byte *info_ptr,
11063 struct die_info *comp_unit_die,
11064 struct dwo_file *dwo_file,
11065 struct dwo_unit *dwo_unit)
11066 {
11067 struct dwarf2_cu *cu = reader->cu;
11068 sect_offset sect_off = cu->per_cu->sect_off;
11069 struct dwarf2_section_info *section = cu->per_cu->section;
11070
11071 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11072 if (!signature.has_value ())
11073 {
11074 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11075 " its dwo_id [in module %s]"),
11076 sect_offset_str (sect_off), dwo_file->dwo_name);
11077 return;
11078 }
11079
11080 dwo_unit->dwo_file = dwo_file;
11081 dwo_unit->signature = *signature;
11082 dwo_unit->section = section;
11083 dwo_unit->sect_off = sect_off;
11084 dwo_unit->length = cu->per_cu->length;
11085
11086 if (dwarf_read_debug)
11087 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11088 sect_offset_str (sect_off),
11089 hex_string (dwo_unit->signature));
11090 }
11091
11092 /* Create the dwo_units for the CUs in a DWO_FILE.
11093 Note: This function processes DWO files only, not DWP files. */
11094
11095 static void
11096 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11097 dwarf2_cu *cu, struct dwo_file &dwo_file,
11098 dwarf2_section_info &section, htab_up &cus_htab)
11099 {
11100 struct objfile *objfile = dwarf2_per_objfile->objfile;
11101 const gdb_byte *info_ptr, *end_ptr;
11102
11103 section.read (objfile);
11104 info_ptr = section.buffer;
11105
11106 if (info_ptr == NULL)
11107 return;
11108
11109 if (dwarf_read_debug)
11110 {
11111 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11112 section.get_name (),
11113 section.get_file_name ());
11114 }
11115
11116 end_ptr = info_ptr + section.size;
11117 while (info_ptr < end_ptr)
11118 {
11119 struct dwarf2_per_cu_data per_cu;
11120 struct dwo_unit read_unit {};
11121 struct dwo_unit *dwo_unit;
11122 void **slot;
11123 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11124
11125 memset (&per_cu, 0, sizeof (per_cu));
11126 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11127 per_cu.is_debug_types = 0;
11128 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11129 per_cu.section = &section;
11130
11131 cutu_reader reader (&per_cu, cu, &dwo_file);
11132 if (!reader.dummy_p)
11133 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11134 &dwo_file, &read_unit);
11135 info_ptr += per_cu.length;
11136
11137 // If the unit could not be parsed, skip it.
11138 if (read_unit.dwo_file == NULL)
11139 continue;
11140
11141 if (cus_htab == NULL)
11142 cus_htab = allocate_dwo_unit_table (objfile);
11143
11144 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11145 *dwo_unit = read_unit;
11146 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11147 gdb_assert (slot != NULL);
11148 if (*slot != NULL)
11149 {
11150 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11151 sect_offset dup_sect_off = dup_cu->sect_off;
11152
11153 complaint (_("debug cu entry at offset %s is duplicate to"
11154 " the entry at offset %s, signature %s"),
11155 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11156 hex_string (dwo_unit->signature));
11157 }
11158 *slot = (void *)dwo_unit;
11159 }
11160 }
11161
11162 /* DWP file .debug_{cu,tu}_index section format:
11163 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11164
11165 DWP Version 1:
11166
11167 Both index sections have the same format, and serve to map a 64-bit
11168 signature to a set of section numbers. Each section begins with a header,
11169 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11170 indexes, and a pool of 32-bit section numbers. The index sections will be
11171 aligned at 8-byte boundaries in the file.
11172
11173 The index section header consists of:
11174
11175 V, 32 bit version number
11176 -, 32 bits unused
11177 N, 32 bit number of compilation units or type units in the index
11178 M, 32 bit number of slots in the hash table
11179
11180 Numbers are recorded using the byte order of the application binary.
11181
11182 The hash table begins at offset 16 in the section, and consists of an array
11183 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11184 order of the application binary). Unused slots in the hash table are 0.
11185 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11186
11187 The parallel table begins immediately after the hash table
11188 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11189 array of 32-bit indexes (using the byte order of the application binary),
11190 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11191 table contains a 32-bit index into the pool of section numbers. For unused
11192 hash table slots, the corresponding entry in the parallel table will be 0.
11193
11194 The pool of section numbers begins immediately following the hash table
11195 (at offset 16 + 12 * M from the beginning of the section). The pool of
11196 section numbers consists of an array of 32-bit words (using the byte order
11197 of the application binary). Each item in the array is indexed starting
11198 from 0. The hash table entry provides the index of the first section
11199 number in the set. Additional section numbers in the set follow, and the
11200 set is terminated by a 0 entry (section number 0 is not used in ELF).
11201
11202 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11203 section must be the first entry in the set, and the .debug_abbrev.dwo must
11204 be the second entry. Other members of the set may follow in any order.
11205
11206 ---
11207
11208 DWP Version 2:
11209
11210 DWP Version 2 combines all the .debug_info, etc. sections into one,
11211 and the entries in the index tables are now offsets into these sections.
11212 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11213 section.
11214
11215 Index Section Contents:
11216 Header
11217 Hash Table of Signatures dwp_hash_table.hash_table
11218 Parallel Table of Indices dwp_hash_table.unit_table
11219 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11220 Table of Section Sizes dwp_hash_table.v2.sizes
11221
11222 The index section header consists of:
11223
11224 V, 32 bit version number
11225 L, 32 bit number of columns in the table of section offsets
11226 N, 32 bit number of compilation units or type units in the index
11227 M, 32 bit number of slots in the hash table
11228
11229 Numbers are recorded using the byte order of the application binary.
11230
11231 The hash table has the same format as version 1.
11232 The parallel table of indices has the same format as version 1,
11233 except that the entries are origin-1 indices into the table of sections
11234 offsets and the table of section sizes.
11235
11236 The table of offsets begins immediately following the parallel table
11237 (at offset 16 + 12 * M from the beginning of the section). The table is
11238 a two-dimensional array of 32-bit words (using the byte order of the
11239 application binary), with L columns and N+1 rows, in row-major order.
11240 Each row in the array is indexed starting from 0. The first row provides
11241 a key to the remaining rows: each column in this row provides an identifier
11242 for a debug section, and the offsets in the same column of subsequent rows
11243 refer to that section. The section identifiers are:
11244
11245 DW_SECT_INFO 1 .debug_info.dwo
11246 DW_SECT_TYPES 2 .debug_types.dwo
11247 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11248 DW_SECT_LINE 4 .debug_line.dwo
11249 DW_SECT_LOC 5 .debug_loc.dwo
11250 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11251 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11252 DW_SECT_MACRO 8 .debug_macro.dwo
11253
11254 The offsets provided by the CU and TU index sections are the base offsets
11255 for the contributions made by each CU or TU to the corresponding section
11256 in the package file. Each CU and TU header contains an abbrev_offset
11257 field, used to find the abbreviations table for that CU or TU within the
11258 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11259 be interpreted as relative to the base offset given in the index section.
11260 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11261 should be interpreted as relative to the base offset for .debug_line.dwo,
11262 and offsets into other debug sections obtained from DWARF attributes should
11263 also be interpreted as relative to the corresponding base offset.
11264
11265 The table of sizes begins immediately following the table of offsets.
11266 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11267 with L columns and N rows, in row-major order. Each row in the array is
11268 indexed starting from 1 (row 0 is shared by the two tables).
11269
11270 ---
11271
11272 Hash table lookup is handled the same in version 1 and 2:
11273
11274 We assume that N and M will not exceed 2^32 - 1.
11275 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11276
11277 Given a 64-bit compilation unit signature or a type signature S, an entry
11278 in the hash table is located as follows:
11279
11280 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11281 the low-order k bits all set to 1.
11282
11283 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11284
11285 3) If the hash table entry at index H matches the signature, use that
11286 entry. If the hash table entry at index H is unused (all zeroes),
11287 terminate the search: the signature is not present in the table.
11288
11289 4) Let H = (H + H') modulo M. Repeat at Step 3.
11290
11291 Because M > N and H' and M are relatively prime, the search is guaranteed
11292 to stop at an unused slot or find the match. */
11293
11294 /* Create a hash table to map DWO IDs to their CU/TU entry in
11295 .debug_{info,types}.dwo in DWP_FILE.
11296 Returns NULL if there isn't one.
11297 Note: This function processes DWP files only, not DWO files. */
11298
11299 static struct dwp_hash_table *
11300 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11301 struct dwp_file *dwp_file, int is_debug_types)
11302 {
11303 struct objfile *objfile = dwarf2_per_objfile->objfile;
11304 bfd *dbfd = dwp_file->dbfd.get ();
11305 const gdb_byte *index_ptr, *index_end;
11306 struct dwarf2_section_info *index;
11307 uint32_t version, nr_columns, nr_units, nr_slots;
11308 struct dwp_hash_table *htab;
11309
11310 if (is_debug_types)
11311 index = &dwp_file->sections.tu_index;
11312 else
11313 index = &dwp_file->sections.cu_index;
11314
11315 if (index->empty ())
11316 return NULL;
11317 index->read (objfile);
11318
11319 index_ptr = index->buffer;
11320 index_end = index_ptr + index->size;
11321
11322 version = read_4_bytes (dbfd, index_ptr);
11323 index_ptr += 4;
11324 if (version == 2)
11325 nr_columns = read_4_bytes (dbfd, index_ptr);
11326 else
11327 nr_columns = 0;
11328 index_ptr += 4;
11329 nr_units = read_4_bytes (dbfd, index_ptr);
11330 index_ptr += 4;
11331 nr_slots = read_4_bytes (dbfd, index_ptr);
11332 index_ptr += 4;
11333
11334 if (version != 1 && version != 2)
11335 {
11336 error (_("Dwarf Error: unsupported DWP file version (%s)"
11337 " [in module %s]"),
11338 pulongest (version), dwp_file->name);
11339 }
11340 if (nr_slots != (nr_slots & -nr_slots))
11341 {
11342 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11343 " is not power of 2 [in module %s]"),
11344 pulongest (nr_slots), dwp_file->name);
11345 }
11346
11347 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11348 htab->version = version;
11349 htab->nr_columns = nr_columns;
11350 htab->nr_units = nr_units;
11351 htab->nr_slots = nr_slots;
11352 htab->hash_table = index_ptr;
11353 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11354
11355 /* Exit early if the table is empty. */
11356 if (nr_slots == 0 || nr_units == 0
11357 || (version == 2 && nr_columns == 0))
11358 {
11359 /* All must be zero. */
11360 if (nr_slots != 0 || nr_units != 0
11361 || (version == 2 && nr_columns != 0))
11362 {
11363 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11364 " all zero [in modules %s]"),
11365 dwp_file->name);
11366 }
11367 return htab;
11368 }
11369
11370 if (version == 1)
11371 {
11372 htab->section_pool.v1.indices =
11373 htab->unit_table + sizeof (uint32_t) * nr_slots;
11374 /* It's harder to decide whether the section is too small in v1.
11375 V1 is deprecated anyway so we punt. */
11376 }
11377 else
11378 {
11379 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11380 int *ids = htab->section_pool.v2.section_ids;
11381 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11382 /* Reverse map for error checking. */
11383 int ids_seen[DW_SECT_MAX + 1];
11384 int i;
11385
11386 if (nr_columns < 2)
11387 {
11388 error (_("Dwarf Error: bad DWP hash table, too few columns"
11389 " in section table [in module %s]"),
11390 dwp_file->name);
11391 }
11392 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11393 {
11394 error (_("Dwarf Error: bad DWP hash table, too many columns"
11395 " in section table [in module %s]"),
11396 dwp_file->name);
11397 }
11398 memset (ids, 255, sizeof_ids);
11399 memset (ids_seen, 255, sizeof (ids_seen));
11400 for (i = 0; i < nr_columns; ++i)
11401 {
11402 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11403
11404 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11405 {
11406 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11407 " in section table [in module %s]"),
11408 id, dwp_file->name);
11409 }
11410 if (ids_seen[id] != -1)
11411 {
11412 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11413 " id %d in section table [in module %s]"),
11414 id, dwp_file->name);
11415 }
11416 ids_seen[id] = i;
11417 ids[i] = id;
11418 }
11419 /* Must have exactly one info or types section. */
11420 if (((ids_seen[DW_SECT_INFO] != -1)
11421 + (ids_seen[DW_SECT_TYPES] != -1))
11422 != 1)
11423 {
11424 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11425 " DWO info/types section [in module %s]"),
11426 dwp_file->name);
11427 }
11428 /* Must have an abbrev section. */
11429 if (ids_seen[DW_SECT_ABBREV] == -1)
11430 {
11431 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11432 " section [in module %s]"),
11433 dwp_file->name);
11434 }
11435 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11436 htab->section_pool.v2.sizes =
11437 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11438 * nr_units * nr_columns);
11439 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11440 * nr_units * nr_columns))
11441 > index_end)
11442 {
11443 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11444 " [in module %s]"),
11445 dwp_file->name);
11446 }
11447 }
11448
11449 return htab;
11450 }
11451
11452 /* Update SECTIONS with the data from SECTP.
11453
11454 This function is like the other "locate" section routines that are
11455 passed to bfd_map_over_sections, but in this context the sections to
11456 read comes from the DWP V1 hash table, not the full ELF section table.
11457
11458 The result is non-zero for success, or zero if an error was found. */
11459
11460 static int
11461 locate_v1_virtual_dwo_sections (asection *sectp,
11462 struct virtual_v1_dwo_sections *sections)
11463 {
11464 const struct dwop_section_names *names = &dwop_section_names;
11465
11466 if (section_is_p (sectp->name, &names->abbrev_dwo))
11467 {
11468 /* There can be only one. */
11469 if (sections->abbrev.s.section != NULL)
11470 return 0;
11471 sections->abbrev.s.section = sectp;
11472 sections->abbrev.size = bfd_section_size (sectp);
11473 }
11474 else if (section_is_p (sectp->name, &names->info_dwo)
11475 || section_is_p (sectp->name, &names->types_dwo))
11476 {
11477 /* There can be only one. */
11478 if (sections->info_or_types.s.section != NULL)
11479 return 0;
11480 sections->info_or_types.s.section = sectp;
11481 sections->info_or_types.size = bfd_section_size (sectp);
11482 }
11483 else if (section_is_p (sectp->name, &names->line_dwo))
11484 {
11485 /* There can be only one. */
11486 if (sections->line.s.section != NULL)
11487 return 0;
11488 sections->line.s.section = sectp;
11489 sections->line.size = bfd_section_size (sectp);
11490 }
11491 else if (section_is_p (sectp->name, &names->loc_dwo))
11492 {
11493 /* There can be only one. */
11494 if (sections->loc.s.section != NULL)
11495 return 0;
11496 sections->loc.s.section = sectp;
11497 sections->loc.size = bfd_section_size (sectp);
11498 }
11499 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11500 {
11501 /* There can be only one. */
11502 if (sections->macinfo.s.section != NULL)
11503 return 0;
11504 sections->macinfo.s.section = sectp;
11505 sections->macinfo.size = bfd_section_size (sectp);
11506 }
11507 else if (section_is_p (sectp->name, &names->macro_dwo))
11508 {
11509 /* There can be only one. */
11510 if (sections->macro.s.section != NULL)
11511 return 0;
11512 sections->macro.s.section = sectp;
11513 sections->macro.size = bfd_section_size (sectp);
11514 }
11515 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11516 {
11517 /* There can be only one. */
11518 if (sections->str_offsets.s.section != NULL)
11519 return 0;
11520 sections->str_offsets.s.section = sectp;
11521 sections->str_offsets.size = bfd_section_size (sectp);
11522 }
11523 else
11524 {
11525 /* No other kind of section is valid. */
11526 return 0;
11527 }
11528
11529 return 1;
11530 }
11531
11532 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11533 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11534 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11535 This is for DWP version 1 files. */
11536
11537 static struct dwo_unit *
11538 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11539 struct dwp_file *dwp_file,
11540 uint32_t unit_index,
11541 const char *comp_dir,
11542 ULONGEST signature, int is_debug_types)
11543 {
11544 struct objfile *objfile = dwarf2_per_objfile->objfile;
11545 const struct dwp_hash_table *dwp_htab =
11546 is_debug_types ? dwp_file->tus : dwp_file->cus;
11547 bfd *dbfd = dwp_file->dbfd.get ();
11548 const char *kind = is_debug_types ? "TU" : "CU";
11549 struct dwo_file *dwo_file;
11550 struct dwo_unit *dwo_unit;
11551 struct virtual_v1_dwo_sections sections;
11552 void **dwo_file_slot;
11553 int i;
11554
11555 gdb_assert (dwp_file->version == 1);
11556
11557 if (dwarf_read_debug)
11558 {
11559 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11560 kind,
11561 pulongest (unit_index), hex_string (signature),
11562 dwp_file->name);
11563 }
11564
11565 /* Fetch the sections of this DWO unit.
11566 Put a limit on the number of sections we look for so that bad data
11567 doesn't cause us to loop forever. */
11568
11569 #define MAX_NR_V1_DWO_SECTIONS \
11570 (1 /* .debug_info or .debug_types */ \
11571 + 1 /* .debug_abbrev */ \
11572 + 1 /* .debug_line */ \
11573 + 1 /* .debug_loc */ \
11574 + 1 /* .debug_str_offsets */ \
11575 + 1 /* .debug_macro or .debug_macinfo */ \
11576 + 1 /* trailing zero */)
11577
11578 memset (&sections, 0, sizeof (sections));
11579
11580 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11581 {
11582 asection *sectp;
11583 uint32_t section_nr =
11584 read_4_bytes (dbfd,
11585 dwp_htab->section_pool.v1.indices
11586 + (unit_index + i) * sizeof (uint32_t));
11587
11588 if (section_nr == 0)
11589 break;
11590 if (section_nr >= dwp_file->num_sections)
11591 {
11592 error (_("Dwarf Error: bad DWP hash table, section number too large"
11593 " [in module %s]"),
11594 dwp_file->name);
11595 }
11596
11597 sectp = dwp_file->elf_sections[section_nr];
11598 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11599 {
11600 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11601 " [in module %s]"),
11602 dwp_file->name);
11603 }
11604 }
11605
11606 if (i < 2
11607 || sections.info_or_types.empty ()
11608 || sections.abbrev.empty ())
11609 {
11610 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11611 " [in module %s]"),
11612 dwp_file->name);
11613 }
11614 if (i == MAX_NR_V1_DWO_SECTIONS)
11615 {
11616 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11617 " [in module %s]"),
11618 dwp_file->name);
11619 }
11620
11621 /* It's easier for the rest of the code if we fake a struct dwo_file and
11622 have dwo_unit "live" in that. At least for now.
11623
11624 The DWP file can be made up of a random collection of CUs and TUs.
11625 However, for each CU + set of TUs that came from the same original DWO
11626 file, we can combine them back into a virtual DWO file to save space
11627 (fewer struct dwo_file objects to allocate). Remember that for really
11628 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11629
11630 std::string virtual_dwo_name =
11631 string_printf ("virtual-dwo/%d-%d-%d-%d",
11632 sections.abbrev.get_id (),
11633 sections.line.get_id (),
11634 sections.loc.get_id (),
11635 sections.str_offsets.get_id ());
11636 /* Can we use an existing virtual DWO file? */
11637 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11638 virtual_dwo_name.c_str (),
11639 comp_dir);
11640 /* Create one if necessary. */
11641 if (*dwo_file_slot == NULL)
11642 {
11643 if (dwarf_read_debug)
11644 {
11645 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11646 virtual_dwo_name.c_str ());
11647 }
11648 dwo_file = new struct dwo_file;
11649 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11650 virtual_dwo_name);
11651 dwo_file->comp_dir = comp_dir;
11652 dwo_file->sections.abbrev = sections.abbrev;
11653 dwo_file->sections.line = sections.line;
11654 dwo_file->sections.loc = sections.loc;
11655 dwo_file->sections.macinfo = sections.macinfo;
11656 dwo_file->sections.macro = sections.macro;
11657 dwo_file->sections.str_offsets = sections.str_offsets;
11658 /* The "str" section is global to the entire DWP file. */
11659 dwo_file->sections.str = dwp_file->sections.str;
11660 /* The info or types section is assigned below to dwo_unit,
11661 there's no need to record it in dwo_file.
11662 Also, we can't simply record type sections in dwo_file because
11663 we record a pointer into the vector in dwo_unit. As we collect more
11664 types we'll grow the vector and eventually have to reallocate space
11665 for it, invalidating all copies of pointers into the previous
11666 contents. */
11667 *dwo_file_slot = dwo_file;
11668 }
11669 else
11670 {
11671 if (dwarf_read_debug)
11672 {
11673 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11674 virtual_dwo_name.c_str ());
11675 }
11676 dwo_file = (struct dwo_file *) *dwo_file_slot;
11677 }
11678
11679 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11680 dwo_unit->dwo_file = dwo_file;
11681 dwo_unit->signature = signature;
11682 dwo_unit->section =
11683 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11684 *dwo_unit->section = sections.info_or_types;
11685 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11686
11687 return dwo_unit;
11688 }
11689
11690 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11691 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11692 piece within that section used by a TU/CU, return a virtual section
11693 of just that piece. */
11694
11695 static struct dwarf2_section_info
11696 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11697 struct dwarf2_section_info *section,
11698 bfd_size_type offset, bfd_size_type size)
11699 {
11700 struct dwarf2_section_info result;
11701 asection *sectp;
11702
11703 gdb_assert (section != NULL);
11704 gdb_assert (!section->is_virtual);
11705
11706 memset (&result, 0, sizeof (result));
11707 result.s.containing_section = section;
11708 result.is_virtual = true;
11709
11710 if (size == 0)
11711 return result;
11712
11713 sectp = section->get_bfd_section ();
11714
11715 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11716 bounds of the real section. This is a pretty-rare event, so just
11717 flag an error (easier) instead of a warning and trying to cope. */
11718 if (sectp == NULL
11719 || offset + size > bfd_section_size (sectp))
11720 {
11721 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11722 " in section %s [in module %s]"),
11723 sectp ? bfd_section_name (sectp) : "<unknown>",
11724 objfile_name (dwarf2_per_objfile->objfile));
11725 }
11726
11727 result.virtual_offset = offset;
11728 result.size = size;
11729 return result;
11730 }
11731
11732 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11733 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11734 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11735 This is for DWP version 2 files. */
11736
11737 static struct dwo_unit *
11738 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11739 struct dwp_file *dwp_file,
11740 uint32_t unit_index,
11741 const char *comp_dir,
11742 ULONGEST signature, int is_debug_types)
11743 {
11744 struct objfile *objfile = dwarf2_per_objfile->objfile;
11745 const struct dwp_hash_table *dwp_htab =
11746 is_debug_types ? dwp_file->tus : dwp_file->cus;
11747 bfd *dbfd = dwp_file->dbfd.get ();
11748 const char *kind = is_debug_types ? "TU" : "CU";
11749 struct dwo_file *dwo_file;
11750 struct dwo_unit *dwo_unit;
11751 struct virtual_v2_dwo_sections sections;
11752 void **dwo_file_slot;
11753 int i;
11754
11755 gdb_assert (dwp_file->version == 2);
11756
11757 if (dwarf_read_debug)
11758 {
11759 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11760 kind,
11761 pulongest (unit_index), hex_string (signature),
11762 dwp_file->name);
11763 }
11764
11765 /* Fetch the section offsets of this DWO unit. */
11766
11767 memset (&sections, 0, sizeof (sections));
11768
11769 for (i = 0; i < dwp_htab->nr_columns; ++i)
11770 {
11771 uint32_t offset = read_4_bytes (dbfd,
11772 dwp_htab->section_pool.v2.offsets
11773 + (((unit_index - 1) * dwp_htab->nr_columns
11774 + i)
11775 * sizeof (uint32_t)));
11776 uint32_t size = read_4_bytes (dbfd,
11777 dwp_htab->section_pool.v2.sizes
11778 + (((unit_index - 1) * dwp_htab->nr_columns
11779 + i)
11780 * sizeof (uint32_t)));
11781
11782 switch (dwp_htab->section_pool.v2.section_ids[i])
11783 {
11784 case DW_SECT_INFO:
11785 case DW_SECT_TYPES:
11786 sections.info_or_types_offset = offset;
11787 sections.info_or_types_size = size;
11788 break;
11789 case DW_SECT_ABBREV:
11790 sections.abbrev_offset = offset;
11791 sections.abbrev_size = size;
11792 break;
11793 case DW_SECT_LINE:
11794 sections.line_offset = offset;
11795 sections.line_size = size;
11796 break;
11797 case DW_SECT_LOC:
11798 sections.loc_offset = offset;
11799 sections.loc_size = size;
11800 break;
11801 case DW_SECT_STR_OFFSETS:
11802 sections.str_offsets_offset = offset;
11803 sections.str_offsets_size = size;
11804 break;
11805 case DW_SECT_MACINFO:
11806 sections.macinfo_offset = offset;
11807 sections.macinfo_size = size;
11808 break;
11809 case DW_SECT_MACRO:
11810 sections.macro_offset = offset;
11811 sections.macro_size = size;
11812 break;
11813 }
11814 }
11815
11816 /* It's easier for the rest of the code if we fake a struct dwo_file and
11817 have dwo_unit "live" in that. At least for now.
11818
11819 The DWP file can be made up of a random collection of CUs and TUs.
11820 However, for each CU + set of TUs that came from the same original DWO
11821 file, we can combine them back into a virtual DWO file to save space
11822 (fewer struct dwo_file objects to allocate). Remember that for really
11823 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11824
11825 std::string virtual_dwo_name =
11826 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11827 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11828 (long) (sections.line_size ? sections.line_offset : 0),
11829 (long) (sections.loc_size ? sections.loc_offset : 0),
11830 (long) (sections.str_offsets_size
11831 ? sections.str_offsets_offset : 0));
11832 /* Can we use an existing virtual DWO file? */
11833 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11834 virtual_dwo_name.c_str (),
11835 comp_dir);
11836 /* Create one if necessary. */
11837 if (*dwo_file_slot == NULL)
11838 {
11839 if (dwarf_read_debug)
11840 {
11841 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11842 virtual_dwo_name.c_str ());
11843 }
11844 dwo_file = new struct dwo_file;
11845 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11846 virtual_dwo_name);
11847 dwo_file->comp_dir = comp_dir;
11848 dwo_file->sections.abbrev =
11849 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11850 sections.abbrev_offset, sections.abbrev_size);
11851 dwo_file->sections.line =
11852 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11853 sections.line_offset, sections.line_size);
11854 dwo_file->sections.loc =
11855 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11856 sections.loc_offset, sections.loc_size);
11857 dwo_file->sections.macinfo =
11858 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11859 sections.macinfo_offset, sections.macinfo_size);
11860 dwo_file->sections.macro =
11861 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11862 sections.macro_offset, sections.macro_size);
11863 dwo_file->sections.str_offsets =
11864 create_dwp_v2_section (dwarf2_per_objfile,
11865 &dwp_file->sections.str_offsets,
11866 sections.str_offsets_offset,
11867 sections.str_offsets_size);
11868 /* The "str" section is global to the entire DWP file. */
11869 dwo_file->sections.str = dwp_file->sections.str;
11870 /* The info or types section is assigned below to dwo_unit,
11871 there's no need to record it in dwo_file.
11872 Also, we can't simply record type sections in dwo_file because
11873 we record a pointer into the vector in dwo_unit. As we collect more
11874 types we'll grow the vector and eventually have to reallocate space
11875 for it, invalidating all copies of pointers into the previous
11876 contents. */
11877 *dwo_file_slot = dwo_file;
11878 }
11879 else
11880 {
11881 if (dwarf_read_debug)
11882 {
11883 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11884 virtual_dwo_name.c_str ());
11885 }
11886 dwo_file = (struct dwo_file *) *dwo_file_slot;
11887 }
11888
11889 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11890 dwo_unit->dwo_file = dwo_file;
11891 dwo_unit->signature = signature;
11892 dwo_unit->section =
11893 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11894 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11895 is_debug_types
11896 ? &dwp_file->sections.types
11897 : &dwp_file->sections.info,
11898 sections.info_or_types_offset,
11899 sections.info_or_types_size);
11900 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11901
11902 return dwo_unit;
11903 }
11904
11905 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11906 Returns NULL if the signature isn't found. */
11907
11908 static struct dwo_unit *
11909 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11910 struct dwp_file *dwp_file, const char *comp_dir,
11911 ULONGEST signature, int is_debug_types)
11912 {
11913 const struct dwp_hash_table *dwp_htab =
11914 is_debug_types ? dwp_file->tus : dwp_file->cus;
11915 bfd *dbfd = dwp_file->dbfd.get ();
11916 uint32_t mask = dwp_htab->nr_slots - 1;
11917 uint32_t hash = signature & mask;
11918 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11919 unsigned int i;
11920 void **slot;
11921 struct dwo_unit find_dwo_cu;
11922
11923 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11924 find_dwo_cu.signature = signature;
11925 slot = htab_find_slot (is_debug_types
11926 ? dwp_file->loaded_tus.get ()
11927 : dwp_file->loaded_cus.get (),
11928 &find_dwo_cu, INSERT);
11929
11930 if (*slot != NULL)
11931 return (struct dwo_unit *) *slot;
11932
11933 /* Use a for loop so that we don't loop forever on bad debug info. */
11934 for (i = 0; i < dwp_htab->nr_slots; ++i)
11935 {
11936 ULONGEST signature_in_table;
11937
11938 signature_in_table =
11939 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11940 if (signature_in_table == signature)
11941 {
11942 uint32_t unit_index =
11943 read_4_bytes (dbfd,
11944 dwp_htab->unit_table + hash * sizeof (uint32_t));
11945
11946 if (dwp_file->version == 1)
11947 {
11948 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11949 dwp_file, unit_index,
11950 comp_dir, signature,
11951 is_debug_types);
11952 }
11953 else
11954 {
11955 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11956 dwp_file, unit_index,
11957 comp_dir, signature,
11958 is_debug_types);
11959 }
11960 return (struct dwo_unit *) *slot;
11961 }
11962 if (signature_in_table == 0)
11963 return NULL;
11964 hash = (hash + hash2) & mask;
11965 }
11966
11967 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11968 " [in module %s]"),
11969 dwp_file->name);
11970 }
11971
11972 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11973 Open the file specified by FILE_NAME and hand it off to BFD for
11974 preliminary analysis. Return a newly initialized bfd *, which
11975 includes a canonicalized copy of FILE_NAME.
11976 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11977 SEARCH_CWD is true if the current directory is to be searched.
11978 It will be searched before debug-file-directory.
11979 If successful, the file is added to the bfd include table of the
11980 objfile's bfd (see gdb_bfd_record_inclusion).
11981 If unable to find/open the file, return NULL.
11982 NOTE: This function is derived from symfile_bfd_open. */
11983
11984 static gdb_bfd_ref_ptr
11985 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11986 const char *file_name, int is_dwp, int search_cwd)
11987 {
11988 int desc;
11989 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11990 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11991 to debug_file_directory. */
11992 const char *search_path;
11993 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11994
11995 gdb::unique_xmalloc_ptr<char> search_path_holder;
11996 if (search_cwd)
11997 {
11998 if (*debug_file_directory != '\0')
11999 {
12000 search_path_holder.reset (concat (".", dirname_separator_string,
12001 debug_file_directory,
12002 (char *) NULL));
12003 search_path = search_path_holder.get ();
12004 }
12005 else
12006 search_path = ".";
12007 }
12008 else
12009 search_path = debug_file_directory;
12010
12011 openp_flags flags = OPF_RETURN_REALPATH;
12012 if (is_dwp)
12013 flags |= OPF_SEARCH_IN_PATH;
12014
12015 gdb::unique_xmalloc_ptr<char> absolute_name;
12016 desc = openp (search_path, flags, file_name,
12017 O_RDONLY | O_BINARY, &absolute_name);
12018 if (desc < 0)
12019 return NULL;
12020
12021 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12022 gnutarget, desc));
12023 if (sym_bfd == NULL)
12024 return NULL;
12025 bfd_set_cacheable (sym_bfd.get (), 1);
12026
12027 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12028 return NULL;
12029
12030 /* Success. Record the bfd as having been included by the objfile's bfd.
12031 This is important because things like demangled_names_hash lives in the
12032 objfile's per_bfd space and may have references to things like symbol
12033 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12034 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12035
12036 return sym_bfd;
12037 }
12038
12039 /* Try to open DWO file FILE_NAME.
12040 COMP_DIR is the DW_AT_comp_dir attribute.
12041 The result is the bfd handle of the file.
12042 If there is a problem finding or opening the file, return NULL.
12043 Upon success, the canonicalized path of the file is stored in the bfd,
12044 same as symfile_bfd_open. */
12045
12046 static gdb_bfd_ref_ptr
12047 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12048 const char *file_name, const char *comp_dir)
12049 {
12050 if (IS_ABSOLUTE_PATH (file_name))
12051 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12052 0 /*is_dwp*/, 0 /*search_cwd*/);
12053
12054 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12055
12056 if (comp_dir != NULL)
12057 {
12058 gdb::unique_xmalloc_ptr<char> path_to_try
12059 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12060
12061 /* NOTE: If comp_dir is a relative path, this will also try the
12062 search path, which seems useful. */
12063 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12064 path_to_try.get (),
12065 0 /*is_dwp*/,
12066 1 /*search_cwd*/));
12067 if (abfd != NULL)
12068 return abfd;
12069 }
12070
12071 /* That didn't work, try debug-file-directory, which, despite its name,
12072 is a list of paths. */
12073
12074 if (*debug_file_directory == '\0')
12075 return NULL;
12076
12077 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12078 0 /*is_dwp*/, 1 /*search_cwd*/);
12079 }
12080
12081 /* This function is mapped across the sections and remembers the offset and
12082 size of each of the DWO debugging sections we are interested in. */
12083
12084 static void
12085 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12086 {
12087 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12088 const struct dwop_section_names *names = &dwop_section_names;
12089
12090 if (section_is_p (sectp->name, &names->abbrev_dwo))
12091 {
12092 dwo_sections->abbrev.s.section = sectp;
12093 dwo_sections->abbrev.size = bfd_section_size (sectp);
12094 }
12095 else if (section_is_p (sectp->name, &names->info_dwo))
12096 {
12097 dwo_sections->info.s.section = sectp;
12098 dwo_sections->info.size = bfd_section_size (sectp);
12099 }
12100 else if (section_is_p (sectp->name, &names->line_dwo))
12101 {
12102 dwo_sections->line.s.section = sectp;
12103 dwo_sections->line.size = bfd_section_size (sectp);
12104 }
12105 else if (section_is_p (sectp->name, &names->loc_dwo))
12106 {
12107 dwo_sections->loc.s.section = sectp;
12108 dwo_sections->loc.size = bfd_section_size (sectp);
12109 }
12110 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12111 {
12112 dwo_sections->macinfo.s.section = sectp;
12113 dwo_sections->macinfo.size = bfd_section_size (sectp);
12114 }
12115 else if (section_is_p (sectp->name, &names->macro_dwo))
12116 {
12117 dwo_sections->macro.s.section = sectp;
12118 dwo_sections->macro.size = bfd_section_size (sectp);
12119 }
12120 else if (section_is_p (sectp->name, &names->str_dwo))
12121 {
12122 dwo_sections->str.s.section = sectp;
12123 dwo_sections->str.size = bfd_section_size (sectp);
12124 }
12125 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12126 {
12127 dwo_sections->str_offsets.s.section = sectp;
12128 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12129 }
12130 else if (section_is_p (sectp->name, &names->types_dwo))
12131 {
12132 struct dwarf2_section_info type_section;
12133
12134 memset (&type_section, 0, sizeof (type_section));
12135 type_section.s.section = sectp;
12136 type_section.size = bfd_section_size (sectp);
12137 dwo_sections->types.push_back (type_section);
12138 }
12139 }
12140
12141 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12142 by PER_CU. This is for the non-DWP case.
12143 The result is NULL if DWO_NAME can't be found. */
12144
12145 static struct dwo_file *
12146 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12147 const char *dwo_name, const char *comp_dir)
12148 {
12149 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12150
12151 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12152 if (dbfd == NULL)
12153 {
12154 if (dwarf_read_debug)
12155 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12156 return NULL;
12157 }
12158
12159 dwo_file_up dwo_file (new struct dwo_file);
12160 dwo_file->dwo_name = dwo_name;
12161 dwo_file->comp_dir = comp_dir;
12162 dwo_file->dbfd = std::move (dbfd);
12163
12164 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12165 &dwo_file->sections);
12166
12167 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12168 dwo_file->sections.info, dwo_file->cus);
12169
12170 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12171 dwo_file->sections.types, dwo_file->tus);
12172
12173 if (dwarf_read_debug)
12174 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12175
12176 return dwo_file.release ();
12177 }
12178
12179 /* This function is mapped across the sections and remembers the offset and
12180 size of each of the DWP debugging sections common to version 1 and 2 that
12181 we are interested in. */
12182
12183 static void
12184 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12185 void *dwp_file_ptr)
12186 {
12187 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12188 const struct dwop_section_names *names = &dwop_section_names;
12189 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12190
12191 /* Record the ELF section number for later lookup: this is what the
12192 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12193 gdb_assert (elf_section_nr < dwp_file->num_sections);
12194 dwp_file->elf_sections[elf_section_nr] = sectp;
12195
12196 /* Look for specific sections that we need. */
12197 if (section_is_p (sectp->name, &names->str_dwo))
12198 {
12199 dwp_file->sections.str.s.section = sectp;
12200 dwp_file->sections.str.size = bfd_section_size (sectp);
12201 }
12202 else if (section_is_p (sectp->name, &names->cu_index))
12203 {
12204 dwp_file->sections.cu_index.s.section = sectp;
12205 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12206 }
12207 else if (section_is_p (sectp->name, &names->tu_index))
12208 {
12209 dwp_file->sections.tu_index.s.section = sectp;
12210 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12211 }
12212 }
12213
12214 /* This function is mapped across the sections and remembers the offset and
12215 size of each of the DWP version 2 debugging sections that we are interested
12216 in. This is split into a separate function because we don't know if we
12217 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12218
12219 static void
12220 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12221 {
12222 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12223 const struct dwop_section_names *names = &dwop_section_names;
12224 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12225
12226 /* Record the ELF section number for later lookup: this is what the
12227 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12228 gdb_assert (elf_section_nr < dwp_file->num_sections);
12229 dwp_file->elf_sections[elf_section_nr] = sectp;
12230
12231 /* Look for specific sections that we need. */
12232 if (section_is_p (sectp->name, &names->abbrev_dwo))
12233 {
12234 dwp_file->sections.abbrev.s.section = sectp;
12235 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12236 }
12237 else if (section_is_p (sectp->name, &names->info_dwo))
12238 {
12239 dwp_file->sections.info.s.section = sectp;
12240 dwp_file->sections.info.size = bfd_section_size (sectp);
12241 }
12242 else if (section_is_p (sectp->name, &names->line_dwo))
12243 {
12244 dwp_file->sections.line.s.section = sectp;
12245 dwp_file->sections.line.size = bfd_section_size (sectp);
12246 }
12247 else if (section_is_p (sectp->name, &names->loc_dwo))
12248 {
12249 dwp_file->sections.loc.s.section = sectp;
12250 dwp_file->sections.loc.size = bfd_section_size (sectp);
12251 }
12252 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12253 {
12254 dwp_file->sections.macinfo.s.section = sectp;
12255 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12256 }
12257 else if (section_is_p (sectp->name, &names->macro_dwo))
12258 {
12259 dwp_file->sections.macro.s.section = sectp;
12260 dwp_file->sections.macro.size = bfd_section_size (sectp);
12261 }
12262 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12263 {
12264 dwp_file->sections.str_offsets.s.section = sectp;
12265 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12266 }
12267 else if (section_is_p (sectp->name, &names->types_dwo))
12268 {
12269 dwp_file->sections.types.s.section = sectp;
12270 dwp_file->sections.types.size = bfd_section_size (sectp);
12271 }
12272 }
12273
12274 /* Hash function for dwp_file loaded CUs/TUs. */
12275
12276 static hashval_t
12277 hash_dwp_loaded_cutus (const void *item)
12278 {
12279 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12280
12281 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12282 return dwo_unit->signature;
12283 }
12284
12285 /* Equality function for dwp_file loaded CUs/TUs. */
12286
12287 static int
12288 eq_dwp_loaded_cutus (const void *a, const void *b)
12289 {
12290 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12291 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12292
12293 return dua->signature == dub->signature;
12294 }
12295
12296 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12297
12298 static htab_up
12299 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12300 {
12301 return htab_up (htab_create_alloc (3,
12302 hash_dwp_loaded_cutus,
12303 eq_dwp_loaded_cutus,
12304 NULL, xcalloc, xfree));
12305 }
12306
12307 /* Try to open DWP file FILE_NAME.
12308 The result is the bfd handle of the file.
12309 If there is a problem finding or opening the file, return NULL.
12310 Upon success, the canonicalized path of the file is stored in the bfd,
12311 same as symfile_bfd_open. */
12312
12313 static gdb_bfd_ref_ptr
12314 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12315 const char *file_name)
12316 {
12317 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12318 1 /*is_dwp*/,
12319 1 /*search_cwd*/));
12320 if (abfd != NULL)
12321 return abfd;
12322
12323 /* Work around upstream bug 15652.
12324 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12325 [Whether that's a "bug" is debatable, but it is getting in our way.]
12326 We have no real idea where the dwp file is, because gdb's realpath-ing
12327 of the executable's path may have discarded the needed info.
12328 [IWBN if the dwp file name was recorded in the executable, akin to
12329 .gnu_debuglink, but that doesn't exist yet.]
12330 Strip the directory from FILE_NAME and search again. */
12331 if (*debug_file_directory != '\0')
12332 {
12333 /* Don't implicitly search the current directory here.
12334 If the user wants to search "." to handle this case,
12335 it must be added to debug-file-directory. */
12336 return try_open_dwop_file (dwarf2_per_objfile,
12337 lbasename (file_name), 1 /*is_dwp*/,
12338 0 /*search_cwd*/);
12339 }
12340
12341 return NULL;
12342 }
12343
12344 /* Initialize the use of the DWP file for the current objfile.
12345 By convention the name of the DWP file is ${objfile}.dwp.
12346 The result is NULL if it can't be found. */
12347
12348 static std::unique_ptr<struct dwp_file>
12349 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12350 {
12351 struct objfile *objfile = dwarf2_per_objfile->objfile;
12352
12353 /* Try to find first .dwp for the binary file before any symbolic links
12354 resolving. */
12355
12356 /* If the objfile is a debug file, find the name of the real binary
12357 file and get the name of dwp file from there. */
12358 std::string dwp_name;
12359 if (objfile->separate_debug_objfile_backlink != NULL)
12360 {
12361 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12362 const char *backlink_basename = lbasename (backlink->original_name);
12363
12364 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12365 }
12366 else
12367 dwp_name = objfile->original_name;
12368
12369 dwp_name += ".dwp";
12370
12371 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12372 if (dbfd == NULL
12373 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12374 {
12375 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12376 dwp_name = objfile_name (objfile);
12377 dwp_name += ".dwp";
12378 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12379 }
12380
12381 if (dbfd == NULL)
12382 {
12383 if (dwarf_read_debug)
12384 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12385 return std::unique_ptr<dwp_file> ();
12386 }
12387
12388 const char *name = bfd_get_filename (dbfd.get ());
12389 std::unique_ptr<struct dwp_file> dwp_file
12390 (new struct dwp_file (name, std::move (dbfd)));
12391
12392 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12393 dwp_file->elf_sections =
12394 OBSTACK_CALLOC (&objfile->objfile_obstack,
12395 dwp_file->num_sections, asection *);
12396
12397 bfd_map_over_sections (dwp_file->dbfd.get (),
12398 dwarf2_locate_common_dwp_sections,
12399 dwp_file.get ());
12400
12401 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12402 0);
12403
12404 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12405 1);
12406
12407 /* The DWP file version is stored in the hash table. Oh well. */
12408 if (dwp_file->cus && dwp_file->tus
12409 && dwp_file->cus->version != dwp_file->tus->version)
12410 {
12411 /* Technically speaking, we should try to limp along, but this is
12412 pretty bizarre. We use pulongest here because that's the established
12413 portability solution (e.g, we cannot use %u for uint32_t). */
12414 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12415 " TU version %s [in DWP file %s]"),
12416 pulongest (dwp_file->cus->version),
12417 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12418 }
12419
12420 if (dwp_file->cus)
12421 dwp_file->version = dwp_file->cus->version;
12422 else if (dwp_file->tus)
12423 dwp_file->version = dwp_file->tus->version;
12424 else
12425 dwp_file->version = 2;
12426
12427 if (dwp_file->version == 2)
12428 bfd_map_over_sections (dwp_file->dbfd.get (),
12429 dwarf2_locate_v2_dwp_sections,
12430 dwp_file.get ());
12431
12432 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12433 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
12434
12435 if (dwarf_read_debug)
12436 {
12437 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12438 fprintf_unfiltered (gdb_stdlog,
12439 " %s CUs, %s TUs\n",
12440 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12441 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12442 }
12443
12444 return dwp_file;
12445 }
12446
12447 /* Wrapper around open_and_init_dwp_file, only open it once. */
12448
12449 static struct dwp_file *
12450 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12451 {
12452 if (! dwarf2_per_objfile->dwp_checked)
12453 {
12454 dwarf2_per_objfile->dwp_file
12455 = open_and_init_dwp_file (dwarf2_per_objfile);
12456 dwarf2_per_objfile->dwp_checked = 1;
12457 }
12458 return dwarf2_per_objfile->dwp_file.get ();
12459 }
12460
12461 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12462 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12463 or in the DWP file for the objfile, referenced by THIS_UNIT.
12464 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12465 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12466
12467 This is called, for example, when wanting to read a variable with a
12468 complex location. Therefore we don't want to do file i/o for every call.
12469 Therefore we don't want to look for a DWO file on every call.
12470 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12471 then we check if we've already seen DWO_NAME, and only THEN do we check
12472 for a DWO file.
12473
12474 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12475 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12476
12477 static struct dwo_unit *
12478 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12479 const char *dwo_name, const char *comp_dir,
12480 ULONGEST signature, int is_debug_types)
12481 {
12482 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12483 struct objfile *objfile = dwarf2_per_objfile->objfile;
12484 const char *kind = is_debug_types ? "TU" : "CU";
12485 void **dwo_file_slot;
12486 struct dwo_file *dwo_file;
12487 struct dwp_file *dwp_file;
12488
12489 /* First see if there's a DWP file.
12490 If we have a DWP file but didn't find the DWO inside it, don't
12491 look for the original DWO file. It makes gdb behave differently
12492 depending on whether one is debugging in the build tree. */
12493
12494 dwp_file = get_dwp_file (dwarf2_per_objfile);
12495 if (dwp_file != NULL)
12496 {
12497 const struct dwp_hash_table *dwp_htab =
12498 is_debug_types ? dwp_file->tus : dwp_file->cus;
12499
12500 if (dwp_htab != NULL)
12501 {
12502 struct dwo_unit *dwo_cutu =
12503 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12504 signature, is_debug_types);
12505
12506 if (dwo_cutu != NULL)
12507 {
12508 if (dwarf_read_debug)
12509 {
12510 fprintf_unfiltered (gdb_stdlog,
12511 "Virtual DWO %s %s found: @%s\n",
12512 kind, hex_string (signature),
12513 host_address_to_string (dwo_cutu));
12514 }
12515 return dwo_cutu;
12516 }
12517 }
12518 }
12519 else
12520 {
12521 /* No DWP file, look for the DWO file. */
12522
12523 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12524 dwo_name, comp_dir);
12525 if (*dwo_file_slot == NULL)
12526 {
12527 /* Read in the file and build a table of the CUs/TUs it contains. */
12528 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12529 }
12530 /* NOTE: This will be NULL if unable to open the file. */
12531 dwo_file = (struct dwo_file *) *dwo_file_slot;
12532
12533 if (dwo_file != NULL)
12534 {
12535 struct dwo_unit *dwo_cutu = NULL;
12536
12537 if (is_debug_types && dwo_file->tus)
12538 {
12539 struct dwo_unit find_dwo_cutu;
12540
12541 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12542 find_dwo_cutu.signature = signature;
12543 dwo_cutu
12544 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12545 &find_dwo_cutu);
12546 }
12547 else if (!is_debug_types && dwo_file->cus)
12548 {
12549 struct dwo_unit find_dwo_cutu;
12550
12551 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12552 find_dwo_cutu.signature = signature;
12553 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12554 &find_dwo_cutu);
12555 }
12556
12557 if (dwo_cutu != NULL)
12558 {
12559 if (dwarf_read_debug)
12560 {
12561 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12562 kind, dwo_name, hex_string (signature),
12563 host_address_to_string (dwo_cutu));
12564 }
12565 return dwo_cutu;
12566 }
12567 }
12568 }
12569
12570 /* We didn't find it. This could mean a dwo_id mismatch, or
12571 someone deleted the DWO/DWP file, or the search path isn't set up
12572 correctly to find the file. */
12573
12574 if (dwarf_read_debug)
12575 {
12576 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12577 kind, dwo_name, hex_string (signature));
12578 }
12579
12580 /* This is a warning and not a complaint because it can be caused by
12581 pilot error (e.g., user accidentally deleting the DWO). */
12582 {
12583 /* Print the name of the DWP file if we looked there, helps the user
12584 better diagnose the problem. */
12585 std::string dwp_text;
12586
12587 if (dwp_file != NULL)
12588 dwp_text = string_printf (" [in DWP file %s]",
12589 lbasename (dwp_file->name));
12590
12591 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12592 " [in module %s]"),
12593 kind, dwo_name, hex_string (signature),
12594 dwp_text.c_str (),
12595 this_unit->is_debug_types ? "TU" : "CU",
12596 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12597 }
12598 return NULL;
12599 }
12600
12601 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12602 See lookup_dwo_cutu_unit for details. */
12603
12604 static struct dwo_unit *
12605 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12606 const char *dwo_name, const char *comp_dir,
12607 ULONGEST signature)
12608 {
12609 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12610 }
12611
12612 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12613 See lookup_dwo_cutu_unit for details. */
12614
12615 static struct dwo_unit *
12616 lookup_dwo_type_unit (struct signatured_type *this_tu,
12617 const char *dwo_name, const char *comp_dir)
12618 {
12619 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12620 }
12621
12622 /* Traversal function for queue_and_load_all_dwo_tus. */
12623
12624 static int
12625 queue_and_load_dwo_tu (void **slot, void *info)
12626 {
12627 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12628 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12629 ULONGEST signature = dwo_unit->signature;
12630 struct signatured_type *sig_type =
12631 lookup_dwo_signatured_type (per_cu->cu, signature);
12632
12633 if (sig_type != NULL)
12634 {
12635 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12636
12637 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12638 a real dependency of PER_CU on SIG_TYPE. That is detected later
12639 while processing PER_CU. */
12640 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12641 load_full_type_unit (sig_cu);
12642 per_cu->imported_symtabs_push (sig_cu);
12643 }
12644
12645 return 1;
12646 }
12647
12648 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12649 The DWO may have the only definition of the type, though it may not be
12650 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12651 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12652
12653 static void
12654 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12655 {
12656 struct dwo_unit *dwo_unit;
12657 struct dwo_file *dwo_file;
12658
12659 gdb_assert (!per_cu->is_debug_types);
12660 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12661 gdb_assert (per_cu->cu != NULL);
12662
12663 dwo_unit = per_cu->cu->dwo_unit;
12664 gdb_assert (dwo_unit != NULL);
12665
12666 dwo_file = dwo_unit->dwo_file;
12667 if (dwo_file->tus != NULL)
12668 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12669 per_cu);
12670 }
12671
12672 /* Read in various DIEs. */
12673
12674 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12675 Inherit only the children of the DW_AT_abstract_origin DIE not being
12676 already referenced by DW_AT_abstract_origin from the children of the
12677 current DIE. */
12678
12679 static void
12680 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12681 {
12682 struct die_info *child_die;
12683 sect_offset *offsetp;
12684 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12685 struct die_info *origin_die;
12686 /* Iterator of the ORIGIN_DIE children. */
12687 struct die_info *origin_child_die;
12688 struct attribute *attr;
12689 struct dwarf2_cu *origin_cu;
12690 struct pending **origin_previous_list_in_scope;
12691
12692 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12693 if (!attr)
12694 return;
12695
12696 /* Note that following die references may follow to a die in a
12697 different cu. */
12698
12699 origin_cu = cu;
12700 origin_die = follow_die_ref (die, attr, &origin_cu);
12701
12702 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12703 symbols in. */
12704 origin_previous_list_in_scope = origin_cu->list_in_scope;
12705 origin_cu->list_in_scope = cu->list_in_scope;
12706
12707 if (die->tag != origin_die->tag
12708 && !(die->tag == DW_TAG_inlined_subroutine
12709 && origin_die->tag == DW_TAG_subprogram))
12710 complaint (_("DIE %s and its abstract origin %s have different tags"),
12711 sect_offset_str (die->sect_off),
12712 sect_offset_str (origin_die->sect_off));
12713
12714 std::vector<sect_offset> offsets;
12715
12716 for (child_die = die->child;
12717 child_die && child_die->tag;
12718 child_die = sibling_die (child_die))
12719 {
12720 struct die_info *child_origin_die;
12721 struct dwarf2_cu *child_origin_cu;
12722
12723 /* We are trying to process concrete instance entries:
12724 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12725 it's not relevant to our analysis here. i.e. detecting DIEs that are
12726 present in the abstract instance but not referenced in the concrete
12727 one. */
12728 if (child_die->tag == DW_TAG_call_site
12729 || child_die->tag == DW_TAG_GNU_call_site)
12730 continue;
12731
12732 /* For each CHILD_DIE, find the corresponding child of
12733 ORIGIN_DIE. If there is more than one layer of
12734 DW_AT_abstract_origin, follow them all; there shouldn't be,
12735 but GCC versions at least through 4.4 generate this (GCC PR
12736 40573). */
12737 child_origin_die = child_die;
12738 child_origin_cu = cu;
12739 while (1)
12740 {
12741 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12742 child_origin_cu);
12743 if (attr == NULL)
12744 break;
12745 child_origin_die = follow_die_ref (child_origin_die, attr,
12746 &child_origin_cu);
12747 }
12748
12749 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12750 counterpart may exist. */
12751 if (child_origin_die != child_die)
12752 {
12753 if (child_die->tag != child_origin_die->tag
12754 && !(child_die->tag == DW_TAG_inlined_subroutine
12755 && child_origin_die->tag == DW_TAG_subprogram))
12756 complaint (_("Child DIE %s and its abstract origin %s have "
12757 "different tags"),
12758 sect_offset_str (child_die->sect_off),
12759 sect_offset_str (child_origin_die->sect_off));
12760 if (child_origin_die->parent != origin_die)
12761 complaint (_("Child DIE %s and its abstract origin %s have "
12762 "different parents"),
12763 sect_offset_str (child_die->sect_off),
12764 sect_offset_str (child_origin_die->sect_off));
12765 else
12766 offsets.push_back (child_origin_die->sect_off);
12767 }
12768 }
12769 std::sort (offsets.begin (), offsets.end ());
12770 sect_offset *offsets_end = offsets.data () + offsets.size ();
12771 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12772 if (offsetp[-1] == *offsetp)
12773 complaint (_("Multiple children of DIE %s refer "
12774 "to DIE %s as their abstract origin"),
12775 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12776
12777 offsetp = offsets.data ();
12778 origin_child_die = origin_die->child;
12779 while (origin_child_die && origin_child_die->tag)
12780 {
12781 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12782 while (offsetp < offsets_end
12783 && *offsetp < origin_child_die->sect_off)
12784 offsetp++;
12785 if (offsetp >= offsets_end
12786 || *offsetp > origin_child_die->sect_off)
12787 {
12788 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12789 Check whether we're already processing ORIGIN_CHILD_DIE.
12790 This can happen with mutually referenced abstract_origins.
12791 PR 16581. */
12792 if (!origin_child_die->in_process)
12793 process_die (origin_child_die, origin_cu);
12794 }
12795 origin_child_die = sibling_die (origin_child_die);
12796 }
12797 origin_cu->list_in_scope = origin_previous_list_in_scope;
12798
12799 if (cu != origin_cu)
12800 compute_delayed_physnames (origin_cu);
12801 }
12802
12803 static void
12804 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12805 {
12806 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12807 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12808 struct context_stack *newobj;
12809 CORE_ADDR lowpc;
12810 CORE_ADDR highpc;
12811 struct die_info *child_die;
12812 struct attribute *attr, *call_line, *call_file;
12813 const char *name;
12814 CORE_ADDR baseaddr;
12815 struct block *block;
12816 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12817 std::vector<struct symbol *> template_args;
12818 struct template_symbol *templ_func = NULL;
12819
12820 if (inlined_func)
12821 {
12822 /* If we do not have call site information, we can't show the
12823 caller of this inlined function. That's too confusing, so
12824 only use the scope for local variables. */
12825 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12826 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12827 if (call_line == NULL || call_file == NULL)
12828 {
12829 read_lexical_block_scope (die, cu);
12830 return;
12831 }
12832 }
12833
12834 baseaddr = objfile->text_section_offset ();
12835
12836 name = dwarf2_name (die, cu);
12837
12838 /* Ignore functions with missing or empty names. These are actually
12839 illegal according to the DWARF standard. */
12840 if (name == NULL)
12841 {
12842 complaint (_("missing name for subprogram DIE at %s"),
12843 sect_offset_str (die->sect_off));
12844 return;
12845 }
12846
12847 /* Ignore functions with missing or invalid low and high pc attributes. */
12848 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12849 <= PC_BOUNDS_INVALID)
12850 {
12851 attr = dwarf2_attr (die, DW_AT_external, cu);
12852 if (!attr || !DW_UNSND (attr))
12853 complaint (_("cannot get low and high bounds "
12854 "for subprogram DIE at %s"),
12855 sect_offset_str (die->sect_off));
12856 return;
12857 }
12858
12859 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12860 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12861
12862 /* If we have any template arguments, then we must allocate a
12863 different sort of symbol. */
12864 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12865 {
12866 if (child_die->tag == DW_TAG_template_type_param
12867 || child_die->tag == DW_TAG_template_value_param)
12868 {
12869 templ_func = allocate_template_symbol (objfile);
12870 templ_func->subclass = SYMBOL_TEMPLATE;
12871 break;
12872 }
12873 }
12874
12875 newobj = cu->get_builder ()->push_context (0, lowpc);
12876 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12877 (struct symbol *) templ_func);
12878
12879 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12880 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12881 cu->language);
12882
12883 /* If there is a location expression for DW_AT_frame_base, record
12884 it. */
12885 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12886 if (attr != nullptr)
12887 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12888
12889 /* If there is a location for the static link, record it. */
12890 newobj->static_link = NULL;
12891 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12892 if (attr != nullptr)
12893 {
12894 newobj->static_link
12895 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12896 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12897 cu->per_cu->addr_type ());
12898 }
12899
12900 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12901
12902 if (die->child != NULL)
12903 {
12904 child_die = die->child;
12905 while (child_die && child_die->tag)
12906 {
12907 if (child_die->tag == DW_TAG_template_type_param
12908 || child_die->tag == DW_TAG_template_value_param)
12909 {
12910 struct symbol *arg = new_symbol (child_die, NULL, cu);
12911
12912 if (arg != NULL)
12913 template_args.push_back (arg);
12914 }
12915 else
12916 process_die (child_die, cu);
12917 child_die = sibling_die (child_die);
12918 }
12919 }
12920
12921 inherit_abstract_dies (die, cu);
12922
12923 /* If we have a DW_AT_specification, we might need to import using
12924 directives from the context of the specification DIE. See the
12925 comment in determine_prefix. */
12926 if (cu->language == language_cplus
12927 && dwarf2_attr (die, DW_AT_specification, cu))
12928 {
12929 struct dwarf2_cu *spec_cu = cu;
12930 struct die_info *spec_die = die_specification (die, &spec_cu);
12931
12932 while (spec_die)
12933 {
12934 child_die = spec_die->child;
12935 while (child_die && child_die->tag)
12936 {
12937 if (child_die->tag == DW_TAG_imported_module)
12938 process_die (child_die, spec_cu);
12939 child_die = sibling_die (child_die);
12940 }
12941
12942 /* In some cases, GCC generates specification DIEs that
12943 themselves contain DW_AT_specification attributes. */
12944 spec_die = die_specification (spec_die, &spec_cu);
12945 }
12946 }
12947
12948 struct context_stack cstk = cu->get_builder ()->pop_context ();
12949 /* Make a block for the local symbols within. */
12950 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12951 cstk.static_link, lowpc, highpc);
12952
12953 /* For C++, set the block's scope. */
12954 if ((cu->language == language_cplus
12955 || cu->language == language_fortran
12956 || cu->language == language_d
12957 || cu->language == language_rust)
12958 && cu->processing_has_namespace_info)
12959 block_set_scope (block, determine_prefix (die, cu),
12960 &objfile->objfile_obstack);
12961
12962 /* If we have address ranges, record them. */
12963 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12964
12965 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12966
12967 /* Attach template arguments to function. */
12968 if (!template_args.empty ())
12969 {
12970 gdb_assert (templ_func != NULL);
12971
12972 templ_func->n_template_arguments = template_args.size ();
12973 templ_func->template_arguments
12974 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12975 templ_func->n_template_arguments);
12976 memcpy (templ_func->template_arguments,
12977 template_args.data (),
12978 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12979
12980 /* Make sure that the symtab is set on the new symbols. Even
12981 though they don't appear in this symtab directly, other parts
12982 of gdb assume that symbols do, and this is reasonably
12983 true. */
12984 for (symbol *sym : template_args)
12985 symbol_set_symtab (sym, symbol_symtab (templ_func));
12986 }
12987
12988 /* In C++, we can have functions nested inside functions (e.g., when
12989 a function declares a class that has methods). This means that
12990 when we finish processing a function scope, we may need to go
12991 back to building a containing block's symbol lists. */
12992 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12993 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12994
12995 /* If we've finished processing a top-level function, subsequent
12996 symbols go in the file symbol list. */
12997 if (cu->get_builder ()->outermost_context_p ())
12998 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12999 }
13000
13001 /* Process all the DIES contained within a lexical block scope. Start
13002 a new scope, process the dies, and then close the scope. */
13003
13004 static void
13005 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13006 {
13007 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13008 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13009 CORE_ADDR lowpc, highpc;
13010 struct die_info *child_die;
13011 CORE_ADDR baseaddr;
13012
13013 baseaddr = objfile->text_section_offset ();
13014
13015 /* Ignore blocks with missing or invalid low and high pc attributes. */
13016 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13017 as multiple lexical blocks? Handling children in a sane way would
13018 be nasty. Might be easier to properly extend generic blocks to
13019 describe ranges. */
13020 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13021 {
13022 case PC_BOUNDS_NOT_PRESENT:
13023 /* DW_TAG_lexical_block has no attributes, process its children as if
13024 there was no wrapping by that DW_TAG_lexical_block.
13025 GCC does no longer produces such DWARF since GCC r224161. */
13026 for (child_die = die->child;
13027 child_die != NULL && child_die->tag;
13028 child_die = sibling_die (child_die))
13029 process_die (child_die, cu);
13030 return;
13031 case PC_BOUNDS_INVALID:
13032 return;
13033 }
13034 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13035 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13036
13037 cu->get_builder ()->push_context (0, lowpc);
13038 if (die->child != NULL)
13039 {
13040 child_die = die->child;
13041 while (child_die && child_die->tag)
13042 {
13043 process_die (child_die, cu);
13044 child_die = sibling_die (child_die);
13045 }
13046 }
13047 inherit_abstract_dies (die, cu);
13048 struct context_stack cstk = cu->get_builder ()->pop_context ();
13049
13050 if (*cu->get_builder ()->get_local_symbols () != NULL
13051 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13052 {
13053 struct block *block
13054 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13055 cstk.start_addr, highpc);
13056
13057 /* Note that recording ranges after traversing children, as we
13058 do here, means that recording a parent's ranges entails
13059 walking across all its children's ranges as they appear in
13060 the address map, which is quadratic behavior.
13061
13062 It would be nicer to record the parent's ranges before
13063 traversing its children, simply overriding whatever you find
13064 there. But since we don't even decide whether to create a
13065 block until after we've traversed its children, that's hard
13066 to do. */
13067 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13068 }
13069 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13070 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13071 }
13072
13073 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13074
13075 static void
13076 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13077 {
13078 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13079 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13080 CORE_ADDR pc, baseaddr;
13081 struct attribute *attr;
13082 struct call_site *call_site, call_site_local;
13083 void **slot;
13084 int nparams;
13085 struct die_info *child_die;
13086
13087 baseaddr = objfile->text_section_offset ();
13088
13089 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13090 if (attr == NULL)
13091 {
13092 /* This was a pre-DWARF-5 GNU extension alias
13093 for DW_AT_call_return_pc. */
13094 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13095 }
13096 if (!attr)
13097 {
13098 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13099 "DIE %s [in module %s]"),
13100 sect_offset_str (die->sect_off), objfile_name (objfile));
13101 return;
13102 }
13103 pc = attr->value_as_address () + baseaddr;
13104 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13105
13106 if (cu->call_site_htab == NULL)
13107 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13108 NULL, &objfile->objfile_obstack,
13109 hashtab_obstack_allocate, NULL);
13110 call_site_local.pc = pc;
13111 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13112 if (*slot != NULL)
13113 {
13114 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13115 "DIE %s [in module %s]"),
13116 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13117 objfile_name (objfile));
13118 return;
13119 }
13120
13121 /* Count parameters at the caller. */
13122
13123 nparams = 0;
13124 for (child_die = die->child; child_die && child_die->tag;
13125 child_die = sibling_die (child_die))
13126 {
13127 if (child_die->tag != DW_TAG_call_site_parameter
13128 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13129 {
13130 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13131 "DW_TAG_call_site child DIE %s [in module %s]"),
13132 child_die->tag, sect_offset_str (child_die->sect_off),
13133 objfile_name (objfile));
13134 continue;
13135 }
13136
13137 nparams++;
13138 }
13139
13140 call_site
13141 = ((struct call_site *)
13142 obstack_alloc (&objfile->objfile_obstack,
13143 sizeof (*call_site)
13144 + (sizeof (*call_site->parameter) * (nparams - 1))));
13145 *slot = call_site;
13146 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13147 call_site->pc = pc;
13148
13149 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13150 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13151 {
13152 struct die_info *func_die;
13153
13154 /* Skip also over DW_TAG_inlined_subroutine. */
13155 for (func_die = die->parent;
13156 func_die && func_die->tag != DW_TAG_subprogram
13157 && func_die->tag != DW_TAG_subroutine_type;
13158 func_die = func_die->parent);
13159
13160 /* DW_AT_call_all_calls is a superset
13161 of DW_AT_call_all_tail_calls. */
13162 if (func_die
13163 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13164 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13165 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13166 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13167 {
13168 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13169 not complete. But keep CALL_SITE for look ups via call_site_htab,
13170 both the initial caller containing the real return address PC and
13171 the final callee containing the current PC of a chain of tail
13172 calls do not need to have the tail call list complete. But any
13173 function candidate for a virtual tail call frame searched via
13174 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13175 determined unambiguously. */
13176 }
13177 else
13178 {
13179 struct type *func_type = NULL;
13180
13181 if (func_die)
13182 func_type = get_die_type (func_die, cu);
13183 if (func_type != NULL)
13184 {
13185 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13186
13187 /* Enlist this call site to the function. */
13188 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13189 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13190 }
13191 else
13192 complaint (_("Cannot find function owning DW_TAG_call_site "
13193 "DIE %s [in module %s]"),
13194 sect_offset_str (die->sect_off), objfile_name (objfile));
13195 }
13196 }
13197
13198 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13199 if (attr == NULL)
13200 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13201 if (attr == NULL)
13202 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13203 if (attr == NULL)
13204 {
13205 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13206 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13207 }
13208 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13209 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13210 /* Keep NULL DWARF_BLOCK. */;
13211 else if (attr->form_is_block ())
13212 {
13213 struct dwarf2_locexpr_baton *dlbaton;
13214
13215 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13216 dlbaton->data = DW_BLOCK (attr)->data;
13217 dlbaton->size = DW_BLOCK (attr)->size;
13218 dlbaton->per_cu = cu->per_cu;
13219
13220 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13221 }
13222 else if (attr->form_is_ref ())
13223 {
13224 struct dwarf2_cu *target_cu = cu;
13225 struct die_info *target_die;
13226
13227 target_die = follow_die_ref (die, attr, &target_cu);
13228 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13229 if (die_is_declaration (target_die, target_cu))
13230 {
13231 const char *target_physname;
13232
13233 /* Prefer the mangled name; otherwise compute the demangled one. */
13234 target_physname = dw2_linkage_name (target_die, target_cu);
13235 if (target_physname == NULL)
13236 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13237 if (target_physname == NULL)
13238 complaint (_("DW_AT_call_target target DIE has invalid "
13239 "physname, for referencing DIE %s [in module %s]"),
13240 sect_offset_str (die->sect_off), objfile_name (objfile));
13241 else
13242 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13243 }
13244 else
13245 {
13246 CORE_ADDR lowpc;
13247
13248 /* DW_AT_entry_pc should be preferred. */
13249 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13250 <= PC_BOUNDS_INVALID)
13251 complaint (_("DW_AT_call_target target DIE has invalid "
13252 "low pc, for referencing DIE %s [in module %s]"),
13253 sect_offset_str (die->sect_off), objfile_name (objfile));
13254 else
13255 {
13256 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13257 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13258 }
13259 }
13260 }
13261 else
13262 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13263 "block nor reference, for DIE %s [in module %s]"),
13264 sect_offset_str (die->sect_off), objfile_name (objfile));
13265
13266 call_site->per_cu = cu->per_cu;
13267
13268 for (child_die = die->child;
13269 child_die && child_die->tag;
13270 child_die = sibling_die (child_die))
13271 {
13272 struct call_site_parameter *parameter;
13273 struct attribute *loc, *origin;
13274
13275 if (child_die->tag != DW_TAG_call_site_parameter
13276 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13277 {
13278 /* Already printed the complaint above. */
13279 continue;
13280 }
13281
13282 gdb_assert (call_site->parameter_count < nparams);
13283 parameter = &call_site->parameter[call_site->parameter_count];
13284
13285 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13286 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13287 register is contained in DW_AT_call_value. */
13288
13289 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13290 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13291 if (origin == NULL)
13292 {
13293 /* This was a pre-DWARF-5 GNU extension alias
13294 for DW_AT_call_parameter. */
13295 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13296 }
13297 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13298 {
13299 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13300
13301 sect_offset sect_off
13302 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13303 if (!cu->header.offset_in_cu_p (sect_off))
13304 {
13305 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13306 binding can be done only inside one CU. Such referenced DIE
13307 therefore cannot be even moved to DW_TAG_partial_unit. */
13308 complaint (_("DW_AT_call_parameter offset is not in CU for "
13309 "DW_TAG_call_site child DIE %s [in module %s]"),
13310 sect_offset_str (child_die->sect_off),
13311 objfile_name (objfile));
13312 continue;
13313 }
13314 parameter->u.param_cu_off
13315 = (cu_offset) (sect_off - cu->header.sect_off);
13316 }
13317 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13318 {
13319 complaint (_("No DW_FORM_block* DW_AT_location for "
13320 "DW_TAG_call_site child DIE %s [in module %s]"),
13321 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13322 continue;
13323 }
13324 else
13325 {
13326 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13327 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13328 if (parameter->u.dwarf_reg != -1)
13329 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13330 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13331 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13332 &parameter->u.fb_offset))
13333 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13334 else
13335 {
13336 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13337 "for DW_FORM_block* DW_AT_location is supported for "
13338 "DW_TAG_call_site child DIE %s "
13339 "[in module %s]"),
13340 sect_offset_str (child_die->sect_off),
13341 objfile_name (objfile));
13342 continue;
13343 }
13344 }
13345
13346 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13347 if (attr == NULL)
13348 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13349 if (attr == NULL || !attr->form_is_block ())
13350 {
13351 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13352 "DW_TAG_call_site child DIE %s [in module %s]"),
13353 sect_offset_str (child_die->sect_off),
13354 objfile_name (objfile));
13355 continue;
13356 }
13357 parameter->value = DW_BLOCK (attr)->data;
13358 parameter->value_size = DW_BLOCK (attr)->size;
13359
13360 /* Parameters are not pre-cleared by memset above. */
13361 parameter->data_value = NULL;
13362 parameter->data_value_size = 0;
13363 call_site->parameter_count++;
13364
13365 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13366 if (attr == NULL)
13367 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13368 if (attr != nullptr)
13369 {
13370 if (!attr->form_is_block ())
13371 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13372 "DW_TAG_call_site child DIE %s [in module %s]"),
13373 sect_offset_str (child_die->sect_off),
13374 objfile_name (objfile));
13375 else
13376 {
13377 parameter->data_value = DW_BLOCK (attr)->data;
13378 parameter->data_value_size = DW_BLOCK (attr)->size;
13379 }
13380 }
13381 }
13382 }
13383
13384 /* Helper function for read_variable. If DIE represents a virtual
13385 table, then return the type of the concrete object that is
13386 associated with the virtual table. Otherwise, return NULL. */
13387
13388 static struct type *
13389 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13390 {
13391 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13392 if (attr == NULL)
13393 return NULL;
13394
13395 /* Find the type DIE. */
13396 struct die_info *type_die = NULL;
13397 struct dwarf2_cu *type_cu = cu;
13398
13399 if (attr->form_is_ref ())
13400 type_die = follow_die_ref (die, attr, &type_cu);
13401 if (type_die == NULL)
13402 return NULL;
13403
13404 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13405 return NULL;
13406 return die_containing_type (type_die, type_cu);
13407 }
13408
13409 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13410
13411 static void
13412 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13413 {
13414 struct rust_vtable_symbol *storage = NULL;
13415
13416 if (cu->language == language_rust)
13417 {
13418 struct type *containing_type = rust_containing_type (die, cu);
13419
13420 if (containing_type != NULL)
13421 {
13422 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13423
13424 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13425 initialize_objfile_symbol (storage);
13426 storage->concrete_type = containing_type;
13427 storage->subclass = SYMBOL_RUST_VTABLE;
13428 }
13429 }
13430
13431 struct symbol *res = new_symbol (die, NULL, cu, storage);
13432 struct attribute *abstract_origin
13433 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13434 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13435 if (res == NULL && loc && abstract_origin)
13436 {
13437 /* We have a variable without a name, but with a location and an abstract
13438 origin. This may be a concrete instance of an abstract variable
13439 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13440 later. */
13441 struct dwarf2_cu *origin_cu = cu;
13442 struct die_info *origin_die
13443 = follow_die_ref (die, abstract_origin, &origin_cu);
13444 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13445 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13446 }
13447 }
13448
13449 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13450 reading .debug_rnglists.
13451 Callback's type should be:
13452 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13453 Return true if the attributes are present and valid, otherwise,
13454 return false. */
13455
13456 template <typename Callback>
13457 static bool
13458 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13459 Callback &&callback)
13460 {
13461 struct dwarf2_per_objfile *dwarf2_per_objfile
13462 = cu->per_cu->dwarf2_per_objfile;
13463 struct objfile *objfile = dwarf2_per_objfile->objfile;
13464 bfd *obfd = objfile->obfd;
13465 /* Base address selection entry. */
13466 CORE_ADDR base;
13467 int found_base;
13468 const gdb_byte *buffer;
13469 CORE_ADDR baseaddr;
13470 bool overflow = false;
13471
13472 found_base = cu->base_known;
13473 base = cu->base_address;
13474
13475 dwarf2_per_objfile->rnglists.read (objfile);
13476 if (offset >= dwarf2_per_objfile->rnglists.size)
13477 {
13478 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13479 offset);
13480 return false;
13481 }
13482 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13483
13484 baseaddr = objfile->text_section_offset ();
13485
13486 while (1)
13487 {
13488 /* Initialize it due to a false compiler warning. */
13489 CORE_ADDR range_beginning = 0, range_end = 0;
13490 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13491 + dwarf2_per_objfile->rnglists.size);
13492 unsigned int bytes_read;
13493
13494 if (buffer == buf_end)
13495 {
13496 overflow = true;
13497 break;
13498 }
13499 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13500 switch (rlet)
13501 {
13502 case DW_RLE_end_of_list:
13503 break;
13504 case DW_RLE_base_address:
13505 if (buffer + cu->header.addr_size > buf_end)
13506 {
13507 overflow = true;
13508 break;
13509 }
13510 base = cu->header.read_address (obfd, buffer, &bytes_read);
13511 found_base = 1;
13512 buffer += bytes_read;
13513 break;
13514 case DW_RLE_start_length:
13515 if (buffer + cu->header.addr_size > buf_end)
13516 {
13517 overflow = true;
13518 break;
13519 }
13520 range_beginning = cu->header.read_address (obfd, buffer,
13521 &bytes_read);
13522 buffer += bytes_read;
13523 range_end = (range_beginning
13524 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13525 buffer += bytes_read;
13526 if (buffer > buf_end)
13527 {
13528 overflow = true;
13529 break;
13530 }
13531 break;
13532 case DW_RLE_offset_pair:
13533 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13534 buffer += bytes_read;
13535 if (buffer > buf_end)
13536 {
13537 overflow = true;
13538 break;
13539 }
13540 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13541 buffer += bytes_read;
13542 if (buffer > buf_end)
13543 {
13544 overflow = true;
13545 break;
13546 }
13547 break;
13548 case DW_RLE_start_end:
13549 if (buffer + 2 * cu->header.addr_size > buf_end)
13550 {
13551 overflow = true;
13552 break;
13553 }
13554 range_beginning = cu->header.read_address (obfd, buffer,
13555 &bytes_read);
13556 buffer += bytes_read;
13557 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13558 buffer += bytes_read;
13559 break;
13560 default:
13561 complaint (_("Invalid .debug_rnglists data (no base address)"));
13562 return false;
13563 }
13564 if (rlet == DW_RLE_end_of_list || overflow)
13565 break;
13566 if (rlet == DW_RLE_base_address)
13567 continue;
13568
13569 if (!found_base)
13570 {
13571 /* We have no valid base address for the ranges
13572 data. */
13573 complaint (_("Invalid .debug_rnglists data (no base address)"));
13574 return false;
13575 }
13576
13577 if (range_beginning > range_end)
13578 {
13579 /* Inverted range entries are invalid. */
13580 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13581 return false;
13582 }
13583
13584 /* Empty range entries have no effect. */
13585 if (range_beginning == range_end)
13586 continue;
13587
13588 range_beginning += base;
13589 range_end += base;
13590
13591 /* A not-uncommon case of bad debug info.
13592 Don't pollute the addrmap with bad data. */
13593 if (range_beginning + baseaddr == 0
13594 && !dwarf2_per_objfile->has_section_at_zero)
13595 {
13596 complaint (_(".debug_rnglists entry has start address of zero"
13597 " [in module %s]"), objfile_name (objfile));
13598 continue;
13599 }
13600
13601 callback (range_beginning, range_end);
13602 }
13603
13604 if (overflow)
13605 {
13606 complaint (_("Offset %d is not terminated "
13607 "for DW_AT_ranges attribute"),
13608 offset);
13609 return false;
13610 }
13611
13612 return true;
13613 }
13614
13615 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13616 Callback's type should be:
13617 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13618 Return 1 if the attributes are present and valid, otherwise, return 0. */
13619
13620 template <typename Callback>
13621 static int
13622 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13623 Callback &&callback)
13624 {
13625 struct dwarf2_per_objfile *dwarf2_per_objfile
13626 = cu->per_cu->dwarf2_per_objfile;
13627 struct objfile *objfile = dwarf2_per_objfile->objfile;
13628 struct comp_unit_head *cu_header = &cu->header;
13629 bfd *obfd = objfile->obfd;
13630 unsigned int addr_size = cu_header->addr_size;
13631 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13632 /* Base address selection entry. */
13633 CORE_ADDR base;
13634 int found_base;
13635 unsigned int dummy;
13636 const gdb_byte *buffer;
13637 CORE_ADDR baseaddr;
13638
13639 if (cu_header->version >= 5)
13640 return dwarf2_rnglists_process (offset, cu, callback);
13641
13642 found_base = cu->base_known;
13643 base = cu->base_address;
13644
13645 dwarf2_per_objfile->ranges.read (objfile);
13646 if (offset >= dwarf2_per_objfile->ranges.size)
13647 {
13648 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13649 offset);
13650 return 0;
13651 }
13652 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13653
13654 baseaddr = objfile->text_section_offset ();
13655
13656 while (1)
13657 {
13658 CORE_ADDR range_beginning, range_end;
13659
13660 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13661 buffer += addr_size;
13662 range_end = cu->header.read_address (obfd, buffer, &dummy);
13663 buffer += addr_size;
13664 offset += 2 * addr_size;
13665
13666 /* An end of list marker is a pair of zero addresses. */
13667 if (range_beginning == 0 && range_end == 0)
13668 /* Found the end of list entry. */
13669 break;
13670
13671 /* Each base address selection entry is a pair of 2 values.
13672 The first is the largest possible address, the second is
13673 the base address. Check for a base address here. */
13674 if ((range_beginning & mask) == mask)
13675 {
13676 /* If we found the largest possible address, then we already
13677 have the base address in range_end. */
13678 base = range_end;
13679 found_base = 1;
13680 continue;
13681 }
13682
13683 if (!found_base)
13684 {
13685 /* We have no valid base address for the ranges
13686 data. */
13687 complaint (_("Invalid .debug_ranges data (no base address)"));
13688 return 0;
13689 }
13690
13691 if (range_beginning > range_end)
13692 {
13693 /* Inverted range entries are invalid. */
13694 complaint (_("Invalid .debug_ranges data (inverted range)"));
13695 return 0;
13696 }
13697
13698 /* Empty range entries have no effect. */
13699 if (range_beginning == range_end)
13700 continue;
13701
13702 range_beginning += base;
13703 range_end += base;
13704
13705 /* A not-uncommon case of bad debug info.
13706 Don't pollute the addrmap with bad data. */
13707 if (range_beginning + baseaddr == 0
13708 && !dwarf2_per_objfile->has_section_at_zero)
13709 {
13710 complaint (_(".debug_ranges entry has start address of zero"
13711 " [in module %s]"), objfile_name (objfile));
13712 continue;
13713 }
13714
13715 callback (range_beginning, range_end);
13716 }
13717
13718 return 1;
13719 }
13720
13721 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13722 Return 1 if the attributes are present and valid, otherwise, return 0.
13723 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13724
13725 static int
13726 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13727 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13728 dwarf2_psymtab *ranges_pst)
13729 {
13730 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13731 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13732 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13733 int low_set = 0;
13734 CORE_ADDR low = 0;
13735 CORE_ADDR high = 0;
13736 int retval;
13737
13738 retval = dwarf2_ranges_process (offset, cu,
13739 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13740 {
13741 if (ranges_pst != NULL)
13742 {
13743 CORE_ADDR lowpc;
13744 CORE_ADDR highpc;
13745
13746 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13747 range_beginning + baseaddr)
13748 - baseaddr);
13749 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13750 range_end + baseaddr)
13751 - baseaddr);
13752 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13753 lowpc, highpc - 1, ranges_pst);
13754 }
13755
13756 /* FIXME: This is recording everything as a low-high
13757 segment of consecutive addresses. We should have a
13758 data structure for discontiguous block ranges
13759 instead. */
13760 if (! low_set)
13761 {
13762 low = range_beginning;
13763 high = range_end;
13764 low_set = 1;
13765 }
13766 else
13767 {
13768 if (range_beginning < low)
13769 low = range_beginning;
13770 if (range_end > high)
13771 high = range_end;
13772 }
13773 });
13774 if (!retval)
13775 return 0;
13776
13777 if (! low_set)
13778 /* If the first entry is an end-of-list marker, the range
13779 describes an empty scope, i.e. no instructions. */
13780 return 0;
13781
13782 if (low_return)
13783 *low_return = low;
13784 if (high_return)
13785 *high_return = high;
13786 return 1;
13787 }
13788
13789 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13790 definition for the return value. *LOWPC and *HIGHPC are set iff
13791 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13792
13793 static enum pc_bounds_kind
13794 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13795 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13796 dwarf2_psymtab *pst)
13797 {
13798 struct dwarf2_per_objfile *dwarf2_per_objfile
13799 = cu->per_cu->dwarf2_per_objfile;
13800 struct attribute *attr;
13801 struct attribute *attr_high;
13802 CORE_ADDR low = 0;
13803 CORE_ADDR high = 0;
13804 enum pc_bounds_kind ret;
13805
13806 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13807 if (attr_high)
13808 {
13809 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13810 if (attr != nullptr)
13811 {
13812 low = attr->value_as_address ();
13813 high = attr_high->value_as_address ();
13814 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13815 high += low;
13816 }
13817 else
13818 /* Found high w/o low attribute. */
13819 return PC_BOUNDS_INVALID;
13820
13821 /* Found consecutive range of addresses. */
13822 ret = PC_BOUNDS_HIGH_LOW;
13823 }
13824 else
13825 {
13826 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13827 if (attr != NULL)
13828 {
13829 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13830 We take advantage of the fact that DW_AT_ranges does not appear
13831 in DW_TAG_compile_unit of DWO files. */
13832 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13833 unsigned int ranges_offset = (DW_UNSND (attr)
13834 + (need_ranges_base
13835 ? cu->ranges_base
13836 : 0));
13837
13838 /* Value of the DW_AT_ranges attribute is the offset in the
13839 .debug_ranges section. */
13840 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13841 return PC_BOUNDS_INVALID;
13842 /* Found discontinuous range of addresses. */
13843 ret = PC_BOUNDS_RANGES;
13844 }
13845 else
13846 return PC_BOUNDS_NOT_PRESENT;
13847 }
13848
13849 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13850 if (high <= low)
13851 return PC_BOUNDS_INVALID;
13852
13853 /* When using the GNU linker, .gnu.linkonce. sections are used to
13854 eliminate duplicate copies of functions and vtables and such.
13855 The linker will arbitrarily choose one and discard the others.
13856 The AT_*_pc values for such functions refer to local labels in
13857 these sections. If the section from that file was discarded, the
13858 labels are not in the output, so the relocs get a value of 0.
13859 If this is a discarded function, mark the pc bounds as invalid,
13860 so that GDB will ignore it. */
13861 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13862 return PC_BOUNDS_INVALID;
13863
13864 *lowpc = low;
13865 if (highpc)
13866 *highpc = high;
13867 return ret;
13868 }
13869
13870 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13871 its low and high PC addresses. Do nothing if these addresses could not
13872 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13873 and HIGHPC to the high address if greater than HIGHPC. */
13874
13875 static void
13876 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13877 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13878 struct dwarf2_cu *cu)
13879 {
13880 CORE_ADDR low, high;
13881 struct die_info *child = die->child;
13882
13883 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13884 {
13885 *lowpc = std::min (*lowpc, low);
13886 *highpc = std::max (*highpc, high);
13887 }
13888
13889 /* If the language does not allow nested subprograms (either inside
13890 subprograms or lexical blocks), we're done. */
13891 if (cu->language != language_ada)
13892 return;
13893
13894 /* Check all the children of the given DIE. If it contains nested
13895 subprograms, then check their pc bounds. Likewise, we need to
13896 check lexical blocks as well, as they may also contain subprogram
13897 definitions. */
13898 while (child && child->tag)
13899 {
13900 if (child->tag == DW_TAG_subprogram
13901 || child->tag == DW_TAG_lexical_block)
13902 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13903 child = sibling_die (child);
13904 }
13905 }
13906
13907 /* Get the low and high pc's represented by the scope DIE, and store
13908 them in *LOWPC and *HIGHPC. If the correct values can't be
13909 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13910
13911 static void
13912 get_scope_pc_bounds (struct die_info *die,
13913 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13914 struct dwarf2_cu *cu)
13915 {
13916 CORE_ADDR best_low = (CORE_ADDR) -1;
13917 CORE_ADDR best_high = (CORE_ADDR) 0;
13918 CORE_ADDR current_low, current_high;
13919
13920 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13921 >= PC_BOUNDS_RANGES)
13922 {
13923 best_low = current_low;
13924 best_high = current_high;
13925 }
13926 else
13927 {
13928 struct die_info *child = die->child;
13929
13930 while (child && child->tag)
13931 {
13932 switch (child->tag) {
13933 case DW_TAG_subprogram:
13934 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13935 break;
13936 case DW_TAG_namespace:
13937 case DW_TAG_module:
13938 /* FIXME: carlton/2004-01-16: Should we do this for
13939 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13940 that current GCC's always emit the DIEs corresponding
13941 to definitions of methods of classes as children of a
13942 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13943 the DIEs giving the declarations, which could be
13944 anywhere). But I don't see any reason why the
13945 standards says that they have to be there. */
13946 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13947
13948 if (current_low != ((CORE_ADDR) -1))
13949 {
13950 best_low = std::min (best_low, current_low);
13951 best_high = std::max (best_high, current_high);
13952 }
13953 break;
13954 default:
13955 /* Ignore. */
13956 break;
13957 }
13958
13959 child = sibling_die (child);
13960 }
13961 }
13962
13963 *lowpc = best_low;
13964 *highpc = best_high;
13965 }
13966
13967 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13968 in DIE. */
13969
13970 static void
13971 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13972 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13973 {
13974 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13975 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13976 struct attribute *attr;
13977 struct attribute *attr_high;
13978
13979 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13980 if (attr_high)
13981 {
13982 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13983 if (attr != nullptr)
13984 {
13985 CORE_ADDR low = attr->value_as_address ();
13986 CORE_ADDR high = attr_high->value_as_address ();
13987
13988 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13989 high += low;
13990
13991 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13992 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13993 cu->get_builder ()->record_block_range (block, low, high - 1);
13994 }
13995 }
13996
13997 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13998 if (attr != nullptr)
13999 {
14000 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14001 We take advantage of the fact that DW_AT_ranges does not appear
14002 in DW_TAG_compile_unit of DWO files. */
14003 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14004
14005 /* The value of the DW_AT_ranges attribute is the offset of the
14006 address range list in the .debug_ranges section. */
14007 unsigned long offset = (DW_UNSND (attr)
14008 + (need_ranges_base ? cu->ranges_base : 0));
14009
14010 std::vector<blockrange> blockvec;
14011 dwarf2_ranges_process (offset, cu,
14012 [&] (CORE_ADDR start, CORE_ADDR end)
14013 {
14014 start += baseaddr;
14015 end += baseaddr;
14016 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14017 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14018 cu->get_builder ()->record_block_range (block, start, end - 1);
14019 blockvec.emplace_back (start, end);
14020 });
14021
14022 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14023 }
14024 }
14025
14026 /* Check whether the producer field indicates either of GCC < 4.6, or the
14027 Intel C/C++ compiler, and cache the result in CU. */
14028
14029 static void
14030 check_producer (struct dwarf2_cu *cu)
14031 {
14032 int major, minor;
14033
14034 if (cu->producer == NULL)
14035 {
14036 /* For unknown compilers expect their behavior is DWARF version
14037 compliant.
14038
14039 GCC started to support .debug_types sections by -gdwarf-4 since
14040 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14041 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14042 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14043 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14044 }
14045 else if (producer_is_gcc (cu->producer, &major, &minor))
14046 {
14047 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14048 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14049 }
14050 else if (producer_is_icc (cu->producer, &major, &minor))
14051 {
14052 cu->producer_is_icc = true;
14053 cu->producer_is_icc_lt_14 = major < 14;
14054 }
14055 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14056 cu->producer_is_codewarrior = true;
14057 else
14058 {
14059 /* For other non-GCC compilers, expect their behavior is DWARF version
14060 compliant. */
14061 }
14062
14063 cu->checked_producer = true;
14064 }
14065
14066 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14067 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14068 during 4.6.0 experimental. */
14069
14070 static bool
14071 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14072 {
14073 if (!cu->checked_producer)
14074 check_producer (cu);
14075
14076 return cu->producer_is_gxx_lt_4_6;
14077 }
14078
14079
14080 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14081 with incorrect is_stmt attributes. */
14082
14083 static bool
14084 producer_is_codewarrior (struct dwarf2_cu *cu)
14085 {
14086 if (!cu->checked_producer)
14087 check_producer (cu);
14088
14089 return cu->producer_is_codewarrior;
14090 }
14091
14092 /* Return the default accessibility type if it is not overridden by
14093 DW_AT_accessibility. */
14094
14095 static enum dwarf_access_attribute
14096 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14097 {
14098 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14099 {
14100 /* The default DWARF 2 accessibility for members is public, the default
14101 accessibility for inheritance is private. */
14102
14103 if (die->tag != DW_TAG_inheritance)
14104 return DW_ACCESS_public;
14105 else
14106 return DW_ACCESS_private;
14107 }
14108 else
14109 {
14110 /* DWARF 3+ defines the default accessibility a different way. The same
14111 rules apply now for DW_TAG_inheritance as for the members and it only
14112 depends on the container kind. */
14113
14114 if (die->parent->tag == DW_TAG_class_type)
14115 return DW_ACCESS_private;
14116 else
14117 return DW_ACCESS_public;
14118 }
14119 }
14120
14121 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14122 offset. If the attribute was not found return 0, otherwise return
14123 1. If it was found but could not properly be handled, set *OFFSET
14124 to 0. */
14125
14126 static int
14127 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14128 LONGEST *offset)
14129 {
14130 struct attribute *attr;
14131
14132 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14133 if (attr != NULL)
14134 {
14135 *offset = 0;
14136
14137 /* Note that we do not check for a section offset first here.
14138 This is because DW_AT_data_member_location is new in DWARF 4,
14139 so if we see it, we can assume that a constant form is really
14140 a constant and not a section offset. */
14141 if (attr->form_is_constant ())
14142 *offset = dwarf2_get_attr_constant_value (attr, 0);
14143 else if (attr->form_is_section_offset ())
14144 dwarf2_complex_location_expr_complaint ();
14145 else if (attr->form_is_block ())
14146 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14147 else
14148 dwarf2_complex_location_expr_complaint ();
14149
14150 return 1;
14151 }
14152
14153 return 0;
14154 }
14155
14156 /* Add an aggregate field to the field list. */
14157
14158 static void
14159 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14160 struct dwarf2_cu *cu)
14161 {
14162 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14163 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14164 struct nextfield *new_field;
14165 struct attribute *attr;
14166 struct field *fp;
14167 const char *fieldname = "";
14168
14169 if (die->tag == DW_TAG_inheritance)
14170 {
14171 fip->baseclasses.emplace_back ();
14172 new_field = &fip->baseclasses.back ();
14173 }
14174 else
14175 {
14176 fip->fields.emplace_back ();
14177 new_field = &fip->fields.back ();
14178 }
14179
14180 fip->nfields++;
14181
14182 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14183 if (attr != nullptr)
14184 new_field->accessibility = DW_UNSND (attr);
14185 else
14186 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14187 if (new_field->accessibility != DW_ACCESS_public)
14188 fip->non_public_fields = 1;
14189
14190 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14191 if (attr != nullptr)
14192 new_field->virtuality = DW_UNSND (attr);
14193 else
14194 new_field->virtuality = DW_VIRTUALITY_none;
14195
14196 fp = &new_field->field;
14197
14198 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14199 {
14200 LONGEST offset;
14201
14202 /* Data member other than a C++ static data member. */
14203
14204 /* Get type of field. */
14205 fp->type = die_type (die, cu);
14206
14207 SET_FIELD_BITPOS (*fp, 0);
14208
14209 /* Get bit size of field (zero if none). */
14210 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14211 if (attr != nullptr)
14212 {
14213 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14214 }
14215 else
14216 {
14217 FIELD_BITSIZE (*fp) = 0;
14218 }
14219
14220 /* Get bit offset of field. */
14221 if (handle_data_member_location (die, cu, &offset))
14222 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14223 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14224 if (attr != nullptr)
14225 {
14226 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14227 {
14228 /* For big endian bits, the DW_AT_bit_offset gives the
14229 additional bit offset from the MSB of the containing
14230 anonymous object to the MSB of the field. We don't
14231 have to do anything special since we don't need to
14232 know the size of the anonymous object. */
14233 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14234 }
14235 else
14236 {
14237 /* For little endian bits, compute the bit offset to the
14238 MSB of the anonymous object, subtract off the number of
14239 bits from the MSB of the field to the MSB of the
14240 object, and then subtract off the number of bits of
14241 the field itself. The result is the bit offset of
14242 the LSB of the field. */
14243 int anonymous_size;
14244 int bit_offset = DW_UNSND (attr);
14245
14246 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14247 if (attr != nullptr)
14248 {
14249 /* The size of the anonymous object containing
14250 the bit field is explicit, so use the
14251 indicated size (in bytes). */
14252 anonymous_size = DW_UNSND (attr);
14253 }
14254 else
14255 {
14256 /* The size of the anonymous object containing
14257 the bit field must be inferred from the type
14258 attribute of the data member containing the
14259 bit field. */
14260 anonymous_size = TYPE_LENGTH (fp->type);
14261 }
14262 SET_FIELD_BITPOS (*fp,
14263 (FIELD_BITPOS (*fp)
14264 + anonymous_size * bits_per_byte
14265 - bit_offset - FIELD_BITSIZE (*fp)));
14266 }
14267 }
14268 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14269 if (attr != NULL)
14270 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14271 + dwarf2_get_attr_constant_value (attr, 0)));
14272
14273 /* Get name of field. */
14274 fieldname = dwarf2_name (die, cu);
14275 if (fieldname == NULL)
14276 fieldname = "";
14277
14278 /* The name is already allocated along with this objfile, so we don't
14279 need to duplicate it for the type. */
14280 fp->name = fieldname;
14281
14282 /* Change accessibility for artificial fields (e.g. virtual table
14283 pointer or virtual base class pointer) to private. */
14284 if (dwarf2_attr (die, DW_AT_artificial, cu))
14285 {
14286 FIELD_ARTIFICIAL (*fp) = 1;
14287 new_field->accessibility = DW_ACCESS_private;
14288 fip->non_public_fields = 1;
14289 }
14290 }
14291 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14292 {
14293 /* C++ static member. */
14294
14295 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14296 is a declaration, but all versions of G++ as of this writing
14297 (so through at least 3.2.1) incorrectly generate
14298 DW_TAG_variable tags. */
14299
14300 const char *physname;
14301
14302 /* Get name of field. */
14303 fieldname = dwarf2_name (die, cu);
14304 if (fieldname == NULL)
14305 return;
14306
14307 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14308 if (attr
14309 /* Only create a symbol if this is an external value.
14310 new_symbol checks this and puts the value in the global symbol
14311 table, which we want. If it is not external, new_symbol
14312 will try to put the value in cu->list_in_scope which is wrong. */
14313 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14314 {
14315 /* A static const member, not much different than an enum as far as
14316 we're concerned, except that we can support more types. */
14317 new_symbol (die, NULL, cu);
14318 }
14319
14320 /* Get physical name. */
14321 physname = dwarf2_physname (fieldname, die, cu);
14322
14323 /* The name is already allocated along with this objfile, so we don't
14324 need to duplicate it for the type. */
14325 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14326 FIELD_TYPE (*fp) = die_type (die, cu);
14327 FIELD_NAME (*fp) = fieldname;
14328 }
14329 else if (die->tag == DW_TAG_inheritance)
14330 {
14331 LONGEST offset;
14332
14333 /* C++ base class field. */
14334 if (handle_data_member_location (die, cu, &offset))
14335 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14336 FIELD_BITSIZE (*fp) = 0;
14337 FIELD_TYPE (*fp) = die_type (die, cu);
14338 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14339 }
14340 else if (die->tag == DW_TAG_variant_part)
14341 {
14342 /* process_structure_scope will treat this DIE as a union. */
14343 process_structure_scope (die, cu);
14344
14345 /* The variant part is relative to the start of the enclosing
14346 structure. */
14347 SET_FIELD_BITPOS (*fp, 0);
14348 fp->type = get_die_type (die, cu);
14349 fp->artificial = 1;
14350 fp->name = "<<variant>>";
14351
14352 /* Normally a DW_TAG_variant_part won't have a size, but our
14353 representation requires one, so set it to the maximum of the
14354 child sizes, being sure to account for the offset at which
14355 each child is seen. */
14356 if (TYPE_LENGTH (fp->type) == 0)
14357 {
14358 unsigned max = 0;
14359 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14360 {
14361 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14362 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14363 if (len > max)
14364 max = len;
14365 }
14366 TYPE_LENGTH (fp->type) = max;
14367 }
14368 }
14369 else
14370 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14371 }
14372
14373 /* Can the type given by DIE define another type? */
14374
14375 static bool
14376 type_can_define_types (const struct die_info *die)
14377 {
14378 switch (die->tag)
14379 {
14380 case DW_TAG_typedef:
14381 case DW_TAG_class_type:
14382 case DW_TAG_structure_type:
14383 case DW_TAG_union_type:
14384 case DW_TAG_enumeration_type:
14385 return true;
14386
14387 default:
14388 return false;
14389 }
14390 }
14391
14392 /* Add a type definition defined in the scope of the FIP's class. */
14393
14394 static void
14395 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14396 struct dwarf2_cu *cu)
14397 {
14398 struct decl_field fp;
14399 memset (&fp, 0, sizeof (fp));
14400
14401 gdb_assert (type_can_define_types (die));
14402
14403 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14404 fp.name = dwarf2_name (die, cu);
14405 fp.type = read_type_die (die, cu);
14406
14407 /* Save accessibility. */
14408 enum dwarf_access_attribute accessibility;
14409 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14410 if (attr != NULL)
14411 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14412 else
14413 accessibility = dwarf2_default_access_attribute (die, cu);
14414 switch (accessibility)
14415 {
14416 case DW_ACCESS_public:
14417 /* The assumed value if neither private nor protected. */
14418 break;
14419 case DW_ACCESS_private:
14420 fp.is_private = 1;
14421 break;
14422 case DW_ACCESS_protected:
14423 fp.is_protected = 1;
14424 break;
14425 default:
14426 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14427 }
14428
14429 if (die->tag == DW_TAG_typedef)
14430 fip->typedef_field_list.push_back (fp);
14431 else
14432 fip->nested_types_list.push_back (fp);
14433 }
14434
14435 /* Create the vector of fields, and attach it to the type. */
14436
14437 static void
14438 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14439 struct dwarf2_cu *cu)
14440 {
14441 int nfields = fip->nfields;
14442
14443 /* Record the field count, allocate space for the array of fields,
14444 and create blank accessibility bitfields if necessary. */
14445 TYPE_NFIELDS (type) = nfields;
14446 TYPE_FIELDS (type) = (struct field *)
14447 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14448
14449 if (fip->non_public_fields && cu->language != language_ada)
14450 {
14451 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14452
14453 TYPE_FIELD_PRIVATE_BITS (type) =
14454 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14455 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14456
14457 TYPE_FIELD_PROTECTED_BITS (type) =
14458 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14459 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14460
14461 TYPE_FIELD_IGNORE_BITS (type) =
14462 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14463 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14464 }
14465
14466 /* If the type has baseclasses, allocate and clear a bit vector for
14467 TYPE_FIELD_VIRTUAL_BITS. */
14468 if (!fip->baseclasses.empty () && cu->language != language_ada)
14469 {
14470 int num_bytes = B_BYTES (fip->baseclasses.size ());
14471 unsigned char *pointer;
14472
14473 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14474 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14475 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14476 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14477 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14478 }
14479
14480 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14481 {
14482 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14483
14484 for (int index = 0; index < nfields; ++index)
14485 {
14486 struct nextfield &field = fip->fields[index];
14487
14488 if (field.variant.is_discriminant)
14489 di->discriminant_index = index;
14490 else if (field.variant.default_branch)
14491 di->default_index = index;
14492 else
14493 di->discriminants[index] = field.variant.discriminant_value;
14494 }
14495 }
14496
14497 /* Copy the saved-up fields into the field vector. */
14498 for (int i = 0; i < nfields; ++i)
14499 {
14500 struct nextfield &field
14501 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14502 : fip->fields[i - fip->baseclasses.size ()]);
14503
14504 TYPE_FIELD (type, i) = field.field;
14505 switch (field.accessibility)
14506 {
14507 case DW_ACCESS_private:
14508 if (cu->language != language_ada)
14509 SET_TYPE_FIELD_PRIVATE (type, i);
14510 break;
14511
14512 case DW_ACCESS_protected:
14513 if (cu->language != language_ada)
14514 SET_TYPE_FIELD_PROTECTED (type, i);
14515 break;
14516
14517 case DW_ACCESS_public:
14518 break;
14519
14520 default:
14521 /* Unknown accessibility. Complain and treat it as public. */
14522 {
14523 complaint (_("unsupported accessibility %d"),
14524 field.accessibility);
14525 }
14526 break;
14527 }
14528 if (i < fip->baseclasses.size ())
14529 {
14530 switch (field.virtuality)
14531 {
14532 case DW_VIRTUALITY_virtual:
14533 case DW_VIRTUALITY_pure_virtual:
14534 if (cu->language == language_ada)
14535 error (_("unexpected virtuality in component of Ada type"));
14536 SET_TYPE_FIELD_VIRTUAL (type, i);
14537 break;
14538 }
14539 }
14540 }
14541 }
14542
14543 /* Return true if this member function is a constructor, false
14544 otherwise. */
14545
14546 static int
14547 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14548 {
14549 const char *fieldname;
14550 const char *type_name;
14551 int len;
14552
14553 if (die->parent == NULL)
14554 return 0;
14555
14556 if (die->parent->tag != DW_TAG_structure_type
14557 && die->parent->tag != DW_TAG_union_type
14558 && die->parent->tag != DW_TAG_class_type)
14559 return 0;
14560
14561 fieldname = dwarf2_name (die, cu);
14562 type_name = dwarf2_name (die->parent, cu);
14563 if (fieldname == NULL || type_name == NULL)
14564 return 0;
14565
14566 len = strlen (fieldname);
14567 return (strncmp (fieldname, type_name, len) == 0
14568 && (type_name[len] == '\0' || type_name[len] == '<'));
14569 }
14570
14571 /* Check if the given VALUE is a recognized enum
14572 dwarf_defaulted_attribute constant according to DWARF5 spec,
14573 Table 7.24. */
14574
14575 static bool
14576 is_valid_DW_AT_defaulted (ULONGEST value)
14577 {
14578 switch (value)
14579 {
14580 case DW_DEFAULTED_no:
14581 case DW_DEFAULTED_in_class:
14582 case DW_DEFAULTED_out_of_class:
14583 return true;
14584 }
14585
14586 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14587 return false;
14588 }
14589
14590 /* Add a member function to the proper fieldlist. */
14591
14592 static void
14593 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14594 struct type *type, struct dwarf2_cu *cu)
14595 {
14596 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14597 struct attribute *attr;
14598 int i;
14599 struct fnfieldlist *flp = nullptr;
14600 struct fn_field *fnp;
14601 const char *fieldname;
14602 struct type *this_type;
14603 enum dwarf_access_attribute accessibility;
14604
14605 if (cu->language == language_ada)
14606 error (_("unexpected member function in Ada type"));
14607
14608 /* Get name of member function. */
14609 fieldname = dwarf2_name (die, cu);
14610 if (fieldname == NULL)
14611 return;
14612
14613 /* Look up member function name in fieldlist. */
14614 for (i = 0; i < fip->fnfieldlists.size (); i++)
14615 {
14616 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14617 {
14618 flp = &fip->fnfieldlists[i];
14619 break;
14620 }
14621 }
14622
14623 /* Create a new fnfieldlist if necessary. */
14624 if (flp == nullptr)
14625 {
14626 fip->fnfieldlists.emplace_back ();
14627 flp = &fip->fnfieldlists.back ();
14628 flp->name = fieldname;
14629 i = fip->fnfieldlists.size () - 1;
14630 }
14631
14632 /* Create a new member function field and add it to the vector of
14633 fnfieldlists. */
14634 flp->fnfields.emplace_back ();
14635 fnp = &flp->fnfields.back ();
14636
14637 /* Delay processing of the physname until later. */
14638 if (cu->language == language_cplus)
14639 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14640 die, cu);
14641 else
14642 {
14643 const char *physname = dwarf2_physname (fieldname, die, cu);
14644 fnp->physname = physname ? physname : "";
14645 }
14646
14647 fnp->type = alloc_type (objfile);
14648 this_type = read_type_die (die, cu);
14649 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14650 {
14651 int nparams = TYPE_NFIELDS (this_type);
14652
14653 /* TYPE is the domain of this method, and THIS_TYPE is the type
14654 of the method itself (TYPE_CODE_METHOD). */
14655 smash_to_method_type (fnp->type, type,
14656 TYPE_TARGET_TYPE (this_type),
14657 TYPE_FIELDS (this_type),
14658 TYPE_NFIELDS (this_type),
14659 TYPE_VARARGS (this_type));
14660
14661 /* Handle static member functions.
14662 Dwarf2 has no clean way to discern C++ static and non-static
14663 member functions. G++ helps GDB by marking the first
14664 parameter for non-static member functions (which is the this
14665 pointer) as artificial. We obtain this information from
14666 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14667 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14668 fnp->voffset = VOFFSET_STATIC;
14669 }
14670 else
14671 complaint (_("member function type missing for '%s'"),
14672 dwarf2_full_name (fieldname, die, cu));
14673
14674 /* Get fcontext from DW_AT_containing_type if present. */
14675 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14676 fnp->fcontext = die_containing_type (die, cu);
14677
14678 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14679 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14680
14681 /* Get accessibility. */
14682 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14683 if (attr != nullptr)
14684 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14685 else
14686 accessibility = dwarf2_default_access_attribute (die, cu);
14687 switch (accessibility)
14688 {
14689 case DW_ACCESS_private:
14690 fnp->is_private = 1;
14691 break;
14692 case DW_ACCESS_protected:
14693 fnp->is_protected = 1;
14694 break;
14695 }
14696
14697 /* Check for artificial methods. */
14698 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14699 if (attr && DW_UNSND (attr) != 0)
14700 fnp->is_artificial = 1;
14701
14702 /* Check for defaulted methods. */
14703 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14704 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14705 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14706
14707 /* Check for deleted methods. */
14708 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14709 if (attr != nullptr && DW_UNSND (attr) != 0)
14710 fnp->is_deleted = 1;
14711
14712 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14713
14714 /* Get index in virtual function table if it is a virtual member
14715 function. For older versions of GCC, this is an offset in the
14716 appropriate virtual table, as specified by DW_AT_containing_type.
14717 For everyone else, it is an expression to be evaluated relative
14718 to the object address. */
14719
14720 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14721 if (attr != nullptr)
14722 {
14723 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14724 {
14725 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14726 {
14727 /* Old-style GCC. */
14728 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14729 }
14730 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14731 || (DW_BLOCK (attr)->size > 1
14732 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14733 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14734 {
14735 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14736 if ((fnp->voffset % cu->header.addr_size) != 0)
14737 dwarf2_complex_location_expr_complaint ();
14738 else
14739 fnp->voffset /= cu->header.addr_size;
14740 fnp->voffset += 2;
14741 }
14742 else
14743 dwarf2_complex_location_expr_complaint ();
14744
14745 if (!fnp->fcontext)
14746 {
14747 /* If there is no `this' field and no DW_AT_containing_type,
14748 we cannot actually find a base class context for the
14749 vtable! */
14750 if (TYPE_NFIELDS (this_type) == 0
14751 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14752 {
14753 complaint (_("cannot determine context for virtual member "
14754 "function \"%s\" (offset %s)"),
14755 fieldname, sect_offset_str (die->sect_off));
14756 }
14757 else
14758 {
14759 fnp->fcontext
14760 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14761 }
14762 }
14763 }
14764 else if (attr->form_is_section_offset ())
14765 {
14766 dwarf2_complex_location_expr_complaint ();
14767 }
14768 else
14769 {
14770 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14771 fieldname);
14772 }
14773 }
14774 else
14775 {
14776 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14777 if (attr && DW_UNSND (attr))
14778 {
14779 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14780 complaint (_("Member function \"%s\" (offset %s) is virtual "
14781 "but the vtable offset is not specified"),
14782 fieldname, sect_offset_str (die->sect_off));
14783 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14784 TYPE_CPLUS_DYNAMIC (type) = 1;
14785 }
14786 }
14787 }
14788
14789 /* Create the vector of member function fields, and attach it to the type. */
14790
14791 static void
14792 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14793 struct dwarf2_cu *cu)
14794 {
14795 if (cu->language == language_ada)
14796 error (_("unexpected member functions in Ada type"));
14797
14798 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14799 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14800 TYPE_ALLOC (type,
14801 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14802
14803 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14804 {
14805 struct fnfieldlist &nf = fip->fnfieldlists[i];
14806 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14807
14808 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14809 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14810 fn_flp->fn_fields = (struct fn_field *)
14811 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14812
14813 for (int k = 0; k < nf.fnfields.size (); ++k)
14814 fn_flp->fn_fields[k] = nf.fnfields[k];
14815 }
14816
14817 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14818 }
14819
14820 /* Returns non-zero if NAME is the name of a vtable member in CU's
14821 language, zero otherwise. */
14822 static int
14823 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14824 {
14825 static const char vptr[] = "_vptr";
14826
14827 /* Look for the C++ form of the vtable. */
14828 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14829 return 1;
14830
14831 return 0;
14832 }
14833
14834 /* GCC outputs unnamed structures that are really pointers to member
14835 functions, with the ABI-specified layout. If TYPE describes
14836 such a structure, smash it into a member function type.
14837
14838 GCC shouldn't do this; it should just output pointer to member DIEs.
14839 This is GCC PR debug/28767. */
14840
14841 static void
14842 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14843 {
14844 struct type *pfn_type, *self_type, *new_type;
14845
14846 /* Check for a structure with no name and two children. */
14847 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14848 return;
14849
14850 /* Check for __pfn and __delta members. */
14851 if (TYPE_FIELD_NAME (type, 0) == NULL
14852 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14853 || TYPE_FIELD_NAME (type, 1) == NULL
14854 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14855 return;
14856
14857 /* Find the type of the method. */
14858 pfn_type = TYPE_FIELD_TYPE (type, 0);
14859 if (pfn_type == NULL
14860 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14861 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14862 return;
14863
14864 /* Look for the "this" argument. */
14865 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14866 if (TYPE_NFIELDS (pfn_type) == 0
14867 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14868 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14869 return;
14870
14871 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14872 new_type = alloc_type (objfile);
14873 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14874 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14875 TYPE_VARARGS (pfn_type));
14876 smash_to_methodptr_type (type, new_type);
14877 }
14878
14879 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14880 appropriate error checking and issuing complaints if there is a
14881 problem. */
14882
14883 static ULONGEST
14884 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14885 {
14886 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14887
14888 if (attr == nullptr)
14889 return 0;
14890
14891 if (!attr->form_is_constant ())
14892 {
14893 complaint (_("DW_AT_alignment must have constant form"
14894 " - DIE at %s [in module %s]"),
14895 sect_offset_str (die->sect_off),
14896 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14897 return 0;
14898 }
14899
14900 ULONGEST align;
14901 if (attr->form == DW_FORM_sdata)
14902 {
14903 LONGEST val = DW_SND (attr);
14904 if (val < 0)
14905 {
14906 complaint (_("DW_AT_alignment value must not be negative"
14907 " - DIE at %s [in module %s]"),
14908 sect_offset_str (die->sect_off),
14909 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14910 return 0;
14911 }
14912 align = val;
14913 }
14914 else
14915 align = DW_UNSND (attr);
14916
14917 if (align == 0)
14918 {
14919 complaint (_("DW_AT_alignment value must not be zero"
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 if ((align & (align - 1)) != 0)
14926 {
14927 complaint (_("DW_AT_alignment value must be a power of 2"
14928 " - DIE at %s [in module %s]"),
14929 sect_offset_str (die->sect_off),
14930 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14931 return 0;
14932 }
14933
14934 return align;
14935 }
14936
14937 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14938 the alignment for TYPE. */
14939
14940 static void
14941 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14942 struct type *type)
14943 {
14944 if (!set_type_align (type, get_alignment (cu, die)))
14945 complaint (_("DW_AT_alignment value too large"
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 }
14950
14951 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14952 constant for a type, according to DWARF5 spec, Table 5.5. */
14953
14954 static bool
14955 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14956 {
14957 switch (value)
14958 {
14959 case DW_CC_normal:
14960 case DW_CC_pass_by_reference:
14961 case DW_CC_pass_by_value:
14962 return true;
14963
14964 default:
14965 complaint (_("unrecognized DW_AT_calling_convention value "
14966 "(%s) for a type"), pulongest (value));
14967 return false;
14968 }
14969 }
14970
14971 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14972 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14973 also according to GNU-specific values (see include/dwarf2.h). */
14974
14975 static bool
14976 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14977 {
14978 switch (value)
14979 {
14980 case DW_CC_normal:
14981 case DW_CC_program:
14982 case DW_CC_nocall:
14983 return true;
14984
14985 case DW_CC_GNU_renesas_sh:
14986 case DW_CC_GNU_borland_fastcall_i386:
14987 case DW_CC_GDB_IBM_OpenCL:
14988 return true;
14989
14990 default:
14991 complaint (_("unrecognized DW_AT_calling_convention value "
14992 "(%s) for a subroutine"), pulongest (value));
14993 return false;
14994 }
14995 }
14996
14997 /* Called when we find the DIE that starts a structure or union scope
14998 (definition) to create a type for the structure or union. Fill in
14999 the type's name and general properties; the members will not be
15000 processed until process_structure_scope. A symbol table entry for
15001 the type will also not be done until process_structure_scope (assuming
15002 the type has a name).
15003
15004 NOTE: we need to call these functions regardless of whether or not the
15005 DIE has a DW_AT_name attribute, since it might be an anonymous
15006 structure or union. This gets the type entered into our set of
15007 user defined types. */
15008
15009 static struct type *
15010 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15011 {
15012 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15013 struct type *type;
15014 struct attribute *attr;
15015 const char *name;
15016
15017 /* If the definition of this type lives in .debug_types, read that type.
15018 Don't follow DW_AT_specification though, that will take us back up
15019 the chain and we want to go down. */
15020 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15021 if (attr != nullptr)
15022 {
15023 type = get_DW_AT_signature_type (die, attr, cu);
15024
15025 /* The type's CU may not be the same as CU.
15026 Ensure TYPE is recorded with CU in die_type_hash. */
15027 return set_die_type (die, type, cu);
15028 }
15029
15030 type = alloc_type (objfile);
15031 INIT_CPLUS_SPECIFIC (type);
15032
15033 name = dwarf2_name (die, cu);
15034 if (name != NULL)
15035 {
15036 if (cu->language == language_cplus
15037 || cu->language == language_d
15038 || cu->language == language_rust)
15039 {
15040 const char *full_name = dwarf2_full_name (name, die, cu);
15041
15042 /* dwarf2_full_name might have already finished building the DIE's
15043 type. If so, there is no need to continue. */
15044 if (get_die_type (die, cu) != NULL)
15045 return get_die_type (die, cu);
15046
15047 TYPE_NAME (type) = full_name;
15048 }
15049 else
15050 {
15051 /* The name is already allocated along with this objfile, so
15052 we don't need to duplicate it for the type. */
15053 TYPE_NAME (type) = name;
15054 }
15055 }
15056
15057 if (die->tag == DW_TAG_structure_type)
15058 {
15059 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15060 }
15061 else if (die->tag == DW_TAG_union_type)
15062 {
15063 TYPE_CODE (type) = TYPE_CODE_UNION;
15064 }
15065 else if (die->tag == DW_TAG_variant_part)
15066 {
15067 TYPE_CODE (type) = TYPE_CODE_UNION;
15068 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15069 }
15070 else
15071 {
15072 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15073 }
15074
15075 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15076 TYPE_DECLARED_CLASS (type) = 1;
15077
15078 /* Store the calling convention in the type if it's available in
15079 the die. Otherwise the calling convention remains set to
15080 the default value DW_CC_normal. */
15081 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15082 if (attr != nullptr
15083 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15084 {
15085 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15086 TYPE_CPLUS_CALLING_CONVENTION (type)
15087 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15088 }
15089
15090 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15091 if (attr != nullptr)
15092 {
15093 if (attr->form_is_constant ())
15094 TYPE_LENGTH (type) = DW_UNSND (attr);
15095 else
15096 {
15097 /* For the moment, dynamic type sizes are not supported
15098 by GDB's struct type. The actual size is determined
15099 on-demand when resolving the type of a given object,
15100 so set the type's length to zero for now. Otherwise,
15101 we record an expression as the length, and that expression
15102 could lead to a very large value, which could eventually
15103 lead to us trying to allocate that much memory when creating
15104 a value of that type. */
15105 TYPE_LENGTH (type) = 0;
15106 }
15107 }
15108 else
15109 {
15110 TYPE_LENGTH (type) = 0;
15111 }
15112
15113 maybe_set_alignment (cu, die, type);
15114
15115 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15116 {
15117 /* ICC<14 does not output the required DW_AT_declaration on
15118 incomplete types, but gives them a size of zero. */
15119 TYPE_STUB (type) = 1;
15120 }
15121 else
15122 TYPE_STUB_SUPPORTED (type) = 1;
15123
15124 if (die_is_declaration (die, cu))
15125 TYPE_STUB (type) = 1;
15126 else if (attr == NULL && die->child == NULL
15127 && producer_is_realview (cu->producer))
15128 /* RealView does not output the required DW_AT_declaration
15129 on incomplete types. */
15130 TYPE_STUB (type) = 1;
15131
15132 /* We need to add the type field to the die immediately so we don't
15133 infinitely recurse when dealing with pointers to the structure
15134 type within the structure itself. */
15135 set_die_type (die, type, cu);
15136
15137 /* set_die_type should be already done. */
15138 set_descriptive_type (type, die, cu);
15139
15140 return type;
15141 }
15142
15143 /* A helper for process_structure_scope that handles a single member
15144 DIE. */
15145
15146 static void
15147 handle_struct_member_die (struct die_info *child_die, struct type *type,
15148 struct field_info *fi,
15149 std::vector<struct symbol *> *template_args,
15150 struct dwarf2_cu *cu)
15151 {
15152 if (child_die->tag == DW_TAG_member
15153 || child_die->tag == DW_TAG_variable
15154 || child_die->tag == DW_TAG_variant_part)
15155 {
15156 /* NOTE: carlton/2002-11-05: A C++ static data member
15157 should be a DW_TAG_member that is a declaration, but
15158 all versions of G++ as of this writing (so through at
15159 least 3.2.1) incorrectly generate DW_TAG_variable
15160 tags for them instead. */
15161 dwarf2_add_field (fi, child_die, cu);
15162 }
15163 else if (child_die->tag == DW_TAG_subprogram)
15164 {
15165 /* Rust doesn't have member functions in the C++ sense.
15166 However, it does emit ordinary functions as children
15167 of a struct DIE. */
15168 if (cu->language == language_rust)
15169 read_func_scope (child_die, cu);
15170 else
15171 {
15172 /* C++ member function. */
15173 dwarf2_add_member_fn (fi, child_die, type, cu);
15174 }
15175 }
15176 else if (child_die->tag == DW_TAG_inheritance)
15177 {
15178 /* C++ base class field. */
15179 dwarf2_add_field (fi, child_die, cu);
15180 }
15181 else if (type_can_define_types (child_die))
15182 dwarf2_add_type_defn (fi, child_die, cu);
15183 else if (child_die->tag == DW_TAG_template_type_param
15184 || child_die->tag == DW_TAG_template_value_param)
15185 {
15186 struct symbol *arg = new_symbol (child_die, NULL, cu);
15187
15188 if (arg != NULL)
15189 template_args->push_back (arg);
15190 }
15191 else if (child_die->tag == DW_TAG_variant)
15192 {
15193 /* In a variant we want to get the discriminant and also add a
15194 field for our sole member child. */
15195 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15196
15197 for (die_info *variant_child = child_die->child;
15198 variant_child != NULL;
15199 variant_child = sibling_die (variant_child))
15200 {
15201 if (variant_child->tag == DW_TAG_member)
15202 {
15203 handle_struct_member_die (variant_child, type, fi,
15204 template_args, cu);
15205 /* Only handle the one. */
15206 break;
15207 }
15208 }
15209
15210 /* We don't handle this but we might as well report it if we see
15211 it. */
15212 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15213 complaint (_("DW_AT_discr_list is not supported yet"
15214 " - DIE at %s [in module %s]"),
15215 sect_offset_str (child_die->sect_off),
15216 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15217
15218 /* The first field was just added, so we can stash the
15219 discriminant there. */
15220 gdb_assert (!fi->fields.empty ());
15221 if (discr == NULL)
15222 fi->fields.back ().variant.default_branch = true;
15223 else
15224 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15225 }
15226 }
15227
15228 /* Finish creating a structure or union type, including filling in
15229 its members and creating a symbol for it. */
15230
15231 static void
15232 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15233 {
15234 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15235 struct die_info *child_die;
15236 struct type *type;
15237
15238 type = get_die_type (die, cu);
15239 if (type == NULL)
15240 type = read_structure_type (die, cu);
15241
15242 /* When reading a DW_TAG_variant_part, we need to notice when we
15243 read the discriminant member, so we can record it later in the
15244 discriminant_info. */
15245 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15246 sect_offset discr_offset {};
15247 bool has_template_parameters = false;
15248
15249 if (is_variant_part)
15250 {
15251 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15252 if (discr == NULL)
15253 {
15254 /* Maybe it's a univariant form, an extension we support.
15255 In this case arrange not to check the offset. */
15256 is_variant_part = false;
15257 }
15258 else if (discr->form_is_ref ())
15259 {
15260 struct dwarf2_cu *target_cu = cu;
15261 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15262
15263 discr_offset = target_die->sect_off;
15264 }
15265 else
15266 {
15267 complaint (_("DW_AT_discr does not have DIE reference form"
15268 " - DIE at %s [in module %s]"),
15269 sect_offset_str (die->sect_off),
15270 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15271 is_variant_part = false;
15272 }
15273 }
15274
15275 if (die->child != NULL && ! die_is_declaration (die, cu))
15276 {
15277 struct field_info fi;
15278 std::vector<struct symbol *> template_args;
15279
15280 child_die = die->child;
15281
15282 while (child_die && child_die->tag)
15283 {
15284 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15285
15286 if (is_variant_part && discr_offset == child_die->sect_off)
15287 fi.fields.back ().variant.is_discriminant = true;
15288
15289 child_die = sibling_die (child_die);
15290 }
15291
15292 /* Attach template arguments to type. */
15293 if (!template_args.empty ())
15294 {
15295 has_template_parameters = true;
15296 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15297 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15298 TYPE_TEMPLATE_ARGUMENTS (type)
15299 = XOBNEWVEC (&objfile->objfile_obstack,
15300 struct symbol *,
15301 TYPE_N_TEMPLATE_ARGUMENTS (type));
15302 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15303 template_args.data (),
15304 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15305 * sizeof (struct symbol *)));
15306 }
15307
15308 /* Attach fields and member functions to the type. */
15309 if (fi.nfields)
15310 dwarf2_attach_fields_to_type (&fi, type, cu);
15311 if (!fi.fnfieldlists.empty ())
15312 {
15313 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15314
15315 /* Get the type which refers to the base class (possibly this
15316 class itself) which contains the vtable pointer for the current
15317 class from the DW_AT_containing_type attribute. This use of
15318 DW_AT_containing_type is a GNU extension. */
15319
15320 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15321 {
15322 struct type *t = die_containing_type (die, cu);
15323
15324 set_type_vptr_basetype (type, t);
15325 if (type == t)
15326 {
15327 int i;
15328
15329 /* Our own class provides vtbl ptr. */
15330 for (i = TYPE_NFIELDS (t) - 1;
15331 i >= TYPE_N_BASECLASSES (t);
15332 --i)
15333 {
15334 const char *fieldname = TYPE_FIELD_NAME (t, i);
15335
15336 if (is_vtable_name (fieldname, cu))
15337 {
15338 set_type_vptr_fieldno (type, i);
15339 break;
15340 }
15341 }
15342
15343 /* Complain if virtual function table field not found. */
15344 if (i < TYPE_N_BASECLASSES (t))
15345 complaint (_("virtual function table pointer "
15346 "not found when defining class '%s'"),
15347 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15348 }
15349 else
15350 {
15351 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15352 }
15353 }
15354 else if (cu->producer
15355 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15356 {
15357 /* The IBM XLC compiler does not provide direct indication
15358 of the containing type, but the vtable pointer is
15359 always named __vfp. */
15360
15361 int i;
15362
15363 for (i = TYPE_NFIELDS (type) - 1;
15364 i >= TYPE_N_BASECLASSES (type);
15365 --i)
15366 {
15367 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15368 {
15369 set_type_vptr_fieldno (type, i);
15370 set_type_vptr_basetype (type, type);
15371 break;
15372 }
15373 }
15374 }
15375 }
15376
15377 /* Copy fi.typedef_field_list linked list elements content into the
15378 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15379 if (!fi.typedef_field_list.empty ())
15380 {
15381 int count = fi.typedef_field_list.size ();
15382
15383 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15384 TYPE_TYPEDEF_FIELD_ARRAY (type)
15385 = ((struct decl_field *)
15386 TYPE_ALLOC (type,
15387 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15388 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15389
15390 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15391 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15392 }
15393
15394 /* Copy fi.nested_types_list linked list elements content into the
15395 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15396 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15397 {
15398 int count = fi.nested_types_list.size ();
15399
15400 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15401 TYPE_NESTED_TYPES_ARRAY (type)
15402 = ((struct decl_field *)
15403 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15404 TYPE_NESTED_TYPES_COUNT (type) = count;
15405
15406 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15407 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15408 }
15409 }
15410
15411 quirk_gcc_member_function_pointer (type, objfile);
15412 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15413 cu->rust_unions.push_back (type);
15414
15415 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15416 snapshots) has been known to create a die giving a declaration
15417 for a class that has, as a child, a die giving a definition for a
15418 nested class. So we have to process our children even if the
15419 current die is a declaration. Normally, of course, a declaration
15420 won't have any children at all. */
15421
15422 child_die = die->child;
15423
15424 while (child_die != NULL && child_die->tag)
15425 {
15426 if (child_die->tag == DW_TAG_member
15427 || child_die->tag == DW_TAG_variable
15428 || child_die->tag == DW_TAG_inheritance
15429 || child_die->tag == DW_TAG_template_value_param
15430 || child_die->tag == DW_TAG_template_type_param)
15431 {
15432 /* Do nothing. */
15433 }
15434 else
15435 process_die (child_die, cu);
15436
15437 child_die = sibling_die (child_die);
15438 }
15439
15440 /* Do not consider external references. According to the DWARF standard,
15441 these DIEs are identified by the fact that they have no byte_size
15442 attribute, and a declaration attribute. */
15443 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15444 || !die_is_declaration (die, cu))
15445 {
15446 struct symbol *sym = new_symbol (die, type, cu);
15447
15448 if (has_template_parameters)
15449 {
15450 struct symtab *symtab;
15451 if (sym != nullptr)
15452 symtab = symbol_symtab (sym);
15453 else if (cu->line_header != nullptr)
15454 {
15455 /* Any related symtab will do. */
15456 symtab
15457 = cu->line_header->file_names ()[0].symtab;
15458 }
15459 else
15460 {
15461 symtab = nullptr;
15462 complaint (_("could not find suitable "
15463 "symtab for template parameter"
15464 " - DIE at %s [in module %s]"),
15465 sect_offset_str (die->sect_off),
15466 objfile_name (objfile));
15467 }
15468
15469 if (symtab != nullptr)
15470 {
15471 /* Make sure that the symtab is set on the new symbols.
15472 Even though they don't appear in this symtab directly,
15473 other parts of gdb assume that symbols do, and this is
15474 reasonably true. */
15475 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15476 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15477 }
15478 }
15479 }
15480 }
15481
15482 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15483 update TYPE using some information only available in DIE's children. */
15484
15485 static void
15486 update_enumeration_type_from_children (struct die_info *die,
15487 struct type *type,
15488 struct dwarf2_cu *cu)
15489 {
15490 struct die_info *child_die;
15491 int unsigned_enum = 1;
15492 int flag_enum = 1;
15493 ULONGEST mask = 0;
15494
15495 auto_obstack obstack;
15496
15497 for (child_die = die->child;
15498 child_die != NULL && child_die->tag;
15499 child_die = sibling_die (child_die))
15500 {
15501 struct attribute *attr;
15502 LONGEST value;
15503 const gdb_byte *bytes;
15504 struct dwarf2_locexpr_baton *baton;
15505 const char *name;
15506
15507 if (child_die->tag != DW_TAG_enumerator)
15508 continue;
15509
15510 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15511 if (attr == NULL)
15512 continue;
15513
15514 name = dwarf2_name (child_die, cu);
15515 if (name == NULL)
15516 name = "<anonymous enumerator>";
15517
15518 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15519 &value, &bytes, &baton);
15520 if (value < 0)
15521 {
15522 unsigned_enum = 0;
15523 flag_enum = 0;
15524 }
15525 else if ((mask & value) != 0)
15526 flag_enum = 0;
15527 else
15528 mask |= value;
15529
15530 /* If we already know that the enum type is neither unsigned, nor
15531 a flag type, no need to look at the rest of the enumerates. */
15532 if (!unsigned_enum && !flag_enum)
15533 break;
15534 }
15535
15536 if (unsigned_enum)
15537 TYPE_UNSIGNED (type) = 1;
15538 if (flag_enum)
15539 TYPE_FLAG_ENUM (type) = 1;
15540 }
15541
15542 /* Given a DW_AT_enumeration_type die, set its type. We do not
15543 complete the type's fields yet, or create any symbols. */
15544
15545 static struct type *
15546 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15547 {
15548 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15549 struct type *type;
15550 struct attribute *attr;
15551 const char *name;
15552
15553 /* If the definition of this type lives in .debug_types, read that type.
15554 Don't follow DW_AT_specification though, that will take us back up
15555 the chain and we want to go down. */
15556 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15557 if (attr != nullptr)
15558 {
15559 type = get_DW_AT_signature_type (die, attr, cu);
15560
15561 /* The type's CU may not be the same as CU.
15562 Ensure TYPE is recorded with CU in die_type_hash. */
15563 return set_die_type (die, type, cu);
15564 }
15565
15566 type = alloc_type (objfile);
15567
15568 TYPE_CODE (type) = TYPE_CODE_ENUM;
15569 name = dwarf2_full_name (NULL, die, cu);
15570 if (name != NULL)
15571 TYPE_NAME (type) = name;
15572
15573 attr = dwarf2_attr (die, DW_AT_type, cu);
15574 if (attr != NULL)
15575 {
15576 struct type *underlying_type = die_type (die, cu);
15577
15578 TYPE_TARGET_TYPE (type) = underlying_type;
15579 }
15580
15581 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15582 if (attr != nullptr)
15583 {
15584 TYPE_LENGTH (type) = DW_UNSND (attr);
15585 }
15586 else
15587 {
15588 TYPE_LENGTH (type) = 0;
15589 }
15590
15591 maybe_set_alignment (cu, die, type);
15592
15593 /* The enumeration DIE can be incomplete. In Ada, any type can be
15594 declared as private in the package spec, and then defined only
15595 inside the package body. Such types are known as Taft Amendment
15596 Types. When another package uses such a type, an incomplete DIE
15597 may be generated by the compiler. */
15598 if (die_is_declaration (die, cu))
15599 TYPE_STUB (type) = 1;
15600
15601 /* Finish the creation of this type by using the enum's children.
15602 We must call this even when the underlying type has been provided
15603 so that we can determine if we're looking at a "flag" enum. */
15604 update_enumeration_type_from_children (die, type, cu);
15605
15606 /* If this type has an underlying type that is not a stub, then we
15607 may use its attributes. We always use the "unsigned" attribute
15608 in this situation, because ordinarily we guess whether the type
15609 is unsigned -- but the guess can be wrong and the underlying type
15610 can tell us the reality. However, we defer to a local size
15611 attribute if one exists, because this lets the compiler override
15612 the underlying type if needed. */
15613 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15614 {
15615 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15616 if (TYPE_LENGTH (type) == 0)
15617 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15618 if (TYPE_RAW_ALIGN (type) == 0
15619 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15620 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15621 }
15622
15623 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15624
15625 return set_die_type (die, type, cu);
15626 }
15627
15628 /* Given a pointer to a die which begins an enumeration, process all
15629 the dies that define the members of the enumeration, and create the
15630 symbol for the enumeration type.
15631
15632 NOTE: We reverse the order of the element list. */
15633
15634 static void
15635 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15636 {
15637 struct type *this_type;
15638
15639 this_type = get_die_type (die, cu);
15640 if (this_type == NULL)
15641 this_type = read_enumeration_type (die, cu);
15642
15643 if (die->child != NULL)
15644 {
15645 struct die_info *child_die;
15646 struct symbol *sym;
15647 std::vector<struct field> fields;
15648 const char *name;
15649
15650 child_die = die->child;
15651 while (child_die && child_die->tag)
15652 {
15653 if (child_die->tag != DW_TAG_enumerator)
15654 {
15655 process_die (child_die, cu);
15656 }
15657 else
15658 {
15659 name = dwarf2_name (child_die, cu);
15660 if (name)
15661 {
15662 sym = new_symbol (child_die, this_type, cu);
15663
15664 fields.emplace_back ();
15665 struct field &field = fields.back ();
15666
15667 FIELD_NAME (field) = sym->linkage_name ();
15668 FIELD_TYPE (field) = NULL;
15669 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15670 FIELD_BITSIZE (field) = 0;
15671 }
15672 }
15673
15674 child_die = sibling_die (child_die);
15675 }
15676
15677 if (!fields.empty ())
15678 {
15679 TYPE_NFIELDS (this_type) = fields.size ();
15680 TYPE_FIELDS (this_type) = (struct field *)
15681 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15682 memcpy (TYPE_FIELDS (this_type), fields.data (),
15683 sizeof (struct field) * fields.size ());
15684 }
15685 }
15686
15687 /* If we are reading an enum from a .debug_types unit, and the enum
15688 is a declaration, and the enum is not the signatured type in the
15689 unit, then we do not want to add a symbol for it. Adding a
15690 symbol would in some cases obscure the true definition of the
15691 enum, giving users an incomplete type when the definition is
15692 actually available. Note that we do not want to do this for all
15693 enums which are just declarations, because C++0x allows forward
15694 enum declarations. */
15695 if (cu->per_cu->is_debug_types
15696 && die_is_declaration (die, cu))
15697 {
15698 struct signatured_type *sig_type;
15699
15700 sig_type = (struct signatured_type *) cu->per_cu;
15701 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15702 if (sig_type->type_offset_in_section != die->sect_off)
15703 return;
15704 }
15705
15706 new_symbol (die, this_type, cu);
15707 }
15708
15709 /* Extract all information from a DW_TAG_array_type DIE and put it in
15710 the DIE's type field. For now, this only handles one dimensional
15711 arrays. */
15712
15713 static struct type *
15714 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15715 {
15716 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15717 struct die_info *child_die;
15718 struct type *type;
15719 struct type *element_type, *range_type, *index_type;
15720 struct attribute *attr;
15721 const char *name;
15722 struct dynamic_prop *byte_stride_prop = NULL;
15723 unsigned int bit_stride = 0;
15724
15725 element_type = die_type (die, cu);
15726
15727 /* The die_type call above may have already set the type for this DIE. */
15728 type = get_die_type (die, cu);
15729 if (type)
15730 return type;
15731
15732 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15733 if (attr != NULL)
15734 {
15735 int stride_ok;
15736 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15737
15738 byte_stride_prop
15739 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15740 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15741 prop_type);
15742 if (!stride_ok)
15743 {
15744 complaint (_("unable to read array DW_AT_byte_stride "
15745 " - DIE at %s [in module %s]"),
15746 sect_offset_str (die->sect_off),
15747 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15748 /* Ignore this attribute. We will likely not be able to print
15749 arrays of this type correctly, but there is little we can do
15750 to help if we cannot read the attribute's value. */
15751 byte_stride_prop = NULL;
15752 }
15753 }
15754
15755 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15756 if (attr != NULL)
15757 bit_stride = DW_UNSND (attr);
15758
15759 /* Irix 6.2 native cc creates array types without children for
15760 arrays with unspecified length. */
15761 if (die->child == NULL)
15762 {
15763 index_type = objfile_type (objfile)->builtin_int;
15764 range_type = create_static_range_type (NULL, index_type, 0, -1);
15765 type = create_array_type_with_stride (NULL, element_type, range_type,
15766 byte_stride_prop, bit_stride);
15767 return set_die_type (die, type, cu);
15768 }
15769
15770 std::vector<struct type *> range_types;
15771 child_die = die->child;
15772 while (child_die && child_die->tag)
15773 {
15774 if (child_die->tag == DW_TAG_subrange_type)
15775 {
15776 struct type *child_type = read_type_die (child_die, cu);
15777
15778 if (child_type != NULL)
15779 {
15780 /* The range type was succesfully read. Save it for the
15781 array type creation. */
15782 range_types.push_back (child_type);
15783 }
15784 }
15785 child_die = sibling_die (child_die);
15786 }
15787
15788 /* Dwarf2 dimensions are output from left to right, create the
15789 necessary array types in backwards order. */
15790
15791 type = element_type;
15792
15793 if (read_array_order (die, cu) == DW_ORD_col_major)
15794 {
15795 int i = 0;
15796
15797 while (i < range_types.size ())
15798 type = create_array_type_with_stride (NULL, type, range_types[i++],
15799 byte_stride_prop, bit_stride);
15800 }
15801 else
15802 {
15803 size_t ndim = range_types.size ();
15804 while (ndim-- > 0)
15805 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15806 byte_stride_prop, bit_stride);
15807 }
15808
15809 /* Understand Dwarf2 support for vector types (like they occur on
15810 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15811 array type. This is not part of the Dwarf2/3 standard yet, but a
15812 custom vendor extension. The main difference between a regular
15813 array and the vector variant is that vectors are passed by value
15814 to functions. */
15815 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15816 if (attr != nullptr)
15817 make_vector_type (type);
15818
15819 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15820 implementation may choose to implement triple vectors using this
15821 attribute. */
15822 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15823 if (attr != nullptr)
15824 {
15825 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15826 TYPE_LENGTH (type) = DW_UNSND (attr);
15827 else
15828 complaint (_("DW_AT_byte_size for array type smaller "
15829 "than the total size of elements"));
15830 }
15831
15832 name = dwarf2_name (die, cu);
15833 if (name)
15834 TYPE_NAME (type) = name;
15835
15836 maybe_set_alignment (cu, die, type);
15837
15838 /* Install the type in the die. */
15839 set_die_type (die, type, cu);
15840
15841 /* set_die_type should be already done. */
15842 set_descriptive_type (type, die, cu);
15843
15844 return type;
15845 }
15846
15847 static enum dwarf_array_dim_ordering
15848 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15849 {
15850 struct attribute *attr;
15851
15852 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15853
15854 if (attr != nullptr)
15855 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15856
15857 /* GNU F77 is a special case, as at 08/2004 array type info is the
15858 opposite order to the dwarf2 specification, but data is still
15859 laid out as per normal fortran.
15860
15861 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15862 version checking. */
15863
15864 if (cu->language == language_fortran
15865 && cu->producer && strstr (cu->producer, "GNU F77"))
15866 {
15867 return DW_ORD_row_major;
15868 }
15869
15870 switch (cu->language_defn->la_array_ordering)
15871 {
15872 case array_column_major:
15873 return DW_ORD_col_major;
15874 case array_row_major:
15875 default:
15876 return DW_ORD_row_major;
15877 };
15878 }
15879
15880 /* Extract all information from a DW_TAG_set_type DIE and put it in
15881 the DIE's type field. */
15882
15883 static struct type *
15884 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15885 {
15886 struct type *domain_type, *set_type;
15887 struct attribute *attr;
15888
15889 domain_type = die_type (die, cu);
15890
15891 /* The die_type call above may have already set the type for this DIE. */
15892 set_type = get_die_type (die, cu);
15893 if (set_type)
15894 return set_type;
15895
15896 set_type = create_set_type (NULL, domain_type);
15897
15898 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15899 if (attr != nullptr)
15900 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15901
15902 maybe_set_alignment (cu, die, set_type);
15903
15904 return set_die_type (die, set_type, cu);
15905 }
15906
15907 /* A helper for read_common_block that creates a locexpr baton.
15908 SYM is the symbol which we are marking as computed.
15909 COMMON_DIE is the DIE for the common block.
15910 COMMON_LOC is the location expression attribute for the common
15911 block itself.
15912 MEMBER_LOC is the location expression attribute for the particular
15913 member of the common block that we are processing.
15914 CU is the CU from which the above come. */
15915
15916 static void
15917 mark_common_block_symbol_computed (struct symbol *sym,
15918 struct die_info *common_die,
15919 struct attribute *common_loc,
15920 struct attribute *member_loc,
15921 struct dwarf2_cu *cu)
15922 {
15923 struct dwarf2_per_objfile *dwarf2_per_objfile
15924 = cu->per_cu->dwarf2_per_objfile;
15925 struct objfile *objfile = dwarf2_per_objfile->objfile;
15926 struct dwarf2_locexpr_baton *baton;
15927 gdb_byte *ptr;
15928 unsigned int cu_off;
15929 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15930 LONGEST offset = 0;
15931
15932 gdb_assert (common_loc && member_loc);
15933 gdb_assert (common_loc->form_is_block ());
15934 gdb_assert (member_loc->form_is_block ()
15935 || member_loc->form_is_constant ());
15936
15937 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15938 baton->per_cu = cu->per_cu;
15939 gdb_assert (baton->per_cu);
15940
15941 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15942
15943 if (member_loc->form_is_constant ())
15944 {
15945 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15946 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15947 }
15948 else
15949 baton->size += DW_BLOCK (member_loc)->size;
15950
15951 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15952 baton->data = ptr;
15953
15954 *ptr++ = DW_OP_call4;
15955 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15956 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15957 ptr += 4;
15958
15959 if (member_loc->form_is_constant ())
15960 {
15961 *ptr++ = DW_OP_addr;
15962 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15963 ptr += cu->header.addr_size;
15964 }
15965 else
15966 {
15967 /* We have to copy the data here, because DW_OP_call4 will only
15968 use a DW_AT_location attribute. */
15969 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15970 ptr += DW_BLOCK (member_loc)->size;
15971 }
15972
15973 *ptr++ = DW_OP_plus;
15974 gdb_assert (ptr - baton->data == baton->size);
15975
15976 SYMBOL_LOCATION_BATON (sym) = baton;
15977 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15978 }
15979
15980 /* Create appropriate locally-scoped variables for all the
15981 DW_TAG_common_block entries. Also create a struct common_block
15982 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15983 is used to separate the common blocks name namespace from regular
15984 variable names. */
15985
15986 static void
15987 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15988 {
15989 struct attribute *attr;
15990
15991 attr = dwarf2_attr (die, DW_AT_location, cu);
15992 if (attr != nullptr)
15993 {
15994 /* Support the .debug_loc offsets. */
15995 if (attr->form_is_block ())
15996 {
15997 /* Ok. */
15998 }
15999 else if (attr->form_is_section_offset ())
16000 {
16001 dwarf2_complex_location_expr_complaint ();
16002 attr = NULL;
16003 }
16004 else
16005 {
16006 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16007 "common block member");
16008 attr = NULL;
16009 }
16010 }
16011
16012 if (die->child != NULL)
16013 {
16014 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16015 struct die_info *child_die;
16016 size_t n_entries = 0, size;
16017 struct common_block *common_block;
16018 struct symbol *sym;
16019
16020 for (child_die = die->child;
16021 child_die && child_die->tag;
16022 child_die = sibling_die (child_die))
16023 ++n_entries;
16024
16025 size = (sizeof (struct common_block)
16026 + (n_entries - 1) * sizeof (struct symbol *));
16027 common_block
16028 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16029 size);
16030 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16031 common_block->n_entries = 0;
16032
16033 for (child_die = die->child;
16034 child_die && child_die->tag;
16035 child_die = sibling_die (child_die))
16036 {
16037 /* Create the symbol in the DW_TAG_common_block block in the current
16038 symbol scope. */
16039 sym = new_symbol (child_die, NULL, cu);
16040 if (sym != NULL)
16041 {
16042 struct attribute *member_loc;
16043
16044 common_block->contents[common_block->n_entries++] = sym;
16045
16046 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16047 cu);
16048 if (member_loc)
16049 {
16050 /* GDB has handled this for a long time, but it is
16051 not specified by DWARF. It seems to have been
16052 emitted by gfortran at least as recently as:
16053 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16054 complaint (_("Variable in common block has "
16055 "DW_AT_data_member_location "
16056 "- DIE at %s [in module %s]"),
16057 sect_offset_str (child_die->sect_off),
16058 objfile_name (objfile));
16059
16060 if (member_loc->form_is_section_offset ())
16061 dwarf2_complex_location_expr_complaint ();
16062 else if (member_loc->form_is_constant ()
16063 || member_loc->form_is_block ())
16064 {
16065 if (attr != nullptr)
16066 mark_common_block_symbol_computed (sym, die, attr,
16067 member_loc, cu);
16068 }
16069 else
16070 dwarf2_complex_location_expr_complaint ();
16071 }
16072 }
16073 }
16074
16075 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16076 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16077 }
16078 }
16079
16080 /* Create a type for a C++ namespace. */
16081
16082 static struct type *
16083 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16084 {
16085 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16086 const char *previous_prefix, *name;
16087 int is_anonymous;
16088 struct type *type;
16089
16090 /* For extensions, reuse the type of the original namespace. */
16091 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16092 {
16093 struct die_info *ext_die;
16094 struct dwarf2_cu *ext_cu = cu;
16095
16096 ext_die = dwarf2_extension (die, &ext_cu);
16097 type = read_type_die (ext_die, ext_cu);
16098
16099 /* EXT_CU may not be the same as CU.
16100 Ensure TYPE is recorded with CU in die_type_hash. */
16101 return set_die_type (die, type, cu);
16102 }
16103
16104 name = namespace_name (die, &is_anonymous, cu);
16105
16106 /* Now build the name of the current namespace. */
16107
16108 previous_prefix = determine_prefix (die, cu);
16109 if (previous_prefix[0] != '\0')
16110 name = typename_concat (&objfile->objfile_obstack,
16111 previous_prefix, name, 0, cu);
16112
16113 /* Create the type. */
16114 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16115
16116 return set_die_type (die, type, cu);
16117 }
16118
16119 /* Read a namespace scope. */
16120
16121 static void
16122 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16123 {
16124 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16125 int is_anonymous;
16126
16127 /* Add a symbol associated to this if we haven't seen the namespace
16128 before. Also, add a using directive if it's an anonymous
16129 namespace. */
16130
16131 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16132 {
16133 struct type *type;
16134
16135 type = read_type_die (die, cu);
16136 new_symbol (die, type, cu);
16137
16138 namespace_name (die, &is_anonymous, cu);
16139 if (is_anonymous)
16140 {
16141 const char *previous_prefix = determine_prefix (die, cu);
16142
16143 std::vector<const char *> excludes;
16144 add_using_directive (using_directives (cu),
16145 previous_prefix, TYPE_NAME (type), NULL,
16146 NULL, excludes, 0, &objfile->objfile_obstack);
16147 }
16148 }
16149
16150 if (die->child != NULL)
16151 {
16152 struct die_info *child_die = die->child;
16153
16154 while (child_die && child_die->tag)
16155 {
16156 process_die (child_die, cu);
16157 child_die = sibling_die (child_die);
16158 }
16159 }
16160 }
16161
16162 /* Read a Fortran module as type. This DIE can be only a declaration used for
16163 imported module. Still we need that type as local Fortran "use ... only"
16164 declaration imports depend on the created type in determine_prefix. */
16165
16166 static struct type *
16167 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16168 {
16169 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16170 const char *module_name;
16171 struct type *type;
16172
16173 module_name = dwarf2_name (die, cu);
16174 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16175
16176 return set_die_type (die, type, cu);
16177 }
16178
16179 /* Read a Fortran module. */
16180
16181 static void
16182 read_module (struct die_info *die, struct dwarf2_cu *cu)
16183 {
16184 struct die_info *child_die = die->child;
16185 struct type *type;
16186
16187 type = read_type_die (die, cu);
16188 new_symbol (die, type, cu);
16189
16190 while (child_die && child_die->tag)
16191 {
16192 process_die (child_die, cu);
16193 child_die = sibling_die (child_die);
16194 }
16195 }
16196
16197 /* Return the name of the namespace represented by DIE. Set
16198 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16199 namespace. */
16200
16201 static const char *
16202 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16203 {
16204 struct die_info *current_die;
16205 const char *name = NULL;
16206
16207 /* Loop through the extensions until we find a name. */
16208
16209 for (current_die = die;
16210 current_die != NULL;
16211 current_die = dwarf2_extension (die, &cu))
16212 {
16213 /* We don't use dwarf2_name here so that we can detect the absence
16214 of a name -> anonymous namespace. */
16215 name = dwarf2_string_attr (die, DW_AT_name, cu);
16216
16217 if (name != NULL)
16218 break;
16219 }
16220
16221 /* Is it an anonymous namespace? */
16222
16223 *is_anonymous = (name == NULL);
16224 if (*is_anonymous)
16225 name = CP_ANONYMOUS_NAMESPACE_STR;
16226
16227 return name;
16228 }
16229
16230 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16231 the user defined type vector. */
16232
16233 static struct type *
16234 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16235 {
16236 struct gdbarch *gdbarch
16237 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16238 struct comp_unit_head *cu_header = &cu->header;
16239 struct type *type;
16240 struct attribute *attr_byte_size;
16241 struct attribute *attr_address_class;
16242 int byte_size, addr_class;
16243 struct type *target_type;
16244
16245 target_type = die_type (die, cu);
16246
16247 /* The die_type call above may have already set the type for this DIE. */
16248 type = get_die_type (die, cu);
16249 if (type)
16250 return type;
16251
16252 type = lookup_pointer_type (target_type);
16253
16254 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16255 if (attr_byte_size)
16256 byte_size = DW_UNSND (attr_byte_size);
16257 else
16258 byte_size = cu_header->addr_size;
16259
16260 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16261 if (attr_address_class)
16262 addr_class = DW_UNSND (attr_address_class);
16263 else
16264 addr_class = DW_ADDR_none;
16265
16266 ULONGEST alignment = get_alignment (cu, die);
16267
16268 /* If the pointer size, alignment, or address class is different
16269 than the default, create a type variant marked as such and set
16270 the length accordingly. */
16271 if (TYPE_LENGTH (type) != byte_size
16272 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16273 && alignment != TYPE_RAW_ALIGN (type))
16274 || addr_class != DW_ADDR_none)
16275 {
16276 if (gdbarch_address_class_type_flags_p (gdbarch))
16277 {
16278 int type_flags;
16279
16280 type_flags = gdbarch_address_class_type_flags
16281 (gdbarch, byte_size, addr_class);
16282 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16283 == 0);
16284 type = make_type_with_address_space (type, type_flags);
16285 }
16286 else if (TYPE_LENGTH (type) != byte_size)
16287 {
16288 complaint (_("invalid pointer size %d"), byte_size);
16289 }
16290 else if (TYPE_RAW_ALIGN (type) != alignment)
16291 {
16292 complaint (_("Invalid DW_AT_alignment"
16293 " - DIE at %s [in module %s]"),
16294 sect_offset_str (die->sect_off),
16295 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16296 }
16297 else
16298 {
16299 /* Should we also complain about unhandled address classes? */
16300 }
16301 }
16302
16303 TYPE_LENGTH (type) = byte_size;
16304 set_type_align (type, alignment);
16305 return set_die_type (die, type, cu);
16306 }
16307
16308 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16309 the user defined type vector. */
16310
16311 static struct type *
16312 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16313 {
16314 struct type *type;
16315 struct type *to_type;
16316 struct type *domain;
16317
16318 to_type = die_type (die, cu);
16319 domain = die_containing_type (die, cu);
16320
16321 /* The calls above may have already set the type for this DIE. */
16322 type = get_die_type (die, cu);
16323 if (type)
16324 return type;
16325
16326 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16327 type = lookup_methodptr_type (to_type);
16328 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16329 {
16330 struct type *new_type
16331 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16332
16333 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16334 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16335 TYPE_VARARGS (to_type));
16336 type = lookup_methodptr_type (new_type);
16337 }
16338 else
16339 type = lookup_memberptr_type (to_type, domain);
16340
16341 return set_die_type (die, type, cu);
16342 }
16343
16344 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16345 the user defined type vector. */
16346
16347 static struct type *
16348 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16349 enum type_code refcode)
16350 {
16351 struct comp_unit_head *cu_header = &cu->header;
16352 struct type *type, *target_type;
16353 struct attribute *attr;
16354
16355 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16356
16357 target_type = die_type (die, cu);
16358
16359 /* The die_type call above may have already set the type for this DIE. */
16360 type = get_die_type (die, cu);
16361 if (type)
16362 return type;
16363
16364 type = lookup_reference_type (target_type, refcode);
16365 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16366 if (attr != nullptr)
16367 {
16368 TYPE_LENGTH (type) = DW_UNSND (attr);
16369 }
16370 else
16371 {
16372 TYPE_LENGTH (type) = cu_header->addr_size;
16373 }
16374 maybe_set_alignment (cu, die, type);
16375 return set_die_type (die, type, cu);
16376 }
16377
16378 /* Add the given cv-qualifiers to the element type of the array. GCC
16379 outputs DWARF type qualifiers that apply to an array, not the
16380 element type. But GDB relies on the array element type to carry
16381 the cv-qualifiers. This mimics section 6.7.3 of the C99
16382 specification. */
16383
16384 static struct type *
16385 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16386 struct type *base_type, int cnst, int voltl)
16387 {
16388 struct type *el_type, *inner_array;
16389
16390 base_type = copy_type (base_type);
16391 inner_array = base_type;
16392
16393 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16394 {
16395 TYPE_TARGET_TYPE (inner_array) =
16396 copy_type (TYPE_TARGET_TYPE (inner_array));
16397 inner_array = TYPE_TARGET_TYPE (inner_array);
16398 }
16399
16400 el_type = TYPE_TARGET_TYPE (inner_array);
16401 cnst |= TYPE_CONST (el_type);
16402 voltl |= TYPE_VOLATILE (el_type);
16403 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16404
16405 return set_die_type (die, base_type, cu);
16406 }
16407
16408 static struct type *
16409 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16410 {
16411 struct type *base_type, *cv_type;
16412
16413 base_type = die_type (die, cu);
16414
16415 /* The die_type call above may have already set the type for this DIE. */
16416 cv_type = get_die_type (die, cu);
16417 if (cv_type)
16418 return cv_type;
16419
16420 /* In case the const qualifier is applied to an array type, the element type
16421 is so qualified, not the array type (section 6.7.3 of C99). */
16422 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16423 return add_array_cv_type (die, cu, base_type, 1, 0);
16424
16425 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16426 return set_die_type (die, cv_type, cu);
16427 }
16428
16429 static struct type *
16430 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16431 {
16432 struct type *base_type, *cv_type;
16433
16434 base_type = die_type (die, cu);
16435
16436 /* The die_type call above may have already set the type for this DIE. */
16437 cv_type = get_die_type (die, cu);
16438 if (cv_type)
16439 return cv_type;
16440
16441 /* In case the volatile qualifier is applied to an array type, the
16442 element type is so qualified, not the array type (section 6.7.3
16443 of C99). */
16444 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16445 return add_array_cv_type (die, cu, base_type, 0, 1);
16446
16447 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16448 return set_die_type (die, cv_type, cu);
16449 }
16450
16451 /* Handle DW_TAG_restrict_type. */
16452
16453 static struct type *
16454 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16455 {
16456 struct type *base_type, *cv_type;
16457
16458 base_type = die_type (die, cu);
16459
16460 /* The die_type call above may have already set the type for this DIE. */
16461 cv_type = get_die_type (die, cu);
16462 if (cv_type)
16463 return cv_type;
16464
16465 cv_type = make_restrict_type (base_type);
16466 return set_die_type (die, cv_type, cu);
16467 }
16468
16469 /* Handle DW_TAG_atomic_type. */
16470
16471 static struct type *
16472 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16473 {
16474 struct type *base_type, *cv_type;
16475
16476 base_type = die_type (die, cu);
16477
16478 /* The die_type call above may have already set the type for this DIE. */
16479 cv_type = get_die_type (die, cu);
16480 if (cv_type)
16481 return cv_type;
16482
16483 cv_type = make_atomic_type (base_type);
16484 return set_die_type (die, cv_type, cu);
16485 }
16486
16487 /* Extract all information from a DW_TAG_string_type DIE and add to
16488 the user defined type vector. It isn't really a user defined type,
16489 but it behaves like one, with other DIE's using an AT_user_def_type
16490 attribute to reference it. */
16491
16492 static struct type *
16493 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16494 {
16495 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16496 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16497 struct type *type, *range_type, *index_type, *char_type;
16498 struct attribute *attr;
16499 struct dynamic_prop prop;
16500 bool length_is_constant = true;
16501 LONGEST length;
16502
16503 /* There are a couple of places where bit sizes might be made use of
16504 when parsing a DW_TAG_string_type, however, no producer that we know
16505 of make use of these. Handling bit sizes that are a multiple of the
16506 byte size is easy enough, but what about other bit sizes? Lets deal
16507 with that problem when we have to. Warn about these attributes being
16508 unsupported, then parse the type and ignore them like we always
16509 have. */
16510 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16511 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16512 {
16513 static bool warning_printed = false;
16514 if (!warning_printed)
16515 {
16516 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16517 "currently supported on DW_TAG_string_type."));
16518 warning_printed = true;
16519 }
16520 }
16521
16522 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16523 if (attr != nullptr && !attr->form_is_constant ())
16524 {
16525 /* The string length describes the location at which the length of
16526 the string can be found. The size of the length field can be
16527 specified with one of the attributes below. */
16528 struct type *prop_type;
16529 struct attribute *len
16530 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16531 if (len == nullptr)
16532 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16533 if (len != nullptr && len->form_is_constant ())
16534 {
16535 /* Pass 0 as the default as we know this attribute is constant
16536 and the default value will not be returned. */
16537 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16538 prop_type = cu->per_cu->int_type (sz, true);
16539 }
16540 else
16541 {
16542 /* If the size is not specified then we assume it is the size of
16543 an address on this target. */
16544 prop_type = cu->per_cu->addr_sized_int_type (true);
16545 }
16546
16547 /* Convert the attribute into a dynamic property. */
16548 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16549 length = 1;
16550 else
16551 length_is_constant = false;
16552 }
16553 else if (attr != nullptr)
16554 {
16555 /* This DW_AT_string_length just contains the length with no
16556 indirection. There's no need to create a dynamic property in this
16557 case. Pass 0 for the default value as we know it will not be
16558 returned in this case. */
16559 length = dwarf2_get_attr_constant_value (attr, 0);
16560 }
16561 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16562 {
16563 /* We don't currently support non-constant byte sizes for strings. */
16564 length = dwarf2_get_attr_constant_value (attr, 1);
16565 }
16566 else
16567 {
16568 /* Use 1 as a fallback length if we have nothing else. */
16569 length = 1;
16570 }
16571
16572 index_type = objfile_type (objfile)->builtin_int;
16573 if (length_is_constant)
16574 range_type = create_static_range_type (NULL, index_type, 1, length);
16575 else
16576 {
16577 struct dynamic_prop low_bound;
16578
16579 low_bound.kind = PROP_CONST;
16580 low_bound.data.const_val = 1;
16581 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16582 }
16583 char_type = language_string_char_type (cu->language_defn, gdbarch);
16584 type = create_string_type (NULL, char_type, range_type);
16585
16586 return set_die_type (die, type, cu);
16587 }
16588
16589 /* Assuming that DIE corresponds to a function, returns nonzero
16590 if the function is prototyped. */
16591
16592 static int
16593 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16594 {
16595 struct attribute *attr;
16596
16597 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16598 if (attr && (DW_UNSND (attr) != 0))
16599 return 1;
16600
16601 /* The DWARF standard implies that the DW_AT_prototyped attribute
16602 is only meaningful for C, but the concept also extends to other
16603 languages that allow unprototyped functions (Eg: Objective C).
16604 For all other languages, assume that functions are always
16605 prototyped. */
16606 if (cu->language != language_c
16607 && cu->language != language_objc
16608 && cu->language != language_opencl)
16609 return 1;
16610
16611 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16612 prototyped and unprototyped functions; default to prototyped,
16613 since that is more common in modern code (and RealView warns
16614 about unprototyped functions). */
16615 if (producer_is_realview (cu->producer))
16616 return 1;
16617
16618 return 0;
16619 }
16620
16621 /* Handle DIES due to C code like:
16622
16623 struct foo
16624 {
16625 int (*funcp)(int a, long l);
16626 int b;
16627 };
16628
16629 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16630
16631 static struct type *
16632 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16633 {
16634 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16635 struct type *type; /* Type that this function returns. */
16636 struct type *ftype; /* Function that returns above type. */
16637 struct attribute *attr;
16638
16639 type = die_type (die, cu);
16640
16641 /* The die_type call above may have already set the type for this DIE. */
16642 ftype = get_die_type (die, cu);
16643 if (ftype)
16644 return ftype;
16645
16646 ftype = lookup_function_type (type);
16647
16648 if (prototyped_function_p (die, cu))
16649 TYPE_PROTOTYPED (ftype) = 1;
16650
16651 /* Store the calling convention in the type if it's available in
16652 the subroutine die. Otherwise set the calling convention to
16653 the default value DW_CC_normal. */
16654 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16655 if (attr != nullptr
16656 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16657 TYPE_CALLING_CONVENTION (ftype)
16658 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16659 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16660 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16661 else
16662 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16663
16664 /* Record whether the function returns normally to its caller or not
16665 if the DWARF producer set that information. */
16666 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16667 if (attr && (DW_UNSND (attr) != 0))
16668 TYPE_NO_RETURN (ftype) = 1;
16669
16670 /* We need to add the subroutine type to the die immediately so
16671 we don't infinitely recurse when dealing with parameters
16672 declared as the same subroutine type. */
16673 set_die_type (die, ftype, cu);
16674
16675 if (die->child != NULL)
16676 {
16677 struct type *void_type = objfile_type (objfile)->builtin_void;
16678 struct die_info *child_die;
16679 int nparams, iparams;
16680
16681 /* Count the number of parameters.
16682 FIXME: GDB currently ignores vararg functions, but knows about
16683 vararg member functions. */
16684 nparams = 0;
16685 child_die = die->child;
16686 while (child_die && child_die->tag)
16687 {
16688 if (child_die->tag == DW_TAG_formal_parameter)
16689 nparams++;
16690 else if (child_die->tag == DW_TAG_unspecified_parameters)
16691 TYPE_VARARGS (ftype) = 1;
16692 child_die = sibling_die (child_die);
16693 }
16694
16695 /* Allocate storage for parameters and fill them in. */
16696 TYPE_NFIELDS (ftype) = nparams;
16697 TYPE_FIELDS (ftype) = (struct field *)
16698 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16699
16700 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16701 even if we error out during the parameters reading below. */
16702 for (iparams = 0; iparams < nparams; iparams++)
16703 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16704
16705 iparams = 0;
16706 child_die = die->child;
16707 while (child_die && child_die->tag)
16708 {
16709 if (child_die->tag == DW_TAG_formal_parameter)
16710 {
16711 struct type *arg_type;
16712
16713 /* DWARF version 2 has no clean way to discern C++
16714 static and non-static member functions. G++ helps
16715 GDB by marking the first parameter for non-static
16716 member functions (which is the this pointer) as
16717 artificial. We pass this information to
16718 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16719
16720 DWARF version 3 added DW_AT_object_pointer, which GCC
16721 4.5 does not yet generate. */
16722 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16723 if (attr != nullptr)
16724 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16725 else
16726 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16727 arg_type = die_type (child_die, cu);
16728
16729 /* RealView does not mark THIS as const, which the testsuite
16730 expects. GCC marks THIS as const in method definitions,
16731 but not in the class specifications (GCC PR 43053). */
16732 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16733 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16734 {
16735 int is_this = 0;
16736 struct dwarf2_cu *arg_cu = cu;
16737 const char *name = dwarf2_name (child_die, cu);
16738
16739 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16740 if (attr != nullptr)
16741 {
16742 /* If the compiler emits this, use it. */
16743 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16744 is_this = 1;
16745 }
16746 else if (name && strcmp (name, "this") == 0)
16747 /* Function definitions will have the argument names. */
16748 is_this = 1;
16749 else if (name == NULL && iparams == 0)
16750 /* Declarations may not have the names, so like
16751 elsewhere in GDB, assume an artificial first
16752 argument is "this". */
16753 is_this = 1;
16754
16755 if (is_this)
16756 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16757 arg_type, 0);
16758 }
16759
16760 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16761 iparams++;
16762 }
16763 child_die = sibling_die (child_die);
16764 }
16765 }
16766
16767 return ftype;
16768 }
16769
16770 static struct type *
16771 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16772 {
16773 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16774 const char *name = NULL;
16775 struct type *this_type, *target_type;
16776
16777 name = dwarf2_full_name (NULL, die, cu);
16778 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16779 TYPE_TARGET_STUB (this_type) = 1;
16780 set_die_type (die, this_type, cu);
16781 target_type = die_type (die, cu);
16782 if (target_type != this_type)
16783 TYPE_TARGET_TYPE (this_type) = target_type;
16784 else
16785 {
16786 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16787 spec and cause infinite loops in GDB. */
16788 complaint (_("Self-referential DW_TAG_typedef "
16789 "- DIE at %s [in module %s]"),
16790 sect_offset_str (die->sect_off), objfile_name (objfile));
16791 TYPE_TARGET_TYPE (this_type) = NULL;
16792 }
16793 return this_type;
16794 }
16795
16796 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16797 (which may be different from NAME) to the architecture back-end to allow
16798 it to guess the correct format if necessary. */
16799
16800 static struct type *
16801 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16802 const char *name_hint, enum bfd_endian byte_order)
16803 {
16804 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16805 const struct floatformat **format;
16806 struct type *type;
16807
16808 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16809 if (format)
16810 type = init_float_type (objfile, bits, name, format, byte_order);
16811 else
16812 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16813
16814 return type;
16815 }
16816
16817 /* Allocate an integer type of size BITS and name NAME. */
16818
16819 static struct type *
16820 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16821 int bits, int unsigned_p, const char *name)
16822 {
16823 struct type *type;
16824
16825 /* Versions of Intel's C Compiler generate an integer type called "void"
16826 instead of using DW_TAG_unspecified_type. This has been seen on
16827 at least versions 14, 17, and 18. */
16828 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16829 && strcmp (name, "void") == 0)
16830 type = objfile_type (objfile)->builtin_void;
16831 else
16832 type = init_integer_type (objfile, bits, unsigned_p, name);
16833
16834 return type;
16835 }
16836
16837 /* Initialise and return a floating point type of size BITS suitable for
16838 use as a component of a complex number. The NAME_HINT is passed through
16839 when initialising the floating point type and is the name of the complex
16840 type.
16841
16842 As DWARF doesn't currently provide an explicit name for the components
16843 of a complex number, but it can be helpful to have these components
16844 named, we try to select a suitable name based on the size of the
16845 component. */
16846 static struct type *
16847 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16848 struct objfile *objfile,
16849 int bits, const char *name_hint,
16850 enum bfd_endian byte_order)
16851 {
16852 gdbarch *gdbarch = get_objfile_arch (objfile);
16853 struct type *tt = nullptr;
16854
16855 /* Try to find a suitable floating point builtin type of size BITS.
16856 We're going to use the name of this type as the name for the complex
16857 target type that we are about to create. */
16858 switch (cu->language)
16859 {
16860 case language_fortran:
16861 switch (bits)
16862 {
16863 case 32:
16864 tt = builtin_f_type (gdbarch)->builtin_real;
16865 break;
16866 case 64:
16867 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16868 break;
16869 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16870 case 128:
16871 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16872 break;
16873 }
16874 break;
16875 default:
16876 switch (bits)
16877 {
16878 case 32:
16879 tt = builtin_type (gdbarch)->builtin_float;
16880 break;
16881 case 64:
16882 tt = builtin_type (gdbarch)->builtin_double;
16883 break;
16884 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16885 case 128:
16886 tt = builtin_type (gdbarch)->builtin_long_double;
16887 break;
16888 }
16889 break;
16890 }
16891
16892 /* If the type we found doesn't match the size we were looking for, then
16893 pretend we didn't find a type at all, the complex target type we
16894 create will then be nameless. */
16895 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16896 tt = nullptr;
16897
16898 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16899 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16900 }
16901
16902 /* Find a representation of a given base type and install
16903 it in the TYPE field of the die. */
16904
16905 static struct type *
16906 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16907 {
16908 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16909 struct type *type;
16910 struct attribute *attr;
16911 int encoding = 0, bits = 0;
16912 const char *name;
16913 gdbarch *arch;
16914
16915 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16916 if (attr != nullptr)
16917 encoding = DW_UNSND (attr);
16918 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16919 if (attr != nullptr)
16920 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16921 name = dwarf2_name (die, cu);
16922 if (!name)
16923 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16924
16925 arch = get_objfile_arch (objfile);
16926 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16927
16928 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16929 if (attr)
16930 {
16931 int endianity = DW_UNSND (attr);
16932
16933 switch (endianity)
16934 {
16935 case DW_END_big:
16936 byte_order = BFD_ENDIAN_BIG;
16937 break;
16938 case DW_END_little:
16939 byte_order = BFD_ENDIAN_LITTLE;
16940 break;
16941 default:
16942 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16943 break;
16944 }
16945 }
16946
16947 switch (encoding)
16948 {
16949 case DW_ATE_address:
16950 /* Turn DW_ATE_address into a void * pointer. */
16951 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16952 type = init_pointer_type (objfile, bits, name, type);
16953 break;
16954 case DW_ATE_boolean:
16955 type = init_boolean_type (objfile, bits, 1, name);
16956 break;
16957 case DW_ATE_complex_float:
16958 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16959 byte_order);
16960 type = init_complex_type (objfile, name, type);
16961 break;
16962 case DW_ATE_decimal_float:
16963 type = init_decfloat_type (objfile, bits, name);
16964 break;
16965 case DW_ATE_float:
16966 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16967 break;
16968 case DW_ATE_signed:
16969 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16970 break;
16971 case DW_ATE_unsigned:
16972 if (cu->language == language_fortran
16973 && name
16974 && startswith (name, "character("))
16975 type = init_character_type (objfile, bits, 1, name);
16976 else
16977 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16978 break;
16979 case DW_ATE_signed_char:
16980 if (cu->language == language_ada || cu->language == language_m2
16981 || cu->language == language_pascal
16982 || cu->language == language_fortran)
16983 type = init_character_type (objfile, bits, 0, name);
16984 else
16985 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16986 break;
16987 case DW_ATE_unsigned_char:
16988 if (cu->language == language_ada || cu->language == language_m2
16989 || cu->language == language_pascal
16990 || cu->language == language_fortran
16991 || cu->language == language_rust)
16992 type = init_character_type (objfile, bits, 1, name);
16993 else
16994 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16995 break;
16996 case DW_ATE_UTF:
16997 {
16998 if (bits == 16)
16999 type = builtin_type (arch)->builtin_char16;
17000 else if (bits == 32)
17001 type = builtin_type (arch)->builtin_char32;
17002 else
17003 {
17004 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17005 bits);
17006 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17007 }
17008 return set_die_type (die, type, cu);
17009 }
17010 break;
17011
17012 default:
17013 complaint (_("unsupported DW_AT_encoding: '%s'"),
17014 dwarf_type_encoding_name (encoding));
17015 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17016 break;
17017 }
17018
17019 if (name && strcmp (name, "char") == 0)
17020 TYPE_NOSIGN (type) = 1;
17021
17022 maybe_set_alignment (cu, die, type);
17023
17024 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17025
17026 return set_die_type (die, type, cu);
17027 }
17028
17029 /* Parse dwarf attribute if it's a block, reference or constant and put the
17030 resulting value of the attribute into struct bound_prop.
17031 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17032
17033 static int
17034 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17035 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17036 struct type *default_type)
17037 {
17038 struct dwarf2_property_baton *baton;
17039 struct obstack *obstack
17040 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17041
17042 gdb_assert (default_type != NULL);
17043
17044 if (attr == NULL || prop == NULL)
17045 return 0;
17046
17047 if (attr->form_is_block ())
17048 {
17049 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17050 baton->property_type = default_type;
17051 baton->locexpr.per_cu = cu->per_cu;
17052 baton->locexpr.size = DW_BLOCK (attr)->size;
17053 baton->locexpr.data = DW_BLOCK (attr)->data;
17054 switch (attr->name)
17055 {
17056 case DW_AT_string_length:
17057 baton->locexpr.is_reference = true;
17058 break;
17059 default:
17060 baton->locexpr.is_reference = false;
17061 break;
17062 }
17063 prop->data.baton = baton;
17064 prop->kind = PROP_LOCEXPR;
17065 gdb_assert (prop->data.baton != NULL);
17066 }
17067 else if (attr->form_is_ref ())
17068 {
17069 struct dwarf2_cu *target_cu = cu;
17070 struct die_info *target_die;
17071 struct attribute *target_attr;
17072
17073 target_die = follow_die_ref (die, attr, &target_cu);
17074 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17075 if (target_attr == NULL)
17076 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17077 target_cu);
17078 if (target_attr == NULL)
17079 return 0;
17080
17081 switch (target_attr->name)
17082 {
17083 case DW_AT_location:
17084 if (target_attr->form_is_section_offset ())
17085 {
17086 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17087 baton->property_type = die_type (target_die, target_cu);
17088 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17089 prop->data.baton = baton;
17090 prop->kind = PROP_LOCLIST;
17091 gdb_assert (prop->data.baton != NULL);
17092 }
17093 else if (target_attr->form_is_block ())
17094 {
17095 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17096 baton->property_type = die_type (target_die, target_cu);
17097 baton->locexpr.per_cu = cu->per_cu;
17098 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17099 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17100 baton->locexpr.is_reference = true;
17101 prop->data.baton = baton;
17102 prop->kind = PROP_LOCEXPR;
17103 gdb_assert (prop->data.baton != NULL);
17104 }
17105 else
17106 {
17107 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17108 "dynamic property");
17109 return 0;
17110 }
17111 break;
17112 case DW_AT_data_member_location:
17113 {
17114 LONGEST offset;
17115
17116 if (!handle_data_member_location (target_die, target_cu,
17117 &offset))
17118 return 0;
17119
17120 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17121 baton->property_type = read_type_die (target_die->parent,
17122 target_cu);
17123 baton->offset_info.offset = offset;
17124 baton->offset_info.type = die_type (target_die, target_cu);
17125 prop->data.baton = baton;
17126 prop->kind = PROP_ADDR_OFFSET;
17127 break;
17128 }
17129 }
17130 }
17131 else if (attr->form_is_constant ())
17132 {
17133 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17134 prop->kind = PROP_CONST;
17135 }
17136 else
17137 {
17138 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17139 dwarf2_name (die, cu));
17140 return 0;
17141 }
17142
17143 return 1;
17144 }
17145
17146 /* See read.h. */
17147
17148 struct type *
17149 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17150 {
17151 struct objfile *objfile = dwarf2_per_objfile->objfile;
17152 struct type *int_type;
17153
17154 /* Helper macro to examine the various builtin types. */
17155 #define TRY_TYPE(F) \
17156 int_type = (unsigned_p \
17157 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17158 : objfile_type (objfile)->builtin_ ## F); \
17159 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17160 return int_type
17161
17162 TRY_TYPE (char);
17163 TRY_TYPE (short);
17164 TRY_TYPE (int);
17165 TRY_TYPE (long);
17166 TRY_TYPE (long_long);
17167
17168 #undef TRY_TYPE
17169
17170 gdb_assert_not_reached ("unable to find suitable integer type");
17171 }
17172
17173 /* See read.h. */
17174
17175 struct type *
17176 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17177 {
17178 int addr_size = this->addr_size ();
17179 return int_type (addr_size, unsigned_p);
17180 }
17181
17182 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17183 present (which is valid) then compute the default type based on the
17184 compilation units address size. */
17185
17186 static struct type *
17187 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17188 {
17189 struct type *index_type = die_type (die, cu);
17190
17191 /* Dwarf-2 specifications explicitly allows to create subrange types
17192 without specifying a base type.
17193 In that case, the base type must be set to the type of
17194 the lower bound, upper bound or count, in that order, if any of these
17195 three attributes references an object that has a type.
17196 If no base type is found, the Dwarf-2 specifications say that
17197 a signed integer type of size equal to the size of an address should
17198 be used.
17199 For the following C code: `extern char gdb_int [];'
17200 GCC produces an empty range DIE.
17201 FIXME: muller/2010-05-28: Possible references to object for low bound,
17202 high bound or count are not yet handled by this code. */
17203 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17204 index_type = cu->per_cu->addr_sized_int_type (false);
17205
17206 return index_type;
17207 }
17208
17209 /* Read the given DW_AT_subrange DIE. */
17210
17211 static struct type *
17212 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17213 {
17214 struct type *base_type, *orig_base_type;
17215 struct type *range_type;
17216 struct attribute *attr;
17217 struct dynamic_prop low, high;
17218 int low_default_is_valid;
17219 int high_bound_is_count = 0;
17220 const char *name;
17221 ULONGEST negative_mask;
17222
17223 orig_base_type = read_subrange_index_type (die, cu);
17224
17225 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17226 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17227 creating the range type, but we use the result of check_typedef
17228 when examining properties of the type. */
17229 base_type = check_typedef (orig_base_type);
17230
17231 /* The die_type call above may have already set the type for this DIE. */
17232 range_type = get_die_type (die, cu);
17233 if (range_type)
17234 return range_type;
17235
17236 low.kind = PROP_CONST;
17237 high.kind = PROP_CONST;
17238 high.data.const_val = 0;
17239
17240 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17241 omitting DW_AT_lower_bound. */
17242 switch (cu->language)
17243 {
17244 case language_c:
17245 case language_cplus:
17246 low.data.const_val = 0;
17247 low_default_is_valid = 1;
17248 break;
17249 case language_fortran:
17250 low.data.const_val = 1;
17251 low_default_is_valid = 1;
17252 break;
17253 case language_d:
17254 case language_objc:
17255 case language_rust:
17256 low.data.const_val = 0;
17257 low_default_is_valid = (cu->header.version >= 4);
17258 break;
17259 case language_ada:
17260 case language_m2:
17261 case language_pascal:
17262 low.data.const_val = 1;
17263 low_default_is_valid = (cu->header.version >= 4);
17264 break;
17265 default:
17266 low.data.const_val = 0;
17267 low_default_is_valid = 0;
17268 break;
17269 }
17270
17271 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17272 if (attr != nullptr)
17273 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17274 else if (!low_default_is_valid)
17275 complaint (_("Missing DW_AT_lower_bound "
17276 "- DIE at %s [in module %s]"),
17277 sect_offset_str (die->sect_off),
17278 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17279
17280 struct attribute *attr_ub, *attr_count;
17281 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17282 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17283 {
17284 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17285 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17286 {
17287 /* If bounds are constant do the final calculation here. */
17288 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17289 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17290 else
17291 high_bound_is_count = 1;
17292 }
17293 else
17294 {
17295 if (attr_ub != NULL)
17296 complaint (_("Unresolved DW_AT_upper_bound "
17297 "- DIE at %s [in module %s]"),
17298 sect_offset_str (die->sect_off),
17299 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17300 if (attr_count != NULL)
17301 complaint (_("Unresolved DW_AT_count "
17302 "- DIE at %s [in module %s]"),
17303 sect_offset_str (die->sect_off),
17304 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17305 }
17306 }
17307
17308 LONGEST bias = 0;
17309 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17310 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17311 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17312
17313 /* Normally, the DWARF producers are expected to use a signed
17314 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17315 But this is unfortunately not always the case, as witnessed
17316 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17317 is used instead. To work around that ambiguity, we treat
17318 the bounds as signed, and thus sign-extend their values, when
17319 the base type is signed. */
17320 negative_mask =
17321 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17322 if (low.kind == PROP_CONST
17323 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17324 low.data.const_val |= negative_mask;
17325 if (high.kind == PROP_CONST
17326 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17327 high.data.const_val |= negative_mask;
17328
17329 /* Check for bit and byte strides. */
17330 struct dynamic_prop byte_stride_prop;
17331 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17332 if (attr_byte_stride != nullptr)
17333 {
17334 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17335 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17336 prop_type);
17337 }
17338
17339 struct dynamic_prop bit_stride_prop;
17340 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17341 if (attr_bit_stride != nullptr)
17342 {
17343 /* It only makes sense to have either a bit or byte stride. */
17344 if (attr_byte_stride != nullptr)
17345 {
17346 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17347 "- DIE at %s [in module %s]"),
17348 sect_offset_str (die->sect_off),
17349 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17350 attr_bit_stride = nullptr;
17351 }
17352 else
17353 {
17354 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17355 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17356 prop_type);
17357 }
17358 }
17359
17360 if (attr_byte_stride != nullptr
17361 || attr_bit_stride != nullptr)
17362 {
17363 bool byte_stride_p = (attr_byte_stride != nullptr);
17364 struct dynamic_prop *stride
17365 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17366
17367 range_type
17368 = create_range_type_with_stride (NULL, orig_base_type, &low,
17369 &high, bias, stride, byte_stride_p);
17370 }
17371 else
17372 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17373
17374 if (high_bound_is_count)
17375 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17376
17377 /* Ada expects an empty array on no boundary attributes. */
17378 if (attr == NULL && cu->language != language_ada)
17379 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17380
17381 name = dwarf2_name (die, cu);
17382 if (name)
17383 TYPE_NAME (range_type) = name;
17384
17385 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17386 if (attr != nullptr)
17387 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17388
17389 maybe_set_alignment (cu, die, range_type);
17390
17391 set_die_type (die, range_type, cu);
17392
17393 /* set_die_type should be already done. */
17394 set_descriptive_type (range_type, die, cu);
17395
17396 return range_type;
17397 }
17398
17399 static struct type *
17400 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17401 {
17402 struct type *type;
17403
17404 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17405 NULL);
17406 TYPE_NAME (type) = dwarf2_name (die, cu);
17407
17408 /* In Ada, an unspecified type is typically used when the description
17409 of the type is deferred to a different unit. When encountering
17410 such a type, we treat it as a stub, and try to resolve it later on,
17411 when needed. */
17412 if (cu->language == language_ada)
17413 TYPE_STUB (type) = 1;
17414
17415 return set_die_type (die, type, cu);
17416 }
17417
17418 /* Read a single die and all its descendents. Set the die's sibling
17419 field to NULL; set other fields in the die correctly, and set all
17420 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17421 location of the info_ptr after reading all of those dies. PARENT
17422 is the parent of the die in question. */
17423
17424 static struct die_info *
17425 read_die_and_children (const struct die_reader_specs *reader,
17426 const gdb_byte *info_ptr,
17427 const gdb_byte **new_info_ptr,
17428 struct die_info *parent)
17429 {
17430 struct die_info *die;
17431 const gdb_byte *cur_ptr;
17432
17433 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17434 if (die == NULL)
17435 {
17436 *new_info_ptr = cur_ptr;
17437 return NULL;
17438 }
17439 store_in_ref_table (die, reader->cu);
17440
17441 if (die->has_children)
17442 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17443 else
17444 {
17445 die->child = NULL;
17446 *new_info_ptr = cur_ptr;
17447 }
17448
17449 die->sibling = NULL;
17450 die->parent = parent;
17451 return die;
17452 }
17453
17454 /* Read a die, all of its descendents, and all of its siblings; set
17455 all of the fields of all of the dies correctly. Arguments are as
17456 in read_die_and_children. */
17457
17458 static struct die_info *
17459 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17460 const gdb_byte *info_ptr,
17461 const gdb_byte **new_info_ptr,
17462 struct die_info *parent)
17463 {
17464 struct die_info *first_die, *last_sibling;
17465 const gdb_byte *cur_ptr;
17466
17467 cur_ptr = info_ptr;
17468 first_die = last_sibling = NULL;
17469
17470 while (1)
17471 {
17472 struct die_info *die
17473 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17474
17475 if (die == NULL)
17476 {
17477 *new_info_ptr = cur_ptr;
17478 return first_die;
17479 }
17480
17481 if (!first_die)
17482 first_die = die;
17483 else
17484 last_sibling->sibling = die;
17485
17486 last_sibling = die;
17487 }
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 This the main entry point for reading a DIE and all its children. */
17494
17495 static struct die_info *
17496 read_die_and_siblings (const struct die_reader_specs *reader,
17497 const gdb_byte *info_ptr,
17498 const gdb_byte **new_info_ptr,
17499 struct die_info *parent)
17500 {
17501 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17502 new_info_ptr, parent);
17503
17504 if (dwarf_die_debug)
17505 {
17506 fprintf_unfiltered (gdb_stdlog,
17507 "Read die from %s@0x%x of %s:\n",
17508 reader->die_section->get_name (),
17509 (unsigned) (info_ptr - reader->die_section->buffer),
17510 bfd_get_filename (reader->abfd));
17511 dump_die (die, dwarf_die_debug);
17512 }
17513
17514 return die;
17515 }
17516
17517 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17518 attributes.
17519 The caller is responsible for filling in the extra attributes
17520 and updating (*DIEP)->num_attrs.
17521 Set DIEP to point to a newly allocated die with its information,
17522 except for its child, sibling, and parent fields. */
17523
17524 static const gdb_byte *
17525 read_full_die_1 (const struct die_reader_specs *reader,
17526 struct die_info **diep, const gdb_byte *info_ptr,
17527 int num_extra_attrs)
17528 {
17529 unsigned int abbrev_number, bytes_read, i;
17530 struct abbrev_info *abbrev;
17531 struct die_info *die;
17532 struct dwarf2_cu *cu = reader->cu;
17533 bfd *abfd = reader->abfd;
17534
17535 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17536 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17537 info_ptr += bytes_read;
17538 if (!abbrev_number)
17539 {
17540 *diep = NULL;
17541 return info_ptr;
17542 }
17543
17544 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17545 if (!abbrev)
17546 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17547 abbrev_number,
17548 bfd_get_filename (abfd));
17549
17550 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17551 die->sect_off = sect_off;
17552 die->tag = abbrev->tag;
17553 die->abbrev = abbrev_number;
17554 die->has_children = abbrev->has_children;
17555
17556 /* Make the result usable.
17557 The caller needs to update num_attrs after adding the extra
17558 attributes. */
17559 die->num_attrs = abbrev->num_attrs;
17560
17561 std::vector<int> indexes_that_need_reprocess;
17562 for (i = 0; i < abbrev->num_attrs; ++i)
17563 {
17564 bool need_reprocess;
17565 info_ptr =
17566 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17567 info_ptr, &need_reprocess);
17568 if (need_reprocess)
17569 indexes_that_need_reprocess.push_back (i);
17570 }
17571
17572 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17573 if (attr != nullptr)
17574 cu->str_offsets_base = DW_UNSND (attr);
17575
17576 auto maybe_addr_base = lookup_addr_base(die);
17577 if (maybe_addr_base.has_value ())
17578 cu->addr_base = *maybe_addr_base;
17579 for (int index : indexes_that_need_reprocess)
17580 read_attribute_reprocess (reader, &die->attrs[index]);
17581 *diep = die;
17582 return info_ptr;
17583 }
17584
17585 /* Read a die and all its attributes.
17586 Set DIEP to point to a newly allocated die with its information,
17587 except for its child, sibling, and parent fields. */
17588
17589 static const gdb_byte *
17590 read_full_die (const struct die_reader_specs *reader,
17591 struct die_info **diep, const gdb_byte *info_ptr)
17592 {
17593 const gdb_byte *result;
17594
17595 result = read_full_die_1 (reader, diep, info_ptr, 0);
17596
17597 if (dwarf_die_debug)
17598 {
17599 fprintf_unfiltered (gdb_stdlog,
17600 "Read die from %s@0x%x of %s:\n",
17601 reader->die_section->get_name (),
17602 (unsigned) (info_ptr - reader->die_section->buffer),
17603 bfd_get_filename (reader->abfd));
17604 dump_die (*diep, dwarf_die_debug);
17605 }
17606
17607 return result;
17608 }
17609 \f
17610
17611 /* Returns nonzero if TAG represents a type that we might generate a partial
17612 symbol for. */
17613
17614 static int
17615 is_type_tag_for_partial (int tag)
17616 {
17617 switch (tag)
17618 {
17619 #if 0
17620 /* Some types that would be reasonable to generate partial symbols for,
17621 that we don't at present. */
17622 case DW_TAG_array_type:
17623 case DW_TAG_file_type:
17624 case DW_TAG_ptr_to_member_type:
17625 case DW_TAG_set_type:
17626 case DW_TAG_string_type:
17627 case DW_TAG_subroutine_type:
17628 #endif
17629 case DW_TAG_base_type:
17630 case DW_TAG_class_type:
17631 case DW_TAG_interface_type:
17632 case DW_TAG_enumeration_type:
17633 case DW_TAG_structure_type:
17634 case DW_TAG_subrange_type:
17635 case DW_TAG_typedef:
17636 case DW_TAG_union_type:
17637 return 1;
17638 default:
17639 return 0;
17640 }
17641 }
17642
17643 /* Load all DIEs that are interesting for partial symbols into memory. */
17644
17645 static struct partial_die_info *
17646 load_partial_dies (const struct die_reader_specs *reader,
17647 const gdb_byte *info_ptr, int building_psymtab)
17648 {
17649 struct dwarf2_cu *cu = reader->cu;
17650 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17651 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17652 unsigned int bytes_read;
17653 unsigned int load_all = 0;
17654 int nesting_level = 1;
17655
17656 parent_die = NULL;
17657 last_die = NULL;
17658
17659 gdb_assert (cu->per_cu != NULL);
17660 if (cu->per_cu->load_all_dies)
17661 load_all = 1;
17662
17663 cu->partial_dies
17664 = htab_create_alloc_ex (cu->header.length / 12,
17665 partial_die_hash,
17666 partial_die_eq,
17667 NULL,
17668 &cu->comp_unit_obstack,
17669 hashtab_obstack_allocate,
17670 dummy_obstack_deallocate);
17671
17672 while (1)
17673 {
17674 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17675
17676 /* A NULL abbrev means the end of a series of children. */
17677 if (abbrev == NULL)
17678 {
17679 if (--nesting_level == 0)
17680 return first_die;
17681
17682 info_ptr += bytes_read;
17683 last_die = parent_die;
17684 parent_die = parent_die->die_parent;
17685 continue;
17686 }
17687
17688 /* Check for template arguments. We never save these; if
17689 they're seen, we just mark the parent, and go on our way. */
17690 if (parent_die != NULL
17691 && cu->language == language_cplus
17692 && (abbrev->tag == DW_TAG_template_type_param
17693 || abbrev->tag == DW_TAG_template_value_param))
17694 {
17695 parent_die->has_template_arguments = 1;
17696
17697 if (!load_all)
17698 {
17699 /* We don't need a partial DIE for the template argument. */
17700 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17701 continue;
17702 }
17703 }
17704
17705 /* We only recurse into c++ subprograms looking for template arguments.
17706 Skip their other children. */
17707 if (!load_all
17708 && cu->language == language_cplus
17709 && parent_die != NULL
17710 && parent_die->tag == DW_TAG_subprogram)
17711 {
17712 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17713 continue;
17714 }
17715
17716 /* Check whether this DIE is interesting enough to save. Normally
17717 we would not be interested in members here, but there may be
17718 later variables referencing them via DW_AT_specification (for
17719 static members). */
17720 if (!load_all
17721 && !is_type_tag_for_partial (abbrev->tag)
17722 && abbrev->tag != DW_TAG_constant
17723 && abbrev->tag != DW_TAG_enumerator
17724 && abbrev->tag != DW_TAG_subprogram
17725 && abbrev->tag != DW_TAG_inlined_subroutine
17726 && abbrev->tag != DW_TAG_lexical_block
17727 && abbrev->tag != DW_TAG_variable
17728 && abbrev->tag != DW_TAG_namespace
17729 && abbrev->tag != DW_TAG_module
17730 && abbrev->tag != DW_TAG_member
17731 && abbrev->tag != DW_TAG_imported_unit
17732 && abbrev->tag != DW_TAG_imported_declaration)
17733 {
17734 /* Otherwise we skip to the next sibling, if any. */
17735 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17736 continue;
17737 }
17738
17739 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17740 abbrev);
17741
17742 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17743
17744 /* This two-pass algorithm for processing partial symbols has a
17745 high cost in cache pressure. Thus, handle some simple cases
17746 here which cover the majority of C partial symbols. DIEs
17747 which neither have specification tags in them, nor could have
17748 specification tags elsewhere pointing at them, can simply be
17749 processed and discarded.
17750
17751 This segment is also optional; scan_partial_symbols and
17752 add_partial_symbol will handle these DIEs if we chain
17753 them in normally. When compilers which do not emit large
17754 quantities of duplicate debug information are more common,
17755 this code can probably be removed. */
17756
17757 /* Any complete simple types at the top level (pretty much all
17758 of them, for a language without namespaces), can be processed
17759 directly. */
17760 if (parent_die == NULL
17761 && pdi.has_specification == 0
17762 && pdi.is_declaration == 0
17763 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17764 || pdi.tag == DW_TAG_base_type
17765 || pdi.tag == DW_TAG_subrange_type))
17766 {
17767 if (building_psymtab && pdi.name != NULL)
17768 add_psymbol_to_list (pdi.name, false,
17769 VAR_DOMAIN, LOC_TYPEDEF, -1,
17770 psymbol_placement::STATIC,
17771 0, cu->language, objfile);
17772 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17773 continue;
17774 }
17775
17776 /* The exception for DW_TAG_typedef with has_children above is
17777 a workaround of GCC PR debug/47510. In the case of this complaint
17778 type_name_or_error will error on such types later.
17779
17780 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17781 it could not find the child DIEs referenced later, this is checked
17782 above. In correct DWARF DW_TAG_typedef should have no children. */
17783
17784 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17785 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17786 "- DIE at %s [in module %s]"),
17787 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17788
17789 /* If we're at the second level, and we're an enumerator, and
17790 our parent has no specification (meaning possibly lives in a
17791 namespace elsewhere), then we can add the partial symbol now
17792 instead of queueing it. */
17793 if (pdi.tag == DW_TAG_enumerator
17794 && parent_die != NULL
17795 && parent_die->die_parent == NULL
17796 && parent_die->tag == DW_TAG_enumeration_type
17797 && parent_die->has_specification == 0)
17798 {
17799 if (pdi.name == NULL)
17800 complaint (_("malformed enumerator DIE ignored"));
17801 else if (building_psymtab)
17802 add_psymbol_to_list (pdi.name, false,
17803 VAR_DOMAIN, LOC_CONST, -1,
17804 cu->language == language_cplus
17805 ? psymbol_placement::GLOBAL
17806 : psymbol_placement::STATIC,
17807 0, cu->language, objfile);
17808
17809 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17810 continue;
17811 }
17812
17813 struct partial_die_info *part_die
17814 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17815
17816 /* We'll save this DIE so link it in. */
17817 part_die->die_parent = parent_die;
17818 part_die->die_sibling = NULL;
17819 part_die->die_child = NULL;
17820
17821 if (last_die && last_die == parent_die)
17822 last_die->die_child = part_die;
17823 else if (last_die)
17824 last_die->die_sibling = part_die;
17825
17826 last_die = part_die;
17827
17828 if (first_die == NULL)
17829 first_die = part_die;
17830
17831 /* Maybe add the DIE to the hash table. Not all DIEs that we
17832 find interesting need to be in the hash table, because we
17833 also have the parent/sibling/child chains; only those that we
17834 might refer to by offset later during partial symbol reading.
17835
17836 For now this means things that might have be the target of a
17837 DW_AT_specification, DW_AT_abstract_origin, or
17838 DW_AT_extension. DW_AT_extension will refer only to
17839 namespaces; DW_AT_abstract_origin refers to functions (and
17840 many things under the function DIE, but we do not recurse
17841 into function DIEs during partial symbol reading) and
17842 possibly variables as well; DW_AT_specification refers to
17843 declarations. Declarations ought to have the DW_AT_declaration
17844 flag. It happens that GCC forgets to put it in sometimes, but
17845 only for functions, not for types.
17846
17847 Adding more things than necessary to the hash table is harmless
17848 except for the performance cost. Adding too few will result in
17849 wasted time in find_partial_die, when we reread the compilation
17850 unit with load_all_dies set. */
17851
17852 if (load_all
17853 || abbrev->tag == DW_TAG_constant
17854 || abbrev->tag == DW_TAG_subprogram
17855 || abbrev->tag == DW_TAG_variable
17856 || abbrev->tag == DW_TAG_namespace
17857 || part_die->is_declaration)
17858 {
17859 void **slot;
17860
17861 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17862 to_underlying (part_die->sect_off),
17863 INSERT);
17864 *slot = part_die;
17865 }
17866
17867 /* For some DIEs we want to follow their children (if any). For C
17868 we have no reason to follow the children of structures; for other
17869 languages we have to, so that we can get at method physnames
17870 to infer fully qualified class names, for DW_AT_specification,
17871 and for C++ template arguments. For C++, we also look one level
17872 inside functions to find template arguments (if the name of the
17873 function does not already contain the template arguments).
17874
17875 For Ada and Fortran, we need to scan the children of subprograms
17876 and lexical blocks as well because these languages allow the
17877 definition of nested entities that could be interesting for the
17878 debugger, such as nested subprograms for instance. */
17879 if (last_die->has_children
17880 && (load_all
17881 || last_die->tag == DW_TAG_namespace
17882 || last_die->tag == DW_TAG_module
17883 || last_die->tag == DW_TAG_enumeration_type
17884 || (cu->language == language_cplus
17885 && last_die->tag == DW_TAG_subprogram
17886 && (last_die->name == NULL
17887 || strchr (last_die->name, '<') == NULL))
17888 || (cu->language != language_c
17889 && (last_die->tag == DW_TAG_class_type
17890 || last_die->tag == DW_TAG_interface_type
17891 || last_die->tag == DW_TAG_structure_type
17892 || last_die->tag == DW_TAG_union_type))
17893 || ((cu->language == language_ada
17894 || cu->language == language_fortran)
17895 && (last_die->tag == DW_TAG_subprogram
17896 || last_die->tag == DW_TAG_lexical_block))))
17897 {
17898 nesting_level++;
17899 parent_die = last_die;
17900 continue;
17901 }
17902
17903 /* Otherwise we skip to the next sibling, if any. */
17904 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17905
17906 /* Back to the top, do it again. */
17907 }
17908 }
17909
17910 partial_die_info::partial_die_info (sect_offset sect_off_,
17911 struct abbrev_info *abbrev)
17912 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17913 {
17914 }
17915
17916 /* Read a minimal amount of information into the minimal die structure.
17917 INFO_PTR should point just after the initial uleb128 of a DIE. */
17918
17919 const gdb_byte *
17920 partial_die_info::read (const struct die_reader_specs *reader,
17921 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17922 {
17923 struct dwarf2_cu *cu = reader->cu;
17924 struct dwarf2_per_objfile *dwarf2_per_objfile
17925 = cu->per_cu->dwarf2_per_objfile;
17926 unsigned int i;
17927 int has_low_pc_attr = 0;
17928 int has_high_pc_attr = 0;
17929 int high_pc_relative = 0;
17930
17931 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17932 for (i = 0; i < abbrev.num_attrs; ++i)
17933 {
17934 bool need_reprocess;
17935 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17936 info_ptr, &need_reprocess);
17937 /* String and address offsets that need to do the reprocessing have
17938 already been read at this point, so there is no need to wait until
17939 the loop terminates to do the reprocessing. */
17940 if (need_reprocess)
17941 read_attribute_reprocess (reader, &attr_vec[i]);
17942 attribute &attr = attr_vec[i];
17943 /* Store the data if it is of an attribute we want to keep in a
17944 partial symbol table. */
17945 switch (attr.name)
17946 {
17947 case DW_AT_name:
17948 switch (tag)
17949 {
17950 case DW_TAG_compile_unit:
17951 case DW_TAG_partial_unit:
17952 case DW_TAG_type_unit:
17953 /* Compilation units have a DW_AT_name that is a filename, not
17954 a source language identifier. */
17955 case DW_TAG_enumeration_type:
17956 case DW_TAG_enumerator:
17957 /* These tags always have simple identifiers already; no need
17958 to canonicalize them. */
17959 name = DW_STRING (&attr);
17960 break;
17961 default:
17962 {
17963 struct objfile *objfile = dwarf2_per_objfile->objfile;
17964
17965 name
17966 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17967 &objfile->per_bfd->storage_obstack);
17968 }
17969 break;
17970 }
17971 break;
17972 case DW_AT_linkage_name:
17973 case DW_AT_MIPS_linkage_name:
17974 /* Note that both forms of linkage name might appear. We
17975 assume they will be the same, and we only store the last
17976 one we see. */
17977 linkage_name = DW_STRING (&attr);
17978 break;
17979 case DW_AT_low_pc:
17980 has_low_pc_attr = 1;
17981 lowpc = attr.value_as_address ();
17982 break;
17983 case DW_AT_high_pc:
17984 has_high_pc_attr = 1;
17985 highpc = attr.value_as_address ();
17986 if (cu->header.version >= 4 && attr.form_is_constant ())
17987 high_pc_relative = 1;
17988 break;
17989 case DW_AT_location:
17990 /* Support the .debug_loc offsets. */
17991 if (attr.form_is_block ())
17992 {
17993 d.locdesc = DW_BLOCK (&attr);
17994 }
17995 else if (attr.form_is_section_offset ())
17996 {
17997 dwarf2_complex_location_expr_complaint ();
17998 }
17999 else
18000 {
18001 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18002 "partial symbol information");
18003 }
18004 break;
18005 case DW_AT_external:
18006 is_external = DW_UNSND (&attr);
18007 break;
18008 case DW_AT_declaration:
18009 is_declaration = DW_UNSND (&attr);
18010 break;
18011 case DW_AT_type:
18012 has_type = 1;
18013 break;
18014 case DW_AT_abstract_origin:
18015 case DW_AT_specification:
18016 case DW_AT_extension:
18017 has_specification = 1;
18018 spec_offset = dwarf2_get_ref_die_offset (&attr);
18019 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18020 || cu->per_cu->is_dwz);
18021 break;
18022 case DW_AT_sibling:
18023 /* Ignore absolute siblings, they might point outside of
18024 the current compile unit. */
18025 if (attr.form == DW_FORM_ref_addr)
18026 complaint (_("ignoring absolute DW_AT_sibling"));
18027 else
18028 {
18029 const gdb_byte *buffer = reader->buffer;
18030 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18031 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18032
18033 if (sibling_ptr < info_ptr)
18034 complaint (_("DW_AT_sibling points backwards"));
18035 else if (sibling_ptr > reader->buffer_end)
18036 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18037 else
18038 sibling = sibling_ptr;
18039 }
18040 break;
18041 case DW_AT_byte_size:
18042 has_byte_size = 1;
18043 break;
18044 case DW_AT_const_value:
18045 has_const_value = 1;
18046 break;
18047 case DW_AT_calling_convention:
18048 /* DWARF doesn't provide a way to identify a program's source-level
18049 entry point. DW_AT_calling_convention attributes are only meant
18050 to describe functions' calling conventions.
18051
18052 However, because it's a necessary piece of information in
18053 Fortran, and before DWARF 4 DW_CC_program was the only
18054 piece of debugging information whose definition refers to
18055 a 'main program' at all, several compilers marked Fortran
18056 main programs with DW_CC_program --- even when those
18057 functions use the standard calling conventions.
18058
18059 Although DWARF now specifies a way to provide this
18060 information, we support this practice for backward
18061 compatibility. */
18062 if (DW_UNSND (&attr) == DW_CC_program
18063 && cu->language == language_fortran)
18064 main_subprogram = 1;
18065 break;
18066 case DW_AT_inline:
18067 if (DW_UNSND (&attr) == DW_INL_inlined
18068 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18069 may_be_inlined = 1;
18070 break;
18071
18072 case DW_AT_import:
18073 if (tag == DW_TAG_imported_unit)
18074 {
18075 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18076 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18077 || cu->per_cu->is_dwz);
18078 }
18079 break;
18080
18081 case DW_AT_main_subprogram:
18082 main_subprogram = DW_UNSND (&attr);
18083 break;
18084
18085 case DW_AT_ranges:
18086 {
18087 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18088 but that requires a full DIE, so instead we just
18089 reimplement it. */
18090 int need_ranges_base = tag != DW_TAG_compile_unit;
18091 unsigned int ranges_offset = (DW_UNSND (&attr)
18092 + (need_ranges_base
18093 ? cu->ranges_base
18094 : 0));
18095
18096 /* Value of the DW_AT_ranges attribute is the offset in the
18097 .debug_ranges section. */
18098 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18099 nullptr))
18100 has_pc_info = 1;
18101 }
18102 break;
18103
18104 default:
18105 break;
18106 }
18107 }
18108
18109 /* For Ada, if both the name and the linkage name appear, we prefer
18110 the latter. This lets "catch exception" work better, regardless
18111 of the order in which the name and linkage name were emitted.
18112 Really, though, this is just a workaround for the fact that gdb
18113 doesn't store both the name and the linkage name. */
18114 if (cu->language == language_ada && linkage_name != nullptr)
18115 name = linkage_name;
18116
18117 if (high_pc_relative)
18118 highpc += lowpc;
18119
18120 if (has_low_pc_attr && has_high_pc_attr)
18121 {
18122 /* When using the GNU linker, .gnu.linkonce. sections are used to
18123 eliminate duplicate copies of functions and vtables and such.
18124 The linker will arbitrarily choose one and discard the others.
18125 The AT_*_pc values for such functions refer to local labels in
18126 these sections. If the section from that file was discarded, the
18127 labels are not in the output, so the relocs get a value of 0.
18128 If this is a discarded function, mark the pc bounds as invalid,
18129 so that GDB will ignore it. */
18130 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18131 {
18132 struct objfile *objfile = dwarf2_per_objfile->objfile;
18133 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18134
18135 complaint (_("DW_AT_low_pc %s is zero "
18136 "for DIE at %s [in module %s]"),
18137 paddress (gdbarch, lowpc),
18138 sect_offset_str (sect_off),
18139 objfile_name (objfile));
18140 }
18141 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18142 else if (lowpc >= highpc)
18143 {
18144 struct objfile *objfile = dwarf2_per_objfile->objfile;
18145 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18146
18147 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18148 "for DIE at %s [in module %s]"),
18149 paddress (gdbarch, lowpc),
18150 paddress (gdbarch, highpc),
18151 sect_offset_str (sect_off),
18152 objfile_name (objfile));
18153 }
18154 else
18155 has_pc_info = 1;
18156 }
18157
18158 return info_ptr;
18159 }
18160
18161 /* Find a cached partial DIE at OFFSET in CU. */
18162
18163 struct partial_die_info *
18164 dwarf2_cu::find_partial_die (sect_offset sect_off)
18165 {
18166 struct partial_die_info *lookup_die = NULL;
18167 struct partial_die_info part_die (sect_off);
18168
18169 lookup_die = ((struct partial_die_info *)
18170 htab_find_with_hash (partial_dies, &part_die,
18171 to_underlying (sect_off)));
18172
18173 return lookup_die;
18174 }
18175
18176 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18177 except in the case of .debug_types DIEs which do not reference
18178 outside their CU (they do however referencing other types via
18179 DW_FORM_ref_sig8). */
18180
18181 static const struct cu_partial_die_info
18182 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18183 {
18184 struct dwarf2_per_objfile *dwarf2_per_objfile
18185 = cu->per_cu->dwarf2_per_objfile;
18186 struct objfile *objfile = dwarf2_per_objfile->objfile;
18187 struct dwarf2_per_cu_data *per_cu = NULL;
18188 struct partial_die_info *pd = NULL;
18189
18190 if (offset_in_dwz == cu->per_cu->is_dwz
18191 && cu->header.offset_in_cu_p (sect_off))
18192 {
18193 pd = cu->find_partial_die (sect_off);
18194 if (pd != NULL)
18195 return { cu, pd };
18196 /* We missed recording what we needed.
18197 Load all dies and try again. */
18198 per_cu = cu->per_cu;
18199 }
18200 else
18201 {
18202 /* TUs don't reference other CUs/TUs (except via type signatures). */
18203 if (cu->per_cu->is_debug_types)
18204 {
18205 error (_("Dwarf Error: Type Unit at offset %s contains"
18206 " external reference to offset %s [in module %s].\n"),
18207 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18208 bfd_get_filename (objfile->obfd));
18209 }
18210 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18211 dwarf2_per_objfile);
18212
18213 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18214 load_partial_comp_unit (per_cu);
18215
18216 per_cu->cu->last_used = 0;
18217 pd = per_cu->cu->find_partial_die (sect_off);
18218 }
18219
18220 /* If we didn't find it, and not all dies have been loaded,
18221 load them all and try again. */
18222
18223 if (pd == NULL && per_cu->load_all_dies == 0)
18224 {
18225 per_cu->load_all_dies = 1;
18226
18227 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18228 THIS_CU->cu may already be in use. So we can't just free it and
18229 replace its DIEs with the ones we read in. Instead, we leave those
18230 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18231 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18232 set. */
18233 load_partial_comp_unit (per_cu);
18234
18235 pd = per_cu->cu->find_partial_die (sect_off);
18236 }
18237
18238 if (pd == NULL)
18239 internal_error (__FILE__, __LINE__,
18240 _("could not find partial DIE %s "
18241 "in cache [from module %s]\n"),
18242 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18243 return { per_cu->cu, pd };
18244 }
18245
18246 /* See if we can figure out if the class lives in a namespace. We do
18247 this by looking for a member function; its demangled name will
18248 contain namespace info, if there is any. */
18249
18250 static void
18251 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18252 struct dwarf2_cu *cu)
18253 {
18254 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18255 what template types look like, because the demangler
18256 frequently doesn't give the same name as the debug info. We
18257 could fix this by only using the demangled name to get the
18258 prefix (but see comment in read_structure_type). */
18259
18260 struct partial_die_info *real_pdi;
18261 struct partial_die_info *child_pdi;
18262
18263 /* If this DIE (this DIE's specification, if any) has a parent, then
18264 we should not do this. We'll prepend the parent's fully qualified
18265 name when we create the partial symbol. */
18266
18267 real_pdi = struct_pdi;
18268 while (real_pdi->has_specification)
18269 {
18270 auto res = find_partial_die (real_pdi->spec_offset,
18271 real_pdi->spec_is_dwz, cu);
18272 real_pdi = res.pdi;
18273 cu = res.cu;
18274 }
18275
18276 if (real_pdi->die_parent != NULL)
18277 return;
18278
18279 for (child_pdi = struct_pdi->die_child;
18280 child_pdi != NULL;
18281 child_pdi = child_pdi->die_sibling)
18282 {
18283 if (child_pdi->tag == DW_TAG_subprogram
18284 && child_pdi->linkage_name != NULL)
18285 {
18286 gdb::unique_xmalloc_ptr<char> actual_class_name
18287 (language_class_name_from_physname (cu->language_defn,
18288 child_pdi->linkage_name));
18289 if (actual_class_name != NULL)
18290 {
18291 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18292 struct_pdi->name
18293 = obstack_strdup (&objfile->per_bfd->storage_obstack,
18294 actual_class_name.get ());
18295 }
18296 break;
18297 }
18298 }
18299 }
18300
18301 void
18302 partial_die_info::fixup (struct dwarf2_cu *cu)
18303 {
18304 /* Once we've fixed up a die, there's no point in doing so again.
18305 This also avoids a memory leak if we were to call
18306 guess_partial_die_structure_name multiple times. */
18307 if (fixup_called)
18308 return;
18309
18310 /* If we found a reference attribute and the DIE has no name, try
18311 to find a name in the referred to DIE. */
18312
18313 if (name == NULL && has_specification)
18314 {
18315 struct partial_die_info *spec_die;
18316
18317 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18318 spec_die = res.pdi;
18319 cu = res.cu;
18320
18321 spec_die->fixup (cu);
18322
18323 if (spec_die->name)
18324 {
18325 name = spec_die->name;
18326
18327 /* Copy DW_AT_external attribute if it is set. */
18328 if (spec_die->is_external)
18329 is_external = spec_die->is_external;
18330 }
18331 }
18332
18333 /* Set default names for some unnamed DIEs. */
18334
18335 if (name == NULL && tag == DW_TAG_namespace)
18336 name = CP_ANONYMOUS_NAMESPACE_STR;
18337
18338 /* If there is no parent die to provide a namespace, and there are
18339 children, see if we can determine the namespace from their linkage
18340 name. */
18341 if (cu->language == language_cplus
18342 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18343 && die_parent == NULL
18344 && has_children
18345 && (tag == DW_TAG_class_type
18346 || tag == DW_TAG_structure_type
18347 || tag == DW_TAG_union_type))
18348 guess_partial_die_structure_name (this, cu);
18349
18350 /* GCC might emit a nameless struct or union that has a linkage
18351 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18352 if (name == NULL
18353 && (tag == DW_TAG_class_type
18354 || tag == DW_TAG_interface_type
18355 || tag == DW_TAG_structure_type
18356 || tag == DW_TAG_union_type)
18357 && linkage_name != NULL)
18358 {
18359 gdb::unique_xmalloc_ptr<char> demangled
18360 (gdb_demangle (linkage_name, DMGL_TYPES));
18361 if (demangled != nullptr)
18362 {
18363 const char *base;
18364
18365 /* Strip any leading namespaces/classes, keep only the base name.
18366 DW_AT_name for named DIEs does not contain the prefixes. */
18367 base = strrchr (demangled.get (), ':');
18368 if (base && base > demangled.get () && base[-1] == ':')
18369 base++;
18370 else
18371 base = demangled.get ();
18372
18373 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18374 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
18375 }
18376 }
18377
18378 fixup_called = 1;
18379 }
18380
18381 /* Process the attributes that had to be skipped in the first round. These
18382 attributes are the ones that need str_offsets_base or addr_base attributes.
18383 They could not have been processed in the first round, because at the time
18384 the values of str_offsets_base or addr_base may not have been known. */
18385 void read_attribute_reprocess (const struct die_reader_specs *reader,
18386 struct attribute *attr)
18387 {
18388 struct dwarf2_cu *cu = reader->cu;
18389 switch (attr->form)
18390 {
18391 case DW_FORM_addrx:
18392 case DW_FORM_GNU_addr_index:
18393 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18394 break;
18395 case DW_FORM_strx:
18396 case DW_FORM_strx1:
18397 case DW_FORM_strx2:
18398 case DW_FORM_strx3:
18399 case DW_FORM_strx4:
18400 case DW_FORM_GNU_str_index:
18401 {
18402 unsigned int str_index = DW_UNSND (attr);
18403 if (reader->dwo_file != NULL)
18404 {
18405 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18406 DW_STRING_IS_CANONICAL (attr) = 0;
18407 }
18408 else
18409 {
18410 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18411 DW_STRING_IS_CANONICAL (attr) = 0;
18412 }
18413 break;
18414 }
18415 default:
18416 gdb_assert_not_reached (_("Unexpected DWARF form."));
18417 }
18418 }
18419
18420 /* Read an attribute value described by an attribute form. */
18421
18422 static const gdb_byte *
18423 read_attribute_value (const struct die_reader_specs *reader,
18424 struct attribute *attr, unsigned form,
18425 LONGEST implicit_const, const gdb_byte *info_ptr,
18426 bool *need_reprocess)
18427 {
18428 struct dwarf2_cu *cu = reader->cu;
18429 struct dwarf2_per_objfile *dwarf2_per_objfile
18430 = cu->per_cu->dwarf2_per_objfile;
18431 struct objfile *objfile = dwarf2_per_objfile->objfile;
18432 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18433 bfd *abfd = reader->abfd;
18434 struct comp_unit_head *cu_header = &cu->header;
18435 unsigned int bytes_read;
18436 struct dwarf_block *blk;
18437 *need_reprocess = false;
18438
18439 attr->form = (enum dwarf_form) form;
18440 switch (form)
18441 {
18442 case DW_FORM_ref_addr:
18443 if (cu->header.version == 2)
18444 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18445 &bytes_read);
18446 else
18447 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18448 &bytes_read);
18449 info_ptr += bytes_read;
18450 break;
18451 case DW_FORM_GNU_ref_alt:
18452 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18453 info_ptr += bytes_read;
18454 break;
18455 case DW_FORM_addr:
18456 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18457 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18458 info_ptr += bytes_read;
18459 break;
18460 case DW_FORM_block2:
18461 blk = dwarf_alloc_block (cu);
18462 blk->size = read_2_bytes (abfd, info_ptr);
18463 info_ptr += 2;
18464 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18465 info_ptr += blk->size;
18466 DW_BLOCK (attr) = blk;
18467 break;
18468 case DW_FORM_block4:
18469 blk = dwarf_alloc_block (cu);
18470 blk->size = read_4_bytes (abfd, info_ptr);
18471 info_ptr += 4;
18472 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18473 info_ptr += blk->size;
18474 DW_BLOCK (attr) = blk;
18475 break;
18476 case DW_FORM_data2:
18477 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18478 info_ptr += 2;
18479 break;
18480 case DW_FORM_data4:
18481 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18482 info_ptr += 4;
18483 break;
18484 case DW_FORM_data8:
18485 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18486 info_ptr += 8;
18487 break;
18488 case DW_FORM_data16:
18489 blk = dwarf_alloc_block (cu);
18490 blk->size = 16;
18491 blk->data = read_n_bytes (abfd, info_ptr, 16);
18492 info_ptr += 16;
18493 DW_BLOCK (attr) = blk;
18494 break;
18495 case DW_FORM_sec_offset:
18496 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18497 info_ptr += bytes_read;
18498 break;
18499 case DW_FORM_string:
18500 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18501 DW_STRING_IS_CANONICAL (attr) = 0;
18502 info_ptr += bytes_read;
18503 break;
18504 case DW_FORM_strp:
18505 if (!cu->per_cu->is_dwz)
18506 {
18507 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18508 abfd, info_ptr, cu_header,
18509 &bytes_read);
18510 DW_STRING_IS_CANONICAL (attr) = 0;
18511 info_ptr += bytes_read;
18512 break;
18513 }
18514 /* FALLTHROUGH */
18515 case DW_FORM_line_strp:
18516 if (!cu->per_cu->is_dwz)
18517 {
18518 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18519 abfd, info_ptr,
18520 cu_header, &bytes_read);
18521 DW_STRING_IS_CANONICAL (attr) = 0;
18522 info_ptr += bytes_read;
18523 break;
18524 }
18525 /* FALLTHROUGH */
18526 case DW_FORM_GNU_strp_alt:
18527 {
18528 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18529 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18530 &bytes_read);
18531
18532 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18533 dwz, str_offset);
18534 DW_STRING_IS_CANONICAL (attr) = 0;
18535 info_ptr += bytes_read;
18536 }
18537 break;
18538 case DW_FORM_exprloc:
18539 case DW_FORM_block:
18540 blk = dwarf_alloc_block (cu);
18541 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18542 info_ptr += bytes_read;
18543 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18544 info_ptr += blk->size;
18545 DW_BLOCK (attr) = blk;
18546 break;
18547 case DW_FORM_block1:
18548 blk = dwarf_alloc_block (cu);
18549 blk->size = read_1_byte (abfd, info_ptr);
18550 info_ptr += 1;
18551 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18552 info_ptr += blk->size;
18553 DW_BLOCK (attr) = blk;
18554 break;
18555 case DW_FORM_data1:
18556 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18557 info_ptr += 1;
18558 break;
18559 case DW_FORM_flag:
18560 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18561 info_ptr += 1;
18562 break;
18563 case DW_FORM_flag_present:
18564 DW_UNSND (attr) = 1;
18565 break;
18566 case DW_FORM_sdata:
18567 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18568 info_ptr += bytes_read;
18569 break;
18570 case DW_FORM_udata:
18571 case DW_FORM_rnglistx:
18572 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18573 info_ptr += bytes_read;
18574 break;
18575 case DW_FORM_ref1:
18576 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18577 + read_1_byte (abfd, info_ptr));
18578 info_ptr += 1;
18579 break;
18580 case DW_FORM_ref2:
18581 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18582 + read_2_bytes (abfd, info_ptr));
18583 info_ptr += 2;
18584 break;
18585 case DW_FORM_ref4:
18586 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18587 + read_4_bytes (abfd, info_ptr));
18588 info_ptr += 4;
18589 break;
18590 case DW_FORM_ref8:
18591 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18592 + read_8_bytes (abfd, info_ptr));
18593 info_ptr += 8;
18594 break;
18595 case DW_FORM_ref_sig8:
18596 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18597 info_ptr += 8;
18598 break;
18599 case DW_FORM_ref_udata:
18600 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18601 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18602 info_ptr += bytes_read;
18603 break;
18604 case DW_FORM_indirect:
18605 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18606 info_ptr += bytes_read;
18607 if (form == DW_FORM_implicit_const)
18608 {
18609 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18610 info_ptr += bytes_read;
18611 }
18612 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18613 info_ptr, need_reprocess);
18614 break;
18615 case DW_FORM_implicit_const:
18616 DW_SND (attr) = implicit_const;
18617 break;
18618 case DW_FORM_addrx:
18619 case DW_FORM_GNU_addr_index:
18620 *need_reprocess = true;
18621 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18622 info_ptr += bytes_read;
18623 break;
18624 case DW_FORM_strx:
18625 case DW_FORM_strx1:
18626 case DW_FORM_strx2:
18627 case DW_FORM_strx3:
18628 case DW_FORM_strx4:
18629 case DW_FORM_GNU_str_index:
18630 {
18631 ULONGEST str_index;
18632 if (form == DW_FORM_strx1)
18633 {
18634 str_index = read_1_byte (abfd, info_ptr);
18635 info_ptr += 1;
18636 }
18637 else if (form == DW_FORM_strx2)
18638 {
18639 str_index = read_2_bytes (abfd, info_ptr);
18640 info_ptr += 2;
18641 }
18642 else if (form == DW_FORM_strx3)
18643 {
18644 str_index = read_3_bytes (abfd, info_ptr);
18645 info_ptr += 3;
18646 }
18647 else if (form == DW_FORM_strx4)
18648 {
18649 str_index = read_4_bytes (abfd, info_ptr);
18650 info_ptr += 4;
18651 }
18652 else
18653 {
18654 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18655 info_ptr += bytes_read;
18656 }
18657 *need_reprocess = true;
18658 DW_UNSND (attr) = str_index;
18659 }
18660 break;
18661 default:
18662 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18663 dwarf_form_name (form),
18664 bfd_get_filename (abfd));
18665 }
18666
18667 /* Super hack. */
18668 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18669 attr->form = DW_FORM_GNU_ref_alt;
18670
18671 /* We have seen instances where the compiler tried to emit a byte
18672 size attribute of -1 which ended up being encoded as an unsigned
18673 0xffffffff. Although 0xffffffff is technically a valid size value,
18674 an object of this size seems pretty unlikely so we can relatively
18675 safely treat these cases as if the size attribute was invalid and
18676 treat them as zero by default. */
18677 if (attr->name == DW_AT_byte_size
18678 && form == DW_FORM_data4
18679 && DW_UNSND (attr) >= 0xffffffff)
18680 {
18681 complaint
18682 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18683 hex_string (DW_UNSND (attr)));
18684 DW_UNSND (attr) = 0;
18685 }
18686
18687 return info_ptr;
18688 }
18689
18690 /* Read an attribute described by an abbreviated attribute. */
18691
18692 static const gdb_byte *
18693 read_attribute (const struct die_reader_specs *reader,
18694 struct attribute *attr, struct attr_abbrev *abbrev,
18695 const gdb_byte *info_ptr, bool *need_reprocess)
18696 {
18697 attr->name = abbrev->name;
18698 return read_attribute_value (reader, attr, abbrev->form,
18699 abbrev->implicit_const, info_ptr,
18700 need_reprocess);
18701 }
18702
18703 /* Cover function for read_initial_length.
18704 Returns the length of the object at BUF, and stores the size of the
18705 initial length in *BYTES_READ and stores the size that offsets will be in
18706 *OFFSET_SIZE.
18707 If the initial length size is not equivalent to that specified in
18708 CU_HEADER then issue a complaint.
18709 This is useful when reading non-comp-unit headers. */
18710
18711 static LONGEST
18712 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
18713 const struct comp_unit_head *cu_header,
18714 unsigned int *bytes_read,
18715 unsigned int *offset_size)
18716 {
18717 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18718
18719 gdb_assert (cu_header->initial_length_size == 4
18720 || cu_header->initial_length_size == 8
18721 || cu_header->initial_length_size == 12);
18722
18723 if (cu_header->initial_length_size != *bytes_read)
18724 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
18725
18726 *offset_size = (*bytes_read == 4) ? 4 : 8;
18727 return length;
18728 }
18729
18730 /* Return pointer to string at section SECT offset STR_OFFSET with error
18731 reporting strings FORM_NAME and SECT_NAME. */
18732
18733 static const char *
18734 read_indirect_string_at_offset_from (struct objfile *objfile,
18735 bfd *abfd, LONGEST str_offset,
18736 struct dwarf2_section_info *sect,
18737 const char *form_name,
18738 const char *sect_name)
18739 {
18740 sect->read (objfile);
18741 if (sect->buffer == NULL)
18742 error (_("%s used without %s section [in module %s]"),
18743 form_name, sect_name, bfd_get_filename (abfd));
18744 if (str_offset >= sect->size)
18745 error (_("%s pointing outside of %s section [in module %s]"),
18746 form_name, sect_name, bfd_get_filename (abfd));
18747 gdb_assert (HOST_CHAR_BIT == 8);
18748 if (sect->buffer[str_offset] == '\0')
18749 return NULL;
18750 return (const char *) (sect->buffer + str_offset);
18751 }
18752
18753 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18754
18755 static const char *
18756 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18757 bfd *abfd, LONGEST str_offset)
18758 {
18759 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18760 abfd, str_offset,
18761 &dwarf2_per_objfile->str,
18762 "DW_FORM_strp", ".debug_str");
18763 }
18764
18765 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18766
18767 static const char *
18768 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18769 bfd *abfd, LONGEST str_offset)
18770 {
18771 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18772 abfd, str_offset,
18773 &dwarf2_per_objfile->line_str,
18774 "DW_FORM_line_strp",
18775 ".debug_line_str");
18776 }
18777
18778 /* Read a string at offset STR_OFFSET in the .debug_str section from
18779 the .dwz file DWZ. Throw an error if the offset is too large. If
18780 the string consists of a single NUL byte, return NULL; otherwise
18781 return a pointer to the string. */
18782
18783 static const char *
18784 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
18785 LONGEST str_offset)
18786 {
18787 dwz->str.read (objfile);
18788
18789 if (dwz->str.buffer == NULL)
18790 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18791 "section [in module %s]"),
18792 bfd_get_filename (dwz->dwz_bfd.get ()));
18793 if (str_offset >= dwz->str.size)
18794 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18795 ".debug_str section [in module %s]"),
18796 bfd_get_filename (dwz->dwz_bfd.get ()));
18797 gdb_assert (HOST_CHAR_BIT == 8);
18798 if (dwz->str.buffer[str_offset] == '\0')
18799 return NULL;
18800 return (const char *) (dwz->str.buffer + str_offset);
18801 }
18802
18803 /* Return pointer to string at .debug_str offset as read from BUF.
18804 BUF is assumed to be in a compilation unit described by CU_HEADER.
18805 Return *BYTES_READ_PTR count of bytes read from BUF. */
18806
18807 static const char *
18808 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18809 const gdb_byte *buf,
18810 const struct comp_unit_head *cu_header,
18811 unsigned int *bytes_read_ptr)
18812 {
18813 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18814
18815 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
18816 }
18817
18818 /* Return pointer to string at .debug_line_str offset as read from BUF.
18819 BUF is assumed to be in a compilation unit described by CU_HEADER.
18820 Return *BYTES_READ_PTR count of bytes read from BUF. */
18821
18822 static const char *
18823 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18824 bfd *abfd, const gdb_byte *buf,
18825 const struct comp_unit_head *cu_header,
18826 unsigned int *bytes_read_ptr)
18827 {
18828 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18829
18830 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18831 str_offset);
18832 }
18833
18834 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18835 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18836 ADDR_SIZE is the size of addresses from the CU header. */
18837
18838 static CORE_ADDR
18839 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18840 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18841 int addr_size)
18842 {
18843 struct objfile *objfile = dwarf2_per_objfile->objfile;
18844 bfd *abfd = objfile->obfd;
18845 const gdb_byte *info_ptr;
18846 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18847
18848 dwarf2_per_objfile->addr.read (objfile);
18849 if (dwarf2_per_objfile->addr.buffer == NULL)
18850 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18851 objfile_name (objfile));
18852 if (addr_base_or_zero + addr_index * addr_size
18853 >= dwarf2_per_objfile->addr.size)
18854 error (_("DW_FORM_addr_index pointing outside of "
18855 ".debug_addr section [in module %s]"),
18856 objfile_name (objfile));
18857 info_ptr = (dwarf2_per_objfile->addr.buffer
18858 + addr_base_or_zero + addr_index * addr_size);
18859 if (addr_size == 4)
18860 return bfd_get_32 (abfd, info_ptr);
18861 else
18862 return bfd_get_64 (abfd, info_ptr);
18863 }
18864
18865 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18866
18867 static CORE_ADDR
18868 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18869 {
18870 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18871 cu->addr_base, cu->header.addr_size);
18872 }
18873
18874 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18875
18876 static CORE_ADDR
18877 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18878 unsigned int *bytes_read)
18879 {
18880 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18881 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18882
18883 return read_addr_index (cu, addr_index);
18884 }
18885
18886 /* Given an index in .debug_addr, fetch the value.
18887 NOTE: This can be called during dwarf expression evaluation,
18888 long after the debug information has been read, and thus per_cu->cu
18889 may no longer exist. */
18890
18891 CORE_ADDR
18892 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18893 unsigned int addr_index)
18894 {
18895 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18896 struct dwarf2_cu *cu = per_cu->cu;
18897 gdb::optional<ULONGEST> addr_base;
18898 int addr_size;
18899
18900 /* We need addr_base and addr_size.
18901 If we don't have PER_CU->cu, we have to get it.
18902 Nasty, but the alternative is storing the needed info in PER_CU,
18903 which at this point doesn't seem justified: it's not clear how frequently
18904 it would get used and it would increase the size of every PER_CU.
18905 Entry points like dwarf2_per_cu_addr_size do a similar thing
18906 so we're not in uncharted territory here.
18907 Alas we need to be a bit more complicated as addr_base is contained
18908 in the DIE.
18909
18910 We don't need to read the entire CU(/TU).
18911 We just need the header and top level die.
18912
18913 IWBN to use the aging mechanism to let us lazily later discard the CU.
18914 For now we skip this optimization. */
18915
18916 if (cu != NULL)
18917 {
18918 addr_base = cu->addr_base;
18919 addr_size = cu->header.addr_size;
18920 }
18921 else
18922 {
18923 cutu_reader reader (per_cu, NULL, 0, 0, false);
18924 addr_base = reader.cu->addr_base;
18925 addr_size = reader.cu->header.addr_size;
18926 }
18927
18928 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18929 addr_size);
18930 }
18931
18932 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18933 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18934 DWO file. */
18935
18936 static const char *
18937 read_str_index (struct dwarf2_cu *cu,
18938 struct dwarf2_section_info *str_section,
18939 struct dwarf2_section_info *str_offsets_section,
18940 ULONGEST str_offsets_base, ULONGEST str_index)
18941 {
18942 struct dwarf2_per_objfile *dwarf2_per_objfile
18943 = cu->per_cu->dwarf2_per_objfile;
18944 struct objfile *objfile = dwarf2_per_objfile->objfile;
18945 const char *objf_name = objfile_name (objfile);
18946 bfd *abfd = objfile->obfd;
18947 const gdb_byte *info_ptr;
18948 ULONGEST str_offset;
18949 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18950
18951 str_section->read (objfile);
18952 str_offsets_section->read (objfile);
18953 if (str_section->buffer == NULL)
18954 error (_("%s used without %s section"
18955 " in CU at offset %s [in module %s]"),
18956 form_name, str_section->get_name (),
18957 sect_offset_str (cu->header.sect_off), objf_name);
18958 if (str_offsets_section->buffer == NULL)
18959 error (_("%s used without %s section"
18960 " in CU at offset %s [in module %s]"),
18961 form_name, str_section->get_name (),
18962 sect_offset_str (cu->header.sect_off), objf_name);
18963 info_ptr = (str_offsets_section->buffer
18964 + str_offsets_base
18965 + str_index * cu->header.offset_size);
18966 if (cu->header.offset_size == 4)
18967 str_offset = bfd_get_32 (abfd, info_ptr);
18968 else
18969 str_offset = bfd_get_64 (abfd, info_ptr);
18970 if (str_offset >= str_section->size)
18971 error (_("Offset from %s pointing outside of"
18972 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18973 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18974 return (const char *) (str_section->buffer + str_offset);
18975 }
18976
18977 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18978
18979 static const char *
18980 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18981 {
18982 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18983 ? reader->cu->header.addr_size : 0;
18984 return read_str_index (reader->cu,
18985 &reader->dwo_file->sections.str,
18986 &reader->dwo_file->sections.str_offsets,
18987 str_offsets_base, str_index);
18988 }
18989
18990 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18991
18992 static const char *
18993 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18994 {
18995 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18996 const char *objf_name = objfile_name (objfile);
18997 static const char form_name[] = "DW_FORM_GNU_str_index";
18998 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18999
19000 if (!cu->str_offsets_base.has_value ())
19001 error (_("%s used in Fission stub without %s"
19002 " in CU at offset 0x%lx [in module %s]"),
19003 form_name, str_offsets_attr_name,
19004 (long) cu->header.offset_size, objf_name);
19005
19006 return read_str_index (cu,
19007 &cu->per_cu->dwarf2_per_objfile->str,
19008 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19009 *cu->str_offsets_base, str_index);
19010 }
19011
19012 /* Return the length of an LEB128 number in BUF. */
19013
19014 static int
19015 leb128_size (const gdb_byte *buf)
19016 {
19017 const gdb_byte *begin = buf;
19018 gdb_byte byte;
19019
19020 while (1)
19021 {
19022 byte = *buf++;
19023 if ((byte & 128) == 0)
19024 return buf - begin;
19025 }
19026 }
19027
19028 static void
19029 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19030 {
19031 switch (lang)
19032 {
19033 case DW_LANG_C89:
19034 case DW_LANG_C99:
19035 case DW_LANG_C11:
19036 case DW_LANG_C:
19037 case DW_LANG_UPC:
19038 cu->language = language_c;
19039 break;
19040 case DW_LANG_Java:
19041 case DW_LANG_C_plus_plus:
19042 case DW_LANG_C_plus_plus_11:
19043 case DW_LANG_C_plus_plus_14:
19044 cu->language = language_cplus;
19045 break;
19046 case DW_LANG_D:
19047 cu->language = language_d;
19048 break;
19049 case DW_LANG_Fortran77:
19050 case DW_LANG_Fortran90:
19051 case DW_LANG_Fortran95:
19052 case DW_LANG_Fortran03:
19053 case DW_LANG_Fortran08:
19054 cu->language = language_fortran;
19055 break;
19056 case DW_LANG_Go:
19057 cu->language = language_go;
19058 break;
19059 case DW_LANG_Mips_Assembler:
19060 cu->language = language_asm;
19061 break;
19062 case DW_LANG_Ada83:
19063 case DW_LANG_Ada95:
19064 cu->language = language_ada;
19065 break;
19066 case DW_LANG_Modula2:
19067 cu->language = language_m2;
19068 break;
19069 case DW_LANG_Pascal83:
19070 cu->language = language_pascal;
19071 break;
19072 case DW_LANG_ObjC:
19073 cu->language = language_objc;
19074 break;
19075 case DW_LANG_Rust:
19076 case DW_LANG_Rust_old:
19077 cu->language = language_rust;
19078 break;
19079 case DW_LANG_Cobol74:
19080 case DW_LANG_Cobol85:
19081 default:
19082 cu->language = language_minimal;
19083 break;
19084 }
19085 cu->language_defn = language_def (cu->language);
19086 }
19087
19088 /* Return the named attribute or NULL if not there. */
19089
19090 static struct attribute *
19091 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19092 {
19093 for (;;)
19094 {
19095 unsigned int i;
19096 struct attribute *spec = NULL;
19097
19098 for (i = 0; i < die->num_attrs; ++i)
19099 {
19100 if (die->attrs[i].name == name)
19101 return &die->attrs[i];
19102 if (die->attrs[i].name == DW_AT_specification
19103 || die->attrs[i].name == DW_AT_abstract_origin)
19104 spec = &die->attrs[i];
19105 }
19106
19107 if (!spec)
19108 break;
19109
19110 die = follow_die_ref (die, spec, &cu);
19111 }
19112
19113 return NULL;
19114 }
19115
19116 /* Return the named attribute or NULL if not there,
19117 but do not follow DW_AT_specification, etc.
19118 This is for use in contexts where we're reading .debug_types dies.
19119 Following DW_AT_specification, DW_AT_abstract_origin will take us
19120 back up the chain, and we want to go down. */
19121
19122 static struct attribute *
19123 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19124 {
19125 unsigned int i;
19126
19127 for (i = 0; i < die->num_attrs; ++i)
19128 if (die->attrs[i].name == name)
19129 return &die->attrs[i];
19130
19131 return NULL;
19132 }
19133
19134 /* Return the string associated with a string-typed attribute, or NULL if it
19135 is either not found or is of an incorrect type. */
19136
19137 static const char *
19138 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19139 {
19140 struct attribute *attr;
19141 const char *str = NULL;
19142
19143 attr = dwarf2_attr (die, name, cu);
19144
19145 if (attr != NULL)
19146 {
19147 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19148 || attr->form == DW_FORM_string
19149 || attr->form == DW_FORM_strx
19150 || attr->form == DW_FORM_strx1
19151 || attr->form == DW_FORM_strx2
19152 || attr->form == DW_FORM_strx3
19153 || attr->form == DW_FORM_strx4
19154 || attr->form == DW_FORM_GNU_str_index
19155 || attr->form == DW_FORM_GNU_strp_alt)
19156 str = DW_STRING (attr);
19157 else
19158 complaint (_("string type expected for attribute %s for "
19159 "DIE at %s in module %s"),
19160 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19161 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19162 }
19163
19164 return str;
19165 }
19166
19167 /* Return the dwo name or NULL if not present. If present, it is in either
19168 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19169 static const char *
19170 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19171 {
19172 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19173 if (dwo_name == nullptr)
19174 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19175 return dwo_name;
19176 }
19177
19178 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19179 and holds a non-zero value. This function should only be used for
19180 DW_FORM_flag or DW_FORM_flag_present attributes. */
19181
19182 static int
19183 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19184 {
19185 struct attribute *attr = dwarf2_attr (die, name, cu);
19186
19187 return (attr && DW_UNSND (attr));
19188 }
19189
19190 static int
19191 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19192 {
19193 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19194 which value is non-zero. However, we have to be careful with
19195 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19196 (via dwarf2_flag_true_p) follows this attribute. So we may
19197 end up accidently finding a declaration attribute that belongs
19198 to a different DIE referenced by the specification attribute,
19199 even though the given DIE does not have a declaration attribute. */
19200 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19201 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19202 }
19203
19204 /* Return the die giving the specification for DIE, if there is
19205 one. *SPEC_CU is the CU containing DIE on input, and the CU
19206 containing the return value on output. If there is no
19207 specification, but there is an abstract origin, that is
19208 returned. */
19209
19210 static struct die_info *
19211 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19212 {
19213 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19214 *spec_cu);
19215
19216 if (spec_attr == NULL)
19217 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19218
19219 if (spec_attr == NULL)
19220 return NULL;
19221 else
19222 return follow_die_ref (die, spec_attr, spec_cu);
19223 }
19224
19225 /* Stub for free_line_header to match void * callback types. */
19226
19227 static void
19228 free_line_header_voidp (void *arg)
19229 {
19230 struct line_header *lh = (struct line_header *) arg;
19231
19232 delete lh;
19233 }
19234
19235 /* A convenience function to find the proper .debug_line section for a CU. */
19236
19237 static struct dwarf2_section_info *
19238 get_debug_line_section (struct dwarf2_cu *cu)
19239 {
19240 struct dwarf2_section_info *section;
19241 struct dwarf2_per_objfile *dwarf2_per_objfile
19242 = cu->per_cu->dwarf2_per_objfile;
19243
19244 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19245 DWO file. */
19246 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19247 section = &cu->dwo_unit->dwo_file->sections.line;
19248 else if (cu->per_cu->is_dwz)
19249 {
19250 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19251
19252 section = &dwz->line;
19253 }
19254 else
19255 section = &dwarf2_per_objfile->line;
19256
19257 return section;
19258 }
19259
19260 /* Read directory or file name entry format, starting with byte of
19261 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19262 entries count and the entries themselves in the described entry
19263 format. */
19264
19265 static void
19266 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19267 bfd *abfd, const gdb_byte **bufp,
19268 struct line_header *lh,
19269 const struct comp_unit_head *cu_header,
19270 void (*callback) (struct line_header *lh,
19271 const char *name,
19272 dir_index d_index,
19273 unsigned int mod_time,
19274 unsigned int length))
19275 {
19276 gdb_byte format_count, formati;
19277 ULONGEST data_count, datai;
19278 const gdb_byte *buf = *bufp;
19279 const gdb_byte *format_header_data;
19280 unsigned int bytes_read;
19281
19282 format_count = read_1_byte (abfd, buf);
19283 buf += 1;
19284 format_header_data = buf;
19285 for (formati = 0; formati < format_count; formati++)
19286 {
19287 read_unsigned_leb128 (abfd, buf, &bytes_read);
19288 buf += bytes_read;
19289 read_unsigned_leb128 (abfd, buf, &bytes_read);
19290 buf += bytes_read;
19291 }
19292
19293 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19294 buf += bytes_read;
19295 for (datai = 0; datai < data_count; datai++)
19296 {
19297 const gdb_byte *format = format_header_data;
19298 struct file_entry fe;
19299
19300 for (formati = 0; formati < format_count; formati++)
19301 {
19302 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19303 format += bytes_read;
19304
19305 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19306 format += bytes_read;
19307
19308 gdb::optional<const char *> string;
19309 gdb::optional<unsigned int> uint;
19310
19311 switch (form)
19312 {
19313 case DW_FORM_string:
19314 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19315 buf += bytes_read;
19316 break;
19317
19318 case DW_FORM_line_strp:
19319 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19320 abfd, buf,
19321 cu_header,
19322 &bytes_read));
19323 buf += bytes_read;
19324 break;
19325
19326 case DW_FORM_data1:
19327 uint.emplace (read_1_byte (abfd, buf));
19328 buf += 1;
19329 break;
19330
19331 case DW_FORM_data2:
19332 uint.emplace (read_2_bytes (abfd, buf));
19333 buf += 2;
19334 break;
19335
19336 case DW_FORM_data4:
19337 uint.emplace (read_4_bytes (abfd, buf));
19338 buf += 4;
19339 break;
19340
19341 case DW_FORM_data8:
19342 uint.emplace (read_8_bytes (abfd, buf));
19343 buf += 8;
19344 break;
19345
19346 case DW_FORM_data16:
19347 /* This is used for MD5, but file_entry does not record MD5s. */
19348 buf += 16;
19349 break;
19350
19351 case DW_FORM_udata:
19352 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19353 buf += bytes_read;
19354 break;
19355
19356 case DW_FORM_block:
19357 /* It is valid only for DW_LNCT_timestamp which is ignored by
19358 current GDB. */
19359 break;
19360 }
19361
19362 switch (content_type)
19363 {
19364 case DW_LNCT_path:
19365 if (string.has_value ())
19366 fe.name = *string;
19367 break;
19368 case DW_LNCT_directory_index:
19369 if (uint.has_value ())
19370 fe.d_index = (dir_index) *uint;
19371 break;
19372 case DW_LNCT_timestamp:
19373 if (uint.has_value ())
19374 fe.mod_time = *uint;
19375 break;
19376 case DW_LNCT_size:
19377 if (uint.has_value ())
19378 fe.length = *uint;
19379 break;
19380 case DW_LNCT_MD5:
19381 break;
19382 default:
19383 complaint (_("Unknown format content type %s"),
19384 pulongest (content_type));
19385 }
19386 }
19387
19388 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
19389 }
19390
19391 *bufp = buf;
19392 }
19393
19394 /* Read the statement program header starting at OFFSET in
19395 .debug_line, or .debug_line.dwo. Return a pointer
19396 to a struct line_header, allocated using xmalloc.
19397 Returns NULL if there is a problem reading the header, e.g., if it
19398 has a version we don't understand.
19399
19400 NOTE: the strings in the include directory and file name tables of
19401 the returned object point into the dwarf line section buffer,
19402 and must not be freed. */
19403
19404 static line_header_up
19405 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19406 {
19407 const gdb_byte *line_ptr;
19408 unsigned int bytes_read, offset_size;
19409 int i;
19410 const char *cur_dir, *cur_file;
19411 struct dwarf2_section_info *section;
19412 bfd *abfd;
19413 struct dwarf2_per_objfile *dwarf2_per_objfile
19414 = cu->per_cu->dwarf2_per_objfile;
19415
19416 section = get_debug_line_section (cu);
19417 section->read (dwarf2_per_objfile->objfile);
19418 if (section->buffer == NULL)
19419 {
19420 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19421 complaint (_("missing .debug_line.dwo section"));
19422 else
19423 complaint (_("missing .debug_line section"));
19424 return 0;
19425 }
19426
19427 /* We can't do this until we know the section is non-empty.
19428 Only then do we know we have such a section. */
19429 abfd = section->get_bfd_owner ();
19430
19431 /* Make sure that at least there's room for the total_length field.
19432 That could be 12 bytes long, but we're just going to fudge that. */
19433 if (to_underlying (sect_off) + 4 >= section->size)
19434 {
19435 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19436 return 0;
19437 }
19438
19439 line_header_up lh (new line_header ());
19440
19441 lh->sect_off = sect_off;
19442 lh->offset_in_dwz = cu->per_cu->is_dwz;
19443
19444 line_ptr = section->buffer + to_underlying (sect_off);
19445
19446 /* Read in the header. */
19447 lh->total_length =
19448 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19449 &bytes_read, &offset_size);
19450 line_ptr += bytes_read;
19451
19452 const gdb_byte *start_here = line_ptr;
19453
19454 if (line_ptr + lh->total_length > (section->buffer + section->size))
19455 {
19456 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19457 return 0;
19458 }
19459 lh->statement_program_end = start_here + lh->total_length;
19460 lh->version = read_2_bytes (abfd, line_ptr);
19461 line_ptr += 2;
19462 if (lh->version > 5)
19463 {
19464 /* This is a version we don't understand. The format could have
19465 changed in ways we don't handle properly so just punt. */
19466 complaint (_("unsupported version in .debug_line section"));
19467 return NULL;
19468 }
19469 if (lh->version >= 5)
19470 {
19471 gdb_byte segment_selector_size;
19472
19473 /* Skip address size. */
19474 read_1_byte (abfd, line_ptr);
19475 line_ptr += 1;
19476
19477 segment_selector_size = read_1_byte (abfd, line_ptr);
19478 line_ptr += 1;
19479 if (segment_selector_size != 0)
19480 {
19481 complaint (_("unsupported segment selector size %u "
19482 "in .debug_line section"),
19483 segment_selector_size);
19484 return NULL;
19485 }
19486 }
19487 lh->header_length = read_offset (abfd, line_ptr, offset_size);
19488 line_ptr += offset_size;
19489 lh->statement_program_start = line_ptr + lh->header_length;
19490 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19491 line_ptr += 1;
19492 if (lh->version >= 4)
19493 {
19494 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19495 line_ptr += 1;
19496 }
19497 else
19498 lh->maximum_ops_per_instruction = 1;
19499
19500 if (lh->maximum_ops_per_instruction == 0)
19501 {
19502 lh->maximum_ops_per_instruction = 1;
19503 complaint (_("invalid maximum_ops_per_instruction "
19504 "in `.debug_line' section"));
19505 }
19506
19507 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19508 line_ptr += 1;
19509 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19510 line_ptr += 1;
19511 lh->line_range = read_1_byte (abfd, line_ptr);
19512 line_ptr += 1;
19513 lh->opcode_base = read_1_byte (abfd, line_ptr);
19514 line_ptr += 1;
19515 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
19516
19517 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19518 for (i = 1; i < lh->opcode_base; ++i)
19519 {
19520 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19521 line_ptr += 1;
19522 }
19523
19524 if (lh->version >= 5)
19525 {
19526 /* Read directory table. */
19527 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19528 &cu->header,
19529 [] (struct line_header *header, const char *name,
19530 dir_index d_index, unsigned int mod_time,
19531 unsigned int length)
19532 {
19533 header->add_include_dir (name);
19534 });
19535
19536 /* Read file name table. */
19537 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19538 &cu->header,
19539 [] (struct line_header *header, const char *name,
19540 dir_index d_index, unsigned int mod_time,
19541 unsigned int length)
19542 {
19543 header->add_file_name (name, d_index, mod_time, length);
19544 });
19545 }
19546 else
19547 {
19548 /* Read directory table. */
19549 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19550 {
19551 line_ptr += bytes_read;
19552 lh->add_include_dir (cur_dir);
19553 }
19554 line_ptr += bytes_read;
19555
19556 /* Read file name table. */
19557 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19558 {
19559 unsigned int mod_time, length;
19560 dir_index d_index;
19561
19562 line_ptr += bytes_read;
19563 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19564 line_ptr += bytes_read;
19565 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19566 line_ptr += bytes_read;
19567 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19568 line_ptr += bytes_read;
19569
19570 lh->add_file_name (cur_file, d_index, mod_time, length);
19571 }
19572 line_ptr += bytes_read;
19573 }
19574
19575 if (line_ptr > (section->buffer + section->size))
19576 complaint (_("line number info header doesn't "
19577 "fit in `.debug_line' section"));
19578
19579 return lh;
19580 }
19581
19582 /* Subroutine of dwarf_decode_lines to simplify it.
19583 Return the file name of the psymtab for the given file_entry.
19584 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19585 If space for the result is malloc'd, *NAME_HOLDER will be set.
19586 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19587
19588 static const char *
19589 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19590 const dwarf2_psymtab *pst,
19591 const char *comp_dir,
19592 gdb::unique_xmalloc_ptr<char> *name_holder)
19593 {
19594 const char *include_name = fe.name;
19595 const char *include_name_to_compare = include_name;
19596 const char *pst_filename;
19597 int file_is_pst;
19598
19599 const char *dir_name = fe.include_dir (lh);
19600
19601 gdb::unique_xmalloc_ptr<char> hold_compare;
19602 if (!IS_ABSOLUTE_PATH (include_name)
19603 && (dir_name != NULL || comp_dir != NULL))
19604 {
19605 /* Avoid creating a duplicate psymtab for PST.
19606 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19607 Before we do the comparison, however, we need to account
19608 for DIR_NAME and COMP_DIR.
19609 First prepend dir_name (if non-NULL). If we still don't
19610 have an absolute path prepend comp_dir (if non-NULL).
19611 However, the directory we record in the include-file's
19612 psymtab does not contain COMP_DIR (to match the
19613 corresponding symtab(s)).
19614
19615 Example:
19616
19617 bash$ cd /tmp
19618 bash$ gcc -g ./hello.c
19619 include_name = "hello.c"
19620 dir_name = "."
19621 DW_AT_comp_dir = comp_dir = "/tmp"
19622 DW_AT_name = "./hello.c"
19623
19624 */
19625
19626 if (dir_name != NULL)
19627 {
19628 name_holder->reset (concat (dir_name, SLASH_STRING,
19629 include_name, (char *) NULL));
19630 include_name = name_holder->get ();
19631 include_name_to_compare = include_name;
19632 }
19633 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19634 {
19635 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19636 include_name, (char *) NULL));
19637 include_name_to_compare = hold_compare.get ();
19638 }
19639 }
19640
19641 pst_filename = pst->filename;
19642 gdb::unique_xmalloc_ptr<char> copied_name;
19643 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19644 {
19645 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19646 pst_filename, (char *) NULL));
19647 pst_filename = copied_name.get ();
19648 }
19649
19650 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19651
19652 if (file_is_pst)
19653 return NULL;
19654 return include_name;
19655 }
19656
19657 /* State machine to track the state of the line number program. */
19658
19659 class lnp_state_machine
19660 {
19661 public:
19662 /* Initialize a machine state for the start of a line number
19663 program. */
19664 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19665 bool record_lines_p);
19666
19667 file_entry *current_file ()
19668 {
19669 /* lh->file_names is 0-based, but the file name numbers in the
19670 statement program are 1-based. */
19671 return m_line_header->file_name_at (m_file);
19672 }
19673
19674 /* Record the line in the state machine. END_SEQUENCE is true if
19675 we're processing the end of a sequence. */
19676 void record_line (bool end_sequence);
19677
19678 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19679 nop-out rest of the lines in this sequence. */
19680 void check_line_address (struct dwarf2_cu *cu,
19681 const gdb_byte *line_ptr,
19682 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19683
19684 void handle_set_discriminator (unsigned int discriminator)
19685 {
19686 m_discriminator = discriminator;
19687 m_line_has_non_zero_discriminator |= discriminator != 0;
19688 }
19689
19690 /* Handle DW_LNE_set_address. */
19691 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19692 {
19693 m_op_index = 0;
19694 address += baseaddr;
19695 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19696 }
19697
19698 /* Handle DW_LNS_advance_pc. */
19699 void handle_advance_pc (CORE_ADDR adjust);
19700
19701 /* Handle a special opcode. */
19702 void handle_special_opcode (unsigned char op_code);
19703
19704 /* Handle DW_LNS_advance_line. */
19705 void handle_advance_line (int line_delta)
19706 {
19707 advance_line (line_delta);
19708 }
19709
19710 /* Handle DW_LNS_set_file. */
19711 void handle_set_file (file_name_index file);
19712
19713 /* Handle DW_LNS_negate_stmt. */
19714 void handle_negate_stmt ()
19715 {
19716 m_is_stmt = !m_is_stmt;
19717 }
19718
19719 /* Handle DW_LNS_const_add_pc. */
19720 void handle_const_add_pc ();
19721
19722 /* Handle DW_LNS_fixed_advance_pc. */
19723 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19724 {
19725 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19726 m_op_index = 0;
19727 }
19728
19729 /* Handle DW_LNS_copy. */
19730 void handle_copy ()
19731 {
19732 record_line (false);
19733 m_discriminator = 0;
19734 }
19735
19736 /* Handle DW_LNE_end_sequence. */
19737 void handle_end_sequence ()
19738 {
19739 m_currently_recording_lines = true;
19740 }
19741
19742 private:
19743 /* Advance the line by LINE_DELTA. */
19744 void advance_line (int line_delta)
19745 {
19746 m_line += line_delta;
19747
19748 if (line_delta != 0)
19749 m_line_has_non_zero_discriminator = m_discriminator != 0;
19750 }
19751
19752 struct dwarf2_cu *m_cu;
19753
19754 gdbarch *m_gdbarch;
19755
19756 /* True if we're recording lines.
19757 Otherwise we're building partial symtabs and are just interested in
19758 finding include files mentioned by the line number program. */
19759 bool m_record_lines_p;
19760
19761 /* The line number header. */
19762 line_header *m_line_header;
19763
19764 /* These are part of the standard DWARF line number state machine,
19765 and initialized according to the DWARF spec. */
19766
19767 unsigned char m_op_index = 0;
19768 /* The line table index of the current file. */
19769 file_name_index m_file = 1;
19770 unsigned int m_line = 1;
19771
19772 /* These are initialized in the constructor. */
19773
19774 CORE_ADDR m_address;
19775 bool m_is_stmt;
19776 unsigned int m_discriminator;
19777
19778 /* Additional bits of state we need to track. */
19779
19780 /* The last file that we called dwarf2_start_subfile for.
19781 This is only used for TLLs. */
19782 unsigned int m_last_file = 0;
19783 /* The last file a line number was recorded for. */
19784 struct subfile *m_last_subfile = NULL;
19785
19786 /* When true, record the lines we decode. */
19787 bool m_currently_recording_lines = false;
19788
19789 /* The last line number that was recorded, used to coalesce
19790 consecutive entries for the same line. This can happen, for
19791 example, when discriminators are present. PR 17276. */
19792 unsigned int m_last_line = 0;
19793 bool m_line_has_non_zero_discriminator = false;
19794 };
19795
19796 void
19797 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19798 {
19799 CORE_ADDR addr_adj = (((m_op_index + adjust)
19800 / m_line_header->maximum_ops_per_instruction)
19801 * m_line_header->minimum_instruction_length);
19802 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19803 m_op_index = ((m_op_index + adjust)
19804 % m_line_header->maximum_ops_per_instruction);
19805 }
19806
19807 void
19808 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19809 {
19810 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19811 CORE_ADDR addr_adj = (((m_op_index
19812 + (adj_opcode / m_line_header->line_range))
19813 / m_line_header->maximum_ops_per_instruction)
19814 * m_line_header->minimum_instruction_length);
19815 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19816 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
19817 % m_line_header->maximum_ops_per_instruction);
19818
19819 int line_delta = (m_line_header->line_base
19820 + (adj_opcode % m_line_header->line_range));
19821 advance_line (line_delta);
19822 record_line (false);
19823 m_discriminator = 0;
19824 }
19825
19826 void
19827 lnp_state_machine::handle_set_file (file_name_index file)
19828 {
19829 m_file = file;
19830
19831 const file_entry *fe = current_file ();
19832 if (fe == NULL)
19833 dwarf2_debug_line_missing_file_complaint ();
19834 else if (m_record_lines_p)
19835 {
19836 const char *dir = fe->include_dir (m_line_header);
19837
19838 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19839 m_line_has_non_zero_discriminator = m_discriminator != 0;
19840 dwarf2_start_subfile (m_cu, fe->name, dir);
19841 }
19842 }
19843
19844 void
19845 lnp_state_machine::handle_const_add_pc ()
19846 {
19847 CORE_ADDR adjust
19848 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19849
19850 CORE_ADDR addr_adj
19851 = (((m_op_index + adjust)
19852 / m_line_header->maximum_ops_per_instruction)
19853 * m_line_header->minimum_instruction_length);
19854
19855 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19856 m_op_index = ((m_op_index + adjust)
19857 % m_line_header->maximum_ops_per_instruction);
19858 }
19859
19860 /* Return non-zero if we should add LINE to the line number table.
19861 LINE is the line to add, LAST_LINE is the last line that was added,
19862 LAST_SUBFILE is the subfile for LAST_LINE.
19863 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19864 had a non-zero discriminator.
19865
19866 We have to be careful in the presence of discriminators.
19867 E.g., for this line:
19868
19869 for (i = 0; i < 100000; i++);
19870
19871 clang can emit four line number entries for that one line,
19872 each with a different discriminator.
19873 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19874
19875 However, we want gdb to coalesce all four entries into one.
19876 Otherwise the user could stepi into the middle of the line and
19877 gdb would get confused about whether the pc really was in the
19878 middle of the line.
19879
19880 Things are further complicated by the fact that two consecutive
19881 line number entries for the same line is a heuristic used by gcc
19882 to denote the end of the prologue. So we can't just discard duplicate
19883 entries, we have to be selective about it. The heuristic we use is
19884 that we only collapse consecutive entries for the same line if at least
19885 one of those entries has a non-zero discriminator. PR 17276.
19886
19887 Note: Addresses in the line number state machine can never go backwards
19888 within one sequence, thus this coalescing is ok. */
19889
19890 static int
19891 dwarf_record_line_p (struct dwarf2_cu *cu,
19892 unsigned int line, unsigned int last_line,
19893 int line_has_non_zero_discriminator,
19894 struct subfile *last_subfile)
19895 {
19896 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19897 return 1;
19898 if (line != last_line)
19899 return 1;
19900 /* Same line for the same file that we've seen already.
19901 As a last check, for pr 17276, only record the line if the line
19902 has never had a non-zero discriminator. */
19903 if (!line_has_non_zero_discriminator)
19904 return 1;
19905 return 0;
19906 }
19907
19908 /* Use the CU's builder to record line number LINE beginning at
19909 address ADDRESS in the line table of subfile SUBFILE. */
19910
19911 static void
19912 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19913 unsigned int line, CORE_ADDR address,
19914 struct dwarf2_cu *cu)
19915 {
19916 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19917
19918 if (dwarf_line_debug)
19919 {
19920 fprintf_unfiltered (gdb_stdlog,
19921 "Recording line %u, file %s, address %s\n",
19922 line, lbasename (subfile->name),
19923 paddress (gdbarch, address));
19924 }
19925
19926 if (cu != nullptr)
19927 cu->get_builder ()->record_line (subfile, line, addr);
19928 }
19929
19930 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19931 Mark the end of a set of line number records.
19932 The arguments are the same as for dwarf_record_line_1.
19933 If SUBFILE is NULL the request is ignored. */
19934
19935 static void
19936 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19937 CORE_ADDR address, struct dwarf2_cu *cu)
19938 {
19939 if (subfile == NULL)
19940 return;
19941
19942 if (dwarf_line_debug)
19943 {
19944 fprintf_unfiltered (gdb_stdlog,
19945 "Finishing current line, file %s, address %s\n",
19946 lbasename (subfile->name),
19947 paddress (gdbarch, address));
19948 }
19949
19950 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
19951 }
19952
19953 void
19954 lnp_state_machine::record_line (bool end_sequence)
19955 {
19956 if (dwarf_line_debug)
19957 {
19958 fprintf_unfiltered (gdb_stdlog,
19959 "Processing actual line %u: file %u,"
19960 " address %s, is_stmt %u, discrim %u%s\n",
19961 m_line, m_file,
19962 paddress (m_gdbarch, m_address),
19963 m_is_stmt, m_discriminator,
19964 (end_sequence ? "\t(end sequence)" : ""));
19965 }
19966
19967 file_entry *fe = current_file ();
19968
19969 if (fe == NULL)
19970 dwarf2_debug_line_missing_file_complaint ();
19971 /* For now we ignore lines not starting on an instruction boundary.
19972 But not when processing end_sequence for compatibility with the
19973 previous version of the code. */
19974 else if (m_op_index == 0 || end_sequence)
19975 {
19976 fe->included_p = 1;
19977 if (m_record_lines_p
19978 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
19979 {
19980 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19981 || end_sequence)
19982 {
19983 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19984 m_currently_recording_lines ? m_cu : nullptr);
19985 }
19986
19987 if (!end_sequence)
19988 {
19989 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19990 m_line_has_non_zero_discriminator,
19991 m_last_subfile))
19992 {
19993 buildsym_compunit *builder = m_cu->get_builder ();
19994 dwarf_record_line_1 (m_gdbarch,
19995 builder->get_current_subfile (),
19996 m_line, m_address,
19997 m_currently_recording_lines ? m_cu : nullptr);
19998 }
19999 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20000 m_last_line = m_line;
20001 }
20002 }
20003 }
20004 }
20005
20006 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20007 line_header *lh, bool record_lines_p)
20008 {
20009 m_cu = cu;
20010 m_gdbarch = arch;
20011 m_record_lines_p = record_lines_p;
20012 m_line_header = lh;
20013
20014 m_currently_recording_lines = true;
20015
20016 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20017 was a line entry for it so that the backend has a chance to adjust it
20018 and also record it in case it needs it. This is currently used by MIPS
20019 code, cf. `mips_adjust_dwarf2_line'. */
20020 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20021 m_is_stmt = lh->default_is_stmt;
20022 m_discriminator = 0;
20023 }
20024
20025 void
20026 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20027 const gdb_byte *line_ptr,
20028 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20029 {
20030 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20031 the pc range of the CU. However, we restrict the test to only ADDRESS
20032 values of zero to preserve GDB's previous behaviour which is to handle
20033 the specific case of a function being GC'd by the linker. */
20034
20035 if (address == 0 && address < unrelocated_lowpc)
20036 {
20037 /* This line table is for a function which has been
20038 GCd by the linker. Ignore it. PR gdb/12528 */
20039
20040 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20041 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20042
20043 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20044 line_offset, objfile_name (objfile));
20045 m_currently_recording_lines = false;
20046 /* Note: m_currently_recording_lines is left as false until we see
20047 DW_LNE_end_sequence. */
20048 }
20049 }
20050
20051 /* Subroutine of dwarf_decode_lines to simplify it.
20052 Process the line number information in LH.
20053 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20054 program in order to set included_p for every referenced header. */
20055
20056 static void
20057 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20058 const int decode_for_pst_p, CORE_ADDR lowpc)
20059 {
20060 const gdb_byte *line_ptr, *extended_end;
20061 const gdb_byte *line_end;
20062 unsigned int bytes_read, extended_len;
20063 unsigned char op_code, extended_op;
20064 CORE_ADDR baseaddr;
20065 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20066 bfd *abfd = objfile->obfd;
20067 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20068 /* True if we're recording line info (as opposed to building partial
20069 symtabs and just interested in finding include files mentioned by
20070 the line number program). */
20071 bool record_lines_p = !decode_for_pst_p;
20072
20073 baseaddr = objfile->text_section_offset ();
20074
20075 line_ptr = lh->statement_program_start;
20076 line_end = lh->statement_program_end;
20077
20078 /* Read the statement sequences until there's nothing left. */
20079 while (line_ptr < line_end)
20080 {
20081 /* The DWARF line number program state machine. Reset the state
20082 machine at the start of each sequence. */
20083 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20084 bool end_sequence = false;
20085
20086 if (record_lines_p)
20087 {
20088 /* Start a subfile for the current file of the state
20089 machine. */
20090 const file_entry *fe = state_machine.current_file ();
20091
20092 if (fe != NULL)
20093 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20094 }
20095
20096 /* Decode the table. */
20097 while (line_ptr < line_end && !end_sequence)
20098 {
20099 op_code = read_1_byte (abfd, line_ptr);
20100 line_ptr += 1;
20101
20102 if (op_code >= lh->opcode_base)
20103 {
20104 /* Special opcode. */
20105 state_machine.handle_special_opcode (op_code);
20106 }
20107 else switch (op_code)
20108 {
20109 case DW_LNS_extended_op:
20110 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20111 &bytes_read);
20112 line_ptr += bytes_read;
20113 extended_end = line_ptr + extended_len;
20114 extended_op = read_1_byte (abfd, line_ptr);
20115 line_ptr += 1;
20116 switch (extended_op)
20117 {
20118 case DW_LNE_end_sequence:
20119 state_machine.handle_end_sequence ();
20120 end_sequence = true;
20121 break;
20122 case DW_LNE_set_address:
20123 {
20124 CORE_ADDR address
20125 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20126 line_ptr += bytes_read;
20127
20128 state_machine.check_line_address (cu, line_ptr,
20129 lowpc - baseaddr, address);
20130 state_machine.handle_set_address (baseaddr, address);
20131 }
20132 break;
20133 case DW_LNE_define_file:
20134 {
20135 const char *cur_file;
20136 unsigned int mod_time, length;
20137 dir_index dindex;
20138
20139 cur_file = read_direct_string (abfd, line_ptr,
20140 &bytes_read);
20141 line_ptr += bytes_read;
20142 dindex = (dir_index)
20143 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20144 line_ptr += bytes_read;
20145 mod_time =
20146 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20147 line_ptr += bytes_read;
20148 length =
20149 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20150 line_ptr += bytes_read;
20151 lh->add_file_name (cur_file, dindex, mod_time, length);
20152 }
20153 break;
20154 case DW_LNE_set_discriminator:
20155 {
20156 /* The discriminator is not interesting to the
20157 debugger; just ignore it. We still need to
20158 check its value though:
20159 if there are consecutive entries for the same
20160 (non-prologue) line we want to coalesce them.
20161 PR 17276. */
20162 unsigned int discr
20163 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20164 line_ptr += bytes_read;
20165
20166 state_machine.handle_set_discriminator (discr);
20167 }
20168 break;
20169 default:
20170 complaint (_("mangled .debug_line section"));
20171 return;
20172 }
20173 /* Make sure that we parsed the extended op correctly. If e.g.
20174 we expected a different address size than the producer used,
20175 we may have read the wrong number of bytes. */
20176 if (line_ptr != extended_end)
20177 {
20178 complaint (_("mangled .debug_line section"));
20179 return;
20180 }
20181 break;
20182 case DW_LNS_copy:
20183 state_machine.handle_copy ();
20184 break;
20185 case DW_LNS_advance_pc:
20186 {
20187 CORE_ADDR adjust
20188 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20189 line_ptr += bytes_read;
20190
20191 state_machine.handle_advance_pc (adjust);
20192 }
20193 break;
20194 case DW_LNS_advance_line:
20195 {
20196 int line_delta
20197 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20198 line_ptr += bytes_read;
20199
20200 state_machine.handle_advance_line (line_delta);
20201 }
20202 break;
20203 case DW_LNS_set_file:
20204 {
20205 file_name_index file
20206 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20207 &bytes_read);
20208 line_ptr += bytes_read;
20209
20210 state_machine.handle_set_file (file);
20211 }
20212 break;
20213 case DW_LNS_set_column:
20214 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20215 line_ptr += bytes_read;
20216 break;
20217 case DW_LNS_negate_stmt:
20218 state_machine.handle_negate_stmt ();
20219 break;
20220 case DW_LNS_set_basic_block:
20221 break;
20222 /* Add to the address register of the state machine the
20223 address increment value corresponding to special opcode
20224 255. I.e., this value is scaled by the minimum
20225 instruction length since special opcode 255 would have
20226 scaled the increment. */
20227 case DW_LNS_const_add_pc:
20228 state_machine.handle_const_add_pc ();
20229 break;
20230 case DW_LNS_fixed_advance_pc:
20231 {
20232 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20233 line_ptr += 2;
20234
20235 state_machine.handle_fixed_advance_pc (addr_adj);
20236 }
20237 break;
20238 default:
20239 {
20240 /* Unknown standard opcode, ignore it. */
20241 int i;
20242
20243 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20244 {
20245 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20246 line_ptr += bytes_read;
20247 }
20248 }
20249 }
20250 }
20251
20252 if (!end_sequence)
20253 dwarf2_debug_line_missing_end_sequence_complaint ();
20254
20255 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20256 in which case we still finish recording the last line). */
20257 state_machine.record_line (true);
20258 }
20259 }
20260
20261 /* Decode the Line Number Program (LNP) for the given line_header
20262 structure and CU. The actual information extracted and the type
20263 of structures created from the LNP depends on the value of PST.
20264
20265 1. If PST is NULL, then this procedure uses the data from the program
20266 to create all necessary symbol tables, and their linetables.
20267
20268 2. If PST is not NULL, this procedure reads the program to determine
20269 the list of files included by the unit represented by PST, and
20270 builds all the associated partial symbol tables.
20271
20272 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20273 It is used for relative paths in the line table.
20274 NOTE: When processing partial symtabs (pst != NULL),
20275 comp_dir == pst->dirname.
20276
20277 NOTE: It is important that psymtabs have the same file name (via strcmp)
20278 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20279 symtab we don't use it in the name of the psymtabs we create.
20280 E.g. expand_line_sal requires this when finding psymtabs to expand.
20281 A good testcase for this is mb-inline.exp.
20282
20283 LOWPC is the lowest address in CU (or 0 if not known).
20284
20285 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20286 for its PC<->lines mapping information. Otherwise only the filename
20287 table is read in. */
20288
20289 static void
20290 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20291 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20292 CORE_ADDR lowpc, int decode_mapping)
20293 {
20294 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20295 const int decode_for_pst_p = (pst != NULL);
20296
20297 if (decode_mapping)
20298 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20299
20300 if (decode_for_pst_p)
20301 {
20302 /* Now that we're done scanning the Line Header Program, we can
20303 create the psymtab of each included file. */
20304 for (auto &file_entry : lh->file_names ())
20305 if (file_entry.included_p == 1)
20306 {
20307 gdb::unique_xmalloc_ptr<char> name_holder;
20308 const char *include_name =
20309 psymtab_include_file_name (lh, file_entry, pst,
20310 comp_dir, &name_holder);
20311 if (include_name != NULL)
20312 dwarf2_create_include_psymtab (include_name, pst, objfile);
20313 }
20314 }
20315 else
20316 {
20317 /* Make sure a symtab is created for every file, even files
20318 which contain only variables (i.e. no code with associated
20319 line numbers). */
20320 buildsym_compunit *builder = cu->get_builder ();
20321 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20322
20323 for (auto &fe : lh->file_names ())
20324 {
20325 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20326 if (builder->get_current_subfile ()->symtab == NULL)
20327 {
20328 builder->get_current_subfile ()->symtab
20329 = allocate_symtab (cust,
20330 builder->get_current_subfile ()->name);
20331 }
20332 fe.symtab = builder->get_current_subfile ()->symtab;
20333 }
20334 }
20335 }
20336
20337 /* Start a subfile for DWARF. FILENAME is the name of the file and
20338 DIRNAME the name of the source directory which contains FILENAME
20339 or NULL if not known.
20340 This routine tries to keep line numbers from identical absolute and
20341 relative file names in a common subfile.
20342
20343 Using the `list' example from the GDB testsuite, which resides in
20344 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20345 of /srcdir/list0.c yields the following debugging information for list0.c:
20346
20347 DW_AT_name: /srcdir/list0.c
20348 DW_AT_comp_dir: /compdir
20349 files.files[0].name: list0.h
20350 files.files[0].dir: /srcdir
20351 files.files[1].name: list0.c
20352 files.files[1].dir: /srcdir
20353
20354 The line number information for list0.c has to end up in a single
20355 subfile, so that `break /srcdir/list0.c:1' works as expected.
20356 start_subfile will ensure that this happens provided that we pass the
20357 concatenation of files.files[1].dir and files.files[1].name as the
20358 subfile's name. */
20359
20360 static void
20361 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20362 const char *dirname)
20363 {
20364 gdb::unique_xmalloc_ptr<char> copy;
20365
20366 /* In order not to lose the line information directory,
20367 we concatenate it to the filename when it makes sense.
20368 Note that the Dwarf3 standard says (speaking of filenames in line
20369 information): ``The directory index is ignored for file names
20370 that represent full path names''. Thus ignoring dirname in the
20371 `else' branch below isn't an issue. */
20372
20373 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20374 {
20375 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20376 filename = copy.get ();
20377 }
20378
20379 cu->get_builder ()->start_subfile (filename);
20380 }
20381
20382 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20383 buildsym_compunit constructor. */
20384
20385 struct compunit_symtab *
20386 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20387 CORE_ADDR low_pc)
20388 {
20389 gdb_assert (m_builder == nullptr);
20390
20391 m_builder.reset (new struct buildsym_compunit
20392 (per_cu->dwarf2_per_objfile->objfile,
20393 name, comp_dir, language, low_pc));
20394
20395 list_in_scope = get_builder ()->get_file_symbols ();
20396
20397 get_builder ()->record_debugformat ("DWARF 2");
20398 get_builder ()->record_producer (producer);
20399
20400 processing_has_namespace_info = false;
20401
20402 return get_builder ()->get_compunit_symtab ();
20403 }
20404
20405 static void
20406 var_decode_location (struct attribute *attr, struct symbol *sym,
20407 struct dwarf2_cu *cu)
20408 {
20409 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20410 struct comp_unit_head *cu_header = &cu->header;
20411
20412 /* NOTE drow/2003-01-30: There used to be a comment and some special
20413 code here to turn a symbol with DW_AT_external and a
20414 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20415 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20416 with some versions of binutils) where shared libraries could have
20417 relocations against symbols in their debug information - the
20418 minimal symbol would have the right address, but the debug info
20419 would not. It's no longer necessary, because we will explicitly
20420 apply relocations when we read in the debug information now. */
20421
20422 /* A DW_AT_location attribute with no contents indicates that a
20423 variable has been optimized away. */
20424 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20425 {
20426 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20427 return;
20428 }
20429
20430 /* Handle one degenerate form of location expression specially, to
20431 preserve GDB's previous behavior when section offsets are
20432 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20433 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20434
20435 if (attr->form_is_block ()
20436 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20437 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20438 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20439 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20440 && (DW_BLOCK (attr)->size
20441 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20442 {
20443 unsigned int dummy;
20444
20445 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20446 SET_SYMBOL_VALUE_ADDRESS
20447 (sym, cu->header.read_address (objfile->obfd,
20448 DW_BLOCK (attr)->data + 1,
20449 &dummy));
20450 else
20451 SET_SYMBOL_VALUE_ADDRESS
20452 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20453 &dummy));
20454 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20455 fixup_symbol_section (sym, objfile);
20456 SET_SYMBOL_VALUE_ADDRESS
20457 (sym,
20458 SYMBOL_VALUE_ADDRESS (sym)
20459 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20460 return;
20461 }
20462
20463 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20464 expression evaluator, and use LOC_COMPUTED only when necessary
20465 (i.e. when the value of a register or memory location is
20466 referenced, or a thread-local block, etc.). Then again, it might
20467 not be worthwhile. I'm assuming that it isn't unless performance
20468 or memory numbers show me otherwise. */
20469
20470 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20471
20472 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20473 cu->has_loclist = true;
20474 }
20475
20476 /* Given a pointer to a DWARF information entry, figure out if we need
20477 to make a symbol table entry for it, and if so, create a new entry
20478 and return a pointer to it.
20479 If TYPE is NULL, determine symbol type from the die, otherwise
20480 used the passed type.
20481 If SPACE is not NULL, use it to hold the new symbol. If it is
20482 NULL, allocate a new symbol on the objfile's obstack. */
20483
20484 static struct symbol *
20485 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20486 struct symbol *space)
20487 {
20488 struct dwarf2_per_objfile *dwarf2_per_objfile
20489 = cu->per_cu->dwarf2_per_objfile;
20490 struct objfile *objfile = dwarf2_per_objfile->objfile;
20491 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20492 struct symbol *sym = NULL;
20493 const char *name;
20494 struct attribute *attr = NULL;
20495 struct attribute *attr2 = NULL;
20496 CORE_ADDR baseaddr;
20497 struct pending **list_to_add = NULL;
20498
20499 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20500
20501 baseaddr = objfile->text_section_offset ();
20502
20503 name = dwarf2_name (die, cu);
20504 if (name)
20505 {
20506 const char *linkagename;
20507 int suppress_add = 0;
20508
20509 if (space)
20510 sym = space;
20511 else
20512 sym = allocate_symbol (objfile);
20513 OBJSTAT (objfile, n_syms++);
20514
20515 /* Cache this symbol's name and the name's demangled form (if any). */
20516 sym->set_language (cu->language, &objfile->objfile_obstack);
20517 linkagename = dwarf2_physname (name, die, cu);
20518 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20519
20520 /* Fortran does not have mangling standard and the mangling does differ
20521 between gfortran, iFort etc. */
20522 if (cu->language == language_fortran
20523 && symbol_get_demangled_name (sym) == NULL)
20524 symbol_set_demangled_name (sym,
20525 dwarf2_full_name (name, die, cu),
20526 NULL);
20527
20528 /* Default assumptions.
20529 Use the passed type or decode it from the die. */
20530 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20531 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20532 if (type != NULL)
20533 SYMBOL_TYPE (sym) = type;
20534 else
20535 SYMBOL_TYPE (sym) = die_type (die, cu);
20536 attr = dwarf2_attr (die,
20537 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20538 cu);
20539 if (attr != nullptr)
20540 {
20541 SYMBOL_LINE (sym) = DW_UNSND (attr);
20542 }
20543
20544 attr = dwarf2_attr (die,
20545 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20546 cu);
20547 if (attr != nullptr)
20548 {
20549 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20550 struct file_entry *fe;
20551
20552 if (cu->line_header != NULL)
20553 fe = cu->line_header->file_name_at (file_index);
20554 else
20555 fe = NULL;
20556
20557 if (fe == NULL)
20558 complaint (_("file index out of range"));
20559 else
20560 symbol_set_symtab (sym, fe->symtab);
20561 }
20562
20563 switch (die->tag)
20564 {
20565 case DW_TAG_label:
20566 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20567 if (attr != nullptr)
20568 {
20569 CORE_ADDR addr;
20570
20571 addr = attr->value_as_address ();
20572 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20573 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20574 }
20575 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20576 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20577 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20578 add_symbol_to_list (sym, cu->list_in_scope);
20579 break;
20580 case DW_TAG_subprogram:
20581 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20582 finish_block. */
20583 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20584 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20585 if ((attr2 && (DW_UNSND (attr2) != 0))
20586 || cu->language == language_ada
20587 || cu->language == language_fortran)
20588 {
20589 /* Subprograms marked external are stored as a global symbol.
20590 Ada and Fortran subprograms, whether marked external or
20591 not, are always stored as a global symbol, because we want
20592 to be able to access them globally. For instance, we want
20593 to be able to break on a nested subprogram without having
20594 to specify the context. */
20595 list_to_add = cu->get_builder ()->get_global_symbols ();
20596 }
20597 else
20598 {
20599 list_to_add = cu->list_in_scope;
20600 }
20601 break;
20602 case DW_TAG_inlined_subroutine:
20603 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20604 finish_block. */
20605 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20606 SYMBOL_INLINED (sym) = 1;
20607 list_to_add = cu->list_in_scope;
20608 break;
20609 case DW_TAG_template_value_param:
20610 suppress_add = 1;
20611 /* Fall through. */
20612 case DW_TAG_constant:
20613 case DW_TAG_variable:
20614 case DW_TAG_member:
20615 /* Compilation with minimal debug info may result in
20616 variables with missing type entries. Change the
20617 misleading `void' type to something sensible. */
20618 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20619 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20620
20621 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20622 /* In the case of DW_TAG_member, we should only be called for
20623 static const members. */
20624 if (die->tag == DW_TAG_member)
20625 {
20626 /* dwarf2_add_field uses die_is_declaration,
20627 so we do the same. */
20628 gdb_assert (die_is_declaration (die, cu));
20629 gdb_assert (attr);
20630 }
20631 if (attr != nullptr)
20632 {
20633 dwarf2_const_value (attr, sym, cu);
20634 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20635 if (!suppress_add)
20636 {
20637 if (attr2 && (DW_UNSND (attr2) != 0))
20638 list_to_add = cu->get_builder ()->get_global_symbols ();
20639 else
20640 list_to_add = cu->list_in_scope;
20641 }
20642 break;
20643 }
20644 attr = dwarf2_attr (die, DW_AT_location, cu);
20645 if (attr != nullptr)
20646 {
20647 var_decode_location (attr, sym, cu);
20648 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20649
20650 /* Fortran explicitly imports any global symbols to the local
20651 scope by DW_TAG_common_block. */
20652 if (cu->language == language_fortran && die->parent
20653 && die->parent->tag == DW_TAG_common_block)
20654 attr2 = NULL;
20655
20656 if (SYMBOL_CLASS (sym) == LOC_STATIC
20657 && SYMBOL_VALUE_ADDRESS (sym) == 0
20658 && !dwarf2_per_objfile->has_section_at_zero)
20659 {
20660 /* When a static variable is eliminated by the linker,
20661 the corresponding debug information is not stripped
20662 out, but the variable address is set to null;
20663 do not add such variables into symbol table. */
20664 }
20665 else if (attr2 && (DW_UNSND (attr2) != 0))
20666 {
20667 if (SYMBOL_CLASS (sym) == LOC_STATIC
20668 && (objfile->flags & OBJF_MAINLINE) == 0
20669 && dwarf2_per_objfile->can_copy)
20670 {
20671 /* A global static variable might be subject to
20672 copy relocation. We first check for a local
20673 minsym, though, because maybe the symbol was
20674 marked hidden, in which case this would not
20675 apply. */
20676 bound_minimal_symbol found
20677 = (lookup_minimal_symbol_linkage
20678 (sym->linkage_name (), objfile));
20679 if (found.minsym != nullptr)
20680 sym->maybe_copied = 1;
20681 }
20682
20683 /* A variable with DW_AT_external is never static,
20684 but it may be block-scoped. */
20685 list_to_add
20686 = ((cu->list_in_scope
20687 == cu->get_builder ()->get_file_symbols ())
20688 ? cu->get_builder ()->get_global_symbols ()
20689 : cu->list_in_scope);
20690 }
20691 else
20692 list_to_add = cu->list_in_scope;
20693 }
20694 else
20695 {
20696 /* We do not know the address of this symbol.
20697 If it is an external symbol and we have type information
20698 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20699 The address of the variable will then be determined from
20700 the minimal symbol table whenever the variable is
20701 referenced. */
20702 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20703
20704 /* Fortran explicitly imports any global symbols to the local
20705 scope by DW_TAG_common_block. */
20706 if (cu->language == language_fortran && die->parent
20707 && die->parent->tag == DW_TAG_common_block)
20708 {
20709 /* SYMBOL_CLASS doesn't matter here because
20710 read_common_block is going to reset it. */
20711 if (!suppress_add)
20712 list_to_add = cu->list_in_scope;
20713 }
20714 else if (attr2 && (DW_UNSND (attr2) != 0)
20715 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20716 {
20717 /* A variable with DW_AT_external is never static, but it
20718 may be block-scoped. */
20719 list_to_add
20720 = ((cu->list_in_scope
20721 == cu->get_builder ()->get_file_symbols ())
20722 ? cu->get_builder ()->get_global_symbols ()
20723 : cu->list_in_scope);
20724
20725 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20726 }
20727 else if (!die_is_declaration (die, cu))
20728 {
20729 /* Use the default LOC_OPTIMIZED_OUT class. */
20730 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20731 if (!suppress_add)
20732 list_to_add = cu->list_in_scope;
20733 }
20734 }
20735 break;
20736 case DW_TAG_formal_parameter:
20737 {
20738 /* If we are inside a function, mark this as an argument. If
20739 not, we might be looking at an argument to an inlined function
20740 when we do not have enough information to show inlined frames;
20741 pretend it's a local variable in that case so that the user can
20742 still see it. */
20743 struct context_stack *curr
20744 = cu->get_builder ()->get_current_context_stack ();
20745 if (curr != nullptr && curr->name != nullptr)
20746 SYMBOL_IS_ARGUMENT (sym) = 1;
20747 attr = dwarf2_attr (die, DW_AT_location, cu);
20748 if (attr != nullptr)
20749 {
20750 var_decode_location (attr, sym, cu);
20751 }
20752 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20753 if (attr != nullptr)
20754 {
20755 dwarf2_const_value (attr, sym, cu);
20756 }
20757
20758 list_to_add = cu->list_in_scope;
20759 }
20760 break;
20761 case DW_TAG_unspecified_parameters:
20762 /* From varargs functions; gdb doesn't seem to have any
20763 interest in this information, so just ignore it for now.
20764 (FIXME?) */
20765 break;
20766 case DW_TAG_template_type_param:
20767 suppress_add = 1;
20768 /* Fall through. */
20769 case DW_TAG_class_type:
20770 case DW_TAG_interface_type:
20771 case DW_TAG_structure_type:
20772 case DW_TAG_union_type:
20773 case DW_TAG_set_type:
20774 case DW_TAG_enumeration_type:
20775 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20776 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20777
20778 {
20779 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20780 really ever be static objects: otherwise, if you try
20781 to, say, break of a class's method and you're in a file
20782 which doesn't mention that class, it won't work unless
20783 the check for all static symbols in lookup_symbol_aux
20784 saves you. See the OtherFileClass tests in
20785 gdb.c++/namespace.exp. */
20786
20787 if (!suppress_add)
20788 {
20789 buildsym_compunit *builder = cu->get_builder ();
20790 list_to_add
20791 = (cu->list_in_scope == builder->get_file_symbols ()
20792 && cu->language == language_cplus
20793 ? builder->get_global_symbols ()
20794 : cu->list_in_scope);
20795
20796 /* The semantics of C++ state that "struct foo {
20797 ... }" also defines a typedef for "foo". */
20798 if (cu->language == language_cplus
20799 || cu->language == language_ada
20800 || cu->language == language_d
20801 || cu->language == language_rust)
20802 {
20803 /* The symbol's name is already allocated along
20804 with this objfile, so we don't need to
20805 duplicate it for the type. */
20806 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20807 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20808 }
20809 }
20810 }
20811 break;
20812 case DW_TAG_typedef:
20813 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20814 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20815 list_to_add = cu->list_in_scope;
20816 break;
20817 case DW_TAG_base_type:
20818 case DW_TAG_subrange_type:
20819 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20820 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20821 list_to_add = cu->list_in_scope;
20822 break;
20823 case DW_TAG_enumerator:
20824 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20825 if (attr != nullptr)
20826 {
20827 dwarf2_const_value (attr, sym, cu);
20828 }
20829 {
20830 /* NOTE: carlton/2003-11-10: See comment above in the
20831 DW_TAG_class_type, etc. block. */
20832
20833 list_to_add
20834 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20835 && cu->language == language_cplus
20836 ? cu->get_builder ()->get_global_symbols ()
20837 : cu->list_in_scope);
20838 }
20839 break;
20840 case DW_TAG_imported_declaration:
20841 case DW_TAG_namespace:
20842 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20843 list_to_add = cu->get_builder ()->get_global_symbols ();
20844 break;
20845 case DW_TAG_module:
20846 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20847 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20848 list_to_add = cu->get_builder ()->get_global_symbols ();
20849 break;
20850 case DW_TAG_common_block:
20851 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20852 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20853 add_symbol_to_list (sym, cu->list_in_scope);
20854 break;
20855 default:
20856 /* Not a tag we recognize. Hopefully we aren't processing
20857 trash data, but since we must specifically ignore things
20858 we don't recognize, there is nothing else we should do at
20859 this point. */
20860 complaint (_("unsupported tag: '%s'"),
20861 dwarf_tag_name (die->tag));
20862 break;
20863 }
20864
20865 if (suppress_add)
20866 {
20867 sym->hash_next = objfile->template_symbols;
20868 objfile->template_symbols = sym;
20869 list_to_add = NULL;
20870 }
20871
20872 if (list_to_add != NULL)
20873 add_symbol_to_list (sym, list_to_add);
20874
20875 /* For the benefit of old versions of GCC, check for anonymous
20876 namespaces based on the demangled name. */
20877 if (!cu->processing_has_namespace_info
20878 && cu->language == language_cplus)
20879 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20880 }
20881 return (sym);
20882 }
20883
20884 /* Given an attr with a DW_FORM_dataN value in host byte order,
20885 zero-extend it as appropriate for the symbol's type. The DWARF
20886 standard (v4) is not entirely clear about the meaning of using
20887 DW_FORM_dataN for a constant with a signed type, where the type is
20888 wider than the data. The conclusion of a discussion on the DWARF
20889 list was that this is unspecified. We choose to always zero-extend
20890 because that is the interpretation long in use by GCC. */
20891
20892 static gdb_byte *
20893 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20894 struct dwarf2_cu *cu, LONGEST *value, int bits)
20895 {
20896 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20897 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20898 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20899 LONGEST l = DW_UNSND (attr);
20900
20901 if (bits < sizeof (*value) * 8)
20902 {
20903 l &= ((LONGEST) 1 << bits) - 1;
20904 *value = l;
20905 }
20906 else if (bits == sizeof (*value) * 8)
20907 *value = l;
20908 else
20909 {
20910 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20911 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20912 return bytes;
20913 }
20914
20915 return NULL;
20916 }
20917
20918 /* Read a constant value from an attribute. Either set *VALUE, or if
20919 the value does not fit in *VALUE, set *BYTES - either already
20920 allocated on the objfile obstack, or newly allocated on OBSTACK,
20921 or, set *BATON, if we translated the constant to a location
20922 expression. */
20923
20924 static void
20925 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20926 const char *name, struct obstack *obstack,
20927 struct dwarf2_cu *cu,
20928 LONGEST *value, const gdb_byte **bytes,
20929 struct dwarf2_locexpr_baton **baton)
20930 {
20931 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20932 struct comp_unit_head *cu_header = &cu->header;
20933 struct dwarf_block *blk;
20934 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20935 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20936
20937 *value = 0;
20938 *bytes = NULL;
20939 *baton = NULL;
20940
20941 switch (attr->form)
20942 {
20943 case DW_FORM_addr:
20944 case DW_FORM_addrx:
20945 case DW_FORM_GNU_addr_index:
20946 {
20947 gdb_byte *data;
20948
20949 if (TYPE_LENGTH (type) != cu_header->addr_size)
20950 dwarf2_const_value_length_mismatch_complaint (name,
20951 cu_header->addr_size,
20952 TYPE_LENGTH (type));
20953 /* Symbols of this form are reasonably rare, so we just
20954 piggyback on the existing location code rather than writing
20955 a new implementation of symbol_computed_ops. */
20956 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20957 (*baton)->per_cu = cu->per_cu;
20958 gdb_assert ((*baton)->per_cu);
20959
20960 (*baton)->size = 2 + cu_header->addr_size;
20961 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20962 (*baton)->data = data;
20963
20964 data[0] = DW_OP_addr;
20965 store_unsigned_integer (&data[1], cu_header->addr_size,
20966 byte_order, DW_ADDR (attr));
20967 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20968 }
20969 break;
20970 case DW_FORM_string:
20971 case DW_FORM_strp:
20972 case DW_FORM_strx:
20973 case DW_FORM_GNU_str_index:
20974 case DW_FORM_GNU_strp_alt:
20975 /* DW_STRING is already allocated on the objfile obstack, point
20976 directly to it. */
20977 *bytes = (const gdb_byte *) DW_STRING (attr);
20978 break;
20979 case DW_FORM_block1:
20980 case DW_FORM_block2:
20981 case DW_FORM_block4:
20982 case DW_FORM_block:
20983 case DW_FORM_exprloc:
20984 case DW_FORM_data16:
20985 blk = DW_BLOCK (attr);
20986 if (TYPE_LENGTH (type) != blk->size)
20987 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20988 TYPE_LENGTH (type));
20989 *bytes = blk->data;
20990 break;
20991
20992 /* The DW_AT_const_value attributes are supposed to carry the
20993 symbol's value "represented as it would be on the target
20994 architecture." By the time we get here, it's already been
20995 converted to host endianness, so we just need to sign- or
20996 zero-extend it as appropriate. */
20997 case DW_FORM_data1:
20998 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20999 break;
21000 case DW_FORM_data2:
21001 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21002 break;
21003 case DW_FORM_data4:
21004 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21005 break;
21006 case DW_FORM_data8:
21007 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21008 break;
21009
21010 case DW_FORM_sdata:
21011 case DW_FORM_implicit_const:
21012 *value = DW_SND (attr);
21013 break;
21014
21015 case DW_FORM_udata:
21016 *value = DW_UNSND (attr);
21017 break;
21018
21019 default:
21020 complaint (_("unsupported const value attribute form: '%s'"),
21021 dwarf_form_name (attr->form));
21022 *value = 0;
21023 break;
21024 }
21025 }
21026
21027
21028 /* Copy constant value from an attribute to a symbol. */
21029
21030 static void
21031 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21032 struct dwarf2_cu *cu)
21033 {
21034 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21035 LONGEST value;
21036 const gdb_byte *bytes;
21037 struct dwarf2_locexpr_baton *baton;
21038
21039 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21040 sym->print_name (),
21041 &objfile->objfile_obstack, cu,
21042 &value, &bytes, &baton);
21043
21044 if (baton != NULL)
21045 {
21046 SYMBOL_LOCATION_BATON (sym) = baton;
21047 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21048 }
21049 else if (bytes != NULL)
21050 {
21051 SYMBOL_VALUE_BYTES (sym) = bytes;
21052 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21053 }
21054 else
21055 {
21056 SYMBOL_VALUE (sym) = value;
21057 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21058 }
21059 }
21060
21061 /* Return the type of the die in question using its DW_AT_type attribute. */
21062
21063 static struct type *
21064 die_type (struct die_info *die, struct dwarf2_cu *cu)
21065 {
21066 struct attribute *type_attr;
21067
21068 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21069 if (!type_attr)
21070 {
21071 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21072 /* A missing DW_AT_type represents a void type. */
21073 return objfile_type (objfile)->builtin_void;
21074 }
21075
21076 return lookup_die_type (die, type_attr, cu);
21077 }
21078
21079 /* True iff CU's producer generates GNAT Ada auxiliary information
21080 that allows to find parallel types through that information instead
21081 of having to do expensive parallel lookups by type name. */
21082
21083 static int
21084 need_gnat_info (struct dwarf2_cu *cu)
21085 {
21086 /* Assume that the Ada compiler was GNAT, which always produces
21087 the auxiliary information. */
21088 return (cu->language == language_ada);
21089 }
21090
21091 /* Return the auxiliary type of the die in question using its
21092 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21093 attribute is not present. */
21094
21095 static struct type *
21096 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21097 {
21098 struct attribute *type_attr;
21099
21100 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21101 if (!type_attr)
21102 return NULL;
21103
21104 return lookup_die_type (die, type_attr, cu);
21105 }
21106
21107 /* If DIE has a descriptive_type attribute, then set the TYPE's
21108 descriptive type accordingly. */
21109
21110 static void
21111 set_descriptive_type (struct type *type, struct die_info *die,
21112 struct dwarf2_cu *cu)
21113 {
21114 struct type *descriptive_type = die_descriptive_type (die, cu);
21115
21116 if (descriptive_type)
21117 {
21118 ALLOCATE_GNAT_AUX_TYPE (type);
21119 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21120 }
21121 }
21122
21123 /* Return the containing type of the die in question using its
21124 DW_AT_containing_type attribute. */
21125
21126 static struct type *
21127 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21128 {
21129 struct attribute *type_attr;
21130 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21131
21132 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21133 if (!type_attr)
21134 error (_("Dwarf Error: Problem turning containing type into gdb type "
21135 "[in module %s]"), objfile_name (objfile));
21136
21137 return lookup_die_type (die, type_attr, cu);
21138 }
21139
21140 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21141
21142 static struct type *
21143 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21144 {
21145 struct dwarf2_per_objfile *dwarf2_per_objfile
21146 = cu->per_cu->dwarf2_per_objfile;
21147 struct objfile *objfile = dwarf2_per_objfile->objfile;
21148 char *saved;
21149
21150 std::string message
21151 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21152 objfile_name (objfile),
21153 sect_offset_str (cu->header.sect_off),
21154 sect_offset_str (die->sect_off));
21155 saved = obstack_strdup (&objfile->objfile_obstack, message);
21156
21157 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21158 }
21159
21160 /* Look up the type of DIE in CU using its type attribute ATTR.
21161 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21162 DW_AT_containing_type.
21163 If there is no type substitute an error marker. */
21164
21165 static struct type *
21166 lookup_die_type (struct die_info *die, const struct attribute *attr,
21167 struct dwarf2_cu *cu)
21168 {
21169 struct dwarf2_per_objfile *dwarf2_per_objfile
21170 = cu->per_cu->dwarf2_per_objfile;
21171 struct objfile *objfile = dwarf2_per_objfile->objfile;
21172 struct type *this_type;
21173
21174 gdb_assert (attr->name == DW_AT_type
21175 || attr->name == DW_AT_GNAT_descriptive_type
21176 || attr->name == DW_AT_containing_type);
21177
21178 /* First see if we have it cached. */
21179
21180 if (attr->form == DW_FORM_GNU_ref_alt)
21181 {
21182 struct dwarf2_per_cu_data *per_cu;
21183 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21184
21185 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21186 dwarf2_per_objfile);
21187 this_type = get_die_type_at_offset (sect_off, per_cu);
21188 }
21189 else if (attr->form_is_ref ())
21190 {
21191 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21192
21193 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21194 }
21195 else if (attr->form == DW_FORM_ref_sig8)
21196 {
21197 ULONGEST signature = DW_SIGNATURE (attr);
21198
21199 return get_signatured_type (die, signature, cu);
21200 }
21201 else
21202 {
21203 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21204 " at %s [in module %s]"),
21205 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21206 objfile_name (objfile));
21207 return build_error_marker_type (cu, die);
21208 }
21209
21210 /* If not cached we need to read it in. */
21211
21212 if (this_type == NULL)
21213 {
21214 struct die_info *type_die = NULL;
21215 struct dwarf2_cu *type_cu = cu;
21216
21217 if (attr->form_is_ref ())
21218 type_die = follow_die_ref (die, attr, &type_cu);
21219 if (type_die == NULL)
21220 return build_error_marker_type (cu, die);
21221 /* If we find the type now, it's probably because the type came
21222 from an inter-CU reference and the type's CU got expanded before
21223 ours. */
21224 this_type = read_type_die (type_die, type_cu);
21225 }
21226
21227 /* If we still don't have a type use an error marker. */
21228
21229 if (this_type == NULL)
21230 return build_error_marker_type (cu, die);
21231
21232 return this_type;
21233 }
21234
21235 /* Return the type in DIE, CU.
21236 Returns NULL for invalid types.
21237
21238 This first does a lookup in die_type_hash,
21239 and only reads the die in if necessary.
21240
21241 NOTE: This can be called when reading in partial or full symbols. */
21242
21243 static struct type *
21244 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21245 {
21246 struct type *this_type;
21247
21248 this_type = get_die_type (die, cu);
21249 if (this_type)
21250 return this_type;
21251
21252 return read_type_die_1 (die, cu);
21253 }
21254
21255 /* Read the type in DIE, CU.
21256 Returns NULL for invalid types. */
21257
21258 static struct type *
21259 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21260 {
21261 struct type *this_type = NULL;
21262
21263 switch (die->tag)
21264 {
21265 case DW_TAG_class_type:
21266 case DW_TAG_interface_type:
21267 case DW_TAG_structure_type:
21268 case DW_TAG_union_type:
21269 this_type = read_structure_type (die, cu);
21270 break;
21271 case DW_TAG_enumeration_type:
21272 this_type = read_enumeration_type (die, cu);
21273 break;
21274 case DW_TAG_subprogram:
21275 case DW_TAG_subroutine_type:
21276 case DW_TAG_inlined_subroutine:
21277 this_type = read_subroutine_type (die, cu);
21278 break;
21279 case DW_TAG_array_type:
21280 this_type = read_array_type (die, cu);
21281 break;
21282 case DW_TAG_set_type:
21283 this_type = read_set_type (die, cu);
21284 break;
21285 case DW_TAG_pointer_type:
21286 this_type = read_tag_pointer_type (die, cu);
21287 break;
21288 case DW_TAG_ptr_to_member_type:
21289 this_type = read_tag_ptr_to_member_type (die, cu);
21290 break;
21291 case DW_TAG_reference_type:
21292 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21293 break;
21294 case DW_TAG_rvalue_reference_type:
21295 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21296 break;
21297 case DW_TAG_const_type:
21298 this_type = read_tag_const_type (die, cu);
21299 break;
21300 case DW_TAG_volatile_type:
21301 this_type = read_tag_volatile_type (die, cu);
21302 break;
21303 case DW_TAG_restrict_type:
21304 this_type = read_tag_restrict_type (die, cu);
21305 break;
21306 case DW_TAG_string_type:
21307 this_type = read_tag_string_type (die, cu);
21308 break;
21309 case DW_TAG_typedef:
21310 this_type = read_typedef (die, cu);
21311 break;
21312 case DW_TAG_subrange_type:
21313 this_type = read_subrange_type (die, cu);
21314 break;
21315 case DW_TAG_base_type:
21316 this_type = read_base_type (die, cu);
21317 break;
21318 case DW_TAG_unspecified_type:
21319 this_type = read_unspecified_type (die, cu);
21320 break;
21321 case DW_TAG_namespace:
21322 this_type = read_namespace_type (die, cu);
21323 break;
21324 case DW_TAG_module:
21325 this_type = read_module_type (die, cu);
21326 break;
21327 case DW_TAG_atomic_type:
21328 this_type = read_tag_atomic_type (die, cu);
21329 break;
21330 default:
21331 complaint (_("unexpected tag in read_type_die: '%s'"),
21332 dwarf_tag_name (die->tag));
21333 break;
21334 }
21335
21336 return this_type;
21337 }
21338
21339 /* See if we can figure out if the class lives in a namespace. We do
21340 this by looking for a member function; its demangled name will
21341 contain namespace info, if there is any.
21342 Return the computed name or NULL.
21343 Space for the result is allocated on the objfile's obstack.
21344 This is the full-die version of guess_partial_die_structure_name.
21345 In this case we know DIE has no useful parent. */
21346
21347 static const char *
21348 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21349 {
21350 struct die_info *spec_die;
21351 struct dwarf2_cu *spec_cu;
21352 struct die_info *child;
21353 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21354
21355 spec_cu = cu;
21356 spec_die = die_specification (die, &spec_cu);
21357 if (spec_die != NULL)
21358 {
21359 die = spec_die;
21360 cu = spec_cu;
21361 }
21362
21363 for (child = die->child;
21364 child != NULL;
21365 child = child->sibling)
21366 {
21367 if (child->tag == DW_TAG_subprogram)
21368 {
21369 const char *linkage_name = dw2_linkage_name (child, cu);
21370
21371 if (linkage_name != NULL)
21372 {
21373 gdb::unique_xmalloc_ptr<char> actual_name
21374 (language_class_name_from_physname (cu->language_defn,
21375 linkage_name));
21376 const char *name = NULL;
21377
21378 if (actual_name != NULL)
21379 {
21380 const char *die_name = dwarf2_name (die, cu);
21381
21382 if (die_name != NULL
21383 && strcmp (die_name, actual_name.get ()) != 0)
21384 {
21385 /* Strip off the class name from the full name.
21386 We want the prefix. */
21387 int die_name_len = strlen (die_name);
21388 int actual_name_len = strlen (actual_name.get ());
21389 const char *ptr = actual_name.get ();
21390
21391 /* Test for '::' as a sanity check. */
21392 if (actual_name_len > die_name_len + 2
21393 && ptr[actual_name_len - die_name_len - 1] == ':')
21394 name = obstack_strndup (
21395 &objfile->per_bfd->storage_obstack,
21396 ptr, actual_name_len - die_name_len - 2);
21397 }
21398 }
21399 return name;
21400 }
21401 }
21402 }
21403
21404 return NULL;
21405 }
21406
21407 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21408 prefix part in such case. See
21409 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21410
21411 static const char *
21412 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21413 {
21414 struct attribute *attr;
21415 const char *base;
21416
21417 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21418 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21419 return NULL;
21420
21421 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21422 return NULL;
21423
21424 attr = dw2_linkage_name_attr (die, cu);
21425 if (attr == NULL || DW_STRING (attr) == NULL)
21426 return NULL;
21427
21428 /* dwarf2_name had to be already called. */
21429 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21430
21431 /* Strip the base name, keep any leading namespaces/classes. */
21432 base = strrchr (DW_STRING (attr), ':');
21433 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21434 return "";
21435
21436 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21437 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21438 DW_STRING (attr),
21439 &base[-1] - DW_STRING (attr));
21440 }
21441
21442 /* Return the name of the namespace/class that DIE is defined within,
21443 or "" if we can't tell. The caller should not xfree the result.
21444
21445 For example, if we're within the method foo() in the following
21446 code:
21447
21448 namespace N {
21449 class C {
21450 void foo () {
21451 }
21452 };
21453 }
21454
21455 then determine_prefix on foo's die will return "N::C". */
21456
21457 static const char *
21458 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21459 {
21460 struct dwarf2_per_objfile *dwarf2_per_objfile
21461 = cu->per_cu->dwarf2_per_objfile;
21462 struct die_info *parent, *spec_die;
21463 struct dwarf2_cu *spec_cu;
21464 struct type *parent_type;
21465 const char *retval;
21466
21467 if (cu->language != language_cplus
21468 && cu->language != language_fortran && cu->language != language_d
21469 && cu->language != language_rust)
21470 return "";
21471
21472 retval = anonymous_struct_prefix (die, cu);
21473 if (retval)
21474 return retval;
21475
21476 /* We have to be careful in the presence of DW_AT_specification.
21477 For example, with GCC 3.4, given the code
21478
21479 namespace N {
21480 void foo() {
21481 // Definition of N::foo.
21482 }
21483 }
21484
21485 then we'll have a tree of DIEs like this:
21486
21487 1: DW_TAG_compile_unit
21488 2: DW_TAG_namespace // N
21489 3: DW_TAG_subprogram // declaration of N::foo
21490 4: DW_TAG_subprogram // definition of N::foo
21491 DW_AT_specification // refers to die #3
21492
21493 Thus, when processing die #4, we have to pretend that we're in
21494 the context of its DW_AT_specification, namely the contex of die
21495 #3. */
21496 spec_cu = cu;
21497 spec_die = die_specification (die, &spec_cu);
21498 if (spec_die == NULL)
21499 parent = die->parent;
21500 else
21501 {
21502 parent = spec_die->parent;
21503 cu = spec_cu;
21504 }
21505
21506 if (parent == NULL)
21507 return "";
21508 else if (parent->building_fullname)
21509 {
21510 const char *name;
21511 const char *parent_name;
21512
21513 /* It has been seen on RealView 2.2 built binaries,
21514 DW_TAG_template_type_param types actually _defined_ as
21515 children of the parent class:
21516
21517 enum E {};
21518 template class <class Enum> Class{};
21519 Class<enum E> class_e;
21520
21521 1: DW_TAG_class_type (Class)
21522 2: DW_TAG_enumeration_type (E)
21523 3: DW_TAG_enumerator (enum1:0)
21524 3: DW_TAG_enumerator (enum2:1)
21525 ...
21526 2: DW_TAG_template_type_param
21527 DW_AT_type DW_FORM_ref_udata (E)
21528
21529 Besides being broken debug info, it can put GDB into an
21530 infinite loop. Consider:
21531
21532 When we're building the full name for Class<E>, we'll start
21533 at Class, and go look over its template type parameters,
21534 finding E. We'll then try to build the full name of E, and
21535 reach here. We're now trying to build the full name of E,
21536 and look over the parent DIE for containing scope. In the
21537 broken case, if we followed the parent DIE of E, we'd again
21538 find Class, and once again go look at its template type
21539 arguments, etc., etc. Simply don't consider such parent die
21540 as source-level parent of this die (it can't be, the language
21541 doesn't allow it), and break the loop here. */
21542 name = dwarf2_name (die, cu);
21543 parent_name = dwarf2_name (parent, cu);
21544 complaint (_("template param type '%s' defined within parent '%s'"),
21545 name ? name : "<unknown>",
21546 parent_name ? parent_name : "<unknown>");
21547 return "";
21548 }
21549 else
21550 switch (parent->tag)
21551 {
21552 case DW_TAG_namespace:
21553 parent_type = read_type_die (parent, cu);
21554 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21555 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21556 Work around this problem here. */
21557 if (cu->language == language_cplus
21558 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21559 return "";
21560 /* We give a name to even anonymous namespaces. */
21561 return TYPE_NAME (parent_type);
21562 case DW_TAG_class_type:
21563 case DW_TAG_interface_type:
21564 case DW_TAG_structure_type:
21565 case DW_TAG_union_type:
21566 case DW_TAG_module:
21567 parent_type = read_type_die (parent, cu);
21568 if (TYPE_NAME (parent_type) != NULL)
21569 return TYPE_NAME (parent_type);
21570 else
21571 /* An anonymous structure is only allowed non-static data
21572 members; no typedefs, no member functions, et cetera.
21573 So it does not need a prefix. */
21574 return "";
21575 case DW_TAG_compile_unit:
21576 case DW_TAG_partial_unit:
21577 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21578 if (cu->language == language_cplus
21579 && !dwarf2_per_objfile->types.empty ()
21580 && die->child != NULL
21581 && (die->tag == DW_TAG_class_type
21582 || die->tag == DW_TAG_structure_type
21583 || die->tag == DW_TAG_union_type))
21584 {
21585 const char *name = guess_full_die_structure_name (die, cu);
21586 if (name != NULL)
21587 return name;
21588 }
21589 return "";
21590 case DW_TAG_subprogram:
21591 /* Nested subroutines in Fortran get a prefix with the name
21592 of the parent's subroutine. */
21593 if (cu->language == language_fortran)
21594 {
21595 if ((die->tag == DW_TAG_subprogram)
21596 && (dwarf2_name (parent, cu) != NULL))
21597 return dwarf2_name (parent, cu);
21598 }
21599 return determine_prefix (parent, cu);
21600 case DW_TAG_enumeration_type:
21601 parent_type = read_type_die (parent, cu);
21602 if (TYPE_DECLARED_CLASS (parent_type))
21603 {
21604 if (TYPE_NAME (parent_type) != NULL)
21605 return TYPE_NAME (parent_type);
21606 return "";
21607 }
21608 /* Fall through. */
21609 default:
21610 return determine_prefix (parent, cu);
21611 }
21612 }
21613
21614 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21615 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21616 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21617 an obconcat, otherwise allocate storage for the result. The CU argument is
21618 used to determine the language and hence, the appropriate separator. */
21619
21620 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21621
21622 static char *
21623 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21624 int physname, struct dwarf2_cu *cu)
21625 {
21626 const char *lead = "";
21627 const char *sep;
21628
21629 if (suffix == NULL || suffix[0] == '\0'
21630 || prefix == NULL || prefix[0] == '\0')
21631 sep = "";
21632 else if (cu->language == language_d)
21633 {
21634 /* For D, the 'main' function could be defined in any module, but it
21635 should never be prefixed. */
21636 if (strcmp (suffix, "D main") == 0)
21637 {
21638 prefix = "";
21639 sep = "";
21640 }
21641 else
21642 sep = ".";
21643 }
21644 else if (cu->language == language_fortran && physname)
21645 {
21646 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21647 DW_AT_MIPS_linkage_name is preferred and used instead. */
21648
21649 lead = "__";
21650 sep = "_MOD_";
21651 }
21652 else
21653 sep = "::";
21654
21655 if (prefix == NULL)
21656 prefix = "";
21657 if (suffix == NULL)
21658 suffix = "";
21659
21660 if (obs == NULL)
21661 {
21662 char *retval
21663 = ((char *)
21664 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21665
21666 strcpy (retval, lead);
21667 strcat (retval, prefix);
21668 strcat (retval, sep);
21669 strcat (retval, suffix);
21670 return retval;
21671 }
21672 else
21673 {
21674 /* We have an obstack. */
21675 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21676 }
21677 }
21678
21679 /* Return sibling of die, NULL if no sibling. */
21680
21681 static struct die_info *
21682 sibling_die (struct die_info *die)
21683 {
21684 return die->sibling;
21685 }
21686
21687 /* Get name of a die, return NULL if not found. */
21688
21689 static const char *
21690 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21691 struct obstack *obstack)
21692 {
21693 if (name && cu->language == language_cplus)
21694 {
21695 std::string canon_name = cp_canonicalize_string (name);
21696
21697 if (!canon_name.empty ())
21698 {
21699 if (canon_name != name)
21700 name = obstack_strdup (obstack, canon_name);
21701 }
21702 }
21703
21704 return name;
21705 }
21706
21707 /* Get name of a die, return NULL if not found.
21708 Anonymous namespaces are converted to their magic string. */
21709
21710 static const char *
21711 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21712 {
21713 struct attribute *attr;
21714 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21715
21716 attr = dwarf2_attr (die, DW_AT_name, cu);
21717 if ((!attr || !DW_STRING (attr))
21718 && die->tag != DW_TAG_namespace
21719 && die->tag != DW_TAG_class_type
21720 && die->tag != DW_TAG_interface_type
21721 && die->tag != DW_TAG_structure_type
21722 && die->tag != DW_TAG_union_type)
21723 return NULL;
21724
21725 switch (die->tag)
21726 {
21727 case DW_TAG_compile_unit:
21728 case DW_TAG_partial_unit:
21729 /* Compilation units have a DW_AT_name that is a filename, not
21730 a source language identifier. */
21731 case DW_TAG_enumeration_type:
21732 case DW_TAG_enumerator:
21733 /* These tags always have simple identifiers already; no need
21734 to canonicalize them. */
21735 return DW_STRING (attr);
21736
21737 case DW_TAG_namespace:
21738 if (attr != NULL && DW_STRING (attr) != NULL)
21739 return DW_STRING (attr);
21740 return CP_ANONYMOUS_NAMESPACE_STR;
21741
21742 case DW_TAG_class_type:
21743 case DW_TAG_interface_type:
21744 case DW_TAG_structure_type:
21745 case DW_TAG_union_type:
21746 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21747 structures or unions. These were of the form "._%d" in GCC 4.1,
21748 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21749 and GCC 4.4. We work around this problem by ignoring these. */
21750 if (attr && DW_STRING (attr)
21751 && (startswith (DW_STRING (attr), "._")
21752 || startswith (DW_STRING (attr), "<anonymous")))
21753 return NULL;
21754
21755 /* GCC might emit a nameless typedef that has a linkage name. See
21756 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21757 if (!attr || DW_STRING (attr) == NULL)
21758 {
21759 attr = dw2_linkage_name_attr (die, cu);
21760 if (attr == NULL || DW_STRING (attr) == NULL)
21761 return NULL;
21762
21763 /* Avoid demangling DW_STRING (attr) the second time on a second
21764 call for the same DIE. */
21765 if (!DW_STRING_IS_CANONICAL (attr))
21766 {
21767 gdb::unique_xmalloc_ptr<char> demangled
21768 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21769
21770 const char *base;
21771
21772 /* FIXME: we already did this for the partial symbol... */
21773 DW_STRING (attr)
21774 = obstack_strdup (&objfile->per_bfd->storage_obstack,
21775 demangled.get ());
21776 DW_STRING_IS_CANONICAL (attr) = 1;
21777
21778 /* Strip any leading namespaces/classes, keep only the base name.
21779 DW_AT_name for named DIEs does not contain the prefixes. */
21780 base = strrchr (DW_STRING (attr), ':');
21781 if (base && base > DW_STRING (attr) && base[-1] == ':')
21782 return &base[1];
21783 else
21784 return DW_STRING (attr);
21785 }
21786 }
21787 break;
21788
21789 default:
21790 break;
21791 }
21792
21793 if (!DW_STRING_IS_CANONICAL (attr))
21794 {
21795 DW_STRING (attr)
21796 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21797 &objfile->per_bfd->storage_obstack);
21798 DW_STRING_IS_CANONICAL (attr) = 1;
21799 }
21800 return DW_STRING (attr);
21801 }
21802
21803 /* Return the die that this die in an extension of, or NULL if there
21804 is none. *EXT_CU is the CU containing DIE on input, and the CU
21805 containing the return value on output. */
21806
21807 static struct die_info *
21808 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21809 {
21810 struct attribute *attr;
21811
21812 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21813 if (attr == NULL)
21814 return NULL;
21815
21816 return follow_die_ref (die, attr, ext_cu);
21817 }
21818
21819 /* A convenience function that returns an "unknown" DWARF name,
21820 including the value of V. STR is the name of the entity being
21821 printed, e.g., "TAG". */
21822
21823 static const char *
21824 dwarf_unknown (const char *str, unsigned v)
21825 {
21826 char *cell = get_print_cell ();
21827 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21828 return cell;
21829 }
21830
21831 /* Convert a DIE tag into its string name. */
21832
21833 static const char *
21834 dwarf_tag_name (unsigned tag)
21835 {
21836 const char *name = get_DW_TAG_name (tag);
21837
21838 if (name == NULL)
21839 return dwarf_unknown ("TAG", tag);
21840
21841 return name;
21842 }
21843
21844 /* Convert a DWARF attribute code into its string name. */
21845
21846 static const char *
21847 dwarf_attr_name (unsigned attr)
21848 {
21849 const char *name;
21850
21851 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21852 if (attr == DW_AT_MIPS_fde)
21853 return "DW_AT_MIPS_fde";
21854 #else
21855 if (attr == DW_AT_HP_block_index)
21856 return "DW_AT_HP_block_index";
21857 #endif
21858
21859 name = get_DW_AT_name (attr);
21860
21861 if (name == NULL)
21862 return dwarf_unknown ("AT", attr);
21863
21864 return name;
21865 }
21866
21867 /* Convert a DWARF value form code into its string name. */
21868
21869 static const char *
21870 dwarf_form_name (unsigned form)
21871 {
21872 const char *name = get_DW_FORM_name (form);
21873
21874 if (name == NULL)
21875 return dwarf_unknown ("FORM", form);
21876
21877 return name;
21878 }
21879
21880 static const char *
21881 dwarf_bool_name (unsigned mybool)
21882 {
21883 if (mybool)
21884 return "TRUE";
21885 else
21886 return "FALSE";
21887 }
21888
21889 /* Convert a DWARF type code into its string name. */
21890
21891 static const char *
21892 dwarf_type_encoding_name (unsigned enc)
21893 {
21894 const char *name = get_DW_ATE_name (enc);
21895
21896 if (name == NULL)
21897 return dwarf_unknown ("ATE", enc);
21898
21899 return name;
21900 }
21901
21902 static void
21903 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21904 {
21905 unsigned int i;
21906
21907 print_spaces (indent, f);
21908 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21909 dwarf_tag_name (die->tag), die->abbrev,
21910 sect_offset_str (die->sect_off));
21911
21912 if (die->parent != NULL)
21913 {
21914 print_spaces (indent, f);
21915 fprintf_unfiltered (f, " parent at offset: %s\n",
21916 sect_offset_str (die->parent->sect_off));
21917 }
21918
21919 print_spaces (indent, f);
21920 fprintf_unfiltered (f, " has children: %s\n",
21921 dwarf_bool_name (die->child != NULL));
21922
21923 print_spaces (indent, f);
21924 fprintf_unfiltered (f, " attributes:\n");
21925
21926 for (i = 0; i < die->num_attrs; ++i)
21927 {
21928 print_spaces (indent, f);
21929 fprintf_unfiltered (f, " %s (%s) ",
21930 dwarf_attr_name (die->attrs[i].name),
21931 dwarf_form_name (die->attrs[i].form));
21932
21933 switch (die->attrs[i].form)
21934 {
21935 case DW_FORM_addr:
21936 case DW_FORM_addrx:
21937 case DW_FORM_GNU_addr_index:
21938 fprintf_unfiltered (f, "address: ");
21939 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21940 break;
21941 case DW_FORM_block2:
21942 case DW_FORM_block4:
21943 case DW_FORM_block:
21944 case DW_FORM_block1:
21945 fprintf_unfiltered (f, "block: size %s",
21946 pulongest (DW_BLOCK (&die->attrs[i])->size));
21947 break;
21948 case DW_FORM_exprloc:
21949 fprintf_unfiltered (f, "expression: size %s",
21950 pulongest (DW_BLOCK (&die->attrs[i])->size));
21951 break;
21952 case DW_FORM_data16:
21953 fprintf_unfiltered (f, "constant of 16 bytes");
21954 break;
21955 case DW_FORM_ref_addr:
21956 fprintf_unfiltered (f, "ref address: ");
21957 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21958 break;
21959 case DW_FORM_GNU_ref_alt:
21960 fprintf_unfiltered (f, "alt ref address: ");
21961 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21962 break;
21963 case DW_FORM_ref1:
21964 case DW_FORM_ref2:
21965 case DW_FORM_ref4:
21966 case DW_FORM_ref8:
21967 case DW_FORM_ref_udata:
21968 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21969 (long) (DW_UNSND (&die->attrs[i])));
21970 break;
21971 case DW_FORM_data1:
21972 case DW_FORM_data2:
21973 case DW_FORM_data4:
21974 case DW_FORM_data8:
21975 case DW_FORM_udata:
21976 case DW_FORM_sdata:
21977 fprintf_unfiltered (f, "constant: %s",
21978 pulongest (DW_UNSND (&die->attrs[i])));
21979 break;
21980 case DW_FORM_sec_offset:
21981 fprintf_unfiltered (f, "section offset: %s",
21982 pulongest (DW_UNSND (&die->attrs[i])));
21983 break;
21984 case DW_FORM_ref_sig8:
21985 fprintf_unfiltered (f, "signature: %s",
21986 hex_string (DW_SIGNATURE (&die->attrs[i])));
21987 break;
21988 case DW_FORM_string:
21989 case DW_FORM_strp:
21990 case DW_FORM_line_strp:
21991 case DW_FORM_strx:
21992 case DW_FORM_GNU_str_index:
21993 case DW_FORM_GNU_strp_alt:
21994 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21995 DW_STRING (&die->attrs[i])
21996 ? DW_STRING (&die->attrs[i]) : "",
21997 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21998 break;
21999 case DW_FORM_flag:
22000 if (DW_UNSND (&die->attrs[i]))
22001 fprintf_unfiltered (f, "flag: TRUE");
22002 else
22003 fprintf_unfiltered (f, "flag: FALSE");
22004 break;
22005 case DW_FORM_flag_present:
22006 fprintf_unfiltered (f, "flag: TRUE");
22007 break;
22008 case DW_FORM_indirect:
22009 /* The reader will have reduced the indirect form to
22010 the "base form" so this form should not occur. */
22011 fprintf_unfiltered (f,
22012 "unexpected attribute form: DW_FORM_indirect");
22013 break;
22014 case DW_FORM_implicit_const:
22015 fprintf_unfiltered (f, "constant: %s",
22016 plongest (DW_SND (&die->attrs[i])));
22017 break;
22018 default:
22019 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22020 die->attrs[i].form);
22021 break;
22022 }
22023 fprintf_unfiltered (f, "\n");
22024 }
22025 }
22026
22027 static void
22028 dump_die_for_error (struct die_info *die)
22029 {
22030 dump_die_shallow (gdb_stderr, 0, die);
22031 }
22032
22033 static void
22034 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22035 {
22036 int indent = level * 4;
22037
22038 gdb_assert (die != NULL);
22039
22040 if (level >= max_level)
22041 return;
22042
22043 dump_die_shallow (f, indent, die);
22044
22045 if (die->child != NULL)
22046 {
22047 print_spaces (indent, f);
22048 fprintf_unfiltered (f, " Children:");
22049 if (level + 1 < max_level)
22050 {
22051 fprintf_unfiltered (f, "\n");
22052 dump_die_1 (f, level + 1, max_level, die->child);
22053 }
22054 else
22055 {
22056 fprintf_unfiltered (f,
22057 " [not printed, max nesting level reached]\n");
22058 }
22059 }
22060
22061 if (die->sibling != NULL && level > 0)
22062 {
22063 dump_die_1 (f, level, max_level, die->sibling);
22064 }
22065 }
22066
22067 /* This is called from the pdie macro in gdbinit.in.
22068 It's not static so gcc will keep a copy callable from gdb. */
22069
22070 void
22071 dump_die (struct die_info *die, int max_level)
22072 {
22073 dump_die_1 (gdb_stdlog, 0, max_level, die);
22074 }
22075
22076 static void
22077 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22078 {
22079 void **slot;
22080
22081 slot = htab_find_slot_with_hash (cu->die_hash, die,
22082 to_underlying (die->sect_off),
22083 INSERT);
22084
22085 *slot = die;
22086 }
22087
22088 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22089 required kind. */
22090
22091 static sect_offset
22092 dwarf2_get_ref_die_offset (const struct attribute *attr)
22093 {
22094 if (attr->form_is_ref ())
22095 return (sect_offset) DW_UNSND (attr);
22096
22097 complaint (_("unsupported die ref attribute form: '%s'"),
22098 dwarf_form_name (attr->form));
22099 return {};
22100 }
22101
22102 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22103 * the value held by the attribute is not constant. */
22104
22105 static LONGEST
22106 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22107 {
22108 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22109 return DW_SND (attr);
22110 else if (attr->form == DW_FORM_udata
22111 || attr->form == DW_FORM_data1
22112 || attr->form == DW_FORM_data2
22113 || attr->form == DW_FORM_data4
22114 || attr->form == DW_FORM_data8)
22115 return DW_UNSND (attr);
22116 else
22117 {
22118 /* For DW_FORM_data16 see attribute::form_is_constant. */
22119 complaint (_("Attribute value is not a constant (%s)"),
22120 dwarf_form_name (attr->form));
22121 return default_value;
22122 }
22123 }
22124
22125 /* Follow reference or signature attribute ATTR of SRC_DIE.
22126 On entry *REF_CU is the CU of SRC_DIE.
22127 On exit *REF_CU is the CU of the result. */
22128
22129 static struct die_info *
22130 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22131 struct dwarf2_cu **ref_cu)
22132 {
22133 struct die_info *die;
22134
22135 if (attr->form_is_ref ())
22136 die = follow_die_ref (src_die, attr, ref_cu);
22137 else if (attr->form == DW_FORM_ref_sig8)
22138 die = follow_die_sig (src_die, attr, ref_cu);
22139 else
22140 {
22141 dump_die_for_error (src_die);
22142 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22143 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22144 }
22145
22146 return die;
22147 }
22148
22149 /* Follow reference OFFSET.
22150 On entry *REF_CU is the CU of the source die referencing OFFSET.
22151 On exit *REF_CU is the CU of the result.
22152 Returns NULL if OFFSET is invalid. */
22153
22154 static struct die_info *
22155 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22156 struct dwarf2_cu **ref_cu)
22157 {
22158 struct die_info temp_die;
22159 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22160 struct dwarf2_per_objfile *dwarf2_per_objfile
22161 = cu->per_cu->dwarf2_per_objfile;
22162
22163 gdb_assert (cu->per_cu != NULL);
22164
22165 target_cu = cu;
22166
22167 if (cu->per_cu->is_debug_types)
22168 {
22169 /* .debug_types CUs cannot reference anything outside their CU.
22170 If they need to, they have to reference a signatured type via
22171 DW_FORM_ref_sig8. */
22172 if (!cu->header.offset_in_cu_p (sect_off))
22173 return NULL;
22174 }
22175 else if (offset_in_dwz != cu->per_cu->is_dwz
22176 || !cu->header.offset_in_cu_p (sect_off))
22177 {
22178 struct dwarf2_per_cu_data *per_cu;
22179
22180 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22181 dwarf2_per_objfile);
22182
22183 /* If necessary, add it to the queue and load its DIEs. */
22184 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22185 load_full_comp_unit (per_cu, false, cu->language);
22186
22187 target_cu = per_cu->cu;
22188 }
22189 else if (cu->dies == NULL)
22190 {
22191 /* We're loading full DIEs during partial symbol reading. */
22192 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22193 load_full_comp_unit (cu->per_cu, false, language_minimal);
22194 }
22195
22196 *ref_cu = target_cu;
22197 temp_die.sect_off = sect_off;
22198
22199 if (target_cu != cu)
22200 target_cu->ancestor = cu;
22201
22202 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22203 &temp_die,
22204 to_underlying (sect_off));
22205 }
22206
22207 /* Follow reference attribute ATTR of SRC_DIE.
22208 On entry *REF_CU is the CU of SRC_DIE.
22209 On exit *REF_CU is the CU of the result. */
22210
22211 static struct die_info *
22212 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22213 struct dwarf2_cu **ref_cu)
22214 {
22215 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22216 struct dwarf2_cu *cu = *ref_cu;
22217 struct die_info *die;
22218
22219 die = follow_die_offset (sect_off,
22220 (attr->form == DW_FORM_GNU_ref_alt
22221 || cu->per_cu->is_dwz),
22222 ref_cu);
22223 if (!die)
22224 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22225 "at %s [in module %s]"),
22226 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22227 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22228
22229 return die;
22230 }
22231
22232 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22233 Returned value is intended for DW_OP_call*. Returned
22234 dwarf2_locexpr_baton->data has lifetime of
22235 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22236
22237 struct dwarf2_locexpr_baton
22238 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22239 struct dwarf2_per_cu_data *per_cu,
22240 CORE_ADDR (*get_frame_pc) (void *baton),
22241 void *baton, bool resolve_abstract_p)
22242 {
22243 struct dwarf2_cu *cu;
22244 struct die_info *die;
22245 struct attribute *attr;
22246 struct dwarf2_locexpr_baton retval;
22247 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22248 struct objfile *objfile = dwarf2_per_objfile->objfile;
22249
22250 if (per_cu->cu == NULL)
22251 load_cu (per_cu, false);
22252 cu = per_cu->cu;
22253 if (cu == NULL)
22254 {
22255 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22256 Instead just throw an error, not much else we can do. */
22257 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22258 sect_offset_str (sect_off), objfile_name (objfile));
22259 }
22260
22261 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22262 if (!die)
22263 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22264 sect_offset_str (sect_off), objfile_name (objfile));
22265
22266 attr = dwarf2_attr (die, DW_AT_location, cu);
22267 if (!attr && resolve_abstract_p
22268 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22269 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22270 {
22271 CORE_ADDR pc = (*get_frame_pc) (baton);
22272 CORE_ADDR baseaddr = objfile->text_section_offset ();
22273 struct gdbarch *gdbarch = get_objfile_arch (objfile);
22274
22275 for (const auto &cand_off
22276 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22277 {
22278 struct dwarf2_cu *cand_cu = cu;
22279 struct die_info *cand
22280 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22281 if (!cand
22282 || !cand->parent
22283 || cand->parent->tag != DW_TAG_subprogram)
22284 continue;
22285
22286 CORE_ADDR pc_low, pc_high;
22287 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22288 if (pc_low == ((CORE_ADDR) -1))
22289 continue;
22290 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22291 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22292 if (!(pc_low <= pc && pc < pc_high))
22293 continue;
22294
22295 die = cand;
22296 attr = dwarf2_attr (die, DW_AT_location, cu);
22297 break;
22298 }
22299 }
22300
22301 if (!attr)
22302 {
22303 /* DWARF: "If there is no such attribute, then there is no effect.".
22304 DATA is ignored if SIZE is 0. */
22305
22306 retval.data = NULL;
22307 retval.size = 0;
22308 }
22309 else if (attr->form_is_section_offset ())
22310 {
22311 struct dwarf2_loclist_baton loclist_baton;
22312 CORE_ADDR pc = (*get_frame_pc) (baton);
22313 size_t size;
22314
22315 fill_in_loclist_baton (cu, &loclist_baton, attr);
22316
22317 retval.data = dwarf2_find_location_expression (&loclist_baton,
22318 &size, pc);
22319 retval.size = size;
22320 }
22321 else
22322 {
22323 if (!attr->form_is_block ())
22324 error (_("Dwarf Error: DIE at %s referenced in module %s "
22325 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22326 sect_offset_str (sect_off), objfile_name (objfile));
22327
22328 retval.data = DW_BLOCK (attr)->data;
22329 retval.size = DW_BLOCK (attr)->size;
22330 }
22331 retval.per_cu = cu->per_cu;
22332
22333 age_cached_comp_units (dwarf2_per_objfile);
22334
22335 return retval;
22336 }
22337
22338 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22339 offset. */
22340
22341 struct dwarf2_locexpr_baton
22342 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22343 struct dwarf2_per_cu_data *per_cu,
22344 CORE_ADDR (*get_frame_pc) (void *baton),
22345 void *baton)
22346 {
22347 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22348
22349 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22350 }
22351
22352 /* Write a constant of a given type as target-ordered bytes into
22353 OBSTACK. */
22354
22355 static const gdb_byte *
22356 write_constant_as_bytes (struct obstack *obstack,
22357 enum bfd_endian byte_order,
22358 struct type *type,
22359 ULONGEST value,
22360 LONGEST *len)
22361 {
22362 gdb_byte *result;
22363
22364 *len = TYPE_LENGTH (type);
22365 result = (gdb_byte *) obstack_alloc (obstack, *len);
22366 store_unsigned_integer (result, *len, byte_order, value);
22367
22368 return result;
22369 }
22370
22371 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22372 pointer to the constant bytes and set LEN to the length of the
22373 data. If memory is needed, allocate it on OBSTACK. If the DIE
22374 does not have a DW_AT_const_value, return NULL. */
22375
22376 const gdb_byte *
22377 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22378 struct dwarf2_per_cu_data *per_cu,
22379 struct obstack *obstack,
22380 LONGEST *len)
22381 {
22382 struct dwarf2_cu *cu;
22383 struct die_info *die;
22384 struct attribute *attr;
22385 const gdb_byte *result = NULL;
22386 struct type *type;
22387 LONGEST value;
22388 enum bfd_endian byte_order;
22389 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22390
22391 if (per_cu->cu == NULL)
22392 load_cu (per_cu, false);
22393 cu = per_cu->cu;
22394 if (cu == NULL)
22395 {
22396 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22397 Instead just throw an error, not much else we can do. */
22398 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22399 sect_offset_str (sect_off), objfile_name (objfile));
22400 }
22401
22402 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22403 if (!die)
22404 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22405 sect_offset_str (sect_off), objfile_name (objfile));
22406
22407 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22408 if (attr == NULL)
22409 return NULL;
22410
22411 byte_order = (bfd_big_endian (objfile->obfd)
22412 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22413
22414 switch (attr->form)
22415 {
22416 case DW_FORM_addr:
22417 case DW_FORM_addrx:
22418 case DW_FORM_GNU_addr_index:
22419 {
22420 gdb_byte *tem;
22421
22422 *len = cu->header.addr_size;
22423 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22424 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22425 result = tem;
22426 }
22427 break;
22428 case DW_FORM_string:
22429 case DW_FORM_strp:
22430 case DW_FORM_strx:
22431 case DW_FORM_GNU_str_index:
22432 case DW_FORM_GNU_strp_alt:
22433 /* DW_STRING is already allocated on the objfile obstack, point
22434 directly to it. */
22435 result = (const gdb_byte *) DW_STRING (attr);
22436 *len = strlen (DW_STRING (attr));
22437 break;
22438 case DW_FORM_block1:
22439 case DW_FORM_block2:
22440 case DW_FORM_block4:
22441 case DW_FORM_block:
22442 case DW_FORM_exprloc:
22443 case DW_FORM_data16:
22444 result = DW_BLOCK (attr)->data;
22445 *len = DW_BLOCK (attr)->size;
22446 break;
22447
22448 /* The DW_AT_const_value attributes are supposed to carry the
22449 symbol's value "represented as it would be on the target
22450 architecture." By the time we get here, it's already been
22451 converted to host endianness, so we just need to sign- or
22452 zero-extend it as appropriate. */
22453 case DW_FORM_data1:
22454 type = die_type (die, cu);
22455 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22456 if (result == NULL)
22457 result = write_constant_as_bytes (obstack, byte_order,
22458 type, value, len);
22459 break;
22460 case DW_FORM_data2:
22461 type = die_type (die, cu);
22462 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22463 if (result == NULL)
22464 result = write_constant_as_bytes (obstack, byte_order,
22465 type, value, len);
22466 break;
22467 case DW_FORM_data4:
22468 type = die_type (die, cu);
22469 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22470 if (result == NULL)
22471 result = write_constant_as_bytes (obstack, byte_order,
22472 type, value, len);
22473 break;
22474 case DW_FORM_data8:
22475 type = die_type (die, cu);
22476 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22477 if (result == NULL)
22478 result = write_constant_as_bytes (obstack, byte_order,
22479 type, value, len);
22480 break;
22481
22482 case DW_FORM_sdata:
22483 case DW_FORM_implicit_const:
22484 type = die_type (die, cu);
22485 result = write_constant_as_bytes (obstack, byte_order,
22486 type, DW_SND (attr), len);
22487 break;
22488
22489 case DW_FORM_udata:
22490 type = die_type (die, cu);
22491 result = write_constant_as_bytes (obstack, byte_order,
22492 type, DW_UNSND (attr), len);
22493 break;
22494
22495 default:
22496 complaint (_("unsupported const value attribute form: '%s'"),
22497 dwarf_form_name (attr->form));
22498 break;
22499 }
22500
22501 return result;
22502 }
22503
22504 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
22505 valid type for this die is found. */
22506
22507 struct type *
22508 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22509 struct dwarf2_per_cu_data *per_cu)
22510 {
22511 struct dwarf2_cu *cu;
22512 struct die_info *die;
22513
22514 if (per_cu->cu == NULL)
22515 load_cu (per_cu, false);
22516 cu = per_cu->cu;
22517 if (!cu)
22518 return NULL;
22519
22520 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22521 if (!die)
22522 return NULL;
22523
22524 return die_type (die, cu);
22525 }
22526
22527 /* Return the type of the DIE at DIE_OFFSET in the CU named by
22528 PER_CU. */
22529
22530 struct type *
22531 dwarf2_get_die_type (cu_offset die_offset,
22532 struct dwarf2_per_cu_data *per_cu)
22533 {
22534 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22535 return get_die_type_at_offset (die_offset_sect, per_cu);
22536 }
22537
22538 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22539 On entry *REF_CU is the CU of SRC_DIE.
22540 On exit *REF_CU is the CU of the result.
22541 Returns NULL if the referenced DIE isn't found. */
22542
22543 static struct die_info *
22544 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22545 struct dwarf2_cu **ref_cu)
22546 {
22547 struct die_info temp_die;
22548 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22549 struct die_info *die;
22550
22551 /* While it might be nice to assert sig_type->type == NULL here,
22552 we can get here for DW_AT_imported_declaration where we need
22553 the DIE not the type. */
22554
22555 /* If necessary, add it to the queue and load its DIEs. */
22556
22557 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22558 read_signatured_type (sig_type);
22559
22560 sig_cu = sig_type->per_cu.cu;
22561 gdb_assert (sig_cu != NULL);
22562 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22563 temp_die.sect_off = sig_type->type_offset_in_section;
22564 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22565 to_underlying (temp_die.sect_off));
22566 if (die)
22567 {
22568 struct dwarf2_per_objfile *dwarf2_per_objfile
22569 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22570
22571 /* For .gdb_index version 7 keep track of included TUs.
22572 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22573 if (dwarf2_per_objfile->index_table != NULL
22574 && dwarf2_per_objfile->index_table->version <= 7)
22575 {
22576 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22577 }
22578
22579 *ref_cu = sig_cu;
22580 if (sig_cu != cu)
22581 sig_cu->ancestor = cu;
22582
22583 return die;
22584 }
22585
22586 return NULL;
22587 }
22588
22589 /* Follow signatured type referenced by ATTR in SRC_DIE.
22590 On entry *REF_CU is the CU of SRC_DIE.
22591 On exit *REF_CU is the CU of the result.
22592 The result is the DIE of the type.
22593 If the referenced type cannot be found an error is thrown. */
22594
22595 static struct die_info *
22596 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22597 struct dwarf2_cu **ref_cu)
22598 {
22599 ULONGEST signature = DW_SIGNATURE (attr);
22600 struct signatured_type *sig_type;
22601 struct die_info *die;
22602
22603 gdb_assert (attr->form == DW_FORM_ref_sig8);
22604
22605 sig_type = lookup_signatured_type (*ref_cu, signature);
22606 /* sig_type will be NULL if the signatured type is missing from
22607 the debug info. */
22608 if (sig_type == NULL)
22609 {
22610 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22611 " from DIE at %s [in module %s]"),
22612 hex_string (signature), sect_offset_str (src_die->sect_off),
22613 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22614 }
22615
22616 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22617 if (die == NULL)
22618 {
22619 dump_die_for_error (src_die);
22620 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22621 " from DIE at %s [in module %s]"),
22622 hex_string (signature), sect_offset_str (src_die->sect_off),
22623 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22624 }
22625
22626 return die;
22627 }
22628
22629 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22630 reading in and processing the type unit if necessary. */
22631
22632 static struct type *
22633 get_signatured_type (struct die_info *die, ULONGEST signature,
22634 struct dwarf2_cu *cu)
22635 {
22636 struct dwarf2_per_objfile *dwarf2_per_objfile
22637 = cu->per_cu->dwarf2_per_objfile;
22638 struct signatured_type *sig_type;
22639 struct dwarf2_cu *type_cu;
22640 struct die_info *type_die;
22641 struct type *type;
22642
22643 sig_type = lookup_signatured_type (cu, signature);
22644 /* sig_type will be NULL if the signatured type is missing from
22645 the debug info. */
22646 if (sig_type == NULL)
22647 {
22648 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22649 " from DIE at %s [in module %s]"),
22650 hex_string (signature), sect_offset_str (die->sect_off),
22651 objfile_name (dwarf2_per_objfile->objfile));
22652 return build_error_marker_type (cu, die);
22653 }
22654
22655 /* If we already know the type we're done. */
22656 if (sig_type->type != NULL)
22657 return sig_type->type;
22658
22659 type_cu = cu;
22660 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22661 if (type_die != NULL)
22662 {
22663 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22664 is created. This is important, for example, because for c++ classes
22665 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22666 type = read_type_die (type_die, type_cu);
22667 if (type == NULL)
22668 {
22669 complaint (_("Dwarf Error: Cannot build signatured type %s"
22670 " referenced from DIE at %s [in module %s]"),
22671 hex_string (signature), sect_offset_str (die->sect_off),
22672 objfile_name (dwarf2_per_objfile->objfile));
22673 type = build_error_marker_type (cu, die);
22674 }
22675 }
22676 else
22677 {
22678 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22679 " from DIE at %s [in module %s]"),
22680 hex_string (signature), sect_offset_str (die->sect_off),
22681 objfile_name (dwarf2_per_objfile->objfile));
22682 type = build_error_marker_type (cu, die);
22683 }
22684 sig_type->type = type;
22685
22686 return type;
22687 }
22688
22689 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22690 reading in and processing the type unit if necessary. */
22691
22692 static struct type *
22693 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22694 struct dwarf2_cu *cu) /* ARI: editCase function */
22695 {
22696 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22697 if (attr->form_is_ref ())
22698 {
22699 struct dwarf2_cu *type_cu = cu;
22700 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22701
22702 return read_type_die (type_die, type_cu);
22703 }
22704 else if (attr->form == DW_FORM_ref_sig8)
22705 {
22706 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22707 }
22708 else
22709 {
22710 struct dwarf2_per_objfile *dwarf2_per_objfile
22711 = cu->per_cu->dwarf2_per_objfile;
22712
22713 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22714 " at %s [in module %s]"),
22715 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22716 objfile_name (dwarf2_per_objfile->objfile));
22717 return build_error_marker_type (cu, die);
22718 }
22719 }
22720
22721 /* Load the DIEs associated with type unit PER_CU into memory. */
22722
22723 static void
22724 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22725 {
22726 struct signatured_type *sig_type;
22727
22728 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22729 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
22730
22731 /* We have the per_cu, but we need the signatured_type.
22732 Fortunately this is an easy translation. */
22733 gdb_assert (per_cu->is_debug_types);
22734 sig_type = (struct signatured_type *) per_cu;
22735
22736 gdb_assert (per_cu->cu == NULL);
22737
22738 read_signatured_type (sig_type);
22739
22740 gdb_assert (per_cu->cu != NULL);
22741 }
22742
22743 /* Read in a signatured type and build its CU and DIEs.
22744 If the type is a stub for the real type in a DWO file,
22745 read in the real type from the DWO file as well. */
22746
22747 static void
22748 read_signatured_type (struct signatured_type *sig_type)
22749 {
22750 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22751
22752 gdb_assert (per_cu->is_debug_types);
22753 gdb_assert (per_cu->cu == NULL);
22754
22755 cutu_reader reader (per_cu, NULL, 0, 1, false);
22756
22757 if (!reader.dummy_p)
22758 {
22759 struct dwarf2_cu *cu = reader.cu;
22760 const gdb_byte *info_ptr = reader.info_ptr;
22761
22762 gdb_assert (cu->die_hash == NULL);
22763 cu->die_hash =
22764 htab_create_alloc_ex (cu->header.length / 12,
22765 die_hash,
22766 die_eq,
22767 NULL,
22768 &cu->comp_unit_obstack,
22769 hashtab_obstack_allocate,
22770 dummy_obstack_deallocate);
22771
22772 if (reader.comp_unit_die->has_children)
22773 reader.comp_unit_die->child
22774 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22775 reader.comp_unit_die);
22776 cu->dies = reader.comp_unit_die;
22777 /* comp_unit_die is not stored in die_hash, no need. */
22778
22779 /* We try not to read any attributes in this function, because
22780 not all CUs needed for references have been loaded yet, and
22781 symbol table processing isn't initialized. But we have to
22782 set the CU language, or we won't be able to build types
22783 correctly. Similarly, if we do not read the producer, we can
22784 not apply producer-specific interpretation. */
22785 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22786 }
22787
22788 sig_type->per_cu.tu_read = 1;
22789 }
22790
22791 /* Decode simple location descriptions.
22792 Given a pointer to a dwarf block that defines a location, compute
22793 the location and return the value.
22794
22795 NOTE drow/2003-11-18: This function is called in two situations
22796 now: for the address of static or global variables (partial symbols
22797 only) and for offsets into structures which are expected to be
22798 (more or less) constant. The partial symbol case should go away,
22799 and only the constant case should remain. That will let this
22800 function complain more accurately. A few special modes are allowed
22801 without complaint for global variables (for instance, global
22802 register values and thread-local values).
22803
22804 A location description containing no operations indicates that the
22805 object is optimized out. The return value is 0 for that case.
22806 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22807 callers will only want a very basic result and this can become a
22808 complaint.
22809
22810 Note that stack[0] is unused except as a default error return. */
22811
22812 static CORE_ADDR
22813 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22814 {
22815 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22816 size_t i;
22817 size_t size = blk->size;
22818 const gdb_byte *data = blk->data;
22819 CORE_ADDR stack[64];
22820 int stacki;
22821 unsigned int bytes_read, unsnd;
22822 gdb_byte op;
22823
22824 i = 0;
22825 stacki = 0;
22826 stack[stacki] = 0;
22827 stack[++stacki] = 0;
22828
22829 while (i < size)
22830 {
22831 op = data[i++];
22832 switch (op)
22833 {
22834 case DW_OP_lit0:
22835 case DW_OP_lit1:
22836 case DW_OP_lit2:
22837 case DW_OP_lit3:
22838 case DW_OP_lit4:
22839 case DW_OP_lit5:
22840 case DW_OP_lit6:
22841 case DW_OP_lit7:
22842 case DW_OP_lit8:
22843 case DW_OP_lit9:
22844 case DW_OP_lit10:
22845 case DW_OP_lit11:
22846 case DW_OP_lit12:
22847 case DW_OP_lit13:
22848 case DW_OP_lit14:
22849 case DW_OP_lit15:
22850 case DW_OP_lit16:
22851 case DW_OP_lit17:
22852 case DW_OP_lit18:
22853 case DW_OP_lit19:
22854 case DW_OP_lit20:
22855 case DW_OP_lit21:
22856 case DW_OP_lit22:
22857 case DW_OP_lit23:
22858 case DW_OP_lit24:
22859 case DW_OP_lit25:
22860 case DW_OP_lit26:
22861 case DW_OP_lit27:
22862 case DW_OP_lit28:
22863 case DW_OP_lit29:
22864 case DW_OP_lit30:
22865 case DW_OP_lit31:
22866 stack[++stacki] = op - DW_OP_lit0;
22867 break;
22868
22869 case DW_OP_reg0:
22870 case DW_OP_reg1:
22871 case DW_OP_reg2:
22872 case DW_OP_reg3:
22873 case DW_OP_reg4:
22874 case DW_OP_reg5:
22875 case DW_OP_reg6:
22876 case DW_OP_reg7:
22877 case DW_OP_reg8:
22878 case DW_OP_reg9:
22879 case DW_OP_reg10:
22880 case DW_OP_reg11:
22881 case DW_OP_reg12:
22882 case DW_OP_reg13:
22883 case DW_OP_reg14:
22884 case DW_OP_reg15:
22885 case DW_OP_reg16:
22886 case DW_OP_reg17:
22887 case DW_OP_reg18:
22888 case DW_OP_reg19:
22889 case DW_OP_reg20:
22890 case DW_OP_reg21:
22891 case DW_OP_reg22:
22892 case DW_OP_reg23:
22893 case DW_OP_reg24:
22894 case DW_OP_reg25:
22895 case DW_OP_reg26:
22896 case DW_OP_reg27:
22897 case DW_OP_reg28:
22898 case DW_OP_reg29:
22899 case DW_OP_reg30:
22900 case DW_OP_reg31:
22901 stack[++stacki] = op - DW_OP_reg0;
22902 if (i < size)
22903 dwarf2_complex_location_expr_complaint ();
22904 break;
22905
22906 case DW_OP_regx:
22907 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22908 i += bytes_read;
22909 stack[++stacki] = unsnd;
22910 if (i < size)
22911 dwarf2_complex_location_expr_complaint ();
22912 break;
22913
22914 case DW_OP_addr:
22915 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22916 &bytes_read);
22917 i += bytes_read;
22918 break;
22919
22920 case DW_OP_const1u:
22921 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22922 i += 1;
22923 break;
22924
22925 case DW_OP_const1s:
22926 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22927 i += 1;
22928 break;
22929
22930 case DW_OP_const2u:
22931 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22932 i += 2;
22933 break;
22934
22935 case DW_OP_const2s:
22936 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22937 i += 2;
22938 break;
22939
22940 case DW_OP_const4u:
22941 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22942 i += 4;
22943 break;
22944
22945 case DW_OP_const4s:
22946 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22947 i += 4;
22948 break;
22949
22950 case DW_OP_const8u:
22951 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22952 i += 8;
22953 break;
22954
22955 case DW_OP_constu:
22956 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22957 &bytes_read);
22958 i += bytes_read;
22959 break;
22960
22961 case DW_OP_consts:
22962 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22963 i += bytes_read;
22964 break;
22965
22966 case DW_OP_dup:
22967 stack[stacki + 1] = stack[stacki];
22968 stacki++;
22969 break;
22970
22971 case DW_OP_plus:
22972 stack[stacki - 1] += stack[stacki];
22973 stacki--;
22974 break;
22975
22976 case DW_OP_plus_uconst:
22977 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22978 &bytes_read);
22979 i += bytes_read;
22980 break;
22981
22982 case DW_OP_minus:
22983 stack[stacki - 1] -= stack[stacki];
22984 stacki--;
22985 break;
22986
22987 case DW_OP_deref:
22988 /* If we're not the last op, then we definitely can't encode
22989 this using GDB's address_class enum. This is valid for partial
22990 global symbols, although the variable's address will be bogus
22991 in the psymtab. */
22992 if (i < size)
22993 dwarf2_complex_location_expr_complaint ();
22994 break;
22995
22996 case DW_OP_GNU_push_tls_address:
22997 case DW_OP_form_tls_address:
22998 /* The top of the stack has the offset from the beginning
22999 of the thread control block at which the variable is located. */
23000 /* Nothing should follow this operator, so the top of stack would
23001 be returned. */
23002 /* This is valid for partial global symbols, but the variable's
23003 address will be bogus in the psymtab. Make it always at least
23004 non-zero to not look as a variable garbage collected by linker
23005 which have DW_OP_addr 0. */
23006 if (i < size)
23007 dwarf2_complex_location_expr_complaint ();
23008 stack[stacki]++;
23009 break;
23010
23011 case DW_OP_GNU_uninit:
23012 break;
23013
23014 case DW_OP_addrx:
23015 case DW_OP_GNU_addr_index:
23016 case DW_OP_GNU_const_index:
23017 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23018 &bytes_read);
23019 i += bytes_read;
23020 break;
23021
23022 default:
23023 {
23024 const char *name = get_DW_OP_name (op);
23025
23026 if (name)
23027 complaint (_("unsupported stack op: '%s'"),
23028 name);
23029 else
23030 complaint (_("unsupported stack op: '%02x'"),
23031 op);
23032 }
23033
23034 return (stack[stacki]);
23035 }
23036
23037 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23038 outside of the allocated space. Also enforce minimum>0. */
23039 if (stacki >= ARRAY_SIZE (stack) - 1)
23040 {
23041 complaint (_("location description stack overflow"));
23042 return 0;
23043 }
23044
23045 if (stacki <= 0)
23046 {
23047 complaint (_("location description stack underflow"));
23048 return 0;
23049 }
23050 }
23051 return (stack[stacki]);
23052 }
23053
23054 /* memory allocation interface */
23055
23056 static struct dwarf_block *
23057 dwarf_alloc_block (struct dwarf2_cu *cu)
23058 {
23059 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23060 }
23061
23062 static struct die_info *
23063 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23064 {
23065 struct die_info *die;
23066 size_t size = sizeof (struct die_info);
23067
23068 if (num_attrs > 1)
23069 size += (num_attrs - 1) * sizeof (struct attribute);
23070
23071 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23072 memset (die, 0, sizeof (struct die_info));
23073 return (die);
23074 }
23075
23076 \f
23077 /* Macro support. */
23078
23079 static struct macro_source_file *
23080 macro_start_file (struct dwarf2_cu *cu,
23081 int file, int line,
23082 struct macro_source_file *current_file,
23083 struct line_header *lh)
23084 {
23085 /* File name relative to the compilation directory of this source file. */
23086 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
23087
23088 if (! current_file)
23089 {
23090 /* Note: We don't create a macro table for this compilation unit
23091 at all until we actually get a filename. */
23092 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
23093
23094 /* If we have no current file, then this must be the start_file
23095 directive for the compilation unit's main source file. */
23096 current_file = macro_set_main (macro_table, file_name.get ());
23097 macro_define_special (macro_table);
23098 }
23099 else
23100 current_file = macro_include (current_file, line, file_name.get ());
23101
23102 return current_file;
23103 }
23104
23105 static const char *
23106 consume_improper_spaces (const char *p, const char *body)
23107 {
23108 if (*p == ' ')
23109 {
23110 complaint (_("macro definition contains spaces "
23111 "in formal argument list:\n`%s'"),
23112 body);
23113
23114 while (*p == ' ')
23115 p++;
23116 }
23117
23118 return p;
23119 }
23120
23121
23122 static void
23123 parse_macro_definition (struct macro_source_file *file, int line,
23124 const char *body)
23125 {
23126 const char *p;
23127
23128 /* The body string takes one of two forms. For object-like macro
23129 definitions, it should be:
23130
23131 <macro name> " " <definition>
23132
23133 For function-like macro definitions, it should be:
23134
23135 <macro name> "() " <definition>
23136 or
23137 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23138
23139 Spaces may appear only where explicitly indicated, and in the
23140 <definition>.
23141
23142 The Dwarf 2 spec says that an object-like macro's name is always
23143 followed by a space, but versions of GCC around March 2002 omit
23144 the space when the macro's definition is the empty string.
23145
23146 The Dwarf 2 spec says that there should be no spaces between the
23147 formal arguments in a function-like macro's formal argument list,
23148 but versions of GCC around March 2002 include spaces after the
23149 commas. */
23150
23151
23152 /* Find the extent of the macro name. The macro name is terminated
23153 by either a space or null character (for an object-like macro) or
23154 an opening paren (for a function-like macro). */
23155 for (p = body; *p; p++)
23156 if (*p == ' ' || *p == '(')
23157 break;
23158
23159 if (*p == ' ' || *p == '\0')
23160 {
23161 /* It's an object-like macro. */
23162 int name_len = p - body;
23163 std::string name (body, name_len);
23164 const char *replacement;
23165
23166 if (*p == ' ')
23167 replacement = body + name_len + 1;
23168 else
23169 {
23170 dwarf2_macro_malformed_definition_complaint (body);
23171 replacement = body + name_len;
23172 }
23173
23174 macro_define_object (file, line, name.c_str (), replacement);
23175 }
23176 else if (*p == '(')
23177 {
23178 /* It's a function-like macro. */
23179 std::string name (body, p - body);
23180 int argc = 0;
23181 int argv_size = 1;
23182 char **argv = XNEWVEC (char *, argv_size);
23183
23184 p++;
23185
23186 p = consume_improper_spaces (p, body);
23187
23188 /* Parse the formal argument list. */
23189 while (*p && *p != ')')
23190 {
23191 /* Find the extent of the current argument name. */
23192 const char *arg_start = p;
23193
23194 while (*p && *p != ',' && *p != ')' && *p != ' ')
23195 p++;
23196
23197 if (! *p || p == arg_start)
23198 dwarf2_macro_malformed_definition_complaint (body);
23199 else
23200 {
23201 /* Make sure argv has room for the new argument. */
23202 if (argc >= argv_size)
23203 {
23204 argv_size *= 2;
23205 argv = XRESIZEVEC (char *, argv, argv_size);
23206 }
23207
23208 argv[argc++] = savestring (arg_start, p - arg_start);
23209 }
23210
23211 p = consume_improper_spaces (p, body);
23212
23213 /* Consume the comma, if present. */
23214 if (*p == ',')
23215 {
23216 p++;
23217
23218 p = consume_improper_spaces (p, body);
23219 }
23220 }
23221
23222 if (*p == ')')
23223 {
23224 p++;
23225
23226 if (*p == ' ')
23227 /* Perfectly formed definition, no complaints. */
23228 macro_define_function (file, line, name.c_str (),
23229 argc, (const char **) argv,
23230 p + 1);
23231 else if (*p == '\0')
23232 {
23233 /* Complain, but do define it. */
23234 dwarf2_macro_malformed_definition_complaint (body);
23235 macro_define_function (file, line, name.c_str (),
23236 argc, (const char **) argv,
23237 p);
23238 }
23239 else
23240 /* Just complain. */
23241 dwarf2_macro_malformed_definition_complaint (body);
23242 }
23243 else
23244 /* Just complain. */
23245 dwarf2_macro_malformed_definition_complaint (body);
23246
23247 {
23248 int i;
23249
23250 for (i = 0; i < argc; i++)
23251 xfree (argv[i]);
23252 }
23253 xfree (argv);
23254 }
23255 else
23256 dwarf2_macro_malformed_definition_complaint (body);
23257 }
23258
23259 /* Skip some bytes from BYTES according to the form given in FORM.
23260 Returns the new pointer. */
23261
23262 static const gdb_byte *
23263 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23264 enum dwarf_form form,
23265 unsigned int offset_size,
23266 struct dwarf2_section_info *section)
23267 {
23268 unsigned int bytes_read;
23269
23270 switch (form)
23271 {
23272 case DW_FORM_data1:
23273 case DW_FORM_flag:
23274 ++bytes;
23275 break;
23276
23277 case DW_FORM_data2:
23278 bytes += 2;
23279 break;
23280
23281 case DW_FORM_data4:
23282 bytes += 4;
23283 break;
23284
23285 case DW_FORM_data8:
23286 bytes += 8;
23287 break;
23288
23289 case DW_FORM_data16:
23290 bytes += 16;
23291 break;
23292
23293 case DW_FORM_string:
23294 read_direct_string (abfd, bytes, &bytes_read);
23295 bytes += bytes_read;
23296 break;
23297
23298 case DW_FORM_sec_offset:
23299 case DW_FORM_strp:
23300 case DW_FORM_GNU_strp_alt:
23301 bytes += offset_size;
23302 break;
23303
23304 case DW_FORM_block:
23305 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23306 bytes += bytes_read;
23307 break;
23308
23309 case DW_FORM_block1:
23310 bytes += 1 + read_1_byte (abfd, bytes);
23311 break;
23312 case DW_FORM_block2:
23313 bytes += 2 + read_2_bytes (abfd, bytes);
23314 break;
23315 case DW_FORM_block4:
23316 bytes += 4 + read_4_bytes (abfd, bytes);
23317 break;
23318
23319 case DW_FORM_addrx:
23320 case DW_FORM_sdata:
23321 case DW_FORM_strx:
23322 case DW_FORM_udata:
23323 case DW_FORM_GNU_addr_index:
23324 case DW_FORM_GNU_str_index:
23325 bytes = gdb_skip_leb128 (bytes, buffer_end);
23326 if (bytes == NULL)
23327 {
23328 dwarf2_section_buffer_overflow_complaint (section);
23329 return NULL;
23330 }
23331 break;
23332
23333 case DW_FORM_implicit_const:
23334 break;
23335
23336 default:
23337 {
23338 complaint (_("invalid form 0x%x in `%s'"),
23339 form, section->get_name ());
23340 return NULL;
23341 }
23342 }
23343
23344 return bytes;
23345 }
23346
23347 /* A helper for dwarf_decode_macros that handles skipping an unknown
23348 opcode. Returns an updated pointer to the macro data buffer; or,
23349 on error, issues a complaint and returns NULL. */
23350
23351 static const gdb_byte *
23352 skip_unknown_opcode (unsigned int opcode,
23353 const gdb_byte **opcode_definitions,
23354 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23355 bfd *abfd,
23356 unsigned int offset_size,
23357 struct dwarf2_section_info *section)
23358 {
23359 unsigned int bytes_read, i;
23360 unsigned long arg;
23361 const gdb_byte *defn;
23362
23363 if (opcode_definitions[opcode] == NULL)
23364 {
23365 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
23366 opcode);
23367 return NULL;
23368 }
23369
23370 defn = opcode_definitions[opcode];
23371 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23372 defn += bytes_read;
23373
23374 for (i = 0; i < arg; ++i)
23375 {
23376 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23377 (enum dwarf_form) defn[i], offset_size,
23378 section);
23379 if (mac_ptr == NULL)
23380 {
23381 /* skip_form_bytes already issued the complaint. */
23382 return NULL;
23383 }
23384 }
23385
23386 return mac_ptr;
23387 }
23388
23389 /* A helper function which parses the header of a macro section.
23390 If the macro section is the extended (for now called "GNU") type,
23391 then this updates *OFFSET_SIZE. Returns a pointer to just after
23392 the header, or issues a complaint and returns NULL on error. */
23393
23394 static const gdb_byte *
23395 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
23396 bfd *abfd,
23397 const gdb_byte *mac_ptr,
23398 unsigned int *offset_size,
23399 int section_is_gnu)
23400 {
23401 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
23402
23403 if (section_is_gnu)
23404 {
23405 unsigned int version, flags;
23406
23407 version = read_2_bytes (abfd, mac_ptr);
23408 if (version != 4 && version != 5)
23409 {
23410 complaint (_("unrecognized version `%d' in .debug_macro section"),
23411 version);
23412 return NULL;
23413 }
23414 mac_ptr += 2;
23415
23416 flags = read_1_byte (abfd, mac_ptr);
23417 ++mac_ptr;
23418 *offset_size = (flags & 1) ? 8 : 4;
23419
23420 if ((flags & 2) != 0)
23421 /* We don't need the line table offset. */
23422 mac_ptr += *offset_size;
23423
23424 /* Vendor opcode descriptions. */
23425 if ((flags & 4) != 0)
23426 {
23427 unsigned int i, count;
23428
23429 count = read_1_byte (abfd, mac_ptr);
23430 ++mac_ptr;
23431 for (i = 0; i < count; ++i)
23432 {
23433 unsigned int opcode, bytes_read;
23434 unsigned long arg;
23435
23436 opcode = read_1_byte (abfd, mac_ptr);
23437 ++mac_ptr;
23438 opcode_definitions[opcode] = mac_ptr;
23439 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23440 mac_ptr += bytes_read;
23441 mac_ptr += arg;
23442 }
23443 }
23444 }
23445
23446 return mac_ptr;
23447 }
23448
23449 /* A helper for dwarf_decode_macros that handles the GNU extensions,
23450 including DW_MACRO_import. */
23451
23452 static void
23453 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
23454 bfd *abfd,
23455 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23456 struct macro_source_file *current_file,
23457 struct line_header *lh,
23458 struct dwarf2_section_info *section,
23459 int section_is_gnu, int section_is_dwz,
23460 unsigned int offset_size,
23461 htab_t include_hash)
23462 {
23463 struct dwarf2_per_objfile *dwarf2_per_objfile
23464 = cu->per_cu->dwarf2_per_objfile;
23465 struct objfile *objfile = dwarf2_per_objfile->objfile;
23466 enum dwarf_macro_record_type macinfo_type;
23467 int at_commandline;
23468 const gdb_byte *opcode_definitions[256];
23469
23470 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23471 &offset_size, section_is_gnu);
23472 if (mac_ptr == NULL)
23473 {
23474 /* We already issued a complaint. */
23475 return;
23476 }
23477
23478 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23479 GDB is still reading the definitions from command line. First
23480 DW_MACINFO_start_file will need to be ignored as it was already executed
23481 to create CURRENT_FILE for the main source holding also the command line
23482 definitions. On first met DW_MACINFO_start_file this flag is reset to
23483 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23484
23485 at_commandline = 1;
23486
23487 do
23488 {
23489 /* Do we at least have room for a macinfo type byte? */
23490 if (mac_ptr >= mac_end)
23491 {
23492 dwarf2_section_buffer_overflow_complaint (section);
23493 break;
23494 }
23495
23496 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23497 mac_ptr++;
23498
23499 /* Note that we rely on the fact that the corresponding GNU and
23500 DWARF constants are the same. */
23501 DIAGNOSTIC_PUSH
23502 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23503 switch (macinfo_type)
23504 {
23505 /* A zero macinfo type indicates the end of the macro
23506 information. */
23507 case 0:
23508 break;
23509
23510 case DW_MACRO_define:
23511 case DW_MACRO_undef:
23512 case DW_MACRO_define_strp:
23513 case DW_MACRO_undef_strp:
23514 case DW_MACRO_define_sup:
23515 case DW_MACRO_undef_sup:
23516 {
23517 unsigned int bytes_read;
23518 int line;
23519 const char *body;
23520 int is_define;
23521
23522 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23523 mac_ptr += bytes_read;
23524
23525 if (macinfo_type == DW_MACRO_define
23526 || macinfo_type == DW_MACRO_undef)
23527 {
23528 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23529 mac_ptr += bytes_read;
23530 }
23531 else
23532 {
23533 LONGEST str_offset;
23534
23535 str_offset = read_offset (abfd, mac_ptr, offset_size);
23536 mac_ptr += offset_size;
23537
23538 if (macinfo_type == DW_MACRO_define_sup
23539 || macinfo_type == DW_MACRO_undef_sup
23540 || section_is_dwz)
23541 {
23542 struct dwz_file *dwz
23543 = dwarf2_get_dwz_file (dwarf2_per_objfile);
23544
23545 body = read_indirect_string_from_dwz (objfile,
23546 dwz, str_offset);
23547 }
23548 else
23549 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23550 abfd, str_offset);
23551 }
23552
23553 is_define = (macinfo_type == DW_MACRO_define
23554 || macinfo_type == DW_MACRO_define_strp
23555 || macinfo_type == DW_MACRO_define_sup);
23556 if (! current_file)
23557 {
23558 /* DWARF violation as no main source is present. */
23559 complaint (_("debug info with no main source gives macro %s "
23560 "on line %d: %s"),
23561 is_define ? _("definition") : _("undefinition"),
23562 line, body);
23563 break;
23564 }
23565 if ((line == 0 && !at_commandline)
23566 || (line != 0 && at_commandline))
23567 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
23568 at_commandline ? _("command-line") : _("in-file"),
23569 is_define ? _("definition") : _("undefinition"),
23570 line == 0 ? _("zero") : _("non-zero"), line, body);
23571
23572 if (body == NULL)
23573 {
23574 /* Fedora's rpm-build's "debugedit" binary
23575 corrupted .debug_macro sections.
23576
23577 For more info, see
23578 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23579 complaint (_("debug info gives %s invalid macro %s "
23580 "without body (corrupted?) at line %d "
23581 "on file %s"),
23582 at_commandline ? _("command-line") : _("in-file"),
23583 is_define ? _("definition") : _("undefinition"),
23584 line, current_file->filename);
23585 }
23586 else if (is_define)
23587 parse_macro_definition (current_file, line, body);
23588 else
23589 {
23590 gdb_assert (macinfo_type == DW_MACRO_undef
23591 || macinfo_type == DW_MACRO_undef_strp
23592 || macinfo_type == DW_MACRO_undef_sup);
23593 macro_undef (current_file, line, body);
23594 }
23595 }
23596 break;
23597
23598 case DW_MACRO_start_file:
23599 {
23600 unsigned int bytes_read;
23601 int line, file;
23602
23603 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23604 mac_ptr += bytes_read;
23605 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23606 mac_ptr += bytes_read;
23607
23608 if ((line == 0 && !at_commandline)
23609 || (line != 0 && at_commandline))
23610 complaint (_("debug info gives source %d included "
23611 "from %s at %s line %d"),
23612 file, at_commandline ? _("command-line") : _("file"),
23613 line == 0 ? _("zero") : _("non-zero"), line);
23614
23615 if (at_commandline)
23616 {
23617 /* This DW_MACRO_start_file was executed in the
23618 pass one. */
23619 at_commandline = 0;
23620 }
23621 else
23622 current_file = macro_start_file (cu, file, line, current_file,
23623 lh);
23624 }
23625 break;
23626
23627 case DW_MACRO_end_file:
23628 if (! current_file)
23629 complaint (_("macro debug info has an unmatched "
23630 "`close_file' directive"));
23631 else
23632 {
23633 current_file = current_file->included_by;
23634 if (! current_file)
23635 {
23636 enum dwarf_macro_record_type next_type;
23637
23638 /* GCC circa March 2002 doesn't produce the zero
23639 type byte marking the end of the compilation
23640 unit. Complain if it's not there, but exit no
23641 matter what. */
23642
23643 /* Do we at least have room for a macinfo type byte? */
23644 if (mac_ptr >= mac_end)
23645 {
23646 dwarf2_section_buffer_overflow_complaint (section);
23647 return;
23648 }
23649
23650 /* We don't increment mac_ptr here, so this is just
23651 a look-ahead. */
23652 next_type
23653 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23654 mac_ptr);
23655 if (next_type != 0)
23656 complaint (_("no terminating 0-type entry for "
23657 "macros in `.debug_macinfo' section"));
23658
23659 return;
23660 }
23661 }
23662 break;
23663
23664 case DW_MACRO_import:
23665 case DW_MACRO_import_sup:
23666 {
23667 LONGEST offset;
23668 void **slot;
23669 bfd *include_bfd = abfd;
23670 struct dwarf2_section_info *include_section = section;
23671 const gdb_byte *include_mac_end = mac_end;
23672 int is_dwz = section_is_dwz;
23673 const gdb_byte *new_mac_ptr;
23674
23675 offset = read_offset (abfd, mac_ptr, offset_size);
23676 mac_ptr += offset_size;
23677
23678 if (macinfo_type == DW_MACRO_import_sup)
23679 {
23680 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
23681
23682 dwz->macro.read (objfile);
23683
23684 include_section = &dwz->macro;
23685 include_bfd = include_section->get_bfd_owner ();
23686 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23687 is_dwz = 1;
23688 }
23689
23690 new_mac_ptr = include_section->buffer + offset;
23691 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23692
23693 if (*slot != NULL)
23694 {
23695 /* This has actually happened; see
23696 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23697 complaint (_("recursive DW_MACRO_import in "
23698 ".debug_macro section"));
23699 }
23700 else
23701 {
23702 *slot = (void *) new_mac_ptr;
23703
23704 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
23705 include_mac_end, current_file, lh,
23706 section, section_is_gnu, is_dwz,
23707 offset_size, include_hash);
23708
23709 htab_remove_elt (include_hash, (void *) new_mac_ptr);
23710 }
23711 }
23712 break;
23713
23714 case DW_MACINFO_vendor_ext:
23715 if (!section_is_gnu)
23716 {
23717 unsigned int bytes_read;
23718
23719 /* This reads the constant, but since we don't recognize
23720 any vendor extensions, we ignore it. */
23721 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23722 mac_ptr += bytes_read;
23723 read_direct_string (abfd, mac_ptr, &bytes_read);
23724 mac_ptr += bytes_read;
23725
23726 /* We don't recognize any vendor extensions. */
23727 break;
23728 }
23729 /* FALLTHROUGH */
23730
23731 default:
23732 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23733 mac_ptr, mac_end, abfd, offset_size,
23734 section);
23735 if (mac_ptr == NULL)
23736 return;
23737 break;
23738 }
23739 DIAGNOSTIC_POP
23740 } while (macinfo_type != 0);
23741 }
23742
23743 static void
23744 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23745 int section_is_gnu)
23746 {
23747 struct dwarf2_per_objfile *dwarf2_per_objfile
23748 = cu->per_cu->dwarf2_per_objfile;
23749 struct objfile *objfile = dwarf2_per_objfile->objfile;
23750 struct line_header *lh = cu->line_header;
23751 bfd *abfd;
23752 const gdb_byte *mac_ptr, *mac_end;
23753 struct macro_source_file *current_file = 0;
23754 enum dwarf_macro_record_type macinfo_type;
23755 unsigned int offset_size = cu->header.offset_size;
23756 const gdb_byte *opcode_definitions[256];
23757 void **slot;
23758 struct dwarf2_section_info *section;
23759 const char *section_name;
23760
23761 if (cu->dwo_unit != NULL)
23762 {
23763 if (section_is_gnu)
23764 {
23765 section = &cu->dwo_unit->dwo_file->sections.macro;
23766 section_name = ".debug_macro.dwo";
23767 }
23768 else
23769 {
23770 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23771 section_name = ".debug_macinfo.dwo";
23772 }
23773 }
23774 else
23775 {
23776 if (section_is_gnu)
23777 {
23778 section = &dwarf2_per_objfile->macro;
23779 section_name = ".debug_macro";
23780 }
23781 else
23782 {
23783 section = &dwarf2_per_objfile->macinfo;
23784 section_name = ".debug_macinfo";
23785 }
23786 }
23787
23788 section->read (objfile);
23789 if (section->buffer == NULL)
23790 {
23791 complaint (_("missing %s section"), section_name);
23792 return;
23793 }
23794 abfd = section->get_bfd_owner ();
23795
23796 /* First pass: Find the name of the base filename.
23797 This filename is needed in order to process all macros whose definition
23798 (or undefinition) comes from the command line. These macros are defined
23799 before the first DW_MACINFO_start_file entry, and yet still need to be
23800 associated to the base file.
23801
23802 To determine the base file name, we scan the macro definitions until we
23803 reach the first DW_MACINFO_start_file entry. We then initialize
23804 CURRENT_FILE accordingly so that any macro definition found before the
23805 first DW_MACINFO_start_file can still be associated to the base file. */
23806
23807 mac_ptr = section->buffer + offset;
23808 mac_end = section->buffer + section->size;
23809
23810 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23811 &offset_size, section_is_gnu);
23812 if (mac_ptr == NULL)
23813 {
23814 /* We already issued a complaint. */
23815 return;
23816 }
23817
23818 do
23819 {
23820 /* Do we at least have room for a macinfo type byte? */
23821 if (mac_ptr >= mac_end)
23822 {
23823 /* Complaint is printed during the second pass as GDB will probably
23824 stop the first pass earlier upon finding
23825 DW_MACINFO_start_file. */
23826 break;
23827 }
23828
23829 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23830 mac_ptr++;
23831
23832 /* Note that we rely on the fact that the corresponding GNU and
23833 DWARF constants are the same. */
23834 DIAGNOSTIC_PUSH
23835 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23836 switch (macinfo_type)
23837 {
23838 /* A zero macinfo type indicates the end of the macro
23839 information. */
23840 case 0:
23841 break;
23842
23843 case DW_MACRO_define:
23844 case DW_MACRO_undef:
23845 /* Only skip the data by MAC_PTR. */
23846 {
23847 unsigned int bytes_read;
23848
23849 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23850 mac_ptr += bytes_read;
23851 read_direct_string (abfd, mac_ptr, &bytes_read);
23852 mac_ptr += bytes_read;
23853 }
23854 break;
23855
23856 case DW_MACRO_start_file:
23857 {
23858 unsigned int bytes_read;
23859 int line, file;
23860
23861 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23862 mac_ptr += bytes_read;
23863 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23864 mac_ptr += bytes_read;
23865
23866 current_file = macro_start_file (cu, file, line, current_file, lh);
23867 }
23868 break;
23869
23870 case DW_MACRO_end_file:
23871 /* No data to skip by MAC_PTR. */
23872 break;
23873
23874 case DW_MACRO_define_strp:
23875 case DW_MACRO_undef_strp:
23876 case DW_MACRO_define_sup:
23877 case DW_MACRO_undef_sup:
23878 {
23879 unsigned int bytes_read;
23880
23881 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23882 mac_ptr += bytes_read;
23883 mac_ptr += offset_size;
23884 }
23885 break;
23886
23887 case DW_MACRO_import:
23888 case DW_MACRO_import_sup:
23889 /* Note that, according to the spec, a transparent include
23890 chain cannot call DW_MACRO_start_file. So, we can just
23891 skip this opcode. */
23892 mac_ptr += offset_size;
23893 break;
23894
23895 case DW_MACINFO_vendor_ext:
23896 /* Only skip the data by MAC_PTR. */
23897 if (!section_is_gnu)
23898 {
23899 unsigned int bytes_read;
23900
23901 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23902 mac_ptr += bytes_read;
23903 read_direct_string (abfd, mac_ptr, &bytes_read);
23904 mac_ptr += bytes_read;
23905 }
23906 /* FALLTHROUGH */
23907
23908 default:
23909 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23910 mac_ptr, mac_end, abfd, offset_size,
23911 section);
23912 if (mac_ptr == NULL)
23913 return;
23914 break;
23915 }
23916 DIAGNOSTIC_POP
23917 } while (macinfo_type != 0 && current_file == NULL);
23918
23919 /* Second pass: Process all entries.
23920
23921 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23922 command-line macro definitions/undefinitions. This flag is unset when we
23923 reach the first DW_MACINFO_start_file entry. */
23924
23925 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23926 htab_eq_pointer,
23927 NULL, xcalloc, xfree));
23928 mac_ptr = section->buffer + offset;
23929 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
23930 *slot = (void *) mac_ptr;
23931 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
23932 current_file, lh, section,
23933 section_is_gnu, 0, offset_size,
23934 include_hash.get ());
23935 }
23936
23937 /* Return the .debug_loc section to use for CU.
23938 For DWO files use .debug_loc.dwo. */
23939
23940 static struct dwarf2_section_info *
23941 cu_debug_loc_section (struct dwarf2_cu *cu)
23942 {
23943 struct dwarf2_per_objfile *dwarf2_per_objfile
23944 = cu->per_cu->dwarf2_per_objfile;
23945
23946 if (cu->dwo_unit)
23947 {
23948 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23949
23950 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23951 }
23952 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23953 : &dwarf2_per_objfile->loc);
23954 }
23955
23956 /* A helper function that fills in a dwarf2_loclist_baton. */
23957
23958 static void
23959 fill_in_loclist_baton (struct dwarf2_cu *cu,
23960 struct dwarf2_loclist_baton *baton,
23961 const struct attribute *attr)
23962 {
23963 struct dwarf2_per_objfile *dwarf2_per_objfile
23964 = cu->per_cu->dwarf2_per_objfile;
23965 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23966
23967 section->read (dwarf2_per_objfile->objfile);
23968
23969 baton->per_cu = cu->per_cu;
23970 gdb_assert (baton->per_cu);
23971 /* We don't know how long the location list is, but make sure we
23972 don't run off the edge of the section. */
23973 baton->size = section->size - DW_UNSND (attr);
23974 baton->data = section->buffer + DW_UNSND (attr);
23975 baton->base_address = cu->base_address;
23976 baton->from_dwo = cu->dwo_unit != NULL;
23977 }
23978
23979 static void
23980 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23981 struct dwarf2_cu *cu, int is_block)
23982 {
23983 struct dwarf2_per_objfile *dwarf2_per_objfile
23984 = cu->per_cu->dwarf2_per_objfile;
23985 struct objfile *objfile = dwarf2_per_objfile->objfile;
23986 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23987
23988 if (attr->form_is_section_offset ()
23989 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23990 the section. If so, fall through to the complaint in the
23991 other branch. */
23992 && DW_UNSND (attr) < section->get_size (objfile))
23993 {
23994 struct dwarf2_loclist_baton *baton;
23995
23996 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23997
23998 fill_in_loclist_baton (cu, baton, attr);
23999
24000 if (cu->base_known == 0)
24001 complaint (_("Location list used without "
24002 "specifying the CU base address."));
24003
24004 SYMBOL_ACLASS_INDEX (sym) = (is_block
24005 ? dwarf2_loclist_block_index
24006 : dwarf2_loclist_index);
24007 SYMBOL_LOCATION_BATON (sym) = baton;
24008 }
24009 else
24010 {
24011 struct dwarf2_locexpr_baton *baton;
24012
24013 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24014 baton->per_cu = cu->per_cu;
24015 gdb_assert (baton->per_cu);
24016
24017 if (attr->form_is_block ())
24018 {
24019 /* Note that we're just copying the block's data pointer
24020 here, not the actual data. We're still pointing into the
24021 info_buffer for SYM's objfile; right now we never release
24022 that buffer, but when we do clean up properly this may
24023 need to change. */
24024 baton->size = DW_BLOCK (attr)->size;
24025 baton->data = DW_BLOCK (attr)->data;
24026 }
24027 else
24028 {
24029 dwarf2_invalid_attrib_class_complaint ("location description",
24030 sym->natural_name ());
24031 baton->size = 0;
24032 }
24033
24034 SYMBOL_ACLASS_INDEX (sym) = (is_block
24035 ? dwarf2_locexpr_block_index
24036 : dwarf2_locexpr_index);
24037 SYMBOL_LOCATION_BATON (sym) = baton;
24038 }
24039 }
24040
24041 /* See read.h. */
24042
24043 struct objfile *
24044 dwarf2_per_cu_data::objfile () const
24045 {
24046 struct objfile *objfile = dwarf2_per_objfile->objfile;
24047
24048 /* Return the master objfile, so that we can report and look up the
24049 correct file containing this variable. */
24050 if (objfile->separate_debug_objfile_backlink)
24051 objfile = objfile->separate_debug_objfile_backlink;
24052
24053 return objfile;
24054 }
24055
24056 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24057 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24058 CU_HEADERP first. */
24059
24060 static const struct comp_unit_head *
24061 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24062 const struct dwarf2_per_cu_data *per_cu)
24063 {
24064 const gdb_byte *info_ptr;
24065
24066 if (per_cu->cu)
24067 return &per_cu->cu->header;
24068
24069 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24070
24071 memset (cu_headerp, 0, sizeof (*cu_headerp));
24072 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24073 rcuh_kind::COMPILE);
24074
24075 return cu_headerp;
24076 }
24077
24078 /* See read.h. */
24079
24080 int
24081 dwarf2_per_cu_data::addr_size () const
24082 {
24083 struct comp_unit_head cu_header_local;
24084 const struct comp_unit_head *cu_headerp;
24085
24086 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24087
24088 return cu_headerp->addr_size;
24089 }
24090
24091 /* See read.h. */
24092
24093 int
24094 dwarf2_per_cu_data::offset_size () const
24095 {
24096 struct comp_unit_head cu_header_local;
24097 const struct comp_unit_head *cu_headerp;
24098
24099 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24100
24101 return cu_headerp->offset_size;
24102 }
24103
24104 /* See read.h. */
24105
24106 int
24107 dwarf2_per_cu_data::ref_addr_size () const
24108 {
24109 struct comp_unit_head cu_header_local;
24110 const struct comp_unit_head *cu_headerp;
24111
24112 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24113
24114 if (cu_headerp->version == 2)
24115 return cu_headerp->addr_size;
24116 else
24117 return cu_headerp->offset_size;
24118 }
24119
24120 /* See read.h. */
24121
24122 CORE_ADDR
24123 dwarf2_per_cu_data::text_offset () const
24124 {
24125 struct objfile *objfile = dwarf2_per_objfile->objfile;
24126
24127 return objfile->text_section_offset ();
24128 }
24129
24130 /* See read.h. */
24131
24132 struct type *
24133 dwarf2_per_cu_data::addr_type () const
24134 {
24135 struct objfile *objfile = dwarf2_per_objfile->objfile;
24136 struct type *void_type = objfile_type (objfile)->builtin_void;
24137 struct type *addr_type = lookup_pointer_type (void_type);
24138 int addr_size = this->addr_size ();
24139
24140 if (TYPE_LENGTH (addr_type) == addr_size)
24141 return addr_type;
24142
24143 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
24144 return addr_type;
24145 }
24146
24147 /* Locate the .debug_info compilation unit from CU's objfile which contains
24148 the DIE at OFFSET. Raises an error on failure. */
24149
24150 static struct dwarf2_per_cu_data *
24151 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24152 unsigned int offset_in_dwz,
24153 struct dwarf2_per_objfile *dwarf2_per_objfile)
24154 {
24155 struct dwarf2_per_cu_data *this_cu;
24156 int low, high;
24157
24158 low = 0;
24159 high = dwarf2_per_objfile->all_comp_units.size () - 1;
24160 while (high > low)
24161 {
24162 struct dwarf2_per_cu_data *mid_cu;
24163 int mid = low + (high - low) / 2;
24164
24165 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
24166 if (mid_cu->is_dwz > offset_in_dwz
24167 || (mid_cu->is_dwz == offset_in_dwz
24168 && mid_cu->sect_off + mid_cu->length >= sect_off))
24169 high = mid;
24170 else
24171 low = mid + 1;
24172 }
24173 gdb_assert (low == high);
24174 this_cu = dwarf2_per_objfile->all_comp_units[low];
24175 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24176 {
24177 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24178 error (_("Dwarf Error: could not find partial DIE containing "
24179 "offset %s [in module %s]"),
24180 sect_offset_str (sect_off),
24181 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24182
24183 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24184 <= sect_off);
24185 return dwarf2_per_objfile->all_comp_units[low-1];
24186 }
24187 else
24188 {
24189 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24190 && sect_off >= this_cu->sect_off + this_cu->length)
24191 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24192 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24193 return this_cu;
24194 }
24195 }
24196
24197 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24198
24199 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24200 : per_cu (per_cu_),
24201 mark (false),
24202 has_loclist (false),
24203 checked_producer (false),
24204 producer_is_gxx_lt_4_6 (false),
24205 producer_is_gcc_lt_4_3 (false),
24206 producer_is_icc (false),
24207 producer_is_icc_lt_14 (false),
24208 producer_is_codewarrior (false),
24209 processing_has_namespace_info (false)
24210 {
24211 per_cu->cu = this;
24212 }
24213
24214 /* Destroy a dwarf2_cu. */
24215
24216 dwarf2_cu::~dwarf2_cu ()
24217 {
24218 per_cu->cu = NULL;
24219 }
24220
24221 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24222
24223 static void
24224 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24225 enum language pretend_language)
24226 {
24227 struct attribute *attr;
24228
24229 /* Set the language we're debugging. */
24230 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24231 if (attr != nullptr)
24232 set_cu_language (DW_UNSND (attr), cu);
24233 else
24234 {
24235 cu->language = pretend_language;
24236 cu->language_defn = language_def (cu->language);
24237 }
24238
24239 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24240 }
24241
24242 /* Increase the age counter on each cached compilation unit, and free
24243 any that are too old. */
24244
24245 static void
24246 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
24247 {
24248 struct dwarf2_per_cu_data *per_cu, **last_chain;
24249
24250 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24251 per_cu = dwarf2_per_objfile->read_in_chain;
24252 while (per_cu != NULL)
24253 {
24254 per_cu->cu->last_used ++;
24255 if (per_cu->cu->last_used <= dwarf_max_cache_age)
24256 dwarf2_mark (per_cu->cu);
24257 per_cu = per_cu->cu->read_in_chain;
24258 }
24259
24260 per_cu = dwarf2_per_objfile->read_in_chain;
24261 last_chain = &dwarf2_per_objfile->read_in_chain;
24262 while (per_cu != NULL)
24263 {
24264 struct dwarf2_per_cu_data *next_cu;
24265
24266 next_cu = per_cu->cu->read_in_chain;
24267
24268 if (!per_cu->cu->mark)
24269 {
24270 delete per_cu->cu;
24271 *last_chain = next_cu;
24272 }
24273 else
24274 last_chain = &per_cu->cu->read_in_chain;
24275
24276 per_cu = next_cu;
24277 }
24278 }
24279
24280 /* Remove a single compilation unit from the cache. */
24281
24282 static void
24283 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
24284 {
24285 struct dwarf2_per_cu_data *per_cu, **last_chain;
24286 struct dwarf2_per_objfile *dwarf2_per_objfile
24287 = target_per_cu->dwarf2_per_objfile;
24288
24289 per_cu = dwarf2_per_objfile->read_in_chain;
24290 last_chain = &dwarf2_per_objfile->read_in_chain;
24291 while (per_cu != NULL)
24292 {
24293 struct dwarf2_per_cu_data *next_cu;
24294
24295 next_cu = per_cu->cu->read_in_chain;
24296
24297 if (per_cu == target_per_cu)
24298 {
24299 delete per_cu->cu;
24300 per_cu->cu = NULL;
24301 *last_chain = next_cu;
24302 break;
24303 }
24304 else
24305 last_chain = &per_cu->cu->read_in_chain;
24306
24307 per_cu = next_cu;
24308 }
24309 }
24310
24311 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24312 We store these in a hash table separate from the DIEs, and preserve them
24313 when the DIEs are flushed out of cache.
24314
24315 The CU "per_cu" pointer is needed because offset alone is not enough to
24316 uniquely identify the type. A file may have multiple .debug_types sections,
24317 or the type may come from a DWO file. Furthermore, while it's more logical
24318 to use per_cu->section+offset, with Fission the section with the data is in
24319 the DWO file but we don't know that section at the point we need it.
24320 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24321 because we can enter the lookup routine, get_die_type_at_offset, from
24322 outside this file, and thus won't necessarily have PER_CU->cu.
24323 Fortunately, PER_CU is stable for the life of the objfile. */
24324
24325 struct dwarf2_per_cu_offset_and_type
24326 {
24327 const struct dwarf2_per_cu_data *per_cu;
24328 sect_offset sect_off;
24329 struct type *type;
24330 };
24331
24332 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24333
24334 static hashval_t
24335 per_cu_offset_and_type_hash (const void *item)
24336 {
24337 const struct dwarf2_per_cu_offset_and_type *ofs
24338 = (const struct dwarf2_per_cu_offset_and_type *) item;
24339
24340 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24341 }
24342
24343 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24344
24345 static int
24346 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24347 {
24348 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24349 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24350 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24351 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24352
24353 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24354 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24355 }
24356
24357 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24358 table if necessary. For convenience, return TYPE.
24359
24360 The DIEs reading must have careful ordering to:
24361 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24362 reading current DIE.
24363 * Not trying to dereference contents of still incompletely read in types
24364 while reading in other DIEs.
24365 * Enable referencing still incompletely read in types just by a pointer to
24366 the type without accessing its fields.
24367
24368 Therefore caller should follow these rules:
24369 * Try to fetch any prerequisite types we may need to build this DIE type
24370 before building the type and calling set_die_type.
24371 * After building type call set_die_type for current DIE as soon as
24372 possible before fetching more types to complete the current type.
24373 * Make the type as complete as possible before fetching more types. */
24374
24375 static struct type *
24376 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24377 {
24378 struct dwarf2_per_objfile *dwarf2_per_objfile
24379 = cu->per_cu->dwarf2_per_objfile;
24380 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24381 struct objfile *objfile = dwarf2_per_objfile->objfile;
24382 struct attribute *attr;
24383 struct dynamic_prop prop;
24384
24385 /* For Ada types, make sure that the gnat-specific data is always
24386 initialized (if not already set). There are a few types where
24387 we should not be doing so, because the type-specific area is
24388 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24389 where the type-specific area is used to store the floatformat).
24390 But this is not a problem, because the gnat-specific information
24391 is actually not needed for these types. */
24392 if (need_gnat_info (cu)
24393 && TYPE_CODE (type) != TYPE_CODE_FUNC
24394 && TYPE_CODE (type) != TYPE_CODE_FLT
24395 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24396 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24397 && TYPE_CODE (type) != TYPE_CODE_METHOD
24398 && !HAVE_GNAT_AUX_INFO (type))
24399 INIT_GNAT_SPECIFIC (type);
24400
24401 /* Read DW_AT_allocated and set in type. */
24402 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24403 if (attr != NULL && attr->form_is_block ())
24404 {
24405 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24406 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24407 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
24408 }
24409 else if (attr != NULL)
24410 {
24411 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
24412 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24413 sect_offset_str (die->sect_off));
24414 }
24415
24416 /* Read DW_AT_associated and set in type. */
24417 attr = dwarf2_attr (die, DW_AT_associated, cu);
24418 if (attr != NULL && attr->form_is_block ())
24419 {
24420 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24421 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24422 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
24423 }
24424 else if (attr != NULL)
24425 {
24426 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
24427 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24428 sect_offset_str (die->sect_off));
24429 }
24430
24431 /* Read DW_AT_data_location and set in type. */
24432 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24433 if (attr_to_dynamic_prop (attr, die, cu, &prop,
24434 cu->per_cu->addr_type ()))
24435 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
24436
24437 if (dwarf2_per_objfile->die_type_hash == NULL)
24438 dwarf2_per_objfile->die_type_hash
24439 = htab_up (htab_create_alloc (127,
24440 per_cu_offset_and_type_hash,
24441 per_cu_offset_and_type_eq,
24442 NULL, xcalloc, xfree));
24443
24444 ofs.per_cu = cu->per_cu;
24445 ofs.sect_off = die->sect_off;
24446 ofs.type = type;
24447 slot = (struct dwarf2_per_cu_offset_and_type **)
24448 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
24449 if (*slot)
24450 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24451 sect_offset_str (die->sect_off));
24452 *slot = XOBNEW (&objfile->objfile_obstack,
24453 struct dwarf2_per_cu_offset_and_type);
24454 **slot = ofs;
24455 return type;
24456 }
24457
24458 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24459 or return NULL if the die does not have a saved type. */
24460
24461 static struct type *
24462 get_die_type_at_offset (sect_offset sect_off,
24463 struct dwarf2_per_cu_data *per_cu)
24464 {
24465 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24466 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
24467
24468 if (dwarf2_per_objfile->die_type_hash == NULL)
24469 return NULL;
24470
24471 ofs.per_cu = per_cu;
24472 ofs.sect_off = sect_off;
24473 slot = ((struct dwarf2_per_cu_offset_and_type *)
24474 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
24475 if (slot)
24476 return slot->type;
24477 else
24478 return NULL;
24479 }
24480
24481 /* Look up the type for DIE in CU in die_type_hash,
24482 or return NULL if DIE does not have a saved type. */
24483
24484 static struct type *
24485 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24486 {
24487 return get_die_type_at_offset (die->sect_off, cu->per_cu);
24488 }
24489
24490 /* Add a dependence relationship from CU to REF_PER_CU. */
24491
24492 static void
24493 dwarf2_add_dependence (struct dwarf2_cu *cu,
24494 struct dwarf2_per_cu_data *ref_per_cu)
24495 {
24496 void **slot;
24497
24498 if (cu->dependencies == NULL)
24499 cu->dependencies
24500 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24501 NULL, &cu->comp_unit_obstack,
24502 hashtab_obstack_allocate,
24503 dummy_obstack_deallocate);
24504
24505 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24506 if (*slot == NULL)
24507 *slot = ref_per_cu;
24508 }
24509
24510 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24511 Set the mark field in every compilation unit in the
24512 cache that we must keep because we are keeping CU. */
24513
24514 static int
24515 dwarf2_mark_helper (void **slot, void *data)
24516 {
24517 struct dwarf2_per_cu_data *per_cu;
24518
24519 per_cu = (struct dwarf2_per_cu_data *) *slot;
24520
24521 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24522 reading of the chain. As such dependencies remain valid it is not much
24523 useful to track and undo them during QUIT cleanups. */
24524 if (per_cu->cu == NULL)
24525 return 1;
24526
24527 if (per_cu->cu->mark)
24528 return 1;
24529 per_cu->cu->mark = true;
24530
24531 if (per_cu->cu->dependencies != NULL)
24532 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24533
24534 return 1;
24535 }
24536
24537 /* Set the mark field in CU and in every other compilation unit in the
24538 cache that we must keep because we are keeping CU. */
24539
24540 static void
24541 dwarf2_mark (struct dwarf2_cu *cu)
24542 {
24543 if (cu->mark)
24544 return;
24545 cu->mark = true;
24546 if (cu->dependencies != NULL)
24547 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
24548 }
24549
24550 static void
24551 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24552 {
24553 while (per_cu)
24554 {
24555 per_cu->cu->mark = false;
24556 per_cu = per_cu->cu->read_in_chain;
24557 }
24558 }
24559
24560 /* Trivial hash function for partial_die_info: the hash value of a DIE
24561 is its offset in .debug_info for this objfile. */
24562
24563 static hashval_t
24564 partial_die_hash (const void *item)
24565 {
24566 const struct partial_die_info *part_die
24567 = (const struct partial_die_info *) item;
24568
24569 return to_underlying (part_die->sect_off);
24570 }
24571
24572 /* Trivial comparison function for partial_die_info structures: two DIEs
24573 are equal if they have the same offset. */
24574
24575 static int
24576 partial_die_eq (const void *item_lhs, const void *item_rhs)
24577 {
24578 const struct partial_die_info *part_die_lhs
24579 = (const struct partial_die_info *) item_lhs;
24580 const struct partial_die_info *part_die_rhs
24581 = (const struct partial_die_info *) item_rhs;
24582
24583 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24584 }
24585
24586 struct cmd_list_element *set_dwarf_cmdlist;
24587 struct cmd_list_element *show_dwarf_cmdlist;
24588
24589 static void
24590 set_dwarf_cmd (const char *args, int from_tty)
24591 {
24592 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
24593 gdb_stdout);
24594 }
24595
24596 static void
24597 show_dwarf_cmd (const char *args, int from_tty)
24598 {
24599 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
24600 }
24601
24602 static void
24603 show_check_physname (struct ui_file *file, int from_tty,
24604 struct cmd_list_element *c, const char *value)
24605 {
24606 fprintf_filtered (file,
24607 _("Whether to check \"physname\" is %s.\n"),
24608 value);
24609 }
24610
24611 void _initialize_dwarf2_read ();
24612 void
24613 _initialize_dwarf2_read ()
24614 {
24615 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24616 Set DWARF specific variables.\n\
24617 Configure DWARF variables such as the cache size."),
24618 &set_dwarf_cmdlist, "maintenance set dwarf ",
24619 0/*allow-unknown*/, &maintenance_set_cmdlist);
24620
24621 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24622 Show DWARF specific variables.\n\
24623 Show DWARF variables such as the cache size."),
24624 &show_dwarf_cmdlist, "maintenance show dwarf ",
24625 0/*allow-unknown*/, &maintenance_show_cmdlist);
24626
24627 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24628 &dwarf_max_cache_age, _("\
24629 Set the upper bound on the age of cached DWARF compilation units."), _("\
24630 Show the upper bound on the age of cached DWARF compilation units."), _("\
24631 A higher limit means that cached compilation units will be stored\n\
24632 in memory longer, and more total memory will be used. Zero disables\n\
24633 caching, which can slow down startup."),
24634 NULL,
24635 show_dwarf_max_cache_age,
24636 &set_dwarf_cmdlist,
24637 &show_dwarf_cmdlist);
24638
24639 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24640 Set debugging of the DWARF reader."), _("\
24641 Show debugging of the DWARF reader."), _("\
24642 When enabled (non-zero), debugging messages are printed during DWARF\n\
24643 reading and symtab expansion. A value of 1 (one) provides basic\n\
24644 information. A value greater than 1 provides more verbose information."),
24645 NULL,
24646 NULL,
24647 &setdebuglist, &showdebuglist);
24648
24649 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24650 Set debugging of the DWARF DIE reader."), _("\
24651 Show debugging of the DWARF DIE reader."), _("\
24652 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24653 The value is the maximum depth to print."),
24654 NULL,
24655 NULL,
24656 &setdebuglist, &showdebuglist);
24657
24658 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24659 Set debugging of the dwarf line reader."), _("\
24660 Show debugging of the dwarf line reader."), _("\
24661 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24662 A value of 1 (one) provides basic information.\n\
24663 A value greater than 1 provides more verbose information."),
24664 NULL,
24665 NULL,
24666 &setdebuglist, &showdebuglist);
24667
24668 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24669 Set cross-checking of \"physname\" code against demangler."), _("\
24670 Show cross-checking of \"physname\" code against demangler."), _("\
24671 When enabled, GDB's internal \"physname\" code is checked against\n\
24672 the demangler."),
24673 NULL, show_check_physname,
24674 &setdebuglist, &showdebuglist);
24675
24676 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24677 no_class, &use_deprecated_index_sections, _("\
24678 Set whether to use deprecated gdb_index sections."), _("\
24679 Show whether to use deprecated gdb_index sections."), _("\
24680 When enabled, deprecated .gdb_index sections are used anyway.\n\
24681 Normally they are ignored either because of a missing feature or\n\
24682 performance issue.\n\
24683 Warning: This option must be enabled before gdb reads the file."),
24684 NULL,
24685 NULL,
24686 &setlist, &showlist);
24687
24688 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24689 &dwarf2_locexpr_funcs);
24690 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24691 &dwarf2_loclist_funcs);
24692
24693 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24694 &dwarf2_block_frame_base_locexpr_funcs);
24695 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24696 &dwarf2_block_frame_base_loclist_funcs);
24697
24698 #if GDB_SELF_TEST
24699 selftests::register_test ("dw2_expand_symtabs_matching",
24700 selftests::dw2_expand_symtabs_matching::run_test);
24701 #endif
24702 }