Convert IS_TYPE_UNIT_GROUP to method
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "bfd.h"
41 #include "elf-bfd.h"
42 #include "symtab.h"
43 #include "gdbtypes.h"
44 #include "objfiles.h"
45 #include "dwarf2.h"
46 #include "buildsym.h"
47 #include "demangle.h"
48 #include "gdb-demangle.h"
49 #include "filenames.h" /* for DOSish file names */
50 #include "macrotab.h"
51 #include "language.h"
52 #include "complaints.h"
53 #include "dwarf2/expr.h"
54 #include "dwarf2/loc.h"
55 #include "cp-support.h"
56 #include "hashtab.h"
57 #include "command.h"
58 #include "gdbcmd.h"
59 #include "block.h"
60 #include "addrmap.h"
61 #include "typeprint.h"
62 #include "psympriv.h"
63 #include "c-lang.h"
64 #include "go-lang.h"
65 #include "valprint.h"
66 #include "gdbcore.h" /* for gnutarget */
67 #include "gdb/gdb-index.h"
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71 #include "build-id.h"
72 #include "namespace.h"
73 #include "gdbsupport/function-view.h"
74 #include "gdbsupport/gdb_optional.h"
75 #include "gdbsupport/underlying.h"
76 #include "gdbsupport/hash_enum.h"
77 #include "filename-seen-cache.h"
78 #include "producer.h"
79 #include <fcntl.h>
80 #include <algorithm>
81 #include <unordered_map>
82 #include "gdbsupport/selftest.h"
83 #include "rust-lang.h"
84 #include "gdbsupport/pathstuff.h"
85 #include "count-one-bits.h"
86
87 /* When == 1, print basic high level tracing messages.
88 When > 1, be more verbose.
89 This is in contrast to the low level DIE reading of dwarf_die_debug. */
90 static unsigned int dwarf_read_debug = 0;
91
92 /* When non-zero, dump DIEs after they are read in. */
93 static unsigned int dwarf_die_debug = 0;
94
95 /* When non-zero, dump line number entries as they are read in. */
96 unsigned int dwarf_line_debug = 0;
97
98 /* When true, cross-check physname against demangler. */
99 static bool check_physname = false;
100
101 /* When true, do not reject deprecated .gdb_index sections. */
102 static bool use_deprecated_index_sections = false;
103
104 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
105
106 /* The "aclass" indices for various kinds of computed DWARF symbols. */
107
108 static int dwarf2_locexpr_index;
109 static int dwarf2_loclist_index;
110 static int dwarf2_locexpr_block_index;
111 static int dwarf2_loclist_block_index;
112
113 /* An index into a (C++) symbol name component in a symbol name as
114 recorded in the mapped_index's symbol table. For each C++ symbol
115 in the symbol table, we record one entry for the start of each
116 component in the symbol in a table of name components, and then
117 sort the table, in order to be able to binary search symbol names,
118 ignoring leading namespaces, both completion and regular look up.
119 For example, for symbol "A::B::C", we'll have an entry that points
120 to "A::B::C", another that points to "B::C", and another for "C".
121 Note that function symbols in GDB index have no parameter
122 information, just the function/method names. You can convert a
123 name_component to a "const char *" using the
124 'mapped_index::symbol_name_at(offset_type)' method. */
125
126 struct name_component
127 {
128 /* Offset in the symbol name where the component starts. Stored as
129 a (32-bit) offset instead of a pointer to save memory and improve
130 locality on 64-bit architectures. */
131 offset_type name_offset;
132
133 /* The symbol's index in the symbol and constant pool tables of a
134 mapped_index. */
135 offset_type idx;
136 };
137
138 /* Base class containing bits shared by both .gdb_index and
139 .debug_name indexes. */
140
141 struct mapped_index_base
142 {
143 mapped_index_base () = default;
144 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
145
146 /* The name_component table (a sorted vector). See name_component's
147 description above. */
148 std::vector<name_component> name_components;
149
150 /* How NAME_COMPONENTS is sorted. */
151 enum case_sensitivity name_components_casing;
152
153 /* Return the number of names in the symbol table. */
154 virtual size_t symbol_name_count () const = 0;
155
156 /* Get the name of the symbol at IDX in the symbol table. */
157 virtual const char *symbol_name_at (offset_type idx) const = 0;
158
159 /* Return whether the name at IDX in the symbol table should be
160 ignored. */
161 virtual bool symbol_name_slot_invalid (offset_type idx) const
162 {
163 return false;
164 }
165
166 /* Build the symbol name component sorted vector, if we haven't
167 yet. */
168 void build_name_components ();
169
170 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
171 possible matches for LN_NO_PARAMS in the name component
172 vector. */
173 std::pair<std::vector<name_component>::const_iterator,
174 std::vector<name_component>::const_iterator>
175 find_name_components_bounds (const lookup_name_info &ln_no_params,
176 enum language lang) const;
177
178 /* Prevent deleting/destroying via a base class pointer. */
179 protected:
180 ~mapped_index_base() = default;
181 };
182
183 /* A description of the mapped index. The file format is described in
184 a comment by the code that writes the index. */
185 struct mapped_index final : public mapped_index_base
186 {
187 /* A slot/bucket in the symbol table hash. */
188 struct symbol_table_slot
189 {
190 const offset_type name;
191 const offset_type vec;
192 };
193
194 /* Index data format version. */
195 int version = 0;
196
197 /* The address table data. */
198 gdb::array_view<const gdb_byte> address_table;
199
200 /* The symbol table, implemented as a hash table. */
201 gdb::array_view<symbol_table_slot> symbol_table;
202
203 /* A pointer to the constant pool. */
204 const char *constant_pool = nullptr;
205
206 bool symbol_name_slot_invalid (offset_type idx) const override
207 {
208 const auto &bucket = this->symbol_table[idx];
209 return bucket.name == 0 && bucket.vec == 0;
210 }
211
212 /* Convenience method to get at the name of the symbol at IDX in the
213 symbol table. */
214 const char *symbol_name_at (offset_type idx) const override
215 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
216
217 size_t symbol_name_count () const override
218 { return this->symbol_table.size (); }
219 };
220
221 /* A description of the mapped .debug_names.
222 Uninitialized map has CU_COUNT 0. */
223 struct mapped_debug_names final : public mapped_index_base
224 {
225 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
226 : dwarf2_per_objfile (dwarf2_per_objfile_)
227 {}
228
229 struct dwarf2_per_objfile *dwarf2_per_objfile;
230 bfd_endian dwarf5_byte_order;
231 bool dwarf5_is_dwarf64;
232 bool augmentation_is_gdb;
233 uint8_t offset_size;
234 uint32_t cu_count = 0;
235 uint32_t tu_count, bucket_count, name_count;
236 const gdb_byte *cu_table_reordered, *tu_table_reordered;
237 const uint32_t *bucket_table_reordered, *hash_table_reordered;
238 const gdb_byte *name_table_string_offs_reordered;
239 const gdb_byte *name_table_entry_offs_reordered;
240 const gdb_byte *entry_pool;
241
242 struct index_val
243 {
244 ULONGEST dwarf_tag;
245 struct attr
246 {
247 /* Attribute name DW_IDX_*. */
248 ULONGEST dw_idx;
249
250 /* Attribute form DW_FORM_*. */
251 ULONGEST form;
252
253 /* Value if FORM is DW_FORM_implicit_const. */
254 LONGEST implicit_const;
255 };
256 std::vector<attr> attr_vec;
257 };
258
259 std::unordered_map<ULONGEST, index_val> abbrev_map;
260
261 const char *namei_to_name (uint32_t namei) const;
262
263 /* Implementation of the mapped_index_base virtual interface, for
264 the name_components cache. */
265
266 const char *symbol_name_at (offset_type idx) const override
267 { return namei_to_name (idx); }
268
269 size_t symbol_name_count () const override
270 { return this->name_count; }
271 };
272
273 /* See dwarf2read.h. */
274
275 dwarf2_per_objfile *
276 get_dwarf2_per_objfile (struct objfile *objfile)
277 {
278 return dwarf2_objfile_data_key.get (objfile);
279 }
280
281 /* Default names of the debugging sections. */
282
283 /* Note that if the debugging section has been compressed, it might
284 have a name like .zdebug_info. */
285
286 static const struct dwarf2_debug_sections dwarf2_elf_names =
287 {
288 { ".debug_info", ".zdebug_info" },
289 { ".debug_abbrev", ".zdebug_abbrev" },
290 { ".debug_line", ".zdebug_line" },
291 { ".debug_loc", ".zdebug_loc" },
292 { ".debug_loclists", ".zdebug_loclists" },
293 { ".debug_macinfo", ".zdebug_macinfo" },
294 { ".debug_macro", ".zdebug_macro" },
295 { ".debug_str", ".zdebug_str" },
296 { ".debug_str_offsets", ".zdebug_str_offsets" },
297 { ".debug_line_str", ".zdebug_line_str" },
298 { ".debug_ranges", ".zdebug_ranges" },
299 { ".debug_rnglists", ".zdebug_rnglists" },
300 { ".debug_types", ".zdebug_types" },
301 { ".debug_addr", ".zdebug_addr" },
302 { ".debug_frame", ".zdebug_frame" },
303 { ".eh_frame", NULL },
304 { ".gdb_index", ".zgdb_index" },
305 { ".debug_names", ".zdebug_names" },
306 { ".debug_aranges", ".zdebug_aranges" },
307 23
308 };
309
310 /* List of DWO/DWP sections. */
311
312 static const struct dwop_section_names
313 {
314 struct dwarf2_section_names abbrev_dwo;
315 struct dwarf2_section_names info_dwo;
316 struct dwarf2_section_names line_dwo;
317 struct dwarf2_section_names loc_dwo;
318 struct dwarf2_section_names loclists_dwo;
319 struct dwarf2_section_names macinfo_dwo;
320 struct dwarf2_section_names macro_dwo;
321 struct dwarf2_section_names str_dwo;
322 struct dwarf2_section_names str_offsets_dwo;
323 struct dwarf2_section_names types_dwo;
324 struct dwarf2_section_names cu_index;
325 struct dwarf2_section_names tu_index;
326 }
327 dwop_section_names =
328 {
329 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
330 { ".debug_info.dwo", ".zdebug_info.dwo" },
331 { ".debug_line.dwo", ".zdebug_line.dwo" },
332 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
333 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
334 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
335 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
336 { ".debug_str.dwo", ".zdebug_str.dwo" },
337 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
338 { ".debug_types.dwo", ".zdebug_types.dwo" },
339 { ".debug_cu_index", ".zdebug_cu_index" },
340 { ".debug_tu_index", ".zdebug_tu_index" },
341 };
342
343 /* local data types */
344
345 /* Type used for delaying computation of method physnames.
346 See comments for compute_delayed_physnames. */
347 struct delayed_method_info
348 {
349 /* The type to which the method is attached, i.e., its parent class. */
350 struct type *type;
351
352 /* The index of the method in the type's function fieldlists. */
353 int fnfield_index;
354
355 /* The index of the method in the fieldlist. */
356 int index;
357
358 /* The name of the DIE. */
359 const char *name;
360
361 /* The DIE associated with this method. */
362 struct die_info *die;
363 };
364
365 /* Internal state when decoding a particular compilation unit. */
366 struct dwarf2_cu
367 {
368 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
369 ~dwarf2_cu ();
370
371 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
372
373 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
374 Create the set of symtabs used by this TU, or if this TU is sharing
375 symtabs with another TU and the symtabs have already been created
376 then restore those symtabs in the line header.
377 We don't need the pc/line-number mapping for type units. */
378 void setup_type_unit_groups (struct die_info *die);
379
380 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
381 buildsym_compunit constructor. */
382 struct compunit_symtab *start_symtab (const char *name,
383 const char *comp_dir,
384 CORE_ADDR low_pc);
385
386 /* Reset the builder. */
387 void reset_builder () { m_builder.reset (); }
388
389 /* The header of the compilation unit. */
390 struct comp_unit_head header {};
391
392 /* Base address of this compilation unit. */
393 CORE_ADDR base_address = 0;
394
395 /* Non-zero if base_address has been set. */
396 int base_known = 0;
397
398 /* The language we are debugging. */
399 enum language language = language_unknown;
400 const struct language_defn *language_defn = nullptr;
401
402 const char *producer = nullptr;
403
404 private:
405 /* The symtab builder for this CU. This is only non-NULL when full
406 symbols are being read. */
407 std::unique_ptr<buildsym_compunit> m_builder;
408
409 public:
410 /* The generic symbol table building routines have separate lists for
411 file scope symbols and all all other scopes (local scopes). So
412 we need to select the right one to pass to add_symbol_to_list().
413 We do it by keeping a pointer to the correct list in list_in_scope.
414
415 FIXME: The original dwarf code just treated the file scope as the
416 first local scope, and all other local scopes as nested local
417 scopes, and worked fine. Check to see if we really need to
418 distinguish these in buildsym.c. */
419 struct pending **list_in_scope = nullptr;
420
421 /* Hash table holding all the loaded partial DIEs
422 with partial_die->offset.SECT_OFF as hash. */
423 htab_t partial_dies = nullptr;
424
425 /* Storage for things with the same lifetime as this read-in compilation
426 unit, including partial DIEs. */
427 auto_obstack comp_unit_obstack;
428
429 /* When multiple dwarf2_cu structures are living in memory, this field
430 chains them all together, so that they can be released efficiently.
431 We will probably also want a generation counter so that most-recently-used
432 compilation units are cached... */
433 struct dwarf2_per_cu_data *read_in_chain = nullptr;
434
435 /* Backlink to our per_cu entry. */
436 struct dwarf2_per_cu_data *per_cu;
437
438 /* How many compilation units ago was this CU last referenced? */
439 int last_used = 0;
440
441 /* A hash table of DIE cu_offset for following references with
442 die_info->offset.sect_off as hash. */
443 htab_t die_hash = nullptr;
444
445 /* Full DIEs if read in. */
446 struct die_info *dies = nullptr;
447
448 /* A set of pointers to dwarf2_per_cu_data objects for compilation
449 units referenced by this one. Only set during full symbol processing;
450 partial symbol tables do not have dependencies. */
451 htab_t dependencies = nullptr;
452
453 /* Header data from the line table, during full symbol processing. */
454 struct line_header *line_header = nullptr;
455 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
456 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
457 this is the DW_TAG_compile_unit die for this CU. We'll hold on
458 to the line header as long as this DIE is being processed. See
459 process_die_scope. */
460 die_info *line_header_die_owner = nullptr;
461
462 /* A list of methods which need to have physnames computed
463 after all type information has been read. */
464 std::vector<delayed_method_info> method_list;
465
466 /* To be copied to symtab->call_site_htab. */
467 htab_t call_site_htab = nullptr;
468
469 /* Non-NULL if this CU came from a DWO file.
470 There is an invariant here that is important to remember:
471 Except for attributes copied from the top level DIE in the "main"
472 (or "stub") file in preparation for reading the DWO file
473 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
474 Either there isn't a DWO file (in which case this is NULL and the point
475 is moot), or there is and either we're not going to read it (in which
476 case this is NULL) or there is and we are reading it (in which case this
477 is non-NULL). */
478 struct dwo_unit *dwo_unit = nullptr;
479
480 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
481 Note this value comes from the Fission stub CU/TU's DIE. */
482 gdb::optional<ULONGEST> addr_base;
483
484 /* The DW_AT_rnglists_base attribute if present.
485 Note this value comes from the Fission stub CU/TU's DIE.
486 Also note that the value is zero in the non-DWO case so this value can
487 be used without needing to know whether DWO files are in use or not.
488 N.B. This does not apply to DW_AT_ranges appearing in
489 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
490 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
491 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
492 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
493 ULONGEST ranges_base = 0;
494
495 /* When reading debug info generated by older versions of rustc, we
496 have to rewrite some union types to be struct types with a
497 variant part. This rewriting must be done after the CU is fully
498 read in, because otherwise at the point of rewriting some struct
499 type might not have been fully processed. So, we keep a list of
500 all such types here and process them after expansion. */
501 std::vector<struct type *> rust_unions;
502
503 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
504 files, the value is implicitly zero. For DWARF 5 version DWO files, the
505 value is often implicit and is the size of the header of
506 .debug_str_offsets section (8 or 4, depending on the address size). */
507 gdb::optional<ULONGEST> str_offsets_base;
508
509 /* Mark used when releasing cached dies. */
510 bool mark : 1;
511
512 /* This CU references .debug_loc. See the symtab->locations_valid field.
513 This test is imperfect as there may exist optimized debug code not using
514 any location list and still facing inlining issues if handled as
515 unoptimized code. For a future better test see GCC PR other/32998. */
516 bool has_loclist : 1;
517
518 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
519 if all the producer_is_* fields are valid. This information is cached
520 because profiling CU expansion showed excessive time spent in
521 producer_is_gxx_lt_4_6. */
522 bool checked_producer : 1;
523 bool producer_is_gxx_lt_4_6 : 1;
524 bool producer_is_gcc_lt_4_3 : 1;
525 bool producer_is_icc : 1;
526 bool producer_is_icc_lt_14 : 1;
527 bool producer_is_codewarrior : 1;
528
529 /* When true, the file that we're processing is known to have
530 debugging info for C++ namespaces. GCC 3.3.x did not produce
531 this information, but later versions do. */
532
533 bool processing_has_namespace_info : 1;
534
535 struct partial_die_info *find_partial_die (sect_offset sect_off);
536
537 /* If this CU was inherited by another CU (via specification,
538 abstract_origin, etc), this is the ancestor CU. */
539 dwarf2_cu *ancestor;
540
541 /* Get the buildsym_compunit for this CU. */
542 buildsym_compunit *get_builder ()
543 {
544 /* If this CU has a builder associated with it, use that. */
545 if (m_builder != nullptr)
546 return m_builder.get ();
547
548 /* Otherwise, search ancestors for a valid builder. */
549 if (ancestor != nullptr)
550 return ancestor->get_builder ();
551
552 return nullptr;
553 }
554 };
555
556 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
557 This includes type_unit_group and quick_file_names. */
558
559 struct stmt_list_hash
560 {
561 /* The DWO unit this table is from or NULL if there is none. */
562 struct dwo_unit *dwo_unit;
563
564 /* Offset in .debug_line or .debug_line.dwo. */
565 sect_offset line_sect_off;
566 };
567
568 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
569 an object of this type. */
570
571 struct type_unit_group
572 {
573 /* dwarf2read.c's main "handle" on a TU symtab.
574 To simplify things we create an artificial CU that "includes" all the
575 type units using this stmt_list so that the rest of the code still has
576 a "per_cu" handle on the symtab. */
577 struct dwarf2_per_cu_data per_cu;
578
579 /* The TUs that share this DW_AT_stmt_list entry.
580 This is added to while parsing type units to build partial symtabs,
581 and is deleted afterwards and not used again. */
582 std::vector<signatured_type *> *tus;
583
584 /* The compunit symtab.
585 Type units in a group needn't all be defined in the same source file,
586 so we create an essentially anonymous symtab as the compunit symtab. */
587 struct compunit_symtab *compunit_symtab;
588
589 /* The data used to construct the hash key. */
590 struct stmt_list_hash hash;
591
592 /* The number of symtabs from the line header.
593 The value here must match line_header.num_file_names. */
594 unsigned int num_symtabs;
595
596 /* The symbol tables for this TU (obtained from the files listed in
597 DW_AT_stmt_list).
598 WARNING: The order of entries here must match the order of entries
599 in the line header. After the first TU using this type_unit_group, the
600 line header for the subsequent TUs is recreated from this. This is done
601 because we need to use the same symtabs for each TU using the same
602 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
603 there's no guarantee the line header doesn't have duplicate entries. */
604 struct symtab **symtabs;
605 };
606
607 /* These sections are what may appear in a (real or virtual) DWO file. */
608
609 struct dwo_sections
610 {
611 struct dwarf2_section_info abbrev;
612 struct dwarf2_section_info line;
613 struct dwarf2_section_info loc;
614 struct dwarf2_section_info loclists;
615 struct dwarf2_section_info macinfo;
616 struct dwarf2_section_info macro;
617 struct dwarf2_section_info str;
618 struct dwarf2_section_info str_offsets;
619 /* In the case of a virtual DWO file, these two are unused. */
620 struct dwarf2_section_info info;
621 std::vector<dwarf2_section_info> types;
622 };
623
624 /* CUs/TUs in DWP/DWO files. */
625
626 struct dwo_unit
627 {
628 /* Backlink to the containing struct dwo_file. */
629 struct dwo_file *dwo_file;
630
631 /* The "id" that distinguishes this CU/TU.
632 .debug_info calls this "dwo_id", .debug_types calls this "signature".
633 Since signatures came first, we stick with it for consistency. */
634 ULONGEST signature;
635
636 /* The section this CU/TU lives in, in the DWO file. */
637 struct dwarf2_section_info *section;
638
639 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
640 sect_offset sect_off;
641 unsigned int length;
642
643 /* For types, offset in the type's DIE of the type defined by this TU. */
644 cu_offset type_offset_in_tu;
645 };
646
647 /* include/dwarf2.h defines the DWP section codes.
648 It defines a max value but it doesn't define a min value, which we
649 use for error checking, so provide one. */
650
651 enum dwp_v2_section_ids
652 {
653 DW_SECT_MIN = 1
654 };
655
656 /* Data for one DWO file.
657
658 This includes virtual DWO files (a virtual DWO file is a DWO file as it
659 appears in a DWP file). DWP files don't really have DWO files per se -
660 comdat folding of types "loses" the DWO file they came from, and from
661 a high level view DWP files appear to contain a mass of random types.
662 However, to maintain consistency with the non-DWP case we pretend DWP
663 files contain virtual DWO files, and we assign each TU with one virtual
664 DWO file (generally based on the line and abbrev section offsets -
665 a heuristic that seems to work in practice). */
666
667 struct dwo_file
668 {
669 dwo_file () = default;
670 DISABLE_COPY_AND_ASSIGN (dwo_file);
671
672 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
673 For virtual DWO files the name is constructed from the section offsets
674 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
675 from related CU+TUs. */
676 const char *dwo_name = nullptr;
677
678 /* The DW_AT_comp_dir attribute. */
679 const char *comp_dir = nullptr;
680
681 /* The bfd, when the file is open. Otherwise this is NULL.
682 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
683 gdb_bfd_ref_ptr dbfd;
684
685 /* The sections that make up this DWO file.
686 Remember that for virtual DWO files in DWP V2, these are virtual
687 sections (for lack of a better name). */
688 struct dwo_sections sections {};
689
690 /* The CUs in the file.
691 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
692 an extension to handle LLVM's Link Time Optimization output (where
693 multiple source files may be compiled into a single object/dwo pair). */
694 htab_up cus;
695
696 /* Table of TUs in the file.
697 Each element is a struct dwo_unit. */
698 htab_up tus;
699 };
700
701 /* These sections are what may appear in a DWP file. */
702
703 struct dwp_sections
704 {
705 /* These are used by both DWP version 1 and 2. */
706 struct dwarf2_section_info str;
707 struct dwarf2_section_info cu_index;
708 struct dwarf2_section_info tu_index;
709
710 /* These are only used by DWP version 2 files.
711 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
712 sections are referenced by section number, and are not recorded here.
713 In DWP version 2 there is at most one copy of all these sections, each
714 section being (effectively) comprised of the concatenation of all of the
715 individual sections that exist in the version 1 format.
716 To keep the code simple we treat each of these concatenated pieces as a
717 section itself (a virtual section?). */
718 struct dwarf2_section_info abbrev;
719 struct dwarf2_section_info info;
720 struct dwarf2_section_info line;
721 struct dwarf2_section_info loc;
722 struct dwarf2_section_info macinfo;
723 struct dwarf2_section_info macro;
724 struct dwarf2_section_info str_offsets;
725 struct dwarf2_section_info types;
726 };
727
728 /* These sections are what may appear in a virtual DWO file in DWP version 1.
729 A virtual DWO file is a DWO file as it appears in a DWP file. */
730
731 struct virtual_v1_dwo_sections
732 {
733 struct dwarf2_section_info abbrev;
734 struct dwarf2_section_info line;
735 struct dwarf2_section_info loc;
736 struct dwarf2_section_info macinfo;
737 struct dwarf2_section_info macro;
738 struct dwarf2_section_info str_offsets;
739 /* Each DWP hash table entry records one CU or one TU.
740 That is recorded here, and copied to dwo_unit.section. */
741 struct dwarf2_section_info info_or_types;
742 };
743
744 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
745 In version 2, the sections of the DWO files are concatenated together
746 and stored in one section of that name. Thus each ELF section contains
747 several "virtual" sections. */
748
749 struct virtual_v2_dwo_sections
750 {
751 bfd_size_type abbrev_offset;
752 bfd_size_type abbrev_size;
753
754 bfd_size_type line_offset;
755 bfd_size_type line_size;
756
757 bfd_size_type loc_offset;
758 bfd_size_type loc_size;
759
760 bfd_size_type macinfo_offset;
761 bfd_size_type macinfo_size;
762
763 bfd_size_type macro_offset;
764 bfd_size_type macro_size;
765
766 bfd_size_type str_offsets_offset;
767 bfd_size_type str_offsets_size;
768
769 /* Each DWP hash table entry records one CU or one TU.
770 That is recorded here, and copied to dwo_unit.section. */
771 bfd_size_type info_or_types_offset;
772 bfd_size_type info_or_types_size;
773 };
774
775 /* Contents of DWP hash tables. */
776
777 struct dwp_hash_table
778 {
779 uint32_t version, nr_columns;
780 uint32_t nr_units, nr_slots;
781 const gdb_byte *hash_table, *unit_table;
782 union
783 {
784 struct
785 {
786 const gdb_byte *indices;
787 } v1;
788 struct
789 {
790 /* This is indexed by column number and gives the id of the section
791 in that column. */
792 #define MAX_NR_V2_DWO_SECTIONS \
793 (1 /* .debug_info or .debug_types */ \
794 + 1 /* .debug_abbrev */ \
795 + 1 /* .debug_line */ \
796 + 1 /* .debug_loc */ \
797 + 1 /* .debug_str_offsets */ \
798 + 1 /* .debug_macro or .debug_macinfo */)
799 int section_ids[MAX_NR_V2_DWO_SECTIONS];
800 const gdb_byte *offsets;
801 const gdb_byte *sizes;
802 } v2;
803 } section_pool;
804 };
805
806 /* Data for one DWP file. */
807
808 struct dwp_file
809 {
810 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
811 : name (name_),
812 dbfd (std::move (abfd))
813 {
814 }
815
816 /* Name of the file. */
817 const char *name;
818
819 /* File format version. */
820 int version = 0;
821
822 /* The bfd. */
823 gdb_bfd_ref_ptr dbfd;
824
825 /* Section info for this file. */
826 struct dwp_sections sections {};
827
828 /* Table of CUs in the file. */
829 const struct dwp_hash_table *cus = nullptr;
830
831 /* Table of TUs in the file. */
832 const struct dwp_hash_table *tus = nullptr;
833
834 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
835 htab_up loaded_cus;
836 htab_up loaded_tus;
837
838 /* Table to map ELF section numbers to their sections.
839 This is only needed for the DWP V1 file format. */
840 unsigned int num_sections = 0;
841 asection **elf_sections = nullptr;
842 };
843
844 /* Struct used to pass misc. parameters to read_die_and_children, et
845 al. which are used for both .debug_info and .debug_types dies.
846 All parameters here are unchanging for the life of the call. This
847 struct exists to abstract away the constant parameters of die reading. */
848
849 struct die_reader_specs
850 {
851 /* The bfd of die_section. */
852 bfd* abfd;
853
854 /* The CU of the DIE we are parsing. */
855 struct dwarf2_cu *cu;
856
857 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
858 struct dwo_file *dwo_file;
859
860 /* The section the die comes from.
861 This is either .debug_info or .debug_types, or the .dwo variants. */
862 struct dwarf2_section_info *die_section;
863
864 /* die_section->buffer. */
865 const gdb_byte *buffer;
866
867 /* The end of the buffer. */
868 const gdb_byte *buffer_end;
869
870 /* The abbreviation table to use when reading the DIEs. */
871 struct abbrev_table *abbrev_table;
872 };
873
874 /* A subclass of die_reader_specs that holds storage and has complex
875 constructor and destructor behavior. */
876
877 class cutu_reader : public die_reader_specs
878 {
879 public:
880
881 cutu_reader (struct dwarf2_per_cu_data *this_cu,
882 struct abbrev_table *abbrev_table,
883 int use_existing_cu,
884 bool skip_partial);
885
886 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
887 struct dwarf2_cu *parent_cu = nullptr,
888 struct dwo_file *dwo_file = nullptr);
889
890 DISABLE_COPY_AND_ASSIGN (cutu_reader);
891
892 const gdb_byte *info_ptr = nullptr;
893 struct die_info *comp_unit_die = nullptr;
894 bool dummy_p = false;
895
896 /* Release the new CU, putting it on the chain. This cannot be done
897 for dummy CUs. */
898 void keep ();
899
900 private:
901 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
902 int use_existing_cu);
903
904 struct dwarf2_per_cu_data *m_this_cu;
905 std::unique_ptr<dwarf2_cu> m_new_cu;
906
907 /* The ordinary abbreviation table. */
908 abbrev_table_up m_abbrev_table_holder;
909
910 /* The DWO abbreviation table. */
911 abbrev_table_up m_dwo_abbrev_table;
912 };
913
914 /* When we construct a partial symbol table entry we only
915 need this much information. */
916 struct partial_die_info : public allocate_on_obstack
917 {
918 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
919
920 /* Disable assign but still keep copy ctor, which is needed
921 load_partial_dies. */
922 partial_die_info& operator=(const partial_die_info& rhs) = delete;
923
924 /* Adjust the partial die before generating a symbol for it. This
925 function may set the is_external flag or change the DIE's
926 name. */
927 void fixup (struct dwarf2_cu *cu);
928
929 /* Read a minimal amount of information into the minimal die
930 structure. */
931 const gdb_byte *read (const struct die_reader_specs *reader,
932 const struct abbrev_info &abbrev,
933 const gdb_byte *info_ptr);
934
935 /* Offset of this DIE. */
936 const sect_offset sect_off;
937
938 /* DWARF-2 tag for this DIE. */
939 const ENUM_BITFIELD(dwarf_tag) tag : 16;
940
941 /* Assorted flags describing the data found in this DIE. */
942 const unsigned int has_children : 1;
943
944 unsigned int is_external : 1;
945 unsigned int is_declaration : 1;
946 unsigned int has_type : 1;
947 unsigned int has_specification : 1;
948 unsigned int has_pc_info : 1;
949 unsigned int may_be_inlined : 1;
950
951 /* This DIE has been marked DW_AT_main_subprogram. */
952 unsigned int main_subprogram : 1;
953
954 /* Flag set if the SCOPE field of this structure has been
955 computed. */
956 unsigned int scope_set : 1;
957
958 /* Flag set if the DIE has a byte_size attribute. */
959 unsigned int has_byte_size : 1;
960
961 /* Flag set if the DIE has a DW_AT_const_value attribute. */
962 unsigned int has_const_value : 1;
963
964 /* Flag set if any of the DIE's children are template arguments. */
965 unsigned int has_template_arguments : 1;
966
967 /* Flag set if fixup has been called on this die. */
968 unsigned int fixup_called : 1;
969
970 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
971 unsigned int is_dwz : 1;
972
973 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
974 unsigned int spec_is_dwz : 1;
975
976 /* The name of this DIE. Normally the value of DW_AT_name, but
977 sometimes a default name for unnamed DIEs. */
978 const char *name = nullptr;
979
980 /* The linkage name, if present. */
981 const char *linkage_name = nullptr;
982
983 /* The scope to prepend to our children. This is generally
984 allocated on the comp_unit_obstack, so will disappear
985 when this compilation unit leaves the cache. */
986 const char *scope = nullptr;
987
988 /* Some data associated with the partial DIE. The tag determines
989 which field is live. */
990 union
991 {
992 /* The location description associated with this DIE, if any. */
993 struct dwarf_block *locdesc;
994 /* The offset of an import, for DW_TAG_imported_unit. */
995 sect_offset sect_off;
996 } d {};
997
998 /* If HAS_PC_INFO, the PC range associated with this DIE. */
999 CORE_ADDR lowpc = 0;
1000 CORE_ADDR highpc = 0;
1001
1002 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1003 DW_AT_sibling, if any. */
1004 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1005 could return DW_AT_sibling values to its caller load_partial_dies. */
1006 const gdb_byte *sibling = nullptr;
1007
1008 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1009 DW_AT_specification (or DW_AT_abstract_origin or
1010 DW_AT_extension). */
1011 sect_offset spec_offset {};
1012
1013 /* Pointers to this DIE's parent, first child, and next sibling,
1014 if any. */
1015 struct partial_die_info *die_parent = nullptr;
1016 struct partial_die_info *die_child = nullptr;
1017 struct partial_die_info *die_sibling = nullptr;
1018
1019 friend struct partial_die_info *
1020 dwarf2_cu::find_partial_die (sect_offset sect_off);
1021
1022 private:
1023 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1024 partial_die_info (sect_offset sect_off)
1025 : partial_die_info (sect_off, DW_TAG_padding, 0)
1026 {
1027 }
1028
1029 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1030 int has_children_)
1031 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1032 {
1033 is_external = 0;
1034 is_declaration = 0;
1035 has_type = 0;
1036 has_specification = 0;
1037 has_pc_info = 0;
1038 may_be_inlined = 0;
1039 main_subprogram = 0;
1040 scope_set = 0;
1041 has_byte_size = 0;
1042 has_const_value = 0;
1043 has_template_arguments = 0;
1044 fixup_called = 0;
1045 is_dwz = 0;
1046 spec_is_dwz = 0;
1047 }
1048 };
1049
1050 /* This data structure holds a complete die structure. */
1051 struct die_info
1052 {
1053 /* DWARF-2 tag for this DIE. */
1054 ENUM_BITFIELD(dwarf_tag) tag : 16;
1055
1056 /* Number of attributes */
1057 unsigned char num_attrs;
1058
1059 /* True if we're presently building the full type name for the
1060 type derived from this DIE. */
1061 unsigned char building_fullname : 1;
1062
1063 /* True if this die is in process. PR 16581. */
1064 unsigned char in_process : 1;
1065
1066 /* True if this DIE has children. */
1067 unsigned char has_children : 1;
1068
1069 /* Abbrev number */
1070 unsigned int abbrev;
1071
1072 /* Offset in .debug_info or .debug_types section. */
1073 sect_offset sect_off;
1074
1075 /* The dies in a compilation unit form an n-ary tree. PARENT
1076 points to this die's parent; CHILD points to the first child of
1077 this node; and all the children of a given node are chained
1078 together via their SIBLING fields. */
1079 struct die_info *child; /* Its first child, if any. */
1080 struct die_info *sibling; /* Its next sibling, if any. */
1081 struct die_info *parent; /* Its parent, if any. */
1082
1083 /* An array of attributes, with NUM_ATTRS elements. There may be
1084 zero, but it's not common and zero-sized arrays are not
1085 sufficiently portable C. */
1086 struct attribute attrs[1];
1087 };
1088
1089 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1090 but this would require a corresponding change in unpack_field_as_long
1091 and friends. */
1092 static int bits_per_byte = 8;
1093
1094 /* When reading a variant or variant part, we track a bit more
1095 information about the field, and store it in an object of this
1096 type. */
1097
1098 struct variant_field
1099 {
1100 /* If we see a DW_TAG_variant, then this will be the discriminant
1101 value. */
1102 ULONGEST discriminant_value;
1103 /* If we see a DW_TAG_variant, then this will be set if this is the
1104 default branch. */
1105 bool default_branch;
1106 /* While reading a DW_TAG_variant_part, this will be set if this
1107 field is the discriminant. */
1108 bool is_discriminant;
1109 };
1110
1111 struct nextfield
1112 {
1113 int accessibility = 0;
1114 int virtuality = 0;
1115 /* Extra information to describe a variant or variant part. */
1116 struct variant_field variant {};
1117 struct field field {};
1118 };
1119
1120 struct fnfieldlist
1121 {
1122 const char *name = nullptr;
1123 std::vector<struct fn_field> fnfields;
1124 };
1125
1126 /* The routines that read and process dies for a C struct or C++ class
1127 pass lists of data member fields and lists of member function fields
1128 in an instance of a field_info structure, as defined below. */
1129 struct field_info
1130 {
1131 /* List of data member and baseclasses fields. */
1132 std::vector<struct nextfield> fields;
1133 std::vector<struct nextfield> baseclasses;
1134
1135 /* Number of fields (including baseclasses). */
1136 int nfields = 0;
1137
1138 /* Set if the accessibility of one of the fields is not public. */
1139 int non_public_fields = 0;
1140
1141 /* Member function fieldlist array, contains name of possibly overloaded
1142 member function, number of overloaded member functions and a pointer
1143 to the head of the member function field chain. */
1144 std::vector<struct fnfieldlist> fnfieldlists;
1145
1146 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1147 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1148 std::vector<struct decl_field> typedef_field_list;
1149
1150 /* Nested types defined by this class and the number of elements in this
1151 list. */
1152 std::vector<struct decl_field> nested_types_list;
1153 };
1154
1155 /* Loaded secondary compilation units are kept in memory until they
1156 have not been referenced for the processing of this many
1157 compilation units. Set this to zero to disable caching. Cache
1158 sizes of up to at least twenty will improve startup time for
1159 typical inter-CU-reference binaries, at an obvious memory cost. */
1160 static int dwarf_max_cache_age = 5;
1161 static void
1162 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1163 struct cmd_list_element *c, const char *value)
1164 {
1165 fprintf_filtered (file, _("The upper bound on the age of cached "
1166 "DWARF compilation units is %s.\n"),
1167 value);
1168 }
1169 \f
1170 /* local function prototypes */
1171
1172 static void dwarf2_find_base_address (struct die_info *die,
1173 struct dwarf2_cu *cu);
1174
1175 static dwarf2_psymtab *create_partial_symtab
1176 (struct dwarf2_per_cu_data *per_cu, const char *name);
1177
1178 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1179 const gdb_byte *info_ptr,
1180 struct die_info *type_unit_die);
1181
1182 static void dwarf2_build_psymtabs_hard
1183 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1184
1185 static void scan_partial_symbols (struct partial_die_info *,
1186 CORE_ADDR *, CORE_ADDR *,
1187 int, struct dwarf2_cu *);
1188
1189 static void add_partial_symbol (struct partial_die_info *,
1190 struct dwarf2_cu *);
1191
1192 static void add_partial_namespace (struct partial_die_info *pdi,
1193 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1194 int set_addrmap, struct dwarf2_cu *cu);
1195
1196 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1197 CORE_ADDR *highpc, int set_addrmap,
1198 struct dwarf2_cu *cu);
1199
1200 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1201 struct dwarf2_cu *cu);
1202
1203 static void add_partial_subprogram (struct partial_die_info *pdi,
1204 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1205 int need_pc, struct dwarf2_cu *cu);
1206
1207 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1208
1209 static struct partial_die_info *load_partial_dies
1210 (const struct die_reader_specs *, const gdb_byte *, int);
1211
1212 /* A pair of partial_die_info and compilation unit. */
1213 struct cu_partial_die_info
1214 {
1215 /* The compilation unit of the partial_die_info. */
1216 struct dwarf2_cu *cu;
1217 /* A partial_die_info. */
1218 struct partial_die_info *pdi;
1219
1220 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1221 : cu (cu),
1222 pdi (pdi)
1223 { /* Nothing. */ }
1224
1225 private:
1226 cu_partial_die_info () = delete;
1227 };
1228
1229 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1230 struct dwarf2_cu *);
1231
1232 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1233 struct attribute *, struct attr_abbrev *,
1234 const gdb_byte *, bool *need_reprocess);
1235
1236 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1237 struct attribute *attr);
1238
1239 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1240
1241 static LONGEST read_checked_initial_length_and_offset
1242 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1243 unsigned int *, unsigned int *);
1244
1245 static sect_offset read_abbrev_offset
1246 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1247 struct dwarf2_section_info *, sect_offset);
1248
1249 static const char *read_indirect_string
1250 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1251 const struct comp_unit_head *, unsigned int *);
1252
1253 static const char *read_indirect_line_string
1254 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1255 const struct comp_unit_head *, unsigned int *);
1256
1257 static const char *read_indirect_string_at_offset
1258 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1259 LONGEST str_offset);
1260
1261 static const char *read_indirect_string_from_dwz
1262 (struct objfile *objfile, struct dwz_file *, LONGEST);
1263
1264 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1265 const gdb_byte *,
1266 unsigned int *);
1267
1268 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1269 ULONGEST str_index);
1270
1271 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1272 ULONGEST str_index);
1273
1274 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1275
1276 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1277 struct dwarf2_cu *);
1278
1279 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1280 unsigned int);
1281
1282 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1283 struct dwarf2_cu *cu);
1284
1285 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1286
1287 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1288 struct dwarf2_cu *cu);
1289
1290 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1291
1292 static struct die_info *die_specification (struct die_info *die,
1293 struct dwarf2_cu **);
1294
1295 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1296 struct dwarf2_cu *cu);
1297
1298 static void dwarf_decode_lines (struct line_header *, const char *,
1299 struct dwarf2_cu *, dwarf2_psymtab *,
1300 CORE_ADDR, int decode_mapping);
1301
1302 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1303 const char *);
1304
1305 static struct symbol *new_symbol (struct die_info *, struct type *,
1306 struct dwarf2_cu *, struct symbol * = NULL);
1307
1308 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1309 struct dwarf2_cu *);
1310
1311 static void dwarf2_const_value_attr (const struct attribute *attr,
1312 struct type *type,
1313 const char *name,
1314 struct obstack *obstack,
1315 struct dwarf2_cu *cu, LONGEST *value,
1316 const gdb_byte **bytes,
1317 struct dwarf2_locexpr_baton **baton);
1318
1319 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1320
1321 static int need_gnat_info (struct dwarf2_cu *);
1322
1323 static struct type *die_descriptive_type (struct die_info *,
1324 struct dwarf2_cu *);
1325
1326 static void set_descriptive_type (struct type *, struct die_info *,
1327 struct dwarf2_cu *);
1328
1329 static struct type *die_containing_type (struct die_info *,
1330 struct dwarf2_cu *);
1331
1332 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1333 struct dwarf2_cu *);
1334
1335 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1336
1337 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1338
1339 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1340
1341 static char *typename_concat (struct obstack *obs, const char *prefix,
1342 const char *suffix, int physname,
1343 struct dwarf2_cu *cu);
1344
1345 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1346
1347 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1348
1349 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1350
1351 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1352
1353 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1354
1355 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1356
1357 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1358 struct dwarf2_cu *, dwarf2_psymtab *);
1359
1360 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1361 values. Keep the items ordered with increasing constraints compliance. */
1362 enum pc_bounds_kind
1363 {
1364 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1365 PC_BOUNDS_NOT_PRESENT,
1366
1367 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1368 were present but they do not form a valid range of PC addresses. */
1369 PC_BOUNDS_INVALID,
1370
1371 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1372 PC_BOUNDS_RANGES,
1373
1374 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1375 PC_BOUNDS_HIGH_LOW,
1376 };
1377
1378 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1379 CORE_ADDR *, CORE_ADDR *,
1380 struct dwarf2_cu *,
1381 dwarf2_psymtab *);
1382
1383 static void get_scope_pc_bounds (struct die_info *,
1384 CORE_ADDR *, CORE_ADDR *,
1385 struct dwarf2_cu *);
1386
1387 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1388 CORE_ADDR, struct dwarf2_cu *);
1389
1390 static void dwarf2_add_field (struct field_info *, struct die_info *,
1391 struct dwarf2_cu *);
1392
1393 static void dwarf2_attach_fields_to_type (struct field_info *,
1394 struct type *, struct dwarf2_cu *);
1395
1396 static void dwarf2_add_member_fn (struct field_info *,
1397 struct die_info *, struct type *,
1398 struct dwarf2_cu *);
1399
1400 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1401 struct type *,
1402 struct dwarf2_cu *);
1403
1404 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1405
1406 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1407
1408 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1409
1410 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1411
1412 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1413
1414 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1415
1416 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1417
1418 static struct type *read_module_type (struct die_info *die,
1419 struct dwarf2_cu *cu);
1420
1421 static const char *namespace_name (struct die_info *die,
1422 int *is_anonymous, struct dwarf2_cu *);
1423
1424 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1425
1426 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1427
1428 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1429 struct dwarf2_cu *);
1430
1431 static struct die_info *read_die_and_siblings_1
1432 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1433 struct die_info *);
1434
1435 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1436 const gdb_byte *info_ptr,
1437 const gdb_byte **new_info_ptr,
1438 struct die_info *parent);
1439
1440 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1441 struct die_info **, const gdb_byte *,
1442 int);
1443
1444 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1445 struct die_info **, const gdb_byte *);
1446
1447 static void process_die (struct die_info *, struct dwarf2_cu *);
1448
1449 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1450 struct obstack *);
1451
1452 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1453
1454 static const char *dwarf2_full_name (const char *name,
1455 struct die_info *die,
1456 struct dwarf2_cu *cu);
1457
1458 static const char *dwarf2_physname (const char *name, struct die_info *die,
1459 struct dwarf2_cu *cu);
1460
1461 static struct die_info *dwarf2_extension (struct die_info *die,
1462 struct dwarf2_cu **);
1463
1464 static const char *dwarf_tag_name (unsigned int);
1465
1466 static const char *dwarf_attr_name (unsigned int);
1467
1468 static const char *dwarf_form_name (unsigned int);
1469
1470 static const char *dwarf_bool_name (unsigned int);
1471
1472 static const char *dwarf_type_encoding_name (unsigned int);
1473
1474 static struct die_info *sibling_die (struct die_info *);
1475
1476 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1477
1478 static void dump_die_for_error (struct die_info *);
1479
1480 static void dump_die_1 (struct ui_file *, int level, int max_level,
1481 struct die_info *);
1482
1483 /*static*/ void dump_die (struct die_info *, int max_level);
1484
1485 static void store_in_ref_table (struct die_info *,
1486 struct dwarf2_cu *);
1487
1488 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1489
1490 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1491
1492 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1493 const struct attribute *,
1494 struct dwarf2_cu **);
1495
1496 static struct die_info *follow_die_ref (struct die_info *,
1497 const struct attribute *,
1498 struct dwarf2_cu **);
1499
1500 static struct die_info *follow_die_sig (struct die_info *,
1501 const struct attribute *,
1502 struct dwarf2_cu **);
1503
1504 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1505 struct dwarf2_cu *);
1506
1507 static struct type *get_DW_AT_signature_type (struct die_info *,
1508 const struct attribute *,
1509 struct dwarf2_cu *);
1510
1511 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1512
1513 static void read_signatured_type (struct signatured_type *);
1514
1515 static int attr_to_dynamic_prop (const struct attribute *attr,
1516 struct die_info *die, struct dwarf2_cu *cu,
1517 struct dynamic_prop *prop, struct type *type);
1518
1519 /* memory allocation interface */
1520
1521 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1522
1523 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1524
1525 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1526
1527 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1528 struct dwarf2_loclist_baton *baton,
1529 const struct attribute *attr);
1530
1531 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1532 struct symbol *sym,
1533 struct dwarf2_cu *cu,
1534 int is_block);
1535
1536 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1537 const gdb_byte *info_ptr,
1538 struct abbrev_info *abbrev);
1539
1540 static hashval_t partial_die_hash (const void *item);
1541
1542 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1543
1544 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1545 (sect_offset sect_off, unsigned int offset_in_dwz,
1546 struct dwarf2_per_objfile *dwarf2_per_objfile);
1547
1548 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1549 struct die_info *comp_unit_die,
1550 enum language pretend_language);
1551
1552 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1553
1554 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1555
1556 static struct type *set_die_type (struct die_info *, struct type *,
1557 struct dwarf2_cu *);
1558
1559 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1560
1561 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1562
1563 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1564 enum language);
1565
1566 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1567 enum language);
1568
1569 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1570 enum language);
1571
1572 static void dwarf2_add_dependence (struct dwarf2_cu *,
1573 struct dwarf2_per_cu_data *);
1574
1575 static void dwarf2_mark (struct dwarf2_cu *);
1576
1577 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1578
1579 static struct type *get_die_type_at_offset (sect_offset,
1580 struct dwarf2_per_cu_data *);
1581
1582 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1583
1584 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1585 enum language pretend_language);
1586
1587 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1588
1589 /* Class, the destructor of which frees all allocated queue entries. This
1590 will only have work to do if an error was thrown while processing the
1591 dwarf. If no error was thrown then the queue entries should have all
1592 been processed, and freed, as we went along. */
1593
1594 class dwarf2_queue_guard
1595 {
1596 public:
1597 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1598 : m_per_objfile (per_objfile)
1599 {
1600 }
1601
1602 /* Free any entries remaining on the queue. There should only be
1603 entries left if we hit an error while processing the dwarf. */
1604 ~dwarf2_queue_guard ()
1605 {
1606 /* Ensure that no memory is allocated by the queue. */
1607 std::queue<dwarf2_queue_item> empty;
1608 std::swap (m_per_objfile->queue, empty);
1609 }
1610
1611 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1612
1613 private:
1614 dwarf2_per_objfile *m_per_objfile;
1615 };
1616
1617 dwarf2_queue_item::~dwarf2_queue_item ()
1618 {
1619 /* Anything still marked queued is likely to be in an
1620 inconsistent state, so discard it. */
1621 if (per_cu->queued)
1622 {
1623 if (per_cu->cu != NULL)
1624 free_one_cached_comp_unit (per_cu);
1625 per_cu->queued = 0;
1626 }
1627 }
1628
1629 /* The return type of find_file_and_directory. Note, the enclosed
1630 string pointers are only valid while this object is valid. */
1631
1632 struct file_and_directory
1633 {
1634 /* The filename. This is never NULL. */
1635 const char *name;
1636
1637 /* The compilation directory. NULL if not known. If we needed to
1638 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1639 points directly to the DW_AT_comp_dir string attribute owned by
1640 the obstack that owns the DIE. */
1641 const char *comp_dir;
1642
1643 /* If we needed to build a new string for comp_dir, this is what
1644 owns the storage. */
1645 std::string comp_dir_storage;
1646 };
1647
1648 static file_and_directory find_file_and_directory (struct die_info *die,
1649 struct dwarf2_cu *cu);
1650
1651 static htab_up allocate_signatured_type_table ();
1652
1653 static htab_up allocate_dwo_unit_table ();
1654
1655 static struct dwo_unit *lookup_dwo_unit_in_dwp
1656 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1657 struct dwp_file *dwp_file, const char *comp_dir,
1658 ULONGEST signature, int is_debug_types);
1659
1660 static struct dwp_file *get_dwp_file
1661 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1662
1663 static struct dwo_unit *lookup_dwo_comp_unit
1664 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1665
1666 static struct dwo_unit *lookup_dwo_type_unit
1667 (struct signatured_type *, const char *, const char *);
1668
1669 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1670
1671 /* A unique pointer to a dwo_file. */
1672
1673 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1674
1675 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1676
1677 static void check_producer (struct dwarf2_cu *cu);
1678
1679 static void free_line_header_voidp (void *arg);
1680 \f
1681 /* Various complaints about symbol reading that don't abort the process. */
1682
1683 static void
1684 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1685 {
1686 complaint (_("statement list doesn't fit in .debug_line section"));
1687 }
1688
1689 static void
1690 dwarf2_debug_line_missing_file_complaint (void)
1691 {
1692 complaint (_(".debug_line section has line data without a file"));
1693 }
1694
1695 static void
1696 dwarf2_debug_line_missing_end_sequence_complaint (void)
1697 {
1698 complaint (_(".debug_line section has line "
1699 "program sequence without an end"));
1700 }
1701
1702 static void
1703 dwarf2_complex_location_expr_complaint (void)
1704 {
1705 complaint (_("location expression too complex"));
1706 }
1707
1708 static void
1709 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1710 int arg3)
1711 {
1712 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1713 arg1, arg2, arg3);
1714 }
1715
1716 static void
1717 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1718 {
1719 complaint (_("debug info runs off end of %s section"
1720 " [in module %s]"),
1721 section->get_name (),
1722 section->get_file_name ());
1723 }
1724
1725 static void
1726 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1727 {
1728 complaint (_("macro debug info contains a "
1729 "malformed macro definition:\n`%s'"),
1730 arg1);
1731 }
1732
1733 static void
1734 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1735 {
1736 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1737 arg1, arg2);
1738 }
1739
1740 /* Hash function for line_header_hash. */
1741
1742 static hashval_t
1743 line_header_hash (const struct line_header *ofs)
1744 {
1745 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1746 }
1747
1748 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1749
1750 static hashval_t
1751 line_header_hash_voidp (const void *item)
1752 {
1753 const struct line_header *ofs = (const struct line_header *) item;
1754
1755 return line_header_hash (ofs);
1756 }
1757
1758 /* Equality function for line_header_hash. */
1759
1760 static int
1761 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1762 {
1763 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1764 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1765
1766 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1767 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1768 }
1769
1770 \f
1771
1772 /* See declaration. */
1773
1774 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1775 const dwarf2_debug_sections *names,
1776 bool can_copy_)
1777 : objfile (objfile_),
1778 can_copy (can_copy_)
1779 {
1780 if (names == NULL)
1781 names = &dwarf2_elf_names;
1782
1783 bfd *obfd = objfile->obfd;
1784
1785 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1786 locate_sections (obfd, sec, *names);
1787 }
1788
1789 dwarf2_per_objfile::~dwarf2_per_objfile ()
1790 {
1791 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1792 free_cached_comp_units ();
1793
1794 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1795 per_cu->imported_symtabs_free ();
1796
1797 for (signatured_type *sig_type : all_type_units)
1798 sig_type->per_cu.imported_symtabs_free ();
1799
1800 /* Everything else should be on the objfile obstack. */
1801 }
1802
1803 /* See declaration. */
1804
1805 void
1806 dwarf2_per_objfile::free_cached_comp_units ()
1807 {
1808 dwarf2_per_cu_data *per_cu = read_in_chain;
1809 dwarf2_per_cu_data **last_chain = &read_in_chain;
1810 while (per_cu != NULL)
1811 {
1812 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1813
1814 delete per_cu->cu;
1815 *last_chain = next_cu;
1816 per_cu = next_cu;
1817 }
1818 }
1819
1820 /* A helper class that calls free_cached_comp_units on
1821 destruction. */
1822
1823 class free_cached_comp_units
1824 {
1825 public:
1826
1827 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1828 : m_per_objfile (per_objfile)
1829 {
1830 }
1831
1832 ~free_cached_comp_units ()
1833 {
1834 m_per_objfile->free_cached_comp_units ();
1835 }
1836
1837 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1838
1839 private:
1840
1841 dwarf2_per_objfile *m_per_objfile;
1842 };
1843
1844 /* Try to locate the sections we need for DWARF 2 debugging
1845 information and return true if we have enough to do something.
1846 NAMES points to the dwarf2 section names, or is NULL if the standard
1847 ELF names are used. CAN_COPY is true for formats where symbol
1848 interposition is possible and so symbol values must follow copy
1849 relocation rules. */
1850
1851 int
1852 dwarf2_has_info (struct objfile *objfile,
1853 const struct dwarf2_debug_sections *names,
1854 bool can_copy)
1855 {
1856 if (objfile->flags & OBJF_READNEVER)
1857 return 0;
1858
1859 struct dwarf2_per_objfile *dwarf2_per_objfile
1860 = get_dwarf2_per_objfile (objfile);
1861
1862 if (dwarf2_per_objfile == NULL)
1863 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1864 names,
1865 can_copy);
1866
1867 return (!dwarf2_per_objfile->info.is_virtual
1868 && dwarf2_per_objfile->info.s.section != NULL
1869 && !dwarf2_per_objfile->abbrev.is_virtual
1870 && dwarf2_per_objfile->abbrev.s.section != NULL);
1871 }
1872
1873 /* When loading sections, we look either for uncompressed section or for
1874 compressed section names. */
1875
1876 static int
1877 section_is_p (const char *section_name,
1878 const struct dwarf2_section_names *names)
1879 {
1880 if (names->normal != NULL
1881 && strcmp (section_name, names->normal) == 0)
1882 return 1;
1883 if (names->compressed != NULL
1884 && strcmp (section_name, names->compressed) == 0)
1885 return 1;
1886 return 0;
1887 }
1888
1889 /* See declaration. */
1890
1891 void
1892 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1893 const dwarf2_debug_sections &names)
1894 {
1895 flagword aflag = bfd_section_flags (sectp);
1896
1897 if ((aflag & SEC_HAS_CONTENTS) == 0)
1898 {
1899 }
1900 else if (elf_section_data (sectp)->this_hdr.sh_size
1901 > bfd_get_file_size (abfd))
1902 {
1903 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1904 warning (_("Discarding section %s which has a section size (%s"
1905 ") larger than the file size [in module %s]"),
1906 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1907 bfd_get_filename (abfd));
1908 }
1909 else if (section_is_p (sectp->name, &names.info))
1910 {
1911 this->info.s.section = sectp;
1912 this->info.size = bfd_section_size (sectp);
1913 }
1914 else if (section_is_p (sectp->name, &names.abbrev))
1915 {
1916 this->abbrev.s.section = sectp;
1917 this->abbrev.size = bfd_section_size (sectp);
1918 }
1919 else if (section_is_p (sectp->name, &names.line))
1920 {
1921 this->line.s.section = sectp;
1922 this->line.size = bfd_section_size (sectp);
1923 }
1924 else if (section_is_p (sectp->name, &names.loc))
1925 {
1926 this->loc.s.section = sectp;
1927 this->loc.size = bfd_section_size (sectp);
1928 }
1929 else if (section_is_p (sectp->name, &names.loclists))
1930 {
1931 this->loclists.s.section = sectp;
1932 this->loclists.size = bfd_section_size (sectp);
1933 }
1934 else if (section_is_p (sectp->name, &names.macinfo))
1935 {
1936 this->macinfo.s.section = sectp;
1937 this->macinfo.size = bfd_section_size (sectp);
1938 }
1939 else if (section_is_p (sectp->name, &names.macro))
1940 {
1941 this->macro.s.section = sectp;
1942 this->macro.size = bfd_section_size (sectp);
1943 }
1944 else if (section_is_p (sectp->name, &names.str))
1945 {
1946 this->str.s.section = sectp;
1947 this->str.size = bfd_section_size (sectp);
1948 }
1949 else if (section_is_p (sectp->name, &names.str_offsets))
1950 {
1951 this->str_offsets.s.section = sectp;
1952 this->str_offsets.size = bfd_section_size (sectp);
1953 }
1954 else if (section_is_p (sectp->name, &names.line_str))
1955 {
1956 this->line_str.s.section = sectp;
1957 this->line_str.size = bfd_section_size (sectp);
1958 }
1959 else if (section_is_p (sectp->name, &names.addr))
1960 {
1961 this->addr.s.section = sectp;
1962 this->addr.size = bfd_section_size (sectp);
1963 }
1964 else if (section_is_p (sectp->name, &names.frame))
1965 {
1966 this->frame.s.section = sectp;
1967 this->frame.size = bfd_section_size (sectp);
1968 }
1969 else if (section_is_p (sectp->name, &names.eh_frame))
1970 {
1971 this->eh_frame.s.section = sectp;
1972 this->eh_frame.size = bfd_section_size (sectp);
1973 }
1974 else if (section_is_p (sectp->name, &names.ranges))
1975 {
1976 this->ranges.s.section = sectp;
1977 this->ranges.size = bfd_section_size (sectp);
1978 }
1979 else if (section_is_p (sectp->name, &names.rnglists))
1980 {
1981 this->rnglists.s.section = sectp;
1982 this->rnglists.size = bfd_section_size (sectp);
1983 }
1984 else if (section_is_p (sectp->name, &names.types))
1985 {
1986 struct dwarf2_section_info type_section;
1987
1988 memset (&type_section, 0, sizeof (type_section));
1989 type_section.s.section = sectp;
1990 type_section.size = bfd_section_size (sectp);
1991
1992 this->types.push_back (type_section);
1993 }
1994 else if (section_is_p (sectp->name, &names.gdb_index))
1995 {
1996 this->gdb_index.s.section = sectp;
1997 this->gdb_index.size = bfd_section_size (sectp);
1998 }
1999 else if (section_is_p (sectp->name, &names.debug_names))
2000 {
2001 this->debug_names.s.section = sectp;
2002 this->debug_names.size = bfd_section_size (sectp);
2003 }
2004 else if (section_is_p (sectp->name, &names.debug_aranges))
2005 {
2006 this->debug_aranges.s.section = sectp;
2007 this->debug_aranges.size = bfd_section_size (sectp);
2008 }
2009
2010 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2011 && bfd_section_vma (sectp) == 0)
2012 this->has_section_at_zero = true;
2013 }
2014
2015 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2016 SECTION_NAME. */
2017
2018 void
2019 dwarf2_get_section_info (struct objfile *objfile,
2020 enum dwarf2_section_enum sect,
2021 asection **sectp, const gdb_byte **bufp,
2022 bfd_size_type *sizep)
2023 {
2024 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2025 struct dwarf2_section_info *info;
2026
2027 /* We may see an objfile without any DWARF, in which case we just
2028 return nothing. */
2029 if (data == NULL)
2030 {
2031 *sectp = NULL;
2032 *bufp = NULL;
2033 *sizep = 0;
2034 return;
2035 }
2036 switch (sect)
2037 {
2038 case DWARF2_DEBUG_FRAME:
2039 info = &data->frame;
2040 break;
2041 case DWARF2_EH_FRAME:
2042 info = &data->eh_frame;
2043 break;
2044 default:
2045 gdb_assert_not_reached ("unexpected section");
2046 }
2047
2048 info->read (objfile);
2049
2050 *sectp = info->get_bfd_section ();
2051 *bufp = info->buffer;
2052 *sizep = info->size;
2053 }
2054
2055 /* A helper function to find the sections for a .dwz file. */
2056
2057 static void
2058 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2059 {
2060 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2061
2062 /* Note that we only support the standard ELF names, because .dwz
2063 is ELF-only (at the time of writing). */
2064 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2065 {
2066 dwz_file->abbrev.s.section = sectp;
2067 dwz_file->abbrev.size = bfd_section_size (sectp);
2068 }
2069 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2070 {
2071 dwz_file->info.s.section = sectp;
2072 dwz_file->info.size = bfd_section_size (sectp);
2073 }
2074 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2075 {
2076 dwz_file->str.s.section = sectp;
2077 dwz_file->str.size = bfd_section_size (sectp);
2078 }
2079 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2080 {
2081 dwz_file->line.s.section = sectp;
2082 dwz_file->line.size = bfd_section_size (sectp);
2083 }
2084 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2085 {
2086 dwz_file->macro.s.section = sectp;
2087 dwz_file->macro.size = bfd_section_size (sectp);
2088 }
2089 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2090 {
2091 dwz_file->gdb_index.s.section = sectp;
2092 dwz_file->gdb_index.size = bfd_section_size (sectp);
2093 }
2094 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2095 {
2096 dwz_file->debug_names.s.section = sectp;
2097 dwz_file->debug_names.size = bfd_section_size (sectp);
2098 }
2099 }
2100
2101 /* See dwarf2read.h. */
2102
2103 struct dwz_file *
2104 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2105 {
2106 const char *filename;
2107 bfd_size_type buildid_len_arg;
2108 size_t buildid_len;
2109 bfd_byte *buildid;
2110
2111 if (dwarf2_per_objfile->dwz_file != NULL)
2112 return dwarf2_per_objfile->dwz_file.get ();
2113
2114 bfd_set_error (bfd_error_no_error);
2115 gdb::unique_xmalloc_ptr<char> data
2116 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2117 &buildid_len_arg, &buildid));
2118 if (data == NULL)
2119 {
2120 if (bfd_get_error () == bfd_error_no_error)
2121 return NULL;
2122 error (_("could not read '.gnu_debugaltlink' section: %s"),
2123 bfd_errmsg (bfd_get_error ()));
2124 }
2125
2126 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2127
2128 buildid_len = (size_t) buildid_len_arg;
2129
2130 filename = data.get ();
2131
2132 std::string abs_storage;
2133 if (!IS_ABSOLUTE_PATH (filename))
2134 {
2135 gdb::unique_xmalloc_ptr<char> abs
2136 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2137
2138 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2139 filename = abs_storage.c_str ();
2140 }
2141
2142 /* First try the file name given in the section. If that doesn't
2143 work, try to use the build-id instead. */
2144 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2145 if (dwz_bfd != NULL)
2146 {
2147 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2148 dwz_bfd.reset (nullptr);
2149 }
2150
2151 if (dwz_bfd == NULL)
2152 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2153
2154 if (dwz_bfd == NULL)
2155 error (_("could not find '.gnu_debugaltlink' file for %s"),
2156 objfile_name (dwarf2_per_objfile->objfile));
2157
2158 std::unique_ptr<struct dwz_file> result
2159 (new struct dwz_file (std::move (dwz_bfd)));
2160
2161 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2162 result.get ());
2163
2164 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2165 result->dwz_bfd.get ());
2166 dwarf2_per_objfile->dwz_file = std::move (result);
2167 return dwarf2_per_objfile->dwz_file.get ();
2168 }
2169 \f
2170 /* DWARF quick_symbols_functions support. */
2171
2172 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2173 unique line tables, so we maintain a separate table of all .debug_line
2174 derived entries to support the sharing.
2175 All the quick functions need is the list of file names. We discard the
2176 line_header when we're done and don't need to record it here. */
2177 struct quick_file_names
2178 {
2179 /* The data used to construct the hash key. */
2180 struct stmt_list_hash hash;
2181
2182 /* The number of entries in file_names, real_names. */
2183 unsigned int num_file_names;
2184
2185 /* The file names from the line table, after being run through
2186 file_full_name. */
2187 const char **file_names;
2188
2189 /* The file names from the line table after being run through
2190 gdb_realpath. These are computed lazily. */
2191 const char **real_names;
2192 };
2193
2194 /* When using the index (and thus not using psymtabs), each CU has an
2195 object of this type. This is used to hold information needed by
2196 the various "quick" methods. */
2197 struct dwarf2_per_cu_quick_data
2198 {
2199 /* The file table. This can be NULL if there was no file table
2200 or it's currently not read in.
2201 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2202 struct quick_file_names *file_names;
2203
2204 /* The corresponding symbol table. This is NULL if symbols for this
2205 CU have not yet been read. */
2206 struct compunit_symtab *compunit_symtab;
2207
2208 /* A temporary mark bit used when iterating over all CUs in
2209 expand_symtabs_matching. */
2210 unsigned int mark : 1;
2211
2212 /* True if we've tried to read the file table and found there isn't one.
2213 There will be no point in trying to read it again next time. */
2214 unsigned int no_file_data : 1;
2215 };
2216
2217 /* Utility hash function for a stmt_list_hash. */
2218
2219 static hashval_t
2220 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2221 {
2222 hashval_t v = 0;
2223
2224 if (stmt_list_hash->dwo_unit != NULL)
2225 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2226 v += to_underlying (stmt_list_hash->line_sect_off);
2227 return v;
2228 }
2229
2230 /* Utility equality function for a stmt_list_hash. */
2231
2232 static int
2233 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2234 const struct stmt_list_hash *rhs)
2235 {
2236 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2237 return 0;
2238 if (lhs->dwo_unit != NULL
2239 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2240 return 0;
2241
2242 return lhs->line_sect_off == rhs->line_sect_off;
2243 }
2244
2245 /* Hash function for a quick_file_names. */
2246
2247 static hashval_t
2248 hash_file_name_entry (const void *e)
2249 {
2250 const struct quick_file_names *file_data
2251 = (const struct quick_file_names *) e;
2252
2253 return hash_stmt_list_entry (&file_data->hash);
2254 }
2255
2256 /* Equality function for a quick_file_names. */
2257
2258 static int
2259 eq_file_name_entry (const void *a, const void *b)
2260 {
2261 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2262 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2263
2264 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2265 }
2266
2267 /* Delete function for a quick_file_names. */
2268
2269 static void
2270 delete_file_name_entry (void *e)
2271 {
2272 struct quick_file_names *file_data = (struct quick_file_names *) e;
2273 int i;
2274
2275 for (i = 0; i < file_data->num_file_names; ++i)
2276 {
2277 xfree ((void*) file_data->file_names[i]);
2278 if (file_data->real_names)
2279 xfree ((void*) file_data->real_names[i]);
2280 }
2281
2282 /* The space for the struct itself lives on objfile_obstack,
2283 so we don't free it here. */
2284 }
2285
2286 /* Create a quick_file_names hash table. */
2287
2288 static htab_up
2289 create_quick_file_names_table (unsigned int nr_initial_entries)
2290 {
2291 return htab_up (htab_create_alloc (nr_initial_entries,
2292 hash_file_name_entry, eq_file_name_entry,
2293 delete_file_name_entry, xcalloc, xfree));
2294 }
2295
2296 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2297 have to be created afterwards. You should call age_cached_comp_units after
2298 processing PER_CU->CU. dw2_setup must have been already called. */
2299
2300 static void
2301 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2302 {
2303 if (per_cu->is_debug_types)
2304 load_full_type_unit (per_cu);
2305 else
2306 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2307
2308 if (per_cu->cu == NULL)
2309 return; /* Dummy CU. */
2310
2311 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2312 }
2313
2314 /* Read in the symbols for PER_CU. */
2315
2316 static void
2317 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2318 {
2319 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2320
2321 /* Skip type_unit_groups, reading the type units they contain
2322 is handled elsewhere. */
2323 if (per_cu->type_unit_group_p ())
2324 return;
2325
2326 /* The destructor of dwarf2_queue_guard frees any entries left on
2327 the queue. After this point we're guaranteed to leave this function
2328 with the dwarf queue empty. */
2329 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2330
2331 if (dwarf2_per_objfile->using_index
2332 ? per_cu->v.quick->compunit_symtab == NULL
2333 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2334 {
2335 queue_comp_unit (per_cu, language_minimal);
2336 load_cu (per_cu, skip_partial);
2337
2338 /* If we just loaded a CU from a DWO, and we're working with an index
2339 that may badly handle TUs, load all the TUs in that DWO as well.
2340 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2341 if (!per_cu->is_debug_types
2342 && per_cu->cu != NULL
2343 && per_cu->cu->dwo_unit != NULL
2344 && dwarf2_per_objfile->index_table != NULL
2345 && dwarf2_per_objfile->index_table->version <= 7
2346 /* DWP files aren't supported yet. */
2347 && get_dwp_file (dwarf2_per_objfile) == NULL)
2348 queue_and_load_all_dwo_tus (per_cu);
2349 }
2350
2351 process_queue (dwarf2_per_objfile);
2352
2353 /* Age the cache, releasing compilation units that have not
2354 been used recently. */
2355 age_cached_comp_units (dwarf2_per_objfile);
2356 }
2357
2358 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2359 the objfile from which this CU came. Returns the resulting symbol
2360 table. */
2361
2362 static struct compunit_symtab *
2363 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2364 {
2365 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2366
2367 gdb_assert (dwarf2_per_objfile->using_index);
2368 if (!per_cu->v.quick->compunit_symtab)
2369 {
2370 free_cached_comp_units freer (dwarf2_per_objfile);
2371 scoped_restore decrementer = increment_reading_symtab ();
2372 dw2_do_instantiate_symtab (per_cu, skip_partial);
2373 process_cu_includes (dwarf2_per_objfile);
2374 }
2375
2376 return per_cu->v.quick->compunit_symtab;
2377 }
2378
2379 /* See declaration. */
2380
2381 dwarf2_per_cu_data *
2382 dwarf2_per_objfile::get_cutu (int index)
2383 {
2384 if (index >= this->all_comp_units.size ())
2385 {
2386 index -= this->all_comp_units.size ();
2387 gdb_assert (index < this->all_type_units.size ());
2388 return &this->all_type_units[index]->per_cu;
2389 }
2390
2391 return this->all_comp_units[index];
2392 }
2393
2394 /* See declaration. */
2395
2396 dwarf2_per_cu_data *
2397 dwarf2_per_objfile::get_cu (int index)
2398 {
2399 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2400
2401 return this->all_comp_units[index];
2402 }
2403
2404 /* See declaration. */
2405
2406 signatured_type *
2407 dwarf2_per_objfile::get_tu (int index)
2408 {
2409 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2410
2411 return this->all_type_units[index];
2412 }
2413
2414 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2415 objfile_obstack, and constructed with the specified field
2416 values. */
2417
2418 static dwarf2_per_cu_data *
2419 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2420 struct dwarf2_section_info *section,
2421 int is_dwz,
2422 sect_offset sect_off, ULONGEST length)
2423 {
2424 struct objfile *objfile = dwarf2_per_objfile->objfile;
2425 dwarf2_per_cu_data *the_cu
2426 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2427 struct dwarf2_per_cu_data);
2428 the_cu->sect_off = sect_off;
2429 the_cu->length = length;
2430 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2431 the_cu->section = section;
2432 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2433 struct dwarf2_per_cu_quick_data);
2434 the_cu->is_dwz = is_dwz;
2435 return the_cu;
2436 }
2437
2438 /* A helper for create_cus_from_index that handles a given list of
2439 CUs. */
2440
2441 static void
2442 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2443 const gdb_byte *cu_list, offset_type n_elements,
2444 struct dwarf2_section_info *section,
2445 int is_dwz)
2446 {
2447 for (offset_type i = 0; i < n_elements; i += 2)
2448 {
2449 gdb_static_assert (sizeof (ULONGEST) >= 8);
2450
2451 sect_offset sect_off
2452 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2453 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2454 cu_list += 2 * 8;
2455
2456 dwarf2_per_cu_data *per_cu
2457 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2458 sect_off, length);
2459 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2460 }
2461 }
2462
2463 /* Read the CU list from the mapped index, and use it to create all
2464 the CU objects for this objfile. */
2465
2466 static void
2467 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2468 const gdb_byte *cu_list, offset_type cu_list_elements,
2469 const gdb_byte *dwz_list, offset_type dwz_elements)
2470 {
2471 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2472 dwarf2_per_objfile->all_comp_units.reserve
2473 ((cu_list_elements + dwz_elements) / 2);
2474
2475 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2476 &dwarf2_per_objfile->info, 0);
2477
2478 if (dwz_elements == 0)
2479 return;
2480
2481 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2482 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2483 &dwz->info, 1);
2484 }
2485
2486 /* Create the signatured type hash table from the index. */
2487
2488 static void
2489 create_signatured_type_table_from_index
2490 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2491 struct dwarf2_section_info *section,
2492 const gdb_byte *bytes,
2493 offset_type elements)
2494 {
2495 struct objfile *objfile = dwarf2_per_objfile->objfile;
2496
2497 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2498 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2499
2500 htab_up sig_types_hash = allocate_signatured_type_table ();
2501
2502 for (offset_type i = 0; i < elements; i += 3)
2503 {
2504 struct signatured_type *sig_type;
2505 ULONGEST signature;
2506 void **slot;
2507 cu_offset type_offset_in_tu;
2508
2509 gdb_static_assert (sizeof (ULONGEST) >= 8);
2510 sect_offset sect_off
2511 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2512 type_offset_in_tu
2513 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2514 BFD_ENDIAN_LITTLE);
2515 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2516 bytes += 3 * 8;
2517
2518 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2519 struct signatured_type);
2520 sig_type->signature = signature;
2521 sig_type->type_offset_in_tu = type_offset_in_tu;
2522 sig_type->per_cu.is_debug_types = 1;
2523 sig_type->per_cu.section = section;
2524 sig_type->per_cu.sect_off = sect_off;
2525 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2526 sig_type->per_cu.v.quick
2527 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2528 struct dwarf2_per_cu_quick_data);
2529
2530 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2531 *slot = sig_type;
2532
2533 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2534 }
2535
2536 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2537 }
2538
2539 /* Create the signatured type hash table from .debug_names. */
2540
2541 static void
2542 create_signatured_type_table_from_debug_names
2543 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2544 const mapped_debug_names &map,
2545 struct dwarf2_section_info *section,
2546 struct dwarf2_section_info *abbrev_section)
2547 {
2548 struct objfile *objfile = dwarf2_per_objfile->objfile;
2549
2550 section->read (objfile);
2551 abbrev_section->read (objfile);
2552
2553 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2554 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2555
2556 htab_up sig_types_hash = allocate_signatured_type_table ();
2557
2558 for (uint32_t i = 0; i < map.tu_count; ++i)
2559 {
2560 struct signatured_type *sig_type;
2561 void **slot;
2562
2563 sect_offset sect_off
2564 = (sect_offset) (extract_unsigned_integer
2565 (map.tu_table_reordered + i * map.offset_size,
2566 map.offset_size,
2567 map.dwarf5_byte_order));
2568
2569 comp_unit_head cu_header;
2570 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2571 abbrev_section,
2572 section->buffer + to_underlying (sect_off),
2573 rcuh_kind::TYPE);
2574
2575 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2576 struct signatured_type);
2577 sig_type->signature = cu_header.signature;
2578 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2579 sig_type->per_cu.is_debug_types = 1;
2580 sig_type->per_cu.section = section;
2581 sig_type->per_cu.sect_off = sect_off;
2582 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2583 sig_type->per_cu.v.quick
2584 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2585 struct dwarf2_per_cu_quick_data);
2586
2587 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2588 *slot = sig_type;
2589
2590 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2591 }
2592
2593 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2594 }
2595
2596 /* Read the address map data from the mapped index, and use it to
2597 populate the objfile's psymtabs_addrmap. */
2598
2599 static void
2600 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2601 struct mapped_index *index)
2602 {
2603 struct objfile *objfile = dwarf2_per_objfile->objfile;
2604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2605 const gdb_byte *iter, *end;
2606 struct addrmap *mutable_map;
2607 CORE_ADDR baseaddr;
2608
2609 auto_obstack temp_obstack;
2610
2611 mutable_map = addrmap_create_mutable (&temp_obstack);
2612
2613 iter = index->address_table.data ();
2614 end = iter + index->address_table.size ();
2615
2616 baseaddr = objfile->text_section_offset ();
2617
2618 while (iter < end)
2619 {
2620 ULONGEST hi, lo, cu_index;
2621 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2622 iter += 8;
2623 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2624 iter += 8;
2625 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2626 iter += 4;
2627
2628 if (lo > hi)
2629 {
2630 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2631 hex_string (lo), hex_string (hi));
2632 continue;
2633 }
2634
2635 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2636 {
2637 complaint (_(".gdb_index address table has invalid CU number %u"),
2638 (unsigned) cu_index);
2639 continue;
2640 }
2641
2642 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2643 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2644 addrmap_set_empty (mutable_map, lo, hi - 1,
2645 dwarf2_per_objfile->get_cu (cu_index));
2646 }
2647
2648 objfile->partial_symtabs->psymtabs_addrmap
2649 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2650 }
2651
2652 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2653 populate the objfile's psymtabs_addrmap. */
2654
2655 static void
2656 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2657 struct dwarf2_section_info *section)
2658 {
2659 struct objfile *objfile = dwarf2_per_objfile->objfile;
2660 bfd *abfd = objfile->obfd;
2661 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2662 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2663
2664 auto_obstack temp_obstack;
2665 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2666
2667 std::unordered_map<sect_offset,
2668 dwarf2_per_cu_data *,
2669 gdb::hash_enum<sect_offset>>
2670 debug_info_offset_to_per_cu;
2671 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2672 {
2673 const auto insertpair
2674 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2675 if (!insertpair.second)
2676 {
2677 warning (_("Section .debug_aranges in %s has duplicate "
2678 "debug_info_offset %s, ignoring .debug_aranges."),
2679 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2680 return;
2681 }
2682 }
2683
2684 section->read (objfile);
2685
2686 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2687
2688 const gdb_byte *addr = section->buffer;
2689
2690 while (addr < section->buffer + section->size)
2691 {
2692 const gdb_byte *const entry_addr = addr;
2693 unsigned int bytes_read;
2694
2695 const LONGEST entry_length = read_initial_length (abfd, addr,
2696 &bytes_read);
2697 addr += bytes_read;
2698
2699 const gdb_byte *const entry_end = addr + entry_length;
2700 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2701 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2702 if (addr + entry_length > section->buffer + section->size)
2703 {
2704 warning (_("Section .debug_aranges in %s entry at offset %s "
2705 "length %s exceeds section length %s, "
2706 "ignoring .debug_aranges."),
2707 objfile_name (objfile),
2708 plongest (entry_addr - section->buffer),
2709 plongest (bytes_read + entry_length),
2710 pulongest (section->size));
2711 return;
2712 }
2713
2714 /* The version number. */
2715 const uint16_t version = read_2_bytes (abfd, addr);
2716 addr += 2;
2717 if (version != 2)
2718 {
2719 warning (_("Section .debug_aranges in %s entry at offset %s "
2720 "has unsupported version %d, ignoring .debug_aranges."),
2721 objfile_name (objfile),
2722 plongest (entry_addr - section->buffer), version);
2723 return;
2724 }
2725
2726 const uint64_t debug_info_offset
2727 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2728 addr += offset_size;
2729 const auto per_cu_it
2730 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2731 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2732 {
2733 warning (_("Section .debug_aranges in %s entry at offset %s "
2734 "debug_info_offset %s does not exists, "
2735 "ignoring .debug_aranges."),
2736 objfile_name (objfile),
2737 plongest (entry_addr - section->buffer),
2738 pulongest (debug_info_offset));
2739 return;
2740 }
2741 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2742
2743 const uint8_t address_size = *addr++;
2744 if (address_size < 1 || address_size > 8)
2745 {
2746 warning (_("Section .debug_aranges in %s entry at offset %s "
2747 "address_size %u is invalid, ignoring .debug_aranges."),
2748 objfile_name (objfile),
2749 plongest (entry_addr - section->buffer), address_size);
2750 return;
2751 }
2752
2753 const uint8_t segment_selector_size = *addr++;
2754 if (segment_selector_size != 0)
2755 {
2756 warning (_("Section .debug_aranges in %s entry at offset %s "
2757 "segment_selector_size %u is not supported, "
2758 "ignoring .debug_aranges."),
2759 objfile_name (objfile),
2760 plongest (entry_addr - section->buffer),
2761 segment_selector_size);
2762 return;
2763 }
2764
2765 /* Must pad to an alignment boundary that is twice the address
2766 size. It is undocumented by the DWARF standard but GCC does
2767 use it. */
2768 for (size_t padding = ((-(addr - section->buffer))
2769 & (2 * address_size - 1));
2770 padding > 0; padding--)
2771 if (*addr++ != 0)
2772 {
2773 warning (_("Section .debug_aranges in %s entry at offset %s "
2774 "padding is not zero, ignoring .debug_aranges."),
2775 objfile_name (objfile),
2776 plongest (entry_addr - section->buffer));
2777 return;
2778 }
2779
2780 for (;;)
2781 {
2782 if (addr + 2 * address_size > entry_end)
2783 {
2784 warning (_("Section .debug_aranges in %s entry at offset %s "
2785 "address list is not properly terminated, "
2786 "ignoring .debug_aranges."),
2787 objfile_name (objfile),
2788 plongest (entry_addr - section->buffer));
2789 return;
2790 }
2791 ULONGEST start = extract_unsigned_integer (addr, address_size,
2792 dwarf5_byte_order);
2793 addr += address_size;
2794 ULONGEST length = extract_unsigned_integer (addr, address_size,
2795 dwarf5_byte_order);
2796 addr += address_size;
2797 if (start == 0 && length == 0)
2798 break;
2799 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2800 {
2801 /* Symbol was eliminated due to a COMDAT group. */
2802 continue;
2803 }
2804 ULONGEST end = start + length;
2805 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2806 - baseaddr);
2807 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2808 - baseaddr);
2809 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2810 }
2811 }
2812
2813 objfile->partial_symtabs->psymtabs_addrmap
2814 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2815 }
2816
2817 /* Find a slot in the mapped index INDEX for the object named NAME.
2818 If NAME is found, set *VEC_OUT to point to the CU vector in the
2819 constant pool and return true. If NAME cannot be found, return
2820 false. */
2821
2822 static bool
2823 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2824 offset_type **vec_out)
2825 {
2826 offset_type hash;
2827 offset_type slot, step;
2828 int (*cmp) (const char *, const char *);
2829
2830 gdb::unique_xmalloc_ptr<char> without_params;
2831 if (current_language->la_language == language_cplus
2832 || current_language->la_language == language_fortran
2833 || current_language->la_language == language_d)
2834 {
2835 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2836 not contain any. */
2837
2838 if (strchr (name, '(') != NULL)
2839 {
2840 without_params = cp_remove_params (name);
2841
2842 if (without_params != NULL)
2843 name = without_params.get ();
2844 }
2845 }
2846
2847 /* Index version 4 did not support case insensitive searches. But the
2848 indices for case insensitive languages are built in lowercase, therefore
2849 simulate our NAME being searched is also lowercased. */
2850 hash = mapped_index_string_hash ((index->version == 4
2851 && case_sensitivity == case_sensitive_off
2852 ? 5 : index->version),
2853 name);
2854
2855 slot = hash & (index->symbol_table.size () - 1);
2856 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2857 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2858
2859 for (;;)
2860 {
2861 const char *str;
2862
2863 const auto &bucket = index->symbol_table[slot];
2864 if (bucket.name == 0 && bucket.vec == 0)
2865 return false;
2866
2867 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2868 if (!cmp (name, str))
2869 {
2870 *vec_out = (offset_type *) (index->constant_pool
2871 + MAYBE_SWAP (bucket.vec));
2872 return true;
2873 }
2874
2875 slot = (slot + step) & (index->symbol_table.size () - 1);
2876 }
2877 }
2878
2879 /* A helper function that reads the .gdb_index from BUFFER and fills
2880 in MAP. FILENAME is the name of the file containing the data;
2881 it is used for error reporting. DEPRECATED_OK is true if it is
2882 ok to use deprecated sections.
2883
2884 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2885 out parameters that are filled in with information about the CU and
2886 TU lists in the section.
2887
2888 Returns true if all went well, false otherwise. */
2889
2890 static bool
2891 read_gdb_index_from_buffer (struct objfile *objfile,
2892 const char *filename,
2893 bool deprecated_ok,
2894 gdb::array_view<const gdb_byte> buffer,
2895 struct mapped_index *map,
2896 const gdb_byte **cu_list,
2897 offset_type *cu_list_elements,
2898 const gdb_byte **types_list,
2899 offset_type *types_list_elements)
2900 {
2901 const gdb_byte *addr = &buffer[0];
2902
2903 /* Version check. */
2904 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2905 /* Versions earlier than 3 emitted every copy of a psymbol. This
2906 causes the index to behave very poorly for certain requests. Version 3
2907 contained incomplete addrmap. So, it seems better to just ignore such
2908 indices. */
2909 if (version < 4)
2910 {
2911 static int warning_printed = 0;
2912 if (!warning_printed)
2913 {
2914 warning (_("Skipping obsolete .gdb_index section in %s."),
2915 filename);
2916 warning_printed = 1;
2917 }
2918 return 0;
2919 }
2920 /* Index version 4 uses a different hash function than index version
2921 5 and later.
2922
2923 Versions earlier than 6 did not emit psymbols for inlined
2924 functions. Using these files will cause GDB not to be able to
2925 set breakpoints on inlined functions by name, so we ignore these
2926 indices unless the user has done
2927 "set use-deprecated-index-sections on". */
2928 if (version < 6 && !deprecated_ok)
2929 {
2930 static int warning_printed = 0;
2931 if (!warning_printed)
2932 {
2933 warning (_("\
2934 Skipping deprecated .gdb_index section in %s.\n\
2935 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2936 to use the section anyway."),
2937 filename);
2938 warning_printed = 1;
2939 }
2940 return 0;
2941 }
2942 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2943 of the TU (for symbols coming from TUs),
2944 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2945 Plus gold-generated indices can have duplicate entries for global symbols,
2946 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2947 These are just performance bugs, and we can't distinguish gdb-generated
2948 indices from gold-generated ones, so issue no warning here. */
2949
2950 /* Indexes with higher version than the one supported by GDB may be no
2951 longer backward compatible. */
2952 if (version > 8)
2953 return 0;
2954
2955 map->version = version;
2956
2957 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2958
2959 int i = 0;
2960 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2961 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2962 / 8);
2963 ++i;
2964
2965 *types_list = addr + MAYBE_SWAP (metadata[i]);
2966 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2967 - MAYBE_SWAP (metadata[i]))
2968 / 8);
2969 ++i;
2970
2971 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2972 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2973 map->address_table
2974 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2975 ++i;
2976
2977 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2978 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2979 map->symbol_table
2980 = gdb::array_view<mapped_index::symbol_table_slot>
2981 ((mapped_index::symbol_table_slot *) symbol_table,
2982 (mapped_index::symbol_table_slot *) symbol_table_end);
2983
2984 ++i;
2985 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2986
2987 return 1;
2988 }
2989
2990 /* Callback types for dwarf2_read_gdb_index. */
2991
2992 typedef gdb::function_view
2993 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2994 get_gdb_index_contents_ftype;
2995 typedef gdb::function_view
2996 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2997 get_gdb_index_contents_dwz_ftype;
2998
2999 /* Read .gdb_index. If everything went ok, initialize the "quick"
3000 elements of all the CUs and return 1. Otherwise, return 0. */
3001
3002 static int
3003 dwarf2_read_gdb_index
3004 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3005 get_gdb_index_contents_ftype get_gdb_index_contents,
3006 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3007 {
3008 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3009 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3010 struct dwz_file *dwz;
3011 struct objfile *objfile = dwarf2_per_objfile->objfile;
3012
3013 gdb::array_view<const gdb_byte> main_index_contents
3014 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3015
3016 if (main_index_contents.empty ())
3017 return 0;
3018
3019 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3020 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3021 use_deprecated_index_sections,
3022 main_index_contents, map.get (), &cu_list,
3023 &cu_list_elements, &types_list,
3024 &types_list_elements))
3025 return 0;
3026
3027 /* Don't use the index if it's empty. */
3028 if (map->symbol_table.empty ())
3029 return 0;
3030
3031 /* If there is a .dwz file, read it so we can get its CU list as
3032 well. */
3033 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3034 if (dwz != NULL)
3035 {
3036 struct mapped_index dwz_map;
3037 const gdb_byte *dwz_types_ignore;
3038 offset_type dwz_types_elements_ignore;
3039
3040 gdb::array_view<const gdb_byte> dwz_index_content
3041 = get_gdb_index_contents_dwz (objfile, dwz);
3042
3043 if (dwz_index_content.empty ())
3044 return 0;
3045
3046 if (!read_gdb_index_from_buffer (objfile,
3047 bfd_get_filename (dwz->dwz_bfd.get ()),
3048 1, dwz_index_content, &dwz_map,
3049 &dwz_list, &dwz_list_elements,
3050 &dwz_types_ignore,
3051 &dwz_types_elements_ignore))
3052 {
3053 warning (_("could not read '.gdb_index' section from %s; skipping"),
3054 bfd_get_filename (dwz->dwz_bfd.get ()));
3055 return 0;
3056 }
3057 }
3058
3059 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3060 dwz_list, dwz_list_elements);
3061
3062 if (types_list_elements)
3063 {
3064 /* We can only handle a single .debug_types when we have an
3065 index. */
3066 if (dwarf2_per_objfile->types.size () != 1)
3067 return 0;
3068
3069 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3070
3071 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3072 types_list, types_list_elements);
3073 }
3074
3075 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3076
3077 dwarf2_per_objfile->index_table = std::move (map);
3078 dwarf2_per_objfile->using_index = 1;
3079 dwarf2_per_objfile->quick_file_names_table =
3080 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3081
3082 return 1;
3083 }
3084
3085 /* die_reader_func for dw2_get_file_names. */
3086
3087 static void
3088 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3089 const gdb_byte *info_ptr,
3090 struct die_info *comp_unit_die)
3091 {
3092 struct dwarf2_cu *cu = reader->cu;
3093 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3094 struct dwarf2_per_objfile *dwarf2_per_objfile
3095 = cu->per_cu->dwarf2_per_objfile;
3096 struct objfile *objfile = dwarf2_per_objfile->objfile;
3097 struct dwarf2_per_cu_data *lh_cu;
3098 struct attribute *attr;
3099 void **slot;
3100 struct quick_file_names *qfn;
3101
3102 gdb_assert (! this_cu->is_debug_types);
3103
3104 /* Our callers never want to match partial units -- instead they
3105 will match the enclosing full CU. */
3106 if (comp_unit_die->tag == DW_TAG_partial_unit)
3107 {
3108 this_cu->v.quick->no_file_data = 1;
3109 return;
3110 }
3111
3112 lh_cu = this_cu;
3113 slot = NULL;
3114
3115 line_header_up lh;
3116 sect_offset line_offset {};
3117
3118 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3119 if (attr != nullptr)
3120 {
3121 struct quick_file_names find_entry;
3122
3123 line_offset = (sect_offset) DW_UNSND (attr);
3124
3125 /* We may have already read in this line header (TU line header sharing).
3126 If we have we're done. */
3127 find_entry.hash.dwo_unit = cu->dwo_unit;
3128 find_entry.hash.line_sect_off = line_offset;
3129 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3130 &find_entry, INSERT);
3131 if (*slot != NULL)
3132 {
3133 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3134 return;
3135 }
3136
3137 lh = dwarf_decode_line_header (line_offset, cu);
3138 }
3139 if (lh == NULL)
3140 {
3141 lh_cu->v.quick->no_file_data = 1;
3142 return;
3143 }
3144
3145 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3146 qfn->hash.dwo_unit = cu->dwo_unit;
3147 qfn->hash.line_sect_off = line_offset;
3148 gdb_assert (slot != NULL);
3149 *slot = qfn;
3150
3151 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3152
3153 int offset = 0;
3154 if (strcmp (fnd.name, "<unknown>") != 0)
3155 ++offset;
3156
3157 qfn->num_file_names = offset + lh->file_names_size ();
3158 qfn->file_names =
3159 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3160 if (offset != 0)
3161 qfn->file_names[0] = xstrdup (fnd.name);
3162 for (int i = 0; i < lh->file_names_size (); ++i)
3163 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3164 fnd.comp_dir).release ();
3165 qfn->real_names = NULL;
3166
3167 lh_cu->v.quick->file_names = qfn;
3168 }
3169
3170 /* A helper for the "quick" functions which attempts to read the line
3171 table for THIS_CU. */
3172
3173 static struct quick_file_names *
3174 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3175 {
3176 /* This should never be called for TUs. */
3177 gdb_assert (! this_cu->is_debug_types);
3178 /* Nor type unit groups. */
3179 gdb_assert (! this_cu->type_unit_group_p ());
3180
3181 if (this_cu->v.quick->file_names != NULL)
3182 return this_cu->v.quick->file_names;
3183 /* If we know there is no line data, no point in looking again. */
3184 if (this_cu->v.quick->no_file_data)
3185 return NULL;
3186
3187 cutu_reader reader (this_cu);
3188 if (!reader.dummy_p)
3189 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3190
3191 if (this_cu->v.quick->no_file_data)
3192 return NULL;
3193 return this_cu->v.quick->file_names;
3194 }
3195
3196 /* A helper for the "quick" functions which computes and caches the
3197 real path for a given file name from the line table. */
3198
3199 static const char *
3200 dw2_get_real_path (struct objfile *objfile,
3201 struct quick_file_names *qfn, int index)
3202 {
3203 if (qfn->real_names == NULL)
3204 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3205 qfn->num_file_names, const char *);
3206
3207 if (qfn->real_names[index] == NULL)
3208 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3209
3210 return qfn->real_names[index];
3211 }
3212
3213 static struct symtab *
3214 dw2_find_last_source_symtab (struct objfile *objfile)
3215 {
3216 struct dwarf2_per_objfile *dwarf2_per_objfile
3217 = get_dwarf2_per_objfile (objfile);
3218 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3219 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3220
3221 if (cust == NULL)
3222 return NULL;
3223
3224 return compunit_primary_filetab (cust);
3225 }
3226
3227 /* Traversal function for dw2_forget_cached_source_info. */
3228
3229 static int
3230 dw2_free_cached_file_names (void **slot, void *info)
3231 {
3232 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3233
3234 if (file_data->real_names)
3235 {
3236 int i;
3237
3238 for (i = 0; i < file_data->num_file_names; ++i)
3239 {
3240 xfree ((void*) file_data->real_names[i]);
3241 file_data->real_names[i] = NULL;
3242 }
3243 }
3244
3245 return 1;
3246 }
3247
3248 static void
3249 dw2_forget_cached_source_info (struct objfile *objfile)
3250 {
3251 struct dwarf2_per_objfile *dwarf2_per_objfile
3252 = get_dwarf2_per_objfile (objfile);
3253
3254 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3255 dw2_free_cached_file_names, NULL);
3256 }
3257
3258 /* Helper function for dw2_map_symtabs_matching_filename that expands
3259 the symtabs and calls the iterator. */
3260
3261 static int
3262 dw2_map_expand_apply (struct objfile *objfile,
3263 struct dwarf2_per_cu_data *per_cu,
3264 const char *name, const char *real_path,
3265 gdb::function_view<bool (symtab *)> callback)
3266 {
3267 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3268
3269 /* Don't visit already-expanded CUs. */
3270 if (per_cu->v.quick->compunit_symtab)
3271 return 0;
3272
3273 /* This may expand more than one symtab, and we want to iterate over
3274 all of them. */
3275 dw2_instantiate_symtab (per_cu, false);
3276
3277 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3278 last_made, callback);
3279 }
3280
3281 /* Implementation of the map_symtabs_matching_filename method. */
3282
3283 static bool
3284 dw2_map_symtabs_matching_filename
3285 (struct objfile *objfile, const char *name, const char *real_path,
3286 gdb::function_view<bool (symtab *)> callback)
3287 {
3288 const char *name_basename = lbasename (name);
3289 struct dwarf2_per_objfile *dwarf2_per_objfile
3290 = get_dwarf2_per_objfile (objfile);
3291
3292 /* The rule is CUs specify all the files, including those used by
3293 any TU, so there's no need to scan TUs here. */
3294
3295 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3296 {
3297 /* We only need to look at symtabs not already expanded. */
3298 if (per_cu->v.quick->compunit_symtab)
3299 continue;
3300
3301 quick_file_names *file_data = dw2_get_file_names (per_cu);
3302 if (file_data == NULL)
3303 continue;
3304
3305 for (int j = 0; j < file_data->num_file_names; ++j)
3306 {
3307 const char *this_name = file_data->file_names[j];
3308 const char *this_real_name;
3309
3310 if (compare_filenames_for_search (this_name, name))
3311 {
3312 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3313 callback))
3314 return true;
3315 continue;
3316 }
3317
3318 /* Before we invoke realpath, which can get expensive when many
3319 files are involved, do a quick comparison of the basenames. */
3320 if (! basenames_may_differ
3321 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3322 continue;
3323
3324 this_real_name = dw2_get_real_path (objfile, file_data, j);
3325 if (compare_filenames_for_search (this_real_name, name))
3326 {
3327 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3328 callback))
3329 return true;
3330 continue;
3331 }
3332
3333 if (real_path != NULL)
3334 {
3335 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3336 gdb_assert (IS_ABSOLUTE_PATH (name));
3337 if (this_real_name != NULL
3338 && FILENAME_CMP (real_path, this_real_name) == 0)
3339 {
3340 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3341 callback))
3342 return true;
3343 continue;
3344 }
3345 }
3346 }
3347 }
3348
3349 return false;
3350 }
3351
3352 /* Struct used to manage iterating over all CUs looking for a symbol. */
3353
3354 struct dw2_symtab_iterator
3355 {
3356 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3357 struct dwarf2_per_objfile *dwarf2_per_objfile;
3358 /* If set, only look for symbols that match that block. Valid values are
3359 GLOBAL_BLOCK and STATIC_BLOCK. */
3360 gdb::optional<block_enum> block_index;
3361 /* The kind of symbol we're looking for. */
3362 domain_enum domain;
3363 /* The list of CUs from the index entry of the symbol,
3364 or NULL if not found. */
3365 offset_type *vec;
3366 /* The next element in VEC to look at. */
3367 int next;
3368 /* The number of elements in VEC, or zero if there is no match. */
3369 int length;
3370 /* Have we seen a global version of the symbol?
3371 If so we can ignore all further global instances.
3372 This is to work around gold/15646, inefficient gold-generated
3373 indices. */
3374 int global_seen;
3375 };
3376
3377 /* Initialize the index symtab iterator ITER. */
3378
3379 static void
3380 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3381 struct dwarf2_per_objfile *dwarf2_per_objfile,
3382 gdb::optional<block_enum> block_index,
3383 domain_enum domain,
3384 const char *name)
3385 {
3386 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3387 iter->block_index = block_index;
3388 iter->domain = domain;
3389 iter->next = 0;
3390 iter->global_seen = 0;
3391
3392 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3393
3394 /* index is NULL if OBJF_READNOW. */
3395 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3396 iter->length = MAYBE_SWAP (*iter->vec);
3397 else
3398 {
3399 iter->vec = NULL;
3400 iter->length = 0;
3401 }
3402 }
3403
3404 /* Return the next matching CU or NULL if there are no more. */
3405
3406 static struct dwarf2_per_cu_data *
3407 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3408 {
3409 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3410
3411 for ( ; iter->next < iter->length; ++iter->next)
3412 {
3413 offset_type cu_index_and_attrs =
3414 MAYBE_SWAP (iter->vec[iter->next + 1]);
3415 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3416 gdb_index_symbol_kind symbol_kind =
3417 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3418 /* Only check the symbol attributes if they're present.
3419 Indices prior to version 7 don't record them,
3420 and indices >= 7 may elide them for certain symbols
3421 (gold does this). */
3422 int attrs_valid =
3423 (dwarf2_per_objfile->index_table->version >= 7
3424 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3425
3426 /* Don't crash on bad data. */
3427 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3428 + dwarf2_per_objfile->all_type_units.size ()))
3429 {
3430 complaint (_(".gdb_index entry has bad CU index"
3431 " [in module %s]"),
3432 objfile_name (dwarf2_per_objfile->objfile));
3433 continue;
3434 }
3435
3436 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3437
3438 /* Skip if already read in. */
3439 if (per_cu->v.quick->compunit_symtab)
3440 continue;
3441
3442 /* Check static vs global. */
3443 if (attrs_valid)
3444 {
3445 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3446
3447 if (iter->block_index.has_value ())
3448 {
3449 bool want_static = *iter->block_index == STATIC_BLOCK;
3450
3451 if (is_static != want_static)
3452 continue;
3453 }
3454
3455 /* Work around gold/15646. */
3456 if (!is_static && iter->global_seen)
3457 continue;
3458 if (!is_static)
3459 iter->global_seen = 1;
3460 }
3461
3462 /* Only check the symbol's kind if it has one. */
3463 if (attrs_valid)
3464 {
3465 switch (iter->domain)
3466 {
3467 case VAR_DOMAIN:
3468 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3469 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3470 /* Some types are also in VAR_DOMAIN. */
3471 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3472 continue;
3473 break;
3474 case STRUCT_DOMAIN:
3475 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3476 continue;
3477 break;
3478 case LABEL_DOMAIN:
3479 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3480 continue;
3481 break;
3482 case MODULE_DOMAIN:
3483 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3484 continue;
3485 break;
3486 default:
3487 break;
3488 }
3489 }
3490
3491 ++iter->next;
3492 return per_cu;
3493 }
3494
3495 return NULL;
3496 }
3497
3498 static struct compunit_symtab *
3499 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3500 const char *name, domain_enum domain)
3501 {
3502 struct compunit_symtab *stab_best = NULL;
3503 struct dwarf2_per_objfile *dwarf2_per_objfile
3504 = get_dwarf2_per_objfile (objfile);
3505
3506 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3507
3508 struct dw2_symtab_iterator iter;
3509 struct dwarf2_per_cu_data *per_cu;
3510
3511 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3512
3513 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3514 {
3515 struct symbol *sym, *with_opaque = NULL;
3516 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3517 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3518 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3519
3520 sym = block_find_symbol (block, name, domain,
3521 block_find_non_opaque_type_preferred,
3522 &with_opaque);
3523
3524 /* Some caution must be observed with overloaded functions
3525 and methods, since the index will not contain any overload
3526 information (but NAME might contain it). */
3527
3528 if (sym != NULL
3529 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3530 return stab;
3531 if (with_opaque != NULL
3532 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3533 stab_best = stab;
3534
3535 /* Keep looking through other CUs. */
3536 }
3537
3538 return stab_best;
3539 }
3540
3541 static void
3542 dw2_print_stats (struct objfile *objfile)
3543 {
3544 struct dwarf2_per_objfile *dwarf2_per_objfile
3545 = get_dwarf2_per_objfile (objfile);
3546 int total = (dwarf2_per_objfile->all_comp_units.size ()
3547 + dwarf2_per_objfile->all_type_units.size ());
3548 int count = 0;
3549
3550 for (int i = 0; i < total; ++i)
3551 {
3552 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3553
3554 if (!per_cu->v.quick->compunit_symtab)
3555 ++count;
3556 }
3557 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3558 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3559 }
3560
3561 /* This dumps minimal information about the index.
3562 It is called via "mt print objfiles".
3563 One use is to verify .gdb_index has been loaded by the
3564 gdb.dwarf2/gdb-index.exp testcase. */
3565
3566 static void
3567 dw2_dump (struct objfile *objfile)
3568 {
3569 struct dwarf2_per_objfile *dwarf2_per_objfile
3570 = get_dwarf2_per_objfile (objfile);
3571
3572 gdb_assert (dwarf2_per_objfile->using_index);
3573 printf_filtered (".gdb_index:");
3574 if (dwarf2_per_objfile->index_table != NULL)
3575 {
3576 printf_filtered (" version %d\n",
3577 dwarf2_per_objfile->index_table->version);
3578 }
3579 else
3580 printf_filtered (" faked for \"readnow\"\n");
3581 printf_filtered ("\n");
3582 }
3583
3584 static void
3585 dw2_expand_symtabs_for_function (struct objfile *objfile,
3586 const char *func_name)
3587 {
3588 struct dwarf2_per_objfile *dwarf2_per_objfile
3589 = get_dwarf2_per_objfile (objfile);
3590
3591 struct dw2_symtab_iterator iter;
3592 struct dwarf2_per_cu_data *per_cu;
3593
3594 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3595
3596 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3597 dw2_instantiate_symtab (per_cu, false);
3598
3599 }
3600
3601 static void
3602 dw2_expand_all_symtabs (struct objfile *objfile)
3603 {
3604 struct dwarf2_per_objfile *dwarf2_per_objfile
3605 = get_dwarf2_per_objfile (objfile);
3606 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3607 + dwarf2_per_objfile->all_type_units.size ());
3608
3609 for (int i = 0; i < total_units; ++i)
3610 {
3611 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3612
3613 /* We don't want to directly expand a partial CU, because if we
3614 read it with the wrong language, then assertion failures can
3615 be triggered later on. See PR symtab/23010. So, tell
3616 dw2_instantiate_symtab to skip partial CUs -- any important
3617 partial CU will be read via DW_TAG_imported_unit anyway. */
3618 dw2_instantiate_symtab (per_cu, true);
3619 }
3620 }
3621
3622 static void
3623 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3624 const char *fullname)
3625 {
3626 struct dwarf2_per_objfile *dwarf2_per_objfile
3627 = get_dwarf2_per_objfile (objfile);
3628
3629 /* We don't need to consider type units here.
3630 This is only called for examining code, e.g. expand_line_sal.
3631 There can be an order of magnitude (or more) more type units
3632 than comp units, and we avoid them if we can. */
3633
3634 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3635 {
3636 /* We only need to look at symtabs not already expanded. */
3637 if (per_cu->v.quick->compunit_symtab)
3638 continue;
3639
3640 quick_file_names *file_data = dw2_get_file_names (per_cu);
3641 if (file_data == NULL)
3642 continue;
3643
3644 for (int j = 0; j < file_data->num_file_names; ++j)
3645 {
3646 const char *this_fullname = file_data->file_names[j];
3647
3648 if (filename_cmp (this_fullname, fullname) == 0)
3649 {
3650 dw2_instantiate_symtab (per_cu, false);
3651 break;
3652 }
3653 }
3654 }
3655 }
3656
3657 static void
3658 dw2_map_matching_symbols
3659 (struct objfile *objfile,
3660 const lookup_name_info &name, domain_enum domain,
3661 int global,
3662 gdb::function_view<symbol_found_callback_ftype> callback,
3663 symbol_compare_ftype *ordered_compare)
3664 {
3665 /* Currently unimplemented; used for Ada. The function can be called if the
3666 current language is Ada for a non-Ada objfile using GNU index. As Ada
3667 does not look for non-Ada symbols this function should just return. */
3668 }
3669
3670 /* Starting from a search name, return the string that finds the upper
3671 bound of all strings that start with SEARCH_NAME in a sorted name
3672 list. Returns the empty string to indicate that the upper bound is
3673 the end of the list. */
3674
3675 static std::string
3676 make_sort_after_prefix_name (const char *search_name)
3677 {
3678 /* When looking to complete "func", we find the upper bound of all
3679 symbols that start with "func" by looking for where we'd insert
3680 the closest string that would follow "func" in lexicographical
3681 order. Usually, that's "func"-with-last-character-incremented,
3682 i.e. "fund". Mind non-ASCII characters, though. Usually those
3683 will be UTF-8 multi-byte sequences, but we can't be certain.
3684 Especially mind the 0xff character, which is a valid character in
3685 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3686 rule out compilers allowing it in identifiers. Note that
3687 conveniently, strcmp/strcasecmp are specified to compare
3688 characters interpreted as unsigned char. So what we do is treat
3689 the whole string as a base 256 number composed of a sequence of
3690 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3691 to 0, and carries 1 to the following more-significant position.
3692 If the very first character in SEARCH_NAME ends up incremented
3693 and carries/overflows, then the upper bound is the end of the
3694 list. The string after the empty string is also the empty
3695 string.
3696
3697 Some examples of this operation:
3698
3699 SEARCH_NAME => "+1" RESULT
3700
3701 "abc" => "abd"
3702 "ab\xff" => "ac"
3703 "\xff" "a" "\xff" => "\xff" "b"
3704 "\xff" => ""
3705 "\xff\xff" => ""
3706 "" => ""
3707
3708 Then, with these symbols for example:
3709
3710 func
3711 func1
3712 fund
3713
3714 completing "func" looks for symbols between "func" and
3715 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3716 which finds "func" and "func1", but not "fund".
3717
3718 And with:
3719
3720 funcÿ (Latin1 'ÿ' [0xff])
3721 funcÿ1
3722 fund
3723
3724 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3725 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3726
3727 And with:
3728
3729 ÿÿ (Latin1 'ÿ' [0xff])
3730 ÿÿ1
3731
3732 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3733 the end of the list.
3734 */
3735 std::string after = search_name;
3736 while (!after.empty () && (unsigned char) after.back () == 0xff)
3737 after.pop_back ();
3738 if (!after.empty ())
3739 after.back () = (unsigned char) after.back () + 1;
3740 return after;
3741 }
3742
3743 /* See declaration. */
3744
3745 std::pair<std::vector<name_component>::const_iterator,
3746 std::vector<name_component>::const_iterator>
3747 mapped_index_base::find_name_components_bounds
3748 (const lookup_name_info &lookup_name_without_params, language lang) const
3749 {
3750 auto *name_cmp
3751 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3752
3753 const char *lang_name
3754 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3755
3756 /* Comparison function object for lower_bound that matches against a
3757 given symbol name. */
3758 auto lookup_compare_lower = [&] (const name_component &elem,
3759 const char *name)
3760 {
3761 const char *elem_qualified = this->symbol_name_at (elem.idx);
3762 const char *elem_name = elem_qualified + elem.name_offset;
3763 return name_cmp (elem_name, name) < 0;
3764 };
3765
3766 /* Comparison function object for upper_bound that matches against a
3767 given symbol name. */
3768 auto lookup_compare_upper = [&] (const char *name,
3769 const name_component &elem)
3770 {
3771 const char *elem_qualified = this->symbol_name_at (elem.idx);
3772 const char *elem_name = elem_qualified + elem.name_offset;
3773 return name_cmp (name, elem_name) < 0;
3774 };
3775
3776 auto begin = this->name_components.begin ();
3777 auto end = this->name_components.end ();
3778
3779 /* Find the lower bound. */
3780 auto lower = [&] ()
3781 {
3782 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3783 return begin;
3784 else
3785 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3786 } ();
3787
3788 /* Find the upper bound. */
3789 auto upper = [&] ()
3790 {
3791 if (lookup_name_without_params.completion_mode ())
3792 {
3793 /* In completion mode, we want UPPER to point past all
3794 symbols names that have the same prefix. I.e., with
3795 these symbols, and completing "func":
3796
3797 function << lower bound
3798 function1
3799 other_function << upper bound
3800
3801 We find the upper bound by looking for the insertion
3802 point of "func"-with-last-character-incremented,
3803 i.e. "fund". */
3804 std::string after = make_sort_after_prefix_name (lang_name);
3805 if (after.empty ())
3806 return end;
3807 return std::lower_bound (lower, end, after.c_str (),
3808 lookup_compare_lower);
3809 }
3810 else
3811 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3812 } ();
3813
3814 return {lower, upper};
3815 }
3816
3817 /* See declaration. */
3818
3819 void
3820 mapped_index_base::build_name_components ()
3821 {
3822 if (!this->name_components.empty ())
3823 return;
3824
3825 this->name_components_casing = case_sensitivity;
3826 auto *name_cmp
3827 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3828
3829 /* The code below only knows how to break apart components of C++
3830 symbol names (and other languages that use '::' as
3831 namespace/module separator) and Ada symbol names. */
3832 auto count = this->symbol_name_count ();
3833 for (offset_type idx = 0; idx < count; idx++)
3834 {
3835 if (this->symbol_name_slot_invalid (idx))
3836 continue;
3837
3838 const char *name = this->symbol_name_at (idx);
3839
3840 /* Add each name component to the name component table. */
3841 unsigned int previous_len = 0;
3842
3843 if (strstr (name, "::") != nullptr)
3844 {
3845 for (unsigned int current_len = cp_find_first_component (name);
3846 name[current_len] != '\0';
3847 current_len += cp_find_first_component (name + current_len))
3848 {
3849 gdb_assert (name[current_len] == ':');
3850 this->name_components.push_back ({previous_len, idx});
3851 /* Skip the '::'. */
3852 current_len += 2;
3853 previous_len = current_len;
3854 }
3855 }
3856 else
3857 {
3858 /* Handle the Ada encoded (aka mangled) form here. */
3859 for (const char *iter = strstr (name, "__");
3860 iter != nullptr;
3861 iter = strstr (iter, "__"))
3862 {
3863 this->name_components.push_back ({previous_len, idx});
3864 iter += 2;
3865 previous_len = iter - name;
3866 }
3867 }
3868
3869 this->name_components.push_back ({previous_len, idx});
3870 }
3871
3872 /* Sort name_components elements by name. */
3873 auto name_comp_compare = [&] (const name_component &left,
3874 const name_component &right)
3875 {
3876 const char *left_qualified = this->symbol_name_at (left.idx);
3877 const char *right_qualified = this->symbol_name_at (right.idx);
3878
3879 const char *left_name = left_qualified + left.name_offset;
3880 const char *right_name = right_qualified + right.name_offset;
3881
3882 return name_cmp (left_name, right_name) < 0;
3883 };
3884
3885 std::sort (this->name_components.begin (),
3886 this->name_components.end (),
3887 name_comp_compare);
3888 }
3889
3890 /* Helper for dw2_expand_symtabs_matching that works with a
3891 mapped_index_base instead of the containing objfile. This is split
3892 to a separate function in order to be able to unit test the
3893 name_components matching using a mock mapped_index_base. For each
3894 symbol name that matches, calls MATCH_CALLBACK, passing it the
3895 symbol's index in the mapped_index_base symbol table. */
3896
3897 static void
3898 dw2_expand_symtabs_matching_symbol
3899 (mapped_index_base &index,
3900 const lookup_name_info &lookup_name_in,
3901 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3902 enum search_domain kind,
3903 gdb::function_view<bool (offset_type)> match_callback)
3904 {
3905 lookup_name_info lookup_name_without_params
3906 = lookup_name_in.make_ignore_params ();
3907
3908 /* Build the symbol name component sorted vector, if we haven't
3909 yet. */
3910 index.build_name_components ();
3911
3912 /* The same symbol may appear more than once in the range though.
3913 E.g., if we're looking for symbols that complete "w", and we have
3914 a symbol named "w1::w2", we'll find the two name components for
3915 that same symbol in the range. To be sure we only call the
3916 callback once per symbol, we first collect the symbol name
3917 indexes that matched in a temporary vector and ignore
3918 duplicates. */
3919 std::vector<offset_type> matches;
3920
3921 struct name_and_matcher
3922 {
3923 symbol_name_matcher_ftype *matcher;
3924 const std::string &name;
3925
3926 bool operator== (const name_and_matcher &other) const
3927 {
3928 return matcher == other.matcher && name == other.name;
3929 }
3930 };
3931
3932 /* A vector holding all the different symbol name matchers, for all
3933 languages. */
3934 std::vector<name_and_matcher> matchers;
3935
3936 for (int i = 0; i < nr_languages; i++)
3937 {
3938 enum language lang_e = (enum language) i;
3939
3940 const language_defn *lang = language_def (lang_e);
3941 symbol_name_matcher_ftype *name_matcher
3942 = get_symbol_name_matcher (lang, lookup_name_without_params);
3943
3944 name_and_matcher key {
3945 name_matcher,
3946 lookup_name_without_params.language_lookup_name (lang_e)
3947 };
3948
3949 /* Don't insert the same comparison routine more than once.
3950 Note that we do this linear walk. This is not a problem in
3951 practice because the number of supported languages is
3952 low. */
3953 if (std::find (matchers.begin (), matchers.end (), key)
3954 != matchers.end ())
3955 continue;
3956 matchers.push_back (std::move (key));
3957
3958 auto bounds
3959 = index.find_name_components_bounds (lookup_name_without_params,
3960 lang_e);
3961
3962 /* Now for each symbol name in range, check to see if we have a name
3963 match, and if so, call the MATCH_CALLBACK callback. */
3964
3965 for (; bounds.first != bounds.second; ++bounds.first)
3966 {
3967 const char *qualified = index.symbol_name_at (bounds.first->idx);
3968
3969 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3970 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3971 continue;
3972
3973 matches.push_back (bounds.first->idx);
3974 }
3975 }
3976
3977 std::sort (matches.begin (), matches.end ());
3978
3979 /* Finally call the callback, once per match. */
3980 ULONGEST prev = -1;
3981 for (offset_type idx : matches)
3982 {
3983 if (prev != idx)
3984 {
3985 if (!match_callback (idx))
3986 break;
3987 prev = idx;
3988 }
3989 }
3990
3991 /* Above we use a type wider than idx's for 'prev', since 0 and
3992 (offset_type)-1 are both possible values. */
3993 static_assert (sizeof (prev) > sizeof (offset_type), "");
3994 }
3995
3996 #if GDB_SELF_TEST
3997
3998 namespace selftests { namespace dw2_expand_symtabs_matching {
3999
4000 /* A mock .gdb_index/.debug_names-like name index table, enough to
4001 exercise dw2_expand_symtabs_matching_symbol, which works with the
4002 mapped_index_base interface. Builds an index from the symbol list
4003 passed as parameter to the constructor. */
4004 class mock_mapped_index : public mapped_index_base
4005 {
4006 public:
4007 mock_mapped_index (gdb::array_view<const char *> symbols)
4008 : m_symbol_table (symbols)
4009 {}
4010
4011 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4012
4013 /* Return the number of names in the symbol table. */
4014 size_t symbol_name_count () const override
4015 {
4016 return m_symbol_table.size ();
4017 }
4018
4019 /* Get the name of the symbol at IDX in the symbol table. */
4020 const char *symbol_name_at (offset_type idx) const override
4021 {
4022 return m_symbol_table[idx];
4023 }
4024
4025 private:
4026 gdb::array_view<const char *> m_symbol_table;
4027 };
4028
4029 /* Convenience function that converts a NULL pointer to a "<null>"
4030 string, to pass to print routines. */
4031
4032 static const char *
4033 string_or_null (const char *str)
4034 {
4035 return str != NULL ? str : "<null>";
4036 }
4037
4038 /* Check if a lookup_name_info built from
4039 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4040 index. EXPECTED_LIST is the list of expected matches, in expected
4041 matching order. If no match expected, then an empty list is
4042 specified. Returns true on success. On failure prints a warning
4043 indicating the file:line that failed, and returns false. */
4044
4045 static bool
4046 check_match (const char *file, int line,
4047 mock_mapped_index &mock_index,
4048 const char *name, symbol_name_match_type match_type,
4049 bool completion_mode,
4050 std::initializer_list<const char *> expected_list)
4051 {
4052 lookup_name_info lookup_name (name, match_type, completion_mode);
4053
4054 bool matched = true;
4055
4056 auto mismatch = [&] (const char *expected_str,
4057 const char *got)
4058 {
4059 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4060 "expected=\"%s\", got=\"%s\"\n"),
4061 file, line,
4062 (match_type == symbol_name_match_type::FULL
4063 ? "FULL" : "WILD"),
4064 name, string_or_null (expected_str), string_or_null (got));
4065 matched = false;
4066 };
4067
4068 auto expected_it = expected_list.begin ();
4069 auto expected_end = expected_list.end ();
4070
4071 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4072 NULL, ALL_DOMAIN,
4073 [&] (offset_type idx)
4074 {
4075 const char *matched_name = mock_index.symbol_name_at (idx);
4076 const char *expected_str
4077 = expected_it == expected_end ? NULL : *expected_it++;
4078
4079 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4080 mismatch (expected_str, matched_name);
4081 return true;
4082 });
4083
4084 const char *expected_str
4085 = expected_it == expected_end ? NULL : *expected_it++;
4086 if (expected_str != NULL)
4087 mismatch (expected_str, NULL);
4088
4089 return matched;
4090 }
4091
4092 /* The symbols added to the mock mapped_index for testing (in
4093 canonical form). */
4094 static const char *test_symbols[] = {
4095 "function",
4096 "std::bar",
4097 "std::zfunction",
4098 "std::zfunction2",
4099 "w1::w2",
4100 "ns::foo<char*>",
4101 "ns::foo<int>",
4102 "ns::foo<long>",
4103 "ns2::tmpl<int>::foo2",
4104 "(anonymous namespace)::A::B::C",
4105
4106 /* These are used to check that the increment-last-char in the
4107 matching algorithm for completion doesn't match "t1_fund" when
4108 completing "t1_func". */
4109 "t1_func",
4110 "t1_func1",
4111 "t1_fund",
4112 "t1_fund1",
4113
4114 /* A UTF-8 name with multi-byte sequences to make sure that
4115 cp-name-parser understands this as a single identifier ("função"
4116 is "function" in PT). */
4117 u8"u8função",
4118
4119 /* \377 (0xff) is Latin1 'ÿ'. */
4120 "yfunc\377",
4121
4122 /* \377 (0xff) is Latin1 'ÿ'. */
4123 "\377",
4124 "\377\377123",
4125
4126 /* A name with all sorts of complications. Starts with "z" to make
4127 it easier for the completion tests below. */
4128 #define Z_SYM_NAME \
4129 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4130 "::tuple<(anonymous namespace)::ui*, " \
4131 "std::default_delete<(anonymous namespace)::ui>, void>"
4132
4133 Z_SYM_NAME
4134 };
4135
4136 /* Returns true if the mapped_index_base::find_name_component_bounds
4137 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4138 in completion mode. */
4139
4140 static bool
4141 check_find_bounds_finds (mapped_index_base &index,
4142 const char *search_name,
4143 gdb::array_view<const char *> expected_syms)
4144 {
4145 lookup_name_info lookup_name (search_name,
4146 symbol_name_match_type::FULL, true);
4147
4148 auto bounds = index.find_name_components_bounds (lookup_name,
4149 language_cplus);
4150
4151 size_t distance = std::distance (bounds.first, bounds.second);
4152 if (distance != expected_syms.size ())
4153 return false;
4154
4155 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4156 {
4157 auto nc_elem = bounds.first + exp_elem;
4158 const char *qualified = index.symbol_name_at (nc_elem->idx);
4159 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4160 return false;
4161 }
4162
4163 return true;
4164 }
4165
4166 /* Test the lower-level mapped_index::find_name_component_bounds
4167 method. */
4168
4169 static void
4170 test_mapped_index_find_name_component_bounds ()
4171 {
4172 mock_mapped_index mock_index (test_symbols);
4173
4174 mock_index.build_name_components ();
4175
4176 /* Test the lower-level mapped_index::find_name_component_bounds
4177 method in completion mode. */
4178 {
4179 static const char *expected_syms[] = {
4180 "t1_func",
4181 "t1_func1",
4182 };
4183
4184 SELF_CHECK (check_find_bounds_finds (mock_index,
4185 "t1_func", expected_syms));
4186 }
4187
4188 /* Check that the increment-last-char in the name matching algorithm
4189 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4190 {
4191 static const char *expected_syms1[] = {
4192 "\377",
4193 "\377\377123",
4194 };
4195 SELF_CHECK (check_find_bounds_finds (mock_index,
4196 "\377", expected_syms1));
4197
4198 static const char *expected_syms2[] = {
4199 "\377\377123",
4200 };
4201 SELF_CHECK (check_find_bounds_finds (mock_index,
4202 "\377\377", expected_syms2));
4203 }
4204 }
4205
4206 /* Test dw2_expand_symtabs_matching_symbol. */
4207
4208 static void
4209 test_dw2_expand_symtabs_matching_symbol ()
4210 {
4211 mock_mapped_index mock_index (test_symbols);
4212
4213 /* We let all tests run until the end even if some fails, for debug
4214 convenience. */
4215 bool any_mismatch = false;
4216
4217 /* Create the expected symbols list (an initializer_list). Needed
4218 because lists have commas, and we need to pass them to CHECK,
4219 which is a macro. */
4220 #define EXPECT(...) { __VA_ARGS__ }
4221
4222 /* Wrapper for check_match that passes down the current
4223 __FILE__/__LINE__. */
4224 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4225 any_mismatch |= !check_match (__FILE__, __LINE__, \
4226 mock_index, \
4227 NAME, MATCH_TYPE, COMPLETION_MODE, \
4228 EXPECTED_LIST)
4229
4230 /* Identity checks. */
4231 for (const char *sym : test_symbols)
4232 {
4233 /* Should be able to match all existing symbols. */
4234 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4235 EXPECT (sym));
4236
4237 /* Should be able to match all existing symbols with
4238 parameters. */
4239 std::string with_params = std::string (sym) + "(int)";
4240 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4241 EXPECT (sym));
4242
4243 /* Should be able to match all existing symbols with
4244 parameters and qualifiers. */
4245 with_params = std::string (sym) + " ( int ) const";
4246 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4247 EXPECT (sym));
4248
4249 /* This should really find sym, but cp-name-parser.y doesn't
4250 know about lvalue/rvalue qualifiers yet. */
4251 with_params = std::string (sym) + " ( int ) &&";
4252 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4253 {});
4254 }
4255
4256 /* Check that the name matching algorithm for completion doesn't get
4257 confused with Latin1 'ÿ' / 0xff. */
4258 {
4259 static const char str[] = "\377";
4260 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4261 EXPECT ("\377", "\377\377123"));
4262 }
4263
4264 /* Check that the increment-last-char in the matching algorithm for
4265 completion doesn't match "t1_fund" when completing "t1_func". */
4266 {
4267 static const char str[] = "t1_func";
4268 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4269 EXPECT ("t1_func", "t1_func1"));
4270 }
4271
4272 /* Check that completion mode works at each prefix of the expected
4273 symbol name. */
4274 {
4275 static const char str[] = "function(int)";
4276 size_t len = strlen (str);
4277 std::string lookup;
4278
4279 for (size_t i = 1; i < len; i++)
4280 {
4281 lookup.assign (str, i);
4282 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4283 EXPECT ("function"));
4284 }
4285 }
4286
4287 /* While "w" is a prefix of both components, the match function
4288 should still only be called once. */
4289 {
4290 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4291 EXPECT ("w1::w2"));
4292 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4293 EXPECT ("w1::w2"));
4294 }
4295
4296 /* Same, with a "complicated" symbol. */
4297 {
4298 static const char str[] = Z_SYM_NAME;
4299 size_t len = strlen (str);
4300 std::string lookup;
4301
4302 for (size_t i = 1; i < len; i++)
4303 {
4304 lookup.assign (str, i);
4305 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4306 EXPECT (Z_SYM_NAME));
4307 }
4308 }
4309
4310 /* In FULL mode, an incomplete symbol doesn't match. */
4311 {
4312 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4313 {});
4314 }
4315
4316 /* A complete symbol with parameters matches any overload, since the
4317 index has no overload info. */
4318 {
4319 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4320 EXPECT ("std::zfunction", "std::zfunction2"));
4321 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4322 EXPECT ("std::zfunction", "std::zfunction2"));
4323 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4324 EXPECT ("std::zfunction", "std::zfunction2"));
4325 }
4326
4327 /* Check that whitespace is ignored appropriately. A symbol with a
4328 template argument list. */
4329 {
4330 static const char expected[] = "ns::foo<int>";
4331 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4332 EXPECT (expected));
4333 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4334 EXPECT (expected));
4335 }
4336
4337 /* Check that whitespace is ignored appropriately. A symbol with a
4338 template argument list that includes a pointer. */
4339 {
4340 static const char expected[] = "ns::foo<char*>";
4341 /* Try both completion and non-completion modes. */
4342 static const bool completion_mode[2] = {false, true};
4343 for (size_t i = 0; i < 2; i++)
4344 {
4345 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4346 completion_mode[i], EXPECT (expected));
4347 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4348 completion_mode[i], EXPECT (expected));
4349
4350 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4351 completion_mode[i], EXPECT (expected));
4352 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4353 completion_mode[i], EXPECT (expected));
4354 }
4355 }
4356
4357 {
4358 /* Check method qualifiers are ignored. */
4359 static const char expected[] = "ns::foo<char*>";
4360 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4361 symbol_name_match_type::FULL, true, EXPECT (expected));
4362 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4363 symbol_name_match_type::FULL, true, EXPECT (expected));
4364 CHECK_MATCH ("foo < char * > ( int ) const",
4365 symbol_name_match_type::WILD, true, EXPECT (expected));
4366 CHECK_MATCH ("foo < char * > ( int ) &&",
4367 symbol_name_match_type::WILD, true, EXPECT (expected));
4368 }
4369
4370 /* Test lookup names that don't match anything. */
4371 {
4372 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4373 {});
4374
4375 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4376 {});
4377 }
4378
4379 /* Some wild matching tests, exercising "(anonymous namespace)",
4380 which should not be confused with a parameter list. */
4381 {
4382 static const char *syms[] = {
4383 "A::B::C",
4384 "B::C",
4385 "C",
4386 "A :: B :: C ( int )",
4387 "B :: C ( int )",
4388 "C ( int )",
4389 };
4390
4391 for (const char *s : syms)
4392 {
4393 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4394 EXPECT ("(anonymous namespace)::A::B::C"));
4395 }
4396 }
4397
4398 {
4399 static const char expected[] = "ns2::tmpl<int>::foo2";
4400 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4401 EXPECT (expected));
4402 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4403 EXPECT (expected));
4404 }
4405
4406 SELF_CHECK (!any_mismatch);
4407
4408 #undef EXPECT
4409 #undef CHECK_MATCH
4410 }
4411
4412 static void
4413 run_test ()
4414 {
4415 test_mapped_index_find_name_component_bounds ();
4416 test_dw2_expand_symtabs_matching_symbol ();
4417 }
4418
4419 }} // namespace selftests::dw2_expand_symtabs_matching
4420
4421 #endif /* GDB_SELF_TEST */
4422
4423 /* If FILE_MATCHER is NULL or if PER_CU has
4424 dwarf2_per_cu_quick_data::MARK set (see
4425 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4426 EXPANSION_NOTIFY on it. */
4427
4428 static void
4429 dw2_expand_symtabs_matching_one
4430 (struct dwarf2_per_cu_data *per_cu,
4431 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4432 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4433 {
4434 if (file_matcher == NULL || per_cu->v.quick->mark)
4435 {
4436 bool symtab_was_null
4437 = (per_cu->v.quick->compunit_symtab == NULL);
4438
4439 dw2_instantiate_symtab (per_cu, false);
4440
4441 if (expansion_notify != NULL
4442 && symtab_was_null
4443 && per_cu->v.quick->compunit_symtab != NULL)
4444 expansion_notify (per_cu->v.quick->compunit_symtab);
4445 }
4446 }
4447
4448 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4449 matched, to expand corresponding CUs that were marked. IDX is the
4450 index of the symbol name that matched. */
4451
4452 static void
4453 dw2_expand_marked_cus
4454 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4455 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4456 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4457 search_domain kind)
4458 {
4459 offset_type *vec, vec_len, vec_idx;
4460 bool global_seen = false;
4461 mapped_index &index = *dwarf2_per_objfile->index_table;
4462
4463 vec = (offset_type *) (index.constant_pool
4464 + MAYBE_SWAP (index.symbol_table[idx].vec));
4465 vec_len = MAYBE_SWAP (vec[0]);
4466 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4467 {
4468 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4469 /* This value is only valid for index versions >= 7. */
4470 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4471 gdb_index_symbol_kind symbol_kind =
4472 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4473 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4474 /* Only check the symbol attributes if they're present.
4475 Indices prior to version 7 don't record them,
4476 and indices >= 7 may elide them for certain symbols
4477 (gold does this). */
4478 int attrs_valid =
4479 (index.version >= 7
4480 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4481
4482 /* Work around gold/15646. */
4483 if (attrs_valid)
4484 {
4485 if (!is_static && global_seen)
4486 continue;
4487 if (!is_static)
4488 global_seen = true;
4489 }
4490
4491 /* Only check the symbol's kind if it has one. */
4492 if (attrs_valid)
4493 {
4494 switch (kind)
4495 {
4496 case VARIABLES_DOMAIN:
4497 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4498 continue;
4499 break;
4500 case FUNCTIONS_DOMAIN:
4501 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4502 continue;
4503 break;
4504 case TYPES_DOMAIN:
4505 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4506 continue;
4507 break;
4508 case MODULES_DOMAIN:
4509 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4510 continue;
4511 break;
4512 default:
4513 break;
4514 }
4515 }
4516
4517 /* Don't crash on bad data. */
4518 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4519 + dwarf2_per_objfile->all_type_units.size ()))
4520 {
4521 complaint (_(".gdb_index entry has bad CU index"
4522 " [in module %s]"),
4523 objfile_name (dwarf2_per_objfile->objfile));
4524 continue;
4525 }
4526
4527 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4528 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4529 expansion_notify);
4530 }
4531 }
4532
4533 /* If FILE_MATCHER is non-NULL, set all the
4534 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4535 that match FILE_MATCHER. */
4536
4537 static void
4538 dw_expand_symtabs_matching_file_matcher
4539 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4540 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4541 {
4542 if (file_matcher == NULL)
4543 return;
4544
4545 objfile *const objfile = dwarf2_per_objfile->objfile;
4546
4547 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4548 htab_eq_pointer,
4549 NULL, xcalloc, xfree));
4550 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4551 htab_eq_pointer,
4552 NULL, xcalloc, xfree));
4553
4554 /* The rule is CUs specify all the files, including those used by
4555 any TU, so there's no need to scan TUs here. */
4556
4557 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4558 {
4559 QUIT;
4560
4561 per_cu->v.quick->mark = 0;
4562
4563 /* We only need to look at symtabs not already expanded. */
4564 if (per_cu->v.quick->compunit_symtab)
4565 continue;
4566
4567 quick_file_names *file_data = dw2_get_file_names (per_cu);
4568 if (file_data == NULL)
4569 continue;
4570
4571 if (htab_find (visited_not_found.get (), file_data) != NULL)
4572 continue;
4573 else if (htab_find (visited_found.get (), file_data) != NULL)
4574 {
4575 per_cu->v.quick->mark = 1;
4576 continue;
4577 }
4578
4579 for (int j = 0; j < file_data->num_file_names; ++j)
4580 {
4581 const char *this_real_name;
4582
4583 if (file_matcher (file_data->file_names[j], false))
4584 {
4585 per_cu->v.quick->mark = 1;
4586 break;
4587 }
4588
4589 /* Before we invoke realpath, which can get expensive when many
4590 files are involved, do a quick comparison of the basenames. */
4591 if (!basenames_may_differ
4592 && !file_matcher (lbasename (file_data->file_names[j]),
4593 true))
4594 continue;
4595
4596 this_real_name = dw2_get_real_path (objfile, file_data, j);
4597 if (file_matcher (this_real_name, false))
4598 {
4599 per_cu->v.quick->mark = 1;
4600 break;
4601 }
4602 }
4603
4604 void **slot = htab_find_slot (per_cu->v.quick->mark
4605 ? visited_found.get ()
4606 : visited_not_found.get (),
4607 file_data, INSERT);
4608 *slot = file_data;
4609 }
4610 }
4611
4612 static void
4613 dw2_expand_symtabs_matching
4614 (struct objfile *objfile,
4615 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4616 const lookup_name_info &lookup_name,
4617 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4618 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4619 enum search_domain kind)
4620 {
4621 struct dwarf2_per_objfile *dwarf2_per_objfile
4622 = get_dwarf2_per_objfile (objfile);
4623
4624 /* index_table is NULL if OBJF_READNOW. */
4625 if (!dwarf2_per_objfile->index_table)
4626 return;
4627
4628 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4629
4630 mapped_index &index = *dwarf2_per_objfile->index_table;
4631
4632 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4633 symbol_matcher,
4634 kind, [&] (offset_type idx)
4635 {
4636 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4637 expansion_notify, kind);
4638 return true;
4639 });
4640 }
4641
4642 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4643 symtab. */
4644
4645 static struct compunit_symtab *
4646 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4647 CORE_ADDR pc)
4648 {
4649 int i;
4650
4651 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4652 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4653 return cust;
4654
4655 if (cust->includes == NULL)
4656 return NULL;
4657
4658 for (i = 0; cust->includes[i]; ++i)
4659 {
4660 struct compunit_symtab *s = cust->includes[i];
4661
4662 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4663 if (s != NULL)
4664 return s;
4665 }
4666
4667 return NULL;
4668 }
4669
4670 static struct compunit_symtab *
4671 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4672 struct bound_minimal_symbol msymbol,
4673 CORE_ADDR pc,
4674 struct obj_section *section,
4675 int warn_if_readin)
4676 {
4677 struct dwarf2_per_cu_data *data;
4678 struct compunit_symtab *result;
4679
4680 if (!objfile->partial_symtabs->psymtabs_addrmap)
4681 return NULL;
4682
4683 CORE_ADDR baseaddr = objfile->text_section_offset ();
4684 data = (struct dwarf2_per_cu_data *) addrmap_find
4685 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4686 if (!data)
4687 return NULL;
4688
4689 if (warn_if_readin && data->v.quick->compunit_symtab)
4690 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4691 paddress (get_objfile_arch (objfile), pc));
4692
4693 result
4694 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4695 false),
4696 pc);
4697 gdb_assert (result != NULL);
4698 return result;
4699 }
4700
4701 static void
4702 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4703 void *data, int need_fullname)
4704 {
4705 struct dwarf2_per_objfile *dwarf2_per_objfile
4706 = get_dwarf2_per_objfile (objfile);
4707
4708 if (!dwarf2_per_objfile->filenames_cache)
4709 {
4710 dwarf2_per_objfile->filenames_cache.emplace ();
4711
4712 htab_up visited (htab_create_alloc (10,
4713 htab_hash_pointer, htab_eq_pointer,
4714 NULL, xcalloc, xfree));
4715
4716 /* The rule is CUs specify all the files, including those used
4717 by any TU, so there's no need to scan TUs here. We can
4718 ignore file names coming from already-expanded CUs. */
4719
4720 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4721 {
4722 if (per_cu->v.quick->compunit_symtab)
4723 {
4724 void **slot = htab_find_slot (visited.get (),
4725 per_cu->v.quick->file_names,
4726 INSERT);
4727
4728 *slot = per_cu->v.quick->file_names;
4729 }
4730 }
4731
4732 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4733 {
4734 /* We only need to look at symtabs not already expanded. */
4735 if (per_cu->v.quick->compunit_symtab)
4736 continue;
4737
4738 quick_file_names *file_data = dw2_get_file_names (per_cu);
4739 if (file_data == NULL)
4740 continue;
4741
4742 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4743 if (*slot)
4744 {
4745 /* Already visited. */
4746 continue;
4747 }
4748 *slot = file_data;
4749
4750 for (int j = 0; j < file_data->num_file_names; ++j)
4751 {
4752 const char *filename = file_data->file_names[j];
4753 dwarf2_per_objfile->filenames_cache->seen (filename);
4754 }
4755 }
4756 }
4757
4758 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4759 {
4760 gdb::unique_xmalloc_ptr<char> this_real_name;
4761
4762 if (need_fullname)
4763 this_real_name = gdb_realpath (filename);
4764 (*fun) (filename, this_real_name.get (), data);
4765 });
4766 }
4767
4768 static int
4769 dw2_has_symbols (struct objfile *objfile)
4770 {
4771 return 1;
4772 }
4773
4774 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4775 {
4776 dw2_has_symbols,
4777 dw2_find_last_source_symtab,
4778 dw2_forget_cached_source_info,
4779 dw2_map_symtabs_matching_filename,
4780 dw2_lookup_symbol,
4781 dw2_print_stats,
4782 dw2_dump,
4783 dw2_expand_symtabs_for_function,
4784 dw2_expand_all_symtabs,
4785 dw2_expand_symtabs_with_fullname,
4786 dw2_map_matching_symbols,
4787 dw2_expand_symtabs_matching,
4788 dw2_find_pc_sect_compunit_symtab,
4789 NULL,
4790 dw2_map_symbol_filenames
4791 };
4792
4793 /* DWARF-5 debug_names reader. */
4794
4795 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4796 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4797
4798 /* A helper function that reads the .debug_names section in SECTION
4799 and fills in MAP. FILENAME is the name of the file containing the
4800 section; it is used for error reporting.
4801
4802 Returns true if all went well, false otherwise. */
4803
4804 static bool
4805 read_debug_names_from_section (struct objfile *objfile,
4806 const char *filename,
4807 struct dwarf2_section_info *section,
4808 mapped_debug_names &map)
4809 {
4810 if (section->empty ())
4811 return false;
4812
4813 /* Older elfutils strip versions could keep the section in the main
4814 executable while splitting it for the separate debug info file. */
4815 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4816 return false;
4817
4818 section->read (objfile);
4819
4820 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4821
4822 const gdb_byte *addr = section->buffer;
4823
4824 bfd *const abfd = section->get_bfd_owner ();
4825
4826 unsigned int bytes_read;
4827 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4828 addr += bytes_read;
4829
4830 map.dwarf5_is_dwarf64 = bytes_read != 4;
4831 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4832 if (bytes_read + length != section->size)
4833 {
4834 /* There may be multiple per-CU indices. */
4835 warning (_("Section .debug_names in %s length %s does not match "
4836 "section length %s, ignoring .debug_names."),
4837 filename, plongest (bytes_read + length),
4838 pulongest (section->size));
4839 return false;
4840 }
4841
4842 /* The version number. */
4843 uint16_t version = read_2_bytes (abfd, addr);
4844 addr += 2;
4845 if (version != 5)
4846 {
4847 warning (_("Section .debug_names in %s has unsupported version %d, "
4848 "ignoring .debug_names."),
4849 filename, version);
4850 return false;
4851 }
4852
4853 /* Padding. */
4854 uint16_t padding = read_2_bytes (abfd, addr);
4855 addr += 2;
4856 if (padding != 0)
4857 {
4858 warning (_("Section .debug_names in %s has unsupported padding %d, "
4859 "ignoring .debug_names."),
4860 filename, padding);
4861 return false;
4862 }
4863
4864 /* comp_unit_count - The number of CUs in the CU list. */
4865 map.cu_count = read_4_bytes (abfd, addr);
4866 addr += 4;
4867
4868 /* local_type_unit_count - The number of TUs in the local TU
4869 list. */
4870 map.tu_count = read_4_bytes (abfd, addr);
4871 addr += 4;
4872
4873 /* foreign_type_unit_count - The number of TUs in the foreign TU
4874 list. */
4875 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4876 addr += 4;
4877 if (foreign_tu_count != 0)
4878 {
4879 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4880 "ignoring .debug_names."),
4881 filename, static_cast<unsigned long> (foreign_tu_count));
4882 return false;
4883 }
4884
4885 /* bucket_count - The number of hash buckets in the hash lookup
4886 table. */
4887 map.bucket_count = read_4_bytes (abfd, addr);
4888 addr += 4;
4889
4890 /* name_count - The number of unique names in the index. */
4891 map.name_count = read_4_bytes (abfd, addr);
4892 addr += 4;
4893
4894 /* abbrev_table_size - The size in bytes of the abbreviations
4895 table. */
4896 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4897 addr += 4;
4898
4899 /* augmentation_string_size - The size in bytes of the augmentation
4900 string. This value is rounded up to a multiple of 4. */
4901 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4902 addr += 4;
4903 map.augmentation_is_gdb = ((augmentation_string_size
4904 == sizeof (dwarf5_augmentation))
4905 && memcmp (addr, dwarf5_augmentation,
4906 sizeof (dwarf5_augmentation)) == 0);
4907 augmentation_string_size += (-augmentation_string_size) & 3;
4908 addr += augmentation_string_size;
4909
4910 /* List of CUs */
4911 map.cu_table_reordered = addr;
4912 addr += map.cu_count * map.offset_size;
4913
4914 /* List of Local TUs */
4915 map.tu_table_reordered = addr;
4916 addr += map.tu_count * map.offset_size;
4917
4918 /* Hash Lookup Table */
4919 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4920 addr += map.bucket_count * 4;
4921 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4922 addr += map.name_count * 4;
4923
4924 /* Name Table */
4925 map.name_table_string_offs_reordered = addr;
4926 addr += map.name_count * map.offset_size;
4927 map.name_table_entry_offs_reordered = addr;
4928 addr += map.name_count * map.offset_size;
4929
4930 const gdb_byte *abbrev_table_start = addr;
4931 for (;;)
4932 {
4933 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4934 addr += bytes_read;
4935 if (index_num == 0)
4936 break;
4937
4938 const auto insertpair
4939 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4940 if (!insertpair.second)
4941 {
4942 warning (_("Section .debug_names in %s has duplicate index %s, "
4943 "ignoring .debug_names."),
4944 filename, pulongest (index_num));
4945 return false;
4946 }
4947 mapped_debug_names::index_val &indexval = insertpair.first->second;
4948 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4949 addr += bytes_read;
4950
4951 for (;;)
4952 {
4953 mapped_debug_names::index_val::attr attr;
4954 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4955 addr += bytes_read;
4956 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4957 addr += bytes_read;
4958 if (attr.form == DW_FORM_implicit_const)
4959 {
4960 attr.implicit_const = read_signed_leb128 (abfd, addr,
4961 &bytes_read);
4962 addr += bytes_read;
4963 }
4964 if (attr.dw_idx == 0 && attr.form == 0)
4965 break;
4966 indexval.attr_vec.push_back (std::move (attr));
4967 }
4968 }
4969 if (addr != abbrev_table_start + abbrev_table_size)
4970 {
4971 warning (_("Section .debug_names in %s has abbreviation_table "
4972 "of size %s vs. written as %u, ignoring .debug_names."),
4973 filename, plongest (addr - abbrev_table_start),
4974 abbrev_table_size);
4975 return false;
4976 }
4977 map.entry_pool = addr;
4978
4979 return true;
4980 }
4981
4982 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4983 list. */
4984
4985 static void
4986 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4987 const mapped_debug_names &map,
4988 dwarf2_section_info &section,
4989 bool is_dwz)
4990 {
4991 sect_offset sect_off_prev;
4992 for (uint32_t i = 0; i <= map.cu_count; ++i)
4993 {
4994 sect_offset sect_off_next;
4995 if (i < map.cu_count)
4996 {
4997 sect_off_next
4998 = (sect_offset) (extract_unsigned_integer
4999 (map.cu_table_reordered + i * map.offset_size,
5000 map.offset_size,
5001 map.dwarf5_byte_order));
5002 }
5003 else
5004 sect_off_next = (sect_offset) section.size;
5005 if (i >= 1)
5006 {
5007 const ULONGEST length = sect_off_next - sect_off_prev;
5008 dwarf2_per_cu_data *per_cu
5009 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5010 sect_off_prev, length);
5011 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5012 }
5013 sect_off_prev = sect_off_next;
5014 }
5015 }
5016
5017 /* Read the CU list from the mapped index, and use it to create all
5018 the CU objects for this dwarf2_per_objfile. */
5019
5020 static void
5021 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5022 const mapped_debug_names &map,
5023 const mapped_debug_names &dwz_map)
5024 {
5025 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5026 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5027
5028 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5029 dwarf2_per_objfile->info,
5030 false /* is_dwz */);
5031
5032 if (dwz_map.cu_count == 0)
5033 return;
5034
5035 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5036 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5037 true /* is_dwz */);
5038 }
5039
5040 /* Read .debug_names. If everything went ok, initialize the "quick"
5041 elements of all the CUs and return true. Otherwise, return false. */
5042
5043 static bool
5044 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5045 {
5046 std::unique_ptr<mapped_debug_names> map
5047 (new mapped_debug_names (dwarf2_per_objfile));
5048 mapped_debug_names dwz_map (dwarf2_per_objfile);
5049 struct objfile *objfile = dwarf2_per_objfile->objfile;
5050
5051 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5052 &dwarf2_per_objfile->debug_names,
5053 *map))
5054 return false;
5055
5056 /* Don't use the index if it's empty. */
5057 if (map->name_count == 0)
5058 return false;
5059
5060 /* If there is a .dwz file, read it so we can get its CU list as
5061 well. */
5062 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5063 if (dwz != NULL)
5064 {
5065 if (!read_debug_names_from_section (objfile,
5066 bfd_get_filename (dwz->dwz_bfd.get ()),
5067 &dwz->debug_names, dwz_map))
5068 {
5069 warning (_("could not read '.debug_names' section from %s; skipping"),
5070 bfd_get_filename (dwz->dwz_bfd.get ()));
5071 return false;
5072 }
5073 }
5074
5075 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5076
5077 if (map->tu_count != 0)
5078 {
5079 /* We can only handle a single .debug_types when we have an
5080 index. */
5081 if (dwarf2_per_objfile->types.size () != 1)
5082 return false;
5083
5084 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5085
5086 create_signatured_type_table_from_debug_names
5087 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5088 }
5089
5090 create_addrmap_from_aranges (dwarf2_per_objfile,
5091 &dwarf2_per_objfile->debug_aranges);
5092
5093 dwarf2_per_objfile->debug_names_table = std::move (map);
5094 dwarf2_per_objfile->using_index = 1;
5095 dwarf2_per_objfile->quick_file_names_table =
5096 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5097
5098 return true;
5099 }
5100
5101 /* Type used to manage iterating over all CUs looking for a symbol for
5102 .debug_names. */
5103
5104 class dw2_debug_names_iterator
5105 {
5106 public:
5107 dw2_debug_names_iterator (const mapped_debug_names &map,
5108 gdb::optional<block_enum> block_index,
5109 domain_enum domain,
5110 const char *name)
5111 : m_map (map), m_block_index (block_index), m_domain (domain),
5112 m_addr (find_vec_in_debug_names (map, name))
5113 {}
5114
5115 dw2_debug_names_iterator (const mapped_debug_names &map,
5116 search_domain search, uint32_t namei)
5117 : m_map (map),
5118 m_search (search),
5119 m_addr (find_vec_in_debug_names (map, namei))
5120 {}
5121
5122 dw2_debug_names_iterator (const mapped_debug_names &map,
5123 block_enum block_index, domain_enum domain,
5124 uint32_t namei)
5125 : m_map (map), m_block_index (block_index), m_domain (domain),
5126 m_addr (find_vec_in_debug_names (map, namei))
5127 {}
5128
5129 /* Return the next matching CU or NULL if there are no more. */
5130 dwarf2_per_cu_data *next ();
5131
5132 private:
5133 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5134 const char *name);
5135 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5136 uint32_t namei);
5137
5138 /* The internalized form of .debug_names. */
5139 const mapped_debug_names &m_map;
5140
5141 /* If set, only look for symbols that match that block. Valid values are
5142 GLOBAL_BLOCK and STATIC_BLOCK. */
5143 const gdb::optional<block_enum> m_block_index;
5144
5145 /* The kind of symbol we're looking for. */
5146 const domain_enum m_domain = UNDEF_DOMAIN;
5147 const search_domain m_search = ALL_DOMAIN;
5148
5149 /* The list of CUs from the index entry of the symbol, or NULL if
5150 not found. */
5151 const gdb_byte *m_addr;
5152 };
5153
5154 const char *
5155 mapped_debug_names::namei_to_name (uint32_t namei) const
5156 {
5157 const ULONGEST namei_string_offs
5158 = extract_unsigned_integer ((name_table_string_offs_reordered
5159 + namei * offset_size),
5160 offset_size,
5161 dwarf5_byte_order);
5162 return read_indirect_string_at_offset
5163 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5164 }
5165
5166 /* Find a slot in .debug_names for the object named NAME. If NAME is
5167 found, return pointer to its pool data. If NAME cannot be found,
5168 return NULL. */
5169
5170 const gdb_byte *
5171 dw2_debug_names_iterator::find_vec_in_debug_names
5172 (const mapped_debug_names &map, const char *name)
5173 {
5174 int (*cmp) (const char *, const char *);
5175
5176 gdb::unique_xmalloc_ptr<char> without_params;
5177 if (current_language->la_language == language_cplus
5178 || current_language->la_language == language_fortran
5179 || current_language->la_language == language_d)
5180 {
5181 /* NAME is already canonical. Drop any qualifiers as
5182 .debug_names does not contain any. */
5183
5184 if (strchr (name, '(') != NULL)
5185 {
5186 without_params = cp_remove_params (name);
5187 if (without_params != NULL)
5188 name = without_params.get ();
5189 }
5190 }
5191
5192 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5193
5194 const uint32_t full_hash = dwarf5_djb_hash (name);
5195 uint32_t namei
5196 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5197 (map.bucket_table_reordered
5198 + (full_hash % map.bucket_count)), 4,
5199 map.dwarf5_byte_order);
5200 if (namei == 0)
5201 return NULL;
5202 --namei;
5203 if (namei >= map.name_count)
5204 {
5205 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5206 "[in module %s]"),
5207 namei, map.name_count,
5208 objfile_name (map.dwarf2_per_objfile->objfile));
5209 return NULL;
5210 }
5211
5212 for (;;)
5213 {
5214 const uint32_t namei_full_hash
5215 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5216 (map.hash_table_reordered + namei), 4,
5217 map.dwarf5_byte_order);
5218 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5219 return NULL;
5220
5221 if (full_hash == namei_full_hash)
5222 {
5223 const char *const namei_string = map.namei_to_name (namei);
5224
5225 #if 0 /* An expensive sanity check. */
5226 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5227 {
5228 complaint (_("Wrong .debug_names hash for string at index %u "
5229 "[in module %s]"),
5230 namei, objfile_name (dwarf2_per_objfile->objfile));
5231 return NULL;
5232 }
5233 #endif
5234
5235 if (cmp (namei_string, name) == 0)
5236 {
5237 const ULONGEST namei_entry_offs
5238 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5239 + namei * map.offset_size),
5240 map.offset_size, map.dwarf5_byte_order);
5241 return map.entry_pool + namei_entry_offs;
5242 }
5243 }
5244
5245 ++namei;
5246 if (namei >= map.name_count)
5247 return NULL;
5248 }
5249 }
5250
5251 const gdb_byte *
5252 dw2_debug_names_iterator::find_vec_in_debug_names
5253 (const mapped_debug_names &map, uint32_t namei)
5254 {
5255 if (namei >= map.name_count)
5256 {
5257 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5258 "[in module %s]"),
5259 namei, map.name_count,
5260 objfile_name (map.dwarf2_per_objfile->objfile));
5261 return NULL;
5262 }
5263
5264 const ULONGEST namei_entry_offs
5265 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5266 + namei * map.offset_size),
5267 map.offset_size, map.dwarf5_byte_order);
5268 return map.entry_pool + namei_entry_offs;
5269 }
5270
5271 /* See dw2_debug_names_iterator. */
5272
5273 dwarf2_per_cu_data *
5274 dw2_debug_names_iterator::next ()
5275 {
5276 if (m_addr == NULL)
5277 return NULL;
5278
5279 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5280 struct objfile *objfile = dwarf2_per_objfile->objfile;
5281 bfd *const abfd = objfile->obfd;
5282
5283 again:
5284
5285 unsigned int bytes_read;
5286 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5287 m_addr += bytes_read;
5288 if (abbrev == 0)
5289 return NULL;
5290
5291 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5292 if (indexval_it == m_map.abbrev_map.cend ())
5293 {
5294 complaint (_("Wrong .debug_names undefined abbrev code %s "
5295 "[in module %s]"),
5296 pulongest (abbrev), objfile_name (objfile));
5297 return NULL;
5298 }
5299 const mapped_debug_names::index_val &indexval = indexval_it->second;
5300 enum class symbol_linkage {
5301 unknown,
5302 static_,
5303 extern_,
5304 } symbol_linkage_ = symbol_linkage::unknown;
5305 dwarf2_per_cu_data *per_cu = NULL;
5306 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5307 {
5308 ULONGEST ull;
5309 switch (attr.form)
5310 {
5311 case DW_FORM_implicit_const:
5312 ull = attr.implicit_const;
5313 break;
5314 case DW_FORM_flag_present:
5315 ull = 1;
5316 break;
5317 case DW_FORM_udata:
5318 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5319 m_addr += bytes_read;
5320 break;
5321 default:
5322 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5323 dwarf_form_name (attr.form),
5324 objfile_name (objfile));
5325 return NULL;
5326 }
5327 switch (attr.dw_idx)
5328 {
5329 case DW_IDX_compile_unit:
5330 /* Don't crash on bad data. */
5331 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5332 {
5333 complaint (_(".debug_names entry has bad CU index %s"
5334 " [in module %s]"),
5335 pulongest (ull),
5336 objfile_name (dwarf2_per_objfile->objfile));
5337 continue;
5338 }
5339 per_cu = dwarf2_per_objfile->get_cutu (ull);
5340 break;
5341 case DW_IDX_type_unit:
5342 /* Don't crash on bad data. */
5343 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5344 {
5345 complaint (_(".debug_names entry has bad TU index %s"
5346 " [in module %s]"),
5347 pulongest (ull),
5348 objfile_name (dwarf2_per_objfile->objfile));
5349 continue;
5350 }
5351 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5352 break;
5353 case DW_IDX_GNU_internal:
5354 if (!m_map.augmentation_is_gdb)
5355 break;
5356 symbol_linkage_ = symbol_linkage::static_;
5357 break;
5358 case DW_IDX_GNU_external:
5359 if (!m_map.augmentation_is_gdb)
5360 break;
5361 symbol_linkage_ = symbol_linkage::extern_;
5362 break;
5363 }
5364 }
5365
5366 /* Skip if already read in. */
5367 if (per_cu->v.quick->compunit_symtab)
5368 goto again;
5369
5370 /* Check static vs global. */
5371 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5372 {
5373 const bool want_static = *m_block_index == STATIC_BLOCK;
5374 const bool symbol_is_static =
5375 symbol_linkage_ == symbol_linkage::static_;
5376 if (want_static != symbol_is_static)
5377 goto again;
5378 }
5379
5380 /* Match dw2_symtab_iter_next, symbol_kind
5381 and debug_names::psymbol_tag. */
5382 switch (m_domain)
5383 {
5384 case VAR_DOMAIN:
5385 switch (indexval.dwarf_tag)
5386 {
5387 case DW_TAG_variable:
5388 case DW_TAG_subprogram:
5389 /* Some types are also in VAR_DOMAIN. */
5390 case DW_TAG_typedef:
5391 case DW_TAG_structure_type:
5392 break;
5393 default:
5394 goto again;
5395 }
5396 break;
5397 case STRUCT_DOMAIN:
5398 switch (indexval.dwarf_tag)
5399 {
5400 case DW_TAG_typedef:
5401 case DW_TAG_structure_type:
5402 break;
5403 default:
5404 goto again;
5405 }
5406 break;
5407 case LABEL_DOMAIN:
5408 switch (indexval.dwarf_tag)
5409 {
5410 case 0:
5411 case DW_TAG_variable:
5412 break;
5413 default:
5414 goto again;
5415 }
5416 break;
5417 case MODULE_DOMAIN:
5418 switch (indexval.dwarf_tag)
5419 {
5420 case DW_TAG_module:
5421 break;
5422 default:
5423 goto again;
5424 }
5425 break;
5426 default:
5427 break;
5428 }
5429
5430 /* Match dw2_expand_symtabs_matching, symbol_kind and
5431 debug_names::psymbol_tag. */
5432 switch (m_search)
5433 {
5434 case VARIABLES_DOMAIN:
5435 switch (indexval.dwarf_tag)
5436 {
5437 case DW_TAG_variable:
5438 break;
5439 default:
5440 goto again;
5441 }
5442 break;
5443 case FUNCTIONS_DOMAIN:
5444 switch (indexval.dwarf_tag)
5445 {
5446 case DW_TAG_subprogram:
5447 break;
5448 default:
5449 goto again;
5450 }
5451 break;
5452 case TYPES_DOMAIN:
5453 switch (indexval.dwarf_tag)
5454 {
5455 case DW_TAG_typedef:
5456 case DW_TAG_structure_type:
5457 break;
5458 default:
5459 goto again;
5460 }
5461 break;
5462 case MODULES_DOMAIN:
5463 switch (indexval.dwarf_tag)
5464 {
5465 case DW_TAG_module:
5466 break;
5467 default:
5468 goto again;
5469 }
5470 default:
5471 break;
5472 }
5473
5474 return per_cu;
5475 }
5476
5477 static struct compunit_symtab *
5478 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5479 const char *name, domain_enum domain)
5480 {
5481 struct dwarf2_per_objfile *dwarf2_per_objfile
5482 = get_dwarf2_per_objfile (objfile);
5483
5484 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5485 if (!mapp)
5486 {
5487 /* index is NULL if OBJF_READNOW. */
5488 return NULL;
5489 }
5490 const auto &map = *mapp;
5491
5492 dw2_debug_names_iterator iter (map, block_index, domain, name);
5493
5494 struct compunit_symtab *stab_best = NULL;
5495 struct dwarf2_per_cu_data *per_cu;
5496 while ((per_cu = iter.next ()) != NULL)
5497 {
5498 struct symbol *sym, *with_opaque = NULL;
5499 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5500 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5501 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5502
5503 sym = block_find_symbol (block, name, domain,
5504 block_find_non_opaque_type_preferred,
5505 &with_opaque);
5506
5507 /* Some caution must be observed with overloaded functions and
5508 methods, since the index will not contain any overload
5509 information (but NAME might contain it). */
5510
5511 if (sym != NULL
5512 && strcmp_iw (sym->search_name (), name) == 0)
5513 return stab;
5514 if (with_opaque != NULL
5515 && strcmp_iw (with_opaque->search_name (), name) == 0)
5516 stab_best = stab;
5517
5518 /* Keep looking through other CUs. */
5519 }
5520
5521 return stab_best;
5522 }
5523
5524 /* This dumps minimal information about .debug_names. It is called
5525 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5526 uses this to verify that .debug_names has been loaded. */
5527
5528 static void
5529 dw2_debug_names_dump (struct objfile *objfile)
5530 {
5531 struct dwarf2_per_objfile *dwarf2_per_objfile
5532 = get_dwarf2_per_objfile (objfile);
5533
5534 gdb_assert (dwarf2_per_objfile->using_index);
5535 printf_filtered (".debug_names:");
5536 if (dwarf2_per_objfile->debug_names_table)
5537 printf_filtered (" exists\n");
5538 else
5539 printf_filtered (" faked for \"readnow\"\n");
5540 printf_filtered ("\n");
5541 }
5542
5543 static void
5544 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5545 const char *func_name)
5546 {
5547 struct dwarf2_per_objfile *dwarf2_per_objfile
5548 = get_dwarf2_per_objfile (objfile);
5549
5550 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5551 if (dwarf2_per_objfile->debug_names_table)
5552 {
5553 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5554
5555 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5556
5557 struct dwarf2_per_cu_data *per_cu;
5558 while ((per_cu = iter.next ()) != NULL)
5559 dw2_instantiate_symtab (per_cu, false);
5560 }
5561 }
5562
5563 static void
5564 dw2_debug_names_map_matching_symbols
5565 (struct objfile *objfile,
5566 const lookup_name_info &name, domain_enum domain,
5567 int global,
5568 gdb::function_view<symbol_found_callback_ftype> callback,
5569 symbol_compare_ftype *ordered_compare)
5570 {
5571 struct dwarf2_per_objfile *dwarf2_per_objfile
5572 = get_dwarf2_per_objfile (objfile);
5573
5574 /* debug_names_table is NULL if OBJF_READNOW. */
5575 if (!dwarf2_per_objfile->debug_names_table)
5576 return;
5577
5578 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5579 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5580
5581 const char *match_name = name.ada ().lookup_name ().c_str ();
5582 auto matcher = [&] (const char *symname)
5583 {
5584 if (ordered_compare == nullptr)
5585 return true;
5586 return ordered_compare (symname, match_name) == 0;
5587 };
5588
5589 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5590 [&] (offset_type namei)
5591 {
5592 /* The name was matched, now expand corresponding CUs that were
5593 marked. */
5594 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5595
5596 struct dwarf2_per_cu_data *per_cu;
5597 while ((per_cu = iter.next ()) != NULL)
5598 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5599 return true;
5600 });
5601
5602 /* It's a shame we couldn't do this inside the
5603 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5604 that have already been expanded. Instead, this loop matches what
5605 the psymtab code does. */
5606 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5607 {
5608 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5609 if (cust != nullptr)
5610 {
5611 const struct block *block
5612 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5613 if (!iterate_over_symbols_terminated (block, name,
5614 domain, callback))
5615 break;
5616 }
5617 }
5618 }
5619
5620 static void
5621 dw2_debug_names_expand_symtabs_matching
5622 (struct objfile *objfile,
5623 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5624 const lookup_name_info &lookup_name,
5625 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5626 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5627 enum search_domain kind)
5628 {
5629 struct dwarf2_per_objfile *dwarf2_per_objfile
5630 = get_dwarf2_per_objfile (objfile);
5631
5632 /* debug_names_table is NULL if OBJF_READNOW. */
5633 if (!dwarf2_per_objfile->debug_names_table)
5634 return;
5635
5636 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5637
5638 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5639
5640 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5641 symbol_matcher,
5642 kind, [&] (offset_type namei)
5643 {
5644 /* The name was matched, now expand corresponding CUs that were
5645 marked. */
5646 dw2_debug_names_iterator iter (map, kind, namei);
5647
5648 struct dwarf2_per_cu_data *per_cu;
5649 while ((per_cu = iter.next ()) != NULL)
5650 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5651 expansion_notify);
5652 return true;
5653 });
5654 }
5655
5656 const struct quick_symbol_functions dwarf2_debug_names_functions =
5657 {
5658 dw2_has_symbols,
5659 dw2_find_last_source_symtab,
5660 dw2_forget_cached_source_info,
5661 dw2_map_symtabs_matching_filename,
5662 dw2_debug_names_lookup_symbol,
5663 dw2_print_stats,
5664 dw2_debug_names_dump,
5665 dw2_debug_names_expand_symtabs_for_function,
5666 dw2_expand_all_symtabs,
5667 dw2_expand_symtabs_with_fullname,
5668 dw2_debug_names_map_matching_symbols,
5669 dw2_debug_names_expand_symtabs_matching,
5670 dw2_find_pc_sect_compunit_symtab,
5671 NULL,
5672 dw2_map_symbol_filenames
5673 };
5674
5675 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5676 to either a dwarf2_per_objfile or dwz_file object. */
5677
5678 template <typename T>
5679 static gdb::array_view<const gdb_byte>
5680 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5681 {
5682 dwarf2_section_info *section = &section_owner->gdb_index;
5683
5684 if (section->empty ())
5685 return {};
5686
5687 /* Older elfutils strip versions could keep the section in the main
5688 executable while splitting it for the separate debug info file. */
5689 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5690 return {};
5691
5692 section->read (obj);
5693
5694 /* dwarf2_section_info::size is a bfd_size_type, while
5695 gdb::array_view works with size_t. On 32-bit hosts, with
5696 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5697 is 32-bit. So we need an explicit narrowing conversion here.
5698 This is fine, because it's impossible to allocate or mmap an
5699 array/buffer larger than what size_t can represent. */
5700 return gdb::make_array_view (section->buffer, section->size);
5701 }
5702
5703 /* Lookup the index cache for the contents of the index associated to
5704 DWARF2_OBJ. */
5705
5706 static gdb::array_view<const gdb_byte>
5707 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5708 {
5709 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5710 if (build_id == nullptr)
5711 return {};
5712
5713 return global_index_cache.lookup_gdb_index (build_id,
5714 &dwarf2_obj->index_cache_res);
5715 }
5716
5717 /* Same as the above, but for DWZ. */
5718
5719 static gdb::array_view<const gdb_byte>
5720 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5721 {
5722 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5723 if (build_id == nullptr)
5724 return {};
5725
5726 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5727 }
5728
5729 /* See symfile.h. */
5730
5731 bool
5732 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5733 {
5734 struct dwarf2_per_objfile *dwarf2_per_objfile
5735 = get_dwarf2_per_objfile (objfile);
5736
5737 /* If we're about to read full symbols, don't bother with the
5738 indices. In this case we also don't care if some other debug
5739 format is making psymtabs, because they are all about to be
5740 expanded anyway. */
5741 if ((objfile->flags & OBJF_READNOW))
5742 {
5743 dwarf2_per_objfile->using_index = 1;
5744 create_all_comp_units (dwarf2_per_objfile);
5745 create_all_type_units (dwarf2_per_objfile);
5746 dwarf2_per_objfile->quick_file_names_table
5747 = create_quick_file_names_table
5748 (dwarf2_per_objfile->all_comp_units.size ());
5749
5750 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5751 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5752 {
5753 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5754
5755 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5756 struct dwarf2_per_cu_quick_data);
5757 }
5758
5759 /* Return 1 so that gdb sees the "quick" functions. However,
5760 these functions will be no-ops because we will have expanded
5761 all symtabs. */
5762 *index_kind = dw_index_kind::GDB_INDEX;
5763 return true;
5764 }
5765
5766 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5767 {
5768 *index_kind = dw_index_kind::DEBUG_NAMES;
5769 return true;
5770 }
5771
5772 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5773 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5774 get_gdb_index_contents_from_section<dwz_file>))
5775 {
5776 *index_kind = dw_index_kind::GDB_INDEX;
5777 return true;
5778 }
5779
5780 /* ... otherwise, try to find the index in the index cache. */
5781 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5782 get_gdb_index_contents_from_cache,
5783 get_gdb_index_contents_from_cache_dwz))
5784 {
5785 global_index_cache.hit ();
5786 *index_kind = dw_index_kind::GDB_INDEX;
5787 return true;
5788 }
5789
5790 global_index_cache.miss ();
5791 return false;
5792 }
5793
5794 \f
5795
5796 /* Build a partial symbol table. */
5797
5798 void
5799 dwarf2_build_psymtabs (struct objfile *objfile)
5800 {
5801 struct dwarf2_per_objfile *dwarf2_per_objfile
5802 = get_dwarf2_per_objfile (objfile);
5803
5804 init_psymbol_list (objfile, 1024);
5805
5806 try
5807 {
5808 /* This isn't really ideal: all the data we allocate on the
5809 objfile's obstack is still uselessly kept around. However,
5810 freeing it seems unsafe. */
5811 psymtab_discarder psymtabs (objfile);
5812 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5813 psymtabs.keep ();
5814
5815 /* (maybe) store an index in the cache. */
5816 global_index_cache.store (dwarf2_per_objfile);
5817 }
5818 catch (const gdb_exception_error &except)
5819 {
5820 exception_print (gdb_stderr, except);
5821 }
5822 }
5823
5824 /* Find the base address of the compilation unit for range lists and
5825 location lists. It will normally be specified by DW_AT_low_pc.
5826 In DWARF-3 draft 4, the base address could be overridden by
5827 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5828 compilation units with discontinuous ranges. */
5829
5830 static void
5831 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5832 {
5833 struct attribute *attr;
5834
5835 cu->base_known = 0;
5836 cu->base_address = 0;
5837
5838 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5839 if (attr != nullptr)
5840 {
5841 cu->base_address = attr->value_as_address ();
5842 cu->base_known = 1;
5843 }
5844 else
5845 {
5846 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5847 if (attr != nullptr)
5848 {
5849 cu->base_address = attr->value_as_address ();
5850 cu->base_known = 1;
5851 }
5852 }
5853 }
5854
5855 /* Helper function that returns the proper abbrev section for
5856 THIS_CU. */
5857
5858 static struct dwarf2_section_info *
5859 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5860 {
5861 struct dwarf2_section_info *abbrev;
5862 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5863
5864 if (this_cu->is_dwz)
5865 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5866 else
5867 abbrev = &dwarf2_per_objfile->abbrev;
5868
5869 return abbrev;
5870 }
5871
5872 /* Fetch the abbreviation table offset from a comp or type unit header. */
5873
5874 static sect_offset
5875 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5876 struct dwarf2_section_info *section,
5877 sect_offset sect_off)
5878 {
5879 bfd *abfd = section->get_bfd_owner ();
5880 const gdb_byte *info_ptr;
5881 unsigned int initial_length_size, offset_size;
5882 uint16_t version;
5883
5884 section->read (dwarf2_per_objfile->objfile);
5885 info_ptr = section->buffer + to_underlying (sect_off);
5886 read_initial_length (abfd, info_ptr, &initial_length_size);
5887 offset_size = initial_length_size == 4 ? 4 : 8;
5888 info_ptr += initial_length_size;
5889
5890 version = read_2_bytes (abfd, info_ptr);
5891 info_ptr += 2;
5892 if (version >= 5)
5893 {
5894 /* Skip unit type and address size. */
5895 info_ptr += 2;
5896 }
5897
5898 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5899 }
5900
5901 /* Allocate a new partial symtab for file named NAME and mark this new
5902 partial symtab as being an include of PST. */
5903
5904 static void
5905 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5906 struct objfile *objfile)
5907 {
5908 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
5909
5910 if (!IS_ABSOLUTE_PATH (subpst->filename))
5911 {
5912 /* It shares objfile->objfile_obstack. */
5913 subpst->dirname = pst->dirname;
5914 }
5915
5916 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5917 subpst->dependencies[0] = pst;
5918 subpst->number_of_dependencies = 1;
5919
5920 /* No private part is necessary for include psymtabs. This property
5921 can be used to differentiate between such include psymtabs and
5922 the regular ones. */
5923 subpst->per_cu_data = nullptr;
5924 }
5925
5926 /* Read the Line Number Program data and extract the list of files
5927 included by the source file represented by PST. Build an include
5928 partial symtab for each of these included files. */
5929
5930 static void
5931 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5932 struct die_info *die,
5933 dwarf2_psymtab *pst)
5934 {
5935 line_header_up lh;
5936 struct attribute *attr;
5937
5938 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5939 if (attr != nullptr)
5940 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5941 if (lh == NULL)
5942 return; /* No linetable, so no includes. */
5943
5944 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5945 that we pass in the raw text_low here; that is ok because we're
5946 only decoding the line table to make include partial symtabs, and
5947 so the addresses aren't really used. */
5948 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5949 pst->raw_text_low (), 1);
5950 }
5951
5952 static hashval_t
5953 hash_signatured_type (const void *item)
5954 {
5955 const struct signatured_type *sig_type
5956 = (const struct signatured_type *) item;
5957
5958 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5959 return sig_type->signature;
5960 }
5961
5962 static int
5963 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5964 {
5965 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5966 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5967
5968 return lhs->signature == rhs->signature;
5969 }
5970
5971 /* Allocate a hash table for signatured types. */
5972
5973 static htab_up
5974 allocate_signatured_type_table ()
5975 {
5976 return htab_up (htab_create_alloc (41,
5977 hash_signatured_type,
5978 eq_signatured_type,
5979 NULL, xcalloc, xfree));
5980 }
5981
5982 /* A helper function to add a signatured type CU to a table. */
5983
5984 static int
5985 add_signatured_type_cu_to_table (void **slot, void *datum)
5986 {
5987 struct signatured_type *sigt = (struct signatured_type *) *slot;
5988 std::vector<signatured_type *> *all_type_units
5989 = (std::vector<signatured_type *> *) datum;
5990
5991 all_type_units->push_back (sigt);
5992
5993 return 1;
5994 }
5995
5996 /* A helper for create_debug_types_hash_table. Read types from SECTION
5997 and fill them into TYPES_HTAB. It will process only type units,
5998 therefore DW_UT_type. */
5999
6000 static void
6001 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6002 struct dwo_file *dwo_file,
6003 dwarf2_section_info *section, htab_up &types_htab,
6004 rcuh_kind section_kind)
6005 {
6006 struct objfile *objfile = dwarf2_per_objfile->objfile;
6007 struct dwarf2_section_info *abbrev_section;
6008 bfd *abfd;
6009 const gdb_byte *info_ptr, *end_ptr;
6010
6011 abbrev_section = (dwo_file != NULL
6012 ? &dwo_file->sections.abbrev
6013 : &dwarf2_per_objfile->abbrev);
6014
6015 if (dwarf_read_debug)
6016 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6017 section->get_name (),
6018 abbrev_section->get_file_name ());
6019
6020 section->read (objfile);
6021 info_ptr = section->buffer;
6022
6023 if (info_ptr == NULL)
6024 return;
6025
6026 /* We can't set abfd until now because the section may be empty or
6027 not present, in which case the bfd is unknown. */
6028 abfd = section->get_bfd_owner ();
6029
6030 /* We don't use cutu_reader here because we don't need to read
6031 any dies: the signature is in the header. */
6032
6033 end_ptr = info_ptr + section->size;
6034 while (info_ptr < end_ptr)
6035 {
6036 struct signatured_type *sig_type;
6037 struct dwo_unit *dwo_tu;
6038 void **slot;
6039 const gdb_byte *ptr = info_ptr;
6040 struct comp_unit_head header;
6041 unsigned int length;
6042
6043 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6044
6045 /* Initialize it due to a false compiler warning. */
6046 header.signature = -1;
6047 header.type_cu_offset_in_tu = (cu_offset) -1;
6048
6049 /* We need to read the type's signature in order to build the hash
6050 table, but we don't need anything else just yet. */
6051
6052 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6053 abbrev_section, ptr, section_kind);
6054
6055 length = header.get_length ();
6056
6057 /* Skip dummy type units. */
6058 if (ptr >= info_ptr + length
6059 || peek_abbrev_code (abfd, ptr) == 0
6060 || header.unit_type != DW_UT_type)
6061 {
6062 info_ptr += length;
6063 continue;
6064 }
6065
6066 if (types_htab == NULL)
6067 {
6068 if (dwo_file)
6069 types_htab = allocate_dwo_unit_table ();
6070 else
6071 types_htab = allocate_signatured_type_table ();
6072 }
6073
6074 if (dwo_file)
6075 {
6076 sig_type = NULL;
6077 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6078 struct dwo_unit);
6079 dwo_tu->dwo_file = dwo_file;
6080 dwo_tu->signature = header.signature;
6081 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6082 dwo_tu->section = section;
6083 dwo_tu->sect_off = sect_off;
6084 dwo_tu->length = length;
6085 }
6086 else
6087 {
6088 /* N.B.: type_offset is not usable if this type uses a DWO file.
6089 The real type_offset is in the DWO file. */
6090 dwo_tu = NULL;
6091 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6092 struct signatured_type);
6093 sig_type->signature = header.signature;
6094 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6095 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6096 sig_type->per_cu.is_debug_types = 1;
6097 sig_type->per_cu.section = section;
6098 sig_type->per_cu.sect_off = sect_off;
6099 sig_type->per_cu.length = length;
6100 }
6101
6102 slot = htab_find_slot (types_htab.get (),
6103 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6104 INSERT);
6105 gdb_assert (slot != NULL);
6106 if (*slot != NULL)
6107 {
6108 sect_offset dup_sect_off;
6109
6110 if (dwo_file)
6111 {
6112 const struct dwo_unit *dup_tu
6113 = (const struct dwo_unit *) *slot;
6114
6115 dup_sect_off = dup_tu->sect_off;
6116 }
6117 else
6118 {
6119 const struct signatured_type *dup_tu
6120 = (const struct signatured_type *) *slot;
6121
6122 dup_sect_off = dup_tu->per_cu.sect_off;
6123 }
6124
6125 complaint (_("debug type entry at offset %s is duplicate to"
6126 " the entry at offset %s, signature %s"),
6127 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6128 hex_string (header.signature));
6129 }
6130 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6131
6132 if (dwarf_read_debug > 1)
6133 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6134 sect_offset_str (sect_off),
6135 hex_string (header.signature));
6136
6137 info_ptr += length;
6138 }
6139 }
6140
6141 /* Create the hash table of all entries in the .debug_types
6142 (or .debug_types.dwo) section(s).
6143 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6144 otherwise it is NULL.
6145
6146 The result is a pointer to the hash table or NULL if there are no types.
6147
6148 Note: This function processes DWO files only, not DWP files. */
6149
6150 static void
6151 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6152 struct dwo_file *dwo_file,
6153 gdb::array_view<dwarf2_section_info> type_sections,
6154 htab_up &types_htab)
6155 {
6156 for (dwarf2_section_info &section : type_sections)
6157 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6158 types_htab, rcuh_kind::TYPE);
6159 }
6160
6161 /* Create the hash table of all entries in the .debug_types section,
6162 and initialize all_type_units.
6163 The result is zero if there is an error (e.g. missing .debug_types section),
6164 otherwise non-zero. */
6165
6166 static int
6167 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6168 {
6169 htab_up types_htab;
6170
6171 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6172 &dwarf2_per_objfile->info, types_htab,
6173 rcuh_kind::COMPILE);
6174 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6175 dwarf2_per_objfile->types, types_htab);
6176 if (types_htab == NULL)
6177 {
6178 dwarf2_per_objfile->signatured_types = NULL;
6179 return 0;
6180 }
6181
6182 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6183
6184 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6185 dwarf2_per_objfile->all_type_units.reserve
6186 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6187
6188 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6189 add_signatured_type_cu_to_table,
6190 &dwarf2_per_objfile->all_type_units);
6191
6192 return 1;
6193 }
6194
6195 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6196 If SLOT is non-NULL, it is the entry to use in the hash table.
6197 Otherwise we find one. */
6198
6199 static struct signatured_type *
6200 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6201 void **slot)
6202 {
6203 struct objfile *objfile = dwarf2_per_objfile->objfile;
6204
6205 if (dwarf2_per_objfile->all_type_units.size ()
6206 == dwarf2_per_objfile->all_type_units.capacity ())
6207 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6208
6209 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6210 struct signatured_type);
6211
6212 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6213 sig_type->signature = sig;
6214 sig_type->per_cu.is_debug_types = 1;
6215 if (dwarf2_per_objfile->using_index)
6216 {
6217 sig_type->per_cu.v.quick =
6218 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6219 struct dwarf2_per_cu_quick_data);
6220 }
6221
6222 if (slot == NULL)
6223 {
6224 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6225 sig_type, INSERT);
6226 }
6227 gdb_assert (*slot == NULL);
6228 *slot = sig_type;
6229 /* The rest of sig_type must be filled in by the caller. */
6230 return sig_type;
6231 }
6232
6233 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6234 Fill in SIG_ENTRY with DWO_ENTRY. */
6235
6236 static void
6237 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6238 struct signatured_type *sig_entry,
6239 struct dwo_unit *dwo_entry)
6240 {
6241 /* Make sure we're not clobbering something we don't expect to. */
6242 gdb_assert (! sig_entry->per_cu.queued);
6243 gdb_assert (sig_entry->per_cu.cu == NULL);
6244 if (dwarf2_per_objfile->using_index)
6245 {
6246 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6247 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6248 }
6249 else
6250 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6251 gdb_assert (sig_entry->signature == dwo_entry->signature);
6252 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6253 gdb_assert (sig_entry->type_unit_group == NULL);
6254 gdb_assert (sig_entry->dwo_unit == NULL);
6255
6256 sig_entry->per_cu.section = dwo_entry->section;
6257 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6258 sig_entry->per_cu.length = dwo_entry->length;
6259 sig_entry->per_cu.reading_dwo_directly = 1;
6260 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6261 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6262 sig_entry->dwo_unit = dwo_entry;
6263 }
6264
6265 /* Subroutine of lookup_signatured_type.
6266 If we haven't read the TU yet, create the signatured_type data structure
6267 for a TU to be read in directly from a DWO file, bypassing the stub.
6268 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6269 using .gdb_index, then when reading a CU we want to stay in the DWO file
6270 containing that CU. Otherwise we could end up reading several other DWO
6271 files (due to comdat folding) to process the transitive closure of all the
6272 mentioned TUs, and that can be slow. The current DWO file will have every
6273 type signature that it needs.
6274 We only do this for .gdb_index because in the psymtab case we already have
6275 to read all the DWOs to build the type unit groups. */
6276
6277 static struct signatured_type *
6278 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6279 {
6280 struct dwarf2_per_objfile *dwarf2_per_objfile
6281 = cu->per_cu->dwarf2_per_objfile;
6282 struct dwo_file *dwo_file;
6283 struct dwo_unit find_dwo_entry, *dwo_entry;
6284 struct signatured_type find_sig_entry, *sig_entry;
6285 void **slot;
6286
6287 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6288
6289 /* If TU skeletons have been removed then we may not have read in any
6290 TUs yet. */
6291 if (dwarf2_per_objfile->signatured_types == NULL)
6292 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6293
6294 /* We only ever need to read in one copy of a signatured type.
6295 Use the global signatured_types array to do our own comdat-folding
6296 of types. If this is the first time we're reading this TU, and
6297 the TU has an entry in .gdb_index, replace the recorded data from
6298 .gdb_index with this TU. */
6299
6300 find_sig_entry.signature = sig;
6301 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6302 &find_sig_entry, INSERT);
6303 sig_entry = (struct signatured_type *) *slot;
6304
6305 /* We can get here with the TU already read, *or* in the process of being
6306 read. Don't reassign the global entry to point to this DWO if that's
6307 the case. Also note that if the TU is already being read, it may not
6308 have come from a DWO, the program may be a mix of Fission-compiled
6309 code and non-Fission-compiled code. */
6310
6311 /* Have we already tried to read this TU?
6312 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6313 needn't exist in the global table yet). */
6314 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6315 return sig_entry;
6316
6317 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6318 dwo_unit of the TU itself. */
6319 dwo_file = cu->dwo_unit->dwo_file;
6320
6321 /* Ok, this is the first time we're reading this TU. */
6322 if (dwo_file->tus == NULL)
6323 return NULL;
6324 find_dwo_entry.signature = sig;
6325 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6326 &find_dwo_entry);
6327 if (dwo_entry == NULL)
6328 return NULL;
6329
6330 /* If the global table doesn't have an entry for this TU, add one. */
6331 if (sig_entry == NULL)
6332 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6333
6334 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6335 sig_entry->per_cu.tu_read = 1;
6336 return sig_entry;
6337 }
6338
6339 /* Subroutine of lookup_signatured_type.
6340 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6341 then try the DWP file. If the TU stub (skeleton) has been removed then
6342 it won't be in .gdb_index. */
6343
6344 static struct signatured_type *
6345 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6346 {
6347 struct dwarf2_per_objfile *dwarf2_per_objfile
6348 = cu->per_cu->dwarf2_per_objfile;
6349 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6350 struct dwo_unit *dwo_entry;
6351 struct signatured_type find_sig_entry, *sig_entry;
6352 void **slot;
6353
6354 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6355 gdb_assert (dwp_file != NULL);
6356
6357 /* If TU skeletons have been removed then we may not have read in any
6358 TUs yet. */
6359 if (dwarf2_per_objfile->signatured_types == NULL)
6360 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6361
6362 find_sig_entry.signature = sig;
6363 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6364 &find_sig_entry, INSERT);
6365 sig_entry = (struct signatured_type *) *slot;
6366
6367 /* Have we already tried to read this TU?
6368 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6369 needn't exist in the global table yet). */
6370 if (sig_entry != NULL)
6371 return sig_entry;
6372
6373 if (dwp_file->tus == NULL)
6374 return NULL;
6375 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6376 sig, 1 /* is_debug_types */);
6377 if (dwo_entry == NULL)
6378 return NULL;
6379
6380 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6381 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6382
6383 return sig_entry;
6384 }
6385
6386 /* Lookup a signature based type for DW_FORM_ref_sig8.
6387 Returns NULL if signature SIG is not present in the table.
6388 It is up to the caller to complain about this. */
6389
6390 static struct signatured_type *
6391 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6392 {
6393 struct dwarf2_per_objfile *dwarf2_per_objfile
6394 = cu->per_cu->dwarf2_per_objfile;
6395
6396 if (cu->dwo_unit
6397 && dwarf2_per_objfile->using_index)
6398 {
6399 /* We're in a DWO/DWP file, and we're using .gdb_index.
6400 These cases require special processing. */
6401 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6402 return lookup_dwo_signatured_type (cu, sig);
6403 else
6404 return lookup_dwp_signatured_type (cu, sig);
6405 }
6406 else
6407 {
6408 struct signatured_type find_entry, *entry;
6409
6410 if (dwarf2_per_objfile->signatured_types == NULL)
6411 return NULL;
6412 find_entry.signature = sig;
6413 entry = ((struct signatured_type *)
6414 htab_find (dwarf2_per_objfile->signatured_types.get (),
6415 &find_entry));
6416 return entry;
6417 }
6418 }
6419
6420 /* Return the address base of the compile unit, which, if exists, is stored
6421 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6422 static gdb::optional<ULONGEST>
6423 lookup_addr_base (struct die_info *comp_unit_die)
6424 {
6425 struct attribute *attr;
6426 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6427 if (attr == nullptr)
6428 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6429 if (attr == nullptr)
6430 return gdb::optional<ULONGEST> ();
6431 return DW_UNSND (attr);
6432 }
6433
6434 /* Return range lists base of the compile unit, which, if exists, is stored
6435 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6436 static ULONGEST
6437 lookup_ranges_base (struct die_info *comp_unit_die)
6438 {
6439 struct attribute *attr;
6440 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6441 if (attr == nullptr)
6442 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6443 if (attr == nullptr)
6444 return 0;
6445 return DW_UNSND (attr);
6446 }
6447
6448 /* Low level DIE reading support. */
6449
6450 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6451
6452 static void
6453 init_cu_die_reader (struct die_reader_specs *reader,
6454 struct dwarf2_cu *cu,
6455 struct dwarf2_section_info *section,
6456 struct dwo_file *dwo_file,
6457 struct abbrev_table *abbrev_table)
6458 {
6459 gdb_assert (section->readin && section->buffer != NULL);
6460 reader->abfd = section->get_bfd_owner ();
6461 reader->cu = cu;
6462 reader->dwo_file = dwo_file;
6463 reader->die_section = section;
6464 reader->buffer = section->buffer;
6465 reader->buffer_end = section->buffer + section->size;
6466 reader->abbrev_table = abbrev_table;
6467 }
6468
6469 /* Subroutine of cutu_reader to simplify it.
6470 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6471 There's just a lot of work to do, and cutu_reader is big enough
6472 already.
6473
6474 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6475 from it to the DIE in the DWO. If NULL we are skipping the stub.
6476 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6477 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6478 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6479 STUB_COMP_DIR may be non-NULL.
6480 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6481 are filled in with the info of the DIE from the DWO file.
6482 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6483 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6484 kept around for at least as long as *RESULT_READER.
6485
6486 The result is non-zero if a valid (non-dummy) DIE was found. */
6487
6488 static int
6489 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6490 struct dwo_unit *dwo_unit,
6491 struct die_info *stub_comp_unit_die,
6492 const char *stub_comp_dir,
6493 struct die_reader_specs *result_reader,
6494 const gdb_byte **result_info_ptr,
6495 struct die_info **result_comp_unit_die,
6496 abbrev_table_up *result_dwo_abbrev_table)
6497 {
6498 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6499 struct objfile *objfile = dwarf2_per_objfile->objfile;
6500 struct dwarf2_cu *cu = this_cu->cu;
6501 bfd *abfd;
6502 const gdb_byte *begin_info_ptr, *info_ptr;
6503 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6504 int i,num_extra_attrs;
6505 struct dwarf2_section_info *dwo_abbrev_section;
6506 struct die_info *comp_unit_die;
6507
6508 /* At most one of these may be provided. */
6509 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6510
6511 /* These attributes aren't processed until later:
6512 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6513 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6514 referenced later. However, these attributes are found in the stub
6515 which we won't have later. In order to not impose this complication
6516 on the rest of the code, we read them here and copy them to the
6517 DWO CU/TU die. */
6518
6519 stmt_list = NULL;
6520 low_pc = NULL;
6521 high_pc = NULL;
6522 ranges = NULL;
6523 comp_dir = NULL;
6524
6525 if (stub_comp_unit_die != NULL)
6526 {
6527 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6528 DWO file. */
6529 if (! this_cu->is_debug_types)
6530 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6531 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6532 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6533 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6534 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6535
6536 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6537
6538 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6539 here (if needed). We need the value before we can process
6540 DW_AT_ranges. */
6541 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6542 }
6543 else if (stub_comp_dir != NULL)
6544 {
6545 /* Reconstruct the comp_dir attribute to simplify the code below. */
6546 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6547 comp_dir->name = DW_AT_comp_dir;
6548 comp_dir->form = DW_FORM_string;
6549 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6550 DW_STRING (comp_dir) = stub_comp_dir;
6551 }
6552
6553 /* Set up for reading the DWO CU/TU. */
6554 cu->dwo_unit = dwo_unit;
6555 dwarf2_section_info *section = dwo_unit->section;
6556 section->read (objfile);
6557 abfd = section->get_bfd_owner ();
6558 begin_info_ptr = info_ptr = (section->buffer
6559 + to_underlying (dwo_unit->sect_off));
6560 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6561
6562 if (this_cu->is_debug_types)
6563 {
6564 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6565
6566 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6567 &cu->header, section,
6568 dwo_abbrev_section,
6569 info_ptr, rcuh_kind::TYPE);
6570 /* This is not an assert because it can be caused by bad debug info. */
6571 if (sig_type->signature != cu->header.signature)
6572 {
6573 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6574 " TU at offset %s [in module %s]"),
6575 hex_string (sig_type->signature),
6576 hex_string (cu->header.signature),
6577 sect_offset_str (dwo_unit->sect_off),
6578 bfd_get_filename (abfd));
6579 }
6580 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6581 /* For DWOs coming from DWP files, we don't know the CU length
6582 nor the type's offset in the TU until now. */
6583 dwo_unit->length = cu->header.get_length ();
6584 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6585
6586 /* Establish the type offset that can be used to lookup the type.
6587 For DWO files, we don't know it until now. */
6588 sig_type->type_offset_in_section
6589 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6590 }
6591 else
6592 {
6593 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6594 &cu->header, section,
6595 dwo_abbrev_section,
6596 info_ptr, rcuh_kind::COMPILE);
6597 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6598 /* For DWOs coming from DWP files, we don't know the CU length
6599 until now. */
6600 dwo_unit->length = cu->header.get_length ();
6601 }
6602
6603 *result_dwo_abbrev_table
6604 = abbrev_table::read (objfile, dwo_abbrev_section,
6605 cu->header.abbrev_sect_off);
6606 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6607 result_dwo_abbrev_table->get ());
6608
6609 /* Read in the die, but leave space to copy over the attributes
6610 from the stub. This has the benefit of simplifying the rest of
6611 the code - all the work to maintain the illusion of a single
6612 DW_TAG_{compile,type}_unit DIE is done here. */
6613 num_extra_attrs = ((stmt_list != NULL)
6614 + (low_pc != NULL)
6615 + (high_pc != NULL)
6616 + (ranges != NULL)
6617 + (comp_dir != NULL));
6618 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6619 num_extra_attrs);
6620
6621 /* Copy over the attributes from the stub to the DIE we just read in. */
6622 comp_unit_die = *result_comp_unit_die;
6623 i = comp_unit_die->num_attrs;
6624 if (stmt_list != NULL)
6625 comp_unit_die->attrs[i++] = *stmt_list;
6626 if (low_pc != NULL)
6627 comp_unit_die->attrs[i++] = *low_pc;
6628 if (high_pc != NULL)
6629 comp_unit_die->attrs[i++] = *high_pc;
6630 if (ranges != NULL)
6631 comp_unit_die->attrs[i++] = *ranges;
6632 if (comp_dir != NULL)
6633 comp_unit_die->attrs[i++] = *comp_dir;
6634 comp_unit_die->num_attrs += num_extra_attrs;
6635
6636 if (dwarf_die_debug)
6637 {
6638 fprintf_unfiltered (gdb_stdlog,
6639 "Read die from %s@0x%x of %s:\n",
6640 section->get_name (),
6641 (unsigned) (begin_info_ptr - section->buffer),
6642 bfd_get_filename (abfd));
6643 dump_die (comp_unit_die, dwarf_die_debug);
6644 }
6645
6646 /* Skip dummy compilation units. */
6647 if (info_ptr >= begin_info_ptr + dwo_unit->length
6648 || peek_abbrev_code (abfd, info_ptr) == 0)
6649 return 0;
6650
6651 *result_info_ptr = info_ptr;
6652 return 1;
6653 }
6654
6655 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6656 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6657 signature is part of the header. */
6658 static gdb::optional<ULONGEST>
6659 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6660 {
6661 if (cu->header.version >= 5)
6662 return cu->header.signature;
6663 struct attribute *attr;
6664 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6665 if (attr == nullptr)
6666 return gdb::optional<ULONGEST> ();
6667 return DW_UNSND (attr);
6668 }
6669
6670 /* Subroutine of cutu_reader to simplify it.
6671 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6672 Returns NULL if the specified DWO unit cannot be found. */
6673
6674 static struct dwo_unit *
6675 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6676 struct die_info *comp_unit_die,
6677 const char *dwo_name)
6678 {
6679 struct dwarf2_cu *cu = this_cu->cu;
6680 struct dwo_unit *dwo_unit;
6681 const char *comp_dir;
6682
6683 gdb_assert (cu != NULL);
6684
6685 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6686 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6687 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6688
6689 if (this_cu->is_debug_types)
6690 {
6691 struct signatured_type *sig_type;
6692
6693 /* Since this_cu is the first member of struct signatured_type,
6694 we can go from a pointer to one to a pointer to the other. */
6695 sig_type = (struct signatured_type *) this_cu;
6696 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6697 }
6698 else
6699 {
6700 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6701 if (!signature.has_value ())
6702 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6703 " [in module %s]"),
6704 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6705 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6706 *signature);
6707 }
6708
6709 return dwo_unit;
6710 }
6711
6712 /* Subroutine of cutu_reader to simplify it.
6713 See it for a description of the parameters.
6714 Read a TU directly from a DWO file, bypassing the stub. */
6715
6716 void
6717 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6718 int use_existing_cu)
6719 {
6720 struct signatured_type *sig_type;
6721 struct die_reader_specs reader;
6722
6723 /* Verify we can do the following downcast, and that we have the
6724 data we need. */
6725 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6726 sig_type = (struct signatured_type *) this_cu;
6727 gdb_assert (sig_type->dwo_unit != NULL);
6728
6729 if (use_existing_cu && this_cu->cu != NULL)
6730 {
6731 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6732 /* There's no need to do the rereading_dwo_cu handling that
6733 cutu_reader does since we don't read the stub. */
6734 }
6735 else
6736 {
6737 /* If !use_existing_cu, this_cu->cu must be NULL. */
6738 gdb_assert (this_cu->cu == NULL);
6739 m_new_cu.reset (new dwarf2_cu (this_cu));
6740 }
6741
6742 /* A future optimization, if needed, would be to use an existing
6743 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6744 could share abbrev tables. */
6745
6746 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6747 NULL /* stub_comp_unit_die */,
6748 sig_type->dwo_unit->dwo_file->comp_dir,
6749 &reader, &info_ptr,
6750 &comp_unit_die,
6751 &m_dwo_abbrev_table) == 0)
6752 {
6753 /* Dummy die. */
6754 dummy_p = true;
6755 }
6756 }
6757
6758 /* Initialize a CU (or TU) and read its DIEs.
6759 If the CU defers to a DWO file, read the DWO file as well.
6760
6761 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6762 Otherwise the table specified in the comp unit header is read in and used.
6763 This is an optimization for when we already have the abbrev table.
6764
6765 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6766 Otherwise, a new CU is allocated with xmalloc. */
6767
6768 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6769 struct abbrev_table *abbrev_table,
6770 int use_existing_cu,
6771 bool skip_partial)
6772 : die_reader_specs {},
6773 m_this_cu (this_cu)
6774 {
6775 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6776 struct objfile *objfile = dwarf2_per_objfile->objfile;
6777 struct dwarf2_section_info *section = this_cu->section;
6778 bfd *abfd = section->get_bfd_owner ();
6779 struct dwarf2_cu *cu;
6780 const gdb_byte *begin_info_ptr;
6781 struct signatured_type *sig_type = NULL;
6782 struct dwarf2_section_info *abbrev_section;
6783 /* Non-zero if CU currently points to a DWO file and we need to
6784 reread it. When this happens we need to reread the skeleton die
6785 before we can reread the DWO file (this only applies to CUs, not TUs). */
6786 int rereading_dwo_cu = 0;
6787
6788 if (dwarf_die_debug)
6789 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6790 this_cu->is_debug_types ? "type" : "comp",
6791 sect_offset_str (this_cu->sect_off));
6792
6793 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6794 file (instead of going through the stub), short-circuit all of this. */
6795 if (this_cu->reading_dwo_directly)
6796 {
6797 /* Narrow down the scope of possibilities to have to understand. */
6798 gdb_assert (this_cu->is_debug_types);
6799 gdb_assert (abbrev_table == NULL);
6800 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6801 return;
6802 }
6803
6804 /* This is cheap if the section is already read in. */
6805 section->read (objfile);
6806
6807 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6808
6809 abbrev_section = get_abbrev_section_for_cu (this_cu);
6810
6811 if (use_existing_cu && this_cu->cu != NULL)
6812 {
6813 cu = this_cu->cu;
6814 /* If this CU is from a DWO file we need to start over, we need to
6815 refetch the attributes from the skeleton CU.
6816 This could be optimized by retrieving those attributes from when we
6817 were here the first time: the previous comp_unit_die was stored in
6818 comp_unit_obstack. But there's no data yet that we need this
6819 optimization. */
6820 if (cu->dwo_unit != NULL)
6821 rereading_dwo_cu = 1;
6822 }
6823 else
6824 {
6825 /* If !use_existing_cu, this_cu->cu must be NULL. */
6826 gdb_assert (this_cu->cu == NULL);
6827 m_new_cu.reset (new dwarf2_cu (this_cu));
6828 cu = m_new_cu.get ();
6829 }
6830
6831 /* Get the header. */
6832 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6833 {
6834 /* We already have the header, there's no need to read it in again. */
6835 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6836 }
6837 else
6838 {
6839 if (this_cu->is_debug_types)
6840 {
6841 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6842 &cu->header, section,
6843 abbrev_section, info_ptr,
6844 rcuh_kind::TYPE);
6845
6846 /* Since per_cu is the first member of struct signatured_type,
6847 we can go from a pointer to one to a pointer to the other. */
6848 sig_type = (struct signatured_type *) this_cu;
6849 gdb_assert (sig_type->signature == cu->header.signature);
6850 gdb_assert (sig_type->type_offset_in_tu
6851 == cu->header.type_cu_offset_in_tu);
6852 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6853
6854 /* LENGTH has not been set yet for type units if we're
6855 using .gdb_index. */
6856 this_cu->length = cu->header.get_length ();
6857
6858 /* Establish the type offset that can be used to lookup the type. */
6859 sig_type->type_offset_in_section =
6860 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6861
6862 this_cu->dwarf_version = cu->header.version;
6863 }
6864 else
6865 {
6866 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6867 &cu->header, section,
6868 abbrev_section,
6869 info_ptr,
6870 rcuh_kind::COMPILE);
6871
6872 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6873 gdb_assert (this_cu->length == cu->header.get_length ());
6874 this_cu->dwarf_version = cu->header.version;
6875 }
6876 }
6877
6878 /* Skip dummy compilation units. */
6879 if (info_ptr >= begin_info_ptr + this_cu->length
6880 || peek_abbrev_code (abfd, info_ptr) == 0)
6881 {
6882 dummy_p = true;
6883 return;
6884 }
6885
6886 /* If we don't have them yet, read the abbrevs for this compilation unit.
6887 And if we need to read them now, make sure they're freed when we're
6888 done. */
6889 if (abbrev_table != NULL)
6890 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6891 else
6892 {
6893 m_abbrev_table_holder
6894 = abbrev_table::read (objfile, abbrev_section,
6895 cu->header.abbrev_sect_off);
6896 abbrev_table = m_abbrev_table_holder.get ();
6897 }
6898
6899 /* Read the top level CU/TU die. */
6900 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6901 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6902
6903 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6904 {
6905 dummy_p = true;
6906 return;
6907 }
6908
6909 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6910 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6911 table from the DWO file and pass the ownership over to us. It will be
6912 referenced from READER, so we must make sure to free it after we're done
6913 with READER.
6914
6915 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6916 DWO CU, that this test will fail (the attribute will not be present). */
6917 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6918 if (dwo_name != nullptr)
6919 {
6920 struct dwo_unit *dwo_unit;
6921 struct die_info *dwo_comp_unit_die;
6922
6923 if (comp_unit_die->has_children)
6924 {
6925 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6926 " has children (offset %s) [in module %s]"),
6927 sect_offset_str (this_cu->sect_off),
6928 bfd_get_filename (abfd));
6929 }
6930 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6931 if (dwo_unit != NULL)
6932 {
6933 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6934 comp_unit_die, NULL,
6935 this, &info_ptr,
6936 &dwo_comp_unit_die,
6937 &m_dwo_abbrev_table) == 0)
6938 {
6939 /* Dummy die. */
6940 dummy_p = true;
6941 return;
6942 }
6943 comp_unit_die = dwo_comp_unit_die;
6944 }
6945 else
6946 {
6947 /* Yikes, we couldn't find the rest of the DIE, we only have
6948 the stub. A complaint has already been logged. There's
6949 not much more we can do except pass on the stub DIE to
6950 die_reader_func. We don't want to throw an error on bad
6951 debug info. */
6952 }
6953 }
6954 }
6955
6956 void
6957 cutu_reader::keep ()
6958 {
6959 /* Done, clean up. */
6960 gdb_assert (!dummy_p);
6961 if (m_new_cu != NULL)
6962 {
6963 struct dwarf2_per_objfile *dwarf2_per_objfile
6964 = m_this_cu->dwarf2_per_objfile;
6965 /* Link this CU into read_in_chain. */
6966 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6967 dwarf2_per_objfile->read_in_chain = m_this_cu;
6968 /* The chain owns it now. */
6969 m_new_cu.release ();
6970 }
6971 }
6972
6973 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6974 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6975 assumed to have already done the lookup to find the DWO file).
6976
6977 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6978 THIS_CU->is_debug_types, but nothing else.
6979
6980 We fill in THIS_CU->length.
6981
6982 THIS_CU->cu is always freed when done.
6983 This is done in order to not leave THIS_CU->cu in a state where we have
6984 to care whether it refers to the "main" CU or the DWO CU.
6985
6986 When parent_cu is passed, it is used to provide a default value for
6987 str_offsets_base and addr_base from the parent. */
6988
6989 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6990 struct dwarf2_cu *parent_cu,
6991 struct dwo_file *dwo_file)
6992 : die_reader_specs {},
6993 m_this_cu (this_cu)
6994 {
6995 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6996 struct objfile *objfile = dwarf2_per_objfile->objfile;
6997 struct dwarf2_section_info *section = this_cu->section;
6998 bfd *abfd = section->get_bfd_owner ();
6999 struct dwarf2_section_info *abbrev_section;
7000 const gdb_byte *begin_info_ptr, *info_ptr;
7001
7002 if (dwarf_die_debug)
7003 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7004 this_cu->is_debug_types ? "type" : "comp",
7005 sect_offset_str (this_cu->sect_off));
7006
7007 gdb_assert (this_cu->cu == NULL);
7008
7009 abbrev_section = (dwo_file != NULL
7010 ? &dwo_file->sections.abbrev
7011 : get_abbrev_section_for_cu (this_cu));
7012
7013 /* This is cheap if the section is already read in. */
7014 section->read (objfile);
7015
7016 m_new_cu.reset (new dwarf2_cu (this_cu));
7017
7018 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7019 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7020 &m_new_cu->header, section,
7021 abbrev_section, info_ptr,
7022 (this_cu->is_debug_types
7023 ? rcuh_kind::TYPE
7024 : rcuh_kind::COMPILE));
7025
7026 if (parent_cu != nullptr)
7027 {
7028 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7029 m_new_cu->addr_base = parent_cu->addr_base;
7030 }
7031 this_cu->length = m_new_cu->header.get_length ();
7032
7033 /* Skip dummy compilation units. */
7034 if (info_ptr >= begin_info_ptr + this_cu->length
7035 || peek_abbrev_code (abfd, info_ptr) == 0)
7036 {
7037 dummy_p = true;
7038 return;
7039 }
7040
7041 m_abbrev_table_holder
7042 = abbrev_table::read (objfile, abbrev_section,
7043 m_new_cu->header.abbrev_sect_off);
7044
7045 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7046 m_abbrev_table_holder.get ());
7047 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7048 }
7049
7050 \f
7051 /* Type Unit Groups.
7052
7053 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7054 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7055 so that all types coming from the same compilation (.o file) are grouped
7056 together. A future step could be to put the types in the same symtab as
7057 the CU the types ultimately came from. */
7058
7059 static hashval_t
7060 hash_type_unit_group (const void *item)
7061 {
7062 const struct type_unit_group *tu_group
7063 = (const struct type_unit_group *) item;
7064
7065 return hash_stmt_list_entry (&tu_group->hash);
7066 }
7067
7068 static int
7069 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7070 {
7071 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7072 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7073
7074 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7075 }
7076
7077 /* Allocate a hash table for type unit groups. */
7078
7079 static htab_up
7080 allocate_type_unit_groups_table ()
7081 {
7082 return htab_up (htab_create_alloc (3,
7083 hash_type_unit_group,
7084 eq_type_unit_group,
7085 NULL, xcalloc, xfree));
7086 }
7087
7088 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7089 partial symtabs. We combine several TUs per psymtab to not let the size
7090 of any one psymtab grow too big. */
7091 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7092 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7093
7094 /* Helper routine for get_type_unit_group.
7095 Create the type_unit_group object used to hold one or more TUs. */
7096
7097 static struct type_unit_group *
7098 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7099 {
7100 struct dwarf2_per_objfile *dwarf2_per_objfile
7101 = cu->per_cu->dwarf2_per_objfile;
7102 struct objfile *objfile = dwarf2_per_objfile->objfile;
7103 struct dwarf2_per_cu_data *per_cu;
7104 struct type_unit_group *tu_group;
7105
7106 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7107 struct type_unit_group);
7108 per_cu = &tu_group->per_cu;
7109 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7110
7111 if (dwarf2_per_objfile->using_index)
7112 {
7113 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7114 struct dwarf2_per_cu_quick_data);
7115 }
7116 else
7117 {
7118 unsigned int line_offset = to_underlying (line_offset_struct);
7119 dwarf2_psymtab *pst;
7120 std::string name;
7121
7122 /* Give the symtab a useful name for debug purposes. */
7123 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7124 name = string_printf ("<type_units_%d>",
7125 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7126 else
7127 name = string_printf ("<type_units_at_0x%x>", line_offset);
7128
7129 pst = create_partial_symtab (per_cu, name.c_str ());
7130 pst->anonymous = true;
7131 }
7132
7133 tu_group->hash.dwo_unit = cu->dwo_unit;
7134 tu_group->hash.line_sect_off = line_offset_struct;
7135
7136 return tu_group;
7137 }
7138
7139 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7140 STMT_LIST is a DW_AT_stmt_list attribute. */
7141
7142 static struct type_unit_group *
7143 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7144 {
7145 struct dwarf2_per_objfile *dwarf2_per_objfile
7146 = cu->per_cu->dwarf2_per_objfile;
7147 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7148 struct type_unit_group *tu_group;
7149 void **slot;
7150 unsigned int line_offset;
7151 struct type_unit_group type_unit_group_for_lookup;
7152
7153 if (dwarf2_per_objfile->type_unit_groups == NULL)
7154 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7155
7156 /* Do we need to create a new group, or can we use an existing one? */
7157
7158 if (stmt_list)
7159 {
7160 line_offset = DW_UNSND (stmt_list);
7161 ++tu_stats->nr_symtab_sharers;
7162 }
7163 else
7164 {
7165 /* Ugh, no stmt_list. Rare, but we have to handle it.
7166 We can do various things here like create one group per TU or
7167 spread them over multiple groups to split up the expansion work.
7168 To avoid worst case scenarios (too many groups or too large groups)
7169 we, umm, group them in bunches. */
7170 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7171 | (tu_stats->nr_stmt_less_type_units
7172 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7173 ++tu_stats->nr_stmt_less_type_units;
7174 }
7175
7176 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7177 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7178 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7179 &type_unit_group_for_lookup, INSERT);
7180 if (*slot != NULL)
7181 {
7182 tu_group = (struct type_unit_group *) *slot;
7183 gdb_assert (tu_group != NULL);
7184 }
7185 else
7186 {
7187 sect_offset line_offset_struct = (sect_offset) line_offset;
7188 tu_group = create_type_unit_group (cu, line_offset_struct);
7189 *slot = tu_group;
7190 ++tu_stats->nr_symtabs;
7191 }
7192
7193 return tu_group;
7194 }
7195 \f
7196 /* Partial symbol tables. */
7197
7198 /* Create a psymtab named NAME and assign it to PER_CU.
7199
7200 The caller must fill in the following details:
7201 dirname, textlow, texthigh. */
7202
7203 static dwarf2_psymtab *
7204 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7205 {
7206 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7207 dwarf2_psymtab *pst;
7208
7209 pst = new dwarf2_psymtab (name, objfile, 0);
7210
7211 pst->psymtabs_addrmap_supported = true;
7212
7213 /* This is the glue that links PST into GDB's symbol API. */
7214 pst->per_cu_data = per_cu;
7215 per_cu->v.psymtab = pst;
7216
7217 return pst;
7218 }
7219
7220 /* DIE reader function for process_psymtab_comp_unit. */
7221
7222 static void
7223 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7224 const gdb_byte *info_ptr,
7225 struct die_info *comp_unit_die,
7226 enum language pretend_language)
7227 {
7228 struct dwarf2_cu *cu = reader->cu;
7229 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7230 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7231 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7232 CORE_ADDR baseaddr;
7233 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7234 dwarf2_psymtab *pst;
7235 enum pc_bounds_kind cu_bounds_kind;
7236 const char *filename;
7237
7238 gdb_assert (! per_cu->is_debug_types);
7239
7240 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7241
7242 /* Allocate a new partial symbol table structure. */
7243 gdb::unique_xmalloc_ptr<char> debug_filename;
7244 static const char artificial[] = "<artificial>";
7245 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7246 if (filename == NULL)
7247 filename = "";
7248 else if (strcmp (filename, artificial) == 0)
7249 {
7250 debug_filename.reset (concat (artificial, "@",
7251 sect_offset_str (per_cu->sect_off),
7252 (char *) NULL));
7253 filename = debug_filename.get ();
7254 }
7255
7256 pst = create_partial_symtab (per_cu, filename);
7257
7258 /* This must be done before calling dwarf2_build_include_psymtabs. */
7259 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7260
7261 baseaddr = objfile->text_section_offset ();
7262
7263 dwarf2_find_base_address (comp_unit_die, cu);
7264
7265 /* Possibly set the default values of LOWPC and HIGHPC from
7266 `DW_AT_ranges'. */
7267 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7268 &best_highpc, cu, pst);
7269 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7270 {
7271 CORE_ADDR low
7272 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7273 - baseaddr);
7274 CORE_ADDR high
7275 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7276 - baseaddr - 1);
7277 /* Store the contiguous range if it is not empty; it can be
7278 empty for CUs with no code. */
7279 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7280 low, high, pst);
7281 }
7282
7283 /* Check if comp unit has_children.
7284 If so, read the rest of the partial symbols from this comp unit.
7285 If not, there's no more debug_info for this comp unit. */
7286 if (comp_unit_die->has_children)
7287 {
7288 struct partial_die_info *first_die;
7289 CORE_ADDR lowpc, highpc;
7290
7291 lowpc = ((CORE_ADDR) -1);
7292 highpc = ((CORE_ADDR) 0);
7293
7294 first_die = load_partial_dies (reader, info_ptr, 1);
7295
7296 scan_partial_symbols (first_die, &lowpc, &highpc,
7297 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7298
7299 /* If we didn't find a lowpc, set it to highpc to avoid
7300 complaints from `maint check'. */
7301 if (lowpc == ((CORE_ADDR) -1))
7302 lowpc = highpc;
7303
7304 /* If the compilation unit didn't have an explicit address range,
7305 then use the information extracted from its child dies. */
7306 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7307 {
7308 best_lowpc = lowpc;
7309 best_highpc = highpc;
7310 }
7311 }
7312 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7313 best_lowpc + baseaddr)
7314 - baseaddr);
7315 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7316 best_highpc + baseaddr)
7317 - baseaddr);
7318
7319 end_psymtab_common (objfile, pst);
7320
7321 if (!cu->per_cu->imported_symtabs_empty ())
7322 {
7323 int i;
7324 int len = cu->per_cu->imported_symtabs_size ();
7325
7326 /* Fill in 'dependencies' here; we fill in 'users' in a
7327 post-pass. */
7328 pst->number_of_dependencies = len;
7329 pst->dependencies
7330 = objfile->partial_symtabs->allocate_dependencies (len);
7331 for (i = 0; i < len; ++i)
7332 {
7333 pst->dependencies[i]
7334 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7335 }
7336
7337 cu->per_cu->imported_symtabs_free ();
7338 }
7339
7340 /* Get the list of files included in the current compilation unit,
7341 and build a psymtab for each of them. */
7342 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7343
7344 if (dwarf_read_debug)
7345 fprintf_unfiltered (gdb_stdlog,
7346 "Psymtab for %s unit @%s: %s - %s"
7347 ", %d global, %d static syms\n",
7348 per_cu->is_debug_types ? "type" : "comp",
7349 sect_offset_str (per_cu->sect_off),
7350 paddress (gdbarch, pst->text_low (objfile)),
7351 paddress (gdbarch, pst->text_high (objfile)),
7352 pst->n_global_syms, pst->n_static_syms);
7353 }
7354
7355 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7356 Process compilation unit THIS_CU for a psymtab. */
7357
7358 static void
7359 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7360 bool want_partial_unit,
7361 enum language pretend_language)
7362 {
7363 /* If this compilation unit was already read in, free the
7364 cached copy in order to read it in again. This is
7365 necessary because we skipped some symbols when we first
7366 read in the compilation unit (see load_partial_dies).
7367 This problem could be avoided, but the benefit is unclear. */
7368 if (this_cu->cu != NULL)
7369 free_one_cached_comp_unit (this_cu);
7370
7371 cutu_reader reader (this_cu, NULL, 0, false);
7372
7373 if (reader.dummy_p)
7374 {
7375 /* Nothing. */
7376 }
7377 else if (this_cu->is_debug_types)
7378 build_type_psymtabs_reader (&reader, reader.info_ptr,
7379 reader.comp_unit_die);
7380 else if (want_partial_unit
7381 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7382 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7383 reader.comp_unit_die,
7384 pretend_language);
7385
7386 /* Age out any secondary CUs. */
7387 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7388 }
7389
7390 /* Reader function for build_type_psymtabs. */
7391
7392 static void
7393 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7394 const gdb_byte *info_ptr,
7395 struct die_info *type_unit_die)
7396 {
7397 struct dwarf2_per_objfile *dwarf2_per_objfile
7398 = reader->cu->per_cu->dwarf2_per_objfile;
7399 struct objfile *objfile = dwarf2_per_objfile->objfile;
7400 struct dwarf2_cu *cu = reader->cu;
7401 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7402 struct signatured_type *sig_type;
7403 struct type_unit_group *tu_group;
7404 struct attribute *attr;
7405 struct partial_die_info *first_die;
7406 CORE_ADDR lowpc, highpc;
7407 dwarf2_psymtab *pst;
7408
7409 gdb_assert (per_cu->is_debug_types);
7410 sig_type = (struct signatured_type *) per_cu;
7411
7412 if (! type_unit_die->has_children)
7413 return;
7414
7415 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
7416 tu_group = get_type_unit_group (cu, attr);
7417
7418 if (tu_group->tus == nullptr)
7419 tu_group->tus = new std::vector<signatured_type *>;
7420 tu_group->tus->push_back (sig_type);
7421
7422 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7423 pst = create_partial_symtab (per_cu, "");
7424 pst->anonymous = true;
7425
7426 first_die = load_partial_dies (reader, info_ptr, 1);
7427
7428 lowpc = (CORE_ADDR) -1;
7429 highpc = (CORE_ADDR) 0;
7430 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7431
7432 end_psymtab_common (objfile, pst);
7433 }
7434
7435 /* Struct used to sort TUs by their abbreviation table offset. */
7436
7437 struct tu_abbrev_offset
7438 {
7439 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7440 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7441 {}
7442
7443 signatured_type *sig_type;
7444 sect_offset abbrev_offset;
7445 };
7446
7447 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7448
7449 static bool
7450 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7451 const struct tu_abbrev_offset &b)
7452 {
7453 return a.abbrev_offset < b.abbrev_offset;
7454 }
7455
7456 /* Efficiently read all the type units.
7457 This does the bulk of the work for build_type_psymtabs.
7458
7459 The efficiency is because we sort TUs by the abbrev table they use and
7460 only read each abbrev table once. In one program there are 200K TUs
7461 sharing 8K abbrev tables.
7462
7463 The main purpose of this function is to support building the
7464 dwarf2_per_objfile->type_unit_groups table.
7465 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7466 can collapse the search space by grouping them by stmt_list.
7467 The savings can be significant, in the same program from above the 200K TUs
7468 share 8K stmt_list tables.
7469
7470 FUNC is expected to call get_type_unit_group, which will create the
7471 struct type_unit_group if necessary and add it to
7472 dwarf2_per_objfile->type_unit_groups. */
7473
7474 static void
7475 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7476 {
7477 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7478 abbrev_table_up abbrev_table;
7479 sect_offset abbrev_offset;
7480
7481 /* It's up to the caller to not call us multiple times. */
7482 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7483
7484 if (dwarf2_per_objfile->all_type_units.empty ())
7485 return;
7486
7487 /* TUs typically share abbrev tables, and there can be way more TUs than
7488 abbrev tables. Sort by abbrev table to reduce the number of times we
7489 read each abbrev table in.
7490 Alternatives are to punt or to maintain a cache of abbrev tables.
7491 This is simpler and efficient enough for now.
7492
7493 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7494 symtab to use). Typically TUs with the same abbrev offset have the same
7495 stmt_list value too so in practice this should work well.
7496
7497 The basic algorithm here is:
7498
7499 sort TUs by abbrev table
7500 for each TU with same abbrev table:
7501 read abbrev table if first user
7502 read TU top level DIE
7503 [IWBN if DWO skeletons had DW_AT_stmt_list]
7504 call FUNC */
7505
7506 if (dwarf_read_debug)
7507 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7508
7509 /* Sort in a separate table to maintain the order of all_type_units
7510 for .gdb_index: TU indices directly index all_type_units. */
7511 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7512 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7513
7514 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7515 sorted_by_abbrev.emplace_back
7516 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7517 sig_type->per_cu.section,
7518 sig_type->per_cu.sect_off));
7519
7520 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7521 sort_tu_by_abbrev_offset);
7522
7523 abbrev_offset = (sect_offset) ~(unsigned) 0;
7524
7525 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7526 {
7527 /* Switch to the next abbrev table if necessary. */
7528 if (abbrev_table == NULL
7529 || tu.abbrev_offset != abbrev_offset)
7530 {
7531 abbrev_offset = tu.abbrev_offset;
7532 abbrev_table =
7533 abbrev_table::read (dwarf2_per_objfile->objfile,
7534 &dwarf2_per_objfile->abbrev,
7535 abbrev_offset);
7536 ++tu_stats->nr_uniq_abbrev_tables;
7537 }
7538
7539 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7540 0, false);
7541 if (!reader.dummy_p)
7542 build_type_psymtabs_reader (&reader, reader.info_ptr,
7543 reader.comp_unit_die);
7544 }
7545 }
7546
7547 /* Print collected type unit statistics. */
7548
7549 static void
7550 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7551 {
7552 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7553
7554 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7555 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7556 dwarf2_per_objfile->all_type_units.size ());
7557 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7558 tu_stats->nr_uniq_abbrev_tables);
7559 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7560 tu_stats->nr_symtabs);
7561 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7562 tu_stats->nr_symtab_sharers);
7563 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7564 tu_stats->nr_stmt_less_type_units);
7565 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7566 tu_stats->nr_all_type_units_reallocs);
7567 }
7568
7569 /* Traversal function for build_type_psymtabs. */
7570
7571 static int
7572 build_type_psymtab_dependencies (void **slot, void *info)
7573 {
7574 struct dwarf2_per_objfile *dwarf2_per_objfile
7575 = (struct dwarf2_per_objfile *) info;
7576 struct objfile *objfile = dwarf2_per_objfile->objfile;
7577 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7578 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7579 dwarf2_psymtab *pst = per_cu->v.psymtab;
7580 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7581 int i;
7582
7583 gdb_assert (len > 0);
7584 gdb_assert (per_cu->type_unit_group_p ());
7585
7586 pst->number_of_dependencies = len;
7587 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7588 for (i = 0; i < len; ++i)
7589 {
7590 struct signatured_type *iter = tu_group->tus->at (i);
7591 gdb_assert (iter->per_cu.is_debug_types);
7592 pst->dependencies[i] = iter->per_cu.v.psymtab;
7593 iter->type_unit_group = tu_group;
7594 }
7595
7596 delete tu_group->tus;
7597 tu_group->tus = nullptr;
7598
7599 return 1;
7600 }
7601
7602 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7603 Build partial symbol tables for the .debug_types comp-units. */
7604
7605 static void
7606 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7607 {
7608 if (! create_all_type_units (dwarf2_per_objfile))
7609 return;
7610
7611 build_type_psymtabs_1 (dwarf2_per_objfile);
7612 }
7613
7614 /* Traversal function for process_skeletonless_type_unit.
7615 Read a TU in a DWO file and build partial symbols for it. */
7616
7617 static int
7618 process_skeletonless_type_unit (void **slot, void *info)
7619 {
7620 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7621 struct dwarf2_per_objfile *dwarf2_per_objfile
7622 = (struct dwarf2_per_objfile *) info;
7623 struct signatured_type find_entry, *entry;
7624
7625 /* If this TU doesn't exist in the global table, add it and read it in. */
7626
7627 if (dwarf2_per_objfile->signatured_types == NULL)
7628 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7629
7630 find_entry.signature = dwo_unit->signature;
7631 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7632 &find_entry, INSERT);
7633 /* If we've already seen this type there's nothing to do. What's happening
7634 is we're doing our own version of comdat-folding here. */
7635 if (*slot != NULL)
7636 return 1;
7637
7638 /* This does the job that create_all_type_units would have done for
7639 this TU. */
7640 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7641 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7642 *slot = entry;
7643
7644 /* This does the job that build_type_psymtabs_1 would have done. */
7645 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7646 if (!reader.dummy_p)
7647 build_type_psymtabs_reader (&reader, reader.info_ptr,
7648 reader.comp_unit_die);
7649
7650 return 1;
7651 }
7652
7653 /* Traversal function for process_skeletonless_type_units. */
7654
7655 static int
7656 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7657 {
7658 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7659
7660 if (dwo_file->tus != NULL)
7661 htab_traverse_noresize (dwo_file->tus.get (),
7662 process_skeletonless_type_unit, info);
7663
7664 return 1;
7665 }
7666
7667 /* Scan all TUs of DWO files, verifying we've processed them.
7668 This is needed in case a TU was emitted without its skeleton.
7669 Note: This can't be done until we know what all the DWO files are. */
7670
7671 static void
7672 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7673 {
7674 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7675 if (get_dwp_file (dwarf2_per_objfile) == NULL
7676 && dwarf2_per_objfile->dwo_files != NULL)
7677 {
7678 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7679 process_dwo_file_for_skeletonless_type_units,
7680 dwarf2_per_objfile);
7681 }
7682 }
7683
7684 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7685
7686 static void
7687 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7688 {
7689 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7690 {
7691 dwarf2_psymtab *pst = per_cu->v.psymtab;
7692
7693 if (pst == NULL)
7694 continue;
7695
7696 for (int j = 0; j < pst->number_of_dependencies; ++j)
7697 {
7698 /* Set the 'user' field only if it is not already set. */
7699 if (pst->dependencies[j]->user == NULL)
7700 pst->dependencies[j]->user = pst;
7701 }
7702 }
7703 }
7704
7705 /* Build the partial symbol table by doing a quick pass through the
7706 .debug_info and .debug_abbrev sections. */
7707
7708 static void
7709 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7710 {
7711 struct objfile *objfile = dwarf2_per_objfile->objfile;
7712
7713 if (dwarf_read_debug)
7714 {
7715 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7716 objfile_name (objfile));
7717 }
7718
7719 scoped_restore restore_reading_psyms
7720 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7721 true);
7722
7723 dwarf2_per_objfile->info.read (objfile);
7724
7725 /* Any cached compilation units will be linked by the per-objfile
7726 read_in_chain. Make sure to free them when we're done. */
7727 free_cached_comp_units freer (dwarf2_per_objfile);
7728
7729 build_type_psymtabs (dwarf2_per_objfile);
7730
7731 create_all_comp_units (dwarf2_per_objfile);
7732
7733 /* Create a temporary address map on a temporary obstack. We later
7734 copy this to the final obstack. */
7735 auto_obstack temp_obstack;
7736
7737 scoped_restore save_psymtabs_addrmap
7738 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7739 addrmap_create_mutable (&temp_obstack));
7740
7741 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7742 process_psymtab_comp_unit (per_cu, false, language_minimal);
7743
7744 /* This has to wait until we read the CUs, we need the list of DWOs. */
7745 process_skeletonless_type_units (dwarf2_per_objfile);
7746
7747 /* Now that all TUs have been processed we can fill in the dependencies. */
7748 if (dwarf2_per_objfile->type_unit_groups != NULL)
7749 {
7750 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7751 build_type_psymtab_dependencies, dwarf2_per_objfile);
7752 }
7753
7754 if (dwarf_read_debug)
7755 print_tu_stats (dwarf2_per_objfile);
7756
7757 set_partial_user (dwarf2_per_objfile);
7758
7759 objfile->partial_symtabs->psymtabs_addrmap
7760 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7761 objfile->partial_symtabs->obstack ());
7762 /* At this point we want to keep the address map. */
7763 save_psymtabs_addrmap.release ();
7764
7765 if (dwarf_read_debug)
7766 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7767 objfile_name (objfile));
7768 }
7769
7770 /* Load the partial DIEs for a secondary CU into memory.
7771 This is also used when rereading a primary CU with load_all_dies. */
7772
7773 static void
7774 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7775 {
7776 cutu_reader reader (this_cu, NULL, 1, false);
7777
7778 if (!reader.dummy_p)
7779 {
7780 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7781 language_minimal);
7782
7783 /* Check if comp unit has_children.
7784 If so, read the rest of the partial symbols from this comp unit.
7785 If not, there's no more debug_info for this comp unit. */
7786 if (reader.comp_unit_die->has_children)
7787 load_partial_dies (&reader, reader.info_ptr, 0);
7788
7789 reader.keep ();
7790 }
7791 }
7792
7793 static void
7794 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7795 struct dwarf2_section_info *section,
7796 struct dwarf2_section_info *abbrev_section,
7797 unsigned int is_dwz)
7798 {
7799 const gdb_byte *info_ptr;
7800 struct objfile *objfile = dwarf2_per_objfile->objfile;
7801
7802 if (dwarf_read_debug)
7803 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7804 section->get_name (),
7805 section->get_file_name ());
7806
7807 section->read (objfile);
7808
7809 info_ptr = section->buffer;
7810
7811 while (info_ptr < section->buffer + section->size)
7812 {
7813 struct dwarf2_per_cu_data *this_cu;
7814
7815 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7816
7817 comp_unit_head cu_header;
7818 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7819 abbrev_section, info_ptr,
7820 rcuh_kind::COMPILE);
7821
7822 /* Save the compilation unit for later lookup. */
7823 if (cu_header.unit_type != DW_UT_type)
7824 {
7825 this_cu = XOBNEW (&objfile->objfile_obstack,
7826 struct dwarf2_per_cu_data);
7827 memset (this_cu, 0, sizeof (*this_cu));
7828 }
7829 else
7830 {
7831 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7832 struct signatured_type);
7833 memset (sig_type, 0, sizeof (*sig_type));
7834 sig_type->signature = cu_header.signature;
7835 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7836 this_cu = &sig_type->per_cu;
7837 }
7838 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7839 this_cu->sect_off = sect_off;
7840 this_cu->length = cu_header.length + cu_header.initial_length_size;
7841 this_cu->is_dwz = is_dwz;
7842 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7843 this_cu->section = section;
7844
7845 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7846
7847 info_ptr = info_ptr + this_cu->length;
7848 }
7849 }
7850
7851 /* Create a list of all compilation units in OBJFILE.
7852 This is only done for -readnow and building partial symtabs. */
7853
7854 static void
7855 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7856 {
7857 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7858 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7859 &dwarf2_per_objfile->abbrev, 0);
7860
7861 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7862 if (dwz != NULL)
7863 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7864 1);
7865 }
7866
7867 /* Process all loaded DIEs for compilation unit CU, starting at
7868 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7869 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7870 DW_AT_ranges). See the comments of add_partial_subprogram on how
7871 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7872
7873 static void
7874 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7875 CORE_ADDR *highpc, int set_addrmap,
7876 struct dwarf2_cu *cu)
7877 {
7878 struct partial_die_info *pdi;
7879
7880 /* Now, march along the PDI's, descending into ones which have
7881 interesting children but skipping the children of the other ones,
7882 until we reach the end of the compilation unit. */
7883
7884 pdi = first_die;
7885
7886 while (pdi != NULL)
7887 {
7888 pdi->fixup (cu);
7889
7890 /* Anonymous namespaces or modules have no name but have interesting
7891 children, so we need to look at them. Ditto for anonymous
7892 enums. */
7893
7894 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7895 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7896 || pdi->tag == DW_TAG_imported_unit
7897 || pdi->tag == DW_TAG_inlined_subroutine)
7898 {
7899 switch (pdi->tag)
7900 {
7901 case DW_TAG_subprogram:
7902 case DW_TAG_inlined_subroutine:
7903 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7904 break;
7905 case DW_TAG_constant:
7906 case DW_TAG_variable:
7907 case DW_TAG_typedef:
7908 case DW_TAG_union_type:
7909 if (!pdi->is_declaration)
7910 {
7911 add_partial_symbol (pdi, cu);
7912 }
7913 break;
7914 case DW_TAG_class_type:
7915 case DW_TAG_interface_type:
7916 case DW_TAG_structure_type:
7917 if (!pdi->is_declaration)
7918 {
7919 add_partial_symbol (pdi, cu);
7920 }
7921 if ((cu->language == language_rust
7922 || cu->language == language_cplus) && pdi->has_children)
7923 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7924 set_addrmap, cu);
7925 break;
7926 case DW_TAG_enumeration_type:
7927 if (!pdi->is_declaration)
7928 add_partial_enumeration (pdi, cu);
7929 break;
7930 case DW_TAG_base_type:
7931 case DW_TAG_subrange_type:
7932 /* File scope base type definitions are added to the partial
7933 symbol table. */
7934 add_partial_symbol (pdi, cu);
7935 break;
7936 case DW_TAG_namespace:
7937 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7938 break;
7939 case DW_TAG_module:
7940 if (!pdi->is_declaration)
7941 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7942 break;
7943 case DW_TAG_imported_unit:
7944 {
7945 struct dwarf2_per_cu_data *per_cu;
7946
7947 /* For now we don't handle imported units in type units. */
7948 if (cu->per_cu->is_debug_types)
7949 {
7950 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7951 " supported in type units [in module %s]"),
7952 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7953 }
7954
7955 per_cu = dwarf2_find_containing_comp_unit
7956 (pdi->d.sect_off, pdi->is_dwz,
7957 cu->per_cu->dwarf2_per_objfile);
7958
7959 /* Go read the partial unit, if needed. */
7960 if (per_cu->v.psymtab == NULL)
7961 process_psymtab_comp_unit (per_cu, true, cu->language);
7962
7963 cu->per_cu->imported_symtabs_push (per_cu);
7964 }
7965 break;
7966 case DW_TAG_imported_declaration:
7967 add_partial_symbol (pdi, cu);
7968 break;
7969 default:
7970 break;
7971 }
7972 }
7973
7974 /* If the die has a sibling, skip to the sibling. */
7975
7976 pdi = pdi->die_sibling;
7977 }
7978 }
7979
7980 /* Functions used to compute the fully scoped name of a partial DIE.
7981
7982 Normally, this is simple. For C++, the parent DIE's fully scoped
7983 name is concatenated with "::" and the partial DIE's name.
7984 Enumerators are an exception; they use the scope of their parent
7985 enumeration type, i.e. the name of the enumeration type is not
7986 prepended to the enumerator.
7987
7988 There are two complexities. One is DW_AT_specification; in this
7989 case "parent" means the parent of the target of the specification,
7990 instead of the direct parent of the DIE. The other is compilers
7991 which do not emit DW_TAG_namespace; in this case we try to guess
7992 the fully qualified name of structure types from their members'
7993 linkage names. This must be done using the DIE's children rather
7994 than the children of any DW_AT_specification target. We only need
7995 to do this for structures at the top level, i.e. if the target of
7996 any DW_AT_specification (if any; otherwise the DIE itself) does not
7997 have a parent. */
7998
7999 /* Compute the scope prefix associated with PDI's parent, in
8000 compilation unit CU. The result will be allocated on CU's
8001 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8002 field. NULL is returned if no prefix is necessary. */
8003 static const char *
8004 partial_die_parent_scope (struct partial_die_info *pdi,
8005 struct dwarf2_cu *cu)
8006 {
8007 const char *grandparent_scope;
8008 struct partial_die_info *parent, *real_pdi;
8009
8010 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8011 then this means the parent of the specification DIE. */
8012
8013 real_pdi = pdi;
8014 while (real_pdi->has_specification)
8015 {
8016 auto res = find_partial_die (real_pdi->spec_offset,
8017 real_pdi->spec_is_dwz, cu);
8018 real_pdi = res.pdi;
8019 cu = res.cu;
8020 }
8021
8022 parent = real_pdi->die_parent;
8023 if (parent == NULL)
8024 return NULL;
8025
8026 if (parent->scope_set)
8027 return parent->scope;
8028
8029 parent->fixup (cu);
8030
8031 grandparent_scope = partial_die_parent_scope (parent, cu);
8032
8033 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8034 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8035 Work around this problem here. */
8036 if (cu->language == language_cplus
8037 && parent->tag == DW_TAG_namespace
8038 && strcmp (parent->name, "::") == 0
8039 && grandparent_scope == NULL)
8040 {
8041 parent->scope = NULL;
8042 parent->scope_set = 1;
8043 return NULL;
8044 }
8045
8046 /* Nested subroutines in Fortran get a prefix. */
8047 if (pdi->tag == DW_TAG_enumerator)
8048 /* Enumerators should not get the name of the enumeration as a prefix. */
8049 parent->scope = grandparent_scope;
8050 else if (parent->tag == DW_TAG_namespace
8051 || parent->tag == DW_TAG_module
8052 || parent->tag == DW_TAG_structure_type
8053 || parent->tag == DW_TAG_class_type
8054 || parent->tag == DW_TAG_interface_type
8055 || parent->tag == DW_TAG_union_type
8056 || parent->tag == DW_TAG_enumeration_type
8057 || (cu->language == language_fortran
8058 && parent->tag == DW_TAG_subprogram
8059 && pdi->tag == DW_TAG_subprogram))
8060 {
8061 if (grandparent_scope == NULL)
8062 parent->scope = parent->name;
8063 else
8064 parent->scope = typename_concat (&cu->comp_unit_obstack,
8065 grandparent_scope,
8066 parent->name, 0, cu);
8067 }
8068 else
8069 {
8070 /* FIXME drow/2004-04-01: What should we be doing with
8071 function-local names? For partial symbols, we should probably be
8072 ignoring them. */
8073 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8074 dwarf_tag_name (parent->tag),
8075 sect_offset_str (pdi->sect_off));
8076 parent->scope = grandparent_scope;
8077 }
8078
8079 parent->scope_set = 1;
8080 return parent->scope;
8081 }
8082
8083 /* Return the fully scoped name associated with PDI, from compilation unit
8084 CU. The result will be allocated with malloc. */
8085
8086 static gdb::unique_xmalloc_ptr<char>
8087 partial_die_full_name (struct partial_die_info *pdi,
8088 struct dwarf2_cu *cu)
8089 {
8090 const char *parent_scope;
8091
8092 /* If this is a template instantiation, we can not work out the
8093 template arguments from partial DIEs. So, unfortunately, we have
8094 to go through the full DIEs. At least any work we do building
8095 types here will be reused if full symbols are loaded later. */
8096 if (pdi->has_template_arguments)
8097 {
8098 pdi->fixup (cu);
8099
8100 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8101 {
8102 struct die_info *die;
8103 struct attribute attr;
8104 struct dwarf2_cu *ref_cu = cu;
8105
8106 /* DW_FORM_ref_addr is using section offset. */
8107 attr.name = (enum dwarf_attribute) 0;
8108 attr.form = DW_FORM_ref_addr;
8109 attr.u.unsnd = to_underlying (pdi->sect_off);
8110 die = follow_die_ref (NULL, &attr, &ref_cu);
8111
8112 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8113 }
8114 }
8115
8116 parent_scope = partial_die_parent_scope (pdi, cu);
8117 if (parent_scope == NULL)
8118 return NULL;
8119 else
8120 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8121 pdi->name, 0, cu));
8122 }
8123
8124 static void
8125 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8126 {
8127 struct dwarf2_per_objfile *dwarf2_per_objfile
8128 = cu->per_cu->dwarf2_per_objfile;
8129 struct objfile *objfile = dwarf2_per_objfile->objfile;
8130 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8131 CORE_ADDR addr = 0;
8132 const char *actual_name = NULL;
8133 CORE_ADDR baseaddr;
8134
8135 baseaddr = objfile->text_section_offset ();
8136
8137 gdb::unique_xmalloc_ptr<char> built_actual_name
8138 = partial_die_full_name (pdi, cu);
8139 if (built_actual_name != NULL)
8140 actual_name = built_actual_name.get ();
8141
8142 if (actual_name == NULL)
8143 actual_name = pdi->name;
8144
8145 switch (pdi->tag)
8146 {
8147 case DW_TAG_inlined_subroutine:
8148 case DW_TAG_subprogram:
8149 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8150 - baseaddr);
8151 if (pdi->is_external
8152 || cu->language == language_ada
8153 || (cu->language == language_fortran
8154 && pdi->die_parent != NULL
8155 && pdi->die_parent->tag == DW_TAG_subprogram))
8156 {
8157 /* Normally, only "external" DIEs are part of the global scope.
8158 But in Ada and Fortran, we want to be able to access nested
8159 procedures globally. So all Ada and Fortran subprograms are
8160 stored in the global scope. */
8161 add_psymbol_to_list (actual_name,
8162 built_actual_name != NULL,
8163 VAR_DOMAIN, LOC_BLOCK,
8164 SECT_OFF_TEXT (objfile),
8165 psymbol_placement::GLOBAL,
8166 addr,
8167 cu->language, objfile);
8168 }
8169 else
8170 {
8171 add_psymbol_to_list (actual_name,
8172 built_actual_name != NULL,
8173 VAR_DOMAIN, LOC_BLOCK,
8174 SECT_OFF_TEXT (objfile),
8175 psymbol_placement::STATIC,
8176 addr, cu->language, objfile);
8177 }
8178
8179 if (pdi->main_subprogram && actual_name != NULL)
8180 set_objfile_main_name (objfile, actual_name, cu->language);
8181 break;
8182 case DW_TAG_constant:
8183 add_psymbol_to_list (actual_name,
8184 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8185 -1, (pdi->is_external
8186 ? psymbol_placement::GLOBAL
8187 : psymbol_placement::STATIC),
8188 0, cu->language, objfile);
8189 break;
8190 case DW_TAG_variable:
8191 if (pdi->d.locdesc)
8192 addr = decode_locdesc (pdi->d.locdesc, cu);
8193
8194 if (pdi->d.locdesc
8195 && addr == 0
8196 && !dwarf2_per_objfile->has_section_at_zero)
8197 {
8198 /* A global or static variable may also have been stripped
8199 out by the linker if unused, in which case its address
8200 will be nullified; do not add such variables into partial
8201 symbol table then. */
8202 }
8203 else if (pdi->is_external)
8204 {
8205 /* Global Variable.
8206 Don't enter into the minimal symbol tables as there is
8207 a minimal symbol table entry from the ELF symbols already.
8208 Enter into partial symbol table if it has a location
8209 descriptor or a type.
8210 If the location descriptor is missing, new_symbol will create
8211 a LOC_UNRESOLVED symbol, the address of the variable will then
8212 be determined from the minimal symbol table whenever the variable
8213 is referenced.
8214 The address for the partial symbol table entry is not
8215 used by GDB, but it comes in handy for debugging partial symbol
8216 table building. */
8217
8218 if (pdi->d.locdesc || pdi->has_type)
8219 add_psymbol_to_list (actual_name,
8220 built_actual_name != NULL,
8221 VAR_DOMAIN, LOC_STATIC,
8222 SECT_OFF_TEXT (objfile),
8223 psymbol_placement::GLOBAL,
8224 addr, cu->language, objfile);
8225 }
8226 else
8227 {
8228 int has_loc = pdi->d.locdesc != NULL;
8229
8230 /* Static Variable. Skip symbols whose value we cannot know (those
8231 without location descriptors or constant values). */
8232 if (!has_loc && !pdi->has_const_value)
8233 return;
8234
8235 add_psymbol_to_list (actual_name,
8236 built_actual_name != NULL,
8237 VAR_DOMAIN, LOC_STATIC,
8238 SECT_OFF_TEXT (objfile),
8239 psymbol_placement::STATIC,
8240 has_loc ? addr : 0,
8241 cu->language, objfile);
8242 }
8243 break;
8244 case DW_TAG_typedef:
8245 case DW_TAG_base_type:
8246 case DW_TAG_subrange_type:
8247 add_psymbol_to_list (actual_name,
8248 built_actual_name != NULL,
8249 VAR_DOMAIN, LOC_TYPEDEF, -1,
8250 psymbol_placement::STATIC,
8251 0, cu->language, objfile);
8252 break;
8253 case DW_TAG_imported_declaration:
8254 case DW_TAG_namespace:
8255 add_psymbol_to_list (actual_name,
8256 built_actual_name != NULL,
8257 VAR_DOMAIN, LOC_TYPEDEF, -1,
8258 psymbol_placement::GLOBAL,
8259 0, cu->language, objfile);
8260 break;
8261 case DW_TAG_module:
8262 /* With Fortran 77 there might be a "BLOCK DATA" module
8263 available without any name. If so, we skip the module as it
8264 doesn't bring any value. */
8265 if (actual_name != nullptr)
8266 add_psymbol_to_list (actual_name,
8267 built_actual_name != NULL,
8268 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8269 psymbol_placement::GLOBAL,
8270 0, cu->language, objfile);
8271 break;
8272 case DW_TAG_class_type:
8273 case DW_TAG_interface_type:
8274 case DW_TAG_structure_type:
8275 case DW_TAG_union_type:
8276 case DW_TAG_enumeration_type:
8277 /* Skip external references. The DWARF standard says in the section
8278 about "Structure, Union, and Class Type Entries": "An incomplete
8279 structure, union or class type is represented by a structure,
8280 union or class entry that does not have a byte size attribute
8281 and that has a DW_AT_declaration attribute." */
8282 if (!pdi->has_byte_size && pdi->is_declaration)
8283 return;
8284
8285 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8286 static vs. global. */
8287 add_psymbol_to_list (actual_name,
8288 built_actual_name != NULL,
8289 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8290 cu->language == language_cplus
8291 ? psymbol_placement::GLOBAL
8292 : psymbol_placement::STATIC,
8293 0, cu->language, objfile);
8294
8295 break;
8296 case DW_TAG_enumerator:
8297 add_psymbol_to_list (actual_name,
8298 built_actual_name != NULL,
8299 VAR_DOMAIN, LOC_CONST, -1,
8300 cu->language == language_cplus
8301 ? psymbol_placement::GLOBAL
8302 : psymbol_placement::STATIC,
8303 0, cu->language, objfile);
8304 break;
8305 default:
8306 break;
8307 }
8308 }
8309
8310 /* Read a partial die corresponding to a namespace; also, add a symbol
8311 corresponding to that namespace to the symbol table. NAMESPACE is
8312 the name of the enclosing namespace. */
8313
8314 static void
8315 add_partial_namespace (struct partial_die_info *pdi,
8316 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8317 int set_addrmap, struct dwarf2_cu *cu)
8318 {
8319 /* Add a symbol for the namespace. */
8320
8321 add_partial_symbol (pdi, cu);
8322
8323 /* Now scan partial symbols in that namespace. */
8324
8325 if (pdi->has_children)
8326 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8327 }
8328
8329 /* Read a partial die corresponding to a Fortran module. */
8330
8331 static void
8332 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8333 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8334 {
8335 /* Add a symbol for the namespace. */
8336
8337 add_partial_symbol (pdi, cu);
8338
8339 /* Now scan partial symbols in that module. */
8340
8341 if (pdi->has_children)
8342 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8343 }
8344
8345 /* Read a partial die corresponding to a subprogram or an inlined
8346 subprogram and create a partial symbol for that subprogram.
8347 When the CU language allows it, this routine also defines a partial
8348 symbol for each nested subprogram that this subprogram contains.
8349 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8350 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8351
8352 PDI may also be a lexical block, in which case we simply search
8353 recursively for subprograms defined inside that lexical block.
8354 Again, this is only performed when the CU language allows this
8355 type of definitions. */
8356
8357 static void
8358 add_partial_subprogram (struct partial_die_info *pdi,
8359 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8360 int set_addrmap, struct dwarf2_cu *cu)
8361 {
8362 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8363 {
8364 if (pdi->has_pc_info)
8365 {
8366 if (pdi->lowpc < *lowpc)
8367 *lowpc = pdi->lowpc;
8368 if (pdi->highpc > *highpc)
8369 *highpc = pdi->highpc;
8370 if (set_addrmap)
8371 {
8372 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8373 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8374 CORE_ADDR baseaddr;
8375 CORE_ADDR this_highpc;
8376 CORE_ADDR this_lowpc;
8377
8378 baseaddr = objfile->text_section_offset ();
8379 this_lowpc
8380 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8381 pdi->lowpc + baseaddr)
8382 - baseaddr);
8383 this_highpc
8384 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8385 pdi->highpc + baseaddr)
8386 - baseaddr);
8387 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8388 this_lowpc, this_highpc - 1,
8389 cu->per_cu->v.psymtab);
8390 }
8391 }
8392
8393 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8394 {
8395 if (!pdi->is_declaration)
8396 /* Ignore subprogram DIEs that do not have a name, they are
8397 illegal. Do not emit a complaint at this point, we will
8398 do so when we convert this psymtab into a symtab. */
8399 if (pdi->name)
8400 add_partial_symbol (pdi, cu);
8401 }
8402 }
8403
8404 if (! pdi->has_children)
8405 return;
8406
8407 if (cu->language == language_ada || cu->language == language_fortran)
8408 {
8409 pdi = pdi->die_child;
8410 while (pdi != NULL)
8411 {
8412 pdi->fixup (cu);
8413 if (pdi->tag == DW_TAG_subprogram
8414 || pdi->tag == DW_TAG_inlined_subroutine
8415 || pdi->tag == DW_TAG_lexical_block)
8416 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8417 pdi = pdi->die_sibling;
8418 }
8419 }
8420 }
8421
8422 /* Read a partial die corresponding to an enumeration type. */
8423
8424 static void
8425 add_partial_enumeration (struct partial_die_info *enum_pdi,
8426 struct dwarf2_cu *cu)
8427 {
8428 struct partial_die_info *pdi;
8429
8430 if (enum_pdi->name != NULL)
8431 add_partial_symbol (enum_pdi, cu);
8432
8433 pdi = enum_pdi->die_child;
8434 while (pdi)
8435 {
8436 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8437 complaint (_("malformed enumerator DIE ignored"));
8438 else
8439 add_partial_symbol (pdi, cu);
8440 pdi = pdi->die_sibling;
8441 }
8442 }
8443
8444 /* Return the initial uleb128 in the die at INFO_PTR. */
8445
8446 static unsigned int
8447 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8448 {
8449 unsigned int bytes_read;
8450
8451 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8452 }
8453
8454 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8455 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8456
8457 Return the corresponding abbrev, or NULL if the number is zero (indicating
8458 an empty DIE). In either case *BYTES_READ will be set to the length of
8459 the initial number. */
8460
8461 static struct abbrev_info *
8462 peek_die_abbrev (const die_reader_specs &reader,
8463 const gdb_byte *info_ptr, unsigned int *bytes_read)
8464 {
8465 dwarf2_cu *cu = reader.cu;
8466 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8467 unsigned int abbrev_number
8468 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8469
8470 if (abbrev_number == 0)
8471 return NULL;
8472
8473 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8474 if (!abbrev)
8475 {
8476 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8477 " at offset %s [in module %s]"),
8478 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8479 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8480 }
8481
8482 return abbrev;
8483 }
8484
8485 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8486 Returns a pointer to the end of a series of DIEs, terminated by an empty
8487 DIE. Any children of the skipped DIEs will also be skipped. */
8488
8489 static const gdb_byte *
8490 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8491 {
8492 while (1)
8493 {
8494 unsigned int bytes_read;
8495 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8496
8497 if (abbrev == NULL)
8498 return info_ptr + bytes_read;
8499 else
8500 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8501 }
8502 }
8503
8504 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8505 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8506 abbrev corresponding to that skipped uleb128 should be passed in
8507 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8508 children. */
8509
8510 static const gdb_byte *
8511 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8512 struct abbrev_info *abbrev)
8513 {
8514 unsigned int bytes_read;
8515 struct attribute attr;
8516 bfd *abfd = reader->abfd;
8517 struct dwarf2_cu *cu = reader->cu;
8518 const gdb_byte *buffer = reader->buffer;
8519 const gdb_byte *buffer_end = reader->buffer_end;
8520 unsigned int form, i;
8521
8522 for (i = 0; i < abbrev->num_attrs; i++)
8523 {
8524 /* The only abbrev we care about is DW_AT_sibling. */
8525 if (abbrev->attrs[i].name == DW_AT_sibling)
8526 {
8527 bool ignored;
8528 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8529 &ignored);
8530 if (attr.form == DW_FORM_ref_addr)
8531 complaint (_("ignoring absolute DW_AT_sibling"));
8532 else
8533 {
8534 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8535 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8536
8537 if (sibling_ptr < info_ptr)
8538 complaint (_("DW_AT_sibling points backwards"));
8539 else if (sibling_ptr > reader->buffer_end)
8540 dwarf2_section_buffer_overflow_complaint (reader->die_section);
8541 else
8542 return sibling_ptr;
8543 }
8544 }
8545
8546 /* If it isn't DW_AT_sibling, skip this attribute. */
8547 form = abbrev->attrs[i].form;
8548 skip_attribute:
8549 switch (form)
8550 {
8551 case DW_FORM_ref_addr:
8552 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8553 and later it is offset sized. */
8554 if (cu->header.version == 2)
8555 info_ptr += cu->header.addr_size;
8556 else
8557 info_ptr += cu->header.offset_size;
8558 break;
8559 case DW_FORM_GNU_ref_alt:
8560 info_ptr += cu->header.offset_size;
8561 break;
8562 case DW_FORM_addr:
8563 info_ptr += cu->header.addr_size;
8564 break;
8565 case DW_FORM_data1:
8566 case DW_FORM_ref1:
8567 case DW_FORM_flag:
8568 case DW_FORM_strx1:
8569 info_ptr += 1;
8570 break;
8571 case DW_FORM_flag_present:
8572 case DW_FORM_implicit_const:
8573 break;
8574 case DW_FORM_data2:
8575 case DW_FORM_ref2:
8576 case DW_FORM_strx2:
8577 info_ptr += 2;
8578 break;
8579 case DW_FORM_strx3:
8580 info_ptr += 3;
8581 break;
8582 case DW_FORM_data4:
8583 case DW_FORM_ref4:
8584 case DW_FORM_strx4:
8585 info_ptr += 4;
8586 break;
8587 case DW_FORM_data8:
8588 case DW_FORM_ref8:
8589 case DW_FORM_ref_sig8:
8590 info_ptr += 8;
8591 break;
8592 case DW_FORM_data16:
8593 info_ptr += 16;
8594 break;
8595 case DW_FORM_string:
8596 read_direct_string (abfd, info_ptr, &bytes_read);
8597 info_ptr += bytes_read;
8598 break;
8599 case DW_FORM_sec_offset:
8600 case DW_FORM_strp:
8601 case DW_FORM_GNU_strp_alt:
8602 info_ptr += cu->header.offset_size;
8603 break;
8604 case DW_FORM_exprloc:
8605 case DW_FORM_block:
8606 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8607 info_ptr += bytes_read;
8608 break;
8609 case DW_FORM_block1:
8610 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8611 break;
8612 case DW_FORM_block2:
8613 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8614 break;
8615 case DW_FORM_block4:
8616 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8617 break;
8618 case DW_FORM_addrx:
8619 case DW_FORM_strx:
8620 case DW_FORM_sdata:
8621 case DW_FORM_udata:
8622 case DW_FORM_ref_udata:
8623 case DW_FORM_GNU_addr_index:
8624 case DW_FORM_GNU_str_index:
8625 case DW_FORM_rnglistx:
8626 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8627 break;
8628 case DW_FORM_indirect:
8629 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8630 info_ptr += bytes_read;
8631 /* We need to continue parsing from here, so just go back to
8632 the top. */
8633 goto skip_attribute;
8634
8635 default:
8636 error (_("Dwarf Error: Cannot handle %s "
8637 "in DWARF reader [in module %s]"),
8638 dwarf_form_name (form),
8639 bfd_get_filename (abfd));
8640 }
8641 }
8642
8643 if (abbrev->has_children)
8644 return skip_children (reader, info_ptr);
8645 else
8646 return info_ptr;
8647 }
8648
8649 /* Locate ORIG_PDI's sibling.
8650 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8651
8652 static const gdb_byte *
8653 locate_pdi_sibling (const struct die_reader_specs *reader,
8654 struct partial_die_info *orig_pdi,
8655 const gdb_byte *info_ptr)
8656 {
8657 /* Do we know the sibling already? */
8658
8659 if (orig_pdi->sibling)
8660 return orig_pdi->sibling;
8661
8662 /* Are there any children to deal with? */
8663
8664 if (!orig_pdi->has_children)
8665 return info_ptr;
8666
8667 /* Skip the children the long way. */
8668
8669 return skip_children (reader, info_ptr);
8670 }
8671
8672 /* Expand this partial symbol table into a full symbol table. SELF is
8673 not NULL. */
8674
8675 void
8676 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8677 {
8678 struct dwarf2_per_objfile *dwarf2_per_objfile
8679 = get_dwarf2_per_objfile (objfile);
8680
8681 gdb_assert (!readin);
8682 /* If this psymtab is constructed from a debug-only objfile, the
8683 has_section_at_zero flag will not necessarily be correct. We
8684 can get the correct value for this flag by looking at the data
8685 associated with the (presumably stripped) associated objfile. */
8686 if (objfile->separate_debug_objfile_backlink)
8687 {
8688 struct dwarf2_per_objfile *dpo_backlink
8689 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8690
8691 dwarf2_per_objfile->has_section_at_zero
8692 = dpo_backlink->has_section_at_zero;
8693 }
8694
8695 expand_psymtab (objfile);
8696
8697 process_cu_includes (dwarf2_per_objfile);
8698 }
8699 \f
8700 /* Reading in full CUs. */
8701
8702 /* Add PER_CU to the queue. */
8703
8704 static void
8705 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8706 enum language pretend_language)
8707 {
8708 per_cu->queued = 1;
8709 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8710 }
8711
8712 /* If PER_CU is not yet queued, add it to the queue.
8713 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8714 dependency.
8715 The result is non-zero if PER_CU was queued, otherwise the result is zero
8716 meaning either PER_CU is already queued or it is already loaded.
8717
8718 N.B. There is an invariant here that if a CU is queued then it is loaded.
8719 The caller is required to load PER_CU if we return non-zero. */
8720
8721 static int
8722 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8723 struct dwarf2_per_cu_data *per_cu,
8724 enum language pretend_language)
8725 {
8726 /* We may arrive here during partial symbol reading, if we need full
8727 DIEs to process an unusual case (e.g. template arguments). Do
8728 not queue PER_CU, just tell our caller to load its DIEs. */
8729 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8730 {
8731 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8732 return 1;
8733 return 0;
8734 }
8735
8736 /* Mark the dependence relation so that we don't flush PER_CU
8737 too early. */
8738 if (dependent_cu != NULL)
8739 dwarf2_add_dependence (dependent_cu, per_cu);
8740
8741 /* If it's already on the queue, we have nothing to do. */
8742 if (per_cu->queued)
8743 return 0;
8744
8745 /* If the compilation unit is already loaded, just mark it as
8746 used. */
8747 if (per_cu->cu != NULL)
8748 {
8749 per_cu->cu->last_used = 0;
8750 return 0;
8751 }
8752
8753 /* Add it to the queue. */
8754 queue_comp_unit (per_cu, pretend_language);
8755
8756 return 1;
8757 }
8758
8759 /* Process the queue. */
8760
8761 static void
8762 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8763 {
8764 if (dwarf_read_debug)
8765 {
8766 fprintf_unfiltered (gdb_stdlog,
8767 "Expanding one or more symtabs of objfile %s ...\n",
8768 objfile_name (dwarf2_per_objfile->objfile));
8769 }
8770
8771 /* The queue starts out with one item, but following a DIE reference
8772 may load a new CU, adding it to the end of the queue. */
8773 while (!dwarf2_per_objfile->queue.empty ())
8774 {
8775 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8776
8777 if ((dwarf2_per_objfile->using_index
8778 ? !item.per_cu->v.quick->compunit_symtab
8779 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8780 /* Skip dummy CUs. */
8781 && item.per_cu->cu != NULL)
8782 {
8783 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8784 unsigned int debug_print_threshold;
8785 char buf[100];
8786
8787 if (per_cu->is_debug_types)
8788 {
8789 struct signatured_type *sig_type =
8790 (struct signatured_type *) per_cu;
8791
8792 sprintf (buf, "TU %s at offset %s",
8793 hex_string (sig_type->signature),
8794 sect_offset_str (per_cu->sect_off));
8795 /* There can be 100s of TUs.
8796 Only print them in verbose mode. */
8797 debug_print_threshold = 2;
8798 }
8799 else
8800 {
8801 sprintf (buf, "CU at offset %s",
8802 sect_offset_str (per_cu->sect_off));
8803 debug_print_threshold = 1;
8804 }
8805
8806 if (dwarf_read_debug >= debug_print_threshold)
8807 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8808
8809 if (per_cu->is_debug_types)
8810 process_full_type_unit (per_cu, item.pretend_language);
8811 else
8812 process_full_comp_unit (per_cu, item.pretend_language);
8813
8814 if (dwarf_read_debug >= debug_print_threshold)
8815 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8816 }
8817
8818 item.per_cu->queued = 0;
8819 dwarf2_per_objfile->queue.pop ();
8820 }
8821
8822 if (dwarf_read_debug)
8823 {
8824 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8825 objfile_name (dwarf2_per_objfile->objfile));
8826 }
8827 }
8828
8829 /* Read in full symbols for PST, and anything it depends on. */
8830
8831 void
8832 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8833 {
8834 struct dwarf2_per_cu_data *per_cu;
8835
8836 if (readin)
8837 return;
8838
8839 read_dependencies (objfile);
8840
8841 per_cu = per_cu_data;
8842
8843 if (per_cu == NULL)
8844 {
8845 /* It's an include file, no symbols to read for it.
8846 Everything is in the parent symtab. */
8847 readin = true;
8848 return;
8849 }
8850
8851 dw2_do_instantiate_symtab (per_cu, false);
8852 }
8853
8854 /* Trivial hash function for die_info: the hash value of a DIE
8855 is its offset in .debug_info for this objfile. */
8856
8857 static hashval_t
8858 die_hash (const void *item)
8859 {
8860 const struct die_info *die = (const struct die_info *) item;
8861
8862 return to_underlying (die->sect_off);
8863 }
8864
8865 /* Trivial comparison function for die_info structures: two DIEs
8866 are equal if they have the same offset. */
8867
8868 static int
8869 die_eq (const void *item_lhs, const void *item_rhs)
8870 {
8871 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8872 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8873
8874 return die_lhs->sect_off == die_rhs->sect_off;
8875 }
8876
8877 /* Load the DIEs associated with PER_CU into memory. */
8878
8879 static void
8880 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8881 bool skip_partial,
8882 enum language pretend_language)
8883 {
8884 gdb_assert (! this_cu->is_debug_types);
8885
8886 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8887 if (reader.dummy_p)
8888 return;
8889
8890 struct dwarf2_cu *cu = reader.cu;
8891 const gdb_byte *info_ptr = reader.info_ptr;
8892
8893 gdb_assert (cu->die_hash == NULL);
8894 cu->die_hash =
8895 htab_create_alloc_ex (cu->header.length / 12,
8896 die_hash,
8897 die_eq,
8898 NULL,
8899 &cu->comp_unit_obstack,
8900 hashtab_obstack_allocate,
8901 dummy_obstack_deallocate);
8902
8903 if (reader.comp_unit_die->has_children)
8904 reader.comp_unit_die->child
8905 = read_die_and_siblings (&reader, reader.info_ptr,
8906 &info_ptr, reader.comp_unit_die);
8907 cu->dies = reader.comp_unit_die;
8908 /* comp_unit_die is not stored in die_hash, no need. */
8909
8910 /* We try not to read any attributes in this function, because not
8911 all CUs needed for references have been loaded yet, and symbol
8912 table processing isn't initialized. But we have to set the CU language,
8913 or we won't be able to build types correctly.
8914 Similarly, if we do not read the producer, we can not apply
8915 producer-specific interpretation. */
8916 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8917
8918 reader.keep ();
8919 }
8920
8921 /* Add a DIE to the delayed physname list. */
8922
8923 static void
8924 add_to_method_list (struct type *type, int fnfield_index, int index,
8925 const char *name, struct die_info *die,
8926 struct dwarf2_cu *cu)
8927 {
8928 struct delayed_method_info mi;
8929 mi.type = type;
8930 mi.fnfield_index = fnfield_index;
8931 mi.index = index;
8932 mi.name = name;
8933 mi.die = die;
8934 cu->method_list.push_back (mi);
8935 }
8936
8937 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8938 "const" / "volatile". If so, decrements LEN by the length of the
8939 modifier and return true. Otherwise return false. */
8940
8941 template<size_t N>
8942 static bool
8943 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8944 {
8945 size_t mod_len = sizeof (mod) - 1;
8946 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8947 {
8948 len -= mod_len;
8949 return true;
8950 }
8951 return false;
8952 }
8953
8954 /* Compute the physnames of any methods on the CU's method list.
8955
8956 The computation of method physnames is delayed in order to avoid the
8957 (bad) condition that one of the method's formal parameters is of an as yet
8958 incomplete type. */
8959
8960 static void
8961 compute_delayed_physnames (struct dwarf2_cu *cu)
8962 {
8963 /* Only C++ delays computing physnames. */
8964 if (cu->method_list.empty ())
8965 return;
8966 gdb_assert (cu->language == language_cplus);
8967
8968 for (const delayed_method_info &mi : cu->method_list)
8969 {
8970 const char *physname;
8971 struct fn_fieldlist *fn_flp
8972 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8973 physname = dwarf2_physname (mi.name, mi.die, cu);
8974 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8975 = physname ? physname : "";
8976
8977 /* Since there's no tag to indicate whether a method is a
8978 const/volatile overload, extract that information out of the
8979 demangled name. */
8980 if (physname != NULL)
8981 {
8982 size_t len = strlen (physname);
8983
8984 while (1)
8985 {
8986 if (physname[len] == ')') /* shortcut */
8987 break;
8988 else if (check_modifier (physname, len, " const"))
8989 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8990 else if (check_modifier (physname, len, " volatile"))
8991 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8992 else
8993 break;
8994 }
8995 }
8996 }
8997
8998 /* The list is no longer needed. */
8999 cu->method_list.clear ();
9000 }
9001
9002 /* Go objects should be embedded in a DW_TAG_module DIE,
9003 and it's not clear if/how imported objects will appear.
9004 To keep Go support simple until that's worked out,
9005 go back through what we've read and create something usable.
9006 We could do this while processing each DIE, and feels kinda cleaner,
9007 but that way is more invasive.
9008 This is to, for example, allow the user to type "p var" or "b main"
9009 without having to specify the package name, and allow lookups
9010 of module.object to work in contexts that use the expression
9011 parser. */
9012
9013 static void
9014 fixup_go_packaging (struct dwarf2_cu *cu)
9015 {
9016 gdb::unique_xmalloc_ptr<char> package_name;
9017 struct pending *list;
9018 int i;
9019
9020 for (list = *cu->get_builder ()->get_global_symbols ();
9021 list != NULL;
9022 list = list->next)
9023 {
9024 for (i = 0; i < list->nsyms; ++i)
9025 {
9026 struct symbol *sym = list->symbol[i];
9027
9028 if (sym->language () == language_go
9029 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9030 {
9031 gdb::unique_xmalloc_ptr<char> this_package_name
9032 (go_symbol_package_name (sym));
9033
9034 if (this_package_name == NULL)
9035 continue;
9036 if (package_name == NULL)
9037 package_name = std::move (this_package_name);
9038 else
9039 {
9040 struct objfile *objfile
9041 = cu->per_cu->dwarf2_per_objfile->objfile;
9042 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9043 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9044 (symbol_symtab (sym) != NULL
9045 ? symtab_to_filename_for_display
9046 (symbol_symtab (sym))
9047 : objfile_name (objfile)),
9048 this_package_name.get (), package_name.get ());
9049 }
9050 }
9051 }
9052 }
9053
9054 if (package_name != NULL)
9055 {
9056 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9057 const char *saved_package_name
9058 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9059 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9060 saved_package_name);
9061 struct symbol *sym;
9062
9063 sym = allocate_symbol (objfile);
9064 sym->set_language (language_go, &objfile->objfile_obstack);
9065 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9066 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9067 e.g., "main" finds the "main" module and not C's main(). */
9068 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9069 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9070 SYMBOL_TYPE (sym) = type;
9071
9072 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9073 }
9074 }
9075
9076 /* Allocate a fully-qualified name consisting of the two parts on the
9077 obstack. */
9078
9079 static const char *
9080 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9081 {
9082 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9083 }
9084
9085 /* A helper that allocates a struct discriminant_info to attach to a
9086 union type. */
9087
9088 static struct discriminant_info *
9089 alloc_discriminant_info (struct type *type, int discriminant_index,
9090 int default_index)
9091 {
9092 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9093 gdb_assert (discriminant_index == -1
9094 || (discriminant_index >= 0
9095 && discriminant_index < TYPE_NFIELDS (type)));
9096 gdb_assert (default_index == -1
9097 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9098
9099 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9100
9101 struct discriminant_info *disc
9102 = ((struct discriminant_info *)
9103 TYPE_ZALLOC (type,
9104 offsetof (struct discriminant_info, discriminants)
9105 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9106 disc->default_index = default_index;
9107 disc->discriminant_index = discriminant_index;
9108
9109 struct dynamic_prop prop;
9110 prop.kind = PROP_UNDEFINED;
9111 prop.data.baton = disc;
9112
9113 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9114
9115 return disc;
9116 }
9117
9118 /* Some versions of rustc emitted enums in an unusual way.
9119
9120 Ordinary enums were emitted as unions. The first element of each
9121 structure in the union was named "RUST$ENUM$DISR". This element
9122 held the discriminant.
9123
9124 These versions of Rust also implemented the "non-zero"
9125 optimization. When the enum had two values, and one is empty and
9126 the other holds a pointer that cannot be zero, the pointer is used
9127 as the discriminant, with a zero value meaning the empty variant.
9128 Here, the union's first member is of the form
9129 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9130 where the fieldnos are the indices of the fields that should be
9131 traversed in order to find the field (which may be several fields deep)
9132 and the variantname is the name of the variant of the case when the
9133 field is zero.
9134
9135 This function recognizes whether TYPE is of one of these forms,
9136 and, if so, smashes it to be a variant type. */
9137
9138 static void
9139 quirk_rust_enum (struct type *type, struct objfile *objfile)
9140 {
9141 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9142
9143 /* We don't need to deal with empty enums. */
9144 if (TYPE_NFIELDS (type) == 0)
9145 return;
9146
9147 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9148 if (TYPE_NFIELDS (type) == 1
9149 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9150 {
9151 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9152
9153 /* Decode the field name to find the offset of the
9154 discriminant. */
9155 ULONGEST bit_offset = 0;
9156 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9157 while (name[0] >= '0' && name[0] <= '9')
9158 {
9159 char *tail;
9160 unsigned long index = strtoul (name, &tail, 10);
9161 name = tail;
9162 if (*name != '$'
9163 || index >= TYPE_NFIELDS (field_type)
9164 || (TYPE_FIELD_LOC_KIND (field_type, index)
9165 != FIELD_LOC_KIND_BITPOS))
9166 {
9167 complaint (_("Could not parse Rust enum encoding string \"%s\""
9168 "[in module %s]"),
9169 TYPE_FIELD_NAME (type, 0),
9170 objfile_name (objfile));
9171 return;
9172 }
9173 ++name;
9174
9175 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9176 field_type = TYPE_FIELD_TYPE (field_type, index);
9177 }
9178
9179 /* Make a union to hold the variants. */
9180 struct type *union_type = alloc_type (objfile);
9181 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9182 TYPE_NFIELDS (union_type) = 3;
9183 TYPE_FIELDS (union_type)
9184 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9185 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9186 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9187
9188 /* Put the discriminant must at index 0. */
9189 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9190 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9191 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9192 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9193
9194 /* The order of fields doesn't really matter, so put the real
9195 field at index 1 and the data-less field at index 2. */
9196 struct discriminant_info *disc
9197 = alloc_discriminant_info (union_type, 0, 1);
9198 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9199 TYPE_FIELD_NAME (union_type, 1)
9200 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9201 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9202 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9203 TYPE_FIELD_NAME (union_type, 1));
9204
9205 const char *dataless_name
9206 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9207 name);
9208 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9209 dataless_name);
9210 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9211 /* NAME points into the original discriminant name, which
9212 already has the correct lifetime. */
9213 TYPE_FIELD_NAME (union_type, 2) = name;
9214 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9215 disc->discriminants[2] = 0;
9216
9217 /* Smash this type to be a structure type. We have to do this
9218 because the type has already been recorded. */
9219 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9220 TYPE_NFIELDS (type) = 1;
9221 TYPE_FIELDS (type)
9222 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9223
9224 /* Install the variant part. */
9225 TYPE_FIELD_TYPE (type, 0) = union_type;
9226 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9227 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9228 }
9229 /* A union with a single anonymous field is probably an old-style
9230 univariant enum. */
9231 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9232 {
9233 /* Smash this type to be a structure type. We have to do this
9234 because the type has already been recorded. */
9235 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9236
9237 /* Make a union to hold the variants. */
9238 struct type *union_type = alloc_type (objfile);
9239 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9240 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9241 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9242 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9243 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9244
9245 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9246 const char *variant_name
9247 = rust_last_path_segment (TYPE_NAME (field_type));
9248 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9249 TYPE_NAME (field_type)
9250 = rust_fully_qualify (&objfile->objfile_obstack,
9251 TYPE_NAME (type), variant_name);
9252
9253 /* Install the union in the outer struct type. */
9254 TYPE_NFIELDS (type) = 1;
9255 TYPE_FIELDS (type)
9256 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9257 TYPE_FIELD_TYPE (type, 0) = union_type;
9258 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9259 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9260
9261 alloc_discriminant_info (union_type, -1, 0);
9262 }
9263 else
9264 {
9265 struct type *disr_type = nullptr;
9266 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9267 {
9268 disr_type = TYPE_FIELD_TYPE (type, i);
9269
9270 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9271 {
9272 /* All fields of a true enum will be structs. */
9273 return;
9274 }
9275 else if (TYPE_NFIELDS (disr_type) == 0)
9276 {
9277 /* Could be data-less variant, so keep going. */
9278 disr_type = nullptr;
9279 }
9280 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9281 "RUST$ENUM$DISR") != 0)
9282 {
9283 /* Not a Rust enum. */
9284 return;
9285 }
9286 else
9287 {
9288 /* Found one. */
9289 break;
9290 }
9291 }
9292
9293 /* If we got here without a discriminant, then it's probably
9294 just a union. */
9295 if (disr_type == nullptr)
9296 return;
9297
9298 /* Smash this type to be a structure type. We have to do this
9299 because the type has already been recorded. */
9300 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9301
9302 /* Make a union to hold the variants. */
9303 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9304 struct type *union_type = alloc_type (objfile);
9305 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9306 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9307 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9308 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9309 TYPE_FIELDS (union_type)
9310 = (struct field *) TYPE_ZALLOC (union_type,
9311 (TYPE_NFIELDS (union_type)
9312 * sizeof (struct field)));
9313
9314 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9315 TYPE_NFIELDS (type) * sizeof (struct field));
9316
9317 /* Install the discriminant at index 0 in the union. */
9318 TYPE_FIELD (union_type, 0) = *disr_field;
9319 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9320 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9321
9322 /* Install the union in the outer struct type. */
9323 TYPE_FIELD_TYPE (type, 0) = union_type;
9324 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9325 TYPE_NFIELDS (type) = 1;
9326
9327 /* Set the size and offset of the union type. */
9328 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9329
9330 /* We need a way to find the correct discriminant given a
9331 variant name. For convenience we build a map here. */
9332 struct type *enum_type = FIELD_TYPE (*disr_field);
9333 std::unordered_map<std::string, ULONGEST> discriminant_map;
9334 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9335 {
9336 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9337 {
9338 const char *name
9339 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9340 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9341 }
9342 }
9343
9344 int n_fields = TYPE_NFIELDS (union_type);
9345 struct discriminant_info *disc
9346 = alloc_discriminant_info (union_type, 0, -1);
9347 /* Skip the discriminant here. */
9348 for (int i = 1; i < n_fields; ++i)
9349 {
9350 /* Find the final word in the name of this variant's type.
9351 That name can be used to look up the correct
9352 discriminant. */
9353 const char *variant_name
9354 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9355 i)));
9356
9357 auto iter = discriminant_map.find (variant_name);
9358 if (iter != discriminant_map.end ())
9359 disc->discriminants[i] = iter->second;
9360
9361 /* Remove the discriminant field, if it exists. */
9362 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9363 if (TYPE_NFIELDS (sub_type) > 0)
9364 {
9365 --TYPE_NFIELDS (sub_type);
9366 ++TYPE_FIELDS (sub_type);
9367 }
9368 TYPE_FIELD_NAME (union_type, i) = variant_name;
9369 TYPE_NAME (sub_type)
9370 = rust_fully_qualify (&objfile->objfile_obstack,
9371 TYPE_NAME (type), variant_name);
9372 }
9373 }
9374 }
9375
9376 /* Rewrite some Rust unions to be structures with variants parts. */
9377
9378 static void
9379 rust_union_quirks (struct dwarf2_cu *cu)
9380 {
9381 gdb_assert (cu->language == language_rust);
9382 for (type *type_ : cu->rust_unions)
9383 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9384 /* We don't need this any more. */
9385 cu->rust_unions.clear ();
9386 }
9387
9388 /* Return the symtab for PER_CU. This works properly regardless of
9389 whether we're using the index or psymtabs. */
9390
9391 static struct compunit_symtab *
9392 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9393 {
9394 return (per_cu->dwarf2_per_objfile->using_index
9395 ? per_cu->v.quick->compunit_symtab
9396 : per_cu->v.psymtab->compunit_symtab);
9397 }
9398
9399 /* A helper function for computing the list of all symbol tables
9400 included by PER_CU. */
9401
9402 static void
9403 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9404 htab_t all_children, htab_t all_type_symtabs,
9405 struct dwarf2_per_cu_data *per_cu,
9406 struct compunit_symtab *immediate_parent)
9407 {
9408 void **slot;
9409 struct compunit_symtab *cust;
9410
9411 slot = htab_find_slot (all_children, per_cu, INSERT);
9412 if (*slot != NULL)
9413 {
9414 /* This inclusion and its children have been processed. */
9415 return;
9416 }
9417
9418 *slot = per_cu;
9419 /* Only add a CU if it has a symbol table. */
9420 cust = get_compunit_symtab (per_cu);
9421 if (cust != NULL)
9422 {
9423 /* If this is a type unit only add its symbol table if we haven't
9424 seen it yet (type unit per_cu's can share symtabs). */
9425 if (per_cu->is_debug_types)
9426 {
9427 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9428 if (*slot == NULL)
9429 {
9430 *slot = cust;
9431 result->push_back (cust);
9432 if (cust->user == NULL)
9433 cust->user = immediate_parent;
9434 }
9435 }
9436 else
9437 {
9438 result->push_back (cust);
9439 if (cust->user == NULL)
9440 cust->user = immediate_parent;
9441 }
9442 }
9443
9444 if (!per_cu->imported_symtabs_empty ())
9445 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9446 {
9447 recursively_compute_inclusions (result, all_children,
9448 all_type_symtabs, ptr, cust);
9449 }
9450 }
9451
9452 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9453 PER_CU. */
9454
9455 static void
9456 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9457 {
9458 gdb_assert (! per_cu->is_debug_types);
9459
9460 if (!per_cu->imported_symtabs_empty ())
9461 {
9462 int len;
9463 std::vector<compunit_symtab *> result_symtabs;
9464 htab_t all_children, all_type_symtabs;
9465 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9466
9467 /* If we don't have a symtab, we can just skip this case. */
9468 if (cust == NULL)
9469 return;
9470
9471 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9472 NULL, xcalloc, xfree);
9473 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9474 NULL, xcalloc, xfree);
9475
9476 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9477 {
9478 recursively_compute_inclusions (&result_symtabs, all_children,
9479 all_type_symtabs, ptr, cust);
9480 }
9481
9482 /* Now we have a transitive closure of all the included symtabs. */
9483 len = result_symtabs.size ();
9484 cust->includes
9485 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9486 struct compunit_symtab *, len + 1);
9487 memcpy (cust->includes, result_symtabs.data (),
9488 len * sizeof (compunit_symtab *));
9489 cust->includes[len] = NULL;
9490
9491 htab_delete (all_children);
9492 htab_delete (all_type_symtabs);
9493 }
9494 }
9495
9496 /* Compute the 'includes' field for the symtabs of all the CUs we just
9497 read. */
9498
9499 static void
9500 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9501 {
9502 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9503 {
9504 if (! iter->is_debug_types)
9505 compute_compunit_symtab_includes (iter);
9506 }
9507
9508 dwarf2_per_objfile->just_read_cus.clear ();
9509 }
9510
9511 /* Generate full symbol information for PER_CU, whose DIEs have
9512 already been loaded into memory. */
9513
9514 static void
9515 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9516 enum language pretend_language)
9517 {
9518 struct dwarf2_cu *cu = per_cu->cu;
9519 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9520 struct objfile *objfile = dwarf2_per_objfile->objfile;
9521 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9522 CORE_ADDR lowpc, highpc;
9523 struct compunit_symtab *cust;
9524 CORE_ADDR baseaddr;
9525 struct block *static_block;
9526 CORE_ADDR addr;
9527
9528 baseaddr = objfile->text_section_offset ();
9529
9530 /* Clear the list here in case something was left over. */
9531 cu->method_list.clear ();
9532
9533 cu->language = pretend_language;
9534 cu->language_defn = language_def (cu->language);
9535
9536 /* Do line number decoding in read_file_scope () */
9537 process_die (cu->dies, cu);
9538
9539 /* For now fudge the Go package. */
9540 if (cu->language == language_go)
9541 fixup_go_packaging (cu);
9542
9543 /* Now that we have processed all the DIEs in the CU, all the types
9544 should be complete, and it should now be safe to compute all of the
9545 physnames. */
9546 compute_delayed_physnames (cu);
9547
9548 if (cu->language == language_rust)
9549 rust_union_quirks (cu);
9550
9551 /* Some compilers don't define a DW_AT_high_pc attribute for the
9552 compilation unit. If the DW_AT_high_pc is missing, synthesize
9553 it, by scanning the DIE's below the compilation unit. */
9554 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9555
9556 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9557 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9558
9559 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9560 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9561 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9562 addrmap to help ensure it has an accurate map of pc values belonging to
9563 this comp unit. */
9564 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9565
9566 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9567 SECT_OFF_TEXT (objfile),
9568 0);
9569
9570 if (cust != NULL)
9571 {
9572 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9573
9574 /* Set symtab language to language from DW_AT_language. If the
9575 compilation is from a C file generated by language preprocessors, do
9576 not set the language if it was already deduced by start_subfile. */
9577 if (!(cu->language == language_c
9578 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9579 COMPUNIT_FILETABS (cust)->language = cu->language;
9580
9581 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9582 produce DW_AT_location with location lists but it can be possibly
9583 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9584 there were bugs in prologue debug info, fixed later in GCC-4.5
9585 by "unwind info for epilogues" patch (which is not directly related).
9586
9587 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9588 needed, it would be wrong due to missing DW_AT_producer there.
9589
9590 Still one can confuse GDB by using non-standard GCC compilation
9591 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9592 */
9593 if (cu->has_loclist && gcc_4_minor >= 5)
9594 cust->locations_valid = 1;
9595
9596 if (gcc_4_minor >= 5)
9597 cust->epilogue_unwind_valid = 1;
9598
9599 cust->call_site_htab = cu->call_site_htab;
9600 }
9601
9602 if (dwarf2_per_objfile->using_index)
9603 per_cu->v.quick->compunit_symtab = cust;
9604 else
9605 {
9606 dwarf2_psymtab *pst = per_cu->v.psymtab;
9607 pst->compunit_symtab = cust;
9608 pst->readin = true;
9609 }
9610
9611 /* Push it for inclusion processing later. */
9612 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9613
9614 /* Not needed any more. */
9615 cu->reset_builder ();
9616 }
9617
9618 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9619 already been loaded into memory. */
9620
9621 static void
9622 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9623 enum language pretend_language)
9624 {
9625 struct dwarf2_cu *cu = per_cu->cu;
9626 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9627 struct objfile *objfile = dwarf2_per_objfile->objfile;
9628 struct compunit_symtab *cust;
9629 struct signatured_type *sig_type;
9630
9631 gdb_assert (per_cu->is_debug_types);
9632 sig_type = (struct signatured_type *) per_cu;
9633
9634 /* Clear the list here in case something was left over. */
9635 cu->method_list.clear ();
9636
9637 cu->language = pretend_language;
9638 cu->language_defn = language_def (cu->language);
9639
9640 /* The symbol tables are set up in read_type_unit_scope. */
9641 process_die (cu->dies, cu);
9642
9643 /* For now fudge the Go package. */
9644 if (cu->language == language_go)
9645 fixup_go_packaging (cu);
9646
9647 /* Now that we have processed all the DIEs in the CU, all the types
9648 should be complete, and it should now be safe to compute all of the
9649 physnames. */
9650 compute_delayed_physnames (cu);
9651
9652 if (cu->language == language_rust)
9653 rust_union_quirks (cu);
9654
9655 /* TUs share symbol tables.
9656 If this is the first TU to use this symtab, complete the construction
9657 of it with end_expandable_symtab. Otherwise, complete the addition of
9658 this TU's symbols to the existing symtab. */
9659 if (sig_type->type_unit_group->compunit_symtab == NULL)
9660 {
9661 buildsym_compunit *builder = cu->get_builder ();
9662 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9663 sig_type->type_unit_group->compunit_symtab = cust;
9664
9665 if (cust != NULL)
9666 {
9667 /* Set symtab language to language from DW_AT_language. If the
9668 compilation is from a C file generated by language preprocessors,
9669 do not set the language if it was already deduced by
9670 start_subfile. */
9671 if (!(cu->language == language_c
9672 && COMPUNIT_FILETABS (cust)->language != language_c))
9673 COMPUNIT_FILETABS (cust)->language = cu->language;
9674 }
9675 }
9676 else
9677 {
9678 cu->get_builder ()->augment_type_symtab ();
9679 cust = sig_type->type_unit_group->compunit_symtab;
9680 }
9681
9682 if (dwarf2_per_objfile->using_index)
9683 per_cu->v.quick->compunit_symtab = cust;
9684 else
9685 {
9686 dwarf2_psymtab *pst = per_cu->v.psymtab;
9687 pst->compunit_symtab = cust;
9688 pst->readin = true;
9689 }
9690
9691 /* Not needed any more. */
9692 cu->reset_builder ();
9693 }
9694
9695 /* Process an imported unit DIE. */
9696
9697 static void
9698 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9699 {
9700 struct attribute *attr;
9701
9702 /* For now we don't handle imported units in type units. */
9703 if (cu->per_cu->is_debug_types)
9704 {
9705 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9706 " supported in type units [in module %s]"),
9707 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9708 }
9709
9710 attr = dwarf2_attr (die, DW_AT_import, cu);
9711 if (attr != NULL)
9712 {
9713 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9714 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9715 dwarf2_per_cu_data *per_cu
9716 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9717 cu->per_cu->dwarf2_per_objfile);
9718
9719 /* If necessary, add it to the queue and load its DIEs. */
9720 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9721 load_full_comp_unit (per_cu, false, cu->language);
9722
9723 cu->per_cu->imported_symtabs_push (per_cu);
9724 }
9725 }
9726
9727 /* RAII object that represents a process_die scope: i.e.,
9728 starts/finishes processing a DIE. */
9729 class process_die_scope
9730 {
9731 public:
9732 process_die_scope (die_info *die, dwarf2_cu *cu)
9733 : m_die (die), m_cu (cu)
9734 {
9735 /* We should only be processing DIEs not already in process. */
9736 gdb_assert (!m_die->in_process);
9737 m_die->in_process = true;
9738 }
9739
9740 ~process_die_scope ()
9741 {
9742 m_die->in_process = false;
9743
9744 /* If we're done processing the DIE for the CU that owns the line
9745 header, we don't need the line header anymore. */
9746 if (m_cu->line_header_die_owner == m_die)
9747 {
9748 delete m_cu->line_header;
9749 m_cu->line_header = NULL;
9750 m_cu->line_header_die_owner = NULL;
9751 }
9752 }
9753
9754 private:
9755 die_info *m_die;
9756 dwarf2_cu *m_cu;
9757 };
9758
9759 /* Process a die and its children. */
9760
9761 static void
9762 process_die (struct die_info *die, struct dwarf2_cu *cu)
9763 {
9764 process_die_scope scope (die, cu);
9765
9766 switch (die->tag)
9767 {
9768 case DW_TAG_padding:
9769 break;
9770 case DW_TAG_compile_unit:
9771 case DW_TAG_partial_unit:
9772 read_file_scope (die, cu);
9773 break;
9774 case DW_TAG_type_unit:
9775 read_type_unit_scope (die, cu);
9776 break;
9777 case DW_TAG_subprogram:
9778 /* Nested subprograms in Fortran get a prefix. */
9779 if (cu->language == language_fortran
9780 && die->parent != NULL
9781 && die->parent->tag == DW_TAG_subprogram)
9782 cu->processing_has_namespace_info = true;
9783 /* Fall through. */
9784 case DW_TAG_inlined_subroutine:
9785 read_func_scope (die, cu);
9786 break;
9787 case DW_TAG_lexical_block:
9788 case DW_TAG_try_block:
9789 case DW_TAG_catch_block:
9790 read_lexical_block_scope (die, cu);
9791 break;
9792 case DW_TAG_call_site:
9793 case DW_TAG_GNU_call_site:
9794 read_call_site_scope (die, cu);
9795 break;
9796 case DW_TAG_class_type:
9797 case DW_TAG_interface_type:
9798 case DW_TAG_structure_type:
9799 case DW_TAG_union_type:
9800 process_structure_scope (die, cu);
9801 break;
9802 case DW_TAG_enumeration_type:
9803 process_enumeration_scope (die, cu);
9804 break;
9805
9806 /* These dies have a type, but processing them does not create
9807 a symbol or recurse to process the children. Therefore we can
9808 read them on-demand through read_type_die. */
9809 case DW_TAG_subroutine_type:
9810 case DW_TAG_set_type:
9811 case DW_TAG_array_type:
9812 case DW_TAG_pointer_type:
9813 case DW_TAG_ptr_to_member_type:
9814 case DW_TAG_reference_type:
9815 case DW_TAG_rvalue_reference_type:
9816 case DW_TAG_string_type:
9817 break;
9818
9819 case DW_TAG_base_type:
9820 case DW_TAG_subrange_type:
9821 case DW_TAG_typedef:
9822 /* Add a typedef symbol for the type definition, if it has a
9823 DW_AT_name. */
9824 new_symbol (die, read_type_die (die, cu), cu);
9825 break;
9826 case DW_TAG_common_block:
9827 read_common_block (die, cu);
9828 break;
9829 case DW_TAG_common_inclusion:
9830 break;
9831 case DW_TAG_namespace:
9832 cu->processing_has_namespace_info = true;
9833 read_namespace (die, cu);
9834 break;
9835 case DW_TAG_module:
9836 cu->processing_has_namespace_info = true;
9837 read_module (die, cu);
9838 break;
9839 case DW_TAG_imported_declaration:
9840 cu->processing_has_namespace_info = true;
9841 if (read_namespace_alias (die, cu))
9842 break;
9843 /* The declaration is not a global namespace alias. */
9844 /* Fall through. */
9845 case DW_TAG_imported_module:
9846 cu->processing_has_namespace_info = true;
9847 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9848 || cu->language != language_fortran))
9849 complaint (_("Tag '%s' has unexpected children"),
9850 dwarf_tag_name (die->tag));
9851 read_import_statement (die, cu);
9852 break;
9853
9854 case DW_TAG_imported_unit:
9855 process_imported_unit_die (die, cu);
9856 break;
9857
9858 case DW_TAG_variable:
9859 read_variable (die, cu);
9860 break;
9861
9862 default:
9863 new_symbol (die, NULL, cu);
9864 break;
9865 }
9866 }
9867 \f
9868 /* DWARF name computation. */
9869
9870 /* A helper function for dwarf2_compute_name which determines whether DIE
9871 needs to have the name of the scope prepended to the name listed in the
9872 die. */
9873
9874 static int
9875 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9876 {
9877 struct attribute *attr;
9878
9879 switch (die->tag)
9880 {
9881 case DW_TAG_namespace:
9882 case DW_TAG_typedef:
9883 case DW_TAG_class_type:
9884 case DW_TAG_interface_type:
9885 case DW_TAG_structure_type:
9886 case DW_TAG_union_type:
9887 case DW_TAG_enumeration_type:
9888 case DW_TAG_enumerator:
9889 case DW_TAG_subprogram:
9890 case DW_TAG_inlined_subroutine:
9891 case DW_TAG_member:
9892 case DW_TAG_imported_declaration:
9893 return 1;
9894
9895 case DW_TAG_variable:
9896 case DW_TAG_constant:
9897 /* We only need to prefix "globally" visible variables. These include
9898 any variable marked with DW_AT_external or any variable that
9899 lives in a namespace. [Variables in anonymous namespaces
9900 require prefixing, but they are not DW_AT_external.] */
9901
9902 if (dwarf2_attr (die, DW_AT_specification, cu))
9903 {
9904 struct dwarf2_cu *spec_cu = cu;
9905
9906 return die_needs_namespace (die_specification (die, &spec_cu),
9907 spec_cu);
9908 }
9909
9910 attr = dwarf2_attr (die, DW_AT_external, cu);
9911 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9912 && die->parent->tag != DW_TAG_module)
9913 return 0;
9914 /* A variable in a lexical block of some kind does not need a
9915 namespace, even though in C++ such variables may be external
9916 and have a mangled name. */
9917 if (die->parent->tag == DW_TAG_lexical_block
9918 || die->parent->tag == DW_TAG_try_block
9919 || die->parent->tag == DW_TAG_catch_block
9920 || die->parent->tag == DW_TAG_subprogram)
9921 return 0;
9922 return 1;
9923
9924 default:
9925 return 0;
9926 }
9927 }
9928
9929 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9930 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9931 defined for the given DIE. */
9932
9933 static struct attribute *
9934 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9935 {
9936 struct attribute *attr;
9937
9938 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9939 if (attr == NULL)
9940 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9941
9942 return attr;
9943 }
9944
9945 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9946 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9947 defined for the given DIE. */
9948
9949 static const char *
9950 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9951 {
9952 const char *linkage_name;
9953
9954 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9955 if (linkage_name == NULL)
9956 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9957
9958 return linkage_name;
9959 }
9960
9961 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9962 compute the physname for the object, which include a method's:
9963 - formal parameters (C++),
9964 - receiver type (Go),
9965
9966 The term "physname" is a bit confusing.
9967 For C++, for example, it is the demangled name.
9968 For Go, for example, it's the mangled name.
9969
9970 For Ada, return the DIE's linkage name rather than the fully qualified
9971 name. PHYSNAME is ignored..
9972
9973 The result is allocated on the objfile_obstack and canonicalized. */
9974
9975 static const char *
9976 dwarf2_compute_name (const char *name,
9977 struct die_info *die, struct dwarf2_cu *cu,
9978 int physname)
9979 {
9980 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9981
9982 if (name == NULL)
9983 name = dwarf2_name (die, cu);
9984
9985 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9986 but otherwise compute it by typename_concat inside GDB.
9987 FIXME: Actually this is not really true, or at least not always true.
9988 It's all very confusing. compute_and_set_names doesn't try to demangle
9989 Fortran names because there is no mangling standard. So new_symbol
9990 will set the demangled name to the result of dwarf2_full_name, and it is
9991 the demangled name that GDB uses if it exists. */
9992 if (cu->language == language_ada
9993 || (cu->language == language_fortran && physname))
9994 {
9995 /* For Ada unit, we prefer the linkage name over the name, as
9996 the former contains the exported name, which the user expects
9997 to be able to reference. Ideally, we want the user to be able
9998 to reference this entity using either natural or linkage name,
9999 but we haven't started looking at this enhancement yet. */
10000 const char *linkage_name = dw2_linkage_name (die, cu);
10001
10002 if (linkage_name != NULL)
10003 return linkage_name;
10004 }
10005
10006 /* These are the only languages we know how to qualify names in. */
10007 if (name != NULL
10008 && (cu->language == language_cplus
10009 || cu->language == language_fortran || cu->language == language_d
10010 || cu->language == language_rust))
10011 {
10012 if (die_needs_namespace (die, cu))
10013 {
10014 const char *prefix;
10015 const char *canonical_name = NULL;
10016
10017 string_file buf;
10018
10019 prefix = determine_prefix (die, cu);
10020 if (*prefix != '\0')
10021 {
10022 gdb::unique_xmalloc_ptr<char> prefixed_name
10023 (typename_concat (NULL, prefix, name, physname, cu));
10024
10025 buf.puts (prefixed_name.get ());
10026 }
10027 else
10028 buf.puts (name);
10029
10030 /* Template parameters may be specified in the DIE's DW_AT_name, or
10031 as children with DW_TAG_template_type_param or
10032 DW_TAG_value_type_param. If the latter, add them to the name
10033 here. If the name already has template parameters, then
10034 skip this step; some versions of GCC emit both, and
10035 it is more efficient to use the pre-computed name.
10036
10037 Something to keep in mind about this process: it is very
10038 unlikely, or in some cases downright impossible, to produce
10039 something that will match the mangled name of a function.
10040 If the definition of the function has the same debug info,
10041 we should be able to match up with it anyway. But fallbacks
10042 using the minimal symbol, for instance to find a method
10043 implemented in a stripped copy of libstdc++, will not work.
10044 If we do not have debug info for the definition, we will have to
10045 match them up some other way.
10046
10047 When we do name matching there is a related problem with function
10048 templates; two instantiated function templates are allowed to
10049 differ only by their return types, which we do not add here. */
10050
10051 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10052 {
10053 struct attribute *attr;
10054 struct die_info *child;
10055 int first = 1;
10056
10057 die->building_fullname = 1;
10058
10059 for (child = die->child; child != NULL; child = child->sibling)
10060 {
10061 struct type *type;
10062 LONGEST value;
10063 const gdb_byte *bytes;
10064 struct dwarf2_locexpr_baton *baton;
10065 struct value *v;
10066
10067 if (child->tag != DW_TAG_template_type_param
10068 && child->tag != DW_TAG_template_value_param)
10069 continue;
10070
10071 if (first)
10072 {
10073 buf.puts ("<");
10074 first = 0;
10075 }
10076 else
10077 buf.puts (", ");
10078
10079 attr = dwarf2_attr (child, DW_AT_type, cu);
10080 if (attr == NULL)
10081 {
10082 complaint (_("template parameter missing DW_AT_type"));
10083 buf.puts ("UNKNOWN_TYPE");
10084 continue;
10085 }
10086 type = die_type (child, cu);
10087
10088 if (child->tag == DW_TAG_template_type_param)
10089 {
10090 c_print_type (type, "", &buf, -1, 0, cu->language,
10091 &type_print_raw_options);
10092 continue;
10093 }
10094
10095 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10096 if (attr == NULL)
10097 {
10098 complaint (_("template parameter missing "
10099 "DW_AT_const_value"));
10100 buf.puts ("UNKNOWN_VALUE");
10101 continue;
10102 }
10103
10104 dwarf2_const_value_attr (attr, type, name,
10105 &cu->comp_unit_obstack, cu,
10106 &value, &bytes, &baton);
10107
10108 if (TYPE_NOSIGN (type))
10109 /* GDB prints characters as NUMBER 'CHAR'. If that's
10110 changed, this can use value_print instead. */
10111 c_printchar (value, type, &buf);
10112 else
10113 {
10114 struct value_print_options opts;
10115
10116 if (baton != NULL)
10117 v = dwarf2_evaluate_loc_desc (type, NULL,
10118 baton->data,
10119 baton->size,
10120 baton->per_cu);
10121 else if (bytes != NULL)
10122 {
10123 v = allocate_value (type);
10124 memcpy (value_contents_writeable (v), bytes,
10125 TYPE_LENGTH (type));
10126 }
10127 else
10128 v = value_from_longest (type, value);
10129
10130 /* Specify decimal so that we do not depend on
10131 the radix. */
10132 get_formatted_print_options (&opts, 'd');
10133 opts.raw = 1;
10134 value_print (v, &buf, &opts);
10135 release_value (v);
10136 }
10137 }
10138
10139 die->building_fullname = 0;
10140
10141 if (!first)
10142 {
10143 /* Close the argument list, with a space if necessary
10144 (nested templates). */
10145 if (!buf.empty () && buf.string ().back () == '>')
10146 buf.puts (" >");
10147 else
10148 buf.puts (">");
10149 }
10150 }
10151
10152 /* For C++ methods, append formal parameter type
10153 information, if PHYSNAME. */
10154
10155 if (physname && die->tag == DW_TAG_subprogram
10156 && cu->language == language_cplus)
10157 {
10158 struct type *type = read_type_die (die, cu);
10159
10160 c_type_print_args (type, &buf, 1, cu->language,
10161 &type_print_raw_options);
10162
10163 if (cu->language == language_cplus)
10164 {
10165 /* Assume that an artificial first parameter is
10166 "this", but do not crash if it is not. RealView
10167 marks unnamed (and thus unused) parameters as
10168 artificial; there is no way to differentiate
10169 the two cases. */
10170 if (TYPE_NFIELDS (type) > 0
10171 && TYPE_FIELD_ARTIFICIAL (type, 0)
10172 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10173 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10174 0))))
10175 buf.puts (" const");
10176 }
10177 }
10178
10179 const std::string &intermediate_name = buf.string ();
10180
10181 if (cu->language == language_cplus)
10182 canonical_name
10183 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10184 &objfile->per_bfd->storage_obstack);
10185
10186 /* If we only computed INTERMEDIATE_NAME, or if
10187 INTERMEDIATE_NAME is already canonical, then we need to
10188 copy it to the appropriate obstack. */
10189 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10190 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10191 intermediate_name);
10192 else
10193 name = canonical_name;
10194 }
10195 }
10196
10197 return name;
10198 }
10199
10200 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10201 If scope qualifiers are appropriate they will be added. The result
10202 will be allocated on the storage_obstack, or NULL if the DIE does
10203 not have a name. NAME may either be from a previous call to
10204 dwarf2_name or NULL.
10205
10206 The output string will be canonicalized (if C++). */
10207
10208 static const char *
10209 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10210 {
10211 return dwarf2_compute_name (name, die, cu, 0);
10212 }
10213
10214 /* Construct a physname for the given DIE in CU. NAME may either be
10215 from a previous call to dwarf2_name or NULL. The result will be
10216 allocated on the objfile_objstack or NULL if the DIE does not have a
10217 name.
10218
10219 The output string will be canonicalized (if C++). */
10220
10221 static const char *
10222 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10223 {
10224 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10225 const char *retval, *mangled = NULL, *canon = NULL;
10226 int need_copy = 1;
10227
10228 /* In this case dwarf2_compute_name is just a shortcut not building anything
10229 on its own. */
10230 if (!die_needs_namespace (die, cu))
10231 return dwarf2_compute_name (name, die, cu, 1);
10232
10233 mangled = dw2_linkage_name (die, cu);
10234
10235 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10236 See https://github.com/rust-lang/rust/issues/32925. */
10237 if (cu->language == language_rust && mangled != NULL
10238 && strchr (mangled, '{') != NULL)
10239 mangled = NULL;
10240
10241 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10242 has computed. */
10243 gdb::unique_xmalloc_ptr<char> demangled;
10244 if (mangled != NULL)
10245 {
10246
10247 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10248 {
10249 /* Do nothing (do not demangle the symbol name). */
10250 }
10251 else if (cu->language == language_go)
10252 {
10253 /* This is a lie, but we already lie to the caller new_symbol.
10254 new_symbol assumes we return the mangled name.
10255 This just undoes that lie until things are cleaned up. */
10256 }
10257 else
10258 {
10259 /* Use DMGL_RET_DROP for C++ template functions to suppress
10260 their return type. It is easier for GDB users to search
10261 for such functions as `name(params)' than `long name(params)'.
10262 In such case the minimal symbol names do not match the full
10263 symbol names but for template functions there is never a need
10264 to look up their definition from their declaration so
10265 the only disadvantage remains the minimal symbol variant
10266 `long name(params)' does not have the proper inferior type. */
10267 demangled.reset (gdb_demangle (mangled,
10268 (DMGL_PARAMS | DMGL_ANSI
10269 | DMGL_RET_DROP)));
10270 }
10271 if (demangled)
10272 canon = demangled.get ();
10273 else
10274 {
10275 canon = mangled;
10276 need_copy = 0;
10277 }
10278 }
10279
10280 if (canon == NULL || check_physname)
10281 {
10282 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10283
10284 if (canon != NULL && strcmp (physname, canon) != 0)
10285 {
10286 /* It may not mean a bug in GDB. The compiler could also
10287 compute DW_AT_linkage_name incorrectly. But in such case
10288 GDB would need to be bug-to-bug compatible. */
10289
10290 complaint (_("Computed physname <%s> does not match demangled <%s> "
10291 "(from linkage <%s>) - DIE at %s [in module %s]"),
10292 physname, canon, mangled, sect_offset_str (die->sect_off),
10293 objfile_name (objfile));
10294
10295 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10296 is available here - over computed PHYSNAME. It is safer
10297 against both buggy GDB and buggy compilers. */
10298
10299 retval = canon;
10300 }
10301 else
10302 {
10303 retval = physname;
10304 need_copy = 0;
10305 }
10306 }
10307 else
10308 retval = canon;
10309
10310 if (need_copy)
10311 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
10312
10313 return retval;
10314 }
10315
10316 /* Inspect DIE in CU for a namespace alias. If one exists, record
10317 a new symbol for it.
10318
10319 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10320
10321 static int
10322 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10323 {
10324 struct attribute *attr;
10325
10326 /* If the die does not have a name, this is not a namespace
10327 alias. */
10328 attr = dwarf2_attr (die, DW_AT_name, cu);
10329 if (attr != NULL)
10330 {
10331 int num;
10332 struct die_info *d = die;
10333 struct dwarf2_cu *imported_cu = cu;
10334
10335 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10336 keep inspecting DIEs until we hit the underlying import. */
10337 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10338 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10339 {
10340 attr = dwarf2_attr (d, DW_AT_import, cu);
10341 if (attr == NULL)
10342 break;
10343
10344 d = follow_die_ref (d, attr, &imported_cu);
10345 if (d->tag != DW_TAG_imported_declaration)
10346 break;
10347 }
10348
10349 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10350 {
10351 complaint (_("DIE at %s has too many recursively imported "
10352 "declarations"), sect_offset_str (d->sect_off));
10353 return 0;
10354 }
10355
10356 if (attr != NULL)
10357 {
10358 struct type *type;
10359 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10360
10361 type = get_die_type_at_offset (sect_off, cu->per_cu);
10362 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10363 {
10364 /* This declaration is a global namespace alias. Add
10365 a symbol for it whose type is the aliased namespace. */
10366 new_symbol (die, type, cu);
10367 return 1;
10368 }
10369 }
10370 }
10371
10372 return 0;
10373 }
10374
10375 /* Return the using directives repository (global or local?) to use in the
10376 current context for CU.
10377
10378 For Ada, imported declarations can materialize renamings, which *may* be
10379 global. However it is impossible (for now?) in DWARF to distinguish
10380 "external" imported declarations and "static" ones. As all imported
10381 declarations seem to be static in all other languages, make them all CU-wide
10382 global only in Ada. */
10383
10384 static struct using_direct **
10385 using_directives (struct dwarf2_cu *cu)
10386 {
10387 if (cu->language == language_ada
10388 && cu->get_builder ()->outermost_context_p ())
10389 return cu->get_builder ()->get_global_using_directives ();
10390 else
10391 return cu->get_builder ()->get_local_using_directives ();
10392 }
10393
10394 /* Read the import statement specified by the given die and record it. */
10395
10396 static void
10397 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10398 {
10399 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10400 struct attribute *import_attr;
10401 struct die_info *imported_die, *child_die;
10402 struct dwarf2_cu *imported_cu;
10403 const char *imported_name;
10404 const char *imported_name_prefix;
10405 const char *canonical_name;
10406 const char *import_alias;
10407 const char *imported_declaration = NULL;
10408 const char *import_prefix;
10409 std::vector<const char *> excludes;
10410
10411 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10412 if (import_attr == NULL)
10413 {
10414 complaint (_("Tag '%s' has no DW_AT_import"),
10415 dwarf_tag_name (die->tag));
10416 return;
10417 }
10418
10419 imported_cu = cu;
10420 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10421 imported_name = dwarf2_name (imported_die, imported_cu);
10422 if (imported_name == NULL)
10423 {
10424 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10425
10426 The import in the following code:
10427 namespace A
10428 {
10429 typedef int B;
10430 }
10431
10432 int main ()
10433 {
10434 using A::B;
10435 B b;
10436 return b;
10437 }
10438
10439 ...
10440 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10441 <52> DW_AT_decl_file : 1
10442 <53> DW_AT_decl_line : 6
10443 <54> DW_AT_import : <0x75>
10444 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10445 <59> DW_AT_name : B
10446 <5b> DW_AT_decl_file : 1
10447 <5c> DW_AT_decl_line : 2
10448 <5d> DW_AT_type : <0x6e>
10449 ...
10450 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10451 <76> DW_AT_byte_size : 4
10452 <77> DW_AT_encoding : 5 (signed)
10453
10454 imports the wrong die ( 0x75 instead of 0x58 ).
10455 This case will be ignored until the gcc bug is fixed. */
10456 return;
10457 }
10458
10459 /* Figure out the local name after import. */
10460 import_alias = dwarf2_name (die, cu);
10461
10462 /* Figure out where the statement is being imported to. */
10463 import_prefix = determine_prefix (die, cu);
10464
10465 /* Figure out what the scope of the imported die is and prepend it
10466 to the name of the imported die. */
10467 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10468
10469 if (imported_die->tag != DW_TAG_namespace
10470 && imported_die->tag != DW_TAG_module)
10471 {
10472 imported_declaration = imported_name;
10473 canonical_name = imported_name_prefix;
10474 }
10475 else if (strlen (imported_name_prefix) > 0)
10476 canonical_name = obconcat (&objfile->objfile_obstack,
10477 imported_name_prefix,
10478 (cu->language == language_d ? "." : "::"),
10479 imported_name, (char *) NULL);
10480 else
10481 canonical_name = imported_name;
10482
10483 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10484 for (child_die = die->child; child_die && child_die->tag;
10485 child_die = sibling_die (child_die))
10486 {
10487 /* DWARF-4: A Fortran use statement with a “rename list” may be
10488 represented by an imported module entry with an import attribute
10489 referring to the module and owned entries corresponding to those
10490 entities that are renamed as part of being imported. */
10491
10492 if (child_die->tag != DW_TAG_imported_declaration)
10493 {
10494 complaint (_("child DW_TAG_imported_declaration expected "
10495 "- DIE at %s [in module %s]"),
10496 sect_offset_str (child_die->sect_off),
10497 objfile_name (objfile));
10498 continue;
10499 }
10500
10501 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10502 if (import_attr == NULL)
10503 {
10504 complaint (_("Tag '%s' has no DW_AT_import"),
10505 dwarf_tag_name (child_die->tag));
10506 continue;
10507 }
10508
10509 imported_cu = cu;
10510 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10511 &imported_cu);
10512 imported_name = dwarf2_name (imported_die, imported_cu);
10513 if (imported_name == NULL)
10514 {
10515 complaint (_("child DW_TAG_imported_declaration has unknown "
10516 "imported name - DIE at %s [in module %s]"),
10517 sect_offset_str (child_die->sect_off),
10518 objfile_name (objfile));
10519 continue;
10520 }
10521
10522 excludes.push_back (imported_name);
10523
10524 process_die (child_die, cu);
10525 }
10526
10527 add_using_directive (using_directives (cu),
10528 import_prefix,
10529 canonical_name,
10530 import_alias,
10531 imported_declaration,
10532 excludes,
10533 0,
10534 &objfile->objfile_obstack);
10535 }
10536
10537 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10538 types, but gives them a size of zero. Starting with version 14,
10539 ICC is compatible with GCC. */
10540
10541 static bool
10542 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10543 {
10544 if (!cu->checked_producer)
10545 check_producer (cu);
10546
10547 return cu->producer_is_icc_lt_14;
10548 }
10549
10550 /* ICC generates a DW_AT_type for C void functions. This was observed on
10551 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10552 which says that void functions should not have a DW_AT_type. */
10553
10554 static bool
10555 producer_is_icc (struct dwarf2_cu *cu)
10556 {
10557 if (!cu->checked_producer)
10558 check_producer (cu);
10559
10560 return cu->producer_is_icc;
10561 }
10562
10563 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10564 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10565 this, it was first present in GCC release 4.3.0. */
10566
10567 static bool
10568 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10569 {
10570 if (!cu->checked_producer)
10571 check_producer (cu);
10572
10573 return cu->producer_is_gcc_lt_4_3;
10574 }
10575
10576 static file_and_directory
10577 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10578 {
10579 file_and_directory res;
10580
10581 /* Find the filename. Do not use dwarf2_name here, since the filename
10582 is not a source language identifier. */
10583 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10584 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10585
10586 if (res.comp_dir == NULL
10587 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10588 && IS_ABSOLUTE_PATH (res.name))
10589 {
10590 res.comp_dir_storage = ldirname (res.name);
10591 if (!res.comp_dir_storage.empty ())
10592 res.comp_dir = res.comp_dir_storage.c_str ();
10593 }
10594 if (res.comp_dir != NULL)
10595 {
10596 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10597 directory, get rid of it. */
10598 const char *cp = strchr (res.comp_dir, ':');
10599
10600 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10601 res.comp_dir = cp + 1;
10602 }
10603
10604 if (res.name == NULL)
10605 res.name = "<unknown>";
10606
10607 return res;
10608 }
10609
10610 /* Handle DW_AT_stmt_list for a compilation unit.
10611 DIE is the DW_TAG_compile_unit die for CU.
10612 COMP_DIR is the compilation directory. LOWPC is passed to
10613 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10614
10615 static void
10616 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10617 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10618 {
10619 struct dwarf2_per_objfile *dwarf2_per_objfile
10620 = cu->per_cu->dwarf2_per_objfile;
10621 struct attribute *attr;
10622 struct line_header line_header_local;
10623 hashval_t line_header_local_hash;
10624 void **slot;
10625 int decode_mapping;
10626
10627 gdb_assert (! cu->per_cu->is_debug_types);
10628
10629 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10630 if (attr == NULL)
10631 return;
10632
10633 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10634
10635 /* The line header hash table is only created if needed (it exists to
10636 prevent redundant reading of the line table for partial_units).
10637 If we're given a partial_unit, we'll need it. If we're given a
10638 compile_unit, then use the line header hash table if it's already
10639 created, but don't create one just yet. */
10640
10641 if (dwarf2_per_objfile->line_header_hash == NULL
10642 && die->tag == DW_TAG_partial_unit)
10643 {
10644 dwarf2_per_objfile->line_header_hash
10645 .reset (htab_create_alloc (127, line_header_hash_voidp,
10646 line_header_eq_voidp,
10647 free_line_header_voidp,
10648 xcalloc, xfree));
10649 }
10650
10651 line_header_local.sect_off = line_offset;
10652 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10653 line_header_local_hash = line_header_hash (&line_header_local);
10654 if (dwarf2_per_objfile->line_header_hash != NULL)
10655 {
10656 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10657 &line_header_local,
10658 line_header_local_hash, NO_INSERT);
10659
10660 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10661 is not present in *SLOT (since if there is something in *SLOT then
10662 it will be for a partial_unit). */
10663 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10664 {
10665 gdb_assert (*slot != NULL);
10666 cu->line_header = (struct line_header *) *slot;
10667 return;
10668 }
10669 }
10670
10671 /* dwarf_decode_line_header does not yet provide sufficient information.
10672 We always have to call also dwarf_decode_lines for it. */
10673 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10674 if (lh == NULL)
10675 return;
10676
10677 cu->line_header = lh.release ();
10678 cu->line_header_die_owner = die;
10679
10680 if (dwarf2_per_objfile->line_header_hash == NULL)
10681 slot = NULL;
10682 else
10683 {
10684 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10685 &line_header_local,
10686 line_header_local_hash, INSERT);
10687 gdb_assert (slot != NULL);
10688 }
10689 if (slot != NULL && *slot == NULL)
10690 {
10691 /* This newly decoded line number information unit will be owned
10692 by line_header_hash hash table. */
10693 *slot = cu->line_header;
10694 cu->line_header_die_owner = NULL;
10695 }
10696 else
10697 {
10698 /* We cannot free any current entry in (*slot) as that struct line_header
10699 may be already used by multiple CUs. Create only temporary decoded
10700 line_header for this CU - it may happen at most once for each line
10701 number information unit. And if we're not using line_header_hash
10702 then this is what we want as well. */
10703 gdb_assert (die->tag != DW_TAG_partial_unit);
10704 }
10705 decode_mapping = (die->tag != DW_TAG_partial_unit);
10706 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10707 decode_mapping);
10708
10709 }
10710
10711 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10712
10713 static void
10714 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10715 {
10716 struct dwarf2_per_objfile *dwarf2_per_objfile
10717 = cu->per_cu->dwarf2_per_objfile;
10718 struct objfile *objfile = dwarf2_per_objfile->objfile;
10719 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10720 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10721 CORE_ADDR highpc = ((CORE_ADDR) 0);
10722 struct attribute *attr;
10723 struct die_info *child_die;
10724 CORE_ADDR baseaddr;
10725
10726 prepare_one_comp_unit (cu, die, cu->language);
10727 baseaddr = objfile->text_section_offset ();
10728
10729 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10730
10731 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10732 from finish_block. */
10733 if (lowpc == ((CORE_ADDR) -1))
10734 lowpc = highpc;
10735 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10736
10737 file_and_directory fnd = find_file_and_directory (die, cu);
10738
10739 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10740 standardised yet. As a workaround for the language detection we fall
10741 back to the DW_AT_producer string. */
10742 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10743 cu->language = language_opencl;
10744
10745 /* Similar hack for Go. */
10746 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10747 set_cu_language (DW_LANG_Go, cu);
10748
10749 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10750
10751 /* Decode line number information if present. We do this before
10752 processing child DIEs, so that the line header table is available
10753 for DW_AT_decl_file. */
10754 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10755
10756 /* Process all dies in compilation unit. */
10757 if (die->child != NULL)
10758 {
10759 child_die = die->child;
10760 while (child_die && child_die->tag)
10761 {
10762 process_die (child_die, cu);
10763 child_die = sibling_die (child_die);
10764 }
10765 }
10766
10767 /* Decode macro information, if present. Dwarf 2 macro information
10768 refers to information in the line number info statement program
10769 header, so we can only read it if we've read the header
10770 successfully. */
10771 attr = dwarf2_attr (die, DW_AT_macros, cu);
10772 if (attr == NULL)
10773 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10774 if (attr && cu->line_header)
10775 {
10776 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10777 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10778
10779 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10780 }
10781 else
10782 {
10783 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10784 if (attr && cu->line_header)
10785 {
10786 unsigned int macro_offset = DW_UNSND (attr);
10787
10788 dwarf_decode_macros (cu, macro_offset, 0);
10789 }
10790 }
10791 }
10792
10793 void
10794 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10795 {
10796 struct type_unit_group *tu_group;
10797 int first_time;
10798 struct attribute *attr;
10799 unsigned int i;
10800 struct signatured_type *sig_type;
10801
10802 gdb_assert (per_cu->is_debug_types);
10803 sig_type = (struct signatured_type *) per_cu;
10804
10805 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10806
10807 /* If we're using .gdb_index (includes -readnow) then
10808 per_cu->type_unit_group may not have been set up yet. */
10809 if (sig_type->type_unit_group == NULL)
10810 sig_type->type_unit_group = get_type_unit_group (this, attr);
10811 tu_group = sig_type->type_unit_group;
10812
10813 /* If we've already processed this stmt_list there's no real need to
10814 do it again, we could fake it and just recreate the part we need
10815 (file name,index -> symtab mapping). If data shows this optimization
10816 is useful we can do it then. */
10817 first_time = tu_group->compunit_symtab == NULL;
10818
10819 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10820 debug info. */
10821 line_header_up lh;
10822 if (attr != NULL)
10823 {
10824 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10825 lh = dwarf_decode_line_header (line_offset, this);
10826 }
10827 if (lh == NULL)
10828 {
10829 if (first_time)
10830 start_symtab ("", NULL, 0);
10831 else
10832 {
10833 gdb_assert (tu_group->symtabs == NULL);
10834 gdb_assert (m_builder == nullptr);
10835 struct compunit_symtab *cust = tu_group->compunit_symtab;
10836 m_builder.reset (new struct buildsym_compunit
10837 (COMPUNIT_OBJFILE (cust), "",
10838 COMPUNIT_DIRNAME (cust),
10839 compunit_language (cust),
10840 0, cust));
10841 }
10842 return;
10843 }
10844
10845 line_header = lh.release ();
10846 line_header_die_owner = die;
10847
10848 if (first_time)
10849 {
10850 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10851
10852 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10853 still initializing it, and our caller (a few levels up)
10854 process_full_type_unit still needs to know if this is the first
10855 time. */
10856
10857 tu_group->num_symtabs = line_header->file_names_size ();
10858 tu_group->symtabs = XNEWVEC (struct symtab *,
10859 line_header->file_names_size ());
10860
10861 auto &file_names = line_header->file_names ();
10862 for (i = 0; i < file_names.size (); ++i)
10863 {
10864 file_entry &fe = file_names[i];
10865 dwarf2_start_subfile (this, fe.name,
10866 fe.include_dir (line_header));
10867 buildsym_compunit *b = get_builder ();
10868 if (b->get_current_subfile ()->symtab == NULL)
10869 {
10870 /* NOTE: start_subfile will recognize when it's been
10871 passed a file it has already seen. So we can't
10872 assume there's a simple mapping from
10873 cu->line_header->file_names to subfiles, plus
10874 cu->line_header->file_names may contain dups. */
10875 b->get_current_subfile ()->symtab
10876 = allocate_symtab (cust, b->get_current_subfile ()->name);
10877 }
10878
10879 fe.symtab = b->get_current_subfile ()->symtab;
10880 tu_group->symtabs[i] = fe.symtab;
10881 }
10882 }
10883 else
10884 {
10885 gdb_assert (m_builder == nullptr);
10886 struct compunit_symtab *cust = tu_group->compunit_symtab;
10887 m_builder.reset (new struct buildsym_compunit
10888 (COMPUNIT_OBJFILE (cust), "",
10889 COMPUNIT_DIRNAME (cust),
10890 compunit_language (cust),
10891 0, cust));
10892
10893 auto &file_names = line_header->file_names ();
10894 for (i = 0; i < file_names.size (); ++i)
10895 {
10896 file_entry &fe = file_names[i];
10897 fe.symtab = tu_group->symtabs[i];
10898 }
10899 }
10900
10901 /* The main symtab is allocated last. Type units don't have DW_AT_name
10902 so they don't have a "real" (so to speak) symtab anyway.
10903 There is later code that will assign the main symtab to all symbols
10904 that don't have one. We need to handle the case of a symbol with a
10905 missing symtab (DW_AT_decl_file) anyway. */
10906 }
10907
10908 /* Process DW_TAG_type_unit.
10909 For TUs we want to skip the first top level sibling if it's not the
10910 actual type being defined by this TU. In this case the first top
10911 level sibling is there to provide context only. */
10912
10913 static void
10914 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10915 {
10916 struct die_info *child_die;
10917
10918 prepare_one_comp_unit (cu, die, language_minimal);
10919
10920 /* Initialize (or reinitialize) the machinery for building symtabs.
10921 We do this before processing child DIEs, so that the line header table
10922 is available for DW_AT_decl_file. */
10923 cu->setup_type_unit_groups (die);
10924
10925 if (die->child != NULL)
10926 {
10927 child_die = die->child;
10928 while (child_die && child_die->tag)
10929 {
10930 process_die (child_die, cu);
10931 child_die = sibling_die (child_die);
10932 }
10933 }
10934 }
10935 \f
10936 /* DWO/DWP files.
10937
10938 http://gcc.gnu.org/wiki/DebugFission
10939 http://gcc.gnu.org/wiki/DebugFissionDWP
10940
10941 To simplify handling of both DWO files ("object" files with the DWARF info)
10942 and DWP files (a file with the DWOs packaged up into one file), we treat
10943 DWP files as having a collection of virtual DWO files. */
10944
10945 static hashval_t
10946 hash_dwo_file (const void *item)
10947 {
10948 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10949 hashval_t hash;
10950
10951 hash = htab_hash_string (dwo_file->dwo_name);
10952 if (dwo_file->comp_dir != NULL)
10953 hash += htab_hash_string (dwo_file->comp_dir);
10954 return hash;
10955 }
10956
10957 static int
10958 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10959 {
10960 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10961 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10962
10963 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10964 return 0;
10965 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10966 return lhs->comp_dir == rhs->comp_dir;
10967 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10968 }
10969
10970 /* Allocate a hash table for DWO files. */
10971
10972 static htab_up
10973 allocate_dwo_file_hash_table ()
10974 {
10975 auto delete_dwo_file = [] (void *item)
10976 {
10977 struct dwo_file *dwo_file = (struct dwo_file *) item;
10978
10979 delete dwo_file;
10980 };
10981
10982 return htab_up (htab_create_alloc (41,
10983 hash_dwo_file,
10984 eq_dwo_file,
10985 delete_dwo_file,
10986 xcalloc, xfree));
10987 }
10988
10989 /* Lookup DWO file DWO_NAME. */
10990
10991 static void **
10992 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10993 const char *dwo_name,
10994 const char *comp_dir)
10995 {
10996 struct dwo_file find_entry;
10997 void **slot;
10998
10999 if (dwarf2_per_objfile->dwo_files == NULL)
11000 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
11001
11002 find_entry.dwo_name = dwo_name;
11003 find_entry.comp_dir = comp_dir;
11004 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11005 INSERT);
11006
11007 return slot;
11008 }
11009
11010 static hashval_t
11011 hash_dwo_unit (const void *item)
11012 {
11013 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11014
11015 /* This drops the top 32 bits of the id, but is ok for a hash. */
11016 return dwo_unit->signature;
11017 }
11018
11019 static int
11020 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11021 {
11022 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11023 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11024
11025 /* The signature is assumed to be unique within the DWO file.
11026 So while object file CU dwo_id's always have the value zero,
11027 that's OK, assuming each object file DWO file has only one CU,
11028 and that's the rule for now. */
11029 return lhs->signature == rhs->signature;
11030 }
11031
11032 /* Allocate a hash table for DWO CUs,TUs.
11033 There is one of these tables for each of CUs,TUs for each DWO file. */
11034
11035 static htab_up
11036 allocate_dwo_unit_table ()
11037 {
11038 /* Start out with a pretty small number.
11039 Generally DWO files contain only one CU and maybe some TUs. */
11040 return htab_up (htab_create_alloc (3,
11041 hash_dwo_unit,
11042 eq_dwo_unit,
11043 NULL, xcalloc, xfree));
11044 }
11045
11046 /* die_reader_func for create_dwo_cu. */
11047
11048 static void
11049 create_dwo_cu_reader (const struct die_reader_specs *reader,
11050 const gdb_byte *info_ptr,
11051 struct die_info *comp_unit_die,
11052 struct dwo_file *dwo_file,
11053 struct dwo_unit *dwo_unit)
11054 {
11055 struct dwarf2_cu *cu = reader->cu;
11056 sect_offset sect_off = cu->per_cu->sect_off;
11057 struct dwarf2_section_info *section = cu->per_cu->section;
11058
11059 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11060 if (!signature.has_value ())
11061 {
11062 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11063 " its dwo_id [in module %s]"),
11064 sect_offset_str (sect_off), dwo_file->dwo_name);
11065 return;
11066 }
11067
11068 dwo_unit->dwo_file = dwo_file;
11069 dwo_unit->signature = *signature;
11070 dwo_unit->section = section;
11071 dwo_unit->sect_off = sect_off;
11072 dwo_unit->length = cu->per_cu->length;
11073
11074 if (dwarf_read_debug)
11075 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11076 sect_offset_str (sect_off),
11077 hex_string (dwo_unit->signature));
11078 }
11079
11080 /* Create the dwo_units for the CUs in a DWO_FILE.
11081 Note: This function processes DWO files only, not DWP files. */
11082
11083 static void
11084 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11085 dwarf2_cu *cu, struct dwo_file &dwo_file,
11086 dwarf2_section_info &section, htab_up &cus_htab)
11087 {
11088 struct objfile *objfile = dwarf2_per_objfile->objfile;
11089 const gdb_byte *info_ptr, *end_ptr;
11090
11091 section.read (objfile);
11092 info_ptr = section.buffer;
11093
11094 if (info_ptr == NULL)
11095 return;
11096
11097 if (dwarf_read_debug)
11098 {
11099 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11100 section.get_name (),
11101 section.get_file_name ());
11102 }
11103
11104 end_ptr = info_ptr + section.size;
11105 while (info_ptr < end_ptr)
11106 {
11107 struct dwarf2_per_cu_data per_cu;
11108 struct dwo_unit read_unit {};
11109 struct dwo_unit *dwo_unit;
11110 void **slot;
11111 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11112
11113 memset (&per_cu, 0, sizeof (per_cu));
11114 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11115 per_cu.is_debug_types = 0;
11116 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11117 per_cu.section = &section;
11118
11119 cutu_reader reader (&per_cu, cu, &dwo_file);
11120 if (!reader.dummy_p)
11121 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11122 &dwo_file, &read_unit);
11123 info_ptr += per_cu.length;
11124
11125 // If the unit could not be parsed, skip it.
11126 if (read_unit.dwo_file == NULL)
11127 continue;
11128
11129 if (cus_htab == NULL)
11130 cus_htab = allocate_dwo_unit_table ();
11131
11132 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11133 *dwo_unit = read_unit;
11134 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11135 gdb_assert (slot != NULL);
11136 if (*slot != NULL)
11137 {
11138 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11139 sect_offset dup_sect_off = dup_cu->sect_off;
11140
11141 complaint (_("debug cu entry at offset %s is duplicate to"
11142 " the entry at offset %s, signature %s"),
11143 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11144 hex_string (dwo_unit->signature));
11145 }
11146 *slot = (void *)dwo_unit;
11147 }
11148 }
11149
11150 /* DWP file .debug_{cu,tu}_index section format:
11151 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11152
11153 DWP Version 1:
11154
11155 Both index sections have the same format, and serve to map a 64-bit
11156 signature to a set of section numbers. Each section begins with a header,
11157 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11158 indexes, and a pool of 32-bit section numbers. The index sections will be
11159 aligned at 8-byte boundaries in the file.
11160
11161 The index section header consists of:
11162
11163 V, 32 bit version number
11164 -, 32 bits unused
11165 N, 32 bit number of compilation units or type units in the index
11166 M, 32 bit number of slots in the hash table
11167
11168 Numbers are recorded using the byte order of the application binary.
11169
11170 The hash table begins at offset 16 in the section, and consists of an array
11171 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11172 order of the application binary). Unused slots in the hash table are 0.
11173 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11174
11175 The parallel table begins immediately after the hash table
11176 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11177 array of 32-bit indexes (using the byte order of the application binary),
11178 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11179 table contains a 32-bit index into the pool of section numbers. For unused
11180 hash table slots, the corresponding entry in the parallel table will be 0.
11181
11182 The pool of section numbers begins immediately following the hash table
11183 (at offset 16 + 12 * M from the beginning of the section). The pool of
11184 section numbers consists of an array of 32-bit words (using the byte order
11185 of the application binary). Each item in the array is indexed starting
11186 from 0. The hash table entry provides the index of the first section
11187 number in the set. Additional section numbers in the set follow, and the
11188 set is terminated by a 0 entry (section number 0 is not used in ELF).
11189
11190 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11191 section must be the first entry in the set, and the .debug_abbrev.dwo must
11192 be the second entry. Other members of the set may follow in any order.
11193
11194 ---
11195
11196 DWP Version 2:
11197
11198 DWP Version 2 combines all the .debug_info, etc. sections into one,
11199 and the entries in the index tables are now offsets into these sections.
11200 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11201 section.
11202
11203 Index Section Contents:
11204 Header
11205 Hash Table of Signatures dwp_hash_table.hash_table
11206 Parallel Table of Indices dwp_hash_table.unit_table
11207 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11208 Table of Section Sizes dwp_hash_table.v2.sizes
11209
11210 The index section header consists of:
11211
11212 V, 32 bit version number
11213 L, 32 bit number of columns in the table of section offsets
11214 N, 32 bit number of compilation units or type units in the index
11215 M, 32 bit number of slots in the hash table
11216
11217 Numbers are recorded using the byte order of the application binary.
11218
11219 The hash table has the same format as version 1.
11220 The parallel table of indices has the same format as version 1,
11221 except that the entries are origin-1 indices into the table of sections
11222 offsets and the table of section sizes.
11223
11224 The table of offsets begins immediately following the parallel table
11225 (at offset 16 + 12 * M from the beginning of the section). The table is
11226 a two-dimensional array of 32-bit words (using the byte order of the
11227 application binary), with L columns and N+1 rows, in row-major order.
11228 Each row in the array is indexed starting from 0. The first row provides
11229 a key to the remaining rows: each column in this row provides an identifier
11230 for a debug section, and the offsets in the same column of subsequent rows
11231 refer to that section. The section identifiers are:
11232
11233 DW_SECT_INFO 1 .debug_info.dwo
11234 DW_SECT_TYPES 2 .debug_types.dwo
11235 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11236 DW_SECT_LINE 4 .debug_line.dwo
11237 DW_SECT_LOC 5 .debug_loc.dwo
11238 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11239 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11240 DW_SECT_MACRO 8 .debug_macro.dwo
11241
11242 The offsets provided by the CU and TU index sections are the base offsets
11243 for the contributions made by each CU or TU to the corresponding section
11244 in the package file. Each CU and TU header contains an abbrev_offset
11245 field, used to find the abbreviations table for that CU or TU within the
11246 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11247 be interpreted as relative to the base offset given in the index section.
11248 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11249 should be interpreted as relative to the base offset for .debug_line.dwo,
11250 and offsets into other debug sections obtained from DWARF attributes should
11251 also be interpreted as relative to the corresponding base offset.
11252
11253 The table of sizes begins immediately following the table of offsets.
11254 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11255 with L columns and N rows, in row-major order. Each row in the array is
11256 indexed starting from 1 (row 0 is shared by the two tables).
11257
11258 ---
11259
11260 Hash table lookup is handled the same in version 1 and 2:
11261
11262 We assume that N and M will not exceed 2^32 - 1.
11263 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11264
11265 Given a 64-bit compilation unit signature or a type signature S, an entry
11266 in the hash table is located as follows:
11267
11268 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11269 the low-order k bits all set to 1.
11270
11271 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11272
11273 3) If the hash table entry at index H matches the signature, use that
11274 entry. If the hash table entry at index H is unused (all zeroes),
11275 terminate the search: the signature is not present in the table.
11276
11277 4) Let H = (H + H') modulo M. Repeat at Step 3.
11278
11279 Because M > N and H' and M are relatively prime, the search is guaranteed
11280 to stop at an unused slot or find the match. */
11281
11282 /* Create a hash table to map DWO IDs to their CU/TU entry in
11283 .debug_{info,types}.dwo in DWP_FILE.
11284 Returns NULL if there isn't one.
11285 Note: This function processes DWP files only, not DWO files. */
11286
11287 static struct dwp_hash_table *
11288 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11289 struct dwp_file *dwp_file, int is_debug_types)
11290 {
11291 struct objfile *objfile = dwarf2_per_objfile->objfile;
11292 bfd *dbfd = dwp_file->dbfd.get ();
11293 const gdb_byte *index_ptr, *index_end;
11294 struct dwarf2_section_info *index;
11295 uint32_t version, nr_columns, nr_units, nr_slots;
11296 struct dwp_hash_table *htab;
11297
11298 if (is_debug_types)
11299 index = &dwp_file->sections.tu_index;
11300 else
11301 index = &dwp_file->sections.cu_index;
11302
11303 if (index->empty ())
11304 return NULL;
11305 index->read (objfile);
11306
11307 index_ptr = index->buffer;
11308 index_end = index_ptr + index->size;
11309
11310 version = read_4_bytes (dbfd, index_ptr);
11311 index_ptr += 4;
11312 if (version == 2)
11313 nr_columns = read_4_bytes (dbfd, index_ptr);
11314 else
11315 nr_columns = 0;
11316 index_ptr += 4;
11317 nr_units = read_4_bytes (dbfd, index_ptr);
11318 index_ptr += 4;
11319 nr_slots = read_4_bytes (dbfd, index_ptr);
11320 index_ptr += 4;
11321
11322 if (version != 1 && version != 2)
11323 {
11324 error (_("Dwarf Error: unsupported DWP file version (%s)"
11325 " [in module %s]"),
11326 pulongest (version), dwp_file->name);
11327 }
11328 if (nr_slots != (nr_slots & -nr_slots))
11329 {
11330 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11331 " is not power of 2 [in module %s]"),
11332 pulongest (nr_slots), dwp_file->name);
11333 }
11334
11335 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11336 htab->version = version;
11337 htab->nr_columns = nr_columns;
11338 htab->nr_units = nr_units;
11339 htab->nr_slots = nr_slots;
11340 htab->hash_table = index_ptr;
11341 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11342
11343 /* Exit early if the table is empty. */
11344 if (nr_slots == 0 || nr_units == 0
11345 || (version == 2 && nr_columns == 0))
11346 {
11347 /* All must be zero. */
11348 if (nr_slots != 0 || nr_units != 0
11349 || (version == 2 && nr_columns != 0))
11350 {
11351 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11352 " all zero [in modules %s]"),
11353 dwp_file->name);
11354 }
11355 return htab;
11356 }
11357
11358 if (version == 1)
11359 {
11360 htab->section_pool.v1.indices =
11361 htab->unit_table + sizeof (uint32_t) * nr_slots;
11362 /* It's harder to decide whether the section is too small in v1.
11363 V1 is deprecated anyway so we punt. */
11364 }
11365 else
11366 {
11367 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11368 int *ids = htab->section_pool.v2.section_ids;
11369 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11370 /* Reverse map for error checking. */
11371 int ids_seen[DW_SECT_MAX + 1];
11372 int i;
11373
11374 if (nr_columns < 2)
11375 {
11376 error (_("Dwarf Error: bad DWP hash table, too few columns"
11377 " in section table [in module %s]"),
11378 dwp_file->name);
11379 }
11380 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11381 {
11382 error (_("Dwarf Error: bad DWP hash table, too many columns"
11383 " in section table [in module %s]"),
11384 dwp_file->name);
11385 }
11386 memset (ids, 255, sizeof_ids);
11387 memset (ids_seen, 255, sizeof (ids_seen));
11388 for (i = 0; i < nr_columns; ++i)
11389 {
11390 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11391
11392 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11393 {
11394 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11395 " in section table [in module %s]"),
11396 id, dwp_file->name);
11397 }
11398 if (ids_seen[id] != -1)
11399 {
11400 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11401 " id %d in section table [in module %s]"),
11402 id, dwp_file->name);
11403 }
11404 ids_seen[id] = i;
11405 ids[i] = id;
11406 }
11407 /* Must have exactly one info or types section. */
11408 if (((ids_seen[DW_SECT_INFO] != -1)
11409 + (ids_seen[DW_SECT_TYPES] != -1))
11410 != 1)
11411 {
11412 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11413 " DWO info/types section [in module %s]"),
11414 dwp_file->name);
11415 }
11416 /* Must have an abbrev section. */
11417 if (ids_seen[DW_SECT_ABBREV] == -1)
11418 {
11419 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11420 " section [in module %s]"),
11421 dwp_file->name);
11422 }
11423 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11424 htab->section_pool.v2.sizes =
11425 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11426 * nr_units * nr_columns);
11427 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11428 * nr_units * nr_columns))
11429 > index_end)
11430 {
11431 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11432 " [in module %s]"),
11433 dwp_file->name);
11434 }
11435 }
11436
11437 return htab;
11438 }
11439
11440 /* Update SECTIONS with the data from SECTP.
11441
11442 This function is like the other "locate" section routines that are
11443 passed to bfd_map_over_sections, but in this context the sections to
11444 read comes from the DWP V1 hash table, not the full ELF section table.
11445
11446 The result is non-zero for success, or zero if an error was found. */
11447
11448 static int
11449 locate_v1_virtual_dwo_sections (asection *sectp,
11450 struct virtual_v1_dwo_sections *sections)
11451 {
11452 const struct dwop_section_names *names = &dwop_section_names;
11453
11454 if (section_is_p (sectp->name, &names->abbrev_dwo))
11455 {
11456 /* There can be only one. */
11457 if (sections->abbrev.s.section != NULL)
11458 return 0;
11459 sections->abbrev.s.section = sectp;
11460 sections->abbrev.size = bfd_section_size (sectp);
11461 }
11462 else if (section_is_p (sectp->name, &names->info_dwo)
11463 || section_is_p (sectp->name, &names->types_dwo))
11464 {
11465 /* There can be only one. */
11466 if (sections->info_or_types.s.section != NULL)
11467 return 0;
11468 sections->info_or_types.s.section = sectp;
11469 sections->info_or_types.size = bfd_section_size (sectp);
11470 }
11471 else if (section_is_p (sectp->name, &names->line_dwo))
11472 {
11473 /* There can be only one. */
11474 if (sections->line.s.section != NULL)
11475 return 0;
11476 sections->line.s.section = sectp;
11477 sections->line.size = bfd_section_size (sectp);
11478 }
11479 else if (section_is_p (sectp->name, &names->loc_dwo))
11480 {
11481 /* There can be only one. */
11482 if (sections->loc.s.section != NULL)
11483 return 0;
11484 sections->loc.s.section = sectp;
11485 sections->loc.size = bfd_section_size (sectp);
11486 }
11487 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11488 {
11489 /* There can be only one. */
11490 if (sections->macinfo.s.section != NULL)
11491 return 0;
11492 sections->macinfo.s.section = sectp;
11493 sections->macinfo.size = bfd_section_size (sectp);
11494 }
11495 else if (section_is_p (sectp->name, &names->macro_dwo))
11496 {
11497 /* There can be only one. */
11498 if (sections->macro.s.section != NULL)
11499 return 0;
11500 sections->macro.s.section = sectp;
11501 sections->macro.size = bfd_section_size (sectp);
11502 }
11503 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11504 {
11505 /* There can be only one. */
11506 if (sections->str_offsets.s.section != NULL)
11507 return 0;
11508 sections->str_offsets.s.section = sectp;
11509 sections->str_offsets.size = bfd_section_size (sectp);
11510 }
11511 else
11512 {
11513 /* No other kind of section is valid. */
11514 return 0;
11515 }
11516
11517 return 1;
11518 }
11519
11520 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11521 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11522 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11523 This is for DWP version 1 files. */
11524
11525 static struct dwo_unit *
11526 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11527 struct dwp_file *dwp_file,
11528 uint32_t unit_index,
11529 const char *comp_dir,
11530 ULONGEST signature, int is_debug_types)
11531 {
11532 struct objfile *objfile = dwarf2_per_objfile->objfile;
11533 const struct dwp_hash_table *dwp_htab =
11534 is_debug_types ? dwp_file->tus : dwp_file->cus;
11535 bfd *dbfd = dwp_file->dbfd.get ();
11536 const char *kind = is_debug_types ? "TU" : "CU";
11537 struct dwo_file *dwo_file;
11538 struct dwo_unit *dwo_unit;
11539 struct virtual_v1_dwo_sections sections;
11540 void **dwo_file_slot;
11541 int i;
11542
11543 gdb_assert (dwp_file->version == 1);
11544
11545 if (dwarf_read_debug)
11546 {
11547 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11548 kind,
11549 pulongest (unit_index), hex_string (signature),
11550 dwp_file->name);
11551 }
11552
11553 /* Fetch the sections of this DWO unit.
11554 Put a limit on the number of sections we look for so that bad data
11555 doesn't cause us to loop forever. */
11556
11557 #define MAX_NR_V1_DWO_SECTIONS \
11558 (1 /* .debug_info or .debug_types */ \
11559 + 1 /* .debug_abbrev */ \
11560 + 1 /* .debug_line */ \
11561 + 1 /* .debug_loc */ \
11562 + 1 /* .debug_str_offsets */ \
11563 + 1 /* .debug_macro or .debug_macinfo */ \
11564 + 1 /* trailing zero */)
11565
11566 memset (&sections, 0, sizeof (sections));
11567
11568 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11569 {
11570 asection *sectp;
11571 uint32_t section_nr =
11572 read_4_bytes (dbfd,
11573 dwp_htab->section_pool.v1.indices
11574 + (unit_index + i) * sizeof (uint32_t));
11575
11576 if (section_nr == 0)
11577 break;
11578 if (section_nr >= dwp_file->num_sections)
11579 {
11580 error (_("Dwarf Error: bad DWP hash table, section number too large"
11581 " [in module %s]"),
11582 dwp_file->name);
11583 }
11584
11585 sectp = dwp_file->elf_sections[section_nr];
11586 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11587 {
11588 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11589 " [in module %s]"),
11590 dwp_file->name);
11591 }
11592 }
11593
11594 if (i < 2
11595 || sections.info_or_types.empty ()
11596 || sections.abbrev.empty ())
11597 {
11598 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11599 " [in module %s]"),
11600 dwp_file->name);
11601 }
11602 if (i == MAX_NR_V1_DWO_SECTIONS)
11603 {
11604 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11605 " [in module %s]"),
11606 dwp_file->name);
11607 }
11608
11609 /* It's easier for the rest of the code if we fake a struct dwo_file and
11610 have dwo_unit "live" in that. At least for now.
11611
11612 The DWP file can be made up of a random collection of CUs and TUs.
11613 However, for each CU + set of TUs that came from the same original DWO
11614 file, we can combine them back into a virtual DWO file to save space
11615 (fewer struct dwo_file objects to allocate). Remember that for really
11616 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11617
11618 std::string virtual_dwo_name =
11619 string_printf ("virtual-dwo/%d-%d-%d-%d",
11620 sections.abbrev.get_id (),
11621 sections.line.get_id (),
11622 sections.loc.get_id (),
11623 sections.str_offsets.get_id ());
11624 /* Can we use an existing virtual DWO file? */
11625 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11626 virtual_dwo_name.c_str (),
11627 comp_dir);
11628 /* Create one if necessary. */
11629 if (*dwo_file_slot == NULL)
11630 {
11631 if (dwarf_read_debug)
11632 {
11633 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11634 virtual_dwo_name.c_str ());
11635 }
11636 dwo_file = new struct dwo_file;
11637 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11638 virtual_dwo_name);
11639 dwo_file->comp_dir = comp_dir;
11640 dwo_file->sections.abbrev = sections.abbrev;
11641 dwo_file->sections.line = sections.line;
11642 dwo_file->sections.loc = sections.loc;
11643 dwo_file->sections.macinfo = sections.macinfo;
11644 dwo_file->sections.macro = sections.macro;
11645 dwo_file->sections.str_offsets = sections.str_offsets;
11646 /* The "str" section is global to the entire DWP file. */
11647 dwo_file->sections.str = dwp_file->sections.str;
11648 /* The info or types section is assigned below to dwo_unit,
11649 there's no need to record it in dwo_file.
11650 Also, we can't simply record type sections in dwo_file because
11651 we record a pointer into the vector in dwo_unit. As we collect more
11652 types we'll grow the vector and eventually have to reallocate space
11653 for it, invalidating all copies of pointers into the previous
11654 contents. */
11655 *dwo_file_slot = dwo_file;
11656 }
11657 else
11658 {
11659 if (dwarf_read_debug)
11660 {
11661 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11662 virtual_dwo_name.c_str ());
11663 }
11664 dwo_file = (struct dwo_file *) *dwo_file_slot;
11665 }
11666
11667 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11668 dwo_unit->dwo_file = dwo_file;
11669 dwo_unit->signature = signature;
11670 dwo_unit->section =
11671 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11672 *dwo_unit->section = sections.info_or_types;
11673 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11674
11675 return dwo_unit;
11676 }
11677
11678 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11679 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11680 piece within that section used by a TU/CU, return a virtual section
11681 of just that piece. */
11682
11683 static struct dwarf2_section_info
11684 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11685 struct dwarf2_section_info *section,
11686 bfd_size_type offset, bfd_size_type size)
11687 {
11688 struct dwarf2_section_info result;
11689 asection *sectp;
11690
11691 gdb_assert (section != NULL);
11692 gdb_assert (!section->is_virtual);
11693
11694 memset (&result, 0, sizeof (result));
11695 result.s.containing_section = section;
11696 result.is_virtual = true;
11697
11698 if (size == 0)
11699 return result;
11700
11701 sectp = section->get_bfd_section ();
11702
11703 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11704 bounds of the real section. This is a pretty-rare event, so just
11705 flag an error (easier) instead of a warning and trying to cope. */
11706 if (sectp == NULL
11707 || offset + size > bfd_section_size (sectp))
11708 {
11709 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11710 " in section %s [in module %s]"),
11711 sectp ? bfd_section_name (sectp) : "<unknown>",
11712 objfile_name (dwarf2_per_objfile->objfile));
11713 }
11714
11715 result.virtual_offset = offset;
11716 result.size = size;
11717 return result;
11718 }
11719
11720 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11721 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11722 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11723 This is for DWP version 2 files. */
11724
11725 static struct dwo_unit *
11726 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11727 struct dwp_file *dwp_file,
11728 uint32_t unit_index,
11729 const char *comp_dir,
11730 ULONGEST signature, int is_debug_types)
11731 {
11732 struct objfile *objfile = dwarf2_per_objfile->objfile;
11733 const struct dwp_hash_table *dwp_htab =
11734 is_debug_types ? dwp_file->tus : dwp_file->cus;
11735 bfd *dbfd = dwp_file->dbfd.get ();
11736 const char *kind = is_debug_types ? "TU" : "CU";
11737 struct dwo_file *dwo_file;
11738 struct dwo_unit *dwo_unit;
11739 struct virtual_v2_dwo_sections sections;
11740 void **dwo_file_slot;
11741 int i;
11742
11743 gdb_assert (dwp_file->version == 2);
11744
11745 if (dwarf_read_debug)
11746 {
11747 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11748 kind,
11749 pulongest (unit_index), hex_string (signature),
11750 dwp_file->name);
11751 }
11752
11753 /* Fetch the section offsets of this DWO unit. */
11754
11755 memset (&sections, 0, sizeof (sections));
11756
11757 for (i = 0; i < dwp_htab->nr_columns; ++i)
11758 {
11759 uint32_t offset = read_4_bytes (dbfd,
11760 dwp_htab->section_pool.v2.offsets
11761 + (((unit_index - 1) * dwp_htab->nr_columns
11762 + i)
11763 * sizeof (uint32_t)));
11764 uint32_t size = read_4_bytes (dbfd,
11765 dwp_htab->section_pool.v2.sizes
11766 + (((unit_index - 1) * dwp_htab->nr_columns
11767 + i)
11768 * sizeof (uint32_t)));
11769
11770 switch (dwp_htab->section_pool.v2.section_ids[i])
11771 {
11772 case DW_SECT_INFO:
11773 case DW_SECT_TYPES:
11774 sections.info_or_types_offset = offset;
11775 sections.info_or_types_size = size;
11776 break;
11777 case DW_SECT_ABBREV:
11778 sections.abbrev_offset = offset;
11779 sections.abbrev_size = size;
11780 break;
11781 case DW_SECT_LINE:
11782 sections.line_offset = offset;
11783 sections.line_size = size;
11784 break;
11785 case DW_SECT_LOC:
11786 sections.loc_offset = offset;
11787 sections.loc_size = size;
11788 break;
11789 case DW_SECT_STR_OFFSETS:
11790 sections.str_offsets_offset = offset;
11791 sections.str_offsets_size = size;
11792 break;
11793 case DW_SECT_MACINFO:
11794 sections.macinfo_offset = offset;
11795 sections.macinfo_size = size;
11796 break;
11797 case DW_SECT_MACRO:
11798 sections.macro_offset = offset;
11799 sections.macro_size = size;
11800 break;
11801 }
11802 }
11803
11804 /* It's easier for the rest of the code if we fake a struct dwo_file and
11805 have dwo_unit "live" in that. At least for now.
11806
11807 The DWP file can be made up of a random collection of CUs and TUs.
11808 However, for each CU + set of TUs that came from the same original DWO
11809 file, we can combine them back into a virtual DWO file to save space
11810 (fewer struct dwo_file objects to allocate). Remember that for really
11811 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11812
11813 std::string virtual_dwo_name =
11814 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11815 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11816 (long) (sections.line_size ? sections.line_offset : 0),
11817 (long) (sections.loc_size ? sections.loc_offset : 0),
11818 (long) (sections.str_offsets_size
11819 ? sections.str_offsets_offset : 0));
11820 /* Can we use an existing virtual DWO file? */
11821 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11822 virtual_dwo_name.c_str (),
11823 comp_dir);
11824 /* Create one if necessary. */
11825 if (*dwo_file_slot == NULL)
11826 {
11827 if (dwarf_read_debug)
11828 {
11829 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11830 virtual_dwo_name.c_str ());
11831 }
11832 dwo_file = new struct dwo_file;
11833 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11834 virtual_dwo_name);
11835 dwo_file->comp_dir = comp_dir;
11836 dwo_file->sections.abbrev =
11837 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11838 sections.abbrev_offset, sections.abbrev_size);
11839 dwo_file->sections.line =
11840 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11841 sections.line_offset, sections.line_size);
11842 dwo_file->sections.loc =
11843 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11844 sections.loc_offset, sections.loc_size);
11845 dwo_file->sections.macinfo =
11846 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11847 sections.macinfo_offset, sections.macinfo_size);
11848 dwo_file->sections.macro =
11849 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11850 sections.macro_offset, sections.macro_size);
11851 dwo_file->sections.str_offsets =
11852 create_dwp_v2_section (dwarf2_per_objfile,
11853 &dwp_file->sections.str_offsets,
11854 sections.str_offsets_offset,
11855 sections.str_offsets_size);
11856 /* The "str" section is global to the entire DWP file. */
11857 dwo_file->sections.str = dwp_file->sections.str;
11858 /* The info or types section is assigned below to dwo_unit,
11859 there's no need to record it in dwo_file.
11860 Also, we can't simply record type sections in dwo_file because
11861 we record a pointer into the vector in dwo_unit. As we collect more
11862 types we'll grow the vector and eventually have to reallocate space
11863 for it, invalidating all copies of pointers into the previous
11864 contents. */
11865 *dwo_file_slot = dwo_file;
11866 }
11867 else
11868 {
11869 if (dwarf_read_debug)
11870 {
11871 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11872 virtual_dwo_name.c_str ());
11873 }
11874 dwo_file = (struct dwo_file *) *dwo_file_slot;
11875 }
11876
11877 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11878 dwo_unit->dwo_file = dwo_file;
11879 dwo_unit->signature = signature;
11880 dwo_unit->section =
11881 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11882 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11883 is_debug_types
11884 ? &dwp_file->sections.types
11885 : &dwp_file->sections.info,
11886 sections.info_or_types_offset,
11887 sections.info_or_types_size);
11888 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11889
11890 return dwo_unit;
11891 }
11892
11893 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11894 Returns NULL if the signature isn't found. */
11895
11896 static struct dwo_unit *
11897 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11898 struct dwp_file *dwp_file, const char *comp_dir,
11899 ULONGEST signature, int is_debug_types)
11900 {
11901 const struct dwp_hash_table *dwp_htab =
11902 is_debug_types ? dwp_file->tus : dwp_file->cus;
11903 bfd *dbfd = dwp_file->dbfd.get ();
11904 uint32_t mask = dwp_htab->nr_slots - 1;
11905 uint32_t hash = signature & mask;
11906 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11907 unsigned int i;
11908 void **slot;
11909 struct dwo_unit find_dwo_cu;
11910
11911 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11912 find_dwo_cu.signature = signature;
11913 slot = htab_find_slot (is_debug_types
11914 ? dwp_file->loaded_tus.get ()
11915 : dwp_file->loaded_cus.get (),
11916 &find_dwo_cu, INSERT);
11917
11918 if (*slot != NULL)
11919 return (struct dwo_unit *) *slot;
11920
11921 /* Use a for loop so that we don't loop forever on bad debug info. */
11922 for (i = 0; i < dwp_htab->nr_slots; ++i)
11923 {
11924 ULONGEST signature_in_table;
11925
11926 signature_in_table =
11927 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11928 if (signature_in_table == signature)
11929 {
11930 uint32_t unit_index =
11931 read_4_bytes (dbfd,
11932 dwp_htab->unit_table + hash * sizeof (uint32_t));
11933
11934 if (dwp_file->version == 1)
11935 {
11936 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11937 dwp_file, unit_index,
11938 comp_dir, signature,
11939 is_debug_types);
11940 }
11941 else
11942 {
11943 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11944 dwp_file, unit_index,
11945 comp_dir, signature,
11946 is_debug_types);
11947 }
11948 return (struct dwo_unit *) *slot;
11949 }
11950 if (signature_in_table == 0)
11951 return NULL;
11952 hash = (hash + hash2) & mask;
11953 }
11954
11955 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11956 " [in module %s]"),
11957 dwp_file->name);
11958 }
11959
11960 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11961 Open the file specified by FILE_NAME and hand it off to BFD for
11962 preliminary analysis. Return a newly initialized bfd *, which
11963 includes a canonicalized copy of FILE_NAME.
11964 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11965 SEARCH_CWD is true if the current directory is to be searched.
11966 It will be searched before debug-file-directory.
11967 If successful, the file is added to the bfd include table of the
11968 objfile's bfd (see gdb_bfd_record_inclusion).
11969 If unable to find/open the file, return NULL.
11970 NOTE: This function is derived from symfile_bfd_open. */
11971
11972 static gdb_bfd_ref_ptr
11973 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11974 const char *file_name, int is_dwp, int search_cwd)
11975 {
11976 int desc;
11977 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11978 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11979 to debug_file_directory. */
11980 const char *search_path;
11981 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11982
11983 gdb::unique_xmalloc_ptr<char> search_path_holder;
11984 if (search_cwd)
11985 {
11986 if (*debug_file_directory != '\0')
11987 {
11988 search_path_holder.reset (concat (".", dirname_separator_string,
11989 debug_file_directory,
11990 (char *) NULL));
11991 search_path = search_path_holder.get ();
11992 }
11993 else
11994 search_path = ".";
11995 }
11996 else
11997 search_path = debug_file_directory;
11998
11999 openp_flags flags = OPF_RETURN_REALPATH;
12000 if (is_dwp)
12001 flags |= OPF_SEARCH_IN_PATH;
12002
12003 gdb::unique_xmalloc_ptr<char> absolute_name;
12004 desc = openp (search_path, flags, file_name,
12005 O_RDONLY | O_BINARY, &absolute_name);
12006 if (desc < 0)
12007 return NULL;
12008
12009 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12010 gnutarget, desc));
12011 if (sym_bfd == NULL)
12012 return NULL;
12013 bfd_set_cacheable (sym_bfd.get (), 1);
12014
12015 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12016 return NULL;
12017
12018 /* Success. Record the bfd as having been included by the objfile's bfd.
12019 This is important because things like demangled_names_hash lives in the
12020 objfile's per_bfd space and may have references to things like symbol
12021 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12022 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12023
12024 return sym_bfd;
12025 }
12026
12027 /* Try to open DWO file FILE_NAME.
12028 COMP_DIR is the DW_AT_comp_dir attribute.
12029 The result is the bfd handle of the file.
12030 If there is a problem finding or opening the file, return NULL.
12031 Upon success, the canonicalized path of the file is stored in the bfd,
12032 same as symfile_bfd_open. */
12033
12034 static gdb_bfd_ref_ptr
12035 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12036 const char *file_name, const char *comp_dir)
12037 {
12038 if (IS_ABSOLUTE_PATH (file_name))
12039 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12040 0 /*is_dwp*/, 0 /*search_cwd*/);
12041
12042 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12043
12044 if (comp_dir != NULL)
12045 {
12046 gdb::unique_xmalloc_ptr<char> path_to_try
12047 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12048
12049 /* NOTE: If comp_dir is a relative path, this will also try the
12050 search path, which seems useful. */
12051 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12052 path_to_try.get (),
12053 0 /*is_dwp*/,
12054 1 /*search_cwd*/));
12055 if (abfd != NULL)
12056 return abfd;
12057 }
12058
12059 /* That didn't work, try debug-file-directory, which, despite its name,
12060 is a list of paths. */
12061
12062 if (*debug_file_directory == '\0')
12063 return NULL;
12064
12065 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12066 0 /*is_dwp*/, 1 /*search_cwd*/);
12067 }
12068
12069 /* This function is mapped across the sections and remembers the offset and
12070 size of each of the DWO debugging sections we are interested in. */
12071
12072 static void
12073 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12074 {
12075 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12076 const struct dwop_section_names *names = &dwop_section_names;
12077
12078 if (section_is_p (sectp->name, &names->abbrev_dwo))
12079 {
12080 dwo_sections->abbrev.s.section = sectp;
12081 dwo_sections->abbrev.size = bfd_section_size (sectp);
12082 }
12083 else if (section_is_p (sectp->name, &names->info_dwo))
12084 {
12085 dwo_sections->info.s.section = sectp;
12086 dwo_sections->info.size = bfd_section_size (sectp);
12087 }
12088 else if (section_is_p (sectp->name, &names->line_dwo))
12089 {
12090 dwo_sections->line.s.section = sectp;
12091 dwo_sections->line.size = bfd_section_size (sectp);
12092 }
12093 else if (section_is_p (sectp->name, &names->loc_dwo))
12094 {
12095 dwo_sections->loc.s.section = sectp;
12096 dwo_sections->loc.size = bfd_section_size (sectp);
12097 }
12098 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12099 {
12100 dwo_sections->macinfo.s.section = sectp;
12101 dwo_sections->macinfo.size = bfd_section_size (sectp);
12102 }
12103 else if (section_is_p (sectp->name, &names->macro_dwo))
12104 {
12105 dwo_sections->macro.s.section = sectp;
12106 dwo_sections->macro.size = bfd_section_size (sectp);
12107 }
12108 else if (section_is_p (sectp->name, &names->str_dwo))
12109 {
12110 dwo_sections->str.s.section = sectp;
12111 dwo_sections->str.size = bfd_section_size (sectp);
12112 }
12113 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12114 {
12115 dwo_sections->str_offsets.s.section = sectp;
12116 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12117 }
12118 else if (section_is_p (sectp->name, &names->types_dwo))
12119 {
12120 struct dwarf2_section_info type_section;
12121
12122 memset (&type_section, 0, sizeof (type_section));
12123 type_section.s.section = sectp;
12124 type_section.size = bfd_section_size (sectp);
12125 dwo_sections->types.push_back (type_section);
12126 }
12127 }
12128
12129 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12130 by PER_CU. This is for the non-DWP case.
12131 The result is NULL if DWO_NAME can't be found. */
12132
12133 static struct dwo_file *
12134 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12135 const char *dwo_name, const char *comp_dir)
12136 {
12137 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12138
12139 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12140 if (dbfd == NULL)
12141 {
12142 if (dwarf_read_debug)
12143 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12144 return NULL;
12145 }
12146
12147 dwo_file_up dwo_file (new struct dwo_file);
12148 dwo_file->dwo_name = dwo_name;
12149 dwo_file->comp_dir = comp_dir;
12150 dwo_file->dbfd = std::move (dbfd);
12151
12152 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12153 &dwo_file->sections);
12154
12155 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12156 dwo_file->sections.info, dwo_file->cus);
12157
12158 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12159 dwo_file->sections.types, dwo_file->tus);
12160
12161 if (dwarf_read_debug)
12162 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12163
12164 return dwo_file.release ();
12165 }
12166
12167 /* This function is mapped across the sections and remembers the offset and
12168 size of each of the DWP debugging sections common to version 1 and 2 that
12169 we are interested in. */
12170
12171 static void
12172 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12173 void *dwp_file_ptr)
12174 {
12175 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12176 const struct dwop_section_names *names = &dwop_section_names;
12177 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12178
12179 /* Record the ELF section number for later lookup: this is what the
12180 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12181 gdb_assert (elf_section_nr < dwp_file->num_sections);
12182 dwp_file->elf_sections[elf_section_nr] = sectp;
12183
12184 /* Look for specific sections that we need. */
12185 if (section_is_p (sectp->name, &names->str_dwo))
12186 {
12187 dwp_file->sections.str.s.section = sectp;
12188 dwp_file->sections.str.size = bfd_section_size (sectp);
12189 }
12190 else if (section_is_p (sectp->name, &names->cu_index))
12191 {
12192 dwp_file->sections.cu_index.s.section = sectp;
12193 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12194 }
12195 else if (section_is_p (sectp->name, &names->tu_index))
12196 {
12197 dwp_file->sections.tu_index.s.section = sectp;
12198 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12199 }
12200 }
12201
12202 /* This function is mapped across the sections and remembers the offset and
12203 size of each of the DWP version 2 debugging sections that we are interested
12204 in. This is split into a separate function because we don't know if we
12205 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12206
12207 static void
12208 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12209 {
12210 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12211 const struct dwop_section_names *names = &dwop_section_names;
12212 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12213
12214 /* Record the ELF section number for later lookup: this is what the
12215 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12216 gdb_assert (elf_section_nr < dwp_file->num_sections);
12217 dwp_file->elf_sections[elf_section_nr] = sectp;
12218
12219 /* Look for specific sections that we need. */
12220 if (section_is_p (sectp->name, &names->abbrev_dwo))
12221 {
12222 dwp_file->sections.abbrev.s.section = sectp;
12223 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12224 }
12225 else if (section_is_p (sectp->name, &names->info_dwo))
12226 {
12227 dwp_file->sections.info.s.section = sectp;
12228 dwp_file->sections.info.size = bfd_section_size (sectp);
12229 }
12230 else if (section_is_p (sectp->name, &names->line_dwo))
12231 {
12232 dwp_file->sections.line.s.section = sectp;
12233 dwp_file->sections.line.size = bfd_section_size (sectp);
12234 }
12235 else if (section_is_p (sectp->name, &names->loc_dwo))
12236 {
12237 dwp_file->sections.loc.s.section = sectp;
12238 dwp_file->sections.loc.size = bfd_section_size (sectp);
12239 }
12240 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12241 {
12242 dwp_file->sections.macinfo.s.section = sectp;
12243 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12244 }
12245 else if (section_is_p (sectp->name, &names->macro_dwo))
12246 {
12247 dwp_file->sections.macro.s.section = sectp;
12248 dwp_file->sections.macro.size = bfd_section_size (sectp);
12249 }
12250 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12251 {
12252 dwp_file->sections.str_offsets.s.section = sectp;
12253 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12254 }
12255 else if (section_is_p (sectp->name, &names->types_dwo))
12256 {
12257 dwp_file->sections.types.s.section = sectp;
12258 dwp_file->sections.types.size = bfd_section_size (sectp);
12259 }
12260 }
12261
12262 /* Hash function for dwp_file loaded CUs/TUs. */
12263
12264 static hashval_t
12265 hash_dwp_loaded_cutus (const void *item)
12266 {
12267 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12268
12269 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12270 return dwo_unit->signature;
12271 }
12272
12273 /* Equality function for dwp_file loaded CUs/TUs. */
12274
12275 static int
12276 eq_dwp_loaded_cutus (const void *a, const void *b)
12277 {
12278 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12279 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12280
12281 return dua->signature == dub->signature;
12282 }
12283
12284 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12285
12286 static htab_up
12287 allocate_dwp_loaded_cutus_table ()
12288 {
12289 return htab_up (htab_create_alloc (3,
12290 hash_dwp_loaded_cutus,
12291 eq_dwp_loaded_cutus,
12292 NULL, xcalloc, xfree));
12293 }
12294
12295 /* Try to open DWP file FILE_NAME.
12296 The result is the bfd handle of the file.
12297 If there is a problem finding or opening the file, return NULL.
12298 Upon success, the canonicalized path of the file is stored in the bfd,
12299 same as symfile_bfd_open. */
12300
12301 static gdb_bfd_ref_ptr
12302 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12303 const char *file_name)
12304 {
12305 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12306 1 /*is_dwp*/,
12307 1 /*search_cwd*/));
12308 if (abfd != NULL)
12309 return abfd;
12310
12311 /* Work around upstream bug 15652.
12312 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12313 [Whether that's a "bug" is debatable, but it is getting in our way.]
12314 We have no real idea where the dwp file is, because gdb's realpath-ing
12315 of the executable's path may have discarded the needed info.
12316 [IWBN if the dwp file name was recorded in the executable, akin to
12317 .gnu_debuglink, but that doesn't exist yet.]
12318 Strip the directory from FILE_NAME and search again. */
12319 if (*debug_file_directory != '\0')
12320 {
12321 /* Don't implicitly search the current directory here.
12322 If the user wants to search "." to handle this case,
12323 it must be added to debug-file-directory. */
12324 return try_open_dwop_file (dwarf2_per_objfile,
12325 lbasename (file_name), 1 /*is_dwp*/,
12326 0 /*search_cwd*/);
12327 }
12328
12329 return NULL;
12330 }
12331
12332 /* Initialize the use of the DWP file for the current objfile.
12333 By convention the name of the DWP file is ${objfile}.dwp.
12334 The result is NULL if it can't be found. */
12335
12336 static std::unique_ptr<struct dwp_file>
12337 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12338 {
12339 struct objfile *objfile = dwarf2_per_objfile->objfile;
12340
12341 /* Try to find first .dwp for the binary file before any symbolic links
12342 resolving. */
12343
12344 /* If the objfile is a debug file, find the name of the real binary
12345 file and get the name of dwp file from there. */
12346 std::string dwp_name;
12347 if (objfile->separate_debug_objfile_backlink != NULL)
12348 {
12349 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12350 const char *backlink_basename = lbasename (backlink->original_name);
12351
12352 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12353 }
12354 else
12355 dwp_name = objfile->original_name;
12356
12357 dwp_name += ".dwp";
12358
12359 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12360 if (dbfd == NULL
12361 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12362 {
12363 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12364 dwp_name = objfile_name (objfile);
12365 dwp_name += ".dwp";
12366 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12367 }
12368
12369 if (dbfd == NULL)
12370 {
12371 if (dwarf_read_debug)
12372 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12373 return std::unique_ptr<dwp_file> ();
12374 }
12375
12376 const char *name = bfd_get_filename (dbfd.get ());
12377 std::unique_ptr<struct dwp_file> dwp_file
12378 (new struct dwp_file (name, std::move (dbfd)));
12379
12380 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12381 dwp_file->elf_sections =
12382 OBSTACK_CALLOC (&objfile->objfile_obstack,
12383 dwp_file->num_sections, asection *);
12384
12385 bfd_map_over_sections (dwp_file->dbfd.get (),
12386 dwarf2_locate_common_dwp_sections,
12387 dwp_file.get ());
12388
12389 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12390 0);
12391
12392 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12393 1);
12394
12395 /* The DWP file version is stored in the hash table. Oh well. */
12396 if (dwp_file->cus && dwp_file->tus
12397 && dwp_file->cus->version != dwp_file->tus->version)
12398 {
12399 /* Technically speaking, we should try to limp along, but this is
12400 pretty bizarre. We use pulongest here because that's the established
12401 portability solution (e.g, we cannot use %u for uint32_t). */
12402 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12403 " TU version %s [in DWP file %s]"),
12404 pulongest (dwp_file->cus->version),
12405 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12406 }
12407
12408 if (dwp_file->cus)
12409 dwp_file->version = dwp_file->cus->version;
12410 else if (dwp_file->tus)
12411 dwp_file->version = dwp_file->tus->version;
12412 else
12413 dwp_file->version = 2;
12414
12415 if (dwp_file->version == 2)
12416 bfd_map_over_sections (dwp_file->dbfd.get (),
12417 dwarf2_locate_v2_dwp_sections,
12418 dwp_file.get ());
12419
12420 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12421 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12422
12423 if (dwarf_read_debug)
12424 {
12425 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12426 fprintf_unfiltered (gdb_stdlog,
12427 " %s CUs, %s TUs\n",
12428 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12429 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12430 }
12431
12432 return dwp_file;
12433 }
12434
12435 /* Wrapper around open_and_init_dwp_file, only open it once. */
12436
12437 static struct dwp_file *
12438 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12439 {
12440 if (! dwarf2_per_objfile->dwp_checked)
12441 {
12442 dwarf2_per_objfile->dwp_file
12443 = open_and_init_dwp_file (dwarf2_per_objfile);
12444 dwarf2_per_objfile->dwp_checked = 1;
12445 }
12446 return dwarf2_per_objfile->dwp_file.get ();
12447 }
12448
12449 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12450 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12451 or in the DWP file for the objfile, referenced by THIS_UNIT.
12452 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12453 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12454
12455 This is called, for example, when wanting to read a variable with a
12456 complex location. Therefore we don't want to do file i/o for every call.
12457 Therefore we don't want to look for a DWO file on every call.
12458 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12459 then we check if we've already seen DWO_NAME, and only THEN do we check
12460 for a DWO file.
12461
12462 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12463 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12464
12465 static struct dwo_unit *
12466 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12467 const char *dwo_name, const char *comp_dir,
12468 ULONGEST signature, int is_debug_types)
12469 {
12470 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12471 struct objfile *objfile = dwarf2_per_objfile->objfile;
12472 const char *kind = is_debug_types ? "TU" : "CU";
12473 void **dwo_file_slot;
12474 struct dwo_file *dwo_file;
12475 struct dwp_file *dwp_file;
12476
12477 /* First see if there's a DWP file.
12478 If we have a DWP file but didn't find the DWO inside it, don't
12479 look for the original DWO file. It makes gdb behave differently
12480 depending on whether one is debugging in the build tree. */
12481
12482 dwp_file = get_dwp_file (dwarf2_per_objfile);
12483 if (dwp_file != NULL)
12484 {
12485 const struct dwp_hash_table *dwp_htab =
12486 is_debug_types ? dwp_file->tus : dwp_file->cus;
12487
12488 if (dwp_htab != NULL)
12489 {
12490 struct dwo_unit *dwo_cutu =
12491 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12492 signature, is_debug_types);
12493
12494 if (dwo_cutu != NULL)
12495 {
12496 if (dwarf_read_debug)
12497 {
12498 fprintf_unfiltered (gdb_stdlog,
12499 "Virtual DWO %s %s found: @%s\n",
12500 kind, hex_string (signature),
12501 host_address_to_string (dwo_cutu));
12502 }
12503 return dwo_cutu;
12504 }
12505 }
12506 }
12507 else
12508 {
12509 /* No DWP file, look for the DWO file. */
12510
12511 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12512 dwo_name, comp_dir);
12513 if (*dwo_file_slot == NULL)
12514 {
12515 /* Read in the file and build a table of the CUs/TUs it contains. */
12516 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12517 }
12518 /* NOTE: This will be NULL if unable to open the file. */
12519 dwo_file = (struct dwo_file *) *dwo_file_slot;
12520
12521 if (dwo_file != NULL)
12522 {
12523 struct dwo_unit *dwo_cutu = NULL;
12524
12525 if (is_debug_types && dwo_file->tus)
12526 {
12527 struct dwo_unit find_dwo_cutu;
12528
12529 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12530 find_dwo_cutu.signature = signature;
12531 dwo_cutu
12532 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12533 &find_dwo_cutu);
12534 }
12535 else if (!is_debug_types && dwo_file->cus)
12536 {
12537 struct dwo_unit find_dwo_cutu;
12538
12539 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12540 find_dwo_cutu.signature = signature;
12541 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12542 &find_dwo_cutu);
12543 }
12544
12545 if (dwo_cutu != NULL)
12546 {
12547 if (dwarf_read_debug)
12548 {
12549 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12550 kind, dwo_name, hex_string (signature),
12551 host_address_to_string (dwo_cutu));
12552 }
12553 return dwo_cutu;
12554 }
12555 }
12556 }
12557
12558 /* We didn't find it. This could mean a dwo_id mismatch, or
12559 someone deleted the DWO/DWP file, or the search path isn't set up
12560 correctly to find the file. */
12561
12562 if (dwarf_read_debug)
12563 {
12564 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12565 kind, dwo_name, hex_string (signature));
12566 }
12567
12568 /* This is a warning and not a complaint because it can be caused by
12569 pilot error (e.g., user accidentally deleting the DWO). */
12570 {
12571 /* Print the name of the DWP file if we looked there, helps the user
12572 better diagnose the problem. */
12573 std::string dwp_text;
12574
12575 if (dwp_file != NULL)
12576 dwp_text = string_printf (" [in DWP file %s]",
12577 lbasename (dwp_file->name));
12578
12579 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12580 " [in module %s]"),
12581 kind, dwo_name, hex_string (signature),
12582 dwp_text.c_str (),
12583 this_unit->is_debug_types ? "TU" : "CU",
12584 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12585 }
12586 return NULL;
12587 }
12588
12589 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12590 See lookup_dwo_cutu_unit for details. */
12591
12592 static struct dwo_unit *
12593 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12594 const char *dwo_name, const char *comp_dir,
12595 ULONGEST signature)
12596 {
12597 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12598 }
12599
12600 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12601 See lookup_dwo_cutu_unit for details. */
12602
12603 static struct dwo_unit *
12604 lookup_dwo_type_unit (struct signatured_type *this_tu,
12605 const char *dwo_name, const char *comp_dir)
12606 {
12607 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12608 }
12609
12610 /* Traversal function for queue_and_load_all_dwo_tus. */
12611
12612 static int
12613 queue_and_load_dwo_tu (void **slot, void *info)
12614 {
12615 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12616 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12617 ULONGEST signature = dwo_unit->signature;
12618 struct signatured_type *sig_type =
12619 lookup_dwo_signatured_type (per_cu->cu, signature);
12620
12621 if (sig_type != NULL)
12622 {
12623 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12624
12625 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12626 a real dependency of PER_CU on SIG_TYPE. That is detected later
12627 while processing PER_CU. */
12628 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12629 load_full_type_unit (sig_cu);
12630 per_cu->imported_symtabs_push (sig_cu);
12631 }
12632
12633 return 1;
12634 }
12635
12636 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12637 The DWO may have the only definition of the type, though it may not be
12638 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12639 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12640
12641 static void
12642 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12643 {
12644 struct dwo_unit *dwo_unit;
12645 struct dwo_file *dwo_file;
12646
12647 gdb_assert (!per_cu->is_debug_types);
12648 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12649 gdb_assert (per_cu->cu != NULL);
12650
12651 dwo_unit = per_cu->cu->dwo_unit;
12652 gdb_assert (dwo_unit != NULL);
12653
12654 dwo_file = dwo_unit->dwo_file;
12655 if (dwo_file->tus != NULL)
12656 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12657 per_cu);
12658 }
12659
12660 /* Read in various DIEs. */
12661
12662 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12663 Inherit only the children of the DW_AT_abstract_origin DIE not being
12664 already referenced by DW_AT_abstract_origin from the children of the
12665 current DIE. */
12666
12667 static void
12668 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12669 {
12670 struct die_info *child_die;
12671 sect_offset *offsetp;
12672 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12673 struct die_info *origin_die;
12674 /* Iterator of the ORIGIN_DIE children. */
12675 struct die_info *origin_child_die;
12676 struct attribute *attr;
12677 struct dwarf2_cu *origin_cu;
12678 struct pending **origin_previous_list_in_scope;
12679
12680 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12681 if (!attr)
12682 return;
12683
12684 /* Note that following die references may follow to a die in a
12685 different cu. */
12686
12687 origin_cu = cu;
12688 origin_die = follow_die_ref (die, attr, &origin_cu);
12689
12690 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12691 symbols in. */
12692 origin_previous_list_in_scope = origin_cu->list_in_scope;
12693 origin_cu->list_in_scope = cu->list_in_scope;
12694
12695 if (die->tag != origin_die->tag
12696 && !(die->tag == DW_TAG_inlined_subroutine
12697 && origin_die->tag == DW_TAG_subprogram))
12698 complaint (_("DIE %s and its abstract origin %s have different tags"),
12699 sect_offset_str (die->sect_off),
12700 sect_offset_str (origin_die->sect_off));
12701
12702 std::vector<sect_offset> offsets;
12703
12704 for (child_die = die->child;
12705 child_die && child_die->tag;
12706 child_die = sibling_die (child_die))
12707 {
12708 struct die_info *child_origin_die;
12709 struct dwarf2_cu *child_origin_cu;
12710
12711 /* We are trying to process concrete instance entries:
12712 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12713 it's not relevant to our analysis here. i.e. detecting DIEs that are
12714 present in the abstract instance but not referenced in the concrete
12715 one. */
12716 if (child_die->tag == DW_TAG_call_site
12717 || child_die->tag == DW_TAG_GNU_call_site)
12718 continue;
12719
12720 /* For each CHILD_DIE, find the corresponding child of
12721 ORIGIN_DIE. If there is more than one layer of
12722 DW_AT_abstract_origin, follow them all; there shouldn't be,
12723 but GCC versions at least through 4.4 generate this (GCC PR
12724 40573). */
12725 child_origin_die = child_die;
12726 child_origin_cu = cu;
12727 while (1)
12728 {
12729 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12730 child_origin_cu);
12731 if (attr == NULL)
12732 break;
12733 child_origin_die = follow_die_ref (child_origin_die, attr,
12734 &child_origin_cu);
12735 }
12736
12737 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12738 counterpart may exist. */
12739 if (child_origin_die != child_die)
12740 {
12741 if (child_die->tag != child_origin_die->tag
12742 && !(child_die->tag == DW_TAG_inlined_subroutine
12743 && child_origin_die->tag == DW_TAG_subprogram))
12744 complaint (_("Child DIE %s and its abstract origin %s have "
12745 "different tags"),
12746 sect_offset_str (child_die->sect_off),
12747 sect_offset_str (child_origin_die->sect_off));
12748 if (child_origin_die->parent != origin_die)
12749 complaint (_("Child DIE %s and its abstract origin %s have "
12750 "different parents"),
12751 sect_offset_str (child_die->sect_off),
12752 sect_offset_str (child_origin_die->sect_off));
12753 else
12754 offsets.push_back (child_origin_die->sect_off);
12755 }
12756 }
12757 std::sort (offsets.begin (), offsets.end ());
12758 sect_offset *offsets_end = offsets.data () + offsets.size ();
12759 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12760 if (offsetp[-1] == *offsetp)
12761 complaint (_("Multiple children of DIE %s refer "
12762 "to DIE %s as their abstract origin"),
12763 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12764
12765 offsetp = offsets.data ();
12766 origin_child_die = origin_die->child;
12767 while (origin_child_die && origin_child_die->tag)
12768 {
12769 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12770 while (offsetp < offsets_end
12771 && *offsetp < origin_child_die->sect_off)
12772 offsetp++;
12773 if (offsetp >= offsets_end
12774 || *offsetp > origin_child_die->sect_off)
12775 {
12776 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12777 Check whether we're already processing ORIGIN_CHILD_DIE.
12778 This can happen with mutually referenced abstract_origins.
12779 PR 16581. */
12780 if (!origin_child_die->in_process)
12781 process_die (origin_child_die, origin_cu);
12782 }
12783 origin_child_die = sibling_die (origin_child_die);
12784 }
12785 origin_cu->list_in_scope = origin_previous_list_in_scope;
12786
12787 if (cu != origin_cu)
12788 compute_delayed_physnames (origin_cu);
12789 }
12790
12791 static void
12792 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12793 {
12794 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12795 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12796 struct context_stack *newobj;
12797 CORE_ADDR lowpc;
12798 CORE_ADDR highpc;
12799 struct die_info *child_die;
12800 struct attribute *attr, *call_line, *call_file;
12801 const char *name;
12802 CORE_ADDR baseaddr;
12803 struct block *block;
12804 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12805 std::vector<struct symbol *> template_args;
12806 struct template_symbol *templ_func = NULL;
12807
12808 if (inlined_func)
12809 {
12810 /* If we do not have call site information, we can't show the
12811 caller of this inlined function. That's too confusing, so
12812 only use the scope for local variables. */
12813 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12814 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12815 if (call_line == NULL || call_file == NULL)
12816 {
12817 read_lexical_block_scope (die, cu);
12818 return;
12819 }
12820 }
12821
12822 baseaddr = objfile->text_section_offset ();
12823
12824 name = dwarf2_name (die, cu);
12825
12826 /* Ignore functions with missing or empty names. These are actually
12827 illegal according to the DWARF standard. */
12828 if (name == NULL)
12829 {
12830 complaint (_("missing name for subprogram DIE at %s"),
12831 sect_offset_str (die->sect_off));
12832 return;
12833 }
12834
12835 /* Ignore functions with missing or invalid low and high pc attributes. */
12836 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12837 <= PC_BOUNDS_INVALID)
12838 {
12839 attr = dwarf2_attr (die, DW_AT_external, cu);
12840 if (!attr || !DW_UNSND (attr))
12841 complaint (_("cannot get low and high bounds "
12842 "for subprogram DIE at %s"),
12843 sect_offset_str (die->sect_off));
12844 return;
12845 }
12846
12847 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12848 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12849
12850 /* If we have any template arguments, then we must allocate a
12851 different sort of symbol. */
12852 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12853 {
12854 if (child_die->tag == DW_TAG_template_type_param
12855 || child_die->tag == DW_TAG_template_value_param)
12856 {
12857 templ_func = allocate_template_symbol (objfile);
12858 templ_func->subclass = SYMBOL_TEMPLATE;
12859 break;
12860 }
12861 }
12862
12863 newobj = cu->get_builder ()->push_context (0, lowpc);
12864 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12865 (struct symbol *) templ_func);
12866
12867 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12868 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12869 cu->language);
12870
12871 /* If there is a location expression for DW_AT_frame_base, record
12872 it. */
12873 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12874 if (attr != nullptr)
12875 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12876
12877 /* If there is a location for the static link, record it. */
12878 newobj->static_link = NULL;
12879 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12880 if (attr != nullptr)
12881 {
12882 newobj->static_link
12883 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12884 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12885 cu->per_cu->addr_type ());
12886 }
12887
12888 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12889
12890 if (die->child != NULL)
12891 {
12892 child_die = die->child;
12893 while (child_die && child_die->tag)
12894 {
12895 if (child_die->tag == DW_TAG_template_type_param
12896 || child_die->tag == DW_TAG_template_value_param)
12897 {
12898 struct symbol *arg = new_symbol (child_die, NULL, cu);
12899
12900 if (arg != NULL)
12901 template_args.push_back (arg);
12902 }
12903 else
12904 process_die (child_die, cu);
12905 child_die = sibling_die (child_die);
12906 }
12907 }
12908
12909 inherit_abstract_dies (die, cu);
12910
12911 /* If we have a DW_AT_specification, we might need to import using
12912 directives from the context of the specification DIE. See the
12913 comment in determine_prefix. */
12914 if (cu->language == language_cplus
12915 && dwarf2_attr (die, DW_AT_specification, cu))
12916 {
12917 struct dwarf2_cu *spec_cu = cu;
12918 struct die_info *spec_die = die_specification (die, &spec_cu);
12919
12920 while (spec_die)
12921 {
12922 child_die = spec_die->child;
12923 while (child_die && child_die->tag)
12924 {
12925 if (child_die->tag == DW_TAG_imported_module)
12926 process_die (child_die, spec_cu);
12927 child_die = sibling_die (child_die);
12928 }
12929
12930 /* In some cases, GCC generates specification DIEs that
12931 themselves contain DW_AT_specification attributes. */
12932 spec_die = die_specification (spec_die, &spec_cu);
12933 }
12934 }
12935
12936 struct context_stack cstk = cu->get_builder ()->pop_context ();
12937 /* Make a block for the local symbols within. */
12938 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12939 cstk.static_link, lowpc, highpc);
12940
12941 /* For C++, set the block's scope. */
12942 if ((cu->language == language_cplus
12943 || cu->language == language_fortran
12944 || cu->language == language_d
12945 || cu->language == language_rust)
12946 && cu->processing_has_namespace_info)
12947 block_set_scope (block, determine_prefix (die, cu),
12948 &objfile->objfile_obstack);
12949
12950 /* If we have address ranges, record them. */
12951 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12952
12953 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12954
12955 /* Attach template arguments to function. */
12956 if (!template_args.empty ())
12957 {
12958 gdb_assert (templ_func != NULL);
12959
12960 templ_func->n_template_arguments = template_args.size ();
12961 templ_func->template_arguments
12962 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12963 templ_func->n_template_arguments);
12964 memcpy (templ_func->template_arguments,
12965 template_args.data (),
12966 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12967
12968 /* Make sure that the symtab is set on the new symbols. Even
12969 though they don't appear in this symtab directly, other parts
12970 of gdb assume that symbols do, and this is reasonably
12971 true. */
12972 for (symbol *sym : template_args)
12973 symbol_set_symtab (sym, symbol_symtab (templ_func));
12974 }
12975
12976 /* In C++, we can have functions nested inside functions (e.g., when
12977 a function declares a class that has methods). This means that
12978 when we finish processing a function scope, we may need to go
12979 back to building a containing block's symbol lists. */
12980 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12981 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12982
12983 /* If we've finished processing a top-level function, subsequent
12984 symbols go in the file symbol list. */
12985 if (cu->get_builder ()->outermost_context_p ())
12986 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12987 }
12988
12989 /* Process all the DIES contained within a lexical block scope. Start
12990 a new scope, process the dies, and then close the scope. */
12991
12992 static void
12993 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12994 {
12995 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12996 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12997 CORE_ADDR lowpc, highpc;
12998 struct die_info *child_die;
12999 CORE_ADDR baseaddr;
13000
13001 baseaddr = objfile->text_section_offset ();
13002
13003 /* Ignore blocks with missing or invalid low and high pc attributes. */
13004 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13005 as multiple lexical blocks? Handling children in a sane way would
13006 be nasty. Might be easier to properly extend generic blocks to
13007 describe ranges. */
13008 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13009 {
13010 case PC_BOUNDS_NOT_PRESENT:
13011 /* DW_TAG_lexical_block has no attributes, process its children as if
13012 there was no wrapping by that DW_TAG_lexical_block.
13013 GCC does no longer produces such DWARF since GCC r224161. */
13014 for (child_die = die->child;
13015 child_die != NULL && child_die->tag;
13016 child_die = sibling_die (child_die))
13017 process_die (child_die, cu);
13018 return;
13019 case PC_BOUNDS_INVALID:
13020 return;
13021 }
13022 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13023 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13024
13025 cu->get_builder ()->push_context (0, lowpc);
13026 if (die->child != NULL)
13027 {
13028 child_die = die->child;
13029 while (child_die && child_die->tag)
13030 {
13031 process_die (child_die, cu);
13032 child_die = sibling_die (child_die);
13033 }
13034 }
13035 inherit_abstract_dies (die, cu);
13036 struct context_stack cstk = cu->get_builder ()->pop_context ();
13037
13038 if (*cu->get_builder ()->get_local_symbols () != NULL
13039 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13040 {
13041 struct block *block
13042 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13043 cstk.start_addr, highpc);
13044
13045 /* Note that recording ranges after traversing children, as we
13046 do here, means that recording a parent's ranges entails
13047 walking across all its children's ranges as they appear in
13048 the address map, which is quadratic behavior.
13049
13050 It would be nicer to record the parent's ranges before
13051 traversing its children, simply overriding whatever you find
13052 there. But since we don't even decide whether to create a
13053 block until after we've traversed its children, that's hard
13054 to do. */
13055 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13056 }
13057 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13058 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13059 }
13060
13061 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13062
13063 static void
13064 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13065 {
13066 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13067 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13068 CORE_ADDR pc, baseaddr;
13069 struct attribute *attr;
13070 struct call_site *call_site, call_site_local;
13071 void **slot;
13072 int nparams;
13073 struct die_info *child_die;
13074
13075 baseaddr = objfile->text_section_offset ();
13076
13077 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13078 if (attr == NULL)
13079 {
13080 /* This was a pre-DWARF-5 GNU extension alias
13081 for DW_AT_call_return_pc. */
13082 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13083 }
13084 if (!attr)
13085 {
13086 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13087 "DIE %s [in module %s]"),
13088 sect_offset_str (die->sect_off), objfile_name (objfile));
13089 return;
13090 }
13091 pc = attr->value_as_address () + baseaddr;
13092 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13093
13094 if (cu->call_site_htab == NULL)
13095 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13096 NULL, &objfile->objfile_obstack,
13097 hashtab_obstack_allocate, NULL);
13098 call_site_local.pc = pc;
13099 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13100 if (*slot != NULL)
13101 {
13102 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13103 "DIE %s [in module %s]"),
13104 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13105 objfile_name (objfile));
13106 return;
13107 }
13108
13109 /* Count parameters at the caller. */
13110
13111 nparams = 0;
13112 for (child_die = die->child; child_die && child_die->tag;
13113 child_die = sibling_die (child_die))
13114 {
13115 if (child_die->tag != DW_TAG_call_site_parameter
13116 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13117 {
13118 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13119 "DW_TAG_call_site child DIE %s [in module %s]"),
13120 child_die->tag, sect_offset_str (child_die->sect_off),
13121 objfile_name (objfile));
13122 continue;
13123 }
13124
13125 nparams++;
13126 }
13127
13128 call_site
13129 = ((struct call_site *)
13130 obstack_alloc (&objfile->objfile_obstack,
13131 sizeof (*call_site)
13132 + (sizeof (*call_site->parameter) * (nparams - 1))));
13133 *slot = call_site;
13134 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13135 call_site->pc = pc;
13136
13137 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13138 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13139 {
13140 struct die_info *func_die;
13141
13142 /* Skip also over DW_TAG_inlined_subroutine. */
13143 for (func_die = die->parent;
13144 func_die && func_die->tag != DW_TAG_subprogram
13145 && func_die->tag != DW_TAG_subroutine_type;
13146 func_die = func_die->parent);
13147
13148 /* DW_AT_call_all_calls is a superset
13149 of DW_AT_call_all_tail_calls. */
13150 if (func_die
13151 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13152 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13153 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13154 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13155 {
13156 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13157 not complete. But keep CALL_SITE for look ups via call_site_htab,
13158 both the initial caller containing the real return address PC and
13159 the final callee containing the current PC of a chain of tail
13160 calls do not need to have the tail call list complete. But any
13161 function candidate for a virtual tail call frame searched via
13162 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13163 determined unambiguously. */
13164 }
13165 else
13166 {
13167 struct type *func_type = NULL;
13168
13169 if (func_die)
13170 func_type = get_die_type (func_die, cu);
13171 if (func_type != NULL)
13172 {
13173 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13174
13175 /* Enlist this call site to the function. */
13176 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13177 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13178 }
13179 else
13180 complaint (_("Cannot find function owning DW_TAG_call_site "
13181 "DIE %s [in module %s]"),
13182 sect_offset_str (die->sect_off), objfile_name (objfile));
13183 }
13184 }
13185
13186 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13187 if (attr == NULL)
13188 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13189 if (attr == NULL)
13190 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13191 if (attr == NULL)
13192 {
13193 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13194 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13195 }
13196 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13197 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13198 /* Keep NULL DWARF_BLOCK. */;
13199 else if (attr->form_is_block ())
13200 {
13201 struct dwarf2_locexpr_baton *dlbaton;
13202
13203 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13204 dlbaton->data = DW_BLOCK (attr)->data;
13205 dlbaton->size = DW_BLOCK (attr)->size;
13206 dlbaton->per_cu = cu->per_cu;
13207
13208 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13209 }
13210 else if (attr->form_is_ref ())
13211 {
13212 struct dwarf2_cu *target_cu = cu;
13213 struct die_info *target_die;
13214
13215 target_die = follow_die_ref (die, attr, &target_cu);
13216 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13217 if (die_is_declaration (target_die, target_cu))
13218 {
13219 const char *target_physname;
13220
13221 /* Prefer the mangled name; otherwise compute the demangled one. */
13222 target_physname = dw2_linkage_name (target_die, target_cu);
13223 if (target_physname == NULL)
13224 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13225 if (target_physname == NULL)
13226 complaint (_("DW_AT_call_target target DIE has invalid "
13227 "physname, for referencing DIE %s [in module %s]"),
13228 sect_offset_str (die->sect_off), objfile_name (objfile));
13229 else
13230 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13231 }
13232 else
13233 {
13234 CORE_ADDR lowpc;
13235
13236 /* DW_AT_entry_pc should be preferred. */
13237 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13238 <= PC_BOUNDS_INVALID)
13239 complaint (_("DW_AT_call_target target DIE has invalid "
13240 "low pc, for referencing DIE %s [in module %s]"),
13241 sect_offset_str (die->sect_off), objfile_name (objfile));
13242 else
13243 {
13244 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13245 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13246 }
13247 }
13248 }
13249 else
13250 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13251 "block nor reference, for DIE %s [in module %s]"),
13252 sect_offset_str (die->sect_off), objfile_name (objfile));
13253
13254 call_site->per_cu = cu->per_cu;
13255
13256 for (child_die = die->child;
13257 child_die && child_die->tag;
13258 child_die = sibling_die (child_die))
13259 {
13260 struct call_site_parameter *parameter;
13261 struct attribute *loc, *origin;
13262
13263 if (child_die->tag != DW_TAG_call_site_parameter
13264 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13265 {
13266 /* Already printed the complaint above. */
13267 continue;
13268 }
13269
13270 gdb_assert (call_site->parameter_count < nparams);
13271 parameter = &call_site->parameter[call_site->parameter_count];
13272
13273 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13274 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13275 register is contained in DW_AT_call_value. */
13276
13277 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13278 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13279 if (origin == NULL)
13280 {
13281 /* This was a pre-DWARF-5 GNU extension alias
13282 for DW_AT_call_parameter. */
13283 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13284 }
13285 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13286 {
13287 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13288
13289 sect_offset sect_off
13290 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13291 if (!cu->header.offset_in_cu_p (sect_off))
13292 {
13293 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13294 binding can be done only inside one CU. Such referenced DIE
13295 therefore cannot be even moved to DW_TAG_partial_unit. */
13296 complaint (_("DW_AT_call_parameter offset is not in CU for "
13297 "DW_TAG_call_site child DIE %s [in module %s]"),
13298 sect_offset_str (child_die->sect_off),
13299 objfile_name (objfile));
13300 continue;
13301 }
13302 parameter->u.param_cu_off
13303 = (cu_offset) (sect_off - cu->header.sect_off);
13304 }
13305 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13306 {
13307 complaint (_("No DW_FORM_block* DW_AT_location for "
13308 "DW_TAG_call_site child DIE %s [in module %s]"),
13309 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13310 continue;
13311 }
13312 else
13313 {
13314 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13315 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13316 if (parameter->u.dwarf_reg != -1)
13317 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13318 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13319 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13320 &parameter->u.fb_offset))
13321 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13322 else
13323 {
13324 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13325 "for DW_FORM_block* DW_AT_location is supported for "
13326 "DW_TAG_call_site child DIE %s "
13327 "[in module %s]"),
13328 sect_offset_str (child_die->sect_off),
13329 objfile_name (objfile));
13330 continue;
13331 }
13332 }
13333
13334 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13335 if (attr == NULL)
13336 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13337 if (attr == NULL || !attr->form_is_block ())
13338 {
13339 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13340 "DW_TAG_call_site child DIE %s [in module %s]"),
13341 sect_offset_str (child_die->sect_off),
13342 objfile_name (objfile));
13343 continue;
13344 }
13345 parameter->value = DW_BLOCK (attr)->data;
13346 parameter->value_size = DW_BLOCK (attr)->size;
13347
13348 /* Parameters are not pre-cleared by memset above. */
13349 parameter->data_value = NULL;
13350 parameter->data_value_size = 0;
13351 call_site->parameter_count++;
13352
13353 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13354 if (attr == NULL)
13355 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13356 if (attr != nullptr)
13357 {
13358 if (!attr->form_is_block ())
13359 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13360 "DW_TAG_call_site child DIE %s [in module %s]"),
13361 sect_offset_str (child_die->sect_off),
13362 objfile_name (objfile));
13363 else
13364 {
13365 parameter->data_value = DW_BLOCK (attr)->data;
13366 parameter->data_value_size = DW_BLOCK (attr)->size;
13367 }
13368 }
13369 }
13370 }
13371
13372 /* Helper function for read_variable. If DIE represents a virtual
13373 table, then return the type of the concrete object that is
13374 associated with the virtual table. Otherwise, return NULL. */
13375
13376 static struct type *
13377 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13378 {
13379 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13380 if (attr == NULL)
13381 return NULL;
13382
13383 /* Find the type DIE. */
13384 struct die_info *type_die = NULL;
13385 struct dwarf2_cu *type_cu = cu;
13386
13387 if (attr->form_is_ref ())
13388 type_die = follow_die_ref (die, attr, &type_cu);
13389 if (type_die == NULL)
13390 return NULL;
13391
13392 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13393 return NULL;
13394 return die_containing_type (type_die, type_cu);
13395 }
13396
13397 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13398
13399 static void
13400 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13401 {
13402 struct rust_vtable_symbol *storage = NULL;
13403
13404 if (cu->language == language_rust)
13405 {
13406 struct type *containing_type = rust_containing_type (die, cu);
13407
13408 if (containing_type != NULL)
13409 {
13410 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13411
13412 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13413 initialize_objfile_symbol (storage);
13414 storage->concrete_type = containing_type;
13415 storage->subclass = SYMBOL_RUST_VTABLE;
13416 }
13417 }
13418
13419 struct symbol *res = new_symbol (die, NULL, cu, storage);
13420 struct attribute *abstract_origin
13421 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13422 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13423 if (res == NULL && loc && abstract_origin)
13424 {
13425 /* We have a variable without a name, but with a location and an abstract
13426 origin. This may be a concrete instance of an abstract variable
13427 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13428 later. */
13429 struct dwarf2_cu *origin_cu = cu;
13430 struct die_info *origin_die
13431 = follow_die_ref (die, abstract_origin, &origin_cu);
13432 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13433 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13434 }
13435 }
13436
13437 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13438 reading .debug_rnglists.
13439 Callback's type should be:
13440 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13441 Return true if the attributes are present and valid, otherwise,
13442 return false. */
13443
13444 template <typename Callback>
13445 static bool
13446 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13447 Callback &&callback)
13448 {
13449 struct dwarf2_per_objfile *dwarf2_per_objfile
13450 = cu->per_cu->dwarf2_per_objfile;
13451 struct objfile *objfile = dwarf2_per_objfile->objfile;
13452 bfd *obfd = objfile->obfd;
13453 /* Base address selection entry. */
13454 CORE_ADDR base;
13455 int found_base;
13456 const gdb_byte *buffer;
13457 CORE_ADDR baseaddr;
13458 bool overflow = false;
13459
13460 found_base = cu->base_known;
13461 base = cu->base_address;
13462
13463 dwarf2_per_objfile->rnglists.read (objfile);
13464 if (offset >= dwarf2_per_objfile->rnglists.size)
13465 {
13466 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13467 offset);
13468 return false;
13469 }
13470 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13471
13472 baseaddr = objfile->text_section_offset ();
13473
13474 while (1)
13475 {
13476 /* Initialize it due to a false compiler warning. */
13477 CORE_ADDR range_beginning = 0, range_end = 0;
13478 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13479 + dwarf2_per_objfile->rnglists.size);
13480 unsigned int bytes_read;
13481
13482 if (buffer == buf_end)
13483 {
13484 overflow = true;
13485 break;
13486 }
13487 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13488 switch (rlet)
13489 {
13490 case DW_RLE_end_of_list:
13491 break;
13492 case DW_RLE_base_address:
13493 if (buffer + cu->header.addr_size > buf_end)
13494 {
13495 overflow = true;
13496 break;
13497 }
13498 base = cu->header.read_address (obfd, buffer, &bytes_read);
13499 found_base = 1;
13500 buffer += bytes_read;
13501 break;
13502 case DW_RLE_start_length:
13503 if (buffer + cu->header.addr_size > buf_end)
13504 {
13505 overflow = true;
13506 break;
13507 }
13508 range_beginning = cu->header.read_address (obfd, buffer,
13509 &bytes_read);
13510 buffer += bytes_read;
13511 range_end = (range_beginning
13512 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13513 buffer += bytes_read;
13514 if (buffer > buf_end)
13515 {
13516 overflow = true;
13517 break;
13518 }
13519 break;
13520 case DW_RLE_offset_pair:
13521 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13522 buffer += bytes_read;
13523 if (buffer > buf_end)
13524 {
13525 overflow = true;
13526 break;
13527 }
13528 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13529 buffer += bytes_read;
13530 if (buffer > buf_end)
13531 {
13532 overflow = true;
13533 break;
13534 }
13535 break;
13536 case DW_RLE_start_end:
13537 if (buffer + 2 * cu->header.addr_size > buf_end)
13538 {
13539 overflow = true;
13540 break;
13541 }
13542 range_beginning = cu->header.read_address (obfd, buffer,
13543 &bytes_read);
13544 buffer += bytes_read;
13545 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13546 buffer += bytes_read;
13547 break;
13548 default:
13549 complaint (_("Invalid .debug_rnglists data (no base address)"));
13550 return false;
13551 }
13552 if (rlet == DW_RLE_end_of_list || overflow)
13553 break;
13554 if (rlet == DW_RLE_base_address)
13555 continue;
13556
13557 if (!found_base)
13558 {
13559 /* We have no valid base address for the ranges
13560 data. */
13561 complaint (_("Invalid .debug_rnglists data (no base address)"));
13562 return false;
13563 }
13564
13565 if (range_beginning > range_end)
13566 {
13567 /* Inverted range entries are invalid. */
13568 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13569 return false;
13570 }
13571
13572 /* Empty range entries have no effect. */
13573 if (range_beginning == range_end)
13574 continue;
13575
13576 range_beginning += base;
13577 range_end += base;
13578
13579 /* A not-uncommon case of bad debug info.
13580 Don't pollute the addrmap with bad data. */
13581 if (range_beginning + baseaddr == 0
13582 && !dwarf2_per_objfile->has_section_at_zero)
13583 {
13584 complaint (_(".debug_rnglists entry has start address of zero"
13585 " [in module %s]"), objfile_name (objfile));
13586 continue;
13587 }
13588
13589 callback (range_beginning, range_end);
13590 }
13591
13592 if (overflow)
13593 {
13594 complaint (_("Offset %d is not terminated "
13595 "for DW_AT_ranges attribute"),
13596 offset);
13597 return false;
13598 }
13599
13600 return true;
13601 }
13602
13603 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13604 Callback's type should be:
13605 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13606 Return 1 if the attributes are present and valid, otherwise, return 0. */
13607
13608 template <typename Callback>
13609 static int
13610 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13611 Callback &&callback)
13612 {
13613 struct dwarf2_per_objfile *dwarf2_per_objfile
13614 = cu->per_cu->dwarf2_per_objfile;
13615 struct objfile *objfile = dwarf2_per_objfile->objfile;
13616 struct comp_unit_head *cu_header = &cu->header;
13617 bfd *obfd = objfile->obfd;
13618 unsigned int addr_size = cu_header->addr_size;
13619 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13620 /* Base address selection entry. */
13621 CORE_ADDR base;
13622 int found_base;
13623 unsigned int dummy;
13624 const gdb_byte *buffer;
13625 CORE_ADDR baseaddr;
13626
13627 if (cu_header->version >= 5)
13628 return dwarf2_rnglists_process (offset, cu, callback);
13629
13630 found_base = cu->base_known;
13631 base = cu->base_address;
13632
13633 dwarf2_per_objfile->ranges.read (objfile);
13634 if (offset >= dwarf2_per_objfile->ranges.size)
13635 {
13636 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13637 offset);
13638 return 0;
13639 }
13640 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13641
13642 baseaddr = objfile->text_section_offset ();
13643
13644 while (1)
13645 {
13646 CORE_ADDR range_beginning, range_end;
13647
13648 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13649 buffer += addr_size;
13650 range_end = cu->header.read_address (obfd, buffer, &dummy);
13651 buffer += addr_size;
13652 offset += 2 * addr_size;
13653
13654 /* An end of list marker is a pair of zero addresses. */
13655 if (range_beginning == 0 && range_end == 0)
13656 /* Found the end of list entry. */
13657 break;
13658
13659 /* Each base address selection entry is a pair of 2 values.
13660 The first is the largest possible address, the second is
13661 the base address. Check for a base address here. */
13662 if ((range_beginning & mask) == mask)
13663 {
13664 /* If we found the largest possible address, then we already
13665 have the base address in range_end. */
13666 base = range_end;
13667 found_base = 1;
13668 continue;
13669 }
13670
13671 if (!found_base)
13672 {
13673 /* We have no valid base address for the ranges
13674 data. */
13675 complaint (_("Invalid .debug_ranges data (no base address)"));
13676 return 0;
13677 }
13678
13679 if (range_beginning > range_end)
13680 {
13681 /* Inverted range entries are invalid. */
13682 complaint (_("Invalid .debug_ranges data (inverted range)"));
13683 return 0;
13684 }
13685
13686 /* Empty range entries have no effect. */
13687 if (range_beginning == range_end)
13688 continue;
13689
13690 range_beginning += base;
13691 range_end += base;
13692
13693 /* A not-uncommon case of bad debug info.
13694 Don't pollute the addrmap with bad data. */
13695 if (range_beginning + baseaddr == 0
13696 && !dwarf2_per_objfile->has_section_at_zero)
13697 {
13698 complaint (_(".debug_ranges entry has start address of zero"
13699 " [in module %s]"), objfile_name (objfile));
13700 continue;
13701 }
13702
13703 callback (range_beginning, range_end);
13704 }
13705
13706 return 1;
13707 }
13708
13709 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13710 Return 1 if the attributes are present and valid, otherwise, return 0.
13711 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13712
13713 static int
13714 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13715 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13716 dwarf2_psymtab *ranges_pst)
13717 {
13718 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13719 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13720 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13721 int low_set = 0;
13722 CORE_ADDR low = 0;
13723 CORE_ADDR high = 0;
13724 int retval;
13725
13726 retval = dwarf2_ranges_process (offset, cu,
13727 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13728 {
13729 if (ranges_pst != NULL)
13730 {
13731 CORE_ADDR lowpc;
13732 CORE_ADDR highpc;
13733
13734 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13735 range_beginning + baseaddr)
13736 - baseaddr);
13737 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13738 range_end + baseaddr)
13739 - baseaddr);
13740 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13741 lowpc, highpc - 1, ranges_pst);
13742 }
13743
13744 /* FIXME: This is recording everything as a low-high
13745 segment of consecutive addresses. We should have a
13746 data structure for discontiguous block ranges
13747 instead. */
13748 if (! low_set)
13749 {
13750 low = range_beginning;
13751 high = range_end;
13752 low_set = 1;
13753 }
13754 else
13755 {
13756 if (range_beginning < low)
13757 low = range_beginning;
13758 if (range_end > high)
13759 high = range_end;
13760 }
13761 });
13762 if (!retval)
13763 return 0;
13764
13765 if (! low_set)
13766 /* If the first entry is an end-of-list marker, the range
13767 describes an empty scope, i.e. no instructions. */
13768 return 0;
13769
13770 if (low_return)
13771 *low_return = low;
13772 if (high_return)
13773 *high_return = high;
13774 return 1;
13775 }
13776
13777 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13778 definition for the return value. *LOWPC and *HIGHPC are set iff
13779 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13780
13781 static enum pc_bounds_kind
13782 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13783 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13784 dwarf2_psymtab *pst)
13785 {
13786 struct dwarf2_per_objfile *dwarf2_per_objfile
13787 = cu->per_cu->dwarf2_per_objfile;
13788 struct attribute *attr;
13789 struct attribute *attr_high;
13790 CORE_ADDR low = 0;
13791 CORE_ADDR high = 0;
13792 enum pc_bounds_kind ret;
13793
13794 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13795 if (attr_high)
13796 {
13797 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13798 if (attr != nullptr)
13799 {
13800 low = attr->value_as_address ();
13801 high = attr_high->value_as_address ();
13802 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13803 high += low;
13804 }
13805 else
13806 /* Found high w/o low attribute. */
13807 return PC_BOUNDS_INVALID;
13808
13809 /* Found consecutive range of addresses. */
13810 ret = PC_BOUNDS_HIGH_LOW;
13811 }
13812 else
13813 {
13814 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13815 if (attr != NULL)
13816 {
13817 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13818 We take advantage of the fact that DW_AT_ranges does not appear
13819 in DW_TAG_compile_unit of DWO files. */
13820 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13821 unsigned int ranges_offset = (DW_UNSND (attr)
13822 + (need_ranges_base
13823 ? cu->ranges_base
13824 : 0));
13825
13826 /* Value of the DW_AT_ranges attribute is the offset in the
13827 .debug_ranges section. */
13828 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13829 return PC_BOUNDS_INVALID;
13830 /* Found discontinuous range of addresses. */
13831 ret = PC_BOUNDS_RANGES;
13832 }
13833 else
13834 return PC_BOUNDS_NOT_PRESENT;
13835 }
13836
13837 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13838 if (high <= low)
13839 return PC_BOUNDS_INVALID;
13840
13841 /* When using the GNU linker, .gnu.linkonce. sections are used to
13842 eliminate duplicate copies of functions and vtables and such.
13843 The linker will arbitrarily choose one and discard the others.
13844 The AT_*_pc values for such functions refer to local labels in
13845 these sections. If the section from that file was discarded, the
13846 labels are not in the output, so the relocs get a value of 0.
13847 If this is a discarded function, mark the pc bounds as invalid,
13848 so that GDB will ignore it. */
13849 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13850 return PC_BOUNDS_INVALID;
13851
13852 *lowpc = low;
13853 if (highpc)
13854 *highpc = high;
13855 return ret;
13856 }
13857
13858 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13859 its low and high PC addresses. Do nothing if these addresses could not
13860 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13861 and HIGHPC to the high address if greater than HIGHPC. */
13862
13863 static void
13864 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13865 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13866 struct dwarf2_cu *cu)
13867 {
13868 CORE_ADDR low, high;
13869 struct die_info *child = die->child;
13870
13871 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13872 {
13873 *lowpc = std::min (*lowpc, low);
13874 *highpc = std::max (*highpc, high);
13875 }
13876
13877 /* If the language does not allow nested subprograms (either inside
13878 subprograms or lexical blocks), we're done. */
13879 if (cu->language != language_ada)
13880 return;
13881
13882 /* Check all the children of the given DIE. If it contains nested
13883 subprograms, then check their pc bounds. Likewise, we need to
13884 check lexical blocks as well, as they may also contain subprogram
13885 definitions. */
13886 while (child && child->tag)
13887 {
13888 if (child->tag == DW_TAG_subprogram
13889 || child->tag == DW_TAG_lexical_block)
13890 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13891 child = sibling_die (child);
13892 }
13893 }
13894
13895 /* Get the low and high pc's represented by the scope DIE, and store
13896 them in *LOWPC and *HIGHPC. If the correct values can't be
13897 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13898
13899 static void
13900 get_scope_pc_bounds (struct die_info *die,
13901 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13902 struct dwarf2_cu *cu)
13903 {
13904 CORE_ADDR best_low = (CORE_ADDR) -1;
13905 CORE_ADDR best_high = (CORE_ADDR) 0;
13906 CORE_ADDR current_low, current_high;
13907
13908 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13909 >= PC_BOUNDS_RANGES)
13910 {
13911 best_low = current_low;
13912 best_high = current_high;
13913 }
13914 else
13915 {
13916 struct die_info *child = die->child;
13917
13918 while (child && child->tag)
13919 {
13920 switch (child->tag) {
13921 case DW_TAG_subprogram:
13922 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13923 break;
13924 case DW_TAG_namespace:
13925 case DW_TAG_module:
13926 /* FIXME: carlton/2004-01-16: Should we do this for
13927 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13928 that current GCC's always emit the DIEs corresponding
13929 to definitions of methods of classes as children of a
13930 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13931 the DIEs giving the declarations, which could be
13932 anywhere). But I don't see any reason why the
13933 standards says that they have to be there. */
13934 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13935
13936 if (current_low != ((CORE_ADDR) -1))
13937 {
13938 best_low = std::min (best_low, current_low);
13939 best_high = std::max (best_high, current_high);
13940 }
13941 break;
13942 default:
13943 /* Ignore. */
13944 break;
13945 }
13946
13947 child = sibling_die (child);
13948 }
13949 }
13950
13951 *lowpc = best_low;
13952 *highpc = best_high;
13953 }
13954
13955 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13956 in DIE. */
13957
13958 static void
13959 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13960 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13961 {
13962 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13963 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13964 struct attribute *attr;
13965 struct attribute *attr_high;
13966
13967 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13968 if (attr_high)
13969 {
13970 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13971 if (attr != nullptr)
13972 {
13973 CORE_ADDR low = attr->value_as_address ();
13974 CORE_ADDR high = attr_high->value_as_address ();
13975
13976 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13977 high += low;
13978
13979 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13980 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13981 cu->get_builder ()->record_block_range (block, low, high - 1);
13982 }
13983 }
13984
13985 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13986 if (attr != nullptr)
13987 {
13988 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13989 We take advantage of the fact that DW_AT_ranges does not appear
13990 in DW_TAG_compile_unit of DWO files. */
13991 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13992
13993 /* The value of the DW_AT_ranges attribute is the offset of the
13994 address range list in the .debug_ranges section. */
13995 unsigned long offset = (DW_UNSND (attr)
13996 + (need_ranges_base ? cu->ranges_base : 0));
13997
13998 std::vector<blockrange> blockvec;
13999 dwarf2_ranges_process (offset, cu,
14000 [&] (CORE_ADDR start, CORE_ADDR end)
14001 {
14002 start += baseaddr;
14003 end += baseaddr;
14004 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14005 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14006 cu->get_builder ()->record_block_range (block, start, end - 1);
14007 blockvec.emplace_back (start, end);
14008 });
14009
14010 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14011 }
14012 }
14013
14014 /* Check whether the producer field indicates either of GCC < 4.6, or the
14015 Intel C/C++ compiler, and cache the result in CU. */
14016
14017 static void
14018 check_producer (struct dwarf2_cu *cu)
14019 {
14020 int major, minor;
14021
14022 if (cu->producer == NULL)
14023 {
14024 /* For unknown compilers expect their behavior is DWARF version
14025 compliant.
14026
14027 GCC started to support .debug_types sections by -gdwarf-4 since
14028 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14029 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14030 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14031 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14032 }
14033 else if (producer_is_gcc (cu->producer, &major, &minor))
14034 {
14035 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14036 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14037 }
14038 else if (producer_is_icc (cu->producer, &major, &minor))
14039 {
14040 cu->producer_is_icc = true;
14041 cu->producer_is_icc_lt_14 = major < 14;
14042 }
14043 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14044 cu->producer_is_codewarrior = true;
14045 else
14046 {
14047 /* For other non-GCC compilers, expect their behavior is DWARF version
14048 compliant. */
14049 }
14050
14051 cu->checked_producer = true;
14052 }
14053
14054 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14055 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14056 during 4.6.0 experimental. */
14057
14058 static bool
14059 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14060 {
14061 if (!cu->checked_producer)
14062 check_producer (cu);
14063
14064 return cu->producer_is_gxx_lt_4_6;
14065 }
14066
14067
14068 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14069 with incorrect is_stmt attributes. */
14070
14071 static bool
14072 producer_is_codewarrior (struct dwarf2_cu *cu)
14073 {
14074 if (!cu->checked_producer)
14075 check_producer (cu);
14076
14077 return cu->producer_is_codewarrior;
14078 }
14079
14080 /* Return the default accessibility type if it is not overridden by
14081 DW_AT_accessibility. */
14082
14083 static enum dwarf_access_attribute
14084 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14085 {
14086 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14087 {
14088 /* The default DWARF 2 accessibility for members is public, the default
14089 accessibility for inheritance is private. */
14090
14091 if (die->tag != DW_TAG_inheritance)
14092 return DW_ACCESS_public;
14093 else
14094 return DW_ACCESS_private;
14095 }
14096 else
14097 {
14098 /* DWARF 3+ defines the default accessibility a different way. The same
14099 rules apply now for DW_TAG_inheritance as for the members and it only
14100 depends on the container kind. */
14101
14102 if (die->parent->tag == DW_TAG_class_type)
14103 return DW_ACCESS_private;
14104 else
14105 return DW_ACCESS_public;
14106 }
14107 }
14108
14109 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14110 offset. If the attribute was not found return 0, otherwise return
14111 1. If it was found but could not properly be handled, set *OFFSET
14112 to 0. */
14113
14114 static int
14115 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14116 LONGEST *offset)
14117 {
14118 struct attribute *attr;
14119
14120 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14121 if (attr != NULL)
14122 {
14123 *offset = 0;
14124
14125 /* Note that we do not check for a section offset first here.
14126 This is because DW_AT_data_member_location is new in DWARF 4,
14127 so if we see it, we can assume that a constant form is really
14128 a constant and not a section offset. */
14129 if (attr->form_is_constant ())
14130 *offset = dwarf2_get_attr_constant_value (attr, 0);
14131 else if (attr->form_is_section_offset ())
14132 dwarf2_complex_location_expr_complaint ();
14133 else if (attr->form_is_block ())
14134 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14135 else
14136 dwarf2_complex_location_expr_complaint ();
14137
14138 return 1;
14139 }
14140
14141 return 0;
14142 }
14143
14144 /* Add an aggregate field to the field list. */
14145
14146 static void
14147 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14148 struct dwarf2_cu *cu)
14149 {
14150 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14151 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14152 struct nextfield *new_field;
14153 struct attribute *attr;
14154 struct field *fp;
14155 const char *fieldname = "";
14156
14157 if (die->tag == DW_TAG_inheritance)
14158 {
14159 fip->baseclasses.emplace_back ();
14160 new_field = &fip->baseclasses.back ();
14161 }
14162 else
14163 {
14164 fip->fields.emplace_back ();
14165 new_field = &fip->fields.back ();
14166 }
14167
14168 fip->nfields++;
14169
14170 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14171 if (attr != nullptr)
14172 new_field->accessibility = DW_UNSND (attr);
14173 else
14174 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14175 if (new_field->accessibility != DW_ACCESS_public)
14176 fip->non_public_fields = 1;
14177
14178 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14179 if (attr != nullptr)
14180 new_field->virtuality = DW_UNSND (attr);
14181 else
14182 new_field->virtuality = DW_VIRTUALITY_none;
14183
14184 fp = &new_field->field;
14185
14186 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14187 {
14188 LONGEST offset;
14189
14190 /* Data member other than a C++ static data member. */
14191
14192 /* Get type of field. */
14193 fp->type = die_type (die, cu);
14194
14195 SET_FIELD_BITPOS (*fp, 0);
14196
14197 /* Get bit size of field (zero if none). */
14198 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14199 if (attr != nullptr)
14200 {
14201 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14202 }
14203 else
14204 {
14205 FIELD_BITSIZE (*fp) = 0;
14206 }
14207
14208 /* Get bit offset of field. */
14209 if (handle_data_member_location (die, cu, &offset))
14210 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14211 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14212 if (attr != nullptr)
14213 {
14214 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14215 {
14216 /* For big endian bits, the DW_AT_bit_offset gives the
14217 additional bit offset from the MSB of the containing
14218 anonymous object to the MSB of the field. We don't
14219 have to do anything special since we don't need to
14220 know the size of the anonymous object. */
14221 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14222 }
14223 else
14224 {
14225 /* For little endian bits, compute the bit offset to the
14226 MSB of the anonymous object, subtract off the number of
14227 bits from the MSB of the field to the MSB of the
14228 object, and then subtract off the number of bits of
14229 the field itself. The result is the bit offset of
14230 the LSB of the field. */
14231 int anonymous_size;
14232 int bit_offset = DW_UNSND (attr);
14233
14234 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14235 if (attr != nullptr)
14236 {
14237 /* The size of the anonymous object containing
14238 the bit field is explicit, so use the
14239 indicated size (in bytes). */
14240 anonymous_size = DW_UNSND (attr);
14241 }
14242 else
14243 {
14244 /* The size of the anonymous object containing
14245 the bit field must be inferred from the type
14246 attribute of the data member containing the
14247 bit field. */
14248 anonymous_size = TYPE_LENGTH (fp->type);
14249 }
14250 SET_FIELD_BITPOS (*fp,
14251 (FIELD_BITPOS (*fp)
14252 + anonymous_size * bits_per_byte
14253 - bit_offset - FIELD_BITSIZE (*fp)));
14254 }
14255 }
14256 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14257 if (attr != NULL)
14258 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14259 + dwarf2_get_attr_constant_value (attr, 0)));
14260
14261 /* Get name of field. */
14262 fieldname = dwarf2_name (die, cu);
14263 if (fieldname == NULL)
14264 fieldname = "";
14265
14266 /* The name is already allocated along with this objfile, so we don't
14267 need to duplicate it for the type. */
14268 fp->name = fieldname;
14269
14270 /* Change accessibility for artificial fields (e.g. virtual table
14271 pointer or virtual base class pointer) to private. */
14272 if (dwarf2_attr (die, DW_AT_artificial, cu))
14273 {
14274 FIELD_ARTIFICIAL (*fp) = 1;
14275 new_field->accessibility = DW_ACCESS_private;
14276 fip->non_public_fields = 1;
14277 }
14278 }
14279 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14280 {
14281 /* C++ static member. */
14282
14283 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14284 is a declaration, but all versions of G++ as of this writing
14285 (so through at least 3.2.1) incorrectly generate
14286 DW_TAG_variable tags. */
14287
14288 const char *physname;
14289
14290 /* Get name of field. */
14291 fieldname = dwarf2_name (die, cu);
14292 if (fieldname == NULL)
14293 return;
14294
14295 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14296 if (attr
14297 /* Only create a symbol if this is an external value.
14298 new_symbol checks this and puts the value in the global symbol
14299 table, which we want. If it is not external, new_symbol
14300 will try to put the value in cu->list_in_scope which is wrong. */
14301 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14302 {
14303 /* A static const member, not much different than an enum as far as
14304 we're concerned, except that we can support more types. */
14305 new_symbol (die, NULL, cu);
14306 }
14307
14308 /* Get physical name. */
14309 physname = dwarf2_physname (fieldname, die, cu);
14310
14311 /* The name is already allocated along with this objfile, so we don't
14312 need to duplicate it for the type. */
14313 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14314 FIELD_TYPE (*fp) = die_type (die, cu);
14315 FIELD_NAME (*fp) = fieldname;
14316 }
14317 else if (die->tag == DW_TAG_inheritance)
14318 {
14319 LONGEST offset;
14320
14321 /* C++ base class field. */
14322 if (handle_data_member_location (die, cu, &offset))
14323 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14324 FIELD_BITSIZE (*fp) = 0;
14325 FIELD_TYPE (*fp) = die_type (die, cu);
14326 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14327 }
14328 else if (die->tag == DW_TAG_variant_part)
14329 {
14330 /* process_structure_scope will treat this DIE as a union. */
14331 process_structure_scope (die, cu);
14332
14333 /* The variant part is relative to the start of the enclosing
14334 structure. */
14335 SET_FIELD_BITPOS (*fp, 0);
14336 fp->type = get_die_type (die, cu);
14337 fp->artificial = 1;
14338 fp->name = "<<variant>>";
14339
14340 /* Normally a DW_TAG_variant_part won't have a size, but our
14341 representation requires one, so set it to the maximum of the
14342 child sizes, being sure to account for the offset at which
14343 each child is seen. */
14344 if (TYPE_LENGTH (fp->type) == 0)
14345 {
14346 unsigned max = 0;
14347 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14348 {
14349 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14350 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14351 if (len > max)
14352 max = len;
14353 }
14354 TYPE_LENGTH (fp->type) = max;
14355 }
14356 }
14357 else
14358 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14359 }
14360
14361 /* Can the type given by DIE define another type? */
14362
14363 static bool
14364 type_can_define_types (const struct die_info *die)
14365 {
14366 switch (die->tag)
14367 {
14368 case DW_TAG_typedef:
14369 case DW_TAG_class_type:
14370 case DW_TAG_structure_type:
14371 case DW_TAG_union_type:
14372 case DW_TAG_enumeration_type:
14373 return true;
14374
14375 default:
14376 return false;
14377 }
14378 }
14379
14380 /* Add a type definition defined in the scope of the FIP's class. */
14381
14382 static void
14383 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14384 struct dwarf2_cu *cu)
14385 {
14386 struct decl_field fp;
14387 memset (&fp, 0, sizeof (fp));
14388
14389 gdb_assert (type_can_define_types (die));
14390
14391 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14392 fp.name = dwarf2_name (die, cu);
14393 fp.type = read_type_die (die, cu);
14394
14395 /* Save accessibility. */
14396 enum dwarf_access_attribute accessibility;
14397 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14398 if (attr != NULL)
14399 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14400 else
14401 accessibility = dwarf2_default_access_attribute (die, cu);
14402 switch (accessibility)
14403 {
14404 case DW_ACCESS_public:
14405 /* The assumed value if neither private nor protected. */
14406 break;
14407 case DW_ACCESS_private:
14408 fp.is_private = 1;
14409 break;
14410 case DW_ACCESS_protected:
14411 fp.is_protected = 1;
14412 break;
14413 default:
14414 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14415 }
14416
14417 if (die->tag == DW_TAG_typedef)
14418 fip->typedef_field_list.push_back (fp);
14419 else
14420 fip->nested_types_list.push_back (fp);
14421 }
14422
14423 /* Create the vector of fields, and attach it to the type. */
14424
14425 static void
14426 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14427 struct dwarf2_cu *cu)
14428 {
14429 int nfields = fip->nfields;
14430
14431 /* Record the field count, allocate space for the array of fields,
14432 and create blank accessibility bitfields if necessary. */
14433 TYPE_NFIELDS (type) = nfields;
14434 TYPE_FIELDS (type) = (struct field *)
14435 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14436
14437 if (fip->non_public_fields && cu->language != language_ada)
14438 {
14439 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14440
14441 TYPE_FIELD_PRIVATE_BITS (type) =
14442 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14443 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14444
14445 TYPE_FIELD_PROTECTED_BITS (type) =
14446 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14447 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14448
14449 TYPE_FIELD_IGNORE_BITS (type) =
14450 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14451 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14452 }
14453
14454 /* If the type has baseclasses, allocate and clear a bit vector for
14455 TYPE_FIELD_VIRTUAL_BITS. */
14456 if (!fip->baseclasses.empty () && cu->language != language_ada)
14457 {
14458 int num_bytes = B_BYTES (fip->baseclasses.size ());
14459 unsigned char *pointer;
14460
14461 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14462 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14463 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14464 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14465 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14466 }
14467
14468 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14469 {
14470 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14471
14472 for (int index = 0; index < nfields; ++index)
14473 {
14474 struct nextfield &field = fip->fields[index];
14475
14476 if (field.variant.is_discriminant)
14477 di->discriminant_index = index;
14478 else if (field.variant.default_branch)
14479 di->default_index = index;
14480 else
14481 di->discriminants[index] = field.variant.discriminant_value;
14482 }
14483 }
14484
14485 /* Copy the saved-up fields into the field vector. */
14486 for (int i = 0; i < nfields; ++i)
14487 {
14488 struct nextfield &field
14489 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14490 : fip->fields[i - fip->baseclasses.size ()]);
14491
14492 TYPE_FIELD (type, i) = field.field;
14493 switch (field.accessibility)
14494 {
14495 case DW_ACCESS_private:
14496 if (cu->language != language_ada)
14497 SET_TYPE_FIELD_PRIVATE (type, i);
14498 break;
14499
14500 case DW_ACCESS_protected:
14501 if (cu->language != language_ada)
14502 SET_TYPE_FIELD_PROTECTED (type, i);
14503 break;
14504
14505 case DW_ACCESS_public:
14506 break;
14507
14508 default:
14509 /* Unknown accessibility. Complain and treat it as public. */
14510 {
14511 complaint (_("unsupported accessibility %d"),
14512 field.accessibility);
14513 }
14514 break;
14515 }
14516 if (i < fip->baseclasses.size ())
14517 {
14518 switch (field.virtuality)
14519 {
14520 case DW_VIRTUALITY_virtual:
14521 case DW_VIRTUALITY_pure_virtual:
14522 if (cu->language == language_ada)
14523 error (_("unexpected virtuality in component of Ada type"));
14524 SET_TYPE_FIELD_VIRTUAL (type, i);
14525 break;
14526 }
14527 }
14528 }
14529 }
14530
14531 /* Return true if this member function is a constructor, false
14532 otherwise. */
14533
14534 static int
14535 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14536 {
14537 const char *fieldname;
14538 const char *type_name;
14539 int len;
14540
14541 if (die->parent == NULL)
14542 return 0;
14543
14544 if (die->parent->tag != DW_TAG_structure_type
14545 && die->parent->tag != DW_TAG_union_type
14546 && die->parent->tag != DW_TAG_class_type)
14547 return 0;
14548
14549 fieldname = dwarf2_name (die, cu);
14550 type_name = dwarf2_name (die->parent, cu);
14551 if (fieldname == NULL || type_name == NULL)
14552 return 0;
14553
14554 len = strlen (fieldname);
14555 return (strncmp (fieldname, type_name, len) == 0
14556 && (type_name[len] == '\0' || type_name[len] == '<'));
14557 }
14558
14559 /* Check if the given VALUE is a recognized enum
14560 dwarf_defaulted_attribute constant according to DWARF5 spec,
14561 Table 7.24. */
14562
14563 static bool
14564 is_valid_DW_AT_defaulted (ULONGEST value)
14565 {
14566 switch (value)
14567 {
14568 case DW_DEFAULTED_no:
14569 case DW_DEFAULTED_in_class:
14570 case DW_DEFAULTED_out_of_class:
14571 return true;
14572 }
14573
14574 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14575 return false;
14576 }
14577
14578 /* Add a member function to the proper fieldlist. */
14579
14580 static void
14581 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14582 struct type *type, struct dwarf2_cu *cu)
14583 {
14584 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14585 struct attribute *attr;
14586 int i;
14587 struct fnfieldlist *flp = nullptr;
14588 struct fn_field *fnp;
14589 const char *fieldname;
14590 struct type *this_type;
14591 enum dwarf_access_attribute accessibility;
14592
14593 if (cu->language == language_ada)
14594 error (_("unexpected member function in Ada type"));
14595
14596 /* Get name of member function. */
14597 fieldname = dwarf2_name (die, cu);
14598 if (fieldname == NULL)
14599 return;
14600
14601 /* Look up member function name in fieldlist. */
14602 for (i = 0; i < fip->fnfieldlists.size (); i++)
14603 {
14604 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14605 {
14606 flp = &fip->fnfieldlists[i];
14607 break;
14608 }
14609 }
14610
14611 /* Create a new fnfieldlist if necessary. */
14612 if (flp == nullptr)
14613 {
14614 fip->fnfieldlists.emplace_back ();
14615 flp = &fip->fnfieldlists.back ();
14616 flp->name = fieldname;
14617 i = fip->fnfieldlists.size () - 1;
14618 }
14619
14620 /* Create a new member function field and add it to the vector of
14621 fnfieldlists. */
14622 flp->fnfields.emplace_back ();
14623 fnp = &flp->fnfields.back ();
14624
14625 /* Delay processing of the physname until later. */
14626 if (cu->language == language_cplus)
14627 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14628 die, cu);
14629 else
14630 {
14631 const char *physname = dwarf2_physname (fieldname, die, cu);
14632 fnp->physname = physname ? physname : "";
14633 }
14634
14635 fnp->type = alloc_type (objfile);
14636 this_type = read_type_die (die, cu);
14637 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14638 {
14639 int nparams = TYPE_NFIELDS (this_type);
14640
14641 /* TYPE is the domain of this method, and THIS_TYPE is the type
14642 of the method itself (TYPE_CODE_METHOD). */
14643 smash_to_method_type (fnp->type, type,
14644 TYPE_TARGET_TYPE (this_type),
14645 TYPE_FIELDS (this_type),
14646 TYPE_NFIELDS (this_type),
14647 TYPE_VARARGS (this_type));
14648
14649 /* Handle static member functions.
14650 Dwarf2 has no clean way to discern C++ static and non-static
14651 member functions. G++ helps GDB by marking the first
14652 parameter for non-static member functions (which is the this
14653 pointer) as artificial. We obtain this information from
14654 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14655 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14656 fnp->voffset = VOFFSET_STATIC;
14657 }
14658 else
14659 complaint (_("member function type missing for '%s'"),
14660 dwarf2_full_name (fieldname, die, cu));
14661
14662 /* Get fcontext from DW_AT_containing_type if present. */
14663 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14664 fnp->fcontext = die_containing_type (die, cu);
14665
14666 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14667 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14668
14669 /* Get accessibility. */
14670 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14671 if (attr != nullptr)
14672 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14673 else
14674 accessibility = dwarf2_default_access_attribute (die, cu);
14675 switch (accessibility)
14676 {
14677 case DW_ACCESS_private:
14678 fnp->is_private = 1;
14679 break;
14680 case DW_ACCESS_protected:
14681 fnp->is_protected = 1;
14682 break;
14683 }
14684
14685 /* Check for artificial methods. */
14686 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14687 if (attr && DW_UNSND (attr) != 0)
14688 fnp->is_artificial = 1;
14689
14690 /* Check for defaulted methods. */
14691 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14692 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14693 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14694
14695 /* Check for deleted methods. */
14696 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14697 if (attr != nullptr && DW_UNSND (attr) != 0)
14698 fnp->is_deleted = 1;
14699
14700 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14701
14702 /* Get index in virtual function table if it is a virtual member
14703 function. For older versions of GCC, this is an offset in the
14704 appropriate virtual table, as specified by DW_AT_containing_type.
14705 For everyone else, it is an expression to be evaluated relative
14706 to the object address. */
14707
14708 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14709 if (attr != nullptr)
14710 {
14711 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14712 {
14713 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14714 {
14715 /* Old-style GCC. */
14716 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14717 }
14718 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14719 || (DW_BLOCK (attr)->size > 1
14720 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14721 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14722 {
14723 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14724 if ((fnp->voffset % cu->header.addr_size) != 0)
14725 dwarf2_complex_location_expr_complaint ();
14726 else
14727 fnp->voffset /= cu->header.addr_size;
14728 fnp->voffset += 2;
14729 }
14730 else
14731 dwarf2_complex_location_expr_complaint ();
14732
14733 if (!fnp->fcontext)
14734 {
14735 /* If there is no `this' field and no DW_AT_containing_type,
14736 we cannot actually find a base class context for the
14737 vtable! */
14738 if (TYPE_NFIELDS (this_type) == 0
14739 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14740 {
14741 complaint (_("cannot determine context for virtual member "
14742 "function \"%s\" (offset %s)"),
14743 fieldname, sect_offset_str (die->sect_off));
14744 }
14745 else
14746 {
14747 fnp->fcontext
14748 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14749 }
14750 }
14751 }
14752 else if (attr->form_is_section_offset ())
14753 {
14754 dwarf2_complex_location_expr_complaint ();
14755 }
14756 else
14757 {
14758 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14759 fieldname);
14760 }
14761 }
14762 else
14763 {
14764 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14765 if (attr && DW_UNSND (attr))
14766 {
14767 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14768 complaint (_("Member function \"%s\" (offset %s) is virtual "
14769 "but the vtable offset is not specified"),
14770 fieldname, sect_offset_str (die->sect_off));
14771 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14772 TYPE_CPLUS_DYNAMIC (type) = 1;
14773 }
14774 }
14775 }
14776
14777 /* Create the vector of member function fields, and attach it to the type. */
14778
14779 static void
14780 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14781 struct dwarf2_cu *cu)
14782 {
14783 if (cu->language == language_ada)
14784 error (_("unexpected member functions in Ada type"));
14785
14786 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14787 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14788 TYPE_ALLOC (type,
14789 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14790
14791 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14792 {
14793 struct fnfieldlist &nf = fip->fnfieldlists[i];
14794 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14795
14796 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14797 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14798 fn_flp->fn_fields = (struct fn_field *)
14799 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14800
14801 for (int k = 0; k < nf.fnfields.size (); ++k)
14802 fn_flp->fn_fields[k] = nf.fnfields[k];
14803 }
14804
14805 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14806 }
14807
14808 /* Returns non-zero if NAME is the name of a vtable member in CU's
14809 language, zero otherwise. */
14810 static int
14811 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14812 {
14813 static const char vptr[] = "_vptr";
14814
14815 /* Look for the C++ form of the vtable. */
14816 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14817 return 1;
14818
14819 return 0;
14820 }
14821
14822 /* GCC outputs unnamed structures that are really pointers to member
14823 functions, with the ABI-specified layout. If TYPE describes
14824 such a structure, smash it into a member function type.
14825
14826 GCC shouldn't do this; it should just output pointer to member DIEs.
14827 This is GCC PR debug/28767. */
14828
14829 static void
14830 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14831 {
14832 struct type *pfn_type, *self_type, *new_type;
14833
14834 /* Check for a structure with no name and two children. */
14835 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14836 return;
14837
14838 /* Check for __pfn and __delta members. */
14839 if (TYPE_FIELD_NAME (type, 0) == NULL
14840 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14841 || TYPE_FIELD_NAME (type, 1) == NULL
14842 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14843 return;
14844
14845 /* Find the type of the method. */
14846 pfn_type = TYPE_FIELD_TYPE (type, 0);
14847 if (pfn_type == NULL
14848 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14849 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14850 return;
14851
14852 /* Look for the "this" argument. */
14853 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14854 if (TYPE_NFIELDS (pfn_type) == 0
14855 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14856 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14857 return;
14858
14859 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14860 new_type = alloc_type (objfile);
14861 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14862 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14863 TYPE_VARARGS (pfn_type));
14864 smash_to_methodptr_type (type, new_type);
14865 }
14866
14867 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14868 appropriate error checking and issuing complaints if there is a
14869 problem. */
14870
14871 static ULONGEST
14872 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14873 {
14874 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14875
14876 if (attr == nullptr)
14877 return 0;
14878
14879 if (!attr->form_is_constant ())
14880 {
14881 complaint (_("DW_AT_alignment must have constant form"
14882 " - DIE at %s [in module %s]"),
14883 sect_offset_str (die->sect_off),
14884 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14885 return 0;
14886 }
14887
14888 ULONGEST align;
14889 if (attr->form == DW_FORM_sdata)
14890 {
14891 LONGEST val = DW_SND (attr);
14892 if (val < 0)
14893 {
14894 complaint (_("DW_AT_alignment value must not be negative"
14895 " - DIE at %s [in module %s]"),
14896 sect_offset_str (die->sect_off),
14897 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14898 return 0;
14899 }
14900 align = val;
14901 }
14902 else
14903 align = DW_UNSND (attr);
14904
14905 if (align == 0)
14906 {
14907 complaint (_("DW_AT_alignment value must not be zero"
14908 " - DIE at %s [in module %s]"),
14909 sect_offset_str (die->sect_off),
14910 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14911 return 0;
14912 }
14913 if ((align & (align - 1)) != 0)
14914 {
14915 complaint (_("DW_AT_alignment value must be a power of 2"
14916 " - DIE at %s [in module %s]"),
14917 sect_offset_str (die->sect_off),
14918 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14919 return 0;
14920 }
14921
14922 return align;
14923 }
14924
14925 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14926 the alignment for TYPE. */
14927
14928 static void
14929 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14930 struct type *type)
14931 {
14932 if (!set_type_align (type, get_alignment (cu, die)))
14933 complaint (_("DW_AT_alignment value too large"
14934 " - DIE at %s [in module %s]"),
14935 sect_offset_str (die->sect_off),
14936 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14937 }
14938
14939 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14940 constant for a type, according to DWARF5 spec, Table 5.5. */
14941
14942 static bool
14943 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14944 {
14945 switch (value)
14946 {
14947 case DW_CC_normal:
14948 case DW_CC_pass_by_reference:
14949 case DW_CC_pass_by_value:
14950 return true;
14951
14952 default:
14953 complaint (_("unrecognized DW_AT_calling_convention value "
14954 "(%s) for a type"), pulongest (value));
14955 return false;
14956 }
14957 }
14958
14959 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14960 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14961 also according to GNU-specific values (see include/dwarf2.h). */
14962
14963 static bool
14964 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14965 {
14966 switch (value)
14967 {
14968 case DW_CC_normal:
14969 case DW_CC_program:
14970 case DW_CC_nocall:
14971 return true;
14972
14973 case DW_CC_GNU_renesas_sh:
14974 case DW_CC_GNU_borland_fastcall_i386:
14975 case DW_CC_GDB_IBM_OpenCL:
14976 return true;
14977
14978 default:
14979 complaint (_("unrecognized DW_AT_calling_convention value "
14980 "(%s) for a subroutine"), pulongest (value));
14981 return false;
14982 }
14983 }
14984
14985 /* Called when we find the DIE that starts a structure or union scope
14986 (definition) to create a type for the structure or union. Fill in
14987 the type's name and general properties; the members will not be
14988 processed until process_structure_scope. A symbol table entry for
14989 the type will also not be done until process_structure_scope (assuming
14990 the type has a name).
14991
14992 NOTE: we need to call these functions regardless of whether or not the
14993 DIE has a DW_AT_name attribute, since it might be an anonymous
14994 structure or union. This gets the type entered into our set of
14995 user defined types. */
14996
14997 static struct type *
14998 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14999 {
15000 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15001 struct type *type;
15002 struct attribute *attr;
15003 const char *name;
15004
15005 /* If the definition of this type lives in .debug_types, read that type.
15006 Don't follow DW_AT_specification though, that will take us back up
15007 the chain and we want to go down. */
15008 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15009 if (attr != nullptr)
15010 {
15011 type = get_DW_AT_signature_type (die, attr, cu);
15012
15013 /* The type's CU may not be the same as CU.
15014 Ensure TYPE is recorded with CU in die_type_hash. */
15015 return set_die_type (die, type, cu);
15016 }
15017
15018 type = alloc_type (objfile);
15019 INIT_CPLUS_SPECIFIC (type);
15020
15021 name = dwarf2_name (die, cu);
15022 if (name != NULL)
15023 {
15024 if (cu->language == language_cplus
15025 || cu->language == language_d
15026 || cu->language == language_rust)
15027 {
15028 const char *full_name = dwarf2_full_name (name, die, cu);
15029
15030 /* dwarf2_full_name might have already finished building the DIE's
15031 type. If so, there is no need to continue. */
15032 if (get_die_type (die, cu) != NULL)
15033 return get_die_type (die, cu);
15034
15035 TYPE_NAME (type) = full_name;
15036 }
15037 else
15038 {
15039 /* The name is already allocated along with this objfile, so
15040 we don't need to duplicate it for the type. */
15041 TYPE_NAME (type) = name;
15042 }
15043 }
15044
15045 if (die->tag == DW_TAG_structure_type)
15046 {
15047 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15048 }
15049 else if (die->tag == DW_TAG_union_type)
15050 {
15051 TYPE_CODE (type) = TYPE_CODE_UNION;
15052 }
15053 else if (die->tag == DW_TAG_variant_part)
15054 {
15055 TYPE_CODE (type) = TYPE_CODE_UNION;
15056 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15057 }
15058 else
15059 {
15060 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15061 }
15062
15063 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15064 TYPE_DECLARED_CLASS (type) = 1;
15065
15066 /* Store the calling convention in the type if it's available in
15067 the die. Otherwise the calling convention remains set to
15068 the default value DW_CC_normal. */
15069 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15070 if (attr != nullptr
15071 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15072 {
15073 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15074 TYPE_CPLUS_CALLING_CONVENTION (type)
15075 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15076 }
15077
15078 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15079 if (attr != nullptr)
15080 {
15081 if (attr->form_is_constant ())
15082 TYPE_LENGTH (type) = DW_UNSND (attr);
15083 else
15084 {
15085 /* For the moment, dynamic type sizes are not supported
15086 by GDB's struct type. The actual size is determined
15087 on-demand when resolving the type of a given object,
15088 so set the type's length to zero for now. Otherwise,
15089 we record an expression as the length, and that expression
15090 could lead to a very large value, which could eventually
15091 lead to us trying to allocate that much memory when creating
15092 a value of that type. */
15093 TYPE_LENGTH (type) = 0;
15094 }
15095 }
15096 else
15097 {
15098 TYPE_LENGTH (type) = 0;
15099 }
15100
15101 maybe_set_alignment (cu, die, type);
15102
15103 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15104 {
15105 /* ICC<14 does not output the required DW_AT_declaration on
15106 incomplete types, but gives them a size of zero. */
15107 TYPE_STUB (type) = 1;
15108 }
15109 else
15110 TYPE_STUB_SUPPORTED (type) = 1;
15111
15112 if (die_is_declaration (die, cu))
15113 TYPE_STUB (type) = 1;
15114 else if (attr == NULL && die->child == NULL
15115 && producer_is_realview (cu->producer))
15116 /* RealView does not output the required DW_AT_declaration
15117 on incomplete types. */
15118 TYPE_STUB (type) = 1;
15119
15120 /* We need to add the type field to the die immediately so we don't
15121 infinitely recurse when dealing with pointers to the structure
15122 type within the structure itself. */
15123 set_die_type (die, type, cu);
15124
15125 /* set_die_type should be already done. */
15126 set_descriptive_type (type, die, cu);
15127
15128 return type;
15129 }
15130
15131 /* A helper for process_structure_scope that handles a single member
15132 DIE. */
15133
15134 static void
15135 handle_struct_member_die (struct die_info *child_die, struct type *type,
15136 struct field_info *fi,
15137 std::vector<struct symbol *> *template_args,
15138 struct dwarf2_cu *cu)
15139 {
15140 if (child_die->tag == DW_TAG_member
15141 || child_die->tag == DW_TAG_variable
15142 || child_die->tag == DW_TAG_variant_part)
15143 {
15144 /* NOTE: carlton/2002-11-05: A C++ static data member
15145 should be a DW_TAG_member that is a declaration, but
15146 all versions of G++ as of this writing (so through at
15147 least 3.2.1) incorrectly generate DW_TAG_variable
15148 tags for them instead. */
15149 dwarf2_add_field (fi, child_die, cu);
15150 }
15151 else if (child_die->tag == DW_TAG_subprogram)
15152 {
15153 /* Rust doesn't have member functions in the C++ sense.
15154 However, it does emit ordinary functions as children
15155 of a struct DIE. */
15156 if (cu->language == language_rust)
15157 read_func_scope (child_die, cu);
15158 else
15159 {
15160 /* C++ member function. */
15161 dwarf2_add_member_fn (fi, child_die, type, cu);
15162 }
15163 }
15164 else if (child_die->tag == DW_TAG_inheritance)
15165 {
15166 /* C++ base class field. */
15167 dwarf2_add_field (fi, child_die, cu);
15168 }
15169 else if (type_can_define_types (child_die))
15170 dwarf2_add_type_defn (fi, child_die, cu);
15171 else if (child_die->tag == DW_TAG_template_type_param
15172 || child_die->tag == DW_TAG_template_value_param)
15173 {
15174 struct symbol *arg = new_symbol (child_die, NULL, cu);
15175
15176 if (arg != NULL)
15177 template_args->push_back (arg);
15178 }
15179 else if (child_die->tag == DW_TAG_variant)
15180 {
15181 /* In a variant we want to get the discriminant and also add a
15182 field for our sole member child. */
15183 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15184
15185 for (die_info *variant_child = child_die->child;
15186 variant_child != NULL;
15187 variant_child = sibling_die (variant_child))
15188 {
15189 if (variant_child->tag == DW_TAG_member)
15190 {
15191 handle_struct_member_die (variant_child, type, fi,
15192 template_args, cu);
15193 /* Only handle the one. */
15194 break;
15195 }
15196 }
15197
15198 /* We don't handle this but we might as well report it if we see
15199 it. */
15200 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15201 complaint (_("DW_AT_discr_list is not supported yet"
15202 " - DIE at %s [in module %s]"),
15203 sect_offset_str (child_die->sect_off),
15204 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15205
15206 /* The first field was just added, so we can stash the
15207 discriminant there. */
15208 gdb_assert (!fi->fields.empty ());
15209 if (discr == NULL)
15210 fi->fields.back ().variant.default_branch = true;
15211 else
15212 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15213 }
15214 }
15215
15216 /* Finish creating a structure or union type, including filling in
15217 its members and creating a symbol for it. */
15218
15219 static void
15220 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15221 {
15222 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15223 struct die_info *child_die;
15224 struct type *type;
15225
15226 type = get_die_type (die, cu);
15227 if (type == NULL)
15228 type = read_structure_type (die, cu);
15229
15230 /* When reading a DW_TAG_variant_part, we need to notice when we
15231 read the discriminant member, so we can record it later in the
15232 discriminant_info. */
15233 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15234 sect_offset discr_offset {};
15235 bool has_template_parameters = false;
15236
15237 if (is_variant_part)
15238 {
15239 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15240 if (discr == NULL)
15241 {
15242 /* Maybe it's a univariant form, an extension we support.
15243 In this case arrange not to check the offset. */
15244 is_variant_part = false;
15245 }
15246 else if (discr->form_is_ref ())
15247 {
15248 struct dwarf2_cu *target_cu = cu;
15249 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15250
15251 discr_offset = target_die->sect_off;
15252 }
15253 else
15254 {
15255 complaint (_("DW_AT_discr does not have DIE reference form"
15256 " - DIE at %s [in module %s]"),
15257 sect_offset_str (die->sect_off),
15258 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15259 is_variant_part = false;
15260 }
15261 }
15262
15263 if (die->child != NULL && ! die_is_declaration (die, cu))
15264 {
15265 struct field_info fi;
15266 std::vector<struct symbol *> template_args;
15267
15268 child_die = die->child;
15269
15270 while (child_die && child_die->tag)
15271 {
15272 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15273
15274 if (is_variant_part && discr_offset == child_die->sect_off)
15275 fi.fields.back ().variant.is_discriminant = true;
15276
15277 child_die = sibling_die (child_die);
15278 }
15279
15280 /* Attach template arguments to type. */
15281 if (!template_args.empty ())
15282 {
15283 has_template_parameters = true;
15284 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15285 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15286 TYPE_TEMPLATE_ARGUMENTS (type)
15287 = XOBNEWVEC (&objfile->objfile_obstack,
15288 struct symbol *,
15289 TYPE_N_TEMPLATE_ARGUMENTS (type));
15290 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15291 template_args.data (),
15292 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15293 * sizeof (struct symbol *)));
15294 }
15295
15296 /* Attach fields and member functions to the type. */
15297 if (fi.nfields)
15298 dwarf2_attach_fields_to_type (&fi, type, cu);
15299 if (!fi.fnfieldlists.empty ())
15300 {
15301 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15302
15303 /* Get the type which refers to the base class (possibly this
15304 class itself) which contains the vtable pointer for the current
15305 class from the DW_AT_containing_type attribute. This use of
15306 DW_AT_containing_type is a GNU extension. */
15307
15308 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15309 {
15310 struct type *t = die_containing_type (die, cu);
15311
15312 set_type_vptr_basetype (type, t);
15313 if (type == t)
15314 {
15315 int i;
15316
15317 /* Our own class provides vtbl ptr. */
15318 for (i = TYPE_NFIELDS (t) - 1;
15319 i >= TYPE_N_BASECLASSES (t);
15320 --i)
15321 {
15322 const char *fieldname = TYPE_FIELD_NAME (t, i);
15323
15324 if (is_vtable_name (fieldname, cu))
15325 {
15326 set_type_vptr_fieldno (type, i);
15327 break;
15328 }
15329 }
15330
15331 /* Complain if virtual function table field not found. */
15332 if (i < TYPE_N_BASECLASSES (t))
15333 complaint (_("virtual function table pointer "
15334 "not found when defining class '%s'"),
15335 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15336 }
15337 else
15338 {
15339 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15340 }
15341 }
15342 else if (cu->producer
15343 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15344 {
15345 /* The IBM XLC compiler does not provide direct indication
15346 of the containing type, but the vtable pointer is
15347 always named __vfp. */
15348
15349 int i;
15350
15351 for (i = TYPE_NFIELDS (type) - 1;
15352 i >= TYPE_N_BASECLASSES (type);
15353 --i)
15354 {
15355 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15356 {
15357 set_type_vptr_fieldno (type, i);
15358 set_type_vptr_basetype (type, type);
15359 break;
15360 }
15361 }
15362 }
15363 }
15364
15365 /* Copy fi.typedef_field_list linked list elements content into the
15366 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15367 if (!fi.typedef_field_list.empty ())
15368 {
15369 int count = fi.typedef_field_list.size ();
15370
15371 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15372 TYPE_TYPEDEF_FIELD_ARRAY (type)
15373 = ((struct decl_field *)
15374 TYPE_ALLOC (type,
15375 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15376 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15377
15378 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15379 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15380 }
15381
15382 /* Copy fi.nested_types_list linked list elements content into the
15383 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15384 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15385 {
15386 int count = fi.nested_types_list.size ();
15387
15388 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15389 TYPE_NESTED_TYPES_ARRAY (type)
15390 = ((struct decl_field *)
15391 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15392 TYPE_NESTED_TYPES_COUNT (type) = count;
15393
15394 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15395 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15396 }
15397 }
15398
15399 quirk_gcc_member_function_pointer (type, objfile);
15400 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15401 cu->rust_unions.push_back (type);
15402
15403 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15404 snapshots) has been known to create a die giving a declaration
15405 for a class that has, as a child, a die giving a definition for a
15406 nested class. So we have to process our children even if the
15407 current die is a declaration. Normally, of course, a declaration
15408 won't have any children at all. */
15409
15410 child_die = die->child;
15411
15412 while (child_die != NULL && child_die->tag)
15413 {
15414 if (child_die->tag == DW_TAG_member
15415 || child_die->tag == DW_TAG_variable
15416 || child_die->tag == DW_TAG_inheritance
15417 || child_die->tag == DW_TAG_template_value_param
15418 || child_die->tag == DW_TAG_template_type_param)
15419 {
15420 /* Do nothing. */
15421 }
15422 else
15423 process_die (child_die, cu);
15424
15425 child_die = sibling_die (child_die);
15426 }
15427
15428 /* Do not consider external references. According to the DWARF standard,
15429 these DIEs are identified by the fact that they have no byte_size
15430 attribute, and a declaration attribute. */
15431 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15432 || !die_is_declaration (die, cu))
15433 {
15434 struct symbol *sym = new_symbol (die, type, cu);
15435
15436 if (has_template_parameters)
15437 {
15438 struct symtab *symtab;
15439 if (sym != nullptr)
15440 symtab = symbol_symtab (sym);
15441 else if (cu->line_header != nullptr)
15442 {
15443 /* Any related symtab will do. */
15444 symtab
15445 = cu->line_header->file_names ()[0].symtab;
15446 }
15447 else
15448 {
15449 symtab = nullptr;
15450 complaint (_("could not find suitable "
15451 "symtab for template parameter"
15452 " - DIE at %s [in module %s]"),
15453 sect_offset_str (die->sect_off),
15454 objfile_name (objfile));
15455 }
15456
15457 if (symtab != nullptr)
15458 {
15459 /* Make sure that the symtab is set on the new symbols.
15460 Even though they don't appear in this symtab directly,
15461 other parts of gdb assume that symbols do, and this is
15462 reasonably true. */
15463 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15464 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15465 }
15466 }
15467 }
15468 }
15469
15470 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15471 update TYPE using some information only available in DIE's children. */
15472
15473 static void
15474 update_enumeration_type_from_children (struct die_info *die,
15475 struct type *type,
15476 struct dwarf2_cu *cu)
15477 {
15478 struct die_info *child_die;
15479 int unsigned_enum = 1;
15480 int flag_enum = 1;
15481
15482 auto_obstack obstack;
15483
15484 for (child_die = die->child;
15485 child_die != NULL && child_die->tag;
15486 child_die = sibling_die (child_die))
15487 {
15488 struct attribute *attr;
15489 LONGEST value;
15490 const gdb_byte *bytes;
15491 struct dwarf2_locexpr_baton *baton;
15492 const char *name;
15493
15494 if (child_die->tag != DW_TAG_enumerator)
15495 continue;
15496
15497 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15498 if (attr == NULL)
15499 continue;
15500
15501 name = dwarf2_name (child_die, cu);
15502 if (name == NULL)
15503 name = "<anonymous enumerator>";
15504
15505 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15506 &value, &bytes, &baton);
15507 if (value < 0)
15508 {
15509 unsigned_enum = 0;
15510 flag_enum = 0;
15511 }
15512 else
15513 {
15514 if (count_one_bits_ll (value) >= 2)
15515 flag_enum = 0;
15516 }
15517
15518 /* If we already know that the enum type is neither unsigned, nor
15519 a flag type, no need to look at the rest of the enumerates. */
15520 if (!unsigned_enum && !flag_enum)
15521 break;
15522 }
15523
15524 if (unsigned_enum)
15525 TYPE_UNSIGNED (type) = 1;
15526 if (flag_enum)
15527 TYPE_FLAG_ENUM (type) = 1;
15528 }
15529
15530 /* Given a DW_AT_enumeration_type die, set its type. We do not
15531 complete the type's fields yet, or create any symbols. */
15532
15533 static struct type *
15534 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15535 {
15536 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15537 struct type *type;
15538 struct attribute *attr;
15539 const char *name;
15540
15541 /* If the definition of this type lives in .debug_types, read that type.
15542 Don't follow DW_AT_specification though, that will take us back up
15543 the chain and we want to go down. */
15544 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15545 if (attr != nullptr)
15546 {
15547 type = get_DW_AT_signature_type (die, attr, cu);
15548
15549 /* The type's CU may not be the same as CU.
15550 Ensure TYPE is recorded with CU in die_type_hash. */
15551 return set_die_type (die, type, cu);
15552 }
15553
15554 type = alloc_type (objfile);
15555
15556 TYPE_CODE (type) = TYPE_CODE_ENUM;
15557 name = dwarf2_full_name (NULL, die, cu);
15558 if (name != NULL)
15559 TYPE_NAME (type) = name;
15560
15561 attr = dwarf2_attr (die, DW_AT_type, cu);
15562 if (attr != NULL)
15563 {
15564 struct type *underlying_type = die_type (die, cu);
15565
15566 TYPE_TARGET_TYPE (type) = underlying_type;
15567 }
15568
15569 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15570 if (attr != nullptr)
15571 {
15572 TYPE_LENGTH (type) = DW_UNSND (attr);
15573 }
15574 else
15575 {
15576 TYPE_LENGTH (type) = 0;
15577 }
15578
15579 maybe_set_alignment (cu, die, type);
15580
15581 /* The enumeration DIE can be incomplete. In Ada, any type can be
15582 declared as private in the package spec, and then defined only
15583 inside the package body. Such types are known as Taft Amendment
15584 Types. When another package uses such a type, an incomplete DIE
15585 may be generated by the compiler. */
15586 if (die_is_declaration (die, cu))
15587 TYPE_STUB (type) = 1;
15588
15589 /* Finish the creation of this type by using the enum's children.
15590 We must call this even when the underlying type has been provided
15591 so that we can determine if we're looking at a "flag" enum. */
15592 update_enumeration_type_from_children (die, type, cu);
15593
15594 /* If this type has an underlying type that is not a stub, then we
15595 may use its attributes. We always use the "unsigned" attribute
15596 in this situation, because ordinarily we guess whether the type
15597 is unsigned -- but the guess can be wrong and the underlying type
15598 can tell us the reality. However, we defer to a local size
15599 attribute if one exists, because this lets the compiler override
15600 the underlying type if needed. */
15601 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15602 {
15603 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15604 if (TYPE_LENGTH (type) == 0)
15605 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15606 if (TYPE_RAW_ALIGN (type) == 0
15607 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15608 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15609 }
15610
15611 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15612
15613 return set_die_type (die, type, cu);
15614 }
15615
15616 /* Given a pointer to a die which begins an enumeration, process all
15617 the dies that define the members of the enumeration, and create the
15618 symbol for the enumeration type.
15619
15620 NOTE: We reverse the order of the element list. */
15621
15622 static void
15623 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15624 {
15625 struct type *this_type;
15626
15627 this_type = get_die_type (die, cu);
15628 if (this_type == NULL)
15629 this_type = read_enumeration_type (die, cu);
15630
15631 if (die->child != NULL)
15632 {
15633 struct die_info *child_die;
15634 struct symbol *sym;
15635 std::vector<struct field> fields;
15636 const char *name;
15637
15638 child_die = die->child;
15639 while (child_die && child_die->tag)
15640 {
15641 if (child_die->tag != DW_TAG_enumerator)
15642 {
15643 process_die (child_die, cu);
15644 }
15645 else
15646 {
15647 name = dwarf2_name (child_die, cu);
15648 if (name)
15649 {
15650 sym = new_symbol (child_die, this_type, cu);
15651
15652 fields.emplace_back ();
15653 struct field &field = fields.back ();
15654
15655 FIELD_NAME (field) = sym->linkage_name ();
15656 FIELD_TYPE (field) = NULL;
15657 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15658 FIELD_BITSIZE (field) = 0;
15659 }
15660 }
15661
15662 child_die = sibling_die (child_die);
15663 }
15664
15665 if (!fields.empty ())
15666 {
15667 TYPE_NFIELDS (this_type) = fields.size ();
15668 TYPE_FIELDS (this_type) = (struct field *)
15669 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15670 memcpy (TYPE_FIELDS (this_type), fields.data (),
15671 sizeof (struct field) * fields.size ());
15672 }
15673 }
15674
15675 /* If we are reading an enum from a .debug_types unit, and the enum
15676 is a declaration, and the enum is not the signatured type in the
15677 unit, then we do not want to add a symbol for it. Adding a
15678 symbol would in some cases obscure the true definition of the
15679 enum, giving users an incomplete type when the definition is
15680 actually available. Note that we do not want to do this for all
15681 enums which are just declarations, because C++0x allows forward
15682 enum declarations. */
15683 if (cu->per_cu->is_debug_types
15684 && die_is_declaration (die, cu))
15685 {
15686 struct signatured_type *sig_type;
15687
15688 sig_type = (struct signatured_type *) cu->per_cu;
15689 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15690 if (sig_type->type_offset_in_section != die->sect_off)
15691 return;
15692 }
15693
15694 new_symbol (die, this_type, cu);
15695 }
15696
15697 /* Extract all information from a DW_TAG_array_type DIE and put it in
15698 the DIE's type field. For now, this only handles one dimensional
15699 arrays. */
15700
15701 static struct type *
15702 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15703 {
15704 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15705 struct die_info *child_die;
15706 struct type *type;
15707 struct type *element_type, *range_type, *index_type;
15708 struct attribute *attr;
15709 const char *name;
15710 struct dynamic_prop *byte_stride_prop = NULL;
15711 unsigned int bit_stride = 0;
15712
15713 element_type = die_type (die, cu);
15714
15715 /* The die_type call above may have already set the type for this DIE. */
15716 type = get_die_type (die, cu);
15717 if (type)
15718 return type;
15719
15720 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15721 if (attr != NULL)
15722 {
15723 int stride_ok;
15724 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15725
15726 byte_stride_prop
15727 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15728 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15729 prop_type);
15730 if (!stride_ok)
15731 {
15732 complaint (_("unable to read array DW_AT_byte_stride "
15733 " - DIE at %s [in module %s]"),
15734 sect_offset_str (die->sect_off),
15735 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15736 /* Ignore this attribute. We will likely not be able to print
15737 arrays of this type correctly, but there is little we can do
15738 to help if we cannot read the attribute's value. */
15739 byte_stride_prop = NULL;
15740 }
15741 }
15742
15743 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15744 if (attr != NULL)
15745 bit_stride = DW_UNSND (attr);
15746
15747 /* Irix 6.2 native cc creates array types without children for
15748 arrays with unspecified length. */
15749 if (die->child == NULL)
15750 {
15751 index_type = objfile_type (objfile)->builtin_int;
15752 range_type = create_static_range_type (NULL, index_type, 0, -1);
15753 type = create_array_type_with_stride (NULL, element_type, range_type,
15754 byte_stride_prop, bit_stride);
15755 return set_die_type (die, type, cu);
15756 }
15757
15758 std::vector<struct type *> range_types;
15759 child_die = die->child;
15760 while (child_die && child_die->tag)
15761 {
15762 if (child_die->tag == DW_TAG_subrange_type)
15763 {
15764 struct type *child_type = read_type_die (child_die, cu);
15765
15766 if (child_type != NULL)
15767 {
15768 /* The range type was succesfully read. Save it for the
15769 array type creation. */
15770 range_types.push_back (child_type);
15771 }
15772 }
15773 child_die = sibling_die (child_die);
15774 }
15775
15776 /* Dwarf2 dimensions are output from left to right, create the
15777 necessary array types in backwards order. */
15778
15779 type = element_type;
15780
15781 if (read_array_order (die, cu) == DW_ORD_col_major)
15782 {
15783 int i = 0;
15784
15785 while (i < range_types.size ())
15786 type = create_array_type_with_stride (NULL, type, range_types[i++],
15787 byte_stride_prop, bit_stride);
15788 }
15789 else
15790 {
15791 size_t ndim = range_types.size ();
15792 while (ndim-- > 0)
15793 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15794 byte_stride_prop, bit_stride);
15795 }
15796
15797 /* Understand Dwarf2 support for vector types (like they occur on
15798 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15799 array type. This is not part of the Dwarf2/3 standard yet, but a
15800 custom vendor extension. The main difference between a regular
15801 array and the vector variant is that vectors are passed by value
15802 to functions. */
15803 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15804 if (attr != nullptr)
15805 make_vector_type (type);
15806
15807 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15808 implementation may choose to implement triple vectors using this
15809 attribute. */
15810 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15811 if (attr != nullptr)
15812 {
15813 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15814 TYPE_LENGTH (type) = DW_UNSND (attr);
15815 else
15816 complaint (_("DW_AT_byte_size for array type smaller "
15817 "than the total size of elements"));
15818 }
15819
15820 name = dwarf2_name (die, cu);
15821 if (name)
15822 TYPE_NAME (type) = name;
15823
15824 maybe_set_alignment (cu, die, type);
15825
15826 /* Install the type in the die. */
15827 set_die_type (die, type, cu);
15828
15829 /* set_die_type should be already done. */
15830 set_descriptive_type (type, die, cu);
15831
15832 return type;
15833 }
15834
15835 static enum dwarf_array_dim_ordering
15836 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15837 {
15838 struct attribute *attr;
15839
15840 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15841
15842 if (attr != nullptr)
15843 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15844
15845 /* GNU F77 is a special case, as at 08/2004 array type info is the
15846 opposite order to the dwarf2 specification, but data is still
15847 laid out as per normal fortran.
15848
15849 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15850 version checking. */
15851
15852 if (cu->language == language_fortran
15853 && cu->producer && strstr (cu->producer, "GNU F77"))
15854 {
15855 return DW_ORD_row_major;
15856 }
15857
15858 switch (cu->language_defn->la_array_ordering)
15859 {
15860 case array_column_major:
15861 return DW_ORD_col_major;
15862 case array_row_major:
15863 default:
15864 return DW_ORD_row_major;
15865 };
15866 }
15867
15868 /* Extract all information from a DW_TAG_set_type DIE and put it in
15869 the DIE's type field. */
15870
15871 static struct type *
15872 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15873 {
15874 struct type *domain_type, *set_type;
15875 struct attribute *attr;
15876
15877 domain_type = die_type (die, cu);
15878
15879 /* The die_type call above may have already set the type for this DIE. */
15880 set_type = get_die_type (die, cu);
15881 if (set_type)
15882 return set_type;
15883
15884 set_type = create_set_type (NULL, domain_type);
15885
15886 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15887 if (attr != nullptr)
15888 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15889
15890 maybe_set_alignment (cu, die, set_type);
15891
15892 return set_die_type (die, set_type, cu);
15893 }
15894
15895 /* A helper for read_common_block that creates a locexpr baton.
15896 SYM is the symbol which we are marking as computed.
15897 COMMON_DIE is the DIE for the common block.
15898 COMMON_LOC is the location expression attribute for the common
15899 block itself.
15900 MEMBER_LOC is the location expression attribute for the particular
15901 member of the common block that we are processing.
15902 CU is the CU from which the above come. */
15903
15904 static void
15905 mark_common_block_symbol_computed (struct symbol *sym,
15906 struct die_info *common_die,
15907 struct attribute *common_loc,
15908 struct attribute *member_loc,
15909 struct dwarf2_cu *cu)
15910 {
15911 struct dwarf2_per_objfile *dwarf2_per_objfile
15912 = cu->per_cu->dwarf2_per_objfile;
15913 struct objfile *objfile = dwarf2_per_objfile->objfile;
15914 struct dwarf2_locexpr_baton *baton;
15915 gdb_byte *ptr;
15916 unsigned int cu_off;
15917 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15918 LONGEST offset = 0;
15919
15920 gdb_assert (common_loc && member_loc);
15921 gdb_assert (common_loc->form_is_block ());
15922 gdb_assert (member_loc->form_is_block ()
15923 || member_loc->form_is_constant ());
15924
15925 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15926 baton->per_cu = cu->per_cu;
15927 gdb_assert (baton->per_cu);
15928
15929 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15930
15931 if (member_loc->form_is_constant ())
15932 {
15933 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15934 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15935 }
15936 else
15937 baton->size += DW_BLOCK (member_loc)->size;
15938
15939 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15940 baton->data = ptr;
15941
15942 *ptr++ = DW_OP_call4;
15943 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15944 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15945 ptr += 4;
15946
15947 if (member_loc->form_is_constant ())
15948 {
15949 *ptr++ = DW_OP_addr;
15950 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15951 ptr += cu->header.addr_size;
15952 }
15953 else
15954 {
15955 /* We have to copy the data here, because DW_OP_call4 will only
15956 use a DW_AT_location attribute. */
15957 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15958 ptr += DW_BLOCK (member_loc)->size;
15959 }
15960
15961 *ptr++ = DW_OP_plus;
15962 gdb_assert (ptr - baton->data == baton->size);
15963
15964 SYMBOL_LOCATION_BATON (sym) = baton;
15965 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15966 }
15967
15968 /* Create appropriate locally-scoped variables for all the
15969 DW_TAG_common_block entries. Also create a struct common_block
15970 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15971 is used to separate the common blocks name namespace from regular
15972 variable names. */
15973
15974 static void
15975 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15976 {
15977 struct attribute *attr;
15978
15979 attr = dwarf2_attr (die, DW_AT_location, cu);
15980 if (attr != nullptr)
15981 {
15982 /* Support the .debug_loc offsets. */
15983 if (attr->form_is_block ())
15984 {
15985 /* Ok. */
15986 }
15987 else if (attr->form_is_section_offset ())
15988 {
15989 dwarf2_complex_location_expr_complaint ();
15990 attr = NULL;
15991 }
15992 else
15993 {
15994 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15995 "common block member");
15996 attr = NULL;
15997 }
15998 }
15999
16000 if (die->child != NULL)
16001 {
16002 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16003 struct die_info *child_die;
16004 size_t n_entries = 0, size;
16005 struct common_block *common_block;
16006 struct symbol *sym;
16007
16008 for (child_die = die->child;
16009 child_die && child_die->tag;
16010 child_die = sibling_die (child_die))
16011 ++n_entries;
16012
16013 size = (sizeof (struct common_block)
16014 + (n_entries - 1) * sizeof (struct symbol *));
16015 common_block
16016 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16017 size);
16018 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16019 common_block->n_entries = 0;
16020
16021 for (child_die = die->child;
16022 child_die && child_die->tag;
16023 child_die = sibling_die (child_die))
16024 {
16025 /* Create the symbol in the DW_TAG_common_block block in the current
16026 symbol scope. */
16027 sym = new_symbol (child_die, NULL, cu);
16028 if (sym != NULL)
16029 {
16030 struct attribute *member_loc;
16031
16032 common_block->contents[common_block->n_entries++] = sym;
16033
16034 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16035 cu);
16036 if (member_loc)
16037 {
16038 /* GDB has handled this for a long time, but it is
16039 not specified by DWARF. It seems to have been
16040 emitted by gfortran at least as recently as:
16041 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16042 complaint (_("Variable in common block has "
16043 "DW_AT_data_member_location "
16044 "- DIE at %s [in module %s]"),
16045 sect_offset_str (child_die->sect_off),
16046 objfile_name (objfile));
16047
16048 if (member_loc->form_is_section_offset ())
16049 dwarf2_complex_location_expr_complaint ();
16050 else if (member_loc->form_is_constant ()
16051 || member_loc->form_is_block ())
16052 {
16053 if (attr != nullptr)
16054 mark_common_block_symbol_computed (sym, die, attr,
16055 member_loc, cu);
16056 }
16057 else
16058 dwarf2_complex_location_expr_complaint ();
16059 }
16060 }
16061 }
16062
16063 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16064 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16065 }
16066 }
16067
16068 /* Create a type for a C++ namespace. */
16069
16070 static struct type *
16071 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16072 {
16073 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16074 const char *previous_prefix, *name;
16075 int is_anonymous;
16076 struct type *type;
16077
16078 /* For extensions, reuse the type of the original namespace. */
16079 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16080 {
16081 struct die_info *ext_die;
16082 struct dwarf2_cu *ext_cu = cu;
16083
16084 ext_die = dwarf2_extension (die, &ext_cu);
16085 type = read_type_die (ext_die, ext_cu);
16086
16087 /* EXT_CU may not be the same as CU.
16088 Ensure TYPE is recorded with CU in die_type_hash. */
16089 return set_die_type (die, type, cu);
16090 }
16091
16092 name = namespace_name (die, &is_anonymous, cu);
16093
16094 /* Now build the name of the current namespace. */
16095
16096 previous_prefix = determine_prefix (die, cu);
16097 if (previous_prefix[0] != '\0')
16098 name = typename_concat (&objfile->objfile_obstack,
16099 previous_prefix, name, 0, cu);
16100
16101 /* Create the type. */
16102 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16103
16104 return set_die_type (die, type, cu);
16105 }
16106
16107 /* Read a namespace scope. */
16108
16109 static void
16110 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16111 {
16112 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16113 int is_anonymous;
16114
16115 /* Add a symbol associated to this if we haven't seen the namespace
16116 before. Also, add a using directive if it's an anonymous
16117 namespace. */
16118
16119 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16120 {
16121 struct type *type;
16122
16123 type = read_type_die (die, cu);
16124 new_symbol (die, type, cu);
16125
16126 namespace_name (die, &is_anonymous, cu);
16127 if (is_anonymous)
16128 {
16129 const char *previous_prefix = determine_prefix (die, cu);
16130
16131 std::vector<const char *> excludes;
16132 add_using_directive (using_directives (cu),
16133 previous_prefix, TYPE_NAME (type), NULL,
16134 NULL, excludes, 0, &objfile->objfile_obstack);
16135 }
16136 }
16137
16138 if (die->child != NULL)
16139 {
16140 struct die_info *child_die = die->child;
16141
16142 while (child_die && child_die->tag)
16143 {
16144 process_die (child_die, cu);
16145 child_die = sibling_die (child_die);
16146 }
16147 }
16148 }
16149
16150 /* Read a Fortran module as type. This DIE can be only a declaration used for
16151 imported module. Still we need that type as local Fortran "use ... only"
16152 declaration imports depend on the created type in determine_prefix. */
16153
16154 static struct type *
16155 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16156 {
16157 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16158 const char *module_name;
16159 struct type *type;
16160
16161 module_name = dwarf2_name (die, cu);
16162 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16163
16164 return set_die_type (die, type, cu);
16165 }
16166
16167 /* Read a Fortran module. */
16168
16169 static void
16170 read_module (struct die_info *die, struct dwarf2_cu *cu)
16171 {
16172 struct die_info *child_die = die->child;
16173 struct type *type;
16174
16175 type = read_type_die (die, cu);
16176 new_symbol (die, type, cu);
16177
16178 while (child_die && child_die->tag)
16179 {
16180 process_die (child_die, cu);
16181 child_die = sibling_die (child_die);
16182 }
16183 }
16184
16185 /* Return the name of the namespace represented by DIE. Set
16186 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16187 namespace. */
16188
16189 static const char *
16190 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16191 {
16192 struct die_info *current_die;
16193 const char *name = NULL;
16194
16195 /* Loop through the extensions until we find a name. */
16196
16197 for (current_die = die;
16198 current_die != NULL;
16199 current_die = dwarf2_extension (die, &cu))
16200 {
16201 /* We don't use dwarf2_name here so that we can detect the absence
16202 of a name -> anonymous namespace. */
16203 name = dwarf2_string_attr (die, DW_AT_name, cu);
16204
16205 if (name != NULL)
16206 break;
16207 }
16208
16209 /* Is it an anonymous namespace? */
16210
16211 *is_anonymous = (name == NULL);
16212 if (*is_anonymous)
16213 name = CP_ANONYMOUS_NAMESPACE_STR;
16214
16215 return name;
16216 }
16217
16218 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16219 the user defined type vector. */
16220
16221 static struct type *
16222 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16223 {
16224 struct gdbarch *gdbarch
16225 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16226 struct comp_unit_head *cu_header = &cu->header;
16227 struct type *type;
16228 struct attribute *attr_byte_size;
16229 struct attribute *attr_address_class;
16230 int byte_size, addr_class;
16231 struct type *target_type;
16232
16233 target_type = die_type (die, cu);
16234
16235 /* The die_type call above may have already set the type for this DIE. */
16236 type = get_die_type (die, cu);
16237 if (type)
16238 return type;
16239
16240 type = lookup_pointer_type (target_type);
16241
16242 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16243 if (attr_byte_size)
16244 byte_size = DW_UNSND (attr_byte_size);
16245 else
16246 byte_size = cu_header->addr_size;
16247
16248 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16249 if (attr_address_class)
16250 addr_class = DW_UNSND (attr_address_class);
16251 else
16252 addr_class = DW_ADDR_none;
16253
16254 ULONGEST alignment = get_alignment (cu, die);
16255
16256 /* If the pointer size, alignment, or address class is different
16257 than the default, create a type variant marked as such and set
16258 the length accordingly. */
16259 if (TYPE_LENGTH (type) != byte_size
16260 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16261 && alignment != TYPE_RAW_ALIGN (type))
16262 || addr_class != DW_ADDR_none)
16263 {
16264 if (gdbarch_address_class_type_flags_p (gdbarch))
16265 {
16266 int type_flags;
16267
16268 type_flags = gdbarch_address_class_type_flags
16269 (gdbarch, byte_size, addr_class);
16270 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16271 == 0);
16272 type = make_type_with_address_space (type, type_flags);
16273 }
16274 else if (TYPE_LENGTH (type) != byte_size)
16275 {
16276 complaint (_("invalid pointer size %d"), byte_size);
16277 }
16278 else if (TYPE_RAW_ALIGN (type) != alignment)
16279 {
16280 complaint (_("Invalid DW_AT_alignment"
16281 " - DIE at %s [in module %s]"),
16282 sect_offset_str (die->sect_off),
16283 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16284 }
16285 else
16286 {
16287 /* Should we also complain about unhandled address classes? */
16288 }
16289 }
16290
16291 TYPE_LENGTH (type) = byte_size;
16292 set_type_align (type, alignment);
16293 return set_die_type (die, type, cu);
16294 }
16295
16296 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16297 the user defined type vector. */
16298
16299 static struct type *
16300 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16301 {
16302 struct type *type;
16303 struct type *to_type;
16304 struct type *domain;
16305
16306 to_type = die_type (die, cu);
16307 domain = die_containing_type (die, cu);
16308
16309 /* The calls above may have already set the type for this DIE. */
16310 type = get_die_type (die, cu);
16311 if (type)
16312 return type;
16313
16314 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16315 type = lookup_methodptr_type (to_type);
16316 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16317 {
16318 struct type *new_type
16319 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16320
16321 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16322 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16323 TYPE_VARARGS (to_type));
16324 type = lookup_methodptr_type (new_type);
16325 }
16326 else
16327 type = lookup_memberptr_type (to_type, domain);
16328
16329 return set_die_type (die, type, cu);
16330 }
16331
16332 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16333 the user defined type vector. */
16334
16335 static struct type *
16336 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16337 enum type_code refcode)
16338 {
16339 struct comp_unit_head *cu_header = &cu->header;
16340 struct type *type, *target_type;
16341 struct attribute *attr;
16342
16343 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16344
16345 target_type = die_type (die, cu);
16346
16347 /* The die_type call above may have already set the type for this DIE. */
16348 type = get_die_type (die, cu);
16349 if (type)
16350 return type;
16351
16352 type = lookup_reference_type (target_type, refcode);
16353 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16354 if (attr != nullptr)
16355 {
16356 TYPE_LENGTH (type) = DW_UNSND (attr);
16357 }
16358 else
16359 {
16360 TYPE_LENGTH (type) = cu_header->addr_size;
16361 }
16362 maybe_set_alignment (cu, die, type);
16363 return set_die_type (die, type, cu);
16364 }
16365
16366 /* Add the given cv-qualifiers to the element type of the array. GCC
16367 outputs DWARF type qualifiers that apply to an array, not the
16368 element type. But GDB relies on the array element type to carry
16369 the cv-qualifiers. This mimics section 6.7.3 of the C99
16370 specification. */
16371
16372 static struct type *
16373 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16374 struct type *base_type, int cnst, int voltl)
16375 {
16376 struct type *el_type, *inner_array;
16377
16378 base_type = copy_type (base_type);
16379 inner_array = base_type;
16380
16381 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16382 {
16383 TYPE_TARGET_TYPE (inner_array) =
16384 copy_type (TYPE_TARGET_TYPE (inner_array));
16385 inner_array = TYPE_TARGET_TYPE (inner_array);
16386 }
16387
16388 el_type = TYPE_TARGET_TYPE (inner_array);
16389 cnst |= TYPE_CONST (el_type);
16390 voltl |= TYPE_VOLATILE (el_type);
16391 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16392
16393 return set_die_type (die, base_type, cu);
16394 }
16395
16396 static struct type *
16397 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16398 {
16399 struct type *base_type, *cv_type;
16400
16401 base_type = die_type (die, cu);
16402
16403 /* The die_type call above may have already set the type for this DIE. */
16404 cv_type = get_die_type (die, cu);
16405 if (cv_type)
16406 return cv_type;
16407
16408 /* In case the const qualifier is applied to an array type, the element type
16409 is so qualified, not the array type (section 6.7.3 of C99). */
16410 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16411 return add_array_cv_type (die, cu, base_type, 1, 0);
16412
16413 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16414 return set_die_type (die, cv_type, cu);
16415 }
16416
16417 static struct type *
16418 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16419 {
16420 struct type *base_type, *cv_type;
16421
16422 base_type = die_type (die, cu);
16423
16424 /* The die_type call above may have already set the type for this DIE. */
16425 cv_type = get_die_type (die, cu);
16426 if (cv_type)
16427 return cv_type;
16428
16429 /* In case the volatile qualifier is applied to an array type, the
16430 element type is so qualified, not the array type (section 6.7.3
16431 of C99). */
16432 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16433 return add_array_cv_type (die, cu, base_type, 0, 1);
16434
16435 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16436 return set_die_type (die, cv_type, cu);
16437 }
16438
16439 /* Handle DW_TAG_restrict_type. */
16440
16441 static struct type *
16442 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16443 {
16444 struct type *base_type, *cv_type;
16445
16446 base_type = die_type (die, cu);
16447
16448 /* The die_type call above may have already set the type for this DIE. */
16449 cv_type = get_die_type (die, cu);
16450 if (cv_type)
16451 return cv_type;
16452
16453 cv_type = make_restrict_type (base_type);
16454 return set_die_type (die, cv_type, cu);
16455 }
16456
16457 /* Handle DW_TAG_atomic_type. */
16458
16459 static struct type *
16460 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16461 {
16462 struct type *base_type, *cv_type;
16463
16464 base_type = die_type (die, cu);
16465
16466 /* The die_type call above may have already set the type for this DIE. */
16467 cv_type = get_die_type (die, cu);
16468 if (cv_type)
16469 return cv_type;
16470
16471 cv_type = make_atomic_type (base_type);
16472 return set_die_type (die, cv_type, cu);
16473 }
16474
16475 /* Extract all information from a DW_TAG_string_type DIE and add to
16476 the user defined type vector. It isn't really a user defined type,
16477 but it behaves like one, with other DIE's using an AT_user_def_type
16478 attribute to reference it. */
16479
16480 static struct type *
16481 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16482 {
16483 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16484 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16485 struct type *type, *range_type, *index_type, *char_type;
16486 struct attribute *attr;
16487 struct dynamic_prop prop;
16488 bool length_is_constant = true;
16489 LONGEST length;
16490
16491 /* There are a couple of places where bit sizes might be made use of
16492 when parsing a DW_TAG_string_type, however, no producer that we know
16493 of make use of these. Handling bit sizes that are a multiple of the
16494 byte size is easy enough, but what about other bit sizes? Lets deal
16495 with that problem when we have to. Warn about these attributes being
16496 unsupported, then parse the type and ignore them like we always
16497 have. */
16498 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16499 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16500 {
16501 static bool warning_printed = false;
16502 if (!warning_printed)
16503 {
16504 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16505 "currently supported on DW_TAG_string_type."));
16506 warning_printed = true;
16507 }
16508 }
16509
16510 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16511 if (attr != nullptr && !attr->form_is_constant ())
16512 {
16513 /* The string length describes the location at which the length of
16514 the string can be found. The size of the length field can be
16515 specified with one of the attributes below. */
16516 struct type *prop_type;
16517 struct attribute *len
16518 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16519 if (len == nullptr)
16520 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16521 if (len != nullptr && len->form_is_constant ())
16522 {
16523 /* Pass 0 as the default as we know this attribute is constant
16524 and the default value will not be returned. */
16525 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16526 prop_type = cu->per_cu->int_type (sz, true);
16527 }
16528 else
16529 {
16530 /* If the size is not specified then we assume it is the size of
16531 an address on this target. */
16532 prop_type = cu->per_cu->addr_sized_int_type (true);
16533 }
16534
16535 /* Convert the attribute into a dynamic property. */
16536 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16537 length = 1;
16538 else
16539 length_is_constant = false;
16540 }
16541 else if (attr != nullptr)
16542 {
16543 /* This DW_AT_string_length just contains the length with no
16544 indirection. There's no need to create a dynamic property in this
16545 case. Pass 0 for the default value as we know it will not be
16546 returned in this case. */
16547 length = dwarf2_get_attr_constant_value (attr, 0);
16548 }
16549 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16550 {
16551 /* We don't currently support non-constant byte sizes for strings. */
16552 length = dwarf2_get_attr_constant_value (attr, 1);
16553 }
16554 else
16555 {
16556 /* Use 1 as a fallback length if we have nothing else. */
16557 length = 1;
16558 }
16559
16560 index_type = objfile_type (objfile)->builtin_int;
16561 if (length_is_constant)
16562 range_type = create_static_range_type (NULL, index_type, 1, length);
16563 else
16564 {
16565 struct dynamic_prop low_bound;
16566
16567 low_bound.kind = PROP_CONST;
16568 low_bound.data.const_val = 1;
16569 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16570 }
16571 char_type = language_string_char_type (cu->language_defn, gdbarch);
16572 type = create_string_type (NULL, char_type, range_type);
16573
16574 return set_die_type (die, type, cu);
16575 }
16576
16577 /* Assuming that DIE corresponds to a function, returns nonzero
16578 if the function is prototyped. */
16579
16580 static int
16581 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16582 {
16583 struct attribute *attr;
16584
16585 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16586 if (attr && (DW_UNSND (attr) != 0))
16587 return 1;
16588
16589 /* The DWARF standard implies that the DW_AT_prototyped attribute
16590 is only meaningful for C, but the concept also extends to other
16591 languages that allow unprototyped functions (Eg: Objective C).
16592 For all other languages, assume that functions are always
16593 prototyped. */
16594 if (cu->language != language_c
16595 && cu->language != language_objc
16596 && cu->language != language_opencl)
16597 return 1;
16598
16599 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16600 prototyped and unprototyped functions; default to prototyped,
16601 since that is more common in modern code (and RealView warns
16602 about unprototyped functions). */
16603 if (producer_is_realview (cu->producer))
16604 return 1;
16605
16606 return 0;
16607 }
16608
16609 /* Handle DIES due to C code like:
16610
16611 struct foo
16612 {
16613 int (*funcp)(int a, long l);
16614 int b;
16615 };
16616
16617 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16618
16619 static struct type *
16620 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16621 {
16622 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16623 struct type *type; /* Type that this function returns. */
16624 struct type *ftype; /* Function that returns above type. */
16625 struct attribute *attr;
16626
16627 type = die_type (die, cu);
16628
16629 /* The die_type call above may have already set the type for this DIE. */
16630 ftype = get_die_type (die, cu);
16631 if (ftype)
16632 return ftype;
16633
16634 ftype = lookup_function_type (type);
16635
16636 if (prototyped_function_p (die, cu))
16637 TYPE_PROTOTYPED (ftype) = 1;
16638
16639 /* Store the calling convention in the type if it's available in
16640 the subroutine die. Otherwise set the calling convention to
16641 the default value DW_CC_normal. */
16642 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16643 if (attr != nullptr
16644 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16645 TYPE_CALLING_CONVENTION (ftype)
16646 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16647 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16648 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16649 else
16650 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16651
16652 /* Record whether the function returns normally to its caller or not
16653 if the DWARF producer set that information. */
16654 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16655 if (attr && (DW_UNSND (attr) != 0))
16656 TYPE_NO_RETURN (ftype) = 1;
16657
16658 /* We need to add the subroutine type to the die immediately so
16659 we don't infinitely recurse when dealing with parameters
16660 declared as the same subroutine type. */
16661 set_die_type (die, ftype, cu);
16662
16663 if (die->child != NULL)
16664 {
16665 struct type *void_type = objfile_type (objfile)->builtin_void;
16666 struct die_info *child_die;
16667 int nparams, iparams;
16668
16669 /* Count the number of parameters.
16670 FIXME: GDB currently ignores vararg functions, but knows about
16671 vararg member functions. */
16672 nparams = 0;
16673 child_die = die->child;
16674 while (child_die && child_die->tag)
16675 {
16676 if (child_die->tag == DW_TAG_formal_parameter)
16677 nparams++;
16678 else if (child_die->tag == DW_TAG_unspecified_parameters)
16679 TYPE_VARARGS (ftype) = 1;
16680 child_die = sibling_die (child_die);
16681 }
16682
16683 /* Allocate storage for parameters and fill them in. */
16684 TYPE_NFIELDS (ftype) = nparams;
16685 TYPE_FIELDS (ftype) = (struct field *)
16686 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16687
16688 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16689 even if we error out during the parameters reading below. */
16690 for (iparams = 0; iparams < nparams; iparams++)
16691 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16692
16693 iparams = 0;
16694 child_die = die->child;
16695 while (child_die && child_die->tag)
16696 {
16697 if (child_die->tag == DW_TAG_formal_parameter)
16698 {
16699 struct type *arg_type;
16700
16701 /* DWARF version 2 has no clean way to discern C++
16702 static and non-static member functions. G++ helps
16703 GDB by marking the first parameter for non-static
16704 member functions (which is the this pointer) as
16705 artificial. We pass this information to
16706 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16707
16708 DWARF version 3 added DW_AT_object_pointer, which GCC
16709 4.5 does not yet generate. */
16710 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16711 if (attr != nullptr)
16712 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16713 else
16714 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16715 arg_type = die_type (child_die, cu);
16716
16717 /* RealView does not mark THIS as const, which the testsuite
16718 expects. GCC marks THIS as const in method definitions,
16719 but not in the class specifications (GCC PR 43053). */
16720 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16721 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16722 {
16723 int is_this = 0;
16724 struct dwarf2_cu *arg_cu = cu;
16725 const char *name = dwarf2_name (child_die, cu);
16726
16727 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16728 if (attr != nullptr)
16729 {
16730 /* If the compiler emits this, use it. */
16731 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16732 is_this = 1;
16733 }
16734 else if (name && strcmp (name, "this") == 0)
16735 /* Function definitions will have the argument names. */
16736 is_this = 1;
16737 else if (name == NULL && iparams == 0)
16738 /* Declarations may not have the names, so like
16739 elsewhere in GDB, assume an artificial first
16740 argument is "this". */
16741 is_this = 1;
16742
16743 if (is_this)
16744 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16745 arg_type, 0);
16746 }
16747
16748 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16749 iparams++;
16750 }
16751 child_die = sibling_die (child_die);
16752 }
16753 }
16754
16755 return ftype;
16756 }
16757
16758 static struct type *
16759 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16760 {
16761 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16762 const char *name = NULL;
16763 struct type *this_type, *target_type;
16764
16765 name = dwarf2_full_name (NULL, die, cu);
16766 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16767 TYPE_TARGET_STUB (this_type) = 1;
16768 set_die_type (die, this_type, cu);
16769 target_type = die_type (die, cu);
16770 if (target_type != this_type)
16771 TYPE_TARGET_TYPE (this_type) = target_type;
16772 else
16773 {
16774 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16775 spec and cause infinite loops in GDB. */
16776 complaint (_("Self-referential DW_TAG_typedef "
16777 "- DIE at %s [in module %s]"),
16778 sect_offset_str (die->sect_off), objfile_name (objfile));
16779 TYPE_TARGET_TYPE (this_type) = NULL;
16780 }
16781 return this_type;
16782 }
16783
16784 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16785 (which may be different from NAME) to the architecture back-end to allow
16786 it to guess the correct format if necessary. */
16787
16788 static struct type *
16789 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16790 const char *name_hint, enum bfd_endian byte_order)
16791 {
16792 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16793 const struct floatformat **format;
16794 struct type *type;
16795
16796 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16797 if (format)
16798 type = init_float_type (objfile, bits, name, format, byte_order);
16799 else
16800 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16801
16802 return type;
16803 }
16804
16805 /* Allocate an integer type of size BITS and name NAME. */
16806
16807 static struct type *
16808 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16809 int bits, int unsigned_p, const char *name)
16810 {
16811 struct type *type;
16812
16813 /* Versions of Intel's C Compiler generate an integer type called "void"
16814 instead of using DW_TAG_unspecified_type. This has been seen on
16815 at least versions 14, 17, and 18. */
16816 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16817 && strcmp (name, "void") == 0)
16818 type = objfile_type (objfile)->builtin_void;
16819 else
16820 type = init_integer_type (objfile, bits, unsigned_p, name);
16821
16822 return type;
16823 }
16824
16825 /* Initialise and return a floating point type of size BITS suitable for
16826 use as a component of a complex number. The NAME_HINT is passed through
16827 when initialising the floating point type and is the name of the complex
16828 type.
16829
16830 As DWARF doesn't currently provide an explicit name for the components
16831 of a complex number, but it can be helpful to have these components
16832 named, we try to select a suitable name based on the size of the
16833 component. */
16834 static struct type *
16835 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16836 struct objfile *objfile,
16837 int bits, const char *name_hint,
16838 enum bfd_endian byte_order)
16839 {
16840 gdbarch *gdbarch = get_objfile_arch (objfile);
16841 struct type *tt = nullptr;
16842
16843 /* Try to find a suitable floating point builtin type of size BITS.
16844 We're going to use the name of this type as the name for the complex
16845 target type that we are about to create. */
16846 switch (cu->language)
16847 {
16848 case language_fortran:
16849 switch (bits)
16850 {
16851 case 32:
16852 tt = builtin_f_type (gdbarch)->builtin_real;
16853 break;
16854 case 64:
16855 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16856 break;
16857 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16858 case 128:
16859 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16860 break;
16861 }
16862 break;
16863 default:
16864 switch (bits)
16865 {
16866 case 32:
16867 tt = builtin_type (gdbarch)->builtin_float;
16868 break;
16869 case 64:
16870 tt = builtin_type (gdbarch)->builtin_double;
16871 break;
16872 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16873 case 128:
16874 tt = builtin_type (gdbarch)->builtin_long_double;
16875 break;
16876 }
16877 break;
16878 }
16879
16880 /* If the type we found doesn't match the size we were looking for, then
16881 pretend we didn't find a type at all, the complex target type we
16882 create will then be nameless. */
16883 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16884 tt = nullptr;
16885
16886 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16887 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16888 }
16889
16890 /* Find a representation of a given base type and install
16891 it in the TYPE field of the die. */
16892
16893 static struct type *
16894 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16895 {
16896 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16897 struct type *type;
16898 struct attribute *attr;
16899 int encoding = 0, bits = 0;
16900 const char *name;
16901 gdbarch *arch;
16902
16903 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16904 if (attr != nullptr)
16905 encoding = DW_UNSND (attr);
16906 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16907 if (attr != nullptr)
16908 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16909 name = dwarf2_name (die, cu);
16910 if (!name)
16911 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16912
16913 arch = get_objfile_arch (objfile);
16914 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16915
16916 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16917 if (attr)
16918 {
16919 int endianity = DW_UNSND (attr);
16920
16921 switch (endianity)
16922 {
16923 case DW_END_big:
16924 byte_order = BFD_ENDIAN_BIG;
16925 break;
16926 case DW_END_little:
16927 byte_order = BFD_ENDIAN_LITTLE;
16928 break;
16929 default:
16930 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16931 break;
16932 }
16933 }
16934
16935 switch (encoding)
16936 {
16937 case DW_ATE_address:
16938 /* Turn DW_ATE_address into a void * pointer. */
16939 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16940 type = init_pointer_type (objfile, bits, name, type);
16941 break;
16942 case DW_ATE_boolean:
16943 type = init_boolean_type (objfile, bits, 1, name);
16944 break;
16945 case DW_ATE_complex_float:
16946 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16947 byte_order);
16948 type = init_complex_type (objfile, name, type);
16949 break;
16950 case DW_ATE_decimal_float:
16951 type = init_decfloat_type (objfile, bits, name);
16952 break;
16953 case DW_ATE_float:
16954 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16955 break;
16956 case DW_ATE_signed:
16957 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16958 break;
16959 case DW_ATE_unsigned:
16960 if (cu->language == language_fortran
16961 && name
16962 && startswith (name, "character("))
16963 type = init_character_type (objfile, bits, 1, name);
16964 else
16965 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16966 break;
16967 case DW_ATE_signed_char:
16968 if (cu->language == language_ada || cu->language == language_m2
16969 || cu->language == language_pascal
16970 || cu->language == language_fortran)
16971 type = init_character_type (objfile, bits, 0, name);
16972 else
16973 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16974 break;
16975 case DW_ATE_unsigned_char:
16976 if (cu->language == language_ada || cu->language == language_m2
16977 || cu->language == language_pascal
16978 || cu->language == language_fortran
16979 || cu->language == language_rust)
16980 type = init_character_type (objfile, bits, 1, name);
16981 else
16982 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16983 break;
16984 case DW_ATE_UTF:
16985 {
16986 if (bits == 16)
16987 type = builtin_type (arch)->builtin_char16;
16988 else if (bits == 32)
16989 type = builtin_type (arch)->builtin_char32;
16990 else
16991 {
16992 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
16993 bits);
16994 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16995 }
16996 return set_die_type (die, type, cu);
16997 }
16998 break;
16999
17000 default:
17001 complaint (_("unsupported DW_AT_encoding: '%s'"),
17002 dwarf_type_encoding_name (encoding));
17003 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17004 break;
17005 }
17006
17007 if (name && strcmp (name, "char") == 0)
17008 TYPE_NOSIGN (type) = 1;
17009
17010 maybe_set_alignment (cu, die, type);
17011
17012 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17013
17014 return set_die_type (die, type, cu);
17015 }
17016
17017 /* Parse dwarf attribute if it's a block, reference or constant and put the
17018 resulting value of the attribute into struct bound_prop.
17019 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17020
17021 static int
17022 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17023 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17024 struct type *default_type)
17025 {
17026 struct dwarf2_property_baton *baton;
17027 struct obstack *obstack
17028 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17029
17030 gdb_assert (default_type != NULL);
17031
17032 if (attr == NULL || prop == NULL)
17033 return 0;
17034
17035 if (attr->form_is_block ())
17036 {
17037 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17038 baton->property_type = default_type;
17039 baton->locexpr.per_cu = cu->per_cu;
17040 baton->locexpr.size = DW_BLOCK (attr)->size;
17041 baton->locexpr.data = DW_BLOCK (attr)->data;
17042 switch (attr->name)
17043 {
17044 case DW_AT_string_length:
17045 baton->locexpr.is_reference = true;
17046 break;
17047 default:
17048 baton->locexpr.is_reference = false;
17049 break;
17050 }
17051 prop->data.baton = baton;
17052 prop->kind = PROP_LOCEXPR;
17053 gdb_assert (prop->data.baton != NULL);
17054 }
17055 else if (attr->form_is_ref ())
17056 {
17057 struct dwarf2_cu *target_cu = cu;
17058 struct die_info *target_die;
17059 struct attribute *target_attr;
17060
17061 target_die = follow_die_ref (die, attr, &target_cu);
17062 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17063 if (target_attr == NULL)
17064 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17065 target_cu);
17066 if (target_attr == NULL)
17067 return 0;
17068
17069 switch (target_attr->name)
17070 {
17071 case DW_AT_location:
17072 if (target_attr->form_is_section_offset ())
17073 {
17074 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17075 baton->property_type = die_type (target_die, target_cu);
17076 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17077 prop->data.baton = baton;
17078 prop->kind = PROP_LOCLIST;
17079 gdb_assert (prop->data.baton != NULL);
17080 }
17081 else if (target_attr->form_is_block ())
17082 {
17083 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17084 baton->property_type = die_type (target_die, target_cu);
17085 baton->locexpr.per_cu = cu->per_cu;
17086 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17087 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17088 baton->locexpr.is_reference = true;
17089 prop->data.baton = baton;
17090 prop->kind = PROP_LOCEXPR;
17091 gdb_assert (prop->data.baton != NULL);
17092 }
17093 else
17094 {
17095 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17096 "dynamic property");
17097 return 0;
17098 }
17099 break;
17100 case DW_AT_data_member_location:
17101 {
17102 LONGEST offset;
17103
17104 if (!handle_data_member_location (target_die, target_cu,
17105 &offset))
17106 return 0;
17107
17108 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17109 baton->property_type = read_type_die (target_die->parent,
17110 target_cu);
17111 baton->offset_info.offset = offset;
17112 baton->offset_info.type = die_type (target_die, target_cu);
17113 prop->data.baton = baton;
17114 prop->kind = PROP_ADDR_OFFSET;
17115 break;
17116 }
17117 }
17118 }
17119 else if (attr->form_is_constant ())
17120 {
17121 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17122 prop->kind = PROP_CONST;
17123 }
17124 else
17125 {
17126 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17127 dwarf2_name (die, cu));
17128 return 0;
17129 }
17130
17131 return 1;
17132 }
17133
17134 /* See read.h. */
17135
17136 struct type *
17137 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17138 {
17139 struct objfile *objfile = dwarf2_per_objfile->objfile;
17140 struct type *int_type;
17141
17142 /* Helper macro to examine the various builtin types. */
17143 #define TRY_TYPE(F) \
17144 int_type = (unsigned_p \
17145 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17146 : objfile_type (objfile)->builtin_ ## F); \
17147 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17148 return int_type
17149
17150 TRY_TYPE (char);
17151 TRY_TYPE (short);
17152 TRY_TYPE (int);
17153 TRY_TYPE (long);
17154 TRY_TYPE (long_long);
17155
17156 #undef TRY_TYPE
17157
17158 gdb_assert_not_reached ("unable to find suitable integer type");
17159 }
17160
17161 /* See read.h. */
17162
17163 struct type *
17164 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17165 {
17166 int addr_size = this->addr_size ();
17167 return int_type (addr_size, unsigned_p);
17168 }
17169
17170 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17171 present (which is valid) then compute the default type based on the
17172 compilation units address size. */
17173
17174 static struct type *
17175 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17176 {
17177 struct type *index_type = die_type (die, cu);
17178
17179 /* Dwarf-2 specifications explicitly allows to create subrange types
17180 without specifying a base type.
17181 In that case, the base type must be set to the type of
17182 the lower bound, upper bound or count, in that order, if any of these
17183 three attributes references an object that has a type.
17184 If no base type is found, the Dwarf-2 specifications say that
17185 a signed integer type of size equal to the size of an address should
17186 be used.
17187 For the following C code: `extern char gdb_int [];'
17188 GCC produces an empty range DIE.
17189 FIXME: muller/2010-05-28: Possible references to object for low bound,
17190 high bound or count are not yet handled by this code. */
17191 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17192 index_type = cu->per_cu->addr_sized_int_type (false);
17193
17194 return index_type;
17195 }
17196
17197 /* Read the given DW_AT_subrange DIE. */
17198
17199 static struct type *
17200 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17201 {
17202 struct type *base_type, *orig_base_type;
17203 struct type *range_type;
17204 struct attribute *attr;
17205 struct dynamic_prop low, high;
17206 int low_default_is_valid;
17207 int high_bound_is_count = 0;
17208 const char *name;
17209 ULONGEST negative_mask;
17210
17211 orig_base_type = read_subrange_index_type (die, cu);
17212
17213 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17214 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17215 creating the range type, but we use the result of check_typedef
17216 when examining properties of the type. */
17217 base_type = check_typedef (orig_base_type);
17218
17219 /* The die_type call above may have already set the type for this DIE. */
17220 range_type = get_die_type (die, cu);
17221 if (range_type)
17222 return range_type;
17223
17224 low.kind = PROP_CONST;
17225 high.kind = PROP_CONST;
17226 high.data.const_val = 0;
17227
17228 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17229 omitting DW_AT_lower_bound. */
17230 switch (cu->language)
17231 {
17232 case language_c:
17233 case language_cplus:
17234 low.data.const_val = 0;
17235 low_default_is_valid = 1;
17236 break;
17237 case language_fortran:
17238 low.data.const_val = 1;
17239 low_default_is_valid = 1;
17240 break;
17241 case language_d:
17242 case language_objc:
17243 case language_rust:
17244 low.data.const_val = 0;
17245 low_default_is_valid = (cu->header.version >= 4);
17246 break;
17247 case language_ada:
17248 case language_m2:
17249 case language_pascal:
17250 low.data.const_val = 1;
17251 low_default_is_valid = (cu->header.version >= 4);
17252 break;
17253 default:
17254 low.data.const_val = 0;
17255 low_default_is_valid = 0;
17256 break;
17257 }
17258
17259 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17260 if (attr != nullptr)
17261 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17262 else if (!low_default_is_valid)
17263 complaint (_("Missing DW_AT_lower_bound "
17264 "- DIE at %s [in module %s]"),
17265 sect_offset_str (die->sect_off),
17266 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17267
17268 struct attribute *attr_ub, *attr_count;
17269 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17270 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17271 {
17272 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17273 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17274 {
17275 /* If bounds are constant do the final calculation here. */
17276 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17277 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17278 else
17279 high_bound_is_count = 1;
17280 }
17281 else
17282 {
17283 if (attr_ub != NULL)
17284 complaint (_("Unresolved DW_AT_upper_bound "
17285 "- DIE at %s [in module %s]"),
17286 sect_offset_str (die->sect_off),
17287 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17288 if (attr_count != NULL)
17289 complaint (_("Unresolved DW_AT_count "
17290 "- DIE at %s [in module %s]"),
17291 sect_offset_str (die->sect_off),
17292 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17293 }
17294 }
17295
17296 LONGEST bias = 0;
17297 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17298 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17299 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17300
17301 /* Normally, the DWARF producers are expected to use a signed
17302 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17303 But this is unfortunately not always the case, as witnessed
17304 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17305 is used instead. To work around that ambiguity, we treat
17306 the bounds as signed, and thus sign-extend their values, when
17307 the base type is signed. */
17308 negative_mask =
17309 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17310 if (low.kind == PROP_CONST
17311 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17312 low.data.const_val |= negative_mask;
17313 if (high.kind == PROP_CONST
17314 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17315 high.data.const_val |= negative_mask;
17316
17317 /* Check for bit and byte strides. */
17318 struct dynamic_prop byte_stride_prop;
17319 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17320 if (attr_byte_stride != nullptr)
17321 {
17322 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17323 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17324 prop_type);
17325 }
17326
17327 struct dynamic_prop bit_stride_prop;
17328 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17329 if (attr_bit_stride != nullptr)
17330 {
17331 /* It only makes sense to have either a bit or byte stride. */
17332 if (attr_byte_stride != nullptr)
17333 {
17334 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17335 "- DIE at %s [in module %s]"),
17336 sect_offset_str (die->sect_off),
17337 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17338 attr_bit_stride = nullptr;
17339 }
17340 else
17341 {
17342 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17343 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17344 prop_type);
17345 }
17346 }
17347
17348 if (attr_byte_stride != nullptr
17349 || attr_bit_stride != nullptr)
17350 {
17351 bool byte_stride_p = (attr_byte_stride != nullptr);
17352 struct dynamic_prop *stride
17353 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17354
17355 range_type
17356 = create_range_type_with_stride (NULL, orig_base_type, &low,
17357 &high, bias, stride, byte_stride_p);
17358 }
17359 else
17360 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17361
17362 if (high_bound_is_count)
17363 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17364
17365 /* Ada expects an empty array on no boundary attributes. */
17366 if (attr == NULL && cu->language != language_ada)
17367 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17368
17369 name = dwarf2_name (die, cu);
17370 if (name)
17371 TYPE_NAME (range_type) = name;
17372
17373 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17374 if (attr != nullptr)
17375 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17376
17377 maybe_set_alignment (cu, die, range_type);
17378
17379 set_die_type (die, range_type, cu);
17380
17381 /* set_die_type should be already done. */
17382 set_descriptive_type (range_type, die, cu);
17383
17384 return range_type;
17385 }
17386
17387 static struct type *
17388 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17389 {
17390 struct type *type;
17391
17392 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17393 NULL);
17394 TYPE_NAME (type) = dwarf2_name (die, cu);
17395
17396 /* In Ada, an unspecified type is typically used when the description
17397 of the type is deferred to a different unit. When encountering
17398 such a type, we treat it as a stub, and try to resolve it later on,
17399 when needed. */
17400 if (cu->language == language_ada)
17401 TYPE_STUB (type) = 1;
17402
17403 return set_die_type (die, type, cu);
17404 }
17405
17406 /* Read a single die and all its descendents. Set the die's sibling
17407 field to NULL; set other fields in the die correctly, and set all
17408 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17409 location of the info_ptr after reading all of those dies. PARENT
17410 is the parent of the die in question. */
17411
17412 static struct die_info *
17413 read_die_and_children (const struct die_reader_specs *reader,
17414 const gdb_byte *info_ptr,
17415 const gdb_byte **new_info_ptr,
17416 struct die_info *parent)
17417 {
17418 struct die_info *die;
17419 const gdb_byte *cur_ptr;
17420
17421 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17422 if (die == NULL)
17423 {
17424 *new_info_ptr = cur_ptr;
17425 return NULL;
17426 }
17427 store_in_ref_table (die, reader->cu);
17428
17429 if (die->has_children)
17430 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17431 else
17432 {
17433 die->child = NULL;
17434 *new_info_ptr = cur_ptr;
17435 }
17436
17437 die->sibling = NULL;
17438 die->parent = parent;
17439 return die;
17440 }
17441
17442 /* Read a die, all of its descendents, and all of its siblings; set
17443 all of the fields of all of the dies correctly. Arguments are as
17444 in read_die_and_children. */
17445
17446 static struct die_info *
17447 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17448 const gdb_byte *info_ptr,
17449 const gdb_byte **new_info_ptr,
17450 struct die_info *parent)
17451 {
17452 struct die_info *first_die, *last_sibling;
17453 const gdb_byte *cur_ptr;
17454
17455 cur_ptr = info_ptr;
17456 first_die = last_sibling = NULL;
17457
17458 while (1)
17459 {
17460 struct die_info *die
17461 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17462
17463 if (die == NULL)
17464 {
17465 *new_info_ptr = cur_ptr;
17466 return first_die;
17467 }
17468
17469 if (!first_die)
17470 first_die = die;
17471 else
17472 last_sibling->sibling = die;
17473
17474 last_sibling = die;
17475 }
17476 }
17477
17478 /* Read a die, all of its descendents, and all of its siblings; set
17479 all of the fields of all of the dies correctly. Arguments are as
17480 in read_die_and_children.
17481 This the main entry point for reading a DIE and all its children. */
17482
17483 static struct die_info *
17484 read_die_and_siblings (const struct die_reader_specs *reader,
17485 const gdb_byte *info_ptr,
17486 const gdb_byte **new_info_ptr,
17487 struct die_info *parent)
17488 {
17489 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17490 new_info_ptr, parent);
17491
17492 if (dwarf_die_debug)
17493 {
17494 fprintf_unfiltered (gdb_stdlog,
17495 "Read die from %s@0x%x of %s:\n",
17496 reader->die_section->get_name (),
17497 (unsigned) (info_ptr - reader->die_section->buffer),
17498 bfd_get_filename (reader->abfd));
17499 dump_die (die, dwarf_die_debug);
17500 }
17501
17502 return die;
17503 }
17504
17505 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17506 attributes.
17507 The caller is responsible for filling in the extra attributes
17508 and updating (*DIEP)->num_attrs.
17509 Set DIEP to point to a newly allocated die with its information,
17510 except for its child, sibling, and parent fields. */
17511
17512 static const gdb_byte *
17513 read_full_die_1 (const struct die_reader_specs *reader,
17514 struct die_info **diep, const gdb_byte *info_ptr,
17515 int num_extra_attrs)
17516 {
17517 unsigned int abbrev_number, bytes_read, i;
17518 struct abbrev_info *abbrev;
17519 struct die_info *die;
17520 struct dwarf2_cu *cu = reader->cu;
17521 bfd *abfd = reader->abfd;
17522
17523 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17524 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17525 info_ptr += bytes_read;
17526 if (!abbrev_number)
17527 {
17528 *diep = NULL;
17529 return info_ptr;
17530 }
17531
17532 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17533 if (!abbrev)
17534 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17535 abbrev_number,
17536 bfd_get_filename (abfd));
17537
17538 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17539 die->sect_off = sect_off;
17540 die->tag = abbrev->tag;
17541 die->abbrev = abbrev_number;
17542 die->has_children = abbrev->has_children;
17543
17544 /* Make the result usable.
17545 The caller needs to update num_attrs after adding the extra
17546 attributes. */
17547 die->num_attrs = abbrev->num_attrs;
17548
17549 std::vector<int> indexes_that_need_reprocess;
17550 for (i = 0; i < abbrev->num_attrs; ++i)
17551 {
17552 bool need_reprocess;
17553 info_ptr =
17554 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17555 info_ptr, &need_reprocess);
17556 if (need_reprocess)
17557 indexes_that_need_reprocess.push_back (i);
17558 }
17559
17560 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17561 if (attr != nullptr)
17562 cu->str_offsets_base = DW_UNSND (attr);
17563
17564 auto maybe_addr_base = lookup_addr_base(die);
17565 if (maybe_addr_base.has_value ())
17566 cu->addr_base = *maybe_addr_base;
17567 for (int index : indexes_that_need_reprocess)
17568 read_attribute_reprocess (reader, &die->attrs[index]);
17569 *diep = die;
17570 return info_ptr;
17571 }
17572
17573 /* Read a die and all its attributes.
17574 Set DIEP to point to a newly allocated die with its information,
17575 except for its child, sibling, and parent fields. */
17576
17577 static const gdb_byte *
17578 read_full_die (const struct die_reader_specs *reader,
17579 struct die_info **diep, const gdb_byte *info_ptr)
17580 {
17581 const gdb_byte *result;
17582
17583 result = read_full_die_1 (reader, diep, info_ptr, 0);
17584
17585 if (dwarf_die_debug)
17586 {
17587 fprintf_unfiltered (gdb_stdlog,
17588 "Read die from %s@0x%x of %s:\n",
17589 reader->die_section->get_name (),
17590 (unsigned) (info_ptr - reader->die_section->buffer),
17591 bfd_get_filename (reader->abfd));
17592 dump_die (*diep, dwarf_die_debug);
17593 }
17594
17595 return result;
17596 }
17597 \f
17598
17599 /* Returns nonzero if TAG represents a type that we might generate a partial
17600 symbol for. */
17601
17602 static int
17603 is_type_tag_for_partial (int tag)
17604 {
17605 switch (tag)
17606 {
17607 #if 0
17608 /* Some types that would be reasonable to generate partial symbols for,
17609 that we don't at present. */
17610 case DW_TAG_array_type:
17611 case DW_TAG_file_type:
17612 case DW_TAG_ptr_to_member_type:
17613 case DW_TAG_set_type:
17614 case DW_TAG_string_type:
17615 case DW_TAG_subroutine_type:
17616 #endif
17617 case DW_TAG_base_type:
17618 case DW_TAG_class_type:
17619 case DW_TAG_interface_type:
17620 case DW_TAG_enumeration_type:
17621 case DW_TAG_structure_type:
17622 case DW_TAG_subrange_type:
17623 case DW_TAG_typedef:
17624 case DW_TAG_union_type:
17625 return 1;
17626 default:
17627 return 0;
17628 }
17629 }
17630
17631 /* Load all DIEs that are interesting for partial symbols into memory. */
17632
17633 static struct partial_die_info *
17634 load_partial_dies (const struct die_reader_specs *reader,
17635 const gdb_byte *info_ptr, int building_psymtab)
17636 {
17637 struct dwarf2_cu *cu = reader->cu;
17638 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17639 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17640 unsigned int bytes_read;
17641 unsigned int load_all = 0;
17642 int nesting_level = 1;
17643
17644 parent_die = NULL;
17645 last_die = NULL;
17646
17647 gdb_assert (cu->per_cu != NULL);
17648 if (cu->per_cu->load_all_dies)
17649 load_all = 1;
17650
17651 cu->partial_dies
17652 = htab_create_alloc_ex (cu->header.length / 12,
17653 partial_die_hash,
17654 partial_die_eq,
17655 NULL,
17656 &cu->comp_unit_obstack,
17657 hashtab_obstack_allocate,
17658 dummy_obstack_deallocate);
17659
17660 while (1)
17661 {
17662 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17663
17664 /* A NULL abbrev means the end of a series of children. */
17665 if (abbrev == NULL)
17666 {
17667 if (--nesting_level == 0)
17668 return first_die;
17669
17670 info_ptr += bytes_read;
17671 last_die = parent_die;
17672 parent_die = parent_die->die_parent;
17673 continue;
17674 }
17675
17676 /* Check for template arguments. We never save these; if
17677 they're seen, we just mark the parent, and go on our way. */
17678 if (parent_die != NULL
17679 && cu->language == language_cplus
17680 && (abbrev->tag == DW_TAG_template_type_param
17681 || abbrev->tag == DW_TAG_template_value_param))
17682 {
17683 parent_die->has_template_arguments = 1;
17684
17685 if (!load_all)
17686 {
17687 /* We don't need a partial DIE for the template argument. */
17688 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17689 continue;
17690 }
17691 }
17692
17693 /* We only recurse into c++ subprograms looking for template arguments.
17694 Skip their other children. */
17695 if (!load_all
17696 && cu->language == language_cplus
17697 && parent_die != NULL
17698 && parent_die->tag == DW_TAG_subprogram)
17699 {
17700 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17701 continue;
17702 }
17703
17704 /* Check whether this DIE is interesting enough to save. Normally
17705 we would not be interested in members here, but there may be
17706 later variables referencing them via DW_AT_specification (for
17707 static members). */
17708 if (!load_all
17709 && !is_type_tag_for_partial (abbrev->tag)
17710 && abbrev->tag != DW_TAG_constant
17711 && abbrev->tag != DW_TAG_enumerator
17712 && abbrev->tag != DW_TAG_subprogram
17713 && abbrev->tag != DW_TAG_inlined_subroutine
17714 && abbrev->tag != DW_TAG_lexical_block
17715 && abbrev->tag != DW_TAG_variable
17716 && abbrev->tag != DW_TAG_namespace
17717 && abbrev->tag != DW_TAG_module
17718 && abbrev->tag != DW_TAG_member
17719 && abbrev->tag != DW_TAG_imported_unit
17720 && abbrev->tag != DW_TAG_imported_declaration)
17721 {
17722 /* Otherwise we skip to the next sibling, if any. */
17723 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17724 continue;
17725 }
17726
17727 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17728 abbrev);
17729
17730 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17731
17732 /* This two-pass algorithm for processing partial symbols has a
17733 high cost in cache pressure. Thus, handle some simple cases
17734 here which cover the majority of C partial symbols. DIEs
17735 which neither have specification tags in them, nor could have
17736 specification tags elsewhere pointing at them, can simply be
17737 processed and discarded.
17738
17739 This segment is also optional; scan_partial_symbols and
17740 add_partial_symbol will handle these DIEs if we chain
17741 them in normally. When compilers which do not emit large
17742 quantities of duplicate debug information are more common,
17743 this code can probably be removed. */
17744
17745 /* Any complete simple types at the top level (pretty much all
17746 of them, for a language without namespaces), can be processed
17747 directly. */
17748 if (parent_die == NULL
17749 && pdi.has_specification == 0
17750 && pdi.is_declaration == 0
17751 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17752 || pdi.tag == DW_TAG_base_type
17753 || pdi.tag == DW_TAG_subrange_type))
17754 {
17755 if (building_psymtab && pdi.name != NULL)
17756 add_psymbol_to_list (pdi.name, false,
17757 VAR_DOMAIN, LOC_TYPEDEF, -1,
17758 psymbol_placement::STATIC,
17759 0, cu->language, objfile);
17760 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17761 continue;
17762 }
17763
17764 /* The exception for DW_TAG_typedef with has_children above is
17765 a workaround of GCC PR debug/47510. In the case of this complaint
17766 type_name_or_error will error on such types later.
17767
17768 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17769 it could not find the child DIEs referenced later, this is checked
17770 above. In correct DWARF DW_TAG_typedef should have no children. */
17771
17772 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17773 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17774 "- DIE at %s [in module %s]"),
17775 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17776
17777 /* If we're at the second level, and we're an enumerator, and
17778 our parent has no specification (meaning possibly lives in a
17779 namespace elsewhere), then we can add the partial symbol now
17780 instead of queueing it. */
17781 if (pdi.tag == DW_TAG_enumerator
17782 && parent_die != NULL
17783 && parent_die->die_parent == NULL
17784 && parent_die->tag == DW_TAG_enumeration_type
17785 && parent_die->has_specification == 0)
17786 {
17787 if (pdi.name == NULL)
17788 complaint (_("malformed enumerator DIE ignored"));
17789 else if (building_psymtab)
17790 add_psymbol_to_list (pdi.name, false,
17791 VAR_DOMAIN, LOC_CONST, -1,
17792 cu->language == language_cplus
17793 ? psymbol_placement::GLOBAL
17794 : psymbol_placement::STATIC,
17795 0, cu->language, objfile);
17796
17797 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17798 continue;
17799 }
17800
17801 struct partial_die_info *part_die
17802 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17803
17804 /* We'll save this DIE so link it in. */
17805 part_die->die_parent = parent_die;
17806 part_die->die_sibling = NULL;
17807 part_die->die_child = NULL;
17808
17809 if (last_die && last_die == parent_die)
17810 last_die->die_child = part_die;
17811 else if (last_die)
17812 last_die->die_sibling = part_die;
17813
17814 last_die = part_die;
17815
17816 if (first_die == NULL)
17817 first_die = part_die;
17818
17819 /* Maybe add the DIE to the hash table. Not all DIEs that we
17820 find interesting need to be in the hash table, because we
17821 also have the parent/sibling/child chains; only those that we
17822 might refer to by offset later during partial symbol reading.
17823
17824 For now this means things that might have be the target of a
17825 DW_AT_specification, DW_AT_abstract_origin, or
17826 DW_AT_extension. DW_AT_extension will refer only to
17827 namespaces; DW_AT_abstract_origin refers to functions (and
17828 many things under the function DIE, but we do not recurse
17829 into function DIEs during partial symbol reading) and
17830 possibly variables as well; DW_AT_specification refers to
17831 declarations. Declarations ought to have the DW_AT_declaration
17832 flag. It happens that GCC forgets to put it in sometimes, but
17833 only for functions, not for types.
17834
17835 Adding more things than necessary to the hash table is harmless
17836 except for the performance cost. Adding too few will result in
17837 wasted time in find_partial_die, when we reread the compilation
17838 unit with load_all_dies set. */
17839
17840 if (load_all
17841 || abbrev->tag == DW_TAG_constant
17842 || abbrev->tag == DW_TAG_subprogram
17843 || abbrev->tag == DW_TAG_variable
17844 || abbrev->tag == DW_TAG_namespace
17845 || part_die->is_declaration)
17846 {
17847 void **slot;
17848
17849 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17850 to_underlying (part_die->sect_off),
17851 INSERT);
17852 *slot = part_die;
17853 }
17854
17855 /* For some DIEs we want to follow their children (if any). For C
17856 we have no reason to follow the children of structures; for other
17857 languages we have to, so that we can get at method physnames
17858 to infer fully qualified class names, for DW_AT_specification,
17859 and for C++ template arguments. For C++, we also look one level
17860 inside functions to find template arguments (if the name of the
17861 function does not already contain the template arguments).
17862
17863 For Ada and Fortran, we need to scan the children of subprograms
17864 and lexical blocks as well because these languages allow the
17865 definition of nested entities that could be interesting for the
17866 debugger, such as nested subprograms for instance. */
17867 if (last_die->has_children
17868 && (load_all
17869 || last_die->tag == DW_TAG_namespace
17870 || last_die->tag == DW_TAG_module
17871 || last_die->tag == DW_TAG_enumeration_type
17872 || (cu->language == language_cplus
17873 && last_die->tag == DW_TAG_subprogram
17874 && (last_die->name == NULL
17875 || strchr (last_die->name, '<') == NULL))
17876 || (cu->language != language_c
17877 && (last_die->tag == DW_TAG_class_type
17878 || last_die->tag == DW_TAG_interface_type
17879 || last_die->tag == DW_TAG_structure_type
17880 || last_die->tag == DW_TAG_union_type))
17881 || ((cu->language == language_ada
17882 || cu->language == language_fortran)
17883 && (last_die->tag == DW_TAG_subprogram
17884 || last_die->tag == DW_TAG_lexical_block))))
17885 {
17886 nesting_level++;
17887 parent_die = last_die;
17888 continue;
17889 }
17890
17891 /* Otherwise we skip to the next sibling, if any. */
17892 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17893
17894 /* Back to the top, do it again. */
17895 }
17896 }
17897
17898 partial_die_info::partial_die_info (sect_offset sect_off_,
17899 struct abbrev_info *abbrev)
17900 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17901 {
17902 }
17903
17904 /* Read a minimal amount of information into the minimal die structure.
17905 INFO_PTR should point just after the initial uleb128 of a DIE. */
17906
17907 const gdb_byte *
17908 partial_die_info::read (const struct die_reader_specs *reader,
17909 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17910 {
17911 struct dwarf2_cu *cu = reader->cu;
17912 struct dwarf2_per_objfile *dwarf2_per_objfile
17913 = cu->per_cu->dwarf2_per_objfile;
17914 unsigned int i;
17915 int has_low_pc_attr = 0;
17916 int has_high_pc_attr = 0;
17917 int high_pc_relative = 0;
17918
17919 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17920 for (i = 0; i < abbrev.num_attrs; ++i)
17921 {
17922 bool need_reprocess;
17923 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17924 info_ptr, &need_reprocess);
17925 /* String and address offsets that need to do the reprocessing have
17926 already been read at this point, so there is no need to wait until
17927 the loop terminates to do the reprocessing. */
17928 if (need_reprocess)
17929 read_attribute_reprocess (reader, &attr_vec[i]);
17930 attribute &attr = attr_vec[i];
17931 /* Store the data if it is of an attribute we want to keep in a
17932 partial symbol table. */
17933 switch (attr.name)
17934 {
17935 case DW_AT_name:
17936 switch (tag)
17937 {
17938 case DW_TAG_compile_unit:
17939 case DW_TAG_partial_unit:
17940 case DW_TAG_type_unit:
17941 /* Compilation units have a DW_AT_name that is a filename, not
17942 a source language identifier. */
17943 case DW_TAG_enumeration_type:
17944 case DW_TAG_enumerator:
17945 /* These tags always have simple identifiers already; no need
17946 to canonicalize them. */
17947 name = DW_STRING (&attr);
17948 break;
17949 default:
17950 {
17951 struct objfile *objfile = dwarf2_per_objfile->objfile;
17952
17953 name
17954 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
17955 &objfile->per_bfd->storage_obstack);
17956 }
17957 break;
17958 }
17959 break;
17960 case DW_AT_linkage_name:
17961 case DW_AT_MIPS_linkage_name:
17962 /* Note that both forms of linkage name might appear. We
17963 assume they will be the same, and we only store the last
17964 one we see. */
17965 linkage_name = DW_STRING (&attr);
17966 break;
17967 case DW_AT_low_pc:
17968 has_low_pc_attr = 1;
17969 lowpc = attr.value_as_address ();
17970 break;
17971 case DW_AT_high_pc:
17972 has_high_pc_attr = 1;
17973 highpc = attr.value_as_address ();
17974 if (cu->header.version >= 4 && attr.form_is_constant ())
17975 high_pc_relative = 1;
17976 break;
17977 case DW_AT_location:
17978 /* Support the .debug_loc offsets. */
17979 if (attr.form_is_block ())
17980 {
17981 d.locdesc = DW_BLOCK (&attr);
17982 }
17983 else if (attr.form_is_section_offset ())
17984 {
17985 dwarf2_complex_location_expr_complaint ();
17986 }
17987 else
17988 {
17989 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17990 "partial symbol information");
17991 }
17992 break;
17993 case DW_AT_external:
17994 is_external = DW_UNSND (&attr);
17995 break;
17996 case DW_AT_declaration:
17997 is_declaration = DW_UNSND (&attr);
17998 break;
17999 case DW_AT_type:
18000 has_type = 1;
18001 break;
18002 case DW_AT_abstract_origin:
18003 case DW_AT_specification:
18004 case DW_AT_extension:
18005 has_specification = 1;
18006 spec_offset = dwarf2_get_ref_die_offset (&attr);
18007 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18008 || cu->per_cu->is_dwz);
18009 break;
18010 case DW_AT_sibling:
18011 /* Ignore absolute siblings, they might point outside of
18012 the current compile unit. */
18013 if (attr.form == DW_FORM_ref_addr)
18014 complaint (_("ignoring absolute DW_AT_sibling"));
18015 else
18016 {
18017 const gdb_byte *buffer = reader->buffer;
18018 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18019 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18020
18021 if (sibling_ptr < info_ptr)
18022 complaint (_("DW_AT_sibling points backwards"));
18023 else if (sibling_ptr > reader->buffer_end)
18024 dwarf2_section_buffer_overflow_complaint (reader->die_section);
18025 else
18026 sibling = sibling_ptr;
18027 }
18028 break;
18029 case DW_AT_byte_size:
18030 has_byte_size = 1;
18031 break;
18032 case DW_AT_const_value:
18033 has_const_value = 1;
18034 break;
18035 case DW_AT_calling_convention:
18036 /* DWARF doesn't provide a way to identify a program's source-level
18037 entry point. DW_AT_calling_convention attributes are only meant
18038 to describe functions' calling conventions.
18039
18040 However, because it's a necessary piece of information in
18041 Fortran, and before DWARF 4 DW_CC_program was the only
18042 piece of debugging information whose definition refers to
18043 a 'main program' at all, several compilers marked Fortran
18044 main programs with DW_CC_program --- even when those
18045 functions use the standard calling conventions.
18046
18047 Although DWARF now specifies a way to provide this
18048 information, we support this practice for backward
18049 compatibility. */
18050 if (DW_UNSND (&attr) == DW_CC_program
18051 && cu->language == language_fortran)
18052 main_subprogram = 1;
18053 break;
18054 case DW_AT_inline:
18055 if (DW_UNSND (&attr) == DW_INL_inlined
18056 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18057 may_be_inlined = 1;
18058 break;
18059
18060 case DW_AT_import:
18061 if (tag == DW_TAG_imported_unit)
18062 {
18063 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18064 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18065 || cu->per_cu->is_dwz);
18066 }
18067 break;
18068
18069 case DW_AT_main_subprogram:
18070 main_subprogram = DW_UNSND (&attr);
18071 break;
18072
18073 case DW_AT_ranges:
18074 {
18075 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18076 but that requires a full DIE, so instead we just
18077 reimplement it. */
18078 int need_ranges_base = tag != DW_TAG_compile_unit;
18079 unsigned int ranges_offset = (DW_UNSND (&attr)
18080 + (need_ranges_base
18081 ? cu->ranges_base
18082 : 0));
18083
18084 /* Value of the DW_AT_ranges attribute is the offset in the
18085 .debug_ranges section. */
18086 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18087 nullptr))
18088 has_pc_info = 1;
18089 }
18090 break;
18091
18092 default:
18093 break;
18094 }
18095 }
18096
18097 /* For Ada, if both the name and the linkage name appear, we prefer
18098 the latter. This lets "catch exception" work better, regardless
18099 of the order in which the name and linkage name were emitted.
18100 Really, though, this is just a workaround for the fact that gdb
18101 doesn't store both the name and the linkage name. */
18102 if (cu->language == language_ada && linkage_name != nullptr)
18103 name = linkage_name;
18104
18105 if (high_pc_relative)
18106 highpc += lowpc;
18107
18108 if (has_low_pc_attr && has_high_pc_attr)
18109 {
18110 /* When using the GNU linker, .gnu.linkonce. sections are used to
18111 eliminate duplicate copies of functions and vtables and such.
18112 The linker will arbitrarily choose one and discard the others.
18113 The AT_*_pc values for such functions refer to local labels in
18114 these sections. If the section from that file was discarded, the
18115 labels are not in the output, so the relocs get a value of 0.
18116 If this is a discarded function, mark the pc bounds as invalid,
18117 so that GDB will ignore it. */
18118 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18119 {
18120 struct objfile *objfile = dwarf2_per_objfile->objfile;
18121 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18122
18123 complaint (_("DW_AT_low_pc %s is zero "
18124 "for DIE at %s [in module %s]"),
18125 paddress (gdbarch, lowpc),
18126 sect_offset_str (sect_off),
18127 objfile_name (objfile));
18128 }
18129 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18130 else if (lowpc >= highpc)
18131 {
18132 struct objfile *objfile = dwarf2_per_objfile->objfile;
18133 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18134
18135 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18136 "for DIE at %s [in module %s]"),
18137 paddress (gdbarch, lowpc),
18138 paddress (gdbarch, highpc),
18139 sect_offset_str (sect_off),
18140 objfile_name (objfile));
18141 }
18142 else
18143 has_pc_info = 1;
18144 }
18145
18146 return info_ptr;
18147 }
18148
18149 /* Find a cached partial DIE at OFFSET in CU. */
18150
18151 struct partial_die_info *
18152 dwarf2_cu::find_partial_die (sect_offset sect_off)
18153 {
18154 struct partial_die_info *lookup_die = NULL;
18155 struct partial_die_info part_die (sect_off);
18156
18157 lookup_die = ((struct partial_die_info *)
18158 htab_find_with_hash (partial_dies, &part_die,
18159 to_underlying (sect_off)));
18160
18161 return lookup_die;
18162 }
18163
18164 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18165 except in the case of .debug_types DIEs which do not reference
18166 outside their CU (they do however referencing other types via
18167 DW_FORM_ref_sig8). */
18168
18169 static const struct cu_partial_die_info
18170 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18171 {
18172 struct dwarf2_per_objfile *dwarf2_per_objfile
18173 = cu->per_cu->dwarf2_per_objfile;
18174 struct objfile *objfile = dwarf2_per_objfile->objfile;
18175 struct dwarf2_per_cu_data *per_cu = NULL;
18176 struct partial_die_info *pd = NULL;
18177
18178 if (offset_in_dwz == cu->per_cu->is_dwz
18179 && cu->header.offset_in_cu_p (sect_off))
18180 {
18181 pd = cu->find_partial_die (sect_off);
18182 if (pd != NULL)
18183 return { cu, pd };
18184 /* We missed recording what we needed.
18185 Load all dies and try again. */
18186 per_cu = cu->per_cu;
18187 }
18188 else
18189 {
18190 /* TUs don't reference other CUs/TUs (except via type signatures). */
18191 if (cu->per_cu->is_debug_types)
18192 {
18193 error (_("Dwarf Error: Type Unit at offset %s contains"
18194 " external reference to offset %s [in module %s].\n"),
18195 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18196 bfd_get_filename (objfile->obfd));
18197 }
18198 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18199 dwarf2_per_objfile);
18200
18201 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18202 load_partial_comp_unit (per_cu);
18203
18204 per_cu->cu->last_used = 0;
18205 pd = per_cu->cu->find_partial_die (sect_off);
18206 }
18207
18208 /* If we didn't find it, and not all dies have been loaded,
18209 load them all and try again. */
18210
18211 if (pd == NULL && per_cu->load_all_dies == 0)
18212 {
18213 per_cu->load_all_dies = 1;
18214
18215 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18216 THIS_CU->cu may already be in use. So we can't just free it and
18217 replace its DIEs with the ones we read in. Instead, we leave those
18218 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18219 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18220 set. */
18221 load_partial_comp_unit (per_cu);
18222
18223 pd = per_cu->cu->find_partial_die (sect_off);
18224 }
18225
18226 if (pd == NULL)
18227 internal_error (__FILE__, __LINE__,
18228 _("could not find partial DIE %s "
18229 "in cache [from module %s]\n"),
18230 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18231 return { per_cu->cu, pd };
18232 }
18233
18234 /* See if we can figure out if the class lives in a namespace. We do
18235 this by looking for a member function; its demangled name will
18236 contain namespace info, if there is any. */
18237
18238 static void
18239 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18240 struct dwarf2_cu *cu)
18241 {
18242 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18243 what template types look like, because the demangler
18244 frequently doesn't give the same name as the debug info. We
18245 could fix this by only using the demangled name to get the
18246 prefix (but see comment in read_structure_type). */
18247
18248 struct partial_die_info *real_pdi;
18249 struct partial_die_info *child_pdi;
18250
18251 /* If this DIE (this DIE's specification, if any) has a parent, then
18252 we should not do this. We'll prepend the parent's fully qualified
18253 name when we create the partial symbol. */
18254
18255 real_pdi = struct_pdi;
18256 while (real_pdi->has_specification)
18257 {
18258 auto res = find_partial_die (real_pdi->spec_offset,
18259 real_pdi->spec_is_dwz, cu);
18260 real_pdi = res.pdi;
18261 cu = res.cu;
18262 }
18263
18264 if (real_pdi->die_parent != NULL)
18265 return;
18266
18267 for (child_pdi = struct_pdi->die_child;
18268 child_pdi != NULL;
18269 child_pdi = child_pdi->die_sibling)
18270 {
18271 if (child_pdi->tag == DW_TAG_subprogram
18272 && child_pdi->linkage_name != NULL)
18273 {
18274 gdb::unique_xmalloc_ptr<char> actual_class_name
18275 (language_class_name_from_physname (cu->language_defn,
18276 child_pdi->linkage_name));
18277 if (actual_class_name != NULL)
18278 {
18279 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18280 struct_pdi->name
18281 = obstack_strdup (&objfile->per_bfd->storage_obstack,
18282 actual_class_name.get ());
18283 }
18284 break;
18285 }
18286 }
18287 }
18288
18289 void
18290 partial_die_info::fixup (struct dwarf2_cu *cu)
18291 {
18292 /* Once we've fixed up a die, there's no point in doing so again.
18293 This also avoids a memory leak if we were to call
18294 guess_partial_die_structure_name multiple times. */
18295 if (fixup_called)
18296 return;
18297
18298 /* If we found a reference attribute and the DIE has no name, try
18299 to find a name in the referred to DIE. */
18300
18301 if (name == NULL && has_specification)
18302 {
18303 struct partial_die_info *spec_die;
18304
18305 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18306 spec_die = res.pdi;
18307 cu = res.cu;
18308
18309 spec_die->fixup (cu);
18310
18311 if (spec_die->name)
18312 {
18313 name = spec_die->name;
18314
18315 /* Copy DW_AT_external attribute if it is set. */
18316 if (spec_die->is_external)
18317 is_external = spec_die->is_external;
18318 }
18319 }
18320
18321 /* Set default names for some unnamed DIEs. */
18322
18323 if (name == NULL && tag == DW_TAG_namespace)
18324 name = CP_ANONYMOUS_NAMESPACE_STR;
18325
18326 /* If there is no parent die to provide a namespace, and there are
18327 children, see if we can determine the namespace from their linkage
18328 name. */
18329 if (cu->language == language_cplus
18330 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18331 && die_parent == NULL
18332 && has_children
18333 && (tag == DW_TAG_class_type
18334 || tag == DW_TAG_structure_type
18335 || tag == DW_TAG_union_type))
18336 guess_partial_die_structure_name (this, cu);
18337
18338 /* GCC might emit a nameless struct or union that has a linkage
18339 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18340 if (name == NULL
18341 && (tag == DW_TAG_class_type
18342 || tag == DW_TAG_interface_type
18343 || tag == DW_TAG_structure_type
18344 || tag == DW_TAG_union_type)
18345 && linkage_name != NULL)
18346 {
18347 gdb::unique_xmalloc_ptr<char> demangled
18348 (gdb_demangle (linkage_name, DMGL_TYPES));
18349 if (demangled != nullptr)
18350 {
18351 const char *base;
18352
18353 /* Strip any leading namespaces/classes, keep only the base name.
18354 DW_AT_name for named DIEs does not contain the prefixes. */
18355 base = strrchr (demangled.get (), ':');
18356 if (base && base > demangled.get () && base[-1] == ':')
18357 base++;
18358 else
18359 base = demangled.get ();
18360
18361 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18362 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
18363 }
18364 }
18365
18366 fixup_called = 1;
18367 }
18368
18369 /* Process the attributes that had to be skipped in the first round. These
18370 attributes are the ones that need str_offsets_base or addr_base attributes.
18371 They could not have been processed in the first round, because at the time
18372 the values of str_offsets_base or addr_base may not have been known. */
18373 void read_attribute_reprocess (const struct die_reader_specs *reader,
18374 struct attribute *attr)
18375 {
18376 struct dwarf2_cu *cu = reader->cu;
18377 switch (attr->form)
18378 {
18379 case DW_FORM_addrx:
18380 case DW_FORM_GNU_addr_index:
18381 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18382 break;
18383 case DW_FORM_strx:
18384 case DW_FORM_strx1:
18385 case DW_FORM_strx2:
18386 case DW_FORM_strx3:
18387 case DW_FORM_strx4:
18388 case DW_FORM_GNU_str_index:
18389 {
18390 unsigned int str_index = DW_UNSND (attr);
18391 if (reader->dwo_file != NULL)
18392 {
18393 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18394 DW_STRING_IS_CANONICAL (attr) = 0;
18395 }
18396 else
18397 {
18398 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18399 DW_STRING_IS_CANONICAL (attr) = 0;
18400 }
18401 break;
18402 }
18403 default:
18404 gdb_assert_not_reached (_("Unexpected DWARF form."));
18405 }
18406 }
18407
18408 /* Read an attribute value described by an attribute form. */
18409
18410 static const gdb_byte *
18411 read_attribute_value (const struct die_reader_specs *reader,
18412 struct attribute *attr, unsigned form,
18413 LONGEST implicit_const, const gdb_byte *info_ptr,
18414 bool *need_reprocess)
18415 {
18416 struct dwarf2_cu *cu = reader->cu;
18417 struct dwarf2_per_objfile *dwarf2_per_objfile
18418 = cu->per_cu->dwarf2_per_objfile;
18419 struct objfile *objfile = dwarf2_per_objfile->objfile;
18420 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18421 bfd *abfd = reader->abfd;
18422 struct comp_unit_head *cu_header = &cu->header;
18423 unsigned int bytes_read;
18424 struct dwarf_block *blk;
18425 *need_reprocess = false;
18426
18427 attr->form = (enum dwarf_form) form;
18428 switch (form)
18429 {
18430 case DW_FORM_ref_addr:
18431 if (cu->header.version == 2)
18432 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18433 &bytes_read);
18434 else
18435 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18436 &bytes_read);
18437 info_ptr += bytes_read;
18438 break;
18439 case DW_FORM_GNU_ref_alt:
18440 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18441 info_ptr += bytes_read;
18442 break;
18443 case DW_FORM_addr:
18444 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18445 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18446 info_ptr += bytes_read;
18447 break;
18448 case DW_FORM_block2:
18449 blk = dwarf_alloc_block (cu);
18450 blk->size = read_2_bytes (abfd, info_ptr);
18451 info_ptr += 2;
18452 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18453 info_ptr += blk->size;
18454 DW_BLOCK (attr) = blk;
18455 break;
18456 case DW_FORM_block4:
18457 blk = dwarf_alloc_block (cu);
18458 blk->size = read_4_bytes (abfd, info_ptr);
18459 info_ptr += 4;
18460 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18461 info_ptr += blk->size;
18462 DW_BLOCK (attr) = blk;
18463 break;
18464 case DW_FORM_data2:
18465 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18466 info_ptr += 2;
18467 break;
18468 case DW_FORM_data4:
18469 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18470 info_ptr += 4;
18471 break;
18472 case DW_FORM_data8:
18473 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18474 info_ptr += 8;
18475 break;
18476 case DW_FORM_data16:
18477 blk = dwarf_alloc_block (cu);
18478 blk->size = 16;
18479 blk->data = read_n_bytes (abfd, info_ptr, 16);
18480 info_ptr += 16;
18481 DW_BLOCK (attr) = blk;
18482 break;
18483 case DW_FORM_sec_offset:
18484 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18485 info_ptr += bytes_read;
18486 break;
18487 case DW_FORM_string:
18488 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18489 DW_STRING_IS_CANONICAL (attr) = 0;
18490 info_ptr += bytes_read;
18491 break;
18492 case DW_FORM_strp:
18493 if (!cu->per_cu->is_dwz)
18494 {
18495 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18496 abfd, info_ptr, cu_header,
18497 &bytes_read);
18498 DW_STRING_IS_CANONICAL (attr) = 0;
18499 info_ptr += bytes_read;
18500 break;
18501 }
18502 /* FALLTHROUGH */
18503 case DW_FORM_line_strp:
18504 if (!cu->per_cu->is_dwz)
18505 {
18506 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18507 abfd, info_ptr,
18508 cu_header, &bytes_read);
18509 DW_STRING_IS_CANONICAL (attr) = 0;
18510 info_ptr += bytes_read;
18511 break;
18512 }
18513 /* FALLTHROUGH */
18514 case DW_FORM_GNU_strp_alt:
18515 {
18516 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18517 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18518 &bytes_read);
18519
18520 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18521 dwz, str_offset);
18522 DW_STRING_IS_CANONICAL (attr) = 0;
18523 info_ptr += bytes_read;
18524 }
18525 break;
18526 case DW_FORM_exprloc:
18527 case DW_FORM_block:
18528 blk = dwarf_alloc_block (cu);
18529 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18530 info_ptr += bytes_read;
18531 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18532 info_ptr += blk->size;
18533 DW_BLOCK (attr) = blk;
18534 break;
18535 case DW_FORM_block1:
18536 blk = dwarf_alloc_block (cu);
18537 blk->size = read_1_byte (abfd, info_ptr);
18538 info_ptr += 1;
18539 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18540 info_ptr += blk->size;
18541 DW_BLOCK (attr) = blk;
18542 break;
18543 case DW_FORM_data1:
18544 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18545 info_ptr += 1;
18546 break;
18547 case DW_FORM_flag:
18548 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18549 info_ptr += 1;
18550 break;
18551 case DW_FORM_flag_present:
18552 DW_UNSND (attr) = 1;
18553 break;
18554 case DW_FORM_sdata:
18555 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18556 info_ptr += bytes_read;
18557 break;
18558 case DW_FORM_udata:
18559 case DW_FORM_rnglistx:
18560 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18561 info_ptr += bytes_read;
18562 break;
18563 case DW_FORM_ref1:
18564 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18565 + read_1_byte (abfd, info_ptr));
18566 info_ptr += 1;
18567 break;
18568 case DW_FORM_ref2:
18569 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18570 + read_2_bytes (abfd, info_ptr));
18571 info_ptr += 2;
18572 break;
18573 case DW_FORM_ref4:
18574 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18575 + read_4_bytes (abfd, info_ptr));
18576 info_ptr += 4;
18577 break;
18578 case DW_FORM_ref8:
18579 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18580 + read_8_bytes (abfd, info_ptr));
18581 info_ptr += 8;
18582 break;
18583 case DW_FORM_ref_sig8:
18584 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18585 info_ptr += 8;
18586 break;
18587 case DW_FORM_ref_udata:
18588 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18589 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18590 info_ptr += bytes_read;
18591 break;
18592 case DW_FORM_indirect:
18593 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18594 info_ptr += bytes_read;
18595 if (form == DW_FORM_implicit_const)
18596 {
18597 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18598 info_ptr += bytes_read;
18599 }
18600 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18601 info_ptr, need_reprocess);
18602 break;
18603 case DW_FORM_implicit_const:
18604 DW_SND (attr) = implicit_const;
18605 break;
18606 case DW_FORM_addrx:
18607 case DW_FORM_GNU_addr_index:
18608 *need_reprocess = true;
18609 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18610 info_ptr += bytes_read;
18611 break;
18612 case DW_FORM_strx:
18613 case DW_FORM_strx1:
18614 case DW_FORM_strx2:
18615 case DW_FORM_strx3:
18616 case DW_FORM_strx4:
18617 case DW_FORM_GNU_str_index:
18618 {
18619 ULONGEST str_index;
18620 if (form == DW_FORM_strx1)
18621 {
18622 str_index = read_1_byte (abfd, info_ptr);
18623 info_ptr += 1;
18624 }
18625 else if (form == DW_FORM_strx2)
18626 {
18627 str_index = read_2_bytes (abfd, info_ptr);
18628 info_ptr += 2;
18629 }
18630 else if (form == DW_FORM_strx3)
18631 {
18632 str_index = read_3_bytes (abfd, info_ptr);
18633 info_ptr += 3;
18634 }
18635 else if (form == DW_FORM_strx4)
18636 {
18637 str_index = read_4_bytes (abfd, info_ptr);
18638 info_ptr += 4;
18639 }
18640 else
18641 {
18642 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18643 info_ptr += bytes_read;
18644 }
18645 *need_reprocess = true;
18646 DW_UNSND (attr) = str_index;
18647 }
18648 break;
18649 default:
18650 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18651 dwarf_form_name (form),
18652 bfd_get_filename (abfd));
18653 }
18654
18655 /* Super hack. */
18656 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18657 attr->form = DW_FORM_GNU_ref_alt;
18658
18659 /* We have seen instances where the compiler tried to emit a byte
18660 size attribute of -1 which ended up being encoded as an unsigned
18661 0xffffffff. Although 0xffffffff is technically a valid size value,
18662 an object of this size seems pretty unlikely so we can relatively
18663 safely treat these cases as if the size attribute was invalid and
18664 treat them as zero by default. */
18665 if (attr->name == DW_AT_byte_size
18666 && form == DW_FORM_data4
18667 && DW_UNSND (attr) >= 0xffffffff)
18668 {
18669 complaint
18670 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18671 hex_string (DW_UNSND (attr)));
18672 DW_UNSND (attr) = 0;
18673 }
18674
18675 return info_ptr;
18676 }
18677
18678 /* Read an attribute described by an abbreviated attribute. */
18679
18680 static const gdb_byte *
18681 read_attribute (const struct die_reader_specs *reader,
18682 struct attribute *attr, struct attr_abbrev *abbrev,
18683 const gdb_byte *info_ptr, bool *need_reprocess)
18684 {
18685 attr->name = abbrev->name;
18686 return read_attribute_value (reader, attr, abbrev->form,
18687 abbrev->implicit_const, info_ptr,
18688 need_reprocess);
18689 }
18690
18691 /* Cover function for read_initial_length.
18692 Returns the length of the object at BUF, and stores the size of the
18693 initial length in *BYTES_READ and stores the size that offsets will be in
18694 *OFFSET_SIZE.
18695 If the initial length size is not equivalent to that specified in
18696 CU_HEADER then issue a complaint.
18697 This is useful when reading non-comp-unit headers. */
18698
18699 static LONGEST
18700 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
18701 const struct comp_unit_head *cu_header,
18702 unsigned int *bytes_read,
18703 unsigned int *offset_size)
18704 {
18705 LONGEST length = read_initial_length (abfd, buf, bytes_read);
18706
18707 gdb_assert (cu_header->initial_length_size == 4
18708 || cu_header->initial_length_size == 8
18709 || cu_header->initial_length_size == 12);
18710
18711 if (cu_header->initial_length_size != *bytes_read)
18712 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
18713
18714 *offset_size = (*bytes_read == 4) ? 4 : 8;
18715 return length;
18716 }
18717
18718 /* Return pointer to string at section SECT offset STR_OFFSET with error
18719 reporting strings FORM_NAME and SECT_NAME. */
18720
18721 static const char *
18722 read_indirect_string_at_offset_from (struct objfile *objfile,
18723 bfd *abfd, LONGEST str_offset,
18724 struct dwarf2_section_info *sect,
18725 const char *form_name,
18726 const char *sect_name)
18727 {
18728 sect->read (objfile);
18729 if (sect->buffer == NULL)
18730 error (_("%s used without %s section [in module %s]"),
18731 form_name, sect_name, bfd_get_filename (abfd));
18732 if (str_offset >= sect->size)
18733 error (_("%s pointing outside of %s section [in module %s]"),
18734 form_name, sect_name, bfd_get_filename (abfd));
18735 gdb_assert (HOST_CHAR_BIT == 8);
18736 if (sect->buffer[str_offset] == '\0')
18737 return NULL;
18738 return (const char *) (sect->buffer + str_offset);
18739 }
18740
18741 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18742
18743 static const char *
18744 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18745 bfd *abfd, LONGEST str_offset)
18746 {
18747 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18748 abfd, str_offset,
18749 &dwarf2_per_objfile->str,
18750 "DW_FORM_strp", ".debug_str");
18751 }
18752
18753 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
18754
18755 static const char *
18756 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18757 bfd *abfd, LONGEST str_offset)
18758 {
18759 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
18760 abfd, str_offset,
18761 &dwarf2_per_objfile->line_str,
18762 "DW_FORM_line_strp",
18763 ".debug_line_str");
18764 }
18765
18766 /* Read a string at offset STR_OFFSET in the .debug_str section from
18767 the .dwz file DWZ. Throw an error if the offset is too large. If
18768 the string consists of a single NUL byte, return NULL; otherwise
18769 return a pointer to the string. */
18770
18771 static const char *
18772 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
18773 LONGEST str_offset)
18774 {
18775 dwz->str.read (objfile);
18776
18777 if (dwz->str.buffer == NULL)
18778 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
18779 "section [in module %s]"),
18780 bfd_get_filename (dwz->dwz_bfd.get ()));
18781 if (str_offset >= dwz->str.size)
18782 error (_("DW_FORM_GNU_strp_alt pointing outside of "
18783 ".debug_str section [in module %s]"),
18784 bfd_get_filename (dwz->dwz_bfd.get ()));
18785 gdb_assert (HOST_CHAR_BIT == 8);
18786 if (dwz->str.buffer[str_offset] == '\0')
18787 return NULL;
18788 return (const char *) (dwz->str.buffer + str_offset);
18789 }
18790
18791 /* Return pointer to string at .debug_str offset as read from BUF.
18792 BUF is assumed to be in a compilation unit described by CU_HEADER.
18793 Return *BYTES_READ_PTR count of bytes read from BUF. */
18794
18795 static const char *
18796 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18797 const gdb_byte *buf,
18798 const struct comp_unit_head *cu_header,
18799 unsigned int *bytes_read_ptr)
18800 {
18801 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18802
18803 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
18804 }
18805
18806 /* Return pointer to string at .debug_line_str offset as read from BUF.
18807 BUF is assumed to be in a compilation unit described by CU_HEADER.
18808 Return *BYTES_READ_PTR count of bytes read from BUF. */
18809
18810 static const char *
18811 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
18812 bfd *abfd, const gdb_byte *buf,
18813 const struct comp_unit_head *cu_header,
18814 unsigned int *bytes_read_ptr)
18815 {
18816 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18817
18818 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
18819 str_offset);
18820 }
18821
18822 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18823 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18824 ADDR_SIZE is the size of addresses from the CU header. */
18825
18826 static CORE_ADDR
18827 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18828 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18829 int addr_size)
18830 {
18831 struct objfile *objfile = dwarf2_per_objfile->objfile;
18832 bfd *abfd = objfile->obfd;
18833 const gdb_byte *info_ptr;
18834 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18835
18836 dwarf2_per_objfile->addr.read (objfile);
18837 if (dwarf2_per_objfile->addr.buffer == NULL)
18838 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18839 objfile_name (objfile));
18840 if (addr_base_or_zero + addr_index * addr_size
18841 >= dwarf2_per_objfile->addr.size)
18842 error (_("DW_FORM_addr_index pointing outside of "
18843 ".debug_addr section [in module %s]"),
18844 objfile_name (objfile));
18845 info_ptr = (dwarf2_per_objfile->addr.buffer
18846 + addr_base_or_zero + addr_index * addr_size);
18847 if (addr_size == 4)
18848 return bfd_get_32 (abfd, info_ptr);
18849 else
18850 return bfd_get_64 (abfd, info_ptr);
18851 }
18852
18853 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18854
18855 static CORE_ADDR
18856 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18857 {
18858 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18859 cu->addr_base, cu->header.addr_size);
18860 }
18861
18862 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18863
18864 static CORE_ADDR
18865 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18866 unsigned int *bytes_read)
18867 {
18868 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18869 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18870
18871 return read_addr_index (cu, addr_index);
18872 }
18873
18874 /* Given an index in .debug_addr, fetch the value.
18875 NOTE: This can be called during dwarf expression evaluation,
18876 long after the debug information has been read, and thus per_cu->cu
18877 may no longer exist. */
18878
18879 CORE_ADDR
18880 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
18881 unsigned int addr_index)
18882 {
18883 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18884 struct dwarf2_cu *cu = per_cu->cu;
18885 gdb::optional<ULONGEST> addr_base;
18886 int addr_size;
18887
18888 /* We need addr_base and addr_size.
18889 If we don't have PER_CU->cu, we have to get it.
18890 Nasty, but the alternative is storing the needed info in PER_CU,
18891 which at this point doesn't seem justified: it's not clear how frequently
18892 it would get used and it would increase the size of every PER_CU.
18893 Entry points like dwarf2_per_cu_addr_size do a similar thing
18894 so we're not in uncharted territory here.
18895 Alas we need to be a bit more complicated as addr_base is contained
18896 in the DIE.
18897
18898 We don't need to read the entire CU(/TU).
18899 We just need the header and top level die.
18900
18901 IWBN to use the aging mechanism to let us lazily later discard the CU.
18902 For now we skip this optimization. */
18903
18904 if (cu != NULL)
18905 {
18906 addr_base = cu->addr_base;
18907 addr_size = cu->header.addr_size;
18908 }
18909 else
18910 {
18911 cutu_reader reader (per_cu, NULL, 0, false);
18912 addr_base = reader.cu->addr_base;
18913 addr_size = reader.cu->header.addr_size;
18914 }
18915
18916 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18917 addr_size);
18918 }
18919
18920 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18921 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18922 DWO file. */
18923
18924 static const char *
18925 read_str_index (struct dwarf2_cu *cu,
18926 struct dwarf2_section_info *str_section,
18927 struct dwarf2_section_info *str_offsets_section,
18928 ULONGEST str_offsets_base, ULONGEST str_index)
18929 {
18930 struct dwarf2_per_objfile *dwarf2_per_objfile
18931 = cu->per_cu->dwarf2_per_objfile;
18932 struct objfile *objfile = dwarf2_per_objfile->objfile;
18933 const char *objf_name = objfile_name (objfile);
18934 bfd *abfd = objfile->obfd;
18935 const gdb_byte *info_ptr;
18936 ULONGEST str_offset;
18937 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18938
18939 str_section->read (objfile);
18940 str_offsets_section->read (objfile);
18941 if (str_section->buffer == NULL)
18942 error (_("%s used without %s section"
18943 " in CU at offset %s [in module %s]"),
18944 form_name, str_section->get_name (),
18945 sect_offset_str (cu->header.sect_off), objf_name);
18946 if (str_offsets_section->buffer == NULL)
18947 error (_("%s used without %s section"
18948 " in CU at offset %s [in module %s]"),
18949 form_name, str_section->get_name (),
18950 sect_offset_str (cu->header.sect_off), objf_name);
18951 info_ptr = (str_offsets_section->buffer
18952 + str_offsets_base
18953 + str_index * cu->header.offset_size);
18954 if (cu->header.offset_size == 4)
18955 str_offset = bfd_get_32 (abfd, info_ptr);
18956 else
18957 str_offset = bfd_get_64 (abfd, info_ptr);
18958 if (str_offset >= str_section->size)
18959 error (_("Offset from %s pointing outside of"
18960 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18961 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18962 return (const char *) (str_section->buffer + str_offset);
18963 }
18964
18965 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18966
18967 static const char *
18968 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18969 {
18970 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18971 ? reader->cu->header.addr_size : 0;
18972 return read_str_index (reader->cu,
18973 &reader->dwo_file->sections.str,
18974 &reader->dwo_file->sections.str_offsets,
18975 str_offsets_base, str_index);
18976 }
18977
18978 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18979
18980 static const char *
18981 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18982 {
18983 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18984 const char *objf_name = objfile_name (objfile);
18985 static const char form_name[] = "DW_FORM_GNU_str_index";
18986 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18987
18988 if (!cu->str_offsets_base.has_value ())
18989 error (_("%s used in Fission stub without %s"
18990 " in CU at offset 0x%lx [in module %s]"),
18991 form_name, str_offsets_attr_name,
18992 (long) cu->header.offset_size, objf_name);
18993
18994 return read_str_index (cu,
18995 &cu->per_cu->dwarf2_per_objfile->str,
18996 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18997 *cu->str_offsets_base, str_index);
18998 }
18999
19000 /* Return the length of an LEB128 number in BUF. */
19001
19002 static int
19003 leb128_size (const gdb_byte *buf)
19004 {
19005 const gdb_byte *begin = buf;
19006 gdb_byte byte;
19007
19008 while (1)
19009 {
19010 byte = *buf++;
19011 if ((byte & 128) == 0)
19012 return buf - begin;
19013 }
19014 }
19015
19016 static void
19017 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19018 {
19019 switch (lang)
19020 {
19021 case DW_LANG_C89:
19022 case DW_LANG_C99:
19023 case DW_LANG_C11:
19024 case DW_LANG_C:
19025 case DW_LANG_UPC:
19026 cu->language = language_c;
19027 break;
19028 case DW_LANG_Java:
19029 case DW_LANG_C_plus_plus:
19030 case DW_LANG_C_plus_plus_11:
19031 case DW_LANG_C_plus_plus_14:
19032 cu->language = language_cplus;
19033 break;
19034 case DW_LANG_D:
19035 cu->language = language_d;
19036 break;
19037 case DW_LANG_Fortran77:
19038 case DW_LANG_Fortran90:
19039 case DW_LANG_Fortran95:
19040 case DW_LANG_Fortran03:
19041 case DW_LANG_Fortran08:
19042 cu->language = language_fortran;
19043 break;
19044 case DW_LANG_Go:
19045 cu->language = language_go;
19046 break;
19047 case DW_LANG_Mips_Assembler:
19048 cu->language = language_asm;
19049 break;
19050 case DW_LANG_Ada83:
19051 case DW_LANG_Ada95:
19052 cu->language = language_ada;
19053 break;
19054 case DW_LANG_Modula2:
19055 cu->language = language_m2;
19056 break;
19057 case DW_LANG_Pascal83:
19058 cu->language = language_pascal;
19059 break;
19060 case DW_LANG_ObjC:
19061 cu->language = language_objc;
19062 break;
19063 case DW_LANG_Rust:
19064 case DW_LANG_Rust_old:
19065 cu->language = language_rust;
19066 break;
19067 case DW_LANG_Cobol74:
19068 case DW_LANG_Cobol85:
19069 default:
19070 cu->language = language_minimal;
19071 break;
19072 }
19073 cu->language_defn = language_def (cu->language);
19074 }
19075
19076 /* Return the named attribute or NULL if not there. */
19077
19078 static struct attribute *
19079 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19080 {
19081 for (;;)
19082 {
19083 unsigned int i;
19084 struct attribute *spec = NULL;
19085
19086 for (i = 0; i < die->num_attrs; ++i)
19087 {
19088 if (die->attrs[i].name == name)
19089 return &die->attrs[i];
19090 if (die->attrs[i].name == DW_AT_specification
19091 || die->attrs[i].name == DW_AT_abstract_origin)
19092 spec = &die->attrs[i];
19093 }
19094
19095 if (!spec)
19096 break;
19097
19098 die = follow_die_ref (die, spec, &cu);
19099 }
19100
19101 return NULL;
19102 }
19103
19104 /* Return the named attribute or NULL if not there,
19105 but do not follow DW_AT_specification, etc.
19106 This is for use in contexts where we're reading .debug_types dies.
19107 Following DW_AT_specification, DW_AT_abstract_origin will take us
19108 back up the chain, and we want to go down. */
19109
19110 static struct attribute *
19111 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
19112 {
19113 unsigned int i;
19114
19115 for (i = 0; i < die->num_attrs; ++i)
19116 if (die->attrs[i].name == name)
19117 return &die->attrs[i];
19118
19119 return NULL;
19120 }
19121
19122 /* Return the string associated with a string-typed attribute, or NULL if it
19123 is either not found or is of an incorrect type. */
19124
19125 static const char *
19126 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19127 {
19128 struct attribute *attr;
19129 const char *str = NULL;
19130
19131 attr = dwarf2_attr (die, name, cu);
19132
19133 if (attr != NULL)
19134 {
19135 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19136 || attr->form == DW_FORM_string
19137 || attr->form == DW_FORM_strx
19138 || attr->form == DW_FORM_strx1
19139 || attr->form == DW_FORM_strx2
19140 || attr->form == DW_FORM_strx3
19141 || attr->form == DW_FORM_strx4
19142 || attr->form == DW_FORM_GNU_str_index
19143 || attr->form == DW_FORM_GNU_strp_alt)
19144 str = DW_STRING (attr);
19145 else
19146 complaint (_("string type expected for attribute %s for "
19147 "DIE at %s in module %s"),
19148 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19149 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19150 }
19151
19152 return str;
19153 }
19154
19155 /* Return the dwo name or NULL if not present. If present, it is in either
19156 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19157 static const char *
19158 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19159 {
19160 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19161 if (dwo_name == nullptr)
19162 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19163 return dwo_name;
19164 }
19165
19166 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19167 and holds a non-zero value. This function should only be used for
19168 DW_FORM_flag or DW_FORM_flag_present attributes. */
19169
19170 static int
19171 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19172 {
19173 struct attribute *attr = dwarf2_attr (die, name, cu);
19174
19175 return (attr && DW_UNSND (attr));
19176 }
19177
19178 static int
19179 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19180 {
19181 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19182 which value is non-zero. However, we have to be careful with
19183 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19184 (via dwarf2_flag_true_p) follows this attribute. So we may
19185 end up accidently finding a declaration attribute that belongs
19186 to a different DIE referenced by the specification attribute,
19187 even though the given DIE does not have a declaration attribute. */
19188 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19189 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19190 }
19191
19192 /* Return the die giving the specification for DIE, if there is
19193 one. *SPEC_CU is the CU containing DIE on input, and the CU
19194 containing the return value on output. If there is no
19195 specification, but there is an abstract origin, that is
19196 returned. */
19197
19198 static struct die_info *
19199 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19200 {
19201 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19202 *spec_cu);
19203
19204 if (spec_attr == NULL)
19205 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19206
19207 if (spec_attr == NULL)
19208 return NULL;
19209 else
19210 return follow_die_ref (die, spec_attr, spec_cu);
19211 }
19212
19213 /* Stub for free_line_header to match void * callback types. */
19214
19215 static void
19216 free_line_header_voidp (void *arg)
19217 {
19218 struct line_header *lh = (struct line_header *) arg;
19219
19220 delete lh;
19221 }
19222
19223 /* A convenience function to find the proper .debug_line section for a CU. */
19224
19225 static struct dwarf2_section_info *
19226 get_debug_line_section (struct dwarf2_cu *cu)
19227 {
19228 struct dwarf2_section_info *section;
19229 struct dwarf2_per_objfile *dwarf2_per_objfile
19230 = cu->per_cu->dwarf2_per_objfile;
19231
19232 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19233 DWO file. */
19234 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19235 section = &cu->dwo_unit->dwo_file->sections.line;
19236 else if (cu->per_cu->is_dwz)
19237 {
19238 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19239
19240 section = &dwz->line;
19241 }
19242 else
19243 section = &dwarf2_per_objfile->line;
19244
19245 return section;
19246 }
19247
19248 /* Read directory or file name entry format, starting with byte of
19249 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19250 entries count and the entries themselves in the described entry
19251 format. */
19252
19253 static void
19254 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19255 bfd *abfd, const gdb_byte **bufp,
19256 struct line_header *lh,
19257 const struct comp_unit_head *cu_header,
19258 void (*callback) (struct line_header *lh,
19259 const char *name,
19260 dir_index d_index,
19261 unsigned int mod_time,
19262 unsigned int length))
19263 {
19264 gdb_byte format_count, formati;
19265 ULONGEST data_count, datai;
19266 const gdb_byte *buf = *bufp;
19267 const gdb_byte *format_header_data;
19268 unsigned int bytes_read;
19269
19270 format_count = read_1_byte (abfd, buf);
19271 buf += 1;
19272 format_header_data = buf;
19273 for (formati = 0; formati < format_count; formati++)
19274 {
19275 read_unsigned_leb128 (abfd, buf, &bytes_read);
19276 buf += bytes_read;
19277 read_unsigned_leb128 (abfd, buf, &bytes_read);
19278 buf += bytes_read;
19279 }
19280
19281 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19282 buf += bytes_read;
19283 for (datai = 0; datai < data_count; datai++)
19284 {
19285 const gdb_byte *format = format_header_data;
19286 struct file_entry fe;
19287
19288 for (formati = 0; formati < format_count; formati++)
19289 {
19290 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
19291 format += bytes_read;
19292
19293 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
19294 format += bytes_read;
19295
19296 gdb::optional<const char *> string;
19297 gdb::optional<unsigned int> uint;
19298
19299 switch (form)
19300 {
19301 case DW_FORM_string:
19302 string.emplace (read_direct_string (abfd, buf, &bytes_read));
19303 buf += bytes_read;
19304 break;
19305
19306 case DW_FORM_line_strp:
19307 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19308 abfd, buf,
19309 cu_header,
19310 &bytes_read));
19311 buf += bytes_read;
19312 break;
19313
19314 case DW_FORM_data1:
19315 uint.emplace (read_1_byte (abfd, buf));
19316 buf += 1;
19317 break;
19318
19319 case DW_FORM_data2:
19320 uint.emplace (read_2_bytes (abfd, buf));
19321 buf += 2;
19322 break;
19323
19324 case DW_FORM_data4:
19325 uint.emplace (read_4_bytes (abfd, buf));
19326 buf += 4;
19327 break;
19328
19329 case DW_FORM_data8:
19330 uint.emplace (read_8_bytes (abfd, buf));
19331 buf += 8;
19332 break;
19333
19334 case DW_FORM_data16:
19335 /* This is used for MD5, but file_entry does not record MD5s. */
19336 buf += 16;
19337 break;
19338
19339 case DW_FORM_udata:
19340 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
19341 buf += bytes_read;
19342 break;
19343
19344 case DW_FORM_block:
19345 /* It is valid only for DW_LNCT_timestamp which is ignored by
19346 current GDB. */
19347 break;
19348 }
19349
19350 switch (content_type)
19351 {
19352 case DW_LNCT_path:
19353 if (string.has_value ())
19354 fe.name = *string;
19355 break;
19356 case DW_LNCT_directory_index:
19357 if (uint.has_value ())
19358 fe.d_index = (dir_index) *uint;
19359 break;
19360 case DW_LNCT_timestamp:
19361 if (uint.has_value ())
19362 fe.mod_time = *uint;
19363 break;
19364 case DW_LNCT_size:
19365 if (uint.has_value ())
19366 fe.length = *uint;
19367 break;
19368 case DW_LNCT_MD5:
19369 break;
19370 default:
19371 complaint (_("Unknown format content type %s"),
19372 pulongest (content_type));
19373 }
19374 }
19375
19376 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
19377 }
19378
19379 *bufp = buf;
19380 }
19381
19382 /* Read the statement program header starting at OFFSET in
19383 .debug_line, or .debug_line.dwo. Return a pointer
19384 to a struct line_header, allocated using xmalloc.
19385 Returns NULL if there is a problem reading the header, e.g., if it
19386 has a version we don't understand.
19387
19388 NOTE: the strings in the include directory and file name tables of
19389 the returned object point into the dwarf line section buffer,
19390 and must not be freed. */
19391
19392 static line_header_up
19393 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19394 {
19395 const gdb_byte *line_ptr;
19396 unsigned int bytes_read, offset_size;
19397 int i;
19398 const char *cur_dir, *cur_file;
19399 struct dwarf2_section_info *section;
19400 bfd *abfd;
19401 struct dwarf2_per_objfile *dwarf2_per_objfile
19402 = cu->per_cu->dwarf2_per_objfile;
19403
19404 section = get_debug_line_section (cu);
19405 section->read (dwarf2_per_objfile->objfile);
19406 if (section->buffer == NULL)
19407 {
19408 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19409 complaint (_("missing .debug_line.dwo section"));
19410 else
19411 complaint (_("missing .debug_line section"));
19412 return 0;
19413 }
19414
19415 /* We can't do this until we know the section is non-empty.
19416 Only then do we know we have such a section. */
19417 abfd = section->get_bfd_owner ();
19418
19419 /* Make sure that at least there's room for the total_length field.
19420 That could be 12 bytes long, but we're just going to fudge that. */
19421 if (to_underlying (sect_off) + 4 >= section->size)
19422 {
19423 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19424 return 0;
19425 }
19426
19427 line_header_up lh (new line_header ());
19428
19429 lh->sect_off = sect_off;
19430 lh->offset_in_dwz = cu->per_cu->is_dwz;
19431
19432 line_ptr = section->buffer + to_underlying (sect_off);
19433
19434 /* Read in the header. */
19435 lh->total_length =
19436 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19437 &bytes_read, &offset_size);
19438 line_ptr += bytes_read;
19439
19440 const gdb_byte *start_here = line_ptr;
19441
19442 if (line_ptr + lh->total_length > (section->buffer + section->size))
19443 {
19444 dwarf2_statement_list_fits_in_line_number_section_complaint ();
19445 return 0;
19446 }
19447 lh->statement_program_end = start_here + lh->total_length;
19448 lh->version = read_2_bytes (abfd, line_ptr);
19449 line_ptr += 2;
19450 if (lh->version > 5)
19451 {
19452 /* This is a version we don't understand. The format could have
19453 changed in ways we don't handle properly so just punt. */
19454 complaint (_("unsupported version in .debug_line section"));
19455 return NULL;
19456 }
19457 if (lh->version >= 5)
19458 {
19459 gdb_byte segment_selector_size;
19460
19461 /* Skip address size. */
19462 read_1_byte (abfd, line_ptr);
19463 line_ptr += 1;
19464
19465 segment_selector_size = read_1_byte (abfd, line_ptr);
19466 line_ptr += 1;
19467 if (segment_selector_size != 0)
19468 {
19469 complaint (_("unsupported segment selector size %u "
19470 "in .debug_line section"),
19471 segment_selector_size);
19472 return NULL;
19473 }
19474 }
19475 lh->header_length = read_offset (abfd, line_ptr, offset_size);
19476 line_ptr += offset_size;
19477 lh->statement_program_start = line_ptr + lh->header_length;
19478 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19479 line_ptr += 1;
19480 if (lh->version >= 4)
19481 {
19482 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19483 line_ptr += 1;
19484 }
19485 else
19486 lh->maximum_ops_per_instruction = 1;
19487
19488 if (lh->maximum_ops_per_instruction == 0)
19489 {
19490 lh->maximum_ops_per_instruction = 1;
19491 complaint (_("invalid maximum_ops_per_instruction "
19492 "in `.debug_line' section"));
19493 }
19494
19495 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19496 line_ptr += 1;
19497 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19498 line_ptr += 1;
19499 lh->line_range = read_1_byte (abfd, line_ptr);
19500 line_ptr += 1;
19501 lh->opcode_base = read_1_byte (abfd, line_ptr);
19502 line_ptr += 1;
19503 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
19504
19505 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19506 for (i = 1; i < lh->opcode_base; ++i)
19507 {
19508 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19509 line_ptr += 1;
19510 }
19511
19512 if (lh->version >= 5)
19513 {
19514 /* Read directory table. */
19515 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19516 &cu->header,
19517 [] (struct line_header *header, const char *name,
19518 dir_index d_index, unsigned int mod_time,
19519 unsigned int length)
19520 {
19521 header->add_include_dir (name);
19522 });
19523
19524 /* Read file name table. */
19525 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19526 &cu->header,
19527 [] (struct line_header *header, const char *name,
19528 dir_index d_index, unsigned int mod_time,
19529 unsigned int length)
19530 {
19531 header->add_file_name (name, d_index, mod_time, length);
19532 });
19533 }
19534 else
19535 {
19536 /* Read directory table. */
19537 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19538 {
19539 line_ptr += bytes_read;
19540 lh->add_include_dir (cur_dir);
19541 }
19542 line_ptr += bytes_read;
19543
19544 /* Read file name table. */
19545 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
19546 {
19547 unsigned int mod_time, length;
19548 dir_index d_index;
19549
19550 line_ptr += bytes_read;
19551 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19552 line_ptr += bytes_read;
19553 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19554 line_ptr += bytes_read;
19555 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19556 line_ptr += bytes_read;
19557
19558 lh->add_file_name (cur_file, d_index, mod_time, length);
19559 }
19560 line_ptr += bytes_read;
19561 }
19562
19563 if (line_ptr > (section->buffer + section->size))
19564 complaint (_("line number info header doesn't "
19565 "fit in `.debug_line' section"));
19566
19567 return lh;
19568 }
19569
19570 /* Subroutine of dwarf_decode_lines to simplify it.
19571 Return the file name of the psymtab for the given file_entry.
19572 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19573 If space for the result is malloc'd, *NAME_HOLDER will be set.
19574 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19575
19576 static const char *
19577 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19578 const dwarf2_psymtab *pst,
19579 const char *comp_dir,
19580 gdb::unique_xmalloc_ptr<char> *name_holder)
19581 {
19582 const char *include_name = fe.name;
19583 const char *include_name_to_compare = include_name;
19584 const char *pst_filename;
19585 int file_is_pst;
19586
19587 const char *dir_name = fe.include_dir (lh);
19588
19589 gdb::unique_xmalloc_ptr<char> hold_compare;
19590 if (!IS_ABSOLUTE_PATH (include_name)
19591 && (dir_name != NULL || comp_dir != NULL))
19592 {
19593 /* Avoid creating a duplicate psymtab for PST.
19594 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19595 Before we do the comparison, however, we need to account
19596 for DIR_NAME and COMP_DIR.
19597 First prepend dir_name (if non-NULL). If we still don't
19598 have an absolute path prepend comp_dir (if non-NULL).
19599 However, the directory we record in the include-file's
19600 psymtab does not contain COMP_DIR (to match the
19601 corresponding symtab(s)).
19602
19603 Example:
19604
19605 bash$ cd /tmp
19606 bash$ gcc -g ./hello.c
19607 include_name = "hello.c"
19608 dir_name = "."
19609 DW_AT_comp_dir = comp_dir = "/tmp"
19610 DW_AT_name = "./hello.c"
19611
19612 */
19613
19614 if (dir_name != NULL)
19615 {
19616 name_holder->reset (concat (dir_name, SLASH_STRING,
19617 include_name, (char *) NULL));
19618 include_name = name_holder->get ();
19619 include_name_to_compare = include_name;
19620 }
19621 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19622 {
19623 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19624 include_name, (char *) NULL));
19625 include_name_to_compare = hold_compare.get ();
19626 }
19627 }
19628
19629 pst_filename = pst->filename;
19630 gdb::unique_xmalloc_ptr<char> copied_name;
19631 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19632 {
19633 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19634 pst_filename, (char *) NULL));
19635 pst_filename = copied_name.get ();
19636 }
19637
19638 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19639
19640 if (file_is_pst)
19641 return NULL;
19642 return include_name;
19643 }
19644
19645 /* State machine to track the state of the line number program. */
19646
19647 class lnp_state_machine
19648 {
19649 public:
19650 /* Initialize a machine state for the start of a line number
19651 program. */
19652 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19653 bool record_lines_p);
19654
19655 file_entry *current_file ()
19656 {
19657 /* lh->file_names is 0-based, but the file name numbers in the
19658 statement program are 1-based. */
19659 return m_line_header->file_name_at (m_file);
19660 }
19661
19662 /* Record the line in the state machine. END_SEQUENCE is true if
19663 we're processing the end of a sequence. */
19664 void record_line (bool end_sequence);
19665
19666 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19667 nop-out rest of the lines in this sequence. */
19668 void check_line_address (struct dwarf2_cu *cu,
19669 const gdb_byte *line_ptr,
19670 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19671
19672 void handle_set_discriminator (unsigned int discriminator)
19673 {
19674 m_discriminator = discriminator;
19675 m_line_has_non_zero_discriminator |= discriminator != 0;
19676 }
19677
19678 /* Handle DW_LNE_set_address. */
19679 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19680 {
19681 m_op_index = 0;
19682 address += baseaddr;
19683 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19684 }
19685
19686 /* Handle DW_LNS_advance_pc. */
19687 void handle_advance_pc (CORE_ADDR adjust);
19688
19689 /* Handle a special opcode. */
19690 void handle_special_opcode (unsigned char op_code);
19691
19692 /* Handle DW_LNS_advance_line. */
19693 void handle_advance_line (int line_delta)
19694 {
19695 advance_line (line_delta);
19696 }
19697
19698 /* Handle DW_LNS_set_file. */
19699 void handle_set_file (file_name_index file);
19700
19701 /* Handle DW_LNS_negate_stmt. */
19702 void handle_negate_stmt ()
19703 {
19704 m_is_stmt = !m_is_stmt;
19705 }
19706
19707 /* Handle DW_LNS_const_add_pc. */
19708 void handle_const_add_pc ();
19709
19710 /* Handle DW_LNS_fixed_advance_pc. */
19711 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19712 {
19713 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19714 m_op_index = 0;
19715 }
19716
19717 /* Handle DW_LNS_copy. */
19718 void handle_copy ()
19719 {
19720 record_line (false);
19721 m_discriminator = 0;
19722 }
19723
19724 /* Handle DW_LNE_end_sequence. */
19725 void handle_end_sequence ()
19726 {
19727 m_currently_recording_lines = true;
19728 }
19729
19730 private:
19731 /* Advance the line by LINE_DELTA. */
19732 void advance_line (int line_delta)
19733 {
19734 m_line += line_delta;
19735
19736 if (line_delta != 0)
19737 m_line_has_non_zero_discriminator = m_discriminator != 0;
19738 }
19739
19740 struct dwarf2_cu *m_cu;
19741
19742 gdbarch *m_gdbarch;
19743
19744 /* True if we're recording lines.
19745 Otherwise we're building partial symtabs and are just interested in
19746 finding include files mentioned by the line number program. */
19747 bool m_record_lines_p;
19748
19749 /* The line number header. */
19750 line_header *m_line_header;
19751
19752 /* These are part of the standard DWARF line number state machine,
19753 and initialized according to the DWARF spec. */
19754
19755 unsigned char m_op_index = 0;
19756 /* The line table index of the current file. */
19757 file_name_index m_file = 1;
19758 unsigned int m_line = 1;
19759
19760 /* These are initialized in the constructor. */
19761
19762 CORE_ADDR m_address;
19763 bool m_is_stmt;
19764 unsigned int m_discriminator;
19765
19766 /* Additional bits of state we need to track. */
19767
19768 /* The last file that we called dwarf2_start_subfile for.
19769 This is only used for TLLs. */
19770 unsigned int m_last_file = 0;
19771 /* The last file a line number was recorded for. */
19772 struct subfile *m_last_subfile = NULL;
19773
19774 /* When true, record the lines we decode. */
19775 bool m_currently_recording_lines = false;
19776
19777 /* The last line number that was recorded, used to coalesce
19778 consecutive entries for the same line. This can happen, for
19779 example, when discriminators are present. PR 17276. */
19780 unsigned int m_last_line = 0;
19781 bool m_line_has_non_zero_discriminator = false;
19782 };
19783
19784 void
19785 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19786 {
19787 CORE_ADDR addr_adj = (((m_op_index + adjust)
19788 / m_line_header->maximum_ops_per_instruction)
19789 * m_line_header->minimum_instruction_length);
19790 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19791 m_op_index = ((m_op_index + adjust)
19792 % m_line_header->maximum_ops_per_instruction);
19793 }
19794
19795 void
19796 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19797 {
19798 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19799 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19800 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19801 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19802 / m_line_header->maximum_ops_per_instruction)
19803 * m_line_header->minimum_instruction_length);
19804 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19805 m_op_index = ((m_op_index + adj_opcode_d)
19806 % m_line_header->maximum_ops_per_instruction);
19807
19808 int line_delta = m_line_header->line_base + adj_opcode_r;
19809 advance_line (line_delta);
19810 record_line (false);
19811 m_discriminator = 0;
19812 }
19813
19814 void
19815 lnp_state_machine::handle_set_file (file_name_index file)
19816 {
19817 m_file = file;
19818
19819 const file_entry *fe = current_file ();
19820 if (fe == NULL)
19821 dwarf2_debug_line_missing_file_complaint ();
19822 else if (m_record_lines_p)
19823 {
19824 const char *dir = fe->include_dir (m_line_header);
19825
19826 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19827 m_line_has_non_zero_discriminator = m_discriminator != 0;
19828 dwarf2_start_subfile (m_cu, fe->name, dir);
19829 }
19830 }
19831
19832 void
19833 lnp_state_machine::handle_const_add_pc ()
19834 {
19835 CORE_ADDR adjust
19836 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19837
19838 CORE_ADDR addr_adj
19839 = (((m_op_index + adjust)
19840 / m_line_header->maximum_ops_per_instruction)
19841 * m_line_header->minimum_instruction_length);
19842
19843 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19844 m_op_index = ((m_op_index + adjust)
19845 % m_line_header->maximum_ops_per_instruction);
19846 }
19847
19848 /* Return non-zero if we should add LINE to the line number table.
19849 LINE is the line to add, LAST_LINE is the last line that was added,
19850 LAST_SUBFILE is the subfile for LAST_LINE.
19851 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19852 had a non-zero discriminator.
19853
19854 We have to be careful in the presence of discriminators.
19855 E.g., for this line:
19856
19857 for (i = 0; i < 100000; i++);
19858
19859 clang can emit four line number entries for that one line,
19860 each with a different discriminator.
19861 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19862
19863 However, we want gdb to coalesce all four entries into one.
19864 Otherwise the user could stepi into the middle of the line and
19865 gdb would get confused about whether the pc really was in the
19866 middle of the line.
19867
19868 Things are further complicated by the fact that two consecutive
19869 line number entries for the same line is a heuristic used by gcc
19870 to denote the end of the prologue. So we can't just discard duplicate
19871 entries, we have to be selective about it. The heuristic we use is
19872 that we only collapse consecutive entries for the same line if at least
19873 one of those entries has a non-zero discriminator. PR 17276.
19874
19875 Note: Addresses in the line number state machine can never go backwards
19876 within one sequence, thus this coalescing is ok. */
19877
19878 static int
19879 dwarf_record_line_p (struct dwarf2_cu *cu,
19880 unsigned int line, unsigned int last_line,
19881 int line_has_non_zero_discriminator,
19882 struct subfile *last_subfile)
19883 {
19884 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19885 return 1;
19886 if (line != last_line)
19887 return 1;
19888 /* Same line for the same file that we've seen already.
19889 As a last check, for pr 17276, only record the line if the line
19890 has never had a non-zero discriminator. */
19891 if (!line_has_non_zero_discriminator)
19892 return 1;
19893 return 0;
19894 }
19895
19896 /* Use the CU's builder to record line number LINE beginning at
19897 address ADDRESS in the line table of subfile SUBFILE. */
19898
19899 static void
19900 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19901 unsigned int line, CORE_ADDR address,
19902 struct dwarf2_cu *cu)
19903 {
19904 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19905
19906 if (dwarf_line_debug)
19907 {
19908 fprintf_unfiltered (gdb_stdlog,
19909 "Recording line %u, file %s, address %s\n",
19910 line, lbasename (subfile->name),
19911 paddress (gdbarch, address));
19912 }
19913
19914 if (cu != nullptr)
19915 cu->get_builder ()->record_line (subfile, line, addr);
19916 }
19917
19918 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19919 Mark the end of a set of line number records.
19920 The arguments are the same as for dwarf_record_line_1.
19921 If SUBFILE is NULL the request is ignored. */
19922
19923 static void
19924 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19925 CORE_ADDR address, struct dwarf2_cu *cu)
19926 {
19927 if (subfile == NULL)
19928 return;
19929
19930 if (dwarf_line_debug)
19931 {
19932 fprintf_unfiltered (gdb_stdlog,
19933 "Finishing current line, file %s, address %s\n",
19934 lbasename (subfile->name),
19935 paddress (gdbarch, address));
19936 }
19937
19938 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
19939 }
19940
19941 void
19942 lnp_state_machine::record_line (bool end_sequence)
19943 {
19944 if (dwarf_line_debug)
19945 {
19946 fprintf_unfiltered (gdb_stdlog,
19947 "Processing actual line %u: file %u,"
19948 " address %s, is_stmt %u, discrim %u%s\n",
19949 m_line, m_file,
19950 paddress (m_gdbarch, m_address),
19951 m_is_stmt, m_discriminator,
19952 (end_sequence ? "\t(end sequence)" : ""));
19953 }
19954
19955 file_entry *fe = current_file ();
19956
19957 if (fe == NULL)
19958 dwarf2_debug_line_missing_file_complaint ();
19959 /* For now we ignore lines not starting on an instruction boundary.
19960 But not when processing end_sequence for compatibility with the
19961 previous version of the code. */
19962 else if (m_op_index == 0 || end_sequence)
19963 {
19964 fe->included_p = 1;
19965 if (m_record_lines_p
19966 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
19967 {
19968 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19969 || end_sequence)
19970 {
19971 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19972 m_currently_recording_lines ? m_cu : nullptr);
19973 }
19974
19975 if (!end_sequence)
19976 {
19977 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19978 m_line_has_non_zero_discriminator,
19979 m_last_subfile))
19980 {
19981 buildsym_compunit *builder = m_cu->get_builder ();
19982 dwarf_record_line_1 (m_gdbarch,
19983 builder->get_current_subfile (),
19984 m_line, m_address,
19985 m_currently_recording_lines ? m_cu : nullptr);
19986 }
19987 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19988 m_last_line = m_line;
19989 }
19990 }
19991 }
19992 }
19993
19994 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19995 line_header *lh, bool record_lines_p)
19996 {
19997 m_cu = cu;
19998 m_gdbarch = arch;
19999 m_record_lines_p = record_lines_p;
20000 m_line_header = lh;
20001
20002 m_currently_recording_lines = true;
20003
20004 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20005 was a line entry for it so that the backend has a chance to adjust it
20006 and also record it in case it needs it. This is currently used by MIPS
20007 code, cf. `mips_adjust_dwarf2_line'. */
20008 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20009 m_is_stmt = lh->default_is_stmt;
20010 m_discriminator = 0;
20011 }
20012
20013 void
20014 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20015 const gdb_byte *line_ptr,
20016 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20017 {
20018 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20019 the pc range of the CU. However, we restrict the test to only ADDRESS
20020 values of zero to preserve GDB's previous behaviour which is to handle
20021 the specific case of a function being GC'd by the linker. */
20022
20023 if (address == 0 && address < unrelocated_lowpc)
20024 {
20025 /* This line table is for a function which has been
20026 GCd by the linker. Ignore it. PR gdb/12528 */
20027
20028 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20029 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20030
20031 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20032 line_offset, objfile_name (objfile));
20033 m_currently_recording_lines = false;
20034 /* Note: m_currently_recording_lines is left as false until we see
20035 DW_LNE_end_sequence. */
20036 }
20037 }
20038
20039 /* Subroutine of dwarf_decode_lines to simplify it.
20040 Process the line number information in LH.
20041 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20042 program in order to set included_p for every referenced header. */
20043
20044 static void
20045 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20046 const int decode_for_pst_p, CORE_ADDR lowpc)
20047 {
20048 const gdb_byte *line_ptr, *extended_end;
20049 const gdb_byte *line_end;
20050 unsigned int bytes_read, extended_len;
20051 unsigned char op_code, extended_op;
20052 CORE_ADDR baseaddr;
20053 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20054 bfd *abfd = objfile->obfd;
20055 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20056 /* True if we're recording line info (as opposed to building partial
20057 symtabs and just interested in finding include files mentioned by
20058 the line number program). */
20059 bool record_lines_p = !decode_for_pst_p;
20060
20061 baseaddr = objfile->text_section_offset ();
20062
20063 line_ptr = lh->statement_program_start;
20064 line_end = lh->statement_program_end;
20065
20066 /* Read the statement sequences until there's nothing left. */
20067 while (line_ptr < line_end)
20068 {
20069 /* The DWARF line number program state machine. Reset the state
20070 machine at the start of each sequence. */
20071 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20072 bool end_sequence = false;
20073
20074 if (record_lines_p)
20075 {
20076 /* Start a subfile for the current file of the state
20077 machine. */
20078 const file_entry *fe = state_machine.current_file ();
20079
20080 if (fe != NULL)
20081 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20082 }
20083
20084 /* Decode the table. */
20085 while (line_ptr < line_end && !end_sequence)
20086 {
20087 op_code = read_1_byte (abfd, line_ptr);
20088 line_ptr += 1;
20089
20090 if (op_code >= lh->opcode_base)
20091 {
20092 /* Special opcode. */
20093 state_machine.handle_special_opcode (op_code);
20094 }
20095 else switch (op_code)
20096 {
20097 case DW_LNS_extended_op:
20098 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20099 &bytes_read);
20100 line_ptr += bytes_read;
20101 extended_end = line_ptr + extended_len;
20102 extended_op = read_1_byte (abfd, line_ptr);
20103 line_ptr += 1;
20104 switch (extended_op)
20105 {
20106 case DW_LNE_end_sequence:
20107 state_machine.handle_end_sequence ();
20108 end_sequence = true;
20109 break;
20110 case DW_LNE_set_address:
20111 {
20112 CORE_ADDR address
20113 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20114 line_ptr += bytes_read;
20115
20116 state_machine.check_line_address (cu, line_ptr,
20117 lowpc - baseaddr, address);
20118 state_machine.handle_set_address (baseaddr, address);
20119 }
20120 break;
20121 case DW_LNE_define_file:
20122 {
20123 const char *cur_file;
20124 unsigned int mod_time, length;
20125 dir_index dindex;
20126
20127 cur_file = read_direct_string (abfd, line_ptr,
20128 &bytes_read);
20129 line_ptr += bytes_read;
20130 dindex = (dir_index)
20131 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20132 line_ptr += bytes_read;
20133 mod_time =
20134 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20135 line_ptr += bytes_read;
20136 length =
20137 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20138 line_ptr += bytes_read;
20139 lh->add_file_name (cur_file, dindex, mod_time, length);
20140 }
20141 break;
20142 case DW_LNE_set_discriminator:
20143 {
20144 /* The discriminator is not interesting to the
20145 debugger; just ignore it. We still need to
20146 check its value though:
20147 if there are consecutive entries for the same
20148 (non-prologue) line we want to coalesce them.
20149 PR 17276. */
20150 unsigned int discr
20151 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20152 line_ptr += bytes_read;
20153
20154 state_machine.handle_set_discriminator (discr);
20155 }
20156 break;
20157 default:
20158 complaint (_("mangled .debug_line section"));
20159 return;
20160 }
20161 /* Make sure that we parsed the extended op correctly. If e.g.
20162 we expected a different address size than the producer used,
20163 we may have read the wrong number of bytes. */
20164 if (line_ptr != extended_end)
20165 {
20166 complaint (_("mangled .debug_line section"));
20167 return;
20168 }
20169 break;
20170 case DW_LNS_copy:
20171 state_machine.handle_copy ();
20172 break;
20173 case DW_LNS_advance_pc:
20174 {
20175 CORE_ADDR adjust
20176 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20177 line_ptr += bytes_read;
20178
20179 state_machine.handle_advance_pc (adjust);
20180 }
20181 break;
20182 case DW_LNS_advance_line:
20183 {
20184 int line_delta
20185 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20186 line_ptr += bytes_read;
20187
20188 state_machine.handle_advance_line (line_delta);
20189 }
20190 break;
20191 case DW_LNS_set_file:
20192 {
20193 file_name_index file
20194 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20195 &bytes_read);
20196 line_ptr += bytes_read;
20197
20198 state_machine.handle_set_file (file);
20199 }
20200 break;
20201 case DW_LNS_set_column:
20202 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20203 line_ptr += bytes_read;
20204 break;
20205 case DW_LNS_negate_stmt:
20206 state_machine.handle_negate_stmt ();
20207 break;
20208 case DW_LNS_set_basic_block:
20209 break;
20210 /* Add to the address register of the state machine the
20211 address increment value corresponding to special opcode
20212 255. I.e., this value is scaled by the minimum
20213 instruction length since special opcode 255 would have
20214 scaled the increment. */
20215 case DW_LNS_const_add_pc:
20216 state_machine.handle_const_add_pc ();
20217 break;
20218 case DW_LNS_fixed_advance_pc:
20219 {
20220 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20221 line_ptr += 2;
20222
20223 state_machine.handle_fixed_advance_pc (addr_adj);
20224 }
20225 break;
20226 default:
20227 {
20228 /* Unknown standard opcode, ignore it. */
20229 int i;
20230
20231 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20232 {
20233 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20234 line_ptr += bytes_read;
20235 }
20236 }
20237 }
20238 }
20239
20240 if (!end_sequence)
20241 dwarf2_debug_line_missing_end_sequence_complaint ();
20242
20243 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20244 in which case we still finish recording the last line). */
20245 state_machine.record_line (true);
20246 }
20247 }
20248
20249 /* Decode the Line Number Program (LNP) for the given line_header
20250 structure and CU. The actual information extracted and the type
20251 of structures created from the LNP depends on the value of PST.
20252
20253 1. If PST is NULL, then this procedure uses the data from the program
20254 to create all necessary symbol tables, and their linetables.
20255
20256 2. If PST is not NULL, this procedure reads the program to determine
20257 the list of files included by the unit represented by PST, and
20258 builds all the associated partial symbol tables.
20259
20260 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20261 It is used for relative paths in the line table.
20262 NOTE: When processing partial symtabs (pst != NULL),
20263 comp_dir == pst->dirname.
20264
20265 NOTE: It is important that psymtabs have the same file name (via strcmp)
20266 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20267 symtab we don't use it in the name of the psymtabs we create.
20268 E.g. expand_line_sal requires this when finding psymtabs to expand.
20269 A good testcase for this is mb-inline.exp.
20270
20271 LOWPC is the lowest address in CU (or 0 if not known).
20272
20273 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20274 for its PC<->lines mapping information. Otherwise only the filename
20275 table is read in. */
20276
20277 static void
20278 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20279 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20280 CORE_ADDR lowpc, int decode_mapping)
20281 {
20282 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20283 const int decode_for_pst_p = (pst != NULL);
20284
20285 if (decode_mapping)
20286 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20287
20288 if (decode_for_pst_p)
20289 {
20290 /* Now that we're done scanning the Line Header Program, we can
20291 create the psymtab of each included file. */
20292 for (auto &file_entry : lh->file_names ())
20293 if (file_entry.included_p == 1)
20294 {
20295 gdb::unique_xmalloc_ptr<char> name_holder;
20296 const char *include_name =
20297 psymtab_include_file_name (lh, file_entry, pst,
20298 comp_dir, &name_holder);
20299 if (include_name != NULL)
20300 dwarf2_create_include_psymtab (include_name, pst, objfile);
20301 }
20302 }
20303 else
20304 {
20305 /* Make sure a symtab is created for every file, even files
20306 which contain only variables (i.e. no code with associated
20307 line numbers). */
20308 buildsym_compunit *builder = cu->get_builder ();
20309 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20310
20311 for (auto &fe : lh->file_names ())
20312 {
20313 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20314 if (builder->get_current_subfile ()->symtab == NULL)
20315 {
20316 builder->get_current_subfile ()->symtab
20317 = allocate_symtab (cust,
20318 builder->get_current_subfile ()->name);
20319 }
20320 fe.symtab = builder->get_current_subfile ()->symtab;
20321 }
20322 }
20323 }
20324
20325 /* Start a subfile for DWARF. FILENAME is the name of the file and
20326 DIRNAME the name of the source directory which contains FILENAME
20327 or NULL if not known.
20328 This routine tries to keep line numbers from identical absolute and
20329 relative file names in a common subfile.
20330
20331 Using the `list' example from the GDB testsuite, which resides in
20332 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20333 of /srcdir/list0.c yields the following debugging information for list0.c:
20334
20335 DW_AT_name: /srcdir/list0.c
20336 DW_AT_comp_dir: /compdir
20337 files.files[0].name: list0.h
20338 files.files[0].dir: /srcdir
20339 files.files[1].name: list0.c
20340 files.files[1].dir: /srcdir
20341
20342 The line number information for list0.c has to end up in a single
20343 subfile, so that `break /srcdir/list0.c:1' works as expected.
20344 start_subfile will ensure that this happens provided that we pass the
20345 concatenation of files.files[1].dir and files.files[1].name as the
20346 subfile's name. */
20347
20348 static void
20349 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20350 const char *dirname)
20351 {
20352 gdb::unique_xmalloc_ptr<char> copy;
20353
20354 /* In order not to lose the line information directory,
20355 we concatenate it to the filename when it makes sense.
20356 Note that the Dwarf3 standard says (speaking of filenames in line
20357 information): ``The directory index is ignored for file names
20358 that represent full path names''. Thus ignoring dirname in the
20359 `else' branch below isn't an issue. */
20360
20361 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20362 {
20363 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20364 filename = copy.get ();
20365 }
20366
20367 cu->get_builder ()->start_subfile (filename);
20368 }
20369
20370 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20371 buildsym_compunit constructor. */
20372
20373 struct compunit_symtab *
20374 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20375 CORE_ADDR low_pc)
20376 {
20377 gdb_assert (m_builder == nullptr);
20378
20379 m_builder.reset (new struct buildsym_compunit
20380 (per_cu->dwarf2_per_objfile->objfile,
20381 name, comp_dir, language, low_pc));
20382
20383 list_in_scope = get_builder ()->get_file_symbols ();
20384
20385 get_builder ()->record_debugformat ("DWARF 2");
20386 get_builder ()->record_producer (producer);
20387
20388 processing_has_namespace_info = false;
20389
20390 return get_builder ()->get_compunit_symtab ();
20391 }
20392
20393 static void
20394 var_decode_location (struct attribute *attr, struct symbol *sym,
20395 struct dwarf2_cu *cu)
20396 {
20397 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20398 struct comp_unit_head *cu_header = &cu->header;
20399
20400 /* NOTE drow/2003-01-30: There used to be a comment and some special
20401 code here to turn a symbol with DW_AT_external and a
20402 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20403 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20404 with some versions of binutils) where shared libraries could have
20405 relocations against symbols in their debug information - the
20406 minimal symbol would have the right address, but the debug info
20407 would not. It's no longer necessary, because we will explicitly
20408 apply relocations when we read in the debug information now. */
20409
20410 /* A DW_AT_location attribute with no contents indicates that a
20411 variable has been optimized away. */
20412 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20413 {
20414 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20415 return;
20416 }
20417
20418 /* Handle one degenerate form of location expression specially, to
20419 preserve GDB's previous behavior when section offsets are
20420 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20421 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20422
20423 if (attr->form_is_block ()
20424 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20425 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20426 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20427 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20428 && (DW_BLOCK (attr)->size
20429 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20430 {
20431 unsigned int dummy;
20432
20433 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20434 SET_SYMBOL_VALUE_ADDRESS
20435 (sym, cu->header.read_address (objfile->obfd,
20436 DW_BLOCK (attr)->data + 1,
20437 &dummy));
20438 else
20439 SET_SYMBOL_VALUE_ADDRESS
20440 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20441 &dummy));
20442 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20443 fixup_symbol_section (sym, objfile);
20444 SET_SYMBOL_VALUE_ADDRESS
20445 (sym,
20446 SYMBOL_VALUE_ADDRESS (sym)
20447 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20448 return;
20449 }
20450
20451 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20452 expression evaluator, and use LOC_COMPUTED only when necessary
20453 (i.e. when the value of a register or memory location is
20454 referenced, or a thread-local block, etc.). Then again, it might
20455 not be worthwhile. I'm assuming that it isn't unless performance
20456 or memory numbers show me otherwise. */
20457
20458 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20459
20460 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20461 cu->has_loclist = true;
20462 }
20463
20464 /* Given a pointer to a DWARF information entry, figure out if we need
20465 to make a symbol table entry for it, and if so, create a new entry
20466 and return a pointer to it.
20467 If TYPE is NULL, determine symbol type from the die, otherwise
20468 used the passed type.
20469 If SPACE is not NULL, use it to hold the new symbol. If it is
20470 NULL, allocate a new symbol on the objfile's obstack. */
20471
20472 static struct symbol *
20473 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20474 struct symbol *space)
20475 {
20476 struct dwarf2_per_objfile *dwarf2_per_objfile
20477 = cu->per_cu->dwarf2_per_objfile;
20478 struct objfile *objfile = dwarf2_per_objfile->objfile;
20479 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20480 struct symbol *sym = NULL;
20481 const char *name;
20482 struct attribute *attr = NULL;
20483 struct attribute *attr2 = NULL;
20484 CORE_ADDR baseaddr;
20485 struct pending **list_to_add = NULL;
20486
20487 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20488
20489 baseaddr = objfile->text_section_offset ();
20490
20491 name = dwarf2_name (die, cu);
20492 if (name)
20493 {
20494 const char *linkagename;
20495 int suppress_add = 0;
20496
20497 if (space)
20498 sym = space;
20499 else
20500 sym = allocate_symbol (objfile);
20501 OBJSTAT (objfile, n_syms++);
20502
20503 /* Cache this symbol's name and the name's demangled form (if any). */
20504 sym->set_language (cu->language, &objfile->objfile_obstack);
20505 linkagename = dwarf2_physname (name, die, cu);
20506 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20507
20508 /* Fortran does not have mangling standard and the mangling does differ
20509 between gfortran, iFort etc. */
20510 if (cu->language == language_fortran
20511 && symbol_get_demangled_name (sym) == NULL)
20512 symbol_set_demangled_name (sym,
20513 dwarf2_full_name (name, die, cu),
20514 NULL);
20515
20516 /* Default assumptions.
20517 Use the passed type or decode it from the die. */
20518 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20519 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20520 if (type != NULL)
20521 SYMBOL_TYPE (sym) = type;
20522 else
20523 SYMBOL_TYPE (sym) = die_type (die, cu);
20524 attr = dwarf2_attr (die,
20525 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20526 cu);
20527 if (attr != nullptr)
20528 {
20529 SYMBOL_LINE (sym) = DW_UNSND (attr);
20530 }
20531
20532 attr = dwarf2_attr (die,
20533 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20534 cu);
20535 if (attr != nullptr)
20536 {
20537 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20538 struct file_entry *fe;
20539
20540 if (cu->line_header != NULL)
20541 fe = cu->line_header->file_name_at (file_index);
20542 else
20543 fe = NULL;
20544
20545 if (fe == NULL)
20546 complaint (_("file index out of range"));
20547 else
20548 symbol_set_symtab (sym, fe->symtab);
20549 }
20550
20551 switch (die->tag)
20552 {
20553 case DW_TAG_label:
20554 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20555 if (attr != nullptr)
20556 {
20557 CORE_ADDR addr;
20558
20559 addr = attr->value_as_address ();
20560 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20561 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20562 }
20563 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20564 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20565 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20566 add_symbol_to_list (sym, cu->list_in_scope);
20567 break;
20568 case DW_TAG_subprogram:
20569 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20570 finish_block. */
20571 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20572 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20573 if ((attr2 && (DW_UNSND (attr2) != 0))
20574 || cu->language == language_ada
20575 || cu->language == language_fortran)
20576 {
20577 /* Subprograms marked external are stored as a global symbol.
20578 Ada and Fortran subprograms, whether marked external or
20579 not, are always stored as a global symbol, because we want
20580 to be able to access them globally. For instance, we want
20581 to be able to break on a nested subprogram without having
20582 to specify the context. */
20583 list_to_add = cu->get_builder ()->get_global_symbols ();
20584 }
20585 else
20586 {
20587 list_to_add = cu->list_in_scope;
20588 }
20589 break;
20590 case DW_TAG_inlined_subroutine:
20591 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20592 finish_block. */
20593 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20594 SYMBOL_INLINED (sym) = 1;
20595 list_to_add = cu->list_in_scope;
20596 break;
20597 case DW_TAG_template_value_param:
20598 suppress_add = 1;
20599 /* Fall through. */
20600 case DW_TAG_constant:
20601 case DW_TAG_variable:
20602 case DW_TAG_member:
20603 /* Compilation with minimal debug info may result in
20604 variables with missing type entries. Change the
20605 misleading `void' type to something sensible. */
20606 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20607 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20608
20609 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20610 /* In the case of DW_TAG_member, we should only be called for
20611 static const members. */
20612 if (die->tag == DW_TAG_member)
20613 {
20614 /* dwarf2_add_field uses die_is_declaration,
20615 so we do the same. */
20616 gdb_assert (die_is_declaration (die, cu));
20617 gdb_assert (attr);
20618 }
20619 if (attr != nullptr)
20620 {
20621 dwarf2_const_value (attr, sym, cu);
20622 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20623 if (!suppress_add)
20624 {
20625 if (attr2 && (DW_UNSND (attr2) != 0))
20626 list_to_add = cu->get_builder ()->get_global_symbols ();
20627 else
20628 list_to_add = cu->list_in_scope;
20629 }
20630 break;
20631 }
20632 attr = dwarf2_attr (die, DW_AT_location, cu);
20633 if (attr != nullptr)
20634 {
20635 var_decode_location (attr, sym, cu);
20636 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20637
20638 /* Fortran explicitly imports any global symbols to the local
20639 scope by DW_TAG_common_block. */
20640 if (cu->language == language_fortran && die->parent
20641 && die->parent->tag == DW_TAG_common_block)
20642 attr2 = NULL;
20643
20644 if (SYMBOL_CLASS (sym) == LOC_STATIC
20645 && SYMBOL_VALUE_ADDRESS (sym) == 0
20646 && !dwarf2_per_objfile->has_section_at_zero)
20647 {
20648 /* When a static variable is eliminated by the linker,
20649 the corresponding debug information is not stripped
20650 out, but the variable address is set to null;
20651 do not add such variables into symbol table. */
20652 }
20653 else if (attr2 && (DW_UNSND (attr2) != 0))
20654 {
20655 if (SYMBOL_CLASS (sym) == LOC_STATIC
20656 && (objfile->flags & OBJF_MAINLINE) == 0
20657 && dwarf2_per_objfile->can_copy)
20658 {
20659 /* A global static variable might be subject to
20660 copy relocation. We first check for a local
20661 minsym, though, because maybe the symbol was
20662 marked hidden, in which case this would not
20663 apply. */
20664 bound_minimal_symbol found
20665 = (lookup_minimal_symbol_linkage
20666 (sym->linkage_name (), objfile));
20667 if (found.minsym != nullptr)
20668 sym->maybe_copied = 1;
20669 }
20670
20671 /* A variable with DW_AT_external is never static,
20672 but it may be block-scoped. */
20673 list_to_add
20674 = ((cu->list_in_scope
20675 == cu->get_builder ()->get_file_symbols ())
20676 ? cu->get_builder ()->get_global_symbols ()
20677 : cu->list_in_scope);
20678 }
20679 else
20680 list_to_add = cu->list_in_scope;
20681 }
20682 else
20683 {
20684 /* We do not know the address of this symbol.
20685 If it is an external symbol and we have type information
20686 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20687 The address of the variable will then be determined from
20688 the minimal symbol table whenever the variable is
20689 referenced. */
20690 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20691
20692 /* Fortran explicitly imports any global symbols to the local
20693 scope by DW_TAG_common_block. */
20694 if (cu->language == language_fortran && die->parent
20695 && die->parent->tag == DW_TAG_common_block)
20696 {
20697 /* SYMBOL_CLASS doesn't matter here because
20698 read_common_block is going to reset it. */
20699 if (!suppress_add)
20700 list_to_add = cu->list_in_scope;
20701 }
20702 else if (attr2 && (DW_UNSND (attr2) != 0)
20703 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20704 {
20705 /* A variable with DW_AT_external is never static, but it
20706 may be block-scoped. */
20707 list_to_add
20708 = ((cu->list_in_scope
20709 == cu->get_builder ()->get_file_symbols ())
20710 ? cu->get_builder ()->get_global_symbols ()
20711 : cu->list_in_scope);
20712
20713 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20714 }
20715 else if (!die_is_declaration (die, cu))
20716 {
20717 /* Use the default LOC_OPTIMIZED_OUT class. */
20718 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20719 if (!suppress_add)
20720 list_to_add = cu->list_in_scope;
20721 }
20722 }
20723 break;
20724 case DW_TAG_formal_parameter:
20725 {
20726 /* If we are inside a function, mark this as an argument. If
20727 not, we might be looking at an argument to an inlined function
20728 when we do not have enough information to show inlined frames;
20729 pretend it's a local variable in that case so that the user can
20730 still see it. */
20731 struct context_stack *curr
20732 = cu->get_builder ()->get_current_context_stack ();
20733 if (curr != nullptr && curr->name != nullptr)
20734 SYMBOL_IS_ARGUMENT (sym) = 1;
20735 attr = dwarf2_attr (die, DW_AT_location, cu);
20736 if (attr != nullptr)
20737 {
20738 var_decode_location (attr, sym, cu);
20739 }
20740 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20741 if (attr != nullptr)
20742 {
20743 dwarf2_const_value (attr, sym, cu);
20744 }
20745
20746 list_to_add = cu->list_in_scope;
20747 }
20748 break;
20749 case DW_TAG_unspecified_parameters:
20750 /* From varargs functions; gdb doesn't seem to have any
20751 interest in this information, so just ignore it for now.
20752 (FIXME?) */
20753 break;
20754 case DW_TAG_template_type_param:
20755 suppress_add = 1;
20756 /* Fall through. */
20757 case DW_TAG_class_type:
20758 case DW_TAG_interface_type:
20759 case DW_TAG_structure_type:
20760 case DW_TAG_union_type:
20761 case DW_TAG_set_type:
20762 case DW_TAG_enumeration_type:
20763 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20764 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20765
20766 {
20767 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20768 really ever be static objects: otherwise, if you try
20769 to, say, break of a class's method and you're in a file
20770 which doesn't mention that class, it won't work unless
20771 the check for all static symbols in lookup_symbol_aux
20772 saves you. See the OtherFileClass tests in
20773 gdb.c++/namespace.exp. */
20774
20775 if (!suppress_add)
20776 {
20777 buildsym_compunit *builder = cu->get_builder ();
20778 list_to_add
20779 = (cu->list_in_scope == builder->get_file_symbols ()
20780 && cu->language == language_cplus
20781 ? builder->get_global_symbols ()
20782 : cu->list_in_scope);
20783
20784 /* The semantics of C++ state that "struct foo {
20785 ... }" also defines a typedef for "foo". */
20786 if (cu->language == language_cplus
20787 || cu->language == language_ada
20788 || cu->language == language_d
20789 || cu->language == language_rust)
20790 {
20791 /* The symbol's name is already allocated along
20792 with this objfile, so we don't need to
20793 duplicate it for the type. */
20794 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20795 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20796 }
20797 }
20798 }
20799 break;
20800 case DW_TAG_typedef:
20801 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20802 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20803 list_to_add = cu->list_in_scope;
20804 break;
20805 case DW_TAG_base_type:
20806 case DW_TAG_subrange_type:
20807 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20808 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20809 list_to_add = cu->list_in_scope;
20810 break;
20811 case DW_TAG_enumerator:
20812 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20813 if (attr != nullptr)
20814 {
20815 dwarf2_const_value (attr, sym, cu);
20816 }
20817 {
20818 /* NOTE: carlton/2003-11-10: See comment above in the
20819 DW_TAG_class_type, etc. block. */
20820
20821 list_to_add
20822 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20823 && cu->language == language_cplus
20824 ? cu->get_builder ()->get_global_symbols ()
20825 : cu->list_in_scope);
20826 }
20827 break;
20828 case DW_TAG_imported_declaration:
20829 case DW_TAG_namespace:
20830 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20831 list_to_add = cu->get_builder ()->get_global_symbols ();
20832 break;
20833 case DW_TAG_module:
20834 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20835 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20836 list_to_add = cu->get_builder ()->get_global_symbols ();
20837 break;
20838 case DW_TAG_common_block:
20839 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20840 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20841 add_symbol_to_list (sym, cu->list_in_scope);
20842 break;
20843 default:
20844 /* Not a tag we recognize. Hopefully we aren't processing
20845 trash data, but since we must specifically ignore things
20846 we don't recognize, there is nothing else we should do at
20847 this point. */
20848 complaint (_("unsupported tag: '%s'"),
20849 dwarf_tag_name (die->tag));
20850 break;
20851 }
20852
20853 if (suppress_add)
20854 {
20855 sym->hash_next = objfile->template_symbols;
20856 objfile->template_symbols = sym;
20857 list_to_add = NULL;
20858 }
20859
20860 if (list_to_add != NULL)
20861 add_symbol_to_list (sym, list_to_add);
20862
20863 /* For the benefit of old versions of GCC, check for anonymous
20864 namespaces based on the demangled name. */
20865 if (!cu->processing_has_namespace_info
20866 && cu->language == language_cplus)
20867 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20868 }
20869 return (sym);
20870 }
20871
20872 /* Given an attr with a DW_FORM_dataN value in host byte order,
20873 zero-extend it as appropriate for the symbol's type. The DWARF
20874 standard (v4) is not entirely clear about the meaning of using
20875 DW_FORM_dataN for a constant with a signed type, where the type is
20876 wider than the data. The conclusion of a discussion on the DWARF
20877 list was that this is unspecified. We choose to always zero-extend
20878 because that is the interpretation long in use by GCC. */
20879
20880 static gdb_byte *
20881 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20882 struct dwarf2_cu *cu, LONGEST *value, int bits)
20883 {
20884 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20885 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20886 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20887 LONGEST l = DW_UNSND (attr);
20888
20889 if (bits < sizeof (*value) * 8)
20890 {
20891 l &= ((LONGEST) 1 << bits) - 1;
20892 *value = l;
20893 }
20894 else if (bits == sizeof (*value) * 8)
20895 *value = l;
20896 else
20897 {
20898 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20899 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20900 return bytes;
20901 }
20902
20903 return NULL;
20904 }
20905
20906 /* Read a constant value from an attribute. Either set *VALUE, or if
20907 the value does not fit in *VALUE, set *BYTES - either already
20908 allocated on the objfile obstack, or newly allocated on OBSTACK,
20909 or, set *BATON, if we translated the constant to a location
20910 expression. */
20911
20912 static void
20913 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20914 const char *name, struct obstack *obstack,
20915 struct dwarf2_cu *cu,
20916 LONGEST *value, const gdb_byte **bytes,
20917 struct dwarf2_locexpr_baton **baton)
20918 {
20919 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20920 struct comp_unit_head *cu_header = &cu->header;
20921 struct dwarf_block *blk;
20922 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20923 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20924
20925 *value = 0;
20926 *bytes = NULL;
20927 *baton = NULL;
20928
20929 switch (attr->form)
20930 {
20931 case DW_FORM_addr:
20932 case DW_FORM_addrx:
20933 case DW_FORM_GNU_addr_index:
20934 {
20935 gdb_byte *data;
20936
20937 if (TYPE_LENGTH (type) != cu_header->addr_size)
20938 dwarf2_const_value_length_mismatch_complaint (name,
20939 cu_header->addr_size,
20940 TYPE_LENGTH (type));
20941 /* Symbols of this form are reasonably rare, so we just
20942 piggyback on the existing location code rather than writing
20943 a new implementation of symbol_computed_ops. */
20944 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20945 (*baton)->per_cu = cu->per_cu;
20946 gdb_assert ((*baton)->per_cu);
20947
20948 (*baton)->size = 2 + cu_header->addr_size;
20949 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20950 (*baton)->data = data;
20951
20952 data[0] = DW_OP_addr;
20953 store_unsigned_integer (&data[1], cu_header->addr_size,
20954 byte_order, DW_ADDR (attr));
20955 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20956 }
20957 break;
20958 case DW_FORM_string:
20959 case DW_FORM_strp:
20960 case DW_FORM_strx:
20961 case DW_FORM_GNU_str_index:
20962 case DW_FORM_GNU_strp_alt:
20963 /* DW_STRING is already allocated on the objfile obstack, point
20964 directly to it. */
20965 *bytes = (const gdb_byte *) DW_STRING (attr);
20966 break;
20967 case DW_FORM_block1:
20968 case DW_FORM_block2:
20969 case DW_FORM_block4:
20970 case DW_FORM_block:
20971 case DW_FORM_exprloc:
20972 case DW_FORM_data16:
20973 blk = DW_BLOCK (attr);
20974 if (TYPE_LENGTH (type) != blk->size)
20975 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20976 TYPE_LENGTH (type));
20977 *bytes = blk->data;
20978 break;
20979
20980 /* The DW_AT_const_value attributes are supposed to carry the
20981 symbol's value "represented as it would be on the target
20982 architecture." By the time we get here, it's already been
20983 converted to host endianness, so we just need to sign- or
20984 zero-extend it as appropriate. */
20985 case DW_FORM_data1:
20986 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20987 break;
20988 case DW_FORM_data2:
20989 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20990 break;
20991 case DW_FORM_data4:
20992 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20993 break;
20994 case DW_FORM_data8:
20995 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20996 break;
20997
20998 case DW_FORM_sdata:
20999 case DW_FORM_implicit_const:
21000 *value = DW_SND (attr);
21001 break;
21002
21003 case DW_FORM_udata:
21004 *value = DW_UNSND (attr);
21005 break;
21006
21007 default:
21008 complaint (_("unsupported const value attribute form: '%s'"),
21009 dwarf_form_name (attr->form));
21010 *value = 0;
21011 break;
21012 }
21013 }
21014
21015
21016 /* Copy constant value from an attribute to a symbol. */
21017
21018 static void
21019 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21020 struct dwarf2_cu *cu)
21021 {
21022 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21023 LONGEST value;
21024 const gdb_byte *bytes;
21025 struct dwarf2_locexpr_baton *baton;
21026
21027 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21028 sym->print_name (),
21029 &objfile->objfile_obstack, cu,
21030 &value, &bytes, &baton);
21031
21032 if (baton != NULL)
21033 {
21034 SYMBOL_LOCATION_BATON (sym) = baton;
21035 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21036 }
21037 else if (bytes != NULL)
21038 {
21039 SYMBOL_VALUE_BYTES (sym) = bytes;
21040 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21041 }
21042 else
21043 {
21044 SYMBOL_VALUE (sym) = value;
21045 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21046 }
21047 }
21048
21049 /* Return the type of the die in question using its DW_AT_type attribute. */
21050
21051 static struct type *
21052 die_type (struct die_info *die, struct dwarf2_cu *cu)
21053 {
21054 struct attribute *type_attr;
21055
21056 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21057 if (!type_attr)
21058 {
21059 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21060 /* A missing DW_AT_type represents a void type. */
21061 return objfile_type (objfile)->builtin_void;
21062 }
21063
21064 return lookup_die_type (die, type_attr, cu);
21065 }
21066
21067 /* True iff CU's producer generates GNAT Ada auxiliary information
21068 that allows to find parallel types through that information instead
21069 of having to do expensive parallel lookups by type name. */
21070
21071 static int
21072 need_gnat_info (struct dwarf2_cu *cu)
21073 {
21074 /* Assume that the Ada compiler was GNAT, which always produces
21075 the auxiliary information. */
21076 return (cu->language == language_ada);
21077 }
21078
21079 /* Return the auxiliary type of the die in question using its
21080 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21081 attribute is not present. */
21082
21083 static struct type *
21084 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21085 {
21086 struct attribute *type_attr;
21087
21088 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21089 if (!type_attr)
21090 return NULL;
21091
21092 return lookup_die_type (die, type_attr, cu);
21093 }
21094
21095 /* If DIE has a descriptive_type attribute, then set the TYPE's
21096 descriptive type accordingly. */
21097
21098 static void
21099 set_descriptive_type (struct type *type, struct die_info *die,
21100 struct dwarf2_cu *cu)
21101 {
21102 struct type *descriptive_type = die_descriptive_type (die, cu);
21103
21104 if (descriptive_type)
21105 {
21106 ALLOCATE_GNAT_AUX_TYPE (type);
21107 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21108 }
21109 }
21110
21111 /* Return the containing type of the die in question using its
21112 DW_AT_containing_type attribute. */
21113
21114 static struct type *
21115 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21116 {
21117 struct attribute *type_attr;
21118 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21119
21120 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21121 if (!type_attr)
21122 error (_("Dwarf Error: Problem turning containing type into gdb type "
21123 "[in module %s]"), objfile_name (objfile));
21124
21125 return lookup_die_type (die, type_attr, cu);
21126 }
21127
21128 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21129
21130 static struct type *
21131 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21132 {
21133 struct dwarf2_per_objfile *dwarf2_per_objfile
21134 = cu->per_cu->dwarf2_per_objfile;
21135 struct objfile *objfile = dwarf2_per_objfile->objfile;
21136 char *saved;
21137
21138 std::string message
21139 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21140 objfile_name (objfile),
21141 sect_offset_str (cu->header.sect_off),
21142 sect_offset_str (die->sect_off));
21143 saved = obstack_strdup (&objfile->objfile_obstack, message);
21144
21145 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21146 }
21147
21148 /* Look up the type of DIE in CU using its type attribute ATTR.
21149 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21150 DW_AT_containing_type.
21151 If there is no type substitute an error marker. */
21152
21153 static struct type *
21154 lookup_die_type (struct die_info *die, const struct attribute *attr,
21155 struct dwarf2_cu *cu)
21156 {
21157 struct dwarf2_per_objfile *dwarf2_per_objfile
21158 = cu->per_cu->dwarf2_per_objfile;
21159 struct objfile *objfile = dwarf2_per_objfile->objfile;
21160 struct type *this_type;
21161
21162 gdb_assert (attr->name == DW_AT_type
21163 || attr->name == DW_AT_GNAT_descriptive_type
21164 || attr->name == DW_AT_containing_type);
21165
21166 /* First see if we have it cached. */
21167
21168 if (attr->form == DW_FORM_GNU_ref_alt)
21169 {
21170 struct dwarf2_per_cu_data *per_cu;
21171 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21172
21173 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21174 dwarf2_per_objfile);
21175 this_type = get_die_type_at_offset (sect_off, per_cu);
21176 }
21177 else if (attr->form_is_ref ())
21178 {
21179 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21180
21181 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21182 }
21183 else if (attr->form == DW_FORM_ref_sig8)
21184 {
21185 ULONGEST signature = DW_SIGNATURE (attr);
21186
21187 return get_signatured_type (die, signature, cu);
21188 }
21189 else
21190 {
21191 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21192 " at %s [in module %s]"),
21193 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21194 objfile_name (objfile));
21195 return build_error_marker_type (cu, die);
21196 }
21197
21198 /* If not cached we need to read it in. */
21199
21200 if (this_type == NULL)
21201 {
21202 struct die_info *type_die = NULL;
21203 struct dwarf2_cu *type_cu = cu;
21204
21205 if (attr->form_is_ref ())
21206 type_die = follow_die_ref (die, attr, &type_cu);
21207 if (type_die == NULL)
21208 return build_error_marker_type (cu, die);
21209 /* If we find the type now, it's probably because the type came
21210 from an inter-CU reference and the type's CU got expanded before
21211 ours. */
21212 this_type = read_type_die (type_die, type_cu);
21213 }
21214
21215 /* If we still don't have a type use an error marker. */
21216
21217 if (this_type == NULL)
21218 return build_error_marker_type (cu, die);
21219
21220 return this_type;
21221 }
21222
21223 /* Return the type in DIE, CU.
21224 Returns NULL for invalid types.
21225
21226 This first does a lookup in die_type_hash,
21227 and only reads the die in if necessary.
21228
21229 NOTE: This can be called when reading in partial or full symbols. */
21230
21231 static struct type *
21232 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21233 {
21234 struct type *this_type;
21235
21236 this_type = get_die_type (die, cu);
21237 if (this_type)
21238 return this_type;
21239
21240 return read_type_die_1 (die, cu);
21241 }
21242
21243 /* Read the type in DIE, CU.
21244 Returns NULL for invalid types. */
21245
21246 static struct type *
21247 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21248 {
21249 struct type *this_type = NULL;
21250
21251 switch (die->tag)
21252 {
21253 case DW_TAG_class_type:
21254 case DW_TAG_interface_type:
21255 case DW_TAG_structure_type:
21256 case DW_TAG_union_type:
21257 this_type = read_structure_type (die, cu);
21258 break;
21259 case DW_TAG_enumeration_type:
21260 this_type = read_enumeration_type (die, cu);
21261 break;
21262 case DW_TAG_subprogram:
21263 case DW_TAG_subroutine_type:
21264 case DW_TAG_inlined_subroutine:
21265 this_type = read_subroutine_type (die, cu);
21266 break;
21267 case DW_TAG_array_type:
21268 this_type = read_array_type (die, cu);
21269 break;
21270 case DW_TAG_set_type:
21271 this_type = read_set_type (die, cu);
21272 break;
21273 case DW_TAG_pointer_type:
21274 this_type = read_tag_pointer_type (die, cu);
21275 break;
21276 case DW_TAG_ptr_to_member_type:
21277 this_type = read_tag_ptr_to_member_type (die, cu);
21278 break;
21279 case DW_TAG_reference_type:
21280 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21281 break;
21282 case DW_TAG_rvalue_reference_type:
21283 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21284 break;
21285 case DW_TAG_const_type:
21286 this_type = read_tag_const_type (die, cu);
21287 break;
21288 case DW_TAG_volatile_type:
21289 this_type = read_tag_volatile_type (die, cu);
21290 break;
21291 case DW_TAG_restrict_type:
21292 this_type = read_tag_restrict_type (die, cu);
21293 break;
21294 case DW_TAG_string_type:
21295 this_type = read_tag_string_type (die, cu);
21296 break;
21297 case DW_TAG_typedef:
21298 this_type = read_typedef (die, cu);
21299 break;
21300 case DW_TAG_subrange_type:
21301 this_type = read_subrange_type (die, cu);
21302 break;
21303 case DW_TAG_base_type:
21304 this_type = read_base_type (die, cu);
21305 break;
21306 case DW_TAG_unspecified_type:
21307 this_type = read_unspecified_type (die, cu);
21308 break;
21309 case DW_TAG_namespace:
21310 this_type = read_namespace_type (die, cu);
21311 break;
21312 case DW_TAG_module:
21313 this_type = read_module_type (die, cu);
21314 break;
21315 case DW_TAG_atomic_type:
21316 this_type = read_tag_atomic_type (die, cu);
21317 break;
21318 default:
21319 complaint (_("unexpected tag in read_type_die: '%s'"),
21320 dwarf_tag_name (die->tag));
21321 break;
21322 }
21323
21324 return this_type;
21325 }
21326
21327 /* See if we can figure out if the class lives in a namespace. We do
21328 this by looking for a member function; its demangled name will
21329 contain namespace info, if there is any.
21330 Return the computed name or NULL.
21331 Space for the result is allocated on the objfile's obstack.
21332 This is the full-die version of guess_partial_die_structure_name.
21333 In this case we know DIE has no useful parent. */
21334
21335 static const char *
21336 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21337 {
21338 struct die_info *spec_die;
21339 struct dwarf2_cu *spec_cu;
21340 struct die_info *child;
21341 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21342
21343 spec_cu = cu;
21344 spec_die = die_specification (die, &spec_cu);
21345 if (spec_die != NULL)
21346 {
21347 die = spec_die;
21348 cu = spec_cu;
21349 }
21350
21351 for (child = die->child;
21352 child != NULL;
21353 child = child->sibling)
21354 {
21355 if (child->tag == DW_TAG_subprogram)
21356 {
21357 const char *linkage_name = dw2_linkage_name (child, cu);
21358
21359 if (linkage_name != NULL)
21360 {
21361 gdb::unique_xmalloc_ptr<char> actual_name
21362 (language_class_name_from_physname (cu->language_defn,
21363 linkage_name));
21364 const char *name = NULL;
21365
21366 if (actual_name != NULL)
21367 {
21368 const char *die_name = dwarf2_name (die, cu);
21369
21370 if (die_name != NULL
21371 && strcmp (die_name, actual_name.get ()) != 0)
21372 {
21373 /* Strip off the class name from the full name.
21374 We want the prefix. */
21375 int die_name_len = strlen (die_name);
21376 int actual_name_len = strlen (actual_name.get ());
21377 const char *ptr = actual_name.get ();
21378
21379 /* Test for '::' as a sanity check. */
21380 if (actual_name_len > die_name_len + 2
21381 && ptr[actual_name_len - die_name_len - 1] == ':')
21382 name = obstack_strndup (
21383 &objfile->per_bfd->storage_obstack,
21384 ptr, actual_name_len - die_name_len - 2);
21385 }
21386 }
21387 return name;
21388 }
21389 }
21390 }
21391
21392 return NULL;
21393 }
21394
21395 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21396 prefix part in such case. See
21397 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21398
21399 static const char *
21400 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21401 {
21402 struct attribute *attr;
21403 const char *base;
21404
21405 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21406 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21407 return NULL;
21408
21409 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21410 return NULL;
21411
21412 attr = dw2_linkage_name_attr (die, cu);
21413 if (attr == NULL || DW_STRING (attr) == NULL)
21414 return NULL;
21415
21416 /* dwarf2_name had to be already called. */
21417 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21418
21419 /* Strip the base name, keep any leading namespaces/classes. */
21420 base = strrchr (DW_STRING (attr), ':');
21421 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21422 return "";
21423
21424 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21425 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21426 DW_STRING (attr),
21427 &base[-1] - DW_STRING (attr));
21428 }
21429
21430 /* Return the name of the namespace/class that DIE is defined within,
21431 or "" if we can't tell. The caller should not xfree the result.
21432
21433 For example, if we're within the method foo() in the following
21434 code:
21435
21436 namespace N {
21437 class C {
21438 void foo () {
21439 }
21440 };
21441 }
21442
21443 then determine_prefix on foo's die will return "N::C". */
21444
21445 static const char *
21446 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21447 {
21448 struct dwarf2_per_objfile *dwarf2_per_objfile
21449 = cu->per_cu->dwarf2_per_objfile;
21450 struct die_info *parent, *spec_die;
21451 struct dwarf2_cu *spec_cu;
21452 struct type *parent_type;
21453 const char *retval;
21454
21455 if (cu->language != language_cplus
21456 && cu->language != language_fortran && cu->language != language_d
21457 && cu->language != language_rust)
21458 return "";
21459
21460 retval = anonymous_struct_prefix (die, cu);
21461 if (retval)
21462 return retval;
21463
21464 /* We have to be careful in the presence of DW_AT_specification.
21465 For example, with GCC 3.4, given the code
21466
21467 namespace N {
21468 void foo() {
21469 // Definition of N::foo.
21470 }
21471 }
21472
21473 then we'll have a tree of DIEs like this:
21474
21475 1: DW_TAG_compile_unit
21476 2: DW_TAG_namespace // N
21477 3: DW_TAG_subprogram // declaration of N::foo
21478 4: DW_TAG_subprogram // definition of N::foo
21479 DW_AT_specification // refers to die #3
21480
21481 Thus, when processing die #4, we have to pretend that we're in
21482 the context of its DW_AT_specification, namely the contex of die
21483 #3. */
21484 spec_cu = cu;
21485 spec_die = die_specification (die, &spec_cu);
21486 if (spec_die == NULL)
21487 parent = die->parent;
21488 else
21489 {
21490 parent = spec_die->parent;
21491 cu = spec_cu;
21492 }
21493
21494 if (parent == NULL)
21495 return "";
21496 else if (parent->building_fullname)
21497 {
21498 const char *name;
21499 const char *parent_name;
21500
21501 /* It has been seen on RealView 2.2 built binaries,
21502 DW_TAG_template_type_param types actually _defined_ as
21503 children of the parent class:
21504
21505 enum E {};
21506 template class <class Enum> Class{};
21507 Class<enum E> class_e;
21508
21509 1: DW_TAG_class_type (Class)
21510 2: DW_TAG_enumeration_type (E)
21511 3: DW_TAG_enumerator (enum1:0)
21512 3: DW_TAG_enumerator (enum2:1)
21513 ...
21514 2: DW_TAG_template_type_param
21515 DW_AT_type DW_FORM_ref_udata (E)
21516
21517 Besides being broken debug info, it can put GDB into an
21518 infinite loop. Consider:
21519
21520 When we're building the full name for Class<E>, we'll start
21521 at Class, and go look over its template type parameters,
21522 finding E. We'll then try to build the full name of E, and
21523 reach here. We're now trying to build the full name of E,
21524 and look over the parent DIE for containing scope. In the
21525 broken case, if we followed the parent DIE of E, we'd again
21526 find Class, and once again go look at its template type
21527 arguments, etc., etc. Simply don't consider such parent die
21528 as source-level parent of this die (it can't be, the language
21529 doesn't allow it), and break the loop here. */
21530 name = dwarf2_name (die, cu);
21531 parent_name = dwarf2_name (parent, cu);
21532 complaint (_("template param type '%s' defined within parent '%s'"),
21533 name ? name : "<unknown>",
21534 parent_name ? parent_name : "<unknown>");
21535 return "";
21536 }
21537 else
21538 switch (parent->tag)
21539 {
21540 case DW_TAG_namespace:
21541 parent_type = read_type_die (parent, cu);
21542 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21543 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21544 Work around this problem here. */
21545 if (cu->language == language_cplus
21546 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21547 return "";
21548 /* We give a name to even anonymous namespaces. */
21549 return TYPE_NAME (parent_type);
21550 case DW_TAG_class_type:
21551 case DW_TAG_interface_type:
21552 case DW_TAG_structure_type:
21553 case DW_TAG_union_type:
21554 case DW_TAG_module:
21555 parent_type = read_type_die (parent, cu);
21556 if (TYPE_NAME (parent_type) != NULL)
21557 return TYPE_NAME (parent_type);
21558 else
21559 /* An anonymous structure is only allowed non-static data
21560 members; no typedefs, no member functions, et cetera.
21561 So it does not need a prefix. */
21562 return "";
21563 case DW_TAG_compile_unit:
21564 case DW_TAG_partial_unit:
21565 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21566 if (cu->language == language_cplus
21567 && !dwarf2_per_objfile->types.empty ()
21568 && die->child != NULL
21569 && (die->tag == DW_TAG_class_type
21570 || die->tag == DW_TAG_structure_type
21571 || die->tag == DW_TAG_union_type))
21572 {
21573 const char *name = guess_full_die_structure_name (die, cu);
21574 if (name != NULL)
21575 return name;
21576 }
21577 return "";
21578 case DW_TAG_subprogram:
21579 /* Nested subroutines in Fortran get a prefix with the name
21580 of the parent's subroutine. */
21581 if (cu->language == language_fortran)
21582 {
21583 if ((die->tag == DW_TAG_subprogram)
21584 && (dwarf2_name (parent, cu) != NULL))
21585 return dwarf2_name (parent, cu);
21586 }
21587 return determine_prefix (parent, cu);
21588 case DW_TAG_enumeration_type:
21589 parent_type = read_type_die (parent, cu);
21590 if (TYPE_DECLARED_CLASS (parent_type))
21591 {
21592 if (TYPE_NAME (parent_type) != NULL)
21593 return TYPE_NAME (parent_type);
21594 return "";
21595 }
21596 /* Fall through. */
21597 default:
21598 return determine_prefix (parent, cu);
21599 }
21600 }
21601
21602 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21603 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21604 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21605 an obconcat, otherwise allocate storage for the result. The CU argument is
21606 used to determine the language and hence, the appropriate separator. */
21607
21608 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21609
21610 static char *
21611 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21612 int physname, struct dwarf2_cu *cu)
21613 {
21614 const char *lead = "";
21615 const char *sep;
21616
21617 if (suffix == NULL || suffix[0] == '\0'
21618 || prefix == NULL || prefix[0] == '\0')
21619 sep = "";
21620 else if (cu->language == language_d)
21621 {
21622 /* For D, the 'main' function could be defined in any module, but it
21623 should never be prefixed. */
21624 if (strcmp (suffix, "D main") == 0)
21625 {
21626 prefix = "";
21627 sep = "";
21628 }
21629 else
21630 sep = ".";
21631 }
21632 else if (cu->language == language_fortran && physname)
21633 {
21634 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21635 DW_AT_MIPS_linkage_name is preferred and used instead. */
21636
21637 lead = "__";
21638 sep = "_MOD_";
21639 }
21640 else
21641 sep = "::";
21642
21643 if (prefix == NULL)
21644 prefix = "";
21645 if (suffix == NULL)
21646 suffix = "";
21647
21648 if (obs == NULL)
21649 {
21650 char *retval
21651 = ((char *)
21652 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21653
21654 strcpy (retval, lead);
21655 strcat (retval, prefix);
21656 strcat (retval, sep);
21657 strcat (retval, suffix);
21658 return retval;
21659 }
21660 else
21661 {
21662 /* We have an obstack. */
21663 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21664 }
21665 }
21666
21667 /* Return sibling of die, NULL if no sibling. */
21668
21669 static struct die_info *
21670 sibling_die (struct die_info *die)
21671 {
21672 return die->sibling;
21673 }
21674
21675 /* Get name of a die, return NULL if not found. */
21676
21677 static const char *
21678 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21679 struct obstack *obstack)
21680 {
21681 if (name && cu->language == language_cplus)
21682 {
21683 std::string canon_name = cp_canonicalize_string (name);
21684
21685 if (!canon_name.empty ())
21686 {
21687 if (canon_name != name)
21688 name = obstack_strdup (obstack, canon_name);
21689 }
21690 }
21691
21692 return name;
21693 }
21694
21695 /* Get name of a die, return NULL if not found.
21696 Anonymous namespaces are converted to their magic string. */
21697
21698 static const char *
21699 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21700 {
21701 struct attribute *attr;
21702 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21703
21704 attr = dwarf2_attr (die, DW_AT_name, cu);
21705 if ((!attr || !DW_STRING (attr))
21706 && die->tag != DW_TAG_namespace
21707 && die->tag != DW_TAG_class_type
21708 && die->tag != DW_TAG_interface_type
21709 && die->tag != DW_TAG_structure_type
21710 && die->tag != DW_TAG_union_type)
21711 return NULL;
21712
21713 switch (die->tag)
21714 {
21715 case DW_TAG_compile_unit:
21716 case DW_TAG_partial_unit:
21717 /* Compilation units have a DW_AT_name that is a filename, not
21718 a source language identifier. */
21719 case DW_TAG_enumeration_type:
21720 case DW_TAG_enumerator:
21721 /* These tags always have simple identifiers already; no need
21722 to canonicalize them. */
21723 return DW_STRING (attr);
21724
21725 case DW_TAG_namespace:
21726 if (attr != NULL && DW_STRING (attr) != NULL)
21727 return DW_STRING (attr);
21728 return CP_ANONYMOUS_NAMESPACE_STR;
21729
21730 case DW_TAG_class_type:
21731 case DW_TAG_interface_type:
21732 case DW_TAG_structure_type:
21733 case DW_TAG_union_type:
21734 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21735 structures or unions. These were of the form "._%d" in GCC 4.1,
21736 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21737 and GCC 4.4. We work around this problem by ignoring these. */
21738 if (attr && DW_STRING (attr)
21739 && (startswith (DW_STRING (attr), "._")
21740 || startswith (DW_STRING (attr), "<anonymous")))
21741 return NULL;
21742
21743 /* GCC might emit a nameless typedef that has a linkage name. See
21744 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21745 if (!attr || DW_STRING (attr) == NULL)
21746 {
21747 attr = dw2_linkage_name_attr (die, cu);
21748 if (attr == NULL || DW_STRING (attr) == NULL)
21749 return NULL;
21750
21751 /* Avoid demangling DW_STRING (attr) the second time on a second
21752 call for the same DIE. */
21753 if (!DW_STRING_IS_CANONICAL (attr))
21754 {
21755 gdb::unique_xmalloc_ptr<char> demangled
21756 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21757 if (demangled == nullptr)
21758 return nullptr;
21759
21760 const char *base;
21761
21762 /* FIXME: we already did this for the partial symbol... */
21763 DW_STRING (attr)
21764 = obstack_strdup (&objfile->per_bfd->storage_obstack,
21765 demangled.get ());
21766 DW_STRING_IS_CANONICAL (attr) = 1;
21767
21768 /* Strip any leading namespaces/classes, keep only the base name.
21769 DW_AT_name for named DIEs does not contain the prefixes. */
21770 base = strrchr (DW_STRING (attr), ':');
21771 if (base && base > DW_STRING (attr) && base[-1] == ':')
21772 return &base[1];
21773 else
21774 return DW_STRING (attr);
21775 }
21776 }
21777 break;
21778
21779 default:
21780 break;
21781 }
21782
21783 if (!DW_STRING_IS_CANONICAL (attr))
21784 {
21785 DW_STRING (attr)
21786 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21787 &objfile->per_bfd->storage_obstack);
21788 DW_STRING_IS_CANONICAL (attr) = 1;
21789 }
21790 return DW_STRING (attr);
21791 }
21792
21793 /* Return the die that this die in an extension of, or NULL if there
21794 is none. *EXT_CU is the CU containing DIE on input, and the CU
21795 containing the return value on output. */
21796
21797 static struct die_info *
21798 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21799 {
21800 struct attribute *attr;
21801
21802 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21803 if (attr == NULL)
21804 return NULL;
21805
21806 return follow_die_ref (die, attr, ext_cu);
21807 }
21808
21809 /* A convenience function that returns an "unknown" DWARF name,
21810 including the value of V. STR is the name of the entity being
21811 printed, e.g., "TAG". */
21812
21813 static const char *
21814 dwarf_unknown (const char *str, unsigned v)
21815 {
21816 char *cell = get_print_cell ();
21817 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21818 return cell;
21819 }
21820
21821 /* Convert a DIE tag into its string name. */
21822
21823 static const char *
21824 dwarf_tag_name (unsigned tag)
21825 {
21826 const char *name = get_DW_TAG_name (tag);
21827
21828 if (name == NULL)
21829 return dwarf_unknown ("TAG", tag);
21830
21831 return name;
21832 }
21833
21834 /* Convert a DWARF attribute code into its string name. */
21835
21836 static const char *
21837 dwarf_attr_name (unsigned attr)
21838 {
21839 const char *name;
21840
21841 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21842 if (attr == DW_AT_MIPS_fde)
21843 return "DW_AT_MIPS_fde";
21844 #else
21845 if (attr == DW_AT_HP_block_index)
21846 return "DW_AT_HP_block_index";
21847 #endif
21848
21849 name = get_DW_AT_name (attr);
21850
21851 if (name == NULL)
21852 return dwarf_unknown ("AT", attr);
21853
21854 return name;
21855 }
21856
21857 /* Convert a DWARF value form code into its string name. */
21858
21859 static const char *
21860 dwarf_form_name (unsigned form)
21861 {
21862 const char *name = get_DW_FORM_name (form);
21863
21864 if (name == NULL)
21865 return dwarf_unknown ("FORM", form);
21866
21867 return name;
21868 }
21869
21870 static const char *
21871 dwarf_bool_name (unsigned mybool)
21872 {
21873 if (mybool)
21874 return "TRUE";
21875 else
21876 return "FALSE";
21877 }
21878
21879 /* Convert a DWARF type code into its string name. */
21880
21881 static const char *
21882 dwarf_type_encoding_name (unsigned enc)
21883 {
21884 const char *name = get_DW_ATE_name (enc);
21885
21886 if (name == NULL)
21887 return dwarf_unknown ("ATE", enc);
21888
21889 return name;
21890 }
21891
21892 static void
21893 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21894 {
21895 unsigned int i;
21896
21897 print_spaces (indent, f);
21898 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21899 dwarf_tag_name (die->tag), die->abbrev,
21900 sect_offset_str (die->sect_off));
21901
21902 if (die->parent != NULL)
21903 {
21904 print_spaces (indent, f);
21905 fprintf_unfiltered (f, " parent at offset: %s\n",
21906 sect_offset_str (die->parent->sect_off));
21907 }
21908
21909 print_spaces (indent, f);
21910 fprintf_unfiltered (f, " has children: %s\n",
21911 dwarf_bool_name (die->child != NULL));
21912
21913 print_spaces (indent, f);
21914 fprintf_unfiltered (f, " attributes:\n");
21915
21916 for (i = 0; i < die->num_attrs; ++i)
21917 {
21918 print_spaces (indent, f);
21919 fprintf_unfiltered (f, " %s (%s) ",
21920 dwarf_attr_name (die->attrs[i].name),
21921 dwarf_form_name (die->attrs[i].form));
21922
21923 switch (die->attrs[i].form)
21924 {
21925 case DW_FORM_addr:
21926 case DW_FORM_addrx:
21927 case DW_FORM_GNU_addr_index:
21928 fprintf_unfiltered (f, "address: ");
21929 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21930 break;
21931 case DW_FORM_block2:
21932 case DW_FORM_block4:
21933 case DW_FORM_block:
21934 case DW_FORM_block1:
21935 fprintf_unfiltered (f, "block: size %s",
21936 pulongest (DW_BLOCK (&die->attrs[i])->size));
21937 break;
21938 case DW_FORM_exprloc:
21939 fprintf_unfiltered (f, "expression: size %s",
21940 pulongest (DW_BLOCK (&die->attrs[i])->size));
21941 break;
21942 case DW_FORM_data16:
21943 fprintf_unfiltered (f, "constant of 16 bytes");
21944 break;
21945 case DW_FORM_ref_addr:
21946 fprintf_unfiltered (f, "ref address: ");
21947 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21948 break;
21949 case DW_FORM_GNU_ref_alt:
21950 fprintf_unfiltered (f, "alt ref address: ");
21951 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21952 break;
21953 case DW_FORM_ref1:
21954 case DW_FORM_ref2:
21955 case DW_FORM_ref4:
21956 case DW_FORM_ref8:
21957 case DW_FORM_ref_udata:
21958 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21959 (long) (DW_UNSND (&die->attrs[i])));
21960 break;
21961 case DW_FORM_data1:
21962 case DW_FORM_data2:
21963 case DW_FORM_data4:
21964 case DW_FORM_data8:
21965 case DW_FORM_udata:
21966 case DW_FORM_sdata:
21967 fprintf_unfiltered (f, "constant: %s",
21968 pulongest (DW_UNSND (&die->attrs[i])));
21969 break;
21970 case DW_FORM_sec_offset:
21971 fprintf_unfiltered (f, "section offset: %s",
21972 pulongest (DW_UNSND (&die->attrs[i])));
21973 break;
21974 case DW_FORM_ref_sig8:
21975 fprintf_unfiltered (f, "signature: %s",
21976 hex_string (DW_SIGNATURE (&die->attrs[i])));
21977 break;
21978 case DW_FORM_string:
21979 case DW_FORM_strp:
21980 case DW_FORM_line_strp:
21981 case DW_FORM_strx:
21982 case DW_FORM_GNU_str_index:
21983 case DW_FORM_GNU_strp_alt:
21984 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21985 DW_STRING (&die->attrs[i])
21986 ? DW_STRING (&die->attrs[i]) : "",
21987 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21988 break;
21989 case DW_FORM_flag:
21990 if (DW_UNSND (&die->attrs[i]))
21991 fprintf_unfiltered (f, "flag: TRUE");
21992 else
21993 fprintf_unfiltered (f, "flag: FALSE");
21994 break;
21995 case DW_FORM_flag_present:
21996 fprintf_unfiltered (f, "flag: TRUE");
21997 break;
21998 case DW_FORM_indirect:
21999 /* The reader will have reduced the indirect form to
22000 the "base form" so this form should not occur. */
22001 fprintf_unfiltered (f,
22002 "unexpected attribute form: DW_FORM_indirect");
22003 break;
22004 case DW_FORM_implicit_const:
22005 fprintf_unfiltered (f, "constant: %s",
22006 plongest (DW_SND (&die->attrs[i])));
22007 break;
22008 default:
22009 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22010 die->attrs[i].form);
22011 break;
22012 }
22013 fprintf_unfiltered (f, "\n");
22014 }
22015 }
22016
22017 static void
22018 dump_die_for_error (struct die_info *die)
22019 {
22020 dump_die_shallow (gdb_stderr, 0, die);
22021 }
22022
22023 static void
22024 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22025 {
22026 int indent = level * 4;
22027
22028 gdb_assert (die != NULL);
22029
22030 if (level >= max_level)
22031 return;
22032
22033 dump_die_shallow (f, indent, die);
22034
22035 if (die->child != NULL)
22036 {
22037 print_spaces (indent, f);
22038 fprintf_unfiltered (f, " Children:");
22039 if (level + 1 < max_level)
22040 {
22041 fprintf_unfiltered (f, "\n");
22042 dump_die_1 (f, level + 1, max_level, die->child);
22043 }
22044 else
22045 {
22046 fprintf_unfiltered (f,
22047 " [not printed, max nesting level reached]\n");
22048 }
22049 }
22050
22051 if (die->sibling != NULL && level > 0)
22052 {
22053 dump_die_1 (f, level, max_level, die->sibling);
22054 }
22055 }
22056
22057 /* This is called from the pdie macro in gdbinit.in.
22058 It's not static so gcc will keep a copy callable from gdb. */
22059
22060 void
22061 dump_die (struct die_info *die, int max_level)
22062 {
22063 dump_die_1 (gdb_stdlog, 0, max_level, die);
22064 }
22065
22066 static void
22067 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22068 {
22069 void **slot;
22070
22071 slot = htab_find_slot_with_hash (cu->die_hash, die,
22072 to_underlying (die->sect_off),
22073 INSERT);
22074
22075 *slot = die;
22076 }
22077
22078 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22079 required kind. */
22080
22081 static sect_offset
22082 dwarf2_get_ref_die_offset (const struct attribute *attr)
22083 {
22084 if (attr->form_is_ref ())
22085 return (sect_offset) DW_UNSND (attr);
22086
22087 complaint (_("unsupported die ref attribute form: '%s'"),
22088 dwarf_form_name (attr->form));
22089 return {};
22090 }
22091
22092 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22093 * the value held by the attribute is not constant. */
22094
22095 static LONGEST
22096 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
22097 {
22098 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
22099 return DW_SND (attr);
22100 else if (attr->form == DW_FORM_udata
22101 || attr->form == DW_FORM_data1
22102 || attr->form == DW_FORM_data2
22103 || attr->form == DW_FORM_data4
22104 || attr->form == DW_FORM_data8)
22105 return DW_UNSND (attr);
22106 else
22107 {
22108 /* For DW_FORM_data16 see attribute::form_is_constant. */
22109 complaint (_("Attribute value is not a constant (%s)"),
22110 dwarf_form_name (attr->form));
22111 return default_value;
22112 }
22113 }
22114
22115 /* Follow reference or signature attribute ATTR of SRC_DIE.
22116 On entry *REF_CU is the CU of SRC_DIE.
22117 On exit *REF_CU is the CU of the result. */
22118
22119 static struct die_info *
22120 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22121 struct dwarf2_cu **ref_cu)
22122 {
22123 struct die_info *die;
22124
22125 if (attr->form_is_ref ())
22126 die = follow_die_ref (src_die, attr, ref_cu);
22127 else if (attr->form == DW_FORM_ref_sig8)
22128 die = follow_die_sig (src_die, attr, ref_cu);
22129 else
22130 {
22131 dump_die_for_error (src_die);
22132 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22133 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22134 }
22135
22136 return die;
22137 }
22138
22139 /* Follow reference OFFSET.
22140 On entry *REF_CU is the CU of the source die referencing OFFSET.
22141 On exit *REF_CU is the CU of the result.
22142 Returns NULL if OFFSET is invalid. */
22143
22144 static struct die_info *
22145 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22146 struct dwarf2_cu **ref_cu)
22147 {
22148 struct die_info temp_die;
22149 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22150 struct dwarf2_per_objfile *dwarf2_per_objfile
22151 = cu->per_cu->dwarf2_per_objfile;
22152
22153 gdb_assert (cu->per_cu != NULL);
22154
22155 target_cu = cu;
22156
22157 if (cu->per_cu->is_debug_types)
22158 {
22159 /* .debug_types CUs cannot reference anything outside their CU.
22160 If they need to, they have to reference a signatured type via
22161 DW_FORM_ref_sig8. */
22162 if (!cu->header.offset_in_cu_p (sect_off))
22163 return NULL;
22164 }
22165 else if (offset_in_dwz != cu->per_cu->is_dwz
22166 || !cu->header.offset_in_cu_p (sect_off))
22167 {
22168 struct dwarf2_per_cu_data *per_cu;
22169
22170 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22171 dwarf2_per_objfile);
22172
22173 /* If necessary, add it to the queue and load its DIEs. */
22174 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22175 load_full_comp_unit (per_cu, false, cu->language);
22176
22177 target_cu = per_cu->cu;
22178 }
22179 else if (cu->dies == NULL)
22180 {
22181 /* We're loading full DIEs during partial symbol reading. */
22182 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
22183 load_full_comp_unit (cu->per_cu, false, language_minimal);
22184 }
22185
22186 *ref_cu = target_cu;
22187 temp_die.sect_off = sect_off;
22188
22189 if (target_cu != cu)
22190 target_cu->ancestor = cu;
22191
22192 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22193 &temp_die,
22194 to_underlying (sect_off));
22195 }
22196
22197 /* Follow reference attribute ATTR of SRC_DIE.
22198 On entry *REF_CU is the CU of SRC_DIE.
22199 On exit *REF_CU is the CU of the result. */
22200
22201 static struct die_info *
22202 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22203 struct dwarf2_cu **ref_cu)
22204 {
22205 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22206 struct dwarf2_cu *cu = *ref_cu;
22207 struct die_info *die;
22208
22209 die = follow_die_offset (sect_off,
22210 (attr->form == DW_FORM_GNU_ref_alt
22211 || cu->per_cu->is_dwz),
22212 ref_cu);
22213 if (!die)
22214 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22215 "at %s [in module %s]"),
22216 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22217 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
22218
22219 return die;
22220 }
22221
22222 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
22223 Returned value is intended for DW_OP_call*. Returned
22224 dwarf2_locexpr_baton->data has lifetime of
22225 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
22226
22227 struct dwarf2_locexpr_baton
22228 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22229 struct dwarf2_per_cu_data *per_cu,
22230 CORE_ADDR (*get_frame_pc) (void *baton),
22231 void *baton, bool resolve_abstract_p)
22232 {
22233 struct dwarf2_cu *cu;
22234 struct die_info *die;
22235 struct attribute *attr;
22236 struct dwarf2_locexpr_baton retval;
22237 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22238 struct objfile *objfile = dwarf2_per_objfile->objfile;
22239
22240 if (per_cu->cu == NULL)
22241 load_cu (per_cu, false);
22242 cu = per_cu->cu;
22243 if (cu == NULL)
22244 {
22245 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22246 Instead just throw an error, not much else we can do. */
22247 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22248 sect_offset_str (sect_off), objfile_name (objfile));
22249 }
22250
22251 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22252 if (!die)
22253 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22254 sect_offset_str (sect_off), objfile_name (objfile));
22255
22256 attr = dwarf2_attr (die, DW_AT_location, cu);
22257 if (!attr && resolve_abstract_p
22258 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
22259 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22260 {
22261 CORE_ADDR pc = (*get_frame_pc) (baton);
22262 CORE_ADDR baseaddr = objfile->text_section_offset ();
22263 struct gdbarch *gdbarch = get_objfile_arch (objfile);
22264
22265 for (const auto &cand_off
22266 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
22267 {
22268 struct dwarf2_cu *cand_cu = cu;
22269 struct die_info *cand
22270 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22271 if (!cand
22272 || !cand->parent
22273 || cand->parent->tag != DW_TAG_subprogram)
22274 continue;
22275
22276 CORE_ADDR pc_low, pc_high;
22277 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22278 if (pc_low == ((CORE_ADDR) -1))
22279 continue;
22280 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22281 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22282 if (!(pc_low <= pc && pc < pc_high))
22283 continue;
22284
22285 die = cand;
22286 attr = dwarf2_attr (die, DW_AT_location, cu);
22287 break;
22288 }
22289 }
22290
22291 if (!attr)
22292 {
22293 /* DWARF: "If there is no such attribute, then there is no effect.".
22294 DATA is ignored if SIZE is 0. */
22295
22296 retval.data = NULL;
22297 retval.size = 0;
22298 }
22299 else if (attr->form_is_section_offset ())
22300 {
22301 struct dwarf2_loclist_baton loclist_baton;
22302 CORE_ADDR pc = (*get_frame_pc) (baton);
22303 size_t size;
22304
22305 fill_in_loclist_baton (cu, &loclist_baton, attr);
22306
22307 retval.data = dwarf2_find_location_expression (&loclist_baton,
22308 &size, pc);
22309 retval.size = size;
22310 }
22311 else
22312 {
22313 if (!attr->form_is_block ())
22314 error (_("Dwarf Error: DIE at %s referenced in module %s "
22315 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22316 sect_offset_str (sect_off), objfile_name (objfile));
22317
22318 retval.data = DW_BLOCK (attr)->data;
22319 retval.size = DW_BLOCK (attr)->size;
22320 }
22321 retval.per_cu = cu->per_cu;
22322
22323 age_cached_comp_units (dwarf2_per_objfile);
22324
22325 return retval;
22326 }
22327
22328 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22329 offset. */
22330
22331 struct dwarf2_locexpr_baton
22332 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22333 struct dwarf2_per_cu_data *per_cu,
22334 CORE_ADDR (*get_frame_pc) (void *baton),
22335 void *baton)
22336 {
22337 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22338
22339 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22340 }
22341
22342 /* Write a constant of a given type as target-ordered bytes into
22343 OBSTACK. */
22344
22345 static const gdb_byte *
22346 write_constant_as_bytes (struct obstack *obstack,
22347 enum bfd_endian byte_order,
22348 struct type *type,
22349 ULONGEST value,
22350 LONGEST *len)
22351 {
22352 gdb_byte *result;
22353
22354 *len = TYPE_LENGTH (type);
22355 result = (gdb_byte *) obstack_alloc (obstack, *len);
22356 store_unsigned_integer (result, *len, byte_order, value);
22357
22358 return result;
22359 }
22360
22361 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22362 pointer to the constant bytes and set LEN to the length of the
22363 data. If memory is needed, allocate it on OBSTACK. If the DIE
22364 does not have a DW_AT_const_value, return NULL. */
22365
22366 const gdb_byte *
22367 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22368 struct dwarf2_per_cu_data *per_cu,
22369 struct obstack *obstack,
22370 LONGEST *len)
22371 {
22372 struct dwarf2_cu *cu;
22373 struct die_info *die;
22374 struct attribute *attr;
22375 const gdb_byte *result = NULL;
22376 struct type *type;
22377 LONGEST value;
22378 enum bfd_endian byte_order;
22379 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22380
22381 if (per_cu->cu == NULL)
22382 load_cu (per_cu, false);
22383 cu = per_cu->cu;
22384 if (cu == NULL)
22385 {
22386 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22387 Instead just throw an error, not much else we can do. */
22388 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22389 sect_offset_str (sect_off), objfile_name (objfile));
22390 }
22391
22392 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22393 if (!die)
22394 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22395 sect_offset_str (sect_off), objfile_name (objfile));
22396
22397 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22398 if (attr == NULL)
22399 return NULL;
22400
22401 byte_order = (bfd_big_endian (objfile->obfd)
22402 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22403
22404 switch (attr->form)
22405 {
22406 case DW_FORM_addr:
22407 case DW_FORM_addrx:
22408 case DW_FORM_GNU_addr_index:
22409 {
22410 gdb_byte *tem;
22411
22412 *len = cu->header.addr_size;
22413 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22414 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22415 result = tem;
22416 }
22417 break;
22418 case DW_FORM_string:
22419 case DW_FORM_strp:
22420 case DW_FORM_strx:
22421 case DW_FORM_GNU_str_index:
22422 case DW_FORM_GNU_strp_alt:
22423 /* DW_STRING is already allocated on the objfile obstack, point
22424 directly to it. */
22425 result = (const gdb_byte *) DW_STRING (attr);
22426 *len = strlen (DW_STRING (attr));
22427 break;
22428 case DW_FORM_block1:
22429 case DW_FORM_block2:
22430 case DW_FORM_block4:
22431 case DW_FORM_block:
22432 case DW_FORM_exprloc:
22433 case DW_FORM_data16:
22434 result = DW_BLOCK (attr)->data;
22435 *len = DW_BLOCK (attr)->size;
22436 break;
22437
22438 /* The DW_AT_const_value attributes are supposed to carry the
22439 symbol's value "represented as it would be on the target
22440 architecture." By the time we get here, it's already been
22441 converted to host endianness, so we just need to sign- or
22442 zero-extend it as appropriate. */
22443 case DW_FORM_data1:
22444 type = die_type (die, cu);
22445 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22446 if (result == NULL)
22447 result = write_constant_as_bytes (obstack, byte_order,
22448 type, value, len);
22449 break;
22450 case DW_FORM_data2:
22451 type = die_type (die, cu);
22452 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22453 if (result == NULL)
22454 result = write_constant_as_bytes (obstack, byte_order,
22455 type, value, len);
22456 break;
22457 case DW_FORM_data4:
22458 type = die_type (die, cu);
22459 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22460 if (result == NULL)
22461 result = write_constant_as_bytes (obstack, byte_order,
22462 type, value, len);
22463 break;
22464 case DW_FORM_data8:
22465 type = die_type (die, cu);
22466 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22467 if (result == NULL)
22468 result = write_constant_as_bytes (obstack, byte_order,
22469 type, value, len);
22470 break;
22471
22472 case DW_FORM_sdata:
22473 case DW_FORM_implicit_const:
22474 type = die_type (die, cu);
22475 result = write_constant_as_bytes (obstack, byte_order,
22476 type, DW_SND (attr), len);
22477 break;
22478
22479 case DW_FORM_udata:
22480 type = die_type (die, cu);
22481 result = write_constant_as_bytes (obstack, byte_order,
22482 type, DW_UNSND (attr), len);
22483 break;
22484
22485 default:
22486 complaint (_("unsupported const value attribute form: '%s'"),
22487 dwarf_form_name (attr->form));
22488 break;
22489 }
22490
22491 return result;
22492 }
22493
22494 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
22495 valid type for this die is found. */
22496
22497 struct type *
22498 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22499 struct dwarf2_per_cu_data *per_cu)
22500 {
22501 struct dwarf2_cu *cu;
22502 struct die_info *die;
22503
22504 if (per_cu->cu == NULL)
22505 load_cu (per_cu, false);
22506 cu = per_cu->cu;
22507 if (!cu)
22508 return NULL;
22509
22510 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22511 if (!die)
22512 return NULL;
22513
22514 return die_type (die, cu);
22515 }
22516
22517 /* Return the type of the DIE at DIE_OFFSET in the CU named by
22518 PER_CU. */
22519
22520 struct type *
22521 dwarf2_get_die_type (cu_offset die_offset,
22522 struct dwarf2_per_cu_data *per_cu)
22523 {
22524 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22525 return get_die_type_at_offset (die_offset_sect, per_cu);
22526 }
22527
22528 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22529 On entry *REF_CU is the CU of SRC_DIE.
22530 On exit *REF_CU is the CU of the result.
22531 Returns NULL if the referenced DIE isn't found. */
22532
22533 static struct die_info *
22534 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22535 struct dwarf2_cu **ref_cu)
22536 {
22537 struct die_info temp_die;
22538 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22539 struct die_info *die;
22540
22541 /* While it might be nice to assert sig_type->type == NULL here,
22542 we can get here for DW_AT_imported_declaration where we need
22543 the DIE not the type. */
22544
22545 /* If necessary, add it to the queue and load its DIEs. */
22546
22547 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22548 read_signatured_type (sig_type);
22549
22550 sig_cu = sig_type->per_cu.cu;
22551 gdb_assert (sig_cu != NULL);
22552 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22553 temp_die.sect_off = sig_type->type_offset_in_section;
22554 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22555 to_underlying (temp_die.sect_off));
22556 if (die)
22557 {
22558 struct dwarf2_per_objfile *dwarf2_per_objfile
22559 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22560
22561 /* For .gdb_index version 7 keep track of included TUs.
22562 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22563 if (dwarf2_per_objfile->index_table != NULL
22564 && dwarf2_per_objfile->index_table->version <= 7)
22565 {
22566 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22567 }
22568
22569 *ref_cu = sig_cu;
22570 if (sig_cu != cu)
22571 sig_cu->ancestor = cu;
22572
22573 return die;
22574 }
22575
22576 return NULL;
22577 }
22578
22579 /* Follow signatured type referenced by ATTR in SRC_DIE.
22580 On entry *REF_CU is the CU of SRC_DIE.
22581 On exit *REF_CU is the CU of the result.
22582 The result is the DIE of the type.
22583 If the referenced type cannot be found an error is thrown. */
22584
22585 static struct die_info *
22586 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22587 struct dwarf2_cu **ref_cu)
22588 {
22589 ULONGEST signature = DW_SIGNATURE (attr);
22590 struct signatured_type *sig_type;
22591 struct die_info *die;
22592
22593 gdb_assert (attr->form == DW_FORM_ref_sig8);
22594
22595 sig_type = lookup_signatured_type (*ref_cu, signature);
22596 /* sig_type will be NULL if the signatured type is missing from
22597 the debug info. */
22598 if (sig_type == NULL)
22599 {
22600 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22601 " from DIE at %s [in module %s]"),
22602 hex_string (signature), sect_offset_str (src_die->sect_off),
22603 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22604 }
22605
22606 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22607 if (die == NULL)
22608 {
22609 dump_die_for_error (src_die);
22610 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22611 " from DIE at %s [in module %s]"),
22612 hex_string (signature), sect_offset_str (src_die->sect_off),
22613 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22614 }
22615
22616 return die;
22617 }
22618
22619 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22620 reading in and processing the type unit if necessary. */
22621
22622 static struct type *
22623 get_signatured_type (struct die_info *die, ULONGEST signature,
22624 struct dwarf2_cu *cu)
22625 {
22626 struct dwarf2_per_objfile *dwarf2_per_objfile
22627 = cu->per_cu->dwarf2_per_objfile;
22628 struct signatured_type *sig_type;
22629 struct dwarf2_cu *type_cu;
22630 struct die_info *type_die;
22631 struct type *type;
22632
22633 sig_type = lookup_signatured_type (cu, signature);
22634 /* sig_type will be NULL if the signatured type is missing from
22635 the debug info. */
22636 if (sig_type == NULL)
22637 {
22638 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22639 " from DIE at %s [in module %s]"),
22640 hex_string (signature), sect_offset_str (die->sect_off),
22641 objfile_name (dwarf2_per_objfile->objfile));
22642 return build_error_marker_type (cu, die);
22643 }
22644
22645 /* If we already know the type we're done. */
22646 if (sig_type->type != NULL)
22647 return sig_type->type;
22648
22649 type_cu = cu;
22650 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22651 if (type_die != NULL)
22652 {
22653 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22654 is created. This is important, for example, because for c++ classes
22655 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22656 type = read_type_die (type_die, type_cu);
22657 if (type == NULL)
22658 {
22659 complaint (_("Dwarf Error: Cannot build signatured type %s"
22660 " referenced 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 type = build_error_marker_type (cu, die);
22664 }
22665 }
22666 else
22667 {
22668 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22669 " from DIE at %s [in module %s]"),
22670 hex_string (signature), sect_offset_str (die->sect_off),
22671 objfile_name (dwarf2_per_objfile->objfile));
22672 type = build_error_marker_type (cu, die);
22673 }
22674 sig_type->type = type;
22675
22676 return type;
22677 }
22678
22679 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22680 reading in and processing the type unit if necessary. */
22681
22682 static struct type *
22683 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22684 struct dwarf2_cu *cu) /* ARI: editCase function */
22685 {
22686 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22687 if (attr->form_is_ref ())
22688 {
22689 struct dwarf2_cu *type_cu = cu;
22690 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22691
22692 return read_type_die (type_die, type_cu);
22693 }
22694 else if (attr->form == DW_FORM_ref_sig8)
22695 {
22696 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22697 }
22698 else
22699 {
22700 struct dwarf2_per_objfile *dwarf2_per_objfile
22701 = cu->per_cu->dwarf2_per_objfile;
22702
22703 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22704 " at %s [in module %s]"),
22705 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22706 objfile_name (dwarf2_per_objfile->objfile));
22707 return build_error_marker_type (cu, die);
22708 }
22709 }
22710
22711 /* Load the DIEs associated with type unit PER_CU into memory. */
22712
22713 static void
22714 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22715 {
22716 struct signatured_type *sig_type;
22717
22718 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22719 gdb_assert (! per_cu->type_unit_group_p ());
22720
22721 /* We have the per_cu, but we need the signatured_type.
22722 Fortunately this is an easy translation. */
22723 gdb_assert (per_cu->is_debug_types);
22724 sig_type = (struct signatured_type *) per_cu;
22725
22726 gdb_assert (per_cu->cu == NULL);
22727
22728 read_signatured_type (sig_type);
22729
22730 gdb_assert (per_cu->cu != NULL);
22731 }
22732
22733 /* Read in a signatured type and build its CU and DIEs.
22734 If the type is a stub for the real type in a DWO file,
22735 read in the real type from the DWO file as well. */
22736
22737 static void
22738 read_signatured_type (struct signatured_type *sig_type)
22739 {
22740 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22741
22742 gdb_assert (per_cu->is_debug_types);
22743 gdb_assert (per_cu->cu == NULL);
22744
22745 cutu_reader reader (per_cu, NULL, 0, false);
22746
22747 if (!reader.dummy_p)
22748 {
22749 struct dwarf2_cu *cu = reader.cu;
22750 const gdb_byte *info_ptr = reader.info_ptr;
22751
22752 gdb_assert (cu->die_hash == NULL);
22753 cu->die_hash =
22754 htab_create_alloc_ex (cu->header.length / 12,
22755 die_hash,
22756 die_eq,
22757 NULL,
22758 &cu->comp_unit_obstack,
22759 hashtab_obstack_allocate,
22760 dummy_obstack_deallocate);
22761
22762 if (reader.comp_unit_die->has_children)
22763 reader.comp_unit_die->child
22764 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22765 reader.comp_unit_die);
22766 cu->dies = reader.comp_unit_die;
22767 /* comp_unit_die is not stored in die_hash, no need. */
22768
22769 /* We try not to read any attributes in this function, because
22770 not all CUs needed for references have been loaded yet, and
22771 symbol table processing isn't initialized. But we have to
22772 set the CU language, or we won't be able to build types
22773 correctly. Similarly, if we do not read the producer, we can
22774 not apply producer-specific interpretation. */
22775 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22776
22777 reader.keep ();
22778 }
22779
22780 sig_type->per_cu.tu_read = 1;
22781 }
22782
22783 /* Decode simple location descriptions.
22784 Given a pointer to a dwarf block that defines a location, compute
22785 the location and return the value.
22786
22787 NOTE drow/2003-11-18: This function is called in two situations
22788 now: for the address of static or global variables (partial symbols
22789 only) and for offsets into structures which are expected to be
22790 (more or less) constant. The partial symbol case should go away,
22791 and only the constant case should remain. That will let this
22792 function complain more accurately. A few special modes are allowed
22793 without complaint for global variables (for instance, global
22794 register values and thread-local values).
22795
22796 A location description containing no operations indicates that the
22797 object is optimized out. The return value is 0 for that case.
22798 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22799 callers will only want a very basic result and this can become a
22800 complaint.
22801
22802 Note that stack[0] is unused except as a default error return. */
22803
22804 static CORE_ADDR
22805 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22806 {
22807 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22808 size_t i;
22809 size_t size = blk->size;
22810 const gdb_byte *data = blk->data;
22811 CORE_ADDR stack[64];
22812 int stacki;
22813 unsigned int bytes_read, unsnd;
22814 gdb_byte op;
22815
22816 i = 0;
22817 stacki = 0;
22818 stack[stacki] = 0;
22819 stack[++stacki] = 0;
22820
22821 while (i < size)
22822 {
22823 op = data[i++];
22824 switch (op)
22825 {
22826 case DW_OP_lit0:
22827 case DW_OP_lit1:
22828 case DW_OP_lit2:
22829 case DW_OP_lit3:
22830 case DW_OP_lit4:
22831 case DW_OP_lit5:
22832 case DW_OP_lit6:
22833 case DW_OP_lit7:
22834 case DW_OP_lit8:
22835 case DW_OP_lit9:
22836 case DW_OP_lit10:
22837 case DW_OP_lit11:
22838 case DW_OP_lit12:
22839 case DW_OP_lit13:
22840 case DW_OP_lit14:
22841 case DW_OP_lit15:
22842 case DW_OP_lit16:
22843 case DW_OP_lit17:
22844 case DW_OP_lit18:
22845 case DW_OP_lit19:
22846 case DW_OP_lit20:
22847 case DW_OP_lit21:
22848 case DW_OP_lit22:
22849 case DW_OP_lit23:
22850 case DW_OP_lit24:
22851 case DW_OP_lit25:
22852 case DW_OP_lit26:
22853 case DW_OP_lit27:
22854 case DW_OP_lit28:
22855 case DW_OP_lit29:
22856 case DW_OP_lit30:
22857 case DW_OP_lit31:
22858 stack[++stacki] = op - DW_OP_lit0;
22859 break;
22860
22861 case DW_OP_reg0:
22862 case DW_OP_reg1:
22863 case DW_OP_reg2:
22864 case DW_OP_reg3:
22865 case DW_OP_reg4:
22866 case DW_OP_reg5:
22867 case DW_OP_reg6:
22868 case DW_OP_reg7:
22869 case DW_OP_reg8:
22870 case DW_OP_reg9:
22871 case DW_OP_reg10:
22872 case DW_OP_reg11:
22873 case DW_OP_reg12:
22874 case DW_OP_reg13:
22875 case DW_OP_reg14:
22876 case DW_OP_reg15:
22877 case DW_OP_reg16:
22878 case DW_OP_reg17:
22879 case DW_OP_reg18:
22880 case DW_OP_reg19:
22881 case DW_OP_reg20:
22882 case DW_OP_reg21:
22883 case DW_OP_reg22:
22884 case DW_OP_reg23:
22885 case DW_OP_reg24:
22886 case DW_OP_reg25:
22887 case DW_OP_reg26:
22888 case DW_OP_reg27:
22889 case DW_OP_reg28:
22890 case DW_OP_reg29:
22891 case DW_OP_reg30:
22892 case DW_OP_reg31:
22893 stack[++stacki] = op - DW_OP_reg0;
22894 if (i < size)
22895 dwarf2_complex_location_expr_complaint ();
22896 break;
22897
22898 case DW_OP_regx:
22899 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22900 i += bytes_read;
22901 stack[++stacki] = unsnd;
22902 if (i < size)
22903 dwarf2_complex_location_expr_complaint ();
22904 break;
22905
22906 case DW_OP_addr:
22907 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22908 &bytes_read);
22909 i += bytes_read;
22910 break;
22911
22912 case DW_OP_const1u:
22913 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22914 i += 1;
22915 break;
22916
22917 case DW_OP_const1s:
22918 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22919 i += 1;
22920 break;
22921
22922 case DW_OP_const2u:
22923 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22924 i += 2;
22925 break;
22926
22927 case DW_OP_const2s:
22928 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22929 i += 2;
22930 break;
22931
22932 case DW_OP_const4u:
22933 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22934 i += 4;
22935 break;
22936
22937 case DW_OP_const4s:
22938 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22939 i += 4;
22940 break;
22941
22942 case DW_OP_const8u:
22943 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22944 i += 8;
22945 break;
22946
22947 case DW_OP_constu:
22948 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22949 &bytes_read);
22950 i += bytes_read;
22951 break;
22952
22953 case DW_OP_consts:
22954 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22955 i += bytes_read;
22956 break;
22957
22958 case DW_OP_dup:
22959 stack[stacki + 1] = stack[stacki];
22960 stacki++;
22961 break;
22962
22963 case DW_OP_plus:
22964 stack[stacki - 1] += stack[stacki];
22965 stacki--;
22966 break;
22967
22968 case DW_OP_plus_uconst:
22969 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22970 &bytes_read);
22971 i += bytes_read;
22972 break;
22973
22974 case DW_OP_minus:
22975 stack[stacki - 1] -= stack[stacki];
22976 stacki--;
22977 break;
22978
22979 case DW_OP_deref:
22980 /* If we're not the last op, then we definitely can't encode
22981 this using GDB's address_class enum. This is valid for partial
22982 global symbols, although the variable's address will be bogus
22983 in the psymtab. */
22984 if (i < size)
22985 dwarf2_complex_location_expr_complaint ();
22986 break;
22987
22988 case DW_OP_GNU_push_tls_address:
22989 case DW_OP_form_tls_address:
22990 /* The top of the stack has the offset from the beginning
22991 of the thread control block at which the variable is located. */
22992 /* Nothing should follow this operator, so the top of stack would
22993 be returned. */
22994 /* This is valid for partial global symbols, but the variable's
22995 address will be bogus in the psymtab. Make it always at least
22996 non-zero to not look as a variable garbage collected by linker
22997 which have DW_OP_addr 0. */
22998 if (i < size)
22999 dwarf2_complex_location_expr_complaint ();
23000 stack[stacki]++;
23001 break;
23002
23003 case DW_OP_GNU_uninit:
23004 break;
23005
23006 case DW_OP_addrx:
23007 case DW_OP_GNU_addr_index:
23008 case DW_OP_GNU_const_index:
23009 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23010 &bytes_read);
23011 i += bytes_read;
23012 break;
23013
23014 default:
23015 {
23016 const char *name = get_DW_OP_name (op);
23017
23018 if (name)
23019 complaint (_("unsupported stack op: '%s'"),
23020 name);
23021 else
23022 complaint (_("unsupported stack op: '%02x'"),
23023 op);
23024 }
23025
23026 return (stack[stacki]);
23027 }
23028
23029 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23030 outside of the allocated space. Also enforce minimum>0. */
23031 if (stacki >= ARRAY_SIZE (stack) - 1)
23032 {
23033 complaint (_("location description stack overflow"));
23034 return 0;
23035 }
23036
23037 if (stacki <= 0)
23038 {
23039 complaint (_("location description stack underflow"));
23040 return 0;
23041 }
23042 }
23043 return (stack[stacki]);
23044 }
23045
23046 /* memory allocation interface */
23047
23048 static struct dwarf_block *
23049 dwarf_alloc_block (struct dwarf2_cu *cu)
23050 {
23051 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23052 }
23053
23054 static struct die_info *
23055 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23056 {
23057 struct die_info *die;
23058 size_t size = sizeof (struct die_info);
23059
23060 if (num_attrs > 1)
23061 size += (num_attrs - 1) * sizeof (struct attribute);
23062
23063 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23064 memset (die, 0, sizeof (struct die_info));
23065 return (die);
23066 }
23067
23068 \f
23069 /* Macro support. */
23070
23071 static struct macro_source_file *
23072 macro_start_file (struct dwarf2_cu *cu,
23073 int file, int line,
23074 struct macro_source_file *current_file,
23075 struct line_header *lh)
23076 {
23077 /* File name relative to the compilation directory of this source file. */
23078 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
23079
23080 if (! current_file)
23081 {
23082 /* Note: We don't create a macro table for this compilation unit
23083 at all until we actually get a filename. */
23084 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
23085
23086 /* If we have no current file, then this must be the start_file
23087 directive for the compilation unit's main source file. */
23088 current_file = macro_set_main (macro_table, file_name.get ());
23089 macro_define_special (macro_table);
23090 }
23091 else
23092 current_file = macro_include (current_file, line, file_name.get ());
23093
23094 return current_file;
23095 }
23096
23097 static const char *
23098 consume_improper_spaces (const char *p, const char *body)
23099 {
23100 if (*p == ' ')
23101 {
23102 complaint (_("macro definition contains spaces "
23103 "in formal argument list:\n`%s'"),
23104 body);
23105
23106 while (*p == ' ')
23107 p++;
23108 }
23109
23110 return p;
23111 }
23112
23113
23114 static void
23115 parse_macro_definition (struct macro_source_file *file, int line,
23116 const char *body)
23117 {
23118 const char *p;
23119
23120 /* The body string takes one of two forms. For object-like macro
23121 definitions, it should be:
23122
23123 <macro name> " " <definition>
23124
23125 For function-like macro definitions, it should be:
23126
23127 <macro name> "() " <definition>
23128 or
23129 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23130
23131 Spaces may appear only where explicitly indicated, and in the
23132 <definition>.
23133
23134 The Dwarf 2 spec says that an object-like macro's name is always
23135 followed by a space, but versions of GCC around March 2002 omit
23136 the space when the macro's definition is the empty string.
23137
23138 The Dwarf 2 spec says that there should be no spaces between the
23139 formal arguments in a function-like macro's formal argument list,
23140 but versions of GCC around March 2002 include spaces after the
23141 commas. */
23142
23143
23144 /* Find the extent of the macro name. The macro name is terminated
23145 by either a space or null character (for an object-like macro) or
23146 an opening paren (for a function-like macro). */
23147 for (p = body; *p; p++)
23148 if (*p == ' ' || *p == '(')
23149 break;
23150
23151 if (*p == ' ' || *p == '\0')
23152 {
23153 /* It's an object-like macro. */
23154 int name_len = p - body;
23155 std::string name (body, name_len);
23156 const char *replacement;
23157
23158 if (*p == ' ')
23159 replacement = body + name_len + 1;
23160 else
23161 {
23162 dwarf2_macro_malformed_definition_complaint (body);
23163 replacement = body + name_len;
23164 }
23165
23166 macro_define_object (file, line, name.c_str (), replacement);
23167 }
23168 else if (*p == '(')
23169 {
23170 /* It's a function-like macro. */
23171 std::string name (body, p - body);
23172 int argc = 0;
23173 int argv_size = 1;
23174 char **argv = XNEWVEC (char *, argv_size);
23175
23176 p++;
23177
23178 p = consume_improper_spaces (p, body);
23179
23180 /* Parse the formal argument list. */
23181 while (*p && *p != ')')
23182 {
23183 /* Find the extent of the current argument name. */
23184 const char *arg_start = p;
23185
23186 while (*p && *p != ',' && *p != ')' && *p != ' ')
23187 p++;
23188
23189 if (! *p || p == arg_start)
23190 dwarf2_macro_malformed_definition_complaint (body);
23191 else
23192 {
23193 /* Make sure argv has room for the new argument. */
23194 if (argc >= argv_size)
23195 {
23196 argv_size *= 2;
23197 argv = XRESIZEVEC (char *, argv, argv_size);
23198 }
23199
23200 argv[argc++] = savestring (arg_start, p - arg_start);
23201 }
23202
23203 p = consume_improper_spaces (p, body);
23204
23205 /* Consume the comma, if present. */
23206 if (*p == ',')
23207 {
23208 p++;
23209
23210 p = consume_improper_spaces (p, body);
23211 }
23212 }
23213
23214 if (*p == ')')
23215 {
23216 p++;
23217
23218 if (*p == ' ')
23219 /* Perfectly formed definition, no complaints. */
23220 macro_define_function (file, line, name.c_str (),
23221 argc, (const char **) argv,
23222 p + 1);
23223 else if (*p == '\0')
23224 {
23225 /* Complain, but do define it. */
23226 dwarf2_macro_malformed_definition_complaint (body);
23227 macro_define_function (file, line, name.c_str (),
23228 argc, (const char **) argv,
23229 p);
23230 }
23231 else
23232 /* Just complain. */
23233 dwarf2_macro_malformed_definition_complaint (body);
23234 }
23235 else
23236 /* Just complain. */
23237 dwarf2_macro_malformed_definition_complaint (body);
23238
23239 {
23240 int i;
23241
23242 for (i = 0; i < argc; i++)
23243 xfree (argv[i]);
23244 }
23245 xfree (argv);
23246 }
23247 else
23248 dwarf2_macro_malformed_definition_complaint (body);
23249 }
23250
23251 /* Skip some bytes from BYTES according to the form given in FORM.
23252 Returns the new pointer. */
23253
23254 static const gdb_byte *
23255 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
23256 enum dwarf_form form,
23257 unsigned int offset_size,
23258 struct dwarf2_section_info *section)
23259 {
23260 unsigned int bytes_read;
23261
23262 switch (form)
23263 {
23264 case DW_FORM_data1:
23265 case DW_FORM_flag:
23266 ++bytes;
23267 break;
23268
23269 case DW_FORM_data2:
23270 bytes += 2;
23271 break;
23272
23273 case DW_FORM_data4:
23274 bytes += 4;
23275 break;
23276
23277 case DW_FORM_data8:
23278 bytes += 8;
23279 break;
23280
23281 case DW_FORM_data16:
23282 bytes += 16;
23283 break;
23284
23285 case DW_FORM_string:
23286 read_direct_string (abfd, bytes, &bytes_read);
23287 bytes += bytes_read;
23288 break;
23289
23290 case DW_FORM_sec_offset:
23291 case DW_FORM_strp:
23292 case DW_FORM_GNU_strp_alt:
23293 bytes += offset_size;
23294 break;
23295
23296 case DW_FORM_block:
23297 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23298 bytes += bytes_read;
23299 break;
23300
23301 case DW_FORM_block1:
23302 bytes += 1 + read_1_byte (abfd, bytes);
23303 break;
23304 case DW_FORM_block2:
23305 bytes += 2 + read_2_bytes (abfd, bytes);
23306 break;
23307 case DW_FORM_block4:
23308 bytes += 4 + read_4_bytes (abfd, bytes);
23309 break;
23310
23311 case DW_FORM_addrx:
23312 case DW_FORM_sdata:
23313 case DW_FORM_strx:
23314 case DW_FORM_udata:
23315 case DW_FORM_GNU_addr_index:
23316 case DW_FORM_GNU_str_index:
23317 bytes = gdb_skip_leb128 (bytes, buffer_end);
23318 if (bytes == NULL)
23319 {
23320 dwarf2_section_buffer_overflow_complaint (section);
23321 return NULL;
23322 }
23323 break;
23324
23325 case DW_FORM_implicit_const:
23326 break;
23327
23328 default:
23329 {
23330 complaint (_("invalid form 0x%x in `%s'"),
23331 form, section->get_name ());
23332 return NULL;
23333 }
23334 }
23335
23336 return bytes;
23337 }
23338
23339 /* A helper for dwarf_decode_macros that handles skipping an unknown
23340 opcode. Returns an updated pointer to the macro data buffer; or,
23341 on error, issues a complaint and returns NULL. */
23342
23343 static const gdb_byte *
23344 skip_unknown_opcode (unsigned int opcode,
23345 const gdb_byte **opcode_definitions,
23346 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23347 bfd *abfd,
23348 unsigned int offset_size,
23349 struct dwarf2_section_info *section)
23350 {
23351 unsigned int bytes_read, i;
23352 unsigned long arg;
23353 const gdb_byte *defn;
23354
23355 if (opcode_definitions[opcode] == NULL)
23356 {
23357 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
23358 opcode);
23359 return NULL;
23360 }
23361
23362 defn = opcode_definitions[opcode];
23363 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23364 defn += bytes_read;
23365
23366 for (i = 0; i < arg; ++i)
23367 {
23368 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23369 (enum dwarf_form) defn[i], offset_size,
23370 section);
23371 if (mac_ptr == NULL)
23372 {
23373 /* skip_form_bytes already issued the complaint. */
23374 return NULL;
23375 }
23376 }
23377
23378 return mac_ptr;
23379 }
23380
23381 /* A helper function which parses the header of a macro section.
23382 If the macro section is the extended (for now called "GNU") type,
23383 then this updates *OFFSET_SIZE. Returns a pointer to just after
23384 the header, or issues a complaint and returns NULL on error. */
23385
23386 static const gdb_byte *
23387 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
23388 bfd *abfd,
23389 const gdb_byte *mac_ptr,
23390 unsigned int *offset_size,
23391 int section_is_gnu)
23392 {
23393 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
23394
23395 if (section_is_gnu)
23396 {
23397 unsigned int version, flags;
23398
23399 version = read_2_bytes (abfd, mac_ptr);
23400 if (version != 4 && version != 5)
23401 {
23402 complaint (_("unrecognized version `%d' in .debug_macro section"),
23403 version);
23404 return NULL;
23405 }
23406 mac_ptr += 2;
23407
23408 flags = read_1_byte (abfd, mac_ptr);
23409 ++mac_ptr;
23410 *offset_size = (flags & 1) ? 8 : 4;
23411
23412 if ((flags & 2) != 0)
23413 /* We don't need the line table offset. */
23414 mac_ptr += *offset_size;
23415
23416 /* Vendor opcode descriptions. */
23417 if ((flags & 4) != 0)
23418 {
23419 unsigned int i, count;
23420
23421 count = read_1_byte (abfd, mac_ptr);
23422 ++mac_ptr;
23423 for (i = 0; i < count; ++i)
23424 {
23425 unsigned int opcode, bytes_read;
23426 unsigned long arg;
23427
23428 opcode = read_1_byte (abfd, mac_ptr);
23429 ++mac_ptr;
23430 opcode_definitions[opcode] = mac_ptr;
23431 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23432 mac_ptr += bytes_read;
23433 mac_ptr += arg;
23434 }
23435 }
23436 }
23437
23438 return mac_ptr;
23439 }
23440
23441 /* A helper for dwarf_decode_macros that handles the GNU extensions,
23442 including DW_MACRO_import. */
23443
23444 static void
23445 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
23446 bfd *abfd,
23447 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
23448 struct macro_source_file *current_file,
23449 struct line_header *lh,
23450 struct dwarf2_section_info *section,
23451 int section_is_gnu, int section_is_dwz,
23452 unsigned int offset_size,
23453 htab_t include_hash)
23454 {
23455 struct dwarf2_per_objfile *dwarf2_per_objfile
23456 = cu->per_cu->dwarf2_per_objfile;
23457 struct objfile *objfile = dwarf2_per_objfile->objfile;
23458 enum dwarf_macro_record_type macinfo_type;
23459 int at_commandline;
23460 const gdb_byte *opcode_definitions[256];
23461
23462 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23463 &offset_size, section_is_gnu);
23464 if (mac_ptr == NULL)
23465 {
23466 /* We already issued a complaint. */
23467 return;
23468 }
23469
23470 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23471 GDB is still reading the definitions from command line. First
23472 DW_MACINFO_start_file will need to be ignored as it was already executed
23473 to create CURRENT_FILE for the main source holding also the command line
23474 definitions. On first met DW_MACINFO_start_file this flag is reset to
23475 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23476
23477 at_commandline = 1;
23478
23479 do
23480 {
23481 /* Do we at least have room for a macinfo type byte? */
23482 if (mac_ptr >= mac_end)
23483 {
23484 dwarf2_section_buffer_overflow_complaint (section);
23485 break;
23486 }
23487
23488 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23489 mac_ptr++;
23490
23491 /* Note that we rely on the fact that the corresponding GNU and
23492 DWARF constants are the same. */
23493 DIAGNOSTIC_PUSH
23494 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23495 switch (macinfo_type)
23496 {
23497 /* A zero macinfo type indicates the end of the macro
23498 information. */
23499 case 0:
23500 break;
23501
23502 case DW_MACRO_define:
23503 case DW_MACRO_undef:
23504 case DW_MACRO_define_strp:
23505 case DW_MACRO_undef_strp:
23506 case DW_MACRO_define_sup:
23507 case DW_MACRO_undef_sup:
23508 {
23509 unsigned int bytes_read;
23510 int line;
23511 const char *body;
23512 int is_define;
23513
23514 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23515 mac_ptr += bytes_read;
23516
23517 if (macinfo_type == DW_MACRO_define
23518 || macinfo_type == DW_MACRO_undef)
23519 {
23520 body = read_direct_string (abfd, mac_ptr, &bytes_read);
23521 mac_ptr += bytes_read;
23522 }
23523 else
23524 {
23525 LONGEST str_offset;
23526
23527 str_offset = read_offset (abfd, mac_ptr, offset_size);
23528 mac_ptr += offset_size;
23529
23530 if (macinfo_type == DW_MACRO_define_sup
23531 || macinfo_type == DW_MACRO_undef_sup
23532 || section_is_dwz)
23533 {
23534 struct dwz_file *dwz
23535 = dwarf2_get_dwz_file (dwarf2_per_objfile);
23536
23537 body = read_indirect_string_from_dwz (objfile,
23538 dwz, str_offset);
23539 }
23540 else
23541 body = read_indirect_string_at_offset (dwarf2_per_objfile,
23542 abfd, str_offset);
23543 }
23544
23545 is_define = (macinfo_type == DW_MACRO_define
23546 || macinfo_type == DW_MACRO_define_strp
23547 || macinfo_type == DW_MACRO_define_sup);
23548 if (! current_file)
23549 {
23550 /* DWARF violation as no main source is present. */
23551 complaint (_("debug info with no main source gives macro %s "
23552 "on line %d: %s"),
23553 is_define ? _("definition") : _("undefinition"),
23554 line, body);
23555 break;
23556 }
23557 if ((line == 0 && !at_commandline)
23558 || (line != 0 && at_commandline))
23559 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
23560 at_commandline ? _("command-line") : _("in-file"),
23561 is_define ? _("definition") : _("undefinition"),
23562 line == 0 ? _("zero") : _("non-zero"), line, body);
23563
23564 if (body == NULL)
23565 {
23566 /* Fedora's rpm-build's "debugedit" binary
23567 corrupted .debug_macro sections.
23568
23569 For more info, see
23570 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
23571 complaint (_("debug info gives %s invalid macro %s "
23572 "without body (corrupted?) at line %d "
23573 "on file %s"),
23574 at_commandline ? _("command-line") : _("in-file"),
23575 is_define ? _("definition") : _("undefinition"),
23576 line, current_file->filename);
23577 }
23578 else if (is_define)
23579 parse_macro_definition (current_file, line, body);
23580 else
23581 {
23582 gdb_assert (macinfo_type == DW_MACRO_undef
23583 || macinfo_type == DW_MACRO_undef_strp
23584 || macinfo_type == DW_MACRO_undef_sup);
23585 macro_undef (current_file, line, body);
23586 }
23587 }
23588 break;
23589
23590 case DW_MACRO_start_file:
23591 {
23592 unsigned int bytes_read;
23593 int line, file;
23594
23595 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23596 mac_ptr += bytes_read;
23597 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23598 mac_ptr += bytes_read;
23599
23600 if ((line == 0 && !at_commandline)
23601 || (line != 0 && at_commandline))
23602 complaint (_("debug info gives source %d included "
23603 "from %s at %s line %d"),
23604 file, at_commandline ? _("command-line") : _("file"),
23605 line == 0 ? _("zero") : _("non-zero"), line);
23606
23607 if (at_commandline)
23608 {
23609 /* This DW_MACRO_start_file was executed in the
23610 pass one. */
23611 at_commandline = 0;
23612 }
23613 else
23614 current_file = macro_start_file (cu, file, line, current_file,
23615 lh);
23616 }
23617 break;
23618
23619 case DW_MACRO_end_file:
23620 if (! current_file)
23621 complaint (_("macro debug info has an unmatched "
23622 "`close_file' directive"));
23623 else
23624 {
23625 current_file = current_file->included_by;
23626 if (! current_file)
23627 {
23628 enum dwarf_macro_record_type next_type;
23629
23630 /* GCC circa March 2002 doesn't produce the zero
23631 type byte marking the end of the compilation
23632 unit. Complain if it's not there, but exit no
23633 matter what. */
23634
23635 /* Do we at least have room for a macinfo type byte? */
23636 if (mac_ptr >= mac_end)
23637 {
23638 dwarf2_section_buffer_overflow_complaint (section);
23639 return;
23640 }
23641
23642 /* We don't increment mac_ptr here, so this is just
23643 a look-ahead. */
23644 next_type
23645 = (enum dwarf_macro_record_type) read_1_byte (abfd,
23646 mac_ptr);
23647 if (next_type != 0)
23648 complaint (_("no terminating 0-type entry for "
23649 "macros in `.debug_macinfo' section"));
23650
23651 return;
23652 }
23653 }
23654 break;
23655
23656 case DW_MACRO_import:
23657 case DW_MACRO_import_sup:
23658 {
23659 LONGEST offset;
23660 void **slot;
23661 bfd *include_bfd = abfd;
23662 struct dwarf2_section_info *include_section = section;
23663 const gdb_byte *include_mac_end = mac_end;
23664 int is_dwz = section_is_dwz;
23665 const gdb_byte *new_mac_ptr;
23666
23667 offset = read_offset (abfd, mac_ptr, offset_size);
23668 mac_ptr += offset_size;
23669
23670 if (macinfo_type == DW_MACRO_import_sup)
23671 {
23672 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
23673
23674 dwz->macro.read (objfile);
23675
23676 include_section = &dwz->macro;
23677 include_bfd = include_section->get_bfd_owner ();
23678 include_mac_end = dwz->macro.buffer + dwz->macro.size;
23679 is_dwz = 1;
23680 }
23681
23682 new_mac_ptr = include_section->buffer + offset;
23683 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
23684
23685 if (*slot != NULL)
23686 {
23687 /* This has actually happened; see
23688 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
23689 complaint (_("recursive DW_MACRO_import in "
23690 ".debug_macro section"));
23691 }
23692 else
23693 {
23694 *slot = (void *) new_mac_ptr;
23695
23696 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
23697 include_mac_end, current_file, lh,
23698 section, section_is_gnu, is_dwz,
23699 offset_size, include_hash);
23700
23701 htab_remove_elt (include_hash, (void *) new_mac_ptr);
23702 }
23703 }
23704 break;
23705
23706 case DW_MACINFO_vendor_ext:
23707 if (!section_is_gnu)
23708 {
23709 unsigned int bytes_read;
23710
23711 /* This reads the constant, but since we don't recognize
23712 any vendor extensions, we ignore it. */
23713 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23714 mac_ptr += bytes_read;
23715 read_direct_string (abfd, mac_ptr, &bytes_read);
23716 mac_ptr += bytes_read;
23717
23718 /* We don't recognize any vendor extensions. */
23719 break;
23720 }
23721 /* FALLTHROUGH */
23722
23723 default:
23724 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23725 mac_ptr, mac_end, abfd, offset_size,
23726 section);
23727 if (mac_ptr == NULL)
23728 return;
23729 break;
23730 }
23731 DIAGNOSTIC_POP
23732 } while (macinfo_type != 0);
23733 }
23734
23735 static void
23736 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23737 int section_is_gnu)
23738 {
23739 struct dwarf2_per_objfile *dwarf2_per_objfile
23740 = cu->per_cu->dwarf2_per_objfile;
23741 struct objfile *objfile = dwarf2_per_objfile->objfile;
23742 struct line_header *lh = cu->line_header;
23743 bfd *abfd;
23744 const gdb_byte *mac_ptr, *mac_end;
23745 struct macro_source_file *current_file = 0;
23746 enum dwarf_macro_record_type macinfo_type;
23747 unsigned int offset_size = cu->header.offset_size;
23748 const gdb_byte *opcode_definitions[256];
23749 void **slot;
23750 struct dwarf2_section_info *section;
23751 const char *section_name;
23752
23753 if (cu->dwo_unit != NULL)
23754 {
23755 if (section_is_gnu)
23756 {
23757 section = &cu->dwo_unit->dwo_file->sections.macro;
23758 section_name = ".debug_macro.dwo";
23759 }
23760 else
23761 {
23762 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23763 section_name = ".debug_macinfo.dwo";
23764 }
23765 }
23766 else
23767 {
23768 if (section_is_gnu)
23769 {
23770 section = &dwarf2_per_objfile->macro;
23771 section_name = ".debug_macro";
23772 }
23773 else
23774 {
23775 section = &dwarf2_per_objfile->macinfo;
23776 section_name = ".debug_macinfo";
23777 }
23778 }
23779
23780 section->read (objfile);
23781 if (section->buffer == NULL)
23782 {
23783 complaint (_("missing %s section"), section_name);
23784 return;
23785 }
23786 abfd = section->get_bfd_owner ();
23787
23788 /* First pass: Find the name of the base filename.
23789 This filename is needed in order to process all macros whose definition
23790 (or undefinition) comes from the command line. These macros are defined
23791 before the first DW_MACINFO_start_file entry, and yet still need to be
23792 associated to the base file.
23793
23794 To determine the base file name, we scan the macro definitions until we
23795 reach the first DW_MACINFO_start_file entry. We then initialize
23796 CURRENT_FILE accordingly so that any macro definition found before the
23797 first DW_MACINFO_start_file can still be associated to the base file. */
23798
23799 mac_ptr = section->buffer + offset;
23800 mac_end = section->buffer + section->size;
23801
23802 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23803 &offset_size, section_is_gnu);
23804 if (mac_ptr == NULL)
23805 {
23806 /* We already issued a complaint. */
23807 return;
23808 }
23809
23810 do
23811 {
23812 /* Do we at least have room for a macinfo type byte? */
23813 if (mac_ptr >= mac_end)
23814 {
23815 /* Complaint is printed during the second pass as GDB will probably
23816 stop the first pass earlier upon finding
23817 DW_MACINFO_start_file. */
23818 break;
23819 }
23820
23821 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
23822 mac_ptr++;
23823
23824 /* Note that we rely on the fact that the corresponding GNU and
23825 DWARF constants are the same. */
23826 DIAGNOSTIC_PUSH
23827 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
23828 switch (macinfo_type)
23829 {
23830 /* A zero macinfo type indicates the end of the macro
23831 information. */
23832 case 0:
23833 break;
23834
23835 case DW_MACRO_define:
23836 case DW_MACRO_undef:
23837 /* Only skip the data by MAC_PTR. */
23838 {
23839 unsigned int bytes_read;
23840
23841 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23842 mac_ptr += bytes_read;
23843 read_direct_string (abfd, mac_ptr, &bytes_read);
23844 mac_ptr += bytes_read;
23845 }
23846 break;
23847
23848 case DW_MACRO_start_file:
23849 {
23850 unsigned int bytes_read;
23851 int line, file;
23852
23853 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23854 mac_ptr += bytes_read;
23855 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23856 mac_ptr += bytes_read;
23857
23858 current_file = macro_start_file (cu, file, line, current_file, lh);
23859 }
23860 break;
23861
23862 case DW_MACRO_end_file:
23863 /* No data to skip by MAC_PTR. */
23864 break;
23865
23866 case DW_MACRO_define_strp:
23867 case DW_MACRO_undef_strp:
23868 case DW_MACRO_define_sup:
23869 case DW_MACRO_undef_sup:
23870 {
23871 unsigned int bytes_read;
23872
23873 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23874 mac_ptr += bytes_read;
23875 mac_ptr += offset_size;
23876 }
23877 break;
23878
23879 case DW_MACRO_import:
23880 case DW_MACRO_import_sup:
23881 /* Note that, according to the spec, a transparent include
23882 chain cannot call DW_MACRO_start_file. So, we can just
23883 skip this opcode. */
23884 mac_ptr += offset_size;
23885 break;
23886
23887 case DW_MACINFO_vendor_ext:
23888 /* Only skip the data by MAC_PTR. */
23889 if (!section_is_gnu)
23890 {
23891 unsigned int bytes_read;
23892
23893 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23894 mac_ptr += bytes_read;
23895 read_direct_string (abfd, mac_ptr, &bytes_read);
23896 mac_ptr += bytes_read;
23897 }
23898 /* FALLTHROUGH */
23899
23900 default:
23901 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
23902 mac_ptr, mac_end, abfd, offset_size,
23903 section);
23904 if (mac_ptr == NULL)
23905 return;
23906 break;
23907 }
23908 DIAGNOSTIC_POP
23909 } while (macinfo_type != 0 && current_file == NULL);
23910
23911 /* Second pass: Process all entries.
23912
23913 Use the AT_COMMAND_LINE flag to determine whether we are still processing
23914 command-line macro definitions/undefinitions. This flag is unset when we
23915 reach the first DW_MACINFO_start_file entry. */
23916
23917 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
23918 htab_eq_pointer,
23919 NULL, xcalloc, xfree));
23920 mac_ptr = section->buffer + offset;
23921 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
23922 *slot = (void *) mac_ptr;
23923 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
23924 current_file, lh, section,
23925 section_is_gnu, 0, offset_size,
23926 include_hash.get ());
23927 }
23928
23929 /* Return the .debug_loc section to use for CU.
23930 For DWO files use .debug_loc.dwo. */
23931
23932 static struct dwarf2_section_info *
23933 cu_debug_loc_section (struct dwarf2_cu *cu)
23934 {
23935 struct dwarf2_per_objfile *dwarf2_per_objfile
23936 = cu->per_cu->dwarf2_per_objfile;
23937
23938 if (cu->dwo_unit)
23939 {
23940 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23941
23942 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23943 }
23944 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23945 : &dwarf2_per_objfile->loc);
23946 }
23947
23948 /* A helper function that fills in a dwarf2_loclist_baton. */
23949
23950 static void
23951 fill_in_loclist_baton (struct dwarf2_cu *cu,
23952 struct dwarf2_loclist_baton *baton,
23953 const struct attribute *attr)
23954 {
23955 struct dwarf2_per_objfile *dwarf2_per_objfile
23956 = cu->per_cu->dwarf2_per_objfile;
23957 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23958
23959 section->read (dwarf2_per_objfile->objfile);
23960
23961 baton->per_cu = cu->per_cu;
23962 gdb_assert (baton->per_cu);
23963 /* We don't know how long the location list is, but make sure we
23964 don't run off the edge of the section. */
23965 baton->size = section->size - DW_UNSND (attr);
23966 baton->data = section->buffer + DW_UNSND (attr);
23967 baton->base_address = cu->base_address;
23968 baton->from_dwo = cu->dwo_unit != NULL;
23969 }
23970
23971 static void
23972 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23973 struct dwarf2_cu *cu, int is_block)
23974 {
23975 struct dwarf2_per_objfile *dwarf2_per_objfile
23976 = cu->per_cu->dwarf2_per_objfile;
23977 struct objfile *objfile = dwarf2_per_objfile->objfile;
23978 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23979
23980 if (attr->form_is_section_offset ()
23981 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23982 the section. If so, fall through to the complaint in the
23983 other branch. */
23984 && DW_UNSND (attr) < section->get_size (objfile))
23985 {
23986 struct dwarf2_loclist_baton *baton;
23987
23988 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23989
23990 fill_in_loclist_baton (cu, baton, attr);
23991
23992 if (cu->base_known == 0)
23993 complaint (_("Location list used without "
23994 "specifying the CU base address."));
23995
23996 SYMBOL_ACLASS_INDEX (sym) = (is_block
23997 ? dwarf2_loclist_block_index
23998 : dwarf2_loclist_index);
23999 SYMBOL_LOCATION_BATON (sym) = baton;
24000 }
24001 else
24002 {
24003 struct dwarf2_locexpr_baton *baton;
24004
24005 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24006 baton->per_cu = cu->per_cu;
24007 gdb_assert (baton->per_cu);
24008
24009 if (attr->form_is_block ())
24010 {
24011 /* Note that we're just copying the block's data pointer
24012 here, not the actual data. We're still pointing into the
24013 info_buffer for SYM's objfile; right now we never release
24014 that buffer, but when we do clean up properly this may
24015 need to change. */
24016 baton->size = DW_BLOCK (attr)->size;
24017 baton->data = DW_BLOCK (attr)->data;
24018 }
24019 else
24020 {
24021 dwarf2_invalid_attrib_class_complaint ("location description",
24022 sym->natural_name ());
24023 baton->size = 0;
24024 }
24025
24026 SYMBOL_ACLASS_INDEX (sym) = (is_block
24027 ? dwarf2_locexpr_block_index
24028 : dwarf2_locexpr_index);
24029 SYMBOL_LOCATION_BATON (sym) = baton;
24030 }
24031 }
24032
24033 /* See read.h. */
24034
24035 struct objfile *
24036 dwarf2_per_cu_data::objfile () const
24037 {
24038 struct objfile *objfile = dwarf2_per_objfile->objfile;
24039
24040 /* Return the master objfile, so that we can report and look up the
24041 correct file containing this variable. */
24042 if (objfile->separate_debug_objfile_backlink)
24043 objfile = objfile->separate_debug_objfile_backlink;
24044
24045 return objfile;
24046 }
24047
24048 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24049 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24050 CU_HEADERP first. */
24051
24052 static const struct comp_unit_head *
24053 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24054 const struct dwarf2_per_cu_data *per_cu)
24055 {
24056 const gdb_byte *info_ptr;
24057
24058 if (per_cu->cu)
24059 return &per_cu->cu->header;
24060
24061 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
24062
24063 memset (cu_headerp, 0, sizeof (*cu_headerp));
24064 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24065 rcuh_kind::COMPILE);
24066
24067 return cu_headerp;
24068 }
24069
24070 /* See read.h. */
24071
24072 int
24073 dwarf2_per_cu_data::addr_size () const
24074 {
24075 struct comp_unit_head cu_header_local;
24076 const struct comp_unit_head *cu_headerp;
24077
24078 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24079
24080 return cu_headerp->addr_size;
24081 }
24082
24083 /* See read.h. */
24084
24085 int
24086 dwarf2_per_cu_data::offset_size () const
24087 {
24088 struct comp_unit_head cu_header_local;
24089 const struct comp_unit_head *cu_headerp;
24090
24091 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24092
24093 return cu_headerp->offset_size;
24094 }
24095
24096 /* See read.h. */
24097
24098 int
24099 dwarf2_per_cu_data::ref_addr_size () const
24100 {
24101 struct comp_unit_head cu_header_local;
24102 const struct comp_unit_head *cu_headerp;
24103
24104 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
24105
24106 if (cu_headerp->version == 2)
24107 return cu_headerp->addr_size;
24108 else
24109 return cu_headerp->offset_size;
24110 }
24111
24112 /* See read.h. */
24113
24114 CORE_ADDR
24115 dwarf2_per_cu_data::text_offset () const
24116 {
24117 struct objfile *objfile = dwarf2_per_objfile->objfile;
24118
24119 return objfile->text_section_offset ();
24120 }
24121
24122 /* See read.h. */
24123
24124 struct type *
24125 dwarf2_per_cu_data::addr_type () const
24126 {
24127 struct objfile *objfile = dwarf2_per_objfile->objfile;
24128 struct type *void_type = objfile_type (objfile)->builtin_void;
24129 struct type *addr_type = lookup_pointer_type (void_type);
24130 int addr_size = this->addr_size ();
24131
24132 if (TYPE_LENGTH (addr_type) == addr_size)
24133 return addr_type;
24134
24135 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
24136 return addr_type;
24137 }
24138
24139 /* A helper function for dwarf2_find_containing_comp_unit that returns
24140 the index of the result, and that searches a vector. It will
24141 return a result even if the offset in question does not actually
24142 occur in any CU. This is separate so that it can be unit
24143 tested. */
24144
24145 static int
24146 dwarf2_find_containing_comp_unit
24147 (sect_offset sect_off,
24148 unsigned int offset_in_dwz,
24149 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24150 {
24151 int low, high;
24152
24153 low = 0;
24154 high = all_comp_units.size () - 1;
24155 while (high > low)
24156 {
24157 struct dwarf2_per_cu_data *mid_cu;
24158 int mid = low + (high - low) / 2;
24159
24160 mid_cu = all_comp_units[mid];
24161 if (mid_cu->is_dwz > offset_in_dwz
24162 || (mid_cu->is_dwz == offset_in_dwz
24163 && mid_cu->sect_off + mid_cu->length > sect_off))
24164 high = mid;
24165 else
24166 low = mid + 1;
24167 }
24168 gdb_assert (low == high);
24169 return low;
24170 }
24171
24172 /* Locate the .debug_info compilation unit from CU's objfile which contains
24173 the DIE at OFFSET. Raises an error on failure. */
24174
24175 static struct dwarf2_per_cu_data *
24176 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24177 unsigned int offset_in_dwz,
24178 struct dwarf2_per_objfile *dwarf2_per_objfile)
24179 {
24180 int low
24181 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
24182 dwarf2_per_objfile->all_comp_units);
24183 struct dwarf2_per_cu_data *this_cu
24184 = dwarf2_per_objfile->all_comp_units[low];
24185
24186 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24187 {
24188 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24189 error (_("Dwarf Error: could not find partial DIE containing "
24190 "offset %s [in module %s]"),
24191 sect_offset_str (sect_off),
24192 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
24193
24194 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24195 <= sect_off);
24196 return dwarf2_per_objfile->all_comp_units[low-1];
24197 }
24198 else
24199 {
24200 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
24201 && sect_off >= this_cu->sect_off + this_cu->length)
24202 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24203 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24204 return this_cu;
24205 }
24206 }
24207
24208 #if GDB_SELF_TEST
24209
24210 namespace selftests {
24211 namespace find_containing_comp_unit {
24212
24213 static void
24214 run_test ()
24215 {
24216 struct dwarf2_per_cu_data one {};
24217 struct dwarf2_per_cu_data two {};
24218 struct dwarf2_per_cu_data three {};
24219 struct dwarf2_per_cu_data four {};
24220
24221 one.length = 5;
24222 two.sect_off = sect_offset (one.length);
24223 two.length = 7;
24224
24225 three.length = 5;
24226 three.is_dwz = 1;
24227 four.sect_off = sect_offset (three.length);
24228 four.length = 7;
24229 four.is_dwz = 1;
24230
24231 std::vector<dwarf2_per_cu_data *> units;
24232 units.push_back (&one);
24233 units.push_back (&two);
24234 units.push_back (&three);
24235 units.push_back (&four);
24236
24237 int result;
24238
24239 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24240 SELF_CHECK (units[result] == &one);
24241 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24242 SELF_CHECK (units[result] == &one);
24243 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24244 SELF_CHECK (units[result] == &two);
24245
24246 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24247 SELF_CHECK (units[result] == &three);
24248 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24249 SELF_CHECK (units[result] == &three);
24250 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24251 SELF_CHECK (units[result] == &four);
24252 }
24253
24254 }
24255 }
24256
24257 #endif /* GDB_SELF_TEST */
24258
24259 /* Initialize dwarf2_cu CU, owned by PER_CU. */
24260
24261 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24262 : per_cu (per_cu_),
24263 mark (false),
24264 has_loclist (false),
24265 checked_producer (false),
24266 producer_is_gxx_lt_4_6 (false),
24267 producer_is_gcc_lt_4_3 (false),
24268 producer_is_icc (false),
24269 producer_is_icc_lt_14 (false),
24270 producer_is_codewarrior (false),
24271 processing_has_namespace_info (false)
24272 {
24273 per_cu->cu = this;
24274 }
24275
24276 /* Destroy a dwarf2_cu. */
24277
24278 dwarf2_cu::~dwarf2_cu ()
24279 {
24280 per_cu->cu = NULL;
24281 }
24282
24283 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24284
24285 static void
24286 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24287 enum language pretend_language)
24288 {
24289 struct attribute *attr;
24290
24291 /* Set the language we're debugging. */
24292 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24293 if (attr != nullptr)
24294 set_cu_language (DW_UNSND (attr), cu);
24295 else
24296 {
24297 cu->language = pretend_language;
24298 cu->language_defn = language_def (cu->language);
24299 }
24300
24301 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24302 }
24303
24304 /* Increase the age counter on each cached compilation unit, and free
24305 any that are too old. */
24306
24307 static void
24308 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
24309 {
24310 struct dwarf2_per_cu_data *per_cu, **last_chain;
24311
24312 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24313 per_cu = dwarf2_per_objfile->read_in_chain;
24314 while (per_cu != NULL)
24315 {
24316 per_cu->cu->last_used ++;
24317 if (per_cu->cu->last_used <= dwarf_max_cache_age)
24318 dwarf2_mark (per_cu->cu);
24319 per_cu = per_cu->cu->read_in_chain;
24320 }
24321
24322 per_cu = dwarf2_per_objfile->read_in_chain;
24323 last_chain = &dwarf2_per_objfile->read_in_chain;
24324 while (per_cu != NULL)
24325 {
24326 struct dwarf2_per_cu_data *next_cu;
24327
24328 next_cu = per_cu->cu->read_in_chain;
24329
24330 if (!per_cu->cu->mark)
24331 {
24332 delete per_cu->cu;
24333 *last_chain = next_cu;
24334 }
24335 else
24336 last_chain = &per_cu->cu->read_in_chain;
24337
24338 per_cu = next_cu;
24339 }
24340 }
24341
24342 /* Remove a single compilation unit from the cache. */
24343
24344 static void
24345 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
24346 {
24347 struct dwarf2_per_cu_data *per_cu, **last_chain;
24348 struct dwarf2_per_objfile *dwarf2_per_objfile
24349 = target_per_cu->dwarf2_per_objfile;
24350
24351 per_cu = dwarf2_per_objfile->read_in_chain;
24352 last_chain = &dwarf2_per_objfile->read_in_chain;
24353 while (per_cu != NULL)
24354 {
24355 struct dwarf2_per_cu_data *next_cu;
24356
24357 next_cu = per_cu->cu->read_in_chain;
24358
24359 if (per_cu == target_per_cu)
24360 {
24361 delete per_cu->cu;
24362 per_cu->cu = NULL;
24363 *last_chain = next_cu;
24364 break;
24365 }
24366 else
24367 last_chain = &per_cu->cu->read_in_chain;
24368
24369 per_cu = next_cu;
24370 }
24371 }
24372
24373 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24374 We store these in a hash table separate from the DIEs, and preserve them
24375 when the DIEs are flushed out of cache.
24376
24377 The CU "per_cu" pointer is needed because offset alone is not enough to
24378 uniquely identify the type. A file may have multiple .debug_types sections,
24379 or the type may come from a DWO file. Furthermore, while it's more logical
24380 to use per_cu->section+offset, with Fission the section with the data is in
24381 the DWO file but we don't know that section at the point we need it.
24382 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24383 because we can enter the lookup routine, get_die_type_at_offset, from
24384 outside this file, and thus won't necessarily have PER_CU->cu.
24385 Fortunately, PER_CU is stable for the life of the objfile. */
24386
24387 struct dwarf2_per_cu_offset_and_type
24388 {
24389 const struct dwarf2_per_cu_data *per_cu;
24390 sect_offset sect_off;
24391 struct type *type;
24392 };
24393
24394 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24395
24396 static hashval_t
24397 per_cu_offset_and_type_hash (const void *item)
24398 {
24399 const struct dwarf2_per_cu_offset_and_type *ofs
24400 = (const struct dwarf2_per_cu_offset_and_type *) item;
24401
24402 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24403 }
24404
24405 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24406
24407 static int
24408 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24409 {
24410 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24411 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24412 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24413 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24414
24415 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24416 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24417 }
24418
24419 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24420 table if necessary. For convenience, return TYPE.
24421
24422 The DIEs reading must have careful ordering to:
24423 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24424 reading current DIE.
24425 * Not trying to dereference contents of still incompletely read in types
24426 while reading in other DIEs.
24427 * Enable referencing still incompletely read in types just by a pointer to
24428 the type without accessing its fields.
24429
24430 Therefore caller should follow these rules:
24431 * Try to fetch any prerequisite types we may need to build this DIE type
24432 before building the type and calling set_die_type.
24433 * After building type call set_die_type for current DIE as soon as
24434 possible before fetching more types to complete the current type.
24435 * Make the type as complete as possible before fetching more types. */
24436
24437 static struct type *
24438 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24439 {
24440 struct dwarf2_per_objfile *dwarf2_per_objfile
24441 = cu->per_cu->dwarf2_per_objfile;
24442 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24443 struct objfile *objfile = dwarf2_per_objfile->objfile;
24444 struct attribute *attr;
24445 struct dynamic_prop prop;
24446
24447 /* For Ada types, make sure that the gnat-specific data is always
24448 initialized (if not already set). There are a few types where
24449 we should not be doing so, because the type-specific area is
24450 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24451 where the type-specific area is used to store the floatformat).
24452 But this is not a problem, because the gnat-specific information
24453 is actually not needed for these types. */
24454 if (need_gnat_info (cu)
24455 && TYPE_CODE (type) != TYPE_CODE_FUNC
24456 && TYPE_CODE (type) != TYPE_CODE_FLT
24457 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24458 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24459 && TYPE_CODE (type) != TYPE_CODE_METHOD
24460 && !HAVE_GNAT_AUX_INFO (type))
24461 INIT_GNAT_SPECIFIC (type);
24462
24463 /* Read DW_AT_allocated and set in type. */
24464 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24465 if (attr != NULL && attr->form_is_block ())
24466 {
24467 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24468 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24469 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
24470 }
24471 else if (attr != NULL)
24472 {
24473 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
24474 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24475 sect_offset_str (die->sect_off));
24476 }
24477
24478 /* Read DW_AT_associated and set in type. */
24479 attr = dwarf2_attr (die, DW_AT_associated, cu);
24480 if (attr != NULL && attr->form_is_block ())
24481 {
24482 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
24483 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24484 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
24485 }
24486 else if (attr != NULL)
24487 {
24488 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
24489 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
24490 sect_offset_str (die->sect_off));
24491 }
24492
24493 /* Read DW_AT_data_location and set in type. */
24494 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24495 if (attr_to_dynamic_prop (attr, die, cu, &prop,
24496 cu->per_cu->addr_type ()))
24497 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
24498
24499 if (dwarf2_per_objfile->die_type_hash == NULL)
24500 dwarf2_per_objfile->die_type_hash
24501 = htab_up (htab_create_alloc (127,
24502 per_cu_offset_and_type_hash,
24503 per_cu_offset_and_type_eq,
24504 NULL, xcalloc, xfree));
24505
24506 ofs.per_cu = cu->per_cu;
24507 ofs.sect_off = die->sect_off;
24508 ofs.type = type;
24509 slot = (struct dwarf2_per_cu_offset_and_type **)
24510 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
24511 if (*slot)
24512 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24513 sect_offset_str (die->sect_off));
24514 *slot = XOBNEW (&objfile->objfile_obstack,
24515 struct dwarf2_per_cu_offset_and_type);
24516 **slot = ofs;
24517 return type;
24518 }
24519
24520 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24521 or return NULL if the die does not have a saved type. */
24522
24523 static struct type *
24524 get_die_type_at_offset (sect_offset sect_off,
24525 struct dwarf2_per_cu_data *per_cu)
24526 {
24527 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24528 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
24529
24530 if (dwarf2_per_objfile->die_type_hash == NULL)
24531 return NULL;
24532
24533 ofs.per_cu = per_cu;
24534 ofs.sect_off = sect_off;
24535 slot = ((struct dwarf2_per_cu_offset_and_type *)
24536 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
24537 if (slot)
24538 return slot->type;
24539 else
24540 return NULL;
24541 }
24542
24543 /* Look up the type for DIE in CU in die_type_hash,
24544 or return NULL if DIE does not have a saved type. */
24545
24546 static struct type *
24547 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24548 {
24549 return get_die_type_at_offset (die->sect_off, cu->per_cu);
24550 }
24551
24552 /* Add a dependence relationship from CU to REF_PER_CU. */
24553
24554 static void
24555 dwarf2_add_dependence (struct dwarf2_cu *cu,
24556 struct dwarf2_per_cu_data *ref_per_cu)
24557 {
24558 void **slot;
24559
24560 if (cu->dependencies == NULL)
24561 cu->dependencies
24562 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24563 NULL, &cu->comp_unit_obstack,
24564 hashtab_obstack_allocate,
24565 dummy_obstack_deallocate);
24566
24567 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24568 if (*slot == NULL)
24569 *slot = ref_per_cu;
24570 }
24571
24572 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24573 Set the mark field in every compilation unit in the
24574 cache that we must keep because we are keeping CU. */
24575
24576 static int
24577 dwarf2_mark_helper (void **slot, void *data)
24578 {
24579 struct dwarf2_per_cu_data *per_cu;
24580
24581 per_cu = (struct dwarf2_per_cu_data *) *slot;
24582
24583 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24584 reading of the chain. As such dependencies remain valid it is not much
24585 useful to track and undo them during QUIT cleanups. */
24586 if (per_cu->cu == NULL)
24587 return 1;
24588
24589 if (per_cu->cu->mark)
24590 return 1;
24591 per_cu->cu->mark = true;
24592
24593 if (per_cu->cu->dependencies != NULL)
24594 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
24595
24596 return 1;
24597 }
24598
24599 /* Set the mark field in CU and in every other compilation unit in the
24600 cache that we must keep because we are keeping CU. */
24601
24602 static void
24603 dwarf2_mark (struct dwarf2_cu *cu)
24604 {
24605 if (cu->mark)
24606 return;
24607 cu->mark = true;
24608 if (cu->dependencies != NULL)
24609 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
24610 }
24611
24612 static void
24613 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
24614 {
24615 while (per_cu)
24616 {
24617 per_cu->cu->mark = false;
24618 per_cu = per_cu->cu->read_in_chain;
24619 }
24620 }
24621
24622 /* Trivial hash function for partial_die_info: the hash value of a DIE
24623 is its offset in .debug_info for this objfile. */
24624
24625 static hashval_t
24626 partial_die_hash (const void *item)
24627 {
24628 const struct partial_die_info *part_die
24629 = (const struct partial_die_info *) item;
24630
24631 return to_underlying (part_die->sect_off);
24632 }
24633
24634 /* Trivial comparison function for partial_die_info structures: two DIEs
24635 are equal if they have the same offset. */
24636
24637 static int
24638 partial_die_eq (const void *item_lhs, const void *item_rhs)
24639 {
24640 const struct partial_die_info *part_die_lhs
24641 = (const struct partial_die_info *) item_lhs;
24642 const struct partial_die_info *part_die_rhs
24643 = (const struct partial_die_info *) item_rhs;
24644
24645 return part_die_lhs->sect_off == part_die_rhs->sect_off;
24646 }
24647
24648 struct cmd_list_element *set_dwarf_cmdlist;
24649 struct cmd_list_element *show_dwarf_cmdlist;
24650
24651 static void
24652 set_dwarf_cmd (const char *args, int from_tty)
24653 {
24654 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
24655 gdb_stdout);
24656 }
24657
24658 static void
24659 show_dwarf_cmd (const char *args, int from_tty)
24660 {
24661 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
24662 }
24663
24664 static void
24665 show_check_physname (struct ui_file *file, int from_tty,
24666 struct cmd_list_element *c, const char *value)
24667 {
24668 fprintf_filtered (file,
24669 _("Whether to check \"physname\" is %s.\n"),
24670 value);
24671 }
24672
24673 void _initialize_dwarf2_read ();
24674 void
24675 _initialize_dwarf2_read ()
24676 {
24677 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
24678 Set DWARF specific variables.\n\
24679 Configure DWARF variables such as the cache size."),
24680 &set_dwarf_cmdlist, "maintenance set dwarf ",
24681 0/*allow-unknown*/, &maintenance_set_cmdlist);
24682
24683 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
24684 Show DWARF specific variables.\n\
24685 Show DWARF variables such as the cache size."),
24686 &show_dwarf_cmdlist, "maintenance show dwarf ",
24687 0/*allow-unknown*/, &maintenance_show_cmdlist);
24688
24689 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24690 &dwarf_max_cache_age, _("\
24691 Set the upper bound on the age of cached DWARF compilation units."), _("\
24692 Show the upper bound on the age of cached DWARF compilation units."), _("\
24693 A higher limit means that cached compilation units will be stored\n\
24694 in memory longer, and more total memory will be used. Zero disables\n\
24695 caching, which can slow down startup."),
24696 NULL,
24697 show_dwarf_max_cache_age,
24698 &set_dwarf_cmdlist,
24699 &show_dwarf_cmdlist);
24700
24701 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24702 Set debugging of the DWARF reader."), _("\
24703 Show debugging of the DWARF reader."), _("\
24704 When enabled (non-zero), debugging messages are printed during DWARF\n\
24705 reading and symtab expansion. A value of 1 (one) provides basic\n\
24706 information. A value greater than 1 provides more verbose information."),
24707 NULL,
24708 NULL,
24709 &setdebuglist, &showdebuglist);
24710
24711 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24712 Set debugging of the DWARF DIE reader."), _("\
24713 Show debugging of the DWARF DIE reader."), _("\
24714 When enabled (non-zero), DIEs are dumped after they are read in.\n\
24715 The value is the maximum depth to print."),
24716 NULL,
24717 NULL,
24718 &setdebuglist, &showdebuglist);
24719
24720 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24721 Set debugging of the dwarf line reader."), _("\
24722 Show debugging of the dwarf line reader."), _("\
24723 When enabled (non-zero), line number entries are dumped as they are read in.\n\
24724 A value of 1 (one) provides basic information.\n\
24725 A value greater than 1 provides more verbose information."),
24726 NULL,
24727 NULL,
24728 &setdebuglist, &showdebuglist);
24729
24730 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24731 Set cross-checking of \"physname\" code against demangler."), _("\
24732 Show cross-checking of \"physname\" code against demangler."), _("\
24733 When enabled, GDB's internal \"physname\" code is checked against\n\
24734 the demangler."),
24735 NULL, show_check_physname,
24736 &setdebuglist, &showdebuglist);
24737
24738 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24739 no_class, &use_deprecated_index_sections, _("\
24740 Set whether to use deprecated gdb_index sections."), _("\
24741 Show whether to use deprecated gdb_index sections."), _("\
24742 When enabled, deprecated .gdb_index sections are used anyway.\n\
24743 Normally they are ignored either because of a missing feature or\n\
24744 performance issue.\n\
24745 Warning: This option must be enabled before gdb reads the file."),
24746 NULL,
24747 NULL,
24748 &setlist, &showlist);
24749
24750 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24751 &dwarf2_locexpr_funcs);
24752 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24753 &dwarf2_loclist_funcs);
24754
24755 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24756 &dwarf2_block_frame_base_locexpr_funcs);
24757 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24758 &dwarf2_block_frame_base_loclist_funcs);
24759
24760 #if GDB_SELF_TEST
24761 selftests::register_test ("dw2_expand_symtabs_matching",
24762 selftests::dw2_expand_symtabs_matching::run_test);
24763 selftests::register_test ("dwarf2_find_containing_comp_unit",
24764 selftests::find_containing_comp_unit::run_test);
24765 #endif
24766 }
This page took 0.521951 seconds and 5 git commands to generate.