[gdb] Use partial symbol table to find language for main
[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 /* Currently unimplemented; used for Ada. The function can be called if the
3596 current language is Ada for a non-Ada objfile using GNU index. As Ada
3597 does not look for non-Ada symbols this function should just return. */
3598 }
3599
3600 /* Starting from a search name, return the string that finds the upper
3601 bound of all strings that start with SEARCH_NAME in a sorted name
3602 list. Returns the empty string to indicate that the upper bound is
3603 the end of the list. */
3604
3605 static std::string
3606 make_sort_after_prefix_name (const char *search_name)
3607 {
3608 /* When looking to complete "func", we find the upper bound of all
3609 symbols that start with "func" by looking for where we'd insert
3610 the closest string that would follow "func" in lexicographical
3611 order. Usually, that's "func"-with-last-character-incremented,
3612 i.e. "fund". Mind non-ASCII characters, though. Usually those
3613 will be UTF-8 multi-byte sequences, but we can't be certain.
3614 Especially mind the 0xff character, which is a valid character in
3615 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3616 rule out compilers allowing it in identifiers. Note that
3617 conveniently, strcmp/strcasecmp are specified to compare
3618 characters interpreted as unsigned char. So what we do is treat
3619 the whole string as a base 256 number composed of a sequence of
3620 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3621 to 0, and carries 1 to the following more-significant position.
3622 If the very first character in SEARCH_NAME ends up incremented
3623 and carries/overflows, then the upper bound is the end of the
3624 list. The string after the empty string is also the empty
3625 string.
3626
3627 Some examples of this operation:
3628
3629 SEARCH_NAME => "+1" RESULT
3630
3631 "abc" => "abd"
3632 "ab\xff" => "ac"
3633 "\xff" "a" "\xff" => "\xff" "b"
3634 "\xff" => ""
3635 "\xff\xff" => ""
3636 "" => ""
3637
3638 Then, with these symbols for example:
3639
3640 func
3641 func1
3642 fund
3643
3644 completing "func" looks for symbols between "func" and
3645 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3646 which finds "func" and "func1", but not "fund".
3647
3648 And with:
3649
3650 funcÿ (Latin1 'ÿ' [0xff])
3651 funcÿ1
3652 fund
3653
3654 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3655 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3656
3657 And with:
3658
3659 ÿÿ (Latin1 'ÿ' [0xff])
3660 ÿÿ1
3661
3662 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3663 the end of the list.
3664 */
3665 std::string after = search_name;
3666 while (!after.empty () && (unsigned char) after.back () == 0xff)
3667 after.pop_back ();
3668 if (!after.empty ())
3669 after.back () = (unsigned char) after.back () + 1;
3670 return after;
3671 }
3672
3673 /* See declaration. */
3674
3675 std::pair<std::vector<name_component>::const_iterator,
3676 std::vector<name_component>::const_iterator>
3677 mapped_index_base::find_name_components_bounds
3678 (const lookup_name_info &lookup_name_without_params, language lang) const
3679 {
3680 auto *name_cmp
3681 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3682
3683 const char *lang_name
3684 = lookup_name_without_params.language_lookup_name (lang);
3685
3686 /* Comparison function object for lower_bound that matches against a
3687 given symbol name. */
3688 auto lookup_compare_lower = [&] (const name_component &elem,
3689 const char *name)
3690 {
3691 const char *elem_qualified = this->symbol_name_at (elem.idx);
3692 const char *elem_name = elem_qualified + elem.name_offset;
3693 return name_cmp (elem_name, name) < 0;
3694 };
3695
3696 /* Comparison function object for upper_bound that matches against a
3697 given symbol name. */
3698 auto lookup_compare_upper = [&] (const char *name,
3699 const name_component &elem)
3700 {
3701 const char *elem_qualified = this->symbol_name_at (elem.idx);
3702 const char *elem_name = elem_qualified + elem.name_offset;
3703 return name_cmp (name, elem_name) < 0;
3704 };
3705
3706 auto begin = this->name_components.begin ();
3707 auto end = this->name_components.end ();
3708
3709 /* Find the lower bound. */
3710 auto lower = [&] ()
3711 {
3712 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3713 return begin;
3714 else
3715 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3716 } ();
3717
3718 /* Find the upper bound. */
3719 auto upper = [&] ()
3720 {
3721 if (lookup_name_without_params.completion_mode ())
3722 {
3723 /* In completion mode, we want UPPER to point past all
3724 symbols names that have the same prefix. I.e., with
3725 these symbols, and completing "func":
3726
3727 function << lower bound
3728 function1
3729 other_function << upper bound
3730
3731 We find the upper bound by looking for the insertion
3732 point of "func"-with-last-character-incremented,
3733 i.e. "fund". */
3734 std::string after = make_sort_after_prefix_name (lang_name);
3735 if (after.empty ())
3736 return end;
3737 return std::lower_bound (lower, end, after.c_str (),
3738 lookup_compare_lower);
3739 }
3740 else
3741 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3742 } ();
3743
3744 return {lower, upper};
3745 }
3746
3747 /* See declaration. */
3748
3749 void
3750 mapped_index_base::build_name_components ()
3751 {
3752 if (!this->name_components.empty ())
3753 return;
3754
3755 this->name_components_casing = case_sensitivity;
3756 auto *name_cmp
3757 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3758
3759 /* The code below only knows how to break apart components of C++
3760 symbol names (and other languages that use '::' as
3761 namespace/module separator) and Ada symbol names. */
3762 auto count = this->symbol_name_count ();
3763 for (offset_type idx = 0; idx < count; idx++)
3764 {
3765 if (this->symbol_name_slot_invalid (idx))
3766 continue;
3767
3768 const char *name = this->symbol_name_at (idx);
3769
3770 /* Add each name component to the name component table. */
3771 unsigned int previous_len = 0;
3772
3773 if (strstr (name, "::") != nullptr)
3774 {
3775 for (unsigned int current_len = cp_find_first_component (name);
3776 name[current_len] != '\0';
3777 current_len += cp_find_first_component (name + current_len))
3778 {
3779 gdb_assert (name[current_len] == ':');
3780 this->name_components.push_back ({previous_len, idx});
3781 /* Skip the '::'. */
3782 current_len += 2;
3783 previous_len = current_len;
3784 }
3785 }
3786 else
3787 {
3788 /* Handle the Ada encoded (aka mangled) form here. */
3789 for (const char *iter = strstr (name, "__");
3790 iter != nullptr;
3791 iter = strstr (iter, "__"))
3792 {
3793 this->name_components.push_back ({previous_len, idx});
3794 iter += 2;
3795 previous_len = iter - name;
3796 }
3797 }
3798
3799 this->name_components.push_back ({previous_len, idx});
3800 }
3801
3802 /* Sort name_components elements by name. */
3803 auto name_comp_compare = [&] (const name_component &left,
3804 const name_component &right)
3805 {
3806 const char *left_qualified = this->symbol_name_at (left.idx);
3807 const char *right_qualified = this->symbol_name_at (right.idx);
3808
3809 const char *left_name = left_qualified + left.name_offset;
3810 const char *right_name = right_qualified + right.name_offset;
3811
3812 return name_cmp (left_name, right_name) < 0;
3813 };
3814
3815 std::sort (this->name_components.begin (),
3816 this->name_components.end (),
3817 name_comp_compare);
3818 }
3819
3820 /* Helper for dw2_expand_symtabs_matching that works with a
3821 mapped_index_base instead of the containing objfile. This is split
3822 to a separate function in order to be able to unit test the
3823 name_components matching using a mock mapped_index_base. For each
3824 symbol name that matches, calls MATCH_CALLBACK, passing it the
3825 symbol's index in the mapped_index_base symbol table. */
3826
3827 static void
3828 dw2_expand_symtabs_matching_symbol
3829 (mapped_index_base &index,
3830 const lookup_name_info &lookup_name_in,
3831 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3832 enum search_domain kind,
3833 gdb::function_view<bool (offset_type)> match_callback)
3834 {
3835 lookup_name_info lookup_name_without_params
3836 = lookup_name_in.make_ignore_params ();
3837
3838 /* Build the symbol name component sorted vector, if we haven't
3839 yet. */
3840 index.build_name_components ();
3841
3842 /* The same symbol may appear more than once in the range though.
3843 E.g., if we're looking for symbols that complete "w", and we have
3844 a symbol named "w1::w2", we'll find the two name components for
3845 that same symbol in the range. To be sure we only call the
3846 callback once per symbol, we first collect the symbol name
3847 indexes that matched in a temporary vector and ignore
3848 duplicates. */
3849 std::vector<offset_type> matches;
3850
3851 struct name_and_matcher
3852 {
3853 symbol_name_matcher_ftype *matcher;
3854 const std::string &name;
3855
3856 bool operator== (const name_and_matcher &other) const
3857 {
3858 return matcher == other.matcher && name == other.name;
3859 }
3860 };
3861
3862 /* A vector holding all the different symbol name matchers, for all
3863 languages. */
3864 std::vector<name_and_matcher> matchers;
3865
3866 for (int i = 0; i < nr_languages; i++)
3867 {
3868 enum language lang_e = (enum language) i;
3869
3870 const language_defn *lang = language_def (lang_e);
3871 symbol_name_matcher_ftype *name_matcher
3872 = get_symbol_name_matcher (lang, lookup_name_without_params);
3873
3874 name_and_matcher key {
3875 name_matcher,
3876 lookup_name_without_params.language_lookup_name (lang_e)
3877 };
3878
3879 /* Don't insert the same comparison routine more than once.
3880 Note that we do this linear walk. This is not a problem in
3881 practice because the number of supported languages is
3882 low. */
3883 if (std::find (matchers.begin (), matchers.end (), key)
3884 != matchers.end ())
3885 continue;
3886 matchers.push_back (std::move (key));
3887
3888 auto bounds
3889 = index.find_name_components_bounds (lookup_name_without_params,
3890 lang_e);
3891
3892 /* Now for each symbol name in range, check to see if we have a name
3893 match, and if so, call the MATCH_CALLBACK callback. */
3894
3895 for (; bounds.first != bounds.second; ++bounds.first)
3896 {
3897 const char *qualified = index.symbol_name_at (bounds.first->idx);
3898
3899 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3900 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3901 continue;
3902
3903 matches.push_back (bounds.first->idx);
3904 }
3905 }
3906
3907 std::sort (matches.begin (), matches.end ());
3908
3909 /* Finally call the callback, once per match. */
3910 ULONGEST prev = -1;
3911 for (offset_type idx : matches)
3912 {
3913 if (prev != idx)
3914 {
3915 if (!match_callback (idx))
3916 break;
3917 prev = idx;
3918 }
3919 }
3920
3921 /* Above we use a type wider than idx's for 'prev', since 0 and
3922 (offset_type)-1 are both possible values. */
3923 static_assert (sizeof (prev) > sizeof (offset_type), "");
3924 }
3925
3926 #if GDB_SELF_TEST
3927
3928 namespace selftests { namespace dw2_expand_symtabs_matching {
3929
3930 /* A mock .gdb_index/.debug_names-like name index table, enough to
3931 exercise dw2_expand_symtabs_matching_symbol, which works with the
3932 mapped_index_base interface. Builds an index from the symbol list
3933 passed as parameter to the constructor. */
3934 class mock_mapped_index : public mapped_index_base
3935 {
3936 public:
3937 mock_mapped_index (gdb::array_view<const char *> symbols)
3938 : m_symbol_table (symbols)
3939 {}
3940
3941 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3942
3943 /* Return the number of names in the symbol table. */
3944 size_t symbol_name_count () const override
3945 {
3946 return m_symbol_table.size ();
3947 }
3948
3949 /* Get the name of the symbol at IDX in the symbol table. */
3950 const char *symbol_name_at (offset_type idx) const override
3951 {
3952 return m_symbol_table[idx];
3953 }
3954
3955 private:
3956 gdb::array_view<const char *> m_symbol_table;
3957 };
3958
3959 /* Convenience function that converts a NULL pointer to a "<null>"
3960 string, to pass to print routines. */
3961
3962 static const char *
3963 string_or_null (const char *str)
3964 {
3965 return str != NULL ? str : "<null>";
3966 }
3967
3968 /* Check if a lookup_name_info built from
3969 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3970 index. EXPECTED_LIST is the list of expected matches, in expected
3971 matching order. If no match expected, then an empty list is
3972 specified. Returns true on success. On failure prints a warning
3973 indicating the file:line that failed, and returns false. */
3974
3975 static bool
3976 check_match (const char *file, int line,
3977 mock_mapped_index &mock_index,
3978 const char *name, symbol_name_match_type match_type,
3979 bool completion_mode,
3980 std::initializer_list<const char *> expected_list)
3981 {
3982 lookup_name_info lookup_name (name, match_type, completion_mode);
3983
3984 bool matched = true;
3985
3986 auto mismatch = [&] (const char *expected_str,
3987 const char *got)
3988 {
3989 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3990 "expected=\"%s\", got=\"%s\"\n"),
3991 file, line,
3992 (match_type == symbol_name_match_type::FULL
3993 ? "FULL" : "WILD"),
3994 name, string_or_null (expected_str), string_or_null (got));
3995 matched = false;
3996 };
3997
3998 auto expected_it = expected_list.begin ();
3999 auto expected_end = expected_list.end ();
4000
4001 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4002 NULL, ALL_DOMAIN,
4003 [&] (offset_type idx)
4004 {
4005 const char *matched_name = mock_index.symbol_name_at (idx);
4006 const char *expected_str
4007 = expected_it == expected_end ? NULL : *expected_it++;
4008
4009 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4010 mismatch (expected_str, matched_name);
4011 return true;
4012 });
4013
4014 const char *expected_str
4015 = expected_it == expected_end ? NULL : *expected_it++;
4016 if (expected_str != NULL)
4017 mismatch (expected_str, NULL);
4018
4019 return matched;
4020 }
4021
4022 /* The symbols added to the mock mapped_index for testing (in
4023 canonical form). */
4024 static const char *test_symbols[] = {
4025 "function",
4026 "std::bar",
4027 "std::zfunction",
4028 "std::zfunction2",
4029 "w1::w2",
4030 "ns::foo<char*>",
4031 "ns::foo<int>",
4032 "ns::foo<long>",
4033 "ns2::tmpl<int>::foo2",
4034 "(anonymous namespace)::A::B::C",
4035
4036 /* These are used to check that the increment-last-char in the
4037 matching algorithm for completion doesn't match "t1_fund" when
4038 completing "t1_func". */
4039 "t1_func",
4040 "t1_func1",
4041 "t1_fund",
4042 "t1_fund1",
4043
4044 /* A UTF-8 name with multi-byte sequences to make sure that
4045 cp-name-parser understands this as a single identifier ("função"
4046 is "function" in PT). */
4047 u8"u8função",
4048
4049 /* \377 (0xff) is Latin1 'ÿ'. */
4050 "yfunc\377",
4051
4052 /* \377 (0xff) is Latin1 'ÿ'. */
4053 "\377",
4054 "\377\377123",
4055
4056 /* A name with all sorts of complications. Starts with "z" to make
4057 it easier for the completion tests below. */
4058 #define Z_SYM_NAME \
4059 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4060 "::tuple<(anonymous namespace)::ui*, " \
4061 "std::default_delete<(anonymous namespace)::ui>, void>"
4062
4063 Z_SYM_NAME
4064 };
4065
4066 /* Returns true if the mapped_index_base::find_name_component_bounds
4067 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4068 in completion mode. */
4069
4070 static bool
4071 check_find_bounds_finds (mapped_index_base &index,
4072 const char *search_name,
4073 gdb::array_view<const char *> expected_syms)
4074 {
4075 lookup_name_info lookup_name (search_name,
4076 symbol_name_match_type::FULL, true);
4077
4078 auto bounds = index.find_name_components_bounds (lookup_name,
4079 language_cplus);
4080
4081 size_t distance = std::distance (bounds.first, bounds.second);
4082 if (distance != expected_syms.size ())
4083 return false;
4084
4085 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4086 {
4087 auto nc_elem = bounds.first + exp_elem;
4088 const char *qualified = index.symbol_name_at (nc_elem->idx);
4089 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4090 return false;
4091 }
4092
4093 return true;
4094 }
4095
4096 /* Test the lower-level mapped_index::find_name_component_bounds
4097 method. */
4098
4099 static void
4100 test_mapped_index_find_name_component_bounds ()
4101 {
4102 mock_mapped_index mock_index (test_symbols);
4103
4104 mock_index.build_name_components ();
4105
4106 /* Test the lower-level mapped_index::find_name_component_bounds
4107 method in completion mode. */
4108 {
4109 static const char *expected_syms[] = {
4110 "t1_func",
4111 "t1_func1",
4112 };
4113
4114 SELF_CHECK (check_find_bounds_finds (mock_index,
4115 "t1_func", expected_syms));
4116 }
4117
4118 /* Check that the increment-last-char in the name matching algorithm
4119 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4120 {
4121 static const char *expected_syms1[] = {
4122 "\377",
4123 "\377\377123",
4124 };
4125 SELF_CHECK (check_find_bounds_finds (mock_index,
4126 "\377", expected_syms1));
4127
4128 static const char *expected_syms2[] = {
4129 "\377\377123",
4130 };
4131 SELF_CHECK (check_find_bounds_finds (mock_index,
4132 "\377\377", expected_syms2));
4133 }
4134 }
4135
4136 /* Test dw2_expand_symtabs_matching_symbol. */
4137
4138 static void
4139 test_dw2_expand_symtabs_matching_symbol ()
4140 {
4141 mock_mapped_index mock_index (test_symbols);
4142
4143 /* We let all tests run until the end even if some fails, for debug
4144 convenience. */
4145 bool any_mismatch = false;
4146
4147 /* Create the expected symbols list (an initializer_list). Needed
4148 because lists have commas, and we need to pass them to CHECK,
4149 which is a macro. */
4150 #define EXPECT(...) { __VA_ARGS__ }
4151
4152 /* Wrapper for check_match that passes down the current
4153 __FILE__/__LINE__. */
4154 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4155 any_mismatch |= !check_match (__FILE__, __LINE__, \
4156 mock_index, \
4157 NAME, MATCH_TYPE, COMPLETION_MODE, \
4158 EXPECTED_LIST)
4159
4160 /* Identity checks. */
4161 for (const char *sym : test_symbols)
4162 {
4163 /* Should be able to match all existing symbols. */
4164 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4165 EXPECT (sym));
4166
4167 /* Should be able to match all existing symbols with
4168 parameters. */
4169 std::string with_params = std::string (sym) + "(int)";
4170 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4171 EXPECT (sym));
4172
4173 /* Should be able to match all existing symbols with
4174 parameters and qualifiers. */
4175 with_params = std::string (sym) + " ( int ) const";
4176 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4177 EXPECT (sym));
4178
4179 /* This should really find sym, but cp-name-parser.y doesn't
4180 know about lvalue/rvalue qualifiers yet. */
4181 with_params = std::string (sym) + " ( int ) &&";
4182 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4183 {});
4184 }
4185
4186 /* Check that the name matching algorithm for completion doesn't get
4187 confused with Latin1 'ÿ' / 0xff. */
4188 {
4189 static const char str[] = "\377";
4190 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4191 EXPECT ("\377", "\377\377123"));
4192 }
4193
4194 /* Check that the increment-last-char in the matching algorithm for
4195 completion doesn't match "t1_fund" when completing "t1_func". */
4196 {
4197 static const char str[] = "t1_func";
4198 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4199 EXPECT ("t1_func", "t1_func1"));
4200 }
4201
4202 /* Check that completion mode works at each prefix of the expected
4203 symbol name. */
4204 {
4205 static const char str[] = "function(int)";
4206 size_t len = strlen (str);
4207 std::string lookup;
4208
4209 for (size_t i = 1; i < len; i++)
4210 {
4211 lookup.assign (str, i);
4212 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4213 EXPECT ("function"));
4214 }
4215 }
4216
4217 /* While "w" is a prefix of both components, the match function
4218 should still only be called once. */
4219 {
4220 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4221 EXPECT ("w1::w2"));
4222 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4223 EXPECT ("w1::w2"));
4224 }
4225
4226 /* Same, with a "complicated" symbol. */
4227 {
4228 static const char str[] = Z_SYM_NAME;
4229 size_t len = strlen (str);
4230 std::string lookup;
4231
4232 for (size_t i = 1; i < len; i++)
4233 {
4234 lookup.assign (str, i);
4235 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4236 EXPECT (Z_SYM_NAME));
4237 }
4238 }
4239
4240 /* In FULL mode, an incomplete symbol doesn't match. */
4241 {
4242 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4243 {});
4244 }
4245
4246 /* A complete symbol with parameters matches any overload, since the
4247 index has no overload info. */
4248 {
4249 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4250 EXPECT ("std::zfunction", "std::zfunction2"));
4251 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4252 EXPECT ("std::zfunction", "std::zfunction2"));
4253 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4254 EXPECT ("std::zfunction", "std::zfunction2"));
4255 }
4256
4257 /* Check that whitespace is ignored appropriately. A symbol with a
4258 template argument list. */
4259 {
4260 static const char expected[] = "ns::foo<int>";
4261 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4262 EXPECT (expected));
4263 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4264 EXPECT (expected));
4265 }
4266
4267 /* Check that whitespace is ignored appropriately. A symbol with a
4268 template argument list that includes a pointer. */
4269 {
4270 static const char expected[] = "ns::foo<char*>";
4271 /* Try both completion and non-completion modes. */
4272 static const bool completion_mode[2] = {false, true};
4273 for (size_t i = 0; i < 2; i++)
4274 {
4275 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4276 completion_mode[i], EXPECT (expected));
4277 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4278 completion_mode[i], EXPECT (expected));
4279
4280 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4281 completion_mode[i], EXPECT (expected));
4282 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4283 completion_mode[i], EXPECT (expected));
4284 }
4285 }
4286
4287 {
4288 /* Check method qualifiers are ignored. */
4289 static const char expected[] = "ns::foo<char*>";
4290 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4291 symbol_name_match_type::FULL, true, EXPECT (expected));
4292 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4293 symbol_name_match_type::FULL, true, EXPECT (expected));
4294 CHECK_MATCH ("foo < char * > ( int ) const",
4295 symbol_name_match_type::WILD, true, EXPECT (expected));
4296 CHECK_MATCH ("foo < char * > ( int ) &&",
4297 symbol_name_match_type::WILD, true, EXPECT (expected));
4298 }
4299
4300 /* Test lookup names that don't match anything. */
4301 {
4302 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4303 {});
4304
4305 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4306 {});
4307 }
4308
4309 /* Some wild matching tests, exercising "(anonymous namespace)",
4310 which should not be confused with a parameter list. */
4311 {
4312 static const char *syms[] = {
4313 "A::B::C",
4314 "B::C",
4315 "C",
4316 "A :: B :: C ( int )",
4317 "B :: C ( int )",
4318 "C ( int )",
4319 };
4320
4321 for (const char *s : syms)
4322 {
4323 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4324 EXPECT ("(anonymous namespace)::A::B::C"));
4325 }
4326 }
4327
4328 {
4329 static const char expected[] = "ns2::tmpl<int>::foo2";
4330 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4331 EXPECT (expected));
4332 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4333 EXPECT (expected));
4334 }
4335
4336 SELF_CHECK (!any_mismatch);
4337
4338 #undef EXPECT
4339 #undef CHECK_MATCH
4340 }
4341
4342 static void
4343 run_test ()
4344 {
4345 test_mapped_index_find_name_component_bounds ();
4346 test_dw2_expand_symtabs_matching_symbol ();
4347 }
4348
4349 }} // namespace selftests::dw2_expand_symtabs_matching
4350
4351 #endif /* GDB_SELF_TEST */
4352
4353 /* If FILE_MATCHER is NULL or if PER_CU has
4354 dwarf2_per_cu_quick_data::MARK set (see
4355 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4356 EXPANSION_NOTIFY on it. */
4357
4358 static void
4359 dw2_expand_symtabs_matching_one
4360 (struct dwarf2_per_cu_data *per_cu,
4361 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4362 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4363 {
4364 if (file_matcher == NULL || per_cu->v.quick->mark)
4365 {
4366 bool symtab_was_null
4367 = (per_cu->v.quick->compunit_symtab == NULL);
4368
4369 dw2_instantiate_symtab (per_cu, false);
4370
4371 if (expansion_notify != NULL
4372 && symtab_was_null
4373 && per_cu->v.quick->compunit_symtab != NULL)
4374 expansion_notify (per_cu->v.quick->compunit_symtab);
4375 }
4376 }
4377
4378 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4379 matched, to expand corresponding CUs that were marked. IDX is the
4380 index of the symbol name that matched. */
4381
4382 static void
4383 dw2_expand_marked_cus
4384 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4385 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4386 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4387 search_domain kind)
4388 {
4389 offset_type *vec, vec_len, vec_idx;
4390 bool global_seen = false;
4391 mapped_index &index = *dwarf2_per_objfile->index_table;
4392
4393 vec = (offset_type *) (index.constant_pool
4394 + MAYBE_SWAP (index.symbol_table[idx].vec));
4395 vec_len = MAYBE_SWAP (vec[0]);
4396 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4397 {
4398 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4399 /* This value is only valid for index versions >= 7. */
4400 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4401 gdb_index_symbol_kind symbol_kind =
4402 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4403 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4404 /* Only check the symbol attributes if they're present.
4405 Indices prior to version 7 don't record them,
4406 and indices >= 7 may elide them for certain symbols
4407 (gold does this). */
4408 int attrs_valid =
4409 (index.version >= 7
4410 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4411
4412 /* Work around gold/15646. */
4413 if (attrs_valid)
4414 {
4415 if (!is_static && global_seen)
4416 continue;
4417 if (!is_static)
4418 global_seen = true;
4419 }
4420
4421 /* Only check the symbol's kind if it has one. */
4422 if (attrs_valid)
4423 {
4424 switch (kind)
4425 {
4426 case VARIABLES_DOMAIN:
4427 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4428 continue;
4429 break;
4430 case FUNCTIONS_DOMAIN:
4431 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4432 continue;
4433 break;
4434 case TYPES_DOMAIN:
4435 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4436 continue;
4437 break;
4438 case MODULES_DOMAIN:
4439 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4440 continue;
4441 break;
4442 default:
4443 break;
4444 }
4445 }
4446
4447 /* Don't crash on bad data. */
4448 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4449 + dwarf2_per_objfile->all_type_units.size ()))
4450 {
4451 complaint (_(".gdb_index entry has bad CU index"
4452 " [in module %s]"),
4453 objfile_name (dwarf2_per_objfile->objfile));
4454 continue;
4455 }
4456
4457 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4458 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4459 expansion_notify);
4460 }
4461 }
4462
4463 /* If FILE_MATCHER is non-NULL, set all the
4464 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4465 that match FILE_MATCHER. */
4466
4467 static void
4468 dw_expand_symtabs_matching_file_matcher
4469 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4470 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4471 {
4472 if (file_matcher == NULL)
4473 return;
4474
4475 objfile *const objfile = dwarf2_per_objfile->objfile;
4476
4477 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4478 htab_eq_pointer,
4479 NULL, xcalloc, xfree));
4480 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4481 htab_eq_pointer,
4482 NULL, xcalloc, xfree));
4483
4484 /* The rule is CUs specify all the files, including those used by
4485 any TU, so there's no need to scan TUs here. */
4486
4487 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4488 {
4489 QUIT;
4490
4491 per_cu->v.quick->mark = 0;
4492
4493 /* We only need to look at symtabs not already expanded. */
4494 if (per_cu->v.quick->compunit_symtab)
4495 continue;
4496
4497 quick_file_names *file_data = dw2_get_file_names (per_cu);
4498 if (file_data == NULL)
4499 continue;
4500
4501 if (htab_find (visited_not_found.get (), file_data) != NULL)
4502 continue;
4503 else if (htab_find (visited_found.get (), file_data) != NULL)
4504 {
4505 per_cu->v.quick->mark = 1;
4506 continue;
4507 }
4508
4509 for (int j = 0; j < file_data->num_file_names; ++j)
4510 {
4511 const char *this_real_name;
4512
4513 if (file_matcher (file_data->file_names[j], false))
4514 {
4515 per_cu->v.quick->mark = 1;
4516 break;
4517 }
4518
4519 /* Before we invoke realpath, which can get expensive when many
4520 files are involved, do a quick comparison of the basenames. */
4521 if (!basenames_may_differ
4522 && !file_matcher (lbasename (file_data->file_names[j]),
4523 true))
4524 continue;
4525
4526 this_real_name = dw2_get_real_path (objfile, file_data, j);
4527 if (file_matcher (this_real_name, false))
4528 {
4529 per_cu->v.quick->mark = 1;
4530 break;
4531 }
4532 }
4533
4534 void **slot = htab_find_slot (per_cu->v.quick->mark
4535 ? visited_found.get ()
4536 : visited_not_found.get (),
4537 file_data, INSERT);
4538 *slot = file_data;
4539 }
4540 }
4541
4542 static void
4543 dw2_expand_symtabs_matching
4544 (struct objfile *objfile,
4545 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4546 const lookup_name_info &lookup_name,
4547 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4548 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4549 enum search_domain kind)
4550 {
4551 struct dwarf2_per_objfile *dwarf2_per_objfile
4552 = get_dwarf2_per_objfile (objfile);
4553
4554 /* index_table is NULL if OBJF_READNOW. */
4555 if (!dwarf2_per_objfile->index_table)
4556 return;
4557
4558 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4559
4560 mapped_index &index = *dwarf2_per_objfile->index_table;
4561
4562 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4563 symbol_matcher,
4564 kind, [&] (offset_type idx)
4565 {
4566 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4567 expansion_notify, kind);
4568 return true;
4569 });
4570 }
4571
4572 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4573 symtab. */
4574
4575 static struct compunit_symtab *
4576 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4577 CORE_ADDR pc)
4578 {
4579 int i;
4580
4581 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4582 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4583 return cust;
4584
4585 if (cust->includes == NULL)
4586 return NULL;
4587
4588 for (i = 0; cust->includes[i]; ++i)
4589 {
4590 struct compunit_symtab *s = cust->includes[i];
4591
4592 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4593 if (s != NULL)
4594 return s;
4595 }
4596
4597 return NULL;
4598 }
4599
4600 static struct compunit_symtab *
4601 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4602 struct bound_minimal_symbol msymbol,
4603 CORE_ADDR pc,
4604 struct obj_section *section,
4605 int warn_if_readin)
4606 {
4607 struct dwarf2_per_cu_data *data;
4608 struct compunit_symtab *result;
4609
4610 if (!objfile->partial_symtabs->psymtabs_addrmap)
4611 return NULL;
4612
4613 CORE_ADDR baseaddr = objfile->text_section_offset ();
4614 data = (struct dwarf2_per_cu_data *) addrmap_find
4615 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4616 if (!data)
4617 return NULL;
4618
4619 if (warn_if_readin && data->v.quick->compunit_symtab)
4620 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4621 paddress (get_objfile_arch (objfile), pc));
4622
4623 result
4624 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4625 false),
4626 pc);
4627 gdb_assert (result != NULL);
4628 return result;
4629 }
4630
4631 static void
4632 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4633 void *data, int need_fullname)
4634 {
4635 struct dwarf2_per_objfile *dwarf2_per_objfile
4636 = get_dwarf2_per_objfile (objfile);
4637
4638 if (!dwarf2_per_objfile->filenames_cache)
4639 {
4640 dwarf2_per_objfile->filenames_cache.emplace ();
4641
4642 htab_up visited (htab_create_alloc (10,
4643 htab_hash_pointer, htab_eq_pointer,
4644 NULL, xcalloc, xfree));
4645
4646 /* The rule is CUs specify all the files, including those used
4647 by any TU, so there's no need to scan TUs here. We can
4648 ignore file names coming from already-expanded CUs. */
4649
4650 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4651 {
4652 if (per_cu->v.quick->compunit_symtab)
4653 {
4654 void **slot = htab_find_slot (visited.get (),
4655 per_cu->v.quick->file_names,
4656 INSERT);
4657
4658 *slot = per_cu->v.quick->file_names;
4659 }
4660 }
4661
4662 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4663 {
4664 /* We only need to look at symtabs not already expanded. */
4665 if (per_cu->v.quick->compunit_symtab)
4666 continue;
4667
4668 quick_file_names *file_data = dw2_get_file_names (per_cu);
4669 if (file_data == NULL)
4670 continue;
4671
4672 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4673 if (*slot)
4674 {
4675 /* Already visited. */
4676 continue;
4677 }
4678 *slot = file_data;
4679
4680 for (int j = 0; j < file_data->num_file_names; ++j)
4681 {
4682 const char *filename = file_data->file_names[j];
4683 dwarf2_per_objfile->filenames_cache->seen (filename);
4684 }
4685 }
4686 }
4687
4688 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4689 {
4690 gdb::unique_xmalloc_ptr<char> this_real_name;
4691
4692 if (need_fullname)
4693 this_real_name = gdb_realpath (filename);
4694 (*fun) (filename, this_real_name.get (), data);
4695 });
4696 }
4697
4698 static int
4699 dw2_has_symbols (struct objfile *objfile)
4700 {
4701 return 1;
4702 }
4703
4704 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4705 {
4706 dw2_has_symbols,
4707 dw2_find_last_source_symtab,
4708 dw2_forget_cached_source_info,
4709 dw2_map_symtabs_matching_filename,
4710 dw2_lookup_symbol,
4711 NULL,
4712 dw2_print_stats,
4713 dw2_dump,
4714 dw2_expand_symtabs_for_function,
4715 dw2_expand_all_symtabs,
4716 dw2_expand_symtabs_with_fullname,
4717 dw2_map_matching_symbols,
4718 dw2_expand_symtabs_matching,
4719 dw2_find_pc_sect_compunit_symtab,
4720 NULL,
4721 dw2_map_symbol_filenames
4722 };
4723
4724 /* DWARF-5 debug_names reader. */
4725
4726 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4727 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4728
4729 /* A helper function that reads the .debug_names section in SECTION
4730 and fills in MAP. FILENAME is the name of the file containing the
4731 section; it is used for error reporting.
4732
4733 Returns true if all went well, false otherwise. */
4734
4735 static bool
4736 read_debug_names_from_section (struct objfile *objfile,
4737 const char *filename,
4738 struct dwarf2_section_info *section,
4739 mapped_debug_names &map)
4740 {
4741 if (section->empty ())
4742 return false;
4743
4744 /* Older elfutils strip versions could keep the section in the main
4745 executable while splitting it for the separate debug info file. */
4746 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4747 return false;
4748
4749 section->read (objfile);
4750
4751 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4752
4753 const gdb_byte *addr = section->buffer;
4754
4755 bfd *const abfd = section->get_bfd_owner ();
4756
4757 unsigned int bytes_read;
4758 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4759 addr += bytes_read;
4760
4761 map.dwarf5_is_dwarf64 = bytes_read != 4;
4762 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4763 if (bytes_read + length != section->size)
4764 {
4765 /* There may be multiple per-CU indices. */
4766 warning (_("Section .debug_names in %s length %s does not match "
4767 "section length %s, ignoring .debug_names."),
4768 filename, plongest (bytes_read + length),
4769 pulongest (section->size));
4770 return false;
4771 }
4772
4773 /* The version number. */
4774 uint16_t version = read_2_bytes (abfd, addr);
4775 addr += 2;
4776 if (version != 5)
4777 {
4778 warning (_("Section .debug_names in %s has unsupported version %d, "
4779 "ignoring .debug_names."),
4780 filename, version);
4781 return false;
4782 }
4783
4784 /* Padding. */
4785 uint16_t padding = read_2_bytes (abfd, addr);
4786 addr += 2;
4787 if (padding != 0)
4788 {
4789 warning (_("Section .debug_names in %s has unsupported padding %d, "
4790 "ignoring .debug_names."),
4791 filename, padding);
4792 return false;
4793 }
4794
4795 /* comp_unit_count - The number of CUs in the CU list. */
4796 map.cu_count = read_4_bytes (abfd, addr);
4797 addr += 4;
4798
4799 /* local_type_unit_count - The number of TUs in the local TU
4800 list. */
4801 map.tu_count = read_4_bytes (abfd, addr);
4802 addr += 4;
4803
4804 /* foreign_type_unit_count - The number of TUs in the foreign TU
4805 list. */
4806 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4807 addr += 4;
4808 if (foreign_tu_count != 0)
4809 {
4810 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4811 "ignoring .debug_names."),
4812 filename, static_cast<unsigned long> (foreign_tu_count));
4813 return false;
4814 }
4815
4816 /* bucket_count - The number of hash buckets in the hash lookup
4817 table. */
4818 map.bucket_count = read_4_bytes (abfd, addr);
4819 addr += 4;
4820
4821 /* name_count - The number of unique names in the index. */
4822 map.name_count = read_4_bytes (abfd, addr);
4823 addr += 4;
4824
4825 /* abbrev_table_size - The size in bytes of the abbreviations
4826 table. */
4827 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4828 addr += 4;
4829
4830 /* augmentation_string_size - The size in bytes of the augmentation
4831 string. This value is rounded up to a multiple of 4. */
4832 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4833 addr += 4;
4834 map.augmentation_is_gdb = ((augmentation_string_size
4835 == sizeof (dwarf5_augmentation))
4836 && memcmp (addr, dwarf5_augmentation,
4837 sizeof (dwarf5_augmentation)) == 0);
4838 augmentation_string_size += (-augmentation_string_size) & 3;
4839 addr += augmentation_string_size;
4840
4841 /* List of CUs */
4842 map.cu_table_reordered = addr;
4843 addr += map.cu_count * map.offset_size;
4844
4845 /* List of Local TUs */
4846 map.tu_table_reordered = addr;
4847 addr += map.tu_count * map.offset_size;
4848
4849 /* Hash Lookup Table */
4850 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4851 addr += map.bucket_count * 4;
4852 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4853 addr += map.name_count * 4;
4854
4855 /* Name Table */
4856 map.name_table_string_offs_reordered = addr;
4857 addr += map.name_count * map.offset_size;
4858 map.name_table_entry_offs_reordered = addr;
4859 addr += map.name_count * map.offset_size;
4860
4861 const gdb_byte *abbrev_table_start = addr;
4862 for (;;)
4863 {
4864 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4865 addr += bytes_read;
4866 if (index_num == 0)
4867 break;
4868
4869 const auto insertpair
4870 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4871 if (!insertpair.second)
4872 {
4873 warning (_("Section .debug_names in %s has duplicate index %s, "
4874 "ignoring .debug_names."),
4875 filename, pulongest (index_num));
4876 return false;
4877 }
4878 mapped_debug_names::index_val &indexval = insertpair.first->second;
4879 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4880 addr += bytes_read;
4881
4882 for (;;)
4883 {
4884 mapped_debug_names::index_val::attr attr;
4885 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4886 addr += bytes_read;
4887 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4888 addr += bytes_read;
4889 if (attr.form == DW_FORM_implicit_const)
4890 {
4891 attr.implicit_const = read_signed_leb128 (abfd, addr,
4892 &bytes_read);
4893 addr += bytes_read;
4894 }
4895 if (attr.dw_idx == 0 && attr.form == 0)
4896 break;
4897 indexval.attr_vec.push_back (std::move (attr));
4898 }
4899 }
4900 if (addr != abbrev_table_start + abbrev_table_size)
4901 {
4902 warning (_("Section .debug_names in %s has abbreviation_table "
4903 "of size %s vs. written as %u, ignoring .debug_names."),
4904 filename, plongest (addr - abbrev_table_start),
4905 abbrev_table_size);
4906 return false;
4907 }
4908 map.entry_pool = addr;
4909
4910 return true;
4911 }
4912
4913 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4914 list. */
4915
4916 static void
4917 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4918 const mapped_debug_names &map,
4919 dwarf2_section_info &section,
4920 bool is_dwz)
4921 {
4922 sect_offset sect_off_prev;
4923 for (uint32_t i = 0; i <= map.cu_count; ++i)
4924 {
4925 sect_offset sect_off_next;
4926 if (i < map.cu_count)
4927 {
4928 sect_off_next
4929 = (sect_offset) (extract_unsigned_integer
4930 (map.cu_table_reordered + i * map.offset_size,
4931 map.offset_size,
4932 map.dwarf5_byte_order));
4933 }
4934 else
4935 sect_off_next = (sect_offset) section.size;
4936 if (i >= 1)
4937 {
4938 const ULONGEST length = sect_off_next - sect_off_prev;
4939 dwarf2_per_cu_data *per_cu
4940 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
4941 sect_off_prev, length);
4942 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
4943 }
4944 sect_off_prev = sect_off_next;
4945 }
4946 }
4947
4948 /* Read the CU list from the mapped index, and use it to create all
4949 the CU objects for this dwarf2_per_objfile. */
4950
4951 static void
4952 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
4953 const mapped_debug_names &map,
4954 const mapped_debug_names &dwz_map)
4955 {
4956 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
4957 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4958
4959 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
4960 dwarf2_per_objfile->info,
4961 false /* is_dwz */);
4962
4963 if (dwz_map.cu_count == 0)
4964 return;
4965
4966 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4967 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
4968 true /* is_dwz */);
4969 }
4970
4971 /* Read .debug_names. If everything went ok, initialize the "quick"
4972 elements of all the CUs and return true. Otherwise, return false. */
4973
4974 static bool
4975 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
4976 {
4977 std::unique_ptr<mapped_debug_names> map
4978 (new mapped_debug_names (dwarf2_per_objfile));
4979 mapped_debug_names dwz_map (dwarf2_per_objfile);
4980 struct objfile *objfile = dwarf2_per_objfile->objfile;
4981
4982 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4983 &dwarf2_per_objfile->debug_names,
4984 *map))
4985 return false;
4986
4987 /* Don't use the index if it's empty. */
4988 if (map->name_count == 0)
4989 return false;
4990
4991 /* If there is a .dwz file, read it so we can get its CU list as
4992 well. */
4993 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4994 if (dwz != NULL)
4995 {
4996 if (!read_debug_names_from_section (objfile,
4997 bfd_get_filename (dwz->dwz_bfd.get ()),
4998 &dwz->debug_names, dwz_map))
4999 {
5000 warning (_("could not read '.debug_names' section from %s; skipping"),
5001 bfd_get_filename (dwz->dwz_bfd.get ()));
5002 return false;
5003 }
5004 }
5005
5006 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5007
5008 if (map->tu_count != 0)
5009 {
5010 /* We can only handle a single .debug_types when we have an
5011 index. */
5012 if (dwarf2_per_objfile->types.size () != 1)
5013 return false;
5014
5015 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5016
5017 create_signatured_type_table_from_debug_names
5018 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5019 }
5020
5021 create_addrmap_from_aranges (dwarf2_per_objfile,
5022 &dwarf2_per_objfile->debug_aranges);
5023
5024 dwarf2_per_objfile->debug_names_table = std::move (map);
5025 dwarf2_per_objfile->using_index = 1;
5026 dwarf2_per_objfile->quick_file_names_table =
5027 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5028
5029 return true;
5030 }
5031
5032 /* Type used to manage iterating over all CUs looking for a symbol for
5033 .debug_names. */
5034
5035 class dw2_debug_names_iterator
5036 {
5037 public:
5038 dw2_debug_names_iterator (const mapped_debug_names &map,
5039 gdb::optional<block_enum> block_index,
5040 domain_enum domain,
5041 const char *name)
5042 : m_map (map), m_block_index (block_index), m_domain (domain),
5043 m_addr (find_vec_in_debug_names (map, name))
5044 {}
5045
5046 dw2_debug_names_iterator (const mapped_debug_names &map,
5047 search_domain search, uint32_t namei)
5048 : m_map (map),
5049 m_search (search),
5050 m_addr (find_vec_in_debug_names (map, namei))
5051 {}
5052
5053 dw2_debug_names_iterator (const mapped_debug_names &map,
5054 block_enum block_index, domain_enum domain,
5055 uint32_t namei)
5056 : m_map (map), m_block_index (block_index), m_domain (domain),
5057 m_addr (find_vec_in_debug_names (map, namei))
5058 {}
5059
5060 /* Return the next matching CU or NULL if there are no more. */
5061 dwarf2_per_cu_data *next ();
5062
5063 private:
5064 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5065 const char *name);
5066 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5067 uint32_t namei);
5068
5069 /* The internalized form of .debug_names. */
5070 const mapped_debug_names &m_map;
5071
5072 /* If set, only look for symbols that match that block. Valid values are
5073 GLOBAL_BLOCK and STATIC_BLOCK. */
5074 const gdb::optional<block_enum> m_block_index;
5075
5076 /* The kind of symbol we're looking for. */
5077 const domain_enum m_domain = UNDEF_DOMAIN;
5078 const search_domain m_search = ALL_DOMAIN;
5079
5080 /* The list of CUs from the index entry of the symbol, or NULL if
5081 not found. */
5082 const gdb_byte *m_addr;
5083 };
5084
5085 const char *
5086 mapped_debug_names::namei_to_name (uint32_t namei) const
5087 {
5088 const ULONGEST namei_string_offs
5089 = extract_unsigned_integer ((name_table_string_offs_reordered
5090 + namei * offset_size),
5091 offset_size,
5092 dwarf5_byte_order);
5093 return read_indirect_string_at_offset (dwarf2_per_objfile,
5094 namei_string_offs);
5095 }
5096
5097 /* Find a slot in .debug_names for the object named NAME. If NAME is
5098 found, return pointer to its pool data. If NAME cannot be found,
5099 return NULL. */
5100
5101 const gdb_byte *
5102 dw2_debug_names_iterator::find_vec_in_debug_names
5103 (const mapped_debug_names &map, const char *name)
5104 {
5105 int (*cmp) (const char *, const char *);
5106
5107 gdb::unique_xmalloc_ptr<char> without_params;
5108 if (current_language->la_language == language_cplus
5109 || current_language->la_language == language_fortran
5110 || current_language->la_language == language_d)
5111 {
5112 /* NAME is already canonical. Drop any qualifiers as
5113 .debug_names does not contain any. */
5114
5115 if (strchr (name, '(') != NULL)
5116 {
5117 without_params = cp_remove_params (name);
5118 if (without_params != NULL)
5119 name = without_params.get ();
5120 }
5121 }
5122
5123 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5124
5125 const uint32_t full_hash = dwarf5_djb_hash (name);
5126 uint32_t namei
5127 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5128 (map.bucket_table_reordered
5129 + (full_hash % map.bucket_count)), 4,
5130 map.dwarf5_byte_order);
5131 if (namei == 0)
5132 return NULL;
5133 --namei;
5134 if (namei >= map.name_count)
5135 {
5136 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5137 "[in module %s]"),
5138 namei, map.name_count,
5139 objfile_name (map.dwarf2_per_objfile->objfile));
5140 return NULL;
5141 }
5142
5143 for (;;)
5144 {
5145 const uint32_t namei_full_hash
5146 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5147 (map.hash_table_reordered + namei), 4,
5148 map.dwarf5_byte_order);
5149 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5150 return NULL;
5151
5152 if (full_hash == namei_full_hash)
5153 {
5154 const char *const namei_string = map.namei_to_name (namei);
5155
5156 #if 0 /* An expensive sanity check. */
5157 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5158 {
5159 complaint (_("Wrong .debug_names hash for string at index %u "
5160 "[in module %s]"),
5161 namei, objfile_name (dwarf2_per_objfile->objfile));
5162 return NULL;
5163 }
5164 #endif
5165
5166 if (cmp (namei_string, name) == 0)
5167 {
5168 const ULONGEST namei_entry_offs
5169 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5170 + namei * map.offset_size),
5171 map.offset_size, map.dwarf5_byte_order);
5172 return map.entry_pool + namei_entry_offs;
5173 }
5174 }
5175
5176 ++namei;
5177 if (namei >= map.name_count)
5178 return NULL;
5179 }
5180 }
5181
5182 const gdb_byte *
5183 dw2_debug_names_iterator::find_vec_in_debug_names
5184 (const mapped_debug_names &map, uint32_t namei)
5185 {
5186 if (namei >= map.name_count)
5187 {
5188 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5189 "[in module %s]"),
5190 namei, map.name_count,
5191 objfile_name (map.dwarf2_per_objfile->objfile));
5192 return NULL;
5193 }
5194
5195 const ULONGEST namei_entry_offs
5196 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5197 + namei * map.offset_size),
5198 map.offset_size, map.dwarf5_byte_order);
5199 return map.entry_pool + namei_entry_offs;
5200 }
5201
5202 /* See dw2_debug_names_iterator. */
5203
5204 dwarf2_per_cu_data *
5205 dw2_debug_names_iterator::next ()
5206 {
5207 if (m_addr == NULL)
5208 return NULL;
5209
5210 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5211 struct objfile *objfile = dwarf2_per_objfile->objfile;
5212 bfd *const abfd = objfile->obfd;
5213
5214 again:
5215
5216 unsigned int bytes_read;
5217 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5218 m_addr += bytes_read;
5219 if (abbrev == 0)
5220 return NULL;
5221
5222 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5223 if (indexval_it == m_map.abbrev_map.cend ())
5224 {
5225 complaint (_("Wrong .debug_names undefined abbrev code %s "
5226 "[in module %s]"),
5227 pulongest (abbrev), objfile_name (objfile));
5228 return NULL;
5229 }
5230 const mapped_debug_names::index_val &indexval = indexval_it->second;
5231 enum class symbol_linkage {
5232 unknown,
5233 static_,
5234 extern_,
5235 } symbol_linkage_ = symbol_linkage::unknown;
5236 dwarf2_per_cu_data *per_cu = NULL;
5237 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5238 {
5239 ULONGEST ull;
5240 switch (attr.form)
5241 {
5242 case DW_FORM_implicit_const:
5243 ull = attr.implicit_const;
5244 break;
5245 case DW_FORM_flag_present:
5246 ull = 1;
5247 break;
5248 case DW_FORM_udata:
5249 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5250 m_addr += bytes_read;
5251 break;
5252 default:
5253 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5254 dwarf_form_name (attr.form),
5255 objfile_name (objfile));
5256 return NULL;
5257 }
5258 switch (attr.dw_idx)
5259 {
5260 case DW_IDX_compile_unit:
5261 /* Don't crash on bad data. */
5262 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5263 {
5264 complaint (_(".debug_names entry has bad CU index %s"
5265 " [in module %s]"),
5266 pulongest (ull),
5267 objfile_name (dwarf2_per_objfile->objfile));
5268 continue;
5269 }
5270 per_cu = dwarf2_per_objfile->get_cutu (ull);
5271 break;
5272 case DW_IDX_type_unit:
5273 /* Don't crash on bad data. */
5274 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5275 {
5276 complaint (_(".debug_names entry has bad TU index %s"
5277 " [in module %s]"),
5278 pulongest (ull),
5279 objfile_name (dwarf2_per_objfile->objfile));
5280 continue;
5281 }
5282 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5283 break;
5284 case DW_IDX_GNU_internal:
5285 if (!m_map.augmentation_is_gdb)
5286 break;
5287 symbol_linkage_ = symbol_linkage::static_;
5288 break;
5289 case DW_IDX_GNU_external:
5290 if (!m_map.augmentation_is_gdb)
5291 break;
5292 symbol_linkage_ = symbol_linkage::extern_;
5293 break;
5294 }
5295 }
5296
5297 /* Skip if already read in. */
5298 if (per_cu->v.quick->compunit_symtab)
5299 goto again;
5300
5301 /* Check static vs global. */
5302 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5303 {
5304 const bool want_static = *m_block_index == STATIC_BLOCK;
5305 const bool symbol_is_static =
5306 symbol_linkage_ == symbol_linkage::static_;
5307 if (want_static != symbol_is_static)
5308 goto again;
5309 }
5310
5311 /* Match dw2_symtab_iter_next, symbol_kind
5312 and debug_names::psymbol_tag. */
5313 switch (m_domain)
5314 {
5315 case VAR_DOMAIN:
5316 switch (indexval.dwarf_tag)
5317 {
5318 case DW_TAG_variable:
5319 case DW_TAG_subprogram:
5320 /* Some types are also in VAR_DOMAIN. */
5321 case DW_TAG_typedef:
5322 case DW_TAG_structure_type:
5323 break;
5324 default:
5325 goto again;
5326 }
5327 break;
5328 case STRUCT_DOMAIN:
5329 switch (indexval.dwarf_tag)
5330 {
5331 case DW_TAG_typedef:
5332 case DW_TAG_structure_type:
5333 break;
5334 default:
5335 goto again;
5336 }
5337 break;
5338 case LABEL_DOMAIN:
5339 switch (indexval.dwarf_tag)
5340 {
5341 case 0:
5342 case DW_TAG_variable:
5343 break;
5344 default:
5345 goto again;
5346 }
5347 break;
5348 case MODULE_DOMAIN:
5349 switch (indexval.dwarf_tag)
5350 {
5351 case DW_TAG_module:
5352 break;
5353 default:
5354 goto again;
5355 }
5356 break;
5357 default:
5358 break;
5359 }
5360
5361 /* Match dw2_expand_symtabs_matching, symbol_kind and
5362 debug_names::psymbol_tag. */
5363 switch (m_search)
5364 {
5365 case VARIABLES_DOMAIN:
5366 switch (indexval.dwarf_tag)
5367 {
5368 case DW_TAG_variable:
5369 break;
5370 default:
5371 goto again;
5372 }
5373 break;
5374 case FUNCTIONS_DOMAIN:
5375 switch (indexval.dwarf_tag)
5376 {
5377 case DW_TAG_subprogram:
5378 break;
5379 default:
5380 goto again;
5381 }
5382 break;
5383 case TYPES_DOMAIN:
5384 switch (indexval.dwarf_tag)
5385 {
5386 case DW_TAG_typedef:
5387 case DW_TAG_structure_type:
5388 break;
5389 default:
5390 goto again;
5391 }
5392 break;
5393 case MODULES_DOMAIN:
5394 switch (indexval.dwarf_tag)
5395 {
5396 case DW_TAG_module:
5397 break;
5398 default:
5399 goto again;
5400 }
5401 default:
5402 break;
5403 }
5404
5405 return per_cu;
5406 }
5407
5408 static struct compunit_symtab *
5409 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5410 const char *name, domain_enum domain)
5411 {
5412 struct dwarf2_per_objfile *dwarf2_per_objfile
5413 = get_dwarf2_per_objfile (objfile);
5414
5415 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5416 if (!mapp)
5417 {
5418 /* index is NULL if OBJF_READNOW. */
5419 return NULL;
5420 }
5421 const auto &map = *mapp;
5422
5423 dw2_debug_names_iterator iter (map, block_index, domain, name);
5424
5425 struct compunit_symtab *stab_best = NULL;
5426 struct dwarf2_per_cu_data *per_cu;
5427 while ((per_cu = iter.next ()) != NULL)
5428 {
5429 struct symbol *sym, *with_opaque = NULL;
5430 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5431 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5432 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5433
5434 sym = block_find_symbol (block, name, domain,
5435 block_find_non_opaque_type_preferred,
5436 &with_opaque);
5437
5438 /* Some caution must be observed with overloaded functions and
5439 methods, since the index will not contain any overload
5440 information (but NAME might contain it). */
5441
5442 if (sym != NULL
5443 && strcmp_iw (sym->search_name (), name) == 0)
5444 return stab;
5445 if (with_opaque != NULL
5446 && strcmp_iw (with_opaque->search_name (), name) == 0)
5447 stab_best = stab;
5448
5449 /* Keep looking through other CUs. */
5450 }
5451
5452 return stab_best;
5453 }
5454
5455 /* This dumps minimal information about .debug_names. It is called
5456 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5457 uses this to verify that .debug_names has been loaded. */
5458
5459 static void
5460 dw2_debug_names_dump (struct objfile *objfile)
5461 {
5462 struct dwarf2_per_objfile *dwarf2_per_objfile
5463 = get_dwarf2_per_objfile (objfile);
5464
5465 gdb_assert (dwarf2_per_objfile->using_index);
5466 printf_filtered (".debug_names:");
5467 if (dwarf2_per_objfile->debug_names_table)
5468 printf_filtered (" exists\n");
5469 else
5470 printf_filtered (" faked for \"readnow\"\n");
5471 printf_filtered ("\n");
5472 }
5473
5474 static void
5475 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5476 const char *func_name)
5477 {
5478 struct dwarf2_per_objfile *dwarf2_per_objfile
5479 = get_dwarf2_per_objfile (objfile);
5480
5481 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5482 if (dwarf2_per_objfile->debug_names_table)
5483 {
5484 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5485
5486 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5487
5488 struct dwarf2_per_cu_data *per_cu;
5489 while ((per_cu = iter.next ()) != NULL)
5490 dw2_instantiate_symtab (per_cu, false);
5491 }
5492 }
5493
5494 static void
5495 dw2_debug_names_map_matching_symbols
5496 (struct objfile *objfile,
5497 const lookup_name_info &name, domain_enum domain,
5498 int global,
5499 gdb::function_view<symbol_found_callback_ftype> callback,
5500 symbol_compare_ftype *ordered_compare)
5501 {
5502 struct dwarf2_per_objfile *dwarf2_per_objfile
5503 = get_dwarf2_per_objfile (objfile);
5504
5505 /* debug_names_table is NULL if OBJF_READNOW. */
5506 if (!dwarf2_per_objfile->debug_names_table)
5507 return;
5508
5509 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5510 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5511
5512 const char *match_name = name.ada ().lookup_name ().c_str ();
5513 auto matcher = [&] (const char *symname)
5514 {
5515 if (ordered_compare == nullptr)
5516 return true;
5517 return ordered_compare (symname, match_name) == 0;
5518 };
5519
5520 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5521 [&] (offset_type namei)
5522 {
5523 /* The name was matched, now expand corresponding CUs that were
5524 marked. */
5525 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5526
5527 struct dwarf2_per_cu_data *per_cu;
5528 while ((per_cu = iter.next ()) != NULL)
5529 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5530 return true;
5531 });
5532
5533 /* It's a shame we couldn't do this inside the
5534 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5535 that have already been expanded. Instead, this loop matches what
5536 the psymtab code does. */
5537 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5538 {
5539 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5540 if (cust != nullptr)
5541 {
5542 const struct block *block
5543 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5544 if (!iterate_over_symbols_terminated (block, name,
5545 domain, callback))
5546 break;
5547 }
5548 }
5549 }
5550
5551 static void
5552 dw2_debug_names_expand_symtabs_matching
5553 (struct objfile *objfile,
5554 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5555 const lookup_name_info &lookup_name,
5556 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5557 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5558 enum search_domain kind)
5559 {
5560 struct dwarf2_per_objfile *dwarf2_per_objfile
5561 = get_dwarf2_per_objfile (objfile);
5562
5563 /* debug_names_table is NULL if OBJF_READNOW. */
5564 if (!dwarf2_per_objfile->debug_names_table)
5565 return;
5566
5567 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5568
5569 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5570
5571 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5572 symbol_matcher,
5573 kind, [&] (offset_type namei)
5574 {
5575 /* The name was matched, now expand corresponding CUs that were
5576 marked. */
5577 dw2_debug_names_iterator iter (map, kind, namei);
5578
5579 struct dwarf2_per_cu_data *per_cu;
5580 while ((per_cu = iter.next ()) != NULL)
5581 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5582 expansion_notify);
5583 return true;
5584 });
5585 }
5586
5587 const struct quick_symbol_functions dwarf2_debug_names_functions =
5588 {
5589 dw2_has_symbols,
5590 dw2_find_last_source_symtab,
5591 dw2_forget_cached_source_info,
5592 dw2_map_symtabs_matching_filename,
5593 dw2_debug_names_lookup_symbol,
5594 NULL,
5595 dw2_print_stats,
5596 dw2_debug_names_dump,
5597 dw2_debug_names_expand_symtabs_for_function,
5598 dw2_expand_all_symtabs,
5599 dw2_expand_symtabs_with_fullname,
5600 dw2_debug_names_map_matching_symbols,
5601 dw2_debug_names_expand_symtabs_matching,
5602 dw2_find_pc_sect_compunit_symtab,
5603 NULL,
5604 dw2_map_symbol_filenames
5605 };
5606
5607 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5608 to either a dwarf2_per_objfile or dwz_file object. */
5609
5610 template <typename T>
5611 static gdb::array_view<const gdb_byte>
5612 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5613 {
5614 dwarf2_section_info *section = &section_owner->gdb_index;
5615
5616 if (section->empty ())
5617 return {};
5618
5619 /* Older elfutils strip versions could keep the section in the main
5620 executable while splitting it for the separate debug info file. */
5621 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5622 return {};
5623
5624 section->read (obj);
5625
5626 /* dwarf2_section_info::size is a bfd_size_type, while
5627 gdb::array_view works with size_t. On 32-bit hosts, with
5628 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5629 is 32-bit. So we need an explicit narrowing conversion here.
5630 This is fine, because it's impossible to allocate or mmap an
5631 array/buffer larger than what size_t can represent. */
5632 return gdb::make_array_view (section->buffer, section->size);
5633 }
5634
5635 /* Lookup the index cache for the contents of the index associated to
5636 DWARF2_OBJ. */
5637
5638 static gdb::array_view<const gdb_byte>
5639 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5640 {
5641 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5642 if (build_id == nullptr)
5643 return {};
5644
5645 return global_index_cache.lookup_gdb_index (build_id,
5646 &dwarf2_obj->index_cache_res);
5647 }
5648
5649 /* Same as the above, but for DWZ. */
5650
5651 static gdb::array_view<const gdb_byte>
5652 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5653 {
5654 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5655 if (build_id == nullptr)
5656 return {};
5657
5658 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5659 }
5660
5661 /* See symfile.h. */
5662
5663 bool
5664 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5665 {
5666 struct dwarf2_per_objfile *dwarf2_per_objfile
5667 = get_dwarf2_per_objfile (objfile);
5668
5669 /* If we're about to read full symbols, don't bother with the
5670 indices. In this case we also don't care if some other debug
5671 format is making psymtabs, because they are all about to be
5672 expanded anyway. */
5673 if ((objfile->flags & OBJF_READNOW))
5674 {
5675 dwarf2_per_objfile->using_index = 1;
5676 create_all_comp_units (dwarf2_per_objfile);
5677 create_all_type_units (dwarf2_per_objfile);
5678 dwarf2_per_objfile->quick_file_names_table
5679 = create_quick_file_names_table
5680 (dwarf2_per_objfile->all_comp_units.size ());
5681
5682 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5683 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5684 {
5685 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5686
5687 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5688 struct dwarf2_per_cu_quick_data);
5689 }
5690
5691 /* Return 1 so that gdb sees the "quick" functions. However,
5692 these functions will be no-ops because we will have expanded
5693 all symtabs. */
5694 *index_kind = dw_index_kind::GDB_INDEX;
5695 return true;
5696 }
5697
5698 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5699 {
5700 *index_kind = dw_index_kind::DEBUG_NAMES;
5701 return true;
5702 }
5703
5704 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5705 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5706 get_gdb_index_contents_from_section<dwz_file>))
5707 {
5708 *index_kind = dw_index_kind::GDB_INDEX;
5709 return true;
5710 }
5711
5712 /* ... otherwise, try to find the index in the index cache. */
5713 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5714 get_gdb_index_contents_from_cache,
5715 get_gdb_index_contents_from_cache_dwz))
5716 {
5717 global_index_cache.hit ();
5718 *index_kind = dw_index_kind::GDB_INDEX;
5719 return true;
5720 }
5721
5722 global_index_cache.miss ();
5723 return false;
5724 }
5725
5726 \f
5727
5728 /* Build a partial symbol table. */
5729
5730 void
5731 dwarf2_build_psymtabs (struct objfile *objfile)
5732 {
5733 struct dwarf2_per_objfile *dwarf2_per_objfile
5734 = get_dwarf2_per_objfile (objfile);
5735
5736 init_psymbol_list (objfile, 1024);
5737
5738 try
5739 {
5740 /* This isn't really ideal: all the data we allocate on the
5741 objfile's obstack is still uselessly kept around. However,
5742 freeing it seems unsafe. */
5743 psymtab_discarder psymtabs (objfile);
5744 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5745 psymtabs.keep ();
5746
5747 /* (maybe) store an index in the cache. */
5748 global_index_cache.store (dwarf2_per_objfile);
5749 }
5750 catch (const gdb_exception_error &except)
5751 {
5752 exception_print (gdb_stderr, except);
5753 }
5754 }
5755
5756 /* Find the base address of the compilation unit for range lists and
5757 location lists. It will normally be specified by DW_AT_low_pc.
5758 In DWARF-3 draft 4, the base address could be overridden by
5759 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5760 compilation units with discontinuous ranges. */
5761
5762 static void
5763 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5764 {
5765 struct attribute *attr;
5766
5767 cu->base_address.reset ();
5768
5769 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5770 if (attr != nullptr)
5771 cu->base_address = attr->value_as_address ();
5772 else
5773 {
5774 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5775 if (attr != nullptr)
5776 cu->base_address = attr->value_as_address ();
5777 }
5778 }
5779
5780 /* Helper function that returns the proper abbrev section for
5781 THIS_CU. */
5782
5783 static struct dwarf2_section_info *
5784 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5785 {
5786 struct dwarf2_section_info *abbrev;
5787 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5788
5789 if (this_cu->is_dwz)
5790 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5791 else
5792 abbrev = &dwarf2_per_objfile->abbrev;
5793
5794 return abbrev;
5795 }
5796
5797 /* Fetch the abbreviation table offset from a comp or type unit header. */
5798
5799 static sect_offset
5800 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5801 struct dwarf2_section_info *section,
5802 sect_offset sect_off)
5803 {
5804 bfd *abfd = section->get_bfd_owner ();
5805 const gdb_byte *info_ptr;
5806 unsigned int initial_length_size, offset_size;
5807 uint16_t version;
5808
5809 section->read (dwarf2_per_objfile->objfile);
5810 info_ptr = section->buffer + to_underlying (sect_off);
5811 read_initial_length (abfd, info_ptr, &initial_length_size);
5812 offset_size = initial_length_size == 4 ? 4 : 8;
5813 info_ptr += initial_length_size;
5814
5815 version = read_2_bytes (abfd, info_ptr);
5816 info_ptr += 2;
5817 if (version >= 5)
5818 {
5819 /* Skip unit type and address size. */
5820 info_ptr += 2;
5821 }
5822
5823 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5824 }
5825
5826 /* A partial symtab that is used only for include files. */
5827 struct dwarf2_include_psymtab : public partial_symtab
5828 {
5829 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5830 : partial_symtab (filename, objfile)
5831 {
5832 }
5833
5834 void read_symtab (struct objfile *objfile) override
5835 {
5836 expand_psymtab (objfile);
5837 }
5838
5839 void expand_psymtab (struct objfile *objfile) override
5840 {
5841 if (m_readin)
5842 return;
5843 /* It's an include file, no symbols to read for it.
5844 Everything is in the parent symtab. */
5845 expand_dependencies (objfile);
5846 m_readin = true;
5847 }
5848
5849 bool readin_p () const override
5850 {
5851 return m_readin;
5852 }
5853
5854 struct compunit_symtab *get_compunit_symtab () const override
5855 {
5856 return nullptr;
5857 }
5858
5859 private:
5860
5861 bool m_readin = false;
5862 };
5863
5864 /* Allocate a new partial symtab for file named NAME and mark this new
5865 partial symtab as being an include of PST. */
5866
5867 static void
5868 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5869 struct objfile *objfile)
5870 {
5871 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5872
5873 if (!IS_ABSOLUTE_PATH (subpst->filename))
5874 {
5875 /* It shares objfile->objfile_obstack. */
5876 subpst->dirname = pst->dirname;
5877 }
5878
5879 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5880 subpst->dependencies[0] = pst;
5881 subpst->number_of_dependencies = 1;
5882 }
5883
5884 /* Read the Line Number Program data and extract the list of files
5885 included by the source file represented by PST. Build an include
5886 partial symtab for each of these included files. */
5887
5888 static void
5889 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5890 struct die_info *die,
5891 dwarf2_psymtab *pst)
5892 {
5893 line_header_up lh;
5894 struct attribute *attr;
5895
5896 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5897 if (attr != nullptr)
5898 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5899 if (lh == NULL)
5900 return; /* No linetable, so no includes. */
5901
5902 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5903 that we pass in the raw text_low here; that is ok because we're
5904 only decoding the line table to make include partial symtabs, and
5905 so the addresses aren't really used. */
5906 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5907 pst->raw_text_low (), 1);
5908 }
5909
5910 static hashval_t
5911 hash_signatured_type (const void *item)
5912 {
5913 const struct signatured_type *sig_type
5914 = (const struct signatured_type *) item;
5915
5916 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5917 return sig_type->signature;
5918 }
5919
5920 static int
5921 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5922 {
5923 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5924 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5925
5926 return lhs->signature == rhs->signature;
5927 }
5928
5929 /* Allocate a hash table for signatured types. */
5930
5931 static htab_up
5932 allocate_signatured_type_table ()
5933 {
5934 return htab_up (htab_create_alloc (41,
5935 hash_signatured_type,
5936 eq_signatured_type,
5937 NULL, xcalloc, xfree));
5938 }
5939
5940 /* A helper function to add a signatured type CU to a table. */
5941
5942 static int
5943 add_signatured_type_cu_to_table (void **slot, void *datum)
5944 {
5945 struct signatured_type *sigt = (struct signatured_type *) *slot;
5946 std::vector<signatured_type *> *all_type_units
5947 = (std::vector<signatured_type *> *) datum;
5948
5949 all_type_units->push_back (sigt);
5950
5951 return 1;
5952 }
5953
5954 /* A helper for create_debug_types_hash_table. Read types from SECTION
5955 and fill them into TYPES_HTAB. It will process only type units,
5956 therefore DW_UT_type. */
5957
5958 static void
5959 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
5960 struct dwo_file *dwo_file,
5961 dwarf2_section_info *section, htab_up &types_htab,
5962 rcuh_kind section_kind)
5963 {
5964 struct objfile *objfile = dwarf2_per_objfile->objfile;
5965 struct dwarf2_section_info *abbrev_section;
5966 bfd *abfd;
5967 const gdb_byte *info_ptr, *end_ptr;
5968
5969 abbrev_section = (dwo_file != NULL
5970 ? &dwo_file->sections.abbrev
5971 : &dwarf2_per_objfile->abbrev);
5972
5973 if (dwarf_read_debug)
5974 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
5975 section->get_name (),
5976 abbrev_section->get_file_name ());
5977
5978 section->read (objfile);
5979 info_ptr = section->buffer;
5980
5981 if (info_ptr == NULL)
5982 return;
5983
5984 /* We can't set abfd until now because the section may be empty or
5985 not present, in which case the bfd is unknown. */
5986 abfd = section->get_bfd_owner ();
5987
5988 /* We don't use cutu_reader here because we don't need to read
5989 any dies: the signature is in the header. */
5990
5991 end_ptr = info_ptr + section->size;
5992 while (info_ptr < end_ptr)
5993 {
5994 struct signatured_type *sig_type;
5995 struct dwo_unit *dwo_tu;
5996 void **slot;
5997 const gdb_byte *ptr = info_ptr;
5998 struct comp_unit_head header;
5999 unsigned int length;
6000
6001 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6002
6003 /* Initialize it due to a false compiler warning. */
6004 header.signature = -1;
6005 header.type_cu_offset_in_tu = (cu_offset) -1;
6006
6007 /* We need to read the type's signature in order to build the hash
6008 table, but we don't need anything else just yet. */
6009
6010 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6011 abbrev_section, ptr, section_kind);
6012
6013 length = header.get_length ();
6014
6015 /* Skip dummy type units. */
6016 if (ptr >= info_ptr + length
6017 || peek_abbrev_code (abfd, ptr) == 0
6018 || header.unit_type != DW_UT_type)
6019 {
6020 info_ptr += length;
6021 continue;
6022 }
6023
6024 if (types_htab == NULL)
6025 {
6026 if (dwo_file)
6027 types_htab = allocate_dwo_unit_table ();
6028 else
6029 types_htab = allocate_signatured_type_table ();
6030 }
6031
6032 if (dwo_file)
6033 {
6034 sig_type = NULL;
6035 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6036 struct dwo_unit);
6037 dwo_tu->dwo_file = dwo_file;
6038 dwo_tu->signature = header.signature;
6039 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6040 dwo_tu->section = section;
6041 dwo_tu->sect_off = sect_off;
6042 dwo_tu->length = length;
6043 }
6044 else
6045 {
6046 /* N.B.: type_offset is not usable if this type uses a DWO file.
6047 The real type_offset is in the DWO file. */
6048 dwo_tu = NULL;
6049 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6050 struct signatured_type);
6051 sig_type->signature = header.signature;
6052 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6053 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6054 sig_type->per_cu.is_debug_types = 1;
6055 sig_type->per_cu.section = section;
6056 sig_type->per_cu.sect_off = sect_off;
6057 sig_type->per_cu.length = length;
6058 }
6059
6060 slot = htab_find_slot (types_htab.get (),
6061 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6062 INSERT);
6063 gdb_assert (slot != NULL);
6064 if (*slot != NULL)
6065 {
6066 sect_offset dup_sect_off;
6067
6068 if (dwo_file)
6069 {
6070 const struct dwo_unit *dup_tu
6071 = (const struct dwo_unit *) *slot;
6072
6073 dup_sect_off = dup_tu->sect_off;
6074 }
6075 else
6076 {
6077 const struct signatured_type *dup_tu
6078 = (const struct signatured_type *) *slot;
6079
6080 dup_sect_off = dup_tu->per_cu.sect_off;
6081 }
6082
6083 complaint (_("debug type entry at offset %s is duplicate to"
6084 " the entry at offset %s, signature %s"),
6085 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6086 hex_string (header.signature));
6087 }
6088 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6089
6090 if (dwarf_read_debug > 1)
6091 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6092 sect_offset_str (sect_off),
6093 hex_string (header.signature));
6094
6095 info_ptr += length;
6096 }
6097 }
6098
6099 /* Create the hash table of all entries in the .debug_types
6100 (or .debug_types.dwo) section(s).
6101 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6102 otherwise it is NULL.
6103
6104 The result is a pointer to the hash table or NULL if there are no types.
6105
6106 Note: This function processes DWO files only, not DWP files. */
6107
6108 static void
6109 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6110 struct dwo_file *dwo_file,
6111 gdb::array_view<dwarf2_section_info> type_sections,
6112 htab_up &types_htab)
6113 {
6114 for (dwarf2_section_info &section : type_sections)
6115 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6116 types_htab, rcuh_kind::TYPE);
6117 }
6118
6119 /* Create the hash table of all entries in the .debug_types section,
6120 and initialize all_type_units.
6121 The result is zero if there is an error (e.g. missing .debug_types section),
6122 otherwise non-zero. */
6123
6124 static int
6125 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6126 {
6127 htab_up types_htab;
6128
6129 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6130 &dwarf2_per_objfile->info, types_htab,
6131 rcuh_kind::COMPILE);
6132 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6133 dwarf2_per_objfile->types, types_htab);
6134 if (types_htab == NULL)
6135 {
6136 dwarf2_per_objfile->signatured_types = NULL;
6137 return 0;
6138 }
6139
6140 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6141
6142 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6143 dwarf2_per_objfile->all_type_units.reserve
6144 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6145
6146 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6147 add_signatured_type_cu_to_table,
6148 &dwarf2_per_objfile->all_type_units);
6149
6150 return 1;
6151 }
6152
6153 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6154 If SLOT is non-NULL, it is the entry to use in the hash table.
6155 Otherwise we find one. */
6156
6157 static struct signatured_type *
6158 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6159 void **slot)
6160 {
6161 struct objfile *objfile = dwarf2_per_objfile->objfile;
6162
6163 if (dwarf2_per_objfile->all_type_units.size ()
6164 == dwarf2_per_objfile->all_type_units.capacity ())
6165 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6166
6167 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6168 struct signatured_type);
6169
6170 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6171 sig_type->signature = sig;
6172 sig_type->per_cu.is_debug_types = 1;
6173 if (dwarf2_per_objfile->using_index)
6174 {
6175 sig_type->per_cu.v.quick =
6176 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6177 struct dwarf2_per_cu_quick_data);
6178 }
6179
6180 if (slot == NULL)
6181 {
6182 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6183 sig_type, INSERT);
6184 }
6185 gdb_assert (*slot == NULL);
6186 *slot = sig_type;
6187 /* The rest of sig_type must be filled in by the caller. */
6188 return sig_type;
6189 }
6190
6191 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6192 Fill in SIG_ENTRY with DWO_ENTRY. */
6193
6194 static void
6195 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6196 struct signatured_type *sig_entry,
6197 struct dwo_unit *dwo_entry)
6198 {
6199 /* Make sure we're not clobbering something we don't expect to. */
6200 gdb_assert (! sig_entry->per_cu.queued);
6201 gdb_assert (sig_entry->per_cu.cu == NULL);
6202 if (dwarf2_per_objfile->using_index)
6203 {
6204 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6205 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6206 }
6207 else
6208 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6209 gdb_assert (sig_entry->signature == dwo_entry->signature);
6210 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6211 gdb_assert (sig_entry->type_unit_group == NULL);
6212 gdb_assert (sig_entry->dwo_unit == NULL);
6213
6214 sig_entry->per_cu.section = dwo_entry->section;
6215 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6216 sig_entry->per_cu.length = dwo_entry->length;
6217 sig_entry->per_cu.reading_dwo_directly = 1;
6218 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6219 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6220 sig_entry->dwo_unit = dwo_entry;
6221 }
6222
6223 /* Subroutine of lookup_signatured_type.
6224 If we haven't read the TU yet, create the signatured_type data structure
6225 for a TU to be read in directly from a DWO file, bypassing the stub.
6226 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6227 using .gdb_index, then when reading a CU we want to stay in the DWO file
6228 containing that CU. Otherwise we could end up reading several other DWO
6229 files (due to comdat folding) to process the transitive closure of all the
6230 mentioned TUs, and that can be slow. The current DWO file will have every
6231 type signature that it needs.
6232 We only do this for .gdb_index because in the psymtab case we already have
6233 to read all the DWOs to build the type unit groups. */
6234
6235 static struct signatured_type *
6236 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6237 {
6238 struct dwarf2_per_objfile *dwarf2_per_objfile
6239 = cu->per_cu->dwarf2_per_objfile;
6240 struct dwo_file *dwo_file;
6241 struct dwo_unit find_dwo_entry, *dwo_entry;
6242 struct signatured_type find_sig_entry, *sig_entry;
6243 void **slot;
6244
6245 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6246
6247 /* If TU skeletons have been removed then we may not have read in any
6248 TUs yet. */
6249 if (dwarf2_per_objfile->signatured_types == NULL)
6250 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6251
6252 /* We only ever need to read in one copy of a signatured type.
6253 Use the global signatured_types array to do our own comdat-folding
6254 of types. If this is the first time we're reading this TU, and
6255 the TU has an entry in .gdb_index, replace the recorded data from
6256 .gdb_index with this TU. */
6257
6258 find_sig_entry.signature = sig;
6259 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6260 &find_sig_entry, INSERT);
6261 sig_entry = (struct signatured_type *) *slot;
6262
6263 /* We can get here with the TU already read, *or* in the process of being
6264 read. Don't reassign the global entry to point to this DWO if that's
6265 the case. Also note that if the TU is already being read, it may not
6266 have come from a DWO, the program may be a mix of Fission-compiled
6267 code and non-Fission-compiled code. */
6268
6269 /* Have we already tried to read this TU?
6270 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6271 needn't exist in the global table yet). */
6272 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6273 return sig_entry;
6274
6275 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6276 dwo_unit of the TU itself. */
6277 dwo_file = cu->dwo_unit->dwo_file;
6278
6279 /* Ok, this is the first time we're reading this TU. */
6280 if (dwo_file->tus == NULL)
6281 return NULL;
6282 find_dwo_entry.signature = sig;
6283 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6284 &find_dwo_entry);
6285 if (dwo_entry == NULL)
6286 return NULL;
6287
6288 /* If the global table doesn't have an entry for this TU, add one. */
6289 if (sig_entry == NULL)
6290 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6291
6292 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6293 sig_entry->per_cu.tu_read = 1;
6294 return sig_entry;
6295 }
6296
6297 /* Subroutine of lookup_signatured_type.
6298 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6299 then try the DWP file. If the TU stub (skeleton) has been removed then
6300 it won't be in .gdb_index. */
6301
6302 static struct signatured_type *
6303 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6304 {
6305 struct dwarf2_per_objfile *dwarf2_per_objfile
6306 = cu->per_cu->dwarf2_per_objfile;
6307 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6308 struct dwo_unit *dwo_entry;
6309 struct signatured_type find_sig_entry, *sig_entry;
6310 void **slot;
6311
6312 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6313 gdb_assert (dwp_file != NULL);
6314
6315 /* If TU skeletons have been removed then we may not have read in any
6316 TUs yet. */
6317 if (dwarf2_per_objfile->signatured_types == NULL)
6318 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6319
6320 find_sig_entry.signature = sig;
6321 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6322 &find_sig_entry, INSERT);
6323 sig_entry = (struct signatured_type *) *slot;
6324
6325 /* Have we already tried to read this TU?
6326 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6327 needn't exist in the global table yet). */
6328 if (sig_entry != NULL)
6329 return sig_entry;
6330
6331 if (dwp_file->tus == NULL)
6332 return NULL;
6333 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6334 sig, 1 /* is_debug_types */);
6335 if (dwo_entry == NULL)
6336 return NULL;
6337
6338 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6339 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6340
6341 return sig_entry;
6342 }
6343
6344 /* Lookup a signature based type for DW_FORM_ref_sig8.
6345 Returns NULL if signature SIG is not present in the table.
6346 It is up to the caller to complain about this. */
6347
6348 static struct signatured_type *
6349 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6350 {
6351 struct dwarf2_per_objfile *dwarf2_per_objfile
6352 = cu->per_cu->dwarf2_per_objfile;
6353
6354 if (cu->dwo_unit
6355 && dwarf2_per_objfile->using_index)
6356 {
6357 /* We're in a DWO/DWP file, and we're using .gdb_index.
6358 These cases require special processing. */
6359 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6360 return lookup_dwo_signatured_type (cu, sig);
6361 else
6362 return lookup_dwp_signatured_type (cu, sig);
6363 }
6364 else
6365 {
6366 struct signatured_type find_entry, *entry;
6367
6368 if (dwarf2_per_objfile->signatured_types == NULL)
6369 return NULL;
6370 find_entry.signature = sig;
6371 entry = ((struct signatured_type *)
6372 htab_find (dwarf2_per_objfile->signatured_types.get (),
6373 &find_entry));
6374 return entry;
6375 }
6376 }
6377
6378 /* Low level DIE reading support. */
6379
6380 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6381
6382 static void
6383 init_cu_die_reader (struct die_reader_specs *reader,
6384 struct dwarf2_cu *cu,
6385 struct dwarf2_section_info *section,
6386 struct dwo_file *dwo_file,
6387 struct abbrev_table *abbrev_table)
6388 {
6389 gdb_assert (section->readin && section->buffer != NULL);
6390 reader->abfd = section->get_bfd_owner ();
6391 reader->cu = cu;
6392 reader->dwo_file = dwo_file;
6393 reader->die_section = section;
6394 reader->buffer = section->buffer;
6395 reader->buffer_end = section->buffer + section->size;
6396 reader->abbrev_table = abbrev_table;
6397 }
6398
6399 /* Subroutine of cutu_reader to simplify it.
6400 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6401 There's just a lot of work to do, and cutu_reader is big enough
6402 already.
6403
6404 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6405 from it to the DIE in the DWO. If NULL we are skipping the stub.
6406 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6407 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6408 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6409 STUB_COMP_DIR may be non-NULL.
6410 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6411 are filled in with the info of the DIE from the DWO file.
6412 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6413 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6414 kept around for at least as long as *RESULT_READER.
6415
6416 The result is non-zero if a valid (non-dummy) DIE was found. */
6417
6418 static int
6419 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6420 struct dwo_unit *dwo_unit,
6421 struct die_info *stub_comp_unit_die,
6422 const char *stub_comp_dir,
6423 struct die_reader_specs *result_reader,
6424 const gdb_byte **result_info_ptr,
6425 struct die_info **result_comp_unit_die,
6426 abbrev_table_up *result_dwo_abbrev_table)
6427 {
6428 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6429 struct objfile *objfile = dwarf2_per_objfile->objfile;
6430 struct dwarf2_cu *cu = this_cu->cu;
6431 bfd *abfd;
6432 const gdb_byte *begin_info_ptr, *info_ptr;
6433 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6434 int i,num_extra_attrs;
6435 struct dwarf2_section_info *dwo_abbrev_section;
6436 struct die_info *comp_unit_die;
6437
6438 /* At most one of these may be provided. */
6439 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6440
6441 /* These attributes aren't processed until later:
6442 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6443 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6444 referenced later. However, these attributes are found in the stub
6445 which we won't have later. In order to not impose this complication
6446 on the rest of the code, we read them here and copy them to the
6447 DWO CU/TU die. */
6448
6449 stmt_list = NULL;
6450 low_pc = NULL;
6451 high_pc = NULL;
6452 ranges = NULL;
6453 comp_dir = NULL;
6454
6455 if (stub_comp_unit_die != NULL)
6456 {
6457 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6458 DWO file. */
6459 if (! this_cu->is_debug_types)
6460 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6461 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6462 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6463 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6464 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6465
6466 cu->addr_base = stub_comp_unit_die->addr_base ();
6467
6468 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6469 here (if needed). We need the value before we can process
6470 DW_AT_ranges. */
6471 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6472 }
6473 else if (stub_comp_dir != NULL)
6474 {
6475 /* Reconstruct the comp_dir attribute to simplify the code below. */
6476 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6477 comp_dir->name = DW_AT_comp_dir;
6478 comp_dir->form = DW_FORM_string;
6479 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6480 DW_STRING (comp_dir) = stub_comp_dir;
6481 }
6482
6483 /* Set up for reading the DWO CU/TU. */
6484 cu->dwo_unit = dwo_unit;
6485 dwarf2_section_info *section = dwo_unit->section;
6486 section->read (objfile);
6487 abfd = section->get_bfd_owner ();
6488 begin_info_ptr = info_ptr = (section->buffer
6489 + to_underlying (dwo_unit->sect_off));
6490 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6491
6492 if (this_cu->is_debug_types)
6493 {
6494 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6495
6496 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6497 &cu->header, section,
6498 dwo_abbrev_section,
6499 info_ptr, rcuh_kind::TYPE);
6500 /* This is not an assert because it can be caused by bad debug info. */
6501 if (sig_type->signature != cu->header.signature)
6502 {
6503 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6504 " TU at offset %s [in module %s]"),
6505 hex_string (sig_type->signature),
6506 hex_string (cu->header.signature),
6507 sect_offset_str (dwo_unit->sect_off),
6508 bfd_get_filename (abfd));
6509 }
6510 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6511 /* For DWOs coming from DWP files, we don't know the CU length
6512 nor the type's offset in the TU until now. */
6513 dwo_unit->length = cu->header.get_length ();
6514 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6515
6516 /* Establish the type offset that can be used to lookup the type.
6517 For DWO files, we don't know it until now. */
6518 sig_type->type_offset_in_section
6519 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6520 }
6521 else
6522 {
6523 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6524 &cu->header, section,
6525 dwo_abbrev_section,
6526 info_ptr, rcuh_kind::COMPILE);
6527 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6528 /* For DWOs coming from DWP files, we don't know the CU length
6529 until now. */
6530 dwo_unit->length = cu->header.get_length ();
6531 }
6532
6533 *result_dwo_abbrev_table
6534 = abbrev_table::read (objfile, dwo_abbrev_section,
6535 cu->header.abbrev_sect_off);
6536 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6537 result_dwo_abbrev_table->get ());
6538
6539 /* Read in the die, but leave space to copy over the attributes
6540 from the stub. This has the benefit of simplifying the rest of
6541 the code - all the work to maintain the illusion of a single
6542 DW_TAG_{compile,type}_unit DIE is done here. */
6543 num_extra_attrs = ((stmt_list != NULL)
6544 + (low_pc != NULL)
6545 + (high_pc != NULL)
6546 + (ranges != NULL)
6547 + (comp_dir != NULL));
6548 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6549 num_extra_attrs);
6550
6551 /* Copy over the attributes from the stub to the DIE we just read in. */
6552 comp_unit_die = *result_comp_unit_die;
6553 i = comp_unit_die->num_attrs;
6554 if (stmt_list != NULL)
6555 comp_unit_die->attrs[i++] = *stmt_list;
6556 if (low_pc != NULL)
6557 comp_unit_die->attrs[i++] = *low_pc;
6558 if (high_pc != NULL)
6559 comp_unit_die->attrs[i++] = *high_pc;
6560 if (ranges != NULL)
6561 comp_unit_die->attrs[i++] = *ranges;
6562 if (comp_dir != NULL)
6563 comp_unit_die->attrs[i++] = *comp_dir;
6564 comp_unit_die->num_attrs += num_extra_attrs;
6565
6566 if (dwarf_die_debug)
6567 {
6568 fprintf_unfiltered (gdb_stdlog,
6569 "Read die from %s@0x%x of %s:\n",
6570 section->get_name (),
6571 (unsigned) (begin_info_ptr - section->buffer),
6572 bfd_get_filename (abfd));
6573 dump_die (comp_unit_die, dwarf_die_debug);
6574 }
6575
6576 /* Skip dummy compilation units. */
6577 if (info_ptr >= begin_info_ptr + dwo_unit->length
6578 || peek_abbrev_code (abfd, info_ptr) == 0)
6579 return 0;
6580
6581 *result_info_ptr = info_ptr;
6582 return 1;
6583 }
6584
6585 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6586 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6587 signature is part of the header. */
6588 static gdb::optional<ULONGEST>
6589 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6590 {
6591 if (cu->header.version >= 5)
6592 return cu->header.signature;
6593 struct attribute *attr;
6594 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6595 if (attr == nullptr)
6596 return gdb::optional<ULONGEST> ();
6597 return DW_UNSND (attr);
6598 }
6599
6600 /* Subroutine of cutu_reader to simplify it.
6601 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6602 Returns NULL if the specified DWO unit cannot be found. */
6603
6604 static struct dwo_unit *
6605 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6606 struct die_info *comp_unit_die,
6607 const char *dwo_name)
6608 {
6609 struct dwarf2_cu *cu = this_cu->cu;
6610 struct dwo_unit *dwo_unit;
6611 const char *comp_dir;
6612
6613 gdb_assert (cu != NULL);
6614
6615 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6616 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6617 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6618
6619 if (this_cu->is_debug_types)
6620 {
6621 struct signatured_type *sig_type;
6622
6623 /* Since this_cu is the first member of struct signatured_type,
6624 we can go from a pointer to one to a pointer to the other. */
6625 sig_type = (struct signatured_type *) this_cu;
6626 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6627 }
6628 else
6629 {
6630 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6631 if (!signature.has_value ())
6632 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6633 " [in module %s]"),
6634 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6635 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6636 *signature);
6637 }
6638
6639 return dwo_unit;
6640 }
6641
6642 /* Subroutine of cutu_reader to simplify it.
6643 See it for a description of the parameters.
6644 Read a TU directly from a DWO file, bypassing the stub. */
6645
6646 void
6647 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6648 int use_existing_cu)
6649 {
6650 struct signatured_type *sig_type;
6651
6652 /* Verify we can do the following downcast, and that we have the
6653 data we need. */
6654 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6655 sig_type = (struct signatured_type *) this_cu;
6656 gdb_assert (sig_type->dwo_unit != NULL);
6657
6658 if (use_existing_cu && this_cu->cu != NULL)
6659 {
6660 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6661 /* There's no need to do the rereading_dwo_cu handling that
6662 cutu_reader does since we don't read the stub. */
6663 }
6664 else
6665 {
6666 /* If !use_existing_cu, this_cu->cu must be NULL. */
6667 gdb_assert (this_cu->cu == NULL);
6668 m_new_cu.reset (new dwarf2_cu (this_cu));
6669 }
6670
6671 /* A future optimization, if needed, would be to use an existing
6672 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6673 could share abbrev tables. */
6674
6675 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6676 NULL /* stub_comp_unit_die */,
6677 sig_type->dwo_unit->dwo_file->comp_dir,
6678 this, &info_ptr,
6679 &comp_unit_die,
6680 &m_dwo_abbrev_table) == 0)
6681 {
6682 /* Dummy die. */
6683 dummy_p = true;
6684 }
6685 }
6686
6687 /* Initialize a CU (or TU) and read its DIEs.
6688 If the CU defers to a DWO file, read the DWO file as well.
6689
6690 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6691 Otherwise the table specified in the comp unit header is read in and used.
6692 This is an optimization for when we already have the abbrev table.
6693
6694 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6695 Otherwise, a new CU is allocated with xmalloc. */
6696
6697 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6698 struct abbrev_table *abbrev_table,
6699 int use_existing_cu,
6700 bool skip_partial)
6701 : die_reader_specs {},
6702 m_this_cu (this_cu)
6703 {
6704 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6705 struct objfile *objfile = dwarf2_per_objfile->objfile;
6706 struct dwarf2_section_info *section = this_cu->section;
6707 bfd *abfd = section->get_bfd_owner ();
6708 struct dwarf2_cu *cu;
6709 const gdb_byte *begin_info_ptr;
6710 struct signatured_type *sig_type = NULL;
6711 struct dwarf2_section_info *abbrev_section;
6712 /* Non-zero if CU currently points to a DWO file and we need to
6713 reread it. When this happens we need to reread the skeleton die
6714 before we can reread the DWO file (this only applies to CUs, not TUs). */
6715 int rereading_dwo_cu = 0;
6716
6717 if (dwarf_die_debug)
6718 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6719 this_cu->is_debug_types ? "type" : "comp",
6720 sect_offset_str (this_cu->sect_off));
6721
6722 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6723 file (instead of going through the stub), short-circuit all of this. */
6724 if (this_cu->reading_dwo_directly)
6725 {
6726 /* Narrow down the scope of possibilities to have to understand. */
6727 gdb_assert (this_cu->is_debug_types);
6728 gdb_assert (abbrev_table == NULL);
6729 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6730 return;
6731 }
6732
6733 /* This is cheap if the section is already read in. */
6734 section->read (objfile);
6735
6736 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6737
6738 abbrev_section = get_abbrev_section_for_cu (this_cu);
6739
6740 if (use_existing_cu && this_cu->cu != NULL)
6741 {
6742 cu = this_cu->cu;
6743 /* If this CU is from a DWO file we need to start over, we need to
6744 refetch the attributes from the skeleton CU.
6745 This could be optimized by retrieving those attributes from when we
6746 were here the first time: the previous comp_unit_die was stored in
6747 comp_unit_obstack. But there's no data yet that we need this
6748 optimization. */
6749 if (cu->dwo_unit != NULL)
6750 rereading_dwo_cu = 1;
6751 }
6752 else
6753 {
6754 /* If !use_existing_cu, this_cu->cu must be NULL. */
6755 gdb_assert (this_cu->cu == NULL);
6756 m_new_cu.reset (new dwarf2_cu (this_cu));
6757 cu = m_new_cu.get ();
6758 }
6759
6760 /* Get the header. */
6761 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6762 {
6763 /* We already have the header, there's no need to read it in again. */
6764 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6765 }
6766 else
6767 {
6768 if (this_cu->is_debug_types)
6769 {
6770 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6771 &cu->header, section,
6772 abbrev_section, info_ptr,
6773 rcuh_kind::TYPE);
6774
6775 /* Since per_cu is the first member of struct signatured_type,
6776 we can go from a pointer to one to a pointer to the other. */
6777 sig_type = (struct signatured_type *) this_cu;
6778 gdb_assert (sig_type->signature == cu->header.signature);
6779 gdb_assert (sig_type->type_offset_in_tu
6780 == cu->header.type_cu_offset_in_tu);
6781 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6782
6783 /* LENGTH has not been set yet for type units if we're
6784 using .gdb_index. */
6785 this_cu->length = cu->header.get_length ();
6786
6787 /* Establish the type offset that can be used to lookup the type. */
6788 sig_type->type_offset_in_section =
6789 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6790
6791 this_cu->dwarf_version = cu->header.version;
6792 }
6793 else
6794 {
6795 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6796 &cu->header, section,
6797 abbrev_section,
6798 info_ptr,
6799 rcuh_kind::COMPILE);
6800
6801 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6802 gdb_assert (this_cu->length == cu->header.get_length ());
6803 this_cu->dwarf_version = cu->header.version;
6804 }
6805 }
6806
6807 /* Skip dummy compilation units. */
6808 if (info_ptr >= begin_info_ptr + this_cu->length
6809 || peek_abbrev_code (abfd, info_ptr) == 0)
6810 {
6811 dummy_p = true;
6812 return;
6813 }
6814
6815 /* If we don't have them yet, read the abbrevs for this compilation unit.
6816 And if we need to read them now, make sure they're freed when we're
6817 done. */
6818 if (abbrev_table != NULL)
6819 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6820 else
6821 {
6822 m_abbrev_table_holder
6823 = abbrev_table::read (objfile, abbrev_section,
6824 cu->header.abbrev_sect_off);
6825 abbrev_table = m_abbrev_table_holder.get ();
6826 }
6827
6828 /* Read the top level CU/TU die. */
6829 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6830 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6831
6832 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6833 {
6834 dummy_p = true;
6835 return;
6836 }
6837
6838 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6839 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6840 table from the DWO file and pass the ownership over to us. It will be
6841 referenced from READER, so we must make sure to free it after we're done
6842 with READER.
6843
6844 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6845 DWO CU, that this test will fail (the attribute will not be present). */
6846 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6847 if (dwo_name != nullptr)
6848 {
6849 struct dwo_unit *dwo_unit;
6850 struct die_info *dwo_comp_unit_die;
6851
6852 if (comp_unit_die->has_children)
6853 {
6854 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6855 " has children (offset %s) [in module %s]"),
6856 sect_offset_str (this_cu->sect_off),
6857 bfd_get_filename (abfd));
6858 }
6859 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6860 if (dwo_unit != NULL)
6861 {
6862 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6863 comp_unit_die, NULL,
6864 this, &info_ptr,
6865 &dwo_comp_unit_die,
6866 &m_dwo_abbrev_table) == 0)
6867 {
6868 /* Dummy die. */
6869 dummy_p = true;
6870 return;
6871 }
6872 comp_unit_die = dwo_comp_unit_die;
6873 }
6874 else
6875 {
6876 /* Yikes, we couldn't find the rest of the DIE, we only have
6877 the stub. A complaint has already been logged. There's
6878 not much more we can do except pass on the stub DIE to
6879 die_reader_func. We don't want to throw an error on bad
6880 debug info. */
6881 }
6882 }
6883 }
6884
6885 void
6886 cutu_reader::keep ()
6887 {
6888 /* Done, clean up. */
6889 gdb_assert (!dummy_p);
6890 if (m_new_cu != NULL)
6891 {
6892 struct dwarf2_per_objfile *dwarf2_per_objfile
6893 = m_this_cu->dwarf2_per_objfile;
6894 /* Link this CU into read_in_chain. */
6895 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6896 dwarf2_per_objfile->read_in_chain = m_this_cu;
6897 /* The chain owns it now. */
6898 m_new_cu.release ();
6899 }
6900 }
6901
6902 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6903 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6904 assumed to have already done the lookup to find the DWO file).
6905
6906 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6907 THIS_CU->is_debug_types, but nothing else.
6908
6909 We fill in THIS_CU->length.
6910
6911 THIS_CU->cu is always freed when done.
6912 This is done in order to not leave THIS_CU->cu in a state where we have
6913 to care whether it refers to the "main" CU or the DWO CU.
6914
6915 When parent_cu is passed, it is used to provide a default value for
6916 str_offsets_base and addr_base from the parent. */
6917
6918 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6919 struct dwarf2_cu *parent_cu,
6920 struct dwo_file *dwo_file)
6921 : die_reader_specs {},
6922 m_this_cu (this_cu)
6923 {
6924 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6925 struct objfile *objfile = dwarf2_per_objfile->objfile;
6926 struct dwarf2_section_info *section = this_cu->section;
6927 bfd *abfd = section->get_bfd_owner ();
6928 struct dwarf2_section_info *abbrev_section;
6929 const gdb_byte *begin_info_ptr, *info_ptr;
6930
6931 if (dwarf_die_debug)
6932 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6933 this_cu->is_debug_types ? "type" : "comp",
6934 sect_offset_str (this_cu->sect_off));
6935
6936 gdb_assert (this_cu->cu == NULL);
6937
6938 abbrev_section = (dwo_file != NULL
6939 ? &dwo_file->sections.abbrev
6940 : get_abbrev_section_for_cu (this_cu));
6941
6942 /* This is cheap if the section is already read in. */
6943 section->read (objfile);
6944
6945 m_new_cu.reset (new dwarf2_cu (this_cu));
6946
6947 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6948 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6949 &m_new_cu->header, section,
6950 abbrev_section, info_ptr,
6951 (this_cu->is_debug_types
6952 ? rcuh_kind::TYPE
6953 : rcuh_kind::COMPILE));
6954
6955 if (parent_cu != nullptr)
6956 {
6957 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6958 m_new_cu->addr_base = parent_cu->addr_base;
6959 }
6960 this_cu->length = m_new_cu->header.get_length ();
6961
6962 /* Skip dummy compilation units. */
6963 if (info_ptr >= begin_info_ptr + this_cu->length
6964 || peek_abbrev_code (abfd, info_ptr) == 0)
6965 {
6966 dummy_p = true;
6967 return;
6968 }
6969
6970 m_abbrev_table_holder
6971 = abbrev_table::read (objfile, abbrev_section,
6972 m_new_cu->header.abbrev_sect_off);
6973
6974 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6975 m_abbrev_table_holder.get ());
6976 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6977 }
6978
6979 \f
6980 /* Type Unit Groups.
6981
6982 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6983 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6984 so that all types coming from the same compilation (.o file) are grouped
6985 together. A future step could be to put the types in the same symtab as
6986 the CU the types ultimately came from. */
6987
6988 static hashval_t
6989 hash_type_unit_group (const void *item)
6990 {
6991 const struct type_unit_group *tu_group
6992 = (const struct type_unit_group *) item;
6993
6994 return hash_stmt_list_entry (&tu_group->hash);
6995 }
6996
6997 static int
6998 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6999 {
7000 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7001 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7002
7003 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7004 }
7005
7006 /* Allocate a hash table for type unit groups. */
7007
7008 static htab_up
7009 allocate_type_unit_groups_table ()
7010 {
7011 return htab_up (htab_create_alloc (3,
7012 hash_type_unit_group,
7013 eq_type_unit_group,
7014 NULL, xcalloc, xfree));
7015 }
7016
7017 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7018 partial symtabs. We combine several TUs per psymtab to not let the size
7019 of any one psymtab grow too big. */
7020 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7021 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7022
7023 /* Helper routine for get_type_unit_group.
7024 Create the type_unit_group object used to hold one or more TUs. */
7025
7026 static struct type_unit_group *
7027 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7028 {
7029 struct dwarf2_per_objfile *dwarf2_per_objfile
7030 = cu->per_cu->dwarf2_per_objfile;
7031 struct objfile *objfile = dwarf2_per_objfile->objfile;
7032 struct dwarf2_per_cu_data *per_cu;
7033 struct type_unit_group *tu_group;
7034
7035 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7036 struct type_unit_group);
7037 per_cu = &tu_group->per_cu;
7038 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7039
7040 if (dwarf2_per_objfile->using_index)
7041 {
7042 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7043 struct dwarf2_per_cu_quick_data);
7044 }
7045 else
7046 {
7047 unsigned int line_offset = to_underlying (line_offset_struct);
7048 dwarf2_psymtab *pst;
7049 std::string name;
7050
7051 /* Give the symtab a useful name for debug purposes. */
7052 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7053 name = string_printf ("<type_units_%d>",
7054 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7055 else
7056 name = string_printf ("<type_units_at_0x%x>", line_offset);
7057
7058 pst = create_partial_symtab (per_cu, name.c_str ());
7059 pst->anonymous = true;
7060 }
7061
7062 tu_group->hash.dwo_unit = cu->dwo_unit;
7063 tu_group->hash.line_sect_off = line_offset_struct;
7064
7065 return tu_group;
7066 }
7067
7068 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7069 STMT_LIST is a DW_AT_stmt_list attribute. */
7070
7071 static struct type_unit_group *
7072 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7073 {
7074 struct dwarf2_per_objfile *dwarf2_per_objfile
7075 = cu->per_cu->dwarf2_per_objfile;
7076 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7077 struct type_unit_group *tu_group;
7078 void **slot;
7079 unsigned int line_offset;
7080 struct type_unit_group type_unit_group_for_lookup;
7081
7082 if (dwarf2_per_objfile->type_unit_groups == NULL)
7083 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7084
7085 /* Do we need to create a new group, or can we use an existing one? */
7086
7087 if (stmt_list)
7088 {
7089 line_offset = DW_UNSND (stmt_list);
7090 ++tu_stats->nr_symtab_sharers;
7091 }
7092 else
7093 {
7094 /* Ugh, no stmt_list. Rare, but we have to handle it.
7095 We can do various things here like create one group per TU or
7096 spread them over multiple groups to split up the expansion work.
7097 To avoid worst case scenarios (too many groups or too large groups)
7098 we, umm, group them in bunches. */
7099 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7100 | (tu_stats->nr_stmt_less_type_units
7101 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7102 ++tu_stats->nr_stmt_less_type_units;
7103 }
7104
7105 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7106 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7107 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7108 &type_unit_group_for_lookup, INSERT);
7109 if (*slot != NULL)
7110 {
7111 tu_group = (struct type_unit_group *) *slot;
7112 gdb_assert (tu_group != NULL);
7113 }
7114 else
7115 {
7116 sect_offset line_offset_struct = (sect_offset) line_offset;
7117 tu_group = create_type_unit_group (cu, line_offset_struct);
7118 *slot = tu_group;
7119 ++tu_stats->nr_symtabs;
7120 }
7121
7122 return tu_group;
7123 }
7124 \f
7125 /* Partial symbol tables. */
7126
7127 /* Create a psymtab named NAME and assign it to PER_CU.
7128
7129 The caller must fill in the following details:
7130 dirname, textlow, texthigh. */
7131
7132 static dwarf2_psymtab *
7133 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7134 {
7135 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7136 dwarf2_psymtab *pst;
7137
7138 pst = new dwarf2_psymtab (name, objfile, 0);
7139
7140 pst->psymtabs_addrmap_supported = true;
7141
7142 /* This is the glue that links PST into GDB's symbol API. */
7143 pst->per_cu_data = per_cu;
7144 per_cu->v.psymtab = pst;
7145
7146 return pst;
7147 }
7148
7149 /* DIE reader function for process_psymtab_comp_unit. */
7150
7151 static void
7152 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7153 const gdb_byte *info_ptr,
7154 struct die_info *comp_unit_die,
7155 enum language pretend_language)
7156 {
7157 struct dwarf2_cu *cu = reader->cu;
7158 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7159 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7160 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7161 CORE_ADDR baseaddr;
7162 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7163 dwarf2_psymtab *pst;
7164 enum pc_bounds_kind cu_bounds_kind;
7165 const char *filename;
7166
7167 gdb_assert (! per_cu->is_debug_types);
7168
7169 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7170
7171 /* Allocate a new partial symbol table structure. */
7172 gdb::unique_xmalloc_ptr<char> debug_filename;
7173 static const char artificial[] = "<artificial>";
7174 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7175 if (filename == NULL)
7176 filename = "";
7177 else if (strcmp (filename, artificial) == 0)
7178 {
7179 debug_filename.reset (concat (artificial, "@",
7180 sect_offset_str (per_cu->sect_off),
7181 (char *) NULL));
7182 filename = debug_filename.get ();
7183 }
7184
7185 pst = create_partial_symtab (per_cu, filename);
7186
7187 /* This must be done before calling dwarf2_build_include_psymtabs. */
7188 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7189
7190 baseaddr = objfile->text_section_offset ();
7191
7192 dwarf2_find_base_address (comp_unit_die, cu);
7193
7194 /* Possibly set the default values of LOWPC and HIGHPC from
7195 `DW_AT_ranges'. */
7196 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7197 &best_highpc, cu, pst);
7198 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7199 {
7200 CORE_ADDR low
7201 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7202 - baseaddr);
7203 CORE_ADDR high
7204 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7205 - baseaddr - 1);
7206 /* Store the contiguous range if it is not empty; it can be
7207 empty for CUs with no code. */
7208 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7209 low, high, pst);
7210 }
7211
7212 /* Check if comp unit has_children.
7213 If so, read the rest of the partial symbols from this comp unit.
7214 If not, there's no more debug_info for this comp unit. */
7215 if (comp_unit_die->has_children)
7216 {
7217 struct partial_die_info *first_die;
7218 CORE_ADDR lowpc, highpc;
7219
7220 lowpc = ((CORE_ADDR) -1);
7221 highpc = ((CORE_ADDR) 0);
7222
7223 first_die = load_partial_dies (reader, info_ptr, 1);
7224
7225 scan_partial_symbols (first_die, &lowpc, &highpc,
7226 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7227
7228 /* If we didn't find a lowpc, set it to highpc to avoid
7229 complaints from `maint check'. */
7230 if (lowpc == ((CORE_ADDR) -1))
7231 lowpc = highpc;
7232
7233 /* If the compilation unit didn't have an explicit address range,
7234 then use the information extracted from its child dies. */
7235 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7236 {
7237 best_lowpc = lowpc;
7238 best_highpc = highpc;
7239 }
7240 }
7241 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7242 best_lowpc + baseaddr)
7243 - baseaddr);
7244 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7245 best_highpc + baseaddr)
7246 - baseaddr);
7247
7248 end_psymtab_common (objfile, pst);
7249
7250 if (!cu->per_cu->imported_symtabs_empty ())
7251 {
7252 int i;
7253 int len = cu->per_cu->imported_symtabs_size ();
7254
7255 /* Fill in 'dependencies' here; we fill in 'users' in a
7256 post-pass. */
7257 pst->number_of_dependencies = len;
7258 pst->dependencies
7259 = objfile->partial_symtabs->allocate_dependencies (len);
7260 for (i = 0; i < len; ++i)
7261 {
7262 pst->dependencies[i]
7263 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7264 }
7265
7266 cu->per_cu->imported_symtabs_free ();
7267 }
7268
7269 /* Get the list of files included in the current compilation unit,
7270 and build a psymtab for each of them. */
7271 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7272
7273 if (dwarf_read_debug)
7274 fprintf_unfiltered (gdb_stdlog,
7275 "Psymtab for %s unit @%s: %s - %s"
7276 ", %d global, %d static syms\n",
7277 per_cu->is_debug_types ? "type" : "comp",
7278 sect_offset_str (per_cu->sect_off),
7279 paddress (gdbarch, pst->text_low (objfile)),
7280 paddress (gdbarch, pst->text_high (objfile)),
7281 pst->n_global_syms, pst->n_static_syms);
7282 }
7283
7284 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7285 Process compilation unit THIS_CU for a psymtab. */
7286
7287 static void
7288 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7289 bool want_partial_unit,
7290 enum language pretend_language)
7291 {
7292 /* If this compilation unit was already read in, free the
7293 cached copy in order to read it in again. This is
7294 necessary because we skipped some symbols when we first
7295 read in the compilation unit (see load_partial_dies).
7296 This problem could be avoided, but the benefit is unclear. */
7297 if (this_cu->cu != NULL)
7298 free_one_cached_comp_unit (this_cu);
7299
7300 cutu_reader reader (this_cu, NULL, 0, false);
7301
7302 switch (reader.comp_unit_die->tag)
7303 {
7304 case DW_TAG_compile_unit:
7305 this_cu->unit_type = DW_UT_compile;
7306 break;
7307 case DW_TAG_partial_unit:
7308 this_cu->unit_type = DW_UT_partial;
7309 break;
7310 default:
7311 abort ();
7312 }
7313
7314 if (reader.dummy_p)
7315 {
7316 /* Nothing. */
7317 }
7318 else if (this_cu->is_debug_types)
7319 build_type_psymtabs_reader (&reader, reader.info_ptr,
7320 reader.comp_unit_die);
7321 else if (want_partial_unit
7322 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7323 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7324 reader.comp_unit_die,
7325 pretend_language);
7326
7327 this_cu->lang = this_cu->cu->language;
7328
7329 /* Age out any secondary CUs. */
7330 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7331 }
7332
7333 /* Reader function for build_type_psymtabs. */
7334
7335 static void
7336 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7337 const gdb_byte *info_ptr,
7338 struct die_info *type_unit_die)
7339 {
7340 struct dwarf2_per_objfile *dwarf2_per_objfile
7341 = reader->cu->per_cu->dwarf2_per_objfile;
7342 struct objfile *objfile = dwarf2_per_objfile->objfile;
7343 struct dwarf2_cu *cu = reader->cu;
7344 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7345 struct signatured_type *sig_type;
7346 struct type_unit_group *tu_group;
7347 struct attribute *attr;
7348 struct partial_die_info *first_die;
7349 CORE_ADDR lowpc, highpc;
7350 dwarf2_psymtab *pst;
7351
7352 gdb_assert (per_cu->is_debug_types);
7353 sig_type = (struct signatured_type *) per_cu;
7354
7355 if (! type_unit_die->has_children)
7356 return;
7357
7358 attr = type_unit_die->attr (DW_AT_stmt_list);
7359 tu_group = get_type_unit_group (cu, attr);
7360
7361 if (tu_group->tus == nullptr)
7362 tu_group->tus = new std::vector<signatured_type *>;
7363 tu_group->tus->push_back (sig_type);
7364
7365 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7366 pst = create_partial_symtab (per_cu, "");
7367 pst->anonymous = true;
7368
7369 first_die = load_partial_dies (reader, info_ptr, 1);
7370
7371 lowpc = (CORE_ADDR) -1;
7372 highpc = (CORE_ADDR) 0;
7373 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7374
7375 end_psymtab_common (objfile, pst);
7376 }
7377
7378 /* Struct used to sort TUs by their abbreviation table offset. */
7379
7380 struct tu_abbrev_offset
7381 {
7382 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7383 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7384 {}
7385
7386 signatured_type *sig_type;
7387 sect_offset abbrev_offset;
7388 };
7389
7390 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7391
7392 static bool
7393 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7394 const struct tu_abbrev_offset &b)
7395 {
7396 return a.abbrev_offset < b.abbrev_offset;
7397 }
7398
7399 /* Efficiently read all the type units.
7400 This does the bulk of the work for build_type_psymtabs.
7401
7402 The efficiency is because we sort TUs by the abbrev table they use and
7403 only read each abbrev table once. In one program there are 200K TUs
7404 sharing 8K abbrev tables.
7405
7406 The main purpose of this function is to support building the
7407 dwarf2_per_objfile->type_unit_groups table.
7408 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7409 can collapse the search space by grouping them by stmt_list.
7410 The savings can be significant, in the same program from above the 200K TUs
7411 share 8K stmt_list tables.
7412
7413 FUNC is expected to call get_type_unit_group, which will create the
7414 struct type_unit_group if necessary and add it to
7415 dwarf2_per_objfile->type_unit_groups. */
7416
7417 static void
7418 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7419 {
7420 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7421 abbrev_table_up abbrev_table;
7422 sect_offset abbrev_offset;
7423
7424 /* It's up to the caller to not call us multiple times. */
7425 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7426
7427 if (dwarf2_per_objfile->all_type_units.empty ())
7428 return;
7429
7430 /* TUs typically share abbrev tables, and there can be way more TUs than
7431 abbrev tables. Sort by abbrev table to reduce the number of times we
7432 read each abbrev table in.
7433 Alternatives are to punt or to maintain a cache of abbrev tables.
7434 This is simpler and efficient enough for now.
7435
7436 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7437 symtab to use). Typically TUs with the same abbrev offset have the same
7438 stmt_list value too so in practice this should work well.
7439
7440 The basic algorithm here is:
7441
7442 sort TUs by abbrev table
7443 for each TU with same abbrev table:
7444 read abbrev table if first user
7445 read TU top level DIE
7446 [IWBN if DWO skeletons had DW_AT_stmt_list]
7447 call FUNC */
7448
7449 if (dwarf_read_debug)
7450 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7451
7452 /* Sort in a separate table to maintain the order of all_type_units
7453 for .gdb_index: TU indices directly index all_type_units. */
7454 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7455 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7456
7457 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7458 sorted_by_abbrev.emplace_back
7459 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7460 sig_type->per_cu.section,
7461 sig_type->per_cu.sect_off));
7462
7463 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7464 sort_tu_by_abbrev_offset);
7465
7466 abbrev_offset = (sect_offset) ~(unsigned) 0;
7467
7468 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7469 {
7470 /* Switch to the next abbrev table if necessary. */
7471 if (abbrev_table == NULL
7472 || tu.abbrev_offset != abbrev_offset)
7473 {
7474 abbrev_offset = tu.abbrev_offset;
7475 abbrev_table =
7476 abbrev_table::read (dwarf2_per_objfile->objfile,
7477 &dwarf2_per_objfile->abbrev,
7478 abbrev_offset);
7479 ++tu_stats->nr_uniq_abbrev_tables;
7480 }
7481
7482 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7483 0, false);
7484 if (!reader.dummy_p)
7485 build_type_psymtabs_reader (&reader, reader.info_ptr,
7486 reader.comp_unit_die);
7487 }
7488 }
7489
7490 /* Print collected type unit statistics. */
7491
7492 static void
7493 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7494 {
7495 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7496
7497 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7498 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7499 dwarf2_per_objfile->all_type_units.size ());
7500 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7501 tu_stats->nr_uniq_abbrev_tables);
7502 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7503 tu_stats->nr_symtabs);
7504 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7505 tu_stats->nr_symtab_sharers);
7506 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7507 tu_stats->nr_stmt_less_type_units);
7508 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7509 tu_stats->nr_all_type_units_reallocs);
7510 }
7511
7512 /* Traversal function for build_type_psymtabs. */
7513
7514 static int
7515 build_type_psymtab_dependencies (void **slot, void *info)
7516 {
7517 struct dwarf2_per_objfile *dwarf2_per_objfile
7518 = (struct dwarf2_per_objfile *) info;
7519 struct objfile *objfile = dwarf2_per_objfile->objfile;
7520 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7521 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7522 dwarf2_psymtab *pst = per_cu->v.psymtab;
7523 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7524 int i;
7525
7526 gdb_assert (len > 0);
7527 gdb_assert (per_cu->type_unit_group_p ());
7528
7529 pst->number_of_dependencies = len;
7530 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7531 for (i = 0; i < len; ++i)
7532 {
7533 struct signatured_type *iter = tu_group->tus->at (i);
7534 gdb_assert (iter->per_cu.is_debug_types);
7535 pst->dependencies[i] = iter->per_cu.v.psymtab;
7536 iter->type_unit_group = tu_group;
7537 }
7538
7539 delete tu_group->tus;
7540 tu_group->tus = nullptr;
7541
7542 return 1;
7543 }
7544
7545 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7546 Build partial symbol tables for the .debug_types comp-units. */
7547
7548 static void
7549 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7550 {
7551 if (! create_all_type_units (dwarf2_per_objfile))
7552 return;
7553
7554 build_type_psymtabs_1 (dwarf2_per_objfile);
7555 }
7556
7557 /* Traversal function for process_skeletonless_type_unit.
7558 Read a TU in a DWO file and build partial symbols for it. */
7559
7560 static int
7561 process_skeletonless_type_unit (void **slot, void *info)
7562 {
7563 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7564 struct dwarf2_per_objfile *dwarf2_per_objfile
7565 = (struct dwarf2_per_objfile *) info;
7566 struct signatured_type find_entry, *entry;
7567
7568 /* If this TU doesn't exist in the global table, add it and read it in. */
7569
7570 if (dwarf2_per_objfile->signatured_types == NULL)
7571 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7572
7573 find_entry.signature = dwo_unit->signature;
7574 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7575 &find_entry, INSERT);
7576 /* If we've already seen this type there's nothing to do. What's happening
7577 is we're doing our own version of comdat-folding here. */
7578 if (*slot != NULL)
7579 return 1;
7580
7581 /* This does the job that create_all_type_units would have done for
7582 this TU. */
7583 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7584 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7585 *slot = entry;
7586
7587 /* This does the job that build_type_psymtabs_1 would have done. */
7588 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7589 if (!reader.dummy_p)
7590 build_type_psymtabs_reader (&reader, reader.info_ptr,
7591 reader.comp_unit_die);
7592
7593 return 1;
7594 }
7595
7596 /* Traversal function for process_skeletonless_type_units. */
7597
7598 static int
7599 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7600 {
7601 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7602
7603 if (dwo_file->tus != NULL)
7604 htab_traverse_noresize (dwo_file->tus.get (),
7605 process_skeletonless_type_unit, info);
7606
7607 return 1;
7608 }
7609
7610 /* Scan all TUs of DWO files, verifying we've processed them.
7611 This is needed in case a TU was emitted without its skeleton.
7612 Note: This can't be done until we know what all the DWO files are. */
7613
7614 static void
7615 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7616 {
7617 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7618 if (get_dwp_file (dwarf2_per_objfile) == NULL
7619 && dwarf2_per_objfile->dwo_files != NULL)
7620 {
7621 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7622 process_dwo_file_for_skeletonless_type_units,
7623 dwarf2_per_objfile);
7624 }
7625 }
7626
7627 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7628
7629 static void
7630 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7631 {
7632 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7633 {
7634 dwarf2_psymtab *pst = per_cu->v.psymtab;
7635
7636 if (pst == NULL)
7637 continue;
7638
7639 for (int j = 0; j < pst->number_of_dependencies; ++j)
7640 {
7641 /* Set the 'user' field only if it is not already set. */
7642 if (pst->dependencies[j]->user == NULL)
7643 pst->dependencies[j]->user = pst;
7644 }
7645 }
7646 }
7647
7648 /* Build the partial symbol table by doing a quick pass through the
7649 .debug_info and .debug_abbrev sections. */
7650
7651 static void
7652 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7653 {
7654 struct objfile *objfile = dwarf2_per_objfile->objfile;
7655
7656 if (dwarf_read_debug)
7657 {
7658 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7659 objfile_name (objfile));
7660 }
7661
7662 scoped_restore restore_reading_psyms
7663 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7664 true);
7665
7666 dwarf2_per_objfile->info.read (objfile);
7667
7668 /* Any cached compilation units will be linked by the per-objfile
7669 read_in_chain. Make sure to free them when we're done. */
7670 free_cached_comp_units freer (dwarf2_per_objfile);
7671
7672 build_type_psymtabs (dwarf2_per_objfile);
7673
7674 create_all_comp_units (dwarf2_per_objfile);
7675
7676 /* Create a temporary address map on a temporary obstack. We later
7677 copy this to the final obstack. */
7678 auto_obstack temp_obstack;
7679
7680 scoped_restore save_psymtabs_addrmap
7681 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7682 addrmap_create_mutable (&temp_obstack));
7683
7684 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7685 process_psymtab_comp_unit (per_cu, false, language_minimal);
7686
7687 /* This has to wait until we read the CUs, we need the list of DWOs. */
7688 process_skeletonless_type_units (dwarf2_per_objfile);
7689
7690 /* Now that all TUs have been processed we can fill in the dependencies. */
7691 if (dwarf2_per_objfile->type_unit_groups != NULL)
7692 {
7693 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7694 build_type_psymtab_dependencies, dwarf2_per_objfile);
7695 }
7696
7697 if (dwarf_read_debug)
7698 print_tu_stats (dwarf2_per_objfile);
7699
7700 set_partial_user (dwarf2_per_objfile);
7701
7702 objfile->partial_symtabs->psymtabs_addrmap
7703 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7704 objfile->partial_symtabs->obstack ());
7705 /* At this point we want to keep the address map. */
7706 save_psymtabs_addrmap.release ();
7707
7708 if (dwarf_read_debug)
7709 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7710 objfile_name (objfile));
7711 }
7712
7713 /* Load the partial DIEs for a secondary CU into memory.
7714 This is also used when rereading a primary CU with load_all_dies. */
7715
7716 static void
7717 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7718 {
7719 cutu_reader reader (this_cu, NULL, 1, false);
7720
7721 if (!reader.dummy_p)
7722 {
7723 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7724 language_minimal);
7725
7726 /* Check if comp unit has_children.
7727 If so, read the rest of the partial symbols from this comp unit.
7728 If not, there's no more debug_info for this comp unit. */
7729 if (reader.comp_unit_die->has_children)
7730 load_partial_dies (&reader, reader.info_ptr, 0);
7731
7732 reader.keep ();
7733 }
7734 }
7735
7736 static void
7737 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7738 struct dwarf2_section_info *section,
7739 struct dwarf2_section_info *abbrev_section,
7740 unsigned int is_dwz)
7741 {
7742 const gdb_byte *info_ptr;
7743 struct objfile *objfile = dwarf2_per_objfile->objfile;
7744
7745 if (dwarf_read_debug)
7746 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7747 section->get_name (),
7748 section->get_file_name ());
7749
7750 section->read (objfile);
7751
7752 info_ptr = section->buffer;
7753
7754 while (info_ptr < section->buffer + section->size)
7755 {
7756 struct dwarf2_per_cu_data *this_cu;
7757
7758 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7759
7760 comp_unit_head cu_header;
7761 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7762 abbrev_section, info_ptr,
7763 rcuh_kind::COMPILE);
7764
7765 /* Save the compilation unit for later lookup. */
7766 if (cu_header.unit_type != DW_UT_type)
7767 {
7768 this_cu = XOBNEW (&objfile->objfile_obstack,
7769 struct dwarf2_per_cu_data);
7770 memset (this_cu, 0, sizeof (*this_cu));
7771 }
7772 else
7773 {
7774 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7775 struct signatured_type);
7776 memset (sig_type, 0, sizeof (*sig_type));
7777 sig_type->signature = cu_header.signature;
7778 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7779 this_cu = &sig_type->per_cu;
7780 }
7781 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7782 this_cu->sect_off = sect_off;
7783 this_cu->length = cu_header.length + cu_header.initial_length_size;
7784 this_cu->is_dwz = is_dwz;
7785 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7786 this_cu->section = section;
7787
7788 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7789
7790 info_ptr = info_ptr + this_cu->length;
7791 }
7792 }
7793
7794 /* Create a list of all compilation units in OBJFILE.
7795 This is only done for -readnow and building partial symtabs. */
7796
7797 static void
7798 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7799 {
7800 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7801 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7802 &dwarf2_per_objfile->abbrev, 0);
7803
7804 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7805 if (dwz != NULL)
7806 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7807 1);
7808 }
7809
7810 /* Process all loaded DIEs for compilation unit CU, starting at
7811 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7812 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7813 DW_AT_ranges). See the comments of add_partial_subprogram on how
7814 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7815
7816 static void
7817 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7818 CORE_ADDR *highpc, int set_addrmap,
7819 struct dwarf2_cu *cu)
7820 {
7821 struct partial_die_info *pdi;
7822
7823 /* Now, march along the PDI's, descending into ones which have
7824 interesting children but skipping the children of the other ones,
7825 until we reach the end of the compilation unit. */
7826
7827 pdi = first_die;
7828
7829 while (pdi != NULL)
7830 {
7831 pdi->fixup (cu);
7832
7833 /* Anonymous namespaces or modules have no name but have interesting
7834 children, so we need to look at them. Ditto for anonymous
7835 enums. */
7836
7837 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7838 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7839 || pdi->tag == DW_TAG_imported_unit
7840 || pdi->tag == DW_TAG_inlined_subroutine)
7841 {
7842 switch (pdi->tag)
7843 {
7844 case DW_TAG_subprogram:
7845 case DW_TAG_inlined_subroutine:
7846 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7847 break;
7848 case DW_TAG_constant:
7849 case DW_TAG_variable:
7850 case DW_TAG_typedef:
7851 case DW_TAG_union_type:
7852 if (!pdi->is_declaration)
7853 {
7854 add_partial_symbol (pdi, cu);
7855 }
7856 break;
7857 case DW_TAG_class_type:
7858 case DW_TAG_interface_type:
7859 case DW_TAG_structure_type:
7860 if (!pdi->is_declaration)
7861 {
7862 add_partial_symbol (pdi, cu);
7863 }
7864 if ((cu->language == language_rust
7865 || cu->language == language_cplus) && pdi->has_children)
7866 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7867 set_addrmap, cu);
7868 break;
7869 case DW_TAG_enumeration_type:
7870 if (!pdi->is_declaration)
7871 add_partial_enumeration (pdi, cu);
7872 break;
7873 case DW_TAG_base_type:
7874 case DW_TAG_subrange_type:
7875 /* File scope base type definitions are added to the partial
7876 symbol table. */
7877 add_partial_symbol (pdi, cu);
7878 break;
7879 case DW_TAG_namespace:
7880 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7881 break;
7882 case DW_TAG_module:
7883 if (!pdi->is_declaration)
7884 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7885 break;
7886 case DW_TAG_imported_unit:
7887 {
7888 struct dwarf2_per_cu_data *per_cu;
7889
7890 /* For now we don't handle imported units in type units. */
7891 if (cu->per_cu->is_debug_types)
7892 {
7893 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7894 " supported in type units [in module %s]"),
7895 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7896 }
7897
7898 per_cu = dwarf2_find_containing_comp_unit
7899 (pdi->d.sect_off, pdi->is_dwz,
7900 cu->per_cu->dwarf2_per_objfile);
7901
7902 /* Go read the partial unit, if needed. */
7903 if (per_cu->v.psymtab == NULL)
7904 process_psymtab_comp_unit (per_cu, true, cu->language);
7905
7906 cu->per_cu->imported_symtabs_push (per_cu);
7907 }
7908 break;
7909 case DW_TAG_imported_declaration:
7910 add_partial_symbol (pdi, cu);
7911 break;
7912 default:
7913 break;
7914 }
7915 }
7916
7917 /* If the die has a sibling, skip to the sibling. */
7918
7919 pdi = pdi->die_sibling;
7920 }
7921 }
7922
7923 /* Functions used to compute the fully scoped name of a partial DIE.
7924
7925 Normally, this is simple. For C++, the parent DIE's fully scoped
7926 name is concatenated with "::" and the partial DIE's name.
7927 Enumerators are an exception; they use the scope of their parent
7928 enumeration type, i.e. the name of the enumeration type is not
7929 prepended to the enumerator.
7930
7931 There are two complexities. One is DW_AT_specification; in this
7932 case "parent" means the parent of the target of the specification,
7933 instead of the direct parent of the DIE. The other is compilers
7934 which do not emit DW_TAG_namespace; in this case we try to guess
7935 the fully qualified name of structure types from their members'
7936 linkage names. This must be done using the DIE's children rather
7937 than the children of any DW_AT_specification target. We only need
7938 to do this for structures at the top level, i.e. if the target of
7939 any DW_AT_specification (if any; otherwise the DIE itself) does not
7940 have a parent. */
7941
7942 /* Compute the scope prefix associated with PDI's parent, in
7943 compilation unit CU. The result will be allocated on CU's
7944 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7945 field. NULL is returned if no prefix is necessary. */
7946 static const char *
7947 partial_die_parent_scope (struct partial_die_info *pdi,
7948 struct dwarf2_cu *cu)
7949 {
7950 const char *grandparent_scope;
7951 struct partial_die_info *parent, *real_pdi;
7952
7953 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7954 then this means the parent of the specification DIE. */
7955
7956 real_pdi = pdi;
7957 while (real_pdi->has_specification)
7958 {
7959 auto res = find_partial_die (real_pdi->spec_offset,
7960 real_pdi->spec_is_dwz, cu);
7961 real_pdi = res.pdi;
7962 cu = res.cu;
7963 }
7964
7965 parent = real_pdi->die_parent;
7966 if (parent == NULL)
7967 return NULL;
7968
7969 if (parent->scope_set)
7970 return parent->scope;
7971
7972 parent->fixup (cu);
7973
7974 grandparent_scope = partial_die_parent_scope (parent, cu);
7975
7976 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7977 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7978 Work around this problem here. */
7979 if (cu->language == language_cplus
7980 && parent->tag == DW_TAG_namespace
7981 && strcmp (parent->name, "::") == 0
7982 && grandparent_scope == NULL)
7983 {
7984 parent->scope = NULL;
7985 parent->scope_set = 1;
7986 return NULL;
7987 }
7988
7989 /* Nested subroutines in Fortran get a prefix. */
7990 if (pdi->tag == DW_TAG_enumerator)
7991 /* Enumerators should not get the name of the enumeration as a prefix. */
7992 parent->scope = grandparent_scope;
7993 else if (parent->tag == DW_TAG_namespace
7994 || parent->tag == DW_TAG_module
7995 || parent->tag == DW_TAG_structure_type
7996 || parent->tag == DW_TAG_class_type
7997 || parent->tag == DW_TAG_interface_type
7998 || parent->tag == DW_TAG_union_type
7999 || parent->tag == DW_TAG_enumeration_type
8000 || (cu->language == language_fortran
8001 && parent->tag == DW_TAG_subprogram
8002 && pdi->tag == DW_TAG_subprogram))
8003 {
8004 if (grandparent_scope == NULL)
8005 parent->scope = parent->name;
8006 else
8007 parent->scope = typename_concat (&cu->comp_unit_obstack,
8008 grandparent_scope,
8009 parent->name, 0, cu);
8010 }
8011 else
8012 {
8013 /* FIXME drow/2004-04-01: What should we be doing with
8014 function-local names? For partial symbols, we should probably be
8015 ignoring them. */
8016 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8017 dwarf_tag_name (parent->tag),
8018 sect_offset_str (pdi->sect_off));
8019 parent->scope = grandparent_scope;
8020 }
8021
8022 parent->scope_set = 1;
8023 return parent->scope;
8024 }
8025
8026 /* Return the fully scoped name associated with PDI, from compilation unit
8027 CU. The result will be allocated with malloc. */
8028
8029 static gdb::unique_xmalloc_ptr<char>
8030 partial_die_full_name (struct partial_die_info *pdi,
8031 struct dwarf2_cu *cu)
8032 {
8033 const char *parent_scope;
8034
8035 /* If this is a template instantiation, we can not work out the
8036 template arguments from partial DIEs. So, unfortunately, we have
8037 to go through the full DIEs. At least any work we do building
8038 types here will be reused if full symbols are loaded later. */
8039 if (pdi->has_template_arguments)
8040 {
8041 pdi->fixup (cu);
8042
8043 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8044 {
8045 struct die_info *die;
8046 struct attribute attr;
8047 struct dwarf2_cu *ref_cu = cu;
8048
8049 /* DW_FORM_ref_addr is using section offset. */
8050 attr.name = (enum dwarf_attribute) 0;
8051 attr.form = DW_FORM_ref_addr;
8052 attr.u.unsnd = to_underlying (pdi->sect_off);
8053 die = follow_die_ref (NULL, &attr, &ref_cu);
8054
8055 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8056 }
8057 }
8058
8059 parent_scope = partial_die_parent_scope (pdi, cu);
8060 if (parent_scope == NULL)
8061 return NULL;
8062 else
8063 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8064 pdi->name, 0, cu));
8065 }
8066
8067 static void
8068 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8069 {
8070 struct dwarf2_per_objfile *dwarf2_per_objfile
8071 = cu->per_cu->dwarf2_per_objfile;
8072 struct objfile *objfile = dwarf2_per_objfile->objfile;
8073 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8074 CORE_ADDR addr = 0;
8075 const char *actual_name = NULL;
8076 CORE_ADDR baseaddr;
8077
8078 baseaddr = objfile->text_section_offset ();
8079
8080 gdb::unique_xmalloc_ptr<char> built_actual_name
8081 = partial_die_full_name (pdi, cu);
8082 if (built_actual_name != NULL)
8083 actual_name = built_actual_name.get ();
8084
8085 if (actual_name == NULL)
8086 actual_name = pdi->name;
8087
8088 switch (pdi->tag)
8089 {
8090 case DW_TAG_inlined_subroutine:
8091 case DW_TAG_subprogram:
8092 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8093 - baseaddr);
8094 if (pdi->is_external
8095 || cu->language == language_ada
8096 || (cu->language == language_fortran
8097 && pdi->die_parent != NULL
8098 && pdi->die_parent->tag == DW_TAG_subprogram))
8099 {
8100 /* Normally, only "external" DIEs are part of the global scope.
8101 But in Ada and Fortran, we want to be able to access nested
8102 procedures globally. So all Ada and Fortran subprograms are
8103 stored in the global scope. */
8104 add_psymbol_to_list (actual_name,
8105 built_actual_name != NULL,
8106 VAR_DOMAIN, LOC_BLOCK,
8107 SECT_OFF_TEXT (objfile),
8108 psymbol_placement::GLOBAL,
8109 addr,
8110 cu->language, objfile);
8111 }
8112 else
8113 {
8114 add_psymbol_to_list (actual_name,
8115 built_actual_name != NULL,
8116 VAR_DOMAIN, LOC_BLOCK,
8117 SECT_OFF_TEXT (objfile),
8118 psymbol_placement::STATIC,
8119 addr, cu->language, objfile);
8120 }
8121
8122 if (pdi->main_subprogram && actual_name != NULL)
8123 set_objfile_main_name (objfile, actual_name, cu->language);
8124 break;
8125 case DW_TAG_constant:
8126 add_psymbol_to_list (actual_name,
8127 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8128 -1, (pdi->is_external
8129 ? psymbol_placement::GLOBAL
8130 : psymbol_placement::STATIC),
8131 0, cu->language, objfile);
8132 break;
8133 case DW_TAG_variable:
8134 if (pdi->d.locdesc)
8135 addr = decode_locdesc (pdi->d.locdesc, cu);
8136
8137 if (pdi->d.locdesc
8138 && addr == 0
8139 && !dwarf2_per_objfile->has_section_at_zero)
8140 {
8141 /* A global or static variable may also have been stripped
8142 out by the linker if unused, in which case its address
8143 will be nullified; do not add such variables into partial
8144 symbol table then. */
8145 }
8146 else if (pdi->is_external)
8147 {
8148 /* Global Variable.
8149 Don't enter into the minimal symbol tables as there is
8150 a minimal symbol table entry from the ELF symbols already.
8151 Enter into partial symbol table if it has a location
8152 descriptor or a type.
8153 If the location descriptor is missing, new_symbol will create
8154 a LOC_UNRESOLVED symbol, the address of the variable will then
8155 be determined from the minimal symbol table whenever the variable
8156 is referenced.
8157 The address for the partial symbol table entry is not
8158 used by GDB, but it comes in handy for debugging partial symbol
8159 table building. */
8160
8161 if (pdi->d.locdesc || pdi->has_type)
8162 add_psymbol_to_list (actual_name,
8163 built_actual_name != NULL,
8164 VAR_DOMAIN, LOC_STATIC,
8165 SECT_OFF_TEXT (objfile),
8166 psymbol_placement::GLOBAL,
8167 addr, cu->language, objfile);
8168 }
8169 else
8170 {
8171 int has_loc = pdi->d.locdesc != NULL;
8172
8173 /* Static Variable. Skip symbols whose value we cannot know (those
8174 without location descriptors or constant values). */
8175 if (!has_loc && !pdi->has_const_value)
8176 return;
8177
8178 add_psymbol_to_list (actual_name,
8179 built_actual_name != NULL,
8180 VAR_DOMAIN, LOC_STATIC,
8181 SECT_OFF_TEXT (objfile),
8182 psymbol_placement::STATIC,
8183 has_loc ? addr : 0,
8184 cu->language, objfile);
8185 }
8186 break;
8187 case DW_TAG_typedef:
8188 case DW_TAG_base_type:
8189 case DW_TAG_subrange_type:
8190 add_psymbol_to_list (actual_name,
8191 built_actual_name != NULL,
8192 VAR_DOMAIN, LOC_TYPEDEF, -1,
8193 psymbol_placement::STATIC,
8194 0, cu->language, objfile);
8195 break;
8196 case DW_TAG_imported_declaration:
8197 case DW_TAG_namespace:
8198 add_psymbol_to_list (actual_name,
8199 built_actual_name != NULL,
8200 VAR_DOMAIN, LOC_TYPEDEF, -1,
8201 psymbol_placement::GLOBAL,
8202 0, cu->language, objfile);
8203 break;
8204 case DW_TAG_module:
8205 /* With Fortran 77 there might be a "BLOCK DATA" module
8206 available without any name. If so, we skip the module as it
8207 doesn't bring any value. */
8208 if (actual_name != nullptr)
8209 add_psymbol_to_list (actual_name,
8210 built_actual_name != NULL,
8211 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8212 psymbol_placement::GLOBAL,
8213 0, cu->language, objfile);
8214 break;
8215 case DW_TAG_class_type:
8216 case DW_TAG_interface_type:
8217 case DW_TAG_structure_type:
8218 case DW_TAG_union_type:
8219 case DW_TAG_enumeration_type:
8220 /* Skip external references. The DWARF standard says in the section
8221 about "Structure, Union, and Class Type Entries": "An incomplete
8222 structure, union or class type is represented by a structure,
8223 union or class entry that does not have a byte size attribute
8224 and that has a DW_AT_declaration attribute." */
8225 if (!pdi->has_byte_size && pdi->is_declaration)
8226 return;
8227
8228 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8229 static vs. global. */
8230 add_psymbol_to_list (actual_name,
8231 built_actual_name != NULL,
8232 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8233 cu->language == language_cplus
8234 ? psymbol_placement::GLOBAL
8235 : psymbol_placement::STATIC,
8236 0, cu->language, objfile);
8237
8238 break;
8239 case DW_TAG_enumerator:
8240 add_psymbol_to_list (actual_name,
8241 built_actual_name != NULL,
8242 VAR_DOMAIN, LOC_CONST, -1,
8243 cu->language == language_cplus
8244 ? psymbol_placement::GLOBAL
8245 : psymbol_placement::STATIC,
8246 0, cu->language, objfile);
8247 break;
8248 default:
8249 break;
8250 }
8251 }
8252
8253 /* Read a partial die corresponding to a namespace; also, add a symbol
8254 corresponding to that namespace to the symbol table. NAMESPACE is
8255 the name of the enclosing namespace. */
8256
8257 static void
8258 add_partial_namespace (struct partial_die_info *pdi,
8259 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8260 int set_addrmap, struct dwarf2_cu *cu)
8261 {
8262 /* Add a symbol for the namespace. */
8263
8264 add_partial_symbol (pdi, cu);
8265
8266 /* Now scan partial symbols in that namespace. */
8267
8268 if (pdi->has_children)
8269 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8270 }
8271
8272 /* Read a partial die corresponding to a Fortran module. */
8273
8274 static void
8275 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8276 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8277 {
8278 /* Add a symbol for the namespace. */
8279
8280 add_partial_symbol (pdi, cu);
8281
8282 /* Now scan partial symbols in that module. */
8283
8284 if (pdi->has_children)
8285 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8286 }
8287
8288 /* Read a partial die corresponding to a subprogram or an inlined
8289 subprogram and create a partial symbol for that subprogram.
8290 When the CU language allows it, this routine also defines a partial
8291 symbol for each nested subprogram that this subprogram contains.
8292 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8293 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8294
8295 PDI may also be a lexical block, in which case we simply search
8296 recursively for subprograms defined inside that lexical block.
8297 Again, this is only performed when the CU language allows this
8298 type of definitions. */
8299
8300 static void
8301 add_partial_subprogram (struct partial_die_info *pdi,
8302 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8303 int set_addrmap, struct dwarf2_cu *cu)
8304 {
8305 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8306 {
8307 if (pdi->has_pc_info)
8308 {
8309 if (pdi->lowpc < *lowpc)
8310 *lowpc = pdi->lowpc;
8311 if (pdi->highpc > *highpc)
8312 *highpc = pdi->highpc;
8313 if (set_addrmap)
8314 {
8315 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8316 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8317 CORE_ADDR baseaddr;
8318 CORE_ADDR this_highpc;
8319 CORE_ADDR this_lowpc;
8320
8321 baseaddr = objfile->text_section_offset ();
8322 this_lowpc
8323 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8324 pdi->lowpc + baseaddr)
8325 - baseaddr);
8326 this_highpc
8327 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8328 pdi->highpc + baseaddr)
8329 - baseaddr);
8330 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8331 this_lowpc, this_highpc - 1,
8332 cu->per_cu->v.psymtab);
8333 }
8334 }
8335
8336 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8337 {
8338 if (!pdi->is_declaration)
8339 /* Ignore subprogram DIEs that do not have a name, they are
8340 illegal. Do not emit a complaint at this point, we will
8341 do so when we convert this psymtab into a symtab. */
8342 if (pdi->name)
8343 add_partial_symbol (pdi, cu);
8344 }
8345 }
8346
8347 if (! pdi->has_children)
8348 return;
8349
8350 if (cu->language == language_ada || cu->language == language_fortran)
8351 {
8352 pdi = pdi->die_child;
8353 while (pdi != NULL)
8354 {
8355 pdi->fixup (cu);
8356 if (pdi->tag == DW_TAG_subprogram
8357 || pdi->tag == DW_TAG_inlined_subroutine
8358 || pdi->tag == DW_TAG_lexical_block)
8359 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8360 pdi = pdi->die_sibling;
8361 }
8362 }
8363 }
8364
8365 /* Read a partial die corresponding to an enumeration type. */
8366
8367 static void
8368 add_partial_enumeration (struct partial_die_info *enum_pdi,
8369 struct dwarf2_cu *cu)
8370 {
8371 struct partial_die_info *pdi;
8372
8373 if (enum_pdi->name != NULL)
8374 add_partial_symbol (enum_pdi, cu);
8375
8376 pdi = enum_pdi->die_child;
8377 while (pdi)
8378 {
8379 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8380 complaint (_("malformed enumerator DIE ignored"));
8381 else
8382 add_partial_symbol (pdi, cu);
8383 pdi = pdi->die_sibling;
8384 }
8385 }
8386
8387 /* Return the initial uleb128 in the die at INFO_PTR. */
8388
8389 static unsigned int
8390 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8391 {
8392 unsigned int bytes_read;
8393
8394 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8395 }
8396
8397 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8398 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8399
8400 Return the corresponding abbrev, or NULL if the number is zero (indicating
8401 an empty DIE). In either case *BYTES_READ will be set to the length of
8402 the initial number. */
8403
8404 static struct abbrev_info *
8405 peek_die_abbrev (const die_reader_specs &reader,
8406 const gdb_byte *info_ptr, unsigned int *bytes_read)
8407 {
8408 dwarf2_cu *cu = reader.cu;
8409 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8410 unsigned int abbrev_number
8411 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8412
8413 if (abbrev_number == 0)
8414 return NULL;
8415
8416 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8417 if (!abbrev)
8418 {
8419 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8420 " at offset %s [in module %s]"),
8421 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8422 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8423 }
8424
8425 return abbrev;
8426 }
8427
8428 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8429 Returns a pointer to the end of a series of DIEs, terminated by an empty
8430 DIE. Any children of the skipped DIEs will also be skipped. */
8431
8432 static const gdb_byte *
8433 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8434 {
8435 while (1)
8436 {
8437 unsigned int bytes_read;
8438 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8439
8440 if (abbrev == NULL)
8441 return info_ptr + bytes_read;
8442 else
8443 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8444 }
8445 }
8446
8447 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8448 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8449 abbrev corresponding to that skipped uleb128 should be passed in
8450 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8451 children. */
8452
8453 static const gdb_byte *
8454 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8455 struct abbrev_info *abbrev)
8456 {
8457 unsigned int bytes_read;
8458 struct attribute attr;
8459 bfd *abfd = reader->abfd;
8460 struct dwarf2_cu *cu = reader->cu;
8461 const gdb_byte *buffer = reader->buffer;
8462 const gdb_byte *buffer_end = reader->buffer_end;
8463 unsigned int form, i;
8464
8465 for (i = 0; i < abbrev->num_attrs; i++)
8466 {
8467 /* The only abbrev we care about is DW_AT_sibling. */
8468 if (abbrev->attrs[i].name == DW_AT_sibling)
8469 {
8470 bool ignored;
8471 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8472 &ignored);
8473 if (attr.form == DW_FORM_ref_addr)
8474 complaint (_("ignoring absolute DW_AT_sibling"));
8475 else
8476 {
8477 sect_offset off = attr.get_ref_die_offset ();
8478 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8479
8480 if (sibling_ptr < info_ptr)
8481 complaint (_("DW_AT_sibling points backwards"));
8482 else if (sibling_ptr > reader->buffer_end)
8483 reader->die_section->overflow_complaint ();
8484 else
8485 return sibling_ptr;
8486 }
8487 }
8488
8489 /* If it isn't DW_AT_sibling, skip this attribute. */
8490 form = abbrev->attrs[i].form;
8491 skip_attribute:
8492 switch (form)
8493 {
8494 case DW_FORM_ref_addr:
8495 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8496 and later it is offset sized. */
8497 if (cu->header.version == 2)
8498 info_ptr += cu->header.addr_size;
8499 else
8500 info_ptr += cu->header.offset_size;
8501 break;
8502 case DW_FORM_GNU_ref_alt:
8503 info_ptr += cu->header.offset_size;
8504 break;
8505 case DW_FORM_addr:
8506 info_ptr += cu->header.addr_size;
8507 break;
8508 case DW_FORM_data1:
8509 case DW_FORM_ref1:
8510 case DW_FORM_flag:
8511 case DW_FORM_strx1:
8512 info_ptr += 1;
8513 break;
8514 case DW_FORM_flag_present:
8515 case DW_FORM_implicit_const:
8516 break;
8517 case DW_FORM_data2:
8518 case DW_FORM_ref2:
8519 case DW_FORM_strx2:
8520 info_ptr += 2;
8521 break;
8522 case DW_FORM_strx3:
8523 info_ptr += 3;
8524 break;
8525 case DW_FORM_data4:
8526 case DW_FORM_ref4:
8527 case DW_FORM_strx4:
8528 info_ptr += 4;
8529 break;
8530 case DW_FORM_data8:
8531 case DW_FORM_ref8:
8532 case DW_FORM_ref_sig8:
8533 info_ptr += 8;
8534 break;
8535 case DW_FORM_data16:
8536 info_ptr += 16;
8537 break;
8538 case DW_FORM_string:
8539 read_direct_string (abfd, info_ptr, &bytes_read);
8540 info_ptr += bytes_read;
8541 break;
8542 case DW_FORM_sec_offset:
8543 case DW_FORM_strp:
8544 case DW_FORM_GNU_strp_alt:
8545 info_ptr += cu->header.offset_size;
8546 break;
8547 case DW_FORM_exprloc:
8548 case DW_FORM_block:
8549 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8550 info_ptr += bytes_read;
8551 break;
8552 case DW_FORM_block1:
8553 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8554 break;
8555 case DW_FORM_block2:
8556 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8557 break;
8558 case DW_FORM_block4:
8559 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8560 break;
8561 case DW_FORM_addrx:
8562 case DW_FORM_strx:
8563 case DW_FORM_sdata:
8564 case DW_FORM_udata:
8565 case DW_FORM_ref_udata:
8566 case DW_FORM_GNU_addr_index:
8567 case DW_FORM_GNU_str_index:
8568 case DW_FORM_rnglistx:
8569 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8570 break;
8571 case DW_FORM_indirect:
8572 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8573 info_ptr += bytes_read;
8574 /* We need to continue parsing from here, so just go back to
8575 the top. */
8576 goto skip_attribute;
8577
8578 default:
8579 error (_("Dwarf Error: Cannot handle %s "
8580 "in DWARF reader [in module %s]"),
8581 dwarf_form_name (form),
8582 bfd_get_filename (abfd));
8583 }
8584 }
8585
8586 if (abbrev->has_children)
8587 return skip_children (reader, info_ptr);
8588 else
8589 return info_ptr;
8590 }
8591
8592 /* Locate ORIG_PDI's sibling.
8593 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8594
8595 static const gdb_byte *
8596 locate_pdi_sibling (const struct die_reader_specs *reader,
8597 struct partial_die_info *orig_pdi,
8598 const gdb_byte *info_ptr)
8599 {
8600 /* Do we know the sibling already? */
8601
8602 if (orig_pdi->sibling)
8603 return orig_pdi->sibling;
8604
8605 /* Are there any children to deal with? */
8606
8607 if (!orig_pdi->has_children)
8608 return info_ptr;
8609
8610 /* Skip the children the long way. */
8611
8612 return skip_children (reader, info_ptr);
8613 }
8614
8615 /* Expand this partial symbol table into a full symbol table. SELF is
8616 not NULL. */
8617
8618 void
8619 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8620 {
8621 struct dwarf2_per_objfile *dwarf2_per_objfile
8622 = get_dwarf2_per_objfile (objfile);
8623
8624 gdb_assert (!readin);
8625 /* If this psymtab is constructed from a debug-only objfile, the
8626 has_section_at_zero flag will not necessarily be correct. We
8627 can get the correct value for this flag by looking at the data
8628 associated with the (presumably stripped) associated objfile. */
8629 if (objfile->separate_debug_objfile_backlink)
8630 {
8631 struct dwarf2_per_objfile *dpo_backlink
8632 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8633
8634 dwarf2_per_objfile->has_section_at_zero
8635 = dpo_backlink->has_section_at_zero;
8636 }
8637
8638 expand_psymtab (objfile);
8639
8640 process_cu_includes (dwarf2_per_objfile);
8641 }
8642 \f
8643 /* Reading in full CUs. */
8644
8645 /* Add PER_CU to the queue. */
8646
8647 static void
8648 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8649 enum language pretend_language)
8650 {
8651 per_cu->queued = 1;
8652 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8653 }
8654
8655 /* If PER_CU is not yet queued, add it to the queue.
8656 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8657 dependency.
8658 The result is non-zero if PER_CU was queued, otherwise the result is zero
8659 meaning either PER_CU is already queued or it is already loaded.
8660
8661 N.B. There is an invariant here that if a CU is queued then it is loaded.
8662 The caller is required to load PER_CU if we return non-zero. */
8663
8664 static int
8665 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8666 struct dwarf2_per_cu_data *per_cu,
8667 enum language pretend_language)
8668 {
8669 /* We may arrive here during partial symbol reading, if we need full
8670 DIEs to process an unusual case (e.g. template arguments). Do
8671 not queue PER_CU, just tell our caller to load its DIEs. */
8672 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8673 {
8674 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8675 return 1;
8676 return 0;
8677 }
8678
8679 /* Mark the dependence relation so that we don't flush PER_CU
8680 too early. */
8681 if (dependent_cu != NULL)
8682 dwarf2_add_dependence (dependent_cu, per_cu);
8683
8684 /* If it's already on the queue, we have nothing to do. */
8685 if (per_cu->queued)
8686 return 0;
8687
8688 /* If the compilation unit is already loaded, just mark it as
8689 used. */
8690 if (per_cu->cu != NULL)
8691 {
8692 per_cu->cu->last_used = 0;
8693 return 0;
8694 }
8695
8696 /* Add it to the queue. */
8697 queue_comp_unit (per_cu, pretend_language);
8698
8699 return 1;
8700 }
8701
8702 /* Process the queue. */
8703
8704 static void
8705 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8706 {
8707 if (dwarf_read_debug)
8708 {
8709 fprintf_unfiltered (gdb_stdlog,
8710 "Expanding one or more symtabs of objfile %s ...\n",
8711 objfile_name (dwarf2_per_objfile->objfile));
8712 }
8713
8714 /* The queue starts out with one item, but following a DIE reference
8715 may load a new CU, adding it to the end of the queue. */
8716 while (!dwarf2_per_objfile->queue.empty ())
8717 {
8718 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8719
8720 if ((dwarf2_per_objfile->using_index
8721 ? !item.per_cu->v.quick->compunit_symtab
8722 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8723 /* Skip dummy CUs. */
8724 && item.per_cu->cu != NULL)
8725 {
8726 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8727 unsigned int debug_print_threshold;
8728 char buf[100];
8729
8730 if (per_cu->is_debug_types)
8731 {
8732 struct signatured_type *sig_type =
8733 (struct signatured_type *) per_cu;
8734
8735 sprintf (buf, "TU %s at offset %s",
8736 hex_string (sig_type->signature),
8737 sect_offset_str (per_cu->sect_off));
8738 /* There can be 100s of TUs.
8739 Only print them in verbose mode. */
8740 debug_print_threshold = 2;
8741 }
8742 else
8743 {
8744 sprintf (buf, "CU at offset %s",
8745 sect_offset_str (per_cu->sect_off));
8746 debug_print_threshold = 1;
8747 }
8748
8749 if (dwarf_read_debug >= debug_print_threshold)
8750 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8751
8752 if (per_cu->is_debug_types)
8753 process_full_type_unit (per_cu, item.pretend_language);
8754 else
8755 process_full_comp_unit (per_cu, item.pretend_language);
8756
8757 if (dwarf_read_debug >= debug_print_threshold)
8758 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8759 }
8760
8761 item.per_cu->queued = 0;
8762 dwarf2_per_objfile->queue.pop ();
8763 }
8764
8765 if (dwarf_read_debug)
8766 {
8767 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8768 objfile_name (dwarf2_per_objfile->objfile));
8769 }
8770 }
8771
8772 /* Read in full symbols for PST, and anything it depends on. */
8773
8774 void
8775 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8776 {
8777 if (readin)
8778 return;
8779
8780 expand_dependencies (objfile);
8781
8782 dw2_do_instantiate_symtab (per_cu_data, false);
8783 gdb_assert (get_compunit_symtab () != nullptr);
8784 }
8785
8786 /* Trivial hash function for die_info: the hash value of a DIE
8787 is its offset in .debug_info for this objfile. */
8788
8789 static hashval_t
8790 die_hash (const void *item)
8791 {
8792 const struct die_info *die = (const struct die_info *) item;
8793
8794 return to_underlying (die->sect_off);
8795 }
8796
8797 /* Trivial comparison function for die_info structures: two DIEs
8798 are equal if they have the same offset. */
8799
8800 static int
8801 die_eq (const void *item_lhs, const void *item_rhs)
8802 {
8803 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8804 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8805
8806 return die_lhs->sect_off == die_rhs->sect_off;
8807 }
8808
8809 /* Load the DIEs associated with PER_CU into memory. */
8810
8811 static void
8812 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8813 bool skip_partial,
8814 enum language pretend_language)
8815 {
8816 gdb_assert (! this_cu->is_debug_types);
8817
8818 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8819 if (reader.dummy_p)
8820 return;
8821
8822 struct dwarf2_cu *cu = reader.cu;
8823 const gdb_byte *info_ptr = reader.info_ptr;
8824
8825 gdb_assert (cu->die_hash == NULL);
8826 cu->die_hash =
8827 htab_create_alloc_ex (cu->header.length / 12,
8828 die_hash,
8829 die_eq,
8830 NULL,
8831 &cu->comp_unit_obstack,
8832 hashtab_obstack_allocate,
8833 dummy_obstack_deallocate);
8834
8835 if (reader.comp_unit_die->has_children)
8836 reader.comp_unit_die->child
8837 = read_die_and_siblings (&reader, reader.info_ptr,
8838 &info_ptr, reader.comp_unit_die);
8839 cu->dies = reader.comp_unit_die;
8840 /* comp_unit_die is not stored in die_hash, no need. */
8841
8842 /* We try not to read any attributes in this function, because not
8843 all CUs needed for references have been loaded yet, and symbol
8844 table processing isn't initialized. But we have to set the CU language,
8845 or we won't be able to build types correctly.
8846 Similarly, if we do not read the producer, we can not apply
8847 producer-specific interpretation. */
8848 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8849
8850 reader.keep ();
8851 }
8852
8853 /* Add a DIE to the delayed physname list. */
8854
8855 static void
8856 add_to_method_list (struct type *type, int fnfield_index, int index,
8857 const char *name, struct die_info *die,
8858 struct dwarf2_cu *cu)
8859 {
8860 struct delayed_method_info mi;
8861 mi.type = type;
8862 mi.fnfield_index = fnfield_index;
8863 mi.index = index;
8864 mi.name = name;
8865 mi.die = die;
8866 cu->method_list.push_back (mi);
8867 }
8868
8869 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8870 "const" / "volatile". If so, decrements LEN by the length of the
8871 modifier and return true. Otherwise return false. */
8872
8873 template<size_t N>
8874 static bool
8875 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8876 {
8877 size_t mod_len = sizeof (mod) - 1;
8878 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8879 {
8880 len -= mod_len;
8881 return true;
8882 }
8883 return false;
8884 }
8885
8886 /* Compute the physnames of any methods on the CU's method list.
8887
8888 The computation of method physnames is delayed in order to avoid the
8889 (bad) condition that one of the method's formal parameters is of an as yet
8890 incomplete type. */
8891
8892 static void
8893 compute_delayed_physnames (struct dwarf2_cu *cu)
8894 {
8895 /* Only C++ delays computing physnames. */
8896 if (cu->method_list.empty ())
8897 return;
8898 gdb_assert (cu->language == language_cplus);
8899
8900 for (const delayed_method_info &mi : cu->method_list)
8901 {
8902 const char *physname;
8903 struct fn_fieldlist *fn_flp
8904 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8905 physname = dwarf2_physname (mi.name, mi.die, cu);
8906 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8907 = physname ? physname : "";
8908
8909 /* Since there's no tag to indicate whether a method is a
8910 const/volatile overload, extract that information out of the
8911 demangled name. */
8912 if (physname != NULL)
8913 {
8914 size_t len = strlen (physname);
8915
8916 while (1)
8917 {
8918 if (physname[len] == ')') /* shortcut */
8919 break;
8920 else if (check_modifier (physname, len, " const"))
8921 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8922 else if (check_modifier (physname, len, " volatile"))
8923 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8924 else
8925 break;
8926 }
8927 }
8928 }
8929
8930 /* The list is no longer needed. */
8931 cu->method_list.clear ();
8932 }
8933
8934 /* Go objects should be embedded in a DW_TAG_module DIE,
8935 and it's not clear if/how imported objects will appear.
8936 To keep Go support simple until that's worked out,
8937 go back through what we've read and create something usable.
8938 We could do this while processing each DIE, and feels kinda cleaner,
8939 but that way is more invasive.
8940 This is to, for example, allow the user to type "p var" or "b main"
8941 without having to specify the package name, and allow lookups
8942 of module.object to work in contexts that use the expression
8943 parser. */
8944
8945 static void
8946 fixup_go_packaging (struct dwarf2_cu *cu)
8947 {
8948 gdb::unique_xmalloc_ptr<char> package_name;
8949 struct pending *list;
8950 int i;
8951
8952 for (list = *cu->get_builder ()->get_global_symbols ();
8953 list != NULL;
8954 list = list->next)
8955 {
8956 for (i = 0; i < list->nsyms; ++i)
8957 {
8958 struct symbol *sym = list->symbol[i];
8959
8960 if (sym->language () == language_go
8961 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8962 {
8963 gdb::unique_xmalloc_ptr<char> this_package_name
8964 (go_symbol_package_name (sym));
8965
8966 if (this_package_name == NULL)
8967 continue;
8968 if (package_name == NULL)
8969 package_name = std::move (this_package_name);
8970 else
8971 {
8972 struct objfile *objfile
8973 = cu->per_cu->dwarf2_per_objfile->objfile;
8974 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
8975 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
8976 (symbol_symtab (sym) != NULL
8977 ? symtab_to_filename_for_display
8978 (symbol_symtab (sym))
8979 : objfile_name (objfile)),
8980 this_package_name.get (), package_name.get ());
8981 }
8982 }
8983 }
8984 }
8985
8986 if (package_name != NULL)
8987 {
8988 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8989 const char *saved_package_name = objfile->intern (package_name.get ());
8990 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
8991 saved_package_name);
8992 struct symbol *sym;
8993
8994 sym = allocate_symbol (objfile);
8995 sym->set_language (language_go, &objfile->objfile_obstack);
8996 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
8997 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
8998 e.g., "main" finds the "main" module and not C's main(). */
8999 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9000 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9001 SYMBOL_TYPE (sym) = type;
9002
9003 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9004 }
9005 }
9006
9007 /* Allocate a fully-qualified name consisting of the two parts on the
9008 obstack. */
9009
9010 static const char *
9011 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9012 {
9013 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9014 }
9015
9016 /* A helper that allocates a struct discriminant_info to attach to a
9017 union type. */
9018
9019 static struct discriminant_info *
9020 alloc_discriminant_info (struct type *type, int discriminant_index,
9021 int default_index)
9022 {
9023 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9024 gdb_assert (discriminant_index == -1
9025 || (discriminant_index >= 0
9026 && discriminant_index < TYPE_NFIELDS (type)));
9027 gdb_assert (default_index == -1
9028 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9029
9030 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9031
9032 struct discriminant_info *disc
9033 = ((struct discriminant_info *)
9034 TYPE_ZALLOC (type,
9035 offsetof (struct discriminant_info, discriminants)
9036 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9037 disc->default_index = default_index;
9038 disc->discriminant_index = discriminant_index;
9039
9040 struct dynamic_prop prop;
9041 prop.kind = PROP_UNDEFINED;
9042 prop.data.baton = disc;
9043
9044 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9045
9046 return disc;
9047 }
9048
9049 /* Some versions of rustc emitted enums in an unusual way.
9050
9051 Ordinary enums were emitted as unions. The first element of each
9052 structure in the union was named "RUST$ENUM$DISR". This element
9053 held the discriminant.
9054
9055 These versions of Rust also implemented the "non-zero"
9056 optimization. When the enum had two values, and one is empty and
9057 the other holds a pointer that cannot be zero, the pointer is used
9058 as the discriminant, with a zero value meaning the empty variant.
9059 Here, the union's first member is of the form
9060 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9061 where the fieldnos are the indices of the fields that should be
9062 traversed in order to find the field (which may be several fields deep)
9063 and the variantname is the name of the variant of the case when the
9064 field is zero.
9065
9066 This function recognizes whether TYPE is of one of these forms,
9067 and, if so, smashes it to be a variant type. */
9068
9069 static void
9070 quirk_rust_enum (struct type *type, struct objfile *objfile)
9071 {
9072 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9073
9074 /* We don't need to deal with empty enums. */
9075 if (TYPE_NFIELDS (type) == 0)
9076 return;
9077
9078 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9079 if (TYPE_NFIELDS (type) == 1
9080 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9081 {
9082 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9083
9084 /* Decode the field name to find the offset of the
9085 discriminant. */
9086 ULONGEST bit_offset = 0;
9087 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9088 while (name[0] >= '0' && name[0] <= '9')
9089 {
9090 char *tail;
9091 unsigned long index = strtoul (name, &tail, 10);
9092 name = tail;
9093 if (*name != '$'
9094 || index >= TYPE_NFIELDS (field_type)
9095 || (TYPE_FIELD_LOC_KIND (field_type, index)
9096 != FIELD_LOC_KIND_BITPOS))
9097 {
9098 complaint (_("Could not parse Rust enum encoding string \"%s\""
9099 "[in module %s]"),
9100 TYPE_FIELD_NAME (type, 0),
9101 objfile_name (objfile));
9102 return;
9103 }
9104 ++name;
9105
9106 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9107 field_type = TYPE_FIELD_TYPE (field_type, index);
9108 }
9109
9110 /* Make a union to hold the variants. */
9111 struct type *union_type = alloc_type (objfile);
9112 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9113 TYPE_NFIELDS (union_type) = 3;
9114 TYPE_FIELDS (union_type)
9115 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9116 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9117 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9118
9119 /* Put the discriminant must at index 0. */
9120 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9121 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9122 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9123 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9124
9125 /* The order of fields doesn't really matter, so put the real
9126 field at index 1 and the data-less field at index 2. */
9127 struct discriminant_info *disc
9128 = alloc_discriminant_info (union_type, 0, 1);
9129 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9130 TYPE_FIELD_NAME (union_type, 1)
9131 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9132 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9133 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9134 TYPE_FIELD_NAME (union_type, 1));
9135
9136 const char *dataless_name
9137 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9138 name);
9139 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9140 dataless_name);
9141 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9142 /* NAME points into the original discriminant name, which
9143 already has the correct lifetime. */
9144 TYPE_FIELD_NAME (union_type, 2) = name;
9145 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9146 disc->discriminants[2] = 0;
9147
9148 /* Smash this type to be a structure type. We have to do this
9149 because the type has already been recorded. */
9150 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9151 TYPE_NFIELDS (type) = 1;
9152 TYPE_FIELDS (type)
9153 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9154
9155 /* Install the variant part. */
9156 TYPE_FIELD_TYPE (type, 0) = union_type;
9157 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9158 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9159 }
9160 /* A union with a single anonymous field is probably an old-style
9161 univariant enum. */
9162 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9163 {
9164 /* Smash this type to be a structure type. We have to do this
9165 because the type has already been recorded. */
9166 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9167
9168 /* Make a union to hold the variants. */
9169 struct type *union_type = alloc_type (objfile);
9170 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9171 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9172 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9173 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9174 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9175
9176 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9177 const char *variant_name
9178 = rust_last_path_segment (TYPE_NAME (field_type));
9179 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9180 TYPE_NAME (field_type)
9181 = rust_fully_qualify (&objfile->objfile_obstack,
9182 TYPE_NAME (type), variant_name);
9183
9184 /* Install the union in the outer struct type. */
9185 TYPE_NFIELDS (type) = 1;
9186 TYPE_FIELDS (type)
9187 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9188 TYPE_FIELD_TYPE (type, 0) = union_type;
9189 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9190 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9191
9192 alloc_discriminant_info (union_type, -1, 0);
9193 }
9194 else
9195 {
9196 struct type *disr_type = nullptr;
9197 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9198 {
9199 disr_type = TYPE_FIELD_TYPE (type, i);
9200
9201 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9202 {
9203 /* All fields of a true enum will be structs. */
9204 return;
9205 }
9206 else if (TYPE_NFIELDS (disr_type) == 0)
9207 {
9208 /* Could be data-less variant, so keep going. */
9209 disr_type = nullptr;
9210 }
9211 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9212 "RUST$ENUM$DISR") != 0)
9213 {
9214 /* Not a Rust enum. */
9215 return;
9216 }
9217 else
9218 {
9219 /* Found one. */
9220 break;
9221 }
9222 }
9223
9224 /* If we got here without a discriminant, then it's probably
9225 just a union. */
9226 if (disr_type == nullptr)
9227 return;
9228
9229 /* Smash this type to be a structure type. We have to do this
9230 because the type has already been recorded. */
9231 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9232
9233 /* Make a union to hold the variants. */
9234 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9235 struct type *union_type = alloc_type (objfile);
9236 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9237 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9238 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9239 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9240 TYPE_FIELDS (union_type)
9241 = (struct field *) TYPE_ZALLOC (union_type,
9242 (TYPE_NFIELDS (union_type)
9243 * sizeof (struct field)));
9244
9245 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9246 TYPE_NFIELDS (type) * sizeof (struct field));
9247
9248 /* Install the discriminant at index 0 in the union. */
9249 TYPE_FIELD (union_type, 0) = *disr_field;
9250 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9251 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9252
9253 /* Install the union in the outer struct type. */
9254 TYPE_FIELD_TYPE (type, 0) = union_type;
9255 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9256 TYPE_NFIELDS (type) = 1;
9257
9258 /* Set the size and offset of the union type. */
9259 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9260
9261 /* We need a way to find the correct discriminant given a
9262 variant name. For convenience we build a map here. */
9263 struct type *enum_type = FIELD_TYPE (*disr_field);
9264 std::unordered_map<std::string, ULONGEST> discriminant_map;
9265 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9266 {
9267 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9268 {
9269 const char *name
9270 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9271 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9272 }
9273 }
9274
9275 int n_fields = TYPE_NFIELDS (union_type);
9276 struct discriminant_info *disc
9277 = alloc_discriminant_info (union_type, 0, -1);
9278 /* Skip the discriminant here. */
9279 for (int i = 1; i < n_fields; ++i)
9280 {
9281 /* Find the final word in the name of this variant's type.
9282 That name can be used to look up the correct
9283 discriminant. */
9284 const char *variant_name
9285 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9286 i)));
9287
9288 auto iter = discriminant_map.find (variant_name);
9289 if (iter != discriminant_map.end ())
9290 disc->discriminants[i] = iter->second;
9291
9292 /* Remove the discriminant field, if it exists. */
9293 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9294 if (TYPE_NFIELDS (sub_type) > 0)
9295 {
9296 --TYPE_NFIELDS (sub_type);
9297 ++TYPE_FIELDS (sub_type);
9298 }
9299 TYPE_FIELD_NAME (union_type, i) = variant_name;
9300 TYPE_NAME (sub_type)
9301 = rust_fully_qualify (&objfile->objfile_obstack,
9302 TYPE_NAME (type), variant_name);
9303 }
9304 }
9305 }
9306
9307 /* Rewrite some Rust unions to be structures with variants parts. */
9308
9309 static void
9310 rust_union_quirks (struct dwarf2_cu *cu)
9311 {
9312 gdb_assert (cu->language == language_rust);
9313 for (type *type_ : cu->rust_unions)
9314 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9315 /* We don't need this any more. */
9316 cu->rust_unions.clear ();
9317 }
9318
9319 /* Return the symtab for PER_CU. This works properly regardless of
9320 whether we're using the index or psymtabs. */
9321
9322 static struct compunit_symtab *
9323 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9324 {
9325 return (per_cu->dwarf2_per_objfile->using_index
9326 ? per_cu->v.quick->compunit_symtab
9327 : per_cu->v.psymtab->compunit_symtab);
9328 }
9329
9330 /* A helper function for computing the list of all symbol tables
9331 included by PER_CU. */
9332
9333 static void
9334 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9335 htab_t all_children, htab_t all_type_symtabs,
9336 struct dwarf2_per_cu_data *per_cu,
9337 struct compunit_symtab *immediate_parent)
9338 {
9339 void **slot;
9340 struct compunit_symtab *cust;
9341
9342 slot = htab_find_slot (all_children, per_cu, INSERT);
9343 if (*slot != NULL)
9344 {
9345 /* This inclusion and its children have been processed. */
9346 return;
9347 }
9348
9349 *slot = per_cu;
9350 /* Only add a CU if it has a symbol table. */
9351 cust = get_compunit_symtab (per_cu);
9352 if (cust != NULL)
9353 {
9354 /* If this is a type unit only add its symbol table if we haven't
9355 seen it yet (type unit per_cu's can share symtabs). */
9356 if (per_cu->is_debug_types)
9357 {
9358 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9359 if (*slot == NULL)
9360 {
9361 *slot = cust;
9362 result->push_back (cust);
9363 if (cust->user == NULL)
9364 cust->user = immediate_parent;
9365 }
9366 }
9367 else
9368 {
9369 result->push_back (cust);
9370 if (cust->user == NULL)
9371 cust->user = immediate_parent;
9372 }
9373 }
9374
9375 if (!per_cu->imported_symtabs_empty ())
9376 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9377 {
9378 recursively_compute_inclusions (result, all_children,
9379 all_type_symtabs, ptr, cust);
9380 }
9381 }
9382
9383 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9384 PER_CU. */
9385
9386 static void
9387 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9388 {
9389 gdb_assert (! per_cu->is_debug_types);
9390
9391 if (!per_cu->imported_symtabs_empty ())
9392 {
9393 int len;
9394 std::vector<compunit_symtab *> result_symtabs;
9395 htab_t all_children, all_type_symtabs;
9396 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9397
9398 /* If we don't have a symtab, we can just skip this case. */
9399 if (cust == NULL)
9400 return;
9401
9402 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9403 NULL, xcalloc, xfree);
9404 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9405 NULL, xcalloc, xfree);
9406
9407 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9408 {
9409 recursively_compute_inclusions (&result_symtabs, all_children,
9410 all_type_symtabs, ptr, cust);
9411 }
9412
9413 /* Now we have a transitive closure of all the included symtabs. */
9414 len = result_symtabs.size ();
9415 cust->includes
9416 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9417 struct compunit_symtab *, len + 1);
9418 memcpy (cust->includes, result_symtabs.data (),
9419 len * sizeof (compunit_symtab *));
9420 cust->includes[len] = NULL;
9421
9422 htab_delete (all_children);
9423 htab_delete (all_type_symtabs);
9424 }
9425 }
9426
9427 /* Compute the 'includes' field for the symtabs of all the CUs we just
9428 read. */
9429
9430 static void
9431 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9432 {
9433 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9434 {
9435 if (! iter->is_debug_types)
9436 compute_compunit_symtab_includes (iter);
9437 }
9438
9439 dwarf2_per_objfile->just_read_cus.clear ();
9440 }
9441
9442 /* Generate full symbol information for PER_CU, whose DIEs have
9443 already been loaded into memory. */
9444
9445 static void
9446 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9447 enum language pretend_language)
9448 {
9449 struct dwarf2_cu *cu = per_cu->cu;
9450 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9451 struct objfile *objfile = dwarf2_per_objfile->objfile;
9452 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9453 CORE_ADDR lowpc, highpc;
9454 struct compunit_symtab *cust;
9455 CORE_ADDR baseaddr;
9456 struct block *static_block;
9457 CORE_ADDR addr;
9458
9459 baseaddr = objfile->text_section_offset ();
9460
9461 /* Clear the list here in case something was left over. */
9462 cu->method_list.clear ();
9463
9464 cu->language = pretend_language;
9465 cu->language_defn = language_def (cu->language);
9466
9467 /* Do line number decoding in read_file_scope () */
9468 process_die (cu->dies, cu);
9469
9470 /* For now fudge the Go package. */
9471 if (cu->language == language_go)
9472 fixup_go_packaging (cu);
9473
9474 /* Now that we have processed all the DIEs in the CU, all the types
9475 should be complete, and it should now be safe to compute all of the
9476 physnames. */
9477 compute_delayed_physnames (cu);
9478
9479 if (cu->language == language_rust)
9480 rust_union_quirks (cu);
9481
9482 /* Some compilers don't define a DW_AT_high_pc attribute for the
9483 compilation unit. If the DW_AT_high_pc is missing, synthesize
9484 it, by scanning the DIE's below the compilation unit. */
9485 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9486
9487 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9488 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9489
9490 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9491 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9492 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9493 addrmap to help ensure it has an accurate map of pc values belonging to
9494 this comp unit. */
9495 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9496
9497 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9498 SECT_OFF_TEXT (objfile),
9499 0);
9500
9501 if (cust != NULL)
9502 {
9503 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9504
9505 /* Set symtab language to language from DW_AT_language. If the
9506 compilation is from a C file generated by language preprocessors, do
9507 not set the language if it was already deduced by start_subfile. */
9508 if (!(cu->language == language_c
9509 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9510 COMPUNIT_FILETABS (cust)->language = cu->language;
9511
9512 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9513 produce DW_AT_location with location lists but it can be possibly
9514 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9515 there were bugs in prologue debug info, fixed later in GCC-4.5
9516 by "unwind info for epilogues" patch (which is not directly related).
9517
9518 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9519 needed, it would be wrong due to missing DW_AT_producer there.
9520
9521 Still one can confuse GDB by using non-standard GCC compilation
9522 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9523 */
9524 if (cu->has_loclist && gcc_4_minor >= 5)
9525 cust->locations_valid = 1;
9526
9527 if (gcc_4_minor >= 5)
9528 cust->epilogue_unwind_valid = 1;
9529
9530 cust->call_site_htab = cu->call_site_htab;
9531 }
9532
9533 if (dwarf2_per_objfile->using_index)
9534 per_cu->v.quick->compunit_symtab = cust;
9535 else
9536 {
9537 dwarf2_psymtab *pst = per_cu->v.psymtab;
9538 pst->compunit_symtab = cust;
9539 pst->readin = true;
9540 }
9541
9542 /* Push it for inclusion processing later. */
9543 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9544
9545 /* Not needed any more. */
9546 cu->reset_builder ();
9547 }
9548
9549 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9550 already been loaded into memory. */
9551
9552 static void
9553 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9554 enum language pretend_language)
9555 {
9556 struct dwarf2_cu *cu = per_cu->cu;
9557 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9558 struct objfile *objfile = dwarf2_per_objfile->objfile;
9559 struct compunit_symtab *cust;
9560 struct signatured_type *sig_type;
9561
9562 gdb_assert (per_cu->is_debug_types);
9563 sig_type = (struct signatured_type *) per_cu;
9564
9565 /* Clear the list here in case something was left over. */
9566 cu->method_list.clear ();
9567
9568 cu->language = pretend_language;
9569 cu->language_defn = language_def (cu->language);
9570
9571 /* The symbol tables are set up in read_type_unit_scope. */
9572 process_die (cu->dies, cu);
9573
9574 /* For now fudge the Go package. */
9575 if (cu->language == language_go)
9576 fixup_go_packaging (cu);
9577
9578 /* Now that we have processed all the DIEs in the CU, all the types
9579 should be complete, and it should now be safe to compute all of the
9580 physnames. */
9581 compute_delayed_physnames (cu);
9582
9583 if (cu->language == language_rust)
9584 rust_union_quirks (cu);
9585
9586 /* TUs share symbol tables.
9587 If this is the first TU to use this symtab, complete the construction
9588 of it with end_expandable_symtab. Otherwise, complete the addition of
9589 this TU's symbols to the existing symtab. */
9590 if (sig_type->type_unit_group->compunit_symtab == NULL)
9591 {
9592 buildsym_compunit *builder = cu->get_builder ();
9593 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9594 sig_type->type_unit_group->compunit_symtab = cust;
9595
9596 if (cust != NULL)
9597 {
9598 /* Set symtab language to language from DW_AT_language. If the
9599 compilation is from a C file generated by language preprocessors,
9600 do not set the language if it was already deduced by
9601 start_subfile. */
9602 if (!(cu->language == language_c
9603 && COMPUNIT_FILETABS (cust)->language != language_c))
9604 COMPUNIT_FILETABS (cust)->language = cu->language;
9605 }
9606 }
9607 else
9608 {
9609 cu->get_builder ()->augment_type_symtab ();
9610 cust = sig_type->type_unit_group->compunit_symtab;
9611 }
9612
9613 if (dwarf2_per_objfile->using_index)
9614 per_cu->v.quick->compunit_symtab = cust;
9615 else
9616 {
9617 dwarf2_psymtab *pst = per_cu->v.psymtab;
9618 pst->compunit_symtab = cust;
9619 pst->readin = true;
9620 }
9621
9622 /* Not needed any more. */
9623 cu->reset_builder ();
9624 }
9625
9626 /* Process an imported unit DIE. */
9627
9628 static void
9629 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9630 {
9631 struct attribute *attr;
9632
9633 /* For now we don't handle imported units in type units. */
9634 if (cu->per_cu->is_debug_types)
9635 {
9636 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9637 " supported in type units [in module %s]"),
9638 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9639 }
9640
9641 attr = dwarf2_attr (die, DW_AT_import, cu);
9642 if (attr != NULL)
9643 {
9644 sect_offset sect_off = attr->get_ref_die_offset ();
9645 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9646 dwarf2_per_cu_data *per_cu
9647 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9648 cu->per_cu->dwarf2_per_objfile);
9649
9650 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9651 into another compilation unit, at root level. Regard this as a hint,
9652 and ignore it. */
9653 if (die->parent && die->parent->parent == NULL
9654 && per_cu->unit_type == DW_UT_compile
9655 && per_cu->lang == language_cplus)
9656 return;
9657
9658 /* If necessary, add it to the queue and load its DIEs. */
9659 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9660 load_full_comp_unit (per_cu, false, cu->language);
9661
9662 cu->per_cu->imported_symtabs_push (per_cu);
9663 }
9664 }
9665
9666 /* RAII object that represents a process_die scope: i.e.,
9667 starts/finishes processing a DIE. */
9668 class process_die_scope
9669 {
9670 public:
9671 process_die_scope (die_info *die, dwarf2_cu *cu)
9672 : m_die (die), m_cu (cu)
9673 {
9674 /* We should only be processing DIEs not already in process. */
9675 gdb_assert (!m_die->in_process);
9676 m_die->in_process = true;
9677 }
9678
9679 ~process_die_scope ()
9680 {
9681 m_die->in_process = false;
9682
9683 /* If we're done processing the DIE for the CU that owns the line
9684 header, we don't need the line header anymore. */
9685 if (m_cu->line_header_die_owner == m_die)
9686 {
9687 delete m_cu->line_header;
9688 m_cu->line_header = NULL;
9689 m_cu->line_header_die_owner = NULL;
9690 }
9691 }
9692
9693 private:
9694 die_info *m_die;
9695 dwarf2_cu *m_cu;
9696 };
9697
9698 /* Process a die and its children. */
9699
9700 static void
9701 process_die (struct die_info *die, struct dwarf2_cu *cu)
9702 {
9703 process_die_scope scope (die, cu);
9704
9705 switch (die->tag)
9706 {
9707 case DW_TAG_padding:
9708 break;
9709 case DW_TAG_compile_unit:
9710 case DW_TAG_partial_unit:
9711 read_file_scope (die, cu);
9712 break;
9713 case DW_TAG_type_unit:
9714 read_type_unit_scope (die, cu);
9715 break;
9716 case DW_TAG_subprogram:
9717 /* Nested subprograms in Fortran get a prefix. */
9718 if (cu->language == language_fortran
9719 && die->parent != NULL
9720 && die->parent->tag == DW_TAG_subprogram)
9721 cu->processing_has_namespace_info = true;
9722 /* Fall through. */
9723 case DW_TAG_inlined_subroutine:
9724 read_func_scope (die, cu);
9725 break;
9726 case DW_TAG_lexical_block:
9727 case DW_TAG_try_block:
9728 case DW_TAG_catch_block:
9729 read_lexical_block_scope (die, cu);
9730 break;
9731 case DW_TAG_call_site:
9732 case DW_TAG_GNU_call_site:
9733 read_call_site_scope (die, cu);
9734 break;
9735 case DW_TAG_class_type:
9736 case DW_TAG_interface_type:
9737 case DW_TAG_structure_type:
9738 case DW_TAG_union_type:
9739 process_structure_scope (die, cu);
9740 break;
9741 case DW_TAG_enumeration_type:
9742 process_enumeration_scope (die, cu);
9743 break;
9744
9745 /* These dies have a type, but processing them does not create
9746 a symbol or recurse to process the children. Therefore we can
9747 read them on-demand through read_type_die. */
9748 case DW_TAG_subroutine_type:
9749 case DW_TAG_set_type:
9750 case DW_TAG_array_type:
9751 case DW_TAG_pointer_type:
9752 case DW_TAG_ptr_to_member_type:
9753 case DW_TAG_reference_type:
9754 case DW_TAG_rvalue_reference_type:
9755 case DW_TAG_string_type:
9756 break;
9757
9758 case DW_TAG_base_type:
9759 case DW_TAG_subrange_type:
9760 case DW_TAG_typedef:
9761 /* Add a typedef symbol for the type definition, if it has a
9762 DW_AT_name. */
9763 new_symbol (die, read_type_die (die, cu), cu);
9764 break;
9765 case DW_TAG_common_block:
9766 read_common_block (die, cu);
9767 break;
9768 case DW_TAG_common_inclusion:
9769 break;
9770 case DW_TAG_namespace:
9771 cu->processing_has_namespace_info = true;
9772 read_namespace (die, cu);
9773 break;
9774 case DW_TAG_module:
9775 cu->processing_has_namespace_info = true;
9776 read_module (die, cu);
9777 break;
9778 case DW_TAG_imported_declaration:
9779 cu->processing_has_namespace_info = true;
9780 if (read_namespace_alias (die, cu))
9781 break;
9782 /* The declaration is not a global namespace alias. */
9783 /* Fall through. */
9784 case DW_TAG_imported_module:
9785 cu->processing_has_namespace_info = true;
9786 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9787 || cu->language != language_fortran))
9788 complaint (_("Tag '%s' has unexpected children"),
9789 dwarf_tag_name (die->tag));
9790 read_import_statement (die, cu);
9791 break;
9792
9793 case DW_TAG_imported_unit:
9794 process_imported_unit_die (die, cu);
9795 break;
9796
9797 case DW_TAG_variable:
9798 read_variable (die, cu);
9799 break;
9800
9801 default:
9802 new_symbol (die, NULL, cu);
9803 break;
9804 }
9805 }
9806 \f
9807 /* DWARF name computation. */
9808
9809 /* A helper function for dwarf2_compute_name which determines whether DIE
9810 needs to have the name of the scope prepended to the name listed in the
9811 die. */
9812
9813 static int
9814 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9815 {
9816 struct attribute *attr;
9817
9818 switch (die->tag)
9819 {
9820 case DW_TAG_namespace:
9821 case DW_TAG_typedef:
9822 case DW_TAG_class_type:
9823 case DW_TAG_interface_type:
9824 case DW_TAG_structure_type:
9825 case DW_TAG_union_type:
9826 case DW_TAG_enumeration_type:
9827 case DW_TAG_enumerator:
9828 case DW_TAG_subprogram:
9829 case DW_TAG_inlined_subroutine:
9830 case DW_TAG_member:
9831 case DW_TAG_imported_declaration:
9832 return 1;
9833
9834 case DW_TAG_variable:
9835 case DW_TAG_constant:
9836 /* We only need to prefix "globally" visible variables. These include
9837 any variable marked with DW_AT_external or any variable that
9838 lives in a namespace. [Variables in anonymous namespaces
9839 require prefixing, but they are not DW_AT_external.] */
9840
9841 if (dwarf2_attr (die, DW_AT_specification, cu))
9842 {
9843 struct dwarf2_cu *spec_cu = cu;
9844
9845 return die_needs_namespace (die_specification (die, &spec_cu),
9846 spec_cu);
9847 }
9848
9849 attr = dwarf2_attr (die, DW_AT_external, cu);
9850 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9851 && die->parent->tag != DW_TAG_module)
9852 return 0;
9853 /* A variable in a lexical block of some kind does not need a
9854 namespace, even though in C++ such variables may be external
9855 and have a mangled name. */
9856 if (die->parent->tag == DW_TAG_lexical_block
9857 || die->parent->tag == DW_TAG_try_block
9858 || die->parent->tag == DW_TAG_catch_block
9859 || die->parent->tag == DW_TAG_subprogram)
9860 return 0;
9861 return 1;
9862
9863 default:
9864 return 0;
9865 }
9866 }
9867
9868 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9869 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9870 defined for the given DIE. */
9871
9872 static struct attribute *
9873 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9874 {
9875 struct attribute *attr;
9876
9877 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9878 if (attr == NULL)
9879 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9880
9881 return attr;
9882 }
9883
9884 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9885 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9886 defined for the given DIE. */
9887
9888 static const char *
9889 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9890 {
9891 const char *linkage_name;
9892
9893 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9894 if (linkage_name == NULL)
9895 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9896
9897 return linkage_name;
9898 }
9899
9900 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9901 compute the physname for the object, which include a method's:
9902 - formal parameters (C++),
9903 - receiver type (Go),
9904
9905 The term "physname" is a bit confusing.
9906 For C++, for example, it is the demangled name.
9907 For Go, for example, it's the mangled name.
9908
9909 For Ada, return the DIE's linkage name rather than the fully qualified
9910 name. PHYSNAME is ignored..
9911
9912 The result is allocated on the objfile_obstack and canonicalized. */
9913
9914 static const char *
9915 dwarf2_compute_name (const char *name,
9916 struct die_info *die, struct dwarf2_cu *cu,
9917 int physname)
9918 {
9919 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9920
9921 if (name == NULL)
9922 name = dwarf2_name (die, cu);
9923
9924 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9925 but otherwise compute it by typename_concat inside GDB.
9926 FIXME: Actually this is not really true, or at least not always true.
9927 It's all very confusing. compute_and_set_names doesn't try to demangle
9928 Fortran names because there is no mangling standard. So new_symbol
9929 will set the demangled name to the result of dwarf2_full_name, and it is
9930 the demangled name that GDB uses if it exists. */
9931 if (cu->language == language_ada
9932 || (cu->language == language_fortran && physname))
9933 {
9934 /* For Ada unit, we prefer the linkage name over the name, as
9935 the former contains the exported name, which the user expects
9936 to be able to reference. Ideally, we want the user to be able
9937 to reference this entity using either natural or linkage name,
9938 but we haven't started looking at this enhancement yet. */
9939 const char *linkage_name = dw2_linkage_name (die, cu);
9940
9941 if (linkage_name != NULL)
9942 return linkage_name;
9943 }
9944
9945 /* These are the only languages we know how to qualify names in. */
9946 if (name != NULL
9947 && (cu->language == language_cplus
9948 || cu->language == language_fortran || cu->language == language_d
9949 || cu->language == language_rust))
9950 {
9951 if (die_needs_namespace (die, cu))
9952 {
9953 const char *prefix;
9954 const char *canonical_name = NULL;
9955
9956 string_file buf;
9957
9958 prefix = determine_prefix (die, cu);
9959 if (*prefix != '\0')
9960 {
9961 gdb::unique_xmalloc_ptr<char> prefixed_name
9962 (typename_concat (NULL, prefix, name, physname, cu));
9963
9964 buf.puts (prefixed_name.get ());
9965 }
9966 else
9967 buf.puts (name);
9968
9969 /* Template parameters may be specified in the DIE's DW_AT_name, or
9970 as children with DW_TAG_template_type_param or
9971 DW_TAG_value_type_param. If the latter, add them to the name
9972 here. If the name already has template parameters, then
9973 skip this step; some versions of GCC emit both, and
9974 it is more efficient to use the pre-computed name.
9975
9976 Something to keep in mind about this process: it is very
9977 unlikely, or in some cases downright impossible, to produce
9978 something that will match the mangled name of a function.
9979 If the definition of the function has the same debug info,
9980 we should be able to match up with it anyway. But fallbacks
9981 using the minimal symbol, for instance to find a method
9982 implemented in a stripped copy of libstdc++, will not work.
9983 If we do not have debug info for the definition, we will have to
9984 match them up some other way.
9985
9986 When we do name matching there is a related problem with function
9987 templates; two instantiated function templates are allowed to
9988 differ only by their return types, which we do not add here. */
9989
9990 if (cu->language == language_cplus && strchr (name, '<') == NULL)
9991 {
9992 struct attribute *attr;
9993 struct die_info *child;
9994 int first = 1;
9995
9996 die->building_fullname = 1;
9997
9998 for (child = die->child; child != NULL; child = child->sibling)
9999 {
10000 struct type *type;
10001 LONGEST value;
10002 const gdb_byte *bytes;
10003 struct dwarf2_locexpr_baton *baton;
10004 struct value *v;
10005
10006 if (child->tag != DW_TAG_template_type_param
10007 && child->tag != DW_TAG_template_value_param)
10008 continue;
10009
10010 if (first)
10011 {
10012 buf.puts ("<");
10013 first = 0;
10014 }
10015 else
10016 buf.puts (", ");
10017
10018 attr = dwarf2_attr (child, DW_AT_type, cu);
10019 if (attr == NULL)
10020 {
10021 complaint (_("template parameter missing DW_AT_type"));
10022 buf.puts ("UNKNOWN_TYPE");
10023 continue;
10024 }
10025 type = die_type (child, cu);
10026
10027 if (child->tag == DW_TAG_template_type_param)
10028 {
10029 c_print_type (type, "", &buf, -1, 0, cu->language,
10030 &type_print_raw_options);
10031 continue;
10032 }
10033
10034 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10035 if (attr == NULL)
10036 {
10037 complaint (_("template parameter missing "
10038 "DW_AT_const_value"));
10039 buf.puts ("UNKNOWN_VALUE");
10040 continue;
10041 }
10042
10043 dwarf2_const_value_attr (attr, type, name,
10044 &cu->comp_unit_obstack, cu,
10045 &value, &bytes, &baton);
10046
10047 if (TYPE_NOSIGN (type))
10048 /* GDB prints characters as NUMBER 'CHAR'. If that's
10049 changed, this can use value_print instead. */
10050 c_printchar (value, type, &buf);
10051 else
10052 {
10053 struct value_print_options opts;
10054
10055 if (baton != NULL)
10056 v = dwarf2_evaluate_loc_desc (type, NULL,
10057 baton->data,
10058 baton->size,
10059 baton->per_cu);
10060 else if (bytes != NULL)
10061 {
10062 v = allocate_value (type);
10063 memcpy (value_contents_writeable (v), bytes,
10064 TYPE_LENGTH (type));
10065 }
10066 else
10067 v = value_from_longest (type, value);
10068
10069 /* Specify decimal so that we do not depend on
10070 the radix. */
10071 get_formatted_print_options (&opts, 'd');
10072 opts.raw = 1;
10073 value_print (v, &buf, &opts);
10074 release_value (v);
10075 }
10076 }
10077
10078 die->building_fullname = 0;
10079
10080 if (!first)
10081 {
10082 /* Close the argument list, with a space if necessary
10083 (nested templates). */
10084 if (!buf.empty () && buf.string ().back () == '>')
10085 buf.puts (" >");
10086 else
10087 buf.puts (">");
10088 }
10089 }
10090
10091 /* For C++ methods, append formal parameter type
10092 information, if PHYSNAME. */
10093
10094 if (physname && die->tag == DW_TAG_subprogram
10095 && cu->language == language_cplus)
10096 {
10097 struct type *type = read_type_die (die, cu);
10098
10099 c_type_print_args (type, &buf, 1, cu->language,
10100 &type_print_raw_options);
10101
10102 if (cu->language == language_cplus)
10103 {
10104 /* Assume that an artificial first parameter is
10105 "this", but do not crash if it is not. RealView
10106 marks unnamed (and thus unused) parameters as
10107 artificial; there is no way to differentiate
10108 the two cases. */
10109 if (TYPE_NFIELDS (type) > 0
10110 && TYPE_FIELD_ARTIFICIAL (type, 0)
10111 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10112 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10113 0))))
10114 buf.puts (" const");
10115 }
10116 }
10117
10118 const std::string &intermediate_name = buf.string ();
10119
10120 if (cu->language == language_cplus)
10121 canonical_name
10122 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10123 objfile);
10124
10125 /* If we only computed INTERMEDIATE_NAME, or if
10126 INTERMEDIATE_NAME is already canonical, then we need to
10127 intern it. */
10128 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10129 name = objfile->intern (intermediate_name);
10130 else
10131 name = canonical_name;
10132 }
10133 }
10134
10135 return name;
10136 }
10137
10138 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10139 If scope qualifiers are appropriate they will be added. The result
10140 will be allocated on the storage_obstack, or NULL if the DIE does
10141 not have a name. NAME may either be from a previous call to
10142 dwarf2_name or NULL.
10143
10144 The output string will be canonicalized (if C++). */
10145
10146 static const char *
10147 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10148 {
10149 return dwarf2_compute_name (name, die, cu, 0);
10150 }
10151
10152 /* Construct a physname for the given DIE in CU. NAME may either be
10153 from a previous call to dwarf2_name or NULL. The result will be
10154 allocated on the objfile_objstack or NULL if the DIE does not have a
10155 name.
10156
10157 The output string will be canonicalized (if C++). */
10158
10159 static const char *
10160 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10161 {
10162 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10163 const char *retval, *mangled = NULL, *canon = NULL;
10164 int need_copy = 1;
10165
10166 /* In this case dwarf2_compute_name is just a shortcut not building anything
10167 on its own. */
10168 if (!die_needs_namespace (die, cu))
10169 return dwarf2_compute_name (name, die, cu, 1);
10170
10171 mangled = dw2_linkage_name (die, cu);
10172
10173 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10174 See https://github.com/rust-lang/rust/issues/32925. */
10175 if (cu->language == language_rust && mangled != NULL
10176 && strchr (mangled, '{') != NULL)
10177 mangled = NULL;
10178
10179 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10180 has computed. */
10181 gdb::unique_xmalloc_ptr<char> demangled;
10182 if (mangled != NULL)
10183 {
10184
10185 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10186 {
10187 /* Do nothing (do not demangle the symbol name). */
10188 }
10189 else if (cu->language == language_go)
10190 {
10191 /* This is a lie, but we already lie to the caller new_symbol.
10192 new_symbol assumes we return the mangled name.
10193 This just undoes that lie until things are cleaned up. */
10194 }
10195 else
10196 {
10197 /* Use DMGL_RET_DROP for C++ template functions to suppress
10198 their return type. It is easier for GDB users to search
10199 for such functions as `name(params)' than `long name(params)'.
10200 In such case the minimal symbol names do not match the full
10201 symbol names but for template functions there is never a need
10202 to look up their definition from their declaration so
10203 the only disadvantage remains the minimal symbol variant
10204 `long name(params)' does not have the proper inferior type. */
10205 demangled.reset (gdb_demangle (mangled,
10206 (DMGL_PARAMS | DMGL_ANSI
10207 | DMGL_RET_DROP)));
10208 }
10209 if (demangled)
10210 canon = demangled.get ();
10211 else
10212 {
10213 canon = mangled;
10214 need_copy = 0;
10215 }
10216 }
10217
10218 if (canon == NULL || check_physname)
10219 {
10220 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10221
10222 if (canon != NULL && strcmp (physname, canon) != 0)
10223 {
10224 /* It may not mean a bug in GDB. The compiler could also
10225 compute DW_AT_linkage_name incorrectly. But in such case
10226 GDB would need to be bug-to-bug compatible. */
10227
10228 complaint (_("Computed physname <%s> does not match demangled <%s> "
10229 "(from linkage <%s>) - DIE at %s [in module %s]"),
10230 physname, canon, mangled, sect_offset_str (die->sect_off),
10231 objfile_name (objfile));
10232
10233 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10234 is available here - over computed PHYSNAME. It is safer
10235 against both buggy GDB and buggy compilers. */
10236
10237 retval = canon;
10238 }
10239 else
10240 {
10241 retval = physname;
10242 need_copy = 0;
10243 }
10244 }
10245 else
10246 retval = canon;
10247
10248 if (need_copy)
10249 retval = objfile->intern (retval);
10250
10251 return retval;
10252 }
10253
10254 /* Inspect DIE in CU for a namespace alias. If one exists, record
10255 a new symbol for it.
10256
10257 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10258
10259 static int
10260 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10261 {
10262 struct attribute *attr;
10263
10264 /* If the die does not have a name, this is not a namespace
10265 alias. */
10266 attr = dwarf2_attr (die, DW_AT_name, cu);
10267 if (attr != NULL)
10268 {
10269 int num;
10270 struct die_info *d = die;
10271 struct dwarf2_cu *imported_cu = cu;
10272
10273 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10274 keep inspecting DIEs until we hit the underlying import. */
10275 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10276 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10277 {
10278 attr = dwarf2_attr (d, DW_AT_import, cu);
10279 if (attr == NULL)
10280 break;
10281
10282 d = follow_die_ref (d, attr, &imported_cu);
10283 if (d->tag != DW_TAG_imported_declaration)
10284 break;
10285 }
10286
10287 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10288 {
10289 complaint (_("DIE at %s has too many recursively imported "
10290 "declarations"), sect_offset_str (d->sect_off));
10291 return 0;
10292 }
10293
10294 if (attr != NULL)
10295 {
10296 struct type *type;
10297 sect_offset sect_off = attr->get_ref_die_offset ();
10298
10299 type = get_die_type_at_offset (sect_off, cu->per_cu);
10300 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10301 {
10302 /* This declaration is a global namespace alias. Add
10303 a symbol for it whose type is the aliased namespace. */
10304 new_symbol (die, type, cu);
10305 return 1;
10306 }
10307 }
10308 }
10309
10310 return 0;
10311 }
10312
10313 /* Return the using directives repository (global or local?) to use in the
10314 current context for CU.
10315
10316 For Ada, imported declarations can materialize renamings, which *may* be
10317 global. However it is impossible (for now?) in DWARF to distinguish
10318 "external" imported declarations and "static" ones. As all imported
10319 declarations seem to be static in all other languages, make them all CU-wide
10320 global only in Ada. */
10321
10322 static struct using_direct **
10323 using_directives (struct dwarf2_cu *cu)
10324 {
10325 if (cu->language == language_ada
10326 && cu->get_builder ()->outermost_context_p ())
10327 return cu->get_builder ()->get_global_using_directives ();
10328 else
10329 return cu->get_builder ()->get_local_using_directives ();
10330 }
10331
10332 /* Read the import statement specified by the given die and record it. */
10333
10334 static void
10335 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10336 {
10337 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10338 struct attribute *import_attr;
10339 struct die_info *imported_die, *child_die;
10340 struct dwarf2_cu *imported_cu;
10341 const char *imported_name;
10342 const char *imported_name_prefix;
10343 const char *canonical_name;
10344 const char *import_alias;
10345 const char *imported_declaration = NULL;
10346 const char *import_prefix;
10347 std::vector<const char *> excludes;
10348
10349 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10350 if (import_attr == NULL)
10351 {
10352 complaint (_("Tag '%s' has no DW_AT_import"),
10353 dwarf_tag_name (die->tag));
10354 return;
10355 }
10356
10357 imported_cu = cu;
10358 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10359 imported_name = dwarf2_name (imported_die, imported_cu);
10360 if (imported_name == NULL)
10361 {
10362 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10363
10364 The import in the following code:
10365 namespace A
10366 {
10367 typedef int B;
10368 }
10369
10370 int main ()
10371 {
10372 using A::B;
10373 B b;
10374 return b;
10375 }
10376
10377 ...
10378 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10379 <52> DW_AT_decl_file : 1
10380 <53> DW_AT_decl_line : 6
10381 <54> DW_AT_import : <0x75>
10382 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10383 <59> DW_AT_name : B
10384 <5b> DW_AT_decl_file : 1
10385 <5c> DW_AT_decl_line : 2
10386 <5d> DW_AT_type : <0x6e>
10387 ...
10388 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10389 <76> DW_AT_byte_size : 4
10390 <77> DW_AT_encoding : 5 (signed)
10391
10392 imports the wrong die ( 0x75 instead of 0x58 ).
10393 This case will be ignored until the gcc bug is fixed. */
10394 return;
10395 }
10396
10397 /* Figure out the local name after import. */
10398 import_alias = dwarf2_name (die, cu);
10399
10400 /* Figure out where the statement is being imported to. */
10401 import_prefix = determine_prefix (die, cu);
10402
10403 /* Figure out what the scope of the imported die is and prepend it
10404 to the name of the imported die. */
10405 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10406
10407 if (imported_die->tag != DW_TAG_namespace
10408 && imported_die->tag != DW_TAG_module)
10409 {
10410 imported_declaration = imported_name;
10411 canonical_name = imported_name_prefix;
10412 }
10413 else if (strlen (imported_name_prefix) > 0)
10414 canonical_name = obconcat (&objfile->objfile_obstack,
10415 imported_name_prefix,
10416 (cu->language == language_d ? "." : "::"),
10417 imported_name, (char *) NULL);
10418 else
10419 canonical_name = imported_name;
10420
10421 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10422 for (child_die = die->child; child_die && child_die->tag;
10423 child_die = child_die->sibling)
10424 {
10425 /* DWARF-4: A Fortran use statement with a “rename list” may be
10426 represented by an imported module entry with an import attribute
10427 referring to the module and owned entries corresponding to those
10428 entities that are renamed as part of being imported. */
10429
10430 if (child_die->tag != DW_TAG_imported_declaration)
10431 {
10432 complaint (_("child DW_TAG_imported_declaration expected "
10433 "- DIE at %s [in module %s]"),
10434 sect_offset_str (child_die->sect_off),
10435 objfile_name (objfile));
10436 continue;
10437 }
10438
10439 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10440 if (import_attr == NULL)
10441 {
10442 complaint (_("Tag '%s' has no DW_AT_import"),
10443 dwarf_tag_name (child_die->tag));
10444 continue;
10445 }
10446
10447 imported_cu = cu;
10448 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10449 &imported_cu);
10450 imported_name = dwarf2_name (imported_die, imported_cu);
10451 if (imported_name == NULL)
10452 {
10453 complaint (_("child DW_TAG_imported_declaration has unknown "
10454 "imported name - DIE at %s [in module %s]"),
10455 sect_offset_str (child_die->sect_off),
10456 objfile_name (objfile));
10457 continue;
10458 }
10459
10460 excludes.push_back (imported_name);
10461
10462 process_die (child_die, cu);
10463 }
10464
10465 add_using_directive (using_directives (cu),
10466 import_prefix,
10467 canonical_name,
10468 import_alias,
10469 imported_declaration,
10470 excludes,
10471 0,
10472 &objfile->objfile_obstack);
10473 }
10474
10475 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10476 types, but gives them a size of zero. Starting with version 14,
10477 ICC is compatible with GCC. */
10478
10479 static bool
10480 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10481 {
10482 if (!cu->checked_producer)
10483 check_producer (cu);
10484
10485 return cu->producer_is_icc_lt_14;
10486 }
10487
10488 /* ICC generates a DW_AT_type for C void functions. This was observed on
10489 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10490 which says that void functions should not have a DW_AT_type. */
10491
10492 static bool
10493 producer_is_icc (struct dwarf2_cu *cu)
10494 {
10495 if (!cu->checked_producer)
10496 check_producer (cu);
10497
10498 return cu->producer_is_icc;
10499 }
10500
10501 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10502 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10503 this, it was first present in GCC release 4.3.0. */
10504
10505 static bool
10506 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10507 {
10508 if (!cu->checked_producer)
10509 check_producer (cu);
10510
10511 return cu->producer_is_gcc_lt_4_3;
10512 }
10513
10514 static file_and_directory
10515 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10516 {
10517 file_and_directory res;
10518
10519 /* Find the filename. Do not use dwarf2_name here, since the filename
10520 is not a source language identifier. */
10521 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10522 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10523
10524 if (res.comp_dir == NULL
10525 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10526 && IS_ABSOLUTE_PATH (res.name))
10527 {
10528 res.comp_dir_storage = ldirname (res.name);
10529 if (!res.comp_dir_storage.empty ())
10530 res.comp_dir = res.comp_dir_storage.c_str ();
10531 }
10532 if (res.comp_dir != NULL)
10533 {
10534 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10535 directory, get rid of it. */
10536 const char *cp = strchr (res.comp_dir, ':');
10537
10538 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10539 res.comp_dir = cp + 1;
10540 }
10541
10542 if (res.name == NULL)
10543 res.name = "<unknown>";
10544
10545 return res;
10546 }
10547
10548 /* Handle DW_AT_stmt_list for a compilation unit.
10549 DIE is the DW_TAG_compile_unit die for CU.
10550 COMP_DIR is the compilation directory. LOWPC is passed to
10551 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10552
10553 static void
10554 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10555 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10556 {
10557 struct dwarf2_per_objfile *dwarf2_per_objfile
10558 = cu->per_cu->dwarf2_per_objfile;
10559 struct attribute *attr;
10560 struct line_header line_header_local;
10561 hashval_t line_header_local_hash;
10562 void **slot;
10563 int decode_mapping;
10564
10565 gdb_assert (! cu->per_cu->is_debug_types);
10566
10567 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10568 if (attr == NULL)
10569 return;
10570
10571 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10572
10573 /* The line header hash table is only created if needed (it exists to
10574 prevent redundant reading of the line table for partial_units).
10575 If we're given a partial_unit, we'll need it. If we're given a
10576 compile_unit, then use the line header hash table if it's already
10577 created, but don't create one just yet. */
10578
10579 if (dwarf2_per_objfile->line_header_hash == NULL
10580 && die->tag == DW_TAG_partial_unit)
10581 {
10582 dwarf2_per_objfile->line_header_hash
10583 .reset (htab_create_alloc (127, line_header_hash_voidp,
10584 line_header_eq_voidp,
10585 free_line_header_voidp,
10586 xcalloc, xfree));
10587 }
10588
10589 line_header_local.sect_off = line_offset;
10590 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10591 line_header_local_hash = line_header_hash (&line_header_local);
10592 if (dwarf2_per_objfile->line_header_hash != NULL)
10593 {
10594 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10595 &line_header_local,
10596 line_header_local_hash, NO_INSERT);
10597
10598 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10599 is not present in *SLOT (since if there is something in *SLOT then
10600 it will be for a partial_unit). */
10601 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10602 {
10603 gdb_assert (*slot != NULL);
10604 cu->line_header = (struct line_header *) *slot;
10605 return;
10606 }
10607 }
10608
10609 /* dwarf_decode_line_header does not yet provide sufficient information.
10610 We always have to call also dwarf_decode_lines for it. */
10611 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10612 if (lh == NULL)
10613 return;
10614
10615 cu->line_header = lh.release ();
10616 cu->line_header_die_owner = die;
10617
10618 if (dwarf2_per_objfile->line_header_hash == NULL)
10619 slot = NULL;
10620 else
10621 {
10622 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10623 &line_header_local,
10624 line_header_local_hash, INSERT);
10625 gdb_assert (slot != NULL);
10626 }
10627 if (slot != NULL && *slot == NULL)
10628 {
10629 /* This newly decoded line number information unit will be owned
10630 by line_header_hash hash table. */
10631 *slot = cu->line_header;
10632 cu->line_header_die_owner = NULL;
10633 }
10634 else
10635 {
10636 /* We cannot free any current entry in (*slot) as that struct line_header
10637 may be already used by multiple CUs. Create only temporary decoded
10638 line_header for this CU - it may happen at most once for each line
10639 number information unit. And if we're not using line_header_hash
10640 then this is what we want as well. */
10641 gdb_assert (die->tag != DW_TAG_partial_unit);
10642 }
10643 decode_mapping = (die->tag != DW_TAG_partial_unit);
10644 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10645 decode_mapping);
10646
10647 }
10648
10649 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10650
10651 static void
10652 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10653 {
10654 struct dwarf2_per_objfile *dwarf2_per_objfile
10655 = cu->per_cu->dwarf2_per_objfile;
10656 struct objfile *objfile = dwarf2_per_objfile->objfile;
10657 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10658 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10659 CORE_ADDR highpc = ((CORE_ADDR) 0);
10660 struct attribute *attr;
10661 struct die_info *child_die;
10662 CORE_ADDR baseaddr;
10663
10664 prepare_one_comp_unit (cu, die, cu->language);
10665 baseaddr = objfile->text_section_offset ();
10666
10667 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10668
10669 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10670 from finish_block. */
10671 if (lowpc == ((CORE_ADDR) -1))
10672 lowpc = highpc;
10673 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10674
10675 file_and_directory fnd = find_file_and_directory (die, cu);
10676
10677 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10678 standardised yet. As a workaround for the language detection we fall
10679 back to the DW_AT_producer string. */
10680 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10681 cu->language = language_opencl;
10682
10683 /* Similar hack for Go. */
10684 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10685 set_cu_language (DW_LANG_Go, cu);
10686
10687 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10688
10689 /* Decode line number information if present. We do this before
10690 processing child DIEs, so that the line header table is available
10691 for DW_AT_decl_file. */
10692 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10693
10694 /* Process all dies in compilation unit. */
10695 if (die->child != NULL)
10696 {
10697 child_die = die->child;
10698 while (child_die && child_die->tag)
10699 {
10700 process_die (child_die, cu);
10701 child_die = child_die->sibling;
10702 }
10703 }
10704
10705 /* Decode macro information, if present. Dwarf 2 macro information
10706 refers to information in the line number info statement program
10707 header, so we can only read it if we've read the header
10708 successfully. */
10709 attr = dwarf2_attr (die, DW_AT_macros, cu);
10710 if (attr == NULL)
10711 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10712 if (attr && cu->line_header)
10713 {
10714 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10715 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10716
10717 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10718 }
10719 else
10720 {
10721 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10722 if (attr && cu->line_header)
10723 {
10724 unsigned int macro_offset = DW_UNSND (attr);
10725
10726 dwarf_decode_macros (cu, macro_offset, 0);
10727 }
10728 }
10729 }
10730
10731 void
10732 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10733 {
10734 struct type_unit_group *tu_group;
10735 int first_time;
10736 struct attribute *attr;
10737 unsigned int i;
10738 struct signatured_type *sig_type;
10739
10740 gdb_assert (per_cu->is_debug_types);
10741 sig_type = (struct signatured_type *) per_cu;
10742
10743 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10744
10745 /* If we're using .gdb_index (includes -readnow) then
10746 per_cu->type_unit_group may not have been set up yet. */
10747 if (sig_type->type_unit_group == NULL)
10748 sig_type->type_unit_group = get_type_unit_group (this, attr);
10749 tu_group = sig_type->type_unit_group;
10750
10751 /* If we've already processed this stmt_list there's no real need to
10752 do it again, we could fake it and just recreate the part we need
10753 (file name,index -> symtab mapping). If data shows this optimization
10754 is useful we can do it then. */
10755 first_time = tu_group->compunit_symtab == NULL;
10756
10757 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10758 debug info. */
10759 line_header_up lh;
10760 if (attr != NULL)
10761 {
10762 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10763 lh = dwarf_decode_line_header (line_offset, this);
10764 }
10765 if (lh == NULL)
10766 {
10767 if (first_time)
10768 start_symtab ("", NULL, 0);
10769 else
10770 {
10771 gdb_assert (tu_group->symtabs == NULL);
10772 gdb_assert (m_builder == nullptr);
10773 struct compunit_symtab *cust = tu_group->compunit_symtab;
10774 m_builder.reset (new struct buildsym_compunit
10775 (COMPUNIT_OBJFILE (cust), "",
10776 COMPUNIT_DIRNAME (cust),
10777 compunit_language (cust),
10778 0, cust));
10779 }
10780 return;
10781 }
10782
10783 line_header = lh.release ();
10784 line_header_die_owner = die;
10785
10786 if (first_time)
10787 {
10788 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10789
10790 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10791 still initializing it, and our caller (a few levels up)
10792 process_full_type_unit still needs to know if this is the first
10793 time. */
10794
10795 tu_group->symtabs
10796 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10797 struct symtab *, line_header->file_names_size ());
10798
10799 auto &file_names = line_header->file_names ();
10800 for (i = 0; i < file_names.size (); ++i)
10801 {
10802 file_entry &fe = file_names[i];
10803 dwarf2_start_subfile (this, fe.name,
10804 fe.include_dir (line_header));
10805 buildsym_compunit *b = get_builder ();
10806 if (b->get_current_subfile ()->symtab == NULL)
10807 {
10808 /* NOTE: start_subfile will recognize when it's been
10809 passed a file it has already seen. So we can't
10810 assume there's a simple mapping from
10811 cu->line_header->file_names to subfiles, plus
10812 cu->line_header->file_names may contain dups. */
10813 b->get_current_subfile ()->symtab
10814 = allocate_symtab (cust, b->get_current_subfile ()->name);
10815 }
10816
10817 fe.symtab = b->get_current_subfile ()->symtab;
10818 tu_group->symtabs[i] = fe.symtab;
10819 }
10820 }
10821 else
10822 {
10823 gdb_assert (m_builder == nullptr);
10824 struct compunit_symtab *cust = tu_group->compunit_symtab;
10825 m_builder.reset (new struct buildsym_compunit
10826 (COMPUNIT_OBJFILE (cust), "",
10827 COMPUNIT_DIRNAME (cust),
10828 compunit_language (cust),
10829 0, cust));
10830
10831 auto &file_names = line_header->file_names ();
10832 for (i = 0; i < file_names.size (); ++i)
10833 {
10834 file_entry &fe = file_names[i];
10835 fe.symtab = tu_group->symtabs[i];
10836 }
10837 }
10838
10839 /* The main symtab is allocated last. Type units don't have DW_AT_name
10840 so they don't have a "real" (so to speak) symtab anyway.
10841 There is later code that will assign the main symtab to all symbols
10842 that don't have one. We need to handle the case of a symbol with a
10843 missing symtab (DW_AT_decl_file) anyway. */
10844 }
10845
10846 /* Process DW_TAG_type_unit.
10847 For TUs we want to skip the first top level sibling if it's not the
10848 actual type being defined by this TU. In this case the first top
10849 level sibling is there to provide context only. */
10850
10851 static void
10852 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10853 {
10854 struct die_info *child_die;
10855
10856 prepare_one_comp_unit (cu, die, language_minimal);
10857
10858 /* Initialize (or reinitialize) the machinery for building symtabs.
10859 We do this before processing child DIEs, so that the line header table
10860 is available for DW_AT_decl_file. */
10861 cu->setup_type_unit_groups (die);
10862
10863 if (die->child != NULL)
10864 {
10865 child_die = die->child;
10866 while (child_die && child_die->tag)
10867 {
10868 process_die (child_die, cu);
10869 child_die = child_die->sibling;
10870 }
10871 }
10872 }
10873 \f
10874 /* DWO/DWP files.
10875
10876 http://gcc.gnu.org/wiki/DebugFission
10877 http://gcc.gnu.org/wiki/DebugFissionDWP
10878
10879 To simplify handling of both DWO files ("object" files with the DWARF info)
10880 and DWP files (a file with the DWOs packaged up into one file), we treat
10881 DWP files as having a collection of virtual DWO files. */
10882
10883 static hashval_t
10884 hash_dwo_file (const void *item)
10885 {
10886 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10887 hashval_t hash;
10888
10889 hash = htab_hash_string (dwo_file->dwo_name);
10890 if (dwo_file->comp_dir != NULL)
10891 hash += htab_hash_string (dwo_file->comp_dir);
10892 return hash;
10893 }
10894
10895 static int
10896 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10897 {
10898 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10899 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10900
10901 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10902 return 0;
10903 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10904 return lhs->comp_dir == rhs->comp_dir;
10905 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10906 }
10907
10908 /* Allocate a hash table for DWO files. */
10909
10910 static htab_up
10911 allocate_dwo_file_hash_table ()
10912 {
10913 auto delete_dwo_file = [] (void *item)
10914 {
10915 struct dwo_file *dwo_file = (struct dwo_file *) item;
10916
10917 delete dwo_file;
10918 };
10919
10920 return htab_up (htab_create_alloc (41,
10921 hash_dwo_file,
10922 eq_dwo_file,
10923 delete_dwo_file,
10924 xcalloc, xfree));
10925 }
10926
10927 /* Lookup DWO file DWO_NAME. */
10928
10929 static void **
10930 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10931 const char *dwo_name,
10932 const char *comp_dir)
10933 {
10934 struct dwo_file find_entry;
10935 void **slot;
10936
10937 if (dwarf2_per_objfile->dwo_files == NULL)
10938 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10939
10940 find_entry.dwo_name = dwo_name;
10941 find_entry.comp_dir = comp_dir;
10942 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
10943 INSERT);
10944
10945 return slot;
10946 }
10947
10948 static hashval_t
10949 hash_dwo_unit (const void *item)
10950 {
10951 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10952
10953 /* This drops the top 32 bits of the id, but is ok for a hash. */
10954 return dwo_unit->signature;
10955 }
10956
10957 static int
10958 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10959 {
10960 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10961 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10962
10963 /* The signature is assumed to be unique within the DWO file.
10964 So while object file CU dwo_id's always have the value zero,
10965 that's OK, assuming each object file DWO file has only one CU,
10966 and that's the rule for now. */
10967 return lhs->signature == rhs->signature;
10968 }
10969
10970 /* Allocate a hash table for DWO CUs,TUs.
10971 There is one of these tables for each of CUs,TUs for each DWO file. */
10972
10973 static htab_up
10974 allocate_dwo_unit_table ()
10975 {
10976 /* Start out with a pretty small number.
10977 Generally DWO files contain only one CU and maybe some TUs. */
10978 return htab_up (htab_create_alloc (3,
10979 hash_dwo_unit,
10980 eq_dwo_unit,
10981 NULL, xcalloc, xfree));
10982 }
10983
10984 /* die_reader_func for create_dwo_cu. */
10985
10986 static void
10987 create_dwo_cu_reader (const struct die_reader_specs *reader,
10988 const gdb_byte *info_ptr,
10989 struct die_info *comp_unit_die,
10990 struct dwo_file *dwo_file,
10991 struct dwo_unit *dwo_unit)
10992 {
10993 struct dwarf2_cu *cu = reader->cu;
10994 sect_offset sect_off = cu->per_cu->sect_off;
10995 struct dwarf2_section_info *section = cu->per_cu->section;
10996
10997 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
10998 if (!signature.has_value ())
10999 {
11000 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11001 " its dwo_id [in module %s]"),
11002 sect_offset_str (sect_off), dwo_file->dwo_name);
11003 return;
11004 }
11005
11006 dwo_unit->dwo_file = dwo_file;
11007 dwo_unit->signature = *signature;
11008 dwo_unit->section = section;
11009 dwo_unit->sect_off = sect_off;
11010 dwo_unit->length = cu->per_cu->length;
11011
11012 if (dwarf_read_debug)
11013 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11014 sect_offset_str (sect_off),
11015 hex_string (dwo_unit->signature));
11016 }
11017
11018 /* Create the dwo_units for the CUs in a DWO_FILE.
11019 Note: This function processes DWO files only, not DWP files. */
11020
11021 static void
11022 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11023 dwarf2_cu *cu, struct dwo_file &dwo_file,
11024 dwarf2_section_info &section, htab_up &cus_htab)
11025 {
11026 struct objfile *objfile = dwarf2_per_objfile->objfile;
11027 const gdb_byte *info_ptr, *end_ptr;
11028
11029 section.read (objfile);
11030 info_ptr = section.buffer;
11031
11032 if (info_ptr == NULL)
11033 return;
11034
11035 if (dwarf_read_debug)
11036 {
11037 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11038 section.get_name (),
11039 section.get_file_name ());
11040 }
11041
11042 end_ptr = info_ptr + section.size;
11043 while (info_ptr < end_ptr)
11044 {
11045 struct dwarf2_per_cu_data per_cu;
11046 struct dwo_unit read_unit {};
11047 struct dwo_unit *dwo_unit;
11048 void **slot;
11049 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11050
11051 memset (&per_cu, 0, sizeof (per_cu));
11052 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11053 per_cu.is_debug_types = 0;
11054 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11055 per_cu.section = &section;
11056
11057 cutu_reader reader (&per_cu, cu, &dwo_file);
11058 if (!reader.dummy_p)
11059 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11060 &dwo_file, &read_unit);
11061 info_ptr += per_cu.length;
11062
11063 // If the unit could not be parsed, skip it.
11064 if (read_unit.dwo_file == NULL)
11065 continue;
11066
11067 if (cus_htab == NULL)
11068 cus_htab = allocate_dwo_unit_table ();
11069
11070 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11071 *dwo_unit = read_unit;
11072 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11073 gdb_assert (slot != NULL);
11074 if (*slot != NULL)
11075 {
11076 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11077 sect_offset dup_sect_off = dup_cu->sect_off;
11078
11079 complaint (_("debug cu entry at offset %s is duplicate to"
11080 " the entry at offset %s, signature %s"),
11081 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11082 hex_string (dwo_unit->signature));
11083 }
11084 *slot = (void *)dwo_unit;
11085 }
11086 }
11087
11088 /* DWP file .debug_{cu,tu}_index section format:
11089 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11090
11091 DWP Version 1:
11092
11093 Both index sections have the same format, and serve to map a 64-bit
11094 signature to a set of section numbers. Each section begins with a header,
11095 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11096 indexes, and a pool of 32-bit section numbers. The index sections will be
11097 aligned at 8-byte boundaries in the file.
11098
11099 The index section header consists of:
11100
11101 V, 32 bit version number
11102 -, 32 bits unused
11103 N, 32 bit number of compilation units or type units in the index
11104 M, 32 bit number of slots in the hash table
11105
11106 Numbers are recorded using the byte order of the application binary.
11107
11108 The hash table begins at offset 16 in the section, and consists of an array
11109 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11110 order of the application binary). Unused slots in the hash table are 0.
11111 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11112
11113 The parallel table begins immediately after the hash table
11114 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11115 array of 32-bit indexes (using the byte order of the application binary),
11116 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11117 table contains a 32-bit index into the pool of section numbers. For unused
11118 hash table slots, the corresponding entry in the parallel table will be 0.
11119
11120 The pool of section numbers begins immediately following the hash table
11121 (at offset 16 + 12 * M from the beginning of the section). The pool of
11122 section numbers consists of an array of 32-bit words (using the byte order
11123 of the application binary). Each item in the array is indexed starting
11124 from 0. The hash table entry provides the index of the first section
11125 number in the set. Additional section numbers in the set follow, and the
11126 set is terminated by a 0 entry (section number 0 is not used in ELF).
11127
11128 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11129 section must be the first entry in the set, and the .debug_abbrev.dwo must
11130 be the second entry. Other members of the set may follow in any order.
11131
11132 ---
11133
11134 DWP Version 2:
11135
11136 DWP Version 2 combines all the .debug_info, etc. sections into one,
11137 and the entries in the index tables are now offsets into these sections.
11138 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11139 section.
11140
11141 Index Section Contents:
11142 Header
11143 Hash Table of Signatures dwp_hash_table.hash_table
11144 Parallel Table of Indices dwp_hash_table.unit_table
11145 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11146 Table of Section Sizes dwp_hash_table.v2.sizes
11147
11148 The index section header consists of:
11149
11150 V, 32 bit version number
11151 L, 32 bit number of columns in the table of section offsets
11152 N, 32 bit number of compilation units or type units in the index
11153 M, 32 bit number of slots in the hash table
11154
11155 Numbers are recorded using the byte order of the application binary.
11156
11157 The hash table has the same format as version 1.
11158 The parallel table of indices has the same format as version 1,
11159 except that the entries are origin-1 indices into the table of sections
11160 offsets and the table of section sizes.
11161
11162 The table of offsets begins immediately following the parallel table
11163 (at offset 16 + 12 * M from the beginning of the section). The table is
11164 a two-dimensional array of 32-bit words (using the byte order of the
11165 application binary), with L columns and N+1 rows, in row-major order.
11166 Each row in the array is indexed starting from 0. The first row provides
11167 a key to the remaining rows: each column in this row provides an identifier
11168 for a debug section, and the offsets in the same column of subsequent rows
11169 refer to that section. The section identifiers are:
11170
11171 DW_SECT_INFO 1 .debug_info.dwo
11172 DW_SECT_TYPES 2 .debug_types.dwo
11173 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11174 DW_SECT_LINE 4 .debug_line.dwo
11175 DW_SECT_LOC 5 .debug_loc.dwo
11176 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11177 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11178 DW_SECT_MACRO 8 .debug_macro.dwo
11179
11180 The offsets provided by the CU and TU index sections are the base offsets
11181 for the contributions made by each CU or TU to the corresponding section
11182 in the package file. Each CU and TU header contains an abbrev_offset
11183 field, used to find the abbreviations table for that CU or TU within the
11184 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11185 be interpreted as relative to the base offset given in the index section.
11186 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11187 should be interpreted as relative to the base offset for .debug_line.dwo,
11188 and offsets into other debug sections obtained from DWARF attributes should
11189 also be interpreted as relative to the corresponding base offset.
11190
11191 The table of sizes begins immediately following the table of offsets.
11192 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11193 with L columns and N rows, in row-major order. Each row in the array is
11194 indexed starting from 1 (row 0 is shared by the two tables).
11195
11196 ---
11197
11198 Hash table lookup is handled the same in version 1 and 2:
11199
11200 We assume that N and M will not exceed 2^32 - 1.
11201 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11202
11203 Given a 64-bit compilation unit signature or a type signature S, an entry
11204 in the hash table is located as follows:
11205
11206 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11207 the low-order k bits all set to 1.
11208
11209 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11210
11211 3) If the hash table entry at index H matches the signature, use that
11212 entry. If the hash table entry at index H is unused (all zeroes),
11213 terminate the search: the signature is not present in the table.
11214
11215 4) Let H = (H + H') modulo M. Repeat at Step 3.
11216
11217 Because M > N and H' and M are relatively prime, the search is guaranteed
11218 to stop at an unused slot or find the match. */
11219
11220 /* Create a hash table to map DWO IDs to their CU/TU entry in
11221 .debug_{info,types}.dwo in DWP_FILE.
11222 Returns NULL if there isn't one.
11223 Note: This function processes DWP files only, not DWO files. */
11224
11225 static struct dwp_hash_table *
11226 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11227 struct dwp_file *dwp_file, int is_debug_types)
11228 {
11229 struct objfile *objfile = dwarf2_per_objfile->objfile;
11230 bfd *dbfd = dwp_file->dbfd.get ();
11231 const gdb_byte *index_ptr, *index_end;
11232 struct dwarf2_section_info *index;
11233 uint32_t version, nr_columns, nr_units, nr_slots;
11234 struct dwp_hash_table *htab;
11235
11236 if (is_debug_types)
11237 index = &dwp_file->sections.tu_index;
11238 else
11239 index = &dwp_file->sections.cu_index;
11240
11241 if (index->empty ())
11242 return NULL;
11243 index->read (objfile);
11244
11245 index_ptr = index->buffer;
11246 index_end = index_ptr + index->size;
11247
11248 version = read_4_bytes (dbfd, index_ptr);
11249 index_ptr += 4;
11250 if (version == 2)
11251 nr_columns = read_4_bytes (dbfd, index_ptr);
11252 else
11253 nr_columns = 0;
11254 index_ptr += 4;
11255 nr_units = read_4_bytes (dbfd, index_ptr);
11256 index_ptr += 4;
11257 nr_slots = read_4_bytes (dbfd, index_ptr);
11258 index_ptr += 4;
11259
11260 if (version != 1 && version != 2)
11261 {
11262 error (_("Dwarf Error: unsupported DWP file version (%s)"
11263 " [in module %s]"),
11264 pulongest (version), dwp_file->name);
11265 }
11266 if (nr_slots != (nr_slots & -nr_slots))
11267 {
11268 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11269 " is not power of 2 [in module %s]"),
11270 pulongest (nr_slots), dwp_file->name);
11271 }
11272
11273 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11274 htab->version = version;
11275 htab->nr_columns = nr_columns;
11276 htab->nr_units = nr_units;
11277 htab->nr_slots = nr_slots;
11278 htab->hash_table = index_ptr;
11279 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11280
11281 /* Exit early if the table is empty. */
11282 if (nr_slots == 0 || nr_units == 0
11283 || (version == 2 && nr_columns == 0))
11284 {
11285 /* All must be zero. */
11286 if (nr_slots != 0 || nr_units != 0
11287 || (version == 2 && nr_columns != 0))
11288 {
11289 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11290 " all zero [in modules %s]"),
11291 dwp_file->name);
11292 }
11293 return htab;
11294 }
11295
11296 if (version == 1)
11297 {
11298 htab->section_pool.v1.indices =
11299 htab->unit_table + sizeof (uint32_t) * nr_slots;
11300 /* It's harder to decide whether the section is too small in v1.
11301 V1 is deprecated anyway so we punt. */
11302 }
11303 else
11304 {
11305 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11306 int *ids = htab->section_pool.v2.section_ids;
11307 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11308 /* Reverse map for error checking. */
11309 int ids_seen[DW_SECT_MAX + 1];
11310 int i;
11311
11312 if (nr_columns < 2)
11313 {
11314 error (_("Dwarf Error: bad DWP hash table, too few columns"
11315 " in section table [in module %s]"),
11316 dwp_file->name);
11317 }
11318 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11319 {
11320 error (_("Dwarf Error: bad DWP hash table, too many columns"
11321 " in section table [in module %s]"),
11322 dwp_file->name);
11323 }
11324 memset (ids, 255, sizeof_ids);
11325 memset (ids_seen, 255, sizeof (ids_seen));
11326 for (i = 0; i < nr_columns; ++i)
11327 {
11328 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11329
11330 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11331 {
11332 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11333 " in section table [in module %s]"),
11334 id, dwp_file->name);
11335 }
11336 if (ids_seen[id] != -1)
11337 {
11338 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11339 " id %d in section table [in module %s]"),
11340 id, dwp_file->name);
11341 }
11342 ids_seen[id] = i;
11343 ids[i] = id;
11344 }
11345 /* Must have exactly one info or types section. */
11346 if (((ids_seen[DW_SECT_INFO] != -1)
11347 + (ids_seen[DW_SECT_TYPES] != -1))
11348 != 1)
11349 {
11350 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11351 " DWO info/types section [in module %s]"),
11352 dwp_file->name);
11353 }
11354 /* Must have an abbrev section. */
11355 if (ids_seen[DW_SECT_ABBREV] == -1)
11356 {
11357 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11358 " section [in module %s]"),
11359 dwp_file->name);
11360 }
11361 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11362 htab->section_pool.v2.sizes =
11363 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11364 * nr_units * nr_columns);
11365 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11366 * nr_units * nr_columns))
11367 > index_end)
11368 {
11369 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11370 " [in module %s]"),
11371 dwp_file->name);
11372 }
11373 }
11374
11375 return htab;
11376 }
11377
11378 /* Update SECTIONS with the data from SECTP.
11379
11380 This function is like the other "locate" section routines that are
11381 passed to bfd_map_over_sections, but in this context the sections to
11382 read comes from the DWP V1 hash table, not the full ELF section table.
11383
11384 The result is non-zero for success, or zero if an error was found. */
11385
11386 static int
11387 locate_v1_virtual_dwo_sections (asection *sectp,
11388 struct virtual_v1_dwo_sections *sections)
11389 {
11390 const struct dwop_section_names *names = &dwop_section_names;
11391
11392 if (section_is_p (sectp->name, &names->abbrev_dwo))
11393 {
11394 /* There can be only one. */
11395 if (sections->abbrev.s.section != NULL)
11396 return 0;
11397 sections->abbrev.s.section = sectp;
11398 sections->abbrev.size = bfd_section_size (sectp);
11399 }
11400 else if (section_is_p (sectp->name, &names->info_dwo)
11401 || section_is_p (sectp->name, &names->types_dwo))
11402 {
11403 /* There can be only one. */
11404 if (sections->info_or_types.s.section != NULL)
11405 return 0;
11406 sections->info_or_types.s.section = sectp;
11407 sections->info_or_types.size = bfd_section_size (sectp);
11408 }
11409 else if (section_is_p (sectp->name, &names->line_dwo))
11410 {
11411 /* There can be only one. */
11412 if (sections->line.s.section != NULL)
11413 return 0;
11414 sections->line.s.section = sectp;
11415 sections->line.size = bfd_section_size (sectp);
11416 }
11417 else if (section_is_p (sectp->name, &names->loc_dwo))
11418 {
11419 /* There can be only one. */
11420 if (sections->loc.s.section != NULL)
11421 return 0;
11422 sections->loc.s.section = sectp;
11423 sections->loc.size = bfd_section_size (sectp);
11424 }
11425 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11426 {
11427 /* There can be only one. */
11428 if (sections->macinfo.s.section != NULL)
11429 return 0;
11430 sections->macinfo.s.section = sectp;
11431 sections->macinfo.size = bfd_section_size (sectp);
11432 }
11433 else if (section_is_p (sectp->name, &names->macro_dwo))
11434 {
11435 /* There can be only one. */
11436 if (sections->macro.s.section != NULL)
11437 return 0;
11438 sections->macro.s.section = sectp;
11439 sections->macro.size = bfd_section_size (sectp);
11440 }
11441 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11442 {
11443 /* There can be only one. */
11444 if (sections->str_offsets.s.section != NULL)
11445 return 0;
11446 sections->str_offsets.s.section = sectp;
11447 sections->str_offsets.size = bfd_section_size (sectp);
11448 }
11449 else
11450 {
11451 /* No other kind of section is valid. */
11452 return 0;
11453 }
11454
11455 return 1;
11456 }
11457
11458 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11459 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11460 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11461 This is for DWP version 1 files. */
11462
11463 static struct dwo_unit *
11464 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11465 struct dwp_file *dwp_file,
11466 uint32_t unit_index,
11467 const char *comp_dir,
11468 ULONGEST signature, int is_debug_types)
11469 {
11470 struct objfile *objfile = dwarf2_per_objfile->objfile;
11471 const struct dwp_hash_table *dwp_htab =
11472 is_debug_types ? dwp_file->tus : dwp_file->cus;
11473 bfd *dbfd = dwp_file->dbfd.get ();
11474 const char *kind = is_debug_types ? "TU" : "CU";
11475 struct dwo_file *dwo_file;
11476 struct dwo_unit *dwo_unit;
11477 struct virtual_v1_dwo_sections sections;
11478 void **dwo_file_slot;
11479 int i;
11480
11481 gdb_assert (dwp_file->version == 1);
11482
11483 if (dwarf_read_debug)
11484 {
11485 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11486 kind,
11487 pulongest (unit_index), hex_string (signature),
11488 dwp_file->name);
11489 }
11490
11491 /* Fetch the sections of this DWO unit.
11492 Put a limit on the number of sections we look for so that bad data
11493 doesn't cause us to loop forever. */
11494
11495 #define MAX_NR_V1_DWO_SECTIONS \
11496 (1 /* .debug_info or .debug_types */ \
11497 + 1 /* .debug_abbrev */ \
11498 + 1 /* .debug_line */ \
11499 + 1 /* .debug_loc */ \
11500 + 1 /* .debug_str_offsets */ \
11501 + 1 /* .debug_macro or .debug_macinfo */ \
11502 + 1 /* trailing zero */)
11503
11504 memset (&sections, 0, sizeof (sections));
11505
11506 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11507 {
11508 asection *sectp;
11509 uint32_t section_nr =
11510 read_4_bytes (dbfd,
11511 dwp_htab->section_pool.v1.indices
11512 + (unit_index + i) * sizeof (uint32_t));
11513
11514 if (section_nr == 0)
11515 break;
11516 if (section_nr >= dwp_file->num_sections)
11517 {
11518 error (_("Dwarf Error: bad DWP hash table, section number too large"
11519 " [in module %s]"),
11520 dwp_file->name);
11521 }
11522
11523 sectp = dwp_file->elf_sections[section_nr];
11524 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11525 {
11526 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11527 " [in module %s]"),
11528 dwp_file->name);
11529 }
11530 }
11531
11532 if (i < 2
11533 || sections.info_or_types.empty ()
11534 || sections.abbrev.empty ())
11535 {
11536 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11537 " [in module %s]"),
11538 dwp_file->name);
11539 }
11540 if (i == MAX_NR_V1_DWO_SECTIONS)
11541 {
11542 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11543 " [in module %s]"),
11544 dwp_file->name);
11545 }
11546
11547 /* It's easier for the rest of the code if we fake a struct dwo_file and
11548 have dwo_unit "live" in that. At least for now.
11549
11550 The DWP file can be made up of a random collection of CUs and TUs.
11551 However, for each CU + set of TUs that came from the same original DWO
11552 file, we can combine them back into a virtual DWO file to save space
11553 (fewer struct dwo_file objects to allocate). Remember that for really
11554 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11555
11556 std::string virtual_dwo_name =
11557 string_printf ("virtual-dwo/%d-%d-%d-%d",
11558 sections.abbrev.get_id (),
11559 sections.line.get_id (),
11560 sections.loc.get_id (),
11561 sections.str_offsets.get_id ());
11562 /* Can we use an existing virtual DWO file? */
11563 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11564 virtual_dwo_name.c_str (),
11565 comp_dir);
11566 /* Create one if necessary. */
11567 if (*dwo_file_slot == NULL)
11568 {
11569 if (dwarf_read_debug)
11570 {
11571 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11572 virtual_dwo_name.c_str ());
11573 }
11574 dwo_file = new struct dwo_file;
11575 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11576 dwo_file->comp_dir = comp_dir;
11577 dwo_file->sections.abbrev = sections.abbrev;
11578 dwo_file->sections.line = sections.line;
11579 dwo_file->sections.loc = sections.loc;
11580 dwo_file->sections.macinfo = sections.macinfo;
11581 dwo_file->sections.macro = sections.macro;
11582 dwo_file->sections.str_offsets = sections.str_offsets;
11583 /* The "str" section is global to the entire DWP file. */
11584 dwo_file->sections.str = dwp_file->sections.str;
11585 /* The info or types section is assigned below to dwo_unit,
11586 there's no need to record it in dwo_file.
11587 Also, we can't simply record type sections in dwo_file because
11588 we record a pointer into the vector in dwo_unit. As we collect more
11589 types we'll grow the vector and eventually have to reallocate space
11590 for it, invalidating all copies of pointers into the previous
11591 contents. */
11592 *dwo_file_slot = dwo_file;
11593 }
11594 else
11595 {
11596 if (dwarf_read_debug)
11597 {
11598 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11599 virtual_dwo_name.c_str ());
11600 }
11601 dwo_file = (struct dwo_file *) *dwo_file_slot;
11602 }
11603
11604 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11605 dwo_unit->dwo_file = dwo_file;
11606 dwo_unit->signature = signature;
11607 dwo_unit->section =
11608 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11609 *dwo_unit->section = sections.info_or_types;
11610 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11611
11612 return dwo_unit;
11613 }
11614
11615 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11616 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11617 piece within that section used by a TU/CU, return a virtual section
11618 of just that piece. */
11619
11620 static struct dwarf2_section_info
11621 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11622 struct dwarf2_section_info *section,
11623 bfd_size_type offset, bfd_size_type size)
11624 {
11625 struct dwarf2_section_info result;
11626 asection *sectp;
11627
11628 gdb_assert (section != NULL);
11629 gdb_assert (!section->is_virtual);
11630
11631 memset (&result, 0, sizeof (result));
11632 result.s.containing_section = section;
11633 result.is_virtual = true;
11634
11635 if (size == 0)
11636 return result;
11637
11638 sectp = section->get_bfd_section ();
11639
11640 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11641 bounds of the real section. This is a pretty-rare event, so just
11642 flag an error (easier) instead of a warning and trying to cope. */
11643 if (sectp == NULL
11644 || offset + size > bfd_section_size (sectp))
11645 {
11646 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11647 " in section %s [in module %s]"),
11648 sectp ? bfd_section_name (sectp) : "<unknown>",
11649 objfile_name (dwarf2_per_objfile->objfile));
11650 }
11651
11652 result.virtual_offset = offset;
11653 result.size = size;
11654 return result;
11655 }
11656
11657 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11658 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11659 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11660 This is for DWP version 2 files. */
11661
11662 static struct dwo_unit *
11663 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11664 struct dwp_file *dwp_file,
11665 uint32_t unit_index,
11666 const char *comp_dir,
11667 ULONGEST signature, int is_debug_types)
11668 {
11669 struct objfile *objfile = dwarf2_per_objfile->objfile;
11670 const struct dwp_hash_table *dwp_htab =
11671 is_debug_types ? dwp_file->tus : dwp_file->cus;
11672 bfd *dbfd = dwp_file->dbfd.get ();
11673 const char *kind = is_debug_types ? "TU" : "CU";
11674 struct dwo_file *dwo_file;
11675 struct dwo_unit *dwo_unit;
11676 struct virtual_v2_dwo_sections sections;
11677 void **dwo_file_slot;
11678 int i;
11679
11680 gdb_assert (dwp_file->version == 2);
11681
11682 if (dwarf_read_debug)
11683 {
11684 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11685 kind,
11686 pulongest (unit_index), hex_string (signature),
11687 dwp_file->name);
11688 }
11689
11690 /* Fetch the section offsets of this DWO unit. */
11691
11692 memset (&sections, 0, sizeof (sections));
11693
11694 for (i = 0; i < dwp_htab->nr_columns; ++i)
11695 {
11696 uint32_t offset = read_4_bytes (dbfd,
11697 dwp_htab->section_pool.v2.offsets
11698 + (((unit_index - 1) * dwp_htab->nr_columns
11699 + i)
11700 * sizeof (uint32_t)));
11701 uint32_t size = read_4_bytes (dbfd,
11702 dwp_htab->section_pool.v2.sizes
11703 + (((unit_index - 1) * dwp_htab->nr_columns
11704 + i)
11705 * sizeof (uint32_t)));
11706
11707 switch (dwp_htab->section_pool.v2.section_ids[i])
11708 {
11709 case DW_SECT_INFO:
11710 case DW_SECT_TYPES:
11711 sections.info_or_types_offset = offset;
11712 sections.info_or_types_size = size;
11713 break;
11714 case DW_SECT_ABBREV:
11715 sections.abbrev_offset = offset;
11716 sections.abbrev_size = size;
11717 break;
11718 case DW_SECT_LINE:
11719 sections.line_offset = offset;
11720 sections.line_size = size;
11721 break;
11722 case DW_SECT_LOC:
11723 sections.loc_offset = offset;
11724 sections.loc_size = size;
11725 break;
11726 case DW_SECT_STR_OFFSETS:
11727 sections.str_offsets_offset = offset;
11728 sections.str_offsets_size = size;
11729 break;
11730 case DW_SECT_MACINFO:
11731 sections.macinfo_offset = offset;
11732 sections.macinfo_size = size;
11733 break;
11734 case DW_SECT_MACRO:
11735 sections.macro_offset = offset;
11736 sections.macro_size = size;
11737 break;
11738 }
11739 }
11740
11741 /* It's easier for the rest of the code if we fake a struct dwo_file and
11742 have dwo_unit "live" in that. At least for now.
11743
11744 The DWP file can be made up of a random collection of CUs and TUs.
11745 However, for each CU + set of TUs that came from the same original DWO
11746 file, we can combine them back into a virtual DWO file to save space
11747 (fewer struct dwo_file objects to allocate). Remember that for really
11748 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11749
11750 std::string virtual_dwo_name =
11751 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11752 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11753 (long) (sections.line_size ? sections.line_offset : 0),
11754 (long) (sections.loc_size ? sections.loc_offset : 0),
11755 (long) (sections.str_offsets_size
11756 ? sections.str_offsets_offset : 0));
11757 /* Can we use an existing virtual DWO file? */
11758 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11759 virtual_dwo_name.c_str (),
11760 comp_dir);
11761 /* Create one if necessary. */
11762 if (*dwo_file_slot == NULL)
11763 {
11764 if (dwarf_read_debug)
11765 {
11766 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11767 virtual_dwo_name.c_str ());
11768 }
11769 dwo_file = new struct dwo_file;
11770 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11771 dwo_file->comp_dir = comp_dir;
11772 dwo_file->sections.abbrev =
11773 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11774 sections.abbrev_offset, sections.abbrev_size);
11775 dwo_file->sections.line =
11776 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11777 sections.line_offset, sections.line_size);
11778 dwo_file->sections.loc =
11779 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11780 sections.loc_offset, sections.loc_size);
11781 dwo_file->sections.macinfo =
11782 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11783 sections.macinfo_offset, sections.macinfo_size);
11784 dwo_file->sections.macro =
11785 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11786 sections.macro_offset, sections.macro_size);
11787 dwo_file->sections.str_offsets =
11788 create_dwp_v2_section (dwarf2_per_objfile,
11789 &dwp_file->sections.str_offsets,
11790 sections.str_offsets_offset,
11791 sections.str_offsets_size);
11792 /* The "str" section is global to the entire DWP file. */
11793 dwo_file->sections.str = dwp_file->sections.str;
11794 /* The info or types section is assigned below to dwo_unit,
11795 there's no need to record it in dwo_file.
11796 Also, we can't simply record type sections in dwo_file because
11797 we record a pointer into the vector in dwo_unit. As we collect more
11798 types we'll grow the vector and eventually have to reallocate space
11799 for it, invalidating all copies of pointers into the previous
11800 contents. */
11801 *dwo_file_slot = dwo_file;
11802 }
11803 else
11804 {
11805 if (dwarf_read_debug)
11806 {
11807 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11808 virtual_dwo_name.c_str ());
11809 }
11810 dwo_file = (struct dwo_file *) *dwo_file_slot;
11811 }
11812
11813 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11814 dwo_unit->dwo_file = dwo_file;
11815 dwo_unit->signature = signature;
11816 dwo_unit->section =
11817 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11818 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11819 is_debug_types
11820 ? &dwp_file->sections.types
11821 : &dwp_file->sections.info,
11822 sections.info_or_types_offset,
11823 sections.info_or_types_size);
11824 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11825
11826 return dwo_unit;
11827 }
11828
11829 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11830 Returns NULL if the signature isn't found. */
11831
11832 static struct dwo_unit *
11833 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11834 struct dwp_file *dwp_file, const char *comp_dir,
11835 ULONGEST signature, int is_debug_types)
11836 {
11837 const struct dwp_hash_table *dwp_htab =
11838 is_debug_types ? dwp_file->tus : dwp_file->cus;
11839 bfd *dbfd = dwp_file->dbfd.get ();
11840 uint32_t mask = dwp_htab->nr_slots - 1;
11841 uint32_t hash = signature & mask;
11842 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11843 unsigned int i;
11844 void **slot;
11845 struct dwo_unit find_dwo_cu;
11846
11847 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11848 find_dwo_cu.signature = signature;
11849 slot = htab_find_slot (is_debug_types
11850 ? dwp_file->loaded_tus.get ()
11851 : dwp_file->loaded_cus.get (),
11852 &find_dwo_cu, INSERT);
11853
11854 if (*slot != NULL)
11855 return (struct dwo_unit *) *slot;
11856
11857 /* Use a for loop so that we don't loop forever on bad debug info. */
11858 for (i = 0; i < dwp_htab->nr_slots; ++i)
11859 {
11860 ULONGEST signature_in_table;
11861
11862 signature_in_table =
11863 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11864 if (signature_in_table == signature)
11865 {
11866 uint32_t unit_index =
11867 read_4_bytes (dbfd,
11868 dwp_htab->unit_table + hash * sizeof (uint32_t));
11869
11870 if (dwp_file->version == 1)
11871 {
11872 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11873 dwp_file, unit_index,
11874 comp_dir, signature,
11875 is_debug_types);
11876 }
11877 else
11878 {
11879 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11880 dwp_file, unit_index,
11881 comp_dir, signature,
11882 is_debug_types);
11883 }
11884 return (struct dwo_unit *) *slot;
11885 }
11886 if (signature_in_table == 0)
11887 return NULL;
11888 hash = (hash + hash2) & mask;
11889 }
11890
11891 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11892 " [in module %s]"),
11893 dwp_file->name);
11894 }
11895
11896 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11897 Open the file specified by FILE_NAME and hand it off to BFD for
11898 preliminary analysis. Return a newly initialized bfd *, which
11899 includes a canonicalized copy of FILE_NAME.
11900 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11901 SEARCH_CWD is true if the current directory is to be searched.
11902 It will be searched before debug-file-directory.
11903 If successful, the file is added to the bfd include table of the
11904 objfile's bfd (see gdb_bfd_record_inclusion).
11905 If unable to find/open the file, return NULL.
11906 NOTE: This function is derived from symfile_bfd_open. */
11907
11908 static gdb_bfd_ref_ptr
11909 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11910 const char *file_name, int is_dwp, int search_cwd)
11911 {
11912 int desc;
11913 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11914 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11915 to debug_file_directory. */
11916 const char *search_path;
11917 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11918
11919 gdb::unique_xmalloc_ptr<char> search_path_holder;
11920 if (search_cwd)
11921 {
11922 if (*debug_file_directory != '\0')
11923 {
11924 search_path_holder.reset (concat (".", dirname_separator_string,
11925 debug_file_directory,
11926 (char *) NULL));
11927 search_path = search_path_holder.get ();
11928 }
11929 else
11930 search_path = ".";
11931 }
11932 else
11933 search_path = debug_file_directory;
11934
11935 openp_flags flags = OPF_RETURN_REALPATH;
11936 if (is_dwp)
11937 flags |= OPF_SEARCH_IN_PATH;
11938
11939 gdb::unique_xmalloc_ptr<char> absolute_name;
11940 desc = openp (search_path, flags, file_name,
11941 O_RDONLY | O_BINARY, &absolute_name);
11942 if (desc < 0)
11943 return NULL;
11944
11945 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11946 gnutarget, desc));
11947 if (sym_bfd == NULL)
11948 return NULL;
11949 bfd_set_cacheable (sym_bfd.get (), 1);
11950
11951 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11952 return NULL;
11953
11954 /* Success. Record the bfd as having been included by the objfile's bfd.
11955 This is important because things like demangled_names_hash lives in the
11956 objfile's per_bfd space and may have references to things like symbol
11957 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11958 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
11959
11960 return sym_bfd;
11961 }
11962
11963 /* Try to open DWO file FILE_NAME.
11964 COMP_DIR is the DW_AT_comp_dir attribute.
11965 The result is the bfd handle of the file.
11966 If there is a problem finding or opening the file, return NULL.
11967 Upon success, the canonicalized path of the file is stored in the bfd,
11968 same as symfile_bfd_open. */
11969
11970 static gdb_bfd_ref_ptr
11971 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11972 const char *file_name, const char *comp_dir)
11973 {
11974 if (IS_ABSOLUTE_PATH (file_name))
11975 return try_open_dwop_file (dwarf2_per_objfile, file_name,
11976 0 /*is_dwp*/, 0 /*search_cwd*/);
11977
11978 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11979
11980 if (comp_dir != NULL)
11981 {
11982 gdb::unique_xmalloc_ptr<char> path_to_try
11983 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
11984
11985 /* NOTE: If comp_dir is a relative path, this will also try the
11986 search path, which seems useful. */
11987 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
11988 path_to_try.get (),
11989 0 /*is_dwp*/,
11990 1 /*search_cwd*/));
11991 if (abfd != NULL)
11992 return abfd;
11993 }
11994
11995 /* That didn't work, try debug-file-directory, which, despite its name,
11996 is a list of paths. */
11997
11998 if (*debug_file_directory == '\0')
11999 return NULL;
12000
12001 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12002 0 /*is_dwp*/, 1 /*search_cwd*/);
12003 }
12004
12005 /* This function is mapped across the sections and remembers the offset and
12006 size of each of the DWO debugging sections we are interested in. */
12007
12008 static void
12009 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12010 {
12011 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12012 const struct dwop_section_names *names = &dwop_section_names;
12013
12014 if (section_is_p (sectp->name, &names->abbrev_dwo))
12015 {
12016 dwo_sections->abbrev.s.section = sectp;
12017 dwo_sections->abbrev.size = bfd_section_size (sectp);
12018 }
12019 else if (section_is_p (sectp->name, &names->info_dwo))
12020 {
12021 dwo_sections->info.s.section = sectp;
12022 dwo_sections->info.size = bfd_section_size (sectp);
12023 }
12024 else if (section_is_p (sectp->name, &names->line_dwo))
12025 {
12026 dwo_sections->line.s.section = sectp;
12027 dwo_sections->line.size = bfd_section_size (sectp);
12028 }
12029 else if (section_is_p (sectp->name, &names->loc_dwo))
12030 {
12031 dwo_sections->loc.s.section = sectp;
12032 dwo_sections->loc.size = bfd_section_size (sectp);
12033 }
12034 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12035 {
12036 dwo_sections->macinfo.s.section = sectp;
12037 dwo_sections->macinfo.size = bfd_section_size (sectp);
12038 }
12039 else if (section_is_p (sectp->name, &names->macro_dwo))
12040 {
12041 dwo_sections->macro.s.section = sectp;
12042 dwo_sections->macro.size = bfd_section_size (sectp);
12043 }
12044 else if (section_is_p (sectp->name, &names->str_dwo))
12045 {
12046 dwo_sections->str.s.section = sectp;
12047 dwo_sections->str.size = bfd_section_size (sectp);
12048 }
12049 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12050 {
12051 dwo_sections->str_offsets.s.section = sectp;
12052 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12053 }
12054 else if (section_is_p (sectp->name, &names->types_dwo))
12055 {
12056 struct dwarf2_section_info type_section;
12057
12058 memset (&type_section, 0, sizeof (type_section));
12059 type_section.s.section = sectp;
12060 type_section.size = bfd_section_size (sectp);
12061 dwo_sections->types.push_back (type_section);
12062 }
12063 }
12064
12065 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12066 by PER_CU. This is for the non-DWP case.
12067 The result is NULL if DWO_NAME can't be found. */
12068
12069 static struct dwo_file *
12070 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12071 const char *dwo_name, const char *comp_dir)
12072 {
12073 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12074
12075 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12076 if (dbfd == NULL)
12077 {
12078 if (dwarf_read_debug)
12079 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12080 return NULL;
12081 }
12082
12083 dwo_file_up dwo_file (new struct dwo_file);
12084 dwo_file->dwo_name = dwo_name;
12085 dwo_file->comp_dir = comp_dir;
12086 dwo_file->dbfd = std::move (dbfd);
12087
12088 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12089 &dwo_file->sections);
12090
12091 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12092 dwo_file->sections.info, dwo_file->cus);
12093
12094 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12095 dwo_file->sections.types, dwo_file->tus);
12096
12097 if (dwarf_read_debug)
12098 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12099
12100 return dwo_file.release ();
12101 }
12102
12103 /* This function is mapped across the sections and remembers the offset and
12104 size of each of the DWP debugging sections common to version 1 and 2 that
12105 we are interested in. */
12106
12107 static void
12108 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12109 void *dwp_file_ptr)
12110 {
12111 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12112 const struct dwop_section_names *names = &dwop_section_names;
12113 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12114
12115 /* Record the ELF section number for later lookup: this is what the
12116 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12117 gdb_assert (elf_section_nr < dwp_file->num_sections);
12118 dwp_file->elf_sections[elf_section_nr] = sectp;
12119
12120 /* Look for specific sections that we need. */
12121 if (section_is_p (sectp->name, &names->str_dwo))
12122 {
12123 dwp_file->sections.str.s.section = sectp;
12124 dwp_file->sections.str.size = bfd_section_size (sectp);
12125 }
12126 else if (section_is_p (sectp->name, &names->cu_index))
12127 {
12128 dwp_file->sections.cu_index.s.section = sectp;
12129 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12130 }
12131 else if (section_is_p (sectp->name, &names->tu_index))
12132 {
12133 dwp_file->sections.tu_index.s.section = sectp;
12134 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12135 }
12136 }
12137
12138 /* This function is mapped across the sections and remembers the offset and
12139 size of each of the DWP version 2 debugging sections that we are interested
12140 in. This is split into a separate function because we don't know if we
12141 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12142
12143 static void
12144 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12145 {
12146 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12147 const struct dwop_section_names *names = &dwop_section_names;
12148 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12149
12150 /* Record the ELF section number for later lookup: this is what the
12151 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12152 gdb_assert (elf_section_nr < dwp_file->num_sections);
12153 dwp_file->elf_sections[elf_section_nr] = sectp;
12154
12155 /* Look for specific sections that we need. */
12156 if (section_is_p (sectp->name, &names->abbrev_dwo))
12157 {
12158 dwp_file->sections.abbrev.s.section = sectp;
12159 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12160 }
12161 else if (section_is_p (sectp->name, &names->info_dwo))
12162 {
12163 dwp_file->sections.info.s.section = sectp;
12164 dwp_file->sections.info.size = bfd_section_size (sectp);
12165 }
12166 else if (section_is_p (sectp->name, &names->line_dwo))
12167 {
12168 dwp_file->sections.line.s.section = sectp;
12169 dwp_file->sections.line.size = bfd_section_size (sectp);
12170 }
12171 else if (section_is_p (sectp->name, &names->loc_dwo))
12172 {
12173 dwp_file->sections.loc.s.section = sectp;
12174 dwp_file->sections.loc.size = bfd_section_size (sectp);
12175 }
12176 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12177 {
12178 dwp_file->sections.macinfo.s.section = sectp;
12179 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12180 }
12181 else if (section_is_p (sectp->name, &names->macro_dwo))
12182 {
12183 dwp_file->sections.macro.s.section = sectp;
12184 dwp_file->sections.macro.size = bfd_section_size (sectp);
12185 }
12186 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12187 {
12188 dwp_file->sections.str_offsets.s.section = sectp;
12189 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12190 }
12191 else if (section_is_p (sectp->name, &names->types_dwo))
12192 {
12193 dwp_file->sections.types.s.section = sectp;
12194 dwp_file->sections.types.size = bfd_section_size (sectp);
12195 }
12196 }
12197
12198 /* Hash function for dwp_file loaded CUs/TUs. */
12199
12200 static hashval_t
12201 hash_dwp_loaded_cutus (const void *item)
12202 {
12203 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12204
12205 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12206 return dwo_unit->signature;
12207 }
12208
12209 /* Equality function for dwp_file loaded CUs/TUs. */
12210
12211 static int
12212 eq_dwp_loaded_cutus (const void *a, const void *b)
12213 {
12214 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12215 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12216
12217 return dua->signature == dub->signature;
12218 }
12219
12220 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12221
12222 static htab_up
12223 allocate_dwp_loaded_cutus_table ()
12224 {
12225 return htab_up (htab_create_alloc (3,
12226 hash_dwp_loaded_cutus,
12227 eq_dwp_loaded_cutus,
12228 NULL, xcalloc, xfree));
12229 }
12230
12231 /* Try to open DWP file FILE_NAME.
12232 The result is the bfd handle of the file.
12233 If there is a problem finding or opening the file, return NULL.
12234 Upon success, the canonicalized path of the file is stored in the bfd,
12235 same as symfile_bfd_open. */
12236
12237 static gdb_bfd_ref_ptr
12238 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12239 const char *file_name)
12240 {
12241 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12242 1 /*is_dwp*/,
12243 1 /*search_cwd*/));
12244 if (abfd != NULL)
12245 return abfd;
12246
12247 /* Work around upstream bug 15652.
12248 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12249 [Whether that's a "bug" is debatable, but it is getting in our way.]
12250 We have no real idea where the dwp file is, because gdb's realpath-ing
12251 of the executable's path may have discarded the needed info.
12252 [IWBN if the dwp file name was recorded in the executable, akin to
12253 .gnu_debuglink, but that doesn't exist yet.]
12254 Strip the directory from FILE_NAME and search again. */
12255 if (*debug_file_directory != '\0')
12256 {
12257 /* Don't implicitly search the current directory here.
12258 If the user wants to search "." to handle this case,
12259 it must be added to debug-file-directory. */
12260 return try_open_dwop_file (dwarf2_per_objfile,
12261 lbasename (file_name), 1 /*is_dwp*/,
12262 0 /*search_cwd*/);
12263 }
12264
12265 return NULL;
12266 }
12267
12268 /* Initialize the use of the DWP file for the current objfile.
12269 By convention the name of the DWP file is ${objfile}.dwp.
12270 The result is NULL if it can't be found. */
12271
12272 static std::unique_ptr<struct dwp_file>
12273 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12274 {
12275 struct objfile *objfile = dwarf2_per_objfile->objfile;
12276
12277 /* Try to find first .dwp for the binary file before any symbolic links
12278 resolving. */
12279
12280 /* If the objfile is a debug file, find the name of the real binary
12281 file and get the name of dwp file from there. */
12282 std::string dwp_name;
12283 if (objfile->separate_debug_objfile_backlink != NULL)
12284 {
12285 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12286 const char *backlink_basename = lbasename (backlink->original_name);
12287
12288 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12289 }
12290 else
12291 dwp_name = objfile->original_name;
12292
12293 dwp_name += ".dwp";
12294
12295 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12296 if (dbfd == NULL
12297 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12298 {
12299 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12300 dwp_name = objfile_name (objfile);
12301 dwp_name += ".dwp";
12302 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12303 }
12304
12305 if (dbfd == NULL)
12306 {
12307 if (dwarf_read_debug)
12308 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12309 return std::unique_ptr<dwp_file> ();
12310 }
12311
12312 const char *name = bfd_get_filename (dbfd.get ());
12313 std::unique_ptr<struct dwp_file> dwp_file
12314 (new struct dwp_file (name, std::move (dbfd)));
12315
12316 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12317 dwp_file->elf_sections =
12318 OBSTACK_CALLOC (&objfile->objfile_obstack,
12319 dwp_file->num_sections, asection *);
12320
12321 bfd_map_over_sections (dwp_file->dbfd.get (),
12322 dwarf2_locate_common_dwp_sections,
12323 dwp_file.get ());
12324
12325 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12326 0);
12327
12328 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12329 1);
12330
12331 /* The DWP file version is stored in the hash table. Oh well. */
12332 if (dwp_file->cus && dwp_file->tus
12333 && dwp_file->cus->version != dwp_file->tus->version)
12334 {
12335 /* Technically speaking, we should try to limp along, but this is
12336 pretty bizarre. We use pulongest here because that's the established
12337 portability solution (e.g, we cannot use %u for uint32_t). */
12338 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12339 " TU version %s [in DWP file %s]"),
12340 pulongest (dwp_file->cus->version),
12341 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12342 }
12343
12344 if (dwp_file->cus)
12345 dwp_file->version = dwp_file->cus->version;
12346 else if (dwp_file->tus)
12347 dwp_file->version = dwp_file->tus->version;
12348 else
12349 dwp_file->version = 2;
12350
12351 if (dwp_file->version == 2)
12352 bfd_map_over_sections (dwp_file->dbfd.get (),
12353 dwarf2_locate_v2_dwp_sections,
12354 dwp_file.get ());
12355
12356 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12357 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12358
12359 if (dwarf_read_debug)
12360 {
12361 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12362 fprintf_unfiltered (gdb_stdlog,
12363 " %s CUs, %s TUs\n",
12364 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12365 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12366 }
12367
12368 return dwp_file;
12369 }
12370
12371 /* Wrapper around open_and_init_dwp_file, only open it once. */
12372
12373 static struct dwp_file *
12374 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12375 {
12376 if (! dwarf2_per_objfile->dwp_checked)
12377 {
12378 dwarf2_per_objfile->dwp_file
12379 = open_and_init_dwp_file (dwarf2_per_objfile);
12380 dwarf2_per_objfile->dwp_checked = 1;
12381 }
12382 return dwarf2_per_objfile->dwp_file.get ();
12383 }
12384
12385 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12386 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12387 or in the DWP file for the objfile, referenced by THIS_UNIT.
12388 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12389 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12390
12391 This is called, for example, when wanting to read a variable with a
12392 complex location. Therefore we don't want to do file i/o for every call.
12393 Therefore we don't want to look for a DWO file on every call.
12394 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12395 then we check if we've already seen DWO_NAME, and only THEN do we check
12396 for a DWO file.
12397
12398 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12399 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12400
12401 static struct dwo_unit *
12402 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12403 const char *dwo_name, const char *comp_dir,
12404 ULONGEST signature, int is_debug_types)
12405 {
12406 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12407 struct objfile *objfile = dwarf2_per_objfile->objfile;
12408 const char *kind = is_debug_types ? "TU" : "CU";
12409 void **dwo_file_slot;
12410 struct dwo_file *dwo_file;
12411 struct dwp_file *dwp_file;
12412
12413 /* First see if there's a DWP file.
12414 If we have a DWP file but didn't find the DWO inside it, don't
12415 look for the original DWO file. It makes gdb behave differently
12416 depending on whether one is debugging in the build tree. */
12417
12418 dwp_file = get_dwp_file (dwarf2_per_objfile);
12419 if (dwp_file != NULL)
12420 {
12421 const struct dwp_hash_table *dwp_htab =
12422 is_debug_types ? dwp_file->tus : dwp_file->cus;
12423
12424 if (dwp_htab != NULL)
12425 {
12426 struct dwo_unit *dwo_cutu =
12427 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12428 signature, is_debug_types);
12429
12430 if (dwo_cutu != NULL)
12431 {
12432 if (dwarf_read_debug)
12433 {
12434 fprintf_unfiltered (gdb_stdlog,
12435 "Virtual DWO %s %s found: @%s\n",
12436 kind, hex_string (signature),
12437 host_address_to_string (dwo_cutu));
12438 }
12439 return dwo_cutu;
12440 }
12441 }
12442 }
12443 else
12444 {
12445 /* No DWP file, look for the DWO file. */
12446
12447 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12448 dwo_name, comp_dir);
12449 if (*dwo_file_slot == NULL)
12450 {
12451 /* Read in the file and build a table of the CUs/TUs it contains. */
12452 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12453 }
12454 /* NOTE: This will be NULL if unable to open the file. */
12455 dwo_file = (struct dwo_file *) *dwo_file_slot;
12456
12457 if (dwo_file != NULL)
12458 {
12459 struct dwo_unit *dwo_cutu = NULL;
12460
12461 if (is_debug_types && dwo_file->tus)
12462 {
12463 struct dwo_unit find_dwo_cutu;
12464
12465 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12466 find_dwo_cutu.signature = signature;
12467 dwo_cutu
12468 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12469 &find_dwo_cutu);
12470 }
12471 else if (!is_debug_types && dwo_file->cus)
12472 {
12473 struct dwo_unit find_dwo_cutu;
12474
12475 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12476 find_dwo_cutu.signature = signature;
12477 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12478 &find_dwo_cutu);
12479 }
12480
12481 if (dwo_cutu != NULL)
12482 {
12483 if (dwarf_read_debug)
12484 {
12485 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12486 kind, dwo_name, hex_string (signature),
12487 host_address_to_string (dwo_cutu));
12488 }
12489 return dwo_cutu;
12490 }
12491 }
12492 }
12493
12494 /* We didn't find it. This could mean a dwo_id mismatch, or
12495 someone deleted the DWO/DWP file, or the search path isn't set up
12496 correctly to find the file. */
12497
12498 if (dwarf_read_debug)
12499 {
12500 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12501 kind, dwo_name, hex_string (signature));
12502 }
12503
12504 /* This is a warning and not a complaint because it can be caused by
12505 pilot error (e.g., user accidentally deleting the DWO). */
12506 {
12507 /* Print the name of the DWP file if we looked there, helps the user
12508 better diagnose the problem. */
12509 std::string dwp_text;
12510
12511 if (dwp_file != NULL)
12512 dwp_text = string_printf (" [in DWP file %s]",
12513 lbasename (dwp_file->name));
12514
12515 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12516 " [in module %s]"),
12517 kind, dwo_name, hex_string (signature),
12518 dwp_text.c_str (),
12519 this_unit->is_debug_types ? "TU" : "CU",
12520 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12521 }
12522 return NULL;
12523 }
12524
12525 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12526 See lookup_dwo_cutu_unit for details. */
12527
12528 static struct dwo_unit *
12529 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12530 const char *dwo_name, const char *comp_dir,
12531 ULONGEST signature)
12532 {
12533 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12534 }
12535
12536 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12537 See lookup_dwo_cutu_unit for details. */
12538
12539 static struct dwo_unit *
12540 lookup_dwo_type_unit (struct signatured_type *this_tu,
12541 const char *dwo_name, const char *comp_dir)
12542 {
12543 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12544 }
12545
12546 /* Traversal function for queue_and_load_all_dwo_tus. */
12547
12548 static int
12549 queue_and_load_dwo_tu (void **slot, void *info)
12550 {
12551 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12552 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12553 ULONGEST signature = dwo_unit->signature;
12554 struct signatured_type *sig_type =
12555 lookup_dwo_signatured_type (per_cu->cu, signature);
12556
12557 if (sig_type != NULL)
12558 {
12559 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12560
12561 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12562 a real dependency of PER_CU on SIG_TYPE. That is detected later
12563 while processing PER_CU. */
12564 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12565 load_full_type_unit (sig_cu);
12566 per_cu->imported_symtabs_push (sig_cu);
12567 }
12568
12569 return 1;
12570 }
12571
12572 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12573 The DWO may have the only definition of the type, though it may not be
12574 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12575 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12576
12577 static void
12578 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12579 {
12580 struct dwo_unit *dwo_unit;
12581 struct dwo_file *dwo_file;
12582
12583 gdb_assert (!per_cu->is_debug_types);
12584 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12585 gdb_assert (per_cu->cu != NULL);
12586
12587 dwo_unit = per_cu->cu->dwo_unit;
12588 gdb_assert (dwo_unit != NULL);
12589
12590 dwo_file = dwo_unit->dwo_file;
12591 if (dwo_file->tus != NULL)
12592 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12593 per_cu);
12594 }
12595
12596 /* Read in various DIEs. */
12597
12598 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12599 Inherit only the children of the DW_AT_abstract_origin DIE not being
12600 already referenced by DW_AT_abstract_origin from the children of the
12601 current DIE. */
12602
12603 static void
12604 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12605 {
12606 struct die_info *child_die;
12607 sect_offset *offsetp;
12608 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12609 struct die_info *origin_die;
12610 /* Iterator of the ORIGIN_DIE children. */
12611 struct die_info *origin_child_die;
12612 struct attribute *attr;
12613 struct dwarf2_cu *origin_cu;
12614 struct pending **origin_previous_list_in_scope;
12615
12616 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12617 if (!attr)
12618 return;
12619
12620 /* Note that following die references may follow to a die in a
12621 different cu. */
12622
12623 origin_cu = cu;
12624 origin_die = follow_die_ref (die, attr, &origin_cu);
12625
12626 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12627 symbols in. */
12628 origin_previous_list_in_scope = origin_cu->list_in_scope;
12629 origin_cu->list_in_scope = cu->list_in_scope;
12630
12631 if (die->tag != origin_die->tag
12632 && !(die->tag == DW_TAG_inlined_subroutine
12633 && origin_die->tag == DW_TAG_subprogram))
12634 complaint (_("DIE %s and its abstract origin %s have different tags"),
12635 sect_offset_str (die->sect_off),
12636 sect_offset_str (origin_die->sect_off));
12637
12638 std::vector<sect_offset> offsets;
12639
12640 for (child_die = die->child;
12641 child_die && child_die->tag;
12642 child_die = child_die->sibling)
12643 {
12644 struct die_info *child_origin_die;
12645 struct dwarf2_cu *child_origin_cu;
12646
12647 /* We are trying to process concrete instance entries:
12648 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12649 it's not relevant to our analysis here. i.e. detecting DIEs that are
12650 present in the abstract instance but not referenced in the concrete
12651 one. */
12652 if (child_die->tag == DW_TAG_call_site
12653 || child_die->tag == DW_TAG_GNU_call_site)
12654 continue;
12655
12656 /* For each CHILD_DIE, find the corresponding child of
12657 ORIGIN_DIE. If there is more than one layer of
12658 DW_AT_abstract_origin, follow them all; there shouldn't be,
12659 but GCC versions at least through 4.4 generate this (GCC PR
12660 40573). */
12661 child_origin_die = child_die;
12662 child_origin_cu = cu;
12663 while (1)
12664 {
12665 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12666 child_origin_cu);
12667 if (attr == NULL)
12668 break;
12669 child_origin_die = follow_die_ref (child_origin_die, attr,
12670 &child_origin_cu);
12671 }
12672
12673 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12674 counterpart may exist. */
12675 if (child_origin_die != child_die)
12676 {
12677 if (child_die->tag != child_origin_die->tag
12678 && !(child_die->tag == DW_TAG_inlined_subroutine
12679 && child_origin_die->tag == DW_TAG_subprogram))
12680 complaint (_("Child DIE %s and its abstract origin %s have "
12681 "different tags"),
12682 sect_offset_str (child_die->sect_off),
12683 sect_offset_str (child_origin_die->sect_off));
12684 if (child_origin_die->parent != origin_die)
12685 complaint (_("Child DIE %s and its abstract origin %s have "
12686 "different parents"),
12687 sect_offset_str (child_die->sect_off),
12688 sect_offset_str (child_origin_die->sect_off));
12689 else
12690 offsets.push_back (child_origin_die->sect_off);
12691 }
12692 }
12693 std::sort (offsets.begin (), offsets.end ());
12694 sect_offset *offsets_end = offsets.data () + offsets.size ();
12695 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12696 if (offsetp[-1] == *offsetp)
12697 complaint (_("Multiple children of DIE %s refer "
12698 "to DIE %s as their abstract origin"),
12699 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12700
12701 offsetp = offsets.data ();
12702 origin_child_die = origin_die->child;
12703 while (origin_child_die && origin_child_die->tag)
12704 {
12705 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12706 while (offsetp < offsets_end
12707 && *offsetp < origin_child_die->sect_off)
12708 offsetp++;
12709 if (offsetp >= offsets_end
12710 || *offsetp > origin_child_die->sect_off)
12711 {
12712 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12713 Check whether we're already processing ORIGIN_CHILD_DIE.
12714 This can happen with mutually referenced abstract_origins.
12715 PR 16581. */
12716 if (!origin_child_die->in_process)
12717 process_die (origin_child_die, origin_cu);
12718 }
12719 origin_child_die = origin_child_die->sibling;
12720 }
12721 origin_cu->list_in_scope = origin_previous_list_in_scope;
12722
12723 if (cu != origin_cu)
12724 compute_delayed_physnames (origin_cu);
12725 }
12726
12727 static void
12728 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12729 {
12730 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12731 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12732 struct context_stack *newobj;
12733 CORE_ADDR lowpc;
12734 CORE_ADDR highpc;
12735 struct die_info *child_die;
12736 struct attribute *attr, *call_line, *call_file;
12737 const char *name;
12738 CORE_ADDR baseaddr;
12739 struct block *block;
12740 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12741 std::vector<struct symbol *> template_args;
12742 struct template_symbol *templ_func = NULL;
12743
12744 if (inlined_func)
12745 {
12746 /* If we do not have call site information, we can't show the
12747 caller of this inlined function. That's too confusing, so
12748 only use the scope for local variables. */
12749 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12750 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12751 if (call_line == NULL || call_file == NULL)
12752 {
12753 read_lexical_block_scope (die, cu);
12754 return;
12755 }
12756 }
12757
12758 baseaddr = objfile->text_section_offset ();
12759
12760 name = dwarf2_name (die, cu);
12761
12762 /* Ignore functions with missing or empty names. These are actually
12763 illegal according to the DWARF standard. */
12764 if (name == NULL)
12765 {
12766 complaint (_("missing name for subprogram DIE at %s"),
12767 sect_offset_str (die->sect_off));
12768 return;
12769 }
12770
12771 /* Ignore functions with missing or invalid low and high pc attributes. */
12772 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12773 <= PC_BOUNDS_INVALID)
12774 {
12775 attr = dwarf2_attr (die, DW_AT_external, cu);
12776 if (!attr || !DW_UNSND (attr))
12777 complaint (_("cannot get low and high bounds "
12778 "for subprogram DIE at %s"),
12779 sect_offset_str (die->sect_off));
12780 return;
12781 }
12782
12783 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12784 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12785
12786 /* If we have any template arguments, then we must allocate a
12787 different sort of symbol. */
12788 for (child_die = die->child; child_die; child_die = child_die->sibling)
12789 {
12790 if (child_die->tag == DW_TAG_template_type_param
12791 || child_die->tag == DW_TAG_template_value_param)
12792 {
12793 templ_func = allocate_template_symbol (objfile);
12794 templ_func->subclass = SYMBOL_TEMPLATE;
12795 break;
12796 }
12797 }
12798
12799 newobj = cu->get_builder ()->push_context (0, lowpc);
12800 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12801 (struct symbol *) templ_func);
12802
12803 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12804 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12805 cu->language);
12806
12807 /* If there is a location expression for DW_AT_frame_base, record
12808 it. */
12809 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12810 if (attr != nullptr)
12811 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12812
12813 /* If there is a location for the static link, record it. */
12814 newobj->static_link = NULL;
12815 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12816 if (attr != nullptr)
12817 {
12818 newobj->static_link
12819 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12820 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12821 cu->per_cu->addr_type ());
12822 }
12823
12824 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12825
12826 if (die->child != NULL)
12827 {
12828 child_die = die->child;
12829 while (child_die && child_die->tag)
12830 {
12831 if (child_die->tag == DW_TAG_template_type_param
12832 || child_die->tag == DW_TAG_template_value_param)
12833 {
12834 struct symbol *arg = new_symbol (child_die, NULL, cu);
12835
12836 if (arg != NULL)
12837 template_args.push_back (arg);
12838 }
12839 else
12840 process_die (child_die, cu);
12841 child_die = child_die->sibling;
12842 }
12843 }
12844
12845 inherit_abstract_dies (die, cu);
12846
12847 /* If we have a DW_AT_specification, we might need to import using
12848 directives from the context of the specification DIE. See the
12849 comment in determine_prefix. */
12850 if (cu->language == language_cplus
12851 && dwarf2_attr (die, DW_AT_specification, cu))
12852 {
12853 struct dwarf2_cu *spec_cu = cu;
12854 struct die_info *spec_die = die_specification (die, &spec_cu);
12855
12856 while (spec_die)
12857 {
12858 child_die = spec_die->child;
12859 while (child_die && child_die->tag)
12860 {
12861 if (child_die->tag == DW_TAG_imported_module)
12862 process_die (child_die, spec_cu);
12863 child_die = child_die->sibling;
12864 }
12865
12866 /* In some cases, GCC generates specification DIEs that
12867 themselves contain DW_AT_specification attributes. */
12868 spec_die = die_specification (spec_die, &spec_cu);
12869 }
12870 }
12871
12872 struct context_stack cstk = cu->get_builder ()->pop_context ();
12873 /* Make a block for the local symbols within. */
12874 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12875 cstk.static_link, lowpc, highpc);
12876
12877 /* For C++, set the block's scope. */
12878 if ((cu->language == language_cplus
12879 || cu->language == language_fortran
12880 || cu->language == language_d
12881 || cu->language == language_rust)
12882 && cu->processing_has_namespace_info)
12883 block_set_scope (block, determine_prefix (die, cu),
12884 &objfile->objfile_obstack);
12885
12886 /* If we have address ranges, record them. */
12887 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12888
12889 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12890
12891 /* Attach template arguments to function. */
12892 if (!template_args.empty ())
12893 {
12894 gdb_assert (templ_func != NULL);
12895
12896 templ_func->n_template_arguments = template_args.size ();
12897 templ_func->template_arguments
12898 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12899 templ_func->n_template_arguments);
12900 memcpy (templ_func->template_arguments,
12901 template_args.data (),
12902 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12903
12904 /* Make sure that the symtab is set on the new symbols. Even
12905 though they don't appear in this symtab directly, other parts
12906 of gdb assume that symbols do, and this is reasonably
12907 true. */
12908 for (symbol *sym : template_args)
12909 symbol_set_symtab (sym, symbol_symtab (templ_func));
12910 }
12911
12912 /* In C++, we can have functions nested inside functions (e.g., when
12913 a function declares a class that has methods). This means that
12914 when we finish processing a function scope, we may need to go
12915 back to building a containing block's symbol lists. */
12916 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12917 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12918
12919 /* If we've finished processing a top-level function, subsequent
12920 symbols go in the file symbol list. */
12921 if (cu->get_builder ()->outermost_context_p ())
12922 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12923 }
12924
12925 /* Process all the DIES contained within a lexical block scope. Start
12926 a new scope, process the dies, and then close the scope. */
12927
12928 static void
12929 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12930 {
12931 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12932 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12933 CORE_ADDR lowpc, highpc;
12934 struct die_info *child_die;
12935 CORE_ADDR baseaddr;
12936
12937 baseaddr = objfile->text_section_offset ();
12938
12939 /* Ignore blocks with missing or invalid low and high pc attributes. */
12940 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12941 as multiple lexical blocks? Handling children in a sane way would
12942 be nasty. Might be easier to properly extend generic blocks to
12943 describe ranges. */
12944 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12945 {
12946 case PC_BOUNDS_NOT_PRESENT:
12947 /* DW_TAG_lexical_block has no attributes, process its children as if
12948 there was no wrapping by that DW_TAG_lexical_block.
12949 GCC does no longer produces such DWARF since GCC r224161. */
12950 for (child_die = die->child;
12951 child_die != NULL && child_die->tag;
12952 child_die = child_die->sibling)
12953 process_die (child_die, cu);
12954 return;
12955 case PC_BOUNDS_INVALID:
12956 return;
12957 }
12958 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12959 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12960
12961 cu->get_builder ()->push_context (0, lowpc);
12962 if (die->child != NULL)
12963 {
12964 child_die = die->child;
12965 while (child_die && child_die->tag)
12966 {
12967 process_die (child_die, cu);
12968 child_die = child_die->sibling;
12969 }
12970 }
12971 inherit_abstract_dies (die, cu);
12972 struct context_stack cstk = cu->get_builder ()->pop_context ();
12973
12974 if (*cu->get_builder ()->get_local_symbols () != NULL
12975 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
12976 {
12977 struct block *block
12978 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
12979 cstk.start_addr, highpc);
12980
12981 /* Note that recording ranges after traversing children, as we
12982 do here, means that recording a parent's ranges entails
12983 walking across all its children's ranges as they appear in
12984 the address map, which is quadratic behavior.
12985
12986 It would be nicer to record the parent's ranges before
12987 traversing its children, simply overriding whatever you find
12988 there. But since we don't even decide whether to create a
12989 block until after we've traversed its children, that's hard
12990 to do. */
12991 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12992 }
12993 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12994 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12995 }
12996
12997 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
12998
12999 static void
13000 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13001 {
13002 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13003 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13004 CORE_ADDR pc, baseaddr;
13005 struct attribute *attr;
13006 struct call_site *call_site, call_site_local;
13007 void **slot;
13008 int nparams;
13009 struct die_info *child_die;
13010
13011 baseaddr = objfile->text_section_offset ();
13012
13013 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13014 if (attr == NULL)
13015 {
13016 /* This was a pre-DWARF-5 GNU extension alias
13017 for DW_AT_call_return_pc. */
13018 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13019 }
13020 if (!attr)
13021 {
13022 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13023 "DIE %s [in module %s]"),
13024 sect_offset_str (die->sect_off), objfile_name (objfile));
13025 return;
13026 }
13027 pc = attr->value_as_address () + baseaddr;
13028 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13029
13030 if (cu->call_site_htab == NULL)
13031 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13032 NULL, &objfile->objfile_obstack,
13033 hashtab_obstack_allocate, NULL);
13034 call_site_local.pc = pc;
13035 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13036 if (*slot != NULL)
13037 {
13038 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13039 "DIE %s [in module %s]"),
13040 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13041 objfile_name (objfile));
13042 return;
13043 }
13044
13045 /* Count parameters at the caller. */
13046
13047 nparams = 0;
13048 for (child_die = die->child; child_die && child_die->tag;
13049 child_die = child_die->sibling)
13050 {
13051 if (child_die->tag != DW_TAG_call_site_parameter
13052 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13053 {
13054 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13055 "DW_TAG_call_site child DIE %s [in module %s]"),
13056 child_die->tag, sect_offset_str (child_die->sect_off),
13057 objfile_name (objfile));
13058 continue;
13059 }
13060
13061 nparams++;
13062 }
13063
13064 call_site
13065 = ((struct call_site *)
13066 obstack_alloc (&objfile->objfile_obstack,
13067 sizeof (*call_site)
13068 + (sizeof (*call_site->parameter) * (nparams - 1))));
13069 *slot = call_site;
13070 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13071 call_site->pc = pc;
13072
13073 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13074 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13075 {
13076 struct die_info *func_die;
13077
13078 /* Skip also over DW_TAG_inlined_subroutine. */
13079 for (func_die = die->parent;
13080 func_die && func_die->tag != DW_TAG_subprogram
13081 && func_die->tag != DW_TAG_subroutine_type;
13082 func_die = func_die->parent);
13083
13084 /* DW_AT_call_all_calls is a superset
13085 of DW_AT_call_all_tail_calls. */
13086 if (func_die
13087 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13088 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13089 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13090 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13091 {
13092 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13093 not complete. But keep CALL_SITE for look ups via call_site_htab,
13094 both the initial caller containing the real return address PC and
13095 the final callee containing the current PC of a chain of tail
13096 calls do not need to have the tail call list complete. But any
13097 function candidate for a virtual tail call frame searched via
13098 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13099 determined unambiguously. */
13100 }
13101 else
13102 {
13103 struct type *func_type = NULL;
13104
13105 if (func_die)
13106 func_type = get_die_type (func_die, cu);
13107 if (func_type != NULL)
13108 {
13109 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13110
13111 /* Enlist this call site to the function. */
13112 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13113 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13114 }
13115 else
13116 complaint (_("Cannot find function owning DW_TAG_call_site "
13117 "DIE %s [in module %s]"),
13118 sect_offset_str (die->sect_off), objfile_name (objfile));
13119 }
13120 }
13121
13122 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13123 if (attr == NULL)
13124 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13125 if (attr == NULL)
13126 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13127 if (attr == NULL)
13128 {
13129 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13130 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13131 }
13132 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13133 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13134 /* Keep NULL DWARF_BLOCK. */;
13135 else if (attr->form_is_block ())
13136 {
13137 struct dwarf2_locexpr_baton *dlbaton;
13138
13139 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13140 dlbaton->data = DW_BLOCK (attr)->data;
13141 dlbaton->size = DW_BLOCK (attr)->size;
13142 dlbaton->per_cu = cu->per_cu;
13143
13144 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13145 }
13146 else if (attr->form_is_ref ())
13147 {
13148 struct dwarf2_cu *target_cu = cu;
13149 struct die_info *target_die;
13150
13151 target_die = follow_die_ref (die, attr, &target_cu);
13152 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13153 if (die_is_declaration (target_die, target_cu))
13154 {
13155 const char *target_physname;
13156
13157 /* Prefer the mangled name; otherwise compute the demangled one. */
13158 target_physname = dw2_linkage_name (target_die, target_cu);
13159 if (target_physname == NULL)
13160 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13161 if (target_physname == NULL)
13162 complaint (_("DW_AT_call_target target DIE has invalid "
13163 "physname, for referencing DIE %s [in module %s]"),
13164 sect_offset_str (die->sect_off), objfile_name (objfile));
13165 else
13166 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13167 }
13168 else
13169 {
13170 CORE_ADDR lowpc;
13171
13172 /* DW_AT_entry_pc should be preferred. */
13173 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13174 <= PC_BOUNDS_INVALID)
13175 complaint (_("DW_AT_call_target target DIE has invalid "
13176 "low pc, for referencing DIE %s [in module %s]"),
13177 sect_offset_str (die->sect_off), objfile_name (objfile));
13178 else
13179 {
13180 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13181 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13182 }
13183 }
13184 }
13185 else
13186 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13187 "block nor reference, for DIE %s [in module %s]"),
13188 sect_offset_str (die->sect_off), objfile_name (objfile));
13189
13190 call_site->per_cu = cu->per_cu;
13191
13192 for (child_die = die->child;
13193 child_die && child_die->tag;
13194 child_die = child_die->sibling)
13195 {
13196 struct call_site_parameter *parameter;
13197 struct attribute *loc, *origin;
13198
13199 if (child_die->tag != DW_TAG_call_site_parameter
13200 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13201 {
13202 /* Already printed the complaint above. */
13203 continue;
13204 }
13205
13206 gdb_assert (call_site->parameter_count < nparams);
13207 parameter = &call_site->parameter[call_site->parameter_count];
13208
13209 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13210 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13211 register is contained in DW_AT_call_value. */
13212
13213 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13214 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13215 if (origin == NULL)
13216 {
13217 /* This was a pre-DWARF-5 GNU extension alias
13218 for DW_AT_call_parameter. */
13219 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13220 }
13221 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13222 {
13223 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13224
13225 sect_offset sect_off = origin->get_ref_die_offset ();
13226 if (!cu->header.offset_in_cu_p (sect_off))
13227 {
13228 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13229 binding can be done only inside one CU. Such referenced DIE
13230 therefore cannot be even moved to DW_TAG_partial_unit. */
13231 complaint (_("DW_AT_call_parameter offset is not in CU for "
13232 "DW_TAG_call_site child DIE %s [in module %s]"),
13233 sect_offset_str (child_die->sect_off),
13234 objfile_name (objfile));
13235 continue;
13236 }
13237 parameter->u.param_cu_off
13238 = (cu_offset) (sect_off - cu->header.sect_off);
13239 }
13240 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13241 {
13242 complaint (_("No DW_FORM_block* DW_AT_location for "
13243 "DW_TAG_call_site child DIE %s [in module %s]"),
13244 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13245 continue;
13246 }
13247 else
13248 {
13249 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13250 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13251 if (parameter->u.dwarf_reg != -1)
13252 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13253 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13254 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13255 &parameter->u.fb_offset))
13256 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13257 else
13258 {
13259 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13260 "for DW_FORM_block* DW_AT_location is supported for "
13261 "DW_TAG_call_site child DIE %s "
13262 "[in module %s]"),
13263 sect_offset_str (child_die->sect_off),
13264 objfile_name (objfile));
13265 continue;
13266 }
13267 }
13268
13269 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13270 if (attr == NULL)
13271 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13272 if (attr == NULL || !attr->form_is_block ())
13273 {
13274 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13275 "DW_TAG_call_site child DIE %s [in module %s]"),
13276 sect_offset_str (child_die->sect_off),
13277 objfile_name (objfile));
13278 continue;
13279 }
13280 parameter->value = DW_BLOCK (attr)->data;
13281 parameter->value_size = DW_BLOCK (attr)->size;
13282
13283 /* Parameters are not pre-cleared by memset above. */
13284 parameter->data_value = NULL;
13285 parameter->data_value_size = 0;
13286 call_site->parameter_count++;
13287
13288 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13289 if (attr == NULL)
13290 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13291 if (attr != nullptr)
13292 {
13293 if (!attr->form_is_block ())
13294 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13295 "DW_TAG_call_site child DIE %s [in module %s]"),
13296 sect_offset_str (child_die->sect_off),
13297 objfile_name (objfile));
13298 else
13299 {
13300 parameter->data_value = DW_BLOCK (attr)->data;
13301 parameter->data_value_size = DW_BLOCK (attr)->size;
13302 }
13303 }
13304 }
13305 }
13306
13307 /* Helper function for read_variable. If DIE represents a virtual
13308 table, then return the type of the concrete object that is
13309 associated with the virtual table. Otherwise, return NULL. */
13310
13311 static struct type *
13312 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13313 {
13314 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13315 if (attr == NULL)
13316 return NULL;
13317
13318 /* Find the type DIE. */
13319 struct die_info *type_die = NULL;
13320 struct dwarf2_cu *type_cu = cu;
13321
13322 if (attr->form_is_ref ())
13323 type_die = follow_die_ref (die, attr, &type_cu);
13324 if (type_die == NULL)
13325 return NULL;
13326
13327 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13328 return NULL;
13329 return die_containing_type (type_die, type_cu);
13330 }
13331
13332 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13333
13334 static void
13335 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13336 {
13337 struct rust_vtable_symbol *storage = NULL;
13338
13339 if (cu->language == language_rust)
13340 {
13341 struct type *containing_type = rust_containing_type (die, cu);
13342
13343 if (containing_type != NULL)
13344 {
13345 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13346
13347 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13348 initialize_objfile_symbol (storage);
13349 storage->concrete_type = containing_type;
13350 storage->subclass = SYMBOL_RUST_VTABLE;
13351 }
13352 }
13353
13354 struct symbol *res = new_symbol (die, NULL, cu, storage);
13355 struct attribute *abstract_origin
13356 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13357 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13358 if (res == NULL && loc && abstract_origin)
13359 {
13360 /* We have a variable without a name, but with a location and an abstract
13361 origin. This may be a concrete instance of an abstract variable
13362 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13363 later. */
13364 struct dwarf2_cu *origin_cu = cu;
13365 struct die_info *origin_die
13366 = follow_die_ref (die, abstract_origin, &origin_cu);
13367 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13368 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13369 }
13370 }
13371
13372 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13373 reading .debug_rnglists.
13374 Callback's type should be:
13375 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13376 Return true if the attributes are present and valid, otherwise,
13377 return false. */
13378
13379 template <typename Callback>
13380 static bool
13381 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13382 Callback &&callback)
13383 {
13384 struct dwarf2_per_objfile *dwarf2_per_objfile
13385 = cu->per_cu->dwarf2_per_objfile;
13386 struct objfile *objfile = dwarf2_per_objfile->objfile;
13387 bfd *obfd = objfile->obfd;
13388 /* Base address selection entry. */
13389 gdb::optional<CORE_ADDR> base;
13390 const gdb_byte *buffer;
13391 CORE_ADDR baseaddr;
13392 bool overflow = false;
13393
13394 base = cu->base_address;
13395
13396 dwarf2_per_objfile->rnglists.read (objfile);
13397 if (offset >= dwarf2_per_objfile->rnglists.size)
13398 {
13399 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13400 offset);
13401 return false;
13402 }
13403 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13404
13405 baseaddr = objfile->text_section_offset ();
13406
13407 while (1)
13408 {
13409 /* Initialize it due to a false compiler warning. */
13410 CORE_ADDR range_beginning = 0, range_end = 0;
13411 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13412 + dwarf2_per_objfile->rnglists.size);
13413 unsigned int bytes_read;
13414
13415 if (buffer == buf_end)
13416 {
13417 overflow = true;
13418 break;
13419 }
13420 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13421 switch (rlet)
13422 {
13423 case DW_RLE_end_of_list:
13424 break;
13425 case DW_RLE_base_address:
13426 if (buffer + cu->header.addr_size > buf_end)
13427 {
13428 overflow = true;
13429 break;
13430 }
13431 base = cu->header.read_address (obfd, buffer, &bytes_read);
13432 buffer += bytes_read;
13433 break;
13434 case DW_RLE_start_length:
13435 if (buffer + cu->header.addr_size > buf_end)
13436 {
13437 overflow = true;
13438 break;
13439 }
13440 range_beginning = cu->header.read_address (obfd, buffer,
13441 &bytes_read);
13442 buffer += bytes_read;
13443 range_end = (range_beginning
13444 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13445 buffer += bytes_read;
13446 if (buffer > buf_end)
13447 {
13448 overflow = true;
13449 break;
13450 }
13451 break;
13452 case DW_RLE_offset_pair:
13453 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13454 buffer += bytes_read;
13455 if (buffer > buf_end)
13456 {
13457 overflow = true;
13458 break;
13459 }
13460 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13461 buffer += bytes_read;
13462 if (buffer > buf_end)
13463 {
13464 overflow = true;
13465 break;
13466 }
13467 break;
13468 case DW_RLE_start_end:
13469 if (buffer + 2 * cu->header.addr_size > buf_end)
13470 {
13471 overflow = true;
13472 break;
13473 }
13474 range_beginning = cu->header.read_address (obfd, buffer,
13475 &bytes_read);
13476 buffer += bytes_read;
13477 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13478 buffer += bytes_read;
13479 break;
13480 default:
13481 complaint (_("Invalid .debug_rnglists data (no base address)"));
13482 return false;
13483 }
13484 if (rlet == DW_RLE_end_of_list || overflow)
13485 break;
13486 if (rlet == DW_RLE_base_address)
13487 continue;
13488
13489 if (!base.has_value ())
13490 {
13491 /* We have no valid base address for the ranges
13492 data. */
13493 complaint (_("Invalid .debug_rnglists data (no base address)"));
13494 return false;
13495 }
13496
13497 if (range_beginning > range_end)
13498 {
13499 /* Inverted range entries are invalid. */
13500 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13501 return false;
13502 }
13503
13504 /* Empty range entries have no effect. */
13505 if (range_beginning == range_end)
13506 continue;
13507
13508 range_beginning += *base;
13509 range_end += *base;
13510
13511 /* A not-uncommon case of bad debug info.
13512 Don't pollute the addrmap with bad data. */
13513 if (range_beginning + baseaddr == 0
13514 && !dwarf2_per_objfile->has_section_at_zero)
13515 {
13516 complaint (_(".debug_rnglists entry has start address of zero"
13517 " [in module %s]"), objfile_name (objfile));
13518 continue;
13519 }
13520
13521 callback (range_beginning, range_end);
13522 }
13523
13524 if (overflow)
13525 {
13526 complaint (_("Offset %d is not terminated "
13527 "for DW_AT_ranges attribute"),
13528 offset);
13529 return false;
13530 }
13531
13532 return true;
13533 }
13534
13535 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13536 Callback's type should be:
13537 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13538 Return 1 if the attributes are present and valid, otherwise, return 0. */
13539
13540 template <typename Callback>
13541 static int
13542 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13543 Callback &&callback)
13544 {
13545 struct dwarf2_per_objfile *dwarf2_per_objfile
13546 = cu->per_cu->dwarf2_per_objfile;
13547 struct objfile *objfile = dwarf2_per_objfile->objfile;
13548 struct comp_unit_head *cu_header = &cu->header;
13549 bfd *obfd = objfile->obfd;
13550 unsigned int addr_size = cu_header->addr_size;
13551 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13552 /* Base address selection entry. */
13553 gdb::optional<CORE_ADDR> base;
13554 unsigned int dummy;
13555 const gdb_byte *buffer;
13556 CORE_ADDR baseaddr;
13557
13558 if (cu_header->version >= 5)
13559 return dwarf2_rnglists_process (offset, cu, callback);
13560
13561 base = cu->base_address;
13562
13563 dwarf2_per_objfile->ranges.read (objfile);
13564 if (offset >= dwarf2_per_objfile->ranges.size)
13565 {
13566 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13567 offset);
13568 return 0;
13569 }
13570 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13571
13572 baseaddr = objfile->text_section_offset ();
13573
13574 while (1)
13575 {
13576 CORE_ADDR range_beginning, range_end;
13577
13578 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13579 buffer += addr_size;
13580 range_end = cu->header.read_address (obfd, buffer, &dummy);
13581 buffer += addr_size;
13582 offset += 2 * addr_size;
13583
13584 /* An end of list marker is a pair of zero addresses. */
13585 if (range_beginning == 0 && range_end == 0)
13586 /* Found the end of list entry. */
13587 break;
13588
13589 /* Each base address selection entry is a pair of 2 values.
13590 The first is the largest possible address, the second is
13591 the base address. Check for a base address here. */
13592 if ((range_beginning & mask) == mask)
13593 {
13594 /* If we found the largest possible address, then we already
13595 have the base address in range_end. */
13596 base = range_end;
13597 continue;
13598 }
13599
13600 if (!base.has_value ())
13601 {
13602 /* We have no valid base address for the ranges
13603 data. */
13604 complaint (_("Invalid .debug_ranges data (no base address)"));
13605 return 0;
13606 }
13607
13608 if (range_beginning > range_end)
13609 {
13610 /* Inverted range entries are invalid. */
13611 complaint (_("Invalid .debug_ranges data (inverted range)"));
13612 return 0;
13613 }
13614
13615 /* Empty range entries have no effect. */
13616 if (range_beginning == range_end)
13617 continue;
13618
13619 range_beginning += *base;
13620 range_end += *base;
13621
13622 /* A not-uncommon case of bad debug info.
13623 Don't pollute the addrmap with bad data. */
13624 if (range_beginning + baseaddr == 0
13625 && !dwarf2_per_objfile->has_section_at_zero)
13626 {
13627 complaint (_(".debug_ranges entry has start address of zero"
13628 " [in module %s]"), objfile_name (objfile));
13629 continue;
13630 }
13631
13632 callback (range_beginning, range_end);
13633 }
13634
13635 return 1;
13636 }
13637
13638 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13639 Return 1 if the attributes are present and valid, otherwise, return 0.
13640 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13641
13642 static int
13643 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13644 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13645 dwarf2_psymtab *ranges_pst)
13646 {
13647 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13648 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13649 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13650 int low_set = 0;
13651 CORE_ADDR low = 0;
13652 CORE_ADDR high = 0;
13653 int retval;
13654
13655 retval = dwarf2_ranges_process (offset, cu,
13656 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13657 {
13658 if (ranges_pst != NULL)
13659 {
13660 CORE_ADDR lowpc;
13661 CORE_ADDR highpc;
13662
13663 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13664 range_beginning + baseaddr)
13665 - baseaddr);
13666 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13667 range_end + baseaddr)
13668 - baseaddr);
13669 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13670 lowpc, highpc - 1, ranges_pst);
13671 }
13672
13673 /* FIXME: This is recording everything as a low-high
13674 segment of consecutive addresses. We should have a
13675 data structure for discontiguous block ranges
13676 instead. */
13677 if (! low_set)
13678 {
13679 low = range_beginning;
13680 high = range_end;
13681 low_set = 1;
13682 }
13683 else
13684 {
13685 if (range_beginning < low)
13686 low = range_beginning;
13687 if (range_end > high)
13688 high = range_end;
13689 }
13690 });
13691 if (!retval)
13692 return 0;
13693
13694 if (! low_set)
13695 /* If the first entry is an end-of-list marker, the range
13696 describes an empty scope, i.e. no instructions. */
13697 return 0;
13698
13699 if (low_return)
13700 *low_return = low;
13701 if (high_return)
13702 *high_return = high;
13703 return 1;
13704 }
13705
13706 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13707 definition for the return value. *LOWPC and *HIGHPC are set iff
13708 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13709
13710 static enum pc_bounds_kind
13711 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13712 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13713 dwarf2_psymtab *pst)
13714 {
13715 struct dwarf2_per_objfile *dwarf2_per_objfile
13716 = cu->per_cu->dwarf2_per_objfile;
13717 struct attribute *attr;
13718 struct attribute *attr_high;
13719 CORE_ADDR low = 0;
13720 CORE_ADDR high = 0;
13721 enum pc_bounds_kind ret;
13722
13723 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13724 if (attr_high)
13725 {
13726 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13727 if (attr != nullptr)
13728 {
13729 low = attr->value_as_address ();
13730 high = attr_high->value_as_address ();
13731 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13732 high += low;
13733 }
13734 else
13735 /* Found high w/o low attribute. */
13736 return PC_BOUNDS_INVALID;
13737
13738 /* Found consecutive range of addresses. */
13739 ret = PC_BOUNDS_HIGH_LOW;
13740 }
13741 else
13742 {
13743 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13744 if (attr != NULL)
13745 {
13746 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13747 We take advantage of the fact that DW_AT_ranges does not appear
13748 in DW_TAG_compile_unit of DWO files. */
13749 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13750 unsigned int ranges_offset = (DW_UNSND (attr)
13751 + (need_ranges_base
13752 ? cu->ranges_base
13753 : 0));
13754
13755 /* Value of the DW_AT_ranges attribute is the offset in the
13756 .debug_ranges section. */
13757 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13758 return PC_BOUNDS_INVALID;
13759 /* Found discontinuous range of addresses. */
13760 ret = PC_BOUNDS_RANGES;
13761 }
13762 else
13763 return PC_BOUNDS_NOT_PRESENT;
13764 }
13765
13766 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13767 if (high <= low)
13768 return PC_BOUNDS_INVALID;
13769
13770 /* When using the GNU linker, .gnu.linkonce. sections are used to
13771 eliminate duplicate copies of functions and vtables and such.
13772 The linker will arbitrarily choose one and discard the others.
13773 The AT_*_pc values for such functions refer to local labels in
13774 these sections. If the section from that file was discarded, the
13775 labels are not in the output, so the relocs get a value of 0.
13776 If this is a discarded function, mark the pc bounds as invalid,
13777 so that GDB will ignore it. */
13778 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13779 return PC_BOUNDS_INVALID;
13780
13781 *lowpc = low;
13782 if (highpc)
13783 *highpc = high;
13784 return ret;
13785 }
13786
13787 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13788 its low and high PC addresses. Do nothing if these addresses could not
13789 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13790 and HIGHPC to the high address if greater than HIGHPC. */
13791
13792 static void
13793 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13794 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13795 struct dwarf2_cu *cu)
13796 {
13797 CORE_ADDR low, high;
13798 struct die_info *child = die->child;
13799
13800 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13801 {
13802 *lowpc = std::min (*lowpc, low);
13803 *highpc = std::max (*highpc, high);
13804 }
13805
13806 /* If the language does not allow nested subprograms (either inside
13807 subprograms or lexical blocks), we're done. */
13808 if (cu->language != language_ada)
13809 return;
13810
13811 /* Check all the children of the given DIE. If it contains nested
13812 subprograms, then check their pc bounds. Likewise, we need to
13813 check lexical blocks as well, as they may also contain subprogram
13814 definitions. */
13815 while (child && child->tag)
13816 {
13817 if (child->tag == DW_TAG_subprogram
13818 || child->tag == DW_TAG_lexical_block)
13819 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13820 child = child->sibling;
13821 }
13822 }
13823
13824 /* Get the low and high pc's represented by the scope DIE, and store
13825 them in *LOWPC and *HIGHPC. If the correct values can't be
13826 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13827
13828 static void
13829 get_scope_pc_bounds (struct die_info *die,
13830 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13831 struct dwarf2_cu *cu)
13832 {
13833 CORE_ADDR best_low = (CORE_ADDR) -1;
13834 CORE_ADDR best_high = (CORE_ADDR) 0;
13835 CORE_ADDR current_low, current_high;
13836
13837 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13838 >= PC_BOUNDS_RANGES)
13839 {
13840 best_low = current_low;
13841 best_high = current_high;
13842 }
13843 else
13844 {
13845 struct die_info *child = die->child;
13846
13847 while (child && child->tag)
13848 {
13849 switch (child->tag) {
13850 case DW_TAG_subprogram:
13851 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13852 break;
13853 case DW_TAG_namespace:
13854 case DW_TAG_module:
13855 /* FIXME: carlton/2004-01-16: Should we do this for
13856 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13857 that current GCC's always emit the DIEs corresponding
13858 to definitions of methods of classes as children of a
13859 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13860 the DIEs giving the declarations, which could be
13861 anywhere). But I don't see any reason why the
13862 standards says that they have to be there. */
13863 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13864
13865 if (current_low != ((CORE_ADDR) -1))
13866 {
13867 best_low = std::min (best_low, current_low);
13868 best_high = std::max (best_high, current_high);
13869 }
13870 break;
13871 default:
13872 /* Ignore. */
13873 break;
13874 }
13875
13876 child = child->sibling;
13877 }
13878 }
13879
13880 *lowpc = best_low;
13881 *highpc = best_high;
13882 }
13883
13884 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13885 in DIE. */
13886
13887 static void
13888 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13889 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13890 {
13891 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13892 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13893 struct attribute *attr;
13894 struct attribute *attr_high;
13895
13896 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13897 if (attr_high)
13898 {
13899 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13900 if (attr != nullptr)
13901 {
13902 CORE_ADDR low = attr->value_as_address ();
13903 CORE_ADDR high = attr_high->value_as_address ();
13904
13905 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13906 high += low;
13907
13908 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13909 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13910 cu->get_builder ()->record_block_range (block, low, high - 1);
13911 }
13912 }
13913
13914 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13915 if (attr != nullptr)
13916 {
13917 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13918 We take advantage of the fact that DW_AT_ranges does not appear
13919 in DW_TAG_compile_unit of DWO files. */
13920 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13921
13922 /* The value of the DW_AT_ranges attribute is the offset of the
13923 address range list in the .debug_ranges section. */
13924 unsigned long offset = (DW_UNSND (attr)
13925 + (need_ranges_base ? cu->ranges_base : 0));
13926
13927 std::vector<blockrange> blockvec;
13928 dwarf2_ranges_process (offset, cu,
13929 [&] (CORE_ADDR start, CORE_ADDR end)
13930 {
13931 start += baseaddr;
13932 end += baseaddr;
13933 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13934 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13935 cu->get_builder ()->record_block_range (block, start, end - 1);
13936 blockvec.emplace_back (start, end);
13937 });
13938
13939 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
13940 }
13941 }
13942
13943 /* Check whether the producer field indicates either of GCC < 4.6, or the
13944 Intel C/C++ compiler, and cache the result in CU. */
13945
13946 static void
13947 check_producer (struct dwarf2_cu *cu)
13948 {
13949 int major, minor;
13950
13951 if (cu->producer == NULL)
13952 {
13953 /* For unknown compilers expect their behavior is DWARF version
13954 compliant.
13955
13956 GCC started to support .debug_types sections by -gdwarf-4 since
13957 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13958 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13959 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13960 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13961 }
13962 else if (producer_is_gcc (cu->producer, &major, &minor))
13963 {
13964 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13965 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13966 }
13967 else if (producer_is_icc (cu->producer, &major, &minor))
13968 {
13969 cu->producer_is_icc = true;
13970 cu->producer_is_icc_lt_14 = major < 14;
13971 }
13972 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
13973 cu->producer_is_codewarrior = true;
13974 else
13975 {
13976 /* For other non-GCC compilers, expect their behavior is DWARF version
13977 compliant. */
13978 }
13979
13980 cu->checked_producer = true;
13981 }
13982
13983 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13984 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13985 during 4.6.0 experimental. */
13986
13987 static bool
13988 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
13989 {
13990 if (!cu->checked_producer)
13991 check_producer (cu);
13992
13993 return cu->producer_is_gxx_lt_4_6;
13994 }
13995
13996
13997 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
13998 with incorrect is_stmt attributes. */
13999
14000 static bool
14001 producer_is_codewarrior (struct dwarf2_cu *cu)
14002 {
14003 if (!cu->checked_producer)
14004 check_producer (cu);
14005
14006 return cu->producer_is_codewarrior;
14007 }
14008
14009 /* Return the default accessibility type if it is not overridden by
14010 DW_AT_accessibility. */
14011
14012 static enum dwarf_access_attribute
14013 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14014 {
14015 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14016 {
14017 /* The default DWARF 2 accessibility for members is public, the default
14018 accessibility for inheritance is private. */
14019
14020 if (die->tag != DW_TAG_inheritance)
14021 return DW_ACCESS_public;
14022 else
14023 return DW_ACCESS_private;
14024 }
14025 else
14026 {
14027 /* DWARF 3+ defines the default accessibility a different way. The same
14028 rules apply now for DW_TAG_inheritance as for the members and it only
14029 depends on the container kind. */
14030
14031 if (die->parent->tag == DW_TAG_class_type)
14032 return DW_ACCESS_private;
14033 else
14034 return DW_ACCESS_public;
14035 }
14036 }
14037
14038 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14039 offset. If the attribute was not found return 0, otherwise return
14040 1. If it was found but could not properly be handled, set *OFFSET
14041 to 0. */
14042
14043 static int
14044 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14045 LONGEST *offset)
14046 {
14047 struct attribute *attr;
14048
14049 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14050 if (attr != NULL)
14051 {
14052 *offset = 0;
14053
14054 /* Note that we do not check for a section offset first here.
14055 This is because DW_AT_data_member_location is new in DWARF 4,
14056 so if we see it, we can assume that a constant form is really
14057 a constant and not a section offset. */
14058 if (attr->form_is_constant ())
14059 *offset = attr->constant_value (0);
14060 else if (attr->form_is_section_offset ())
14061 dwarf2_complex_location_expr_complaint ();
14062 else if (attr->form_is_block ())
14063 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14064 else
14065 dwarf2_complex_location_expr_complaint ();
14066
14067 return 1;
14068 }
14069
14070 return 0;
14071 }
14072
14073 /* Add an aggregate field to the field list. */
14074
14075 static void
14076 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14077 struct dwarf2_cu *cu)
14078 {
14079 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14080 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14081 struct nextfield *new_field;
14082 struct attribute *attr;
14083 struct field *fp;
14084 const char *fieldname = "";
14085
14086 if (die->tag == DW_TAG_inheritance)
14087 {
14088 fip->baseclasses.emplace_back ();
14089 new_field = &fip->baseclasses.back ();
14090 }
14091 else
14092 {
14093 fip->fields.emplace_back ();
14094 new_field = &fip->fields.back ();
14095 }
14096
14097 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14098 if (attr != nullptr)
14099 new_field->accessibility = DW_UNSND (attr);
14100 else
14101 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14102 if (new_field->accessibility != DW_ACCESS_public)
14103 fip->non_public_fields = 1;
14104
14105 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14106 if (attr != nullptr)
14107 new_field->virtuality = DW_UNSND (attr);
14108 else
14109 new_field->virtuality = DW_VIRTUALITY_none;
14110
14111 fp = &new_field->field;
14112
14113 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14114 {
14115 LONGEST offset;
14116
14117 /* Data member other than a C++ static data member. */
14118
14119 /* Get type of field. */
14120 fp->type = die_type (die, cu);
14121
14122 SET_FIELD_BITPOS (*fp, 0);
14123
14124 /* Get bit size of field (zero if none). */
14125 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14126 if (attr != nullptr)
14127 {
14128 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14129 }
14130 else
14131 {
14132 FIELD_BITSIZE (*fp) = 0;
14133 }
14134
14135 /* Get bit offset of field. */
14136 if (handle_data_member_location (die, cu, &offset))
14137 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14138 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14139 if (attr != nullptr)
14140 {
14141 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14142 {
14143 /* For big endian bits, the DW_AT_bit_offset gives the
14144 additional bit offset from the MSB of the containing
14145 anonymous object to the MSB of the field. We don't
14146 have to do anything special since we don't need to
14147 know the size of the anonymous object. */
14148 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14149 }
14150 else
14151 {
14152 /* For little endian bits, compute the bit offset to the
14153 MSB of the anonymous object, subtract off the number of
14154 bits from the MSB of the field to the MSB of the
14155 object, and then subtract off the number of bits of
14156 the field itself. The result is the bit offset of
14157 the LSB of the field. */
14158 int anonymous_size;
14159 int bit_offset = DW_UNSND (attr);
14160
14161 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14162 if (attr != nullptr)
14163 {
14164 /* The size of the anonymous object containing
14165 the bit field is explicit, so use the
14166 indicated size (in bytes). */
14167 anonymous_size = DW_UNSND (attr);
14168 }
14169 else
14170 {
14171 /* The size of the anonymous object containing
14172 the bit field must be inferred from the type
14173 attribute of the data member containing the
14174 bit field. */
14175 anonymous_size = TYPE_LENGTH (fp->type);
14176 }
14177 SET_FIELD_BITPOS (*fp,
14178 (FIELD_BITPOS (*fp)
14179 + anonymous_size * bits_per_byte
14180 - bit_offset - FIELD_BITSIZE (*fp)));
14181 }
14182 }
14183 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14184 if (attr != NULL)
14185 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14186 + attr->constant_value (0)));
14187
14188 /* Get name of field. */
14189 fieldname = dwarf2_name (die, cu);
14190 if (fieldname == NULL)
14191 fieldname = "";
14192
14193 /* The name is already allocated along with this objfile, so we don't
14194 need to duplicate it for the type. */
14195 fp->name = fieldname;
14196
14197 /* Change accessibility for artificial fields (e.g. virtual table
14198 pointer or virtual base class pointer) to private. */
14199 if (dwarf2_attr (die, DW_AT_artificial, cu))
14200 {
14201 FIELD_ARTIFICIAL (*fp) = 1;
14202 new_field->accessibility = DW_ACCESS_private;
14203 fip->non_public_fields = 1;
14204 }
14205 }
14206 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14207 {
14208 /* C++ static member. */
14209
14210 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14211 is a declaration, but all versions of G++ as of this writing
14212 (so through at least 3.2.1) incorrectly generate
14213 DW_TAG_variable tags. */
14214
14215 const char *physname;
14216
14217 /* Get name of field. */
14218 fieldname = dwarf2_name (die, cu);
14219 if (fieldname == NULL)
14220 return;
14221
14222 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14223 if (attr
14224 /* Only create a symbol if this is an external value.
14225 new_symbol checks this and puts the value in the global symbol
14226 table, which we want. If it is not external, new_symbol
14227 will try to put the value in cu->list_in_scope which is wrong. */
14228 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14229 {
14230 /* A static const member, not much different than an enum as far as
14231 we're concerned, except that we can support more types. */
14232 new_symbol (die, NULL, cu);
14233 }
14234
14235 /* Get physical name. */
14236 physname = dwarf2_physname (fieldname, die, cu);
14237
14238 /* The name is already allocated along with this objfile, so we don't
14239 need to duplicate it for the type. */
14240 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14241 FIELD_TYPE (*fp) = die_type (die, cu);
14242 FIELD_NAME (*fp) = fieldname;
14243 }
14244 else if (die->tag == DW_TAG_inheritance)
14245 {
14246 LONGEST offset;
14247
14248 /* C++ base class field. */
14249 if (handle_data_member_location (die, cu, &offset))
14250 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14251 FIELD_BITSIZE (*fp) = 0;
14252 FIELD_TYPE (*fp) = die_type (die, cu);
14253 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14254 }
14255 else if (die->tag == DW_TAG_variant_part)
14256 {
14257 /* process_structure_scope will treat this DIE as a union. */
14258 process_structure_scope (die, cu);
14259
14260 /* The variant part is relative to the start of the enclosing
14261 structure. */
14262 SET_FIELD_BITPOS (*fp, 0);
14263 fp->type = get_die_type (die, cu);
14264 fp->artificial = 1;
14265 fp->name = "<<variant>>";
14266
14267 /* Normally a DW_TAG_variant_part won't have a size, but our
14268 representation requires one, so set it to the maximum of the
14269 child sizes, being sure to account for the offset at which
14270 each child is seen. */
14271 if (TYPE_LENGTH (fp->type) == 0)
14272 {
14273 unsigned max = 0;
14274 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14275 {
14276 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14277 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14278 if (len > max)
14279 max = len;
14280 }
14281 TYPE_LENGTH (fp->type) = max;
14282 }
14283 }
14284 else
14285 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14286 }
14287
14288 /* Can the type given by DIE define another type? */
14289
14290 static bool
14291 type_can_define_types (const struct die_info *die)
14292 {
14293 switch (die->tag)
14294 {
14295 case DW_TAG_typedef:
14296 case DW_TAG_class_type:
14297 case DW_TAG_structure_type:
14298 case DW_TAG_union_type:
14299 case DW_TAG_enumeration_type:
14300 return true;
14301
14302 default:
14303 return false;
14304 }
14305 }
14306
14307 /* Add a type definition defined in the scope of the FIP's class. */
14308
14309 static void
14310 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14311 struct dwarf2_cu *cu)
14312 {
14313 struct decl_field fp;
14314 memset (&fp, 0, sizeof (fp));
14315
14316 gdb_assert (type_can_define_types (die));
14317
14318 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14319 fp.name = dwarf2_name (die, cu);
14320 fp.type = read_type_die (die, cu);
14321
14322 /* Save accessibility. */
14323 enum dwarf_access_attribute accessibility;
14324 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14325 if (attr != NULL)
14326 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14327 else
14328 accessibility = dwarf2_default_access_attribute (die, cu);
14329 switch (accessibility)
14330 {
14331 case DW_ACCESS_public:
14332 /* The assumed value if neither private nor protected. */
14333 break;
14334 case DW_ACCESS_private:
14335 fp.is_private = 1;
14336 break;
14337 case DW_ACCESS_protected:
14338 fp.is_protected = 1;
14339 break;
14340 default:
14341 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14342 }
14343
14344 if (die->tag == DW_TAG_typedef)
14345 fip->typedef_field_list.push_back (fp);
14346 else
14347 fip->nested_types_list.push_back (fp);
14348 }
14349
14350 /* Create the vector of fields, and attach it to the type. */
14351
14352 static void
14353 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14354 struct dwarf2_cu *cu)
14355 {
14356 int nfields = fip->nfields ();
14357
14358 /* Record the field count, allocate space for the array of fields,
14359 and create blank accessibility bitfields if necessary. */
14360 TYPE_NFIELDS (type) = nfields;
14361 TYPE_FIELDS (type) = (struct field *)
14362 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14363
14364 if (fip->non_public_fields && cu->language != language_ada)
14365 {
14366 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14367
14368 TYPE_FIELD_PRIVATE_BITS (type) =
14369 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14370 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14371
14372 TYPE_FIELD_PROTECTED_BITS (type) =
14373 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14374 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14375
14376 TYPE_FIELD_IGNORE_BITS (type) =
14377 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14378 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14379 }
14380
14381 /* If the type has baseclasses, allocate and clear a bit vector for
14382 TYPE_FIELD_VIRTUAL_BITS. */
14383 if (!fip->baseclasses.empty () && cu->language != language_ada)
14384 {
14385 int num_bytes = B_BYTES (fip->baseclasses.size ());
14386 unsigned char *pointer;
14387
14388 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14389 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14390 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14391 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14392 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14393 }
14394
14395 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14396 {
14397 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14398
14399 for (int index = 0; index < nfields; ++index)
14400 {
14401 struct nextfield &field = fip->fields[index];
14402
14403 if (field.variant.is_discriminant)
14404 di->discriminant_index = index;
14405 else if (field.variant.default_branch)
14406 di->default_index = index;
14407 else
14408 di->discriminants[index] = field.variant.discriminant_value;
14409 }
14410 }
14411
14412 /* Copy the saved-up fields into the field vector. */
14413 for (int i = 0; i < nfields; ++i)
14414 {
14415 struct nextfield &field
14416 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14417 : fip->fields[i - fip->baseclasses.size ()]);
14418
14419 TYPE_FIELD (type, i) = field.field;
14420 switch (field.accessibility)
14421 {
14422 case DW_ACCESS_private:
14423 if (cu->language != language_ada)
14424 SET_TYPE_FIELD_PRIVATE (type, i);
14425 break;
14426
14427 case DW_ACCESS_protected:
14428 if (cu->language != language_ada)
14429 SET_TYPE_FIELD_PROTECTED (type, i);
14430 break;
14431
14432 case DW_ACCESS_public:
14433 break;
14434
14435 default:
14436 /* Unknown accessibility. Complain and treat it as public. */
14437 {
14438 complaint (_("unsupported accessibility %d"),
14439 field.accessibility);
14440 }
14441 break;
14442 }
14443 if (i < fip->baseclasses.size ())
14444 {
14445 switch (field.virtuality)
14446 {
14447 case DW_VIRTUALITY_virtual:
14448 case DW_VIRTUALITY_pure_virtual:
14449 if (cu->language == language_ada)
14450 error (_("unexpected virtuality in component of Ada type"));
14451 SET_TYPE_FIELD_VIRTUAL (type, i);
14452 break;
14453 }
14454 }
14455 }
14456 }
14457
14458 /* Return true if this member function is a constructor, false
14459 otherwise. */
14460
14461 static int
14462 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14463 {
14464 const char *fieldname;
14465 const char *type_name;
14466 int len;
14467
14468 if (die->parent == NULL)
14469 return 0;
14470
14471 if (die->parent->tag != DW_TAG_structure_type
14472 && die->parent->tag != DW_TAG_union_type
14473 && die->parent->tag != DW_TAG_class_type)
14474 return 0;
14475
14476 fieldname = dwarf2_name (die, cu);
14477 type_name = dwarf2_name (die->parent, cu);
14478 if (fieldname == NULL || type_name == NULL)
14479 return 0;
14480
14481 len = strlen (fieldname);
14482 return (strncmp (fieldname, type_name, len) == 0
14483 && (type_name[len] == '\0' || type_name[len] == '<'));
14484 }
14485
14486 /* Check if the given VALUE is a recognized enum
14487 dwarf_defaulted_attribute constant according to DWARF5 spec,
14488 Table 7.24. */
14489
14490 static bool
14491 is_valid_DW_AT_defaulted (ULONGEST value)
14492 {
14493 switch (value)
14494 {
14495 case DW_DEFAULTED_no:
14496 case DW_DEFAULTED_in_class:
14497 case DW_DEFAULTED_out_of_class:
14498 return true;
14499 }
14500
14501 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14502 return false;
14503 }
14504
14505 /* Add a member function to the proper fieldlist. */
14506
14507 static void
14508 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14509 struct type *type, struct dwarf2_cu *cu)
14510 {
14511 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14512 struct attribute *attr;
14513 int i;
14514 struct fnfieldlist *flp = nullptr;
14515 struct fn_field *fnp;
14516 const char *fieldname;
14517 struct type *this_type;
14518 enum dwarf_access_attribute accessibility;
14519
14520 if (cu->language == language_ada)
14521 error (_("unexpected member function in Ada type"));
14522
14523 /* Get name of member function. */
14524 fieldname = dwarf2_name (die, cu);
14525 if (fieldname == NULL)
14526 return;
14527
14528 /* Look up member function name in fieldlist. */
14529 for (i = 0; i < fip->fnfieldlists.size (); i++)
14530 {
14531 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14532 {
14533 flp = &fip->fnfieldlists[i];
14534 break;
14535 }
14536 }
14537
14538 /* Create a new fnfieldlist if necessary. */
14539 if (flp == nullptr)
14540 {
14541 fip->fnfieldlists.emplace_back ();
14542 flp = &fip->fnfieldlists.back ();
14543 flp->name = fieldname;
14544 i = fip->fnfieldlists.size () - 1;
14545 }
14546
14547 /* Create a new member function field and add it to the vector of
14548 fnfieldlists. */
14549 flp->fnfields.emplace_back ();
14550 fnp = &flp->fnfields.back ();
14551
14552 /* Delay processing of the physname until later. */
14553 if (cu->language == language_cplus)
14554 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14555 die, cu);
14556 else
14557 {
14558 const char *physname = dwarf2_physname (fieldname, die, cu);
14559 fnp->physname = physname ? physname : "";
14560 }
14561
14562 fnp->type = alloc_type (objfile);
14563 this_type = read_type_die (die, cu);
14564 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14565 {
14566 int nparams = TYPE_NFIELDS (this_type);
14567
14568 /* TYPE is the domain of this method, and THIS_TYPE is the type
14569 of the method itself (TYPE_CODE_METHOD). */
14570 smash_to_method_type (fnp->type, type,
14571 TYPE_TARGET_TYPE (this_type),
14572 TYPE_FIELDS (this_type),
14573 TYPE_NFIELDS (this_type),
14574 TYPE_VARARGS (this_type));
14575
14576 /* Handle static member functions.
14577 Dwarf2 has no clean way to discern C++ static and non-static
14578 member functions. G++ helps GDB by marking the first
14579 parameter for non-static member functions (which is the this
14580 pointer) as artificial. We obtain this information from
14581 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14582 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14583 fnp->voffset = VOFFSET_STATIC;
14584 }
14585 else
14586 complaint (_("member function type missing for '%s'"),
14587 dwarf2_full_name (fieldname, die, cu));
14588
14589 /* Get fcontext from DW_AT_containing_type if present. */
14590 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14591 fnp->fcontext = die_containing_type (die, cu);
14592
14593 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14594 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14595
14596 /* Get accessibility. */
14597 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14598 if (attr != nullptr)
14599 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14600 else
14601 accessibility = dwarf2_default_access_attribute (die, cu);
14602 switch (accessibility)
14603 {
14604 case DW_ACCESS_private:
14605 fnp->is_private = 1;
14606 break;
14607 case DW_ACCESS_protected:
14608 fnp->is_protected = 1;
14609 break;
14610 }
14611
14612 /* Check for artificial methods. */
14613 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14614 if (attr && DW_UNSND (attr) != 0)
14615 fnp->is_artificial = 1;
14616
14617 /* Check for defaulted methods. */
14618 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14619 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14620 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14621
14622 /* Check for deleted methods. */
14623 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14624 if (attr != nullptr && DW_UNSND (attr) != 0)
14625 fnp->is_deleted = 1;
14626
14627 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14628
14629 /* Get index in virtual function table if it is a virtual member
14630 function. For older versions of GCC, this is an offset in the
14631 appropriate virtual table, as specified by DW_AT_containing_type.
14632 For everyone else, it is an expression to be evaluated relative
14633 to the object address. */
14634
14635 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14636 if (attr != nullptr)
14637 {
14638 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14639 {
14640 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14641 {
14642 /* Old-style GCC. */
14643 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14644 }
14645 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14646 || (DW_BLOCK (attr)->size > 1
14647 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14648 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14649 {
14650 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14651 if ((fnp->voffset % cu->header.addr_size) != 0)
14652 dwarf2_complex_location_expr_complaint ();
14653 else
14654 fnp->voffset /= cu->header.addr_size;
14655 fnp->voffset += 2;
14656 }
14657 else
14658 dwarf2_complex_location_expr_complaint ();
14659
14660 if (!fnp->fcontext)
14661 {
14662 /* If there is no `this' field and no DW_AT_containing_type,
14663 we cannot actually find a base class context for the
14664 vtable! */
14665 if (TYPE_NFIELDS (this_type) == 0
14666 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14667 {
14668 complaint (_("cannot determine context for virtual member "
14669 "function \"%s\" (offset %s)"),
14670 fieldname, sect_offset_str (die->sect_off));
14671 }
14672 else
14673 {
14674 fnp->fcontext
14675 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14676 }
14677 }
14678 }
14679 else if (attr->form_is_section_offset ())
14680 {
14681 dwarf2_complex_location_expr_complaint ();
14682 }
14683 else
14684 {
14685 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14686 fieldname);
14687 }
14688 }
14689 else
14690 {
14691 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14692 if (attr && DW_UNSND (attr))
14693 {
14694 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14695 complaint (_("Member function \"%s\" (offset %s) is virtual "
14696 "but the vtable offset is not specified"),
14697 fieldname, sect_offset_str (die->sect_off));
14698 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14699 TYPE_CPLUS_DYNAMIC (type) = 1;
14700 }
14701 }
14702 }
14703
14704 /* Create the vector of member function fields, and attach it to the type. */
14705
14706 static void
14707 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14708 struct dwarf2_cu *cu)
14709 {
14710 if (cu->language == language_ada)
14711 error (_("unexpected member functions in Ada type"));
14712
14713 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14714 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14715 TYPE_ALLOC (type,
14716 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14717
14718 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14719 {
14720 struct fnfieldlist &nf = fip->fnfieldlists[i];
14721 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14722
14723 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14724 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14725 fn_flp->fn_fields = (struct fn_field *)
14726 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14727
14728 for (int k = 0; k < nf.fnfields.size (); ++k)
14729 fn_flp->fn_fields[k] = nf.fnfields[k];
14730 }
14731
14732 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14733 }
14734
14735 /* Returns non-zero if NAME is the name of a vtable member in CU's
14736 language, zero otherwise. */
14737 static int
14738 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14739 {
14740 static const char vptr[] = "_vptr";
14741
14742 /* Look for the C++ form of the vtable. */
14743 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14744 return 1;
14745
14746 return 0;
14747 }
14748
14749 /* GCC outputs unnamed structures that are really pointers to member
14750 functions, with the ABI-specified layout. If TYPE describes
14751 such a structure, smash it into a member function type.
14752
14753 GCC shouldn't do this; it should just output pointer to member DIEs.
14754 This is GCC PR debug/28767. */
14755
14756 static void
14757 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14758 {
14759 struct type *pfn_type, *self_type, *new_type;
14760
14761 /* Check for a structure with no name and two children. */
14762 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14763 return;
14764
14765 /* Check for __pfn and __delta members. */
14766 if (TYPE_FIELD_NAME (type, 0) == NULL
14767 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14768 || TYPE_FIELD_NAME (type, 1) == NULL
14769 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14770 return;
14771
14772 /* Find the type of the method. */
14773 pfn_type = TYPE_FIELD_TYPE (type, 0);
14774 if (pfn_type == NULL
14775 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14776 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14777 return;
14778
14779 /* Look for the "this" argument. */
14780 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14781 if (TYPE_NFIELDS (pfn_type) == 0
14782 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14783 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14784 return;
14785
14786 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14787 new_type = alloc_type (objfile);
14788 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14789 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14790 TYPE_VARARGS (pfn_type));
14791 smash_to_methodptr_type (type, new_type);
14792 }
14793
14794 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14795 appropriate error checking and issuing complaints if there is a
14796 problem. */
14797
14798 static ULONGEST
14799 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14800 {
14801 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14802
14803 if (attr == nullptr)
14804 return 0;
14805
14806 if (!attr->form_is_constant ())
14807 {
14808 complaint (_("DW_AT_alignment must have constant form"
14809 " - DIE at %s [in module %s]"),
14810 sect_offset_str (die->sect_off),
14811 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14812 return 0;
14813 }
14814
14815 ULONGEST align;
14816 if (attr->form == DW_FORM_sdata)
14817 {
14818 LONGEST val = DW_SND (attr);
14819 if (val < 0)
14820 {
14821 complaint (_("DW_AT_alignment value must not be negative"
14822 " - DIE at %s [in module %s]"),
14823 sect_offset_str (die->sect_off),
14824 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14825 return 0;
14826 }
14827 align = val;
14828 }
14829 else
14830 align = DW_UNSND (attr);
14831
14832 if (align == 0)
14833 {
14834 complaint (_("DW_AT_alignment value must not be zero"
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 if ((align & (align - 1)) != 0)
14841 {
14842 complaint (_("DW_AT_alignment value must be a power of 2"
14843 " - DIE at %s [in module %s]"),
14844 sect_offset_str (die->sect_off),
14845 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14846 return 0;
14847 }
14848
14849 return align;
14850 }
14851
14852 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14853 the alignment for TYPE. */
14854
14855 static void
14856 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14857 struct type *type)
14858 {
14859 if (!set_type_align (type, get_alignment (cu, die)))
14860 complaint (_("DW_AT_alignment value too large"
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 }
14865
14866 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14867 constant for a type, according to DWARF5 spec, Table 5.5. */
14868
14869 static bool
14870 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14871 {
14872 switch (value)
14873 {
14874 case DW_CC_normal:
14875 case DW_CC_pass_by_reference:
14876 case DW_CC_pass_by_value:
14877 return true;
14878
14879 default:
14880 complaint (_("unrecognized DW_AT_calling_convention value "
14881 "(%s) for a type"), pulongest (value));
14882 return false;
14883 }
14884 }
14885
14886 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14887 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14888 also according to GNU-specific values (see include/dwarf2.h). */
14889
14890 static bool
14891 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14892 {
14893 switch (value)
14894 {
14895 case DW_CC_normal:
14896 case DW_CC_program:
14897 case DW_CC_nocall:
14898 return true;
14899
14900 case DW_CC_GNU_renesas_sh:
14901 case DW_CC_GNU_borland_fastcall_i386:
14902 case DW_CC_GDB_IBM_OpenCL:
14903 return true;
14904
14905 default:
14906 complaint (_("unrecognized DW_AT_calling_convention value "
14907 "(%s) for a subroutine"), pulongest (value));
14908 return false;
14909 }
14910 }
14911
14912 /* Called when we find the DIE that starts a structure or union scope
14913 (definition) to create a type for the structure or union. Fill in
14914 the type's name and general properties; the members will not be
14915 processed until process_structure_scope. A symbol table entry for
14916 the type will also not be done until process_structure_scope (assuming
14917 the type has a name).
14918
14919 NOTE: we need to call these functions regardless of whether or not the
14920 DIE has a DW_AT_name attribute, since it might be an anonymous
14921 structure or union. This gets the type entered into our set of
14922 user defined types. */
14923
14924 static struct type *
14925 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14926 {
14927 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14928 struct type *type;
14929 struct attribute *attr;
14930 const char *name;
14931
14932 /* If the definition of this type lives in .debug_types, read that type.
14933 Don't follow DW_AT_specification though, that will take us back up
14934 the chain and we want to go down. */
14935 attr = die->attr (DW_AT_signature);
14936 if (attr != nullptr)
14937 {
14938 type = get_DW_AT_signature_type (die, attr, cu);
14939
14940 /* The type's CU may not be the same as CU.
14941 Ensure TYPE is recorded with CU in die_type_hash. */
14942 return set_die_type (die, type, cu);
14943 }
14944
14945 type = alloc_type (objfile);
14946 INIT_CPLUS_SPECIFIC (type);
14947
14948 name = dwarf2_name (die, cu);
14949 if (name != NULL)
14950 {
14951 if (cu->language == language_cplus
14952 || cu->language == language_d
14953 || cu->language == language_rust)
14954 {
14955 const char *full_name = dwarf2_full_name (name, die, cu);
14956
14957 /* dwarf2_full_name might have already finished building the DIE's
14958 type. If so, there is no need to continue. */
14959 if (get_die_type (die, cu) != NULL)
14960 return get_die_type (die, cu);
14961
14962 TYPE_NAME (type) = full_name;
14963 }
14964 else
14965 {
14966 /* The name is already allocated along with this objfile, so
14967 we don't need to duplicate it for the type. */
14968 TYPE_NAME (type) = name;
14969 }
14970 }
14971
14972 if (die->tag == DW_TAG_structure_type)
14973 {
14974 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14975 }
14976 else if (die->tag == DW_TAG_union_type)
14977 {
14978 TYPE_CODE (type) = TYPE_CODE_UNION;
14979 }
14980 else if (die->tag == DW_TAG_variant_part)
14981 {
14982 TYPE_CODE (type) = TYPE_CODE_UNION;
14983 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
14984 }
14985 else
14986 {
14987 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14988 }
14989
14990 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
14991 TYPE_DECLARED_CLASS (type) = 1;
14992
14993 /* Store the calling convention in the type if it's available in
14994 the die. Otherwise the calling convention remains set to
14995 the default value DW_CC_normal. */
14996 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
14997 if (attr != nullptr
14998 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
14999 {
15000 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15001 TYPE_CPLUS_CALLING_CONVENTION (type)
15002 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15003 }
15004
15005 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15006 if (attr != nullptr)
15007 {
15008 if (attr->form_is_constant ())
15009 TYPE_LENGTH (type) = DW_UNSND (attr);
15010 else
15011 {
15012 /* For the moment, dynamic type sizes are not supported
15013 by GDB's struct type. The actual size is determined
15014 on-demand when resolving the type of a given object,
15015 so set the type's length to zero for now. Otherwise,
15016 we record an expression as the length, and that expression
15017 could lead to a very large value, which could eventually
15018 lead to us trying to allocate that much memory when creating
15019 a value of that type. */
15020 TYPE_LENGTH (type) = 0;
15021 }
15022 }
15023 else
15024 {
15025 TYPE_LENGTH (type) = 0;
15026 }
15027
15028 maybe_set_alignment (cu, die, type);
15029
15030 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15031 {
15032 /* ICC<14 does not output the required DW_AT_declaration on
15033 incomplete types, but gives them a size of zero. */
15034 TYPE_STUB (type) = 1;
15035 }
15036 else
15037 TYPE_STUB_SUPPORTED (type) = 1;
15038
15039 if (die_is_declaration (die, cu))
15040 TYPE_STUB (type) = 1;
15041 else if (attr == NULL && die->child == NULL
15042 && producer_is_realview (cu->producer))
15043 /* RealView does not output the required DW_AT_declaration
15044 on incomplete types. */
15045 TYPE_STUB (type) = 1;
15046
15047 /* We need to add the type field to the die immediately so we don't
15048 infinitely recurse when dealing with pointers to the structure
15049 type within the structure itself. */
15050 set_die_type (die, type, cu);
15051
15052 /* set_die_type should be already done. */
15053 set_descriptive_type (type, die, cu);
15054
15055 return type;
15056 }
15057
15058 /* A helper for process_structure_scope that handles a single member
15059 DIE. */
15060
15061 static void
15062 handle_struct_member_die (struct die_info *child_die, struct type *type,
15063 struct field_info *fi,
15064 std::vector<struct symbol *> *template_args,
15065 struct dwarf2_cu *cu)
15066 {
15067 if (child_die->tag == DW_TAG_member
15068 || child_die->tag == DW_TAG_variable
15069 || child_die->tag == DW_TAG_variant_part)
15070 {
15071 /* NOTE: carlton/2002-11-05: A C++ static data member
15072 should be a DW_TAG_member that is a declaration, but
15073 all versions of G++ as of this writing (so through at
15074 least 3.2.1) incorrectly generate DW_TAG_variable
15075 tags for them instead. */
15076 dwarf2_add_field (fi, child_die, cu);
15077 }
15078 else if (child_die->tag == DW_TAG_subprogram)
15079 {
15080 /* Rust doesn't have member functions in the C++ sense.
15081 However, it does emit ordinary functions as children
15082 of a struct DIE. */
15083 if (cu->language == language_rust)
15084 read_func_scope (child_die, cu);
15085 else
15086 {
15087 /* C++ member function. */
15088 dwarf2_add_member_fn (fi, child_die, type, cu);
15089 }
15090 }
15091 else if (child_die->tag == DW_TAG_inheritance)
15092 {
15093 /* C++ base class field. */
15094 dwarf2_add_field (fi, child_die, cu);
15095 }
15096 else if (type_can_define_types (child_die))
15097 dwarf2_add_type_defn (fi, child_die, cu);
15098 else if (child_die->tag == DW_TAG_template_type_param
15099 || child_die->tag == DW_TAG_template_value_param)
15100 {
15101 struct symbol *arg = new_symbol (child_die, NULL, cu);
15102
15103 if (arg != NULL)
15104 template_args->push_back (arg);
15105 }
15106 else if (child_die->tag == DW_TAG_variant)
15107 {
15108 /* In a variant we want to get the discriminant and also add a
15109 field for our sole member child. */
15110 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15111
15112 for (die_info *variant_child = child_die->child;
15113 variant_child != NULL;
15114 variant_child = variant_child->sibling)
15115 {
15116 if (variant_child->tag == DW_TAG_member)
15117 {
15118 handle_struct_member_die (variant_child, type, fi,
15119 template_args, cu);
15120 /* Only handle the one. */
15121 break;
15122 }
15123 }
15124
15125 /* We don't handle this but we might as well report it if we see
15126 it. */
15127 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15128 complaint (_("DW_AT_discr_list is not supported yet"
15129 " - DIE at %s [in module %s]"),
15130 sect_offset_str (child_die->sect_off),
15131 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15132
15133 /* The first field was just added, so we can stash the
15134 discriminant there. */
15135 gdb_assert (!fi->fields.empty ());
15136 if (discr == NULL)
15137 fi->fields.back ().variant.default_branch = true;
15138 else
15139 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15140 }
15141 }
15142
15143 /* Finish creating a structure or union type, including filling in
15144 its members and creating a symbol for it. */
15145
15146 static void
15147 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15148 {
15149 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15150 struct die_info *child_die;
15151 struct type *type;
15152
15153 type = get_die_type (die, cu);
15154 if (type == NULL)
15155 type = read_structure_type (die, cu);
15156
15157 /* When reading a DW_TAG_variant_part, we need to notice when we
15158 read the discriminant member, so we can record it later in the
15159 discriminant_info. */
15160 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15161 sect_offset discr_offset {};
15162 bool has_template_parameters = false;
15163
15164 if (is_variant_part)
15165 {
15166 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15167 if (discr == NULL)
15168 {
15169 /* Maybe it's a univariant form, an extension we support.
15170 In this case arrange not to check the offset. */
15171 is_variant_part = false;
15172 }
15173 else if (discr->form_is_ref ())
15174 {
15175 struct dwarf2_cu *target_cu = cu;
15176 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15177
15178 discr_offset = target_die->sect_off;
15179 }
15180 else
15181 {
15182 complaint (_("DW_AT_discr does not have DIE reference form"
15183 " - DIE at %s [in module %s]"),
15184 sect_offset_str (die->sect_off),
15185 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15186 is_variant_part = false;
15187 }
15188 }
15189
15190 if (die->child != NULL && ! die_is_declaration (die, cu))
15191 {
15192 struct field_info fi;
15193 std::vector<struct symbol *> template_args;
15194
15195 child_die = die->child;
15196
15197 while (child_die && child_die->tag)
15198 {
15199 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15200
15201 if (is_variant_part && discr_offset == child_die->sect_off)
15202 fi.fields.back ().variant.is_discriminant = true;
15203
15204 child_die = child_die->sibling;
15205 }
15206
15207 /* Attach template arguments to type. */
15208 if (!template_args.empty ())
15209 {
15210 has_template_parameters = true;
15211 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15212 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15213 TYPE_TEMPLATE_ARGUMENTS (type)
15214 = XOBNEWVEC (&objfile->objfile_obstack,
15215 struct symbol *,
15216 TYPE_N_TEMPLATE_ARGUMENTS (type));
15217 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15218 template_args.data (),
15219 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15220 * sizeof (struct symbol *)));
15221 }
15222
15223 /* Attach fields and member functions to the type. */
15224 if (fi.nfields () > 0)
15225 dwarf2_attach_fields_to_type (&fi, type, cu);
15226 if (!fi.fnfieldlists.empty ())
15227 {
15228 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15229
15230 /* Get the type which refers to the base class (possibly this
15231 class itself) which contains the vtable pointer for the current
15232 class from the DW_AT_containing_type attribute. This use of
15233 DW_AT_containing_type is a GNU extension. */
15234
15235 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15236 {
15237 struct type *t = die_containing_type (die, cu);
15238
15239 set_type_vptr_basetype (type, t);
15240 if (type == t)
15241 {
15242 int i;
15243
15244 /* Our own class provides vtbl ptr. */
15245 for (i = TYPE_NFIELDS (t) - 1;
15246 i >= TYPE_N_BASECLASSES (t);
15247 --i)
15248 {
15249 const char *fieldname = TYPE_FIELD_NAME (t, i);
15250
15251 if (is_vtable_name (fieldname, cu))
15252 {
15253 set_type_vptr_fieldno (type, i);
15254 break;
15255 }
15256 }
15257
15258 /* Complain if virtual function table field not found. */
15259 if (i < TYPE_N_BASECLASSES (t))
15260 complaint (_("virtual function table pointer "
15261 "not found when defining class '%s'"),
15262 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15263 }
15264 else
15265 {
15266 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15267 }
15268 }
15269 else if (cu->producer
15270 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15271 {
15272 /* The IBM XLC compiler does not provide direct indication
15273 of the containing type, but the vtable pointer is
15274 always named __vfp. */
15275
15276 int i;
15277
15278 for (i = TYPE_NFIELDS (type) - 1;
15279 i >= TYPE_N_BASECLASSES (type);
15280 --i)
15281 {
15282 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15283 {
15284 set_type_vptr_fieldno (type, i);
15285 set_type_vptr_basetype (type, type);
15286 break;
15287 }
15288 }
15289 }
15290 }
15291
15292 /* Copy fi.typedef_field_list linked list elements content into the
15293 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15294 if (!fi.typedef_field_list.empty ())
15295 {
15296 int count = fi.typedef_field_list.size ();
15297
15298 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15299 TYPE_TYPEDEF_FIELD_ARRAY (type)
15300 = ((struct decl_field *)
15301 TYPE_ALLOC (type,
15302 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15303 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15304
15305 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15306 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15307 }
15308
15309 /* Copy fi.nested_types_list linked list elements content into the
15310 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15311 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15312 {
15313 int count = fi.nested_types_list.size ();
15314
15315 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15316 TYPE_NESTED_TYPES_ARRAY (type)
15317 = ((struct decl_field *)
15318 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15319 TYPE_NESTED_TYPES_COUNT (type) = count;
15320
15321 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15322 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15323 }
15324 }
15325
15326 quirk_gcc_member_function_pointer (type, objfile);
15327 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15328 cu->rust_unions.push_back (type);
15329
15330 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15331 snapshots) has been known to create a die giving a declaration
15332 for a class that has, as a child, a die giving a definition for a
15333 nested class. So we have to process our children even if the
15334 current die is a declaration. Normally, of course, a declaration
15335 won't have any children at all. */
15336
15337 child_die = die->child;
15338
15339 while (child_die != NULL && child_die->tag)
15340 {
15341 if (child_die->tag == DW_TAG_member
15342 || child_die->tag == DW_TAG_variable
15343 || child_die->tag == DW_TAG_inheritance
15344 || child_die->tag == DW_TAG_template_value_param
15345 || child_die->tag == DW_TAG_template_type_param)
15346 {
15347 /* Do nothing. */
15348 }
15349 else
15350 process_die (child_die, cu);
15351
15352 child_die = child_die->sibling;
15353 }
15354
15355 /* Do not consider external references. According to the DWARF standard,
15356 these DIEs are identified by the fact that they have no byte_size
15357 attribute, and a declaration attribute. */
15358 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15359 || !die_is_declaration (die, cu))
15360 {
15361 struct symbol *sym = new_symbol (die, type, cu);
15362
15363 if (has_template_parameters)
15364 {
15365 struct symtab *symtab;
15366 if (sym != nullptr)
15367 symtab = symbol_symtab (sym);
15368 else if (cu->line_header != nullptr)
15369 {
15370 /* Any related symtab will do. */
15371 symtab
15372 = cu->line_header->file_names ()[0].symtab;
15373 }
15374 else
15375 {
15376 symtab = nullptr;
15377 complaint (_("could not find suitable "
15378 "symtab for template parameter"
15379 " - DIE at %s [in module %s]"),
15380 sect_offset_str (die->sect_off),
15381 objfile_name (objfile));
15382 }
15383
15384 if (symtab != nullptr)
15385 {
15386 /* Make sure that the symtab is set on the new symbols.
15387 Even though they don't appear in this symtab directly,
15388 other parts of gdb assume that symbols do, and this is
15389 reasonably true. */
15390 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15391 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15392 }
15393 }
15394 }
15395 }
15396
15397 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15398 update TYPE using some information only available in DIE's children. */
15399
15400 static void
15401 update_enumeration_type_from_children (struct die_info *die,
15402 struct type *type,
15403 struct dwarf2_cu *cu)
15404 {
15405 struct die_info *child_die;
15406 int unsigned_enum = 1;
15407 int flag_enum = 1;
15408
15409 auto_obstack obstack;
15410
15411 for (child_die = die->child;
15412 child_die != NULL && child_die->tag;
15413 child_die = child_die->sibling)
15414 {
15415 struct attribute *attr;
15416 LONGEST value;
15417 const gdb_byte *bytes;
15418 struct dwarf2_locexpr_baton *baton;
15419 const char *name;
15420
15421 if (child_die->tag != DW_TAG_enumerator)
15422 continue;
15423
15424 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15425 if (attr == NULL)
15426 continue;
15427
15428 name = dwarf2_name (child_die, cu);
15429 if (name == NULL)
15430 name = "<anonymous enumerator>";
15431
15432 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15433 &value, &bytes, &baton);
15434 if (value < 0)
15435 {
15436 unsigned_enum = 0;
15437 flag_enum = 0;
15438 }
15439 else
15440 {
15441 if (count_one_bits_ll (value) >= 2)
15442 flag_enum = 0;
15443 }
15444
15445 /* If we already know that the enum type is neither unsigned, nor
15446 a flag type, no need to look at the rest of the enumerates. */
15447 if (!unsigned_enum && !flag_enum)
15448 break;
15449 }
15450
15451 if (unsigned_enum)
15452 TYPE_UNSIGNED (type) = 1;
15453 if (flag_enum)
15454 TYPE_FLAG_ENUM (type) = 1;
15455 }
15456
15457 /* Given a DW_AT_enumeration_type die, set its type. We do not
15458 complete the type's fields yet, or create any symbols. */
15459
15460 static struct type *
15461 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15462 {
15463 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15464 struct type *type;
15465 struct attribute *attr;
15466 const char *name;
15467
15468 /* If the definition of this type lives in .debug_types, read that type.
15469 Don't follow DW_AT_specification though, that will take us back up
15470 the chain and we want to go down. */
15471 attr = die->attr (DW_AT_signature);
15472 if (attr != nullptr)
15473 {
15474 type = get_DW_AT_signature_type (die, attr, cu);
15475
15476 /* The type's CU may not be the same as CU.
15477 Ensure TYPE is recorded with CU in die_type_hash. */
15478 return set_die_type (die, type, cu);
15479 }
15480
15481 type = alloc_type (objfile);
15482
15483 TYPE_CODE (type) = TYPE_CODE_ENUM;
15484 name = dwarf2_full_name (NULL, die, cu);
15485 if (name != NULL)
15486 TYPE_NAME (type) = name;
15487
15488 attr = dwarf2_attr (die, DW_AT_type, cu);
15489 if (attr != NULL)
15490 {
15491 struct type *underlying_type = die_type (die, cu);
15492
15493 TYPE_TARGET_TYPE (type) = underlying_type;
15494 }
15495
15496 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15497 if (attr != nullptr)
15498 {
15499 TYPE_LENGTH (type) = DW_UNSND (attr);
15500 }
15501 else
15502 {
15503 TYPE_LENGTH (type) = 0;
15504 }
15505
15506 maybe_set_alignment (cu, die, type);
15507
15508 /* The enumeration DIE can be incomplete. In Ada, any type can be
15509 declared as private in the package spec, and then defined only
15510 inside the package body. Such types are known as Taft Amendment
15511 Types. When another package uses such a type, an incomplete DIE
15512 may be generated by the compiler. */
15513 if (die_is_declaration (die, cu))
15514 TYPE_STUB (type) = 1;
15515
15516 /* Finish the creation of this type by using the enum's children.
15517 We must call this even when the underlying type has been provided
15518 so that we can determine if we're looking at a "flag" enum. */
15519 update_enumeration_type_from_children (die, type, cu);
15520
15521 /* If this type has an underlying type that is not a stub, then we
15522 may use its attributes. We always use the "unsigned" attribute
15523 in this situation, because ordinarily we guess whether the type
15524 is unsigned -- but the guess can be wrong and the underlying type
15525 can tell us the reality. However, we defer to a local size
15526 attribute if one exists, because this lets the compiler override
15527 the underlying type if needed. */
15528 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15529 {
15530 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15531 if (TYPE_LENGTH (type) == 0)
15532 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15533 if (TYPE_RAW_ALIGN (type) == 0
15534 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15535 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15536 }
15537
15538 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15539
15540 return set_die_type (die, type, cu);
15541 }
15542
15543 /* Given a pointer to a die which begins an enumeration, process all
15544 the dies that define the members of the enumeration, and create the
15545 symbol for the enumeration type.
15546
15547 NOTE: We reverse the order of the element list. */
15548
15549 static void
15550 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15551 {
15552 struct type *this_type;
15553
15554 this_type = get_die_type (die, cu);
15555 if (this_type == NULL)
15556 this_type = read_enumeration_type (die, cu);
15557
15558 if (die->child != NULL)
15559 {
15560 struct die_info *child_die;
15561 struct symbol *sym;
15562 std::vector<struct field> fields;
15563 const char *name;
15564
15565 child_die = die->child;
15566 while (child_die && child_die->tag)
15567 {
15568 if (child_die->tag != DW_TAG_enumerator)
15569 {
15570 process_die (child_die, cu);
15571 }
15572 else
15573 {
15574 name = dwarf2_name (child_die, cu);
15575 if (name)
15576 {
15577 sym = new_symbol (child_die, this_type, cu);
15578
15579 fields.emplace_back ();
15580 struct field &field = fields.back ();
15581
15582 FIELD_NAME (field) = sym->linkage_name ();
15583 FIELD_TYPE (field) = NULL;
15584 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15585 FIELD_BITSIZE (field) = 0;
15586 }
15587 }
15588
15589 child_die = child_die->sibling;
15590 }
15591
15592 if (!fields.empty ())
15593 {
15594 TYPE_NFIELDS (this_type) = fields.size ();
15595 TYPE_FIELDS (this_type) = (struct field *)
15596 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15597 memcpy (TYPE_FIELDS (this_type), fields.data (),
15598 sizeof (struct field) * fields.size ());
15599 }
15600 }
15601
15602 /* If we are reading an enum from a .debug_types unit, and the enum
15603 is a declaration, and the enum is not the signatured type in the
15604 unit, then we do not want to add a symbol for it. Adding a
15605 symbol would in some cases obscure the true definition of the
15606 enum, giving users an incomplete type when the definition is
15607 actually available. Note that we do not want to do this for all
15608 enums which are just declarations, because C++0x allows forward
15609 enum declarations. */
15610 if (cu->per_cu->is_debug_types
15611 && die_is_declaration (die, cu))
15612 {
15613 struct signatured_type *sig_type;
15614
15615 sig_type = (struct signatured_type *) cu->per_cu;
15616 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15617 if (sig_type->type_offset_in_section != die->sect_off)
15618 return;
15619 }
15620
15621 new_symbol (die, this_type, cu);
15622 }
15623
15624 /* Extract all information from a DW_TAG_array_type DIE and put it in
15625 the DIE's type field. For now, this only handles one dimensional
15626 arrays. */
15627
15628 static struct type *
15629 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15630 {
15631 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15632 struct die_info *child_die;
15633 struct type *type;
15634 struct type *element_type, *range_type, *index_type;
15635 struct attribute *attr;
15636 const char *name;
15637 struct dynamic_prop *byte_stride_prop = NULL;
15638 unsigned int bit_stride = 0;
15639
15640 element_type = die_type (die, cu);
15641
15642 /* The die_type call above may have already set the type for this DIE. */
15643 type = get_die_type (die, cu);
15644 if (type)
15645 return type;
15646
15647 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15648 if (attr != NULL)
15649 {
15650 int stride_ok;
15651 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15652
15653 byte_stride_prop
15654 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15655 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15656 prop_type);
15657 if (!stride_ok)
15658 {
15659 complaint (_("unable to read array DW_AT_byte_stride "
15660 " - DIE at %s [in module %s]"),
15661 sect_offset_str (die->sect_off),
15662 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15663 /* Ignore this attribute. We will likely not be able to print
15664 arrays of this type correctly, but there is little we can do
15665 to help if we cannot read the attribute's value. */
15666 byte_stride_prop = NULL;
15667 }
15668 }
15669
15670 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15671 if (attr != NULL)
15672 bit_stride = DW_UNSND (attr);
15673
15674 /* Irix 6.2 native cc creates array types without children for
15675 arrays with unspecified length. */
15676 if (die->child == NULL)
15677 {
15678 index_type = objfile_type (objfile)->builtin_int;
15679 range_type = create_static_range_type (NULL, index_type, 0, -1);
15680 type = create_array_type_with_stride (NULL, element_type, range_type,
15681 byte_stride_prop, bit_stride);
15682 return set_die_type (die, type, cu);
15683 }
15684
15685 std::vector<struct type *> range_types;
15686 child_die = die->child;
15687 while (child_die && child_die->tag)
15688 {
15689 if (child_die->tag == DW_TAG_subrange_type)
15690 {
15691 struct type *child_type = read_type_die (child_die, cu);
15692
15693 if (child_type != NULL)
15694 {
15695 /* The range type was succesfully read. Save it for the
15696 array type creation. */
15697 range_types.push_back (child_type);
15698 }
15699 }
15700 child_die = child_die->sibling;
15701 }
15702
15703 /* Dwarf2 dimensions are output from left to right, create the
15704 necessary array types in backwards order. */
15705
15706 type = element_type;
15707
15708 if (read_array_order (die, cu) == DW_ORD_col_major)
15709 {
15710 int i = 0;
15711
15712 while (i < range_types.size ())
15713 type = create_array_type_with_stride (NULL, type, range_types[i++],
15714 byte_stride_prop, bit_stride);
15715 }
15716 else
15717 {
15718 size_t ndim = range_types.size ();
15719 while (ndim-- > 0)
15720 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15721 byte_stride_prop, bit_stride);
15722 }
15723
15724 /* Understand Dwarf2 support for vector types (like they occur on
15725 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15726 array type. This is not part of the Dwarf2/3 standard yet, but a
15727 custom vendor extension. The main difference between a regular
15728 array and the vector variant is that vectors are passed by value
15729 to functions. */
15730 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15731 if (attr != nullptr)
15732 make_vector_type (type);
15733
15734 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15735 implementation may choose to implement triple vectors using this
15736 attribute. */
15737 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15738 if (attr != nullptr)
15739 {
15740 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15741 TYPE_LENGTH (type) = DW_UNSND (attr);
15742 else
15743 complaint (_("DW_AT_byte_size for array type smaller "
15744 "than the total size of elements"));
15745 }
15746
15747 name = dwarf2_name (die, cu);
15748 if (name)
15749 TYPE_NAME (type) = name;
15750
15751 maybe_set_alignment (cu, die, type);
15752
15753 /* Install the type in the die. */
15754 set_die_type (die, type, cu);
15755
15756 /* set_die_type should be already done. */
15757 set_descriptive_type (type, die, cu);
15758
15759 return type;
15760 }
15761
15762 static enum dwarf_array_dim_ordering
15763 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15764 {
15765 struct attribute *attr;
15766
15767 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15768
15769 if (attr != nullptr)
15770 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15771
15772 /* GNU F77 is a special case, as at 08/2004 array type info is the
15773 opposite order to the dwarf2 specification, but data is still
15774 laid out as per normal fortran.
15775
15776 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15777 version checking. */
15778
15779 if (cu->language == language_fortran
15780 && cu->producer && strstr (cu->producer, "GNU F77"))
15781 {
15782 return DW_ORD_row_major;
15783 }
15784
15785 switch (cu->language_defn->la_array_ordering)
15786 {
15787 case array_column_major:
15788 return DW_ORD_col_major;
15789 case array_row_major:
15790 default:
15791 return DW_ORD_row_major;
15792 };
15793 }
15794
15795 /* Extract all information from a DW_TAG_set_type DIE and put it in
15796 the DIE's type field. */
15797
15798 static struct type *
15799 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15800 {
15801 struct type *domain_type, *set_type;
15802 struct attribute *attr;
15803
15804 domain_type = die_type (die, cu);
15805
15806 /* The die_type call above may have already set the type for this DIE. */
15807 set_type = get_die_type (die, cu);
15808 if (set_type)
15809 return set_type;
15810
15811 set_type = create_set_type (NULL, domain_type);
15812
15813 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15814 if (attr != nullptr)
15815 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15816
15817 maybe_set_alignment (cu, die, set_type);
15818
15819 return set_die_type (die, set_type, cu);
15820 }
15821
15822 /* A helper for read_common_block that creates a locexpr baton.
15823 SYM is the symbol which we are marking as computed.
15824 COMMON_DIE is the DIE for the common block.
15825 COMMON_LOC is the location expression attribute for the common
15826 block itself.
15827 MEMBER_LOC is the location expression attribute for the particular
15828 member of the common block that we are processing.
15829 CU is the CU from which the above come. */
15830
15831 static void
15832 mark_common_block_symbol_computed (struct symbol *sym,
15833 struct die_info *common_die,
15834 struct attribute *common_loc,
15835 struct attribute *member_loc,
15836 struct dwarf2_cu *cu)
15837 {
15838 struct dwarf2_per_objfile *dwarf2_per_objfile
15839 = cu->per_cu->dwarf2_per_objfile;
15840 struct objfile *objfile = dwarf2_per_objfile->objfile;
15841 struct dwarf2_locexpr_baton *baton;
15842 gdb_byte *ptr;
15843 unsigned int cu_off;
15844 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15845 LONGEST offset = 0;
15846
15847 gdb_assert (common_loc && member_loc);
15848 gdb_assert (common_loc->form_is_block ());
15849 gdb_assert (member_loc->form_is_block ()
15850 || member_loc->form_is_constant ());
15851
15852 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15853 baton->per_cu = cu->per_cu;
15854 gdb_assert (baton->per_cu);
15855
15856 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15857
15858 if (member_loc->form_is_constant ())
15859 {
15860 offset = member_loc->constant_value (0);
15861 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15862 }
15863 else
15864 baton->size += DW_BLOCK (member_loc)->size;
15865
15866 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15867 baton->data = ptr;
15868
15869 *ptr++ = DW_OP_call4;
15870 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15871 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15872 ptr += 4;
15873
15874 if (member_loc->form_is_constant ())
15875 {
15876 *ptr++ = DW_OP_addr;
15877 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15878 ptr += cu->header.addr_size;
15879 }
15880 else
15881 {
15882 /* We have to copy the data here, because DW_OP_call4 will only
15883 use a DW_AT_location attribute. */
15884 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15885 ptr += DW_BLOCK (member_loc)->size;
15886 }
15887
15888 *ptr++ = DW_OP_plus;
15889 gdb_assert (ptr - baton->data == baton->size);
15890
15891 SYMBOL_LOCATION_BATON (sym) = baton;
15892 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15893 }
15894
15895 /* Create appropriate locally-scoped variables for all the
15896 DW_TAG_common_block entries. Also create a struct common_block
15897 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15898 is used to separate the common blocks name namespace from regular
15899 variable names. */
15900
15901 static void
15902 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15903 {
15904 struct attribute *attr;
15905
15906 attr = dwarf2_attr (die, DW_AT_location, cu);
15907 if (attr != nullptr)
15908 {
15909 /* Support the .debug_loc offsets. */
15910 if (attr->form_is_block ())
15911 {
15912 /* Ok. */
15913 }
15914 else if (attr->form_is_section_offset ())
15915 {
15916 dwarf2_complex_location_expr_complaint ();
15917 attr = NULL;
15918 }
15919 else
15920 {
15921 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15922 "common block member");
15923 attr = NULL;
15924 }
15925 }
15926
15927 if (die->child != NULL)
15928 {
15929 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15930 struct die_info *child_die;
15931 size_t n_entries = 0, size;
15932 struct common_block *common_block;
15933 struct symbol *sym;
15934
15935 for (child_die = die->child;
15936 child_die && child_die->tag;
15937 child_die = child_die->sibling)
15938 ++n_entries;
15939
15940 size = (sizeof (struct common_block)
15941 + (n_entries - 1) * sizeof (struct symbol *));
15942 common_block
15943 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15944 size);
15945 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15946 common_block->n_entries = 0;
15947
15948 for (child_die = die->child;
15949 child_die && child_die->tag;
15950 child_die = child_die->sibling)
15951 {
15952 /* Create the symbol in the DW_TAG_common_block block in the current
15953 symbol scope. */
15954 sym = new_symbol (child_die, NULL, cu);
15955 if (sym != NULL)
15956 {
15957 struct attribute *member_loc;
15958
15959 common_block->contents[common_block->n_entries++] = sym;
15960
15961 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15962 cu);
15963 if (member_loc)
15964 {
15965 /* GDB has handled this for a long time, but it is
15966 not specified by DWARF. It seems to have been
15967 emitted by gfortran at least as recently as:
15968 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15969 complaint (_("Variable in common block has "
15970 "DW_AT_data_member_location "
15971 "- DIE at %s [in module %s]"),
15972 sect_offset_str (child_die->sect_off),
15973 objfile_name (objfile));
15974
15975 if (member_loc->form_is_section_offset ())
15976 dwarf2_complex_location_expr_complaint ();
15977 else if (member_loc->form_is_constant ()
15978 || member_loc->form_is_block ())
15979 {
15980 if (attr != nullptr)
15981 mark_common_block_symbol_computed (sym, die, attr,
15982 member_loc, cu);
15983 }
15984 else
15985 dwarf2_complex_location_expr_complaint ();
15986 }
15987 }
15988 }
15989
15990 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
15991 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
15992 }
15993 }
15994
15995 /* Create a type for a C++ namespace. */
15996
15997 static struct type *
15998 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
15999 {
16000 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16001 const char *previous_prefix, *name;
16002 int is_anonymous;
16003 struct type *type;
16004
16005 /* For extensions, reuse the type of the original namespace. */
16006 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16007 {
16008 struct die_info *ext_die;
16009 struct dwarf2_cu *ext_cu = cu;
16010
16011 ext_die = dwarf2_extension (die, &ext_cu);
16012 type = read_type_die (ext_die, ext_cu);
16013
16014 /* EXT_CU may not be the same as CU.
16015 Ensure TYPE is recorded with CU in die_type_hash. */
16016 return set_die_type (die, type, cu);
16017 }
16018
16019 name = namespace_name (die, &is_anonymous, cu);
16020
16021 /* Now build the name of the current namespace. */
16022
16023 previous_prefix = determine_prefix (die, cu);
16024 if (previous_prefix[0] != '\0')
16025 name = typename_concat (&objfile->objfile_obstack,
16026 previous_prefix, name, 0, cu);
16027
16028 /* Create the type. */
16029 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16030
16031 return set_die_type (die, type, cu);
16032 }
16033
16034 /* Read a namespace scope. */
16035
16036 static void
16037 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16038 {
16039 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16040 int is_anonymous;
16041
16042 /* Add a symbol associated to this if we haven't seen the namespace
16043 before. Also, add a using directive if it's an anonymous
16044 namespace. */
16045
16046 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16047 {
16048 struct type *type;
16049
16050 type = read_type_die (die, cu);
16051 new_symbol (die, type, cu);
16052
16053 namespace_name (die, &is_anonymous, cu);
16054 if (is_anonymous)
16055 {
16056 const char *previous_prefix = determine_prefix (die, cu);
16057
16058 std::vector<const char *> excludes;
16059 add_using_directive (using_directives (cu),
16060 previous_prefix, TYPE_NAME (type), NULL,
16061 NULL, excludes, 0, &objfile->objfile_obstack);
16062 }
16063 }
16064
16065 if (die->child != NULL)
16066 {
16067 struct die_info *child_die = die->child;
16068
16069 while (child_die && child_die->tag)
16070 {
16071 process_die (child_die, cu);
16072 child_die = child_die->sibling;
16073 }
16074 }
16075 }
16076
16077 /* Read a Fortran module as type. This DIE can be only a declaration used for
16078 imported module. Still we need that type as local Fortran "use ... only"
16079 declaration imports depend on the created type in determine_prefix. */
16080
16081 static struct type *
16082 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16083 {
16084 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16085 const char *module_name;
16086 struct type *type;
16087
16088 module_name = dwarf2_name (die, cu);
16089 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16090
16091 return set_die_type (die, type, cu);
16092 }
16093
16094 /* Read a Fortran module. */
16095
16096 static void
16097 read_module (struct die_info *die, struct dwarf2_cu *cu)
16098 {
16099 struct die_info *child_die = die->child;
16100 struct type *type;
16101
16102 type = read_type_die (die, cu);
16103 new_symbol (die, type, cu);
16104
16105 while (child_die && child_die->tag)
16106 {
16107 process_die (child_die, cu);
16108 child_die = child_die->sibling;
16109 }
16110 }
16111
16112 /* Return the name of the namespace represented by DIE. Set
16113 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16114 namespace. */
16115
16116 static const char *
16117 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16118 {
16119 struct die_info *current_die;
16120 const char *name = NULL;
16121
16122 /* Loop through the extensions until we find a name. */
16123
16124 for (current_die = die;
16125 current_die != NULL;
16126 current_die = dwarf2_extension (die, &cu))
16127 {
16128 /* We don't use dwarf2_name here so that we can detect the absence
16129 of a name -> anonymous namespace. */
16130 name = dwarf2_string_attr (die, DW_AT_name, cu);
16131
16132 if (name != NULL)
16133 break;
16134 }
16135
16136 /* Is it an anonymous namespace? */
16137
16138 *is_anonymous = (name == NULL);
16139 if (*is_anonymous)
16140 name = CP_ANONYMOUS_NAMESPACE_STR;
16141
16142 return name;
16143 }
16144
16145 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16146 the user defined type vector. */
16147
16148 static struct type *
16149 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16150 {
16151 struct gdbarch *gdbarch
16152 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16153 struct comp_unit_head *cu_header = &cu->header;
16154 struct type *type;
16155 struct attribute *attr_byte_size;
16156 struct attribute *attr_address_class;
16157 int byte_size, addr_class;
16158 struct type *target_type;
16159
16160 target_type = die_type (die, cu);
16161
16162 /* The die_type call above may have already set the type for this DIE. */
16163 type = get_die_type (die, cu);
16164 if (type)
16165 return type;
16166
16167 type = lookup_pointer_type (target_type);
16168
16169 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16170 if (attr_byte_size)
16171 byte_size = DW_UNSND (attr_byte_size);
16172 else
16173 byte_size = cu_header->addr_size;
16174
16175 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16176 if (attr_address_class)
16177 addr_class = DW_UNSND (attr_address_class);
16178 else
16179 addr_class = DW_ADDR_none;
16180
16181 ULONGEST alignment = get_alignment (cu, die);
16182
16183 /* If the pointer size, alignment, or address class is different
16184 than the default, create a type variant marked as such and set
16185 the length accordingly. */
16186 if (TYPE_LENGTH (type) != byte_size
16187 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16188 && alignment != TYPE_RAW_ALIGN (type))
16189 || addr_class != DW_ADDR_none)
16190 {
16191 if (gdbarch_address_class_type_flags_p (gdbarch))
16192 {
16193 int type_flags;
16194
16195 type_flags = gdbarch_address_class_type_flags
16196 (gdbarch, byte_size, addr_class);
16197 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16198 == 0);
16199 type = make_type_with_address_space (type, type_flags);
16200 }
16201 else if (TYPE_LENGTH (type) != byte_size)
16202 {
16203 complaint (_("invalid pointer size %d"), byte_size);
16204 }
16205 else if (TYPE_RAW_ALIGN (type) != alignment)
16206 {
16207 complaint (_("Invalid DW_AT_alignment"
16208 " - DIE at %s [in module %s]"),
16209 sect_offset_str (die->sect_off),
16210 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16211 }
16212 else
16213 {
16214 /* Should we also complain about unhandled address classes? */
16215 }
16216 }
16217
16218 TYPE_LENGTH (type) = byte_size;
16219 set_type_align (type, alignment);
16220 return set_die_type (die, type, cu);
16221 }
16222
16223 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16224 the user defined type vector. */
16225
16226 static struct type *
16227 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16228 {
16229 struct type *type;
16230 struct type *to_type;
16231 struct type *domain;
16232
16233 to_type = die_type (die, cu);
16234 domain = die_containing_type (die, cu);
16235
16236 /* The calls above may have already set the type for this DIE. */
16237 type = get_die_type (die, cu);
16238 if (type)
16239 return type;
16240
16241 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16242 type = lookup_methodptr_type (to_type);
16243 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16244 {
16245 struct type *new_type
16246 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16247
16248 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16249 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16250 TYPE_VARARGS (to_type));
16251 type = lookup_methodptr_type (new_type);
16252 }
16253 else
16254 type = lookup_memberptr_type (to_type, domain);
16255
16256 return set_die_type (die, type, cu);
16257 }
16258
16259 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16260 the user defined type vector. */
16261
16262 static struct type *
16263 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16264 enum type_code refcode)
16265 {
16266 struct comp_unit_head *cu_header = &cu->header;
16267 struct type *type, *target_type;
16268 struct attribute *attr;
16269
16270 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16271
16272 target_type = die_type (die, cu);
16273
16274 /* The die_type call above may have already set the type for this DIE. */
16275 type = get_die_type (die, cu);
16276 if (type)
16277 return type;
16278
16279 type = lookup_reference_type (target_type, refcode);
16280 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16281 if (attr != nullptr)
16282 {
16283 TYPE_LENGTH (type) = DW_UNSND (attr);
16284 }
16285 else
16286 {
16287 TYPE_LENGTH (type) = cu_header->addr_size;
16288 }
16289 maybe_set_alignment (cu, die, type);
16290 return set_die_type (die, type, cu);
16291 }
16292
16293 /* Add the given cv-qualifiers to the element type of the array. GCC
16294 outputs DWARF type qualifiers that apply to an array, not the
16295 element type. But GDB relies on the array element type to carry
16296 the cv-qualifiers. This mimics section 6.7.3 of the C99
16297 specification. */
16298
16299 static struct type *
16300 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16301 struct type *base_type, int cnst, int voltl)
16302 {
16303 struct type *el_type, *inner_array;
16304
16305 base_type = copy_type (base_type);
16306 inner_array = base_type;
16307
16308 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16309 {
16310 TYPE_TARGET_TYPE (inner_array) =
16311 copy_type (TYPE_TARGET_TYPE (inner_array));
16312 inner_array = TYPE_TARGET_TYPE (inner_array);
16313 }
16314
16315 el_type = TYPE_TARGET_TYPE (inner_array);
16316 cnst |= TYPE_CONST (el_type);
16317 voltl |= TYPE_VOLATILE (el_type);
16318 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16319
16320 return set_die_type (die, base_type, cu);
16321 }
16322
16323 static struct type *
16324 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16325 {
16326 struct type *base_type, *cv_type;
16327
16328 base_type = die_type (die, cu);
16329
16330 /* The die_type call above may have already set the type for this DIE. */
16331 cv_type = get_die_type (die, cu);
16332 if (cv_type)
16333 return cv_type;
16334
16335 /* In case the const qualifier is applied to an array type, the element type
16336 is so qualified, not the array type (section 6.7.3 of C99). */
16337 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16338 return add_array_cv_type (die, cu, base_type, 1, 0);
16339
16340 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16341 return set_die_type (die, cv_type, cu);
16342 }
16343
16344 static struct type *
16345 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16346 {
16347 struct type *base_type, *cv_type;
16348
16349 base_type = die_type (die, cu);
16350
16351 /* The die_type call above may have already set the type for this DIE. */
16352 cv_type = get_die_type (die, cu);
16353 if (cv_type)
16354 return cv_type;
16355
16356 /* In case the volatile qualifier is applied to an array type, the
16357 element type is so qualified, not the array type (section 6.7.3
16358 of C99). */
16359 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16360 return add_array_cv_type (die, cu, base_type, 0, 1);
16361
16362 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16363 return set_die_type (die, cv_type, cu);
16364 }
16365
16366 /* Handle DW_TAG_restrict_type. */
16367
16368 static struct type *
16369 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16370 {
16371 struct type *base_type, *cv_type;
16372
16373 base_type = die_type (die, cu);
16374
16375 /* The die_type call above may have already set the type for this DIE. */
16376 cv_type = get_die_type (die, cu);
16377 if (cv_type)
16378 return cv_type;
16379
16380 cv_type = make_restrict_type (base_type);
16381 return set_die_type (die, cv_type, cu);
16382 }
16383
16384 /* Handle DW_TAG_atomic_type. */
16385
16386 static struct type *
16387 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16388 {
16389 struct type *base_type, *cv_type;
16390
16391 base_type = die_type (die, cu);
16392
16393 /* The die_type call above may have already set the type for this DIE. */
16394 cv_type = get_die_type (die, cu);
16395 if (cv_type)
16396 return cv_type;
16397
16398 cv_type = make_atomic_type (base_type);
16399 return set_die_type (die, cv_type, cu);
16400 }
16401
16402 /* Extract all information from a DW_TAG_string_type DIE and add to
16403 the user defined type vector. It isn't really a user defined type,
16404 but it behaves like one, with other DIE's using an AT_user_def_type
16405 attribute to reference it. */
16406
16407 static struct type *
16408 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16409 {
16410 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16411 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16412 struct type *type, *range_type, *index_type, *char_type;
16413 struct attribute *attr;
16414 struct dynamic_prop prop;
16415 bool length_is_constant = true;
16416 LONGEST length;
16417
16418 /* There are a couple of places where bit sizes might be made use of
16419 when parsing a DW_TAG_string_type, however, no producer that we know
16420 of make use of these. Handling bit sizes that are a multiple of the
16421 byte size is easy enough, but what about other bit sizes? Lets deal
16422 with that problem when we have to. Warn about these attributes being
16423 unsupported, then parse the type and ignore them like we always
16424 have. */
16425 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16426 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16427 {
16428 static bool warning_printed = false;
16429 if (!warning_printed)
16430 {
16431 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16432 "currently supported on DW_TAG_string_type."));
16433 warning_printed = true;
16434 }
16435 }
16436
16437 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16438 if (attr != nullptr && !attr->form_is_constant ())
16439 {
16440 /* The string length describes the location at which the length of
16441 the string can be found. The size of the length field can be
16442 specified with one of the attributes below. */
16443 struct type *prop_type;
16444 struct attribute *len
16445 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16446 if (len == nullptr)
16447 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16448 if (len != nullptr && len->form_is_constant ())
16449 {
16450 /* Pass 0 as the default as we know this attribute is constant
16451 and the default value will not be returned. */
16452 LONGEST sz = len->constant_value (0);
16453 prop_type = cu->per_cu->int_type (sz, true);
16454 }
16455 else
16456 {
16457 /* If the size is not specified then we assume it is the size of
16458 an address on this target. */
16459 prop_type = cu->per_cu->addr_sized_int_type (true);
16460 }
16461
16462 /* Convert the attribute into a dynamic property. */
16463 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16464 length = 1;
16465 else
16466 length_is_constant = false;
16467 }
16468 else if (attr != nullptr)
16469 {
16470 /* This DW_AT_string_length just contains the length with no
16471 indirection. There's no need to create a dynamic property in this
16472 case. Pass 0 for the default value as we know it will not be
16473 returned in this case. */
16474 length = attr->constant_value (0);
16475 }
16476 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16477 {
16478 /* We don't currently support non-constant byte sizes for strings. */
16479 length = attr->constant_value (1);
16480 }
16481 else
16482 {
16483 /* Use 1 as a fallback length if we have nothing else. */
16484 length = 1;
16485 }
16486
16487 index_type = objfile_type (objfile)->builtin_int;
16488 if (length_is_constant)
16489 range_type = create_static_range_type (NULL, index_type, 1, length);
16490 else
16491 {
16492 struct dynamic_prop low_bound;
16493
16494 low_bound.kind = PROP_CONST;
16495 low_bound.data.const_val = 1;
16496 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16497 }
16498 char_type = language_string_char_type (cu->language_defn, gdbarch);
16499 type = create_string_type (NULL, char_type, range_type);
16500
16501 return set_die_type (die, type, cu);
16502 }
16503
16504 /* Assuming that DIE corresponds to a function, returns nonzero
16505 if the function is prototyped. */
16506
16507 static int
16508 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16509 {
16510 struct attribute *attr;
16511
16512 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16513 if (attr && (DW_UNSND (attr) != 0))
16514 return 1;
16515
16516 /* The DWARF standard implies that the DW_AT_prototyped attribute
16517 is only meaningful for C, but the concept also extends to other
16518 languages that allow unprototyped functions (Eg: Objective C).
16519 For all other languages, assume that functions are always
16520 prototyped. */
16521 if (cu->language != language_c
16522 && cu->language != language_objc
16523 && cu->language != language_opencl)
16524 return 1;
16525
16526 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16527 prototyped and unprototyped functions; default to prototyped,
16528 since that is more common in modern code (and RealView warns
16529 about unprototyped functions). */
16530 if (producer_is_realview (cu->producer))
16531 return 1;
16532
16533 return 0;
16534 }
16535
16536 /* Handle DIES due to C code like:
16537
16538 struct foo
16539 {
16540 int (*funcp)(int a, long l);
16541 int b;
16542 };
16543
16544 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16545
16546 static struct type *
16547 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16548 {
16549 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16550 struct type *type; /* Type that this function returns. */
16551 struct type *ftype; /* Function that returns above type. */
16552 struct attribute *attr;
16553
16554 type = die_type (die, cu);
16555
16556 /* The die_type call above may have already set the type for this DIE. */
16557 ftype = get_die_type (die, cu);
16558 if (ftype)
16559 return ftype;
16560
16561 ftype = lookup_function_type (type);
16562
16563 if (prototyped_function_p (die, cu))
16564 TYPE_PROTOTYPED (ftype) = 1;
16565
16566 /* Store the calling convention in the type if it's available in
16567 the subroutine die. Otherwise set the calling convention to
16568 the default value DW_CC_normal. */
16569 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16570 if (attr != nullptr
16571 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16572 TYPE_CALLING_CONVENTION (ftype)
16573 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16574 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16575 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16576 else
16577 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16578
16579 /* Record whether the function returns normally to its caller or not
16580 if the DWARF producer set that information. */
16581 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16582 if (attr && (DW_UNSND (attr) != 0))
16583 TYPE_NO_RETURN (ftype) = 1;
16584
16585 /* We need to add the subroutine type to the die immediately so
16586 we don't infinitely recurse when dealing with parameters
16587 declared as the same subroutine type. */
16588 set_die_type (die, ftype, cu);
16589
16590 if (die->child != NULL)
16591 {
16592 struct type *void_type = objfile_type (objfile)->builtin_void;
16593 struct die_info *child_die;
16594 int nparams, iparams;
16595
16596 /* Count the number of parameters.
16597 FIXME: GDB currently ignores vararg functions, but knows about
16598 vararg member functions. */
16599 nparams = 0;
16600 child_die = die->child;
16601 while (child_die && child_die->tag)
16602 {
16603 if (child_die->tag == DW_TAG_formal_parameter)
16604 nparams++;
16605 else if (child_die->tag == DW_TAG_unspecified_parameters)
16606 TYPE_VARARGS (ftype) = 1;
16607 child_die = child_die->sibling;
16608 }
16609
16610 /* Allocate storage for parameters and fill them in. */
16611 TYPE_NFIELDS (ftype) = nparams;
16612 TYPE_FIELDS (ftype) = (struct field *)
16613 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16614
16615 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16616 even if we error out during the parameters reading below. */
16617 for (iparams = 0; iparams < nparams; iparams++)
16618 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16619
16620 iparams = 0;
16621 child_die = die->child;
16622 while (child_die && child_die->tag)
16623 {
16624 if (child_die->tag == DW_TAG_formal_parameter)
16625 {
16626 struct type *arg_type;
16627
16628 /* DWARF version 2 has no clean way to discern C++
16629 static and non-static member functions. G++ helps
16630 GDB by marking the first parameter for non-static
16631 member functions (which is the this pointer) as
16632 artificial. We pass this information to
16633 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16634
16635 DWARF version 3 added DW_AT_object_pointer, which GCC
16636 4.5 does not yet generate. */
16637 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16638 if (attr != nullptr)
16639 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16640 else
16641 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16642 arg_type = die_type (child_die, cu);
16643
16644 /* RealView does not mark THIS as const, which the testsuite
16645 expects. GCC marks THIS as const in method definitions,
16646 but not in the class specifications (GCC PR 43053). */
16647 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16648 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16649 {
16650 int is_this = 0;
16651 struct dwarf2_cu *arg_cu = cu;
16652 const char *name = dwarf2_name (child_die, cu);
16653
16654 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16655 if (attr != nullptr)
16656 {
16657 /* If the compiler emits this, use it. */
16658 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16659 is_this = 1;
16660 }
16661 else if (name && strcmp (name, "this") == 0)
16662 /* Function definitions will have the argument names. */
16663 is_this = 1;
16664 else if (name == NULL && iparams == 0)
16665 /* Declarations may not have the names, so like
16666 elsewhere in GDB, assume an artificial first
16667 argument is "this". */
16668 is_this = 1;
16669
16670 if (is_this)
16671 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16672 arg_type, 0);
16673 }
16674
16675 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16676 iparams++;
16677 }
16678 child_die = child_die->sibling;
16679 }
16680 }
16681
16682 return ftype;
16683 }
16684
16685 static struct type *
16686 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16687 {
16688 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16689 const char *name = NULL;
16690 struct type *this_type, *target_type;
16691
16692 name = dwarf2_full_name (NULL, die, cu);
16693 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16694 TYPE_TARGET_STUB (this_type) = 1;
16695 set_die_type (die, this_type, cu);
16696 target_type = die_type (die, cu);
16697 if (target_type != this_type)
16698 TYPE_TARGET_TYPE (this_type) = target_type;
16699 else
16700 {
16701 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16702 spec and cause infinite loops in GDB. */
16703 complaint (_("Self-referential DW_TAG_typedef "
16704 "- DIE at %s [in module %s]"),
16705 sect_offset_str (die->sect_off), objfile_name (objfile));
16706 TYPE_TARGET_TYPE (this_type) = NULL;
16707 }
16708 if (name == NULL)
16709 {
16710 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16711 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16712 Handle these by just returning the target type, rather than
16713 constructing an anonymous typedef type and trying to handle this
16714 elsewhere. */
16715 set_die_type (die, target_type, cu);
16716 return target_type;
16717 }
16718 return this_type;
16719 }
16720
16721 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16722 (which may be different from NAME) to the architecture back-end to allow
16723 it to guess the correct format if necessary. */
16724
16725 static struct type *
16726 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16727 const char *name_hint, enum bfd_endian byte_order)
16728 {
16729 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16730 const struct floatformat **format;
16731 struct type *type;
16732
16733 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16734 if (format)
16735 type = init_float_type (objfile, bits, name, format, byte_order);
16736 else
16737 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16738
16739 return type;
16740 }
16741
16742 /* Allocate an integer type of size BITS and name NAME. */
16743
16744 static struct type *
16745 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16746 int bits, int unsigned_p, const char *name)
16747 {
16748 struct type *type;
16749
16750 /* Versions of Intel's C Compiler generate an integer type called "void"
16751 instead of using DW_TAG_unspecified_type. This has been seen on
16752 at least versions 14, 17, and 18. */
16753 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16754 && strcmp (name, "void") == 0)
16755 type = objfile_type (objfile)->builtin_void;
16756 else
16757 type = init_integer_type (objfile, bits, unsigned_p, name);
16758
16759 return type;
16760 }
16761
16762 /* Initialise and return a floating point type of size BITS suitable for
16763 use as a component of a complex number. The NAME_HINT is passed through
16764 when initialising the floating point type and is the name of the complex
16765 type.
16766
16767 As DWARF doesn't currently provide an explicit name for the components
16768 of a complex number, but it can be helpful to have these components
16769 named, we try to select a suitable name based on the size of the
16770 component. */
16771 static struct type *
16772 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16773 struct objfile *objfile,
16774 int bits, const char *name_hint,
16775 enum bfd_endian byte_order)
16776 {
16777 gdbarch *gdbarch = get_objfile_arch (objfile);
16778 struct type *tt = nullptr;
16779
16780 /* Try to find a suitable floating point builtin type of size BITS.
16781 We're going to use the name of this type as the name for the complex
16782 target type that we are about to create. */
16783 switch (cu->language)
16784 {
16785 case language_fortran:
16786 switch (bits)
16787 {
16788 case 32:
16789 tt = builtin_f_type (gdbarch)->builtin_real;
16790 break;
16791 case 64:
16792 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16793 break;
16794 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16795 case 128:
16796 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16797 break;
16798 }
16799 break;
16800 default:
16801 switch (bits)
16802 {
16803 case 32:
16804 tt = builtin_type (gdbarch)->builtin_float;
16805 break;
16806 case 64:
16807 tt = builtin_type (gdbarch)->builtin_double;
16808 break;
16809 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16810 case 128:
16811 tt = builtin_type (gdbarch)->builtin_long_double;
16812 break;
16813 }
16814 break;
16815 }
16816
16817 /* If the type we found doesn't match the size we were looking for, then
16818 pretend we didn't find a type at all, the complex target type we
16819 create will then be nameless. */
16820 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16821 tt = nullptr;
16822
16823 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16824 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16825 }
16826
16827 /* Find a representation of a given base type and install
16828 it in the TYPE field of the die. */
16829
16830 static struct type *
16831 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16832 {
16833 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16834 struct type *type;
16835 struct attribute *attr;
16836 int encoding = 0, bits = 0;
16837 const char *name;
16838 gdbarch *arch;
16839
16840 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16841 if (attr != nullptr)
16842 encoding = DW_UNSND (attr);
16843 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16844 if (attr != nullptr)
16845 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16846 name = dwarf2_name (die, cu);
16847 if (!name)
16848 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16849
16850 arch = get_objfile_arch (objfile);
16851 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16852
16853 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16854 if (attr)
16855 {
16856 int endianity = DW_UNSND (attr);
16857
16858 switch (endianity)
16859 {
16860 case DW_END_big:
16861 byte_order = BFD_ENDIAN_BIG;
16862 break;
16863 case DW_END_little:
16864 byte_order = BFD_ENDIAN_LITTLE;
16865 break;
16866 default:
16867 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16868 break;
16869 }
16870 }
16871
16872 switch (encoding)
16873 {
16874 case DW_ATE_address:
16875 /* Turn DW_ATE_address into a void * pointer. */
16876 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16877 type = init_pointer_type (objfile, bits, name, type);
16878 break;
16879 case DW_ATE_boolean:
16880 type = init_boolean_type (objfile, bits, 1, name);
16881 break;
16882 case DW_ATE_complex_float:
16883 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16884 byte_order);
16885 type = init_complex_type (name, type);
16886 break;
16887 case DW_ATE_decimal_float:
16888 type = init_decfloat_type (objfile, bits, name);
16889 break;
16890 case DW_ATE_float:
16891 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16892 break;
16893 case DW_ATE_signed:
16894 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16895 break;
16896 case DW_ATE_unsigned:
16897 if (cu->language == language_fortran
16898 && name
16899 && startswith (name, "character("))
16900 type = init_character_type (objfile, bits, 1, name);
16901 else
16902 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16903 break;
16904 case DW_ATE_signed_char:
16905 if (cu->language == language_ada || cu->language == language_m2
16906 || cu->language == language_pascal
16907 || cu->language == language_fortran)
16908 type = init_character_type (objfile, bits, 0, name);
16909 else
16910 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16911 break;
16912 case DW_ATE_unsigned_char:
16913 if (cu->language == language_ada || cu->language == language_m2
16914 || cu->language == language_pascal
16915 || cu->language == language_fortran
16916 || cu->language == language_rust)
16917 type = init_character_type (objfile, bits, 1, name);
16918 else
16919 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16920 break;
16921 case DW_ATE_UTF:
16922 {
16923 if (bits == 16)
16924 type = builtin_type (arch)->builtin_char16;
16925 else if (bits == 32)
16926 type = builtin_type (arch)->builtin_char32;
16927 else
16928 {
16929 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
16930 bits);
16931 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16932 }
16933 return set_die_type (die, type, cu);
16934 }
16935 break;
16936
16937 default:
16938 complaint (_("unsupported DW_AT_encoding: '%s'"),
16939 dwarf_type_encoding_name (encoding));
16940 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16941 break;
16942 }
16943
16944 if (name && strcmp (name, "char") == 0)
16945 TYPE_NOSIGN (type) = 1;
16946
16947 maybe_set_alignment (cu, die, type);
16948
16949 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
16950
16951 return set_die_type (die, type, cu);
16952 }
16953
16954 /* Parse dwarf attribute if it's a block, reference or constant and put the
16955 resulting value of the attribute into struct bound_prop.
16956 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16957
16958 static int
16959 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
16960 struct dwarf2_cu *cu, struct dynamic_prop *prop,
16961 struct type *default_type)
16962 {
16963 struct dwarf2_property_baton *baton;
16964 struct obstack *obstack
16965 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
16966
16967 gdb_assert (default_type != NULL);
16968
16969 if (attr == NULL || prop == NULL)
16970 return 0;
16971
16972 if (attr->form_is_block ())
16973 {
16974 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16975 baton->property_type = default_type;
16976 baton->locexpr.per_cu = cu->per_cu;
16977 baton->locexpr.size = DW_BLOCK (attr)->size;
16978 baton->locexpr.data = DW_BLOCK (attr)->data;
16979 switch (attr->name)
16980 {
16981 case DW_AT_string_length:
16982 baton->locexpr.is_reference = true;
16983 break;
16984 default:
16985 baton->locexpr.is_reference = false;
16986 break;
16987 }
16988 prop->data.baton = baton;
16989 prop->kind = PROP_LOCEXPR;
16990 gdb_assert (prop->data.baton != NULL);
16991 }
16992 else if (attr->form_is_ref ())
16993 {
16994 struct dwarf2_cu *target_cu = cu;
16995 struct die_info *target_die;
16996 struct attribute *target_attr;
16997
16998 target_die = follow_die_ref (die, attr, &target_cu);
16999 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17000 if (target_attr == NULL)
17001 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17002 target_cu);
17003 if (target_attr == NULL)
17004 return 0;
17005
17006 switch (target_attr->name)
17007 {
17008 case DW_AT_location:
17009 if (target_attr->form_is_section_offset ())
17010 {
17011 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17012 baton->property_type = die_type (target_die, target_cu);
17013 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17014 prop->data.baton = baton;
17015 prop->kind = PROP_LOCLIST;
17016 gdb_assert (prop->data.baton != NULL);
17017 }
17018 else if (target_attr->form_is_block ())
17019 {
17020 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17021 baton->property_type = die_type (target_die, target_cu);
17022 baton->locexpr.per_cu = cu->per_cu;
17023 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17024 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17025 baton->locexpr.is_reference = true;
17026 prop->data.baton = baton;
17027 prop->kind = PROP_LOCEXPR;
17028 gdb_assert (prop->data.baton != NULL);
17029 }
17030 else
17031 {
17032 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17033 "dynamic property");
17034 return 0;
17035 }
17036 break;
17037 case DW_AT_data_member_location:
17038 {
17039 LONGEST offset;
17040
17041 if (!handle_data_member_location (target_die, target_cu,
17042 &offset))
17043 return 0;
17044
17045 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17046 baton->property_type = read_type_die (target_die->parent,
17047 target_cu);
17048 baton->offset_info.offset = offset;
17049 baton->offset_info.type = die_type (target_die, target_cu);
17050 prop->data.baton = baton;
17051 prop->kind = PROP_ADDR_OFFSET;
17052 break;
17053 }
17054 }
17055 }
17056 else if (attr->form_is_constant ())
17057 {
17058 prop->data.const_val = attr->constant_value (0);
17059 prop->kind = PROP_CONST;
17060 }
17061 else
17062 {
17063 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17064 dwarf2_name (die, cu));
17065 return 0;
17066 }
17067
17068 return 1;
17069 }
17070
17071 /* See read.h. */
17072
17073 struct type *
17074 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17075 {
17076 struct objfile *objfile = dwarf2_per_objfile->objfile;
17077 struct type *int_type;
17078
17079 /* Helper macro to examine the various builtin types. */
17080 #define TRY_TYPE(F) \
17081 int_type = (unsigned_p \
17082 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17083 : objfile_type (objfile)->builtin_ ## F); \
17084 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17085 return int_type
17086
17087 TRY_TYPE (char);
17088 TRY_TYPE (short);
17089 TRY_TYPE (int);
17090 TRY_TYPE (long);
17091 TRY_TYPE (long_long);
17092
17093 #undef TRY_TYPE
17094
17095 gdb_assert_not_reached ("unable to find suitable integer type");
17096 }
17097
17098 /* See read.h. */
17099
17100 struct type *
17101 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17102 {
17103 int addr_size = this->addr_size ();
17104 return int_type (addr_size, unsigned_p);
17105 }
17106
17107 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17108 present (which is valid) then compute the default type based on the
17109 compilation units address size. */
17110
17111 static struct type *
17112 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17113 {
17114 struct type *index_type = die_type (die, cu);
17115
17116 /* Dwarf-2 specifications explicitly allows to create subrange types
17117 without specifying a base type.
17118 In that case, the base type must be set to the type of
17119 the lower bound, upper bound or count, in that order, if any of these
17120 three attributes references an object that has a type.
17121 If no base type is found, the Dwarf-2 specifications say that
17122 a signed integer type of size equal to the size of an address should
17123 be used.
17124 For the following C code: `extern char gdb_int [];'
17125 GCC produces an empty range DIE.
17126 FIXME: muller/2010-05-28: Possible references to object for low bound,
17127 high bound or count are not yet handled by this code. */
17128 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17129 index_type = cu->per_cu->addr_sized_int_type (false);
17130
17131 return index_type;
17132 }
17133
17134 /* Read the given DW_AT_subrange DIE. */
17135
17136 static struct type *
17137 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17138 {
17139 struct type *base_type, *orig_base_type;
17140 struct type *range_type;
17141 struct attribute *attr;
17142 struct dynamic_prop low, high;
17143 int low_default_is_valid;
17144 int high_bound_is_count = 0;
17145 const char *name;
17146 ULONGEST negative_mask;
17147
17148 orig_base_type = read_subrange_index_type (die, cu);
17149
17150 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17151 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17152 creating the range type, but we use the result of check_typedef
17153 when examining properties of the type. */
17154 base_type = check_typedef (orig_base_type);
17155
17156 /* The die_type call above may have already set the type for this DIE. */
17157 range_type = get_die_type (die, cu);
17158 if (range_type)
17159 return range_type;
17160
17161 low.kind = PROP_CONST;
17162 high.kind = PROP_CONST;
17163 high.data.const_val = 0;
17164
17165 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17166 omitting DW_AT_lower_bound. */
17167 switch (cu->language)
17168 {
17169 case language_c:
17170 case language_cplus:
17171 low.data.const_val = 0;
17172 low_default_is_valid = 1;
17173 break;
17174 case language_fortran:
17175 low.data.const_val = 1;
17176 low_default_is_valid = 1;
17177 break;
17178 case language_d:
17179 case language_objc:
17180 case language_rust:
17181 low.data.const_val = 0;
17182 low_default_is_valid = (cu->header.version >= 4);
17183 break;
17184 case language_ada:
17185 case language_m2:
17186 case language_pascal:
17187 low.data.const_val = 1;
17188 low_default_is_valid = (cu->header.version >= 4);
17189 break;
17190 default:
17191 low.data.const_val = 0;
17192 low_default_is_valid = 0;
17193 break;
17194 }
17195
17196 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17197 if (attr != nullptr)
17198 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17199 else if (!low_default_is_valid)
17200 complaint (_("Missing DW_AT_lower_bound "
17201 "- DIE at %s [in module %s]"),
17202 sect_offset_str (die->sect_off),
17203 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17204
17205 struct attribute *attr_ub, *attr_count;
17206 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17207 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17208 {
17209 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17210 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17211 {
17212 /* If bounds are constant do the final calculation here. */
17213 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17214 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17215 else
17216 high_bound_is_count = 1;
17217 }
17218 else
17219 {
17220 if (attr_ub != NULL)
17221 complaint (_("Unresolved DW_AT_upper_bound "
17222 "- DIE at %s [in module %s]"),
17223 sect_offset_str (die->sect_off),
17224 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17225 if (attr_count != NULL)
17226 complaint (_("Unresolved DW_AT_count "
17227 "- DIE at %s [in module %s]"),
17228 sect_offset_str (die->sect_off),
17229 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17230 }
17231 }
17232
17233 LONGEST bias = 0;
17234 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17235 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17236 bias = bias_attr->constant_value (0);
17237
17238 /* Normally, the DWARF producers are expected to use a signed
17239 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17240 But this is unfortunately not always the case, as witnessed
17241 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17242 is used instead. To work around that ambiguity, we treat
17243 the bounds as signed, and thus sign-extend their values, when
17244 the base type is signed. */
17245 negative_mask =
17246 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17247 if (low.kind == PROP_CONST
17248 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17249 low.data.const_val |= negative_mask;
17250 if (high.kind == PROP_CONST
17251 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17252 high.data.const_val |= negative_mask;
17253
17254 /* Check for bit and byte strides. */
17255 struct dynamic_prop byte_stride_prop;
17256 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17257 if (attr_byte_stride != nullptr)
17258 {
17259 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17260 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17261 prop_type);
17262 }
17263
17264 struct dynamic_prop bit_stride_prop;
17265 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17266 if (attr_bit_stride != nullptr)
17267 {
17268 /* It only makes sense to have either a bit or byte stride. */
17269 if (attr_byte_stride != nullptr)
17270 {
17271 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17272 "- DIE at %s [in module %s]"),
17273 sect_offset_str (die->sect_off),
17274 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17275 attr_bit_stride = nullptr;
17276 }
17277 else
17278 {
17279 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17280 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17281 prop_type);
17282 }
17283 }
17284
17285 if (attr_byte_stride != nullptr
17286 || attr_bit_stride != nullptr)
17287 {
17288 bool byte_stride_p = (attr_byte_stride != nullptr);
17289 struct dynamic_prop *stride
17290 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17291
17292 range_type
17293 = create_range_type_with_stride (NULL, orig_base_type, &low,
17294 &high, bias, stride, byte_stride_p);
17295 }
17296 else
17297 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17298
17299 if (high_bound_is_count)
17300 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17301
17302 /* Ada expects an empty array on no boundary attributes. */
17303 if (attr == NULL && cu->language != language_ada)
17304 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17305
17306 name = dwarf2_name (die, cu);
17307 if (name)
17308 TYPE_NAME (range_type) = name;
17309
17310 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17311 if (attr != nullptr)
17312 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17313
17314 maybe_set_alignment (cu, die, range_type);
17315
17316 set_die_type (die, range_type, cu);
17317
17318 /* set_die_type should be already done. */
17319 set_descriptive_type (range_type, die, cu);
17320
17321 return range_type;
17322 }
17323
17324 static struct type *
17325 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17326 {
17327 struct type *type;
17328
17329 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17330 NULL);
17331 TYPE_NAME (type) = dwarf2_name (die, cu);
17332
17333 /* In Ada, an unspecified type is typically used when the description
17334 of the type is deferred to a different unit. When encountering
17335 such a type, we treat it as a stub, and try to resolve it later on,
17336 when needed. */
17337 if (cu->language == language_ada)
17338 TYPE_STUB (type) = 1;
17339
17340 return set_die_type (die, type, cu);
17341 }
17342
17343 /* Read a single die and all its descendents. Set the die's sibling
17344 field to NULL; set other fields in the die correctly, and set all
17345 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17346 location of the info_ptr after reading all of those dies. PARENT
17347 is the parent of the die in question. */
17348
17349 static struct die_info *
17350 read_die_and_children (const struct die_reader_specs *reader,
17351 const gdb_byte *info_ptr,
17352 const gdb_byte **new_info_ptr,
17353 struct die_info *parent)
17354 {
17355 struct die_info *die;
17356 const gdb_byte *cur_ptr;
17357
17358 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17359 if (die == NULL)
17360 {
17361 *new_info_ptr = cur_ptr;
17362 return NULL;
17363 }
17364 store_in_ref_table (die, reader->cu);
17365
17366 if (die->has_children)
17367 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17368 else
17369 {
17370 die->child = NULL;
17371 *new_info_ptr = cur_ptr;
17372 }
17373
17374 die->sibling = NULL;
17375 die->parent = parent;
17376 return die;
17377 }
17378
17379 /* Read a die, all of its descendents, and all of its siblings; set
17380 all of the fields of all of the dies correctly. Arguments are as
17381 in read_die_and_children. */
17382
17383 static struct die_info *
17384 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17385 const gdb_byte *info_ptr,
17386 const gdb_byte **new_info_ptr,
17387 struct die_info *parent)
17388 {
17389 struct die_info *first_die, *last_sibling;
17390 const gdb_byte *cur_ptr;
17391
17392 cur_ptr = info_ptr;
17393 first_die = last_sibling = NULL;
17394
17395 while (1)
17396 {
17397 struct die_info *die
17398 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17399
17400 if (die == NULL)
17401 {
17402 *new_info_ptr = cur_ptr;
17403 return first_die;
17404 }
17405
17406 if (!first_die)
17407 first_die = die;
17408 else
17409 last_sibling->sibling = die;
17410
17411 last_sibling = die;
17412 }
17413 }
17414
17415 /* Read a die, all of its descendents, and all of its siblings; set
17416 all of the fields of all of the dies correctly. Arguments are as
17417 in read_die_and_children.
17418 This the main entry point for reading a DIE and all its children. */
17419
17420 static struct die_info *
17421 read_die_and_siblings (const struct die_reader_specs *reader,
17422 const gdb_byte *info_ptr,
17423 const gdb_byte **new_info_ptr,
17424 struct die_info *parent)
17425 {
17426 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17427 new_info_ptr, parent);
17428
17429 if (dwarf_die_debug)
17430 {
17431 fprintf_unfiltered (gdb_stdlog,
17432 "Read die from %s@0x%x of %s:\n",
17433 reader->die_section->get_name (),
17434 (unsigned) (info_ptr - reader->die_section->buffer),
17435 bfd_get_filename (reader->abfd));
17436 dump_die (die, dwarf_die_debug);
17437 }
17438
17439 return die;
17440 }
17441
17442 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17443 attributes.
17444 The caller is responsible for filling in the extra attributes
17445 and updating (*DIEP)->num_attrs.
17446 Set DIEP to point to a newly allocated die with its information,
17447 except for its child, sibling, and parent fields. */
17448
17449 static const gdb_byte *
17450 read_full_die_1 (const struct die_reader_specs *reader,
17451 struct die_info **diep, const gdb_byte *info_ptr,
17452 int num_extra_attrs)
17453 {
17454 unsigned int abbrev_number, bytes_read, i;
17455 struct abbrev_info *abbrev;
17456 struct die_info *die;
17457 struct dwarf2_cu *cu = reader->cu;
17458 bfd *abfd = reader->abfd;
17459
17460 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17461 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17462 info_ptr += bytes_read;
17463 if (!abbrev_number)
17464 {
17465 *diep = NULL;
17466 return info_ptr;
17467 }
17468
17469 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17470 if (!abbrev)
17471 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17472 abbrev_number,
17473 bfd_get_filename (abfd));
17474
17475 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17476 die->sect_off = sect_off;
17477 die->tag = abbrev->tag;
17478 die->abbrev = abbrev_number;
17479 die->has_children = abbrev->has_children;
17480
17481 /* Make the result usable.
17482 The caller needs to update num_attrs after adding the extra
17483 attributes. */
17484 die->num_attrs = abbrev->num_attrs;
17485
17486 std::vector<int> indexes_that_need_reprocess;
17487 for (i = 0; i < abbrev->num_attrs; ++i)
17488 {
17489 bool need_reprocess;
17490 info_ptr =
17491 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17492 info_ptr, &need_reprocess);
17493 if (need_reprocess)
17494 indexes_that_need_reprocess.push_back (i);
17495 }
17496
17497 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17498 if (attr != nullptr)
17499 cu->str_offsets_base = DW_UNSND (attr);
17500
17501 auto maybe_addr_base = die->addr_base ();
17502 if (maybe_addr_base.has_value ())
17503 cu->addr_base = *maybe_addr_base;
17504 for (int index : indexes_that_need_reprocess)
17505 read_attribute_reprocess (reader, &die->attrs[index]);
17506 *diep = die;
17507 return info_ptr;
17508 }
17509
17510 /* Read a die and all its attributes.
17511 Set DIEP to point to a newly allocated die with its information,
17512 except for its child, sibling, and parent fields. */
17513
17514 static const gdb_byte *
17515 read_full_die (const struct die_reader_specs *reader,
17516 struct die_info **diep, const gdb_byte *info_ptr)
17517 {
17518 const gdb_byte *result;
17519
17520 result = read_full_die_1 (reader, diep, info_ptr, 0);
17521
17522 if (dwarf_die_debug)
17523 {
17524 fprintf_unfiltered (gdb_stdlog,
17525 "Read die from %s@0x%x of %s:\n",
17526 reader->die_section->get_name (),
17527 (unsigned) (info_ptr - reader->die_section->buffer),
17528 bfd_get_filename (reader->abfd));
17529 dump_die (*diep, dwarf_die_debug);
17530 }
17531
17532 return result;
17533 }
17534 \f
17535
17536 /* Returns nonzero if TAG represents a type that we might generate a partial
17537 symbol for. */
17538
17539 static int
17540 is_type_tag_for_partial (int tag)
17541 {
17542 switch (tag)
17543 {
17544 #if 0
17545 /* Some types that would be reasonable to generate partial symbols for,
17546 that we don't at present. */
17547 case DW_TAG_array_type:
17548 case DW_TAG_file_type:
17549 case DW_TAG_ptr_to_member_type:
17550 case DW_TAG_set_type:
17551 case DW_TAG_string_type:
17552 case DW_TAG_subroutine_type:
17553 #endif
17554 case DW_TAG_base_type:
17555 case DW_TAG_class_type:
17556 case DW_TAG_interface_type:
17557 case DW_TAG_enumeration_type:
17558 case DW_TAG_structure_type:
17559 case DW_TAG_subrange_type:
17560 case DW_TAG_typedef:
17561 case DW_TAG_union_type:
17562 return 1;
17563 default:
17564 return 0;
17565 }
17566 }
17567
17568 /* Load all DIEs that are interesting for partial symbols into memory. */
17569
17570 static struct partial_die_info *
17571 load_partial_dies (const struct die_reader_specs *reader,
17572 const gdb_byte *info_ptr, int building_psymtab)
17573 {
17574 struct dwarf2_cu *cu = reader->cu;
17575 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17576 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17577 unsigned int bytes_read;
17578 unsigned int load_all = 0;
17579 int nesting_level = 1;
17580
17581 parent_die = NULL;
17582 last_die = NULL;
17583
17584 gdb_assert (cu->per_cu != NULL);
17585 if (cu->per_cu->load_all_dies)
17586 load_all = 1;
17587
17588 cu->partial_dies
17589 = htab_create_alloc_ex (cu->header.length / 12,
17590 partial_die_hash,
17591 partial_die_eq,
17592 NULL,
17593 &cu->comp_unit_obstack,
17594 hashtab_obstack_allocate,
17595 dummy_obstack_deallocate);
17596
17597 while (1)
17598 {
17599 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17600
17601 /* A NULL abbrev means the end of a series of children. */
17602 if (abbrev == NULL)
17603 {
17604 if (--nesting_level == 0)
17605 return first_die;
17606
17607 info_ptr += bytes_read;
17608 last_die = parent_die;
17609 parent_die = parent_die->die_parent;
17610 continue;
17611 }
17612
17613 /* Check for template arguments. We never save these; if
17614 they're seen, we just mark the parent, and go on our way. */
17615 if (parent_die != NULL
17616 && cu->language == language_cplus
17617 && (abbrev->tag == DW_TAG_template_type_param
17618 || abbrev->tag == DW_TAG_template_value_param))
17619 {
17620 parent_die->has_template_arguments = 1;
17621
17622 if (!load_all)
17623 {
17624 /* We don't need a partial DIE for the template argument. */
17625 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17626 continue;
17627 }
17628 }
17629
17630 /* We only recurse into c++ subprograms looking for template arguments.
17631 Skip their other children. */
17632 if (!load_all
17633 && cu->language == language_cplus
17634 && parent_die != NULL
17635 && parent_die->tag == DW_TAG_subprogram)
17636 {
17637 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17638 continue;
17639 }
17640
17641 /* Check whether this DIE is interesting enough to save. Normally
17642 we would not be interested in members here, but there may be
17643 later variables referencing them via DW_AT_specification (for
17644 static members). */
17645 if (!load_all
17646 && !is_type_tag_for_partial (abbrev->tag)
17647 && abbrev->tag != DW_TAG_constant
17648 && abbrev->tag != DW_TAG_enumerator
17649 && abbrev->tag != DW_TAG_subprogram
17650 && abbrev->tag != DW_TAG_inlined_subroutine
17651 && abbrev->tag != DW_TAG_lexical_block
17652 && abbrev->tag != DW_TAG_variable
17653 && abbrev->tag != DW_TAG_namespace
17654 && abbrev->tag != DW_TAG_module
17655 && abbrev->tag != DW_TAG_member
17656 && abbrev->tag != DW_TAG_imported_unit
17657 && abbrev->tag != DW_TAG_imported_declaration)
17658 {
17659 /* Otherwise we skip to the next sibling, if any. */
17660 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17661 continue;
17662 }
17663
17664 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17665 abbrev);
17666
17667 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17668
17669 /* This two-pass algorithm for processing partial symbols has a
17670 high cost in cache pressure. Thus, handle some simple cases
17671 here which cover the majority of C partial symbols. DIEs
17672 which neither have specification tags in them, nor could have
17673 specification tags elsewhere pointing at them, can simply be
17674 processed and discarded.
17675
17676 This segment is also optional; scan_partial_symbols and
17677 add_partial_symbol will handle these DIEs if we chain
17678 them in normally. When compilers which do not emit large
17679 quantities of duplicate debug information are more common,
17680 this code can probably be removed. */
17681
17682 /* Any complete simple types at the top level (pretty much all
17683 of them, for a language without namespaces), can be processed
17684 directly. */
17685 if (parent_die == NULL
17686 && pdi.has_specification == 0
17687 && pdi.is_declaration == 0
17688 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17689 || pdi.tag == DW_TAG_base_type
17690 || pdi.tag == DW_TAG_subrange_type))
17691 {
17692 if (building_psymtab && pdi.name != NULL)
17693 add_psymbol_to_list (pdi.name, false,
17694 VAR_DOMAIN, LOC_TYPEDEF, -1,
17695 psymbol_placement::STATIC,
17696 0, cu->language, objfile);
17697 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17698 continue;
17699 }
17700
17701 /* The exception for DW_TAG_typedef with has_children above is
17702 a workaround of GCC PR debug/47510. In the case of this complaint
17703 type_name_or_error will error on such types later.
17704
17705 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17706 it could not find the child DIEs referenced later, this is checked
17707 above. In correct DWARF DW_TAG_typedef should have no children. */
17708
17709 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17710 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17711 "- DIE at %s [in module %s]"),
17712 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17713
17714 /* If we're at the second level, and we're an enumerator, and
17715 our parent has no specification (meaning possibly lives in a
17716 namespace elsewhere), then we can add the partial symbol now
17717 instead of queueing it. */
17718 if (pdi.tag == DW_TAG_enumerator
17719 && parent_die != NULL
17720 && parent_die->die_parent == NULL
17721 && parent_die->tag == DW_TAG_enumeration_type
17722 && parent_die->has_specification == 0)
17723 {
17724 if (pdi.name == NULL)
17725 complaint (_("malformed enumerator DIE ignored"));
17726 else if (building_psymtab)
17727 add_psymbol_to_list (pdi.name, false,
17728 VAR_DOMAIN, LOC_CONST, -1,
17729 cu->language == language_cplus
17730 ? psymbol_placement::GLOBAL
17731 : psymbol_placement::STATIC,
17732 0, cu->language, objfile);
17733
17734 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17735 continue;
17736 }
17737
17738 struct partial_die_info *part_die
17739 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17740
17741 /* We'll save this DIE so link it in. */
17742 part_die->die_parent = parent_die;
17743 part_die->die_sibling = NULL;
17744 part_die->die_child = NULL;
17745
17746 if (last_die && last_die == parent_die)
17747 last_die->die_child = part_die;
17748 else if (last_die)
17749 last_die->die_sibling = part_die;
17750
17751 last_die = part_die;
17752
17753 if (first_die == NULL)
17754 first_die = part_die;
17755
17756 /* Maybe add the DIE to the hash table. Not all DIEs that we
17757 find interesting need to be in the hash table, because we
17758 also have the parent/sibling/child chains; only those that we
17759 might refer to by offset later during partial symbol reading.
17760
17761 For now this means things that might have be the target of a
17762 DW_AT_specification, DW_AT_abstract_origin, or
17763 DW_AT_extension. DW_AT_extension will refer only to
17764 namespaces; DW_AT_abstract_origin refers to functions (and
17765 many things under the function DIE, but we do not recurse
17766 into function DIEs during partial symbol reading) and
17767 possibly variables as well; DW_AT_specification refers to
17768 declarations. Declarations ought to have the DW_AT_declaration
17769 flag. It happens that GCC forgets to put it in sometimes, but
17770 only for functions, not for types.
17771
17772 Adding more things than necessary to the hash table is harmless
17773 except for the performance cost. Adding too few will result in
17774 wasted time in find_partial_die, when we reread the compilation
17775 unit with load_all_dies set. */
17776
17777 if (load_all
17778 || abbrev->tag == DW_TAG_constant
17779 || abbrev->tag == DW_TAG_subprogram
17780 || abbrev->tag == DW_TAG_variable
17781 || abbrev->tag == DW_TAG_namespace
17782 || part_die->is_declaration)
17783 {
17784 void **slot;
17785
17786 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17787 to_underlying (part_die->sect_off),
17788 INSERT);
17789 *slot = part_die;
17790 }
17791
17792 /* For some DIEs we want to follow their children (if any). For C
17793 we have no reason to follow the children of structures; for other
17794 languages we have to, so that we can get at method physnames
17795 to infer fully qualified class names, for DW_AT_specification,
17796 and for C++ template arguments. For C++, we also look one level
17797 inside functions to find template arguments (if the name of the
17798 function does not already contain the template arguments).
17799
17800 For Ada and Fortran, we need to scan the children of subprograms
17801 and lexical blocks as well because these languages allow the
17802 definition of nested entities that could be interesting for the
17803 debugger, such as nested subprograms for instance. */
17804 if (last_die->has_children
17805 && (load_all
17806 || last_die->tag == DW_TAG_namespace
17807 || last_die->tag == DW_TAG_module
17808 || last_die->tag == DW_TAG_enumeration_type
17809 || (cu->language == language_cplus
17810 && last_die->tag == DW_TAG_subprogram
17811 && (last_die->name == NULL
17812 || strchr (last_die->name, '<') == NULL))
17813 || (cu->language != language_c
17814 && (last_die->tag == DW_TAG_class_type
17815 || last_die->tag == DW_TAG_interface_type
17816 || last_die->tag == DW_TAG_structure_type
17817 || last_die->tag == DW_TAG_union_type))
17818 || ((cu->language == language_ada
17819 || cu->language == language_fortran)
17820 && (last_die->tag == DW_TAG_subprogram
17821 || last_die->tag == DW_TAG_lexical_block))))
17822 {
17823 nesting_level++;
17824 parent_die = last_die;
17825 continue;
17826 }
17827
17828 /* Otherwise we skip to the next sibling, if any. */
17829 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17830
17831 /* Back to the top, do it again. */
17832 }
17833 }
17834
17835 partial_die_info::partial_die_info (sect_offset sect_off_,
17836 struct abbrev_info *abbrev)
17837 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17838 {
17839 }
17840
17841 /* Read a minimal amount of information into the minimal die structure.
17842 INFO_PTR should point just after the initial uleb128 of a DIE. */
17843
17844 const gdb_byte *
17845 partial_die_info::read (const struct die_reader_specs *reader,
17846 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17847 {
17848 struct dwarf2_cu *cu = reader->cu;
17849 struct dwarf2_per_objfile *dwarf2_per_objfile
17850 = cu->per_cu->dwarf2_per_objfile;
17851 unsigned int i;
17852 int has_low_pc_attr = 0;
17853 int has_high_pc_attr = 0;
17854 int high_pc_relative = 0;
17855
17856 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17857 for (i = 0; i < abbrev.num_attrs; ++i)
17858 {
17859 bool need_reprocess;
17860 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17861 info_ptr, &need_reprocess);
17862 /* String and address offsets that need to do the reprocessing have
17863 already been read at this point, so there is no need to wait until
17864 the loop terminates to do the reprocessing. */
17865 if (need_reprocess)
17866 read_attribute_reprocess (reader, &attr_vec[i]);
17867 attribute &attr = attr_vec[i];
17868 /* Store the data if it is of an attribute we want to keep in a
17869 partial symbol table. */
17870 switch (attr.name)
17871 {
17872 case DW_AT_name:
17873 switch (tag)
17874 {
17875 case DW_TAG_compile_unit:
17876 case DW_TAG_partial_unit:
17877 case DW_TAG_type_unit:
17878 /* Compilation units have a DW_AT_name that is a filename, not
17879 a source language identifier. */
17880 case DW_TAG_enumeration_type:
17881 case DW_TAG_enumerator:
17882 /* These tags always have simple identifiers already; no need
17883 to canonicalize them. */
17884 name = DW_STRING (&attr);
17885 break;
17886 default:
17887 {
17888 struct objfile *objfile = dwarf2_per_objfile->objfile;
17889
17890 name
17891 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
17892 }
17893 break;
17894 }
17895 break;
17896 case DW_AT_linkage_name:
17897 case DW_AT_MIPS_linkage_name:
17898 /* Note that both forms of linkage name might appear. We
17899 assume they will be the same, and we only store the last
17900 one we see. */
17901 linkage_name = DW_STRING (&attr);
17902 break;
17903 case DW_AT_low_pc:
17904 has_low_pc_attr = 1;
17905 lowpc = attr.value_as_address ();
17906 break;
17907 case DW_AT_high_pc:
17908 has_high_pc_attr = 1;
17909 highpc = attr.value_as_address ();
17910 if (cu->header.version >= 4 && attr.form_is_constant ())
17911 high_pc_relative = 1;
17912 break;
17913 case DW_AT_location:
17914 /* Support the .debug_loc offsets. */
17915 if (attr.form_is_block ())
17916 {
17917 d.locdesc = DW_BLOCK (&attr);
17918 }
17919 else if (attr.form_is_section_offset ())
17920 {
17921 dwarf2_complex_location_expr_complaint ();
17922 }
17923 else
17924 {
17925 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17926 "partial symbol information");
17927 }
17928 break;
17929 case DW_AT_external:
17930 is_external = DW_UNSND (&attr);
17931 break;
17932 case DW_AT_declaration:
17933 is_declaration = DW_UNSND (&attr);
17934 break;
17935 case DW_AT_type:
17936 has_type = 1;
17937 break;
17938 case DW_AT_abstract_origin:
17939 case DW_AT_specification:
17940 case DW_AT_extension:
17941 has_specification = 1;
17942 spec_offset = attr.get_ref_die_offset ();
17943 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17944 || cu->per_cu->is_dwz);
17945 break;
17946 case DW_AT_sibling:
17947 /* Ignore absolute siblings, they might point outside of
17948 the current compile unit. */
17949 if (attr.form == DW_FORM_ref_addr)
17950 complaint (_("ignoring absolute DW_AT_sibling"));
17951 else
17952 {
17953 const gdb_byte *buffer = reader->buffer;
17954 sect_offset off = attr.get_ref_die_offset ();
17955 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
17956
17957 if (sibling_ptr < info_ptr)
17958 complaint (_("DW_AT_sibling points backwards"));
17959 else if (sibling_ptr > reader->buffer_end)
17960 reader->die_section->overflow_complaint ();
17961 else
17962 sibling = sibling_ptr;
17963 }
17964 break;
17965 case DW_AT_byte_size:
17966 has_byte_size = 1;
17967 break;
17968 case DW_AT_const_value:
17969 has_const_value = 1;
17970 break;
17971 case DW_AT_calling_convention:
17972 /* DWARF doesn't provide a way to identify a program's source-level
17973 entry point. DW_AT_calling_convention attributes are only meant
17974 to describe functions' calling conventions.
17975
17976 However, because it's a necessary piece of information in
17977 Fortran, and before DWARF 4 DW_CC_program was the only
17978 piece of debugging information whose definition refers to
17979 a 'main program' at all, several compilers marked Fortran
17980 main programs with DW_CC_program --- even when those
17981 functions use the standard calling conventions.
17982
17983 Although DWARF now specifies a way to provide this
17984 information, we support this practice for backward
17985 compatibility. */
17986 if (DW_UNSND (&attr) == DW_CC_program
17987 && cu->language == language_fortran)
17988 main_subprogram = 1;
17989 break;
17990 case DW_AT_inline:
17991 if (DW_UNSND (&attr) == DW_INL_inlined
17992 || DW_UNSND (&attr) == DW_INL_declared_inlined)
17993 may_be_inlined = 1;
17994 break;
17995
17996 case DW_AT_import:
17997 if (tag == DW_TAG_imported_unit)
17998 {
17999 d.sect_off = attr.get_ref_die_offset ();
18000 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18001 || cu->per_cu->is_dwz);
18002 }
18003 break;
18004
18005 case DW_AT_main_subprogram:
18006 main_subprogram = DW_UNSND (&attr);
18007 break;
18008
18009 case DW_AT_ranges:
18010 {
18011 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18012 but that requires a full DIE, so instead we just
18013 reimplement it. */
18014 int need_ranges_base = tag != DW_TAG_compile_unit;
18015 unsigned int ranges_offset = (DW_UNSND (&attr)
18016 + (need_ranges_base
18017 ? cu->ranges_base
18018 : 0));
18019
18020 /* Value of the DW_AT_ranges attribute is the offset in the
18021 .debug_ranges section. */
18022 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18023 nullptr))
18024 has_pc_info = 1;
18025 }
18026 break;
18027
18028 default:
18029 break;
18030 }
18031 }
18032
18033 /* For Ada, if both the name and the linkage name appear, we prefer
18034 the latter. This lets "catch exception" work better, regardless
18035 of the order in which the name and linkage name were emitted.
18036 Really, though, this is just a workaround for the fact that gdb
18037 doesn't store both the name and the linkage name. */
18038 if (cu->language == language_ada && linkage_name != nullptr)
18039 name = linkage_name;
18040
18041 if (high_pc_relative)
18042 highpc += lowpc;
18043
18044 if (has_low_pc_attr && has_high_pc_attr)
18045 {
18046 /* When using the GNU linker, .gnu.linkonce. sections are used to
18047 eliminate duplicate copies of functions and vtables and such.
18048 The linker will arbitrarily choose one and discard the others.
18049 The AT_*_pc values for such functions refer to local labels in
18050 these sections. If the section from that file was discarded, the
18051 labels are not in the output, so the relocs get a value of 0.
18052 If this is a discarded function, mark the pc bounds as invalid,
18053 so that GDB will ignore it. */
18054 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18055 {
18056 struct objfile *objfile = dwarf2_per_objfile->objfile;
18057 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18058
18059 complaint (_("DW_AT_low_pc %s is zero "
18060 "for DIE at %s [in module %s]"),
18061 paddress (gdbarch, lowpc),
18062 sect_offset_str (sect_off),
18063 objfile_name (objfile));
18064 }
18065 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18066 else if (lowpc >= highpc)
18067 {
18068 struct objfile *objfile = dwarf2_per_objfile->objfile;
18069 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18070
18071 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18072 "for DIE at %s [in module %s]"),
18073 paddress (gdbarch, lowpc),
18074 paddress (gdbarch, highpc),
18075 sect_offset_str (sect_off),
18076 objfile_name (objfile));
18077 }
18078 else
18079 has_pc_info = 1;
18080 }
18081
18082 return info_ptr;
18083 }
18084
18085 /* Find a cached partial DIE at OFFSET in CU. */
18086
18087 struct partial_die_info *
18088 dwarf2_cu::find_partial_die (sect_offset sect_off)
18089 {
18090 struct partial_die_info *lookup_die = NULL;
18091 struct partial_die_info part_die (sect_off);
18092
18093 lookup_die = ((struct partial_die_info *)
18094 htab_find_with_hash (partial_dies, &part_die,
18095 to_underlying (sect_off)));
18096
18097 return lookup_die;
18098 }
18099
18100 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18101 except in the case of .debug_types DIEs which do not reference
18102 outside their CU (they do however referencing other types via
18103 DW_FORM_ref_sig8). */
18104
18105 static const struct cu_partial_die_info
18106 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18107 {
18108 struct dwarf2_per_objfile *dwarf2_per_objfile
18109 = cu->per_cu->dwarf2_per_objfile;
18110 struct objfile *objfile = dwarf2_per_objfile->objfile;
18111 struct dwarf2_per_cu_data *per_cu = NULL;
18112 struct partial_die_info *pd = NULL;
18113
18114 if (offset_in_dwz == cu->per_cu->is_dwz
18115 && cu->header.offset_in_cu_p (sect_off))
18116 {
18117 pd = cu->find_partial_die (sect_off);
18118 if (pd != NULL)
18119 return { cu, pd };
18120 /* We missed recording what we needed.
18121 Load all dies and try again. */
18122 per_cu = cu->per_cu;
18123 }
18124 else
18125 {
18126 /* TUs don't reference other CUs/TUs (except via type signatures). */
18127 if (cu->per_cu->is_debug_types)
18128 {
18129 error (_("Dwarf Error: Type Unit at offset %s contains"
18130 " external reference to offset %s [in module %s].\n"),
18131 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18132 bfd_get_filename (objfile->obfd));
18133 }
18134 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18135 dwarf2_per_objfile);
18136
18137 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18138 load_partial_comp_unit (per_cu);
18139
18140 per_cu->cu->last_used = 0;
18141 pd = per_cu->cu->find_partial_die (sect_off);
18142 }
18143
18144 /* If we didn't find it, and not all dies have been loaded,
18145 load them all and try again. */
18146
18147 if (pd == NULL && per_cu->load_all_dies == 0)
18148 {
18149 per_cu->load_all_dies = 1;
18150
18151 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18152 THIS_CU->cu may already be in use. So we can't just free it and
18153 replace its DIEs with the ones we read in. Instead, we leave those
18154 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18155 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18156 set. */
18157 load_partial_comp_unit (per_cu);
18158
18159 pd = per_cu->cu->find_partial_die (sect_off);
18160 }
18161
18162 if (pd == NULL)
18163 internal_error (__FILE__, __LINE__,
18164 _("could not find partial DIE %s "
18165 "in cache [from module %s]\n"),
18166 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18167 return { per_cu->cu, pd };
18168 }
18169
18170 /* See if we can figure out if the class lives in a namespace. We do
18171 this by looking for a member function; its demangled name will
18172 contain namespace info, if there is any. */
18173
18174 static void
18175 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18176 struct dwarf2_cu *cu)
18177 {
18178 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18179 what template types look like, because the demangler
18180 frequently doesn't give the same name as the debug info. We
18181 could fix this by only using the demangled name to get the
18182 prefix (but see comment in read_structure_type). */
18183
18184 struct partial_die_info *real_pdi;
18185 struct partial_die_info *child_pdi;
18186
18187 /* If this DIE (this DIE's specification, if any) has a parent, then
18188 we should not do this. We'll prepend the parent's fully qualified
18189 name when we create the partial symbol. */
18190
18191 real_pdi = struct_pdi;
18192 while (real_pdi->has_specification)
18193 {
18194 auto res = find_partial_die (real_pdi->spec_offset,
18195 real_pdi->spec_is_dwz, cu);
18196 real_pdi = res.pdi;
18197 cu = res.cu;
18198 }
18199
18200 if (real_pdi->die_parent != NULL)
18201 return;
18202
18203 for (child_pdi = struct_pdi->die_child;
18204 child_pdi != NULL;
18205 child_pdi = child_pdi->die_sibling)
18206 {
18207 if (child_pdi->tag == DW_TAG_subprogram
18208 && child_pdi->linkage_name != NULL)
18209 {
18210 gdb::unique_xmalloc_ptr<char> actual_class_name
18211 (language_class_name_from_physname (cu->language_defn,
18212 child_pdi->linkage_name));
18213 if (actual_class_name != NULL)
18214 {
18215 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18216 struct_pdi->name = objfile->intern (actual_class_name.get ());
18217 }
18218 break;
18219 }
18220 }
18221 }
18222
18223 void
18224 partial_die_info::fixup (struct dwarf2_cu *cu)
18225 {
18226 /* Once we've fixed up a die, there's no point in doing so again.
18227 This also avoids a memory leak if we were to call
18228 guess_partial_die_structure_name multiple times. */
18229 if (fixup_called)
18230 return;
18231
18232 /* If we found a reference attribute and the DIE has no name, try
18233 to find a name in the referred to DIE. */
18234
18235 if (name == NULL && has_specification)
18236 {
18237 struct partial_die_info *spec_die;
18238
18239 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18240 spec_die = res.pdi;
18241 cu = res.cu;
18242
18243 spec_die->fixup (cu);
18244
18245 if (spec_die->name)
18246 {
18247 name = spec_die->name;
18248
18249 /* Copy DW_AT_external attribute if it is set. */
18250 if (spec_die->is_external)
18251 is_external = spec_die->is_external;
18252 }
18253 }
18254
18255 /* Set default names for some unnamed DIEs. */
18256
18257 if (name == NULL && tag == DW_TAG_namespace)
18258 name = CP_ANONYMOUS_NAMESPACE_STR;
18259
18260 /* If there is no parent die to provide a namespace, and there are
18261 children, see if we can determine the namespace from their linkage
18262 name. */
18263 if (cu->language == language_cplus
18264 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18265 && die_parent == NULL
18266 && has_children
18267 && (tag == DW_TAG_class_type
18268 || tag == DW_TAG_structure_type
18269 || tag == DW_TAG_union_type))
18270 guess_partial_die_structure_name (this, cu);
18271
18272 /* GCC might emit a nameless struct or union that has a linkage
18273 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18274 if (name == NULL
18275 && (tag == DW_TAG_class_type
18276 || tag == DW_TAG_interface_type
18277 || tag == DW_TAG_structure_type
18278 || tag == DW_TAG_union_type)
18279 && linkage_name != NULL)
18280 {
18281 gdb::unique_xmalloc_ptr<char> demangled
18282 (gdb_demangle (linkage_name, DMGL_TYPES));
18283 if (demangled != nullptr)
18284 {
18285 const char *base;
18286
18287 /* Strip any leading namespaces/classes, keep only the base name.
18288 DW_AT_name for named DIEs does not contain the prefixes. */
18289 base = strrchr (demangled.get (), ':');
18290 if (base && base > demangled.get () && base[-1] == ':')
18291 base++;
18292 else
18293 base = demangled.get ();
18294
18295 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18296 name = objfile->intern (base);
18297 }
18298 }
18299
18300 fixup_called = 1;
18301 }
18302
18303 /* Process the attributes that had to be skipped in the first round. These
18304 attributes are the ones that need str_offsets_base or addr_base attributes.
18305 They could not have been processed in the first round, because at the time
18306 the values of str_offsets_base or addr_base may not have been known. */
18307 static void
18308 read_attribute_reprocess (const struct die_reader_specs *reader,
18309 struct attribute *attr)
18310 {
18311 struct dwarf2_cu *cu = reader->cu;
18312 switch (attr->form)
18313 {
18314 case DW_FORM_addrx:
18315 case DW_FORM_GNU_addr_index:
18316 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18317 break;
18318 case DW_FORM_strx:
18319 case DW_FORM_strx1:
18320 case DW_FORM_strx2:
18321 case DW_FORM_strx3:
18322 case DW_FORM_strx4:
18323 case DW_FORM_GNU_str_index:
18324 {
18325 unsigned int str_index = DW_UNSND (attr);
18326 if (reader->dwo_file != NULL)
18327 {
18328 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18329 DW_STRING_IS_CANONICAL (attr) = 0;
18330 }
18331 else
18332 {
18333 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18334 DW_STRING_IS_CANONICAL (attr) = 0;
18335 }
18336 break;
18337 }
18338 default:
18339 gdb_assert_not_reached (_("Unexpected DWARF form."));
18340 }
18341 }
18342
18343 /* Read an attribute value described by an attribute form. */
18344
18345 static const gdb_byte *
18346 read_attribute_value (const struct die_reader_specs *reader,
18347 struct attribute *attr, unsigned form,
18348 LONGEST implicit_const, const gdb_byte *info_ptr,
18349 bool *need_reprocess)
18350 {
18351 struct dwarf2_cu *cu = reader->cu;
18352 struct dwarf2_per_objfile *dwarf2_per_objfile
18353 = cu->per_cu->dwarf2_per_objfile;
18354 struct objfile *objfile = dwarf2_per_objfile->objfile;
18355 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18356 bfd *abfd = reader->abfd;
18357 struct comp_unit_head *cu_header = &cu->header;
18358 unsigned int bytes_read;
18359 struct dwarf_block *blk;
18360 *need_reprocess = false;
18361
18362 attr->form = (enum dwarf_form) form;
18363 switch (form)
18364 {
18365 case DW_FORM_ref_addr:
18366 if (cu->header.version == 2)
18367 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18368 &bytes_read);
18369 else
18370 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18371 &bytes_read);
18372 info_ptr += bytes_read;
18373 break;
18374 case DW_FORM_GNU_ref_alt:
18375 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18376 info_ptr += bytes_read;
18377 break;
18378 case DW_FORM_addr:
18379 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18380 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18381 info_ptr += bytes_read;
18382 break;
18383 case DW_FORM_block2:
18384 blk = dwarf_alloc_block (cu);
18385 blk->size = read_2_bytes (abfd, info_ptr);
18386 info_ptr += 2;
18387 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18388 info_ptr += blk->size;
18389 DW_BLOCK (attr) = blk;
18390 break;
18391 case DW_FORM_block4:
18392 blk = dwarf_alloc_block (cu);
18393 blk->size = read_4_bytes (abfd, info_ptr);
18394 info_ptr += 4;
18395 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18396 info_ptr += blk->size;
18397 DW_BLOCK (attr) = blk;
18398 break;
18399 case DW_FORM_data2:
18400 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18401 info_ptr += 2;
18402 break;
18403 case DW_FORM_data4:
18404 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18405 info_ptr += 4;
18406 break;
18407 case DW_FORM_data8:
18408 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18409 info_ptr += 8;
18410 break;
18411 case DW_FORM_data16:
18412 blk = dwarf_alloc_block (cu);
18413 blk->size = 16;
18414 blk->data = read_n_bytes (abfd, info_ptr, 16);
18415 info_ptr += 16;
18416 DW_BLOCK (attr) = blk;
18417 break;
18418 case DW_FORM_sec_offset:
18419 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18420 info_ptr += bytes_read;
18421 break;
18422 case DW_FORM_string:
18423 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18424 DW_STRING_IS_CANONICAL (attr) = 0;
18425 info_ptr += bytes_read;
18426 break;
18427 case DW_FORM_strp:
18428 if (!cu->per_cu->is_dwz)
18429 {
18430 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18431 abfd, info_ptr, cu_header,
18432 &bytes_read);
18433 DW_STRING_IS_CANONICAL (attr) = 0;
18434 info_ptr += bytes_read;
18435 break;
18436 }
18437 /* FALLTHROUGH */
18438 case DW_FORM_line_strp:
18439 if (!cu->per_cu->is_dwz)
18440 {
18441 DW_STRING (attr)
18442 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18443 &bytes_read);
18444 DW_STRING_IS_CANONICAL (attr) = 0;
18445 info_ptr += bytes_read;
18446 break;
18447 }
18448 /* FALLTHROUGH */
18449 case DW_FORM_GNU_strp_alt:
18450 {
18451 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18452 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18453 &bytes_read);
18454
18455 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18456 DW_STRING_IS_CANONICAL (attr) = 0;
18457 info_ptr += bytes_read;
18458 }
18459 break;
18460 case DW_FORM_exprloc:
18461 case DW_FORM_block:
18462 blk = dwarf_alloc_block (cu);
18463 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18464 info_ptr += bytes_read;
18465 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18466 info_ptr += blk->size;
18467 DW_BLOCK (attr) = blk;
18468 break;
18469 case DW_FORM_block1:
18470 blk = dwarf_alloc_block (cu);
18471 blk->size = read_1_byte (abfd, info_ptr);
18472 info_ptr += 1;
18473 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18474 info_ptr += blk->size;
18475 DW_BLOCK (attr) = blk;
18476 break;
18477 case DW_FORM_data1:
18478 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18479 info_ptr += 1;
18480 break;
18481 case DW_FORM_flag:
18482 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18483 info_ptr += 1;
18484 break;
18485 case DW_FORM_flag_present:
18486 DW_UNSND (attr) = 1;
18487 break;
18488 case DW_FORM_sdata:
18489 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18490 info_ptr += bytes_read;
18491 break;
18492 case DW_FORM_udata:
18493 case DW_FORM_rnglistx:
18494 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18495 info_ptr += bytes_read;
18496 break;
18497 case DW_FORM_ref1:
18498 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18499 + read_1_byte (abfd, info_ptr));
18500 info_ptr += 1;
18501 break;
18502 case DW_FORM_ref2:
18503 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18504 + read_2_bytes (abfd, info_ptr));
18505 info_ptr += 2;
18506 break;
18507 case DW_FORM_ref4:
18508 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18509 + read_4_bytes (abfd, info_ptr));
18510 info_ptr += 4;
18511 break;
18512 case DW_FORM_ref8:
18513 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18514 + read_8_bytes (abfd, info_ptr));
18515 info_ptr += 8;
18516 break;
18517 case DW_FORM_ref_sig8:
18518 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18519 info_ptr += 8;
18520 break;
18521 case DW_FORM_ref_udata:
18522 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18523 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18524 info_ptr += bytes_read;
18525 break;
18526 case DW_FORM_indirect:
18527 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18528 info_ptr += bytes_read;
18529 if (form == DW_FORM_implicit_const)
18530 {
18531 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18532 info_ptr += bytes_read;
18533 }
18534 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18535 info_ptr, need_reprocess);
18536 break;
18537 case DW_FORM_implicit_const:
18538 DW_SND (attr) = implicit_const;
18539 break;
18540 case DW_FORM_addrx:
18541 case DW_FORM_GNU_addr_index:
18542 *need_reprocess = true;
18543 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18544 info_ptr += bytes_read;
18545 break;
18546 case DW_FORM_strx:
18547 case DW_FORM_strx1:
18548 case DW_FORM_strx2:
18549 case DW_FORM_strx3:
18550 case DW_FORM_strx4:
18551 case DW_FORM_GNU_str_index:
18552 {
18553 ULONGEST str_index;
18554 if (form == DW_FORM_strx1)
18555 {
18556 str_index = read_1_byte (abfd, info_ptr);
18557 info_ptr += 1;
18558 }
18559 else if (form == DW_FORM_strx2)
18560 {
18561 str_index = read_2_bytes (abfd, info_ptr);
18562 info_ptr += 2;
18563 }
18564 else if (form == DW_FORM_strx3)
18565 {
18566 str_index = read_3_bytes (abfd, info_ptr);
18567 info_ptr += 3;
18568 }
18569 else if (form == DW_FORM_strx4)
18570 {
18571 str_index = read_4_bytes (abfd, info_ptr);
18572 info_ptr += 4;
18573 }
18574 else
18575 {
18576 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18577 info_ptr += bytes_read;
18578 }
18579 *need_reprocess = true;
18580 DW_UNSND (attr) = str_index;
18581 }
18582 break;
18583 default:
18584 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18585 dwarf_form_name (form),
18586 bfd_get_filename (abfd));
18587 }
18588
18589 /* Super hack. */
18590 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18591 attr->form = DW_FORM_GNU_ref_alt;
18592
18593 /* We have seen instances where the compiler tried to emit a byte
18594 size attribute of -1 which ended up being encoded as an unsigned
18595 0xffffffff. Although 0xffffffff is technically a valid size value,
18596 an object of this size seems pretty unlikely so we can relatively
18597 safely treat these cases as if the size attribute was invalid and
18598 treat them as zero by default. */
18599 if (attr->name == DW_AT_byte_size
18600 && form == DW_FORM_data4
18601 && DW_UNSND (attr) >= 0xffffffff)
18602 {
18603 complaint
18604 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18605 hex_string (DW_UNSND (attr)));
18606 DW_UNSND (attr) = 0;
18607 }
18608
18609 return info_ptr;
18610 }
18611
18612 /* Read an attribute described by an abbreviated attribute. */
18613
18614 static const gdb_byte *
18615 read_attribute (const struct die_reader_specs *reader,
18616 struct attribute *attr, struct attr_abbrev *abbrev,
18617 const gdb_byte *info_ptr, bool *need_reprocess)
18618 {
18619 attr->name = abbrev->name;
18620 return read_attribute_value (reader, attr, abbrev->form,
18621 abbrev->implicit_const, info_ptr,
18622 need_reprocess);
18623 }
18624
18625 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18626
18627 static const char *
18628 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18629 LONGEST str_offset)
18630 {
18631 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18632 str_offset, "DW_FORM_strp");
18633 }
18634
18635 /* Return pointer to string at .debug_str offset as read from BUF.
18636 BUF is assumed to be in a compilation unit described by CU_HEADER.
18637 Return *BYTES_READ_PTR count of bytes read from BUF. */
18638
18639 static const char *
18640 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18641 const gdb_byte *buf,
18642 const struct comp_unit_head *cu_header,
18643 unsigned int *bytes_read_ptr)
18644 {
18645 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18646
18647 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
18648 }
18649
18650 /* See read.h. */
18651
18652 const char *
18653 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
18654 const struct comp_unit_head *cu_header,
18655 unsigned int *bytes_read_ptr)
18656 {
18657 bfd *abfd = objfile->obfd;
18658 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18659
18660 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
18661 }
18662
18663 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18664 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18665 ADDR_SIZE is the size of addresses from the CU header. */
18666
18667 static CORE_ADDR
18668 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18669 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18670 int addr_size)
18671 {
18672 struct objfile *objfile = dwarf2_per_objfile->objfile;
18673 bfd *abfd = objfile->obfd;
18674 const gdb_byte *info_ptr;
18675 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18676
18677 dwarf2_per_objfile->addr.read (objfile);
18678 if (dwarf2_per_objfile->addr.buffer == NULL)
18679 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18680 objfile_name (objfile));
18681 if (addr_base_or_zero + addr_index * addr_size
18682 >= dwarf2_per_objfile->addr.size)
18683 error (_("DW_FORM_addr_index pointing outside of "
18684 ".debug_addr section [in module %s]"),
18685 objfile_name (objfile));
18686 info_ptr = (dwarf2_per_objfile->addr.buffer
18687 + addr_base_or_zero + addr_index * addr_size);
18688 if (addr_size == 4)
18689 return bfd_get_32 (abfd, info_ptr);
18690 else
18691 return bfd_get_64 (abfd, info_ptr);
18692 }
18693
18694 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18695
18696 static CORE_ADDR
18697 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18698 {
18699 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18700 cu->addr_base, cu->header.addr_size);
18701 }
18702
18703 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18704
18705 static CORE_ADDR
18706 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18707 unsigned int *bytes_read)
18708 {
18709 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18710 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18711
18712 return read_addr_index (cu, addr_index);
18713 }
18714
18715 /* See read.h. */
18716
18717 CORE_ADDR
18718 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18719 {
18720 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18721 struct dwarf2_cu *cu = per_cu->cu;
18722 gdb::optional<ULONGEST> addr_base;
18723 int addr_size;
18724
18725 /* We need addr_base and addr_size.
18726 If we don't have PER_CU->cu, we have to get it.
18727 Nasty, but the alternative is storing the needed info in PER_CU,
18728 which at this point doesn't seem justified: it's not clear how frequently
18729 it would get used and it would increase the size of every PER_CU.
18730 Entry points like dwarf2_per_cu_addr_size do a similar thing
18731 so we're not in uncharted territory here.
18732 Alas we need to be a bit more complicated as addr_base is contained
18733 in the DIE.
18734
18735 We don't need to read the entire CU(/TU).
18736 We just need the header and top level die.
18737
18738 IWBN to use the aging mechanism to let us lazily later discard the CU.
18739 For now we skip this optimization. */
18740
18741 if (cu != NULL)
18742 {
18743 addr_base = cu->addr_base;
18744 addr_size = cu->header.addr_size;
18745 }
18746 else
18747 {
18748 cutu_reader reader (per_cu, NULL, 0, false);
18749 addr_base = reader.cu->addr_base;
18750 addr_size = reader.cu->header.addr_size;
18751 }
18752
18753 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18754 addr_size);
18755 }
18756
18757 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18758 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18759 DWO file. */
18760
18761 static const char *
18762 read_str_index (struct dwarf2_cu *cu,
18763 struct dwarf2_section_info *str_section,
18764 struct dwarf2_section_info *str_offsets_section,
18765 ULONGEST str_offsets_base, ULONGEST str_index)
18766 {
18767 struct dwarf2_per_objfile *dwarf2_per_objfile
18768 = cu->per_cu->dwarf2_per_objfile;
18769 struct objfile *objfile = dwarf2_per_objfile->objfile;
18770 const char *objf_name = objfile_name (objfile);
18771 bfd *abfd = objfile->obfd;
18772 const gdb_byte *info_ptr;
18773 ULONGEST str_offset;
18774 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18775
18776 str_section->read (objfile);
18777 str_offsets_section->read (objfile);
18778 if (str_section->buffer == NULL)
18779 error (_("%s used without %s section"
18780 " in CU at offset %s [in module %s]"),
18781 form_name, str_section->get_name (),
18782 sect_offset_str (cu->header.sect_off), objf_name);
18783 if (str_offsets_section->buffer == NULL)
18784 error (_("%s used without %s section"
18785 " in CU at offset %s [in module %s]"),
18786 form_name, str_section->get_name (),
18787 sect_offset_str (cu->header.sect_off), objf_name);
18788 info_ptr = (str_offsets_section->buffer
18789 + str_offsets_base
18790 + str_index * cu->header.offset_size);
18791 if (cu->header.offset_size == 4)
18792 str_offset = bfd_get_32 (abfd, info_ptr);
18793 else
18794 str_offset = bfd_get_64 (abfd, info_ptr);
18795 if (str_offset >= str_section->size)
18796 error (_("Offset from %s pointing outside of"
18797 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18798 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18799 return (const char *) (str_section->buffer + str_offset);
18800 }
18801
18802 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18803
18804 static const char *
18805 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18806 {
18807 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18808 ? reader->cu->header.addr_size : 0;
18809 return read_str_index (reader->cu,
18810 &reader->dwo_file->sections.str,
18811 &reader->dwo_file->sections.str_offsets,
18812 str_offsets_base, str_index);
18813 }
18814
18815 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18816
18817 static const char *
18818 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18819 {
18820 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18821 const char *objf_name = objfile_name (objfile);
18822 static const char form_name[] = "DW_FORM_GNU_str_index";
18823 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18824
18825 if (!cu->str_offsets_base.has_value ())
18826 error (_("%s used in Fission stub without %s"
18827 " in CU at offset 0x%lx [in module %s]"),
18828 form_name, str_offsets_attr_name,
18829 (long) cu->header.offset_size, objf_name);
18830
18831 return read_str_index (cu,
18832 &cu->per_cu->dwarf2_per_objfile->str,
18833 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18834 *cu->str_offsets_base, str_index);
18835 }
18836
18837 /* Return the length of an LEB128 number in BUF. */
18838
18839 static int
18840 leb128_size (const gdb_byte *buf)
18841 {
18842 const gdb_byte *begin = buf;
18843 gdb_byte byte;
18844
18845 while (1)
18846 {
18847 byte = *buf++;
18848 if ((byte & 128) == 0)
18849 return buf - begin;
18850 }
18851 }
18852
18853 static void
18854 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18855 {
18856 switch (lang)
18857 {
18858 case DW_LANG_C89:
18859 case DW_LANG_C99:
18860 case DW_LANG_C11:
18861 case DW_LANG_C:
18862 case DW_LANG_UPC:
18863 cu->language = language_c;
18864 break;
18865 case DW_LANG_Java:
18866 case DW_LANG_C_plus_plus:
18867 case DW_LANG_C_plus_plus_11:
18868 case DW_LANG_C_plus_plus_14:
18869 cu->language = language_cplus;
18870 break;
18871 case DW_LANG_D:
18872 cu->language = language_d;
18873 break;
18874 case DW_LANG_Fortran77:
18875 case DW_LANG_Fortran90:
18876 case DW_LANG_Fortran95:
18877 case DW_LANG_Fortran03:
18878 case DW_LANG_Fortran08:
18879 cu->language = language_fortran;
18880 break;
18881 case DW_LANG_Go:
18882 cu->language = language_go;
18883 break;
18884 case DW_LANG_Mips_Assembler:
18885 cu->language = language_asm;
18886 break;
18887 case DW_LANG_Ada83:
18888 case DW_LANG_Ada95:
18889 cu->language = language_ada;
18890 break;
18891 case DW_LANG_Modula2:
18892 cu->language = language_m2;
18893 break;
18894 case DW_LANG_Pascal83:
18895 cu->language = language_pascal;
18896 break;
18897 case DW_LANG_ObjC:
18898 cu->language = language_objc;
18899 break;
18900 case DW_LANG_Rust:
18901 case DW_LANG_Rust_old:
18902 cu->language = language_rust;
18903 break;
18904 case DW_LANG_Cobol74:
18905 case DW_LANG_Cobol85:
18906 default:
18907 cu->language = language_minimal;
18908 break;
18909 }
18910 cu->language_defn = language_def (cu->language);
18911 }
18912
18913 /* Return the named attribute or NULL if not there. */
18914
18915 static struct attribute *
18916 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18917 {
18918 for (;;)
18919 {
18920 unsigned int i;
18921 struct attribute *spec = NULL;
18922
18923 for (i = 0; i < die->num_attrs; ++i)
18924 {
18925 if (die->attrs[i].name == name)
18926 return &die->attrs[i];
18927 if (die->attrs[i].name == DW_AT_specification
18928 || die->attrs[i].name == DW_AT_abstract_origin)
18929 spec = &die->attrs[i];
18930 }
18931
18932 if (!spec)
18933 break;
18934
18935 die = follow_die_ref (die, spec, &cu);
18936 }
18937
18938 return NULL;
18939 }
18940
18941 /* Return the string associated with a string-typed attribute, or NULL if it
18942 is either not found or is of an incorrect type. */
18943
18944 static const char *
18945 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18946 {
18947 struct attribute *attr;
18948 const char *str = NULL;
18949
18950 attr = dwarf2_attr (die, name, cu);
18951
18952 if (attr != NULL)
18953 {
18954 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
18955 || attr->form == DW_FORM_string
18956 || attr->form == DW_FORM_strx
18957 || attr->form == DW_FORM_strx1
18958 || attr->form == DW_FORM_strx2
18959 || attr->form == DW_FORM_strx3
18960 || attr->form == DW_FORM_strx4
18961 || attr->form == DW_FORM_GNU_str_index
18962 || attr->form == DW_FORM_GNU_strp_alt)
18963 str = DW_STRING (attr);
18964 else
18965 complaint (_("string type expected for attribute %s for "
18966 "DIE at %s in module %s"),
18967 dwarf_attr_name (name), sect_offset_str (die->sect_off),
18968 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18969 }
18970
18971 return str;
18972 }
18973
18974 /* Return the dwo name or NULL if not present. If present, it is in either
18975 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
18976 static const char *
18977 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
18978 {
18979 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
18980 if (dwo_name == nullptr)
18981 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
18982 return dwo_name;
18983 }
18984
18985 /* Return non-zero iff the attribute NAME is defined for the given DIE,
18986 and holds a non-zero value. This function should only be used for
18987 DW_FORM_flag or DW_FORM_flag_present attributes. */
18988
18989 static int
18990 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
18991 {
18992 struct attribute *attr = dwarf2_attr (die, name, cu);
18993
18994 return (attr && DW_UNSND (attr));
18995 }
18996
18997 static int
18998 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
18999 {
19000 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19001 which value is non-zero. However, we have to be careful with
19002 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19003 (via dwarf2_flag_true_p) follows this attribute. So we may
19004 end up accidently finding a declaration attribute that belongs
19005 to a different DIE referenced by the specification attribute,
19006 even though the given DIE does not have a declaration attribute. */
19007 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19008 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19009 }
19010
19011 /* Return the die giving the specification for DIE, if there is
19012 one. *SPEC_CU is the CU containing DIE on input, and the CU
19013 containing the return value on output. If there is no
19014 specification, but there is an abstract origin, that is
19015 returned. */
19016
19017 static struct die_info *
19018 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19019 {
19020 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19021 *spec_cu);
19022
19023 if (spec_attr == NULL)
19024 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19025
19026 if (spec_attr == NULL)
19027 return NULL;
19028 else
19029 return follow_die_ref (die, spec_attr, spec_cu);
19030 }
19031
19032 /* Stub for free_line_header to match void * callback types. */
19033
19034 static void
19035 free_line_header_voidp (void *arg)
19036 {
19037 struct line_header *lh = (struct line_header *) arg;
19038
19039 delete lh;
19040 }
19041
19042 /* A convenience function to find the proper .debug_line section for a CU. */
19043
19044 static struct dwarf2_section_info *
19045 get_debug_line_section (struct dwarf2_cu *cu)
19046 {
19047 struct dwarf2_section_info *section;
19048 struct dwarf2_per_objfile *dwarf2_per_objfile
19049 = cu->per_cu->dwarf2_per_objfile;
19050
19051 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19052 DWO file. */
19053 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19054 section = &cu->dwo_unit->dwo_file->sections.line;
19055 else if (cu->per_cu->is_dwz)
19056 {
19057 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19058
19059 section = &dwz->line;
19060 }
19061 else
19062 section = &dwarf2_per_objfile->line;
19063
19064 return section;
19065 }
19066
19067 /* Read the statement program header starting at OFFSET in
19068 .debug_line, or .debug_line.dwo. Return a pointer
19069 to a struct line_header, allocated using xmalloc.
19070 Returns NULL if there is a problem reading the header, e.g., if it
19071 has a version we don't understand.
19072
19073 NOTE: the strings in the include directory and file name tables of
19074 the returned object point into the dwarf line section buffer,
19075 and must not be freed. */
19076
19077 static line_header_up
19078 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19079 {
19080 struct dwarf2_section_info *section;
19081 struct dwarf2_per_objfile *dwarf2_per_objfile
19082 = cu->per_cu->dwarf2_per_objfile;
19083
19084 section = get_debug_line_section (cu);
19085 section->read (dwarf2_per_objfile->objfile);
19086 if (section->buffer == NULL)
19087 {
19088 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19089 complaint (_("missing .debug_line.dwo section"));
19090 else
19091 complaint (_("missing .debug_line section"));
19092 return 0;
19093 }
19094
19095 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19096 dwarf2_per_objfile, section,
19097 &cu->header);
19098 }
19099
19100 /* Subroutine of dwarf_decode_lines to simplify it.
19101 Return the file name of the psymtab for the given file_entry.
19102 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19103 If space for the result is malloc'd, *NAME_HOLDER will be set.
19104 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19105
19106 static const char *
19107 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19108 const dwarf2_psymtab *pst,
19109 const char *comp_dir,
19110 gdb::unique_xmalloc_ptr<char> *name_holder)
19111 {
19112 const char *include_name = fe.name;
19113 const char *include_name_to_compare = include_name;
19114 const char *pst_filename;
19115 int file_is_pst;
19116
19117 const char *dir_name = fe.include_dir (lh);
19118
19119 gdb::unique_xmalloc_ptr<char> hold_compare;
19120 if (!IS_ABSOLUTE_PATH (include_name)
19121 && (dir_name != NULL || comp_dir != NULL))
19122 {
19123 /* Avoid creating a duplicate psymtab for PST.
19124 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19125 Before we do the comparison, however, we need to account
19126 for DIR_NAME and COMP_DIR.
19127 First prepend dir_name (if non-NULL). If we still don't
19128 have an absolute path prepend comp_dir (if non-NULL).
19129 However, the directory we record in the include-file's
19130 psymtab does not contain COMP_DIR (to match the
19131 corresponding symtab(s)).
19132
19133 Example:
19134
19135 bash$ cd /tmp
19136 bash$ gcc -g ./hello.c
19137 include_name = "hello.c"
19138 dir_name = "."
19139 DW_AT_comp_dir = comp_dir = "/tmp"
19140 DW_AT_name = "./hello.c"
19141
19142 */
19143
19144 if (dir_name != NULL)
19145 {
19146 name_holder->reset (concat (dir_name, SLASH_STRING,
19147 include_name, (char *) NULL));
19148 include_name = name_holder->get ();
19149 include_name_to_compare = include_name;
19150 }
19151 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19152 {
19153 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19154 include_name, (char *) NULL));
19155 include_name_to_compare = hold_compare.get ();
19156 }
19157 }
19158
19159 pst_filename = pst->filename;
19160 gdb::unique_xmalloc_ptr<char> copied_name;
19161 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19162 {
19163 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19164 pst_filename, (char *) NULL));
19165 pst_filename = copied_name.get ();
19166 }
19167
19168 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19169
19170 if (file_is_pst)
19171 return NULL;
19172 return include_name;
19173 }
19174
19175 /* State machine to track the state of the line number program. */
19176
19177 class lnp_state_machine
19178 {
19179 public:
19180 /* Initialize a machine state for the start of a line number
19181 program. */
19182 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19183 bool record_lines_p);
19184
19185 file_entry *current_file ()
19186 {
19187 /* lh->file_names is 0-based, but the file name numbers in the
19188 statement program are 1-based. */
19189 return m_line_header->file_name_at (m_file);
19190 }
19191
19192 /* Record the line in the state machine. END_SEQUENCE is true if
19193 we're processing the end of a sequence. */
19194 void record_line (bool end_sequence);
19195
19196 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19197 nop-out rest of the lines in this sequence. */
19198 void check_line_address (struct dwarf2_cu *cu,
19199 const gdb_byte *line_ptr,
19200 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19201
19202 void handle_set_discriminator (unsigned int discriminator)
19203 {
19204 m_discriminator = discriminator;
19205 m_line_has_non_zero_discriminator |= discriminator != 0;
19206 }
19207
19208 /* Handle DW_LNE_set_address. */
19209 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19210 {
19211 m_op_index = 0;
19212 address += baseaddr;
19213 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19214 }
19215
19216 /* Handle DW_LNS_advance_pc. */
19217 void handle_advance_pc (CORE_ADDR adjust);
19218
19219 /* Handle a special opcode. */
19220 void handle_special_opcode (unsigned char op_code);
19221
19222 /* Handle DW_LNS_advance_line. */
19223 void handle_advance_line (int line_delta)
19224 {
19225 advance_line (line_delta);
19226 }
19227
19228 /* Handle DW_LNS_set_file. */
19229 void handle_set_file (file_name_index file);
19230
19231 /* Handle DW_LNS_negate_stmt. */
19232 void handle_negate_stmt ()
19233 {
19234 m_is_stmt = !m_is_stmt;
19235 }
19236
19237 /* Handle DW_LNS_const_add_pc. */
19238 void handle_const_add_pc ();
19239
19240 /* Handle DW_LNS_fixed_advance_pc. */
19241 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19242 {
19243 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19244 m_op_index = 0;
19245 }
19246
19247 /* Handle DW_LNS_copy. */
19248 void handle_copy ()
19249 {
19250 record_line (false);
19251 m_discriminator = 0;
19252 }
19253
19254 /* Handle DW_LNE_end_sequence. */
19255 void handle_end_sequence ()
19256 {
19257 m_currently_recording_lines = true;
19258 }
19259
19260 private:
19261 /* Advance the line by LINE_DELTA. */
19262 void advance_line (int line_delta)
19263 {
19264 m_line += line_delta;
19265
19266 if (line_delta != 0)
19267 m_line_has_non_zero_discriminator = m_discriminator != 0;
19268 }
19269
19270 struct dwarf2_cu *m_cu;
19271
19272 gdbarch *m_gdbarch;
19273
19274 /* True if we're recording lines.
19275 Otherwise we're building partial symtabs and are just interested in
19276 finding include files mentioned by the line number program. */
19277 bool m_record_lines_p;
19278
19279 /* The line number header. */
19280 line_header *m_line_header;
19281
19282 /* These are part of the standard DWARF line number state machine,
19283 and initialized according to the DWARF spec. */
19284
19285 unsigned char m_op_index = 0;
19286 /* The line table index of the current file. */
19287 file_name_index m_file = 1;
19288 unsigned int m_line = 1;
19289
19290 /* These are initialized in the constructor. */
19291
19292 CORE_ADDR m_address;
19293 bool m_is_stmt;
19294 unsigned int m_discriminator;
19295
19296 /* Additional bits of state we need to track. */
19297
19298 /* The last file that we called dwarf2_start_subfile for.
19299 This is only used for TLLs. */
19300 unsigned int m_last_file = 0;
19301 /* The last file a line number was recorded for. */
19302 struct subfile *m_last_subfile = NULL;
19303
19304 /* When true, record the lines we decode. */
19305 bool m_currently_recording_lines = false;
19306
19307 /* The last line number that was recorded, used to coalesce
19308 consecutive entries for the same line. This can happen, for
19309 example, when discriminators are present. PR 17276. */
19310 unsigned int m_last_line = 0;
19311 bool m_line_has_non_zero_discriminator = false;
19312 };
19313
19314 void
19315 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19316 {
19317 CORE_ADDR addr_adj = (((m_op_index + adjust)
19318 / m_line_header->maximum_ops_per_instruction)
19319 * m_line_header->minimum_instruction_length);
19320 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19321 m_op_index = ((m_op_index + adjust)
19322 % m_line_header->maximum_ops_per_instruction);
19323 }
19324
19325 void
19326 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19327 {
19328 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19329 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19330 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19331 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19332 / m_line_header->maximum_ops_per_instruction)
19333 * m_line_header->minimum_instruction_length);
19334 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19335 m_op_index = ((m_op_index + adj_opcode_d)
19336 % m_line_header->maximum_ops_per_instruction);
19337
19338 int line_delta = m_line_header->line_base + adj_opcode_r;
19339 advance_line (line_delta);
19340 record_line (false);
19341 m_discriminator = 0;
19342 }
19343
19344 void
19345 lnp_state_machine::handle_set_file (file_name_index file)
19346 {
19347 m_file = file;
19348
19349 const file_entry *fe = current_file ();
19350 if (fe == NULL)
19351 dwarf2_debug_line_missing_file_complaint ();
19352 else if (m_record_lines_p)
19353 {
19354 const char *dir = fe->include_dir (m_line_header);
19355
19356 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19357 m_line_has_non_zero_discriminator = m_discriminator != 0;
19358 dwarf2_start_subfile (m_cu, fe->name, dir);
19359 }
19360 }
19361
19362 void
19363 lnp_state_machine::handle_const_add_pc ()
19364 {
19365 CORE_ADDR adjust
19366 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19367
19368 CORE_ADDR addr_adj
19369 = (((m_op_index + adjust)
19370 / m_line_header->maximum_ops_per_instruction)
19371 * m_line_header->minimum_instruction_length);
19372
19373 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19374 m_op_index = ((m_op_index + adjust)
19375 % m_line_header->maximum_ops_per_instruction);
19376 }
19377
19378 /* Return non-zero if we should add LINE to the line number table.
19379 LINE is the line to add, LAST_LINE is the last line that was added,
19380 LAST_SUBFILE is the subfile for LAST_LINE.
19381 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19382 had a non-zero discriminator.
19383
19384 We have to be careful in the presence of discriminators.
19385 E.g., for this line:
19386
19387 for (i = 0; i < 100000; i++);
19388
19389 clang can emit four line number entries for that one line,
19390 each with a different discriminator.
19391 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19392
19393 However, we want gdb to coalesce all four entries into one.
19394 Otherwise the user could stepi into the middle of the line and
19395 gdb would get confused about whether the pc really was in the
19396 middle of the line.
19397
19398 Things are further complicated by the fact that two consecutive
19399 line number entries for the same line is a heuristic used by gcc
19400 to denote the end of the prologue. So we can't just discard duplicate
19401 entries, we have to be selective about it. The heuristic we use is
19402 that we only collapse consecutive entries for the same line if at least
19403 one of those entries has a non-zero discriminator. PR 17276.
19404
19405 Note: Addresses in the line number state machine can never go backwards
19406 within one sequence, thus this coalescing is ok. */
19407
19408 static int
19409 dwarf_record_line_p (struct dwarf2_cu *cu,
19410 unsigned int line, unsigned int last_line,
19411 int line_has_non_zero_discriminator,
19412 struct subfile *last_subfile)
19413 {
19414 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19415 return 1;
19416 if (line != last_line)
19417 return 1;
19418 /* Same line for the same file that we've seen already.
19419 As a last check, for pr 17276, only record the line if the line
19420 has never had a non-zero discriminator. */
19421 if (!line_has_non_zero_discriminator)
19422 return 1;
19423 return 0;
19424 }
19425
19426 /* Use the CU's builder to record line number LINE beginning at
19427 address ADDRESS in the line table of subfile SUBFILE. */
19428
19429 static void
19430 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19431 unsigned int line, CORE_ADDR address, bool is_stmt,
19432 struct dwarf2_cu *cu)
19433 {
19434 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19435
19436 if (dwarf_line_debug)
19437 {
19438 fprintf_unfiltered (gdb_stdlog,
19439 "Recording line %u, file %s, address %s\n",
19440 line, lbasename (subfile->name),
19441 paddress (gdbarch, address));
19442 }
19443
19444 if (cu != nullptr)
19445 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19446 }
19447
19448 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19449 Mark the end of a set of line number records.
19450 The arguments are the same as for dwarf_record_line_1.
19451 If SUBFILE is NULL the request is ignored. */
19452
19453 static void
19454 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19455 CORE_ADDR address, struct dwarf2_cu *cu)
19456 {
19457 if (subfile == NULL)
19458 return;
19459
19460 if (dwarf_line_debug)
19461 {
19462 fprintf_unfiltered (gdb_stdlog,
19463 "Finishing current line, file %s, address %s\n",
19464 lbasename (subfile->name),
19465 paddress (gdbarch, address));
19466 }
19467
19468 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19469 }
19470
19471 void
19472 lnp_state_machine::record_line (bool end_sequence)
19473 {
19474 if (dwarf_line_debug)
19475 {
19476 fprintf_unfiltered (gdb_stdlog,
19477 "Processing actual line %u: file %u,"
19478 " address %s, is_stmt %u, discrim %u%s\n",
19479 m_line, m_file,
19480 paddress (m_gdbarch, m_address),
19481 m_is_stmt, m_discriminator,
19482 (end_sequence ? "\t(end sequence)" : ""));
19483 }
19484
19485 file_entry *fe = current_file ();
19486
19487 if (fe == NULL)
19488 dwarf2_debug_line_missing_file_complaint ();
19489 /* For now we ignore lines not starting on an instruction boundary.
19490 But not when processing end_sequence for compatibility with the
19491 previous version of the code. */
19492 else if (m_op_index == 0 || end_sequence)
19493 {
19494 fe->included_p = 1;
19495 if (m_record_lines_p)
19496 {
19497 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19498 || end_sequence)
19499 {
19500 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19501 m_currently_recording_lines ? m_cu : nullptr);
19502 }
19503
19504 if (!end_sequence)
19505 {
19506 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19507
19508 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19509 m_line_has_non_zero_discriminator,
19510 m_last_subfile))
19511 {
19512 buildsym_compunit *builder = m_cu->get_builder ();
19513 dwarf_record_line_1 (m_gdbarch,
19514 builder->get_current_subfile (),
19515 m_line, m_address, is_stmt,
19516 m_currently_recording_lines ? m_cu : nullptr);
19517 }
19518 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19519 m_last_line = m_line;
19520 }
19521 }
19522 }
19523 }
19524
19525 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19526 line_header *lh, bool record_lines_p)
19527 {
19528 m_cu = cu;
19529 m_gdbarch = arch;
19530 m_record_lines_p = record_lines_p;
19531 m_line_header = lh;
19532
19533 m_currently_recording_lines = true;
19534
19535 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19536 was a line entry for it so that the backend has a chance to adjust it
19537 and also record it in case it needs it. This is currently used by MIPS
19538 code, cf. `mips_adjust_dwarf2_line'. */
19539 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19540 m_is_stmt = lh->default_is_stmt;
19541 m_discriminator = 0;
19542 }
19543
19544 void
19545 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19546 const gdb_byte *line_ptr,
19547 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
19548 {
19549 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19550 the pc range of the CU. However, we restrict the test to only ADDRESS
19551 values of zero to preserve GDB's previous behaviour which is to handle
19552 the specific case of a function being GC'd by the linker. */
19553
19554 if (address == 0 && address < unrelocated_lowpc)
19555 {
19556 /* This line table is for a function which has been
19557 GCd by the linker. Ignore it. PR gdb/12528 */
19558
19559 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19560 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19561
19562 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19563 line_offset, objfile_name (objfile));
19564 m_currently_recording_lines = false;
19565 /* Note: m_currently_recording_lines is left as false until we see
19566 DW_LNE_end_sequence. */
19567 }
19568 }
19569
19570 /* Subroutine of dwarf_decode_lines to simplify it.
19571 Process the line number information in LH.
19572 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19573 program in order to set included_p for every referenced header. */
19574
19575 static void
19576 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19577 const int decode_for_pst_p, CORE_ADDR lowpc)
19578 {
19579 const gdb_byte *line_ptr, *extended_end;
19580 const gdb_byte *line_end;
19581 unsigned int bytes_read, extended_len;
19582 unsigned char op_code, extended_op;
19583 CORE_ADDR baseaddr;
19584 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19585 bfd *abfd = objfile->obfd;
19586 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19587 /* True if we're recording line info (as opposed to building partial
19588 symtabs and just interested in finding include files mentioned by
19589 the line number program). */
19590 bool record_lines_p = !decode_for_pst_p;
19591
19592 baseaddr = objfile->text_section_offset ();
19593
19594 line_ptr = lh->statement_program_start;
19595 line_end = lh->statement_program_end;
19596
19597 /* Read the statement sequences until there's nothing left. */
19598 while (line_ptr < line_end)
19599 {
19600 /* The DWARF line number program state machine. Reset the state
19601 machine at the start of each sequence. */
19602 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
19603 bool end_sequence = false;
19604
19605 if (record_lines_p)
19606 {
19607 /* Start a subfile for the current file of the state
19608 machine. */
19609 const file_entry *fe = state_machine.current_file ();
19610
19611 if (fe != NULL)
19612 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
19613 }
19614
19615 /* Decode the table. */
19616 while (line_ptr < line_end && !end_sequence)
19617 {
19618 op_code = read_1_byte (abfd, line_ptr);
19619 line_ptr += 1;
19620
19621 if (op_code >= lh->opcode_base)
19622 {
19623 /* Special opcode. */
19624 state_machine.handle_special_opcode (op_code);
19625 }
19626 else switch (op_code)
19627 {
19628 case DW_LNS_extended_op:
19629 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19630 &bytes_read);
19631 line_ptr += bytes_read;
19632 extended_end = line_ptr + extended_len;
19633 extended_op = read_1_byte (abfd, line_ptr);
19634 line_ptr += 1;
19635 switch (extended_op)
19636 {
19637 case DW_LNE_end_sequence:
19638 state_machine.handle_end_sequence ();
19639 end_sequence = true;
19640 break;
19641 case DW_LNE_set_address:
19642 {
19643 CORE_ADDR address
19644 = cu->header.read_address (abfd, line_ptr, &bytes_read);
19645 line_ptr += bytes_read;
19646
19647 state_machine.check_line_address (cu, line_ptr,
19648 lowpc - baseaddr, address);
19649 state_machine.handle_set_address (baseaddr, address);
19650 }
19651 break;
19652 case DW_LNE_define_file:
19653 {
19654 const char *cur_file;
19655 unsigned int mod_time, length;
19656 dir_index dindex;
19657
19658 cur_file = read_direct_string (abfd, line_ptr,
19659 &bytes_read);
19660 line_ptr += bytes_read;
19661 dindex = (dir_index)
19662 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19663 line_ptr += bytes_read;
19664 mod_time =
19665 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19666 line_ptr += bytes_read;
19667 length =
19668 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19669 line_ptr += bytes_read;
19670 lh->add_file_name (cur_file, dindex, mod_time, length);
19671 }
19672 break;
19673 case DW_LNE_set_discriminator:
19674 {
19675 /* The discriminator is not interesting to the
19676 debugger; just ignore it. We still need to
19677 check its value though:
19678 if there are consecutive entries for the same
19679 (non-prologue) line we want to coalesce them.
19680 PR 17276. */
19681 unsigned int discr
19682 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19683 line_ptr += bytes_read;
19684
19685 state_machine.handle_set_discriminator (discr);
19686 }
19687 break;
19688 default:
19689 complaint (_("mangled .debug_line section"));
19690 return;
19691 }
19692 /* Make sure that we parsed the extended op correctly. If e.g.
19693 we expected a different address size than the producer used,
19694 we may have read the wrong number of bytes. */
19695 if (line_ptr != extended_end)
19696 {
19697 complaint (_("mangled .debug_line section"));
19698 return;
19699 }
19700 break;
19701 case DW_LNS_copy:
19702 state_machine.handle_copy ();
19703 break;
19704 case DW_LNS_advance_pc:
19705 {
19706 CORE_ADDR adjust
19707 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19708 line_ptr += bytes_read;
19709
19710 state_machine.handle_advance_pc (adjust);
19711 }
19712 break;
19713 case DW_LNS_advance_line:
19714 {
19715 int line_delta
19716 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19717 line_ptr += bytes_read;
19718
19719 state_machine.handle_advance_line (line_delta);
19720 }
19721 break;
19722 case DW_LNS_set_file:
19723 {
19724 file_name_index file
19725 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19726 &bytes_read);
19727 line_ptr += bytes_read;
19728
19729 state_machine.handle_set_file (file);
19730 }
19731 break;
19732 case DW_LNS_set_column:
19733 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19734 line_ptr += bytes_read;
19735 break;
19736 case DW_LNS_negate_stmt:
19737 state_machine.handle_negate_stmt ();
19738 break;
19739 case DW_LNS_set_basic_block:
19740 break;
19741 /* Add to the address register of the state machine the
19742 address increment value corresponding to special opcode
19743 255. I.e., this value is scaled by the minimum
19744 instruction length since special opcode 255 would have
19745 scaled the increment. */
19746 case DW_LNS_const_add_pc:
19747 state_machine.handle_const_add_pc ();
19748 break;
19749 case DW_LNS_fixed_advance_pc:
19750 {
19751 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19752 line_ptr += 2;
19753
19754 state_machine.handle_fixed_advance_pc (addr_adj);
19755 }
19756 break;
19757 default:
19758 {
19759 /* Unknown standard opcode, ignore it. */
19760 int i;
19761
19762 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19763 {
19764 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19765 line_ptr += bytes_read;
19766 }
19767 }
19768 }
19769 }
19770
19771 if (!end_sequence)
19772 dwarf2_debug_line_missing_end_sequence_complaint ();
19773
19774 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19775 in which case we still finish recording the last line). */
19776 state_machine.record_line (true);
19777 }
19778 }
19779
19780 /* Decode the Line Number Program (LNP) for the given line_header
19781 structure and CU. The actual information extracted and the type
19782 of structures created from the LNP depends on the value of PST.
19783
19784 1. If PST is NULL, then this procedure uses the data from the program
19785 to create all necessary symbol tables, and their linetables.
19786
19787 2. If PST is not NULL, this procedure reads the program to determine
19788 the list of files included by the unit represented by PST, and
19789 builds all the associated partial symbol tables.
19790
19791 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19792 It is used for relative paths in the line table.
19793 NOTE: When processing partial symtabs (pst != NULL),
19794 comp_dir == pst->dirname.
19795
19796 NOTE: It is important that psymtabs have the same file name (via strcmp)
19797 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19798 symtab we don't use it in the name of the psymtabs we create.
19799 E.g. expand_line_sal requires this when finding psymtabs to expand.
19800 A good testcase for this is mb-inline.exp.
19801
19802 LOWPC is the lowest address in CU (or 0 if not known).
19803
19804 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19805 for its PC<->lines mapping information. Otherwise only the filename
19806 table is read in. */
19807
19808 static void
19809 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
19810 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
19811 CORE_ADDR lowpc, int decode_mapping)
19812 {
19813 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19814 const int decode_for_pst_p = (pst != NULL);
19815
19816 if (decode_mapping)
19817 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
19818
19819 if (decode_for_pst_p)
19820 {
19821 /* Now that we're done scanning the Line Header Program, we can
19822 create the psymtab of each included file. */
19823 for (auto &file_entry : lh->file_names ())
19824 if (file_entry.included_p == 1)
19825 {
19826 gdb::unique_xmalloc_ptr<char> name_holder;
19827 const char *include_name =
19828 psymtab_include_file_name (lh, file_entry, pst,
19829 comp_dir, &name_holder);
19830 if (include_name != NULL)
19831 dwarf2_create_include_psymtab (include_name, pst, objfile);
19832 }
19833 }
19834 else
19835 {
19836 /* Make sure a symtab is created for every file, even files
19837 which contain only variables (i.e. no code with associated
19838 line numbers). */
19839 buildsym_compunit *builder = cu->get_builder ();
19840 struct compunit_symtab *cust = builder->get_compunit_symtab ();
19841
19842 for (auto &fe : lh->file_names ())
19843 {
19844 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
19845 if (builder->get_current_subfile ()->symtab == NULL)
19846 {
19847 builder->get_current_subfile ()->symtab
19848 = allocate_symtab (cust,
19849 builder->get_current_subfile ()->name);
19850 }
19851 fe.symtab = builder->get_current_subfile ()->symtab;
19852 }
19853 }
19854 }
19855
19856 /* Start a subfile for DWARF. FILENAME is the name of the file and
19857 DIRNAME the name of the source directory which contains FILENAME
19858 or NULL if not known.
19859 This routine tries to keep line numbers from identical absolute and
19860 relative file names in a common subfile.
19861
19862 Using the `list' example from the GDB testsuite, which resides in
19863 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19864 of /srcdir/list0.c yields the following debugging information for list0.c:
19865
19866 DW_AT_name: /srcdir/list0.c
19867 DW_AT_comp_dir: /compdir
19868 files.files[0].name: list0.h
19869 files.files[0].dir: /srcdir
19870 files.files[1].name: list0.c
19871 files.files[1].dir: /srcdir
19872
19873 The line number information for list0.c has to end up in a single
19874 subfile, so that `break /srcdir/list0.c:1' works as expected.
19875 start_subfile will ensure that this happens provided that we pass the
19876 concatenation of files.files[1].dir and files.files[1].name as the
19877 subfile's name. */
19878
19879 static void
19880 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
19881 const char *dirname)
19882 {
19883 gdb::unique_xmalloc_ptr<char> copy;
19884
19885 /* In order not to lose the line information directory,
19886 we concatenate it to the filename when it makes sense.
19887 Note that the Dwarf3 standard says (speaking of filenames in line
19888 information): ``The directory index is ignored for file names
19889 that represent full path names''. Thus ignoring dirname in the
19890 `else' branch below isn't an issue. */
19891
19892 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
19893 {
19894 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
19895 filename = copy.get ();
19896 }
19897
19898 cu->get_builder ()->start_subfile (filename);
19899 }
19900
19901 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
19902 buildsym_compunit constructor. */
19903
19904 struct compunit_symtab *
19905 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
19906 CORE_ADDR low_pc)
19907 {
19908 gdb_assert (m_builder == nullptr);
19909
19910 m_builder.reset (new struct buildsym_compunit
19911 (per_cu->dwarf2_per_objfile->objfile,
19912 name, comp_dir, language, low_pc));
19913
19914 list_in_scope = get_builder ()->get_file_symbols ();
19915
19916 get_builder ()->record_debugformat ("DWARF 2");
19917 get_builder ()->record_producer (producer);
19918
19919 processing_has_namespace_info = false;
19920
19921 return get_builder ()->get_compunit_symtab ();
19922 }
19923
19924 static void
19925 var_decode_location (struct attribute *attr, struct symbol *sym,
19926 struct dwarf2_cu *cu)
19927 {
19928 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19929 struct comp_unit_head *cu_header = &cu->header;
19930
19931 /* NOTE drow/2003-01-30: There used to be a comment and some special
19932 code here to turn a symbol with DW_AT_external and a
19933 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19934 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19935 with some versions of binutils) where shared libraries could have
19936 relocations against symbols in their debug information - the
19937 minimal symbol would have the right address, but the debug info
19938 would not. It's no longer necessary, because we will explicitly
19939 apply relocations when we read in the debug information now. */
19940
19941 /* A DW_AT_location attribute with no contents indicates that a
19942 variable has been optimized away. */
19943 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
19944 {
19945 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19946 return;
19947 }
19948
19949 /* Handle one degenerate form of location expression specially, to
19950 preserve GDB's previous behavior when section offsets are
19951 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
19952 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
19953
19954 if (attr->form_is_block ()
19955 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19956 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19957 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19958 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
19959 && (DW_BLOCK (attr)->size
19960 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
19961 {
19962 unsigned int dummy;
19963
19964 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19965 SET_SYMBOL_VALUE_ADDRESS
19966 (sym, cu->header.read_address (objfile->obfd,
19967 DW_BLOCK (attr)->data + 1,
19968 &dummy));
19969 else
19970 SET_SYMBOL_VALUE_ADDRESS
19971 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
19972 &dummy));
19973 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
19974 fixup_symbol_section (sym, objfile);
19975 SET_SYMBOL_VALUE_ADDRESS
19976 (sym,
19977 SYMBOL_VALUE_ADDRESS (sym)
19978 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
19979 return;
19980 }
19981
19982 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19983 expression evaluator, and use LOC_COMPUTED only when necessary
19984 (i.e. when the value of a register or memory location is
19985 referenced, or a thread-local block, etc.). Then again, it might
19986 not be worthwhile. I'm assuming that it isn't unless performance
19987 or memory numbers show me otherwise. */
19988
19989 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
19990
19991 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
19992 cu->has_loclist = true;
19993 }
19994
19995 /* Given a pointer to a DWARF information entry, figure out if we need
19996 to make a symbol table entry for it, and if so, create a new entry
19997 and return a pointer to it.
19998 If TYPE is NULL, determine symbol type from the die, otherwise
19999 used the passed type.
20000 If SPACE is not NULL, use it to hold the new symbol. If it is
20001 NULL, allocate a new symbol on the objfile's obstack. */
20002
20003 static struct symbol *
20004 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20005 struct symbol *space)
20006 {
20007 struct dwarf2_per_objfile *dwarf2_per_objfile
20008 = cu->per_cu->dwarf2_per_objfile;
20009 struct objfile *objfile = dwarf2_per_objfile->objfile;
20010 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20011 struct symbol *sym = NULL;
20012 const char *name;
20013 struct attribute *attr = NULL;
20014 struct attribute *attr2 = NULL;
20015 CORE_ADDR baseaddr;
20016 struct pending **list_to_add = NULL;
20017
20018 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20019
20020 baseaddr = objfile->text_section_offset ();
20021
20022 name = dwarf2_name (die, cu);
20023 if (name)
20024 {
20025 const char *linkagename;
20026 int suppress_add = 0;
20027
20028 if (space)
20029 sym = space;
20030 else
20031 sym = allocate_symbol (objfile);
20032 OBJSTAT (objfile, n_syms++);
20033
20034 /* Cache this symbol's name and the name's demangled form (if any). */
20035 sym->set_language (cu->language, &objfile->objfile_obstack);
20036 linkagename = dwarf2_physname (name, die, cu);
20037 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20038
20039 /* Fortran does not have mangling standard and the mangling does differ
20040 between gfortran, iFort etc. */
20041 if (cu->language == language_fortran
20042 && symbol_get_demangled_name (sym) == NULL)
20043 symbol_set_demangled_name (sym,
20044 dwarf2_full_name (name, die, cu),
20045 NULL);
20046
20047 /* Default assumptions.
20048 Use the passed type or decode it from the die. */
20049 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20050 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20051 if (type != NULL)
20052 SYMBOL_TYPE (sym) = type;
20053 else
20054 SYMBOL_TYPE (sym) = die_type (die, cu);
20055 attr = dwarf2_attr (die,
20056 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20057 cu);
20058 if (attr != nullptr)
20059 {
20060 SYMBOL_LINE (sym) = DW_UNSND (attr);
20061 }
20062
20063 attr = dwarf2_attr (die,
20064 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20065 cu);
20066 if (attr != nullptr)
20067 {
20068 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20069 struct file_entry *fe;
20070
20071 if (cu->line_header != NULL)
20072 fe = cu->line_header->file_name_at (file_index);
20073 else
20074 fe = NULL;
20075
20076 if (fe == NULL)
20077 complaint (_("file index out of range"));
20078 else
20079 symbol_set_symtab (sym, fe->symtab);
20080 }
20081
20082 switch (die->tag)
20083 {
20084 case DW_TAG_label:
20085 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20086 if (attr != nullptr)
20087 {
20088 CORE_ADDR addr;
20089
20090 addr = attr->value_as_address ();
20091 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20092 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20093 }
20094 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20095 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20096 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20097 add_symbol_to_list (sym, cu->list_in_scope);
20098 break;
20099 case DW_TAG_subprogram:
20100 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20101 finish_block. */
20102 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20103 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20104 if ((attr2 && (DW_UNSND (attr2) != 0))
20105 || cu->language == language_ada
20106 || cu->language == language_fortran)
20107 {
20108 /* Subprograms marked external are stored as a global symbol.
20109 Ada and Fortran subprograms, whether marked external or
20110 not, are always stored as a global symbol, because we want
20111 to be able to access them globally. For instance, we want
20112 to be able to break on a nested subprogram without having
20113 to specify the context. */
20114 list_to_add = cu->get_builder ()->get_global_symbols ();
20115 }
20116 else
20117 {
20118 list_to_add = cu->list_in_scope;
20119 }
20120 break;
20121 case DW_TAG_inlined_subroutine:
20122 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20123 finish_block. */
20124 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20125 SYMBOL_INLINED (sym) = 1;
20126 list_to_add = cu->list_in_scope;
20127 break;
20128 case DW_TAG_template_value_param:
20129 suppress_add = 1;
20130 /* Fall through. */
20131 case DW_TAG_constant:
20132 case DW_TAG_variable:
20133 case DW_TAG_member:
20134 /* Compilation with minimal debug info may result in
20135 variables with missing type entries. Change the
20136 misleading `void' type to something sensible. */
20137 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20138 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20139
20140 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20141 /* In the case of DW_TAG_member, we should only be called for
20142 static const members. */
20143 if (die->tag == DW_TAG_member)
20144 {
20145 /* dwarf2_add_field uses die_is_declaration,
20146 so we do the same. */
20147 gdb_assert (die_is_declaration (die, cu));
20148 gdb_assert (attr);
20149 }
20150 if (attr != nullptr)
20151 {
20152 dwarf2_const_value (attr, sym, cu);
20153 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20154 if (!suppress_add)
20155 {
20156 if (attr2 && (DW_UNSND (attr2) != 0))
20157 list_to_add = cu->get_builder ()->get_global_symbols ();
20158 else
20159 list_to_add = cu->list_in_scope;
20160 }
20161 break;
20162 }
20163 attr = dwarf2_attr (die, DW_AT_location, cu);
20164 if (attr != nullptr)
20165 {
20166 var_decode_location (attr, sym, cu);
20167 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20168
20169 /* Fortran explicitly imports any global symbols to the local
20170 scope by DW_TAG_common_block. */
20171 if (cu->language == language_fortran && die->parent
20172 && die->parent->tag == DW_TAG_common_block)
20173 attr2 = NULL;
20174
20175 if (SYMBOL_CLASS (sym) == LOC_STATIC
20176 && SYMBOL_VALUE_ADDRESS (sym) == 0
20177 && !dwarf2_per_objfile->has_section_at_zero)
20178 {
20179 /* When a static variable is eliminated by the linker,
20180 the corresponding debug information is not stripped
20181 out, but the variable address is set to null;
20182 do not add such variables into symbol table. */
20183 }
20184 else if (attr2 && (DW_UNSND (attr2) != 0))
20185 {
20186 if (SYMBOL_CLASS (sym) == LOC_STATIC
20187 && (objfile->flags & OBJF_MAINLINE) == 0
20188 && dwarf2_per_objfile->can_copy)
20189 {
20190 /* A global static variable might be subject to
20191 copy relocation. We first check for a local
20192 minsym, though, because maybe the symbol was
20193 marked hidden, in which case this would not
20194 apply. */
20195 bound_minimal_symbol found
20196 = (lookup_minimal_symbol_linkage
20197 (sym->linkage_name (), objfile));
20198 if (found.minsym != nullptr)
20199 sym->maybe_copied = 1;
20200 }
20201
20202 /* A variable with DW_AT_external is never static,
20203 but it may be block-scoped. */
20204 list_to_add
20205 = ((cu->list_in_scope
20206 == cu->get_builder ()->get_file_symbols ())
20207 ? cu->get_builder ()->get_global_symbols ()
20208 : cu->list_in_scope);
20209 }
20210 else
20211 list_to_add = cu->list_in_scope;
20212 }
20213 else
20214 {
20215 /* We do not know the address of this symbol.
20216 If it is an external symbol and we have type information
20217 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20218 The address of the variable will then be determined from
20219 the minimal symbol table whenever the variable is
20220 referenced. */
20221 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20222
20223 /* Fortran explicitly imports any global symbols to the local
20224 scope by DW_TAG_common_block. */
20225 if (cu->language == language_fortran && die->parent
20226 && die->parent->tag == DW_TAG_common_block)
20227 {
20228 /* SYMBOL_CLASS doesn't matter here because
20229 read_common_block is going to reset it. */
20230 if (!suppress_add)
20231 list_to_add = cu->list_in_scope;
20232 }
20233 else if (attr2 && (DW_UNSND (attr2) != 0)
20234 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20235 {
20236 /* A variable with DW_AT_external is never static, but it
20237 may be block-scoped. */
20238 list_to_add
20239 = ((cu->list_in_scope
20240 == cu->get_builder ()->get_file_symbols ())
20241 ? cu->get_builder ()->get_global_symbols ()
20242 : cu->list_in_scope);
20243
20244 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20245 }
20246 else if (!die_is_declaration (die, cu))
20247 {
20248 /* Use the default LOC_OPTIMIZED_OUT class. */
20249 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20250 if (!suppress_add)
20251 list_to_add = cu->list_in_scope;
20252 }
20253 }
20254 break;
20255 case DW_TAG_formal_parameter:
20256 {
20257 /* If we are inside a function, mark this as an argument. If
20258 not, we might be looking at an argument to an inlined function
20259 when we do not have enough information to show inlined frames;
20260 pretend it's a local variable in that case so that the user can
20261 still see it. */
20262 struct context_stack *curr
20263 = cu->get_builder ()->get_current_context_stack ();
20264 if (curr != nullptr && curr->name != nullptr)
20265 SYMBOL_IS_ARGUMENT (sym) = 1;
20266 attr = dwarf2_attr (die, DW_AT_location, cu);
20267 if (attr != nullptr)
20268 {
20269 var_decode_location (attr, sym, cu);
20270 }
20271 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20272 if (attr != nullptr)
20273 {
20274 dwarf2_const_value (attr, sym, cu);
20275 }
20276
20277 list_to_add = cu->list_in_scope;
20278 }
20279 break;
20280 case DW_TAG_unspecified_parameters:
20281 /* From varargs functions; gdb doesn't seem to have any
20282 interest in this information, so just ignore it for now.
20283 (FIXME?) */
20284 break;
20285 case DW_TAG_template_type_param:
20286 suppress_add = 1;
20287 /* Fall through. */
20288 case DW_TAG_class_type:
20289 case DW_TAG_interface_type:
20290 case DW_TAG_structure_type:
20291 case DW_TAG_union_type:
20292 case DW_TAG_set_type:
20293 case DW_TAG_enumeration_type:
20294 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20295 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20296
20297 {
20298 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20299 really ever be static objects: otherwise, if you try
20300 to, say, break of a class's method and you're in a file
20301 which doesn't mention that class, it won't work unless
20302 the check for all static symbols in lookup_symbol_aux
20303 saves you. See the OtherFileClass tests in
20304 gdb.c++/namespace.exp. */
20305
20306 if (!suppress_add)
20307 {
20308 buildsym_compunit *builder = cu->get_builder ();
20309 list_to_add
20310 = (cu->list_in_scope == builder->get_file_symbols ()
20311 && cu->language == language_cplus
20312 ? builder->get_global_symbols ()
20313 : cu->list_in_scope);
20314
20315 /* The semantics of C++ state that "struct foo {
20316 ... }" also defines a typedef for "foo". */
20317 if (cu->language == language_cplus
20318 || cu->language == language_ada
20319 || cu->language == language_d
20320 || cu->language == language_rust)
20321 {
20322 /* The symbol's name is already allocated along
20323 with this objfile, so we don't need to
20324 duplicate it for the type. */
20325 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20326 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20327 }
20328 }
20329 }
20330 break;
20331 case DW_TAG_typedef:
20332 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20333 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20334 list_to_add = cu->list_in_scope;
20335 break;
20336 case DW_TAG_base_type:
20337 case DW_TAG_subrange_type:
20338 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20339 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20340 list_to_add = cu->list_in_scope;
20341 break;
20342 case DW_TAG_enumerator:
20343 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20344 if (attr != nullptr)
20345 {
20346 dwarf2_const_value (attr, sym, cu);
20347 }
20348 {
20349 /* NOTE: carlton/2003-11-10: See comment above in the
20350 DW_TAG_class_type, etc. block. */
20351
20352 list_to_add
20353 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20354 && cu->language == language_cplus
20355 ? cu->get_builder ()->get_global_symbols ()
20356 : cu->list_in_scope);
20357 }
20358 break;
20359 case DW_TAG_imported_declaration:
20360 case DW_TAG_namespace:
20361 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20362 list_to_add = cu->get_builder ()->get_global_symbols ();
20363 break;
20364 case DW_TAG_module:
20365 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20366 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20367 list_to_add = cu->get_builder ()->get_global_symbols ();
20368 break;
20369 case DW_TAG_common_block:
20370 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20371 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20372 add_symbol_to_list (sym, cu->list_in_scope);
20373 break;
20374 default:
20375 /* Not a tag we recognize. Hopefully we aren't processing
20376 trash data, but since we must specifically ignore things
20377 we don't recognize, there is nothing else we should do at
20378 this point. */
20379 complaint (_("unsupported tag: '%s'"),
20380 dwarf_tag_name (die->tag));
20381 break;
20382 }
20383
20384 if (suppress_add)
20385 {
20386 sym->hash_next = objfile->template_symbols;
20387 objfile->template_symbols = sym;
20388 list_to_add = NULL;
20389 }
20390
20391 if (list_to_add != NULL)
20392 add_symbol_to_list (sym, list_to_add);
20393
20394 /* For the benefit of old versions of GCC, check for anonymous
20395 namespaces based on the demangled name. */
20396 if (!cu->processing_has_namespace_info
20397 && cu->language == language_cplus)
20398 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20399 }
20400 return (sym);
20401 }
20402
20403 /* Given an attr with a DW_FORM_dataN value in host byte order,
20404 zero-extend it as appropriate for the symbol's type. The DWARF
20405 standard (v4) is not entirely clear about the meaning of using
20406 DW_FORM_dataN for a constant with a signed type, where the type is
20407 wider than the data. The conclusion of a discussion on the DWARF
20408 list was that this is unspecified. We choose to always zero-extend
20409 because that is the interpretation long in use by GCC. */
20410
20411 static gdb_byte *
20412 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20413 struct dwarf2_cu *cu, LONGEST *value, int bits)
20414 {
20415 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20416 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20417 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20418 LONGEST l = DW_UNSND (attr);
20419
20420 if (bits < sizeof (*value) * 8)
20421 {
20422 l &= ((LONGEST) 1 << bits) - 1;
20423 *value = l;
20424 }
20425 else if (bits == sizeof (*value) * 8)
20426 *value = l;
20427 else
20428 {
20429 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20430 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20431 return bytes;
20432 }
20433
20434 return NULL;
20435 }
20436
20437 /* Read a constant value from an attribute. Either set *VALUE, or if
20438 the value does not fit in *VALUE, set *BYTES - either already
20439 allocated on the objfile obstack, or newly allocated on OBSTACK,
20440 or, set *BATON, if we translated the constant to a location
20441 expression. */
20442
20443 static void
20444 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20445 const char *name, struct obstack *obstack,
20446 struct dwarf2_cu *cu,
20447 LONGEST *value, const gdb_byte **bytes,
20448 struct dwarf2_locexpr_baton **baton)
20449 {
20450 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20451 struct comp_unit_head *cu_header = &cu->header;
20452 struct dwarf_block *blk;
20453 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20454 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20455
20456 *value = 0;
20457 *bytes = NULL;
20458 *baton = NULL;
20459
20460 switch (attr->form)
20461 {
20462 case DW_FORM_addr:
20463 case DW_FORM_addrx:
20464 case DW_FORM_GNU_addr_index:
20465 {
20466 gdb_byte *data;
20467
20468 if (TYPE_LENGTH (type) != cu_header->addr_size)
20469 dwarf2_const_value_length_mismatch_complaint (name,
20470 cu_header->addr_size,
20471 TYPE_LENGTH (type));
20472 /* Symbols of this form are reasonably rare, so we just
20473 piggyback on the existing location code rather than writing
20474 a new implementation of symbol_computed_ops. */
20475 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20476 (*baton)->per_cu = cu->per_cu;
20477 gdb_assert ((*baton)->per_cu);
20478
20479 (*baton)->size = 2 + cu_header->addr_size;
20480 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20481 (*baton)->data = data;
20482
20483 data[0] = DW_OP_addr;
20484 store_unsigned_integer (&data[1], cu_header->addr_size,
20485 byte_order, DW_ADDR (attr));
20486 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20487 }
20488 break;
20489 case DW_FORM_string:
20490 case DW_FORM_strp:
20491 case DW_FORM_strx:
20492 case DW_FORM_GNU_str_index:
20493 case DW_FORM_GNU_strp_alt:
20494 /* DW_STRING is already allocated on the objfile obstack, point
20495 directly to it. */
20496 *bytes = (const gdb_byte *) DW_STRING (attr);
20497 break;
20498 case DW_FORM_block1:
20499 case DW_FORM_block2:
20500 case DW_FORM_block4:
20501 case DW_FORM_block:
20502 case DW_FORM_exprloc:
20503 case DW_FORM_data16:
20504 blk = DW_BLOCK (attr);
20505 if (TYPE_LENGTH (type) != blk->size)
20506 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20507 TYPE_LENGTH (type));
20508 *bytes = blk->data;
20509 break;
20510
20511 /* The DW_AT_const_value attributes are supposed to carry the
20512 symbol's value "represented as it would be on the target
20513 architecture." By the time we get here, it's already been
20514 converted to host endianness, so we just need to sign- or
20515 zero-extend it as appropriate. */
20516 case DW_FORM_data1:
20517 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20518 break;
20519 case DW_FORM_data2:
20520 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20521 break;
20522 case DW_FORM_data4:
20523 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20524 break;
20525 case DW_FORM_data8:
20526 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20527 break;
20528
20529 case DW_FORM_sdata:
20530 case DW_FORM_implicit_const:
20531 *value = DW_SND (attr);
20532 break;
20533
20534 case DW_FORM_udata:
20535 *value = DW_UNSND (attr);
20536 break;
20537
20538 default:
20539 complaint (_("unsupported const value attribute form: '%s'"),
20540 dwarf_form_name (attr->form));
20541 *value = 0;
20542 break;
20543 }
20544 }
20545
20546
20547 /* Copy constant value from an attribute to a symbol. */
20548
20549 static void
20550 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20551 struct dwarf2_cu *cu)
20552 {
20553 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20554 LONGEST value;
20555 const gdb_byte *bytes;
20556 struct dwarf2_locexpr_baton *baton;
20557
20558 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20559 sym->print_name (),
20560 &objfile->objfile_obstack, cu,
20561 &value, &bytes, &baton);
20562
20563 if (baton != NULL)
20564 {
20565 SYMBOL_LOCATION_BATON (sym) = baton;
20566 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20567 }
20568 else if (bytes != NULL)
20569 {
20570 SYMBOL_VALUE_BYTES (sym) = bytes;
20571 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20572 }
20573 else
20574 {
20575 SYMBOL_VALUE (sym) = value;
20576 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20577 }
20578 }
20579
20580 /* Return the type of the die in question using its DW_AT_type attribute. */
20581
20582 static struct type *
20583 die_type (struct die_info *die, struct dwarf2_cu *cu)
20584 {
20585 struct attribute *type_attr;
20586
20587 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20588 if (!type_attr)
20589 {
20590 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20591 /* A missing DW_AT_type represents a void type. */
20592 return objfile_type (objfile)->builtin_void;
20593 }
20594
20595 return lookup_die_type (die, type_attr, cu);
20596 }
20597
20598 /* True iff CU's producer generates GNAT Ada auxiliary information
20599 that allows to find parallel types through that information instead
20600 of having to do expensive parallel lookups by type name. */
20601
20602 static int
20603 need_gnat_info (struct dwarf2_cu *cu)
20604 {
20605 /* Assume that the Ada compiler was GNAT, which always produces
20606 the auxiliary information. */
20607 return (cu->language == language_ada);
20608 }
20609
20610 /* Return the auxiliary type of the die in question using its
20611 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20612 attribute is not present. */
20613
20614 static struct type *
20615 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20616 {
20617 struct attribute *type_attr;
20618
20619 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20620 if (!type_attr)
20621 return NULL;
20622
20623 return lookup_die_type (die, type_attr, cu);
20624 }
20625
20626 /* If DIE has a descriptive_type attribute, then set the TYPE's
20627 descriptive type accordingly. */
20628
20629 static void
20630 set_descriptive_type (struct type *type, struct die_info *die,
20631 struct dwarf2_cu *cu)
20632 {
20633 struct type *descriptive_type = die_descriptive_type (die, cu);
20634
20635 if (descriptive_type)
20636 {
20637 ALLOCATE_GNAT_AUX_TYPE (type);
20638 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20639 }
20640 }
20641
20642 /* Return the containing type of the die in question using its
20643 DW_AT_containing_type attribute. */
20644
20645 static struct type *
20646 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20647 {
20648 struct attribute *type_attr;
20649 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20650
20651 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20652 if (!type_attr)
20653 error (_("Dwarf Error: Problem turning containing type into gdb type "
20654 "[in module %s]"), objfile_name (objfile));
20655
20656 return lookup_die_type (die, type_attr, cu);
20657 }
20658
20659 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20660
20661 static struct type *
20662 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20663 {
20664 struct dwarf2_per_objfile *dwarf2_per_objfile
20665 = cu->per_cu->dwarf2_per_objfile;
20666 struct objfile *objfile = dwarf2_per_objfile->objfile;
20667 char *saved;
20668
20669 std::string message
20670 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20671 objfile_name (objfile),
20672 sect_offset_str (cu->header.sect_off),
20673 sect_offset_str (die->sect_off));
20674 saved = obstack_strdup (&objfile->objfile_obstack, message);
20675
20676 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20677 }
20678
20679 /* Look up the type of DIE in CU using its type attribute ATTR.
20680 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20681 DW_AT_containing_type.
20682 If there is no type substitute an error marker. */
20683
20684 static struct type *
20685 lookup_die_type (struct die_info *die, const struct attribute *attr,
20686 struct dwarf2_cu *cu)
20687 {
20688 struct dwarf2_per_objfile *dwarf2_per_objfile
20689 = cu->per_cu->dwarf2_per_objfile;
20690 struct objfile *objfile = dwarf2_per_objfile->objfile;
20691 struct type *this_type;
20692
20693 gdb_assert (attr->name == DW_AT_type
20694 || attr->name == DW_AT_GNAT_descriptive_type
20695 || attr->name == DW_AT_containing_type);
20696
20697 /* First see if we have it cached. */
20698
20699 if (attr->form == DW_FORM_GNU_ref_alt)
20700 {
20701 struct dwarf2_per_cu_data *per_cu;
20702 sect_offset sect_off = attr->get_ref_die_offset ();
20703
20704 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20705 dwarf2_per_objfile);
20706 this_type = get_die_type_at_offset (sect_off, per_cu);
20707 }
20708 else if (attr->form_is_ref ())
20709 {
20710 sect_offset sect_off = attr->get_ref_die_offset ();
20711
20712 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20713 }
20714 else if (attr->form == DW_FORM_ref_sig8)
20715 {
20716 ULONGEST signature = DW_SIGNATURE (attr);
20717
20718 return get_signatured_type (die, signature, cu);
20719 }
20720 else
20721 {
20722 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
20723 " at %s [in module %s]"),
20724 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
20725 objfile_name (objfile));
20726 return build_error_marker_type (cu, die);
20727 }
20728
20729 /* If not cached we need to read it in. */
20730
20731 if (this_type == NULL)
20732 {
20733 struct die_info *type_die = NULL;
20734 struct dwarf2_cu *type_cu = cu;
20735
20736 if (attr->form_is_ref ())
20737 type_die = follow_die_ref (die, attr, &type_cu);
20738 if (type_die == NULL)
20739 return build_error_marker_type (cu, die);
20740 /* If we find the type now, it's probably because the type came
20741 from an inter-CU reference and the type's CU got expanded before
20742 ours. */
20743 this_type = read_type_die (type_die, type_cu);
20744 }
20745
20746 /* If we still don't have a type use an error marker. */
20747
20748 if (this_type == NULL)
20749 return build_error_marker_type (cu, die);
20750
20751 return this_type;
20752 }
20753
20754 /* Return the type in DIE, CU.
20755 Returns NULL for invalid types.
20756
20757 This first does a lookup in die_type_hash,
20758 and only reads the die in if necessary.
20759
20760 NOTE: This can be called when reading in partial or full symbols. */
20761
20762 static struct type *
20763 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20764 {
20765 struct type *this_type;
20766
20767 this_type = get_die_type (die, cu);
20768 if (this_type)
20769 return this_type;
20770
20771 return read_type_die_1 (die, cu);
20772 }
20773
20774 /* Read the type in DIE, CU.
20775 Returns NULL for invalid types. */
20776
20777 static struct type *
20778 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20779 {
20780 struct type *this_type = NULL;
20781
20782 switch (die->tag)
20783 {
20784 case DW_TAG_class_type:
20785 case DW_TAG_interface_type:
20786 case DW_TAG_structure_type:
20787 case DW_TAG_union_type:
20788 this_type = read_structure_type (die, cu);
20789 break;
20790 case DW_TAG_enumeration_type:
20791 this_type = read_enumeration_type (die, cu);
20792 break;
20793 case DW_TAG_subprogram:
20794 case DW_TAG_subroutine_type:
20795 case DW_TAG_inlined_subroutine:
20796 this_type = read_subroutine_type (die, cu);
20797 break;
20798 case DW_TAG_array_type:
20799 this_type = read_array_type (die, cu);
20800 break;
20801 case DW_TAG_set_type:
20802 this_type = read_set_type (die, cu);
20803 break;
20804 case DW_TAG_pointer_type:
20805 this_type = read_tag_pointer_type (die, cu);
20806 break;
20807 case DW_TAG_ptr_to_member_type:
20808 this_type = read_tag_ptr_to_member_type (die, cu);
20809 break;
20810 case DW_TAG_reference_type:
20811 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20812 break;
20813 case DW_TAG_rvalue_reference_type:
20814 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
20815 break;
20816 case DW_TAG_const_type:
20817 this_type = read_tag_const_type (die, cu);
20818 break;
20819 case DW_TAG_volatile_type:
20820 this_type = read_tag_volatile_type (die, cu);
20821 break;
20822 case DW_TAG_restrict_type:
20823 this_type = read_tag_restrict_type (die, cu);
20824 break;
20825 case DW_TAG_string_type:
20826 this_type = read_tag_string_type (die, cu);
20827 break;
20828 case DW_TAG_typedef:
20829 this_type = read_typedef (die, cu);
20830 break;
20831 case DW_TAG_subrange_type:
20832 this_type = read_subrange_type (die, cu);
20833 break;
20834 case DW_TAG_base_type:
20835 this_type = read_base_type (die, cu);
20836 break;
20837 case DW_TAG_unspecified_type:
20838 this_type = read_unspecified_type (die, cu);
20839 break;
20840 case DW_TAG_namespace:
20841 this_type = read_namespace_type (die, cu);
20842 break;
20843 case DW_TAG_module:
20844 this_type = read_module_type (die, cu);
20845 break;
20846 case DW_TAG_atomic_type:
20847 this_type = read_tag_atomic_type (die, cu);
20848 break;
20849 default:
20850 complaint (_("unexpected tag in read_type_die: '%s'"),
20851 dwarf_tag_name (die->tag));
20852 break;
20853 }
20854
20855 return this_type;
20856 }
20857
20858 /* See if we can figure out if the class lives in a namespace. We do
20859 this by looking for a member function; its demangled name will
20860 contain namespace info, if there is any.
20861 Return the computed name or NULL.
20862 Space for the result is allocated on the objfile's obstack.
20863 This is the full-die version of guess_partial_die_structure_name.
20864 In this case we know DIE has no useful parent. */
20865
20866 static const char *
20867 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20868 {
20869 struct die_info *spec_die;
20870 struct dwarf2_cu *spec_cu;
20871 struct die_info *child;
20872 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20873
20874 spec_cu = cu;
20875 spec_die = die_specification (die, &spec_cu);
20876 if (spec_die != NULL)
20877 {
20878 die = spec_die;
20879 cu = spec_cu;
20880 }
20881
20882 for (child = die->child;
20883 child != NULL;
20884 child = child->sibling)
20885 {
20886 if (child->tag == DW_TAG_subprogram)
20887 {
20888 const char *linkage_name = dw2_linkage_name (child, cu);
20889
20890 if (linkage_name != NULL)
20891 {
20892 gdb::unique_xmalloc_ptr<char> actual_name
20893 (language_class_name_from_physname (cu->language_defn,
20894 linkage_name));
20895 const char *name = NULL;
20896
20897 if (actual_name != NULL)
20898 {
20899 const char *die_name = dwarf2_name (die, cu);
20900
20901 if (die_name != NULL
20902 && strcmp (die_name, actual_name.get ()) != 0)
20903 {
20904 /* Strip off the class name from the full name.
20905 We want the prefix. */
20906 int die_name_len = strlen (die_name);
20907 int actual_name_len = strlen (actual_name.get ());
20908 const char *ptr = actual_name.get ();
20909
20910 /* Test for '::' as a sanity check. */
20911 if (actual_name_len > die_name_len + 2
20912 && ptr[actual_name_len - die_name_len - 1] == ':')
20913 name = obstack_strndup (
20914 &objfile->per_bfd->storage_obstack,
20915 ptr, actual_name_len - die_name_len - 2);
20916 }
20917 }
20918 return name;
20919 }
20920 }
20921 }
20922
20923 return NULL;
20924 }
20925
20926 /* GCC might emit a nameless typedef that has a linkage name. Determine the
20927 prefix part in such case. See
20928 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20929
20930 static const char *
20931 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20932 {
20933 struct attribute *attr;
20934 const char *base;
20935
20936 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20937 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20938 return NULL;
20939
20940 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
20941 return NULL;
20942
20943 attr = dw2_linkage_name_attr (die, cu);
20944 if (attr == NULL || DW_STRING (attr) == NULL)
20945 return NULL;
20946
20947 /* dwarf2_name had to be already called. */
20948 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20949
20950 /* Strip the base name, keep any leading namespaces/classes. */
20951 base = strrchr (DW_STRING (attr), ':');
20952 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20953 return "";
20954
20955 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20956 return obstack_strndup (&objfile->per_bfd->storage_obstack,
20957 DW_STRING (attr),
20958 &base[-1] - DW_STRING (attr));
20959 }
20960
20961 /* Return the name of the namespace/class that DIE is defined within,
20962 or "" if we can't tell. The caller should not xfree the result.
20963
20964 For example, if we're within the method foo() in the following
20965 code:
20966
20967 namespace N {
20968 class C {
20969 void foo () {
20970 }
20971 };
20972 }
20973
20974 then determine_prefix on foo's die will return "N::C". */
20975
20976 static const char *
20977 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
20978 {
20979 struct dwarf2_per_objfile *dwarf2_per_objfile
20980 = cu->per_cu->dwarf2_per_objfile;
20981 struct die_info *parent, *spec_die;
20982 struct dwarf2_cu *spec_cu;
20983 struct type *parent_type;
20984 const char *retval;
20985
20986 if (cu->language != language_cplus
20987 && cu->language != language_fortran && cu->language != language_d
20988 && cu->language != language_rust)
20989 return "";
20990
20991 retval = anonymous_struct_prefix (die, cu);
20992 if (retval)
20993 return retval;
20994
20995 /* We have to be careful in the presence of DW_AT_specification.
20996 For example, with GCC 3.4, given the code
20997
20998 namespace N {
20999 void foo() {
21000 // Definition of N::foo.
21001 }
21002 }
21003
21004 then we'll have a tree of DIEs like this:
21005
21006 1: DW_TAG_compile_unit
21007 2: DW_TAG_namespace // N
21008 3: DW_TAG_subprogram // declaration of N::foo
21009 4: DW_TAG_subprogram // definition of N::foo
21010 DW_AT_specification // refers to die #3
21011
21012 Thus, when processing die #4, we have to pretend that we're in
21013 the context of its DW_AT_specification, namely the contex of die
21014 #3. */
21015 spec_cu = cu;
21016 spec_die = die_specification (die, &spec_cu);
21017 if (spec_die == NULL)
21018 parent = die->parent;
21019 else
21020 {
21021 parent = spec_die->parent;
21022 cu = spec_cu;
21023 }
21024
21025 if (parent == NULL)
21026 return "";
21027 else if (parent->building_fullname)
21028 {
21029 const char *name;
21030 const char *parent_name;
21031
21032 /* It has been seen on RealView 2.2 built binaries,
21033 DW_TAG_template_type_param types actually _defined_ as
21034 children of the parent class:
21035
21036 enum E {};
21037 template class <class Enum> Class{};
21038 Class<enum E> class_e;
21039
21040 1: DW_TAG_class_type (Class)
21041 2: DW_TAG_enumeration_type (E)
21042 3: DW_TAG_enumerator (enum1:0)
21043 3: DW_TAG_enumerator (enum2:1)
21044 ...
21045 2: DW_TAG_template_type_param
21046 DW_AT_type DW_FORM_ref_udata (E)
21047
21048 Besides being broken debug info, it can put GDB into an
21049 infinite loop. Consider:
21050
21051 When we're building the full name for Class<E>, we'll start
21052 at Class, and go look over its template type parameters,
21053 finding E. We'll then try to build the full name of E, and
21054 reach here. We're now trying to build the full name of E,
21055 and look over the parent DIE for containing scope. In the
21056 broken case, if we followed the parent DIE of E, we'd again
21057 find Class, and once again go look at its template type
21058 arguments, etc., etc. Simply don't consider such parent die
21059 as source-level parent of this die (it can't be, the language
21060 doesn't allow it), and break the loop here. */
21061 name = dwarf2_name (die, cu);
21062 parent_name = dwarf2_name (parent, cu);
21063 complaint (_("template param type '%s' defined within parent '%s'"),
21064 name ? name : "<unknown>",
21065 parent_name ? parent_name : "<unknown>");
21066 return "";
21067 }
21068 else
21069 switch (parent->tag)
21070 {
21071 case DW_TAG_namespace:
21072 parent_type = read_type_die (parent, cu);
21073 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21074 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21075 Work around this problem here. */
21076 if (cu->language == language_cplus
21077 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21078 return "";
21079 /* We give a name to even anonymous namespaces. */
21080 return TYPE_NAME (parent_type);
21081 case DW_TAG_class_type:
21082 case DW_TAG_interface_type:
21083 case DW_TAG_structure_type:
21084 case DW_TAG_union_type:
21085 case DW_TAG_module:
21086 parent_type = read_type_die (parent, cu);
21087 if (TYPE_NAME (parent_type) != NULL)
21088 return TYPE_NAME (parent_type);
21089 else
21090 /* An anonymous structure is only allowed non-static data
21091 members; no typedefs, no member functions, et cetera.
21092 So it does not need a prefix. */
21093 return "";
21094 case DW_TAG_compile_unit:
21095 case DW_TAG_partial_unit:
21096 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21097 if (cu->language == language_cplus
21098 && !dwarf2_per_objfile->types.empty ()
21099 && die->child != NULL
21100 && (die->tag == DW_TAG_class_type
21101 || die->tag == DW_TAG_structure_type
21102 || die->tag == DW_TAG_union_type))
21103 {
21104 const char *name = guess_full_die_structure_name (die, cu);
21105 if (name != NULL)
21106 return name;
21107 }
21108 return "";
21109 case DW_TAG_subprogram:
21110 /* Nested subroutines in Fortran get a prefix with the name
21111 of the parent's subroutine. */
21112 if (cu->language == language_fortran)
21113 {
21114 if ((die->tag == DW_TAG_subprogram)
21115 && (dwarf2_name (parent, cu) != NULL))
21116 return dwarf2_name (parent, cu);
21117 }
21118 return determine_prefix (parent, cu);
21119 case DW_TAG_enumeration_type:
21120 parent_type = read_type_die (parent, cu);
21121 if (TYPE_DECLARED_CLASS (parent_type))
21122 {
21123 if (TYPE_NAME (parent_type) != NULL)
21124 return TYPE_NAME (parent_type);
21125 return "";
21126 }
21127 /* Fall through. */
21128 default:
21129 return determine_prefix (parent, cu);
21130 }
21131 }
21132
21133 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21134 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21135 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21136 an obconcat, otherwise allocate storage for the result. The CU argument is
21137 used to determine the language and hence, the appropriate separator. */
21138
21139 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21140
21141 static char *
21142 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21143 int physname, struct dwarf2_cu *cu)
21144 {
21145 const char *lead = "";
21146 const char *sep;
21147
21148 if (suffix == NULL || suffix[0] == '\0'
21149 || prefix == NULL || prefix[0] == '\0')
21150 sep = "";
21151 else if (cu->language == language_d)
21152 {
21153 /* For D, the 'main' function could be defined in any module, but it
21154 should never be prefixed. */
21155 if (strcmp (suffix, "D main") == 0)
21156 {
21157 prefix = "";
21158 sep = "";
21159 }
21160 else
21161 sep = ".";
21162 }
21163 else if (cu->language == language_fortran && physname)
21164 {
21165 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21166 DW_AT_MIPS_linkage_name is preferred and used instead. */
21167
21168 lead = "__";
21169 sep = "_MOD_";
21170 }
21171 else
21172 sep = "::";
21173
21174 if (prefix == NULL)
21175 prefix = "";
21176 if (suffix == NULL)
21177 suffix = "";
21178
21179 if (obs == NULL)
21180 {
21181 char *retval
21182 = ((char *)
21183 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21184
21185 strcpy (retval, lead);
21186 strcat (retval, prefix);
21187 strcat (retval, sep);
21188 strcat (retval, suffix);
21189 return retval;
21190 }
21191 else
21192 {
21193 /* We have an obstack. */
21194 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21195 }
21196 }
21197
21198 /* Get name of a die, return NULL if not found. */
21199
21200 static const char *
21201 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21202 struct objfile *objfile)
21203 {
21204 if (name && cu->language == language_cplus)
21205 {
21206 std::string canon_name = cp_canonicalize_string (name);
21207
21208 if (!canon_name.empty ())
21209 {
21210 if (canon_name != name)
21211 name = objfile->intern (canon_name);
21212 }
21213 }
21214
21215 return name;
21216 }
21217
21218 /* Get name of a die, return NULL if not found.
21219 Anonymous namespaces are converted to their magic string. */
21220
21221 static const char *
21222 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21223 {
21224 struct attribute *attr;
21225 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21226
21227 attr = dwarf2_attr (die, DW_AT_name, cu);
21228 if ((!attr || !DW_STRING (attr))
21229 && die->tag != DW_TAG_namespace
21230 && die->tag != DW_TAG_class_type
21231 && die->tag != DW_TAG_interface_type
21232 && die->tag != DW_TAG_structure_type
21233 && die->tag != DW_TAG_union_type)
21234 return NULL;
21235
21236 switch (die->tag)
21237 {
21238 case DW_TAG_compile_unit:
21239 case DW_TAG_partial_unit:
21240 /* Compilation units have a DW_AT_name that is a filename, not
21241 a source language identifier. */
21242 case DW_TAG_enumeration_type:
21243 case DW_TAG_enumerator:
21244 /* These tags always have simple identifiers already; no need
21245 to canonicalize them. */
21246 return DW_STRING (attr);
21247
21248 case DW_TAG_namespace:
21249 if (attr != NULL && DW_STRING (attr) != NULL)
21250 return DW_STRING (attr);
21251 return CP_ANONYMOUS_NAMESPACE_STR;
21252
21253 case DW_TAG_class_type:
21254 case DW_TAG_interface_type:
21255 case DW_TAG_structure_type:
21256 case DW_TAG_union_type:
21257 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21258 structures or unions. These were of the form "._%d" in GCC 4.1,
21259 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21260 and GCC 4.4. We work around this problem by ignoring these. */
21261 if (attr && DW_STRING (attr)
21262 && (startswith (DW_STRING (attr), "._")
21263 || startswith (DW_STRING (attr), "<anonymous")))
21264 return NULL;
21265
21266 /* GCC might emit a nameless typedef that has a linkage name. See
21267 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21268 if (!attr || DW_STRING (attr) == NULL)
21269 {
21270 attr = dw2_linkage_name_attr (die, cu);
21271 if (attr == NULL || DW_STRING (attr) == NULL)
21272 return NULL;
21273
21274 /* Avoid demangling DW_STRING (attr) the second time on a second
21275 call for the same DIE. */
21276 if (!DW_STRING_IS_CANONICAL (attr))
21277 {
21278 gdb::unique_xmalloc_ptr<char> demangled
21279 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21280 if (demangled == nullptr)
21281 return nullptr;
21282
21283 DW_STRING (attr) = objfile->intern (demangled.get ());
21284 DW_STRING_IS_CANONICAL (attr) = 1;
21285 }
21286
21287 /* Strip any leading namespaces/classes, keep only the base name.
21288 DW_AT_name for named DIEs does not contain the prefixes. */
21289 const char *base = strrchr (DW_STRING (attr), ':');
21290 if (base && base > DW_STRING (attr) && base[-1] == ':')
21291 return &base[1];
21292 else
21293 return DW_STRING (attr);
21294 }
21295 break;
21296
21297 default:
21298 break;
21299 }
21300
21301 if (!DW_STRING_IS_CANONICAL (attr))
21302 {
21303 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21304 objfile);
21305 DW_STRING_IS_CANONICAL (attr) = 1;
21306 }
21307 return DW_STRING (attr);
21308 }
21309
21310 /* Return the die that this die in an extension of, or NULL if there
21311 is none. *EXT_CU is the CU containing DIE on input, and the CU
21312 containing the return value on output. */
21313
21314 static struct die_info *
21315 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21316 {
21317 struct attribute *attr;
21318
21319 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21320 if (attr == NULL)
21321 return NULL;
21322
21323 return follow_die_ref (die, attr, ext_cu);
21324 }
21325
21326 static void
21327 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21328 {
21329 unsigned int i;
21330
21331 print_spaces (indent, f);
21332 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21333 dwarf_tag_name (die->tag), die->abbrev,
21334 sect_offset_str (die->sect_off));
21335
21336 if (die->parent != NULL)
21337 {
21338 print_spaces (indent, f);
21339 fprintf_unfiltered (f, " parent at offset: %s\n",
21340 sect_offset_str (die->parent->sect_off));
21341 }
21342
21343 print_spaces (indent, f);
21344 fprintf_unfiltered (f, " has children: %s\n",
21345 dwarf_bool_name (die->child != NULL));
21346
21347 print_spaces (indent, f);
21348 fprintf_unfiltered (f, " attributes:\n");
21349
21350 for (i = 0; i < die->num_attrs; ++i)
21351 {
21352 print_spaces (indent, f);
21353 fprintf_unfiltered (f, " %s (%s) ",
21354 dwarf_attr_name (die->attrs[i].name),
21355 dwarf_form_name (die->attrs[i].form));
21356
21357 switch (die->attrs[i].form)
21358 {
21359 case DW_FORM_addr:
21360 case DW_FORM_addrx:
21361 case DW_FORM_GNU_addr_index:
21362 fprintf_unfiltered (f, "address: ");
21363 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21364 break;
21365 case DW_FORM_block2:
21366 case DW_FORM_block4:
21367 case DW_FORM_block:
21368 case DW_FORM_block1:
21369 fprintf_unfiltered (f, "block: size %s",
21370 pulongest (DW_BLOCK (&die->attrs[i])->size));
21371 break;
21372 case DW_FORM_exprloc:
21373 fprintf_unfiltered (f, "expression: size %s",
21374 pulongest (DW_BLOCK (&die->attrs[i])->size));
21375 break;
21376 case DW_FORM_data16:
21377 fprintf_unfiltered (f, "constant of 16 bytes");
21378 break;
21379 case DW_FORM_ref_addr:
21380 fprintf_unfiltered (f, "ref address: ");
21381 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21382 break;
21383 case DW_FORM_GNU_ref_alt:
21384 fprintf_unfiltered (f, "alt ref address: ");
21385 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21386 break;
21387 case DW_FORM_ref1:
21388 case DW_FORM_ref2:
21389 case DW_FORM_ref4:
21390 case DW_FORM_ref8:
21391 case DW_FORM_ref_udata:
21392 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21393 (long) (DW_UNSND (&die->attrs[i])));
21394 break;
21395 case DW_FORM_data1:
21396 case DW_FORM_data2:
21397 case DW_FORM_data4:
21398 case DW_FORM_data8:
21399 case DW_FORM_udata:
21400 case DW_FORM_sdata:
21401 fprintf_unfiltered (f, "constant: %s",
21402 pulongest (DW_UNSND (&die->attrs[i])));
21403 break;
21404 case DW_FORM_sec_offset:
21405 fprintf_unfiltered (f, "section offset: %s",
21406 pulongest (DW_UNSND (&die->attrs[i])));
21407 break;
21408 case DW_FORM_ref_sig8:
21409 fprintf_unfiltered (f, "signature: %s",
21410 hex_string (DW_SIGNATURE (&die->attrs[i])));
21411 break;
21412 case DW_FORM_string:
21413 case DW_FORM_strp:
21414 case DW_FORM_line_strp:
21415 case DW_FORM_strx:
21416 case DW_FORM_GNU_str_index:
21417 case DW_FORM_GNU_strp_alt:
21418 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21419 DW_STRING (&die->attrs[i])
21420 ? DW_STRING (&die->attrs[i]) : "",
21421 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21422 break;
21423 case DW_FORM_flag:
21424 if (DW_UNSND (&die->attrs[i]))
21425 fprintf_unfiltered (f, "flag: TRUE");
21426 else
21427 fprintf_unfiltered (f, "flag: FALSE");
21428 break;
21429 case DW_FORM_flag_present:
21430 fprintf_unfiltered (f, "flag: TRUE");
21431 break;
21432 case DW_FORM_indirect:
21433 /* The reader will have reduced the indirect form to
21434 the "base form" so this form should not occur. */
21435 fprintf_unfiltered (f,
21436 "unexpected attribute form: DW_FORM_indirect");
21437 break;
21438 case DW_FORM_implicit_const:
21439 fprintf_unfiltered (f, "constant: %s",
21440 plongest (DW_SND (&die->attrs[i])));
21441 break;
21442 default:
21443 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21444 die->attrs[i].form);
21445 break;
21446 }
21447 fprintf_unfiltered (f, "\n");
21448 }
21449 }
21450
21451 static void
21452 dump_die_for_error (struct die_info *die)
21453 {
21454 dump_die_shallow (gdb_stderr, 0, die);
21455 }
21456
21457 static void
21458 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21459 {
21460 int indent = level * 4;
21461
21462 gdb_assert (die != NULL);
21463
21464 if (level >= max_level)
21465 return;
21466
21467 dump_die_shallow (f, indent, die);
21468
21469 if (die->child != NULL)
21470 {
21471 print_spaces (indent, f);
21472 fprintf_unfiltered (f, " Children:");
21473 if (level + 1 < max_level)
21474 {
21475 fprintf_unfiltered (f, "\n");
21476 dump_die_1 (f, level + 1, max_level, die->child);
21477 }
21478 else
21479 {
21480 fprintf_unfiltered (f,
21481 " [not printed, max nesting level reached]\n");
21482 }
21483 }
21484
21485 if (die->sibling != NULL && level > 0)
21486 {
21487 dump_die_1 (f, level, max_level, die->sibling);
21488 }
21489 }
21490
21491 /* This is called from the pdie macro in gdbinit.in.
21492 It's not static so gcc will keep a copy callable from gdb. */
21493
21494 void
21495 dump_die (struct die_info *die, int max_level)
21496 {
21497 dump_die_1 (gdb_stdlog, 0, max_level, die);
21498 }
21499
21500 static void
21501 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21502 {
21503 void **slot;
21504
21505 slot = htab_find_slot_with_hash (cu->die_hash, die,
21506 to_underlying (die->sect_off),
21507 INSERT);
21508
21509 *slot = die;
21510 }
21511
21512 /* Follow reference or signature attribute ATTR of SRC_DIE.
21513 On entry *REF_CU is the CU of SRC_DIE.
21514 On exit *REF_CU is the CU of the result. */
21515
21516 static struct die_info *
21517 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21518 struct dwarf2_cu **ref_cu)
21519 {
21520 struct die_info *die;
21521
21522 if (attr->form_is_ref ())
21523 die = follow_die_ref (src_die, attr, ref_cu);
21524 else if (attr->form == DW_FORM_ref_sig8)
21525 die = follow_die_sig (src_die, attr, ref_cu);
21526 else
21527 {
21528 dump_die_for_error (src_die);
21529 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21530 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
21531 }
21532
21533 return die;
21534 }
21535
21536 /* Follow reference OFFSET.
21537 On entry *REF_CU is the CU of the source die referencing OFFSET.
21538 On exit *REF_CU is the CU of the result.
21539 Returns NULL if OFFSET is invalid. */
21540
21541 static struct die_info *
21542 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21543 struct dwarf2_cu **ref_cu)
21544 {
21545 struct die_info temp_die;
21546 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21547 struct dwarf2_per_objfile *dwarf2_per_objfile
21548 = cu->per_cu->dwarf2_per_objfile;
21549
21550 gdb_assert (cu->per_cu != NULL);
21551
21552 target_cu = cu;
21553
21554 if (cu->per_cu->is_debug_types)
21555 {
21556 /* .debug_types CUs cannot reference anything outside their CU.
21557 If they need to, they have to reference a signatured type via
21558 DW_FORM_ref_sig8. */
21559 if (!cu->header.offset_in_cu_p (sect_off))
21560 return NULL;
21561 }
21562 else if (offset_in_dwz != cu->per_cu->is_dwz
21563 || !cu->header.offset_in_cu_p (sect_off))
21564 {
21565 struct dwarf2_per_cu_data *per_cu;
21566
21567 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21568 dwarf2_per_objfile);
21569
21570 /* If necessary, add it to the queue and load its DIEs. */
21571 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21572 load_full_comp_unit (per_cu, false, cu->language);
21573
21574 target_cu = per_cu->cu;
21575 }
21576 else if (cu->dies == NULL)
21577 {
21578 /* We're loading full DIEs during partial symbol reading. */
21579 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21580 load_full_comp_unit (cu->per_cu, false, language_minimal);
21581 }
21582
21583 *ref_cu = target_cu;
21584 temp_die.sect_off = sect_off;
21585
21586 if (target_cu != cu)
21587 target_cu->ancestor = cu;
21588
21589 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21590 &temp_die,
21591 to_underlying (sect_off));
21592 }
21593
21594 /* Follow reference attribute ATTR of SRC_DIE.
21595 On entry *REF_CU is the CU of SRC_DIE.
21596 On exit *REF_CU is the CU of the result. */
21597
21598 static struct die_info *
21599 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21600 struct dwarf2_cu **ref_cu)
21601 {
21602 sect_offset sect_off = attr->get_ref_die_offset ();
21603 struct dwarf2_cu *cu = *ref_cu;
21604 struct die_info *die;
21605
21606 die = follow_die_offset (sect_off,
21607 (attr->form == DW_FORM_GNU_ref_alt
21608 || cu->per_cu->is_dwz),
21609 ref_cu);
21610 if (!die)
21611 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21612 "at %s [in module %s]"),
21613 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
21614 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
21615
21616 return die;
21617 }
21618
21619 /* See read.h. */
21620
21621 struct dwarf2_locexpr_baton
21622 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21623 dwarf2_per_cu_data *per_cu,
21624 CORE_ADDR (*get_frame_pc) (void *baton),
21625 void *baton, bool resolve_abstract_p)
21626 {
21627 struct dwarf2_cu *cu;
21628 struct die_info *die;
21629 struct attribute *attr;
21630 struct dwarf2_locexpr_baton retval;
21631 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21632 struct objfile *objfile = dwarf2_per_objfile->objfile;
21633
21634 if (per_cu->cu == NULL)
21635 load_cu (per_cu, false);
21636 cu = per_cu->cu;
21637 if (cu == NULL)
21638 {
21639 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21640 Instead just throw an error, not much else we can do. */
21641 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21642 sect_offset_str (sect_off), objfile_name (objfile));
21643 }
21644
21645 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21646 if (!die)
21647 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21648 sect_offset_str (sect_off), objfile_name (objfile));
21649
21650 attr = dwarf2_attr (die, DW_AT_location, cu);
21651 if (!attr && resolve_abstract_p
21652 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
21653 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21654 {
21655 CORE_ADDR pc = (*get_frame_pc) (baton);
21656 CORE_ADDR baseaddr = objfile->text_section_offset ();
21657 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21658
21659 for (const auto &cand_off
21660 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
21661 {
21662 struct dwarf2_cu *cand_cu = cu;
21663 struct die_info *cand
21664 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21665 if (!cand
21666 || !cand->parent
21667 || cand->parent->tag != DW_TAG_subprogram)
21668 continue;
21669
21670 CORE_ADDR pc_low, pc_high;
21671 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
21672 if (pc_low == ((CORE_ADDR) -1))
21673 continue;
21674 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21675 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21676 if (!(pc_low <= pc && pc < pc_high))
21677 continue;
21678
21679 die = cand;
21680 attr = dwarf2_attr (die, DW_AT_location, cu);
21681 break;
21682 }
21683 }
21684
21685 if (!attr)
21686 {
21687 /* DWARF: "If there is no such attribute, then there is no effect.".
21688 DATA is ignored if SIZE is 0. */
21689
21690 retval.data = NULL;
21691 retval.size = 0;
21692 }
21693 else if (attr->form_is_section_offset ())
21694 {
21695 struct dwarf2_loclist_baton loclist_baton;
21696 CORE_ADDR pc = (*get_frame_pc) (baton);
21697 size_t size;
21698
21699 fill_in_loclist_baton (cu, &loclist_baton, attr);
21700
21701 retval.data = dwarf2_find_location_expression (&loclist_baton,
21702 &size, pc);
21703 retval.size = size;
21704 }
21705 else
21706 {
21707 if (!attr->form_is_block ())
21708 error (_("Dwarf Error: DIE at %s referenced in module %s "
21709 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21710 sect_offset_str (sect_off), objfile_name (objfile));
21711
21712 retval.data = DW_BLOCK (attr)->data;
21713 retval.size = DW_BLOCK (attr)->size;
21714 }
21715 retval.per_cu = cu->per_cu;
21716
21717 age_cached_comp_units (dwarf2_per_objfile);
21718
21719 return retval;
21720 }
21721
21722 /* See read.h. */
21723
21724 struct dwarf2_locexpr_baton
21725 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21726 dwarf2_per_cu_data *per_cu,
21727 CORE_ADDR (*get_frame_pc) (void *baton),
21728 void *baton)
21729 {
21730 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21731
21732 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21733 }
21734
21735 /* Write a constant of a given type as target-ordered bytes into
21736 OBSTACK. */
21737
21738 static const gdb_byte *
21739 write_constant_as_bytes (struct obstack *obstack,
21740 enum bfd_endian byte_order,
21741 struct type *type,
21742 ULONGEST value,
21743 LONGEST *len)
21744 {
21745 gdb_byte *result;
21746
21747 *len = TYPE_LENGTH (type);
21748 result = (gdb_byte *) obstack_alloc (obstack, *len);
21749 store_unsigned_integer (result, *len, byte_order, value);
21750
21751 return result;
21752 }
21753
21754 /* See read.h. */
21755
21756 const gdb_byte *
21757 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21758 dwarf2_per_cu_data *per_cu,
21759 obstack *obstack,
21760 LONGEST *len)
21761 {
21762 struct dwarf2_cu *cu;
21763 struct die_info *die;
21764 struct attribute *attr;
21765 const gdb_byte *result = NULL;
21766 struct type *type;
21767 LONGEST value;
21768 enum bfd_endian byte_order;
21769 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
21770
21771 if (per_cu->cu == NULL)
21772 load_cu (per_cu, false);
21773 cu = per_cu->cu;
21774 if (cu == NULL)
21775 {
21776 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21777 Instead just throw an error, not much else we can do. */
21778 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21779 sect_offset_str (sect_off), objfile_name (objfile));
21780 }
21781
21782 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21783 if (!die)
21784 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21785 sect_offset_str (sect_off), objfile_name (objfile));
21786
21787 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21788 if (attr == NULL)
21789 return NULL;
21790
21791 byte_order = (bfd_big_endian (objfile->obfd)
21792 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21793
21794 switch (attr->form)
21795 {
21796 case DW_FORM_addr:
21797 case DW_FORM_addrx:
21798 case DW_FORM_GNU_addr_index:
21799 {
21800 gdb_byte *tem;
21801
21802 *len = cu->header.addr_size;
21803 tem = (gdb_byte *) obstack_alloc (obstack, *len);
21804 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21805 result = tem;
21806 }
21807 break;
21808 case DW_FORM_string:
21809 case DW_FORM_strp:
21810 case DW_FORM_strx:
21811 case DW_FORM_GNU_str_index:
21812 case DW_FORM_GNU_strp_alt:
21813 /* DW_STRING is already allocated on the objfile obstack, point
21814 directly to it. */
21815 result = (const gdb_byte *) DW_STRING (attr);
21816 *len = strlen (DW_STRING (attr));
21817 break;
21818 case DW_FORM_block1:
21819 case DW_FORM_block2:
21820 case DW_FORM_block4:
21821 case DW_FORM_block:
21822 case DW_FORM_exprloc:
21823 case DW_FORM_data16:
21824 result = DW_BLOCK (attr)->data;
21825 *len = DW_BLOCK (attr)->size;
21826 break;
21827
21828 /* The DW_AT_const_value attributes are supposed to carry the
21829 symbol's value "represented as it would be on the target
21830 architecture." By the time we get here, it's already been
21831 converted to host endianness, so we just need to sign- or
21832 zero-extend it as appropriate. */
21833 case DW_FORM_data1:
21834 type = die_type (die, cu);
21835 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21836 if (result == NULL)
21837 result = write_constant_as_bytes (obstack, byte_order,
21838 type, value, len);
21839 break;
21840 case DW_FORM_data2:
21841 type = die_type (die, cu);
21842 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21843 if (result == NULL)
21844 result = write_constant_as_bytes (obstack, byte_order,
21845 type, value, len);
21846 break;
21847 case DW_FORM_data4:
21848 type = die_type (die, cu);
21849 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21850 if (result == NULL)
21851 result = write_constant_as_bytes (obstack, byte_order,
21852 type, value, len);
21853 break;
21854 case DW_FORM_data8:
21855 type = die_type (die, cu);
21856 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21857 if (result == NULL)
21858 result = write_constant_as_bytes (obstack, byte_order,
21859 type, value, len);
21860 break;
21861
21862 case DW_FORM_sdata:
21863 case DW_FORM_implicit_const:
21864 type = die_type (die, cu);
21865 result = write_constant_as_bytes (obstack, byte_order,
21866 type, DW_SND (attr), len);
21867 break;
21868
21869 case DW_FORM_udata:
21870 type = die_type (die, cu);
21871 result = write_constant_as_bytes (obstack, byte_order,
21872 type, DW_UNSND (attr), len);
21873 break;
21874
21875 default:
21876 complaint (_("unsupported const value attribute form: '%s'"),
21877 dwarf_form_name (attr->form));
21878 break;
21879 }
21880
21881 return result;
21882 }
21883
21884 /* See read.h. */
21885
21886 struct type *
21887 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
21888 dwarf2_per_cu_data *per_cu)
21889 {
21890 struct dwarf2_cu *cu;
21891 struct die_info *die;
21892
21893 if (per_cu->cu == NULL)
21894 load_cu (per_cu, false);
21895 cu = per_cu->cu;
21896 if (!cu)
21897 return NULL;
21898
21899 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21900 if (!die)
21901 return NULL;
21902
21903 return die_type (die, cu);
21904 }
21905
21906 /* See read.h. */
21907
21908 struct type *
21909 dwarf2_get_die_type (cu_offset die_offset,
21910 struct dwarf2_per_cu_data *per_cu)
21911 {
21912 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
21913 return get_die_type_at_offset (die_offset_sect, per_cu);
21914 }
21915
21916 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
21917 On entry *REF_CU is the CU of SRC_DIE.
21918 On exit *REF_CU is the CU of the result.
21919 Returns NULL if the referenced DIE isn't found. */
21920
21921 static struct die_info *
21922 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21923 struct dwarf2_cu **ref_cu)
21924 {
21925 struct die_info temp_die;
21926 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
21927 struct die_info *die;
21928
21929 /* While it might be nice to assert sig_type->type == NULL here,
21930 we can get here for DW_AT_imported_declaration where we need
21931 the DIE not the type. */
21932
21933 /* If necessary, add it to the queue and load its DIEs. */
21934
21935 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
21936 read_signatured_type (sig_type);
21937
21938 sig_cu = sig_type->per_cu.cu;
21939 gdb_assert (sig_cu != NULL);
21940 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
21941 temp_die.sect_off = sig_type->type_offset_in_section;
21942 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
21943 to_underlying (temp_die.sect_off));
21944 if (die)
21945 {
21946 struct dwarf2_per_objfile *dwarf2_per_objfile
21947 = (*ref_cu)->per_cu->dwarf2_per_objfile;
21948
21949 /* For .gdb_index version 7 keep track of included TUs.
21950 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
21951 if (dwarf2_per_objfile->index_table != NULL
21952 && dwarf2_per_objfile->index_table->version <= 7)
21953 {
21954 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
21955 }
21956
21957 *ref_cu = sig_cu;
21958 if (sig_cu != cu)
21959 sig_cu->ancestor = cu;
21960
21961 return die;
21962 }
21963
21964 return NULL;
21965 }
21966
21967 /* Follow signatured type referenced by ATTR in SRC_DIE.
21968 On entry *REF_CU is the CU of SRC_DIE.
21969 On exit *REF_CU is the CU of the result.
21970 The result is the DIE of the type.
21971 If the referenced type cannot be found an error is thrown. */
21972
21973 static struct die_info *
21974 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
21975 struct dwarf2_cu **ref_cu)
21976 {
21977 ULONGEST signature = DW_SIGNATURE (attr);
21978 struct signatured_type *sig_type;
21979 struct die_info *die;
21980
21981 gdb_assert (attr->form == DW_FORM_ref_sig8);
21982
21983 sig_type = lookup_signatured_type (*ref_cu, signature);
21984 /* sig_type will be NULL if the signatured type is missing from
21985 the debug info. */
21986 if (sig_type == NULL)
21987 {
21988 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
21989 " from DIE at %s [in module %s]"),
21990 hex_string (signature), sect_offset_str (src_die->sect_off),
21991 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
21992 }
21993
21994 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
21995 if (die == NULL)
21996 {
21997 dump_die_for_error (src_die);
21998 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
21999 " from DIE at %s [in module %s]"),
22000 hex_string (signature), sect_offset_str (src_die->sect_off),
22001 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22002 }
22003
22004 return die;
22005 }
22006
22007 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22008 reading in and processing the type unit if necessary. */
22009
22010 static struct type *
22011 get_signatured_type (struct die_info *die, ULONGEST signature,
22012 struct dwarf2_cu *cu)
22013 {
22014 struct dwarf2_per_objfile *dwarf2_per_objfile
22015 = cu->per_cu->dwarf2_per_objfile;
22016 struct signatured_type *sig_type;
22017 struct dwarf2_cu *type_cu;
22018 struct die_info *type_die;
22019 struct type *type;
22020
22021 sig_type = lookup_signatured_type (cu, signature);
22022 /* sig_type will be NULL if the signatured type is missing from
22023 the debug info. */
22024 if (sig_type == NULL)
22025 {
22026 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22027 " from DIE at %s [in module %s]"),
22028 hex_string (signature), sect_offset_str (die->sect_off),
22029 objfile_name (dwarf2_per_objfile->objfile));
22030 return build_error_marker_type (cu, die);
22031 }
22032
22033 /* If we already know the type we're done. */
22034 if (sig_type->type != NULL)
22035 return sig_type->type;
22036
22037 type_cu = cu;
22038 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22039 if (type_die != NULL)
22040 {
22041 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22042 is created. This is important, for example, because for c++ classes
22043 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22044 type = read_type_die (type_die, type_cu);
22045 if (type == NULL)
22046 {
22047 complaint (_("Dwarf Error: Cannot build signatured type %s"
22048 " referenced from DIE at %s [in module %s]"),
22049 hex_string (signature), sect_offset_str (die->sect_off),
22050 objfile_name (dwarf2_per_objfile->objfile));
22051 type = build_error_marker_type (cu, die);
22052 }
22053 }
22054 else
22055 {
22056 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22057 " from DIE at %s [in module %s]"),
22058 hex_string (signature), sect_offset_str (die->sect_off),
22059 objfile_name (dwarf2_per_objfile->objfile));
22060 type = build_error_marker_type (cu, die);
22061 }
22062 sig_type->type = type;
22063
22064 return type;
22065 }
22066
22067 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22068 reading in and processing the type unit if necessary. */
22069
22070 static struct type *
22071 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22072 struct dwarf2_cu *cu) /* ARI: editCase function */
22073 {
22074 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22075 if (attr->form_is_ref ())
22076 {
22077 struct dwarf2_cu *type_cu = cu;
22078 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22079
22080 return read_type_die (type_die, type_cu);
22081 }
22082 else if (attr->form == DW_FORM_ref_sig8)
22083 {
22084 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22085 }
22086 else
22087 {
22088 struct dwarf2_per_objfile *dwarf2_per_objfile
22089 = cu->per_cu->dwarf2_per_objfile;
22090
22091 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22092 " at %s [in module %s]"),
22093 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22094 objfile_name (dwarf2_per_objfile->objfile));
22095 return build_error_marker_type (cu, die);
22096 }
22097 }
22098
22099 /* Load the DIEs associated with type unit PER_CU into memory. */
22100
22101 static void
22102 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22103 {
22104 struct signatured_type *sig_type;
22105
22106 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22107 gdb_assert (! per_cu->type_unit_group_p ());
22108
22109 /* We have the per_cu, but we need the signatured_type.
22110 Fortunately this is an easy translation. */
22111 gdb_assert (per_cu->is_debug_types);
22112 sig_type = (struct signatured_type *) per_cu;
22113
22114 gdb_assert (per_cu->cu == NULL);
22115
22116 read_signatured_type (sig_type);
22117
22118 gdb_assert (per_cu->cu != NULL);
22119 }
22120
22121 /* Read in a signatured type and build its CU and DIEs.
22122 If the type is a stub for the real type in a DWO file,
22123 read in the real type from the DWO file as well. */
22124
22125 static void
22126 read_signatured_type (struct signatured_type *sig_type)
22127 {
22128 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22129
22130 gdb_assert (per_cu->is_debug_types);
22131 gdb_assert (per_cu->cu == NULL);
22132
22133 cutu_reader reader (per_cu, NULL, 0, false);
22134
22135 if (!reader.dummy_p)
22136 {
22137 struct dwarf2_cu *cu = reader.cu;
22138 const gdb_byte *info_ptr = reader.info_ptr;
22139
22140 gdb_assert (cu->die_hash == NULL);
22141 cu->die_hash =
22142 htab_create_alloc_ex (cu->header.length / 12,
22143 die_hash,
22144 die_eq,
22145 NULL,
22146 &cu->comp_unit_obstack,
22147 hashtab_obstack_allocate,
22148 dummy_obstack_deallocate);
22149
22150 if (reader.comp_unit_die->has_children)
22151 reader.comp_unit_die->child
22152 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22153 reader.comp_unit_die);
22154 cu->dies = reader.comp_unit_die;
22155 /* comp_unit_die is not stored in die_hash, no need. */
22156
22157 /* We try not to read any attributes in this function, because
22158 not all CUs needed for references have been loaded yet, and
22159 symbol table processing isn't initialized. But we have to
22160 set the CU language, or we won't be able to build types
22161 correctly. Similarly, if we do not read the producer, we can
22162 not apply producer-specific interpretation. */
22163 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22164
22165 reader.keep ();
22166 }
22167
22168 sig_type->per_cu.tu_read = 1;
22169 }
22170
22171 /* Decode simple location descriptions.
22172 Given a pointer to a dwarf block that defines a location, compute
22173 the location and return the value.
22174
22175 NOTE drow/2003-11-18: This function is called in two situations
22176 now: for the address of static or global variables (partial symbols
22177 only) and for offsets into structures which are expected to be
22178 (more or less) constant. The partial symbol case should go away,
22179 and only the constant case should remain. That will let this
22180 function complain more accurately. A few special modes are allowed
22181 without complaint for global variables (for instance, global
22182 register values and thread-local values).
22183
22184 A location description containing no operations indicates that the
22185 object is optimized out. The return value is 0 for that case.
22186 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22187 callers will only want a very basic result and this can become a
22188 complaint.
22189
22190 Note that stack[0] is unused except as a default error return. */
22191
22192 static CORE_ADDR
22193 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22194 {
22195 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22196 size_t i;
22197 size_t size = blk->size;
22198 const gdb_byte *data = blk->data;
22199 CORE_ADDR stack[64];
22200 int stacki;
22201 unsigned int bytes_read, unsnd;
22202 gdb_byte op;
22203
22204 i = 0;
22205 stacki = 0;
22206 stack[stacki] = 0;
22207 stack[++stacki] = 0;
22208
22209 while (i < size)
22210 {
22211 op = data[i++];
22212 switch (op)
22213 {
22214 case DW_OP_lit0:
22215 case DW_OP_lit1:
22216 case DW_OP_lit2:
22217 case DW_OP_lit3:
22218 case DW_OP_lit4:
22219 case DW_OP_lit5:
22220 case DW_OP_lit6:
22221 case DW_OP_lit7:
22222 case DW_OP_lit8:
22223 case DW_OP_lit9:
22224 case DW_OP_lit10:
22225 case DW_OP_lit11:
22226 case DW_OP_lit12:
22227 case DW_OP_lit13:
22228 case DW_OP_lit14:
22229 case DW_OP_lit15:
22230 case DW_OP_lit16:
22231 case DW_OP_lit17:
22232 case DW_OP_lit18:
22233 case DW_OP_lit19:
22234 case DW_OP_lit20:
22235 case DW_OP_lit21:
22236 case DW_OP_lit22:
22237 case DW_OP_lit23:
22238 case DW_OP_lit24:
22239 case DW_OP_lit25:
22240 case DW_OP_lit26:
22241 case DW_OP_lit27:
22242 case DW_OP_lit28:
22243 case DW_OP_lit29:
22244 case DW_OP_lit30:
22245 case DW_OP_lit31:
22246 stack[++stacki] = op - DW_OP_lit0;
22247 break;
22248
22249 case DW_OP_reg0:
22250 case DW_OP_reg1:
22251 case DW_OP_reg2:
22252 case DW_OP_reg3:
22253 case DW_OP_reg4:
22254 case DW_OP_reg5:
22255 case DW_OP_reg6:
22256 case DW_OP_reg7:
22257 case DW_OP_reg8:
22258 case DW_OP_reg9:
22259 case DW_OP_reg10:
22260 case DW_OP_reg11:
22261 case DW_OP_reg12:
22262 case DW_OP_reg13:
22263 case DW_OP_reg14:
22264 case DW_OP_reg15:
22265 case DW_OP_reg16:
22266 case DW_OP_reg17:
22267 case DW_OP_reg18:
22268 case DW_OP_reg19:
22269 case DW_OP_reg20:
22270 case DW_OP_reg21:
22271 case DW_OP_reg22:
22272 case DW_OP_reg23:
22273 case DW_OP_reg24:
22274 case DW_OP_reg25:
22275 case DW_OP_reg26:
22276 case DW_OP_reg27:
22277 case DW_OP_reg28:
22278 case DW_OP_reg29:
22279 case DW_OP_reg30:
22280 case DW_OP_reg31:
22281 stack[++stacki] = op - DW_OP_reg0;
22282 if (i < size)
22283 dwarf2_complex_location_expr_complaint ();
22284 break;
22285
22286 case DW_OP_regx:
22287 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22288 i += bytes_read;
22289 stack[++stacki] = unsnd;
22290 if (i < size)
22291 dwarf2_complex_location_expr_complaint ();
22292 break;
22293
22294 case DW_OP_addr:
22295 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22296 &bytes_read);
22297 i += bytes_read;
22298 break;
22299
22300 case DW_OP_const1u:
22301 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22302 i += 1;
22303 break;
22304
22305 case DW_OP_const1s:
22306 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22307 i += 1;
22308 break;
22309
22310 case DW_OP_const2u:
22311 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22312 i += 2;
22313 break;
22314
22315 case DW_OP_const2s:
22316 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22317 i += 2;
22318 break;
22319
22320 case DW_OP_const4u:
22321 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22322 i += 4;
22323 break;
22324
22325 case DW_OP_const4s:
22326 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22327 i += 4;
22328 break;
22329
22330 case DW_OP_const8u:
22331 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22332 i += 8;
22333 break;
22334
22335 case DW_OP_constu:
22336 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22337 &bytes_read);
22338 i += bytes_read;
22339 break;
22340
22341 case DW_OP_consts:
22342 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22343 i += bytes_read;
22344 break;
22345
22346 case DW_OP_dup:
22347 stack[stacki + 1] = stack[stacki];
22348 stacki++;
22349 break;
22350
22351 case DW_OP_plus:
22352 stack[stacki - 1] += stack[stacki];
22353 stacki--;
22354 break;
22355
22356 case DW_OP_plus_uconst:
22357 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22358 &bytes_read);
22359 i += bytes_read;
22360 break;
22361
22362 case DW_OP_minus:
22363 stack[stacki - 1] -= stack[stacki];
22364 stacki--;
22365 break;
22366
22367 case DW_OP_deref:
22368 /* If we're not the last op, then we definitely can't encode
22369 this using GDB's address_class enum. This is valid for partial
22370 global symbols, although the variable's address will be bogus
22371 in the psymtab. */
22372 if (i < size)
22373 dwarf2_complex_location_expr_complaint ();
22374 break;
22375
22376 case DW_OP_GNU_push_tls_address:
22377 case DW_OP_form_tls_address:
22378 /* The top of the stack has the offset from the beginning
22379 of the thread control block at which the variable is located. */
22380 /* Nothing should follow this operator, so the top of stack would
22381 be returned. */
22382 /* This is valid for partial global symbols, but the variable's
22383 address will be bogus in the psymtab. Make it always at least
22384 non-zero to not look as a variable garbage collected by linker
22385 which have DW_OP_addr 0. */
22386 if (i < size)
22387 dwarf2_complex_location_expr_complaint ();
22388 stack[stacki]++;
22389 break;
22390
22391 case DW_OP_GNU_uninit:
22392 break;
22393
22394 case DW_OP_addrx:
22395 case DW_OP_GNU_addr_index:
22396 case DW_OP_GNU_const_index:
22397 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22398 &bytes_read);
22399 i += bytes_read;
22400 break;
22401
22402 default:
22403 {
22404 const char *name = get_DW_OP_name (op);
22405
22406 if (name)
22407 complaint (_("unsupported stack op: '%s'"),
22408 name);
22409 else
22410 complaint (_("unsupported stack op: '%02x'"),
22411 op);
22412 }
22413
22414 return (stack[stacki]);
22415 }
22416
22417 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22418 outside of the allocated space. Also enforce minimum>0. */
22419 if (stacki >= ARRAY_SIZE (stack) - 1)
22420 {
22421 complaint (_("location description stack overflow"));
22422 return 0;
22423 }
22424
22425 if (stacki <= 0)
22426 {
22427 complaint (_("location description stack underflow"));
22428 return 0;
22429 }
22430 }
22431 return (stack[stacki]);
22432 }
22433
22434 /* memory allocation interface */
22435
22436 static struct dwarf_block *
22437 dwarf_alloc_block (struct dwarf2_cu *cu)
22438 {
22439 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22440 }
22441
22442 static struct die_info *
22443 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22444 {
22445 struct die_info *die;
22446 size_t size = sizeof (struct die_info);
22447
22448 if (num_attrs > 1)
22449 size += (num_attrs - 1) * sizeof (struct attribute);
22450
22451 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22452 memset (die, 0, sizeof (struct die_info));
22453 return (die);
22454 }
22455
22456 \f
22457
22458 /* Macro support. */
22459
22460 /* An overload of dwarf_decode_macros that finds the correct section
22461 and ensures it is read in before calling the other overload. */
22462
22463 static void
22464 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22465 int section_is_gnu)
22466 {
22467 struct dwarf2_per_objfile *dwarf2_per_objfile
22468 = cu->per_cu->dwarf2_per_objfile;
22469 struct objfile *objfile = dwarf2_per_objfile->objfile;
22470 const struct line_header *lh = cu->line_header;
22471 unsigned int offset_size = cu->header.offset_size;
22472 struct dwarf2_section_info *section;
22473 const char *section_name;
22474
22475 if (cu->dwo_unit != nullptr)
22476 {
22477 if (section_is_gnu)
22478 {
22479 section = &cu->dwo_unit->dwo_file->sections.macro;
22480 section_name = ".debug_macro.dwo";
22481 }
22482 else
22483 {
22484 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22485 section_name = ".debug_macinfo.dwo";
22486 }
22487 }
22488 else
22489 {
22490 if (section_is_gnu)
22491 {
22492 section = &dwarf2_per_objfile->macro;
22493 section_name = ".debug_macro";
22494 }
22495 else
22496 {
22497 section = &dwarf2_per_objfile->macinfo;
22498 section_name = ".debug_macinfo";
22499 }
22500 }
22501
22502 section->read (objfile);
22503 if (section->buffer == nullptr)
22504 {
22505 complaint (_("missing %s section"), section_name);
22506 return;
22507 }
22508
22509 buildsym_compunit *builder = cu->get_builder ();
22510
22511 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22512 offset_size, offset, section_is_gnu);
22513 }
22514
22515 /* Return the .debug_loc section to use for CU.
22516 For DWO files use .debug_loc.dwo. */
22517
22518 static struct dwarf2_section_info *
22519 cu_debug_loc_section (struct dwarf2_cu *cu)
22520 {
22521 struct dwarf2_per_objfile *dwarf2_per_objfile
22522 = cu->per_cu->dwarf2_per_objfile;
22523
22524 if (cu->dwo_unit)
22525 {
22526 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22527
22528 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22529 }
22530 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22531 : &dwarf2_per_objfile->loc);
22532 }
22533
22534 /* A helper function that fills in a dwarf2_loclist_baton. */
22535
22536 static void
22537 fill_in_loclist_baton (struct dwarf2_cu *cu,
22538 struct dwarf2_loclist_baton *baton,
22539 const struct attribute *attr)
22540 {
22541 struct dwarf2_per_objfile *dwarf2_per_objfile
22542 = cu->per_cu->dwarf2_per_objfile;
22543 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22544
22545 section->read (dwarf2_per_objfile->objfile);
22546
22547 baton->per_cu = cu->per_cu;
22548 gdb_assert (baton->per_cu);
22549 /* We don't know how long the location list is, but make sure we
22550 don't run off the edge of the section. */
22551 baton->size = section->size - DW_UNSND (attr);
22552 baton->data = section->buffer + DW_UNSND (attr);
22553 if (cu->base_address.has_value ())
22554 baton->base_address = *cu->base_address;
22555 else
22556 baton->base_address = 0;
22557 baton->from_dwo = cu->dwo_unit != NULL;
22558 }
22559
22560 static void
22561 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22562 struct dwarf2_cu *cu, int is_block)
22563 {
22564 struct dwarf2_per_objfile *dwarf2_per_objfile
22565 = cu->per_cu->dwarf2_per_objfile;
22566 struct objfile *objfile = dwarf2_per_objfile->objfile;
22567 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22568
22569 if (attr->form_is_section_offset ()
22570 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22571 the section. If so, fall through to the complaint in the
22572 other branch. */
22573 && DW_UNSND (attr) < section->get_size (objfile))
22574 {
22575 struct dwarf2_loclist_baton *baton;
22576
22577 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22578
22579 fill_in_loclist_baton (cu, baton, attr);
22580
22581 if (!cu->base_address.has_value ())
22582 complaint (_("Location list used without "
22583 "specifying the CU base address."));
22584
22585 SYMBOL_ACLASS_INDEX (sym) = (is_block
22586 ? dwarf2_loclist_block_index
22587 : dwarf2_loclist_index);
22588 SYMBOL_LOCATION_BATON (sym) = baton;
22589 }
22590 else
22591 {
22592 struct dwarf2_locexpr_baton *baton;
22593
22594 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22595 baton->per_cu = cu->per_cu;
22596 gdb_assert (baton->per_cu);
22597
22598 if (attr->form_is_block ())
22599 {
22600 /* Note that we're just copying the block's data pointer
22601 here, not the actual data. We're still pointing into the
22602 info_buffer for SYM's objfile; right now we never release
22603 that buffer, but when we do clean up properly this may
22604 need to change. */
22605 baton->size = DW_BLOCK (attr)->size;
22606 baton->data = DW_BLOCK (attr)->data;
22607 }
22608 else
22609 {
22610 dwarf2_invalid_attrib_class_complaint ("location description",
22611 sym->natural_name ());
22612 baton->size = 0;
22613 }
22614
22615 SYMBOL_ACLASS_INDEX (sym) = (is_block
22616 ? dwarf2_locexpr_block_index
22617 : dwarf2_locexpr_index);
22618 SYMBOL_LOCATION_BATON (sym) = baton;
22619 }
22620 }
22621
22622 /* See read.h. */
22623
22624 struct objfile *
22625 dwarf2_per_cu_data::objfile () const
22626 {
22627 struct objfile *objfile = dwarf2_per_objfile->objfile;
22628
22629 /* Return the master objfile, so that we can report and look up the
22630 correct file containing this variable. */
22631 if (objfile->separate_debug_objfile_backlink)
22632 objfile = objfile->separate_debug_objfile_backlink;
22633
22634 return objfile;
22635 }
22636
22637 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22638 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22639 CU_HEADERP first. */
22640
22641 static const struct comp_unit_head *
22642 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22643 const struct dwarf2_per_cu_data *per_cu)
22644 {
22645 const gdb_byte *info_ptr;
22646
22647 if (per_cu->cu)
22648 return &per_cu->cu->header;
22649
22650 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22651
22652 memset (cu_headerp, 0, sizeof (*cu_headerp));
22653 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22654 rcuh_kind::COMPILE);
22655
22656 return cu_headerp;
22657 }
22658
22659 /* See read.h. */
22660
22661 int
22662 dwarf2_per_cu_data::addr_size () const
22663 {
22664 struct comp_unit_head cu_header_local;
22665 const struct comp_unit_head *cu_headerp;
22666
22667 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22668
22669 return cu_headerp->addr_size;
22670 }
22671
22672 /* See read.h. */
22673
22674 int
22675 dwarf2_per_cu_data::offset_size () const
22676 {
22677 struct comp_unit_head cu_header_local;
22678 const struct comp_unit_head *cu_headerp;
22679
22680 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22681
22682 return cu_headerp->offset_size;
22683 }
22684
22685 /* See read.h. */
22686
22687 int
22688 dwarf2_per_cu_data::ref_addr_size () const
22689 {
22690 struct comp_unit_head cu_header_local;
22691 const struct comp_unit_head *cu_headerp;
22692
22693 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22694
22695 if (cu_headerp->version == 2)
22696 return cu_headerp->addr_size;
22697 else
22698 return cu_headerp->offset_size;
22699 }
22700
22701 /* See read.h. */
22702
22703 CORE_ADDR
22704 dwarf2_per_cu_data::text_offset () const
22705 {
22706 struct objfile *objfile = dwarf2_per_objfile->objfile;
22707
22708 return objfile->text_section_offset ();
22709 }
22710
22711 /* See read.h. */
22712
22713 struct type *
22714 dwarf2_per_cu_data::addr_type () const
22715 {
22716 struct objfile *objfile = dwarf2_per_objfile->objfile;
22717 struct type *void_type = objfile_type (objfile)->builtin_void;
22718 struct type *addr_type = lookup_pointer_type (void_type);
22719 int addr_size = this->addr_size ();
22720
22721 if (TYPE_LENGTH (addr_type) == addr_size)
22722 return addr_type;
22723
22724 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
22725 return addr_type;
22726 }
22727
22728 /* A helper function for dwarf2_find_containing_comp_unit that returns
22729 the index of the result, and that searches a vector. It will
22730 return a result even if the offset in question does not actually
22731 occur in any CU. This is separate so that it can be unit
22732 tested. */
22733
22734 static int
22735 dwarf2_find_containing_comp_unit
22736 (sect_offset sect_off,
22737 unsigned int offset_in_dwz,
22738 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
22739 {
22740 int low, high;
22741
22742 low = 0;
22743 high = all_comp_units.size () - 1;
22744 while (high > low)
22745 {
22746 struct dwarf2_per_cu_data *mid_cu;
22747 int mid = low + (high - low) / 2;
22748
22749 mid_cu = all_comp_units[mid];
22750 if (mid_cu->is_dwz > offset_in_dwz
22751 || (mid_cu->is_dwz == offset_in_dwz
22752 && mid_cu->sect_off + mid_cu->length > sect_off))
22753 high = mid;
22754 else
22755 low = mid + 1;
22756 }
22757 gdb_assert (low == high);
22758 return low;
22759 }
22760
22761 /* Locate the .debug_info compilation unit from CU's objfile which contains
22762 the DIE at OFFSET. Raises an error on failure. */
22763
22764 static struct dwarf2_per_cu_data *
22765 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22766 unsigned int offset_in_dwz,
22767 struct dwarf2_per_objfile *dwarf2_per_objfile)
22768 {
22769 int low
22770 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22771 dwarf2_per_objfile->all_comp_units);
22772 struct dwarf2_per_cu_data *this_cu
22773 = dwarf2_per_objfile->all_comp_units[low];
22774
22775 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
22776 {
22777 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22778 error (_("Dwarf Error: could not find partial DIE containing "
22779 "offset %s [in module %s]"),
22780 sect_offset_str (sect_off),
22781 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
22782
22783 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22784 <= sect_off);
22785 return dwarf2_per_objfile->all_comp_units[low-1];
22786 }
22787 else
22788 {
22789 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
22790 && sect_off >= this_cu->sect_off + this_cu->length)
22791 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
22792 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
22793 return this_cu;
22794 }
22795 }
22796
22797 #if GDB_SELF_TEST
22798
22799 namespace selftests {
22800 namespace find_containing_comp_unit {
22801
22802 static void
22803 run_test ()
22804 {
22805 struct dwarf2_per_cu_data one {};
22806 struct dwarf2_per_cu_data two {};
22807 struct dwarf2_per_cu_data three {};
22808 struct dwarf2_per_cu_data four {};
22809
22810 one.length = 5;
22811 two.sect_off = sect_offset (one.length);
22812 two.length = 7;
22813
22814 three.length = 5;
22815 three.is_dwz = 1;
22816 four.sect_off = sect_offset (three.length);
22817 four.length = 7;
22818 four.is_dwz = 1;
22819
22820 std::vector<dwarf2_per_cu_data *> units;
22821 units.push_back (&one);
22822 units.push_back (&two);
22823 units.push_back (&three);
22824 units.push_back (&four);
22825
22826 int result;
22827
22828 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
22829 SELF_CHECK (units[result] == &one);
22830 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
22831 SELF_CHECK (units[result] == &one);
22832 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
22833 SELF_CHECK (units[result] == &two);
22834
22835 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
22836 SELF_CHECK (units[result] == &three);
22837 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
22838 SELF_CHECK (units[result] == &three);
22839 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
22840 SELF_CHECK (units[result] == &four);
22841 }
22842
22843 }
22844 }
22845
22846 #endif /* GDB_SELF_TEST */
22847
22848 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22849
22850 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
22851 : per_cu (per_cu_),
22852 mark (false),
22853 has_loclist (false),
22854 checked_producer (false),
22855 producer_is_gxx_lt_4_6 (false),
22856 producer_is_gcc_lt_4_3 (false),
22857 producer_is_icc (false),
22858 producer_is_icc_lt_14 (false),
22859 producer_is_codewarrior (false),
22860 processing_has_namespace_info (false)
22861 {
22862 per_cu->cu = this;
22863 }
22864
22865 /* Destroy a dwarf2_cu. */
22866
22867 dwarf2_cu::~dwarf2_cu ()
22868 {
22869 per_cu->cu = NULL;
22870 }
22871
22872 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22873
22874 static void
22875 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22876 enum language pretend_language)
22877 {
22878 struct attribute *attr;
22879
22880 /* Set the language we're debugging. */
22881 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
22882 if (attr != nullptr)
22883 set_cu_language (DW_UNSND (attr), cu);
22884 else
22885 {
22886 cu->language = pretend_language;
22887 cu->language_defn = language_def (cu->language);
22888 }
22889
22890 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
22891 }
22892
22893 /* Increase the age counter on each cached compilation unit, and free
22894 any that are too old. */
22895
22896 static void
22897 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
22898 {
22899 struct dwarf2_per_cu_data *per_cu, **last_chain;
22900
22901 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22902 per_cu = dwarf2_per_objfile->read_in_chain;
22903 while (per_cu != NULL)
22904 {
22905 per_cu->cu->last_used ++;
22906 if (per_cu->cu->last_used <= dwarf_max_cache_age)
22907 dwarf2_mark (per_cu->cu);
22908 per_cu = per_cu->cu->read_in_chain;
22909 }
22910
22911 per_cu = dwarf2_per_objfile->read_in_chain;
22912 last_chain = &dwarf2_per_objfile->read_in_chain;
22913 while (per_cu != NULL)
22914 {
22915 struct dwarf2_per_cu_data *next_cu;
22916
22917 next_cu = per_cu->cu->read_in_chain;
22918
22919 if (!per_cu->cu->mark)
22920 {
22921 delete per_cu->cu;
22922 *last_chain = next_cu;
22923 }
22924 else
22925 last_chain = &per_cu->cu->read_in_chain;
22926
22927 per_cu = next_cu;
22928 }
22929 }
22930
22931 /* Remove a single compilation unit from the cache. */
22932
22933 static void
22934 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
22935 {
22936 struct dwarf2_per_cu_data *per_cu, **last_chain;
22937 struct dwarf2_per_objfile *dwarf2_per_objfile
22938 = target_per_cu->dwarf2_per_objfile;
22939
22940 per_cu = dwarf2_per_objfile->read_in_chain;
22941 last_chain = &dwarf2_per_objfile->read_in_chain;
22942 while (per_cu != NULL)
22943 {
22944 struct dwarf2_per_cu_data *next_cu;
22945
22946 next_cu = per_cu->cu->read_in_chain;
22947
22948 if (per_cu == target_per_cu)
22949 {
22950 delete per_cu->cu;
22951 per_cu->cu = NULL;
22952 *last_chain = next_cu;
22953 break;
22954 }
22955 else
22956 last_chain = &per_cu->cu->read_in_chain;
22957
22958 per_cu = next_cu;
22959 }
22960 }
22961
22962 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
22963 We store these in a hash table separate from the DIEs, and preserve them
22964 when the DIEs are flushed out of cache.
22965
22966 The CU "per_cu" pointer is needed because offset alone is not enough to
22967 uniquely identify the type. A file may have multiple .debug_types sections,
22968 or the type may come from a DWO file. Furthermore, while it's more logical
22969 to use per_cu->section+offset, with Fission the section with the data is in
22970 the DWO file but we don't know that section at the point we need it.
22971 We have to use something in dwarf2_per_cu_data (or the pointer to it)
22972 because we can enter the lookup routine, get_die_type_at_offset, from
22973 outside this file, and thus won't necessarily have PER_CU->cu.
22974 Fortunately, PER_CU is stable for the life of the objfile. */
22975
22976 struct dwarf2_per_cu_offset_and_type
22977 {
22978 const struct dwarf2_per_cu_data *per_cu;
22979 sect_offset sect_off;
22980 struct type *type;
22981 };
22982
22983 /* Hash function for a dwarf2_per_cu_offset_and_type. */
22984
22985 static hashval_t
22986 per_cu_offset_and_type_hash (const void *item)
22987 {
22988 const struct dwarf2_per_cu_offset_and_type *ofs
22989 = (const struct dwarf2_per_cu_offset_and_type *) item;
22990
22991 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
22992 }
22993
22994 /* Equality function for a dwarf2_per_cu_offset_and_type. */
22995
22996 static int
22997 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
22998 {
22999 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23000 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23001 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23002 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23003
23004 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23005 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23006 }
23007
23008 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23009 table if necessary. For convenience, return TYPE.
23010
23011 The DIEs reading must have careful ordering to:
23012 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23013 reading current DIE.
23014 * Not trying to dereference contents of still incompletely read in types
23015 while reading in other DIEs.
23016 * Enable referencing still incompletely read in types just by a pointer to
23017 the type without accessing its fields.
23018
23019 Therefore caller should follow these rules:
23020 * Try to fetch any prerequisite types we may need to build this DIE type
23021 before building the type and calling set_die_type.
23022 * After building type call set_die_type for current DIE as soon as
23023 possible before fetching more types to complete the current type.
23024 * Make the type as complete as possible before fetching more types. */
23025
23026 static struct type *
23027 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23028 {
23029 struct dwarf2_per_objfile *dwarf2_per_objfile
23030 = cu->per_cu->dwarf2_per_objfile;
23031 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23032 struct objfile *objfile = dwarf2_per_objfile->objfile;
23033 struct attribute *attr;
23034 struct dynamic_prop prop;
23035
23036 /* For Ada types, make sure that the gnat-specific data is always
23037 initialized (if not already set). There are a few types where
23038 we should not be doing so, because the type-specific area is
23039 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23040 where the type-specific area is used to store the floatformat).
23041 But this is not a problem, because the gnat-specific information
23042 is actually not needed for these types. */
23043 if (need_gnat_info (cu)
23044 && TYPE_CODE (type) != TYPE_CODE_FUNC
23045 && TYPE_CODE (type) != TYPE_CODE_FLT
23046 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23047 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23048 && TYPE_CODE (type) != TYPE_CODE_METHOD
23049 && !HAVE_GNAT_AUX_INFO (type))
23050 INIT_GNAT_SPECIFIC (type);
23051
23052 /* Read DW_AT_allocated and set in type. */
23053 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23054 if (attr != NULL && attr->form_is_block ())
23055 {
23056 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23057 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23058 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23059 }
23060 else if (attr != NULL)
23061 {
23062 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23063 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23064 sect_offset_str (die->sect_off));
23065 }
23066
23067 /* Read DW_AT_associated and set in type. */
23068 attr = dwarf2_attr (die, DW_AT_associated, cu);
23069 if (attr != NULL && attr->form_is_block ())
23070 {
23071 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23072 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23073 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23074 }
23075 else if (attr != NULL)
23076 {
23077 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23078 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23079 sect_offset_str (die->sect_off));
23080 }
23081
23082 /* Read DW_AT_data_location and set in type. */
23083 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23084 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23085 cu->per_cu->addr_type ()))
23086 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23087
23088 if (dwarf2_per_objfile->die_type_hash == NULL)
23089 dwarf2_per_objfile->die_type_hash
23090 = htab_up (htab_create_alloc (127,
23091 per_cu_offset_and_type_hash,
23092 per_cu_offset_and_type_eq,
23093 NULL, xcalloc, xfree));
23094
23095 ofs.per_cu = cu->per_cu;
23096 ofs.sect_off = die->sect_off;
23097 ofs.type = type;
23098 slot = (struct dwarf2_per_cu_offset_and_type **)
23099 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23100 if (*slot)
23101 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23102 sect_offset_str (die->sect_off));
23103 *slot = XOBNEW (&objfile->objfile_obstack,
23104 struct dwarf2_per_cu_offset_and_type);
23105 **slot = ofs;
23106 return type;
23107 }
23108
23109 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23110 or return NULL if the die does not have a saved type. */
23111
23112 static struct type *
23113 get_die_type_at_offset (sect_offset sect_off,
23114 struct dwarf2_per_cu_data *per_cu)
23115 {
23116 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23117 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23118
23119 if (dwarf2_per_objfile->die_type_hash == NULL)
23120 return NULL;
23121
23122 ofs.per_cu = per_cu;
23123 ofs.sect_off = sect_off;
23124 slot = ((struct dwarf2_per_cu_offset_and_type *)
23125 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23126 if (slot)
23127 return slot->type;
23128 else
23129 return NULL;
23130 }
23131
23132 /* Look up the type for DIE in CU in die_type_hash,
23133 or return NULL if DIE does not have a saved type. */
23134
23135 static struct type *
23136 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23137 {
23138 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23139 }
23140
23141 /* Add a dependence relationship from CU to REF_PER_CU. */
23142
23143 static void
23144 dwarf2_add_dependence (struct dwarf2_cu *cu,
23145 struct dwarf2_per_cu_data *ref_per_cu)
23146 {
23147 void **slot;
23148
23149 if (cu->dependencies == NULL)
23150 cu->dependencies
23151 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23152 NULL, &cu->comp_unit_obstack,
23153 hashtab_obstack_allocate,
23154 dummy_obstack_deallocate);
23155
23156 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23157 if (*slot == NULL)
23158 *slot = ref_per_cu;
23159 }
23160
23161 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23162 Set the mark field in every compilation unit in the
23163 cache that we must keep because we are keeping CU. */
23164
23165 static int
23166 dwarf2_mark_helper (void **slot, void *data)
23167 {
23168 struct dwarf2_per_cu_data *per_cu;
23169
23170 per_cu = (struct dwarf2_per_cu_data *) *slot;
23171
23172 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23173 reading of the chain. As such dependencies remain valid it is not much
23174 useful to track and undo them during QUIT cleanups. */
23175 if (per_cu->cu == NULL)
23176 return 1;
23177
23178 if (per_cu->cu->mark)
23179 return 1;
23180 per_cu->cu->mark = true;
23181
23182 if (per_cu->cu->dependencies != NULL)
23183 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23184
23185 return 1;
23186 }
23187
23188 /* Set the mark field in CU and in every other compilation unit in the
23189 cache that we must keep because we are keeping CU. */
23190
23191 static void
23192 dwarf2_mark (struct dwarf2_cu *cu)
23193 {
23194 if (cu->mark)
23195 return;
23196 cu->mark = true;
23197 if (cu->dependencies != NULL)
23198 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23199 }
23200
23201 static void
23202 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23203 {
23204 while (per_cu)
23205 {
23206 per_cu->cu->mark = false;
23207 per_cu = per_cu->cu->read_in_chain;
23208 }
23209 }
23210
23211 /* Trivial hash function for partial_die_info: the hash value of a DIE
23212 is its offset in .debug_info for this objfile. */
23213
23214 static hashval_t
23215 partial_die_hash (const void *item)
23216 {
23217 const struct partial_die_info *part_die
23218 = (const struct partial_die_info *) item;
23219
23220 return to_underlying (part_die->sect_off);
23221 }
23222
23223 /* Trivial comparison function for partial_die_info structures: two DIEs
23224 are equal if they have the same offset. */
23225
23226 static int
23227 partial_die_eq (const void *item_lhs, const void *item_rhs)
23228 {
23229 const struct partial_die_info *part_die_lhs
23230 = (const struct partial_die_info *) item_lhs;
23231 const struct partial_die_info *part_die_rhs
23232 = (const struct partial_die_info *) item_rhs;
23233
23234 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23235 }
23236
23237 struct cmd_list_element *set_dwarf_cmdlist;
23238 struct cmd_list_element *show_dwarf_cmdlist;
23239
23240 static void
23241 set_dwarf_cmd (const char *args, int from_tty)
23242 {
23243 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23244 gdb_stdout);
23245 }
23246
23247 static void
23248 show_dwarf_cmd (const char *args, int from_tty)
23249 {
23250 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23251 }
23252
23253 static void
23254 show_check_physname (struct ui_file *file, int from_tty,
23255 struct cmd_list_element *c, const char *value)
23256 {
23257 fprintf_filtered (file,
23258 _("Whether to check \"physname\" is %s.\n"),
23259 value);
23260 }
23261
23262 void _initialize_dwarf2_read ();
23263 void
23264 _initialize_dwarf2_read ()
23265 {
23266 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23267 Set DWARF specific variables.\n\
23268 Configure DWARF variables such as the cache size."),
23269 &set_dwarf_cmdlist, "maintenance set dwarf ",
23270 0/*allow-unknown*/, &maintenance_set_cmdlist);
23271
23272 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23273 Show DWARF specific variables.\n\
23274 Show DWARF variables such as the cache size."),
23275 &show_dwarf_cmdlist, "maintenance show dwarf ",
23276 0/*allow-unknown*/, &maintenance_show_cmdlist);
23277
23278 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23279 &dwarf_max_cache_age, _("\
23280 Set the upper bound on the age of cached DWARF compilation units."), _("\
23281 Show the upper bound on the age of cached DWARF compilation units."), _("\
23282 A higher limit means that cached compilation units will be stored\n\
23283 in memory longer, and more total memory will be used. Zero disables\n\
23284 caching, which can slow down startup."),
23285 NULL,
23286 show_dwarf_max_cache_age,
23287 &set_dwarf_cmdlist,
23288 &show_dwarf_cmdlist);
23289
23290 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23291 Set debugging of the DWARF reader."), _("\
23292 Show debugging of the DWARF reader."), _("\
23293 When enabled (non-zero), debugging messages are printed during DWARF\n\
23294 reading and symtab expansion. A value of 1 (one) provides basic\n\
23295 information. A value greater than 1 provides more verbose information."),
23296 NULL,
23297 NULL,
23298 &setdebuglist, &showdebuglist);
23299
23300 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23301 Set debugging of the DWARF DIE reader."), _("\
23302 Show debugging of the DWARF DIE reader."), _("\
23303 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23304 The value is the maximum depth to print."),
23305 NULL,
23306 NULL,
23307 &setdebuglist, &showdebuglist);
23308
23309 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23310 Set debugging of the dwarf line reader."), _("\
23311 Show debugging of the dwarf line reader."), _("\
23312 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23313 A value of 1 (one) provides basic information.\n\
23314 A value greater than 1 provides more verbose information."),
23315 NULL,
23316 NULL,
23317 &setdebuglist, &showdebuglist);
23318
23319 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23320 Set cross-checking of \"physname\" code against demangler."), _("\
23321 Show cross-checking of \"physname\" code against demangler."), _("\
23322 When enabled, GDB's internal \"physname\" code is checked against\n\
23323 the demangler."),
23324 NULL, show_check_physname,
23325 &setdebuglist, &showdebuglist);
23326
23327 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23328 no_class, &use_deprecated_index_sections, _("\
23329 Set whether to use deprecated gdb_index sections."), _("\
23330 Show whether to use deprecated gdb_index sections."), _("\
23331 When enabled, deprecated .gdb_index sections are used anyway.\n\
23332 Normally they are ignored either because of a missing feature or\n\
23333 performance issue.\n\
23334 Warning: This option must be enabled before gdb reads the file."),
23335 NULL,
23336 NULL,
23337 &setlist, &showlist);
23338
23339 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23340 &dwarf2_locexpr_funcs);
23341 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23342 &dwarf2_loclist_funcs);
23343
23344 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23345 &dwarf2_block_frame_base_locexpr_funcs);
23346 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23347 &dwarf2_block_frame_base_loclist_funcs);
23348
23349 #if GDB_SELF_TEST
23350 selftests::register_test ("dw2_expand_symtabs_matching",
23351 selftests::dw2_expand_symtabs_matching::run_test);
23352 selftests::register_test ("dwarf2_find_containing_comp_unit",
23353 selftests::find_containing_comp_unit::run_test);
23354 #endif
23355 }
This page took 0.512227 seconds and 5 git commands to generate.