[gdb/symtab] Add symbol with inherited DW_AT_const_value to psymtabs
[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 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15557 underlying_type = check_typedef (underlying_type);
15558 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
15559 if (TYPE_LENGTH (type) == 0)
15560 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
15561 if (TYPE_RAW_ALIGN (type) == 0
15562 && TYPE_RAW_ALIGN (underlying_type) != 0)
15563 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
15564 }
15565
15566 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15567
15568 return set_die_type (die, type, cu);
15569 }
15570
15571 /* Given a pointer to a die which begins an enumeration, process all
15572 the dies that define the members of the enumeration, and create the
15573 symbol for the enumeration type.
15574
15575 NOTE: We reverse the order of the element list. */
15576
15577 static void
15578 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15579 {
15580 struct type *this_type;
15581
15582 this_type = get_die_type (die, cu);
15583 if (this_type == NULL)
15584 this_type = read_enumeration_type (die, cu);
15585
15586 if (die->child != NULL)
15587 {
15588 struct die_info *child_die;
15589 struct symbol *sym;
15590 std::vector<struct field> fields;
15591 const char *name;
15592
15593 child_die = die->child;
15594 while (child_die && child_die->tag)
15595 {
15596 if (child_die->tag != DW_TAG_enumerator)
15597 {
15598 process_die (child_die, cu);
15599 }
15600 else
15601 {
15602 name = dwarf2_name (child_die, cu);
15603 if (name)
15604 {
15605 sym = new_symbol (child_die, this_type, cu);
15606
15607 fields.emplace_back ();
15608 struct field &field = fields.back ();
15609
15610 FIELD_NAME (field) = sym->linkage_name ();
15611 FIELD_TYPE (field) = NULL;
15612 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15613 FIELD_BITSIZE (field) = 0;
15614 }
15615 }
15616
15617 child_die = child_die->sibling;
15618 }
15619
15620 if (!fields.empty ())
15621 {
15622 TYPE_NFIELDS (this_type) = fields.size ();
15623 TYPE_FIELDS (this_type) = (struct field *)
15624 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15625 memcpy (TYPE_FIELDS (this_type), fields.data (),
15626 sizeof (struct field) * fields.size ());
15627 }
15628 }
15629
15630 /* If we are reading an enum from a .debug_types unit, and the enum
15631 is a declaration, and the enum is not the signatured type in the
15632 unit, then we do not want to add a symbol for it. Adding a
15633 symbol would in some cases obscure the true definition of the
15634 enum, giving users an incomplete type when the definition is
15635 actually available. Note that we do not want to do this for all
15636 enums which are just declarations, because C++0x allows forward
15637 enum declarations. */
15638 if (cu->per_cu->is_debug_types
15639 && die_is_declaration (die, cu))
15640 {
15641 struct signatured_type *sig_type;
15642
15643 sig_type = (struct signatured_type *) cu->per_cu;
15644 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15645 if (sig_type->type_offset_in_section != die->sect_off)
15646 return;
15647 }
15648
15649 new_symbol (die, this_type, cu);
15650 }
15651
15652 /* Extract all information from a DW_TAG_array_type DIE and put it in
15653 the DIE's type field. For now, this only handles one dimensional
15654 arrays. */
15655
15656 static struct type *
15657 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15658 {
15659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15660 struct die_info *child_die;
15661 struct type *type;
15662 struct type *element_type, *range_type, *index_type;
15663 struct attribute *attr;
15664 const char *name;
15665 struct dynamic_prop *byte_stride_prop = NULL;
15666 unsigned int bit_stride = 0;
15667
15668 element_type = die_type (die, cu);
15669
15670 /* The die_type call above may have already set the type for this DIE. */
15671 type = get_die_type (die, cu);
15672 if (type)
15673 return type;
15674
15675 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15676 if (attr != NULL)
15677 {
15678 int stride_ok;
15679 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15680
15681 byte_stride_prop
15682 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15683 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15684 prop_type);
15685 if (!stride_ok)
15686 {
15687 complaint (_("unable to read array DW_AT_byte_stride "
15688 " - DIE at %s [in module %s]"),
15689 sect_offset_str (die->sect_off),
15690 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15691 /* Ignore this attribute. We will likely not be able to print
15692 arrays of this type correctly, but there is little we can do
15693 to help if we cannot read the attribute's value. */
15694 byte_stride_prop = NULL;
15695 }
15696 }
15697
15698 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15699 if (attr != NULL)
15700 bit_stride = DW_UNSND (attr);
15701
15702 /* Irix 6.2 native cc creates array types without children for
15703 arrays with unspecified length. */
15704 if (die->child == NULL)
15705 {
15706 index_type = objfile_type (objfile)->builtin_int;
15707 range_type = create_static_range_type (NULL, index_type, 0, -1);
15708 type = create_array_type_with_stride (NULL, element_type, range_type,
15709 byte_stride_prop, bit_stride);
15710 return set_die_type (die, type, cu);
15711 }
15712
15713 std::vector<struct type *> range_types;
15714 child_die = die->child;
15715 while (child_die && child_die->tag)
15716 {
15717 if (child_die->tag == DW_TAG_subrange_type)
15718 {
15719 struct type *child_type = read_type_die (child_die, cu);
15720
15721 if (child_type != NULL)
15722 {
15723 /* The range type was succesfully read. Save it for the
15724 array type creation. */
15725 range_types.push_back (child_type);
15726 }
15727 }
15728 child_die = child_die->sibling;
15729 }
15730
15731 /* Dwarf2 dimensions are output from left to right, create the
15732 necessary array types in backwards order. */
15733
15734 type = element_type;
15735
15736 if (read_array_order (die, cu) == DW_ORD_col_major)
15737 {
15738 int i = 0;
15739
15740 while (i < range_types.size ())
15741 type = create_array_type_with_stride (NULL, type, range_types[i++],
15742 byte_stride_prop, bit_stride);
15743 }
15744 else
15745 {
15746 size_t ndim = range_types.size ();
15747 while (ndim-- > 0)
15748 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15749 byte_stride_prop, bit_stride);
15750 }
15751
15752 /* Understand Dwarf2 support for vector types (like they occur on
15753 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15754 array type. This is not part of the Dwarf2/3 standard yet, but a
15755 custom vendor extension. The main difference between a regular
15756 array and the vector variant is that vectors are passed by value
15757 to functions. */
15758 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15759 if (attr != nullptr)
15760 make_vector_type (type);
15761
15762 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15763 implementation may choose to implement triple vectors using this
15764 attribute. */
15765 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15766 if (attr != nullptr)
15767 {
15768 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15769 TYPE_LENGTH (type) = DW_UNSND (attr);
15770 else
15771 complaint (_("DW_AT_byte_size for array type smaller "
15772 "than the total size of elements"));
15773 }
15774
15775 name = dwarf2_name (die, cu);
15776 if (name)
15777 TYPE_NAME (type) = name;
15778
15779 maybe_set_alignment (cu, die, type);
15780
15781 /* Install the type in the die. */
15782 set_die_type (die, type, cu);
15783
15784 /* set_die_type should be already done. */
15785 set_descriptive_type (type, die, cu);
15786
15787 return type;
15788 }
15789
15790 static enum dwarf_array_dim_ordering
15791 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15792 {
15793 struct attribute *attr;
15794
15795 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15796
15797 if (attr != nullptr)
15798 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15799
15800 /* GNU F77 is a special case, as at 08/2004 array type info is the
15801 opposite order to the dwarf2 specification, but data is still
15802 laid out as per normal fortran.
15803
15804 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15805 version checking. */
15806
15807 if (cu->language == language_fortran
15808 && cu->producer && strstr (cu->producer, "GNU F77"))
15809 {
15810 return DW_ORD_row_major;
15811 }
15812
15813 switch (cu->language_defn->la_array_ordering)
15814 {
15815 case array_column_major:
15816 return DW_ORD_col_major;
15817 case array_row_major:
15818 default:
15819 return DW_ORD_row_major;
15820 };
15821 }
15822
15823 /* Extract all information from a DW_TAG_set_type DIE and put it in
15824 the DIE's type field. */
15825
15826 static struct type *
15827 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15828 {
15829 struct type *domain_type, *set_type;
15830 struct attribute *attr;
15831
15832 domain_type = die_type (die, cu);
15833
15834 /* The die_type call above may have already set the type for this DIE. */
15835 set_type = get_die_type (die, cu);
15836 if (set_type)
15837 return set_type;
15838
15839 set_type = create_set_type (NULL, domain_type);
15840
15841 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15842 if (attr != nullptr)
15843 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15844
15845 maybe_set_alignment (cu, die, set_type);
15846
15847 return set_die_type (die, set_type, cu);
15848 }
15849
15850 /* A helper for read_common_block that creates a locexpr baton.
15851 SYM is the symbol which we are marking as computed.
15852 COMMON_DIE is the DIE for the common block.
15853 COMMON_LOC is the location expression attribute for the common
15854 block itself.
15855 MEMBER_LOC is the location expression attribute for the particular
15856 member of the common block that we are processing.
15857 CU is the CU from which the above come. */
15858
15859 static void
15860 mark_common_block_symbol_computed (struct symbol *sym,
15861 struct die_info *common_die,
15862 struct attribute *common_loc,
15863 struct attribute *member_loc,
15864 struct dwarf2_cu *cu)
15865 {
15866 struct dwarf2_per_objfile *dwarf2_per_objfile
15867 = cu->per_cu->dwarf2_per_objfile;
15868 struct objfile *objfile = dwarf2_per_objfile->objfile;
15869 struct dwarf2_locexpr_baton *baton;
15870 gdb_byte *ptr;
15871 unsigned int cu_off;
15872 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15873 LONGEST offset = 0;
15874
15875 gdb_assert (common_loc && member_loc);
15876 gdb_assert (common_loc->form_is_block ());
15877 gdb_assert (member_loc->form_is_block ()
15878 || member_loc->form_is_constant ());
15879
15880 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15881 baton->per_cu = cu->per_cu;
15882 gdb_assert (baton->per_cu);
15883
15884 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15885
15886 if (member_loc->form_is_constant ())
15887 {
15888 offset = member_loc->constant_value (0);
15889 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15890 }
15891 else
15892 baton->size += DW_BLOCK (member_loc)->size;
15893
15894 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15895 baton->data = ptr;
15896
15897 *ptr++ = DW_OP_call4;
15898 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15899 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15900 ptr += 4;
15901
15902 if (member_loc->form_is_constant ())
15903 {
15904 *ptr++ = DW_OP_addr;
15905 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15906 ptr += cu->header.addr_size;
15907 }
15908 else
15909 {
15910 /* We have to copy the data here, because DW_OP_call4 will only
15911 use a DW_AT_location attribute. */
15912 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15913 ptr += DW_BLOCK (member_loc)->size;
15914 }
15915
15916 *ptr++ = DW_OP_plus;
15917 gdb_assert (ptr - baton->data == baton->size);
15918
15919 SYMBOL_LOCATION_BATON (sym) = baton;
15920 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15921 }
15922
15923 /* Create appropriate locally-scoped variables for all the
15924 DW_TAG_common_block entries. Also create a struct common_block
15925 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15926 is used to separate the common blocks name namespace from regular
15927 variable names. */
15928
15929 static void
15930 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15931 {
15932 struct attribute *attr;
15933
15934 attr = dwarf2_attr (die, DW_AT_location, cu);
15935 if (attr != nullptr)
15936 {
15937 /* Support the .debug_loc offsets. */
15938 if (attr->form_is_block ())
15939 {
15940 /* Ok. */
15941 }
15942 else if (attr->form_is_section_offset ())
15943 {
15944 dwarf2_complex_location_expr_complaint ();
15945 attr = NULL;
15946 }
15947 else
15948 {
15949 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15950 "common block member");
15951 attr = NULL;
15952 }
15953 }
15954
15955 if (die->child != NULL)
15956 {
15957 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15958 struct die_info *child_die;
15959 size_t n_entries = 0, size;
15960 struct common_block *common_block;
15961 struct symbol *sym;
15962
15963 for (child_die = die->child;
15964 child_die && child_die->tag;
15965 child_die = child_die->sibling)
15966 ++n_entries;
15967
15968 size = (sizeof (struct common_block)
15969 + (n_entries - 1) * sizeof (struct symbol *));
15970 common_block
15971 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15972 size);
15973 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15974 common_block->n_entries = 0;
15975
15976 for (child_die = die->child;
15977 child_die && child_die->tag;
15978 child_die = child_die->sibling)
15979 {
15980 /* Create the symbol in the DW_TAG_common_block block in the current
15981 symbol scope. */
15982 sym = new_symbol (child_die, NULL, cu);
15983 if (sym != NULL)
15984 {
15985 struct attribute *member_loc;
15986
15987 common_block->contents[common_block->n_entries++] = sym;
15988
15989 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15990 cu);
15991 if (member_loc)
15992 {
15993 /* GDB has handled this for a long time, but it is
15994 not specified by DWARF. It seems to have been
15995 emitted by gfortran at least as recently as:
15996 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15997 complaint (_("Variable in common block has "
15998 "DW_AT_data_member_location "
15999 "- DIE at %s [in module %s]"),
16000 sect_offset_str (child_die->sect_off),
16001 objfile_name (objfile));
16002
16003 if (member_loc->form_is_section_offset ())
16004 dwarf2_complex_location_expr_complaint ();
16005 else if (member_loc->form_is_constant ()
16006 || member_loc->form_is_block ())
16007 {
16008 if (attr != nullptr)
16009 mark_common_block_symbol_computed (sym, die, attr,
16010 member_loc, cu);
16011 }
16012 else
16013 dwarf2_complex_location_expr_complaint ();
16014 }
16015 }
16016 }
16017
16018 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16019 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16020 }
16021 }
16022
16023 /* Create a type for a C++ namespace. */
16024
16025 static struct type *
16026 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16027 {
16028 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16029 const char *previous_prefix, *name;
16030 int is_anonymous;
16031 struct type *type;
16032
16033 /* For extensions, reuse the type of the original namespace. */
16034 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16035 {
16036 struct die_info *ext_die;
16037 struct dwarf2_cu *ext_cu = cu;
16038
16039 ext_die = dwarf2_extension (die, &ext_cu);
16040 type = read_type_die (ext_die, ext_cu);
16041
16042 /* EXT_CU may not be the same as CU.
16043 Ensure TYPE is recorded with CU in die_type_hash. */
16044 return set_die_type (die, type, cu);
16045 }
16046
16047 name = namespace_name (die, &is_anonymous, cu);
16048
16049 /* Now build the name of the current namespace. */
16050
16051 previous_prefix = determine_prefix (die, cu);
16052 if (previous_prefix[0] != '\0')
16053 name = typename_concat (&objfile->objfile_obstack,
16054 previous_prefix, name, 0, cu);
16055
16056 /* Create the type. */
16057 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16058
16059 return set_die_type (die, type, cu);
16060 }
16061
16062 /* Read a namespace scope. */
16063
16064 static void
16065 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16066 {
16067 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16068 int is_anonymous;
16069
16070 /* Add a symbol associated to this if we haven't seen the namespace
16071 before. Also, add a using directive if it's an anonymous
16072 namespace. */
16073
16074 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16075 {
16076 struct type *type;
16077
16078 type = read_type_die (die, cu);
16079 new_symbol (die, type, cu);
16080
16081 namespace_name (die, &is_anonymous, cu);
16082 if (is_anonymous)
16083 {
16084 const char *previous_prefix = determine_prefix (die, cu);
16085
16086 std::vector<const char *> excludes;
16087 add_using_directive (using_directives (cu),
16088 previous_prefix, TYPE_NAME (type), NULL,
16089 NULL, excludes, 0, &objfile->objfile_obstack);
16090 }
16091 }
16092
16093 if (die->child != NULL)
16094 {
16095 struct die_info *child_die = die->child;
16096
16097 while (child_die && child_die->tag)
16098 {
16099 process_die (child_die, cu);
16100 child_die = child_die->sibling;
16101 }
16102 }
16103 }
16104
16105 /* Read a Fortran module as type. This DIE can be only a declaration used for
16106 imported module. Still we need that type as local Fortran "use ... only"
16107 declaration imports depend on the created type in determine_prefix. */
16108
16109 static struct type *
16110 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16111 {
16112 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16113 const char *module_name;
16114 struct type *type;
16115
16116 module_name = dwarf2_name (die, cu);
16117 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16118
16119 return set_die_type (die, type, cu);
16120 }
16121
16122 /* Read a Fortran module. */
16123
16124 static void
16125 read_module (struct die_info *die, struct dwarf2_cu *cu)
16126 {
16127 struct die_info *child_die = die->child;
16128 struct type *type;
16129
16130 type = read_type_die (die, cu);
16131 new_symbol (die, type, cu);
16132
16133 while (child_die && child_die->tag)
16134 {
16135 process_die (child_die, cu);
16136 child_die = child_die->sibling;
16137 }
16138 }
16139
16140 /* Return the name of the namespace represented by DIE. Set
16141 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16142 namespace. */
16143
16144 static const char *
16145 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16146 {
16147 struct die_info *current_die;
16148 const char *name = NULL;
16149
16150 /* Loop through the extensions until we find a name. */
16151
16152 for (current_die = die;
16153 current_die != NULL;
16154 current_die = dwarf2_extension (die, &cu))
16155 {
16156 /* We don't use dwarf2_name here so that we can detect the absence
16157 of a name -> anonymous namespace. */
16158 name = dwarf2_string_attr (die, DW_AT_name, cu);
16159
16160 if (name != NULL)
16161 break;
16162 }
16163
16164 /* Is it an anonymous namespace? */
16165
16166 *is_anonymous = (name == NULL);
16167 if (*is_anonymous)
16168 name = CP_ANONYMOUS_NAMESPACE_STR;
16169
16170 return name;
16171 }
16172
16173 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16174 the user defined type vector. */
16175
16176 static struct type *
16177 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16178 {
16179 struct gdbarch *gdbarch
16180 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16181 struct comp_unit_head *cu_header = &cu->header;
16182 struct type *type;
16183 struct attribute *attr_byte_size;
16184 struct attribute *attr_address_class;
16185 int byte_size, addr_class;
16186 struct type *target_type;
16187
16188 target_type = die_type (die, cu);
16189
16190 /* The die_type call above may have already set the type for this DIE. */
16191 type = get_die_type (die, cu);
16192 if (type)
16193 return type;
16194
16195 type = lookup_pointer_type (target_type);
16196
16197 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16198 if (attr_byte_size)
16199 byte_size = DW_UNSND (attr_byte_size);
16200 else
16201 byte_size = cu_header->addr_size;
16202
16203 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16204 if (attr_address_class)
16205 addr_class = DW_UNSND (attr_address_class);
16206 else
16207 addr_class = DW_ADDR_none;
16208
16209 ULONGEST alignment = get_alignment (cu, die);
16210
16211 /* If the pointer size, alignment, or address class is different
16212 than the default, create a type variant marked as such and set
16213 the length accordingly. */
16214 if (TYPE_LENGTH (type) != byte_size
16215 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16216 && alignment != TYPE_RAW_ALIGN (type))
16217 || addr_class != DW_ADDR_none)
16218 {
16219 if (gdbarch_address_class_type_flags_p (gdbarch))
16220 {
16221 int type_flags;
16222
16223 type_flags = gdbarch_address_class_type_flags
16224 (gdbarch, byte_size, addr_class);
16225 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16226 == 0);
16227 type = make_type_with_address_space (type, type_flags);
16228 }
16229 else if (TYPE_LENGTH (type) != byte_size)
16230 {
16231 complaint (_("invalid pointer size %d"), byte_size);
16232 }
16233 else if (TYPE_RAW_ALIGN (type) != alignment)
16234 {
16235 complaint (_("Invalid DW_AT_alignment"
16236 " - DIE at %s [in module %s]"),
16237 sect_offset_str (die->sect_off),
16238 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16239 }
16240 else
16241 {
16242 /* Should we also complain about unhandled address classes? */
16243 }
16244 }
16245
16246 TYPE_LENGTH (type) = byte_size;
16247 set_type_align (type, alignment);
16248 return set_die_type (die, type, cu);
16249 }
16250
16251 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16252 the user defined type vector. */
16253
16254 static struct type *
16255 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16256 {
16257 struct type *type;
16258 struct type *to_type;
16259 struct type *domain;
16260
16261 to_type = die_type (die, cu);
16262 domain = die_containing_type (die, cu);
16263
16264 /* The calls above may have already set the type for this DIE. */
16265 type = get_die_type (die, cu);
16266 if (type)
16267 return type;
16268
16269 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16270 type = lookup_methodptr_type (to_type);
16271 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16272 {
16273 struct type *new_type
16274 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16275
16276 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16277 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16278 TYPE_VARARGS (to_type));
16279 type = lookup_methodptr_type (new_type);
16280 }
16281 else
16282 type = lookup_memberptr_type (to_type, domain);
16283
16284 return set_die_type (die, type, cu);
16285 }
16286
16287 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16288 the user defined type vector. */
16289
16290 static struct type *
16291 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16292 enum type_code refcode)
16293 {
16294 struct comp_unit_head *cu_header = &cu->header;
16295 struct type *type, *target_type;
16296 struct attribute *attr;
16297
16298 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16299
16300 target_type = die_type (die, cu);
16301
16302 /* The die_type call above may have already set the type for this DIE. */
16303 type = get_die_type (die, cu);
16304 if (type)
16305 return type;
16306
16307 type = lookup_reference_type (target_type, refcode);
16308 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16309 if (attr != nullptr)
16310 {
16311 TYPE_LENGTH (type) = DW_UNSND (attr);
16312 }
16313 else
16314 {
16315 TYPE_LENGTH (type) = cu_header->addr_size;
16316 }
16317 maybe_set_alignment (cu, die, type);
16318 return set_die_type (die, type, cu);
16319 }
16320
16321 /* Add the given cv-qualifiers to the element type of the array. GCC
16322 outputs DWARF type qualifiers that apply to an array, not the
16323 element type. But GDB relies on the array element type to carry
16324 the cv-qualifiers. This mimics section 6.7.3 of the C99
16325 specification. */
16326
16327 static struct type *
16328 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16329 struct type *base_type, int cnst, int voltl)
16330 {
16331 struct type *el_type, *inner_array;
16332
16333 base_type = copy_type (base_type);
16334 inner_array = base_type;
16335
16336 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16337 {
16338 TYPE_TARGET_TYPE (inner_array) =
16339 copy_type (TYPE_TARGET_TYPE (inner_array));
16340 inner_array = TYPE_TARGET_TYPE (inner_array);
16341 }
16342
16343 el_type = TYPE_TARGET_TYPE (inner_array);
16344 cnst |= TYPE_CONST (el_type);
16345 voltl |= TYPE_VOLATILE (el_type);
16346 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16347
16348 return set_die_type (die, base_type, cu);
16349 }
16350
16351 static struct type *
16352 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16353 {
16354 struct type *base_type, *cv_type;
16355
16356 base_type = die_type (die, cu);
16357
16358 /* The die_type call above may have already set the type for this DIE. */
16359 cv_type = get_die_type (die, cu);
16360 if (cv_type)
16361 return cv_type;
16362
16363 /* In case the const qualifier is applied to an array type, the element type
16364 is so qualified, not the array type (section 6.7.3 of C99). */
16365 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16366 return add_array_cv_type (die, cu, base_type, 1, 0);
16367
16368 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16369 return set_die_type (die, cv_type, cu);
16370 }
16371
16372 static struct type *
16373 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16374 {
16375 struct type *base_type, *cv_type;
16376
16377 base_type = die_type (die, cu);
16378
16379 /* The die_type call above may have already set the type for this DIE. */
16380 cv_type = get_die_type (die, cu);
16381 if (cv_type)
16382 return cv_type;
16383
16384 /* In case the volatile qualifier is applied to an array type, the
16385 element type is so qualified, not the array type (section 6.7.3
16386 of C99). */
16387 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16388 return add_array_cv_type (die, cu, base_type, 0, 1);
16389
16390 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16391 return set_die_type (die, cv_type, cu);
16392 }
16393
16394 /* Handle DW_TAG_restrict_type. */
16395
16396 static struct type *
16397 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16398 {
16399 struct type *base_type, *cv_type;
16400
16401 base_type = die_type (die, cu);
16402
16403 /* The die_type call above may have already set the type for this DIE. */
16404 cv_type = get_die_type (die, cu);
16405 if (cv_type)
16406 return cv_type;
16407
16408 cv_type = make_restrict_type (base_type);
16409 return set_die_type (die, cv_type, cu);
16410 }
16411
16412 /* Handle DW_TAG_atomic_type. */
16413
16414 static struct type *
16415 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16416 {
16417 struct type *base_type, *cv_type;
16418
16419 base_type = die_type (die, cu);
16420
16421 /* The die_type call above may have already set the type for this DIE. */
16422 cv_type = get_die_type (die, cu);
16423 if (cv_type)
16424 return cv_type;
16425
16426 cv_type = make_atomic_type (base_type);
16427 return set_die_type (die, cv_type, cu);
16428 }
16429
16430 /* Extract all information from a DW_TAG_string_type DIE and add to
16431 the user defined type vector. It isn't really a user defined type,
16432 but it behaves like one, with other DIE's using an AT_user_def_type
16433 attribute to reference it. */
16434
16435 static struct type *
16436 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16437 {
16438 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16439 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16440 struct type *type, *range_type, *index_type, *char_type;
16441 struct attribute *attr;
16442 struct dynamic_prop prop;
16443 bool length_is_constant = true;
16444 LONGEST length;
16445
16446 /* There are a couple of places where bit sizes might be made use of
16447 when parsing a DW_TAG_string_type, however, no producer that we know
16448 of make use of these. Handling bit sizes that are a multiple of the
16449 byte size is easy enough, but what about other bit sizes? Lets deal
16450 with that problem when we have to. Warn about these attributes being
16451 unsupported, then parse the type and ignore them like we always
16452 have. */
16453 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16454 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16455 {
16456 static bool warning_printed = false;
16457 if (!warning_printed)
16458 {
16459 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16460 "currently supported on DW_TAG_string_type."));
16461 warning_printed = true;
16462 }
16463 }
16464
16465 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16466 if (attr != nullptr && !attr->form_is_constant ())
16467 {
16468 /* The string length describes the location at which the length of
16469 the string can be found. The size of the length field can be
16470 specified with one of the attributes below. */
16471 struct type *prop_type;
16472 struct attribute *len
16473 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16474 if (len == nullptr)
16475 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16476 if (len != nullptr && len->form_is_constant ())
16477 {
16478 /* Pass 0 as the default as we know this attribute is constant
16479 and the default value will not be returned. */
16480 LONGEST sz = len->constant_value (0);
16481 prop_type = cu->per_cu->int_type (sz, true);
16482 }
16483 else
16484 {
16485 /* If the size is not specified then we assume it is the size of
16486 an address on this target. */
16487 prop_type = cu->per_cu->addr_sized_int_type (true);
16488 }
16489
16490 /* Convert the attribute into a dynamic property. */
16491 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16492 length = 1;
16493 else
16494 length_is_constant = false;
16495 }
16496 else if (attr != nullptr)
16497 {
16498 /* This DW_AT_string_length just contains the length with no
16499 indirection. There's no need to create a dynamic property in this
16500 case. Pass 0 for the default value as we know it will not be
16501 returned in this case. */
16502 length = attr->constant_value (0);
16503 }
16504 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16505 {
16506 /* We don't currently support non-constant byte sizes for strings. */
16507 length = attr->constant_value (1);
16508 }
16509 else
16510 {
16511 /* Use 1 as a fallback length if we have nothing else. */
16512 length = 1;
16513 }
16514
16515 index_type = objfile_type (objfile)->builtin_int;
16516 if (length_is_constant)
16517 range_type = create_static_range_type (NULL, index_type, 1, length);
16518 else
16519 {
16520 struct dynamic_prop low_bound;
16521
16522 low_bound.kind = PROP_CONST;
16523 low_bound.data.const_val = 1;
16524 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16525 }
16526 char_type = language_string_char_type (cu->language_defn, gdbarch);
16527 type = create_string_type (NULL, char_type, range_type);
16528
16529 return set_die_type (die, type, cu);
16530 }
16531
16532 /* Assuming that DIE corresponds to a function, returns nonzero
16533 if the function is prototyped. */
16534
16535 static int
16536 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16537 {
16538 struct attribute *attr;
16539
16540 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16541 if (attr && (DW_UNSND (attr) != 0))
16542 return 1;
16543
16544 /* The DWARF standard implies that the DW_AT_prototyped attribute
16545 is only meaningful for C, but the concept also extends to other
16546 languages that allow unprototyped functions (Eg: Objective C).
16547 For all other languages, assume that functions are always
16548 prototyped. */
16549 if (cu->language != language_c
16550 && cu->language != language_objc
16551 && cu->language != language_opencl)
16552 return 1;
16553
16554 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16555 prototyped and unprototyped functions; default to prototyped,
16556 since that is more common in modern code (and RealView warns
16557 about unprototyped functions). */
16558 if (producer_is_realview (cu->producer))
16559 return 1;
16560
16561 return 0;
16562 }
16563
16564 /* Handle DIES due to C code like:
16565
16566 struct foo
16567 {
16568 int (*funcp)(int a, long l);
16569 int b;
16570 };
16571
16572 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16573
16574 static struct type *
16575 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16576 {
16577 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16578 struct type *type; /* Type that this function returns. */
16579 struct type *ftype; /* Function that returns above type. */
16580 struct attribute *attr;
16581
16582 type = die_type (die, cu);
16583
16584 /* The die_type call above may have already set the type for this DIE. */
16585 ftype = get_die_type (die, cu);
16586 if (ftype)
16587 return ftype;
16588
16589 ftype = lookup_function_type (type);
16590
16591 if (prototyped_function_p (die, cu))
16592 TYPE_PROTOTYPED (ftype) = 1;
16593
16594 /* Store the calling convention in the type if it's available in
16595 the subroutine die. Otherwise set the calling convention to
16596 the default value DW_CC_normal. */
16597 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16598 if (attr != nullptr
16599 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16600 TYPE_CALLING_CONVENTION (ftype)
16601 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16602 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16603 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16604 else
16605 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16606
16607 /* Record whether the function returns normally to its caller or not
16608 if the DWARF producer set that information. */
16609 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16610 if (attr && (DW_UNSND (attr) != 0))
16611 TYPE_NO_RETURN (ftype) = 1;
16612
16613 /* We need to add the subroutine type to the die immediately so
16614 we don't infinitely recurse when dealing with parameters
16615 declared as the same subroutine type. */
16616 set_die_type (die, ftype, cu);
16617
16618 if (die->child != NULL)
16619 {
16620 struct type *void_type = objfile_type (objfile)->builtin_void;
16621 struct die_info *child_die;
16622 int nparams, iparams;
16623
16624 /* Count the number of parameters.
16625 FIXME: GDB currently ignores vararg functions, but knows about
16626 vararg member functions. */
16627 nparams = 0;
16628 child_die = die->child;
16629 while (child_die && child_die->tag)
16630 {
16631 if (child_die->tag == DW_TAG_formal_parameter)
16632 nparams++;
16633 else if (child_die->tag == DW_TAG_unspecified_parameters)
16634 TYPE_VARARGS (ftype) = 1;
16635 child_die = child_die->sibling;
16636 }
16637
16638 /* Allocate storage for parameters and fill them in. */
16639 TYPE_NFIELDS (ftype) = nparams;
16640 TYPE_FIELDS (ftype) = (struct field *)
16641 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16642
16643 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16644 even if we error out during the parameters reading below. */
16645 for (iparams = 0; iparams < nparams; iparams++)
16646 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16647
16648 iparams = 0;
16649 child_die = die->child;
16650 while (child_die && child_die->tag)
16651 {
16652 if (child_die->tag == DW_TAG_formal_parameter)
16653 {
16654 struct type *arg_type;
16655
16656 /* DWARF version 2 has no clean way to discern C++
16657 static and non-static member functions. G++ helps
16658 GDB by marking the first parameter for non-static
16659 member functions (which is the this pointer) as
16660 artificial. We pass this information to
16661 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16662
16663 DWARF version 3 added DW_AT_object_pointer, which GCC
16664 4.5 does not yet generate. */
16665 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16666 if (attr != nullptr)
16667 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16668 else
16669 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16670 arg_type = die_type (child_die, cu);
16671
16672 /* RealView does not mark THIS as const, which the testsuite
16673 expects. GCC marks THIS as const in method definitions,
16674 but not in the class specifications (GCC PR 43053). */
16675 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16676 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16677 {
16678 int is_this = 0;
16679 struct dwarf2_cu *arg_cu = cu;
16680 const char *name = dwarf2_name (child_die, cu);
16681
16682 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16683 if (attr != nullptr)
16684 {
16685 /* If the compiler emits this, use it. */
16686 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16687 is_this = 1;
16688 }
16689 else if (name && strcmp (name, "this") == 0)
16690 /* Function definitions will have the argument names. */
16691 is_this = 1;
16692 else if (name == NULL && iparams == 0)
16693 /* Declarations may not have the names, so like
16694 elsewhere in GDB, assume an artificial first
16695 argument is "this". */
16696 is_this = 1;
16697
16698 if (is_this)
16699 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16700 arg_type, 0);
16701 }
16702
16703 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16704 iparams++;
16705 }
16706 child_die = child_die->sibling;
16707 }
16708 }
16709
16710 return ftype;
16711 }
16712
16713 static struct type *
16714 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16715 {
16716 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16717 const char *name = NULL;
16718 struct type *this_type, *target_type;
16719
16720 name = dwarf2_full_name (NULL, die, cu);
16721 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16722 TYPE_TARGET_STUB (this_type) = 1;
16723 set_die_type (die, this_type, cu);
16724 target_type = die_type (die, cu);
16725 if (target_type != this_type)
16726 TYPE_TARGET_TYPE (this_type) = target_type;
16727 else
16728 {
16729 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16730 spec and cause infinite loops in GDB. */
16731 complaint (_("Self-referential DW_TAG_typedef "
16732 "- DIE at %s [in module %s]"),
16733 sect_offset_str (die->sect_off), objfile_name (objfile));
16734 TYPE_TARGET_TYPE (this_type) = NULL;
16735 }
16736 if (name == NULL)
16737 {
16738 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16739 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16740 Handle these by just returning the target type, rather than
16741 constructing an anonymous typedef type and trying to handle this
16742 elsewhere. */
16743 set_die_type (die, target_type, cu);
16744 return target_type;
16745 }
16746 return this_type;
16747 }
16748
16749 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16750 (which may be different from NAME) to the architecture back-end to allow
16751 it to guess the correct format if necessary. */
16752
16753 static struct type *
16754 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16755 const char *name_hint, enum bfd_endian byte_order)
16756 {
16757 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16758 const struct floatformat **format;
16759 struct type *type;
16760
16761 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16762 if (format)
16763 type = init_float_type (objfile, bits, name, format, byte_order);
16764 else
16765 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16766
16767 return type;
16768 }
16769
16770 /* Allocate an integer type of size BITS and name NAME. */
16771
16772 static struct type *
16773 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16774 int bits, int unsigned_p, const char *name)
16775 {
16776 struct type *type;
16777
16778 /* Versions of Intel's C Compiler generate an integer type called "void"
16779 instead of using DW_TAG_unspecified_type. This has been seen on
16780 at least versions 14, 17, and 18. */
16781 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16782 && strcmp (name, "void") == 0)
16783 type = objfile_type (objfile)->builtin_void;
16784 else
16785 type = init_integer_type (objfile, bits, unsigned_p, name);
16786
16787 return type;
16788 }
16789
16790 /* Initialise and return a floating point type of size BITS suitable for
16791 use as a component of a complex number. The NAME_HINT is passed through
16792 when initialising the floating point type and is the name of the complex
16793 type.
16794
16795 As DWARF doesn't currently provide an explicit name for the components
16796 of a complex number, but it can be helpful to have these components
16797 named, we try to select a suitable name based on the size of the
16798 component. */
16799 static struct type *
16800 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16801 struct objfile *objfile,
16802 int bits, const char *name_hint,
16803 enum bfd_endian byte_order)
16804 {
16805 gdbarch *gdbarch = get_objfile_arch (objfile);
16806 struct type *tt = nullptr;
16807
16808 /* Try to find a suitable floating point builtin type of size BITS.
16809 We're going to use the name of this type as the name for the complex
16810 target type that we are about to create. */
16811 switch (cu->language)
16812 {
16813 case language_fortran:
16814 switch (bits)
16815 {
16816 case 32:
16817 tt = builtin_f_type (gdbarch)->builtin_real;
16818 break;
16819 case 64:
16820 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16821 break;
16822 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16823 case 128:
16824 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16825 break;
16826 }
16827 break;
16828 default:
16829 switch (bits)
16830 {
16831 case 32:
16832 tt = builtin_type (gdbarch)->builtin_float;
16833 break;
16834 case 64:
16835 tt = builtin_type (gdbarch)->builtin_double;
16836 break;
16837 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16838 case 128:
16839 tt = builtin_type (gdbarch)->builtin_long_double;
16840 break;
16841 }
16842 break;
16843 }
16844
16845 /* If the type we found doesn't match the size we were looking for, then
16846 pretend we didn't find a type at all, the complex target type we
16847 create will then be nameless. */
16848 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16849 tt = nullptr;
16850
16851 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16852 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16853 }
16854
16855 /* Find a representation of a given base type and install
16856 it in the TYPE field of the die. */
16857
16858 static struct type *
16859 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16860 {
16861 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16862 struct type *type;
16863 struct attribute *attr;
16864 int encoding = 0, bits = 0;
16865 const char *name;
16866 gdbarch *arch;
16867
16868 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16869 if (attr != nullptr)
16870 encoding = DW_UNSND (attr);
16871 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16872 if (attr != nullptr)
16873 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16874 name = dwarf2_name (die, cu);
16875 if (!name)
16876 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16877
16878 arch = get_objfile_arch (objfile);
16879 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16880
16881 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16882 if (attr)
16883 {
16884 int endianity = DW_UNSND (attr);
16885
16886 switch (endianity)
16887 {
16888 case DW_END_big:
16889 byte_order = BFD_ENDIAN_BIG;
16890 break;
16891 case DW_END_little:
16892 byte_order = BFD_ENDIAN_LITTLE;
16893 break;
16894 default:
16895 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16896 break;
16897 }
16898 }
16899
16900 switch (encoding)
16901 {
16902 case DW_ATE_address:
16903 /* Turn DW_ATE_address into a void * pointer. */
16904 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16905 type = init_pointer_type (objfile, bits, name, type);
16906 break;
16907 case DW_ATE_boolean:
16908 type = init_boolean_type (objfile, bits, 1, name);
16909 break;
16910 case DW_ATE_complex_float:
16911 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16912 byte_order);
16913 if (TYPE_CODE (type) == TYPE_CODE_ERROR)
16914 {
16915 if (name == nullptr)
16916 {
16917 struct obstack *obstack
16918 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
16919 name = obconcat (obstack, "_Complex ", TYPE_NAME (type),
16920 nullptr);
16921 }
16922 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16923 }
16924 else
16925 type = init_complex_type (name, type);
16926 break;
16927 case DW_ATE_decimal_float:
16928 type = init_decfloat_type (objfile, bits, name);
16929 break;
16930 case DW_ATE_float:
16931 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16932 break;
16933 case DW_ATE_signed:
16934 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16935 break;
16936 case DW_ATE_unsigned:
16937 if (cu->language == language_fortran
16938 && name
16939 && startswith (name, "character("))
16940 type = init_character_type (objfile, bits, 1, name);
16941 else
16942 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16943 break;
16944 case DW_ATE_signed_char:
16945 if (cu->language == language_ada || cu->language == language_m2
16946 || cu->language == language_pascal
16947 || cu->language == language_fortran)
16948 type = init_character_type (objfile, bits, 0, name);
16949 else
16950 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16951 break;
16952 case DW_ATE_unsigned_char:
16953 if (cu->language == language_ada || cu->language == language_m2
16954 || cu->language == language_pascal
16955 || cu->language == language_fortran
16956 || cu->language == language_rust)
16957 type = init_character_type (objfile, bits, 1, name);
16958 else
16959 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16960 break;
16961 case DW_ATE_UTF:
16962 {
16963 if (bits == 16)
16964 type = builtin_type (arch)->builtin_char16;
16965 else if (bits == 32)
16966 type = builtin_type (arch)->builtin_char32;
16967 else
16968 {
16969 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
16970 bits);
16971 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16972 }
16973 return set_die_type (die, type, cu);
16974 }
16975 break;
16976
16977 default:
16978 complaint (_("unsupported DW_AT_encoding: '%s'"),
16979 dwarf_type_encoding_name (encoding));
16980 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16981 break;
16982 }
16983
16984 if (name && strcmp (name, "char") == 0)
16985 TYPE_NOSIGN (type) = 1;
16986
16987 maybe_set_alignment (cu, die, type);
16988
16989 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
16990
16991 return set_die_type (die, type, cu);
16992 }
16993
16994 /* Parse dwarf attribute if it's a block, reference or constant and put the
16995 resulting value of the attribute into struct bound_prop.
16996 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16997
16998 static int
16999 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17000 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17001 struct type *default_type)
17002 {
17003 struct dwarf2_property_baton *baton;
17004 struct obstack *obstack
17005 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17006
17007 gdb_assert (default_type != NULL);
17008
17009 if (attr == NULL || prop == NULL)
17010 return 0;
17011
17012 if (attr->form_is_block ())
17013 {
17014 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17015 baton->property_type = default_type;
17016 baton->locexpr.per_cu = cu->per_cu;
17017 baton->locexpr.size = DW_BLOCK (attr)->size;
17018 baton->locexpr.data = DW_BLOCK (attr)->data;
17019 switch (attr->name)
17020 {
17021 case DW_AT_string_length:
17022 baton->locexpr.is_reference = true;
17023 break;
17024 default:
17025 baton->locexpr.is_reference = false;
17026 break;
17027 }
17028 prop->data.baton = baton;
17029 prop->kind = PROP_LOCEXPR;
17030 gdb_assert (prop->data.baton != NULL);
17031 }
17032 else if (attr->form_is_ref ())
17033 {
17034 struct dwarf2_cu *target_cu = cu;
17035 struct die_info *target_die;
17036 struct attribute *target_attr;
17037
17038 target_die = follow_die_ref (die, attr, &target_cu);
17039 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17040 if (target_attr == NULL)
17041 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17042 target_cu);
17043 if (target_attr == NULL)
17044 return 0;
17045
17046 switch (target_attr->name)
17047 {
17048 case DW_AT_location:
17049 if (target_attr->form_is_section_offset ())
17050 {
17051 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17052 baton->property_type = die_type (target_die, target_cu);
17053 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17054 prop->data.baton = baton;
17055 prop->kind = PROP_LOCLIST;
17056 gdb_assert (prop->data.baton != NULL);
17057 }
17058 else if (target_attr->form_is_block ())
17059 {
17060 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17061 baton->property_type = die_type (target_die, target_cu);
17062 baton->locexpr.per_cu = cu->per_cu;
17063 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17064 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17065 baton->locexpr.is_reference = true;
17066 prop->data.baton = baton;
17067 prop->kind = PROP_LOCEXPR;
17068 gdb_assert (prop->data.baton != NULL);
17069 }
17070 else
17071 {
17072 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17073 "dynamic property");
17074 return 0;
17075 }
17076 break;
17077 case DW_AT_data_member_location:
17078 {
17079 LONGEST offset;
17080
17081 if (!handle_data_member_location (target_die, target_cu,
17082 &offset))
17083 return 0;
17084
17085 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17086 baton->property_type = read_type_die (target_die->parent,
17087 target_cu);
17088 baton->offset_info.offset = offset;
17089 baton->offset_info.type = die_type (target_die, target_cu);
17090 prop->data.baton = baton;
17091 prop->kind = PROP_ADDR_OFFSET;
17092 break;
17093 }
17094 }
17095 }
17096 else if (attr->form_is_constant ())
17097 {
17098 prop->data.const_val = attr->constant_value (0);
17099 prop->kind = PROP_CONST;
17100 }
17101 else
17102 {
17103 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17104 dwarf2_name (die, cu));
17105 return 0;
17106 }
17107
17108 return 1;
17109 }
17110
17111 /* See read.h. */
17112
17113 struct type *
17114 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17115 {
17116 struct objfile *objfile = dwarf2_per_objfile->objfile;
17117 struct type *int_type;
17118
17119 /* Helper macro to examine the various builtin types. */
17120 #define TRY_TYPE(F) \
17121 int_type = (unsigned_p \
17122 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17123 : objfile_type (objfile)->builtin_ ## F); \
17124 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17125 return int_type
17126
17127 TRY_TYPE (char);
17128 TRY_TYPE (short);
17129 TRY_TYPE (int);
17130 TRY_TYPE (long);
17131 TRY_TYPE (long_long);
17132
17133 #undef TRY_TYPE
17134
17135 gdb_assert_not_reached ("unable to find suitable integer type");
17136 }
17137
17138 /* See read.h. */
17139
17140 struct type *
17141 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17142 {
17143 int addr_size = this->addr_size ();
17144 return int_type (addr_size, unsigned_p);
17145 }
17146
17147 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17148 present (which is valid) then compute the default type based on the
17149 compilation units address size. */
17150
17151 static struct type *
17152 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17153 {
17154 struct type *index_type = die_type (die, cu);
17155
17156 /* Dwarf-2 specifications explicitly allows to create subrange types
17157 without specifying a base type.
17158 In that case, the base type must be set to the type of
17159 the lower bound, upper bound or count, in that order, if any of these
17160 three attributes references an object that has a type.
17161 If no base type is found, the Dwarf-2 specifications say that
17162 a signed integer type of size equal to the size of an address should
17163 be used.
17164 For the following C code: `extern char gdb_int [];'
17165 GCC produces an empty range DIE.
17166 FIXME: muller/2010-05-28: Possible references to object for low bound,
17167 high bound or count are not yet handled by this code. */
17168 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17169 index_type = cu->per_cu->addr_sized_int_type (false);
17170
17171 return index_type;
17172 }
17173
17174 /* Read the given DW_AT_subrange DIE. */
17175
17176 static struct type *
17177 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17178 {
17179 struct type *base_type, *orig_base_type;
17180 struct type *range_type;
17181 struct attribute *attr;
17182 struct dynamic_prop low, high;
17183 int low_default_is_valid;
17184 int high_bound_is_count = 0;
17185 const char *name;
17186 ULONGEST negative_mask;
17187
17188 orig_base_type = read_subrange_index_type (die, cu);
17189
17190 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17191 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17192 creating the range type, but we use the result of check_typedef
17193 when examining properties of the type. */
17194 base_type = check_typedef (orig_base_type);
17195
17196 /* The die_type call above may have already set the type for this DIE. */
17197 range_type = get_die_type (die, cu);
17198 if (range_type)
17199 return range_type;
17200
17201 low.kind = PROP_CONST;
17202 high.kind = PROP_CONST;
17203 high.data.const_val = 0;
17204
17205 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17206 omitting DW_AT_lower_bound. */
17207 switch (cu->language)
17208 {
17209 case language_c:
17210 case language_cplus:
17211 low.data.const_val = 0;
17212 low_default_is_valid = 1;
17213 break;
17214 case language_fortran:
17215 low.data.const_val = 1;
17216 low_default_is_valid = 1;
17217 break;
17218 case language_d:
17219 case language_objc:
17220 case language_rust:
17221 low.data.const_val = 0;
17222 low_default_is_valid = (cu->header.version >= 4);
17223 break;
17224 case language_ada:
17225 case language_m2:
17226 case language_pascal:
17227 low.data.const_val = 1;
17228 low_default_is_valid = (cu->header.version >= 4);
17229 break;
17230 default:
17231 low.data.const_val = 0;
17232 low_default_is_valid = 0;
17233 break;
17234 }
17235
17236 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17237 if (attr != nullptr)
17238 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17239 else if (!low_default_is_valid)
17240 complaint (_("Missing DW_AT_lower_bound "
17241 "- DIE at %s [in module %s]"),
17242 sect_offset_str (die->sect_off),
17243 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17244
17245 struct attribute *attr_ub, *attr_count;
17246 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17247 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17248 {
17249 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17250 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17251 {
17252 /* If bounds are constant do the final calculation here. */
17253 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17254 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17255 else
17256 high_bound_is_count = 1;
17257 }
17258 else
17259 {
17260 if (attr_ub != NULL)
17261 complaint (_("Unresolved DW_AT_upper_bound "
17262 "- DIE at %s [in module %s]"),
17263 sect_offset_str (die->sect_off),
17264 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17265 if (attr_count != NULL)
17266 complaint (_("Unresolved DW_AT_count "
17267 "- DIE at %s [in module %s]"),
17268 sect_offset_str (die->sect_off),
17269 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17270 }
17271 }
17272
17273 LONGEST bias = 0;
17274 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17275 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17276 bias = bias_attr->constant_value (0);
17277
17278 /* Normally, the DWARF producers are expected to use a signed
17279 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17280 But this is unfortunately not always the case, as witnessed
17281 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17282 is used instead. To work around that ambiguity, we treat
17283 the bounds as signed, and thus sign-extend their values, when
17284 the base type is signed. */
17285 negative_mask =
17286 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17287 if (low.kind == PROP_CONST
17288 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17289 low.data.const_val |= negative_mask;
17290 if (high.kind == PROP_CONST
17291 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17292 high.data.const_val |= negative_mask;
17293
17294 /* Check for bit and byte strides. */
17295 struct dynamic_prop byte_stride_prop;
17296 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17297 if (attr_byte_stride != nullptr)
17298 {
17299 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17300 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17301 prop_type);
17302 }
17303
17304 struct dynamic_prop bit_stride_prop;
17305 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17306 if (attr_bit_stride != nullptr)
17307 {
17308 /* It only makes sense to have either a bit or byte stride. */
17309 if (attr_byte_stride != nullptr)
17310 {
17311 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17312 "- DIE at %s [in module %s]"),
17313 sect_offset_str (die->sect_off),
17314 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17315 attr_bit_stride = nullptr;
17316 }
17317 else
17318 {
17319 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17320 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17321 prop_type);
17322 }
17323 }
17324
17325 if (attr_byte_stride != nullptr
17326 || attr_bit_stride != nullptr)
17327 {
17328 bool byte_stride_p = (attr_byte_stride != nullptr);
17329 struct dynamic_prop *stride
17330 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17331
17332 range_type
17333 = create_range_type_with_stride (NULL, orig_base_type, &low,
17334 &high, bias, stride, byte_stride_p);
17335 }
17336 else
17337 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17338
17339 if (high_bound_is_count)
17340 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17341
17342 /* Ada expects an empty array on no boundary attributes. */
17343 if (attr == NULL && cu->language != language_ada)
17344 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17345
17346 name = dwarf2_name (die, cu);
17347 if (name)
17348 TYPE_NAME (range_type) = name;
17349
17350 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17351 if (attr != nullptr)
17352 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17353
17354 maybe_set_alignment (cu, die, range_type);
17355
17356 set_die_type (die, range_type, cu);
17357
17358 /* set_die_type should be already done. */
17359 set_descriptive_type (range_type, die, cu);
17360
17361 return range_type;
17362 }
17363
17364 static struct type *
17365 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17366 {
17367 struct type *type;
17368
17369 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17370 NULL);
17371 TYPE_NAME (type) = dwarf2_name (die, cu);
17372
17373 /* In Ada, an unspecified type is typically used when the description
17374 of the type is deferred to a different unit. When encountering
17375 such a type, we treat it as a stub, and try to resolve it later on,
17376 when needed. */
17377 if (cu->language == language_ada)
17378 TYPE_STUB (type) = 1;
17379
17380 return set_die_type (die, type, cu);
17381 }
17382
17383 /* Read a single die and all its descendents. Set the die's sibling
17384 field to NULL; set other fields in the die correctly, and set all
17385 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17386 location of the info_ptr after reading all of those dies. PARENT
17387 is the parent of the die in question. */
17388
17389 static struct die_info *
17390 read_die_and_children (const struct die_reader_specs *reader,
17391 const gdb_byte *info_ptr,
17392 const gdb_byte **new_info_ptr,
17393 struct die_info *parent)
17394 {
17395 struct die_info *die;
17396 const gdb_byte *cur_ptr;
17397
17398 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17399 if (die == NULL)
17400 {
17401 *new_info_ptr = cur_ptr;
17402 return NULL;
17403 }
17404 store_in_ref_table (die, reader->cu);
17405
17406 if (die->has_children)
17407 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17408 else
17409 {
17410 die->child = NULL;
17411 *new_info_ptr = cur_ptr;
17412 }
17413
17414 die->sibling = NULL;
17415 die->parent = parent;
17416 return die;
17417 }
17418
17419 /* Read a die, all of its descendents, and all of its siblings; set
17420 all of the fields of all of the dies correctly. Arguments are as
17421 in read_die_and_children. */
17422
17423 static struct die_info *
17424 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17425 const gdb_byte *info_ptr,
17426 const gdb_byte **new_info_ptr,
17427 struct die_info *parent)
17428 {
17429 struct die_info *first_die, *last_sibling;
17430 const gdb_byte *cur_ptr;
17431
17432 cur_ptr = info_ptr;
17433 first_die = last_sibling = NULL;
17434
17435 while (1)
17436 {
17437 struct die_info *die
17438 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17439
17440 if (die == NULL)
17441 {
17442 *new_info_ptr = cur_ptr;
17443 return first_die;
17444 }
17445
17446 if (!first_die)
17447 first_die = die;
17448 else
17449 last_sibling->sibling = die;
17450
17451 last_sibling = die;
17452 }
17453 }
17454
17455 /* Read a die, all of its descendents, and all of its siblings; set
17456 all of the fields of all of the dies correctly. Arguments are as
17457 in read_die_and_children.
17458 This the main entry point for reading a DIE and all its children. */
17459
17460 static struct die_info *
17461 read_die_and_siblings (const struct die_reader_specs *reader,
17462 const gdb_byte *info_ptr,
17463 const gdb_byte **new_info_ptr,
17464 struct die_info *parent)
17465 {
17466 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17467 new_info_ptr, parent);
17468
17469 if (dwarf_die_debug)
17470 {
17471 fprintf_unfiltered (gdb_stdlog,
17472 "Read die from %s@0x%x of %s:\n",
17473 reader->die_section->get_name (),
17474 (unsigned) (info_ptr - reader->die_section->buffer),
17475 bfd_get_filename (reader->abfd));
17476 dump_die (die, dwarf_die_debug);
17477 }
17478
17479 return die;
17480 }
17481
17482 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17483 attributes.
17484 The caller is responsible for filling in the extra attributes
17485 and updating (*DIEP)->num_attrs.
17486 Set DIEP to point to a newly allocated die with its information,
17487 except for its child, sibling, and parent fields. */
17488
17489 static const gdb_byte *
17490 read_full_die_1 (const struct die_reader_specs *reader,
17491 struct die_info **diep, const gdb_byte *info_ptr,
17492 int num_extra_attrs)
17493 {
17494 unsigned int abbrev_number, bytes_read, i;
17495 struct abbrev_info *abbrev;
17496 struct die_info *die;
17497 struct dwarf2_cu *cu = reader->cu;
17498 bfd *abfd = reader->abfd;
17499
17500 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17501 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17502 info_ptr += bytes_read;
17503 if (!abbrev_number)
17504 {
17505 *diep = NULL;
17506 return info_ptr;
17507 }
17508
17509 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17510 if (!abbrev)
17511 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17512 abbrev_number,
17513 bfd_get_filename (abfd));
17514
17515 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17516 die->sect_off = sect_off;
17517 die->tag = abbrev->tag;
17518 die->abbrev = abbrev_number;
17519 die->has_children = abbrev->has_children;
17520
17521 /* Make the result usable.
17522 The caller needs to update num_attrs after adding the extra
17523 attributes. */
17524 die->num_attrs = abbrev->num_attrs;
17525
17526 std::vector<int> indexes_that_need_reprocess;
17527 for (i = 0; i < abbrev->num_attrs; ++i)
17528 {
17529 bool need_reprocess;
17530 info_ptr =
17531 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17532 info_ptr, &need_reprocess);
17533 if (need_reprocess)
17534 indexes_that_need_reprocess.push_back (i);
17535 }
17536
17537 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17538 if (attr != nullptr)
17539 cu->str_offsets_base = DW_UNSND (attr);
17540
17541 auto maybe_addr_base = die->addr_base ();
17542 if (maybe_addr_base.has_value ())
17543 cu->addr_base = *maybe_addr_base;
17544 for (int index : indexes_that_need_reprocess)
17545 read_attribute_reprocess (reader, &die->attrs[index]);
17546 *diep = die;
17547 return info_ptr;
17548 }
17549
17550 /* Read a die and all its attributes.
17551 Set DIEP to point to a newly allocated die with its information,
17552 except for its child, sibling, and parent fields. */
17553
17554 static const gdb_byte *
17555 read_full_die (const struct die_reader_specs *reader,
17556 struct die_info **diep, const gdb_byte *info_ptr)
17557 {
17558 const gdb_byte *result;
17559
17560 result = read_full_die_1 (reader, diep, info_ptr, 0);
17561
17562 if (dwarf_die_debug)
17563 {
17564 fprintf_unfiltered (gdb_stdlog,
17565 "Read die from %s@0x%x of %s:\n",
17566 reader->die_section->get_name (),
17567 (unsigned) (info_ptr - reader->die_section->buffer),
17568 bfd_get_filename (reader->abfd));
17569 dump_die (*diep, dwarf_die_debug);
17570 }
17571
17572 return result;
17573 }
17574 \f
17575
17576 /* Returns nonzero if TAG represents a type that we might generate a partial
17577 symbol for. */
17578
17579 static int
17580 is_type_tag_for_partial (int tag)
17581 {
17582 switch (tag)
17583 {
17584 #if 0
17585 /* Some types that would be reasonable to generate partial symbols for,
17586 that we don't at present. */
17587 case DW_TAG_array_type:
17588 case DW_TAG_file_type:
17589 case DW_TAG_ptr_to_member_type:
17590 case DW_TAG_set_type:
17591 case DW_TAG_string_type:
17592 case DW_TAG_subroutine_type:
17593 #endif
17594 case DW_TAG_base_type:
17595 case DW_TAG_class_type:
17596 case DW_TAG_interface_type:
17597 case DW_TAG_enumeration_type:
17598 case DW_TAG_structure_type:
17599 case DW_TAG_subrange_type:
17600 case DW_TAG_typedef:
17601 case DW_TAG_union_type:
17602 return 1;
17603 default:
17604 return 0;
17605 }
17606 }
17607
17608 /* Load all DIEs that are interesting for partial symbols into memory. */
17609
17610 static struct partial_die_info *
17611 load_partial_dies (const struct die_reader_specs *reader,
17612 const gdb_byte *info_ptr, int building_psymtab)
17613 {
17614 struct dwarf2_cu *cu = reader->cu;
17615 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17616 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17617 unsigned int bytes_read;
17618 unsigned int load_all = 0;
17619 int nesting_level = 1;
17620
17621 parent_die = NULL;
17622 last_die = NULL;
17623
17624 gdb_assert (cu->per_cu != NULL);
17625 if (cu->per_cu->load_all_dies)
17626 load_all = 1;
17627
17628 cu->partial_dies
17629 = htab_create_alloc_ex (cu->header.length / 12,
17630 partial_die_hash,
17631 partial_die_eq,
17632 NULL,
17633 &cu->comp_unit_obstack,
17634 hashtab_obstack_allocate,
17635 dummy_obstack_deallocate);
17636
17637 while (1)
17638 {
17639 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17640
17641 /* A NULL abbrev means the end of a series of children. */
17642 if (abbrev == NULL)
17643 {
17644 if (--nesting_level == 0)
17645 return first_die;
17646
17647 info_ptr += bytes_read;
17648 last_die = parent_die;
17649 parent_die = parent_die->die_parent;
17650 continue;
17651 }
17652
17653 /* Check for template arguments. We never save these; if
17654 they're seen, we just mark the parent, and go on our way. */
17655 if (parent_die != NULL
17656 && cu->language == language_cplus
17657 && (abbrev->tag == DW_TAG_template_type_param
17658 || abbrev->tag == DW_TAG_template_value_param))
17659 {
17660 parent_die->has_template_arguments = 1;
17661
17662 if (!load_all)
17663 {
17664 /* We don't need a partial DIE for the template argument. */
17665 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17666 continue;
17667 }
17668 }
17669
17670 /* We only recurse into c++ subprograms looking for template arguments.
17671 Skip their other children. */
17672 if (!load_all
17673 && cu->language == language_cplus
17674 && parent_die != NULL
17675 && parent_die->tag == DW_TAG_subprogram)
17676 {
17677 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17678 continue;
17679 }
17680
17681 /* Check whether this DIE is interesting enough to save. Normally
17682 we would not be interested in members here, but there may be
17683 later variables referencing them via DW_AT_specification (for
17684 static members). */
17685 if (!load_all
17686 && !is_type_tag_for_partial (abbrev->tag)
17687 && abbrev->tag != DW_TAG_constant
17688 && abbrev->tag != DW_TAG_enumerator
17689 && abbrev->tag != DW_TAG_subprogram
17690 && abbrev->tag != DW_TAG_inlined_subroutine
17691 && abbrev->tag != DW_TAG_lexical_block
17692 && abbrev->tag != DW_TAG_variable
17693 && abbrev->tag != DW_TAG_namespace
17694 && abbrev->tag != DW_TAG_module
17695 && abbrev->tag != DW_TAG_member
17696 && abbrev->tag != DW_TAG_imported_unit
17697 && abbrev->tag != DW_TAG_imported_declaration)
17698 {
17699 /* Otherwise we skip to the next sibling, if any. */
17700 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17701 continue;
17702 }
17703
17704 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17705 abbrev);
17706
17707 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17708
17709 /* This two-pass algorithm for processing partial symbols has a
17710 high cost in cache pressure. Thus, handle some simple cases
17711 here which cover the majority of C partial symbols. DIEs
17712 which neither have specification tags in them, nor could have
17713 specification tags elsewhere pointing at them, can simply be
17714 processed and discarded.
17715
17716 This segment is also optional; scan_partial_symbols and
17717 add_partial_symbol will handle these DIEs if we chain
17718 them in normally. When compilers which do not emit large
17719 quantities of duplicate debug information are more common,
17720 this code can probably be removed. */
17721
17722 /* Any complete simple types at the top level (pretty much all
17723 of them, for a language without namespaces), can be processed
17724 directly. */
17725 if (parent_die == NULL
17726 && pdi.has_specification == 0
17727 && pdi.is_declaration == 0
17728 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17729 || pdi.tag == DW_TAG_base_type
17730 || pdi.tag == DW_TAG_subrange_type))
17731 {
17732 if (building_psymtab && pdi.name != NULL)
17733 add_psymbol_to_list (pdi.name, false,
17734 VAR_DOMAIN, LOC_TYPEDEF, -1,
17735 psymbol_placement::STATIC,
17736 0, cu->language, objfile);
17737 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17738 continue;
17739 }
17740
17741 /* The exception for DW_TAG_typedef with has_children above is
17742 a workaround of GCC PR debug/47510. In the case of this complaint
17743 type_name_or_error will error on such types later.
17744
17745 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17746 it could not find the child DIEs referenced later, this is checked
17747 above. In correct DWARF DW_TAG_typedef should have no children. */
17748
17749 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17750 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17751 "- DIE at %s [in module %s]"),
17752 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17753
17754 /* If we're at the second level, and we're an enumerator, and
17755 our parent has no specification (meaning possibly lives in a
17756 namespace elsewhere), then we can add the partial symbol now
17757 instead of queueing it. */
17758 if (pdi.tag == DW_TAG_enumerator
17759 && parent_die != NULL
17760 && parent_die->die_parent == NULL
17761 && parent_die->tag == DW_TAG_enumeration_type
17762 && parent_die->has_specification == 0)
17763 {
17764 if (pdi.name == NULL)
17765 complaint (_("malformed enumerator DIE ignored"));
17766 else if (building_psymtab)
17767 add_psymbol_to_list (pdi.name, false,
17768 VAR_DOMAIN, LOC_CONST, -1,
17769 cu->language == language_cplus
17770 ? psymbol_placement::GLOBAL
17771 : psymbol_placement::STATIC,
17772 0, cu->language, objfile);
17773
17774 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17775 continue;
17776 }
17777
17778 struct partial_die_info *part_die
17779 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17780
17781 /* We'll save this DIE so link it in. */
17782 part_die->die_parent = parent_die;
17783 part_die->die_sibling = NULL;
17784 part_die->die_child = NULL;
17785
17786 if (last_die && last_die == parent_die)
17787 last_die->die_child = part_die;
17788 else if (last_die)
17789 last_die->die_sibling = part_die;
17790
17791 last_die = part_die;
17792
17793 if (first_die == NULL)
17794 first_die = part_die;
17795
17796 /* Maybe add the DIE to the hash table. Not all DIEs that we
17797 find interesting need to be in the hash table, because we
17798 also have the parent/sibling/child chains; only those that we
17799 might refer to by offset later during partial symbol reading.
17800
17801 For now this means things that might have be the target of a
17802 DW_AT_specification, DW_AT_abstract_origin, or
17803 DW_AT_extension. DW_AT_extension will refer only to
17804 namespaces; DW_AT_abstract_origin refers to functions (and
17805 many things under the function DIE, but we do not recurse
17806 into function DIEs during partial symbol reading) and
17807 possibly variables as well; DW_AT_specification refers to
17808 declarations. Declarations ought to have the DW_AT_declaration
17809 flag. It happens that GCC forgets to put it in sometimes, but
17810 only for functions, not for types.
17811
17812 Adding more things than necessary to the hash table is harmless
17813 except for the performance cost. Adding too few will result in
17814 wasted time in find_partial_die, when we reread the compilation
17815 unit with load_all_dies set. */
17816
17817 if (load_all
17818 || abbrev->tag == DW_TAG_constant
17819 || abbrev->tag == DW_TAG_subprogram
17820 || abbrev->tag == DW_TAG_variable
17821 || abbrev->tag == DW_TAG_namespace
17822 || part_die->is_declaration)
17823 {
17824 void **slot;
17825
17826 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17827 to_underlying (part_die->sect_off),
17828 INSERT);
17829 *slot = part_die;
17830 }
17831
17832 /* For some DIEs we want to follow their children (if any). For C
17833 we have no reason to follow the children of structures; for other
17834 languages we have to, so that we can get at method physnames
17835 to infer fully qualified class names, for DW_AT_specification,
17836 and for C++ template arguments. For C++, we also look one level
17837 inside functions to find template arguments (if the name of the
17838 function does not already contain the template arguments).
17839
17840 For Ada and Fortran, we need to scan the children of subprograms
17841 and lexical blocks as well because these languages allow the
17842 definition of nested entities that could be interesting for the
17843 debugger, such as nested subprograms for instance. */
17844 if (last_die->has_children
17845 && (load_all
17846 || last_die->tag == DW_TAG_namespace
17847 || last_die->tag == DW_TAG_module
17848 || last_die->tag == DW_TAG_enumeration_type
17849 || (cu->language == language_cplus
17850 && last_die->tag == DW_TAG_subprogram
17851 && (last_die->name == NULL
17852 || strchr (last_die->name, '<') == NULL))
17853 || (cu->language != language_c
17854 && (last_die->tag == DW_TAG_class_type
17855 || last_die->tag == DW_TAG_interface_type
17856 || last_die->tag == DW_TAG_structure_type
17857 || last_die->tag == DW_TAG_union_type))
17858 || ((cu->language == language_ada
17859 || cu->language == language_fortran)
17860 && (last_die->tag == DW_TAG_subprogram
17861 || last_die->tag == DW_TAG_lexical_block))))
17862 {
17863 nesting_level++;
17864 parent_die = last_die;
17865 continue;
17866 }
17867
17868 /* Otherwise we skip to the next sibling, if any. */
17869 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17870
17871 /* Back to the top, do it again. */
17872 }
17873 }
17874
17875 partial_die_info::partial_die_info (sect_offset sect_off_,
17876 struct abbrev_info *abbrev)
17877 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17878 {
17879 }
17880
17881 /* Read a minimal amount of information into the minimal die structure.
17882 INFO_PTR should point just after the initial uleb128 of a DIE. */
17883
17884 const gdb_byte *
17885 partial_die_info::read (const struct die_reader_specs *reader,
17886 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17887 {
17888 struct dwarf2_cu *cu = reader->cu;
17889 struct dwarf2_per_objfile *dwarf2_per_objfile
17890 = cu->per_cu->dwarf2_per_objfile;
17891 unsigned int i;
17892 int has_low_pc_attr = 0;
17893 int has_high_pc_attr = 0;
17894 int high_pc_relative = 0;
17895
17896 for (i = 0; i < abbrev.num_attrs; ++i)
17897 {
17898 attribute attr;
17899 bool need_reprocess;
17900 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
17901 info_ptr, &need_reprocess);
17902 /* String and address offsets that need to do the reprocessing have
17903 already been read at this point, so there is no need to wait until
17904 the loop terminates to do the reprocessing. */
17905 if (need_reprocess)
17906 read_attribute_reprocess (reader, &attr);
17907 /* Store the data if it is of an attribute we want to keep in a
17908 partial symbol table. */
17909 switch (attr.name)
17910 {
17911 case DW_AT_name:
17912 switch (tag)
17913 {
17914 case DW_TAG_compile_unit:
17915 case DW_TAG_partial_unit:
17916 case DW_TAG_type_unit:
17917 /* Compilation units have a DW_AT_name that is a filename, not
17918 a source language identifier. */
17919 case DW_TAG_enumeration_type:
17920 case DW_TAG_enumerator:
17921 /* These tags always have simple identifiers already; no need
17922 to canonicalize them. */
17923 name = DW_STRING (&attr);
17924 break;
17925 default:
17926 {
17927 struct objfile *objfile = dwarf2_per_objfile->objfile;
17928
17929 name
17930 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
17931 }
17932 break;
17933 }
17934 break;
17935 case DW_AT_linkage_name:
17936 case DW_AT_MIPS_linkage_name:
17937 /* Note that both forms of linkage name might appear. We
17938 assume they will be the same, and we only store the last
17939 one we see. */
17940 linkage_name = DW_STRING (&attr);
17941 break;
17942 case DW_AT_low_pc:
17943 has_low_pc_attr = 1;
17944 lowpc = attr.value_as_address ();
17945 break;
17946 case DW_AT_high_pc:
17947 has_high_pc_attr = 1;
17948 highpc = attr.value_as_address ();
17949 if (cu->header.version >= 4 && attr.form_is_constant ())
17950 high_pc_relative = 1;
17951 break;
17952 case DW_AT_location:
17953 /* Support the .debug_loc offsets. */
17954 if (attr.form_is_block ())
17955 {
17956 d.locdesc = DW_BLOCK (&attr);
17957 }
17958 else if (attr.form_is_section_offset ())
17959 {
17960 dwarf2_complex_location_expr_complaint ();
17961 }
17962 else
17963 {
17964 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17965 "partial symbol information");
17966 }
17967 break;
17968 case DW_AT_external:
17969 is_external = DW_UNSND (&attr);
17970 break;
17971 case DW_AT_declaration:
17972 is_declaration = DW_UNSND (&attr);
17973 break;
17974 case DW_AT_type:
17975 has_type = 1;
17976 break;
17977 case DW_AT_abstract_origin:
17978 case DW_AT_specification:
17979 case DW_AT_extension:
17980 has_specification = 1;
17981 spec_offset = attr.get_ref_die_offset ();
17982 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17983 || cu->per_cu->is_dwz);
17984 break;
17985 case DW_AT_sibling:
17986 /* Ignore absolute siblings, they might point outside of
17987 the current compile unit. */
17988 if (attr.form == DW_FORM_ref_addr)
17989 complaint (_("ignoring absolute DW_AT_sibling"));
17990 else
17991 {
17992 const gdb_byte *buffer = reader->buffer;
17993 sect_offset off = attr.get_ref_die_offset ();
17994 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
17995
17996 if (sibling_ptr < info_ptr)
17997 complaint (_("DW_AT_sibling points backwards"));
17998 else if (sibling_ptr > reader->buffer_end)
17999 reader->die_section->overflow_complaint ();
18000 else
18001 sibling = sibling_ptr;
18002 }
18003 break;
18004 case DW_AT_byte_size:
18005 has_byte_size = 1;
18006 break;
18007 case DW_AT_const_value:
18008 has_const_value = 1;
18009 break;
18010 case DW_AT_calling_convention:
18011 /* DWARF doesn't provide a way to identify a program's source-level
18012 entry point. DW_AT_calling_convention attributes are only meant
18013 to describe functions' calling conventions.
18014
18015 However, because it's a necessary piece of information in
18016 Fortran, and before DWARF 4 DW_CC_program was the only
18017 piece of debugging information whose definition refers to
18018 a 'main program' at all, several compilers marked Fortran
18019 main programs with DW_CC_program --- even when those
18020 functions use the standard calling conventions.
18021
18022 Although DWARF now specifies a way to provide this
18023 information, we support this practice for backward
18024 compatibility. */
18025 if (DW_UNSND (&attr) == DW_CC_program
18026 && cu->language == language_fortran)
18027 main_subprogram = 1;
18028 break;
18029 case DW_AT_inline:
18030 if (DW_UNSND (&attr) == DW_INL_inlined
18031 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18032 may_be_inlined = 1;
18033 break;
18034
18035 case DW_AT_import:
18036 if (tag == DW_TAG_imported_unit)
18037 {
18038 d.sect_off = attr.get_ref_die_offset ();
18039 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18040 || cu->per_cu->is_dwz);
18041 }
18042 break;
18043
18044 case DW_AT_main_subprogram:
18045 main_subprogram = DW_UNSND (&attr);
18046 break;
18047
18048 case DW_AT_ranges:
18049 {
18050 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18051 but that requires a full DIE, so instead we just
18052 reimplement it. */
18053 int need_ranges_base = tag != DW_TAG_compile_unit;
18054 unsigned int ranges_offset = (DW_UNSND (&attr)
18055 + (need_ranges_base
18056 ? cu->ranges_base
18057 : 0));
18058
18059 /* Value of the DW_AT_ranges attribute is the offset in the
18060 .debug_ranges section. */
18061 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18062 nullptr))
18063 has_pc_info = 1;
18064 }
18065 break;
18066
18067 default:
18068 break;
18069 }
18070 }
18071
18072 /* For Ada, if both the name and the linkage name appear, we prefer
18073 the latter. This lets "catch exception" work better, regardless
18074 of the order in which the name and linkage name were emitted.
18075 Really, though, this is just a workaround for the fact that gdb
18076 doesn't store both the name and the linkage name. */
18077 if (cu->language == language_ada && linkage_name != nullptr)
18078 name = linkage_name;
18079
18080 if (high_pc_relative)
18081 highpc += lowpc;
18082
18083 if (has_low_pc_attr && has_high_pc_attr)
18084 {
18085 /* When using the GNU linker, .gnu.linkonce. sections are used to
18086 eliminate duplicate copies of functions and vtables and such.
18087 The linker will arbitrarily choose one and discard the others.
18088 The AT_*_pc values for such functions refer to local labels in
18089 these sections. If the section from that file was discarded, the
18090 labels are not in the output, so the relocs get a value of 0.
18091 If this is a discarded function, mark the pc bounds as invalid,
18092 so that GDB will ignore it. */
18093 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18094 {
18095 struct objfile *objfile = dwarf2_per_objfile->objfile;
18096 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18097
18098 complaint (_("DW_AT_low_pc %s is zero "
18099 "for DIE at %s [in module %s]"),
18100 paddress (gdbarch, lowpc),
18101 sect_offset_str (sect_off),
18102 objfile_name (objfile));
18103 }
18104 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18105 else if (lowpc >= highpc)
18106 {
18107 struct objfile *objfile = dwarf2_per_objfile->objfile;
18108 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18109
18110 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18111 "for DIE at %s [in module %s]"),
18112 paddress (gdbarch, lowpc),
18113 paddress (gdbarch, highpc),
18114 sect_offset_str (sect_off),
18115 objfile_name (objfile));
18116 }
18117 else
18118 has_pc_info = 1;
18119 }
18120
18121 return info_ptr;
18122 }
18123
18124 /* Find a cached partial DIE at OFFSET in CU. */
18125
18126 struct partial_die_info *
18127 dwarf2_cu::find_partial_die (sect_offset sect_off)
18128 {
18129 struct partial_die_info *lookup_die = NULL;
18130 struct partial_die_info part_die (sect_off);
18131
18132 lookup_die = ((struct partial_die_info *)
18133 htab_find_with_hash (partial_dies, &part_die,
18134 to_underlying (sect_off)));
18135
18136 return lookup_die;
18137 }
18138
18139 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18140 except in the case of .debug_types DIEs which do not reference
18141 outside their CU (they do however referencing other types via
18142 DW_FORM_ref_sig8). */
18143
18144 static const struct cu_partial_die_info
18145 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18146 {
18147 struct dwarf2_per_objfile *dwarf2_per_objfile
18148 = cu->per_cu->dwarf2_per_objfile;
18149 struct objfile *objfile = dwarf2_per_objfile->objfile;
18150 struct dwarf2_per_cu_data *per_cu = NULL;
18151 struct partial_die_info *pd = NULL;
18152
18153 if (offset_in_dwz == cu->per_cu->is_dwz
18154 && cu->header.offset_in_cu_p (sect_off))
18155 {
18156 pd = cu->find_partial_die (sect_off);
18157 if (pd != NULL)
18158 return { cu, pd };
18159 /* We missed recording what we needed.
18160 Load all dies and try again. */
18161 per_cu = cu->per_cu;
18162 }
18163 else
18164 {
18165 /* TUs don't reference other CUs/TUs (except via type signatures). */
18166 if (cu->per_cu->is_debug_types)
18167 {
18168 error (_("Dwarf Error: Type Unit at offset %s contains"
18169 " external reference to offset %s [in module %s].\n"),
18170 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18171 bfd_get_filename (objfile->obfd));
18172 }
18173 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18174 dwarf2_per_objfile);
18175
18176 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18177 load_partial_comp_unit (per_cu);
18178
18179 per_cu->cu->last_used = 0;
18180 pd = per_cu->cu->find_partial_die (sect_off);
18181 }
18182
18183 /* If we didn't find it, and not all dies have been loaded,
18184 load them all and try again. */
18185
18186 if (pd == NULL && per_cu->load_all_dies == 0)
18187 {
18188 per_cu->load_all_dies = 1;
18189
18190 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18191 THIS_CU->cu may already be in use. So we can't just free it and
18192 replace its DIEs with the ones we read in. Instead, we leave those
18193 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18194 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18195 set. */
18196 load_partial_comp_unit (per_cu);
18197
18198 pd = per_cu->cu->find_partial_die (sect_off);
18199 }
18200
18201 if (pd == NULL)
18202 internal_error (__FILE__, __LINE__,
18203 _("could not find partial DIE %s "
18204 "in cache [from module %s]\n"),
18205 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18206 return { per_cu->cu, pd };
18207 }
18208
18209 /* See if we can figure out if the class lives in a namespace. We do
18210 this by looking for a member function; its demangled name will
18211 contain namespace info, if there is any. */
18212
18213 static void
18214 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18215 struct dwarf2_cu *cu)
18216 {
18217 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18218 what template types look like, because the demangler
18219 frequently doesn't give the same name as the debug info. We
18220 could fix this by only using the demangled name to get the
18221 prefix (but see comment in read_structure_type). */
18222
18223 struct partial_die_info *real_pdi;
18224 struct partial_die_info *child_pdi;
18225
18226 /* If this DIE (this DIE's specification, if any) has a parent, then
18227 we should not do this. We'll prepend the parent's fully qualified
18228 name when we create the partial symbol. */
18229
18230 real_pdi = struct_pdi;
18231 while (real_pdi->has_specification)
18232 {
18233 auto res = find_partial_die (real_pdi->spec_offset,
18234 real_pdi->spec_is_dwz, cu);
18235 real_pdi = res.pdi;
18236 cu = res.cu;
18237 }
18238
18239 if (real_pdi->die_parent != NULL)
18240 return;
18241
18242 for (child_pdi = struct_pdi->die_child;
18243 child_pdi != NULL;
18244 child_pdi = child_pdi->die_sibling)
18245 {
18246 if (child_pdi->tag == DW_TAG_subprogram
18247 && child_pdi->linkage_name != NULL)
18248 {
18249 gdb::unique_xmalloc_ptr<char> actual_class_name
18250 (language_class_name_from_physname (cu->language_defn,
18251 child_pdi->linkage_name));
18252 if (actual_class_name != NULL)
18253 {
18254 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18255 struct_pdi->name = objfile->intern (actual_class_name.get ());
18256 }
18257 break;
18258 }
18259 }
18260 }
18261
18262 /* Return true if a DIE with TAG may have the DW_AT_const_value
18263 attribute. */
18264
18265 static bool
18266 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18267 {
18268 switch (tag)
18269 {
18270 case DW_TAG_constant:
18271 case DW_TAG_enumerator:
18272 case DW_TAG_formal_parameter:
18273 case DW_TAG_template_value_param:
18274 case DW_TAG_variable:
18275 return true;
18276 }
18277
18278 return false;
18279 }
18280
18281 void
18282 partial_die_info::fixup (struct dwarf2_cu *cu)
18283 {
18284 /* Once we've fixed up a die, there's no point in doing so again.
18285 This also avoids a memory leak if we were to call
18286 guess_partial_die_structure_name multiple times. */
18287 if (fixup_called)
18288 return;
18289
18290 /* If we found a reference attribute and the DIE has no name, try
18291 to find a name in the referred to DIE. */
18292
18293 if (name == NULL && has_specification)
18294 {
18295 struct partial_die_info *spec_die;
18296
18297 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18298 spec_die = res.pdi;
18299 cu = res.cu;
18300
18301 spec_die->fixup (cu);
18302
18303 if (spec_die->name)
18304 {
18305 name = spec_die->name;
18306
18307 /* Copy DW_AT_external attribute if it is set. */
18308 if (spec_die->is_external)
18309 is_external = spec_die->is_external;
18310 }
18311 }
18312
18313 if (!has_const_value && has_specification
18314 && can_have_DW_AT_const_value_p (tag))
18315 {
18316 struct partial_die_info *spec_die;
18317
18318 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18319 spec_die = res.pdi;
18320 cu = res.cu;
18321
18322 spec_die->fixup (cu);
18323
18324 if (spec_die->has_const_value)
18325 {
18326 /* Copy DW_AT_const_value attribute if it is set. */
18327 has_const_value = spec_die->has_const_value;
18328 }
18329 }
18330
18331 /* Set default names for some unnamed DIEs. */
18332
18333 if (name == NULL && tag == DW_TAG_namespace)
18334 name = CP_ANONYMOUS_NAMESPACE_STR;
18335
18336 /* If there is no parent die to provide a namespace, and there are
18337 children, see if we can determine the namespace from their linkage
18338 name. */
18339 if (cu->language == language_cplus
18340 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18341 && die_parent == NULL
18342 && has_children
18343 && (tag == DW_TAG_class_type
18344 || tag == DW_TAG_structure_type
18345 || tag == DW_TAG_union_type))
18346 guess_partial_die_structure_name (this, cu);
18347
18348 /* GCC might emit a nameless struct or union that has a linkage
18349 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18350 if (name == NULL
18351 && (tag == DW_TAG_class_type
18352 || tag == DW_TAG_interface_type
18353 || tag == DW_TAG_structure_type
18354 || tag == DW_TAG_union_type)
18355 && linkage_name != NULL)
18356 {
18357 gdb::unique_xmalloc_ptr<char> demangled
18358 (gdb_demangle (linkage_name, DMGL_TYPES));
18359 if (demangled != nullptr)
18360 {
18361 const char *base;
18362
18363 /* Strip any leading namespaces/classes, keep only the base name.
18364 DW_AT_name for named DIEs does not contain the prefixes. */
18365 base = strrchr (demangled.get (), ':');
18366 if (base && base > demangled.get () && base[-1] == ':')
18367 base++;
18368 else
18369 base = demangled.get ();
18370
18371 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18372 name = objfile->intern (base);
18373 }
18374 }
18375
18376 fixup_called = 1;
18377 }
18378
18379 /* Process the attributes that had to be skipped in the first round. These
18380 attributes are the ones that need str_offsets_base or addr_base attributes.
18381 They could not have been processed in the first round, because at the time
18382 the values of str_offsets_base or addr_base may not have been known. */
18383 static void
18384 read_attribute_reprocess (const struct die_reader_specs *reader,
18385 struct attribute *attr)
18386 {
18387 struct dwarf2_cu *cu = reader->cu;
18388 switch (attr->form)
18389 {
18390 case DW_FORM_addrx:
18391 case DW_FORM_GNU_addr_index:
18392 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18393 break;
18394 case DW_FORM_strx:
18395 case DW_FORM_strx1:
18396 case DW_FORM_strx2:
18397 case DW_FORM_strx3:
18398 case DW_FORM_strx4:
18399 case DW_FORM_GNU_str_index:
18400 {
18401 unsigned int str_index = DW_UNSND (attr);
18402 if (reader->dwo_file != NULL)
18403 {
18404 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18405 DW_STRING_IS_CANONICAL (attr) = 0;
18406 }
18407 else
18408 {
18409 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18410 DW_STRING_IS_CANONICAL (attr) = 0;
18411 }
18412 break;
18413 }
18414 default:
18415 gdb_assert_not_reached (_("Unexpected DWARF form."));
18416 }
18417 }
18418
18419 /* Read an attribute value described by an attribute form. */
18420
18421 static const gdb_byte *
18422 read_attribute_value (const struct die_reader_specs *reader,
18423 struct attribute *attr, unsigned form,
18424 LONGEST implicit_const, const gdb_byte *info_ptr,
18425 bool *need_reprocess)
18426 {
18427 struct dwarf2_cu *cu = reader->cu;
18428 struct dwarf2_per_objfile *dwarf2_per_objfile
18429 = cu->per_cu->dwarf2_per_objfile;
18430 struct objfile *objfile = dwarf2_per_objfile->objfile;
18431 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18432 bfd *abfd = reader->abfd;
18433 struct comp_unit_head *cu_header = &cu->header;
18434 unsigned int bytes_read;
18435 struct dwarf_block *blk;
18436 *need_reprocess = false;
18437
18438 attr->form = (enum dwarf_form) form;
18439 switch (form)
18440 {
18441 case DW_FORM_ref_addr:
18442 if (cu->header.version == 2)
18443 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18444 &bytes_read);
18445 else
18446 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18447 &bytes_read);
18448 info_ptr += bytes_read;
18449 break;
18450 case DW_FORM_GNU_ref_alt:
18451 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18452 info_ptr += bytes_read;
18453 break;
18454 case DW_FORM_addr:
18455 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18456 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18457 info_ptr += bytes_read;
18458 break;
18459 case DW_FORM_block2:
18460 blk = dwarf_alloc_block (cu);
18461 blk->size = read_2_bytes (abfd, info_ptr);
18462 info_ptr += 2;
18463 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18464 info_ptr += blk->size;
18465 DW_BLOCK (attr) = blk;
18466 break;
18467 case DW_FORM_block4:
18468 blk = dwarf_alloc_block (cu);
18469 blk->size = read_4_bytes (abfd, info_ptr);
18470 info_ptr += 4;
18471 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18472 info_ptr += blk->size;
18473 DW_BLOCK (attr) = blk;
18474 break;
18475 case DW_FORM_data2:
18476 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18477 info_ptr += 2;
18478 break;
18479 case DW_FORM_data4:
18480 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18481 info_ptr += 4;
18482 break;
18483 case DW_FORM_data8:
18484 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18485 info_ptr += 8;
18486 break;
18487 case DW_FORM_data16:
18488 blk = dwarf_alloc_block (cu);
18489 blk->size = 16;
18490 blk->data = read_n_bytes (abfd, info_ptr, 16);
18491 info_ptr += 16;
18492 DW_BLOCK (attr) = blk;
18493 break;
18494 case DW_FORM_sec_offset:
18495 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18496 info_ptr += bytes_read;
18497 break;
18498 case DW_FORM_string:
18499 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18500 DW_STRING_IS_CANONICAL (attr) = 0;
18501 info_ptr += bytes_read;
18502 break;
18503 case DW_FORM_strp:
18504 if (!cu->per_cu->is_dwz)
18505 {
18506 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18507 abfd, info_ptr, cu_header,
18508 &bytes_read);
18509 DW_STRING_IS_CANONICAL (attr) = 0;
18510 info_ptr += bytes_read;
18511 break;
18512 }
18513 /* FALLTHROUGH */
18514 case DW_FORM_line_strp:
18515 if (!cu->per_cu->is_dwz)
18516 {
18517 DW_STRING (attr)
18518 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18519 &bytes_read);
18520 DW_STRING_IS_CANONICAL (attr) = 0;
18521 info_ptr += bytes_read;
18522 break;
18523 }
18524 /* FALLTHROUGH */
18525 case DW_FORM_GNU_strp_alt:
18526 {
18527 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18528 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18529 &bytes_read);
18530
18531 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18532 DW_STRING_IS_CANONICAL (attr) = 0;
18533 info_ptr += bytes_read;
18534 }
18535 break;
18536 case DW_FORM_exprloc:
18537 case DW_FORM_block:
18538 blk = dwarf_alloc_block (cu);
18539 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18540 info_ptr += bytes_read;
18541 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18542 info_ptr += blk->size;
18543 DW_BLOCK (attr) = blk;
18544 break;
18545 case DW_FORM_block1:
18546 blk = dwarf_alloc_block (cu);
18547 blk->size = read_1_byte (abfd, info_ptr);
18548 info_ptr += 1;
18549 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18550 info_ptr += blk->size;
18551 DW_BLOCK (attr) = blk;
18552 break;
18553 case DW_FORM_data1:
18554 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18555 info_ptr += 1;
18556 break;
18557 case DW_FORM_flag:
18558 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18559 info_ptr += 1;
18560 break;
18561 case DW_FORM_flag_present:
18562 DW_UNSND (attr) = 1;
18563 break;
18564 case DW_FORM_sdata:
18565 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18566 info_ptr += bytes_read;
18567 break;
18568 case DW_FORM_udata:
18569 case DW_FORM_rnglistx:
18570 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18571 info_ptr += bytes_read;
18572 break;
18573 case DW_FORM_ref1:
18574 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18575 + read_1_byte (abfd, info_ptr));
18576 info_ptr += 1;
18577 break;
18578 case DW_FORM_ref2:
18579 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18580 + read_2_bytes (abfd, info_ptr));
18581 info_ptr += 2;
18582 break;
18583 case DW_FORM_ref4:
18584 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18585 + read_4_bytes (abfd, info_ptr));
18586 info_ptr += 4;
18587 break;
18588 case DW_FORM_ref8:
18589 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18590 + read_8_bytes (abfd, info_ptr));
18591 info_ptr += 8;
18592 break;
18593 case DW_FORM_ref_sig8:
18594 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18595 info_ptr += 8;
18596 break;
18597 case DW_FORM_ref_udata:
18598 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18599 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18600 info_ptr += bytes_read;
18601 break;
18602 case DW_FORM_indirect:
18603 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18604 info_ptr += bytes_read;
18605 if (form == DW_FORM_implicit_const)
18606 {
18607 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18608 info_ptr += bytes_read;
18609 }
18610 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18611 info_ptr, need_reprocess);
18612 break;
18613 case DW_FORM_implicit_const:
18614 DW_SND (attr) = implicit_const;
18615 break;
18616 case DW_FORM_addrx:
18617 case DW_FORM_GNU_addr_index:
18618 *need_reprocess = true;
18619 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18620 info_ptr += bytes_read;
18621 break;
18622 case DW_FORM_strx:
18623 case DW_FORM_strx1:
18624 case DW_FORM_strx2:
18625 case DW_FORM_strx3:
18626 case DW_FORM_strx4:
18627 case DW_FORM_GNU_str_index:
18628 {
18629 ULONGEST str_index;
18630 if (form == DW_FORM_strx1)
18631 {
18632 str_index = read_1_byte (abfd, info_ptr);
18633 info_ptr += 1;
18634 }
18635 else if (form == DW_FORM_strx2)
18636 {
18637 str_index = read_2_bytes (abfd, info_ptr);
18638 info_ptr += 2;
18639 }
18640 else if (form == DW_FORM_strx3)
18641 {
18642 str_index = read_3_bytes (abfd, info_ptr);
18643 info_ptr += 3;
18644 }
18645 else if (form == DW_FORM_strx4)
18646 {
18647 str_index = read_4_bytes (abfd, info_ptr);
18648 info_ptr += 4;
18649 }
18650 else
18651 {
18652 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18653 info_ptr += bytes_read;
18654 }
18655 *need_reprocess = true;
18656 DW_UNSND (attr) = str_index;
18657 }
18658 break;
18659 default:
18660 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18661 dwarf_form_name (form),
18662 bfd_get_filename (abfd));
18663 }
18664
18665 /* Super hack. */
18666 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18667 attr->form = DW_FORM_GNU_ref_alt;
18668
18669 /* We have seen instances where the compiler tried to emit a byte
18670 size attribute of -1 which ended up being encoded as an unsigned
18671 0xffffffff. Although 0xffffffff is technically a valid size value,
18672 an object of this size seems pretty unlikely so we can relatively
18673 safely treat these cases as if the size attribute was invalid and
18674 treat them as zero by default. */
18675 if (attr->name == DW_AT_byte_size
18676 && form == DW_FORM_data4
18677 && DW_UNSND (attr) >= 0xffffffff)
18678 {
18679 complaint
18680 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18681 hex_string (DW_UNSND (attr)));
18682 DW_UNSND (attr) = 0;
18683 }
18684
18685 return info_ptr;
18686 }
18687
18688 /* Read an attribute described by an abbreviated attribute. */
18689
18690 static const gdb_byte *
18691 read_attribute (const struct die_reader_specs *reader,
18692 struct attribute *attr, struct attr_abbrev *abbrev,
18693 const gdb_byte *info_ptr, bool *need_reprocess)
18694 {
18695 attr->name = abbrev->name;
18696 return read_attribute_value (reader, attr, abbrev->form,
18697 abbrev->implicit_const, info_ptr,
18698 need_reprocess);
18699 }
18700
18701 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18702
18703 static const char *
18704 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18705 LONGEST str_offset)
18706 {
18707 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18708 str_offset, "DW_FORM_strp");
18709 }
18710
18711 /* Return pointer to string at .debug_str offset as read from BUF.
18712 BUF is assumed to be in a compilation unit described by CU_HEADER.
18713 Return *BYTES_READ_PTR count of bytes read from BUF. */
18714
18715 static const char *
18716 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18717 const gdb_byte *buf,
18718 const struct comp_unit_head *cu_header,
18719 unsigned int *bytes_read_ptr)
18720 {
18721 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18722
18723 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
18724 }
18725
18726 /* See read.h. */
18727
18728 const char *
18729 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
18730 const struct comp_unit_head *cu_header,
18731 unsigned int *bytes_read_ptr)
18732 {
18733 bfd *abfd = objfile->obfd;
18734 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18735
18736 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
18737 }
18738
18739 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18740 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18741 ADDR_SIZE is the size of addresses from the CU header. */
18742
18743 static CORE_ADDR
18744 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18745 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18746 int addr_size)
18747 {
18748 struct objfile *objfile = dwarf2_per_objfile->objfile;
18749 bfd *abfd = objfile->obfd;
18750 const gdb_byte *info_ptr;
18751 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18752
18753 dwarf2_per_objfile->addr.read (objfile);
18754 if (dwarf2_per_objfile->addr.buffer == NULL)
18755 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18756 objfile_name (objfile));
18757 if (addr_base_or_zero + addr_index * addr_size
18758 >= dwarf2_per_objfile->addr.size)
18759 error (_("DW_FORM_addr_index pointing outside of "
18760 ".debug_addr section [in module %s]"),
18761 objfile_name (objfile));
18762 info_ptr = (dwarf2_per_objfile->addr.buffer
18763 + addr_base_or_zero + addr_index * addr_size);
18764 if (addr_size == 4)
18765 return bfd_get_32 (abfd, info_ptr);
18766 else
18767 return bfd_get_64 (abfd, info_ptr);
18768 }
18769
18770 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18771
18772 static CORE_ADDR
18773 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18774 {
18775 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18776 cu->addr_base, cu->header.addr_size);
18777 }
18778
18779 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18780
18781 static CORE_ADDR
18782 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18783 unsigned int *bytes_read)
18784 {
18785 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18786 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18787
18788 return read_addr_index (cu, addr_index);
18789 }
18790
18791 /* See read.h. */
18792
18793 CORE_ADDR
18794 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18795 {
18796 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18797 struct dwarf2_cu *cu = per_cu->cu;
18798 gdb::optional<ULONGEST> addr_base;
18799 int addr_size;
18800
18801 /* We need addr_base and addr_size.
18802 If we don't have PER_CU->cu, we have to get it.
18803 Nasty, but the alternative is storing the needed info in PER_CU,
18804 which at this point doesn't seem justified: it's not clear how frequently
18805 it would get used and it would increase the size of every PER_CU.
18806 Entry points like dwarf2_per_cu_addr_size do a similar thing
18807 so we're not in uncharted territory here.
18808 Alas we need to be a bit more complicated as addr_base is contained
18809 in the DIE.
18810
18811 We don't need to read the entire CU(/TU).
18812 We just need the header and top level die.
18813
18814 IWBN to use the aging mechanism to let us lazily later discard the CU.
18815 For now we skip this optimization. */
18816
18817 if (cu != NULL)
18818 {
18819 addr_base = cu->addr_base;
18820 addr_size = cu->header.addr_size;
18821 }
18822 else
18823 {
18824 cutu_reader reader (per_cu, NULL, 0, false);
18825 addr_base = reader.cu->addr_base;
18826 addr_size = reader.cu->header.addr_size;
18827 }
18828
18829 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18830 addr_size);
18831 }
18832
18833 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18834 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18835 DWO file. */
18836
18837 static const char *
18838 read_str_index (struct dwarf2_cu *cu,
18839 struct dwarf2_section_info *str_section,
18840 struct dwarf2_section_info *str_offsets_section,
18841 ULONGEST str_offsets_base, ULONGEST str_index)
18842 {
18843 struct dwarf2_per_objfile *dwarf2_per_objfile
18844 = cu->per_cu->dwarf2_per_objfile;
18845 struct objfile *objfile = dwarf2_per_objfile->objfile;
18846 const char *objf_name = objfile_name (objfile);
18847 bfd *abfd = objfile->obfd;
18848 const gdb_byte *info_ptr;
18849 ULONGEST str_offset;
18850 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18851
18852 str_section->read (objfile);
18853 str_offsets_section->read (objfile);
18854 if (str_section->buffer == NULL)
18855 error (_("%s used without %s section"
18856 " in CU at offset %s [in module %s]"),
18857 form_name, str_section->get_name (),
18858 sect_offset_str (cu->header.sect_off), objf_name);
18859 if (str_offsets_section->buffer == NULL)
18860 error (_("%s used without %s section"
18861 " in CU at offset %s [in module %s]"),
18862 form_name, str_section->get_name (),
18863 sect_offset_str (cu->header.sect_off), objf_name);
18864 info_ptr = (str_offsets_section->buffer
18865 + str_offsets_base
18866 + str_index * cu->header.offset_size);
18867 if (cu->header.offset_size == 4)
18868 str_offset = bfd_get_32 (abfd, info_ptr);
18869 else
18870 str_offset = bfd_get_64 (abfd, info_ptr);
18871 if (str_offset >= str_section->size)
18872 error (_("Offset from %s pointing outside of"
18873 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18874 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18875 return (const char *) (str_section->buffer + str_offset);
18876 }
18877
18878 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18879
18880 static const char *
18881 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18882 {
18883 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18884 ? reader->cu->header.addr_size : 0;
18885 return read_str_index (reader->cu,
18886 &reader->dwo_file->sections.str,
18887 &reader->dwo_file->sections.str_offsets,
18888 str_offsets_base, str_index);
18889 }
18890
18891 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18892
18893 static const char *
18894 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18895 {
18896 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18897 const char *objf_name = objfile_name (objfile);
18898 static const char form_name[] = "DW_FORM_GNU_str_index";
18899 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18900
18901 if (!cu->str_offsets_base.has_value ())
18902 error (_("%s used in Fission stub without %s"
18903 " in CU at offset 0x%lx [in module %s]"),
18904 form_name, str_offsets_attr_name,
18905 (long) cu->header.offset_size, objf_name);
18906
18907 return read_str_index (cu,
18908 &cu->per_cu->dwarf2_per_objfile->str,
18909 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18910 *cu->str_offsets_base, str_index);
18911 }
18912
18913 /* Return the length of an LEB128 number in BUF. */
18914
18915 static int
18916 leb128_size (const gdb_byte *buf)
18917 {
18918 const gdb_byte *begin = buf;
18919 gdb_byte byte;
18920
18921 while (1)
18922 {
18923 byte = *buf++;
18924 if ((byte & 128) == 0)
18925 return buf - begin;
18926 }
18927 }
18928
18929 static void
18930 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18931 {
18932 switch (lang)
18933 {
18934 case DW_LANG_C89:
18935 case DW_LANG_C99:
18936 case DW_LANG_C11:
18937 case DW_LANG_C:
18938 case DW_LANG_UPC:
18939 cu->language = language_c;
18940 break;
18941 case DW_LANG_Java:
18942 case DW_LANG_C_plus_plus:
18943 case DW_LANG_C_plus_plus_11:
18944 case DW_LANG_C_plus_plus_14:
18945 cu->language = language_cplus;
18946 break;
18947 case DW_LANG_D:
18948 cu->language = language_d;
18949 break;
18950 case DW_LANG_Fortran77:
18951 case DW_LANG_Fortran90:
18952 case DW_LANG_Fortran95:
18953 case DW_LANG_Fortran03:
18954 case DW_LANG_Fortran08:
18955 cu->language = language_fortran;
18956 break;
18957 case DW_LANG_Go:
18958 cu->language = language_go;
18959 break;
18960 case DW_LANG_Mips_Assembler:
18961 cu->language = language_asm;
18962 break;
18963 case DW_LANG_Ada83:
18964 case DW_LANG_Ada95:
18965 cu->language = language_ada;
18966 break;
18967 case DW_LANG_Modula2:
18968 cu->language = language_m2;
18969 break;
18970 case DW_LANG_Pascal83:
18971 cu->language = language_pascal;
18972 break;
18973 case DW_LANG_ObjC:
18974 cu->language = language_objc;
18975 break;
18976 case DW_LANG_Rust:
18977 case DW_LANG_Rust_old:
18978 cu->language = language_rust;
18979 break;
18980 case DW_LANG_Cobol74:
18981 case DW_LANG_Cobol85:
18982 default:
18983 cu->language = language_minimal;
18984 break;
18985 }
18986 cu->language_defn = language_def (cu->language);
18987 }
18988
18989 /* Return the named attribute or NULL if not there. */
18990
18991 static struct attribute *
18992 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18993 {
18994 for (;;)
18995 {
18996 unsigned int i;
18997 struct attribute *spec = NULL;
18998
18999 for (i = 0; i < die->num_attrs; ++i)
19000 {
19001 if (die->attrs[i].name == name)
19002 return &die->attrs[i];
19003 if (die->attrs[i].name == DW_AT_specification
19004 || die->attrs[i].name == DW_AT_abstract_origin)
19005 spec = &die->attrs[i];
19006 }
19007
19008 if (!spec)
19009 break;
19010
19011 die = follow_die_ref (die, spec, &cu);
19012 }
19013
19014 return NULL;
19015 }
19016
19017 /* Return the string associated with a string-typed attribute, or NULL if it
19018 is either not found or is of an incorrect type. */
19019
19020 static const char *
19021 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19022 {
19023 struct attribute *attr;
19024 const char *str = NULL;
19025
19026 attr = dwarf2_attr (die, name, cu);
19027
19028 if (attr != NULL)
19029 {
19030 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
19031 || attr->form == DW_FORM_string
19032 || attr->form == DW_FORM_strx
19033 || attr->form == DW_FORM_strx1
19034 || attr->form == DW_FORM_strx2
19035 || attr->form == DW_FORM_strx3
19036 || attr->form == DW_FORM_strx4
19037 || attr->form == DW_FORM_GNU_str_index
19038 || attr->form == DW_FORM_GNU_strp_alt)
19039 str = DW_STRING (attr);
19040 else
19041 complaint (_("string type expected for attribute %s for "
19042 "DIE at %s in module %s"),
19043 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19044 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19045 }
19046
19047 return str;
19048 }
19049
19050 /* Return the dwo name or NULL if not present. If present, it is in either
19051 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19052 static const char *
19053 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19054 {
19055 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19056 if (dwo_name == nullptr)
19057 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19058 return dwo_name;
19059 }
19060
19061 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19062 and holds a non-zero value. This function should only be used for
19063 DW_FORM_flag or DW_FORM_flag_present attributes. */
19064
19065 static int
19066 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19067 {
19068 struct attribute *attr = dwarf2_attr (die, name, cu);
19069
19070 return (attr && DW_UNSND (attr));
19071 }
19072
19073 static int
19074 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19075 {
19076 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19077 which value is non-zero. However, we have to be careful with
19078 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19079 (via dwarf2_flag_true_p) follows this attribute. So we may
19080 end up accidently finding a declaration attribute that belongs
19081 to a different DIE referenced by the specification attribute,
19082 even though the given DIE does not have a declaration attribute. */
19083 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19084 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19085 }
19086
19087 /* Return the die giving the specification for DIE, if there is
19088 one. *SPEC_CU is the CU containing DIE on input, and the CU
19089 containing the return value on output. If there is no
19090 specification, but there is an abstract origin, that is
19091 returned. */
19092
19093 static struct die_info *
19094 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19095 {
19096 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19097 *spec_cu);
19098
19099 if (spec_attr == NULL)
19100 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19101
19102 if (spec_attr == NULL)
19103 return NULL;
19104 else
19105 return follow_die_ref (die, spec_attr, spec_cu);
19106 }
19107
19108 /* Stub for free_line_header to match void * callback types. */
19109
19110 static void
19111 free_line_header_voidp (void *arg)
19112 {
19113 struct line_header *lh = (struct line_header *) arg;
19114
19115 delete lh;
19116 }
19117
19118 /* A convenience function to find the proper .debug_line section for a CU. */
19119
19120 static struct dwarf2_section_info *
19121 get_debug_line_section (struct dwarf2_cu *cu)
19122 {
19123 struct dwarf2_section_info *section;
19124 struct dwarf2_per_objfile *dwarf2_per_objfile
19125 = cu->per_cu->dwarf2_per_objfile;
19126
19127 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19128 DWO file. */
19129 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19130 section = &cu->dwo_unit->dwo_file->sections.line;
19131 else if (cu->per_cu->is_dwz)
19132 {
19133 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19134
19135 section = &dwz->line;
19136 }
19137 else
19138 section = &dwarf2_per_objfile->line;
19139
19140 return section;
19141 }
19142
19143 /* Read the statement program header starting at OFFSET in
19144 .debug_line, or .debug_line.dwo. Return a pointer
19145 to a struct line_header, allocated using xmalloc.
19146 Returns NULL if there is a problem reading the header, e.g., if it
19147 has a version we don't understand.
19148
19149 NOTE: the strings in the include directory and file name tables of
19150 the returned object point into the dwarf line section buffer,
19151 and must not be freed. */
19152
19153 static line_header_up
19154 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19155 {
19156 struct dwarf2_section_info *section;
19157 struct dwarf2_per_objfile *dwarf2_per_objfile
19158 = cu->per_cu->dwarf2_per_objfile;
19159
19160 section = get_debug_line_section (cu);
19161 section->read (dwarf2_per_objfile->objfile);
19162 if (section->buffer == NULL)
19163 {
19164 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19165 complaint (_("missing .debug_line.dwo section"));
19166 else
19167 complaint (_("missing .debug_line section"));
19168 return 0;
19169 }
19170
19171 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19172 dwarf2_per_objfile, section,
19173 &cu->header);
19174 }
19175
19176 /* Subroutine of dwarf_decode_lines to simplify it.
19177 Return the file name of the psymtab for the given file_entry.
19178 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19179 If space for the result is malloc'd, *NAME_HOLDER will be set.
19180 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19181
19182 static const char *
19183 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19184 const dwarf2_psymtab *pst,
19185 const char *comp_dir,
19186 gdb::unique_xmalloc_ptr<char> *name_holder)
19187 {
19188 const char *include_name = fe.name;
19189 const char *include_name_to_compare = include_name;
19190 const char *pst_filename;
19191 int file_is_pst;
19192
19193 const char *dir_name = fe.include_dir (lh);
19194
19195 gdb::unique_xmalloc_ptr<char> hold_compare;
19196 if (!IS_ABSOLUTE_PATH (include_name)
19197 && (dir_name != NULL || comp_dir != NULL))
19198 {
19199 /* Avoid creating a duplicate psymtab for PST.
19200 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19201 Before we do the comparison, however, we need to account
19202 for DIR_NAME and COMP_DIR.
19203 First prepend dir_name (if non-NULL). If we still don't
19204 have an absolute path prepend comp_dir (if non-NULL).
19205 However, the directory we record in the include-file's
19206 psymtab does not contain COMP_DIR (to match the
19207 corresponding symtab(s)).
19208
19209 Example:
19210
19211 bash$ cd /tmp
19212 bash$ gcc -g ./hello.c
19213 include_name = "hello.c"
19214 dir_name = "."
19215 DW_AT_comp_dir = comp_dir = "/tmp"
19216 DW_AT_name = "./hello.c"
19217
19218 */
19219
19220 if (dir_name != NULL)
19221 {
19222 name_holder->reset (concat (dir_name, SLASH_STRING,
19223 include_name, (char *) NULL));
19224 include_name = name_holder->get ();
19225 include_name_to_compare = include_name;
19226 }
19227 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19228 {
19229 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19230 include_name, (char *) NULL));
19231 include_name_to_compare = hold_compare.get ();
19232 }
19233 }
19234
19235 pst_filename = pst->filename;
19236 gdb::unique_xmalloc_ptr<char> copied_name;
19237 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19238 {
19239 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19240 pst_filename, (char *) NULL));
19241 pst_filename = copied_name.get ();
19242 }
19243
19244 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19245
19246 if (file_is_pst)
19247 return NULL;
19248 return include_name;
19249 }
19250
19251 /* State machine to track the state of the line number program. */
19252
19253 class lnp_state_machine
19254 {
19255 public:
19256 /* Initialize a machine state for the start of a line number
19257 program. */
19258 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19259 bool record_lines_p);
19260
19261 file_entry *current_file ()
19262 {
19263 /* lh->file_names is 0-based, but the file name numbers in the
19264 statement program are 1-based. */
19265 return m_line_header->file_name_at (m_file);
19266 }
19267
19268 /* Record the line in the state machine. END_SEQUENCE is true if
19269 we're processing the end of a sequence. */
19270 void record_line (bool end_sequence);
19271
19272 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19273 nop-out rest of the lines in this sequence. */
19274 void check_line_address (struct dwarf2_cu *cu,
19275 const gdb_byte *line_ptr,
19276 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19277
19278 void handle_set_discriminator (unsigned int discriminator)
19279 {
19280 m_discriminator = discriminator;
19281 m_line_has_non_zero_discriminator |= discriminator != 0;
19282 }
19283
19284 /* Handle DW_LNE_set_address. */
19285 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19286 {
19287 m_op_index = 0;
19288 address += baseaddr;
19289 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19290 }
19291
19292 /* Handle DW_LNS_advance_pc. */
19293 void handle_advance_pc (CORE_ADDR adjust);
19294
19295 /* Handle a special opcode. */
19296 void handle_special_opcode (unsigned char op_code);
19297
19298 /* Handle DW_LNS_advance_line. */
19299 void handle_advance_line (int line_delta)
19300 {
19301 advance_line (line_delta);
19302 }
19303
19304 /* Handle DW_LNS_set_file. */
19305 void handle_set_file (file_name_index file);
19306
19307 /* Handle DW_LNS_negate_stmt. */
19308 void handle_negate_stmt ()
19309 {
19310 m_is_stmt = !m_is_stmt;
19311 }
19312
19313 /* Handle DW_LNS_const_add_pc. */
19314 void handle_const_add_pc ();
19315
19316 /* Handle DW_LNS_fixed_advance_pc. */
19317 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19318 {
19319 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19320 m_op_index = 0;
19321 }
19322
19323 /* Handle DW_LNS_copy. */
19324 void handle_copy ()
19325 {
19326 record_line (false);
19327 m_discriminator = 0;
19328 }
19329
19330 /* Handle DW_LNE_end_sequence. */
19331 void handle_end_sequence ()
19332 {
19333 m_currently_recording_lines = true;
19334 }
19335
19336 private:
19337 /* Advance the line by LINE_DELTA. */
19338 void advance_line (int line_delta)
19339 {
19340 m_line += line_delta;
19341
19342 if (line_delta != 0)
19343 m_line_has_non_zero_discriminator = m_discriminator != 0;
19344 }
19345
19346 struct dwarf2_cu *m_cu;
19347
19348 gdbarch *m_gdbarch;
19349
19350 /* True if we're recording lines.
19351 Otherwise we're building partial symtabs and are just interested in
19352 finding include files mentioned by the line number program. */
19353 bool m_record_lines_p;
19354
19355 /* The line number header. */
19356 line_header *m_line_header;
19357
19358 /* These are part of the standard DWARF line number state machine,
19359 and initialized according to the DWARF spec. */
19360
19361 unsigned char m_op_index = 0;
19362 /* The line table index of the current file. */
19363 file_name_index m_file = 1;
19364 unsigned int m_line = 1;
19365
19366 /* These are initialized in the constructor. */
19367
19368 CORE_ADDR m_address;
19369 bool m_is_stmt;
19370 unsigned int m_discriminator;
19371
19372 /* Additional bits of state we need to track. */
19373
19374 /* The last file that we called dwarf2_start_subfile for.
19375 This is only used for TLLs. */
19376 unsigned int m_last_file = 0;
19377 /* The last file a line number was recorded for. */
19378 struct subfile *m_last_subfile = NULL;
19379
19380 /* When true, record the lines we decode. */
19381 bool m_currently_recording_lines = false;
19382
19383 /* The last line number that was recorded, used to coalesce
19384 consecutive entries for the same line. This can happen, for
19385 example, when discriminators are present. PR 17276. */
19386 unsigned int m_last_line = 0;
19387 bool m_line_has_non_zero_discriminator = false;
19388 };
19389
19390 void
19391 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19392 {
19393 CORE_ADDR addr_adj = (((m_op_index + adjust)
19394 / m_line_header->maximum_ops_per_instruction)
19395 * m_line_header->minimum_instruction_length);
19396 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19397 m_op_index = ((m_op_index + adjust)
19398 % m_line_header->maximum_ops_per_instruction);
19399 }
19400
19401 void
19402 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19403 {
19404 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19405 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19406 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19407 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19408 / m_line_header->maximum_ops_per_instruction)
19409 * m_line_header->minimum_instruction_length);
19410 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19411 m_op_index = ((m_op_index + adj_opcode_d)
19412 % m_line_header->maximum_ops_per_instruction);
19413
19414 int line_delta = m_line_header->line_base + adj_opcode_r;
19415 advance_line (line_delta);
19416 record_line (false);
19417 m_discriminator = 0;
19418 }
19419
19420 void
19421 lnp_state_machine::handle_set_file (file_name_index file)
19422 {
19423 m_file = file;
19424
19425 const file_entry *fe = current_file ();
19426 if (fe == NULL)
19427 dwarf2_debug_line_missing_file_complaint ();
19428 else if (m_record_lines_p)
19429 {
19430 const char *dir = fe->include_dir (m_line_header);
19431
19432 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19433 m_line_has_non_zero_discriminator = m_discriminator != 0;
19434 dwarf2_start_subfile (m_cu, fe->name, dir);
19435 }
19436 }
19437
19438 void
19439 lnp_state_machine::handle_const_add_pc ()
19440 {
19441 CORE_ADDR adjust
19442 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19443
19444 CORE_ADDR addr_adj
19445 = (((m_op_index + adjust)
19446 / m_line_header->maximum_ops_per_instruction)
19447 * m_line_header->minimum_instruction_length);
19448
19449 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19450 m_op_index = ((m_op_index + adjust)
19451 % m_line_header->maximum_ops_per_instruction);
19452 }
19453
19454 /* Return non-zero if we should add LINE to the line number table.
19455 LINE is the line to add, LAST_LINE is the last line that was added,
19456 LAST_SUBFILE is the subfile for LAST_LINE.
19457 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19458 had a non-zero discriminator.
19459
19460 We have to be careful in the presence of discriminators.
19461 E.g., for this line:
19462
19463 for (i = 0; i < 100000; i++);
19464
19465 clang can emit four line number entries for that one line,
19466 each with a different discriminator.
19467 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19468
19469 However, we want gdb to coalesce all four entries into one.
19470 Otherwise the user could stepi into the middle of the line and
19471 gdb would get confused about whether the pc really was in the
19472 middle of the line.
19473
19474 Things are further complicated by the fact that two consecutive
19475 line number entries for the same line is a heuristic used by gcc
19476 to denote the end of the prologue. So we can't just discard duplicate
19477 entries, we have to be selective about it. The heuristic we use is
19478 that we only collapse consecutive entries for the same line if at least
19479 one of those entries has a non-zero discriminator. PR 17276.
19480
19481 Note: Addresses in the line number state machine can never go backwards
19482 within one sequence, thus this coalescing is ok. */
19483
19484 static int
19485 dwarf_record_line_p (struct dwarf2_cu *cu,
19486 unsigned int line, unsigned int last_line,
19487 int line_has_non_zero_discriminator,
19488 struct subfile *last_subfile)
19489 {
19490 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19491 return 1;
19492 if (line != last_line)
19493 return 1;
19494 /* Same line for the same file that we've seen already.
19495 As a last check, for pr 17276, only record the line if the line
19496 has never had a non-zero discriminator. */
19497 if (!line_has_non_zero_discriminator)
19498 return 1;
19499 return 0;
19500 }
19501
19502 /* Use the CU's builder to record line number LINE beginning at
19503 address ADDRESS in the line table of subfile SUBFILE. */
19504
19505 static void
19506 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19507 unsigned int line, CORE_ADDR address, bool is_stmt,
19508 struct dwarf2_cu *cu)
19509 {
19510 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19511
19512 if (dwarf_line_debug)
19513 {
19514 fprintf_unfiltered (gdb_stdlog,
19515 "Recording line %u, file %s, address %s\n",
19516 line, lbasename (subfile->name),
19517 paddress (gdbarch, address));
19518 }
19519
19520 if (cu != nullptr)
19521 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19522 }
19523
19524 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19525 Mark the end of a set of line number records.
19526 The arguments are the same as for dwarf_record_line_1.
19527 If SUBFILE is NULL the request is ignored. */
19528
19529 static void
19530 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19531 CORE_ADDR address, struct dwarf2_cu *cu)
19532 {
19533 if (subfile == NULL)
19534 return;
19535
19536 if (dwarf_line_debug)
19537 {
19538 fprintf_unfiltered (gdb_stdlog,
19539 "Finishing current line, file %s, address %s\n",
19540 lbasename (subfile->name),
19541 paddress (gdbarch, address));
19542 }
19543
19544 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19545 }
19546
19547 void
19548 lnp_state_machine::record_line (bool end_sequence)
19549 {
19550 if (dwarf_line_debug)
19551 {
19552 fprintf_unfiltered (gdb_stdlog,
19553 "Processing actual line %u: file %u,"
19554 " address %s, is_stmt %u, discrim %u%s\n",
19555 m_line, m_file,
19556 paddress (m_gdbarch, m_address),
19557 m_is_stmt, m_discriminator,
19558 (end_sequence ? "\t(end sequence)" : ""));
19559 }
19560
19561 file_entry *fe = current_file ();
19562
19563 if (fe == NULL)
19564 dwarf2_debug_line_missing_file_complaint ();
19565 /* For now we ignore lines not starting on an instruction boundary.
19566 But not when processing end_sequence for compatibility with the
19567 previous version of the code. */
19568 else if (m_op_index == 0 || end_sequence)
19569 {
19570 fe->included_p = 1;
19571 if (m_record_lines_p)
19572 {
19573 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19574 || end_sequence)
19575 {
19576 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19577 m_currently_recording_lines ? m_cu : nullptr);
19578 }
19579
19580 if (!end_sequence)
19581 {
19582 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19583
19584 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19585 m_line_has_non_zero_discriminator,
19586 m_last_subfile))
19587 {
19588 buildsym_compunit *builder = m_cu->get_builder ();
19589 dwarf_record_line_1 (m_gdbarch,
19590 builder->get_current_subfile (),
19591 m_line, m_address, is_stmt,
19592 m_currently_recording_lines ? m_cu : nullptr);
19593 }
19594 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19595 m_last_line = m_line;
19596 }
19597 }
19598 }
19599 }
19600
19601 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19602 line_header *lh, bool record_lines_p)
19603 {
19604 m_cu = cu;
19605 m_gdbarch = arch;
19606 m_record_lines_p = record_lines_p;
19607 m_line_header = lh;
19608
19609 m_currently_recording_lines = true;
19610
19611 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19612 was a line entry for it so that the backend has a chance to adjust it
19613 and also record it in case it needs it. This is currently used by MIPS
19614 code, cf. `mips_adjust_dwarf2_line'. */
19615 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19616 m_is_stmt = lh->default_is_stmt;
19617 m_discriminator = 0;
19618 }
19619
19620 void
19621 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19622 const gdb_byte *line_ptr,
19623 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
19624 {
19625 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19626 the pc range of the CU. However, we restrict the test to only ADDRESS
19627 values of zero to preserve GDB's previous behaviour which is to handle
19628 the specific case of a function being GC'd by the linker. */
19629
19630 if (address == 0 && address < unrelocated_lowpc)
19631 {
19632 /* This line table is for a function which has been
19633 GCd by the linker. Ignore it. PR gdb/12528 */
19634
19635 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19636 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19637
19638 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19639 line_offset, objfile_name (objfile));
19640 m_currently_recording_lines = false;
19641 /* Note: m_currently_recording_lines is left as false until we see
19642 DW_LNE_end_sequence. */
19643 }
19644 }
19645
19646 /* Subroutine of dwarf_decode_lines to simplify it.
19647 Process the line number information in LH.
19648 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19649 program in order to set included_p for every referenced header. */
19650
19651 static void
19652 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19653 const int decode_for_pst_p, CORE_ADDR lowpc)
19654 {
19655 const gdb_byte *line_ptr, *extended_end;
19656 const gdb_byte *line_end;
19657 unsigned int bytes_read, extended_len;
19658 unsigned char op_code, extended_op;
19659 CORE_ADDR baseaddr;
19660 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19661 bfd *abfd = objfile->obfd;
19662 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19663 /* True if we're recording line info (as opposed to building partial
19664 symtabs and just interested in finding include files mentioned by
19665 the line number program). */
19666 bool record_lines_p = !decode_for_pst_p;
19667
19668 baseaddr = objfile->text_section_offset ();
19669
19670 line_ptr = lh->statement_program_start;
19671 line_end = lh->statement_program_end;
19672
19673 /* Read the statement sequences until there's nothing left. */
19674 while (line_ptr < line_end)
19675 {
19676 /* The DWARF line number program state machine. Reset the state
19677 machine at the start of each sequence. */
19678 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
19679 bool end_sequence = false;
19680
19681 if (record_lines_p)
19682 {
19683 /* Start a subfile for the current file of the state
19684 machine. */
19685 const file_entry *fe = state_machine.current_file ();
19686
19687 if (fe != NULL)
19688 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
19689 }
19690
19691 /* Decode the table. */
19692 while (line_ptr < line_end && !end_sequence)
19693 {
19694 op_code = read_1_byte (abfd, line_ptr);
19695 line_ptr += 1;
19696
19697 if (op_code >= lh->opcode_base)
19698 {
19699 /* Special opcode. */
19700 state_machine.handle_special_opcode (op_code);
19701 }
19702 else switch (op_code)
19703 {
19704 case DW_LNS_extended_op:
19705 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19706 &bytes_read);
19707 line_ptr += bytes_read;
19708 extended_end = line_ptr + extended_len;
19709 extended_op = read_1_byte (abfd, line_ptr);
19710 line_ptr += 1;
19711 switch (extended_op)
19712 {
19713 case DW_LNE_end_sequence:
19714 state_machine.handle_end_sequence ();
19715 end_sequence = true;
19716 break;
19717 case DW_LNE_set_address:
19718 {
19719 CORE_ADDR address
19720 = cu->header.read_address (abfd, line_ptr, &bytes_read);
19721 line_ptr += bytes_read;
19722
19723 state_machine.check_line_address (cu, line_ptr,
19724 lowpc - baseaddr, address);
19725 state_machine.handle_set_address (baseaddr, address);
19726 }
19727 break;
19728 case DW_LNE_define_file:
19729 {
19730 const char *cur_file;
19731 unsigned int mod_time, length;
19732 dir_index dindex;
19733
19734 cur_file = read_direct_string (abfd, line_ptr,
19735 &bytes_read);
19736 line_ptr += bytes_read;
19737 dindex = (dir_index)
19738 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19739 line_ptr += bytes_read;
19740 mod_time =
19741 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19742 line_ptr += bytes_read;
19743 length =
19744 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19745 line_ptr += bytes_read;
19746 lh->add_file_name (cur_file, dindex, mod_time, length);
19747 }
19748 break;
19749 case DW_LNE_set_discriminator:
19750 {
19751 /* The discriminator is not interesting to the
19752 debugger; just ignore it. We still need to
19753 check its value though:
19754 if there are consecutive entries for the same
19755 (non-prologue) line we want to coalesce them.
19756 PR 17276. */
19757 unsigned int discr
19758 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19759 line_ptr += bytes_read;
19760
19761 state_machine.handle_set_discriminator (discr);
19762 }
19763 break;
19764 default:
19765 complaint (_("mangled .debug_line section"));
19766 return;
19767 }
19768 /* Make sure that we parsed the extended op correctly. If e.g.
19769 we expected a different address size than the producer used,
19770 we may have read the wrong number of bytes. */
19771 if (line_ptr != extended_end)
19772 {
19773 complaint (_("mangled .debug_line section"));
19774 return;
19775 }
19776 break;
19777 case DW_LNS_copy:
19778 state_machine.handle_copy ();
19779 break;
19780 case DW_LNS_advance_pc:
19781 {
19782 CORE_ADDR adjust
19783 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19784 line_ptr += bytes_read;
19785
19786 state_machine.handle_advance_pc (adjust);
19787 }
19788 break;
19789 case DW_LNS_advance_line:
19790 {
19791 int line_delta
19792 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19793 line_ptr += bytes_read;
19794
19795 state_machine.handle_advance_line (line_delta);
19796 }
19797 break;
19798 case DW_LNS_set_file:
19799 {
19800 file_name_index file
19801 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19802 &bytes_read);
19803 line_ptr += bytes_read;
19804
19805 state_machine.handle_set_file (file);
19806 }
19807 break;
19808 case DW_LNS_set_column:
19809 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19810 line_ptr += bytes_read;
19811 break;
19812 case DW_LNS_negate_stmt:
19813 state_machine.handle_negate_stmt ();
19814 break;
19815 case DW_LNS_set_basic_block:
19816 break;
19817 /* Add to the address register of the state machine the
19818 address increment value corresponding to special opcode
19819 255. I.e., this value is scaled by the minimum
19820 instruction length since special opcode 255 would have
19821 scaled the increment. */
19822 case DW_LNS_const_add_pc:
19823 state_machine.handle_const_add_pc ();
19824 break;
19825 case DW_LNS_fixed_advance_pc:
19826 {
19827 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19828 line_ptr += 2;
19829
19830 state_machine.handle_fixed_advance_pc (addr_adj);
19831 }
19832 break;
19833 default:
19834 {
19835 /* Unknown standard opcode, ignore it. */
19836 int i;
19837
19838 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19839 {
19840 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19841 line_ptr += bytes_read;
19842 }
19843 }
19844 }
19845 }
19846
19847 if (!end_sequence)
19848 dwarf2_debug_line_missing_end_sequence_complaint ();
19849
19850 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19851 in which case we still finish recording the last line). */
19852 state_machine.record_line (true);
19853 }
19854 }
19855
19856 /* Decode the Line Number Program (LNP) for the given line_header
19857 structure and CU. The actual information extracted and the type
19858 of structures created from the LNP depends on the value of PST.
19859
19860 1. If PST is NULL, then this procedure uses the data from the program
19861 to create all necessary symbol tables, and their linetables.
19862
19863 2. If PST is not NULL, this procedure reads the program to determine
19864 the list of files included by the unit represented by PST, and
19865 builds all the associated partial symbol tables.
19866
19867 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19868 It is used for relative paths in the line table.
19869 NOTE: When processing partial symtabs (pst != NULL),
19870 comp_dir == pst->dirname.
19871
19872 NOTE: It is important that psymtabs have the same file name (via strcmp)
19873 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19874 symtab we don't use it in the name of the psymtabs we create.
19875 E.g. expand_line_sal requires this when finding psymtabs to expand.
19876 A good testcase for this is mb-inline.exp.
19877
19878 LOWPC is the lowest address in CU (or 0 if not known).
19879
19880 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19881 for its PC<->lines mapping information. Otherwise only the filename
19882 table is read in. */
19883
19884 static void
19885 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
19886 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
19887 CORE_ADDR lowpc, int decode_mapping)
19888 {
19889 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19890 const int decode_for_pst_p = (pst != NULL);
19891
19892 if (decode_mapping)
19893 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
19894
19895 if (decode_for_pst_p)
19896 {
19897 /* Now that we're done scanning the Line Header Program, we can
19898 create the psymtab of each included file. */
19899 for (auto &file_entry : lh->file_names ())
19900 if (file_entry.included_p == 1)
19901 {
19902 gdb::unique_xmalloc_ptr<char> name_holder;
19903 const char *include_name =
19904 psymtab_include_file_name (lh, file_entry, pst,
19905 comp_dir, &name_holder);
19906 if (include_name != NULL)
19907 dwarf2_create_include_psymtab (include_name, pst, objfile);
19908 }
19909 }
19910 else
19911 {
19912 /* Make sure a symtab is created for every file, even files
19913 which contain only variables (i.e. no code with associated
19914 line numbers). */
19915 buildsym_compunit *builder = cu->get_builder ();
19916 struct compunit_symtab *cust = builder->get_compunit_symtab ();
19917
19918 for (auto &fe : lh->file_names ())
19919 {
19920 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
19921 if (builder->get_current_subfile ()->symtab == NULL)
19922 {
19923 builder->get_current_subfile ()->symtab
19924 = allocate_symtab (cust,
19925 builder->get_current_subfile ()->name);
19926 }
19927 fe.symtab = builder->get_current_subfile ()->symtab;
19928 }
19929 }
19930 }
19931
19932 /* Start a subfile for DWARF. FILENAME is the name of the file and
19933 DIRNAME the name of the source directory which contains FILENAME
19934 or NULL if not known.
19935 This routine tries to keep line numbers from identical absolute and
19936 relative file names in a common subfile.
19937
19938 Using the `list' example from the GDB testsuite, which resides in
19939 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19940 of /srcdir/list0.c yields the following debugging information for list0.c:
19941
19942 DW_AT_name: /srcdir/list0.c
19943 DW_AT_comp_dir: /compdir
19944 files.files[0].name: list0.h
19945 files.files[0].dir: /srcdir
19946 files.files[1].name: list0.c
19947 files.files[1].dir: /srcdir
19948
19949 The line number information for list0.c has to end up in a single
19950 subfile, so that `break /srcdir/list0.c:1' works as expected.
19951 start_subfile will ensure that this happens provided that we pass the
19952 concatenation of files.files[1].dir and files.files[1].name as the
19953 subfile's name. */
19954
19955 static void
19956 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
19957 const char *dirname)
19958 {
19959 gdb::unique_xmalloc_ptr<char> copy;
19960
19961 /* In order not to lose the line information directory,
19962 we concatenate it to the filename when it makes sense.
19963 Note that the Dwarf3 standard says (speaking of filenames in line
19964 information): ``The directory index is ignored for file names
19965 that represent full path names''. Thus ignoring dirname in the
19966 `else' branch below isn't an issue. */
19967
19968 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
19969 {
19970 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
19971 filename = copy.get ();
19972 }
19973
19974 cu->get_builder ()->start_subfile (filename);
19975 }
19976
19977 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
19978 buildsym_compunit constructor. */
19979
19980 struct compunit_symtab *
19981 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
19982 CORE_ADDR low_pc)
19983 {
19984 gdb_assert (m_builder == nullptr);
19985
19986 m_builder.reset (new struct buildsym_compunit
19987 (per_cu->dwarf2_per_objfile->objfile,
19988 name, comp_dir, language, low_pc));
19989
19990 list_in_scope = get_builder ()->get_file_symbols ();
19991
19992 get_builder ()->record_debugformat ("DWARF 2");
19993 get_builder ()->record_producer (producer);
19994
19995 processing_has_namespace_info = false;
19996
19997 return get_builder ()->get_compunit_symtab ();
19998 }
19999
20000 static void
20001 var_decode_location (struct attribute *attr, struct symbol *sym,
20002 struct dwarf2_cu *cu)
20003 {
20004 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20005 struct comp_unit_head *cu_header = &cu->header;
20006
20007 /* NOTE drow/2003-01-30: There used to be a comment and some special
20008 code here to turn a symbol with DW_AT_external and a
20009 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20010 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20011 with some versions of binutils) where shared libraries could have
20012 relocations against symbols in their debug information - the
20013 minimal symbol would have the right address, but the debug info
20014 would not. It's no longer necessary, because we will explicitly
20015 apply relocations when we read in the debug information now. */
20016
20017 /* A DW_AT_location attribute with no contents indicates that a
20018 variable has been optimized away. */
20019 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20020 {
20021 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20022 return;
20023 }
20024
20025 /* Handle one degenerate form of location expression specially, to
20026 preserve GDB's previous behavior when section offsets are
20027 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20028 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20029
20030 if (attr->form_is_block ()
20031 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20032 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20033 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20034 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20035 && (DW_BLOCK (attr)->size
20036 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20037 {
20038 unsigned int dummy;
20039
20040 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20041 SET_SYMBOL_VALUE_ADDRESS
20042 (sym, cu->header.read_address (objfile->obfd,
20043 DW_BLOCK (attr)->data + 1,
20044 &dummy));
20045 else
20046 SET_SYMBOL_VALUE_ADDRESS
20047 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20048 &dummy));
20049 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20050 fixup_symbol_section (sym, objfile);
20051 SET_SYMBOL_VALUE_ADDRESS
20052 (sym,
20053 SYMBOL_VALUE_ADDRESS (sym)
20054 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20055 return;
20056 }
20057
20058 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20059 expression evaluator, and use LOC_COMPUTED only when necessary
20060 (i.e. when the value of a register or memory location is
20061 referenced, or a thread-local block, etc.). Then again, it might
20062 not be worthwhile. I'm assuming that it isn't unless performance
20063 or memory numbers show me otherwise. */
20064
20065 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20066
20067 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20068 cu->has_loclist = true;
20069 }
20070
20071 /* Given a pointer to a DWARF information entry, figure out if we need
20072 to make a symbol table entry for it, and if so, create a new entry
20073 and return a pointer to it.
20074 If TYPE is NULL, determine symbol type from the die, otherwise
20075 used the passed type.
20076 If SPACE is not NULL, use it to hold the new symbol. If it is
20077 NULL, allocate a new symbol on the objfile's obstack. */
20078
20079 static struct symbol *
20080 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20081 struct symbol *space)
20082 {
20083 struct dwarf2_per_objfile *dwarf2_per_objfile
20084 = cu->per_cu->dwarf2_per_objfile;
20085 struct objfile *objfile = dwarf2_per_objfile->objfile;
20086 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20087 struct symbol *sym = NULL;
20088 const char *name;
20089 struct attribute *attr = NULL;
20090 struct attribute *attr2 = NULL;
20091 CORE_ADDR baseaddr;
20092 struct pending **list_to_add = NULL;
20093
20094 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20095
20096 baseaddr = objfile->text_section_offset ();
20097
20098 name = dwarf2_name (die, cu);
20099 if (name)
20100 {
20101 const char *linkagename;
20102 int suppress_add = 0;
20103
20104 if (space)
20105 sym = space;
20106 else
20107 sym = allocate_symbol (objfile);
20108 OBJSTAT (objfile, n_syms++);
20109
20110 /* Cache this symbol's name and the name's demangled form (if any). */
20111 sym->set_language (cu->language, &objfile->objfile_obstack);
20112 linkagename = dwarf2_physname (name, die, cu);
20113 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20114
20115 /* Fortran does not have mangling standard and the mangling does differ
20116 between gfortran, iFort etc. */
20117 if (cu->language == language_fortran
20118 && symbol_get_demangled_name (sym) == NULL)
20119 symbol_set_demangled_name (sym,
20120 dwarf2_full_name (name, die, cu),
20121 NULL);
20122
20123 /* Default assumptions.
20124 Use the passed type or decode it from the die. */
20125 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20126 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20127 if (type != NULL)
20128 SYMBOL_TYPE (sym) = type;
20129 else
20130 SYMBOL_TYPE (sym) = die_type (die, cu);
20131 attr = dwarf2_attr (die,
20132 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20133 cu);
20134 if (attr != nullptr)
20135 {
20136 SYMBOL_LINE (sym) = DW_UNSND (attr);
20137 }
20138
20139 attr = dwarf2_attr (die,
20140 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20141 cu);
20142 if (attr != nullptr)
20143 {
20144 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20145 struct file_entry *fe;
20146
20147 if (cu->line_header != NULL)
20148 fe = cu->line_header->file_name_at (file_index);
20149 else
20150 fe = NULL;
20151
20152 if (fe == NULL)
20153 complaint (_("file index out of range"));
20154 else
20155 symbol_set_symtab (sym, fe->symtab);
20156 }
20157
20158 switch (die->tag)
20159 {
20160 case DW_TAG_label:
20161 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20162 if (attr != nullptr)
20163 {
20164 CORE_ADDR addr;
20165
20166 addr = attr->value_as_address ();
20167 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20168 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20169 }
20170 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20171 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20172 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20173 add_symbol_to_list (sym, cu->list_in_scope);
20174 break;
20175 case DW_TAG_subprogram:
20176 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20177 finish_block. */
20178 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20179 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20180 if ((attr2 && (DW_UNSND (attr2) != 0))
20181 || cu->language == language_ada
20182 || cu->language == language_fortran)
20183 {
20184 /* Subprograms marked external are stored as a global symbol.
20185 Ada and Fortran subprograms, whether marked external or
20186 not, are always stored as a global symbol, because we want
20187 to be able to access them globally. For instance, we want
20188 to be able to break on a nested subprogram without having
20189 to specify the context. */
20190 list_to_add = cu->get_builder ()->get_global_symbols ();
20191 }
20192 else
20193 {
20194 list_to_add = cu->list_in_scope;
20195 }
20196 break;
20197 case DW_TAG_inlined_subroutine:
20198 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20199 finish_block. */
20200 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20201 SYMBOL_INLINED (sym) = 1;
20202 list_to_add = cu->list_in_scope;
20203 break;
20204 case DW_TAG_template_value_param:
20205 suppress_add = 1;
20206 /* Fall through. */
20207 case DW_TAG_constant:
20208 case DW_TAG_variable:
20209 case DW_TAG_member:
20210 /* Compilation with minimal debug info may result in
20211 variables with missing type entries. Change the
20212 misleading `void' type to something sensible. */
20213 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20214 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20215
20216 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20217 /* In the case of DW_TAG_member, we should only be called for
20218 static const members. */
20219 if (die->tag == DW_TAG_member)
20220 {
20221 /* dwarf2_add_field uses die_is_declaration,
20222 so we do the same. */
20223 gdb_assert (die_is_declaration (die, cu));
20224 gdb_assert (attr);
20225 }
20226 if (attr != nullptr)
20227 {
20228 dwarf2_const_value (attr, sym, cu);
20229 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20230 if (!suppress_add)
20231 {
20232 if (attr2 && (DW_UNSND (attr2) != 0))
20233 list_to_add = cu->get_builder ()->get_global_symbols ();
20234 else
20235 list_to_add = cu->list_in_scope;
20236 }
20237 break;
20238 }
20239 attr = dwarf2_attr (die, DW_AT_location, cu);
20240 if (attr != nullptr)
20241 {
20242 var_decode_location (attr, sym, cu);
20243 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20244
20245 /* Fortran explicitly imports any global symbols to the local
20246 scope by DW_TAG_common_block. */
20247 if (cu->language == language_fortran && die->parent
20248 && die->parent->tag == DW_TAG_common_block)
20249 attr2 = NULL;
20250
20251 if (SYMBOL_CLASS (sym) == LOC_STATIC
20252 && SYMBOL_VALUE_ADDRESS (sym) == 0
20253 && !dwarf2_per_objfile->has_section_at_zero)
20254 {
20255 /* When a static variable is eliminated by the linker,
20256 the corresponding debug information is not stripped
20257 out, but the variable address is set to null;
20258 do not add such variables into symbol table. */
20259 }
20260 else if (attr2 && (DW_UNSND (attr2) != 0))
20261 {
20262 if (SYMBOL_CLASS (sym) == LOC_STATIC
20263 && (objfile->flags & OBJF_MAINLINE) == 0
20264 && dwarf2_per_objfile->can_copy)
20265 {
20266 /* A global static variable might be subject to
20267 copy relocation. We first check for a local
20268 minsym, though, because maybe the symbol was
20269 marked hidden, in which case this would not
20270 apply. */
20271 bound_minimal_symbol found
20272 = (lookup_minimal_symbol_linkage
20273 (sym->linkage_name (), objfile));
20274 if (found.minsym != nullptr)
20275 sym->maybe_copied = 1;
20276 }
20277
20278 /* A variable with DW_AT_external is never static,
20279 but it may be block-scoped. */
20280 list_to_add
20281 = ((cu->list_in_scope
20282 == cu->get_builder ()->get_file_symbols ())
20283 ? cu->get_builder ()->get_global_symbols ()
20284 : cu->list_in_scope);
20285 }
20286 else
20287 list_to_add = cu->list_in_scope;
20288 }
20289 else
20290 {
20291 /* We do not know the address of this symbol.
20292 If it is an external symbol and we have type information
20293 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20294 The address of the variable will then be determined from
20295 the minimal symbol table whenever the variable is
20296 referenced. */
20297 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20298
20299 /* Fortran explicitly imports any global symbols to the local
20300 scope by DW_TAG_common_block. */
20301 if (cu->language == language_fortran && die->parent
20302 && die->parent->tag == DW_TAG_common_block)
20303 {
20304 /* SYMBOL_CLASS doesn't matter here because
20305 read_common_block is going to reset it. */
20306 if (!suppress_add)
20307 list_to_add = cu->list_in_scope;
20308 }
20309 else if (attr2 && (DW_UNSND (attr2) != 0)
20310 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20311 {
20312 /* A variable with DW_AT_external is never static, but it
20313 may be block-scoped. */
20314 list_to_add
20315 = ((cu->list_in_scope
20316 == cu->get_builder ()->get_file_symbols ())
20317 ? cu->get_builder ()->get_global_symbols ()
20318 : cu->list_in_scope);
20319
20320 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20321 }
20322 else if (!die_is_declaration (die, cu))
20323 {
20324 /* Use the default LOC_OPTIMIZED_OUT class. */
20325 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20326 if (!suppress_add)
20327 list_to_add = cu->list_in_scope;
20328 }
20329 }
20330 break;
20331 case DW_TAG_formal_parameter:
20332 {
20333 /* If we are inside a function, mark this as an argument. If
20334 not, we might be looking at an argument to an inlined function
20335 when we do not have enough information to show inlined frames;
20336 pretend it's a local variable in that case so that the user can
20337 still see it. */
20338 struct context_stack *curr
20339 = cu->get_builder ()->get_current_context_stack ();
20340 if (curr != nullptr && curr->name != nullptr)
20341 SYMBOL_IS_ARGUMENT (sym) = 1;
20342 attr = dwarf2_attr (die, DW_AT_location, cu);
20343 if (attr != nullptr)
20344 {
20345 var_decode_location (attr, sym, cu);
20346 }
20347 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20348 if (attr != nullptr)
20349 {
20350 dwarf2_const_value (attr, sym, cu);
20351 }
20352
20353 list_to_add = cu->list_in_scope;
20354 }
20355 break;
20356 case DW_TAG_unspecified_parameters:
20357 /* From varargs functions; gdb doesn't seem to have any
20358 interest in this information, so just ignore it for now.
20359 (FIXME?) */
20360 break;
20361 case DW_TAG_template_type_param:
20362 suppress_add = 1;
20363 /* Fall through. */
20364 case DW_TAG_class_type:
20365 case DW_TAG_interface_type:
20366 case DW_TAG_structure_type:
20367 case DW_TAG_union_type:
20368 case DW_TAG_set_type:
20369 case DW_TAG_enumeration_type:
20370 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20371 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20372
20373 {
20374 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20375 really ever be static objects: otherwise, if you try
20376 to, say, break of a class's method and you're in a file
20377 which doesn't mention that class, it won't work unless
20378 the check for all static symbols in lookup_symbol_aux
20379 saves you. See the OtherFileClass tests in
20380 gdb.c++/namespace.exp. */
20381
20382 if (!suppress_add)
20383 {
20384 buildsym_compunit *builder = cu->get_builder ();
20385 list_to_add
20386 = (cu->list_in_scope == builder->get_file_symbols ()
20387 && cu->language == language_cplus
20388 ? builder->get_global_symbols ()
20389 : cu->list_in_scope);
20390
20391 /* The semantics of C++ state that "struct foo {
20392 ... }" also defines a typedef for "foo". */
20393 if (cu->language == language_cplus
20394 || cu->language == language_ada
20395 || cu->language == language_d
20396 || cu->language == language_rust)
20397 {
20398 /* The symbol's name is already allocated along
20399 with this objfile, so we don't need to
20400 duplicate it for the type. */
20401 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20402 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20403 }
20404 }
20405 }
20406 break;
20407 case DW_TAG_typedef:
20408 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20409 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20410 list_to_add = cu->list_in_scope;
20411 break;
20412 case DW_TAG_base_type:
20413 case DW_TAG_subrange_type:
20414 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20415 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20416 list_to_add = cu->list_in_scope;
20417 break;
20418 case DW_TAG_enumerator:
20419 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20420 if (attr != nullptr)
20421 {
20422 dwarf2_const_value (attr, sym, cu);
20423 }
20424 {
20425 /* NOTE: carlton/2003-11-10: See comment above in the
20426 DW_TAG_class_type, etc. block. */
20427
20428 list_to_add
20429 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20430 && cu->language == language_cplus
20431 ? cu->get_builder ()->get_global_symbols ()
20432 : cu->list_in_scope);
20433 }
20434 break;
20435 case DW_TAG_imported_declaration:
20436 case DW_TAG_namespace:
20437 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20438 list_to_add = cu->get_builder ()->get_global_symbols ();
20439 break;
20440 case DW_TAG_module:
20441 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20442 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20443 list_to_add = cu->get_builder ()->get_global_symbols ();
20444 break;
20445 case DW_TAG_common_block:
20446 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20447 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20448 add_symbol_to_list (sym, cu->list_in_scope);
20449 break;
20450 default:
20451 /* Not a tag we recognize. Hopefully we aren't processing
20452 trash data, but since we must specifically ignore things
20453 we don't recognize, there is nothing else we should do at
20454 this point. */
20455 complaint (_("unsupported tag: '%s'"),
20456 dwarf_tag_name (die->tag));
20457 break;
20458 }
20459
20460 if (suppress_add)
20461 {
20462 sym->hash_next = objfile->template_symbols;
20463 objfile->template_symbols = sym;
20464 list_to_add = NULL;
20465 }
20466
20467 if (list_to_add != NULL)
20468 add_symbol_to_list (sym, list_to_add);
20469
20470 /* For the benefit of old versions of GCC, check for anonymous
20471 namespaces based on the demangled name. */
20472 if (!cu->processing_has_namespace_info
20473 && cu->language == language_cplus)
20474 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20475 }
20476 return (sym);
20477 }
20478
20479 /* Given an attr with a DW_FORM_dataN value in host byte order,
20480 zero-extend it as appropriate for the symbol's type. The DWARF
20481 standard (v4) is not entirely clear about the meaning of using
20482 DW_FORM_dataN for a constant with a signed type, where the type is
20483 wider than the data. The conclusion of a discussion on the DWARF
20484 list was that this is unspecified. We choose to always zero-extend
20485 because that is the interpretation long in use by GCC. */
20486
20487 static gdb_byte *
20488 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20489 struct dwarf2_cu *cu, LONGEST *value, int bits)
20490 {
20491 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20492 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20493 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20494 LONGEST l = DW_UNSND (attr);
20495
20496 if (bits < sizeof (*value) * 8)
20497 {
20498 l &= ((LONGEST) 1 << bits) - 1;
20499 *value = l;
20500 }
20501 else if (bits == sizeof (*value) * 8)
20502 *value = l;
20503 else
20504 {
20505 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20506 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20507 return bytes;
20508 }
20509
20510 return NULL;
20511 }
20512
20513 /* Read a constant value from an attribute. Either set *VALUE, or if
20514 the value does not fit in *VALUE, set *BYTES - either already
20515 allocated on the objfile obstack, or newly allocated on OBSTACK,
20516 or, set *BATON, if we translated the constant to a location
20517 expression. */
20518
20519 static void
20520 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20521 const char *name, struct obstack *obstack,
20522 struct dwarf2_cu *cu,
20523 LONGEST *value, const gdb_byte **bytes,
20524 struct dwarf2_locexpr_baton **baton)
20525 {
20526 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20527 struct comp_unit_head *cu_header = &cu->header;
20528 struct dwarf_block *blk;
20529 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20530 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20531
20532 *value = 0;
20533 *bytes = NULL;
20534 *baton = NULL;
20535
20536 switch (attr->form)
20537 {
20538 case DW_FORM_addr:
20539 case DW_FORM_addrx:
20540 case DW_FORM_GNU_addr_index:
20541 {
20542 gdb_byte *data;
20543
20544 if (TYPE_LENGTH (type) != cu_header->addr_size)
20545 dwarf2_const_value_length_mismatch_complaint (name,
20546 cu_header->addr_size,
20547 TYPE_LENGTH (type));
20548 /* Symbols of this form are reasonably rare, so we just
20549 piggyback on the existing location code rather than writing
20550 a new implementation of symbol_computed_ops. */
20551 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20552 (*baton)->per_cu = cu->per_cu;
20553 gdb_assert ((*baton)->per_cu);
20554
20555 (*baton)->size = 2 + cu_header->addr_size;
20556 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20557 (*baton)->data = data;
20558
20559 data[0] = DW_OP_addr;
20560 store_unsigned_integer (&data[1], cu_header->addr_size,
20561 byte_order, DW_ADDR (attr));
20562 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20563 }
20564 break;
20565 case DW_FORM_string:
20566 case DW_FORM_strp:
20567 case DW_FORM_strx:
20568 case DW_FORM_GNU_str_index:
20569 case DW_FORM_GNU_strp_alt:
20570 /* DW_STRING is already allocated on the objfile obstack, point
20571 directly to it. */
20572 *bytes = (const gdb_byte *) DW_STRING (attr);
20573 break;
20574 case DW_FORM_block1:
20575 case DW_FORM_block2:
20576 case DW_FORM_block4:
20577 case DW_FORM_block:
20578 case DW_FORM_exprloc:
20579 case DW_FORM_data16:
20580 blk = DW_BLOCK (attr);
20581 if (TYPE_LENGTH (type) != blk->size)
20582 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20583 TYPE_LENGTH (type));
20584 *bytes = blk->data;
20585 break;
20586
20587 /* The DW_AT_const_value attributes are supposed to carry the
20588 symbol's value "represented as it would be on the target
20589 architecture." By the time we get here, it's already been
20590 converted to host endianness, so we just need to sign- or
20591 zero-extend it as appropriate. */
20592 case DW_FORM_data1:
20593 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20594 break;
20595 case DW_FORM_data2:
20596 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20597 break;
20598 case DW_FORM_data4:
20599 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20600 break;
20601 case DW_FORM_data8:
20602 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20603 break;
20604
20605 case DW_FORM_sdata:
20606 case DW_FORM_implicit_const:
20607 *value = DW_SND (attr);
20608 break;
20609
20610 case DW_FORM_udata:
20611 *value = DW_UNSND (attr);
20612 break;
20613
20614 default:
20615 complaint (_("unsupported const value attribute form: '%s'"),
20616 dwarf_form_name (attr->form));
20617 *value = 0;
20618 break;
20619 }
20620 }
20621
20622
20623 /* Copy constant value from an attribute to a symbol. */
20624
20625 static void
20626 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20627 struct dwarf2_cu *cu)
20628 {
20629 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20630 LONGEST value;
20631 const gdb_byte *bytes;
20632 struct dwarf2_locexpr_baton *baton;
20633
20634 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20635 sym->print_name (),
20636 &objfile->objfile_obstack, cu,
20637 &value, &bytes, &baton);
20638
20639 if (baton != NULL)
20640 {
20641 SYMBOL_LOCATION_BATON (sym) = baton;
20642 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20643 }
20644 else if (bytes != NULL)
20645 {
20646 SYMBOL_VALUE_BYTES (sym) = bytes;
20647 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20648 }
20649 else
20650 {
20651 SYMBOL_VALUE (sym) = value;
20652 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20653 }
20654 }
20655
20656 /* Return the type of the die in question using its DW_AT_type attribute. */
20657
20658 static struct type *
20659 die_type (struct die_info *die, struct dwarf2_cu *cu)
20660 {
20661 struct attribute *type_attr;
20662
20663 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20664 if (!type_attr)
20665 {
20666 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20667 /* A missing DW_AT_type represents a void type. */
20668 return objfile_type (objfile)->builtin_void;
20669 }
20670
20671 return lookup_die_type (die, type_attr, cu);
20672 }
20673
20674 /* True iff CU's producer generates GNAT Ada auxiliary information
20675 that allows to find parallel types through that information instead
20676 of having to do expensive parallel lookups by type name. */
20677
20678 static int
20679 need_gnat_info (struct dwarf2_cu *cu)
20680 {
20681 /* Assume that the Ada compiler was GNAT, which always produces
20682 the auxiliary information. */
20683 return (cu->language == language_ada);
20684 }
20685
20686 /* Return the auxiliary type of the die in question using its
20687 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20688 attribute is not present. */
20689
20690 static struct type *
20691 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20692 {
20693 struct attribute *type_attr;
20694
20695 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20696 if (!type_attr)
20697 return NULL;
20698
20699 return lookup_die_type (die, type_attr, cu);
20700 }
20701
20702 /* If DIE has a descriptive_type attribute, then set the TYPE's
20703 descriptive type accordingly. */
20704
20705 static void
20706 set_descriptive_type (struct type *type, struct die_info *die,
20707 struct dwarf2_cu *cu)
20708 {
20709 struct type *descriptive_type = die_descriptive_type (die, cu);
20710
20711 if (descriptive_type)
20712 {
20713 ALLOCATE_GNAT_AUX_TYPE (type);
20714 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20715 }
20716 }
20717
20718 /* Return the containing type of the die in question using its
20719 DW_AT_containing_type attribute. */
20720
20721 static struct type *
20722 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20723 {
20724 struct attribute *type_attr;
20725 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20726
20727 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20728 if (!type_attr)
20729 error (_("Dwarf Error: Problem turning containing type into gdb type "
20730 "[in module %s]"), objfile_name (objfile));
20731
20732 return lookup_die_type (die, type_attr, cu);
20733 }
20734
20735 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20736
20737 static struct type *
20738 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20739 {
20740 struct dwarf2_per_objfile *dwarf2_per_objfile
20741 = cu->per_cu->dwarf2_per_objfile;
20742 struct objfile *objfile = dwarf2_per_objfile->objfile;
20743 char *saved;
20744
20745 std::string message
20746 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20747 objfile_name (objfile),
20748 sect_offset_str (cu->header.sect_off),
20749 sect_offset_str (die->sect_off));
20750 saved = obstack_strdup (&objfile->objfile_obstack, message);
20751
20752 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20753 }
20754
20755 /* Look up the type of DIE in CU using its type attribute ATTR.
20756 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20757 DW_AT_containing_type.
20758 If there is no type substitute an error marker. */
20759
20760 static struct type *
20761 lookup_die_type (struct die_info *die, const struct attribute *attr,
20762 struct dwarf2_cu *cu)
20763 {
20764 struct dwarf2_per_objfile *dwarf2_per_objfile
20765 = cu->per_cu->dwarf2_per_objfile;
20766 struct objfile *objfile = dwarf2_per_objfile->objfile;
20767 struct type *this_type;
20768
20769 gdb_assert (attr->name == DW_AT_type
20770 || attr->name == DW_AT_GNAT_descriptive_type
20771 || attr->name == DW_AT_containing_type);
20772
20773 /* First see if we have it cached. */
20774
20775 if (attr->form == DW_FORM_GNU_ref_alt)
20776 {
20777 struct dwarf2_per_cu_data *per_cu;
20778 sect_offset sect_off = attr->get_ref_die_offset ();
20779
20780 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20781 dwarf2_per_objfile);
20782 this_type = get_die_type_at_offset (sect_off, per_cu);
20783 }
20784 else if (attr->form_is_ref ())
20785 {
20786 sect_offset sect_off = attr->get_ref_die_offset ();
20787
20788 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20789 }
20790 else if (attr->form == DW_FORM_ref_sig8)
20791 {
20792 ULONGEST signature = DW_SIGNATURE (attr);
20793
20794 return get_signatured_type (die, signature, cu);
20795 }
20796 else
20797 {
20798 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
20799 " at %s [in module %s]"),
20800 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
20801 objfile_name (objfile));
20802 return build_error_marker_type (cu, die);
20803 }
20804
20805 /* If not cached we need to read it in. */
20806
20807 if (this_type == NULL)
20808 {
20809 struct die_info *type_die = NULL;
20810 struct dwarf2_cu *type_cu = cu;
20811
20812 if (attr->form_is_ref ())
20813 type_die = follow_die_ref (die, attr, &type_cu);
20814 if (type_die == NULL)
20815 return build_error_marker_type (cu, die);
20816 /* If we find the type now, it's probably because the type came
20817 from an inter-CU reference and the type's CU got expanded before
20818 ours. */
20819 this_type = read_type_die (type_die, type_cu);
20820 }
20821
20822 /* If we still don't have a type use an error marker. */
20823
20824 if (this_type == NULL)
20825 return build_error_marker_type (cu, die);
20826
20827 return this_type;
20828 }
20829
20830 /* Return the type in DIE, CU.
20831 Returns NULL for invalid types.
20832
20833 This first does a lookup in die_type_hash,
20834 and only reads the die in if necessary.
20835
20836 NOTE: This can be called when reading in partial or full symbols. */
20837
20838 static struct type *
20839 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20840 {
20841 struct type *this_type;
20842
20843 this_type = get_die_type (die, cu);
20844 if (this_type)
20845 return this_type;
20846
20847 return read_type_die_1 (die, cu);
20848 }
20849
20850 /* Read the type in DIE, CU.
20851 Returns NULL for invalid types. */
20852
20853 static struct type *
20854 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20855 {
20856 struct type *this_type = NULL;
20857
20858 switch (die->tag)
20859 {
20860 case DW_TAG_class_type:
20861 case DW_TAG_interface_type:
20862 case DW_TAG_structure_type:
20863 case DW_TAG_union_type:
20864 this_type = read_structure_type (die, cu);
20865 break;
20866 case DW_TAG_enumeration_type:
20867 this_type = read_enumeration_type (die, cu);
20868 break;
20869 case DW_TAG_subprogram:
20870 case DW_TAG_subroutine_type:
20871 case DW_TAG_inlined_subroutine:
20872 this_type = read_subroutine_type (die, cu);
20873 break;
20874 case DW_TAG_array_type:
20875 this_type = read_array_type (die, cu);
20876 break;
20877 case DW_TAG_set_type:
20878 this_type = read_set_type (die, cu);
20879 break;
20880 case DW_TAG_pointer_type:
20881 this_type = read_tag_pointer_type (die, cu);
20882 break;
20883 case DW_TAG_ptr_to_member_type:
20884 this_type = read_tag_ptr_to_member_type (die, cu);
20885 break;
20886 case DW_TAG_reference_type:
20887 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20888 break;
20889 case DW_TAG_rvalue_reference_type:
20890 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
20891 break;
20892 case DW_TAG_const_type:
20893 this_type = read_tag_const_type (die, cu);
20894 break;
20895 case DW_TAG_volatile_type:
20896 this_type = read_tag_volatile_type (die, cu);
20897 break;
20898 case DW_TAG_restrict_type:
20899 this_type = read_tag_restrict_type (die, cu);
20900 break;
20901 case DW_TAG_string_type:
20902 this_type = read_tag_string_type (die, cu);
20903 break;
20904 case DW_TAG_typedef:
20905 this_type = read_typedef (die, cu);
20906 break;
20907 case DW_TAG_subrange_type:
20908 this_type = read_subrange_type (die, cu);
20909 break;
20910 case DW_TAG_base_type:
20911 this_type = read_base_type (die, cu);
20912 break;
20913 case DW_TAG_unspecified_type:
20914 this_type = read_unspecified_type (die, cu);
20915 break;
20916 case DW_TAG_namespace:
20917 this_type = read_namespace_type (die, cu);
20918 break;
20919 case DW_TAG_module:
20920 this_type = read_module_type (die, cu);
20921 break;
20922 case DW_TAG_atomic_type:
20923 this_type = read_tag_atomic_type (die, cu);
20924 break;
20925 default:
20926 complaint (_("unexpected tag in read_type_die: '%s'"),
20927 dwarf_tag_name (die->tag));
20928 break;
20929 }
20930
20931 return this_type;
20932 }
20933
20934 /* See if we can figure out if the class lives in a namespace. We do
20935 this by looking for a member function; its demangled name will
20936 contain namespace info, if there is any.
20937 Return the computed name or NULL.
20938 Space for the result is allocated on the objfile's obstack.
20939 This is the full-die version of guess_partial_die_structure_name.
20940 In this case we know DIE has no useful parent. */
20941
20942 static const char *
20943 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20944 {
20945 struct die_info *spec_die;
20946 struct dwarf2_cu *spec_cu;
20947 struct die_info *child;
20948 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20949
20950 spec_cu = cu;
20951 spec_die = die_specification (die, &spec_cu);
20952 if (spec_die != NULL)
20953 {
20954 die = spec_die;
20955 cu = spec_cu;
20956 }
20957
20958 for (child = die->child;
20959 child != NULL;
20960 child = child->sibling)
20961 {
20962 if (child->tag == DW_TAG_subprogram)
20963 {
20964 const char *linkage_name = dw2_linkage_name (child, cu);
20965
20966 if (linkage_name != NULL)
20967 {
20968 gdb::unique_xmalloc_ptr<char> actual_name
20969 (language_class_name_from_physname (cu->language_defn,
20970 linkage_name));
20971 const char *name = NULL;
20972
20973 if (actual_name != NULL)
20974 {
20975 const char *die_name = dwarf2_name (die, cu);
20976
20977 if (die_name != NULL
20978 && strcmp (die_name, actual_name.get ()) != 0)
20979 {
20980 /* Strip off the class name from the full name.
20981 We want the prefix. */
20982 int die_name_len = strlen (die_name);
20983 int actual_name_len = strlen (actual_name.get ());
20984 const char *ptr = actual_name.get ();
20985
20986 /* Test for '::' as a sanity check. */
20987 if (actual_name_len > die_name_len + 2
20988 && ptr[actual_name_len - die_name_len - 1] == ':')
20989 name = obstack_strndup (
20990 &objfile->per_bfd->storage_obstack,
20991 ptr, actual_name_len - die_name_len - 2);
20992 }
20993 }
20994 return name;
20995 }
20996 }
20997 }
20998
20999 return NULL;
21000 }
21001
21002 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21003 prefix part in such case. See
21004 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21005
21006 static const char *
21007 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21008 {
21009 struct attribute *attr;
21010 const char *base;
21011
21012 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21013 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21014 return NULL;
21015
21016 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21017 return NULL;
21018
21019 attr = dw2_linkage_name_attr (die, cu);
21020 if (attr == NULL || DW_STRING (attr) == NULL)
21021 return NULL;
21022
21023 /* dwarf2_name had to be already called. */
21024 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21025
21026 /* Strip the base name, keep any leading namespaces/classes. */
21027 base = strrchr (DW_STRING (attr), ':');
21028 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21029 return "";
21030
21031 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21032 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21033 DW_STRING (attr),
21034 &base[-1] - DW_STRING (attr));
21035 }
21036
21037 /* Return the name of the namespace/class that DIE is defined within,
21038 or "" if we can't tell. The caller should not xfree the result.
21039
21040 For example, if we're within the method foo() in the following
21041 code:
21042
21043 namespace N {
21044 class C {
21045 void foo () {
21046 }
21047 };
21048 }
21049
21050 then determine_prefix on foo's die will return "N::C". */
21051
21052 static const char *
21053 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21054 {
21055 struct dwarf2_per_objfile *dwarf2_per_objfile
21056 = cu->per_cu->dwarf2_per_objfile;
21057 struct die_info *parent, *spec_die;
21058 struct dwarf2_cu *spec_cu;
21059 struct type *parent_type;
21060 const char *retval;
21061
21062 if (cu->language != language_cplus
21063 && cu->language != language_fortran && cu->language != language_d
21064 && cu->language != language_rust)
21065 return "";
21066
21067 retval = anonymous_struct_prefix (die, cu);
21068 if (retval)
21069 return retval;
21070
21071 /* We have to be careful in the presence of DW_AT_specification.
21072 For example, with GCC 3.4, given the code
21073
21074 namespace N {
21075 void foo() {
21076 // Definition of N::foo.
21077 }
21078 }
21079
21080 then we'll have a tree of DIEs like this:
21081
21082 1: DW_TAG_compile_unit
21083 2: DW_TAG_namespace // N
21084 3: DW_TAG_subprogram // declaration of N::foo
21085 4: DW_TAG_subprogram // definition of N::foo
21086 DW_AT_specification // refers to die #3
21087
21088 Thus, when processing die #4, we have to pretend that we're in
21089 the context of its DW_AT_specification, namely the contex of die
21090 #3. */
21091 spec_cu = cu;
21092 spec_die = die_specification (die, &spec_cu);
21093 if (spec_die == NULL)
21094 parent = die->parent;
21095 else
21096 {
21097 parent = spec_die->parent;
21098 cu = spec_cu;
21099 }
21100
21101 if (parent == NULL)
21102 return "";
21103 else if (parent->building_fullname)
21104 {
21105 const char *name;
21106 const char *parent_name;
21107
21108 /* It has been seen on RealView 2.2 built binaries,
21109 DW_TAG_template_type_param types actually _defined_ as
21110 children of the parent class:
21111
21112 enum E {};
21113 template class <class Enum> Class{};
21114 Class<enum E> class_e;
21115
21116 1: DW_TAG_class_type (Class)
21117 2: DW_TAG_enumeration_type (E)
21118 3: DW_TAG_enumerator (enum1:0)
21119 3: DW_TAG_enumerator (enum2:1)
21120 ...
21121 2: DW_TAG_template_type_param
21122 DW_AT_type DW_FORM_ref_udata (E)
21123
21124 Besides being broken debug info, it can put GDB into an
21125 infinite loop. Consider:
21126
21127 When we're building the full name for Class<E>, we'll start
21128 at Class, and go look over its template type parameters,
21129 finding E. We'll then try to build the full name of E, and
21130 reach here. We're now trying to build the full name of E,
21131 and look over the parent DIE for containing scope. In the
21132 broken case, if we followed the parent DIE of E, we'd again
21133 find Class, and once again go look at its template type
21134 arguments, etc., etc. Simply don't consider such parent die
21135 as source-level parent of this die (it can't be, the language
21136 doesn't allow it), and break the loop here. */
21137 name = dwarf2_name (die, cu);
21138 parent_name = dwarf2_name (parent, cu);
21139 complaint (_("template param type '%s' defined within parent '%s'"),
21140 name ? name : "<unknown>",
21141 parent_name ? parent_name : "<unknown>");
21142 return "";
21143 }
21144 else
21145 switch (parent->tag)
21146 {
21147 case DW_TAG_namespace:
21148 parent_type = read_type_die (parent, cu);
21149 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21150 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21151 Work around this problem here. */
21152 if (cu->language == language_cplus
21153 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21154 return "";
21155 /* We give a name to even anonymous namespaces. */
21156 return TYPE_NAME (parent_type);
21157 case DW_TAG_class_type:
21158 case DW_TAG_interface_type:
21159 case DW_TAG_structure_type:
21160 case DW_TAG_union_type:
21161 case DW_TAG_module:
21162 parent_type = read_type_die (parent, cu);
21163 if (TYPE_NAME (parent_type) != NULL)
21164 return TYPE_NAME (parent_type);
21165 else
21166 /* An anonymous structure is only allowed non-static data
21167 members; no typedefs, no member functions, et cetera.
21168 So it does not need a prefix. */
21169 return "";
21170 case DW_TAG_compile_unit:
21171 case DW_TAG_partial_unit:
21172 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21173 if (cu->language == language_cplus
21174 && !dwarf2_per_objfile->types.empty ()
21175 && die->child != NULL
21176 && (die->tag == DW_TAG_class_type
21177 || die->tag == DW_TAG_structure_type
21178 || die->tag == DW_TAG_union_type))
21179 {
21180 const char *name = guess_full_die_structure_name (die, cu);
21181 if (name != NULL)
21182 return name;
21183 }
21184 return "";
21185 case DW_TAG_subprogram:
21186 /* Nested subroutines in Fortran get a prefix with the name
21187 of the parent's subroutine. */
21188 if (cu->language == language_fortran)
21189 {
21190 if ((die->tag == DW_TAG_subprogram)
21191 && (dwarf2_name (parent, cu) != NULL))
21192 return dwarf2_name (parent, cu);
21193 }
21194 return determine_prefix (parent, cu);
21195 case DW_TAG_enumeration_type:
21196 parent_type = read_type_die (parent, cu);
21197 if (TYPE_DECLARED_CLASS (parent_type))
21198 {
21199 if (TYPE_NAME (parent_type) != NULL)
21200 return TYPE_NAME (parent_type);
21201 return "";
21202 }
21203 /* Fall through. */
21204 default:
21205 return determine_prefix (parent, cu);
21206 }
21207 }
21208
21209 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21210 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21211 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21212 an obconcat, otherwise allocate storage for the result. The CU argument is
21213 used to determine the language and hence, the appropriate separator. */
21214
21215 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21216
21217 static char *
21218 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21219 int physname, struct dwarf2_cu *cu)
21220 {
21221 const char *lead = "";
21222 const char *sep;
21223
21224 if (suffix == NULL || suffix[0] == '\0'
21225 || prefix == NULL || prefix[0] == '\0')
21226 sep = "";
21227 else if (cu->language == language_d)
21228 {
21229 /* For D, the 'main' function could be defined in any module, but it
21230 should never be prefixed. */
21231 if (strcmp (suffix, "D main") == 0)
21232 {
21233 prefix = "";
21234 sep = "";
21235 }
21236 else
21237 sep = ".";
21238 }
21239 else if (cu->language == language_fortran && physname)
21240 {
21241 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21242 DW_AT_MIPS_linkage_name is preferred and used instead. */
21243
21244 lead = "__";
21245 sep = "_MOD_";
21246 }
21247 else
21248 sep = "::";
21249
21250 if (prefix == NULL)
21251 prefix = "";
21252 if (suffix == NULL)
21253 suffix = "";
21254
21255 if (obs == NULL)
21256 {
21257 char *retval
21258 = ((char *)
21259 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21260
21261 strcpy (retval, lead);
21262 strcat (retval, prefix);
21263 strcat (retval, sep);
21264 strcat (retval, suffix);
21265 return retval;
21266 }
21267 else
21268 {
21269 /* We have an obstack. */
21270 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21271 }
21272 }
21273
21274 /* Get name of a die, return NULL if not found. */
21275
21276 static const char *
21277 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21278 struct objfile *objfile)
21279 {
21280 if (name && cu->language == language_cplus)
21281 {
21282 std::string canon_name = cp_canonicalize_string (name);
21283
21284 if (!canon_name.empty ())
21285 {
21286 if (canon_name != name)
21287 name = objfile->intern (canon_name);
21288 }
21289 }
21290
21291 return name;
21292 }
21293
21294 /* Get name of a die, return NULL if not found.
21295 Anonymous namespaces are converted to their magic string. */
21296
21297 static const char *
21298 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21299 {
21300 struct attribute *attr;
21301 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21302
21303 attr = dwarf2_attr (die, DW_AT_name, cu);
21304 if ((!attr || !DW_STRING (attr))
21305 && die->tag != DW_TAG_namespace
21306 && die->tag != DW_TAG_class_type
21307 && die->tag != DW_TAG_interface_type
21308 && die->tag != DW_TAG_structure_type
21309 && die->tag != DW_TAG_union_type)
21310 return NULL;
21311
21312 switch (die->tag)
21313 {
21314 case DW_TAG_compile_unit:
21315 case DW_TAG_partial_unit:
21316 /* Compilation units have a DW_AT_name that is a filename, not
21317 a source language identifier. */
21318 case DW_TAG_enumeration_type:
21319 case DW_TAG_enumerator:
21320 /* These tags always have simple identifiers already; no need
21321 to canonicalize them. */
21322 return DW_STRING (attr);
21323
21324 case DW_TAG_namespace:
21325 if (attr != NULL && DW_STRING (attr) != NULL)
21326 return DW_STRING (attr);
21327 return CP_ANONYMOUS_NAMESPACE_STR;
21328
21329 case DW_TAG_class_type:
21330 case DW_TAG_interface_type:
21331 case DW_TAG_structure_type:
21332 case DW_TAG_union_type:
21333 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21334 structures or unions. These were of the form "._%d" in GCC 4.1,
21335 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21336 and GCC 4.4. We work around this problem by ignoring these. */
21337 if (attr && DW_STRING (attr)
21338 && (startswith (DW_STRING (attr), "._")
21339 || startswith (DW_STRING (attr), "<anonymous")))
21340 return NULL;
21341
21342 /* GCC might emit a nameless typedef that has a linkage name. See
21343 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21344 if (!attr || DW_STRING (attr) == NULL)
21345 {
21346 attr = dw2_linkage_name_attr (die, cu);
21347 if (attr == NULL || DW_STRING (attr) == NULL)
21348 return NULL;
21349
21350 /* Avoid demangling DW_STRING (attr) the second time on a second
21351 call for the same DIE. */
21352 if (!DW_STRING_IS_CANONICAL (attr))
21353 {
21354 gdb::unique_xmalloc_ptr<char> demangled
21355 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21356 if (demangled == nullptr)
21357 return nullptr;
21358
21359 DW_STRING (attr) = objfile->intern (demangled.get ());
21360 DW_STRING_IS_CANONICAL (attr) = 1;
21361 }
21362
21363 /* Strip any leading namespaces/classes, keep only the base name.
21364 DW_AT_name for named DIEs does not contain the prefixes. */
21365 const char *base = strrchr (DW_STRING (attr), ':');
21366 if (base && base > DW_STRING (attr) && base[-1] == ':')
21367 return &base[1];
21368 else
21369 return DW_STRING (attr);
21370 }
21371 break;
21372
21373 default:
21374 break;
21375 }
21376
21377 if (!DW_STRING_IS_CANONICAL (attr))
21378 {
21379 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21380 objfile);
21381 DW_STRING_IS_CANONICAL (attr) = 1;
21382 }
21383 return DW_STRING (attr);
21384 }
21385
21386 /* Return the die that this die in an extension of, or NULL if there
21387 is none. *EXT_CU is the CU containing DIE on input, and the CU
21388 containing the return value on output. */
21389
21390 static struct die_info *
21391 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21392 {
21393 struct attribute *attr;
21394
21395 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21396 if (attr == NULL)
21397 return NULL;
21398
21399 return follow_die_ref (die, attr, ext_cu);
21400 }
21401
21402 static void
21403 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21404 {
21405 unsigned int i;
21406
21407 print_spaces (indent, f);
21408 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21409 dwarf_tag_name (die->tag), die->abbrev,
21410 sect_offset_str (die->sect_off));
21411
21412 if (die->parent != NULL)
21413 {
21414 print_spaces (indent, f);
21415 fprintf_unfiltered (f, " parent at offset: %s\n",
21416 sect_offset_str (die->parent->sect_off));
21417 }
21418
21419 print_spaces (indent, f);
21420 fprintf_unfiltered (f, " has children: %s\n",
21421 dwarf_bool_name (die->child != NULL));
21422
21423 print_spaces (indent, f);
21424 fprintf_unfiltered (f, " attributes:\n");
21425
21426 for (i = 0; i < die->num_attrs; ++i)
21427 {
21428 print_spaces (indent, f);
21429 fprintf_unfiltered (f, " %s (%s) ",
21430 dwarf_attr_name (die->attrs[i].name),
21431 dwarf_form_name (die->attrs[i].form));
21432
21433 switch (die->attrs[i].form)
21434 {
21435 case DW_FORM_addr:
21436 case DW_FORM_addrx:
21437 case DW_FORM_GNU_addr_index:
21438 fprintf_unfiltered (f, "address: ");
21439 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21440 break;
21441 case DW_FORM_block2:
21442 case DW_FORM_block4:
21443 case DW_FORM_block:
21444 case DW_FORM_block1:
21445 fprintf_unfiltered (f, "block: size %s",
21446 pulongest (DW_BLOCK (&die->attrs[i])->size));
21447 break;
21448 case DW_FORM_exprloc:
21449 fprintf_unfiltered (f, "expression: size %s",
21450 pulongest (DW_BLOCK (&die->attrs[i])->size));
21451 break;
21452 case DW_FORM_data16:
21453 fprintf_unfiltered (f, "constant of 16 bytes");
21454 break;
21455 case DW_FORM_ref_addr:
21456 fprintf_unfiltered (f, "ref address: ");
21457 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21458 break;
21459 case DW_FORM_GNU_ref_alt:
21460 fprintf_unfiltered (f, "alt ref address: ");
21461 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21462 break;
21463 case DW_FORM_ref1:
21464 case DW_FORM_ref2:
21465 case DW_FORM_ref4:
21466 case DW_FORM_ref8:
21467 case DW_FORM_ref_udata:
21468 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21469 (long) (DW_UNSND (&die->attrs[i])));
21470 break;
21471 case DW_FORM_data1:
21472 case DW_FORM_data2:
21473 case DW_FORM_data4:
21474 case DW_FORM_data8:
21475 case DW_FORM_udata:
21476 case DW_FORM_sdata:
21477 fprintf_unfiltered (f, "constant: %s",
21478 pulongest (DW_UNSND (&die->attrs[i])));
21479 break;
21480 case DW_FORM_sec_offset:
21481 fprintf_unfiltered (f, "section offset: %s",
21482 pulongest (DW_UNSND (&die->attrs[i])));
21483 break;
21484 case DW_FORM_ref_sig8:
21485 fprintf_unfiltered (f, "signature: %s",
21486 hex_string (DW_SIGNATURE (&die->attrs[i])));
21487 break;
21488 case DW_FORM_string:
21489 case DW_FORM_strp:
21490 case DW_FORM_line_strp:
21491 case DW_FORM_strx:
21492 case DW_FORM_GNU_str_index:
21493 case DW_FORM_GNU_strp_alt:
21494 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21495 DW_STRING (&die->attrs[i])
21496 ? DW_STRING (&die->attrs[i]) : "",
21497 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21498 break;
21499 case DW_FORM_flag:
21500 if (DW_UNSND (&die->attrs[i]))
21501 fprintf_unfiltered (f, "flag: TRUE");
21502 else
21503 fprintf_unfiltered (f, "flag: FALSE");
21504 break;
21505 case DW_FORM_flag_present:
21506 fprintf_unfiltered (f, "flag: TRUE");
21507 break;
21508 case DW_FORM_indirect:
21509 /* The reader will have reduced the indirect form to
21510 the "base form" so this form should not occur. */
21511 fprintf_unfiltered (f,
21512 "unexpected attribute form: DW_FORM_indirect");
21513 break;
21514 case DW_FORM_implicit_const:
21515 fprintf_unfiltered (f, "constant: %s",
21516 plongest (DW_SND (&die->attrs[i])));
21517 break;
21518 default:
21519 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21520 die->attrs[i].form);
21521 break;
21522 }
21523 fprintf_unfiltered (f, "\n");
21524 }
21525 }
21526
21527 static void
21528 dump_die_for_error (struct die_info *die)
21529 {
21530 dump_die_shallow (gdb_stderr, 0, die);
21531 }
21532
21533 static void
21534 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21535 {
21536 int indent = level * 4;
21537
21538 gdb_assert (die != NULL);
21539
21540 if (level >= max_level)
21541 return;
21542
21543 dump_die_shallow (f, indent, die);
21544
21545 if (die->child != NULL)
21546 {
21547 print_spaces (indent, f);
21548 fprintf_unfiltered (f, " Children:");
21549 if (level + 1 < max_level)
21550 {
21551 fprintf_unfiltered (f, "\n");
21552 dump_die_1 (f, level + 1, max_level, die->child);
21553 }
21554 else
21555 {
21556 fprintf_unfiltered (f,
21557 " [not printed, max nesting level reached]\n");
21558 }
21559 }
21560
21561 if (die->sibling != NULL && level > 0)
21562 {
21563 dump_die_1 (f, level, max_level, die->sibling);
21564 }
21565 }
21566
21567 /* This is called from the pdie macro in gdbinit.in.
21568 It's not static so gcc will keep a copy callable from gdb. */
21569
21570 void
21571 dump_die (struct die_info *die, int max_level)
21572 {
21573 dump_die_1 (gdb_stdlog, 0, max_level, die);
21574 }
21575
21576 static void
21577 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21578 {
21579 void **slot;
21580
21581 slot = htab_find_slot_with_hash (cu->die_hash, die,
21582 to_underlying (die->sect_off),
21583 INSERT);
21584
21585 *slot = die;
21586 }
21587
21588 /* Follow reference or signature attribute ATTR of SRC_DIE.
21589 On entry *REF_CU is the CU of SRC_DIE.
21590 On exit *REF_CU is the CU of the result. */
21591
21592 static struct die_info *
21593 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21594 struct dwarf2_cu **ref_cu)
21595 {
21596 struct die_info *die;
21597
21598 if (attr->form_is_ref ())
21599 die = follow_die_ref (src_die, attr, ref_cu);
21600 else if (attr->form == DW_FORM_ref_sig8)
21601 die = follow_die_sig (src_die, attr, ref_cu);
21602 else
21603 {
21604 dump_die_for_error (src_die);
21605 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21606 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
21607 }
21608
21609 return die;
21610 }
21611
21612 /* Follow reference OFFSET.
21613 On entry *REF_CU is the CU of the source die referencing OFFSET.
21614 On exit *REF_CU is the CU of the result.
21615 Returns NULL if OFFSET is invalid. */
21616
21617 static struct die_info *
21618 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21619 struct dwarf2_cu **ref_cu)
21620 {
21621 struct die_info temp_die;
21622 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21623 struct dwarf2_per_objfile *dwarf2_per_objfile
21624 = cu->per_cu->dwarf2_per_objfile;
21625
21626 gdb_assert (cu->per_cu != NULL);
21627
21628 target_cu = cu;
21629
21630 if (cu->per_cu->is_debug_types)
21631 {
21632 /* .debug_types CUs cannot reference anything outside their CU.
21633 If they need to, they have to reference a signatured type via
21634 DW_FORM_ref_sig8. */
21635 if (!cu->header.offset_in_cu_p (sect_off))
21636 return NULL;
21637 }
21638 else if (offset_in_dwz != cu->per_cu->is_dwz
21639 || !cu->header.offset_in_cu_p (sect_off))
21640 {
21641 struct dwarf2_per_cu_data *per_cu;
21642
21643 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21644 dwarf2_per_objfile);
21645
21646 /* If necessary, add it to the queue and load its DIEs. */
21647 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21648 load_full_comp_unit (per_cu, false, cu->language);
21649
21650 target_cu = per_cu->cu;
21651 }
21652 else if (cu->dies == NULL)
21653 {
21654 /* We're loading full DIEs during partial symbol reading. */
21655 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21656 load_full_comp_unit (cu->per_cu, false, language_minimal);
21657 }
21658
21659 *ref_cu = target_cu;
21660 temp_die.sect_off = sect_off;
21661
21662 if (target_cu != cu)
21663 target_cu->ancestor = cu;
21664
21665 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21666 &temp_die,
21667 to_underlying (sect_off));
21668 }
21669
21670 /* Follow reference attribute ATTR of SRC_DIE.
21671 On entry *REF_CU is the CU of SRC_DIE.
21672 On exit *REF_CU is the CU of the result. */
21673
21674 static struct die_info *
21675 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21676 struct dwarf2_cu **ref_cu)
21677 {
21678 sect_offset sect_off = attr->get_ref_die_offset ();
21679 struct dwarf2_cu *cu = *ref_cu;
21680 struct die_info *die;
21681
21682 die = follow_die_offset (sect_off,
21683 (attr->form == DW_FORM_GNU_ref_alt
21684 || cu->per_cu->is_dwz),
21685 ref_cu);
21686 if (!die)
21687 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21688 "at %s [in module %s]"),
21689 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
21690 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
21691
21692 return die;
21693 }
21694
21695 /* See read.h. */
21696
21697 struct dwarf2_locexpr_baton
21698 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21699 dwarf2_per_cu_data *per_cu,
21700 CORE_ADDR (*get_frame_pc) (void *baton),
21701 void *baton, bool resolve_abstract_p)
21702 {
21703 struct dwarf2_cu *cu;
21704 struct die_info *die;
21705 struct attribute *attr;
21706 struct dwarf2_locexpr_baton retval;
21707 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21708 struct objfile *objfile = dwarf2_per_objfile->objfile;
21709
21710 if (per_cu->cu == NULL)
21711 load_cu (per_cu, false);
21712 cu = per_cu->cu;
21713 if (cu == NULL)
21714 {
21715 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21716 Instead just throw an error, not much else we can do. */
21717 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21718 sect_offset_str (sect_off), objfile_name (objfile));
21719 }
21720
21721 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21722 if (!die)
21723 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21724 sect_offset_str (sect_off), objfile_name (objfile));
21725
21726 attr = dwarf2_attr (die, DW_AT_location, cu);
21727 if (!attr && resolve_abstract_p
21728 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
21729 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21730 {
21731 CORE_ADDR pc = (*get_frame_pc) (baton);
21732 CORE_ADDR baseaddr = objfile->text_section_offset ();
21733 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21734
21735 for (const auto &cand_off
21736 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
21737 {
21738 struct dwarf2_cu *cand_cu = cu;
21739 struct die_info *cand
21740 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21741 if (!cand
21742 || !cand->parent
21743 || cand->parent->tag != DW_TAG_subprogram)
21744 continue;
21745
21746 CORE_ADDR pc_low, pc_high;
21747 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
21748 if (pc_low == ((CORE_ADDR) -1))
21749 continue;
21750 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21751 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21752 if (!(pc_low <= pc && pc < pc_high))
21753 continue;
21754
21755 die = cand;
21756 attr = dwarf2_attr (die, DW_AT_location, cu);
21757 break;
21758 }
21759 }
21760
21761 if (!attr)
21762 {
21763 /* DWARF: "If there is no such attribute, then there is no effect.".
21764 DATA is ignored if SIZE is 0. */
21765
21766 retval.data = NULL;
21767 retval.size = 0;
21768 }
21769 else if (attr->form_is_section_offset ())
21770 {
21771 struct dwarf2_loclist_baton loclist_baton;
21772 CORE_ADDR pc = (*get_frame_pc) (baton);
21773 size_t size;
21774
21775 fill_in_loclist_baton (cu, &loclist_baton, attr);
21776
21777 retval.data = dwarf2_find_location_expression (&loclist_baton,
21778 &size, pc);
21779 retval.size = size;
21780 }
21781 else
21782 {
21783 if (!attr->form_is_block ())
21784 error (_("Dwarf Error: DIE at %s referenced in module %s "
21785 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21786 sect_offset_str (sect_off), objfile_name (objfile));
21787
21788 retval.data = DW_BLOCK (attr)->data;
21789 retval.size = DW_BLOCK (attr)->size;
21790 }
21791 retval.per_cu = cu->per_cu;
21792
21793 age_cached_comp_units (dwarf2_per_objfile);
21794
21795 return retval;
21796 }
21797
21798 /* See read.h. */
21799
21800 struct dwarf2_locexpr_baton
21801 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21802 dwarf2_per_cu_data *per_cu,
21803 CORE_ADDR (*get_frame_pc) (void *baton),
21804 void *baton)
21805 {
21806 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21807
21808 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21809 }
21810
21811 /* Write a constant of a given type as target-ordered bytes into
21812 OBSTACK. */
21813
21814 static const gdb_byte *
21815 write_constant_as_bytes (struct obstack *obstack,
21816 enum bfd_endian byte_order,
21817 struct type *type,
21818 ULONGEST value,
21819 LONGEST *len)
21820 {
21821 gdb_byte *result;
21822
21823 *len = TYPE_LENGTH (type);
21824 result = (gdb_byte *) obstack_alloc (obstack, *len);
21825 store_unsigned_integer (result, *len, byte_order, value);
21826
21827 return result;
21828 }
21829
21830 /* See read.h. */
21831
21832 const gdb_byte *
21833 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21834 dwarf2_per_cu_data *per_cu,
21835 obstack *obstack,
21836 LONGEST *len)
21837 {
21838 struct dwarf2_cu *cu;
21839 struct die_info *die;
21840 struct attribute *attr;
21841 const gdb_byte *result = NULL;
21842 struct type *type;
21843 LONGEST value;
21844 enum bfd_endian byte_order;
21845 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
21846
21847 if (per_cu->cu == NULL)
21848 load_cu (per_cu, false);
21849 cu = per_cu->cu;
21850 if (cu == NULL)
21851 {
21852 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21853 Instead just throw an error, not much else we can do. */
21854 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21855 sect_offset_str (sect_off), objfile_name (objfile));
21856 }
21857
21858 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21859 if (!die)
21860 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21861 sect_offset_str (sect_off), objfile_name (objfile));
21862
21863 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21864 if (attr == NULL)
21865 return NULL;
21866
21867 byte_order = (bfd_big_endian (objfile->obfd)
21868 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21869
21870 switch (attr->form)
21871 {
21872 case DW_FORM_addr:
21873 case DW_FORM_addrx:
21874 case DW_FORM_GNU_addr_index:
21875 {
21876 gdb_byte *tem;
21877
21878 *len = cu->header.addr_size;
21879 tem = (gdb_byte *) obstack_alloc (obstack, *len);
21880 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21881 result = tem;
21882 }
21883 break;
21884 case DW_FORM_string:
21885 case DW_FORM_strp:
21886 case DW_FORM_strx:
21887 case DW_FORM_GNU_str_index:
21888 case DW_FORM_GNU_strp_alt:
21889 /* DW_STRING is already allocated on the objfile obstack, point
21890 directly to it. */
21891 result = (const gdb_byte *) DW_STRING (attr);
21892 *len = strlen (DW_STRING (attr));
21893 break;
21894 case DW_FORM_block1:
21895 case DW_FORM_block2:
21896 case DW_FORM_block4:
21897 case DW_FORM_block:
21898 case DW_FORM_exprloc:
21899 case DW_FORM_data16:
21900 result = DW_BLOCK (attr)->data;
21901 *len = DW_BLOCK (attr)->size;
21902 break;
21903
21904 /* The DW_AT_const_value attributes are supposed to carry the
21905 symbol's value "represented as it would be on the target
21906 architecture." By the time we get here, it's already been
21907 converted to host endianness, so we just need to sign- or
21908 zero-extend it as appropriate. */
21909 case DW_FORM_data1:
21910 type = die_type (die, cu);
21911 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21912 if (result == NULL)
21913 result = write_constant_as_bytes (obstack, byte_order,
21914 type, value, len);
21915 break;
21916 case DW_FORM_data2:
21917 type = die_type (die, cu);
21918 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21919 if (result == NULL)
21920 result = write_constant_as_bytes (obstack, byte_order,
21921 type, value, len);
21922 break;
21923 case DW_FORM_data4:
21924 type = die_type (die, cu);
21925 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21926 if (result == NULL)
21927 result = write_constant_as_bytes (obstack, byte_order,
21928 type, value, len);
21929 break;
21930 case DW_FORM_data8:
21931 type = die_type (die, cu);
21932 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21933 if (result == NULL)
21934 result = write_constant_as_bytes (obstack, byte_order,
21935 type, value, len);
21936 break;
21937
21938 case DW_FORM_sdata:
21939 case DW_FORM_implicit_const:
21940 type = die_type (die, cu);
21941 result = write_constant_as_bytes (obstack, byte_order,
21942 type, DW_SND (attr), len);
21943 break;
21944
21945 case DW_FORM_udata:
21946 type = die_type (die, cu);
21947 result = write_constant_as_bytes (obstack, byte_order,
21948 type, DW_UNSND (attr), len);
21949 break;
21950
21951 default:
21952 complaint (_("unsupported const value attribute form: '%s'"),
21953 dwarf_form_name (attr->form));
21954 break;
21955 }
21956
21957 return result;
21958 }
21959
21960 /* See read.h. */
21961
21962 struct type *
21963 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
21964 dwarf2_per_cu_data *per_cu)
21965 {
21966 struct dwarf2_cu *cu;
21967 struct die_info *die;
21968
21969 if (per_cu->cu == NULL)
21970 load_cu (per_cu, false);
21971 cu = per_cu->cu;
21972 if (!cu)
21973 return NULL;
21974
21975 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21976 if (!die)
21977 return NULL;
21978
21979 return die_type (die, cu);
21980 }
21981
21982 /* See read.h. */
21983
21984 struct type *
21985 dwarf2_get_die_type (cu_offset die_offset,
21986 struct dwarf2_per_cu_data *per_cu)
21987 {
21988 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
21989 return get_die_type_at_offset (die_offset_sect, per_cu);
21990 }
21991
21992 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
21993 On entry *REF_CU is the CU of SRC_DIE.
21994 On exit *REF_CU is the CU of the result.
21995 Returns NULL if the referenced DIE isn't found. */
21996
21997 static struct die_info *
21998 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21999 struct dwarf2_cu **ref_cu)
22000 {
22001 struct die_info temp_die;
22002 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22003 struct die_info *die;
22004
22005 /* While it might be nice to assert sig_type->type == NULL here,
22006 we can get here for DW_AT_imported_declaration where we need
22007 the DIE not the type. */
22008
22009 /* If necessary, add it to the queue and load its DIEs. */
22010
22011 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22012 read_signatured_type (sig_type);
22013
22014 sig_cu = sig_type->per_cu.cu;
22015 gdb_assert (sig_cu != NULL);
22016 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22017 temp_die.sect_off = sig_type->type_offset_in_section;
22018 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22019 to_underlying (temp_die.sect_off));
22020 if (die)
22021 {
22022 struct dwarf2_per_objfile *dwarf2_per_objfile
22023 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22024
22025 /* For .gdb_index version 7 keep track of included TUs.
22026 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22027 if (dwarf2_per_objfile->index_table != NULL
22028 && dwarf2_per_objfile->index_table->version <= 7)
22029 {
22030 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22031 }
22032
22033 *ref_cu = sig_cu;
22034 if (sig_cu != cu)
22035 sig_cu->ancestor = cu;
22036
22037 return die;
22038 }
22039
22040 return NULL;
22041 }
22042
22043 /* Follow signatured type referenced by ATTR in SRC_DIE.
22044 On entry *REF_CU is the CU of SRC_DIE.
22045 On exit *REF_CU is the CU of the result.
22046 The result is the DIE of the type.
22047 If the referenced type cannot be found an error is thrown. */
22048
22049 static struct die_info *
22050 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22051 struct dwarf2_cu **ref_cu)
22052 {
22053 ULONGEST signature = DW_SIGNATURE (attr);
22054 struct signatured_type *sig_type;
22055 struct die_info *die;
22056
22057 gdb_assert (attr->form == DW_FORM_ref_sig8);
22058
22059 sig_type = lookup_signatured_type (*ref_cu, signature);
22060 /* sig_type will be NULL if the signatured type is missing from
22061 the debug info. */
22062 if (sig_type == NULL)
22063 {
22064 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22065 " from DIE at %s [in module %s]"),
22066 hex_string (signature), sect_offset_str (src_die->sect_off),
22067 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22068 }
22069
22070 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22071 if (die == NULL)
22072 {
22073 dump_die_for_error (src_die);
22074 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22075 " from DIE at %s [in module %s]"),
22076 hex_string (signature), sect_offset_str (src_die->sect_off),
22077 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22078 }
22079
22080 return die;
22081 }
22082
22083 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22084 reading in and processing the type unit if necessary. */
22085
22086 static struct type *
22087 get_signatured_type (struct die_info *die, ULONGEST signature,
22088 struct dwarf2_cu *cu)
22089 {
22090 struct dwarf2_per_objfile *dwarf2_per_objfile
22091 = cu->per_cu->dwarf2_per_objfile;
22092 struct signatured_type *sig_type;
22093 struct dwarf2_cu *type_cu;
22094 struct die_info *type_die;
22095 struct type *type;
22096
22097 sig_type = lookup_signatured_type (cu, signature);
22098 /* sig_type will be NULL if the signatured type is missing from
22099 the debug info. */
22100 if (sig_type == NULL)
22101 {
22102 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22103 " from DIE at %s [in module %s]"),
22104 hex_string (signature), sect_offset_str (die->sect_off),
22105 objfile_name (dwarf2_per_objfile->objfile));
22106 return build_error_marker_type (cu, die);
22107 }
22108
22109 /* If we already know the type we're done. */
22110 if (sig_type->type != NULL)
22111 return sig_type->type;
22112
22113 type_cu = cu;
22114 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22115 if (type_die != NULL)
22116 {
22117 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22118 is created. This is important, for example, because for c++ classes
22119 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22120 type = read_type_die (type_die, type_cu);
22121 if (type == NULL)
22122 {
22123 complaint (_("Dwarf Error: Cannot build signatured type %s"
22124 " referenced from DIE at %s [in module %s]"),
22125 hex_string (signature), sect_offset_str (die->sect_off),
22126 objfile_name (dwarf2_per_objfile->objfile));
22127 type = build_error_marker_type (cu, die);
22128 }
22129 }
22130 else
22131 {
22132 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22133 " from DIE at %s [in module %s]"),
22134 hex_string (signature), sect_offset_str (die->sect_off),
22135 objfile_name (dwarf2_per_objfile->objfile));
22136 type = build_error_marker_type (cu, die);
22137 }
22138 sig_type->type = type;
22139
22140 return type;
22141 }
22142
22143 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22144 reading in and processing the type unit if necessary. */
22145
22146 static struct type *
22147 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22148 struct dwarf2_cu *cu) /* ARI: editCase function */
22149 {
22150 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22151 if (attr->form_is_ref ())
22152 {
22153 struct dwarf2_cu *type_cu = cu;
22154 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22155
22156 return read_type_die (type_die, type_cu);
22157 }
22158 else if (attr->form == DW_FORM_ref_sig8)
22159 {
22160 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22161 }
22162 else
22163 {
22164 struct dwarf2_per_objfile *dwarf2_per_objfile
22165 = cu->per_cu->dwarf2_per_objfile;
22166
22167 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22168 " at %s [in module %s]"),
22169 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22170 objfile_name (dwarf2_per_objfile->objfile));
22171 return build_error_marker_type (cu, die);
22172 }
22173 }
22174
22175 /* Load the DIEs associated with type unit PER_CU into memory. */
22176
22177 static void
22178 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22179 {
22180 struct signatured_type *sig_type;
22181
22182 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22183 gdb_assert (! per_cu->type_unit_group_p ());
22184
22185 /* We have the per_cu, but we need the signatured_type.
22186 Fortunately this is an easy translation. */
22187 gdb_assert (per_cu->is_debug_types);
22188 sig_type = (struct signatured_type *) per_cu;
22189
22190 gdb_assert (per_cu->cu == NULL);
22191
22192 read_signatured_type (sig_type);
22193
22194 gdb_assert (per_cu->cu != NULL);
22195 }
22196
22197 /* Read in a signatured type and build its CU and DIEs.
22198 If the type is a stub for the real type in a DWO file,
22199 read in the real type from the DWO file as well. */
22200
22201 static void
22202 read_signatured_type (struct signatured_type *sig_type)
22203 {
22204 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22205
22206 gdb_assert (per_cu->is_debug_types);
22207 gdb_assert (per_cu->cu == NULL);
22208
22209 cutu_reader reader (per_cu, NULL, 0, false);
22210
22211 if (!reader.dummy_p)
22212 {
22213 struct dwarf2_cu *cu = reader.cu;
22214 const gdb_byte *info_ptr = reader.info_ptr;
22215
22216 gdb_assert (cu->die_hash == NULL);
22217 cu->die_hash =
22218 htab_create_alloc_ex (cu->header.length / 12,
22219 die_hash,
22220 die_eq,
22221 NULL,
22222 &cu->comp_unit_obstack,
22223 hashtab_obstack_allocate,
22224 dummy_obstack_deallocate);
22225
22226 if (reader.comp_unit_die->has_children)
22227 reader.comp_unit_die->child
22228 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22229 reader.comp_unit_die);
22230 cu->dies = reader.comp_unit_die;
22231 /* comp_unit_die is not stored in die_hash, no need. */
22232
22233 /* We try not to read any attributes in this function, because
22234 not all CUs needed for references have been loaded yet, and
22235 symbol table processing isn't initialized. But we have to
22236 set the CU language, or we won't be able to build types
22237 correctly. Similarly, if we do not read the producer, we can
22238 not apply producer-specific interpretation. */
22239 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22240
22241 reader.keep ();
22242 }
22243
22244 sig_type->per_cu.tu_read = 1;
22245 }
22246
22247 /* Decode simple location descriptions.
22248 Given a pointer to a dwarf block that defines a location, compute
22249 the location and return the value.
22250
22251 NOTE drow/2003-11-18: This function is called in two situations
22252 now: for the address of static or global variables (partial symbols
22253 only) and for offsets into structures which are expected to be
22254 (more or less) constant. The partial symbol case should go away,
22255 and only the constant case should remain. That will let this
22256 function complain more accurately. A few special modes are allowed
22257 without complaint for global variables (for instance, global
22258 register values and thread-local values).
22259
22260 A location description containing no operations indicates that the
22261 object is optimized out. The return value is 0 for that case.
22262 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22263 callers will only want a very basic result and this can become a
22264 complaint.
22265
22266 Note that stack[0] is unused except as a default error return. */
22267
22268 static CORE_ADDR
22269 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22270 {
22271 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22272 size_t i;
22273 size_t size = blk->size;
22274 const gdb_byte *data = blk->data;
22275 CORE_ADDR stack[64];
22276 int stacki;
22277 unsigned int bytes_read, unsnd;
22278 gdb_byte op;
22279
22280 i = 0;
22281 stacki = 0;
22282 stack[stacki] = 0;
22283 stack[++stacki] = 0;
22284
22285 while (i < size)
22286 {
22287 op = data[i++];
22288 switch (op)
22289 {
22290 case DW_OP_lit0:
22291 case DW_OP_lit1:
22292 case DW_OP_lit2:
22293 case DW_OP_lit3:
22294 case DW_OP_lit4:
22295 case DW_OP_lit5:
22296 case DW_OP_lit6:
22297 case DW_OP_lit7:
22298 case DW_OP_lit8:
22299 case DW_OP_lit9:
22300 case DW_OP_lit10:
22301 case DW_OP_lit11:
22302 case DW_OP_lit12:
22303 case DW_OP_lit13:
22304 case DW_OP_lit14:
22305 case DW_OP_lit15:
22306 case DW_OP_lit16:
22307 case DW_OP_lit17:
22308 case DW_OP_lit18:
22309 case DW_OP_lit19:
22310 case DW_OP_lit20:
22311 case DW_OP_lit21:
22312 case DW_OP_lit22:
22313 case DW_OP_lit23:
22314 case DW_OP_lit24:
22315 case DW_OP_lit25:
22316 case DW_OP_lit26:
22317 case DW_OP_lit27:
22318 case DW_OP_lit28:
22319 case DW_OP_lit29:
22320 case DW_OP_lit30:
22321 case DW_OP_lit31:
22322 stack[++stacki] = op - DW_OP_lit0;
22323 break;
22324
22325 case DW_OP_reg0:
22326 case DW_OP_reg1:
22327 case DW_OP_reg2:
22328 case DW_OP_reg3:
22329 case DW_OP_reg4:
22330 case DW_OP_reg5:
22331 case DW_OP_reg6:
22332 case DW_OP_reg7:
22333 case DW_OP_reg8:
22334 case DW_OP_reg9:
22335 case DW_OP_reg10:
22336 case DW_OP_reg11:
22337 case DW_OP_reg12:
22338 case DW_OP_reg13:
22339 case DW_OP_reg14:
22340 case DW_OP_reg15:
22341 case DW_OP_reg16:
22342 case DW_OP_reg17:
22343 case DW_OP_reg18:
22344 case DW_OP_reg19:
22345 case DW_OP_reg20:
22346 case DW_OP_reg21:
22347 case DW_OP_reg22:
22348 case DW_OP_reg23:
22349 case DW_OP_reg24:
22350 case DW_OP_reg25:
22351 case DW_OP_reg26:
22352 case DW_OP_reg27:
22353 case DW_OP_reg28:
22354 case DW_OP_reg29:
22355 case DW_OP_reg30:
22356 case DW_OP_reg31:
22357 stack[++stacki] = op - DW_OP_reg0;
22358 if (i < size)
22359 dwarf2_complex_location_expr_complaint ();
22360 break;
22361
22362 case DW_OP_regx:
22363 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22364 i += bytes_read;
22365 stack[++stacki] = unsnd;
22366 if (i < size)
22367 dwarf2_complex_location_expr_complaint ();
22368 break;
22369
22370 case DW_OP_addr:
22371 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22372 &bytes_read);
22373 i += bytes_read;
22374 break;
22375
22376 case DW_OP_const1u:
22377 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22378 i += 1;
22379 break;
22380
22381 case DW_OP_const1s:
22382 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22383 i += 1;
22384 break;
22385
22386 case DW_OP_const2u:
22387 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22388 i += 2;
22389 break;
22390
22391 case DW_OP_const2s:
22392 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22393 i += 2;
22394 break;
22395
22396 case DW_OP_const4u:
22397 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22398 i += 4;
22399 break;
22400
22401 case DW_OP_const4s:
22402 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22403 i += 4;
22404 break;
22405
22406 case DW_OP_const8u:
22407 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22408 i += 8;
22409 break;
22410
22411 case DW_OP_constu:
22412 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22413 &bytes_read);
22414 i += bytes_read;
22415 break;
22416
22417 case DW_OP_consts:
22418 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22419 i += bytes_read;
22420 break;
22421
22422 case DW_OP_dup:
22423 stack[stacki + 1] = stack[stacki];
22424 stacki++;
22425 break;
22426
22427 case DW_OP_plus:
22428 stack[stacki - 1] += stack[stacki];
22429 stacki--;
22430 break;
22431
22432 case DW_OP_plus_uconst:
22433 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22434 &bytes_read);
22435 i += bytes_read;
22436 break;
22437
22438 case DW_OP_minus:
22439 stack[stacki - 1] -= stack[stacki];
22440 stacki--;
22441 break;
22442
22443 case DW_OP_deref:
22444 /* If we're not the last op, then we definitely can't encode
22445 this using GDB's address_class enum. This is valid for partial
22446 global symbols, although the variable's address will be bogus
22447 in the psymtab. */
22448 if (i < size)
22449 dwarf2_complex_location_expr_complaint ();
22450 break;
22451
22452 case DW_OP_GNU_push_tls_address:
22453 case DW_OP_form_tls_address:
22454 /* The top of the stack has the offset from the beginning
22455 of the thread control block at which the variable is located. */
22456 /* Nothing should follow this operator, so the top of stack would
22457 be returned. */
22458 /* This is valid for partial global symbols, but the variable's
22459 address will be bogus in the psymtab. Make it always at least
22460 non-zero to not look as a variable garbage collected by linker
22461 which have DW_OP_addr 0. */
22462 if (i < size)
22463 dwarf2_complex_location_expr_complaint ();
22464 stack[stacki]++;
22465 break;
22466
22467 case DW_OP_GNU_uninit:
22468 break;
22469
22470 case DW_OP_addrx:
22471 case DW_OP_GNU_addr_index:
22472 case DW_OP_GNU_const_index:
22473 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22474 &bytes_read);
22475 i += bytes_read;
22476 break;
22477
22478 default:
22479 {
22480 const char *name = get_DW_OP_name (op);
22481
22482 if (name)
22483 complaint (_("unsupported stack op: '%s'"),
22484 name);
22485 else
22486 complaint (_("unsupported stack op: '%02x'"),
22487 op);
22488 }
22489
22490 return (stack[stacki]);
22491 }
22492
22493 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22494 outside of the allocated space. Also enforce minimum>0. */
22495 if (stacki >= ARRAY_SIZE (stack) - 1)
22496 {
22497 complaint (_("location description stack overflow"));
22498 return 0;
22499 }
22500
22501 if (stacki <= 0)
22502 {
22503 complaint (_("location description stack underflow"));
22504 return 0;
22505 }
22506 }
22507 return (stack[stacki]);
22508 }
22509
22510 /* memory allocation interface */
22511
22512 static struct dwarf_block *
22513 dwarf_alloc_block (struct dwarf2_cu *cu)
22514 {
22515 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22516 }
22517
22518 static struct die_info *
22519 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22520 {
22521 struct die_info *die;
22522 size_t size = sizeof (struct die_info);
22523
22524 if (num_attrs > 1)
22525 size += (num_attrs - 1) * sizeof (struct attribute);
22526
22527 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22528 memset (die, 0, sizeof (struct die_info));
22529 return (die);
22530 }
22531
22532 \f
22533
22534 /* Macro support. */
22535
22536 /* An overload of dwarf_decode_macros that finds the correct section
22537 and ensures it is read in before calling the other overload. */
22538
22539 static void
22540 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22541 int section_is_gnu)
22542 {
22543 struct dwarf2_per_objfile *dwarf2_per_objfile
22544 = cu->per_cu->dwarf2_per_objfile;
22545 struct objfile *objfile = dwarf2_per_objfile->objfile;
22546 const struct line_header *lh = cu->line_header;
22547 unsigned int offset_size = cu->header.offset_size;
22548 struct dwarf2_section_info *section;
22549 const char *section_name;
22550
22551 if (cu->dwo_unit != nullptr)
22552 {
22553 if (section_is_gnu)
22554 {
22555 section = &cu->dwo_unit->dwo_file->sections.macro;
22556 section_name = ".debug_macro.dwo";
22557 }
22558 else
22559 {
22560 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22561 section_name = ".debug_macinfo.dwo";
22562 }
22563 }
22564 else
22565 {
22566 if (section_is_gnu)
22567 {
22568 section = &dwarf2_per_objfile->macro;
22569 section_name = ".debug_macro";
22570 }
22571 else
22572 {
22573 section = &dwarf2_per_objfile->macinfo;
22574 section_name = ".debug_macinfo";
22575 }
22576 }
22577
22578 section->read (objfile);
22579 if (section->buffer == nullptr)
22580 {
22581 complaint (_("missing %s section"), section_name);
22582 return;
22583 }
22584
22585 buildsym_compunit *builder = cu->get_builder ();
22586
22587 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22588 offset_size, offset, section_is_gnu);
22589 }
22590
22591 /* Return the .debug_loc section to use for CU.
22592 For DWO files use .debug_loc.dwo. */
22593
22594 static struct dwarf2_section_info *
22595 cu_debug_loc_section (struct dwarf2_cu *cu)
22596 {
22597 struct dwarf2_per_objfile *dwarf2_per_objfile
22598 = cu->per_cu->dwarf2_per_objfile;
22599
22600 if (cu->dwo_unit)
22601 {
22602 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22603
22604 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22605 }
22606 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22607 : &dwarf2_per_objfile->loc);
22608 }
22609
22610 /* A helper function that fills in a dwarf2_loclist_baton. */
22611
22612 static void
22613 fill_in_loclist_baton (struct dwarf2_cu *cu,
22614 struct dwarf2_loclist_baton *baton,
22615 const struct attribute *attr)
22616 {
22617 struct dwarf2_per_objfile *dwarf2_per_objfile
22618 = cu->per_cu->dwarf2_per_objfile;
22619 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22620
22621 section->read (dwarf2_per_objfile->objfile);
22622
22623 baton->per_cu = cu->per_cu;
22624 gdb_assert (baton->per_cu);
22625 /* We don't know how long the location list is, but make sure we
22626 don't run off the edge of the section. */
22627 baton->size = section->size - DW_UNSND (attr);
22628 baton->data = section->buffer + DW_UNSND (attr);
22629 if (cu->base_address.has_value ())
22630 baton->base_address = *cu->base_address;
22631 else
22632 baton->base_address = 0;
22633 baton->from_dwo = cu->dwo_unit != NULL;
22634 }
22635
22636 static void
22637 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22638 struct dwarf2_cu *cu, int is_block)
22639 {
22640 struct dwarf2_per_objfile *dwarf2_per_objfile
22641 = cu->per_cu->dwarf2_per_objfile;
22642 struct objfile *objfile = dwarf2_per_objfile->objfile;
22643 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22644
22645 if (attr->form_is_section_offset ()
22646 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22647 the section. If so, fall through to the complaint in the
22648 other branch. */
22649 && DW_UNSND (attr) < section->get_size (objfile))
22650 {
22651 struct dwarf2_loclist_baton *baton;
22652
22653 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22654
22655 fill_in_loclist_baton (cu, baton, attr);
22656
22657 if (!cu->base_address.has_value ())
22658 complaint (_("Location list used without "
22659 "specifying the CU base address."));
22660
22661 SYMBOL_ACLASS_INDEX (sym) = (is_block
22662 ? dwarf2_loclist_block_index
22663 : dwarf2_loclist_index);
22664 SYMBOL_LOCATION_BATON (sym) = baton;
22665 }
22666 else
22667 {
22668 struct dwarf2_locexpr_baton *baton;
22669
22670 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22671 baton->per_cu = cu->per_cu;
22672 gdb_assert (baton->per_cu);
22673
22674 if (attr->form_is_block ())
22675 {
22676 /* Note that we're just copying the block's data pointer
22677 here, not the actual data. We're still pointing into the
22678 info_buffer for SYM's objfile; right now we never release
22679 that buffer, but when we do clean up properly this may
22680 need to change. */
22681 baton->size = DW_BLOCK (attr)->size;
22682 baton->data = DW_BLOCK (attr)->data;
22683 }
22684 else
22685 {
22686 dwarf2_invalid_attrib_class_complaint ("location description",
22687 sym->natural_name ());
22688 baton->size = 0;
22689 }
22690
22691 SYMBOL_ACLASS_INDEX (sym) = (is_block
22692 ? dwarf2_locexpr_block_index
22693 : dwarf2_locexpr_index);
22694 SYMBOL_LOCATION_BATON (sym) = baton;
22695 }
22696 }
22697
22698 /* See read.h. */
22699
22700 struct objfile *
22701 dwarf2_per_cu_data::objfile () const
22702 {
22703 struct objfile *objfile = dwarf2_per_objfile->objfile;
22704
22705 /* Return the master objfile, so that we can report and look up the
22706 correct file containing this variable. */
22707 if (objfile->separate_debug_objfile_backlink)
22708 objfile = objfile->separate_debug_objfile_backlink;
22709
22710 return objfile;
22711 }
22712
22713 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22714 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22715 CU_HEADERP first. */
22716
22717 static const struct comp_unit_head *
22718 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22719 const struct dwarf2_per_cu_data *per_cu)
22720 {
22721 const gdb_byte *info_ptr;
22722
22723 if (per_cu->cu)
22724 return &per_cu->cu->header;
22725
22726 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22727
22728 memset (cu_headerp, 0, sizeof (*cu_headerp));
22729 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22730 rcuh_kind::COMPILE);
22731
22732 return cu_headerp;
22733 }
22734
22735 /* See read.h. */
22736
22737 int
22738 dwarf2_per_cu_data::addr_size () const
22739 {
22740 struct comp_unit_head cu_header_local;
22741 const struct comp_unit_head *cu_headerp;
22742
22743 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22744
22745 return cu_headerp->addr_size;
22746 }
22747
22748 /* See read.h. */
22749
22750 int
22751 dwarf2_per_cu_data::offset_size () const
22752 {
22753 struct comp_unit_head cu_header_local;
22754 const struct comp_unit_head *cu_headerp;
22755
22756 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22757
22758 return cu_headerp->offset_size;
22759 }
22760
22761 /* See read.h. */
22762
22763 int
22764 dwarf2_per_cu_data::ref_addr_size () const
22765 {
22766 struct comp_unit_head cu_header_local;
22767 const struct comp_unit_head *cu_headerp;
22768
22769 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22770
22771 if (cu_headerp->version == 2)
22772 return cu_headerp->addr_size;
22773 else
22774 return cu_headerp->offset_size;
22775 }
22776
22777 /* See read.h. */
22778
22779 CORE_ADDR
22780 dwarf2_per_cu_data::text_offset () const
22781 {
22782 struct objfile *objfile = dwarf2_per_objfile->objfile;
22783
22784 return objfile->text_section_offset ();
22785 }
22786
22787 /* See read.h. */
22788
22789 struct type *
22790 dwarf2_per_cu_data::addr_type () const
22791 {
22792 struct objfile *objfile = dwarf2_per_objfile->objfile;
22793 struct type *void_type = objfile_type (objfile)->builtin_void;
22794 struct type *addr_type = lookup_pointer_type (void_type);
22795 int addr_size = this->addr_size ();
22796
22797 if (TYPE_LENGTH (addr_type) == addr_size)
22798 return addr_type;
22799
22800 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
22801 return addr_type;
22802 }
22803
22804 /* A helper function for dwarf2_find_containing_comp_unit that returns
22805 the index of the result, and that searches a vector. It will
22806 return a result even if the offset in question does not actually
22807 occur in any CU. This is separate so that it can be unit
22808 tested. */
22809
22810 static int
22811 dwarf2_find_containing_comp_unit
22812 (sect_offset sect_off,
22813 unsigned int offset_in_dwz,
22814 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
22815 {
22816 int low, high;
22817
22818 low = 0;
22819 high = all_comp_units.size () - 1;
22820 while (high > low)
22821 {
22822 struct dwarf2_per_cu_data *mid_cu;
22823 int mid = low + (high - low) / 2;
22824
22825 mid_cu = all_comp_units[mid];
22826 if (mid_cu->is_dwz > offset_in_dwz
22827 || (mid_cu->is_dwz == offset_in_dwz
22828 && mid_cu->sect_off + mid_cu->length > sect_off))
22829 high = mid;
22830 else
22831 low = mid + 1;
22832 }
22833 gdb_assert (low == high);
22834 return low;
22835 }
22836
22837 /* Locate the .debug_info compilation unit from CU's objfile which contains
22838 the DIE at OFFSET. Raises an error on failure. */
22839
22840 static struct dwarf2_per_cu_data *
22841 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22842 unsigned int offset_in_dwz,
22843 struct dwarf2_per_objfile *dwarf2_per_objfile)
22844 {
22845 int low
22846 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22847 dwarf2_per_objfile->all_comp_units);
22848 struct dwarf2_per_cu_data *this_cu
22849 = dwarf2_per_objfile->all_comp_units[low];
22850
22851 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
22852 {
22853 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22854 error (_("Dwarf Error: could not find partial DIE containing "
22855 "offset %s [in module %s]"),
22856 sect_offset_str (sect_off),
22857 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
22858
22859 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22860 <= sect_off);
22861 return dwarf2_per_objfile->all_comp_units[low-1];
22862 }
22863 else
22864 {
22865 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
22866 && sect_off >= this_cu->sect_off + this_cu->length)
22867 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
22868 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
22869 return this_cu;
22870 }
22871 }
22872
22873 #if GDB_SELF_TEST
22874
22875 namespace selftests {
22876 namespace find_containing_comp_unit {
22877
22878 static void
22879 run_test ()
22880 {
22881 struct dwarf2_per_cu_data one {};
22882 struct dwarf2_per_cu_data two {};
22883 struct dwarf2_per_cu_data three {};
22884 struct dwarf2_per_cu_data four {};
22885
22886 one.length = 5;
22887 two.sect_off = sect_offset (one.length);
22888 two.length = 7;
22889
22890 three.length = 5;
22891 three.is_dwz = 1;
22892 four.sect_off = sect_offset (three.length);
22893 four.length = 7;
22894 four.is_dwz = 1;
22895
22896 std::vector<dwarf2_per_cu_data *> units;
22897 units.push_back (&one);
22898 units.push_back (&two);
22899 units.push_back (&three);
22900 units.push_back (&four);
22901
22902 int result;
22903
22904 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
22905 SELF_CHECK (units[result] == &one);
22906 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
22907 SELF_CHECK (units[result] == &one);
22908 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
22909 SELF_CHECK (units[result] == &two);
22910
22911 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
22912 SELF_CHECK (units[result] == &three);
22913 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
22914 SELF_CHECK (units[result] == &three);
22915 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
22916 SELF_CHECK (units[result] == &four);
22917 }
22918
22919 }
22920 }
22921
22922 #endif /* GDB_SELF_TEST */
22923
22924 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22925
22926 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
22927 : per_cu (per_cu_),
22928 mark (false),
22929 has_loclist (false),
22930 checked_producer (false),
22931 producer_is_gxx_lt_4_6 (false),
22932 producer_is_gcc_lt_4_3 (false),
22933 producer_is_icc (false),
22934 producer_is_icc_lt_14 (false),
22935 producer_is_codewarrior (false),
22936 processing_has_namespace_info (false)
22937 {
22938 per_cu->cu = this;
22939 }
22940
22941 /* Destroy a dwarf2_cu. */
22942
22943 dwarf2_cu::~dwarf2_cu ()
22944 {
22945 per_cu->cu = NULL;
22946 }
22947
22948 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22949
22950 static void
22951 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22952 enum language pretend_language)
22953 {
22954 struct attribute *attr;
22955
22956 /* Set the language we're debugging. */
22957 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22958 if (attr != nullptr)
22959 set_cu_language (DW_UNSND (attr), cu);
22960 else
22961 {
22962 cu->language = pretend_language;
22963 cu->language_defn = language_def (cu->language);
22964 }
22965
22966 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22967 }
22968
22969 /* Increase the age counter on each cached compilation unit, and free
22970 any that are too old. */
22971
22972 static void
22973 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
22974 {
22975 struct dwarf2_per_cu_data *per_cu, **last_chain;
22976
22977 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22978 per_cu = dwarf2_per_objfile->read_in_chain;
22979 while (per_cu != NULL)
22980 {
22981 per_cu->cu->last_used ++;
22982 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22983 dwarf2_mark (per_cu->cu);
22984 per_cu = per_cu->cu->read_in_chain;
22985 }
22986
22987 per_cu = dwarf2_per_objfile->read_in_chain;
22988 last_chain = &dwarf2_per_objfile->read_in_chain;
22989 while (per_cu != NULL)
22990 {
22991 struct dwarf2_per_cu_data *next_cu;
22992
22993 next_cu = per_cu->cu->read_in_chain;
22994
22995 if (!per_cu->cu->mark)
22996 {
22997 delete per_cu->cu;
22998 *last_chain = next_cu;
22999 }
23000 else
23001 last_chain = &per_cu->cu->read_in_chain;
23002
23003 per_cu = next_cu;
23004 }
23005 }
23006
23007 /* Remove a single compilation unit from the cache. */
23008
23009 static void
23010 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23011 {
23012 struct dwarf2_per_cu_data *per_cu, **last_chain;
23013 struct dwarf2_per_objfile *dwarf2_per_objfile
23014 = target_per_cu->dwarf2_per_objfile;
23015
23016 per_cu = dwarf2_per_objfile->read_in_chain;
23017 last_chain = &dwarf2_per_objfile->read_in_chain;
23018 while (per_cu != NULL)
23019 {
23020 struct dwarf2_per_cu_data *next_cu;
23021
23022 next_cu = per_cu->cu->read_in_chain;
23023
23024 if (per_cu == target_per_cu)
23025 {
23026 delete per_cu->cu;
23027 per_cu->cu = NULL;
23028 *last_chain = next_cu;
23029 break;
23030 }
23031 else
23032 last_chain = &per_cu->cu->read_in_chain;
23033
23034 per_cu = next_cu;
23035 }
23036 }
23037
23038 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23039 We store these in a hash table separate from the DIEs, and preserve them
23040 when the DIEs are flushed out of cache.
23041
23042 The CU "per_cu" pointer is needed because offset alone is not enough to
23043 uniquely identify the type. A file may have multiple .debug_types sections,
23044 or the type may come from a DWO file. Furthermore, while it's more logical
23045 to use per_cu->section+offset, with Fission the section with the data is in
23046 the DWO file but we don't know that section at the point we need it.
23047 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23048 because we can enter the lookup routine, get_die_type_at_offset, from
23049 outside this file, and thus won't necessarily have PER_CU->cu.
23050 Fortunately, PER_CU is stable for the life of the objfile. */
23051
23052 struct dwarf2_per_cu_offset_and_type
23053 {
23054 const struct dwarf2_per_cu_data *per_cu;
23055 sect_offset sect_off;
23056 struct type *type;
23057 };
23058
23059 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23060
23061 static hashval_t
23062 per_cu_offset_and_type_hash (const void *item)
23063 {
23064 const struct dwarf2_per_cu_offset_and_type *ofs
23065 = (const struct dwarf2_per_cu_offset_and_type *) item;
23066
23067 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23068 }
23069
23070 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23071
23072 static int
23073 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23074 {
23075 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23076 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23077 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23078 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23079
23080 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23081 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23082 }
23083
23084 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23085 table if necessary. For convenience, return TYPE.
23086
23087 The DIEs reading must have careful ordering to:
23088 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23089 reading current DIE.
23090 * Not trying to dereference contents of still incompletely read in types
23091 while reading in other DIEs.
23092 * Enable referencing still incompletely read in types just by a pointer to
23093 the type without accessing its fields.
23094
23095 Therefore caller should follow these rules:
23096 * Try to fetch any prerequisite types we may need to build this DIE type
23097 before building the type and calling set_die_type.
23098 * After building type call set_die_type for current DIE as soon as
23099 possible before fetching more types to complete the current type.
23100 * Make the type as complete as possible before fetching more types. */
23101
23102 static struct type *
23103 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23104 {
23105 struct dwarf2_per_objfile *dwarf2_per_objfile
23106 = cu->per_cu->dwarf2_per_objfile;
23107 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23108 struct objfile *objfile = dwarf2_per_objfile->objfile;
23109 struct attribute *attr;
23110 struct dynamic_prop prop;
23111
23112 /* For Ada types, make sure that the gnat-specific data is always
23113 initialized (if not already set). There are a few types where
23114 we should not be doing so, because the type-specific area is
23115 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23116 where the type-specific area is used to store the floatformat).
23117 But this is not a problem, because the gnat-specific information
23118 is actually not needed for these types. */
23119 if (need_gnat_info (cu)
23120 && TYPE_CODE (type) != TYPE_CODE_FUNC
23121 && TYPE_CODE (type) != TYPE_CODE_FLT
23122 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23123 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23124 && TYPE_CODE (type) != TYPE_CODE_METHOD
23125 && !HAVE_GNAT_AUX_INFO (type))
23126 INIT_GNAT_SPECIFIC (type);
23127
23128 /* Read DW_AT_allocated and set in type. */
23129 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23130 if (attr != NULL && attr->form_is_block ())
23131 {
23132 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23133 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23134 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23135 }
23136 else if (attr != NULL)
23137 {
23138 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23139 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23140 sect_offset_str (die->sect_off));
23141 }
23142
23143 /* Read DW_AT_associated and set in type. */
23144 attr = dwarf2_attr (die, DW_AT_associated, cu);
23145 if (attr != NULL && attr->form_is_block ())
23146 {
23147 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23148 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23149 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23150 }
23151 else if (attr != NULL)
23152 {
23153 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23154 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23155 sect_offset_str (die->sect_off));
23156 }
23157
23158 /* Read DW_AT_data_location and set in type. */
23159 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23160 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23161 cu->per_cu->addr_type ()))
23162 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23163
23164 if (dwarf2_per_objfile->die_type_hash == NULL)
23165 dwarf2_per_objfile->die_type_hash
23166 = htab_up (htab_create_alloc (127,
23167 per_cu_offset_and_type_hash,
23168 per_cu_offset_and_type_eq,
23169 NULL, xcalloc, xfree));
23170
23171 ofs.per_cu = cu->per_cu;
23172 ofs.sect_off = die->sect_off;
23173 ofs.type = type;
23174 slot = (struct dwarf2_per_cu_offset_and_type **)
23175 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23176 if (*slot)
23177 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23178 sect_offset_str (die->sect_off));
23179 *slot = XOBNEW (&objfile->objfile_obstack,
23180 struct dwarf2_per_cu_offset_and_type);
23181 **slot = ofs;
23182 return type;
23183 }
23184
23185 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23186 or return NULL if the die does not have a saved type. */
23187
23188 static struct type *
23189 get_die_type_at_offset (sect_offset sect_off,
23190 struct dwarf2_per_cu_data *per_cu)
23191 {
23192 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23193 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23194
23195 if (dwarf2_per_objfile->die_type_hash == NULL)
23196 return NULL;
23197
23198 ofs.per_cu = per_cu;
23199 ofs.sect_off = sect_off;
23200 slot = ((struct dwarf2_per_cu_offset_and_type *)
23201 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23202 if (slot)
23203 return slot->type;
23204 else
23205 return NULL;
23206 }
23207
23208 /* Look up the type for DIE in CU in die_type_hash,
23209 or return NULL if DIE does not have a saved type. */
23210
23211 static struct type *
23212 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23213 {
23214 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23215 }
23216
23217 /* Add a dependence relationship from CU to REF_PER_CU. */
23218
23219 static void
23220 dwarf2_add_dependence (struct dwarf2_cu *cu,
23221 struct dwarf2_per_cu_data *ref_per_cu)
23222 {
23223 void **slot;
23224
23225 if (cu->dependencies == NULL)
23226 cu->dependencies
23227 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23228 NULL, &cu->comp_unit_obstack,
23229 hashtab_obstack_allocate,
23230 dummy_obstack_deallocate);
23231
23232 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23233 if (*slot == NULL)
23234 *slot = ref_per_cu;
23235 }
23236
23237 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23238 Set the mark field in every compilation unit in the
23239 cache that we must keep because we are keeping CU. */
23240
23241 static int
23242 dwarf2_mark_helper (void **slot, void *data)
23243 {
23244 struct dwarf2_per_cu_data *per_cu;
23245
23246 per_cu = (struct dwarf2_per_cu_data *) *slot;
23247
23248 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23249 reading of the chain. As such dependencies remain valid it is not much
23250 useful to track and undo them during QUIT cleanups. */
23251 if (per_cu->cu == NULL)
23252 return 1;
23253
23254 if (per_cu->cu->mark)
23255 return 1;
23256 per_cu->cu->mark = true;
23257
23258 if (per_cu->cu->dependencies != NULL)
23259 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23260
23261 return 1;
23262 }
23263
23264 /* Set the mark field in CU and in every other compilation unit in the
23265 cache that we must keep because we are keeping CU. */
23266
23267 static void
23268 dwarf2_mark (struct dwarf2_cu *cu)
23269 {
23270 if (cu->mark)
23271 return;
23272 cu->mark = true;
23273 if (cu->dependencies != NULL)
23274 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23275 }
23276
23277 static void
23278 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23279 {
23280 while (per_cu)
23281 {
23282 per_cu->cu->mark = false;
23283 per_cu = per_cu->cu->read_in_chain;
23284 }
23285 }
23286
23287 /* Trivial hash function for partial_die_info: the hash value of a DIE
23288 is its offset in .debug_info for this objfile. */
23289
23290 static hashval_t
23291 partial_die_hash (const void *item)
23292 {
23293 const struct partial_die_info *part_die
23294 = (const struct partial_die_info *) item;
23295
23296 return to_underlying (part_die->sect_off);
23297 }
23298
23299 /* Trivial comparison function for partial_die_info structures: two DIEs
23300 are equal if they have the same offset. */
23301
23302 static int
23303 partial_die_eq (const void *item_lhs, const void *item_rhs)
23304 {
23305 const struct partial_die_info *part_die_lhs
23306 = (const struct partial_die_info *) item_lhs;
23307 const struct partial_die_info *part_die_rhs
23308 = (const struct partial_die_info *) item_rhs;
23309
23310 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23311 }
23312
23313 struct cmd_list_element *set_dwarf_cmdlist;
23314 struct cmd_list_element *show_dwarf_cmdlist;
23315
23316 static void
23317 set_dwarf_cmd (const char *args, int from_tty)
23318 {
23319 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23320 gdb_stdout);
23321 }
23322
23323 static void
23324 show_dwarf_cmd (const char *args, int from_tty)
23325 {
23326 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23327 }
23328
23329 static void
23330 show_check_physname (struct ui_file *file, int from_tty,
23331 struct cmd_list_element *c, const char *value)
23332 {
23333 fprintf_filtered (file,
23334 _("Whether to check \"physname\" is %s.\n"),
23335 value);
23336 }
23337
23338 void _initialize_dwarf2_read ();
23339 void
23340 _initialize_dwarf2_read ()
23341 {
23342 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23343 Set DWARF specific variables.\n\
23344 Configure DWARF variables such as the cache size."),
23345 &set_dwarf_cmdlist, "maintenance set dwarf ",
23346 0/*allow-unknown*/, &maintenance_set_cmdlist);
23347
23348 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23349 Show DWARF specific variables.\n\
23350 Show DWARF variables such as the cache size."),
23351 &show_dwarf_cmdlist, "maintenance show dwarf ",
23352 0/*allow-unknown*/, &maintenance_show_cmdlist);
23353
23354 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23355 &dwarf_max_cache_age, _("\
23356 Set the upper bound on the age of cached DWARF compilation units."), _("\
23357 Show the upper bound on the age of cached DWARF compilation units."), _("\
23358 A higher limit means that cached compilation units will be stored\n\
23359 in memory longer, and more total memory will be used. Zero disables\n\
23360 caching, which can slow down startup."),
23361 NULL,
23362 show_dwarf_max_cache_age,
23363 &set_dwarf_cmdlist,
23364 &show_dwarf_cmdlist);
23365
23366 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23367 Set debugging of the DWARF reader."), _("\
23368 Show debugging of the DWARF reader."), _("\
23369 When enabled (non-zero), debugging messages are printed during DWARF\n\
23370 reading and symtab expansion. A value of 1 (one) provides basic\n\
23371 information. A value greater than 1 provides more verbose information."),
23372 NULL,
23373 NULL,
23374 &setdebuglist, &showdebuglist);
23375
23376 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23377 Set debugging of the DWARF DIE reader."), _("\
23378 Show debugging of the DWARF DIE reader."), _("\
23379 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23380 The value is the maximum depth to print."),
23381 NULL,
23382 NULL,
23383 &setdebuglist, &showdebuglist);
23384
23385 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23386 Set debugging of the dwarf line reader."), _("\
23387 Show debugging of the dwarf line reader."), _("\
23388 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23389 A value of 1 (one) provides basic information.\n\
23390 A value greater than 1 provides more verbose information."),
23391 NULL,
23392 NULL,
23393 &setdebuglist, &showdebuglist);
23394
23395 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23396 Set cross-checking of \"physname\" code against demangler."), _("\
23397 Show cross-checking of \"physname\" code against demangler."), _("\
23398 When enabled, GDB's internal \"physname\" code is checked against\n\
23399 the demangler."),
23400 NULL, show_check_physname,
23401 &setdebuglist, &showdebuglist);
23402
23403 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23404 no_class, &use_deprecated_index_sections, _("\
23405 Set whether to use deprecated gdb_index sections."), _("\
23406 Show whether to use deprecated gdb_index sections."), _("\
23407 When enabled, deprecated .gdb_index sections are used anyway.\n\
23408 Normally they are ignored either because of a missing feature or\n\
23409 performance issue.\n\
23410 Warning: This option must be enabled before gdb reads the file."),
23411 NULL,
23412 NULL,
23413 &setlist, &showlist);
23414
23415 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23416 &dwarf2_locexpr_funcs);
23417 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23418 &dwarf2_loclist_funcs);
23419
23420 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23421 &dwarf2_block_frame_base_locexpr_funcs);
23422 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23423 &dwarf2_block_frame_base_loclist_funcs);
23424
23425 #if GDB_SELF_TEST
23426 selftests::register_test ("dw2_expand_symtabs_matching",
23427 selftests::dw2_expand_symtabs_matching::run_test);
23428 selftests::register_test ("dwarf2_find_containing_comp_unit",
23429 selftests::find_containing_comp_unit::run_test);
23430 #endif
23431 }
This page took 0.480215 seconds and 5 git commands to generate.