gdb/testsuite: Add compiler options parameter to function_range helper
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
109
110 /* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112 static int dwarf2_locexpr_index;
113 static int dwarf2_loclist_index;
114 static int dwarf2_locexpr_block_index;
115 static int dwarf2_loclist_block_index;
116
117 /* An index into a (C++) symbol name component in a symbol name as
118 recorded in the mapped_index's symbol table. For each C++ symbol
119 in the symbol table, we record one entry for the start of each
120 component in the symbol in a table of name components, and then
121 sort the table, in order to be able to binary search symbol names,
122 ignoring leading namespaces, both completion and regular look up.
123 For example, for symbol "A::B::C", we'll have an entry that points
124 to "A::B::C", another that points to "B::C", and another for "C".
125 Note that function symbols in GDB index have no parameter
126 information, just the function/method names. You can convert a
127 name_component to a "const char *" using the
128 'mapped_index::symbol_name_at(offset_type)' method. */
129
130 struct name_component
131 {
132 /* Offset in the symbol name where the component starts. Stored as
133 a (32-bit) offset instead of a pointer to save memory and improve
134 locality on 64-bit architectures. */
135 offset_type name_offset;
136
137 /* The symbol's index in the symbol and constant pool tables of a
138 mapped_index. */
139 offset_type idx;
140 };
141
142 /* Base class containing bits shared by both .gdb_index and
143 .debug_name indexes. */
144
145 struct mapped_index_base
146 {
147 mapped_index_base () = default;
148 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
149
150 /* The name_component table (a sorted vector). See name_component's
151 description above. */
152 std::vector<name_component> name_components;
153
154 /* How NAME_COMPONENTS is sorted. */
155 enum case_sensitivity name_components_casing;
156
157 /* Return the number of names in the symbol table. */
158 virtual size_t symbol_name_count () const = 0;
159
160 /* Get the name of the symbol at IDX in the symbol table. */
161 virtual const char *symbol_name_at (offset_type idx) const = 0;
162
163 /* Return whether the name at IDX in the symbol table should be
164 ignored. */
165 virtual bool symbol_name_slot_invalid (offset_type idx) const
166 {
167 return false;
168 }
169
170 /* Build the symbol name component sorted vector, if we haven't
171 yet. */
172 void build_name_components ();
173
174 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
175 possible matches for LN_NO_PARAMS in the name component
176 vector. */
177 std::pair<std::vector<name_component>::const_iterator,
178 std::vector<name_component>::const_iterator>
179 find_name_components_bounds (const lookup_name_info &ln_no_params,
180 enum language lang) const;
181
182 /* Prevent deleting/destroying via a base class pointer. */
183 protected:
184 ~mapped_index_base() = default;
185 };
186
187 /* A description of the mapped index. The file format is described in
188 a comment by the code that writes the index. */
189 struct mapped_index final : public mapped_index_base
190 {
191 /* A slot/bucket in the symbol table hash. */
192 struct symbol_table_slot
193 {
194 const offset_type name;
195 const offset_type vec;
196 };
197
198 /* Index data format version. */
199 int version = 0;
200
201 /* The address table data. */
202 gdb::array_view<const gdb_byte> address_table;
203
204 /* The symbol table, implemented as a hash table. */
205 gdb::array_view<symbol_table_slot> symbol_table;
206
207 /* A pointer to the constant pool. */
208 const char *constant_pool = nullptr;
209
210 bool symbol_name_slot_invalid (offset_type idx) const override
211 {
212 const auto &bucket = this->symbol_table[idx];
213 return bucket.name == 0 && bucket.vec == 0;
214 }
215
216 /* Convenience method to get at the name of the symbol at IDX in the
217 symbol table. */
218 const char *symbol_name_at (offset_type idx) const override
219 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
220
221 size_t symbol_name_count () const override
222 { return this->symbol_table.size (); }
223 };
224
225 /* A description of the mapped .debug_names.
226 Uninitialized map has CU_COUNT 0. */
227 struct mapped_debug_names final : public mapped_index_base
228 {
229 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
230 : dwarf2_per_objfile (dwarf2_per_objfile_)
231 {}
232
233 struct dwarf2_per_objfile *dwarf2_per_objfile;
234 bfd_endian dwarf5_byte_order;
235 bool dwarf5_is_dwarf64;
236 bool augmentation_is_gdb;
237 uint8_t offset_size;
238 uint32_t cu_count = 0;
239 uint32_t tu_count, bucket_count, name_count;
240 const gdb_byte *cu_table_reordered, *tu_table_reordered;
241 const uint32_t *bucket_table_reordered, *hash_table_reordered;
242 const gdb_byte *name_table_string_offs_reordered;
243 const gdb_byte *name_table_entry_offs_reordered;
244 const gdb_byte *entry_pool;
245
246 struct index_val
247 {
248 ULONGEST dwarf_tag;
249 struct attr
250 {
251 /* Attribute name DW_IDX_*. */
252 ULONGEST dw_idx;
253
254 /* Attribute form DW_FORM_*. */
255 ULONGEST form;
256
257 /* Value if FORM is DW_FORM_implicit_const. */
258 LONGEST implicit_const;
259 };
260 std::vector<attr> attr_vec;
261 };
262
263 std::unordered_map<ULONGEST, index_val> abbrev_map;
264
265 const char *namei_to_name (uint32_t namei) const;
266
267 /* Implementation of the mapped_index_base virtual interface, for
268 the name_components cache. */
269
270 const char *symbol_name_at (offset_type idx) const override
271 { return namei_to_name (idx); }
272
273 size_t symbol_name_count () const override
274 { return this->name_count; }
275 };
276
277 /* See dwarf2read.h. */
278
279 dwarf2_per_objfile *
280 get_dwarf2_per_objfile (struct objfile *objfile)
281 {
282 return dwarf2_objfile_data_key.get (objfile);
283 }
284
285 /* Default names of the debugging sections. */
286
287 /* Note that if the debugging section has been compressed, it might
288 have a name like .zdebug_info. */
289
290 static const struct dwarf2_debug_sections dwarf2_elf_names =
291 {
292 { ".debug_info", ".zdebug_info" },
293 { ".debug_abbrev", ".zdebug_abbrev" },
294 { ".debug_line", ".zdebug_line" },
295 { ".debug_loc", ".zdebug_loc" },
296 { ".debug_loclists", ".zdebug_loclists" },
297 { ".debug_macinfo", ".zdebug_macinfo" },
298 { ".debug_macro", ".zdebug_macro" },
299 { ".debug_str", ".zdebug_str" },
300 { ".debug_str_offsets", ".zdebug_str_offsets" },
301 { ".debug_line_str", ".zdebug_line_str" },
302 { ".debug_ranges", ".zdebug_ranges" },
303 { ".debug_rnglists", ".zdebug_rnglists" },
304 { ".debug_types", ".zdebug_types" },
305 { ".debug_addr", ".zdebug_addr" },
306 { ".debug_frame", ".zdebug_frame" },
307 { ".eh_frame", NULL },
308 { ".gdb_index", ".zgdb_index" },
309 { ".debug_names", ".zdebug_names" },
310 { ".debug_aranges", ".zdebug_aranges" },
311 23
312 };
313
314 /* List of DWO/DWP sections. */
315
316 static const struct dwop_section_names
317 {
318 struct dwarf2_section_names abbrev_dwo;
319 struct dwarf2_section_names info_dwo;
320 struct dwarf2_section_names line_dwo;
321 struct dwarf2_section_names loc_dwo;
322 struct dwarf2_section_names loclists_dwo;
323 struct dwarf2_section_names macinfo_dwo;
324 struct dwarf2_section_names macro_dwo;
325 struct dwarf2_section_names str_dwo;
326 struct dwarf2_section_names str_offsets_dwo;
327 struct dwarf2_section_names types_dwo;
328 struct dwarf2_section_names cu_index;
329 struct dwarf2_section_names tu_index;
330 }
331 dwop_section_names =
332 {
333 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
334 { ".debug_info.dwo", ".zdebug_info.dwo" },
335 { ".debug_line.dwo", ".zdebug_line.dwo" },
336 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
337 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
338 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
339 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
340 { ".debug_str.dwo", ".zdebug_str.dwo" },
341 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
342 { ".debug_types.dwo", ".zdebug_types.dwo" },
343 { ".debug_cu_index", ".zdebug_cu_index" },
344 { ".debug_tu_index", ".zdebug_tu_index" },
345 };
346
347 /* local data types */
348
349 /* Type used for delaying computation of method physnames.
350 See comments for compute_delayed_physnames. */
351 struct delayed_method_info
352 {
353 /* The type to which the method is attached, i.e., its parent class. */
354 struct type *type;
355
356 /* The index of the method in the type's function fieldlists. */
357 int fnfield_index;
358
359 /* The index of the method in the fieldlist. */
360 int index;
361
362 /* The name of the DIE. */
363 const char *name;
364
365 /* The DIE associated with this method. */
366 struct die_info *die;
367 };
368
369 /* Internal state when decoding a particular compilation unit. */
370 struct dwarf2_cu
371 {
372 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
373 ~dwarf2_cu ();
374
375 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
376
377 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
378 Create the set of symtabs used by this TU, or if this TU is sharing
379 symtabs with another TU and the symtabs have already been created
380 then restore those symtabs in the line header.
381 We don't need the pc/line-number mapping for type units. */
382 void setup_type_unit_groups (struct die_info *die);
383
384 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
385 buildsym_compunit constructor. */
386 struct compunit_symtab *start_symtab (const char *name,
387 const char *comp_dir,
388 CORE_ADDR low_pc);
389
390 /* Reset the builder. */
391 void reset_builder () { m_builder.reset (); }
392
393 /* The header of the compilation unit. */
394 struct comp_unit_head header {};
395
396 /* Base address of this compilation unit. */
397 gdb::optional<CORE_ADDR> base_address;
398
399 /* The language we are debugging. */
400 enum language language = language_unknown;
401 const struct language_defn *language_defn = nullptr;
402
403 const char *producer = nullptr;
404
405 private:
406 /* The symtab builder for this CU. This is only non-NULL when full
407 symbols are being read. */
408 std::unique_ptr<buildsym_compunit> m_builder;
409
410 public:
411 /* The generic symbol table building routines have separate lists for
412 file scope symbols and all all other scopes (local scopes). So
413 we need to select the right one to pass to add_symbol_to_list().
414 We do it by keeping a pointer to the correct list in list_in_scope.
415
416 FIXME: The original dwarf code just treated the file scope as the
417 first local scope, and all other local scopes as nested local
418 scopes, and worked fine. Check to see if we really need to
419 distinguish these in buildsym.c. */
420 struct pending **list_in_scope = nullptr;
421
422 /* Hash table holding all the loaded partial DIEs
423 with partial_die->offset.SECT_OFF as hash. */
424 htab_t partial_dies = nullptr;
425
426 /* Storage for things with the same lifetime as this read-in compilation
427 unit, including partial DIEs. */
428 auto_obstack comp_unit_obstack;
429
430 /* When multiple dwarf2_cu structures are living in memory, this field
431 chains them all together, so that they can be released efficiently.
432 We will probably also want a generation counter so that most-recently-used
433 compilation units are cached... */
434 struct dwarf2_per_cu_data *read_in_chain = nullptr;
435
436 /* Backlink to our per_cu entry. */
437 struct dwarf2_per_cu_data *per_cu;
438
439 /* How many compilation units ago was this CU last referenced? */
440 int last_used = 0;
441
442 /* A hash table of DIE cu_offset for following references with
443 die_info->offset.sect_off as hash. */
444 htab_t die_hash = nullptr;
445
446 /* Full DIEs if read in. */
447 struct die_info *dies = nullptr;
448
449 /* A set of pointers to dwarf2_per_cu_data objects for compilation
450 units referenced by this one. Only set during full symbol processing;
451 partial symbol tables do not have dependencies. */
452 htab_t dependencies = nullptr;
453
454 /* Header data from the line table, during full symbol processing. */
455 struct line_header *line_header = nullptr;
456 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
457 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
458 this is the DW_TAG_compile_unit die for this CU. We'll hold on
459 to the line header as long as this DIE is being processed. See
460 process_die_scope. */
461 die_info *line_header_die_owner = nullptr;
462
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
465 std::vector<delayed_method_info> method_list;
466
467 /* To be copied to symtab->call_site_htab. */
468 htab_t call_site_htab = nullptr;
469
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
474 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
478 is non-NULL). */
479 struct dwo_unit *dwo_unit = nullptr;
480
481 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
482 Note this value comes from the Fission stub CU/TU's DIE. */
483 gdb::optional<ULONGEST> addr_base;
484
485 /* The DW_AT_rnglists_base attribute if present.
486 Note this value comes from the Fission stub CU/TU's DIE.
487 Also note that the value is zero in the non-DWO case so this value can
488 be used without needing to know whether DWO files are in use or not.
489 N.B. This does not apply to DW_AT_ranges appearing in
490 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
491 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
492 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
493 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
494 ULONGEST ranges_base = 0;
495
496 /* When reading debug info generated by older versions of rustc, we
497 have to rewrite some union types to be struct types with a
498 variant part. This rewriting must be done after the CU is fully
499 read in, because otherwise at the point of rewriting some struct
500 type might not have been fully processed. So, we keep a list of
501 all such types here and process them after expansion. */
502 std::vector<struct type *> rust_unions;
503
504 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
505 files, the value is implicitly zero. For DWARF 5 version DWO files, the
506 value is often implicit and is the size of the header of
507 .debug_str_offsets section (8 or 4, depending on the address size). */
508 gdb::optional<ULONGEST> str_offsets_base;
509
510 /* Mark used when releasing cached dies. */
511 bool mark : 1;
512
513 /* This CU references .debug_loc. See the symtab->locations_valid field.
514 This test is imperfect as there may exist optimized debug code not using
515 any location list and still facing inlining issues if handled as
516 unoptimized code. For a future better test see GCC PR other/32998. */
517 bool has_loclist : 1;
518
519 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
520 if all the producer_is_* fields are valid. This information is cached
521 because profiling CU expansion showed excessive time spent in
522 producer_is_gxx_lt_4_6. */
523 bool checked_producer : 1;
524 bool producer_is_gxx_lt_4_6 : 1;
525 bool producer_is_gcc_lt_4_3 : 1;
526 bool producer_is_icc : 1;
527 bool producer_is_icc_lt_14 : 1;
528 bool producer_is_codewarrior : 1;
529
530 /* When true, the file that we're processing is known to have
531 debugging info for C++ namespaces. GCC 3.3.x did not produce
532 this information, but later versions do. */
533
534 bool processing_has_namespace_info : 1;
535
536 struct partial_die_info *find_partial_die (sect_offset sect_off);
537
538 /* If this CU was inherited by another CU (via specification,
539 abstract_origin, etc), this is the ancestor CU. */
540 dwarf2_cu *ancestor;
541
542 /* Get the buildsym_compunit for this CU. */
543 buildsym_compunit *get_builder ()
544 {
545 /* If this CU has a builder associated with it, use that. */
546 if (m_builder != nullptr)
547 return m_builder.get ();
548
549 /* Otherwise, search ancestors for a valid builder. */
550 if (ancestor != nullptr)
551 return ancestor->get_builder ();
552
553 return nullptr;
554 }
555 };
556
557 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
558 This includes type_unit_group and quick_file_names. */
559
560 struct stmt_list_hash
561 {
562 /* The DWO unit this table is from or NULL if there is none. */
563 struct dwo_unit *dwo_unit;
564
565 /* Offset in .debug_line or .debug_line.dwo. */
566 sect_offset line_sect_off;
567 };
568
569 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
570 an object of this type. */
571
572 struct type_unit_group
573 {
574 /* dwarf2read.c's main "handle" on a TU symtab.
575 To simplify things we create an artificial CU that "includes" all the
576 type units using this stmt_list so that the rest of the code still has
577 a "per_cu" handle on the symtab. */
578 struct dwarf2_per_cu_data per_cu;
579
580 /* The TUs that share this DW_AT_stmt_list entry.
581 This is added to while parsing type units to build partial symtabs,
582 and is deleted afterwards and not used again. */
583 std::vector<signatured_type *> *tus;
584
585 /* The compunit symtab.
586 Type units in a group needn't all be defined in the same source file,
587 so we create an essentially anonymous symtab as the compunit symtab. */
588 struct compunit_symtab *compunit_symtab;
589
590 /* The data used to construct the hash key. */
591 struct stmt_list_hash hash;
592
593 /* The symbol tables for this TU (obtained from the files listed in
594 DW_AT_stmt_list).
595 WARNING: The order of entries here must match the order of entries
596 in the line header. After the first TU using this type_unit_group, the
597 line header for the subsequent TUs is recreated from this. This is done
598 because we need to use the same symtabs for each TU using the same
599 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
600 there's no guarantee the line header doesn't have duplicate entries. */
601 struct symtab **symtabs;
602 };
603
604 /* These sections are what may appear in a (real or virtual) DWO file. */
605
606 struct dwo_sections
607 {
608 struct dwarf2_section_info abbrev;
609 struct dwarf2_section_info line;
610 struct dwarf2_section_info loc;
611 struct dwarf2_section_info loclists;
612 struct dwarf2_section_info macinfo;
613 struct dwarf2_section_info macro;
614 struct dwarf2_section_info str;
615 struct dwarf2_section_info str_offsets;
616 /* In the case of a virtual DWO file, these two are unused. */
617 struct dwarf2_section_info info;
618 std::vector<dwarf2_section_info> types;
619 };
620
621 /* CUs/TUs in DWP/DWO files. */
622
623 struct dwo_unit
624 {
625 /* Backlink to the containing struct dwo_file. */
626 struct dwo_file *dwo_file;
627
628 /* The "id" that distinguishes this CU/TU.
629 .debug_info calls this "dwo_id", .debug_types calls this "signature".
630 Since signatures came first, we stick with it for consistency. */
631 ULONGEST signature;
632
633 /* The section this CU/TU lives in, in the DWO file. */
634 struct dwarf2_section_info *section;
635
636 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
637 sect_offset sect_off;
638 unsigned int length;
639
640 /* For types, offset in the type's DIE of the type defined by this TU. */
641 cu_offset type_offset_in_tu;
642 };
643
644 /* include/dwarf2.h defines the DWP section codes.
645 It defines a max value but it doesn't define a min value, which we
646 use for error checking, so provide one. */
647
648 enum dwp_v2_section_ids
649 {
650 DW_SECT_MIN = 1
651 };
652
653 /* Data for one DWO file.
654
655 This includes virtual DWO files (a virtual DWO file is a DWO file as it
656 appears in a DWP file). DWP files don't really have DWO files per se -
657 comdat folding of types "loses" the DWO file they came from, and from
658 a high level view DWP files appear to contain a mass of random types.
659 However, to maintain consistency with the non-DWP case we pretend DWP
660 files contain virtual DWO files, and we assign each TU with one virtual
661 DWO file (generally based on the line and abbrev section offsets -
662 a heuristic that seems to work in practice). */
663
664 struct dwo_file
665 {
666 dwo_file () = default;
667 DISABLE_COPY_AND_ASSIGN (dwo_file);
668
669 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
670 For virtual DWO files the name is constructed from the section offsets
671 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
672 from related CU+TUs. */
673 const char *dwo_name = nullptr;
674
675 /* The DW_AT_comp_dir attribute. */
676 const char *comp_dir = nullptr;
677
678 /* The bfd, when the file is open. Otherwise this is NULL.
679 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
680 gdb_bfd_ref_ptr dbfd;
681
682 /* The sections that make up this DWO file.
683 Remember that for virtual DWO files in DWP V2, these are virtual
684 sections (for lack of a better name). */
685 struct dwo_sections sections {};
686
687 /* The CUs in the file.
688 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
689 an extension to handle LLVM's Link Time Optimization output (where
690 multiple source files may be compiled into a single object/dwo pair). */
691 htab_up cus;
692
693 /* Table of TUs in the file.
694 Each element is a struct dwo_unit. */
695 htab_up tus;
696 };
697
698 /* These sections are what may appear in a DWP file. */
699
700 struct dwp_sections
701 {
702 /* These are used by both DWP version 1 and 2. */
703 struct dwarf2_section_info str;
704 struct dwarf2_section_info cu_index;
705 struct dwarf2_section_info tu_index;
706
707 /* These are only used by DWP version 2 files.
708 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
709 sections are referenced by section number, and are not recorded here.
710 In DWP version 2 there is at most one copy of all these sections, each
711 section being (effectively) comprised of the concatenation of all of the
712 individual sections that exist in the version 1 format.
713 To keep the code simple we treat each of these concatenated pieces as a
714 section itself (a virtual section?). */
715 struct dwarf2_section_info abbrev;
716 struct dwarf2_section_info info;
717 struct dwarf2_section_info line;
718 struct dwarf2_section_info loc;
719 struct dwarf2_section_info macinfo;
720 struct dwarf2_section_info macro;
721 struct dwarf2_section_info str_offsets;
722 struct dwarf2_section_info types;
723 };
724
725 /* These sections are what may appear in a virtual DWO file in DWP version 1.
726 A virtual DWO file is a DWO file as it appears in a DWP file. */
727
728 struct virtual_v1_dwo_sections
729 {
730 struct dwarf2_section_info abbrev;
731 struct dwarf2_section_info line;
732 struct dwarf2_section_info loc;
733 struct dwarf2_section_info macinfo;
734 struct dwarf2_section_info macro;
735 struct dwarf2_section_info str_offsets;
736 /* Each DWP hash table entry records one CU or one TU.
737 That is recorded here, and copied to dwo_unit.section. */
738 struct dwarf2_section_info info_or_types;
739 };
740
741 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
742 In version 2, the sections of the DWO files are concatenated together
743 and stored in one section of that name. Thus each ELF section contains
744 several "virtual" sections. */
745
746 struct virtual_v2_dwo_sections
747 {
748 bfd_size_type abbrev_offset;
749 bfd_size_type abbrev_size;
750
751 bfd_size_type line_offset;
752 bfd_size_type line_size;
753
754 bfd_size_type loc_offset;
755 bfd_size_type loc_size;
756
757 bfd_size_type macinfo_offset;
758 bfd_size_type macinfo_size;
759
760 bfd_size_type macro_offset;
761 bfd_size_type macro_size;
762
763 bfd_size_type str_offsets_offset;
764 bfd_size_type str_offsets_size;
765
766 /* Each DWP hash table entry records one CU or one TU.
767 That is recorded here, and copied to dwo_unit.section. */
768 bfd_size_type info_or_types_offset;
769 bfd_size_type info_or_types_size;
770 };
771
772 /* Contents of DWP hash tables. */
773
774 struct dwp_hash_table
775 {
776 uint32_t version, nr_columns;
777 uint32_t nr_units, nr_slots;
778 const gdb_byte *hash_table, *unit_table;
779 union
780 {
781 struct
782 {
783 const gdb_byte *indices;
784 } v1;
785 struct
786 {
787 /* This is indexed by column number and gives the id of the section
788 in that column. */
789 #define MAX_NR_V2_DWO_SECTIONS \
790 (1 /* .debug_info or .debug_types */ \
791 + 1 /* .debug_abbrev */ \
792 + 1 /* .debug_line */ \
793 + 1 /* .debug_loc */ \
794 + 1 /* .debug_str_offsets */ \
795 + 1 /* .debug_macro or .debug_macinfo */)
796 int section_ids[MAX_NR_V2_DWO_SECTIONS];
797 const gdb_byte *offsets;
798 const gdb_byte *sizes;
799 } v2;
800 } section_pool;
801 };
802
803 /* Data for one DWP file. */
804
805 struct dwp_file
806 {
807 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
808 : name (name_),
809 dbfd (std::move (abfd))
810 {
811 }
812
813 /* Name of the file. */
814 const char *name;
815
816 /* File format version. */
817 int version = 0;
818
819 /* The bfd. */
820 gdb_bfd_ref_ptr dbfd;
821
822 /* Section info for this file. */
823 struct dwp_sections sections {};
824
825 /* Table of CUs in the file. */
826 const struct dwp_hash_table *cus = nullptr;
827
828 /* Table of TUs in the file. */
829 const struct dwp_hash_table *tus = nullptr;
830
831 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
832 htab_up loaded_cus;
833 htab_up loaded_tus;
834
835 /* Table to map ELF section numbers to their sections.
836 This is only needed for the DWP V1 file format. */
837 unsigned int num_sections = 0;
838 asection **elf_sections = nullptr;
839 };
840
841 /* Struct used to pass misc. parameters to read_die_and_children, et
842 al. which are used for both .debug_info and .debug_types dies.
843 All parameters here are unchanging for the life of the call. This
844 struct exists to abstract away the constant parameters of die reading. */
845
846 struct die_reader_specs
847 {
848 /* The bfd of die_section. */
849 bfd* abfd;
850
851 /* The CU of the DIE we are parsing. */
852 struct dwarf2_cu *cu;
853
854 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
855 struct dwo_file *dwo_file;
856
857 /* The section the die comes from.
858 This is either .debug_info or .debug_types, or the .dwo variants. */
859 struct dwarf2_section_info *die_section;
860
861 /* die_section->buffer. */
862 const gdb_byte *buffer;
863
864 /* The end of the buffer. */
865 const gdb_byte *buffer_end;
866
867 /* The abbreviation table to use when reading the DIEs. */
868 struct abbrev_table *abbrev_table;
869 };
870
871 /* A subclass of die_reader_specs that holds storage and has complex
872 constructor and destructor behavior. */
873
874 class cutu_reader : public die_reader_specs
875 {
876 public:
877
878 cutu_reader (struct dwarf2_per_cu_data *this_cu,
879 struct abbrev_table *abbrev_table,
880 int use_existing_cu,
881 bool skip_partial);
882
883 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
884 struct dwarf2_cu *parent_cu = nullptr,
885 struct dwo_file *dwo_file = nullptr);
886
887 DISABLE_COPY_AND_ASSIGN (cutu_reader);
888
889 const gdb_byte *info_ptr = nullptr;
890 struct die_info *comp_unit_die = nullptr;
891 bool dummy_p = false;
892
893 /* Release the new CU, putting it on the chain. This cannot be done
894 for dummy CUs. */
895 void keep ();
896
897 private:
898 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
899 int use_existing_cu);
900
901 struct dwarf2_per_cu_data *m_this_cu;
902 std::unique_ptr<dwarf2_cu> m_new_cu;
903
904 /* The ordinary abbreviation table. */
905 abbrev_table_up m_abbrev_table_holder;
906
907 /* The DWO abbreviation table. */
908 abbrev_table_up m_dwo_abbrev_table;
909 };
910
911 /* When we construct a partial symbol table entry we only
912 need this much information. */
913 struct partial_die_info : public allocate_on_obstack
914 {
915 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
916
917 /* Disable assign but still keep copy ctor, which is needed
918 load_partial_dies. */
919 partial_die_info& operator=(const partial_die_info& rhs) = delete;
920
921 /* Adjust the partial die before generating a symbol for it. This
922 function may set the is_external flag or change the DIE's
923 name. */
924 void fixup (struct dwarf2_cu *cu);
925
926 /* Read a minimal amount of information into the minimal die
927 structure. */
928 const gdb_byte *read (const struct die_reader_specs *reader,
929 const struct abbrev_info &abbrev,
930 const gdb_byte *info_ptr);
931
932 /* Offset of this DIE. */
933 const sect_offset sect_off;
934
935 /* DWARF-2 tag for this DIE. */
936 const ENUM_BITFIELD(dwarf_tag) tag : 16;
937
938 /* Assorted flags describing the data found in this DIE. */
939 const unsigned int has_children : 1;
940
941 unsigned int is_external : 1;
942 unsigned int is_declaration : 1;
943 unsigned int has_type : 1;
944 unsigned int has_specification : 1;
945 unsigned int has_pc_info : 1;
946 unsigned int may_be_inlined : 1;
947
948 /* This DIE has been marked DW_AT_main_subprogram. */
949 unsigned int main_subprogram : 1;
950
951 /* Flag set if the SCOPE field of this structure has been
952 computed. */
953 unsigned int scope_set : 1;
954
955 /* Flag set if the DIE has a byte_size attribute. */
956 unsigned int has_byte_size : 1;
957
958 /* Flag set if the DIE has a DW_AT_const_value attribute. */
959 unsigned int has_const_value : 1;
960
961 /* Flag set if any of the DIE's children are template arguments. */
962 unsigned int has_template_arguments : 1;
963
964 /* Flag set if fixup has been called on this die. */
965 unsigned int fixup_called : 1;
966
967 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
968 unsigned int is_dwz : 1;
969
970 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
971 unsigned int spec_is_dwz : 1;
972
973 /* The name of this DIE. Normally the value of DW_AT_name, but
974 sometimes a default name for unnamed DIEs. */
975 const char *name = nullptr;
976
977 /* The linkage name, if present. */
978 const char *linkage_name = nullptr;
979
980 /* The scope to prepend to our children. This is generally
981 allocated on the comp_unit_obstack, so will disappear
982 when this compilation unit leaves the cache. */
983 const char *scope = nullptr;
984
985 /* Some data associated with the partial DIE. The tag determines
986 which field is live. */
987 union
988 {
989 /* The location description associated with this DIE, if any. */
990 struct dwarf_block *locdesc;
991 /* The offset of an import, for DW_TAG_imported_unit. */
992 sect_offset sect_off;
993 } d {};
994
995 /* If HAS_PC_INFO, the PC range associated with this DIE. */
996 CORE_ADDR lowpc = 0;
997 CORE_ADDR highpc = 0;
998
999 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1000 DW_AT_sibling, if any. */
1001 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1002 could return DW_AT_sibling values to its caller load_partial_dies. */
1003 const gdb_byte *sibling = nullptr;
1004
1005 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1006 DW_AT_specification (or DW_AT_abstract_origin or
1007 DW_AT_extension). */
1008 sect_offset spec_offset {};
1009
1010 /* Pointers to this DIE's parent, first child, and next sibling,
1011 if any. */
1012 struct partial_die_info *die_parent = nullptr;
1013 struct partial_die_info *die_child = nullptr;
1014 struct partial_die_info *die_sibling = nullptr;
1015
1016 friend struct partial_die_info *
1017 dwarf2_cu::find_partial_die (sect_offset sect_off);
1018
1019 private:
1020 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1021 partial_die_info (sect_offset sect_off)
1022 : partial_die_info (sect_off, DW_TAG_padding, 0)
1023 {
1024 }
1025
1026 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1027 int has_children_)
1028 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1029 {
1030 is_external = 0;
1031 is_declaration = 0;
1032 has_type = 0;
1033 has_specification = 0;
1034 has_pc_info = 0;
1035 may_be_inlined = 0;
1036 main_subprogram = 0;
1037 scope_set = 0;
1038 has_byte_size = 0;
1039 has_const_value = 0;
1040 has_template_arguments = 0;
1041 fixup_called = 0;
1042 is_dwz = 0;
1043 spec_is_dwz = 0;
1044 }
1045 };
1046
1047 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1048 but this would require a corresponding change in unpack_field_as_long
1049 and friends. */
1050 static int bits_per_byte = 8;
1051
1052 /* When reading a variant or variant part, we track a bit more
1053 information about the field, and store it in an object of this
1054 type. */
1055
1056 struct variant_field
1057 {
1058 /* If we see a DW_TAG_variant, then this will be the discriminant
1059 value. */
1060 ULONGEST discriminant_value;
1061 /* If we see a DW_TAG_variant, then this will be set if this is the
1062 default branch. */
1063 bool default_branch;
1064 /* While reading a DW_TAG_variant_part, this will be set if this
1065 field is the discriminant. */
1066 bool is_discriminant;
1067 };
1068
1069 struct nextfield
1070 {
1071 int accessibility = 0;
1072 int virtuality = 0;
1073 /* Extra information to describe a variant or variant part. */
1074 struct variant_field variant {};
1075 struct field field {};
1076 };
1077
1078 struct fnfieldlist
1079 {
1080 const char *name = nullptr;
1081 std::vector<struct fn_field> fnfields;
1082 };
1083
1084 /* The routines that read and process dies for a C struct or C++ class
1085 pass lists of data member fields and lists of member function fields
1086 in an instance of a field_info structure, as defined below. */
1087 struct field_info
1088 {
1089 /* List of data member and baseclasses fields. */
1090 std::vector<struct nextfield> fields;
1091 std::vector<struct nextfield> baseclasses;
1092
1093 /* Set if the accessibility of one of the fields is not public. */
1094 int non_public_fields = 0;
1095
1096 /* Member function fieldlist array, contains name of possibly overloaded
1097 member function, number of overloaded member functions and a pointer
1098 to the head of the member function field chain. */
1099 std::vector<struct fnfieldlist> fnfieldlists;
1100
1101 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1102 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1103 std::vector<struct decl_field> typedef_field_list;
1104
1105 /* Nested types defined by this class and the number of elements in this
1106 list. */
1107 std::vector<struct decl_field> nested_types_list;
1108
1109 /* Return the total number of fields (including baseclasses). */
1110 int nfields () const
1111 {
1112 return fields.size () + baseclasses.size ();
1113 }
1114 };
1115
1116 /* Loaded secondary compilation units are kept in memory until they
1117 have not been referenced for the processing of this many
1118 compilation units. Set this to zero to disable caching. Cache
1119 sizes of up to at least twenty will improve startup time for
1120 typical inter-CU-reference binaries, at an obvious memory cost. */
1121 static int dwarf_max_cache_age = 5;
1122 static void
1123 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1124 struct cmd_list_element *c, const char *value)
1125 {
1126 fprintf_filtered (file, _("The upper bound on the age of cached "
1127 "DWARF compilation units is %s.\n"),
1128 value);
1129 }
1130 \f
1131 /* local function prototypes */
1132
1133 static void dwarf2_find_base_address (struct die_info *die,
1134 struct dwarf2_cu *cu);
1135
1136 static dwarf2_psymtab *create_partial_symtab
1137 (struct dwarf2_per_cu_data *per_cu, const char *name);
1138
1139 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1140 const gdb_byte *info_ptr,
1141 struct die_info *type_unit_die);
1142
1143 static void dwarf2_build_psymtabs_hard
1144 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1145
1146 static void scan_partial_symbols (struct partial_die_info *,
1147 CORE_ADDR *, CORE_ADDR *,
1148 int, struct dwarf2_cu *);
1149
1150 static void add_partial_symbol (struct partial_die_info *,
1151 struct dwarf2_cu *);
1152
1153 static void add_partial_namespace (struct partial_die_info *pdi,
1154 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1155 int set_addrmap, struct dwarf2_cu *cu);
1156
1157 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1158 CORE_ADDR *highpc, int set_addrmap,
1159 struct dwarf2_cu *cu);
1160
1161 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1162 struct dwarf2_cu *cu);
1163
1164 static void add_partial_subprogram (struct partial_die_info *pdi,
1165 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1166 int need_pc, struct dwarf2_cu *cu);
1167
1168 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1169
1170 static struct partial_die_info *load_partial_dies
1171 (const struct die_reader_specs *, const gdb_byte *, int);
1172
1173 /* A pair of partial_die_info and compilation unit. */
1174 struct cu_partial_die_info
1175 {
1176 /* The compilation unit of the partial_die_info. */
1177 struct dwarf2_cu *cu;
1178 /* A partial_die_info. */
1179 struct partial_die_info *pdi;
1180
1181 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1182 : cu (cu),
1183 pdi (pdi)
1184 { /* Nothing. */ }
1185
1186 private:
1187 cu_partial_die_info () = delete;
1188 };
1189
1190 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1191 struct dwarf2_cu *);
1192
1193 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1194 struct attribute *, struct attr_abbrev *,
1195 const gdb_byte *, bool *need_reprocess);
1196
1197 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1198 struct attribute *attr);
1199
1200 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1201
1202 static sect_offset read_abbrev_offset
1203 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1204 struct dwarf2_section_info *, sect_offset);
1205
1206 static const char *read_indirect_string
1207 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1208 const struct comp_unit_head *, unsigned int *);
1209
1210 static const char *read_indirect_string_at_offset
1211 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1212
1213 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1214 const gdb_byte *,
1215 unsigned int *);
1216
1217 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1218 ULONGEST str_index);
1219
1220 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1221 ULONGEST str_index);
1222
1223 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1224
1225 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1226 struct dwarf2_cu *);
1227
1228 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1229 struct dwarf2_cu *cu);
1230
1231 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1232
1233 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1234 struct dwarf2_cu *cu);
1235
1236 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1237
1238 static struct die_info *die_specification (struct die_info *die,
1239 struct dwarf2_cu **);
1240
1241 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1242 struct dwarf2_cu *cu);
1243
1244 static void dwarf_decode_lines (struct line_header *, const char *,
1245 struct dwarf2_cu *, dwarf2_psymtab *,
1246 CORE_ADDR, int decode_mapping);
1247
1248 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1249 const char *);
1250
1251 static struct symbol *new_symbol (struct die_info *, struct type *,
1252 struct dwarf2_cu *, struct symbol * = NULL);
1253
1254 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1255 struct dwarf2_cu *);
1256
1257 static void dwarf2_const_value_attr (const struct attribute *attr,
1258 struct type *type,
1259 const char *name,
1260 struct obstack *obstack,
1261 struct dwarf2_cu *cu, LONGEST *value,
1262 const gdb_byte **bytes,
1263 struct dwarf2_locexpr_baton **baton);
1264
1265 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1266
1267 static int need_gnat_info (struct dwarf2_cu *);
1268
1269 static struct type *die_descriptive_type (struct die_info *,
1270 struct dwarf2_cu *);
1271
1272 static void set_descriptive_type (struct type *, struct die_info *,
1273 struct dwarf2_cu *);
1274
1275 static struct type *die_containing_type (struct die_info *,
1276 struct dwarf2_cu *);
1277
1278 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1279 struct dwarf2_cu *);
1280
1281 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1282
1283 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1284
1285 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1286
1287 static char *typename_concat (struct obstack *obs, const char *prefix,
1288 const char *suffix, int physname,
1289 struct dwarf2_cu *cu);
1290
1291 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1292
1293 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1294
1295 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1296
1297 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1298
1299 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1300
1301 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1302
1303 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1304 struct dwarf2_cu *, dwarf2_psymtab *);
1305
1306 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1307 values. Keep the items ordered with increasing constraints compliance. */
1308 enum pc_bounds_kind
1309 {
1310 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1311 PC_BOUNDS_NOT_PRESENT,
1312
1313 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1314 were present but they do not form a valid range of PC addresses. */
1315 PC_BOUNDS_INVALID,
1316
1317 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1318 PC_BOUNDS_RANGES,
1319
1320 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1321 PC_BOUNDS_HIGH_LOW,
1322 };
1323
1324 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1325 CORE_ADDR *, CORE_ADDR *,
1326 struct dwarf2_cu *,
1327 dwarf2_psymtab *);
1328
1329 static void get_scope_pc_bounds (struct die_info *,
1330 CORE_ADDR *, CORE_ADDR *,
1331 struct dwarf2_cu *);
1332
1333 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1334 CORE_ADDR, struct dwarf2_cu *);
1335
1336 static void dwarf2_add_field (struct field_info *, struct die_info *,
1337 struct dwarf2_cu *);
1338
1339 static void dwarf2_attach_fields_to_type (struct field_info *,
1340 struct type *, struct dwarf2_cu *);
1341
1342 static void dwarf2_add_member_fn (struct field_info *,
1343 struct die_info *, struct type *,
1344 struct dwarf2_cu *);
1345
1346 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1347 struct type *,
1348 struct dwarf2_cu *);
1349
1350 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1351
1352 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1353
1354 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1355
1356 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1357
1358 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1359
1360 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1361
1362 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1363
1364 static struct type *read_module_type (struct die_info *die,
1365 struct dwarf2_cu *cu);
1366
1367 static const char *namespace_name (struct die_info *die,
1368 int *is_anonymous, struct dwarf2_cu *);
1369
1370 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1371
1372 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1373
1374 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1375 struct dwarf2_cu *);
1376
1377 static struct die_info *read_die_and_siblings_1
1378 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1379 struct die_info *);
1380
1381 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1382 const gdb_byte *info_ptr,
1383 const gdb_byte **new_info_ptr,
1384 struct die_info *parent);
1385
1386 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1387 struct die_info **, const gdb_byte *,
1388 int);
1389
1390 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1391 struct die_info **, const gdb_byte *);
1392
1393 static void process_die (struct die_info *, struct dwarf2_cu *);
1394
1395 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1396 struct objfile *);
1397
1398 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1399
1400 static const char *dwarf2_full_name (const char *name,
1401 struct die_info *die,
1402 struct dwarf2_cu *cu);
1403
1404 static const char *dwarf2_physname (const char *name, struct die_info *die,
1405 struct dwarf2_cu *cu);
1406
1407 static struct die_info *dwarf2_extension (struct die_info *die,
1408 struct dwarf2_cu **);
1409
1410 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1411
1412 static void dump_die_for_error (struct die_info *);
1413
1414 static void dump_die_1 (struct ui_file *, int level, int max_level,
1415 struct die_info *);
1416
1417 /*static*/ void dump_die (struct die_info *, int max_level);
1418
1419 static void store_in_ref_table (struct die_info *,
1420 struct dwarf2_cu *);
1421
1422 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1423 const struct attribute *,
1424 struct dwarf2_cu **);
1425
1426 static struct die_info *follow_die_ref (struct die_info *,
1427 const struct attribute *,
1428 struct dwarf2_cu **);
1429
1430 static struct die_info *follow_die_sig (struct die_info *,
1431 const struct attribute *,
1432 struct dwarf2_cu **);
1433
1434 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1435 struct dwarf2_cu *);
1436
1437 static struct type *get_DW_AT_signature_type (struct die_info *,
1438 const struct attribute *,
1439 struct dwarf2_cu *);
1440
1441 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1442
1443 static void read_signatured_type (struct signatured_type *);
1444
1445 static int attr_to_dynamic_prop (const struct attribute *attr,
1446 struct die_info *die, struct dwarf2_cu *cu,
1447 struct dynamic_prop *prop, struct type *type);
1448
1449 /* memory allocation interface */
1450
1451 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1452
1453 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1454
1455 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1456
1457 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1458 struct dwarf2_loclist_baton *baton,
1459 const struct attribute *attr);
1460
1461 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1462 struct symbol *sym,
1463 struct dwarf2_cu *cu,
1464 int is_block);
1465
1466 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1467 const gdb_byte *info_ptr,
1468 struct abbrev_info *abbrev);
1469
1470 static hashval_t partial_die_hash (const void *item);
1471
1472 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1473
1474 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1475 (sect_offset sect_off, unsigned int offset_in_dwz,
1476 struct dwarf2_per_objfile *dwarf2_per_objfile);
1477
1478 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1479 struct die_info *comp_unit_die,
1480 enum language pretend_language);
1481
1482 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1483
1484 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1485
1486 static struct type *set_die_type (struct die_info *, struct type *,
1487 struct dwarf2_cu *);
1488
1489 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1490
1491 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1492
1493 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1494 enum language);
1495
1496 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1497 enum language);
1498
1499 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1500 enum language);
1501
1502 static void dwarf2_add_dependence (struct dwarf2_cu *,
1503 struct dwarf2_per_cu_data *);
1504
1505 static void dwarf2_mark (struct dwarf2_cu *);
1506
1507 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1508
1509 static struct type *get_die_type_at_offset (sect_offset,
1510 struct dwarf2_per_cu_data *);
1511
1512 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1513
1514 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1515 enum language pretend_language);
1516
1517 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1518
1519 /* Class, the destructor of which frees all allocated queue entries. This
1520 will only have work to do if an error was thrown while processing the
1521 dwarf. If no error was thrown then the queue entries should have all
1522 been processed, and freed, as we went along. */
1523
1524 class dwarf2_queue_guard
1525 {
1526 public:
1527 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1528 : m_per_objfile (per_objfile)
1529 {
1530 }
1531
1532 /* Free any entries remaining on the queue. There should only be
1533 entries left if we hit an error while processing the dwarf. */
1534 ~dwarf2_queue_guard ()
1535 {
1536 /* Ensure that no memory is allocated by the queue. */
1537 std::queue<dwarf2_queue_item> empty;
1538 std::swap (m_per_objfile->queue, empty);
1539 }
1540
1541 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1542
1543 private:
1544 dwarf2_per_objfile *m_per_objfile;
1545 };
1546
1547 dwarf2_queue_item::~dwarf2_queue_item ()
1548 {
1549 /* Anything still marked queued is likely to be in an
1550 inconsistent state, so discard it. */
1551 if (per_cu->queued)
1552 {
1553 if (per_cu->cu != NULL)
1554 free_one_cached_comp_unit (per_cu);
1555 per_cu->queued = 0;
1556 }
1557 }
1558
1559 /* The return type of find_file_and_directory. Note, the enclosed
1560 string pointers are only valid while this object is valid. */
1561
1562 struct file_and_directory
1563 {
1564 /* The filename. This is never NULL. */
1565 const char *name;
1566
1567 /* The compilation directory. NULL if not known. If we needed to
1568 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1569 points directly to the DW_AT_comp_dir string attribute owned by
1570 the obstack that owns the DIE. */
1571 const char *comp_dir;
1572
1573 /* If we needed to build a new string for comp_dir, this is what
1574 owns the storage. */
1575 std::string comp_dir_storage;
1576 };
1577
1578 static file_and_directory find_file_and_directory (struct die_info *die,
1579 struct dwarf2_cu *cu);
1580
1581 static htab_up allocate_signatured_type_table ();
1582
1583 static htab_up allocate_dwo_unit_table ();
1584
1585 static struct dwo_unit *lookup_dwo_unit_in_dwp
1586 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1587 struct dwp_file *dwp_file, const char *comp_dir,
1588 ULONGEST signature, int is_debug_types);
1589
1590 static struct dwp_file *get_dwp_file
1591 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1592
1593 static struct dwo_unit *lookup_dwo_comp_unit
1594 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1595
1596 static struct dwo_unit *lookup_dwo_type_unit
1597 (struct signatured_type *, const char *, const char *);
1598
1599 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1600
1601 /* A unique pointer to a dwo_file. */
1602
1603 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1604
1605 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1606
1607 static void check_producer (struct dwarf2_cu *cu);
1608
1609 static void free_line_header_voidp (void *arg);
1610 \f
1611 /* Various complaints about symbol reading that don't abort the process. */
1612
1613 static void
1614 dwarf2_debug_line_missing_file_complaint (void)
1615 {
1616 complaint (_(".debug_line section has line data without a file"));
1617 }
1618
1619 static void
1620 dwarf2_debug_line_missing_end_sequence_complaint (void)
1621 {
1622 complaint (_(".debug_line section has line "
1623 "program sequence without an end"));
1624 }
1625
1626 static void
1627 dwarf2_complex_location_expr_complaint (void)
1628 {
1629 complaint (_("location expression too complex"));
1630 }
1631
1632 static void
1633 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1634 int arg3)
1635 {
1636 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1637 arg1, arg2, arg3);
1638 }
1639
1640 static void
1641 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1642 {
1643 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1644 arg1, arg2);
1645 }
1646
1647 /* Hash function for line_header_hash. */
1648
1649 static hashval_t
1650 line_header_hash (const struct line_header *ofs)
1651 {
1652 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1653 }
1654
1655 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1656
1657 static hashval_t
1658 line_header_hash_voidp (const void *item)
1659 {
1660 const struct line_header *ofs = (const struct line_header *) item;
1661
1662 return line_header_hash (ofs);
1663 }
1664
1665 /* Equality function for line_header_hash. */
1666
1667 static int
1668 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1669 {
1670 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1671 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1672
1673 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1674 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1675 }
1676
1677 \f
1678
1679 /* See declaration. */
1680
1681 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1682 const dwarf2_debug_sections *names,
1683 bool can_copy_)
1684 : objfile (objfile_),
1685 can_copy (can_copy_)
1686 {
1687 if (names == NULL)
1688 names = &dwarf2_elf_names;
1689
1690 bfd *obfd = objfile->obfd;
1691
1692 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1693 locate_sections (obfd, sec, *names);
1694 }
1695
1696 dwarf2_per_objfile::~dwarf2_per_objfile ()
1697 {
1698 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1699 free_cached_comp_units ();
1700
1701 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1702 per_cu->imported_symtabs_free ();
1703
1704 for (signatured_type *sig_type : all_type_units)
1705 sig_type->per_cu.imported_symtabs_free ();
1706
1707 /* Everything else should be on the objfile obstack. */
1708 }
1709
1710 /* See declaration. */
1711
1712 void
1713 dwarf2_per_objfile::free_cached_comp_units ()
1714 {
1715 dwarf2_per_cu_data *per_cu = read_in_chain;
1716 dwarf2_per_cu_data **last_chain = &read_in_chain;
1717 while (per_cu != NULL)
1718 {
1719 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1720
1721 delete per_cu->cu;
1722 *last_chain = next_cu;
1723 per_cu = next_cu;
1724 }
1725 }
1726
1727 /* A helper class that calls free_cached_comp_units on
1728 destruction. */
1729
1730 class free_cached_comp_units
1731 {
1732 public:
1733
1734 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1735 : m_per_objfile (per_objfile)
1736 {
1737 }
1738
1739 ~free_cached_comp_units ()
1740 {
1741 m_per_objfile->free_cached_comp_units ();
1742 }
1743
1744 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1745
1746 private:
1747
1748 dwarf2_per_objfile *m_per_objfile;
1749 };
1750
1751 /* Try to locate the sections we need for DWARF 2 debugging
1752 information and return true if we have enough to do something.
1753 NAMES points to the dwarf2 section names, or is NULL if the standard
1754 ELF names are used. CAN_COPY is true for formats where symbol
1755 interposition is possible and so symbol values must follow copy
1756 relocation rules. */
1757
1758 int
1759 dwarf2_has_info (struct objfile *objfile,
1760 const struct dwarf2_debug_sections *names,
1761 bool can_copy)
1762 {
1763 if (objfile->flags & OBJF_READNEVER)
1764 return 0;
1765
1766 struct dwarf2_per_objfile *dwarf2_per_objfile
1767 = get_dwarf2_per_objfile (objfile);
1768
1769 if (dwarf2_per_objfile == NULL)
1770 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1771 names,
1772 can_copy);
1773
1774 return (!dwarf2_per_objfile->info.is_virtual
1775 && dwarf2_per_objfile->info.s.section != NULL
1776 && !dwarf2_per_objfile->abbrev.is_virtual
1777 && dwarf2_per_objfile->abbrev.s.section != NULL);
1778 }
1779
1780 /* When loading sections, we look either for uncompressed section or for
1781 compressed section names. */
1782
1783 static int
1784 section_is_p (const char *section_name,
1785 const struct dwarf2_section_names *names)
1786 {
1787 if (names->normal != NULL
1788 && strcmp (section_name, names->normal) == 0)
1789 return 1;
1790 if (names->compressed != NULL
1791 && strcmp (section_name, names->compressed) == 0)
1792 return 1;
1793 return 0;
1794 }
1795
1796 /* See declaration. */
1797
1798 void
1799 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1800 const dwarf2_debug_sections &names)
1801 {
1802 flagword aflag = bfd_section_flags (sectp);
1803
1804 if ((aflag & SEC_HAS_CONTENTS) == 0)
1805 {
1806 }
1807 else if (elf_section_data (sectp)->this_hdr.sh_size
1808 > bfd_get_file_size (abfd))
1809 {
1810 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1811 warning (_("Discarding section %s which has a section size (%s"
1812 ") larger than the file size [in module %s]"),
1813 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1814 bfd_get_filename (abfd));
1815 }
1816 else if (section_is_p (sectp->name, &names.info))
1817 {
1818 this->info.s.section = sectp;
1819 this->info.size = bfd_section_size (sectp);
1820 }
1821 else if (section_is_p (sectp->name, &names.abbrev))
1822 {
1823 this->abbrev.s.section = sectp;
1824 this->abbrev.size = bfd_section_size (sectp);
1825 }
1826 else if (section_is_p (sectp->name, &names.line))
1827 {
1828 this->line.s.section = sectp;
1829 this->line.size = bfd_section_size (sectp);
1830 }
1831 else if (section_is_p (sectp->name, &names.loc))
1832 {
1833 this->loc.s.section = sectp;
1834 this->loc.size = bfd_section_size (sectp);
1835 }
1836 else if (section_is_p (sectp->name, &names.loclists))
1837 {
1838 this->loclists.s.section = sectp;
1839 this->loclists.size = bfd_section_size (sectp);
1840 }
1841 else if (section_is_p (sectp->name, &names.macinfo))
1842 {
1843 this->macinfo.s.section = sectp;
1844 this->macinfo.size = bfd_section_size (sectp);
1845 }
1846 else if (section_is_p (sectp->name, &names.macro))
1847 {
1848 this->macro.s.section = sectp;
1849 this->macro.size = bfd_section_size (sectp);
1850 }
1851 else if (section_is_p (sectp->name, &names.str))
1852 {
1853 this->str.s.section = sectp;
1854 this->str.size = bfd_section_size (sectp);
1855 }
1856 else if (section_is_p (sectp->name, &names.str_offsets))
1857 {
1858 this->str_offsets.s.section = sectp;
1859 this->str_offsets.size = bfd_section_size (sectp);
1860 }
1861 else if (section_is_p (sectp->name, &names.line_str))
1862 {
1863 this->line_str.s.section = sectp;
1864 this->line_str.size = bfd_section_size (sectp);
1865 }
1866 else if (section_is_p (sectp->name, &names.addr))
1867 {
1868 this->addr.s.section = sectp;
1869 this->addr.size = bfd_section_size (sectp);
1870 }
1871 else if (section_is_p (sectp->name, &names.frame))
1872 {
1873 this->frame.s.section = sectp;
1874 this->frame.size = bfd_section_size (sectp);
1875 }
1876 else if (section_is_p (sectp->name, &names.eh_frame))
1877 {
1878 this->eh_frame.s.section = sectp;
1879 this->eh_frame.size = bfd_section_size (sectp);
1880 }
1881 else if (section_is_p (sectp->name, &names.ranges))
1882 {
1883 this->ranges.s.section = sectp;
1884 this->ranges.size = bfd_section_size (sectp);
1885 }
1886 else if (section_is_p (sectp->name, &names.rnglists))
1887 {
1888 this->rnglists.s.section = sectp;
1889 this->rnglists.size = bfd_section_size (sectp);
1890 }
1891 else if (section_is_p (sectp->name, &names.types))
1892 {
1893 struct dwarf2_section_info type_section;
1894
1895 memset (&type_section, 0, sizeof (type_section));
1896 type_section.s.section = sectp;
1897 type_section.size = bfd_section_size (sectp);
1898
1899 this->types.push_back (type_section);
1900 }
1901 else if (section_is_p (sectp->name, &names.gdb_index))
1902 {
1903 this->gdb_index.s.section = sectp;
1904 this->gdb_index.size = bfd_section_size (sectp);
1905 }
1906 else if (section_is_p (sectp->name, &names.debug_names))
1907 {
1908 this->debug_names.s.section = sectp;
1909 this->debug_names.size = bfd_section_size (sectp);
1910 }
1911 else if (section_is_p (sectp->name, &names.debug_aranges))
1912 {
1913 this->debug_aranges.s.section = sectp;
1914 this->debug_aranges.size = bfd_section_size (sectp);
1915 }
1916
1917 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1918 && bfd_section_vma (sectp) == 0)
1919 this->has_section_at_zero = true;
1920 }
1921
1922 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1923 SECTION_NAME. */
1924
1925 void
1926 dwarf2_get_section_info (struct objfile *objfile,
1927 enum dwarf2_section_enum sect,
1928 asection **sectp, const gdb_byte **bufp,
1929 bfd_size_type *sizep)
1930 {
1931 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
1932 struct dwarf2_section_info *info;
1933
1934 /* We may see an objfile without any DWARF, in which case we just
1935 return nothing. */
1936 if (data == NULL)
1937 {
1938 *sectp = NULL;
1939 *bufp = NULL;
1940 *sizep = 0;
1941 return;
1942 }
1943 switch (sect)
1944 {
1945 case DWARF2_DEBUG_FRAME:
1946 info = &data->frame;
1947 break;
1948 case DWARF2_EH_FRAME:
1949 info = &data->eh_frame;
1950 break;
1951 default:
1952 gdb_assert_not_reached ("unexpected section");
1953 }
1954
1955 info->read (objfile);
1956
1957 *sectp = info->get_bfd_section ();
1958 *bufp = info->buffer;
1959 *sizep = info->size;
1960 }
1961
1962 /* A helper function to find the sections for a .dwz file. */
1963
1964 static void
1965 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1966 {
1967 struct dwz_file *dwz_file = (struct dwz_file *) arg;
1968
1969 /* Note that we only support the standard ELF names, because .dwz
1970 is ELF-only (at the time of writing). */
1971 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1972 {
1973 dwz_file->abbrev.s.section = sectp;
1974 dwz_file->abbrev.size = bfd_section_size (sectp);
1975 }
1976 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1977 {
1978 dwz_file->info.s.section = sectp;
1979 dwz_file->info.size = bfd_section_size (sectp);
1980 }
1981 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
1982 {
1983 dwz_file->str.s.section = sectp;
1984 dwz_file->str.size = bfd_section_size (sectp);
1985 }
1986 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
1987 {
1988 dwz_file->line.s.section = sectp;
1989 dwz_file->line.size = bfd_section_size (sectp);
1990 }
1991 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
1992 {
1993 dwz_file->macro.s.section = sectp;
1994 dwz_file->macro.size = bfd_section_size (sectp);
1995 }
1996 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
1997 {
1998 dwz_file->gdb_index.s.section = sectp;
1999 dwz_file->gdb_index.size = bfd_section_size (sectp);
2000 }
2001 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2002 {
2003 dwz_file->debug_names.s.section = sectp;
2004 dwz_file->debug_names.size = bfd_section_size (sectp);
2005 }
2006 }
2007
2008 /* See dwarf2read.h. */
2009
2010 struct dwz_file *
2011 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2012 {
2013 const char *filename;
2014 bfd_size_type buildid_len_arg;
2015 size_t buildid_len;
2016 bfd_byte *buildid;
2017
2018 if (dwarf2_per_objfile->dwz_file != NULL)
2019 return dwarf2_per_objfile->dwz_file.get ();
2020
2021 bfd_set_error (bfd_error_no_error);
2022 gdb::unique_xmalloc_ptr<char> data
2023 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2024 &buildid_len_arg, &buildid));
2025 if (data == NULL)
2026 {
2027 if (bfd_get_error () == bfd_error_no_error)
2028 return NULL;
2029 error (_("could not read '.gnu_debugaltlink' section: %s"),
2030 bfd_errmsg (bfd_get_error ()));
2031 }
2032
2033 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2034
2035 buildid_len = (size_t) buildid_len_arg;
2036
2037 filename = data.get ();
2038
2039 std::string abs_storage;
2040 if (!IS_ABSOLUTE_PATH (filename))
2041 {
2042 gdb::unique_xmalloc_ptr<char> abs
2043 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2044
2045 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2046 filename = abs_storage.c_str ();
2047 }
2048
2049 /* First try the file name given in the section. If that doesn't
2050 work, try to use the build-id instead. */
2051 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2052 if (dwz_bfd != NULL)
2053 {
2054 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2055 dwz_bfd.reset (nullptr);
2056 }
2057
2058 if (dwz_bfd == NULL)
2059 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2060
2061 if (dwz_bfd == nullptr)
2062 {
2063 gdb::unique_xmalloc_ptr<char> alt_filename;
2064 const char *origname = dwarf2_per_objfile->objfile->original_name;
2065
2066 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2067 buildid_len,
2068 origname,
2069 &alt_filename));
2070
2071 if (fd.get () >= 0)
2072 {
2073 /* File successfully retrieved from server. */
2074 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2075
2076 if (dwz_bfd == nullptr)
2077 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2078 alt_filename.get ());
2079 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2080 dwz_bfd.reset (nullptr);
2081 }
2082 }
2083
2084 if (dwz_bfd == NULL)
2085 error (_("could not find '.gnu_debugaltlink' file for %s"),
2086 objfile_name (dwarf2_per_objfile->objfile));
2087
2088 std::unique_ptr<struct dwz_file> result
2089 (new struct dwz_file (std::move (dwz_bfd)));
2090
2091 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2092 result.get ());
2093
2094 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2095 result->dwz_bfd.get ());
2096 dwarf2_per_objfile->dwz_file = std::move (result);
2097 return dwarf2_per_objfile->dwz_file.get ();
2098 }
2099 \f
2100 /* DWARF quick_symbols_functions support. */
2101
2102 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2103 unique line tables, so we maintain a separate table of all .debug_line
2104 derived entries to support the sharing.
2105 All the quick functions need is the list of file names. We discard the
2106 line_header when we're done and don't need to record it here. */
2107 struct quick_file_names
2108 {
2109 /* The data used to construct the hash key. */
2110 struct stmt_list_hash hash;
2111
2112 /* The number of entries in file_names, real_names. */
2113 unsigned int num_file_names;
2114
2115 /* The file names from the line table, after being run through
2116 file_full_name. */
2117 const char **file_names;
2118
2119 /* The file names from the line table after being run through
2120 gdb_realpath. These are computed lazily. */
2121 const char **real_names;
2122 };
2123
2124 /* When using the index (and thus not using psymtabs), each CU has an
2125 object of this type. This is used to hold information needed by
2126 the various "quick" methods. */
2127 struct dwarf2_per_cu_quick_data
2128 {
2129 /* The file table. This can be NULL if there was no file table
2130 or it's currently not read in.
2131 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2132 struct quick_file_names *file_names;
2133
2134 /* The corresponding symbol table. This is NULL if symbols for this
2135 CU have not yet been read. */
2136 struct compunit_symtab *compunit_symtab;
2137
2138 /* A temporary mark bit used when iterating over all CUs in
2139 expand_symtabs_matching. */
2140 unsigned int mark : 1;
2141
2142 /* True if we've tried to read the file table and found there isn't one.
2143 There will be no point in trying to read it again next time. */
2144 unsigned int no_file_data : 1;
2145 };
2146
2147 /* Utility hash function for a stmt_list_hash. */
2148
2149 static hashval_t
2150 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2151 {
2152 hashval_t v = 0;
2153
2154 if (stmt_list_hash->dwo_unit != NULL)
2155 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2156 v += to_underlying (stmt_list_hash->line_sect_off);
2157 return v;
2158 }
2159
2160 /* Utility equality function for a stmt_list_hash. */
2161
2162 static int
2163 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2164 const struct stmt_list_hash *rhs)
2165 {
2166 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2167 return 0;
2168 if (lhs->dwo_unit != NULL
2169 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2170 return 0;
2171
2172 return lhs->line_sect_off == rhs->line_sect_off;
2173 }
2174
2175 /* Hash function for a quick_file_names. */
2176
2177 static hashval_t
2178 hash_file_name_entry (const void *e)
2179 {
2180 const struct quick_file_names *file_data
2181 = (const struct quick_file_names *) e;
2182
2183 return hash_stmt_list_entry (&file_data->hash);
2184 }
2185
2186 /* Equality function for a quick_file_names. */
2187
2188 static int
2189 eq_file_name_entry (const void *a, const void *b)
2190 {
2191 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2192 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2193
2194 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2195 }
2196
2197 /* Delete function for a quick_file_names. */
2198
2199 static void
2200 delete_file_name_entry (void *e)
2201 {
2202 struct quick_file_names *file_data = (struct quick_file_names *) e;
2203 int i;
2204
2205 for (i = 0; i < file_data->num_file_names; ++i)
2206 {
2207 xfree ((void*) file_data->file_names[i]);
2208 if (file_data->real_names)
2209 xfree ((void*) file_data->real_names[i]);
2210 }
2211
2212 /* The space for the struct itself lives on objfile_obstack,
2213 so we don't free it here. */
2214 }
2215
2216 /* Create a quick_file_names hash table. */
2217
2218 static htab_up
2219 create_quick_file_names_table (unsigned int nr_initial_entries)
2220 {
2221 return htab_up (htab_create_alloc (nr_initial_entries,
2222 hash_file_name_entry, eq_file_name_entry,
2223 delete_file_name_entry, xcalloc, xfree));
2224 }
2225
2226 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2227 have to be created afterwards. You should call age_cached_comp_units after
2228 processing PER_CU->CU. dw2_setup must have been already called. */
2229
2230 static void
2231 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2232 {
2233 if (per_cu->is_debug_types)
2234 load_full_type_unit (per_cu);
2235 else
2236 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2237
2238 if (per_cu->cu == NULL)
2239 return; /* Dummy CU. */
2240
2241 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2242 }
2243
2244 /* Read in the symbols for PER_CU. */
2245
2246 static void
2247 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2248 {
2249 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2250
2251 /* Skip type_unit_groups, reading the type units they contain
2252 is handled elsewhere. */
2253 if (per_cu->type_unit_group_p ())
2254 return;
2255
2256 /* The destructor of dwarf2_queue_guard frees any entries left on
2257 the queue. After this point we're guaranteed to leave this function
2258 with the dwarf queue empty. */
2259 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2260
2261 if (dwarf2_per_objfile->using_index
2262 ? per_cu->v.quick->compunit_symtab == NULL
2263 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2264 {
2265 queue_comp_unit (per_cu, language_minimal);
2266 load_cu (per_cu, skip_partial);
2267
2268 /* If we just loaded a CU from a DWO, and we're working with an index
2269 that may badly handle TUs, load all the TUs in that DWO as well.
2270 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2271 if (!per_cu->is_debug_types
2272 && per_cu->cu != NULL
2273 && per_cu->cu->dwo_unit != NULL
2274 && dwarf2_per_objfile->index_table != NULL
2275 && dwarf2_per_objfile->index_table->version <= 7
2276 /* DWP files aren't supported yet. */
2277 && get_dwp_file (dwarf2_per_objfile) == NULL)
2278 queue_and_load_all_dwo_tus (per_cu);
2279 }
2280
2281 process_queue (dwarf2_per_objfile);
2282
2283 /* Age the cache, releasing compilation units that have not
2284 been used recently. */
2285 age_cached_comp_units (dwarf2_per_objfile);
2286 }
2287
2288 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2289 the objfile from which this CU came. Returns the resulting symbol
2290 table. */
2291
2292 static struct compunit_symtab *
2293 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2294 {
2295 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2296
2297 gdb_assert (dwarf2_per_objfile->using_index);
2298 if (!per_cu->v.quick->compunit_symtab)
2299 {
2300 free_cached_comp_units freer (dwarf2_per_objfile);
2301 scoped_restore decrementer = increment_reading_symtab ();
2302 dw2_do_instantiate_symtab (per_cu, skip_partial);
2303 process_cu_includes (dwarf2_per_objfile);
2304 }
2305
2306 return per_cu->v.quick->compunit_symtab;
2307 }
2308
2309 /* See declaration. */
2310
2311 dwarf2_per_cu_data *
2312 dwarf2_per_objfile::get_cutu (int index)
2313 {
2314 if (index >= this->all_comp_units.size ())
2315 {
2316 index -= this->all_comp_units.size ();
2317 gdb_assert (index < this->all_type_units.size ());
2318 return &this->all_type_units[index]->per_cu;
2319 }
2320
2321 return this->all_comp_units[index];
2322 }
2323
2324 /* See declaration. */
2325
2326 dwarf2_per_cu_data *
2327 dwarf2_per_objfile::get_cu (int index)
2328 {
2329 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2330
2331 return this->all_comp_units[index];
2332 }
2333
2334 /* See declaration. */
2335
2336 signatured_type *
2337 dwarf2_per_objfile::get_tu (int index)
2338 {
2339 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2340
2341 return this->all_type_units[index];
2342 }
2343
2344 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2345 objfile_obstack, and constructed with the specified field
2346 values. */
2347
2348 static dwarf2_per_cu_data *
2349 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2350 struct dwarf2_section_info *section,
2351 int is_dwz,
2352 sect_offset sect_off, ULONGEST length)
2353 {
2354 struct objfile *objfile = dwarf2_per_objfile->objfile;
2355 dwarf2_per_cu_data *the_cu
2356 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2357 struct dwarf2_per_cu_data);
2358 the_cu->sect_off = sect_off;
2359 the_cu->length = length;
2360 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2361 the_cu->section = section;
2362 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2363 struct dwarf2_per_cu_quick_data);
2364 the_cu->is_dwz = is_dwz;
2365 return the_cu;
2366 }
2367
2368 /* A helper for create_cus_from_index that handles a given list of
2369 CUs. */
2370
2371 static void
2372 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2373 const gdb_byte *cu_list, offset_type n_elements,
2374 struct dwarf2_section_info *section,
2375 int is_dwz)
2376 {
2377 for (offset_type i = 0; i < n_elements; i += 2)
2378 {
2379 gdb_static_assert (sizeof (ULONGEST) >= 8);
2380
2381 sect_offset sect_off
2382 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2383 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2384 cu_list += 2 * 8;
2385
2386 dwarf2_per_cu_data *per_cu
2387 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2388 sect_off, length);
2389 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2390 }
2391 }
2392
2393 /* Read the CU list from the mapped index, and use it to create all
2394 the CU objects for this objfile. */
2395
2396 static void
2397 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2398 const gdb_byte *cu_list, offset_type cu_list_elements,
2399 const gdb_byte *dwz_list, offset_type dwz_elements)
2400 {
2401 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2402 dwarf2_per_objfile->all_comp_units.reserve
2403 ((cu_list_elements + dwz_elements) / 2);
2404
2405 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2406 &dwarf2_per_objfile->info, 0);
2407
2408 if (dwz_elements == 0)
2409 return;
2410
2411 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2412 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2413 &dwz->info, 1);
2414 }
2415
2416 /* Create the signatured type hash table from the index. */
2417
2418 static void
2419 create_signatured_type_table_from_index
2420 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2421 struct dwarf2_section_info *section,
2422 const gdb_byte *bytes,
2423 offset_type elements)
2424 {
2425 struct objfile *objfile = dwarf2_per_objfile->objfile;
2426
2427 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2428 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2429
2430 htab_up sig_types_hash = allocate_signatured_type_table ();
2431
2432 for (offset_type i = 0; i < elements; i += 3)
2433 {
2434 struct signatured_type *sig_type;
2435 ULONGEST signature;
2436 void **slot;
2437 cu_offset type_offset_in_tu;
2438
2439 gdb_static_assert (sizeof (ULONGEST) >= 8);
2440 sect_offset sect_off
2441 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2442 type_offset_in_tu
2443 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2444 BFD_ENDIAN_LITTLE);
2445 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2446 bytes += 3 * 8;
2447
2448 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2449 struct signatured_type);
2450 sig_type->signature = signature;
2451 sig_type->type_offset_in_tu = type_offset_in_tu;
2452 sig_type->per_cu.is_debug_types = 1;
2453 sig_type->per_cu.section = section;
2454 sig_type->per_cu.sect_off = sect_off;
2455 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2456 sig_type->per_cu.v.quick
2457 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2458 struct dwarf2_per_cu_quick_data);
2459
2460 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2461 *slot = sig_type;
2462
2463 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2464 }
2465
2466 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2467 }
2468
2469 /* Create the signatured type hash table from .debug_names. */
2470
2471 static void
2472 create_signatured_type_table_from_debug_names
2473 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2474 const mapped_debug_names &map,
2475 struct dwarf2_section_info *section,
2476 struct dwarf2_section_info *abbrev_section)
2477 {
2478 struct objfile *objfile = dwarf2_per_objfile->objfile;
2479
2480 section->read (objfile);
2481 abbrev_section->read (objfile);
2482
2483 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2484 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2485
2486 htab_up sig_types_hash = allocate_signatured_type_table ();
2487
2488 for (uint32_t i = 0; i < map.tu_count; ++i)
2489 {
2490 struct signatured_type *sig_type;
2491 void **slot;
2492
2493 sect_offset sect_off
2494 = (sect_offset) (extract_unsigned_integer
2495 (map.tu_table_reordered + i * map.offset_size,
2496 map.offset_size,
2497 map.dwarf5_byte_order));
2498
2499 comp_unit_head cu_header;
2500 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2501 abbrev_section,
2502 section->buffer + to_underlying (sect_off),
2503 rcuh_kind::TYPE);
2504
2505 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2506 struct signatured_type);
2507 sig_type->signature = cu_header.signature;
2508 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2509 sig_type->per_cu.is_debug_types = 1;
2510 sig_type->per_cu.section = section;
2511 sig_type->per_cu.sect_off = sect_off;
2512 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2513 sig_type->per_cu.v.quick
2514 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2515 struct dwarf2_per_cu_quick_data);
2516
2517 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2518 *slot = sig_type;
2519
2520 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2521 }
2522
2523 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2524 }
2525
2526 /* Read the address map data from the mapped index, and use it to
2527 populate the objfile's psymtabs_addrmap. */
2528
2529 static void
2530 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2531 struct mapped_index *index)
2532 {
2533 struct objfile *objfile = dwarf2_per_objfile->objfile;
2534 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2535 const gdb_byte *iter, *end;
2536 struct addrmap *mutable_map;
2537 CORE_ADDR baseaddr;
2538
2539 auto_obstack temp_obstack;
2540
2541 mutable_map = addrmap_create_mutable (&temp_obstack);
2542
2543 iter = index->address_table.data ();
2544 end = iter + index->address_table.size ();
2545
2546 baseaddr = objfile->text_section_offset ();
2547
2548 while (iter < end)
2549 {
2550 ULONGEST hi, lo, cu_index;
2551 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2552 iter += 8;
2553 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2554 iter += 8;
2555 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2556 iter += 4;
2557
2558 if (lo > hi)
2559 {
2560 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2561 hex_string (lo), hex_string (hi));
2562 continue;
2563 }
2564
2565 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2566 {
2567 complaint (_(".gdb_index address table has invalid CU number %u"),
2568 (unsigned) cu_index);
2569 continue;
2570 }
2571
2572 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2573 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2574 addrmap_set_empty (mutable_map, lo, hi - 1,
2575 dwarf2_per_objfile->get_cu (cu_index));
2576 }
2577
2578 objfile->partial_symtabs->psymtabs_addrmap
2579 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2580 }
2581
2582 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2583 populate the objfile's psymtabs_addrmap. */
2584
2585 static void
2586 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2587 struct dwarf2_section_info *section)
2588 {
2589 struct objfile *objfile = dwarf2_per_objfile->objfile;
2590 bfd *abfd = objfile->obfd;
2591 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2592 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2593
2594 auto_obstack temp_obstack;
2595 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2596
2597 std::unordered_map<sect_offset,
2598 dwarf2_per_cu_data *,
2599 gdb::hash_enum<sect_offset>>
2600 debug_info_offset_to_per_cu;
2601 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2602 {
2603 const auto insertpair
2604 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2605 if (!insertpair.second)
2606 {
2607 warning (_("Section .debug_aranges in %s has duplicate "
2608 "debug_info_offset %s, ignoring .debug_aranges."),
2609 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2610 return;
2611 }
2612 }
2613
2614 section->read (objfile);
2615
2616 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2617
2618 const gdb_byte *addr = section->buffer;
2619
2620 while (addr < section->buffer + section->size)
2621 {
2622 const gdb_byte *const entry_addr = addr;
2623 unsigned int bytes_read;
2624
2625 const LONGEST entry_length = read_initial_length (abfd, addr,
2626 &bytes_read);
2627 addr += bytes_read;
2628
2629 const gdb_byte *const entry_end = addr + entry_length;
2630 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2631 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2632 if (addr + entry_length > section->buffer + section->size)
2633 {
2634 warning (_("Section .debug_aranges in %s entry at offset %s "
2635 "length %s exceeds section length %s, "
2636 "ignoring .debug_aranges."),
2637 objfile_name (objfile),
2638 plongest (entry_addr - section->buffer),
2639 plongest (bytes_read + entry_length),
2640 pulongest (section->size));
2641 return;
2642 }
2643
2644 /* The version number. */
2645 const uint16_t version = read_2_bytes (abfd, addr);
2646 addr += 2;
2647 if (version != 2)
2648 {
2649 warning (_("Section .debug_aranges in %s entry at offset %s "
2650 "has unsupported version %d, ignoring .debug_aranges."),
2651 objfile_name (objfile),
2652 plongest (entry_addr - section->buffer), version);
2653 return;
2654 }
2655
2656 const uint64_t debug_info_offset
2657 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2658 addr += offset_size;
2659 const auto per_cu_it
2660 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2661 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2662 {
2663 warning (_("Section .debug_aranges in %s entry at offset %s "
2664 "debug_info_offset %s does not exists, "
2665 "ignoring .debug_aranges."),
2666 objfile_name (objfile),
2667 plongest (entry_addr - section->buffer),
2668 pulongest (debug_info_offset));
2669 return;
2670 }
2671 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2672
2673 const uint8_t address_size = *addr++;
2674 if (address_size < 1 || address_size > 8)
2675 {
2676 warning (_("Section .debug_aranges in %s entry at offset %s "
2677 "address_size %u is invalid, ignoring .debug_aranges."),
2678 objfile_name (objfile),
2679 plongest (entry_addr - section->buffer), address_size);
2680 return;
2681 }
2682
2683 const uint8_t segment_selector_size = *addr++;
2684 if (segment_selector_size != 0)
2685 {
2686 warning (_("Section .debug_aranges in %s entry at offset %s "
2687 "segment_selector_size %u is not supported, "
2688 "ignoring .debug_aranges."),
2689 objfile_name (objfile),
2690 plongest (entry_addr - section->buffer),
2691 segment_selector_size);
2692 return;
2693 }
2694
2695 /* Must pad to an alignment boundary that is twice the address
2696 size. It is undocumented by the DWARF standard but GCC does
2697 use it. */
2698 for (size_t padding = ((-(addr - section->buffer))
2699 & (2 * address_size - 1));
2700 padding > 0; padding--)
2701 if (*addr++ != 0)
2702 {
2703 warning (_("Section .debug_aranges in %s entry at offset %s "
2704 "padding is not zero, ignoring .debug_aranges."),
2705 objfile_name (objfile),
2706 plongest (entry_addr - section->buffer));
2707 return;
2708 }
2709
2710 for (;;)
2711 {
2712 if (addr + 2 * address_size > entry_end)
2713 {
2714 warning (_("Section .debug_aranges in %s entry at offset %s "
2715 "address list is not properly terminated, "
2716 "ignoring .debug_aranges."),
2717 objfile_name (objfile),
2718 plongest (entry_addr - section->buffer));
2719 return;
2720 }
2721 ULONGEST start = extract_unsigned_integer (addr, address_size,
2722 dwarf5_byte_order);
2723 addr += address_size;
2724 ULONGEST length = extract_unsigned_integer (addr, address_size,
2725 dwarf5_byte_order);
2726 addr += address_size;
2727 if (start == 0 && length == 0)
2728 break;
2729 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2730 {
2731 /* Symbol was eliminated due to a COMDAT group. */
2732 continue;
2733 }
2734 ULONGEST end = start + length;
2735 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2736 - baseaddr);
2737 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2738 - baseaddr);
2739 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2740 }
2741 }
2742
2743 objfile->partial_symtabs->psymtabs_addrmap
2744 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2745 }
2746
2747 /* Find a slot in the mapped index INDEX for the object named NAME.
2748 If NAME is found, set *VEC_OUT to point to the CU vector in the
2749 constant pool and return true. If NAME cannot be found, return
2750 false. */
2751
2752 static bool
2753 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2754 offset_type **vec_out)
2755 {
2756 offset_type hash;
2757 offset_type slot, step;
2758 int (*cmp) (const char *, const char *);
2759
2760 gdb::unique_xmalloc_ptr<char> without_params;
2761 if (current_language->la_language == language_cplus
2762 || current_language->la_language == language_fortran
2763 || current_language->la_language == language_d)
2764 {
2765 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2766 not contain any. */
2767
2768 if (strchr (name, '(') != NULL)
2769 {
2770 without_params = cp_remove_params (name);
2771
2772 if (without_params != NULL)
2773 name = without_params.get ();
2774 }
2775 }
2776
2777 /* Index version 4 did not support case insensitive searches. But the
2778 indices for case insensitive languages are built in lowercase, therefore
2779 simulate our NAME being searched is also lowercased. */
2780 hash = mapped_index_string_hash ((index->version == 4
2781 && case_sensitivity == case_sensitive_off
2782 ? 5 : index->version),
2783 name);
2784
2785 slot = hash & (index->symbol_table.size () - 1);
2786 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2787 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2788
2789 for (;;)
2790 {
2791 const char *str;
2792
2793 const auto &bucket = index->symbol_table[slot];
2794 if (bucket.name == 0 && bucket.vec == 0)
2795 return false;
2796
2797 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2798 if (!cmp (name, str))
2799 {
2800 *vec_out = (offset_type *) (index->constant_pool
2801 + MAYBE_SWAP (bucket.vec));
2802 return true;
2803 }
2804
2805 slot = (slot + step) & (index->symbol_table.size () - 1);
2806 }
2807 }
2808
2809 /* A helper function that reads the .gdb_index from BUFFER and fills
2810 in MAP. FILENAME is the name of the file containing the data;
2811 it is used for error reporting. DEPRECATED_OK is true if it is
2812 ok to use deprecated sections.
2813
2814 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2815 out parameters that are filled in with information about the CU and
2816 TU lists in the section.
2817
2818 Returns true if all went well, false otherwise. */
2819
2820 static bool
2821 read_gdb_index_from_buffer (struct objfile *objfile,
2822 const char *filename,
2823 bool deprecated_ok,
2824 gdb::array_view<const gdb_byte> buffer,
2825 struct mapped_index *map,
2826 const gdb_byte **cu_list,
2827 offset_type *cu_list_elements,
2828 const gdb_byte **types_list,
2829 offset_type *types_list_elements)
2830 {
2831 const gdb_byte *addr = &buffer[0];
2832
2833 /* Version check. */
2834 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2835 /* Versions earlier than 3 emitted every copy of a psymbol. This
2836 causes the index to behave very poorly for certain requests. Version 3
2837 contained incomplete addrmap. So, it seems better to just ignore such
2838 indices. */
2839 if (version < 4)
2840 {
2841 static int warning_printed = 0;
2842 if (!warning_printed)
2843 {
2844 warning (_("Skipping obsolete .gdb_index section in %s."),
2845 filename);
2846 warning_printed = 1;
2847 }
2848 return 0;
2849 }
2850 /* Index version 4 uses a different hash function than index version
2851 5 and later.
2852
2853 Versions earlier than 6 did not emit psymbols for inlined
2854 functions. Using these files will cause GDB not to be able to
2855 set breakpoints on inlined functions by name, so we ignore these
2856 indices unless the user has done
2857 "set use-deprecated-index-sections on". */
2858 if (version < 6 && !deprecated_ok)
2859 {
2860 static int warning_printed = 0;
2861 if (!warning_printed)
2862 {
2863 warning (_("\
2864 Skipping deprecated .gdb_index section in %s.\n\
2865 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2866 to use the section anyway."),
2867 filename);
2868 warning_printed = 1;
2869 }
2870 return 0;
2871 }
2872 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2873 of the TU (for symbols coming from TUs),
2874 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2875 Plus gold-generated indices can have duplicate entries for global symbols,
2876 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2877 These are just performance bugs, and we can't distinguish gdb-generated
2878 indices from gold-generated ones, so issue no warning here. */
2879
2880 /* Indexes with higher version than the one supported by GDB may be no
2881 longer backward compatible. */
2882 if (version > 8)
2883 return 0;
2884
2885 map->version = version;
2886
2887 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2888
2889 int i = 0;
2890 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2891 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2892 / 8);
2893 ++i;
2894
2895 *types_list = addr + MAYBE_SWAP (metadata[i]);
2896 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2897 - MAYBE_SWAP (metadata[i]))
2898 / 8);
2899 ++i;
2900
2901 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2902 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2903 map->address_table
2904 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2905 ++i;
2906
2907 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2908 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2909 map->symbol_table
2910 = gdb::array_view<mapped_index::symbol_table_slot>
2911 ((mapped_index::symbol_table_slot *) symbol_table,
2912 (mapped_index::symbol_table_slot *) symbol_table_end);
2913
2914 ++i;
2915 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2916
2917 return 1;
2918 }
2919
2920 /* Callback types for dwarf2_read_gdb_index. */
2921
2922 typedef gdb::function_view
2923 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2924 get_gdb_index_contents_ftype;
2925 typedef gdb::function_view
2926 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2927 get_gdb_index_contents_dwz_ftype;
2928
2929 /* Read .gdb_index. If everything went ok, initialize the "quick"
2930 elements of all the CUs and return 1. Otherwise, return 0. */
2931
2932 static int
2933 dwarf2_read_gdb_index
2934 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2935 get_gdb_index_contents_ftype get_gdb_index_contents,
2936 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2937 {
2938 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2939 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2940 struct dwz_file *dwz;
2941 struct objfile *objfile = dwarf2_per_objfile->objfile;
2942
2943 gdb::array_view<const gdb_byte> main_index_contents
2944 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
2945
2946 if (main_index_contents.empty ())
2947 return 0;
2948
2949 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2950 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
2951 use_deprecated_index_sections,
2952 main_index_contents, map.get (), &cu_list,
2953 &cu_list_elements, &types_list,
2954 &types_list_elements))
2955 return 0;
2956
2957 /* Don't use the index if it's empty. */
2958 if (map->symbol_table.empty ())
2959 return 0;
2960
2961 /* If there is a .dwz file, read it so we can get its CU list as
2962 well. */
2963 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2964 if (dwz != NULL)
2965 {
2966 struct mapped_index dwz_map;
2967 const gdb_byte *dwz_types_ignore;
2968 offset_type dwz_types_elements_ignore;
2969
2970 gdb::array_view<const gdb_byte> dwz_index_content
2971 = get_gdb_index_contents_dwz (objfile, dwz);
2972
2973 if (dwz_index_content.empty ())
2974 return 0;
2975
2976 if (!read_gdb_index_from_buffer (objfile,
2977 bfd_get_filename (dwz->dwz_bfd.get ()),
2978 1, dwz_index_content, &dwz_map,
2979 &dwz_list, &dwz_list_elements,
2980 &dwz_types_ignore,
2981 &dwz_types_elements_ignore))
2982 {
2983 warning (_("could not read '.gdb_index' section from %s; skipping"),
2984 bfd_get_filename (dwz->dwz_bfd.get ()));
2985 return 0;
2986 }
2987 }
2988
2989 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
2990 dwz_list, dwz_list_elements);
2991
2992 if (types_list_elements)
2993 {
2994 /* We can only handle a single .debug_types when we have an
2995 index. */
2996 if (dwarf2_per_objfile->types.size () != 1)
2997 return 0;
2998
2999 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3000
3001 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3002 types_list, types_list_elements);
3003 }
3004
3005 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3006
3007 dwarf2_per_objfile->index_table = std::move (map);
3008 dwarf2_per_objfile->using_index = 1;
3009 dwarf2_per_objfile->quick_file_names_table =
3010 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3011
3012 return 1;
3013 }
3014
3015 /* die_reader_func for dw2_get_file_names. */
3016
3017 static void
3018 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3019 const gdb_byte *info_ptr,
3020 struct die_info *comp_unit_die)
3021 {
3022 struct dwarf2_cu *cu = reader->cu;
3023 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3024 struct dwarf2_per_objfile *dwarf2_per_objfile
3025 = cu->per_cu->dwarf2_per_objfile;
3026 struct objfile *objfile = dwarf2_per_objfile->objfile;
3027 struct dwarf2_per_cu_data *lh_cu;
3028 struct attribute *attr;
3029 void **slot;
3030 struct quick_file_names *qfn;
3031
3032 gdb_assert (! this_cu->is_debug_types);
3033
3034 /* Our callers never want to match partial units -- instead they
3035 will match the enclosing full CU. */
3036 if (comp_unit_die->tag == DW_TAG_partial_unit)
3037 {
3038 this_cu->v.quick->no_file_data = 1;
3039 return;
3040 }
3041
3042 lh_cu = this_cu;
3043 slot = NULL;
3044
3045 line_header_up lh;
3046 sect_offset line_offset {};
3047
3048 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3049 if (attr != nullptr)
3050 {
3051 struct quick_file_names find_entry;
3052
3053 line_offset = (sect_offset) DW_UNSND (attr);
3054
3055 /* We may have already read in this line header (TU line header sharing).
3056 If we have we're done. */
3057 find_entry.hash.dwo_unit = cu->dwo_unit;
3058 find_entry.hash.line_sect_off = line_offset;
3059 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3060 &find_entry, INSERT);
3061 if (*slot != NULL)
3062 {
3063 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3064 return;
3065 }
3066
3067 lh = dwarf_decode_line_header (line_offset, cu);
3068 }
3069 if (lh == NULL)
3070 {
3071 lh_cu->v.quick->no_file_data = 1;
3072 return;
3073 }
3074
3075 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3076 qfn->hash.dwo_unit = cu->dwo_unit;
3077 qfn->hash.line_sect_off = line_offset;
3078 gdb_assert (slot != NULL);
3079 *slot = qfn;
3080
3081 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3082
3083 int offset = 0;
3084 if (strcmp (fnd.name, "<unknown>") != 0)
3085 ++offset;
3086
3087 qfn->num_file_names = offset + lh->file_names_size ();
3088 qfn->file_names =
3089 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3090 if (offset != 0)
3091 qfn->file_names[0] = xstrdup (fnd.name);
3092 for (int i = 0; i < lh->file_names_size (); ++i)
3093 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3094 fnd.comp_dir).release ();
3095 qfn->real_names = NULL;
3096
3097 lh_cu->v.quick->file_names = qfn;
3098 }
3099
3100 /* A helper for the "quick" functions which attempts to read the line
3101 table for THIS_CU. */
3102
3103 static struct quick_file_names *
3104 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3105 {
3106 /* This should never be called for TUs. */
3107 gdb_assert (! this_cu->is_debug_types);
3108 /* Nor type unit groups. */
3109 gdb_assert (! this_cu->type_unit_group_p ());
3110
3111 if (this_cu->v.quick->file_names != NULL)
3112 return this_cu->v.quick->file_names;
3113 /* If we know there is no line data, no point in looking again. */
3114 if (this_cu->v.quick->no_file_data)
3115 return NULL;
3116
3117 cutu_reader reader (this_cu);
3118 if (!reader.dummy_p)
3119 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3120
3121 if (this_cu->v.quick->no_file_data)
3122 return NULL;
3123 return this_cu->v.quick->file_names;
3124 }
3125
3126 /* A helper for the "quick" functions which computes and caches the
3127 real path for a given file name from the line table. */
3128
3129 static const char *
3130 dw2_get_real_path (struct objfile *objfile,
3131 struct quick_file_names *qfn, int index)
3132 {
3133 if (qfn->real_names == NULL)
3134 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3135 qfn->num_file_names, const char *);
3136
3137 if (qfn->real_names[index] == NULL)
3138 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3139
3140 return qfn->real_names[index];
3141 }
3142
3143 static struct symtab *
3144 dw2_find_last_source_symtab (struct objfile *objfile)
3145 {
3146 struct dwarf2_per_objfile *dwarf2_per_objfile
3147 = get_dwarf2_per_objfile (objfile);
3148 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3149 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3150
3151 if (cust == NULL)
3152 return NULL;
3153
3154 return compunit_primary_filetab (cust);
3155 }
3156
3157 /* Traversal function for dw2_forget_cached_source_info. */
3158
3159 static int
3160 dw2_free_cached_file_names (void **slot, void *info)
3161 {
3162 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3163
3164 if (file_data->real_names)
3165 {
3166 int i;
3167
3168 for (i = 0; i < file_data->num_file_names; ++i)
3169 {
3170 xfree ((void*) file_data->real_names[i]);
3171 file_data->real_names[i] = NULL;
3172 }
3173 }
3174
3175 return 1;
3176 }
3177
3178 static void
3179 dw2_forget_cached_source_info (struct objfile *objfile)
3180 {
3181 struct dwarf2_per_objfile *dwarf2_per_objfile
3182 = get_dwarf2_per_objfile (objfile);
3183
3184 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3185 dw2_free_cached_file_names, NULL);
3186 }
3187
3188 /* Helper function for dw2_map_symtabs_matching_filename that expands
3189 the symtabs and calls the iterator. */
3190
3191 static int
3192 dw2_map_expand_apply (struct objfile *objfile,
3193 struct dwarf2_per_cu_data *per_cu,
3194 const char *name, const char *real_path,
3195 gdb::function_view<bool (symtab *)> callback)
3196 {
3197 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3198
3199 /* Don't visit already-expanded CUs. */
3200 if (per_cu->v.quick->compunit_symtab)
3201 return 0;
3202
3203 /* This may expand more than one symtab, and we want to iterate over
3204 all of them. */
3205 dw2_instantiate_symtab (per_cu, false);
3206
3207 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3208 last_made, callback);
3209 }
3210
3211 /* Implementation of the map_symtabs_matching_filename method. */
3212
3213 static bool
3214 dw2_map_symtabs_matching_filename
3215 (struct objfile *objfile, const char *name, const char *real_path,
3216 gdb::function_view<bool (symtab *)> callback)
3217 {
3218 const char *name_basename = lbasename (name);
3219 struct dwarf2_per_objfile *dwarf2_per_objfile
3220 = get_dwarf2_per_objfile (objfile);
3221
3222 /* The rule is CUs specify all the files, including those used by
3223 any TU, so there's no need to scan TUs here. */
3224
3225 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3226 {
3227 /* We only need to look at symtabs not already expanded. */
3228 if (per_cu->v.quick->compunit_symtab)
3229 continue;
3230
3231 quick_file_names *file_data = dw2_get_file_names (per_cu);
3232 if (file_data == NULL)
3233 continue;
3234
3235 for (int j = 0; j < file_data->num_file_names; ++j)
3236 {
3237 const char *this_name = file_data->file_names[j];
3238 const char *this_real_name;
3239
3240 if (compare_filenames_for_search (this_name, name))
3241 {
3242 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3243 callback))
3244 return true;
3245 continue;
3246 }
3247
3248 /* Before we invoke realpath, which can get expensive when many
3249 files are involved, do a quick comparison of the basenames. */
3250 if (! basenames_may_differ
3251 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3252 continue;
3253
3254 this_real_name = dw2_get_real_path (objfile, file_data, j);
3255 if (compare_filenames_for_search (this_real_name, name))
3256 {
3257 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3258 callback))
3259 return true;
3260 continue;
3261 }
3262
3263 if (real_path != NULL)
3264 {
3265 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3266 gdb_assert (IS_ABSOLUTE_PATH (name));
3267 if (this_real_name != NULL
3268 && FILENAME_CMP (real_path, this_real_name) == 0)
3269 {
3270 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3271 callback))
3272 return true;
3273 continue;
3274 }
3275 }
3276 }
3277 }
3278
3279 return false;
3280 }
3281
3282 /* Struct used to manage iterating over all CUs looking for a symbol. */
3283
3284 struct dw2_symtab_iterator
3285 {
3286 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3287 struct dwarf2_per_objfile *dwarf2_per_objfile;
3288 /* If set, only look for symbols that match that block. Valid values are
3289 GLOBAL_BLOCK and STATIC_BLOCK. */
3290 gdb::optional<block_enum> block_index;
3291 /* The kind of symbol we're looking for. */
3292 domain_enum domain;
3293 /* The list of CUs from the index entry of the symbol,
3294 or NULL if not found. */
3295 offset_type *vec;
3296 /* The next element in VEC to look at. */
3297 int next;
3298 /* The number of elements in VEC, or zero if there is no match. */
3299 int length;
3300 /* Have we seen a global version of the symbol?
3301 If so we can ignore all further global instances.
3302 This is to work around gold/15646, inefficient gold-generated
3303 indices. */
3304 int global_seen;
3305 };
3306
3307 /* Initialize the index symtab iterator ITER. */
3308
3309 static void
3310 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3311 struct dwarf2_per_objfile *dwarf2_per_objfile,
3312 gdb::optional<block_enum> block_index,
3313 domain_enum domain,
3314 const char *name)
3315 {
3316 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3317 iter->block_index = block_index;
3318 iter->domain = domain;
3319 iter->next = 0;
3320 iter->global_seen = 0;
3321
3322 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3323
3324 /* index is NULL if OBJF_READNOW. */
3325 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3326 iter->length = MAYBE_SWAP (*iter->vec);
3327 else
3328 {
3329 iter->vec = NULL;
3330 iter->length = 0;
3331 }
3332 }
3333
3334 /* Return the next matching CU or NULL if there are no more. */
3335
3336 static struct dwarf2_per_cu_data *
3337 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3338 {
3339 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3340
3341 for ( ; iter->next < iter->length; ++iter->next)
3342 {
3343 offset_type cu_index_and_attrs =
3344 MAYBE_SWAP (iter->vec[iter->next + 1]);
3345 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3346 gdb_index_symbol_kind symbol_kind =
3347 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3348 /* Only check the symbol attributes if they're present.
3349 Indices prior to version 7 don't record them,
3350 and indices >= 7 may elide them for certain symbols
3351 (gold does this). */
3352 int attrs_valid =
3353 (dwarf2_per_objfile->index_table->version >= 7
3354 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3355
3356 /* Don't crash on bad data. */
3357 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3358 + dwarf2_per_objfile->all_type_units.size ()))
3359 {
3360 complaint (_(".gdb_index entry has bad CU index"
3361 " [in module %s]"),
3362 objfile_name (dwarf2_per_objfile->objfile));
3363 continue;
3364 }
3365
3366 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3367
3368 /* Skip if already read in. */
3369 if (per_cu->v.quick->compunit_symtab)
3370 continue;
3371
3372 /* Check static vs global. */
3373 if (attrs_valid)
3374 {
3375 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3376
3377 if (iter->block_index.has_value ())
3378 {
3379 bool want_static = *iter->block_index == STATIC_BLOCK;
3380
3381 if (is_static != want_static)
3382 continue;
3383 }
3384
3385 /* Work around gold/15646. */
3386 if (!is_static && iter->global_seen)
3387 continue;
3388 if (!is_static)
3389 iter->global_seen = 1;
3390 }
3391
3392 /* Only check the symbol's kind if it has one. */
3393 if (attrs_valid)
3394 {
3395 switch (iter->domain)
3396 {
3397 case VAR_DOMAIN:
3398 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3399 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3400 /* Some types are also in VAR_DOMAIN. */
3401 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3402 continue;
3403 break;
3404 case STRUCT_DOMAIN:
3405 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3406 continue;
3407 break;
3408 case LABEL_DOMAIN:
3409 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3410 continue;
3411 break;
3412 case MODULE_DOMAIN:
3413 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3414 continue;
3415 break;
3416 default:
3417 break;
3418 }
3419 }
3420
3421 ++iter->next;
3422 return per_cu;
3423 }
3424
3425 return NULL;
3426 }
3427
3428 static struct compunit_symtab *
3429 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3430 const char *name, domain_enum domain)
3431 {
3432 struct compunit_symtab *stab_best = NULL;
3433 struct dwarf2_per_objfile *dwarf2_per_objfile
3434 = get_dwarf2_per_objfile (objfile);
3435
3436 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3437
3438 struct dw2_symtab_iterator iter;
3439 struct dwarf2_per_cu_data *per_cu;
3440
3441 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3442
3443 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3444 {
3445 struct symbol *sym, *with_opaque = NULL;
3446 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3447 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3448 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3449
3450 sym = block_find_symbol (block, name, domain,
3451 block_find_non_opaque_type_preferred,
3452 &with_opaque);
3453
3454 /* Some caution must be observed with overloaded functions
3455 and methods, since the index will not contain any overload
3456 information (but NAME might contain it). */
3457
3458 if (sym != NULL
3459 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3460 return stab;
3461 if (with_opaque != NULL
3462 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3463 stab_best = stab;
3464
3465 /* Keep looking through other CUs. */
3466 }
3467
3468 return stab_best;
3469 }
3470
3471 static void
3472 dw2_print_stats (struct objfile *objfile)
3473 {
3474 struct dwarf2_per_objfile *dwarf2_per_objfile
3475 = get_dwarf2_per_objfile (objfile);
3476 int total = (dwarf2_per_objfile->all_comp_units.size ()
3477 + dwarf2_per_objfile->all_type_units.size ());
3478 int count = 0;
3479
3480 for (int i = 0; i < total; ++i)
3481 {
3482 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3483
3484 if (!per_cu->v.quick->compunit_symtab)
3485 ++count;
3486 }
3487 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3488 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3489 }
3490
3491 /* This dumps minimal information about the index.
3492 It is called via "mt print objfiles".
3493 One use is to verify .gdb_index has been loaded by the
3494 gdb.dwarf2/gdb-index.exp testcase. */
3495
3496 static void
3497 dw2_dump (struct objfile *objfile)
3498 {
3499 struct dwarf2_per_objfile *dwarf2_per_objfile
3500 = get_dwarf2_per_objfile (objfile);
3501
3502 gdb_assert (dwarf2_per_objfile->using_index);
3503 printf_filtered (".gdb_index:");
3504 if (dwarf2_per_objfile->index_table != NULL)
3505 {
3506 printf_filtered (" version %d\n",
3507 dwarf2_per_objfile->index_table->version);
3508 }
3509 else
3510 printf_filtered (" faked for \"readnow\"\n");
3511 printf_filtered ("\n");
3512 }
3513
3514 static void
3515 dw2_expand_symtabs_for_function (struct objfile *objfile,
3516 const char *func_name)
3517 {
3518 struct dwarf2_per_objfile *dwarf2_per_objfile
3519 = get_dwarf2_per_objfile (objfile);
3520
3521 struct dw2_symtab_iterator iter;
3522 struct dwarf2_per_cu_data *per_cu;
3523
3524 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3525
3526 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3527 dw2_instantiate_symtab (per_cu, false);
3528
3529 }
3530
3531 static void
3532 dw2_expand_all_symtabs (struct objfile *objfile)
3533 {
3534 struct dwarf2_per_objfile *dwarf2_per_objfile
3535 = get_dwarf2_per_objfile (objfile);
3536 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3537 + dwarf2_per_objfile->all_type_units.size ());
3538
3539 for (int i = 0; i < total_units; ++i)
3540 {
3541 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3542
3543 /* We don't want to directly expand a partial CU, because if we
3544 read it with the wrong language, then assertion failures can
3545 be triggered later on. See PR symtab/23010. So, tell
3546 dw2_instantiate_symtab to skip partial CUs -- any important
3547 partial CU will be read via DW_TAG_imported_unit anyway. */
3548 dw2_instantiate_symtab (per_cu, true);
3549 }
3550 }
3551
3552 static void
3553 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3554 const char *fullname)
3555 {
3556 struct dwarf2_per_objfile *dwarf2_per_objfile
3557 = get_dwarf2_per_objfile (objfile);
3558
3559 /* We don't need to consider type units here.
3560 This is only called for examining code, e.g. expand_line_sal.
3561 There can be an order of magnitude (or more) more type units
3562 than comp units, and we avoid them if we can. */
3563
3564 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3565 {
3566 /* We only need to look at symtabs not already expanded. */
3567 if (per_cu->v.quick->compunit_symtab)
3568 continue;
3569
3570 quick_file_names *file_data = dw2_get_file_names (per_cu);
3571 if (file_data == NULL)
3572 continue;
3573
3574 for (int j = 0; j < file_data->num_file_names; ++j)
3575 {
3576 const char *this_fullname = file_data->file_names[j];
3577
3578 if (filename_cmp (this_fullname, fullname) == 0)
3579 {
3580 dw2_instantiate_symtab (per_cu, false);
3581 break;
3582 }
3583 }
3584 }
3585 }
3586
3587 static void
3588 dw2_map_matching_symbols
3589 (struct objfile *objfile,
3590 const lookup_name_info &name, domain_enum domain,
3591 int global,
3592 gdb::function_view<symbol_found_callback_ftype> callback,
3593 symbol_compare_ftype *ordered_compare)
3594 {
3595 /* Used for Ada. */
3596 struct dwarf2_per_objfile *dwarf2_per_objfile
3597 = get_dwarf2_per_objfile (objfile);
3598
3599 if (dwarf2_per_objfile->index_table != nullptr)
3600 {
3601 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3602 here though if the current language is Ada for a non-Ada objfile
3603 using GNU index. As Ada does not look for non-Ada symbols this
3604 function should just return. */
3605 return;
3606 }
3607
3608 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3609 inline psym_map_matching_symbols here, assuming all partial symtabs have
3610 been read in. */
3611 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3612
3613 for (compunit_symtab *cust : objfile->compunits ())
3614 {
3615 const struct block *block;
3616
3617 if (cust == NULL)
3618 continue;
3619 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3620 if (!iterate_over_symbols_terminated (block, name,
3621 domain, callback))
3622 return;
3623 }
3624 }
3625
3626 /* Starting from a search name, return the string that finds the upper
3627 bound of all strings that start with SEARCH_NAME in a sorted name
3628 list. Returns the empty string to indicate that the upper bound is
3629 the end of the list. */
3630
3631 static std::string
3632 make_sort_after_prefix_name (const char *search_name)
3633 {
3634 /* When looking to complete "func", we find the upper bound of all
3635 symbols that start with "func" by looking for where we'd insert
3636 the closest string that would follow "func" in lexicographical
3637 order. Usually, that's "func"-with-last-character-incremented,
3638 i.e. "fund". Mind non-ASCII characters, though. Usually those
3639 will be UTF-8 multi-byte sequences, but we can't be certain.
3640 Especially mind the 0xff character, which is a valid character in
3641 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3642 rule out compilers allowing it in identifiers. Note that
3643 conveniently, strcmp/strcasecmp are specified to compare
3644 characters interpreted as unsigned char. So what we do is treat
3645 the whole string as a base 256 number composed of a sequence of
3646 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3647 to 0, and carries 1 to the following more-significant position.
3648 If the very first character in SEARCH_NAME ends up incremented
3649 and carries/overflows, then the upper bound is the end of the
3650 list. The string after the empty string is also the empty
3651 string.
3652
3653 Some examples of this operation:
3654
3655 SEARCH_NAME => "+1" RESULT
3656
3657 "abc" => "abd"
3658 "ab\xff" => "ac"
3659 "\xff" "a" "\xff" => "\xff" "b"
3660 "\xff" => ""
3661 "\xff\xff" => ""
3662 "" => ""
3663
3664 Then, with these symbols for example:
3665
3666 func
3667 func1
3668 fund
3669
3670 completing "func" looks for symbols between "func" and
3671 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3672 which finds "func" and "func1", but not "fund".
3673
3674 And with:
3675
3676 funcÿ (Latin1 'ÿ' [0xff])
3677 funcÿ1
3678 fund
3679
3680 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3681 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3682
3683 And with:
3684
3685 ÿÿ (Latin1 'ÿ' [0xff])
3686 ÿÿ1
3687
3688 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3689 the end of the list.
3690 */
3691 std::string after = search_name;
3692 while (!after.empty () && (unsigned char) after.back () == 0xff)
3693 after.pop_back ();
3694 if (!after.empty ())
3695 after.back () = (unsigned char) after.back () + 1;
3696 return after;
3697 }
3698
3699 /* See declaration. */
3700
3701 std::pair<std::vector<name_component>::const_iterator,
3702 std::vector<name_component>::const_iterator>
3703 mapped_index_base::find_name_components_bounds
3704 (const lookup_name_info &lookup_name_without_params, language lang) const
3705 {
3706 auto *name_cmp
3707 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3708
3709 const char *lang_name
3710 = lookup_name_without_params.language_lookup_name (lang);
3711
3712 /* Comparison function object for lower_bound that matches against a
3713 given symbol name. */
3714 auto lookup_compare_lower = [&] (const name_component &elem,
3715 const char *name)
3716 {
3717 const char *elem_qualified = this->symbol_name_at (elem.idx);
3718 const char *elem_name = elem_qualified + elem.name_offset;
3719 return name_cmp (elem_name, name) < 0;
3720 };
3721
3722 /* Comparison function object for upper_bound that matches against a
3723 given symbol name. */
3724 auto lookup_compare_upper = [&] (const char *name,
3725 const name_component &elem)
3726 {
3727 const char *elem_qualified = this->symbol_name_at (elem.idx);
3728 const char *elem_name = elem_qualified + elem.name_offset;
3729 return name_cmp (name, elem_name) < 0;
3730 };
3731
3732 auto begin = this->name_components.begin ();
3733 auto end = this->name_components.end ();
3734
3735 /* Find the lower bound. */
3736 auto lower = [&] ()
3737 {
3738 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3739 return begin;
3740 else
3741 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3742 } ();
3743
3744 /* Find the upper bound. */
3745 auto upper = [&] ()
3746 {
3747 if (lookup_name_without_params.completion_mode ())
3748 {
3749 /* In completion mode, we want UPPER to point past all
3750 symbols names that have the same prefix. I.e., with
3751 these symbols, and completing "func":
3752
3753 function << lower bound
3754 function1
3755 other_function << upper bound
3756
3757 We find the upper bound by looking for the insertion
3758 point of "func"-with-last-character-incremented,
3759 i.e. "fund". */
3760 std::string after = make_sort_after_prefix_name (lang_name);
3761 if (after.empty ())
3762 return end;
3763 return std::lower_bound (lower, end, after.c_str (),
3764 lookup_compare_lower);
3765 }
3766 else
3767 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3768 } ();
3769
3770 return {lower, upper};
3771 }
3772
3773 /* See declaration. */
3774
3775 void
3776 mapped_index_base::build_name_components ()
3777 {
3778 if (!this->name_components.empty ())
3779 return;
3780
3781 this->name_components_casing = case_sensitivity;
3782 auto *name_cmp
3783 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3784
3785 /* The code below only knows how to break apart components of C++
3786 symbol names (and other languages that use '::' as
3787 namespace/module separator) and Ada symbol names. */
3788 auto count = this->symbol_name_count ();
3789 for (offset_type idx = 0; idx < count; idx++)
3790 {
3791 if (this->symbol_name_slot_invalid (idx))
3792 continue;
3793
3794 const char *name = this->symbol_name_at (idx);
3795
3796 /* Add each name component to the name component table. */
3797 unsigned int previous_len = 0;
3798
3799 if (strstr (name, "::") != nullptr)
3800 {
3801 for (unsigned int current_len = cp_find_first_component (name);
3802 name[current_len] != '\0';
3803 current_len += cp_find_first_component (name + current_len))
3804 {
3805 gdb_assert (name[current_len] == ':');
3806 this->name_components.push_back ({previous_len, idx});
3807 /* Skip the '::'. */
3808 current_len += 2;
3809 previous_len = current_len;
3810 }
3811 }
3812 else
3813 {
3814 /* Handle the Ada encoded (aka mangled) form here. */
3815 for (const char *iter = strstr (name, "__");
3816 iter != nullptr;
3817 iter = strstr (iter, "__"))
3818 {
3819 this->name_components.push_back ({previous_len, idx});
3820 iter += 2;
3821 previous_len = iter - name;
3822 }
3823 }
3824
3825 this->name_components.push_back ({previous_len, idx});
3826 }
3827
3828 /* Sort name_components elements by name. */
3829 auto name_comp_compare = [&] (const name_component &left,
3830 const name_component &right)
3831 {
3832 const char *left_qualified = this->symbol_name_at (left.idx);
3833 const char *right_qualified = this->symbol_name_at (right.idx);
3834
3835 const char *left_name = left_qualified + left.name_offset;
3836 const char *right_name = right_qualified + right.name_offset;
3837
3838 return name_cmp (left_name, right_name) < 0;
3839 };
3840
3841 std::sort (this->name_components.begin (),
3842 this->name_components.end (),
3843 name_comp_compare);
3844 }
3845
3846 /* Helper for dw2_expand_symtabs_matching that works with a
3847 mapped_index_base instead of the containing objfile. This is split
3848 to a separate function in order to be able to unit test the
3849 name_components matching using a mock mapped_index_base. For each
3850 symbol name that matches, calls MATCH_CALLBACK, passing it the
3851 symbol's index in the mapped_index_base symbol table. */
3852
3853 static void
3854 dw2_expand_symtabs_matching_symbol
3855 (mapped_index_base &index,
3856 const lookup_name_info &lookup_name_in,
3857 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3858 enum search_domain kind,
3859 gdb::function_view<bool (offset_type)> match_callback)
3860 {
3861 lookup_name_info lookup_name_without_params
3862 = lookup_name_in.make_ignore_params ();
3863
3864 /* Build the symbol name component sorted vector, if we haven't
3865 yet. */
3866 index.build_name_components ();
3867
3868 /* The same symbol may appear more than once in the range though.
3869 E.g., if we're looking for symbols that complete "w", and we have
3870 a symbol named "w1::w2", we'll find the two name components for
3871 that same symbol in the range. To be sure we only call the
3872 callback once per symbol, we first collect the symbol name
3873 indexes that matched in a temporary vector and ignore
3874 duplicates. */
3875 std::vector<offset_type> matches;
3876
3877 struct name_and_matcher
3878 {
3879 symbol_name_matcher_ftype *matcher;
3880 const std::string &name;
3881
3882 bool operator== (const name_and_matcher &other) const
3883 {
3884 return matcher == other.matcher && name == other.name;
3885 }
3886 };
3887
3888 /* A vector holding all the different symbol name matchers, for all
3889 languages. */
3890 std::vector<name_and_matcher> matchers;
3891
3892 for (int i = 0; i < nr_languages; i++)
3893 {
3894 enum language lang_e = (enum language) i;
3895
3896 const language_defn *lang = language_def (lang_e);
3897 symbol_name_matcher_ftype *name_matcher
3898 = get_symbol_name_matcher (lang, lookup_name_without_params);
3899
3900 name_and_matcher key {
3901 name_matcher,
3902 lookup_name_without_params.language_lookup_name (lang_e)
3903 };
3904
3905 /* Don't insert the same comparison routine more than once.
3906 Note that we do this linear walk. This is not a problem in
3907 practice because the number of supported languages is
3908 low. */
3909 if (std::find (matchers.begin (), matchers.end (), key)
3910 != matchers.end ())
3911 continue;
3912 matchers.push_back (std::move (key));
3913
3914 auto bounds
3915 = index.find_name_components_bounds (lookup_name_without_params,
3916 lang_e);
3917
3918 /* Now for each symbol name in range, check to see if we have a name
3919 match, and if so, call the MATCH_CALLBACK callback. */
3920
3921 for (; bounds.first != bounds.second; ++bounds.first)
3922 {
3923 const char *qualified = index.symbol_name_at (bounds.first->idx);
3924
3925 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3926 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3927 continue;
3928
3929 matches.push_back (bounds.first->idx);
3930 }
3931 }
3932
3933 std::sort (matches.begin (), matches.end ());
3934
3935 /* Finally call the callback, once per match. */
3936 ULONGEST prev = -1;
3937 for (offset_type idx : matches)
3938 {
3939 if (prev != idx)
3940 {
3941 if (!match_callback (idx))
3942 break;
3943 prev = idx;
3944 }
3945 }
3946
3947 /* Above we use a type wider than idx's for 'prev', since 0 and
3948 (offset_type)-1 are both possible values. */
3949 static_assert (sizeof (prev) > sizeof (offset_type), "");
3950 }
3951
3952 #if GDB_SELF_TEST
3953
3954 namespace selftests { namespace dw2_expand_symtabs_matching {
3955
3956 /* A mock .gdb_index/.debug_names-like name index table, enough to
3957 exercise dw2_expand_symtabs_matching_symbol, which works with the
3958 mapped_index_base interface. Builds an index from the symbol list
3959 passed as parameter to the constructor. */
3960 class mock_mapped_index : public mapped_index_base
3961 {
3962 public:
3963 mock_mapped_index (gdb::array_view<const char *> symbols)
3964 : m_symbol_table (symbols)
3965 {}
3966
3967 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3968
3969 /* Return the number of names in the symbol table. */
3970 size_t symbol_name_count () const override
3971 {
3972 return m_symbol_table.size ();
3973 }
3974
3975 /* Get the name of the symbol at IDX in the symbol table. */
3976 const char *symbol_name_at (offset_type idx) const override
3977 {
3978 return m_symbol_table[idx];
3979 }
3980
3981 private:
3982 gdb::array_view<const char *> m_symbol_table;
3983 };
3984
3985 /* Convenience function that converts a NULL pointer to a "<null>"
3986 string, to pass to print routines. */
3987
3988 static const char *
3989 string_or_null (const char *str)
3990 {
3991 return str != NULL ? str : "<null>";
3992 }
3993
3994 /* Check if a lookup_name_info built from
3995 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3996 index. EXPECTED_LIST is the list of expected matches, in expected
3997 matching order. If no match expected, then an empty list is
3998 specified. Returns true on success. On failure prints a warning
3999 indicating the file:line that failed, and returns false. */
4000
4001 static bool
4002 check_match (const char *file, int line,
4003 mock_mapped_index &mock_index,
4004 const char *name, symbol_name_match_type match_type,
4005 bool completion_mode,
4006 std::initializer_list<const char *> expected_list)
4007 {
4008 lookup_name_info lookup_name (name, match_type, completion_mode);
4009
4010 bool matched = true;
4011
4012 auto mismatch = [&] (const char *expected_str,
4013 const char *got)
4014 {
4015 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4016 "expected=\"%s\", got=\"%s\"\n"),
4017 file, line,
4018 (match_type == symbol_name_match_type::FULL
4019 ? "FULL" : "WILD"),
4020 name, string_or_null (expected_str), string_or_null (got));
4021 matched = false;
4022 };
4023
4024 auto expected_it = expected_list.begin ();
4025 auto expected_end = expected_list.end ();
4026
4027 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4028 NULL, ALL_DOMAIN,
4029 [&] (offset_type idx)
4030 {
4031 const char *matched_name = mock_index.symbol_name_at (idx);
4032 const char *expected_str
4033 = expected_it == expected_end ? NULL : *expected_it++;
4034
4035 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4036 mismatch (expected_str, matched_name);
4037 return true;
4038 });
4039
4040 const char *expected_str
4041 = expected_it == expected_end ? NULL : *expected_it++;
4042 if (expected_str != NULL)
4043 mismatch (expected_str, NULL);
4044
4045 return matched;
4046 }
4047
4048 /* The symbols added to the mock mapped_index for testing (in
4049 canonical form). */
4050 static const char *test_symbols[] = {
4051 "function",
4052 "std::bar",
4053 "std::zfunction",
4054 "std::zfunction2",
4055 "w1::w2",
4056 "ns::foo<char*>",
4057 "ns::foo<int>",
4058 "ns::foo<long>",
4059 "ns2::tmpl<int>::foo2",
4060 "(anonymous namespace)::A::B::C",
4061
4062 /* These are used to check that the increment-last-char in the
4063 matching algorithm for completion doesn't match "t1_fund" when
4064 completing "t1_func". */
4065 "t1_func",
4066 "t1_func1",
4067 "t1_fund",
4068 "t1_fund1",
4069
4070 /* A UTF-8 name with multi-byte sequences to make sure that
4071 cp-name-parser understands this as a single identifier ("função"
4072 is "function" in PT). */
4073 u8"u8função",
4074
4075 /* \377 (0xff) is Latin1 'ÿ'. */
4076 "yfunc\377",
4077
4078 /* \377 (0xff) is Latin1 'ÿ'. */
4079 "\377",
4080 "\377\377123",
4081
4082 /* A name with all sorts of complications. Starts with "z" to make
4083 it easier for the completion tests below. */
4084 #define Z_SYM_NAME \
4085 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4086 "::tuple<(anonymous namespace)::ui*, " \
4087 "std::default_delete<(anonymous namespace)::ui>, void>"
4088
4089 Z_SYM_NAME
4090 };
4091
4092 /* Returns true if the mapped_index_base::find_name_component_bounds
4093 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4094 in completion mode. */
4095
4096 static bool
4097 check_find_bounds_finds (mapped_index_base &index,
4098 const char *search_name,
4099 gdb::array_view<const char *> expected_syms)
4100 {
4101 lookup_name_info lookup_name (search_name,
4102 symbol_name_match_type::FULL, true);
4103
4104 auto bounds = index.find_name_components_bounds (lookup_name,
4105 language_cplus);
4106
4107 size_t distance = std::distance (bounds.first, bounds.second);
4108 if (distance != expected_syms.size ())
4109 return false;
4110
4111 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4112 {
4113 auto nc_elem = bounds.first + exp_elem;
4114 const char *qualified = index.symbol_name_at (nc_elem->idx);
4115 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4116 return false;
4117 }
4118
4119 return true;
4120 }
4121
4122 /* Test the lower-level mapped_index::find_name_component_bounds
4123 method. */
4124
4125 static void
4126 test_mapped_index_find_name_component_bounds ()
4127 {
4128 mock_mapped_index mock_index (test_symbols);
4129
4130 mock_index.build_name_components ();
4131
4132 /* Test the lower-level mapped_index::find_name_component_bounds
4133 method in completion mode. */
4134 {
4135 static const char *expected_syms[] = {
4136 "t1_func",
4137 "t1_func1",
4138 };
4139
4140 SELF_CHECK (check_find_bounds_finds (mock_index,
4141 "t1_func", expected_syms));
4142 }
4143
4144 /* Check that the increment-last-char in the name matching algorithm
4145 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4146 {
4147 static const char *expected_syms1[] = {
4148 "\377",
4149 "\377\377123",
4150 };
4151 SELF_CHECK (check_find_bounds_finds (mock_index,
4152 "\377", expected_syms1));
4153
4154 static const char *expected_syms2[] = {
4155 "\377\377123",
4156 };
4157 SELF_CHECK (check_find_bounds_finds (mock_index,
4158 "\377\377", expected_syms2));
4159 }
4160 }
4161
4162 /* Test dw2_expand_symtabs_matching_symbol. */
4163
4164 static void
4165 test_dw2_expand_symtabs_matching_symbol ()
4166 {
4167 mock_mapped_index mock_index (test_symbols);
4168
4169 /* We let all tests run until the end even if some fails, for debug
4170 convenience. */
4171 bool any_mismatch = false;
4172
4173 /* Create the expected symbols list (an initializer_list). Needed
4174 because lists have commas, and we need to pass them to CHECK,
4175 which is a macro. */
4176 #define EXPECT(...) { __VA_ARGS__ }
4177
4178 /* Wrapper for check_match that passes down the current
4179 __FILE__/__LINE__. */
4180 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4181 any_mismatch |= !check_match (__FILE__, __LINE__, \
4182 mock_index, \
4183 NAME, MATCH_TYPE, COMPLETION_MODE, \
4184 EXPECTED_LIST)
4185
4186 /* Identity checks. */
4187 for (const char *sym : test_symbols)
4188 {
4189 /* Should be able to match all existing symbols. */
4190 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4191 EXPECT (sym));
4192
4193 /* Should be able to match all existing symbols with
4194 parameters. */
4195 std::string with_params = std::string (sym) + "(int)";
4196 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4197 EXPECT (sym));
4198
4199 /* Should be able to match all existing symbols with
4200 parameters and qualifiers. */
4201 with_params = std::string (sym) + " ( int ) const";
4202 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4203 EXPECT (sym));
4204
4205 /* This should really find sym, but cp-name-parser.y doesn't
4206 know about lvalue/rvalue qualifiers yet. */
4207 with_params = std::string (sym) + " ( int ) &&";
4208 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4209 {});
4210 }
4211
4212 /* Check that the name matching algorithm for completion doesn't get
4213 confused with Latin1 'ÿ' / 0xff. */
4214 {
4215 static const char str[] = "\377";
4216 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4217 EXPECT ("\377", "\377\377123"));
4218 }
4219
4220 /* Check that the increment-last-char in the matching algorithm for
4221 completion doesn't match "t1_fund" when completing "t1_func". */
4222 {
4223 static const char str[] = "t1_func";
4224 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4225 EXPECT ("t1_func", "t1_func1"));
4226 }
4227
4228 /* Check that completion mode works at each prefix of the expected
4229 symbol name. */
4230 {
4231 static const char str[] = "function(int)";
4232 size_t len = strlen (str);
4233 std::string lookup;
4234
4235 for (size_t i = 1; i < len; i++)
4236 {
4237 lookup.assign (str, i);
4238 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4239 EXPECT ("function"));
4240 }
4241 }
4242
4243 /* While "w" is a prefix of both components, the match function
4244 should still only be called once. */
4245 {
4246 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4247 EXPECT ("w1::w2"));
4248 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4249 EXPECT ("w1::w2"));
4250 }
4251
4252 /* Same, with a "complicated" symbol. */
4253 {
4254 static const char str[] = Z_SYM_NAME;
4255 size_t len = strlen (str);
4256 std::string lookup;
4257
4258 for (size_t i = 1; i < len; i++)
4259 {
4260 lookup.assign (str, i);
4261 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4262 EXPECT (Z_SYM_NAME));
4263 }
4264 }
4265
4266 /* In FULL mode, an incomplete symbol doesn't match. */
4267 {
4268 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4269 {});
4270 }
4271
4272 /* A complete symbol with parameters matches any overload, since the
4273 index has no overload info. */
4274 {
4275 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4276 EXPECT ("std::zfunction", "std::zfunction2"));
4277 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4278 EXPECT ("std::zfunction", "std::zfunction2"));
4279 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4280 EXPECT ("std::zfunction", "std::zfunction2"));
4281 }
4282
4283 /* Check that whitespace is ignored appropriately. A symbol with a
4284 template argument list. */
4285 {
4286 static const char expected[] = "ns::foo<int>";
4287 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4288 EXPECT (expected));
4289 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4290 EXPECT (expected));
4291 }
4292
4293 /* Check that whitespace is ignored appropriately. A symbol with a
4294 template argument list that includes a pointer. */
4295 {
4296 static const char expected[] = "ns::foo<char*>";
4297 /* Try both completion and non-completion modes. */
4298 static const bool completion_mode[2] = {false, true};
4299 for (size_t i = 0; i < 2; i++)
4300 {
4301 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4302 completion_mode[i], EXPECT (expected));
4303 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4304 completion_mode[i], EXPECT (expected));
4305
4306 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4307 completion_mode[i], EXPECT (expected));
4308 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4309 completion_mode[i], EXPECT (expected));
4310 }
4311 }
4312
4313 {
4314 /* Check method qualifiers are ignored. */
4315 static const char expected[] = "ns::foo<char*>";
4316 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4317 symbol_name_match_type::FULL, true, EXPECT (expected));
4318 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4319 symbol_name_match_type::FULL, true, EXPECT (expected));
4320 CHECK_MATCH ("foo < char * > ( int ) const",
4321 symbol_name_match_type::WILD, true, EXPECT (expected));
4322 CHECK_MATCH ("foo < char * > ( int ) &&",
4323 symbol_name_match_type::WILD, true, EXPECT (expected));
4324 }
4325
4326 /* Test lookup names that don't match anything. */
4327 {
4328 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4329 {});
4330
4331 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4332 {});
4333 }
4334
4335 /* Some wild matching tests, exercising "(anonymous namespace)",
4336 which should not be confused with a parameter list. */
4337 {
4338 static const char *syms[] = {
4339 "A::B::C",
4340 "B::C",
4341 "C",
4342 "A :: B :: C ( int )",
4343 "B :: C ( int )",
4344 "C ( int )",
4345 };
4346
4347 for (const char *s : syms)
4348 {
4349 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4350 EXPECT ("(anonymous namespace)::A::B::C"));
4351 }
4352 }
4353
4354 {
4355 static const char expected[] = "ns2::tmpl<int>::foo2";
4356 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4357 EXPECT (expected));
4358 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4359 EXPECT (expected));
4360 }
4361
4362 SELF_CHECK (!any_mismatch);
4363
4364 #undef EXPECT
4365 #undef CHECK_MATCH
4366 }
4367
4368 static void
4369 run_test ()
4370 {
4371 test_mapped_index_find_name_component_bounds ();
4372 test_dw2_expand_symtabs_matching_symbol ();
4373 }
4374
4375 }} // namespace selftests::dw2_expand_symtabs_matching
4376
4377 #endif /* GDB_SELF_TEST */
4378
4379 /* If FILE_MATCHER is NULL or if PER_CU has
4380 dwarf2_per_cu_quick_data::MARK set (see
4381 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4382 EXPANSION_NOTIFY on it. */
4383
4384 static void
4385 dw2_expand_symtabs_matching_one
4386 (struct dwarf2_per_cu_data *per_cu,
4387 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4388 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4389 {
4390 if (file_matcher == NULL || per_cu->v.quick->mark)
4391 {
4392 bool symtab_was_null
4393 = (per_cu->v.quick->compunit_symtab == NULL);
4394
4395 dw2_instantiate_symtab (per_cu, false);
4396
4397 if (expansion_notify != NULL
4398 && symtab_was_null
4399 && per_cu->v.quick->compunit_symtab != NULL)
4400 expansion_notify (per_cu->v.quick->compunit_symtab);
4401 }
4402 }
4403
4404 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4405 matched, to expand corresponding CUs that were marked. IDX is the
4406 index of the symbol name that matched. */
4407
4408 static void
4409 dw2_expand_marked_cus
4410 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4411 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4412 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4413 search_domain kind)
4414 {
4415 offset_type *vec, vec_len, vec_idx;
4416 bool global_seen = false;
4417 mapped_index &index = *dwarf2_per_objfile->index_table;
4418
4419 vec = (offset_type *) (index.constant_pool
4420 + MAYBE_SWAP (index.symbol_table[idx].vec));
4421 vec_len = MAYBE_SWAP (vec[0]);
4422 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4423 {
4424 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4425 /* This value is only valid for index versions >= 7. */
4426 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4427 gdb_index_symbol_kind symbol_kind =
4428 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4429 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4430 /* Only check the symbol attributes if they're present.
4431 Indices prior to version 7 don't record them,
4432 and indices >= 7 may elide them for certain symbols
4433 (gold does this). */
4434 int attrs_valid =
4435 (index.version >= 7
4436 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4437
4438 /* Work around gold/15646. */
4439 if (attrs_valid)
4440 {
4441 if (!is_static && global_seen)
4442 continue;
4443 if (!is_static)
4444 global_seen = true;
4445 }
4446
4447 /* Only check the symbol's kind if it has one. */
4448 if (attrs_valid)
4449 {
4450 switch (kind)
4451 {
4452 case VARIABLES_DOMAIN:
4453 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4454 continue;
4455 break;
4456 case FUNCTIONS_DOMAIN:
4457 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4458 continue;
4459 break;
4460 case TYPES_DOMAIN:
4461 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4462 continue;
4463 break;
4464 case MODULES_DOMAIN:
4465 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4466 continue;
4467 break;
4468 default:
4469 break;
4470 }
4471 }
4472
4473 /* Don't crash on bad data. */
4474 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4475 + dwarf2_per_objfile->all_type_units.size ()))
4476 {
4477 complaint (_(".gdb_index entry has bad CU index"
4478 " [in module %s]"),
4479 objfile_name (dwarf2_per_objfile->objfile));
4480 continue;
4481 }
4482
4483 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4484 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4485 expansion_notify);
4486 }
4487 }
4488
4489 /* If FILE_MATCHER is non-NULL, set all the
4490 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4491 that match FILE_MATCHER. */
4492
4493 static void
4494 dw_expand_symtabs_matching_file_matcher
4495 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4496 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4497 {
4498 if (file_matcher == NULL)
4499 return;
4500
4501 objfile *const objfile = dwarf2_per_objfile->objfile;
4502
4503 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4504 htab_eq_pointer,
4505 NULL, xcalloc, xfree));
4506 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4507 htab_eq_pointer,
4508 NULL, xcalloc, xfree));
4509
4510 /* The rule is CUs specify all the files, including those used by
4511 any TU, so there's no need to scan TUs here. */
4512
4513 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4514 {
4515 QUIT;
4516
4517 per_cu->v.quick->mark = 0;
4518
4519 /* We only need to look at symtabs not already expanded. */
4520 if (per_cu->v.quick->compunit_symtab)
4521 continue;
4522
4523 quick_file_names *file_data = dw2_get_file_names (per_cu);
4524 if (file_data == NULL)
4525 continue;
4526
4527 if (htab_find (visited_not_found.get (), file_data) != NULL)
4528 continue;
4529 else if (htab_find (visited_found.get (), file_data) != NULL)
4530 {
4531 per_cu->v.quick->mark = 1;
4532 continue;
4533 }
4534
4535 for (int j = 0; j < file_data->num_file_names; ++j)
4536 {
4537 const char *this_real_name;
4538
4539 if (file_matcher (file_data->file_names[j], false))
4540 {
4541 per_cu->v.quick->mark = 1;
4542 break;
4543 }
4544
4545 /* Before we invoke realpath, which can get expensive when many
4546 files are involved, do a quick comparison of the basenames. */
4547 if (!basenames_may_differ
4548 && !file_matcher (lbasename (file_data->file_names[j]),
4549 true))
4550 continue;
4551
4552 this_real_name = dw2_get_real_path (objfile, file_data, j);
4553 if (file_matcher (this_real_name, false))
4554 {
4555 per_cu->v.quick->mark = 1;
4556 break;
4557 }
4558 }
4559
4560 void **slot = htab_find_slot (per_cu->v.quick->mark
4561 ? visited_found.get ()
4562 : visited_not_found.get (),
4563 file_data, INSERT);
4564 *slot = file_data;
4565 }
4566 }
4567
4568 static void
4569 dw2_expand_symtabs_matching
4570 (struct objfile *objfile,
4571 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4572 const lookup_name_info &lookup_name,
4573 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4574 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4575 enum search_domain kind)
4576 {
4577 struct dwarf2_per_objfile *dwarf2_per_objfile
4578 = get_dwarf2_per_objfile (objfile);
4579
4580 /* index_table is NULL if OBJF_READNOW. */
4581 if (!dwarf2_per_objfile->index_table)
4582 return;
4583
4584 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4585
4586 mapped_index &index = *dwarf2_per_objfile->index_table;
4587
4588 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4589 symbol_matcher,
4590 kind, [&] (offset_type idx)
4591 {
4592 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4593 expansion_notify, kind);
4594 return true;
4595 });
4596 }
4597
4598 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4599 symtab. */
4600
4601 static struct compunit_symtab *
4602 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4603 CORE_ADDR pc)
4604 {
4605 int i;
4606
4607 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4608 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4609 return cust;
4610
4611 if (cust->includes == NULL)
4612 return NULL;
4613
4614 for (i = 0; cust->includes[i]; ++i)
4615 {
4616 struct compunit_symtab *s = cust->includes[i];
4617
4618 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4619 if (s != NULL)
4620 return s;
4621 }
4622
4623 return NULL;
4624 }
4625
4626 static struct compunit_symtab *
4627 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4628 struct bound_minimal_symbol msymbol,
4629 CORE_ADDR pc,
4630 struct obj_section *section,
4631 int warn_if_readin)
4632 {
4633 struct dwarf2_per_cu_data *data;
4634 struct compunit_symtab *result;
4635
4636 if (!objfile->partial_symtabs->psymtabs_addrmap)
4637 return NULL;
4638
4639 CORE_ADDR baseaddr = objfile->text_section_offset ();
4640 data = (struct dwarf2_per_cu_data *) addrmap_find
4641 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4642 if (!data)
4643 return NULL;
4644
4645 if (warn_if_readin && data->v.quick->compunit_symtab)
4646 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4647 paddress (get_objfile_arch (objfile), pc));
4648
4649 result
4650 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4651 false),
4652 pc);
4653 gdb_assert (result != NULL);
4654 return result;
4655 }
4656
4657 static void
4658 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4659 void *data, int need_fullname)
4660 {
4661 struct dwarf2_per_objfile *dwarf2_per_objfile
4662 = get_dwarf2_per_objfile (objfile);
4663
4664 if (!dwarf2_per_objfile->filenames_cache)
4665 {
4666 dwarf2_per_objfile->filenames_cache.emplace ();
4667
4668 htab_up visited (htab_create_alloc (10,
4669 htab_hash_pointer, htab_eq_pointer,
4670 NULL, xcalloc, xfree));
4671
4672 /* The rule is CUs specify all the files, including those used
4673 by any TU, so there's no need to scan TUs here. We can
4674 ignore file names coming from already-expanded CUs. */
4675
4676 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4677 {
4678 if (per_cu->v.quick->compunit_symtab)
4679 {
4680 void **slot = htab_find_slot (visited.get (),
4681 per_cu->v.quick->file_names,
4682 INSERT);
4683
4684 *slot = per_cu->v.quick->file_names;
4685 }
4686 }
4687
4688 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4689 {
4690 /* We only need to look at symtabs not already expanded. */
4691 if (per_cu->v.quick->compunit_symtab)
4692 continue;
4693
4694 quick_file_names *file_data = dw2_get_file_names (per_cu);
4695 if (file_data == NULL)
4696 continue;
4697
4698 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4699 if (*slot)
4700 {
4701 /* Already visited. */
4702 continue;
4703 }
4704 *slot = file_data;
4705
4706 for (int j = 0; j < file_data->num_file_names; ++j)
4707 {
4708 const char *filename = file_data->file_names[j];
4709 dwarf2_per_objfile->filenames_cache->seen (filename);
4710 }
4711 }
4712 }
4713
4714 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4715 {
4716 gdb::unique_xmalloc_ptr<char> this_real_name;
4717
4718 if (need_fullname)
4719 this_real_name = gdb_realpath (filename);
4720 (*fun) (filename, this_real_name.get (), data);
4721 });
4722 }
4723
4724 static int
4725 dw2_has_symbols (struct objfile *objfile)
4726 {
4727 return 1;
4728 }
4729
4730 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4731 {
4732 dw2_has_symbols,
4733 dw2_find_last_source_symtab,
4734 dw2_forget_cached_source_info,
4735 dw2_map_symtabs_matching_filename,
4736 dw2_lookup_symbol,
4737 NULL,
4738 dw2_print_stats,
4739 dw2_dump,
4740 dw2_expand_symtabs_for_function,
4741 dw2_expand_all_symtabs,
4742 dw2_expand_symtabs_with_fullname,
4743 dw2_map_matching_symbols,
4744 dw2_expand_symtabs_matching,
4745 dw2_find_pc_sect_compunit_symtab,
4746 NULL,
4747 dw2_map_symbol_filenames
4748 };
4749
4750 /* DWARF-5 debug_names reader. */
4751
4752 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4753 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4754
4755 /* A helper function that reads the .debug_names section in SECTION
4756 and fills in MAP. FILENAME is the name of the file containing the
4757 section; it is used for error reporting.
4758
4759 Returns true if all went well, false otherwise. */
4760
4761 static bool
4762 read_debug_names_from_section (struct objfile *objfile,
4763 const char *filename,
4764 struct dwarf2_section_info *section,
4765 mapped_debug_names &map)
4766 {
4767 if (section->empty ())
4768 return false;
4769
4770 /* Older elfutils strip versions could keep the section in the main
4771 executable while splitting it for the separate debug info file. */
4772 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4773 return false;
4774
4775 section->read (objfile);
4776
4777 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4778
4779 const gdb_byte *addr = section->buffer;
4780
4781 bfd *const abfd = section->get_bfd_owner ();
4782
4783 unsigned int bytes_read;
4784 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4785 addr += bytes_read;
4786
4787 map.dwarf5_is_dwarf64 = bytes_read != 4;
4788 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4789 if (bytes_read + length != section->size)
4790 {
4791 /* There may be multiple per-CU indices. */
4792 warning (_("Section .debug_names in %s length %s does not match "
4793 "section length %s, ignoring .debug_names."),
4794 filename, plongest (bytes_read + length),
4795 pulongest (section->size));
4796 return false;
4797 }
4798
4799 /* The version number. */
4800 uint16_t version = read_2_bytes (abfd, addr);
4801 addr += 2;
4802 if (version != 5)
4803 {
4804 warning (_("Section .debug_names in %s has unsupported version %d, "
4805 "ignoring .debug_names."),
4806 filename, version);
4807 return false;
4808 }
4809
4810 /* Padding. */
4811 uint16_t padding = read_2_bytes (abfd, addr);
4812 addr += 2;
4813 if (padding != 0)
4814 {
4815 warning (_("Section .debug_names in %s has unsupported padding %d, "
4816 "ignoring .debug_names."),
4817 filename, padding);
4818 return false;
4819 }
4820
4821 /* comp_unit_count - The number of CUs in the CU list. */
4822 map.cu_count = read_4_bytes (abfd, addr);
4823 addr += 4;
4824
4825 /* local_type_unit_count - The number of TUs in the local TU
4826 list. */
4827 map.tu_count = read_4_bytes (abfd, addr);
4828 addr += 4;
4829
4830 /* foreign_type_unit_count - The number of TUs in the foreign TU
4831 list. */
4832 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4833 addr += 4;
4834 if (foreign_tu_count != 0)
4835 {
4836 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4837 "ignoring .debug_names."),
4838 filename, static_cast<unsigned long> (foreign_tu_count));
4839 return false;
4840 }
4841
4842 /* bucket_count - The number of hash buckets in the hash lookup
4843 table. */
4844 map.bucket_count = read_4_bytes (abfd, addr);
4845 addr += 4;
4846
4847 /* name_count - The number of unique names in the index. */
4848 map.name_count = read_4_bytes (abfd, addr);
4849 addr += 4;
4850
4851 /* abbrev_table_size - The size in bytes of the abbreviations
4852 table. */
4853 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4854 addr += 4;
4855
4856 /* augmentation_string_size - The size in bytes of the augmentation
4857 string. This value is rounded up to a multiple of 4. */
4858 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4859 addr += 4;
4860 map.augmentation_is_gdb = ((augmentation_string_size
4861 == sizeof (dwarf5_augmentation))
4862 && memcmp (addr, dwarf5_augmentation,
4863 sizeof (dwarf5_augmentation)) == 0);
4864 augmentation_string_size += (-augmentation_string_size) & 3;
4865 addr += augmentation_string_size;
4866
4867 /* List of CUs */
4868 map.cu_table_reordered = addr;
4869 addr += map.cu_count * map.offset_size;
4870
4871 /* List of Local TUs */
4872 map.tu_table_reordered = addr;
4873 addr += map.tu_count * map.offset_size;
4874
4875 /* Hash Lookup Table */
4876 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4877 addr += map.bucket_count * 4;
4878 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4879 addr += map.name_count * 4;
4880
4881 /* Name Table */
4882 map.name_table_string_offs_reordered = addr;
4883 addr += map.name_count * map.offset_size;
4884 map.name_table_entry_offs_reordered = addr;
4885 addr += map.name_count * map.offset_size;
4886
4887 const gdb_byte *abbrev_table_start = addr;
4888 for (;;)
4889 {
4890 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4891 addr += bytes_read;
4892 if (index_num == 0)
4893 break;
4894
4895 const auto insertpair
4896 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4897 if (!insertpair.second)
4898 {
4899 warning (_("Section .debug_names in %s has duplicate index %s, "
4900 "ignoring .debug_names."),
4901 filename, pulongest (index_num));
4902 return false;
4903 }
4904 mapped_debug_names::index_val &indexval = insertpair.first->second;
4905 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4906 addr += bytes_read;
4907
4908 for (;;)
4909 {
4910 mapped_debug_names::index_val::attr attr;
4911 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4912 addr += bytes_read;
4913 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4914 addr += bytes_read;
4915 if (attr.form == DW_FORM_implicit_const)
4916 {
4917 attr.implicit_const = read_signed_leb128 (abfd, addr,
4918 &bytes_read);
4919 addr += bytes_read;
4920 }
4921 if (attr.dw_idx == 0 && attr.form == 0)
4922 break;
4923 indexval.attr_vec.push_back (std::move (attr));
4924 }
4925 }
4926 if (addr != abbrev_table_start + abbrev_table_size)
4927 {
4928 warning (_("Section .debug_names in %s has abbreviation_table "
4929 "of size %s vs. written as %u, ignoring .debug_names."),
4930 filename, plongest (addr - abbrev_table_start),
4931 abbrev_table_size);
4932 return false;
4933 }
4934 map.entry_pool = addr;
4935
4936 return true;
4937 }
4938
4939 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4940 list. */
4941
4942 static void
4943 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4944 const mapped_debug_names &map,
4945 dwarf2_section_info &section,
4946 bool is_dwz)
4947 {
4948 sect_offset sect_off_prev;
4949 for (uint32_t i = 0; i <= map.cu_count; ++i)
4950 {
4951 sect_offset sect_off_next;
4952 if (i < map.cu_count)
4953 {
4954 sect_off_next
4955 = (sect_offset) (extract_unsigned_integer
4956 (map.cu_table_reordered + i * map.offset_size,
4957 map.offset_size,
4958 map.dwarf5_byte_order));
4959 }
4960 else
4961 sect_off_next = (sect_offset) section.size;
4962 if (i >= 1)
4963 {
4964 const ULONGEST length = sect_off_next - sect_off_prev;
4965 dwarf2_per_cu_data *per_cu
4966 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
4967 sect_off_prev, length);
4968 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
4969 }
4970 sect_off_prev = sect_off_next;
4971 }
4972 }
4973
4974 /* Read the CU list from the mapped index, and use it to create all
4975 the CU objects for this dwarf2_per_objfile. */
4976
4977 static void
4978 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
4979 const mapped_debug_names &map,
4980 const mapped_debug_names &dwz_map)
4981 {
4982 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
4983 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4984
4985 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
4986 dwarf2_per_objfile->info,
4987 false /* is_dwz */);
4988
4989 if (dwz_map.cu_count == 0)
4990 return;
4991
4992 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4993 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
4994 true /* is_dwz */);
4995 }
4996
4997 /* Read .debug_names. If everything went ok, initialize the "quick"
4998 elements of all the CUs and return true. Otherwise, return false. */
4999
5000 static bool
5001 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5002 {
5003 std::unique_ptr<mapped_debug_names> map
5004 (new mapped_debug_names (dwarf2_per_objfile));
5005 mapped_debug_names dwz_map (dwarf2_per_objfile);
5006 struct objfile *objfile = dwarf2_per_objfile->objfile;
5007
5008 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5009 &dwarf2_per_objfile->debug_names,
5010 *map))
5011 return false;
5012
5013 /* Don't use the index if it's empty. */
5014 if (map->name_count == 0)
5015 return false;
5016
5017 /* If there is a .dwz file, read it so we can get its CU list as
5018 well. */
5019 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5020 if (dwz != NULL)
5021 {
5022 if (!read_debug_names_from_section (objfile,
5023 bfd_get_filename (dwz->dwz_bfd.get ()),
5024 &dwz->debug_names, dwz_map))
5025 {
5026 warning (_("could not read '.debug_names' section from %s; skipping"),
5027 bfd_get_filename (dwz->dwz_bfd.get ()));
5028 return false;
5029 }
5030 }
5031
5032 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5033
5034 if (map->tu_count != 0)
5035 {
5036 /* We can only handle a single .debug_types when we have an
5037 index. */
5038 if (dwarf2_per_objfile->types.size () != 1)
5039 return false;
5040
5041 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5042
5043 create_signatured_type_table_from_debug_names
5044 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5045 }
5046
5047 create_addrmap_from_aranges (dwarf2_per_objfile,
5048 &dwarf2_per_objfile->debug_aranges);
5049
5050 dwarf2_per_objfile->debug_names_table = std::move (map);
5051 dwarf2_per_objfile->using_index = 1;
5052 dwarf2_per_objfile->quick_file_names_table =
5053 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5054
5055 return true;
5056 }
5057
5058 /* Type used to manage iterating over all CUs looking for a symbol for
5059 .debug_names. */
5060
5061 class dw2_debug_names_iterator
5062 {
5063 public:
5064 dw2_debug_names_iterator (const mapped_debug_names &map,
5065 gdb::optional<block_enum> block_index,
5066 domain_enum domain,
5067 const char *name)
5068 : m_map (map), m_block_index (block_index), m_domain (domain),
5069 m_addr (find_vec_in_debug_names (map, name))
5070 {}
5071
5072 dw2_debug_names_iterator (const mapped_debug_names &map,
5073 search_domain search, uint32_t namei)
5074 : m_map (map),
5075 m_search (search),
5076 m_addr (find_vec_in_debug_names (map, namei))
5077 {}
5078
5079 dw2_debug_names_iterator (const mapped_debug_names &map,
5080 block_enum block_index, domain_enum domain,
5081 uint32_t namei)
5082 : m_map (map), m_block_index (block_index), m_domain (domain),
5083 m_addr (find_vec_in_debug_names (map, namei))
5084 {}
5085
5086 /* Return the next matching CU or NULL if there are no more. */
5087 dwarf2_per_cu_data *next ();
5088
5089 private:
5090 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5091 const char *name);
5092 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5093 uint32_t namei);
5094
5095 /* The internalized form of .debug_names. */
5096 const mapped_debug_names &m_map;
5097
5098 /* If set, only look for symbols that match that block. Valid values are
5099 GLOBAL_BLOCK and STATIC_BLOCK. */
5100 const gdb::optional<block_enum> m_block_index;
5101
5102 /* The kind of symbol we're looking for. */
5103 const domain_enum m_domain = UNDEF_DOMAIN;
5104 const search_domain m_search = ALL_DOMAIN;
5105
5106 /* The list of CUs from the index entry of the symbol, or NULL if
5107 not found. */
5108 const gdb_byte *m_addr;
5109 };
5110
5111 const char *
5112 mapped_debug_names::namei_to_name (uint32_t namei) const
5113 {
5114 const ULONGEST namei_string_offs
5115 = extract_unsigned_integer ((name_table_string_offs_reordered
5116 + namei * offset_size),
5117 offset_size,
5118 dwarf5_byte_order);
5119 return read_indirect_string_at_offset (dwarf2_per_objfile,
5120 namei_string_offs);
5121 }
5122
5123 /* Find a slot in .debug_names for the object named NAME. If NAME is
5124 found, return pointer to its pool data. If NAME cannot be found,
5125 return NULL. */
5126
5127 const gdb_byte *
5128 dw2_debug_names_iterator::find_vec_in_debug_names
5129 (const mapped_debug_names &map, const char *name)
5130 {
5131 int (*cmp) (const char *, const char *);
5132
5133 gdb::unique_xmalloc_ptr<char> without_params;
5134 if (current_language->la_language == language_cplus
5135 || current_language->la_language == language_fortran
5136 || current_language->la_language == language_d)
5137 {
5138 /* NAME is already canonical. Drop any qualifiers as
5139 .debug_names does not contain any. */
5140
5141 if (strchr (name, '(') != NULL)
5142 {
5143 without_params = cp_remove_params (name);
5144 if (without_params != NULL)
5145 name = without_params.get ();
5146 }
5147 }
5148
5149 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5150
5151 const uint32_t full_hash = dwarf5_djb_hash (name);
5152 uint32_t namei
5153 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5154 (map.bucket_table_reordered
5155 + (full_hash % map.bucket_count)), 4,
5156 map.dwarf5_byte_order);
5157 if (namei == 0)
5158 return NULL;
5159 --namei;
5160 if (namei >= map.name_count)
5161 {
5162 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5163 "[in module %s]"),
5164 namei, map.name_count,
5165 objfile_name (map.dwarf2_per_objfile->objfile));
5166 return NULL;
5167 }
5168
5169 for (;;)
5170 {
5171 const uint32_t namei_full_hash
5172 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5173 (map.hash_table_reordered + namei), 4,
5174 map.dwarf5_byte_order);
5175 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5176 return NULL;
5177
5178 if (full_hash == namei_full_hash)
5179 {
5180 const char *const namei_string = map.namei_to_name (namei);
5181
5182 #if 0 /* An expensive sanity check. */
5183 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5184 {
5185 complaint (_("Wrong .debug_names hash for string at index %u "
5186 "[in module %s]"),
5187 namei, objfile_name (dwarf2_per_objfile->objfile));
5188 return NULL;
5189 }
5190 #endif
5191
5192 if (cmp (namei_string, name) == 0)
5193 {
5194 const ULONGEST namei_entry_offs
5195 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5196 + namei * map.offset_size),
5197 map.offset_size, map.dwarf5_byte_order);
5198 return map.entry_pool + namei_entry_offs;
5199 }
5200 }
5201
5202 ++namei;
5203 if (namei >= map.name_count)
5204 return NULL;
5205 }
5206 }
5207
5208 const gdb_byte *
5209 dw2_debug_names_iterator::find_vec_in_debug_names
5210 (const mapped_debug_names &map, uint32_t namei)
5211 {
5212 if (namei >= map.name_count)
5213 {
5214 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5215 "[in module %s]"),
5216 namei, map.name_count,
5217 objfile_name (map.dwarf2_per_objfile->objfile));
5218 return NULL;
5219 }
5220
5221 const ULONGEST namei_entry_offs
5222 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5223 + namei * map.offset_size),
5224 map.offset_size, map.dwarf5_byte_order);
5225 return map.entry_pool + namei_entry_offs;
5226 }
5227
5228 /* See dw2_debug_names_iterator. */
5229
5230 dwarf2_per_cu_data *
5231 dw2_debug_names_iterator::next ()
5232 {
5233 if (m_addr == NULL)
5234 return NULL;
5235
5236 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5237 struct objfile *objfile = dwarf2_per_objfile->objfile;
5238 bfd *const abfd = objfile->obfd;
5239
5240 again:
5241
5242 unsigned int bytes_read;
5243 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5244 m_addr += bytes_read;
5245 if (abbrev == 0)
5246 return NULL;
5247
5248 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5249 if (indexval_it == m_map.abbrev_map.cend ())
5250 {
5251 complaint (_("Wrong .debug_names undefined abbrev code %s "
5252 "[in module %s]"),
5253 pulongest (abbrev), objfile_name (objfile));
5254 return NULL;
5255 }
5256 const mapped_debug_names::index_val &indexval = indexval_it->second;
5257 enum class symbol_linkage {
5258 unknown,
5259 static_,
5260 extern_,
5261 } symbol_linkage_ = symbol_linkage::unknown;
5262 dwarf2_per_cu_data *per_cu = NULL;
5263 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5264 {
5265 ULONGEST ull;
5266 switch (attr.form)
5267 {
5268 case DW_FORM_implicit_const:
5269 ull = attr.implicit_const;
5270 break;
5271 case DW_FORM_flag_present:
5272 ull = 1;
5273 break;
5274 case DW_FORM_udata:
5275 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5276 m_addr += bytes_read;
5277 break;
5278 default:
5279 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5280 dwarf_form_name (attr.form),
5281 objfile_name (objfile));
5282 return NULL;
5283 }
5284 switch (attr.dw_idx)
5285 {
5286 case DW_IDX_compile_unit:
5287 /* Don't crash on bad data. */
5288 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5289 {
5290 complaint (_(".debug_names entry has bad CU index %s"
5291 " [in module %s]"),
5292 pulongest (ull),
5293 objfile_name (dwarf2_per_objfile->objfile));
5294 continue;
5295 }
5296 per_cu = dwarf2_per_objfile->get_cutu (ull);
5297 break;
5298 case DW_IDX_type_unit:
5299 /* Don't crash on bad data. */
5300 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5301 {
5302 complaint (_(".debug_names entry has bad TU index %s"
5303 " [in module %s]"),
5304 pulongest (ull),
5305 objfile_name (dwarf2_per_objfile->objfile));
5306 continue;
5307 }
5308 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5309 break;
5310 case DW_IDX_GNU_internal:
5311 if (!m_map.augmentation_is_gdb)
5312 break;
5313 symbol_linkage_ = symbol_linkage::static_;
5314 break;
5315 case DW_IDX_GNU_external:
5316 if (!m_map.augmentation_is_gdb)
5317 break;
5318 symbol_linkage_ = symbol_linkage::extern_;
5319 break;
5320 }
5321 }
5322
5323 /* Skip if already read in. */
5324 if (per_cu->v.quick->compunit_symtab)
5325 goto again;
5326
5327 /* Check static vs global. */
5328 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5329 {
5330 const bool want_static = *m_block_index == STATIC_BLOCK;
5331 const bool symbol_is_static =
5332 symbol_linkage_ == symbol_linkage::static_;
5333 if (want_static != symbol_is_static)
5334 goto again;
5335 }
5336
5337 /* Match dw2_symtab_iter_next, symbol_kind
5338 and debug_names::psymbol_tag. */
5339 switch (m_domain)
5340 {
5341 case VAR_DOMAIN:
5342 switch (indexval.dwarf_tag)
5343 {
5344 case DW_TAG_variable:
5345 case DW_TAG_subprogram:
5346 /* Some types are also in VAR_DOMAIN. */
5347 case DW_TAG_typedef:
5348 case DW_TAG_structure_type:
5349 break;
5350 default:
5351 goto again;
5352 }
5353 break;
5354 case STRUCT_DOMAIN:
5355 switch (indexval.dwarf_tag)
5356 {
5357 case DW_TAG_typedef:
5358 case DW_TAG_structure_type:
5359 break;
5360 default:
5361 goto again;
5362 }
5363 break;
5364 case LABEL_DOMAIN:
5365 switch (indexval.dwarf_tag)
5366 {
5367 case 0:
5368 case DW_TAG_variable:
5369 break;
5370 default:
5371 goto again;
5372 }
5373 break;
5374 case MODULE_DOMAIN:
5375 switch (indexval.dwarf_tag)
5376 {
5377 case DW_TAG_module:
5378 break;
5379 default:
5380 goto again;
5381 }
5382 break;
5383 default:
5384 break;
5385 }
5386
5387 /* Match dw2_expand_symtabs_matching, symbol_kind and
5388 debug_names::psymbol_tag. */
5389 switch (m_search)
5390 {
5391 case VARIABLES_DOMAIN:
5392 switch (indexval.dwarf_tag)
5393 {
5394 case DW_TAG_variable:
5395 break;
5396 default:
5397 goto again;
5398 }
5399 break;
5400 case FUNCTIONS_DOMAIN:
5401 switch (indexval.dwarf_tag)
5402 {
5403 case DW_TAG_subprogram:
5404 break;
5405 default:
5406 goto again;
5407 }
5408 break;
5409 case TYPES_DOMAIN:
5410 switch (indexval.dwarf_tag)
5411 {
5412 case DW_TAG_typedef:
5413 case DW_TAG_structure_type:
5414 break;
5415 default:
5416 goto again;
5417 }
5418 break;
5419 case MODULES_DOMAIN:
5420 switch (indexval.dwarf_tag)
5421 {
5422 case DW_TAG_module:
5423 break;
5424 default:
5425 goto again;
5426 }
5427 default:
5428 break;
5429 }
5430
5431 return per_cu;
5432 }
5433
5434 static struct compunit_symtab *
5435 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5436 const char *name, domain_enum domain)
5437 {
5438 struct dwarf2_per_objfile *dwarf2_per_objfile
5439 = get_dwarf2_per_objfile (objfile);
5440
5441 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5442 if (!mapp)
5443 {
5444 /* index is NULL if OBJF_READNOW. */
5445 return NULL;
5446 }
5447 const auto &map = *mapp;
5448
5449 dw2_debug_names_iterator iter (map, block_index, domain, name);
5450
5451 struct compunit_symtab *stab_best = NULL;
5452 struct dwarf2_per_cu_data *per_cu;
5453 while ((per_cu = iter.next ()) != NULL)
5454 {
5455 struct symbol *sym, *with_opaque = NULL;
5456 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5457 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5458 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5459
5460 sym = block_find_symbol (block, name, domain,
5461 block_find_non_opaque_type_preferred,
5462 &with_opaque);
5463
5464 /* Some caution must be observed with overloaded functions and
5465 methods, since the index will not contain any overload
5466 information (but NAME might contain it). */
5467
5468 if (sym != NULL
5469 && strcmp_iw (sym->search_name (), name) == 0)
5470 return stab;
5471 if (with_opaque != NULL
5472 && strcmp_iw (with_opaque->search_name (), name) == 0)
5473 stab_best = stab;
5474
5475 /* Keep looking through other CUs. */
5476 }
5477
5478 return stab_best;
5479 }
5480
5481 /* This dumps minimal information about .debug_names. It is called
5482 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5483 uses this to verify that .debug_names has been loaded. */
5484
5485 static void
5486 dw2_debug_names_dump (struct objfile *objfile)
5487 {
5488 struct dwarf2_per_objfile *dwarf2_per_objfile
5489 = get_dwarf2_per_objfile (objfile);
5490
5491 gdb_assert (dwarf2_per_objfile->using_index);
5492 printf_filtered (".debug_names:");
5493 if (dwarf2_per_objfile->debug_names_table)
5494 printf_filtered (" exists\n");
5495 else
5496 printf_filtered (" faked for \"readnow\"\n");
5497 printf_filtered ("\n");
5498 }
5499
5500 static void
5501 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5502 const char *func_name)
5503 {
5504 struct dwarf2_per_objfile *dwarf2_per_objfile
5505 = get_dwarf2_per_objfile (objfile);
5506
5507 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5508 if (dwarf2_per_objfile->debug_names_table)
5509 {
5510 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5511
5512 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5513
5514 struct dwarf2_per_cu_data *per_cu;
5515 while ((per_cu = iter.next ()) != NULL)
5516 dw2_instantiate_symtab (per_cu, false);
5517 }
5518 }
5519
5520 static void
5521 dw2_debug_names_map_matching_symbols
5522 (struct objfile *objfile,
5523 const lookup_name_info &name, domain_enum domain,
5524 int global,
5525 gdb::function_view<symbol_found_callback_ftype> callback,
5526 symbol_compare_ftype *ordered_compare)
5527 {
5528 struct dwarf2_per_objfile *dwarf2_per_objfile
5529 = get_dwarf2_per_objfile (objfile);
5530
5531 /* debug_names_table is NULL if OBJF_READNOW. */
5532 if (!dwarf2_per_objfile->debug_names_table)
5533 return;
5534
5535 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5536 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5537
5538 const char *match_name = name.ada ().lookup_name ().c_str ();
5539 auto matcher = [&] (const char *symname)
5540 {
5541 if (ordered_compare == nullptr)
5542 return true;
5543 return ordered_compare (symname, match_name) == 0;
5544 };
5545
5546 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5547 [&] (offset_type namei)
5548 {
5549 /* The name was matched, now expand corresponding CUs that were
5550 marked. */
5551 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5552
5553 struct dwarf2_per_cu_data *per_cu;
5554 while ((per_cu = iter.next ()) != NULL)
5555 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5556 return true;
5557 });
5558
5559 /* It's a shame we couldn't do this inside the
5560 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5561 that have already been expanded. Instead, this loop matches what
5562 the psymtab code does. */
5563 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5564 {
5565 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5566 if (cust != nullptr)
5567 {
5568 const struct block *block
5569 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5570 if (!iterate_over_symbols_terminated (block, name,
5571 domain, callback))
5572 break;
5573 }
5574 }
5575 }
5576
5577 static void
5578 dw2_debug_names_expand_symtabs_matching
5579 (struct objfile *objfile,
5580 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5581 const lookup_name_info &lookup_name,
5582 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5583 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5584 enum search_domain kind)
5585 {
5586 struct dwarf2_per_objfile *dwarf2_per_objfile
5587 = get_dwarf2_per_objfile (objfile);
5588
5589 /* debug_names_table is NULL if OBJF_READNOW. */
5590 if (!dwarf2_per_objfile->debug_names_table)
5591 return;
5592
5593 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5594
5595 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5596
5597 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5598 symbol_matcher,
5599 kind, [&] (offset_type namei)
5600 {
5601 /* The name was matched, now expand corresponding CUs that were
5602 marked. */
5603 dw2_debug_names_iterator iter (map, kind, namei);
5604
5605 struct dwarf2_per_cu_data *per_cu;
5606 while ((per_cu = iter.next ()) != NULL)
5607 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5608 expansion_notify);
5609 return true;
5610 });
5611 }
5612
5613 const struct quick_symbol_functions dwarf2_debug_names_functions =
5614 {
5615 dw2_has_symbols,
5616 dw2_find_last_source_symtab,
5617 dw2_forget_cached_source_info,
5618 dw2_map_symtabs_matching_filename,
5619 dw2_debug_names_lookup_symbol,
5620 NULL,
5621 dw2_print_stats,
5622 dw2_debug_names_dump,
5623 dw2_debug_names_expand_symtabs_for_function,
5624 dw2_expand_all_symtabs,
5625 dw2_expand_symtabs_with_fullname,
5626 dw2_debug_names_map_matching_symbols,
5627 dw2_debug_names_expand_symtabs_matching,
5628 dw2_find_pc_sect_compunit_symtab,
5629 NULL,
5630 dw2_map_symbol_filenames
5631 };
5632
5633 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5634 to either a dwarf2_per_objfile or dwz_file object. */
5635
5636 template <typename T>
5637 static gdb::array_view<const gdb_byte>
5638 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5639 {
5640 dwarf2_section_info *section = &section_owner->gdb_index;
5641
5642 if (section->empty ())
5643 return {};
5644
5645 /* Older elfutils strip versions could keep the section in the main
5646 executable while splitting it for the separate debug info file. */
5647 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5648 return {};
5649
5650 section->read (obj);
5651
5652 /* dwarf2_section_info::size is a bfd_size_type, while
5653 gdb::array_view works with size_t. On 32-bit hosts, with
5654 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5655 is 32-bit. So we need an explicit narrowing conversion here.
5656 This is fine, because it's impossible to allocate or mmap an
5657 array/buffer larger than what size_t can represent. */
5658 return gdb::make_array_view (section->buffer, section->size);
5659 }
5660
5661 /* Lookup the index cache for the contents of the index associated to
5662 DWARF2_OBJ. */
5663
5664 static gdb::array_view<const gdb_byte>
5665 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5666 {
5667 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5668 if (build_id == nullptr)
5669 return {};
5670
5671 return global_index_cache.lookup_gdb_index (build_id,
5672 &dwarf2_obj->index_cache_res);
5673 }
5674
5675 /* Same as the above, but for DWZ. */
5676
5677 static gdb::array_view<const gdb_byte>
5678 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5679 {
5680 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5681 if (build_id == nullptr)
5682 return {};
5683
5684 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5685 }
5686
5687 /* See symfile.h. */
5688
5689 bool
5690 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5691 {
5692 struct dwarf2_per_objfile *dwarf2_per_objfile
5693 = get_dwarf2_per_objfile (objfile);
5694
5695 /* If we're about to read full symbols, don't bother with the
5696 indices. In this case we also don't care if some other debug
5697 format is making psymtabs, because they are all about to be
5698 expanded anyway. */
5699 if ((objfile->flags & OBJF_READNOW))
5700 {
5701 dwarf2_per_objfile->using_index = 1;
5702 create_all_comp_units (dwarf2_per_objfile);
5703 create_all_type_units (dwarf2_per_objfile);
5704 dwarf2_per_objfile->quick_file_names_table
5705 = create_quick_file_names_table
5706 (dwarf2_per_objfile->all_comp_units.size ());
5707
5708 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5709 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5710 {
5711 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5712
5713 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5714 struct dwarf2_per_cu_quick_data);
5715 }
5716
5717 /* Return 1 so that gdb sees the "quick" functions. However,
5718 these functions will be no-ops because we will have expanded
5719 all symtabs. */
5720 *index_kind = dw_index_kind::GDB_INDEX;
5721 return true;
5722 }
5723
5724 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5725 {
5726 *index_kind = dw_index_kind::DEBUG_NAMES;
5727 return true;
5728 }
5729
5730 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5731 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5732 get_gdb_index_contents_from_section<dwz_file>))
5733 {
5734 *index_kind = dw_index_kind::GDB_INDEX;
5735 return true;
5736 }
5737
5738 /* ... otherwise, try to find the index in the index cache. */
5739 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5740 get_gdb_index_contents_from_cache,
5741 get_gdb_index_contents_from_cache_dwz))
5742 {
5743 global_index_cache.hit ();
5744 *index_kind = dw_index_kind::GDB_INDEX;
5745 return true;
5746 }
5747
5748 global_index_cache.miss ();
5749 return false;
5750 }
5751
5752 \f
5753
5754 /* Build a partial symbol table. */
5755
5756 void
5757 dwarf2_build_psymtabs (struct objfile *objfile)
5758 {
5759 struct dwarf2_per_objfile *dwarf2_per_objfile
5760 = get_dwarf2_per_objfile (objfile);
5761
5762 init_psymbol_list (objfile, 1024);
5763
5764 try
5765 {
5766 /* This isn't really ideal: all the data we allocate on the
5767 objfile's obstack is still uselessly kept around. However,
5768 freeing it seems unsafe. */
5769 psymtab_discarder psymtabs (objfile);
5770 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5771 psymtabs.keep ();
5772
5773 /* (maybe) store an index in the cache. */
5774 global_index_cache.store (dwarf2_per_objfile);
5775 }
5776 catch (const gdb_exception_error &except)
5777 {
5778 exception_print (gdb_stderr, except);
5779 }
5780 }
5781
5782 /* Find the base address of the compilation unit for range lists and
5783 location lists. It will normally be specified by DW_AT_low_pc.
5784 In DWARF-3 draft 4, the base address could be overridden by
5785 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5786 compilation units with discontinuous ranges. */
5787
5788 static void
5789 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5790 {
5791 struct attribute *attr;
5792
5793 cu->base_address.reset ();
5794
5795 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5796 if (attr != nullptr)
5797 cu->base_address = attr->value_as_address ();
5798 else
5799 {
5800 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5801 if (attr != nullptr)
5802 cu->base_address = attr->value_as_address ();
5803 }
5804 }
5805
5806 /* Helper function that returns the proper abbrev section for
5807 THIS_CU. */
5808
5809 static struct dwarf2_section_info *
5810 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5811 {
5812 struct dwarf2_section_info *abbrev;
5813 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5814
5815 if (this_cu->is_dwz)
5816 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5817 else
5818 abbrev = &dwarf2_per_objfile->abbrev;
5819
5820 return abbrev;
5821 }
5822
5823 /* Fetch the abbreviation table offset from a comp or type unit header. */
5824
5825 static sect_offset
5826 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5827 struct dwarf2_section_info *section,
5828 sect_offset sect_off)
5829 {
5830 bfd *abfd = section->get_bfd_owner ();
5831 const gdb_byte *info_ptr;
5832 unsigned int initial_length_size, offset_size;
5833 uint16_t version;
5834
5835 section->read (dwarf2_per_objfile->objfile);
5836 info_ptr = section->buffer + to_underlying (sect_off);
5837 read_initial_length (abfd, info_ptr, &initial_length_size);
5838 offset_size = initial_length_size == 4 ? 4 : 8;
5839 info_ptr += initial_length_size;
5840
5841 version = read_2_bytes (abfd, info_ptr);
5842 info_ptr += 2;
5843 if (version >= 5)
5844 {
5845 /* Skip unit type and address size. */
5846 info_ptr += 2;
5847 }
5848
5849 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5850 }
5851
5852 /* A partial symtab that is used only for include files. */
5853 struct dwarf2_include_psymtab : public partial_symtab
5854 {
5855 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5856 : partial_symtab (filename, objfile)
5857 {
5858 }
5859
5860 void read_symtab (struct objfile *objfile) override
5861 {
5862 expand_psymtab (objfile);
5863 }
5864
5865 void expand_psymtab (struct objfile *objfile) override
5866 {
5867 if (m_readin)
5868 return;
5869 /* It's an include file, no symbols to read for it.
5870 Everything is in the parent symtab. */
5871 expand_dependencies (objfile);
5872 m_readin = true;
5873 }
5874
5875 bool readin_p () const override
5876 {
5877 return m_readin;
5878 }
5879
5880 struct compunit_symtab *get_compunit_symtab () const override
5881 {
5882 return nullptr;
5883 }
5884
5885 private:
5886
5887 bool m_readin = false;
5888 };
5889
5890 /* Allocate a new partial symtab for file named NAME and mark this new
5891 partial symtab as being an include of PST. */
5892
5893 static void
5894 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5895 struct objfile *objfile)
5896 {
5897 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5898
5899 if (!IS_ABSOLUTE_PATH (subpst->filename))
5900 {
5901 /* It shares objfile->objfile_obstack. */
5902 subpst->dirname = pst->dirname;
5903 }
5904
5905 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5906 subpst->dependencies[0] = pst;
5907 subpst->number_of_dependencies = 1;
5908 }
5909
5910 /* Read the Line Number Program data and extract the list of files
5911 included by the source file represented by PST. Build an include
5912 partial symtab for each of these included files. */
5913
5914 static void
5915 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5916 struct die_info *die,
5917 dwarf2_psymtab *pst)
5918 {
5919 line_header_up lh;
5920 struct attribute *attr;
5921
5922 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5923 if (attr != nullptr)
5924 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5925 if (lh == NULL)
5926 return; /* No linetable, so no includes. */
5927
5928 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5929 that we pass in the raw text_low here; that is ok because we're
5930 only decoding the line table to make include partial symtabs, and
5931 so the addresses aren't really used. */
5932 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5933 pst->raw_text_low (), 1);
5934 }
5935
5936 static hashval_t
5937 hash_signatured_type (const void *item)
5938 {
5939 const struct signatured_type *sig_type
5940 = (const struct signatured_type *) item;
5941
5942 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5943 return sig_type->signature;
5944 }
5945
5946 static int
5947 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5948 {
5949 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5950 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5951
5952 return lhs->signature == rhs->signature;
5953 }
5954
5955 /* Allocate a hash table for signatured types. */
5956
5957 static htab_up
5958 allocate_signatured_type_table ()
5959 {
5960 return htab_up (htab_create_alloc (41,
5961 hash_signatured_type,
5962 eq_signatured_type,
5963 NULL, xcalloc, xfree));
5964 }
5965
5966 /* A helper function to add a signatured type CU to a table. */
5967
5968 static int
5969 add_signatured_type_cu_to_table (void **slot, void *datum)
5970 {
5971 struct signatured_type *sigt = (struct signatured_type *) *slot;
5972 std::vector<signatured_type *> *all_type_units
5973 = (std::vector<signatured_type *> *) datum;
5974
5975 all_type_units->push_back (sigt);
5976
5977 return 1;
5978 }
5979
5980 /* A helper for create_debug_types_hash_table. Read types from SECTION
5981 and fill them into TYPES_HTAB. It will process only type units,
5982 therefore DW_UT_type. */
5983
5984 static void
5985 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
5986 struct dwo_file *dwo_file,
5987 dwarf2_section_info *section, htab_up &types_htab,
5988 rcuh_kind section_kind)
5989 {
5990 struct objfile *objfile = dwarf2_per_objfile->objfile;
5991 struct dwarf2_section_info *abbrev_section;
5992 bfd *abfd;
5993 const gdb_byte *info_ptr, *end_ptr;
5994
5995 abbrev_section = (dwo_file != NULL
5996 ? &dwo_file->sections.abbrev
5997 : &dwarf2_per_objfile->abbrev);
5998
5999 if (dwarf_read_debug)
6000 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6001 section->get_name (),
6002 abbrev_section->get_file_name ());
6003
6004 section->read (objfile);
6005 info_ptr = section->buffer;
6006
6007 if (info_ptr == NULL)
6008 return;
6009
6010 /* We can't set abfd until now because the section may be empty or
6011 not present, in which case the bfd is unknown. */
6012 abfd = section->get_bfd_owner ();
6013
6014 /* We don't use cutu_reader here because we don't need to read
6015 any dies: the signature is in the header. */
6016
6017 end_ptr = info_ptr + section->size;
6018 while (info_ptr < end_ptr)
6019 {
6020 struct signatured_type *sig_type;
6021 struct dwo_unit *dwo_tu;
6022 void **slot;
6023 const gdb_byte *ptr = info_ptr;
6024 struct comp_unit_head header;
6025 unsigned int length;
6026
6027 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6028
6029 /* Initialize it due to a false compiler warning. */
6030 header.signature = -1;
6031 header.type_cu_offset_in_tu = (cu_offset) -1;
6032
6033 /* We need to read the type's signature in order to build the hash
6034 table, but we don't need anything else just yet. */
6035
6036 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6037 abbrev_section, ptr, section_kind);
6038
6039 length = header.get_length ();
6040
6041 /* Skip dummy type units. */
6042 if (ptr >= info_ptr + length
6043 || peek_abbrev_code (abfd, ptr) == 0
6044 || header.unit_type != DW_UT_type)
6045 {
6046 info_ptr += length;
6047 continue;
6048 }
6049
6050 if (types_htab == NULL)
6051 {
6052 if (dwo_file)
6053 types_htab = allocate_dwo_unit_table ();
6054 else
6055 types_htab = allocate_signatured_type_table ();
6056 }
6057
6058 if (dwo_file)
6059 {
6060 sig_type = NULL;
6061 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6062 struct dwo_unit);
6063 dwo_tu->dwo_file = dwo_file;
6064 dwo_tu->signature = header.signature;
6065 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6066 dwo_tu->section = section;
6067 dwo_tu->sect_off = sect_off;
6068 dwo_tu->length = length;
6069 }
6070 else
6071 {
6072 /* N.B.: type_offset is not usable if this type uses a DWO file.
6073 The real type_offset is in the DWO file. */
6074 dwo_tu = NULL;
6075 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6076 struct signatured_type);
6077 sig_type->signature = header.signature;
6078 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6079 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6080 sig_type->per_cu.is_debug_types = 1;
6081 sig_type->per_cu.section = section;
6082 sig_type->per_cu.sect_off = sect_off;
6083 sig_type->per_cu.length = length;
6084 }
6085
6086 slot = htab_find_slot (types_htab.get (),
6087 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6088 INSERT);
6089 gdb_assert (slot != NULL);
6090 if (*slot != NULL)
6091 {
6092 sect_offset dup_sect_off;
6093
6094 if (dwo_file)
6095 {
6096 const struct dwo_unit *dup_tu
6097 = (const struct dwo_unit *) *slot;
6098
6099 dup_sect_off = dup_tu->sect_off;
6100 }
6101 else
6102 {
6103 const struct signatured_type *dup_tu
6104 = (const struct signatured_type *) *slot;
6105
6106 dup_sect_off = dup_tu->per_cu.sect_off;
6107 }
6108
6109 complaint (_("debug type entry at offset %s is duplicate to"
6110 " the entry at offset %s, signature %s"),
6111 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6112 hex_string (header.signature));
6113 }
6114 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6115
6116 if (dwarf_read_debug > 1)
6117 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6118 sect_offset_str (sect_off),
6119 hex_string (header.signature));
6120
6121 info_ptr += length;
6122 }
6123 }
6124
6125 /* Create the hash table of all entries in the .debug_types
6126 (or .debug_types.dwo) section(s).
6127 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6128 otherwise it is NULL.
6129
6130 The result is a pointer to the hash table or NULL if there are no types.
6131
6132 Note: This function processes DWO files only, not DWP files. */
6133
6134 static void
6135 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6136 struct dwo_file *dwo_file,
6137 gdb::array_view<dwarf2_section_info> type_sections,
6138 htab_up &types_htab)
6139 {
6140 for (dwarf2_section_info &section : type_sections)
6141 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6142 types_htab, rcuh_kind::TYPE);
6143 }
6144
6145 /* Create the hash table of all entries in the .debug_types section,
6146 and initialize all_type_units.
6147 The result is zero if there is an error (e.g. missing .debug_types section),
6148 otherwise non-zero. */
6149
6150 static int
6151 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6152 {
6153 htab_up types_htab;
6154
6155 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6156 &dwarf2_per_objfile->info, types_htab,
6157 rcuh_kind::COMPILE);
6158 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6159 dwarf2_per_objfile->types, types_htab);
6160 if (types_htab == NULL)
6161 {
6162 dwarf2_per_objfile->signatured_types = NULL;
6163 return 0;
6164 }
6165
6166 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6167
6168 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6169 dwarf2_per_objfile->all_type_units.reserve
6170 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6171
6172 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6173 add_signatured_type_cu_to_table,
6174 &dwarf2_per_objfile->all_type_units);
6175
6176 return 1;
6177 }
6178
6179 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6180 If SLOT is non-NULL, it is the entry to use in the hash table.
6181 Otherwise we find one. */
6182
6183 static struct signatured_type *
6184 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6185 void **slot)
6186 {
6187 struct objfile *objfile = dwarf2_per_objfile->objfile;
6188
6189 if (dwarf2_per_objfile->all_type_units.size ()
6190 == dwarf2_per_objfile->all_type_units.capacity ())
6191 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6192
6193 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6194 struct signatured_type);
6195
6196 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6197 sig_type->signature = sig;
6198 sig_type->per_cu.is_debug_types = 1;
6199 if (dwarf2_per_objfile->using_index)
6200 {
6201 sig_type->per_cu.v.quick =
6202 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6203 struct dwarf2_per_cu_quick_data);
6204 }
6205
6206 if (slot == NULL)
6207 {
6208 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6209 sig_type, INSERT);
6210 }
6211 gdb_assert (*slot == NULL);
6212 *slot = sig_type;
6213 /* The rest of sig_type must be filled in by the caller. */
6214 return sig_type;
6215 }
6216
6217 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6218 Fill in SIG_ENTRY with DWO_ENTRY. */
6219
6220 static void
6221 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6222 struct signatured_type *sig_entry,
6223 struct dwo_unit *dwo_entry)
6224 {
6225 /* Make sure we're not clobbering something we don't expect to. */
6226 gdb_assert (! sig_entry->per_cu.queued);
6227 gdb_assert (sig_entry->per_cu.cu == NULL);
6228 if (dwarf2_per_objfile->using_index)
6229 {
6230 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6231 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6232 }
6233 else
6234 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6235 gdb_assert (sig_entry->signature == dwo_entry->signature);
6236 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6237 gdb_assert (sig_entry->type_unit_group == NULL);
6238 gdb_assert (sig_entry->dwo_unit == NULL);
6239
6240 sig_entry->per_cu.section = dwo_entry->section;
6241 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6242 sig_entry->per_cu.length = dwo_entry->length;
6243 sig_entry->per_cu.reading_dwo_directly = 1;
6244 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6245 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6246 sig_entry->dwo_unit = dwo_entry;
6247 }
6248
6249 /* Subroutine of lookup_signatured_type.
6250 If we haven't read the TU yet, create the signatured_type data structure
6251 for a TU to be read in directly from a DWO file, bypassing the stub.
6252 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6253 using .gdb_index, then when reading a CU we want to stay in the DWO file
6254 containing that CU. Otherwise we could end up reading several other DWO
6255 files (due to comdat folding) to process the transitive closure of all the
6256 mentioned TUs, and that can be slow. The current DWO file will have every
6257 type signature that it needs.
6258 We only do this for .gdb_index because in the psymtab case we already have
6259 to read all the DWOs to build the type unit groups. */
6260
6261 static struct signatured_type *
6262 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6263 {
6264 struct dwarf2_per_objfile *dwarf2_per_objfile
6265 = cu->per_cu->dwarf2_per_objfile;
6266 struct dwo_file *dwo_file;
6267 struct dwo_unit find_dwo_entry, *dwo_entry;
6268 struct signatured_type find_sig_entry, *sig_entry;
6269 void **slot;
6270
6271 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6272
6273 /* If TU skeletons have been removed then we may not have read in any
6274 TUs yet. */
6275 if (dwarf2_per_objfile->signatured_types == NULL)
6276 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6277
6278 /* We only ever need to read in one copy of a signatured type.
6279 Use the global signatured_types array to do our own comdat-folding
6280 of types. If this is the first time we're reading this TU, and
6281 the TU has an entry in .gdb_index, replace the recorded data from
6282 .gdb_index with this TU. */
6283
6284 find_sig_entry.signature = sig;
6285 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6286 &find_sig_entry, INSERT);
6287 sig_entry = (struct signatured_type *) *slot;
6288
6289 /* We can get here with the TU already read, *or* in the process of being
6290 read. Don't reassign the global entry to point to this DWO if that's
6291 the case. Also note that if the TU is already being read, it may not
6292 have come from a DWO, the program may be a mix of Fission-compiled
6293 code and non-Fission-compiled code. */
6294
6295 /* Have we already tried to read this TU?
6296 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6297 needn't exist in the global table yet). */
6298 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6299 return sig_entry;
6300
6301 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6302 dwo_unit of the TU itself. */
6303 dwo_file = cu->dwo_unit->dwo_file;
6304
6305 /* Ok, this is the first time we're reading this TU. */
6306 if (dwo_file->tus == NULL)
6307 return NULL;
6308 find_dwo_entry.signature = sig;
6309 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6310 &find_dwo_entry);
6311 if (dwo_entry == NULL)
6312 return NULL;
6313
6314 /* If the global table doesn't have an entry for this TU, add one. */
6315 if (sig_entry == NULL)
6316 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6317
6318 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6319 sig_entry->per_cu.tu_read = 1;
6320 return sig_entry;
6321 }
6322
6323 /* Subroutine of lookup_signatured_type.
6324 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6325 then try the DWP file. If the TU stub (skeleton) has been removed then
6326 it won't be in .gdb_index. */
6327
6328 static struct signatured_type *
6329 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6330 {
6331 struct dwarf2_per_objfile *dwarf2_per_objfile
6332 = cu->per_cu->dwarf2_per_objfile;
6333 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6334 struct dwo_unit *dwo_entry;
6335 struct signatured_type find_sig_entry, *sig_entry;
6336 void **slot;
6337
6338 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6339 gdb_assert (dwp_file != NULL);
6340
6341 /* If TU skeletons have been removed then we may not have read in any
6342 TUs yet. */
6343 if (dwarf2_per_objfile->signatured_types == NULL)
6344 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6345
6346 find_sig_entry.signature = sig;
6347 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6348 &find_sig_entry, INSERT);
6349 sig_entry = (struct signatured_type *) *slot;
6350
6351 /* Have we already tried to read this TU?
6352 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6353 needn't exist in the global table yet). */
6354 if (sig_entry != NULL)
6355 return sig_entry;
6356
6357 if (dwp_file->tus == NULL)
6358 return NULL;
6359 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6360 sig, 1 /* is_debug_types */);
6361 if (dwo_entry == NULL)
6362 return NULL;
6363
6364 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6365 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6366
6367 return sig_entry;
6368 }
6369
6370 /* Lookup a signature based type for DW_FORM_ref_sig8.
6371 Returns NULL if signature SIG is not present in the table.
6372 It is up to the caller to complain about this. */
6373
6374 static struct signatured_type *
6375 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6376 {
6377 struct dwarf2_per_objfile *dwarf2_per_objfile
6378 = cu->per_cu->dwarf2_per_objfile;
6379
6380 if (cu->dwo_unit
6381 && dwarf2_per_objfile->using_index)
6382 {
6383 /* We're in a DWO/DWP file, and we're using .gdb_index.
6384 These cases require special processing. */
6385 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6386 return lookup_dwo_signatured_type (cu, sig);
6387 else
6388 return lookup_dwp_signatured_type (cu, sig);
6389 }
6390 else
6391 {
6392 struct signatured_type find_entry, *entry;
6393
6394 if (dwarf2_per_objfile->signatured_types == NULL)
6395 return NULL;
6396 find_entry.signature = sig;
6397 entry = ((struct signatured_type *)
6398 htab_find (dwarf2_per_objfile->signatured_types.get (),
6399 &find_entry));
6400 return entry;
6401 }
6402 }
6403
6404 /* Low level DIE reading support. */
6405
6406 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6407
6408 static void
6409 init_cu_die_reader (struct die_reader_specs *reader,
6410 struct dwarf2_cu *cu,
6411 struct dwarf2_section_info *section,
6412 struct dwo_file *dwo_file,
6413 struct abbrev_table *abbrev_table)
6414 {
6415 gdb_assert (section->readin && section->buffer != NULL);
6416 reader->abfd = section->get_bfd_owner ();
6417 reader->cu = cu;
6418 reader->dwo_file = dwo_file;
6419 reader->die_section = section;
6420 reader->buffer = section->buffer;
6421 reader->buffer_end = section->buffer + section->size;
6422 reader->abbrev_table = abbrev_table;
6423 }
6424
6425 /* Subroutine of cutu_reader to simplify it.
6426 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6427 There's just a lot of work to do, and cutu_reader is big enough
6428 already.
6429
6430 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6431 from it to the DIE in the DWO. If NULL we are skipping the stub.
6432 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6433 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6434 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6435 STUB_COMP_DIR may be non-NULL.
6436 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6437 are filled in with the info of the DIE from the DWO file.
6438 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6439 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6440 kept around for at least as long as *RESULT_READER.
6441
6442 The result is non-zero if a valid (non-dummy) DIE was found. */
6443
6444 static int
6445 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6446 struct dwo_unit *dwo_unit,
6447 struct die_info *stub_comp_unit_die,
6448 const char *stub_comp_dir,
6449 struct die_reader_specs *result_reader,
6450 const gdb_byte **result_info_ptr,
6451 struct die_info **result_comp_unit_die,
6452 abbrev_table_up *result_dwo_abbrev_table)
6453 {
6454 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6455 struct objfile *objfile = dwarf2_per_objfile->objfile;
6456 struct dwarf2_cu *cu = this_cu->cu;
6457 bfd *abfd;
6458 const gdb_byte *begin_info_ptr, *info_ptr;
6459 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6460 int i,num_extra_attrs;
6461 struct dwarf2_section_info *dwo_abbrev_section;
6462 struct die_info *comp_unit_die;
6463
6464 /* At most one of these may be provided. */
6465 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6466
6467 /* These attributes aren't processed until later:
6468 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6469 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6470 referenced later. However, these attributes are found in the stub
6471 which we won't have later. In order to not impose this complication
6472 on the rest of the code, we read them here and copy them to the
6473 DWO CU/TU die. */
6474
6475 stmt_list = NULL;
6476 low_pc = NULL;
6477 high_pc = NULL;
6478 ranges = NULL;
6479 comp_dir = NULL;
6480
6481 if (stub_comp_unit_die != NULL)
6482 {
6483 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6484 DWO file. */
6485 if (! this_cu->is_debug_types)
6486 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6487 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6488 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6489 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6490 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6491
6492 cu->addr_base = stub_comp_unit_die->addr_base ();
6493
6494 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6495 here (if needed). We need the value before we can process
6496 DW_AT_ranges. */
6497 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6498 }
6499 else if (stub_comp_dir != NULL)
6500 {
6501 /* Reconstruct the comp_dir attribute to simplify the code below. */
6502 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6503 comp_dir->name = DW_AT_comp_dir;
6504 comp_dir->form = DW_FORM_string;
6505 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6506 DW_STRING (comp_dir) = stub_comp_dir;
6507 }
6508
6509 /* Set up for reading the DWO CU/TU. */
6510 cu->dwo_unit = dwo_unit;
6511 dwarf2_section_info *section = dwo_unit->section;
6512 section->read (objfile);
6513 abfd = section->get_bfd_owner ();
6514 begin_info_ptr = info_ptr = (section->buffer
6515 + to_underlying (dwo_unit->sect_off));
6516 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6517
6518 if (this_cu->is_debug_types)
6519 {
6520 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6521
6522 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6523 &cu->header, section,
6524 dwo_abbrev_section,
6525 info_ptr, rcuh_kind::TYPE);
6526 /* This is not an assert because it can be caused by bad debug info. */
6527 if (sig_type->signature != cu->header.signature)
6528 {
6529 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6530 " TU at offset %s [in module %s]"),
6531 hex_string (sig_type->signature),
6532 hex_string (cu->header.signature),
6533 sect_offset_str (dwo_unit->sect_off),
6534 bfd_get_filename (abfd));
6535 }
6536 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6537 /* For DWOs coming from DWP files, we don't know the CU length
6538 nor the type's offset in the TU until now. */
6539 dwo_unit->length = cu->header.get_length ();
6540 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6541
6542 /* Establish the type offset that can be used to lookup the type.
6543 For DWO files, we don't know it until now. */
6544 sig_type->type_offset_in_section
6545 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6546 }
6547 else
6548 {
6549 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6550 &cu->header, section,
6551 dwo_abbrev_section,
6552 info_ptr, rcuh_kind::COMPILE);
6553 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6554 /* For DWOs coming from DWP files, we don't know the CU length
6555 until now. */
6556 dwo_unit->length = cu->header.get_length ();
6557 }
6558
6559 *result_dwo_abbrev_table
6560 = abbrev_table::read (objfile, dwo_abbrev_section,
6561 cu->header.abbrev_sect_off);
6562 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6563 result_dwo_abbrev_table->get ());
6564
6565 /* Read in the die, but leave space to copy over the attributes
6566 from the stub. This has the benefit of simplifying the rest of
6567 the code - all the work to maintain the illusion of a single
6568 DW_TAG_{compile,type}_unit DIE is done here. */
6569 num_extra_attrs = ((stmt_list != NULL)
6570 + (low_pc != NULL)
6571 + (high_pc != NULL)
6572 + (ranges != NULL)
6573 + (comp_dir != NULL));
6574 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6575 num_extra_attrs);
6576
6577 /* Copy over the attributes from the stub to the DIE we just read in. */
6578 comp_unit_die = *result_comp_unit_die;
6579 i = comp_unit_die->num_attrs;
6580 if (stmt_list != NULL)
6581 comp_unit_die->attrs[i++] = *stmt_list;
6582 if (low_pc != NULL)
6583 comp_unit_die->attrs[i++] = *low_pc;
6584 if (high_pc != NULL)
6585 comp_unit_die->attrs[i++] = *high_pc;
6586 if (ranges != NULL)
6587 comp_unit_die->attrs[i++] = *ranges;
6588 if (comp_dir != NULL)
6589 comp_unit_die->attrs[i++] = *comp_dir;
6590 comp_unit_die->num_attrs += num_extra_attrs;
6591
6592 if (dwarf_die_debug)
6593 {
6594 fprintf_unfiltered (gdb_stdlog,
6595 "Read die from %s@0x%x of %s:\n",
6596 section->get_name (),
6597 (unsigned) (begin_info_ptr - section->buffer),
6598 bfd_get_filename (abfd));
6599 dump_die (comp_unit_die, dwarf_die_debug);
6600 }
6601
6602 /* Skip dummy compilation units. */
6603 if (info_ptr >= begin_info_ptr + dwo_unit->length
6604 || peek_abbrev_code (abfd, info_ptr) == 0)
6605 return 0;
6606
6607 *result_info_ptr = info_ptr;
6608 return 1;
6609 }
6610
6611 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6612 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6613 signature is part of the header. */
6614 static gdb::optional<ULONGEST>
6615 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6616 {
6617 if (cu->header.version >= 5)
6618 return cu->header.signature;
6619 struct attribute *attr;
6620 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6621 if (attr == nullptr)
6622 return gdb::optional<ULONGEST> ();
6623 return DW_UNSND (attr);
6624 }
6625
6626 /* Subroutine of cutu_reader to simplify it.
6627 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6628 Returns NULL if the specified DWO unit cannot be found. */
6629
6630 static struct dwo_unit *
6631 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6632 struct die_info *comp_unit_die,
6633 const char *dwo_name)
6634 {
6635 struct dwarf2_cu *cu = this_cu->cu;
6636 struct dwo_unit *dwo_unit;
6637 const char *comp_dir;
6638
6639 gdb_assert (cu != NULL);
6640
6641 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6642 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6643 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6644
6645 if (this_cu->is_debug_types)
6646 {
6647 struct signatured_type *sig_type;
6648
6649 /* Since this_cu is the first member of struct signatured_type,
6650 we can go from a pointer to one to a pointer to the other. */
6651 sig_type = (struct signatured_type *) this_cu;
6652 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6653 }
6654 else
6655 {
6656 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6657 if (!signature.has_value ())
6658 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6659 " [in module %s]"),
6660 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6661 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6662 *signature);
6663 }
6664
6665 return dwo_unit;
6666 }
6667
6668 /* Subroutine of cutu_reader to simplify it.
6669 See it for a description of the parameters.
6670 Read a TU directly from a DWO file, bypassing the stub. */
6671
6672 void
6673 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6674 int use_existing_cu)
6675 {
6676 struct signatured_type *sig_type;
6677
6678 /* Verify we can do the following downcast, and that we have the
6679 data we need. */
6680 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6681 sig_type = (struct signatured_type *) this_cu;
6682 gdb_assert (sig_type->dwo_unit != NULL);
6683
6684 if (use_existing_cu && this_cu->cu != NULL)
6685 {
6686 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6687 /* There's no need to do the rereading_dwo_cu handling that
6688 cutu_reader does since we don't read the stub. */
6689 }
6690 else
6691 {
6692 /* If !use_existing_cu, this_cu->cu must be NULL. */
6693 gdb_assert (this_cu->cu == NULL);
6694 m_new_cu.reset (new dwarf2_cu (this_cu));
6695 }
6696
6697 /* A future optimization, if needed, would be to use an existing
6698 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6699 could share abbrev tables. */
6700
6701 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6702 NULL /* stub_comp_unit_die */,
6703 sig_type->dwo_unit->dwo_file->comp_dir,
6704 this, &info_ptr,
6705 &comp_unit_die,
6706 &m_dwo_abbrev_table) == 0)
6707 {
6708 /* Dummy die. */
6709 dummy_p = true;
6710 }
6711 }
6712
6713 /* Initialize a CU (or TU) and read its DIEs.
6714 If the CU defers to a DWO file, read the DWO file as well.
6715
6716 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6717 Otherwise the table specified in the comp unit header is read in and used.
6718 This is an optimization for when we already have the abbrev table.
6719
6720 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6721 Otherwise, a new CU is allocated with xmalloc. */
6722
6723 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6724 struct abbrev_table *abbrev_table,
6725 int use_existing_cu,
6726 bool skip_partial)
6727 : die_reader_specs {},
6728 m_this_cu (this_cu)
6729 {
6730 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6731 struct objfile *objfile = dwarf2_per_objfile->objfile;
6732 struct dwarf2_section_info *section = this_cu->section;
6733 bfd *abfd = section->get_bfd_owner ();
6734 struct dwarf2_cu *cu;
6735 const gdb_byte *begin_info_ptr;
6736 struct signatured_type *sig_type = NULL;
6737 struct dwarf2_section_info *abbrev_section;
6738 /* Non-zero if CU currently points to a DWO file and we need to
6739 reread it. When this happens we need to reread the skeleton die
6740 before we can reread the DWO file (this only applies to CUs, not TUs). */
6741 int rereading_dwo_cu = 0;
6742
6743 if (dwarf_die_debug)
6744 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6745 this_cu->is_debug_types ? "type" : "comp",
6746 sect_offset_str (this_cu->sect_off));
6747
6748 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6749 file (instead of going through the stub), short-circuit all of this. */
6750 if (this_cu->reading_dwo_directly)
6751 {
6752 /* Narrow down the scope of possibilities to have to understand. */
6753 gdb_assert (this_cu->is_debug_types);
6754 gdb_assert (abbrev_table == NULL);
6755 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6756 return;
6757 }
6758
6759 /* This is cheap if the section is already read in. */
6760 section->read (objfile);
6761
6762 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6763
6764 abbrev_section = get_abbrev_section_for_cu (this_cu);
6765
6766 if (use_existing_cu && this_cu->cu != NULL)
6767 {
6768 cu = this_cu->cu;
6769 /* If this CU is from a DWO file we need to start over, we need to
6770 refetch the attributes from the skeleton CU.
6771 This could be optimized by retrieving those attributes from when we
6772 were here the first time: the previous comp_unit_die was stored in
6773 comp_unit_obstack. But there's no data yet that we need this
6774 optimization. */
6775 if (cu->dwo_unit != NULL)
6776 rereading_dwo_cu = 1;
6777 }
6778 else
6779 {
6780 /* If !use_existing_cu, this_cu->cu must be NULL. */
6781 gdb_assert (this_cu->cu == NULL);
6782 m_new_cu.reset (new dwarf2_cu (this_cu));
6783 cu = m_new_cu.get ();
6784 }
6785
6786 /* Get the header. */
6787 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6788 {
6789 /* We already have the header, there's no need to read it in again. */
6790 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6791 }
6792 else
6793 {
6794 if (this_cu->is_debug_types)
6795 {
6796 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6797 &cu->header, section,
6798 abbrev_section, info_ptr,
6799 rcuh_kind::TYPE);
6800
6801 /* Since per_cu is the first member of struct signatured_type,
6802 we can go from a pointer to one to a pointer to the other. */
6803 sig_type = (struct signatured_type *) this_cu;
6804 gdb_assert (sig_type->signature == cu->header.signature);
6805 gdb_assert (sig_type->type_offset_in_tu
6806 == cu->header.type_cu_offset_in_tu);
6807 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6808
6809 /* LENGTH has not been set yet for type units if we're
6810 using .gdb_index. */
6811 this_cu->length = cu->header.get_length ();
6812
6813 /* Establish the type offset that can be used to lookup the type. */
6814 sig_type->type_offset_in_section =
6815 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6816
6817 this_cu->dwarf_version = cu->header.version;
6818 }
6819 else
6820 {
6821 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6822 &cu->header, section,
6823 abbrev_section,
6824 info_ptr,
6825 rcuh_kind::COMPILE);
6826
6827 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6828 gdb_assert (this_cu->length == cu->header.get_length ());
6829 this_cu->dwarf_version = cu->header.version;
6830 }
6831 }
6832
6833 /* Skip dummy compilation units. */
6834 if (info_ptr >= begin_info_ptr + this_cu->length
6835 || peek_abbrev_code (abfd, info_ptr) == 0)
6836 {
6837 dummy_p = true;
6838 return;
6839 }
6840
6841 /* If we don't have them yet, read the abbrevs for this compilation unit.
6842 And if we need to read them now, make sure they're freed when we're
6843 done. */
6844 if (abbrev_table != NULL)
6845 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6846 else
6847 {
6848 m_abbrev_table_holder
6849 = abbrev_table::read (objfile, abbrev_section,
6850 cu->header.abbrev_sect_off);
6851 abbrev_table = m_abbrev_table_holder.get ();
6852 }
6853
6854 /* Read the top level CU/TU die. */
6855 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6856 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6857
6858 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6859 {
6860 dummy_p = true;
6861 return;
6862 }
6863
6864 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6865 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6866 table from the DWO file and pass the ownership over to us. It will be
6867 referenced from READER, so we must make sure to free it after we're done
6868 with READER.
6869
6870 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6871 DWO CU, that this test will fail (the attribute will not be present). */
6872 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6873 if (dwo_name != nullptr)
6874 {
6875 struct dwo_unit *dwo_unit;
6876 struct die_info *dwo_comp_unit_die;
6877
6878 if (comp_unit_die->has_children)
6879 {
6880 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6881 " has children (offset %s) [in module %s]"),
6882 sect_offset_str (this_cu->sect_off),
6883 bfd_get_filename (abfd));
6884 }
6885 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6886 if (dwo_unit != NULL)
6887 {
6888 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6889 comp_unit_die, NULL,
6890 this, &info_ptr,
6891 &dwo_comp_unit_die,
6892 &m_dwo_abbrev_table) == 0)
6893 {
6894 /* Dummy die. */
6895 dummy_p = true;
6896 return;
6897 }
6898 comp_unit_die = dwo_comp_unit_die;
6899 }
6900 else
6901 {
6902 /* Yikes, we couldn't find the rest of the DIE, we only have
6903 the stub. A complaint has already been logged. There's
6904 not much more we can do except pass on the stub DIE to
6905 die_reader_func. We don't want to throw an error on bad
6906 debug info. */
6907 }
6908 }
6909 }
6910
6911 void
6912 cutu_reader::keep ()
6913 {
6914 /* Done, clean up. */
6915 gdb_assert (!dummy_p);
6916 if (m_new_cu != NULL)
6917 {
6918 struct dwarf2_per_objfile *dwarf2_per_objfile
6919 = m_this_cu->dwarf2_per_objfile;
6920 /* Link this CU into read_in_chain. */
6921 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6922 dwarf2_per_objfile->read_in_chain = m_this_cu;
6923 /* The chain owns it now. */
6924 m_new_cu.release ();
6925 }
6926 }
6927
6928 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6929 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6930 assumed to have already done the lookup to find the DWO file).
6931
6932 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6933 THIS_CU->is_debug_types, but nothing else.
6934
6935 We fill in THIS_CU->length.
6936
6937 THIS_CU->cu is always freed when done.
6938 This is done in order to not leave THIS_CU->cu in a state where we have
6939 to care whether it refers to the "main" CU or the DWO CU.
6940
6941 When parent_cu is passed, it is used to provide a default value for
6942 str_offsets_base and addr_base from the parent. */
6943
6944 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6945 struct dwarf2_cu *parent_cu,
6946 struct dwo_file *dwo_file)
6947 : die_reader_specs {},
6948 m_this_cu (this_cu)
6949 {
6950 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6951 struct objfile *objfile = dwarf2_per_objfile->objfile;
6952 struct dwarf2_section_info *section = this_cu->section;
6953 bfd *abfd = section->get_bfd_owner ();
6954 struct dwarf2_section_info *abbrev_section;
6955 const gdb_byte *begin_info_ptr, *info_ptr;
6956
6957 if (dwarf_die_debug)
6958 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6959 this_cu->is_debug_types ? "type" : "comp",
6960 sect_offset_str (this_cu->sect_off));
6961
6962 gdb_assert (this_cu->cu == NULL);
6963
6964 abbrev_section = (dwo_file != NULL
6965 ? &dwo_file->sections.abbrev
6966 : get_abbrev_section_for_cu (this_cu));
6967
6968 /* This is cheap if the section is already read in. */
6969 section->read (objfile);
6970
6971 m_new_cu.reset (new dwarf2_cu (this_cu));
6972
6973 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6974 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6975 &m_new_cu->header, section,
6976 abbrev_section, info_ptr,
6977 (this_cu->is_debug_types
6978 ? rcuh_kind::TYPE
6979 : rcuh_kind::COMPILE));
6980
6981 if (parent_cu != nullptr)
6982 {
6983 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6984 m_new_cu->addr_base = parent_cu->addr_base;
6985 }
6986 this_cu->length = m_new_cu->header.get_length ();
6987
6988 /* Skip dummy compilation units. */
6989 if (info_ptr >= begin_info_ptr + this_cu->length
6990 || peek_abbrev_code (abfd, info_ptr) == 0)
6991 {
6992 dummy_p = true;
6993 return;
6994 }
6995
6996 m_abbrev_table_holder
6997 = abbrev_table::read (objfile, abbrev_section,
6998 m_new_cu->header.abbrev_sect_off);
6999
7000 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7001 m_abbrev_table_holder.get ());
7002 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7003 }
7004
7005 \f
7006 /* Type Unit Groups.
7007
7008 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7009 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7010 so that all types coming from the same compilation (.o file) are grouped
7011 together. A future step could be to put the types in the same symtab as
7012 the CU the types ultimately came from. */
7013
7014 static hashval_t
7015 hash_type_unit_group (const void *item)
7016 {
7017 const struct type_unit_group *tu_group
7018 = (const struct type_unit_group *) item;
7019
7020 return hash_stmt_list_entry (&tu_group->hash);
7021 }
7022
7023 static int
7024 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7025 {
7026 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7027 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7028
7029 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7030 }
7031
7032 /* Allocate a hash table for type unit groups. */
7033
7034 static htab_up
7035 allocate_type_unit_groups_table ()
7036 {
7037 return htab_up (htab_create_alloc (3,
7038 hash_type_unit_group,
7039 eq_type_unit_group,
7040 NULL, xcalloc, xfree));
7041 }
7042
7043 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7044 partial symtabs. We combine several TUs per psymtab to not let the size
7045 of any one psymtab grow too big. */
7046 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7047 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7048
7049 /* Helper routine for get_type_unit_group.
7050 Create the type_unit_group object used to hold one or more TUs. */
7051
7052 static struct type_unit_group *
7053 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7054 {
7055 struct dwarf2_per_objfile *dwarf2_per_objfile
7056 = cu->per_cu->dwarf2_per_objfile;
7057 struct objfile *objfile = dwarf2_per_objfile->objfile;
7058 struct dwarf2_per_cu_data *per_cu;
7059 struct type_unit_group *tu_group;
7060
7061 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7062 struct type_unit_group);
7063 per_cu = &tu_group->per_cu;
7064 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7065
7066 if (dwarf2_per_objfile->using_index)
7067 {
7068 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7069 struct dwarf2_per_cu_quick_data);
7070 }
7071 else
7072 {
7073 unsigned int line_offset = to_underlying (line_offset_struct);
7074 dwarf2_psymtab *pst;
7075 std::string name;
7076
7077 /* Give the symtab a useful name for debug purposes. */
7078 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7079 name = string_printf ("<type_units_%d>",
7080 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7081 else
7082 name = string_printf ("<type_units_at_0x%x>", line_offset);
7083
7084 pst = create_partial_symtab (per_cu, name.c_str ());
7085 pst->anonymous = true;
7086 }
7087
7088 tu_group->hash.dwo_unit = cu->dwo_unit;
7089 tu_group->hash.line_sect_off = line_offset_struct;
7090
7091 return tu_group;
7092 }
7093
7094 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7095 STMT_LIST is a DW_AT_stmt_list attribute. */
7096
7097 static struct type_unit_group *
7098 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7099 {
7100 struct dwarf2_per_objfile *dwarf2_per_objfile
7101 = cu->per_cu->dwarf2_per_objfile;
7102 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7103 struct type_unit_group *tu_group;
7104 void **slot;
7105 unsigned int line_offset;
7106 struct type_unit_group type_unit_group_for_lookup;
7107
7108 if (dwarf2_per_objfile->type_unit_groups == NULL)
7109 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7110
7111 /* Do we need to create a new group, or can we use an existing one? */
7112
7113 if (stmt_list)
7114 {
7115 line_offset = DW_UNSND (stmt_list);
7116 ++tu_stats->nr_symtab_sharers;
7117 }
7118 else
7119 {
7120 /* Ugh, no stmt_list. Rare, but we have to handle it.
7121 We can do various things here like create one group per TU or
7122 spread them over multiple groups to split up the expansion work.
7123 To avoid worst case scenarios (too many groups or too large groups)
7124 we, umm, group them in bunches. */
7125 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7126 | (tu_stats->nr_stmt_less_type_units
7127 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7128 ++tu_stats->nr_stmt_less_type_units;
7129 }
7130
7131 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7132 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7133 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7134 &type_unit_group_for_lookup, INSERT);
7135 if (*slot != NULL)
7136 {
7137 tu_group = (struct type_unit_group *) *slot;
7138 gdb_assert (tu_group != NULL);
7139 }
7140 else
7141 {
7142 sect_offset line_offset_struct = (sect_offset) line_offset;
7143 tu_group = create_type_unit_group (cu, line_offset_struct);
7144 *slot = tu_group;
7145 ++tu_stats->nr_symtabs;
7146 }
7147
7148 return tu_group;
7149 }
7150 \f
7151 /* Partial symbol tables. */
7152
7153 /* Create a psymtab named NAME and assign it to PER_CU.
7154
7155 The caller must fill in the following details:
7156 dirname, textlow, texthigh. */
7157
7158 static dwarf2_psymtab *
7159 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7160 {
7161 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7162 dwarf2_psymtab *pst;
7163
7164 pst = new dwarf2_psymtab (name, objfile, 0);
7165
7166 pst->psymtabs_addrmap_supported = true;
7167
7168 /* This is the glue that links PST into GDB's symbol API. */
7169 pst->per_cu_data = per_cu;
7170 per_cu->v.psymtab = pst;
7171
7172 return pst;
7173 }
7174
7175 /* DIE reader function for process_psymtab_comp_unit. */
7176
7177 static void
7178 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7179 const gdb_byte *info_ptr,
7180 struct die_info *comp_unit_die,
7181 enum language pretend_language)
7182 {
7183 struct dwarf2_cu *cu = reader->cu;
7184 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7185 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7186 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7187 CORE_ADDR baseaddr;
7188 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7189 dwarf2_psymtab *pst;
7190 enum pc_bounds_kind cu_bounds_kind;
7191 const char *filename;
7192
7193 gdb_assert (! per_cu->is_debug_types);
7194
7195 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7196
7197 /* Allocate a new partial symbol table structure. */
7198 gdb::unique_xmalloc_ptr<char> debug_filename;
7199 static const char artificial[] = "<artificial>";
7200 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7201 if (filename == NULL)
7202 filename = "";
7203 else if (strcmp (filename, artificial) == 0)
7204 {
7205 debug_filename.reset (concat (artificial, "@",
7206 sect_offset_str (per_cu->sect_off),
7207 (char *) NULL));
7208 filename = debug_filename.get ();
7209 }
7210
7211 pst = create_partial_symtab (per_cu, filename);
7212
7213 /* This must be done before calling dwarf2_build_include_psymtabs. */
7214 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7215
7216 baseaddr = objfile->text_section_offset ();
7217
7218 dwarf2_find_base_address (comp_unit_die, cu);
7219
7220 /* Possibly set the default values of LOWPC and HIGHPC from
7221 `DW_AT_ranges'. */
7222 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7223 &best_highpc, cu, pst);
7224 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7225 {
7226 CORE_ADDR low
7227 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7228 - baseaddr);
7229 CORE_ADDR high
7230 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7231 - baseaddr - 1);
7232 /* Store the contiguous range if it is not empty; it can be
7233 empty for CUs with no code. */
7234 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7235 low, high, pst);
7236 }
7237
7238 /* Check if comp unit has_children.
7239 If so, read the rest of the partial symbols from this comp unit.
7240 If not, there's no more debug_info for this comp unit. */
7241 if (comp_unit_die->has_children)
7242 {
7243 struct partial_die_info *first_die;
7244 CORE_ADDR lowpc, highpc;
7245
7246 lowpc = ((CORE_ADDR) -1);
7247 highpc = ((CORE_ADDR) 0);
7248
7249 first_die = load_partial_dies (reader, info_ptr, 1);
7250
7251 scan_partial_symbols (first_die, &lowpc, &highpc,
7252 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7253
7254 /* If we didn't find a lowpc, set it to highpc to avoid
7255 complaints from `maint check'. */
7256 if (lowpc == ((CORE_ADDR) -1))
7257 lowpc = highpc;
7258
7259 /* If the compilation unit didn't have an explicit address range,
7260 then use the information extracted from its child dies. */
7261 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7262 {
7263 best_lowpc = lowpc;
7264 best_highpc = highpc;
7265 }
7266 }
7267 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7268 best_lowpc + baseaddr)
7269 - baseaddr);
7270 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7271 best_highpc + baseaddr)
7272 - baseaddr);
7273
7274 end_psymtab_common (objfile, pst);
7275
7276 if (!cu->per_cu->imported_symtabs_empty ())
7277 {
7278 int i;
7279 int len = cu->per_cu->imported_symtabs_size ();
7280
7281 /* Fill in 'dependencies' here; we fill in 'users' in a
7282 post-pass. */
7283 pst->number_of_dependencies = len;
7284 pst->dependencies
7285 = objfile->partial_symtabs->allocate_dependencies (len);
7286 for (i = 0; i < len; ++i)
7287 {
7288 pst->dependencies[i]
7289 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7290 }
7291
7292 cu->per_cu->imported_symtabs_free ();
7293 }
7294
7295 /* Get the list of files included in the current compilation unit,
7296 and build a psymtab for each of them. */
7297 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7298
7299 if (dwarf_read_debug)
7300 fprintf_unfiltered (gdb_stdlog,
7301 "Psymtab for %s unit @%s: %s - %s"
7302 ", %d global, %d static syms\n",
7303 per_cu->is_debug_types ? "type" : "comp",
7304 sect_offset_str (per_cu->sect_off),
7305 paddress (gdbarch, pst->text_low (objfile)),
7306 paddress (gdbarch, pst->text_high (objfile)),
7307 pst->n_global_syms, pst->n_static_syms);
7308 }
7309
7310 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7311 Process compilation unit THIS_CU for a psymtab. */
7312
7313 static void
7314 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7315 bool want_partial_unit,
7316 enum language pretend_language)
7317 {
7318 /* If this compilation unit was already read in, free the
7319 cached copy in order to read it in again. This is
7320 necessary because we skipped some symbols when we first
7321 read in the compilation unit (see load_partial_dies).
7322 This problem could be avoided, but the benefit is unclear. */
7323 if (this_cu->cu != NULL)
7324 free_one_cached_comp_unit (this_cu);
7325
7326 cutu_reader reader (this_cu, NULL, 0, false);
7327
7328 switch (reader.comp_unit_die->tag)
7329 {
7330 case DW_TAG_compile_unit:
7331 this_cu->unit_type = DW_UT_compile;
7332 break;
7333 case DW_TAG_partial_unit:
7334 this_cu->unit_type = DW_UT_partial;
7335 break;
7336 default:
7337 abort ();
7338 }
7339
7340 if (reader.dummy_p)
7341 {
7342 /* Nothing. */
7343 }
7344 else if (this_cu->is_debug_types)
7345 build_type_psymtabs_reader (&reader, reader.info_ptr,
7346 reader.comp_unit_die);
7347 else if (want_partial_unit
7348 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7349 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7350 reader.comp_unit_die,
7351 pretend_language);
7352
7353 this_cu->lang = this_cu->cu->language;
7354
7355 /* Age out any secondary CUs. */
7356 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7357 }
7358
7359 /* Reader function for build_type_psymtabs. */
7360
7361 static void
7362 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7363 const gdb_byte *info_ptr,
7364 struct die_info *type_unit_die)
7365 {
7366 struct dwarf2_per_objfile *dwarf2_per_objfile
7367 = reader->cu->per_cu->dwarf2_per_objfile;
7368 struct objfile *objfile = dwarf2_per_objfile->objfile;
7369 struct dwarf2_cu *cu = reader->cu;
7370 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7371 struct signatured_type *sig_type;
7372 struct type_unit_group *tu_group;
7373 struct attribute *attr;
7374 struct partial_die_info *first_die;
7375 CORE_ADDR lowpc, highpc;
7376 dwarf2_psymtab *pst;
7377
7378 gdb_assert (per_cu->is_debug_types);
7379 sig_type = (struct signatured_type *) per_cu;
7380
7381 if (! type_unit_die->has_children)
7382 return;
7383
7384 attr = type_unit_die->attr (DW_AT_stmt_list);
7385 tu_group = get_type_unit_group (cu, attr);
7386
7387 if (tu_group->tus == nullptr)
7388 tu_group->tus = new std::vector<signatured_type *>;
7389 tu_group->tus->push_back (sig_type);
7390
7391 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7392 pst = create_partial_symtab (per_cu, "");
7393 pst->anonymous = true;
7394
7395 first_die = load_partial_dies (reader, info_ptr, 1);
7396
7397 lowpc = (CORE_ADDR) -1;
7398 highpc = (CORE_ADDR) 0;
7399 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7400
7401 end_psymtab_common (objfile, pst);
7402 }
7403
7404 /* Struct used to sort TUs by their abbreviation table offset. */
7405
7406 struct tu_abbrev_offset
7407 {
7408 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7409 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7410 {}
7411
7412 signatured_type *sig_type;
7413 sect_offset abbrev_offset;
7414 };
7415
7416 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7417
7418 static bool
7419 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7420 const struct tu_abbrev_offset &b)
7421 {
7422 return a.abbrev_offset < b.abbrev_offset;
7423 }
7424
7425 /* Efficiently read all the type units.
7426 This does the bulk of the work for build_type_psymtabs.
7427
7428 The efficiency is because we sort TUs by the abbrev table they use and
7429 only read each abbrev table once. In one program there are 200K TUs
7430 sharing 8K abbrev tables.
7431
7432 The main purpose of this function is to support building the
7433 dwarf2_per_objfile->type_unit_groups table.
7434 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7435 can collapse the search space by grouping them by stmt_list.
7436 The savings can be significant, in the same program from above the 200K TUs
7437 share 8K stmt_list tables.
7438
7439 FUNC is expected to call get_type_unit_group, which will create the
7440 struct type_unit_group if necessary and add it to
7441 dwarf2_per_objfile->type_unit_groups. */
7442
7443 static void
7444 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7445 {
7446 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7447 abbrev_table_up abbrev_table;
7448 sect_offset abbrev_offset;
7449
7450 /* It's up to the caller to not call us multiple times. */
7451 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7452
7453 if (dwarf2_per_objfile->all_type_units.empty ())
7454 return;
7455
7456 /* TUs typically share abbrev tables, and there can be way more TUs than
7457 abbrev tables. Sort by abbrev table to reduce the number of times we
7458 read each abbrev table in.
7459 Alternatives are to punt or to maintain a cache of abbrev tables.
7460 This is simpler and efficient enough for now.
7461
7462 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7463 symtab to use). Typically TUs with the same abbrev offset have the same
7464 stmt_list value too so in practice this should work well.
7465
7466 The basic algorithm here is:
7467
7468 sort TUs by abbrev table
7469 for each TU with same abbrev table:
7470 read abbrev table if first user
7471 read TU top level DIE
7472 [IWBN if DWO skeletons had DW_AT_stmt_list]
7473 call FUNC */
7474
7475 if (dwarf_read_debug)
7476 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7477
7478 /* Sort in a separate table to maintain the order of all_type_units
7479 for .gdb_index: TU indices directly index all_type_units. */
7480 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7481 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7482
7483 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7484 sorted_by_abbrev.emplace_back
7485 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7486 sig_type->per_cu.section,
7487 sig_type->per_cu.sect_off));
7488
7489 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7490 sort_tu_by_abbrev_offset);
7491
7492 abbrev_offset = (sect_offset) ~(unsigned) 0;
7493
7494 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7495 {
7496 /* Switch to the next abbrev table if necessary. */
7497 if (abbrev_table == NULL
7498 || tu.abbrev_offset != abbrev_offset)
7499 {
7500 abbrev_offset = tu.abbrev_offset;
7501 abbrev_table =
7502 abbrev_table::read (dwarf2_per_objfile->objfile,
7503 &dwarf2_per_objfile->abbrev,
7504 abbrev_offset);
7505 ++tu_stats->nr_uniq_abbrev_tables;
7506 }
7507
7508 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7509 0, false);
7510 if (!reader.dummy_p)
7511 build_type_psymtabs_reader (&reader, reader.info_ptr,
7512 reader.comp_unit_die);
7513 }
7514 }
7515
7516 /* Print collected type unit statistics. */
7517
7518 static void
7519 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7520 {
7521 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7522
7523 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7524 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7525 dwarf2_per_objfile->all_type_units.size ());
7526 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7527 tu_stats->nr_uniq_abbrev_tables);
7528 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7529 tu_stats->nr_symtabs);
7530 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7531 tu_stats->nr_symtab_sharers);
7532 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7533 tu_stats->nr_stmt_less_type_units);
7534 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7535 tu_stats->nr_all_type_units_reallocs);
7536 }
7537
7538 /* Traversal function for build_type_psymtabs. */
7539
7540 static int
7541 build_type_psymtab_dependencies (void **slot, void *info)
7542 {
7543 struct dwarf2_per_objfile *dwarf2_per_objfile
7544 = (struct dwarf2_per_objfile *) info;
7545 struct objfile *objfile = dwarf2_per_objfile->objfile;
7546 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7547 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7548 dwarf2_psymtab *pst = per_cu->v.psymtab;
7549 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7550 int i;
7551
7552 gdb_assert (len > 0);
7553 gdb_assert (per_cu->type_unit_group_p ());
7554
7555 pst->number_of_dependencies = len;
7556 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7557 for (i = 0; i < len; ++i)
7558 {
7559 struct signatured_type *iter = tu_group->tus->at (i);
7560 gdb_assert (iter->per_cu.is_debug_types);
7561 pst->dependencies[i] = iter->per_cu.v.psymtab;
7562 iter->type_unit_group = tu_group;
7563 }
7564
7565 delete tu_group->tus;
7566 tu_group->tus = nullptr;
7567
7568 return 1;
7569 }
7570
7571 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7572 Build partial symbol tables for the .debug_types comp-units. */
7573
7574 static void
7575 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7576 {
7577 if (! create_all_type_units (dwarf2_per_objfile))
7578 return;
7579
7580 build_type_psymtabs_1 (dwarf2_per_objfile);
7581 }
7582
7583 /* Traversal function for process_skeletonless_type_unit.
7584 Read a TU in a DWO file and build partial symbols for it. */
7585
7586 static int
7587 process_skeletonless_type_unit (void **slot, void *info)
7588 {
7589 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7590 struct dwarf2_per_objfile *dwarf2_per_objfile
7591 = (struct dwarf2_per_objfile *) info;
7592 struct signatured_type find_entry, *entry;
7593
7594 /* If this TU doesn't exist in the global table, add it and read it in. */
7595
7596 if (dwarf2_per_objfile->signatured_types == NULL)
7597 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7598
7599 find_entry.signature = dwo_unit->signature;
7600 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7601 &find_entry, INSERT);
7602 /* If we've already seen this type there's nothing to do. What's happening
7603 is we're doing our own version of comdat-folding here. */
7604 if (*slot != NULL)
7605 return 1;
7606
7607 /* This does the job that create_all_type_units would have done for
7608 this TU. */
7609 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7610 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7611 *slot = entry;
7612
7613 /* This does the job that build_type_psymtabs_1 would have done. */
7614 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7615 if (!reader.dummy_p)
7616 build_type_psymtabs_reader (&reader, reader.info_ptr,
7617 reader.comp_unit_die);
7618
7619 return 1;
7620 }
7621
7622 /* Traversal function for process_skeletonless_type_units. */
7623
7624 static int
7625 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7626 {
7627 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7628
7629 if (dwo_file->tus != NULL)
7630 htab_traverse_noresize (dwo_file->tus.get (),
7631 process_skeletonless_type_unit, info);
7632
7633 return 1;
7634 }
7635
7636 /* Scan all TUs of DWO files, verifying we've processed them.
7637 This is needed in case a TU was emitted without its skeleton.
7638 Note: This can't be done until we know what all the DWO files are. */
7639
7640 static void
7641 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7642 {
7643 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7644 if (get_dwp_file (dwarf2_per_objfile) == NULL
7645 && dwarf2_per_objfile->dwo_files != NULL)
7646 {
7647 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7648 process_dwo_file_for_skeletonless_type_units,
7649 dwarf2_per_objfile);
7650 }
7651 }
7652
7653 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7654
7655 static void
7656 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7657 {
7658 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7659 {
7660 dwarf2_psymtab *pst = per_cu->v.psymtab;
7661
7662 if (pst == NULL)
7663 continue;
7664
7665 for (int j = 0; j < pst->number_of_dependencies; ++j)
7666 {
7667 /* Set the 'user' field only if it is not already set. */
7668 if (pst->dependencies[j]->user == NULL)
7669 pst->dependencies[j]->user = pst;
7670 }
7671 }
7672 }
7673
7674 /* Build the partial symbol table by doing a quick pass through the
7675 .debug_info and .debug_abbrev sections. */
7676
7677 static void
7678 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7679 {
7680 struct objfile *objfile = dwarf2_per_objfile->objfile;
7681
7682 if (dwarf_read_debug)
7683 {
7684 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7685 objfile_name (objfile));
7686 }
7687
7688 scoped_restore restore_reading_psyms
7689 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7690 true);
7691
7692 dwarf2_per_objfile->info.read (objfile);
7693
7694 /* Any cached compilation units will be linked by the per-objfile
7695 read_in_chain. Make sure to free them when we're done. */
7696 free_cached_comp_units freer (dwarf2_per_objfile);
7697
7698 build_type_psymtabs (dwarf2_per_objfile);
7699
7700 create_all_comp_units (dwarf2_per_objfile);
7701
7702 /* Create a temporary address map on a temporary obstack. We later
7703 copy this to the final obstack. */
7704 auto_obstack temp_obstack;
7705
7706 scoped_restore save_psymtabs_addrmap
7707 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7708 addrmap_create_mutable (&temp_obstack));
7709
7710 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7711 process_psymtab_comp_unit (per_cu, false, language_minimal);
7712
7713 /* This has to wait until we read the CUs, we need the list of DWOs. */
7714 process_skeletonless_type_units (dwarf2_per_objfile);
7715
7716 /* Now that all TUs have been processed we can fill in the dependencies. */
7717 if (dwarf2_per_objfile->type_unit_groups != NULL)
7718 {
7719 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7720 build_type_psymtab_dependencies, dwarf2_per_objfile);
7721 }
7722
7723 if (dwarf_read_debug)
7724 print_tu_stats (dwarf2_per_objfile);
7725
7726 set_partial_user (dwarf2_per_objfile);
7727
7728 objfile->partial_symtabs->psymtabs_addrmap
7729 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7730 objfile->partial_symtabs->obstack ());
7731 /* At this point we want to keep the address map. */
7732 save_psymtabs_addrmap.release ();
7733
7734 if (dwarf_read_debug)
7735 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7736 objfile_name (objfile));
7737 }
7738
7739 /* Load the partial DIEs for a secondary CU into memory.
7740 This is also used when rereading a primary CU with load_all_dies. */
7741
7742 static void
7743 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7744 {
7745 cutu_reader reader (this_cu, NULL, 1, false);
7746
7747 if (!reader.dummy_p)
7748 {
7749 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7750 language_minimal);
7751
7752 /* Check if comp unit has_children.
7753 If so, read the rest of the partial symbols from this comp unit.
7754 If not, there's no more debug_info for this comp unit. */
7755 if (reader.comp_unit_die->has_children)
7756 load_partial_dies (&reader, reader.info_ptr, 0);
7757
7758 reader.keep ();
7759 }
7760 }
7761
7762 static void
7763 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7764 struct dwarf2_section_info *section,
7765 struct dwarf2_section_info *abbrev_section,
7766 unsigned int is_dwz)
7767 {
7768 const gdb_byte *info_ptr;
7769 struct objfile *objfile = dwarf2_per_objfile->objfile;
7770
7771 if (dwarf_read_debug)
7772 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7773 section->get_name (),
7774 section->get_file_name ());
7775
7776 section->read (objfile);
7777
7778 info_ptr = section->buffer;
7779
7780 while (info_ptr < section->buffer + section->size)
7781 {
7782 struct dwarf2_per_cu_data *this_cu;
7783
7784 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7785
7786 comp_unit_head cu_header;
7787 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7788 abbrev_section, info_ptr,
7789 rcuh_kind::COMPILE);
7790
7791 /* Save the compilation unit for later lookup. */
7792 if (cu_header.unit_type != DW_UT_type)
7793 {
7794 this_cu = XOBNEW (&objfile->objfile_obstack,
7795 struct dwarf2_per_cu_data);
7796 memset (this_cu, 0, sizeof (*this_cu));
7797 }
7798 else
7799 {
7800 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7801 struct signatured_type);
7802 memset (sig_type, 0, sizeof (*sig_type));
7803 sig_type->signature = cu_header.signature;
7804 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7805 this_cu = &sig_type->per_cu;
7806 }
7807 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7808 this_cu->sect_off = sect_off;
7809 this_cu->length = cu_header.length + cu_header.initial_length_size;
7810 this_cu->is_dwz = is_dwz;
7811 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7812 this_cu->section = section;
7813
7814 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7815
7816 info_ptr = info_ptr + this_cu->length;
7817 }
7818 }
7819
7820 /* Create a list of all compilation units in OBJFILE.
7821 This is only done for -readnow and building partial symtabs. */
7822
7823 static void
7824 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7825 {
7826 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7827 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7828 &dwarf2_per_objfile->abbrev, 0);
7829
7830 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7831 if (dwz != NULL)
7832 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7833 1);
7834 }
7835
7836 /* Process all loaded DIEs for compilation unit CU, starting at
7837 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7838 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7839 DW_AT_ranges). See the comments of add_partial_subprogram on how
7840 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7841
7842 static void
7843 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7844 CORE_ADDR *highpc, int set_addrmap,
7845 struct dwarf2_cu *cu)
7846 {
7847 struct partial_die_info *pdi;
7848
7849 /* Now, march along the PDI's, descending into ones which have
7850 interesting children but skipping the children of the other ones,
7851 until we reach the end of the compilation unit. */
7852
7853 pdi = first_die;
7854
7855 while (pdi != NULL)
7856 {
7857 pdi->fixup (cu);
7858
7859 /* Anonymous namespaces or modules have no name but have interesting
7860 children, so we need to look at them. Ditto for anonymous
7861 enums. */
7862
7863 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7864 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7865 || pdi->tag == DW_TAG_imported_unit
7866 || pdi->tag == DW_TAG_inlined_subroutine)
7867 {
7868 switch (pdi->tag)
7869 {
7870 case DW_TAG_subprogram:
7871 case DW_TAG_inlined_subroutine:
7872 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7873 break;
7874 case DW_TAG_constant:
7875 case DW_TAG_variable:
7876 case DW_TAG_typedef:
7877 case DW_TAG_union_type:
7878 if (!pdi->is_declaration)
7879 {
7880 add_partial_symbol (pdi, cu);
7881 }
7882 break;
7883 case DW_TAG_class_type:
7884 case DW_TAG_interface_type:
7885 case DW_TAG_structure_type:
7886 if (!pdi->is_declaration)
7887 {
7888 add_partial_symbol (pdi, cu);
7889 }
7890 if ((cu->language == language_rust
7891 || cu->language == language_cplus) && pdi->has_children)
7892 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7893 set_addrmap, cu);
7894 break;
7895 case DW_TAG_enumeration_type:
7896 if (!pdi->is_declaration)
7897 add_partial_enumeration (pdi, cu);
7898 break;
7899 case DW_TAG_base_type:
7900 case DW_TAG_subrange_type:
7901 /* File scope base type definitions are added to the partial
7902 symbol table. */
7903 add_partial_symbol (pdi, cu);
7904 break;
7905 case DW_TAG_namespace:
7906 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7907 break;
7908 case DW_TAG_module:
7909 if (!pdi->is_declaration)
7910 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7911 break;
7912 case DW_TAG_imported_unit:
7913 {
7914 struct dwarf2_per_cu_data *per_cu;
7915
7916 /* For now we don't handle imported units in type units. */
7917 if (cu->per_cu->is_debug_types)
7918 {
7919 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7920 " supported in type units [in module %s]"),
7921 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7922 }
7923
7924 per_cu = dwarf2_find_containing_comp_unit
7925 (pdi->d.sect_off, pdi->is_dwz,
7926 cu->per_cu->dwarf2_per_objfile);
7927
7928 /* Go read the partial unit, if needed. */
7929 if (per_cu->v.psymtab == NULL)
7930 process_psymtab_comp_unit (per_cu, true, cu->language);
7931
7932 cu->per_cu->imported_symtabs_push (per_cu);
7933 }
7934 break;
7935 case DW_TAG_imported_declaration:
7936 add_partial_symbol (pdi, cu);
7937 break;
7938 default:
7939 break;
7940 }
7941 }
7942
7943 /* If the die has a sibling, skip to the sibling. */
7944
7945 pdi = pdi->die_sibling;
7946 }
7947 }
7948
7949 /* Functions used to compute the fully scoped name of a partial DIE.
7950
7951 Normally, this is simple. For C++, the parent DIE's fully scoped
7952 name is concatenated with "::" and the partial DIE's name.
7953 Enumerators are an exception; they use the scope of their parent
7954 enumeration type, i.e. the name of the enumeration type is not
7955 prepended to the enumerator.
7956
7957 There are two complexities. One is DW_AT_specification; in this
7958 case "parent" means the parent of the target of the specification,
7959 instead of the direct parent of the DIE. The other is compilers
7960 which do not emit DW_TAG_namespace; in this case we try to guess
7961 the fully qualified name of structure types from their members'
7962 linkage names. This must be done using the DIE's children rather
7963 than the children of any DW_AT_specification target. We only need
7964 to do this for structures at the top level, i.e. if the target of
7965 any DW_AT_specification (if any; otherwise the DIE itself) does not
7966 have a parent. */
7967
7968 /* Compute the scope prefix associated with PDI's parent, in
7969 compilation unit CU. The result will be allocated on CU's
7970 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7971 field. NULL is returned if no prefix is necessary. */
7972 static const char *
7973 partial_die_parent_scope (struct partial_die_info *pdi,
7974 struct dwarf2_cu *cu)
7975 {
7976 const char *grandparent_scope;
7977 struct partial_die_info *parent, *real_pdi;
7978
7979 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7980 then this means the parent of the specification DIE. */
7981
7982 real_pdi = pdi;
7983 while (real_pdi->has_specification)
7984 {
7985 auto res = find_partial_die (real_pdi->spec_offset,
7986 real_pdi->spec_is_dwz, cu);
7987 real_pdi = res.pdi;
7988 cu = res.cu;
7989 }
7990
7991 parent = real_pdi->die_parent;
7992 if (parent == NULL)
7993 return NULL;
7994
7995 if (parent->scope_set)
7996 return parent->scope;
7997
7998 parent->fixup (cu);
7999
8000 grandparent_scope = partial_die_parent_scope (parent, cu);
8001
8002 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8003 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8004 Work around this problem here. */
8005 if (cu->language == language_cplus
8006 && parent->tag == DW_TAG_namespace
8007 && strcmp (parent->name, "::") == 0
8008 && grandparent_scope == NULL)
8009 {
8010 parent->scope = NULL;
8011 parent->scope_set = 1;
8012 return NULL;
8013 }
8014
8015 /* Nested subroutines in Fortran get a prefix. */
8016 if (pdi->tag == DW_TAG_enumerator)
8017 /* Enumerators should not get the name of the enumeration as a prefix. */
8018 parent->scope = grandparent_scope;
8019 else if (parent->tag == DW_TAG_namespace
8020 || parent->tag == DW_TAG_module
8021 || parent->tag == DW_TAG_structure_type
8022 || parent->tag == DW_TAG_class_type
8023 || parent->tag == DW_TAG_interface_type
8024 || parent->tag == DW_TAG_union_type
8025 || parent->tag == DW_TAG_enumeration_type
8026 || (cu->language == language_fortran
8027 && parent->tag == DW_TAG_subprogram
8028 && pdi->tag == DW_TAG_subprogram))
8029 {
8030 if (grandparent_scope == NULL)
8031 parent->scope = parent->name;
8032 else
8033 parent->scope = typename_concat (&cu->comp_unit_obstack,
8034 grandparent_scope,
8035 parent->name, 0, cu);
8036 }
8037 else
8038 {
8039 /* FIXME drow/2004-04-01: What should we be doing with
8040 function-local names? For partial symbols, we should probably be
8041 ignoring them. */
8042 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8043 dwarf_tag_name (parent->tag),
8044 sect_offset_str (pdi->sect_off));
8045 parent->scope = grandparent_scope;
8046 }
8047
8048 parent->scope_set = 1;
8049 return parent->scope;
8050 }
8051
8052 /* Return the fully scoped name associated with PDI, from compilation unit
8053 CU. The result will be allocated with malloc. */
8054
8055 static gdb::unique_xmalloc_ptr<char>
8056 partial_die_full_name (struct partial_die_info *pdi,
8057 struct dwarf2_cu *cu)
8058 {
8059 const char *parent_scope;
8060
8061 /* If this is a template instantiation, we can not work out the
8062 template arguments from partial DIEs. So, unfortunately, we have
8063 to go through the full DIEs. At least any work we do building
8064 types here will be reused if full symbols are loaded later. */
8065 if (pdi->has_template_arguments)
8066 {
8067 pdi->fixup (cu);
8068
8069 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8070 {
8071 struct die_info *die;
8072 struct attribute attr;
8073 struct dwarf2_cu *ref_cu = cu;
8074
8075 /* DW_FORM_ref_addr is using section offset. */
8076 attr.name = (enum dwarf_attribute) 0;
8077 attr.form = DW_FORM_ref_addr;
8078 attr.u.unsnd = to_underlying (pdi->sect_off);
8079 die = follow_die_ref (NULL, &attr, &ref_cu);
8080
8081 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8082 }
8083 }
8084
8085 parent_scope = partial_die_parent_scope (pdi, cu);
8086 if (parent_scope == NULL)
8087 return NULL;
8088 else
8089 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8090 pdi->name, 0, cu));
8091 }
8092
8093 static void
8094 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8095 {
8096 struct dwarf2_per_objfile *dwarf2_per_objfile
8097 = cu->per_cu->dwarf2_per_objfile;
8098 struct objfile *objfile = dwarf2_per_objfile->objfile;
8099 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8100 CORE_ADDR addr = 0;
8101 const char *actual_name = NULL;
8102 CORE_ADDR baseaddr;
8103
8104 baseaddr = objfile->text_section_offset ();
8105
8106 gdb::unique_xmalloc_ptr<char> built_actual_name
8107 = partial_die_full_name (pdi, cu);
8108 if (built_actual_name != NULL)
8109 actual_name = built_actual_name.get ();
8110
8111 if (actual_name == NULL)
8112 actual_name = pdi->name;
8113
8114 switch (pdi->tag)
8115 {
8116 case DW_TAG_inlined_subroutine:
8117 case DW_TAG_subprogram:
8118 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8119 - baseaddr);
8120 if (pdi->is_external
8121 || cu->language == language_ada
8122 || (cu->language == language_fortran
8123 && pdi->die_parent != NULL
8124 && pdi->die_parent->tag == DW_TAG_subprogram))
8125 {
8126 /* Normally, only "external" DIEs are part of the global scope.
8127 But in Ada and Fortran, we want to be able to access nested
8128 procedures globally. So all Ada and Fortran subprograms are
8129 stored in the global scope. */
8130 add_psymbol_to_list (actual_name,
8131 built_actual_name != NULL,
8132 VAR_DOMAIN, LOC_BLOCK,
8133 SECT_OFF_TEXT (objfile),
8134 psymbol_placement::GLOBAL,
8135 addr,
8136 cu->language, objfile);
8137 }
8138 else
8139 {
8140 add_psymbol_to_list (actual_name,
8141 built_actual_name != NULL,
8142 VAR_DOMAIN, LOC_BLOCK,
8143 SECT_OFF_TEXT (objfile),
8144 psymbol_placement::STATIC,
8145 addr, cu->language, objfile);
8146 }
8147
8148 if (pdi->main_subprogram && actual_name != NULL)
8149 set_objfile_main_name (objfile, actual_name, cu->language);
8150 break;
8151 case DW_TAG_constant:
8152 add_psymbol_to_list (actual_name,
8153 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8154 -1, (pdi->is_external
8155 ? psymbol_placement::GLOBAL
8156 : psymbol_placement::STATIC),
8157 0, cu->language, objfile);
8158 break;
8159 case DW_TAG_variable:
8160 if (pdi->d.locdesc)
8161 addr = decode_locdesc (pdi->d.locdesc, cu);
8162
8163 if (pdi->d.locdesc
8164 && addr == 0
8165 && !dwarf2_per_objfile->has_section_at_zero)
8166 {
8167 /* A global or static variable may also have been stripped
8168 out by the linker if unused, in which case its address
8169 will be nullified; do not add such variables into partial
8170 symbol table then. */
8171 }
8172 else if (pdi->is_external)
8173 {
8174 /* Global Variable.
8175 Don't enter into the minimal symbol tables as there is
8176 a minimal symbol table entry from the ELF symbols already.
8177 Enter into partial symbol table if it has a location
8178 descriptor or a type.
8179 If the location descriptor is missing, new_symbol will create
8180 a LOC_UNRESOLVED symbol, the address of the variable will then
8181 be determined from the minimal symbol table whenever the variable
8182 is referenced.
8183 The address for the partial symbol table entry is not
8184 used by GDB, but it comes in handy for debugging partial symbol
8185 table building. */
8186
8187 if (pdi->d.locdesc || pdi->has_type)
8188 add_psymbol_to_list (actual_name,
8189 built_actual_name != NULL,
8190 VAR_DOMAIN, LOC_STATIC,
8191 SECT_OFF_TEXT (objfile),
8192 psymbol_placement::GLOBAL,
8193 addr, cu->language, objfile);
8194 }
8195 else
8196 {
8197 int has_loc = pdi->d.locdesc != NULL;
8198
8199 /* Static Variable. Skip symbols whose value we cannot know (those
8200 without location descriptors or constant values). */
8201 if (!has_loc && !pdi->has_const_value)
8202 return;
8203
8204 add_psymbol_to_list (actual_name,
8205 built_actual_name != NULL,
8206 VAR_DOMAIN, LOC_STATIC,
8207 SECT_OFF_TEXT (objfile),
8208 psymbol_placement::STATIC,
8209 has_loc ? addr : 0,
8210 cu->language, objfile);
8211 }
8212 break;
8213 case DW_TAG_typedef:
8214 case DW_TAG_base_type:
8215 case DW_TAG_subrange_type:
8216 add_psymbol_to_list (actual_name,
8217 built_actual_name != NULL,
8218 VAR_DOMAIN, LOC_TYPEDEF, -1,
8219 psymbol_placement::STATIC,
8220 0, cu->language, objfile);
8221 break;
8222 case DW_TAG_imported_declaration:
8223 case DW_TAG_namespace:
8224 add_psymbol_to_list (actual_name,
8225 built_actual_name != NULL,
8226 VAR_DOMAIN, LOC_TYPEDEF, -1,
8227 psymbol_placement::GLOBAL,
8228 0, cu->language, objfile);
8229 break;
8230 case DW_TAG_module:
8231 /* With Fortran 77 there might be a "BLOCK DATA" module
8232 available without any name. If so, we skip the module as it
8233 doesn't bring any value. */
8234 if (actual_name != nullptr)
8235 add_psymbol_to_list (actual_name,
8236 built_actual_name != NULL,
8237 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8238 psymbol_placement::GLOBAL,
8239 0, cu->language, objfile);
8240 break;
8241 case DW_TAG_class_type:
8242 case DW_TAG_interface_type:
8243 case DW_TAG_structure_type:
8244 case DW_TAG_union_type:
8245 case DW_TAG_enumeration_type:
8246 /* Skip external references. The DWARF standard says in the section
8247 about "Structure, Union, and Class Type Entries": "An incomplete
8248 structure, union or class type is represented by a structure,
8249 union or class entry that does not have a byte size attribute
8250 and that has a DW_AT_declaration attribute." */
8251 if (!pdi->has_byte_size && pdi->is_declaration)
8252 return;
8253
8254 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8255 static vs. global. */
8256 add_psymbol_to_list (actual_name,
8257 built_actual_name != NULL,
8258 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8259 cu->language == language_cplus
8260 ? psymbol_placement::GLOBAL
8261 : psymbol_placement::STATIC,
8262 0, cu->language, objfile);
8263
8264 break;
8265 case DW_TAG_enumerator:
8266 add_psymbol_to_list (actual_name,
8267 built_actual_name != NULL,
8268 VAR_DOMAIN, LOC_CONST, -1,
8269 cu->language == language_cplus
8270 ? psymbol_placement::GLOBAL
8271 : psymbol_placement::STATIC,
8272 0, cu->language, objfile);
8273 break;
8274 default:
8275 break;
8276 }
8277 }
8278
8279 /* Read a partial die corresponding to a namespace; also, add a symbol
8280 corresponding to that namespace to the symbol table. NAMESPACE is
8281 the name of the enclosing namespace. */
8282
8283 static void
8284 add_partial_namespace (struct partial_die_info *pdi,
8285 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8286 int set_addrmap, struct dwarf2_cu *cu)
8287 {
8288 /* Add a symbol for the namespace. */
8289
8290 add_partial_symbol (pdi, cu);
8291
8292 /* Now scan partial symbols in that namespace. */
8293
8294 if (pdi->has_children)
8295 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8296 }
8297
8298 /* Read a partial die corresponding to a Fortran module. */
8299
8300 static void
8301 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8302 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8303 {
8304 /* Add a symbol for the namespace. */
8305
8306 add_partial_symbol (pdi, cu);
8307
8308 /* Now scan partial symbols in that module. */
8309
8310 if (pdi->has_children)
8311 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8312 }
8313
8314 /* Read a partial die corresponding to a subprogram or an inlined
8315 subprogram and create a partial symbol for that subprogram.
8316 When the CU language allows it, this routine also defines a partial
8317 symbol for each nested subprogram that this subprogram contains.
8318 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8319 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8320
8321 PDI may also be a lexical block, in which case we simply search
8322 recursively for subprograms defined inside that lexical block.
8323 Again, this is only performed when the CU language allows this
8324 type of definitions. */
8325
8326 static void
8327 add_partial_subprogram (struct partial_die_info *pdi,
8328 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8329 int set_addrmap, struct dwarf2_cu *cu)
8330 {
8331 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8332 {
8333 if (pdi->has_pc_info)
8334 {
8335 if (pdi->lowpc < *lowpc)
8336 *lowpc = pdi->lowpc;
8337 if (pdi->highpc > *highpc)
8338 *highpc = pdi->highpc;
8339 if (set_addrmap)
8340 {
8341 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8342 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8343 CORE_ADDR baseaddr;
8344 CORE_ADDR this_highpc;
8345 CORE_ADDR this_lowpc;
8346
8347 baseaddr = objfile->text_section_offset ();
8348 this_lowpc
8349 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8350 pdi->lowpc + baseaddr)
8351 - baseaddr);
8352 this_highpc
8353 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8354 pdi->highpc + baseaddr)
8355 - baseaddr);
8356 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8357 this_lowpc, this_highpc - 1,
8358 cu->per_cu->v.psymtab);
8359 }
8360 }
8361
8362 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8363 {
8364 if (!pdi->is_declaration)
8365 /* Ignore subprogram DIEs that do not have a name, they are
8366 illegal. Do not emit a complaint at this point, we will
8367 do so when we convert this psymtab into a symtab. */
8368 if (pdi->name)
8369 add_partial_symbol (pdi, cu);
8370 }
8371 }
8372
8373 if (! pdi->has_children)
8374 return;
8375
8376 if (cu->language == language_ada || cu->language == language_fortran)
8377 {
8378 pdi = pdi->die_child;
8379 while (pdi != NULL)
8380 {
8381 pdi->fixup (cu);
8382 if (pdi->tag == DW_TAG_subprogram
8383 || pdi->tag == DW_TAG_inlined_subroutine
8384 || pdi->tag == DW_TAG_lexical_block)
8385 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8386 pdi = pdi->die_sibling;
8387 }
8388 }
8389 }
8390
8391 /* Read a partial die corresponding to an enumeration type. */
8392
8393 static void
8394 add_partial_enumeration (struct partial_die_info *enum_pdi,
8395 struct dwarf2_cu *cu)
8396 {
8397 struct partial_die_info *pdi;
8398
8399 if (enum_pdi->name != NULL)
8400 add_partial_symbol (enum_pdi, cu);
8401
8402 pdi = enum_pdi->die_child;
8403 while (pdi)
8404 {
8405 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8406 complaint (_("malformed enumerator DIE ignored"));
8407 else
8408 add_partial_symbol (pdi, cu);
8409 pdi = pdi->die_sibling;
8410 }
8411 }
8412
8413 /* Return the initial uleb128 in the die at INFO_PTR. */
8414
8415 static unsigned int
8416 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8417 {
8418 unsigned int bytes_read;
8419
8420 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8421 }
8422
8423 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8424 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8425
8426 Return the corresponding abbrev, or NULL if the number is zero (indicating
8427 an empty DIE). In either case *BYTES_READ will be set to the length of
8428 the initial number. */
8429
8430 static struct abbrev_info *
8431 peek_die_abbrev (const die_reader_specs &reader,
8432 const gdb_byte *info_ptr, unsigned int *bytes_read)
8433 {
8434 dwarf2_cu *cu = reader.cu;
8435 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8436 unsigned int abbrev_number
8437 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8438
8439 if (abbrev_number == 0)
8440 return NULL;
8441
8442 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8443 if (!abbrev)
8444 {
8445 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8446 " at offset %s [in module %s]"),
8447 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8448 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8449 }
8450
8451 return abbrev;
8452 }
8453
8454 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8455 Returns a pointer to the end of a series of DIEs, terminated by an empty
8456 DIE. Any children of the skipped DIEs will also be skipped. */
8457
8458 static const gdb_byte *
8459 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8460 {
8461 while (1)
8462 {
8463 unsigned int bytes_read;
8464 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8465
8466 if (abbrev == NULL)
8467 return info_ptr + bytes_read;
8468 else
8469 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8470 }
8471 }
8472
8473 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8474 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8475 abbrev corresponding to that skipped uleb128 should be passed in
8476 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8477 children. */
8478
8479 static const gdb_byte *
8480 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8481 struct abbrev_info *abbrev)
8482 {
8483 unsigned int bytes_read;
8484 struct attribute attr;
8485 bfd *abfd = reader->abfd;
8486 struct dwarf2_cu *cu = reader->cu;
8487 const gdb_byte *buffer = reader->buffer;
8488 const gdb_byte *buffer_end = reader->buffer_end;
8489 unsigned int form, i;
8490
8491 for (i = 0; i < abbrev->num_attrs; i++)
8492 {
8493 /* The only abbrev we care about is DW_AT_sibling. */
8494 if (abbrev->attrs[i].name == DW_AT_sibling)
8495 {
8496 bool ignored;
8497 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8498 &ignored);
8499 if (attr.form == DW_FORM_ref_addr)
8500 complaint (_("ignoring absolute DW_AT_sibling"));
8501 else
8502 {
8503 sect_offset off = attr.get_ref_die_offset ();
8504 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8505
8506 if (sibling_ptr < info_ptr)
8507 complaint (_("DW_AT_sibling points backwards"));
8508 else if (sibling_ptr > reader->buffer_end)
8509 reader->die_section->overflow_complaint ();
8510 else
8511 return sibling_ptr;
8512 }
8513 }
8514
8515 /* If it isn't DW_AT_sibling, skip this attribute. */
8516 form = abbrev->attrs[i].form;
8517 skip_attribute:
8518 switch (form)
8519 {
8520 case DW_FORM_ref_addr:
8521 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8522 and later it is offset sized. */
8523 if (cu->header.version == 2)
8524 info_ptr += cu->header.addr_size;
8525 else
8526 info_ptr += cu->header.offset_size;
8527 break;
8528 case DW_FORM_GNU_ref_alt:
8529 info_ptr += cu->header.offset_size;
8530 break;
8531 case DW_FORM_addr:
8532 info_ptr += cu->header.addr_size;
8533 break;
8534 case DW_FORM_data1:
8535 case DW_FORM_ref1:
8536 case DW_FORM_flag:
8537 case DW_FORM_strx1:
8538 info_ptr += 1;
8539 break;
8540 case DW_FORM_flag_present:
8541 case DW_FORM_implicit_const:
8542 break;
8543 case DW_FORM_data2:
8544 case DW_FORM_ref2:
8545 case DW_FORM_strx2:
8546 info_ptr += 2;
8547 break;
8548 case DW_FORM_strx3:
8549 info_ptr += 3;
8550 break;
8551 case DW_FORM_data4:
8552 case DW_FORM_ref4:
8553 case DW_FORM_strx4:
8554 info_ptr += 4;
8555 break;
8556 case DW_FORM_data8:
8557 case DW_FORM_ref8:
8558 case DW_FORM_ref_sig8:
8559 info_ptr += 8;
8560 break;
8561 case DW_FORM_data16:
8562 info_ptr += 16;
8563 break;
8564 case DW_FORM_string:
8565 read_direct_string (abfd, info_ptr, &bytes_read);
8566 info_ptr += bytes_read;
8567 break;
8568 case DW_FORM_sec_offset:
8569 case DW_FORM_strp:
8570 case DW_FORM_GNU_strp_alt:
8571 info_ptr += cu->header.offset_size;
8572 break;
8573 case DW_FORM_exprloc:
8574 case DW_FORM_block:
8575 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8576 info_ptr += bytes_read;
8577 break;
8578 case DW_FORM_block1:
8579 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8580 break;
8581 case DW_FORM_block2:
8582 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8583 break;
8584 case DW_FORM_block4:
8585 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8586 break;
8587 case DW_FORM_addrx:
8588 case DW_FORM_strx:
8589 case DW_FORM_sdata:
8590 case DW_FORM_udata:
8591 case DW_FORM_ref_udata:
8592 case DW_FORM_GNU_addr_index:
8593 case DW_FORM_GNU_str_index:
8594 case DW_FORM_rnglistx:
8595 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8596 break;
8597 case DW_FORM_indirect:
8598 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8599 info_ptr += bytes_read;
8600 /* We need to continue parsing from here, so just go back to
8601 the top. */
8602 goto skip_attribute;
8603
8604 default:
8605 error (_("Dwarf Error: Cannot handle %s "
8606 "in DWARF reader [in module %s]"),
8607 dwarf_form_name (form),
8608 bfd_get_filename (abfd));
8609 }
8610 }
8611
8612 if (abbrev->has_children)
8613 return skip_children (reader, info_ptr);
8614 else
8615 return info_ptr;
8616 }
8617
8618 /* Locate ORIG_PDI's sibling.
8619 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8620
8621 static const gdb_byte *
8622 locate_pdi_sibling (const struct die_reader_specs *reader,
8623 struct partial_die_info *orig_pdi,
8624 const gdb_byte *info_ptr)
8625 {
8626 /* Do we know the sibling already? */
8627
8628 if (orig_pdi->sibling)
8629 return orig_pdi->sibling;
8630
8631 /* Are there any children to deal with? */
8632
8633 if (!orig_pdi->has_children)
8634 return info_ptr;
8635
8636 /* Skip the children the long way. */
8637
8638 return skip_children (reader, info_ptr);
8639 }
8640
8641 /* Expand this partial symbol table into a full symbol table. SELF is
8642 not NULL. */
8643
8644 void
8645 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8646 {
8647 struct dwarf2_per_objfile *dwarf2_per_objfile
8648 = get_dwarf2_per_objfile (objfile);
8649
8650 gdb_assert (!readin);
8651 /* If this psymtab is constructed from a debug-only objfile, the
8652 has_section_at_zero flag will not necessarily be correct. We
8653 can get the correct value for this flag by looking at the data
8654 associated with the (presumably stripped) associated objfile. */
8655 if (objfile->separate_debug_objfile_backlink)
8656 {
8657 struct dwarf2_per_objfile *dpo_backlink
8658 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8659
8660 dwarf2_per_objfile->has_section_at_zero
8661 = dpo_backlink->has_section_at_zero;
8662 }
8663
8664 expand_psymtab (objfile);
8665
8666 process_cu_includes (dwarf2_per_objfile);
8667 }
8668 \f
8669 /* Reading in full CUs. */
8670
8671 /* Add PER_CU to the queue. */
8672
8673 static void
8674 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8675 enum language pretend_language)
8676 {
8677 per_cu->queued = 1;
8678 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8679 }
8680
8681 /* If PER_CU is not yet queued, add it to the queue.
8682 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8683 dependency.
8684 The result is non-zero if PER_CU was queued, otherwise the result is zero
8685 meaning either PER_CU is already queued or it is already loaded.
8686
8687 N.B. There is an invariant here that if a CU is queued then it is loaded.
8688 The caller is required to load PER_CU if we return non-zero. */
8689
8690 static int
8691 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8692 struct dwarf2_per_cu_data *per_cu,
8693 enum language pretend_language)
8694 {
8695 /* We may arrive here during partial symbol reading, if we need full
8696 DIEs to process an unusual case (e.g. template arguments). Do
8697 not queue PER_CU, just tell our caller to load its DIEs. */
8698 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8699 {
8700 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8701 return 1;
8702 return 0;
8703 }
8704
8705 /* Mark the dependence relation so that we don't flush PER_CU
8706 too early. */
8707 if (dependent_cu != NULL)
8708 dwarf2_add_dependence (dependent_cu, per_cu);
8709
8710 /* If it's already on the queue, we have nothing to do. */
8711 if (per_cu->queued)
8712 return 0;
8713
8714 /* If the compilation unit is already loaded, just mark it as
8715 used. */
8716 if (per_cu->cu != NULL)
8717 {
8718 per_cu->cu->last_used = 0;
8719 return 0;
8720 }
8721
8722 /* Add it to the queue. */
8723 queue_comp_unit (per_cu, pretend_language);
8724
8725 return 1;
8726 }
8727
8728 /* Process the queue. */
8729
8730 static void
8731 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8732 {
8733 if (dwarf_read_debug)
8734 {
8735 fprintf_unfiltered (gdb_stdlog,
8736 "Expanding one or more symtabs of objfile %s ...\n",
8737 objfile_name (dwarf2_per_objfile->objfile));
8738 }
8739
8740 /* The queue starts out with one item, but following a DIE reference
8741 may load a new CU, adding it to the end of the queue. */
8742 while (!dwarf2_per_objfile->queue.empty ())
8743 {
8744 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8745
8746 if ((dwarf2_per_objfile->using_index
8747 ? !item.per_cu->v.quick->compunit_symtab
8748 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8749 /* Skip dummy CUs. */
8750 && item.per_cu->cu != NULL)
8751 {
8752 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8753 unsigned int debug_print_threshold;
8754 char buf[100];
8755
8756 if (per_cu->is_debug_types)
8757 {
8758 struct signatured_type *sig_type =
8759 (struct signatured_type *) per_cu;
8760
8761 sprintf (buf, "TU %s at offset %s",
8762 hex_string (sig_type->signature),
8763 sect_offset_str (per_cu->sect_off));
8764 /* There can be 100s of TUs.
8765 Only print them in verbose mode. */
8766 debug_print_threshold = 2;
8767 }
8768 else
8769 {
8770 sprintf (buf, "CU at offset %s",
8771 sect_offset_str (per_cu->sect_off));
8772 debug_print_threshold = 1;
8773 }
8774
8775 if (dwarf_read_debug >= debug_print_threshold)
8776 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8777
8778 if (per_cu->is_debug_types)
8779 process_full_type_unit (per_cu, item.pretend_language);
8780 else
8781 process_full_comp_unit (per_cu, item.pretend_language);
8782
8783 if (dwarf_read_debug >= debug_print_threshold)
8784 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8785 }
8786
8787 item.per_cu->queued = 0;
8788 dwarf2_per_objfile->queue.pop ();
8789 }
8790
8791 if (dwarf_read_debug)
8792 {
8793 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8794 objfile_name (dwarf2_per_objfile->objfile));
8795 }
8796 }
8797
8798 /* Read in full symbols for PST, and anything it depends on. */
8799
8800 void
8801 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8802 {
8803 if (readin)
8804 return;
8805
8806 expand_dependencies (objfile);
8807
8808 dw2_do_instantiate_symtab (per_cu_data, false);
8809 gdb_assert (get_compunit_symtab () != nullptr);
8810 }
8811
8812 /* Trivial hash function for die_info: the hash value of a DIE
8813 is its offset in .debug_info for this objfile. */
8814
8815 static hashval_t
8816 die_hash (const void *item)
8817 {
8818 const struct die_info *die = (const struct die_info *) item;
8819
8820 return to_underlying (die->sect_off);
8821 }
8822
8823 /* Trivial comparison function for die_info structures: two DIEs
8824 are equal if they have the same offset. */
8825
8826 static int
8827 die_eq (const void *item_lhs, const void *item_rhs)
8828 {
8829 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8830 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8831
8832 return die_lhs->sect_off == die_rhs->sect_off;
8833 }
8834
8835 /* Load the DIEs associated with PER_CU into memory. */
8836
8837 static void
8838 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8839 bool skip_partial,
8840 enum language pretend_language)
8841 {
8842 gdb_assert (! this_cu->is_debug_types);
8843
8844 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8845 if (reader.dummy_p)
8846 return;
8847
8848 struct dwarf2_cu *cu = reader.cu;
8849 const gdb_byte *info_ptr = reader.info_ptr;
8850
8851 gdb_assert (cu->die_hash == NULL);
8852 cu->die_hash =
8853 htab_create_alloc_ex (cu->header.length / 12,
8854 die_hash,
8855 die_eq,
8856 NULL,
8857 &cu->comp_unit_obstack,
8858 hashtab_obstack_allocate,
8859 dummy_obstack_deallocate);
8860
8861 if (reader.comp_unit_die->has_children)
8862 reader.comp_unit_die->child
8863 = read_die_and_siblings (&reader, reader.info_ptr,
8864 &info_ptr, reader.comp_unit_die);
8865 cu->dies = reader.comp_unit_die;
8866 /* comp_unit_die is not stored in die_hash, no need. */
8867
8868 /* We try not to read any attributes in this function, because not
8869 all CUs needed for references have been loaded yet, and symbol
8870 table processing isn't initialized. But we have to set the CU language,
8871 or we won't be able to build types correctly.
8872 Similarly, if we do not read the producer, we can not apply
8873 producer-specific interpretation. */
8874 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8875
8876 reader.keep ();
8877 }
8878
8879 /* Add a DIE to the delayed physname list. */
8880
8881 static void
8882 add_to_method_list (struct type *type, int fnfield_index, int index,
8883 const char *name, struct die_info *die,
8884 struct dwarf2_cu *cu)
8885 {
8886 struct delayed_method_info mi;
8887 mi.type = type;
8888 mi.fnfield_index = fnfield_index;
8889 mi.index = index;
8890 mi.name = name;
8891 mi.die = die;
8892 cu->method_list.push_back (mi);
8893 }
8894
8895 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8896 "const" / "volatile". If so, decrements LEN by the length of the
8897 modifier and return true. Otherwise return false. */
8898
8899 template<size_t N>
8900 static bool
8901 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8902 {
8903 size_t mod_len = sizeof (mod) - 1;
8904 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8905 {
8906 len -= mod_len;
8907 return true;
8908 }
8909 return false;
8910 }
8911
8912 /* Compute the physnames of any methods on the CU's method list.
8913
8914 The computation of method physnames is delayed in order to avoid the
8915 (bad) condition that one of the method's formal parameters is of an as yet
8916 incomplete type. */
8917
8918 static void
8919 compute_delayed_physnames (struct dwarf2_cu *cu)
8920 {
8921 /* Only C++ delays computing physnames. */
8922 if (cu->method_list.empty ())
8923 return;
8924 gdb_assert (cu->language == language_cplus);
8925
8926 for (const delayed_method_info &mi : cu->method_list)
8927 {
8928 const char *physname;
8929 struct fn_fieldlist *fn_flp
8930 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8931 physname = dwarf2_physname (mi.name, mi.die, cu);
8932 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8933 = physname ? physname : "";
8934
8935 /* Since there's no tag to indicate whether a method is a
8936 const/volatile overload, extract that information out of the
8937 demangled name. */
8938 if (physname != NULL)
8939 {
8940 size_t len = strlen (physname);
8941
8942 while (1)
8943 {
8944 if (physname[len] == ')') /* shortcut */
8945 break;
8946 else if (check_modifier (physname, len, " const"))
8947 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8948 else if (check_modifier (physname, len, " volatile"))
8949 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8950 else
8951 break;
8952 }
8953 }
8954 }
8955
8956 /* The list is no longer needed. */
8957 cu->method_list.clear ();
8958 }
8959
8960 /* Go objects should be embedded in a DW_TAG_module DIE,
8961 and it's not clear if/how imported objects will appear.
8962 To keep Go support simple until that's worked out,
8963 go back through what we've read and create something usable.
8964 We could do this while processing each DIE, and feels kinda cleaner,
8965 but that way is more invasive.
8966 This is to, for example, allow the user to type "p var" or "b main"
8967 without having to specify the package name, and allow lookups
8968 of module.object to work in contexts that use the expression
8969 parser. */
8970
8971 static void
8972 fixup_go_packaging (struct dwarf2_cu *cu)
8973 {
8974 gdb::unique_xmalloc_ptr<char> package_name;
8975 struct pending *list;
8976 int i;
8977
8978 for (list = *cu->get_builder ()->get_global_symbols ();
8979 list != NULL;
8980 list = list->next)
8981 {
8982 for (i = 0; i < list->nsyms; ++i)
8983 {
8984 struct symbol *sym = list->symbol[i];
8985
8986 if (sym->language () == language_go
8987 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8988 {
8989 gdb::unique_xmalloc_ptr<char> this_package_name
8990 (go_symbol_package_name (sym));
8991
8992 if (this_package_name == NULL)
8993 continue;
8994 if (package_name == NULL)
8995 package_name = std::move (this_package_name);
8996 else
8997 {
8998 struct objfile *objfile
8999 = cu->per_cu->dwarf2_per_objfile->objfile;
9000 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9001 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9002 (symbol_symtab (sym) != NULL
9003 ? symtab_to_filename_for_display
9004 (symbol_symtab (sym))
9005 : objfile_name (objfile)),
9006 this_package_name.get (), package_name.get ());
9007 }
9008 }
9009 }
9010 }
9011
9012 if (package_name != NULL)
9013 {
9014 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9015 const char *saved_package_name = objfile->intern (package_name.get ());
9016 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9017 saved_package_name);
9018 struct symbol *sym;
9019
9020 sym = allocate_symbol (objfile);
9021 sym->set_language (language_go, &objfile->objfile_obstack);
9022 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9023 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9024 e.g., "main" finds the "main" module and not C's main(). */
9025 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9026 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9027 SYMBOL_TYPE (sym) = type;
9028
9029 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9030 }
9031 }
9032
9033 /* Allocate a fully-qualified name consisting of the two parts on the
9034 obstack. */
9035
9036 static const char *
9037 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9038 {
9039 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9040 }
9041
9042 /* A helper that allocates a struct discriminant_info to attach to a
9043 union type. */
9044
9045 static struct discriminant_info *
9046 alloc_discriminant_info (struct type *type, int discriminant_index,
9047 int default_index)
9048 {
9049 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9050 gdb_assert (discriminant_index == -1
9051 || (discriminant_index >= 0
9052 && discriminant_index < TYPE_NFIELDS (type)));
9053 gdb_assert (default_index == -1
9054 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9055
9056 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9057
9058 struct discriminant_info *disc
9059 = ((struct discriminant_info *)
9060 TYPE_ZALLOC (type,
9061 offsetof (struct discriminant_info, discriminants)
9062 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9063 disc->default_index = default_index;
9064 disc->discriminant_index = discriminant_index;
9065
9066 struct dynamic_prop prop;
9067 prop.kind = PROP_UNDEFINED;
9068 prop.data.baton = disc;
9069
9070 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9071
9072 return disc;
9073 }
9074
9075 /* Some versions of rustc emitted enums in an unusual way.
9076
9077 Ordinary enums were emitted as unions. The first element of each
9078 structure in the union was named "RUST$ENUM$DISR". This element
9079 held the discriminant.
9080
9081 These versions of Rust also implemented the "non-zero"
9082 optimization. When the enum had two values, and one is empty and
9083 the other holds a pointer that cannot be zero, the pointer is used
9084 as the discriminant, with a zero value meaning the empty variant.
9085 Here, the union's first member is of the form
9086 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9087 where the fieldnos are the indices of the fields that should be
9088 traversed in order to find the field (which may be several fields deep)
9089 and the variantname is the name of the variant of the case when the
9090 field is zero.
9091
9092 This function recognizes whether TYPE is of one of these forms,
9093 and, if so, smashes it to be a variant type. */
9094
9095 static void
9096 quirk_rust_enum (struct type *type, struct objfile *objfile)
9097 {
9098 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9099
9100 /* We don't need to deal with empty enums. */
9101 if (TYPE_NFIELDS (type) == 0)
9102 return;
9103
9104 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9105 if (TYPE_NFIELDS (type) == 1
9106 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9107 {
9108 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9109
9110 /* Decode the field name to find the offset of the
9111 discriminant. */
9112 ULONGEST bit_offset = 0;
9113 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9114 while (name[0] >= '0' && name[0] <= '9')
9115 {
9116 char *tail;
9117 unsigned long index = strtoul (name, &tail, 10);
9118 name = tail;
9119 if (*name != '$'
9120 || index >= TYPE_NFIELDS (field_type)
9121 || (TYPE_FIELD_LOC_KIND (field_type, index)
9122 != FIELD_LOC_KIND_BITPOS))
9123 {
9124 complaint (_("Could not parse Rust enum encoding string \"%s\""
9125 "[in module %s]"),
9126 TYPE_FIELD_NAME (type, 0),
9127 objfile_name (objfile));
9128 return;
9129 }
9130 ++name;
9131
9132 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9133 field_type = TYPE_FIELD_TYPE (field_type, index);
9134 }
9135
9136 /* Make a union to hold the variants. */
9137 struct type *union_type = alloc_type (objfile);
9138 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9139 TYPE_NFIELDS (union_type) = 3;
9140 TYPE_FIELDS (union_type)
9141 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9142 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9143 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9144
9145 /* Put the discriminant must at index 0. */
9146 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9147 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9148 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9149 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9150
9151 /* The order of fields doesn't really matter, so put the real
9152 field at index 1 and the data-less field at index 2. */
9153 struct discriminant_info *disc
9154 = alloc_discriminant_info (union_type, 0, 1);
9155 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9156 TYPE_FIELD_NAME (union_type, 1)
9157 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9158 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9159 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9160 TYPE_FIELD_NAME (union_type, 1));
9161
9162 const char *dataless_name
9163 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9164 name);
9165 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9166 dataless_name);
9167 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9168 /* NAME points into the original discriminant name, which
9169 already has the correct lifetime. */
9170 TYPE_FIELD_NAME (union_type, 2) = name;
9171 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9172 disc->discriminants[2] = 0;
9173
9174 /* Smash this type to be a structure type. We have to do this
9175 because the type has already been recorded. */
9176 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9177 TYPE_NFIELDS (type) = 1;
9178 TYPE_FIELDS (type)
9179 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9180
9181 /* Install the variant part. */
9182 TYPE_FIELD_TYPE (type, 0) = union_type;
9183 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9184 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9185 }
9186 /* A union with a single anonymous field is probably an old-style
9187 univariant enum. */
9188 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9189 {
9190 /* Smash this type to be a structure type. We have to do this
9191 because the type has already been recorded. */
9192 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9193
9194 /* Make a union to hold the variants. */
9195 struct type *union_type = alloc_type (objfile);
9196 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9197 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9198 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9199 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9200 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9201
9202 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9203 const char *variant_name
9204 = rust_last_path_segment (TYPE_NAME (field_type));
9205 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9206 TYPE_NAME (field_type)
9207 = rust_fully_qualify (&objfile->objfile_obstack,
9208 TYPE_NAME (type), variant_name);
9209
9210 /* Install the union in the outer struct type. */
9211 TYPE_NFIELDS (type) = 1;
9212 TYPE_FIELDS (type)
9213 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9214 TYPE_FIELD_TYPE (type, 0) = union_type;
9215 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9216 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9217
9218 alloc_discriminant_info (union_type, -1, 0);
9219 }
9220 else
9221 {
9222 struct type *disr_type = nullptr;
9223 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9224 {
9225 disr_type = TYPE_FIELD_TYPE (type, i);
9226
9227 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9228 {
9229 /* All fields of a true enum will be structs. */
9230 return;
9231 }
9232 else if (TYPE_NFIELDS (disr_type) == 0)
9233 {
9234 /* Could be data-less variant, so keep going. */
9235 disr_type = nullptr;
9236 }
9237 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9238 "RUST$ENUM$DISR") != 0)
9239 {
9240 /* Not a Rust enum. */
9241 return;
9242 }
9243 else
9244 {
9245 /* Found one. */
9246 break;
9247 }
9248 }
9249
9250 /* If we got here without a discriminant, then it's probably
9251 just a union. */
9252 if (disr_type == nullptr)
9253 return;
9254
9255 /* Smash this type to be a structure type. We have to do this
9256 because the type has already been recorded. */
9257 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9258
9259 /* Make a union to hold the variants. */
9260 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9261 struct type *union_type = alloc_type (objfile);
9262 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9263 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9264 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9265 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9266 TYPE_FIELDS (union_type)
9267 = (struct field *) TYPE_ZALLOC (union_type,
9268 (TYPE_NFIELDS (union_type)
9269 * sizeof (struct field)));
9270
9271 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9272 TYPE_NFIELDS (type) * sizeof (struct field));
9273
9274 /* Install the discriminant at index 0 in the union. */
9275 TYPE_FIELD (union_type, 0) = *disr_field;
9276 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9277 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9278
9279 /* Install the union in the outer struct type. */
9280 TYPE_FIELD_TYPE (type, 0) = union_type;
9281 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9282 TYPE_NFIELDS (type) = 1;
9283
9284 /* Set the size and offset of the union type. */
9285 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9286
9287 /* We need a way to find the correct discriminant given a
9288 variant name. For convenience we build a map here. */
9289 struct type *enum_type = FIELD_TYPE (*disr_field);
9290 std::unordered_map<std::string, ULONGEST> discriminant_map;
9291 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9292 {
9293 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9294 {
9295 const char *name
9296 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9297 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9298 }
9299 }
9300
9301 int n_fields = TYPE_NFIELDS (union_type);
9302 struct discriminant_info *disc
9303 = alloc_discriminant_info (union_type, 0, -1);
9304 /* Skip the discriminant here. */
9305 for (int i = 1; i < n_fields; ++i)
9306 {
9307 /* Find the final word in the name of this variant's type.
9308 That name can be used to look up the correct
9309 discriminant. */
9310 const char *variant_name
9311 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9312 i)));
9313
9314 auto iter = discriminant_map.find (variant_name);
9315 if (iter != discriminant_map.end ())
9316 disc->discriminants[i] = iter->second;
9317
9318 /* Remove the discriminant field, if it exists. */
9319 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9320 if (TYPE_NFIELDS (sub_type) > 0)
9321 {
9322 --TYPE_NFIELDS (sub_type);
9323 ++TYPE_FIELDS (sub_type);
9324 }
9325 TYPE_FIELD_NAME (union_type, i) = variant_name;
9326 TYPE_NAME (sub_type)
9327 = rust_fully_qualify (&objfile->objfile_obstack,
9328 TYPE_NAME (type), variant_name);
9329 }
9330 }
9331 }
9332
9333 /* Rewrite some Rust unions to be structures with variants parts. */
9334
9335 static void
9336 rust_union_quirks (struct dwarf2_cu *cu)
9337 {
9338 gdb_assert (cu->language == language_rust);
9339 for (type *type_ : cu->rust_unions)
9340 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9341 /* We don't need this any more. */
9342 cu->rust_unions.clear ();
9343 }
9344
9345 /* Return the symtab for PER_CU. This works properly regardless of
9346 whether we're using the index or psymtabs. */
9347
9348 static struct compunit_symtab *
9349 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9350 {
9351 return (per_cu->dwarf2_per_objfile->using_index
9352 ? per_cu->v.quick->compunit_symtab
9353 : per_cu->v.psymtab->compunit_symtab);
9354 }
9355
9356 /* A helper function for computing the list of all symbol tables
9357 included by PER_CU. */
9358
9359 static void
9360 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9361 htab_t all_children, htab_t all_type_symtabs,
9362 struct dwarf2_per_cu_data *per_cu,
9363 struct compunit_symtab *immediate_parent)
9364 {
9365 void **slot;
9366 struct compunit_symtab *cust;
9367
9368 slot = htab_find_slot (all_children, per_cu, INSERT);
9369 if (*slot != NULL)
9370 {
9371 /* This inclusion and its children have been processed. */
9372 return;
9373 }
9374
9375 *slot = per_cu;
9376 /* Only add a CU if it has a symbol table. */
9377 cust = get_compunit_symtab (per_cu);
9378 if (cust != NULL)
9379 {
9380 /* If this is a type unit only add its symbol table if we haven't
9381 seen it yet (type unit per_cu's can share symtabs). */
9382 if (per_cu->is_debug_types)
9383 {
9384 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9385 if (*slot == NULL)
9386 {
9387 *slot = cust;
9388 result->push_back (cust);
9389 if (cust->user == NULL)
9390 cust->user = immediate_parent;
9391 }
9392 }
9393 else
9394 {
9395 result->push_back (cust);
9396 if (cust->user == NULL)
9397 cust->user = immediate_parent;
9398 }
9399 }
9400
9401 if (!per_cu->imported_symtabs_empty ())
9402 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9403 {
9404 recursively_compute_inclusions (result, all_children,
9405 all_type_symtabs, ptr, cust);
9406 }
9407 }
9408
9409 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9410 PER_CU. */
9411
9412 static void
9413 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9414 {
9415 gdb_assert (! per_cu->is_debug_types);
9416
9417 if (!per_cu->imported_symtabs_empty ())
9418 {
9419 int len;
9420 std::vector<compunit_symtab *> result_symtabs;
9421 htab_t all_children, all_type_symtabs;
9422 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9423
9424 /* If we don't have a symtab, we can just skip this case. */
9425 if (cust == NULL)
9426 return;
9427
9428 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9429 NULL, xcalloc, xfree);
9430 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9431 NULL, xcalloc, xfree);
9432
9433 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9434 {
9435 recursively_compute_inclusions (&result_symtabs, all_children,
9436 all_type_symtabs, ptr, cust);
9437 }
9438
9439 /* Now we have a transitive closure of all the included symtabs. */
9440 len = result_symtabs.size ();
9441 cust->includes
9442 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9443 struct compunit_symtab *, len + 1);
9444 memcpy (cust->includes, result_symtabs.data (),
9445 len * sizeof (compunit_symtab *));
9446 cust->includes[len] = NULL;
9447
9448 htab_delete (all_children);
9449 htab_delete (all_type_symtabs);
9450 }
9451 }
9452
9453 /* Compute the 'includes' field for the symtabs of all the CUs we just
9454 read. */
9455
9456 static void
9457 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9458 {
9459 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9460 {
9461 if (! iter->is_debug_types)
9462 compute_compunit_symtab_includes (iter);
9463 }
9464
9465 dwarf2_per_objfile->just_read_cus.clear ();
9466 }
9467
9468 /* Generate full symbol information for PER_CU, whose DIEs have
9469 already been loaded into memory. */
9470
9471 static void
9472 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9473 enum language pretend_language)
9474 {
9475 struct dwarf2_cu *cu = per_cu->cu;
9476 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9477 struct objfile *objfile = dwarf2_per_objfile->objfile;
9478 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9479 CORE_ADDR lowpc, highpc;
9480 struct compunit_symtab *cust;
9481 CORE_ADDR baseaddr;
9482 struct block *static_block;
9483 CORE_ADDR addr;
9484
9485 baseaddr = objfile->text_section_offset ();
9486
9487 /* Clear the list here in case something was left over. */
9488 cu->method_list.clear ();
9489
9490 cu->language = pretend_language;
9491 cu->language_defn = language_def (cu->language);
9492
9493 /* Do line number decoding in read_file_scope () */
9494 process_die (cu->dies, cu);
9495
9496 /* For now fudge the Go package. */
9497 if (cu->language == language_go)
9498 fixup_go_packaging (cu);
9499
9500 /* Now that we have processed all the DIEs in the CU, all the types
9501 should be complete, and it should now be safe to compute all of the
9502 physnames. */
9503 compute_delayed_physnames (cu);
9504
9505 if (cu->language == language_rust)
9506 rust_union_quirks (cu);
9507
9508 /* Some compilers don't define a DW_AT_high_pc attribute for the
9509 compilation unit. If the DW_AT_high_pc is missing, synthesize
9510 it, by scanning the DIE's below the compilation unit. */
9511 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9512
9513 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9514 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9515
9516 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9517 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9518 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9519 addrmap to help ensure it has an accurate map of pc values belonging to
9520 this comp unit. */
9521 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9522
9523 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9524 SECT_OFF_TEXT (objfile),
9525 0);
9526
9527 if (cust != NULL)
9528 {
9529 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9530
9531 /* Set symtab language to language from DW_AT_language. If the
9532 compilation is from a C file generated by language preprocessors, do
9533 not set the language if it was already deduced by start_subfile. */
9534 if (!(cu->language == language_c
9535 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9536 COMPUNIT_FILETABS (cust)->language = cu->language;
9537
9538 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9539 produce DW_AT_location with location lists but it can be possibly
9540 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9541 there were bugs in prologue debug info, fixed later in GCC-4.5
9542 by "unwind info for epilogues" patch (which is not directly related).
9543
9544 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9545 needed, it would be wrong due to missing DW_AT_producer there.
9546
9547 Still one can confuse GDB by using non-standard GCC compilation
9548 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9549 */
9550 if (cu->has_loclist && gcc_4_minor >= 5)
9551 cust->locations_valid = 1;
9552
9553 if (gcc_4_minor >= 5)
9554 cust->epilogue_unwind_valid = 1;
9555
9556 cust->call_site_htab = cu->call_site_htab;
9557 }
9558
9559 if (dwarf2_per_objfile->using_index)
9560 per_cu->v.quick->compunit_symtab = cust;
9561 else
9562 {
9563 dwarf2_psymtab *pst = per_cu->v.psymtab;
9564 pst->compunit_symtab = cust;
9565 pst->readin = true;
9566 }
9567
9568 /* Push it for inclusion processing later. */
9569 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9570
9571 /* Not needed any more. */
9572 cu->reset_builder ();
9573 }
9574
9575 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9576 already been loaded into memory. */
9577
9578 static void
9579 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9580 enum language pretend_language)
9581 {
9582 struct dwarf2_cu *cu = per_cu->cu;
9583 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9584 struct objfile *objfile = dwarf2_per_objfile->objfile;
9585 struct compunit_symtab *cust;
9586 struct signatured_type *sig_type;
9587
9588 gdb_assert (per_cu->is_debug_types);
9589 sig_type = (struct signatured_type *) per_cu;
9590
9591 /* Clear the list here in case something was left over. */
9592 cu->method_list.clear ();
9593
9594 cu->language = pretend_language;
9595 cu->language_defn = language_def (cu->language);
9596
9597 /* The symbol tables are set up in read_type_unit_scope. */
9598 process_die (cu->dies, cu);
9599
9600 /* For now fudge the Go package. */
9601 if (cu->language == language_go)
9602 fixup_go_packaging (cu);
9603
9604 /* Now that we have processed all the DIEs in the CU, all the types
9605 should be complete, and it should now be safe to compute all of the
9606 physnames. */
9607 compute_delayed_physnames (cu);
9608
9609 if (cu->language == language_rust)
9610 rust_union_quirks (cu);
9611
9612 /* TUs share symbol tables.
9613 If this is the first TU to use this symtab, complete the construction
9614 of it with end_expandable_symtab. Otherwise, complete the addition of
9615 this TU's symbols to the existing symtab. */
9616 if (sig_type->type_unit_group->compunit_symtab == NULL)
9617 {
9618 buildsym_compunit *builder = cu->get_builder ();
9619 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9620 sig_type->type_unit_group->compunit_symtab = cust;
9621
9622 if (cust != NULL)
9623 {
9624 /* Set symtab language to language from DW_AT_language. If the
9625 compilation is from a C file generated by language preprocessors,
9626 do not set the language if it was already deduced by
9627 start_subfile. */
9628 if (!(cu->language == language_c
9629 && COMPUNIT_FILETABS (cust)->language != language_c))
9630 COMPUNIT_FILETABS (cust)->language = cu->language;
9631 }
9632 }
9633 else
9634 {
9635 cu->get_builder ()->augment_type_symtab ();
9636 cust = sig_type->type_unit_group->compunit_symtab;
9637 }
9638
9639 if (dwarf2_per_objfile->using_index)
9640 per_cu->v.quick->compunit_symtab = cust;
9641 else
9642 {
9643 dwarf2_psymtab *pst = per_cu->v.psymtab;
9644 pst->compunit_symtab = cust;
9645 pst->readin = true;
9646 }
9647
9648 /* Not needed any more. */
9649 cu->reset_builder ();
9650 }
9651
9652 /* Process an imported unit DIE. */
9653
9654 static void
9655 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9656 {
9657 struct attribute *attr;
9658
9659 /* For now we don't handle imported units in type units. */
9660 if (cu->per_cu->is_debug_types)
9661 {
9662 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9663 " supported in type units [in module %s]"),
9664 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9665 }
9666
9667 attr = dwarf2_attr (die, DW_AT_import, cu);
9668 if (attr != NULL)
9669 {
9670 sect_offset sect_off = attr->get_ref_die_offset ();
9671 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9672 dwarf2_per_cu_data *per_cu
9673 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9674 cu->per_cu->dwarf2_per_objfile);
9675
9676 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9677 into another compilation unit, at root level. Regard this as a hint,
9678 and ignore it. */
9679 if (die->parent && die->parent->parent == NULL
9680 && per_cu->unit_type == DW_UT_compile
9681 && per_cu->lang == language_cplus)
9682 return;
9683
9684 /* If necessary, add it to the queue and load its DIEs. */
9685 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9686 load_full_comp_unit (per_cu, false, cu->language);
9687
9688 cu->per_cu->imported_symtabs_push (per_cu);
9689 }
9690 }
9691
9692 /* RAII object that represents a process_die scope: i.e.,
9693 starts/finishes processing a DIE. */
9694 class process_die_scope
9695 {
9696 public:
9697 process_die_scope (die_info *die, dwarf2_cu *cu)
9698 : m_die (die), m_cu (cu)
9699 {
9700 /* We should only be processing DIEs not already in process. */
9701 gdb_assert (!m_die->in_process);
9702 m_die->in_process = true;
9703 }
9704
9705 ~process_die_scope ()
9706 {
9707 m_die->in_process = false;
9708
9709 /* If we're done processing the DIE for the CU that owns the line
9710 header, we don't need the line header anymore. */
9711 if (m_cu->line_header_die_owner == m_die)
9712 {
9713 delete m_cu->line_header;
9714 m_cu->line_header = NULL;
9715 m_cu->line_header_die_owner = NULL;
9716 }
9717 }
9718
9719 private:
9720 die_info *m_die;
9721 dwarf2_cu *m_cu;
9722 };
9723
9724 /* Process a die and its children. */
9725
9726 static void
9727 process_die (struct die_info *die, struct dwarf2_cu *cu)
9728 {
9729 process_die_scope scope (die, cu);
9730
9731 switch (die->tag)
9732 {
9733 case DW_TAG_padding:
9734 break;
9735 case DW_TAG_compile_unit:
9736 case DW_TAG_partial_unit:
9737 read_file_scope (die, cu);
9738 break;
9739 case DW_TAG_type_unit:
9740 read_type_unit_scope (die, cu);
9741 break;
9742 case DW_TAG_subprogram:
9743 /* Nested subprograms in Fortran get a prefix. */
9744 if (cu->language == language_fortran
9745 && die->parent != NULL
9746 && die->parent->tag == DW_TAG_subprogram)
9747 cu->processing_has_namespace_info = true;
9748 /* Fall through. */
9749 case DW_TAG_inlined_subroutine:
9750 read_func_scope (die, cu);
9751 break;
9752 case DW_TAG_lexical_block:
9753 case DW_TAG_try_block:
9754 case DW_TAG_catch_block:
9755 read_lexical_block_scope (die, cu);
9756 break;
9757 case DW_TAG_call_site:
9758 case DW_TAG_GNU_call_site:
9759 read_call_site_scope (die, cu);
9760 break;
9761 case DW_TAG_class_type:
9762 case DW_TAG_interface_type:
9763 case DW_TAG_structure_type:
9764 case DW_TAG_union_type:
9765 process_structure_scope (die, cu);
9766 break;
9767 case DW_TAG_enumeration_type:
9768 process_enumeration_scope (die, cu);
9769 break;
9770
9771 /* These dies have a type, but processing them does not create
9772 a symbol or recurse to process the children. Therefore we can
9773 read them on-demand through read_type_die. */
9774 case DW_TAG_subroutine_type:
9775 case DW_TAG_set_type:
9776 case DW_TAG_array_type:
9777 case DW_TAG_pointer_type:
9778 case DW_TAG_ptr_to_member_type:
9779 case DW_TAG_reference_type:
9780 case DW_TAG_rvalue_reference_type:
9781 case DW_TAG_string_type:
9782 break;
9783
9784 case DW_TAG_base_type:
9785 case DW_TAG_subrange_type:
9786 case DW_TAG_typedef:
9787 /* Add a typedef symbol for the type definition, if it has a
9788 DW_AT_name. */
9789 new_symbol (die, read_type_die (die, cu), cu);
9790 break;
9791 case DW_TAG_common_block:
9792 read_common_block (die, cu);
9793 break;
9794 case DW_TAG_common_inclusion:
9795 break;
9796 case DW_TAG_namespace:
9797 cu->processing_has_namespace_info = true;
9798 read_namespace (die, cu);
9799 break;
9800 case DW_TAG_module:
9801 cu->processing_has_namespace_info = true;
9802 read_module (die, cu);
9803 break;
9804 case DW_TAG_imported_declaration:
9805 cu->processing_has_namespace_info = true;
9806 if (read_namespace_alias (die, cu))
9807 break;
9808 /* The declaration is not a global namespace alias. */
9809 /* Fall through. */
9810 case DW_TAG_imported_module:
9811 cu->processing_has_namespace_info = true;
9812 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9813 || cu->language != language_fortran))
9814 complaint (_("Tag '%s' has unexpected children"),
9815 dwarf_tag_name (die->tag));
9816 read_import_statement (die, cu);
9817 break;
9818
9819 case DW_TAG_imported_unit:
9820 process_imported_unit_die (die, cu);
9821 break;
9822
9823 case DW_TAG_variable:
9824 read_variable (die, cu);
9825 break;
9826
9827 default:
9828 new_symbol (die, NULL, cu);
9829 break;
9830 }
9831 }
9832 \f
9833 /* DWARF name computation. */
9834
9835 /* A helper function for dwarf2_compute_name which determines whether DIE
9836 needs to have the name of the scope prepended to the name listed in the
9837 die. */
9838
9839 static int
9840 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9841 {
9842 struct attribute *attr;
9843
9844 switch (die->tag)
9845 {
9846 case DW_TAG_namespace:
9847 case DW_TAG_typedef:
9848 case DW_TAG_class_type:
9849 case DW_TAG_interface_type:
9850 case DW_TAG_structure_type:
9851 case DW_TAG_union_type:
9852 case DW_TAG_enumeration_type:
9853 case DW_TAG_enumerator:
9854 case DW_TAG_subprogram:
9855 case DW_TAG_inlined_subroutine:
9856 case DW_TAG_member:
9857 case DW_TAG_imported_declaration:
9858 return 1;
9859
9860 case DW_TAG_variable:
9861 case DW_TAG_constant:
9862 /* We only need to prefix "globally" visible variables. These include
9863 any variable marked with DW_AT_external or any variable that
9864 lives in a namespace. [Variables in anonymous namespaces
9865 require prefixing, but they are not DW_AT_external.] */
9866
9867 if (dwarf2_attr (die, DW_AT_specification, cu))
9868 {
9869 struct dwarf2_cu *spec_cu = cu;
9870
9871 return die_needs_namespace (die_specification (die, &spec_cu),
9872 spec_cu);
9873 }
9874
9875 attr = dwarf2_attr (die, DW_AT_external, cu);
9876 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9877 && die->parent->tag != DW_TAG_module)
9878 return 0;
9879 /* A variable in a lexical block of some kind does not need a
9880 namespace, even though in C++ such variables may be external
9881 and have a mangled name. */
9882 if (die->parent->tag == DW_TAG_lexical_block
9883 || die->parent->tag == DW_TAG_try_block
9884 || die->parent->tag == DW_TAG_catch_block
9885 || die->parent->tag == DW_TAG_subprogram)
9886 return 0;
9887 return 1;
9888
9889 default:
9890 return 0;
9891 }
9892 }
9893
9894 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9895 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9896 defined for the given DIE. */
9897
9898 static struct attribute *
9899 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9900 {
9901 struct attribute *attr;
9902
9903 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9904 if (attr == NULL)
9905 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9906
9907 return attr;
9908 }
9909
9910 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9911 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9912 defined for the given DIE. */
9913
9914 static const char *
9915 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9916 {
9917 const char *linkage_name;
9918
9919 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9920 if (linkage_name == NULL)
9921 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9922
9923 return linkage_name;
9924 }
9925
9926 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9927 compute the physname for the object, which include a method's:
9928 - formal parameters (C++),
9929 - receiver type (Go),
9930
9931 The term "physname" is a bit confusing.
9932 For C++, for example, it is the demangled name.
9933 For Go, for example, it's the mangled name.
9934
9935 For Ada, return the DIE's linkage name rather than the fully qualified
9936 name. PHYSNAME is ignored..
9937
9938 The result is allocated on the objfile_obstack and canonicalized. */
9939
9940 static const char *
9941 dwarf2_compute_name (const char *name,
9942 struct die_info *die, struct dwarf2_cu *cu,
9943 int physname)
9944 {
9945 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9946
9947 if (name == NULL)
9948 name = dwarf2_name (die, cu);
9949
9950 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9951 but otherwise compute it by typename_concat inside GDB.
9952 FIXME: Actually this is not really true, or at least not always true.
9953 It's all very confusing. compute_and_set_names doesn't try to demangle
9954 Fortran names because there is no mangling standard. So new_symbol
9955 will set the demangled name to the result of dwarf2_full_name, and it is
9956 the demangled name that GDB uses if it exists. */
9957 if (cu->language == language_ada
9958 || (cu->language == language_fortran && physname))
9959 {
9960 /* For Ada unit, we prefer the linkage name over the name, as
9961 the former contains the exported name, which the user expects
9962 to be able to reference. Ideally, we want the user to be able
9963 to reference this entity using either natural or linkage name,
9964 but we haven't started looking at this enhancement yet. */
9965 const char *linkage_name = dw2_linkage_name (die, cu);
9966
9967 if (linkage_name != NULL)
9968 return linkage_name;
9969 }
9970
9971 /* These are the only languages we know how to qualify names in. */
9972 if (name != NULL
9973 && (cu->language == language_cplus
9974 || cu->language == language_fortran || cu->language == language_d
9975 || cu->language == language_rust))
9976 {
9977 if (die_needs_namespace (die, cu))
9978 {
9979 const char *prefix;
9980 const char *canonical_name = NULL;
9981
9982 string_file buf;
9983
9984 prefix = determine_prefix (die, cu);
9985 if (*prefix != '\0')
9986 {
9987 gdb::unique_xmalloc_ptr<char> prefixed_name
9988 (typename_concat (NULL, prefix, name, physname, cu));
9989
9990 buf.puts (prefixed_name.get ());
9991 }
9992 else
9993 buf.puts (name);
9994
9995 /* Template parameters may be specified in the DIE's DW_AT_name, or
9996 as children with DW_TAG_template_type_param or
9997 DW_TAG_value_type_param. If the latter, add them to the name
9998 here. If the name already has template parameters, then
9999 skip this step; some versions of GCC emit both, and
10000 it is more efficient to use the pre-computed name.
10001
10002 Something to keep in mind about this process: it is very
10003 unlikely, or in some cases downright impossible, to produce
10004 something that will match the mangled name of a function.
10005 If the definition of the function has the same debug info,
10006 we should be able to match up with it anyway. But fallbacks
10007 using the minimal symbol, for instance to find a method
10008 implemented in a stripped copy of libstdc++, will not work.
10009 If we do not have debug info for the definition, we will have to
10010 match them up some other way.
10011
10012 When we do name matching there is a related problem with function
10013 templates; two instantiated function templates are allowed to
10014 differ only by their return types, which we do not add here. */
10015
10016 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10017 {
10018 struct attribute *attr;
10019 struct die_info *child;
10020 int first = 1;
10021
10022 die->building_fullname = 1;
10023
10024 for (child = die->child; child != NULL; child = child->sibling)
10025 {
10026 struct type *type;
10027 LONGEST value;
10028 const gdb_byte *bytes;
10029 struct dwarf2_locexpr_baton *baton;
10030 struct value *v;
10031
10032 if (child->tag != DW_TAG_template_type_param
10033 && child->tag != DW_TAG_template_value_param)
10034 continue;
10035
10036 if (first)
10037 {
10038 buf.puts ("<");
10039 first = 0;
10040 }
10041 else
10042 buf.puts (", ");
10043
10044 attr = dwarf2_attr (child, DW_AT_type, cu);
10045 if (attr == NULL)
10046 {
10047 complaint (_("template parameter missing DW_AT_type"));
10048 buf.puts ("UNKNOWN_TYPE");
10049 continue;
10050 }
10051 type = die_type (child, cu);
10052
10053 if (child->tag == DW_TAG_template_type_param)
10054 {
10055 c_print_type (type, "", &buf, -1, 0, cu->language,
10056 &type_print_raw_options);
10057 continue;
10058 }
10059
10060 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10061 if (attr == NULL)
10062 {
10063 complaint (_("template parameter missing "
10064 "DW_AT_const_value"));
10065 buf.puts ("UNKNOWN_VALUE");
10066 continue;
10067 }
10068
10069 dwarf2_const_value_attr (attr, type, name,
10070 &cu->comp_unit_obstack, cu,
10071 &value, &bytes, &baton);
10072
10073 if (TYPE_NOSIGN (type))
10074 /* GDB prints characters as NUMBER 'CHAR'. If that's
10075 changed, this can use value_print instead. */
10076 c_printchar (value, type, &buf);
10077 else
10078 {
10079 struct value_print_options opts;
10080
10081 if (baton != NULL)
10082 v = dwarf2_evaluate_loc_desc (type, NULL,
10083 baton->data,
10084 baton->size,
10085 baton->per_cu);
10086 else if (bytes != NULL)
10087 {
10088 v = allocate_value (type);
10089 memcpy (value_contents_writeable (v), bytes,
10090 TYPE_LENGTH (type));
10091 }
10092 else
10093 v = value_from_longest (type, value);
10094
10095 /* Specify decimal so that we do not depend on
10096 the radix. */
10097 get_formatted_print_options (&opts, 'd');
10098 opts.raw = 1;
10099 value_print (v, &buf, &opts);
10100 release_value (v);
10101 }
10102 }
10103
10104 die->building_fullname = 0;
10105
10106 if (!first)
10107 {
10108 /* Close the argument list, with a space if necessary
10109 (nested templates). */
10110 if (!buf.empty () && buf.string ().back () == '>')
10111 buf.puts (" >");
10112 else
10113 buf.puts (">");
10114 }
10115 }
10116
10117 /* For C++ methods, append formal parameter type
10118 information, if PHYSNAME. */
10119
10120 if (physname && die->tag == DW_TAG_subprogram
10121 && cu->language == language_cplus)
10122 {
10123 struct type *type = read_type_die (die, cu);
10124
10125 c_type_print_args (type, &buf, 1, cu->language,
10126 &type_print_raw_options);
10127
10128 if (cu->language == language_cplus)
10129 {
10130 /* Assume that an artificial first parameter is
10131 "this", but do not crash if it is not. RealView
10132 marks unnamed (and thus unused) parameters as
10133 artificial; there is no way to differentiate
10134 the two cases. */
10135 if (TYPE_NFIELDS (type) > 0
10136 && TYPE_FIELD_ARTIFICIAL (type, 0)
10137 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10138 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10139 0))))
10140 buf.puts (" const");
10141 }
10142 }
10143
10144 const std::string &intermediate_name = buf.string ();
10145
10146 if (cu->language == language_cplus)
10147 canonical_name
10148 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10149 objfile);
10150
10151 /* If we only computed INTERMEDIATE_NAME, or if
10152 INTERMEDIATE_NAME is already canonical, then we need to
10153 intern it. */
10154 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10155 name = objfile->intern (intermediate_name);
10156 else
10157 name = canonical_name;
10158 }
10159 }
10160
10161 return name;
10162 }
10163
10164 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10165 If scope qualifiers are appropriate they will be added. The result
10166 will be allocated on the storage_obstack, or NULL if the DIE does
10167 not have a name. NAME may either be from a previous call to
10168 dwarf2_name or NULL.
10169
10170 The output string will be canonicalized (if C++). */
10171
10172 static const char *
10173 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10174 {
10175 return dwarf2_compute_name (name, die, cu, 0);
10176 }
10177
10178 /* Construct a physname for the given DIE in CU. NAME may either be
10179 from a previous call to dwarf2_name or NULL. The result will be
10180 allocated on the objfile_objstack or NULL if the DIE does not have a
10181 name.
10182
10183 The output string will be canonicalized (if C++). */
10184
10185 static const char *
10186 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10187 {
10188 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10189 const char *retval, *mangled = NULL, *canon = NULL;
10190 int need_copy = 1;
10191
10192 /* In this case dwarf2_compute_name is just a shortcut not building anything
10193 on its own. */
10194 if (!die_needs_namespace (die, cu))
10195 return dwarf2_compute_name (name, die, cu, 1);
10196
10197 mangled = dw2_linkage_name (die, cu);
10198
10199 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10200 See https://github.com/rust-lang/rust/issues/32925. */
10201 if (cu->language == language_rust && mangled != NULL
10202 && strchr (mangled, '{') != NULL)
10203 mangled = NULL;
10204
10205 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10206 has computed. */
10207 gdb::unique_xmalloc_ptr<char> demangled;
10208 if (mangled != NULL)
10209 {
10210
10211 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10212 {
10213 /* Do nothing (do not demangle the symbol name). */
10214 }
10215 else if (cu->language == language_go)
10216 {
10217 /* This is a lie, but we already lie to the caller new_symbol.
10218 new_symbol assumes we return the mangled name.
10219 This just undoes that lie until things are cleaned up. */
10220 }
10221 else
10222 {
10223 /* Use DMGL_RET_DROP for C++ template functions to suppress
10224 their return type. It is easier for GDB users to search
10225 for such functions as `name(params)' than `long name(params)'.
10226 In such case the minimal symbol names do not match the full
10227 symbol names but for template functions there is never a need
10228 to look up their definition from their declaration so
10229 the only disadvantage remains the minimal symbol variant
10230 `long name(params)' does not have the proper inferior type. */
10231 demangled.reset (gdb_demangle (mangled,
10232 (DMGL_PARAMS | DMGL_ANSI
10233 | DMGL_RET_DROP)));
10234 }
10235 if (demangled)
10236 canon = demangled.get ();
10237 else
10238 {
10239 canon = mangled;
10240 need_copy = 0;
10241 }
10242 }
10243
10244 if (canon == NULL || check_physname)
10245 {
10246 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10247
10248 if (canon != NULL && strcmp (physname, canon) != 0)
10249 {
10250 /* It may not mean a bug in GDB. The compiler could also
10251 compute DW_AT_linkage_name incorrectly. But in such case
10252 GDB would need to be bug-to-bug compatible. */
10253
10254 complaint (_("Computed physname <%s> does not match demangled <%s> "
10255 "(from linkage <%s>) - DIE at %s [in module %s]"),
10256 physname, canon, mangled, sect_offset_str (die->sect_off),
10257 objfile_name (objfile));
10258
10259 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10260 is available here - over computed PHYSNAME. It is safer
10261 against both buggy GDB and buggy compilers. */
10262
10263 retval = canon;
10264 }
10265 else
10266 {
10267 retval = physname;
10268 need_copy = 0;
10269 }
10270 }
10271 else
10272 retval = canon;
10273
10274 if (need_copy)
10275 retval = objfile->intern (retval);
10276
10277 return retval;
10278 }
10279
10280 /* Inspect DIE in CU for a namespace alias. If one exists, record
10281 a new symbol for it.
10282
10283 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10284
10285 static int
10286 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10287 {
10288 struct attribute *attr;
10289
10290 /* If the die does not have a name, this is not a namespace
10291 alias. */
10292 attr = dwarf2_attr (die, DW_AT_name, cu);
10293 if (attr != NULL)
10294 {
10295 int num;
10296 struct die_info *d = die;
10297 struct dwarf2_cu *imported_cu = cu;
10298
10299 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10300 keep inspecting DIEs until we hit the underlying import. */
10301 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10302 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10303 {
10304 attr = dwarf2_attr (d, DW_AT_import, cu);
10305 if (attr == NULL)
10306 break;
10307
10308 d = follow_die_ref (d, attr, &imported_cu);
10309 if (d->tag != DW_TAG_imported_declaration)
10310 break;
10311 }
10312
10313 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10314 {
10315 complaint (_("DIE at %s has too many recursively imported "
10316 "declarations"), sect_offset_str (d->sect_off));
10317 return 0;
10318 }
10319
10320 if (attr != NULL)
10321 {
10322 struct type *type;
10323 sect_offset sect_off = attr->get_ref_die_offset ();
10324
10325 type = get_die_type_at_offset (sect_off, cu->per_cu);
10326 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10327 {
10328 /* This declaration is a global namespace alias. Add
10329 a symbol for it whose type is the aliased namespace. */
10330 new_symbol (die, type, cu);
10331 return 1;
10332 }
10333 }
10334 }
10335
10336 return 0;
10337 }
10338
10339 /* Return the using directives repository (global or local?) to use in the
10340 current context for CU.
10341
10342 For Ada, imported declarations can materialize renamings, which *may* be
10343 global. However it is impossible (for now?) in DWARF to distinguish
10344 "external" imported declarations and "static" ones. As all imported
10345 declarations seem to be static in all other languages, make them all CU-wide
10346 global only in Ada. */
10347
10348 static struct using_direct **
10349 using_directives (struct dwarf2_cu *cu)
10350 {
10351 if (cu->language == language_ada
10352 && cu->get_builder ()->outermost_context_p ())
10353 return cu->get_builder ()->get_global_using_directives ();
10354 else
10355 return cu->get_builder ()->get_local_using_directives ();
10356 }
10357
10358 /* Read the import statement specified by the given die and record it. */
10359
10360 static void
10361 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10362 {
10363 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10364 struct attribute *import_attr;
10365 struct die_info *imported_die, *child_die;
10366 struct dwarf2_cu *imported_cu;
10367 const char *imported_name;
10368 const char *imported_name_prefix;
10369 const char *canonical_name;
10370 const char *import_alias;
10371 const char *imported_declaration = NULL;
10372 const char *import_prefix;
10373 std::vector<const char *> excludes;
10374
10375 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10376 if (import_attr == NULL)
10377 {
10378 complaint (_("Tag '%s' has no DW_AT_import"),
10379 dwarf_tag_name (die->tag));
10380 return;
10381 }
10382
10383 imported_cu = cu;
10384 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10385 imported_name = dwarf2_name (imported_die, imported_cu);
10386 if (imported_name == NULL)
10387 {
10388 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10389
10390 The import in the following code:
10391 namespace A
10392 {
10393 typedef int B;
10394 }
10395
10396 int main ()
10397 {
10398 using A::B;
10399 B b;
10400 return b;
10401 }
10402
10403 ...
10404 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10405 <52> DW_AT_decl_file : 1
10406 <53> DW_AT_decl_line : 6
10407 <54> DW_AT_import : <0x75>
10408 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10409 <59> DW_AT_name : B
10410 <5b> DW_AT_decl_file : 1
10411 <5c> DW_AT_decl_line : 2
10412 <5d> DW_AT_type : <0x6e>
10413 ...
10414 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10415 <76> DW_AT_byte_size : 4
10416 <77> DW_AT_encoding : 5 (signed)
10417
10418 imports the wrong die ( 0x75 instead of 0x58 ).
10419 This case will be ignored until the gcc bug is fixed. */
10420 return;
10421 }
10422
10423 /* Figure out the local name after import. */
10424 import_alias = dwarf2_name (die, cu);
10425
10426 /* Figure out where the statement is being imported to. */
10427 import_prefix = determine_prefix (die, cu);
10428
10429 /* Figure out what the scope of the imported die is and prepend it
10430 to the name of the imported die. */
10431 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10432
10433 if (imported_die->tag != DW_TAG_namespace
10434 && imported_die->tag != DW_TAG_module)
10435 {
10436 imported_declaration = imported_name;
10437 canonical_name = imported_name_prefix;
10438 }
10439 else if (strlen (imported_name_prefix) > 0)
10440 canonical_name = obconcat (&objfile->objfile_obstack,
10441 imported_name_prefix,
10442 (cu->language == language_d ? "." : "::"),
10443 imported_name, (char *) NULL);
10444 else
10445 canonical_name = imported_name;
10446
10447 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10448 for (child_die = die->child; child_die && child_die->tag;
10449 child_die = child_die->sibling)
10450 {
10451 /* DWARF-4: A Fortran use statement with a “rename list” may be
10452 represented by an imported module entry with an import attribute
10453 referring to the module and owned entries corresponding to those
10454 entities that are renamed as part of being imported. */
10455
10456 if (child_die->tag != DW_TAG_imported_declaration)
10457 {
10458 complaint (_("child DW_TAG_imported_declaration expected "
10459 "- DIE at %s [in module %s]"),
10460 sect_offset_str (child_die->sect_off),
10461 objfile_name (objfile));
10462 continue;
10463 }
10464
10465 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10466 if (import_attr == NULL)
10467 {
10468 complaint (_("Tag '%s' has no DW_AT_import"),
10469 dwarf_tag_name (child_die->tag));
10470 continue;
10471 }
10472
10473 imported_cu = cu;
10474 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10475 &imported_cu);
10476 imported_name = dwarf2_name (imported_die, imported_cu);
10477 if (imported_name == NULL)
10478 {
10479 complaint (_("child DW_TAG_imported_declaration has unknown "
10480 "imported name - DIE at %s [in module %s]"),
10481 sect_offset_str (child_die->sect_off),
10482 objfile_name (objfile));
10483 continue;
10484 }
10485
10486 excludes.push_back (imported_name);
10487
10488 process_die (child_die, cu);
10489 }
10490
10491 add_using_directive (using_directives (cu),
10492 import_prefix,
10493 canonical_name,
10494 import_alias,
10495 imported_declaration,
10496 excludes,
10497 0,
10498 &objfile->objfile_obstack);
10499 }
10500
10501 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10502 types, but gives them a size of zero. Starting with version 14,
10503 ICC is compatible with GCC. */
10504
10505 static bool
10506 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10507 {
10508 if (!cu->checked_producer)
10509 check_producer (cu);
10510
10511 return cu->producer_is_icc_lt_14;
10512 }
10513
10514 /* ICC generates a DW_AT_type for C void functions. This was observed on
10515 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10516 which says that void functions should not have a DW_AT_type. */
10517
10518 static bool
10519 producer_is_icc (struct dwarf2_cu *cu)
10520 {
10521 if (!cu->checked_producer)
10522 check_producer (cu);
10523
10524 return cu->producer_is_icc;
10525 }
10526
10527 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10528 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10529 this, it was first present in GCC release 4.3.0. */
10530
10531 static bool
10532 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10533 {
10534 if (!cu->checked_producer)
10535 check_producer (cu);
10536
10537 return cu->producer_is_gcc_lt_4_3;
10538 }
10539
10540 static file_and_directory
10541 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10542 {
10543 file_and_directory res;
10544
10545 /* Find the filename. Do not use dwarf2_name here, since the filename
10546 is not a source language identifier. */
10547 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10548 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10549
10550 if (res.comp_dir == NULL
10551 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10552 && IS_ABSOLUTE_PATH (res.name))
10553 {
10554 res.comp_dir_storage = ldirname (res.name);
10555 if (!res.comp_dir_storage.empty ())
10556 res.comp_dir = res.comp_dir_storage.c_str ();
10557 }
10558 if (res.comp_dir != NULL)
10559 {
10560 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10561 directory, get rid of it. */
10562 const char *cp = strchr (res.comp_dir, ':');
10563
10564 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10565 res.comp_dir = cp + 1;
10566 }
10567
10568 if (res.name == NULL)
10569 res.name = "<unknown>";
10570
10571 return res;
10572 }
10573
10574 /* Handle DW_AT_stmt_list for a compilation unit.
10575 DIE is the DW_TAG_compile_unit die for CU.
10576 COMP_DIR is the compilation directory. LOWPC is passed to
10577 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10578
10579 static void
10580 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10581 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10582 {
10583 struct dwarf2_per_objfile *dwarf2_per_objfile
10584 = cu->per_cu->dwarf2_per_objfile;
10585 struct attribute *attr;
10586 struct line_header line_header_local;
10587 hashval_t line_header_local_hash;
10588 void **slot;
10589 int decode_mapping;
10590
10591 gdb_assert (! cu->per_cu->is_debug_types);
10592
10593 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10594 if (attr == NULL)
10595 return;
10596
10597 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10598
10599 /* The line header hash table is only created if needed (it exists to
10600 prevent redundant reading of the line table for partial_units).
10601 If we're given a partial_unit, we'll need it. If we're given a
10602 compile_unit, then use the line header hash table if it's already
10603 created, but don't create one just yet. */
10604
10605 if (dwarf2_per_objfile->line_header_hash == NULL
10606 && die->tag == DW_TAG_partial_unit)
10607 {
10608 dwarf2_per_objfile->line_header_hash
10609 .reset (htab_create_alloc (127, line_header_hash_voidp,
10610 line_header_eq_voidp,
10611 free_line_header_voidp,
10612 xcalloc, xfree));
10613 }
10614
10615 line_header_local.sect_off = line_offset;
10616 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10617 line_header_local_hash = line_header_hash (&line_header_local);
10618 if (dwarf2_per_objfile->line_header_hash != NULL)
10619 {
10620 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10621 &line_header_local,
10622 line_header_local_hash, NO_INSERT);
10623
10624 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10625 is not present in *SLOT (since if there is something in *SLOT then
10626 it will be for a partial_unit). */
10627 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10628 {
10629 gdb_assert (*slot != NULL);
10630 cu->line_header = (struct line_header *) *slot;
10631 return;
10632 }
10633 }
10634
10635 /* dwarf_decode_line_header does not yet provide sufficient information.
10636 We always have to call also dwarf_decode_lines for it. */
10637 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10638 if (lh == NULL)
10639 return;
10640
10641 cu->line_header = lh.release ();
10642 cu->line_header_die_owner = die;
10643
10644 if (dwarf2_per_objfile->line_header_hash == NULL)
10645 slot = NULL;
10646 else
10647 {
10648 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10649 &line_header_local,
10650 line_header_local_hash, INSERT);
10651 gdb_assert (slot != NULL);
10652 }
10653 if (slot != NULL && *slot == NULL)
10654 {
10655 /* This newly decoded line number information unit will be owned
10656 by line_header_hash hash table. */
10657 *slot = cu->line_header;
10658 cu->line_header_die_owner = NULL;
10659 }
10660 else
10661 {
10662 /* We cannot free any current entry in (*slot) as that struct line_header
10663 may be already used by multiple CUs. Create only temporary decoded
10664 line_header for this CU - it may happen at most once for each line
10665 number information unit. And if we're not using line_header_hash
10666 then this is what we want as well. */
10667 gdb_assert (die->tag != DW_TAG_partial_unit);
10668 }
10669 decode_mapping = (die->tag != DW_TAG_partial_unit);
10670 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10671 decode_mapping);
10672
10673 }
10674
10675 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10676
10677 static void
10678 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10679 {
10680 struct dwarf2_per_objfile *dwarf2_per_objfile
10681 = cu->per_cu->dwarf2_per_objfile;
10682 struct objfile *objfile = dwarf2_per_objfile->objfile;
10683 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10684 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10685 CORE_ADDR highpc = ((CORE_ADDR) 0);
10686 struct attribute *attr;
10687 struct die_info *child_die;
10688 CORE_ADDR baseaddr;
10689
10690 prepare_one_comp_unit (cu, die, cu->language);
10691 baseaddr = objfile->text_section_offset ();
10692
10693 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10694
10695 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10696 from finish_block. */
10697 if (lowpc == ((CORE_ADDR) -1))
10698 lowpc = highpc;
10699 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10700
10701 file_and_directory fnd = find_file_and_directory (die, cu);
10702
10703 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10704 standardised yet. As a workaround for the language detection we fall
10705 back to the DW_AT_producer string. */
10706 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10707 cu->language = language_opencl;
10708
10709 /* Similar hack for Go. */
10710 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10711 set_cu_language (DW_LANG_Go, cu);
10712
10713 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10714
10715 /* Decode line number information if present. We do this before
10716 processing child DIEs, so that the line header table is available
10717 for DW_AT_decl_file. */
10718 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10719
10720 /* Process all dies in compilation unit. */
10721 if (die->child != NULL)
10722 {
10723 child_die = die->child;
10724 while (child_die && child_die->tag)
10725 {
10726 process_die (child_die, cu);
10727 child_die = child_die->sibling;
10728 }
10729 }
10730
10731 /* Decode macro information, if present. Dwarf 2 macro information
10732 refers to information in the line number info statement program
10733 header, so we can only read it if we've read the header
10734 successfully. */
10735 attr = dwarf2_attr (die, DW_AT_macros, cu);
10736 if (attr == NULL)
10737 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10738 if (attr && cu->line_header)
10739 {
10740 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10741 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10742
10743 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10744 }
10745 else
10746 {
10747 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10748 if (attr && cu->line_header)
10749 {
10750 unsigned int macro_offset = DW_UNSND (attr);
10751
10752 dwarf_decode_macros (cu, macro_offset, 0);
10753 }
10754 }
10755 }
10756
10757 void
10758 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10759 {
10760 struct type_unit_group *tu_group;
10761 int first_time;
10762 struct attribute *attr;
10763 unsigned int i;
10764 struct signatured_type *sig_type;
10765
10766 gdb_assert (per_cu->is_debug_types);
10767 sig_type = (struct signatured_type *) per_cu;
10768
10769 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10770
10771 /* If we're using .gdb_index (includes -readnow) then
10772 per_cu->type_unit_group may not have been set up yet. */
10773 if (sig_type->type_unit_group == NULL)
10774 sig_type->type_unit_group = get_type_unit_group (this, attr);
10775 tu_group = sig_type->type_unit_group;
10776
10777 /* If we've already processed this stmt_list there's no real need to
10778 do it again, we could fake it and just recreate the part we need
10779 (file name,index -> symtab mapping). If data shows this optimization
10780 is useful we can do it then. */
10781 first_time = tu_group->compunit_symtab == NULL;
10782
10783 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10784 debug info. */
10785 line_header_up lh;
10786 if (attr != NULL)
10787 {
10788 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10789 lh = dwarf_decode_line_header (line_offset, this);
10790 }
10791 if (lh == NULL)
10792 {
10793 if (first_time)
10794 start_symtab ("", NULL, 0);
10795 else
10796 {
10797 gdb_assert (tu_group->symtabs == NULL);
10798 gdb_assert (m_builder == nullptr);
10799 struct compunit_symtab *cust = tu_group->compunit_symtab;
10800 m_builder.reset (new struct buildsym_compunit
10801 (COMPUNIT_OBJFILE (cust), "",
10802 COMPUNIT_DIRNAME (cust),
10803 compunit_language (cust),
10804 0, cust));
10805 }
10806 return;
10807 }
10808
10809 line_header = lh.release ();
10810 line_header_die_owner = die;
10811
10812 if (first_time)
10813 {
10814 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10815
10816 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10817 still initializing it, and our caller (a few levels up)
10818 process_full_type_unit still needs to know if this is the first
10819 time. */
10820
10821 tu_group->symtabs
10822 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10823 struct symtab *, line_header->file_names_size ());
10824
10825 auto &file_names = line_header->file_names ();
10826 for (i = 0; i < file_names.size (); ++i)
10827 {
10828 file_entry &fe = file_names[i];
10829 dwarf2_start_subfile (this, fe.name,
10830 fe.include_dir (line_header));
10831 buildsym_compunit *b = get_builder ();
10832 if (b->get_current_subfile ()->symtab == NULL)
10833 {
10834 /* NOTE: start_subfile will recognize when it's been
10835 passed a file it has already seen. So we can't
10836 assume there's a simple mapping from
10837 cu->line_header->file_names to subfiles, plus
10838 cu->line_header->file_names may contain dups. */
10839 b->get_current_subfile ()->symtab
10840 = allocate_symtab (cust, b->get_current_subfile ()->name);
10841 }
10842
10843 fe.symtab = b->get_current_subfile ()->symtab;
10844 tu_group->symtabs[i] = fe.symtab;
10845 }
10846 }
10847 else
10848 {
10849 gdb_assert (m_builder == nullptr);
10850 struct compunit_symtab *cust = tu_group->compunit_symtab;
10851 m_builder.reset (new struct buildsym_compunit
10852 (COMPUNIT_OBJFILE (cust), "",
10853 COMPUNIT_DIRNAME (cust),
10854 compunit_language (cust),
10855 0, cust));
10856
10857 auto &file_names = line_header->file_names ();
10858 for (i = 0; i < file_names.size (); ++i)
10859 {
10860 file_entry &fe = file_names[i];
10861 fe.symtab = tu_group->symtabs[i];
10862 }
10863 }
10864
10865 /* The main symtab is allocated last. Type units don't have DW_AT_name
10866 so they don't have a "real" (so to speak) symtab anyway.
10867 There is later code that will assign the main symtab to all symbols
10868 that don't have one. We need to handle the case of a symbol with a
10869 missing symtab (DW_AT_decl_file) anyway. */
10870 }
10871
10872 /* Process DW_TAG_type_unit.
10873 For TUs we want to skip the first top level sibling if it's not the
10874 actual type being defined by this TU. In this case the first top
10875 level sibling is there to provide context only. */
10876
10877 static void
10878 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10879 {
10880 struct die_info *child_die;
10881
10882 prepare_one_comp_unit (cu, die, language_minimal);
10883
10884 /* Initialize (or reinitialize) the machinery for building symtabs.
10885 We do this before processing child DIEs, so that the line header table
10886 is available for DW_AT_decl_file. */
10887 cu->setup_type_unit_groups (die);
10888
10889 if (die->child != NULL)
10890 {
10891 child_die = die->child;
10892 while (child_die && child_die->tag)
10893 {
10894 process_die (child_die, cu);
10895 child_die = child_die->sibling;
10896 }
10897 }
10898 }
10899 \f
10900 /* DWO/DWP files.
10901
10902 http://gcc.gnu.org/wiki/DebugFission
10903 http://gcc.gnu.org/wiki/DebugFissionDWP
10904
10905 To simplify handling of both DWO files ("object" files with the DWARF info)
10906 and DWP files (a file with the DWOs packaged up into one file), we treat
10907 DWP files as having a collection of virtual DWO files. */
10908
10909 static hashval_t
10910 hash_dwo_file (const void *item)
10911 {
10912 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10913 hashval_t hash;
10914
10915 hash = htab_hash_string (dwo_file->dwo_name);
10916 if (dwo_file->comp_dir != NULL)
10917 hash += htab_hash_string (dwo_file->comp_dir);
10918 return hash;
10919 }
10920
10921 static int
10922 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10923 {
10924 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10925 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10926
10927 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10928 return 0;
10929 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10930 return lhs->comp_dir == rhs->comp_dir;
10931 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10932 }
10933
10934 /* Allocate a hash table for DWO files. */
10935
10936 static htab_up
10937 allocate_dwo_file_hash_table ()
10938 {
10939 auto delete_dwo_file = [] (void *item)
10940 {
10941 struct dwo_file *dwo_file = (struct dwo_file *) item;
10942
10943 delete dwo_file;
10944 };
10945
10946 return htab_up (htab_create_alloc (41,
10947 hash_dwo_file,
10948 eq_dwo_file,
10949 delete_dwo_file,
10950 xcalloc, xfree));
10951 }
10952
10953 /* Lookup DWO file DWO_NAME. */
10954
10955 static void **
10956 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10957 const char *dwo_name,
10958 const char *comp_dir)
10959 {
10960 struct dwo_file find_entry;
10961 void **slot;
10962
10963 if (dwarf2_per_objfile->dwo_files == NULL)
10964 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10965
10966 find_entry.dwo_name = dwo_name;
10967 find_entry.comp_dir = comp_dir;
10968 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
10969 INSERT);
10970
10971 return slot;
10972 }
10973
10974 static hashval_t
10975 hash_dwo_unit (const void *item)
10976 {
10977 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10978
10979 /* This drops the top 32 bits of the id, but is ok for a hash. */
10980 return dwo_unit->signature;
10981 }
10982
10983 static int
10984 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10985 {
10986 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10987 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10988
10989 /* The signature is assumed to be unique within the DWO file.
10990 So while object file CU dwo_id's always have the value zero,
10991 that's OK, assuming each object file DWO file has only one CU,
10992 and that's the rule for now. */
10993 return lhs->signature == rhs->signature;
10994 }
10995
10996 /* Allocate a hash table for DWO CUs,TUs.
10997 There is one of these tables for each of CUs,TUs for each DWO file. */
10998
10999 static htab_up
11000 allocate_dwo_unit_table ()
11001 {
11002 /* Start out with a pretty small number.
11003 Generally DWO files contain only one CU and maybe some TUs. */
11004 return htab_up (htab_create_alloc (3,
11005 hash_dwo_unit,
11006 eq_dwo_unit,
11007 NULL, xcalloc, xfree));
11008 }
11009
11010 /* die_reader_func for create_dwo_cu. */
11011
11012 static void
11013 create_dwo_cu_reader (const struct die_reader_specs *reader,
11014 const gdb_byte *info_ptr,
11015 struct die_info *comp_unit_die,
11016 struct dwo_file *dwo_file,
11017 struct dwo_unit *dwo_unit)
11018 {
11019 struct dwarf2_cu *cu = reader->cu;
11020 sect_offset sect_off = cu->per_cu->sect_off;
11021 struct dwarf2_section_info *section = cu->per_cu->section;
11022
11023 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11024 if (!signature.has_value ())
11025 {
11026 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11027 " its dwo_id [in module %s]"),
11028 sect_offset_str (sect_off), dwo_file->dwo_name);
11029 return;
11030 }
11031
11032 dwo_unit->dwo_file = dwo_file;
11033 dwo_unit->signature = *signature;
11034 dwo_unit->section = section;
11035 dwo_unit->sect_off = sect_off;
11036 dwo_unit->length = cu->per_cu->length;
11037
11038 if (dwarf_read_debug)
11039 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11040 sect_offset_str (sect_off),
11041 hex_string (dwo_unit->signature));
11042 }
11043
11044 /* Create the dwo_units for the CUs in a DWO_FILE.
11045 Note: This function processes DWO files only, not DWP files. */
11046
11047 static void
11048 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11049 dwarf2_cu *cu, struct dwo_file &dwo_file,
11050 dwarf2_section_info &section, htab_up &cus_htab)
11051 {
11052 struct objfile *objfile = dwarf2_per_objfile->objfile;
11053 const gdb_byte *info_ptr, *end_ptr;
11054
11055 section.read (objfile);
11056 info_ptr = section.buffer;
11057
11058 if (info_ptr == NULL)
11059 return;
11060
11061 if (dwarf_read_debug)
11062 {
11063 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11064 section.get_name (),
11065 section.get_file_name ());
11066 }
11067
11068 end_ptr = info_ptr + section.size;
11069 while (info_ptr < end_ptr)
11070 {
11071 struct dwarf2_per_cu_data per_cu;
11072 struct dwo_unit read_unit {};
11073 struct dwo_unit *dwo_unit;
11074 void **slot;
11075 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11076
11077 memset (&per_cu, 0, sizeof (per_cu));
11078 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11079 per_cu.is_debug_types = 0;
11080 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11081 per_cu.section = &section;
11082
11083 cutu_reader reader (&per_cu, cu, &dwo_file);
11084 if (!reader.dummy_p)
11085 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11086 &dwo_file, &read_unit);
11087 info_ptr += per_cu.length;
11088
11089 // If the unit could not be parsed, skip it.
11090 if (read_unit.dwo_file == NULL)
11091 continue;
11092
11093 if (cus_htab == NULL)
11094 cus_htab = allocate_dwo_unit_table ();
11095
11096 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11097 *dwo_unit = read_unit;
11098 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11099 gdb_assert (slot != NULL);
11100 if (*slot != NULL)
11101 {
11102 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11103 sect_offset dup_sect_off = dup_cu->sect_off;
11104
11105 complaint (_("debug cu entry at offset %s is duplicate to"
11106 " the entry at offset %s, signature %s"),
11107 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11108 hex_string (dwo_unit->signature));
11109 }
11110 *slot = (void *)dwo_unit;
11111 }
11112 }
11113
11114 /* DWP file .debug_{cu,tu}_index section format:
11115 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11116
11117 DWP Version 1:
11118
11119 Both index sections have the same format, and serve to map a 64-bit
11120 signature to a set of section numbers. Each section begins with a header,
11121 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11122 indexes, and a pool of 32-bit section numbers. The index sections will be
11123 aligned at 8-byte boundaries in the file.
11124
11125 The index section header consists of:
11126
11127 V, 32 bit version number
11128 -, 32 bits unused
11129 N, 32 bit number of compilation units or type units in the index
11130 M, 32 bit number of slots in the hash table
11131
11132 Numbers are recorded using the byte order of the application binary.
11133
11134 The hash table begins at offset 16 in the section, and consists of an array
11135 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11136 order of the application binary). Unused slots in the hash table are 0.
11137 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11138
11139 The parallel table begins immediately after the hash table
11140 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11141 array of 32-bit indexes (using the byte order of the application binary),
11142 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11143 table contains a 32-bit index into the pool of section numbers. For unused
11144 hash table slots, the corresponding entry in the parallel table will be 0.
11145
11146 The pool of section numbers begins immediately following the hash table
11147 (at offset 16 + 12 * M from the beginning of the section). The pool of
11148 section numbers consists of an array of 32-bit words (using the byte order
11149 of the application binary). Each item in the array is indexed starting
11150 from 0. The hash table entry provides the index of the first section
11151 number in the set. Additional section numbers in the set follow, and the
11152 set is terminated by a 0 entry (section number 0 is not used in ELF).
11153
11154 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11155 section must be the first entry in the set, and the .debug_abbrev.dwo must
11156 be the second entry. Other members of the set may follow in any order.
11157
11158 ---
11159
11160 DWP Version 2:
11161
11162 DWP Version 2 combines all the .debug_info, etc. sections into one,
11163 and the entries in the index tables are now offsets into these sections.
11164 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11165 section.
11166
11167 Index Section Contents:
11168 Header
11169 Hash Table of Signatures dwp_hash_table.hash_table
11170 Parallel Table of Indices dwp_hash_table.unit_table
11171 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11172 Table of Section Sizes dwp_hash_table.v2.sizes
11173
11174 The index section header consists of:
11175
11176 V, 32 bit version number
11177 L, 32 bit number of columns in the table of section offsets
11178 N, 32 bit number of compilation units or type units in the index
11179 M, 32 bit number of slots in the hash table
11180
11181 Numbers are recorded using the byte order of the application binary.
11182
11183 The hash table has the same format as version 1.
11184 The parallel table of indices has the same format as version 1,
11185 except that the entries are origin-1 indices into the table of sections
11186 offsets and the table of section sizes.
11187
11188 The table of offsets begins immediately following the parallel table
11189 (at offset 16 + 12 * M from the beginning of the section). The table is
11190 a two-dimensional array of 32-bit words (using the byte order of the
11191 application binary), with L columns and N+1 rows, in row-major order.
11192 Each row in the array is indexed starting from 0. The first row provides
11193 a key to the remaining rows: each column in this row provides an identifier
11194 for a debug section, and the offsets in the same column of subsequent rows
11195 refer to that section. The section identifiers are:
11196
11197 DW_SECT_INFO 1 .debug_info.dwo
11198 DW_SECT_TYPES 2 .debug_types.dwo
11199 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11200 DW_SECT_LINE 4 .debug_line.dwo
11201 DW_SECT_LOC 5 .debug_loc.dwo
11202 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11203 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11204 DW_SECT_MACRO 8 .debug_macro.dwo
11205
11206 The offsets provided by the CU and TU index sections are the base offsets
11207 for the contributions made by each CU or TU to the corresponding section
11208 in the package file. Each CU and TU header contains an abbrev_offset
11209 field, used to find the abbreviations table for that CU or TU within the
11210 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11211 be interpreted as relative to the base offset given in the index section.
11212 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11213 should be interpreted as relative to the base offset for .debug_line.dwo,
11214 and offsets into other debug sections obtained from DWARF attributes should
11215 also be interpreted as relative to the corresponding base offset.
11216
11217 The table of sizes begins immediately following the table of offsets.
11218 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11219 with L columns and N rows, in row-major order. Each row in the array is
11220 indexed starting from 1 (row 0 is shared by the two tables).
11221
11222 ---
11223
11224 Hash table lookup is handled the same in version 1 and 2:
11225
11226 We assume that N and M will not exceed 2^32 - 1.
11227 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11228
11229 Given a 64-bit compilation unit signature or a type signature S, an entry
11230 in the hash table is located as follows:
11231
11232 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11233 the low-order k bits all set to 1.
11234
11235 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11236
11237 3) If the hash table entry at index H matches the signature, use that
11238 entry. If the hash table entry at index H is unused (all zeroes),
11239 terminate the search: the signature is not present in the table.
11240
11241 4) Let H = (H + H') modulo M. Repeat at Step 3.
11242
11243 Because M > N and H' and M are relatively prime, the search is guaranteed
11244 to stop at an unused slot or find the match. */
11245
11246 /* Create a hash table to map DWO IDs to their CU/TU entry in
11247 .debug_{info,types}.dwo in DWP_FILE.
11248 Returns NULL if there isn't one.
11249 Note: This function processes DWP files only, not DWO files. */
11250
11251 static struct dwp_hash_table *
11252 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11253 struct dwp_file *dwp_file, int is_debug_types)
11254 {
11255 struct objfile *objfile = dwarf2_per_objfile->objfile;
11256 bfd *dbfd = dwp_file->dbfd.get ();
11257 const gdb_byte *index_ptr, *index_end;
11258 struct dwarf2_section_info *index;
11259 uint32_t version, nr_columns, nr_units, nr_slots;
11260 struct dwp_hash_table *htab;
11261
11262 if (is_debug_types)
11263 index = &dwp_file->sections.tu_index;
11264 else
11265 index = &dwp_file->sections.cu_index;
11266
11267 if (index->empty ())
11268 return NULL;
11269 index->read (objfile);
11270
11271 index_ptr = index->buffer;
11272 index_end = index_ptr + index->size;
11273
11274 version = read_4_bytes (dbfd, index_ptr);
11275 index_ptr += 4;
11276 if (version == 2)
11277 nr_columns = read_4_bytes (dbfd, index_ptr);
11278 else
11279 nr_columns = 0;
11280 index_ptr += 4;
11281 nr_units = read_4_bytes (dbfd, index_ptr);
11282 index_ptr += 4;
11283 nr_slots = read_4_bytes (dbfd, index_ptr);
11284 index_ptr += 4;
11285
11286 if (version != 1 && version != 2)
11287 {
11288 error (_("Dwarf Error: unsupported DWP file version (%s)"
11289 " [in module %s]"),
11290 pulongest (version), dwp_file->name);
11291 }
11292 if (nr_slots != (nr_slots & -nr_slots))
11293 {
11294 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11295 " is not power of 2 [in module %s]"),
11296 pulongest (nr_slots), dwp_file->name);
11297 }
11298
11299 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11300 htab->version = version;
11301 htab->nr_columns = nr_columns;
11302 htab->nr_units = nr_units;
11303 htab->nr_slots = nr_slots;
11304 htab->hash_table = index_ptr;
11305 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11306
11307 /* Exit early if the table is empty. */
11308 if (nr_slots == 0 || nr_units == 0
11309 || (version == 2 && nr_columns == 0))
11310 {
11311 /* All must be zero. */
11312 if (nr_slots != 0 || nr_units != 0
11313 || (version == 2 && nr_columns != 0))
11314 {
11315 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11316 " all zero [in modules %s]"),
11317 dwp_file->name);
11318 }
11319 return htab;
11320 }
11321
11322 if (version == 1)
11323 {
11324 htab->section_pool.v1.indices =
11325 htab->unit_table + sizeof (uint32_t) * nr_slots;
11326 /* It's harder to decide whether the section is too small in v1.
11327 V1 is deprecated anyway so we punt. */
11328 }
11329 else
11330 {
11331 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11332 int *ids = htab->section_pool.v2.section_ids;
11333 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11334 /* Reverse map for error checking. */
11335 int ids_seen[DW_SECT_MAX + 1];
11336 int i;
11337
11338 if (nr_columns < 2)
11339 {
11340 error (_("Dwarf Error: bad DWP hash table, too few columns"
11341 " in section table [in module %s]"),
11342 dwp_file->name);
11343 }
11344 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11345 {
11346 error (_("Dwarf Error: bad DWP hash table, too many columns"
11347 " in section table [in module %s]"),
11348 dwp_file->name);
11349 }
11350 memset (ids, 255, sizeof_ids);
11351 memset (ids_seen, 255, sizeof (ids_seen));
11352 for (i = 0; i < nr_columns; ++i)
11353 {
11354 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11355
11356 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11357 {
11358 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11359 " in section table [in module %s]"),
11360 id, dwp_file->name);
11361 }
11362 if (ids_seen[id] != -1)
11363 {
11364 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11365 " id %d in section table [in module %s]"),
11366 id, dwp_file->name);
11367 }
11368 ids_seen[id] = i;
11369 ids[i] = id;
11370 }
11371 /* Must have exactly one info or types section. */
11372 if (((ids_seen[DW_SECT_INFO] != -1)
11373 + (ids_seen[DW_SECT_TYPES] != -1))
11374 != 1)
11375 {
11376 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11377 " DWO info/types section [in module %s]"),
11378 dwp_file->name);
11379 }
11380 /* Must have an abbrev section. */
11381 if (ids_seen[DW_SECT_ABBREV] == -1)
11382 {
11383 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11384 " section [in module %s]"),
11385 dwp_file->name);
11386 }
11387 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11388 htab->section_pool.v2.sizes =
11389 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11390 * nr_units * nr_columns);
11391 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11392 * nr_units * nr_columns))
11393 > index_end)
11394 {
11395 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11396 " [in module %s]"),
11397 dwp_file->name);
11398 }
11399 }
11400
11401 return htab;
11402 }
11403
11404 /* Update SECTIONS with the data from SECTP.
11405
11406 This function is like the other "locate" section routines that are
11407 passed to bfd_map_over_sections, but in this context the sections to
11408 read comes from the DWP V1 hash table, not the full ELF section table.
11409
11410 The result is non-zero for success, or zero if an error was found. */
11411
11412 static int
11413 locate_v1_virtual_dwo_sections (asection *sectp,
11414 struct virtual_v1_dwo_sections *sections)
11415 {
11416 const struct dwop_section_names *names = &dwop_section_names;
11417
11418 if (section_is_p (sectp->name, &names->abbrev_dwo))
11419 {
11420 /* There can be only one. */
11421 if (sections->abbrev.s.section != NULL)
11422 return 0;
11423 sections->abbrev.s.section = sectp;
11424 sections->abbrev.size = bfd_section_size (sectp);
11425 }
11426 else if (section_is_p (sectp->name, &names->info_dwo)
11427 || section_is_p (sectp->name, &names->types_dwo))
11428 {
11429 /* There can be only one. */
11430 if (sections->info_or_types.s.section != NULL)
11431 return 0;
11432 sections->info_or_types.s.section = sectp;
11433 sections->info_or_types.size = bfd_section_size (sectp);
11434 }
11435 else if (section_is_p (sectp->name, &names->line_dwo))
11436 {
11437 /* There can be only one. */
11438 if (sections->line.s.section != NULL)
11439 return 0;
11440 sections->line.s.section = sectp;
11441 sections->line.size = bfd_section_size (sectp);
11442 }
11443 else if (section_is_p (sectp->name, &names->loc_dwo))
11444 {
11445 /* There can be only one. */
11446 if (sections->loc.s.section != NULL)
11447 return 0;
11448 sections->loc.s.section = sectp;
11449 sections->loc.size = bfd_section_size (sectp);
11450 }
11451 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11452 {
11453 /* There can be only one. */
11454 if (sections->macinfo.s.section != NULL)
11455 return 0;
11456 sections->macinfo.s.section = sectp;
11457 sections->macinfo.size = bfd_section_size (sectp);
11458 }
11459 else if (section_is_p (sectp->name, &names->macro_dwo))
11460 {
11461 /* There can be only one. */
11462 if (sections->macro.s.section != NULL)
11463 return 0;
11464 sections->macro.s.section = sectp;
11465 sections->macro.size = bfd_section_size (sectp);
11466 }
11467 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11468 {
11469 /* There can be only one. */
11470 if (sections->str_offsets.s.section != NULL)
11471 return 0;
11472 sections->str_offsets.s.section = sectp;
11473 sections->str_offsets.size = bfd_section_size (sectp);
11474 }
11475 else
11476 {
11477 /* No other kind of section is valid. */
11478 return 0;
11479 }
11480
11481 return 1;
11482 }
11483
11484 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11485 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11486 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11487 This is for DWP version 1 files. */
11488
11489 static struct dwo_unit *
11490 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11491 struct dwp_file *dwp_file,
11492 uint32_t unit_index,
11493 const char *comp_dir,
11494 ULONGEST signature, int is_debug_types)
11495 {
11496 struct objfile *objfile = dwarf2_per_objfile->objfile;
11497 const struct dwp_hash_table *dwp_htab =
11498 is_debug_types ? dwp_file->tus : dwp_file->cus;
11499 bfd *dbfd = dwp_file->dbfd.get ();
11500 const char *kind = is_debug_types ? "TU" : "CU";
11501 struct dwo_file *dwo_file;
11502 struct dwo_unit *dwo_unit;
11503 struct virtual_v1_dwo_sections sections;
11504 void **dwo_file_slot;
11505 int i;
11506
11507 gdb_assert (dwp_file->version == 1);
11508
11509 if (dwarf_read_debug)
11510 {
11511 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11512 kind,
11513 pulongest (unit_index), hex_string (signature),
11514 dwp_file->name);
11515 }
11516
11517 /* Fetch the sections of this DWO unit.
11518 Put a limit on the number of sections we look for so that bad data
11519 doesn't cause us to loop forever. */
11520
11521 #define MAX_NR_V1_DWO_SECTIONS \
11522 (1 /* .debug_info or .debug_types */ \
11523 + 1 /* .debug_abbrev */ \
11524 + 1 /* .debug_line */ \
11525 + 1 /* .debug_loc */ \
11526 + 1 /* .debug_str_offsets */ \
11527 + 1 /* .debug_macro or .debug_macinfo */ \
11528 + 1 /* trailing zero */)
11529
11530 memset (&sections, 0, sizeof (sections));
11531
11532 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11533 {
11534 asection *sectp;
11535 uint32_t section_nr =
11536 read_4_bytes (dbfd,
11537 dwp_htab->section_pool.v1.indices
11538 + (unit_index + i) * sizeof (uint32_t));
11539
11540 if (section_nr == 0)
11541 break;
11542 if (section_nr >= dwp_file->num_sections)
11543 {
11544 error (_("Dwarf Error: bad DWP hash table, section number too large"
11545 " [in module %s]"),
11546 dwp_file->name);
11547 }
11548
11549 sectp = dwp_file->elf_sections[section_nr];
11550 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11551 {
11552 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11553 " [in module %s]"),
11554 dwp_file->name);
11555 }
11556 }
11557
11558 if (i < 2
11559 || sections.info_or_types.empty ()
11560 || sections.abbrev.empty ())
11561 {
11562 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11563 " [in module %s]"),
11564 dwp_file->name);
11565 }
11566 if (i == MAX_NR_V1_DWO_SECTIONS)
11567 {
11568 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11569 " [in module %s]"),
11570 dwp_file->name);
11571 }
11572
11573 /* It's easier for the rest of the code if we fake a struct dwo_file and
11574 have dwo_unit "live" in that. At least for now.
11575
11576 The DWP file can be made up of a random collection of CUs and TUs.
11577 However, for each CU + set of TUs that came from the same original DWO
11578 file, we can combine them back into a virtual DWO file to save space
11579 (fewer struct dwo_file objects to allocate). Remember that for really
11580 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11581
11582 std::string virtual_dwo_name =
11583 string_printf ("virtual-dwo/%d-%d-%d-%d",
11584 sections.abbrev.get_id (),
11585 sections.line.get_id (),
11586 sections.loc.get_id (),
11587 sections.str_offsets.get_id ());
11588 /* Can we use an existing virtual DWO file? */
11589 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11590 virtual_dwo_name.c_str (),
11591 comp_dir);
11592 /* Create one if necessary. */
11593 if (*dwo_file_slot == NULL)
11594 {
11595 if (dwarf_read_debug)
11596 {
11597 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11598 virtual_dwo_name.c_str ());
11599 }
11600 dwo_file = new struct dwo_file;
11601 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11602 dwo_file->comp_dir = comp_dir;
11603 dwo_file->sections.abbrev = sections.abbrev;
11604 dwo_file->sections.line = sections.line;
11605 dwo_file->sections.loc = sections.loc;
11606 dwo_file->sections.macinfo = sections.macinfo;
11607 dwo_file->sections.macro = sections.macro;
11608 dwo_file->sections.str_offsets = sections.str_offsets;
11609 /* The "str" section is global to the entire DWP file. */
11610 dwo_file->sections.str = dwp_file->sections.str;
11611 /* The info or types section is assigned below to dwo_unit,
11612 there's no need to record it in dwo_file.
11613 Also, we can't simply record type sections in dwo_file because
11614 we record a pointer into the vector in dwo_unit. As we collect more
11615 types we'll grow the vector and eventually have to reallocate space
11616 for it, invalidating all copies of pointers into the previous
11617 contents. */
11618 *dwo_file_slot = dwo_file;
11619 }
11620 else
11621 {
11622 if (dwarf_read_debug)
11623 {
11624 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11625 virtual_dwo_name.c_str ());
11626 }
11627 dwo_file = (struct dwo_file *) *dwo_file_slot;
11628 }
11629
11630 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11631 dwo_unit->dwo_file = dwo_file;
11632 dwo_unit->signature = signature;
11633 dwo_unit->section =
11634 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11635 *dwo_unit->section = sections.info_or_types;
11636 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11637
11638 return dwo_unit;
11639 }
11640
11641 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11642 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11643 piece within that section used by a TU/CU, return a virtual section
11644 of just that piece. */
11645
11646 static struct dwarf2_section_info
11647 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11648 struct dwarf2_section_info *section,
11649 bfd_size_type offset, bfd_size_type size)
11650 {
11651 struct dwarf2_section_info result;
11652 asection *sectp;
11653
11654 gdb_assert (section != NULL);
11655 gdb_assert (!section->is_virtual);
11656
11657 memset (&result, 0, sizeof (result));
11658 result.s.containing_section = section;
11659 result.is_virtual = true;
11660
11661 if (size == 0)
11662 return result;
11663
11664 sectp = section->get_bfd_section ();
11665
11666 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11667 bounds of the real section. This is a pretty-rare event, so just
11668 flag an error (easier) instead of a warning and trying to cope. */
11669 if (sectp == NULL
11670 || offset + size > bfd_section_size (sectp))
11671 {
11672 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11673 " in section %s [in module %s]"),
11674 sectp ? bfd_section_name (sectp) : "<unknown>",
11675 objfile_name (dwarf2_per_objfile->objfile));
11676 }
11677
11678 result.virtual_offset = offset;
11679 result.size = size;
11680 return result;
11681 }
11682
11683 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11684 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11685 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11686 This is for DWP version 2 files. */
11687
11688 static struct dwo_unit *
11689 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11690 struct dwp_file *dwp_file,
11691 uint32_t unit_index,
11692 const char *comp_dir,
11693 ULONGEST signature, int is_debug_types)
11694 {
11695 struct objfile *objfile = dwarf2_per_objfile->objfile;
11696 const struct dwp_hash_table *dwp_htab =
11697 is_debug_types ? dwp_file->tus : dwp_file->cus;
11698 bfd *dbfd = dwp_file->dbfd.get ();
11699 const char *kind = is_debug_types ? "TU" : "CU";
11700 struct dwo_file *dwo_file;
11701 struct dwo_unit *dwo_unit;
11702 struct virtual_v2_dwo_sections sections;
11703 void **dwo_file_slot;
11704 int i;
11705
11706 gdb_assert (dwp_file->version == 2);
11707
11708 if (dwarf_read_debug)
11709 {
11710 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11711 kind,
11712 pulongest (unit_index), hex_string (signature),
11713 dwp_file->name);
11714 }
11715
11716 /* Fetch the section offsets of this DWO unit. */
11717
11718 memset (&sections, 0, sizeof (sections));
11719
11720 for (i = 0; i < dwp_htab->nr_columns; ++i)
11721 {
11722 uint32_t offset = read_4_bytes (dbfd,
11723 dwp_htab->section_pool.v2.offsets
11724 + (((unit_index - 1) * dwp_htab->nr_columns
11725 + i)
11726 * sizeof (uint32_t)));
11727 uint32_t size = read_4_bytes (dbfd,
11728 dwp_htab->section_pool.v2.sizes
11729 + (((unit_index - 1) * dwp_htab->nr_columns
11730 + i)
11731 * sizeof (uint32_t)));
11732
11733 switch (dwp_htab->section_pool.v2.section_ids[i])
11734 {
11735 case DW_SECT_INFO:
11736 case DW_SECT_TYPES:
11737 sections.info_or_types_offset = offset;
11738 sections.info_or_types_size = size;
11739 break;
11740 case DW_SECT_ABBREV:
11741 sections.abbrev_offset = offset;
11742 sections.abbrev_size = size;
11743 break;
11744 case DW_SECT_LINE:
11745 sections.line_offset = offset;
11746 sections.line_size = size;
11747 break;
11748 case DW_SECT_LOC:
11749 sections.loc_offset = offset;
11750 sections.loc_size = size;
11751 break;
11752 case DW_SECT_STR_OFFSETS:
11753 sections.str_offsets_offset = offset;
11754 sections.str_offsets_size = size;
11755 break;
11756 case DW_SECT_MACINFO:
11757 sections.macinfo_offset = offset;
11758 sections.macinfo_size = size;
11759 break;
11760 case DW_SECT_MACRO:
11761 sections.macro_offset = offset;
11762 sections.macro_size = size;
11763 break;
11764 }
11765 }
11766
11767 /* It's easier for the rest of the code if we fake a struct dwo_file and
11768 have dwo_unit "live" in that. At least for now.
11769
11770 The DWP file can be made up of a random collection of CUs and TUs.
11771 However, for each CU + set of TUs that came from the same original DWO
11772 file, we can combine them back into a virtual DWO file to save space
11773 (fewer struct dwo_file objects to allocate). Remember that for really
11774 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11775
11776 std::string virtual_dwo_name =
11777 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11778 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11779 (long) (sections.line_size ? sections.line_offset : 0),
11780 (long) (sections.loc_size ? sections.loc_offset : 0),
11781 (long) (sections.str_offsets_size
11782 ? sections.str_offsets_offset : 0));
11783 /* Can we use an existing virtual DWO file? */
11784 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11785 virtual_dwo_name.c_str (),
11786 comp_dir);
11787 /* Create one if necessary. */
11788 if (*dwo_file_slot == NULL)
11789 {
11790 if (dwarf_read_debug)
11791 {
11792 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11793 virtual_dwo_name.c_str ());
11794 }
11795 dwo_file = new struct dwo_file;
11796 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11797 dwo_file->comp_dir = comp_dir;
11798 dwo_file->sections.abbrev =
11799 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11800 sections.abbrev_offset, sections.abbrev_size);
11801 dwo_file->sections.line =
11802 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11803 sections.line_offset, sections.line_size);
11804 dwo_file->sections.loc =
11805 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11806 sections.loc_offset, sections.loc_size);
11807 dwo_file->sections.macinfo =
11808 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11809 sections.macinfo_offset, sections.macinfo_size);
11810 dwo_file->sections.macro =
11811 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11812 sections.macro_offset, sections.macro_size);
11813 dwo_file->sections.str_offsets =
11814 create_dwp_v2_section (dwarf2_per_objfile,
11815 &dwp_file->sections.str_offsets,
11816 sections.str_offsets_offset,
11817 sections.str_offsets_size);
11818 /* The "str" section is global to the entire DWP file. */
11819 dwo_file->sections.str = dwp_file->sections.str;
11820 /* The info or types section is assigned below to dwo_unit,
11821 there's no need to record it in dwo_file.
11822 Also, we can't simply record type sections in dwo_file because
11823 we record a pointer into the vector in dwo_unit. As we collect more
11824 types we'll grow the vector and eventually have to reallocate space
11825 for it, invalidating all copies of pointers into the previous
11826 contents. */
11827 *dwo_file_slot = dwo_file;
11828 }
11829 else
11830 {
11831 if (dwarf_read_debug)
11832 {
11833 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11834 virtual_dwo_name.c_str ());
11835 }
11836 dwo_file = (struct dwo_file *) *dwo_file_slot;
11837 }
11838
11839 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11840 dwo_unit->dwo_file = dwo_file;
11841 dwo_unit->signature = signature;
11842 dwo_unit->section =
11843 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11844 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11845 is_debug_types
11846 ? &dwp_file->sections.types
11847 : &dwp_file->sections.info,
11848 sections.info_or_types_offset,
11849 sections.info_or_types_size);
11850 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11851
11852 return dwo_unit;
11853 }
11854
11855 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11856 Returns NULL if the signature isn't found. */
11857
11858 static struct dwo_unit *
11859 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11860 struct dwp_file *dwp_file, const char *comp_dir,
11861 ULONGEST signature, int is_debug_types)
11862 {
11863 const struct dwp_hash_table *dwp_htab =
11864 is_debug_types ? dwp_file->tus : dwp_file->cus;
11865 bfd *dbfd = dwp_file->dbfd.get ();
11866 uint32_t mask = dwp_htab->nr_slots - 1;
11867 uint32_t hash = signature & mask;
11868 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11869 unsigned int i;
11870 void **slot;
11871 struct dwo_unit find_dwo_cu;
11872
11873 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11874 find_dwo_cu.signature = signature;
11875 slot = htab_find_slot (is_debug_types
11876 ? dwp_file->loaded_tus.get ()
11877 : dwp_file->loaded_cus.get (),
11878 &find_dwo_cu, INSERT);
11879
11880 if (*slot != NULL)
11881 return (struct dwo_unit *) *slot;
11882
11883 /* Use a for loop so that we don't loop forever on bad debug info. */
11884 for (i = 0; i < dwp_htab->nr_slots; ++i)
11885 {
11886 ULONGEST signature_in_table;
11887
11888 signature_in_table =
11889 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11890 if (signature_in_table == signature)
11891 {
11892 uint32_t unit_index =
11893 read_4_bytes (dbfd,
11894 dwp_htab->unit_table + hash * sizeof (uint32_t));
11895
11896 if (dwp_file->version == 1)
11897 {
11898 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11899 dwp_file, unit_index,
11900 comp_dir, signature,
11901 is_debug_types);
11902 }
11903 else
11904 {
11905 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11906 dwp_file, unit_index,
11907 comp_dir, signature,
11908 is_debug_types);
11909 }
11910 return (struct dwo_unit *) *slot;
11911 }
11912 if (signature_in_table == 0)
11913 return NULL;
11914 hash = (hash + hash2) & mask;
11915 }
11916
11917 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11918 " [in module %s]"),
11919 dwp_file->name);
11920 }
11921
11922 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11923 Open the file specified by FILE_NAME and hand it off to BFD for
11924 preliminary analysis. Return a newly initialized bfd *, which
11925 includes a canonicalized copy of FILE_NAME.
11926 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11927 SEARCH_CWD is true if the current directory is to be searched.
11928 It will be searched before debug-file-directory.
11929 If successful, the file is added to the bfd include table of the
11930 objfile's bfd (see gdb_bfd_record_inclusion).
11931 If unable to find/open the file, return NULL.
11932 NOTE: This function is derived from symfile_bfd_open. */
11933
11934 static gdb_bfd_ref_ptr
11935 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11936 const char *file_name, int is_dwp, int search_cwd)
11937 {
11938 int desc;
11939 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11940 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11941 to debug_file_directory. */
11942 const char *search_path;
11943 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11944
11945 gdb::unique_xmalloc_ptr<char> search_path_holder;
11946 if (search_cwd)
11947 {
11948 if (*debug_file_directory != '\0')
11949 {
11950 search_path_holder.reset (concat (".", dirname_separator_string,
11951 debug_file_directory,
11952 (char *) NULL));
11953 search_path = search_path_holder.get ();
11954 }
11955 else
11956 search_path = ".";
11957 }
11958 else
11959 search_path = debug_file_directory;
11960
11961 openp_flags flags = OPF_RETURN_REALPATH;
11962 if (is_dwp)
11963 flags |= OPF_SEARCH_IN_PATH;
11964
11965 gdb::unique_xmalloc_ptr<char> absolute_name;
11966 desc = openp (search_path, flags, file_name,
11967 O_RDONLY | O_BINARY, &absolute_name);
11968 if (desc < 0)
11969 return NULL;
11970
11971 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11972 gnutarget, desc));
11973 if (sym_bfd == NULL)
11974 return NULL;
11975 bfd_set_cacheable (sym_bfd.get (), 1);
11976
11977 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11978 return NULL;
11979
11980 /* Success. Record the bfd as having been included by the objfile's bfd.
11981 This is important because things like demangled_names_hash lives in the
11982 objfile's per_bfd space and may have references to things like symbol
11983 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11984 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
11985
11986 return sym_bfd;
11987 }
11988
11989 /* Try to open DWO file FILE_NAME.
11990 COMP_DIR is the DW_AT_comp_dir attribute.
11991 The result is the bfd handle of the file.
11992 If there is a problem finding or opening the file, return NULL.
11993 Upon success, the canonicalized path of the file is stored in the bfd,
11994 same as symfile_bfd_open. */
11995
11996 static gdb_bfd_ref_ptr
11997 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11998 const char *file_name, const char *comp_dir)
11999 {
12000 if (IS_ABSOLUTE_PATH (file_name))
12001 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12002 0 /*is_dwp*/, 0 /*search_cwd*/);
12003
12004 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12005
12006 if (comp_dir != NULL)
12007 {
12008 gdb::unique_xmalloc_ptr<char> path_to_try
12009 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12010
12011 /* NOTE: If comp_dir is a relative path, this will also try the
12012 search path, which seems useful. */
12013 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12014 path_to_try.get (),
12015 0 /*is_dwp*/,
12016 1 /*search_cwd*/));
12017 if (abfd != NULL)
12018 return abfd;
12019 }
12020
12021 /* That didn't work, try debug-file-directory, which, despite its name,
12022 is a list of paths. */
12023
12024 if (*debug_file_directory == '\0')
12025 return NULL;
12026
12027 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12028 0 /*is_dwp*/, 1 /*search_cwd*/);
12029 }
12030
12031 /* This function is mapped across the sections and remembers the offset and
12032 size of each of the DWO debugging sections we are interested in. */
12033
12034 static void
12035 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12036 {
12037 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12038 const struct dwop_section_names *names = &dwop_section_names;
12039
12040 if (section_is_p (sectp->name, &names->abbrev_dwo))
12041 {
12042 dwo_sections->abbrev.s.section = sectp;
12043 dwo_sections->abbrev.size = bfd_section_size (sectp);
12044 }
12045 else if (section_is_p (sectp->name, &names->info_dwo))
12046 {
12047 dwo_sections->info.s.section = sectp;
12048 dwo_sections->info.size = bfd_section_size (sectp);
12049 }
12050 else if (section_is_p (sectp->name, &names->line_dwo))
12051 {
12052 dwo_sections->line.s.section = sectp;
12053 dwo_sections->line.size = bfd_section_size (sectp);
12054 }
12055 else if (section_is_p (sectp->name, &names->loc_dwo))
12056 {
12057 dwo_sections->loc.s.section = sectp;
12058 dwo_sections->loc.size = bfd_section_size (sectp);
12059 }
12060 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12061 {
12062 dwo_sections->macinfo.s.section = sectp;
12063 dwo_sections->macinfo.size = bfd_section_size (sectp);
12064 }
12065 else if (section_is_p (sectp->name, &names->macro_dwo))
12066 {
12067 dwo_sections->macro.s.section = sectp;
12068 dwo_sections->macro.size = bfd_section_size (sectp);
12069 }
12070 else if (section_is_p (sectp->name, &names->str_dwo))
12071 {
12072 dwo_sections->str.s.section = sectp;
12073 dwo_sections->str.size = bfd_section_size (sectp);
12074 }
12075 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12076 {
12077 dwo_sections->str_offsets.s.section = sectp;
12078 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12079 }
12080 else if (section_is_p (sectp->name, &names->types_dwo))
12081 {
12082 struct dwarf2_section_info type_section;
12083
12084 memset (&type_section, 0, sizeof (type_section));
12085 type_section.s.section = sectp;
12086 type_section.size = bfd_section_size (sectp);
12087 dwo_sections->types.push_back (type_section);
12088 }
12089 }
12090
12091 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12092 by PER_CU. This is for the non-DWP case.
12093 The result is NULL if DWO_NAME can't be found. */
12094
12095 static struct dwo_file *
12096 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12097 const char *dwo_name, const char *comp_dir)
12098 {
12099 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12100
12101 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12102 if (dbfd == NULL)
12103 {
12104 if (dwarf_read_debug)
12105 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12106 return NULL;
12107 }
12108
12109 dwo_file_up dwo_file (new struct dwo_file);
12110 dwo_file->dwo_name = dwo_name;
12111 dwo_file->comp_dir = comp_dir;
12112 dwo_file->dbfd = std::move (dbfd);
12113
12114 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12115 &dwo_file->sections);
12116
12117 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12118 dwo_file->sections.info, dwo_file->cus);
12119
12120 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12121 dwo_file->sections.types, dwo_file->tus);
12122
12123 if (dwarf_read_debug)
12124 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12125
12126 return dwo_file.release ();
12127 }
12128
12129 /* This function is mapped across the sections and remembers the offset and
12130 size of each of the DWP debugging sections common to version 1 and 2 that
12131 we are interested in. */
12132
12133 static void
12134 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12135 void *dwp_file_ptr)
12136 {
12137 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12138 const struct dwop_section_names *names = &dwop_section_names;
12139 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12140
12141 /* Record the ELF section number for later lookup: this is what the
12142 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12143 gdb_assert (elf_section_nr < dwp_file->num_sections);
12144 dwp_file->elf_sections[elf_section_nr] = sectp;
12145
12146 /* Look for specific sections that we need. */
12147 if (section_is_p (sectp->name, &names->str_dwo))
12148 {
12149 dwp_file->sections.str.s.section = sectp;
12150 dwp_file->sections.str.size = bfd_section_size (sectp);
12151 }
12152 else if (section_is_p (sectp->name, &names->cu_index))
12153 {
12154 dwp_file->sections.cu_index.s.section = sectp;
12155 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12156 }
12157 else if (section_is_p (sectp->name, &names->tu_index))
12158 {
12159 dwp_file->sections.tu_index.s.section = sectp;
12160 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12161 }
12162 }
12163
12164 /* This function is mapped across the sections and remembers the offset and
12165 size of each of the DWP version 2 debugging sections that we are interested
12166 in. This is split into a separate function because we don't know if we
12167 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12168
12169 static void
12170 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12171 {
12172 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12173 const struct dwop_section_names *names = &dwop_section_names;
12174 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12175
12176 /* Record the ELF section number for later lookup: this is what the
12177 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12178 gdb_assert (elf_section_nr < dwp_file->num_sections);
12179 dwp_file->elf_sections[elf_section_nr] = sectp;
12180
12181 /* Look for specific sections that we need. */
12182 if (section_is_p (sectp->name, &names->abbrev_dwo))
12183 {
12184 dwp_file->sections.abbrev.s.section = sectp;
12185 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12186 }
12187 else if (section_is_p (sectp->name, &names->info_dwo))
12188 {
12189 dwp_file->sections.info.s.section = sectp;
12190 dwp_file->sections.info.size = bfd_section_size (sectp);
12191 }
12192 else if (section_is_p (sectp->name, &names->line_dwo))
12193 {
12194 dwp_file->sections.line.s.section = sectp;
12195 dwp_file->sections.line.size = bfd_section_size (sectp);
12196 }
12197 else if (section_is_p (sectp->name, &names->loc_dwo))
12198 {
12199 dwp_file->sections.loc.s.section = sectp;
12200 dwp_file->sections.loc.size = bfd_section_size (sectp);
12201 }
12202 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12203 {
12204 dwp_file->sections.macinfo.s.section = sectp;
12205 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12206 }
12207 else if (section_is_p (sectp->name, &names->macro_dwo))
12208 {
12209 dwp_file->sections.macro.s.section = sectp;
12210 dwp_file->sections.macro.size = bfd_section_size (sectp);
12211 }
12212 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12213 {
12214 dwp_file->sections.str_offsets.s.section = sectp;
12215 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12216 }
12217 else if (section_is_p (sectp->name, &names->types_dwo))
12218 {
12219 dwp_file->sections.types.s.section = sectp;
12220 dwp_file->sections.types.size = bfd_section_size (sectp);
12221 }
12222 }
12223
12224 /* Hash function for dwp_file loaded CUs/TUs. */
12225
12226 static hashval_t
12227 hash_dwp_loaded_cutus (const void *item)
12228 {
12229 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12230
12231 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12232 return dwo_unit->signature;
12233 }
12234
12235 /* Equality function for dwp_file loaded CUs/TUs. */
12236
12237 static int
12238 eq_dwp_loaded_cutus (const void *a, const void *b)
12239 {
12240 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12241 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12242
12243 return dua->signature == dub->signature;
12244 }
12245
12246 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12247
12248 static htab_up
12249 allocate_dwp_loaded_cutus_table ()
12250 {
12251 return htab_up (htab_create_alloc (3,
12252 hash_dwp_loaded_cutus,
12253 eq_dwp_loaded_cutus,
12254 NULL, xcalloc, xfree));
12255 }
12256
12257 /* Try to open DWP file FILE_NAME.
12258 The result is the bfd handle of the file.
12259 If there is a problem finding or opening the file, return NULL.
12260 Upon success, the canonicalized path of the file is stored in the bfd,
12261 same as symfile_bfd_open. */
12262
12263 static gdb_bfd_ref_ptr
12264 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12265 const char *file_name)
12266 {
12267 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12268 1 /*is_dwp*/,
12269 1 /*search_cwd*/));
12270 if (abfd != NULL)
12271 return abfd;
12272
12273 /* Work around upstream bug 15652.
12274 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12275 [Whether that's a "bug" is debatable, but it is getting in our way.]
12276 We have no real idea where the dwp file is, because gdb's realpath-ing
12277 of the executable's path may have discarded the needed info.
12278 [IWBN if the dwp file name was recorded in the executable, akin to
12279 .gnu_debuglink, but that doesn't exist yet.]
12280 Strip the directory from FILE_NAME and search again. */
12281 if (*debug_file_directory != '\0')
12282 {
12283 /* Don't implicitly search the current directory here.
12284 If the user wants to search "." to handle this case,
12285 it must be added to debug-file-directory. */
12286 return try_open_dwop_file (dwarf2_per_objfile,
12287 lbasename (file_name), 1 /*is_dwp*/,
12288 0 /*search_cwd*/);
12289 }
12290
12291 return NULL;
12292 }
12293
12294 /* Initialize the use of the DWP file for the current objfile.
12295 By convention the name of the DWP file is ${objfile}.dwp.
12296 The result is NULL if it can't be found. */
12297
12298 static std::unique_ptr<struct dwp_file>
12299 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12300 {
12301 struct objfile *objfile = dwarf2_per_objfile->objfile;
12302
12303 /* Try to find first .dwp for the binary file before any symbolic links
12304 resolving. */
12305
12306 /* If the objfile is a debug file, find the name of the real binary
12307 file and get the name of dwp file from there. */
12308 std::string dwp_name;
12309 if (objfile->separate_debug_objfile_backlink != NULL)
12310 {
12311 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12312 const char *backlink_basename = lbasename (backlink->original_name);
12313
12314 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12315 }
12316 else
12317 dwp_name = objfile->original_name;
12318
12319 dwp_name += ".dwp";
12320
12321 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12322 if (dbfd == NULL
12323 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12324 {
12325 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12326 dwp_name = objfile_name (objfile);
12327 dwp_name += ".dwp";
12328 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12329 }
12330
12331 if (dbfd == NULL)
12332 {
12333 if (dwarf_read_debug)
12334 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12335 return std::unique_ptr<dwp_file> ();
12336 }
12337
12338 const char *name = bfd_get_filename (dbfd.get ());
12339 std::unique_ptr<struct dwp_file> dwp_file
12340 (new struct dwp_file (name, std::move (dbfd)));
12341
12342 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12343 dwp_file->elf_sections =
12344 OBSTACK_CALLOC (&objfile->objfile_obstack,
12345 dwp_file->num_sections, asection *);
12346
12347 bfd_map_over_sections (dwp_file->dbfd.get (),
12348 dwarf2_locate_common_dwp_sections,
12349 dwp_file.get ());
12350
12351 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12352 0);
12353
12354 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12355 1);
12356
12357 /* The DWP file version is stored in the hash table. Oh well. */
12358 if (dwp_file->cus && dwp_file->tus
12359 && dwp_file->cus->version != dwp_file->tus->version)
12360 {
12361 /* Technically speaking, we should try to limp along, but this is
12362 pretty bizarre. We use pulongest here because that's the established
12363 portability solution (e.g, we cannot use %u for uint32_t). */
12364 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12365 " TU version %s [in DWP file %s]"),
12366 pulongest (dwp_file->cus->version),
12367 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12368 }
12369
12370 if (dwp_file->cus)
12371 dwp_file->version = dwp_file->cus->version;
12372 else if (dwp_file->tus)
12373 dwp_file->version = dwp_file->tus->version;
12374 else
12375 dwp_file->version = 2;
12376
12377 if (dwp_file->version == 2)
12378 bfd_map_over_sections (dwp_file->dbfd.get (),
12379 dwarf2_locate_v2_dwp_sections,
12380 dwp_file.get ());
12381
12382 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12383 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12384
12385 if (dwarf_read_debug)
12386 {
12387 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12388 fprintf_unfiltered (gdb_stdlog,
12389 " %s CUs, %s TUs\n",
12390 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12391 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12392 }
12393
12394 return dwp_file;
12395 }
12396
12397 /* Wrapper around open_and_init_dwp_file, only open it once. */
12398
12399 static struct dwp_file *
12400 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12401 {
12402 if (! dwarf2_per_objfile->dwp_checked)
12403 {
12404 dwarf2_per_objfile->dwp_file
12405 = open_and_init_dwp_file (dwarf2_per_objfile);
12406 dwarf2_per_objfile->dwp_checked = 1;
12407 }
12408 return dwarf2_per_objfile->dwp_file.get ();
12409 }
12410
12411 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12412 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12413 or in the DWP file for the objfile, referenced by THIS_UNIT.
12414 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12415 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12416
12417 This is called, for example, when wanting to read a variable with a
12418 complex location. Therefore we don't want to do file i/o for every call.
12419 Therefore we don't want to look for a DWO file on every call.
12420 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12421 then we check if we've already seen DWO_NAME, and only THEN do we check
12422 for a DWO file.
12423
12424 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12425 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12426
12427 static struct dwo_unit *
12428 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12429 const char *dwo_name, const char *comp_dir,
12430 ULONGEST signature, int is_debug_types)
12431 {
12432 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12433 struct objfile *objfile = dwarf2_per_objfile->objfile;
12434 const char *kind = is_debug_types ? "TU" : "CU";
12435 void **dwo_file_slot;
12436 struct dwo_file *dwo_file;
12437 struct dwp_file *dwp_file;
12438
12439 /* First see if there's a DWP file.
12440 If we have a DWP file but didn't find the DWO inside it, don't
12441 look for the original DWO file. It makes gdb behave differently
12442 depending on whether one is debugging in the build tree. */
12443
12444 dwp_file = get_dwp_file (dwarf2_per_objfile);
12445 if (dwp_file != NULL)
12446 {
12447 const struct dwp_hash_table *dwp_htab =
12448 is_debug_types ? dwp_file->tus : dwp_file->cus;
12449
12450 if (dwp_htab != NULL)
12451 {
12452 struct dwo_unit *dwo_cutu =
12453 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12454 signature, is_debug_types);
12455
12456 if (dwo_cutu != NULL)
12457 {
12458 if (dwarf_read_debug)
12459 {
12460 fprintf_unfiltered (gdb_stdlog,
12461 "Virtual DWO %s %s found: @%s\n",
12462 kind, hex_string (signature),
12463 host_address_to_string (dwo_cutu));
12464 }
12465 return dwo_cutu;
12466 }
12467 }
12468 }
12469 else
12470 {
12471 /* No DWP file, look for the DWO file. */
12472
12473 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12474 dwo_name, comp_dir);
12475 if (*dwo_file_slot == NULL)
12476 {
12477 /* Read in the file and build a table of the CUs/TUs it contains. */
12478 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12479 }
12480 /* NOTE: This will be NULL if unable to open the file. */
12481 dwo_file = (struct dwo_file *) *dwo_file_slot;
12482
12483 if (dwo_file != NULL)
12484 {
12485 struct dwo_unit *dwo_cutu = NULL;
12486
12487 if (is_debug_types && dwo_file->tus)
12488 {
12489 struct dwo_unit find_dwo_cutu;
12490
12491 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12492 find_dwo_cutu.signature = signature;
12493 dwo_cutu
12494 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12495 &find_dwo_cutu);
12496 }
12497 else if (!is_debug_types && dwo_file->cus)
12498 {
12499 struct dwo_unit find_dwo_cutu;
12500
12501 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12502 find_dwo_cutu.signature = signature;
12503 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12504 &find_dwo_cutu);
12505 }
12506
12507 if (dwo_cutu != NULL)
12508 {
12509 if (dwarf_read_debug)
12510 {
12511 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12512 kind, dwo_name, hex_string (signature),
12513 host_address_to_string (dwo_cutu));
12514 }
12515 return dwo_cutu;
12516 }
12517 }
12518 }
12519
12520 /* We didn't find it. This could mean a dwo_id mismatch, or
12521 someone deleted the DWO/DWP file, or the search path isn't set up
12522 correctly to find the file. */
12523
12524 if (dwarf_read_debug)
12525 {
12526 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12527 kind, dwo_name, hex_string (signature));
12528 }
12529
12530 /* This is a warning and not a complaint because it can be caused by
12531 pilot error (e.g., user accidentally deleting the DWO). */
12532 {
12533 /* Print the name of the DWP file if we looked there, helps the user
12534 better diagnose the problem. */
12535 std::string dwp_text;
12536
12537 if (dwp_file != NULL)
12538 dwp_text = string_printf (" [in DWP file %s]",
12539 lbasename (dwp_file->name));
12540
12541 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12542 " [in module %s]"),
12543 kind, dwo_name, hex_string (signature),
12544 dwp_text.c_str (),
12545 this_unit->is_debug_types ? "TU" : "CU",
12546 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12547 }
12548 return NULL;
12549 }
12550
12551 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12552 See lookup_dwo_cutu_unit for details. */
12553
12554 static struct dwo_unit *
12555 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12556 const char *dwo_name, const char *comp_dir,
12557 ULONGEST signature)
12558 {
12559 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12560 }
12561
12562 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12563 See lookup_dwo_cutu_unit for details. */
12564
12565 static struct dwo_unit *
12566 lookup_dwo_type_unit (struct signatured_type *this_tu,
12567 const char *dwo_name, const char *comp_dir)
12568 {
12569 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12570 }
12571
12572 /* Traversal function for queue_and_load_all_dwo_tus. */
12573
12574 static int
12575 queue_and_load_dwo_tu (void **slot, void *info)
12576 {
12577 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12578 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12579 ULONGEST signature = dwo_unit->signature;
12580 struct signatured_type *sig_type =
12581 lookup_dwo_signatured_type (per_cu->cu, signature);
12582
12583 if (sig_type != NULL)
12584 {
12585 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12586
12587 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12588 a real dependency of PER_CU on SIG_TYPE. That is detected later
12589 while processing PER_CU. */
12590 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12591 load_full_type_unit (sig_cu);
12592 per_cu->imported_symtabs_push (sig_cu);
12593 }
12594
12595 return 1;
12596 }
12597
12598 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12599 The DWO may have the only definition of the type, though it may not be
12600 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12601 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12602
12603 static void
12604 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12605 {
12606 struct dwo_unit *dwo_unit;
12607 struct dwo_file *dwo_file;
12608
12609 gdb_assert (!per_cu->is_debug_types);
12610 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12611 gdb_assert (per_cu->cu != NULL);
12612
12613 dwo_unit = per_cu->cu->dwo_unit;
12614 gdb_assert (dwo_unit != NULL);
12615
12616 dwo_file = dwo_unit->dwo_file;
12617 if (dwo_file->tus != NULL)
12618 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12619 per_cu);
12620 }
12621
12622 /* Read in various DIEs. */
12623
12624 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12625 Inherit only the children of the DW_AT_abstract_origin DIE not being
12626 already referenced by DW_AT_abstract_origin from the children of the
12627 current DIE. */
12628
12629 static void
12630 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12631 {
12632 struct die_info *child_die;
12633 sect_offset *offsetp;
12634 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12635 struct die_info *origin_die;
12636 /* Iterator of the ORIGIN_DIE children. */
12637 struct die_info *origin_child_die;
12638 struct attribute *attr;
12639 struct dwarf2_cu *origin_cu;
12640 struct pending **origin_previous_list_in_scope;
12641
12642 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12643 if (!attr)
12644 return;
12645
12646 /* Note that following die references may follow to a die in a
12647 different cu. */
12648
12649 origin_cu = cu;
12650 origin_die = follow_die_ref (die, attr, &origin_cu);
12651
12652 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12653 symbols in. */
12654 origin_previous_list_in_scope = origin_cu->list_in_scope;
12655 origin_cu->list_in_scope = cu->list_in_scope;
12656
12657 if (die->tag != origin_die->tag
12658 && !(die->tag == DW_TAG_inlined_subroutine
12659 && origin_die->tag == DW_TAG_subprogram))
12660 complaint (_("DIE %s and its abstract origin %s have different tags"),
12661 sect_offset_str (die->sect_off),
12662 sect_offset_str (origin_die->sect_off));
12663
12664 std::vector<sect_offset> offsets;
12665
12666 for (child_die = die->child;
12667 child_die && child_die->tag;
12668 child_die = child_die->sibling)
12669 {
12670 struct die_info *child_origin_die;
12671 struct dwarf2_cu *child_origin_cu;
12672
12673 /* We are trying to process concrete instance entries:
12674 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12675 it's not relevant to our analysis here. i.e. detecting DIEs that are
12676 present in the abstract instance but not referenced in the concrete
12677 one. */
12678 if (child_die->tag == DW_TAG_call_site
12679 || child_die->tag == DW_TAG_GNU_call_site)
12680 continue;
12681
12682 /* For each CHILD_DIE, find the corresponding child of
12683 ORIGIN_DIE. If there is more than one layer of
12684 DW_AT_abstract_origin, follow them all; there shouldn't be,
12685 but GCC versions at least through 4.4 generate this (GCC PR
12686 40573). */
12687 child_origin_die = child_die;
12688 child_origin_cu = cu;
12689 while (1)
12690 {
12691 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12692 child_origin_cu);
12693 if (attr == NULL)
12694 break;
12695 child_origin_die = follow_die_ref (child_origin_die, attr,
12696 &child_origin_cu);
12697 }
12698
12699 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12700 counterpart may exist. */
12701 if (child_origin_die != child_die)
12702 {
12703 if (child_die->tag != child_origin_die->tag
12704 && !(child_die->tag == DW_TAG_inlined_subroutine
12705 && child_origin_die->tag == DW_TAG_subprogram))
12706 complaint (_("Child DIE %s and its abstract origin %s have "
12707 "different tags"),
12708 sect_offset_str (child_die->sect_off),
12709 sect_offset_str (child_origin_die->sect_off));
12710 if (child_origin_die->parent != origin_die)
12711 complaint (_("Child DIE %s and its abstract origin %s have "
12712 "different parents"),
12713 sect_offset_str (child_die->sect_off),
12714 sect_offset_str (child_origin_die->sect_off));
12715 else
12716 offsets.push_back (child_origin_die->sect_off);
12717 }
12718 }
12719 std::sort (offsets.begin (), offsets.end ());
12720 sect_offset *offsets_end = offsets.data () + offsets.size ();
12721 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12722 if (offsetp[-1] == *offsetp)
12723 complaint (_("Multiple children of DIE %s refer "
12724 "to DIE %s as their abstract origin"),
12725 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12726
12727 offsetp = offsets.data ();
12728 origin_child_die = origin_die->child;
12729 while (origin_child_die && origin_child_die->tag)
12730 {
12731 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12732 while (offsetp < offsets_end
12733 && *offsetp < origin_child_die->sect_off)
12734 offsetp++;
12735 if (offsetp >= offsets_end
12736 || *offsetp > origin_child_die->sect_off)
12737 {
12738 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12739 Check whether we're already processing ORIGIN_CHILD_DIE.
12740 This can happen with mutually referenced abstract_origins.
12741 PR 16581. */
12742 if (!origin_child_die->in_process)
12743 process_die (origin_child_die, origin_cu);
12744 }
12745 origin_child_die = origin_child_die->sibling;
12746 }
12747 origin_cu->list_in_scope = origin_previous_list_in_scope;
12748
12749 if (cu != origin_cu)
12750 compute_delayed_physnames (origin_cu);
12751 }
12752
12753 static void
12754 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12755 {
12756 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12757 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12758 struct context_stack *newobj;
12759 CORE_ADDR lowpc;
12760 CORE_ADDR highpc;
12761 struct die_info *child_die;
12762 struct attribute *attr, *call_line, *call_file;
12763 const char *name;
12764 CORE_ADDR baseaddr;
12765 struct block *block;
12766 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12767 std::vector<struct symbol *> template_args;
12768 struct template_symbol *templ_func = NULL;
12769
12770 if (inlined_func)
12771 {
12772 /* If we do not have call site information, we can't show the
12773 caller of this inlined function. That's too confusing, so
12774 only use the scope for local variables. */
12775 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12776 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12777 if (call_line == NULL || call_file == NULL)
12778 {
12779 read_lexical_block_scope (die, cu);
12780 return;
12781 }
12782 }
12783
12784 baseaddr = objfile->text_section_offset ();
12785
12786 name = dwarf2_name (die, cu);
12787
12788 /* Ignore functions with missing or empty names. These are actually
12789 illegal according to the DWARF standard. */
12790 if (name == NULL)
12791 {
12792 complaint (_("missing name for subprogram DIE at %s"),
12793 sect_offset_str (die->sect_off));
12794 return;
12795 }
12796
12797 /* Ignore functions with missing or invalid low and high pc attributes. */
12798 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12799 <= PC_BOUNDS_INVALID)
12800 {
12801 attr = dwarf2_attr (die, DW_AT_external, cu);
12802 if (!attr || !DW_UNSND (attr))
12803 complaint (_("cannot get low and high bounds "
12804 "for subprogram DIE at %s"),
12805 sect_offset_str (die->sect_off));
12806 return;
12807 }
12808
12809 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12810 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12811
12812 /* If we have any template arguments, then we must allocate a
12813 different sort of symbol. */
12814 for (child_die = die->child; child_die; child_die = child_die->sibling)
12815 {
12816 if (child_die->tag == DW_TAG_template_type_param
12817 || child_die->tag == DW_TAG_template_value_param)
12818 {
12819 templ_func = allocate_template_symbol (objfile);
12820 templ_func->subclass = SYMBOL_TEMPLATE;
12821 break;
12822 }
12823 }
12824
12825 newobj = cu->get_builder ()->push_context (0, lowpc);
12826 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12827 (struct symbol *) templ_func);
12828
12829 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12830 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12831 cu->language);
12832
12833 /* If there is a location expression for DW_AT_frame_base, record
12834 it. */
12835 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12836 if (attr != nullptr)
12837 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12838
12839 /* If there is a location for the static link, record it. */
12840 newobj->static_link = NULL;
12841 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12842 if (attr != nullptr)
12843 {
12844 newobj->static_link
12845 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12846 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12847 cu->per_cu->addr_type ());
12848 }
12849
12850 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12851
12852 if (die->child != NULL)
12853 {
12854 child_die = die->child;
12855 while (child_die && child_die->tag)
12856 {
12857 if (child_die->tag == DW_TAG_template_type_param
12858 || child_die->tag == DW_TAG_template_value_param)
12859 {
12860 struct symbol *arg = new_symbol (child_die, NULL, cu);
12861
12862 if (arg != NULL)
12863 template_args.push_back (arg);
12864 }
12865 else
12866 process_die (child_die, cu);
12867 child_die = child_die->sibling;
12868 }
12869 }
12870
12871 inherit_abstract_dies (die, cu);
12872
12873 /* If we have a DW_AT_specification, we might need to import using
12874 directives from the context of the specification DIE. See the
12875 comment in determine_prefix. */
12876 if (cu->language == language_cplus
12877 && dwarf2_attr (die, DW_AT_specification, cu))
12878 {
12879 struct dwarf2_cu *spec_cu = cu;
12880 struct die_info *spec_die = die_specification (die, &spec_cu);
12881
12882 while (spec_die)
12883 {
12884 child_die = spec_die->child;
12885 while (child_die && child_die->tag)
12886 {
12887 if (child_die->tag == DW_TAG_imported_module)
12888 process_die (child_die, spec_cu);
12889 child_die = child_die->sibling;
12890 }
12891
12892 /* In some cases, GCC generates specification DIEs that
12893 themselves contain DW_AT_specification attributes. */
12894 spec_die = die_specification (spec_die, &spec_cu);
12895 }
12896 }
12897
12898 struct context_stack cstk = cu->get_builder ()->pop_context ();
12899 /* Make a block for the local symbols within. */
12900 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12901 cstk.static_link, lowpc, highpc);
12902
12903 /* For C++, set the block's scope. */
12904 if ((cu->language == language_cplus
12905 || cu->language == language_fortran
12906 || cu->language == language_d
12907 || cu->language == language_rust)
12908 && cu->processing_has_namespace_info)
12909 block_set_scope (block, determine_prefix (die, cu),
12910 &objfile->objfile_obstack);
12911
12912 /* If we have address ranges, record them. */
12913 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12914
12915 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12916
12917 /* Attach template arguments to function. */
12918 if (!template_args.empty ())
12919 {
12920 gdb_assert (templ_func != NULL);
12921
12922 templ_func->n_template_arguments = template_args.size ();
12923 templ_func->template_arguments
12924 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12925 templ_func->n_template_arguments);
12926 memcpy (templ_func->template_arguments,
12927 template_args.data (),
12928 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12929
12930 /* Make sure that the symtab is set on the new symbols. Even
12931 though they don't appear in this symtab directly, other parts
12932 of gdb assume that symbols do, and this is reasonably
12933 true. */
12934 for (symbol *sym : template_args)
12935 symbol_set_symtab (sym, symbol_symtab (templ_func));
12936 }
12937
12938 /* In C++, we can have functions nested inside functions (e.g., when
12939 a function declares a class that has methods). This means that
12940 when we finish processing a function scope, we may need to go
12941 back to building a containing block's symbol lists. */
12942 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12943 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12944
12945 /* If we've finished processing a top-level function, subsequent
12946 symbols go in the file symbol list. */
12947 if (cu->get_builder ()->outermost_context_p ())
12948 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12949 }
12950
12951 /* Process all the DIES contained within a lexical block scope. Start
12952 a new scope, process the dies, and then close the scope. */
12953
12954 static void
12955 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12956 {
12957 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12958 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12959 CORE_ADDR lowpc, highpc;
12960 struct die_info *child_die;
12961 CORE_ADDR baseaddr;
12962
12963 baseaddr = objfile->text_section_offset ();
12964
12965 /* Ignore blocks with missing or invalid low and high pc attributes. */
12966 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12967 as multiple lexical blocks? Handling children in a sane way would
12968 be nasty. Might be easier to properly extend generic blocks to
12969 describe ranges. */
12970 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12971 {
12972 case PC_BOUNDS_NOT_PRESENT:
12973 /* DW_TAG_lexical_block has no attributes, process its children as if
12974 there was no wrapping by that DW_TAG_lexical_block.
12975 GCC does no longer produces such DWARF since GCC r224161. */
12976 for (child_die = die->child;
12977 child_die != NULL && child_die->tag;
12978 child_die = child_die->sibling)
12979 process_die (child_die, cu);
12980 return;
12981 case PC_BOUNDS_INVALID:
12982 return;
12983 }
12984 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12985 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12986
12987 cu->get_builder ()->push_context (0, lowpc);
12988 if (die->child != NULL)
12989 {
12990 child_die = die->child;
12991 while (child_die && child_die->tag)
12992 {
12993 process_die (child_die, cu);
12994 child_die = child_die->sibling;
12995 }
12996 }
12997 inherit_abstract_dies (die, cu);
12998 struct context_stack cstk = cu->get_builder ()->pop_context ();
12999
13000 if (*cu->get_builder ()->get_local_symbols () != NULL
13001 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13002 {
13003 struct block *block
13004 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13005 cstk.start_addr, highpc);
13006
13007 /* Note that recording ranges after traversing children, as we
13008 do here, means that recording a parent's ranges entails
13009 walking across all its children's ranges as they appear in
13010 the address map, which is quadratic behavior.
13011
13012 It would be nicer to record the parent's ranges before
13013 traversing its children, simply overriding whatever you find
13014 there. But since we don't even decide whether to create a
13015 block until after we've traversed its children, that's hard
13016 to do. */
13017 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13018 }
13019 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13020 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13021 }
13022
13023 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13024
13025 static void
13026 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13027 {
13028 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13029 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13030 CORE_ADDR pc, baseaddr;
13031 struct attribute *attr;
13032 struct call_site *call_site, call_site_local;
13033 void **slot;
13034 int nparams;
13035 struct die_info *child_die;
13036
13037 baseaddr = objfile->text_section_offset ();
13038
13039 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13040 if (attr == NULL)
13041 {
13042 /* This was a pre-DWARF-5 GNU extension alias
13043 for DW_AT_call_return_pc. */
13044 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13045 }
13046 if (!attr)
13047 {
13048 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13049 "DIE %s [in module %s]"),
13050 sect_offset_str (die->sect_off), objfile_name (objfile));
13051 return;
13052 }
13053 pc = attr->value_as_address () + baseaddr;
13054 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13055
13056 if (cu->call_site_htab == NULL)
13057 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13058 NULL, &objfile->objfile_obstack,
13059 hashtab_obstack_allocate, NULL);
13060 call_site_local.pc = pc;
13061 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13062 if (*slot != NULL)
13063 {
13064 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13065 "DIE %s [in module %s]"),
13066 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13067 objfile_name (objfile));
13068 return;
13069 }
13070
13071 /* Count parameters at the caller. */
13072
13073 nparams = 0;
13074 for (child_die = die->child; child_die && child_die->tag;
13075 child_die = child_die->sibling)
13076 {
13077 if (child_die->tag != DW_TAG_call_site_parameter
13078 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13079 {
13080 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13081 "DW_TAG_call_site child DIE %s [in module %s]"),
13082 child_die->tag, sect_offset_str (child_die->sect_off),
13083 objfile_name (objfile));
13084 continue;
13085 }
13086
13087 nparams++;
13088 }
13089
13090 call_site
13091 = ((struct call_site *)
13092 obstack_alloc (&objfile->objfile_obstack,
13093 sizeof (*call_site)
13094 + (sizeof (*call_site->parameter) * (nparams - 1))));
13095 *slot = call_site;
13096 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13097 call_site->pc = pc;
13098
13099 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13100 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13101 {
13102 struct die_info *func_die;
13103
13104 /* Skip also over DW_TAG_inlined_subroutine. */
13105 for (func_die = die->parent;
13106 func_die && func_die->tag != DW_TAG_subprogram
13107 && func_die->tag != DW_TAG_subroutine_type;
13108 func_die = func_die->parent);
13109
13110 /* DW_AT_call_all_calls is a superset
13111 of DW_AT_call_all_tail_calls. */
13112 if (func_die
13113 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13114 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13115 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13116 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13117 {
13118 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13119 not complete. But keep CALL_SITE for look ups via call_site_htab,
13120 both the initial caller containing the real return address PC and
13121 the final callee containing the current PC of a chain of tail
13122 calls do not need to have the tail call list complete. But any
13123 function candidate for a virtual tail call frame searched via
13124 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13125 determined unambiguously. */
13126 }
13127 else
13128 {
13129 struct type *func_type = NULL;
13130
13131 if (func_die)
13132 func_type = get_die_type (func_die, cu);
13133 if (func_type != NULL)
13134 {
13135 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13136
13137 /* Enlist this call site to the function. */
13138 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13139 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13140 }
13141 else
13142 complaint (_("Cannot find function owning DW_TAG_call_site "
13143 "DIE %s [in module %s]"),
13144 sect_offset_str (die->sect_off), objfile_name (objfile));
13145 }
13146 }
13147
13148 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13149 if (attr == NULL)
13150 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13151 if (attr == NULL)
13152 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13153 if (attr == NULL)
13154 {
13155 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13156 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13157 }
13158 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13159 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13160 /* Keep NULL DWARF_BLOCK. */;
13161 else if (attr->form_is_block ())
13162 {
13163 struct dwarf2_locexpr_baton *dlbaton;
13164
13165 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13166 dlbaton->data = DW_BLOCK (attr)->data;
13167 dlbaton->size = DW_BLOCK (attr)->size;
13168 dlbaton->per_cu = cu->per_cu;
13169
13170 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13171 }
13172 else if (attr->form_is_ref ())
13173 {
13174 struct dwarf2_cu *target_cu = cu;
13175 struct die_info *target_die;
13176
13177 target_die = follow_die_ref (die, attr, &target_cu);
13178 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13179 if (die_is_declaration (target_die, target_cu))
13180 {
13181 const char *target_physname;
13182
13183 /* Prefer the mangled name; otherwise compute the demangled one. */
13184 target_physname = dw2_linkage_name (target_die, target_cu);
13185 if (target_physname == NULL)
13186 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13187 if (target_physname == NULL)
13188 complaint (_("DW_AT_call_target target DIE has invalid "
13189 "physname, for referencing DIE %s [in module %s]"),
13190 sect_offset_str (die->sect_off), objfile_name (objfile));
13191 else
13192 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13193 }
13194 else
13195 {
13196 CORE_ADDR lowpc;
13197
13198 /* DW_AT_entry_pc should be preferred. */
13199 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13200 <= PC_BOUNDS_INVALID)
13201 complaint (_("DW_AT_call_target target DIE has invalid "
13202 "low pc, for referencing DIE %s [in module %s]"),
13203 sect_offset_str (die->sect_off), objfile_name (objfile));
13204 else
13205 {
13206 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13207 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13208 }
13209 }
13210 }
13211 else
13212 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13213 "block nor reference, for DIE %s [in module %s]"),
13214 sect_offset_str (die->sect_off), objfile_name (objfile));
13215
13216 call_site->per_cu = cu->per_cu;
13217
13218 for (child_die = die->child;
13219 child_die && child_die->tag;
13220 child_die = child_die->sibling)
13221 {
13222 struct call_site_parameter *parameter;
13223 struct attribute *loc, *origin;
13224
13225 if (child_die->tag != DW_TAG_call_site_parameter
13226 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13227 {
13228 /* Already printed the complaint above. */
13229 continue;
13230 }
13231
13232 gdb_assert (call_site->parameter_count < nparams);
13233 parameter = &call_site->parameter[call_site->parameter_count];
13234
13235 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13236 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13237 register is contained in DW_AT_call_value. */
13238
13239 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13240 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13241 if (origin == NULL)
13242 {
13243 /* This was a pre-DWARF-5 GNU extension alias
13244 for DW_AT_call_parameter. */
13245 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13246 }
13247 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13248 {
13249 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13250
13251 sect_offset sect_off = origin->get_ref_die_offset ();
13252 if (!cu->header.offset_in_cu_p (sect_off))
13253 {
13254 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13255 binding can be done only inside one CU. Such referenced DIE
13256 therefore cannot be even moved to DW_TAG_partial_unit. */
13257 complaint (_("DW_AT_call_parameter offset is not in CU for "
13258 "DW_TAG_call_site child DIE %s [in module %s]"),
13259 sect_offset_str (child_die->sect_off),
13260 objfile_name (objfile));
13261 continue;
13262 }
13263 parameter->u.param_cu_off
13264 = (cu_offset) (sect_off - cu->header.sect_off);
13265 }
13266 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13267 {
13268 complaint (_("No DW_FORM_block* DW_AT_location for "
13269 "DW_TAG_call_site child DIE %s [in module %s]"),
13270 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13271 continue;
13272 }
13273 else
13274 {
13275 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13276 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13277 if (parameter->u.dwarf_reg != -1)
13278 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13279 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13280 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13281 &parameter->u.fb_offset))
13282 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13283 else
13284 {
13285 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13286 "for DW_FORM_block* DW_AT_location is supported for "
13287 "DW_TAG_call_site child DIE %s "
13288 "[in module %s]"),
13289 sect_offset_str (child_die->sect_off),
13290 objfile_name (objfile));
13291 continue;
13292 }
13293 }
13294
13295 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13296 if (attr == NULL)
13297 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13298 if (attr == NULL || !attr->form_is_block ())
13299 {
13300 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13301 "DW_TAG_call_site child DIE %s [in module %s]"),
13302 sect_offset_str (child_die->sect_off),
13303 objfile_name (objfile));
13304 continue;
13305 }
13306 parameter->value = DW_BLOCK (attr)->data;
13307 parameter->value_size = DW_BLOCK (attr)->size;
13308
13309 /* Parameters are not pre-cleared by memset above. */
13310 parameter->data_value = NULL;
13311 parameter->data_value_size = 0;
13312 call_site->parameter_count++;
13313
13314 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13315 if (attr == NULL)
13316 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13317 if (attr != nullptr)
13318 {
13319 if (!attr->form_is_block ())
13320 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13321 "DW_TAG_call_site child DIE %s [in module %s]"),
13322 sect_offset_str (child_die->sect_off),
13323 objfile_name (objfile));
13324 else
13325 {
13326 parameter->data_value = DW_BLOCK (attr)->data;
13327 parameter->data_value_size = DW_BLOCK (attr)->size;
13328 }
13329 }
13330 }
13331 }
13332
13333 /* Helper function for read_variable. If DIE represents a virtual
13334 table, then return the type of the concrete object that is
13335 associated with the virtual table. Otherwise, return NULL. */
13336
13337 static struct type *
13338 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13339 {
13340 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13341 if (attr == NULL)
13342 return NULL;
13343
13344 /* Find the type DIE. */
13345 struct die_info *type_die = NULL;
13346 struct dwarf2_cu *type_cu = cu;
13347
13348 if (attr->form_is_ref ())
13349 type_die = follow_die_ref (die, attr, &type_cu);
13350 if (type_die == NULL)
13351 return NULL;
13352
13353 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13354 return NULL;
13355 return die_containing_type (type_die, type_cu);
13356 }
13357
13358 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13359
13360 static void
13361 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13362 {
13363 struct rust_vtable_symbol *storage = NULL;
13364
13365 if (cu->language == language_rust)
13366 {
13367 struct type *containing_type = rust_containing_type (die, cu);
13368
13369 if (containing_type != NULL)
13370 {
13371 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13372
13373 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13374 initialize_objfile_symbol (storage);
13375 storage->concrete_type = containing_type;
13376 storage->subclass = SYMBOL_RUST_VTABLE;
13377 }
13378 }
13379
13380 struct symbol *res = new_symbol (die, NULL, cu, storage);
13381 struct attribute *abstract_origin
13382 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13383 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13384 if (res == NULL && loc && abstract_origin)
13385 {
13386 /* We have a variable without a name, but with a location and an abstract
13387 origin. This may be a concrete instance of an abstract variable
13388 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13389 later. */
13390 struct dwarf2_cu *origin_cu = cu;
13391 struct die_info *origin_die
13392 = follow_die_ref (die, abstract_origin, &origin_cu);
13393 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13394 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13395 }
13396 }
13397
13398 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13399 reading .debug_rnglists.
13400 Callback's type should be:
13401 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13402 Return true if the attributes are present and valid, otherwise,
13403 return false. */
13404
13405 template <typename Callback>
13406 static bool
13407 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13408 Callback &&callback)
13409 {
13410 struct dwarf2_per_objfile *dwarf2_per_objfile
13411 = cu->per_cu->dwarf2_per_objfile;
13412 struct objfile *objfile = dwarf2_per_objfile->objfile;
13413 bfd *obfd = objfile->obfd;
13414 /* Base address selection entry. */
13415 gdb::optional<CORE_ADDR> base;
13416 const gdb_byte *buffer;
13417 CORE_ADDR baseaddr;
13418 bool overflow = false;
13419
13420 base = cu->base_address;
13421
13422 dwarf2_per_objfile->rnglists.read (objfile);
13423 if (offset >= dwarf2_per_objfile->rnglists.size)
13424 {
13425 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13426 offset);
13427 return false;
13428 }
13429 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13430
13431 baseaddr = objfile->text_section_offset ();
13432
13433 while (1)
13434 {
13435 /* Initialize it due to a false compiler warning. */
13436 CORE_ADDR range_beginning = 0, range_end = 0;
13437 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13438 + dwarf2_per_objfile->rnglists.size);
13439 unsigned int bytes_read;
13440
13441 if (buffer == buf_end)
13442 {
13443 overflow = true;
13444 break;
13445 }
13446 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13447 switch (rlet)
13448 {
13449 case DW_RLE_end_of_list:
13450 break;
13451 case DW_RLE_base_address:
13452 if (buffer + cu->header.addr_size > buf_end)
13453 {
13454 overflow = true;
13455 break;
13456 }
13457 base = cu->header.read_address (obfd, buffer, &bytes_read);
13458 buffer += bytes_read;
13459 break;
13460 case DW_RLE_start_length:
13461 if (buffer + cu->header.addr_size > buf_end)
13462 {
13463 overflow = true;
13464 break;
13465 }
13466 range_beginning = cu->header.read_address (obfd, buffer,
13467 &bytes_read);
13468 buffer += bytes_read;
13469 range_end = (range_beginning
13470 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13471 buffer += bytes_read;
13472 if (buffer > buf_end)
13473 {
13474 overflow = true;
13475 break;
13476 }
13477 break;
13478 case DW_RLE_offset_pair:
13479 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13480 buffer += bytes_read;
13481 if (buffer > buf_end)
13482 {
13483 overflow = true;
13484 break;
13485 }
13486 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13487 buffer += bytes_read;
13488 if (buffer > buf_end)
13489 {
13490 overflow = true;
13491 break;
13492 }
13493 break;
13494 case DW_RLE_start_end:
13495 if (buffer + 2 * cu->header.addr_size > buf_end)
13496 {
13497 overflow = true;
13498 break;
13499 }
13500 range_beginning = cu->header.read_address (obfd, buffer,
13501 &bytes_read);
13502 buffer += bytes_read;
13503 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13504 buffer += bytes_read;
13505 break;
13506 default:
13507 complaint (_("Invalid .debug_rnglists data (no base address)"));
13508 return false;
13509 }
13510 if (rlet == DW_RLE_end_of_list || overflow)
13511 break;
13512 if (rlet == DW_RLE_base_address)
13513 continue;
13514
13515 if (!base.has_value ())
13516 {
13517 /* We have no valid base address for the ranges
13518 data. */
13519 complaint (_("Invalid .debug_rnglists data (no base address)"));
13520 return false;
13521 }
13522
13523 if (range_beginning > range_end)
13524 {
13525 /* Inverted range entries are invalid. */
13526 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13527 return false;
13528 }
13529
13530 /* Empty range entries have no effect. */
13531 if (range_beginning == range_end)
13532 continue;
13533
13534 range_beginning += *base;
13535 range_end += *base;
13536
13537 /* A not-uncommon case of bad debug info.
13538 Don't pollute the addrmap with bad data. */
13539 if (range_beginning + baseaddr == 0
13540 && !dwarf2_per_objfile->has_section_at_zero)
13541 {
13542 complaint (_(".debug_rnglists entry has start address of zero"
13543 " [in module %s]"), objfile_name (objfile));
13544 continue;
13545 }
13546
13547 callback (range_beginning, range_end);
13548 }
13549
13550 if (overflow)
13551 {
13552 complaint (_("Offset %d is not terminated "
13553 "for DW_AT_ranges attribute"),
13554 offset);
13555 return false;
13556 }
13557
13558 return true;
13559 }
13560
13561 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13562 Callback's type should be:
13563 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13564 Return 1 if the attributes are present and valid, otherwise, return 0. */
13565
13566 template <typename Callback>
13567 static int
13568 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13569 Callback &&callback)
13570 {
13571 struct dwarf2_per_objfile *dwarf2_per_objfile
13572 = cu->per_cu->dwarf2_per_objfile;
13573 struct objfile *objfile = dwarf2_per_objfile->objfile;
13574 struct comp_unit_head *cu_header = &cu->header;
13575 bfd *obfd = objfile->obfd;
13576 unsigned int addr_size = cu_header->addr_size;
13577 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13578 /* Base address selection entry. */
13579 gdb::optional<CORE_ADDR> base;
13580 unsigned int dummy;
13581 const gdb_byte *buffer;
13582 CORE_ADDR baseaddr;
13583
13584 if (cu_header->version >= 5)
13585 return dwarf2_rnglists_process (offset, cu, callback);
13586
13587 base = cu->base_address;
13588
13589 dwarf2_per_objfile->ranges.read (objfile);
13590 if (offset >= dwarf2_per_objfile->ranges.size)
13591 {
13592 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13593 offset);
13594 return 0;
13595 }
13596 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13597
13598 baseaddr = objfile->text_section_offset ();
13599
13600 while (1)
13601 {
13602 CORE_ADDR range_beginning, range_end;
13603
13604 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13605 buffer += addr_size;
13606 range_end = cu->header.read_address (obfd, buffer, &dummy);
13607 buffer += addr_size;
13608 offset += 2 * addr_size;
13609
13610 /* An end of list marker is a pair of zero addresses. */
13611 if (range_beginning == 0 && range_end == 0)
13612 /* Found the end of list entry. */
13613 break;
13614
13615 /* Each base address selection entry is a pair of 2 values.
13616 The first is the largest possible address, the second is
13617 the base address. Check for a base address here. */
13618 if ((range_beginning & mask) == mask)
13619 {
13620 /* If we found the largest possible address, then we already
13621 have the base address in range_end. */
13622 base = range_end;
13623 continue;
13624 }
13625
13626 if (!base.has_value ())
13627 {
13628 /* We have no valid base address for the ranges
13629 data. */
13630 complaint (_("Invalid .debug_ranges data (no base address)"));
13631 return 0;
13632 }
13633
13634 if (range_beginning > range_end)
13635 {
13636 /* Inverted range entries are invalid. */
13637 complaint (_("Invalid .debug_ranges data (inverted range)"));
13638 return 0;
13639 }
13640
13641 /* Empty range entries have no effect. */
13642 if (range_beginning == range_end)
13643 continue;
13644
13645 range_beginning += *base;
13646 range_end += *base;
13647
13648 /* A not-uncommon case of bad debug info.
13649 Don't pollute the addrmap with bad data. */
13650 if (range_beginning + baseaddr == 0
13651 && !dwarf2_per_objfile->has_section_at_zero)
13652 {
13653 complaint (_(".debug_ranges entry has start address of zero"
13654 " [in module %s]"), objfile_name (objfile));
13655 continue;
13656 }
13657
13658 callback (range_beginning, range_end);
13659 }
13660
13661 return 1;
13662 }
13663
13664 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13665 Return 1 if the attributes are present and valid, otherwise, return 0.
13666 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13667
13668 static int
13669 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13670 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13671 dwarf2_psymtab *ranges_pst)
13672 {
13673 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13674 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13675 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13676 int low_set = 0;
13677 CORE_ADDR low = 0;
13678 CORE_ADDR high = 0;
13679 int retval;
13680
13681 retval = dwarf2_ranges_process (offset, cu,
13682 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13683 {
13684 if (ranges_pst != NULL)
13685 {
13686 CORE_ADDR lowpc;
13687 CORE_ADDR highpc;
13688
13689 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13690 range_beginning + baseaddr)
13691 - baseaddr);
13692 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13693 range_end + baseaddr)
13694 - baseaddr);
13695 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13696 lowpc, highpc - 1, ranges_pst);
13697 }
13698
13699 /* FIXME: This is recording everything as a low-high
13700 segment of consecutive addresses. We should have a
13701 data structure for discontiguous block ranges
13702 instead. */
13703 if (! low_set)
13704 {
13705 low = range_beginning;
13706 high = range_end;
13707 low_set = 1;
13708 }
13709 else
13710 {
13711 if (range_beginning < low)
13712 low = range_beginning;
13713 if (range_end > high)
13714 high = range_end;
13715 }
13716 });
13717 if (!retval)
13718 return 0;
13719
13720 if (! low_set)
13721 /* If the first entry is an end-of-list marker, the range
13722 describes an empty scope, i.e. no instructions. */
13723 return 0;
13724
13725 if (low_return)
13726 *low_return = low;
13727 if (high_return)
13728 *high_return = high;
13729 return 1;
13730 }
13731
13732 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13733 definition for the return value. *LOWPC and *HIGHPC are set iff
13734 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13735
13736 static enum pc_bounds_kind
13737 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13738 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13739 dwarf2_psymtab *pst)
13740 {
13741 struct dwarf2_per_objfile *dwarf2_per_objfile
13742 = cu->per_cu->dwarf2_per_objfile;
13743 struct attribute *attr;
13744 struct attribute *attr_high;
13745 CORE_ADDR low = 0;
13746 CORE_ADDR high = 0;
13747 enum pc_bounds_kind ret;
13748
13749 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13750 if (attr_high)
13751 {
13752 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13753 if (attr != nullptr)
13754 {
13755 low = attr->value_as_address ();
13756 high = attr_high->value_as_address ();
13757 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13758 high += low;
13759 }
13760 else
13761 /* Found high w/o low attribute. */
13762 return PC_BOUNDS_INVALID;
13763
13764 /* Found consecutive range of addresses. */
13765 ret = PC_BOUNDS_HIGH_LOW;
13766 }
13767 else
13768 {
13769 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13770 if (attr != NULL)
13771 {
13772 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13773 We take advantage of the fact that DW_AT_ranges does not appear
13774 in DW_TAG_compile_unit of DWO files. */
13775 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13776 unsigned int ranges_offset = (DW_UNSND (attr)
13777 + (need_ranges_base
13778 ? cu->ranges_base
13779 : 0));
13780
13781 /* Value of the DW_AT_ranges attribute is the offset in the
13782 .debug_ranges section. */
13783 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13784 return PC_BOUNDS_INVALID;
13785 /* Found discontinuous range of addresses. */
13786 ret = PC_BOUNDS_RANGES;
13787 }
13788 else
13789 return PC_BOUNDS_NOT_PRESENT;
13790 }
13791
13792 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13793 if (high <= low)
13794 return PC_BOUNDS_INVALID;
13795
13796 /* When using the GNU linker, .gnu.linkonce. sections are used to
13797 eliminate duplicate copies of functions and vtables and such.
13798 The linker will arbitrarily choose one and discard the others.
13799 The AT_*_pc values for such functions refer to local labels in
13800 these sections. If the section from that file was discarded, the
13801 labels are not in the output, so the relocs get a value of 0.
13802 If this is a discarded function, mark the pc bounds as invalid,
13803 so that GDB will ignore it. */
13804 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13805 return PC_BOUNDS_INVALID;
13806
13807 *lowpc = low;
13808 if (highpc)
13809 *highpc = high;
13810 return ret;
13811 }
13812
13813 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13814 its low and high PC addresses. Do nothing if these addresses could not
13815 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13816 and HIGHPC to the high address if greater than HIGHPC. */
13817
13818 static void
13819 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13820 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13821 struct dwarf2_cu *cu)
13822 {
13823 CORE_ADDR low, high;
13824 struct die_info *child = die->child;
13825
13826 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13827 {
13828 *lowpc = std::min (*lowpc, low);
13829 *highpc = std::max (*highpc, high);
13830 }
13831
13832 /* If the language does not allow nested subprograms (either inside
13833 subprograms or lexical blocks), we're done. */
13834 if (cu->language != language_ada)
13835 return;
13836
13837 /* Check all the children of the given DIE. If it contains nested
13838 subprograms, then check their pc bounds. Likewise, we need to
13839 check lexical blocks as well, as they may also contain subprogram
13840 definitions. */
13841 while (child && child->tag)
13842 {
13843 if (child->tag == DW_TAG_subprogram
13844 || child->tag == DW_TAG_lexical_block)
13845 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13846 child = child->sibling;
13847 }
13848 }
13849
13850 /* Get the low and high pc's represented by the scope DIE, and store
13851 them in *LOWPC and *HIGHPC. If the correct values can't be
13852 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13853
13854 static void
13855 get_scope_pc_bounds (struct die_info *die,
13856 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13857 struct dwarf2_cu *cu)
13858 {
13859 CORE_ADDR best_low = (CORE_ADDR) -1;
13860 CORE_ADDR best_high = (CORE_ADDR) 0;
13861 CORE_ADDR current_low, current_high;
13862
13863 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13864 >= PC_BOUNDS_RANGES)
13865 {
13866 best_low = current_low;
13867 best_high = current_high;
13868 }
13869 else
13870 {
13871 struct die_info *child = die->child;
13872
13873 while (child && child->tag)
13874 {
13875 switch (child->tag) {
13876 case DW_TAG_subprogram:
13877 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13878 break;
13879 case DW_TAG_namespace:
13880 case DW_TAG_module:
13881 /* FIXME: carlton/2004-01-16: Should we do this for
13882 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13883 that current GCC's always emit the DIEs corresponding
13884 to definitions of methods of classes as children of a
13885 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13886 the DIEs giving the declarations, which could be
13887 anywhere). But I don't see any reason why the
13888 standards says that they have to be there. */
13889 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13890
13891 if (current_low != ((CORE_ADDR) -1))
13892 {
13893 best_low = std::min (best_low, current_low);
13894 best_high = std::max (best_high, current_high);
13895 }
13896 break;
13897 default:
13898 /* Ignore. */
13899 break;
13900 }
13901
13902 child = child->sibling;
13903 }
13904 }
13905
13906 *lowpc = best_low;
13907 *highpc = best_high;
13908 }
13909
13910 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13911 in DIE. */
13912
13913 static void
13914 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13915 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13916 {
13917 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13918 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13919 struct attribute *attr;
13920 struct attribute *attr_high;
13921
13922 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13923 if (attr_high)
13924 {
13925 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13926 if (attr != nullptr)
13927 {
13928 CORE_ADDR low = attr->value_as_address ();
13929 CORE_ADDR high = attr_high->value_as_address ();
13930
13931 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13932 high += low;
13933
13934 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13935 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13936 cu->get_builder ()->record_block_range (block, low, high - 1);
13937 }
13938 }
13939
13940 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13941 if (attr != nullptr)
13942 {
13943 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13944 We take advantage of the fact that DW_AT_ranges does not appear
13945 in DW_TAG_compile_unit of DWO files. */
13946 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13947
13948 /* The value of the DW_AT_ranges attribute is the offset of the
13949 address range list in the .debug_ranges section. */
13950 unsigned long offset = (DW_UNSND (attr)
13951 + (need_ranges_base ? cu->ranges_base : 0));
13952
13953 std::vector<blockrange> blockvec;
13954 dwarf2_ranges_process (offset, cu,
13955 [&] (CORE_ADDR start, CORE_ADDR end)
13956 {
13957 start += baseaddr;
13958 end += baseaddr;
13959 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13960 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13961 cu->get_builder ()->record_block_range (block, start, end - 1);
13962 blockvec.emplace_back (start, end);
13963 });
13964
13965 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
13966 }
13967 }
13968
13969 /* Check whether the producer field indicates either of GCC < 4.6, or the
13970 Intel C/C++ compiler, and cache the result in CU. */
13971
13972 static void
13973 check_producer (struct dwarf2_cu *cu)
13974 {
13975 int major, minor;
13976
13977 if (cu->producer == NULL)
13978 {
13979 /* For unknown compilers expect their behavior is DWARF version
13980 compliant.
13981
13982 GCC started to support .debug_types sections by -gdwarf-4 since
13983 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13984 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13985 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13986 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13987 }
13988 else if (producer_is_gcc (cu->producer, &major, &minor))
13989 {
13990 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13991 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13992 }
13993 else if (producer_is_icc (cu->producer, &major, &minor))
13994 {
13995 cu->producer_is_icc = true;
13996 cu->producer_is_icc_lt_14 = major < 14;
13997 }
13998 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
13999 cu->producer_is_codewarrior = true;
14000 else
14001 {
14002 /* For other non-GCC compilers, expect their behavior is DWARF version
14003 compliant. */
14004 }
14005
14006 cu->checked_producer = true;
14007 }
14008
14009 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14010 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14011 during 4.6.0 experimental. */
14012
14013 static bool
14014 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14015 {
14016 if (!cu->checked_producer)
14017 check_producer (cu);
14018
14019 return cu->producer_is_gxx_lt_4_6;
14020 }
14021
14022
14023 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14024 with incorrect is_stmt attributes. */
14025
14026 static bool
14027 producer_is_codewarrior (struct dwarf2_cu *cu)
14028 {
14029 if (!cu->checked_producer)
14030 check_producer (cu);
14031
14032 return cu->producer_is_codewarrior;
14033 }
14034
14035 /* Return the default accessibility type if it is not overridden by
14036 DW_AT_accessibility. */
14037
14038 static enum dwarf_access_attribute
14039 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14040 {
14041 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14042 {
14043 /* The default DWARF 2 accessibility for members is public, the default
14044 accessibility for inheritance is private. */
14045
14046 if (die->tag != DW_TAG_inheritance)
14047 return DW_ACCESS_public;
14048 else
14049 return DW_ACCESS_private;
14050 }
14051 else
14052 {
14053 /* DWARF 3+ defines the default accessibility a different way. The same
14054 rules apply now for DW_TAG_inheritance as for the members and it only
14055 depends on the container kind. */
14056
14057 if (die->parent->tag == DW_TAG_class_type)
14058 return DW_ACCESS_private;
14059 else
14060 return DW_ACCESS_public;
14061 }
14062 }
14063
14064 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14065 offset. If the attribute was not found return 0, otherwise return
14066 1. If it was found but could not properly be handled, set *OFFSET
14067 to 0. */
14068
14069 static int
14070 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14071 LONGEST *offset)
14072 {
14073 struct attribute *attr;
14074
14075 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14076 if (attr != NULL)
14077 {
14078 *offset = 0;
14079
14080 /* Note that we do not check for a section offset first here.
14081 This is because DW_AT_data_member_location is new in DWARF 4,
14082 so if we see it, we can assume that a constant form is really
14083 a constant and not a section offset. */
14084 if (attr->form_is_constant ())
14085 *offset = attr->constant_value (0);
14086 else if (attr->form_is_section_offset ())
14087 dwarf2_complex_location_expr_complaint ();
14088 else if (attr->form_is_block ())
14089 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14090 else
14091 dwarf2_complex_location_expr_complaint ();
14092
14093 return 1;
14094 }
14095
14096 return 0;
14097 }
14098
14099 /* Add an aggregate field to the field list. */
14100
14101 static void
14102 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14103 struct dwarf2_cu *cu)
14104 {
14105 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14106 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14107 struct nextfield *new_field;
14108 struct attribute *attr;
14109 struct field *fp;
14110 const char *fieldname = "";
14111
14112 if (die->tag == DW_TAG_inheritance)
14113 {
14114 fip->baseclasses.emplace_back ();
14115 new_field = &fip->baseclasses.back ();
14116 }
14117 else
14118 {
14119 fip->fields.emplace_back ();
14120 new_field = &fip->fields.back ();
14121 }
14122
14123 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14124 if (attr != nullptr)
14125 new_field->accessibility = DW_UNSND (attr);
14126 else
14127 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14128 if (new_field->accessibility != DW_ACCESS_public)
14129 fip->non_public_fields = 1;
14130
14131 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14132 if (attr != nullptr)
14133 new_field->virtuality = DW_UNSND (attr);
14134 else
14135 new_field->virtuality = DW_VIRTUALITY_none;
14136
14137 fp = &new_field->field;
14138
14139 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14140 {
14141 LONGEST offset;
14142
14143 /* Data member other than a C++ static data member. */
14144
14145 /* Get type of field. */
14146 fp->type = die_type (die, cu);
14147
14148 SET_FIELD_BITPOS (*fp, 0);
14149
14150 /* Get bit size of field (zero if none). */
14151 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14152 if (attr != nullptr)
14153 {
14154 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14155 }
14156 else
14157 {
14158 FIELD_BITSIZE (*fp) = 0;
14159 }
14160
14161 /* Get bit offset of field. */
14162 if (handle_data_member_location (die, cu, &offset))
14163 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14164 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14165 if (attr != nullptr)
14166 {
14167 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14168 {
14169 /* For big endian bits, the DW_AT_bit_offset gives the
14170 additional bit offset from the MSB of the containing
14171 anonymous object to the MSB of the field. We don't
14172 have to do anything special since we don't need to
14173 know the size of the anonymous object. */
14174 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14175 }
14176 else
14177 {
14178 /* For little endian bits, compute the bit offset to the
14179 MSB of the anonymous object, subtract off the number of
14180 bits from the MSB of the field to the MSB of the
14181 object, and then subtract off the number of bits of
14182 the field itself. The result is the bit offset of
14183 the LSB of the field. */
14184 int anonymous_size;
14185 int bit_offset = DW_UNSND (attr);
14186
14187 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14188 if (attr != nullptr)
14189 {
14190 /* The size of the anonymous object containing
14191 the bit field is explicit, so use the
14192 indicated size (in bytes). */
14193 anonymous_size = DW_UNSND (attr);
14194 }
14195 else
14196 {
14197 /* The size of the anonymous object containing
14198 the bit field must be inferred from the type
14199 attribute of the data member containing the
14200 bit field. */
14201 anonymous_size = TYPE_LENGTH (fp->type);
14202 }
14203 SET_FIELD_BITPOS (*fp,
14204 (FIELD_BITPOS (*fp)
14205 + anonymous_size * bits_per_byte
14206 - bit_offset - FIELD_BITSIZE (*fp)));
14207 }
14208 }
14209 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14210 if (attr != NULL)
14211 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14212 + attr->constant_value (0)));
14213
14214 /* Get name of field. */
14215 fieldname = dwarf2_name (die, cu);
14216 if (fieldname == NULL)
14217 fieldname = "";
14218
14219 /* The name is already allocated along with this objfile, so we don't
14220 need to duplicate it for the type. */
14221 fp->name = fieldname;
14222
14223 /* Change accessibility for artificial fields (e.g. virtual table
14224 pointer or virtual base class pointer) to private. */
14225 if (dwarf2_attr (die, DW_AT_artificial, cu))
14226 {
14227 FIELD_ARTIFICIAL (*fp) = 1;
14228 new_field->accessibility = DW_ACCESS_private;
14229 fip->non_public_fields = 1;
14230 }
14231 }
14232 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14233 {
14234 /* C++ static member. */
14235
14236 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14237 is a declaration, but all versions of G++ as of this writing
14238 (so through at least 3.2.1) incorrectly generate
14239 DW_TAG_variable tags. */
14240
14241 const char *physname;
14242
14243 /* Get name of field. */
14244 fieldname = dwarf2_name (die, cu);
14245 if (fieldname == NULL)
14246 return;
14247
14248 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14249 if (attr
14250 /* Only create a symbol if this is an external value.
14251 new_symbol checks this and puts the value in the global symbol
14252 table, which we want. If it is not external, new_symbol
14253 will try to put the value in cu->list_in_scope which is wrong. */
14254 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14255 {
14256 /* A static const member, not much different than an enum as far as
14257 we're concerned, except that we can support more types. */
14258 new_symbol (die, NULL, cu);
14259 }
14260
14261 /* Get physical name. */
14262 physname = dwarf2_physname (fieldname, die, cu);
14263
14264 /* The name is already allocated along with this objfile, so we don't
14265 need to duplicate it for the type. */
14266 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14267 FIELD_TYPE (*fp) = die_type (die, cu);
14268 FIELD_NAME (*fp) = fieldname;
14269 }
14270 else if (die->tag == DW_TAG_inheritance)
14271 {
14272 LONGEST offset;
14273
14274 /* C++ base class field. */
14275 if (handle_data_member_location (die, cu, &offset))
14276 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14277 FIELD_BITSIZE (*fp) = 0;
14278 FIELD_TYPE (*fp) = die_type (die, cu);
14279 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14280 }
14281 else if (die->tag == DW_TAG_variant_part)
14282 {
14283 /* process_structure_scope will treat this DIE as a union. */
14284 process_structure_scope (die, cu);
14285
14286 /* The variant part is relative to the start of the enclosing
14287 structure. */
14288 SET_FIELD_BITPOS (*fp, 0);
14289 fp->type = get_die_type (die, cu);
14290 fp->artificial = 1;
14291 fp->name = "<<variant>>";
14292
14293 /* Normally a DW_TAG_variant_part won't have a size, but our
14294 representation requires one, so set it to the maximum of the
14295 child sizes, being sure to account for the offset at which
14296 each child is seen. */
14297 if (TYPE_LENGTH (fp->type) == 0)
14298 {
14299 unsigned max = 0;
14300 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14301 {
14302 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14303 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14304 if (len > max)
14305 max = len;
14306 }
14307 TYPE_LENGTH (fp->type) = max;
14308 }
14309 }
14310 else
14311 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14312 }
14313
14314 /* Can the type given by DIE define another type? */
14315
14316 static bool
14317 type_can_define_types (const struct die_info *die)
14318 {
14319 switch (die->tag)
14320 {
14321 case DW_TAG_typedef:
14322 case DW_TAG_class_type:
14323 case DW_TAG_structure_type:
14324 case DW_TAG_union_type:
14325 case DW_TAG_enumeration_type:
14326 return true;
14327
14328 default:
14329 return false;
14330 }
14331 }
14332
14333 /* Add a type definition defined in the scope of the FIP's class. */
14334
14335 static void
14336 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14337 struct dwarf2_cu *cu)
14338 {
14339 struct decl_field fp;
14340 memset (&fp, 0, sizeof (fp));
14341
14342 gdb_assert (type_can_define_types (die));
14343
14344 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14345 fp.name = dwarf2_name (die, cu);
14346 fp.type = read_type_die (die, cu);
14347
14348 /* Save accessibility. */
14349 enum dwarf_access_attribute accessibility;
14350 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14351 if (attr != NULL)
14352 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14353 else
14354 accessibility = dwarf2_default_access_attribute (die, cu);
14355 switch (accessibility)
14356 {
14357 case DW_ACCESS_public:
14358 /* The assumed value if neither private nor protected. */
14359 break;
14360 case DW_ACCESS_private:
14361 fp.is_private = 1;
14362 break;
14363 case DW_ACCESS_protected:
14364 fp.is_protected = 1;
14365 break;
14366 default:
14367 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14368 }
14369
14370 if (die->tag == DW_TAG_typedef)
14371 fip->typedef_field_list.push_back (fp);
14372 else
14373 fip->nested_types_list.push_back (fp);
14374 }
14375
14376 /* Create the vector of fields, and attach it to the type. */
14377
14378 static void
14379 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14380 struct dwarf2_cu *cu)
14381 {
14382 int nfields = fip->nfields ();
14383
14384 /* Record the field count, allocate space for the array of fields,
14385 and create blank accessibility bitfields if necessary. */
14386 TYPE_NFIELDS (type) = nfields;
14387 TYPE_FIELDS (type) = (struct field *)
14388 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14389
14390 if (fip->non_public_fields && cu->language != language_ada)
14391 {
14392 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14393
14394 TYPE_FIELD_PRIVATE_BITS (type) =
14395 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14396 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14397
14398 TYPE_FIELD_PROTECTED_BITS (type) =
14399 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14400 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14401
14402 TYPE_FIELD_IGNORE_BITS (type) =
14403 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14404 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14405 }
14406
14407 /* If the type has baseclasses, allocate and clear a bit vector for
14408 TYPE_FIELD_VIRTUAL_BITS. */
14409 if (!fip->baseclasses.empty () && cu->language != language_ada)
14410 {
14411 int num_bytes = B_BYTES (fip->baseclasses.size ());
14412 unsigned char *pointer;
14413
14414 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14415 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14416 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14417 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14418 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14419 }
14420
14421 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14422 {
14423 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14424
14425 for (int index = 0; index < nfields; ++index)
14426 {
14427 struct nextfield &field = fip->fields[index];
14428
14429 if (field.variant.is_discriminant)
14430 di->discriminant_index = index;
14431 else if (field.variant.default_branch)
14432 di->default_index = index;
14433 else
14434 di->discriminants[index] = field.variant.discriminant_value;
14435 }
14436 }
14437
14438 /* Copy the saved-up fields into the field vector. */
14439 for (int i = 0; i < nfields; ++i)
14440 {
14441 struct nextfield &field
14442 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14443 : fip->fields[i - fip->baseclasses.size ()]);
14444
14445 TYPE_FIELD (type, i) = field.field;
14446 switch (field.accessibility)
14447 {
14448 case DW_ACCESS_private:
14449 if (cu->language != language_ada)
14450 SET_TYPE_FIELD_PRIVATE (type, i);
14451 break;
14452
14453 case DW_ACCESS_protected:
14454 if (cu->language != language_ada)
14455 SET_TYPE_FIELD_PROTECTED (type, i);
14456 break;
14457
14458 case DW_ACCESS_public:
14459 break;
14460
14461 default:
14462 /* Unknown accessibility. Complain and treat it as public. */
14463 {
14464 complaint (_("unsupported accessibility %d"),
14465 field.accessibility);
14466 }
14467 break;
14468 }
14469 if (i < fip->baseclasses.size ())
14470 {
14471 switch (field.virtuality)
14472 {
14473 case DW_VIRTUALITY_virtual:
14474 case DW_VIRTUALITY_pure_virtual:
14475 if (cu->language == language_ada)
14476 error (_("unexpected virtuality in component of Ada type"));
14477 SET_TYPE_FIELD_VIRTUAL (type, i);
14478 break;
14479 }
14480 }
14481 }
14482 }
14483
14484 /* Return true if this member function is a constructor, false
14485 otherwise. */
14486
14487 static int
14488 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14489 {
14490 const char *fieldname;
14491 const char *type_name;
14492 int len;
14493
14494 if (die->parent == NULL)
14495 return 0;
14496
14497 if (die->parent->tag != DW_TAG_structure_type
14498 && die->parent->tag != DW_TAG_union_type
14499 && die->parent->tag != DW_TAG_class_type)
14500 return 0;
14501
14502 fieldname = dwarf2_name (die, cu);
14503 type_name = dwarf2_name (die->parent, cu);
14504 if (fieldname == NULL || type_name == NULL)
14505 return 0;
14506
14507 len = strlen (fieldname);
14508 return (strncmp (fieldname, type_name, len) == 0
14509 && (type_name[len] == '\0' || type_name[len] == '<'));
14510 }
14511
14512 /* Check if the given VALUE is a recognized enum
14513 dwarf_defaulted_attribute constant according to DWARF5 spec,
14514 Table 7.24. */
14515
14516 static bool
14517 is_valid_DW_AT_defaulted (ULONGEST value)
14518 {
14519 switch (value)
14520 {
14521 case DW_DEFAULTED_no:
14522 case DW_DEFAULTED_in_class:
14523 case DW_DEFAULTED_out_of_class:
14524 return true;
14525 }
14526
14527 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14528 return false;
14529 }
14530
14531 /* Add a member function to the proper fieldlist. */
14532
14533 static void
14534 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14535 struct type *type, struct dwarf2_cu *cu)
14536 {
14537 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14538 struct attribute *attr;
14539 int i;
14540 struct fnfieldlist *flp = nullptr;
14541 struct fn_field *fnp;
14542 const char *fieldname;
14543 struct type *this_type;
14544 enum dwarf_access_attribute accessibility;
14545
14546 if (cu->language == language_ada)
14547 error (_("unexpected member function in Ada type"));
14548
14549 /* Get name of member function. */
14550 fieldname = dwarf2_name (die, cu);
14551 if (fieldname == NULL)
14552 return;
14553
14554 /* Look up member function name in fieldlist. */
14555 for (i = 0; i < fip->fnfieldlists.size (); i++)
14556 {
14557 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14558 {
14559 flp = &fip->fnfieldlists[i];
14560 break;
14561 }
14562 }
14563
14564 /* Create a new fnfieldlist if necessary. */
14565 if (flp == nullptr)
14566 {
14567 fip->fnfieldlists.emplace_back ();
14568 flp = &fip->fnfieldlists.back ();
14569 flp->name = fieldname;
14570 i = fip->fnfieldlists.size () - 1;
14571 }
14572
14573 /* Create a new member function field and add it to the vector of
14574 fnfieldlists. */
14575 flp->fnfields.emplace_back ();
14576 fnp = &flp->fnfields.back ();
14577
14578 /* Delay processing of the physname until later. */
14579 if (cu->language == language_cplus)
14580 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14581 die, cu);
14582 else
14583 {
14584 const char *physname = dwarf2_physname (fieldname, die, cu);
14585 fnp->physname = physname ? physname : "";
14586 }
14587
14588 fnp->type = alloc_type (objfile);
14589 this_type = read_type_die (die, cu);
14590 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14591 {
14592 int nparams = TYPE_NFIELDS (this_type);
14593
14594 /* TYPE is the domain of this method, and THIS_TYPE is the type
14595 of the method itself (TYPE_CODE_METHOD). */
14596 smash_to_method_type (fnp->type, type,
14597 TYPE_TARGET_TYPE (this_type),
14598 TYPE_FIELDS (this_type),
14599 TYPE_NFIELDS (this_type),
14600 TYPE_VARARGS (this_type));
14601
14602 /* Handle static member functions.
14603 Dwarf2 has no clean way to discern C++ static and non-static
14604 member functions. G++ helps GDB by marking the first
14605 parameter for non-static member functions (which is the this
14606 pointer) as artificial. We obtain this information from
14607 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14608 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14609 fnp->voffset = VOFFSET_STATIC;
14610 }
14611 else
14612 complaint (_("member function type missing for '%s'"),
14613 dwarf2_full_name (fieldname, die, cu));
14614
14615 /* Get fcontext from DW_AT_containing_type if present. */
14616 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14617 fnp->fcontext = die_containing_type (die, cu);
14618
14619 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14620 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14621
14622 /* Get accessibility. */
14623 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14624 if (attr != nullptr)
14625 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14626 else
14627 accessibility = dwarf2_default_access_attribute (die, cu);
14628 switch (accessibility)
14629 {
14630 case DW_ACCESS_private:
14631 fnp->is_private = 1;
14632 break;
14633 case DW_ACCESS_protected:
14634 fnp->is_protected = 1;
14635 break;
14636 }
14637
14638 /* Check for artificial methods. */
14639 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14640 if (attr && DW_UNSND (attr) != 0)
14641 fnp->is_artificial = 1;
14642
14643 /* Check for defaulted methods. */
14644 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14645 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14646 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14647
14648 /* Check for deleted methods. */
14649 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14650 if (attr != nullptr && DW_UNSND (attr) != 0)
14651 fnp->is_deleted = 1;
14652
14653 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14654
14655 /* Get index in virtual function table if it is a virtual member
14656 function. For older versions of GCC, this is an offset in the
14657 appropriate virtual table, as specified by DW_AT_containing_type.
14658 For everyone else, it is an expression to be evaluated relative
14659 to the object address. */
14660
14661 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14662 if (attr != nullptr)
14663 {
14664 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14665 {
14666 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14667 {
14668 /* Old-style GCC. */
14669 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14670 }
14671 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14672 || (DW_BLOCK (attr)->size > 1
14673 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14674 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14675 {
14676 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14677 if ((fnp->voffset % cu->header.addr_size) != 0)
14678 dwarf2_complex_location_expr_complaint ();
14679 else
14680 fnp->voffset /= cu->header.addr_size;
14681 fnp->voffset += 2;
14682 }
14683 else
14684 dwarf2_complex_location_expr_complaint ();
14685
14686 if (!fnp->fcontext)
14687 {
14688 /* If there is no `this' field and no DW_AT_containing_type,
14689 we cannot actually find a base class context for the
14690 vtable! */
14691 if (TYPE_NFIELDS (this_type) == 0
14692 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14693 {
14694 complaint (_("cannot determine context for virtual member "
14695 "function \"%s\" (offset %s)"),
14696 fieldname, sect_offset_str (die->sect_off));
14697 }
14698 else
14699 {
14700 fnp->fcontext
14701 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14702 }
14703 }
14704 }
14705 else if (attr->form_is_section_offset ())
14706 {
14707 dwarf2_complex_location_expr_complaint ();
14708 }
14709 else
14710 {
14711 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14712 fieldname);
14713 }
14714 }
14715 else
14716 {
14717 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14718 if (attr && DW_UNSND (attr))
14719 {
14720 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14721 complaint (_("Member function \"%s\" (offset %s) is virtual "
14722 "but the vtable offset is not specified"),
14723 fieldname, sect_offset_str (die->sect_off));
14724 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14725 TYPE_CPLUS_DYNAMIC (type) = 1;
14726 }
14727 }
14728 }
14729
14730 /* Create the vector of member function fields, and attach it to the type. */
14731
14732 static void
14733 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14734 struct dwarf2_cu *cu)
14735 {
14736 if (cu->language == language_ada)
14737 error (_("unexpected member functions in Ada type"));
14738
14739 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14740 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14741 TYPE_ALLOC (type,
14742 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14743
14744 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14745 {
14746 struct fnfieldlist &nf = fip->fnfieldlists[i];
14747 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14748
14749 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14750 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14751 fn_flp->fn_fields = (struct fn_field *)
14752 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14753
14754 for (int k = 0; k < nf.fnfields.size (); ++k)
14755 fn_flp->fn_fields[k] = nf.fnfields[k];
14756 }
14757
14758 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14759 }
14760
14761 /* Returns non-zero if NAME is the name of a vtable member in CU's
14762 language, zero otherwise. */
14763 static int
14764 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14765 {
14766 static const char vptr[] = "_vptr";
14767
14768 /* Look for the C++ form of the vtable. */
14769 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14770 return 1;
14771
14772 return 0;
14773 }
14774
14775 /* GCC outputs unnamed structures that are really pointers to member
14776 functions, with the ABI-specified layout. If TYPE describes
14777 such a structure, smash it into a member function type.
14778
14779 GCC shouldn't do this; it should just output pointer to member DIEs.
14780 This is GCC PR debug/28767. */
14781
14782 static void
14783 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14784 {
14785 struct type *pfn_type, *self_type, *new_type;
14786
14787 /* Check for a structure with no name and two children. */
14788 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14789 return;
14790
14791 /* Check for __pfn and __delta members. */
14792 if (TYPE_FIELD_NAME (type, 0) == NULL
14793 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14794 || TYPE_FIELD_NAME (type, 1) == NULL
14795 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14796 return;
14797
14798 /* Find the type of the method. */
14799 pfn_type = TYPE_FIELD_TYPE (type, 0);
14800 if (pfn_type == NULL
14801 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14802 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14803 return;
14804
14805 /* Look for the "this" argument. */
14806 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14807 if (TYPE_NFIELDS (pfn_type) == 0
14808 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14809 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14810 return;
14811
14812 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14813 new_type = alloc_type (objfile);
14814 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14815 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14816 TYPE_VARARGS (pfn_type));
14817 smash_to_methodptr_type (type, new_type);
14818 }
14819
14820 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14821 appropriate error checking and issuing complaints if there is a
14822 problem. */
14823
14824 static ULONGEST
14825 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14826 {
14827 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14828
14829 if (attr == nullptr)
14830 return 0;
14831
14832 if (!attr->form_is_constant ())
14833 {
14834 complaint (_("DW_AT_alignment must have constant form"
14835 " - DIE at %s [in module %s]"),
14836 sect_offset_str (die->sect_off),
14837 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14838 return 0;
14839 }
14840
14841 ULONGEST align;
14842 if (attr->form == DW_FORM_sdata)
14843 {
14844 LONGEST val = DW_SND (attr);
14845 if (val < 0)
14846 {
14847 complaint (_("DW_AT_alignment value must not be negative"
14848 " - DIE at %s [in module %s]"),
14849 sect_offset_str (die->sect_off),
14850 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14851 return 0;
14852 }
14853 align = val;
14854 }
14855 else
14856 align = DW_UNSND (attr);
14857
14858 if (align == 0)
14859 {
14860 complaint (_("DW_AT_alignment value must not be zero"
14861 " - DIE at %s [in module %s]"),
14862 sect_offset_str (die->sect_off),
14863 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14864 return 0;
14865 }
14866 if ((align & (align - 1)) != 0)
14867 {
14868 complaint (_("DW_AT_alignment value must be a power of 2"
14869 " - DIE at %s [in module %s]"),
14870 sect_offset_str (die->sect_off),
14871 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14872 return 0;
14873 }
14874
14875 return align;
14876 }
14877
14878 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14879 the alignment for TYPE. */
14880
14881 static void
14882 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14883 struct type *type)
14884 {
14885 if (!set_type_align (type, get_alignment (cu, die)))
14886 complaint (_("DW_AT_alignment value too large"
14887 " - DIE at %s [in module %s]"),
14888 sect_offset_str (die->sect_off),
14889 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14890 }
14891
14892 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14893 constant for a type, according to DWARF5 spec, Table 5.5. */
14894
14895 static bool
14896 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14897 {
14898 switch (value)
14899 {
14900 case DW_CC_normal:
14901 case DW_CC_pass_by_reference:
14902 case DW_CC_pass_by_value:
14903 return true;
14904
14905 default:
14906 complaint (_("unrecognized DW_AT_calling_convention value "
14907 "(%s) for a type"), pulongest (value));
14908 return false;
14909 }
14910 }
14911
14912 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14913 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14914 also according to GNU-specific values (see include/dwarf2.h). */
14915
14916 static bool
14917 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14918 {
14919 switch (value)
14920 {
14921 case DW_CC_normal:
14922 case DW_CC_program:
14923 case DW_CC_nocall:
14924 return true;
14925
14926 case DW_CC_GNU_renesas_sh:
14927 case DW_CC_GNU_borland_fastcall_i386:
14928 case DW_CC_GDB_IBM_OpenCL:
14929 return true;
14930
14931 default:
14932 complaint (_("unrecognized DW_AT_calling_convention value "
14933 "(%s) for a subroutine"), pulongest (value));
14934 return false;
14935 }
14936 }
14937
14938 /* Called when we find the DIE that starts a structure or union scope
14939 (definition) to create a type for the structure or union. Fill in
14940 the type's name and general properties; the members will not be
14941 processed until process_structure_scope. A symbol table entry for
14942 the type will also not be done until process_structure_scope (assuming
14943 the type has a name).
14944
14945 NOTE: we need to call these functions regardless of whether or not the
14946 DIE has a DW_AT_name attribute, since it might be an anonymous
14947 structure or union. This gets the type entered into our set of
14948 user defined types. */
14949
14950 static struct type *
14951 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14952 {
14953 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14954 struct type *type;
14955 struct attribute *attr;
14956 const char *name;
14957
14958 /* If the definition of this type lives in .debug_types, read that type.
14959 Don't follow DW_AT_specification though, that will take us back up
14960 the chain and we want to go down. */
14961 attr = die->attr (DW_AT_signature);
14962 if (attr != nullptr)
14963 {
14964 type = get_DW_AT_signature_type (die, attr, cu);
14965
14966 /* The type's CU may not be the same as CU.
14967 Ensure TYPE is recorded with CU in die_type_hash. */
14968 return set_die_type (die, type, cu);
14969 }
14970
14971 type = alloc_type (objfile);
14972 INIT_CPLUS_SPECIFIC (type);
14973
14974 name = dwarf2_name (die, cu);
14975 if (name != NULL)
14976 {
14977 if (cu->language == language_cplus
14978 || cu->language == language_d
14979 || cu->language == language_rust)
14980 {
14981 const char *full_name = dwarf2_full_name (name, die, cu);
14982
14983 /* dwarf2_full_name might have already finished building the DIE's
14984 type. If so, there is no need to continue. */
14985 if (get_die_type (die, cu) != NULL)
14986 return get_die_type (die, cu);
14987
14988 TYPE_NAME (type) = full_name;
14989 }
14990 else
14991 {
14992 /* The name is already allocated along with this objfile, so
14993 we don't need to duplicate it for the type. */
14994 TYPE_NAME (type) = name;
14995 }
14996 }
14997
14998 if (die->tag == DW_TAG_structure_type)
14999 {
15000 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15001 }
15002 else if (die->tag == DW_TAG_union_type)
15003 {
15004 TYPE_CODE (type) = TYPE_CODE_UNION;
15005 }
15006 else if (die->tag == DW_TAG_variant_part)
15007 {
15008 TYPE_CODE (type) = TYPE_CODE_UNION;
15009 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15010 }
15011 else
15012 {
15013 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15014 }
15015
15016 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15017 TYPE_DECLARED_CLASS (type) = 1;
15018
15019 /* Store the calling convention in the type if it's available in
15020 the die. Otherwise the calling convention remains set to
15021 the default value DW_CC_normal. */
15022 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15023 if (attr != nullptr
15024 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15025 {
15026 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15027 TYPE_CPLUS_CALLING_CONVENTION (type)
15028 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15029 }
15030
15031 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15032 if (attr != nullptr)
15033 {
15034 if (attr->form_is_constant ())
15035 TYPE_LENGTH (type) = DW_UNSND (attr);
15036 else
15037 {
15038 /* For the moment, dynamic type sizes are not supported
15039 by GDB's struct type. The actual size is determined
15040 on-demand when resolving the type of a given object,
15041 so set the type's length to zero for now. Otherwise,
15042 we record an expression as the length, and that expression
15043 could lead to a very large value, which could eventually
15044 lead to us trying to allocate that much memory when creating
15045 a value of that type. */
15046 TYPE_LENGTH (type) = 0;
15047 }
15048 }
15049 else
15050 {
15051 TYPE_LENGTH (type) = 0;
15052 }
15053
15054 maybe_set_alignment (cu, die, type);
15055
15056 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15057 {
15058 /* ICC<14 does not output the required DW_AT_declaration on
15059 incomplete types, but gives them a size of zero. */
15060 TYPE_STUB (type) = 1;
15061 }
15062 else
15063 TYPE_STUB_SUPPORTED (type) = 1;
15064
15065 if (die_is_declaration (die, cu))
15066 TYPE_STUB (type) = 1;
15067 else if (attr == NULL && die->child == NULL
15068 && producer_is_realview (cu->producer))
15069 /* RealView does not output the required DW_AT_declaration
15070 on incomplete types. */
15071 TYPE_STUB (type) = 1;
15072
15073 /* We need to add the type field to the die immediately so we don't
15074 infinitely recurse when dealing with pointers to the structure
15075 type within the structure itself. */
15076 set_die_type (die, type, cu);
15077
15078 /* set_die_type should be already done. */
15079 set_descriptive_type (type, die, cu);
15080
15081 return type;
15082 }
15083
15084 /* A helper for process_structure_scope that handles a single member
15085 DIE. */
15086
15087 static void
15088 handle_struct_member_die (struct die_info *child_die, struct type *type,
15089 struct field_info *fi,
15090 std::vector<struct symbol *> *template_args,
15091 struct dwarf2_cu *cu)
15092 {
15093 if (child_die->tag == DW_TAG_member
15094 || child_die->tag == DW_TAG_variable
15095 || child_die->tag == DW_TAG_variant_part)
15096 {
15097 /* NOTE: carlton/2002-11-05: A C++ static data member
15098 should be a DW_TAG_member that is a declaration, but
15099 all versions of G++ as of this writing (so through at
15100 least 3.2.1) incorrectly generate DW_TAG_variable
15101 tags for them instead. */
15102 dwarf2_add_field (fi, child_die, cu);
15103 }
15104 else if (child_die->tag == DW_TAG_subprogram)
15105 {
15106 /* Rust doesn't have member functions in the C++ sense.
15107 However, it does emit ordinary functions as children
15108 of a struct DIE. */
15109 if (cu->language == language_rust)
15110 read_func_scope (child_die, cu);
15111 else
15112 {
15113 /* C++ member function. */
15114 dwarf2_add_member_fn (fi, child_die, type, cu);
15115 }
15116 }
15117 else if (child_die->tag == DW_TAG_inheritance)
15118 {
15119 /* C++ base class field. */
15120 dwarf2_add_field (fi, child_die, cu);
15121 }
15122 else if (type_can_define_types (child_die))
15123 dwarf2_add_type_defn (fi, child_die, cu);
15124 else if (child_die->tag == DW_TAG_template_type_param
15125 || child_die->tag == DW_TAG_template_value_param)
15126 {
15127 struct symbol *arg = new_symbol (child_die, NULL, cu);
15128
15129 if (arg != NULL)
15130 template_args->push_back (arg);
15131 }
15132 else if (child_die->tag == DW_TAG_variant)
15133 {
15134 /* In a variant we want to get the discriminant and also add a
15135 field for our sole member child. */
15136 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15137
15138 for (die_info *variant_child = child_die->child;
15139 variant_child != NULL;
15140 variant_child = variant_child->sibling)
15141 {
15142 if (variant_child->tag == DW_TAG_member)
15143 {
15144 handle_struct_member_die (variant_child, type, fi,
15145 template_args, cu);
15146 /* Only handle the one. */
15147 break;
15148 }
15149 }
15150
15151 /* We don't handle this but we might as well report it if we see
15152 it. */
15153 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15154 complaint (_("DW_AT_discr_list is not supported yet"
15155 " - DIE at %s [in module %s]"),
15156 sect_offset_str (child_die->sect_off),
15157 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15158
15159 /* The first field was just added, so we can stash the
15160 discriminant there. */
15161 gdb_assert (!fi->fields.empty ());
15162 if (discr == NULL)
15163 fi->fields.back ().variant.default_branch = true;
15164 else
15165 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15166 }
15167 }
15168
15169 /* Finish creating a structure or union type, including filling in
15170 its members and creating a symbol for it. */
15171
15172 static void
15173 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15174 {
15175 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15176 struct die_info *child_die;
15177 struct type *type;
15178
15179 type = get_die_type (die, cu);
15180 if (type == NULL)
15181 type = read_structure_type (die, cu);
15182
15183 /* When reading a DW_TAG_variant_part, we need to notice when we
15184 read the discriminant member, so we can record it later in the
15185 discriminant_info. */
15186 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15187 sect_offset discr_offset {};
15188 bool has_template_parameters = false;
15189
15190 if (is_variant_part)
15191 {
15192 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15193 if (discr == NULL)
15194 {
15195 /* Maybe it's a univariant form, an extension we support.
15196 In this case arrange not to check the offset. */
15197 is_variant_part = false;
15198 }
15199 else if (discr->form_is_ref ())
15200 {
15201 struct dwarf2_cu *target_cu = cu;
15202 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15203
15204 discr_offset = target_die->sect_off;
15205 }
15206 else
15207 {
15208 complaint (_("DW_AT_discr does not have DIE reference form"
15209 " - DIE at %s [in module %s]"),
15210 sect_offset_str (die->sect_off),
15211 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15212 is_variant_part = false;
15213 }
15214 }
15215
15216 if (die->child != NULL && ! die_is_declaration (die, cu))
15217 {
15218 struct field_info fi;
15219 std::vector<struct symbol *> template_args;
15220
15221 child_die = die->child;
15222
15223 while (child_die && child_die->tag)
15224 {
15225 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15226
15227 if (is_variant_part && discr_offset == child_die->sect_off)
15228 fi.fields.back ().variant.is_discriminant = true;
15229
15230 child_die = child_die->sibling;
15231 }
15232
15233 /* Attach template arguments to type. */
15234 if (!template_args.empty ())
15235 {
15236 has_template_parameters = true;
15237 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15238 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15239 TYPE_TEMPLATE_ARGUMENTS (type)
15240 = XOBNEWVEC (&objfile->objfile_obstack,
15241 struct symbol *,
15242 TYPE_N_TEMPLATE_ARGUMENTS (type));
15243 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15244 template_args.data (),
15245 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15246 * sizeof (struct symbol *)));
15247 }
15248
15249 /* Attach fields and member functions to the type. */
15250 if (fi.nfields () > 0)
15251 dwarf2_attach_fields_to_type (&fi, type, cu);
15252 if (!fi.fnfieldlists.empty ())
15253 {
15254 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15255
15256 /* Get the type which refers to the base class (possibly this
15257 class itself) which contains the vtable pointer for the current
15258 class from the DW_AT_containing_type attribute. This use of
15259 DW_AT_containing_type is a GNU extension. */
15260
15261 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15262 {
15263 struct type *t = die_containing_type (die, cu);
15264
15265 set_type_vptr_basetype (type, t);
15266 if (type == t)
15267 {
15268 int i;
15269
15270 /* Our own class provides vtbl ptr. */
15271 for (i = TYPE_NFIELDS (t) - 1;
15272 i >= TYPE_N_BASECLASSES (t);
15273 --i)
15274 {
15275 const char *fieldname = TYPE_FIELD_NAME (t, i);
15276
15277 if (is_vtable_name (fieldname, cu))
15278 {
15279 set_type_vptr_fieldno (type, i);
15280 break;
15281 }
15282 }
15283
15284 /* Complain if virtual function table field not found. */
15285 if (i < TYPE_N_BASECLASSES (t))
15286 complaint (_("virtual function table pointer "
15287 "not found when defining class '%s'"),
15288 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15289 }
15290 else
15291 {
15292 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15293 }
15294 }
15295 else if (cu->producer
15296 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15297 {
15298 /* The IBM XLC compiler does not provide direct indication
15299 of the containing type, but the vtable pointer is
15300 always named __vfp. */
15301
15302 int i;
15303
15304 for (i = TYPE_NFIELDS (type) - 1;
15305 i >= TYPE_N_BASECLASSES (type);
15306 --i)
15307 {
15308 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15309 {
15310 set_type_vptr_fieldno (type, i);
15311 set_type_vptr_basetype (type, type);
15312 break;
15313 }
15314 }
15315 }
15316 }
15317
15318 /* Copy fi.typedef_field_list linked list elements content into the
15319 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15320 if (!fi.typedef_field_list.empty ())
15321 {
15322 int count = fi.typedef_field_list.size ();
15323
15324 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15325 TYPE_TYPEDEF_FIELD_ARRAY (type)
15326 = ((struct decl_field *)
15327 TYPE_ALLOC (type,
15328 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15329 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15330
15331 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15332 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15333 }
15334
15335 /* Copy fi.nested_types_list linked list elements content into the
15336 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15337 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15338 {
15339 int count = fi.nested_types_list.size ();
15340
15341 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15342 TYPE_NESTED_TYPES_ARRAY (type)
15343 = ((struct decl_field *)
15344 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15345 TYPE_NESTED_TYPES_COUNT (type) = count;
15346
15347 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15348 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15349 }
15350 }
15351
15352 quirk_gcc_member_function_pointer (type, objfile);
15353 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15354 cu->rust_unions.push_back (type);
15355
15356 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15357 snapshots) has been known to create a die giving a declaration
15358 for a class that has, as a child, a die giving a definition for a
15359 nested class. So we have to process our children even if the
15360 current die is a declaration. Normally, of course, a declaration
15361 won't have any children at all. */
15362
15363 child_die = die->child;
15364
15365 while (child_die != NULL && child_die->tag)
15366 {
15367 if (child_die->tag == DW_TAG_member
15368 || child_die->tag == DW_TAG_variable
15369 || child_die->tag == DW_TAG_inheritance
15370 || child_die->tag == DW_TAG_template_value_param
15371 || child_die->tag == DW_TAG_template_type_param)
15372 {
15373 /* Do nothing. */
15374 }
15375 else
15376 process_die (child_die, cu);
15377
15378 child_die = child_die->sibling;
15379 }
15380
15381 /* Do not consider external references. According to the DWARF standard,
15382 these DIEs are identified by the fact that they have no byte_size
15383 attribute, and a declaration attribute. */
15384 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15385 || !die_is_declaration (die, cu))
15386 {
15387 struct symbol *sym = new_symbol (die, type, cu);
15388
15389 if (has_template_parameters)
15390 {
15391 struct symtab *symtab;
15392 if (sym != nullptr)
15393 symtab = symbol_symtab (sym);
15394 else if (cu->line_header != nullptr)
15395 {
15396 /* Any related symtab will do. */
15397 symtab
15398 = cu->line_header->file_names ()[0].symtab;
15399 }
15400 else
15401 {
15402 symtab = nullptr;
15403 complaint (_("could not find suitable "
15404 "symtab for template parameter"
15405 " - DIE at %s [in module %s]"),
15406 sect_offset_str (die->sect_off),
15407 objfile_name (objfile));
15408 }
15409
15410 if (symtab != nullptr)
15411 {
15412 /* Make sure that the symtab is set on the new symbols.
15413 Even though they don't appear in this symtab directly,
15414 other parts of gdb assume that symbols do, and this is
15415 reasonably true. */
15416 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15417 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15418 }
15419 }
15420 }
15421 }
15422
15423 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15424 update TYPE using some information only available in DIE's children. */
15425
15426 static void
15427 update_enumeration_type_from_children (struct die_info *die,
15428 struct type *type,
15429 struct dwarf2_cu *cu)
15430 {
15431 struct die_info *child_die;
15432 int unsigned_enum = 1;
15433 int flag_enum = 1;
15434
15435 auto_obstack obstack;
15436
15437 for (child_die = die->child;
15438 child_die != NULL && child_die->tag;
15439 child_die = child_die->sibling)
15440 {
15441 struct attribute *attr;
15442 LONGEST value;
15443 const gdb_byte *bytes;
15444 struct dwarf2_locexpr_baton *baton;
15445 const char *name;
15446
15447 if (child_die->tag != DW_TAG_enumerator)
15448 continue;
15449
15450 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15451 if (attr == NULL)
15452 continue;
15453
15454 name = dwarf2_name (child_die, cu);
15455 if (name == NULL)
15456 name = "<anonymous enumerator>";
15457
15458 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15459 &value, &bytes, &baton);
15460 if (value < 0)
15461 {
15462 unsigned_enum = 0;
15463 flag_enum = 0;
15464 }
15465 else
15466 {
15467 if (count_one_bits_ll (value) >= 2)
15468 flag_enum = 0;
15469 }
15470
15471 /* If we already know that the enum type is neither unsigned, nor
15472 a flag type, no need to look at the rest of the enumerates. */
15473 if (!unsigned_enum && !flag_enum)
15474 break;
15475 }
15476
15477 if (unsigned_enum)
15478 TYPE_UNSIGNED (type) = 1;
15479 if (flag_enum)
15480 TYPE_FLAG_ENUM (type) = 1;
15481 }
15482
15483 /* Given a DW_AT_enumeration_type die, set its type. We do not
15484 complete the type's fields yet, or create any symbols. */
15485
15486 static struct type *
15487 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15488 {
15489 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15490 struct type *type;
15491 struct attribute *attr;
15492 const char *name;
15493
15494 /* If the definition of this type lives in .debug_types, read that type.
15495 Don't follow DW_AT_specification though, that will take us back up
15496 the chain and we want to go down. */
15497 attr = die->attr (DW_AT_signature);
15498 if (attr != nullptr)
15499 {
15500 type = get_DW_AT_signature_type (die, attr, cu);
15501
15502 /* The type's CU may not be the same as CU.
15503 Ensure TYPE is recorded with CU in die_type_hash. */
15504 return set_die_type (die, type, cu);
15505 }
15506
15507 type = alloc_type (objfile);
15508
15509 TYPE_CODE (type) = TYPE_CODE_ENUM;
15510 name = dwarf2_full_name (NULL, die, cu);
15511 if (name != NULL)
15512 TYPE_NAME (type) = name;
15513
15514 attr = dwarf2_attr (die, DW_AT_type, cu);
15515 if (attr != NULL)
15516 {
15517 struct type *underlying_type = die_type (die, cu);
15518
15519 TYPE_TARGET_TYPE (type) = underlying_type;
15520 }
15521
15522 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15523 if (attr != nullptr)
15524 {
15525 TYPE_LENGTH (type) = DW_UNSND (attr);
15526 }
15527 else
15528 {
15529 TYPE_LENGTH (type) = 0;
15530 }
15531
15532 maybe_set_alignment (cu, die, type);
15533
15534 /* The enumeration DIE can be incomplete. In Ada, any type can be
15535 declared as private in the package spec, and then defined only
15536 inside the package body. Such types are known as Taft Amendment
15537 Types. When another package uses such a type, an incomplete DIE
15538 may be generated by the compiler. */
15539 if (die_is_declaration (die, cu))
15540 TYPE_STUB (type) = 1;
15541
15542 /* Finish the creation of this type by using the enum's children.
15543 We must call this even when the underlying type has been provided
15544 so that we can determine if we're looking at a "flag" enum. */
15545 update_enumeration_type_from_children (die, type, cu);
15546
15547 /* If this type has an underlying type that is not a stub, then we
15548 may use its attributes. We always use the "unsigned" attribute
15549 in this situation, because ordinarily we guess whether the type
15550 is unsigned -- but the guess can be wrong and the underlying type
15551 can tell us the reality. However, we defer to a local size
15552 attribute if one exists, because this lets the compiler override
15553 the underlying type if needed. */
15554 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15555 {
15556 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15557 if (TYPE_LENGTH (type) == 0)
15558 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15559 if (TYPE_RAW_ALIGN (type) == 0
15560 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15561 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15562 }
15563
15564 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15565
15566 return set_die_type (die, type, cu);
15567 }
15568
15569 /* Given a pointer to a die which begins an enumeration, process all
15570 the dies that define the members of the enumeration, and create the
15571 symbol for the enumeration type.
15572
15573 NOTE: We reverse the order of the element list. */
15574
15575 static void
15576 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15577 {
15578 struct type *this_type;
15579
15580 this_type = get_die_type (die, cu);
15581 if (this_type == NULL)
15582 this_type = read_enumeration_type (die, cu);
15583
15584 if (die->child != NULL)
15585 {
15586 struct die_info *child_die;
15587 struct symbol *sym;
15588 std::vector<struct field> fields;
15589 const char *name;
15590
15591 child_die = die->child;
15592 while (child_die && child_die->tag)
15593 {
15594 if (child_die->tag != DW_TAG_enumerator)
15595 {
15596 process_die (child_die, cu);
15597 }
15598 else
15599 {
15600 name = dwarf2_name (child_die, cu);
15601 if (name)
15602 {
15603 sym = new_symbol (child_die, this_type, cu);
15604
15605 fields.emplace_back ();
15606 struct field &field = fields.back ();
15607
15608 FIELD_NAME (field) = sym->linkage_name ();
15609 FIELD_TYPE (field) = NULL;
15610 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15611 FIELD_BITSIZE (field) = 0;
15612 }
15613 }
15614
15615 child_die = child_die->sibling;
15616 }
15617
15618 if (!fields.empty ())
15619 {
15620 TYPE_NFIELDS (this_type) = fields.size ();
15621 TYPE_FIELDS (this_type) = (struct field *)
15622 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15623 memcpy (TYPE_FIELDS (this_type), fields.data (),
15624 sizeof (struct field) * fields.size ());
15625 }
15626 }
15627
15628 /* If we are reading an enum from a .debug_types unit, and the enum
15629 is a declaration, and the enum is not the signatured type in the
15630 unit, then we do not want to add a symbol for it. Adding a
15631 symbol would in some cases obscure the true definition of the
15632 enum, giving users an incomplete type when the definition is
15633 actually available. Note that we do not want to do this for all
15634 enums which are just declarations, because C++0x allows forward
15635 enum declarations. */
15636 if (cu->per_cu->is_debug_types
15637 && die_is_declaration (die, cu))
15638 {
15639 struct signatured_type *sig_type;
15640
15641 sig_type = (struct signatured_type *) cu->per_cu;
15642 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15643 if (sig_type->type_offset_in_section != die->sect_off)
15644 return;
15645 }
15646
15647 new_symbol (die, this_type, cu);
15648 }
15649
15650 /* Extract all information from a DW_TAG_array_type DIE and put it in
15651 the DIE's type field. For now, this only handles one dimensional
15652 arrays. */
15653
15654 static struct type *
15655 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15656 {
15657 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15658 struct die_info *child_die;
15659 struct type *type;
15660 struct type *element_type, *range_type, *index_type;
15661 struct attribute *attr;
15662 const char *name;
15663 struct dynamic_prop *byte_stride_prop = NULL;
15664 unsigned int bit_stride = 0;
15665
15666 element_type = die_type (die, cu);
15667
15668 /* The die_type call above may have already set the type for this DIE. */
15669 type = get_die_type (die, cu);
15670 if (type)
15671 return type;
15672
15673 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15674 if (attr != NULL)
15675 {
15676 int stride_ok;
15677 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15678
15679 byte_stride_prop
15680 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15681 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15682 prop_type);
15683 if (!stride_ok)
15684 {
15685 complaint (_("unable to read array DW_AT_byte_stride "
15686 " - DIE at %s [in module %s]"),
15687 sect_offset_str (die->sect_off),
15688 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15689 /* Ignore this attribute. We will likely not be able to print
15690 arrays of this type correctly, but there is little we can do
15691 to help if we cannot read the attribute's value. */
15692 byte_stride_prop = NULL;
15693 }
15694 }
15695
15696 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15697 if (attr != NULL)
15698 bit_stride = DW_UNSND (attr);
15699
15700 /* Irix 6.2 native cc creates array types without children for
15701 arrays with unspecified length. */
15702 if (die->child == NULL)
15703 {
15704 index_type = objfile_type (objfile)->builtin_int;
15705 range_type = create_static_range_type (NULL, index_type, 0, -1);
15706 type = create_array_type_with_stride (NULL, element_type, range_type,
15707 byte_stride_prop, bit_stride);
15708 return set_die_type (die, type, cu);
15709 }
15710
15711 std::vector<struct type *> range_types;
15712 child_die = die->child;
15713 while (child_die && child_die->tag)
15714 {
15715 if (child_die->tag == DW_TAG_subrange_type)
15716 {
15717 struct type *child_type = read_type_die (child_die, cu);
15718
15719 if (child_type != NULL)
15720 {
15721 /* The range type was succesfully read. Save it for the
15722 array type creation. */
15723 range_types.push_back (child_type);
15724 }
15725 }
15726 child_die = child_die->sibling;
15727 }
15728
15729 /* Dwarf2 dimensions are output from left to right, create the
15730 necessary array types in backwards order. */
15731
15732 type = element_type;
15733
15734 if (read_array_order (die, cu) == DW_ORD_col_major)
15735 {
15736 int i = 0;
15737
15738 while (i < range_types.size ())
15739 type = create_array_type_with_stride (NULL, type, range_types[i++],
15740 byte_stride_prop, bit_stride);
15741 }
15742 else
15743 {
15744 size_t ndim = range_types.size ();
15745 while (ndim-- > 0)
15746 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15747 byte_stride_prop, bit_stride);
15748 }
15749
15750 /* Understand Dwarf2 support for vector types (like they occur on
15751 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15752 array type. This is not part of the Dwarf2/3 standard yet, but a
15753 custom vendor extension. The main difference between a regular
15754 array and the vector variant is that vectors are passed by value
15755 to functions. */
15756 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15757 if (attr != nullptr)
15758 make_vector_type (type);
15759
15760 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15761 implementation may choose to implement triple vectors using this
15762 attribute. */
15763 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15764 if (attr != nullptr)
15765 {
15766 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15767 TYPE_LENGTH (type) = DW_UNSND (attr);
15768 else
15769 complaint (_("DW_AT_byte_size for array type smaller "
15770 "than the total size of elements"));
15771 }
15772
15773 name = dwarf2_name (die, cu);
15774 if (name)
15775 TYPE_NAME (type) = name;
15776
15777 maybe_set_alignment (cu, die, type);
15778
15779 /* Install the type in the die. */
15780 set_die_type (die, type, cu);
15781
15782 /* set_die_type should be already done. */
15783 set_descriptive_type (type, die, cu);
15784
15785 return type;
15786 }
15787
15788 static enum dwarf_array_dim_ordering
15789 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15790 {
15791 struct attribute *attr;
15792
15793 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15794
15795 if (attr != nullptr)
15796 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15797
15798 /* GNU F77 is a special case, as at 08/2004 array type info is the
15799 opposite order to the dwarf2 specification, but data is still
15800 laid out as per normal fortran.
15801
15802 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15803 version checking. */
15804
15805 if (cu->language == language_fortran
15806 && cu->producer && strstr (cu->producer, "GNU F77"))
15807 {
15808 return DW_ORD_row_major;
15809 }
15810
15811 switch (cu->language_defn->la_array_ordering)
15812 {
15813 case array_column_major:
15814 return DW_ORD_col_major;
15815 case array_row_major:
15816 default:
15817 return DW_ORD_row_major;
15818 };
15819 }
15820
15821 /* Extract all information from a DW_TAG_set_type DIE and put it in
15822 the DIE's type field. */
15823
15824 static struct type *
15825 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15826 {
15827 struct type *domain_type, *set_type;
15828 struct attribute *attr;
15829
15830 domain_type = die_type (die, cu);
15831
15832 /* The die_type call above may have already set the type for this DIE. */
15833 set_type = get_die_type (die, cu);
15834 if (set_type)
15835 return set_type;
15836
15837 set_type = create_set_type (NULL, domain_type);
15838
15839 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15840 if (attr != nullptr)
15841 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15842
15843 maybe_set_alignment (cu, die, set_type);
15844
15845 return set_die_type (die, set_type, cu);
15846 }
15847
15848 /* A helper for read_common_block that creates a locexpr baton.
15849 SYM is the symbol which we are marking as computed.
15850 COMMON_DIE is the DIE for the common block.
15851 COMMON_LOC is the location expression attribute for the common
15852 block itself.
15853 MEMBER_LOC is the location expression attribute for the particular
15854 member of the common block that we are processing.
15855 CU is the CU from which the above come. */
15856
15857 static void
15858 mark_common_block_symbol_computed (struct symbol *sym,
15859 struct die_info *common_die,
15860 struct attribute *common_loc,
15861 struct attribute *member_loc,
15862 struct dwarf2_cu *cu)
15863 {
15864 struct dwarf2_per_objfile *dwarf2_per_objfile
15865 = cu->per_cu->dwarf2_per_objfile;
15866 struct objfile *objfile = dwarf2_per_objfile->objfile;
15867 struct dwarf2_locexpr_baton *baton;
15868 gdb_byte *ptr;
15869 unsigned int cu_off;
15870 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15871 LONGEST offset = 0;
15872
15873 gdb_assert (common_loc && member_loc);
15874 gdb_assert (common_loc->form_is_block ());
15875 gdb_assert (member_loc->form_is_block ()
15876 || member_loc->form_is_constant ());
15877
15878 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15879 baton->per_cu = cu->per_cu;
15880 gdb_assert (baton->per_cu);
15881
15882 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15883
15884 if (member_loc->form_is_constant ())
15885 {
15886 offset = member_loc->constant_value (0);
15887 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15888 }
15889 else
15890 baton->size += DW_BLOCK (member_loc)->size;
15891
15892 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15893 baton->data = ptr;
15894
15895 *ptr++ = DW_OP_call4;
15896 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15897 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15898 ptr += 4;
15899
15900 if (member_loc->form_is_constant ())
15901 {
15902 *ptr++ = DW_OP_addr;
15903 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15904 ptr += cu->header.addr_size;
15905 }
15906 else
15907 {
15908 /* We have to copy the data here, because DW_OP_call4 will only
15909 use a DW_AT_location attribute. */
15910 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15911 ptr += DW_BLOCK (member_loc)->size;
15912 }
15913
15914 *ptr++ = DW_OP_plus;
15915 gdb_assert (ptr - baton->data == baton->size);
15916
15917 SYMBOL_LOCATION_BATON (sym) = baton;
15918 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15919 }
15920
15921 /* Create appropriate locally-scoped variables for all the
15922 DW_TAG_common_block entries. Also create a struct common_block
15923 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15924 is used to separate the common blocks name namespace from regular
15925 variable names. */
15926
15927 static void
15928 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15929 {
15930 struct attribute *attr;
15931
15932 attr = dwarf2_attr (die, DW_AT_location, cu);
15933 if (attr != nullptr)
15934 {
15935 /* Support the .debug_loc offsets. */
15936 if (attr->form_is_block ())
15937 {
15938 /* Ok. */
15939 }
15940 else if (attr->form_is_section_offset ())
15941 {
15942 dwarf2_complex_location_expr_complaint ();
15943 attr = NULL;
15944 }
15945 else
15946 {
15947 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15948 "common block member");
15949 attr = NULL;
15950 }
15951 }
15952
15953 if (die->child != NULL)
15954 {
15955 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15956 struct die_info *child_die;
15957 size_t n_entries = 0, size;
15958 struct common_block *common_block;
15959 struct symbol *sym;
15960
15961 for (child_die = die->child;
15962 child_die && child_die->tag;
15963 child_die = child_die->sibling)
15964 ++n_entries;
15965
15966 size = (sizeof (struct common_block)
15967 + (n_entries - 1) * sizeof (struct symbol *));
15968 common_block
15969 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15970 size);
15971 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15972 common_block->n_entries = 0;
15973
15974 for (child_die = die->child;
15975 child_die && child_die->tag;
15976 child_die = child_die->sibling)
15977 {
15978 /* Create the symbol in the DW_TAG_common_block block in the current
15979 symbol scope. */
15980 sym = new_symbol (child_die, NULL, cu);
15981 if (sym != NULL)
15982 {
15983 struct attribute *member_loc;
15984
15985 common_block->contents[common_block->n_entries++] = sym;
15986
15987 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15988 cu);
15989 if (member_loc)
15990 {
15991 /* GDB has handled this for a long time, but it is
15992 not specified by DWARF. It seems to have been
15993 emitted by gfortran at least as recently as:
15994 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15995 complaint (_("Variable in common block has "
15996 "DW_AT_data_member_location "
15997 "- DIE at %s [in module %s]"),
15998 sect_offset_str (child_die->sect_off),
15999 objfile_name (objfile));
16000
16001 if (member_loc->form_is_section_offset ())
16002 dwarf2_complex_location_expr_complaint ();
16003 else if (member_loc->form_is_constant ()
16004 || member_loc->form_is_block ())
16005 {
16006 if (attr != nullptr)
16007 mark_common_block_symbol_computed (sym, die, attr,
16008 member_loc, cu);
16009 }
16010 else
16011 dwarf2_complex_location_expr_complaint ();
16012 }
16013 }
16014 }
16015
16016 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16017 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16018 }
16019 }
16020
16021 /* Create a type for a C++ namespace. */
16022
16023 static struct type *
16024 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16025 {
16026 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16027 const char *previous_prefix, *name;
16028 int is_anonymous;
16029 struct type *type;
16030
16031 /* For extensions, reuse the type of the original namespace. */
16032 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16033 {
16034 struct die_info *ext_die;
16035 struct dwarf2_cu *ext_cu = cu;
16036
16037 ext_die = dwarf2_extension (die, &ext_cu);
16038 type = read_type_die (ext_die, ext_cu);
16039
16040 /* EXT_CU may not be the same as CU.
16041 Ensure TYPE is recorded with CU in die_type_hash. */
16042 return set_die_type (die, type, cu);
16043 }
16044
16045 name = namespace_name (die, &is_anonymous, cu);
16046
16047 /* Now build the name of the current namespace. */
16048
16049 previous_prefix = determine_prefix (die, cu);
16050 if (previous_prefix[0] != '\0')
16051 name = typename_concat (&objfile->objfile_obstack,
16052 previous_prefix, name, 0, cu);
16053
16054 /* Create the type. */
16055 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16056
16057 return set_die_type (die, type, cu);
16058 }
16059
16060 /* Read a namespace scope. */
16061
16062 static void
16063 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16064 {
16065 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16066 int is_anonymous;
16067
16068 /* Add a symbol associated to this if we haven't seen the namespace
16069 before. Also, add a using directive if it's an anonymous
16070 namespace. */
16071
16072 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16073 {
16074 struct type *type;
16075
16076 type = read_type_die (die, cu);
16077 new_symbol (die, type, cu);
16078
16079 namespace_name (die, &is_anonymous, cu);
16080 if (is_anonymous)
16081 {
16082 const char *previous_prefix = determine_prefix (die, cu);
16083
16084 std::vector<const char *> excludes;
16085 add_using_directive (using_directives (cu),
16086 previous_prefix, TYPE_NAME (type), NULL,
16087 NULL, excludes, 0, &objfile->objfile_obstack);
16088 }
16089 }
16090
16091 if (die->child != NULL)
16092 {
16093 struct die_info *child_die = die->child;
16094
16095 while (child_die && child_die->tag)
16096 {
16097 process_die (child_die, cu);
16098 child_die = child_die->sibling;
16099 }
16100 }
16101 }
16102
16103 /* Read a Fortran module as type. This DIE can be only a declaration used for
16104 imported module. Still we need that type as local Fortran "use ... only"
16105 declaration imports depend on the created type in determine_prefix. */
16106
16107 static struct type *
16108 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16109 {
16110 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16111 const char *module_name;
16112 struct type *type;
16113
16114 module_name = dwarf2_name (die, cu);
16115 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16116
16117 return set_die_type (die, type, cu);
16118 }
16119
16120 /* Read a Fortran module. */
16121
16122 static void
16123 read_module (struct die_info *die, struct dwarf2_cu *cu)
16124 {
16125 struct die_info *child_die = die->child;
16126 struct type *type;
16127
16128 type = read_type_die (die, cu);
16129 new_symbol (die, type, cu);
16130
16131 while (child_die && child_die->tag)
16132 {
16133 process_die (child_die, cu);
16134 child_die = child_die->sibling;
16135 }
16136 }
16137
16138 /* Return the name of the namespace represented by DIE. Set
16139 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16140 namespace. */
16141
16142 static const char *
16143 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16144 {
16145 struct die_info *current_die;
16146 const char *name = NULL;
16147
16148 /* Loop through the extensions until we find a name. */
16149
16150 for (current_die = die;
16151 current_die != NULL;
16152 current_die = dwarf2_extension (die, &cu))
16153 {
16154 /* We don't use dwarf2_name here so that we can detect the absence
16155 of a name -> anonymous namespace. */
16156 name = dwarf2_string_attr (die, DW_AT_name, cu);
16157
16158 if (name != NULL)
16159 break;
16160 }
16161
16162 /* Is it an anonymous namespace? */
16163
16164 *is_anonymous = (name == NULL);
16165 if (*is_anonymous)
16166 name = CP_ANONYMOUS_NAMESPACE_STR;
16167
16168 return name;
16169 }
16170
16171 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16172 the user defined type vector. */
16173
16174 static struct type *
16175 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16176 {
16177 struct gdbarch *gdbarch
16178 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16179 struct comp_unit_head *cu_header = &cu->header;
16180 struct type *type;
16181 struct attribute *attr_byte_size;
16182 struct attribute *attr_address_class;
16183 int byte_size, addr_class;
16184 struct type *target_type;
16185
16186 target_type = die_type (die, cu);
16187
16188 /* The die_type call above may have already set the type for this DIE. */
16189 type = get_die_type (die, cu);
16190 if (type)
16191 return type;
16192
16193 type = lookup_pointer_type (target_type);
16194
16195 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16196 if (attr_byte_size)
16197 byte_size = DW_UNSND (attr_byte_size);
16198 else
16199 byte_size = cu_header->addr_size;
16200
16201 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16202 if (attr_address_class)
16203 addr_class = DW_UNSND (attr_address_class);
16204 else
16205 addr_class = DW_ADDR_none;
16206
16207 ULONGEST alignment = get_alignment (cu, die);
16208
16209 /* If the pointer size, alignment, or address class is different
16210 than the default, create a type variant marked as such and set
16211 the length accordingly. */
16212 if (TYPE_LENGTH (type) != byte_size
16213 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16214 && alignment != TYPE_RAW_ALIGN (type))
16215 || addr_class != DW_ADDR_none)
16216 {
16217 if (gdbarch_address_class_type_flags_p (gdbarch))
16218 {
16219 int type_flags;
16220
16221 type_flags = gdbarch_address_class_type_flags
16222 (gdbarch, byte_size, addr_class);
16223 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16224 == 0);
16225 type = make_type_with_address_space (type, type_flags);
16226 }
16227 else if (TYPE_LENGTH (type) != byte_size)
16228 {
16229 complaint (_("invalid pointer size %d"), byte_size);
16230 }
16231 else if (TYPE_RAW_ALIGN (type) != alignment)
16232 {
16233 complaint (_("Invalid DW_AT_alignment"
16234 " - DIE at %s [in module %s]"),
16235 sect_offset_str (die->sect_off),
16236 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16237 }
16238 else
16239 {
16240 /* Should we also complain about unhandled address classes? */
16241 }
16242 }
16243
16244 TYPE_LENGTH (type) = byte_size;
16245 set_type_align (type, alignment);
16246 return set_die_type (die, type, cu);
16247 }
16248
16249 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16250 the user defined type vector. */
16251
16252 static struct type *
16253 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16254 {
16255 struct type *type;
16256 struct type *to_type;
16257 struct type *domain;
16258
16259 to_type = die_type (die, cu);
16260 domain = die_containing_type (die, cu);
16261
16262 /* The calls above may have already set the type for this DIE. */
16263 type = get_die_type (die, cu);
16264 if (type)
16265 return type;
16266
16267 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16268 type = lookup_methodptr_type (to_type);
16269 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16270 {
16271 struct type *new_type
16272 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16273
16274 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16275 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16276 TYPE_VARARGS (to_type));
16277 type = lookup_methodptr_type (new_type);
16278 }
16279 else
16280 type = lookup_memberptr_type (to_type, domain);
16281
16282 return set_die_type (die, type, cu);
16283 }
16284
16285 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16286 the user defined type vector. */
16287
16288 static struct type *
16289 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16290 enum type_code refcode)
16291 {
16292 struct comp_unit_head *cu_header = &cu->header;
16293 struct type *type, *target_type;
16294 struct attribute *attr;
16295
16296 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16297
16298 target_type = die_type (die, cu);
16299
16300 /* The die_type call above may have already set the type for this DIE. */
16301 type = get_die_type (die, cu);
16302 if (type)
16303 return type;
16304
16305 type = lookup_reference_type (target_type, refcode);
16306 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16307 if (attr != nullptr)
16308 {
16309 TYPE_LENGTH (type) = DW_UNSND (attr);
16310 }
16311 else
16312 {
16313 TYPE_LENGTH (type) = cu_header->addr_size;
16314 }
16315 maybe_set_alignment (cu, die, type);
16316 return set_die_type (die, type, cu);
16317 }
16318
16319 /* Add the given cv-qualifiers to the element type of the array. GCC
16320 outputs DWARF type qualifiers that apply to an array, not the
16321 element type. But GDB relies on the array element type to carry
16322 the cv-qualifiers. This mimics section 6.7.3 of the C99
16323 specification. */
16324
16325 static struct type *
16326 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16327 struct type *base_type, int cnst, int voltl)
16328 {
16329 struct type *el_type, *inner_array;
16330
16331 base_type = copy_type (base_type);
16332 inner_array = base_type;
16333
16334 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16335 {
16336 TYPE_TARGET_TYPE (inner_array) =
16337 copy_type (TYPE_TARGET_TYPE (inner_array));
16338 inner_array = TYPE_TARGET_TYPE (inner_array);
16339 }
16340
16341 el_type = TYPE_TARGET_TYPE (inner_array);
16342 cnst |= TYPE_CONST (el_type);
16343 voltl |= TYPE_VOLATILE (el_type);
16344 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16345
16346 return set_die_type (die, base_type, cu);
16347 }
16348
16349 static struct type *
16350 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16351 {
16352 struct type *base_type, *cv_type;
16353
16354 base_type = die_type (die, cu);
16355
16356 /* The die_type call above may have already set the type for this DIE. */
16357 cv_type = get_die_type (die, cu);
16358 if (cv_type)
16359 return cv_type;
16360
16361 /* In case the const qualifier is applied to an array type, the element type
16362 is so qualified, not the array type (section 6.7.3 of C99). */
16363 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16364 return add_array_cv_type (die, cu, base_type, 1, 0);
16365
16366 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16367 return set_die_type (die, cv_type, cu);
16368 }
16369
16370 static struct type *
16371 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16372 {
16373 struct type *base_type, *cv_type;
16374
16375 base_type = die_type (die, cu);
16376
16377 /* The die_type call above may have already set the type for this DIE. */
16378 cv_type = get_die_type (die, cu);
16379 if (cv_type)
16380 return cv_type;
16381
16382 /* In case the volatile qualifier is applied to an array type, the
16383 element type is so qualified, not the array type (section 6.7.3
16384 of C99). */
16385 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16386 return add_array_cv_type (die, cu, base_type, 0, 1);
16387
16388 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16389 return set_die_type (die, cv_type, cu);
16390 }
16391
16392 /* Handle DW_TAG_restrict_type. */
16393
16394 static struct type *
16395 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16396 {
16397 struct type *base_type, *cv_type;
16398
16399 base_type = die_type (die, cu);
16400
16401 /* The die_type call above may have already set the type for this DIE. */
16402 cv_type = get_die_type (die, cu);
16403 if (cv_type)
16404 return cv_type;
16405
16406 cv_type = make_restrict_type (base_type);
16407 return set_die_type (die, cv_type, cu);
16408 }
16409
16410 /* Handle DW_TAG_atomic_type. */
16411
16412 static struct type *
16413 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16414 {
16415 struct type *base_type, *cv_type;
16416
16417 base_type = die_type (die, cu);
16418
16419 /* The die_type call above may have already set the type for this DIE. */
16420 cv_type = get_die_type (die, cu);
16421 if (cv_type)
16422 return cv_type;
16423
16424 cv_type = make_atomic_type (base_type);
16425 return set_die_type (die, cv_type, cu);
16426 }
16427
16428 /* Extract all information from a DW_TAG_string_type DIE and add to
16429 the user defined type vector. It isn't really a user defined type,
16430 but it behaves like one, with other DIE's using an AT_user_def_type
16431 attribute to reference it. */
16432
16433 static struct type *
16434 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16435 {
16436 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16437 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16438 struct type *type, *range_type, *index_type, *char_type;
16439 struct attribute *attr;
16440 struct dynamic_prop prop;
16441 bool length_is_constant = true;
16442 LONGEST length;
16443
16444 /* There are a couple of places where bit sizes might be made use of
16445 when parsing a DW_TAG_string_type, however, no producer that we know
16446 of make use of these. Handling bit sizes that are a multiple of the
16447 byte size is easy enough, but what about other bit sizes? Lets deal
16448 with that problem when we have to. Warn about these attributes being
16449 unsupported, then parse the type and ignore them like we always
16450 have. */
16451 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16452 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16453 {
16454 static bool warning_printed = false;
16455 if (!warning_printed)
16456 {
16457 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16458 "currently supported on DW_TAG_string_type."));
16459 warning_printed = true;
16460 }
16461 }
16462
16463 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16464 if (attr != nullptr && !attr->form_is_constant ())
16465 {
16466 /* The string length describes the location at which the length of
16467 the string can be found. The size of the length field can be
16468 specified with one of the attributes below. */
16469 struct type *prop_type;
16470 struct attribute *len
16471 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16472 if (len == nullptr)
16473 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16474 if (len != nullptr && len->form_is_constant ())
16475 {
16476 /* Pass 0 as the default as we know this attribute is constant
16477 and the default value will not be returned. */
16478 LONGEST sz = len->constant_value (0);
16479 prop_type = cu->per_cu->int_type (sz, true);
16480 }
16481 else
16482 {
16483 /* If the size is not specified then we assume it is the size of
16484 an address on this target. */
16485 prop_type = cu->per_cu->addr_sized_int_type (true);
16486 }
16487
16488 /* Convert the attribute into a dynamic property. */
16489 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16490 length = 1;
16491 else
16492 length_is_constant = false;
16493 }
16494 else if (attr != nullptr)
16495 {
16496 /* This DW_AT_string_length just contains the length with no
16497 indirection. There's no need to create a dynamic property in this
16498 case. Pass 0 for the default value as we know it will not be
16499 returned in this case. */
16500 length = attr->constant_value (0);
16501 }
16502 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16503 {
16504 /* We don't currently support non-constant byte sizes for strings. */
16505 length = attr->constant_value (1);
16506 }
16507 else
16508 {
16509 /* Use 1 as a fallback length if we have nothing else. */
16510 length = 1;
16511 }
16512
16513 index_type = objfile_type (objfile)->builtin_int;
16514 if (length_is_constant)
16515 range_type = create_static_range_type (NULL, index_type, 1, length);
16516 else
16517 {
16518 struct dynamic_prop low_bound;
16519
16520 low_bound.kind = PROP_CONST;
16521 low_bound.data.const_val = 1;
16522 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16523 }
16524 char_type = language_string_char_type (cu->language_defn, gdbarch);
16525 type = create_string_type (NULL, char_type, range_type);
16526
16527 return set_die_type (die, type, cu);
16528 }
16529
16530 /* Assuming that DIE corresponds to a function, returns nonzero
16531 if the function is prototyped. */
16532
16533 static int
16534 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16535 {
16536 struct attribute *attr;
16537
16538 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16539 if (attr && (DW_UNSND (attr) != 0))
16540 return 1;
16541
16542 /* The DWARF standard implies that the DW_AT_prototyped attribute
16543 is only meaningful for C, but the concept also extends to other
16544 languages that allow unprototyped functions (Eg: Objective C).
16545 For all other languages, assume that functions are always
16546 prototyped. */
16547 if (cu->language != language_c
16548 && cu->language != language_objc
16549 && cu->language != language_opencl)
16550 return 1;
16551
16552 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16553 prototyped and unprototyped functions; default to prototyped,
16554 since that is more common in modern code (and RealView warns
16555 about unprototyped functions). */
16556 if (producer_is_realview (cu->producer))
16557 return 1;
16558
16559 return 0;
16560 }
16561
16562 /* Handle DIES due to C code like:
16563
16564 struct foo
16565 {
16566 int (*funcp)(int a, long l);
16567 int b;
16568 };
16569
16570 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16571
16572 static struct type *
16573 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16574 {
16575 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16576 struct type *type; /* Type that this function returns. */
16577 struct type *ftype; /* Function that returns above type. */
16578 struct attribute *attr;
16579
16580 type = die_type (die, cu);
16581
16582 /* The die_type call above may have already set the type for this DIE. */
16583 ftype = get_die_type (die, cu);
16584 if (ftype)
16585 return ftype;
16586
16587 ftype = lookup_function_type (type);
16588
16589 if (prototyped_function_p (die, cu))
16590 TYPE_PROTOTYPED (ftype) = 1;
16591
16592 /* Store the calling convention in the type if it's available in
16593 the subroutine die. Otherwise set the calling convention to
16594 the default value DW_CC_normal. */
16595 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16596 if (attr != nullptr
16597 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16598 TYPE_CALLING_CONVENTION (ftype)
16599 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16600 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16601 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16602 else
16603 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16604
16605 /* Record whether the function returns normally to its caller or not
16606 if the DWARF producer set that information. */
16607 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16608 if (attr && (DW_UNSND (attr) != 0))
16609 TYPE_NO_RETURN (ftype) = 1;
16610
16611 /* We need to add the subroutine type to the die immediately so
16612 we don't infinitely recurse when dealing with parameters
16613 declared as the same subroutine type. */
16614 set_die_type (die, ftype, cu);
16615
16616 if (die->child != NULL)
16617 {
16618 struct type *void_type = objfile_type (objfile)->builtin_void;
16619 struct die_info *child_die;
16620 int nparams, iparams;
16621
16622 /* Count the number of parameters.
16623 FIXME: GDB currently ignores vararg functions, but knows about
16624 vararg member functions. */
16625 nparams = 0;
16626 child_die = die->child;
16627 while (child_die && child_die->tag)
16628 {
16629 if (child_die->tag == DW_TAG_formal_parameter)
16630 nparams++;
16631 else if (child_die->tag == DW_TAG_unspecified_parameters)
16632 TYPE_VARARGS (ftype) = 1;
16633 child_die = child_die->sibling;
16634 }
16635
16636 /* Allocate storage for parameters and fill them in. */
16637 TYPE_NFIELDS (ftype) = nparams;
16638 TYPE_FIELDS (ftype) = (struct field *)
16639 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16640
16641 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16642 even if we error out during the parameters reading below. */
16643 for (iparams = 0; iparams < nparams; iparams++)
16644 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16645
16646 iparams = 0;
16647 child_die = die->child;
16648 while (child_die && child_die->tag)
16649 {
16650 if (child_die->tag == DW_TAG_formal_parameter)
16651 {
16652 struct type *arg_type;
16653
16654 /* DWARF version 2 has no clean way to discern C++
16655 static and non-static member functions. G++ helps
16656 GDB by marking the first parameter for non-static
16657 member functions (which is the this pointer) as
16658 artificial. We pass this information to
16659 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16660
16661 DWARF version 3 added DW_AT_object_pointer, which GCC
16662 4.5 does not yet generate. */
16663 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16664 if (attr != nullptr)
16665 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16666 else
16667 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16668 arg_type = die_type (child_die, cu);
16669
16670 /* RealView does not mark THIS as const, which the testsuite
16671 expects. GCC marks THIS as const in method definitions,
16672 but not in the class specifications (GCC PR 43053). */
16673 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16674 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16675 {
16676 int is_this = 0;
16677 struct dwarf2_cu *arg_cu = cu;
16678 const char *name = dwarf2_name (child_die, cu);
16679
16680 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16681 if (attr != nullptr)
16682 {
16683 /* If the compiler emits this, use it. */
16684 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16685 is_this = 1;
16686 }
16687 else if (name && strcmp (name, "this") == 0)
16688 /* Function definitions will have the argument names. */
16689 is_this = 1;
16690 else if (name == NULL && iparams == 0)
16691 /* Declarations may not have the names, so like
16692 elsewhere in GDB, assume an artificial first
16693 argument is "this". */
16694 is_this = 1;
16695
16696 if (is_this)
16697 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16698 arg_type, 0);
16699 }
16700
16701 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16702 iparams++;
16703 }
16704 child_die = child_die->sibling;
16705 }
16706 }
16707
16708 return ftype;
16709 }
16710
16711 static struct type *
16712 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16713 {
16714 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16715 const char *name = NULL;
16716 struct type *this_type, *target_type;
16717
16718 name = dwarf2_full_name (NULL, die, cu);
16719 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16720 TYPE_TARGET_STUB (this_type) = 1;
16721 set_die_type (die, this_type, cu);
16722 target_type = die_type (die, cu);
16723 if (target_type != this_type)
16724 TYPE_TARGET_TYPE (this_type) = target_type;
16725 else
16726 {
16727 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16728 spec and cause infinite loops in GDB. */
16729 complaint (_("Self-referential DW_TAG_typedef "
16730 "- DIE at %s [in module %s]"),
16731 sect_offset_str (die->sect_off), objfile_name (objfile));
16732 TYPE_TARGET_TYPE (this_type) = NULL;
16733 }
16734 if (name == NULL)
16735 {
16736 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16737 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16738 Handle these by just returning the target type, rather than
16739 constructing an anonymous typedef type and trying to handle this
16740 elsewhere. */
16741 set_die_type (die, target_type, cu);
16742 return target_type;
16743 }
16744 return this_type;
16745 }
16746
16747 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16748 (which may be different from NAME) to the architecture back-end to allow
16749 it to guess the correct format if necessary. */
16750
16751 static struct type *
16752 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16753 const char *name_hint, enum bfd_endian byte_order)
16754 {
16755 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16756 const struct floatformat **format;
16757 struct type *type;
16758
16759 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16760 if (format)
16761 type = init_float_type (objfile, bits, name, format, byte_order);
16762 else
16763 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16764
16765 return type;
16766 }
16767
16768 /* Allocate an integer type of size BITS and name NAME. */
16769
16770 static struct type *
16771 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16772 int bits, int unsigned_p, const char *name)
16773 {
16774 struct type *type;
16775
16776 /* Versions of Intel's C Compiler generate an integer type called "void"
16777 instead of using DW_TAG_unspecified_type. This has been seen on
16778 at least versions 14, 17, and 18. */
16779 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16780 && strcmp (name, "void") == 0)
16781 type = objfile_type (objfile)->builtin_void;
16782 else
16783 type = init_integer_type (objfile, bits, unsigned_p, name);
16784
16785 return type;
16786 }
16787
16788 /* Initialise and return a floating point type of size BITS suitable for
16789 use as a component of a complex number. The NAME_HINT is passed through
16790 when initialising the floating point type and is the name of the complex
16791 type.
16792
16793 As DWARF doesn't currently provide an explicit name for the components
16794 of a complex number, but it can be helpful to have these components
16795 named, we try to select a suitable name based on the size of the
16796 component. */
16797 static struct type *
16798 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16799 struct objfile *objfile,
16800 int bits, const char *name_hint,
16801 enum bfd_endian byte_order)
16802 {
16803 gdbarch *gdbarch = get_objfile_arch (objfile);
16804 struct type *tt = nullptr;
16805
16806 /* Try to find a suitable floating point builtin type of size BITS.
16807 We're going to use the name of this type as the name for the complex
16808 target type that we are about to create. */
16809 switch (cu->language)
16810 {
16811 case language_fortran:
16812 switch (bits)
16813 {
16814 case 32:
16815 tt = builtin_f_type (gdbarch)->builtin_real;
16816 break;
16817 case 64:
16818 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16819 break;
16820 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16821 case 128:
16822 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16823 break;
16824 }
16825 break;
16826 default:
16827 switch (bits)
16828 {
16829 case 32:
16830 tt = builtin_type (gdbarch)->builtin_float;
16831 break;
16832 case 64:
16833 tt = builtin_type (gdbarch)->builtin_double;
16834 break;
16835 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16836 case 128:
16837 tt = builtin_type (gdbarch)->builtin_long_double;
16838 break;
16839 }
16840 break;
16841 }
16842
16843 /* If the type we found doesn't match the size we were looking for, then
16844 pretend we didn't find a type at all, the complex target type we
16845 create will then be nameless. */
16846 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16847 tt = nullptr;
16848
16849 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16850 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16851 }
16852
16853 /* Find a representation of a given base type and install
16854 it in the TYPE field of the die. */
16855
16856 static struct type *
16857 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16858 {
16859 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16860 struct type *type;
16861 struct attribute *attr;
16862 int encoding = 0, bits = 0;
16863 const char *name;
16864 gdbarch *arch;
16865
16866 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16867 if (attr != nullptr)
16868 encoding = DW_UNSND (attr);
16869 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16870 if (attr != nullptr)
16871 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16872 name = dwarf2_name (die, cu);
16873 if (!name)
16874 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16875
16876 arch = get_objfile_arch (objfile);
16877 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16878
16879 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16880 if (attr)
16881 {
16882 int endianity = DW_UNSND (attr);
16883
16884 switch (endianity)
16885 {
16886 case DW_END_big:
16887 byte_order = BFD_ENDIAN_BIG;
16888 break;
16889 case DW_END_little:
16890 byte_order = BFD_ENDIAN_LITTLE;
16891 break;
16892 default:
16893 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16894 break;
16895 }
16896 }
16897
16898 switch (encoding)
16899 {
16900 case DW_ATE_address:
16901 /* Turn DW_ATE_address into a void * pointer. */
16902 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16903 type = init_pointer_type (objfile, bits, name, type);
16904 break;
16905 case DW_ATE_boolean:
16906 type = init_boolean_type (objfile, bits, 1, name);
16907 break;
16908 case DW_ATE_complex_float:
16909 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16910 byte_order);
16911 type = init_complex_type (name, type);
16912 break;
16913 case DW_ATE_decimal_float:
16914 type = init_decfloat_type (objfile, bits, name);
16915 break;
16916 case DW_ATE_float:
16917 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16918 break;
16919 case DW_ATE_signed:
16920 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16921 break;
16922 case DW_ATE_unsigned:
16923 if (cu->language == language_fortran
16924 && name
16925 && startswith (name, "character("))
16926 type = init_character_type (objfile, bits, 1, name);
16927 else
16928 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16929 break;
16930 case DW_ATE_signed_char:
16931 if (cu->language == language_ada || cu->language == language_m2
16932 || cu->language == language_pascal
16933 || cu->language == language_fortran)
16934 type = init_character_type (objfile, bits, 0, name);
16935 else
16936 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16937 break;
16938 case DW_ATE_unsigned_char:
16939 if (cu->language == language_ada || cu->language == language_m2
16940 || cu->language == language_pascal
16941 || cu->language == language_fortran
16942 || cu->language == language_rust)
16943 type = init_character_type (objfile, bits, 1, name);
16944 else
16945 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16946 break;
16947 case DW_ATE_UTF:
16948 {
16949 if (bits == 16)
16950 type = builtin_type (arch)->builtin_char16;
16951 else if (bits == 32)
16952 type = builtin_type (arch)->builtin_char32;
16953 else
16954 {
16955 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
16956 bits);
16957 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16958 }
16959 return set_die_type (die, type, cu);
16960 }
16961 break;
16962
16963 default:
16964 complaint (_("unsupported DW_AT_encoding: '%s'"),
16965 dwarf_type_encoding_name (encoding));
16966 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16967 break;
16968 }
16969
16970 if (name && strcmp (name, "char") == 0)
16971 TYPE_NOSIGN (type) = 1;
16972
16973 maybe_set_alignment (cu, die, type);
16974
16975 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
16976
16977 return set_die_type (die, type, cu);
16978 }
16979
16980 /* Parse dwarf attribute if it's a block, reference or constant and put the
16981 resulting value of the attribute into struct bound_prop.
16982 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16983
16984 static int
16985 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
16986 struct dwarf2_cu *cu, struct dynamic_prop *prop,
16987 struct type *default_type)
16988 {
16989 struct dwarf2_property_baton *baton;
16990 struct obstack *obstack
16991 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
16992
16993 gdb_assert (default_type != NULL);
16994
16995 if (attr == NULL || prop == NULL)
16996 return 0;
16997
16998 if (attr->form_is_block ())
16999 {
17000 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17001 baton->property_type = default_type;
17002 baton->locexpr.per_cu = cu->per_cu;
17003 baton->locexpr.size = DW_BLOCK (attr)->size;
17004 baton->locexpr.data = DW_BLOCK (attr)->data;
17005 switch (attr->name)
17006 {
17007 case DW_AT_string_length:
17008 baton->locexpr.is_reference = true;
17009 break;
17010 default:
17011 baton->locexpr.is_reference = false;
17012 break;
17013 }
17014 prop->data.baton = baton;
17015 prop->kind = PROP_LOCEXPR;
17016 gdb_assert (prop->data.baton != NULL);
17017 }
17018 else if (attr->form_is_ref ())
17019 {
17020 struct dwarf2_cu *target_cu = cu;
17021 struct die_info *target_die;
17022 struct attribute *target_attr;
17023
17024 target_die = follow_die_ref (die, attr, &target_cu);
17025 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17026 if (target_attr == NULL)
17027 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17028 target_cu);
17029 if (target_attr == NULL)
17030 return 0;
17031
17032 switch (target_attr->name)
17033 {
17034 case DW_AT_location:
17035 if (target_attr->form_is_section_offset ())
17036 {
17037 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17038 baton->property_type = die_type (target_die, target_cu);
17039 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17040 prop->data.baton = baton;
17041 prop->kind = PROP_LOCLIST;
17042 gdb_assert (prop->data.baton != NULL);
17043 }
17044 else if (target_attr->form_is_block ())
17045 {
17046 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17047 baton->property_type = die_type (target_die, target_cu);
17048 baton->locexpr.per_cu = cu->per_cu;
17049 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17050 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17051 baton->locexpr.is_reference = true;
17052 prop->data.baton = baton;
17053 prop->kind = PROP_LOCEXPR;
17054 gdb_assert (prop->data.baton != NULL);
17055 }
17056 else
17057 {
17058 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17059 "dynamic property");
17060 return 0;
17061 }
17062 break;
17063 case DW_AT_data_member_location:
17064 {
17065 LONGEST offset;
17066
17067 if (!handle_data_member_location (target_die, target_cu,
17068 &offset))
17069 return 0;
17070
17071 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17072 baton->property_type = read_type_die (target_die->parent,
17073 target_cu);
17074 baton->offset_info.offset = offset;
17075 baton->offset_info.type = die_type (target_die, target_cu);
17076 prop->data.baton = baton;
17077 prop->kind = PROP_ADDR_OFFSET;
17078 break;
17079 }
17080 }
17081 }
17082 else if (attr->form_is_constant ())
17083 {
17084 prop->data.const_val = attr->constant_value (0);
17085 prop->kind = PROP_CONST;
17086 }
17087 else
17088 {
17089 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17090 dwarf2_name (die, cu));
17091 return 0;
17092 }
17093
17094 return 1;
17095 }
17096
17097 /* See read.h. */
17098
17099 struct type *
17100 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17101 {
17102 struct objfile *objfile = dwarf2_per_objfile->objfile;
17103 struct type *int_type;
17104
17105 /* Helper macro to examine the various builtin types. */
17106 #define TRY_TYPE(F) \
17107 int_type = (unsigned_p \
17108 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17109 : objfile_type (objfile)->builtin_ ## F); \
17110 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17111 return int_type
17112
17113 TRY_TYPE (char);
17114 TRY_TYPE (short);
17115 TRY_TYPE (int);
17116 TRY_TYPE (long);
17117 TRY_TYPE (long_long);
17118
17119 #undef TRY_TYPE
17120
17121 gdb_assert_not_reached ("unable to find suitable integer type");
17122 }
17123
17124 /* See read.h. */
17125
17126 struct type *
17127 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17128 {
17129 int addr_size = this->addr_size ();
17130 return int_type (addr_size, unsigned_p);
17131 }
17132
17133 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17134 present (which is valid) then compute the default type based on the
17135 compilation units address size. */
17136
17137 static struct type *
17138 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17139 {
17140 struct type *index_type = die_type (die, cu);
17141
17142 /* Dwarf-2 specifications explicitly allows to create subrange types
17143 without specifying a base type.
17144 In that case, the base type must be set to the type of
17145 the lower bound, upper bound or count, in that order, if any of these
17146 three attributes references an object that has a type.
17147 If no base type is found, the Dwarf-2 specifications say that
17148 a signed integer type of size equal to the size of an address should
17149 be used.
17150 For the following C code: `extern char gdb_int [];'
17151 GCC produces an empty range DIE.
17152 FIXME: muller/2010-05-28: Possible references to object for low bound,
17153 high bound or count are not yet handled by this code. */
17154 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17155 index_type = cu->per_cu->addr_sized_int_type (false);
17156
17157 return index_type;
17158 }
17159
17160 /* Read the given DW_AT_subrange DIE. */
17161
17162 static struct type *
17163 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17164 {
17165 struct type *base_type, *orig_base_type;
17166 struct type *range_type;
17167 struct attribute *attr;
17168 struct dynamic_prop low, high;
17169 int low_default_is_valid;
17170 int high_bound_is_count = 0;
17171 const char *name;
17172 ULONGEST negative_mask;
17173
17174 orig_base_type = read_subrange_index_type (die, cu);
17175
17176 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17177 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17178 creating the range type, but we use the result of check_typedef
17179 when examining properties of the type. */
17180 base_type = check_typedef (orig_base_type);
17181
17182 /* The die_type call above may have already set the type for this DIE. */
17183 range_type = get_die_type (die, cu);
17184 if (range_type)
17185 return range_type;
17186
17187 low.kind = PROP_CONST;
17188 high.kind = PROP_CONST;
17189 high.data.const_val = 0;
17190
17191 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17192 omitting DW_AT_lower_bound. */
17193 switch (cu->language)
17194 {
17195 case language_c:
17196 case language_cplus:
17197 low.data.const_val = 0;
17198 low_default_is_valid = 1;
17199 break;
17200 case language_fortran:
17201 low.data.const_val = 1;
17202 low_default_is_valid = 1;
17203 break;
17204 case language_d:
17205 case language_objc:
17206 case language_rust:
17207 low.data.const_val = 0;
17208 low_default_is_valid = (cu->header.version >= 4);
17209 break;
17210 case language_ada:
17211 case language_m2:
17212 case language_pascal:
17213 low.data.const_val = 1;
17214 low_default_is_valid = (cu->header.version >= 4);
17215 break;
17216 default:
17217 low.data.const_val = 0;
17218 low_default_is_valid = 0;
17219 break;
17220 }
17221
17222 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17223 if (attr != nullptr)
17224 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17225 else if (!low_default_is_valid)
17226 complaint (_("Missing DW_AT_lower_bound "
17227 "- DIE at %s [in module %s]"),
17228 sect_offset_str (die->sect_off),
17229 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17230
17231 struct attribute *attr_ub, *attr_count;
17232 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17233 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17234 {
17235 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17236 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17237 {
17238 /* If bounds are constant do the final calculation here. */
17239 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17240 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17241 else
17242 high_bound_is_count = 1;
17243 }
17244 else
17245 {
17246 if (attr_ub != NULL)
17247 complaint (_("Unresolved DW_AT_upper_bound "
17248 "- DIE at %s [in module %s]"),
17249 sect_offset_str (die->sect_off),
17250 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17251 if (attr_count != NULL)
17252 complaint (_("Unresolved DW_AT_count "
17253 "- DIE at %s [in module %s]"),
17254 sect_offset_str (die->sect_off),
17255 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17256 }
17257 }
17258
17259 LONGEST bias = 0;
17260 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17261 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17262 bias = bias_attr->constant_value (0);
17263
17264 /* Normally, the DWARF producers are expected to use a signed
17265 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17266 But this is unfortunately not always the case, as witnessed
17267 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17268 is used instead. To work around that ambiguity, we treat
17269 the bounds as signed, and thus sign-extend their values, when
17270 the base type is signed. */
17271 negative_mask =
17272 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17273 if (low.kind == PROP_CONST
17274 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17275 low.data.const_val |= negative_mask;
17276 if (high.kind == PROP_CONST
17277 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17278 high.data.const_val |= negative_mask;
17279
17280 /* Check for bit and byte strides. */
17281 struct dynamic_prop byte_stride_prop;
17282 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17283 if (attr_byte_stride != nullptr)
17284 {
17285 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17286 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17287 prop_type);
17288 }
17289
17290 struct dynamic_prop bit_stride_prop;
17291 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17292 if (attr_bit_stride != nullptr)
17293 {
17294 /* It only makes sense to have either a bit or byte stride. */
17295 if (attr_byte_stride != nullptr)
17296 {
17297 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17298 "- DIE at %s [in module %s]"),
17299 sect_offset_str (die->sect_off),
17300 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17301 attr_bit_stride = nullptr;
17302 }
17303 else
17304 {
17305 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17306 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17307 prop_type);
17308 }
17309 }
17310
17311 if (attr_byte_stride != nullptr
17312 || attr_bit_stride != nullptr)
17313 {
17314 bool byte_stride_p = (attr_byte_stride != nullptr);
17315 struct dynamic_prop *stride
17316 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17317
17318 range_type
17319 = create_range_type_with_stride (NULL, orig_base_type, &low,
17320 &high, bias, stride, byte_stride_p);
17321 }
17322 else
17323 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17324
17325 if (high_bound_is_count)
17326 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17327
17328 /* Ada expects an empty array on no boundary attributes. */
17329 if (attr == NULL && cu->language != language_ada)
17330 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17331
17332 name = dwarf2_name (die, cu);
17333 if (name)
17334 TYPE_NAME (range_type) = name;
17335
17336 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17337 if (attr != nullptr)
17338 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17339
17340 maybe_set_alignment (cu, die, range_type);
17341
17342 set_die_type (die, range_type, cu);
17343
17344 /* set_die_type should be already done. */
17345 set_descriptive_type (range_type, die, cu);
17346
17347 return range_type;
17348 }
17349
17350 static struct type *
17351 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17352 {
17353 struct type *type;
17354
17355 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17356 NULL);
17357 TYPE_NAME (type) = dwarf2_name (die, cu);
17358
17359 /* In Ada, an unspecified type is typically used when the description
17360 of the type is deferred to a different unit. When encountering
17361 such a type, we treat it as a stub, and try to resolve it later on,
17362 when needed. */
17363 if (cu->language == language_ada)
17364 TYPE_STUB (type) = 1;
17365
17366 return set_die_type (die, type, cu);
17367 }
17368
17369 /* Read a single die and all its descendents. Set the die's sibling
17370 field to NULL; set other fields in the die correctly, and set all
17371 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17372 location of the info_ptr after reading all of those dies. PARENT
17373 is the parent of the die in question. */
17374
17375 static struct die_info *
17376 read_die_and_children (const struct die_reader_specs *reader,
17377 const gdb_byte *info_ptr,
17378 const gdb_byte **new_info_ptr,
17379 struct die_info *parent)
17380 {
17381 struct die_info *die;
17382 const gdb_byte *cur_ptr;
17383
17384 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17385 if (die == NULL)
17386 {
17387 *new_info_ptr = cur_ptr;
17388 return NULL;
17389 }
17390 store_in_ref_table (die, reader->cu);
17391
17392 if (die->has_children)
17393 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17394 else
17395 {
17396 die->child = NULL;
17397 *new_info_ptr = cur_ptr;
17398 }
17399
17400 die->sibling = NULL;
17401 die->parent = parent;
17402 return die;
17403 }
17404
17405 /* Read a die, all of its descendents, and all of its siblings; set
17406 all of the fields of all of the dies correctly. Arguments are as
17407 in read_die_and_children. */
17408
17409 static struct die_info *
17410 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17411 const gdb_byte *info_ptr,
17412 const gdb_byte **new_info_ptr,
17413 struct die_info *parent)
17414 {
17415 struct die_info *first_die, *last_sibling;
17416 const gdb_byte *cur_ptr;
17417
17418 cur_ptr = info_ptr;
17419 first_die = last_sibling = NULL;
17420
17421 while (1)
17422 {
17423 struct die_info *die
17424 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17425
17426 if (die == NULL)
17427 {
17428 *new_info_ptr = cur_ptr;
17429 return first_die;
17430 }
17431
17432 if (!first_die)
17433 first_die = die;
17434 else
17435 last_sibling->sibling = die;
17436
17437 last_sibling = die;
17438 }
17439 }
17440
17441 /* Read a die, all of its descendents, and all of its siblings; set
17442 all of the fields of all of the dies correctly. Arguments are as
17443 in read_die_and_children.
17444 This the main entry point for reading a DIE and all its children. */
17445
17446 static struct die_info *
17447 read_die_and_siblings (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 *die = read_die_and_siblings_1 (reader, info_ptr,
17453 new_info_ptr, parent);
17454
17455 if (dwarf_die_debug)
17456 {
17457 fprintf_unfiltered (gdb_stdlog,
17458 "Read die from %s@0x%x of %s:\n",
17459 reader->die_section->get_name (),
17460 (unsigned) (info_ptr - reader->die_section->buffer),
17461 bfd_get_filename (reader->abfd));
17462 dump_die (die, dwarf_die_debug);
17463 }
17464
17465 return die;
17466 }
17467
17468 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17469 attributes.
17470 The caller is responsible for filling in the extra attributes
17471 and updating (*DIEP)->num_attrs.
17472 Set DIEP to point to a newly allocated die with its information,
17473 except for its child, sibling, and parent fields. */
17474
17475 static const gdb_byte *
17476 read_full_die_1 (const struct die_reader_specs *reader,
17477 struct die_info **diep, const gdb_byte *info_ptr,
17478 int num_extra_attrs)
17479 {
17480 unsigned int abbrev_number, bytes_read, i;
17481 struct abbrev_info *abbrev;
17482 struct die_info *die;
17483 struct dwarf2_cu *cu = reader->cu;
17484 bfd *abfd = reader->abfd;
17485
17486 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17487 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17488 info_ptr += bytes_read;
17489 if (!abbrev_number)
17490 {
17491 *diep = NULL;
17492 return info_ptr;
17493 }
17494
17495 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17496 if (!abbrev)
17497 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17498 abbrev_number,
17499 bfd_get_filename (abfd));
17500
17501 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17502 die->sect_off = sect_off;
17503 die->tag = abbrev->tag;
17504 die->abbrev = abbrev_number;
17505 die->has_children = abbrev->has_children;
17506
17507 /* Make the result usable.
17508 The caller needs to update num_attrs after adding the extra
17509 attributes. */
17510 die->num_attrs = abbrev->num_attrs;
17511
17512 std::vector<int> indexes_that_need_reprocess;
17513 for (i = 0; i < abbrev->num_attrs; ++i)
17514 {
17515 bool need_reprocess;
17516 info_ptr =
17517 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17518 info_ptr, &need_reprocess);
17519 if (need_reprocess)
17520 indexes_that_need_reprocess.push_back (i);
17521 }
17522
17523 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17524 if (attr != nullptr)
17525 cu->str_offsets_base = DW_UNSND (attr);
17526
17527 auto maybe_addr_base = die->addr_base ();
17528 if (maybe_addr_base.has_value ())
17529 cu->addr_base = *maybe_addr_base;
17530 for (int index : indexes_that_need_reprocess)
17531 read_attribute_reprocess (reader, &die->attrs[index]);
17532 *diep = die;
17533 return info_ptr;
17534 }
17535
17536 /* Read a die and all its attributes.
17537 Set DIEP to point to a newly allocated die with its information,
17538 except for its child, sibling, and parent fields. */
17539
17540 static const gdb_byte *
17541 read_full_die (const struct die_reader_specs *reader,
17542 struct die_info **diep, const gdb_byte *info_ptr)
17543 {
17544 const gdb_byte *result;
17545
17546 result = read_full_die_1 (reader, diep, info_ptr, 0);
17547
17548 if (dwarf_die_debug)
17549 {
17550 fprintf_unfiltered (gdb_stdlog,
17551 "Read die from %s@0x%x of %s:\n",
17552 reader->die_section->get_name (),
17553 (unsigned) (info_ptr - reader->die_section->buffer),
17554 bfd_get_filename (reader->abfd));
17555 dump_die (*diep, dwarf_die_debug);
17556 }
17557
17558 return result;
17559 }
17560 \f
17561
17562 /* Returns nonzero if TAG represents a type that we might generate a partial
17563 symbol for. */
17564
17565 static int
17566 is_type_tag_for_partial (int tag)
17567 {
17568 switch (tag)
17569 {
17570 #if 0
17571 /* Some types that would be reasonable to generate partial symbols for,
17572 that we don't at present. */
17573 case DW_TAG_array_type:
17574 case DW_TAG_file_type:
17575 case DW_TAG_ptr_to_member_type:
17576 case DW_TAG_set_type:
17577 case DW_TAG_string_type:
17578 case DW_TAG_subroutine_type:
17579 #endif
17580 case DW_TAG_base_type:
17581 case DW_TAG_class_type:
17582 case DW_TAG_interface_type:
17583 case DW_TAG_enumeration_type:
17584 case DW_TAG_structure_type:
17585 case DW_TAG_subrange_type:
17586 case DW_TAG_typedef:
17587 case DW_TAG_union_type:
17588 return 1;
17589 default:
17590 return 0;
17591 }
17592 }
17593
17594 /* Load all DIEs that are interesting for partial symbols into memory. */
17595
17596 static struct partial_die_info *
17597 load_partial_dies (const struct die_reader_specs *reader,
17598 const gdb_byte *info_ptr, int building_psymtab)
17599 {
17600 struct dwarf2_cu *cu = reader->cu;
17601 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17602 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17603 unsigned int bytes_read;
17604 unsigned int load_all = 0;
17605 int nesting_level = 1;
17606
17607 parent_die = NULL;
17608 last_die = NULL;
17609
17610 gdb_assert (cu->per_cu != NULL);
17611 if (cu->per_cu->load_all_dies)
17612 load_all = 1;
17613
17614 cu->partial_dies
17615 = htab_create_alloc_ex (cu->header.length / 12,
17616 partial_die_hash,
17617 partial_die_eq,
17618 NULL,
17619 &cu->comp_unit_obstack,
17620 hashtab_obstack_allocate,
17621 dummy_obstack_deallocate);
17622
17623 while (1)
17624 {
17625 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17626
17627 /* A NULL abbrev means the end of a series of children. */
17628 if (abbrev == NULL)
17629 {
17630 if (--nesting_level == 0)
17631 return first_die;
17632
17633 info_ptr += bytes_read;
17634 last_die = parent_die;
17635 parent_die = parent_die->die_parent;
17636 continue;
17637 }
17638
17639 /* Check for template arguments. We never save these; if
17640 they're seen, we just mark the parent, and go on our way. */
17641 if (parent_die != NULL
17642 && cu->language == language_cplus
17643 && (abbrev->tag == DW_TAG_template_type_param
17644 || abbrev->tag == DW_TAG_template_value_param))
17645 {
17646 parent_die->has_template_arguments = 1;
17647
17648 if (!load_all)
17649 {
17650 /* We don't need a partial DIE for the template argument. */
17651 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17652 continue;
17653 }
17654 }
17655
17656 /* We only recurse into c++ subprograms looking for template arguments.
17657 Skip their other children. */
17658 if (!load_all
17659 && cu->language == language_cplus
17660 && parent_die != NULL
17661 && parent_die->tag == DW_TAG_subprogram)
17662 {
17663 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17664 continue;
17665 }
17666
17667 /* Check whether this DIE is interesting enough to save. Normally
17668 we would not be interested in members here, but there may be
17669 later variables referencing them via DW_AT_specification (for
17670 static members). */
17671 if (!load_all
17672 && !is_type_tag_for_partial (abbrev->tag)
17673 && abbrev->tag != DW_TAG_constant
17674 && abbrev->tag != DW_TAG_enumerator
17675 && abbrev->tag != DW_TAG_subprogram
17676 && abbrev->tag != DW_TAG_inlined_subroutine
17677 && abbrev->tag != DW_TAG_lexical_block
17678 && abbrev->tag != DW_TAG_variable
17679 && abbrev->tag != DW_TAG_namespace
17680 && abbrev->tag != DW_TAG_module
17681 && abbrev->tag != DW_TAG_member
17682 && abbrev->tag != DW_TAG_imported_unit
17683 && abbrev->tag != DW_TAG_imported_declaration)
17684 {
17685 /* Otherwise we skip to the next sibling, if any. */
17686 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17687 continue;
17688 }
17689
17690 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17691 abbrev);
17692
17693 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17694
17695 /* This two-pass algorithm for processing partial symbols has a
17696 high cost in cache pressure. Thus, handle some simple cases
17697 here which cover the majority of C partial symbols. DIEs
17698 which neither have specification tags in them, nor could have
17699 specification tags elsewhere pointing at them, can simply be
17700 processed and discarded.
17701
17702 This segment is also optional; scan_partial_symbols and
17703 add_partial_symbol will handle these DIEs if we chain
17704 them in normally. When compilers which do not emit large
17705 quantities of duplicate debug information are more common,
17706 this code can probably be removed. */
17707
17708 /* Any complete simple types at the top level (pretty much all
17709 of them, for a language without namespaces), can be processed
17710 directly. */
17711 if (parent_die == NULL
17712 && pdi.has_specification == 0
17713 && pdi.is_declaration == 0
17714 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17715 || pdi.tag == DW_TAG_base_type
17716 || pdi.tag == DW_TAG_subrange_type))
17717 {
17718 if (building_psymtab && pdi.name != NULL)
17719 add_psymbol_to_list (pdi.name, false,
17720 VAR_DOMAIN, LOC_TYPEDEF, -1,
17721 psymbol_placement::STATIC,
17722 0, cu->language, objfile);
17723 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17724 continue;
17725 }
17726
17727 /* The exception for DW_TAG_typedef with has_children above is
17728 a workaround of GCC PR debug/47510. In the case of this complaint
17729 type_name_or_error will error on such types later.
17730
17731 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17732 it could not find the child DIEs referenced later, this is checked
17733 above. In correct DWARF DW_TAG_typedef should have no children. */
17734
17735 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17736 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17737 "- DIE at %s [in module %s]"),
17738 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17739
17740 /* If we're at the second level, and we're an enumerator, and
17741 our parent has no specification (meaning possibly lives in a
17742 namespace elsewhere), then we can add the partial symbol now
17743 instead of queueing it. */
17744 if (pdi.tag == DW_TAG_enumerator
17745 && parent_die != NULL
17746 && parent_die->die_parent == NULL
17747 && parent_die->tag == DW_TAG_enumeration_type
17748 && parent_die->has_specification == 0)
17749 {
17750 if (pdi.name == NULL)
17751 complaint (_("malformed enumerator DIE ignored"));
17752 else if (building_psymtab)
17753 add_psymbol_to_list (pdi.name, false,
17754 VAR_DOMAIN, LOC_CONST, -1,
17755 cu->language == language_cplus
17756 ? psymbol_placement::GLOBAL
17757 : psymbol_placement::STATIC,
17758 0, cu->language, objfile);
17759
17760 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17761 continue;
17762 }
17763
17764 struct partial_die_info *part_die
17765 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17766
17767 /* We'll save this DIE so link it in. */
17768 part_die->die_parent = parent_die;
17769 part_die->die_sibling = NULL;
17770 part_die->die_child = NULL;
17771
17772 if (last_die && last_die == parent_die)
17773 last_die->die_child = part_die;
17774 else if (last_die)
17775 last_die->die_sibling = part_die;
17776
17777 last_die = part_die;
17778
17779 if (first_die == NULL)
17780 first_die = part_die;
17781
17782 /* Maybe add the DIE to the hash table. Not all DIEs that we
17783 find interesting need to be in the hash table, because we
17784 also have the parent/sibling/child chains; only those that we
17785 might refer to by offset later during partial symbol reading.
17786
17787 For now this means things that might have be the target of a
17788 DW_AT_specification, DW_AT_abstract_origin, or
17789 DW_AT_extension. DW_AT_extension will refer only to
17790 namespaces; DW_AT_abstract_origin refers to functions (and
17791 many things under the function DIE, but we do not recurse
17792 into function DIEs during partial symbol reading) and
17793 possibly variables as well; DW_AT_specification refers to
17794 declarations. Declarations ought to have the DW_AT_declaration
17795 flag. It happens that GCC forgets to put it in sometimes, but
17796 only for functions, not for types.
17797
17798 Adding more things than necessary to the hash table is harmless
17799 except for the performance cost. Adding too few will result in
17800 wasted time in find_partial_die, when we reread the compilation
17801 unit with load_all_dies set. */
17802
17803 if (load_all
17804 || abbrev->tag == DW_TAG_constant
17805 || abbrev->tag == DW_TAG_subprogram
17806 || abbrev->tag == DW_TAG_variable
17807 || abbrev->tag == DW_TAG_namespace
17808 || part_die->is_declaration)
17809 {
17810 void **slot;
17811
17812 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17813 to_underlying (part_die->sect_off),
17814 INSERT);
17815 *slot = part_die;
17816 }
17817
17818 /* For some DIEs we want to follow their children (if any). For C
17819 we have no reason to follow the children of structures; for other
17820 languages we have to, so that we can get at method physnames
17821 to infer fully qualified class names, for DW_AT_specification,
17822 and for C++ template arguments. For C++, we also look one level
17823 inside functions to find template arguments (if the name of the
17824 function does not already contain the template arguments).
17825
17826 For Ada and Fortran, we need to scan the children of subprograms
17827 and lexical blocks as well because these languages allow the
17828 definition of nested entities that could be interesting for the
17829 debugger, such as nested subprograms for instance. */
17830 if (last_die->has_children
17831 && (load_all
17832 || last_die->tag == DW_TAG_namespace
17833 || last_die->tag == DW_TAG_module
17834 || last_die->tag == DW_TAG_enumeration_type
17835 || (cu->language == language_cplus
17836 && last_die->tag == DW_TAG_subprogram
17837 && (last_die->name == NULL
17838 || strchr (last_die->name, '<') == NULL))
17839 || (cu->language != language_c
17840 && (last_die->tag == DW_TAG_class_type
17841 || last_die->tag == DW_TAG_interface_type
17842 || last_die->tag == DW_TAG_structure_type
17843 || last_die->tag == DW_TAG_union_type))
17844 || ((cu->language == language_ada
17845 || cu->language == language_fortran)
17846 && (last_die->tag == DW_TAG_subprogram
17847 || last_die->tag == DW_TAG_lexical_block))))
17848 {
17849 nesting_level++;
17850 parent_die = last_die;
17851 continue;
17852 }
17853
17854 /* Otherwise we skip to the next sibling, if any. */
17855 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17856
17857 /* Back to the top, do it again. */
17858 }
17859 }
17860
17861 partial_die_info::partial_die_info (sect_offset sect_off_,
17862 struct abbrev_info *abbrev)
17863 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17864 {
17865 }
17866
17867 /* Read a minimal amount of information into the minimal die structure.
17868 INFO_PTR should point just after the initial uleb128 of a DIE. */
17869
17870 const gdb_byte *
17871 partial_die_info::read (const struct die_reader_specs *reader,
17872 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17873 {
17874 struct dwarf2_cu *cu = reader->cu;
17875 struct dwarf2_per_objfile *dwarf2_per_objfile
17876 = cu->per_cu->dwarf2_per_objfile;
17877 unsigned int i;
17878 int has_low_pc_attr = 0;
17879 int has_high_pc_attr = 0;
17880 int high_pc_relative = 0;
17881
17882 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17883 for (i = 0; i < abbrev.num_attrs; ++i)
17884 {
17885 bool need_reprocess;
17886 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17887 info_ptr, &need_reprocess);
17888 /* String and address offsets that need to do the reprocessing have
17889 already been read at this point, so there is no need to wait until
17890 the loop terminates to do the reprocessing. */
17891 if (need_reprocess)
17892 read_attribute_reprocess (reader, &attr_vec[i]);
17893 attribute &attr = attr_vec[i];
17894 /* Store the data if it is of an attribute we want to keep in a
17895 partial symbol table. */
17896 switch (attr.name)
17897 {
17898 case DW_AT_name:
17899 switch (tag)
17900 {
17901 case DW_TAG_compile_unit:
17902 case DW_TAG_partial_unit:
17903 case DW_TAG_type_unit:
17904 /* Compilation units have a DW_AT_name that is a filename, not
17905 a source language identifier. */
17906 case DW_TAG_enumeration_type:
17907 case DW_TAG_enumerator:
17908 /* These tags always have simple identifiers already; no need
17909 to canonicalize them. */
17910 name = DW_STRING (&attr);
17911 break;
17912 default:
17913 {
17914 struct objfile *objfile = dwarf2_per_objfile->objfile;
17915
17916 name
17917 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
17918 }
17919 break;
17920 }
17921 break;
17922 case DW_AT_linkage_name:
17923 case DW_AT_MIPS_linkage_name:
17924 /* Note that both forms of linkage name might appear. We
17925 assume they will be the same, and we only store the last
17926 one we see. */
17927 linkage_name = DW_STRING (&attr);
17928 break;
17929 case DW_AT_low_pc:
17930 has_low_pc_attr = 1;
17931 lowpc = attr.value_as_address ();
17932 break;
17933 case DW_AT_high_pc:
17934 has_high_pc_attr = 1;
17935 highpc = attr.value_as_address ();
17936 if (cu->header.version >= 4 && attr.form_is_constant ())
17937 high_pc_relative = 1;
17938 break;
17939 case DW_AT_location:
17940 /* Support the .debug_loc offsets. */
17941 if (attr.form_is_block ())
17942 {
17943 d.locdesc = DW_BLOCK (&attr);
17944 }
17945 else if (attr.form_is_section_offset ())
17946 {
17947 dwarf2_complex_location_expr_complaint ();
17948 }
17949 else
17950 {
17951 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17952 "partial symbol information");
17953 }
17954 break;
17955 case DW_AT_external:
17956 is_external = DW_UNSND (&attr);
17957 break;
17958 case DW_AT_declaration:
17959 is_declaration = DW_UNSND (&attr);
17960 break;
17961 case DW_AT_type:
17962 has_type = 1;
17963 break;
17964 case DW_AT_abstract_origin:
17965 case DW_AT_specification:
17966 case DW_AT_extension:
17967 has_specification = 1;
17968 spec_offset = attr.get_ref_die_offset ();
17969 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17970 || cu->per_cu->is_dwz);
17971 break;
17972 case DW_AT_sibling:
17973 /* Ignore absolute siblings, they might point outside of
17974 the current compile unit. */
17975 if (attr.form == DW_FORM_ref_addr)
17976 complaint (_("ignoring absolute DW_AT_sibling"));
17977 else
17978 {
17979 const gdb_byte *buffer = reader->buffer;
17980 sect_offset off = attr.get_ref_die_offset ();
17981 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
17982
17983 if (sibling_ptr < info_ptr)
17984 complaint (_("DW_AT_sibling points backwards"));
17985 else if (sibling_ptr > reader->buffer_end)
17986 reader->die_section->overflow_complaint ();
17987 else
17988 sibling = sibling_ptr;
17989 }
17990 break;
17991 case DW_AT_byte_size:
17992 has_byte_size = 1;
17993 break;
17994 case DW_AT_const_value:
17995 has_const_value = 1;
17996 break;
17997 case DW_AT_calling_convention:
17998 /* DWARF doesn't provide a way to identify a program's source-level
17999 entry point. DW_AT_calling_convention attributes are only meant
18000 to describe functions' calling conventions.
18001
18002 However, because it's a necessary piece of information in
18003 Fortran, and before DWARF 4 DW_CC_program was the only
18004 piece of debugging information whose definition refers to
18005 a 'main program' at all, several compilers marked Fortran
18006 main programs with DW_CC_program --- even when those
18007 functions use the standard calling conventions.
18008
18009 Although DWARF now specifies a way to provide this
18010 information, we support this practice for backward
18011 compatibility. */
18012 if (DW_UNSND (&attr) == DW_CC_program
18013 && cu->language == language_fortran)
18014 main_subprogram = 1;
18015 break;
18016 case DW_AT_inline:
18017 if (DW_UNSND (&attr) == DW_INL_inlined
18018 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18019 may_be_inlined = 1;
18020 break;
18021
18022 case DW_AT_import:
18023 if (tag == DW_TAG_imported_unit)
18024 {
18025 d.sect_off = attr.get_ref_die_offset ();
18026 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18027 || cu->per_cu->is_dwz);
18028 }
18029 break;
18030
18031 case DW_AT_main_subprogram:
18032 main_subprogram = DW_UNSND (&attr);
18033 break;
18034
18035 case DW_AT_ranges:
18036 {
18037 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18038 but that requires a full DIE, so instead we just
18039 reimplement it. */
18040 int need_ranges_base = tag != DW_TAG_compile_unit;
18041 unsigned int ranges_offset = (DW_UNSND (&attr)
18042 + (need_ranges_base
18043 ? cu->ranges_base
18044 : 0));
18045
18046 /* Value of the DW_AT_ranges attribute is the offset in the
18047 .debug_ranges section. */
18048 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18049 nullptr))
18050 has_pc_info = 1;
18051 }
18052 break;
18053
18054 default:
18055 break;
18056 }
18057 }
18058
18059 /* For Ada, if both the name and the linkage name appear, we prefer
18060 the latter. This lets "catch exception" work better, regardless
18061 of the order in which the name and linkage name were emitted.
18062 Really, though, this is just a workaround for the fact that gdb
18063 doesn't store both the name and the linkage name. */
18064 if (cu->language == language_ada && linkage_name != nullptr)
18065 name = linkage_name;
18066
18067 if (high_pc_relative)
18068 highpc += lowpc;
18069
18070 if (has_low_pc_attr && has_high_pc_attr)
18071 {
18072 /* When using the GNU linker, .gnu.linkonce. sections are used to
18073 eliminate duplicate copies of functions and vtables and such.
18074 The linker will arbitrarily choose one and discard the others.
18075 The AT_*_pc values for such functions refer to local labels in
18076 these sections. If the section from that file was discarded, the
18077 labels are not in the output, so the relocs get a value of 0.
18078 If this is a discarded function, mark the pc bounds as invalid,
18079 so that GDB will ignore it. */
18080 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18081 {
18082 struct objfile *objfile = dwarf2_per_objfile->objfile;
18083 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18084
18085 complaint (_("DW_AT_low_pc %s is zero "
18086 "for DIE at %s [in module %s]"),
18087 paddress (gdbarch, lowpc),
18088 sect_offset_str (sect_off),
18089 objfile_name (objfile));
18090 }
18091 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18092 else if (lowpc >= highpc)
18093 {
18094 struct objfile *objfile = dwarf2_per_objfile->objfile;
18095 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18096
18097 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18098 "for DIE at %s [in module %s]"),
18099 paddress (gdbarch, lowpc),
18100 paddress (gdbarch, highpc),
18101 sect_offset_str (sect_off),
18102 objfile_name (objfile));
18103 }
18104 else
18105 has_pc_info = 1;
18106 }
18107
18108 return info_ptr;
18109 }
18110
18111 /* Find a cached partial DIE at OFFSET in CU. */
18112
18113 struct partial_die_info *
18114 dwarf2_cu::find_partial_die (sect_offset sect_off)
18115 {
18116 struct partial_die_info *lookup_die = NULL;
18117 struct partial_die_info part_die (sect_off);
18118
18119 lookup_die = ((struct partial_die_info *)
18120 htab_find_with_hash (partial_dies, &part_die,
18121 to_underlying (sect_off)));
18122
18123 return lookup_die;
18124 }
18125
18126 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18127 except in the case of .debug_types DIEs which do not reference
18128 outside their CU (they do however referencing other types via
18129 DW_FORM_ref_sig8). */
18130
18131 static const struct cu_partial_die_info
18132 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18133 {
18134 struct dwarf2_per_objfile *dwarf2_per_objfile
18135 = cu->per_cu->dwarf2_per_objfile;
18136 struct objfile *objfile = dwarf2_per_objfile->objfile;
18137 struct dwarf2_per_cu_data *per_cu = NULL;
18138 struct partial_die_info *pd = NULL;
18139
18140 if (offset_in_dwz == cu->per_cu->is_dwz
18141 && cu->header.offset_in_cu_p (sect_off))
18142 {
18143 pd = cu->find_partial_die (sect_off);
18144 if (pd != NULL)
18145 return { cu, pd };
18146 /* We missed recording what we needed.
18147 Load all dies and try again. */
18148 per_cu = cu->per_cu;
18149 }
18150 else
18151 {
18152 /* TUs don't reference other CUs/TUs (except via type signatures). */
18153 if (cu->per_cu->is_debug_types)
18154 {
18155 error (_("Dwarf Error: Type Unit at offset %s contains"
18156 " external reference to offset %s [in module %s].\n"),
18157 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18158 bfd_get_filename (objfile->obfd));
18159 }
18160 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18161 dwarf2_per_objfile);
18162
18163 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18164 load_partial_comp_unit (per_cu);
18165
18166 per_cu->cu->last_used = 0;
18167 pd = per_cu->cu->find_partial_die (sect_off);
18168 }
18169
18170 /* If we didn't find it, and not all dies have been loaded,
18171 load them all and try again. */
18172
18173 if (pd == NULL && per_cu->load_all_dies == 0)
18174 {
18175 per_cu->load_all_dies = 1;
18176
18177 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18178 THIS_CU->cu may already be in use. So we can't just free it and
18179 replace its DIEs with the ones we read in. Instead, we leave those
18180 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18181 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18182 set. */
18183 load_partial_comp_unit (per_cu);
18184
18185 pd = per_cu->cu->find_partial_die (sect_off);
18186 }
18187
18188 if (pd == NULL)
18189 internal_error (__FILE__, __LINE__,
18190 _("could not find partial DIE %s "
18191 "in cache [from module %s]\n"),
18192 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18193 return { per_cu->cu, pd };
18194 }
18195
18196 /* See if we can figure out if the class lives in a namespace. We do
18197 this by looking for a member function; its demangled name will
18198 contain namespace info, if there is any. */
18199
18200 static void
18201 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18202 struct dwarf2_cu *cu)
18203 {
18204 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18205 what template types look like, because the demangler
18206 frequently doesn't give the same name as the debug info. We
18207 could fix this by only using the demangled name to get the
18208 prefix (but see comment in read_structure_type). */
18209
18210 struct partial_die_info *real_pdi;
18211 struct partial_die_info *child_pdi;
18212
18213 /* If this DIE (this DIE's specification, if any) has a parent, then
18214 we should not do this. We'll prepend the parent's fully qualified
18215 name when we create the partial symbol. */
18216
18217 real_pdi = struct_pdi;
18218 while (real_pdi->has_specification)
18219 {
18220 auto res = find_partial_die (real_pdi->spec_offset,
18221 real_pdi->spec_is_dwz, cu);
18222 real_pdi = res.pdi;
18223 cu = res.cu;
18224 }
18225
18226 if (real_pdi->die_parent != NULL)
18227 return;
18228
18229 for (child_pdi = struct_pdi->die_child;
18230 child_pdi != NULL;
18231 child_pdi = child_pdi->die_sibling)
18232 {
18233 if (child_pdi->tag == DW_TAG_subprogram
18234 && child_pdi->linkage_name != NULL)
18235 {
18236 gdb::unique_xmalloc_ptr<char> actual_class_name
18237 (language_class_name_from_physname (cu->language_defn,
18238 child_pdi->linkage_name));
18239 if (actual_class_name != NULL)
18240 {
18241 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18242 struct_pdi->name = objfile->intern (actual_class_name.get ());
18243 }
18244 break;
18245 }
18246 }
18247 }
18248
18249 void
18250 partial_die_info::fixup (struct dwarf2_cu *cu)
18251 {
18252 /* Once we've fixed up a die, there's no point in doing so again.
18253 This also avoids a memory leak if we were to call
18254 guess_partial_die_structure_name multiple times. */
18255 if (fixup_called)
18256 return;
18257
18258 /* If we found a reference attribute and the DIE has no name, try
18259 to find a name in the referred to DIE. */
18260
18261 if (name == NULL && has_specification)
18262 {
18263 struct partial_die_info *spec_die;
18264
18265 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18266 spec_die = res.pdi;
18267 cu = res.cu;
18268
18269 spec_die->fixup (cu);
18270
18271 if (spec_die->name)
18272 {
18273 name = spec_die->name;
18274
18275 /* Copy DW_AT_external attribute if it is set. */
18276 if (spec_die->is_external)
18277 is_external = spec_die->is_external;
18278 }
18279 }
18280
18281 /* Set default names for some unnamed DIEs. */
18282
18283 if (name == NULL && tag == DW_TAG_namespace)
18284 name = CP_ANONYMOUS_NAMESPACE_STR;
18285
18286 /* If there is no parent die to provide a namespace, and there are
18287 children, see if we can determine the namespace from their linkage
18288 name. */
18289 if (cu->language == language_cplus
18290 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18291 && die_parent == NULL
18292 && has_children
18293 && (tag == DW_TAG_class_type
18294 || tag == DW_TAG_structure_type
18295 || tag == DW_TAG_union_type))
18296 guess_partial_die_structure_name (this, cu);
18297
18298 /* GCC might emit a nameless struct or union that has a linkage
18299 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18300 if (name == NULL
18301 && (tag == DW_TAG_class_type
18302 || tag == DW_TAG_interface_type
18303 || tag == DW_TAG_structure_type
18304 || tag == DW_TAG_union_type)
18305 && linkage_name != NULL)
18306 {
18307 gdb::unique_xmalloc_ptr<char> demangled
18308 (gdb_demangle (linkage_name, DMGL_TYPES));
18309 if (demangled != nullptr)
18310 {
18311 const char *base;
18312
18313 /* Strip any leading namespaces/classes, keep only the base name.
18314 DW_AT_name for named DIEs does not contain the prefixes. */
18315 base = strrchr (demangled.get (), ':');
18316 if (base && base > demangled.get () && base[-1] == ':')
18317 base++;
18318 else
18319 base = demangled.get ();
18320
18321 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18322 name = objfile->intern (base);
18323 }
18324 }
18325
18326 fixup_called = 1;
18327 }
18328
18329 /* Process the attributes that had to be skipped in the first round. These
18330 attributes are the ones that need str_offsets_base or addr_base attributes.
18331 They could not have been processed in the first round, because at the time
18332 the values of str_offsets_base or addr_base may not have been known. */
18333 static void
18334 read_attribute_reprocess (const struct die_reader_specs *reader,
18335 struct attribute *attr)
18336 {
18337 struct dwarf2_cu *cu = reader->cu;
18338 switch (attr->form)
18339 {
18340 case DW_FORM_addrx:
18341 case DW_FORM_GNU_addr_index:
18342 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18343 break;
18344 case DW_FORM_strx:
18345 case DW_FORM_strx1:
18346 case DW_FORM_strx2:
18347 case DW_FORM_strx3:
18348 case DW_FORM_strx4:
18349 case DW_FORM_GNU_str_index:
18350 {
18351 unsigned int str_index = DW_UNSND (attr);
18352 if (reader->dwo_file != NULL)
18353 {
18354 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18355 DW_STRING_IS_CANONICAL (attr) = 0;
18356 }
18357 else
18358 {
18359 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18360 DW_STRING_IS_CANONICAL (attr) = 0;
18361 }
18362 break;
18363 }
18364 default:
18365 gdb_assert_not_reached (_("Unexpected DWARF form."));
18366 }
18367 }
18368
18369 /* Read an attribute value described by an attribute form. */
18370
18371 static const gdb_byte *
18372 read_attribute_value (const struct die_reader_specs *reader,
18373 struct attribute *attr, unsigned form,
18374 LONGEST implicit_const, const gdb_byte *info_ptr,
18375 bool *need_reprocess)
18376 {
18377 struct dwarf2_cu *cu = reader->cu;
18378 struct dwarf2_per_objfile *dwarf2_per_objfile
18379 = cu->per_cu->dwarf2_per_objfile;
18380 struct objfile *objfile = dwarf2_per_objfile->objfile;
18381 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18382 bfd *abfd = reader->abfd;
18383 struct comp_unit_head *cu_header = &cu->header;
18384 unsigned int bytes_read;
18385 struct dwarf_block *blk;
18386 *need_reprocess = false;
18387
18388 attr->form = (enum dwarf_form) form;
18389 switch (form)
18390 {
18391 case DW_FORM_ref_addr:
18392 if (cu->header.version == 2)
18393 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18394 &bytes_read);
18395 else
18396 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18397 &bytes_read);
18398 info_ptr += bytes_read;
18399 break;
18400 case DW_FORM_GNU_ref_alt:
18401 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18402 info_ptr += bytes_read;
18403 break;
18404 case DW_FORM_addr:
18405 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18406 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18407 info_ptr += bytes_read;
18408 break;
18409 case DW_FORM_block2:
18410 blk = dwarf_alloc_block (cu);
18411 blk->size = read_2_bytes (abfd, info_ptr);
18412 info_ptr += 2;
18413 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18414 info_ptr += blk->size;
18415 DW_BLOCK (attr) = blk;
18416 break;
18417 case DW_FORM_block4:
18418 blk = dwarf_alloc_block (cu);
18419 blk->size = read_4_bytes (abfd, info_ptr);
18420 info_ptr += 4;
18421 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18422 info_ptr += blk->size;
18423 DW_BLOCK (attr) = blk;
18424 break;
18425 case DW_FORM_data2:
18426 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18427 info_ptr += 2;
18428 break;
18429 case DW_FORM_data4:
18430 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18431 info_ptr += 4;
18432 break;
18433 case DW_FORM_data8:
18434 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18435 info_ptr += 8;
18436 break;
18437 case DW_FORM_data16:
18438 blk = dwarf_alloc_block (cu);
18439 blk->size = 16;
18440 blk->data = read_n_bytes (abfd, info_ptr, 16);
18441 info_ptr += 16;
18442 DW_BLOCK (attr) = blk;
18443 break;
18444 case DW_FORM_sec_offset:
18445 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18446 info_ptr += bytes_read;
18447 break;
18448 case DW_FORM_string:
18449 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18450 DW_STRING_IS_CANONICAL (attr) = 0;
18451 info_ptr += bytes_read;
18452 break;
18453 case DW_FORM_strp:
18454 if (!cu->per_cu->is_dwz)
18455 {
18456 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18457 abfd, info_ptr, cu_header,
18458 &bytes_read);
18459 DW_STRING_IS_CANONICAL (attr) = 0;
18460 info_ptr += bytes_read;
18461 break;
18462 }
18463 /* FALLTHROUGH */
18464 case DW_FORM_line_strp:
18465 if (!cu->per_cu->is_dwz)
18466 {
18467 DW_STRING (attr)
18468 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18469 &bytes_read);
18470 DW_STRING_IS_CANONICAL (attr) = 0;
18471 info_ptr += bytes_read;
18472 break;
18473 }
18474 /* FALLTHROUGH */
18475 case DW_FORM_GNU_strp_alt:
18476 {
18477 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18478 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18479 &bytes_read);
18480
18481 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18482 DW_STRING_IS_CANONICAL (attr) = 0;
18483 info_ptr += bytes_read;
18484 }
18485 break;
18486 case DW_FORM_exprloc:
18487 case DW_FORM_block:
18488 blk = dwarf_alloc_block (cu);
18489 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18490 info_ptr += bytes_read;
18491 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18492 info_ptr += blk->size;
18493 DW_BLOCK (attr) = blk;
18494 break;
18495 case DW_FORM_block1:
18496 blk = dwarf_alloc_block (cu);
18497 blk->size = read_1_byte (abfd, info_ptr);
18498 info_ptr += 1;
18499 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18500 info_ptr += blk->size;
18501 DW_BLOCK (attr) = blk;
18502 break;
18503 case DW_FORM_data1:
18504 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18505 info_ptr += 1;
18506 break;
18507 case DW_FORM_flag:
18508 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18509 info_ptr += 1;
18510 break;
18511 case DW_FORM_flag_present:
18512 DW_UNSND (attr) = 1;
18513 break;
18514 case DW_FORM_sdata:
18515 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18516 info_ptr += bytes_read;
18517 break;
18518 case DW_FORM_udata:
18519 case DW_FORM_rnglistx:
18520 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18521 info_ptr += bytes_read;
18522 break;
18523 case DW_FORM_ref1:
18524 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18525 + read_1_byte (abfd, info_ptr));
18526 info_ptr += 1;
18527 break;
18528 case DW_FORM_ref2:
18529 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18530 + read_2_bytes (abfd, info_ptr));
18531 info_ptr += 2;
18532 break;
18533 case DW_FORM_ref4:
18534 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18535 + read_4_bytes (abfd, info_ptr));
18536 info_ptr += 4;
18537 break;
18538 case DW_FORM_ref8:
18539 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18540 + read_8_bytes (abfd, info_ptr));
18541 info_ptr += 8;
18542 break;
18543 case DW_FORM_ref_sig8:
18544 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18545 info_ptr += 8;
18546 break;
18547 case DW_FORM_ref_udata:
18548 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18549 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18550 info_ptr += bytes_read;
18551 break;
18552 case DW_FORM_indirect:
18553 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18554 info_ptr += bytes_read;
18555 if (form == DW_FORM_implicit_const)
18556 {
18557 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18558 info_ptr += bytes_read;
18559 }
18560 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18561 info_ptr, need_reprocess);
18562 break;
18563 case DW_FORM_implicit_const:
18564 DW_SND (attr) = implicit_const;
18565 break;
18566 case DW_FORM_addrx:
18567 case DW_FORM_GNU_addr_index:
18568 *need_reprocess = true;
18569 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18570 info_ptr += bytes_read;
18571 break;
18572 case DW_FORM_strx:
18573 case DW_FORM_strx1:
18574 case DW_FORM_strx2:
18575 case DW_FORM_strx3:
18576 case DW_FORM_strx4:
18577 case DW_FORM_GNU_str_index:
18578 {
18579 ULONGEST str_index;
18580 if (form == DW_FORM_strx1)
18581 {
18582 str_index = read_1_byte (abfd, info_ptr);
18583 info_ptr += 1;
18584 }
18585 else if (form == DW_FORM_strx2)
18586 {
18587 str_index = read_2_bytes (abfd, info_ptr);
18588 info_ptr += 2;
18589 }
18590 else if (form == DW_FORM_strx3)
18591 {
18592 str_index = read_3_bytes (abfd, info_ptr);
18593 info_ptr += 3;
18594 }
18595 else if (form == DW_FORM_strx4)
18596 {
18597 str_index = read_4_bytes (abfd, info_ptr);
18598 info_ptr += 4;
18599 }
18600 else
18601 {
18602 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18603 info_ptr += bytes_read;
18604 }
18605 *need_reprocess = true;
18606 DW_UNSND (attr) = str_index;
18607 }
18608 break;
18609 default:
18610 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18611 dwarf_form_name (form),
18612 bfd_get_filename (abfd));
18613 }
18614
18615 /* Super hack. */
18616 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18617 attr->form = DW_FORM_GNU_ref_alt;
18618
18619 /* We have seen instances where the compiler tried to emit a byte
18620 size attribute of -1 which ended up being encoded as an unsigned
18621 0xffffffff. Although 0xffffffff is technically a valid size value,
18622 an object of this size seems pretty unlikely so we can relatively
18623 safely treat these cases as if the size attribute was invalid and
18624 treat them as zero by default. */
18625 if (attr->name == DW_AT_byte_size
18626 && form == DW_FORM_data4
18627 && DW_UNSND (attr) >= 0xffffffff)
18628 {
18629 complaint
18630 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18631 hex_string (DW_UNSND (attr)));
18632 DW_UNSND (attr) = 0;
18633 }
18634
18635 return info_ptr;
18636 }
18637
18638 /* Read an attribute described by an abbreviated attribute. */
18639
18640 static const gdb_byte *
18641 read_attribute (const struct die_reader_specs *reader,
18642 struct attribute *attr, struct attr_abbrev *abbrev,
18643 const gdb_byte *info_ptr, bool *need_reprocess)
18644 {
18645 attr->name = abbrev->name;
18646 return read_attribute_value (reader, attr, abbrev->form,
18647 abbrev->implicit_const, info_ptr,
18648 need_reprocess);
18649 }
18650
18651 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18652
18653 static const char *
18654 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18655 LONGEST str_offset)
18656 {
18657 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18658 str_offset, "DW_FORM_strp");
18659 }
18660
18661 /* Return pointer to string at .debug_str offset as read from BUF.
18662 BUF is assumed to be in a compilation unit described by CU_HEADER.
18663 Return *BYTES_READ_PTR count of bytes read from BUF. */
18664
18665 static const char *
18666 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18667 const gdb_byte *buf,
18668 const struct comp_unit_head *cu_header,
18669 unsigned int *bytes_read_ptr)
18670 {
18671 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18672
18673 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
18674 }
18675
18676 /* See read.h. */
18677
18678 const char *
18679 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
18680 const struct comp_unit_head *cu_header,
18681 unsigned int *bytes_read_ptr)
18682 {
18683 bfd *abfd = objfile->obfd;
18684 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18685
18686 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
18687 }
18688
18689 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18690 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18691 ADDR_SIZE is the size of addresses from the CU header. */
18692
18693 static CORE_ADDR
18694 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18695 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18696 int addr_size)
18697 {
18698 struct objfile *objfile = dwarf2_per_objfile->objfile;
18699 bfd *abfd = objfile->obfd;
18700 const gdb_byte *info_ptr;
18701 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18702
18703 dwarf2_per_objfile->addr.read (objfile);
18704 if (dwarf2_per_objfile->addr.buffer == NULL)
18705 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18706 objfile_name (objfile));
18707 if (addr_base_or_zero + addr_index * addr_size
18708 >= dwarf2_per_objfile->addr.size)
18709 error (_("DW_FORM_addr_index pointing outside of "
18710 ".debug_addr section [in module %s]"),
18711 objfile_name (objfile));
18712 info_ptr = (dwarf2_per_objfile->addr.buffer
18713 + addr_base_or_zero + addr_index * addr_size);
18714 if (addr_size == 4)
18715 return bfd_get_32 (abfd, info_ptr);
18716 else
18717 return bfd_get_64 (abfd, info_ptr);
18718 }
18719
18720 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18721
18722 static CORE_ADDR
18723 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18724 {
18725 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18726 cu->addr_base, cu->header.addr_size);
18727 }
18728
18729 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18730
18731 static CORE_ADDR
18732 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18733 unsigned int *bytes_read)
18734 {
18735 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18736 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18737
18738 return read_addr_index (cu, addr_index);
18739 }
18740
18741 /* See read.h. */
18742
18743 CORE_ADDR
18744 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18745 {
18746 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18747 struct dwarf2_cu *cu = per_cu->cu;
18748 gdb::optional<ULONGEST> addr_base;
18749 int addr_size;
18750
18751 /* We need addr_base and addr_size.
18752 If we don't have PER_CU->cu, we have to get it.
18753 Nasty, but the alternative is storing the needed info in PER_CU,
18754 which at this point doesn't seem justified: it's not clear how frequently
18755 it would get used and it would increase the size of every PER_CU.
18756 Entry points like dwarf2_per_cu_addr_size do a similar thing
18757 so we're not in uncharted territory here.
18758 Alas we need to be a bit more complicated as addr_base is contained
18759 in the DIE.
18760
18761 We don't need to read the entire CU(/TU).
18762 We just need the header and top level die.
18763
18764 IWBN to use the aging mechanism to let us lazily later discard the CU.
18765 For now we skip this optimization. */
18766
18767 if (cu != NULL)
18768 {
18769 addr_base = cu->addr_base;
18770 addr_size = cu->header.addr_size;
18771 }
18772 else
18773 {
18774 cutu_reader reader (per_cu, NULL, 0, false);
18775 addr_base = reader.cu->addr_base;
18776 addr_size = reader.cu->header.addr_size;
18777 }
18778
18779 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18780 addr_size);
18781 }
18782
18783 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18784 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18785 DWO file. */
18786
18787 static const char *
18788 read_str_index (struct dwarf2_cu *cu,
18789 struct dwarf2_section_info *str_section,
18790 struct dwarf2_section_info *str_offsets_section,
18791 ULONGEST str_offsets_base, ULONGEST str_index)
18792 {
18793 struct dwarf2_per_objfile *dwarf2_per_objfile
18794 = cu->per_cu->dwarf2_per_objfile;
18795 struct objfile *objfile = dwarf2_per_objfile->objfile;
18796 const char *objf_name = objfile_name (objfile);
18797 bfd *abfd = objfile->obfd;
18798 const gdb_byte *info_ptr;
18799 ULONGEST str_offset;
18800 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18801
18802 str_section->read (objfile);
18803 str_offsets_section->read (objfile);
18804 if (str_section->buffer == NULL)
18805 error (_("%s used without %s section"
18806 " in CU at offset %s [in module %s]"),
18807 form_name, str_section->get_name (),
18808 sect_offset_str (cu->header.sect_off), objf_name);
18809 if (str_offsets_section->buffer == NULL)
18810 error (_("%s used without %s section"
18811 " in CU at offset %s [in module %s]"),
18812 form_name, str_section->get_name (),
18813 sect_offset_str (cu->header.sect_off), objf_name);
18814 info_ptr = (str_offsets_section->buffer
18815 + str_offsets_base
18816 + str_index * cu->header.offset_size);
18817 if (cu->header.offset_size == 4)
18818 str_offset = bfd_get_32 (abfd, info_ptr);
18819 else
18820 str_offset = bfd_get_64 (abfd, info_ptr);
18821 if (str_offset >= str_section->size)
18822 error (_("Offset from %s pointing outside of"
18823 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18824 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18825 return (const char *) (str_section->buffer + str_offset);
18826 }
18827
18828 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18829
18830 static const char *
18831 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18832 {
18833 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18834 ? reader->cu->header.addr_size : 0;
18835 return read_str_index (reader->cu,
18836 &reader->dwo_file->sections.str,
18837 &reader->dwo_file->sections.str_offsets,
18838 str_offsets_base, str_index);
18839 }
18840
18841 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18842
18843 static const char *
18844 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18845 {
18846 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18847 const char *objf_name = objfile_name (objfile);
18848 static const char form_name[] = "DW_FORM_GNU_str_index";
18849 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18850
18851 if (!cu->str_offsets_base.has_value ())
18852 error (_("%s used in Fission stub without %s"
18853 " in CU at offset 0x%lx [in module %s]"),
18854 form_name, str_offsets_attr_name,
18855 (long) cu->header.offset_size, objf_name);
18856
18857 return read_str_index (cu,
18858 &cu->per_cu->dwarf2_per_objfile->str,
18859 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18860 *cu->str_offsets_base, str_index);
18861 }
18862
18863 /* Return the length of an LEB128 number in BUF. */
18864
18865 static int
18866 leb128_size (const gdb_byte *buf)
18867 {
18868 const gdb_byte *begin = buf;
18869 gdb_byte byte;
18870
18871 while (1)
18872 {
18873 byte = *buf++;
18874 if ((byte & 128) == 0)
18875 return buf - begin;
18876 }
18877 }
18878
18879 static void
18880 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18881 {
18882 switch (lang)
18883 {
18884 case DW_LANG_C89:
18885 case DW_LANG_C99:
18886 case DW_LANG_C11:
18887 case DW_LANG_C:
18888 case DW_LANG_UPC:
18889 cu->language = language_c;
18890 break;
18891 case DW_LANG_Java:
18892 case DW_LANG_C_plus_plus:
18893 case DW_LANG_C_plus_plus_11:
18894 case DW_LANG_C_plus_plus_14:
18895 cu->language = language_cplus;
18896 break;
18897 case DW_LANG_D:
18898 cu->language = language_d;
18899 break;
18900 case DW_LANG_Fortran77:
18901 case DW_LANG_Fortran90:
18902 case DW_LANG_Fortran95:
18903 case DW_LANG_Fortran03:
18904 case DW_LANG_Fortran08:
18905 cu->language = language_fortran;
18906 break;
18907 case DW_LANG_Go:
18908 cu->language = language_go;
18909 break;
18910 case DW_LANG_Mips_Assembler:
18911 cu->language = language_asm;
18912 break;
18913 case DW_LANG_Ada83:
18914 case DW_LANG_Ada95:
18915 cu->language = language_ada;
18916 break;
18917 case DW_LANG_Modula2:
18918 cu->language = language_m2;
18919 break;
18920 case DW_LANG_Pascal83:
18921 cu->language = language_pascal;
18922 break;
18923 case DW_LANG_ObjC:
18924 cu->language = language_objc;
18925 break;
18926 case DW_LANG_Rust:
18927 case DW_LANG_Rust_old:
18928 cu->language = language_rust;
18929 break;
18930 case DW_LANG_Cobol74:
18931 case DW_LANG_Cobol85:
18932 default:
18933 cu->language = language_minimal;
18934 break;
18935 }
18936 cu->language_defn = language_def (cu->language);
18937 }
18938
18939 /* Return the named attribute or NULL if not there. */
18940
18941 static struct attribute *
18942 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18943 {
18944 for (;;)
18945 {
18946 unsigned int i;
18947 struct attribute *spec = NULL;
18948
18949 for (i = 0; i < die->num_attrs; ++i)
18950 {
18951 if (die->attrs[i].name == name)
18952 return &die->attrs[i];
18953 if (die->attrs[i].name == DW_AT_specification
18954 || die->attrs[i].name == DW_AT_abstract_origin)
18955 spec = &die->attrs[i];
18956 }
18957
18958 if (!spec)
18959 break;
18960
18961 die = follow_die_ref (die, spec, &cu);
18962 }
18963
18964 return NULL;
18965 }
18966
18967 /* Return the string associated with a string-typed attribute, or NULL if it
18968 is either not found or is of an incorrect type. */
18969
18970 static const char *
18971 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18972 {
18973 struct attribute *attr;
18974 const char *str = NULL;
18975
18976 attr = dwarf2_attr (die, name, cu);
18977
18978 if (attr != NULL)
18979 {
18980 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
18981 || attr->form == DW_FORM_string
18982 || attr->form == DW_FORM_strx
18983 || attr->form == DW_FORM_strx1
18984 || attr->form == DW_FORM_strx2
18985 || attr->form == DW_FORM_strx3
18986 || attr->form == DW_FORM_strx4
18987 || attr->form == DW_FORM_GNU_str_index
18988 || attr->form == DW_FORM_GNU_strp_alt)
18989 str = DW_STRING (attr);
18990 else
18991 complaint (_("string type expected for attribute %s for "
18992 "DIE at %s in module %s"),
18993 dwarf_attr_name (name), sect_offset_str (die->sect_off),
18994 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18995 }
18996
18997 return str;
18998 }
18999
19000 /* Return the dwo name or NULL if not present. If present, it is in either
19001 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19002 static const char *
19003 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19004 {
19005 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19006 if (dwo_name == nullptr)
19007 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19008 return dwo_name;
19009 }
19010
19011 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19012 and holds a non-zero value. This function should only be used for
19013 DW_FORM_flag or DW_FORM_flag_present attributes. */
19014
19015 static int
19016 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19017 {
19018 struct attribute *attr = dwarf2_attr (die, name, cu);
19019
19020 return (attr && DW_UNSND (attr));
19021 }
19022
19023 static int
19024 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19025 {
19026 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19027 which value is non-zero. However, we have to be careful with
19028 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19029 (via dwarf2_flag_true_p) follows this attribute. So we may
19030 end up accidently finding a declaration attribute that belongs
19031 to a different DIE referenced by the specification attribute,
19032 even though the given DIE does not have a declaration attribute. */
19033 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19034 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19035 }
19036
19037 /* Return the die giving the specification for DIE, if there is
19038 one. *SPEC_CU is the CU containing DIE on input, and the CU
19039 containing the return value on output. If there is no
19040 specification, but there is an abstract origin, that is
19041 returned. */
19042
19043 static struct die_info *
19044 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19045 {
19046 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19047 *spec_cu);
19048
19049 if (spec_attr == NULL)
19050 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19051
19052 if (spec_attr == NULL)
19053 return NULL;
19054 else
19055 return follow_die_ref (die, spec_attr, spec_cu);
19056 }
19057
19058 /* Stub for free_line_header to match void * callback types. */
19059
19060 static void
19061 free_line_header_voidp (void *arg)
19062 {
19063 struct line_header *lh = (struct line_header *) arg;
19064
19065 delete lh;
19066 }
19067
19068 /* A convenience function to find the proper .debug_line section for a CU. */
19069
19070 static struct dwarf2_section_info *
19071 get_debug_line_section (struct dwarf2_cu *cu)
19072 {
19073 struct dwarf2_section_info *section;
19074 struct dwarf2_per_objfile *dwarf2_per_objfile
19075 = cu->per_cu->dwarf2_per_objfile;
19076
19077 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19078 DWO file. */
19079 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19080 section = &cu->dwo_unit->dwo_file->sections.line;
19081 else if (cu->per_cu->is_dwz)
19082 {
19083 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19084
19085 section = &dwz->line;
19086 }
19087 else
19088 section = &dwarf2_per_objfile->line;
19089
19090 return section;
19091 }
19092
19093 /* Read the statement program header starting at OFFSET in
19094 .debug_line, or .debug_line.dwo. Return a pointer
19095 to a struct line_header, allocated using xmalloc.
19096 Returns NULL if there is a problem reading the header, e.g., if it
19097 has a version we don't understand.
19098
19099 NOTE: the strings in the include directory and file name tables of
19100 the returned object point into the dwarf line section buffer,
19101 and must not be freed. */
19102
19103 static line_header_up
19104 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19105 {
19106 struct dwarf2_section_info *section;
19107 struct dwarf2_per_objfile *dwarf2_per_objfile
19108 = cu->per_cu->dwarf2_per_objfile;
19109
19110 section = get_debug_line_section (cu);
19111 section->read (dwarf2_per_objfile->objfile);
19112 if (section->buffer == NULL)
19113 {
19114 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19115 complaint (_("missing .debug_line.dwo section"));
19116 else
19117 complaint (_("missing .debug_line section"));
19118 return 0;
19119 }
19120
19121 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19122 dwarf2_per_objfile, section,
19123 &cu->header);
19124 }
19125
19126 /* Subroutine of dwarf_decode_lines to simplify it.
19127 Return the file name of the psymtab for the given file_entry.
19128 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19129 If space for the result is malloc'd, *NAME_HOLDER will be set.
19130 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19131
19132 static const char *
19133 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19134 const dwarf2_psymtab *pst,
19135 const char *comp_dir,
19136 gdb::unique_xmalloc_ptr<char> *name_holder)
19137 {
19138 const char *include_name = fe.name;
19139 const char *include_name_to_compare = include_name;
19140 const char *pst_filename;
19141 int file_is_pst;
19142
19143 const char *dir_name = fe.include_dir (lh);
19144
19145 gdb::unique_xmalloc_ptr<char> hold_compare;
19146 if (!IS_ABSOLUTE_PATH (include_name)
19147 && (dir_name != NULL || comp_dir != NULL))
19148 {
19149 /* Avoid creating a duplicate psymtab for PST.
19150 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19151 Before we do the comparison, however, we need to account
19152 for DIR_NAME and COMP_DIR.
19153 First prepend dir_name (if non-NULL). If we still don't
19154 have an absolute path prepend comp_dir (if non-NULL).
19155 However, the directory we record in the include-file's
19156 psymtab does not contain COMP_DIR (to match the
19157 corresponding symtab(s)).
19158
19159 Example:
19160
19161 bash$ cd /tmp
19162 bash$ gcc -g ./hello.c
19163 include_name = "hello.c"
19164 dir_name = "."
19165 DW_AT_comp_dir = comp_dir = "/tmp"
19166 DW_AT_name = "./hello.c"
19167
19168 */
19169
19170 if (dir_name != NULL)
19171 {
19172 name_holder->reset (concat (dir_name, SLASH_STRING,
19173 include_name, (char *) NULL));
19174 include_name = name_holder->get ();
19175 include_name_to_compare = include_name;
19176 }
19177 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19178 {
19179 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19180 include_name, (char *) NULL));
19181 include_name_to_compare = hold_compare.get ();
19182 }
19183 }
19184
19185 pst_filename = pst->filename;
19186 gdb::unique_xmalloc_ptr<char> copied_name;
19187 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19188 {
19189 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19190 pst_filename, (char *) NULL));
19191 pst_filename = copied_name.get ();
19192 }
19193
19194 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19195
19196 if (file_is_pst)
19197 return NULL;
19198 return include_name;
19199 }
19200
19201 /* State machine to track the state of the line number program. */
19202
19203 class lnp_state_machine
19204 {
19205 public:
19206 /* Initialize a machine state for the start of a line number
19207 program. */
19208 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19209 bool record_lines_p);
19210
19211 file_entry *current_file ()
19212 {
19213 /* lh->file_names is 0-based, but the file name numbers in the
19214 statement program are 1-based. */
19215 return m_line_header->file_name_at (m_file);
19216 }
19217
19218 /* Record the line in the state machine. END_SEQUENCE is true if
19219 we're processing the end of a sequence. */
19220 void record_line (bool end_sequence);
19221
19222 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19223 nop-out rest of the lines in this sequence. */
19224 void check_line_address (struct dwarf2_cu *cu,
19225 const gdb_byte *line_ptr,
19226 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19227
19228 void handle_set_discriminator (unsigned int discriminator)
19229 {
19230 m_discriminator = discriminator;
19231 m_line_has_non_zero_discriminator |= discriminator != 0;
19232 }
19233
19234 /* Handle DW_LNE_set_address. */
19235 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19236 {
19237 m_op_index = 0;
19238 address += baseaddr;
19239 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19240 }
19241
19242 /* Handle DW_LNS_advance_pc. */
19243 void handle_advance_pc (CORE_ADDR adjust);
19244
19245 /* Handle a special opcode. */
19246 void handle_special_opcode (unsigned char op_code);
19247
19248 /* Handle DW_LNS_advance_line. */
19249 void handle_advance_line (int line_delta)
19250 {
19251 advance_line (line_delta);
19252 }
19253
19254 /* Handle DW_LNS_set_file. */
19255 void handle_set_file (file_name_index file);
19256
19257 /* Handle DW_LNS_negate_stmt. */
19258 void handle_negate_stmt ()
19259 {
19260 m_is_stmt = !m_is_stmt;
19261 }
19262
19263 /* Handle DW_LNS_const_add_pc. */
19264 void handle_const_add_pc ();
19265
19266 /* Handle DW_LNS_fixed_advance_pc. */
19267 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19268 {
19269 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19270 m_op_index = 0;
19271 }
19272
19273 /* Handle DW_LNS_copy. */
19274 void handle_copy ()
19275 {
19276 record_line (false);
19277 m_discriminator = 0;
19278 }
19279
19280 /* Handle DW_LNE_end_sequence. */
19281 void handle_end_sequence ()
19282 {
19283 m_currently_recording_lines = true;
19284 }
19285
19286 private:
19287 /* Advance the line by LINE_DELTA. */
19288 void advance_line (int line_delta)
19289 {
19290 m_line += line_delta;
19291
19292 if (line_delta != 0)
19293 m_line_has_non_zero_discriminator = m_discriminator != 0;
19294 }
19295
19296 struct dwarf2_cu *m_cu;
19297
19298 gdbarch *m_gdbarch;
19299
19300 /* True if we're recording lines.
19301 Otherwise we're building partial symtabs and are just interested in
19302 finding include files mentioned by the line number program. */
19303 bool m_record_lines_p;
19304
19305 /* The line number header. */
19306 line_header *m_line_header;
19307
19308 /* These are part of the standard DWARF line number state machine,
19309 and initialized according to the DWARF spec. */
19310
19311 unsigned char m_op_index = 0;
19312 /* The line table index of the current file. */
19313 file_name_index m_file = 1;
19314 unsigned int m_line = 1;
19315
19316 /* These are initialized in the constructor. */
19317
19318 CORE_ADDR m_address;
19319 bool m_is_stmt;
19320 unsigned int m_discriminator;
19321
19322 /* Additional bits of state we need to track. */
19323
19324 /* The last file that we called dwarf2_start_subfile for.
19325 This is only used for TLLs. */
19326 unsigned int m_last_file = 0;
19327 /* The last file a line number was recorded for. */
19328 struct subfile *m_last_subfile = NULL;
19329
19330 /* When true, record the lines we decode. */
19331 bool m_currently_recording_lines = false;
19332
19333 /* The last line number that was recorded, used to coalesce
19334 consecutive entries for the same line. This can happen, for
19335 example, when discriminators are present. PR 17276. */
19336 unsigned int m_last_line = 0;
19337 bool m_line_has_non_zero_discriminator = false;
19338 };
19339
19340 void
19341 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19342 {
19343 CORE_ADDR addr_adj = (((m_op_index + adjust)
19344 / m_line_header->maximum_ops_per_instruction)
19345 * m_line_header->minimum_instruction_length);
19346 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19347 m_op_index = ((m_op_index + adjust)
19348 % m_line_header->maximum_ops_per_instruction);
19349 }
19350
19351 void
19352 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19353 {
19354 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19355 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19356 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19357 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19358 / m_line_header->maximum_ops_per_instruction)
19359 * m_line_header->minimum_instruction_length);
19360 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19361 m_op_index = ((m_op_index + adj_opcode_d)
19362 % m_line_header->maximum_ops_per_instruction);
19363
19364 int line_delta = m_line_header->line_base + adj_opcode_r;
19365 advance_line (line_delta);
19366 record_line (false);
19367 m_discriminator = 0;
19368 }
19369
19370 void
19371 lnp_state_machine::handle_set_file (file_name_index file)
19372 {
19373 m_file = file;
19374
19375 const file_entry *fe = current_file ();
19376 if (fe == NULL)
19377 dwarf2_debug_line_missing_file_complaint ();
19378 else if (m_record_lines_p)
19379 {
19380 const char *dir = fe->include_dir (m_line_header);
19381
19382 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19383 m_line_has_non_zero_discriminator = m_discriminator != 0;
19384 dwarf2_start_subfile (m_cu, fe->name, dir);
19385 }
19386 }
19387
19388 void
19389 lnp_state_machine::handle_const_add_pc ()
19390 {
19391 CORE_ADDR adjust
19392 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19393
19394 CORE_ADDR addr_adj
19395 = (((m_op_index + adjust)
19396 / m_line_header->maximum_ops_per_instruction)
19397 * m_line_header->minimum_instruction_length);
19398
19399 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19400 m_op_index = ((m_op_index + adjust)
19401 % m_line_header->maximum_ops_per_instruction);
19402 }
19403
19404 /* Return non-zero if we should add LINE to the line number table.
19405 LINE is the line to add, LAST_LINE is the last line that was added,
19406 LAST_SUBFILE is the subfile for LAST_LINE.
19407 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19408 had a non-zero discriminator.
19409
19410 We have to be careful in the presence of discriminators.
19411 E.g., for this line:
19412
19413 for (i = 0; i < 100000; i++);
19414
19415 clang can emit four line number entries for that one line,
19416 each with a different discriminator.
19417 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19418
19419 However, we want gdb to coalesce all four entries into one.
19420 Otherwise the user could stepi into the middle of the line and
19421 gdb would get confused about whether the pc really was in the
19422 middle of the line.
19423
19424 Things are further complicated by the fact that two consecutive
19425 line number entries for the same line is a heuristic used by gcc
19426 to denote the end of the prologue. So we can't just discard duplicate
19427 entries, we have to be selective about it. The heuristic we use is
19428 that we only collapse consecutive entries for the same line if at least
19429 one of those entries has a non-zero discriminator. PR 17276.
19430
19431 Note: Addresses in the line number state machine can never go backwards
19432 within one sequence, thus this coalescing is ok. */
19433
19434 static int
19435 dwarf_record_line_p (struct dwarf2_cu *cu,
19436 unsigned int line, unsigned int last_line,
19437 int line_has_non_zero_discriminator,
19438 struct subfile *last_subfile)
19439 {
19440 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19441 return 1;
19442 if (line != last_line)
19443 return 1;
19444 /* Same line for the same file that we've seen already.
19445 As a last check, for pr 17276, only record the line if the line
19446 has never had a non-zero discriminator. */
19447 if (!line_has_non_zero_discriminator)
19448 return 1;
19449 return 0;
19450 }
19451
19452 /* Use the CU's builder to record line number LINE beginning at
19453 address ADDRESS in the line table of subfile SUBFILE. */
19454
19455 static void
19456 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19457 unsigned int line, CORE_ADDR address, bool is_stmt,
19458 struct dwarf2_cu *cu)
19459 {
19460 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19461
19462 if (dwarf_line_debug)
19463 {
19464 fprintf_unfiltered (gdb_stdlog,
19465 "Recording line %u, file %s, address %s\n",
19466 line, lbasename (subfile->name),
19467 paddress (gdbarch, address));
19468 }
19469
19470 if (cu != nullptr)
19471 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19472 }
19473
19474 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19475 Mark the end of a set of line number records.
19476 The arguments are the same as for dwarf_record_line_1.
19477 If SUBFILE is NULL the request is ignored. */
19478
19479 static void
19480 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19481 CORE_ADDR address, struct dwarf2_cu *cu)
19482 {
19483 if (subfile == NULL)
19484 return;
19485
19486 if (dwarf_line_debug)
19487 {
19488 fprintf_unfiltered (gdb_stdlog,
19489 "Finishing current line, file %s, address %s\n",
19490 lbasename (subfile->name),
19491 paddress (gdbarch, address));
19492 }
19493
19494 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19495 }
19496
19497 void
19498 lnp_state_machine::record_line (bool end_sequence)
19499 {
19500 if (dwarf_line_debug)
19501 {
19502 fprintf_unfiltered (gdb_stdlog,
19503 "Processing actual line %u: file %u,"
19504 " address %s, is_stmt %u, discrim %u%s\n",
19505 m_line, m_file,
19506 paddress (m_gdbarch, m_address),
19507 m_is_stmt, m_discriminator,
19508 (end_sequence ? "\t(end sequence)" : ""));
19509 }
19510
19511 file_entry *fe = current_file ();
19512
19513 if (fe == NULL)
19514 dwarf2_debug_line_missing_file_complaint ();
19515 /* For now we ignore lines not starting on an instruction boundary.
19516 But not when processing end_sequence for compatibility with the
19517 previous version of the code. */
19518 else if (m_op_index == 0 || end_sequence)
19519 {
19520 fe->included_p = 1;
19521 if (m_record_lines_p)
19522 {
19523 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19524 || end_sequence)
19525 {
19526 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19527 m_currently_recording_lines ? m_cu : nullptr);
19528 }
19529
19530 if (!end_sequence)
19531 {
19532 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19533
19534 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19535 m_line_has_non_zero_discriminator,
19536 m_last_subfile))
19537 {
19538 buildsym_compunit *builder = m_cu->get_builder ();
19539 dwarf_record_line_1 (m_gdbarch,
19540 builder->get_current_subfile (),
19541 m_line, m_address, is_stmt,
19542 m_currently_recording_lines ? m_cu : nullptr);
19543 }
19544 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19545 m_last_line = m_line;
19546 }
19547 }
19548 }
19549 }
19550
19551 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19552 line_header *lh, bool record_lines_p)
19553 {
19554 m_cu = cu;
19555 m_gdbarch = arch;
19556 m_record_lines_p = record_lines_p;
19557 m_line_header = lh;
19558
19559 m_currently_recording_lines = true;
19560
19561 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19562 was a line entry for it so that the backend has a chance to adjust it
19563 and also record it in case it needs it. This is currently used by MIPS
19564 code, cf. `mips_adjust_dwarf2_line'. */
19565 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19566 m_is_stmt = lh->default_is_stmt;
19567 m_discriminator = 0;
19568 }
19569
19570 void
19571 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19572 const gdb_byte *line_ptr,
19573 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
19574 {
19575 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19576 the pc range of the CU. However, we restrict the test to only ADDRESS
19577 values of zero to preserve GDB's previous behaviour which is to handle
19578 the specific case of a function being GC'd by the linker. */
19579
19580 if (address == 0 && address < unrelocated_lowpc)
19581 {
19582 /* This line table is for a function which has been
19583 GCd by the linker. Ignore it. PR gdb/12528 */
19584
19585 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19586 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19587
19588 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19589 line_offset, objfile_name (objfile));
19590 m_currently_recording_lines = false;
19591 /* Note: m_currently_recording_lines is left as false until we see
19592 DW_LNE_end_sequence. */
19593 }
19594 }
19595
19596 /* Subroutine of dwarf_decode_lines to simplify it.
19597 Process the line number information in LH.
19598 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19599 program in order to set included_p for every referenced header. */
19600
19601 static void
19602 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19603 const int decode_for_pst_p, CORE_ADDR lowpc)
19604 {
19605 const gdb_byte *line_ptr, *extended_end;
19606 const gdb_byte *line_end;
19607 unsigned int bytes_read, extended_len;
19608 unsigned char op_code, extended_op;
19609 CORE_ADDR baseaddr;
19610 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19611 bfd *abfd = objfile->obfd;
19612 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19613 /* True if we're recording line info (as opposed to building partial
19614 symtabs and just interested in finding include files mentioned by
19615 the line number program). */
19616 bool record_lines_p = !decode_for_pst_p;
19617
19618 baseaddr = objfile->text_section_offset ();
19619
19620 line_ptr = lh->statement_program_start;
19621 line_end = lh->statement_program_end;
19622
19623 /* Read the statement sequences until there's nothing left. */
19624 while (line_ptr < line_end)
19625 {
19626 /* The DWARF line number program state machine. Reset the state
19627 machine at the start of each sequence. */
19628 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
19629 bool end_sequence = false;
19630
19631 if (record_lines_p)
19632 {
19633 /* Start a subfile for the current file of the state
19634 machine. */
19635 const file_entry *fe = state_machine.current_file ();
19636
19637 if (fe != NULL)
19638 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
19639 }
19640
19641 /* Decode the table. */
19642 while (line_ptr < line_end && !end_sequence)
19643 {
19644 op_code = read_1_byte (abfd, line_ptr);
19645 line_ptr += 1;
19646
19647 if (op_code >= lh->opcode_base)
19648 {
19649 /* Special opcode. */
19650 state_machine.handle_special_opcode (op_code);
19651 }
19652 else switch (op_code)
19653 {
19654 case DW_LNS_extended_op:
19655 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19656 &bytes_read);
19657 line_ptr += bytes_read;
19658 extended_end = line_ptr + extended_len;
19659 extended_op = read_1_byte (abfd, line_ptr);
19660 line_ptr += 1;
19661 switch (extended_op)
19662 {
19663 case DW_LNE_end_sequence:
19664 state_machine.handle_end_sequence ();
19665 end_sequence = true;
19666 break;
19667 case DW_LNE_set_address:
19668 {
19669 CORE_ADDR address
19670 = cu->header.read_address (abfd, line_ptr, &bytes_read);
19671 line_ptr += bytes_read;
19672
19673 state_machine.check_line_address (cu, line_ptr,
19674 lowpc - baseaddr, address);
19675 state_machine.handle_set_address (baseaddr, address);
19676 }
19677 break;
19678 case DW_LNE_define_file:
19679 {
19680 const char *cur_file;
19681 unsigned int mod_time, length;
19682 dir_index dindex;
19683
19684 cur_file = read_direct_string (abfd, line_ptr,
19685 &bytes_read);
19686 line_ptr += bytes_read;
19687 dindex = (dir_index)
19688 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19689 line_ptr += bytes_read;
19690 mod_time =
19691 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19692 line_ptr += bytes_read;
19693 length =
19694 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19695 line_ptr += bytes_read;
19696 lh->add_file_name (cur_file, dindex, mod_time, length);
19697 }
19698 break;
19699 case DW_LNE_set_discriminator:
19700 {
19701 /* The discriminator is not interesting to the
19702 debugger; just ignore it. We still need to
19703 check its value though:
19704 if there are consecutive entries for the same
19705 (non-prologue) line we want to coalesce them.
19706 PR 17276. */
19707 unsigned int discr
19708 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19709 line_ptr += bytes_read;
19710
19711 state_machine.handle_set_discriminator (discr);
19712 }
19713 break;
19714 default:
19715 complaint (_("mangled .debug_line section"));
19716 return;
19717 }
19718 /* Make sure that we parsed the extended op correctly. If e.g.
19719 we expected a different address size than the producer used,
19720 we may have read the wrong number of bytes. */
19721 if (line_ptr != extended_end)
19722 {
19723 complaint (_("mangled .debug_line section"));
19724 return;
19725 }
19726 break;
19727 case DW_LNS_copy:
19728 state_machine.handle_copy ();
19729 break;
19730 case DW_LNS_advance_pc:
19731 {
19732 CORE_ADDR adjust
19733 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19734 line_ptr += bytes_read;
19735
19736 state_machine.handle_advance_pc (adjust);
19737 }
19738 break;
19739 case DW_LNS_advance_line:
19740 {
19741 int line_delta
19742 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19743 line_ptr += bytes_read;
19744
19745 state_machine.handle_advance_line (line_delta);
19746 }
19747 break;
19748 case DW_LNS_set_file:
19749 {
19750 file_name_index file
19751 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19752 &bytes_read);
19753 line_ptr += bytes_read;
19754
19755 state_machine.handle_set_file (file);
19756 }
19757 break;
19758 case DW_LNS_set_column:
19759 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19760 line_ptr += bytes_read;
19761 break;
19762 case DW_LNS_negate_stmt:
19763 state_machine.handle_negate_stmt ();
19764 break;
19765 case DW_LNS_set_basic_block:
19766 break;
19767 /* Add to the address register of the state machine the
19768 address increment value corresponding to special opcode
19769 255. I.e., this value is scaled by the minimum
19770 instruction length since special opcode 255 would have
19771 scaled the increment. */
19772 case DW_LNS_const_add_pc:
19773 state_machine.handle_const_add_pc ();
19774 break;
19775 case DW_LNS_fixed_advance_pc:
19776 {
19777 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19778 line_ptr += 2;
19779
19780 state_machine.handle_fixed_advance_pc (addr_adj);
19781 }
19782 break;
19783 default:
19784 {
19785 /* Unknown standard opcode, ignore it. */
19786 int i;
19787
19788 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19789 {
19790 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19791 line_ptr += bytes_read;
19792 }
19793 }
19794 }
19795 }
19796
19797 if (!end_sequence)
19798 dwarf2_debug_line_missing_end_sequence_complaint ();
19799
19800 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19801 in which case we still finish recording the last line). */
19802 state_machine.record_line (true);
19803 }
19804 }
19805
19806 /* Decode the Line Number Program (LNP) for the given line_header
19807 structure and CU. The actual information extracted and the type
19808 of structures created from the LNP depends on the value of PST.
19809
19810 1. If PST is NULL, then this procedure uses the data from the program
19811 to create all necessary symbol tables, and their linetables.
19812
19813 2. If PST is not NULL, this procedure reads the program to determine
19814 the list of files included by the unit represented by PST, and
19815 builds all the associated partial symbol tables.
19816
19817 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19818 It is used for relative paths in the line table.
19819 NOTE: When processing partial symtabs (pst != NULL),
19820 comp_dir == pst->dirname.
19821
19822 NOTE: It is important that psymtabs have the same file name (via strcmp)
19823 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19824 symtab we don't use it in the name of the psymtabs we create.
19825 E.g. expand_line_sal requires this when finding psymtabs to expand.
19826 A good testcase for this is mb-inline.exp.
19827
19828 LOWPC is the lowest address in CU (or 0 if not known).
19829
19830 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19831 for its PC<->lines mapping information. Otherwise only the filename
19832 table is read in. */
19833
19834 static void
19835 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
19836 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
19837 CORE_ADDR lowpc, int decode_mapping)
19838 {
19839 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19840 const int decode_for_pst_p = (pst != NULL);
19841
19842 if (decode_mapping)
19843 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
19844
19845 if (decode_for_pst_p)
19846 {
19847 /* Now that we're done scanning the Line Header Program, we can
19848 create the psymtab of each included file. */
19849 for (auto &file_entry : lh->file_names ())
19850 if (file_entry.included_p == 1)
19851 {
19852 gdb::unique_xmalloc_ptr<char> name_holder;
19853 const char *include_name =
19854 psymtab_include_file_name (lh, file_entry, pst,
19855 comp_dir, &name_holder);
19856 if (include_name != NULL)
19857 dwarf2_create_include_psymtab (include_name, pst, objfile);
19858 }
19859 }
19860 else
19861 {
19862 /* Make sure a symtab is created for every file, even files
19863 which contain only variables (i.e. no code with associated
19864 line numbers). */
19865 buildsym_compunit *builder = cu->get_builder ();
19866 struct compunit_symtab *cust = builder->get_compunit_symtab ();
19867
19868 for (auto &fe : lh->file_names ())
19869 {
19870 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
19871 if (builder->get_current_subfile ()->symtab == NULL)
19872 {
19873 builder->get_current_subfile ()->symtab
19874 = allocate_symtab (cust,
19875 builder->get_current_subfile ()->name);
19876 }
19877 fe.symtab = builder->get_current_subfile ()->symtab;
19878 }
19879 }
19880 }
19881
19882 /* Start a subfile for DWARF. FILENAME is the name of the file and
19883 DIRNAME the name of the source directory which contains FILENAME
19884 or NULL if not known.
19885 This routine tries to keep line numbers from identical absolute and
19886 relative file names in a common subfile.
19887
19888 Using the `list' example from the GDB testsuite, which resides in
19889 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19890 of /srcdir/list0.c yields the following debugging information for list0.c:
19891
19892 DW_AT_name: /srcdir/list0.c
19893 DW_AT_comp_dir: /compdir
19894 files.files[0].name: list0.h
19895 files.files[0].dir: /srcdir
19896 files.files[1].name: list0.c
19897 files.files[1].dir: /srcdir
19898
19899 The line number information for list0.c has to end up in a single
19900 subfile, so that `break /srcdir/list0.c:1' works as expected.
19901 start_subfile will ensure that this happens provided that we pass the
19902 concatenation of files.files[1].dir and files.files[1].name as the
19903 subfile's name. */
19904
19905 static void
19906 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
19907 const char *dirname)
19908 {
19909 gdb::unique_xmalloc_ptr<char> copy;
19910
19911 /* In order not to lose the line information directory,
19912 we concatenate it to the filename when it makes sense.
19913 Note that the Dwarf3 standard says (speaking of filenames in line
19914 information): ``The directory index is ignored for file names
19915 that represent full path names''. Thus ignoring dirname in the
19916 `else' branch below isn't an issue. */
19917
19918 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
19919 {
19920 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
19921 filename = copy.get ();
19922 }
19923
19924 cu->get_builder ()->start_subfile (filename);
19925 }
19926
19927 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
19928 buildsym_compunit constructor. */
19929
19930 struct compunit_symtab *
19931 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
19932 CORE_ADDR low_pc)
19933 {
19934 gdb_assert (m_builder == nullptr);
19935
19936 m_builder.reset (new struct buildsym_compunit
19937 (per_cu->dwarf2_per_objfile->objfile,
19938 name, comp_dir, language, low_pc));
19939
19940 list_in_scope = get_builder ()->get_file_symbols ();
19941
19942 get_builder ()->record_debugformat ("DWARF 2");
19943 get_builder ()->record_producer (producer);
19944
19945 processing_has_namespace_info = false;
19946
19947 return get_builder ()->get_compunit_symtab ();
19948 }
19949
19950 static void
19951 var_decode_location (struct attribute *attr, struct symbol *sym,
19952 struct dwarf2_cu *cu)
19953 {
19954 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19955 struct comp_unit_head *cu_header = &cu->header;
19956
19957 /* NOTE drow/2003-01-30: There used to be a comment and some special
19958 code here to turn a symbol with DW_AT_external and a
19959 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19960 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19961 with some versions of binutils) where shared libraries could have
19962 relocations against symbols in their debug information - the
19963 minimal symbol would have the right address, but the debug info
19964 would not. It's no longer necessary, because we will explicitly
19965 apply relocations when we read in the debug information now. */
19966
19967 /* A DW_AT_location attribute with no contents indicates that a
19968 variable has been optimized away. */
19969 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
19970 {
19971 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19972 return;
19973 }
19974
19975 /* Handle one degenerate form of location expression specially, to
19976 preserve GDB's previous behavior when section offsets are
19977 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
19978 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
19979
19980 if (attr->form_is_block ()
19981 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19982 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19983 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19984 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
19985 && (DW_BLOCK (attr)->size
19986 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
19987 {
19988 unsigned int dummy;
19989
19990 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19991 SET_SYMBOL_VALUE_ADDRESS
19992 (sym, cu->header.read_address (objfile->obfd,
19993 DW_BLOCK (attr)->data + 1,
19994 &dummy));
19995 else
19996 SET_SYMBOL_VALUE_ADDRESS
19997 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
19998 &dummy));
19999 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20000 fixup_symbol_section (sym, objfile);
20001 SET_SYMBOL_VALUE_ADDRESS
20002 (sym,
20003 SYMBOL_VALUE_ADDRESS (sym)
20004 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20005 return;
20006 }
20007
20008 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20009 expression evaluator, and use LOC_COMPUTED only when necessary
20010 (i.e. when the value of a register or memory location is
20011 referenced, or a thread-local block, etc.). Then again, it might
20012 not be worthwhile. I'm assuming that it isn't unless performance
20013 or memory numbers show me otherwise. */
20014
20015 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20016
20017 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20018 cu->has_loclist = true;
20019 }
20020
20021 /* Given a pointer to a DWARF information entry, figure out if we need
20022 to make a symbol table entry for it, and if so, create a new entry
20023 and return a pointer to it.
20024 If TYPE is NULL, determine symbol type from the die, otherwise
20025 used the passed type.
20026 If SPACE is not NULL, use it to hold the new symbol. If it is
20027 NULL, allocate a new symbol on the objfile's obstack. */
20028
20029 static struct symbol *
20030 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20031 struct symbol *space)
20032 {
20033 struct dwarf2_per_objfile *dwarf2_per_objfile
20034 = cu->per_cu->dwarf2_per_objfile;
20035 struct objfile *objfile = dwarf2_per_objfile->objfile;
20036 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20037 struct symbol *sym = NULL;
20038 const char *name;
20039 struct attribute *attr = NULL;
20040 struct attribute *attr2 = NULL;
20041 CORE_ADDR baseaddr;
20042 struct pending **list_to_add = NULL;
20043
20044 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20045
20046 baseaddr = objfile->text_section_offset ();
20047
20048 name = dwarf2_name (die, cu);
20049 if (name)
20050 {
20051 const char *linkagename;
20052 int suppress_add = 0;
20053
20054 if (space)
20055 sym = space;
20056 else
20057 sym = allocate_symbol (objfile);
20058 OBJSTAT (objfile, n_syms++);
20059
20060 /* Cache this symbol's name and the name's demangled form (if any). */
20061 sym->set_language (cu->language, &objfile->objfile_obstack);
20062 linkagename = dwarf2_physname (name, die, cu);
20063 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20064
20065 /* Fortran does not have mangling standard and the mangling does differ
20066 between gfortran, iFort etc. */
20067 if (cu->language == language_fortran
20068 && symbol_get_demangled_name (sym) == NULL)
20069 symbol_set_demangled_name (sym,
20070 dwarf2_full_name (name, die, cu),
20071 NULL);
20072
20073 /* Default assumptions.
20074 Use the passed type or decode it from the die. */
20075 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20076 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20077 if (type != NULL)
20078 SYMBOL_TYPE (sym) = type;
20079 else
20080 SYMBOL_TYPE (sym) = die_type (die, cu);
20081 attr = dwarf2_attr (die,
20082 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20083 cu);
20084 if (attr != nullptr)
20085 {
20086 SYMBOL_LINE (sym) = DW_UNSND (attr);
20087 }
20088
20089 attr = dwarf2_attr (die,
20090 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20091 cu);
20092 if (attr != nullptr)
20093 {
20094 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20095 struct file_entry *fe;
20096
20097 if (cu->line_header != NULL)
20098 fe = cu->line_header->file_name_at (file_index);
20099 else
20100 fe = NULL;
20101
20102 if (fe == NULL)
20103 complaint (_("file index out of range"));
20104 else
20105 symbol_set_symtab (sym, fe->symtab);
20106 }
20107
20108 switch (die->tag)
20109 {
20110 case DW_TAG_label:
20111 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20112 if (attr != nullptr)
20113 {
20114 CORE_ADDR addr;
20115
20116 addr = attr->value_as_address ();
20117 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20118 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20119 }
20120 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20121 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20122 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20123 add_symbol_to_list (sym, cu->list_in_scope);
20124 break;
20125 case DW_TAG_subprogram:
20126 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20127 finish_block. */
20128 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20129 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20130 if ((attr2 && (DW_UNSND (attr2) != 0))
20131 || cu->language == language_ada
20132 || cu->language == language_fortran)
20133 {
20134 /* Subprograms marked external are stored as a global symbol.
20135 Ada and Fortran subprograms, whether marked external or
20136 not, are always stored as a global symbol, because we want
20137 to be able to access them globally. For instance, we want
20138 to be able to break on a nested subprogram without having
20139 to specify the context. */
20140 list_to_add = cu->get_builder ()->get_global_symbols ();
20141 }
20142 else
20143 {
20144 list_to_add = cu->list_in_scope;
20145 }
20146 break;
20147 case DW_TAG_inlined_subroutine:
20148 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20149 finish_block. */
20150 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20151 SYMBOL_INLINED (sym) = 1;
20152 list_to_add = cu->list_in_scope;
20153 break;
20154 case DW_TAG_template_value_param:
20155 suppress_add = 1;
20156 /* Fall through. */
20157 case DW_TAG_constant:
20158 case DW_TAG_variable:
20159 case DW_TAG_member:
20160 /* Compilation with minimal debug info may result in
20161 variables with missing type entries. Change the
20162 misleading `void' type to something sensible. */
20163 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20164 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20165
20166 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20167 /* In the case of DW_TAG_member, we should only be called for
20168 static const members. */
20169 if (die->tag == DW_TAG_member)
20170 {
20171 /* dwarf2_add_field uses die_is_declaration,
20172 so we do the same. */
20173 gdb_assert (die_is_declaration (die, cu));
20174 gdb_assert (attr);
20175 }
20176 if (attr != nullptr)
20177 {
20178 dwarf2_const_value (attr, sym, cu);
20179 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20180 if (!suppress_add)
20181 {
20182 if (attr2 && (DW_UNSND (attr2) != 0))
20183 list_to_add = cu->get_builder ()->get_global_symbols ();
20184 else
20185 list_to_add = cu->list_in_scope;
20186 }
20187 break;
20188 }
20189 attr = dwarf2_attr (die, DW_AT_location, cu);
20190 if (attr != nullptr)
20191 {
20192 var_decode_location (attr, sym, cu);
20193 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20194
20195 /* Fortran explicitly imports any global symbols to the local
20196 scope by DW_TAG_common_block. */
20197 if (cu->language == language_fortran && die->parent
20198 && die->parent->tag == DW_TAG_common_block)
20199 attr2 = NULL;
20200
20201 if (SYMBOL_CLASS (sym) == LOC_STATIC
20202 && SYMBOL_VALUE_ADDRESS (sym) == 0
20203 && !dwarf2_per_objfile->has_section_at_zero)
20204 {
20205 /* When a static variable is eliminated by the linker,
20206 the corresponding debug information is not stripped
20207 out, but the variable address is set to null;
20208 do not add such variables into symbol table. */
20209 }
20210 else if (attr2 && (DW_UNSND (attr2) != 0))
20211 {
20212 if (SYMBOL_CLASS (sym) == LOC_STATIC
20213 && (objfile->flags & OBJF_MAINLINE) == 0
20214 && dwarf2_per_objfile->can_copy)
20215 {
20216 /* A global static variable might be subject to
20217 copy relocation. We first check for a local
20218 minsym, though, because maybe the symbol was
20219 marked hidden, in which case this would not
20220 apply. */
20221 bound_minimal_symbol found
20222 = (lookup_minimal_symbol_linkage
20223 (sym->linkage_name (), objfile));
20224 if (found.minsym != nullptr)
20225 sym->maybe_copied = 1;
20226 }
20227
20228 /* A variable with DW_AT_external is never static,
20229 but it may be block-scoped. */
20230 list_to_add
20231 = ((cu->list_in_scope
20232 == cu->get_builder ()->get_file_symbols ())
20233 ? cu->get_builder ()->get_global_symbols ()
20234 : cu->list_in_scope);
20235 }
20236 else
20237 list_to_add = cu->list_in_scope;
20238 }
20239 else
20240 {
20241 /* We do not know the address of this symbol.
20242 If it is an external symbol and we have type information
20243 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20244 The address of the variable will then be determined from
20245 the minimal symbol table whenever the variable is
20246 referenced. */
20247 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20248
20249 /* Fortran explicitly imports any global symbols to the local
20250 scope by DW_TAG_common_block. */
20251 if (cu->language == language_fortran && die->parent
20252 && die->parent->tag == DW_TAG_common_block)
20253 {
20254 /* SYMBOL_CLASS doesn't matter here because
20255 read_common_block is going to reset it. */
20256 if (!suppress_add)
20257 list_to_add = cu->list_in_scope;
20258 }
20259 else if (attr2 && (DW_UNSND (attr2) != 0)
20260 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20261 {
20262 /* A variable with DW_AT_external is never static, but it
20263 may be block-scoped. */
20264 list_to_add
20265 = ((cu->list_in_scope
20266 == cu->get_builder ()->get_file_symbols ())
20267 ? cu->get_builder ()->get_global_symbols ()
20268 : cu->list_in_scope);
20269
20270 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20271 }
20272 else if (!die_is_declaration (die, cu))
20273 {
20274 /* Use the default LOC_OPTIMIZED_OUT class. */
20275 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20276 if (!suppress_add)
20277 list_to_add = cu->list_in_scope;
20278 }
20279 }
20280 break;
20281 case DW_TAG_formal_parameter:
20282 {
20283 /* If we are inside a function, mark this as an argument. If
20284 not, we might be looking at an argument to an inlined function
20285 when we do not have enough information to show inlined frames;
20286 pretend it's a local variable in that case so that the user can
20287 still see it. */
20288 struct context_stack *curr
20289 = cu->get_builder ()->get_current_context_stack ();
20290 if (curr != nullptr && curr->name != nullptr)
20291 SYMBOL_IS_ARGUMENT (sym) = 1;
20292 attr = dwarf2_attr (die, DW_AT_location, cu);
20293 if (attr != nullptr)
20294 {
20295 var_decode_location (attr, sym, cu);
20296 }
20297 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20298 if (attr != nullptr)
20299 {
20300 dwarf2_const_value (attr, sym, cu);
20301 }
20302
20303 list_to_add = cu->list_in_scope;
20304 }
20305 break;
20306 case DW_TAG_unspecified_parameters:
20307 /* From varargs functions; gdb doesn't seem to have any
20308 interest in this information, so just ignore it for now.
20309 (FIXME?) */
20310 break;
20311 case DW_TAG_template_type_param:
20312 suppress_add = 1;
20313 /* Fall through. */
20314 case DW_TAG_class_type:
20315 case DW_TAG_interface_type:
20316 case DW_TAG_structure_type:
20317 case DW_TAG_union_type:
20318 case DW_TAG_set_type:
20319 case DW_TAG_enumeration_type:
20320 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20321 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20322
20323 {
20324 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20325 really ever be static objects: otherwise, if you try
20326 to, say, break of a class's method and you're in a file
20327 which doesn't mention that class, it won't work unless
20328 the check for all static symbols in lookup_symbol_aux
20329 saves you. See the OtherFileClass tests in
20330 gdb.c++/namespace.exp. */
20331
20332 if (!suppress_add)
20333 {
20334 buildsym_compunit *builder = cu->get_builder ();
20335 list_to_add
20336 = (cu->list_in_scope == builder->get_file_symbols ()
20337 && cu->language == language_cplus
20338 ? builder->get_global_symbols ()
20339 : cu->list_in_scope);
20340
20341 /* The semantics of C++ state that "struct foo {
20342 ... }" also defines a typedef for "foo". */
20343 if (cu->language == language_cplus
20344 || cu->language == language_ada
20345 || cu->language == language_d
20346 || cu->language == language_rust)
20347 {
20348 /* The symbol's name is already allocated along
20349 with this objfile, so we don't need to
20350 duplicate it for the type. */
20351 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20352 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20353 }
20354 }
20355 }
20356 break;
20357 case DW_TAG_typedef:
20358 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20359 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20360 list_to_add = cu->list_in_scope;
20361 break;
20362 case DW_TAG_base_type:
20363 case DW_TAG_subrange_type:
20364 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20365 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20366 list_to_add = cu->list_in_scope;
20367 break;
20368 case DW_TAG_enumerator:
20369 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20370 if (attr != nullptr)
20371 {
20372 dwarf2_const_value (attr, sym, cu);
20373 }
20374 {
20375 /* NOTE: carlton/2003-11-10: See comment above in the
20376 DW_TAG_class_type, etc. block. */
20377
20378 list_to_add
20379 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20380 && cu->language == language_cplus
20381 ? cu->get_builder ()->get_global_symbols ()
20382 : cu->list_in_scope);
20383 }
20384 break;
20385 case DW_TAG_imported_declaration:
20386 case DW_TAG_namespace:
20387 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20388 list_to_add = cu->get_builder ()->get_global_symbols ();
20389 break;
20390 case DW_TAG_module:
20391 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20392 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20393 list_to_add = cu->get_builder ()->get_global_symbols ();
20394 break;
20395 case DW_TAG_common_block:
20396 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20397 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20398 add_symbol_to_list (sym, cu->list_in_scope);
20399 break;
20400 default:
20401 /* Not a tag we recognize. Hopefully we aren't processing
20402 trash data, but since we must specifically ignore things
20403 we don't recognize, there is nothing else we should do at
20404 this point. */
20405 complaint (_("unsupported tag: '%s'"),
20406 dwarf_tag_name (die->tag));
20407 break;
20408 }
20409
20410 if (suppress_add)
20411 {
20412 sym->hash_next = objfile->template_symbols;
20413 objfile->template_symbols = sym;
20414 list_to_add = NULL;
20415 }
20416
20417 if (list_to_add != NULL)
20418 add_symbol_to_list (sym, list_to_add);
20419
20420 /* For the benefit of old versions of GCC, check for anonymous
20421 namespaces based on the demangled name. */
20422 if (!cu->processing_has_namespace_info
20423 && cu->language == language_cplus)
20424 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20425 }
20426 return (sym);
20427 }
20428
20429 /* Given an attr with a DW_FORM_dataN value in host byte order,
20430 zero-extend it as appropriate for the symbol's type. The DWARF
20431 standard (v4) is not entirely clear about the meaning of using
20432 DW_FORM_dataN for a constant with a signed type, where the type is
20433 wider than the data. The conclusion of a discussion on the DWARF
20434 list was that this is unspecified. We choose to always zero-extend
20435 because that is the interpretation long in use by GCC. */
20436
20437 static gdb_byte *
20438 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20439 struct dwarf2_cu *cu, LONGEST *value, int bits)
20440 {
20441 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20442 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20443 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20444 LONGEST l = DW_UNSND (attr);
20445
20446 if (bits < sizeof (*value) * 8)
20447 {
20448 l &= ((LONGEST) 1 << bits) - 1;
20449 *value = l;
20450 }
20451 else if (bits == sizeof (*value) * 8)
20452 *value = l;
20453 else
20454 {
20455 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20456 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20457 return bytes;
20458 }
20459
20460 return NULL;
20461 }
20462
20463 /* Read a constant value from an attribute. Either set *VALUE, or if
20464 the value does not fit in *VALUE, set *BYTES - either already
20465 allocated on the objfile obstack, or newly allocated on OBSTACK,
20466 or, set *BATON, if we translated the constant to a location
20467 expression. */
20468
20469 static void
20470 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20471 const char *name, struct obstack *obstack,
20472 struct dwarf2_cu *cu,
20473 LONGEST *value, const gdb_byte **bytes,
20474 struct dwarf2_locexpr_baton **baton)
20475 {
20476 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20477 struct comp_unit_head *cu_header = &cu->header;
20478 struct dwarf_block *blk;
20479 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20480 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20481
20482 *value = 0;
20483 *bytes = NULL;
20484 *baton = NULL;
20485
20486 switch (attr->form)
20487 {
20488 case DW_FORM_addr:
20489 case DW_FORM_addrx:
20490 case DW_FORM_GNU_addr_index:
20491 {
20492 gdb_byte *data;
20493
20494 if (TYPE_LENGTH (type) != cu_header->addr_size)
20495 dwarf2_const_value_length_mismatch_complaint (name,
20496 cu_header->addr_size,
20497 TYPE_LENGTH (type));
20498 /* Symbols of this form are reasonably rare, so we just
20499 piggyback on the existing location code rather than writing
20500 a new implementation of symbol_computed_ops. */
20501 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20502 (*baton)->per_cu = cu->per_cu;
20503 gdb_assert ((*baton)->per_cu);
20504
20505 (*baton)->size = 2 + cu_header->addr_size;
20506 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20507 (*baton)->data = data;
20508
20509 data[0] = DW_OP_addr;
20510 store_unsigned_integer (&data[1], cu_header->addr_size,
20511 byte_order, DW_ADDR (attr));
20512 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20513 }
20514 break;
20515 case DW_FORM_string:
20516 case DW_FORM_strp:
20517 case DW_FORM_strx:
20518 case DW_FORM_GNU_str_index:
20519 case DW_FORM_GNU_strp_alt:
20520 /* DW_STRING is already allocated on the objfile obstack, point
20521 directly to it. */
20522 *bytes = (const gdb_byte *) DW_STRING (attr);
20523 break;
20524 case DW_FORM_block1:
20525 case DW_FORM_block2:
20526 case DW_FORM_block4:
20527 case DW_FORM_block:
20528 case DW_FORM_exprloc:
20529 case DW_FORM_data16:
20530 blk = DW_BLOCK (attr);
20531 if (TYPE_LENGTH (type) != blk->size)
20532 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20533 TYPE_LENGTH (type));
20534 *bytes = blk->data;
20535 break;
20536
20537 /* The DW_AT_const_value attributes are supposed to carry the
20538 symbol's value "represented as it would be on the target
20539 architecture." By the time we get here, it's already been
20540 converted to host endianness, so we just need to sign- or
20541 zero-extend it as appropriate. */
20542 case DW_FORM_data1:
20543 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20544 break;
20545 case DW_FORM_data2:
20546 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20547 break;
20548 case DW_FORM_data4:
20549 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20550 break;
20551 case DW_FORM_data8:
20552 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20553 break;
20554
20555 case DW_FORM_sdata:
20556 case DW_FORM_implicit_const:
20557 *value = DW_SND (attr);
20558 break;
20559
20560 case DW_FORM_udata:
20561 *value = DW_UNSND (attr);
20562 break;
20563
20564 default:
20565 complaint (_("unsupported const value attribute form: '%s'"),
20566 dwarf_form_name (attr->form));
20567 *value = 0;
20568 break;
20569 }
20570 }
20571
20572
20573 /* Copy constant value from an attribute to a symbol. */
20574
20575 static void
20576 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20577 struct dwarf2_cu *cu)
20578 {
20579 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20580 LONGEST value;
20581 const gdb_byte *bytes;
20582 struct dwarf2_locexpr_baton *baton;
20583
20584 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20585 sym->print_name (),
20586 &objfile->objfile_obstack, cu,
20587 &value, &bytes, &baton);
20588
20589 if (baton != NULL)
20590 {
20591 SYMBOL_LOCATION_BATON (sym) = baton;
20592 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20593 }
20594 else if (bytes != NULL)
20595 {
20596 SYMBOL_VALUE_BYTES (sym) = bytes;
20597 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20598 }
20599 else
20600 {
20601 SYMBOL_VALUE (sym) = value;
20602 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20603 }
20604 }
20605
20606 /* Return the type of the die in question using its DW_AT_type attribute. */
20607
20608 static struct type *
20609 die_type (struct die_info *die, struct dwarf2_cu *cu)
20610 {
20611 struct attribute *type_attr;
20612
20613 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20614 if (!type_attr)
20615 {
20616 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20617 /* A missing DW_AT_type represents a void type. */
20618 return objfile_type (objfile)->builtin_void;
20619 }
20620
20621 return lookup_die_type (die, type_attr, cu);
20622 }
20623
20624 /* True iff CU's producer generates GNAT Ada auxiliary information
20625 that allows to find parallel types through that information instead
20626 of having to do expensive parallel lookups by type name. */
20627
20628 static int
20629 need_gnat_info (struct dwarf2_cu *cu)
20630 {
20631 /* Assume that the Ada compiler was GNAT, which always produces
20632 the auxiliary information. */
20633 return (cu->language == language_ada);
20634 }
20635
20636 /* Return the auxiliary type of the die in question using its
20637 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20638 attribute is not present. */
20639
20640 static struct type *
20641 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20642 {
20643 struct attribute *type_attr;
20644
20645 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20646 if (!type_attr)
20647 return NULL;
20648
20649 return lookup_die_type (die, type_attr, cu);
20650 }
20651
20652 /* If DIE has a descriptive_type attribute, then set the TYPE's
20653 descriptive type accordingly. */
20654
20655 static void
20656 set_descriptive_type (struct type *type, struct die_info *die,
20657 struct dwarf2_cu *cu)
20658 {
20659 struct type *descriptive_type = die_descriptive_type (die, cu);
20660
20661 if (descriptive_type)
20662 {
20663 ALLOCATE_GNAT_AUX_TYPE (type);
20664 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20665 }
20666 }
20667
20668 /* Return the containing type of the die in question using its
20669 DW_AT_containing_type attribute. */
20670
20671 static struct type *
20672 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20673 {
20674 struct attribute *type_attr;
20675 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20676
20677 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20678 if (!type_attr)
20679 error (_("Dwarf Error: Problem turning containing type into gdb type "
20680 "[in module %s]"), objfile_name (objfile));
20681
20682 return lookup_die_type (die, type_attr, cu);
20683 }
20684
20685 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20686
20687 static struct type *
20688 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20689 {
20690 struct dwarf2_per_objfile *dwarf2_per_objfile
20691 = cu->per_cu->dwarf2_per_objfile;
20692 struct objfile *objfile = dwarf2_per_objfile->objfile;
20693 char *saved;
20694
20695 std::string message
20696 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20697 objfile_name (objfile),
20698 sect_offset_str (cu->header.sect_off),
20699 sect_offset_str (die->sect_off));
20700 saved = obstack_strdup (&objfile->objfile_obstack, message);
20701
20702 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20703 }
20704
20705 /* Look up the type of DIE in CU using its type attribute ATTR.
20706 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20707 DW_AT_containing_type.
20708 If there is no type substitute an error marker. */
20709
20710 static struct type *
20711 lookup_die_type (struct die_info *die, const struct attribute *attr,
20712 struct dwarf2_cu *cu)
20713 {
20714 struct dwarf2_per_objfile *dwarf2_per_objfile
20715 = cu->per_cu->dwarf2_per_objfile;
20716 struct objfile *objfile = dwarf2_per_objfile->objfile;
20717 struct type *this_type;
20718
20719 gdb_assert (attr->name == DW_AT_type
20720 || attr->name == DW_AT_GNAT_descriptive_type
20721 || attr->name == DW_AT_containing_type);
20722
20723 /* First see if we have it cached. */
20724
20725 if (attr->form == DW_FORM_GNU_ref_alt)
20726 {
20727 struct dwarf2_per_cu_data *per_cu;
20728 sect_offset sect_off = attr->get_ref_die_offset ();
20729
20730 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20731 dwarf2_per_objfile);
20732 this_type = get_die_type_at_offset (sect_off, per_cu);
20733 }
20734 else if (attr->form_is_ref ())
20735 {
20736 sect_offset sect_off = attr->get_ref_die_offset ();
20737
20738 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20739 }
20740 else if (attr->form == DW_FORM_ref_sig8)
20741 {
20742 ULONGEST signature = DW_SIGNATURE (attr);
20743
20744 return get_signatured_type (die, signature, cu);
20745 }
20746 else
20747 {
20748 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
20749 " at %s [in module %s]"),
20750 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
20751 objfile_name (objfile));
20752 return build_error_marker_type (cu, die);
20753 }
20754
20755 /* If not cached we need to read it in. */
20756
20757 if (this_type == NULL)
20758 {
20759 struct die_info *type_die = NULL;
20760 struct dwarf2_cu *type_cu = cu;
20761
20762 if (attr->form_is_ref ())
20763 type_die = follow_die_ref (die, attr, &type_cu);
20764 if (type_die == NULL)
20765 return build_error_marker_type (cu, die);
20766 /* If we find the type now, it's probably because the type came
20767 from an inter-CU reference and the type's CU got expanded before
20768 ours. */
20769 this_type = read_type_die (type_die, type_cu);
20770 }
20771
20772 /* If we still don't have a type use an error marker. */
20773
20774 if (this_type == NULL)
20775 return build_error_marker_type (cu, die);
20776
20777 return this_type;
20778 }
20779
20780 /* Return the type in DIE, CU.
20781 Returns NULL for invalid types.
20782
20783 This first does a lookup in die_type_hash,
20784 and only reads the die in if necessary.
20785
20786 NOTE: This can be called when reading in partial or full symbols. */
20787
20788 static struct type *
20789 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20790 {
20791 struct type *this_type;
20792
20793 this_type = get_die_type (die, cu);
20794 if (this_type)
20795 return this_type;
20796
20797 return read_type_die_1 (die, cu);
20798 }
20799
20800 /* Read the type in DIE, CU.
20801 Returns NULL for invalid types. */
20802
20803 static struct type *
20804 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20805 {
20806 struct type *this_type = NULL;
20807
20808 switch (die->tag)
20809 {
20810 case DW_TAG_class_type:
20811 case DW_TAG_interface_type:
20812 case DW_TAG_structure_type:
20813 case DW_TAG_union_type:
20814 this_type = read_structure_type (die, cu);
20815 break;
20816 case DW_TAG_enumeration_type:
20817 this_type = read_enumeration_type (die, cu);
20818 break;
20819 case DW_TAG_subprogram:
20820 case DW_TAG_subroutine_type:
20821 case DW_TAG_inlined_subroutine:
20822 this_type = read_subroutine_type (die, cu);
20823 break;
20824 case DW_TAG_array_type:
20825 this_type = read_array_type (die, cu);
20826 break;
20827 case DW_TAG_set_type:
20828 this_type = read_set_type (die, cu);
20829 break;
20830 case DW_TAG_pointer_type:
20831 this_type = read_tag_pointer_type (die, cu);
20832 break;
20833 case DW_TAG_ptr_to_member_type:
20834 this_type = read_tag_ptr_to_member_type (die, cu);
20835 break;
20836 case DW_TAG_reference_type:
20837 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20838 break;
20839 case DW_TAG_rvalue_reference_type:
20840 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
20841 break;
20842 case DW_TAG_const_type:
20843 this_type = read_tag_const_type (die, cu);
20844 break;
20845 case DW_TAG_volatile_type:
20846 this_type = read_tag_volatile_type (die, cu);
20847 break;
20848 case DW_TAG_restrict_type:
20849 this_type = read_tag_restrict_type (die, cu);
20850 break;
20851 case DW_TAG_string_type:
20852 this_type = read_tag_string_type (die, cu);
20853 break;
20854 case DW_TAG_typedef:
20855 this_type = read_typedef (die, cu);
20856 break;
20857 case DW_TAG_subrange_type:
20858 this_type = read_subrange_type (die, cu);
20859 break;
20860 case DW_TAG_base_type:
20861 this_type = read_base_type (die, cu);
20862 break;
20863 case DW_TAG_unspecified_type:
20864 this_type = read_unspecified_type (die, cu);
20865 break;
20866 case DW_TAG_namespace:
20867 this_type = read_namespace_type (die, cu);
20868 break;
20869 case DW_TAG_module:
20870 this_type = read_module_type (die, cu);
20871 break;
20872 case DW_TAG_atomic_type:
20873 this_type = read_tag_atomic_type (die, cu);
20874 break;
20875 default:
20876 complaint (_("unexpected tag in read_type_die: '%s'"),
20877 dwarf_tag_name (die->tag));
20878 break;
20879 }
20880
20881 return this_type;
20882 }
20883
20884 /* See if we can figure out if the class lives in a namespace. We do
20885 this by looking for a member function; its demangled name will
20886 contain namespace info, if there is any.
20887 Return the computed name or NULL.
20888 Space for the result is allocated on the objfile's obstack.
20889 This is the full-die version of guess_partial_die_structure_name.
20890 In this case we know DIE has no useful parent. */
20891
20892 static const char *
20893 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20894 {
20895 struct die_info *spec_die;
20896 struct dwarf2_cu *spec_cu;
20897 struct die_info *child;
20898 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20899
20900 spec_cu = cu;
20901 spec_die = die_specification (die, &spec_cu);
20902 if (spec_die != NULL)
20903 {
20904 die = spec_die;
20905 cu = spec_cu;
20906 }
20907
20908 for (child = die->child;
20909 child != NULL;
20910 child = child->sibling)
20911 {
20912 if (child->tag == DW_TAG_subprogram)
20913 {
20914 const char *linkage_name = dw2_linkage_name (child, cu);
20915
20916 if (linkage_name != NULL)
20917 {
20918 gdb::unique_xmalloc_ptr<char> actual_name
20919 (language_class_name_from_physname (cu->language_defn,
20920 linkage_name));
20921 const char *name = NULL;
20922
20923 if (actual_name != NULL)
20924 {
20925 const char *die_name = dwarf2_name (die, cu);
20926
20927 if (die_name != NULL
20928 && strcmp (die_name, actual_name.get ()) != 0)
20929 {
20930 /* Strip off the class name from the full name.
20931 We want the prefix. */
20932 int die_name_len = strlen (die_name);
20933 int actual_name_len = strlen (actual_name.get ());
20934 const char *ptr = actual_name.get ();
20935
20936 /* Test for '::' as a sanity check. */
20937 if (actual_name_len > die_name_len + 2
20938 && ptr[actual_name_len - die_name_len - 1] == ':')
20939 name = obstack_strndup (
20940 &objfile->per_bfd->storage_obstack,
20941 ptr, actual_name_len - die_name_len - 2);
20942 }
20943 }
20944 return name;
20945 }
20946 }
20947 }
20948
20949 return NULL;
20950 }
20951
20952 /* GCC might emit a nameless typedef that has a linkage name. Determine the
20953 prefix part in such case. See
20954 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20955
20956 static const char *
20957 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20958 {
20959 struct attribute *attr;
20960 const char *base;
20961
20962 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20963 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20964 return NULL;
20965
20966 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
20967 return NULL;
20968
20969 attr = dw2_linkage_name_attr (die, cu);
20970 if (attr == NULL || DW_STRING (attr) == NULL)
20971 return NULL;
20972
20973 /* dwarf2_name had to be already called. */
20974 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20975
20976 /* Strip the base name, keep any leading namespaces/classes. */
20977 base = strrchr (DW_STRING (attr), ':');
20978 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20979 return "";
20980
20981 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20982 return obstack_strndup (&objfile->per_bfd->storage_obstack,
20983 DW_STRING (attr),
20984 &base[-1] - DW_STRING (attr));
20985 }
20986
20987 /* Return the name of the namespace/class that DIE is defined within,
20988 or "" if we can't tell. The caller should not xfree the result.
20989
20990 For example, if we're within the method foo() in the following
20991 code:
20992
20993 namespace N {
20994 class C {
20995 void foo () {
20996 }
20997 };
20998 }
20999
21000 then determine_prefix on foo's die will return "N::C". */
21001
21002 static const char *
21003 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21004 {
21005 struct dwarf2_per_objfile *dwarf2_per_objfile
21006 = cu->per_cu->dwarf2_per_objfile;
21007 struct die_info *parent, *spec_die;
21008 struct dwarf2_cu *spec_cu;
21009 struct type *parent_type;
21010 const char *retval;
21011
21012 if (cu->language != language_cplus
21013 && cu->language != language_fortran && cu->language != language_d
21014 && cu->language != language_rust)
21015 return "";
21016
21017 retval = anonymous_struct_prefix (die, cu);
21018 if (retval)
21019 return retval;
21020
21021 /* We have to be careful in the presence of DW_AT_specification.
21022 For example, with GCC 3.4, given the code
21023
21024 namespace N {
21025 void foo() {
21026 // Definition of N::foo.
21027 }
21028 }
21029
21030 then we'll have a tree of DIEs like this:
21031
21032 1: DW_TAG_compile_unit
21033 2: DW_TAG_namespace // N
21034 3: DW_TAG_subprogram // declaration of N::foo
21035 4: DW_TAG_subprogram // definition of N::foo
21036 DW_AT_specification // refers to die #3
21037
21038 Thus, when processing die #4, we have to pretend that we're in
21039 the context of its DW_AT_specification, namely the contex of die
21040 #3. */
21041 spec_cu = cu;
21042 spec_die = die_specification (die, &spec_cu);
21043 if (spec_die == NULL)
21044 parent = die->parent;
21045 else
21046 {
21047 parent = spec_die->parent;
21048 cu = spec_cu;
21049 }
21050
21051 if (parent == NULL)
21052 return "";
21053 else if (parent->building_fullname)
21054 {
21055 const char *name;
21056 const char *parent_name;
21057
21058 /* It has been seen on RealView 2.2 built binaries,
21059 DW_TAG_template_type_param types actually _defined_ as
21060 children of the parent class:
21061
21062 enum E {};
21063 template class <class Enum> Class{};
21064 Class<enum E> class_e;
21065
21066 1: DW_TAG_class_type (Class)
21067 2: DW_TAG_enumeration_type (E)
21068 3: DW_TAG_enumerator (enum1:0)
21069 3: DW_TAG_enumerator (enum2:1)
21070 ...
21071 2: DW_TAG_template_type_param
21072 DW_AT_type DW_FORM_ref_udata (E)
21073
21074 Besides being broken debug info, it can put GDB into an
21075 infinite loop. Consider:
21076
21077 When we're building the full name for Class<E>, we'll start
21078 at Class, and go look over its template type parameters,
21079 finding E. We'll then try to build the full name of E, and
21080 reach here. We're now trying to build the full name of E,
21081 and look over the parent DIE for containing scope. In the
21082 broken case, if we followed the parent DIE of E, we'd again
21083 find Class, and once again go look at its template type
21084 arguments, etc., etc. Simply don't consider such parent die
21085 as source-level parent of this die (it can't be, the language
21086 doesn't allow it), and break the loop here. */
21087 name = dwarf2_name (die, cu);
21088 parent_name = dwarf2_name (parent, cu);
21089 complaint (_("template param type '%s' defined within parent '%s'"),
21090 name ? name : "<unknown>",
21091 parent_name ? parent_name : "<unknown>");
21092 return "";
21093 }
21094 else
21095 switch (parent->tag)
21096 {
21097 case DW_TAG_namespace:
21098 parent_type = read_type_die (parent, cu);
21099 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21100 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21101 Work around this problem here. */
21102 if (cu->language == language_cplus
21103 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21104 return "";
21105 /* We give a name to even anonymous namespaces. */
21106 return TYPE_NAME (parent_type);
21107 case DW_TAG_class_type:
21108 case DW_TAG_interface_type:
21109 case DW_TAG_structure_type:
21110 case DW_TAG_union_type:
21111 case DW_TAG_module:
21112 parent_type = read_type_die (parent, cu);
21113 if (TYPE_NAME (parent_type) != NULL)
21114 return TYPE_NAME (parent_type);
21115 else
21116 /* An anonymous structure is only allowed non-static data
21117 members; no typedefs, no member functions, et cetera.
21118 So it does not need a prefix. */
21119 return "";
21120 case DW_TAG_compile_unit:
21121 case DW_TAG_partial_unit:
21122 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21123 if (cu->language == language_cplus
21124 && !dwarf2_per_objfile->types.empty ()
21125 && die->child != NULL
21126 && (die->tag == DW_TAG_class_type
21127 || die->tag == DW_TAG_structure_type
21128 || die->tag == DW_TAG_union_type))
21129 {
21130 const char *name = guess_full_die_structure_name (die, cu);
21131 if (name != NULL)
21132 return name;
21133 }
21134 return "";
21135 case DW_TAG_subprogram:
21136 /* Nested subroutines in Fortran get a prefix with the name
21137 of the parent's subroutine. */
21138 if (cu->language == language_fortran)
21139 {
21140 if ((die->tag == DW_TAG_subprogram)
21141 && (dwarf2_name (parent, cu) != NULL))
21142 return dwarf2_name (parent, cu);
21143 }
21144 return determine_prefix (parent, cu);
21145 case DW_TAG_enumeration_type:
21146 parent_type = read_type_die (parent, cu);
21147 if (TYPE_DECLARED_CLASS (parent_type))
21148 {
21149 if (TYPE_NAME (parent_type) != NULL)
21150 return TYPE_NAME (parent_type);
21151 return "";
21152 }
21153 /* Fall through. */
21154 default:
21155 return determine_prefix (parent, cu);
21156 }
21157 }
21158
21159 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21160 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21161 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21162 an obconcat, otherwise allocate storage for the result. The CU argument is
21163 used to determine the language and hence, the appropriate separator. */
21164
21165 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21166
21167 static char *
21168 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21169 int physname, struct dwarf2_cu *cu)
21170 {
21171 const char *lead = "";
21172 const char *sep;
21173
21174 if (suffix == NULL || suffix[0] == '\0'
21175 || prefix == NULL || prefix[0] == '\0')
21176 sep = "";
21177 else if (cu->language == language_d)
21178 {
21179 /* For D, the 'main' function could be defined in any module, but it
21180 should never be prefixed. */
21181 if (strcmp (suffix, "D main") == 0)
21182 {
21183 prefix = "";
21184 sep = "";
21185 }
21186 else
21187 sep = ".";
21188 }
21189 else if (cu->language == language_fortran && physname)
21190 {
21191 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21192 DW_AT_MIPS_linkage_name is preferred and used instead. */
21193
21194 lead = "__";
21195 sep = "_MOD_";
21196 }
21197 else
21198 sep = "::";
21199
21200 if (prefix == NULL)
21201 prefix = "";
21202 if (suffix == NULL)
21203 suffix = "";
21204
21205 if (obs == NULL)
21206 {
21207 char *retval
21208 = ((char *)
21209 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21210
21211 strcpy (retval, lead);
21212 strcat (retval, prefix);
21213 strcat (retval, sep);
21214 strcat (retval, suffix);
21215 return retval;
21216 }
21217 else
21218 {
21219 /* We have an obstack. */
21220 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21221 }
21222 }
21223
21224 /* Get name of a die, return NULL if not found. */
21225
21226 static const char *
21227 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21228 struct objfile *objfile)
21229 {
21230 if (name && cu->language == language_cplus)
21231 {
21232 std::string canon_name = cp_canonicalize_string (name);
21233
21234 if (!canon_name.empty ())
21235 {
21236 if (canon_name != name)
21237 name = objfile->intern (canon_name);
21238 }
21239 }
21240
21241 return name;
21242 }
21243
21244 /* Get name of a die, return NULL if not found.
21245 Anonymous namespaces are converted to their magic string. */
21246
21247 static const char *
21248 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21249 {
21250 struct attribute *attr;
21251 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21252
21253 attr = dwarf2_attr (die, DW_AT_name, cu);
21254 if ((!attr || !DW_STRING (attr))
21255 && die->tag != DW_TAG_namespace
21256 && die->tag != DW_TAG_class_type
21257 && die->tag != DW_TAG_interface_type
21258 && die->tag != DW_TAG_structure_type
21259 && die->tag != DW_TAG_union_type)
21260 return NULL;
21261
21262 switch (die->tag)
21263 {
21264 case DW_TAG_compile_unit:
21265 case DW_TAG_partial_unit:
21266 /* Compilation units have a DW_AT_name that is a filename, not
21267 a source language identifier. */
21268 case DW_TAG_enumeration_type:
21269 case DW_TAG_enumerator:
21270 /* These tags always have simple identifiers already; no need
21271 to canonicalize them. */
21272 return DW_STRING (attr);
21273
21274 case DW_TAG_namespace:
21275 if (attr != NULL && DW_STRING (attr) != NULL)
21276 return DW_STRING (attr);
21277 return CP_ANONYMOUS_NAMESPACE_STR;
21278
21279 case DW_TAG_class_type:
21280 case DW_TAG_interface_type:
21281 case DW_TAG_structure_type:
21282 case DW_TAG_union_type:
21283 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21284 structures or unions. These were of the form "._%d" in GCC 4.1,
21285 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21286 and GCC 4.4. We work around this problem by ignoring these. */
21287 if (attr && DW_STRING (attr)
21288 && (startswith (DW_STRING (attr), "._")
21289 || startswith (DW_STRING (attr), "<anonymous")))
21290 return NULL;
21291
21292 /* GCC might emit a nameless typedef that has a linkage name. See
21293 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21294 if (!attr || DW_STRING (attr) == NULL)
21295 {
21296 attr = dw2_linkage_name_attr (die, cu);
21297 if (attr == NULL || DW_STRING (attr) == NULL)
21298 return NULL;
21299
21300 /* Avoid demangling DW_STRING (attr) the second time on a second
21301 call for the same DIE. */
21302 if (!DW_STRING_IS_CANONICAL (attr))
21303 {
21304 gdb::unique_xmalloc_ptr<char> demangled
21305 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21306 if (demangled == nullptr)
21307 return nullptr;
21308
21309 DW_STRING (attr) = objfile->intern (demangled.get ());
21310 DW_STRING_IS_CANONICAL (attr) = 1;
21311 }
21312
21313 /* Strip any leading namespaces/classes, keep only the base name.
21314 DW_AT_name for named DIEs does not contain the prefixes. */
21315 const char *base = strrchr (DW_STRING (attr), ':');
21316 if (base && base > DW_STRING (attr) && base[-1] == ':')
21317 return &base[1];
21318 else
21319 return DW_STRING (attr);
21320 }
21321 break;
21322
21323 default:
21324 break;
21325 }
21326
21327 if (!DW_STRING_IS_CANONICAL (attr))
21328 {
21329 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21330 objfile);
21331 DW_STRING_IS_CANONICAL (attr) = 1;
21332 }
21333 return DW_STRING (attr);
21334 }
21335
21336 /* Return the die that this die in an extension of, or NULL if there
21337 is none. *EXT_CU is the CU containing DIE on input, and the CU
21338 containing the return value on output. */
21339
21340 static struct die_info *
21341 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21342 {
21343 struct attribute *attr;
21344
21345 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21346 if (attr == NULL)
21347 return NULL;
21348
21349 return follow_die_ref (die, attr, ext_cu);
21350 }
21351
21352 static void
21353 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21354 {
21355 unsigned int i;
21356
21357 print_spaces (indent, f);
21358 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21359 dwarf_tag_name (die->tag), die->abbrev,
21360 sect_offset_str (die->sect_off));
21361
21362 if (die->parent != NULL)
21363 {
21364 print_spaces (indent, f);
21365 fprintf_unfiltered (f, " parent at offset: %s\n",
21366 sect_offset_str (die->parent->sect_off));
21367 }
21368
21369 print_spaces (indent, f);
21370 fprintf_unfiltered (f, " has children: %s\n",
21371 dwarf_bool_name (die->child != NULL));
21372
21373 print_spaces (indent, f);
21374 fprintf_unfiltered (f, " attributes:\n");
21375
21376 for (i = 0; i < die->num_attrs; ++i)
21377 {
21378 print_spaces (indent, f);
21379 fprintf_unfiltered (f, " %s (%s) ",
21380 dwarf_attr_name (die->attrs[i].name),
21381 dwarf_form_name (die->attrs[i].form));
21382
21383 switch (die->attrs[i].form)
21384 {
21385 case DW_FORM_addr:
21386 case DW_FORM_addrx:
21387 case DW_FORM_GNU_addr_index:
21388 fprintf_unfiltered (f, "address: ");
21389 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21390 break;
21391 case DW_FORM_block2:
21392 case DW_FORM_block4:
21393 case DW_FORM_block:
21394 case DW_FORM_block1:
21395 fprintf_unfiltered (f, "block: size %s",
21396 pulongest (DW_BLOCK (&die->attrs[i])->size));
21397 break;
21398 case DW_FORM_exprloc:
21399 fprintf_unfiltered (f, "expression: size %s",
21400 pulongest (DW_BLOCK (&die->attrs[i])->size));
21401 break;
21402 case DW_FORM_data16:
21403 fprintf_unfiltered (f, "constant of 16 bytes");
21404 break;
21405 case DW_FORM_ref_addr:
21406 fprintf_unfiltered (f, "ref address: ");
21407 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21408 break;
21409 case DW_FORM_GNU_ref_alt:
21410 fprintf_unfiltered (f, "alt ref address: ");
21411 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21412 break;
21413 case DW_FORM_ref1:
21414 case DW_FORM_ref2:
21415 case DW_FORM_ref4:
21416 case DW_FORM_ref8:
21417 case DW_FORM_ref_udata:
21418 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21419 (long) (DW_UNSND (&die->attrs[i])));
21420 break;
21421 case DW_FORM_data1:
21422 case DW_FORM_data2:
21423 case DW_FORM_data4:
21424 case DW_FORM_data8:
21425 case DW_FORM_udata:
21426 case DW_FORM_sdata:
21427 fprintf_unfiltered (f, "constant: %s",
21428 pulongest (DW_UNSND (&die->attrs[i])));
21429 break;
21430 case DW_FORM_sec_offset:
21431 fprintf_unfiltered (f, "section offset: %s",
21432 pulongest (DW_UNSND (&die->attrs[i])));
21433 break;
21434 case DW_FORM_ref_sig8:
21435 fprintf_unfiltered (f, "signature: %s",
21436 hex_string (DW_SIGNATURE (&die->attrs[i])));
21437 break;
21438 case DW_FORM_string:
21439 case DW_FORM_strp:
21440 case DW_FORM_line_strp:
21441 case DW_FORM_strx:
21442 case DW_FORM_GNU_str_index:
21443 case DW_FORM_GNU_strp_alt:
21444 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21445 DW_STRING (&die->attrs[i])
21446 ? DW_STRING (&die->attrs[i]) : "",
21447 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21448 break;
21449 case DW_FORM_flag:
21450 if (DW_UNSND (&die->attrs[i]))
21451 fprintf_unfiltered (f, "flag: TRUE");
21452 else
21453 fprintf_unfiltered (f, "flag: FALSE");
21454 break;
21455 case DW_FORM_flag_present:
21456 fprintf_unfiltered (f, "flag: TRUE");
21457 break;
21458 case DW_FORM_indirect:
21459 /* The reader will have reduced the indirect form to
21460 the "base form" so this form should not occur. */
21461 fprintf_unfiltered (f,
21462 "unexpected attribute form: DW_FORM_indirect");
21463 break;
21464 case DW_FORM_implicit_const:
21465 fprintf_unfiltered (f, "constant: %s",
21466 plongest (DW_SND (&die->attrs[i])));
21467 break;
21468 default:
21469 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21470 die->attrs[i].form);
21471 break;
21472 }
21473 fprintf_unfiltered (f, "\n");
21474 }
21475 }
21476
21477 static void
21478 dump_die_for_error (struct die_info *die)
21479 {
21480 dump_die_shallow (gdb_stderr, 0, die);
21481 }
21482
21483 static void
21484 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21485 {
21486 int indent = level * 4;
21487
21488 gdb_assert (die != NULL);
21489
21490 if (level >= max_level)
21491 return;
21492
21493 dump_die_shallow (f, indent, die);
21494
21495 if (die->child != NULL)
21496 {
21497 print_spaces (indent, f);
21498 fprintf_unfiltered (f, " Children:");
21499 if (level + 1 < max_level)
21500 {
21501 fprintf_unfiltered (f, "\n");
21502 dump_die_1 (f, level + 1, max_level, die->child);
21503 }
21504 else
21505 {
21506 fprintf_unfiltered (f,
21507 " [not printed, max nesting level reached]\n");
21508 }
21509 }
21510
21511 if (die->sibling != NULL && level > 0)
21512 {
21513 dump_die_1 (f, level, max_level, die->sibling);
21514 }
21515 }
21516
21517 /* This is called from the pdie macro in gdbinit.in.
21518 It's not static so gcc will keep a copy callable from gdb. */
21519
21520 void
21521 dump_die (struct die_info *die, int max_level)
21522 {
21523 dump_die_1 (gdb_stdlog, 0, max_level, die);
21524 }
21525
21526 static void
21527 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21528 {
21529 void **slot;
21530
21531 slot = htab_find_slot_with_hash (cu->die_hash, die,
21532 to_underlying (die->sect_off),
21533 INSERT);
21534
21535 *slot = die;
21536 }
21537
21538 /* Follow reference or signature attribute ATTR of SRC_DIE.
21539 On entry *REF_CU is the CU of SRC_DIE.
21540 On exit *REF_CU is the CU of the result. */
21541
21542 static struct die_info *
21543 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21544 struct dwarf2_cu **ref_cu)
21545 {
21546 struct die_info *die;
21547
21548 if (attr->form_is_ref ())
21549 die = follow_die_ref (src_die, attr, ref_cu);
21550 else if (attr->form == DW_FORM_ref_sig8)
21551 die = follow_die_sig (src_die, attr, ref_cu);
21552 else
21553 {
21554 dump_die_for_error (src_die);
21555 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21556 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
21557 }
21558
21559 return die;
21560 }
21561
21562 /* Follow reference OFFSET.
21563 On entry *REF_CU is the CU of the source die referencing OFFSET.
21564 On exit *REF_CU is the CU of the result.
21565 Returns NULL if OFFSET is invalid. */
21566
21567 static struct die_info *
21568 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21569 struct dwarf2_cu **ref_cu)
21570 {
21571 struct die_info temp_die;
21572 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21573 struct dwarf2_per_objfile *dwarf2_per_objfile
21574 = cu->per_cu->dwarf2_per_objfile;
21575
21576 gdb_assert (cu->per_cu != NULL);
21577
21578 target_cu = cu;
21579
21580 if (cu->per_cu->is_debug_types)
21581 {
21582 /* .debug_types CUs cannot reference anything outside their CU.
21583 If they need to, they have to reference a signatured type via
21584 DW_FORM_ref_sig8. */
21585 if (!cu->header.offset_in_cu_p (sect_off))
21586 return NULL;
21587 }
21588 else if (offset_in_dwz != cu->per_cu->is_dwz
21589 || !cu->header.offset_in_cu_p (sect_off))
21590 {
21591 struct dwarf2_per_cu_data *per_cu;
21592
21593 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21594 dwarf2_per_objfile);
21595
21596 /* If necessary, add it to the queue and load its DIEs. */
21597 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21598 load_full_comp_unit (per_cu, false, cu->language);
21599
21600 target_cu = per_cu->cu;
21601 }
21602 else if (cu->dies == NULL)
21603 {
21604 /* We're loading full DIEs during partial symbol reading. */
21605 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21606 load_full_comp_unit (cu->per_cu, false, language_minimal);
21607 }
21608
21609 *ref_cu = target_cu;
21610 temp_die.sect_off = sect_off;
21611
21612 if (target_cu != cu)
21613 target_cu->ancestor = cu;
21614
21615 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21616 &temp_die,
21617 to_underlying (sect_off));
21618 }
21619
21620 /* Follow reference attribute ATTR of SRC_DIE.
21621 On entry *REF_CU is the CU of SRC_DIE.
21622 On exit *REF_CU is the CU of the result. */
21623
21624 static struct die_info *
21625 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21626 struct dwarf2_cu **ref_cu)
21627 {
21628 sect_offset sect_off = attr->get_ref_die_offset ();
21629 struct dwarf2_cu *cu = *ref_cu;
21630 struct die_info *die;
21631
21632 die = follow_die_offset (sect_off,
21633 (attr->form == DW_FORM_GNU_ref_alt
21634 || cu->per_cu->is_dwz),
21635 ref_cu);
21636 if (!die)
21637 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21638 "at %s [in module %s]"),
21639 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
21640 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
21641
21642 return die;
21643 }
21644
21645 /* See read.h. */
21646
21647 struct dwarf2_locexpr_baton
21648 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21649 dwarf2_per_cu_data *per_cu,
21650 CORE_ADDR (*get_frame_pc) (void *baton),
21651 void *baton, bool resolve_abstract_p)
21652 {
21653 struct dwarf2_cu *cu;
21654 struct die_info *die;
21655 struct attribute *attr;
21656 struct dwarf2_locexpr_baton retval;
21657 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21658 struct objfile *objfile = dwarf2_per_objfile->objfile;
21659
21660 if (per_cu->cu == NULL)
21661 load_cu (per_cu, false);
21662 cu = per_cu->cu;
21663 if (cu == NULL)
21664 {
21665 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21666 Instead just throw an error, not much else we can do. */
21667 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21668 sect_offset_str (sect_off), objfile_name (objfile));
21669 }
21670
21671 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21672 if (!die)
21673 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21674 sect_offset_str (sect_off), objfile_name (objfile));
21675
21676 attr = dwarf2_attr (die, DW_AT_location, cu);
21677 if (!attr && resolve_abstract_p
21678 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
21679 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21680 {
21681 CORE_ADDR pc = (*get_frame_pc) (baton);
21682 CORE_ADDR baseaddr = objfile->text_section_offset ();
21683 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21684
21685 for (const auto &cand_off
21686 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
21687 {
21688 struct dwarf2_cu *cand_cu = cu;
21689 struct die_info *cand
21690 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21691 if (!cand
21692 || !cand->parent
21693 || cand->parent->tag != DW_TAG_subprogram)
21694 continue;
21695
21696 CORE_ADDR pc_low, pc_high;
21697 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
21698 if (pc_low == ((CORE_ADDR) -1))
21699 continue;
21700 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21701 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21702 if (!(pc_low <= pc && pc < pc_high))
21703 continue;
21704
21705 die = cand;
21706 attr = dwarf2_attr (die, DW_AT_location, cu);
21707 break;
21708 }
21709 }
21710
21711 if (!attr)
21712 {
21713 /* DWARF: "If there is no such attribute, then there is no effect.".
21714 DATA is ignored if SIZE is 0. */
21715
21716 retval.data = NULL;
21717 retval.size = 0;
21718 }
21719 else if (attr->form_is_section_offset ())
21720 {
21721 struct dwarf2_loclist_baton loclist_baton;
21722 CORE_ADDR pc = (*get_frame_pc) (baton);
21723 size_t size;
21724
21725 fill_in_loclist_baton (cu, &loclist_baton, attr);
21726
21727 retval.data = dwarf2_find_location_expression (&loclist_baton,
21728 &size, pc);
21729 retval.size = size;
21730 }
21731 else
21732 {
21733 if (!attr->form_is_block ())
21734 error (_("Dwarf Error: DIE at %s referenced in module %s "
21735 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21736 sect_offset_str (sect_off), objfile_name (objfile));
21737
21738 retval.data = DW_BLOCK (attr)->data;
21739 retval.size = DW_BLOCK (attr)->size;
21740 }
21741 retval.per_cu = cu->per_cu;
21742
21743 age_cached_comp_units (dwarf2_per_objfile);
21744
21745 return retval;
21746 }
21747
21748 /* See read.h. */
21749
21750 struct dwarf2_locexpr_baton
21751 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21752 dwarf2_per_cu_data *per_cu,
21753 CORE_ADDR (*get_frame_pc) (void *baton),
21754 void *baton)
21755 {
21756 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21757
21758 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21759 }
21760
21761 /* Write a constant of a given type as target-ordered bytes into
21762 OBSTACK. */
21763
21764 static const gdb_byte *
21765 write_constant_as_bytes (struct obstack *obstack,
21766 enum bfd_endian byte_order,
21767 struct type *type,
21768 ULONGEST value,
21769 LONGEST *len)
21770 {
21771 gdb_byte *result;
21772
21773 *len = TYPE_LENGTH (type);
21774 result = (gdb_byte *) obstack_alloc (obstack, *len);
21775 store_unsigned_integer (result, *len, byte_order, value);
21776
21777 return result;
21778 }
21779
21780 /* See read.h. */
21781
21782 const gdb_byte *
21783 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21784 dwarf2_per_cu_data *per_cu,
21785 obstack *obstack,
21786 LONGEST *len)
21787 {
21788 struct dwarf2_cu *cu;
21789 struct die_info *die;
21790 struct attribute *attr;
21791 const gdb_byte *result = NULL;
21792 struct type *type;
21793 LONGEST value;
21794 enum bfd_endian byte_order;
21795 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
21796
21797 if (per_cu->cu == NULL)
21798 load_cu (per_cu, false);
21799 cu = per_cu->cu;
21800 if (cu == NULL)
21801 {
21802 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21803 Instead just throw an error, not much else we can do. */
21804 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21805 sect_offset_str (sect_off), objfile_name (objfile));
21806 }
21807
21808 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21809 if (!die)
21810 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21811 sect_offset_str (sect_off), objfile_name (objfile));
21812
21813 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21814 if (attr == NULL)
21815 return NULL;
21816
21817 byte_order = (bfd_big_endian (objfile->obfd)
21818 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21819
21820 switch (attr->form)
21821 {
21822 case DW_FORM_addr:
21823 case DW_FORM_addrx:
21824 case DW_FORM_GNU_addr_index:
21825 {
21826 gdb_byte *tem;
21827
21828 *len = cu->header.addr_size;
21829 tem = (gdb_byte *) obstack_alloc (obstack, *len);
21830 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21831 result = tem;
21832 }
21833 break;
21834 case DW_FORM_string:
21835 case DW_FORM_strp:
21836 case DW_FORM_strx:
21837 case DW_FORM_GNU_str_index:
21838 case DW_FORM_GNU_strp_alt:
21839 /* DW_STRING is already allocated on the objfile obstack, point
21840 directly to it. */
21841 result = (const gdb_byte *) DW_STRING (attr);
21842 *len = strlen (DW_STRING (attr));
21843 break;
21844 case DW_FORM_block1:
21845 case DW_FORM_block2:
21846 case DW_FORM_block4:
21847 case DW_FORM_block:
21848 case DW_FORM_exprloc:
21849 case DW_FORM_data16:
21850 result = DW_BLOCK (attr)->data;
21851 *len = DW_BLOCK (attr)->size;
21852 break;
21853
21854 /* The DW_AT_const_value attributes are supposed to carry the
21855 symbol's value "represented as it would be on the target
21856 architecture." By the time we get here, it's already been
21857 converted to host endianness, so we just need to sign- or
21858 zero-extend it as appropriate. */
21859 case DW_FORM_data1:
21860 type = die_type (die, cu);
21861 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21862 if (result == NULL)
21863 result = write_constant_as_bytes (obstack, byte_order,
21864 type, value, len);
21865 break;
21866 case DW_FORM_data2:
21867 type = die_type (die, cu);
21868 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21869 if (result == NULL)
21870 result = write_constant_as_bytes (obstack, byte_order,
21871 type, value, len);
21872 break;
21873 case DW_FORM_data4:
21874 type = die_type (die, cu);
21875 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21876 if (result == NULL)
21877 result = write_constant_as_bytes (obstack, byte_order,
21878 type, value, len);
21879 break;
21880 case DW_FORM_data8:
21881 type = die_type (die, cu);
21882 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21883 if (result == NULL)
21884 result = write_constant_as_bytes (obstack, byte_order,
21885 type, value, len);
21886 break;
21887
21888 case DW_FORM_sdata:
21889 case DW_FORM_implicit_const:
21890 type = die_type (die, cu);
21891 result = write_constant_as_bytes (obstack, byte_order,
21892 type, DW_SND (attr), len);
21893 break;
21894
21895 case DW_FORM_udata:
21896 type = die_type (die, cu);
21897 result = write_constant_as_bytes (obstack, byte_order,
21898 type, DW_UNSND (attr), len);
21899 break;
21900
21901 default:
21902 complaint (_("unsupported const value attribute form: '%s'"),
21903 dwarf_form_name (attr->form));
21904 break;
21905 }
21906
21907 return result;
21908 }
21909
21910 /* See read.h. */
21911
21912 struct type *
21913 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
21914 dwarf2_per_cu_data *per_cu)
21915 {
21916 struct dwarf2_cu *cu;
21917 struct die_info *die;
21918
21919 if (per_cu->cu == NULL)
21920 load_cu (per_cu, false);
21921 cu = per_cu->cu;
21922 if (!cu)
21923 return NULL;
21924
21925 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21926 if (!die)
21927 return NULL;
21928
21929 return die_type (die, cu);
21930 }
21931
21932 /* See read.h. */
21933
21934 struct type *
21935 dwarf2_get_die_type (cu_offset die_offset,
21936 struct dwarf2_per_cu_data *per_cu)
21937 {
21938 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
21939 return get_die_type_at_offset (die_offset_sect, per_cu);
21940 }
21941
21942 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
21943 On entry *REF_CU is the CU of SRC_DIE.
21944 On exit *REF_CU is the CU of the result.
21945 Returns NULL if the referenced DIE isn't found. */
21946
21947 static struct die_info *
21948 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21949 struct dwarf2_cu **ref_cu)
21950 {
21951 struct die_info temp_die;
21952 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
21953 struct die_info *die;
21954
21955 /* While it might be nice to assert sig_type->type == NULL here,
21956 we can get here for DW_AT_imported_declaration where we need
21957 the DIE not the type. */
21958
21959 /* If necessary, add it to the queue and load its DIEs. */
21960
21961 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
21962 read_signatured_type (sig_type);
21963
21964 sig_cu = sig_type->per_cu.cu;
21965 gdb_assert (sig_cu != NULL);
21966 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21967 temp_die.sect_off = sig_type->type_offset_in_section;
21968 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
21969 to_underlying (temp_die.sect_off));
21970 if (die)
21971 {
21972 struct dwarf2_per_objfile *dwarf2_per_objfile
21973 = (*ref_cu)->per_cu->dwarf2_per_objfile;
21974
21975 /* For .gdb_index version 7 keep track of included TUs.
21976 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21977 if (dwarf2_per_objfile->index_table != NULL
21978 && dwarf2_per_objfile->index_table->version <= 7)
21979 {
21980 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
21981 }
21982
21983 *ref_cu = sig_cu;
21984 if (sig_cu != cu)
21985 sig_cu->ancestor = cu;
21986
21987 return die;
21988 }
21989
21990 return NULL;
21991 }
21992
21993 /* Follow signatured type referenced by ATTR in SRC_DIE.
21994 On entry *REF_CU is the CU of SRC_DIE.
21995 On exit *REF_CU is the CU of the result.
21996 The result is the DIE of the type.
21997 If the referenced type cannot be found an error is thrown. */
21998
21999 static struct die_info *
22000 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22001 struct dwarf2_cu **ref_cu)
22002 {
22003 ULONGEST signature = DW_SIGNATURE (attr);
22004 struct signatured_type *sig_type;
22005 struct die_info *die;
22006
22007 gdb_assert (attr->form == DW_FORM_ref_sig8);
22008
22009 sig_type = lookup_signatured_type (*ref_cu, signature);
22010 /* sig_type will be NULL if the signatured type is missing from
22011 the debug info. */
22012 if (sig_type == NULL)
22013 {
22014 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22015 " from DIE at %s [in module %s]"),
22016 hex_string (signature), sect_offset_str (src_die->sect_off),
22017 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22018 }
22019
22020 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22021 if (die == NULL)
22022 {
22023 dump_die_for_error (src_die);
22024 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22025 " from DIE at %s [in module %s]"),
22026 hex_string (signature), sect_offset_str (src_die->sect_off),
22027 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22028 }
22029
22030 return die;
22031 }
22032
22033 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22034 reading in and processing the type unit if necessary. */
22035
22036 static struct type *
22037 get_signatured_type (struct die_info *die, ULONGEST signature,
22038 struct dwarf2_cu *cu)
22039 {
22040 struct dwarf2_per_objfile *dwarf2_per_objfile
22041 = cu->per_cu->dwarf2_per_objfile;
22042 struct signatured_type *sig_type;
22043 struct dwarf2_cu *type_cu;
22044 struct die_info *type_die;
22045 struct type *type;
22046
22047 sig_type = lookup_signatured_type (cu, signature);
22048 /* sig_type will be NULL if the signatured type is missing from
22049 the debug info. */
22050 if (sig_type == NULL)
22051 {
22052 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22053 " from DIE at %s [in module %s]"),
22054 hex_string (signature), sect_offset_str (die->sect_off),
22055 objfile_name (dwarf2_per_objfile->objfile));
22056 return build_error_marker_type (cu, die);
22057 }
22058
22059 /* If we already know the type we're done. */
22060 if (sig_type->type != NULL)
22061 return sig_type->type;
22062
22063 type_cu = cu;
22064 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22065 if (type_die != NULL)
22066 {
22067 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22068 is created. This is important, for example, because for c++ classes
22069 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22070 type = read_type_die (type_die, type_cu);
22071 if (type == NULL)
22072 {
22073 complaint (_("Dwarf Error: Cannot build signatured type %s"
22074 " referenced from DIE at %s [in module %s]"),
22075 hex_string (signature), sect_offset_str (die->sect_off),
22076 objfile_name (dwarf2_per_objfile->objfile));
22077 type = build_error_marker_type (cu, die);
22078 }
22079 }
22080 else
22081 {
22082 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22083 " from DIE at %s [in module %s]"),
22084 hex_string (signature), sect_offset_str (die->sect_off),
22085 objfile_name (dwarf2_per_objfile->objfile));
22086 type = build_error_marker_type (cu, die);
22087 }
22088 sig_type->type = type;
22089
22090 return type;
22091 }
22092
22093 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22094 reading in and processing the type unit if necessary. */
22095
22096 static struct type *
22097 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22098 struct dwarf2_cu *cu) /* ARI: editCase function */
22099 {
22100 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22101 if (attr->form_is_ref ())
22102 {
22103 struct dwarf2_cu *type_cu = cu;
22104 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22105
22106 return read_type_die (type_die, type_cu);
22107 }
22108 else if (attr->form == DW_FORM_ref_sig8)
22109 {
22110 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22111 }
22112 else
22113 {
22114 struct dwarf2_per_objfile *dwarf2_per_objfile
22115 = cu->per_cu->dwarf2_per_objfile;
22116
22117 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22118 " at %s [in module %s]"),
22119 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22120 objfile_name (dwarf2_per_objfile->objfile));
22121 return build_error_marker_type (cu, die);
22122 }
22123 }
22124
22125 /* Load the DIEs associated with type unit PER_CU into memory. */
22126
22127 static void
22128 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22129 {
22130 struct signatured_type *sig_type;
22131
22132 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22133 gdb_assert (! per_cu->type_unit_group_p ());
22134
22135 /* We have the per_cu, but we need the signatured_type.
22136 Fortunately this is an easy translation. */
22137 gdb_assert (per_cu->is_debug_types);
22138 sig_type = (struct signatured_type *) per_cu;
22139
22140 gdb_assert (per_cu->cu == NULL);
22141
22142 read_signatured_type (sig_type);
22143
22144 gdb_assert (per_cu->cu != NULL);
22145 }
22146
22147 /* Read in a signatured type and build its CU and DIEs.
22148 If the type is a stub for the real type in a DWO file,
22149 read in the real type from the DWO file as well. */
22150
22151 static void
22152 read_signatured_type (struct signatured_type *sig_type)
22153 {
22154 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22155
22156 gdb_assert (per_cu->is_debug_types);
22157 gdb_assert (per_cu->cu == NULL);
22158
22159 cutu_reader reader (per_cu, NULL, 0, false);
22160
22161 if (!reader.dummy_p)
22162 {
22163 struct dwarf2_cu *cu = reader.cu;
22164 const gdb_byte *info_ptr = reader.info_ptr;
22165
22166 gdb_assert (cu->die_hash == NULL);
22167 cu->die_hash =
22168 htab_create_alloc_ex (cu->header.length / 12,
22169 die_hash,
22170 die_eq,
22171 NULL,
22172 &cu->comp_unit_obstack,
22173 hashtab_obstack_allocate,
22174 dummy_obstack_deallocate);
22175
22176 if (reader.comp_unit_die->has_children)
22177 reader.comp_unit_die->child
22178 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22179 reader.comp_unit_die);
22180 cu->dies = reader.comp_unit_die;
22181 /* comp_unit_die is not stored in die_hash, no need. */
22182
22183 /* We try not to read any attributes in this function, because
22184 not all CUs needed for references have been loaded yet, and
22185 symbol table processing isn't initialized. But we have to
22186 set the CU language, or we won't be able to build types
22187 correctly. Similarly, if we do not read the producer, we can
22188 not apply producer-specific interpretation. */
22189 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22190
22191 reader.keep ();
22192 }
22193
22194 sig_type->per_cu.tu_read = 1;
22195 }
22196
22197 /* Decode simple location descriptions.
22198 Given a pointer to a dwarf block that defines a location, compute
22199 the location and return the value.
22200
22201 NOTE drow/2003-11-18: This function is called in two situations
22202 now: for the address of static or global variables (partial symbols
22203 only) and for offsets into structures which are expected to be
22204 (more or less) constant. The partial symbol case should go away,
22205 and only the constant case should remain. That will let this
22206 function complain more accurately. A few special modes are allowed
22207 without complaint for global variables (for instance, global
22208 register values and thread-local values).
22209
22210 A location description containing no operations indicates that the
22211 object is optimized out. The return value is 0 for that case.
22212 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22213 callers will only want a very basic result and this can become a
22214 complaint.
22215
22216 Note that stack[0] is unused except as a default error return. */
22217
22218 static CORE_ADDR
22219 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22220 {
22221 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22222 size_t i;
22223 size_t size = blk->size;
22224 const gdb_byte *data = blk->data;
22225 CORE_ADDR stack[64];
22226 int stacki;
22227 unsigned int bytes_read, unsnd;
22228 gdb_byte op;
22229
22230 i = 0;
22231 stacki = 0;
22232 stack[stacki] = 0;
22233 stack[++stacki] = 0;
22234
22235 while (i < size)
22236 {
22237 op = data[i++];
22238 switch (op)
22239 {
22240 case DW_OP_lit0:
22241 case DW_OP_lit1:
22242 case DW_OP_lit2:
22243 case DW_OP_lit3:
22244 case DW_OP_lit4:
22245 case DW_OP_lit5:
22246 case DW_OP_lit6:
22247 case DW_OP_lit7:
22248 case DW_OP_lit8:
22249 case DW_OP_lit9:
22250 case DW_OP_lit10:
22251 case DW_OP_lit11:
22252 case DW_OP_lit12:
22253 case DW_OP_lit13:
22254 case DW_OP_lit14:
22255 case DW_OP_lit15:
22256 case DW_OP_lit16:
22257 case DW_OP_lit17:
22258 case DW_OP_lit18:
22259 case DW_OP_lit19:
22260 case DW_OP_lit20:
22261 case DW_OP_lit21:
22262 case DW_OP_lit22:
22263 case DW_OP_lit23:
22264 case DW_OP_lit24:
22265 case DW_OP_lit25:
22266 case DW_OP_lit26:
22267 case DW_OP_lit27:
22268 case DW_OP_lit28:
22269 case DW_OP_lit29:
22270 case DW_OP_lit30:
22271 case DW_OP_lit31:
22272 stack[++stacki] = op - DW_OP_lit0;
22273 break;
22274
22275 case DW_OP_reg0:
22276 case DW_OP_reg1:
22277 case DW_OP_reg2:
22278 case DW_OP_reg3:
22279 case DW_OP_reg4:
22280 case DW_OP_reg5:
22281 case DW_OP_reg6:
22282 case DW_OP_reg7:
22283 case DW_OP_reg8:
22284 case DW_OP_reg9:
22285 case DW_OP_reg10:
22286 case DW_OP_reg11:
22287 case DW_OP_reg12:
22288 case DW_OP_reg13:
22289 case DW_OP_reg14:
22290 case DW_OP_reg15:
22291 case DW_OP_reg16:
22292 case DW_OP_reg17:
22293 case DW_OP_reg18:
22294 case DW_OP_reg19:
22295 case DW_OP_reg20:
22296 case DW_OP_reg21:
22297 case DW_OP_reg22:
22298 case DW_OP_reg23:
22299 case DW_OP_reg24:
22300 case DW_OP_reg25:
22301 case DW_OP_reg26:
22302 case DW_OP_reg27:
22303 case DW_OP_reg28:
22304 case DW_OP_reg29:
22305 case DW_OP_reg30:
22306 case DW_OP_reg31:
22307 stack[++stacki] = op - DW_OP_reg0;
22308 if (i < size)
22309 dwarf2_complex_location_expr_complaint ();
22310 break;
22311
22312 case DW_OP_regx:
22313 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22314 i += bytes_read;
22315 stack[++stacki] = unsnd;
22316 if (i < size)
22317 dwarf2_complex_location_expr_complaint ();
22318 break;
22319
22320 case DW_OP_addr:
22321 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22322 &bytes_read);
22323 i += bytes_read;
22324 break;
22325
22326 case DW_OP_const1u:
22327 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22328 i += 1;
22329 break;
22330
22331 case DW_OP_const1s:
22332 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22333 i += 1;
22334 break;
22335
22336 case DW_OP_const2u:
22337 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22338 i += 2;
22339 break;
22340
22341 case DW_OP_const2s:
22342 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22343 i += 2;
22344 break;
22345
22346 case DW_OP_const4u:
22347 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22348 i += 4;
22349 break;
22350
22351 case DW_OP_const4s:
22352 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22353 i += 4;
22354 break;
22355
22356 case DW_OP_const8u:
22357 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22358 i += 8;
22359 break;
22360
22361 case DW_OP_constu:
22362 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22363 &bytes_read);
22364 i += bytes_read;
22365 break;
22366
22367 case DW_OP_consts:
22368 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22369 i += bytes_read;
22370 break;
22371
22372 case DW_OP_dup:
22373 stack[stacki + 1] = stack[stacki];
22374 stacki++;
22375 break;
22376
22377 case DW_OP_plus:
22378 stack[stacki - 1] += stack[stacki];
22379 stacki--;
22380 break;
22381
22382 case DW_OP_plus_uconst:
22383 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22384 &bytes_read);
22385 i += bytes_read;
22386 break;
22387
22388 case DW_OP_minus:
22389 stack[stacki - 1] -= stack[stacki];
22390 stacki--;
22391 break;
22392
22393 case DW_OP_deref:
22394 /* If we're not the last op, then we definitely can't encode
22395 this using GDB's address_class enum. This is valid for partial
22396 global symbols, although the variable's address will be bogus
22397 in the psymtab. */
22398 if (i < size)
22399 dwarf2_complex_location_expr_complaint ();
22400 break;
22401
22402 case DW_OP_GNU_push_tls_address:
22403 case DW_OP_form_tls_address:
22404 /* The top of the stack has the offset from the beginning
22405 of the thread control block at which the variable is located. */
22406 /* Nothing should follow this operator, so the top of stack would
22407 be returned. */
22408 /* This is valid for partial global symbols, but the variable's
22409 address will be bogus in the psymtab. Make it always at least
22410 non-zero to not look as a variable garbage collected by linker
22411 which have DW_OP_addr 0. */
22412 if (i < size)
22413 dwarf2_complex_location_expr_complaint ();
22414 stack[stacki]++;
22415 break;
22416
22417 case DW_OP_GNU_uninit:
22418 break;
22419
22420 case DW_OP_addrx:
22421 case DW_OP_GNU_addr_index:
22422 case DW_OP_GNU_const_index:
22423 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22424 &bytes_read);
22425 i += bytes_read;
22426 break;
22427
22428 default:
22429 {
22430 const char *name = get_DW_OP_name (op);
22431
22432 if (name)
22433 complaint (_("unsupported stack op: '%s'"),
22434 name);
22435 else
22436 complaint (_("unsupported stack op: '%02x'"),
22437 op);
22438 }
22439
22440 return (stack[stacki]);
22441 }
22442
22443 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22444 outside of the allocated space. Also enforce minimum>0. */
22445 if (stacki >= ARRAY_SIZE (stack) - 1)
22446 {
22447 complaint (_("location description stack overflow"));
22448 return 0;
22449 }
22450
22451 if (stacki <= 0)
22452 {
22453 complaint (_("location description stack underflow"));
22454 return 0;
22455 }
22456 }
22457 return (stack[stacki]);
22458 }
22459
22460 /* memory allocation interface */
22461
22462 static struct dwarf_block *
22463 dwarf_alloc_block (struct dwarf2_cu *cu)
22464 {
22465 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22466 }
22467
22468 static struct die_info *
22469 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22470 {
22471 struct die_info *die;
22472 size_t size = sizeof (struct die_info);
22473
22474 if (num_attrs > 1)
22475 size += (num_attrs - 1) * sizeof (struct attribute);
22476
22477 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22478 memset (die, 0, sizeof (struct die_info));
22479 return (die);
22480 }
22481
22482 \f
22483
22484 /* Macro support. */
22485
22486 /* An overload of dwarf_decode_macros that finds the correct section
22487 and ensures it is read in before calling the other overload. */
22488
22489 static void
22490 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22491 int section_is_gnu)
22492 {
22493 struct dwarf2_per_objfile *dwarf2_per_objfile
22494 = cu->per_cu->dwarf2_per_objfile;
22495 struct objfile *objfile = dwarf2_per_objfile->objfile;
22496 const struct line_header *lh = cu->line_header;
22497 unsigned int offset_size = cu->header.offset_size;
22498 struct dwarf2_section_info *section;
22499 const char *section_name;
22500
22501 if (cu->dwo_unit != nullptr)
22502 {
22503 if (section_is_gnu)
22504 {
22505 section = &cu->dwo_unit->dwo_file->sections.macro;
22506 section_name = ".debug_macro.dwo";
22507 }
22508 else
22509 {
22510 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22511 section_name = ".debug_macinfo.dwo";
22512 }
22513 }
22514 else
22515 {
22516 if (section_is_gnu)
22517 {
22518 section = &dwarf2_per_objfile->macro;
22519 section_name = ".debug_macro";
22520 }
22521 else
22522 {
22523 section = &dwarf2_per_objfile->macinfo;
22524 section_name = ".debug_macinfo";
22525 }
22526 }
22527
22528 section->read (objfile);
22529 if (section->buffer == nullptr)
22530 {
22531 complaint (_("missing %s section"), section_name);
22532 return;
22533 }
22534
22535 buildsym_compunit *builder = cu->get_builder ();
22536
22537 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22538 offset_size, offset, section_is_gnu);
22539 }
22540
22541 /* Return the .debug_loc section to use for CU.
22542 For DWO files use .debug_loc.dwo. */
22543
22544 static struct dwarf2_section_info *
22545 cu_debug_loc_section (struct dwarf2_cu *cu)
22546 {
22547 struct dwarf2_per_objfile *dwarf2_per_objfile
22548 = cu->per_cu->dwarf2_per_objfile;
22549
22550 if (cu->dwo_unit)
22551 {
22552 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22553
22554 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22555 }
22556 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22557 : &dwarf2_per_objfile->loc);
22558 }
22559
22560 /* A helper function that fills in a dwarf2_loclist_baton. */
22561
22562 static void
22563 fill_in_loclist_baton (struct dwarf2_cu *cu,
22564 struct dwarf2_loclist_baton *baton,
22565 const struct attribute *attr)
22566 {
22567 struct dwarf2_per_objfile *dwarf2_per_objfile
22568 = cu->per_cu->dwarf2_per_objfile;
22569 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22570
22571 section->read (dwarf2_per_objfile->objfile);
22572
22573 baton->per_cu = cu->per_cu;
22574 gdb_assert (baton->per_cu);
22575 /* We don't know how long the location list is, but make sure we
22576 don't run off the edge of the section. */
22577 baton->size = section->size - DW_UNSND (attr);
22578 baton->data = section->buffer + DW_UNSND (attr);
22579 if (cu->base_address.has_value ())
22580 baton->base_address = *cu->base_address;
22581 else
22582 baton->base_address = 0;
22583 baton->from_dwo = cu->dwo_unit != NULL;
22584 }
22585
22586 static void
22587 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22588 struct dwarf2_cu *cu, int is_block)
22589 {
22590 struct dwarf2_per_objfile *dwarf2_per_objfile
22591 = cu->per_cu->dwarf2_per_objfile;
22592 struct objfile *objfile = dwarf2_per_objfile->objfile;
22593 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22594
22595 if (attr->form_is_section_offset ()
22596 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22597 the section. If so, fall through to the complaint in the
22598 other branch. */
22599 && DW_UNSND (attr) < section->get_size (objfile))
22600 {
22601 struct dwarf2_loclist_baton *baton;
22602
22603 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22604
22605 fill_in_loclist_baton (cu, baton, attr);
22606
22607 if (!cu->base_address.has_value ())
22608 complaint (_("Location list used without "
22609 "specifying the CU base address."));
22610
22611 SYMBOL_ACLASS_INDEX (sym) = (is_block
22612 ? dwarf2_loclist_block_index
22613 : dwarf2_loclist_index);
22614 SYMBOL_LOCATION_BATON (sym) = baton;
22615 }
22616 else
22617 {
22618 struct dwarf2_locexpr_baton *baton;
22619
22620 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22621 baton->per_cu = cu->per_cu;
22622 gdb_assert (baton->per_cu);
22623
22624 if (attr->form_is_block ())
22625 {
22626 /* Note that we're just copying the block's data pointer
22627 here, not the actual data. We're still pointing into the
22628 info_buffer for SYM's objfile; right now we never release
22629 that buffer, but when we do clean up properly this may
22630 need to change. */
22631 baton->size = DW_BLOCK (attr)->size;
22632 baton->data = DW_BLOCK (attr)->data;
22633 }
22634 else
22635 {
22636 dwarf2_invalid_attrib_class_complaint ("location description",
22637 sym->natural_name ());
22638 baton->size = 0;
22639 }
22640
22641 SYMBOL_ACLASS_INDEX (sym) = (is_block
22642 ? dwarf2_locexpr_block_index
22643 : dwarf2_locexpr_index);
22644 SYMBOL_LOCATION_BATON (sym) = baton;
22645 }
22646 }
22647
22648 /* See read.h. */
22649
22650 struct objfile *
22651 dwarf2_per_cu_data::objfile () const
22652 {
22653 struct objfile *objfile = dwarf2_per_objfile->objfile;
22654
22655 /* Return the master objfile, so that we can report and look up the
22656 correct file containing this variable. */
22657 if (objfile->separate_debug_objfile_backlink)
22658 objfile = objfile->separate_debug_objfile_backlink;
22659
22660 return objfile;
22661 }
22662
22663 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22664 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22665 CU_HEADERP first. */
22666
22667 static const struct comp_unit_head *
22668 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22669 const struct dwarf2_per_cu_data *per_cu)
22670 {
22671 const gdb_byte *info_ptr;
22672
22673 if (per_cu->cu)
22674 return &per_cu->cu->header;
22675
22676 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22677
22678 memset (cu_headerp, 0, sizeof (*cu_headerp));
22679 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22680 rcuh_kind::COMPILE);
22681
22682 return cu_headerp;
22683 }
22684
22685 /* See read.h. */
22686
22687 int
22688 dwarf2_per_cu_data::addr_size () const
22689 {
22690 struct comp_unit_head cu_header_local;
22691 const struct comp_unit_head *cu_headerp;
22692
22693 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22694
22695 return cu_headerp->addr_size;
22696 }
22697
22698 /* See read.h. */
22699
22700 int
22701 dwarf2_per_cu_data::offset_size () const
22702 {
22703 struct comp_unit_head cu_header_local;
22704 const struct comp_unit_head *cu_headerp;
22705
22706 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22707
22708 return cu_headerp->offset_size;
22709 }
22710
22711 /* See read.h. */
22712
22713 int
22714 dwarf2_per_cu_data::ref_addr_size () const
22715 {
22716 struct comp_unit_head cu_header_local;
22717 const struct comp_unit_head *cu_headerp;
22718
22719 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22720
22721 if (cu_headerp->version == 2)
22722 return cu_headerp->addr_size;
22723 else
22724 return cu_headerp->offset_size;
22725 }
22726
22727 /* See read.h. */
22728
22729 CORE_ADDR
22730 dwarf2_per_cu_data::text_offset () const
22731 {
22732 struct objfile *objfile = dwarf2_per_objfile->objfile;
22733
22734 return objfile->text_section_offset ();
22735 }
22736
22737 /* See read.h. */
22738
22739 struct type *
22740 dwarf2_per_cu_data::addr_type () const
22741 {
22742 struct objfile *objfile = dwarf2_per_objfile->objfile;
22743 struct type *void_type = objfile_type (objfile)->builtin_void;
22744 struct type *addr_type = lookup_pointer_type (void_type);
22745 int addr_size = this->addr_size ();
22746
22747 if (TYPE_LENGTH (addr_type) == addr_size)
22748 return addr_type;
22749
22750 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
22751 return addr_type;
22752 }
22753
22754 /* A helper function for dwarf2_find_containing_comp_unit that returns
22755 the index of the result, and that searches a vector. It will
22756 return a result even if the offset in question does not actually
22757 occur in any CU. This is separate so that it can be unit
22758 tested. */
22759
22760 static int
22761 dwarf2_find_containing_comp_unit
22762 (sect_offset sect_off,
22763 unsigned int offset_in_dwz,
22764 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
22765 {
22766 int low, high;
22767
22768 low = 0;
22769 high = all_comp_units.size () - 1;
22770 while (high > low)
22771 {
22772 struct dwarf2_per_cu_data *mid_cu;
22773 int mid = low + (high - low) / 2;
22774
22775 mid_cu = all_comp_units[mid];
22776 if (mid_cu->is_dwz > offset_in_dwz
22777 || (mid_cu->is_dwz == offset_in_dwz
22778 && mid_cu->sect_off + mid_cu->length > sect_off))
22779 high = mid;
22780 else
22781 low = mid + 1;
22782 }
22783 gdb_assert (low == high);
22784 return low;
22785 }
22786
22787 /* Locate the .debug_info compilation unit from CU's objfile which contains
22788 the DIE at OFFSET. Raises an error on failure. */
22789
22790 static struct dwarf2_per_cu_data *
22791 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22792 unsigned int offset_in_dwz,
22793 struct dwarf2_per_objfile *dwarf2_per_objfile)
22794 {
22795 int low
22796 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22797 dwarf2_per_objfile->all_comp_units);
22798 struct dwarf2_per_cu_data *this_cu
22799 = dwarf2_per_objfile->all_comp_units[low];
22800
22801 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
22802 {
22803 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22804 error (_("Dwarf Error: could not find partial DIE containing "
22805 "offset %s [in module %s]"),
22806 sect_offset_str (sect_off),
22807 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
22808
22809 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22810 <= sect_off);
22811 return dwarf2_per_objfile->all_comp_units[low-1];
22812 }
22813 else
22814 {
22815 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
22816 && sect_off >= this_cu->sect_off + this_cu->length)
22817 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
22818 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
22819 return this_cu;
22820 }
22821 }
22822
22823 #if GDB_SELF_TEST
22824
22825 namespace selftests {
22826 namespace find_containing_comp_unit {
22827
22828 static void
22829 run_test ()
22830 {
22831 struct dwarf2_per_cu_data one {};
22832 struct dwarf2_per_cu_data two {};
22833 struct dwarf2_per_cu_data three {};
22834 struct dwarf2_per_cu_data four {};
22835
22836 one.length = 5;
22837 two.sect_off = sect_offset (one.length);
22838 two.length = 7;
22839
22840 three.length = 5;
22841 three.is_dwz = 1;
22842 four.sect_off = sect_offset (three.length);
22843 four.length = 7;
22844 four.is_dwz = 1;
22845
22846 std::vector<dwarf2_per_cu_data *> units;
22847 units.push_back (&one);
22848 units.push_back (&two);
22849 units.push_back (&three);
22850 units.push_back (&four);
22851
22852 int result;
22853
22854 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
22855 SELF_CHECK (units[result] == &one);
22856 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
22857 SELF_CHECK (units[result] == &one);
22858 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
22859 SELF_CHECK (units[result] == &two);
22860
22861 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
22862 SELF_CHECK (units[result] == &three);
22863 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
22864 SELF_CHECK (units[result] == &three);
22865 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
22866 SELF_CHECK (units[result] == &four);
22867 }
22868
22869 }
22870 }
22871
22872 #endif /* GDB_SELF_TEST */
22873
22874 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22875
22876 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
22877 : per_cu (per_cu_),
22878 mark (false),
22879 has_loclist (false),
22880 checked_producer (false),
22881 producer_is_gxx_lt_4_6 (false),
22882 producer_is_gcc_lt_4_3 (false),
22883 producer_is_icc (false),
22884 producer_is_icc_lt_14 (false),
22885 producer_is_codewarrior (false),
22886 processing_has_namespace_info (false)
22887 {
22888 per_cu->cu = this;
22889 }
22890
22891 /* Destroy a dwarf2_cu. */
22892
22893 dwarf2_cu::~dwarf2_cu ()
22894 {
22895 per_cu->cu = NULL;
22896 }
22897
22898 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22899
22900 static void
22901 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22902 enum language pretend_language)
22903 {
22904 struct attribute *attr;
22905
22906 /* Set the language we're debugging. */
22907 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22908 if (attr != nullptr)
22909 set_cu_language (DW_UNSND (attr), cu);
22910 else
22911 {
22912 cu->language = pretend_language;
22913 cu->language_defn = language_def (cu->language);
22914 }
22915
22916 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22917 }
22918
22919 /* Increase the age counter on each cached compilation unit, and free
22920 any that are too old. */
22921
22922 static void
22923 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
22924 {
22925 struct dwarf2_per_cu_data *per_cu, **last_chain;
22926
22927 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22928 per_cu = dwarf2_per_objfile->read_in_chain;
22929 while (per_cu != NULL)
22930 {
22931 per_cu->cu->last_used ++;
22932 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22933 dwarf2_mark (per_cu->cu);
22934 per_cu = per_cu->cu->read_in_chain;
22935 }
22936
22937 per_cu = dwarf2_per_objfile->read_in_chain;
22938 last_chain = &dwarf2_per_objfile->read_in_chain;
22939 while (per_cu != NULL)
22940 {
22941 struct dwarf2_per_cu_data *next_cu;
22942
22943 next_cu = per_cu->cu->read_in_chain;
22944
22945 if (!per_cu->cu->mark)
22946 {
22947 delete per_cu->cu;
22948 *last_chain = next_cu;
22949 }
22950 else
22951 last_chain = &per_cu->cu->read_in_chain;
22952
22953 per_cu = next_cu;
22954 }
22955 }
22956
22957 /* Remove a single compilation unit from the cache. */
22958
22959 static void
22960 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22961 {
22962 struct dwarf2_per_cu_data *per_cu, **last_chain;
22963 struct dwarf2_per_objfile *dwarf2_per_objfile
22964 = target_per_cu->dwarf2_per_objfile;
22965
22966 per_cu = dwarf2_per_objfile->read_in_chain;
22967 last_chain = &dwarf2_per_objfile->read_in_chain;
22968 while (per_cu != NULL)
22969 {
22970 struct dwarf2_per_cu_data *next_cu;
22971
22972 next_cu = per_cu->cu->read_in_chain;
22973
22974 if (per_cu == target_per_cu)
22975 {
22976 delete per_cu->cu;
22977 per_cu->cu = NULL;
22978 *last_chain = next_cu;
22979 break;
22980 }
22981 else
22982 last_chain = &per_cu->cu->read_in_chain;
22983
22984 per_cu = next_cu;
22985 }
22986 }
22987
22988 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22989 We store these in a hash table separate from the DIEs, and preserve them
22990 when the DIEs are flushed out of cache.
22991
22992 The CU "per_cu" pointer is needed because offset alone is not enough to
22993 uniquely identify the type. A file may have multiple .debug_types sections,
22994 or the type may come from a DWO file. Furthermore, while it's more logical
22995 to use per_cu->section+offset, with Fission the section with the data is in
22996 the DWO file but we don't know that section at the point we need it.
22997 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22998 because we can enter the lookup routine, get_die_type_at_offset, from
22999 outside this file, and thus won't necessarily have PER_CU->cu.
23000 Fortunately, PER_CU is stable for the life of the objfile. */
23001
23002 struct dwarf2_per_cu_offset_and_type
23003 {
23004 const struct dwarf2_per_cu_data *per_cu;
23005 sect_offset sect_off;
23006 struct type *type;
23007 };
23008
23009 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23010
23011 static hashval_t
23012 per_cu_offset_and_type_hash (const void *item)
23013 {
23014 const struct dwarf2_per_cu_offset_and_type *ofs
23015 = (const struct dwarf2_per_cu_offset_and_type *) item;
23016
23017 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23018 }
23019
23020 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23021
23022 static int
23023 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23024 {
23025 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23026 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23027 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23028 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23029
23030 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23031 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23032 }
23033
23034 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23035 table if necessary. For convenience, return TYPE.
23036
23037 The DIEs reading must have careful ordering to:
23038 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23039 reading current DIE.
23040 * Not trying to dereference contents of still incompletely read in types
23041 while reading in other DIEs.
23042 * Enable referencing still incompletely read in types just by a pointer to
23043 the type without accessing its fields.
23044
23045 Therefore caller should follow these rules:
23046 * Try to fetch any prerequisite types we may need to build this DIE type
23047 before building the type and calling set_die_type.
23048 * After building type call set_die_type for current DIE as soon as
23049 possible before fetching more types to complete the current type.
23050 * Make the type as complete as possible before fetching more types. */
23051
23052 static struct type *
23053 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23054 {
23055 struct dwarf2_per_objfile *dwarf2_per_objfile
23056 = cu->per_cu->dwarf2_per_objfile;
23057 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23058 struct objfile *objfile = dwarf2_per_objfile->objfile;
23059 struct attribute *attr;
23060 struct dynamic_prop prop;
23061
23062 /* For Ada types, make sure that the gnat-specific data is always
23063 initialized (if not already set). There are a few types where
23064 we should not be doing so, because the type-specific area is
23065 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23066 where the type-specific area is used to store the floatformat).
23067 But this is not a problem, because the gnat-specific information
23068 is actually not needed for these types. */
23069 if (need_gnat_info (cu)
23070 && TYPE_CODE (type) != TYPE_CODE_FUNC
23071 && TYPE_CODE (type) != TYPE_CODE_FLT
23072 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23073 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23074 && TYPE_CODE (type) != TYPE_CODE_METHOD
23075 && !HAVE_GNAT_AUX_INFO (type))
23076 INIT_GNAT_SPECIFIC (type);
23077
23078 /* Read DW_AT_allocated and set in type. */
23079 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23080 if (attr != NULL && attr->form_is_block ())
23081 {
23082 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23083 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23084 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23085 }
23086 else if (attr != NULL)
23087 {
23088 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23089 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23090 sect_offset_str (die->sect_off));
23091 }
23092
23093 /* Read DW_AT_associated and set in type. */
23094 attr = dwarf2_attr (die, DW_AT_associated, cu);
23095 if (attr != NULL && attr->form_is_block ())
23096 {
23097 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23098 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23099 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23100 }
23101 else if (attr != NULL)
23102 {
23103 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23104 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23105 sect_offset_str (die->sect_off));
23106 }
23107
23108 /* Read DW_AT_data_location and set in type. */
23109 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23110 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23111 cu->per_cu->addr_type ()))
23112 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23113
23114 if (dwarf2_per_objfile->die_type_hash == NULL)
23115 dwarf2_per_objfile->die_type_hash
23116 = htab_up (htab_create_alloc (127,
23117 per_cu_offset_and_type_hash,
23118 per_cu_offset_and_type_eq,
23119 NULL, xcalloc, xfree));
23120
23121 ofs.per_cu = cu->per_cu;
23122 ofs.sect_off = die->sect_off;
23123 ofs.type = type;
23124 slot = (struct dwarf2_per_cu_offset_and_type **)
23125 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23126 if (*slot)
23127 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23128 sect_offset_str (die->sect_off));
23129 *slot = XOBNEW (&objfile->objfile_obstack,
23130 struct dwarf2_per_cu_offset_and_type);
23131 **slot = ofs;
23132 return type;
23133 }
23134
23135 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23136 or return NULL if the die does not have a saved type. */
23137
23138 static struct type *
23139 get_die_type_at_offset (sect_offset sect_off,
23140 struct dwarf2_per_cu_data *per_cu)
23141 {
23142 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23143 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23144
23145 if (dwarf2_per_objfile->die_type_hash == NULL)
23146 return NULL;
23147
23148 ofs.per_cu = per_cu;
23149 ofs.sect_off = sect_off;
23150 slot = ((struct dwarf2_per_cu_offset_and_type *)
23151 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23152 if (slot)
23153 return slot->type;
23154 else
23155 return NULL;
23156 }
23157
23158 /* Look up the type for DIE in CU in die_type_hash,
23159 or return NULL if DIE does not have a saved type. */
23160
23161 static struct type *
23162 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23163 {
23164 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23165 }
23166
23167 /* Add a dependence relationship from CU to REF_PER_CU. */
23168
23169 static void
23170 dwarf2_add_dependence (struct dwarf2_cu *cu,
23171 struct dwarf2_per_cu_data *ref_per_cu)
23172 {
23173 void **slot;
23174
23175 if (cu->dependencies == NULL)
23176 cu->dependencies
23177 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23178 NULL, &cu->comp_unit_obstack,
23179 hashtab_obstack_allocate,
23180 dummy_obstack_deallocate);
23181
23182 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23183 if (*slot == NULL)
23184 *slot = ref_per_cu;
23185 }
23186
23187 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23188 Set the mark field in every compilation unit in the
23189 cache that we must keep because we are keeping CU. */
23190
23191 static int
23192 dwarf2_mark_helper (void **slot, void *data)
23193 {
23194 struct dwarf2_per_cu_data *per_cu;
23195
23196 per_cu = (struct dwarf2_per_cu_data *) *slot;
23197
23198 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23199 reading of the chain. As such dependencies remain valid it is not much
23200 useful to track and undo them during QUIT cleanups. */
23201 if (per_cu->cu == NULL)
23202 return 1;
23203
23204 if (per_cu->cu->mark)
23205 return 1;
23206 per_cu->cu->mark = true;
23207
23208 if (per_cu->cu->dependencies != NULL)
23209 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23210
23211 return 1;
23212 }
23213
23214 /* Set the mark field in CU and in every other compilation unit in the
23215 cache that we must keep because we are keeping CU. */
23216
23217 static void
23218 dwarf2_mark (struct dwarf2_cu *cu)
23219 {
23220 if (cu->mark)
23221 return;
23222 cu->mark = true;
23223 if (cu->dependencies != NULL)
23224 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23225 }
23226
23227 static void
23228 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23229 {
23230 while (per_cu)
23231 {
23232 per_cu->cu->mark = false;
23233 per_cu = per_cu->cu->read_in_chain;
23234 }
23235 }
23236
23237 /* Trivial hash function for partial_die_info: the hash value of a DIE
23238 is its offset in .debug_info for this objfile. */
23239
23240 static hashval_t
23241 partial_die_hash (const void *item)
23242 {
23243 const struct partial_die_info *part_die
23244 = (const struct partial_die_info *) item;
23245
23246 return to_underlying (part_die->sect_off);
23247 }
23248
23249 /* Trivial comparison function for partial_die_info structures: two DIEs
23250 are equal if they have the same offset. */
23251
23252 static int
23253 partial_die_eq (const void *item_lhs, const void *item_rhs)
23254 {
23255 const struct partial_die_info *part_die_lhs
23256 = (const struct partial_die_info *) item_lhs;
23257 const struct partial_die_info *part_die_rhs
23258 = (const struct partial_die_info *) item_rhs;
23259
23260 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23261 }
23262
23263 struct cmd_list_element *set_dwarf_cmdlist;
23264 struct cmd_list_element *show_dwarf_cmdlist;
23265
23266 static void
23267 set_dwarf_cmd (const char *args, int from_tty)
23268 {
23269 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23270 gdb_stdout);
23271 }
23272
23273 static void
23274 show_dwarf_cmd (const char *args, int from_tty)
23275 {
23276 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23277 }
23278
23279 static void
23280 show_check_physname (struct ui_file *file, int from_tty,
23281 struct cmd_list_element *c, const char *value)
23282 {
23283 fprintf_filtered (file,
23284 _("Whether to check \"physname\" is %s.\n"),
23285 value);
23286 }
23287
23288 void _initialize_dwarf2_read ();
23289 void
23290 _initialize_dwarf2_read ()
23291 {
23292 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23293 Set DWARF specific variables.\n\
23294 Configure DWARF variables such as the cache size."),
23295 &set_dwarf_cmdlist, "maintenance set dwarf ",
23296 0/*allow-unknown*/, &maintenance_set_cmdlist);
23297
23298 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23299 Show DWARF specific variables.\n\
23300 Show DWARF variables such as the cache size."),
23301 &show_dwarf_cmdlist, "maintenance show dwarf ",
23302 0/*allow-unknown*/, &maintenance_show_cmdlist);
23303
23304 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23305 &dwarf_max_cache_age, _("\
23306 Set the upper bound on the age of cached DWARF compilation units."), _("\
23307 Show the upper bound on the age of cached DWARF compilation units."), _("\
23308 A higher limit means that cached compilation units will be stored\n\
23309 in memory longer, and more total memory will be used. Zero disables\n\
23310 caching, which can slow down startup."),
23311 NULL,
23312 show_dwarf_max_cache_age,
23313 &set_dwarf_cmdlist,
23314 &show_dwarf_cmdlist);
23315
23316 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23317 Set debugging of the DWARF reader."), _("\
23318 Show debugging of the DWARF reader."), _("\
23319 When enabled (non-zero), debugging messages are printed during DWARF\n\
23320 reading and symtab expansion. A value of 1 (one) provides basic\n\
23321 information. A value greater than 1 provides more verbose information."),
23322 NULL,
23323 NULL,
23324 &setdebuglist, &showdebuglist);
23325
23326 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23327 Set debugging of the DWARF DIE reader."), _("\
23328 Show debugging of the DWARF DIE reader."), _("\
23329 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23330 The value is the maximum depth to print."),
23331 NULL,
23332 NULL,
23333 &setdebuglist, &showdebuglist);
23334
23335 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23336 Set debugging of the dwarf line reader."), _("\
23337 Show debugging of the dwarf line reader."), _("\
23338 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23339 A value of 1 (one) provides basic information.\n\
23340 A value greater than 1 provides more verbose information."),
23341 NULL,
23342 NULL,
23343 &setdebuglist, &showdebuglist);
23344
23345 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23346 Set cross-checking of \"physname\" code against demangler."), _("\
23347 Show cross-checking of \"physname\" code against demangler."), _("\
23348 When enabled, GDB's internal \"physname\" code is checked against\n\
23349 the demangler."),
23350 NULL, show_check_physname,
23351 &setdebuglist, &showdebuglist);
23352
23353 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23354 no_class, &use_deprecated_index_sections, _("\
23355 Set whether to use deprecated gdb_index sections."), _("\
23356 Show whether to use deprecated gdb_index sections."), _("\
23357 When enabled, deprecated .gdb_index sections are used anyway.\n\
23358 Normally they are ignored either because of a missing feature or\n\
23359 performance issue.\n\
23360 Warning: This option must be enabled before gdb reads the file."),
23361 NULL,
23362 NULL,
23363 &setlist, &showlist);
23364
23365 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23366 &dwarf2_locexpr_funcs);
23367 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23368 &dwarf2_loclist_funcs);
23369
23370 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23371 &dwarf2_block_frame_base_locexpr_funcs);
23372 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23373 &dwarf2_block_frame_base_loclist_funcs);
23374
23375 #if GDB_SELF_TEST
23376 selftests::register_test ("dw2_expand_symtabs_matching",
23377 selftests::dw2_expand_symtabs_matching::run_test);
23378 selftests::register_test ("dwarf2_find_containing_comp_unit",
23379 selftests::find_containing_comp_unit::run_test);
23380 #endif
23381 }
This page took 0.833674 seconds and 4 git commands to generate.