Add dwz.c and dwz_file::read_string
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "bfd.h"
42 #include "elf-bfd.h"
43 #include "symtab.h"
44 #include "gdbtypes.h"
45 #include "objfiles.h"
46 #include "dwarf2.h"
47 #include "buildsym.h"
48 #include "demangle.h"
49 #include "gdb-demangle.h"
50 #include "filenames.h" /* for DOSish file names */
51 #include "macrotab.h"
52 #include "language.h"
53 #include "complaints.h"
54 #include "dwarf2/expr.h"
55 #include "dwarf2/loc.h"
56 #include "cp-support.h"
57 #include "hashtab.h"
58 #include "command.h"
59 #include "gdbcmd.h"
60 #include "block.h"
61 #include "addrmap.h"
62 #include "typeprint.h"
63 #include "psympriv.h"
64 #include "c-lang.h"
65 #include "go-lang.h"
66 #include "valprint.h"
67 #include "gdbcore.h" /* for gnutarget */
68 #include "gdb/gdb-index.h"
69 #include "gdb_bfd.h"
70 #include "f-lang.h"
71 #include "source.h"
72 #include "build-id.h"
73 #include "namespace.h"
74 #include "gdbsupport/function-view.h"
75 #include "gdbsupport/gdb_optional.h"
76 #include "gdbsupport/underlying.h"
77 #include "gdbsupport/hash_enum.h"
78 #include "filename-seen-cache.h"
79 #include "producer.h"
80 #include <fcntl.h>
81 #include <algorithm>
82 #include <unordered_map>
83 #include "gdbsupport/selftest.h"
84 #include "rust-lang.h"
85 #include "gdbsupport/pathstuff.h"
86 #include "count-one-bits.h"
87 #include "debuginfod-support.h"
88
89 /* When == 1, print basic high level tracing messages.
90 When > 1, be more verbose.
91 This is in contrast to the low level DIE reading of dwarf_die_debug. */
92 static unsigned int dwarf_read_debug = 0;
93
94 /* When non-zero, dump DIEs after they are read in. */
95 static unsigned int dwarf_die_debug = 0;
96
97 /* When non-zero, dump line number entries as they are read in. */
98 unsigned int dwarf_line_debug = 0;
99
100 /* When true, cross-check physname against demangler. */
101 static bool check_physname = false;
102
103 /* When true, do not reject deprecated .gdb_index sections. */
104 static bool use_deprecated_index_sections = false;
105
106 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
107
108 /* The "aclass" indices for various kinds of computed DWARF symbols. */
109
110 static int dwarf2_locexpr_index;
111 static int dwarf2_loclist_index;
112 static int dwarf2_locexpr_block_index;
113 static int dwarf2_loclist_block_index;
114
115 /* An index into a (C++) symbol name component in a symbol name as
116 recorded in the mapped_index's symbol table. For each C++ symbol
117 in the symbol table, we record one entry for the start of each
118 component in the symbol in a table of name components, and then
119 sort the table, in order to be able to binary search symbol names,
120 ignoring leading namespaces, both completion and regular look up.
121 For example, for symbol "A::B::C", we'll have an entry that points
122 to "A::B::C", another that points to "B::C", and another for "C".
123 Note that function symbols in GDB index have no parameter
124 information, just the function/method names. You can convert a
125 name_component to a "const char *" using the
126 'mapped_index::symbol_name_at(offset_type)' method. */
127
128 struct name_component
129 {
130 /* Offset in the symbol name where the component starts. Stored as
131 a (32-bit) offset instead of a pointer to save memory and improve
132 locality on 64-bit architectures. */
133 offset_type name_offset;
134
135 /* The symbol's index in the symbol and constant pool tables of a
136 mapped_index. */
137 offset_type idx;
138 };
139
140 /* Base class containing bits shared by both .gdb_index and
141 .debug_name indexes. */
142
143 struct mapped_index_base
144 {
145 mapped_index_base () = default;
146 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
147
148 /* The name_component table (a sorted vector). See name_component's
149 description above. */
150 std::vector<name_component> name_components;
151
152 /* How NAME_COMPONENTS is sorted. */
153 enum case_sensitivity name_components_casing;
154
155 /* Return the number of names in the symbol table. */
156 virtual size_t symbol_name_count () const = 0;
157
158 /* Get the name of the symbol at IDX in the symbol table. */
159 virtual const char *symbol_name_at (offset_type idx) const = 0;
160
161 /* Return whether the name at IDX in the symbol table should be
162 ignored. */
163 virtual bool symbol_name_slot_invalid (offset_type idx) const
164 {
165 return false;
166 }
167
168 /* Build the symbol name component sorted vector, if we haven't
169 yet. */
170 void build_name_components ();
171
172 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
173 possible matches for LN_NO_PARAMS in the name component
174 vector. */
175 std::pair<std::vector<name_component>::const_iterator,
176 std::vector<name_component>::const_iterator>
177 find_name_components_bounds (const lookup_name_info &ln_no_params,
178 enum language lang) const;
179
180 /* Prevent deleting/destroying via a base class pointer. */
181 protected:
182 ~mapped_index_base() = default;
183 };
184
185 /* A description of the mapped index. The file format is described in
186 a comment by the code that writes the index. */
187 struct mapped_index final : public mapped_index_base
188 {
189 /* A slot/bucket in the symbol table hash. */
190 struct symbol_table_slot
191 {
192 const offset_type name;
193 const offset_type vec;
194 };
195
196 /* Index data format version. */
197 int version = 0;
198
199 /* The address table data. */
200 gdb::array_view<const gdb_byte> address_table;
201
202 /* The symbol table, implemented as a hash table. */
203 gdb::array_view<symbol_table_slot> symbol_table;
204
205 /* A pointer to the constant pool. */
206 const char *constant_pool = nullptr;
207
208 bool symbol_name_slot_invalid (offset_type idx) const override
209 {
210 const auto &bucket = this->symbol_table[idx];
211 return bucket.name == 0 && bucket.vec == 0;
212 }
213
214 /* Convenience method to get at the name of the symbol at IDX in the
215 symbol table. */
216 const char *symbol_name_at (offset_type idx) const override
217 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
218
219 size_t symbol_name_count () const override
220 { return this->symbol_table.size (); }
221 };
222
223 /* A description of the mapped .debug_names.
224 Uninitialized map has CU_COUNT 0. */
225 struct mapped_debug_names final : public mapped_index_base
226 {
227 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
228 : dwarf2_per_objfile (dwarf2_per_objfile_)
229 {}
230
231 struct dwarf2_per_objfile *dwarf2_per_objfile;
232 bfd_endian dwarf5_byte_order;
233 bool dwarf5_is_dwarf64;
234 bool augmentation_is_gdb;
235 uint8_t offset_size;
236 uint32_t cu_count = 0;
237 uint32_t tu_count, bucket_count, name_count;
238 const gdb_byte *cu_table_reordered, *tu_table_reordered;
239 const uint32_t *bucket_table_reordered, *hash_table_reordered;
240 const gdb_byte *name_table_string_offs_reordered;
241 const gdb_byte *name_table_entry_offs_reordered;
242 const gdb_byte *entry_pool;
243
244 struct index_val
245 {
246 ULONGEST dwarf_tag;
247 struct attr
248 {
249 /* Attribute name DW_IDX_*. */
250 ULONGEST dw_idx;
251
252 /* Attribute form DW_FORM_*. */
253 ULONGEST form;
254
255 /* Value if FORM is DW_FORM_implicit_const. */
256 LONGEST implicit_const;
257 };
258 std::vector<attr> attr_vec;
259 };
260
261 std::unordered_map<ULONGEST, index_val> abbrev_map;
262
263 const char *namei_to_name (uint32_t namei) const;
264
265 /* Implementation of the mapped_index_base virtual interface, for
266 the name_components cache. */
267
268 const char *symbol_name_at (offset_type idx) const override
269 { return namei_to_name (idx); }
270
271 size_t symbol_name_count () const override
272 { return this->name_count; }
273 };
274
275 /* See dwarf2read.h. */
276
277 dwarf2_per_objfile *
278 get_dwarf2_per_objfile (struct objfile *objfile)
279 {
280 return dwarf2_objfile_data_key.get (objfile);
281 }
282
283 /* Default names of the debugging sections. */
284
285 /* Note that if the debugging section has been compressed, it might
286 have a name like .zdebug_info. */
287
288 static const struct dwarf2_debug_sections dwarf2_elf_names =
289 {
290 { ".debug_info", ".zdebug_info" },
291 { ".debug_abbrev", ".zdebug_abbrev" },
292 { ".debug_line", ".zdebug_line" },
293 { ".debug_loc", ".zdebug_loc" },
294 { ".debug_loclists", ".zdebug_loclists" },
295 { ".debug_macinfo", ".zdebug_macinfo" },
296 { ".debug_macro", ".zdebug_macro" },
297 { ".debug_str", ".zdebug_str" },
298 { ".debug_str_offsets", ".zdebug_str_offsets" },
299 { ".debug_line_str", ".zdebug_line_str" },
300 { ".debug_ranges", ".zdebug_ranges" },
301 { ".debug_rnglists", ".zdebug_rnglists" },
302 { ".debug_types", ".zdebug_types" },
303 { ".debug_addr", ".zdebug_addr" },
304 { ".debug_frame", ".zdebug_frame" },
305 { ".eh_frame", NULL },
306 { ".gdb_index", ".zgdb_index" },
307 { ".debug_names", ".zdebug_names" },
308 { ".debug_aranges", ".zdebug_aranges" },
309 23
310 };
311
312 /* List of DWO/DWP sections. */
313
314 static const struct dwop_section_names
315 {
316 struct dwarf2_section_names abbrev_dwo;
317 struct dwarf2_section_names info_dwo;
318 struct dwarf2_section_names line_dwo;
319 struct dwarf2_section_names loc_dwo;
320 struct dwarf2_section_names loclists_dwo;
321 struct dwarf2_section_names macinfo_dwo;
322 struct dwarf2_section_names macro_dwo;
323 struct dwarf2_section_names str_dwo;
324 struct dwarf2_section_names str_offsets_dwo;
325 struct dwarf2_section_names types_dwo;
326 struct dwarf2_section_names cu_index;
327 struct dwarf2_section_names tu_index;
328 }
329 dwop_section_names =
330 {
331 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
332 { ".debug_info.dwo", ".zdebug_info.dwo" },
333 { ".debug_line.dwo", ".zdebug_line.dwo" },
334 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
335 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
336 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
337 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
338 { ".debug_str.dwo", ".zdebug_str.dwo" },
339 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
340 { ".debug_types.dwo", ".zdebug_types.dwo" },
341 { ".debug_cu_index", ".zdebug_cu_index" },
342 { ".debug_tu_index", ".zdebug_tu_index" },
343 };
344
345 /* local data types */
346
347 /* Type used for delaying computation of method physnames.
348 See comments for compute_delayed_physnames. */
349 struct delayed_method_info
350 {
351 /* The type to which the method is attached, i.e., its parent class. */
352 struct type *type;
353
354 /* The index of the method in the type's function fieldlists. */
355 int fnfield_index;
356
357 /* The index of the method in the fieldlist. */
358 int index;
359
360 /* The name of the DIE. */
361 const char *name;
362
363 /* The DIE associated with this method. */
364 struct die_info *die;
365 };
366
367 /* Internal state when decoding a particular compilation unit. */
368 struct dwarf2_cu
369 {
370 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
371 ~dwarf2_cu ();
372
373 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
374
375 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
376 Create the set of symtabs used by this TU, or if this TU is sharing
377 symtabs with another TU and the symtabs have already been created
378 then restore those symtabs in the line header.
379 We don't need the pc/line-number mapping for type units. */
380 void setup_type_unit_groups (struct die_info *die);
381
382 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
383 buildsym_compunit constructor. */
384 struct compunit_symtab *start_symtab (const char *name,
385 const char *comp_dir,
386 CORE_ADDR low_pc);
387
388 /* Reset the builder. */
389 void reset_builder () { m_builder.reset (); }
390
391 /* The header of the compilation unit. */
392 struct comp_unit_head header {};
393
394 /* Base address of this compilation unit. */
395 CORE_ADDR base_address = 0;
396
397 /* Non-zero if base_address has been set. */
398 int base_known = 0;
399
400 /* The language we are debugging. */
401 enum language language = language_unknown;
402 const struct language_defn *language_defn = nullptr;
403
404 const char *producer = nullptr;
405
406 private:
407 /* The symtab builder for this CU. This is only non-NULL when full
408 symbols are being read. */
409 std::unique_ptr<buildsym_compunit> m_builder;
410
411 public:
412 /* The generic symbol table building routines have separate lists for
413 file scope symbols and all all other scopes (local scopes). So
414 we need to select the right one to pass to add_symbol_to_list().
415 We do it by keeping a pointer to the correct list in list_in_scope.
416
417 FIXME: The original dwarf code just treated the file scope as the
418 first local scope, and all other local scopes as nested local
419 scopes, and worked fine. Check to see if we really need to
420 distinguish these in buildsym.c. */
421 struct pending **list_in_scope = nullptr;
422
423 /* Hash table holding all the loaded partial DIEs
424 with partial_die->offset.SECT_OFF as hash. */
425 htab_t partial_dies = nullptr;
426
427 /* Storage for things with the same lifetime as this read-in compilation
428 unit, including partial DIEs. */
429 auto_obstack comp_unit_obstack;
430
431 /* When multiple dwarf2_cu structures are living in memory, this field
432 chains them all together, so that they can be released efficiently.
433 We will probably also want a generation counter so that most-recently-used
434 compilation units are cached... */
435 struct dwarf2_per_cu_data *read_in_chain = nullptr;
436
437 /* Backlink to our per_cu entry. */
438 struct dwarf2_per_cu_data *per_cu;
439
440 /* How many compilation units ago was this CU last referenced? */
441 int last_used = 0;
442
443 /* A hash table of DIE cu_offset for following references with
444 die_info->offset.sect_off as hash. */
445 htab_t die_hash = nullptr;
446
447 /* Full DIEs if read in. */
448 struct die_info *dies = nullptr;
449
450 /* A set of pointers to dwarf2_per_cu_data objects for compilation
451 units referenced by this one. Only set during full symbol processing;
452 partial symbol tables do not have dependencies. */
453 htab_t dependencies = nullptr;
454
455 /* Header data from the line table, during full symbol processing. */
456 struct line_header *line_header = nullptr;
457 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
458 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
459 this is the DW_TAG_compile_unit die for this CU. We'll hold on
460 to the line header as long as this DIE is being processed. See
461 process_die_scope. */
462 die_info *line_header_die_owner = nullptr;
463
464 /* A list of methods which need to have physnames computed
465 after all type information has been read. */
466 std::vector<delayed_method_info> method_list;
467
468 /* To be copied to symtab->call_site_htab. */
469 htab_t call_site_htab = nullptr;
470
471 /* Non-NULL if this CU came from a DWO file.
472 There is an invariant here that is important to remember:
473 Except for attributes copied from the top level DIE in the "main"
474 (or "stub") file in preparation for reading the DWO file
475 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
476 Either there isn't a DWO file (in which case this is NULL and the point
477 is moot), or there is and either we're not going to read it (in which
478 case this is NULL) or there is and we are reading it (in which case this
479 is non-NULL). */
480 struct dwo_unit *dwo_unit = nullptr;
481
482 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
483 Note this value comes from the Fission stub CU/TU's DIE. */
484 gdb::optional<ULONGEST> addr_base;
485
486 /* The DW_AT_rnglists_base attribute if present.
487 Note this value comes from the Fission stub CU/TU's DIE.
488 Also note that the value is zero in the non-DWO case so this value can
489 be used without needing to know whether DWO files are in use or not.
490 N.B. This does not apply to DW_AT_ranges appearing in
491 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
492 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
493 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
494 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
495 ULONGEST ranges_base = 0;
496
497 /* When reading debug info generated by older versions of rustc, we
498 have to rewrite some union types to be struct types with a
499 variant part. This rewriting must be done after the CU is fully
500 read in, because otherwise at the point of rewriting some struct
501 type might not have been fully processed. So, we keep a list of
502 all such types here and process them after expansion. */
503 std::vector<struct type *> rust_unions;
504
505 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
506 files, the value is implicitly zero. For DWARF 5 version DWO files, the
507 value is often implicit and is the size of the header of
508 .debug_str_offsets section (8 or 4, depending on the address size). */
509 gdb::optional<ULONGEST> str_offsets_base;
510
511 /* Mark used when releasing cached dies. */
512 bool mark : 1;
513
514 /* This CU references .debug_loc. See the symtab->locations_valid field.
515 This test is imperfect as there may exist optimized debug code not using
516 any location list and still facing inlining issues if handled as
517 unoptimized code. For a future better test see GCC PR other/32998. */
518 bool has_loclist : 1;
519
520 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
521 if all the producer_is_* fields are valid. This information is cached
522 because profiling CU expansion showed excessive time spent in
523 producer_is_gxx_lt_4_6. */
524 bool checked_producer : 1;
525 bool producer_is_gxx_lt_4_6 : 1;
526 bool producer_is_gcc_lt_4_3 : 1;
527 bool producer_is_icc : 1;
528 bool producer_is_icc_lt_14 : 1;
529 bool producer_is_codewarrior : 1;
530
531 /* When true, the file that we're processing is known to have
532 debugging info for C++ namespaces. GCC 3.3.x did not produce
533 this information, but later versions do. */
534
535 bool processing_has_namespace_info : 1;
536
537 struct partial_die_info *find_partial_die (sect_offset sect_off);
538
539 /* If this CU was inherited by another CU (via specification,
540 abstract_origin, etc), this is the ancestor CU. */
541 dwarf2_cu *ancestor;
542
543 /* Get the buildsym_compunit for this CU. */
544 buildsym_compunit *get_builder ()
545 {
546 /* If this CU has a builder associated with it, use that. */
547 if (m_builder != nullptr)
548 return m_builder.get ();
549
550 /* Otherwise, search ancestors for a valid builder. */
551 if (ancestor != nullptr)
552 return ancestor->get_builder ();
553
554 return nullptr;
555 }
556 };
557
558 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
559 This includes type_unit_group and quick_file_names. */
560
561 struct stmt_list_hash
562 {
563 /* The DWO unit this table is from or NULL if there is none. */
564 struct dwo_unit *dwo_unit;
565
566 /* Offset in .debug_line or .debug_line.dwo. */
567 sect_offset line_sect_off;
568 };
569
570 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
571 an object of this type. */
572
573 struct type_unit_group
574 {
575 /* dwarf2read.c's main "handle" on a TU symtab.
576 To simplify things we create an artificial CU that "includes" all the
577 type units using this stmt_list so that the rest of the code still has
578 a "per_cu" handle on the symtab. */
579 struct dwarf2_per_cu_data per_cu;
580
581 /* The TUs that share this DW_AT_stmt_list entry.
582 This is added to while parsing type units to build partial symtabs,
583 and is deleted afterwards and not used again. */
584 std::vector<signatured_type *> *tus;
585
586 /* The compunit symtab.
587 Type units in a group needn't all be defined in the same source file,
588 so we create an essentially anonymous symtab as the compunit symtab. */
589 struct compunit_symtab *compunit_symtab;
590
591 /* The data used to construct the hash key. */
592 struct stmt_list_hash hash;
593
594 /* The symbol tables for this TU (obtained from the files listed in
595 DW_AT_stmt_list).
596 WARNING: The order of entries here must match the order of entries
597 in the line header. After the first TU using this type_unit_group, the
598 line header for the subsequent TUs is recreated from this. This is done
599 because we need to use the same symtabs for each TU using the same
600 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
601 there's no guarantee the line header doesn't have duplicate entries. */
602 struct symtab **symtabs;
603 };
604
605 /* These sections are what may appear in a (real or virtual) DWO file. */
606
607 struct dwo_sections
608 {
609 struct dwarf2_section_info abbrev;
610 struct dwarf2_section_info line;
611 struct dwarf2_section_info loc;
612 struct dwarf2_section_info loclists;
613 struct dwarf2_section_info macinfo;
614 struct dwarf2_section_info macro;
615 struct dwarf2_section_info str;
616 struct dwarf2_section_info str_offsets;
617 /* In the case of a virtual DWO file, these two are unused. */
618 struct dwarf2_section_info info;
619 std::vector<dwarf2_section_info> types;
620 };
621
622 /* CUs/TUs in DWP/DWO files. */
623
624 struct dwo_unit
625 {
626 /* Backlink to the containing struct dwo_file. */
627 struct dwo_file *dwo_file;
628
629 /* The "id" that distinguishes this CU/TU.
630 .debug_info calls this "dwo_id", .debug_types calls this "signature".
631 Since signatures came first, we stick with it for consistency. */
632 ULONGEST signature;
633
634 /* The section this CU/TU lives in, in the DWO file. */
635 struct dwarf2_section_info *section;
636
637 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
638 sect_offset sect_off;
639 unsigned int length;
640
641 /* For types, offset in the type's DIE of the type defined by this TU. */
642 cu_offset type_offset_in_tu;
643 };
644
645 /* include/dwarf2.h defines the DWP section codes.
646 It defines a max value but it doesn't define a min value, which we
647 use for error checking, so provide one. */
648
649 enum dwp_v2_section_ids
650 {
651 DW_SECT_MIN = 1
652 };
653
654 /* Data for one DWO file.
655
656 This includes virtual DWO files (a virtual DWO file is a DWO file as it
657 appears in a DWP file). DWP files don't really have DWO files per se -
658 comdat folding of types "loses" the DWO file they came from, and from
659 a high level view DWP files appear to contain a mass of random types.
660 However, to maintain consistency with the non-DWP case we pretend DWP
661 files contain virtual DWO files, and we assign each TU with one virtual
662 DWO file (generally based on the line and abbrev section offsets -
663 a heuristic that seems to work in practice). */
664
665 struct dwo_file
666 {
667 dwo_file () = default;
668 DISABLE_COPY_AND_ASSIGN (dwo_file);
669
670 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
671 For virtual DWO files the name is constructed from the section offsets
672 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
673 from related CU+TUs. */
674 const char *dwo_name = nullptr;
675
676 /* The DW_AT_comp_dir attribute. */
677 const char *comp_dir = nullptr;
678
679 /* The bfd, when the file is open. Otherwise this is NULL.
680 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
681 gdb_bfd_ref_ptr dbfd;
682
683 /* The sections that make up this DWO file.
684 Remember that for virtual DWO files in DWP V2, these are virtual
685 sections (for lack of a better name). */
686 struct dwo_sections sections {};
687
688 /* The CUs in the file.
689 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
690 an extension to handle LLVM's Link Time Optimization output (where
691 multiple source files may be compiled into a single object/dwo pair). */
692 htab_up cus;
693
694 /* Table of TUs in the file.
695 Each element is a struct dwo_unit. */
696 htab_up tus;
697 };
698
699 /* These sections are what may appear in a DWP file. */
700
701 struct dwp_sections
702 {
703 /* These are used by both DWP version 1 and 2. */
704 struct dwarf2_section_info str;
705 struct dwarf2_section_info cu_index;
706 struct dwarf2_section_info tu_index;
707
708 /* These are only used by DWP version 2 files.
709 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
710 sections are referenced by section number, and are not recorded here.
711 In DWP version 2 there is at most one copy of all these sections, each
712 section being (effectively) comprised of the concatenation of all of the
713 individual sections that exist in the version 1 format.
714 To keep the code simple we treat each of these concatenated pieces as a
715 section itself (a virtual section?). */
716 struct dwarf2_section_info abbrev;
717 struct dwarf2_section_info info;
718 struct dwarf2_section_info line;
719 struct dwarf2_section_info loc;
720 struct dwarf2_section_info macinfo;
721 struct dwarf2_section_info macro;
722 struct dwarf2_section_info str_offsets;
723 struct dwarf2_section_info types;
724 };
725
726 /* These sections are what may appear in a virtual DWO file in DWP version 1.
727 A virtual DWO file is a DWO file as it appears in a DWP file. */
728
729 struct virtual_v1_dwo_sections
730 {
731 struct dwarf2_section_info abbrev;
732 struct dwarf2_section_info line;
733 struct dwarf2_section_info loc;
734 struct dwarf2_section_info macinfo;
735 struct dwarf2_section_info macro;
736 struct dwarf2_section_info str_offsets;
737 /* Each DWP hash table entry records one CU or one TU.
738 That is recorded here, and copied to dwo_unit.section. */
739 struct dwarf2_section_info info_or_types;
740 };
741
742 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
743 In version 2, the sections of the DWO files are concatenated together
744 and stored in one section of that name. Thus each ELF section contains
745 several "virtual" sections. */
746
747 struct virtual_v2_dwo_sections
748 {
749 bfd_size_type abbrev_offset;
750 bfd_size_type abbrev_size;
751
752 bfd_size_type line_offset;
753 bfd_size_type line_size;
754
755 bfd_size_type loc_offset;
756 bfd_size_type loc_size;
757
758 bfd_size_type macinfo_offset;
759 bfd_size_type macinfo_size;
760
761 bfd_size_type macro_offset;
762 bfd_size_type macro_size;
763
764 bfd_size_type str_offsets_offset;
765 bfd_size_type str_offsets_size;
766
767 /* Each DWP hash table entry records one CU or one TU.
768 That is recorded here, and copied to dwo_unit.section. */
769 bfd_size_type info_or_types_offset;
770 bfd_size_type info_or_types_size;
771 };
772
773 /* Contents of DWP hash tables. */
774
775 struct dwp_hash_table
776 {
777 uint32_t version, nr_columns;
778 uint32_t nr_units, nr_slots;
779 const gdb_byte *hash_table, *unit_table;
780 union
781 {
782 struct
783 {
784 const gdb_byte *indices;
785 } v1;
786 struct
787 {
788 /* This is indexed by column number and gives the id of the section
789 in that column. */
790 #define MAX_NR_V2_DWO_SECTIONS \
791 (1 /* .debug_info or .debug_types */ \
792 + 1 /* .debug_abbrev */ \
793 + 1 /* .debug_line */ \
794 + 1 /* .debug_loc */ \
795 + 1 /* .debug_str_offsets */ \
796 + 1 /* .debug_macro or .debug_macinfo */)
797 int section_ids[MAX_NR_V2_DWO_SECTIONS];
798 const gdb_byte *offsets;
799 const gdb_byte *sizes;
800 } v2;
801 } section_pool;
802 };
803
804 /* Data for one DWP file. */
805
806 struct dwp_file
807 {
808 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
809 : name (name_),
810 dbfd (std::move (abfd))
811 {
812 }
813
814 /* Name of the file. */
815 const char *name;
816
817 /* File format version. */
818 int version = 0;
819
820 /* The bfd. */
821 gdb_bfd_ref_ptr dbfd;
822
823 /* Section info for this file. */
824 struct dwp_sections sections {};
825
826 /* Table of CUs in the file. */
827 const struct dwp_hash_table *cus = nullptr;
828
829 /* Table of TUs in the file. */
830 const struct dwp_hash_table *tus = nullptr;
831
832 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
833 htab_up loaded_cus;
834 htab_up loaded_tus;
835
836 /* Table to map ELF section numbers to their sections.
837 This is only needed for the DWP V1 file format. */
838 unsigned int num_sections = 0;
839 asection **elf_sections = nullptr;
840 };
841
842 /* Struct used to pass misc. parameters to read_die_and_children, et
843 al. which are used for both .debug_info and .debug_types dies.
844 All parameters here are unchanging for the life of the call. This
845 struct exists to abstract away the constant parameters of die reading. */
846
847 struct die_reader_specs
848 {
849 /* The bfd of die_section. */
850 bfd* abfd;
851
852 /* The CU of the DIE we are parsing. */
853 struct dwarf2_cu *cu;
854
855 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
856 struct dwo_file *dwo_file;
857
858 /* The section the die comes from.
859 This is either .debug_info or .debug_types, or the .dwo variants. */
860 struct dwarf2_section_info *die_section;
861
862 /* die_section->buffer. */
863 const gdb_byte *buffer;
864
865 /* The end of the buffer. */
866 const gdb_byte *buffer_end;
867
868 /* The abbreviation table to use when reading the DIEs. */
869 struct abbrev_table *abbrev_table;
870 };
871
872 /* A subclass of die_reader_specs that holds storage and has complex
873 constructor and destructor behavior. */
874
875 class cutu_reader : public die_reader_specs
876 {
877 public:
878
879 cutu_reader (struct dwarf2_per_cu_data *this_cu,
880 struct abbrev_table *abbrev_table,
881 int use_existing_cu,
882 bool skip_partial);
883
884 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
885 struct dwarf2_cu *parent_cu = nullptr,
886 struct dwo_file *dwo_file = nullptr);
887
888 DISABLE_COPY_AND_ASSIGN (cutu_reader);
889
890 const gdb_byte *info_ptr = nullptr;
891 struct die_info *comp_unit_die = nullptr;
892 bool dummy_p = false;
893
894 /* Release the new CU, putting it on the chain. This cannot be done
895 for dummy CUs. */
896 void keep ();
897
898 private:
899 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
900 int use_existing_cu);
901
902 struct dwarf2_per_cu_data *m_this_cu;
903 std::unique_ptr<dwarf2_cu> m_new_cu;
904
905 /* The ordinary abbreviation table. */
906 abbrev_table_up m_abbrev_table_holder;
907
908 /* The DWO abbreviation table. */
909 abbrev_table_up m_dwo_abbrev_table;
910 };
911
912 /* When we construct a partial symbol table entry we only
913 need this much information. */
914 struct partial_die_info : public allocate_on_obstack
915 {
916 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
917
918 /* Disable assign but still keep copy ctor, which is needed
919 load_partial_dies. */
920 partial_die_info& operator=(const partial_die_info& rhs) = delete;
921
922 /* Adjust the partial die before generating a symbol for it. This
923 function may set the is_external flag or change the DIE's
924 name. */
925 void fixup (struct dwarf2_cu *cu);
926
927 /* Read a minimal amount of information into the minimal die
928 structure. */
929 const gdb_byte *read (const struct die_reader_specs *reader,
930 const struct abbrev_info &abbrev,
931 const gdb_byte *info_ptr);
932
933 /* Offset of this DIE. */
934 const sect_offset sect_off;
935
936 /* DWARF-2 tag for this DIE. */
937 const ENUM_BITFIELD(dwarf_tag) tag : 16;
938
939 /* Assorted flags describing the data found in this DIE. */
940 const unsigned int has_children : 1;
941
942 unsigned int is_external : 1;
943 unsigned int is_declaration : 1;
944 unsigned int has_type : 1;
945 unsigned int has_specification : 1;
946 unsigned int has_pc_info : 1;
947 unsigned int may_be_inlined : 1;
948
949 /* This DIE has been marked DW_AT_main_subprogram. */
950 unsigned int main_subprogram : 1;
951
952 /* Flag set if the SCOPE field of this structure has been
953 computed. */
954 unsigned int scope_set : 1;
955
956 /* Flag set if the DIE has a byte_size attribute. */
957 unsigned int has_byte_size : 1;
958
959 /* Flag set if the DIE has a DW_AT_const_value attribute. */
960 unsigned int has_const_value : 1;
961
962 /* Flag set if any of the DIE's children are template arguments. */
963 unsigned int has_template_arguments : 1;
964
965 /* Flag set if fixup has been called on this die. */
966 unsigned int fixup_called : 1;
967
968 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
969 unsigned int is_dwz : 1;
970
971 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
972 unsigned int spec_is_dwz : 1;
973
974 /* The name of this DIE. Normally the value of DW_AT_name, but
975 sometimes a default name for unnamed DIEs. */
976 const char *name = nullptr;
977
978 /* The linkage name, if present. */
979 const char *linkage_name = nullptr;
980
981 /* The scope to prepend to our children. This is generally
982 allocated on the comp_unit_obstack, so will disappear
983 when this compilation unit leaves the cache. */
984 const char *scope = nullptr;
985
986 /* Some data associated with the partial DIE. The tag determines
987 which field is live. */
988 union
989 {
990 /* The location description associated with this DIE, if any. */
991 struct dwarf_block *locdesc;
992 /* The offset of an import, for DW_TAG_imported_unit. */
993 sect_offset sect_off;
994 } d {};
995
996 /* If HAS_PC_INFO, the PC range associated with this DIE. */
997 CORE_ADDR lowpc = 0;
998 CORE_ADDR highpc = 0;
999
1000 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1001 DW_AT_sibling, if any. */
1002 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1003 could return DW_AT_sibling values to its caller load_partial_dies. */
1004 const gdb_byte *sibling = nullptr;
1005
1006 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1007 DW_AT_specification (or DW_AT_abstract_origin or
1008 DW_AT_extension). */
1009 sect_offset spec_offset {};
1010
1011 /* Pointers to this DIE's parent, first child, and next sibling,
1012 if any. */
1013 struct partial_die_info *die_parent = nullptr;
1014 struct partial_die_info *die_child = nullptr;
1015 struct partial_die_info *die_sibling = nullptr;
1016
1017 friend struct partial_die_info *
1018 dwarf2_cu::find_partial_die (sect_offset sect_off);
1019
1020 private:
1021 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1022 partial_die_info (sect_offset sect_off)
1023 : partial_die_info (sect_off, DW_TAG_padding, 0)
1024 {
1025 }
1026
1027 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1028 int has_children_)
1029 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1030 {
1031 is_external = 0;
1032 is_declaration = 0;
1033 has_type = 0;
1034 has_specification = 0;
1035 has_pc_info = 0;
1036 may_be_inlined = 0;
1037 main_subprogram = 0;
1038 scope_set = 0;
1039 has_byte_size = 0;
1040 has_const_value = 0;
1041 has_template_arguments = 0;
1042 fixup_called = 0;
1043 is_dwz = 0;
1044 spec_is_dwz = 0;
1045 }
1046 };
1047
1048 /* This data structure holds a complete die structure. */
1049 struct die_info
1050 {
1051 /* DWARF-2 tag for this DIE. */
1052 ENUM_BITFIELD(dwarf_tag) tag : 16;
1053
1054 /* Number of attributes */
1055 unsigned char num_attrs;
1056
1057 /* True if we're presently building the full type name for the
1058 type derived from this DIE. */
1059 unsigned char building_fullname : 1;
1060
1061 /* True if this die is in process. PR 16581. */
1062 unsigned char in_process : 1;
1063
1064 /* True if this DIE has children. */
1065 unsigned char has_children : 1;
1066
1067 /* Abbrev number */
1068 unsigned int abbrev;
1069
1070 /* Offset in .debug_info or .debug_types section. */
1071 sect_offset sect_off;
1072
1073 /* The dies in a compilation unit form an n-ary tree. PARENT
1074 points to this die's parent; CHILD points to the first child of
1075 this node; and all the children of a given node are chained
1076 together via their SIBLING fields. */
1077 struct die_info *child; /* Its first child, if any. */
1078 struct die_info *sibling; /* Its next sibling, if any. */
1079 struct die_info *parent; /* Its parent, if any. */
1080
1081 /* An array of attributes, with NUM_ATTRS elements. There may be
1082 zero, but it's not common and zero-sized arrays are not
1083 sufficiently portable C. */
1084 struct attribute attrs[1];
1085 };
1086
1087 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1088 but this would require a corresponding change in unpack_field_as_long
1089 and friends. */
1090 static int bits_per_byte = 8;
1091
1092 /* When reading a variant or variant part, we track a bit more
1093 information about the field, and store it in an object of this
1094 type. */
1095
1096 struct variant_field
1097 {
1098 /* If we see a DW_TAG_variant, then this will be the discriminant
1099 value. */
1100 ULONGEST discriminant_value;
1101 /* If we see a DW_TAG_variant, then this will be set if this is the
1102 default branch. */
1103 bool default_branch;
1104 /* While reading a DW_TAG_variant_part, this will be set if this
1105 field is the discriminant. */
1106 bool is_discriminant;
1107 };
1108
1109 struct nextfield
1110 {
1111 int accessibility = 0;
1112 int virtuality = 0;
1113 /* Extra information to describe a variant or variant part. */
1114 struct variant_field variant {};
1115 struct field field {};
1116 };
1117
1118 struct fnfieldlist
1119 {
1120 const char *name = nullptr;
1121 std::vector<struct fn_field> fnfields;
1122 };
1123
1124 /* The routines that read and process dies for a C struct or C++ class
1125 pass lists of data member fields and lists of member function fields
1126 in an instance of a field_info structure, as defined below. */
1127 struct field_info
1128 {
1129 /* List of data member and baseclasses fields. */
1130 std::vector<struct nextfield> fields;
1131 std::vector<struct nextfield> baseclasses;
1132
1133 /* Set if the accessibility of one of the fields is not public. */
1134 int non_public_fields = 0;
1135
1136 /* Member function fieldlist array, contains name of possibly overloaded
1137 member function, number of overloaded member functions and a pointer
1138 to the head of the member function field chain. */
1139 std::vector<struct fnfieldlist> fnfieldlists;
1140
1141 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1142 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1143 std::vector<struct decl_field> typedef_field_list;
1144
1145 /* Nested types defined by this class and the number of elements in this
1146 list. */
1147 std::vector<struct decl_field> nested_types_list;
1148
1149 /* Return the total number of fields (including baseclasses). */
1150 int nfields () const
1151 {
1152 return fields.size () + baseclasses.size ();
1153 }
1154 };
1155
1156 /* Loaded secondary compilation units are kept in memory until they
1157 have not been referenced for the processing of this many
1158 compilation units. Set this to zero to disable caching. Cache
1159 sizes of up to at least twenty will improve startup time for
1160 typical inter-CU-reference binaries, at an obvious memory cost. */
1161 static int dwarf_max_cache_age = 5;
1162 static void
1163 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1164 struct cmd_list_element *c, const char *value)
1165 {
1166 fprintf_filtered (file, _("The upper bound on the age of cached "
1167 "DWARF compilation units is %s.\n"),
1168 value);
1169 }
1170 \f
1171 /* local function prototypes */
1172
1173 static void dwarf2_find_base_address (struct die_info *die,
1174 struct dwarf2_cu *cu);
1175
1176 static dwarf2_psymtab *create_partial_symtab
1177 (struct dwarf2_per_cu_data *per_cu, const char *name);
1178
1179 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1180 const gdb_byte *info_ptr,
1181 struct die_info *type_unit_die);
1182
1183 static void dwarf2_build_psymtabs_hard
1184 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1185
1186 static void scan_partial_symbols (struct partial_die_info *,
1187 CORE_ADDR *, CORE_ADDR *,
1188 int, struct dwarf2_cu *);
1189
1190 static void add_partial_symbol (struct partial_die_info *,
1191 struct dwarf2_cu *);
1192
1193 static void add_partial_namespace (struct partial_die_info *pdi,
1194 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1195 int set_addrmap, struct dwarf2_cu *cu);
1196
1197 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1198 CORE_ADDR *highpc, int set_addrmap,
1199 struct dwarf2_cu *cu);
1200
1201 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1202 struct dwarf2_cu *cu);
1203
1204 static void add_partial_subprogram (struct partial_die_info *pdi,
1205 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1206 int need_pc, struct dwarf2_cu *cu);
1207
1208 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1209
1210 static struct partial_die_info *load_partial_dies
1211 (const struct die_reader_specs *, const gdb_byte *, int);
1212
1213 /* A pair of partial_die_info and compilation unit. */
1214 struct cu_partial_die_info
1215 {
1216 /* The compilation unit of the partial_die_info. */
1217 struct dwarf2_cu *cu;
1218 /* A partial_die_info. */
1219 struct partial_die_info *pdi;
1220
1221 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1222 : cu (cu),
1223 pdi (pdi)
1224 { /* Nothing. */ }
1225
1226 private:
1227 cu_partial_die_info () = delete;
1228 };
1229
1230 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1231 struct dwarf2_cu *);
1232
1233 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1234 struct attribute *, struct attr_abbrev *,
1235 const gdb_byte *, bool *need_reprocess);
1236
1237 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1238 struct attribute *attr);
1239
1240 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1241
1242 static LONGEST read_checked_initial_length_and_offset
1243 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1244 unsigned int *, unsigned int *);
1245
1246 static sect_offset read_abbrev_offset
1247 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1248 struct dwarf2_section_info *, sect_offset);
1249
1250 static const char *read_indirect_string
1251 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1252 const struct comp_unit_head *, unsigned int *);
1253
1254 static const char *read_indirect_line_string
1255 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1256 const struct comp_unit_head *, unsigned int *);
1257
1258 static const char *read_indirect_string_at_offset
1259 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1260 LONGEST str_offset);
1261
1262 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1263 const gdb_byte *,
1264 unsigned int *);
1265
1266 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1267 ULONGEST str_index);
1268
1269 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1270 ULONGEST str_index);
1271
1272 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1273
1274 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1275 struct dwarf2_cu *);
1276
1277 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1278 unsigned int);
1279
1280 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1281 struct dwarf2_cu *cu);
1282
1283 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1284
1285 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1286 struct dwarf2_cu *cu);
1287
1288 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1289
1290 static struct die_info *die_specification (struct die_info *die,
1291 struct dwarf2_cu **);
1292
1293 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1294 struct dwarf2_cu *cu);
1295
1296 static void dwarf_decode_lines (struct line_header *, const char *,
1297 struct dwarf2_cu *, dwarf2_psymtab *,
1298 CORE_ADDR, int decode_mapping);
1299
1300 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1301 const char *);
1302
1303 static struct symbol *new_symbol (struct die_info *, struct type *,
1304 struct dwarf2_cu *, struct symbol * = NULL);
1305
1306 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1307 struct dwarf2_cu *);
1308
1309 static void dwarf2_const_value_attr (const struct attribute *attr,
1310 struct type *type,
1311 const char *name,
1312 struct obstack *obstack,
1313 struct dwarf2_cu *cu, LONGEST *value,
1314 const gdb_byte **bytes,
1315 struct dwarf2_locexpr_baton **baton);
1316
1317 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1318
1319 static int need_gnat_info (struct dwarf2_cu *);
1320
1321 static struct type *die_descriptive_type (struct die_info *,
1322 struct dwarf2_cu *);
1323
1324 static void set_descriptive_type (struct type *, struct die_info *,
1325 struct dwarf2_cu *);
1326
1327 static struct type *die_containing_type (struct die_info *,
1328 struct dwarf2_cu *);
1329
1330 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1331 struct dwarf2_cu *);
1332
1333 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1334
1335 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1336
1337 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1338
1339 static char *typename_concat (struct obstack *obs, const char *prefix,
1340 const char *suffix, int physname,
1341 struct dwarf2_cu *cu);
1342
1343 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1344
1345 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1346
1347 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1348
1349 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1350
1351 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1352
1353 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1354
1355 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1356 struct dwarf2_cu *, dwarf2_psymtab *);
1357
1358 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1359 values. Keep the items ordered with increasing constraints compliance. */
1360 enum pc_bounds_kind
1361 {
1362 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1363 PC_BOUNDS_NOT_PRESENT,
1364
1365 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1366 were present but they do not form a valid range of PC addresses. */
1367 PC_BOUNDS_INVALID,
1368
1369 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1370 PC_BOUNDS_RANGES,
1371
1372 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1373 PC_BOUNDS_HIGH_LOW,
1374 };
1375
1376 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1377 CORE_ADDR *, CORE_ADDR *,
1378 struct dwarf2_cu *,
1379 dwarf2_psymtab *);
1380
1381 static void get_scope_pc_bounds (struct die_info *,
1382 CORE_ADDR *, CORE_ADDR *,
1383 struct dwarf2_cu *);
1384
1385 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1386 CORE_ADDR, struct dwarf2_cu *);
1387
1388 static void dwarf2_add_field (struct field_info *, struct die_info *,
1389 struct dwarf2_cu *);
1390
1391 static void dwarf2_attach_fields_to_type (struct field_info *,
1392 struct type *, struct dwarf2_cu *);
1393
1394 static void dwarf2_add_member_fn (struct field_info *,
1395 struct die_info *, struct type *,
1396 struct dwarf2_cu *);
1397
1398 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1399 struct type *,
1400 struct dwarf2_cu *);
1401
1402 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1403
1404 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1405
1406 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1407
1408 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1409
1410 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1411
1412 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1413
1414 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1415
1416 static struct type *read_module_type (struct die_info *die,
1417 struct dwarf2_cu *cu);
1418
1419 static const char *namespace_name (struct die_info *die,
1420 int *is_anonymous, struct dwarf2_cu *);
1421
1422 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1423
1424 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1425
1426 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1427 struct dwarf2_cu *);
1428
1429 static struct die_info *read_die_and_siblings_1
1430 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1431 struct die_info *);
1432
1433 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1434 const gdb_byte *info_ptr,
1435 const gdb_byte **new_info_ptr,
1436 struct die_info *parent);
1437
1438 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1439 struct die_info **, const gdb_byte *,
1440 int);
1441
1442 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1443 struct die_info **, const gdb_byte *);
1444
1445 static void process_die (struct die_info *, struct dwarf2_cu *);
1446
1447 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1448 struct objfile *);
1449
1450 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1451
1452 static const char *dwarf2_full_name (const char *name,
1453 struct die_info *die,
1454 struct dwarf2_cu *cu);
1455
1456 static const char *dwarf2_physname (const char *name, struct die_info *die,
1457 struct dwarf2_cu *cu);
1458
1459 static struct die_info *dwarf2_extension (struct die_info *die,
1460 struct dwarf2_cu **);
1461
1462 static const char *dwarf_tag_name (unsigned int);
1463
1464 static const char *dwarf_attr_name (unsigned int);
1465
1466 static const char *dwarf_form_name (unsigned int);
1467
1468 static const char *dwarf_bool_name (unsigned int);
1469
1470 static const char *dwarf_type_encoding_name (unsigned int);
1471
1472 static struct die_info *sibling_die (struct die_info *);
1473
1474 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1475
1476 static void dump_die_for_error (struct die_info *);
1477
1478 static void dump_die_1 (struct ui_file *, int level, int max_level,
1479 struct die_info *);
1480
1481 /*static*/ void dump_die (struct die_info *, int max_level);
1482
1483 static void store_in_ref_table (struct die_info *,
1484 struct dwarf2_cu *);
1485
1486 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1487
1488 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1489
1490 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1491 const struct attribute *,
1492 struct dwarf2_cu **);
1493
1494 static struct die_info *follow_die_ref (struct die_info *,
1495 const struct attribute *,
1496 struct dwarf2_cu **);
1497
1498 static struct die_info *follow_die_sig (struct die_info *,
1499 const struct attribute *,
1500 struct dwarf2_cu **);
1501
1502 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1503 struct dwarf2_cu *);
1504
1505 static struct type *get_DW_AT_signature_type (struct die_info *,
1506 const struct attribute *,
1507 struct dwarf2_cu *);
1508
1509 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1510
1511 static void read_signatured_type (struct signatured_type *);
1512
1513 static int attr_to_dynamic_prop (const struct attribute *attr,
1514 struct die_info *die, struct dwarf2_cu *cu,
1515 struct dynamic_prop *prop, struct type *type);
1516
1517 /* memory allocation interface */
1518
1519 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1520
1521 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1522
1523 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1524
1525 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1526 struct dwarf2_loclist_baton *baton,
1527 const struct attribute *attr);
1528
1529 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1530 struct symbol *sym,
1531 struct dwarf2_cu *cu,
1532 int is_block);
1533
1534 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1535 const gdb_byte *info_ptr,
1536 struct abbrev_info *abbrev);
1537
1538 static hashval_t partial_die_hash (const void *item);
1539
1540 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1541
1542 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1543 (sect_offset sect_off, unsigned int offset_in_dwz,
1544 struct dwarf2_per_objfile *dwarf2_per_objfile);
1545
1546 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1547 struct die_info *comp_unit_die,
1548 enum language pretend_language);
1549
1550 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1551
1552 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1553
1554 static struct type *set_die_type (struct die_info *, struct type *,
1555 struct dwarf2_cu *);
1556
1557 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1558
1559 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1560
1561 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1562 enum language);
1563
1564 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1565 enum language);
1566
1567 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1568 enum language);
1569
1570 static void dwarf2_add_dependence (struct dwarf2_cu *,
1571 struct dwarf2_per_cu_data *);
1572
1573 static void dwarf2_mark (struct dwarf2_cu *);
1574
1575 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1576
1577 static struct type *get_die_type_at_offset (sect_offset,
1578 struct dwarf2_per_cu_data *);
1579
1580 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1581
1582 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1583 enum language pretend_language);
1584
1585 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1586
1587 /* Class, the destructor of which frees all allocated queue entries. This
1588 will only have work to do if an error was thrown while processing the
1589 dwarf. If no error was thrown then the queue entries should have all
1590 been processed, and freed, as we went along. */
1591
1592 class dwarf2_queue_guard
1593 {
1594 public:
1595 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1596 : m_per_objfile (per_objfile)
1597 {
1598 }
1599
1600 /* Free any entries remaining on the queue. There should only be
1601 entries left if we hit an error while processing the dwarf. */
1602 ~dwarf2_queue_guard ()
1603 {
1604 /* Ensure that no memory is allocated by the queue. */
1605 std::queue<dwarf2_queue_item> empty;
1606 std::swap (m_per_objfile->queue, empty);
1607 }
1608
1609 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1610
1611 private:
1612 dwarf2_per_objfile *m_per_objfile;
1613 };
1614
1615 dwarf2_queue_item::~dwarf2_queue_item ()
1616 {
1617 /* Anything still marked queued is likely to be in an
1618 inconsistent state, so discard it. */
1619 if (per_cu->queued)
1620 {
1621 if (per_cu->cu != NULL)
1622 free_one_cached_comp_unit (per_cu);
1623 per_cu->queued = 0;
1624 }
1625 }
1626
1627 /* The return type of find_file_and_directory. Note, the enclosed
1628 string pointers are only valid while this object is valid. */
1629
1630 struct file_and_directory
1631 {
1632 /* The filename. This is never NULL. */
1633 const char *name;
1634
1635 /* The compilation directory. NULL if not known. If we needed to
1636 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1637 points directly to the DW_AT_comp_dir string attribute owned by
1638 the obstack that owns the DIE. */
1639 const char *comp_dir;
1640
1641 /* If we needed to build a new string for comp_dir, this is what
1642 owns the storage. */
1643 std::string comp_dir_storage;
1644 };
1645
1646 static file_and_directory find_file_and_directory (struct die_info *die,
1647 struct dwarf2_cu *cu);
1648
1649 static htab_up allocate_signatured_type_table ();
1650
1651 static htab_up allocate_dwo_unit_table ();
1652
1653 static struct dwo_unit *lookup_dwo_unit_in_dwp
1654 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1655 struct dwp_file *dwp_file, const char *comp_dir,
1656 ULONGEST signature, int is_debug_types);
1657
1658 static struct dwp_file *get_dwp_file
1659 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1660
1661 static struct dwo_unit *lookup_dwo_comp_unit
1662 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1663
1664 static struct dwo_unit *lookup_dwo_type_unit
1665 (struct signatured_type *, const char *, const char *);
1666
1667 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1668
1669 /* A unique pointer to a dwo_file. */
1670
1671 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1672
1673 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1674
1675 static void check_producer (struct dwarf2_cu *cu);
1676
1677 static void free_line_header_voidp (void *arg);
1678 \f
1679 /* Various complaints about symbol reading that don't abort the process. */
1680
1681 static void
1682 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1683 {
1684 complaint (_("statement list doesn't fit in .debug_line section"));
1685 }
1686
1687 static void
1688 dwarf2_debug_line_missing_file_complaint (void)
1689 {
1690 complaint (_(".debug_line section has line data without a file"));
1691 }
1692
1693 static void
1694 dwarf2_debug_line_missing_end_sequence_complaint (void)
1695 {
1696 complaint (_(".debug_line section has line "
1697 "program sequence without an end"));
1698 }
1699
1700 static void
1701 dwarf2_complex_location_expr_complaint (void)
1702 {
1703 complaint (_("location expression too complex"));
1704 }
1705
1706 static void
1707 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1708 int arg3)
1709 {
1710 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1711 arg1, arg2, arg3);
1712 }
1713
1714 static void
1715 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1716 {
1717 complaint (_("debug info runs off end of %s section"
1718 " [in module %s]"),
1719 section->get_name (),
1720 section->get_file_name ());
1721 }
1722
1723 static void
1724 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1725 {
1726 complaint (_("macro debug info contains a "
1727 "malformed macro definition:\n`%s'"),
1728 arg1);
1729 }
1730
1731 static void
1732 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1733 {
1734 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1735 arg1, arg2);
1736 }
1737
1738 /* Hash function for line_header_hash. */
1739
1740 static hashval_t
1741 line_header_hash (const struct line_header *ofs)
1742 {
1743 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1744 }
1745
1746 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1747
1748 static hashval_t
1749 line_header_hash_voidp (const void *item)
1750 {
1751 const struct line_header *ofs = (const struct line_header *) item;
1752
1753 return line_header_hash (ofs);
1754 }
1755
1756 /* Equality function for line_header_hash. */
1757
1758 static int
1759 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1760 {
1761 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1762 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1763
1764 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1765 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1766 }
1767
1768 \f
1769
1770 /* See declaration. */
1771
1772 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1773 const dwarf2_debug_sections *names,
1774 bool can_copy_)
1775 : objfile (objfile_),
1776 can_copy (can_copy_)
1777 {
1778 if (names == NULL)
1779 names = &dwarf2_elf_names;
1780
1781 bfd *obfd = objfile->obfd;
1782
1783 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1784 locate_sections (obfd, sec, *names);
1785 }
1786
1787 dwarf2_per_objfile::~dwarf2_per_objfile ()
1788 {
1789 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1790 free_cached_comp_units ();
1791
1792 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1793 per_cu->imported_symtabs_free ();
1794
1795 for (signatured_type *sig_type : all_type_units)
1796 sig_type->per_cu.imported_symtabs_free ();
1797
1798 /* Everything else should be on the objfile obstack. */
1799 }
1800
1801 /* See declaration. */
1802
1803 void
1804 dwarf2_per_objfile::free_cached_comp_units ()
1805 {
1806 dwarf2_per_cu_data *per_cu = read_in_chain;
1807 dwarf2_per_cu_data **last_chain = &read_in_chain;
1808 while (per_cu != NULL)
1809 {
1810 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1811
1812 delete per_cu->cu;
1813 *last_chain = next_cu;
1814 per_cu = next_cu;
1815 }
1816 }
1817
1818 /* A helper class that calls free_cached_comp_units on
1819 destruction. */
1820
1821 class free_cached_comp_units
1822 {
1823 public:
1824
1825 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1826 : m_per_objfile (per_objfile)
1827 {
1828 }
1829
1830 ~free_cached_comp_units ()
1831 {
1832 m_per_objfile->free_cached_comp_units ();
1833 }
1834
1835 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1836
1837 private:
1838
1839 dwarf2_per_objfile *m_per_objfile;
1840 };
1841
1842 /* Try to locate the sections we need for DWARF 2 debugging
1843 information and return true if we have enough to do something.
1844 NAMES points to the dwarf2 section names, or is NULL if the standard
1845 ELF names are used. CAN_COPY is true for formats where symbol
1846 interposition is possible and so symbol values must follow copy
1847 relocation rules. */
1848
1849 int
1850 dwarf2_has_info (struct objfile *objfile,
1851 const struct dwarf2_debug_sections *names,
1852 bool can_copy)
1853 {
1854 if (objfile->flags & OBJF_READNEVER)
1855 return 0;
1856
1857 struct dwarf2_per_objfile *dwarf2_per_objfile
1858 = get_dwarf2_per_objfile (objfile);
1859
1860 if (dwarf2_per_objfile == NULL)
1861 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1862 names,
1863 can_copy);
1864
1865 return (!dwarf2_per_objfile->info.is_virtual
1866 && dwarf2_per_objfile->info.s.section != NULL
1867 && !dwarf2_per_objfile->abbrev.is_virtual
1868 && dwarf2_per_objfile->abbrev.s.section != NULL);
1869 }
1870
1871 /* When loading sections, we look either for uncompressed section or for
1872 compressed section names. */
1873
1874 static int
1875 section_is_p (const char *section_name,
1876 const struct dwarf2_section_names *names)
1877 {
1878 if (names->normal != NULL
1879 && strcmp (section_name, names->normal) == 0)
1880 return 1;
1881 if (names->compressed != NULL
1882 && strcmp (section_name, names->compressed) == 0)
1883 return 1;
1884 return 0;
1885 }
1886
1887 /* See declaration. */
1888
1889 void
1890 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1891 const dwarf2_debug_sections &names)
1892 {
1893 flagword aflag = bfd_section_flags (sectp);
1894
1895 if ((aflag & SEC_HAS_CONTENTS) == 0)
1896 {
1897 }
1898 else if (elf_section_data (sectp)->this_hdr.sh_size
1899 > bfd_get_file_size (abfd))
1900 {
1901 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1902 warning (_("Discarding section %s which has a section size (%s"
1903 ") larger than the file size [in module %s]"),
1904 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1905 bfd_get_filename (abfd));
1906 }
1907 else if (section_is_p (sectp->name, &names.info))
1908 {
1909 this->info.s.section = sectp;
1910 this->info.size = bfd_section_size (sectp);
1911 }
1912 else if (section_is_p (sectp->name, &names.abbrev))
1913 {
1914 this->abbrev.s.section = sectp;
1915 this->abbrev.size = bfd_section_size (sectp);
1916 }
1917 else if (section_is_p (sectp->name, &names.line))
1918 {
1919 this->line.s.section = sectp;
1920 this->line.size = bfd_section_size (sectp);
1921 }
1922 else if (section_is_p (sectp->name, &names.loc))
1923 {
1924 this->loc.s.section = sectp;
1925 this->loc.size = bfd_section_size (sectp);
1926 }
1927 else if (section_is_p (sectp->name, &names.loclists))
1928 {
1929 this->loclists.s.section = sectp;
1930 this->loclists.size = bfd_section_size (sectp);
1931 }
1932 else if (section_is_p (sectp->name, &names.macinfo))
1933 {
1934 this->macinfo.s.section = sectp;
1935 this->macinfo.size = bfd_section_size (sectp);
1936 }
1937 else if (section_is_p (sectp->name, &names.macro))
1938 {
1939 this->macro.s.section = sectp;
1940 this->macro.size = bfd_section_size (sectp);
1941 }
1942 else if (section_is_p (sectp->name, &names.str))
1943 {
1944 this->str.s.section = sectp;
1945 this->str.size = bfd_section_size (sectp);
1946 }
1947 else if (section_is_p (sectp->name, &names.str_offsets))
1948 {
1949 this->str_offsets.s.section = sectp;
1950 this->str_offsets.size = bfd_section_size (sectp);
1951 }
1952 else if (section_is_p (sectp->name, &names.line_str))
1953 {
1954 this->line_str.s.section = sectp;
1955 this->line_str.size = bfd_section_size (sectp);
1956 }
1957 else if (section_is_p (sectp->name, &names.addr))
1958 {
1959 this->addr.s.section = sectp;
1960 this->addr.size = bfd_section_size (sectp);
1961 }
1962 else if (section_is_p (sectp->name, &names.frame))
1963 {
1964 this->frame.s.section = sectp;
1965 this->frame.size = bfd_section_size (sectp);
1966 }
1967 else if (section_is_p (sectp->name, &names.eh_frame))
1968 {
1969 this->eh_frame.s.section = sectp;
1970 this->eh_frame.size = bfd_section_size (sectp);
1971 }
1972 else if (section_is_p (sectp->name, &names.ranges))
1973 {
1974 this->ranges.s.section = sectp;
1975 this->ranges.size = bfd_section_size (sectp);
1976 }
1977 else if (section_is_p (sectp->name, &names.rnglists))
1978 {
1979 this->rnglists.s.section = sectp;
1980 this->rnglists.size = bfd_section_size (sectp);
1981 }
1982 else if (section_is_p (sectp->name, &names.types))
1983 {
1984 struct dwarf2_section_info type_section;
1985
1986 memset (&type_section, 0, sizeof (type_section));
1987 type_section.s.section = sectp;
1988 type_section.size = bfd_section_size (sectp);
1989
1990 this->types.push_back (type_section);
1991 }
1992 else if (section_is_p (sectp->name, &names.gdb_index))
1993 {
1994 this->gdb_index.s.section = sectp;
1995 this->gdb_index.size = bfd_section_size (sectp);
1996 }
1997 else if (section_is_p (sectp->name, &names.debug_names))
1998 {
1999 this->debug_names.s.section = sectp;
2000 this->debug_names.size = bfd_section_size (sectp);
2001 }
2002 else if (section_is_p (sectp->name, &names.debug_aranges))
2003 {
2004 this->debug_aranges.s.section = sectp;
2005 this->debug_aranges.size = bfd_section_size (sectp);
2006 }
2007
2008 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2009 && bfd_section_vma (sectp) == 0)
2010 this->has_section_at_zero = true;
2011 }
2012
2013 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2014 SECTION_NAME. */
2015
2016 void
2017 dwarf2_get_section_info (struct objfile *objfile,
2018 enum dwarf2_section_enum sect,
2019 asection **sectp, const gdb_byte **bufp,
2020 bfd_size_type *sizep)
2021 {
2022 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2023 struct dwarf2_section_info *info;
2024
2025 /* We may see an objfile without any DWARF, in which case we just
2026 return nothing. */
2027 if (data == NULL)
2028 {
2029 *sectp = NULL;
2030 *bufp = NULL;
2031 *sizep = 0;
2032 return;
2033 }
2034 switch (sect)
2035 {
2036 case DWARF2_DEBUG_FRAME:
2037 info = &data->frame;
2038 break;
2039 case DWARF2_EH_FRAME:
2040 info = &data->eh_frame;
2041 break;
2042 default:
2043 gdb_assert_not_reached ("unexpected section");
2044 }
2045
2046 info->read (objfile);
2047
2048 *sectp = info->get_bfd_section ();
2049 *bufp = info->buffer;
2050 *sizep = info->size;
2051 }
2052
2053 /* A helper function to find the sections for a .dwz file. */
2054
2055 static void
2056 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2057 {
2058 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2059
2060 /* Note that we only support the standard ELF names, because .dwz
2061 is ELF-only (at the time of writing). */
2062 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2063 {
2064 dwz_file->abbrev.s.section = sectp;
2065 dwz_file->abbrev.size = bfd_section_size (sectp);
2066 }
2067 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2068 {
2069 dwz_file->info.s.section = sectp;
2070 dwz_file->info.size = bfd_section_size (sectp);
2071 }
2072 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2073 {
2074 dwz_file->str.s.section = sectp;
2075 dwz_file->str.size = bfd_section_size (sectp);
2076 }
2077 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2078 {
2079 dwz_file->line.s.section = sectp;
2080 dwz_file->line.size = bfd_section_size (sectp);
2081 }
2082 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2083 {
2084 dwz_file->macro.s.section = sectp;
2085 dwz_file->macro.size = bfd_section_size (sectp);
2086 }
2087 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2088 {
2089 dwz_file->gdb_index.s.section = sectp;
2090 dwz_file->gdb_index.size = bfd_section_size (sectp);
2091 }
2092 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2093 {
2094 dwz_file->debug_names.s.section = sectp;
2095 dwz_file->debug_names.size = bfd_section_size (sectp);
2096 }
2097 }
2098
2099 /* See dwarf2read.h. */
2100
2101 struct dwz_file *
2102 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2103 {
2104 const char *filename;
2105 bfd_size_type buildid_len_arg;
2106 size_t buildid_len;
2107 bfd_byte *buildid;
2108
2109 if (dwarf2_per_objfile->dwz_file != NULL)
2110 return dwarf2_per_objfile->dwz_file.get ();
2111
2112 bfd_set_error (bfd_error_no_error);
2113 gdb::unique_xmalloc_ptr<char> data
2114 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2115 &buildid_len_arg, &buildid));
2116 if (data == NULL)
2117 {
2118 if (bfd_get_error () == bfd_error_no_error)
2119 return NULL;
2120 error (_("could not read '.gnu_debugaltlink' section: %s"),
2121 bfd_errmsg (bfd_get_error ()));
2122 }
2123
2124 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2125
2126 buildid_len = (size_t) buildid_len_arg;
2127
2128 filename = data.get ();
2129
2130 std::string abs_storage;
2131 if (!IS_ABSOLUTE_PATH (filename))
2132 {
2133 gdb::unique_xmalloc_ptr<char> abs
2134 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2135
2136 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2137 filename = abs_storage.c_str ();
2138 }
2139
2140 /* First try the file name given in the section. If that doesn't
2141 work, try to use the build-id instead. */
2142 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2143 if (dwz_bfd != NULL)
2144 {
2145 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2146 dwz_bfd.reset (nullptr);
2147 }
2148
2149 if (dwz_bfd == NULL)
2150 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2151
2152 if (dwz_bfd == nullptr)
2153 {
2154 gdb::unique_xmalloc_ptr<char> alt_filename;
2155 const char *origname = dwarf2_per_objfile->objfile->original_name;
2156
2157 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2158 buildid_len,
2159 origname,
2160 &alt_filename));
2161
2162 if (fd.get () >= 0)
2163 {
2164 /* File successfully retrieved from server. */
2165 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2166
2167 if (dwz_bfd == nullptr)
2168 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2169 alt_filename.get ());
2170 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2171 dwz_bfd.reset (nullptr);
2172 }
2173 }
2174
2175 if (dwz_bfd == NULL)
2176 error (_("could not find '.gnu_debugaltlink' file for %s"),
2177 objfile_name (dwarf2_per_objfile->objfile));
2178
2179 std::unique_ptr<struct dwz_file> result
2180 (new struct dwz_file (std::move (dwz_bfd)));
2181
2182 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2183 result.get ());
2184
2185 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2186 result->dwz_bfd.get ());
2187 dwarf2_per_objfile->dwz_file = std::move (result);
2188 return dwarf2_per_objfile->dwz_file.get ();
2189 }
2190 \f
2191 /* DWARF quick_symbols_functions support. */
2192
2193 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2194 unique line tables, so we maintain a separate table of all .debug_line
2195 derived entries to support the sharing.
2196 All the quick functions need is the list of file names. We discard the
2197 line_header when we're done and don't need to record it here. */
2198 struct quick_file_names
2199 {
2200 /* The data used to construct the hash key. */
2201 struct stmt_list_hash hash;
2202
2203 /* The number of entries in file_names, real_names. */
2204 unsigned int num_file_names;
2205
2206 /* The file names from the line table, after being run through
2207 file_full_name. */
2208 const char **file_names;
2209
2210 /* The file names from the line table after being run through
2211 gdb_realpath. These are computed lazily. */
2212 const char **real_names;
2213 };
2214
2215 /* When using the index (and thus not using psymtabs), each CU has an
2216 object of this type. This is used to hold information needed by
2217 the various "quick" methods. */
2218 struct dwarf2_per_cu_quick_data
2219 {
2220 /* The file table. This can be NULL if there was no file table
2221 or it's currently not read in.
2222 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2223 struct quick_file_names *file_names;
2224
2225 /* The corresponding symbol table. This is NULL if symbols for this
2226 CU have not yet been read. */
2227 struct compunit_symtab *compunit_symtab;
2228
2229 /* A temporary mark bit used when iterating over all CUs in
2230 expand_symtabs_matching. */
2231 unsigned int mark : 1;
2232
2233 /* True if we've tried to read the file table and found there isn't one.
2234 There will be no point in trying to read it again next time. */
2235 unsigned int no_file_data : 1;
2236 };
2237
2238 /* Utility hash function for a stmt_list_hash. */
2239
2240 static hashval_t
2241 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2242 {
2243 hashval_t v = 0;
2244
2245 if (stmt_list_hash->dwo_unit != NULL)
2246 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2247 v += to_underlying (stmt_list_hash->line_sect_off);
2248 return v;
2249 }
2250
2251 /* Utility equality function for a stmt_list_hash. */
2252
2253 static int
2254 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2255 const struct stmt_list_hash *rhs)
2256 {
2257 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2258 return 0;
2259 if (lhs->dwo_unit != NULL
2260 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2261 return 0;
2262
2263 return lhs->line_sect_off == rhs->line_sect_off;
2264 }
2265
2266 /* Hash function for a quick_file_names. */
2267
2268 static hashval_t
2269 hash_file_name_entry (const void *e)
2270 {
2271 const struct quick_file_names *file_data
2272 = (const struct quick_file_names *) e;
2273
2274 return hash_stmt_list_entry (&file_data->hash);
2275 }
2276
2277 /* Equality function for a quick_file_names. */
2278
2279 static int
2280 eq_file_name_entry (const void *a, const void *b)
2281 {
2282 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2283 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2284
2285 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2286 }
2287
2288 /* Delete function for a quick_file_names. */
2289
2290 static void
2291 delete_file_name_entry (void *e)
2292 {
2293 struct quick_file_names *file_data = (struct quick_file_names *) e;
2294 int i;
2295
2296 for (i = 0; i < file_data->num_file_names; ++i)
2297 {
2298 xfree ((void*) file_data->file_names[i]);
2299 if (file_data->real_names)
2300 xfree ((void*) file_data->real_names[i]);
2301 }
2302
2303 /* The space for the struct itself lives on objfile_obstack,
2304 so we don't free it here. */
2305 }
2306
2307 /* Create a quick_file_names hash table. */
2308
2309 static htab_up
2310 create_quick_file_names_table (unsigned int nr_initial_entries)
2311 {
2312 return htab_up (htab_create_alloc (nr_initial_entries,
2313 hash_file_name_entry, eq_file_name_entry,
2314 delete_file_name_entry, xcalloc, xfree));
2315 }
2316
2317 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2318 have to be created afterwards. You should call age_cached_comp_units after
2319 processing PER_CU->CU. dw2_setup must have been already called. */
2320
2321 static void
2322 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2323 {
2324 if (per_cu->is_debug_types)
2325 load_full_type_unit (per_cu);
2326 else
2327 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2328
2329 if (per_cu->cu == NULL)
2330 return; /* Dummy CU. */
2331
2332 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2333 }
2334
2335 /* Read in the symbols for PER_CU. */
2336
2337 static void
2338 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2339 {
2340 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2341
2342 /* Skip type_unit_groups, reading the type units they contain
2343 is handled elsewhere. */
2344 if (per_cu->type_unit_group_p ())
2345 return;
2346
2347 /* The destructor of dwarf2_queue_guard frees any entries left on
2348 the queue. After this point we're guaranteed to leave this function
2349 with the dwarf queue empty. */
2350 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2351
2352 if (dwarf2_per_objfile->using_index
2353 ? per_cu->v.quick->compunit_symtab == NULL
2354 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2355 {
2356 queue_comp_unit (per_cu, language_minimal);
2357 load_cu (per_cu, skip_partial);
2358
2359 /* If we just loaded a CU from a DWO, and we're working with an index
2360 that may badly handle TUs, load all the TUs in that DWO as well.
2361 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2362 if (!per_cu->is_debug_types
2363 && per_cu->cu != NULL
2364 && per_cu->cu->dwo_unit != NULL
2365 && dwarf2_per_objfile->index_table != NULL
2366 && dwarf2_per_objfile->index_table->version <= 7
2367 /* DWP files aren't supported yet. */
2368 && get_dwp_file (dwarf2_per_objfile) == NULL)
2369 queue_and_load_all_dwo_tus (per_cu);
2370 }
2371
2372 process_queue (dwarf2_per_objfile);
2373
2374 /* Age the cache, releasing compilation units that have not
2375 been used recently. */
2376 age_cached_comp_units (dwarf2_per_objfile);
2377 }
2378
2379 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2380 the objfile from which this CU came. Returns the resulting symbol
2381 table. */
2382
2383 static struct compunit_symtab *
2384 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2385 {
2386 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2387
2388 gdb_assert (dwarf2_per_objfile->using_index);
2389 if (!per_cu->v.quick->compunit_symtab)
2390 {
2391 free_cached_comp_units freer (dwarf2_per_objfile);
2392 scoped_restore decrementer = increment_reading_symtab ();
2393 dw2_do_instantiate_symtab (per_cu, skip_partial);
2394 process_cu_includes (dwarf2_per_objfile);
2395 }
2396
2397 return per_cu->v.quick->compunit_symtab;
2398 }
2399
2400 /* See declaration. */
2401
2402 dwarf2_per_cu_data *
2403 dwarf2_per_objfile::get_cutu (int index)
2404 {
2405 if (index >= this->all_comp_units.size ())
2406 {
2407 index -= this->all_comp_units.size ();
2408 gdb_assert (index < this->all_type_units.size ());
2409 return &this->all_type_units[index]->per_cu;
2410 }
2411
2412 return this->all_comp_units[index];
2413 }
2414
2415 /* See declaration. */
2416
2417 dwarf2_per_cu_data *
2418 dwarf2_per_objfile::get_cu (int index)
2419 {
2420 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2421
2422 return this->all_comp_units[index];
2423 }
2424
2425 /* See declaration. */
2426
2427 signatured_type *
2428 dwarf2_per_objfile::get_tu (int index)
2429 {
2430 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2431
2432 return this->all_type_units[index];
2433 }
2434
2435 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2436 objfile_obstack, and constructed with the specified field
2437 values. */
2438
2439 static dwarf2_per_cu_data *
2440 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2441 struct dwarf2_section_info *section,
2442 int is_dwz,
2443 sect_offset sect_off, ULONGEST length)
2444 {
2445 struct objfile *objfile = dwarf2_per_objfile->objfile;
2446 dwarf2_per_cu_data *the_cu
2447 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2448 struct dwarf2_per_cu_data);
2449 the_cu->sect_off = sect_off;
2450 the_cu->length = length;
2451 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2452 the_cu->section = section;
2453 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2454 struct dwarf2_per_cu_quick_data);
2455 the_cu->is_dwz = is_dwz;
2456 return the_cu;
2457 }
2458
2459 /* A helper for create_cus_from_index that handles a given list of
2460 CUs. */
2461
2462 static void
2463 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2464 const gdb_byte *cu_list, offset_type n_elements,
2465 struct dwarf2_section_info *section,
2466 int is_dwz)
2467 {
2468 for (offset_type i = 0; i < n_elements; i += 2)
2469 {
2470 gdb_static_assert (sizeof (ULONGEST) >= 8);
2471
2472 sect_offset sect_off
2473 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2474 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2475 cu_list += 2 * 8;
2476
2477 dwarf2_per_cu_data *per_cu
2478 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2479 sect_off, length);
2480 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2481 }
2482 }
2483
2484 /* Read the CU list from the mapped index, and use it to create all
2485 the CU objects for this objfile. */
2486
2487 static void
2488 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2489 const gdb_byte *cu_list, offset_type cu_list_elements,
2490 const gdb_byte *dwz_list, offset_type dwz_elements)
2491 {
2492 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2493 dwarf2_per_objfile->all_comp_units.reserve
2494 ((cu_list_elements + dwz_elements) / 2);
2495
2496 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2497 &dwarf2_per_objfile->info, 0);
2498
2499 if (dwz_elements == 0)
2500 return;
2501
2502 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2503 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2504 &dwz->info, 1);
2505 }
2506
2507 /* Create the signatured type hash table from the index. */
2508
2509 static void
2510 create_signatured_type_table_from_index
2511 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2512 struct dwarf2_section_info *section,
2513 const gdb_byte *bytes,
2514 offset_type elements)
2515 {
2516 struct objfile *objfile = dwarf2_per_objfile->objfile;
2517
2518 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2519 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2520
2521 htab_up sig_types_hash = allocate_signatured_type_table ();
2522
2523 for (offset_type i = 0; i < elements; i += 3)
2524 {
2525 struct signatured_type *sig_type;
2526 ULONGEST signature;
2527 void **slot;
2528 cu_offset type_offset_in_tu;
2529
2530 gdb_static_assert (sizeof (ULONGEST) >= 8);
2531 sect_offset sect_off
2532 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2533 type_offset_in_tu
2534 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2535 BFD_ENDIAN_LITTLE);
2536 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2537 bytes += 3 * 8;
2538
2539 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2540 struct signatured_type);
2541 sig_type->signature = signature;
2542 sig_type->type_offset_in_tu = type_offset_in_tu;
2543 sig_type->per_cu.is_debug_types = 1;
2544 sig_type->per_cu.section = section;
2545 sig_type->per_cu.sect_off = sect_off;
2546 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2547 sig_type->per_cu.v.quick
2548 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2549 struct dwarf2_per_cu_quick_data);
2550
2551 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2552 *slot = sig_type;
2553
2554 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2555 }
2556
2557 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2558 }
2559
2560 /* Create the signatured type hash table from .debug_names. */
2561
2562 static void
2563 create_signatured_type_table_from_debug_names
2564 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2565 const mapped_debug_names &map,
2566 struct dwarf2_section_info *section,
2567 struct dwarf2_section_info *abbrev_section)
2568 {
2569 struct objfile *objfile = dwarf2_per_objfile->objfile;
2570
2571 section->read (objfile);
2572 abbrev_section->read (objfile);
2573
2574 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2575 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2576
2577 htab_up sig_types_hash = allocate_signatured_type_table ();
2578
2579 for (uint32_t i = 0; i < map.tu_count; ++i)
2580 {
2581 struct signatured_type *sig_type;
2582 void **slot;
2583
2584 sect_offset sect_off
2585 = (sect_offset) (extract_unsigned_integer
2586 (map.tu_table_reordered + i * map.offset_size,
2587 map.offset_size,
2588 map.dwarf5_byte_order));
2589
2590 comp_unit_head cu_header;
2591 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2592 abbrev_section,
2593 section->buffer + to_underlying (sect_off),
2594 rcuh_kind::TYPE);
2595
2596 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2597 struct signatured_type);
2598 sig_type->signature = cu_header.signature;
2599 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2600 sig_type->per_cu.is_debug_types = 1;
2601 sig_type->per_cu.section = section;
2602 sig_type->per_cu.sect_off = sect_off;
2603 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2604 sig_type->per_cu.v.quick
2605 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2606 struct dwarf2_per_cu_quick_data);
2607
2608 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2609 *slot = sig_type;
2610
2611 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2612 }
2613
2614 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2615 }
2616
2617 /* Read the address map data from the mapped index, and use it to
2618 populate the objfile's psymtabs_addrmap. */
2619
2620 static void
2621 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2622 struct mapped_index *index)
2623 {
2624 struct objfile *objfile = dwarf2_per_objfile->objfile;
2625 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2626 const gdb_byte *iter, *end;
2627 struct addrmap *mutable_map;
2628 CORE_ADDR baseaddr;
2629
2630 auto_obstack temp_obstack;
2631
2632 mutable_map = addrmap_create_mutable (&temp_obstack);
2633
2634 iter = index->address_table.data ();
2635 end = iter + index->address_table.size ();
2636
2637 baseaddr = objfile->text_section_offset ();
2638
2639 while (iter < end)
2640 {
2641 ULONGEST hi, lo, cu_index;
2642 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2643 iter += 8;
2644 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2645 iter += 8;
2646 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2647 iter += 4;
2648
2649 if (lo > hi)
2650 {
2651 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2652 hex_string (lo), hex_string (hi));
2653 continue;
2654 }
2655
2656 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2657 {
2658 complaint (_(".gdb_index address table has invalid CU number %u"),
2659 (unsigned) cu_index);
2660 continue;
2661 }
2662
2663 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2664 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2665 addrmap_set_empty (mutable_map, lo, hi - 1,
2666 dwarf2_per_objfile->get_cu (cu_index));
2667 }
2668
2669 objfile->partial_symtabs->psymtabs_addrmap
2670 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2671 }
2672
2673 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2674 populate the objfile's psymtabs_addrmap. */
2675
2676 static void
2677 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2678 struct dwarf2_section_info *section)
2679 {
2680 struct objfile *objfile = dwarf2_per_objfile->objfile;
2681 bfd *abfd = objfile->obfd;
2682 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2683 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2684
2685 auto_obstack temp_obstack;
2686 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2687
2688 std::unordered_map<sect_offset,
2689 dwarf2_per_cu_data *,
2690 gdb::hash_enum<sect_offset>>
2691 debug_info_offset_to_per_cu;
2692 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2693 {
2694 const auto insertpair
2695 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2696 if (!insertpair.second)
2697 {
2698 warning (_("Section .debug_aranges in %s has duplicate "
2699 "debug_info_offset %s, ignoring .debug_aranges."),
2700 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2701 return;
2702 }
2703 }
2704
2705 section->read (objfile);
2706
2707 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2708
2709 const gdb_byte *addr = section->buffer;
2710
2711 while (addr < section->buffer + section->size)
2712 {
2713 const gdb_byte *const entry_addr = addr;
2714 unsigned int bytes_read;
2715
2716 const LONGEST entry_length = read_initial_length (abfd, addr,
2717 &bytes_read);
2718 addr += bytes_read;
2719
2720 const gdb_byte *const entry_end = addr + entry_length;
2721 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2722 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2723 if (addr + entry_length > section->buffer + section->size)
2724 {
2725 warning (_("Section .debug_aranges in %s entry at offset %s "
2726 "length %s exceeds section length %s, "
2727 "ignoring .debug_aranges."),
2728 objfile_name (objfile),
2729 plongest (entry_addr - section->buffer),
2730 plongest (bytes_read + entry_length),
2731 pulongest (section->size));
2732 return;
2733 }
2734
2735 /* The version number. */
2736 const uint16_t version = read_2_bytes (abfd, addr);
2737 addr += 2;
2738 if (version != 2)
2739 {
2740 warning (_("Section .debug_aranges in %s entry at offset %s "
2741 "has unsupported version %d, ignoring .debug_aranges."),
2742 objfile_name (objfile),
2743 plongest (entry_addr - section->buffer), version);
2744 return;
2745 }
2746
2747 const uint64_t debug_info_offset
2748 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2749 addr += offset_size;
2750 const auto per_cu_it
2751 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2752 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2753 {
2754 warning (_("Section .debug_aranges in %s entry at offset %s "
2755 "debug_info_offset %s does not exists, "
2756 "ignoring .debug_aranges."),
2757 objfile_name (objfile),
2758 plongest (entry_addr - section->buffer),
2759 pulongest (debug_info_offset));
2760 return;
2761 }
2762 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2763
2764 const uint8_t address_size = *addr++;
2765 if (address_size < 1 || address_size > 8)
2766 {
2767 warning (_("Section .debug_aranges in %s entry at offset %s "
2768 "address_size %u is invalid, ignoring .debug_aranges."),
2769 objfile_name (objfile),
2770 plongest (entry_addr - section->buffer), address_size);
2771 return;
2772 }
2773
2774 const uint8_t segment_selector_size = *addr++;
2775 if (segment_selector_size != 0)
2776 {
2777 warning (_("Section .debug_aranges in %s entry at offset %s "
2778 "segment_selector_size %u is not supported, "
2779 "ignoring .debug_aranges."),
2780 objfile_name (objfile),
2781 plongest (entry_addr - section->buffer),
2782 segment_selector_size);
2783 return;
2784 }
2785
2786 /* Must pad to an alignment boundary that is twice the address
2787 size. It is undocumented by the DWARF standard but GCC does
2788 use it. */
2789 for (size_t padding = ((-(addr - section->buffer))
2790 & (2 * address_size - 1));
2791 padding > 0; padding--)
2792 if (*addr++ != 0)
2793 {
2794 warning (_("Section .debug_aranges in %s entry at offset %s "
2795 "padding is not zero, ignoring .debug_aranges."),
2796 objfile_name (objfile),
2797 plongest (entry_addr - section->buffer));
2798 return;
2799 }
2800
2801 for (;;)
2802 {
2803 if (addr + 2 * address_size > entry_end)
2804 {
2805 warning (_("Section .debug_aranges in %s entry at offset %s "
2806 "address list is not properly terminated, "
2807 "ignoring .debug_aranges."),
2808 objfile_name (objfile),
2809 plongest (entry_addr - section->buffer));
2810 return;
2811 }
2812 ULONGEST start = extract_unsigned_integer (addr, address_size,
2813 dwarf5_byte_order);
2814 addr += address_size;
2815 ULONGEST length = extract_unsigned_integer (addr, address_size,
2816 dwarf5_byte_order);
2817 addr += address_size;
2818 if (start == 0 && length == 0)
2819 break;
2820 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2821 {
2822 /* Symbol was eliminated due to a COMDAT group. */
2823 continue;
2824 }
2825 ULONGEST end = start + length;
2826 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2827 - baseaddr);
2828 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2829 - baseaddr);
2830 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2831 }
2832 }
2833
2834 objfile->partial_symtabs->psymtabs_addrmap
2835 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2836 }
2837
2838 /* Find a slot in the mapped index INDEX for the object named NAME.
2839 If NAME is found, set *VEC_OUT to point to the CU vector in the
2840 constant pool and return true. If NAME cannot be found, return
2841 false. */
2842
2843 static bool
2844 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2845 offset_type **vec_out)
2846 {
2847 offset_type hash;
2848 offset_type slot, step;
2849 int (*cmp) (const char *, const char *);
2850
2851 gdb::unique_xmalloc_ptr<char> without_params;
2852 if (current_language->la_language == language_cplus
2853 || current_language->la_language == language_fortran
2854 || current_language->la_language == language_d)
2855 {
2856 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2857 not contain any. */
2858
2859 if (strchr (name, '(') != NULL)
2860 {
2861 without_params = cp_remove_params (name);
2862
2863 if (without_params != NULL)
2864 name = without_params.get ();
2865 }
2866 }
2867
2868 /* Index version 4 did not support case insensitive searches. But the
2869 indices for case insensitive languages are built in lowercase, therefore
2870 simulate our NAME being searched is also lowercased. */
2871 hash = mapped_index_string_hash ((index->version == 4
2872 && case_sensitivity == case_sensitive_off
2873 ? 5 : index->version),
2874 name);
2875
2876 slot = hash & (index->symbol_table.size () - 1);
2877 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2878 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2879
2880 for (;;)
2881 {
2882 const char *str;
2883
2884 const auto &bucket = index->symbol_table[slot];
2885 if (bucket.name == 0 && bucket.vec == 0)
2886 return false;
2887
2888 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2889 if (!cmp (name, str))
2890 {
2891 *vec_out = (offset_type *) (index->constant_pool
2892 + MAYBE_SWAP (bucket.vec));
2893 return true;
2894 }
2895
2896 slot = (slot + step) & (index->symbol_table.size () - 1);
2897 }
2898 }
2899
2900 /* A helper function that reads the .gdb_index from BUFFER and fills
2901 in MAP. FILENAME is the name of the file containing the data;
2902 it is used for error reporting. DEPRECATED_OK is true if it is
2903 ok to use deprecated sections.
2904
2905 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2906 out parameters that are filled in with information about the CU and
2907 TU lists in the section.
2908
2909 Returns true if all went well, false otherwise. */
2910
2911 static bool
2912 read_gdb_index_from_buffer (struct objfile *objfile,
2913 const char *filename,
2914 bool deprecated_ok,
2915 gdb::array_view<const gdb_byte> buffer,
2916 struct mapped_index *map,
2917 const gdb_byte **cu_list,
2918 offset_type *cu_list_elements,
2919 const gdb_byte **types_list,
2920 offset_type *types_list_elements)
2921 {
2922 const gdb_byte *addr = &buffer[0];
2923
2924 /* Version check. */
2925 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2926 /* Versions earlier than 3 emitted every copy of a psymbol. This
2927 causes the index to behave very poorly for certain requests. Version 3
2928 contained incomplete addrmap. So, it seems better to just ignore such
2929 indices. */
2930 if (version < 4)
2931 {
2932 static int warning_printed = 0;
2933 if (!warning_printed)
2934 {
2935 warning (_("Skipping obsolete .gdb_index section in %s."),
2936 filename);
2937 warning_printed = 1;
2938 }
2939 return 0;
2940 }
2941 /* Index version 4 uses a different hash function than index version
2942 5 and later.
2943
2944 Versions earlier than 6 did not emit psymbols for inlined
2945 functions. Using these files will cause GDB not to be able to
2946 set breakpoints on inlined functions by name, so we ignore these
2947 indices unless the user has done
2948 "set use-deprecated-index-sections on". */
2949 if (version < 6 && !deprecated_ok)
2950 {
2951 static int warning_printed = 0;
2952 if (!warning_printed)
2953 {
2954 warning (_("\
2955 Skipping deprecated .gdb_index section in %s.\n\
2956 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2957 to use the section anyway."),
2958 filename);
2959 warning_printed = 1;
2960 }
2961 return 0;
2962 }
2963 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2964 of the TU (for symbols coming from TUs),
2965 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2966 Plus gold-generated indices can have duplicate entries for global symbols,
2967 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2968 These are just performance bugs, and we can't distinguish gdb-generated
2969 indices from gold-generated ones, so issue no warning here. */
2970
2971 /* Indexes with higher version than the one supported by GDB may be no
2972 longer backward compatible. */
2973 if (version > 8)
2974 return 0;
2975
2976 map->version = version;
2977
2978 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2979
2980 int i = 0;
2981 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2982 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2983 / 8);
2984 ++i;
2985
2986 *types_list = addr + MAYBE_SWAP (metadata[i]);
2987 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2988 - MAYBE_SWAP (metadata[i]))
2989 / 8);
2990 ++i;
2991
2992 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2993 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2994 map->address_table
2995 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2996 ++i;
2997
2998 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2999 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3000 map->symbol_table
3001 = gdb::array_view<mapped_index::symbol_table_slot>
3002 ((mapped_index::symbol_table_slot *) symbol_table,
3003 (mapped_index::symbol_table_slot *) symbol_table_end);
3004
3005 ++i;
3006 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3007
3008 return 1;
3009 }
3010
3011 /* Callback types for dwarf2_read_gdb_index. */
3012
3013 typedef gdb::function_view
3014 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3015 get_gdb_index_contents_ftype;
3016 typedef gdb::function_view
3017 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3018 get_gdb_index_contents_dwz_ftype;
3019
3020 /* Read .gdb_index. If everything went ok, initialize the "quick"
3021 elements of all the CUs and return 1. Otherwise, return 0. */
3022
3023 static int
3024 dwarf2_read_gdb_index
3025 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3026 get_gdb_index_contents_ftype get_gdb_index_contents,
3027 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3028 {
3029 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3030 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3031 struct dwz_file *dwz;
3032 struct objfile *objfile = dwarf2_per_objfile->objfile;
3033
3034 gdb::array_view<const gdb_byte> main_index_contents
3035 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3036
3037 if (main_index_contents.empty ())
3038 return 0;
3039
3040 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3041 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3042 use_deprecated_index_sections,
3043 main_index_contents, map.get (), &cu_list,
3044 &cu_list_elements, &types_list,
3045 &types_list_elements))
3046 return 0;
3047
3048 /* Don't use the index if it's empty. */
3049 if (map->symbol_table.empty ())
3050 return 0;
3051
3052 /* If there is a .dwz file, read it so we can get its CU list as
3053 well. */
3054 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3055 if (dwz != NULL)
3056 {
3057 struct mapped_index dwz_map;
3058 const gdb_byte *dwz_types_ignore;
3059 offset_type dwz_types_elements_ignore;
3060
3061 gdb::array_view<const gdb_byte> dwz_index_content
3062 = get_gdb_index_contents_dwz (objfile, dwz);
3063
3064 if (dwz_index_content.empty ())
3065 return 0;
3066
3067 if (!read_gdb_index_from_buffer (objfile,
3068 bfd_get_filename (dwz->dwz_bfd.get ()),
3069 1, dwz_index_content, &dwz_map,
3070 &dwz_list, &dwz_list_elements,
3071 &dwz_types_ignore,
3072 &dwz_types_elements_ignore))
3073 {
3074 warning (_("could not read '.gdb_index' section from %s; skipping"),
3075 bfd_get_filename (dwz->dwz_bfd.get ()));
3076 return 0;
3077 }
3078 }
3079
3080 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3081 dwz_list, dwz_list_elements);
3082
3083 if (types_list_elements)
3084 {
3085 /* We can only handle a single .debug_types when we have an
3086 index. */
3087 if (dwarf2_per_objfile->types.size () != 1)
3088 return 0;
3089
3090 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3091
3092 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3093 types_list, types_list_elements);
3094 }
3095
3096 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3097
3098 dwarf2_per_objfile->index_table = std::move (map);
3099 dwarf2_per_objfile->using_index = 1;
3100 dwarf2_per_objfile->quick_file_names_table =
3101 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3102
3103 return 1;
3104 }
3105
3106 /* die_reader_func for dw2_get_file_names. */
3107
3108 static void
3109 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3110 const gdb_byte *info_ptr,
3111 struct die_info *comp_unit_die)
3112 {
3113 struct dwarf2_cu *cu = reader->cu;
3114 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3115 struct dwarf2_per_objfile *dwarf2_per_objfile
3116 = cu->per_cu->dwarf2_per_objfile;
3117 struct objfile *objfile = dwarf2_per_objfile->objfile;
3118 struct dwarf2_per_cu_data *lh_cu;
3119 struct attribute *attr;
3120 void **slot;
3121 struct quick_file_names *qfn;
3122
3123 gdb_assert (! this_cu->is_debug_types);
3124
3125 /* Our callers never want to match partial units -- instead they
3126 will match the enclosing full CU. */
3127 if (comp_unit_die->tag == DW_TAG_partial_unit)
3128 {
3129 this_cu->v.quick->no_file_data = 1;
3130 return;
3131 }
3132
3133 lh_cu = this_cu;
3134 slot = NULL;
3135
3136 line_header_up lh;
3137 sect_offset line_offset {};
3138
3139 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3140 if (attr != nullptr)
3141 {
3142 struct quick_file_names find_entry;
3143
3144 line_offset = (sect_offset) DW_UNSND (attr);
3145
3146 /* We may have already read in this line header (TU line header sharing).
3147 If we have we're done. */
3148 find_entry.hash.dwo_unit = cu->dwo_unit;
3149 find_entry.hash.line_sect_off = line_offset;
3150 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3151 &find_entry, INSERT);
3152 if (*slot != NULL)
3153 {
3154 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3155 return;
3156 }
3157
3158 lh = dwarf_decode_line_header (line_offset, cu);
3159 }
3160 if (lh == NULL)
3161 {
3162 lh_cu->v.quick->no_file_data = 1;
3163 return;
3164 }
3165
3166 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3167 qfn->hash.dwo_unit = cu->dwo_unit;
3168 qfn->hash.line_sect_off = line_offset;
3169 gdb_assert (slot != NULL);
3170 *slot = qfn;
3171
3172 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3173
3174 int offset = 0;
3175 if (strcmp (fnd.name, "<unknown>") != 0)
3176 ++offset;
3177
3178 qfn->num_file_names = offset + lh->file_names_size ();
3179 qfn->file_names =
3180 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3181 if (offset != 0)
3182 qfn->file_names[0] = xstrdup (fnd.name);
3183 for (int i = 0; i < lh->file_names_size (); ++i)
3184 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3185 fnd.comp_dir).release ();
3186 qfn->real_names = NULL;
3187
3188 lh_cu->v.quick->file_names = qfn;
3189 }
3190
3191 /* A helper for the "quick" functions which attempts to read the line
3192 table for THIS_CU. */
3193
3194 static struct quick_file_names *
3195 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3196 {
3197 /* This should never be called for TUs. */
3198 gdb_assert (! this_cu->is_debug_types);
3199 /* Nor type unit groups. */
3200 gdb_assert (! this_cu->type_unit_group_p ());
3201
3202 if (this_cu->v.quick->file_names != NULL)
3203 return this_cu->v.quick->file_names;
3204 /* If we know there is no line data, no point in looking again. */
3205 if (this_cu->v.quick->no_file_data)
3206 return NULL;
3207
3208 cutu_reader reader (this_cu);
3209 if (!reader.dummy_p)
3210 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3211
3212 if (this_cu->v.quick->no_file_data)
3213 return NULL;
3214 return this_cu->v.quick->file_names;
3215 }
3216
3217 /* A helper for the "quick" functions which computes and caches the
3218 real path for a given file name from the line table. */
3219
3220 static const char *
3221 dw2_get_real_path (struct objfile *objfile,
3222 struct quick_file_names *qfn, int index)
3223 {
3224 if (qfn->real_names == NULL)
3225 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3226 qfn->num_file_names, const char *);
3227
3228 if (qfn->real_names[index] == NULL)
3229 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3230
3231 return qfn->real_names[index];
3232 }
3233
3234 static struct symtab *
3235 dw2_find_last_source_symtab (struct objfile *objfile)
3236 {
3237 struct dwarf2_per_objfile *dwarf2_per_objfile
3238 = get_dwarf2_per_objfile (objfile);
3239 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3240 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3241
3242 if (cust == NULL)
3243 return NULL;
3244
3245 return compunit_primary_filetab (cust);
3246 }
3247
3248 /* Traversal function for dw2_forget_cached_source_info. */
3249
3250 static int
3251 dw2_free_cached_file_names (void **slot, void *info)
3252 {
3253 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3254
3255 if (file_data->real_names)
3256 {
3257 int i;
3258
3259 for (i = 0; i < file_data->num_file_names; ++i)
3260 {
3261 xfree ((void*) file_data->real_names[i]);
3262 file_data->real_names[i] = NULL;
3263 }
3264 }
3265
3266 return 1;
3267 }
3268
3269 static void
3270 dw2_forget_cached_source_info (struct objfile *objfile)
3271 {
3272 struct dwarf2_per_objfile *dwarf2_per_objfile
3273 = get_dwarf2_per_objfile (objfile);
3274
3275 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3276 dw2_free_cached_file_names, NULL);
3277 }
3278
3279 /* Helper function for dw2_map_symtabs_matching_filename that expands
3280 the symtabs and calls the iterator. */
3281
3282 static int
3283 dw2_map_expand_apply (struct objfile *objfile,
3284 struct dwarf2_per_cu_data *per_cu,
3285 const char *name, const char *real_path,
3286 gdb::function_view<bool (symtab *)> callback)
3287 {
3288 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3289
3290 /* Don't visit already-expanded CUs. */
3291 if (per_cu->v.quick->compunit_symtab)
3292 return 0;
3293
3294 /* This may expand more than one symtab, and we want to iterate over
3295 all of them. */
3296 dw2_instantiate_symtab (per_cu, false);
3297
3298 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3299 last_made, callback);
3300 }
3301
3302 /* Implementation of the map_symtabs_matching_filename method. */
3303
3304 static bool
3305 dw2_map_symtabs_matching_filename
3306 (struct objfile *objfile, const char *name, const char *real_path,
3307 gdb::function_view<bool (symtab *)> callback)
3308 {
3309 const char *name_basename = lbasename (name);
3310 struct dwarf2_per_objfile *dwarf2_per_objfile
3311 = get_dwarf2_per_objfile (objfile);
3312
3313 /* The rule is CUs specify all the files, including those used by
3314 any TU, so there's no need to scan TUs here. */
3315
3316 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3317 {
3318 /* We only need to look at symtabs not already expanded. */
3319 if (per_cu->v.quick->compunit_symtab)
3320 continue;
3321
3322 quick_file_names *file_data = dw2_get_file_names (per_cu);
3323 if (file_data == NULL)
3324 continue;
3325
3326 for (int j = 0; j < file_data->num_file_names; ++j)
3327 {
3328 const char *this_name = file_data->file_names[j];
3329 const char *this_real_name;
3330
3331 if (compare_filenames_for_search (this_name, name))
3332 {
3333 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3334 callback))
3335 return true;
3336 continue;
3337 }
3338
3339 /* Before we invoke realpath, which can get expensive when many
3340 files are involved, do a quick comparison of the basenames. */
3341 if (! basenames_may_differ
3342 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3343 continue;
3344
3345 this_real_name = dw2_get_real_path (objfile, file_data, j);
3346 if (compare_filenames_for_search (this_real_name, name))
3347 {
3348 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3349 callback))
3350 return true;
3351 continue;
3352 }
3353
3354 if (real_path != NULL)
3355 {
3356 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3357 gdb_assert (IS_ABSOLUTE_PATH (name));
3358 if (this_real_name != NULL
3359 && FILENAME_CMP (real_path, this_real_name) == 0)
3360 {
3361 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3362 callback))
3363 return true;
3364 continue;
3365 }
3366 }
3367 }
3368 }
3369
3370 return false;
3371 }
3372
3373 /* Struct used to manage iterating over all CUs looking for a symbol. */
3374
3375 struct dw2_symtab_iterator
3376 {
3377 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3378 struct dwarf2_per_objfile *dwarf2_per_objfile;
3379 /* If set, only look for symbols that match that block. Valid values are
3380 GLOBAL_BLOCK and STATIC_BLOCK. */
3381 gdb::optional<block_enum> block_index;
3382 /* The kind of symbol we're looking for. */
3383 domain_enum domain;
3384 /* The list of CUs from the index entry of the symbol,
3385 or NULL if not found. */
3386 offset_type *vec;
3387 /* The next element in VEC to look at. */
3388 int next;
3389 /* The number of elements in VEC, or zero if there is no match. */
3390 int length;
3391 /* Have we seen a global version of the symbol?
3392 If so we can ignore all further global instances.
3393 This is to work around gold/15646, inefficient gold-generated
3394 indices. */
3395 int global_seen;
3396 };
3397
3398 /* Initialize the index symtab iterator ITER. */
3399
3400 static void
3401 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3402 struct dwarf2_per_objfile *dwarf2_per_objfile,
3403 gdb::optional<block_enum> block_index,
3404 domain_enum domain,
3405 const char *name)
3406 {
3407 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3408 iter->block_index = block_index;
3409 iter->domain = domain;
3410 iter->next = 0;
3411 iter->global_seen = 0;
3412
3413 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3414
3415 /* index is NULL if OBJF_READNOW. */
3416 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3417 iter->length = MAYBE_SWAP (*iter->vec);
3418 else
3419 {
3420 iter->vec = NULL;
3421 iter->length = 0;
3422 }
3423 }
3424
3425 /* Return the next matching CU or NULL if there are no more. */
3426
3427 static struct dwarf2_per_cu_data *
3428 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3429 {
3430 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3431
3432 for ( ; iter->next < iter->length; ++iter->next)
3433 {
3434 offset_type cu_index_and_attrs =
3435 MAYBE_SWAP (iter->vec[iter->next + 1]);
3436 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3437 gdb_index_symbol_kind symbol_kind =
3438 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3439 /* Only check the symbol attributes if they're present.
3440 Indices prior to version 7 don't record them,
3441 and indices >= 7 may elide them for certain symbols
3442 (gold does this). */
3443 int attrs_valid =
3444 (dwarf2_per_objfile->index_table->version >= 7
3445 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3446
3447 /* Don't crash on bad data. */
3448 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3449 + dwarf2_per_objfile->all_type_units.size ()))
3450 {
3451 complaint (_(".gdb_index entry has bad CU index"
3452 " [in module %s]"),
3453 objfile_name (dwarf2_per_objfile->objfile));
3454 continue;
3455 }
3456
3457 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3458
3459 /* Skip if already read in. */
3460 if (per_cu->v.quick->compunit_symtab)
3461 continue;
3462
3463 /* Check static vs global. */
3464 if (attrs_valid)
3465 {
3466 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3467
3468 if (iter->block_index.has_value ())
3469 {
3470 bool want_static = *iter->block_index == STATIC_BLOCK;
3471
3472 if (is_static != want_static)
3473 continue;
3474 }
3475
3476 /* Work around gold/15646. */
3477 if (!is_static && iter->global_seen)
3478 continue;
3479 if (!is_static)
3480 iter->global_seen = 1;
3481 }
3482
3483 /* Only check the symbol's kind if it has one. */
3484 if (attrs_valid)
3485 {
3486 switch (iter->domain)
3487 {
3488 case VAR_DOMAIN:
3489 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3490 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3491 /* Some types are also in VAR_DOMAIN. */
3492 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3493 continue;
3494 break;
3495 case STRUCT_DOMAIN:
3496 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3497 continue;
3498 break;
3499 case LABEL_DOMAIN:
3500 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3501 continue;
3502 break;
3503 case MODULE_DOMAIN:
3504 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3505 continue;
3506 break;
3507 default:
3508 break;
3509 }
3510 }
3511
3512 ++iter->next;
3513 return per_cu;
3514 }
3515
3516 return NULL;
3517 }
3518
3519 static struct compunit_symtab *
3520 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3521 const char *name, domain_enum domain)
3522 {
3523 struct compunit_symtab *stab_best = NULL;
3524 struct dwarf2_per_objfile *dwarf2_per_objfile
3525 = get_dwarf2_per_objfile (objfile);
3526
3527 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3528
3529 struct dw2_symtab_iterator iter;
3530 struct dwarf2_per_cu_data *per_cu;
3531
3532 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3533
3534 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3535 {
3536 struct symbol *sym, *with_opaque = NULL;
3537 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3538 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3539 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3540
3541 sym = block_find_symbol (block, name, domain,
3542 block_find_non_opaque_type_preferred,
3543 &with_opaque);
3544
3545 /* Some caution must be observed with overloaded functions
3546 and methods, since the index will not contain any overload
3547 information (but NAME might contain it). */
3548
3549 if (sym != NULL
3550 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3551 return stab;
3552 if (with_opaque != NULL
3553 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3554 stab_best = stab;
3555
3556 /* Keep looking through other CUs. */
3557 }
3558
3559 return stab_best;
3560 }
3561
3562 static void
3563 dw2_print_stats (struct objfile *objfile)
3564 {
3565 struct dwarf2_per_objfile *dwarf2_per_objfile
3566 = get_dwarf2_per_objfile (objfile);
3567 int total = (dwarf2_per_objfile->all_comp_units.size ()
3568 + dwarf2_per_objfile->all_type_units.size ());
3569 int count = 0;
3570
3571 for (int i = 0; i < total; ++i)
3572 {
3573 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3574
3575 if (!per_cu->v.quick->compunit_symtab)
3576 ++count;
3577 }
3578 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3579 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3580 }
3581
3582 /* This dumps minimal information about the index.
3583 It is called via "mt print objfiles".
3584 One use is to verify .gdb_index has been loaded by the
3585 gdb.dwarf2/gdb-index.exp testcase. */
3586
3587 static void
3588 dw2_dump (struct objfile *objfile)
3589 {
3590 struct dwarf2_per_objfile *dwarf2_per_objfile
3591 = get_dwarf2_per_objfile (objfile);
3592
3593 gdb_assert (dwarf2_per_objfile->using_index);
3594 printf_filtered (".gdb_index:");
3595 if (dwarf2_per_objfile->index_table != NULL)
3596 {
3597 printf_filtered (" version %d\n",
3598 dwarf2_per_objfile->index_table->version);
3599 }
3600 else
3601 printf_filtered (" faked for \"readnow\"\n");
3602 printf_filtered ("\n");
3603 }
3604
3605 static void
3606 dw2_expand_symtabs_for_function (struct objfile *objfile,
3607 const char *func_name)
3608 {
3609 struct dwarf2_per_objfile *dwarf2_per_objfile
3610 = get_dwarf2_per_objfile (objfile);
3611
3612 struct dw2_symtab_iterator iter;
3613 struct dwarf2_per_cu_data *per_cu;
3614
3615 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3616
3617 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3618 dw2_instantiate_symtab (per_cu, false);
3619
3620 }
3621
3622 static void
3623 dw2_expand_all_symtabs (struct objfile *objfile)
3624 {
3625 struct dwarf2_per_objfile *dwarf2_per_objfile
3626 = get_dwarf2_per_objfile (objfile);
3627 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3628 + dwarf2_per_objfile->all_type_units.size ());
3629
3630 for (int i = 0; i < total_units; ++i)
3631 {
3632 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3633
3634 /* We don't want to directly expand a partial CU, because if we
3635 read it with the wrong language, then assertion failures can
3636 be triggered later on. See PR symtab/23010. So, tell
3637 dw2_instantiate_symtab to skip partial CUs -- any important
3638 partial CU will be read via DW_TAG_imported_unit anyway. */
3639 dw2_instantiate_symtab (per_cu, true);
3640 }
3641 }
3642
3643 static void
3644 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3645 const char *fullname)
3646 {
3647 struct dwarf2_per_objfile *dwarf2_per_objfile
3648 = get_dwarf2_per_objfile (objfile);
3649
3650 /* We don't need to consider type units here.
3651 This is only called for examining code, e.g. expand_line_sal.
3652 There can be an order of magnitude (or more) more type units
3653 than comp units, and we avoid them if we can. */
3654
3655 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3656 {
3657 /* We only need to look at symtabs not already expanded. */
3658 if (per_cu->v.quick->compunit_symtab)
3659 continue;
3660
3661 quick_file_names *file_data = dw2_get_file_names (per_cu);
3662 if (file_data == NULL)
3663 continue;
3664
3665 for (int j = 0; j < file_data->num_file_names; ++j)
3666 {
3667 const char *this_fullname = file_data->file_names[j];
3668
3669 if (filename_cmp (this_fullname, fullname) == 0)
3670 {
3671 dw2_instantiate_symtab (per_cu, false);
3672 break;
3673 }
3674 }
3675 }
3676 }
3677
3678 static void
3679 dw2_map_matching_symbols
3680 (struct objfile *objfile,
3681 const lookup_name_info &name, domain_enum domain,
3682 int global,
3683 gdb::function_view<symbol_found_callback_ftype> callback,
3684 symbol_compare_ftype *ordered_compare)
3685 {
3686 /* Currently unimplemented; used for Ada. The function can be called if the
3687 current language is Ada for a non-Ada objfile using GNU index. As Ada
3688 does not look for non-Ada symbols this function should just return. */
3689 }
3690
3691 /* Starting from a search name, return the string that finds the upper
3692 bound of all strings that start with SEARCH_NAME in a sorted name
3693 list. Returns the empty string to indicate that the upper bound is
3694 the end of the list. */
3695
3696 static std::string
3697 make_sort_after_prefix_name (const char *search_name)
3698 {
3699 /* When looking to complete "func", we find the upper bound of all
3700 symbols that start with "func" by looking for where we'd insert
3701 the closest string that would follow "func" in lexicographical
3702 order. Usually, that's "func"-with-last-character-incremented,
3703 i.e. "fund". Mind non-ASCII characters, though. Usually those
3704 will be UTF-8 multi-byte sequences, but we can't be certain.
3705 Especially mind the 0xff character, which is a valid character in
3706 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3707 rule out compilers allowing it in identifiers. Note that
3708 conveniently, strcmp/strcasecmp are specified to compare
3709 characters interpreted as unsigned char. So what we do is treat
3710 the whole string as a base 256 number composed of a sequence of
3711 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3712 to 0, and carries 1 to the following more-significant position.
3713 If the very first character in SEARCH_NAME ends up incremented
3714 and carries/overflows, then the upper bound is the end of the
3715 list. The string after the empty string is also the empty
3716 string.
3717
3718 Some examples of this operation:
3719
3720 SEARCH_NAME => "+1" RESULT
3721
3722 "abc" => "abd"
3723 "ab\xff" => "ac"
3724 "\xff" "a" "\xff" => "\xff" "b"
3725 "\xff" => ""
3726 "\xff\xff" => ""
3727 "" => ""
3728
3729 Then, with these symbols for example:
3730
3731 func
3732 func1
3733 fund
3734
3735 completing "func" looks for symbols between "func" and
3736 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3737 which finds "func" and "func1", but not "fund".
3738
3739 And with:
3740
3741 funcÿ (Latin1 'ÿ' [0xff])
3742 funcÿ1
3743 fund
3744
3745 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3746 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3747
3748 And with:
3749
3750 ÿÿ (Latin1 'ÿ' [0xff])
3751 ÿÿ1
3752
3753 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3754 the end of the list.
3755 */
3756 std::string after = search_name;
3757 while (!after.empty () && (unsigned char) after.back () == 0xff)
3758 after.pop_back ();
3759 if (!after.empty ())
3760 after.back () = (unsigned char) after.back () + 1;
3761 return after;
3762 }
3763
3764 /* See declaration. */
3765
3766 std::pair<std::vector<name_component>::const_iterator,
3767 std::vector<name_component>::const_iterator>
3768 mapped_index_base::find_name_components_bounds
3769 (const lookup_name_info &lookup_name_without_params, language lang) const
3770 {
3771 auto *name_cmp
3772 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3773
3774 const char *lang_name
3775 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3776
3777 /* Comparison function object for lower_bound that matches against a
3778 given symbol name. */
3779 auto lookup_compare_lower = [&] (const name_component &elem,
3780 const char *name)
3781 {
3782 const char *elem_qualified = this->symbol_name_at (elem.idx);
3783 const char *elem_name = elem_qualified + elem.name_offset;
3784 return name_cmp (elem_name, name) < 0;
3785 };
3786
3787 /* Comparison function object for upper_bound that matches against a
3788 given symbol name. */
3789 auto lookup_compare_upper = [&] (const char *name,
3790 const name_component &elem)
3791 {
3792 const char *elem_qualified = this->symbol_name_at (elem.idx);
3793 const char *elem_name = elem_qualified + elem.name_offset;
3794 return name_cmp (name, elem_name) < 0;
3795 };
3796
3797 auto begin = this->name_components.begin ();
3798 auto end = this->name_components.end ();
3799
3800 /* Find the lower bound. */
3801 auto lower = [&] ()
3802 {
3803 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3804 return begin;
3805 else
3806 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3807 } ();
3808
3809 /* Find the upper bound. */
3810 auto upper = [&] ()
3811 {
3812 if (lookup_name_without_params.completion_mode ())
3813 {
3814 /* In completion mode, we want UPPER to point past all
3815 symbols names that have the same prefix. I.e., with
3816 these symbols, and completing "func":
3817
3818 function << lower bound
3819 function1
3820 other_function << upper bound
3821
3822 We find the upper bound by looking for the insertion
3823 point of "func"-with-last-character-incremented,
3824 i.e. "fund". */
3825 std::string after = make_sort_after_prefix_name (lang_name);
3826 if (after.empty ())
3827 return end;
3828 return std::lower_bound (lower, end, after.c_str (),
3829 lookup_compare_lower);
3830 }
3831 else
3832 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3833 } ();
3834
3835 return {lower, upper};
3836 }
3837
3838 /* See declaration. */
3839
3840 void
3841 mapped_index_base::build_name_components ()
3842 {
3843 if (!this->name_components.empty ())
3844 return;
3845
3846 this->name_components_casing = case_sensitivity;
3847 auto *name_cmp
3848 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3849
3850 /* The code below only knows how to break apart components of C++
3851 symbol names (and other languages that use '::' as
3852 namespace/module separator) and Ada symbol names. */
3853 auto count = this->symbol_name_count ();
3854 for (offset_type idx = 0; idx < count; idx++)
3855 {
3856 if (this->symbol_name_slot_invalid (idx))
3857 continue;
3858
3859 const char *name = this->symbol_name_at (idx);
3860
3861 /* Add each name component to the name component table. */
3862 unsigned int previous_len = 0;
3863
3864 if (strstr (name, "::") != nullptr)
3865 {
3866 for (unsigned int current_len = cp_find_first_component (name);
3867 name[current_len] != '\0';
3868 current_len += cp_find_first_component (name + current_len))
3869 {
3870 gdb_assert (name[current_len] == ':');
3871 this->name_components.push_back ({previous_len, idx});
3872 /* Skip the '::'. */
3873 current_len += 2;
3874 previous_len = current_len;
3875 }
3876 }
3877 else
3878 {
3879 /* Handle the Ada encoded (aka mangled) form here. */
3880 for (const char *iter = strstr (name, "__");
3881 iter != nullptr;
3882 iter = strstr (iter, "__"))
3883 {
3884 this->name_components.push_back ({previous_len, idx});
3885 iter += 2;
3886 previous_len = iter - name;
3887 }
3888 }
3889
3890 this->name_components.push_back ({previous_len, idx});
3891 }
3892
3893 /* Sort name_components elements by name. */
3894 auto name_comp_compare = [&] (const name_component &left,
3895 const name_component &right)
3896 {
3897 const char *left_qualified = this->symbol_name_at (left.idx);
3898 const char *right_qualified = this->symbol_name_at (right.idx);
3899
3900 const char *left_name = left_qualified + left.name_offset;
3901 const char *right_name = right_qualified + right.name_offset;
3902
3903 return name_cmp (left_name, right_name) < 0;
3904 };
3905
3906 std::sort (this->name_components.begin (),
3907 this->name_components.end (),
3908 name_comp_compare);
3909 }
3910
3911 /* Helper for dw2_expand_symtabs_matching that works with a
3912 mapped_index_base instead of the containing objfile. This is split
3913 to a separate function in order to be able to unit test the
3914 name_components matching using a mock mapped_index_base. For each
3915 symbol name that matches, calls MATCH_CALLBACK, passing it the
3916 symbol's index in the mapped_index_base symbol table. */
3917
3918 static void
3919 dw2_expand_symtabs_matching_symbol
3920 (mapped_index_base &index,
3921 const lookup_name_info &lookup_name_in,
3922 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3923 enum search_domain kind,
3924 gdb::function_view<bool (offset_type)> match_callback)
3925 {
3926 lookup_name_info lookup_name_without_params
3927 = lookup_name_in.make_ignore_params ();
3928
3929 /* Build the symbol name component sorted vector, if we haven't
3930 yet. */
3931 index.build_name_components ();
3932
3933 /* The same symbol may appear more than once in the range though.
3934 E.g., if we're looking for symbols that complete "w", and we have
3935 a symbol named "w1::w2", we'll find the two name components for
3936 that same symbol in the range. To be sure we only call the
3937 callback once per symbol, we first collect the symbol name
3938 indexes that matched in a temporary vector and ignore
3939 duplicates. */
3940 std::vector<offset_type> matches;
3941
3942 struct name_and_matcher
3943 {
3944 symbol_name_matcher_ftype *matcher;
3945 const std::string &name;
3946
3947 bool operator== (const name_and_matcher &other) const
3948 {
3949 return matcher == other.matcher && name == other.name;
3950 }
3951 };
3952
3953 /* A vector holding all the different symbol name matchers, for all
3954 languages. */
3955 std::vector<name_and_matcher> matchers;
3956
3957 for (int i = 0; i < nr_languages; i++)
3958 {
3959 enum language lang_e = (enum language) i;
3960
3961 const language_defn *lang = language_def (lang_e);
3962 symbol_name_matcher_ftype *name_matcher
3963 = get_symbol_name_matcher (lang, lookup_name_without_params);
3964
3965 name_and_matcher key {
3966 name_matcher,
3967 lookup_name_without_params.language_lookup_name (lang_e)
3968 };
3969
3970 /* Don't insert the same comparison routine more than once.
3971 Note that we do this linear walk. This is not a problem in
3972 practice because the number of supported languages is
3973 low. */
3974 if (std::find (matchers.begin (), matchers.end (), key)
3975 != matchers.end ())
3976 continue;
3977 matchers.push_back (std::move (key));
3978
3979 auto bounds
3980 = index.find_name_components_bounds (lookup_name_without_params,
3981 lang_e);
3982
3983 /* Now for each symbol name in range, check to see if we have a name
3984 match, and if so, call the MATCH_CALLBACK callback. */
3985
3986 for (; bounds.first != bounds.second; ++bounds.first)
3987 {
3988 const char *qualified = index.symbol_name_at (bounds.first->idx);
3989
3990 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3991 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3992 continue;
3993
3994 matches.push_back (bounds.first->idx);
3995 }
3996 }
3997
3998 std::sort (matches.begin (), matches.end ());
3999
4000 /* Finally call the callback, once per match. */
4001 ULONGEST prev = -1;
4002 for (offset_type idx : matches)
4003 {
4004 if (prev != idx)
4005 {
4006 if (!match_callback (idx))
4007 break;
4008 prev = idx;
4009 }
4010 }
4011
4012 /* Above we use a type wider than idx's for 'prev', since 0 and
4013 (offset_type)-1 are both possible values. */
4014 static_assert (sizeof (prev) > sizeof (offset_type), "");
4015 }
4016
4017 #if GDB_SELF_TEST
4018
4019 namespace selftests { namespace dw2_expand_symtabs_matching {
4020
4021 /* A mock .gdb_index/.debug_names-like name index table, enough to
4022 exercise dw2_expand_symtabs_matching_symbol, which works with the
4023 mapped_index_base interface. Builds an index from the symbol list
4024 passed as parameter to the constructor. */
4025 class mock_mapped_index : public mapped_index_base
4026 {
4027 public:
4028 mock_mapped_index (gdb::array_view<const char *> symbols)
4029 : m_symbol_table (symbols)
4030 {}
4031
4032 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4033
4034 /* Return the number of names in the symbol table. */
4035 size_t symbol_name_count () const override
4036 {
4037 return m_symbol_table.size ();
4038 }
4039
4040 /* Get the name of the symbol at IDX in the symbol table. */
4041 const char *symbol_name_at (offset_type idx) const override
4042 {
4043 return m_symbol_table[idx];
4044 }
4045
4046 private:
4047 gdb::array_view<const char *> m_symbol_table;
4048 };
4049
4050 /* Convenience function that converts a NULL pointer to a "<null>"
4051 string, to pass to print routines. */
4052
4053 static const char *
4054 string_or_null (const char *str)
4055 {
4056 return str != NULL ? str : "<null>";
4057 }
4058
4059 /* Check if a lookup_name_info built from
4060 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4061 index. EXPECTED_LIST is the list of expected matches, in expected
4062 matching order. If no match expected, then an empty list is
4063 specified. Returns true on success. On failure prints a warning
4064 indicating the file:line that failed, and returns false. */
4065
4066 static bool
4067 check_match (const char *file, int line,
4068 mock_mapped_index &mock_index,
4069 const char *name, symbol_name_match_type match_type,
4070 bool completion_mode,
4071 std::initializer_list<const char *> expected_list)
4072 {
4073 lookup_name_info lookup_name (name, match_type, completion_mode);
4074
4075 bool matched = true;
4076
4077 auto mismatch = [&] (const char *expected_str,
4078 const char *got)
4079 {
4080 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4081 "expected=\"%s\", got=\"%s\"\n"),
4082 file, line,
4083 (match_type == symbol_name_match_type::FULL
4084 ? "FULL" : "WILD"),
4085 name, string_or_null (expected_str), string_or_null (got));
4086 matched = false;
4087 };
4088
4089 auto expected_it = expected_list.begin ();
4090 auto expected_end = expected_list.end ();
4091
4092 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4093 NULL, ALL_DOMAIN,
4094 [&] (offset_type idx)
4095 {
4096 const char *matched_name = mock_index.symbol_name_at (idx);
4097 const char *expected_str
4098 = expected_it == expected_end ? NULL : *expected_it++;
4099
4100 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4101 mismatch (expected_str, matched_name);
4102 return true;
4103 });
4104
4105 const char *expected_str
4106 = expected_it == expected_end ? NULL : *expected_it++;
4107 if (expected_str != NULL)
4108 mismatch (expected_str, NULL);
4109
4110 return matched;
4111 }
4112
4113 /* The symbols added to the mock mapped_index for testing (in
4114 canonical form). */
4115 static const char *test_symbols[] = {
4116 "function",
4117 "std::bar",
4118 "std::zfunction",
4119 "std::zfunction2",
4120 "w1::w2",
4121 "ns::foo<char*>",
4122 "ns::foo<int>",
4123 "ns::foo<long>",
4124 "ns2::tmpl<int>::foo2",
4125 "(anonymous namespace)::A::B::C",
4126
4127 /* These are used to check that the increment-last-char in the
4128 matching algorithm for completion doesn't match "t1_fund" when
4129 completing "t1_func". */
4130 "t1_func",
4131 "t1_func1",
4132 "t1_fund",
4133 "t1_fund1",
4134
4135 /* A UTF-8 name with multi-byte sequences to make sure that
4136 cp-name-parser understands this as a single identifier ("função"
4137 is "function" in PT). */
4138 u8"u8função",
4139
4140 /* \377 (0xff) is Latin1 'ÿ'. */
4141 "yfunc\377",
4142
4143 /* \377 (0xff) is Latin1 'ÿ'. */
4144 "\377",
4145 "\377\377123",
4146
4147 /* A name with all sorts of complications. Starts with "z" to make
4148 it easier for the completion tests below. */
4149 #define Z_SYM_NAME \
4150 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4151 "::tuple<(anonymous namespace)::ui*, " \
4152 "std::default_delete<(anonymous namespace)::ui>, void>"
4153
4154 Z_SYM_NAME
4155 };
4156
4157 /* Returns true if the mapped_index_base::find_name_component_bounds
4158 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4159 in completion mode. */
4160
4161 static bool
4162 check_find_bounds_finds (mapped_index_base &index,
4163 const char *search_name,
4164 gdb::array_view<const char *> expected_syms)
4165 {
4166 lookup_name_info lookup_name (search_name,
4167 symbol_name_match_type::FULL, true);
4168
4169 auto bounds = index.find_name_components_bounds (lookup_name,
4170 language_cplus);
4171
4172 size_t distance = std::distance (bounds.first, bounds.second);
4173 if (distance != expected_syms.size ())
4174 return false;
4175
4176 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4177 {
4178 auto nc_elem = bounds.first + exp_elem;
4179 const char *qualified = index.symbol_name_at (nc_elem->idx);
4180 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4181 return false;
4182 }
4183
4184 return true;
4185 }
4186
4187 /* Test the lower-level mapped_index::find_name_component_bounds
4188 method. */
4189
4190 static void
4191 test_mapped_index_find_name_component_bounds ()
4192 {
4193 mock_mapped_index mock_index (test_symbols);
4194
4195 mock_index.build_name_components ();
4196
4197 /* Test the lower-level mapped_index::find_name_component_bounds
4198 method in completion mode. */
4199 {
4200 static const char *expected_syms[] = {
4201 "t1_func",
4202 "t1_func1",
4203 };
4204
4205 SELF_CHECK (check_find_bounds_finds (mock_index,
4206 "t1_func", expected_syms));
4207 }
4208
4209 /* Check that the increment-last-char in the name matching algorithm
4210 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4211 {
4212 static const char *expected_syms1[] = {
4213 "\377",
4214 "\377\377123",
4215 };
4216 SELF_CHECK (check_find_bounds_finds (mock_index,
4217 "\377", expected_syms1));
4218
4219 static const char *expected_syms2[] = {
4220 "\377\377123",
4221 };
4222 SELF_CHECK (check_find_bounds_finds (mock_index,
4223 "\377\377", expected_syms2));
4224 }
4225 }
4226
4227 /* Test dw2_expand_symtabs_matching_symbol. */
4228
4229 static void
4230 test_dw2_expand_symtabs_matching_symbol ()
4231 {
4232 mock_mapped_index mock_index (test_symbols);
4233
4234 /* We let all tests run until the end even if some fails, for debug
4235 convenience. */
4236 bool any_mismatch = false;
4237
4238 /* Create the expected symbols list (an initializer_list). Needed
4239 because lists have commas, and we need to pass them to CHECK,
4240 which is a macro. */
4241 #define EXPECT(...) { __VA_ARGS__ }
4242
4243 /* Wrapper for check_match that passes down the current
4244 __FILE__/__LINE__. */
4245 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4246 any_mismatch |= !check_match (__FILE__, __LINE__, \
4247 mock_index, \
4248 NAME, MATCH_TYPE, COMPLETION_MODE, \
4249 EXPECTED_LIST)
4250
4251 /* Identity checks. */
4252 for (const char *sym : test_symbols)
4253 {
4254 /* Should be able to match all existing symbols. */
4255 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4256 EXPECT (sym));
4257
4258 /* Should be able to match all existing symbols with
4259 parameters. */
4260 std::string with_params = std::string (sym) + "(int)";
4261 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4262 EXPECT (sym));
4263
4264 /* Should be able to match all existing symbols with
4265 parameters and qualifiers. */
4266 with_params = std::string (sym) + " ( int ) const";
4267 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4268 EXPECT (sym));
4269
4270 /* This should really find sym, but cp-name-parser.y doesn't
4271 know about lvalue/rvalue qualifiers yet. */
4272 with_params = std::string (sym) + " ( int ) &&";
4273 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4274 {});
4275 }
4276
4277 /* Check that the name matching algorithm for completion doesn't get
4278 confused with Latin1 'ÿ' / 0xff. */
4279 {
4280 static const char str[] = "\377";
4281 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4282 EXPECT ("\377", "\377\377123"));
4283 }
4284
4285 /* Check that the increment-last-char in the matching algorithm for
4286 completion doesn't match "t1_fund" when completing "t1_func". */
4287 {
4288 static const char str[] = "t1_func";
4289 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4290 EXPECT ("t1_func", "t1_func1"));
4291 }
4292
4293 /* Check that completion mode works at each prefix of the expected
4294 symbol name. */
4295 {
4296 static const char str[] = "function(int)";
4297 size_t len = strlen (str);
4298 std::string lookup;
4299
4300 for (size_t i = 1; i < len; i++)
4301 {
4302 lookup.assign (str, i);
4303 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4304 EXPECT ("function"));
4305 }
4306 }
4307
4308 /* While "w" is a prefix of both components, the match function
4309 should still only be called once. */
4310 {
4311 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4312 EXPECT ("w1::w2"));
4313 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4314 EXPECT ("w1::w2"));
4315 }
4316
4317 /* Same, with a "complicated" symbol. */
4318 {
4319 static const char str[] = Z_SYM_NAME;
4320 size_t len = strlen (str);
4321 std::string lookup;
4322
4323 for (size_t i = 1; i < len; i++)
4324 {
4325 lookup.assign (str, i);
4326 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4327 EXPECT (Z_SYM_NAME));
4328 }
4329 }
4330
4331 /* In FULL mode, an incomplete symbol doesn't match. */
4332 {
4333 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4334 {});
4335 }
4336
4337 /* A complete symbol with parameters matches any overload, since the
4338 index has no overload info. */
4339 {
4340 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4341 EXPECT ("std::zfunction", "std::zfunction2"));
4342 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4343 EXPECT ("std::zfunction", "std::zfunction2"));
4344 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4345 EXPECT ("std::zfunction", "std::zfunction2"));
4346 }
4347
4348 /* Check that whitespace is ignored appropriately. A symbol with a
4349 template argument list. */
4350 {
4351 static const char expected[] = "ns::foo<int>";
4352 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4353 EXPECT (expected));
4354 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4355 EXPECT (expected));
4356 }
4357
4358 /* Check that whitespace is ignored appropriately. A symbol with a
4359 template argument list that includes a pointer. */
4360 {
4361 static const char expected[] = "ns::foo<char*>";
4362 /* Try both completion and non-completion modes. */
4363 static const bool completion_mode[2] = {false, true};
4364 for (size_t i = 0; i < 2; i++)
4365 {
4366 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4367 completion_mode[i], EXPECT (expected));
4368 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4369 completion_mode[i], EXPECT (expected));
4370
4371 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4372 completion_mode[i], EXPECT (expected));
4373 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4374 completion_mode[i], EXPECT (expected));
4375 }
4376 }
4377
4378 {
4379 /* Check method qualifiers are ignored. */
4380 static const char expected[] = "ns::foo<char*>";
4381 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4382 symbol_name_match_type::FULL, true, EXPECT (expected));
4383 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4384 symbol_name_match_type::FULL, true, EXPECT (expected));
4385 CHECK_MATCH ("foo < char * > ( int ) const",
4386 symbol_name_match_type::WILD, true, EXPECT (expected));
4387 CHECK_MATCH ("foo < char * > ( int ) &&",
4388 symbol_name_match_type::WILD, true, EXPECT (expected));
4389 }
4390
4391 /* Test lookup names that don't match anything. */
4392 {
4393 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4394 {});
4395
4396 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4397 {});
4398 }
4399
4400 /* Some wild matching tests, exercising "(anonymous namespace)",
4401 which should not be confused with a parameter list. */
4402 {
4403 static const char *syms[] = {
4404 "A::B::C",
4405 "B::C",
4406 "C",
4407 "A :: B :: C ( int )",
4408 "B :: C ( int )",
4409 "C ( int )",
4410 };
4411
4412 for (const char *s : syms)
4413 {
4414 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4415 EXPECT ("(anonymous namespace)::A::B::C"));
4416 }
4417 }
4418
4419 {
4420 static const char expected[] = "ns2::tmpl<int>::foo2";
4421 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4422 EXPECT (expected));
4423 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4424 EXPECT (expected));
4425 }
4426
4427 SELF_CHECK (!any_mismatch);
4428
4429 #undef EXPECT
4430 #undef CHECK_MATCH
4431 }
4432
4433 static void
4434 run_test ()
4435 {
4436 test_mapped_index_find_name_component_bounds ();
4437 test_dw2_expand_symtabs_matching_symbol ();
4438 }
4439
4440 }} // namespace selftests::dw2_expand_symtabs_matching
4441
4442 #endif /* GDB_SELF_TEST */
4443
4444 /* If FILE_MATCHER is NULL or if PER_CU has
4445 dwarf2_per_cu_quick_data::MARK set (see
4446 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4447 EXPANSION_NOTIFY on it. */
4448
4449 static void
4450 dw2_expand_symtabs_matching_one
4451 (struct dwarf2_per_cu_data *per_cu,
4452 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4453 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4454 {
4455 if (file_matcher == NULL || per_cu->v.quick->mark)
4456 {
4457 bool symtab_was_null
4458 = (per_cu->v.quick->compunit_symtab == NULL);
4459
4460 dw2_instantiate_symtab (per_cu, false);
4461
4462 if (expansion_notify != NULL
4463 && symtab_was_null
4464 && per_cu->v.quick->compunit_symtab != NULL)
4465 expansion_notify (per_cu->v.quick->compunit_symtab);
4466 }
4467 }
4468
4469 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4470 matched, to expand corresponding CUs that were marked. IDX is the
4471 index of the symbol name that matched. */
4472
4473 static void
4474 dw2_expand_marked_cus
4475 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4476 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4477 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4478 search_domain kind)
4479 {
4480 offset_type *vec, vec_len, vec_idx;
4481 bool global_seen = false;
4482 mapped_index &index = *dwarf2_per_objfile->index_table;
4483
4484 vec = (offset_type *) (index.constant_pool
4485 + MAYBE_SWAP (index.symbol_table[idx].vec));
4486 vec_len = MAYBE_SWAP (vec[0]);
4487 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4488 {
4489 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4490 /* This value is only valid for index versions >= 7. */
4491 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4492 gdb_index_symbol_kind symbol_kind =
4493 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4494 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4495 /* Only check the symbol attributes if they're present.
4496 Indices prior to version 7 don't record them,
4497 and indices >= 7 may elide them for certain symbols
4498 (gold does this). */
4499 int attrs_valid =
4500 (index.version >= 7
4501 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4502
4503 /* Work around gold/15646. */
4504 if (attrs_valid)
4505 {
4506 if (!is_static && global_seen)
4507 continue;
4508 if (!is_static)
4509 global_seen = true;
4510 }
4511
4512 /* Only check the symbol's kind if it has one. */
4513 if (attrs_valid)
4514 {
4515 switch (kind)
4516 {
4517 case VARIABLES_DOMAIN:
4518 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4519 continue;
4520 break;
4521 case FUNCTIONS_DOMAIN:
4522 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4523 continue;
4524 break;
4525 case TYPES_DOMAIN:
4526 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4527 continue;
4528 break;
4529 case MODULES_DOMAIN:
4530 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4531 continue;
4532 break;
4533 default:
4534 break;
4535 }
4536 }
4537
4538 /* Don't crash on bad data. */
4539 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4540 + dwarf2_per_objfile->all_type_units.size ()))
4541 {
4542 complaint (_(".gdb_index entry has bad CU index"
4543 " [in module %s]"),
4544 objfile_name (dwarf2_per_objfile->objfile));
4545 continue;
4546 }
4547
4548 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4549 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4550 expansion_notify);
4551 }
4552 }
4553
4554 /* If FILE_MATCHER is non-NULL, set all the
4555 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4556 that match FILE_MATCHER. */
4557
4558 static void
4559 dw_expand_symtabs_matching_file_matcher
4560 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4561 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4562 {
4563 if (file_matcher == NULL)
4564 return;
4565
4566 objfile *const objfile = dwarf2_per_objfile->objfile;
4567
4568 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4569 htab_eq_pointer,
4570 NULL, xcalloc, xfree));
4571 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4572 htab_eq_pointer,
4573 NULL, xcalloc, xfree));
4574
4575 /* The rule is CUs specify all the files, including those used by
4576 any TU, so there's no need to scan TUs here. */
4577
4578 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4579 {
4580 QUIT;
4581
4582 per_cu->v.quick->mark = 0;
4583
4584 /* We only need to look at symtabs not already expanded. */
4585 if (per_cu->v.quick->compunit_symtab)
4586 continue;
4587
4588 quick_file_names *file_data = dw2_get_file_names (per_cu);
4589 if (file_data == NULL)
4590 continue;
4591
4592 if (htab_find (visited_not_found.get (), file_data) != NULL)
4593 continue;
4594 else if (htab_find (visited_found.get (), file_data) != NULL)
4595 {
4596 per_cu->v.quick->mark = 1;
4597 continue;
4598 }
4599
4600 for (int j = 0; j < file_data->num_file_names; ++j)
4601 {
4602 const char *this_real_name;
4603
4604 if (file_matcher (file_data->file_names[j], false))
4605 {
4606 per_cu->v.quick->mark = 1;
4607 break;
4608 }
4609
4610 /* Before we invoke realpath, which can get expensive when many
4611 files are involved, do a quick comparison of the basenames. */
4612 if (!basenames_may_differ
4613 && !file_matcher (lbasename (file_data->file_names[j]),
4614 true))
4615 continue;
4616
4617 this_real_name = dw2_get_real_path (objfile, file_data, j);
4618 if (file_matcher (this_real_name, false))
4619 {
4620 per_cu->v.quick->mark = 1;
4621 break;
4622 }
4623 }
4624
4625 void **slot = htab_find_slot (per_cu->v.quick->mark
4626 ? visited_found.get ()
4627 : visited_not_found.get (),
4628 file_data, INSERT);
4629 *slot = file_data;
4630 }
4631 }
4632
4633 static void
4634 dw2_expand_symtabs_matching
4635 (struct objfile *objfile,
4636 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4637 const lookup_name_info &lookup_name,
4638 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4639 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4640 enum search_domain kind)
4641 {
4642 struct dwarf2_per_objfile *dwarf2_per_objfile
4643 = get_dwarf2_per_objfile (objfile);
4644
4645 /* index_table is NULL if OBJF_READNOW. */
4646 if (!dwarf2_per_objfile->index_table)
4647 return;
4648
4649 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4650
4651 mapped_index &index = *dwarf2_per_objfile->index_table;
4652
4653 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4654 symbol_matcher,
4655 kind, [&] (offset_type idx)
4656 {
4657 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4658 expansion_notify, kind);
4659 return true;
4660 });
4661 }
4662
4663 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4664 symtab. */
4665
4666 static struct compunit_symtab *
4667 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4668 CORE_ADDR pc)
4669 {
4670 int i;
4671
4672 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4673 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4674 return cust;
4675
4676 if (cust->includes == NULL)
4677 return NULL;
4678
4679 for (i = 0; cust->includes[i]; ++i)
4680 {
4681 struct compunit_symtab *s = cust->includes[i];
4682
4683 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4684 if (s != NULL)
4685 return s;
4686 }
4687
4688 return NULL;
4689 }
4690
4691 static struct compunit_symtab *
4692 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4693 struct bound_minimal_symbol msymbol,
4694 CORE_ADDR pc,
4695 struct obj_section *section,
4696 int warn_if_readin)
4697 {
4698 struct dwarf2_per_cu_data *data;
4699 struct compunit_symtab *result;
4700
4701 if (!objfile->partial_symtabs->psymtabs_addrmap)
4702 return NULL;
4703
4704 CORE_ADDR baseaddr = objfile->text_section_offset ();
4705 data = (struct dwarf2_per_cu_data *) addrmap_find
4706 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4707 if (!data)
4708 return NULL;
4709
4710 if (warn_if_readin && data->v.quick->compunit_symtab)
4711 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4712 paddress (get_objfile_arch (objfile), pc));
4713
4714 result
4715 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4716 false),
4717 pc);
4718 gdb_assert (result != NULL);
4719 return result;
4720 }
4721
4722 static void
4723 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4724 void *data, int need_fullname)
4725 {
4726 struct dwarf2_per_objfile *dwarf2_per_objfile
4727 = get_dwarf2_per_objfile (objfile);
4728
4729 if (!dwarf2_per_objfile->filenames_cache)
4730 {
4731 dwarf2_per_objfile->filenames_cache.emplace ();
4732
4733 htab_up visited (htab_create_alloc (10,
4734 htab_hash_pointer, htab_eq_pointer,
4735 NULL, xcalloc, xfree));
4736
4737 /* The rule is CUs specify all the files, including those used
4738 by any TU, so there's no need to scan TUs here. We can
4739 ignore file names coming from already-expanded CUs. */
4740
4741 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4742 {
4743 if (per_cu->v.quick->compunit_symtab)
4744 {
4745 void **slot = htab_find_slot (visited.get (),
4746 per_cu->v.quick->file_names,
4747 INSERT);
4748
4749 *slot = per_cu->v.quick->file_names;
4750 }
4751 }
4752
4753 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4754 {
4755 /* We only need to look at symtabs not already expanded. */
4756 if (per_cu->v.quick->compunit_symtab)
4757 continue;
4758
4759 quick_file_names *file_data = dw2_get_file_names (per_cu);
4760 if (file_data == NULL)
4761 continue;
4762
4763 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4764 if (*slot)
4765 {
4766 /* Already visited. */
4767 continue;
4768 }
4769 *slot = file_data;
4770
4771 for (int j = 0; j < file_data->num_file_names; ++j)
4772 {
4773 const char *filename = file_data->file_names[j];
4774 dwarf2_per_objfile->filenames_cache->seen (filename);
4775 }
4776 }
4777 }
4778
4779 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4780 {
4781 gdb::unique_xmalloc_ptr<char> this_real_name;
4782
4783 if (need_fullname)
4784 this_real_name = gdb_realpath (filename);
4785 (*fun) (filename, this_real_name.get (), data);
4786 });
4787 }
4788
4789 static int
4790 dw2_has_symbols (struct objfile *objfile)
4791 {
4792 return 1;
4793 }
4794
4795 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4796 {
4797 dw2_has_symbols,
4798 dw2_find_last_source_symtab,
4799 dw2_forget_cached_source_info,
4800 dw2_map_symtabs_matching_filename,
4801 dw2_lookup_symbol,
4802 dw2_print_stats,
4803 dw2_dump,
4804 dw2_expand_symtabs_for_function,
4805 dw2_expand_all_symtabs,
4806 dw2_expand_symtabs_with_fullname,
4807 dw2_map_matching_symbols,
4808 dw2_expand_symtabs_matching,
4809 dw2_find_pc_sect_compunit_symtab,
4810 NULL,
4811 dw2_map_symbol_filenames
4812 };
4813
4814 /* DWARF-5 debug_names reader. */
4815
4816 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4817 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4818
4819 /* A helper function that reads the .debug_names section in SECTION
4820 and fills in MAP. FILENAME is the name of the file containing the
4821 section; it is used for error reporting.
4822
4823 Returns true if all went well, false otherwise. */
4824
4825 static bool
4826 read_debug_names_from_section (struct objfile *objfile,
4827 const char *filename,
4828 struct dwarf2_section_info *section,
4829 mapped_debug_names &map)
4830 {
4831 if (section->empty ())
4832 return false;
4833
4834 /* Older elfutils strip versions could keep the section in the main
4835 executable while splitting it for the separate debug info file. */
4836 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4837 return false;
4838
4839 section->read (objfile);
4840
4841 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4842
4843 const gdb_byte *addr = section->buffer;
4844
4845 bfd *const abfd = section->get_bfd_owner ();
4846
4847 unsigned int bytes_read;
4848 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4849 addr += bytes_read;
4850
4851 map.dwarf5_is_dwarf64 = bytes_read != 4;
4852 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4853 if (bytes_read + length != section->size)
4854 {
4855 /* There may be multiple per-CU indices. */
4856 warning (_("Section .debug_names in %s length %s does not match "
4857 "section length %s, ignoring .debug_names."),
4858 filename, plongest (bytes_read + length),
4859 pulongest (section->size));
4860 return false;
4861 }
4862
4863 /* The version number. */
4864 uint16_t version = read_2_bytes (abfd, addr);
4865 addr += 2;
4866 if (version != 5)
4867 {
4868 warning (_("Section .debug_names in %s has unsupported version %d, "
4869 "ignoring .debug_names."),
4870 filename, version);
4871 return false;
4872 }
4873
4874 /* Padding. */
4875 uint16_t padding = read_2_bytes (abfd, addr);
4876 addr += 2;
4877 if (padding != 0)
4878 {
4879 warning (_("Section .debug_names in %s has unsupported padding %d, "
4880 "ignoring .debug_names."),
4881 filename, padding);
4882 return false;
4883 }
4884
4885 /* comp_unit_count - The number of CUs in the CU list. */
4886 map.cu_count = read_4_bytes (abfd, addr);
4887 addr += 4;
4888
4889 /* local_type_unit_count - The number of TUs in the local TU
4890 list. */
4891 map.tu_count = read_4_bytes (abfd, addr);
4892 addr += 4;
4893
4894 /* foreign_type_unit_count - The number of TUs in the foreign TU
4895 list. */
4896 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4897 addr += 4;
4898 if (foreign_tu_count != 0)
4899 {
4900 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4901 "ignoring .debug_names."),
4902 filename, static_cast<unsigned long> (foreign_tu_count));
4903 return false;
4904 }
4905
4906 /* bucket_count - The number of hash buckets in the hash lookup
4907 table. */
4908 map.bucket_count = read_4_bytes (abfd, addr);
4909 addr += 4;
4910
4911 /* name_count - The number of unique names in the index. */
4912 map.name_count = read_4_bytes (abfd, addr);
4913 addr += 4;
4914
4915 /* abbrev_table_size - The size in bytes of the abbreviations
4916 table. */
4917 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4918 addr += 4;
4919
4920 /* augmentation_string_size - The size in bytes of the augmentation
4921 string. This value is rounded up to a multiple of 4. */
4922 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4923 addr += 4;
4924 map.augmentation_is_gdb = ((augmentation_string_size
4925 == sizeof (dwarf5_augmentation))
4926 && memcmp (addr, dwarf5_augmentation,
4927 sizeof (dwarf5_augmentation)) == 0);
4928 augmentation_string_size += (-augmentation_string_size) & 3;
4929 addr += augmentation_string_size;
4930
4931 /* List of CUs */
4932 map.cu_table_reordered = addr;
4933 addr += map.cu_count * map.offset_size;
4934
4935 /* List of Local TUs */
4936 map.tu_table_reordered = addr;
4937 addr += map.tu_count * map.offset_size;
4938
4939 /* Hash Lookup Table */
4940 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4941 addr += map.bucket_count * 4;
4942 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4943 addr += map.name_count * 4;
4944
4945 /* Name Table */
4946 map.name_table_string_offs_reordered = addr;
4947 addr += map.name_count * map.offset_size;
4948 map.name_table_entry_offs_reordered = addr;
4949 addr += map.name_count * map.offset_size;
4950
4951 const gdb_byte *abbrev_table_start = addr;
4952 for (;;)
4953 {
4954 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4955 addr += bytes_read;
4956 if (index_num == 0)
4957 break;
4958
4959 const auto insertpair
4960 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4961 if (!insertpair.second)
4962 {
4963 warning (_("Section .debug_names in %s has duplicate index %s, "
4964 "ignoring .debug_names."),
4965 filename, pulongest (index_num));
4966 return false;
4967 }
4968 mapped_debug_names::index_val &indexval = insertpair.first->second;
4969 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4970 addr += bytes_read;
4971
4972 for (;;)
4973 {
4974 mapped_debug_names::index_val::attr attr;
4975 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4976 addr += bytes_read;
4977 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4978 addr += bytes_read;
4979 if (attr.form == DW_FORM_implicit_const)
4980 {
4981 attr.implicit_const = read_signed_leb128 (abfd, addr,
4982 &bytes_read);
4983 addr += bytes_read;
4984 }
4985 if (attr.dw_idx == 0 && attr.form == 0)
4986 break;
4987 indexval.attr_vec.push_back (std::move (attr));
4988 }
4989 }
4990 if (addr != abbrev_table_start + abbrev_table_size)
4991 {
4992 warning (_("Section .debug_names in %s has abbreviation_table "
4993 "of size %s vs. written as %u, ignoring .debug_names."),
4994 filename, plongest (addr - abbrev_table_start),
4995 abbrev_table_size);
4996 return false;
4997 }
4998 map.entry_pool = addr;
4999
5000 return true;
5001 }
5002
5003 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5004 list. */
5005
5006 static void
5007 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5008 const mapped_debug_names &map,
5009 dwarf2_section_info &section,
5010 bool is_dwz)
5011 {
5012 sect_offset sect_off_prev;
5013 for (uint32_t i = 0; i <= map.cu_count; ++i)
5014 {
5015 sect_offset sect_off_next;
5016 if (i < map.cu_count)
5017 {
5018 sect_off_next
5019 = (sect_offset) (extract_unsigned_integer
5020 (map.cu_table_reordered + i * map.offset_size,
5021 map.offset_size,
5022 map.dwarf5_byte_order));
5023 }
5024 else
5025 sect_off_next = (sect_offset) section.size;
5026 if (i >= 1)
5027 {
5028 const ULONGEST length = sect_off_next - sect_off_prev;
5029 dwarf2_per_cu_data *per_cu
5030 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5031 sect_off_prev, length);
5032 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5033 }
5034 sect_off_prev = sect_off_next;
5035 }
5036 }
5037
5038 /* Read the CU list from the mapped index, and use it to create all
5039 the CU objects for this dwarf2_per_objfile. */
5040
5041 static void
5042 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5043 const mapped_debug_names &map,
5044 const mapped_debug_names &dwz_map)
5045 {
5046 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5047 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5048
5049 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5050 dwarf2_per_objfile->info,
5051 false /* is_dwz */);
5052
5053 if (dwz_map.cu_count == 0)
5054 return;
5055
5056 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5057 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5058 true /* is_dwz */);
5059 }
5060
5061 /* Read .debug_names. If everything went ok, initialize the "quick"
5062 elements of all the CUs and return true. Otherwise, return false. */
5063
5064 static bool
5065 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5066 {
5067 std::unique_ptr<mapped_debug_names> map
5068 (new mapped_debug_names (dwarf2_per_objfile));
5069 mapped_debug_names dwz_map (dwarf2_per_objfile);
5070 struct objfile *objfile = dwarf2_per_objfile->objfile;
5071
5072 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5073 &dwarf2_per_objfile->debug_names,
5074 *map))
5075 return false;
5076
5077 /* Don't use the index if it's empty. */
5078 if (map->name_count == 0)
5079 return false;
5080
5081 /* If there is a .dwz file, read it so we can get its CU list as
5082 well. */
5083 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5084 if (dwz != NULL)
5085 {
5086 if (!read_debug_names_from_section (objfile,
5087 bfd_get_filename (dwz->dwz_bfd.get ()),
5088 &dwz->debug_names, dwz_map))
5089 {
5090 warning (_("could not read '.debug_names' section from %s; skipping"),
5091 bfd_get_filename (dwz->dwz_bfd.get ()));
5092 return false;
5093 }
5094 }
5095
5096 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5097
5098 if (map->tu_count != 0)
5099 {
5100 /* We can only handle a single .debug_types when we have an
5101 index. */
5102 if (dwarf2_per_objfile->types.size () != 1)
5103 return false;
5104
5105 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5106
5107 create_signatured_type_table_from_debug_names
5108 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5109 }
5110
5111 create_addrmap_from_aranges (dwarf2_per_objfile,
5112 &dwarf2_per_objfile->debug_aranges);
5113
5114 dwarf2_per_objfile->debug_names_table = std::move (map);
5115 dwarf2_per_objfile->using_index = 1;
5116 dwarf2_per_objfile->quick_file_names_table =
5117 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5118
5119 return true;
5120 }
5121
5122 /* Type used to manage iterating over all CUs looking for a symbol for
5123 .debug_names. */
5124
5125 class dw2_debug_names_iterator
5126 {
5127 public:
5128 dw2_debug_names_iterator (const mapped_debug_names &map,
5129 gdb::optional<block_enum> block_index,
5130 domain_enum domain,
5131 const char *name)
5132 : m_map (map), m_block_index (block_index), m_domain (domain),
5133 m_addr (find_vec_in_debug_names (map, name))
5134 {}
5135
5136 dw2_debug_names_iterator (const mapped_debug_names &map,
5137 search_domain search, uint32_t namei)
5138 : m_map (map),
5139 m_search (search),
5140 m_addr (find_vec_in_debug_names (map, namei))
5141 {}
5142
5143 dw2_debug_names_iterator (const mapped_debug_names &map,
5144 block_enum block_index, domain_enum domain,
5145 uint32_t namei)
5146 : m_map (map), m_block_index (block_index), m_domain (domain),
5147 m_addr (find_vec_in_debug_names (map, namei))
5148 {}
5149
5150 /* Return the next matching CU or NULL if there are no more. */
5151 dwarf2_per_cu_data *next ();
5152
5153 private:
5154 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5155 const char *name);
5156 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5157 uint32_t namei);
5158
5159 /* The internalized form of .debug_names. */
5160 const mapped_debug_names &m_map;
5161
5162 /* If set, only look for symbols that match that block. Valid values are
5163 GLOBAL_BLOCK and STATIC_BLOCK. */
5164 const gdb::optional<block_enum> m_block_index;
5165
5166 /* The kind of symbol we're looking for. */
5167 const domain_enum m_domain = UNDEF_DOMAIN;
5168 const search_domain m_search = ALL_DOMAIN;
5169
5170 /* The list of CUs from the index entry of the symbol, or NULL if
5171 not found. */
5172 const gdb_byte *m_addr;
5173 };
5174
5175 const char *
5176 mapped_debug_names::namei_to_name (uint32_t namei) const
5177 {
5178 const ULONGEST namei_string_offs
5179 = extract_unsigned_integer ((name_table_string_offs_reordered
5180 + namei * offset_size),
5181 offset_size,
5182 dwarf5_byte_order);
5183 return read_indirect_string_at_offset
5184 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5185 }
5186
5187 /* Find a slot in .debug_names for the object named NAME. If NAME is
5188 found, return pointer to its pool data. If NAME cannot be found,
5189 return NULL. */
5190
5191 const gdb_byte *
5192 dw2_debug_names_iterator::find_vec_in_debug_names
5193 (const mapped_debug_names &map, const char *name)
5194 {
5195 int (*cmp) (const char *, const char *);
5196
5197 gdb::unique_xmalloc_ptr<char> without_params;
5198 if (current_language->la_language == language_cplus
5199 || current_language->la_language == language_fortran
5200 || current_language->la_language == language_d)
5201 {
5202 /* NAME is already canonical. Drop any qualifiers as
5203 .debug_names does not contain any. */
5204
5205 if (strchr (name, '(') != NULL)
5206 {
5207 without_params = cp_remove_params (name);
5208 if (without_params != NULL)
5209 name = without_params.get ();
5210 }
5211 }
5212
5213 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5214
5215 const uint32_t full_hash = dwarf5_djb_hash (name);
5216 uint32_t namei
5217 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5218 (map.bucket_table_reordered
5219 + (full_hash % map.bucket_count)), 4,
5220 map.dwarf5_byte_order);
5221 if (namei == 0)
5222 return NULL;
5223 --namei;
5224 if (namei >= map.name_count)
5225 {
5226 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5227 "[in module %s]"),
5228 namei, map.name_count,
5229 objfile_name (map.dwarf2_per_objfile->objfile));
5230 return NULL;
5231 }
5232
5233 for (;;)
5234 {
5235 const uint32_t namei_full_hash
5236 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5237 (map.hash_table_reordered + namei), 4,
5238 map.dwarf5_byte_order);
5239 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5240 return NULL;
5241
5242 if (full_hash == namei_full_hash)
5243 {
5244 const char *const namei_string = map.namei_to_name (namei);
5245
5246 #if 0 /* An expensive sanity check. */
5247 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5248 {
5249 complaint (_("Wrong .debug_names hash for string at index %u "
5250 "[in module %s]"),
5251 namei, objfile_name (dwarf2_per_objfile->objfile));
5252 return NULL;
5253 }
5254 #endif
5255
5256 if (cmp (namei_string, name) == 0)
5257 {
5258 const ULONGEST namei_entry_offs
5259 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5260 + namei * map.offset_size),
5261 map.offset_size, map.dwarf5_byte_order);
5262 return map.entry_pool + namei_entry_offs;
5263 }
5264 }
5265
5266 ++namei;
5267 if (namei >= map.name_count)
5268 return NULL;
5269 }
5270 }
5271
5272 const gdb_byte *
5273 dw2_debug_names_iterator::find_vec_in_debug_names
5274 (const mapped_debug_names &map, uint32_t namei)
5275 {
5276 if (namei >= map.name_count)
5277 {
5278 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5279 "[in module %s]"),
5280 namei, map.name_count,
5281 objfile_name (map.dwarf2_per_objfile->objfile));
5282 return NULL;
5283 }
5284
5285 const ULONGEST namei_entry_offs
5286 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5287 + namei * map.offset_size),
5288 map.offset_size, map.dwarf5_byte_order);
5289 return map.entry_pool + namei_entry_offs;
5290 }
5291
5292 /* See dw2_debug_names_iterator. */
5293
5294 dwarf2_per_cu_data *
5295 dw2_debug_names_iterator::next ()
5296 {
5297 if (m_addr == NULL)
5298 return NULL;
5299
5300 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5301 struct objfile *objfile = dwarf2_per_objfile->objfile;
5302 bfd *const abfd = objfile->obfd;
5303
5304 again:
5305
5306 unsigned int bytes_read;
5307 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5308 m_addr += bytes_read;
5309 if (abbrev == 0)
5310 return NULL;
5311
5312 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5313 if (indexval_it == m_map.abbrev_map.cend ())
5314 {
5315 complaint (_("Wrong .debug_names undefined abbrev code %s "
5316 "[in module %s]"),
5317 pulongest (abbrev), objfile_name (objfile));
5318 return NULL;
5319 }
5320 const mapped_debug_names::index_val &indexval = indexval_it->second;
5321 enum class symbol_linkage {
5322 unknown,
5323 static_,
5324 extern_,
5325 } symbol_linkage_ = symbol_linkage::unknown;
5326 dwarf2_per_cu_data *per_cu = NULL;
5327 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5328 {
5329 ULONGEST ull;
5330 switch (attr.form)
5331 {
5332 case DW_FORM_implicit_const:
5333 ull = attr.implicit_const;
5334 break;
5335 case DW_FORM_flag_present:
5336 ull = 1;
5337 break;
5338 case DW_FORM_udata:
5339 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5340 m_addr += bytes_read;
5341 break;
5342 default:
5343 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5344 dwarf_form_name (attr.form),
5345 objfile_name (objfile));
5346 return NULL;
5347 }
5348 switch (attr.dw_idx)
5349 {
5350 case DW_IDX_compile_unit:
5351 /* Don't crash on bad data. */
5352 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5353 {
5354 complaint (_(".debug_names entry has bad CU index %s"
5355 " [in module %s]"),
5356 pulongest (ull),
5357 objfile_name (dwarf2_per_objfile->objfile));
5358 continue;
5359 }
5360 per_cu = dwarf2_per_objfile->get_cutu (ull);
5361 break;
5362 case DW_IDX_type_unit:
5363 /* Don't crash on bad data. */
5364 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5365 {
5366 complaint (_(".debug_names entry has bad TU index %s"
5367 " [in module %s]"),
5368 pulongest (ull),
5369 objfile_name (dwarf2_per_objfile->objfile));
5370 continue;
5371 }
5372 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5373 break;
5374 case DW_IDX_GNU_internal:
5375 if (!m_map.augmentation_is_gdb)
5376 break;
5377 symbol_linkage_ = symbol_linkage::static_;
5378 break;
5379 case DW_IDX_GNU_external:
5380 if (!m_map.augmentation_is_gdb)
5381 break;
5382 symbol_linkage_ = symbol_linkage::extern_;
5383 break;
5384 }
5385 }
5386
5387 /* Skip if already read in. */
5388 if (per_cu->v.quick->compunit_symtab)
5389 goto again;
5390
5391 /* Check static vs global. */
5392 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5393 {
5394 const bool want_static = *m_block_index == STATIC_BLOCK;
5395 const bool symbol_is_static =
5396 symbol_linkage_ == symbol_linkage::static_;
5397 if (want_static != symbol_is_static)
5398 goto again;
5399 }
5400
5401 /* Match dw2_symtab_iter_next, symbol_kind
5402 and debug_names::psymbol_tag. */
5403 switch (m_domain)
5404 {
5405 case VAR_DOMAIN:
5406 switch (indexval.dwarf_tag)
5407 {
5408 case DW_TAG_variable:
5409 case DW_TAG_subprogram:
5410 /* Some types are also in VAR_DOMAIN. */
5411 case DW_TAG_typedef:
5412 case DW_TAG_structure_type:
5413 break;
5414 default:
5415 goto again;
5416 }
5417 break;
5418 case STRUCT_DOMAIN:
5419 switch (indexval.dwarf_tag)
5420 {
5421 case DW_TAG_typedef:
5422 case DW_TAG_structure_type:
5423 break;
5424 default:
5425 goto again;
5426 }
5427 break;
5428 case LABEL_DOMAIN:
5429 switch (indexval.dwarf_tag)
5430 {
5431 case 0:
5432 case DW_TAG_variable:
5433 break;
5434 default:
5435 goto again;
5436 }
5437 break;
5438 case MODULE_DOMAIN:
5439 switch (indexval.dwarf_tag)
5440 {
5441 case DW_TAG_module:
5442 break;
5443 default:
5444 goto again;
5445 }
5446 break;
5447 default:
5448 break;
5449 }
5450
5451 /* Match dw2_expand_symtabs_matching, symbol_kind and
5452 debug_names::psymbol_tag. */
5453 switch (m_search)
5454 {
5455 case VARIABLES_DOMAIN:
5456 switch (indexval.dwarf_tag)
5457 {
5458 case DW_TAG_variable:
5459 break;
5460 default:
5461 goto again;
5462 }
5463 break;
5464 case FUNCTIONS_DOMAIN:
5465 switch (indexval.dwarf_tag)
5466 {
5467 case DW_TAG_subprogram:
5468 break;
5469 default:
5470 goto again;
5471 }
5472 break;
5473 case TYPES_DOMAIN:
5474 switch (indexval.dwarf_tag)
5475 {
5476 case DW_TAG_typedef:
5477 case DW_TAG_structure_type:
5478 break;
5479 default:
5480 goto again;
5481 }
5482 break;
5483 case MODULES_DOMAIN:
5484 switch (indexval.dwarf_tag)
5485 {
5486 case DW_TAG_module:
5487 break;
5488 default:
5489 goto again;
5490 }
5491 default:
5492 break;
5493 }
5494
5495 return per_cu;
5496 }
5497
5498 static struct compunit_symtab *
5499 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5500 const char *name, domain_enum domain)
5501 {
5502 struct dwarf2_per_objfile *dwarf2_per_objfile
5503 = get_dwarf2_per_objfile (objfile);
5504
5505 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5506 if (!mapp)
5507 {
5508 /* index is NULL if OBJF_READNOW. */
5509 return NULL;
5510 }
5511 const auto &map = *mapp;
5512
5513 dw2_debug_names_iterator iter (map, block_index, domain, name);
5514
5515 struct compunit_symtab *stab_best = NULL;
5516 struct dwarf2_per_cu_data *per_cu;
5517 while ((per_cu = iter.next ()) != NULL)
5518 {
5519 struct symbol *sym, *with_opaque = NULL;
5520 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5521 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5522 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5523
5524 sym = block_find_symbol (block, name, domain,
5525 block_find_non_opaque_type_preferred,
5526 &with_opaque);
5527
5528 /* Some caution must be observed with overloaded functions and
5529 methods, since the index will not contain any overload
5530 information (but NAME might contain it). */
5531
5532 if (sym != NULL
5533 && strcmp_iw (sym->search_name (), name) == 0)
5534 return stab;
5535 if (with_opaque != NULL
5536 && strcmp_iw (with_opaque->search_name (), name) == 0)
5537 stab_best = stab;
5538
5539 /* Keep looking through other CUs. */
5540 }
5541
5542 return stab_best;
5543 }
5544
5545 /* This dumps minimal information about .debug_names. It is called
5546 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5547 uses this to verify that .debug_names has been loaded. */
5548
5549 static void
5550 dw2_debug_names_dump (struct objfile *objfile)
5551 {
5552 struct dwarf2_per_objfile *dwarf2_per_objfile
5553 = get_dwarf2_per_objfile (objfile);
5554
5555 gdb_assert (dwarf2_per_objfile->using_index);
5556 printf_filtered (".debug_names:");
5557 if (dwarf2_per_objfile->debug_names_table)
5558 printf_filtered (" exists\n");
5559 else
5560 printf_filtered (" faked for \"readnow\"\n");
5561 printf_filtered ("\n");
5562 }
5563
5564 static void
5565 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5566 const char *func_name)
5567 {
5568 struct dwarf2_per_objfile *dwarf2_per_objfile
5569 = get_dwarf2_per_objfile (objfile);
5570
5571 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5572 if (dwarf2_per_objfile->debug_names_table)
5573 {
5574 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5575
5576 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5577
5578 struct dwarf2_per_cu_data *per_cu;
5579 while ((per_cu = iter.next ()) != NULL)
5580 dw2_instantiate_symtab (per_cu, false);
5581 }
5582 }
5583
5584 static void
5585 dw2_debug_names_map_matching_symbols
5586 (struct objfile *objfile,
5587 const lookup_name_info &name, domain_enum domain,
5588 int global,
5589 gdb::function_view<symbol_found_callback_ftype> callback,
5590 symbol_compare_ftype *ordered_compare)
5591 {
5592 struct dwarf2_per_objfile *dwarf2_per_objfile
5593 = get_dwarf2_per_objfile (objfile);
5594
5595 /* debug_names_table is NULL if OBJF_READNOW. */
5596 if (!dwarf2_per_objfile->debug_names_table)
5597 return;
5598
5599 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5600 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5601
5602 const char *match_name = name.ada ().lookup_name ().c_str ();
5603 auto matcher = [&] (const char *symname)
5604 {
5605 if (ordered_compare == nullptr)
5606 return true;
5607 return ordered_compare (symname, match_name) == 0;
5608 };
5609
5610 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5611 [&] (offset_type namei)
5612 {
5613 /* The name was matched, now expand corresponding CUs that were
5614 marked. */
5615 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5616
5617 struct dwarf2_per_cu_data *per_cu;
5618 while ((per_cu = iter.next ()) != NULL)
5619 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5620 return true;
5621 });
5622
5623 /* It's a shame we couldn't do this inside the
5624 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5625 that have already been expanded. Instead, this loop matches what
5626 the psymtab code does. */
5627 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5628 {
5629 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5630 if (cust != nullptr)
5631 {
5632 const struct block *block
5633 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5634 if (!iterate_over_symbols_terminated (block, name,
5635 domain, callback))
5636 break;
5637 }
5638 }
5639 }
5640
5641 static void
5642 dw2_debug_names_expand_symtabs_matching
5643 (struct objfile *objfile,
5644 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5645 const lookup_name_info &lookup_name,
5646 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5647 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5648 enum search_domain kind)
5649 {
5650 struct dwarf2_per_objfile *dwarf2_per_objfile
5651 = get_dwarf2_per_objfile (objfile);
5652
5653 /* debug_names_table is NULL if OBJF_READNOW. */
5654 if (!dwarf2_per_objfile->debug_names_table)
5655 return;
5656
5657 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5658
5659 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5660
5661 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5662 symbol_matcher,
5663 kind, [&] (offset_type namei)
5664 {
5665 /* The name was matched, now expand corresponding CUs that were
5666 marked. */
5667 dw2_debug_names_iterator iter (map, kind, namei);
5668
5669 struct dwarf2_per_cu_data *per_cu;
5670 while ((per_cu = iter.next ()) != NULL)
5671 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5672 expansion_notify);
5673 return true;
5674 });
5675 }
5676
5677 const struct quick_symbol_functions dwarf2_debug_names_functions =
5678 {
5679 dw2_has_symbols,
5680 dw2_find_last_source_symtab,
5681 dw2_forget_cached_source_info,
5682 dw2_map_symtabs_matching_filename,
5683 dw2_debug_names_lookup_symbol,
5684 dw2_print_stats,
5685 dw2_debug_names_dump,
5686 dw2_debug_names_expand_symtabs_for_function,
5687 dw2_expand_all_symtabs,
5688 dw2_expand_symtabs_with_fullname,
5689 dw2_debug_names_map_matching_symbols,
5690 dw2_debug_names_expand_symtabs_matching,
5691 dw2_find_pc_sect_compunit_symtab,
5692 NULL,
5693 dw2_map_symbol_filenames
5694 };
5695
5696 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5697 to either a dwarf2_per_objfile or dwz_file object. */
5698
5699 template <typename T>
5700 static gdb::array_view<const gdb_byte>
5701 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5702 {
5703 dwarf2_section_info *section = &section_owner->gdb_index;
5704
5705 if (section->empty ())
5706 return {};
5707
5708 /* Older elfutils strip versions could keep the section in the main
5709 executable while splitting it for the separate debug info file. */
5710 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5711 return {};
5712
5713 section->read (obj);
5714
5715 /* dwarf2_section_info::size is a bfd_size_type, while
5716 gdb::array_view works with size_t. On 32-bit hosts, with
5717 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5718 is 32-bit. So we need an explicit narrowing conversion here.
5719 This is fine, because it's impossible to allocate or mmap an
5720 array/buffer larger than what size_t can represent. */
5721 return gdb::make_array_view (section->buffer, section->size);
5722 }
5723
5724 /* Lookup the index cache for the contents of the index associated to
5725 DWARF2_OBJ. */
5726
5727 static gdb::array_view<const gdb_byte>
5728 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5729 {
5730 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5731 if (build_id == nullptr)
5732 return {};
5733
5734 return global_index_cache.lookup_gdb_index (build_id,
5735 &dwarf2_obj->index_cache_res);
5736 }
5737
5738 /* Same as the above, but for DWZ. */
5739
5740 static gdb::array_view<const gdb_byte>
5741 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5742 {
5743 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5744 if (build_id == nullptr)
5745 return {};
5746
5747 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5748 }
5749
5750 /* See symfile.h. */
5751
5752 bool
5753 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5754 {
5755 struct dwarf2_per_objfile *dwarf2_per_objfile
5756 = get_dwarf2_per_objfile (objfile);
5757
5758 /* If we're about to read full symbols, don't bother with the
5759 indices. In this case we also don't care if some other debug
5760 format is making psymtabs, because they are all about to be
5761 expanded anyway. */
5762 if ((objfile->flags & OBJF_READNOW))
5763 {
5764 dwarf2_per_objfile->using_index = 1;
5765 create_all_comp_units (dwarf2_per_objfile);
5766 create_all_type_units (dwarf2_per_objfile);
5767 dwarf2_per_objfile->quick_file_names_table
5768 = create_quick_file_names_table
5769 (dwarf2_per_objfile->all_comp_units.size ());
5770
5771 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5772 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5773 {
5774 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5775
5776 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5777 struct dwarf2_per_cu_quick_data);
5778 }
5779
5780 /* Return 1 so that gdb sees the "quick" functions. However,
5781 these functions will be no-ops because we will have expanded
5782 all symtabs. */
5783 *index_kind = dw_index_kind::GDB_INDEX;
5784 return true;
5785 }
5786
5787 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5788 {
5789 *index_kind = dw_index_kind::DEBUG_NAMES;
5790 return true;
5791 }
5792
5793 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5794 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5795 get_gdb_index_contents_from_section<dwz_file>))
5796 {
5797 *index_kind = dw_index_kind::GDB_INDEX;
5798 return true;
5799 }
5800
5801 /* ... otherwise, try to find the index in the index cache. */
5802 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5803 get_gdb_index_contents_from_cache,
5804 get_gdb_index_contents_from_cache_dwz))
5805 {
5806 global_index_cache.hit ();
5807 *index_kind = dw_index_kind::GDB_INDEX;
5808 return true;
5809 }
5810
5811 global_index_cache.miss ();
5812 return false;
5813 }
5814
5815 \f
5816
5817 /* Build a partial symbol table. */
5818
5819 void
5820 dwarf2_build_psymtabs (struct objfile *objfile)
5821 {
5822 struct dwarf2_per_objfile *dwarf2_per_objfile
5823 = get_dwarf2_per_objfile (objfile);
5824
5825 init_psymbol_list (objfile, 1024);
5826
5827 try
5828 {
5829 /* This isn't really ideal: all the data we allocate on the
5830 objfile's obstack is still uselessly kept around. However,
5831 freeing it seems unsafe. */
5832 psymtab_discarder psymtabs (objfile);
5833 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5834 psymtabs.keep ();
5835
5836 /* (maybe) store an index in the cache. */
5837 global_index_cache.store (dwarf2_per_objfile);
5838 }
5839 catch (const gdb_exception_error &except)
5840 {
5841 exception_print (gdb_stderr, except);
5842 }
5843 }
5844
5845 /* Find the base address of the compilation unit for range lists and
5846 location lists. It will normally be specified by DW_AT_low_pc.
5847 In DWARF-3 draft 4, the base address could be overridden by
5848 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5849 compilation units with discontinuous ranges. */
5850
5851 static void
5852 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5853 {
5854 struct attribute *attr;
5855
5856 cu->base_known = 0;
5857 cu->base_address = 0;
5858
5859 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5860 if (attr != nullptr)
5861 {
5862 cu->base_address = attr->value_as_address ();
5863 cu->base_known = 1;
5864 }
5865 else
5866 {
5867 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5868 if (attr != nullptr)
5869 {
5870 cu->base_address = attr->value_as_address ();
5871 cu->base_known = 1;
5872 }
5873 }
5874 }
5875
5876 /* Helper function that returns the proper abbrev section for
5877 THIS_CU. */
5878
5879 static struct dwarf2_section_info *
5880 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5881 {
5882 struct dwarf2_section_info *abbrev;
5883 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5884
5885 if (this_cu->is_dwz)
5886 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5887 else
5888 abbrev = &dwarf2_per_objfile->abbrev;
5889
5890 return abbrev;
5891 }
5892
5893 /* Fetch the abbreviation table offset from a comp or type unit header. */
5894
5895 static sect_offset
5896 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5897 struct dwarf2_section_info *section,
5898 sect_offset sect_off)
5899 {
5900 bfd *abfd = section->get_bfd_owner ();
5901 const gdb_byte *info_ptr;
5902 unsigned int initial_length_size, offset_size;
5903 uint16_t version;
5904
5905 section->read (dwarf2_per_objfile->objfile);
5906 info_ptr = section->buffer + to_underlying (sect_off);
5907 read_initial_length (abfd, info_ptr, &initial_length_size);
5908 offset_size = initial_length_size == 4 ? 4 : 8;
5909 info_ptr += initial_length_size;
5910
5911 version = read_2_bytes (abfd, info_ptr);
5912 info_ptr += 2;
5913 if (version >= 5)
5914 {
5915 /* Skip unit type and address size. */
5916 info_ptr += 2;
5917 }
5918
5919 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5920 }
5921
5922 /* A partial symtab that is used only for include files. */
5923 struct dwarf2_include_psymtab : public partial_symtab
5924 {
5925 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5926 : partial_symtab (filename, objfile)
5927 {
5928 }
5929
5930 void read_symtab (struct objfile *objfile) override
5931 {
5932 expand_psymtab (objfile);
5933 }
5934
5935 void expand_psymtab (struct objfile *objfile) override
5936 {
5937 if (m_readin)
5938 return;
5939 /* It's an include file, no symbols to read for it.
5940 Everything is in the parent symtab. */
5941 read_dependencies (objfile);
5942 m_readin = true;
5943 }
5944
5945 bool readin_p () const override
5946 {
5947 return m_readin;
5948 }
5949
5950 struct compunit_symtab *get_compunit_symtab () const override
5951 {
5952 return nullptr;
5953 }
5954
5955 private:
5956
5957 bool m_readin = false;
5958 };
5959
5960 /* Allocate a new partial symtab for file named NAME and mark this new
5961 partial symtab as being an include of PST. */
5962
5963 static void
5964 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5965 struct objfile *objfile)
5966 {
5967 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5968
5969 if (!IS_ABSOLUTE_PATH (subpst->filename))
5970 {
5971 /* It shares objfile->objfile_obstack. */
5972 subpst->dirname = pst->dirname;
5973 }
5974
5975 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5976 subpst->dependencies[0] = pst;
5977 subpst->number_of_dependencies = 1;
5978 }
5979
5980 /* Read the Line Number Program data and extract the list of files
5981 included by the source file represented by PST. Build an include
5982 partial symtab for each of these included files. */
5983
5984 static void
5985 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5986 struct die_info *die,
5987 dwarf2_psymtab *pst)
5988 {
5989 line_header_up lh;
5990 struct attribute *attr;
5991
5992 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5993 if (attr != nullptr)
5994 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5995 if (lh == NULL)
5996 return; /* No linetable, so no includes. */
5997
5998 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5999 that we pass in the raw text_low here; that is ok because we're
6000 only decoding the line table to make include partial symtabs, and
6001 so the addresses aren't really used. */
6002 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6003 pst->raw_text_low (), 1);
6004 }
6005
6006 static hashval_t
6007 hash_signatured_type (const void *item)
6008 {
6009 const struct signatured_type *sig_type
6010 = (const struct signatured_type *) item;
6011
6012 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6013 return sig_type->signature;
6014 }
6015
6016 static int
6017 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6018 {
6019 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6020 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6021
6022 return lhs->signature == rhs->signature;
6023 }
6024
6025 /* Allocate a hash table for signatured types. */
6026
6027 static htab_up
6028 allocate_signatured_type_table ()
6029 {
6030 return htab_up (htab_create_alloc (41,
6031 hash_signatured_type,
6032 eq_signatured_type,
6033 NULL, xcalloc, xfree));
6034 }
6035
6036 /* A helper function to add a signatured type CU to a table. */
6037
6038 static int
6039 add_signatured_type_cu_to_table (void **slot, void *datum)
6040 {
6041 struct signatured_type *sigt = (struct signatured_type *) *slot;
6042 std::vector<signatured_type *> *all_type_units
6043 = (std::vector<signatured_type *> *) datum;
6044
6045 all_type_units->push_back (sigt);
6046
6047 return 1;
6048 }
6049
6050 /* A helper for create_debug_types_hash_table. Read types from SECTION
6051 and fill them into TYPES_HTAB. It will process only type units,
6052 therefore DW_UT_type. */
6053
6054 static void
6055 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6056 struct dwo_file *dwo_file,
6057 dwarf2_section_info *section, htab_up &types_htab,
6058 rcuh_kind section_kind)
6059 {
6060 struct objfile *objfile = dwarf2_per_objfile->objfile;
6061 struct dwarf2_section_info *abbrev_section;
6062 bfd *abfd;
6063 const gdb_byte *info_ptr, *end_ptr;
6064
6065 abbrev_section = (dwo_file != NULL
6066 ? &dwo_file->sections.abbrev
6067 : &dwarf2_per_objfile->abbrev);
6068
6069 if (dwarf_read_debug)
6070 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6071 section->get_name (),
6072 abbrev_section->get_file_name ());
6073
6074 section->read (objfile);
6075 info_ptr = section->buffer;
6076
6077 if (info_ptr == NULL)
6078 return;
6079
6080 /* We can't set abfd until now because the section may be empty or
6081 not present, in which case the bfd is unknown. */
6082 abfd = section->get_bfd_owner ();
6083
6084 /* We don't use cutu_reader here because we don't need to read
6085 any dies: the signature is in the header. */
6086
6087 end_ptr = info_ptr + section->size;
6088 while (info_ptr < end_ptr)
6089 {
6090 struct signatured_type *sig_type;
6091 struct dwo_unit *dwo_tu;
6092 void **slot;
6093 const gdb_byte *ptr = info_ptr;
6094 struct comp_unit_head header;
6095 unsigned int length;
6096
6097 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6098
6099 /* Initialize it due to a false compiler warning. */
6100 header.signature = -1;
6101 header.type_cu_offset_in_tu = (cu_offset) -1;
6102
6103 /* We need to read the type's signature in order to build the hash
6104 table, but we don't need anything else just yet. */
6105
6106 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6107 abbrev_section, ptr, section_kind);
6108
6109 length = header.get_length ();
6110
6111 /* Skip dummy type units. */
6112 if (ptr >= info_ptr + length
6113 || peek_abbrev_code (abfd, ptr) == 0
6114 || header.unit_type != DW_UT_type)
6115 {
6116 info_ptr += length;
6117 continue;
6118 }
6119
6120 if (types_htab == NULL)
6121 {
6122 if (dwo_file)
6123 types_htab = allocate_dwo_unit_table ();
6124 else
6125 types_htab = allocate_signatured_type_table ();
6126 }
6127
6128 if (dwo_file)
6129 {
6130 sig_type = NULL;
6131 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6132 struct dwo_unit);
6133 dwo_tu->dwo_file = dwo_file;
6134 dwo_tu->signature = header.signature;
6135 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6136 dwo_tu->section = section;
6137 dwo_tu->sect_off = sect_off;
6138 dwo_tu->length = length;
6139 }
6140 else
6141 {
6142 /* N.B.: type_offset is not usable if this type uses a DWO file.
6143 The real type_offset is in the DWO file. */
6144 dwo_tu = NULL;
6145 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6146 struct signatured_type);
6147 sig_type->signature = header.signature;
6148 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6149 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6150 sig_type->per_cu.is_debug_types = 1;
6151 sig_type->per_cu.section = section;
6152 sig_type->per_cu.sect_off = sect_off;
6153 sig_type->per_cu.length = length;
6154 }
6155
6156 slot = htab_find_slot (types_htab.get (),
6157 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6158 INSERT);
6159 gdb_assert (slot != NULL);
6160 if (*slot != NULL)
6161 {
6162 sect_offset dup_sect_off;
6163
6164 if (dwo_file)
6165 {
6166 const struct dwo_unit *dup_tu
6167 = (const struct dwo_unit *) *slot;
6168
6169 dup_sect_off = dup_tu->sect_off;
6170 }
6171 else
6172 {
6173 const struct signatured_type *dup_tu
6174 = (const struct signatured_type *) *slot;
6175
6176 dup_sect_off = dup_tu->per_cu.sect_off;
6177 }
6178
6179 complaint (_("debug type entry at offset %s is duplicate to"
6180 " the entry at offset %s, signature %s"),
6181 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6182 hex_string (header.signature));
6183 }
6184 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6185
6186 if (dwarf_read_debug > 1)
6187 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6188 sect_offset_str (sect_off),
6189 hex_string (header.signature));
6190
6191 info_ptr += length;
6192 }
6193 }
6194
6195 /* Create the hash table of all entries in the .debug_types
6196 (or .debug_types.dwo) section(s).
6197 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6198 otherwise it is NULL.
6199
6200 The result is a pointer to the hash table or NULL if there are no types.
6201
6202 Note: This function processes DWO files only, not DWP files. */
6203
6204 static void
6205 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6206 struct dwo_file *dwo_file,
6207 gdb::array_view<dwarf2_section_info> type_sections,
6208 htab_up &types_htab)
6209 {
6210 for (dwarf2_section_info &section : type_sections)
6211 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6212 types_htab, rcuh_kind::TYPE);
6213 }
6214
6215 /* Create the hash table of all entries in the .debug_types section,
6216 and initialize all_type_units.
6217 The result is zero if there is an error (e.g. missing .debug_types section),
6218 otherwise non-zero. */
6219
6220 static int
6221 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6222 {
6223 htab_up types_htab;
6224
6225 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6226 &dwarf2_per_objfile->info, types_htab,
6227 rcuh_kind::COMPILE);
6228 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6229 dwarf2_per_objfile->types, types_htab);
6230 if (types_htab == NULL)
6231 {
6232 dwarf2_per_objfile->signatured_types = NULL;
6233 return 0;
6234 }
6235
6236 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6237
6238 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6239 dwarf2_per_objfile->all_type_units.reserve
6240 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6241
6242 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6243 add_signatured_type_cu_to_table,
6244 &dwarf2_per_objfile->all_type_units);
6245
6246 return 1;
6247 }
6248
6249 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6250 If SLOT is non-NULL, it is the entry to use in the hash table.
6251 Otherwise we find one. */
6252
6253 static struct signatured_type *
6254 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6255 void **slot)
6256 {
6257 struct objfile *objfile = dwarf2_per_objfile->objfile;
6258
6259 if (dwarf2_per_objfile->all_type_units.size ()
6260 == dwarf2_per_objfile->all_type_units.capacity ())
6261 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6262
6263 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6264 struct signatured_type);
6265
6266 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6267 sig_type->signature = sig;
6268 sig_type->per_cu.is_debug_types = 1;
6269 if (dwarf2_per_objfile->using_index)
6270 {
6271 sig_type->per_cu.v.quick =
6272 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6273 struct dwarf2_per_cu_quick_data);
6274 }
6275
6276 if (slot == NULL)
6277 {
6278 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6279 sig_type, INSERT);
6280 }
6281 gdb_assert (*slot == NULL);
6282 *slot = sig_type;
6283 /* The rest of sig_type must be filled in by the caller. */
6284 return sig_type;
6285 }
6286
6287 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6288 Fill in SIG_ENTRY with DWO_ENTRY. */
6289
6290 static void
6291 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6292 struct signatured_type *sig_entry,
6293 struct dwo_unit *dwo_entry)
6294 {
6295 /* Make sure we're not clobbering something we don't expect to. */
6296 gdb_assert (! sig_entry->per_cu.queued);
6297 gdb_assert (sig_entry->per_cu.cu == NULL);
6298 if (dwarf2_per_objfile->using_index)
6299 {
6300 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6301 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6302 }
6303 else
6304 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6305 gdb_assert (sig_entry->signature == dwo_entry->signature);
6306 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6307 gdb_assert (sig_entry->type_unit_group == NULL);
6308 gdb_assert (sig_entry->dwo_unit == NULL);
6309
6310 sig_entry->per_cu.section = dwo_entry->section;
6311 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6312 sig_entry->per_cu.length = dwo_entry->length;
6313 sig_entry->per_cu.reading_dwo_directly = 1;
6314 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6315 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6316 sig_entry->dwo_unit = dwo_entry;
6317 }
6318
6319 /* Subroutine of lookup_signatured_type.
6320 If we haven't read the TU yet, create the signatured_type data structure
6321 for a TU to be read in directly from a DWO file, bypassing the stub.
6322 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6323 using .gdb_index, then when reading a CU we want to stay in the DWO file
6324 containing that CU. Otherwise we could end up reading several other DWO
6325 files (due to comdat folding) to process the transitive closure of all the
6326 mentioned TUs, and that can be slow. The current DWO file will have every
6327 type signature that it needs.
6328 We only do this for .gdb_index because in the psymtab case we already have
6329 to read all the DWOs to build the type unit groups. */
6330
6331 static struct signatured_type *
6332 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6333 {
6334 struct dwarf2_per_objfile *dwarf2_per_objfile
6335 = cu->per_cu->dwarf2_per_objfile;
6336 struct dwo_file *dwo_file;
6337 struct dwo_unit find_dwo_entry, *dwo_entry;
6338 struct signatured_type find_sig_entry, *sig_entry;
6339 void **slot;
6340
6341 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6342
6343 /* If TU skeletons have been removed then we may not have read in any
6344 TUs yet. */
6345 if (dwarf2_per_objfile->signatured_types == NULL)
6346 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6347
6348 /* We only ever need to read in one copy of a signatured type.
6349 Use the global signatured_types array to do our own comdat-folding
6350 of types. If this is the first time we're reading this TU, and
6351 the TU has an entry in .gdb_index, replace the recorded data from
6352 .gdb_index with this TU. */
6353
6354 find_sig_entry.signature = sig;
6355 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6356 &find_sig_entry, INSERT);
6357 sig_entry = (struct signatured_type *) *slot;
6358
6359 /* We can get here with the TU already read, *or* in the process of being
6360 read. Don't reassign the global entry to point to this DWO if that's
6361 the case. Also note that if the TU is already being read, it may not
6362 have come from a DWO, the program may be a mix of Fission-compiled
6363 code and non-Fission-compiled code. */
6364
6365 /* Have we already tried to read this TU?
6366 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6367 needn't exist in the global table yet). */
6368 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6369 return sig_entry;
6370
6371 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6372 dwo_unit of the TU itself. */
6373 dwo_file = cu->dwo_unit->dwo_file;
6374
6375 /* Ok, this is the first time we're reading this TU. */
6376 if (dwo_file->tus == NULL)
6377 return NULL;
6378 find_dwo_entry.signature = sig;
6379 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6380 &find_dwo_entry);
6381 if (dwo_entry == NULL)
6382 return NULL;
6383
6384 /* If the global table doesn't have an entry for this TU, add one. */
6385 if (sig_entry == NULL)
6386 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6387
6388 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6389 sig_entry->per_cu.tu_read = 1;
6390 return sig_entry;
6391 }
6392
6393 /* Subroutine of lookup_signatured_type.
6394 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6395 then try the DWP file. If the TU stub (skeleton) has been removed then
6396 it won't be in .gdb_index. */
6397
6398 static struct signatured_type *
6399 lookup_dwp_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 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6404 struct dwo_unit *dwo_entry;
6405 struct signatured_type find_sig_entry, *sig_entry;
6406 void **slot;
6407
6408 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6409 gdb_assert (dwp_file != NULL);
6410
6411 /* If TU skeletons have been removed then we may not have read in any
6412 TUs yet. */
6413 if (dwarf2_per_objfile->signatured_types == NULL)
6414 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6415
6416 find_sig_entry.signature = sig;
6417 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6418 &find_sig_entry, INSERT);
6419 sig_entry = (struct signatured_type *) *slot;
6420
6421 /* Have we already tried to read this TU?
6422 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6423 needn't exist in the global table yet). */
6424 if (sig_entry != NULL)
6425 return sig_entry;
6426
6427 if (dwp_file->tus == NULL)
6428 return NULL;
6429 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6430 sig, 1 /* is_debug_types */);
6431 if (dwo_entry == NULL)
6432 return NULL;
6433
6434 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6435 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6436
6437 return sig_entry;
6438 }
6439
6440 /* Lookup a signature based type for DW_FORM_ref_sig8.
6441 Returns NULL if signature SIG is not present in the table.
6442 It is up to the caller to complain about this. */
6443
6444 static struct signatured_type *
6445 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6446 {
6447 struct dwarf2_per_objfile *dwarf2_per_objfile
6448 = cu->per_cu->dwarf2_per_objfile;
6449
6450 if (cu->dwo_unit
6451 && dwarf2_per_objfile->using_index)
6452 {
6453 /* We're in a DWO/DWP file, and we're using .gdb_index.
6454 These cases require special processing. */
6455 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6456 return lookup_dwo_signatured_type (cu, sig);
6457 else
6458 return lookup_dwp_signatured_type (cu, sig);
6459 }
6460 else
6461 {
6462 struct signatured_type find_entry, *entry;
6463
6464 if (dwarf2_per_objfile->signatured_types == NULL)
6465 return NULL;
6466 find_entry.signature = sig;
6467 entry = ((struct signatured_type *)
6468 htab_find (dwarf2_per_objfile->signatured_types.get (),
6469 &find_entry));
6470 return entry;
6471 }
6472 }
6473
6474 /* Return the address base of the compile unit, which, if exists, is stored
6475 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6476 static gdb::optional<ULONGEST>
6477 lookup_addr_base (struct die_info *comp_unit_die)
6478 {
6479 struct attribute *attr;
6480 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6481 if (attr == nullptr)
6482 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6483 if (attr == nullptr)
6484 return gdb::optional<ULONGEST> ();
6485 return DW_UNSND (attr);
6486 }
6487
6488 /* Return range lists base of the compile unit, which, if exists, is stored
6489 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6490 static ULONGEST
6491 lookup_ranges_base (struct die_info *comp_unit_die)
6492 {
6493 struct attribute *attr;
6494 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6495 if (attr == nullptr)
6496 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6497 if (attr == nullptr)
6498 return 0;
6499 return DW_UNSND (attr);
6500 }
6501
6502 /* Low level DIE reading support. */
6503
6504 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6505
6506 static void
6507 init_cu_die_reader (struct die_reader_specs *reader,
6508 struct dwarf2_cu *cu,
6509 struct dwarf2_section_info *section,
6510 struct dwo_file *dwo_file,
6511 struct abbrev_table *abbrev_table)
6512 {
6513 gdb_assert (section->readin && section->buffer != NULL);
6514 reader->abfd = section->get_bfd_owner ();
6515 reader->cu = cu;
6516 reader->dwo_file = dwo_file;
6517 reader->die_section = section;
6518 reader->buffer = section->buffer;
6519 reader->buffer_end = section->buffer + section->size;
6520 reader->abbrev_table = abbrev_table;
6521 }
6522
6523 /* Subroutine of cutu_reader to simplify it.
6524 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6525 There's just a lot of work to do, and cutu_reader is big enough
6526 already.
6527
6528 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6529 from it to the DIE in the DWO. If NULL we are skipping the stub.
6530 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6531 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6532 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6533 STUB_COMP_DIR may be non-NULL.
6534 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6535 are filled in with the info of the DIE from the DWO file.
6536 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6537 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6538 kept around for at least as long as *RESULT_READER.
6539
6540 The result is non-zero if a valid (non-dummy) DIE was found. */
6541
6542 static int
6543 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6544 struct dwo_unit *dwo_unit,
6545 struct die_info *stub_comp_unit_die,
6546 const char *stub_comp_dir,
6547 struct die_reader_specs *result_reader,
6548 const gdb_byte **result_info_ptr,
6549 struct die_info **result_comp_unit_die,
6550 abbrev_table_up *result_dwo_abbrev_table)
6551 {
6552 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6553 struct objfile *objfile = dwarf2_per_objfile->objfile;
6554 struct dwarf2_cu *cu = this_cu->cu;
6555 bfd *abfd;
6556 const gdb_byte *begin_info_ptr, *info_ptr;
6557 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6558 int i,num_extra_attrs;
6559 struct dwarf2_section_info *dwo_abbrev_section;
6560 struct die_info *comp_unit_die;
6561
6562 /* At most one of these may be provided. */
6563 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6564
6565 /* These attributes aren't processed until later:
6566 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6567 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6568 referenced later. However, these attributes are found in the stub
6569 which we won't have later. In order to not impose this complication
6570 on the rest of the code, we read them here and copy them to the
6571 DWO CU/TU die. */
6572
6573 stmt_list = NULL;
6574 low_pc = NULL;
6575 high_pc = NULL;
6576 ranges = NULL;
6577 comp_dir = NULL;
6578
6579 if (stub_comp_unit_die != NULL)
6580 {
6581 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6582 DWO file. */
6583 if (! this_cu->is_debug_types)
6584 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6585 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6586 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6587 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6588 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6589
6590 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6591
6592 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6593 here (if needed). We need the value before we can process
6594 DW_AT_ranges. */
6595 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6596 }
6597 else if (stub_comp_dir != NULL)
6598 {
6599 /* Reconstruct the comp_dir attribute to simplify the code below. */
6600 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6601 comp_dir->name = DW_AT_comp_dir;
6602 comp_dir->form = DW_FORM_string;
6603 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6604 DW_STRING (comp_dir) = stub_comp_dir;
6605 }
6606
6607 /* Set up for reading the DWO CU/TU. */
6608 cu->dwo_unit = dwo_unit;
6609 dwarf2_section_info *section = dwo_unit->section;
6610 section->read (objfile);
6611 abfd = section->get_bfd_owner ();
6612 begin_info_ptr = info_ptr = (section->buffer
6613 + to_underlying (dwo_unit->sect_off));
6614 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6615
6616 if (this_cu->is_debug_types)
6617 {
6618 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6619
6620 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6621 &cu->header, section,
6622 dwo_abbrev_section,
6623 info_ptr, rcuh_kind::TYPE);
6624 /* This is not an assert because it can be caused by bad debug info. */
6625 if (sig_type->signature != cu->header.signature)
6626 {
6627 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6628 " TU at offset %s [in module %s]"),
6629 hex_string (sig_type->signature),
6630 hex_string (cu->header.signature),
6631 sect_offset_str (dwo_unit->sect_off),
6632 bfd_get_filename (abfd));
6633 }
6634 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6635 /* For DWOs coming from DWP files, we don't know the CU length
6636 nor the type's offset in the TU until now. */
6637 dwo_unit->length = cu->header.get_length ();
6638 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6639
6640 /* Establish the type offset that can be used to lookup the type.
6641 For DWO files, we don't know it until now. */
6642 sig_type->type_offset_in_section
6643 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6644 }
6645 else
6646 {
6647 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6648 &cu->header, section,
6649 dwo_abbrev_section,
6650 info_ptr, rcuh_kind::COMPILE);
6651 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6652 /* For DWOs coming from DWP files, we don't know the CU length
6653 until now. */
6654 dwo_unit->length = cu->header.get_length ();
6655 }
6656
6657 *result_dwo_abbrev_table
6658 = abbrev_table::read (objfile, dwo_abbrev_section,
6659 cu->header.abbrev_sect_off);
6660 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6661 result_dwo_abbrev_table->get ());
6662
6663 /* Read in the die, but leave space to copy over the attributes
6664 from the stub. This has the benefit of simplifying the rest of
6665 the code - all the work to maintain the illusion of a single
6666 DW_TAG_{compile,type}_unit DIE is done here. */
6667 num_extra_attrs = ((stmt_list != NULL)
6668 + (low_pc != NULL)
6669 + (high_pc != NULL)
6670 + (ranges != NULL)
6671 + (comp_dir != NULL));
6672 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6673 num_extra_attrs);
6674
6675 /* Copy over the attributes from the stub to the DIE we just read in. */
6676 comp_unit_die = *result_comp_unit_die;
6677 i = comp_unit_die->num_attrs;
6678 if (stmt_list != NULL)
6679 comp_unit_die->attrs[i++] = *stmt_list;
6680 if (low_pc != NULL)
6681 comp_unit_die->attrs[i++] = *low_pc;
6682 if (high_pc != NULL)
6683 comp_unit_die->attrs[i++] = *high_pc;
6684 if (ranges != NULL)
6685 comp_unit_die->attrs[i++] = *ranges;
6686 if (comp_dir != NULL)
6687 comp_unit_die->attrs[i++] = *comp_dir;
6688 comp_unit_die->num_attrs += num_extra_attrs;
6689
6690 if (dwarf_die_debug)
6691 {
6692 fprintf_unfiltered (gdb_stdlog,
6693 "Read die from %s@0x%x of %s:\n",
6694 section->get_name (),
6695 (unsigned) (begin_info_ptr - section->buffer),
6696 bfd_get_filename (abfd));
6697 dump_die (comp_unit_die, dwarf_die_debug);
6698 }
6699
6700 /* Skip dummy compilation units. */
6701 if (info_ptr >= begin_info_ptr + dwo_unit->length
6702 || peek_abbrev_code (abfd, info_ptr) == 0)
6703 return 0;
6704
6705 *result_info_ptr = info_ptr;
6706 return 1;
6707 }
6708
6709 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6710 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6711 signature is part of the header. */
6712 static gdb::optional<ULONGEST>
6713 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6714 {
6715 if (cu->header.version >= 5)
6716 return cu->header.signature;
6717 struct attribute *attr;
6718 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6719 if (attr == nullptr)
6720 return gdb::optional<ULONGEST> ();
6721 return DW_UNSND (attr);
6722 }
6723
6724 /* Subroutine of cutu_reader to simplify it.
6725 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6726 Returns NULL if the specified DWO unit cannot be found. */
6727
6728 static struct dwo_unit *
6729 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6730 struct die_info *comp_unit_die,
6731 const char *dwo_name)
6732 {
6733 struct dwarf2_cu *cu = this_cu->cu;
6734 struct dwo_unit *dwo_unit;
6735 const char *comp_dir;
6736
6737 gdb_assert (cu != NULL);
6738
6739 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6740 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6741 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6742
6743 if (this_cu->is_debug_types)
6744 {
6745 struct signatured_type *sig_type;
6746
6747 /* Since this_cu is the first member of struct signatured_type,
6748 we can go from a pointer to one to a pointer to the other. */
6749 sig_type = (struct signatured_type *) this_cu;
6750 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6751 }
6752 else
6753 {
6754 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6755 if (!signature.has_value ())
6756 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6757 " [in module %s]"),
6758 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6759 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6760 *signature);
6761 }
6762
6763 return dwo_unit;
6764 }
6765
6766 /* Subroutine of cutu_reader to simplify it.
6767 See it for a description of the parameters.
6768 Read a TU directly from a DWO file, bypassing the stub. */
6769
6770 void
6771 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6772 int use_existing_cu)
6773 {
6774 struct signatured_type *sig_type;
6775
6776 /* Verify we can do the following downcast, and that we have the
6777 data we need. */
6778 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6779 sig_type = (struct signatured_type *) this_cu;
6780 gdb_assert (sig_type->dwo_unit != NULL);
6781
6782 if (use_existing_cu && this_cu->cu != NULL)
6783 {
6784 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6785 /* There's no need to do the rereading_dwo_cu handling that
6786 cutu_reader does since we don't read the stub. */
6787 }
6788 else
6789 {
6790 /* If !use_existing_cu, this_cu->cu must be NULL. */
6791 gdb_assert (this_cu->cu == NULL);
6792 m_new_cu.reset (new dwarf2_cu (this_cu));
6793 }
6794
6795 /* A future optimization, if needed, would be to use an existing
6796 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6797 could share abbrev tables. */
6798
6799 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6800 NULL /* stub_comp_unit_die */,
6801 sig_type->dwo_unit->dwo_file->comp_dir,
6802 this, &info_ptr,
6803 &comp_unit_die,
6804 &m_dwo_abbrev_table) == 0)
6805 {
6806 /* Dummy die. */
6807 dummy_p = true;
6808 }
6809 }
6810
6811 /* Initialize a CU (or TU) and read its DIEs.
6812 If the CU defers to a DWO file, read the DWO file as well.
6813
6814 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6815 Otherwise the table specified in the comp unit header is read in and used.
6816 This is an optimization for when we already have the abbrev table.
6817
6818 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6819 Otherwise, a new CU is allocated with xmalloc. */
6820
6821 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6822 struct abbrev_table *abbrev_table,
6823 int use_existing_cu,
6824 bool skip_partial)
6825 : die_reader_specs {},
6826 m_this_cu (this_cu)
6827 {
6828 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6829 struct objfile *objfile = dwarf2_per_objfile->objfile;
6830 struct dwarf2_section_info *section = this_cu->section;
6831 bfd *abfd = section->get_bfd_owner ();
6832 struct dwarf2_cu *cu;
6833 const gdb_byte *begin_info_ptr;
6834 struct signatured_type *sig_type = NULL;
6835 struct dwarf2_section_info *abbrev_section;
6836 /* Non-zero if CU currently points to a DWO file and we need to
6837 reread it. When this happens we need to reread the skeleton die
6838 before we can reread the DWO file (this only applies to CUs, not TUs). */
6839 int rereading_dwo_cu = 0;
6840
6841 if (dwarf_die_debug)
6842 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6843 this_cu->is_debug_types ? "type" : "comp",
6844 sect_offset_str (this_cu->sect_off));
6845
6846 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6847 file (instead of going through the stub), short-circuit all of this. */
6848 if (this_cu->reading_dwo_directly)
6849 {
6850 /* Narrow down the scope of possibilities to have to understand. */
6851 gdb_assert (this_cu->is_debug_types);
6852 gdb_assert (abbrev_table == NULL);
6853 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6854 return;
6855 }
6856
6857 /* This is cheap if the section is already read in. */
6858 section->read (objfile);
6859
6860 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6861
6862 abbrev_section = get_abbrev_section_for_cu (this_cu);
6863
6864 if (use_existing_cu && this_cu->cu != NULL)
6865 {
6866 cu = this_cu->cu;
6867 /* If this CU is from a DWO file we need to start over, we need to
6868 refetch the attributes from the skeleton CU.
6869 This could be optimized by retrieving those attributes from when we
6870 were here the first time: the previous comp_unit_die was stored in
6871 comp_unit_obstack. But there's no data yet that we need this
6872 optimization. */
6873 if (cu->dwo_unit != NULL)
6874 rereading_dwo_cu = 1;
6875 }
6876 else
6877 {
6878 /* If !use_existing_cu, this_cu->cu must be NULL. */
6879 gdb_assert (this_cu->cu == NULL);
6880 m_new_cu.reset (new dwarf2_cu (this_cu));
6881 cu = m_new_cu.get ();
6882 }
6883
6884 /* Get the header. */
6885 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6886 {
6887 /* We already have the header, there's no need to read it in again. */
6888 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6889 }
6890 else
6891 {
6892 if (this_cu->is_debug_types)
6893 {
6894 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6895 &cu->header, section,
6896 abbrev_section, info_ptr,
6897 rcuh_kind::TYPE);
6898
6899 /* Since per_cu is the first member of struct signatured_type,
6900 we can go from a pointer to one to a pointer to the other. */
6901 sig_type = (struct signatured_type *) this_cu;
6902 gdb_assert (sig_type->signature == cu->header.signature);
6903 gdb_assert (sig_type->type_offset_in_tu
6904 == cu->header.type_cu_offset_in_tu);
6905 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6906
6907 /* LENGTH has not been set yet for type units if we're
6908 using .gdb_index. */
6909 this_cu->length = cu->header.get_length ();
6910
6911 /* Establish the type offset that can be used to lookup the type. */
6912 sig_type->type_offset_in_section =
6913 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6914
6915 this_cu->dwarf_version = cu->header.version;
6916 }
6917 else
6918 {
6919 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6920 &cu->header, section,
6921 abbrev_section,
6922 info_ptr,
6923 rcuh_kind::COMPILE);
6924
6925 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6926 gdb_assert (this_cu->length == cu->header.get_length ());
6927 this_cu->dwarf_version = cu->header.version;
6928 }
6929 }
6930
6931 /* Skip dummy compilation units. */
6932 if (info_ptr >= begin_info_ptr + this_cu->length
6933 || peek_abbrev_code (abfd, info_ptr) == 0)
6934 {
6935 dummy_p = true;
6936 return;
6937 }
6938
6939 /* If we don't have them yet, read the abbrevs for this compilation unit.
6940 And if we need to read them now, make sure they're freed when we're
6941 done. */
6942 if (abbrev_table != NULL)
6943 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6944 else
6945 {
6946 m_abbrev_table_holder
6947 = abbrev_table::read (objfile, abbrev_section,
6948 cu->header.abbrev_sect_off);
6949 abbrev_table = m_abbrev_table_holder.get ();
6950 }
6951
6952 /* Read the top level CU/TU die. */
6953 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6954 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6955
6956 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6957 {
6958 dummy_p = true;
6959 return;
6960 }
6961
6962 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6963 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6964 table from the DWO file and pass the ownership over to us. It will be
6965 referenced from READER, so we must make sure to free it after we're done
6966 with READER.
6967
6968 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6969 DWO CU, that this test will fail (the attribute will not be present). */
6970 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6971 if (dwo_name != nullptr)
6972 {
6973 struct dwo_unit *dwo_unit;
6974 struct die_info *dwo_comp_unit_die;
6975
6976 if (comp_unit_die->has_children)
6977 {
6978 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6979 " has children (offset %s) [in module %s]"),
6980 sect_offset_str (this_cu->sect_off),
6981 bfd_get_filename (abfd));
6982 }
6983 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6984 if (dwo_unit != NULL)
6985 {
6986 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6987 comp_unit_die, NULL,
6988 this, &info_ptr,
6989 &dwo_comp_unit_die,
6990 &m_dwo_abbrev_table) == 0)
6991 {
6992 /* Dummy die. */
6993 dummy_p = true;
6994 return;
6995 }
6996 comp_unit_die = dwo_comp_unit_die;
6997 }
6998 else
6999 {
7000 /* Yikes, we couldn't find the rest of the DIE, we only have
7001 the stub. A complaint has already been logged. There's
7002 not much more we can do except pass on the stub DIE to
7003 die_reader_func. We don't want to throw an error on bad
7004 debug info. */
7005 }
7006 }
7007 }
7008
7009 void
7010 cutu_reader::keep ()
7011 {
7012 /* Done, clean up. */
7013 gdb_assert (!dummy_p);
7014 if (m_new_cu != NULL)
7015 {
7016 struct dwarf2_per_objfile *dwarf2_per_objfile
7017 = m_this_cu->dwarf2_per_objfile;
7018 /* Link this CU into read_in_chain. */
7019 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7020 dwarf2_per_objfile->read_in_chain = m_this_cu;
7021 /* The chain owns it now. */
7022 m_new_cu.release ();
7023 }
7024 }
7025
7026 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7027 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7028 assumed to have already done the lookup to find the DWO file).
7029
7030 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7031 THIS_CU->is_debug_types, but nothing else.
7032
7033 We fill in THIS_CU->length.
7034
7035 THIS_CU->cu is always freed when done.
7036 This is done in order to not leave THIS_CU->cu in a state where we have
7037 to care whether it refers to the "main" CU or the DWO CU.
7038
7039 When parent_cu is passed, it is used to provide a default value for
7040 str_offsets_base and addr_base from the parent. */
7041
7042 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7043 struct dwarf2_cu *parent_cu,
7044 struct dwo_file *dwo_file)
7045 : die_reader_specs {},
7046 m_this_cu (this_cu)
7047 {
7048 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7049 struct objfile *objfile = dwarf2_per_objfile->objfile;
7050 struct dwarf2_section_info *section = this_cu->section;
7051 bfd *abfd = section->get_bfd_owner ();
7052 struct dwarf2_section_info *abbrev_section;
7053 const gdb_byte *begin_info_ptr, *info_ptr;
7054
7055 if (dwarf_die_debug)
7056 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7057 this_cu->is_debug_types ? "type" : "comp",
7058 sect_offset_str (this_cu->sect_off));
7059
7060 gdb_assert (this_cu->cu == NULL);
7061
7062 abbrev_section = (dwo_file != NULL
7063 ? &dwo_file->sections.abbrev
7064 : get_abbrev_section_for_cu (this_cu));
7065
7066 /* This is cheap if the section is already read in. */
7067 section->read (objfile);
7068
7069 m_new_cu.reset (new dwarf2_cu (this_cu));
7070
7071 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7072 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7073 &m_new_cu->header, section,
7074 abbrev_section, info_ptr,
7075 (this_cu->is_debug_types
7076 ? rcuh_kind::TYPE
7077 : rcuh_kind::COMPILE));
7078
7079 if (parent_cu != nullptr)
7080 {
7081 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7082 m_new_cu->addr_base = parent_cu->addr_base;
7083 }
7084 this_cu->length = m_new_cu->header.get_length ();
7085
7086 /* Skip dummy compilation units. */
7087 if (info_ptr >= begin_info_ptr + this_cu->length
7088 || peek_abbrev_code (abfd, info_ptr) == 0)
7089 {
7090 dummy_p = true;
7091 return;
7092 }
7093
7094 m_abbrev_table_holder
7095 = abbrev_table::read (objfile, abbrev_section,
7096 m_new_cu->header.abbrev_sect_off);
7097
7098 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7099 m_abbrev_table_holder.get ());
7100 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7101 }
7102
7103 \f
7104 /* Type Unit Groups.
7105
7106 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7107 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7108 so that all types coming from the same compilation (.o file) are grouped
7109 together. A future step could be to put the types in the same symtab as
7110 the CU the types ultimately came from. */
7111
7112 static hashval_t
7113 hash_type_unit_group (const void *item)
7114 {
7115 const struct type_unit_group *tu_group
7116 = (const struct type_unit_group *) item;
7117
7118 return hash_stmt_list_entry (&tu_group->hash);
7119 }
7120
7121 static int
7122 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7123 {
7124 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7125 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7126
7127 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7128 }
7129
7130 /* Allocate a hash table for type unit groups. */
7131
7132 static htab_up
7133 allocate_type_unit_groups_table ()
7134 {
7135 return htab_up (htab_create_alloc (3,
7136 hash_type_unit_group,
7137 eq_type_unit_group,
7138 NULL, xcalloc, xfree));
7139 }
7140
7141 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7142 partial symtabs. We combine several TUs per psymtab to not let the size
7143 of any one psymtab grow too big. */
7144 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7145 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7146
7147 /* Helper routine for get_type_unit_group.
7148 Create the type_unit_group object used to hold one or more TUs. */
7149
7150 static struct type_unit_group *
7151 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7152 {
7153 struct dwarf2_per_objfile *dwarf2_per_objfile
7154 = cu->per_cu->dwarf2_per_objfile;
7155 struct objfile *objfile = dwarf2_per_objfile->objfile;
7156 struct dwarf2_per_cu_data *per_cu;
7157 struct type_unit_group *tu_group;
7158
7159 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7160 struct type_unit_group);
7161 per_cu = &tu_group->per_cu;
7162 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7163
7164 if (dwarf2_per_objfile->using_index)
7165 {
7166 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7167 struct dwarf2_per_cu_quick_data);
7168 }
7169 else
7170 {
7171 unsigned int line_offset = to_underlying (line_offset_struct);
7172 dwarf2_psymtab *pst;
7173 std::string name;
7174
7175 /* Give the symtab a useful name for debug purposes. */
7176 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7177 name = string_printf ("<type_units_%d>",
7178 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7179 else
7180 name = string_printf ("<type_units_at_0x%x>", line_offset);
7181
7182 pst = create_partial_symtab (per_cu, name.c_str ());
7183 pst->anonymous = true;
7184 }
7185
7186 tu_group->hash.dwo_unit = cu->dwo_unit;
7187 tu_group->hash.line_sect_off = line_offset_struct;
7188
7189 return tu_group;
7190 }
7191
7192 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7193 STMT_LIST is a DW_AT_stmt_list attribute. */
7194
7195 static struct type_unit_group *
7196 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7197 {
7198 struct dwarf2_per_objfile *dwarf2_per_objfile
7199 = cu->per_cu->dwarf2_per_objfile;
7200 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7201 struct type_unit_group *tu_group;
7202 void **slot;
7203 unsigned int line_offset;
7204 struct type_unit_group type_unit_group_for_lookup;
7205
7206 if (dwarf2_per_objfile->type_unit_groups == NULL)
7207 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7208
7209 /* Do we need to create a new group, or can we use an existing one? */
7210
7211 if (stmt_list)
7212 {
7213 line_offset = DW_UNSND (stmt_list);
7214 ++tu_stats->nr_symtab_sharers;
7215 }
7216 else
7217 {
7218 /* Ugh, no stmt_list. Rare, but we have to handle it.
7219 We can do various things here like create one group per TU or
7220 spread them over multiple groups to split up the expansion work.
7221 To avoid worst case scenarios (too many groups or too large groups)
7222 we, umm, group them in bunches. */
7223 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7224 | (tu_stats->nr_stmt_less_type_units
7225 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7226 ++tu_stats->nr_stmt_less_type_units;
7227 }
7228
7229 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7230 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7231 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7232 &type_unit_group_for_lookup, INSERT);
7233 if (*slot != NULL)
7234 {
7235 tu_group = (struct type_unit_group *) *slot;
7236 gdb_assert (tu_group != NULL);
7237 }
7238 else
7239 {
7240 sect_offset line_offset_struct = (sect_offset) line_offset;
7241 tu_group = create_type_unit_group (cu, line_offset_struct);
7242 *slot = tu_group;
7243 ++tu_stats->nr_symtabs;
7244 }
7245
7246 return tu_group;
7247 }
7248 \f
7249 /* Partial symbol tables. */
7250
7251 /* Create a psymtab named NAME and assign it to PER_CU.
7252
7253 The caller must fill in the following details:
7254 dirname, textlow, texthigh. */
7255
7256 static dwarf2_psymtab *
7257 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7258 {
7259 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7260 dwarf2_psymtab *pst;
7261
7262 pst = new dwarf2_psymtab (name, objfile, 0);
7263
7264 pst->psymtabs_addrmap_supported = true;
7265
7266 /* This is the glue that links PST into GDB's symbol API. */
7267 pst->per_cu_data = per_cu;
7268 per_cu->v.psymtab = pst;
7269
7270 return pst;
7271 }
7272
7273 /* DIE reader function for process_psymtab_comp_unit. */
7274
7275 static void
7276 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7277 const gdb_byte *info_ptr,
7278 struct die_info *comp_unit_die,
7279 enum language pretend_language)
7280 {
7281 struct dwarf2_cu *cu = reader->cu;
7282 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7283 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7284 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7285 CORE_ADDR baseaddr;
7286 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7287 dwarf2_psymtab *pst;
7288 enum pc_bounds_kind cu_bounds_kind;
7289 const char *filename;
7290
7291 gdb_assert (! per_cu->is_debug_types);
7292
7293 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7294
7295 /* Allocate a new partial symbol table structure. */
7296 gdb::unique_xmalloc_ptr<char> debug_filename;
7297 static const char artificial[] = "<artificial>";
7298 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7299 if (filename == NULL)
7300 filename = "";
7301 else if (strcmp (filename, artificial) == 0)
7302 {
7303 debug_filename.reset (concat (artificial, "@",
7304 sect_offset_str (per_cu->sect_off),
7305 (char *) NULL));
7306 filename = debug_filename.get ();
7307 }
7308
7309 pst = create_partial_symtab (per_cu, filename);
7310
7311 /* This must be done before calling dwarf2_build_include_psymtabs. */
7312 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7313
7314 baseaddr = objfile->text_section_offset ();
7315
7316 dwarf2_find_base_address (comp_unit_die, cu);
7317
7318 /* Possibly set the default values of LOWPC and HIGHPC from
7319 `DW_AT_ranges'. */
7320 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7321 &best_highpc, cu, pst);
7322 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7323 {
7324 CORE_ADDR low
7325 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7326 - baseaddr);
7327 CORE_ADDR high
7328 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7329 - baseaddr - 1);
7330 /* Store the contiguous range if it is not empty; it can be
7331 empty for CUs with no code. */
7332 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7333 low, high, pst);
7334 }
7335
7336 /* Check if comp unit has_children.
7337 If so, read the rest of the partial symbols from this comp unit.
7338 If not, there's no more debug_info for this comp unit. */
7339 if (comp_unit_die->has_children)
7340 {
7341 struct partial_die_info *first_die;
7342 CORE_ADDR lowpc, highpc;
7343
7344 lowpc = ((CORE_ADDR) -1);
7345 highpc = ((CORE_ADDR) 0);
7346
7347 first_die = load_partial_dies (reader, info_ptr, 1);
7348
7349 scan_partial_symbols (first_die, &lowpc, &highpc,
7350 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7351
7352 /* If we didn't find a lowpc, set it to highpc to avoid
7353 complaints from `maint check'. */
7354 if (lowpc == ((CORE_ADDR) -1))
7355 lowpc = highpc;
7356
7357 /* If the compilation unit didn't have an explicit address range,
7358 then use the information extracted from its child dies. */
7359 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7360 {
7361 best_lowpc = lowpc;
7362 best_highpc = highpc;
7363 }
7364 }
7365 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7366 best_lowpc + baseaddr)
7367 - baseaddr);
7368 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7369 best_highpc + baseaddr)
7370 - baseaddr);
7371
7372 end_psymtab_common (objfile, pst);
7373
7374 if (!cu->per_cu->imported_symtabs_empty ())
7375 {
7376 int i;
7377 int len = cu->per_cu->imported_symtabs_size ();
7378
7379 /* Fill in 'dependencies' here; we fill in 'users' in a
7380 post-pass. */
7381 pst->number_of_dependencies = len;
7382 pst->dependencies
7383 = objfile->partial_symtabs->allocate_dependencies (len);
7384 for (i = 0; i < len; ++i)
7385 {
7386 pst->dependencies[i]
7387 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7388 }
7389
7390 cu->per_cu->imported_symtabs_free ();
7391 }
7392
7393 /* Get the list of files included in the current compilation unit,
7394 and build a psymtab for each of them. */
7395 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7396
7397 if (dwarf_read_debug)
7398 fprintf_unfiltered (gdb_stdlog,
7399 "Psymtab for %s unit @%s: %s - %s"
7400 ", %d global, %d static syms\n",
7401 per_cu->is_debug_types ? "type" : "comp",
7402 sect_offset_str (per_cu->sect_off),
7403 paddress (gdbarch, pst->text_low (objfile)),
7404 paddress (gdbarch, pst->text_high (objfile)),
7405 pst->n_global_syms, pst->n_static_syms);
7406 }
7407
7408 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7409 Process compilation unit THIS_CU for a psymtab. */
7410
7411 static void
7412 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7413 bool want_partial_unit,
7414 enum language pretend_language)
7415 {
7416 /* If this compilation unit was already read in, free the
7417 cached copy in order to read it in again. This is
7418 necessary because we skipped some symbols when we first
7419 read in the compilation unit (see load_partial_dies).
7420 This problem could be avoided, but the benefit is unclear. */
7421 if (this_cu->cu != NULL)
7422 free_one_cached_comp_unit (this_cu);
7423
7424 cutu_reader reader (this_cu, NULL, 0, false);
7425
7426 switch (reader.comp_unit_die->tag)
7427 {
7428 case DW_TAG_compile_unit:
7429 this_cu->unit_type = DW_UT_compile;
7430 break;
7431 case DW_TAG_partial_unit:
7432 this_cu->unit_type = DW_UT_partial;
7433 break;
7434 default:
7435 abort ();
7436 }
7437
7438 if (reader.dummy_p)
7439 {
7440 /* Nothing. */
7441 }
7442 else if (this_cu->is_debug_types)
7443 build_type_psymtabs_reader (&reader, reader.info_ptr,
7444 reader.comp_unit_die);
7445 else if (want_partial_unit
7446 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7447 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7448 reader.comp_unit_die,
7449 pretend_language);
7450
7451 this_cu->lang = this_cu->cu->language;
7452
7453 /* Age out any secondary CUs. */
7454 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7455 }
7456
7457 /* Reader function for build_type_psymtabs. */
7458
7459 static void
7460 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7461 const gdb_byte *info_ptr,
7462 struct die_info *type_unit_die)
7463 {
7464 struct dwarf2_per_objfile *dwarf2_per_objfile
7465 = reader->cu->per_cu->dwarf2_per_objfile;
7466 struct objfile *objfile = dwarf2_per_objfile->objfile;
7467 struct dwarf2_cu *cu = reader->cu;
7468 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7469 struct signatured_type *sig_type;
7470 struct type_unit_group *tu_group;
7471 struct attribute *attr;
7472 struct partial_die_info *first_die;
7473 CORE_ADDR lowpc, highpc;
7474 dwarf2_psymtab *pst;
7475
7476 gdb_assert (per_cu->is_debug_types);
7477 sig_type = (struct signatured_type *) per_cu;
7478
7479 if (! type_unit_die->has_children)
7480 return;
7481
7482 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7483 tu_group = get_type_unit_group (cu, attr);
7484
7485 if (tu_group->tus == nullptr)
7486 tu_group->tus = new std::vector<signatured_type *>;
7487 tu_group->tus->push_back (sig_type);
7488
7489 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7490 pst = create_partial_symtab (per_cu, "");
7491 pst->anonymous = true;
7492
7493 first_die = load_partial_dies (reader, info_ptr, 1);
7494
7495 lowpc = (CORE_ADDR) -1;
7496 highpc = (CORE_ADDR) 0;
7497 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7498
7499 end_psymtab_common (objfile, pst);
7500 }
7501
7502 /* Struct used to sort TUs by their abbreviation table offset. */
7503
7504 struct tu_abbrev_offset
7505 {
7506 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7507 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7508 {}
7509
7510 signatured_type *sig_type;
7511 sect_offset abbrev_offset;
7512 };
7513
7514 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7515
7516 static bool
7517 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7518 const struct tu_abbrev_offset &b)
7519 {
7520 return a.abbrev_offset < b.abbrev_offset;
7521 }
7522
7523 /* Efficiently read all the type units.
7524 This does the bulk of the work for build_type_psymtabs.
7525
7526 The efficiency is because we sort TUs by the abbrev table they use and
7527 only read each abbrev table once. In one program there are 200K TUs
7528 sharing 8K abbrev tables.
7529
7530 The main purpose of this function is to support building the
7531 dwarf2_per_objfile->type_unit_groups table.
7532 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7533 can collapse the search space by grouping them by stmt_list.
7534 The savings can be significant, in the same program from above the 200K TUs
7535 share 8K stmt_list tables.
7536
7537 FUNC is expected to call get_type_unit_group, which will create the
7538 struct type_unit_group if necessary and add it to
7539 dwarf2_per_objfile->type_unit_groups. */
7540
7541 static void
7542 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7543 {
7544 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7545 abbrev_table_up abbrev_table;
7546 sect_offset abbrev_offset;
7547
7548 /* It's up to the caller to not call us multiple times. */
7549 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7550
7551 if (dwarf2_per_objfile->all_type_units.empty ())
7552 return;
7553
7554 /* TUs typically share abbrev tables, and there can be way more TUs than
7555 abbrev tables. Sort by abbrev table to reduce the number of times we
7556 read each abbrev table in.
7557 Alternatives are to punt or to maintain a cache of abbrev tables.
7558 This is simpler and efficient enough for now.
7559
7560 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7561 symtab to use). Typically TUs with the same abbrev offset have the same
7562 stmt_list value too so in practice this should work well.
7563
7564 The basic algorithm here is:
7565
7566 sort TUs by abbrev table
7567 for each TU with same abbrev table:
7568 read abbrev table if first user
7569 read TU top level DIE
7570 [IWBN if DWO skeletons had DW_AT_stmt_list]
7571 call FUNC */
7572
7573 if (dwarf_read_debug)
7574 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7575
7576 /* Sort in a separate table to maintain the order of all_type_units
7577 for .gdb_index: TU indices directly index all_type_units. */
7578 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7579 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7580
7581 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7582 sorted_by_abbrev.emplace_back
7583 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7584 sig_type->per_cu.section,
7585 sig_type->per_cu.sect_off));
7586
7587 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7588 sort_tu_by_abbrev_offset);
7589
7590 abbrev_offset = (sect_offset) ~(unsigned) 0;
7591
7592 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7593 {
7594 /* Switch to the next abbrev table if necessary. */
7595 if (abbrev_table == NULL
7596 || tu.abbrev_offset != abbrev_offset)
7597 {
7598 abbrev_offset = tu.abbrev_offset;
7599 abbrev_table =
7600 abbrev_table::read (dwarf2_per_objfile->objfile,
7601 &dwarf2_per_objfile->abbrev,
7602 abbrev_offset);
7603 ++tu_stats->nr_uniq_abbrev_tables;
7604 }
7605
7606 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7607 0, false);
7608 if (!reader.dummy_p)
7609 build_type_psymtabs_reader (&reader, reader.info_ptr,
7610 reader.comp_unit_die);
7611 }
7612 }
7613
7614 /* Print collected type unit statistics. */
7615
7616 static void
7617 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7618 {
7619 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7620
7621 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7622 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7623 dwarf2_per_objfile->all_type_units.size ());
7624 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7625 tu_stats->nr_uniq_abbrev_tables);
7626 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7627 tu_stats->nr_symtabs);
7628 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7629 tu_stats->nr_symtab_sharers);
7630 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7631 tu_stats->nr_stmt_less_type_units);
7632 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7633 tu_stats->nr_all_type_units_reallocs);
7634 }
7635
7636 /* Traversal function for build_type_psymtabs. */
7637
7638 static int
7639 build_type_psymtab_dependencies (void **slot, void *info)
7640 {
7641 struct dwarf2_per_objfile *dwarf2_per_objfile
7642 = (struct dwarf2_per_objfile *) info;
7643 struct objfile *objfile = dwarf2_per_objfile->objfile;
7644 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7645 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7646 dwarf2_psymtab *pst = per_cu->v.psymtab;
7647 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7648 int i;
7649
7650 gdb_assert (len > 0);
7651 gdb_assert (per_cu->type_unit_group_p ());
7652
7653 pst->number_of_dependencies = len;
7654 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7655 for (i = 0; i < len; ++i)
7656 {
7657 struct signatured_type *iter = tu_group->tus->at (i);
7658 gdb_assert (iter->per_cu.is_debug_types);
7659 pst->dependencies[i] = iter->per_cu.v.psymtab;
7660 iter->type_unit_group = tu_group;
7661 }
7662
7663 delete tu_group->tus;
7664 tu_group->tus = nullptr;
7665
7666 return 1;
7667 }
7668
7669 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7670 Build partial symbol tables for the .debug_types comp-units. */
7671
7672 static void
7673 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7674 {
7675 if (! create_all_type_units (dwarf2_per_objfile))
7676 return;
7677
7678 build_type_psymtabs_1 (dwarf2_per_objfile);
7679 }
7680
7681 /* Traversal function for process_skeletonless_type_unit.
7682 Read a TU in a DWO file and build partial symbols for it. */
7683
7684 static int
7685 process_skeletonless_type_unit (void **slot, void *info)
7686 {
7687 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7688 struct dwarf2_per_objfile *dwarf2_per_objfile
7689 = (struct dwarf2_per_objfile *) info;
7690 struct signatured_type find_entry, *entry;
7691
7692 /* If this TU doesn't exist in the global table, add it and read it in. */
7693
7694 if (dwarf2_per_objfile->signatured_types == NULL)
7695 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7696
7697 find_entry.signature = dwo_unit->signature;
7698 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7699 &find_entry, INSERT);
7700 /* If we've already seen this type there's nothing to do. What's happening
7701 is we're doing our own version of comdat-folding here. */
7702 if (*slot != NULL)
7703 return 1;
7704
7705 /* This does the job that create_all_type_units would have done for
7706 this TU. */
7707 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7708 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7709 *slot = entry;
7710
7711 /* This does the job that build_type_psymtabs_1 would have done. */
7712 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7713 if (!reader.dummy_p)
7714 build_type_psymtabs_reader (&reader, reader.info_ptr,
7715 reader.comp_unit_die);
7716
7717 return 1;
7718 }
7719
7720 /* Traversal function for process_skeletonless_type_units. */
7721
7722 static int
7723 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7724 {
7725 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7726
7727 if (dwo_file->tus != NULL)
7728 htab_traverse_noresize (dwo_file->tus.get (),
7729 process_skeletonless_type_unit, info);
7730
7731 return 1;
7732 }
7733
7734 /* Scan all TUs of DWO files, verifying we've processed them.
7735 This is needed in case a TU was emitted without its skeleton.
7736 Note: This can't be done until we know what all the DWO files are. */
7737
7738 static void
7739 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7740 {
7741 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7742 if (get_dwp_file (dwarf2_per_objfile) == NULL
7743 && dwarf2_per_objfile->dwo_files != NULL)
7744 {
7745 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7746 process_dwo_file_for_skeletonless_type_units,
7747 dwarf2_per_objfile);
7748 }
7749 }
7750
7751 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7752
7753 static void
7754 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7755 {
7756 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7757 {
7758 dwarf2_psymtab *pst = per_cu->v.psymtab;
7759
7760 if (pst == NULL)
7761 continue;
7762
7763 for (int j = 0; j < pst->number_of_dependencies; ++j)
7764 {
7765 /* Set the 'user' field only if it is not already set. */
7766 if (pst->dependencies[j]->user == NULL)
7767 pst->dependencies[j]->user = pst;
7768 }
7769 }
7770 }
7771
7772 /* Build the partial symbol table by doing a quick pass through the
7773 .debug_info and .debug_abbrev sections. */
7774
7775 static void
7776 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7777 {
7778 struct objfile *objfile = dwarf2_per_objfile->objfile;
7779
7780 if (dwarf_read_debug)
7781 {
7782 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7783 objfile_name (objfile));
7784 }
7785
7786 scoped_restore restore_reading_psyms
7787 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7788 true);
7789
7790 dwarf2_per_objfile->info.read (objfile);
7791
7792 /* Any cached compilation units will be linked by the per-objfile
7793 read_in_chain. Make sure to free them when we're done. */
7794 free_cached_comp_units freer (dwarf2_per_objfile);
7795
7796 build_type_psymtabs (dwarf2_per_objfile);
7797
7798 create_all_comp_units (dwarf2_per_objfile);
7799
7800 /* Create a temporary address map on a temporary obstack. We later
7801 copy this to the final obstack. */
7802 auto_obstack temp_obstack;
7803
7804 scoped_restore save_psymtabs_addrmap
7805 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7806 addrmap_create_mutable (&temp_obstack));
7807
7808 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7809 process_psymtab_comp_unit (per_cu, false, language_minimal);
7810
7811 /* This has to wait until we read the CUs, we need the list of DWOs. */
7812 process_skeletonless_type_units (dwarf2_per_objfile);
7813
7814 /* Now that all TUs have been processed we can fill in the dependencies. */
7815 if (dwarf2_per_objfile->type_unit_groups != NULL)
7816 {
7817 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7818 build_type_psymtab_dependencies, dwarf2_per_objfile);
7819 }
7820
7821 if (dwarf_read_debug)
7822 print_tu_stats (dwarf2_per_objfile);
7823
7824 set_partial_user (dwarf2_per_objfile);
7825
7826 objfile->partial_symtabs->psymtabs_addrmap
7827 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7828 objfile->partial_symtabs->obstack ());
7829 /* At this point we want to keep the address map. */
7830 save_psymtabs_addrmap.release ();
7831
7832 if (dwarf_read_debug)
7833 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7834 objfile_name (objfile));
7835 }
7836
7837 /* Load the partial DIEs for a secondary CU into memory.
7838 This is also used when rereading a primary CU with load_all_dies. */
7839
7840 static void
7841 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7842 {
7843 cutu_reader reader (this_cu, NULL, 1, false);
7844
7845 if (!reader.dummy_p)
7846 {
7847 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7848 language_minimal);
7849
7850 /* Check if comp unit has_children.
7851 If so, read the rest of the partial symbols from this comp unit.
7852 If not, there's no more debug_info for this comp unit. */
7853 if (reader.comp_unit_die->has_children)
7854 load_partial_dies (&reader, reader.info_ptr, 0);
7855
7856 reader.keep ();
7857 }
7858 }
7859
7860 static void
7861 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7862 struct dwarf2_section_info *section,
7863 struct dwarf2_section_info *abbrev_section,
7864 unsigned int is_dwz)
7865 {
7866 const gdb_byte *info_ptr;
7867 struct objfile *objfile = dwarf2_per_objfile->objfile;
7868
7869 if (dwarf_read_debug)
7870 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7871 section->get_name (),
7872 section->get_file_name ());
7873
7874 section->read (objfile);
7875
7876 info_ptr = section->buffer;
7877
7878 while (info_ptr < section->buffer + section->size)
7879 {
7880 struct dwarf2_per_cu_data *this_cu;
7881
7882 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7883
7884 comp_unit_head cu_header;
7885 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7886 abbrev_section, info_ptr,
7887 rcuh_kind::COMPILE);
7888
7889 /* Save the compilation unit for later lookup. */
7890 if (cu_header.unit_type != DW_UT_type)
7891 {
7892 this_cu = XOBNEW (&objfile->objfile_obstack,
7893 struct dwarf2_per_cu_data);
7894 memset (this_cu, 0, sizeof (*this_cu));
7895 }
7896 else
7897 {
7898 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7899 struct signatured_type);
7900 memset (sig_type, 0, sizeof (*sig_type));
7901 sig_type->signature = cu_header.signature;
7902 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7903 this_cu = &sig_type->per_cu;
7904 }
7905 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7906 this_cu->sect_off = sect_off;
7907 this_cu->length = cu_header.length + cu_header.initial_length_size;
7908 this_cu->is_dwz = is_dwz;
7909 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7910 this_cu->section = section;
7911
7912 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7913
7914 info_ptr = info_ptr + this_cu->length;
7915 }
7916 }
7917
7918 /* Create a list of all compilation units in OBJFILE.
7919 This is only done for -readnow and building partial symtabs. */
7920
7921 static void
7922 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7923 {
7924 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7925 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7926 &dwarf2_per_objfile->abbrev, 0);
7927
7928 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7929 if (dwz != NULL)
7930 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7931 1);
7932 }
7933
7934 /* Process all loaded DIEs for compilation unit CU, starting at
7935 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7936 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7937 DW_AT_ranges). See the comments of add_partial_subprogram on how
7938 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7939
7940 static void
7941 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7942 CORE_ADDR *highpc, int set_addrmap,
7943 struct dwarf2_cu *cu)
7944 {
7945 struct partial_die_info *pdi;
7946
7947 /* Now, march along the PDI's, descending into ones which have
7948 interesting children but skipping the children of the other ones,
7949 until we reach the end of the compilation unit. */
7950
7951 pdi = first_die;
7952
7953 while (pdi != NULL)
7954 {
7955 pdi->fixup (cu);
7956
7957 /* Anonymous namespaces or modules have no name but have interesting
7958 children, so we need to look at them. Ditto for anonymous
7959 enums. */
7960
7961 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7962 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7963 || pdi->tag == DW_TAG_imported_unit
7964 || pdi->tag == DW_TAG_inlined_subroutine)
7965 {
7966 switch (pdi->tag)
7967 {
7968 case DW_TAG_subprogram:
7969 case DW_TAG_inlined_subroutine:
7970 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7971 break;
7972 case DW_TAG_constant:
7973 case DW_TAG_variable:
7974 case DW_TAG_typedef:
7975 case DW_TAG_union_type:
7976 if (!pdi->is_declaration)
7977 {
7978 add_partial_symbol (pdi, cu);
7979 }
7980 break;
7981 case DW_TAG_class_type:
7982 case DW_TAG_interface_type:
7983 case DW_TAG_structure_type:
7984 if (!pdi->is_declaration)
7985 {
7986 add_partial_symbol (pdi, cu);
7987 }
7988 if ((cu->language == language_rust
7989 || cu->language == language_cplus) && pdi->has_children)
7990 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7991 set_addrmap, cu);
7992 break;
7993 case DW_TAG_enumeration_type:
7994 if (!pdi->is_declaration)
7995 add_partial_enumeration (pdi, cu);
7996 break;
7997 case DW_TAG_base_type:
7998 case DW_TAG_subrange_type:
7999 /* File scope base type definitions are added to the partial
8000 symbol table. */
8001 add_partial_symbol (pdi, cu);
8002 break;
8003 case DW_TAG_namespace:
8004 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8005 break;
8006 case DW_TAG_module:
8007 if (!pdi->is_declaration)
8008 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8009 break;
8010 case DW_TAG_imported_unit:
8011 {
8012 struct dwarf2_per_cu_data *per_cu;
8013
8014 /* For now we don't handle imported units in type units. */
8015 if (cu->per_cu->is_debug_types)
8016 {
8017 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8018 " supported in type units [in module %s]"),
8019 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8020 }
8021
8022 per_cu = dwarf2_find_containing_comp_unit
8023 (pdi->d.sect_off, pdi->is_dwz,
8024 cu->per_cu->dwarf2_per_objfile);
8025
8026 /* Go read the partial unit, if needed. */
8027 if (per_cu->v.psymtab == NULL)
8028 process_psymtab_comp_unit (per_cu, true, cu->language);
8029
8030 cu->per_cu->imported_symtabs_push (per_cu);
8031 }
8032 break;
8033 case DW_TAG_imported_declaration:
8034 add_partial_symbol (pdi, cu);
8035 break;
8036 default:
8037 break;
8038 }
8039 }
8040
8041 /* If the die has a sibling, skip to the sibling. */
8042
8043 pdi = pdi->die_sibling;
8044 }
8045 }
8046
8047 /* Functions used to compute the fully scoped name of a partial DIE.
8048
8049 Normally, this is simple. For C++, the parent DIE's fully scoped
8050 name is concatenated with "::" and the partial DIE's name.
8051 Enumerators are an exception; they use the scope of their parent
8052 enumeration type, i.e. the name of the enumeration type is not
8053 prepended to the enumerator.
8054
8055 There are two complexities. One is DW_AT_specification; in this
8056 case "parent" means the parent of the target of the specification,
8057 instead of the direct parent of the DIE. The other is compilers
8058 which do not emit DW_TAG_namespace; in this case we try to guess
8059 the fully qualified name of structure types from their members'
8060 linkage names. This must be done using the DIE's children rather
8061 than the children of any DW_AT_specification target. We only need
8062 to do this for structures at the top level, i.e. if the target of
8063 any DW_AT_specification (if any; otherwise the DIE itself) does not
8064 have a parent. */
8065
8066 /* Compute the scope prefix associated with PDI's parent, in
8067 compilation unit CU. The result will be allocated on CU's
8068 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8069 field. NULL is returned if no prefix is necessary. */
8070 static const char *
8071 partial_die_parent_scope (struct partial_die_info *pdi,
8072 struct dwarf2_cu *cu)
8073 {
8074 const char *grandparent_scope;
8075 struct partial_die_info *parent, *real_pdi;
8076
8077 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8078 then this means the parent of the specification DIE. */
8079
8080 real_pdi = pdi;
8081 while (real_pdi->has_specification)
8082 {
8083 auto res = find_partial_die (real_pdi->spec_offset,
8084 real_pdi->spec_is_dwz, cu);
8085 real_pdi = res.pdi;
8086 cu = res.cu;
8087 }
8088
8089 parent = real_pdi->die_parent;
8090 if (parent == NULL)
8091 return NULL;
8092
8093 if (parent->scope_set)
8094 return parent->scope;
8095
8096 parent->fixup (cu);
8097
8098 grandparent_scope = partial_die_parent_scope (parent, cu);
8099
8100 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8101 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8102 Work around this problem here. */
8103 if (cu->language == language_cplus
8104 && parent->tag == DW_TAG_namespace
8105 && strcmp (parent->name, "::") == 0
8106 && grandparent_scope == NULL)
8107 {
8108 parent->scope = NULL;
8109 parent->scope_set = 1;
8110 return NULL;
8111 }
8112
8113 /* Nested subroutines in Fortran get a prefix. */
8114 if (pdi->tag == DW_TAG_enumerator)
8115 /* Enumerators should not get the name of the enumeration as a prefix. */
8116 parent->scope = grandparent_scope;
8117 else if (parent->tag == DW_TAG_namespace
8118 || parent->tag == DW_TAG_module
8119 || parent->tag == DW_TAG_structure_type
8120 || parent->tag == DW_TAG_class_type
8121 || parent->tag == DW_TAG_interface_type
8122 || parent->tag == DW_TAG_union_type
8123 || parent->tag == DW_TAG_enumeration_type
8124 || (cu->language == language_fortran
8125 && parent->tag == DW_TAG_subprogram
8126 && pdi->tag == DW_TAG_subprogram))
8127 {
8128 if (grandparent_scope == NULL)
8129 parent->scope = parent->name;
8130 else
8131 parent->scope = typename_concat (&cu->comp_unit_obstack,
8132 grandparent_scope,
8133 parent->name, 0, cu);
8134 }
8135 else
8136 {
8137 /* FIXME drow/2004-04-01: What should we be doing with
8138 function-local names? For partial symbols, we should probably be
8139 ignoring them. */
8140 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8141 dwarf_tag_name (parent->tag),
8142 sect_offset_str (pdi->sect_off));
8143 parent->scope = grandparent_scope;
8144 }
8145
8146 parent->scope_set = 1;
8147 return parent->scope;
8148 }
8149
8150 /* Return the fully scoped name associated with PDI, from compilation unit
8151 CU. The result will be allocated with malloc. */
8152
8153 static gdb::unique_xmalloc_ptr<char>
8154 partial_die_full_name (struct partial_die_info *pdi,
8155 struct dwarf2_cu *cu)
8156 {
8157 const char *parent_scope;
8158
8159 /* If this is a template instantiation, we can not work out the
8160 template arguments from partial DIEs. So, unfortunately, we have
8161 to go through the full DIEs. At least any work we do building
8162 types here will be reused if full symbols are loaded later. */
8163 if (pdi->has_template_arguments)
8164 {
8165 pdi->fixup (cu);
8166
8167 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8168 {
8169 struct die_info *die;
8170 struct attribute attr;
8171 struct dwarf2_cu *ref_cu = cu;
8172
8173 /* DW_FORM_ref_addr is using section offset. */
8174 attr.name = (enum dwarf_attribute) 0;
8175 attr.form = DW_FORM_ref_addr;
8176 attr.u.unsnd = to_underlying (pdi->sect_off);
8177 die = follow_die_ref (NULL, &attr, &ref_cu);
8178
8179 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8180 }
8181 }
8182
8183 parent_scope = partial_die_parent_scope (pdi, cu);
8184 if (parent_scope == NULL)
8185 return NULL;
8186 else
8187 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8188 pdi->name, 0, cu));
8189 }
8190
8191 static void
8192 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8193 {
8194 struct dwarf2_per_objfile *dwarf2_per_objfile
8195 = cu->per_cu->dwarf2_per_objfile;
8196 struct objfile *objfile = dwarf2_per_objfile->objfile;
8197 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8198 CORE_ADDR addr = 0;
8199 const char *actual_name = NULL;
8200 CORE_ADDR baseaddr;
8201
8202 baseaddr = objfile->text_section_offset ();
8203
8204 gdb::unique_xmalloc_ptr<char> built_actual_name
8205 = partial_die_full_name (pdi, cu);
8206 if (built_actual_name != NULL)
8207 actual_name = built_actual_name.get ();
8208
8209 if (actual_name == NULL)
8210 actual_name = pdi->name;
8211
8212 switch (pdi->tag)
8213 {
8214 case DW_TAG_inlined_subroutine:
8215 case DW_TAG_subprogram:
8216 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8217 - baseaddr);
8218 if (pdi->is_external
8219 || cu->language == language_ada
8220 || (cu->language == language_fortran
8221 && pdi->die_parent != NULL
8222 && pdi->die_parent->tag == DW_TAG_subprogram))
8223 {
8224 /* Normally, only "external" DIEs are part of the global scope.
8225 But in Ada and Fortran, we want to be able to access nested
8226 procedures globally. So all Ada and Fortran subprograms are
8227 stored in the global scope. */
8228 add_psymbol_to_list (actual_name,
8229 built_actual_name != NULL,
8230 VAR_DOMAIN, LOC_BLOCK,
8231 SECT_OFF_TEXT (objfile),
8232 psymbol_placement::GLOBAL,
8233 addr,
8234 cu->language, objfile);
8235 }
8236 else
8237 {
8238 add_psymbol_to_list (actual_name,
8239 built_actual_name != NULL,
8240 VAR_DOMAIN, LOC_BLOCK,
8241 SECT_OFF_TEXT (objfile),
8242 psymbol_placement::STATIC,
8243 addr, cu->language, objfile);
8244 }
8245
8246 if (pdi->main_subprogram && actual_name != NULL)
8247 set_objfile_main_name (objfile, actual_name, cu->language);
8248 break;
8249 case DW_TAG_constant:
8250 add_psymbol_to_list (actual_name,
8251 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8252 -1, (pdi->is_external
8253 ? psymbol_placement::GLOBAL
8254 : psymbol_placement::STATIC),
8255 0, cu->language, objfile);
8256 break;
8257 case DW_TAG_variable:
8258 if (pdi->d.locdesc)
8259 addr = decode_locdesc (pdi->d.locdesc, cu);
8260
8261 if (pdi->d.locdesc
8262 && addr == 0
8263 && !dwarf2_per_objfile->has_section_at_zero)
8264 {
8265 /* A global or static variable may also have been stripped
8266 out by the linker if unused, in which case its address
8267 will be nullified; do not add such variables into partial
8268 symbol table then. */
8269 }
8270 else if (pdi->is_external)
8271 {
8272 /* Global Variable.
8273 Don't enter into the minimal symbol tables as there is
8274 a minimal symbol table entry from the ELF symbols already.
8275 Enter into partial symbol table if it has a location
8276 descriptor or a type.
8277 If the location descriptor is missing, new_symbol will create
8278 a LOC_UNRESOLVED symbol, the address of the variable will then
8279 be determined from the minimal symbol table whenever the variable
8280 is referenced.
8281 The address for the partial symbol table entry is not
8282 used by GDB, but it comes in handy for debugging partial symbol
8283 table building. */
8284
8285 if (pdi->d.locdesc || pdi->has_type)
8286 add_psymbol_to_list (actual_name,
8287 built_actual_name != NULL,
8288 VAR_DOMAIN, LOC_STATIC,
8289 SECT_OFF_TEXT (objfile),
8290 psymbol_placement::GLOBAL,
8291 addr, cu->language, objfile);
8292 }
8293 else
8294 {
8295 int has_loc = pdi->d.locdesc != NULL;
8296
8297 /* Static Variable. Skip symbols whose value we cannot know (those
8298 without location descriptors or constant values). */
8299 if (!has_loc && !pdi->has_const_value)
8300 return;
8301
8302 add_psymbol_to_list (actual_name,
8303 built_actual_name != NULL,
8304 VAR_DOMAIN, LOC_STATIC,
8305 SECT_OFF_TEXT (objfile),
8306 psymbol_placement::STATIC,
8307 has_loc ? addr : 0,
8308 cu->language, objfile);
8309 }
8310 break;
8311 case DW_TAG_typedef:
8312 case DW_TAG_base_type:
8313 case DW_TAG_subrange_type:
8314 add_psymbol_to_list (actual_name,
8315 built_actual_name != NULL,
8316 VAR_DOMAIN, LOC_TYPEDEF, -1,
8317 psymbol_placement::STATIC,
8318 0, cu->language, objfile);
8319 break;
8320 case DW_TAG_imported_declaration:
8321 case DW_TAG_namespace:
8322 add_psymbol_to_list (actual_name,
8323 built_actual_name != NULL,
8324 VAR_DOMAIN, LOC_TYPEDEF, -1,
8325 psymbol_placement::GLOBAL,
8326 0, cu->language, objfile);
8327 break;
8328 case DW_TAG_module:
8329 /* With Fortran 77 there might be a "BLOCK DATA" module
8330 available without any name. If so, we skip the module as it
8331 doesn't bring any value. */
8332 if (actual_name != nullptr)
8333 add_psymbol_to_list (actual_name,
8334 built_actual_name != NULL,
8335 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8336 psymbol_placement::GLOBAL,
8337 0, cu->language, objfile);
8338 break;
8339 case DW_TAG_class_type:
8340 case DW_TAG_interface_type:
8341 case DW_TAG_structure_type:
8342 case DW_TAG_union_type:
8343 case DW_TAG_enumeration_type:
8344 /* Skip external references. The DWARF standard says in the section
8345 about "Structure, Union, and Class Type Entries": "An incomplete
8346 structure, union or class type is represented by a structure,
8347 union or class entry that does not have a byte size attribute
8348 and that has a DW_AT_declaration attribute." */
8349 if (!pdi->has_byte_size && pdi->is_declaration)
8350 return;
8351
8352 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8353 static vs. global. */
8354 add_psymbol_to_list (actual_name,
8355 built_actual_name != NULL,
8356 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8357 cu->language == language_cplus
8358 ? psymbol_placement::GLOBAL
8359 : psymbol_placement::STATIC,
8360 0, cu->language, objfile);
8361
8362 break;
8363 case DW_TAG_enumerator:
8364 add_psymbol_to_list (actual_name,
8365 built_actual_name != NULL,
8366 VAR_DOMAIN, LOC_CONST, -1,
8367 cu->language == language_cplus
8368 ? psymbol_placement::GLOBAL
8369 : psymbol_placement::STATIC,
8370 0, cu->language, objfile);
8371 break;
8372 default:
8373 break;
8374 }
8375 }
8376
8377 /* Read a partial die corresponding to a namespace; also, add a symbol
8378 corresponding to that namespace to the symbol table. NAMESPACE is
8379 the name of the enclosing namespace. */
8380
8381 static void
8382 add_partial_namespace (struct partial_die_info *pdi,
8383 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8384 int set_addrmap, struct dwarf2_cu *cu)
8385 {
8386 /* Add a symbol for the namespace. */
8387
8388 add_partial_symbol (pdi, cu);
8389
8390 /* Now scan partial symbols in that namespace. */
8391
8392 if (pdi->has_children)
8393 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8394 }
8395
8396 /* Read a partial die corresponding to a Fortran module. */
8397
8398 static void
8399 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8400 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8401 {
8402 /* Add a symbol for the namespace. */
8403
8404 add_partial_symbol (pdi, cu);
8405
8406 /* Now scan partial symbols in that module. */
8407
8408 if (pdi->has_children)
8409 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8410 }
8411
8412 /* Read a partial die corresponding to a subprogram or an inlined
8413 subprogram and create a partial symbol for that subprogram.
8414 When the CU language allows it, this routine also defines a partial
8415 symbol for each nested subprogram that this subprogram contains.
8416 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8417 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8418
8419 PDI may also be a lexical block, in which case we simply search
8420 recursively for subprograms defined inside that lexical block.
8421 Again, this is only performed when the CU language allows this
8422 type of definitions. */
8423
8424 static void
8425 add_partial_subprogram (struct partial_die_info *pdi,
8426 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8427 int set_addrmap, struct dwarf2_cu *cu)
8428 {
8429 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8430 {
8431 if (pdi->has_pc_info)
8432 {
8433 if (pdi->lowpc < *lowpc)
8434 *lowpc = pdi->lowpc;
8435 if (pdi->highpc > *highpc)
8436 *highpc = pdi->highpc;
8437 if (set_addrmap)
8438 {
8439 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8440 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8441 CORE_ADDR baseaddr;
8442 CORE_ADDR this_highpc;
8443 CORE_ADDR this_lowpc;
8444
8445 baseaddr = objfile->text_section_offset ();
8446 this_lowpc
8447 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8448 pdi->lowpc + baseaddr)
8449 - baseaddr);
8450 this_highpc
8451 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8452 pdi->highpc + baseaddr)
8453 - baseaddr);
8454 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8455 this_lowpc, this_highpc - 1,
8456 cu->per_cu->v.psymtab);
8457 }
8458 }
8459
8460 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8461 {
8462 if (!pdi->is_declaration)
8463 /* Ignore subprogram DIEs that do not have a name, they are
8464 illegal. Do not emit a complaint at this point, we will
8465 do so when we convert this psymtab into a symtab. */
8466 if (pdi->name)
8467 add_partial_symbol (pdi, cu);
8468 }
8469 }
8470
8471 if (! pdi->has_children)
8472 return;
8473
8474 if (cu->language == language_ada || cu->language == language_fortran)
8475 {
8476 pdi = pdi->die_child;
8477 while (pdi != NULL)
8478 {
8479 pdi->fixup (cu);
8480 if (pdi->tag == DW_TAG_subprogram
8481 || pdi->tag == DW_TAG_inlined_subroutine
8482 || pdi->tag == DW_TAG_lexical_block)
8483 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8484 pdi = pdi->die_sibling;
8485 }
8486 }
8487 }
8488
8489 /* Read a partial die corresponding to an enumeration type. */
8490
8491 static void
8492 add_partial_enumeration (struct partial_die_info *enum_pdi,
8493 struct dwarf2_cu *cu)
8494 {
8495 struct partial_die_info *pdi;
8496
8497 if (enum_pdi->name != NULL)
8498 add_partial_symbol (enum_pdi, cu);
8499
8500 pdi = enum_pdi->die_child;
8501 while (pdi)
8502 {
8503 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8504 complaint (_("malformed enumerator DIE ignored"));
8505 else
8506 add_partial_symbol (pdi, cu);
8507 pdi = pdi->die_sibling;
8508 }
8509 }
8510
8511 /* Return the initial uleb128 in the die at INFO_PTR. */
8512
8513 static unsigned int
8514 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8515 {
8516 unsigned int bytes_read;
8517
8518 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8519 }
8520
8521 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8522 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8523
8524 Return the corresponding abbrev, or NULL if the number is zero (indicating
8525 an empty DIE). In either case *BYTES_READ will be set to the length of
8526 the initial number. */
8527
8528 static struct abbrev_info *
8529 peek_die_abbrev (const die_reader_specs &reader,
8530 const gdb_byte *info_ptr, unsigned int *bytes_read)
8531 {
8532 dwarf2_cu *cu = reader.cu;
8533 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8534 unsigned int abbrev_number
8535 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8536
8537 if (abbrev_number == 0)
8538 return NULL;
8539
8540 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8541 if (!abbrev)
8542 {
8543 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8544 " at offset %s [in module %s]"),
8545 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8546 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8547 }
8548
8549 return abbrev;
8550 }
8551
8552 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8553 Returns a pointer to the end of a series of DIEs, terminated by an empty
8554 DIE. Any children of the skipped DIEs will also be skipped. */
8555
8556 static const gdb_byte *
8557 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8558 {
8559 while (1)
8560 {
8561 unsigned int bytes_read;
8562 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8563
8564 if (abbrev == NULL)
8565 return info_ptr + bytes_read;
8566 else
8567 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8568 }
8569 }
8570
8571 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8572 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8573 abbrev corresponding to that skipped uleb128 should be passed in
8574 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8575 children. */
8576
8577 static const gdb_byte *
8578 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8579 struct abbrev_info *abbrev)
8580 {
8581 unsigned int bytes_read;
8582 struct attribute attr;
8583 bfd *abfd = reader->abfd;
8584 struct dwarf2_cu *cu = reader->cu;
8585 const gdb_byte *buffer = reader->buffer;
8586 const gdb_byte *buffer_end = reader->buffer_end;
8587 unsigned int form, i;
8588
8589 for (i = 0; i < abbrev->num_attrs; i++)
8590 {
8591 /* The only abbrev we care about is DW_AT_sibling. */
8592 if (abbrev->attrs[i].name == DW_AT_sibling)
8593 {
8594 bool ignored;
8595 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8596 &ignored);
8597 if (attr.form == DW_FORM_ref_addr)
8598 complaint (_("ignoring absolute DW_AT_sibling"));
8599 else
8600 {
8601 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8602 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8603
8604 if (sibling_ptr < info_ptr)
8605 complaint (_("DW_AT_sibling points backwards"));
8606 else if (sibling_ptr > reader->buffer_end)
8607 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8608 else
8609 return sibling_ptr;
8610 }
8611 }
8612
8613 /* If it isn't DW_AT_sibling, skip this attribute. */
8614 form = abbrev->attrs[i].form;
8615 skip_attribute:
8616 switch (form)
8617 {
8618 case DW_FORM_ref_addr:
8619 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8620 and later it is offset sized. */
8621 if (cu->header.version == 2)
8622 info_ptr += cu->header.addr_size;
8623 else
8624 info_ptr += cu->header.offset_size;
8625 break;
8626 case DW_FORM_GNU_ref_alt:
8627 info_ptr += cu->header.offset_size;
8628 break;
8629 case DW_FORM_addr:
8630 info_ptr += cu->header.addr_size;
8631 break;
8632 case DW_FORM_data1:
8633 case DW_FORM_ref1:
8634 case DW_FORM_flag:
8635 case DW_FORM_strx1:
8636 info_ptr += 1;
8637 break;
8638 case DW_FORM_flag_present:
8639 case DW_FORM_implicit_const:
8640 break;
8641 case DW_FORM_data2:
8642 case DW_FORM_ref2:
8643 case DW_FORM_strx2:
8644 info_ptr += 2;
8645 break;
8646 case DW_FORM_strx3:
8647 info_ptr += 3;
8648 break;
8649 case DW_FORM_data4:
8650 case DW_FORM_ref4:
8651 case DW_FORM_strx4:
8652 info_ptr += 4;
8653 break;
8654 case DW_FORM_data8:
8655 case DW_FORM_ref8:
8656 case DW_FORM_ref_sig8:
8657 info_ptr += 8;
8658 break;
8659 case DW_FORM_data16:
8660 info_ptr += 16;
8661 break;
8662 case DW_FORM_string:
8663 read_direct_string (abfd, info_ptr, &bytes_read);
8664 info_ptr += bytes_read;
8665 break;
8666 case DW_FORM_sec_offset:
8667 case DW_FORM_strp:
8668 case DW_FORM_GNU_strp_alt:
8669 info_ptr += cu->header.offset_size;
8670 break;
8671 case DW_FORM_exprloc:
8672 case DW_FORM_block:
8673 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8674 info_ptr += bytes_read;
8675 break;
8676 case DW_FORM_block1:
8677 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8678 break;
8679 case DW_FORM_block2:
8680 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8681 break;
8682 case DW_FORM_block4:
8683 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8684 break;
8685 case DW_FORM_addrx:
8686 case DW_FORM_strx:
8687 case DW_FORM_sdata:
8688 case DW_FORM_udata:
8689 case DW_FORM_ref_udata:
8690 case DW_FORM_GNU_addr_index:
8691 case DW_FORM_GNU_str_index:
8692 case DW_FORM_rnglistx:
8693 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8694 break;
8695 case DW_FORM_indirect:
8696 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8697 info_ptr += bytes_read;
8698 /* We need to continue parsing from here, so just go back to
8699 the top. */
8700 goto skip_attribute;
8701
8702 default:
8703 error (_("Dwarf Error: Cannot handle %s "
8704 "in DWARF reader [in module %s]"),
8705 dwarf_form_name (form),
8706 bfd_get_filename (abfd));
8707 }
8708 }
8709
8710 if (abbrev->has_children)
8711 return skip_children (reader, info_ptr);
8712 else
8713 return info_ptr;
8714 }
8715
8716 /* Locate ORIG_PDI's sibling.
8717 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8718
8719 static const gdb_byte *
8720 locate_pdi_sibling (const struct die_reader_specs *reader,
8721 struct partial_die_info *orig_pdi,
8722 const gdb_byte *info_ptr)
8723 {
8724 /* Do we know the sibling already? */
8725
8726 if (orig_pdi->sibling)
8727 return orig_pdi->sibling;
8728
8729 /* Are there any children to deal with? */
8730
8731 if (!orig_pdi->has_children)
8732 return info_ptr;
8733
8734 /* Skip the children the long way. */
8735
8736 return skip_children (reader, info_ptr);
8737 }
8738
8739 /* Expand this partial symbol table into a full symbol table. SELF is
8740 not NULL. */
8741
8742 void
8743 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8744 {
8745 struct dwarf2_per_objfile *dwarf2_per_objfile
8746 = get_dwarf2_per_objfile (objfile);
8747
8748 gdb_assert (!readin);
8749 /* If this psymtab is constructed from a debug-only objfile, the
8750 has_section_at_zero flag will not necessarily be correct. We
8751 can get the correct value for this flag by looking at the data
8752 associated with the (presumably stripped) associated objfile. */
8753 if (objfile->separate_debug_objfile_backlink)
8754 {
8755 struct dwarf2_per_objfile *dpo_backlink
8756 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8757
8758 dwarf2_per_objfile->has_section_at_zero
8759 = dpo_backlink->has_section_at_zero;
8760 }
8761
8762 expand_psymtab (objfile);
8763
8764 process_cu_includes (dwarf2_per_objfile);
8765 }
8766 \f
8767 /* Reading in full CUs. */
8768
8769 /* Add PER_CU to the queue. */
8770
8771 static void
8772 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8773 enum language pretend_language)
8774 {
8775 per_cu->queued = 1;
8776 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8777 }
8778
8779 /* If PER_CU is not yet queued, add it to the queue.
8780 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8781 dependency.
8782 The result is non-zero if PER_CU was queued, otherwise the result is zero
8783 meaning either PER_CU is already queued or it is already loaded.
8784
8785 N.B. There is an invariant here that if a CU is queued then it is loaded.
8786 The caller is required to load PER_CU if we return non-zero. */
8787
8788 static int
8789 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8790 struct dwarf2_per_cu_data *per_cu,
8791 enum language pretend_language)
8792 {
8793 /* We may arrive here during partial symbol reading, if we need full
8794 DIEs to process an unusual case (e.g. template arguments). Do
8795 not queue PER_CU, just tell our caller to load its DIEs. */
8796 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8797 {
8798 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8799 return 1;
8800 return 0;
8801 }
8802
8803 /* Mark the dependence relation so that we don't flush PER_CU
8804 too early. */
8805 if (dependent_cu != NULL)
8806 dwarf2_add_dependence (dependent_cu, per_cu);
8807
8808 /* If it's already on the queue, we have nothing to do. */
8809 if (per_cu->queued)
8810 return 0;
8811
8812 /* If the compilation unit is already loaded, just mark it as
8813 used. */
8814 if (per_cu->cu != NULL)
8815 {
8816 per_cu->cu->last_used = 0;
8817 return 0;
8818 }
8819
8820 /* Add it to the queue. */
8821 queue_comp_unit (per_cu, pretend_language);
8822
8823 return 1;
8824 }
8825
8826 /* Process the queue. */
8827
8828 static void
8829 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8830 {
8831 if (dwarf_read_debug)
8832 {
8833 fprintf_unfiltered (gdb_stdlog,
8834 "Expanding one or more symtabs of objfile %s ...\n",
8835 objfile_name (dwarf2_per_objfile->objfile));
8836 }
8837
8838 /* The queue starts out with one item, but following a DIE reference
8839 may load a new CU, adding it to the end of the queue. */
8840 while (!dwarf2_per_objfile->queue.empty ())
8841 {
8842 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8843
8844 if ((dwarf2_per_objfile->using_index
8845 ? !item.per_cu->v.quick->compunit_symtab
8846 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8847 /* Skip dummy CUs. */
8848 && item.per_cu->cu != NULL)
8849 {
8850 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8851 unsigned int debug_print_threshold;
8852 char buf[100];
8853
8854 if (per_cu->is_debug_types)
8855 {
8856 struct signatured_type *sig_type =
8857 (struct signatured_type *) per_cu;
8858
8859 sprintf (buf, "TU %s at offset %s",
8860 hex_string (sig_type->signature),
8861 sect_offset_str (per_cu->sect_off));
8862 /* There can be 100s of TUs.
8863 Only print them in verbose mode. */
8864 debug_print_threshold = 2;
8865 }
8866 else
8867 {
8868 sprintf (buf, "CU at offset %s",
8869 sect_offset_str (per_cu->sect_off));
8870 debug_print_threshold = 1;
8871 }
8872
8873 if (dwarf_read_debug >= debug_print_threshold)
8874 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8875
8876 if (per_cu->is_debug_types)
8877 process_full_type_unit (per_cu, item.pretend_language);
8878 else
8879 process_full_comp_unit (per_cu, item.pretend_language);
8880
8881 if (dwarf_read_debug >= debug_print_threshold)
8882 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8883 }
8884
8885 item.per_cu->queued = 0;
8886 dwarf2_per_objfile->queue.pop ();
8887 }
8888
8889 if (dwarf_read_debug)
8890 {
8891 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8892 objfile_name (dwarf2_per_objfile->objfile));
8893 }
8894 }
8895
8896 /* Read in full symbols for PST, and anything it depends on. */
8897
8898 void
8899 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8900 {
8901 if (readin)
8902 return;
8903
8904 read_dependencies (objfile);
8905
8906 dw2_do_instantiate_symtab (per_cu_data, false);
8907 gdb_assert (get_compunit_symtab () != nullptr);
8908 }
8909
8910 /* Trivial hash function for die_info: the hash value of a DIE
8911 is its offset in .debug_info for this objfile. */
8912
8913 static hashval_t
8914 die_hash (const void *item)
8915 {
8916 const struct die_info *die = (const struct die_info *) item;
8917
8918 return to_underlying (die->sect_off);
8919 }
8920
8921 /* Trivial comparison function for die_info structures: two DIEs
8922 are equal if they have the same offset. */
8923
8924 static int
8925 die_eq (const void *item_lhs, const void *item_rhs)
8926 {
8927 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8928 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8929
8930 return die_lhs->sect_off == die_rhs->sect_off;
8931 }
8932
8933 /* Load the DIEs associated with PER_CU into memory. */
8934
8935 static void
8936 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8937 bool skip_partial,
8938 enum language pretend_language)
8939 {
8940 gdb_assert (! this_cu->is_debug_types);
8941
8942 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8943 if (reader.dummy_p)
8944 return;
8945
8946 struct dwarf2_cu *cu = reader.cu;
8947 const gdb_byte *info_ptr = reader.info_ptr;
8948
8949 gdb_assert (cu->die_hash == NULL);
8950 cu->die_hash =
8951 htab_create_alloc_ex (cu->header.length / 12,
8952 die_hash,
8953 die_eq,
8954 NULL,
8955 &cu->comp_unit_obstack,
8956 hashtab_obstack_allocate,
8957 dummy_obstack_deallocate);
8958
8959 if (reader.comp_unit_die->has_children)
8960 reader.comp_unit_die->child
8961 = read_die_and_siblings (&reader, reader.info_ptr,
8962 &info_ptr, reader.comp_unit_die);
8963 cu->dies = reader.comp_unit_die;
8964 /* comp_unit_die is not stored in die_hash, no need. */
8965
8966 /* We try not to read any attributes in this function, because not
8967 all CUs needed for references have been loaded yet, and symbol
8968 table processing isn't initialized. But we have to set the CU language,
8969 or we won't be able to build types correctly.
8970 Similarly, if we do not read the producer, we can not apply
8971 producer-specific interpretation. */
8972 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8973
8974 reader.keep ();
8975 }
8976
8977 /* Add a DIE to the delayed physname list. */
8978
8979 static void
8980 add_to_method_list (struct type *type, int fnfield_index, int index,
8981 const char *name, struct die_info *die,
8982 struct dwarf2_cu *cu)
8983 {
8984 struct delayed_method_info mi;
8985 mi.type = type;
8986 mi.fnfield_index = fnfield_index;
8987 mi.index = index;
8988 mi.name = name;
8989 mi.die = die;
8990 cu->method_list.push_back (mi);
8991 }
8992
8993 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8994 "const" / "volatile". If so, decrements LEN by the length of the
8995 modifier and return true. Otherwise return false. */
8996
8997 template<size_t N>
8998 static bool
8999 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9000 {
9001 size_t mod_len = sizeof (mod) - 1;
9002 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9003 {
9004 len -= mod_len;
9005 return true;
9006 }
9007 return false;
9008 }
9009
9010 /* Compute the physnames of any methods on the CU's method list.
9011
9012 The computation of method physnames is delayed in order to avoid the
9013 (bad) condition that one of the method's formal parameters is of an as yet
9014 incomplete type. */
9015
9016 static void
9017 compute_delayed_physnames (struct dwarf2_cu *cu)
9018 {
9019 /* Only C++ delays computing physnames. */
9020 if (cu->method_list.empty ())
9021 return;
9022 gdb_assert (cu->language == language_cplus);
9023
9024 for (const delayed_method_info &mi : cu->method_list)
9025 {
9026 const char *physname;
9027 struct fn_fieldlist *fn_flp
9028 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9029 physname = dwarf2_physname (mi.name, mi.die, cu);
9030 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9031 = physname ? physname : "";
9032
9033 /* Since there's no tag to indicate whether a method is a
9034 const/volatile overload, extract that information out of the
9035 demangled name. */
9036 if (physname != NULL)
9037 {
9038 size_t len = strlen (physname);
9039
9040 while (1)
9041 {
9042 if (physname[len] == ')') /* shortcut */
9043 break;
9044 else if (check_modifier (physname, len, " const"))
9045 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9046 else if (check_modifier (physname, len, " volatile"))
9047 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9048 else
9049 break;
9050 }
9051 }
9052 }
9053
9054 /* The list is no longer needed. */
9055 cu->method_list.clear ();
9056 }
9057
9058 /* Go objects should be embedded in a DW_TAG_module DIE,
9059 and it's not clear if/how imported objects will appear.
9060 To keep Go support simple until that's worked out,
9061 go back through what we've read and create something usable.
9062 We could do this while processing each DIE, and feels kinda cleaner,
9063 but that way is more invasive.
9064 This is to, for example, allow the user to type "p var" or "b main"
9065 without having to specify the package name, and allow lookups
9066 of module.object to work in contexts that use the expression
9067 parser. */
9068
9069 static void
9070 fixup_go_packaging (struct dwarf2_cu *cu)
9071 {
9072 gdb::unique_xmalloc_ptr<char> package_name;
9073 struct pending *list;
9074 int i;
9075
9076 for (list = *cu->get_builder ()->get_global_symbols ();
9077 list != NULL;
9078 list = list->next)
9079 {
9080 for (i = 0; i < list->nsyms; ++i)
9081 {
9082 struct symbol *sym = list->symbol[i];
9083
9084 if (sym->language () == language_go
9085 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9086 {
9087 gdb::unique_xmalloc_ptr<char> this_package_name
9088 (go_symbol_package_name (sym));
9089
9090 if (this_package_name == NULL)
9091 continue;
9092 if (package_name == NULL)
9093 package_name = std::move (this_package_name);
9094 else
9095 {
9096 struct objfile *objfile
9097 = cu->per_cu->dwarf2_per_objfile->objfile;
9098 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9099 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9100 (symbol_symtab (sym) != NULL
9101 ? symtab_to_filename_for_display
9102 (symbol_symtab (sym))
9103 : objfile_name (objfile)),
9104 this_package_name.get (), package_name.get ());
9105 }
9106 }
9107 }
9108 }
9109
9110 if (package_name != NULL)
9111 {
9112 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9113 const char *saved_package_name = objfile->intern (package_name.get ());
9114 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9115 saved_package_name);
9116 struct symbol *sym;
9117
9118 sym = allocate_symbol (objfile);
9119 sym->set_language (language_go, &objfile->objfile_obstack);
9120 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9121 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9122 e.g., "main" finds the "main" module and not C's main(). */
9123 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9124 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9125 SYMBOL_TYPE (sym) = type;
9126
9127 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9128 }
9129 }
9130
9131 /* Allocate a fully-qualified name consisting of the two parts on the
9132 obstack. */
9133
9134 static const char *
9135 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9136 {
9137 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9138 }
9139
9140 /* A helper that allocates a struct discriminant_info to attach to a
9141 union type. */
9142
9143 static struct discriminant_info *
9144 alloc_discriminant_info (struct type *type, int discriminant_index,
9145 int default_index)
9146 {
9147 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9148 gdb_assert (discriminant_index == -1
9149 || (discriminant_index >= 0
9150 && discriminant_index < TYPE_NFIELDS (type)));
9151 gdb_assert (default_index == -1
9152 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9153
9154 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9155
9156 struct discriminant_info *disc
9157 = ((struct discriminant_info *)
9158 TYPE_ZALLOC (type,
9159 offsetof (struct discriminant_info, discriminants)
9160 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9161 disc->default_index = default_index;
9162 disc->discriminant_index = discriminant_index;
9163
9164 struct dynamic_prop prop;
9165 prop.kind = PROP_UNDEFINED;
9166 prop.data.baton = disc;
9167
9168 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9169
9170 return disc;
9171 }
9172
9173 /* Some versions of rustc emitted enums in an unusual way.
9174
9175 Ordinary enums were emitted as unions. The first element of each
9176 structure in the union was named "RUST$ENUM$DISR". This element
9177 held the discriminant.
9178
9179 These versions of Rust also implemented the "non-zero"
9180 optimization. When the enum had two values, and one is empty and
9181 the other holds a pointer that cannot be zero, the pointer is used
9182 as the discriminant, with a zero value meaning the empty variant.
9183 Here, the union's first member is of the form
9184 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9185 where the fieldnos are the indices of the fields that should be
9186 traversed in order to find the field (which may be several fields deep)
9187 and the variantname is the name of the variant of the case when the
9188 field is zero.
9189
9190 This function recognizes whether TYPE is of one of these forms,
9191 and, if so, smashes it to be a variant type. */
9192
9193 static void
9194 quirk_rust_enum (struct type *type, struct objfile *objfile)
9195 {
9196 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9197
9198 /* We don't need to deal with empty enums. */
9199 if (TYPE_NFIELDS (type) == 0)
9200 return;
9201
9202 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9203 if (TYPE_NFIELDS (type) == 1
9204 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9205 {
9206 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9207
9208 /* Decode the field name to find the offset of the
9209 discriminant. */
9210 ULONGEST bit_offset = 0;
9211 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9212 while (name[0] >= '0' && name[0] <= '9')
9213 {
9214 char *tail;
9215 unsigned long index = strtoul (name, &tail, 10);
9216 name = tail;
9217 if (*name != '$'
9218 || index >= TYPE_NFIELDS (field_type)
9219 || (TYPE_FIELD_LOC_KIND (field_type, index)
9220 != FIELD_LOC_KIND_BITPOS))
9221 {
9222 complaint (_("Could not parse Rust enum encoding string \"%s\""
9223 "[in module %s]"),
9224 TYPE_FIELD_NAME (type, 0),
9225 objfile_name (objfile));
9226 return;
9227 }
9228 ++name;
9229
9230 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9231 field_type = TYPE_FIELD_TYPE (field_type, index);
9232 }
9233
9234 /* Make a union to hold the variants. */
9235 struct type *union_type = alloc_type (objfile);
9236 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9237 TYPE_NFIELDS (union_type) = 3;
9238 TYPE_FIELDS (union_type)
9239 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9240 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9241 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9242
9243 /* Put the discriminant must at index 0. */
9244 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9245 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9246 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9247 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9248
9249 /* The order of fields doesn't really matter, so put the real
9250 field at index 1 and the data-less field at index 2. */
9251 struct discriminant_info *disc
9252 = alloc_discriminant_info (union_type, 0, 1);
9253 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9254 TYPE_FIELD_NAME (union_type, 1)
9255 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9256 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9257 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9258 TYPE_FIELD_NAME (union_type, 1));
9259
9260 const char *dataless_name
9261 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9262 name);
9263 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9264 dataless_name);
9265 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9266 /* NAME points into the original discriminant name, which
9267 already has the correct lifetime. */
9268 TYPE_FIELD_NAME (union_type, 2) = name;
9269 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9270 disc->discriminants[2] = 0;
9271
9272 /* Smash this type to be a structure type. We have to do this
9273 because the type has already been recorded. */
9274 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9275 TYPE_NFIELDS (type) = 1;
9276 TYPE_FIELDS (type)
9277 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9278
9279 /* Install the variant part. */
9280 TYPE_FIELD_TYPE (type, 0) = union_type;
9281 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9282 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9283 }
9284 /* A union with a single anonymous field is probably an old-style
9285 univariant enum. */
9286 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9287 {
9288 /* Smash this type to be a structure type. We have to do this
9289 because the type has already been recorded. */
9290 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9291
9292 /* Make a union to hold the variants. */
9293 struct type *union_type = alloc_type (objfile);
9294 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9295 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9296 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9297 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9298 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9299
9300 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9301 const char *variant_name
9302 = rust_last_path_segment (TYPE_NAME (field_type));
9303 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9304 TYPE_NAME (field_type)
9305 = rust_fully_qualify (&objfile->objfile_obstack,
9306 TYPE_NAME (type), variant_name);
9307
9308 /* Install the union in the outer struct type. */
9309 TYPE_NFIELDS (type) = 1;
9310 TYPE_FIELDS (type)
9311 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9312 TYPE_FIELD_TYPE (type, 0) = union_type;
9313 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9314 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9315
9316 alloc_discriminant_info (union_type, -1, 0);
9317 }
9318 else
9319 {
9320 struct type *disr_type = nullptr;
9321 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9322 {
9323 disr_type = TYPE_FIELD_TYPE (type, i);
9324
9325 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9326 {
9327 /* All fields of a true enum will be structs. */
9328 return;
9329 }
9330 else if (TYPE_NFIELDS (disr_type) == 0)
9331 {
9332 /* Could be data-less variant, so keep going. */
9333 disr_type = nullptr;
9334 }
9335 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9336 "RUST$ENUM$DISR") != 0)
9337 {
9338 /* Not a Rust enum. */
9339 return;
9340 }
9341 else
9342 {
9343 /* Found one. */
9344 break;
9345 }
9346 }
9347
9348 /* If we got here without a discriminant, then it's probably
9349 just a union. */
9350 if (disr_type == nullptr)
9351 return;
9352
9353 /* Smash this type to be a structure type. We have to do this
9354 because the type has already been recorded. */
9355 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9356
9357 /* Make a union to hold the variants. */
9358 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9359 struct type *union_type = alloc_type (objfile);
9360 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9361 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9362 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9363 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9364 TYPE_FIELDS (union_type)
9365 = (struct field *) TYPE_ZALLOC (union_type,
9366 (TYPE_NFIELDS (union_type)
9367 * sizeof (struct field)));
9368
9369 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9370 TYPE_NFIELDS (type) * sizeof (struct field));
9371
9372 /* Install the discriminant at index 0 in the union. */
9373 TYPE_FIELD (union_type, 0) = *disr_field;
9374 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9375 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9376
9377 /* Install the union in the outer struct type. */
9378 TYPE_FIELD_TYPE (type, 0) = union_type;
9379 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9380 TYPE_NFIELDS (type) = 1;
9381
9382 /* Set the size and offset of the union type. */
9383 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9384
9385 /* We need a way to find the correct discriminant given a
9386 variant name. For convenience we build a map here. */
9387 struct type *enum_type = FIELD_TYPE (*disr_field);
9388 std::unordered_map<std::string, ULONGEST> discriminant_map;
9389 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9390 {
9391 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9392 {
9393 const char *name
9394 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9395 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9396 }
9397 }
9398
9399 int n_fields = TYPE_NFIELDS (union_type);
9400 struct discriminant_info *disc
9401 = alloc_discriminant_info (union_type, 0, -1);
9402 /* Skip the discriminant here. */
9403 for (int i = 1; i < n_fields; ++i)
9404 {
9405 /* Find the final word in the name of this variant's type.
9406 That name can be used to look up the correct
9407 discriminant. */
9408 const char *variant_name
9409 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9410 i)));
9411
9412 auto iter = discriminant_map.find (variant_name);
9413 if (iter != discriminant_map.end ())
9414 disc->discriminants[i] = iter->second;
9415
9416 /* Remove the discriminant field, if it exists. */
9417 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9418 if (TYPE_NFIELDS (sub_type) > 0)
9419 {
9420 --TYPE_NFIELDS (sub_type);
9421 ++TYPE_FIELDS (sub_type);
9422 }
9423 TYPE_FIELD_NAME (union_type, i) = variant_name;
9424 TYPE_NAME (sub_type)
9425 = rust_fully_qualify (&objfile->objfile_obstack,
9426 TYPE_NAME (type), variant_name);
9427 }
9428 }
9429 }
9430
9431 /* Rewrite some Rust unions to be structures with variants parts. */
9432
9433 static void
9434 rust_union_quirks (struct dwarf2_cu *cu)
9435 {
9436 gdb_assert (cu->language == language_rust);
9437 for (type *type_ : cu->rust_unions)
9438 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9439 /* We don't need this any more. */
9440 cu->rust_unions.clear ();
9441 }
9442
9443 /* Return the symtab for PER_CU. This works properly regardless of
9444 whether we're using the index or psymtabs. */
9445
9446 static struct compunit_symtab *
9447 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9448 {
9449 return (per_cu->dwarf2_per_objfile->using_index
9450 ? per_cu->v.quick->compunit_symtab
9451 : per_cu->v.psymtab->compunit_symtab);
9452 }
9453
9454 /* A helper function for computing the list of all symbol tables
9455 included by PER_CU. */
9456
9457 static void
9458 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9459 htab_t all_children, htab_t all_type_symtabs,
9460 struct dwarf2_per_cu_data *per_cu,
9461 struct compunit_symtab *immediate_parent)
9462 {
9463 void **slot;
9464 struct compunit_symtab *cust;
9465
9466 slot = htab_find_slot (all_children, per_cu, INSERT);
9467 if (*slot != NULL)
9468 {
9469 /* This inclusion and its children have been processed. */
9470 return;
9471 }
9472
9473 *slot = per_cu;
9474 /* Only add a CU if it has a symbol table. */
9475 cust = get_compunit_symtab (per_cu);
9476 if (cust != NULL)
9477 {
9478 /* If this is a type unit only add its symbol table if we haven't
9479 seen it yet (type unit per_cu's can share symtabs). */
9480 if (per_cu->is_debug_types)
9481 {
9482 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9483 if (*slot == NULL)
9484 {
9485 *slot = cust;
9486 result->push_back (cust);
9487 if (cust->user == NULL)
9488 cust->user = immediate_parent;
9489 }
9490 }
9491 else
9492 {
9493 result->push_back (cust);
9494 if (cust->user == NULL)
9495 cust->user = immediate_parent;
9496 }
9497 }
9498
9499 if (!per_cu->imported_symtabs_empty ())
9500 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9501 {
9502 recursively_compute_inclusions (result, all_children,
9503 all_type_symtabs, ptr, cust);
9504 }
9505 }
9506
9507 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9508 PER_CU. */
9509
9510 static void
9511 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9512 {
9513 gdb_assert (! per_cu->is_debug_types);
9514
9515 if (!per_cu->imported_symtabs_empty ())
9516 {
9517 int len;
9518 std::vector<compunit_symtab *> result_symtabs;
9519 htab_t all_children, all_type_symtabs;
9520 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9521
9522 /* If we don't have a symtab, we can just skip this case. */
9523 if (cust == NULL)
9524 return;
9525
9526 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9527 NULL, xcalloc, xfree);
9528 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9529 NULL, xcalloc, xfree);
9530
9531 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9532 {
9533 recursively_compute_inclusions (&result_symtabs, all_children,
9534 all_type_symtabs, ptr, cust);
9535 }
9536
9537 /* Now we have a transitive closure of all the included symtabs. */
9538 len = result_symtabs.size ();
9539 cust->includes
9540 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9541 struct compunit_symtab *, len + 1);
9542 memcpy (cust->includes, result_symtabs.data (),
9543 len * sizeof (compunit_symtab *));
9544 cust->includes[len] = NULL;
9545
9546 htab_delete (all_children);
9547 htab_delete (all_type_symtabs);
9548 }
9549 }
9550
9551 /* Compute the 'includes' field for the symtabs of all the CUs we just
9552 read. */
9553
9554 static void
9555 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9556 {
9557 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9558 {
9559 if (! iter->is_debug_types)
9560 compute_compunit_symtab_includes (iter);
9561 }
9562
9563 dwarf2_per_objfile->just_read_cus.clear ();
9564 }
9565
9566 /* Generate full symbol information for PER_CU, whose DIEs have
9567 already been loaded into memory. */
9568
9569 static void
9570 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9571 enum language pretend_language)
9572 {
9573 struct dwarf2_cu *cu = per_cu->cu;
9574 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9575 struct objfile *objfile = dwarf2_per_objfile->objfile;
9576 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9577 CORE_ADDR lowpc, highpc;
9578 struct compunit_symtab *cust;
9579 CORE_ADDR baseaddr;
9580 struct block *static_block;
9581 CORE_ADDR addr;
9582
9583 baseaddr = objfile->text_section_offset ();
9584
9585 /* Clear the list here in case something was left over. */
9586 cu->method_list.clear ();
9587
9588 cu->language = pretend_language;
9589 cu->language_defn = language_def (cu->language);
9590
9591 /* Do line number decoding in read_file_scope () */
9592 process_die (cu->dies, cu);
9593
9594 /* For now fudge the Go package. */
9595 if (cu->language == language_go)
9596 fixup_go_packaging (cu);
9597
9598 /* Now that we have processed all the DIEs in the CU, all the types
9599 should be complete, and it should now be safe to compute all of the
9600 physnames. */
9601 compute_delayed_physnames (cu);
9602
9603 if (cu->language == language_rust)
9604 rust_union_quirks (cu);
9605
9606 /* Some compilers don't define a DW_AT_high_pc attribute for the
9607 compilation unit. If the DW_AT_high_pc is missing, synthesize
9608 it, by scanning the DIE's below the compilation unit. */
9609 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9610
9611 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9612 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9613
9614 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9615 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9616 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9617 addrmap to help ensure it has an accurate map of pc values belonging to
9618 this comp unit. */
9619 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9620
9621 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9622 SECT_OFF_TEXT (objfile),
9623 0);
9624
9625 if (cust != NULL)
9626 {
9627 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9628
9629 /* Set symtab language to language from DW_AT_language. If the
9630 compilation is from a C file generated by language preprocessors, do
9631 not set the language if it was already deduced by start_subfile. */
9632 if (!(cu->language == language_c
9633 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9634 COMPUNIT_FILETABS (cust)->language = cu->language;
9635
9636 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9637 produce DW_AT_location with location lists but it can be possibly
9638 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9639 there were bugs in prologue debug info, fixed later in GCC-4.5
9640 by "unwind info for epilogues" patch (which is not directly related).
9641
9642 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9643 needed, it would be wrong due to missing DW_AT_producer there.
9644
9645 Still one can confuse GDB by using non-standard GCC compilation
9646 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9647 */
9648 if (cu->has_loclist && gcc_4_minor >= 5)
9649 cust->locations_valid = 1;
9650
9651 if (gcc_4_minor >= 5)
9652 cust->epilogue_unwind_valid = 1;
9653
9654 cust->call_site_htab = cu->call_site_htab;
9655 }
9656
9657 if (dwarf2_per_objfile->using_index)
9658 per_cu->v.quick->compunit_symtab = cust;
9659 else
9660 {
9661 dwarf2_psymtab *pst = per_cu->v.psymtab;
9662 pst->compunit_symtab = cust;
9663 pst->readin = true;
9664 }
9665
9666 /* Push it for inclusion processing later. */
9667 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9668
9669 /* Not needed any more. */
9670 cu->reset_builder ();
9671 }
9672
9673 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9674 already been loaded into memory. */
9675
9676 static void
9677 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9678 enum language pretend_language)
9679 {
9680 struct dwarf2_cu *cu = per_cu->cu;
9681 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9682 struct objfile *objfile = dwarf2_per_objfile->objfile;
9683 struct compunit_symtab *cust;
9684 struct signatured_type *sig_type;
9685
9686 gdb_assert (per_cu->is_debug_types);
9687 sig_type = (struct signatured_type *) per_cu;
9688
9689 /* Clear the list here in case something was left over. */
9690 cu->method_list.clear ();
9691
9692 cu->language = pretend_language;
9693 cu->language_defn = language_def (cu->language);
9694
9695 /* The symbol tables are set up in read_type_unit_scope. */
9696 process_die (cu->dies, cu);
9697
9698 /* For now fudge the Go package. */
9699 if (cu->language == language_go)
9700 fixup_go_packaging (cu);
9701
9702 /* Now that we have processed all the DIEs in the CU, all the types
9703 should be complete, and it should now be safe to compute all of the
9704 physnames. */
9705 compute_delayed_physnames (cu);
9706
9707 if (cu->language == language_rust)
9708 rust_union_quirks (cu);
9709
9710 /* TUs share symbol tables.
9711 If this is the first TU to use this symtab, complete the construction
9712 of it with end_expandable_symtab. Otherwise, complete the addition of
9713 this TU's symbols to the existing symtab. */
9714 if (sig_type->type_unit_group->compunit_symtab == NULL)
9715 {
9716 buildsym_compunit *builder = cu->get_builder ();
9717 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9718 sig_type->type_unit_group->compunit_symtab = cust;
9719
9720 if (cust != NULL)
9721 {
9722 /* Set symtab language to language from DW_AT_language. If the
9723 compilation is from a C file generated by language preprocessors,
9724 do not set the language if it was already deduced by
9725 start_subfile. */
9726 if (!(cu->language == language_c
9727 && COMPUNIT_FILETABS (cust)->language != language_c))
9728 COMPUNIT_FILETABS (cust)->language = cu->language;
9729 }
9730 }
9731 else
9732 {
9733 cu->get_builder ()->augment_type_symtab ();
9734 cust = sig_type->type_unit_group->compunit_symtab;
9735 }
9736
9737 if (dwarf2_per_objfile->using_index)
9738 per_cu->v.quick->compunit_symtab = cust;
9739 else
9740 {
9741 dwarf2_psymtab *pst = per_cu->v.psymtab;
9742 pst->compunit_symtab = cust;
9743 pst->readin = true;
9744 }
9745
9746 /* Not needed any more. */
9747 cu->reset_builder ();
9748 }
9749
9750 /* Process an imported unit DIE. */
9751
9752 static void
9753 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9754 {
9755 struct attribute *attr;
9756
9757 /* For now we don't handle imported units in type units. */
9758 if (cu->per_cu->is_debug_types)
9759 {
9760 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9761 " supported in type units [in module %s]"),
9762 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9763 }
9764
9765 attr = dwarf2_attr (die, DW_AT_import, cu);
9766 if (attr != NULL)
9767 {
9768 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9769 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9770 dwarf2_per_cu_data *per_cu
9771 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9772 cu->per_cu->dwarf2_per_objfile);
9773
9774 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9775 into another compilation unit, at root level. Regard this as a hint,
9776 and ignore it. */
9777 if (die->parent && die->parent->parent == NULL
9778 && per_cu->unit_type == DW_UT_compile
9779 && per_cu->lang == language_cplus)
9780 return;
9781
9782 /* If necessary, add it to the queue and load its DIEs. */
9783 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9784 load_full_comp_unit (per_cu, false, cu->language);
9785
9786 cu->per_cu->imported_symtabs_push (per_cu);
9787 }
9788 }
9789
9790 /* RAII object that represents a process_die scope: i.e.,
9791 starts/finishes processing a DIE. */
9792 class process_die_scope
9793 {
9794 public:
9795 process_die_scope (die_info *die, dwarf2_cu *cu)
9796 : m_die (die), m_cu (cu)
9797 {
9798 /* We should only be processing DIEs not already in process. */
9799 gdb_assert (!m_die->in_process);
9800 m_die->in_process = true;
9801 }
9802
9803 ~process_die_scope ()
9804 {
9805 m_die->in_process = false;
9806
9807 /* If we're done processing the DIE for the CU that owns the line
9808 header, we don't need the line header anymore. */
9809 if (m_cu->line_header_die_owner == m_die)
9810 {
9811 delete m_cu->line_header;
9812 m_cu->line_header = NULL;
9813 m_cu->line_header_die_owner = NULL;
9814 }
9815 }
9816
9817 private:
9818 die_info *m_die;
9819 dwarf2_cu *m_cu;
9820 };
9821
9822 /* Process a die and its children. */
9823
9824 static void
9825 process_die (struct die_info *die, struct dwarf2_cu *cu)
9826 {
9827 process_die_scope scope (die, cu);
9828
9829 switch (die->tag)
9830 {
9831 case DW_TAG_padding:
9832 break;
9833 case DW_TAG_compile_unit:
9834 case DW_TAG_partial_unit:
9835 read_file_scope (die, cu);
9836 break;
9837 case DW_TAG_type_unit:
9838 read_type_unit_scope (die, cu);
9839 break;
9840 case DW_TAG_subprogram:
9841 /* Nested subprograms in Fortran get a prefix. */
9842 if (cu->language == language_fortran
9843 && die->parent != NULL
9844 && die->parent->tag == DW_TAG_subprogram)
9845 cu->processing_has_namespace_info = true;
9846 /* Fall through. */
9847 case DW_TAG_inlined_subroutine:
9848 read_func_scope (die, cu);
9849 break;
9850 case DW_TAG_lexical_block:
9851 case DW_TAG_try_block:
9852 case DW_TAG_catch_block:
9853 read_lexical_block_scope (die, cu);
9854 break;
9855 case DW_TAG_call_site:
9856 case DW_TAG_GNU_call_site:
9857 read_call_site_scope (die, cu);
9858 break;
9859 case DW_TAG_class_type:
9860 case DW_TAG_interface_type:
9861 case DW_TAG_structure_type:
9862 case DW_TAG_union_type:
9863 process_structure_scope (die, cu);
9864 break;
9865 case DW_TAG_enumeration_type:
9866 process_enumeration_scope (die, cu);
9867 break;
9868
9869 /* These dies have a type, but processing them does not create
9870 a symbol or recurse to process the children. Therefore we can
9871 read them on-demand through read_type_die. */
9872 case DW_TAG_subroutine_type:
9873 case DW_TAG_set_type:
9874 case DW_TAG_array_type:
9875 case DW_TAG_pointer_type:
9876 case DW_TAG_ptr_to_member_type:
9877 case DW_TAG_reference_type:
9878 case DW_TAG_rvalue_reference_type:
9879 case DW_TAG_string_type:
9880 break;
9881
9882 case DW_TAG_base_type:
9883 case DW_TAG_subrange_type:
9884 case DW_TAG_typedef:
9885 /* Add a typedef symbol for the type definition, if it has a
9886 DW_AT_name. */
9887 new_symbol (die, read_type_die (die, cu), cu);
9888 break;
9889 case DW_TAG_common_block:
9890 read_common_block (die, cu);
9891 break;
9892 case DW_TAG_common_inclusion:
9893 break;
9894 case DW_TAG_namespace:
9895 cu->processing_has_namespace_info = true;
9896 read_namespace (die, cu);
9897 break;
9898 case DW_TAG_module:
9899 cu->processing_has_namespace_info = true;
9900 read_module (die, cu);
9901 break;
9902 case DW_TAG_imported_declaration:
9903 cu->processing_has_namespace_info = true;
9904 if (read_namespace_alias (die, cu))
9905 break;
9906 /* The declaration is not a global namespace alias. */
9907 /* Fall through. */
9908 case DW_TAG_imported_module:
9909 cu->processing_has_namespace_info = true;
9910 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9911 || cu->language != language_fortran))
9912 complaint (_("Tag '%s' has unexpected children"),
9913 dwarf_tag_name (die->tag));
9914 read_import_statement (die, cu);
9915 break;
9916
9917 case DW_TAG_imported_unit:
9918 process_imported_unit_die (die, cu);
9919 break;
9920
9921 case DW_TAG_variable:
9922 read_variable (die, cu);
9923 break;
9924
9925 default:
9926 new_symbol (die, NULL, cu);
9927 break;
9928 }
9929 }
9930 \f
9931 /* DWARF name computation. */
9932
9933 /* A helper function for dwarf2_compute_name which determines whether DIE
9934 needs to have the name of the scope prepended to the name listed in the
9935 die. */
9936
9937 static int
9938 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9939 {
9940 struct attribute *attr;
9941
9942 switch (die->tag)
9943 {
9944 case DW_TAG_namespace:
9945 case DW_TAG_typedef:
9946 case DW_TAG_class_type:
9947 case DW_TAG_interface_type:
9948 case DW_TAG_structure_type:
9949 case DW_TAG_union_type:
9950 case DW_TAG_enumeration_type:
9951 case DW_TAG_enumerator:
9952 case DW_TAG_subprogram:
9953 case DW_TAG_inlined_subroutine:
9954 case DW_TAG_member:
9955 case DW_TAG_imported_declaration:
9956 return 1;
9957
9958 case DW_TAG_variable:
9959 case DW_TAG_constant:
9960 /* We only need to prefix "globally" visible variables. These include
9961 any variable marked with DW_AT_external or any variable that
9962 lives in a namespace. [Variables in anonymous namespaces
9963 require prefixing, but they are not DW_AT_external.] */
9964
9965 if (dwarf2_attr (die, DW_AT_specification, cu))
9966 {
9967 struct dwarf2_cu *spec_cu = cu;
9968
9969 return die_needs_namespace (die_specification (die, &spec_cu),
9970 spec_cu);
9971 }
9972
9973 attr = dwarf2_attr (die, DW_AT_external, cu);
9974 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9975 && die->parent->tag != DW_TAG_module)
9976 return 0;
9977 /* A variable in a lexical block of some kind does not need a
9978 namespace, even though in C++ such variables may be external
9979 and have a mangled name. */
9980 if (die->parent->tag == DW_TAG_lexical_block
9981 || die->parent->tag == DW_TAG_try_block
9982 || die->parent->tag == DW_TAG_catch_block
9983 || die->parent->tag == DW_TAG_subprogram)
9984 return 0;
9985 return 1;
9986
9987 default:
9988 return 0;
9989 }
9990 }
9991
9992 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9993 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9994 defined for the given DIE. */
9995
9996 static struct attribute *
9997 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9998 {
9999 struct attribute *attr;
10000
10001 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10002 if (attr == NULL)
10003 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10004
10005 return attr;
10006 }
10007
10008 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10009 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10010 defined for the given DIE. */
10011
10012 static const char *
10013 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10014 {
10015 const char *linkage_name;
10016
10017 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10018 if (linkage_name == NULL)
10019 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10020
10021 return linkage_name;
10022 }
10023
10024 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10025 compute the physname for the object, which include a method's:
10026 - formal parameters (C++),
10027 - receiver type (Go),
10028
10029 The term "physname" is a bit confusing.
10030 For C++, for example, it is the demangled name.
10031 For Go, for example, it's the mangled name.
10032
10033 For Ada, return the DIE's linkage name rather than the fully qualified
10034 name. PHYSNAME is ignored..
10035
10036 The result is allocated on the objfile_obstack and canonicalized. */
10037
10038 static const char *
10039 dwarf2_compute_name (const char *name,
10040 struct die_info *die, struct dwarf2_cu *cu,
10041 int physname)
10042 {
10043 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10044
10045 if (name == NULL)
10046 name = dwarf2_name (die, cu);
10047
10048 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10049 but otherwise compute it by typename_concat inside GDB.
10050 FIXME: Actually this is not really true, or at least not always true.
10051 It's all very confusing. compute_and_set_names doesn't try to demangle
10052 Fortran names because there is no mangling standard. So new_symbol
10053 will set the demangled name to the result of dwarf2_full_name, and it is
10054 the demangled name that GDB uses if it exists. */
10055 if (cu->language == language_ada
10056 || (cu->language == language_fortran && physname))
10057 {
10058 /* For Ada unit, we prefer the linkage name over the name, as
10059 the former contains the exported name, which the user expects
10060 to be able to reference. Ideally, we want the user to be able
10061 to reference this entity using either natural or linkage name,
10062 but we haven't started looking at this enhancement yet. */
10063 const char *linkage_name = dw2_linkage_name (die, cu);
10064
10065 if (linkage_name != NULL)
10066 return linkage_name;
10067 }
10068
10069 /* These are the only languages we know how to qualify names in. */
10070 if (name != NULL
10071 && (cu->language == language_cplus
10072 || cu->language == language_fortran || cu->language == language_d
10073 || cu->language == language_rust))
10074 {
10075 if (die_needs_namespace (die, cu))
10076 {
10077 const char *prefix;
10078 const char *canonical_name = NULL;
10079
10080 string_file buf;
10081
10082 prefix = determine_prefix (die, cu);
10083 if (*prefix != '\0')
10084 {
10085 gdb::unique_xmalloc_ptr<char> prefixed_name
10086 (typename_concat (NULL, prefix, name, physname, cu));
10087
10088 buf.puts (prefixed_name.get ());
10089 }
10090 else
10091 buf.puts (name);
10092
10093 /* Template parameters may be specified in the DIE's DW_AT_name, or
10094 as children with DW_TAG_template_type_param or
10095 DW_TAG_value_type_param. If the latter, add them to the name
10096 here. If the name already has template parameters, then
10097 skip this step; some versions of GCC emit both, and
10098 it is more efficient to use the pre-computed name.
10099
10100 Something to keep in mind about this process: it is very
10101 unlikely, or in some cases downright impossible, to produce
10102 something that will match the mangled name of a function.
10103 If the definition of the function has the same debug info,
10104 we should be able to match up with it anyway. But fallbacks
10105 using the minimal symbol, for instance to find a method
10106 implemented in a stripped copy of libstdc++, will not work.
10107 If we do not have debug info for the definition, we will have to
10108 match them up some other way.
10109
10110 When we do name matching there is a related problem with function
10111 templates; two instantiated function templates are allowed to
10112 differ only by their return types, which we do not add here. */
10113
10114 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10115 {
10116 struct attribute *attr;
10117 struct die_info *child;
10118 int first = 1;
10119
10120 die->building_fullname = 1;
10121
10122 for (child = die->child; child != NULL; child = child->sibling)
10123 {
10124 struct type *type;
10125 LONGEST value;
10126 const gdb_byte *bytes;
10127 struct dwarf2_locexpr_baton *baton;
10128 struct value *v;
10129
10130 if (child->tag != DW_TAG_template_type_param
10131 && child->tag != DW_TAG_template_value_param)
10132 continue;
10133
10134 if (first)
10135 {
10136 buf.puts ("<");
10137 first = 0;
10138 }
10139 else
10140 buf.puts (", ");
10141
10142 attr = dwarf2_attr (child, DW_AT_type, cu);
10143 if (attr == NULL)
10144 {
10145 complaint (_("template parameter missing DW_AT_type"));
10146 buf.puts ("UNKNOWN_TYPE");
10147 continue;
10148 }
10149 type = die_type (child, cu);
10150
10151 if (child->tag == DW_TAG_template_type_param)
10152 {
10153 c_print_type (type, "", &buf, -1, 0, cu->language,
10154 &type_print_raw_options);
10155 continue;
10156 }
10157
10158 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10159 if (attr == NULL)
10160 {
10161 complaint (_("template parameter missing "
10162 "DW_AT_const_value"));
10163 buf.puts ("UNKNOWN_VALUE");
10164 continue;
10165 }
10166
10167 dwarf2_const_value_attr (attr, type, name,
10168 &cu->comp_unit_obstack, cu,
10169 &value, &bytes, &baton);
10170
10171 if (TYPE_NOSIGN (type))
10172 /* GDB prints characters as NUMBER 'CHAR'. If that's
10173 changed, this can use value_print instead. */
10174 c_printchar (value, type, &buf);
10175 else
10176 {
10177 struct value_print_options opts;
10178
10179 if (baton != NULL)
10180 v = dwarf2_evaluate_loc_desc (type, NULL,
10181 baton->data,
10182 baton->size,
10183 baton->per_cu);
10184 else if (bytes != NULL)
10185 {
10186 v = allocate_value (type);
10187 memcpy (value_contents_writeable (v), bytes,
10188 TYPE_LENGTH (type));
10189 }
10190 else
10191 v = value_from_longest (type, value);
10192
10193 /* Specify decimal so that we do not depend on
10194 the radix. */
10195 get_formatted_print_options (&opts, 'd');
10196 opts.raw = 1;
10197 value_print (v, &buf, &opts);
10198 release_value (v);
10199 }
10200 }
10201
10202 die->building_fullname = 0;
10203
10204 if (!first)
10205 {
10206 /* Close the argument list, with a space if necessary
10207 (nested templates). */
10208 if (!buf.empty () && buf.string ().back () == '>')
10209 buf.puts (" >");
10210 else
10211 buf.puts (">");
10212 }
10213 }
10214
10215 /* For C++ methods, append formal parameter type
10216 information, if PHYSNAME. */
10217
10218 if (physname && die->tag == DW_TAG_subprogram
10219 && cu->language == language_cplus)
10220 {
10221 struct type *type = read_type_die (die, cu);
10222
10223 c_type_print_args (type, &buf, 1, cu->language,
10224 &type_print_raw_options);
10225
10226 if (cu->language == language_cplus)
10227 {
10228 /* Assume that an artificial first parameter is
10229 "this", but do not crash if it is not. RealView
10230 marks unnamed (and thus unused) parameters as
10231 artificial; there is no way to differentiate
10232 the two cases. */
10233 if (TYPE_NFIELDS (type) > 0
10234 && TYPE_FIELD_ARTIFICIAL (type, 0)
10235 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10236 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10237 0))))
10238 buf.puts (" const");
10239 }
10240 }
10241
10242 const std::string &intermediate_name = buf.string ();
10243
10244 if (cu->language == language_cplus)
10245 canonical_name
10246 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10247 objfile);
10248
10249 /* If we only computed INTERMEDIATE_NAME, or if
10250 INTERMEDIATE_NAME is already canonical, then we need to
10251 intern it. */
10252 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10253 name = objfile->intern (intermediate_name);
10254 else
10255 name = canonical_name;
10256 }
10257 }
10258
10259 return name;
10260 }
10261
10262 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10263 If scope qualifiers are appropriate they will be added. The result
10264 will be allocated on the storage_obstack, or NULL if the DIE does
10265 not have a name. NAME may either be from a previous call to
10266 dwarf2_name or NULL.
10267
10268 The output string will be canonicalized (if C++). */
10269
10270 static const char *
10271 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10272 {
10273 return dwarf2_compute_name (name, die, cu, 0);
10274 }
10275
10276 /* Construct a physname for the given DIE in CU. NAME may either be
10277 from a previous call to dwarf2_name or NULL. The result will be
10278 allocated on the objfile_objstack or NULL if the DIE does not have a
10279 name.
10280
10281 The output string will be canonicalized (if C++). */
10282
10283 static const char *
10284 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10285 {
10286 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10287 const char *retval, *mangled = NULL, *canon = NULL;
10288 int need_copy = 1;
10289
10290 /* In this case dwarf2_compute_name is just a shortcut not building anything
10291 on its own. */
10292 if (!die_needs_namespace (die, cu))
10293 return dwarf2_compute_name (name, die, cu, 1);
10294
10295 mangled = dw2_linkage_name (die, cu);
10296
10297 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10298 See https://github.com/rust-lang/rust/issues/32925. */
10299 if (cu->language == language_rust && mangled != NULL
10300 && strchr (mangled, '{') != NULL)
10301 mangled = NULL;
10302
10303 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10304 has computed. */
10305 gdb::unique_xmalloc_ptr<char> demangled;
10306 if (mangled != NULL)
10307 {
10308
10309 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10310 {
10311 /* Do nothing (do not demangle the symbol name). */
10312 }
10313 else if (cu->language == language_go)
10314 {
10315 /* This is a lie, but we already lie to the caller new_symbol.
10316 new_symbol assumes we return the mangled name.
10317 This just undoes that lie until things are cleaned up. */
10318 }
10319 else
10320 {
10321 /* Use DMGL_RET_DROP for C++ template functions to suppress
10322 their return type. It is easier for GDB users to search
10323 for such functions as `name(params)' than `long name(params)'.
10324 In such case the minimal symbol names do not match the full
10325 symbol names but for template functions there is never a need
10326 to look up their definition from their declaration so
10327 the only disadvantage remains the minimal symbol variant
10328 `long name(params)' does not have the proper inferior type. */
10329 demangled.reset (gdb_demangle (mangled,
10330 (DMGL_PARAMS | DMGL_ANSI
10331 | DMGL_RET_DROP)));
10332 }
10333 if (demangled)
10334 canon = demangled.get ();
10335 else
10336 {
10337 canon = mangled;
10338 need_copy = 0;
10339 }
10340 }
10341
10342 if (canon == NULL || check_physname)
10343 {
10344 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10345
10346 if (canon != NULL && strcmp (physname, canon) != 0)
10347 {
10348 /* It may not mean a bug in GDB. The compiler could also
10349 compute DW_AT_linkage_name incorrectly. But in such case
10350 GDB would need to be bug-to-bug compatible. */
10351
10352 complaint (_("Computed physname <%s> does not match demangled <%s> "
10353 "(from linkage <%s>) - DIE at %s [in module %s]"),
10354 physname, canon, mangled, sect_offset_str (die->sect_off),
10355 objfile_name (objfile));
10356
10357 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10358 is available here - over computed PHYSNAME. It is safer
10359 against both buggy GDB and buggy compilers. */
10360
10361 retval = canon;
10362 }
10363 else
10364 {
10365 retval = physname;
10366 need_copy = 0;
10367 }
10368 }
10369 else
10370 retval = canon;
10371
10372 if (need_copy)
10373 retval = objfile->intern (retval);
10374
10375 return retval;
10376 }
10377
10378 /* Inspect DIE in CU for a namespace alias. If one exists, record
10379 a new symbol for it.
10380
10381 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10382
10383 static int
10384 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10385 {
10386 struct attribute *attr;
10387
10388 /* If the die does not have a name, this is not a namespace
10389 alias. */
10390 attr = dwarf2_attr (die, DW_AT_name, cu);
10391 if (attr != NULL)
10392 {
10393 int num;
10394 struct die_info *d = die;
10395 struct dwarf2_cu *imported_cu = cu;
10396
10397 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10398 keep inspecting DIEs until we hit the underlying import. */
10399 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10400 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10401 {
10402 attr = dwarf2_attr (d, DW_AT_import, cu);
10403 if (attr == NULL)
10404 break;
10405
10406 d = follow_die_ref (d, attr, &imported_cu);
10407 if (d->tag != DW_TAG_imported_declaration)
10408 break;
10409 }
10410
10411 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10412 {
10413 complaint (_("DIE at %s has too many recursively imported "
10414 "declarations"), sect_offset_str (d->sect_off));
10415 return 0;
10416 }
10417
10418 if (attr != NULL)
10419 {
10420 struct type *type;
10421 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10422
10423 type = get_die_type_at_offset (sect_off, cu->per_cu);
10424 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10425 {
10426 /* This declaration is a global namespace alias. Add
10427 a symbol for it whose type is the aliased namespace. */
10428 new_symbol (die, type, cu);
10429 return 1;
10430 }
10431 }
10432 }
10433
10434 return 0;
10435 }
10436
10437 /* Return the using directives repository (global or local?) to use in the
10438 current context for CU.
10439
10440 For Ada, imported declarations can materialize renamings, which *may* be
10441 global. However it is impossible (for now?) in DWARF to distinguish
10442 "external" imported declarations and "static" ones. As all imported
10443 declarations seem to be static in all other languages, make them all CU-wide
10444 global only in Ada. */
10445
10446 static struct using_direct **
10447 using_directives (struct dwarf2_cu *cu)
10448 {
10449 if (cu->language == language_ada
10450 && cu->get_builder ()->outermost_context_p ())
10451 return cu->get_builder ()->get_global_using_directives ();
10452 else
10453 return cu->get_builder ()->get_local_using_directives ();
10454 }
10455
10456 /* Read the import statement specified by the given die and record it. */
10457
10458 static void
10459 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10460 {
10461 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10462 struct attribute *import_attr;
10463 struct die_info *imported_die, *child_die;
10464 struct dwarf2_cu *imported_cu;
10465 const char *imported_name;
10466 const char *imported_name_prefix;
10467 const char *canonical_name;
10468 const char *import_alias;
10469 const char *imported_declaration = NULL;
10470 const char *import_prefix;
10471 std::vector<const char *> excludes;
10472
10473 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10474 if (import_attr == NULL)
10475 {
10476 complaint (_("Tag '%s' has no DW_AT_import"),
10477 dwarf_tag_name (die->tag));
10478 return;
10479 }
10480
10481 imported_cu = cu;
10482 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10483 imported_name = dwarf2_name (imported_die, imported_cu);
10484 if (imported_name == NULL)
10485 {
10486 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10487
10488 The import in the following code:
10489 namespace A
10490 {
10491 typedef int B;
10492 }
10493
10494 int main ()
10495 {
10496 using A::B;
10497 B b;
10498 return b;
10499 }
10500
10501 ...
10502 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10503 <52> DW_AT_decl_file : 1
10504 <53> DW_AT_decl_line : 6
10505 <54> DW_AT_import : <0x75>
10506 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10507 <59> DW_AT_name : B
10508 <5b> DW_AT_decl_file : 1
10509 <5c> DW_AT_decl_line : 2
10510 <5d> DW_AT_type : <0x6e>
10511 ...
10512 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10513 <76> DW_AT_byte_size : 4
10514 <77> DW_AT_encoding : 5 (signed)
10515
10516 imports the wrong die ( 0x75 instead of 0x58 ).
10517 This case will be ignored until the gcc bug is fixed. */
10518 return;
10519 }
10520
10521 /* Figure out the local name after import. */
10522 import_alias = dwarf2_name (die, cu);
10523
10524 /* Figure out where the statement is being imported to. */
10525 import_prefix = determine_prefix (die, cu);
10526
10527 /* Figure out what the scope of the imported die is and prepend it
10528 to the name of the imported die. */
10529 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10530
10531 if (imported_die->tag != DW_TAG_namespace
10532 && imported_die->tag != DW_TAG_module)
10533 {
10534 imported_declaration = imported_name;
10535 canonical_name = imported_name_prefix;
10536 }
10537 else if (strlen (imported_name_prefix) > 0)
10538 canonical_name = obconcat (&objfile->objfile_obstack,
10539 imported_name_prefix,
10540 (cu->language == language_d ? "." : "::"),
10541 imported_name, (char *) NULL);
10542 else
10543 canonical_name = imported_name;
10544
10545 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10546 for (child_die = die->child; child_die && child_die->tag;
10547 child_die = sibling_die (child_die))
10548 {
10549 /* DWARF-4: A Fortran use statement with a “rename list” may be
10550 represented by an imported module entry with an import attribute
10551 referring to the module and owned entries corresponding to those
10552 entities that are renamed as part of being imported. */
10553
10554 if (child_die->tag != DW_TAG_imported_declaration)
10555 {
10556 complaint (_("child DW_TAG_imported_declaration expected "
10557 "- DIE at %s [in module %s]"),
10558 sect_offset_str (child_die->sect_off),
10559 objfile_name (objfile));
10560 continue;
10561 }
10562
10563 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10564 if (import_attr == NULL)
10565 {
10566 complaint (_("Tag '%s' has no DW_AT_import"),
10567 dwarf_tag_name (child_die->tag));
10568 continue;
10569 }
10570
10571 imported_cu = cu;
10572 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10573 &imported_cu);
10574 imported_name = dwarf2_name (imported_die, imported_cu);
10575 if (imported_name == NULL)
10576 {
10577 complaint (_("child DW_TAG_imported_declaration has unknown "
10578 "imported name - DIE at %s [in module %s]"),
10579 sect_offset_str (child_die->sect_off),
10580 objfile_name (objfile));
10581 continue;
10582 }
10583
10584 excludes.push_back (imported_name);
10585
10586 process_die (child_die, cu);
10587 }
10588
10589 add_using_directive (using_directives (cu),
10590 import_prefix,
10591 canonical_name,
10592 import_alias,
10593 imported_declaration,
10594 excludes,
10595 0,
10596 &objfile->objfile_obstack);
10597 }
10598
10599 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10600 types, but gives them a size of zero. Starting with version 14,
10601 ICC is compatible with GCC. */
10602
10603 static bool
10604 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10605 {
10606 if (!cu->checked_producer)
10607 check_producer (cu);
10608
10609 return cu->producer_is_icc_lt_14;
10610 }
10611
10612 /* ICC generates a DW_AT_type for C void functions. This was observed on
10613 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10614 which says that void functions should not have a DW_AT_type. */
10615
10616 static bool
10617 producer_is_icc (struct dwarf2_cu *cu)
10618 {
10619 if (!cu->checked_producer)
10620 check_producer (cu);
10621
10622 return cu->producer_is_icc;
10623 }
10624
10625 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10626 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10627 this, it was first present in GCC release 4.3.0. */
10628
10629 static bool
10630 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10631 {
10632 if (!cu->checked_producer)
10633 check_producer (cu);
10634
10635 return cu->producer_is_gcc_lt_4_3;
10636 }
10637
10638 static file_and_directory
10639 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10640 {
10641 file_and_directory res;
10642
10643 /* Find the filename. Do not use dwarf2_name here, since the filename
10644 is not a source language identifier. */
10645 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10646 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10647
10648 if (res.comp_dir == NULL
10649 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10650 && IS_ABSOLUTE_PATH (res.name))
10651 {
10652 res.comp_dir_storage = ldirname (res.name);
10653 if (!res.comp_dir_storage.empty ())
10654 res.comp_dir = res.comp_dir_storage.c_str ();
10655 }
10656 if (res.comp_dir != NULL)
10657 {
10658 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10659 directory, get rid of it. */
10660 const char *cp = strchr (res.comp_dir, ':');
10661
10662 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10663 res.comp_dir = cp + 1;
10664 }
10665
10666 if (res.name == NULL)
10667 res.name = "<unknown>";
10668
10669 return res;
10670 }
10671
10672 /* Handle DW_AT_stmt_list for a compilation unit.
10673 DIE is the DW_TAG_compile_unit die for CU.
10674 COMP_DIR is the compilation directory. LOWPC is passed to
10675 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10676
10677 static void
10678 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10679 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10680 {
10681 struct dwarf2_per_objfile *dwarf2_per_objfile
10682 = cu->per_cu->dwarf2_per_objfile;
10683 struct attribute *attr;
10684 struct line_header line_header_local;
10685 hashval_t line_header_local_hash;
10686 void **slot;
10687 int decode_mapping;
10688
10689 gdb_assert (! cu->per_cu->is_debug_types);
10690
10691 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10692 if (attr == NULL)
10693 return;
10694
10695 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10696
10697 /* The line header hash table is only created if needed (it exists to
10698 prevent redundant reading of the line table for partial_units).
10699 If we're given a partial_unit, we'll need it. If we're given a
10700 compile_unit, then use the line header hash table if it's already
10701 created, but don't create one just yet. */
10702
10703 if (dwarf2_per_objfile->line_header_hash == NULL
10704 && die->tag == DW_TAG_partial_unit)
10705 {
10706 dwarf2_per_objfile->line_header_hash
10707 .reset (htab_create_alloc (127, line_header_hash_voidp,
10708 line_header_eq_voidp,
10709 free_line_header_voidp,
10710 xcalloc, xfree));
10711 }
10712
10713 line_header_local.sect_off = line_offset;
10714 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10715 line_header_local_hash = line_header_hash (&line_header_local);
10716 if (dwarf2_per_objfile->line_header_hash != NULL)
10717 {
10718 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10719 &line_header_local,
10720 line_header_local_hash, NO_INSERT);
10721
10722 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10723 is not present in *SLOT (since if there is something in *SLOT then
10724 it will be for a partial_unit). */
10725 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10726 {
10727 gdb_assert (*slot != NULL);
10728 cu->line_header = (struct line_header *) *slot;
10729 return;
10730 }
10731 }
10732
10733 /* dwarf_decode_line_header does not yet provide sufficient information.
10734 We always have to call also dwarf_decode_lines for it. */
10735 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10736 if (lh == NULL)
10737 return;
10738
10739 cu->line_header = lh.release ();
10740 cu->line_header_die_owner = die;
10741
10742 if (dwarf2_per_objfile->line_header_hash == NULL)
10743 slot = NULL;
10744 else
10745 {
10746 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10747 &line_header_local,
10748 line_header_local_hash, INSERT);
10749 gdb_assert (slot != NULL);
10750 }
10751 if (slot != NULL && *slot == NULL)
10752 {
10753 /* This newly decoded line number information unit will be owned
10754 by line_header_hash hash table. */
10755 *slot = cu->line_header;
10756 cu->line_header_die_owner = NULL;
10757 }
10758 else
10759 {
10760 /* We cannot free any current entry in (*slot) as that struct line_header
10761 may be already used by multiple CUs. Create only temporary decoded
10762 line_header for this CU - it may happen at most once for each line
10763 number information unit. And if we're not using line_header_hash
10764 then this is what we want as well. */
10765 gdb_assert (die->tag != DW_TAG_partial_unit);
10766 }
10767 decode_mapping = (die->tag != DW_TAG_partial_unit);
10768 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10769 decode_mapping);
10770
10771 }
10772
10773 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10774
10775 static void
10776 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10777 {
10778 struct dwarf2_per_objfile *dwarf2_per_objfile
10779 = cu->per_cu->dwarf2_per_objfile;
10780 struct objfile *objfile = dwarf2_per_objfile->objfile;
10781 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10782 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10783 CORE_ADDR highpc = ((CORE_ADDR) 0);
10784 struct attribute *attr;
10785 struct die_info *child_die;
10786 CORE_ADDR baseaddr;
10787
10788 prepare_one_comp_unit (cu, die, cu->language);
10789 baseaddr = objfile->text_section_offset ();
10790
10791 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10792
10793 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10794 from finish_block. */
10795 if (lowpc == ((CORE_ADDR) -1))
10796 lowpc = highpc;
10797 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10798
10799 file_and_directory fnd = find_file_and_directory (die, cu);
10800
10801 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10802 standardised yet. As a workaround for the language detection we fall
10803 back to the DW_AT_producer string. */
10804 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10805 cu->language = language_opencl;
10806
10807 /* Similar hack for Go. */
10808 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10809 set_cu_language (DW_LANG_Go, cu);
10810
10811 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10812
10813 /* Decode line number information if present. We do this before
10814 processing child DIEs, so that the line header table is available
10815 for DW_AT_decl_file. */
10816 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10817
10818 /* Process all dies in compilation unit. */
10819 if (die->child != NULL)
10820 {
10821 child_die = die->child;
10822 while (child_die && child_die->tag)
10823 {
10824 process_die (child_die, cu);
10825 child_die = sibling_die (child_die);
10826 }
10827 }
10828
10829 /* Decode macro information, if present. Dwarf 2 macro information
10830 refers to information in the line number info statement program
10831 header, so we can only read it if we've read the header
10832 successfully. */
10833 attr = dwarf2_attr (die, DW_AT_macros, cu);
10834 if (attr == NULL)
10835 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10836 if (attr && cu->line_header)
10837 {
10838 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10839 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10840
10841 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10842 }
10843 else
10844 {
10845 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10846 if (attr && cu->line_header)
10847 {
10848 unsigned int macro_offset = DW_UNSND (attr);
10849
10850 dwarf_decode_macros (cu, macro_offset, 0);
10851 }
10852 }
10853 }
10854
10855 void
10856 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10857 {
10858 struct type_unit_group *tu_group;
10859 int first_time;
10860 struct attribute *attr;
10861 unsigned int i;
10862 struct signatured_type *sig_type;
10863
10864 gdb_assert (per_cu->is_debug_types);
10865 sig_type = (struct signatured_type *) per_cu;
10866
10867 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10868
10869 /* If we're using .gdb_index (includes -readnow) then
10870 per_cu->type_unit_group may not have been set up yet. */
10871 if (sig_type->type_unit_group == NULL)
10872 sig_type->type_unit_group = get_type_unit_group (this, attr);
10873 tu_group = sig_type->type_unit_group;
10874
10875 /* If we've already processed this stmt_list there's no real need to
10876 do it again, we could fake it and just recreate the part we need
10877 (file name,index -> symtab mapping). If data shows this optimization
10878 is useful we can do it then. */
10879 first_time = tu_group->compunit_symtab == NULL;
10880
10881 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10882 debug info. */
10883 line_header_up lh;
10884 if (attr != NULL)
10885 {
10886 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10887 lh = dwarf_decode_line_header (line_offset, this);
10888 }
10889 if (lh == NULL)
10890 {
10891 if (first_time)
10892 start_symtab ("", NULL, 0);
10893 else
10894 {
10895 gdb_assert (tu_group->symtabs == NULL);
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 return;
10905 }
10906
10907 line_header = lh.release ();
10908 line_header_die_owner = die;
10909
10910 if (first_time)
10911 {
10912 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10913
10914 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10915 still initializing it, and our caller (a few levels up)
10916 process_full_type_unit still needs to know if this is the first
10917 time. */
10918
10919 tu_group->symtabs
10920 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10921 struct symtab *, line_header->file_names_size ());
10922
10923 auto &file_names = line_header->file_names ();
10924 for (i = 0; i < file_names.size (); ++i)
10925 {
10926 file_entry &fe = file_names[i];
10927 dwarf2_start_subfile (this, fe.name,
10928 fe.include_dir (line_header));
10929 buildsym_compunit *b = get_builder ();
10930 if (b->get_current_subfile ()->symtab == NULL)
10931 {
10932 /* NOTE: start_subfile will recognize when it's been
10933 passed a file it has already seen. So we can't
10934 assume there's a simple mapping from
10935 cu->line_header->file_names to subfiles, plus
10936 cu->line_header->file_names may contain dups. */
10937 b->get_current_subfile ()->symtab
10938 = allocate_symtab (cust, b->get_current_subfile ()->name);
10939 }
10940
10941 fe.symtab = b->get_current_subfile ()->symtab;
10942 tu_group->symtabs[i] = fe.symtab;
10943 }
10944 }
10945 else
10946 {
10947 gdb_assert (m_builder == nullptr);
10948 struct compunit_symtab *cust = tu_group->compunit_symtab;
10949 m_builder.reset (new struct buildsym_compunit
10950 (COMPUNIT_OBJFILE (cust), "",
10951 COMPUNIT_DIRNAME (cust),
10952 compunit_language (cust),
10953 0, cust));
10954
10955 auto &file_names = line_header->file_names ();
10956 for (i = 0; i < file_names.size (); ++i)
10957 {
10958 file_entry &fe = file_names[i];
10959 fe.symtab = tu_group->symtabs[i];
10960 }
10961 }
10962
10963 /* The main symtab is allocated last. Type units don't have DW_AT_name
10964 so they don't have a "real" (so to speak) symtab anyway.
10965 There is later code that will assign the main symtab to all symbols
10966 that don't have one. We need to handle the case of a symbol with a
10967 missing symtab (DW_AT_decl_file) anyway. */
10968 }
10969
10970 /* Process DW_TAG_type_unit.
10971 For TUs we want to skip the first top level sibling if it's not the
10972 actual type being defined by this TU. In this case the first top
10973 level sibling is there to provide context only. */
10974
10975 static void
10976 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10977 {
10978 struct die_info *child_die;
10979
10980 prepare_one_comp_unit (cu, die, language_minimal);
10981
10982 /* Initialize (or reinitialize) the machinery for building symtabs.
10983 We do this before processing child DIEs, so that the line header table
10984 is available for DW_AT_decl_file. */
10985 cu->setup_type_unit_groups (die);
10986
10987 if (die->child != NULL)
10988 {
10989 child_die = die->child;
10990 while (child_die && child_die->tag)
10991 {
10992 process_die (child_die, cu);
10993 child_die = sibling_die (child_die);
10994 }
10995 }
10996 }
10997 \f
10998 /* DWO/DWP files.
10999
11000 http://gcc.gnu.org/wiki/DebugFission
11001 http://gcc.gnu.org/wiki/DebugFissionDWP
11002
11003 To simplify handling of both DWO files ("object" files with the DWARF info)
11004 and DWP files (a file with the DWOs packaged up into one file), we treat
11005 DWP files as having a collection of virtual DWO files. */
11006
11007 static hashval_t
11008 hash_dwo_file (const void *item)
11009 {
11010 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11011 hashval_t hash;
11012
11013 hash = htab_hash_string (dwo_file->dwo_name);
11014 if (dwo_file->comp_dir != NULL)
11015 hash += htab_hash_string (dwo_file->comp_dir);
11016 return hash;
11017 }
11018
11019 static int
11020 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11021 {
11022 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11023 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11024
11025 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11026 return 0;
11027 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11028 return lhs->comp_dir == rhs->comp_dir;
11029 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11030 }
11031
11032 /* Allocate a hash table for DWO files. */
11033
11034 static htab_up
11035 allocate_dwo_file_hash_table ()
11036 {
11037 auto delete_dwo_file = [] (void *item)
11038 {
11039 struct dwo_file *dwo_file = (struct dwo_file *) item;
11040
11041 delete dwo_file;
11042 };
11043
11044 return htab_up (htab_create_alloc (41,
11045 hash_dwo_file,
11046 eq_dwo_file,
11047 delete_dwo_file,
11048 xcalloc, xfree));
11049 }
11050
11051 /* Lookup DWO file DWO_NAME. */
11052
11053 static void **
11054 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11055 const char *dwo_name,
11056 const char *comp_dir)
11057 {
11058 struct dwo_file find_entry;
11059 void **slot;
11060
11061 if (dwarf2_per_objfile->dwo_files == NULL)
11062 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11063
11064 find_entry.dwo_name = dwo_name;
11065 find_entry.comp_dir = comp_dir;
11066 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11067 INSERT);
11068
11069 return slot;
11070 }
11071
11072 static hashval_t
11073 hash_dwo_unit (const void *item)
11074 {
11075 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11076
11077 /* This drops the top 32 bits of the id, but is ok for a hash. */
11078 return dwo_unit->signature;
11079 }
11080
11081 static int
11082 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11083 {
11084 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11085 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11086
11087 /* The signature is assumed to be unique within the DWO file.
11088 So while object file CU dwo_id's always have the value zero,
11089 that's OK, assuming each object file DWO file has only one CU,
11090 and that's the rule for now. */
11091 return lhs->signature == rhs->signature;
11092 }
11093
11094 /* Allocate a hash table for DWO CUs,TUs.
11095 There is one of these tables for each of CUs,TUs for each DWO file. */
11096
11097 static htab_up
11098 allocate_dwo_unit_table ()
11099 {
11100 /* Start out with a pretty small number.
11101 Generally DWO files contain only one CU and maybe some TUs. */
11102 return htab_up (htab_create_alloc (3,
11103 hash_dwo_unit,
11104 eq_dwo_unit,
11105 NULL, xcalloc, xfree));
11106 }
11107
11108 /* die_reader_func for create_dwo_cu. */
11109
11110 static void
11111 create_dwo_cu_reader (const struct die_reader_specs *reader,
11112 const gdb_byte *info_ptr,
11113 struct die_info *comp_unit_die,
11114 struct dwo_file *dwo_file,
11115 struct dwo_unit *dwo_unit)
11116 {
11117 struct dwarf2_cu *cu = reader->cu;
11118 sect_offset sect_off = cu->per_cu->sect_off;
11119 struct dwarf2_section_info *section = cu->per_cu->section;
11120
11121 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11122 if (!signature.has_value ())
11123 {
11124 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11125 " its dwo_id [in module %s]"),
11126 sect_offset_str (sect_off), dwo_file->dwo_name);
11127 return;
11128 }
11129
11130 dwo_unit->dwo_file = dwo_file;
11131 dwo_unit->signature = *signature;
11132 dwo_unit->section = section;
11133 dwo_unit->sect_off = sect_off;
11134 dwo_unit->length = cu->per_cu->length;
11135
11136 if (dwarf_read_debug)
11137 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11138 sect_offset_str (sect_off),
11139 hex_string (dwo_unit->signature));
11140 }
11141
11142 /* Create the dwo_units for the CUs in a DWO_FILE.
11143 Note: This function processes DWO files only, not DWP files. */
11144
11145 static void
11146 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11147 dwarf2_cu *cu, struct dwo_file &dwo_file,
11148 dwarf2_section_info &section, htab_up &cus_htab)
11149 {
11150 struct objfile *objfile = dwarf2_per_objfile->objfile;
11151 const gdb_byte *info_ptr, *end_ptr;
11152
11153 section.read (objfile);
11154 info_ptr = section.buffer;
11155
11156 if (info_ptr == NULL)
11157 return;
11158
11159 if (dwarf_read_debug)
11160 {
11161 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11162 section.get_name (),
11163 section.get_file_name ());
11164 }
11165
11166 end_ptr = info_ptr + section.size;
11167 while (info_ptr < end_ptr)
11168 {
11169 struct dwarf2_per_cu_data per_cu;
11170 struct dwo_unit read_unit {};
11171 struct dwo_unit *dwo_unit;
11172 void **slot;
11173 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11174
11175 memset (&per_cu, 0, sizeof (per_cu));
11176 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11177 per_cu.is_debug_types = 0;
11178 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11179 per_cu.section = &section;
11180
11181 cutu_reader reader (&per_cu, cu, &dwo_file);
11182 if (!reader.dummy_p)
11183 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11184 &dwo_file, &read_unit);
11185 info_ptr += per_cu.length;
11186
11187 // If the unit could not be parsed, skip it.
11188 if (read_unit.dwo_file == NULL)
11189 continue;
11190
11191 if (cus_htab == NULL)
11192 cus_htab = allocate_dwo_unit_table ();
11193
11194 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11195 *dwo_unit = read_unit;
11196 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11197 gdb_assert (slot != NULL);
11198 if (*slot != NULL)
11199 {
11200 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11201 sect_offset dup_sect_off = dup_cu->sect_off;
11202
11203 complaint (_("debug cu entry at offset %s is duplicate to"
11204 " the entry at offset %s, signature %s"),
11205 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11206 hex_string (dwo_unit->signature));
11207 }
11208 *slot = (void *)dwo_unit;
11209 }
11210 }
11211
11212 /* DWP file .debug_{cu,tu}_index section format:
11213 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11214
11215 DWP Version 1:
11216
11217 Both index sections have the same format, and serve to map a 64-bit
11218 signature to a set of section numbers. Each section begins with a header,
11219 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11220 indexes, and a pool of 32-bit section numbers. The index sections will be
11221 aligned at 8-byte boundaries in the file.
11222
11223 The index section header consists of:
11224
11225 V, 32 bit version number
11226 -, 32 bits unused
11227 N, 32 bit number of compilation units or type units in the index
11228 M, 32 bit number of slots in the hash table
11229
11230 Numbers are recorded using the byte order of the application binary.
11231
11232 The hash table begins at offset 16 in the section, and consists of an array
11233 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11234 order of the application binary). Unused slots in the hash table are 0.
11235 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11236
11237 The parallel table begins immediately after the hash table
11238 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11239 array of 32-bit indexes (using the byte order of the application binary),
11240 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11241 table contains a 32-bit index into the pool of section numbers. For unused
11242 hash table slots, the corresponding entry in the parallel table will be 0.
11243
11244 The pool of section numbers begins immediately following the hash table
11245 (at offset 16 + 12 * M from the beginning of the section). The pool of
11246 section numbers consists of an array of 32-bit words (using the byte order
11247 of the application binary). Each item in the array is indexed starting
11248 from 0. The hash table entry provides the index of the first section
11249 number in the set. Additional section numbers in the set follow, and the
11250 set is terminated by a 0 entry (section number 0 is not used in ELF).
11251
11252 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11253 section must be the first entry in the set, and the .debug_abbrev.dwo must
11254 be the second entry. Other members of the set may follow in any order.
11255
11256 ---
11257
11258 DWP Version 2:
11259
11260 DWP Version 2 combines all the .debug_info, etc. sections into one,
11261 and the entries in the index tables are now offsets into these sections.
11262 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11263 section.
11264
11265 Index Section Contents:
11266 Header
11267 Hash Table of Signatures dwp_hash_table.hash_table
11268 Parallel Table of Indices dwp_hash_table.unit_table
11269 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11270 Table of Section Sizes dwp_hash_table.v2.sizes
11271
11272 The index section header consists of:
11273
11274 V, 32 bit version number
11275 L, 32 bit number of columns in the table of section offsets
11276 N, 32 bit number of compilation units or type units in the index
11277 M, 32 bit number of slots in the hash table
11278
11279 Numbers are recorded using the byte order of the application binary.
11280
11281 The hash table has the same format as version 1.
11282 The parallel table of indices has the same format as version 1,
11283 except that the entries are origin-1 indices into the table of sections
11284 offsets and the table of section sizes.
11285
11286 The table of offsets begins immediately following the parallel table
11287 (at offset 16 + 12 * M from the beginning of the section). The table is
11288 a two-dimensional array of 32-bit words (using the byte order of the
11289 application binary), with L columns and N+1 rows, in row-major order.
11290 Each row in the array is indexed starting from 0. The first row provides
11291 a key to the remaining rows: each column in this row provides an identifier
11292 for a debug section, and the offsets in the same column of subsequent rows
11293 refer to that section. The section identifiers are:
11294
11295 DW_SECT_INFO 1 .debug_info.dwo
11296 DW_SECT_TYPES 2 .debug_types.dwo
11297 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11298 DW_SECT_LINE 4 .debug_line.dwo
11299 DW_SECT_LOC 5 .debug_loc.dwo
11300 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11301 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11302 DW_SECT_MACRO 8 .debug_macro.dwo
11303
11304 The offsets provided by the CU and TU index sections are the base offsets
11305 for the contributions made by each CU or TU to the corresponding section
11306 in the package file. Each CU and TU header contains an abbrev_offset
11307 field, used to find the abbreviations table for that CU or TU within the
11308 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11309 be interpreted as relative to the base offset given in the index section.
11310 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11311 should be interpreted as relative to the base offset for .debug_line.dwo,
11312 and offsets into other debug sections obtained from DWARF attributes should
11313 also be interpreted as relative to the corresponding base offset.
11314
11315 The table of sizes begins immediately following the table of offsets.
11316 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11317 with L columns and N rows, in row-major order. Each row in the array is
11318 indexed starting from 1 (row 0 is shared by the two tables).
11319
11320 ---
11321
11322 Hash table lookup is handled the same in version 1 and 2:
11323
11324 We assume that N and M will not exceed 2^32 - 1.
11325 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11326
11327 Given a 64-bit compilation unit signature or a type signature S, an entry
11328 in the hash table is located as follows:
11329
11330 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11331 the low-order k bits all set to 1.
11332
11333 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11334
11335 3) If the hash table entry at index H matches the signature, use that
11336 entry. If the hash table entry at index H is unused (all zeroes),
11337 terminate the search: the signature is not present in the table.
11338
11339 4) Let H = (H + H') modulo M. Repeat at Step 3.
11340
11341 Because M > N and H' and M are relatively prime, the search is guaranteed
11342 to stop at an unused slot or find the match. */
11343
11344 /* Create a hash table to map DWO IDs to their CU/TU entry in
11345 .debug_{info,types}.dwo in DWP_FILE.
11346 Returns NULL if there isn't one.
11347 Note: This function processes DWP files only, not DWO files. */
11348
11349 static struct dwp_hash_table *
11350 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11351 struct dwp_file *dwp_file, int is_debug_types)
11352 {
11353 struct objfile *objfile = dwarf2_per_objfile->objfile;
11354 bfd *dbfd = dwp_file->dbfd.get ();
11355 const gdb_byte *index_ptr, *index_end;
11356 struct dwarf2_section_info *index;
11357 uint32_t version, nr_columns, nr_units, nr_slots;
11358 struct dwp_hash_table *htab;
11359
11360 if (is_debug_types)
11361 index = &dwp_file->sections.tu_index;
11362 else
11363 index = &dwp_file->sections.cu_index;
11364
11365 if (index->empty ())
11366 return NULL;
11367 index->read (objfile);
11368
11369 index_ptr = index->buffer;
11370 index_end = index_ptr + index->size;
11371
11372 version = read_4_bytes (dbfd, index_ptr);
11373 index_ptr += 4;
11374 if (version == 2)
11375 nr_columns = read_4_bytes (dbfd, index_ptr);
11376 else
11377 nr_columns = 0;
11378 index_ptr += 4;
11379 nr_units = read_4_bytes (dbfd, index_ptr);
11380 index_ptr += 4;
11381 nr_slots = read_4_bytes (dbfd, index_ptr);
11382 index_ptr += 4;
11383
11384 if (version != 1 && version != 2)
11385 {
11386 error (_("Dwarf Error: unsupported DWP file version (%s)"
11387 " [in module %s]"),
11388 pulongest (version), dwp_file->name);
11389 }
11390 if (nr_slots != (nr_slots & -nr_slots))
11391 {
11392 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11393 " is not power of 2 [in module %s]"),
11394 pulongest (nr_slots), dwp_file->name);
11395 }
11396
11397 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11398 htab->version = version;
11399 htab->nr_columns = nr_columns;
11400 htab->nr_units = nr_units;
11401 htab->nr_slots = nr_slots;
11402 htab->hash_table = index_ptr;
11403 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11404
11405 /* Exit early if the table is empty. */
11406 if (nr_slots == 0 || nr_units == 0
11407 || (version == 2 && nr_columns == 0))
11408 {
11409 /* All must be zero. */
11410 if (nr_slots != 0 || nr_units != 0
11411 || (version == 2 && nr_columns != 0))
11412 {
11413 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11414 " all zero [in modules %s]"),
11415 dwp_file->name);
11416 }
11417 return htab;
11418 }
11419
11420 if (version == 1)
11421 {
11422 htab->section_pool.v1.indices =
11423 htab->unit_table + sizeof (uint32_t) * nr_slots;
11424 /* It's harder to decide whether the section is too small in v1.
11425 V1 is deprecated anyway so we punt. */
11426 }
11427 else
11428 {
11429 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11430 int *ids = htab->section_pool.v2.section_ids;
11431 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11432 /* Reverse map for error checking. */
11433 int ids_seen[DW_SECT_MAX + 1];
11434 int i;
11435
11436 if (nr_columns < 2)
11437 {
11438 error (_("Dwarf Error: bad DWP hash table, too few columns"
11439 " in section table [in module %s]"),
11440 dwp_file->name);
11441 }
11442 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11443 {
11444 error (_("Dwarf Error: bad DWP hash table, too many columns"
11445 " in section table [in module %s]"),
11446 dwp_file->name);
11447 }
11448 memset (ids, 255, sizeof_ids);
11449 memset (ids_seen, 255, sizeof (ids_seen));
11450 for (i = 0; i < nr_columns; ++i)
11451 {
11452 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11453
11454 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11455 {
11456 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11457 " in section table [in module %s]"),
11458 id, dwp_file->name);
11459 }
11460 if (ids_seen[id] != -1)
11461 {
11462 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11463 " id %d in section table [in module %s]"),
11464 id, dwp_file->name);
11465 }
11466 ids_seen[id] = i;
11467 ids[i] = id;
11468 }
11469 /* Must have exactly one info or types section. */
11470 if (((ids_seen[DW_SECT_INFO] != -1)
11471 + (ids_seen[DW_SECT_TYPES] != -1))
11472 != 1)
11473 {
11474 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11475 " DWO info/types section [in module %s]"),
11476 dwp_file->name);
11477 }
11478 /* Must have an abbrev section. */
11479 if (ids_seen[DW_SECT_ABBREV] == -1)
11480 {
11481 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11482 " section [in module %s]"),
11483 dwp_file->name);
11484 }
11485 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11486 htab->section_pool.v2.sizes =
11487 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11488 * nr_units * nr_columns);
11489 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11490 * nr_units * nr_columns))
11491 > index_end)
11492 {
11493 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11494 " [in module %s]"),
11495 dwp_file->name);
11496 }
11497 }
11498
11499 return htab;
11500 }
11501
11502 /* Update SECTIONS with the data from SECTP.
11503
11504 This function is like the other "locate" section routines that are
11505 passed to bfd_map_over_sections, but in this context the sections to
11506 read comes from the DWP V1 hash table, not the full ELF section table.
11507
11508 The result is non-zero for success, or zero if an error was found. */
11509
11510 static int
11511 locate_v1_virtual_dwo_sections (asection *sectp,
11512 struct virtual_v1_dwo_sections *sections)
11513 {
11514 const struct dwop_section_names *names = &dwop_section_names;
11515
11516 if (section_is_p (sectp->name, &names->abbrev_dwo))
11517 {
11518 /* There can be only one. */
11519 if (sections->abbrev.s.section != NULL)
11520 return 0;
11521 sections->abbrev.s.section = sectp;
11522 sections->abbrev.size = bfd_section_size (sectp);
11523 }
11524 else if (section_is_p (sectp->name, &names->info_dwo)
11525 || section_is_p (sectp->name, &names->types_dwo))
11526 {
11527 /* There can be only one. */
11528 if (sections->info_or_types.s.section != NULL)
11529 return 0;
11530 sections->info_or_types.s.section = sectp;
11531 sections->info_or_types.size = bfd_section_size (sectp);
11532 }
11533 else if (section_is_p (sectp->name, &names->line_dwo))
11534 {
11535 /* There can be only one. */
11536 if (sections->line.s.section != NULL)
11537 return 0;
11538 sections->line.s.section = sectp;
11539 sections->line.size = bfd_section_size (sectp);
11540 }
11541 else if (section_is_p (sectp->name, &names->loc_dwo))
11542 {
11543 /* There can be only one. */
11544 if (sections->loc.s.section != NULL)
11545 return 0;
11546 sections->loc.s.section = sectp;
11547 sections->loc.size = bfd_section_size (sectp);
11548 }
11549 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11550 {
11551 /* There can be only one. */
11552 if (sections->macinfo.s.section != NULL)
11553 return 0;
11554 sections->macinfo.s.section = sectp;
11555 sections->macinfo.size = bfd_section_size (sectp);
11556 }
11557 else if (section_is_p (sectp->name, &names->macro_dwo))
11558 {
11559 /* There can be only one. */
11560 if (sections->macro.s.section != NULL)
11561 return 0;
11562 sections->macro.s.section = sectp;
11563 sections->macro.size = bfd_section_size (sectp);
11564 }
11565 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11566 {
11567 /* There can be only one. */
11568 if (sections->str_offsets.s.section != NULL)
11569 return 0;
11570 sections->str_offsets.s.section = sectp;
11571 sections->str_offsets.size = bfd_section_size (sectp);
11572 }
11573 else
11574 {
11575 /* No other kind of section is valid. */
11576 return 0;
11577 }
11578
11579 return 1;
11580 }
11581
11582 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11583 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11584 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11585 This is for DWP version 1 files. */
11586
11587 static struct dwo_unit *
11588 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11589 struct dwp_file *dwp_file,
11590 uint32_t unit_index,
11591 const char *comp_dir,
11592 ULONGEST signature, int is_debug_types)
11593 {
11594 struct objfile *objfile = dwarf2_per_objfile->objfile;
11595 const struct dwp_hash_table *dwp_htab =
11596 is_debug_types ? dwp_file->tus : dwp_file->cus;
11597 bfd *dbfd = dwp_file->dbfd.get ();
11598 const char *kind = is_debug_types ? "TU" : "CU";
11599 struct dwo_file *dwo_file;
11600 struct dwo_unit *dwo_unit;
11601 struct virtual_v1_dwo_sections sections;
11602 void **dwo_file_slot;
11603 int i;
11604
11605 gdb_assert (dwp_file->version == 1);
11606
11607 if (dwarf_read_debug)
11608 {
11609 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11610 kind,
11611 pulongest (unit_index), hex_string (signature),
11612 dwp_file->name);
11613 }
11614
11615 /* Fetch the sections of this DWO unit.
11616 Put a limit on the number of sections we look for so that bad data
11617 doesn't cause us to loop forever. */
11618
11619 #define MAX_NR_V1_DWO_SECTIONS \
11620 (1 /* .debug_info or .debug_types */ \
11621 + 1 /* .debug_abbrev */ \
11622 + 1 /* .debug_line */ \
11623 + 1 /* .debug_loc */ \
11624 + 1 /* .debug_str_offsets */ \
11625 + 1 /* .debug_macro or .debug_macinfo */ \
11626 + 1 /* trailing zero */)
11627
11628 memset (&sections, 0, sizeof (sections));
11629
11630 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11631 {
11632 asection *sectp;
11633 uint32_t section_nr =
11634 read_4_bytes (dbfd,
11635 dwp_htab->section_pool.v1.indices
11636 + (unit_index + i) * sizeof (uint32_t));
11637
11638 if (section_nr == 0)
11639 break;
11640 if (section_nr >= dwp_file->num_sections)
11641 {
11642 error (_("Dwarf Error: bad DWP hash table, section number too large"
11643 " [in module %s]"),
11644 dwp_file->name);
11645 }
11646
11647 sectp = dwp_file->elf_sections[section_nr];
11648 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11649 {
11650 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11651 " [in module %s]"),
11652 dwp_file->name);
11653 }
11654 }
11655
11656 if (i < 2
11657 || sections.info_or_types.empty ()
11658 || sections.abbrev.empty ())
11659 {
11660 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11661 " [in module %s]"),
11662 dwp_file->name);
11663 }
11664 if (i == MAX_NR_V1_DWO_SECTIONS)
11665 {
11666 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11667 " [in module %s]"),
11668 dwp_file->name);
11669 }
11670
11671 /* It's easier for the rest of the code if we fake a struct dwo_file and
11672 have dwo_unit "live" in that. At least for now.
11673
11674 The DWP file can be made up of a random collection of CUs and TUs.
11675 However, for each CU + set of TUs that came from the same original DWO
11676 file, we can combine them back into a virtual DWO file to save space
11677 (fewer struct dwo_file objects to allocate). Remember that for really
11678 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11679
11680 std::string virtual_dwo_name =
11681 string_printf ("virtual-dwo/%d-%d-%d-%d",
11682 sections.abbrev.get_id (),
11683 sections.line.get_id (),
11684 sections.loc.get_id (),
11685 sections.str_offsets.get_id ());
11686 /* Can we use an existing virtual DWO file? */
11687 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11688 virtual_dwo_name.c_str (),
11689 comp_dir);
11690 /* Create one if necessary. */
11691 if (*dwo_file_slot == NULL)
11692 {
11693 if (dwarf_read_debug)
11694 {
11695 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11696 virtual_dwo_name.c_str ());
11697 }
11698 dwo_file = new struct dwo_file;
11699 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11700 dwo_file->comp_dir = comp_dir;
11701 dwo_file->sections.abbrev = sections.abbrev;
11702 dwo_file->sections.line = sections.line;
11703 dwo_file->sections.loc = sections.loc;
11704 dwo_file->sections.macinfo = sections.macinfo;
11705 dwo_file->sections.macro = sections.macro;
11706 dwo_file->sections.str_offsets = sections.str_offsets;
11707 /* The "str" section is global to the entire DWP file. */
11708 dwo_file->sections.str = dwp_file->sections.str;
11709 /* The info or types section is assigned below to dwo_unit,
11710 there's no need to record it in dwo_file.
11711 Also, we can't simply record type sections in dwo_file because
11712 we record a pointer into the vector in dwo_unit. As we collect more
11713 types we'll grow the vector and eventually have to reallocate space
11714 for it, invalidating all copies of pointers into the previous
11715 contents. */
11716 *dwo_file_slot = dwo_file;
11717 }
11718 else
11719 {
11720 if (dwarf_read_debug)
11721 {
11722 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11723 virtual_dwo_name.c_str ());
11724 }
11725 dwo_file = (struct dwo_file *) *dwo_file_slot;
11726 }
11727
11728 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11729 dwo_unit->dwo_file = dwo_file;
11730 dwo_unit->signature = signature;
11731 dwo_unit->section =
11732 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11733 *dwo_unit->section = sections.info_or_types;
11734 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11735
11736 return dwo_unit;
11737 }
11738
11739 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11740 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11741 piece within that section used by a TU/CU, return a virtual section
11742 of just that piece. */
11743
11744 static struct dwarf2_section_info
11745 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11746 struct dwarf2_section_info *section,
11747 bfd_size_type offset, bfd_size_type size)
11748 {
11749 struct dwarf2_section_info result;
11750 asection *sectp;
11751
11752 gdb_assert (section != NULL);
11753 gdb_assert (!section->is_virtual);
11754
11755 memset (&result, 0, sizeof (result));
11756 result.s.containing_section = section;
11757 result.is_virtual = true;
11758
11759 if (size == 0)
11760 return result;
11761
11762 sectp = section->get_bfd_section ();
11763
11764 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11765 bounds of the real section. This is a pretty-rare event, so just
11766 flag an error (easier) instead of a warning and trying to cope. */
11767 if (sectp == NULL
11768 || offset + size > bfd_section_size (sectp))
11769 {
11770 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11771 " in section %s [in module %s]"),
11772 sectp ? bfd_section_name (sectp) : "<unknown>",
11773 objfile_name (dwarf2_per_objfile->objfile));
11774 }
11775
11776 result.virtual_offset = offset;
11777 result.size = size;
11778 return result;
11779 }
11780
11781 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11782 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11783 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11784 This is for DWP version 2 files. */
11785
11786 static struct dwo_unit *
11787 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11788 struct dwp_file *dwp_file,
11789 uint32_t unit_index,
11790 const char *comp_dir,
11791 ULONGEST signature, int is_debug_types)
11792 {
11793 struct objfile *objfile = dwarf2_per_objfile->objfile;
11794 const struct dwp_hash_table *dwp_htab =
11795 is_debug_types ? dwp_file->tus : dwp_file->cus;
11796 bfd *dbfd = dwp_file->dbfd.get ();
11797 const char *kind = is_debug_types ? "TU" : "CU";
11798 struct dwo_file *dwo_file;
11799 struct dwo_unit *dwo_unit;
11800 struct virtual_v2_dwo_sections sections;
11801 void **dwo_file_slot;
11802 int i;
11803
11804 gdb_assert (dwp_file->version == 2);
11805
11806 if (dwarf_read_debug)
11807 {
11808 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11809 kind,
11810 pulongest (unit_index), hex_string (signature),
11811 dwp_file->name);
11812 }
11813
11814 /* Fetch the section offsets of this DWO unit. */
11815
11816 memset (&sections, 0, sizeof (sections));
11817
11818 for (i = 0; i < dwp_htab->nr_columns; ++i)
11819 {
11820 uint32_t offset = read_4_bytes (dbfd,
11821 dwp_htab->section_pool.v2.offsets
11822 + (((unit_index - 1) * dwp_htab->nr_columns
11823 + i)
11824 * sizeof (uint32_t)));
11825 uint32_t size = read_4_bytes (dbfd,
11826 dwp_htab->section_pool.v2.sizes
11827 + (((unit_index - 1) * dwp_htab->nr_columns
11828 + i)
11829 * sizeof (uint32_t)));
11830
11831 switch (dwp_htab->section_pool.v2.section_ids[i])
11832 {
11833 case DW_SECT_INFO:
11834 case DW_SECT_TYPES:
11835 sections.info_or_types_offset = offset;
11836 sections.info_or_types_size = size;
11837 break;
11838 case DW_SECT_ABBREV:
11839 sections.abbrev_offset = offset;
11840 sections.abbrev_size = size;
11841 break;
11842 case DW_SECT_LINE:
11843 sections.line_offset = offset;
11844 sections.line_size = size;
11845 break;
11846 case DW_SECT_LOC:
11847 sections.loc_offset = offset;
11848 sections.loc_size = size;
11849 break;
11850 case DW_SECT_STR_OFFSETS:
11851 sections.str_offsets_offset = offset;
11852 sections.str_offsets_size = size;
11853 break;
11854 case DW_SECT_MACINFO:
11855 sections.macinfo_offset = offset;
11856 sections.macinfo_size = size;
11857 break;
11858 case DW_SECT_MACRO:
11859 sections.macro_offset = offset;
11860 sections.macro_size = size;
11861 break;
11862 }
11863 }
11864
11865 /* It's easier for the rest of the code if we fake a struct dwo_file and
11866 have dwo_unit "live" in that. At least for now.
11867
11868 The DWP file can be made up of a random collection of CUs and TUs.
11869 However, for each CU + set of TUs that came from the same original DWO
11870 file, we can combine them back into a virtual DWO file to save space
11871 (fewer struct dwo_file objects to allocate). Remember that for really
11872 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11873
11874 std::string virtual_dwo_name =
11875 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11876 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11877 (long) (sections.line_size ? sections.line_offset : 0),
11878 (long) (sections.loc_size ? sections.loc_offset : 0),
11879 (long) (sections.str_offsets_size
11880 ? sections.str_offsets_offset : 0));
11881 /* Can we use an existing virtual DWO file? */
11882 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11883 virtual_dwo_name.c_str (),
11884 comp_dir);
11885 /* Create one if necessary. */
11886 if (*dwo_file_slot == NULL)
11887 {
11888 if (dwarf_read_debug)
11889 {
11890 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11891 virtual_dwo_name.c_str ());
11892 }
11893 dwo_file = new struct dwo_file;
11894 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11895 dwo_file->comp_dir = comp_dir;
11896 dwo_file->sections.abbrev =
11897 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11898 sections.abbrev_offset, sections.abbrev_size);
11899 dwo_file->sections.line =
11900 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11901 sections.line_offset, sections.line_size);
11902 dwo_file->sections.loc =
11903 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11904 sections.loc_offset, sections.loc_size);
11905 dwo_file->sections.macinfo =
11906 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11907 sections.macinfo_offset, sections.macinfo_size);
11908 dwo_file->sections.macro =
11909 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11910 sections.macro_offset, sections.macro_size);
11911 dwo_file->sections.str_offsets =
11912 create_dwp_v2_section (dwarf2_per_objfile,
11913 &dwp_file->sections.str_offsets,
11914 sections.str_offsets_offset,
11915 sections.str_offsets_size);
11916 /* The "str" section is global to the entire DWP file. */
11917 dwo_file->sections.str = dwp_file->sections.str;
11918 /* The info or types section is assigned below to dwo_unit,
11919 there's no need to record it in dwo_file.
11920 Also, we can't simply record type sections in dwo_file because
11921 we record a pointer into the vector in dwo_unit. As we collect more
11922 types we'll grow the vector and eventually have to reallocate space
11923 for it, invalidating all copies of pointers into the previous
11924 contents. */
11925 *dwo_file_slot = dwo_file;
11926 }
11927 else
11928 {
11929 if (dwarf_read_debug)
11930 {
11931 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11932 virtual_dwo_name.c_str ());
11933 }
11934 dwo_file = (struct dwo_file *) *dwo_file_slot;
11935 }
11936
11937 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11938 dwo_unit->dwo_file = dwo_file;
11939 dwo_unit->signature = signature;
11940 dwo_unit->section =
11941 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11942 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11943 is_debug_types
11944 ? &dwp_file->sections.types
11945 : &dwp_file->sections.info,
11946 sections.info_or_types_offset,
11947 sections.info_or_types_size);
11948 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11949
11950 return dwo_unit;
11951 }
11952
11953 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11954 Returns NULL if the signature isn't found. */
11955
11956 static struct dwo_unit *
11957 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11958 struct dwp_file *dwp_file, const char *comp_dir,
11959 ULONGEST signature, int is_debug_types)
11960 {
11961 const struct dwp_hash_table *dwp_htab =
11962 is_debug_types ? dwp_file->tus : dwp_file->cus;
11963 bfd *dbfd = dwp_file->dbfd.get ();
11964 uint32_t mask = dwp_htab->nr_slots - 1;
11965 uint32_t hash = signature & mask;
11966 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11967 unsigned int i;
11968 void **slot;
11969 struct dwo_unit find_dwo_cu;
11970
11971 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11972 find_dwo_cu.signature = signature;
11973 slot = htab_find_slot (is_debug_types
11974 ? dwp_file->loaded_tus.get ()
11975 : dwp_file->loaded_cus.get (),
11976 &find_dwo_cu, INSERT);
11977
11978 if (*slot != NULL)
11979 return (struct dwo_unit *) *slot;
11980
11981 /* Use a for loop so that we don't loop forever on bad debug info. */
11982 for (i = 0; i < dwp_htab->nr_slots; ++i)
11983 {
11984 ULONGEST signature_in_table;
11985
11986 signature_in_table =
11987 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11988 if (signature_in_table == signature)
11989 {
11990 uint32_t unit_index =
11991 read_4_bytes (dbfd,
11992 dwp_htab->unit_table + hash * sizeof (uint32_t));
11993
11994 if (dwp_file->version == 1)
11995 {
11996 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11997 dwp_file, unit_index,
11998 comp_dir, signature,
11999 is_debug_types);
12000 }
12001 else
12002 {
12003 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12004 dwp_file, unit_index,
12005 comp_dir, signature,
12006 is_debug_types);
12007 }
12008 return (struct dwo_unit *) *slot;
12009 }
12010 if (signature_in_table == 0)
12011 return NULL;
12012 hash = (hash + hash2) & mask;
12013 }
12014
12015 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12016 " [in module %s]"),
12017 dwp_file->name);
12018 }
12019
12020 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12021 Open the file specified by FILE_NAME and hand it off to BFD for
12022 preliminary analysis. Return a newly initialized bfd *, which
12023 includes a canonicalized copy of FILE_NAME.
12024 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12025 SEARCH_CWD is true if the current directory is to be searched.
12026 It will be searched before debug-file-directory.
12027 If successful, the file is added to the bfd include table of the
12028 objfile's bfd (see gdb_bfd_record_inclusion).
12029 If unable to find/open the file, return NULL.
12030 NOTE: This function is derived from symfile_bfd_open. */
12031
12032 static gdb_bfd_ref_ptr
12033 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12034 const char *file_name, int is_dwp, int search_cwd)
12035 {
12036 int desc;
12037 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12038 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12039 to debug_file_directory. */
12040 const char *search_path;
12041 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12042
12043 gdb::unique_xmalloc_ptr<char> search_path_holder;
12044 if (search_cwd)
12045 {
12046 if (*debug_file_directory != '\0')
12047 {
12048 search_path_holder.reset (concat (".", dirname_separator_string,
12049 debug_file_directory,
12050 (char *) NULL));
12051 search_path = search_path_holder.get ();
12052 }
12053 else
12054 search_path = ".";
12055 }
12056 else
12057 search_path = debug_file_directory;
12058
12059 openp_flags flags = OPF_RETURN_REALPATH;
12060 if (is_dwp)
12061 flags |= OPF_SEARCH_IN_PATH;
12062
12063 gdb::unique_xmalloc_ptr<char> absolute_name;
12064 desc = openp (search_path, flags, file_name,
12065 O_RDONLY | O_BINARY, &absolute_name);
12066 if (desc < 0)
12067 return NULL;
12068
12069 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12070 gnutarget, desc));
12071 if (sym_bfd == NULL)
12072 return NULL;
12073 bfd_set_cacheable (sym_bfd.get (), 1);
12074
12075 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12076 return NULL;
12077
12078 /* Success. Record the bfd as having been included by the objfile's bfd.
12079 This is important because things like demangled_names_hash lives in the
12080 objfile's per_bfd space and may have references to things like symbol
12081 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12082 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12083
12084 return sym_bfd;
12085 }
12086
12087 /* Try to open DWO file FILE_NAME.
12088 COMP_DIR is the DW_AT_comp_dir attribute.
12089 The result is the bfd handle of the file.
12090 If there is a problem finding or opening the file, return NULL.
12091 Upon success, the canonicalized path of the file is stored in the bfd,
12092 same as symfile_bfd_open. */
12093
12094 static gdb_bfd_ref_ptr
12095 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12096 const char *file_name, const char *comp_dir)
12097 {
12098 if (IS_ABSOLUTE_PATH (file_name))
12099 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12100 0 /*is_dwp*/, 0 /*search_cwd*/);
12101
12102 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12103
12104 if (comp_dir != NULL)
12105 {
12106 gdb::unique_xmalloc_ptr<char> path_to_try
12107 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12108
12109 /* NOTE: If comp_dir is a relative path, this will also try the
12110 search path, which seems useful. */
12111 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12112 path_to_try.get (),
12113 0 /*is_dwp*/,
12114 1 /*search_cwd*/));
12115 if (abfd != NULL)
12116 return abfd;
12117 }
12118
12119 /* That didn't work, try debug-file-directory, which, despite its name,
12120 is a list of paths. */
12121
12122 if (*debug_file_directory == '\0')
12123 return NULL;
12124
12125 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12126 0 /*is_dwp*/, 1 /*search_cwd*/);
12127 }
12128
12129 /* This function is mapped across the sections and remembers the offset and
12130 size of each of the DWO debugging sections we are interested in. */
12131
12132 static void
12133 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12134 {
12135 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12136 const struct dwop_section_names *names = &dwop_section_names;
12137
12138 if (section_is_p (sectp->name, &names->abbrev_dwo))
12139 {
12140 dwo_sections->abbrev.s.section = sectp;
12141 dwo_sections->abbrev.size = bfd_section_size (sectp);
12142 }
12143 else if (section_is_p (sectp->name, &names->info_dwo))
12144 {
12145 dwo_sections->info.s.section = sectp;
12146 dwo_sections->info.size = bfd_section_size (sectp);
12147 }
12148 else if (section_is_p (sectp->name, &names->line_dwo))
12149 {
12150 dwo_sections->line.s.section = sectp;
12151 dwo_sections->line.size = bfd_section_size (sectp);
12152 }
12153 else if (section_is_p (sectp->name, &names->loc_dwo))
12154 {
12155 dwo_sections->loc.s.section = sectp;
12156 dwo_sections->loc.size = bfd_section_size (sectp);
12157 }
12158 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12159 {
12160 dwo_sections->macinfo.s.section = sectp;
12161 dwo_sections->macinfo.size = bfd_section_size (sectp);
12162 }
12163 else if (section_is_p (sectp->name, &names->macro_dwo))
12164 {
12165 dwo_sections->macro.s.section = sectp;
12166 dwo_sections->macro.size = bfd_section_size (sectp);
12167 }
12168 else if (section_is_p (sectp->name, &names->str_dwo))
12169 {
12170 dwo_sections->str.s.section = sectp;
12171 dwo_sections->str.size = bfd_section_size (sectp);
12172 }
12173 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12174 {
12175 dwo_sections->str_offsets.s.section = sectp;
12176 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12177 }
12178 else if (section_is_p (sectp->name, &names->types_dwo))
12179 {
12180 struct dwarf2_section_info type_section;
12181
12182 memset (&type_section, 0, sizeof (type_section));
12183 type_section.s.section = sectp;
12184 type_section.size = bfd_section_size (sectp);
12185 dwo_sections->types.push_back (type_section);
12186 }
12187 }
12188
12189 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12190 by PER_CU. This is for the non-DWP case.
12191 The result is NULL if DWO_NAME can't be found. */
12192
12193 static struct dwo_file *
12194 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12195 const char *dwo_name, const char *comp_dir)
12196 {
12197 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12198
12199 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12200 if (dbfd == NULL)
12201 {
12202 if (dwarf_read_debug)
12203 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12204 return NULL;
12205 }
12206
12207 dwo_file_up dwo_file (new struct dwo_file);
12208 dwo_file->dwo_name = dwo_name;
12209 dwo_file->comp_dir = comp_dir;
12210 dwo_file->dbfd = std::move (dbfd);
12211
12212 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12213 &dwo_file->sections);
12214
12215 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12216 dwo_file->sections.info, dwo_file->cus);
12217
12218 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12219 dwo_file->sections.types, dwo_file->tus);
12220
12221 if (dwarf_read_debug)
12222 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12223
12224 return dwo_file.release ();
12225 }
12226
12227 /* This function is mapped across the sections and remembers the offset and
12228 size of each of the DWP debugging sections common to version 1 and 2 that
12229 we are interested in. */
12230
12231 static void
12232 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12233 void *dwp_file_ptr)
12234 {
12235 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12236 const struct dwop_section_names *names = &dwop_section_names;
12237 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12238
12239 /* Record the ELF section number for later lookup: this is what the
12240 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12241 gdb_assert (elf_section_nr < dwp_file->num_sections);
12242 dwp_file->elf_sections[elf_section_nr] = sectp;
12243
12244 /* Look for specific sections that we need. */
12245 if (section_is_p (sectp->name, &names->str_dwo))
12246 {
12247 dwp_file->sections.str.s.section = sectp;
12248 dwp_file->sections.str.size = bfd_section_size (sectp);
12249 }
12250 else if (section_is_p (sectp->name, &names->cu_index))
12251 {
12252 dwp_file->sections.cu_index.s.section = sectp;
12253 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12254 }
12255 else if (section_is_p (sectp->name, &names->tu_index))
12256 {
12257 dwp_file->sections.tu_index.s.section = sectp;
12258 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12259 }
12260 }
12261
12262 /* This function is mapped across the sections and remembers the offset and
12263 size of each of the DWP version 2 debugging sections that we are interested
12264 in. This is split into a separate function because we don't know if we
12265 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12266
12267 static void
12268 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12269 {
12270 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12271 const struct dwop_section_names *names = &dwop_section_names;
12272 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12273
12274 /* Record the ELF section number for later lookup: this is what the
12275 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12276 gdb_assert (elf_section_nr < dwp_file->num_sections);
12277 dwp_file->elf_sections[elf_section_nr] = sectp;
12278
12279 /* Look for specific sections that we need. */
12280 if (section_is_p (sectp->name, &names->abbrev_dwo))
12281 {
12282 dwp_file->sections.abbrev.s.section = sectp;
12283 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12284 }
12285 else if (section_is_p (sectp->name, &names->info_dwo))
12286 {
12287 dwp_file->sections.info.s.section = sectp;
12288 dwp_file->sections.info.size = bfd_section_size (sectp);
12289 }
12290 else if (section_is_p (sectp->name, &names->line_dwo))
12291 {
12292 dwp_file->sections.line.s.section = sectp;
12293 dwp_file->sections.line.size = bfd_section_size (sectp);
12294 }
12295 else if (section_is_p (sectp->name, &names->loc_dwo))
12296 {
12297 dwp_file->sections.loc.s.section = sectp;
12298 dwp_file->sections.loc.size = bfd_section_size (sectp);
12299 }
12300 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12301 {
12302 dwp_file->sections.macinfo.s.section = sectp;
12303 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12304 }
12305 else if (section_is_p (sectp->name, &names->macro_dwo))
12306 {
12307 dwp_file->sections.macro.s.section = sectp;
12308 dwp_file->sections.macro.size = bfd_section_size (sectp);
12309 }
12310 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12311 {
12312 dwp_file->sections.str_offsets.s.section = sectp;
12313 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12314 }
12315 else if (section_is_p (sectp->name, &names->types_dwo))
12316 {
12317 dwp_file->sections.types.s.section = sectp;
12318 dwp_file->sections.types.size = bfd_section_size (sectp);
12319 }
12320 }
12321
12322 /* Hash function for dwp_file loaded CUs/TUs. */
12323
12324 static hashval_t
12325 hash_dwp_loaded_cutus (const void *item)
12326 {
12327 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12328
12329 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12330 return dwo_unit->signature;
12331 }
12332
12333 /* Equality function for dwp_file loaded CUs/TUs. */
12334
12335 static int
12336 eq_dwp_loaded_cutus (const void *a, const void *b)
12337 {
12338 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12339 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12340
12341 return dua->signature == dub->signature;
12342 }
12343
12344 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12345
12346 static htab_up
12347 allocate_dwp_loaded_cutus_table ()
12348 {
12349 return htab_up (htab_create_alloc (3,
12350 hash_dwp_loaded_cutus,
12351 eq_dwp_loaded_cutus,
12352 NULL, xcalloc, xfree));
12353 }
12354
12355 /* Try to open DWP file FILE_NAME.
12356 The result is the bfd handle of the file.
12357 If there is a problem finding or opening the file, return NULL.
12358 Upon success, the canonicalized path of the file is stored in the bfd,
12359 same as symfile_bfd_open. */
12360
12361 static gdb_bfd_ref_ptr
12362 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12363 const char *file_name)
12364 {
12365 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12366 1 /*is_dwp*/,
12367 1 /*search_cwd*/));
12368 if (abfd != NULL)
12369 return abfd;
12370
12371 /* Work around upstream bug 15652.
12372 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12373 [Whether that's a "bug" is debatable, but it is getting in our way.]
12374 We have no real idea where the dwp file is, because gdb's realpath-ing
12375 of the executable's path may have discarded the needed info.
12376 [IWBN if the dwp file name was recorded in the executable, akin to
12377 .gnu_debuglink, but that doesn't exist yet.]
12378 Strip the directory from FILE_NAME and search again. */
12379 if (*debug_file_directory != '\0')
12380 {
12381 /* Don't implicitly search the current directory here.
12382 If the user wants to search "." to handle this case,
12383 it must be added to debug-file-directory. */
12384 return try_open_dwop_file (dwarf2_per_objfile,
12385 lbasename (file_name), 1 /*is_dwp*/,
12386 0 /*search_cwd*/);
12387 }
12388
12389 return NULL;
12390 }
12391
12392 /* Initialize the use of the DWP file for the current objfile.
12393 By convention the name of the DWP file is ${objfile}.dwp.
12394 The result is NULL if it can't be found. */
12395
12396 static std::unique_ptr<struct dwp_file>
12397 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12398 {
12399 struct objfile *objfile = dwarf2_per_objfile->objfile;
12400
12401 /* Try to find first .dwp for the binary file before any symbolic links
12402 resolving. */
12403
12404 /* If the objfile is a debug file, find the name of the real binary
12405 file and get the name of dwp file from there. */
12406 std::string dwp_name;
12407 if (objfile->separate_debug_objfile_backlink != NULL)
12408 {
12409 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12410 const char *backlink_basename = lbasename (backlink->original_name);
12411
12412 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12413 }
12414 else
12415 dwp_name = objfile->original_name;
12416
12417 dwp_name += ".dwp";
12418
12419 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12420 if (dbfd == NULL
12421 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12422 {
12423 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12424 dwp_name = objfile_name (objfile);
12425 dwp_name += ".dwp";
12426 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12427 }
12428
12429 if (dbfd == NULL)
12430 {
12431 if (dwarf_read_debug)
12432 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12433 return std::unique_ptr<dwp_file> ();
12434 }
12435
12436 const char *name = bfd_get_filename (dbfd.get ());
12437 std::unique_ptr<struct dwp_file> dwp_file
12438 (new struct dwp_file (name, std::move (dbfd)));
12439
12440 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12441 dwp_file->elf_sections =
12442 OBSTACK_CALLOC (&objfile->objfile_obstack,
12443 dwp_file->num_sections, asection *);
12444
12445 bfd_map_over_sections (dwp_file->dbfd.get (),
12446 dwarf2_locate_common_dwp_sections,
12447 dwp_file.get ());
12448
12449 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12450 0);
12451
12452 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12453 1);
12454
12455 /* The DWP file version is stored in the hash table. Oh well. */
12456 if (dwp_file->cus && dwp_file->tus
12457 && dwp_file->cus->version != dwp_file->tus->version)
12458 {
12459 /* Technically speaking, we should try to limp along, but this is
12460 pretty bizarre. We use pulongest here because that's the established
12461 portability solution (e.g, we cannot use %u for uint32_t). */
12462 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12463 " TU version %s [in DWP file %s]"),
12464 pulongest (dwp_file->cus->version),
12465 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12466 }
12467
12468 if (dwp_file->cus)
12469 dwp_file->version = dwp_file->cus->version;
12470 else if (dwp_file->tus)
12471 dwp_file->version = dwp_file->tus->version;
12472 else
12473 dwp_file->version = 2;
12474
12475 if (dwp_file->version == 2)
12476 bfd_map_over_sections (dwp_file->dbfd.get (),
12477 dwarf2_locate_v2_dwp_sections,
12478 dwp_file.get ());
12479
12480 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12481 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12482
12483 if (dwarf_read_debug)
12484 {
12485 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12486 fprintf_unfiltered (gdb_stdlog,
12487 " %s CUs, %s TUs\n",
12488 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12489 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12490 }
12491
12492 return dwp_file;
12493 }
12494
12495 /* Wrapper around open_and_init_dwp_file, only open it once. */
12496
12497 static struct dwp_file *
12498 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12499 {
12500 if (! dwarf2_per_objfile->dwp_checked)
12501 {
12502 dwarf2_per_objfile->dwp_file
12503 = open_and_init_dwp_file (dwarf2_per_objfile);
12504 dwarf2_per_objfile->dwp_checked = 1;
12505 }
12506 return dwarf2_per_objfile->dwp_file.get ();
12507 }
12508
12509 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12510 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12511 or in the DWP file for the objfile, referenced by THIS_UNIT.
12512 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12513 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12514
12515 This is called, for example, when wanting to read a variable with a
12516 complex location. Therefore we don't want to do file i/o for every call.
12517 Therefore we don't want to look for a DWO file on every call.
12518 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12519 then we check if we've already seen DWO_NAME, and only THEN do we check
12520 for a DWO file.
12521
12522 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12523 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12524
12525 static struct dwo_unit *
12526 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12527 const char *dwo_name, const char *comp_dir,
12528 ULONGEST signature, int is_debug_types)
12529 {
12530 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12531 struct objfile *objfile = dwarf2_per_objfile->objfile;
12532 const char *kind = is_debug_types ? "TU" : "CU";
12533 void **dwo_file_slot;
12534 struct dwo_file *dwo_file;
12535 struct dwp_file *dwp_file;
12536
12537 /* First see if there's a DWP file.
12538 If we have a DWP file but didn't find the DWO inside it, don't
12539 look for the original DWO file. It makes gdb behave differently
12540 depending on whether one is debugging in the build tree. */
12541
12542 dwp_file = get_dwp_file (dwarf2_per_objfile);
12543 if (dwp_file != NULL)
12544 {
12545 const struct dwp_hash_table *dwp_htab =
12546 is_debug_types ? dwp_file->tus : dwp_file->cus;
12547
12548 if (dwp_htab != NULL)
12549 {
12550 struct dwo_unit *dwo_cutu =
12551 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12552 signature, is_debug_types);
12553
12554 if (dwo_cutu != NULL)
12555 {
12556 if (dwarf_read_debug)
12557 {
12558 fprintf_unfiltered (gdb_stdlog,
12559 "Virtual DWO %s %s found: @%s\n",
12560 kind, hex_string (signature),
12561 host_address_to_string (dwo_cutu));
12562 }
12563 return dwo_cutu;
12564 }
12565 }
12566 }
12567 else
12568 {
12569 /* No DWP file, look for the DWO file. */
12570
12571 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12572 dwo_name, comp_dir);
12573 if (*dwo_file_slot == NULL)
12574 {
12575 /* Read in the file and build a table of the CUs/TUs it contains. */
12576 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12577 }
12578 /* NOTE: This will be NULL if unable to open the file. */
12579 dwo_file = (struct dwo_file *) *dwo_file_slot;
12580
12581 if (dwo_file != NULL)
12582 {
12583 struct dwo_unit *dwo_cutu = NULL;
12584
12585 if (is_debug_types && dwo_file->tus)
12586 {
12587 struct dwo_unit find_dwo_cutu;
12588
12589 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12590 find_dwo_cutu.signature = signature;
12591 dwo_cutu
12592 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12593 &find_dwo_cutu);
12594 }
12595 else if (!is_debug_types && dwo_file->cus)
12596 {
12597 struct dwo_unit find_dwo_cutu;
12598
12599 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12600 find_dwo_cutu.signature = signature;
12601 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12602 &find_dwo_cutu);
12603 }
12604
12605 if (dwo_cutu != NULL)
12606 {
12607 if (dwarf_read_debug)
12608 {
12609 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12610 kind, dwo_name, hex_string (signature),
12611 host_address_to_string (dwo_cutu));
12612 }
12613 return dwo_cutu;
12614 }
12615 }
12616 }
12617
12618 /* We didn't find it. This could mean a dwo_id mismatch, or
12619 someone deleted the DWO/DWP file, or the search path isn't set up
12620 correctly to find the file. */
12621
12622 if (dwarf_read_debug)
12623 {
12624 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12625 kind, dwo_name, hex_string (signature));
12626 }
12627
12628 /* This is a warning and not a complaint because it can be caused by
12629 pilot error (e.g., user accidentally deleting the DWO). */
12630 {
12631 /* Print the name of the DWP file if we looked there, helps the user
12632 better diagnose the problem. */
12633 std::string dwp_text;
12634
12635 if (dwp_file != NULL)
12636 dwp_text = string_printf (" [in DWP file %s]",
12637 lbasename (dwp_file->name));
12638
12639 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12640 " [in module %s]"),
12641 kind, dwo_name, hex_string (signature),
12642 dwp_text.c_str (),
12643 this_unit->is_debug_types ? "TU" : "CU",
12644 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12645 }
12646 return NULL;
12647 }
12648
12649 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12650 See lookup_dwo_cutu_unit for details. */
12651
12652 static struct dwo_unit *
12653 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12654 const char *dwo_name, const char *comp_dir,
12655 ULONGEST signature)
12656 {
12657 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12658 }
12659
12660 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12661 See lookup_dwo_cutu_unit for details. */
12662
12663 static struct dwo_unit *
12664 lookup_dwo_type_unit (struct signatured_type *this_tu,
12665 const char *dwo_name, const char *comp_dir)
12666 {
12667 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12668 }
12669
12670 /* Traversal function for queue_and_load_all_dwo_tus. */
12671
12672 static int
12673 queue_and_load_dwo_tu (void **slot, void *info)
12674 {
12675 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12676 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12677 ULONGEST signature = dwo_unit->signature;
12678 struct signatured_type *sig_type =
12679 lookup_dwo_signatured_type (per_cu->cu, signature);
12680
12681 if (sig_type != NULL)
12682 {
12683 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12684
12685 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12686 a real dependency of PER_CU on SIG_TYPE. That is detected later
12687 while processing PER_CU. */
12688 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12689 load_full_type_unit (sig_cu);
12690 per_cu->imported_symtabs_push (sig_cu);
12691 }
12692
12693 return 1;
12694 }
12695
12696 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12697 The DWO may have the only definition of the type, though it may not be
12698 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12699 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12700
12701 static void
12702 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12703 {
12704 struct dwo_unit *dwo_unit;
12705 struct dwo_file *dwo_file;
12706
12707 gdb_assert (!per_cu->is_debug_types);
12708 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12709 gdb_assert (per_cu->cu != NULL);
12710
12711 dwo_unit = per_cu->cu->dwo_unit;
12712 gdb_assert (dwo_unit != NULL);
12713
12714 dwo_file = dwo_unit->dwo_file;
12715 if (dwo_file->tus != NULL)
12716 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12717 per_cu);
12718 }
12719
12720 /* Read in various DIEs. */
12721
12722 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12723 Inherit only the children of the DW_AT_abstract_origin DIE not being
12724 already referenced by DW_AT_abstract_origin from the children of the
12725 current DIE. */
12726
12727 static void
12728 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12729 {
12730 struct die_info *child_die;
12731 sect_offset *offsetp;
12732 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12733 struct die_info *origin_die;
12734 /* Iterator of the ORIGIN_DIE children. */
12735 struct die_info *origin_child_die;
12736 struct attribute *attr;
12737 struct dwarf2_cu *origin_cu;
12738 struct pending **origin_previous_list_in_scope;
12739
12740 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12741 if (!attr)
12742 return;
12743
12744 /* Note that following die references may follow to a die in a
12745 different cu. */
12746
12747 origin_cu = cu;
12748 origin_die = follow_die_ref (die, attr, &origin_cu);
12749
12750 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12751 symbols in. */
12752 origin_previous_list_in_scope = origin_cu->list_in_scope;
12753 origin_cu->list_in_scope = cu->list_in_scope;
12754
12755 if (die->tag != origin_die->tag
12756 && !(die->tag == DW_TAG_inlined_subroutine
12757 && origin_die->tag == DW_TAG_subprogram))
12758 complaint (_("DIE %s and its abstract origin %s have different tags"),
12759 sect_offset_str (die->sect_off),
12760 sect_offset_str (origin_die->sect_off));
12761
12762 std::vector<sect_offset> offsets;
12763
12764 for (child_die = die->child;
12765 child_die && child_die->tag;
12766 child_die = sibling_die (child_die))
12767 {
12768 struct die_info *child_origin_die;
12769 struct dwarf2_cu *child_origin_cu;
12770
12771 /* We are trying to process concrete instance entries:
12772 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12773 it's not relevant to our analysis here. i.e. detecting DIEs that are
12774 present in the abstract instance but not referenced in the concrete
12775 one. */
12776 if (child_die->tag == DW_TAG_call_site
12777 || child_die->tag == DW_TAG_GNU_call_site)
12778 continue;
12779
12780 /* For each CHILD_DIE, find the corresponding child of
12781 ORIGIN_DIE. If there is more than one layer of
12782 DW_AT_abstract_origin, follow them all; there shouldn't be,
12783 but GCC versions at least through 4.4 generate this (GCC PR
12784 40573). */
12785 child_origin_die = child_die;
12786 child_origin_cu = cu;
12787 while (1)
12788 {
12789 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12790 child_origin_cu);
12791 if (attr == NULL)
12792 break;
12793 child_origin_die = follow_die_ref (child_origin_die, attr,
12794 &child_origin_cu);
12795 }
12796
12797 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12798 counterpart may exist. */
12799 if (child_origin_die != child_die)
12800 {
12801 if (child_die->tag != child_origin_die->tag
12802 && !(child_die->tag == DW_TAG_inlined_subroutine
12803 && child_origin_die->tag == DW_TAG_subprogram))
12804 complaint (_("Child DIE %s and its abstract origin %s have "
12805 "different tags"),
12806 sect_offset_str (child_die->sect_off),
12807 sect_offset_str (child_origin_die->sect_off));
12808 if (child_origin_die->parent != origin_die)
12809 complaint (_("Child DIE %s and its abstract origin %s have "
12810 "different parents"),
12811 sect_offset_str (child_die->sect_off),
12812 sect_offset_str (child_origin_die->sect_off));
12813 else
12814 offsets.push_back (child_origin_die->sect_off);
12815 }
12816 }
12817 std::sort (offsets.begin (), offsets.end ());
12818 sect_offset *offsets_end = offsets.data () + offsets.size ();
12819 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12820 if (offsetp[-1] == *offsetp)
12821 complaint (_("Multiple children of DIE %s refer "
12822 "to DIE %s as their abstract origin"),
12823 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12824
12825 offsetp = offsets.data ();
12826 origin_child_die = origin_die->child;
12827 while (origin_child_die && origin_child_die->tag)
12828 {
12829 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12830 while (offsetp < offsets_end
12831 && *offsetp < origin_child_die->sect_off)
12832 offsetp++;
12833 if (offsetp >= offsets_end
12834 || *offsetp > origin_child_die->sect_off)
12835 {
12836 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12837 Check whether we're already processing ORIGIN_CHILD_DIE.
12838 This can happen with mutually referenced abstract_origins.
12839 PR 16581. */
12840 if (!origin_child_die->in_process)
12841 process_die (origin_child_die, origin_cu);
12842 }
12843 origin_child_die = sibling_die (origin_child_die);
12844 }
12845 origin_cu->list_in_scope = origin_previous_list_in_scope;
12846
12847 if (cu != origin_cu)
12848 compute_delayed_physnames (origin_cu);
12849 }
12850
12851 static void
12852 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12853 {
12854 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12855 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12856 struct context_stack *newobj;
12857 CORE_ADDR lowpc;
12858 CORE_ADDR highpc;
12859 struct die_info *child_die;
12860 struct attribute *attr, *call_line, *call_file;
12861 const char *name;
12862 CORE_ADDR baseaddr;
12863 struct block *block;
12864 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12865 std::vector<struct symbol *> template_args;
12866 struct template_symbol *templ_func = NULL;
12867
12868 if (inlined_func)
12869 {
12870 /* If we do not have call site information, we can't show the
12871 caller of this inlined function. That's too confusing, so
12872 only use the scope for local variables. */
12873 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12874 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12875 if (call_line == NULL || call_file == NULL)
12876 {
12877 read_lexical_block_scope (die, cu);
12878 return;
12879 }
12880 }
12881
12882 baseaddr = objfile->text_section_offset ();
12883
12884 name = dwarf2_name (die, cu);
12885
12886 /* Ignore functions with missing or empty names. These are actually
12887 illegal according to the DWARF standard. */
12888 if (name == NULL)
12889 {
12890 complaint (_("missing name for subprogram DIE at %s"),
12891 sect_offset_str (die->sect_off));
12892 return;
12893 }
12894
12895 /* Ignore functions with missing or invalid low and high pc attributes. */
12896 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12897 <= PC_BOUNDS_INVALID)
12898 {
12899 attr = dwarf2_attr (die, DW_AT_external, cu);
12900 if (!attr || !DW_UNSND (attr))
12901 complaint (_("cannot get low and high bounds "
12902 "for subprogram DIE at %s"),
12903 sect_offset_str (die->sect_off));
12904 return;
12905 }
12906
12907 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12908 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12909
12910 /* If we have any template arguments, then we must allocate a
12911 different sort of symbol. */
12912 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12913 {
12914 if (child_die->tag == DW_TAG_template_type_param
12915 || child_die->tag == DW_TAG_template_value_param)
12916 {
12917 templ_func = allocate_template_symbol (objfile);
12918 templ_func->subclass = SYMBOL_TEMPLATE;
12919 break;
12920 }
12921 }
12922
12923 newobj = cu->get_builder ()->push_context (0, lowpc);
12924 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12925 (struct symbol *) templ_func);
12926
12927 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12928 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12929 cu->language);
12930
12931 /* If there is a location expression for DW_AT_frame_base, record
12932 it. */
12933 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12934 if (attr != nullptr)
12935 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12936
12937 /* If there is a location for the static link, record it. */
12938 newobj->static_link = NULL;
12939 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12940 if (attr != nullptr)
12941 {
12942 newobj->static_link
12943 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12944 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12945 cu->per_cu->addr_type ());
12946 }
12947
12948 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12949
12950 if (die->child != NULL)
12951 {
12952 child_die = die->child;
12953 while (child_die && child_die->tag)
12954 {
12955 if (child_die->tag == DW_TAG_template_type_param
12956 || child_die->tag == DW_TAG_template_value_param)
12957 {
12958 struct symbol *arg = new_symbol (child_die, NULL, cu);
12959
12960 if (arg != NULL)
12961 template_args.push_back (arg);
12962 }
12963 else
12964 process_die (child_die, cu);
12965 child_die = sibling_die (child_die);
12966 }
12967 }
12968
12969 inherit_abstract_dies (die, cu);
12970
12971 /* If we have a DW_AT_specification, we might need to import using
12972 directives from the context of the specification DIE. See the
12973 comment in determine_prefix. */
12974 if (cu->language == language_cplus
12975 && dwarf2_attr (die, DW_AT_specification, cu))
12976 {
12977 struct dwarf2_cu *spec_cu = cu;
12978 struct die_info *spec_die = die_specification (die, &spec_cu);
12979
12980 while (spec_die)
12981 {
12982 child_die = spec_die->child;
12983 while (child_die && child_die->tag)
12984 {
12985 if (child_die->tag == DW_TAG_imported_module)
12986 process_die (child_die, spec_cu);
12987 child_die = sibling_die (child_die);
12988 }
12989
12990 /* In some cases, GCC generates specification DIEs that
12991 themselves contain DW_AT_specification attributes. */
12992 spec_die = die_specification (spec_die, &spec_cu);
12993 }
12994 }
12995
12996 struct context_stack cstk = cu->get_builder ()->pop_context ();
12997 /* Make a block for the local symbols within. */
12998 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12999 cstk.static_link, lowpc, highpc);
13000
13001 /* For C++, set the block's scope. */
13002 if ((cu->language == language_cplus
13003 || cu->language == language_fortran
13004 || cu->language == language_d
13005 || cu->language == language_rust)
13006 && cu->processing_has_namespace_info)
13007 block_set_scope (block, determine_prefix (die, cu),
13008 &objfile->objfile_obstack);
13009
13010 /* If we have address ranges, record them. */
13011 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13012
13013 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13014
13015 /* Attach template arguments to function. */
13016 if (!template_args.empty ())
13017 {
13018 gdb_assert (templ_func != NULL);
13019
13020 templ_func->n_template_arguments = template_args.size ();
13021 templ_func->template_arguments
13022 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13023 templ_func->n_template_arguments);
13024 memcpy (templ_func->template_arguments,
13025 template_args.data (),
13026 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13027
13028 /* Make sure that the symtab is set on the new symbols. Even
13029 though they don't appear in this symtab directly, other parts
13030 of gdb assume that symbols do, and this is reasonably
13031 true. */
13032 for (symbol *sym : template_args)
13033 symbol_set_symtab (sym, symbol_symtab (templ_func));
13034 }
13035
13036 /* In C++, we can have functions nested inside functions (e.g., when
13037 a function declares a class that has methods). This means that
13038 when we finish processing a function scope, we may need to go
13039 back to building a containing block's symbol lists. */
13040 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13041 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13042
13043 /* If we've finished processing a top-level function, subsequent
13044 symbols go in the file symbol list. */
13045 if (cu->get_builder ()->outermost_context_p ())
13046 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13047 }
13048
13049 /* Process all the DIES contained within a lexical block scope. Start
13050 a new scope, process the dies, and then close the scope. */
13051
13052 static void
13053 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13054 {
13055 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13056 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13057 CORE_ADDR lowpc, highpc;
13058 struct die_info *child_die;
13059 CORE_ADDR baseaddr;
13060
13061 baseaddr = objfile->text_section_offset ();
13062
13063 /* Ignore blocks with missing or invalid low and high pc attributes. */
13064 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13065 as multiple lexical blocks? Handling children in a sane way would
13066 be nasty. Might be easier to properly extend generic blocks to
13067 describe ranges. */
13068 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13069 {
13070 case PC_BOUNDS_NOT_PRESENT:
13071 /* DW_TAG_lexical_block has no attributes, process its children as if
13072 there was no wrapping by that DW_TAG_lexical_block.
13073 GCC does no longer produces such DWARF since GCC r224161. */
13074 for (child_die = die->child;
13075 child_die != NULL && child_die->tag;
13076 child_die = sibling_die (child_die))
13077 process_die (child_die, cu);
13078 return;
13079 case PC_BOUNDS_INVALID:
13080 return;
13081 }
13082 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13083 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13084
13085 cu->get_builder ()->push_context (0, lowpc);
13086 if (die->child != NULL)
13087 {
13088 child_die = die->child;
13089 while (child_die && child_die->tag)
13090 {
13091 process_die (child_die, cu);
13092 child_die = sibling_die (child_die);
13093 }
13094 }
13095 inherit_abstract_dies (die, cu);
13096 struct context_stack cstk = cu->get_builder ()->pop_context ();
13097
13098 if (*cu->get_builder ()->get_local_symbols () != NULL
13099 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13100 {
13101 struct block *block
13102 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13103 cstk.start_addr, highpc);
13104
13105 /* Note that recording ranges after traversing children, as we
13106 do here, means that recording a parent's ranges entails
13107 walking across all its children's ranges as they appear in
13108 the address map, which is quadratic behavior.
13109
13110 It would be nicer to record the parent's ranges before
13111 traversing its children, simply overriding whatever you find
13112 there. But since we don't even decide whether to create a
13113 block until after we've traversed its children, that's hard
13114 to do. */
13115 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13116 }
13117 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13118 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13119 }
13120
13121 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13122
13123 static void
13124 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13125 {
13126 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13127 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13128 CORE_ADDR pc, baseaddr;
13129 struct attribute *attr;
13130 struct call_site *call_site, call_site_local;
13131 void **slot;
13132 int nparams;
13133 struct die_info *child_die;
13134
13135 baseaddr = objfile->text_section_offset ();
13136
13137 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13138 if (attr == NULL)
13139 {
13140 /* This was a pre-DWARF-5 GNU extension alias
13141 for DW_AT_call_return_pc. */
13142 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13143 }
13144 if (!attr)
13145 {
13146 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13147 "DIE %s [in module %s]"),
13148 sect_offset_str (die->sect_off), objfile_name (objfile));
13149 return;
13150 }
13151 pc = attr->value_as_address () + baseaddr;
13152 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13153
13154 if (cu->call_site_htab == NULL)
13155 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13156 NULL, &objfile->objfile_obstack,
13157 hashtab_obstack_allocate, NULL);
13158 call_site_local.pc = pc;
13159 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13160 if (*slot != NULL)
13161 {
13162 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13163 "DIE %s [in module %s]"),
13164 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13165 objfile_name (objfile));
13166 return;
13167 }
13168
13169 /* Count parameters at the caller. */
13170
13171 nparams = 0;
13172 for (child_die = die->child; child_die && child_die->tag;
13173 child_die = sibling_die (child_die))
13174 {
13175 if (child_die->tag != DW_TAG_call_site_parameter
13176 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13177 {
13178 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13179 "DW_TAG_call_site child DIE %s [in module %s]"),
13180 child_die->tag, sect_offset_str (child_die->sect_off),
13181 objfile_name (objfile));
13182 continue;
13183 }
13184
13185 nparams++;
13186 }
13187
13188 call_site
13189 = ((struct call_site *)
13190 obstack_alloc (&objfile->objfile_obstack,
13191 sizeof (*call_site)
13192 + (sizeof (*call_site->parameter) * (nparams - 1))));
13193 *slot = call_site;
13194 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13195 call_site->pc = pc;
13196
13197 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13198 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13199 {
13200 struct die_info *func_die;
13201
13202 /* Skip also over DW_TAG_inlined_subroutine. */
13203 for (func_die = die->parent;
13204 func_die && func_die->tag != DW_TAG_subprogram
13205 && func_die->tag != DW_TAG_subroutine_type;
13206 func_die = func_die->parent);
13207
13208 /* DW_AT_call_all_calls is a superset
13209 of DW_AT_call_all_tail_calls. */
13210 if (func_die
13211 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13212 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13213 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13214 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13215 {
13216 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13217 not complete. But keep CALL_SITE for look ups via call_site_htab,
13218 both the initial caller containing the real return address PC and
13219 the final callee containing the current PC of a chain of tail
13220 calls do not need to have the tail call list complete. But any
13221 function candidate for a virtual tail call frame searched via
13222 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13223 determined unambiguously. */
13224 }
13225 else
13226 {
13227 struct type *func_type = NULL;
13228
13229 if (func_die)
13230 func_type = get_die_type (func_die, cu);
13231 if (func_type != NULL)
13232 {
13233 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13234
13235 /* Enlist this call site to the function. */
13236 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13237 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13238 }
13239 else
13240 complaint (_("Cannot find function owning DW_TAG_call_site "
13241 "DIE %s [in module %s]"),
13242 sect_offset_str (die->sect_off), objfile_name (objfile));
13243 }
13244 }
13245
13246 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13247 if (attr == NULL)
13248 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13249 if (attr == NULL)
13250 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13251 if (attr == NULL)
13252 {
13253 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13254 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13255 }
13256 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13257 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13258 /* Keep NULL DWARF_BLOCK. */;
13259 else if (attr->form_is_block ())
13260 {
13261 struct dwarf2_locexpr_baton *dlbaton;
13262
13263 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13264 dlbaton->data = DW_BLOCK (attr)->data;
13265 dlbaton->size = DW_BLOCK (attr)->size;
13266 dlbaton->per_cu = cu->per_cu;
13267
13268 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13269 }
13270 else if (attr->form_is_ref ())
13271 {
13272 struct dwarf2_cu *target_cu = cu;
13273 struct die_info *target_die;
13274
13275 target_die = follow_die_ref (die, attr, &target_cu);
13276 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13277 if (die_is_declaration (target_die, target_cu))
13278 {
13279 const char *target_physname;
13280
13281 /* Prefer the mangled name; otherwise compute the demangled one. */
13282 target_physname = dw2_linkage_name (target_die, target_cu);
13283 if (target_physname == NULL)
13284 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13285 if (target_physname == NULL)
13286 complaint (_("DW_AT_call_target target DIE has invalid "
13287 "physname, for referencing DIE %s [in module %s]"),
13288 sect_offset_str (die->sect_off), objfile_name (objfile));
13289 else
13290 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13291 }
13292 else
13293 {
13294 CORE_ADDR lowpc;
13295
13296 /* DW_AT_entry_pc should be preferred. */
13297 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13298 <= PC_BOUNDS_INVALID)
13299 complaint (_("DW_AT_call_target target DIE has invalid "
13300 "low pc, for referencing DIE %s [in module %s]"),
13301 sect_offset_str (die->sect_off), objfile_name (objfile));
13302 else
13303 {
13304 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13305 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13306 }
13307 }
13308 }
13309 else
13310 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13311 "block nor reference, for DIE %s [in module %s]"),
13312 sect_offset_str (die->sect_off), objfile_name (objfile));
13313
13314 call_site->per_cu = cu->per_cu;
13315
13316 for (child_die = die->child;
13317 child_die && child_die->tag;
13318 child_die = sibling_die (child_die))
13319 {
13320 struct call_site_parameter *parameter;
13321 struct attribute *loc, *origin;
13322
13323 if (child_die->tag != DW_TAG_call_site_parameter
13324 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13325 {
13326 /* Already printed the complaint above. */
13327 continue;
13328 }
13329
13330 gdb_assert (call_site->parameter_count < nparams);
13331 parameter = &call_site->parameter[call_site->parameter_count];
13332
13333 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13334 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13335 register is contained in DW_AT_call_value. */
13336
13337 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13338 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13339 if (origin == NULL)
13340 {
13341 /* This was a pre-DWARF-5 GNU extension alias
13342 for DW_AT_call_parameter. */
13343 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13344 }
13345 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13346 {
13347 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13348
13349 sect_offset sect_off
13350 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13351 if (!cu->header.offset_in_cu_p (sect_off))
13352 {
13353 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13354 binding can be done only inside one CU. Such referenced DIE
13355 therefore cannot be even moved to DW_TAG_partial_unit. */
13356 complaint (_("DW_AT_call_parameter offset is not in CU for "
13357 "DW_TAG_call_site child DIE %s [in module %s]"),
13358 sect_offset_str (child_die->sect_off),
13359 objfile_name (objfile));
13360 continue;
13361 }
13362 parameter->u.param_cu_off
13363 = (cu_offset) (sect_off - cu->header.sect_off);
13364 }
13365 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13366 {
13367 complaint (_("No DW_FORM_block* DW_AT_location for "
13368 "DW_TAG_call_site child DIE %s [in module %s]"),
13369 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13370 continue;
13371 }
13372 else
13373 {
13374 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13375 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13376 if (parameter->u.dwarf_reg != -1)
13377 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13378 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13379 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13380 &parameter->u.fb_offset))
13381 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13382 else
13383 {
13384 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13385 "for DW_FORM_block* DW_AT_location is supported for "
13386 "DW_TAG_call_site child DIE %s "
13387 "[in module %s]"),
13388 sect_offset_str (child_die->sect_off),
13389 objfile_name (objfile));
13390 continue;
13391 }
13392 }
13393
13394 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13395 if (attr == NULL)
13396 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13397 if (attr == NULL || !attr->form_is_block ())
13398 {
13399 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13400 "DW_TAG_call_site child DIE %s [in module %s]"),
13401 sect_offset_str (child_die->sect_off),
13402 objfile_name (objfile));
13403 continue;
13404 }
13405 parameter->value = DW_BLOCK (attr)->data;
13406 parameter->value_size = DW_BLOCK (attr)->size;
13407
13408 /* Parameters are not pre-cleared by memset above. */
13409 parameter->data_value = NULL;
13410 parameter->data_value_size = 0;
13411 call_site->parameter_count++;
13412
13413 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13414 if (attr == NULL)
13415 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13416 if (attr != nullptr)
13417 {
13418 if (!attr->form_is_block ())
13419 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13420 "DW_TAG_call_site child DIE %s [in module %s]"),
13421 sect_offset_str (child_die->sect_off),
13422 objfile_name (objfile));
13423 else
13424 {
13425 parameter->data_value = DW_BLOCK (attr)->data;
13426 parameter->data_value_size = DW_BLOCK (attr)->size;
13427 }
13428 }
13429 }
13430 }
13431
13432 /* Helper function for read_variable. If DIE represents a virtual
13433 table, then return the type of the concrete object that is
13434 associated with the virtual table. Otherwise, return NULL. */
13435
13436 static struct type *
13437 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13438 {
13439 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13440 if (attr == NULL)
13441 return NULL;
13442
13443 /* Find the type DIE. */
13444 struct die_info *type_die = NULL;
13445 struct dwarf2_cu *type_cu = cu;
13446
13447 if (attr->form_is_ref ())
13448 type_die = follow_die_ref (die, attr, &type_cu);
13449 if (type_die == NULL)
13450 return NULL;
13451
13452 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13453 return NULL;
13454 return die_containing_type (type_die, type_cu);
13455 }
13456
13457 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13458
13459 static void
13460 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13461 {
13462 struct rust_vtable_symbol *storage = NULL;
13463
13464 if (cu->language == language_rust)
13465 {
13466 struct type *containing_type = rust_containing_type (die, cu);
13467
13468 if (containing_type != NULL)
13469 {
13470 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13471
13472 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13473 initialize_objfile_symbol (storage);
13474 storage->concrete_type = containing_type;
13475 storage->subclass = SYMBOL_RUST_VTABLE;
13476 }
13477 }
13478
13479 struct symbol *res = new_symbol (die, NULL, cu, storage);
13480 struct attribute *abstract_origin
13481 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13482 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13483 if (res == NULL && loc && abstract_origin)
13484 {
13485 /* We have a variable without a name, but with a location and an abstract
13486 origin. This may be a concrete instance of an abstract variable
13487 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13488 later. */
13489 struct dwarf2_cu *origin_cu = cu;
13490 struct die_info *origin_die
13491 = follow_die_ref (die, abstract_origin, &origin_cu);
13492 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13493 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13494 }
13495 }
13496
13497 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13498 reading .debug_rnglists.
13499 Callback's type should be:
13500 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13501 Return true if the attributes are present and valid, otherwise,
13502 return false. */
13503
13504 template <typename Callback>
13505 static bool
13506 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13507 Callback &&callback)
13508 {
13509 struct dwarf2_per_objfile *dwarf2_per_objfile
13510 = cu->per_cu->dwarf2_per_objfile;
13511 struct objfile *objfile = dwarf2_per_objfile->objfile;
13512 bfd *obfd = objfile->obfd;
13513 /* Base address selection entry. */
13514 CORE_ADDR base;
13515 int found_base;
13516 const gdb_byte *buffer;
13517 CORE_ADDR baseaddr;
13518 bool overflow = false;
13519
13520 found_base = cu->base_known;
13521 base = cu->base_address;
13522
13523 dwarf2_per_objfile->rnglists.read (objfile);
13524 if (offset >= dwarf2_per_objfile->rnglists.size)
13525 {
13526 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13527 offset);
13528 return false;
13529 }
13530 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13531
13532 baseaddr = objfile->text_section_offset ();
13533
13534 while (1)
13535 {
13536 /* Initialize it due to a false compiler warning. */
13537 CORE_ADDR range_beginning = 0, range_end = 0;
13538 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13539 + dwarf2_per_objfile->rnglists.size);
13540 unsigned int bytes_read;
13541
13542 if (buffer == buf_end)
13543 {
13544 overflow = true;
13545 break;
13546 }
13547 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13548 switch (rlet)
13549 {
13550 case DW_RLE_end_of_list:
13551 break;
13552 case DW_RLE_base_address:
13553 if (buffer + cu->header.addr_size > buf_end)
13554 {
13555 overflow = true;
13556 break;
13557 }
13558 base = cu->header.read_address (obfd, buffer, &bytes_read);
13559 found_base = 1;
13560 buffer += bytes_read;
13561 break;
13562 case DW_RLE_start_length:
13563 if (buffer + cu->header.addr_size > buf_end)
13564 {
13565 overflow = true;
13566 break;
13567 }
13568 range_beginning = cu->header.read_address (obfd, buffer,
13569 &bytes_read);
13570 buffer += bytes_read;
13571 range_end = (range_beginning
13572 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13573 buffer += bytes_read;
13574 if (buffer > buf_end)
13575 {
13576 overflow = true;
13577 break;
13578 }
13579 break;
13580 case DW_RLE_offset_pair:
13581 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13582 buffer += bytes_read;
13583 if (buffer > buf_end)
13584 {
13585 overflow = true;
13586 break;
13587 }
13588 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13589 buffer += bytes_read;
13590 if (buffer > buf_end)
13591 {
13592 overflow = true;
13593 break;
13594 }
13595 break;
13596 case DW_RLE_start_end:
13597 if (buffer + 2 * cu->header.addr_size > buf_end)
13598 {
13599 overflow = true;
13600 break;
13601 }
13602 range_beginning = cu->header.read_address (obfd, buffer,
13603 &bytes_read);
13604 buffer += bytes_read;
13605 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13606 buffer += bytes_read;
13607 break;
13608 default:
13609 complaint (_("Invalid .debug_rnglists data (no base address)"));
13610 return false;
13611 }
13612 if (rlet == DW_RLE_end_of_list || overflow)
13613 break;
13614 if (rlet == DW_RLE_base_address)
13615 continue;
13616
13617 if (!found_base)
13618 {
13619 /* We have no valid base address for the ranges
13620 data. */
13621 complaint (_("Invalid .debug_rnglists data (no base address)"));
13622 return false;
13623 }
13624
13625 if (range_beginning > range_end)
13626 {
13627 /* Inverted range entries are invalid. */
13628 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13629 return false;
13630 }
13631
13632 /* Empty range entries have no effect. */
13633 if (range_beginning == range_end)
13634 continue;
13635
13636 range_beginning += base;
13637 range_end += base;
13638
13639 /* A not-uncommon case of bad debug info.
13640 Don't pollute the addrmap with bad data. */
13641 if (range_beginning + baseaddr == 0
13642 && !dwarf2_per_objfile->has_section_at_zero)
13643 {
13644 complaint (_(".debug_rnglists entry has start address of zero"
13645 " [in module %s]"), objfile_name (objfile));
13646 continue;
13647 }
13648
13649 callback (range_beginning, range_end);
13650 }
13651
13652 if (overflow)
13653 {
13654 complaint (_("Offset %d is not terminated "
13655 "for DW_AT_ranges attribute"),
13656 offset);
13657 return false;
13658 }
13659
13660 return true;
13661 }
13662
13663 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13664 Callback's type should be:
13665 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13666 Return 1 if the attributes are present and valid, otherwise, return 0. */
13667
13668 template <typename Callback>
13669 static int
13670 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13671 Callback &&callback)
13672 {
13673 struct dwarf2_per_objfile *dwarf2_per_objfile
13674 = cu->per_cu->dwarf2_per_objfile;
13675 struct objfile *objfile = dwarf2_per_objfile->objfile;
13676 struct comp_unit_head *cu_header = &cu->header;
13677 bfd *obfd = objfile->obfd;
13678 unsigned int addr_size = cu_header->addr_size;
13679 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13680 /* Base address selection entry. */
13681 CORE_ADDR base;
13682 int found_base;
13683 unsigned int dummy;
13684 const gdb_byte *buffer;
13685 CORE_ADDR baseaddr;
13686
13687 if (cu_header->version >= 5)
13688 return dwarf2_rnglists_process (offset, cu, callback);
13689
13690 found_base = cu->base_known;
13691 base = cu->base_address;
13692
13693 dwarf2_per_objfile->ranges.read (objfile);
13694 if (offset >= dwarf2_per_objfile->ranges.size)
13695 {
13696 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13697 offset);
13698 return 0;
13699 }
13700 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13701
13702 baseaddr = objfile->text_section_offset ();
13703
13704 while (1)
13705 {
13706 CORE_ADDR range_beginning, range_end;
13707
13708 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13709 buffer += addr_size;
13710 range_end = cu->header.read_address (obfd, buffer, &dummy);
13711 buffer += addr_size;
13712 offset += 2 * addr_size;
13713
13714 /* An end of list marker is a pair of zero addresses. */
13715 if (range_beginning == 0 && range_end == 0)
13716 /* Found the end of list entry. */
13717 break;
13718
13719 /* Each base address selection entry is a pair of 2 values.
13720 The first is the largest possible address, the second is
13721 the base address. Check for a base address here. */
13722 if ((range_beginning & mask) == mask)
13723 {
13724 /* If we found the largest possible address, then we already
13725 have the base address in range_end. */
13726 base = range_end;
13727 found_base = 1;
13728 continue;
13729 }
13730
13731 if (!found_base)
13732 {
13733 /* We have no valid base address for the ranges
13734 data. */
13735 complaint (_("Invalid .debug_ranges data (no base address)"));
13736 return 0;
13737 }
13738
13739 if (range_beginning > range_end)
13740 {
13741 /* Inverted range entries are invalid. */
13742 complaint (_("Invalid .debug_ranges data (inverted range)"));
13743 return 0;
13744 }
13745
13746 /* Empty range entries have no effect. */
13747 if (range_beginning == range_end)
13748 continue;
13749
13750 range_beginning += base;
13751 range_end += base;
13752
13753 /* A not-uncommon case of bad debug info.
13754 Don't pollute the addrmap with bad data. */
13755 if (range_beginning + baseaddr == 0
13756 && !dwarf2_per_objfile->has_section_at_zero)
13757 {
13758 complaint (_(".debug_ranges entry has start address of zero"
13759 " [in module %s]"), objfile_name (objfile));
13760 continue;
13761 }
13762
13763 callback (range_beginning, range_end);
13764 }
13765
13766 return 1;
13767 }
13768
13769 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13770 Return 1 if the attributes are present and valid, otherwise, return 0.
13771 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13772
13773 static int
13774 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13775 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13776 dwarf2_psymtab *ranges_pst)
13777 {
13778 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13779 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13780 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13781 int low_set = 0;
13782 CORE_ADDR low = 0;
13783 CORE_ADDR high = 0;
13784 int retval;
13785
13786 retval = dwarf2_ranges_process (offset, cu,
13787 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13788 {
13789 if (ranges_pst != NULL)
13790 {
13791 CORE_ADDR lowpc;
13792 CORE_ADDR highpc;
13793
13794 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13795 range_beginning + baseaddr)
13796 - baseaddr);
13797 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13798 range_end + baseaddr)
13799 - baseaddr);
13800 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13801 lowpc, highpc - 1, ranges_pst);
13802 }
13803
13804 /* FIXME: This is recording everything as a low-high
13805 segment of consecutive addresses. We should have a
13806 data structure for discontiguous block ranges
13807 instead. */
13808 if (! low_set)
13809 {
13810 low = range_beginning;
13811 high = range_end;
13812 low_set = 1;
13813 }
13814 else
13815 {
13816 if (range_beginning < low)
13817 low = range_beginning;
13818 if (range_end > high)
13819 high = range_end;
13820 }
13821 });
13822 if (!retval)
13823 return 0;
13824
13825 if (! low_set)
13826 /* If the first entry is an end-of-list marker, the range
13827 describes an empty scope, i.e. no instructions. */
13828 return 0;
13829
13830 if (low_return)
13831 *low_return = low;
13832 if (high_return)
13833 *high_return = high;
13834 return 1;
13835 }
13836
13837 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13838 definition for the return value. *LOWPC and *HIGHPC are set iff
13839 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13840
13841 static enum pc_bounds_kind
13842 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13843 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13844 dwarf2_psymtab *pst)
13845 {
13846 struct dwarf2_per_objfile *dwarf2_per_objfile
13847 = cu->per_cu->dwarf2_per_objfile;
13848 struct attribute *attr;
13849 struct attribute *attr_high;
13850 CORE_ADDR low = 0;
13851 CORE_ADDR high = 0;
13852 enum pc_bounds_kind ret;
13853
13854 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13855 if (attr_high)
13856 {
13857 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13858 if (attr != nullptr)
13859 {
13860 low = attr->value_as_address ();
13861 high = attr_high->value_as_address ();
13862 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13863 high += low;
13864 }
13865 else
13866 /* Found high w/o low attribute. */
13867 return PC_BOUNDS_INVALID;
13868
13869 /* Found consecutive range of addresses. */
13870 ret = PC_BOUNDS_HIGH_LOW;
13871 }
13872 else
13873 {
13874 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13875 if (attr != NULL)
13876 {
13877 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13878 We take advantage of the fact that DW_AT_ranges does not appear
13879 in DW_TAG_compile_unit of DWO files. */
13880 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13881 unsigned int ranges_offset = (DW_UNSND (attr)
13882 + (need_ranges_base
13883 ? cu->ranges_base
13884 : 0));
13885
13886 /* Value of the DW_AT_ranges attribute is the offset in the
13887 .debug_ranges section. */
13888 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13889 return PC_BOUNDS_INVALID;
13890 /* Found discontinuous range of addresses. */
13891 ret = PC_BOUNDS_RANGES;
13892 }
13893 else
13894 return PC_BOUNDS_NOT_PRESENT;
13895 }
13896
13897 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13898 if (high <= low)
13899 return PC_BOUNDS_INVALID;
13900
13901 /* When using the GNU linker, .gnu.linkonce. sections are used to
13902 eliminate duplicate copies of functions and vtables and such.
13903 The linker will arbitrarily choose one and discard the others.
13904 The AT_*_pc values for such functions refer to local labels in
13905 these sections. If the section from that file was discarded, the
13906 labels are not in the output, so the relocs get a value of 0.
13907 If this is a discarded function, mark the pc bounds as invalid,
13908 so that GDB will ignore it. */
13909 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13910 return PC_BOUNDS_INVALID;
13911
13912 *lowpc = low;
13913 if (highpc)
13914 *highpc = high;
13915 return ret;
13916 }
13917
13918 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13919 its low and high PC addresses. Do nothing if these addresses could not
13920 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13921 and HIGHPC to the high address if greater than HIGHPC. */
13922
13923 static void
13924 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13925 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13926 struct dwarf2_cu *cu)
13927 {
13928 CORE_ADDR low, high;
13929 struct die_info *child = die->child;
13930
13931 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13932 {
13933 *lowpc = std::min (*lowpc, low);
13934 *highpc = std::max (*highpc, high);
13935 }
13936
13937 /* If the language does not allow nested subprograms (either inside
13938 subprograms or lexical blocks), we're done. */
13939 if (cu->language != language_ada)
13940 return;
13941
13942 /* Check all the children of the given DIE. If it contains nested
13943 subprograms, then check their pc bounds. Likewise, we need to
13944 check lexical blocks as well, as they may also contain subprogram
13945 definitions. */
13946 while (child && child->tag)
13947 {
13948 if (child->tag == DW_TAG_subprogram
13949 || child->tag == DW_TAG_lexical_block)
13950 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13951 child = sibling_die (child);
13952 }
13953 }
13954
13955 /* Get the low and high pc's represented by the scope DIE, and store
13956 them in *LOWPC and *HIGHPC. If the correct values can't be
13957 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13958
13959 static void
13960 get_scope_pc_bounds (struct die_info *die,
13961 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13962 struct dwarf2_cu *cu)
13963 {
13964 CORE_ADDR best_low = (CORE_ADDR) -1;
13965 CORE_ADDR best_high = (CORE_ADDR) 0;
13966 CORE_ADDR current_low, current_high;
13967
13968 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13969 >= PC_BOUNDS_RANGES)
13970 {
13971 best_low = current_low;
13972 best_high = current_high;
13973 }
13974 else
13975 {
13976 struct die_info *child = die->child;
13977
13978 while (child && child->tag)
13979 {
13980 switch (child->tag) {
13981 case DW_TAG_subprogram:
13982 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13983 break;
13984 case DW_TAG_namespace:
13985 case DW_TAG_module:
13986 /* FIXME: carlton/2004-01-16: Should we do this for
13987 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13988 that current GCC's always emit the DIEs corresponding
13989 to definitions of methods of classes as children of a
13990 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13991 the DIEs giving the declarations, which could be
13992 anywhere). But I don't see any reason why the
13993 standards says that they have to be there. */
13994 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13995
13996 if (current_low != ((CORE_ADDR) -1))
13997 {
13998 best_low = std::min (best_low, current_low);
13999 best_high = std::max (best_high, current_high);
14000 }
14001 break;
14002 default:
14003 /* Ignore. */
14004 break;
14005 }
14006
14007 child = sibling_die (child);
14008 }
14009 }
14010
14011 *lowpc = best_low;
14012 *highpc = best_high;
14013 }
14014
14015 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14016 in DIE. */
14017
14018 static void
14019 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14020 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14021 {
14022 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14023 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14024 struct attribute *attr;
14025 struct attribute *attr_high;
14026
14027 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14028 if (attr_high)
14029 {
14030 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14031 if (attr != nullptr)
14032 {
14033 CORE_ADDR low = attr->value_as_address ();
14034 CORE_ADDR high = attr_high->value_as_address ();
14035
14036 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14037 high += low;
14038
14039 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14040 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14041 cu->get_builder ()->record_block_range (block, low, high - 1);
14042 }
14043 }
14044
14045 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14046 if (attr != nullptr)
14047 {
14048 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14049 We take advantage of the fact that DW_AT_ranges does not appear
14050 in DW_TAG_compile_unit of DWO files. */
14051 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14052
14053 /* The value of the DW_AT_ranges attribute is the offset of the
14054 address range list in the .debug_ranges section. */
14055 unsigned long offset = (DW_UNSND (attr)
14056 + (need_ranges_base ? cu->ranges_base : 0));
14057
14058 std::vector<blockrange> blockvec;
14059 dwarf2_ranges_process (offset, cu,
14060 [&] (CORE_ADDR start, CORE_ADDR end)
14061 {
14062 start += baseaddr;
14063 end += baseaddr;
14064 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14065 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14066 cu->get_builder ()->record_block_range (block, start, end - 1);
14067 blockvec.emplace_back (start, end);
14068 });
14069
14070 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14071 }
14072 }
14073
14074 /* Check whether the producer field indicates either of GCC < 4.6, or the
14075 Intel C/C++ compiler, and cache the result in CU. */
14076
14077 static void
14078 check_producer (struct dwarf2_cu *cu)
14079 {
14080 int major, minor;
14081
14082 if (cu->producer == NULL)
14083 {
14084 /* For unknown compilers expect their behavior is DWARF version
14085 compliant.
14086
14087 GCC started to support .debug_types sections by -gdwarf-4 since
14088 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14089 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14090 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14091 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14092 }
14093 else if (producer_is_gcc (cu->producer, &major, &minor))
14094 {
14095 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14096 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14097 }
14098 else if (producer_is_icc (cu->producer, &major, &minor))
14099 {
14100 cu->producer_is_icc = true;
14101 cu->producer_is_icc_lt_14 = major < 14;
14102 }
14103 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14104 cu->producer_is_codewarrior = true;
14105 else
14106 {
14107 /* For other non-GCC compilers, expect their behavior is DWARF version
14108 compliant. */
14109 }
14110
14111 cu->checked_producer = true;
14112 }
14113
14114 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14115 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14116 during 4.6.0 experimental. */
14117
14118 static bool
14119 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14120 {
14121 if (!cu->checked_producer)
14122 check_producer (cu);
14123
14124 return cu->producer_is_gxx_lt_4_6;
14125 }
14126
14127
14128 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14129 with incorrect is_stmt attributes. */
14130
14131 static bool
14132 producer_is_codewarrior (struct dwarf2_cu *cu)
14133 {
14134 if (!cu->checked_producer)
14135 check_producer (cu);
14136
14137 return cu->producer_is_codewarrior;
14138 }
14139
14140 /* Return the default accessibility type if it is not overridden by
14141 DW_AT_accessibility. */
14142
14143 static enum dwarf_access_attribute
14144 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14145 {
14146 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14147 {
14148 /* The default DWARF 2 accessibility for members is public, the default
14149 accessibility for inheritance is private. */
14150
14151 if (die->tag != DW_TAG_inheritance)
14152 return DW_ACCESS_public;
14153 else
14154 return DW_ACCESS_private;
14155 }
14156 else
14157 {
14158 /* DWARF 3+ defines the default accessibility a different way. The same
14159 rules apply now for DW_TAG_inheritance as for the members and it only
14160 depends on the container kind. */
14161
14162 if (die->parent->tag == DW_TAG_class_type)
14163 return DW_ACCESS_private;
14164 else
14165 return DW_ACCESS_public;
14166 }
14167 }
14168
14169 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14170 offset. If the attribute was not found return 0, otherwise return
14171 1. If it was found but could not properly be handled, set *OFFSET
14172 to 0. */
14173
14174 static int
14175 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14176 LONGEST *offset)
14177 {
14178 struct attribute *attr;
14179
14180 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14181 if (attr != NULL)
14182 {
14183 *offset = 0;
14184
14185 /* Note that we do not check for a section offset first here.
14186 This is because DW_AT_data_member_location is new in DWARF 4,
14187 so if we see it, we can assume that a constant form is really
14188 a constant and not a section offset. */
14189 if (attr->form_is_constant ())
14190 *offset = dwarf2_get_attr_constant_value (attr, 0);
14191 else if (attr->form_is_section_offset ())
14192 dwarf2_complex_location_expr_complaint ();
14193 else if (attr->form_is_block ())
14194 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14195 else
14196 dwarf2_complex_location_expr_complaint ();
14197
14198 return 1;
14199 }
14200
14201 return 0;
14202 }
14203
14204 /* Add an aggregate field to the field list. */
14205
14206 static void
14207 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14208 struct dwarf2_cu *cu)
14209 {
14210 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14211 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14212 struct nextfield *new_field;
14213 struct attribute *attr;
14214 struct field *fp;
14215 const char *fieldname = "";
14216
14217 if (die->tag == DW_TAG_inheritance)
14218 {
14219 fip->baseclasses.emplace_back ();
14220 new_field = &fip->baseclasses.back ();
14221 }
14222 else
14223 {
14224 fip->fields.emplace_back ();
14225 new_field = &fip->fields.back ();
14226 }
14227
14228 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14229 if (attr != nullptr)
14230 new_field->accessibility = DW_UNSND (attr);
14231 else
14232 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14233 if (new_field->accessibility != DW_ACCESS_public)
14234 fip->non_public_fields = 1;
14235
14236 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14237 if (attr != nullptr)
14238 new_field->virtuality = DW_UNSND (attr);
14239 else
14240 new_field->virtuality = DW_VIRTUALITY_none;
14241
14242 fp = &new_field->field;
14243
14244 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14245 {
14246 LONGEST offset;
14247
14248 /* Data member other than a C++ static data member. */
14249
14250 /* Get type of field. */
14251 fp->type = die_type (die, cu);
14252
14253 SET_FIELD_BITPOS (*fp, 0);
14254
14255 /* Get bit size of field (zero if none). */
14256 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14257 if (attr != nullptr)
14258 {
14259 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14260 }
14261 else
14262 {
14263 FIELD_BITSIZE (*fp) = 0;
14264 }
14265
14266 /* Get bit offset of field. */
14267 if (handle_data_member_location (die, cu, &offset))
14268 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14269 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14270 if (attr != nullptr)
14271 {
14272 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14273 {
14274 /* For big endian bits, the DW_AT_bit_offset gives the
14275 additional bit offset from the MSB of the containing
14276 anonymous object to the MSB of the field. We don't
14277 have to do anything special since we don't need to
14278 know the size of the anonymous object. */
14279 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14280 }
14281 else
14282 {
14283 /* For little endian bits, compute the bit offset to the
14284 MSB of the anonymous object, subtract off the number of
14285 bits from the MSB of the field to the MSB of the
14286 object, and then subtract off the number of bits of
14287 the field itself. The result is the bit offset of
14288 the LSB of the field. */
14289 int anonymous_size;
14290 int bit_offset = DW_UNSND (attr);
14291
14292 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14293 if (attr != nullptr)
14294 {
14295 /* The size of the anonymous object containing
14296 the bit field is explicit, so use the
14297 indicated size (in bytes). */
14298 anonymous_size = DW_UNSND (attr);
14299 }
14300 else
14301 {
14302 /* The size of the anonymous object containing
14303 the bit field must be inferred from the type
14304 attribute of the data member containing the
14305 bit field. */
14306 anonymous_size = TYPE_LENGTH (fp->type);
14307 }
14308 SET_FIELD_BITPOS (*fp,
14309 (FIELD_BITPOS (*fp)
14310 + anonymous_size * bits_per_byte
14311 - bit_offset - FIELD_BITSIZE (*fp)));
14312 }
14313 }
14314 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14315 if (attr != NULL)
14316 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14317 + dwarf2_get_attr_constant_value (attr, 0)));
14318
14319 /* Get name of field. */
14320 fieldname = dwarf2_name (die, cu);
14321 if (fieldname == NULL)
14322 fieldname = "";
14323
14324 /* The name is already allocated along with this objfile, so we don't
14325 need to duplicate it for the type. */
14326 fp->name = fieldname;
14327
14328 /* Change accessibility for artificial fields (e.g. virtual table
14329 pointer or virtual base class pointer) to private. */
14330 if (dwarf2_attr (die, DW_AT_artificial, cu))
14331 {
14332 FIELD_ARTIFICIAL (*fp) = 1;
14333 new_field->accessibility = DW_ACCESS_private;
14334 fip->non_public_fields = 1;
14335 }
14336 }
14337 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14338 {
14339 /* C++ static member. */
14340
14341 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14342 is a declaration, but all versions of G++ as of this writing
14343 (so through at least 3.2.1) incorrectly generate
14344 DW_TAG_variable tags. */
14345
14346 const char *physname;
14347
14348 /* Get name of field. */
14349 fieldname = dwarf2_name (die, cu);
14350 if (fieldname == NULL)
14351 return;
14352
14353 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14354 if (attr
14355 /* Only create a symbol if this is an external value.
14356 new_symbol checks this and puts the value in the global symbol
14357 table, which we want. If it is not external, new_symbol
14358 will try to put the value in cu->list_in_scope which is wrong. */
14359 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14360 {
14361 /* A static const member, not much different than an enum as far as
14362 we're concerned, except that we can support more types. */
14363 new_symbol (die, NULL, cu);
14364 }
14365
14366 /* Get physical name. */
14367 physname = dwarf2_physname (fieldname, die, cu);
14368
14369 /* The name is already allocated along with this objfile, so we don't
14370 need to duplicate it for the type. */
14371 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14372 FIELD_TYPE (*fp) = die_type (die, cu);
14373 FIELD_NAME (*fp) = fieldname;
14374 }
14375 else if (die->tag == DW_TAG_inheritance)
14376 {
14377 LONGEST offset;
14378
14379 /* C++ base class field. */
14380 if (handle_data_member_location (die, cu, &offset))
14381 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14382 FIELD_BITSIZE (*fp) = 0;
14383 FIELD_TYPE (*fp) = die_type (die, cu);
14384 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14385 }
14386 else if (die->tag == DW_TAG_variant_part)
14387 {
14388 /* process_structure_scope will treat this DIE as a union. */
14389 process_structure_scope (die, cu);
14390
14391 /* The variant part is relative to the start of the enclosing
14392 structure. */
14393 SET_FIELD_BITPOS (*fp, 0);
14394 fp->type = get_die_type (die, cu);
14395 fp->artificial = 1;
14396 fp->name = "<<variant>>";
14397
14398 /* Normally a DW_TAG_variant_part won't have a size, but our
14399 representation requires one, so set it to the maximum of the
14400 child sizes, being sure to account for the offset at which
14401 each child is seen. */
14402 if (TYPE_LENGTH (fp->type) == 0)
14403 {
14404 unsigned max = 0;
14405 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14406 {
14407 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14408 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14409 if (len > max)
14410 max = len;
14411 }
14412 TYPE_LENGTH (fp->type) = max;
14413 }
14414 }
14415 else
14416 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14417 }
14418
14419 /* Can the type given by DIE define another type? */
14420
14421 static bool
14422 type_can_define_types (const struct die_info *die)
14423 {
14424 switch (die->tag)
14425 {
14426 case DW_TAG_typedef:
14427 case DW_TAG_class_type:
14428 case DW_TAG_structure_type:
14429 case DW_TAG_union_type:
14430 case DW_TAG_enumeration_type:
14431 return true;
14432
14433 default:
14434 return false;
14435 }
14436 }
14437
14438 /* Add a type definition defined in the scope of the FIP's class. */
14439
14440 static void
14441 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14442 struct dwarf2_cu *cu)
14443 {
14444 struct decl_field fp;
14445 memset (&fp, 0, sizeof (fp));
14446
14447 gdb_assert (type_can_define_types (die));
14448
14449 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14450 fp.name = dwarf2_name (die, cu);
14451 fp.type = read_type_die (die, cu);
14452
14453 /* Save accessibility. */
14454 enum dwarf_access_attribute accessibility;
14455 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14456 if (attr != NULL)
14457 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14458 else
14459 accessibility = dwarf2_default_access_attribute (die, cu);
14460 switch (accessibility)
14461 {
14462 case DW_ACCESS_public:
14463 /* The assumed value if neither private nor protected. */
14464 break;
14465 case DW_ACCESS_private:
14466 fp.is_private = 1;
14467 break;
14468 case DW_ACCESS_protected:
14469 fp.is_protected = 1;
14470 break;
14471 default:
14472 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14473 }
14474
14475 if (die->tag == DW_TAG_typedef)
14476 fip->typedef_field_list.push_back (fp);
14477 else
14478 fip->nested_types_list.push_back (fp);
14479 }
14480
14481 /* Create the vector of fields, and attach it to the type. */
14482
14483 static void
14484 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14485 struct dwarf2_cu *cu)
14486 {
14487 int nfields = fip->nfields ();
14488
14489 /* Record the field count, allocate space for the array of fields,
14490 and create blank accessibility bitfields if necessary. */
14491 TYPE_NFIELDS (type) = nfields;
14492 TYPE_FIELDS (type) = (struct field *)
14493 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14494
14495 if (fip->non_public_fields && cu->language != language_ada)
14496 {
14497 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14498
14499 TYPE_FIELD_PRIVATE_BITS (type) =
14500 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14501 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14502
14503 TYPE_FIELD_PROTECTED_BITS (type) =
14504 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14505 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14506
14507 TYPE_FIELD_IGNORE_BITS (type) =
14508 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14509 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14510 }
14511
14512 /* If the type has baseclasses, allocate and clear a bit vector for
14513 TYPE_FIELD_VIRTUAL_BITS. */
14514 if (!fip->baseclasses.empty () && cu->language != language_ada)
14515 {
14516 int num_bytes = B_BYTES (fip->baseclasses.size ());
14517 unsigned char *pointer;
14518
14519 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14520 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14521 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14522 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14523 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14524 }
14525
14526 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14527 {
14528 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14529
14530 for (int index = 0; index < nfields; ++index)
14531 {
14532 struct nextfield &field = fip->fields[index];
14533
14534 if (field.variant.is_discriminant)
14535 di->discriminant_index = index;
14536 else if (field.variant.default_branch)
14537 di->default_index = index;
14538 else
14539 di->discriminants[index] = field.variant.discriminant_value;
14540 }
14541 }
14542
14543 /* Copy the saved-up fields into the field vector. */
14544 for (int i = 0; i < nfields; ++i)
14545 {
14546 struct nextfield &field
14547 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14548 : fip->fields[i - fip->baseclasses.size ()]);
14549
14550 TYPE_FIELD (type, i) = field.field;
14551 switch (field.accessibility)
14552 {
14553 case DW_ACCESS_private:
14554 if (cu->language != language_ada)
14555 SET_TYPE_FIELD_PRIVATE (type, i);
14556 break;
14557
14558 case DW_ACCESS_protected:
14559 if (cu->language != language_ada)
14560 SET_TYPE_FIELD_PROTECTED (type, i);
14561 break;
14562
14563 case DW_ACCESS_public:
14564 break;
14565
14566 default:
14567 /* Unknown accessibility. Complain and treat it as public. */
14568 {
14569 complaint (_("unsupported accessibility %d"),
14570 field.accessibility);
14571 }
14572 break;
14573 }
14574 if (i < fip->baseclasses.size ())
14575 {
14576 switch (field.virtuality)
14577 {
14578 case DW_VIRTUALITY_virtual:
14579 case DW_VIRTUALITY_pure_virtual:
14580 if (cu->language == language_ada)
14581 error (_("unexpected virtuality in component of Ada type"));
14582 SET_TYPE_FIELD_VIRTUAL (type, i);
14583 break;
14584 }
14585 }
14586 }
14587 }
14588
14589 /* Return true if this member function is a constructor, false
14590 otherwise. */
14591
14592 static int
14593 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14594 {
14595 const char *fieldname;
14596 const char *type_name;
14597 int len;
14598
14599 if (die->parent == NULL)
14600 return 0;
14601
14602 if (die->parent->tag != DW_TAG_structure_type
14603 && die->parent->tag != DW_TAG_union_type
14604 && die->parent->tag != DW_TAG_class_type)
14605 return 0;
14606
14607 fieldname = dwarf2_name (die, cu);
14608 type_name = dwarf2_name (die->parent, cu);
14609 if (fieldname == NULL || type_name == NULL)
14610 return 0;
14611
14612 len = strlen (fieldname);
14613 return (strncmp (fieldname, type_name, len) == 0
14614 && (type_name[len] == '\0' || type_name[len] == '<'));
14615 }
14616
14617 /* Check if the given VALUE is a recognized enum
14618 dwarf_defaulted_attribute constant according to DWARF5 spec,
14619 Table 7.24. */
14620
14621 static bool
14622 is_valid_DW_AT_defaulted (ULONGEST value)
14623 {
14624 switch (value)
14625 {
14626 case DW_DEFAULTED_no:
14627 case DW_DEFAULTED_in_class:
14628 case DW_DEFAULTED_out_of_class:
14629 return true;
14630 }
14631
14632 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14633 return false;
14634 }
14635
14636 /* Add a member function to the proper fieldlist. */
14637
14638 static void
14639 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14640 struct type *type, struct dwarf2_cu *cu)
14641 {
14642 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14643 struct attribute *attr;
14644 int i;
14645 struct fnfieldlist *flp = nullptr;
14646 struct fn_field *fnp;
14647 const char *fieldname;
14648 struct type *this_type;
14649 enum dwarf_access_attribute accessibility;
14650
14651 if (cu->language == language_ada)
14652 error (_("unexpected member function in Ada type"));
14653
14654 /* Get name of member function. */
14655 fieldname = dwarf2_name (die, cu);
14656 if (fieldname == NULL)
14657 return;
14658
14659 /* Look up member function name in fieldlist. */
14660 for (i = 0; i < fip->fnfieldlists.size (); i++)
14661 {
14662 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14663 {
14664 flp = &fip->fnfieldlists[i];
14665 break;
14666 }
14667 }
14668
14669 /* Create a new fnfieldlist if necessary. */
14670 if (flp == nullptr)
14671 {
14672 fip->fnfieldlists.emplace_back ();
14673 flp = &fip->fnfieldlists.back ();
14674 flp->name = fieldname;
14675 i = fip->fnfieldlists.size () - 1;
14676 }
14677
14678 /* Create a new member function field and add it to the vector of
14679 fnfieldlists. */
14680 flp->fnfields.emplace_back ();
14681 fnp = &flp->fnfields.back ();
14682
14683 /* Delay processing of the physname until later. */
14684 if (cu->language == language_cplus)
14685 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14686 die, cu);
14687 else
14688 {
14689 const char *physname = dwarf2_physname (fieldname, die, cu);
14690 fnp->physname = physname ? physname : "";
14691 }
14692
14693 fnp->type = alloc_type (objfile);
14694 this_type = read_type_die (die, cu);
14695 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14696 {
14697 int nparams = TYPE_NFIELDS (this_type);
14698
14699 /* TYPE is the domain of this method, and THIS_TYPE is the type
14700 of the method itself (TYPE_CODE_METHOD). */
14701 smash_to_method_type (fnp->type, type,
14702 TYPE_TARGET_TYPE (this_type),
14703 TYPE_FIELDS (this_type),
14704 TYPE_NFIELDS (this_type),
14705 TYPE_VARARGS (this_type));
14706
14707 /* Handle static member functions.
14708 Dwarf2 has no clean way to discern C++ static and non-static
14709 member functions. G++ helps GDB by marking the first
14710 parameter for non-static member functions (which is the this
14711 pointer) as artificial. We obtain this information from
14712 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14713 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14714 fnp->voffset = VOFFSET_STATIC;
14715 }
14716 else
14717 complaint (_("member function type missing for '%s'"),
14718 dwarf2_full_name (fieldname, die, cu));
14719
14720 /* Get fcontext from DW_AT_containing_type if present. */
14721 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14722 fnp->fcontext = die_containing_type (die, cu);
14723
14724 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14725 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14726
14727 /* Get accessibility. */
14728 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14729 if (attr != nullptr)
14730 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14731 else
14732 accessibility = dwarf2_default_access_attribute (die, cu);
14733 switch (accessibility)
14734 {
14735 case DW_ACCESS_private:
14736 fnp->is_private = 1;
14737 break;
14738 case DW_ACCESS_protected:
14739 fnp->is_protected = 1;
14740 break;
14741 }
14742
14743 /* Check for artificial methods. */
14744 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14745 if (attr && DW_UNSND (attr) != 0)
14746 fnp->is_artificial = 1;
14747
14748 /* Check for defaulted methods. */
14749 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14750 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14751 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14752
14753 /* Check for deleted methods. */
14754 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14755 if (attr != nullptr && DW_UNSND (attr) != 0)
14756 fnp->is_deleted = 1;
14757
14758 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14759
14760 /* Get index in virtual function table if it is a virtual member
14761 function. For older versions of GCC, this is an offset in the
14762 appropriate virtual table, as specified by DW_AT_containing_type.
14763 For everyone else, it is an expression to be evaluated relative
14764 to the object address. */
14765
14766 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14767 if (attr != nullptr)
14768 {
14769 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14770 {
14771 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14772 {
14773 /* Old-style GCC. */
14774 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14775 }
14776 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14777 || (DW_BLOCK (attr)->size > 1
14778 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14779 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14780 {
14781 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14782 if ((fnp->voffset % cu->header.addr_size) != 0)
14783 dwarf2_complex_location_expr_complaint ();
14784 else
14785 fnp->voffset /= cu->header.addr_size;
14786 fnp->voffset += 2;
14787 }
14788 else
14789 dwarf2_complex_location_expr_complaint ();
14790
14791 if (!fnp->fcontext)
14792 {
14793 /* If there is no `this' field and no DW_AT_containing_type,
14794 we cannot actually find a base class context for the
14795 vtable! */
14796 if (TYPE_NFIELDS (this_type) == 0
14797 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14798 {
14799 complaint (_("cannot determine context for virtual member "
14800 "function \"%s\" (offset %s)"),
14801 fieldname, sect_offset_str (die->sect_off));
14802 }
14803 else
14804 {
14805 fnp->fcontext
14806 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14807 }
14808 }
14809 }
14810 else if (attr->form_is_section_offset ())
14811 {
14812 dwarf2_complex_location_expr_complaint ();
14813 }
14814 else
14815 {
14816 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14817 fieldname);
14818 }
14819 }
14820 else
14821 {
14822 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14823 if (attr && DW_UNSND (attr))
14824 {
14825 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14826 complaint (_("Member function \"%s\" (offset %s) is virtual "
14827 "but the vtable offset is not specified"),
14828 fieldname, sect_offset_str (die->sect_off));
14829 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14830 TYPE_CPLUS_DYNAMIC (type) = 1;
14831 }
14832 }
14833 }
14834
14835 /* Create the vector of member function fields, and attach it to the type. */
14836
14837 static void
14838 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14839 struct dwarf2_cu *cu)
14840 {
14841 if (cu->language == language_ada)
14842 error (_("unexpected member functions in Ada type"));
14843
14844 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14845 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14846 TYPE_ALLOC (type,
14847 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14848
14849 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14850 {
14851 struct fnfieldlist &nf = fip->fnfieldlists[i];
14852 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14853
14854 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14855 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14856 fn_flp->fn_fields = (struct fn_field *)
14857 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14858
14859 for (int k = 0; k < nf.fnfields.size (); ++k)
14860 fn_flp->fn_fields[k] = nf.fnfields[k];
14861 }
14862
14863 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14864 }
14865
14866 /* Returns non-zero if NAME is the name of a vtable member in CU's
14867 language, zero otherwise. */
14868 static int
14869 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14870 {
14871 static const char vptr[] = "_vptr";
14872
14873 /* Look for the C++ form of the vtable. */
14874 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14875 return 1;
14876
14877 return 0;
14878 }
14879
14880 /* GCC outputs unnamed structures that are really pointers to member
14881 functions, with the ABI-specified layout. If TYPE describes
14882 such a structure, smash it into a member function type.
14883
14884 GCC shouldn't do this; it should just output pointer to member DIEs.
14885 This is GCC PR debug/28767. */
14886
14887 static void
14888 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14889 {
14890 struct type *pfn_type, *self_type, *new_type;
14891
14892 /* Check for a structure with no name and two children. */
14893 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14894 return;
14895
14896 /* Check for __pfn and __delta members. */
14897 if (TYPE_FIELD_NAME (type, 0) == NULL
14898 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14899 || TYPE_FIELD_NAME (type, 1) == NULL
14900 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14901 return;
14902
14903 /* Find the type of the method. */
14904 pfn_type = TYPE_FIELD_TYPE (type, 0);
14905 if (pfn_type == NULL
14906 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14907 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14908 return;
14909
14910 /* Look for the "this" argument. */
14911 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14912 if (TYPE_NFIELDS (pfn_type) == 0
14913 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14914 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14915 return;
14916
14917 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14918 new_type = alloc_type (objfile);
14919 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14920 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14921 TYPE_VARARGS (pfn_type));
14922 smash_to_methodptr_type (type, new_type);
14923 }
14924
14925 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14926 appropriate error checking and issuing complaints if there is a
14927 problem. */
14928
14929 static ULONGEST
14930 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14931 {
14932 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14933
14934 if (attr == nullptr)
14935 return 0;
14936
14937 if (!attr->form_is_constant ())
14938 {
14939 complaint (_("DW_AT_alignment must have constant form"
14940 " - DIE at %s [in module %s]"),
14941 sect_offset_str (die->sect_off),
14942 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14943 return 0;
14944 }
14945
14946 ULONGEST align;
14947 if (attr->form == DW_FORM_sdata)
14948 {
14949 LONGEST val = DW_SND (attr);
14950 if (val < 0)
14951 {
14952 complaint (_("DW_AT_alignment value must not be negative"
14953 " - DIE at %s [in module %s]"),
14954 sect_offset_str (die->sect_off),
14955 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14956 return 0;
14957 }
14958 align = val;
14959 }
14960 else
14961 align = DW_UNSND (attr);
14962
14963 if (align == 0)
14964 {
14965 complaint (_("DW_AT_alignment value must not be zero"
14966 " - DIE at %s [in module %s]"),
14967 sect_offset_str (die->sect_off),
14968 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14969 return 0;
14970 }
14971 if ((align & (align - 1)) != 0)
14972 {
14973 complaint (_("DW_AT_alignment value must be a power of 2"
14974 " - DIE at %s [in module %s]"),
14975 sect_offset_str (die->sect_off),
14976 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14977 return 0;
14978 }
14979
14980 return align;
14981 }
14982
14983 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14984 the alignment for TYPE. */
14985
14986 static void
14987 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14988 struct type *type)
14989 {
14990 if (!set_type_align (type, get_alignment (cu, die)))
14991 complaint (_("DW_AT_alignment value too large"
14992 " - DIE at %s [in module %s]"),
14993 sect_offset_str (die->sect_off),
14994 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14995 }
14996
14997 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14998 constant for a type, according to DWARF5 spec, Table 5.5. */
14999
15000 static bool
15001 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15002 {
15003 switch (value)
15004 {
15005 case DW_CC_normal:
15006 case DW_CC_pass_by_reference:
15007 case DW_CC_pass_by_value:
15008 return true;
15009
15010 default:
15011 complaint (_("unrecognized DW_AT_calling_convention value "
15012 "(%s) for a type"), pulongest (value));
15013 return false;
15014 }
15015 }
15016
15017 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15018 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15019 also according to GNU-specific values (see include/dwarf2.h). */
15020
15021 static bool
15022 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15023 {
15024 switch (value)
15025 {
15026 case DW_CC_normal:
15027 case DW_CC_program:
15028 case DW_CC_nocall:
15029 return true;
15030
15031 case DW_CC_GNU_renesas_sh:
15032 case DW_CC_GNU_borland_fastcall_i386:
15033 case DW_CC_GDB_IBM_OpenCL:
15034 return true;
15035
15036 default:
15037 complaint (_("unrecognized DW_AT_calling_convention value "
15038 "(%s) for a subroutine"), pulongest (value));
15039 return false;
15040 }
15041 }
15042
15043 /* Called when we find the DIE that starts a structure or union scope
15044 (definition) to create a type for the structure or union. Fill in
15045 the type's name and general properties; the members will not be
15046 processed until process_structure_scope. A symbol table entry for
15047 the type will also not be done until process_structure_scope (assuming
15048 the type has a name).
15049
15050 NOTE: we need to call these functions regardless of whether or not the
15051 DIE has a DW_AT_name attribute, since it might be an anonymous
15052 structure or union. This gets the type entered into our set of
15053 user defined types. */
15054
15055 static struct type *
15056 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15057 {
15058 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15059 struct type *type;
15060 struct attribute *attr;
15061 const char *name;
15062
15063 /* If the definition of this type lives in .debug_types, read that type.
15064 Don't follow DW_AT_specification though, that will take us back up
15065 the chain and we want to go down. */
15066 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15067 if (attr != nullptr)
15068 {
15069 type = get_DW_AT_signature_type (die, attr, cu);
15070
15071 /* The type's CU may not be the same as CU.
15072 Ensure TYPE is recorded with CU in die_type_hash. */
15073 return set_die_type (die, type, cu);
15074 }
15075
15076 type = alloc_type (objfile);
15077 INIT_CPLUS_SPECIFIC (type);
15078
15079 name = dwarf2_name (die, cu);
15080 if (name != NULL)
15081 {
15082 if (cu->language == language_cplus
15083 || cu->language == language_d
15084 || cu->language == language_rust)
15085 {
15086 const char *full_name = dwarf2_full_name (name, die, cu);
15087
15088 /* dwarf2_full_name might have already finished building the DIE's
15089 type. If so, there is no need to continue. */
15090 if (get_die_type (die, cu) != NULL)
15091 return get_die_type (die, cu);
15092
15093 TYPE_NAME (type) = full_name;
15094 }
15095 else
15096 {
15097 /* The name is already allocated along with this objfile, so
15098 we don't need to duplicate it for the type. */
15099 TYPE_NAME (type) = name;
15100 }
15101 }
15102
15103 if (die->tag == DW_TAG_structure_type)
15104 {
15105 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15106 }
15107 else if (die->tag == DW_TAG_union_type)
15108 {
15109 TYPE_CODE (type) = TYPE_CODE_UNION;
15110 }
15111 else if (die->tag == DW_TAG_variant_part)
15112 {
15113 TYPE_CODE (type) = TYPE_CODE_UNION;
15114 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15115 }
15116 else
15117 {
15118 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15119 }
15120
15121 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15122 TYPE_DECLARED_CLASS (type) = 1;
15123
15124 /* Store the calling convention in the type if it's available in
15125 the die. Otherwise the calling convention remains set to
15126 the default value DW_CC_normal. */
15127 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15128 if (attr != nullptr
15129 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15130 {
15131 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15132 TYPE_CPLUS_CALLING_CONVENTION (type)
15133 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15134 }
15135
15136 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15137 if (attr != nullptr)
15138 {
15139 if (attr->form_is_constant ())
15140 TYPE_LENGTH (type) = DW_UNSND (attr);
15141 else
15142 {
15143 /* For the moment, dynamic type sizes are not supported
15144 by GDB's struct type. The actual size is determined
15145 on-demand when resolving the type of a given object,
15146 so set the type's length to zero for now. Otherwise,
15147 we record an expression as the length, and that expression
15148 could lead to a very large value, which could eventually
15149 lead to us trying to allocate that much memory when creating
15150 a value of that type. */
15151 TYPE_LENGTH (type) = 0;
15152 }
15153 }
15154 else
15155 {
15156 TYPE_LENGTH (type) = 0;
15157 }
15158
15159 maybe_set_alignment (cu, die, type);
15160
15161 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15162 {
15163 /* ICC<14 does not output the required DW_AT_declaration on
15164 incomplete types, but gives them a size of zero. */
15165 TYPE_STUB (type) = 1;
15166 }
15167 else
15168 TYPE_STUB_SUPPORTED (type) = 1;
15169
15170 if (die_is_declaration (die, cu))
15171 TYPE_STUB (type) = 1;
15172 else if (attr == NULL && die->child == NULL
15173 && producer_is_realview (cu->producer))
15174 /* RealView does not output the required DW_AT_declaration
15175 on incomplete types. */
15176 TYPE_STUB (type) = 1;
15177
15178 /* We need to add the type field to the die immediately so we don't
15179 infinitely recurse when dealing with pointers to the structure
15180 type within the structure itself. */
15181 set_die_type (die, type, cu);
15182
15183 /* set_die_type should be already done. */
15184 set_descriptive_type (type, die, cu);
15185
15186 return type;
15187 }
15188
15189 /* A helper for process_structure_scope that handles a single member
15190 DIE. */
15191
15192 static void
15193 handle_struct_member_die (struct die_info *child_die, struct type *type,
15194 struct field_info *fi,
15195 std::vector<struct symbol *> *template_args,
15196 struct dwarf2_cu *cu)
15197 {
15198 if (child_die->tag == DW_TAG_member
15199 || child_die->tag == DW_TAG_variable
15200 || child_die->tag == DW_TAG_variant_part)
15201 {
15202 /* NOTE: carlton/2002-11-05: A C++ static data member
15203 should be a DW_TAG_member that is a declaration, but
15204 all versions of G++ as of this writing (so through at
15205 least 3.2.1) incorrectly generate DW_TAG_variable
15206 tags for them instead. */
15207 dwarf2_add_field (fi, child_die, cu);
15208 }
15209 else if (child_die->tag == DW_TAG_subprogram)
15210 {
15211 /* Rust doesn't have member functions in the C++ sense.
15212 However, it does emit ordinary functions as children
15213 of a struct DIE. */
15214 if (cu->language == language_rust)
15215 read_func_scope (child_die, cu);
15216 else
15217 {
15218 /* C++ member function. */
15219 dwarf2_add_member_fn (fi, child_die, type, cu);
15220 }
15221 }
15222 else if (child_die->tag == DW_TAG_inheritance)
15223 {
15224 /* C++ base class field. */
15225 dwarf2_add_field (fi, child_die, cu);
15226 }
15227 else if (type_can_define_types (child_die))
15228 dwarf2_add_type_defn (fi, child_die, cu);
15229 else if (child_die->tag == DW_TAG_template_type_param
15230 || child_die->tag == DW_TAG_template_value_param)
15231 {
15232 struct symbol *arg = new_symbol (child_die, NULL, cu);
15233
15234 if (arg != NULL)
15235 template_args->push_back (arg);
15236 }
15237 else if (child_die->tag == DW_TAG_variant)
15238 {
15239 /* In a variant we want to get the discriminant and also add a
15240 field for our sole member child. */
15241 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15242
15243 for (die_info *variant_child = child_die->child;
15244 variant_child != NULL;
15245 variant_child = sibling_die (variant_child))
15246 {
15247 if (variant_child->tag == DW_TAG_member)
15248 {
15249 handle_struct_member_die (variant_child, type, fi,
15250 template_args, cu);
15251 /* Only handle the one. */
15252 break;
15253 }
15254 }
15255
15256 /* We don't handle this but we might as well report it if we see
15257 it. */
15258 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15259 complaint (_("DW_AT_discr_list is not supported yet"
15260 " - DIE at %s [in module %s]"),
15261 sect_offset_str (child_die->sect_off),
15262 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15263
15264 /* The first field was just added, so we can stash the
15265 discriminant there. */
15266 gdb_assert (!fi->fields.empty ());
15267 if (discr == NULL)
15268 fi->fields.back ().variant.default_branch = true;
15269 else
15270 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15271 }
15272 }
15273
15274 /* Finish creating a structure or union type, including filling in
15275 its members and creating a symbol for it. */
15276
15277 static void
15278 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15279 {
15280 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15281 struct die_info *child_die;
15282 struct type *type;
15283
15284 type = get_die_type (die, cu);
15285 if (type == NULL)
15286 type = read_structure_type (die, cu);
15287
15288 /* When reading a DW_TAG_variant_part, we need to notice when we
15289 read the discriminant member, so we can record it later in the
15290 discriminant_info. */
15291 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15292 sect_offset discr_offset {};
15293 bool has_template_parameters = false;
15294
15295 if (is_variant_part)
15296 {
15297 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15298 if (discr == NULL)
15299 {
15300 /* Maybe it's a univariant form, an extension we support.
15301 In this case arrange not to check the offset. */
15302 is_variant_part = false;
15303 }
15304 else if (discr->form_is_ref ())
15305 {
15306 struct dwarf2_cu *target_cu = cu;
15307 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15308
15309 discr_offset = target_die->sect_off;
15310 }
15311 else
15312 {
15313 complaint (_("DW_AT_discr does not have DIE reference form"
15314 " - DIE at %s [in module %s]"),
15315 sect_offset_str (die->sect_off),
15316 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15317 is_variant_part = false;
15318 }
15319 }
15320
15321 if (die->child != NULL && ! die_is_declaration (die, cu))
15322 {
15323 struct field_info fi;
15324 std::vector<struct symbol *> template_args;
15325
15326 child_die = die->child;
15327
15328 while (child_die && child_die->tag)
15329 {
15330 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15331
15332 if (is_variant_part && discr_offset == child_die->sect_off)
15333 fi.fields.back ().variant.is_discriminant = true;
15334
15335 child_die = sibling_die (child_die);
15336 }
15337
15338 /* Attach template arguments to type. */
15339 if (!template_args.empty ())
15340 {
15341 has_template_parameters = true;
15342 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15343 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15344 TYPE_TEMPLATE_ARGUMENTS (type)
15345 = XOBNEWVEC (&objfile->objfile_obstack,
15346 struct symbol *,
15347 TYPE_N_TEMPLATE_ARGUMENTS (type));
15348 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15349 template_args.data (),
15350 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15351 * sizeof (struct symbol *)));
15352 }
15353
15354 /* Attach fields and member functions to the type. */
15355 if (fi.nfields () > 0)
15356 dwarf2_attach_fields_to_type (&fi, type, cu);
15357 if (!fi.fnfieldlists.empty ())
15358 {
15359 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15360
15361 /* Get the type which refers to the base class (possibly this
15362 class itself) which contains the vtable pointer for the current
15363 class from the DW_AT_containing_type attribute. This use of
15364 DW_AT_containing_type is a GNU extension. */
15365
15366 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15367 {
15368 struct type *t = die_containing_type (die, cu);
15369
15370 set_type_vptr_basetype (type, t);
15371 if (type == t)
15372 {
15373 int i;
15374
15375 /* Our own class provides vtbl ptr. */
15376 for (i = TYPE_NFIELDS (t) - 1;
15377 i >= TYPE_N_BASECLASSES (t);
15378 --i)
15379 {
15380 const char *fieldname = TYPE_FIELD_NAME (t, i);
15381
15382 if (is_vtable_name (fieldname, cu))
15383 {
15384 set_type_vptr_fieldno (type, i);
15385 break;
15386 }
15387 }
15388
15389 /* Complain if virtual function table field not found. */
15390 if (i < TYPE_N_BASECLASSES (t))
15391 complaint (_("virtual function table pointer "
15392 "not found when defining class '%s'"),
15393 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15394 }
15395 else
15396 {
15397 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15398 }
15399 }
15400 else if (cu->producer
15401 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15402 {
15403 /* The IBM XLC compiler does not provide direct indication
15404 of the containing type, but the vtable pointer is
15405 always named __vfp. */
15406
15407 int i;
15408
15409 for (i = TYPE_NFIELDS (type) - 1;
15410 i >= TYPE_N_BASECLASSES (type);
15411 --i)
15412 {
15413 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15414 {
15415 set_type_vptr_fieldno (type, i);
15416 set_type_vptr_basetype (type, type);
15417 break;
15418 }
15419 }
15420 }
15421 }
15422
15423 /* Copy fi.typedef_field_list linked list elements content into the
15424 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15425 if (!fi.typedef_field_list.empty ())
15426 {
15427 int count = fi.typedef_field_list.size ();
15428
15429 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15430 TYPE_TYPEDEF_FIELD_ARRAY (type)
15431 = ((struct decl_field *)
15432 TYPE_ALLOC (type,
15433 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15434 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15435
15436 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15437 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15438 }
15439
15440 /* Copy fi.nested_types_list linked list elements content into the
15441 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15442 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15443 {
15444 int count = fi.nested_types_list.size ();
15445
15446 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15447 TYPE_NESTED_TYPES_ARRAY (type)
15448 = ((struct decl_field *)
15449 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15450 TYPE_NESTED_TYPES_COUNT (type) = count;
15451
15452 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15453 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15454 }
15455 }
15456
15457 quirk_gcc_member_function_pointer (type, objfile);
15458 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15459 cu->rust_unions.push_back (type);
15460
15461 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15462 snapshots) has been known to create a die giving a declaration
15463 for a class that has, as a child, a die giving a definition for a
15464 nested class. So we have to process our children even if the
15465 current die is a declaration. Normally, of course, a declaration
15466 won't have any children at all. */
15467
15468 child_die = die->child;
15469
15470 while (child_die != NULL && child_die->tag)
15471 {
15472 if (child_die->tag == DW_TAG_member
15473 || child_die->tag == DW_TAG_variable
15474 || child_die->tag == DW_TAG_inheritance
15475 || child_die->tag == DW_TAG_template_value_param
15476 || child_die->tag == DW_TAG_template_type_param)
15477 {
15478 /* Do nothing. */
15479 }
15480 else
15481 process_die (child_die, cu);
15482
15483 child_die = sibling_die (child_die);
15484 }
15485
15486 /* Do not consider external references. According to the DWARF standard,
15487 these DIEs are identified by the fact that they have no byte_size
15488 attribute, and a declaration attribute. */
15489 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15490 || !die_is_declaration (die, cu))
15491 {
15492 struct symbol *sym = new_symbol (die, type, cu);
15493
15494 if (has_template_parameters)
15495 {
15496 struct symtab *symtab;
15497 if (sym != nullptr)
15498 symtab = symbol_symtab (sym);
15499 else if (cu->line_header != nullptr)
15500 {
15501 /* Any related symtab will do. */
15502 symtab
15503 = cu->line_header->file_names ()[0].symtab;
15504 }
15505 else
15506 {
15507 symtab = nullptr;
15508 complaint (_("could not find suitable "
15509 "symtab for template parameter"
15510 " - DIE at %s [in module %s]"),
15511 sect_offset_str (die->sect_off),
15512 objfile_name (objfile));
15513 }
15514
15515 if (symtab != nullptr)
15516 {
15517 /* Make sure that the symtab is set on the new symbols.
15518 Even though they don't appear in this symtab directly,
15519 other parts of gdb assume that symbols do, and this is
15520 reasonably true. */
15521 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15522 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15523 }
15524 }
15525 }
15526 }
15527
15528 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15529 update TYPE using some information only available in DIE's children. */
15530
15531 static void
15532 update_enumeration_type_from_children (struct die_info *die,
15533 struct type *type,
15534 struct dwarf2_cu *cu)
15535 {
15536 struct die_info *child_die;
15537 int unsigned_enum = 1;
15538 int flag_enum = 1;
15539
15540 auto_obstack obstack;
15541
15542 for (child_die = die->child;
15543 child_die != NULL && child_die->tag;
15544 child_die = sibling_die (child_die))
15545 {
15546 struct attribute *attr;
15547 LONGEST value;
15548 const gdb_byte *bytes;
15549 struct dwarf2_locexpr_baton *baton;
15550 const char *name;
15551
15552 if (child_die->tag != DW_TAG_enumerator)
15553 continue;
15554
15555 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15556 if (attr == NULL)
15557 continue;
15558
15559 name = dwarf2_name (child_die, cu);
15560 if (name == NULL)
15561 name = "<anonymous enumerator>";
15562
15563 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15564 &value, &bytes, &baton);
15565 if (value < 0)
15566 {
15567 unsigned_enum = 0;
15568 flag_enum = 0;
15569 }
15570 else
15571 {
15572 if (count_one_bits_ll (value) >= 2)
15573 flag_enum = 0;
15574 }
15575
15576 /* If we already know that the enum type is neither unsigned, nor
15577 a flag type, no need to look at the rest of the enumerates. */
15578 if (!unsigned_enum && !flag_enum)
15579 break;
15580 }
15581
15582 if (unsigned_enum)
15583 TYPE_UNSIGNED (type) = 1;
15584 if (flag_enum)
15585 TYPE_FLAG_ENUM (type) = 1;
15586 }
15587
15588 /* Given a DW_AT_enumeration_type die, set its type. We do not
15589 complete the type's fields yet, or create any symbols. */
15590
15591 static struct type *
15592 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15593 {
15594 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15595 struct type *type;
15596 struct attribute *attr;
15597 const char *name;
15598
15599 /* If the definition of this type lives in .debug_types, read that type.
15600 Don't follow DW_AT_specification though, that will take us back up
15601 the chain and we want to go down. */
15602 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15603 if (attr != nullptr)
15604 {
15605 type = get_DW_AT_signature_type (die, attr, cu);
15606
15607 /* The type's CU may not be the same as CU.
15608 Ensure TYPE is recorded with CU in die_type_hash. */
15609 return set_die_type (die, type, cu);
15610 }
15611
15612 type = alloc_type (objfile);
15613
15614 TYPE_CODE (type) = TYPE_CODE_ENUM;
15615 name = dwarf2_full_name (NULL, die, cu);
15616 if (name != NULL)
15617 TYPE_NAME (type) = name;
15618
15619 attr = dwarf2_attr (die, DW_AT_type, cu);
15620 if (attr != NULL)
15621 {
15622 struct type *underlying_type = die_type (die, cu);
15623
15624 TYPE_TARGET_TYPE (type) = underlying_type;
15625 }
15626
15627 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15628 if (attr != nullptr)
15629 {
15630 TYPE_LENGTH (type) = DW_UNSND (attr);
15631 }
15632 else
15633 {
15634 TYPE_LENGTH (type) = 0;
15635 }
15636
15637 maybe_set_alignment (cu, die, type);
15638
15639 /* The enumeration DIE can be incomplete. In Ada, any type can be
15640 declared as private in the package spec, and then defined only
15641 inside the package body. Such types are known as Taft Amendment
15642 Types. When another package uses such a type, an incomplete DIE
15643 may be generated by the compiler. */
15644 if (die_is_declaration (die, cu))
15645 TYPE_STUB (type) = 1;
15646
15647 /* Finish the creation of this type by using the enum's children.
15648 We must call this even when the underlying type has been provided
15649 so that we can determine if we're looking at a "flag" enum. */
15650 update_enumeration_type_from_children (die, type, cu);
15651
15652 /* If this type has an underlying type that is not a stub, then we
15653 may use its attributes. We always use the "unsigned" attribute
15654 in this situation, because ordinarily we guess whether the type
15655 is unsigned -- but the guess can be wrong and the underlying type
15656 can tell us the reality. However, we defer to a local size
15657 attribute if one exists, because this lets the compiler override
15658 the underlying type if needed. */
15659 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15660 {
15661 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15662 if (TYPE_LENGTH (type) == 0)
15663 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15664 if (TYPE_RAW_ALIGN (type) == 0
15665 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15666 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15667 }
15668
15669 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15670
15671 return set_die_type (die, type, cu);
15672 }
15673
15674 /* Given a pointer to a die which begins an enumeration, process all
15675 the dies that define the members of the enumeration, and create the
15676 symbol for the enumeration type.
15677
15678 NOTE: We reverse the order of the element list. */
15679
15680 static void
15681 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15682 {
15683 struct type *this_type;
15684
15685 this_type = get_die_type (die, cu);
15686 if (this_type == NULL)
15687 this_type = read_enumeration_type (die, cu);
15688
15689 if (die->child != NULL)
15690 {
15691 struct die_info *child_die;
15692 struct symbol *sym;
15693 std::vector<struct field> fields;
15694 const char *name;
15695
15696 child_die = die->child;
15697 while (child_die && child_die->tag)
15698 {
15699 if (child_die->tag != DW_TAG_enumerator)
15700 {
15701 process_die (child_die, cu);
15702 }
15703 else
15704 {
15705 name = dwarf2_name (child_die, cu);
15706 if (name)
15707 {
15708 sym = new_symbol (child_die, this_type, cu);
15709
15710 fields.emplace_back ();
15711 struct field &field = fields.back ();
15712
15713 FIELD_NAME (field) = sym->linkage_name ();
15714 FIELD_TYPE (field) = NULL;
15715 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15716 FIELD_BITSIZE (field) = 0;
15717 }
15718 }
15719
15720 child_die = sibling_die (child_die);
15721 }
15722
15723 if (!fields.empty ())
15724 {
15725 TYPE_NFIELDS (this_type) = fields.size ();
15726 TYPE_FIELDS (this_type) = (struct field *)
15727 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15728 memcpy (TYPE_FIELDS (this_type), fields.data (),
15729 sizeof (struct field) * fields.size ());
15730 }
15731 }
15732
15733 /* If we are reading an enum from a .debug_types unit, and the enum
15734 is a declaration, and the enum is not the signatured type in the
15735 unit, then we do not want to add a symbol for it. Adding a
15736 symbol would in some cases obscure the true definition of the
15737 enum, giving users an incomplete type when the definition is
15738 actually available. Note that we do not want to do this for all
15739 enums which are just declarations, because C++0x allows forward
15740 enum declarations. */
15741 if (cu->per_cu->is_debug_types
15742 && die_is_declaration (die, cu))
15743 {
15744 struct signatured_type *sig_type;
15745
15746 sig_type = (struct signatured_type *) cu->per_cu;
15747 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15748 if (sig_type->type_offset_in_section != die->sect_off)
15749 return;
15750 }
15751
15752 new_symbol (die, this_type, cu);
15753 }
15754
15755 /* Extract all information from a DW_TAG_array_type DIE and put it in
15756 the DIE's type field. For now, this only handles one dimensional
15757 arrays. */
15758
15759 static struct type *
15760 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15761 {
15762 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15763 struct die_info *child_die;
15764 struct type *type;
15765 struct type *element_type, *range_type, *index_type;
15766 struct attribute *attr;
15767 const char *name;
15768 struct dynamic_prop *byte_stride_prop = NULL;
15769 unsigned int bit_stride = 0;
15770
15771 element_type = die_type (die, cu);
15772
15773 /* The die_type call above may have already set the type for this DIE. */
15774 type = get_die_type (die, cu);
15775 if (type)
15776 return type;
15777
15778 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15779 if (attr != NULL)
15780 {
15781 int stride_ok;
15782 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15783
15784 byte_stride_prop
15785 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15786 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15787 prop_type);
15788 if (!stride_ok)
15789 {
15790 complaint (_("unable to read array DW_AT_byte_stride "
15791 " - DIE at %s [in module %s]"),
15792 sect_offset_str (die->sect_off),
15793 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15794 /* Ignore this attribute. We will likely not be able to print
15795 arrays of this type correctly, but there is little we can do
15796 to help if we cannot read the attribute's value. */
15797 byte_stride_prop = NULL;
15798 }
15799 }
15800
15801 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15802 if (attr != NULL)
15803 bit_stride = DW_UNSND (attr);
15804
15805 /* Irix 6.2 native cc creates array types without children for
15806 arrays with unspecified length. */
15807 if (die->child == NULL)
15808 {
15809 index_type = objfile_type (objfile)->builtin_int;
15810 range_type = create_static_range_type (NULL, index_type, 0, -1);
15811 type = create_array_type_with_stride (NULL, element_type, range_type,
15812 byte_stride_prop, bit_stride);
15813 return set_die_type (die, type, cu);
15814 }
15815
15816 std::vector<struct type *> range_types;
15817 child_die = die->child;
15818 while (child_die && child_die->tag)
15819 {
15820 if (child_die->tag == DW_TAG_subrange_type)
15821 {
15822 struct type *child_type = read_type_die (child_die, cu);
15823
15824 if (child_type != NULL)
15825 {
15826 /* The range type was succesfully read. Save it for the
15827 array type creation. */
15828 range_types.push_back (child_type);
15829 }
15830 }
15831 child_die = sibling_die (child_die);
15832 }
15833
15834 /* Dwarf2 dimensions are output from left to right, create the
15835 necessary array types in backwards order. */
15836
15837 type = element_type;
15838
15839 if (read_array_order (die, cu) == DW_ORD_col_major)
15840 {
15841 int i = 0;
15842
15843 while (i < range_types.size ())
15844 type = create_array_type_with_stride (NULL, type, range_types[i++],
15845 byte_stride_prop, bit_stride);
15846 }
15847 else
15848 {
15849 size_t ndim = range_types.size ();
15850 while (ndim-- > 0)
15851 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15852 byte_stride_prop, bit_stride);
15853 }
15854
15855 /* Understand Dwarf2 support for vector types (like they occur on
15856 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15857 array type. This is not part of the Dwarf2/3 standard yet, but a
15858 custom vendor extension. The main difference between a regular
15859 array and the vector variant is that vectors are passed by value
15860 to functions. */
15861 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15862 if (attr != nullptr)
15863 make_vector_type (type);
15864
15865 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15866 implementation may choose to implement triple vectors using this
15867 attribute. */
15868 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15869 if (attr != nullptr)
15870 {
15871 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15872 TYPE_LENGTH (type) = DW_UNSND (attr);
15873 else
15874 complaint (_("DW_AT_byte_size for array type smaller "
15875 "than the total size of elements"));
15876 }
15877
15878 name = dwarf2_name (die, cu);
15879 if (name)
15880 TYPE_NAME (type) = name;
15881
15882 maybe_set_alignment (cu, die, type);
15883
15884 /* Install the type in the die. */
15885 set_die_type (die, type, cu);
15886
15887 /* set_die_type should be already done. */
15888 set_descriptive_type (type, die, cu);
15889
15890 return type;
15891 }
15892
15893 static enum dwarf_array_dim_ordering
15894 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15895 {
15896 struct attribute *attr;
15897
15898 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15899
15900 if (attr != nullptr)
15901 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15902
15903 /* GNU F77 is a special case, as at 08/2004 array type info is the
15904 opposite order to the dwarf2 specification, but data is still
15905 laid out as per normal fortran.
15906
15907 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15908 version checking. */
15909
15910 if (cu->language == language_fortran
15911 && cu->producer && strstr (cu->producer, "GNU F77"))
15912 {
15913 return DW_ORD_row_major;
15914 }
15915
15916 switch (cu->language_defn->la_array_ordering)
15917 {
15918 case array_column_major:
15919 return DW_ORD_col_major;
15920 case array_row_major:
15921 default:
15922 return DW_ORD_row_major;
15923 };
15924 }
15925
15926 /* Extract all information from a DW_TAG_set_type DIE and put it in
15927 the DIE's type field. */
15928
15929 static struct type *
15930 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15931 {
15932 struct type *domain_type, *set_type;
15933 struct attribute *attr;
15934
15935 domain_type = die_type (die, cu);
15936
15937 /* The die_type call above may have already set the type for this DIE. */
15938 set_type = get_die_type (die, cu);
15939 if (set_type)
15940 return set_type;
15941
15942 set_type = create_set_type (NULL, domain_type);
15943
15944 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15945 if (attr != nullptr)
15946 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15947
15948 maybe_set_alignment (cu, die, set_type);
15949
15950 return set_die_type (die, set_type, cu);
15951 }
15952
15953 /* A helper for read_common_block that creates a locexpr baton.
15954 SYM is the symbol which we are marking as computed.
15955 COMMON_DIE is the DIE for the common block.
15956 COMMON_LOC is the location expression attribute for the common
15957 block itself.
15958 MEMBER_LOC is the location expression attribute for the particular
15959 member of the common block that we are processing.
15960 CU is the CU from which the above come. */
15961
15962 static void
15963 mark_common_block_symbol_computed (struct symbol *sym,
15964 struct die_info *common_die,
15965 struct attribute *common_loc,
15966 struct attribute *member_loc,
15967 struct dwarf2_cu *cu)
15968 {
15969 struct dwarf2_per_objfile *dwarf2_per_objfile
15970 = cu->per_cu->dwarf2_per_objfile;
15971 struct objfile *objfile = dwarf2_per_objfile->objfile;
15972 struct dwarf2_locexpr_baton *baton;
15973 gdb_byte *ptr;
15974 unsigned int cu_off;
15975 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15976 LONGEST offset = 0;
15977
15978 gdb_assert (common_loc && member_loc);
15979 gdb_assert (common_loc->form_is_block ());
15980 gdb_assert (member_loc->form_is_block ()
15981 || member_loc->form_is_constant ());
15982
15983 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15984 baton->per_cu = cu->per_cu;
15985 gdb_assert (baton->per_cu);
15986
15987 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15988
15989 if (member_loc->form_is_constant ())
15990 {
15991 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15992 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15993 }
15994 else
15995 baton->size += DW_BLOCK (member_loc)->size;
15996
15997 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15998 baton->data = ptr;
15999
16000 *ptr++ = DW_OP_call4;
16001 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16002 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16003 ptr += 4;
16004
16005 if (member_loc->form_is_constant ())
16006 {
16007 *ptr++ = DW_OP_addr;
16008 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16009 ptr += cu->header.addr_size;
16010 }
16011 else
16012 {
16013 /* We have to copy the data here, because DW_OP_call4 will only
16014 use a DW_AT_location attribute. */
16015 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16016 ptr += DW_BLOCK (member_loc)->size;
16017 }
16018
16019 *ptr++ = DW_OP_plus;
16020 gdb_assert (ptr - baton->data == baton->size);
16021
16022 SYMBOL_LOCATION_BATON (sym) = baton;
16023 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16024 }
16025
16026 /* Create appropriate locally-scoped variables for all the
16027 DW_TAG_common_block entries. Also create a struct common_block
16028 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16029 is used to separate the common blocks name namespace from regular
16030 variable names. */
16031
16032 static void
16033 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16034 {
16035 struct attribute *attr;
16036
16037 attr = dwarf2_attr (die, DW_AT_location, cu);
16038 if (attr != nullptr)
16039 {
16040 /* Support the .debug_loc offsets. */
16041 if (attr->form_is_block ())
16042 {
16043 /* Ok. */
16044 }
16045 else if (attr->form_is_section_offset ())
16046 {
16047 dwarf2_complex_location_expr_complaint ();
16048 attr = NULL;
16049 }
16050 else
16051 {
16052 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16053 "common block member");
16054 attr = NULL;
16055 }
16056 }
16057
16058 if (die->child != NULL)
16059 {
16060 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16061 struct die_info *child_die;
16062 size_t n_entries = 0, size;
16063 struct common_block *common_block;
16064 struct symbol *sym;
16065
16066 for (child_die = die->child;
16067 child_die && child_die->tag;
16068 child_die = sibling_die (child_die))
16069 ++n_entries;
16070
16071 size = (sizeof (struct common_block)
16072 + (n_entries - 1) * sizeof (struct symbol *));
16073 common_block
16074 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16075 size);
16076 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16077 common_block->n_entries = 0;
16078
16079 for (child_die = die->child;
16080 child_die && child_die->tag;
16081 child_die = sibling_die (child_die))
16082 {
16083 /* Create the symbol in the DW_TAG_common_block block in the current
16084 symbol scope. */
16085 sym = new_symbol (child_die, NULL, cu);
16086 if (sym != NULL)
16087 {
16088 struct attribute *member_loc;
16089
16090 common_block->contents[common_block->n_entries++] = sym;
16091
16092 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16093 cu);
16094 if (member_loc)
16095 {
16096 /* GDB has handled this for a long time, but it is
16097 not specified by DWARF. It seems to have been
16098 emitted by gfortran at least as recently as:
16099 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16100 complaint (_("Variable in common block has "
16101 "DW_AT_data_member_location "
16102 "- DIE at %s [in module %s]"),
16103 sect_offset_str (child_die->sect_off),
16104 objfile_name (objfile));
16105
16106 if (member_loc->form_is_section_offset ())
16107 dwarf2_complex_location_expr_complaint ();
16108 else if (member_loc->form_is_constant ()
16109 || member_loc->form_is_block ())
16110 {
16111 if (attr != nullptr)
16112 mark_common_block_symbol_computed (sym, die, attr,
16113 member_loc, cu);
16114 }
16115 else
16116 dwarf2_complex_location_expr_complaint ();
16117 }
16118 }
16119 }
16120
16121 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16122 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16123 }
16124 }
16125
16126 /* Create a type for a C++ namespace. */
16127
16128 static struct type *
16129 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16130 {
16131 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16132 const char *previous_prefix, *name;
16133 int is_anonymous;
16134 struct type *type;
16135
16136 /* For extensions, reuse the type of the original namespace. */
16137 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16138 {
16139 struct die_info *ext_die;
16140 struct dwarf2_cu *ext_cu = cu;
16141
16142 ext_die = dwarf2_extension (die, &ext_cu);
16143 type = read_type_die (ext_die, ext_cu);
16144
16145 /* EXT_CU may not be the same as CU.
16146 Ensure TYPE is recorded with CU in die_type_hash. */
16147 return set_die_type (die, type, cu);
16148 }
16149
16150 name = namespace_name (die, &is_anonymous, cu);
16151
16152 /* Now build the name of the current namespace. */
16153
16154 previous_prefix = determine_prefix (die, cu);
16155 if (previous_prefix[0] != '\0')
16156 name = typename_concat (&objfile->objfile_obstack,
16157 previous_prefix, name, 0, cu);
16158
16159 /* Create the type. */
16160 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16161
16162 return set_die_type (die, type, cu);
16163 }
16164
16165 /* Read a namespace scope. */
16166
16167 static void
16168 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16169 {
16170 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16171 int is_anonymous;
16172
16173 /* Add a symbol associated to this if we haven't seen the namespace
16174 before. Also, add a using directive if it's an anonymous
16175 namespace. */
16176
16177 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16178 {
16179 struct type *type;
16180
16181 type = read_type_die (die, cu);
16182 new_symbol (die, type, cu);
16183
16184 namespace_name (die, &is_anonymous, cu);
16185 if (is_anonymous)
16186 {
16187 const char *previous_prefix = determine_prefix (die, cu);
16188
16189 std::vector<const char *> excludes;
16190 add_using_directive (using_directives (cu),
16191 previous_prefix, TYPE_NAME (type), NULL,
16192 NULL, excludes, 0, &objfile->objfile_obstack);
16193 }
16194 }
16195
16196 if (die->child != NULL)
16197 {
16198 struct die_info *child_die = die->child;
16199
16200 while (child_die && child_die->tag)
16201 {
16202 process_die (child_die, cu);
16203 child_die = sibling_die (child_die);
16204 }
16205 }
16206 }
16207
16208 /* Read a Fortran module as type. This DIE can be only a declaration used for
16209 imported module. Still we need that type as local Fortran "use ... only"
16210 declaration imports depend on the created type in determine_prefix. */
16211
16212 static struct type *
16213 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16214 {
16215 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16216 const char *module_name;
16217 struct type *type;
16218
16219 module_name = dwarf2_name (die, cu);
16220 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16221
16222 return set_die_type (die, type, cu);
16223 }
16224
16225 /* Read a Fortran module. */
16226
16227 static void
16228 read_module (struct die_info *die, struct dwarf2_cu *cu)
16229 {
16230 struct die_info *child_die = die->child;
16231 struct type *type;
16232
16233 type = read_type_die (die, cu);
16234 new_symbol (die, type, cu);
16235
16236 while (child_die && child_die->tag)
16237 {
16238 process_die (child_die, cu);
16239 child_die = sibling_die (child_die);
16240 }
16241 }
16242
16243 /* Return the name of the namespace represented by DIE. Set
16244 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16245 namespace. */
16246
16247 static const char *
16248 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16249 {
16250 struct die_info *current_die;
16251 const char *name = NULL;
16252
16253 /* Loop through the extensions until we find a name. */
16254
16255 for (current_die = die;
16256 current_die != NULL;
16257 current_die = dwarf2_extension (die, &cu))
16258 {
16259 /* We don't use dwarf2_name here so that we can detect the absence
16260 of a name -> anonymous namespace. */
16261 name = dwarf2_string_attr (die, DW_AT_name, cu);
16262
16263 if (name != NULL)
16264 break;
16265 }
16266
16267 /* Is it an anonymous namespace? */
16268
16269 *is_anonymous = (name == NULL);
16270 if (*is_anonymous)
16271 name = CP_ANONYMOUS_NAMESPACE_STR;
16272
16273 return name;
16274 }
16275
16276 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16277 the user defined type vector. */
16278
16279 static struct type *
16280 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16281 {
16282 struct gdbarch *gdbarch
16283 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16284 struct comp_unit_head *cu_header = &cu->header;
16285 struct type *type;
16286 struct attribute *attr_byte_size;
16287 struct attribute *attr_address_class;
16288 int byte_size, addr_class;
16289 struct type *target_type;
16290
16291 target_type = die_type (die, cu);
16292
16293 /* The die_type call above may have already set the type for this DIE. */
16294 type = get_die_type (die, cu);
16295 if (type)
16296 return type;
16297
16298 type = lookup_pointer_type (target_type);
16299
16300 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16301 if (attr_byte_size)
16302 byte_size = DW_UNSND (attr_byte_size);
16303 else
16304 byte_size = cu_header->addr_size;
16305
16306 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16307 if (attr_address_class)
16308 addr_class = DW_UNSND (attr_address_class);
16309 else
16310 addr_class = DW_ADDR_none;
16311
16312 ULONGEST alignment = get_alignment (cu, die);
16313
16314 /* If the pointer size, alignment, or address class is different
16315 than the default, create a type variant marked as such and set
16316 the length accordingly. */
16317 if (TYPE_LENGTH (type) != byte_size
16318 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16319 && alignment != TYPE_RAW_ALIGN (type))
16320 || addr_class != DW_ADDR_none)
16321 {
16322 if (gdbarch_address_class_type_flags_p (gdbarch))
16323 {
16324 int type_flags;
16325
16326 type_flags = gdbarch_address_class_type_flags
16327 (gdbarch, byte_size, addr_class);
16328 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16329 == 0);
16330 type = make_type_with_address_space (type, type_flags);
16331 }
16332 else if (TYPE_LENGTH (type) != byte_size)
16333 {
16334 complaint (_("invalid pointer size %d"), byte_size);
16335 }
16336 else if (TYPE_RAW_ALIGN (type) != alignment)
16337 {
16338 complaint (_("Invalid DW_AT_alignment"
16339 " - DIE at %s [in module %s]"),
16340 sect_offset_str (die->sect_off),
16341 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16342 }
16343 else
16344 {
16345 /* Should we also complain about unhandled address classes? */
16346 }
16347 }
16348
16349 TYPE_LENGTH (type) = byte_size;
16350 set_type_align (type, alignment);
16351 return set_die_type (die, type, cu);
16352 }
16353
16354 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16355 the user defined type vector. */
16356
16357 static struct type *
16358 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16359 {
16360 struct type *type;
16361 struct type *to_type;
16362 struct type *domain;
16363
16364 to_type = die_type (die, cu);
16365 domain = die_containing_type (die, cu);
16366
16367 /* The calls above may have already set the type for this DIE. */
16368 type = get_die_type (die, cu);
16369 if (type)
16370 return type;
16371
16372 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16373 type = lookup_methodptr_type (to_type);
16374 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16375 {
16376 struct type *new_type
16377 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16378
16379 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16380 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16381 TYPE_VARARGS (to_type));
16382 type = lookup_methodptr_type (new_type);
16383 }
16384 else
16385 type = lookup_memberptr_type (to_type, domain);
16386
16387 return set_die_type (die, type, cu);
16388 }
16389
16390 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16391 the user defined type vector. */
16392
16393 static struct type *
16394 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16395 enum type_code refcode)
16396 {
16397 struct comp_unit_head *cu_header = &cu->header;
16398 struct type *type, *target_type;
16399 struct attribute *attr;
16400
16401 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16402
16403 target_type = die_type (die, cu);
16404
16405 /* The die_type call above may have already set the type for this DIE. */
16406 type = get_die_type (die, cu);
16407 if (type)
16408 return type;
16409
16410 type = lookup_reference_type (target_type, refcode);
16411 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16412 if (attr != nullptr)
16413 {
16414 TYPE_LENGTH (type) = DW_UNSND (attr);
16415 }
16416 else
16417 {
16418 TYPE_LENGTH (type) = cu_header->addr_size;
16419 }
16420 maybe_set_alignment (cu, die, type);
16421 return set_die_type (die, type, cu);
16422 }
16423
16424 /* Add the given cv-qualifiers to the element type of the array. GCC
16425 outputs DWARF type qualifiers that apply to an array, not the
16426 element type. But GDB relies on the array element type to carry
16427 the cv-qualifiers. This mimics section 6.7.3 of the C99
16428 specification. */
16429
16430 static struct type *
16431 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16432 struct type *base_type, int cnst, int voltl)
16433 {
16434 struct type *el_type, *inner_array;
16435
16436 base_type = copy_type (base_type);
16437 inner_array = base_type;
16438
16439 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16440 {
16441 TYPE_TARGET_TYPE (inner_array) =
16442 copy_type (TYPE_TARGET_TYPE (inner_array));
16443 inner_array = TYPE_TARGET_TYPE (inner_array);
16444 }
16445
16446 el_type = TYPE_TARGET_TYPE (inner_array);
16447 cnst |= TYPE_CONST (el_type);
16448 voltl |= TYPE_VOLATILE (el_type);
16449 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16450
16451 return set_die_type (die, base_type, cu);
16452 }
16453
16454 static struct type *
16455 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16456 {
16457 struct type *base_type, *cv_type;
16458
16459 base_type = die_type (die, cu);
16460
16461 /* The die_type call above may have already set the type for this DIE. */
16462 cv_type = get_die_type (die, cu);
16463 if (cv_type)
16464 return cv_type;
16465
16466 /* In case the const qualifier is applied to an array type, the element type
16467 is so qualified, not the array type (section 6.7.3 of C99). */
16468 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16469 return add_array_cv_type (die, cu, base_type, 1, 0);
16470
16471 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16472 return set_die_type (die, cv_type, cu);
16473 }
16474
16475 static struct type *
16476 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16477 {
16478 struct type *base_type, *cv_type;
16479
16480 base_type = die_type (die, cu);
16481
16482 /* The die_type call above may have already set the type for this DIE. */
16483 cv_type = get_die_type (die, cu);
16484 if (cv_type)
16485 return cv_type;
16486
16487 /* In case the volatile qualifier is applied to an array type, the
16488 element type is so qualified, not the array type (section 6.7.3
16489 of C99). */
16490 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16491 return add_array_cv_type (die, cu, base_type, 0, 1);
16492
16493 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16494 return set_die_type (die, cv_type, cu);
16495 }
16496
16497 /* Handle DW_TAG_restrict_type. */
16498
16499 static struct type *
16500 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16501 {
16502 struct type *base_type, *cv_type;
16503
16504 base_type = die_type (die, cu);
16505
16506 /* The die_type call above may have already set the type for this DIE. */
16507 cv_type = get_die_type (die, cu);
16508 if (cv_type)
16509 return cv_type;
16510
16511 cv_type = make_restrict_type (base_type);
16512 return set_die_type (die, cv_type, cu);
16513 }
16514
16515 /* Handle DW_TAG_atomic_type. */
16516
16517 static struct type *
16518 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16519 {
16520 struct type *base_type, *cv_type;
16521
16522 base_type = die_type (die, cu);
16523
16524 /* The die_type call above may have already set the type for this DIE. */
16525 cv_type = get_die_type (die, cu);
16526 if (cv_type)
16527 return cv_type;
16528
16529 cv_type = make_atomic_type (base_type);
16530 return set_die_type (die, cv_type, cu);
16531 }
16532
16533 /* Extract all information from a DW_TAG_string_type DIE and add to
16534 the user defined type vector. It isn't really a user defined type,
16535 but it behaves like one, with other DIE's using an AT_user_def_type
16536 attribute to reference it. */
16537
16538 static struct type *
16539 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16540 {
16541 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16542 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16543 struct type *type, *range_type, *index_type, *char_type;
16544 struct attribute *attr;
16545 struct dynamic_prop prop;
16546 bool length_is_constant = true;
16547 LONGEST length;
16548
16549 /* There are a couple of places where bit sizes might be made use of
16550 when parsing a DW_TAG_string_type, however, no producer that we know
16551 of make use of these. Handling bit sizes that are a multiple of the
16552 byte size is easy enough, but what about other bit sizes? Lets deal
16553 with that problem when we have to. Warn about these attributes being
16554 unsupported, then parse the type and ignore them like we always
16555 have. */
16556 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16557 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16558 {
16559 static bool warning_printed = false;
16560 if (!warning_printed)
16561 {
16562 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16563 "currently supported on DW_TAG_string_type."));
16564 warning_printed = true;
16565 }
16566 }
16567
16568 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16569 if (attr != nullptr && !attr->form_is_constant ())
16570 {
16571 /* The string length describes the location at which the length of
16572 the string can be found. The size of the length field can be
16573 specified with one of the attributes below. */
16574 struct type *prop_type;
16575 struct attribute *len
16576 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16577 if (len == nullptr)
16578 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16579 if (len != nullptr && len->form_is_constant ())
16580 {
16581 /* Pass 0 as the default as we know this attribute is constant
16582 and the default value will not be returned. */
16583 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16584 prop_type = cu->per_cu->int_type (sz, true);
16585 }
16586 else
16587 {
16588 /* If the size is not specified then we assume it is the size of
16589 an address on this target. */
16590 prop_type = cu->per_cu->addr_sized_int_type (true);
16591 }
16592
16593 /* Convert the attribute into a dynamic property. */
16594 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16595 length = 1;
16596 else
16597 length_is_constant = false;
16598 }
16599 else if (attr != nullptr)
16600 {
16601 /* This DW_AT_string_length just contains the length with no
16602 indirection. There's no need to create a dynamic property in this
16603 case. Pass 0 for the default value as we know it will not be
16604 returned in this case. */
16605 length = dwarf2_get_attr_constant_value (attr, 0);
16606 }
16607 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16608 {
16609 /* We don't currently support non-constant byte sizes for strings. */
16610 length = dwarf2_get_attr_constant_value (attr, 1);
16611 }
16612 else
16613 {
16614 /* Use 1 as a fallback length if we have nothing else. */
16615 length = 1;
16616 }
16617
16618 index_type = objfile_type (objfile)->builtin_int;
16619 if (length_is_constant)
16620 range_type = create_static_range_type (NULL, index_type, 1, length);
16621 else
16622 {
16623 struct dynamic_prop low_bound;
16624
16625 low_bound.kind = PROP_CONST;
16626 low_bound.data.const_val = 1;
16627 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16628 }
16629 char_type = language_string_char_type (cu->language_defn, gdbarch);
16630 type = create_string_type (NULL, char_type, range_type);
16631
16632 return set_die_type (die, type, cu);
16633 }
16634
16635 /* Assuming that DIE corresponds to a function, returns nonzero
16636 if the function is prototyped. */
16637
16638 static int
16639 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16640 {
16641 struct attribute *attr;
16642
16643 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16644 if (attr && (DW_UNSND (attr) != 0))
16645 return 1;
16646
16647 /* The DWARF standard implies that the DW_AT_prototyped attribute
16648 is only meaningful for C, but the concept also extends to other
16649 languages that allow unprototyped functions (Eg: Objective C).
16650 For all other languages, assume that functions are always
16651 prototyped. */
16652 if (cu->language != language_c
16653 && cu->language != language_objc
16654 && cu->language != language_opencl)
16655 return 1;
16656
16657 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16658 prototyped and unprototyped functions; default to prototyped,
16659 since that is more common in modern code (and RealView warns
16660 about unprototyped functions). */
16661 if (producer_is_realview (cu->producer))
16662 return 1;
16663
16664 return 0;
16665 }
16666
16667 /* Handle DIES due to C code like:
16668
16669 struct foo
16670 {
16671 int (*funcp)(int a, long l);
16672 int b;
16673 };
16674
16675 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16676
16677 static struct type *
16678 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16679 {
16680 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16681 struct type *type; /* Type that this function returns. */
16682 struct type *ftype; /* Function that returns above type. */
16683 struct attribute *attr;
16684
16685 type = die_type (die, cu);
16686
16687 /* The die_type call above may have already set the type for this DIE. */
16688 ftype = get_die_type (die, cu);
16689 if (ftype)
16690 return ftype;
16691
16692 ftype = lookup_function_type (type);
16693
16694 if (prototyped_function_p (die, cu))
16695 TYPE_PROTOTYPED (ftype) = 1;
16696
16697 /* Store the calling convention in the type if it's available in
16698 the subroutine die. Otherwise set the calling convention to
16699 the default value DW_CC_normal. */
16700 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16701 if (attr != nullptr
16702 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16703 TYPE_CALLING_CONVENTION (ftype)
16704 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16705 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16706 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16707 else
16708 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16709
16710 /* Record whether the function returns normally to its caller or not
16711 if the DWARF producer set that information. */
16712 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16713 if (attr && (DW_UNSND (attr) != 0))
16714 TYPE_NO_RETURN (ftype) = 1;
16715
16716 /* We need to add the subroutine type to the die immediately so
16717 we don't infinitely recurse when dealing with parameters
16718 declared as the same subroutine type. */
16719 set_die_type (die, ftype, cu);
16720
16721 if (die->child != NULL)
16722 {
16723 struct type *void_type = objfile_type (objfile)->builtin_void;
16724 struct die_info *child_die;
16725 int nparams, iparams;
16726
16727 /* Count the number of parameters.
16728 FIXME: GDB currently ignores vararg functions, but knows about
16729 vararg member functions. */
16730 nparams = 0;
16731 child_die = die->child;
16732 while (child_die && child_die->tag)
16733 {
16734 if (child_die->tag == DW_TAG_formal_parameter)
16735 nparams++;
16736 else if (child_die->tag == DW_TAG_unspecified_parameters)
16737 TYPE_VARARGS (ftype) = 1;
16738 child_die = sibling_die (child_die);
16739 }
16740
16741 /* Allocate storage for parameters and fill them in. */
16742 TYPE_NFIELDS (ftype) = nparams;
16743 TYPE_FIELDS (ftype) = (struct field *)
16744 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16745
16746 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16747 even if we error out during the parameters reading below. */
16748 for (iparams = 0; iparams < nparams; iparams++)
16749 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16750
16751 iparams = 0;
16752 child_die = die->child;
16753 while (child_die && child_die->tag)
16754 {
16755 if (child_die->tag == DW_TAG_formal_parameter)
16756 {
16757 struct type *arg_type;
16758
16759 /* DWARF version 2 has no clean way to discern C++
16760 static and non-static member functions. G++ helps
16761 GDB by marking the first parameter for non-static
16762 member functions (which is the this pointer) as
16763 artificial. We pass this information to
16764 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16765
16766 DWARF version 3 added DW_AT_object_pointer, which GCC
16767 4.5 does not yet generate. */
16768 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16769 if (attr != nullptr)
16770 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16771 else
16772 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16773 arg_type = die_type (child_die, cu);
16774
16775 /* RealView does not mark THIS as const, which the testsuite
16776 expects. GCC marks THIS as const in method definitions,
16777 but not in the class specifications (GCC PR 43053). */
16778 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16779 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16780 {
16781 int is_this = 0;
16782 struct dwarf2_cu *arg_cu = cu;
16783 const char *name = dwarf2_name (child_die, cu);
16784
16785 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16786 if (attr != nullptr)
16787 {
16788 /* If the compiler emits this, use it. */
16789 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16790 is_this = 1;
16791 }
16792 else if (name && strcmp (name, "this") == 0)
16793 /* Function definitions will have the argument names. */
16794 is_this = 1;
16795 else if (name == NULL && iparams == 0)
16796 /* Declarations may not have the names, so like
16797 elsewhere in GDB, assume an artificial first
16798 argument is "this". */
16799 is_this = 1;
16800
16801 if (is_this)
16802 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16803 arg_type, 0);
16804 }
16805
16806 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16807 iparams++;
16808 }
16809 child_die = sibling_die (child_die);
16810 }
16811 }
16812
16813 return ftype;
16814 }
16815
16816 static struct type *
16817 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16818 {
16819 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16820 const char *name = NULL;
16821 struct type *this_type, *target_type;
16822
16823 name = dwarf2_full_name (NULL, die, cu);
16824 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16825 TYPE_TARGET_STUB (this_type) = 1;
16826 set_die_type (die, this_type, cu);
16827 target_type = die_type (die, cu);
16828 if (target_type != this_type)
16829 TYPE_TARGET_TYPE (this_type) = target_type;
16830 else
16831 {
16832 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16833 spec and cause infinite loops in GDB. */
16834 complaint (_("Self-referential DW_TAG_typedef "
16835 "- DIE at %s [in module %s]"),
16836 sect_offset_str (die->sect_off), objfile_name (objfile));
16837 TYPE_TARGET_TYPE (this_type) = NULL;
16838 }
16839 if (name == NULL)
16840 {
16841 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16842 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16843 Handle these by just returning the target type, rather than
16844 constructing an anonymous typedef type and trying to handle this
16845 elsewhere. */
16846 set_die_type (die, target_type, cu);
16847 return target_type;
16848 }
16849 return this_type;
16850 }
16851
16852 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16853 (which may be different from NAME) to the architecture back-end to allow
16854 it to guess the correct format if necessary. */
16855
16856 static struct type *
16857 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16858 const char *name_hint, enum bfd_endian byte_order)
16859 {
16860 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16861 const struct floatformat **format;
16862 struct type *type;
16863
16864 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16865 if (format)
16866 type = init_float_type (objfile, bits, name, format, byte_order);
16867 else
16868 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16869
16870 return type;
16871 }
16872
16873 /* Allocate an integer type of size BITS and name NAME. */
16874
16875 static struct type *
16876 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16877 int bits, int unsigned_p, const char *name)
16878 {
16879 struct type *type;
16880
16881 /* Versions of Intel's C Compiler generate an integer type called "void"
16882 instead of using DW_TAG_unspecified_type. This has been seen on
16883 at least versions 14, 17, and 18. */
16884 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16885 && strcmp (name, "void") == 0)
16886 type = objfile_type (objfile)->builtin_void;
16887 else
16888 type = init_integer_type (objfile, bits, unsigned_p, name);
16889
16890 return type;
16891 }
16892
16893 /* Initialise and return a floating point type of size BITS suitable for
16894 use as a component of a complex number. The NAME_HINT is passed through
16895 when initialising the floating point type and is the name of the complex
16896 type.
16897
16898 As DWARF doesn't currently provide an explicit name for the components
16899 of a complex number, but it can be helpful to have these components
16900 named, we try to select a suitable name based on the size of the
16901 component. */
16902 static struct type *
16903 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16904 struct objfile *objfile,
16905 int bits, const char *name_hint,
16906 enum bfd_endian byte_order)
16907 {
16908 gdbarch *gdbarch = get_objfile_arch (objfile);
16909 struct type *tt = nullptr;
16910
16911 /* Try to find a suitable floating point builtin type of size BITS.
16912 We're going to use the name of this type as the name for the complex
16913 target type that we are about to create. */
16914 switch (cu->language)
16915 {
16916 case language_fortran:
16917 switch (bits)
16918 {
16919 case 32:
16920 tt = builtin_f_type (gdbarch)->builtin_real;
16921 break;
16922 case 64:
16923 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16924 break;
16925 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16926 case 128:
16927 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16928 break;
16929 }
16930 break;
16931 default:
16932 switch (bits)
16933 {
16934 case 32:
16935 tt = builtin_type (gdbarch)->builtin_float;
16936 break;
16937 case 64:
16938 tt = builtin_type (gdbarch)->builtin_double;
16939 break;
16940 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16941 case 128:
16942 tt = builtin_type (gdbarch)->builtin_long_double;
16943 break;
16944 }
16945 break;
16946 }
16947
16948 /* If the type we found doesn't match the size we were looking for, then
16949 pretend we didn't find a type at all, the complex target type we
16950 create will then be nameless. */
16951 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16952 tt = nullptr;
16953
16954 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16955 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16956 }
16957
16958 /* Find a representation of a given base type and install
16959 it in the TYPE field of the die. */
16960
16961 static struct type *
16962 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16963 {
16964 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16965 struct type *type;
16966 struct attribute *attr;
16967 int encoding = 0, bits = 0;
16968 const char *name;
16969 gdbarch *arch;
16970
16971 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16972 if (attr != nullptr)
16973 encoding = DW_UNSND (attr);
16974 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16975 if (attr != nullptr)
16976 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16977 name = dwarf2_name (die, cu);
16978 if (!name)
16979 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16980
16981 arch = get_objfile_arch (objfile);
16982 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16983
16984 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16985 if (attr)
16986 {
16987 int endianity = DW_UNSND (attr);
16988
16989 switch (endianity)
16990 {
16991 case DW_END_big:
16992 byte_order = BFD_ENDIAN_BIG;
16993 break;
16994 case DW_END_little:
16995 byte_order = BFD_ENDIAN_LITTLE;
16996 break;
16997 default:
16998 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16999 break;
17000 }
17001 }
17002
17003 switch (encoding)
17004 {
17005 case DW_ATE_address:
17006 /* Turn DW_ATE_address into a void * pointer. */
17007 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17008 type = init_pointer_type (objfile, bits, name, type);
17009 break;
17010 case DW_ATE_boolean:
17011 type = init_boolean_type (objfile, bits, 1, name);
17012 break;
17013 case DW_ATE_complex_float:
17014 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17015 byte_order);
17016 type = init_complex_type (objfile, name, type);
17017 break;
17018 case DW_ATE_decimal_float:
17019 type = init_decfloat_type (objfile, bits, name);
17020 break;
17021 case DW_ATE_float:
17022 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17023 break;
17024 case DW_ATE_signed:
17025 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17026 break;
17027 case DW_ATE_unsigned:
17028 if (cu->language == language_fortran
17029 && name
17030 && startswith (name, "character("))
17031 type = init_character_type (objfile, bits, 1, name);
17032 else
17033 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17034 break;
17035 case DW_ATE_signed_char:
17036 if (cu->language == language_ada || cu->language == language_m2
17037 || cu->language == language_pascal
17038 || cu->language == language_fortran)
17039 type = init_character_type (objfile, bits, 0, name);
17040 else
17041 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17042 break;
17043 case DW_ATE_unsigned_char:
17044 if (cu->language == language_ada || cu->language == language_m2
17045 || cu->language == language_pascal
17046 || cu->language == language_fortran
17047 || cu->language == language_rust)
17048 type = init_character_type (objfile, bits, 1, name);
17049 else
17050 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17051 break;
17052 case DW_ATE_UTF:
17053 {
17054 if (bits == 16)
17055 type = builtin_type (arch)->builtin_char16;
17056 else if (bits == 32)
17057 type = builtin_type (arch)->builtin_char32;
17058 else
17059 {
17060 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17061 bits);
17062 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17063 }
17064 return set_die_type (die, type, cu);
17065 }
17066 break;
17067
17068 default:
17069 complaint (_("unsupported DW_AT_encoding: '%s'"),
17070 dwarf_type_encoding_name (encoding));
17071 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17072 break;
17073 }
17074
17075 if (name && strcmp (name, "char") == 0)
17076 TYPE_NOSIGN (type) = 1;
17077
17078 maybe_set_alignment (cu, die, type);
17079
17080 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17081
17082 return set_die_type (die, type, cu);
17083 }
17084
17085 /* Parse dwarf attribute if it's a block, reference or constant and put the
17086 resulting value of the attribute into struct bound_prop.
17087 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17088
17089 static int
17090 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17091 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17092 struct type *default_type)
17093 {
17094 struct dwarf2_property_baton *baton;
17095 struct obstack *obstack
17096 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17097
17098 gdb_assert (default_type != NULL);
17099
17100 if (attr == NULL || prop == NULL)
17101 return 0;
17102
17103 if (attr->form_is_block ())
17104 {
17105 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17106 baton->property_type = default_type;
17107 baton->locexpr.per_cu = cu->per_cu;
17108 baton->locexpr.size = DW_BLOCK (attr)->size;
17109 baton->locexpr.data = DW_BLOCK (attr)->data;
17110 switch (attr->name)
17111 {
17112 case DW_AT_string_length:
17113 baton->locexpr.is_reference = true;
17114 break;
17115 default:
17116 baton->locexpr.is_reference = false;
17117 break;
17118 }
17119 prop->data.baton = baton;
17120 prop->kind = PROP_LOCEXPR;
17121 gdb_assert (prop->data.baton != NULL);
17122 }
17123 else if (attr->form_is_ref ())
17124 {
17125 struct dwarf2_cu *target_cu = cu;
17126 struct die_info *target_die;
17127 struct attribute *target_attr;
17128
17129 target_die = follow_die_ref (die, attr, &target_cu);
17130 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17131 if (target_attr == NULL)
17132 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17133 target_cu);
17134 if (target_attr == NULL)
17135 return 0;
17136
17137 switch (target_attr->name)
17138 {
17139 case DW_AT_location:
17140 if (target_attr->form_is_section_offset ())
17141 {
17142 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17143 baton->property_type = die_type (target_die, target_cu);
17144 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17145 prop->data.baton = baton;
17146 prop->kind = PROP_LOCLIST;
17147 gdb_assert (prop->data.baton != NULL);
17148 }
17149 else if (target_attr->form_is_block ())
17150 {
17151 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17152 baton->property_type = die_type (target_die, target_cu);
17153 baton->locexpr.per_cu = cu->per_cu;
17154 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17155 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17156 baton->locexpr.is_reference = true;
17157 prop->data.baton = baton;
17158 prop->kind = PROP_LOCEXPR;
17159 gdb_assert (prop->data.baton != NULL);
17160 }
17161 else
17162 {
17163 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17164 "dynamic property");
17165 return 0;
17166 }
17167 break;
17168 case DW_AT_data_member_location:
17169 {
17170 LONGEST offset;
17171
17172 if (!handle_data_member_location (target_die, target_cu,
17173 &offset))
17174 return 0;
17175
17176 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17177 baton->property_type = read_type_die (target_die->parent,
17178 target_cu);
17179 baton->offset_info.offset = offset;
17180 baton->offset_info.type = die_type (target_die, target_cu);
17181 prop->data.baton = baton;
17182 prop->kind = PROP_ADDR_OFFSET;
17183 break;
17184 }
17185 }
17186 }
17187 else if (attr->form_is_constant ())
17188 {
17189 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17190 prop->kind = PROP_CONST;
17191 }
17192 else
17193 {
17194 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17195 dwarf2_name (die, cu));
17196 return 0;
17197 }
17198
17199 return 1;
17200 }
17201
17202 /* See read.h. */
17203
17204 struct type *
17205 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17206 {
17207 struct objfile *objfile = dwarf2_per_objfile->objfile;
17208 struct type *int_type;
17209
17210 /* Helper macro to examine the various builtin types. */
17211 #define TRY_TYPE(F) \
17212 int_type = (unsigned_p \
17213 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17214 : objfile_type (objfile)->builtin_ ## F); \
17215 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17216 return int_type
17217
17218 TRY_TYPE (char);
17219 TRY_TYPE (short);
17220 TRY_TYPE (int);
17221 TRY_TYPE (long);
17222 TRY_TYPE (long_long);
17223
17224 #undef TRY_TYPE
17225
17226 gdb_assert_not_reached ("unable to find suitable integer type");
17227 }
17228
17229 /* See read.h. */
17230
17231 struct type *
17232 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17233 {
17234 int addr_size = this->addr_size ();
17235 return int_type (addr_size, unsigned_p);
17236 }
17237
17238 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17239 present (which is valid) then compute the default type based on the
17240 compilation units address size. */
17241
17242 static struct type *
17243 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17244 {
17245 struct type *index_type = die_type (die, cu);
17246
17247 /* Dwarf-2 specifications explicitly allows to create subrange types
17248 without specifying a base type.
17249 In that case, the base type must be set to the type of
17250 the lower bound, upper bound or count, in that order, if any of these
17251 three attributes references an object that has a type.
17252 If no base type is found, the Dwarf-2 specifications say that
17253 a signed integer type of size equal to the size of an address should
17254 be used.
17255 For the following C code: `extern char gdb_int [];'
17256 GCC produces an empty range DIE.
17257 FIXME: muller/2010-05-28: Possible references to object for low bound,
17258 high bound or count are not yet handled by this code. */
17259 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17260 index_type = cu->per_cu->addr_sized_int_type (false);
17261
17262 return index_type;
17263 }
17264
17265 /* Read the given DW_AT_subrange DIE. */
17266
17267 static struct type *
17268 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17269 {
17270 struct type *base_type, *orig_base_type;
17271 struct type *range_type;
17272 struct attribute *attr;
17273 struct dynamic_prop low, high;
17274 int low_default_is_valid;
17275 int high_bound_is_count = 0;
17276 const char *name;
17277 ULONGEST negative_mask;
17278
17279 orig_base_type = read_subrange_index_type (die, cu);
17280
17281 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17282 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17283 creating the range type, but we use the result of check_typedef
17284 when examining properties of the type. */
17285 base_type = check_typedef (orig_base_type);
17286
17287 /* The die_type call above may have already set the type for this DIE. */
17288 range_type = get_die_type (die, cu);
17289 if (range_type)
17290 return range_type;
17291
17292 low.kind = PROP_CONST;
17293 high.kind = PROP_CONST;
17294 high.data.const_val = 0;
17295
17296 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17297 omitting DW_AT_lower_bound. */
17298 switch (cu->language)
17299 {
17300 case language_c:
17301 case language_cplus:
17302 low.data.const_val = 0;
17303 low_default_is_valid = 1;
17304 break;
17305 case language_fortran:
17306 low.data.const_val = 1;
17307 low_default_is_valid = 1;
17308 break;
17309 case language_d:
17310 case language_objc:
17311 case language_rust:
17312 low.data.const_val = 0;
17313 low_default_is_valid = (cu->header.version >= 4);
17314 break;
17315 case language_ada:
17316 case language_m2:
17317 case language_pascal:
17318 low.data.const_val = 1;
17319 low_default_is_valid = (cu->header.version >= 4);
17320 break;
17321 default:
17322 low.data.const_val = 0;
17323 low_default_is_valid = 0;
17324 break;
17325 }
17326
17327 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17328 if (attr != nullptr)
17329 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17330 else if (!low_default_is_valid)
17331 complaint (_("Missing DW_AT_lower_bound "
17332 "- DIE at %s [in module %s]"),
17333 sect_offset_str (die->sect_off),
17334 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17335
17336 struct attribute *attr_ub, *attr_count;
17337 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17338 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17339 {
17340 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17341 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17342 {
17343 /* If bounds are constant do the final calculation here. */
17344 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17345 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17346 else
17347 high_bound_is_count = 1;
17348 }
17349 else
17350 {
17351 if (attr_ub != NULL)
17352 complaint (_("Unresolved DW_AT_upper_bound "
17353 "- DIE at %s [in module %s]"),
17354 sect_offset_str (die->sect_off),
17355 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17356 if (attr_count != NULL)
17357 complaint (_("Unresolved DW_AT_count "
17358 "- DIE at %s [in module %s]"),
17359 sect_offset_str (die->sect_off),
17360 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17361 }
17362 }
17363
17364 LONGEST bias = 0;
17365 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17366 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17367 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17368
17369 /* Normally, the DWARF producers are expected to use a signed
17370 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17371 But this is unfortunately not always the case, as witnessed
17372 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17373 is used instead. To work around that ambiguity, we treat
17374 the bounds as signed, and thus sign-extend their values, when
17375 the base type is signed. */
17376 negative_mask =
17377 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17378 if (low.kind == PROP_CONST
17379 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17380 low.data.const_val |= negative_mask;
17381 if (high.kind == PROP_CONST
17382 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17383 high.data.const_val |= negative_mask;
17384
17385 /* Check for bit and byte strides. */
17386 struct dynamic_prop byte_stride_prop;
17387 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17388 if (attr_byte_stride != nullptr)
17389 {
17390 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17391 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17392 prop_type);
17393 }
17394
17395 struct dynamic_prop bit_stride_prop;
17396 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17397 if (attr_bit_stride != nullptr)
17398 {
17399 /* It only makes sense to have either a bit or byte stride. */
17400 if (attr_byte_stride != nullptr)
17401 {
17402 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17403 "- DIE at %s [in module %s]"),
17404 sect_offset_str (die->sect_off),
17405 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17406 attr_bit_stride = nullptr;
17407 }
17408 else
17409 {
17410 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17411 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17412 prop_type);
17413 }
17414 }
17415
17416 if (attr_byte_stride != nullptr
17417 || attr_bit_stride != nullptr)
17418 {
17419 bool byte_stride_p = (attr_byte_stride != nullptr);
17420 struct dynamic_prop *stride
17421 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17422
17423 range_type
17424 = create_range_type_with_stride (NULL, orig_base_type, &low,
17425 &high, bias, stride, byte_stride_p);
17426 }
17427 else
17428 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17429
17430 if (high_bound_is_count)
17431 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17432
17433 /* Ada expects an empty array on no boundary attributes. */
17434 if (attr == NULL && cu->language != language_ada)
17435 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17436
17437 name = dwarf2_name (die, cu);
17438 if (name)
17439 TYPE_NAME (range_type) = name;
17440
17441 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17442 if (attr != nullptr)
17443 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17444
17445 maybe_set_alignment (cu, die, range_type);
17446
17447 set_die_type (die, range_type, cu);
17448
17449 /* set_die_type should be already done. */
17450 set_descriptive_type (range_type, die, cu);
17451
17452 return range_type;
17453 }
17454
17455 static struct type *
17456 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17457 {
17458 struct type *type;
17459
17460 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17461 NULL);
17462 TYPE_NAME (type) = dwarf2_name (die, cu);
17463
17464 /* In Ada, an unspecified type is typically used when the description
17465 of the type is deferred to a different unit. When encountering
17466 such a type, we treat it as a stub, and try to resolve it later on,
17467 when needed. */
17468 if (cu->language == language_ada)
17469 TYPE_STUB (type) = 1;
17470
17471 return set_die_type (die, type, cu);
17472 }
17473
17474 /* Read a single die and all its descendents. Set the die's sibling
17475 field to NULL; set other fields in the die correctly, and set all
17476 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17477 location of the info_ptr after reading all of those dies. PARENT
17478 is the parent of the die in question. */
17479
17480 static struct die_info *
17481 read_die_and_children (const struct die_reader_specs *reader,
17482 const gdb_byte *info_ptr,
17483 const gdb_byte **new_info_ptr,
17484 struct die_info *parent)
17485 {
17486 struct die_info *die;
17487 const gdb_byte *cur_ptr;
17488
17489 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17490 if (die == NULL)
17491 {
17492 *new_info_ptr = cur_ptr;
17493 return NULL;
17494 }
17495 store_in_ref_table (die, reader->cu);
17496
17497 if (die->has_children)
17498 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17499 else
17500 {
17501 die->child = NULL;
17502 *new_info_ptr = cur_ptr;
17503 }
17504
17505 die->sibling = NULL;
17506 die->parent = parent;
17507 return die;
17508 }
17509
17510 /* Read a die, all of its descendents, and all of its siblings; set
17511 all of the fields of all of the dies correctly. Arguments are as
17512 in read_die_and_children. */
17513
17514 static struct die_info *
17515 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17516 const gdb_byte *info_ptr,
17517 const gdb_byte **new_info_ptr,
17518 struct die_info *parent)
17519 {
17520 struct die_info *first_die, *last_sibling;
17521 const gdb_byte *cur_ptr;
17522
17523 cur_ptr = info_ptr;
17524 first_die = last_sibling = NULL;
17525
17526 while (1)
17527 {
17528 struct die_info *die
17529 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17530
17531 if (die == NULL)
17532 {
17533 *new_info_ptr = cur_ptr;
17534 return first_die;
17535 }
17536
17537 if (!first_die)
17538 first_die = die;
17539 else
17540 last_sibling->sibling = die;
17541
17542 last_sibling = die;
17543 }
17544 }
17545
17546 /* Read a die, all of its descendents, and all of its siblings; set
17547 all of the fields of all of the dies correctly. Arguments are as
17548 in read_die_and_children.
17549 This the main entry point for reading a DIE and all its children. */
17550
17551 static struct die_info *
17552 read_die_and_siblings (const struct die_reader_specs *reader,
17553 const gdb_byte *info_ptr,
17554 const gdb_byte **new_info_ptr,
17555 struct die_info *parent)
17556 {
17557 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17558 new_info_ptr, parent);
17559
17560 if (dwarf_die_debug)
17561 {
17562 fprintf_unfiltered (gdb_stdlog,
17563 "Read die from %s@0x%x of %s:\n",
17564 reader->die_section->get_name (),
17565 (unsigned) (info_ptr - reader->die_section->buffer),
17566 bfd_get_filename (reader->abfd));
17567 dump_die (die, dwarf_die_debug);
17568 }
17569
17570 return die;
17571 }
17572
17573 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17574 attributes.
17575 The caller is responsible for filling in the extra attributes
17576 and updating (*DIEP)->num_attrs.
17577 Set DIEP to point to a newly allocated die with its information,
17578 except for its child, sibling, and parent fields. */
17579
17580 static const gdb_byte *
17581 read_full_die_1 (const struct die_reader_specs *reader,
17582 struct die_info **diep, const gdb_byte *info_ptr,
17583 int num_extra_attrs)
17584 {
17585 unsigned int abbrev_number, bytes_read, i;
17586 struct abbrev_info *abbrev;
17587 struct die_info *die;
17588 struct dwarf2_cu *cu = reader->cu;
17589 bfd *abfd = reader->abfd;
17590
17591 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17592 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17593 info_ptr += bytes_read;
17594 if (!abbrev_number)
17595 {
17596 *diep = NULL;
17597 return info_ptr;
17598 }
17599
17600 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17601 if (!abbrev)
17602 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17603 abbrev_number,
17604 bfd_get_filename (abfd));
17605
17606 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17607 die->sect_off = sect_off;
17608 die->tag = abbrev->tag;
17609 die->abbrev = abbrev_number;
17610 die->has_children = abbrev->has_children;
17611
17612 /* Make the result usable.
17613 The caller needs to update num_attrs after adding the extra
17614 attributes. */
17615 die->num_attrs = abbrev->num_attrs;
17616
17617 std::vector<int> indexes_that_need_reprocess;
17618 for (i = 0; i < abbrev->num_attrs; ++i)
17619 {
17620 bool need_reprocess;
17621 info_ptr =
17622 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17623 info_ptr, &need_reprocess);
17624 if (need_reprocess)
17625 indexes_that_need_reprocess.push_back (i);
17626 }
17627
17628 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17629 if (attr != nullptr)
17630 cu->str_offsets_base = DW_UNSND (attr);
17631
17632 auto maybe_addr_base = lookup_addr_base(die);
17633 if (maybe_addr_base.has_value ())
17634 cu->addr_base = *maybe_addr_base;
17635 for (int index : indexes_that_need_reprocess)
17636 read_attribute_reprocess (reader, &die->attrs[index]);
17637 *diep = die;
17638 return info_ptr;
17639 }
17640
17641 /* Read a die and all its attributes.
17642 Set DIEP to point to a newly allocated die with its information,
17643 except for its child, sibling, and parent fields. */
17644
17645 static const gdb_byte *
17646 read_full_die (const struct die_reader_specs *reader,
17647 struct die_info **diep, const gdb_byte *info_ptr)
17648 {
17649 const gdb_byte *result;
17650
17651 result = read_full_die_1 (reader, diep, info_ptr, 0);
17652
17653 if (dwarf_die_debug)
17654 {
17655 fprintf_unfiltered (gdb_stdlog,
17656 "Read die from %s@0x%x of %s:\n",
17657 reader->die_section->get_name (),
17658 (unsigned) (info_ptr - reader->die_section->buffer),
17659 bfd_get_filename (reader->abfd));
17660 dump_die (*diep, dwarf_die_debug);
17661 }
17662
17663 return result;
17664 }
17665 \f
17666
17667 /* Returns nonzero if TAG represents a type that we might generate a partial
17668 symbol for. */
17669
17670 static int
17671 is_type_tag_for_partial (int tag)
17672 {
17673 switch (tag)
17674 {
17675 #if 0
17676 /* Some types that would be reasonable to generate partial symbols for,
17677 that we don't at present. */
17678 case DW_TAG_array_type:
17679 case DW_TAG_file_type:
17680 case DW_TAG_ptr_to_member_type:
17681 case DW_TAG_set_type:
17682 case DW_TAG_string_type:
17683 case DW_TAG_subroutine_type:
17684 #endif
17685 case DW_TAG_base_type:
17686 case DW_TAG_class_type:
17687 case DW_TAG_interface_type:
17688 case DW_TAG_enumeration_type:
17689 case DW_TAG_structure_type:
17690 case DW_TAG_subrange_type:
17691 case DW_TAG_typedef:
17692 case DW_TAG_union_type:
17693 return 1;
17694 default:
17695 return 0;
17696 }
17697 }
17698
17699 /* Load all DIEs that are interesting for partial symbols into memory. */
17700
17701 static struct partial_die_info *
17702 load_partial_dies (const struct die_reader_specs *reader,
17703 const gdb_byte *info_ptr, int building_psymtab)
17704 {
17705 struct dwarf2_cu *cu = reader->cu;
17706 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17707 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17708 unsigned int bytes_read;
17709 unsigned int load_all = 0;
17710 int nesting_level = 1;
17711
17712 parent_die = NULL;
17713 last_die = NULL;
17714
17715 gdb_assert (cu->per_cu != NULL);
17716 if (cu->per_cu->load_all_dies)
17717 load_all = 1;
17718
17719 cu->partial_dies
17720 = htab_create_alloc_ex (cu->header.length / 12,
17721 partial_die_hash,
17722 partial_die_eq,
17723 NULL,
17724 &cu->comp_unit_obstack,
17725 hashtab_obstack_allocate,
17726 dummy_obstack_deallocate);
17727
17728 while (1)
17729 {
17730 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17731
17732 /* A NULL abbrev means the end of a series of children. */
17733 if (abbrev == NULL)
17734 {
17735 if (--nesting_level == 0)
17736 return first_die;
17737
17738 info_ptr += bytes_read;
17739 last_die = parent_die;
17740 parent_die = parent_die->die_parent;
17741 continue;
17742 }
17743
17744 /* Check for template arguments. We never save these; if
17745 they're seen, we just mark the parent, and go on our way. */
17746 if (parent_die != NULL
17747 && cu->language == language_cplus
17748 && (abbrev->tag == DW_TAG_template_type_param
17749 || abbrev->tag == DW_TAG_template_value_param))
17750 {
17751 parent_die->has_template_arguments = 1;
17752
17753 if (!load_all)
17754 {
17755 /* We don't need a partial DIE for the template argument. */
17756 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17757 continue;
17758 }
17759 }
17760
17761 /* We only recurse into c++ subprograms looking for template arguments.
17762 Skip their other children. */
17763 if (!load_all
17764 && cu->language == language_cplus
17765 && parent_die != NULL
17766 && parent_die->tag == DW_TAG_subprogram)
17767 {
17768 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17769 continue;
17770 }
17771
17772 /* Check whether this DIE is interesting enough to save. Normally
17773 we would not be interested in members here, but there may be
17774 later variables referencing them via DW_AT_specification (for
17775 static members). */
17776 if (!load_all
17777 && !is_type_tag_for_partial (abbrev->tag)
17778 && abbrev->tag != DW_TAG_constant
17779 && abbrev->tag != DW_TAG_enumerator
17780 && abbrev->tag != DW_TAG_subprogram
17781 && abbrev->tag != DW_TAG_inlined_subroutine
17782 && abbrev->tag != DW_TAG_lexical_block
17783 && abbrev->tag != DW_TAG_variable
17784 && abbrev->tag != DW_TAG_namespace
17785 && abbrev->tag != DW_TAG_module
17786 && abbrev->tag != DW_TAG_member
17787 && abbrev->tag != DW_TAG_imported_unit
17788 && abbrev->tag != DW_TAG_imported_declaration)
17789 {
17790 /* Otherwise we skip to the next sibling, if any. */
17791 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17792 continue;
17793 }
17794
17795 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17796 abbrev);
17797
17798 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17799
17800 /* This two-pass algorithm for processing partial symbols has a
17801 high cost in cache pressure. Thus, handle some simple cases
17802 here which cover the majority of C partial symbols. DIEs
17803 which neither have specification tags in them, nor could have
17804 specification tags elsewhere pointing at them, can simply be
17805 processed and discarded.
17806
17807 This segment is also optional; scan_partial_symbols and
17808 add_partial_symbol will handle these DIEs if we chain
17809 them in normally. When compilers which do not emit large
17810 quantities of duplicate debug information are more common,
17811 this code can probably be removed. */
17812
17813 /* Any complete simple types at the top level (pretty much all
17814 of them, for a language without namespaces), can be processed
17815 directly. */
17816 if (parent_die == NULL
17817 && pdi.has_specification == 0
17818 && pdi.is_declaration == 0
17819 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17820 || pdi.tag == DW_TAG_base_type
17821 || pdi.tag == DW_TAG_subrange_type))
17822 {
17823 if (building_psymtab && pdi.name != NULL)
17824 add_psymbol_to_list (pdi.name, false,
17825 VAR_DOMAIN, LOC_TYPEDEF, -1,
17826 psymbol_placement::STATIC,
17827 0, cu->language, objfile);
17828 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17829 continue;
17830 }
17831
17832 /* The exception for DW_TAG_typedef with has_children above is
17833 a workaround of GCC PR debug/47510. In the case of this complaint
17834 type_name_or_error will error on such types later.
17835
17836 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17837 it could not find the child DIEs referenced later, this is checked
17838 above. In correct DWARF DW_TAG_typedef should have no children. */
17839
17840 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17841 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17842 "- DIE at %s [in module %s]"),
17843 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17844
17845 /* If we're at the second level, and we're an enumerator, and
17846 our parent has no specification (meaning possibly lives in a
17847 namespace elsewhere), then we can add the partial symbol now
17848 instead of queueing it. */
17849 if (pdi.tag == DW_TAG_enumerator
17850 && parent_die != NULL
17851 && parent_die->die_parent == NULL
17852 && parent_die->tag == DW_TAG_enumeration_type
17853 && parent_die->has_specification == 0)
17854 {
17855 if (pdi.name == NULL)
17856 complaint (_("malformed enumerator DIE ignored"));
17857 else if (building_psymtab)
17858 add_psymbol_to_list (pdi.name, false,
17859 VAR_DOMAIN, LOC_CONST, -1,
17860 cu->language == language_cplus
17861 ? psymbol_placement::GLOBAL
17862 : psymbol_placement::STATIC,
17863 0, cu->language, objfile);
17864
17865 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17866 continue;
17867 }
17868
17869 struct partial_die_info *part_die
17870 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17871
17872 /* We'll save this DIE so link it in. */
17873 part_die->die_parent = parent_die;
17874 part_die->die_sibling = NULL;
17875 part_die->die_child = NULL;
17876
17877 if (last_die && last_die == parent_die)
17878 last_die->die_child = part_die;
17879 else if (last_die)
17880 last_die->die_sibling = part_die;
17881
17882 last_die = part_die;
17883
17884 if (first_die == NULL)
17885 first_die = part_die;
17886
17887 /* Maybe add the DIE to the hash table. Not all DIEs that we
17888 find interesting need to be in the hash table, because we
17889 also have the parent/sibling/child chains; only those that we
17890 might refer to by offset later during partial symbol reading.
17891
17892 For now this means things that might have be the target of a
17893 DW_AT_specification, DW_AT_abstract_origin, or
17894 DW_AT_extension. DW_AT_extension will refer only to
17895 namespaces; DW_AT_abstract_origin refers to functions (and
17896 many things under the function DIE, but we do not recurse
17897 into function DIEs during partial symbol reading) and
17898 possibly variables as well; DW_AT_specification refers to
17899 declarations. Declarations ought to have the DW_AT_declaration
17900 flag. It happens that GCC forgets to put it in sometimes, but
17901 only for functions, not for types.
17902
17903 Adding more things than necessary to the hash table is harmless
17904 except for the performance cost. Adding too few will result in
17905 wasted time in find_partial_die, when we reread the compilation
17906 unit with load_all_dies set. */
17907
17908 if (load_all
17909 || abbrev->tag == DW_TAG_constant
17910 || abbrev->tag == DW_TAG_subprogram
17911 || abbrev->tag == DW_TAG_variable
17912 || abbrev->tag == DW_TAG_namespace
17913 || part_die->is_declaration)
17914 {
17915 void **slot;
17916
17917 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17918 to_underlying (part_die->sect_off),
17919 INSERT);
17920 *slot = part_die;
17921 }
17922
17923 /* For some DIEs we want to follow their children (if any). For C
17924 we have no reason to follow the children of structures; for other
17925 languages we have to, so that we can get at method physnames
17926 to infer fully qualified class names, for DW_AT_specification,
17927 and for C++ template arguments. For C++, we also look one level
17928 inside functions to find template arguments (if the name of the
17929 function does not already contain the template arguments).
17930
17931 For Ada and Fortran, we need to scan the children of subprograms
17932 and lexical blocks as well because these languages allow the
17933 definition of nested entities that could be interesting for the
17934 debugger, such as nested subprograms for instance. */
17935 if (last_die->has_children
17936 && (load_all
17937 || last_die->tag == DW_TAG_namespace
17938 || last_die->tag == DW_TAG_module
17939 || last_die->tag == DW_TAG_enumeration_type
17940 || (cu->language == language_cplus
17941 && last_die->tag == DW_TAG_subprogram
17942 && (last_die->name == NULL
17943 || strchr (last_die->name, '<') == NULL))
17944 || (cu->language != language_c
17945 && (last_die->tag == DW_TAG_class_type
17946 || last_die->tag == DW_TAG_interface_type
17947 || last_die->tag == DW_TAG_structure_type
17948 || last_die->tag == DW_TAG_union_type))
17949 || ((cu->language == language_ada
17950 || cu->language == language_fortran)
17951 && (last_die->tag == DW_TAG_subprogram
17952 || last_die->tag == DW_TAG_lexical_block))))
17953 {
17954 nesting_level++;
17955 parent_die = last_die;
17956 continue;
17957 }
17958
17959 /* Otherwise we skip to the next sibling, if any. */
17960 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17961
17962 /* Back to the top, do it again. */
17963 }
17964 }
17965
17966 partial_die_info::partial_die_info (sect_offset sect_off_,
17967 struct abbrev_info *abbrev)
17968 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17969 {
17970 }
17971
17972 /* Read a minimal amount of information into the minimal die structure.
17973 INFO_PTR should point just after the initial uleb128 of a DIE. */
17974
17975 const gdb_byte *
17976 partial_die_info::read (const struct die_reader_specs *reader,
17977 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17978 {
17979 struct dwarf2_cu *cu = reader->cu;
17980 struct dwarf2_per_objfile *dwarf2_per_objfile
17981 = cu->per_cu->dwarf2_per_objfile;
17982 unsigned int i;
17983 int has_low_pc_attr = 0;
17984 int has_high_pc_attr = 0;
17985 int high_pc_relative = 0;
17986
17987 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17988 for (i = 0; i < abbrev.num_attrs; ++i)
17989 {
17990 bool need_reprocess;
17991 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17992 info_ptr, &need_reprocess);
17993 /* String and address offsets that need to do the reprocessing have
17994 already been read at this point, so there is no need to wait until
17995 the loop terminates to do the reprocessing. */
17996 if (need_reprocess)
17997 read_attribute_reprocess (reader, &attr_vec[i]);
17998 attribute &attr = attr_vec[i];
17999 /* Store the data if it is of an attribute we want to keep in a
18000 partial symbol table. */
18001 switch (attr.name)
18002 {
18003 case DW_AT_name:
18004 switch (tag)
18005 {
18006 case DW_TAG_compile_unit:
18007 case DW_TAG_partial_unit:
18008 case DW_TAG_type_unit:
18009 /* Compilation units have a DW_AT_name that is a filename, not
18010 a source language identifier. */
18011 case DW_TAG_enumeration_type:
18012 case DW_TAG_enumerator:
18013 /* These tags always have simple identifiers already; no need
18014 to canonicalize them. */
18015 name = DW_STRING (&attr);
18016 break;
18017 default:
18018 {
18019 struct objfile *objfile = dwarf2_per_objfile->objfile;
18020
18021 name
18022 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18023 }
18024 break;
18025 }
18026 break;
18027 case DW_AT_linkage_name:
18028 case DW_AT_MIPS_linkage_name:
18029 /* Note that both forms of linkage name might appear. We
18030 assume they will be the same, and we only store the last
18031 one we see. */
18032 linkage_name = DW_STRING (&attr);
18033 break;
18034 case DW_AT_low_pc:
18035 has_low_pc_attr = 1;
18036 lowpc = attr.value_as_address ();
18037 break;
18038 case DW_AT_high_pc:
18039 has_high_pc_attr = 1;
18040 highpc = attr.value_as_address ();
18041 if (cu->header.version >= 4 && attr.form_is_constant ())
18042 high_pc_relative = 1;
18043 break;
18044 case DW_AT_location:
18045 /* Support the .debug_loc offsets. */
18046 if (attr.form_is_block ())
18047 {
18048 d.locdesc = DW_BLOCK (&attr);
18049 }
18050 else if (attr.form_is_section_offset ())
18051 {
18052 dwarf2_complex_location_expr_complaint ();
18053 }
18054 else
18055 {
18056 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18057 "partial symbol information");
18058 }
18059 break;
18060 case DW_AT_external:
18061 is_external = DW_UNSND (&attr);
18062 break;
18063 case DW_AT_declaration:
18064 is_declaration = DW_UNSND (&attr);
18065 break;
18066 case DW_AT_type:
18067 has_type = 1;
18068 break;
18069 case DW_AT_abstract_origin:
18070 case DW_AT_specification:
18071 case DW_AT_extension:
18072 has_specification = 1;
18073 spec_offset = dwarf2_get_ref_die_offset (&attr);
18074 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18075 || cu->per_cu->is_dwz);
18076 break;
18077 case DW_AT_sibling:
18078 /* Ignore absolute siblings, they might point outside of
18079 the current compile unit. */
18080 if (attr.form == DW_FORM_ref_addr)
18081 complaint (_("ignoring absolute DW_AT_sibling"));
18082 else
18083 {
18084 const gdb_byte *buffer = reader->buffer;
18085 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18086 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18087
18088 if (sibling_ptr < info_ptr)
18089 complaint (_("DW_AT_sibling points backwards"));
18090 else if (sibling_ptr > reader->buffer_end)
18091 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18092 else
18093 sibling = sibling_ptr;
18094 }
18095 break;
18096 case DW_AT_byte_size:
18097 has_byte_size = 1;
18098 break;
18099 case DW_AT_const_value:
18100 has_const_value = 1;
18101 break;
18102 case DW_AT_calling_convention:
18103 /* DWARF doesn't provide a way to identify a program's source-level
18104 entry point. DW_AT_calling_convention attributes are only meant
18105 to describe functions' calling conventions.
18106
18107 However, because it's a necessary piece of information in
18108 Fortran, and before DWARF 4 DW_CC_program was the only
18109 piece of debugging information whose definition refers to
18110 a 'main program' at all, several compilers marked Fortran
18111 main programs with DW_CC_program --- even when those
18112 functions use the standard calling conventions.
18113
18114 Although DWARF now specifies a way to provide this
18115 information, we support this practice for backward
18116 compatibility. */
18117 if (DW_UNSND (&attr) == DW_CC_program
18118 && cu->language == language_fortran)
18119 main_subprogram = 1;
18120 break;
18121 case DW_AT_inline:
18122 if (DW_UNSND (&attr) == DW_INL_inlined
18123 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18124 may_be_inlined = 1;
18125 break;
18126
18127 case DW_AT_import:
18128 if (tag == DW_TAG_imported_unit)
18129 {
18130 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18131 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18132 || cu->per_cu->is_dwz);
18133 }
18134 break;
18135
18136 case DW_AT_main_subprogram:
18137 main_subprogram = DW_UNSND (&attr);
18138 break;
18139
18140 case DW_AT_ranges:
18141 {
18142 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18143 but that requires a full DIE, so instead we just
18144 reimplement it. */
18145 int need_ranges_base = tag != DW_TAG_compile_unit;
18146 unsigned int ranges_offset = (DW_UNSND (&attr)
18147 + (need_ranges_base
18148 ? cu->ranges_base
18149 : 0));
18150
18151 /* Value of the DW_AT_ranges attribute is the offset in the
18152 .debug_ranges section. */
18153 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18154 nullptr))
18155 has_pc_info = 1;
18156 }
18157 break;
18158
18159 default:
18160 break;
18161 }
18162 }
18163
18164 /* For Ada, if both the name and the linkage name appear, we prefer
18165 the latter. This lets "catch exception" work better, regardless
18166 of the order in which the name and linkage name were emitted.
18167 Really, though, this is just a workaround for the fact that gdb
18168 doesn't store both the name and the linkage name. */
18169 if (cu->language == language_ada && linkage_name != nullptr)
18170 name = linkage_name;
18171
18172 if (high_pc_relative)
18173 highpc += lowpc;
18174
18175 if (has_low_pc_attr && has_high_pc_attr)
18176 {
18177 /* When using the GNU linker, .gnu.linkonce. sections are used to
18178 eliminate duplicate copies of functions and vtables and such.
18179 The linker will arbitrarily choose one and discard the others.
18180 The AT_*_pc values for such functions refer to local labels in
18181 these sections. If the section from that file was discarded, the
18182 labels are not in the output, so the relocs get a value of 0.
18183 If this is a discarded function, mark the pc bounds as invalid,
18184 so that GDB will ignore it. */
18185 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18186 {
18187 struct objfile *objfile = dwarf2_per_objfile->objfile;
18188 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18189
18190 complaint (_("DW_AT_low_pc %s is zero "
18191 "for DIE at %s [in module %s]"),
18192 paddress (gdbarch, lowpc),
18193 sect_offset_str (sect_off),
18194 objfile_name (objfile));
18195 }
18196 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18197 else if (lowpc >= highpc)
18198 {
18199 struct objfile *objfile = dwarf2_per_objfile->objfile;
18200 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18201
18202 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18203 "for DIE at %s [in module %s]"),
18204 paddress (gdbarch, lowpc),
18205 paddress (gdbarch, highpc),
18206 sect_offset_str (sect_off),
18207 objfile_name (objfile));
18208 }
18209 else
18210 has_pc_info = 1;
18211 }
18212
18213 return info_ptr;
18214 }
18215
18216 /* Find a cached partial DIE at OFFSET in CU. */
18217
18218 struct partial_die_info *
18219 dwarf2_cu::find_partial_die (sect_offset sect_off)
18220 {
18221 struct partial_die_info *lookup_die = NULL;
18222 struct partial_die_info part_die (sect_off);
18223
18224 lookup_die = ((struct partial_die_info *)
18225 htab_find_with_hash (partial_dies, &part_die,
18226 to_underlying (sect_off)));
18227
18228 return lookup_die;
18229 }
18230
18231 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18232 except in the case of .debug_types DIEs which do not reference
18233 outside their CU (they do however referencing other types via
18234 DW_FORM_ref_sig8). */
18235
18236 static const struct cu_partial_die_info
18237 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18238 {
18239 struct dwarf2_per_objfile *dwarf2_per_objfile
18240 = cu->per_cu->dwarf2_per_objfile;
18241 struct objfile *objfile = dwarf2_per_objfile->objfile;
18242 struct dwarf2_per_cu_data *per_cu = NULL;
18243 struct partial_die_info *pd = NULL;
18244
18245 if (offset_in_dwz == cu->per_cu->is_dwz
18246 && cu->header.offset_in_cu_p (sect_off))
18247 {
18248 pd = cu->find_partial_die (sect_off);
18249 if (pd != NULL)
18250 return { cu, pd };
18251 /* We missed recording what we needed.
18252 Load all dies and try again. */
18253 per_cu = cu->per_cu;
18254 }
18255 else
18256 {
18257 /* TUs don't reference other CUs/TUs (except via type signatures). */
18258 if (cu->per_cu->is_debug_types)
18259 {
18260 error (_("Dwarf Error: Type Unit at offset %s contains"
18261 " external reference to offset %s [in module %s].\n"),
18262 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18263 bfd_get_filename (objfile->obfd));
18264 }
18265 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18266 dwarf2_per_objfile);
18267
18268 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18269 load_partial_comp_unit (per_cu);
18270
18271 per_cu->cu->last_used = 0;
18272 pd = per_cu->cu->find_partial_die (sect_off);
18273 }
18274
18275 /* If we didn't find it, and not all dies have been loaded,
18276 load them all and try again. */
18277
18278 if (pd == NULL && per_cu->load_all_dies == 0)
18279 {
18280 per_cu->load_all_dies = 1;
18281
18282 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18283 THIS_CU->cu may already be in use. So we can't just free it and
18284 replace its DIEs with the ones we read in. Instead, we leave those
18285 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18286 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18287 set. */
18288 load_partial_comp_unit (per_cu);
18289
18290 pd = per_cu->cu->find_partial_die (sect_off);
18291 }
18292
18293 if (pd == NULL)
18294 internal_error (__FILE__, __LINE__,
18295 _("could not find partial DIE %s "
18296 "in cache [from module %s]\n"),
18297 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18298 return { per_cu->cu, pd };
18299 }
18300
18301 /* See if we can figure out if the class lives in a namespace. We do
18302 this by looking for a member function; its demangled name will
18303 contain namespace info, if there is any. */
18304
18305 static void
18306 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18307 struct dwarf2_cu *cu)
18308 {
18309 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18310 what template types look like, because the demangler
18311 frequently doesn't give the same name as the debug info. We
18312 could fix this by only using the demangled name to get the
18313 prefix (but see comment in read_structure_type). */
18314
18315 struct partial_die_info *real_pdi;
18316 struct partial_die_info *child_pdi;
18317
18318 /* If this DIE (this DIE's specification, if any) has a parent, then
18319 we should not do this. We'll prepend the parent's fully qualified
18320 name when we create the partial symbol. */
18321
18322 real_pdi = struct_pdi;
18323 while (real_pdi->has_specification)
18324 {
18325 auto res = find_partial_die (real_pdi->spec_offset,
18326 real_pdi->spec_is_dwz, cu);
18327 real_pdi = res.pdi;
18328 cu = res.cu;
18329 }
18330
18331 if (real_pdi->die_parent != NULL)
18332 return;
18333
18334 for (child_pdi = struct_pdi->die_child;
18335 child_pdi != NULL;
18336 child_pdi = child_pdi->die_sibling)
18337 {
18338 if (child_pdi->tag == DW_TAG_subprogram
18339 && child_pdi->linkage_name != NULL)
18340 {
18341 gdb::unique_xmalloc_ptr<char> actual_class_name
18342 (language_class_name_from_physname (cu->language_defn,
18343 child_pdi->linkage_name));
18344 if (actual_class_name != NULL)
18345 {
18346 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18347 struct_pdi->name = objfile->intern (actual_class_name.get ());
18348 }
18349 break;
18350 }
18351 }
18352 }
18353
18354 void
18355 partial_die_info::fixup (struct dwarf2_cu *cu)
18356 {
18357 /* Once we've fixed up a die, there's no point in doing so again.
18358 This also avoids a memory leak if we were to call
18359 guess_partial_die_structure_name multiple times. */
18360 if (fixup_called)
18361 return;
18362
18363 /* If we found a reference attribute and the DIE has no name, try
18364 to find a name in the referred to DIE. */
18365
18366 if (name == NULL && has_specification)
18367 {
18368 struct partial_die_info *spec_die;
18369
18370 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18371 spec_die = res.pdi;
18372 cu = res.cu;
18373
18374 spec_die->fixup (cu);
18375
18376 if (spec_die->name)
18377 {
18378 name = spec_die->name;
18379
18380 /* Copy DW_AT_external attribute if it is set. */
18381 if (spec_die->is_external)
18382 is_external = spec_die->is_external;
18383 }
18384 }
18385
18386 /* Set default names for some unnamed DIEs. */
18387
18388 if (name == NULL && tag == DW_TAG_namespace)
18389 name = CP_ANONYMOUS_NAMESPACE_STR;
18390
18391 /* If there is no parent die to provide a namespace, and there are
18392 children, see if we can determine the namespace from their linkage
18393 name. */
18394 if (cu->language == language_cplus
18395 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18396 && die_parent == NULL
18397 && has_children
18398 && (tag == DW_TAG_class_type
18399 || tag == DW_TAG_structure_type
18400 || tag == DW_TAG_union_type))
18401 guess_partial_die_structure_name (this, cu);
18402
18403 /* GCC might emit a nameless struct or union that has a linkage
18404 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18405 if (name == NULL
18406 && (tag == DW_TAG_class_type
18407 || tag == DW_TAG_interface_type
18408 || tag == DW_TAG_structure_type
18409 || tag == DW_TAG_union_type)
18410 && linkage_name != NULL)
18411 {
18412 gdb::unique_xmalloc_ptr<char> demangled
18413 (gdb_demangle (linkage_name, DMGL_TYPES));
18414 if (demangled != nullptr)
18415 {
18416 const char *base;
18417
18418 /* Strip any leading namespaces/classes, keep only the base name.
18419 DW_AT_name for named DIEs does not contain the prefixes. */
18420 base = strrchr (demangled.get (), ':');
18421 if (base && base > demangled.get () && base[-1] == ':')
18422 base++;
18423 else
18424 base = demangled.get ();
18425
18426 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18427 name = objfile->intern (base);
18428 }
18429 }
18430
18431 fixup_called = 1;
18432 }
18433
18434 /* Process the attributes that had to be skipped in the first round. These
18435 attributes are the ones that need str_offsets_base or addr_base attributes.
18436 They could not have been processed in the first round, because at the time
18437 the values of str_offsets_base or addr_base may not have been known. */
18438 void read_attribute_reprocess (const struct die_reader_specs *reader,
18439 struct attribute *attr)
18440 {
18441 struct dwarf2_cu *cu = reader->cu;
18442 switch (attr->form)
18443 {
18444 case DW_FORM_addrx:
18445 case DW_FORM_GNU_addr_index:
18446 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18447 break;
18448 case DW_FORM_strx:
18449 case DW_FORM_strx1:
18450 case DW_FORM_strx2:
18451 case DW_FORM_strx3:
18452 case DW_FORM_strx4:
18453 case DW_FORM_GNU_str_index:
18454 {
18455 unsigned int str_index = DW_UNSND (attr);
18456 if (reader->dwo_file != NULL)
18457 {
18458 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18459 DW_STRING_IS_CANONICAL (attr) = 0;
18460 }
18461 else
18462 {
18463 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18464 DW_STRING_IS_CANONICAL (attr) = 0;
18465 }
18466 break;
18467 }
18468 default:
18469 gdb_assert_not_reached (_("Unexpected DWARF form."));
18470 }
18471 }
18472
18473 /* Read an attribute value described by an attribute form. */
18474
18475 static const gdb_byte *
18476 read_attribute_value (const struct die_reader_specs *reader,
18477 struct attribute *attr, unsigned form,
18478 LONGEST implicit_const, const gdb_byte *info_ptr,
18479 bool *need_reprocess)
18480 {
18481 struct dwarf2_cu *cu = reader->cu;
18482 struct dwarf2_per_objfile *dwarf2_per_objfile
18483 = cu->per_cu->dwarf2_per_objfile;
18484 struct objfile *objfile = dwarf2_per_objfile->objfile;
18485 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18486 bfd *abfd = reader->abfd;
18487 struct comp_unit_head *cu_header = &cu->header;
18488 unsigned int bytes_read;
18489 struct dwarf_block *blk;
18490 *need_reprocess = false;
18491
18492 attr->form = (enum dwarf_form) form;
18493 switch (form)
18494 {
18495 case DW_FORM_ref_addr:
18496 if (cu->header.version == 2)
18497 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18498 &bytes_read);
18499 else
18500 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18501 &bytes_read);
18502 info_ptr += bytes_read;
18503 break;
18504 case DW_FORM_GNU_ref_alt:
18505 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18506 info_ptr += bytes_read;
18507 break;
18508 case DW_FORM_addr:
18509 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18510 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18511 info_ptr += bytes_read;
18512 break;
18513 case DW_FORM_block2:
18514 blk = dwarf_alloc_block (cu);
18515 blk->size = read_2_bytes (abfd, info_ptr);
18516 info_ptr += 2;
18517 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18518 info_ptr += blk->size;
18519 DW_BLOCK (attr) = blk;
18520 break;
18521 case DW_FORM_block4:
18522 blk = dwarf_alloc_block (cu);
18523 blk->size = read_4_bytes (abfd, info_ptr);
18524 info_ptr += 4;
18525 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18526 info_ptr += blk->size;
18527 DW_BLOCK (attr) = blk;
18528 break;
18529 case DW_FORM_data2:
18530 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18531 info_ptr += 2;
18532 break;
18533 case DW_FORM_data4:
18534 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18535 info_ptr += 4;
18536 break;
18537 case DW_FORM_data8:
18538 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18539 info_ptr += 8;
18540 break;
18541 case DW_FORM_data16:
18542 blk = dwarf_alloc_block (cu);
18543 blk->size = 16;
18544 blk->data = read_n_bytes (abfd, info_ptr, 16);
18545 info_ptr += 16;
18546 DW_BLOCK (attr) = blk;
18547 break;
18548 case DW_FORM_sec_offset:
18549 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18550 info_ptr += bytes_read;
18551 break;
18552 case DW_FORM_string:
18553 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18554 DW_STRING_IS_CANONICAL (attr) = 0;
18555 info_ptr += bytes_read;
18556 break;
18557 case DW_FORM_strp:
18558 if (!cu->per_cu->is_dwz)
18559 {
18560 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18561 abfd, info_ptr, cu_header,
18562 &bytes_read);
18563 DW_STRING_IS_CANONICAL (attr) = 0;
18564 info_ptr += bytes_read;
18565 break;
18566 }
18567 /* FALLTHROUGH */
18568 case DW_FORM_line_strp:
18569 if (!cu->per_cu->is_dwz)
18570 {
18571 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18572 abfd, info_ptr,
18573 cu_header, &bytes_read);
18574 DW_STRING_IS_CANONICAL (attr) = 0;
18575 info_ptr += bytes_read;
18576 break;
18577 }
18578 /* FALLTHROUGH */
18579 case DW_FORM_GNU_strp_alt:
18580 {
18581 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18582 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18583 &bytes_read);
18584
18585 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18586 DW_STRING_IS_CANONICAL (attr) = 0;
18587 info_ptr += bytes_read;
18588 }
18589 break;
18590 case DW_FORM_exprloc:
18591 case DW_FORM_block:
18592 blk = dwarf_alloc_block (cu);
18593 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18594 info_ptr += bytes_read;
18595 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18596 info_ptr += blk->size;
18597 DW_BLOCK (attr) = blk;
18598 break;
18599 case DW_FORM_block1:
18600 blk = dwarf_alloc_block (cu);
18601 blk->size = read_1_byte (abfd, info_ptr);
18602 info_ptr += 1;
18603 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18604 info_ptr += blk->size;
18605 DW_BLOCK (attr) = blk;
18606 break;
18607 case DW_FORM_data1:
18608 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18609 info_ptr += 1;
18610 break;
18611 case DW_FORM_flag:
18612 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18613 info_ptr += 1;
18614 break;
18615 case DW_FORM_flag_present:
18616 DW_UNSND (attr) = 1;
18617 break;
18618 case DW_FORM_sdata:
18619 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18620 info_ptr += bytes_read;
18621 break;
18622 case DW_FORM_udata:
18623 case DW_FORM_rnglistx:
18624 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18625 info_ptr += bytes_read;
18626 break;
18627 case DW_FORM_ref1:
18628 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18629 + read_1_byte (abfd, info_ptr));
18630 info_ptr += 1;
18631 break;
18632 case DW_FORM_ref2:
18633 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18634 + read_2_bytes (abfd, info_ptr));
18635 info_ptr += 2;
18636 break;
18637 case DW_FORM_ref4:
18638 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18639 + read_4_bytes (abfd, info_ptr));
18640 info_ptr += 4;
18641 break;
18642 case DW_FORM_ref8:
18643 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18644 + read_8_bytes (abfd, info_ptr));
18645 info_ptr += 8;
18646 break;
18647 case DW_FORM_ref_sig8:
18648 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18649 info_ptr += 8;
18650 break;
18651 case DW_FORM_ref_udata:
18652 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18653 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18654 info_ptr += bytes_read;
18655 break;
18656 case DW_FORM_indirect:
18657 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18658 info_ptr += bytes_read;
18659 if (form == DW_FORM_implicit_const)
18660 {
18661 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18662 info_ptr += bytes_read;
18663 }
18664 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18665 info_ptr, need_reprocess);
18666 break;
18667 case DW_FORM_implicit_const:
18668 DW_SND (attr) = implicit_const;
18669 break;
18670 case DW_FORM_addrx:
18671 case DW_FORM_GNU_addr_index:
18672 *need_reprocess = true;
18673 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18674 info_ptr += bytes_read;
18675 break;
18676 case DW_FORM_strx:
18677 case DW_FORM_strx1:
18678 case DW_FORM_strx2:
18679 case DW_FORM_strx3:
18680 case DW_FORM_strx4:
18681 case DW_FORM_GNU_str_index:
18682 {
18683 ULONGEST str_index;
18684 if (form == DW_FORM_strx1)
18685 {
18686 str_index = read_1_byte (abfd, info_ptr);
18687 info_ptr += 1;
18688 }
18689 else if (form == DW_FORM_strx2)
18690 {
18691 str_index = read_2_bytes (abfd, info_ptr);
18692 info_ptr += 2;
18693 }
18694 else if (form == DW_FORM_strx3)
18695 {
18696 str_index = read_3_bytes (abfd, info_ptr);
18697 info_ptr += 3;
18698 }
18699 else if (form == DW_FORM_strx4)
18700 {
18701 str_index = read_4_bytes (abfd, info_ptr);
18702 info_ptr += 4;
18703 }
18704 else
18705 {
18706 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18707 info_ptr += bytes_read;
18708 }
18709 *need_reprocess = true;
18710 DW_UNSND (attr) = str_index;
18711 }
18712 break;
18713 default:
18714 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18715 dwarf_form_name (form),
18716 bfd_get_filename (abfd));
18717 }
18718
18719 /* Super hack. */
18720 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18721 attr->form = DW_FORM_GNU_ref_alt;
18722
18723 /* We have seen instances where the compiler tried to emit a byte
18724 size attribute of -1 which ended up being encoded as an unsigned
18725 0xffffffff. Although 0xffffffff is technically a valid size value,
18726 an object of this size seems pretty unlikely so we can relatively
18727 safely treat these cases as if the size attribute was invalid and
18728 treat them as zero by default. */
18729 if (attr->name == DW_AT_byte_size
18730 && form == DW_FORM_data4
18731 && DW_UNSND (attr) >= 0xffffffff)
18732 {
18733 complaint
18734 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18735 hex_string (DW_UNSND (attr)));
18736 DW_UNSND (attr) = 0;
18737 }
18738
18739 return info_ptr;
18740 }
18741
18742 /* Read an attribute described by an abbreviated attribute. */
18743
18744 static const gdb_byte *
18745 read_attribute (const struct die_reader_specs *reader,
18746 struct attribute *attr, struct attr_abbrev *abbrev,
18747 const gdb_byte *info_ptr, bool *need_reprocess)
18748 {
18749 attr->name = abbrev->name;
18750 return read_attribute_value (reader, attr, abbrev->form,
18751 abbrev->implicit_const, info_ptr,
18752 need_reprocess);
18753 }
18754
18755 /* Cover function for read_initial_length.
18756 Returns the length of the object at BUF, and stores the size of the
18757 initial length in *BYTES_READ and stores the size that offsets will be in
18758 *OFFSET_SIZE.
18759 If the initial length size is not equivalent to that specified in
18760 CU_HEADER then issue a complaint.
18761 This is useful when reading non-comp-unit headers. */
18762
18763 static LONGEST
18764 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
18765 const struct comp_unit_head *cu_header,
18766 unsigned int *bytes_read,
18767 unsigned int *offset_size)
18768 {
18769 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18770
18771 gdb_assert (cu_header->initial_length_size == 4
18772 || cu_header->initial_length_size == 8
18773 || cu_header->initial_length_size == 12);
18774
18775 if (cu_header->initial_length_size != *bytes_read)
18776 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
18777
18778 *offset_size = (*bytes_read == 4) ? 4 : 8;
18779 return length;
18780 }
18781
18782 /* Return pointer to string at section SECT offset STR_OFFSET with error
18783 reporting strings FORM_NAME and SECT_NAME. */
18784
18785 static const char *
18786 read_indirect_string_at_offset_from (struct objfile *objfile,
18787 bfd *abfd, LONGEST str_offset,
18788 struct dwarf2_section_info *sect,
18789 const char *form_name,
18790 const char *sect_name)
18791 {
18792 sect->read (objfile);
18793 if (sect->buffer == NULL)
18794 error (_("%s used without %s section [in module %s]"),
18795 form_name, sect_name, bfd_get_filename (abfd));
18796 if (str_offset >= sect->size)
18797 error (_("%s pointing outside of %s section [in module %s]"),
18798 form_name, sect_name, bfd_get_filename (abfd));
18799 gdb_assert (HOST_CHAR_BIT == 8);
18800 if (sect->buffer[str_offset] == '\0')
18801 return NULL;
18802 return (const char *) (sect->buffer + str_offset);
18803 }
18804
18805 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18806
18807 static const char *
18808 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18809 bfd *abfd, LONGEST str_offset)
18810 {
18811 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18812 abfd, str_offset,
18813 &dwarf2_per_objfile->str,
18814 "DW_FORM_strp", ".debug_str");
18815 }
18816
18817 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18818
18819 static const char *
18820 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18821 bfd *abfd, LONGEST str_offset)
18822 {
18823 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18824 abfd, str_offset,
18825 &dwarf2_per_objfile->line_str,
18826 "DW_FORM_line_strp",
18827 ".debug_line_str");
18828 }
18829
18830 /* Return pointer to string at .debug_str offset as read from BUF.
18831 BUF is assumed to be in a compilation unit described by CU_HEADER.
18832 Return *BYTES_READ_PTR count of bytes read from BUF. */
18833
18834 static const char *
18835 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18836 const gdb_byte *buf,
18837 const struct comp_unit_head *cu_header,
18838 unsigned int *bytes_read_ptr)
18839 {
18840 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18841
18842 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
18843 }
18844
18845 /* Return pointer to string at .debug_line_str offset as read from BUF.
18846 BUF is assumed to be in a compilation unit described by CU_HEADER.
18847 Return *BYTES_READ_PTR count of bytes read from BUF. */
18848
18849 static const char *
18850 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18851 bfd *abfd, const gdb_byte *buf,
18852 const struct comp_unit_head *cu_header,
18853 unsigned int *bytes_read_ptr)
18854 {
18855 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18856
18857 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18858 str_offset);
18859 }
18860
18861 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18862 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18863 ADDR_SIZE is the size of addresses from the CU header. */
18864
18865 static CORE_ADDR
18866 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18867 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18868 int addr_size)
18869 {
18870 struct objfile *objfile = dwarf2_per_objfile->objfile;
18871 bfd *abfd = objfile->obfd;
18872 const gdb_byte *info_ptr;
18873 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18874
18875 dwarf2_per_objfile->addr.read (objfile);
18876 if (dwarf2_per_objfile->addr.buffer == NULL)
18877 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18878 objfile_name (objfile));
18879 if (addr_base_or_zero + addr_index * addr_size
18880 >= dwarf2_per_objfile->addr.size)
18881 error (_("DW_FORM_addr_index pointing outside of "
18882 ".debug_addr section [in module %s]"),
18883 objfile_name (objfile));
18884 info_ptr = (dwarf2_per_objfile->addr.buffer
18885 + addr_base_or_zero + addr_index * addr_size);
18886 if (addr_size == 4)
18887 return bfd_get_32 (abfd, info_ptr);
18888 else
18889 return bfd_get_64 (abfd, info_ptr);
18890 }
18891
18892 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18893
18894 static CORE_ADDR
18895 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18896 {
18897 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18898 cu->addr_base, cu->header.addr_size);
18899 }
18900
18901 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18902
18903 static CORE_ADDR
18904 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18905 unsigned int *bytes_read)
18906 {
18907 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18908 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18909
18910 return read_addr_index (cu, addr_index);
18911 }
18912
18913 /* See read.h. */
18914
18915 CORE_ADDR
18916 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18917 {
18918 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18919 struct dwarf2_cu *cu = per_cu->cu;
18920 gdb::optional<ULONGEST> addr_base;
18921 int addr_size;
18922
18923 /* We need addr_base and addr_size.
18924 If we don't have PER_CU->cu, we have to get it.
18925 Nasty, but the alternative is storing the needed info in PER_CU,
18926 which at this point doesn't seem justified: it's not clear how frequently
18927 it would get used and it would increase the size of every PER_CU.
18928 Entry points like dwarf2_per_cu_addr_size do a similar thing
18929 so we're not in uncharted territory here.
18930 Alas we need to be a bit more complicated as addr_base is contained
18931 in the DIE.
18932
18933 We don't need to read the entire CU(/TU).
18934 We just need the header and top level die.
18935
18936 IWBN to use the aging mechanism to let us lazily later discard the CU.
18937 For now we skip this optimization. */
18938
18939 if (cu != NULL)
18940 {
18941 addr_base = cu->addr_base;
18942 addr_size = cu->header.addr_size;
18943 }
18944 else
18945 {
18946 cutu_reader reader (per_cu, NULL, 0, false);
18947 addr_base = reader.cu->addr_base;
18948 addr_size = reader.cu->header.addr_size;
18949 }
18950
18951 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18952 addr_size);
18953 }
18954
18955 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18956 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18957 DWO file. */
18958
18959 static const char *
18960 read_str_index (struct dwarf2_cu *cu,
18961 struct dwarf2_section_info *str_section,
18962 struct dwarf2_section_info *str_offsets_section,
18963 ULONGEST str_offsets_base, ULONGEST str_index)
18964 {
18965 struct dwarf2_per_objfile *dwarf2_per_objfile
18966 = cu->per_cu->dwarf2_per_objfile;
18967 struct objfile *objfile = dwarf2_per_objfile->objfile;
18968 const char *objf_name = objfile_name (objfile);
18969 bfd *abfd = objfile->obfd;
18970 const gdb_byte *info_ptr;
18971 ULONGEST str_offset;
18972 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18973
18974 str_section->read (objfile);
18975 str_offsets_section->read (objfile);
18976 if (str_section->buffer == NULL)
18977 error (_("%s used without %s section"
18978 " in CU at offset %s [in module %s]"),
18979 form_name, str_section->get_name (),
18980 sect_offset_str (cu->header.sect_off), objf_name);
18981 if (str_offsets_section->buffer == NULL)
18982 error (_("%s used without %s section"
18983 " in CU at offset %s [in module %s]"),
18984 form_name, str_section->get_name (),
18985 sect_offset_str (cu->header.sect_off), objf_name);
18986 info_ptr = (str_offsets_section->buffer
18987 + str_offsets_base
18988 + str_index * cu->header.offset_size);
18989 if (cu->header.offset_size == 4)
18990 str_offset = bfd_get_32 (abfd, info_ptr);
18991 else
18992 str_offset = bfd_get_64 (abfd, info_ptr);
18993 if (str_offset >= str_section->size)
18994 error (_("Offset from %s pointing outside of"
18995 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18996 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18997 return (const char *) (str_section->buffer + str_offset);
18998 }
18999
19000 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19001
19002 static const char *
19003 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19004 {
19005 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19006 ? reader->cu->header.addr_size : 0;
19007 return read_str_index (reader->cu,
19008 &reader->dwo_file->sections.str,
19009 &reader->dwo_file->sections.str_offsets,
19010 str_offsets_base, str_index);
19011 }
19012
19013 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19014
19015 static const char *
19016 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19017 {
19018 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19019 const char *objf_name = objfile_name (objfile);
19020 static const char form_name[] = "DW_FORM_GNU_str_index";
19021 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19022
19023 if (!cu->str_offsets_base.has_value ())
19024 error (_("%s used in Fission stub without %s"
19025 " in CU at offset 0x%lx [in module %s]"),
19026 form_name, str_offsets_attr_name,
19027 (long) cu->header.offset_size, objf_name);
19028
19029 return read_str_index (cu,
19030 &cu->per_cu->dwarf2_per_objfile->str,
19031 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19032 *cu->str_offsets_base, str_index);
19033 }
19034
19035 /* Return the length of an LEB128 number in BUF. */
19036
19037 static int
19038 leb128_size (const gdb_byte *buf)
19039 {
19040 const gdb_byte *begin = buf;
19041 gdb_byte byte;
19042
19043 while (1)
19044 {
19045 byte = *buf++;
19046 if ((byte & 128) == 0)
19047 return buf - begin;
19048 }
19049 }
19050
19051 static void
19052 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19053 {
19054 switch (lang)
19055 {
19056 case DW_LANG_C89:
19057 case DW_LANG_C99:
19058 case DW_LANG_C11:
19059 case DW_LANG_C:
19060 case DW_LANG_UPC:
19061 cu->language = language_c;
19062 break;
19063 case DW_LANG_Java:
19064 case DW_LANG_C_plus_plus:
19065 case DW_LANG_C_plus_plus_11:
19066 case DW_LANG_C_plus_plus_14:
19067 cu->language = language_cplus;
19068 break;
19069 case DW_LANG_D:
19070 cu->language = language_d;
19071 break;
19072 case DW_LANG_Fortran77:
19073 case DW_LANG_Fortran90:
19074 case DW_LANG_Fortran95:
19075 case DW_LANG_Fortran03:
19076 case DW_LANG_Fortran08:
19077 cu->language = language_fortran;
19078 break;
19079 case DW_LANG_Go:
19080 cu->language = language_go;
19081 break;
19082 case DW_LANG_Mips_Assembler:
19083 cu->language = language_asm;
19084 break;
19085 case DW_LANG_Ada83:
19086 case DW_LANG_Ada95:
19087 cu->language = language_ada;
19088 break;
19089 case DW_LANG_Modula2:
19090 cu->language = language_m2;
19091 break;
19092 case DW_LANG_Pascal83:
19093 cu->language = language_pascal;
19094 break;
19095 case DW_LANG_ObjC:
19096 cu->language = language_objc;
19097 break;
19098 case DW_LANG_Rust:
19099 case DW_LANG_Rust_old:
19100 cu->language = language_rust;
19101 break;
19102 case DW_LANG_Cobol74:
19103 case DW_LANG_Cobol85:
19104 default:
19105 cu->language = language_minimal;
19106 break;
19107 }
19108 cu->language_defn = language_def (cu->language);
19109 }
19110
19111 /* Return the named attribute or NULL if not there. */
19112
19113 static struct attribute *
19114 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19115 {
19116 for (;;)
19117 {
19118 unsigned int i;
19119 struct attribute *spec = NULL;
19120
19121 for (i = 0; i < die->num_attrs; ++i)
19122 {
19123 if (die->attrs[i].name == name)
19124 return &die->attrs[i];
19125 if (die->attrs[i].name == DW_AT_specification
19126 || die->attrs[i].name == DW_AT_abstract_origin)
19127 spec = &die->attrs[i];
19128 }
19129
19130 if (!spec)
19131 break;
19132
19133 die = follow_die_ref (die, spec, &cu);
19134 }
19135
19136 return NULL;
19137 }
19138
19139 /* Return the named attribute or NULL if not there,
19140 but do not follow DW_AT_specification, etc.
19141 This is for use in contexts where we're reading .debug_types dies.
19142 Following DW_AT_specification, DW_AT_abstract_origin will take us
19143 back up the chain, and we want to go down. */
19144
19145 static struct attribute *
19146 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19147 {
19148 unsigned int i;
19149
19150 for (i = 0; i < die->num_attrs; ++i)
19151 if (die->attrs[i].name == name)
19152 return &die->attrs[i];
19153
19154 return NULL;
19155 }
19156
19157 /* Return the string associated with a string-typed attribute, or NULL if it
19158 is either not found or is of an incorrect type. */
19159
19160 static const char *
19161 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19162 {
19163 struct attribute *attr;
19164 const char *str = NULL;
19165
19166 attr = dwarf2_attr (die, name, cu);
19167
19168 if (attr != NULL)
19169 {
19170 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19171 || attr->form == DW_FORM_string
19172 || attr->form == DW_FORM_strx
19173 || attr->form == DW_FORM_strx1
19174 || attr->form == DW_FORM_strx2
19175 || attr->form == DW_FORM_strx3
19176 || attr->form == DW_FORM_strx4
19177 || attr->form == DW_FORM_GNU_str_index
19178 || attr->form == DW_FORM_GNU_strp_alt)
19179 str = DW_STRING (attr);
19180 else
19181 complaint (_("string type expected for attribute %s for "
19182 "DIE at %s in module %s"),
19183 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19184 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19185 }
19186
19187 return str;
19188 }
19189
19190 /* Return the dwo name or NULL if not present. If present, it is in either
19191 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19192 static const char *
19193 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19194 {
19195 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19196 if (dwo_name == nullptr)
19197 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19198 return dwo_name;
19199 }
19200
19201 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19202 and holds a non-zero value. This function should only be used for
19203 DW_FORM_flag or DW_FORM_flag_present attributes. */
19204
19205 static int
19206 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19207 {
19208 struct attribute *attr = dwarf2_attr (die, name, cu);
19209
19210 return (attr && DW_UNSND (attr));
19211 }
19212
19213 static int
19214 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19215 {
19216 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19217 which value is non-zero. However, we have to be careful with
19218 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19219 (via dwarf2_flag_true_p) follows this attribute. So we may
19220 end up accidently finding a declaration attribute that belongs
19221 to a different DIE referenced by the specification attribute,
19222 even though the given DIE does not have a declaration attribute. */
19223 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19224 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19225 }
19226
19227 /* Return the die giving the specification for DIE, if there is
19228 one. *SPEC_CU is the CU containing DIE on input, and the CU
19229 containing the return value on output. If there is no
19230 specification, but there is an abstract origin, that is
19231 returned. */
19232
19233 static struct die_info *
19234 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19235 {
19236 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19237 *spec_cu);
19238
19239 if (spec_attr == NULL)
19240 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19241
19242 if (spec_attr == NULL)
19243 return NULL;
19244 else
19245 return follow_die_ref (die, spec_attr, spec_cu);
19246 }
19247
19248 /* Stub for free_line_header to match void * callback types. */
19249
19250 static void
19251 free_line_header_voidp (void *arg)
19252 {
19253 struct line_header *lh = (struct line_header *) arg;
19254
19255 delete lh;
19256 }
19257
19258 /* A convenience function to find the proper .debug_line section for a CU. */
19259
19260 static struct dwarf2_section_info *
19261 get_debug_line_section (struct dwarf2_cu *cu)
19262 {
19263 struct dwarf2_section_info *section;
19264 struct dwarf2_per_objfile *dwarf2_per_objfile
19265 = cu->per_cu->dwarf2_per_objfile;
19266
19267 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19268 DWO file. */
19269 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19270 section = &cu->dwo_unit->dwo_file->sections.line;
19271 else if (cu->per_cu->is_dwz)
19272 {
19273 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19274
19275 section = &dwz->line;
19276 }
19277 else
19278 section = &dwarf2_per_objfile->line;
19279
19280 return section;
19281 }
19282
19283 /* Read directory or file name entry format, starting with byte of
19284 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19285 entries count and the entries themselves in the described entry
19286 format. */
19287
19288 static void
19289 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19290 bfd *abfd, const gdb_byte **bufp,
19291 struct line_header *lh,
19292 const struct comp_unit_head *cu_header,
19293 void (*callback) (struct line_header *lh,
19294 const char *name,
19295 dir_index d_index,
19296 unsigned int mod_time,
19297 unsigned int length))
19298 {
19299 gdb_byte format_count, formati;
19300 ULONGEST data_count, datai;
19301 const gdb_byte *buf = *bufp;
19302 const gdb_byte *format_header_data;
19303 unsigned int bytes_read;
19304
19305 format_count = read_1_byte (abfd, buf);
19306 buf += 1;
19307 format_header_data = buf;
19308 for (formati = 0; formati < format_count; formati++)
19309 {
19310 read_unsigned_leb128 (abfd, buf, &bytes_read);
19311 buf += bytes_read;
19312 read_unsigned_leb128 (abfd, buf, &bytes_read);
19313 buf += bytes_read;
19314 }
19315
19316 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19317 buf += bytes_read;
19318 for (datai = 0; datai < data_count; datai++)
19319 {
19320 const gdb_byte *format = format_header_data;
19321 struct file_entry fe;
19322
19323 for (formati = 0; formati < format_count; formati++)
19324 {
19325 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19326 format += bytes_read;
19327
19328 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19329 format += bytes_read;
19330
19331 gdb::optional<const char *> string;
19332 gdb::optional<unsigned int> uint;
19333
19334 switch (form)
19335 {
19336 case DW_FORM_string:
19337 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19338 buf += bytes_read;
19339 break;
19340
19341 case DW_FORM_line_strp:
19342 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19343 abfd, buf,
19344 cu_header,
19345 &bytes_read));
19346 buf += bytes_read;
19347 break;
19348
19349 case DW_FORM_data1:
19350 uint.emplace (read_1_byte (abfd, buf));
19351 buf += 1;
19352 break;
19353
19354 case DW_FORM_data2:
19355 uint.emplace (read_2_bytes (abfd, buf));
19356 buf += 2;
19357 break;
19358
19359 case DW_FORM_data4:
19360 uint.emplace (read_4_bytes (abfd, buf));
19361 buf += 4;
19362 break;
19363
19364 case DW_FORM_data8:
19365 uint.emplace (read_8_bytes (abfd, buf));
19366 buf += 8;
19367 break;
19368
19369 case DW_FORM_data16:
19370 /* This is used for MD5, but file_entry does not record MD5s. */
19371 buf += 16;
19372 break;
19373
19374 case DW_FORM_udata:
19375 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19376 buf += bytes_read;
19377 break;
19378
19379 case DW_FORM_block:
19380 /* It is valid only for DW_LNCT_timestamp which is ignored by
19381 current GDB. */
19382 break;
19383 }
19384
19385 switch (content_type)
19386 {
19387 case DW_LNCT_path:
19388 if (string.has_value ())
19389 fe.name = *string;
19390 break;
19391 case DW_LNCT_directory_index:
19392 if (uint.has_value ())
19393 fe.d_index = (dir_index) *uint;
19394 break;
19395 case DW_LNCT_timestamp:
19396 if (uint.has_value ())
19397 fe.mod_time = *uint;
19398 break;
19399 case DW_LNCT_size:
19400 if (uint.has_value ())
19401 fe.length = *uint;
19402 break;
19403 case DW_LNCT_MD5:
19404 break;
19405 default:
19406 complaint (_("Unknown format content type %s"),
19407 pulongest (content_type));
19408 }
19409 }
19410
19411 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
19412 }
19413
19414 *bufp = buf;
19415 }
19416
19417 /* Read the statement program header starting at OFFSET in
19418 .debug_line, or .debug_line.dwo. Return a pointer
19419 to a struct line_header, allocated using xmalloc.
19420 Returns NULL if there is a problem reading the header, e.g., if it
19421 has a version we don't understand.
19422
19423 NOTE: the strings in the include directory and file name tables of
19424 the returned object point into the dwarf line section buffer,
19425 and must not be freed. */
19426
19427 static line_header_up
19428 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19429 {
19430 const gdb_byte *line_ptr;
19431 unsigned int bytes_read, offset_size;
19432 int i;
19433 const char *cur_dir, *cur_file;
19434 struct dwarf2_section_info *section;
19435 bfd *abfd;
19436 struct dwarf2_per_objfile *dwarf2_per_objfile
19437 = cu->per_cu->dwarf2_per_objfile;
19438
19439 section = get_debug_line_section (cu);
19440 section->read (dwarf2_per_objfile->objfile);
19441 if (section->buffer == NULL)
19442 {
19443 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19444 complaint (_("missing .debug_line.dwo section"));
19445 else
19446 complaint (_("missing .debug_line section"));
19447 return 0;
19448 }
19449
19450 /* We can't do this until we know the section is non-empty.
19451 Only then do we know we have such a section. */
19452 abfd = section->get_bfd_owner ();
19453
19454 /* Make sure that at least there's room for the total_length field.
19455 That could be 12 bytes long, but we're just going to fudge that. */
19456 if (to_underlying (sect_off) + 4 >= section->size)
19457 {
19458 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19459 return 0;
19460 }
19461
19462 line_header_up lh (new line_header ());
19463
19464 lh->sect_off = sect_off;
19465 lh->offset_in_dwz = cu->per_cu->is_dwz;
19466
19467 line_ptr = section->buffer + to_underlying (sect_off);
19468
19469 /* Read in the header. */
19470 lh->total_length =
19471 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19472 &bytes_read, &offset_size);
19473 line_ptr += bytes_read;
19474
19475 const gdb_byte *start_here = line_ptr;
19476
19477 if (line_ptr + lh->total_length > (section->buffer + section->size))
19478 {
19479 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19480 return 0;
19481 }
19482 lh->statement_program_end = start_here + lh->total_length;
19483 lh->version = read_2_bytes (abfd, line_ptr);
19484 line_ptr += 2;
19485 if (lh->version > 5)
19486 {
19487 /* This is a version we don't understand. The format could have
19488 changed in ways we don't handle properly so just punt. */
19489 complaint (_("unsupported version in .debug_line section"));
19490 return NULL;
19491 }
19492 if (lh->version >= 5)
19493 {
19494 gdb_byte segment_selector_size;
19495
19496 /* Skip address size. */
19497 read_1_byte (abfd, line_ptr);
19498 line_ptr += 1;
19499
19500 segment_selector_size = read_1_byte (abfd, line_ptr);
19501 line_ptr += 1;
19502 if (segment_selector_size != 0)
19503 {
19504 complaint (_("unsupported segment selector size %u "
19505 "in .debug_line section"),
19506 segment_selector_size);
19507 return NULL;
19508 }
19509 }
19510 lh->header_length = read_offset (abfd, line_ptr, offset_size);
19511 line_ptr += offset_size;
19512 lh->statement_program_start = line_ptr + lh->header_length;
19513 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19514 line_ptr += 1;
19515 if (lh->version >= 4)
19516 {
19517 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19518 line_ptr += 1;
19519 }
19520 else
19521 lh->maximum_ops_per_instruction = 1;
19522
19523 if (lh->maximum_ops_per_instruction == 0)
19524 {
19525 lh->maximum_ops_per_instruction = 1;
19526 complaint (_("invalid maximum_ops_per_instruction "
19527 "in `.debug_line' section"));
19528 }
19529
19530 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19531 line_ptr += 1;
19532 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19533 line_ptr += 1;
19534 lh->line_range = read_1_byte (abfd, line_ptr);
19535 line_ptr += 1;
19536 lh->opcode_base = read_1_byte (abfd, line_ptr);
19537 line_ptr += 1;
19538 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
19539
19540 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19541 for (i = 1; i < lh->opcode_base; ++i)
19542 {
19543 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19544 line_ptr += 1;
19545 }
19546
19547 if (lh->version >= 5)
19548 {
19549 /* Read directory table. */
19550 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19551 &cu->header,
19552 [] (struct line_header *header, const char *name,
19553 dir_index d_index, unsigned int mod_time,
19554 unsigned int length)
19555 {
19556 header->add_include_dir (name);
19557 });
19558
19559 /* Read file name table. */
19560 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19561 &cu->header,
19562 [] (struct line_header *header, const char *name,
19563 dir_index d_index, unsigned int mod_time,
19564 unsigned int length)
19565 {
19566 header->add_file_name (name, d_index, mod_time, length);
19567 });
19568 }
19569 else
19570 {
19571 /* Read directory table. */
19572 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19573 {
19574 line_ptr += bytes_read;
19575 lh->add_include_dir (cur_dir);
19576 }
19577 line_ptr += bytes_read;
19578
19579 /* Read file name table. */
19580 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19581 {
19582 unsigned int mod_time, length;
19583 dir_index d_index;
19584
19585 line_ptr += bytes_read;
19586 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19587 line_ptr += bytes_read;
19588 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19589 line_ptr += bytes_read;
19590 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19591 line_ptr += bytes_read;
19592
19593 lh->add_file_name (cur_file, d_index, mod_time, length);
19594 }
19595 line_ptr += bytes_read;
19596 }
19597
19598 if (line_ptr > (section->buffer + section->size))
19599 complaint (_("line number info header doesn't "
19600 "fit in `.debug_line' section"));
19601
19602 return lh;
19603 }
19604
19605 /* Subroutine of dwarf_decode_lines to simplify it.
19606 Return the file name of the psymtab for the given file_entry.
19607 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19608 If space for the result is malloc'd, *NAME_HOLDER will be set.
19609 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19610
19611 static const char *
19612 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19613 const dwarf2_psymtab *pst,
19614 const char *comp_dir,
19615 gdb::unique_xmalloc_ptr<char> *name_holder)
19616 {
19617 const char *include_name = fe.name;
19618 const char *include_name_to_compare = include_name;
19619 const char *pst_filename;
19620 int file_is_pst;
19621
19622 const char *dir_name = fe.include_dir (lh);
19623
19624 gdb::unique_xmalloc_ptr<char> hold_compare;
19625 if (!IS_ABSOLUTE_PATH (include_name)
19626 && (dir_name != NULL || comp_dir != NULL))
19627 {
19628 /* Avoid creating a duplicate psymtab for PST.
19629 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19630 Before we do the comparison, however, we need to account
19631 for DIR_NAME and COMP_DIR.
19632 First prepend dir_name (if non-NULL). If we still don't
19633 have an absolute path prepend comp_dir (if non-NULL).
19634 However, the directory we record in the include-file's
19635 psymtab does not contain COMP_DIR (to match the
19636 corresponding symtab(s)).
19637
19638 Example:
19639
19640 bash$ cd /tmp
19641 bash$ gcc -g ./hello.c
19642 include_name = "hello.c"
19643 dir_name = "."
19644 DW_AT_comp_dir = comp_dir = "/tmp"
19645 DW_AT_name = "./hello.c"
19646
19647 */
19648
19649 if (dir_name != NULL)
19650 {
19651 name_holder->reset (concat (dir_name, SLASH_STRING,
19652 include_name, (char *) NULL));
19653 include_name = name_holder->get ();
19654 include_name_to_compare = include_name;
19655 }
19656 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19657 {
19658 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19659 include_name, (char *) NULL));
19660 include_name_to_compare = hold_compare.get ();
19661 }
19662 }
19663
19664 pst_filename = pst->filename;
19665 gdb::unique_xmalloc_ptr<char> copied_name;
19666 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19667 {
19668 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19669 pst_filename, (char *) NULL));
19670 pst_filename = copied_name.get ();
19671 }
19672
19673 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19674
19675 if (file_is_pst)
19676 return NULL;
19677 return include_name;
19678 }
19679
19680 /* State machine to track the state of the line number program. */
19681
19682 class lnp_state_machine
19683 {
19684 public:
19685 /* Initialize a machine state for the start of a line number
19686 program. */
19687 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19688 bool record_lines_p);
19689
19690 file_entry *current_file ()
19691 {
19692 /* lh->file_names is 0-based, but the file name numbers in the
19693 statement program are 1-based. */
19694 return m_line_header->file_name_at (m_file);
19695 }
19696
19697 /* Record the line in the state machine. END_SEQUENCE is true if
19698 we're processing the end of a sequence. */
19699 void record_line (bool end_sequence);
19700
19701 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19702 nop-out rest of the lines in this sequence. */
19703 void check_line_address (struct dwarf2_cu *cu,
19704 const gdb_byte *line_ptr,
19705 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19706
19707 void handle_set_discriminator (unsigned int discriminator)
19708 {
19709 m_discriminator = discriminator;
19710 m_line_has_non_zero_discriminator |= discriminator != 0;
19711 }
19712
19713 /* Handle DW_LNE_set_address. */
19714 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19715 {
19716 m_op_index = 0;
19717 address += baseaddr;
19718 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19719 }
19720
19721 /* Handle DW_LNS_advance_pc. */
19722 void handle_advance_pc (CORE_ADDR adjust);
19723
19724 /* Handle a special opcode. */
19725 void handle_special_opcode (unsigned char op_code);
19726
19727 /* Handle DW_LNS_advance_line. */
19728 void handle_advance_line (int line_delta)
19729 {
19730 advance_line (line_delta);
19731 }
19732
19733 /* Handle DW_LNS_set_file. */
19734 void handle_set_file (file_name_index file);
19735
19736 /* Handle DW_LNS_negate_stmt. */
19737 void handle_negate_stmt ()
19738 {
19739 m_is_stmt = !m_is_stmt;
19740 }
19741
19742 /* Handle DW_LNS_const_add_pc. */
19743 void handle_const_add_pc ();
19744
19745 /* Handle DW_LNS_fixed_advance_pc. */
19746 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19747 {
19748 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19749 m_op_index = 0;
19750 }
19751
19752 /* Handle DW_LNS_copy. */
19753 void handle_copy ()
19754 {
19755 record_line (false);
19756 m_discriminator = 0;
19757 }
19758
19759 /* Handle DW_LNE_end_sequence. */
19760 void handle_end_sequence ()
19761 {
19762 m_currently_recording_lines = true;
19763 }
19764
19765 private:
19766 /* Advance the line by LINE_DELTA. */
19767 void advance_line (int line_delta)
19768 {
19769 m_line += line_delta;
19770
19771 if (line_delta != 0)
19772 m_line_has_non_zero_discriminator = m_discriminator != 0;
19773 }
19774
19775 struct dwarf2_cu *m_cu;
19776
19777 gdbarch *m_gdbarch;
19778
19779 /* True if we're recording lines.
19780 Otherwise we're building partial symtabs and are just interested in
19781 finding include files mentioned by the line number program. */
19782 bool m_record_lines_p;
19783
19784 /* The line number header. */
19785 line_header *m_line_header;
19786
19787 /* These are part of the standard DWARF line number state machine,
19788 and initialized according to the DWARF spec. */
19789
19790 unsigned char m_op_index = 0;
19791 /* The line table index of the current file. */
19792 file_name_index m_file = 1;
19793 unsigned int m_line = 1;
19794
19795 /* These are initialized in the constructor. */
19796
19797 CORE_ADDR m_address;
19798 bool m_is_stmt;
19799 unsigned int m_discriminator;
19800
19801 /* Additional bits of state we need to track. */
19802
19803 /* The last file that we called dwarf2_start_subfile for.
19804 This is only used for TLLs. */
19805 unsigned int m_last_file = 0;
19806 /* The last file a line number was recorded for. */
19807 struct subfile *m_last_subfile = NULL;
19808
19809 /* When true, record the lines we decode. */
19810 bool m_currently_recording_lines = false;
19811
19812 /* The last line number that was recorded, used to coalesce
19813 consecutive entries for the same line. This can happen, for
19814 example, when discriminators are present. PR 17276. */
19815 unsigned int m_last_line = 0;
19816 bool m_line_has_non_zero_discriminator = false;
19817 };
19818
19819 void
19820 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19821 {
19822 CORE_ADDR addr_adj = (((m_op_index + adjust)
19823 / m_line_header->maximum_ops_per_instruction)
19824 * m_line_header->minimum_instruction_length);
19825 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19826 m_op_index = ((m_op_index + adjust)
19827 % m_line_header->maximum_ops_per_instruction);
19828 }
19829
19830 void
19831 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19832 {
19833 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19834 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19835 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19836 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19837 / m_line_header->maximum_ops_per_instruction)
19838 * m_line_header->minimum_instruction_length);
19839 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19840 m_op_index = ((m_op_index + adj_opcode_d)
19841 % m_line_header->maximum_ops_per_instruction);
19842
19843 int line_delta = m_line_header->line_base + adj_opcode_r;
19844 advance_line (line_delta);
19845 record_line (false);
19846 m_discriminator = 0;
19847 }
19848
19849 void
19850 lnp_state_machine::handle_set_file (file_name_index file)
19851 {
19852 m_file = file;
19853
19854 const file_entry *fe = current_file ();
19855 if (fe == NULL)
19856 dwarf2_debug_line_missing_file_complaint ();
19857 else if (m_record_lines_p)
19858 {
19859 const char *dir = fe->include_dir (m_line_header);
19860
19861 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19862 m_line_has_non_zero_discriminator = m_discriminator != 0;
19863 dwarf2_start_subfile (m_cu, fe->name, dir);
19864 }
19865 }
19866
19867 void
19868 lnp_state_machine::handle_const_add_pc ()
19869 {
19870 CORE_ADDR adjust
19871 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19872
19873 CORE_ADDR addr_adj
19874 = (((m_op_index + adjust)
19875 / m_line_header->maximum_ops_per_instruction)
19876 * m_line_header->minimum_instruction_length);
19877
19878 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19879 m_op_index = ((m_op_index + adjust)
19880 % m_line_header->maximum_ops_per_instruction);
19881 }
19882
19883 /* Return non-zero if we should add LINE to the line number table.
19884 LINE is the line to add, LAST_LINE is the last line that was added,
19885 LAST_SUBFILE is the subfile for LAST_LINE.
19886 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19887 had a non-zero discriminator.
19888
19889 We have to be careful in the presence of discriminators.
19890 E.g., for this line:
19891
19892 for (i = 0; i < 100000; i++);
19893
19894 clang can emit four line number entries for that one line,
19895 each with a different discriminator.
19896 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19897
19898 However, we want gdb to coalesce all four entries into one.
19899 Otherwise the user could stepi into the middle of the line and
19900 gdb would get confused about whether the pc really was in the
19901 middle of the line.
19902
19903 Things are further complicated by the fact that two consecutive
19904 line number entries for the same line is a heuristic used by gcc
19905 to denote the end of the prologue. So we can't just discard duplicate
19906 entries, we have to be selective about it. The heuristic we use is
19907 that we only collapse consecutive entries for the same line if at least
19908 one of those entries has a non-zero discriminator. PR 17276.
19909
19910 Note: Addresses in the line number state machine can never go backwards
19911 within one sequence, thus this coalescing is ok. */
19912
19913 static int
19914 dwarf_record_line_p (struct dwarf2_cu *cu,
19915 unsigned int line, unsigned int last_line,
19916 int line_has_non_zero_discriminator,
19917 struct subfile *last_subfile)
19918 {
19919 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19920 return 1;
19921 if (line != last_line)
19922 return 1;
19923 /* Same line for the same file that we've seen already.
19924 As a last check, for pr 17276, only record the line if the line
19925 has never had a non-zero discriminator. */
19926 if (!line_has_non_zero_discriminator)
19927 return 1;
19928 return 0;
19929 }
19930
19931 /* Use the CU's builder to record line number LINE beginning at
19932 address ADDRESS in the line table of subfile SUBFILE. */
19933
19934 static void
19935 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19936 unsigned int line, CORE_ADDR address, bool is_stmt,
19937 struct dwarf2_cu *cu)
19938 {
19939 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19940
19941 if (dwarf_line_debug)
19942 {
19943 fprintf_unfiltered (gdb_stdlog,
19944 "Recording line %u, file %s, address %s\n",
19945 line, lbasename (subfile->name),
19946 paddress (gdbarch, address));
19947 }
19948
19949 if (cu != nullptr)
19950 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19951 }
19952
19953 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19954 Mark the end of a set of line number records.
19955 The arguments are the same as for dwarf_record_line_1.
19956 If SUBFILE is NULL the request is ignored. */
19957
19958 static void
19959 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19960 CORE_ADDR address, struct dwarf2_cu *cu)
19961 {
19962 if (subfile == NULL)
19963 return;
19964
19965 if (dwarf_line_debug)
19966 {
19967 fprintf_unfiltered (gdb_stdlog,
19968 "Finishing current line, file %s, address %s\n",
19969 lbasename (subfile->name),
19970 paddress (gdbarch, address));
19971 }
19972
19973 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19974 }
19975
19976 void
19977 lnp_state_machine::record_line (bool end_sequence)
19978 {
19979 if (dwarf_line_debug)
19980 {
19981 fprintf_unfiltered (gdb_stdlog,
19982 "Processing actual line %u: file %u,"
19983 " address %s, is_stmt %u, discrim %u%s\n",
19984 m_line, m_file,
19985 paddress (m_gdbarch, m_address),
19986 m_is_stmt, m_discriminator,
19987 (end_sequence ? "\t(end sequence)" : ""));
19988 }
19989
19990 file_entry *fe = current_file ();
19991
19992 if (fe == NULL)
19993 dwarf2_debug_line_missing_file_complaint ();
19994 /* For now we ignore lines not starting on an instruction boundary.
19995 But not when processing end_sequence for compatibility with the
19996 previous version of the code. */
19997 else if (m_op_index == 0 || end_sequence)
19998 {
19999 fe->included_p = 1;
20000 if (m_record_lines_p)
20001 {
20002 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20003 || end_sequence)
20004 {
20005 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20006 m_currently_recording_lines ? m_cu : nullptr);
20007 }
20008
20009 if (!end_sequence)
20010 {
20011 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20012
20013 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20014 m_line_has_non_zero_discriminator,
20015 m_last_subfile))
20016 {
20017 buildsym_compunit *builder = m_cu->get_builder ();
20018 dwarf_record_line_1 (m_gdbarch,
20019 builder->get_current_subfile (),
20020 m_line, m_address, is_stmt,
20021 m_currently_recording_lines ? m_cu : nullptr);
20022 }
20023 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20024 m_last_line = m_line;
20025 }
20026 }
20027 }
20028 }
20029
20030 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20031 line_header *lh, bool record_lines_p)
20032 {
20033 m_cu = cu;
20034 m_gdbarch = arch;
20035 m_record_lines_p = record_lines_p;
20036 m_line_header = lh;
20037
20038 m_currently_recording_lines = true;
20039
20040 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20041 was a line entry for it so that the backend has a chance to adjust it
20042 and also record it in case it needs it. This is currently used by MIPS
20043 code, cf. `mips_adjust_dwarf2_line'. */
20044 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20045 m_is_stmt = lh->default_is_stmt;
20046 m_discriminator = 0;
20047 }
20048
20049 void
20050 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20051 const gdb_byte *line_ptr,
20052 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20053 {
20054 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20055 the pc range of the CU. However, we restrict the test to only ADDRESS
20056 values of zero to preserve GDB's previous behaviour which is to handle
20057 the specific case of a function being GC'd by the linker. */
20058
20059 if (address == 0 && address < unrelocated_lowpc)
20060 {
20061 /* This line table is for a function which has been
20062 GCd by the linker. Ignore it. PR gdb/12528 */
20063
20064 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20065 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20066
20067 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20068 line_offset, objfile_name (objfile));
20069 m_currently_recording_lines = false;
20070 /* Note: m_currently_recording_lines is left as false until we see
20071 DW_LNE_end_sequence. */
20072 }
20073 }
20074
20075 /* Subroutine of dwarf_decode_lines to simplify it.
20076 Process the line number information in LH.
20077 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20078 program in order to set included_p for every referenced header. */
20079
20080 static void
20081 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20082 const int decode_for_pst_p, CORE_ADDR lowpc)
20083 {
20084 const gdb_byte *line_ptr, *extended_end;
20085 const gdb_byte *line_end;
20086 unsigned int bytes_read, extended_len;
20087 unsigned char op_code, extended_op;
20088 CORE_ADDR baseaddr;
20089 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20090 bfd *abfd = objfile->obfd;
20091 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20092 /* True if we're recording line info (as opposed to building partial
20093 symtabs and just interested in finding include files mentioned by
20094 the line number program). */
20095 bool record_lines_p = !decode_for_pst_p;
20096
20097 baseaddr = objfile->text_section_offset ();
20098
20099 line_ptr = lh->statement_program_start;
20100 line_end = lh->statement_program_end;
20101
20102 /* Read the statement sequences until there's nothing left. */
20103 while (line_ptr < line_end)
20104 {
20105 /* The DWARF line number program state machine. Reset the state
20106 machine at the start of each sequence. */
20107 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20108 bool end_sequence = false;
20109
20110 if (record_lines_p)
20111 {
20112 /* Start a subfile for the current file of the state
20113 machine. */
20114 const file_entry *fe = state_machine.current_file ();
20115
20116 if (fe != NULL)
20117 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20118 }
20119
20120 /* Decode the table. */
20121 while (line_ptr < line_end && !end_sequence)
20122 {
20123 op_code = read_1_byte (abfd, line_ptr);
20124 line_ptr += 1;
20125
20126 if (op_code >= lh->opcode_base)
20127 {
20128 /* Special opcode. */
20129 state_machine.handle_special_opcode (op_code);
20130 }
20131 else switch (op_code)
20132 {
20133 case DW_LNS_extended_op:
20134 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20135 &bytes_read);
20136 line_ptr += bytes_read;
20137 extended_end = line_ptr + extended_len;
20138 extended_op = read_1_byte (abfd, line_ptr);
20139 line_ptr += 1;
20140 switch (extended_op)
20141 {
20142 case DW_LNE_end_sequence:
20143 state_machine.handle_end_sequence ();
20144 end_sequence = true;
20145 break;
20146 case DW_LNE_set_address:
20147 {
20148 CORE_ADDR address
20149 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20150 line_ptr += bytes_read;
20151
20152 state_machine.check_line_address (cu, line_ptr,
20153 lowpc - baseaddr, address);
20154 state_machine.handle_set_address (baseaddr, address);
20155 }
20156 break;
20157 case DW_LNE_define_file:
20158 {
20159 const char *cur_file;
20160 unsigned int mod_time, length;
20161 dir_index dindex;
20162
20163 cur_file = read_direct_string (abfd, line_ptr,
20164 &bytes_read);
20165 line_ptr += bytes_read;
20166 dindex = (dir_index)
20167 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20168 line_ptr += bytes_read;
20169 mod_time =
20170 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20171 line_ptr += bytes_read;
20172 length =
20173 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20174 line_ptr += bytes_read;
20175 lh->add_file_name (cur_file, dindex, mod_time, length);
20176 }
20177 break;
20178 case DW_LNE_set_discriminator:
20179 {
20180 /* The discriminator is not interesting to the
20181 debugger; just ignore it. We still need to
20182 check its value though:
20183 if there are consecutive entries for the same
20184 (non-prologue) line we want to coalesce them.
20185 PR 17276. */
20186 unsigned int discr
20187 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20188 line_ptr += bytes_read;
20189
20190 state_machine.handle_set_discriminator (discr);
20191 }
20192 break;
20193 default:
20194 complaint (_("mangled .debug_line section"));
20195 return;
20196 }
20197 /* Make sure that we parsed the extended op correctly. If e.g.
20198 we expected a different address size than the producer used,
20199 we may have read the wrong number of bytes. */
20200 if (line_ptr != extended_end)
20201 {
20202 complaint (_("mangled .debug_line section"));
20203 return;
20204 }
20205 break;
20206 case DW_LNS_copy:
20207 state_machine.handle_copy ();
20208 break;
20209 case DW_LNS_advance_pc:
20210 {
20211 CORE_ADDR adjust
20212 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20213 line_ptr += bytes_read;
20214
20215 state_machine.handle_advance_pc (adjust);
20216 }
20217 break;
20218 case DW_LNS_advance_line:
20219 {
20220 int line_delta
20221 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20222 line_ptr += bytes_read;
20223
20224 state_machine.handle_advance_line (line_delta);
20225 }
20226 break;
20227 case DW_LNS_set_file:
20228 {
20229 file_name_index file
20230 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20231 &bytes_read);
20232 line_ptr += bytes_read;
20233
20234 state_machine.handle_set_file (file);
20235 }
20236 break;
20237 case DW_LNS_set_column:
20238 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20239 line_ptr += bytes_read;
20240 break;
20241 case DW_LNS_negate_stmt:
20242 state_machine.handle_negate_stmt ();
20243 break;
20244 case DW_LNS_set_basic_block:
20245 break;
20246 /* Add to the address register of the state machine the
20247 address increment value corresponding to special opcode
20248 255. I.e., this value is scaled by the minimum
20249 instruction length since special opcode 255 would have
20250 scaled the increment. */
20251 case DW_LNS_const_add_pc:
20252 state_machine.handle_const_add_pc ();
20253 break;
20254 case DW_LNS_fixed_advance_pc:
20255 {
20256 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20257 line_ptr += 2;
20258
20259 state_machine.handle_fixed_advance_pc (addr_adj);
20260 }
20261 break;
20262 default:
20263 {
20264 /* Unknown standard opcode, ignore it. */
20265 int i;
20266
20267 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20268 {
20269 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20270 line_ptr += bytes_read;
20271 }
20272 }
20273 }
20274 }
20275
20276 if (!end_sequence)
20277 dwarf2_debug_line_missing_end_sequence_complaint ();
20278
20279 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20280 in which case we still finish recording the last line). */
20281 state_machine.record_line (true);
20282 }
20283 }
20284
20285 /* Decode the Line Number Program (LNP) for the given line_header
20286 structure and CU. The actual information extracted and the type
20287 of structures created from the LNP depends on the value of PST.
20288
20289 1. If PST is NULL, then this procedure uses the data from the program
20290 to create all necessary symbol tables, and their linetables.
20291
20292 2. If PST is not NULL, this procedure reads the program to determine
20293 the list of files included by the unit represented by PST, and
20294 builds all the associated partial symbol tables.
20295
20296 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20297 It is used for relative paths in the line table.
20298 NOTE: When processing partial symtabs (pst != NULL),
20299 comp_dir == pst->dirname.
20300
20301 NOTE: It is important that psymtabs have the same file name (via strcmp)
20302 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20303 symtab we don't use it in the name of the psymtabs we create.
20304 E.g. expand_line_sal requires this when finding psymtabs to expand.
20305 A good testcase for this is mb-inline.exp.
20306
20307 LOWPC is the lowest address in CU (or 0 if not known).
20308
20309 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20310 for its PC<->lines mapping information. Otherwise only the filename
20311 table is read in. */
20312
20313 static void
20314 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20315 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20316 CORE_ADDR lowpc, int decode_mapping)
20317 {
20318 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20319 const int decode_for_pst_p = (pst != NULL);
20320
20321 if (decode_mapping)
20322 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20323
20324 if (decode_for_pst_p)
20325 {
20326 /* Now that we're done scanning the Line Header Program, we can
20327 create the psymtab of each included file. */
20328 for (auto &file_entry : lh->file_names ())
20329 if (file_entry.included_p == 1)
20330 {
20331 gdb::unique_xmalloc_ptr<char> name_holder;
20332 const char *include_name =
20333 psymtab_include_file_name (lh, file_entry, pst,
20334 comp_dir, &name_holder);
20335 if (include_name != NULL)
20336 dwarf2_create_include_psymtab (include_name, pst, objfile);
20337 }
20338 }
20339 else
20340 {
20341 /* Make sure a symtab is created for every file, even files
20342 which contain only variables (i.e. no code with associated
20343 line numbers). */
20344 buildsym_compunit *builder = cu->get_builder ();
20345 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20346
20347 for (auto &fe : lh->file_names ())
20348 {
20349 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20350 if (builder->get_current_subfile ()->symtab == NULL)
20351 {
20352 builder->get_current_subfile ()->symtab
20353 = allocate_symtab (cust,
20354 builder->get_current_subfile ()->name);
20355 }
20356 fe.symtab = builder->get_current_subfile ()->symtab;
20357 }
20358 }
20359 }
20360
20361 /* Start a subfile for DWARF. FILENAME is the name of the file and
20362 DIRNAME the name of the source directory which contains FILENAME
20363 or NULL if not known.
20364 This routine tries to keep line numbers from identical absolute and
20365 relative file names in a common subfile.
20366
20367 Using the `list' example from the GDB testsuite, which resides in
20368 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20369 of /srcdir/list0.c yields the following debugging information for list0.c:
20370
20371 DW_AT_name: /srcdir/list0.c
20372 DW_AT_comp_dir: /compdir
20373 files.files[0].name: list0.h
20374 files.files[0].dir: /srcdir
20375 files.files[1].name: list0.c
20376 files.files[1].dir: /srcdir
20377
20378 The line number information for list0.c has to end up in a single
20379 subfile, so that `break /srcdir/list0.c:1' works as expected.
20380 start_subfile will ensure that this happens provided that we pass the
20381 concatenation of files.files[1].dir and files.files[1].name as the
20382 subfile's name. */
20383
20384 static void
20385 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20386 const char *dirname)
20387 {
20388 gdb::unique_xmalloc_ptr<char> copy;
20389
20390 /* In order not to lose the line information directory,
20391 we concatenate it to the filename when it makes sense.
20392 Note that the Dwarf3 standard says (speaking of filenames in line
20393 information): ``The directory index is ignored for file names
20394 that represent full path names''. Thus ignoring dirname in the
20395 `else' branch below isn't an issue. */
20396
20397 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20398 {
20399 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20400 filename = copy.get ();
20401 }
20402
20403 cu->get_builder ()->start_subfile (filename);
20404 }
20405
20406 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20407 buildsym_compunit constructor. */
20408
20409 struct compunit_symtab *
20410 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20411 CORE_ADDR low_pc)
20412 {
20413 gdb_assert (m_builder == nullptr);
20414
20415 m_builder.reset (new struct buildsym_compunit
20416 (per_cu->dwarf2_per_objfile->objfile,
20417 name, comp_dir, language, low_pc));
20418
20419 list_in_scope = get_builder ()->get_file_symbols ();
20420
20421 get_builder ()->record_debugformat ("DWARF 2");
20422 get_builder ()->record_producer (producer);
20423
20424 processing_has_namespace_info = false;
20425
20426 return get_builder ()->get_compunit_symtab ();
20427 }
20428
20429 static void
20430 var_decode_location (struct attribute *attr, struct symbol *sym,
20431 struct dwarf2_cu *cu)
20432 {
20433 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20434 struct comp_unit_head *cu_header = &cu->header;
20435
20436 /* NOTE drow/2003-01-30: There used to be a comment and some special
20437 code here to turn a symbol with DW_AT_external and a
20438 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20439 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20440 with some versions of binutils) where shared libraries could have
20441 relocations against symbols in their debug information - the
20442 minimal symbol would have the right address, but the debug info
20443 would not. It's no longer necessary, because we will explicitly
20444 apply relocations when we read in the debug information now. */
20445
20446 /* A DW_AT_location attribute with no contents indicates that a
20447 variable has been optimized away. */
20448 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20449 {
20450 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20451 return;
20452 }
20453
20454 /* Handle one degenerate form of location expression specially, to
20455 preserve GDB's previous behavior when section offsets are
20456 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20457 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20458
20459 if (attr->form_is_block ()
20460 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20461 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20462 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20463 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20464 && (DW_BLOCK (attr)->size
20465 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20466 {
20467 unsigned int dummy;
20468
20469 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20470 SET_SYMBOL_VALUE_ADDRESS
20471 (sym, cu->header.read_address (objfile->obfd,
20472 DW_BLOCK (attr)->data + 1,
20473 &dummy));
20474 else
20475 SET_SYMBOL_VALUE_ADDRESS
20476 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20477 &dummy));
20478 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20479 fixup_symbol_section (sym, objfile);
20480 SET_SYMBOL_VALUE_ADDRESS
20481 (sym,
20482 SYMBOL_VALUE_ADDRESS (sym)
20483 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20484 return;
20485 }
20486
20487 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20488 expression evaluator, and use LOC_COMPUTED only when necessary
20489 (i.e. when the value of a register or memory location is
20490 referenced, or a thread-local block, etc.). Then again, it might
20491 not be worthwhile. I'm assuming that it isn't unless performance
20492 or memory numbers show me otherwise. */
20493
20494 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20495
20496 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20497 cu->has_loclist = true;
20498 }
20499
20500 /* Given a pointer to a DWARF information entry, figure out if we need
20501 to make a symbol table entry for it, and if so, create a new entry
20502 and return a pointer to it.
20503 If TYPE is NULL, determine symbol type from the die, otherwise
20504 used the passed type.
20505 If SPACE is not NULL, use it to hold the new symbol. If it is
20506 NULL, allocate a new symbol on the objfile's obstack. */
20507
20508 static struct symbol *
20509 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20510 struct symbol *space)
20511 {
20512 struct dwarf2_per_objfile *dwarf2_per_objfile
20513 = cu->per_cu->dwarf2_per_objfile;
20514 struct objfile *objfile = dwarf2_per_objfile->objfile;
20515 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20516 struct symbol *sym = NULL;
20517 const char *name;
20518 struct attribute *attr = NULL;
20519 struct attribute *attr2 = NULL;
20520 CORE_ADDR baseaddr;
20521 struct pending **list_to_add = NULL;
20522
20523 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20524
20525 baseaddr = objfile->text_section_offset ();
20526
20527 name = dwarf2_name (die, cu);
20528 if (name)
20529 {
20530 const char *linkagename;
20531 int suppress_add = 0;
20532
20533 if (space)
20534 sym = space;
20535 else
20536 sym = allocate_symbol (objfile);
20537 OBJSTAT (objfile, n_syms++);
20538
20539 /* Cache this symbol's name and the name's demangled form (if any). */
20540 sym->set_language (cu->language, &objfile->objfile_obstack);
20541 linkagename = dwarf2_physname (name, die, cu);
20542 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20543
20544 /* Fortran does not have mangling standard and the mangling does differ
20545 between gfortran, iFort etc. */
20546 if (cu->language == language_fortran
20547 && symbol_get_demangled_name (sym) == NULL)
20548 symbol_set_demangled_name (sym,
20549 dwarf2_full_name (name, die, cu),
20550 NULL);
20551
20552 /* Default assumptions.
20553 Use the passed type or decode it from the die. */
20554 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20555 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20556 if (type != NULL)
20557 SYMBOL_TYPE (sym) = type;
20558 else
20559 SYMBOL_TYPE (sym) = die_type (die, cu);
20560 attr = dwarf2_attr (die,
20561 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20562 cu);
20563 if (attr != nullptr)
20564 {
20565 SYMBOL_LINE (sym) = DW_UNSND (attr);
20566 }
20567
20568 attr = dwarf2_attr (die,
20569 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20570 cu);
20571 if (attr != nullptr)
20572 {
20573 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20574 struct file_entry *fe;
20575
20576 if (cu->line_header != NULL)
20577 fe = cu->line_header->file_name_at (file_index);
20578 else
20579 fe = NULL;
20580
20581 if (fe == NULL)
20582 complaint (_("file index out of range"));
20583 else
20584 symbol_set_symtab (sym, fe->symtab);
20585 }
20586
20587 switch (die->tag)
20588 {
20589 case DW_TAG_label:
20590 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20591 if (attr != nullptr)
20592 {
20593 CORE_ADDR addr;
20594
20595 addr = attr->value_as_address ();
20596 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20597 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20598 }
20599 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20600 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20601 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20602 add_symbol_to_list (sym, cu->list_in_scope);
20603 break;
20604 case DW_TAG_subprogram:
20605 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20606 finish_block. */
20607 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20608 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20609 if ((attr2 && (DW_UNSND (attr2) != 0))
20610 || cu->language == language_ada
20611 || cu->language == language_fortran)
20612 {
20613 /* Subprograms marked external are stored as a global symbol.
20614 Ada and Fortran subprograms, whether marked external or
20615 not, are always stored as a global symbol, because we want
20616 to be able to access them globally. For instance, we want
20617 to be able to break on a nested subprogram without having
20618 to specify the context. */
20619 list_to_add = cu->get_builder ()->get_global_symbols ();
20620 }
20621 else
20622 {
20623 list_to_add = cu->list_in_scope;
20624 }
20625 break;
20626 case DW_TAG_inlined_subroutine:
20627 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20628 finish_block. */
20629 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20630 SYMBOL_INLINED (sym) = 1;
20631 list_to_add = cu->list_in_scope;
20632 break;
20633 case DW_TAG_template_value_param:
20634 suppress_add = 1;
20635 /* Fall through. */
20636 case DW_TAG_constant:
20637 case DW_TAG_variable:
20638 case DW_TAG_member:
20639 /* Compilation with minimal debug info may result in
20640 variables with missing type entries. Change the
20641 misleading `void' type to something sensible. */
20642 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20643 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20644
20645 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20646 /* In the case of DW_TAG_member, we should only be called for
20647 static const members. */
20648 if (die->tag == DW_TAG_member)
20649 {
20650 /* dwarf2_add_field uses die_is_declaration,
20651 so we do the same. */
20652 gdb_assert (die_is_declaration (die, cu));
20653 gdb_assert (attr);
20654 }
20655 if (attr != nullptr)
20656 {
20657 dwarf2_const_value (attr, sym, cu);
20658 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20659 if (!suppress_add)
20660 {
20661 if (attr2 && (DW_UNSND (attr2) != 0))
20662 list_to_add = cu->get_builder ()->get_global_symbols ();
20663 else
20664 list_to_add = cu->list_in_scope;
20665 }
20666 break;
20667 }
20668 attr = dwarf2_attr (die, DW_AT_location, cu);
20669 if (attr != nullptr)
20670 {
20671 var_decode_location (attr, sym, cu);
20672 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20673
20674 /* Fortran explicitly imports any global symbols to the local
20675 scope by DW_TAG_common_block. */
20676 if (cu->language == language_fortran && die->parent
20677 && die->parent->tag == DW_TAG_common_block)
20678 attr2 = NULL;
20679
20680 if (SYMBOL_CLASS (sym) == LOC_STATIC
20681 && SYMBOL_VALUE_ADDRESS (sym) == 0
20682 && !dwarf2_per_objfile->has_section_at_zero)
20683 {
20684 /* When a static variable is eliminated by the linker,
20685 the corresponding debug information is not stripped
20686 out, but the variable address is set to null;
20687 do not add such variables into symbol table. */
20688 }
20689 else if (attr2 && (DW_UNSND (attr2) != 0))
20690 {
20691 if (SYMBOL_CLASS (sym) == LOC_STATIC
20692 && (objfile->flags & OBJF_MAINLINE) == 0
20693 && dwarf2_per_objfile->can_copy)
20694 {
20695 /* A global static variable might be subject to
20696 copy relocation. We first check for a local
20697 minsym, though, because maybe the symbol was
20698 marked hidden, in which case this would not
20699 apply. */
20700 bound_minimal_symbol found
20701 = (lookup_minimal_symbol_linkage
20702 (sym->linkage_name (), objfile));
20703 if (found.minsym != nullptr)
20704 sym->maybe_copied = 1;
20705 }
20706
20707 /* A variable with DW_AT_external is never static,
20708 but it may be block-scoped. */
20709 list_to_add
20710 = ((cu->list_in_scope
20711 == cu->get_builder ()->get_file_symbols ())
20712 ? cu->get_builder ()->get_global_symbols ()
20713 : cu->list_in_scope);
20714 }
20715 else
20716 list_to_add = cu->list_in_scope;
20717 }
20718 else
20719 {
20720 /* We do not know the address of this symbol.
20721 If it is an external symbol and we have type information
20722 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20723 The address of the variable will then be determined from
20724 the minimal symbol table whenever the variable is
20725 referenced. */
20726 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20727
20728 /* Fortran explicitly imports any global symbols to the local
20729 scope by DW_TAG_common_block. */
20730 if (cu->language == language_fortran && die->parent
20731 && die->parent->tag == DW_TAG_common_block)
20732 {
20733 /* SYMBOL_CLASS doesn't matter here because
20734 read_common_block is going to reset it. */
20735 if (!suppress_add)
20736 list_to_add = cu->list_in_scope;
20737 }
20738 else if (attr2 && (DW_UNSND (attr2) != 0)
20739 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20740 {
20741 /* A variable with DW_AT_external is never static, but it
20742 may be block-scoped. */
20743 list_to_add
20744 = ((cu->list_in_scope
20745 == cu->get_builder ()->get_file_symbols ())
20746 ? cu->get_builder ()->get_global_symbols ()
20747 : cu->list_in_scope);
20748
20749 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20750 }
20751 else if (!die_is_declaration (die, cu))
20752 {
20753 /* Use the default LOC_OPTIMIZED_OUT class. */
20754 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20755 if (!suppress_add)
20756 list_to_add = cu->list_in_scope;
20757 }
20758 }
20759 break;
20760 case DW_TAG_formal_parameter:
20761 {
20762 /* If we are inside a function, mark this as an argument. If
20763 not, we might be looking at an argument to an inlined function
20764 when we do not have enough information to show inlined frames;
20765 pretend it's a local variable in that case so that the user can
20766 still see it. */
20767 struct context_stack *curr
20768 = cu->get_builder ()->get_current_context_stack ();
20769 if (curr != nullptr && curr->name != nullptr)
20770 SYMBOL_IS_ARGUMENT (sym) = 1;
20771 attr = dwarf2_attr (die, DW_AT_location, cu);
20772 if (attr != nullptr)
20773 {
20774 var_decode_location (attr, sym, cu);
20775 }
20776 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20777 if (attr != nullptr)
20778 {
20779 dwarf2_const_value (attr, sym, cu);
20780 }
20781
20782 list_to_add = cu->list_in_scope;
20783 }
20784 break;
20785 case DW_TAG_unspecified_parameters:
20786 /* From varargs functions; gdb doesn't seem to have any
20787 interest in this information, so just ignore it for now.
20788 (FIXME?) */
20789 break;
20790 case DW_TAG_template_type_param:
20791 suppress_add = 1;
20792 /* Fall through. */
20793 case DW_TAG_class_type:
20794 case DW_TAG_interface_type:
20795 case DW_TAG_structure_type:
20796 case DW_TAG_union_type:
20797 case DW_TAG_set_type:
20798 case DW_TAG_enumeration_type:
20799 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20800 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20801
20802 {
20803 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20804 really ever be static objects: otherwise, if you try
20805 to, say, break of a class's method and you're in a file
20806 which doesn't mention that class, it won't work unless
20807 the check for all static symbols in lookup_symbol_aux
20808 saves you. See the OtherFileClass tests in
20809 gdb.c++/namespace.exp. */
20810
20811 if (!suppress_add)
20812 {
20813 buildsym_compunit *builder = cu->get_builder ();
20814 list_to_add
20815 = (cu->list_in_scope == builder->get_file_symbols ()
20816 && cu->language == language_cplus
20817 ? builder->get_global_symbols ()
20818 : cu->list_in_scope);
20819
20820 /* The semantics of C++ state that "struct foo {
20821 ... }" also defines a typedef for "foo". */
20822 if (cu->language == language_cplus
20823 || cu->language == language_ada
20824 || cu->language == language_d
20825 || cu->language == language_rust)
20826 {
20827 /* The symbol's name is already allocated along
20828 with this objfile, so we don't need to
20829 duplicate it for the type. */
20830 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20831 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20832 }
20833 }
20834 }
20835 break;
20836 case DW_TAG_typedef:
20837 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20838 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20839 list_to_add = cu->list_in_scope;
20840 break;
20841 case DW_TAG_base_type:
20842 case DW_TAG_subrange_type:
20843 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20844 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20845 list_to_add = cu->list_in_scope;
20846 break;
20847 case DW_TAG_enumerator:
20848 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20849 if (attr != nullptr)
20850 {
20851 dwarf2_const_value (attr, sym, cu);
20852 }
20853 {
20854 /* NOTE: carlton/2003-11-10: See comment above in the
20855 DW_TAG_class_type, etc. block. */
20856
20857 list_to_add
20858 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20859 && cu->language == language_cplus
20860 ? cu->get_builder ()->get_global_symbols ()
20861 : cu->list_in_scope);
20862 }
20863 break;
20864 case DW_TAG_imported_declaration:
20865 case DW_TAG_namespace:
20866 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20867 list_to_add = cu->get_builder ()->get_global_symbols ();
20868 break;
20869 case DW_TAG_module:
20870 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20871 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20872 list_to_add = cu->get_builder ()->get_global_symbols ();
20873 break;
20874 case DW_TAG_common_block:
20875 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20876 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20877 add_symbol_to_list (sym, cu->list_in_scope);
20878 break;
20879 default:
20880 /* Not a tag we recognize. Hopefully we aren't processing
20881 trash data, but since we must specifically ignore things
20882 we don't recognize, there is nothing else we should do at
20883 this point. */
20884 complaint (_("unsupported tag: '%s'"),
20885 dwarf_tag_name (die->tag));
20886 break;
20887 }
20888
20889 if (suppress_add)
20890 {
20891 sym->hash_next = objfile->template_symbols;
20892 objfile->template_symbols = sym;
20893 list_to_add = NULL;
20894 }
20895
20896 if (list_to_add != NULL)
20897 add_symbol_to_list (sym, list_to_add);
20898
20899 /* For the benefit of old versions of GCC, check for anonymous
20900 namespaces based on the demangled name. */
20901 if (!cu->processing_has_namespace_info
20902 && cu->language == language_cplus)
20903 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20904 }
20905 return (sym);
20906 }
20907
20908 /* Given an attr with a DW_FORM_dataN value in host byte order,
20909 zero-extend it as appropriate for the symbol's type. The DWARF
20910 standard (v4) is not entirely clear about the meaning of using
20911 DW_FORM_dataN for a constant with a signed type, where the type is
20912 wider than the data. The conclusion of a discussion on the DWARF
20913 list was that this is unspecified. We choose to always zero-extend
20914 because that is the interpretation long in use by GCC. */
20915
20916 static gdb_byte *
20917 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20918 struct dwarf2_cu *cu, LONGEST *value, int bits)
20919 {
20920 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20921 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20922 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20923 LONGEST l = DW_UNSND (attr);
20924
20925 if (bits < sizeof (*value) * 8)
20926 {
20927 l &= ((LONGEST) 1 << bits) - 1;
20928 *value = l;
20929 }
20930 else if (bits == sizeof (*value) * 8)
20931 *value = l;
20932 else
20933 {
20934 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20935 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20936 return bytes;
20937 }
20938
20939 return NULL;
20940 }
20941
20942 /* Read a constant value from an attribute. Either set *VALUE, or if
20943 the value does not fit in *VALUE, set *BYTES - either already
20944 allocated on the objfile obstack, or newly allocated on OBSTACK,
20945 or, set *BATON, if we translated the constant to a location
20946 expression. */
20947
20948 static void
20949 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20950 const char *name, struct obstack *obstack,
20951 struct dwarf2_cu *cu,
20952 LONGEST *value, const gdb_byte **bytes,
20953 struct dwarf2_locexpr_baton **baton)
20954 {
20955 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20956 struct comp_unit_head *cu_header = &cu->header;
20957 struct dwarf_block *blk;
20958 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20959 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20960
20961 *value = 0;
20962 *bytes = NULL;
20963 *baton = NULL;
20964
20965 switch (attr->form)
20966 {
20967 case DW_FORM_addr:
20968 case DW_FORM_addrx:
20969 case DW_FORM_GNU_addr_index:
20970 {
20971 gdb_byte *data;
20972
20973 if (TYPE_LENGTH (type) != cu_header->addr_size)
20974 dwarf2_const_value_length_mismatch_complaint (name,
20975 cu_header->addr_size,
20976 TYPE_LENGTH (type));
20977 /* Symbols of this form are reasonably rare, so we just
20978 piggyback on the existing location code rather than writing
20979 a new implementation of symbol_computed_ops. */
20980 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20981 (*baton)->per_cu = cu->per_cu;
20982 gdb_assert ((*baton)->per_cu);
20983
20984 (*baton)->size = 2 + cu_header->addr_size;
20985 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20986 (*baton)->data = data;
20987
20988 data[0] = DW_OP_addr;
20989 store_unsigned_integer (&data[1], cu_header->addr_size,
20990 byte_order, DW_ADDR (attr));
20991 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20992 }
20993 break;
20994 case DW_FORM_string:
20995 case DW_FORM_strp:
20996 case DW_FORM_strx:
20997 case DW_FORM_GNU_str_index:
20998 case DW_FORM_GNU_strp_alt:
20999 /* DW_STRING is already allocated on the objfile obstack, point
21000 directly to it. */
21001 *bytes = (const gdb_byte *) DW_STRING (attr);
21002 break;
21003 case DW_FORM_block1:
21004 case DW_FORM_block2:
21005 case DW_FORM_block4:
21006 case DW_FORM_block:
21007 case DW_FORM_exprloc:
21008 case DW_FORM_data16:
21009 blk = DW_BLOCK (attr);
21010 if (TYPE_LENGTH (type) != blk->size)
21011 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21012 TYPE_LENGTH (type));
21013 *bytes = blk->data;
21014 break;
21015
21016 /* The DW_AT_const_value attributes are supposed to carry the
21017 symbol's value "represented as it would be on the target
21018 architecture." By the time we get here, it's already been
21019 converted to host endianness, so we just need to sign- or
21020 zero-extend it as appropriate. */
21021 case DW_FORM_data1:
21022 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21023 break;
21024 case DW_FORM_data2:
21025 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21026 break;
21027 case DW_FORM_data4:
21028 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21029 break;
21030 case DW_FORM_data8:
21031 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21032 break;
21033
21034 case DW_FORM_sdata:
21035 case DW_FORM_implicit_const:
21036 *value = DW_SND (attr);
21037 break;
21038
21039 case DW_FORM_udata:
21040 *value = DW_UNSND (attr);
21041 break;
21042
21043 default:
21044 complaint (_("unsupported const value attribute form: '%s'"),
21045 dwarf_form_name (attr->form));
21046 *value = 0;
21047 break;
21048 }
21049 }
21050
21051
21052 /* Copy constant value from an attribute to a symbol. */
21053
21054 static void
21055 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21056 struct dwarf2_cu *cu)
21057 {
21058 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21059 LONGEST value;
21060 const gdb_byte *bytes;
21061 struct dwarf2_locexpr_baton *baton;
21062
21063 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21064 sym->print_name (),
21065 &objfile->objfile_obstack, cu,
21066 &value, &bytes, &baton);
21067
21068 if (baton != NULL)
21069 {
21070 SYMBOL_LOCATION_BATON (sym) = baton;
21071 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21072 }
21073 else if (bytes != NULL)
21074 {
21075 SYMBOL_VALUE_BYTES (sym) = bytes;
21076 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21077 }
21078 else
21079 {
21080 SYMBOL_VALUE (sym) = value;
21081 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21082 }
21083 }
21084
21085 /* Return the type of the die in question using its DW_AT_type attribute. */
21086
21087 static struct type *
21088 die_type (struct die_info *die, struct dwarf2_cu *cu)
21089 {
21090 struct attribute *type_attr;
21091
21092 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21093 if (!type_attr)
21094 {
21095 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21096 /* A missing DW_AT_type represents a void type. */
21097 return objfile_type (objfile)->builtin_void;
21098 }
21099
21100 return lookup_die_type (die, type_attr, cu);
21101 }
21102
21103 /* True iff CU's producer generates GNAT Ada auxiliary information
21104 that allows to find parallel types through that information instead
21105 of having to do expensive parallel lookups by type name. */
21106
21107 static int
21108 need_gnat_info (struct dwarf2_cu *cu)
21109 {
21110 /* Assume that the Ada compiler was GNAT, which always produces
21111 the auxiliary information. */
21112 return (cu->language == language_ada);
21113 }
21114
21115 /* Return the auxiliary type of the die in question using its
21116 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21117 attribute is not present. */
21118
21119 static struct type *
21120 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21121 {
21122 struct attribute *type_attr;
21123
21124 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21125 if (!type_attr)
21126 return NULL;
21127
21128 return lookup_die_type (die, type_attr, cu);
21129 }
21130
21131 /* If DIE has a descriptive_type attribute, then set the TYPE's
21132 descriptive type accordingly. */
21133
21134 static void
21135 set_descriptive_type (struct type *type, struct die_info *die,
21136 struct dwarf2_cu *cu)
21137 {
21138 struct type *descriptive_type = die_descriptive_type (die, cu);
21139
21140 if (descriptive_type)
21141 {
21142 ALLOCATE_GNAT_AUX_TYPE (type);
21143 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21144 }
21145 }
21146
21147 /* Return the containing type of the die in question using its
21148 DW_AT_containing_type attribute. */
21149
21150 static struct type *
21151 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21152 {
21153 struct attribute *type_attr;
21154 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21155
21156 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21157 if (!type_attr)
21158 error (_("Dwarf Error: Problem turning containing type into gdb type "
21159 "[in module %s]"), objfile_name (objfile));
21160
21161 return lookup_die_type (die, type_attr, cu);
21162 }
21163
21164 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21165
21166 static struct type *
21167 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
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 char *saved;
21173
21174 std::string message
21175 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21176 objfile_name (objfile),
21177 sect_offset_str (cu->header.sect_off),
21178 sect_offset_str (die->sect_off));
21179 saved = obstack_strdup (&objfile->objfile_obstack, message);
21180
21181 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21182 }
21183
21184 /* Look up the type of DIE in CU using its type attribute ATTR.
21185 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21186 DW_AT_containing_type.
21187 If there is no type substitute an error marker. */
21188
21189 static struct type *
21190 lookup_die_type (struct die_info *die, const struct attribute *attr,
21191 struct dwarf2_cu *cu)
21192 {
21193 struct dwarf2_per_objfile *dwarf2_per_objfile
21194 = cu->per_cu->dwarf2_per_objfile;
21195 struct objfile *objfile = dwarf2_per_objfile->objfile;
21196 struct type *this_type;
21197
21198 gdb_assert (attr->name == DW_AT_type
21199 || attr->name == DW_AT_GNAT_descriptive_type
21200 || attr->name == DW_AT_containing_type);
21201
21202 /* First see if we have it cached. */
21203
21204 if (attr->form == DW_FORM_GNU_ref_alt)
21205 {
21206 struct dwarf2_per_cu_data *per_cu;
21207 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21208
21209 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21210 dwarf2_per_objfile);
21211 this_type = get_die_type_at_offset (sect_off, per_cu);
21212 }
21213 else if (attr->form_is_ref ())
21214 {
21215 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21216
21217 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21218 }
21219 else if (attr->form == DW_FORM_ref_sig8)
21220 {
21221 ULONGEST signature = DW_SIGNATURE (attr);
21222
21223 return get_signatured_type (die, signature, cu);
21224 }
21225 else
21226 {
21227 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21228 " at %s [in module %s]"),
21229 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21230 objfile_name (objfile));
21231 return build_error_marker_type (cu, die);
21232 }
21233
21234 /* If not cached we need to read it in. */
21235
21236 if (this_type == NULL)
21237 {
21238 struct die_info *type_die = NULL;
21239 struct dwarf2_cu *type_cu = cu;
21240
21241 if (attr->form_is_ref ())
21242 type_die = follow_die_ref (die, attr, &type_cu);
21243 if (type_die == NULL)
21244 return build_error_marker_type (cu, die);
21245 /* If we find the type now, it's probably because the type came
21246 from an inter-CU reference and the type's CU got expanded before
21247 ours. */
21248 this_type = read_type_die (type_die, type_cu);
21249 }
21250
21251 /* If we still don't have a type use an error marker. */
21252
21253 if (this_type == NULL)
21254 return build_error_marker_type (cu, die);
21255
21256 return this_type;
21257 }
21258
21259 /* Return the type in DIE, CU.
21260 Returns NULL for invalid types.
21261
21262 This first does a lookup in die_type_hash,
21263 and only reads the die in if necessary.
21264
21265 NOTE: This can be called when reading in partial or full symbols. */
21266
21267 static struct type *
21268 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21269 {
21270 struct type *this_type;
21271
21272 this_type = get_die_type (die, cu);
21273 if (this_type)
21274 return this_type;
21275
21276 return read_type_die_1 (die, cu);
21277 }
21278
21279 /* Read the type in DIE, CU.
21280 Returns NULL for invalid types. */
21281
21282 static struct type *
21283 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21284 {
21285 struct type *this_type = NULL;
21286
21287 switch (die->tag)
21288 {
21289 case DW_TAG_class_type:
21290 case DW_TAG_interface_type:
21291 case DW_TAG_structure_type:
21292 case DW_TAG_union_type:
21293 this_type = read_structure_type (die, cu);
21294 break;
21295 case DW_TAG_enumeration_type:
21296 this_type = read_enumeration_type (die, cu);
21297 break;
21298 case DW_TAG_subprogram:
21299 case DW_TAG_subroutine_type:
21300 case DW_TAG_inlined_subroutine:
21301 this_type = read_subroutine_type (die, cu);
21302 break;
21303 case DW_TAG_array_type:
21304 this_type = read_array_type (die, cu);
21305 break;
21306 case DW_TAG_set_type:
21307 this_type = read_set_type (die, cu);
21308 break;
21309 case DW_TAG_pointer_type:
21310 this_type = read_tag_pointer_type (die, cu);
21311 break;
21312 case DW_TAG_ptr_to_member_type:
21313 this_type = read_tag_ptr_to_member_type (die, cu);
21314 break;
21315 case DW_TAG_reference_type:
21316 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21317 break;
21318 case DW_TAG_rvalue_reference_type:
21319 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21320 break;
21321 case DW_TAG_const_type:
21322 this_type = read_tag_const_type (die, cu);
21323 break;
21324 case DW_TAG_volatile_type:
21325 this_type = read_tag_volatile_type (die, cu);
21326 break;
21327 case DW_TAG_restrict_type:
21328 this_type = read_tag_restrict_type (die, cu);
21329 break;
21330 case DW_TAG_string_type:
21331 this_type = read_tag_string_type (die, cu);
21332 break;
21333 case DW_TAG_typedef:
21334 this_type = read_typedef (die, cu);
21335 break;
21336 case DW_TAG_subrange_type:
21337 this_type = read_subrange_type (die, cu);
21338 break;
21339 case DW_TAG_base_type:
21340 this_type = read_base_type (die, cu);
21341 break;
21342 case DW_TAG_unspecified_type:
21343 this_type = read_unspecified_type (die, cu);
21344 break;
21345 case DW_TAG_namespace:
21346 this_type = read_namespace_type (die, cu);
21347 break;
21348 case DW_TAG_module:
21349 this_type = read_module_type (die, cu);
21350 break;
21351 case DW_TAG_atomic_type:
21352 this_type = read_tag_atomic_type (die, cu);
21353 break;
21354 default:
21355 complaint (_("unexpected tag in read_type_die: '%s'"),
21356 dwarf_tag_name (die->tag));
21357 break;
21358 }
21359
21360 return this_type;
21361 }
21362
21363 /* See if we can figure out if the class lives in a namespace. We do
21364 this by looking for a member function; its demangled name will
21365 contain namespace info, if there is any.
21366 Return the computed name or NULL.
21367 Space for the result is allocated on the objfile's obstack.
21368 This is the full-die version of guess_partial_die_structure_name.
21369 In this case we know DIE has no useful parent. */
21370
21371 static const char *
21372 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21373 {
21374 struct die_info *spec_die;
21375 struct dwarf2_cu *spec_cu;
21376 struct die_info *child;
21377 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21378
21379 spec_cu = cu;
21380 spec_die = die_specification (die, &spec_cu);
21381 if (spec_die != NULL)
21382 {
21383 die = spec_die;
21384 cu = spec_cu;
21385 }
21386
21387 for (child = die->child;
21388 child != NULL;
21389 child = child->sibling)
21390 {
21391 if (child->tag == DW_TAG_subprogram)
21392 {
21393 const char *linkage_name = dw2_linkage_name (child, cu);
21394
21395 if (linkage_name != NULL)
21396 {
21397 gdb::unique_xmalloc_ptr<char> actual_name
21398 (language_class_name_from_physname (cu->language_defn,
21399 linkage_name));
21400 const char *name = NULL;
21401
21402 if (actual_name != NULL)
21403 {
21404 const char *die_name = dwarf2_name (die, cu);
21405
21406 if (die_name != NULL
21407 && strcmp (die_name, actual_name.get ()) != 0)
21408 {
21409 /* Strip off the class name from the full name.
21410 We want the prefix. */
21411 int die_name_len = strlen (die_name);
21412 int actual_name_len = strlen (actual_name.get ());
21413 const char *ptr = actual_name.get ();
21414
21415 /* Test for '::' as a sanity check. */
21416 if (actual_name_len > die_name_len + 2
21417 && ptr[actual_name_len - die_name_len - 1] == ':')
21418 name = obstack_strndup (
21419 &objfile->per_bfd->storage_obstack,
21420 ptr, actual_name_len - die_name_len - 2);
21421 }
21422 }
21423 return name;
21424 }
21425 }
21426 }
21427
21428 return NULL;
21429 }
21430
21431 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21432 prefix part in such case. See
21433 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21434
21435 static const char *
21436 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21437 {
21438 struct attribute *attr;
21439 const char *base;
21440
21441 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21442 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21443 return NULL;
21444
21445 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21446 return NULL;
21447
21448 attr = dw2_linkage_name_attr (die, cu);
21449 if (attr == NULL || DW_STRING (attr) == NULL)
21450 return NULL;
21451
21452 /* dwarf2_name had to be already called. */
21453 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21454
21455 /* Strip the base name, keep any leading namespaces/classes. */
21456 base = strrchr (DW_STRING (attr), ':');
21457 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21458 return "";
21459
21460 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21461 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21462 DW_STRING (attr),
21463 &base[-1] - DW_STRING (attr));
21464 }
21465
21466 /* Return the name of the namespace/class that DIE is defined within,
21467 or "" if we can't tell. The caller should not xfree the result.
21468
21469 For example, if we're within the method foo() in the following
21470 code:
21471
21472 namespace N {
21473 class C {
21474 void foo () {
21475 }
21476 };
21477 }
21478
21479 then determine_prefix on foo's die will return "N::C". */
21480
21481 static const char *
21482 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21483 {
21484 struct dwarf2_per_objfile *dwarf2_per_objfile
21485 = cu->per_cu->dwarf2_per_objfile;
21486 struct die_info *parent, *spec_die;
21487 struct dwarf2_cu *spec_cu;
21488 struct type *parent_type;
21489 const char *retval;
21490
21491 if (cu->language != language_cplus
21492 && cu->language != language_fortran && cu->language != language_d
21493 && cu->language != language_rust)
21494 return "";
21495
21496 retval = anonymous_struct_prefix (die, cu);
21497 if (retval)
21498 return retval;
21499
21500 /* We have to be careful in the presence of DW_AT_specification.
21501 For example, with GCC 3.4, given the code
21502
21503 namespace N {
21504 void foo() {
21505 // Definition of N::foo.
21506 }
21507 }
21508
21509 then we'll have a tree of DIEs like this:
21510
21511 1: DW_TAG_compile_unit
21512 2: DW_TAG_namespace // N
21513 3: DW_TAG_subprogram // declaration of N::foo
21514 4: DW_TAG_subprogram // definition of N::foo
21515 DW_AT_specification // refers to die #3
21516
21517 Thus, when processing die #4, we have to pretend that we're in
21518 the context of its DW_AT_specification, namely the contex of die
21519 #3. */
21520 spec_cu = cu;
21521 spec_die = die_specification (die, &spec_cu);
21522 if (spec_die == NULL)
21523 parent = die->parent;
21524 else
21525 {
21526 parent = spec_die->parent;
21527 cu = spec_cu;
21528 }
21529
21530 if (parent == NULL)
21531 return "";
21532 else if (parent->building_fullname)
21533 {
21534 const char *name;
21535 const char *parent_name;
21536
21537 /* It has been seen on RealView 2.2 built binaries,
21538 DW_TAG_template_type_param types actually _defined_ as
21539 children of the parent class:
21540
21541 enum E {};
21542 template class <class Enum> Class{};
21543 Class<enum E> class_e;
21544
21545 1: DW_TAG_class_type (Class)
21546 2: DW_TAG_enumeration_type (E)
21547 3: DW_TAG_enumerator (enum1:0)
21548 3: DW_TAG_enumerator (enum2:1)
21549 ...
21550 2: DW_TAG_template_type_param
21551 DW_AT_type DW_FORM_ref_udata (E)
21552
21553 Besides being broken debug info, it can put GDB into an
21554 infinite loop. Consider:
21555
21556 When we're building the full name for Class<E>, we'll start
21557 at Class, and go look over its template type parameters,
21558 finding E. We'll then try to build the full name of E, and
21559 reach here. We're now trying to build the full name of E,
21560 and look over the parent DIE for containing scope. In the
21561 broken case, if we followed the parent DIE of E, we'd again
21562 find Class, and once again go look at its template type
21563 arguments, etc., etc. Simply don't consider such parent die
21564 as source-level parent of this die (it can't be, the language
21565 doesn't allow it), and break the loop here. */
21566 name = dwarf2_name (die, cu);
21567 parent_name = dwarf2_name (parent, cu);
21568 complaint (_("template param type '%s' defined within parent '%s'"),
21569 name ? name : "<unknown>",
21570 parent_name ? parent_name : "<unknown>");
21571 return "";
21572 }
21573 else
21574 switch (parent->tag)
21575 {
21576 case DW_TAG_namespace:
21577 parent_type = read_type_die (parent, cu);
21578 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21579 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21580 Work around this problem here. */
21581 if (cu->language == language_cplus
21582 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21583 return "";
21584 /* We give a name to even anonymous namespaces. */
21585 return TYPE_NAME (parent_type);
21586 case DW_TAG_class_type:
21587 case DW_TAG_interface_type:
21588 case DW_TAG_structure_type:
21589 case DW_TAG_union_type:
21590 case DW_TAG_module:
21591 parent_type = read_type_die (parent, cu);
21592 if (TYPE_NAME (parent_type) != NULL)
21593 return TYPE_NAME (parent_type);
21594 else
21595 /* An anonymous structure is only allowed non-static data
21596 members; no typedefs, no member functions, et cetera.
21597 So it does not need a prefix. */
21598 return "";
21599 case DW_TAG_compile_unit:
21600 case DW_TAG_partial_unit:
21601 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21602 if (cu->language == language_cplus
21603 && !dwarf2_per_objfile->types.empty ()
21604 && die->child != NULL
21605 && (die->tag == DW_TAG_class_type
21606 || die->tag == DW_TAG_structure_type
21607 || die->tag == DW_TAG_union_type))
21608 {
21609 const char *name = guess_full_die_structure_name (die, cu);
21610 if (name != NULL)
21611 return name;
21612 }
21613 return "";
21614 case DW_TAG_subprogram:
21615 /* Nested subroutines in Fortran get a prefix with the name
21616 of the parent's subroutine. */
21617 if (cu->language == language_fortran)
21618 {
21619 if ((die->tag == DW_TAG_subprogram)
21620 && (dwarf2_name (parent, cu) != NULL))
21621 return dwarf2_name (parent, cu);
21622 }
21623 return determine_prefix (parent, cu);
21624 case DW_TAG_enumeration_type:
21625 parent_type = read_type_die (parent, cu);
21626 if (TYPE_DECLARED_CLASS (parent_type))
21627 {
21628 if (TYPE_NAME (parent_type) != NULL)
21629 return TYPE_NAME (parent_type);
21630 return "";
21631 }
21632 /* Fall through. */
21633 default:
21634 return determine_prefix (parent, cu);
21635 }
21636 }
21637
21638 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21639 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21640 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21641 an obconcat, otherwise allocate storage for the result. The CU argument is
21642 used to determine the language and hence, the appropriate separator. */
21643
21644 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21645
21646 static char *
21647 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21648 int physname, struct dwarf2_cu *cu)
21649 {
21650 const char *lead = "";
21651 const char *sep;
21652
21653 if (suffix == NULL || suffix[0] == '\0'
21654 || prefix == NULL || prefix[0] == '\0')
21655 sep = "";
21656 else if (cu->language == language_d)
21657 {
21658 /* For D, the 'main' function could be defined in any module, but it
21659 should never be prefixed. */
21660 if (strcmp (suffix, "D main") == 0)
21661 {
21662 prefix = "";
21663 sep = "";
21664 }
21665 else
21666 sep = ".";
21667 }
21668 else if (cu->language == language_fortran && physname)
21669 {
21670 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21671 DW_AT_MIPS_linkage_name is preferred and used instead. */
21672
21673 lead = "__";
21674 sep = "_MOD_";
21675 }
21676 else
21677 sep = "::";
21678
21679 if (prefix == NULL)
21680 prefix = "";
21681 if (suffix == NULL)
21682 suffix = "";
21683
21684 if (obs == NULL)
21685 {
21686 char *retval
21687 = ((char *)
21688 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21689
21690 strcpy (retval, lead);
21691 strcat (retval, prefix);
21692 strcat (retval, sep);
21693 strcat (retval, suffix);
21694 return retval;
21695 }
21696 else
21697 {
21698 /* We have an obstack. */
21699 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21700 }
21701 }
21702
21703 /* Return sibling of die, NULL if no sibling. */
21704
21705 static struct die_info *
21706 sibling_die (struct die_info *die)
21707 {
21708 return die->sibling;
21709 }
21710
21711 /* Get name of a die, return NULL if not found. */
21712
21713 static const char *
21714 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21715 struct objfile *objfile)
21716 {
21717 if (name && cu->language == language_cplus)
21718 {
21719 std::string canon_name = cp_canonicalize_string (name);
21720
21721 if (!canon_name.empty ())
21722 {
21723 if (canon_name != name)
21724 name = objfile->intern (canon_name);
21725 }
21726 }
21727
21728 return name;
21729 }
21730
21731 /* Get name of a die, return NULL if not found.
21732 Anonymous namespaces are converted to their magic string. */
21733
21734 static const char *
21735 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21736 {
21737 struct attribute *attr;
21738 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21739
21740 attr = dwarf2_attr (die, DW_AT_name, cu);
21741 if ((!attr || !DW_STRING (attr))
21742 && die->tag != DW_TAG_namespace
21743 && die->tag != DW_TAG_class_type
21744 && die->tag != DW_TAG_interface_type
21745 && die->tag != DW_TAG_structure_type
21746 && die->tag != DW_TAG_union_type)
21747 return NULL;
21748
21749 switch (die->tag)
21750 {
21751 case DW_TAG_compile_unit:
21752 case DW_TAG_partial_unit:
21753 /* Compilation units have a DW_AT_name that is a filename, not
21754 a source language identifier. */
21755 case DW_TAG_enumeration_type:
21756 case DW_TAG_enumerator:
21757 /* These tags always have simple identifiers already; no need
21758 to canonicalize them. */
21759 return DW_STRING (attr);
21760
21761 case DW_TAG_namespace:
21762 if (attr != NULL && DW_STRING (attr) != NULL)
21763 return DW_STRING (attr);
21764 return CP_ANONYMOUS_NAMESPACE_STR;
21765
21766 case DW_TAG_class_type:
21767 case DW_TAG_interface_type:
21768 case DW_TAG_structure_type:
21769 case DW_TAG_union_type:
21770 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21771 structures or unions. These were of the form "._%d" in GCC 4.1,
21772 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21773 and GCC 4.4. We work around this problem by ignoring these. */
21774 if (attr && DW_STRING (attr)
21775 && (startswith (DW_STRING (attr), "._")
21776 || startswith (DW_STRING (attr), "<anonymous")))
21777 return NULL;
21778
21779 /* GCC might emit a nameless typedef that has a linkage name. See
21780 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21781 if (!attr || DW_STRING (attr) == NULL)
21782 {
21783 attr = dw2_linkage_name_attr (die, cu);
21784 if (attr == NULL || DW_STRING (attr) == NULL)
21785 return NULL;
21786
21787 /* Avoid demangling DW_STRING (attr) the second time on a second
21788 call for the same DIE. */
21789 if (!DW_STRING_IS_CANONICAL (attr))
21790 {
21791 gdb::unique_xmalloc_ptr<char> demangled
21792 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21793 if (demangled == nullptr)
21794 return nullptr;
21795
21796 DW_STRING (attr) = objfile->intern (demangled.get ());
21797 DW_STRING_IS_CANONICAL (attr) = 1;
21798 }
21799
21800 /* Strip any leading namespaces/classes, keep only the base name.
21801 DW_AT_name for named DIEs does not contain the prefixes. */
21802 const char *base = strrchr (DW_STRING (attr), ':');
21803 if (base && base > DW_STRING (attr) && base[-1] == ':')
21804 return &base[1];
21805 else
21806 return DW_STRING (attr);
21807 }
21808 break;
21809
21810 default:
21811 break;
21812 }
21813
21814 if (!DW_STRING_IS_CANONICAL (attr))
21815 {
21816 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21817 objfile);
21818 DW_STRING_IS_CANONICAL (attr) = 1;
21819 }
21820 return DW_STRING (attr);
21821 }
21822
21823 /* Return the die that this die in an extension of, or NULL if there
21824 is none. *EXT_CU is the CU containing DIE on input, and the CU
21825 containing the return value on output. */
21826
21827 static struct die_info *
21828 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21829 {
21830 struct attribute *attr;
21831
21832 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21833 if (attr == NULL)
21834 return NULL;
21835
21836 return follow_die_ref (die, attr, ext_cu);
21837 }
21838
21839 /* A convenience function that returns an "unknown" DWARF name,
21840 including the value of V. STR is the name of the entity being
21841 printed, e.g., "TAG". */
21842
21843 static const char *
21844 dwarf_unknown (const char *str, unsigned v)
21845 {
21846 char *cell = get_print_cell ();
21847 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21848 return cell;
21849 }
21850
21851 /* Convert a DIE tag into its string name. */
21852
21853 static const char *
21854 dwarf_tag_name (unsigned tag)
21855 {
21856 const char *name = get_DW_TAG_name (tag);
21857
21858 if (name == NULL)
21859 return dwarf_unknown ("TAG", tag);
21860
21861 return name;
21862 }
21863
21864 /* Convert a DWARF attribute code into its string name. */
21865
21866 static const char *
21867 dwarf_attr_name (unsigned attr)
21868 {
21869 const char *name;
21870
21871 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21872 if (attr == DW_AT_MIPS_fde)
21873 return "DW_AT_MIPS_fde";
21874 #else
21875 if (attr == DW_AT_HP_block_index)
21876 return "DW_AT_HP_block_index";
21877 #endif
21878
21879 name = get_DW_AT_name (attr);
21880
21881 if (name == NULL)
21882 return dwarf_unknown ("AT", attr);
21883
21884 return name;
21885 }
21886
21887 /* Convert a DWARF value form code into its string name. */
21888
21889 static const char *
21890 dwarf_form_name (unsigned form)
21891 {
21892 const char *name = get_DW_FORM_name (form);
21893
21894 if (name == NULL)
21895 return dwarf_unknown ("FORM", form);
21896
21897 return name;
21898 }
21899
21900 static const char *
21901 dwarf_bool_name (unsigned mybool)
21902 {
21903 if (mybool)
21904 return "TRUE";
21905 else
21906 return "FALSE";
21907 }
21908
21909 /* Convert a DWARF type code into its string name. */
21910
21911 static const char *
21912 dwarf_type_encoding_name (unsigned enc)
21913 {
21914 const char *name = get_DW_ATE_name (enc);
21915
21916 if (name == NULL)
21917 return dwarf_unknown ("ATE", enc);
21918
21919 return name;
21920 }
21921
21922 static void
21923 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21924 {
21925 unsigned int i;
21926
21927 print_spaces (indent, f);
21928 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21929 dwarf_tag_name (die->tag), die->abbrev,
21930 sect_offset_str (die->sect_off));
21931
21932 if (die->parent != NULL)
21933 {
21934 print_spaces (indent, f);
21935 fprintf_unfiltered (f, " parent at offset: %s\n",
21936 sect_offset_str (die->parent->sect_off));
21937 }
21938
21939 print_spaces (indent, f);
21940 fprintf_unfiltered (f, " has children: %s\n",
21941 dwarf_bool_name (die->child != NULL));
21942
21943 print_spaces (indent, f);
21944 fprintf_unfiltered (f, " attributes:\n");
21945
21946 for (i = 0; i < die->num_attrs; ++i)
21947 {
21948 print_spaces (indent, f);
21949 fprintf_unfiltered (f, " %s (%s) ",
21950 dwarf_attr_name (die->attrs[i].name),
21951 dwarf_form_name (die->attrs[i].form));
21952
21953 switch (die->attrs[i].form)
21954 {
21955 case DW_FORM_addr:
21956 case DW_FORM_addrx:
21957 case DW_FORM_GNU_addr_index:
21958 fprintf_unfiltered (f, "address: ");
21959 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21960 break;
21961 case DW_FORM_block2:
21962 case DW_FORM_block4:
21963 case DW_FORM_block:
21964 case DW_FORM_block1:
21965 fprintf_unfiltered (f, "block: size %s",
21966 pulongest (DW_BLOCK (&die->attrs[i])->size));
21967 break;
21968 case DW_FORM_exprloc:
21969 fprintf_unfiltered (f, "expression: size %s",
21970 pulongest (DW_BLOCK (&die->attrs[i])->size));
21971 break;
21972 case DW_FORM_data16:
21973 fprintf_unfiltered (f, "constant of 16 bytes");
21974 break;
21975 case DW_FORM_ref_addr:
21976 fprintf_unfiltered (f, "ref address: ");
21977 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21978 break;
21979 case DW_FORM_GNU_ref_alt:
21980 fprintf_unfiltered (f, "alt ref address: ");
21981 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21982 break;
21983 case DW_FORM_ref1:
21984 case DW_FORM_ref2:
21985 case DW_FORM_ref4:
21986 case DW_FORM_ref8:
21987 case DW_FORM_ref_udata:
21988 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21989 (long) (DW_UNSND (&die->attrs[i])));
21990 break;
21991 case DW_FORM_data1:
21992 case DW_FORM_data2:
21993 case DW_FORM_data4:
21994 case DW_FORM_data8:
21995 case DW_FORM_udata:
21996 case DW_FORM_sdata:
21997 fprintf_unfiltered (f, "constant: %s",
21998 pulongest (DW_UNSND (&die->attrs[i])));
21999 break;
22000 case DW_FORM_sec_offset:
22001 fprintf_unfiltered (f, "section offset: %s",
22002 pulongest (DW_UNSND (&die->attrs[i])));
22003 break;
22004 case DW_FORM_ref_sig8:
22005 fprintf_unfiltered (f, "signature: %s",
22006 hex_string (DW_SIGNATURE (&die->attrs[i])));
22007 break;
22008 case DW_FORM_string:
22009 case DW_FORM_strp:
22010 case DW_FORM_line_strp:
22011 case DW_FORM_strx:
22012 case DW_FORM_GNU_str_index:
22013 case DW_FORM_GNU_strp_alt:
22014 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22015 DW_STRING (&die->attrs[i])
22016 ? DW_STRING (&die->attrs[i]) : "",
22017 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22018 break;
22019 case DW_FORM_flag:
22020 if (DW_UNSND (&die->attrs[i]))
22021 fprintf_unfiltered (f, "flag: TRUE");
22022 else
22023 fprintf_unfiltered (f, "flag: FALSE");
22024 break;
22025 case DW_FORM_flag_present:
22026 fprintf_unfiltered (f, "flag: TRUE");
22027 break;
22028 case DW_FORM_indirect:
22029 /* The reader will have reduced the indirect form to
22030 the "base form" so this form should not occur. */
22031 fprintf_unfiltered (f,
22032 "unexpected attribute form: DW_FORM_indirect");
22033 break;
22034 case DW_FORM_implicit_const:
22035 fprintf_unfiltered (f, "constant: %s",
22036 plongest (DW_SND (&die->attrs[i])));
22037 break;
22038 default:
22039 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22040 die->attrs[i].form);
22041 break;
22042 }
22043 fprintf_unfiltered (f, "\n");
22044 }
22045 }
22046
22047 static void
22048 dump_die_for_error (struct die_info *die)
22049 {
22050 dump_die_shallow (gdb_stderr, 0, die);
22051 }
22052
22053 static void
22054 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22055 {
22056 int indent = level * 4;
22057
22058 gdb_assert (die != NULL);
22059
22060 if (level >= max_level)
22061 return;
22062
22063 dump_die_shallow (f, indent, die);
22064
22065 if (die->child != NULL)
22066 {
22067 print_spaces (indent, f);
22068 fprintf_unfiltered (f, " Children:");
22069 if (level + 1 < max_level)
22070 {
22071 fprintf_unfiltered (f, "\n");
22072 dump_die_1 (f, level + 1, max_level, die->child);
22073 }
22074 else
22075 {
22076 fprintf_unfiltered (f,
22077 " [not printed, max nesting level reached]\n");
22078 }
22079 }
22080
22081 if (die->sibling != NULL && level > 0)
22082 {
22083 dump_die_1 (f, level, max_level, die->sibling);
22084 }
22085 }
22086
22087 /* This is called from the pdie macro in gdbinit.in.
22088 It's not static so gcc will keep a copy callable from gdb. */
22089
22090 void
22091 dump_die (struct die_info *die, int max_level)
22092 {
22093 dump_die_1 (gdb_stdlog, 0, max_level, die);
22094 }
22095
22096 static void
22097 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22098 {
22099 void **slot;
22100
22101 slot = htab_find_slot_with_hash (cu->die_hash, die,
22102 to_underlying (die->sect_off),
22103 INSERT);
22104
22105 *slot = die;
22106 }
22107
22108 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22109 required kind. */
22110
22111 static sect_offset
22112 dwarf2_get_ref_die_offset (const struct attribute *attr)
22113 {
22114 if (attr->form_is_ref ())
22115 return (sect_offset) DW_UNSND (attr);
22116
22117 complaint (_("unsupported die ref attribute form: '%s'"),
22118 dwarf_form_name (attr->form));
22119 return {};
22120 }
22121
22122 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22123 * the value held by the attribute is not constant. */
22124
22125 static LONGEST
22126 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22127 {
22128 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22129 return DW_SND (attr);
22130 else if (attr->form == DW_FORM_udata
22131 || attr->form == DW_FORM_data1
22132 || attr->form == DW_FORM_data2
22133 || attr->form == DW_FORM_data4
22134 || attr->form == DW_FORM_data8)
22135 return DW_UNSND (attr);
22136 else
22137 {
22138 /* For DW_FORM_data16 see attribute::form_is_constant. */
22139 complaint (_("Attribute value is not a constant (%s)"),
22140 dwarf_form_name (attr->form));
22141 return default_value;
22142 }
22143 }
22144
22145 /* Follow reference or signature attribute ATTR of SRC_DIE.
22146 On entry *REF_CU is the CU of SRC_DIE.
22147 On exit *REF_CU is the CU of the result. */
22148
22149 static struct die_info *
22150 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22151 struct dwarf2_cu **ref_cu)
22152 {
22153 struct die_info *die;
22154
22155 if (attr->form_is_ref ())
22156 die = follow_die_ref (src_die, attr, ref_cu);
22157 else if (attr->form == DW_FORM_ref_sig8)
22158 die = follow_die_sig (src_die, attr, ref_cu);
22159 else
22160 {
22161 dump_die_for_error (src_die);
22162 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22163 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22164 }
22165
22166 return die;
22167 }
22168
22169 /* Follow reference OFFSET.
22170 On entry *REF_CU is the CU of the source die referencing OFFSET.
22171 On exit *REF_CU is the CU of the result.
22172 Returns NULL if OFFSET is invalid. */
22173
22174 static struct die_info *
22175 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22176 struct dwarf2_cu **ref_cu)
22177 {
22178 struct die_info temp_die;
22179 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22180 struct dwarf2_per_objfile *dwarf2_per_objfile
22181 = cu->per_cu->dwarf2_per_objfile;
22182
22183 gdb_assert (cu->per_cu != NULL);
22184
22185 target_cu = cu;
22186
22187 if (cu->per_cu->is_debug_types)
22188 {
22189 /* .debug_types CUs cannot reference anything outside their CU.
22190 If they need to, they have to reference a signatured type via
22191 DW_FORM_ref_sig8. */
22192 if (!cu->header.offset_in_cu_p (sect_off))
22193 return NULL;
22194 }
22195 else if (offset_in_dwz != cu->per_cu->is_dwz
22196 || !cu->header.offset_in_cu_p (sect_off))
22197 {
22198 struct dwarf2_per_cu_data *per_cu;
22199
22200 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22201 dwarf2_per_objfile);
22202
22203 /* If necessary, add it to the queue and load its DIEs. */
22204 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22205 load_full_comp_unit (per_cu, false, cu->language);
22206
22207 target_cu = per_cu->cu;
22208 }
22209 else if (cu->dies == NULL)
22210 {
22211 /* We're loading full DIEs during partial symbol reading. */
22212 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22213 load_full_comp_unit (cu->per_cu, false, language_minimal);
22214 }
22215
22216 *ref_cu = target_cu;
22217 temp_die.sect_off = sect_off;
22218
22219 if (target_cu != cu)
22220 target_cu->ancestor = cu;
22221
22222 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22223 &temp_die,
22224 to_underlying (sect_off));
22225 }
22226
22227 /* Follow reference attribute ATTR of SRC_DIE.
22228 On entry *REF_CU is the CU of SRC_DIE.
22229 On exit *REF_CU is the CU of the result. */
22230
22231 static struct die_info *
22232 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22233 struct dwarf2_cu **ref_cu)
22234 {
22235 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22236 struct dwarf2_cu *cu = *ref_cu;
22237 struct die_info *die;
22238
22239 die = follow_die_offset (sect_off,
22240 (attr->form == DW_FORM_GNU_ref_alt
22241 || cu->per_cu->is_dwz),
22242 ref_cu);
22243 if (!die)
22244 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22245 "at %s [in module %s]"),
22246 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22247 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22248
22249 return die;
22250 }
22251
22252 /* See read.h. */
22253
22254 struct dwarf2_locexpr_baton
22255 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22256 dwarf2_per_cu_data *per_cu,
22257 CORE_ADDR (*get_frame_pc) (void *baton),
22258 void *baton, bool resolve_abstract_p)
22259 {
22260 struct dwarf2_cu *cu;
22261 struct die_info *die;
22262 struct attribute *attr;
22263 struct dwarf2_locexpr_baton retval;
22264 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22265 struct objfile *objfile = dwarf2_per_objfile->objfile;
22266
22267 if (per_cu->cu == NULL)
22268 load_cu (per_cu, false);
22269 cu = per_cu->cu;
22270 if (cu == NULL)
22271 {
22272 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22273 Instead just throw an error, not much else we can do. */
22274 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22275 sect_offset_str (sect_off), objfile_name (objfile));
22276 }
22277
22278 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22279 if (!die)
22280 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22281 sect_offset_str (sect_off), objfile_name (objfile));
22282
22283 attr = dwarf2_attr (die, DW_AT_location, cu);
22284 if (!attr && resolve_abstract_p
22285 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22286 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22287 {
22288 CORE_ADDR pc = (*get_frame_pc) (baton);
22289 CORE_ADDR baseaddr = objfile->text_section_offset ();
22290 struct gdbarch *gdbarch = get_objfile_arch (objfile);
22291
22292 for (const auto &cand_off
22293 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22294 {
22295 struct dwarf2_cu *cand_cu = cu;
22296 struct die_info *cand
22297 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22298 if (!cand
22299 || !cand->parent
22300 || cand->parent->tag != DW_TAG_subprogram)
22301 continue;
22302
22303 CORE_ADDR pc_low, pc_high;
22304 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22305 if (pc_low == ((CORE_ADDR) -1))
22306 continue;
22307 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22308 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22309 if (!(pc_low <= pc && pc < pc_high))
22310 continue;
22311
22312 die = cand;
22313 attr = dwarf2_attr (die, DW_AT_location, cu);
22314 break;
22315 }
22316 }
22317
22318 if (!attr)
22319 {
22320 /* DWARF: "If there is no such attribute, then there is no effect.".
22321 DATA is ignored if SIZE is 0. */
22322
22323 retval.data = NULL;
22324 retval.size = 0;
22325 }
22326 else if (attr->form_is_section_offset ())
22327 {
22328 struct dwarf2_loclist_baton loclist_baton;
22329 CORE_ADDR pc = (*get_frame_pc) (baton);
22330 size_t size;
22331
22332 fill_in_loclist_baton (cu, &loclist_baton, attr);
22333
22334 retval.data = dwarf2_find_location_expression (&loclist_baton,
22335 &size, pc);
22336 retval.size = size;
22337 }
22338 else
22339 {
22340 if (!attr->form_is_block ())
22341 error (_("Dwarf Error: DIE at %s referenced in module %s "
22342 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22343 sect_offset_str (sect_off), objfile_name (objfile));
22344
22345 retval.data = DW_BLOCK (attr)->data;
22346 retval.size = DW_BLOCK (attr)->size;
22347 }
22348 retval.per_cu = cu->per_cu;
22349
22350 age_cached_comp_units (dwarf2_per_objfile);
22351
22352 return retval;
22353 }
22354
22355 /* See read.h. */
22356
22357 struct dwarf2_locexpr_baton
22358 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22359 dwarf2_per_cu_data *per_cu,
22360 CORE_ADDR (*get_frame_pc) (void *baton),
22361 void *baton)
22362 {
22363 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22364
22365 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22366 }
22367
22368 /* Write a constant of a given type as target-ordered bytes into
22369 OBSTACK. */
22370
22371 static const gdb_byte *
22372 write_constant_as_bytes (struct obstack *obstack,
22373 enum bfd_endian byte_order,
22374 struct type *type,
22375 ULONGEST value,
22376 LONGEST *len)
22377 {
22378 gdb_byte *result;
22379
22380 *len = TYPE_LENGTH (type);
22381 result = (gdb_byte *) obstack_alloc (obstack, *len);
22382 store_unsigned_integer (result, *len, byte_order, value);
22383
22384 return result;
22385 }
22386
22387 /* See read.h. */
22388
22389 const gdb_byte *
22390 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22391 dwarf2_per_cu_data *per_cu,
22392 obstack *obstack,
22393 LONGEST *len)
22394 {
22395 struct dwarf2_cu *cu;
22396 struct die_info *die;
22397 struct attribute *attr;
22398 const gdb_byte *result = NULL;
22399 struct type *type;
22400 LONGEST value;
22401 enum bfd_endian byte_order;
22402 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22403
22404 if (per_cu->cu == NULL)
22405 load_cu (per_cu, false);
22406 cu = per_cu->cu;
22407 if (cu == NULL)
22408 {
22409 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22410 Instead just throw an error, not much else we can do. */
22411 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22412 sect_offset_str (sect_off), objfile_name (objfile));
22413 }
22414
22415 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22416 if (!die)
22417 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22418 sect_offset_str (sect_off), objfile_name (objfile));
22419
22420 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22421 if (attr == NULL)
22422 return NULL;
22423
22424 byte_order = (bfd_big_endian (objfile->obfd)
22425 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22426
22427 switch (attr->form)
22428 {
22429 case DW_FORM_addr:
22430 case DW_FORM_addrx:
22431 case DW_FORM_GNU_addr_index:
22432 {
22433 gdb_byte *tem;
22434
22435 *len = cu->header.addr_size;
22436 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22437 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22438 result = tem;
22439 }
22440 break;
22441 case DW_FORM_string:
22442 case DW_FORM_strp:
22443 case DW_FORM_strx:
22444 case DW_FORM_GNU_str_index:
22445 case DW_FORM_GNU_strp_alt:
22446 /* DW_STRING is already allocated on the objfile obstack, point
22447 directly to it. */
22448 result = (const gdb_byte *) DW_STRING (attr);
22449 *len = strlen (DW_STRING (attr));
22450 break;
22451 case DW_FORM_block1:
22452 case DW_FORM_block2:
22453 case DW_FORM_block4:
22454 case DW_FORM_block:
22455 case DW_FORM_exprloc:
22456 case DW_FORM_data16:
22457 result = DW_BLOCK (attr)->data;
22458 *len = DW_BLOCK (attr)->size;
22459 break;
22460
22461 /* The DW_AT_const_value attributes are supposed to carry the
22462 symbol's value "represented as it would be on the target
22463 architecture." By the time we get here, it's already been
22464 converted to host endianness, so we just need to sign- or
22465 zero-extend it as appropriate. */
22466 case DW_FORM_data1:
22467 type = die_type (die, cu);
22468 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22469 if (result == NULL)
22470 result = write_constant_as_bytes (obstack, byte_order,
22471 type, value, len);
22472 break;
22473 case DW_FORM_data2:
22474 type = die_type (die, cu);
22475 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22476 if (result == NULL)
22477 result = write_constant_as_bytes (obstack, byte_order,
22478 type, value, len);
22479 break;
22480 case DW_FORM_data4:
22481 type = die_type (die, cu);
22482 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22483 if (result == NULL)
22484 result = write_constant_as_bytes (obstack, byte_order,
22485 type, value, len);
22486 break;
22487 case DW_FORM_data8:
22488 type = die_type (die, cu);
22489 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22490 if (result == NULL)
22491 result = write_constant_as_bytes (obstack, byte_order,
22492 type, value, len);
22493 break;
22494
22495 case DW_FORM_sdata:
22496 case DW_FORM_implicit_const:
22497 type = die_type (die, cu);
22498 result = write_constant_as_bytes (obstack, byte_order,
22499 type, DW_SND (attr), len);
22500 break;
22501
22502 case DW_FORM_udata:
22503 type = die_type (die, cu);
22504 result = write_constant_as_bytes (obstack, byte_order,
22505 type, DW_UNSND (attr), len);
22506 break;
22507
22508 default:
22509 complaint (_("unsupported const value attribute form: '%s'"),
22510 dwarf_form_name (attr->form));
22511 break;
22512 }
22513
22514 return result;
22515 }
22516
22517 /* See read.h. */
22518
22519 struct type *
22520 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22521 dwarf2_per_cu_data *per_cu)
22522 {
22523 struct dwarf2_cu *cu;
22524 struct die_info *die;
22525
22526 if (per_cu->cu == NULL)
22527 load_cu (per_cu, false);
22528 cu = per_cu->cu;
22529 if (!cu)
22530 return NULL;
22531
22532 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22533 if (!die)
22534 return NULL;
22535
22536 return die_type (die, cu);
22537 }
22538
22539 /* See read.h. */
22540
22541 struct type *
22542 dwarf2_get_die_type (cu_offset die_offset,
22543 struct dwarf2_per_cu_data *per_cu)
22544 {
22545 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22546 return get_die_type_at_offset (die_offset_sect, per_cu);
22547 }
22548
22549 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22550 On entry *REF_CU is the CU of SRC_DIE.
22551 On exit *REF_CU is the CU of the result.
22552 Returns NULL if the referenced DIE isn't found. */
22553
22554 static struct die_info *
22555 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22556 struct dwarf2_cu **ref_cu)
22557 {
22558 struct die_info temp_die;
22559 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22560 struct die_info *die;
22561
22562 /* While it might be nice to assert sig_type->type == NULL here,
22563 we can get here for DW_AT_imported_declaration where we need
22564 the DIE not the type. */
22565
22566 /* If necessary, add it to the queue and load its DIEs. */
22567
22568 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22569 read_signatured_type (sig_type);
22570
22571 sig_cu = sig_type->per_cu.cu;
22572 gdb_assert (sig_cu != NULL);
22573 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22574 temp_die.sect_off = sig_type->type_offset_in_section;
22575 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22576 to_underlying (temp_die.sect_off));
22577 if (die)
22578 {
22579 struct dwarf2_per_objfile *dwarf2_per_objfile
22580 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22581
22582 /* For .gdb_index version 7 keep track of included TUs.
22583 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22584 if (dwarf2_per_objfile->index_table != NULL
22585 && dwarf2_per_objfile->index_table->version <= 7)
22586 {
22587 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22588 }
22589
22590 *ref_cu = sig_cu;
22591 if (sig_cu != cu)
22592 sig_cu->ancestor = cu;
22593
22594 return die;
22595 }
22596
22597 return NULL;
22598 }
22599
22600 /* Follow signatured type referenced by ATTR in SRC_DIE.
22601 On entry *REF_CU is the CU of SRC_DIE.
22602 On exit *REF_CU is the CU of the result.
22603 The result is the DIE of the type.
22604 If the referenced type cannot be found an error is thrown. */
22605
22606 static struct die_info *
22607 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22608 struct dwarf2_cu **ref_cu)
22609 {
22610 ULONGEST signature = DW_SIGNATURE (attr);
22611 struct signatured_type *sig_type;
22612 struct die_info *die;
22613
22614 gdb_assert (attr->form == DW_FORM_ref_sig8);
22615
22616 sig_type = lookup_signatured_type (*ref_cu, signature);
22617 /* sig_type will be NULL if the signatured type is missing from
22618 the debug info. */
22619 if (sig_type == NULL)
22620 {
22621 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22622 " from DIE at %s [in module %s]"),
22623 hex_string (signature), sect_offset_str (src_die->sect_off),
22624 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22625 }
22626
22627 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22628 if (die == NULL)
22629 {
22630 dump_die_for_error (src_die);
22631 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22632 " from DIE at %s [in module %s]"),
22633 hex_string (signature), sect_offset_str (src_die->sect_off),
22634 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22635 }
22636
22637 return die;
22638 }
22639
22640 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22641 reading in and processing the type unit if necessary. */
22642
22643 static struct type *
22644 get_signatured_type (struct die_info *die, ULONGEST signature,
22645 struct dwarf2_cu *cu)
22646 {
22647 struct dwarf2_per_objfile *dwarf2_per_objfile
22648 = cu->per_cu->dwarf2_per_objfile;
22649 struct signatured_type *sig_type;
22650 struct dwarf2_cu *type_cu;
22651 struct die_info *type_die;
22652 struct type *type;
22653
22654 sig_type = lookup_signatured_type (cu, signature);
22655 /* sig_type will be NULL if the signatured type is missing from
22656 the debug info. */
22657 if (sig_type == NULL)
22658 {
22659 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22660 " from DIE at %s [in module %s]"),
22661 hex_string (signature), sect_offset_str (die->sect_off),
22662 objfile_name (dwarf2_per_objfile->objfile));
22663 return build_error_marker_type (cu, die);
22664 }
22665
22666 /* If we already know the type we're done. */
22667 if (sig_type->type != NULL)
22668 return sig_type->type;
22669
22670 type_cu = cu;
22671 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22672 if (type_die != NULL)
22673 {
22674 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22675 is created. This is important, for example, because for c++ classes
22676 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22677 type = read_type_die (type_die, type_cu);
22678 if (type == NULL)
22679 {
22680 complaint (_("Dwarf Error: Cannot build signatured type %s"
22681 " referenced from DIE at %s [in module %s]"),
22682 hex_string (signature), sect_offset_str (die->sect_off),
22683 objfile_name (dwarf2_per_objfile->objfile));
22684 type = build_error_marker_type (cu, die);
22685 }
22686 }
22687 else
22688 {
22689 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22690 " from DIE at %s [in module %s]"),
22691 hex_string (signature), sect_offset_str (die->sect_off),
22692 objfile_name (dwarf2_per_objfile->objfile));
22693 type = build_error_marker_type (cu, die);
22694 }
22695 sig_type->type = type;
22696
22697 return type;
22698 }
22699
22700 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22701 reading in and processing the type unit if necessary. */
22702
22703 static struct type *
22704 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22705 struct dwarf2_cu *cu) /* ARI: editCase function */
22706 {
22707 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22708 if (attr->form_is_ref ())
22709 {
22710 struct dwarf2_cu *type_cu = cu;
22711 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22712
22713 return read_type_die (type_die, type_cu);
22714 }
22715 else if (attr->form == DW_FORM_ref_sig8)
22716 {
22717 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22718 }
22719 else
22720 {
22721 struct dwarf2_per_objfile *dwarf2_per_objfile
22722 = cu->per_cu->dwarf2_per_objfile;
22723
22724 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22725 " at %s [in module %s]"),
22726 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22727 objfile_name (dwarf2_per_objfile->objfile));
22728 return build_error_marker_type (cu, die);
22729 }
22730 }
22731
22732 /* Load the DIEs associated with type unit PER_CU into memory. */
22733
22734 static void
22735 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22736 {
22737 struct signatured_type *sig_type;
22738
22739 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22740 gdb_assert (! per_cu->type_unit_group_p ());
22741
22742 /* We have the per_cu, but we need the signatured_type.
22743 Fortunately this is an easy translation. */
22744 gdb_assert (per_cu->is_debug_types);
22745 sig_type = (struct signatured_type *) per_cu;
22746
22747 gdb_assert (per_cu->cu == NULL);
22748
22749 read_signatured_type (sig_type);
22750
22751 gdb_assert (per_cu->cu != NULL);
22752 }
22753
22754 /* Read in a signatured type and build its CU and DIEs.
22755 If the type is a stub for the real type in a DWO file,
22756 read in the real type from the DWO file as well. */
22757
22758 static void
22759 read_signatured_type (struct signatured_type *sig_type)
22760 {
22761 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22762
22763 gdb_assert (per_cu->is_debug_types);
22764 gdb_assert (per_cu->cu == NULL);
22765
22766 cutu_reader reader (per_cu, NULL, 0, false);
22767
22768 if (!reader.dummy_p)
22769 {
22770 struct dwarf2_cu *cu = reader.cu;
22771 const gdb_byte *info_ptr = reader.info_ptr;
22772
22773 gdb_assert (cu->die_hash == NULL);
22774 cu->die_hash =
22775 htab_create_alloc_ex (cu->header.length / 12,
22776 die_hash,
22777 die_eq,
22778 NULL,
22779 &cu->comp_unit_obstack,
22780 hashtab_obstack_allocate,
22781 dummy_obstack_deallocate);
22782
22783 if (reader.comp_unit_die->has_children)
22784 reader.comp_unit_die->child
22785 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22786 reader.comp_unit_die);
22787 cu->dies = reader.comp_unit_die;
22788 /* comp_unit_die is not stored in die_hash, no need. */
22789
22790 /* We try not to read any attributes in this function, because
22791 not all CUs needed for references have been loaded yet, and
22792 symbol table processing isn't initialized. But we have to
22793 set the CU language, or we won't be able to build types
22794 correctly. Similarly, if we do not read the producer, we can
22795 not apply producer-specific interpretation. */
22796 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22797
22798 reader.keep ();
22799 }
22800
22801 sig_type->per_cu.tu_read = 1;
22802 }
22803
22804 /* Decode simple location descriptions.
22805 Given a pointer to a dwarf block that defines a location, compute
22806 the location and return the value.
22807
22808 NOTE drow/2003-11-18: This function is called in two situations
22809 now: for the address of static or global variables (partial symbols
22810 only) and for offsets into structures which are expected to be
22811 (more or less) constant. The partial symbol case should go away,
22812 and only the constant case should remain. That will let this
22813 function complain more accurately. A few special modes are allowed
22814 without complaint for global variables (for instance, global
22815 register values and thread-local values).
22816
22817 A location description containing no operations indicates that the
22818 object is optimized out. The return value is 0 for that case.
22819 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22820 callers will only want a very basic result and this can become a
22821 complaint.
22822
22823 Note that stack[0] is unused except as a default error return. */
22824
22825 static CORE_ADDR
22826 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22827 {
22828 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22829 size_t i;
22830 size_t size = blk->size;
22831 const gdb_byte *data = blk->data;
22832 CORE_ADDR stack[64];
22833 int stacki;
22834 unsigned int bytes_read, unsnd;
22835 gdb_byte op;
22836
22837 i = 0;
22838 stacki = 0;
22839 stack[stacki] = 0;
22840 stack[++stacki] = 0;
22841
22842 while (i < size)
22843 {
22844 op = data[i++];
22845 switch (op)
22846 {
22847 case DW_OP_lit0:
22848 case DW_OP_lit1:
22849 case DW_OP_lit2:
22850 case DW_OP_lit3:
22851 case DW_OP_lit4:
22852 case DW_OP_lit5:
22853 case DW_OP_lit6:
22854 case DW_OP_lit7:
22855 case DW_OP_lit8:
22856 case DW_OP_lit9:
22857 case DW_OP_lit10:
22858 case DW_OP_lit11:
22859 case DW_OP_lit12:
22860 case DW_OP_lit13:
22861 case DW_OP_lit14:
22862 case DW_OP_lit15:
22863 case DW_OP_lit16:
22864 case DW_OP_lit17:
22865 case DW_OP_lit18:
22866 case DW_OP_lit19:
22867 case DW_OP_lit20:
22868 case DW_OP_lit21:
22869 case DW_OP_lit22:
22870 case DW_OP_lit23:
22871 case DW_OP_lit24:
22872 case DW_OP_lit25:
22873 case DW_OP_lit26:
22874 case DW_OP_lit27:
22875 case DW_OP_lit28:
22876 case DW_OP_lit29:
22877 case DW_OP_lit30:
22878 case DW_OP_lit31:
22879 stack[++stacki] = op - DW_OP_lit0;
22880 break;
22881
22882 case DW_OP_reg0:
22883 case DW_OP_reg1:
22884 case DW_OP_reg2:
22885 case DW_OP_reg3:
22886 case DW_OP_reg4:
22887 case DW_OP_reg5:
22888 case DW_OP_reg6:
22889 case DW_OP_reg7:
22890 case DW_OP_reg8:
22891 case DW_OP_reg9:
22892 case DW_OP_reg10:
22893 case DW_OP_reg11:
22894 case DW_OP_reg12:
22895 case DW_OP_reg13:
22896 case DW_OP_reg14:
22897 case DW_OP_reg15:
22898 case DW_OP_reg16:
22899 case DW_OP_reg17:
22900 case DW_OP_reg18:
22901 case DW_OP_reg19:
22902 case DW_OP_reg20:
22903 case DW_OP_reg21:
22904 case DW_OP_reg22:
22905 case DW_OP_reg23:
22906 case DW_OP_reg24:
22907 case DW_OP_reg25:
22908 case DW_OP_reg26:
22909 case DW_OP_reg27:
22910 case DW_OP_reg28:
22911 case DW_OP_reg29:
22912 case DW_OP_reg30:
22913 case DW_OP_reg31:
22914 stack[++stacki] = op - DW_OP_reg0;
22915 if (i < size)
22916 dwarf2_complex_location_expr_complaint ();
22917 break;
22918
22919 case DW_OP_regx:
22920 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22921 i += bytes_read;
22922 stack[++stacki] = unsnd;
22923 if (i < size)
22924 dwarf2_complex_location_expr_complaint ();
22925 break;
22926
22927 case DW_OP_addr:
22928 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22929 &bytes_read);
22930 i += bytes_read;
22931 break;
22932
22933 case DW_OP_const1u:
22934 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22935 i += 1;
22936 break;
22937
22938 case DW_OP_const1s:
22939 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22940 i += 1;
22941 break;
22942
22943 case DW_OP_const2u:
22944 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22945 i += 2;
22946 break;
22947
22948 case DW_OP_const2s:
22949 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22950 i += 2;
22951 break;
22952
22953 case DW_OP_const4u:
22954 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22955 i += 4;
22956 break;
22957
22958 case DW_OP_const4s:
22959 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22960 i += 4;
22961 break;
22962
22963 case DW_OP_const8u:
22964 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22965 i += 8;
22966 break;
22967
22968 case DW_OP_constu:
22969 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22970 &bytes_read);
22971 i += bytes_read;
22972 break;
22973
22974 case DW_OP_consts:
22975 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22976 i += bytes_read;
22977 break;
22978
22979 case DW_OP_dup:
22980 stack[stacki + 1] = stack[stacki];
22981 stacki++;
22982 break;
22983
22984 case DW_OP_plus:
22985 stack[stacki - 1] += stack[stacki];
22986 stacki--;
22987 break;
22988
22989 case DW_OP_plus_uconst:
22990 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22991 &bytes_read);
22992 i += bytes_read;
22993 break;
22994
22995 case DW_OP_minus:
22996 stack[stacki - 1] -= stack[stacki];
22997 stacki--;
22998 break;
22999
23000 case DW_OP_deref:
23001 /* If we're not the last op, then we definitely can't encode
23002 this using GDB's address_class enum. This is valid for partial
23003 global symbols, although the variable's address will be bogus
23004 in the psymtab. */
23005 if (i < size)
23006 dwarf2_complex_location_expr_complaint ();
23007 break;
23008
23009 case DW_OP_GNU_push_tls_address:
23010 case DW_OP_form_tls_address:
23011 /* The top of the stack has the offset from the beginning
23012 of the thread control block at which the variable is located. */
23013 /* Nothing should follow this operator, so the top of stack would
23014 be returned. */
23015 /* This is valid for partial global symbols, but the variable's
23016 address will be bogus in the psymtab. Make it always at least
23017 non-zero to not look as a variable garbage collected by linker
23018 which have DW_OP_addr 0. */
23019 if (i < size)
23020 dwarf2_complex_location_expr_complaint ();
23021 stack[stacki]++;
23022 break;
23023
23024 case DW_OP_GNU_uninit:
23025 break;
23026
23027 case DW_OP_addrx:
23028 case DW_OP_GNU_addr_index:
23029 case DW_OP_GNU_const_index:
23030 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23031 &bytes_read);
23032 i += bytes_read;
23033 break;
23034
23035 default:
23036 {
23037 const char *name = get_DW_OP_name (op);
23038
23039 if (name)
23040 complaint (_("unsupported stack op: '%s'"),
23041 name);
23042 else
23043 complaint (_("unsupported stack op: '%02x'"),
23044 op);
23045 }
23046
23047 return (stack[stacki]);
23048 }
23049
23050 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23051 outside of the allocated space. Also enforce minimum>0. */
23052 if (stacki >= ARRAY_SIZE (stack) - 1)
23053 {
23054 complaint (_("location description stack overflow"));
23055 return 0;
23056 }
23057
23058 if (stacki <= 0)
23059 {
23060 complaint (_("location description stack underflow"));
23061 return 0;
23062 }
23063 }
23064 return (stack[stacki]);
23065 }
23066
23067 /* memory allocation interface */
23068
23069 static struct dwarf_block *
23070 dwarf_alloc_block (struct dwarf2_cu *cu)
23071 {
23072 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23073 }
23074
23075 static struct die_info *
23076 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23077 {
23078 struct die_info *die;
23079 size_t size = sizeof (struct die_info);
23080
23081 if (num_attrs > 1)
23082 size += (num_attrs - 1) * sizeof (struct attribute);
23083
23084 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23085 memset (die, 0, sizeof (struct die_info));
23086 return (die);
23087 }
23088
23089 \f
23090 /* Macro support. */
23091
23092 static struct macro_source_file *
23093 macro_start_file (struct dwarf2_cu *cu,
23094 int file, int line,
23095 struct macro_source_file *current_file,
23096 struct line_header *lh)
23097 {
23098 /* File name relative to the compilation directory of this source file. */
23099 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
23100
23101 if (! current_file)
23102 {
23103 /* Note: We don't create a macro table for this compilation unit
23104 at all until we actually get a filename. */
23105 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
23106
23107 /* If we have no current file, then this must be the start_file
23108 directive for the compilation unit's main source file. */
23109 current_file = macro_set_main (macro_table, file_name.get ());
23110 macro_define_special (macro_table);
23111 }
23112 else
23113 current_file = macro_include (current_file, line, file_name.get ());
23114
23115 return current_file;
23116 }
23117
23118 static const char *
23119 consume_improper_spaces (const char *p, const char *body)
23120 {
23121 if (*p == ' ')
23122 {
23123 complaint (_("macro definition contains spaces "
23124 "in formal argument list:\n`%s'"),
23125 body);
23126
23127 while (*p == ' ')
23128 p++;
23129 }
23130
23131 return p;
23132 }
23133
23134
23135 static void
23136 parse_macro_definition (struct macro_source_file *file, int line,
23137 const char *body)
23138 {
23139 const char *p;
23140
23141 /* The body string takes one of two forms. For object-like macro
23142 definitions, it should be:
23143
23144 <macro name> " " <definition>
23145
23146 For function-like macro definitions, it should be:
23147
23148 <macro name> "() " <definition>
23149 or
23150 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23151
23152 Spaces may appear only where explicitly indicated, and in the
23153 <definition>.
23154
23155 The Dwarf 2 spec says that an object-like macro's name is always
23156 followed by a space, but versions of GCC around March 2002 omit
23157 the space when the macro's definition is the empty string.
23158
23159 The Dwarf 2 spec says that there should be no spaces between the
23160 formal arguments in a function-like macro's formal argument list,
23161 but versions of GCC around March 2002 include spaces after the
23162 commas. */
23163
23164
23165 /* Find the extent of the macro name. The macro name is terminated
23166 by either a space or null character (for an object-like macro) or
23167 an opening paren (for a function-like macro). */
23168 for (p = body; *p; p++)
23169 if (*p == ' ' || *p == '(')
23170 break;
23171
23172 if (*p == ' ' || *p == '\0')
23173 {
23174 /* It's an object-like macro. */
23175 int name_len = p - body;
23176 std::string name (body, name_len);
23177 const char *replacement;
23178
23179 if (*p == ' ')
23180 replacement = body + name_len + 1;
23181 else
23182 {
23183 dwarf2_macro_malformed_definition_complaint (body);
23184 replacement = body + name_len;
23185 }
23186
23187 macro_define_object (file, line, name.c_str (), replacement);
23188 }
23189 else if (*p == '(')
23190 {
23191 /* It's a function-like macro. */
23192 std::string name (body, p - body);
23193 int argc = 0;
23194 int argv_size = 1;
23195 char **argv = XNEWVEC (char *, argv_size);
23196
23197 p++;
23198
23199 p = consume_improper_spaces (p, body);
23200
23201 /* Parse the formal argument list. */
23202 while (*p && *p != ')')
23203 {
23204 /* Find the extent of the current argument name. */
23205 const char *arg_start = p;
23206
23207 while (*p && *p != ',' && *p != ')' && *p != ' ')
23208 p++;
23209
23210 if (! *p || p == arg_start)
23211 dwarf2_macro_malformed_definition_complaint (body);
23212 else
23213 {
23214 /* Make sure argv has room for the new argument. */
23215 if (argc >= argv_size)
23216 {
23217 argv_size *= 2;
23218 argv = XRESIZEVEC (char *, argv, argv_size);
23219 }
23220
23221 argv[argc++] = savestring (arg_start, p - arg_start);
23222 }
23223
23224 p = consume_improper_spaces (p, body);
23225
23226 /* Consume the comma, if present. */
23227 if (*p == ',')
23228 {
23229 p++;
23230
23231 p = consume_improper_spaces (p, body);
23232 }
23233 }
23234
23235 if (*p == ')')
23236 {
23237 p++;
23238
23239 if (*p == ' ')
23240 /* Perfectly formed definition, no complaints. */
23241 macro_define_function (file, line, name.c_str (),
23242 argc, (const char **) argv,
23243 p + 1);
23244 else if (*p == '\0')
23245 {
23246 /* Complain, but do define it. */
23247 dwarf2_macro_malformed_definition_complaint (body);
23248 macro_define_function (file, line, name.c_str (),
23249 argc, (const char **) argv,
23250 p);
23251 }
23252 else
23253 /* Just complain. */
23254 dwarf2_macro_malformed_definition_complaint (body);
23255 }
23256 else
23257 /* Just complain. */
23258 dwarf2_macro_malformed_definition_complaint (body);
23259
23260 {
23261 int i;
23262
23263 for (i = 0; i < argc; i++)
23264 xfree (argv[i]);
23265 }
23266 xfree (argv);
23267 }
23268 else
23269 dwarf2_macro_malformed_definition_complaint (body);
23270 }
23271
23272 /* Skip some bytes from BYTES according to the form given in FORM.
23273 Returns the new pointer. */
23274
23275 static const gdb_byte *
23276 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23277 enum dwarf_form form,
23278 unsigned int offset_size,
23279 struct dwarf2_section_info *section)
23280 {
23281 unsigned int bytes_read;
23282
23283 switch (form)
23284 {
23285 case DW_FORM_data1:
23286 case DW_FORM_flag:
23287 ++bytes;
23288 break;
23289
23290 case DW_FORM_data2:
23291 bytes += 2;
23292 break;
23293
23294 case DW_FORM_data4:
23295 bytes += 4;
23296 break;
23297
23298 case DW_FORM_data8:
23299 bytes += 8;
23300 break;
23301
23302 case DW_FORM_data16:
23303 bytes += 16;
23304 break;
23305
23306 case DW_FORM_string:
23307 read_direct_string (abfd, bytes, &bytes_read);
23308 bytes += bytes_read;
23309 break;
23310
23311 case DW_FORM_sec_offset:
23312 case DW_FORM_strp:
23313 case DW_FORM_GNU_strp_alt:
23314 bytes += offset_size;
23315 break;
23316
23317 case DW_FORM_block:
23318 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23319 bytes += bytes_read;
23320 break;
23321
23322 case DW_FORM_block1:
23323 bytes += 1 + read_1_byte (abfd, bytes);
23324 break;
23325 case DW_FORM_block2:
23326 bytes += 2 + read_2_bytes (abfd, bytes);
23327 break;
23328 case DW_FORM_block4:
23329 bytes += 4 + read_4_bytes (abfd, bytes);
23330 break;
23331
23332 case DW_FORM_addrx:
23333 case DW_FORM_sdata:
23334 case DW_FORM_strx:
23335 case DW_FORM_udata:
23336 case DW_FORM_GNU_addr_index:
23337 case DW_FORM_GNU_str_index:
23338 bytes = gdb_skip_leb128 (bytes, buffer_end);
23339 if (bytes == NULL)
23340 {
23341 dwarf2_section_buffer_overflow_complaint (section);
23342 return NULL;
23343 }
23344 break;
23345
23346 case DW_FORM_implicit_const:
23347 break;
23348
23349 default:
23350 {
23351 complaint (_("invalid form 0x%x in `%s'"),
23352 form, section->get_name ());
23353 return NULL;
23354 }
23355 }
23356
23357 return bytes;
23358 }
23359
23360 /* A helper for dwarf_decode_macros that handles skipping an unknown
23361 opcode. Returns an updated pointer to the macro data buffer; or,
23362 on error, issues a complaint and returns NULL. */
23363
23364 static const gdb_byte *
23365 skip_unknown_opcode (unsigned int opcode,
23366 const gdb_byte **opcode_definitions,
23367 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23368 bfd *abfd,
23369 unsigned int offset_size,
23370 struct dwarf2_section_info *section)
23371 {
23372 unsigned int bytes_read, i;
23373 unsigned long arg;
23374 const gdb_byte *defn;
23375
23376 if (opcode_definitions[opcode] == NULL)
23377 {
23378 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
23379 opcode);
23380 return NULL;
23381 }
23382
23383 defn = opcode_definitions[opcode];
23384 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23385 defn += bytes_read;
23386
23387 for (i = 0; i < arg; ++i)
23388 {
23389 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23390 (enum dwarf_form) defn[i], offset_size,
23391 section);
23392 if (mac_ptr == NULL)
23393 {
23394 /* skip_form_bytes already issued the complaint. */
23395 return NULL;
23396 }
23397 }
23398
23399 return mac_ptr;
23400 }
23401
23402 /* A helper function which parses the header of a macro section.
23403 If the macro section is the extended (for now called "GNU") type,
23404 then this updates *OFFSET_SIZE. Returns a pointer to just after
23405 the header, or issues a complaint and returns NULL on error. */
23406
23407 static const gdb_byte *
23408 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
23409 bfd *abfd,
23410 const gdb_byte *mac_ptr,
23411 unsigned int *offset_size,
23412 int section_is_gnu)
23413 {
23414 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
23415
23416 if (section_is_gnu)
23417 {
23418 unsigned int version, flags;
23419
23420 version = read_2_bytes (abfd, mac_ptr);
23421 if (version != 4 && version != 5)
23422 {
23423 complaint (_("unrecognized version `%d' in .debug_macro section"),
23424 version);
23425 return NULL;
23426 }
23427 mac_ptr += 2;
23428
23429 flags = read_1_byte (abfd, mac_ptr);
23430 ++mac_ptr;
23431 *offset_size = (flags & 1) ? 8 : 4;
23432
23433 if ((flags & 2) != 0)
23434 /* We don't need the line table offset. */
23435 mac_ptr += *offset_size;
23436
23437 /* Vendor opcode descriptions. */
23438 if ((flags & 4) != 0)
23439 {
23440 unsigned int i, count;
23441
23442 count = read_1_byte (abfd, mac_ptr);
23443 ++mac_ptr;
23444 for (i = 0; i < count; ++i)
23445 {
23446 unsigned int opcode, bytes_read;
23447 unsigned long arg;
23448
23449 opcode = read_1_byte (abfd, mac_ptr);
23450 ++mac_ptr;
23451 opcode_definitions[opcode] = mac_ptr;
23452 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23453 mac_ptr += bytes_read;
23454 mac_ptr += arg;
23455 }
23456 }
23457 }
23458
23459 return mac_ptr;
23460 }
23461
23462 /* A helper for dwarf_decode_macros that handles the GNU extensions,
23463 including DW_MACRO_import. */
23464
23465 static void
23466 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
23467 bfd *abfd,
23468 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23469 struct macro_source_file *current_file,
23470 struct line_header *lh,
23471 struct dwarf2_section_info *section,
23472 int section_is_gnu, int section_is_dwz,
23473 unsigned int offset_size,
23474 htab_t include_hash)
23475 {
23476 struct dwarf2_per_objfile *dwarf2_per_objfile
23477 = cu->per_cu->dwarf2_per_objfile;
23478 struct objfile *objfile = dwarf2_per_objfile->objfile;
23479 enum dwarf_macro_record_type macinfo_type;
23480 int at_commandline;
23481 const gdb_byte *opcode_definitions[256];
23482
23483 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23484 &offset_size, section_is_gnu);
23485 if (mac_ptr == NULL)
23486 {
23487 /* We already issued a complaint. */
23488 return;
23489 }
23490
23491 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23492 GDB is still reading the definitions from command line. First
23493 DW_MACINFO_start_file will need to be ignored as it was already executed
23494 to create CURRENT_FILE for the main source holding also the command line
23495 definitions. On first met DW_MACINFO_start_file this flag is reset to
23496 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23497
23498 at_commandline = 1;
23499
23500 do
23501 {
23502 /* Do we at least have room for a macinfo type byte? */
23503 if (mac_ptr >= mac_end)
23504 {
23505 dwarf2_section_buffer_overflow_complaint (section);
23506 break;
23507 }
23508
23509 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23510 mac_ptr++;
23511
23512 /* Note that we rely on the fact that the corresponding GNU and
23513 DWARF constants are the same. */
23514 DIAGNOSTIC_PUSH
23515 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23516 switch (macinfo_type)
23517 {
23518 /* A zero macinfo type indicates the end of the macro
23519 information. */
23520 case 0:
23521 break;
23522
23523 case DW_MACRO_define:
23524 case DW_MACRO_undef:
23525 case DW_MACRO_define_strp:
23526 case DW_MACRO_undef_strp:
23527 case DW_MACRO_define_sup:
23528 case DW_MACRO_undef_sup:
23529 {
23530 unsigned int bytes_read;
23531 int line;
23532 const char *body;
23533 int is_define;
23534
23535 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23536 mac_ptr += bytes_read;
23537
23538 if (macinfo_type == DW_MACRO_define
23539 || macinfo_type == DW_MACRO_undef)
23540 {
23541 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23542 mac_ptr += bytes_read;
23543 }
23544 else
23545 {
23546 LONGEST str_offset;
23547
23548 str_offset = read_offset (abfd, mac_ptr, offset_size);
23549 mac_ptr += offset_size;
23550
23551 if (macinfo_type == DW_MACRO_define_sup
23552 || macinfo_type == DW_MACRO_undef_sup
23553 || section_is_dwz)
23554 {
23555 struct dwz_file *dwz
23556 = dwarf2_get_dwz_file (dwarf2_per_objfile);
23557
23558 body = dwz->read_string (objfile, str_offset);
23559 }
23560 else
23561 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23562 abfd, str_offset);
23563 }
23564
23565 is_define = (macinfo_type == DW_MACRO_define
23566 || macinfo_type == DW_MACRO_define_strp
23567 || macinfo_type == DW_MACRO_define_sup);
23568 if (! current_file)
23569 {
23570 /* DWARF violation as no main source is present. */
23571 complaint (_("debug info with no main source gives macro %s "
23572 "on line %d: %s"),
23573 is_define ? _("definition") : _("undefinition"),
23574 line, body);
23575 break;
23576 }
23577 if ((line == 0 && !at_commandline)
23578 || (line != 0 && at_commandline))
23579 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
23580 at_commandline ? _("command-line") : _("in-file"),
23581 is_define ? _("definition") : _("undefinition"),
23582 line == 0 ? _("zero") : _("non-zero"), line, body);
23583
23584 if (body == NULL)
23585 {
23586 /* Fedora's rpm-build's "debugedit" binary
23587 corrupted .debug_macro sections.
23588
23589 For more info, see
23590 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23591 complaint (_("debug info gives %s invalid macro %s "
23592 "without body (corrupted?) at line %d "
23593 "on file %s"),
23594 at_commandline ? _("command-line") : _("in-file"),
23595 is_define ? _("definition") : _("undefinition"),
23596 line, current_file->filename);
23597 }
23598 else if (is_define)
23599 parse_macro_definition (current_file, line, body);
23600 else
23601 {
23602 gdb_assert (macinfo_type == DW_MACRO_undef
23603 || macinfo_type == DW_MACRO_undef_strp
23604 || macinfo_type == DW_MACRO_undef_sup);
23605 macro_undef (current_file, line, body);
23606 }
23607 }
23608 break;
23609
23610 case DW_MACRO_start_file:
23611 {
23612 unsigned int bytes_read;
23613 int line, file;
23614
23615 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23616 mac_ptr += bytes_read;
23617 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23618 mac_ptr += bytes_read;
23619
23620 if ((line == 0 && !at_commandline)
23621 || (line != 0 && at_commandline))
23622 complaint (_("debug info gives source %d included "
23623 "from %s at %s line %d"),
23624 file, at_commandline ? _("command-line") : _("file"),
23625 line == 0 ? _("zero") : _("non-zero"), line);
23626
23627 if (at_commandline)
23628 {
23629 /* This DW_MACRO_start_file was executed in the
23630 pass one. */
23631 at_commandline = 0;
23632 }
23633 else
23634 current_file = macro_start_file (cu, file, line, current_file,
23635 lh);
23636 }
23637 break;
23638
23639 case DW_MACRO_end_file:
23640 if (! current_file)
23641 complaint (_("macro debug info has an unmatched "
23642 "`close_file' directive"));
23643 else
23644 {
23645 current_file = current_file->included_by;
23646 if (! current_file)
23647 {
23648 enum dwarf_macro_record_type next_type;
23649
23650 /* GCC circa March 2002 doesn't produce the zero
23651 type byte marking the end of the compilation
23652 unit. Complain if it's not there, but exit no
23653 matter what. */
23654
23655 /* Do we at least have room for a macinfo type byte? */
23656 if (mac_ptr >= mac_end)
23657 {
23658 dwarf2_section_buffer_overflow_complaint (section);
23659 return;
23660 }
23661
23662 /* We don't increment mac_ptr here, so this is just
23663 a look-ahead. */
23664 next_type
23665 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23666 mac_ptr);
23667 if (next_type != 0)
23668 complaint (_("no terminating 0-type entry for "
23669 "macros in `.debug_macinfo' section"));
23670
23671 return;
23672 }
23673 }
23674 break;
23675
23676 case DW_MACRO_import:
23677 case DW_MACRO_import_sup:
23678 {
23679 LONGEST offset;
23680 void **slot;
23681 bfd *include_bfd = abfd;
23682 struct dwarf2_section_info *include_section = section;
23683 const gdb_byte *include_mac_end = mac_end;
23684 int is_dwz = section_is_dwz;
23685 const gdb_byte *new_mac_ptr;
23686
23687 offset = read_offset (abfd, mac_ptr, offset_size);
23688 mac_ptr += offset_size;
23689
23690 if (macinfo_type == DW_MACRO_import_sup)
23691 {
23692 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
23693
23694 dwz->macro.read (objfile);
23695
23696 include_section = &dwz->macro;
23697 include_bfd = include_section->get_bfd_owner ();
23698 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23699 is_dwz = 1;
23700 }
23701
23702 new_mac_ptr = include_section->buffer + offset;
23703 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23704
23705 if (*slot != NULL)
23706 {
23707 /* This has actually happened; see
23708 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23709 complaint (_("recursive DW_MACRO_import in "
23710 ".debug_macro section"));
23711 }
23712 else
23713 {
23714 *slot = (void *) new_mac_ptr;
23715
23716 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
23717 include_mac_end, current_file, lh,
23718 section, section_is_gnu, is_dwz,
23719 offset_size, include_hash);
23720
23721 htab_remove_elt (include_hash, (void *) new_mac_ptr);
23722 }
23723 }
23724 break;
23725
23726 case DW_MACINFO_vendor_ext:
23727 if (!section_is_gnu)
23728 {
23729 unsigned int bytes_read;
23730
23731 /* This reads the constant, but since we don't recognize
23732 any vendor extensions, we ignore it. */
23733 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23734 mac_ptr += bytes_read;
23735 read_direct_string (abfd, mac_ptr, &bytes_read);
23736 mac_ptr += bytes_read;
23737
23738 /* We don't recognize any vendor extensions. */
23739 break;
23740 }
23741 /* FALLTHROUGH */
23742
23743 default:
23744 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23745 mac_ptr, mac_end, abfd, offset_size,
23746 section);
23747 if (mac_ptr == NULL)
23748 return;
23749 break;
23750 }
23751 DIAGNOSTIC_POP
23752 } while (macinfo_type != 0);
23753 }
23754
23755 static void
23756 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23757 int section_is_gnu)
23758 {
23759 struct dwarf2_per_objfile *dwarf2_per_objfile
23760 = cu->per_cu->dwarf2_per_objfile;
23761 struct objfile *objfile = dwarf2_per_objfile->objfile;
23762 struct line_header *lh = cu->line_header;
23763 bfd *abfd;
23764 const gdb_byte *mac_ptr, *mac_end;
23765 struct macro_source_file *current_file = 0;
23766 enum dwarf_macro_record_type macinfo_type;
23767 unsigned int offset_size = cu->header.offset_size;
23768 const gdb_byte *opcode_definitions[256];
23769 void **slot;
23770 struct dwarf2_section_info *section;
23771 const char *section_name;
23772
23773 if (cu->dwo_unit != NULL)
23774 {
23775 if (section_is_gnu)
23776 {
23777 section = &cu->dwo_unit->dwo_file->sections.macro;
23778 section_name = ".debug_macro.dwo";
23779 }
23780 else
23781 {
23782 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23783 section_name = ".debug_macinfo.dwo";
23784 }
23785 }
23786 else
23787 {
23788 if (section_is_gnu)
23789 {
23790 section = &dwarf2_per_objfile->macro;
23791 section_name = ".debug_macro";
23792 }
23793 else
23794 {
23795 section = &dwarf2_per_objfile->macinfo;
23796 section_name = ".debug_macinfo";
23797 }
23798 }
23799
23800 section->read (objfile);
23801 if (section->buffer == NULL)
23802 {
23803 complaint (_("missing %s section"), section_name);
23804 return;
23805 }
23806 abfd = section->get_bfd_owner ();
23807
23808 /* First pass: Find the name of the base filename.
23809 This filename is needed in order to process all macros whose definition
23810 (or undefinition) comes from the command line. These macros are defined
23811 before the first DW_MACINFO_start_file entry, and yet still need to be
23812 associated to the base file.
23813
23814 To determine the base file name, we scan the macro definitions until we
23815 reach the first DW_MACINFO_start_file entry. We then initialize
23816 CURRENT_FILE accordingly so that any macro definition found before the
23817 first DW_MACINFO_start_file can still be associated to the base file. */
23818
23819 mac_ptr = section->buffer + offset;
23820 mac_end = section->buffer + section->size;
23821
23822 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23823 &offset_size, section_is_gnu);
23824 if (mac_ptr == NULL)
23825 {
23826 /* We already issued a complaint. */
23827 return;
23828 }
23829
23830 do
23831 {
23832 /* Do we at least have room for a macinfo type byte? */
23833 if (mac_ptr >= mac_end)
23834 {
23835 /* Complaint is printed during the second pass as GDB will probably
23836 stop the first pass earlier upon finding
23837 DW_MACINFO_start_file. */
23838 break;
23839 }
23840
23841 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23842 mac_ptr++;
23843
23844 /* Note that we rely on the fact that the corresponding GNU and
23845 DWARF constants are the same. */
23846 DIAGNOSTIC_PUSH
23847 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23848 switch (macinfo_type)
23849 {
23850 /* A zero macinfo type indicates the end of the macro
23851 information. */
23852 case 0:
23853 break;
23854
23855 case DW_MACRO_define:
23856 case DW_MACRO_undef:
23857 /* Only skip the data by MAC_PTR. */
23858 {
23859 unsigned int bytes_read;
23860
23861 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23862 mac_ptr += bytes_read;
23863 read_direct_string (abfd, mac_ptr, &bytes_read);
23864 mac_ptr += bytes_read;
23865 }
23866 break;
23867
23868 case DW_MACRO_start_file:
23869 {
23870 unsigned int bytes_read;
23871 int line, file;
23872
23873 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23874 mac_ptr += bytes_read;
23875 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23876 mac_ptr += bytes_read;
23877
23878 current_file = macro_start_file (cu, file, line, current_file, lh);
23879 }
23880 break;
23881
23882 case DW_MACRO_end_file:
23883 /* No data to skip by MAC_PTR. */
23884 break;
23885
23886 case DW_MACRO_define_strp:
23887 case DW_MACRO_undef_strp:
23888 case DW_MACRO_define_sup:
23889 case DW_MACRO_undef_sup:
23890 {
23891 unsigned int bytes_read;
23892
23893 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23894 mac_ptr += bytes_read;
23895 mac_ptr += offset_size;
23896 }
23897 break;
23898
23899 case DW_MACRO_import:
23900 case DW_MACRO_import_sup:
23901 /* Note that, according to the spec, a transparent include
23902 chain cannot call DW_MACRO_start_file. So, we can just
23903 skip this opcode. */
23904 mac_ptr += offset_size;
23905 break;
23906
23907 case DW_MACINFO_vendor_ext:
23908 /* Only skip the data by MAC_PTR. */
23909 if (!section_is_gnu)
23910 {
23911 unsigned int bytes_read;
23912
23913 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23914 mac_ptr += bytes_read;
23915 read_direct_string (abfd, mac_ptr, &bytes_read);
23916 mac_ptr += bytes_read;
23917 }
23918 /* FALLTHROUGH */
23919
23920 default:
23921 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23922 mac_ptr, mac_end, abfd, offset_size,
23923 section);
23924 if (mac_ptr == NULL)
23925 return;
23926 break;
23927 }
23928 DIAGNOSTIC_POP
23929 } while (macinfo_type != 0 && current_file == NULL);
23930
23931 /* Second pass: Process all entries.
23932
23933 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23934 command-line macro definitions/undefinitions. This flag is unset when we
23935 reach the first DW_MACINFO_start_file entry. */
23936
23937 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23938 htab_eq_pointer,
23939 NULL, xcalloc, xfree));
23940 mac_ptr = section->buffer + offset;
23941 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
23942 *slot = (void *) mac_ptr;
23943 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
23944 current_file, lh, section,
23945 section_is_gnu, 0, offset_size,
23946 include_hash.get ());
23947 }
23948
23949 /* Return the .debug_loc section to use for CU.
23950 For DWO files use .debug_loc.dwo. */
23951
23952 static struct dwarf2_section_info *
23953 cu_debug_loc_section (struct dwarf2_cu *cu)
23954 {
23955 struct dwarf2_per_objfile *dwarf2_per_objfile
23956 = cu->per_cu->dwarf2_per_objfile;
23957
23958 if (cu->dwo_unit)
23959 {
23960 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23961
23962 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23963 }
23964 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23965 : &dwarf2_per_objfile->loc);
23966 }
23967
23968 /* A helper function that fills in a dwarf2_loclist_baton. */
23969
23970 static void
23971 fill_in_loclist_baton (struct dwarf2_cu *cu,
23972 struct dwarf2_loclist_baton *baton,
23973 const struct attribute *attr)
23974 {
23975 struct dwarf2_per_objfile *dwarf2_per_objfile
23976 = cu->per_cu->dwarf2_per_objfile;
23977 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23978
23979 section->read (dwarf2_per_objfile->objfile);
23980
23981 baton->per_cu = cu->per_cu;
23982 gdb_assert (baton->per_cu);
23983 /* We don't know how long the location list is, but make sure we
23984 don't run off the edge of the section. */
23985 baton->size = section->size - DW_UNSND (attr);
23986 baton->data = section->buffer + DW_UNSND (attr);
23987 baton->base_address = cu->base_address;
23988 baton->from_dwo = cu->dwo_unit != NULL;
23989 }
23990
23991 static void
23992 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23993 struct dwarf2_cu *cu, int is_block)
23994 {
23995 struct dwarf2_per_objfile *dwarf2_per_objfile
23996 = cu->per_cu->dwarf2_per_objfile;
23997 struct objfile *objfile = dwarf2_per_objfile->objfile;
23998 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23999
24000 if (attr->form_is_section_offset ()
24001 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24002 the section. If so, fall through to the complaint in the
24003 other branch. */
24004 && DW_UNSND (attr) < section->get_size (objfile))
24005 {
24006 struct dwarf2_loclist_baton *baton;
24007
24008 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24009
24010 fill_in_loclist_baton (cu, baton, attr);
24011
24012 if (cu->base_known == 0)
24013 complaint (_("Location list used without "
24014 "specifying the CU base address."));
24015
24016 SYMBOL_ACLASS_INDEX (sym) = (is_block
24017 ? dwarf2_loclist_block_index
24018 : dwarf2_loclist_index);
24019 SYMBOL_LOCATION_BATON (sym) = baton;
24020 }
24021 else
24022 {
24023 struct dwarf2_locexpr_baton *baton;
24024
24025 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24026 baton->per_cu = cu->per_cu;
24027 gdb_assert (baton->per_cu);
24028
24029 if (attr->form_is_block ())
24030 {
24031 /* Note that we're just copying the block's data pointer
24032 here, not the actual data. We're still pointing into the
24033 info_buffer for SYM's objfile; right now we never release
24034 that buffer, but when we do clean up properly this may
24035 need to change. */
24036 baton->size = DW_BLOCK (attr)->size;
24037 baton->data = DW_BLOCK (attr)->data;
24038 }
24039 else
24040 {
24041 dwarf2_invalid_attrib_class_complaint ("location description",
24042 sym->natural_name ());
24043 baton->size = 0;
24044 }
24045
24046 SYMBOL_ACLASS_INDEX (sym) = (is_block
24047 ? dwarf2_locexpr_block_index
24048 : dwarf2_locexpr_index);
24049 SYMBOL_LOCATION_BATON (sym) = baton;
24050 }
24051 }
24052
24053 /* See read.h. */
24054
24055 struct objfile *
24056 dwarf2_per_cu_data::objfile () const
24057 {
24058 struct objfile *objfile = dwarf2_per_objfile->objfile;
24059
24060 /* Return the master objfile, so that we can report and look up the
24061 correct file containing this variable. */
24062 if (objfile->separate_debug_objfile_backlink)
24063 objfile = objfile->separate_debug_objfile_backlink;
24064
24065 return objfile;
24066 }
24067
24068 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24069 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24070 CU_HEADERP first. */
24071
24072 static const struct comp_unit_head *
24073 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24074 const struct dwarf2_per_cu_data *per_cu)
24075 {
24076 const gdb_byte *info_ptr;
24077
24078 if (per_cu->cu)
24079 return &per_cu->cu->header;
24080
24081 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24082
24083 memset (cu_headerp, 0, sizeof (*cu_headerp));
24084 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24085 rcuh_kind::COMPILE);
24086
24087 return cu_headerp;
24088 }
24089
24090 /* See read.h. */
24091
24092 int
24093 dwarf2_per_cu_data::addr_size () const
24094 {
24095 struct comp_unit_head cu_header_local;
24096 const struct comp_unit_head *cu_headerp;
24097
24098 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24099
24100 return cu_headerp->addr_size;
24101 }
24102
24103 /* See read.h. */
24104
24105 int
24106 dwarf2_per_cu_data::offset_size () const
24107 {
24108 struct comp_unit_head cu_header_local;
24109 const struct comp_unit_head *cu_headerp;
24110
24111 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24112
24113 return cu_headerp->offset_size;
24114 }
24115
24116 /* See read.h. */
24117
24118 int
24119 dwarf2_per_cu_data::ref_addr_size () const
24120 {
24121 struct comp_unit_head cu_header_local;
24122 const struct comp_unit_head *cu_headerp;
24123
24124 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24125
24126 if (cu_headerp->version == 2)
24127 return cu_headerp->addr_size;
24128 else
24129 return cu_headerp->offset_size;
24130 }
24131
24132 /* See read.h. */
24133
24134 CORE_ADDR
24135 dwarf2_per_cu_data::text_offset () const
24136 {
24137 struct objfile *objfile = dwarf2_per_objfile->objfile;
24138
24139 return objfile->text_section_offset ();
24140 }
24141
24142 /* See read.h. */
24143
24144 struct type *
24145 dwarf2_per_cu_data::addr_type () const
24146 {
24147 struct objfile *objfile = dwarf2_per_objfile->objfile;
24148 struct type *void_type = objfile_type (objfile)->builtin_void;
24149 struct type *addr_type = lookup_pointer_type (void_type);
24150 int addr_size = this->addr_size ();
24151
24152 if (TYPE_LENGTH (addr_type) == addr_size)
24153 return addr_type;
24154
24155 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
24156 return addr_type;
24157 }
24158
24159 /* A helper function for dwarf2_find_containing_comp_unit that returns
24160 the index of the result, and that searches a vector. It will
24161 return a result even if the offset in question does not actually
24162 occur in any CU. This is separate so that it can be unit
24163 tested. */
24164
24165 static int
24166 dwarf2_find_containing_comp_unit
24167 (sect_offset sect_off,
24168 unsigned int offset_in_dwz,
24169 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24170 {
24171 int low, high;
24172
24173 low = 0;
24174 high = all_comp_units.size () - 1;
24175 while (high > low)
24176 {
24177 struct dwarf2_per_cu_data *mid_cu;
24178 int mid = low + (high - low) / 2;
24179
24180 mid_cu = all_comp_units[mid];
24181 if (mid_cu->is_dwz > offset_in_dwz
24182 || (mid_cu->is_dwz == offset_in_dwz
24183 && mid_cu->sect_off + mid_cu->length > sect_off))
24184 high = mid;
24185 else
24186 low = mid + 1;
24187 }
24188 gdb_assert (low == high);
24189 return low;
24190 }
24191
24192 /* Locate the .debug_info compilation unit from CU's objfile which contains
24193 the DIE at OFFSET. Raises an error on failure. */
24194
24195 static struct dwarf2_per_cu_data *
24196 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24197 unsigned int offset_in_dwz,
24198 struct dwarf2_per_objfile *dwarf2_per_objfile)
24199 {
24200 int low
24201 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
24202 dwarf2_per_objfile->all_comp_units);
24203 struct dwarf2_per_cu_data *this_cu
24204 = dwarf2_per_objfile->all_comp_units[low];
24205
24206 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24207 {
24208 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24209 error (_("Dwarf Error: could not find partial DIE containing "
24210 "offset %s [in module %s]"),
24211 sect_offset_str (sect_off),
24212 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24213
24214 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24215 <= sect_off);
24216 return dwarf2_per_objfile->all_comp_units[low-1];
24217 }
24218 else
24219 {
24220 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24221 && sect_off >= this_cu->sect_off + this_cu->length)
24222 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24223 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24224 return this_cu;
24225 }
24226 }
24227
24228 #if GDB_SELF_TEST
24229
24230 namespace selftests {
24231 namespace find_containing_comp_unit {
24232
24233 static void
24234 run_test ()
24235 {
24236 struct dwarf2_per_cu_data one {};
24237 struct dwarf2_per_cu_data two {};
24238 struct dwarf2_per_cu_data three {};
24239 struct dwarf2_per_cu_data four {};
24240
24241 one.length = 5;
24242 two.sect_off = sect_offset (one.length);
24243 two.length = 7;
24244
24245 three.length = 5;
24246 three.is_dwz = 1;
24247 four.sect_off = sect_offset (three.length);
24248 four.length = 7;
24249 four.is_dwz = 1;
24250
24251 std::vector<dwarf2_per_cu_data *> units;
24252 units.push_back (&one);
24253 units.push_back (&two);
24254 units.push_back (&three);
24255 units.push_back (&four);
24256
24257 int result;
24258
24259 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24260 SELF_CHECK (units[result] == &one);
24261 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24262 SELF_CHECK (units[result] == &one);
24263 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24264 SELF_CHECK (units[result] == &two);
24265
24266 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24267 SELF_CHECK (units[result] == &three);
24268 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24269 SELF_CHECK (units[result] == &three);
24270 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24271 SELF_CHECK (units[result] == &four);
24272 }
24273
24274 }
24275 }
24276
24277 #endif /* GDB_SELF_TEST */
24278
24279 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24280
24281 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24282 : per_cu (per_cu_),
24283 mark (false),
24284 has_loclist (false),
24285 checked_producer (false),
24286 producer_is_gxx_lt_4_6 (false),
24287 producer_is_gcc_lt_4_3 (false),
24288 producer_is_icc (false),
24289 producer_is_icc_lt_14 (false),
24290 producer_is_codewarrior (false),
24291 processing_has_namespace_info (false)
24292 {
24293 per_cu->cu = this;
24294 }
24295
24296 /* Destroy a dwarf2_cu. */
24297
24298 dwarf2_cu::~dwarf2_cu ()
24299 {
24300 per_cu->cu = NULL;
24301 }
24302
24303 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24304
24305 static void
24306 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24307 enum language pretend_language)
24308 {
24309 struct attribute *attr;
24310
24311 /* Set the language we're debugging. */
24312 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24313 if (attr != nullptr)
24314 set_cu_language (DW_UNSND (attr), cu);
24315 else
24316 {
24317 cu->language = pretend_language;
24318 cu->language_defn = language_def (cu->language);
24319 }
24320
24321 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24322 }
24323
24324 /* Increase the age counter on each cached compilation unit, and free
24325 any that are too old. */
24326
24327 static void
24328 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
24329 {
24330 struct dwarf2_per_cu_data *per_cu, **last_chain;
24331
24332 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24333 per_cu = dwarf2_per_objfile->read_in_chain;
24334 while (per_cu != NULL)
24335 {
24336 per_cu->cu->last_used ++;
24337 if (per_cu->cu->last_used <= dwarf_max_cache_age)
24338 dwarf2_mark (per_cu->cu);
24339 per_cu = per_cu->cu->read_in_chain;
24340 }
24341
24342 per_cu = dwarf2_per_objfile->read_in_chain;
24343 last_chain = &dwarf2_per_objfile->read_in_chain;
24344 while (per_cu != NULL)
24345 {
24346 struct dwarf2_per_cu_data *next_cu;
24347
24348 next_cu = per_cu->cu->read_in_chain;
24349
24350 if (!per_cu->cu->mark)
24351 {
24352 delete per_cu->cu;
24353 *last_chain = next_cu;
24354 }
24355 else
24356 last_chain = &per_cu->cu->read_in_chain;
24357
24358 per_cu = next_cu;
24359 }
24360 }
24361
24362 /* Remove a single compilation unit from the cache. */
24363
24364 static void
24365 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
24366 {
24367 struct dwarf2_per_cu_data *per_cu, **last_chain;
24368 struct dwarf2_per_objfile *dwarf2_per_objfile
24369 = target_per_cu->dwarf2_per_objfile;
24370
24371 per_cu = dwarf2_per_objfile->read_in_chain;
24372 last_chain = &dwarf2_per_objfile->read_in_chain;
24373 while (per_cu != NULL)
24374 {
24375 struct dwarf2_per_cu_data *next_cu;
24376
24377 next_cu = per_cu->cu->read_in_chain;
24378
24379 if (per_cu == target_per_cu)
24380 {
24381 delete per_cu->cu;
24382 per_cu->cu = NULL;
24383 *last_chain = next_cu;
24384 break;
24385 }
24386 else
24387 last_chain = &per_cu->cu->read_in_chain;
24388
24389 per_cu = next_cu;
24390 }
24391 }
24392
24393 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24394 We store these in a hash table separate from the DIEs, and preserve them
24395 when the DIEs are flushed out of cache.
24396
24397 The CU "per_cu" pointer is needed because offset alone is not enough to
24398 uniquely identify the type. A file may have multiple .debug_types sections,
24399 or the type may come from a DWO file. Furthermore, while it's more logical
24400 to use per_cu->section+offset, with Fission the section with the data is in
24401 the DWO file but we don't know that section at the point we need it.
24402 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24403 because we can enter the lookup routine, get_die_type_at_offset, from
24404 outside this file, and thus won't necessarily have PER_CU->cu.
24405 Fortunately, PER_CU is stable for the life of the objfile. */
24406
24407 struct dwarf2_per_cu_offset_and_type
24408 {
24409 const struct dwarf2_per_cu_data *per_cu;
24410 sect_offset sect_off;
24411 struct type *type;
24412 };
24413
24414 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24415
24416 static hashval_t
24417 per_cu_offset_and_type_hash (const void *item)
24418 {
24419 const struct dwarf2_per_cu_offset_and_type *ofs
24420 = (const struct dwarf2_per_cu_offset_and_type *) item;
24421
24422 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24423 }
24424
24425 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24426
24427 static int
24428 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24429 {
24430 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24431 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24432 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24433 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24434
24435 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24436 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24437 }
24438
24439 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24440 table if necessary. For convenience, return TYPE.
24441
24442 The DIEs reading must have careful ordering to:
24443 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24444 reading current DIE.
24445 * Not trying to dereference contents of still incompletely read in types
24446 while reading in other DIEs.
24447 * Enable referencing still incompletely read in types just by a pointer to
24448 the type without accessing its fields.
24449
24450 Therefore caller should follow these rules:
24451 * Try to fetch any prerequisite types we may need to build this DIE type
24452 before building the type and calling set_die_type.
24453 * After building type call set_die_type for current DIE as soon as
24454 possible before fetching more types to complete the current type.
24455 * Make the type as complete as possible before fetching more types. */
24456
24457 static struct type *
24458 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24459 {
24460 struct dwarf2_per_objfile *dwarf2_per_objfile
24461 = cu->per_cu->dwarf2_per_objfile;
24462 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24463 struct objfile *objfile = dwarf2_per_objfile->objfile;
24464 struct attribute *attr;
24465 struct dynamic_prop prop;
24466
24467 /* For Ada types, make sure that the gnat-specific data is always
24468 initialized (if not already set). There are a few types where
24469 we should not be doing so, because the type-specific area is
24470 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24471 where the type-specific area is used to store the floatformat).
24472 But this is not a problem, because the gnat-specific information
24473 is actually not needed for these types. */
24474 if (need_gnat_info (cu)
24475 && TYPE_CODE (type) != TYPE_CODE_FUNC
24476 && TYPE_CODE (type) != TYPE_CODE_FLT
24477 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24478 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24479 && TYPE_CODE (type) != TYPE_CODE_METHOD
24480 && !HAVE_GNAT_AUX_INFO (type))
24481 INIT_GNAT_SPECIFIC (type);
24482
24483 /* Read DW_AT_allocated and set in type. */
24484 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24485 if (attr != NULL && attr->form_is_block ())
24486 {
24487 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24488 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24489 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
24490 }
24491 else if (attr != NULL)
24492 {
24493 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
24494 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24495 sect_offset_str (die->sect_off));
24496 }
24497
24498 /* Read DW_AT_associated and set in type. */
24499 attr = dwarf2_attr (die, DW_AT_associated, cu);
24500 if (attr != NULL && attr->form_is_block ())
24501 {
24502 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24503 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24504 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
24505 }
24506 else if (attr != NULL)
24507 {
24508 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
24509 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24510 sect_offset_str (die->sect_off));
24511 }
24512
24513 /* Read DW_AT_data_location and set in type. */
24514 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24515 if (attr_to_dynamic_prop (attr, die, cu, &prop,
24516 cu->per_cu->addr_type ()))
24517 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
24518
24519 if (dwarf2_per_objfile->die_type_hash == NULL)
24520 dwarf2_per_objfile->die_type_hash
24521 = htab_up (htab_create_alloc (127,
24522 per_cu_offset_and_type_hash,
24523 per_cu_offset_and_type_eq,
24524 NULL, xcalloc, xfree));
24525
24526 ofs.per_cu = cu->per_cu;
24527 ofs.sect_off = die->sect_off;
24528 ofs.type = type;
24529 slot = (struct dwarf2_per_cu_offset_and_type **)
24530 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
24531 if (*slot)
24532 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24533 sect_offset_str (die->sect_off));
24534 *slot = XOBNEW (&objfile->objfile_obstack,
24535 struct dwarf2_per_cu_offset_and_type);
24536 **slot = ofs;
24537 return type;
24538 }
24539
24540 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24541 or return NULL if the die does not have a saved type. */
24542
24543 static struct type *
24544 get_die_type_at_offset (sect_offset sect_off,
24545 struct dwarf2_per_cu_data *per_cu)
24546 {
24547 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24548 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
24549
24550 if (dwarf2_per_objfile->die_type_hash == NULL)
24551 return NULL;
24552
24553 ofs.per_cu = per_cu;
24554 ofs.sect_off = sect_off;
24555 slot = ((struct dwarf2_per_cu_offset_and_type *)
24556 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
24557 if (slot)
24558 return slot->type;
24559 else
24560 return NULL;
24561 }
24562
24563 /* Look up the type for DIE in CU in die_type_hash,
24564 or return NULL if DIE does not have a saved type. */
24565
24566 static struct type *
24567 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24568 {
24569 return get_die_type_at_offset (die->sect_off, cu->per_cu);
24570 }
24571
24572 /* Add a dependence relationship from CU to REF_PER_CU. */
24573
24574 static void
24575 dwarf2_add_dependence (struct dwarf2_cu *cu,
24576 struct dwarf2_per_cu_data *ref_per_cu)
24577 {
24578 void **slot;
24579
24580 if (cu->dependencies == NULL)
24581 cu->dependencies
24582 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24583 NULL, &cu->comp_unit_obstack,
24584 hashtab_obstack_allocate,
24585 dummy_obstack_deallocate);
24586
24587 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24588 if (*slot == NULL)
24589 *slot = ref_per_cu;
24590 }
24591
24592 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24593 Set the mark field in every compilation unit in the
24594 cache that we must keep because we are keeping CU. */
24595
24596 static int
24597 dwarf2_mark_helper (void **slot, void *data)
24598 {
24599 struct dwarf2_per_cu_data *per_cu;
24600
24601 per_cu = (struct dwarf2_per_cu_data *) *slot;
24602
24603 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24604 reading of the chain. As such dependencies remain valid it is not much
24605 useful to track and undo them during QUIT cleanups. */
24606 if (per_cu->cu == NULL)
24607 return 1;
24608
24609 if (per_cu->cu->mark)
24610 return 1;
24611 per_cu->cu->mark = true;
24612
24613 if (per_cu->cu->dependencies != NULL)
24614 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24615
24616 return 1;
24617 }
24618
24619 /* Set the mark field in CU and in every other compilation unit in the
24620 cache that we must keep because we are keeping CU. */
24621
24622 static void
24623 dwarf2_mark (struct dwarf2_cu *cu)
24624 {
24625 if (cu->mark)
24626 return;
24627 cu->mark = true;
24628 if (cu->dependencies != NULL)
24629 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
24630 }
24631
24632 static void
24633 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24634 {
24635 while (per_cu)
24636 {
24637 per_cu->cu->mark = false;
24638 per_cu = per_cu->cu->read_in_chain;
24639 }
24640 }
24641
24642 /* Trivial hash function for partial_die_info: the hash value of a DIE
24643 is its offset in .debug_info for this objfile. */
24644
24645 static hashval_t
24646 partial_die_hash (const void *item)
24647 {
24648 const struct partial_die_info *part_die
24649 = (const struct partial_die_info *) item;
24650
24651 return to_underlying (part_die->sect_off);
24652 }
24653
24654 /* Trivial comparison function for partial_die_info structures: two DIEs
24655 are equal if they have the same offset. */
24656
24657 static int
24658 partial_die_eq (const void *item_lhs, const void *item_rhs)
24659 {
24660 const struct partial_die_info *part_die_lhs
24661 = (const struct partial_die_info *) item_lhs;
24662 const struct partial_die_info *part_die_rhs
24663 = (const struct partial_die_info *) item_rhs;
24664
24665 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24666 }
24667
24668 struct cmd_list_element *set_dwarf_cmdlist;
24669 struct cmd_list_element *show_dwarf_cmdlist;
24670
24671 static void
24672 set_dwarf_cmd (const char *args, int from_tty)
24673 {
24674 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
24675 gdb_stdout);
24676 }
24677
24678 static void
24679 show_dwarf_cmd (const char *args, int from_tty)
24680 {
24681 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
24682 }
24683
24684 static void
24685 show_check_physname (struct ui_file *file, int from_tty,
24686 struct cmd_list_element *c, const char *value)
24687 {
24688 fprintf_filtered (file,
24689 _("Whether to check \"physname\" is %s.\n"),
24690 value);
24691 }
24692
24693 void _initialize_dwarf2_read ();
24694 void
24695 _initialize_dwarf2_read ()
24696 {
24697 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24698 Set DWARF specific variables.\n\
24699 Configure DWARF variables such as the cache size."),
24700 &set_dwarf_cmdlist, "maintenance set dwarf ",
24701 0/*allow-unknown*/, &maintenance_set_cmdlist);
24702
24703 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24704 Show DWARF specific variables.\n\
24705 Show DWARF variables such as the cache size."),
24706 &show_dwarf_cmdlist, "maintenance show dwarf ",
24707 0/*allow-unknown*/, &maintenance_show_cmdlist);
24708
24709 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24710 &dwarf_max_cache_age, _("\
24711 Set the upper bound on the age of cached DWARF compilation units."), _("\
24712 Show the upper bound on the age of cached DWARF compilation units."), _("\
24713 A higher limit means that cached compilation units will be stored\n\
24714 in memory longer, and more total memory will be used. Zero disables\n\
24715 caching, which can slow down startup."),
24716 NULL,
24717 show_dwarf_max_cache_age,
24718 &set_dwarf_cmdlist,
24719 &show_dwarf_cmdlist);
24720
24721 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24722 Set debugging of the DWARF reader."), _("\
24723 Show debugging of the DWARF reader."), _("\
24724 When enabled (non-zero), debugging messages are printed during DWARF\n\
24725 reading and symtab expansion. A value of 1 (one) provides basic\n\
24726 information. A value greater than 1 provides more verbose information."),
24727 NULL,
24728 NULL,
24729 &setdebuglist, &showdebuglist);
24730
24731 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24732 Set debugging of the DWARF DIE reader."), _("\
24733 Show debugging of the DWARF DIE reader."), _("\
24734 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24735 The value is the maximum depth to print."),
24736 NULL,
24737 NULL,
24738 &setdebuglist, &showdebuglist);
24739
24740 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24741 Set debugging of the dwarf line reader."), _("\
24742 Show debugging of the dwarf line reader."), _("\
24743 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24744 A value of 1 (one) provides basic information.\n\
24745 A value greater than 1 provides more verbose information."),
24746 NULL,
24747 NULL,
24748 &setdebuglist, &showdebuglist);
24749
24750 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24751 Set cross-checking of \"physname\" code against demangler."), _("\
24752 Show cross-checking of \"physname\" code against demangler."), _("\
24753 When enabled, GDB's internal \"physname\" code is checked against\n\
24754 the demangler."),
24755 NULL, show_check_physname,
24756 &setdebuglist, &showdebuglist);
24757
24758 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24759 no_class, &use_deprecated_index_sections, _("\
24760 Set whether to use deprecated gdb_index sections."), _("\
24761 Show whether to use deprecated gdb_index sections."), _("\
24762 When enabled, deprecated .gdb_index sections are used anyway.\n\
24763 Normally they are ignored either because of a missing feature or\n\
24764 performance issue.\n\
24765 Warning: This option must be enabled before gdb reads the file."),
24766 NULL,
24767 NULL,
24768 &setlist, &showlist);
24769
24770 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24771 &dwarf2_locexpr_funcs);
24772 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24773 &dwarf2_loclist_funcs);
24774
24775 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24776 &dwarf2_block_frame_base_locexpr_funcs);
24777 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24778 &dwarf2_block_frame_base_loclist_funcs);
24779
24780 #if GDB_SELF_TEST
24781 selftests::register_test ("dw2_expand_symtabs_matching",
24782 selftests::dw2_expand_symtabs_matching::run_test);
24783 selftests::register_test ("dwarf2_find_containing_comp_unit",
24784 selftests::find_containing_comp_unit::run_test);
24785 #endif
24786 }
This page took 0.501611 seconds and 5 git commands to generate.