8bde26534e300c52c8f5ccd97b7f5667b49c9ffa
[deliverable/binutils-gdb.git] / gdb / dwarf2read.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 "dwarf2read.h"
33 #include "dwarf-index-cache.h"
34 #include "dwarf-index-common.h"
35 #include "bfd.h"
36 #include "elf-bfd.h"
37 #include "symtab.h"
38 #include "gdbtypes.h"
39 #include "objfiles.h"
40 #include "dwarf2.h"
41 #include "buildsym.h"
42 #include "demangle.h"
43 #include "gdb-demangle.h"
44 #include "filenames.h" /* for DOSish file names */
45 #include "macrotab.h"
46 #include "language.h"
47 #include "complaints.h"
48 #include "dwarf2expr.h"
49 #include "dwarf2loc.h"
50 #include "cp-support.h"
51 #include "hashtab.h"
52 #include "command.h"
53 #include "gdbcmd.h"
54 #include "block.h"
55 #include "addrmap.h"
56 #include "typeprint.h"
57 #include "psympriv.h"
58 #include "c-lang.h"
59 #include "go-lang.h"
60 #include "valprint.h"
61 #include "gdbcore.h" /* for gnutarget */
62 #include "gdb/gdb-index.h"
63 #include "gdb_bfd.h"
64 #include "f-lang.h"
65 #include "source.h"
66 #include "build-id.h"
67 #include "namespace.h"
68 #include "gdbsupport/function-view.h"
69 #include "gdbsupport/gdb_optional.h"
70 #include "gdbsupport/underlying.h"
71 #include "gdbsupport/hash_enum.h"
72 #include "filename-seen-cache.h"
73 #include "producer.h"
74 #include <fcntl.h>
75 #include <algorithm>
76 #include <unordered_map>
77 #include "gdbsupport/selftest.h"
78 #include "rust-lang.h"
79 #include "gdbsupport/pathstuff.h"
80
81 /* When == 1, print basic high level tracing messages.
82 When > 1, be more verbose.
83 This is in contrast to the low level DIE reading of dwarf_die_debug. */
84 static unsigned int dwarf_read_debug = 0;
85
86 /* When non-zero, dump DIEs after they are read in. */
87 static unsigned int dwarf_die_debug = 0;
88
89 /* When non-zero, dump line number entries as they are read in. */
90 static unsigned int dwarf_line_debug = 0;
91
92 /* When true, cross-check physname against demangler. */
93 static bool check_physname = false;
94
95 /* When true, do not reject deprecated .gdb_index sections. */
96 static bool use_deprecated_index_sections = false;
97
98 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
99
100 /* The "aclass" indices for various kinds of computed DWARF symbols. */
101
102 static int dwarf2_locexpr_index;
103 static int dwarf2_loclist_index;
104 static int dwarf2_locexpr_block_index;
105 static int dwarf2_loclist_block_index;
106
107 /* An index into a (C++) symbol name component in a symbol name as
108 recorded in the mapped_index's symbol table. For each C++ symbol
109 in the symbol table, we record one entry for the start of each
110 component in the symbol in a table of name components, and then
111 sort the table, in order to be able to binary search symbol names,
112 ignoring leading namespaces, both completion and regular look up.
113 For example, for symbol "A::B::C", we'll have an entry that points
114 to "A::B::C", another that points to "B::C", and another for "C".
115 Note that function symbols in GDB index have no parameter
116 information, just the function/method names. You can convert a
117 name_component to a "const char *" using the
118 'mapped_index::symbol_name_at(offset_type)' method. */
119
120 struct name_component
121 {
122 /* Offset in the symbol name where the component starts. Stored as
123 a (32-bit) offset instead of a pointer to save memory and improve
124 locality on 64-bit architectures. */
125 offset_type name_offset;
126
127 /* The symbol's index in the symbol and constant pool tables of a
128 mapped_index. */
129 offset_type idx;
130 };
131
132 /* Base class containing bits shared by both .gdb_index and
133 .debug_name indexes. */
134
135 struct mapped_index_base
136 {
137 mapped_index_base () = default;
138 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
139
140 /* The name_component table (a sorted vector). See name_component's
141 description above. */
142 std::vector<name_component> name_components;
143
144 /* How NAME_COMPONENTS is sorted. */
145 enum case_sensitivity name_components_casing;
146
147 /* Return the number of names in the symbol table. */
148 virtual size_t symbol_name_count () const = 0;
149
150 /* Get the name of the symbol at IDX in the symbol table. */
151 virtual const char *symbol_name_at (offset_type idx) const = 0;
152
153 /* Return whether the name at IDX in the symbol table should be
154 ignored. */
155 virtual bool symbol_name_slot_invalid (offset_type idx) const
156 {
157 return false;
158 }
159
160 /* Build the symbol name component sorted vector, if we haven't
161 yet. */
162 void build_name_components ();
163
164 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
165 possible matches for LN_NO_PARAMS in the name component
166 vector. */
167 std::pair<std::vector<name_component>::const_iterator,
168 std::vector<name_component>::const_iterator>
169 find_name_components_bounds (const lookup_name_info &ln_no_params,
170 enum language lang) const;
171
172 /* Prevent deleting/destroying via a base class pointer. */
173 protected:
174 ~mapped_index_base() = default;
175 };
176
177 /* A description of the mapped index. The file format is described in
178 a comment by the code that writes the index. */
179 struct mapped_index final : public mapped_index_base
180 {
181 /* A slot/bucket in the symbol table hash. */
182 struct symbol_table_slot
183 {
184 const offset_type name;
185 const offset_type vec;
186 };
187
188 /* Index data format version. */
189 int version = 0;
190
191 /* The address table data. */
192 gdb::array_view<const gdb_byte> address_table;
193
194 /* The symbol table, implemented as a hash table. */
195 gdb::array_view<symbol_table_slot> symbol_table;
196
197 /* A pointer to the constant pool. */
198 const char *constant_pool = nullptr;
199
200 bool symbol_name_slot_invalid (offset_type idx) const override
201 {
202 const auto &bucket = this->symbol_table[idx];
203 return bucket.name == 0 && bucket.vec == 0;
204 }
205
206 /* Convenience method to get at the name of the symbol at IDX in the
207 symbol table. */
208 const char *symbol_name_at (offset_type idx) const override
209 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
210
211 size_t symbol_name_count () const override
212 { return this->symbol_table.size (); }
213 };
214
215 /* A description of the mapped .debug_names.
216 Uninitialized map has CU_COUNT 0. */
217 struct mapped_debug_names final : public mapped_index_base
218 {
219 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
220 : dwarf2_per_objfile (dwarf2_per_objfile_)
221 {}
222
223 struct dwarf2_per_objfile *dwarf2_per_objfile;
224 bfd_endian dwarf5_byte_order;
225 bool dwarf5_is_dwarf64;
226 bool augmentation_is_gdb;
227 uint8_t offset_size;
228 uint32_t cu_count = 0;
229 uint32_t tu_count, bucket_count, name_count;
230 const gdb_byte *cu_table_reordered, *tu_table_reordered;
231 const uint32_t *bucket_table_reordered, *hash_table_reordered;
232 const gdb_byte *name_table_string_offs_reordered;
233 const gdb_byte *name_table_entry_offs_reordered;
234 const gdb_byte *entry_pool;
235
236 struct index_val
237 {
238 ULONGEST dwarf_tag;
239 struct attr
240 {
241 /* Attribute name DW_IDX_*. */
242 ULONGEST dw_idx;
243
244 /* Attribute form DW_FORM_*. */
245 ULONGEST form;
246
247 /* Value if FORM is DW_FORM_implicit_const. */
248 LONGEST implicit_const;
249 };
250 std::vector<attr> attr_vec;
251 };
252
253 std::unordered_map<ULONGEST, index_val> abbrev_map;
254
255 const char *namei_to_name (uint32_t namei) const;
256
257 /* Implementation of the mapped_index_base virtual interface, for
258 the name_components cache. */
259
260 const char *symbol_name_at (offset_type idx) const override
261 { return namei_to_name (idx); }
262
263 size_t symbol_name_count () const override
264 { return this->name_count; }
265 };
266
267 /* See dwarf2read.h. */
268
269 dwarf2_per_objfile *
270 get_dwarf2_per_objfile (struct objfile *objfile)
271 {
272 return dwarf2_objfile_data_key.get (objfile);
273 }
274
275 /* Default names of the debugging sections. */
276
277 /* Note that if the debugging section has been compressed, it might
278 have a name like .zdebug_info. */
279
280 static const struct dwarf2_debug_sections dwarf2_elf_names =
281 {
282 { ".debug_info", ".zdebug_info" },
283 { ".debug_abbrev", ".zdebug_abbrev" },
284 { ".debug_line", ".zdebug_line" },
285 { ".debug_loc", ".zdebug_loc" },
286 { ".debug_loclists", ".zdebug_loclists" },
287 { ".debug_macinfo", ".zdebug_macinfo" },
288 { ".debug_macro", ".zdebug_macro" },
289 { ".debug_str", ".zdebug_str" },
290 { ".debug_str_offsets", ".zdebug_str_offsets" },
291 { ".debug_line_str", ".zdebug_line_str" },
292 { ".debug_ranges", ".zdebug_ranges" },
293 { ".debug_rnglists", ".zdebug_rnglists" },
294 { ".debug_types", ".zdebug_types" },
295 { ".debug_addr", ".zdebug_addr" },
296 { ".debug_frame", ".zdebug_frame" },
297 { ".eh_frame", NULL },
298 { ".gdb_index", ".zgdb_index" },
299 { ".debug_names", ".zdebug_names" },
300 { ".debug_aranges", ".zdebug_aranges" },
301 23
302 };
303
304 /* List of DWO/DWP sections. */
305
306 static const struct dwop_section_names
307 {
308 struct dwarf2_section_names abbrev_dwo;
309 struct dwarf2_section_names info_dwo;
310 struct dwarf2_section_names line_dwo;
311 struct dwarf2_section_names loc_dwo;
312 struct dwarf2_section_names loclists_dwo;
313 struct dwarf2_section_names macinfo_dwo;
314 struct dwarf2_section_names macro_dwo;
315 struct dwarf2_section_names str_dwo;
316 struct dwarf2_section_names str_offsets_dwo;
317 struct dwarf2_section_names types_dwo;
318 struct dwarf2_section_names cu_index;
319 struct dwarf2_section_names tu_index;
320 }
321 dwop_section_names =
322 {
323 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
324 { ".debug_info.dwo", ".zdebug_info.dwo" },
325 { ".debug_line.dwo", ".zdebug_line.dwo" },
326 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
327 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
328 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
329 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
330 { ".debug_str.dwo", ".zdebug_str.dwo" },
331 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
332 { ".debug_types.dwo", ".zdebug_types.dwo" },
333 { ".debug_cu_index", ".zdebug_cu_index" },
334 { ".debug_tu_index", ".zdebug_tu_index" },
335 };
336
337 /* local data types */
338
339 /* The data in a compilation unit header, after target2host
340 translation, looks like this. */
341 struct comp_unit_head
342 {
343 unsigned int length;
344 short version;
345 unsigned char addr_size;
346 unsigned char signed_addr_p;
347 sect_offset abbrev_sect_off;
348
349 /* Size of file offsets; either 4 or 8. */
350 unsigned int offset_size;
351
352 /* Size of the length field; either 4 or 12. */
353 unsigned int initial_length_size;
354
355 enum dwarf_unit_type unit_type;
356
357 /* Offset to the first byte of this compilation unit header in the
358 .debug_info section, for resolving relative reference dies. */
359 sect_offset sect_off;
360
361 /* Offset to first die in this cu from the start of the cu.
362 This will be the first byte following the compilation unit header. */
363 cu_offset first_die_cu_offset;
364
365
366 /* 64-bit signature of this unit. For type units, it denotes the signature of
367 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
368 Also used in DWARF 5, to denote the dwo id when the unit type is
369 DW_UT_skeleton or DW_UT_split_compile. */
370 ULONGEST signature;
371
372 /* For types, offset in the type's DIE of the type defined by this TU. */
373 cu_offset type_cu_offset_in_tu;
374 };
375
376 /* Type used for delaying computation of method physnames.
377 See comments for compute_delayed_physnames. */
378 struct delayed_method_info
379 {
380 /* The type to which the method is attached, i.e., its parent class. */
381 struct type *type;
382
383 /* The index of the method in the type's function fieldlists. */
384 int fnfield_index;
385
386 /* The index of the method in the fieldlist. */
387 int index;
388
389 /* The name of the DIE. */
390 const char *name;
391
392 /* The DIE associated with this method. */
393 struct die_info *die;
394 };
395
396 /* Internal state when decoding a particular compilation unit. */
397 struct dwarf2_cu
398 {
399 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
400 ~dwarf2_cu ();
401
402 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
403
404 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
405 Create the set of symtabs used by this TU, or if this TU is sharing
406 symtabs with another TU and the symtabs have already been created
407 then restore those symtabs in the line header.
408 We don't need the pc/line-number mapping for type units. */
409 void setup_type_unit_groups (struct die_info *die);
410
411 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
412 buildsym_compunit constructor. */
413 struct compunit_symtab *start_symtab (const char *name,
414 const char *comp_dir,
415 CORE_ADDR low_pc);
416
417 /* Reset the builder. */
418 void reset_builder () { m_builder.reset (); }
419
420 /* The header of the compilation unit. */
421 struct comp_unit_head header {};
422
423 /* Base address of this compilation unit. */
424 CORE_ADDR base_address = 0;
425
426 /* Non-zero if base_address has been set. */
427 int base_known = 0;
428
429 /* The language we are debugging. */
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
432
433 const char *producer = nullptr;
434
435 private:
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
438 std::unique_ptr<buildsym_compunit> m_builder;
439
440 public:
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
450 struct pending **list_in_scope = nullptr;
451
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
454 htab_t partial_dies = nullptr;
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
458 auto_obstack comp_unit_obstack;
459
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
465
466 /* Backlink to our per_cu entry. */
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
470 int last_used = 0;
471
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
474 htab_t die_hash = nullptr;
475
476 /* Full DIEs if read in. */
477 struct die_info *dies = nullptr;
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
482 htab_t dependencies = nullptr;
483
484 /* Header data from the line table, during full symbol processing. */
485 struct line_header *line_header = nullptr;
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
491 die_info *line_header_die_owner = nullptr;
492
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
495 std::vector<delayed_method_info> method_list;
496
497 /* To be copied to symtab->call_site_htab. */
498 htab_t call_site_htab = nullptr;
499
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
509 struct dwo_unit *dwo_unit = nullptr;
510
511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
512 Note this value comes from the Fission stub CU/TU's DIE. */
513 gdb::optional<ULONGEST> addr_base;
514
515 /* The DW_AT_rnglists_base attribute if present.
516 Note this value comes from the Fission stub CU/TU's DIE.
517 Also note that the value is zero in the non-DWO case so this value can
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
524 ULONGEST ranges_base = 0;
525
526 /* When reading debug info generated by older versions of rustc, we
527 have to rewrite some union types to be struct types with a
528 variant part. This rewriting must be done after the CU is fully
529 read in, because otherwise at the point of rewriting some struct
530 type might not have been fully processed. So, we keep a list of
531 all such types here and process them after expansion. */
532 std::vector<struct type *> rust_unions;
533
534 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
535 files, the value is implicitly zero. For DWARF 5 version DWO files, the
536 value is often implicit and is the size of the header of
537 .debug_str_offsets section (8 or 4, depending on the address size). */
538 gdb::optional<ULONGEST> str_offsets_base;
539
540 /* Mark used when releasing cached dies. */
541 bool mark : 1;
542
543 /* This CU references .debug_loc. See the symtab->locations_valid field.
544 This test is imperfect as there may exist optimized debug code not using
545 any location list and still facing inlining issues if handled as
546 unoptimized code. For a future better test see GCC PR other/32998. */
547 bool has_loclist : 1;
548
549 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
550 if all the producer_is_* fields are valid. This information is cached
551 because profiling CU expansion showed excessive time spent in
552 producer_is_gxx_lt_4_6. */
553 bool checked_producer : 1;
554 bool producer_is_gxx_lt_4_6 : 1;
555 bool producer_is_gcc_lt_4_3 : 1;
556 bool producer_is_icc : 1;
557 bool producer_is_icc_lt_14 : 1;
558 bool producer_is_codewarrior : 1;
559
560 /* When true, the file that we're processing is known to have
561 debugging info for C++ namespaces. GCC 3.3.x did not produce
562 this information, but later versions do. */
563
564 bool processing_has_namespace_info : 1;
565
566 struct partial_die_info *find_partial_die (sect_offset sect_off);
567
568 /* If this CU was inherited by another CU (via specification,
569 abstract_origin, etc), this is the ancestor CU. */
570 dwarf2_cu *ancestor;
571
572 /* Get the buildsym_compunit for this CU. */
573 buildsym_compunit *get_builder ()
574 {
575 /* If this CU has a builder associated with it, use that. */
576 if (m_builder != nullptr)
577 return m_builder.get ();
578
579 /* Otherwise, search ancestors for a valid builder. */
580 if (ancestor != nullptr)
581 return ancestor->get_builder ();
582
583 return nullptr;
584 }
585 };
586
587 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
588 This includes type_unit_group and quick_file_names. */
589
590 struct stmt_list_hash
591 {
592 /* The DWO unit this table is from or NULL if there is none. */
593 struct dwo_unit *dwo_unit;
594
595 /* Offset in .debug_line or .debug_line.dwo. */
596 sect_offset line_sect_off;
597 };
598
599 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
600 an object of this type. */
601
602 struct type_unit_group
603 {
604 /* dwarf2read.c's main "handle" on a TU symtab.
605 To simplify things we create an artificial CU that "includes" all the
606 type units using this stmt_list so that the rest of the code still has
607 a "per_cu" handle on the symtab.
608 This PER_CU is recognized by having no section. */
609 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
610 struct dwarf2_per_cu_data per_cu;
611
612 /* The TUs that share this DW_AT_stmt_list entry.
613 This is added to while parsing type units to build partial symtabs,
614 and is deleted afterwards and not used again. */
615 std::vector<signatured_type *> *tus;
616
617 /* The compunit symtab.
618 Type units in a group needn't all be defined in the same source file,
619 so we create an essentially anonymous symtab as the compunit symtab. */
620 struct compunit_symtab *compunit_symtab;
621
622 /* The data used to construct the hash key. */
623 struct stmt_list_hash hash;
624
625 /* The number of symtabs from the line header.
626 The value here must match line_header.num_file_names. */
627 unsigned int num_symtabs;
628
629 /* The symbol tables for this TU (obtained from the files listed in
630 DW_AT_stmt_list).
631 WARNING: The order of entries here must match the order of entries
632 in the line header. After the first TU using this type_unit_group, the
633 line header for the subsequent TUs is recreated from this. This is done
634 because we need to use the same symtabs for each TU using the same
635 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
636 there's no guarantee the line header doesn't have duplicate entries. */
637 struct symtab **symtabs;
638 };
639
640 /* These sections are what may appear in a (real or virtual) DWO file. */
641
642 struct dwo_sections
643 {
644 struct dwarf2_section_info abbrev;
645 struct dwarf2_section_info line;
646 struct dwarf2_section_info loc;
647 struct dwarf2_section_info loclists;
648 struct dwarf2_section_info macinfo;
649 struct dwarf2_section_info macro;
650 struct dwarf2_section_info str;
651 struct dwarf2_section_info str_offsets;
652 /* In the case of a virtual DWO file, these two are unused. */
653 struct dwarf2_section_info info;
654 std::vector<dwarf2_section_info> types;
655 };
656
657 /* CUs/TUs in DWP/DWO files. */
658
659 struct dwo_unit
660 {
661 /* Backlink to the containing struct dwo_file. */
662 struct dwo_file *dwo_file;
663
664 /* The "id" that distinguishes this CU/TU.
665 .debug_info calls this "dwo_id", .debug_types calls this "signature".
666 Since signatures came first, we stick with it for consistency. */
667 ULONGEST signature;
668
669 /* The section this CU/TU lives in, in the DWO file. */
670 struct dwarf2_section_info *section;
671
672 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
673 sect_offset sect_off;
674 unsigned int length;
675
676 /* For types, offset in the type's DIE of the type defined by this TU. */
677 cu_offset type_offset_in_tu;
678 };
679
680 /* include/dwarf2.h defines the DWP section codes.
681 It defines a max value but it doesn't define a min value, which we
682 use for error checking, so provide one. */
683
684 enum dwp_v2_section_ids
685 {
686 DW_SECT_MIN = 1
687 };
688
689 /* Data for one DWO file.
690
691 This includes virtual DWO files (a virtual DWO file is a DWO file as it
692 appears in a DWP file). DWP files don't really have DWO files per se -
693 comdat folding of types "loses" the DWO file they came from, and from
694 a high level view DWP files appear to contain a mass of random types.
695 However, to maintain consistency with the non-DWP case we pretend DWP
696 files contain virtual DWO files, and we assign each TU with one virtual
697 DWO file (generally based on the line and abbrev section offsets -
698 a heuristic that seems to work in practice). */
699
700 struct dwo_file
701 {
702 dwo_file () = default;
703 DISABLE_COPY_AND_ASSIGN (dwo_file);
704
705 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
706 For virtual DWO files the name is constructed from the section offsets
707 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
708 from related CU+TUs. */
709 const char *dwo_name = nullptr;
710
711 /* The DW_AT_comp_dir attribute. */
712 const char *comp_dir = nullptr;
713
714 /* The bfd, when the file is open. Otherwise this is NULL.
715 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
716 gdb_bfd_ref_ptr dbfd;
717
718 /* The sections that make up this DWO file.
719 Remember that for virtual DWO files in DWP V2, these are virtual
720 sections (for lack of a better name). */
721 struct dwo_sections sections {};
722
723 /* The CUs in the file.
724 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
725 an extension to handle LLVM's Link Time Optimization output (where
726 multiple source files may be compiled into a single object/dwo pair). */
727 htab_t cus {};
728
729 /* Table of TUs in the file.
730 Each element is a struct dwo_unit. */
731 htab_t tus {};
732 };
733
734 /* These sections are what may appear in a DWP file. */
735
736 struct dwp_sections
737 {
738 /* These are used by both DWP version 1 and 2. */
739 struct dwarf2_section_info str;
740 struct dwarf2_section_info cu_index;
741 struct dwarf2_section_info tu_index;
742
743 /* These are only used by DWP version 2 files.
744 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
745 sections are referenced by section number, and are not recorded here.
746 In DWP version 2 there is at most one copy of all these sections, each
747 section being (effectively) comprised of the concatenation of all of the
748 individual sections that exist in the version 1 format.
749 To keep the code simple we treat each of these concatenated pieces as a
750 section itself (a virtual section?). */
751 struct dwarf2_section_info abbrev;
752 struct dwarf2_section_info info;
753 struct dwarf2_section_info line;
754 struct dwarf2_section_info loc;
755 struct dwarf2_section_info macinfo;
756 struct dwarf2_section_info macro;
757 struct dwarf2_section_info str_offsets;
758 struct dwarf2_section_info types;
759 };
760
761 /* These sections are what may appear in a virtual DWO file in DWP version 1.
762 A virtual DWO file is a DWO file as it appears in a DWP file. */
763
764 struct virtual_v1_dwo_sections
765 {
766 struct dwarf2_section_info abbrev;
767 struct dwarf2_section_info line;
768 struct dwarf2_section_info loc;
769 struct dwarf2_section_info macinfo;
770 struct dwarf2_section_info macro;
771 struct dwarf2_section_info str_offsets;
772 /* Each DWP hash table entry records one CU or one TU.
773 That is recorded here, and copied to dwo_unit.section. */
774 struct dwarf2_section_info info_or_types;
775 };
776
777 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
778 In version 2, the sections of the DWO files are concatenated together
779 and stored in one section of that name. Thus each ELF section contains
780 several "virtual" sections. */
781
782 struct virtual_v2_dwo_sections
783 {
784 bfd_size_type abbrev_offset;
785 bfd_size_type abbrev_size;
786
787 bfd_size_type line_offset;
788 bfd_size_type line_size;
789
790 bfd_size_type loc_offset;
791 bfd_size_type loc_size;
792
793 bfd_size_type macinfo_offset;
794 bfd_size_type macinfo_size;
795
796 bfd_size_type macro_offset;
797 bfd_size_type macro_size;
798
799 bfd_size_type str_offsets_offset;
800 bfd_size_type str_offsets_size;
801
802 /* Each DWP hash table entry records one CU or one TU.
803 That is recorded here, and copied to dwo_unit.section. */
804 bfd_size_type info_or_types_offset;
805 bfd_size_type info_or_types_size;
806 };
807
808 /* Contents of DWP hash tables. */
809
810 struct dwp_hash_table
811 {
812 uint32_t version, nr_columns;
813 uint32_t nr_units, nr_slots;
814 const gdb_byte *hash_table, *unit_table;
815 union
816 {
817 struct
818 {
819 const gdb_byte *indices;
820 } v1;
821 struct
822 {
823 /* This is indexed by column number and gives the id of the section
824 in that column. */
825 #define MAX_NR_V2_DWO_SECTIONS \
826 (1 /* .debug_info or .debug_types */ \
827 + 1 /* .debug_abbrev */ \
828 + 1 /* .debug_line */ \
829 + 1 /* .debug_loc */ \
830 + 1 /* .debug_str_offsets */ \
831 + 1 /* .debug_macro or .debug_macinfo */)
832 int section_ids[MAX_NR_V2_DWO_SECTIONS];
833 const gdb_byte *offsets;
834 const gdb_byte *sizes;
835 } v2;
836 } section_pool;
837 };
838
839 /* Data for one DWP file. */
840
841 struct dwp_file
842 {
843 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
844 : name (name_),
845 dbfd (std::move (abfd))
846 {
847 }
848
849 /* Name of the file. */
850 const char *name;
851
852 /* File format version. */
853 int version = 0;
854
855 /* The bfd. */
856 gdb_bfd_ref_ptr dbfd;
857
858 /* Section info for this file. */
859 struct dwp_sections sections {};
860
861 /* Table of CUs in the file. */
862 const struct dwp_hash_table *cus = nullptr;
863
864 /* Table of TUs in the file. */
865 const struct dwp_hash_table *tus = nullptr;
866
867 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
868 htab_t loaded_cus {};
869 htab_t loaded_tus {};
870
871 /* Table to map ELF section numbers to their sections.
872 This is only needed for the DWP V1 file format. */
873 unsigned int num_sections = 0;
874 asection **elf_sections = nullptr;
875 };
876
877 struct abbrev_table;
878 typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
879
880 /* Struct used to pass misc. parameters to read_die_and_children, et
881 al. which are used for both .debug_info and .debug_types dies.
882 All parameters here are unchanging for the life of the call. This
883 struct exists to abstract away the constant parameters of die reading. */
884
885 struct die_reader_specs
886 {
887 /* The bfd of die_section. */
888 bfd* abfd;
889
890 /* The CU of the DIE we are parsing. */
891 struct dwarf2_cu *cu;
892
893 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
894 struct dwo_file *dwo_file;
895
896 /* The section the die comes from.
897 This is either .debug_info or .debug_types, or the .dwo variants. */
898 struct dwarf2_section_info *die_section;
899
900 /* die_section->buffer. */
901 const gdb_byte *buffer;
902
903 /* The end of the buffer. */
904 const gdb_byte *buffer_end;
905
906 /* The value of the DW_AT_comp_dir attribute. */
907 const char *comp_dir;
908
909 /* The abbreviation table to use when reading the DIEs. */
910 struct abbrev_table *abbrev_table;
911 };
912
913 /* A subclass of die_reader_specs that holds storage and has complex
914 constructor and destructor behavior. */
915
916 class cutu_reader : public die_reader_specs
917 {
918 public:
919
920 cutu_reader (struct dwarf2_per_cu_data *this_cu,
921 struct abbrev_table *abbrev_table,
922 int use_existing_cu, int keep,
923 bool skip_partial);
924
925 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
926 struct dwarf2_cu *parent_cu = nullptr,
927 struct dwo_file *dwo_file = nullptr);
928
929 ~cutu_reader ();
930
931 DISABLE_COPY_AND_ASSIGN (cutu_reader);
932
933 const gdb_byte *info_ptr = nullptr;
934 struct die_info *comp_unit_die = nullptr;
935 int has_children = 0;
936 bool dummy_p = false;
937
938 private:
939 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
940 int use_existing_cu, int keep);
941
942 struct dwarf2_per_cu_data *m_this_cu;
943 int m_keep = 0;
944 std::unique_ptr<dwarf2_cu> m_new_cu;
945
946 /* The ordinary abbreviation table. */
947 abbrev_table_up m_abbrev_table_holder;
948
949 /* The DWO abbreviation table. */
950 abbrev_table_up m_dwo_abbrev_table;
951 };
952
953 /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
954 later. */
955 typedef int dir_index;
956
957 /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
958 and later. */
959 typedef int file_name_index;
960
961 struct file_entry
962 {
963 file_entry () = default;
964
965 file_entry (const char *name_, dir_index d_index_,
966 unsigned int mod_time_, unsigned int length_)
967 : name (name_),
968 d_index (d_index_),
969 mod_time (mod_time_),
970 length (length_)
971 {}
972
973 /* Return the include directory at D_INDEX stored in LH. Returns
974 NULL if D_INDEX is out of bounds. */
975 const char *include_dir (const line_header *lh) const;
976
977 /* The file name. Note this is an observing pointer. The memory is
978 owned by debug_line_buffer. */
979 const char *name {};
980
981 /* The directory index (1-based). */
982 dir_index d_index {};
983
984 unsigned int mod_time {};
985
986 unsigned int length {};
987
988 /* True if referenced by the Line Number Program. */
989 bool included_p {};
990
991 /* The associated symbol table, if any. */
992 struct symtab *symtab {};
993 };
994
995 /* The line number information for a compilation unit (found in the
996 .debug_line section) begins with a "statement program header",
997 which contains the following information. */
998 struct line_header
999 {
1000 line_header ()
1001 : offset_in_dwz {}
1002 {}
1003
1004 /* Add an entry to the include directory table. */
1005 void add_include_dir (const char *include_dir);
1006
1007 /* Add an entry to the file name table. */
1008 void add_file_name (const char *name, dir_index d_index,
1009 unsigned int mod_time, unsigned int length);
1010
1011 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
1012 Returns NULL if INDEX is out of bounds. */
1013 const char *include_dir_at (dir_index index) const
1014 {
1015 int vec_index;
1016 if (version >= 5)
1017 vec_index = index;
1018 else
1019 vec_index = index - 1;
1020 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
1021 return NULL;
1022 return m_include_dirs[vec_index];
1023 }
1024
1025 bool is_valid_file_index (int file_index)
1026 {
1027 if (version >= 5)
1028 return 0 <= file_index && file_index < file_names_size ();
1029 return 1 <= file_index && file_index <= file_names_size ();
1030 }
1031
1032 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
1033 Returns NULL if INDEX is out of bounds. */
1034 file_entry *file_name_at (file_name_index index)
1035 {
1036 int vec_index;
1037 if (version >= 5)
1038 vec_index = index;
1039 else
1040 vec_index = index - 1;
1041 if (vec_index < 0 || vec_index >= m_file_names.size ())
1042 return NULL;
1043 return &m_file_names[vec_index];
1044 }
1045
1046 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1047 this method should only be used to iterate through all file entries in an
1048 index-agnostic manner. */
1049 std::vector<file_entry> &file_names ()
1050 { return m_file_names; }
1051
1052 /* Offset of line number information in .debug_line section. */
1053 sect_offset sect_off {};
1054
1055 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
1056 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1057
1058 unsigned int total_length {};
1059 unsigned short version {};
1060 unsigned int header_length {};
1061 unsigned char minimum_instruction_length {};
1062 unsigned char maximum_ops_per_instruction {};
1063 unsigned char default_is_stmt {};
1064 int line_base {};
1065 unsigned char line_range {};
1066 unsigned char opcode_base {};
1067
1068 /* standard_opcode_lengths[i] is the number of operands for the
1069 standard opcode whose value is i. This means that
1070 standard_opcode_lengths[0] is unused, and the last meaningful
1071 element is standard_opcode_lengths[opcode_base - 1]. */
1072 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
1073
1074 int file_names_size ()
1075 { return m_file_names.size(); }
1076
1077 /* The start and end of the statement program following this
1078 header. These point into dwarf2_per_objfile->line_buffer. */
1079 const gdb_byte *statement_program_start {}, *statement_program_end {};
1080
1081 private:
1082 /* The include_directories table. Note these are observing
1083 pointers. The memory is owned by debug_line_buffer. */
1084 std::vector<const char *> m_include_dirs;
1085
1086 /* The file_names table. This is private because the meaning of indexes
1087 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1088 before, and is 0 in DWARF 5 and later). So the client should use
1089 file_name_at method for access. */
1090 std::vector<file_entry> m_file_names;
1091 };
1092
1093 typedef std::unique_ptr<line_header> line_header_up;
1094
1095 const char *
1096 file_entry::include_dir (const line_header *lh) const
1097 {
1098 return lh->include_dir_at (d_index);
1099 }
1100
1101 /* When we construct a partial symbol table entry we only
1102 need this much information. */
1103 struct partial_die_info : public allocate_on_obstack
1104 {
1105 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1106
1107 /* Disable assign but still keep copy ctor, which is needed
1108 load_partial_dies. */
1109 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1110
1111 /* Adjust the partial die before generating a symbol for it. This
1112 function may set the is_external flag or change the DIE's
1113 name. */
1114 void fixup (struct dwarf2_cu *cu);
1115
1116 /* Read a minimal amount of information into the minimal die
1117 structure. */
1118 const gdb_byte *read (const struct die_reader_specs *reader,
1119 const struct abbrev_info &abbrev,
1120 const gdb_byte *info_ptr);
1121
1122 /* Offset of this DIE. */
1123 const sect_offset sect_off;
1124
1125 /* DWARF-2 tag for this DIE. */
1126 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1127
1128 /* Assorted flags describing the data found in this DIE. */
1129 const unsigned int has_children : 1;
1130
1131 unsigned int is_external : 1;
1132 unsigned int is_declaration : 1;
1133 unsigned int has_type : 1;
1134 unsigned int has_specification : 1;
1135 unsigned int has_pc_info : 1;
1136 unsigned int may_be_inlined : 1;
1137
1138 /* This DIE has been marked DW_AT_main_subprogram. */
1139 unsigned int main_subprogram : 1;
1140
1141 /* Flag set if the SCOPE field of this structure has been
1142 computed. */
1143 unsigned int scope_set : 1;
1144
1145 /* Flag set if the DIE has a byte_size attribute. */
1146 unsigned int has_byte_size : 1;
1147
1148 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1149 unsigned int has_const_value : 1;
1150
1151 /* Flag set if any of the DIE's children are template arguments. */
1152 unsigned int has_template_arguments : 1;
1153
1154 /* Flag set if fixup has been called on this die. */
1155 unsigned int fixup_called : 1;
1156
1157 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1158 unsigned int is_dwz : 1;
1159
1160 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1161 unsigned int spec_is_dwz : 1;
1162
1163 /* The name of this DIE. Normally the value of DW_AT_name, but
1164 sometimes a default name for unnamed DIEs. */
1165 const char *name = nullptr;
1166
1167 /* The linkage name, if present. */
1168 const char *linkage_name = nullptr;
1169
1170 /* The scope to prepend to our children. This is generally
1171 allocated on the comp_unit_obstack, so will disappear
1172 when this compilation unit leaves the cache. */
1173 const char *scope = nullptr;
1174
1175 /* Some data associated with the partial DIE. The tag determines
1176 which field is live. */
1177 union
1178 {
1179 /* The location description associated with this DIE, if any. */
1180 struct dwarf_block *locdesc;
1181 /* The offset of an import, for DW_TAG_imported_unit. */
1182 sect_offset sect_off;
1183 } d {};
1184
1185 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1186 CORE_ADDR lowpc = 0;
1187 CORE_ADDR highpc = 0;
1188
1189 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1190 DW_AT_sibling, if any. */
1191 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1192 could return DW_AT_sibling values to its caller load_partial_dies. */
1193 const gdb_byte *sibling = nullptr;
1194
1195 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1196 DW_AT_specification (or DW_AT_abstract_origin or
1197 DW_AT_extension). */
1198 sect_offset spec_offset {};
1199
1200 /* Pointers to this DIE's parent, first child, and next sibling,
1201 if any. */
1202 struct partial_die_info *die_parent = nullptr;
1203 struct partial_die_info *die_child = nullptr;
1204 struct partial_die_info *die_sibling = nullptr;
1205
1206 friend struct partial_die_info *
1207 dwarf2_cu::find_partial_die (sect_offset sect_off);
1208
1209 private:
1210 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1211 partial_die_info (sect_offset sect_off)
1212 : partial_die_info (sect_off, DW_TAG_padding, 0)
1213 {
1214 }
1215
1216 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1217 int has_children_)
1218 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1219 {
1220 is_external = 0;
1221 is_declaration = 0;
1222 has_type = 0;
1223 has_specification = 0;
1224 has_pc_info = 0;
1225 may_be_inlined = 0;
1226 main_subprogram = 0;
1227 scope_set = 0;
1228 has_byte_size = 0;
1229 has_const_value = 0;
1230 has_template_arguments = 0;
1231 fixup_called = 0;
1232 is_dwz = 0;
1233 spec_is_dwz = 0;
1234 }
1235 };
1236
1237 /* This data structure holds the information of an abbrev. */
1238 struct abbrev_info
1239 {
1240 unsigned int number; /* number identifying abbrev */
1241 enum dwarf_tag tag; /* dwarf tag */
1242 unsigned short has_children; /* boolean */
1243 unsigned short num_attrs; /* number of attributes */
1244 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1245 struct abbrev_info *next; /* next in chain */
1246 };
1247
1248 struct attr_abbrev
1249 {
1250 ENUM_BITFIELD(dwarf_attribute) name : 16;
1251 ENUM_BITFIELD(dwarf_form) form : 16;
1252
1253 /* It is valid only if FORM is DW_FORM_implicit_const. */
1254 LONGEST implicit_const;
1255 };
1256
1257 /* Size of abbrev_table.abbrev_hash_table. */
1258 #define ABBREV_HASH_SIZE 121
1259
1260 /* Top level data structure to contain an abbreviation table. */
1261
1262 struct abbrev_table
1263 {
1264 explicit abbrev_table (sect_offset off)
1265 : sect_off (off)
1266 {
1267 m_abbrevs =
1268 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
1269 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
1270 }
1271
1272 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1273
1274 /* Allocate space for a struct abbrev_info object in
1275 ABBREV_TABLE. */
1276 struct abbrev_info *alloc_abbrev ();
1277
1278 /* Add an abbreviation to the table. */
1279 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1280
1281 /* Look up an abbrev in the table.
1282 Returns NULL if the abbrev is not found. */
1283
1284 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1285
1286
1287 /* Where the abbrev table came from.
1288 This is used as a sanity check when the table is used. */
1289 const sect_offset sect_off;
1290
1291 /* Storage for the abbrev table. */
1292 auto_obstack abbrev_obstack;
1293
1294 private:
1295
1296 /* Hash table of abbrevs.
1297 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1298 It could be statically allocated, but the previous code didn't so we
1299 don't either. */
1300 struct abbrev_info **m_abbrevs;
1301 };
1302
1303 /* Attributes have a name and a value. */
1304 struct attribute
1305 {
1306 ENUM_BITFIELD(dwarf_attribute) name : 16;
1307 ENUM_BITFIELD(dwarf_form) form : 15;
1308
1309 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1310 field should be in u.str (existing only for DW_STRING) but it is kept
1311 here for better struct attribute alignment. */
1312 unsigned int string_is_canonical : 1;
1313
1314 union
1315 {
1316 const char *str;
1317 struct dwarf_block *blk;
1318 ULONGEST unsnd;
1319 LONGEST snd;
1320 CORE_ADDR addr;
1321 ULONGEST signature;
1322 }
1323 u;
1324 };
1325
1326 /* This data structure holds a complete die structure. */
1327 struct die_info
1328 {
1329 /* DWARF-2 tag for this DIE. */
1330 ENUM_BITFIELD(dwarf_tag) tag : 16;
1331
1332 /* Number of attributes */
1333 unsigned char num_attrs;
1334
1335 /* True if we're presently building the full type name for the
1336 type derived from this DIE. */
1337 unsigned char building_fullname : 1;
1338
1339 /* True if this die is in process. PR 16581. */
1340 unsigned char in_process : 1;
1341
1342 /* Abbrev number */
1343 unsigned int abbrev;
1344
1345 /* Offset in .debug_info or .debug_types section. */
1346 sect_offset sect_off;
1347
1348 /* The dies in a compilation unit form an n-ary tree. PARENT
1349 points to this die's parent; CHILD points to the first child of
1350 this node; and all the children of a given node are chained
1351 together via their SIBLING fields. */
1352 struct die_info *child; /* Its first child, if any. */
1353 struct die_info *sibling; /* Its next sibling, if any. */
1354 struct die_info *parent; /* Its parent, if any. */
1355
1356 /* An array of attributes, with NUM_ATTRS elements. There may be
1357 zero, but it's not common and zero-sized arrays are not
1358 sufficiently portable C. */
1359 struct attribute attrs[1];
1360 };
1361
1362 /* Get at parts of an attribute structure. */
1363
1364 #define DW_STRING(attr) ((attr)->u.str)
1365 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1366 #define DW_UNSND(attr) ((attr)->u.unsnd)
1367 #define DW_BLOCK(attr) ((attr)->u.blk)
1368 #define DW_SND(attr) ((attr)->u.snd)
1369 #define DW_ADDR(attr) ((attr)->u.addr)
1370 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1371
1372 /* Blocks are a bunch of untyped bytes. */
1373 struct dwarf_block
1374 {
1375 size_t size;
1376
1377 /* Valid only if SIZE is not zero. */
1378 const gdb_byte *data;
1379 };
1380
1381 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1382 but this would require a corresponding change in unpack_field_as_long
1383 and friends. */
1384 static int bits_per_byte = 8;
1385
1386 /* When reading a variant or variant part, we track a bit more
1387 information about the field, and store it in an object of this
1388 type. */
1389
1390 struct variant_field
1391 {
1392 /* If we see a DW_TAG_variant, then this will be the discriminant
1393 value. */
1394 ULONGEST discriminant_value;
1395 /* If we see a DW_TAG_variant, then this will be set if this is the
1396 default branch. */
1397 bool default_branch;
1398 /* While reading a DW_TAG_variant_part, this will be set if this
1399 field is the discriminant. */
1400 bool is_discriminant;
1401 };
1402
1403 struct nextfield
1404 {
1405 int accessibility = 0;
1406 int virtuality = 0;
1407 /* Extra information to describe a variant or variant part. */
1408 struct variant_field variant {};
1409 struct field field {};
1410 };
1411
1412 struct fnfieldlist
1413 {
1414 const char *name = nullptr;
1415 std::vector<struct fn_field> fnfields;
1416 };
1417
1418 /* The routines that read and process dies for a C struct or C++ class
1419 pass lists of data member fields and lists of member function fields
1420 in an instance of a field_info structure, as defined below. */
1421 struct field_info
1422 {
1423 /* List of data member and baseclasses fields. */
1424 std::vector<struct nextfield> fields;
1425 std::vector<struct nextfield> baseclasses;
1426
1427 /* Number of fields (including baseclasses). */
1428 int nfields = 0;
1429
1430 /* Set if the accessibility of one of the fields is not public. */
1431 int non_public_fields = 0;
1432
1433 /* Member function fieldlist array, contains name of possibly overloaded
1434 member function, number of overloaded member functions and a pointer
1435 to the head of the member function field chain. */
1436 std::vector<struct fnfieldlist> fnfieldlists;
1437
1438 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1439 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1440 std::vector<struct decl_field> typedef_field_list;
1441
1442 /* Nested types defined by this class and the number of elements in this
1443 list. */
1444 std::vector<struct decl_field> nested_types_list;
1445 };
1446
1447 /* One item on the queue of compilation units to read in full symbols
1448 for. */
1449 struct dwarf2_queue_item
1450 {
1451 struct dwarf2_per_cu_data *per_cu;
1452 enum language pretend_language;
1453 struct dwarf2_queue_item *next;
1454 };
1455
1456 /* The current queue. */
1457 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1458
1459 /* Loaded secondary compilation units are kept in memory until they
1460 have not been referenced for the processing of this many
1461 compilation units. Set this to zero to disable caching. Cache
1462 sizes of up to at least twenty will improve startup time for
1463 typical inter-CU-reference binaries, at an obvious memory cost. */
1464 static int dwarf_max_cache_age = 5;
1465 static void
1466 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1467 struct cmd_list_element *c, const char *value)
1468 {
1469 fprintf_filtered (file, _("The upper bound on the age of cached "
1470 "DWARF compilation units is %s.\n"),
1471 value);
1472 }
1473 \f
1474 /* local function prototypes */
1475
1476 static const char *get_section_name (const struct dwarf2_section_info *);
1477
1478 static const char *get_section_file_name (const struct dwarf2_section_info *);
1479
1480 static void dwarf2_find_base_address (struct die_info *die,
1481 struct dwarf2_cu *cu);
1482
1483 static dwarf2_psymtab *create_partial_symtab
1484 (struct dwarf2_per_cu_data *per_cu, const char *name);
1485
1486 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1487 const gdb_byte *info_ptr,
1488 struct die_info *type_unit_die,
1489 int has_children);
1490
1491 static void dwarf2_build_psymtabs_hard
1492 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1493
1494 static void scan_partial_symbols (struct partial_die_info *,
1495 CORE_ADDR *, CORE_ADDR *,
1496 int, struct dwarf2_cu *);
1497
1498 static void add_partial_symbol (struct partial_die_info *,
1499 struct dwarf2_cu *);
1500
1501 static void add_partial_namespace (struct partial_die_info *pdi,
1502 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1503 int set_addrmap, struct dwarf2_cu *cu);
1504
1505 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1506 CORE_ADDR *highpc, int set_addrmap,
1507 struct dwarf2_cu *cu);
1508
1509 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1510 struct dwarf2_cu *cu);
1511
1512 static void add_partial_subprogram (struct partial_die_info *pdi,
1513 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1514 int need_pc, struct dwarf2_cu *cu);
1515
1516 static void psymtab_to_symtab_1 (dwarf2_psymtab *);
1517
1518 static abbrev_table_up abbrev_table_read_table
1519 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1520 sect_offset);
1521
1522 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1523
1524 static struct partial_die_info *load_partial_dies
1525 (const struct die_reader_specs *, const gdb_byte *, int);
1526
1527 /* A pair of partial_die_info and compilation unit. */
1528 struct cu_partial_die_info
1529 {
1530 /* The compilation unit of the partial_die_info. */
1531 struct dwarf2_cu *cu;
1532 /* A partial_die_info. */
1533 struct partial_die_info *pdi;
1534
1535 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1536 : cu (cu),
1537 pdi (pdi)
1538 { /* Nothing. */ }
1539
1540 private:
1541 cu_partial_die_info () = delete;
1542 };
1543
1544 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1545 struct dwarf2_cu *);
1546
1547 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1548 struct attribute *, struct attr_abbrev *,
1549 const gdb_byte *, bool *need_reprocess);
1550
1551 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1552 struct attribute *attr);
1553
1554 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1555
1556 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1557
1558 static int read_1_signed_byte (bfd *, const gdb_byte *);
1559
1560 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1561
1562 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1563 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1564
1565 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1566
1567 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1568
1569 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1570 unsigned int *);
1571
1572 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1573
1574 static LONGEST read_checked_initial_length_and_offset
1575 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1576 unsigned int *, unsigned int *);
1577
1578 static LONGEST read_offset (bfd *, const gdb_byte *,
1579 const struct comp_unit_head *,
1580 unsigned int *);
1581
1582 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1583
1584 static sect_offset read_abbrev_offset
1585 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1586 struct dwarf2_section_info *, sect_offset);
1587
1588 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1589
1590 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1591
1592 static const char *read_indirect_string
1593 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1594 const struct comp_unit_head *, unsigned int *);
1595
1596 static const char *read_indirect_line_string
1597 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1598 const struct comp_unit_head *, unsigned int *);
1599
1600 static const char *read_indirect_string_at_offset
1601 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1602 LONGEST str_offset);
1603
1604 static const char *read_indirect_string_from_dwz
1605 (struct objfile *objfile, struct dwz_file *, LONGEST);
1606
1607 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1608
1609 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1610 const gdb_byte *,
1611 unsigned int *);
1612
1613 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1614 ULONGEST str_index);
1615
1616 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1617 ULONGEST str_index);
1618
1619 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1620
1621 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1622 struct dwarf2_cu *);
1623
1624 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1625 unsigned int);
1626
1627 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1628 struct dwarf2_cu *cu);
1629
1630 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1631
1632 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1633 struct dwarf2_cu *cu);
1634
1635 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1636
1637 static struct die_info *die_specification (struct die_info *die,
1638 struct dwarf2_cu **);
1639
1640 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1641 struct dwarf2_cu *cu);
1642
1643 static void dwarf_decode_lines (struct line_header *, const char *,
1644 struct dwarf2_cu *, dwarf2_psymtab *,
1645 CORE_ADDR, int decode_mapping);
1646
1647 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1648 const char *);
1649
1650 static struct symbol *new_symbol (struct die_info *, struct type *,
1651 struct dwarf2_cu *, struct symbol * = NULL);
1652
1653 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1654 struct dwarf2_cu *);
1655
1656 static void dwarf2_const_value_attr (const struct attribute *attr,
1657 struct type *type,
1658 const char *name,
1659 struct obstack *obstack,
1660 struct dwarf2_cu *cu, LONGEST *value,
1661 const gdb_byte **bytes,
1662 struct dwarf2_locexpr_baton **baton);
1663
1664 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1665
1666 static int need_gnat_info (struct dwarf2_cu *);
1667
1668 static struct type *die_descriptive_type (struct die_info *,
1669 struct dwarf2_cu *);
1670
1671 static void set_descriptive_type (struct type *, struct die_info *,
1672 struct dwarf2_cu *);
1673
1674 static struct type *die_containing_type (struct die_info *,
1675 struct dwarf2_cu *);
1676
1677 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1678 struct dwarf2_cu *);
1679
1680 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1681
1682 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1683
1684 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1685
1686 static char *typename_concat (struct obstack *obs, const char *prefix,
1687 const char *suffix, int physname,
1688 struct dwarf2_cu *cu);
1689
1690 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1691
1692 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1693
1694 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1695
1696 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1697
1698 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1699
1700 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1701
1702 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1703 struct dwarf2_cu *, dwarf2_psymtab *);
1704
1705 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1706 values. Keep the items ordered with increasing constraints compliance. */
1707 enum pc_bounds_kind
1708 {
1709 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1710 PC_BOUNDS_NOT_PRESENT,
1711
1712 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1713 were present but they do not form a valid range of PC addresses. */
1714 PC_BOUNDS_INVALID,
1715
1716 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1717 PC_BOUNDS_RANGES,
1718
1719 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1720 PC_BOUNDS_HIGH_LOW,
1721 };
1722
1723 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1724 CORE_ADDR *, CORE_ADDR *,
1725 struct dwarf2_cu *,
1726 dwarf2_psymtab *);
1727
1728 static void get_scope_pc_bounds (struct die_info *,
1729 CORE_ADDR *, CORE_ADDR *,
1730 struct dwarf2_cu *);
1731
1732 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1733 CORE_ADDR, struct dwarf2_cu *);
1734
1735 static void dwarf2_add_field (struct field_info *, struct die_info *,
1736 struct dwarf2_cu *);
1737
1738 static void dwarf2_attach_fields_to_type (struct field_info *,
1739 struct type *, struct dwarf2_cu *);
1740
1741 static void dwarf2_add_member_fn (struct field_info *,
1742 struct die_info *, struct type *,
1743 struct dwarf2_cu *);
1744
1745 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1746 struct type *,
1747 struct dwarf2_cu *);
1748
1749 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1750
1751 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1752
1753 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1754
1755 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1756
1757 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1758
1759 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1760
1761 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1762
1763 static struct type *read_module_type (struct die_info *die,
1764 struct dwarf2_cu *cu);
1765
1766 static const char *namespace_name (struct die_info *die,
1767 int *is_anonymous, struct dwarf2_cu *);
1768
1769 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1770
1771 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1772
1773 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1774 struct dwarf2_cu *);
1775
1776 static struct die_info *read_die_and_siblings_1
1777 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1778 struct die_info *);
1779
1780 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1781 const gdb_byte *info_ptr,
1782 const gdb_byte **new_info_ptr,
1783 struct die_info *parent);
1784
1785 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1786 struct die_info **, const gdb_byte *,
1787 int *, int);
1788
1789 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1790 struct die_info **, const gdb_byte *,
1791 int *);
1792
1793 static void process_die (struct die_info *, struct dwarf2_cu *);
1794
1795 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1796 struct obstack *);
1797
1798 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1799
1800 static const char *dwarf2_full_name (const char *name,
1801 struct die_info *die,
1802 struct dwarf2_cu *cu);
1803
1804 static const char *dwarf2_physname (const char *name, struct die_info *die,
1805 struct dwarf2_cu *cu);
1806
1807 static struct die_info *dwarf2_extension (struct die_info *die,
1808 struct dwarf2_cu **);
1809
1810 static const char *dwarf_tag_name (unsigned int);
1811
1812 static const char *dwarf_attr_name (unsigned int);
1813
1814 static const char *dwarf_unit_type_name (int unit_type);
1815
1816 static const char *dwarf_form_name (unsigned int);
1817
1818 static const char *dwarf_bool_name (unsigned int);
1819
1820 static const char *dwarf_type_encoding_name (unsigned int);
1821
1822 static struct die_info *sibling_die (struct die_info *);
1823
1824 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1825
1826 static void dump_die_for_error (struct die_info *);
1827
1828 static void dump_die_1 (struct ui_file *, int level, int max_level,
1829 struct die_info *);
1830
1831 /*static*/ void dump_die (struct die_info *, int max_level);
1832
1833 static void store_in_ref_table (struct die_info *,
1834 struct dwarf2_cu *);
1835
1836 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1837
1838 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1839
1840 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1841 const struct attribute *,
1842 struct dwarf2_cu **);
1843
1844 static struct die_info *follow_die_ref (struct die_info *,
1845 const struct attribute *,
1846 struct dwarf2_cu **);
1847
1848 static struct die_info *follow_die_sig (struct die_info *,
1849 const struct attribute *,
1850 struct dwarf2_cu **);
1851
1852 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1853 struct dwarf2_cu *);
1854
1855 static struct type *get_DW_AT_signature_type (struct die_info *,
1856 const struct attribute *,
1857 struct dwarf2_cu *);
1858
1859 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1860
1861 static void read_signatured_type (struct signatured_type *);
1862
1863 static int attr_to_dynamic_prop (const struct attribute *attr,
1864 struct die_info *die, struct dwarf2_cu *cu,
1865 struct dynamic_prop *prop, struct type *type);
1866
1867 /* memory allocation interface */
1868
1869 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1870
1871 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1872
1873 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1874
1875 static int attr_form_is_block (const struct attribute *);
1876
1877 static int attr_form_is_section_offset (const struct attribute *);
1878
1879 static int attr_form_is_constant (const struct attribute *);
1880
1881 static int attr_form_is_ref (const struct attribute *);
1882
1883 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1884 struct dwarf2_loclist_baton *baton,
1885 const struct attribute *attr);
1886
1887 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1888 struct symbol *sym,
1889 struct dwarf2_cu *cu,
1890 int is_block);
1891
1892 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1893 const gdb_byte *info_ptr,
1894 struct abbrev_info *abbrev);
1895
1896 static hashval_t partial_die_hash (const void *item);
1897
1898 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1899
1900 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1901 (sect_offset sect_off, unsigned int offset_in_dwz,
1902 struct dwarf2_per_objfile *dwarf2_per_objfile);
1903
1904 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1905 struct die_info *comp_unit_die,
1906 enum language pretend_language);
1907
1908 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1909
1910 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1911
1912 static struct type *set_die_type (struct die_info *, struct type *,
1913 struct dwarf2_cu *);
1914
1915 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1916
1917 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1918
1919 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1920 enum language);
1921
1922 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1923 enum language);
1924
1925 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1926 enum language);
1927
1928 static void dwarf2_add_dependence (struct dwarf2_cu *,
1929 struct dwarf2_per_cu_data *);
1930
1931 static void dwarf2_mark (struct dwarf2_cu *);
1932
1933 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1934
1935 static struct type *get_die_type_at_offset (sect_offset,
1936 struct dwarf2_per_cu_data *);
1937
1938 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1939
1940 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1941 enum language pretend_language);
1942
1943 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1944
1945 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1946 static struct type *dwarf2_per_cu_addr_sized_int_type
1947 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1948 static struct type *dwarf2_per_cu_int_type
1949 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1950 bool unsigned_p);
1951
1952 /* Class, the destructor of which frees all allocated queue entries. This
1953 will only have work to do if an error was thrown while processing the
1954 dwarf. If no error was thrown then the queue entries should have all
1955 been processed, and freed, as we went along. */
1956
1957 class dwarf2_queue_guard
1958 {
1959 public:
1960 dwarf2_queue_guard () = default;
1961
1962 /* Free any entries remaining on the queue. There should only be
1963 entries left if we hit an error while processing the dwarf. */
1964 ~dwarf2_queue_guard ()
1965 {
1966 struct dwarf2_queue_item *item, *last;
1967
1968 item = dwarf2_queue;
1969 while (item)
1970 {
1971 /* Anything still marked queued is likely to be in an
1972 inconsistent state, so discard it. */
1973 if (item->per_cu->queued)
1974 {
1975 if (item->per_cu->cu != NULL)
1976 free_one_cached_comp_unit (item->per_cu);
1977 item->per_cu->queued = 0;
1978 }
1979
1980 last = item;
1981 item = item->next;
1982 xfree (last);
1983 }
1984
1985 dwarf2_queue = dwarf2_queue_tail = NULL;
1986 }
1987 };
1988
1989 /* The return type of find_file_and_directory. Note, the enclosed
1990 string pointers are only valid while this object is valid. */
1991
1992 struct file_and_directory
1993 {
1994 /* The filename. This is never NULL. */
1995 const char *name;
1996
1997 /* The compilation directory. NULL if not known. If we needed to
1998 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1999 points directly to the DW_AT_comp_dir string attribute owned by
2000 the obstack that owns the DIE. */
2001 const char *comp_dir;
2002
2003 /* If we needed to build a new string for comp_dir, this is what
2004 owns the storage. */
2005 std::string comp_dir_storage;
2006 };
2007
2008 static file_and_directory find_file_and_directory (struct die_info *die,
2009 struct dwarf2_cu *cu);
2010
2011 static char *file_full_name (int file, struct line_header *lh,
2012 const char *comp_dir);
2013
2014 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2015 enum class rcuh_kind { COMPILE, TYPE };
2016
2017 static const gdb_byte *read_and_check_comp_unit_head
2018 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2019 struct comp_unit_head *header,
2020 struct dwarf2_section_info *section,
2021 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2022 rcuh_kind section_kind);
2023
2024 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2025
2026 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2027
2028 static struct dwo_unit *lookup_dwo_unit_in_dwp
2029 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2030 struct dwp_file *dwp_file, const char *comp_dir,
2031 ULONGEST signature, int is_debug_types);
2032
2033 static struct dwp_file *get_dwp_file
2034 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2035
2036 static struct dwo_unit *lookup_dwo_comp_unit
2037 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2038
2039 static struct dwo_unit *lookup_dwo_type_unit
2040 (struct signatured_type *, const char *, const char *);
2041
2042 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2043
2044 /* A unique pointer to a dwo_file. */
2045
2046 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2047
2048 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2049
2050 static void check_producer (struct dwarf2_cu *cu);
2051
2052 static void free_line_header_voidp (void *arg);
2053 \f
2054 /* Various complaints about symbol reading that don't abort the process. */
2055
2056 static void
2057 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2058 {
2059 complaint (_("statement list doesn't fit in .debug_line section"));
2060 }
2061
2062 static void
2063 dwarf2_debug_line_missing_file_complaint (void)
2064 {
2065 complaint (_(".debug_line section has line data without a file"));
2066 }
2067
2068 static void
2069 dwarf2_debug_line_missing_end_sequence_complaint (void)
2070 {
2071 complaint (_(".debug_line section has line "
2072 "program sequence without an end"));
2073 }
2074
2075 static void
2076 dwarf2_complex_location_expr_complaint (void)
2077 {
2078 complaint (_("location expression too complex"));
2079 }
2080
2081 static void
2082 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2083 int arg3)
2084 {
2085 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2086 arg1, arg2, arg3);
2087 }
2088
2089 static void
2090 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2091 {
2092 complaint (_("debug info runs off end of %s section"
2093 " [in module %s]"),
2094 get_section_name (section),
2095 get_section_file_name (section));
2096 }
2097
2098 static void
2099 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2100 {
2101 complaint (_("macro debug info contains a "
2102 "malformed macro definition:\n`%s'"),
2103 arg1);
2104 }
2105
2106 static void
2107 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2108 {
2109 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2110 arg1, arg2);
2111 }
2112
2113 /* Hash function for line_header_hash. */
2114
2115 static hashval_t
2116 line_header_hash (const struct line_header *ofs)
2117 {
2118 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2119 }
2120
2121 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2122
2123 static hashval_t
2124 line_header_hash_voidp (const void *item)
2125 {
2126 const struct line_header *ofs = (const struct line_header *) item;
2127
2128 return line_header_hash (ofs);
2129 }
2130
2131 /* Equality function for line_header_hash. */
2132
2133 static int
2134 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2135 {
2136 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2137 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2138
2139 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2140 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2141 }
2142
2143 \f
2144
2145 /* Read the given attribute value as an address, taking the attribute's
2146 form into account. */
2147
2148 static CORE_ADDR
2149 attr_value_as_address (struct attribute *attr)
2150 {
2151 CORE_ADDR addr;
2152
2153 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2154 && attr->form != DW_FORM_GNU_addr_index)
2155 {
2156 /* Aside from a few clearly defined exceptions, attributes that
2157 contain an address must always be in DW_FORM_addr form.
2158 Unfortunately, some compilers happen to be violating this
2159 requirement by encoding addresses using other forms, such
2160 as DW_FORM_data4 for example. For those broken compilers,
2161 we try to do our best, without any guarantee of success,
2162 to interpret the address correctly. It would also be nice
2163 to generate a complaint, but that would require us to maintain
2164 a list of legitimate cases where a non-address form is allowed,
2165 as well as update callers to pass in at least the CU's DWARF
2166 version. This is more overhead than what we're willing to
2167 expand for a pretty rare case. */
2168 addr = DW_UNSND (attr);
2169 }
2170 else
2171 addr = DW_ADDR (attr);
2172
2173 return addr;
2174 }
2175
2176 /* See declaration. */
2177
2178 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2179 const dwarf2_debug_sections *names,
2180 bool can_copy_)
2181 : objfile (objfile_),
2182 can_copy (can_copy_)
2183 {
2184 if (names == NULL)
2185 names = &dwarf2_elf_names;
2186
2187 bfd *obfd = objfile->obfd;
2188
2189 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2190 locate_sections (obfd, sec, *names);
2191 }
2192
2193 dwarf2_per_objfile::~dwarf2_per_objfile ()
2194 {
2195 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2196 free_cached_comp_units ();
2197
2198 if (quick_file_names_table)
2199 htab_delete (quick_file_names_table);
2200
2201 if (line_header_hash)
2202 htab_delete (line_header_hash);
2203
2204 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2205 per_cu->imported_symtabs_free ();
2206
2207 for (signatured_type *sig_type : all_type_units)
2208 sig_type->per_cu.imported_symtabs_free ();
2209
2210 /* Everything else should be on the objfile obstack. */
2211 }
2212
2213 /* See declaration. */
2214
2215 void
2216 dwarf2_per_objfile::free_cached_comp_units ()
2217 {
2218 dwarf2_per_cu_data *per_cu = read_in_chain;
2219 dwarf2_per_cu_data **last_chain = &read_in_chain;
2220 while (per_cu != NULL)
2221 {
2222 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2223
2224 delete per_cu->cu;
2225 *last_chain = next_cu;
2226 per_cu = next_cu;
2227 }
2228 }
2229
2230 /* A helper class that calls free_cached_comp_units on
2231 destruction. */
2232
2233 class free_cached_comp_units
2234 {
2235 public:
2236
2237 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2238 : m_per_objfile (per_objfile)
2239 {
2240 }
2241
2242 ~free_cached_comp_units ()
2243 {
2244 m_per_objfile->free_cached_comp_units ();
2245 }
2246
2247 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2248
2249 private:
2250
2251 dwarf2_per_objfile *m_per_objfile;
2252 };
2253
2254 /* Try to locate the sections we need for DWARF 2 debugging
2255 information and return true if we have enough to do something.
2256 NAMES points to the dwarf2 section names, or is NULL if the standard
2257 ELF names are used. CAN_COPY is true for formats where symbol
2258 interposition is possible and so symbol values must follow copy
2259 relocation rules. */
2260
2261 int
2262 dwarf2_has_info (struct objfile *objfile,
2263 const struct dwarf2_debug_sections *names,
2264 bool can_copy)
2265 {
2266 if (objfile->flags & OBJF_READNEVER)
2267 return 0;
2268
2269 struct dwarf2_per_objfile *dwarf2_per_objfile
2270 = get_dwarf2_per_objfile (objfile);
2271
2272 if (dwarf2_per_objfile == NULL)
2273 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2274 names,
2275 can_copy);
2276
2277 return (!dwarf2_per_objfile->info.is_virtual
2278 && dwarf2_per_objfile->info.s.section != NULL
2279 && !dwarf2_per_objfile->abbrev.is_virtual
2280 && dwarf2_per_objfile->abbrev.s.section != NULL);
2281 }
2282
2283 /* Return the containing section of virtual section SECTION. */
2284
2285 static struct dwarf2_section_info *
2286 get_containing_section (const struct dwarf2_section_info *section)
2287 {
2288 gdb_assert (section->is_virtual);
2289 return section->s.containing_section;
2290 }
2291
2292 /* Return the bfd owner of SECTION. */
2293
2294 static struct bfd *
2295 get_section_bfd_owner (const struct dwarf2_section_info *section)
2296 {
2297 if (section->is_virtual)
2298 {
2299 section = get_containing_section (section);
2300 gdb_assert (!section->is_virtual);
2301 }
2302 return section->s.section->owner;
2303 }
2304
2305 /* Return the bfd section of SECTION.
2306 Returns NULL if the section is not present. */
2307
2308 static asection *
2309 get_section_bfd_section (const struct dwarf2_section_info *section)
2310 {
2311 if (section->is_virtual)
2312 {
2313 section = get_containing_section (section);
2314 gdb_assert (!section->is_virtual);
2315 }
2316 return section->s.section;
2317 }
2318
2319 /* Return the name of SECTION. */
2320
2321 static const char *
2322 get_section_name (const struct dwarf2_section_info *section)
2323 {
2324 asection *sectp = get_section_bfd_section (section);
2325
2326 gdb_assert (sectp != NULL);
2327 return bfd_section_name (sectp);
2328 }
2329
2330 /* Return the name of the file SECTION is in. */
2331
2332 static const char *
2333 get_section_file_name (const struct dwarf2_section_info *section)
2334 {
2335 bfd *abfd = get_section_bfd_owner (section);
2336
2337 return bfd_get_filename (abfd);
2338 }
2339
2340 /* Return the id of SECTION.
2341 Returns 0 if SECTION doesn't exist. */
2342
2343 static int
2344 get_section_id (const struct dwarf2_section_info *section)
2345 {
2346 asection *sectp = get_section_bfd_section (section);
2347
2348 if (sectp == NULL)
2349 return 0;
2350 return sectp->id;
2351 }
2352
2353 /* Return the flags of SECTION.
2354 SECTION (or containing section if this is a virtual section) must exist. */
2355
2356 static int
2357 get_section_flags (const struct dwarf2_section_info *section)
2358 {
2359 asection *sectp = get_section_bfd_section (section);
2360
2361 gdb_assert (sectp != NULL);
2362 return bfd_section_flags (sectp);
2363 }
2364
2365 /* When loading sections, we look either for uncompressed section or for
2366 compressed section names. */
2367
2368 static int
2369 section_is_p (const char *section_name,
2370 const struct dwarf2_section_names *names)
2371 {
2372 if (names->normal != NULL
2373 && strcmp (section_name, names->normal) == 0)
2374 return 1;
2375 if (names->compressed != NULL
2376 && strcmp (section_name, names->compressed) == 0)
2377 return 1;
2378 return 0;
2379 }
2380
2381 /* See declaration. */
2382
2383 void
2384 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2385 const dwarf2_debug_sections &names)
2386 {
2387 flagword aflag = bfd_section_flags (sectp);
2388
2389 if ((aflag & SEC_HAS_CONTENTS) == 0)
2390 {
2391 }
2392 else if (elf_section_data (sectp)->this_hdr.sh_size
2393 > bfd_get_file_size (abfd))
2394 {
2395 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2396 warning (_("Discarding section %s which has a section size (%s"
2397 ") larger than the file size [in module %s]"),
2398 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2399 bfd_get_filename (abfd));
2400 }
2401 else if (section_is_p (sectp->name, &names.info))
2402 {
2403 this->info.s.section = sectp;
2404 this->info.size = bfd_section_size (sectp);
2405 }
2406 else if (section_is_p (sectp->name, &names.abbrev))
2407 {
2408 this->abbrev.s.section = sectp;
2409 this->abbrev.size = bfd_section_size (sectp);
2410 }
2411 else if (section_is_p (sectp->name, &names.line))
2412 {
2413 this->line.s.section = sectp;
2414 this->line.size = bfd_section_size (sectp);
2415 }
2416 else if (section_is_p (sectp->name, &names.loc))
2417 {
2418 this->loc.s.section = sectp;
2419 this->loc.size = bfd_section_size (sectp);
2420 }
2421 else if (section_is_p (sectp->name, &names.loclists))
2422 {
2423 this->loclists.s.section = sectp;
2424 this->loclists.size = bfd_section_size (sectp);
2425 }
2426 else if (section_is_p (sectp->name, &names.macinfo))
2427 {
2428 this->macinfo.s.section = sectp;
2429 this->macinfo.size = bfd_section_size (sectp);
2430 }
2431 else if (section_is_p (sectp->name, &names.macro))
2432 {
2433 this->macro.s.section = sectp;
2434 this->macro.size = bfd_section_size (sectp);
2435 }
2436 else if (section_is_p (sectp->name, &names.str))
2437 {
2438 this->str.s.section = sectp;
2439 this->str.size = bfd_section_size (sectp);
2440 }
2441 else if (section_is_p (sectp->name, &names.str_offsets))
2442 {
2443 this->str_offsets.s.section = sectp;
2444 this->str_offsets.size = bfd_section_size (sectp);
2445 }
2446 else if (section_is_p (sectp->name, &names.line_str))
2447 {
2448 this->line_str.s.section = sectp;
2449 this->line_str.size = bfd_section_size (sectp);
2450 }
2451 else if (section_is_p (sectp->name, &names.addr))
2452 {
2453 this->addr.s.section = sectp;
2454 this->addr.size = bfd_section_size (sectp);
2455 }
2456 else if (section_is_p (sectp->name, &names.frame))
2457 {
2458 this->frame.s.section = sectp;
2459 this->frame.size = bfd_section_size (sectp);
2460 }
2461 else if (section_is_p (sectp->name, &names.eh_frame))
2462 {
2463 this->eh_frame.s.section = sectp;
2464 this->eh_frame.size = bfd_section_size (sectp);
2465 }
2466 else if (section_is_p (sectp->name, &names.ranges))
2467 {
2468 this->ranges.s.section = sectp;
2469 this->ranges.size = bfd_section_size (sectp);
2470 }
2471 else if (section_is_p (sectp->name, &names.rnglists))
2472 {
2473 this->rnglists.s.section = sectp;
2474 this->rnglists.size = bfd_section_size (sectp);
2475 }
2476 else if (section_is_p (sectp->name, &names.types))
2477 {
2478 struct dwarf2_section_info type_section;
2479
2480 memset (&type_section, 0, sizeof (type_section));
2481 type_section.s.section = sectp;
2482 type_section.size = bfd_section_size (sectp);
2483
2484 this->types.push_back (type_section);
2485 }
2486 else if (section_is_p (sectp->name, &names.gdb_index))
2487 {
2488 this->gdb_index.s.section = sectp;
2489 this->gdb_index.size = bfd_section_size (sectp);
2490 }
2491 else if (section_is_p (sectp->name, &names.debug_names))
2492 {
2493 this->debug_names.s.section = sectp;
2494 this->debug_names.size = bfd_section_size (sectp);
2495 }
2496 else if (section_is_p (sectp->name, &names.debug_aranges))
2497 {
2498 this->debug_aranges.s.section = sectp;
2499 this->debug_aranges.size = bfd_section_size (sectp);
2500 }
2501
2502 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2503 && bfd_section_vma (sectp) == 0)
2504 this->has_section_at_zero = true;
2505 }
2506
2507 /* A helper function that decides whether a section is empty,
2508 or not present. */
2509
2510 static int
2511 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2512 {
2513 if (section->is_virtual)
2514 return section->size == 0;
2515 return section->s.section == NULL || section->size == 0;
2516 }
2517
2518 /* See dwarf2read.h. */
2519
2520 void
2521 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2522 {
2523 asection *sectp;
2524 bfd *abfd;
2525 gdb_byte *buf, *retbuf;
2526
2527 if (info->readin)
2528 return;
2529 info->buffer = NULL;
2530 info->readin = true;
2531
2532 if (dwarf2_section_empty_p (info))
2533 return;
2534
2535 sectp = get_section_bfd_section (info);
2536
2537 /* If this is a virtual section we need to read in the real one first. */
2538 if (info->is_virtual)
2539 {
2540 struct dwarf2_section_info *containing_section =
2541 get_containing_section (info);
2542
2543 gdb_assert (sectp != NULL);
2544 if ((sectp->flags & SEC_RELOC) != 0)
2545 {
2546 error (_("Dwarf Error: DWP format V2 with relocations is not"
2547 " supported in section %s [in module %s]"),
2548 get_section_name (info), get_section_file_name (info));
2549 }
2550 dwarf2_read_section (objfile, containing_section);
2551 /* Other code should have already caught virtual sections that don't
2552 fit. */
2553 gdb_assert (info->virtual_offset + info->size
2554 <= containing_section->size);
2555 /* If the real section is empty or there was a problem reading the
2556 section we shouldn't get here. */
2557 gdb_assert (containing_section->buffer != NULL);
2558 info->buffer = containing_section->buffer + info->virtual_offset;
2559 return;
2560 }
2561
2562 /* If the section has relocations, we must read it ourselves.
2563 Otherwise we attach it to the BFD. */
2564 if ((sectp->flags & SEC_RELOC) == 0)
2565 {
2566 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2567 return;
2568 }
2569
2570 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2571 info->buffer = buf;
2572
2573 /* When debugging .o files, we may need to apply relocations; see
2574 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2575 We never compress sections in .o files, so we only need to
2576 try this when the section is not compressed. */
2577 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2578 if (retbuf != NULL)
2579 {
2580 info->buffer = retbuf;
2581 return;
2582 }
2583
2584 abfd = get_section_bfd_owner (info);
2585 gdb_assert (abfd != NULL);
2586
2587 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2588 || bfd_bread (buf, info->size, abfd) != info->size)
2589 {
2590 error (_("Dwarf Error: Can't read DWARF data"
2591 " in section %s [in module %s]"),
2592 bfd_section_name (sectp), bfd_get_filename (abfd));
2593 }
2594 }
2595
2596 /* A helper function that returns the size of a section in a safe way.
2597 If you are positive that the section has been read before using the
2598 size, then it is safe to refer to the dwarf2_section_info object's
2599 "size" field directly. In other cases, you must call this
2600 function, because for compressed sections the size field is not set
2601 correctly until the section has been read. */
2602
2603 static bfd_size_type
2604 dwarf2_section_size (struct objfile *objfile,
2605 struct dwarf2_section_info *info)
2606 {
2607 if (!info->readin)
2608 dwarf2_read_section (objfile, info);
2609 return info->size;
2610 }
2611
2612 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2613 SECTION_NAME. */
2614
2615 void
2616 dwarf2_get_section_info (struct objfile *objfile,
2617 enum dwarf2_section_enum sect,
2618 asection **sectp, const gdb_byte **bufp,
2619 bfd_size_type *sizep)
2620 {
2621 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2622 struct dwarf2_section_info *info;
2623
2624 /* We may see an objfile without any DWARF, in which case we just
2625 return nothing. */
2626 if (data == NULL)
2627 {
2628 *sectp = NULL;
2629 *bufp = NULL;
2630 *sizep = 0;
2631 return;
2632 }
2633 switch (sect)
2634 {
2635 case DWARF2_DEBUG_FRAME:
2636 info = &data->frame;
2637 break;
2638 case DWARF2_EH_FRAME:
2639 info = &data->eh_frame;
2640 break;
2641 default:
2642 gdb_assert_not_reached ("unexpected section");
2643 }
2644
2645 dwarf2_read_section (objfile, info);
2646
2647 *sectp = get_section_bfd_section (info);
2648 *bufp = info->buffer;
2649 *sizep = info->size;
2650 }
2651
2652 /* A helper function to find the sections for a .dwz file. */
2653
2654 static void
2655 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2656 {
2657 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2658
2659 /* Note that we only support the standard ELF names, because .dwz
2660 is ELF-only (at the time of writing). */
2661 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2662 {
2663 dwz_file->abbrev.s.section = sectp;
2664 dwz_file->abbrev.size = bfd_section_size (sectp);
2665 }
2666 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2667 {
2668 dwz_file->info.s.section = sectp;
2669 dwz_file->info.size = bfd_section_size (sectp);
2670 }
2671 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2672 {
2673 dwz_file->str.s.section = sectp;
2674 dwz_file->str.size = bfd_section_size (sectp);
2675 }
2676 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2677 {
2678 dwz_file->line.s.section = sectp;
2679 dwz_file->line.size = bfd_section_size (sectp);
2680 }
2681 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2682 {
2683 dwz_file->macro.s.section = sectp;
2684 dwz_file->macro.size = bfd_section_size (sectp);
2685 }
2686 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2687 {
2688 dwz_file->gdb_index.s.section = sectp;
2689 dwz_file->gdb_index.size = bfd_section_size (sectp);
2690 }
2691 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2692 {
2693 dwz_file->debug_names.s.section = sectp;
2694 dwz_file->debug_names.size = bfd_section_size (sectp);
2695 }
2696 }
2697
2698 /* See dwarf2read.h. */
2699
2700 struct dwz_file *
2701 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2702 {
2703 const char *filename;
2704 bfd_size_type buildid_len_arg;
2705 size_t buildid_len;
2706 bfd_byte *buildid;
2707
2708 if (dwarf2_per_objfile->dwz_file != NULL)
2709 return dwarf2_per_objfile->dwz_file.get ();
2710
2711 bfd_set_error (bfd_error_no_error);
2712 gdb::unique_xmalloc_ptr<char> data
2713 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2714 &buildid_len_arg, &buildid));
2715 if (data == NULL)
2716 {
2717 if (bfd_get_error () == bfd_error_no_error)
2718 return NULL;
2719 error (_("could not read '.gnu_debugaltlink' section: %s"),
2720 bfd_errmsg (bfd_get_error ()));
2721 }
2722
2723 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2724
2725 buildid_len = (size_t) buildid_len_arg;
2726
2727 filename = data.get ();
2728
2729 std::string abs_storage;
2730 if (!IS_ABSOLUTE_PATH (filename))
2731 {
2732 gdb::unique_xmalloc_ptr<char> abs
2733 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2734
2735 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2736 filename = abs_storage.c_str ();
2737 }
2738
2739 /* First try the file name given in the section. If that doesn't
2740 work, try to use the build-id instead. */
2741 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2742 if (dwz_bfd != NULL)
2743 {
2744 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2745 dwz_bfd.reset (nullptr);
2746 }
2747
2748 if (dwz_bfd == NULL)
2749 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2750
2751 if (dwz_bfd == NULL)
2752 error (_("could not find '.gnu_debugaltlink' file for %s"),
2753 objfile_name (dwarf2_per_objfile->objfile));
2754
2755 std::unique_ptr<struct dwz_file> result
2756 (new struct dwz_file (std::move (dwz_bfd)));
2757
2758 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2759 result.get ());
2760
2761 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2762 result->dwz_bfd.get ());
2763 dwarf2_per_objfile->dwz_file = std::move (result);
2764 return dwarf2_per_objfile->dwz_file.get ();
2765 }
2766 \f
2767 /* DWARF quick_symbols_functions support. */
2768
2769 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2770 unique line tables, so we maintain a separate table of all .debug_line
2771 derived entries to support the sharing.
2772 All the quick functions need is the list of file names. We discard the
2773 line_header when we're done and don't need to record it here. */
2774 struct quick_file_names
2775 {
2776 /* The data used to construct the hash key. */
2777 struct stmt_list_hash hash;
2778
2779 /* The number of entries in file_names, real_names. */
2780 unsigned int num_file_names;
2781
2782 /* The file names from the line table, after being run through
2783 file_full_name. */
2784 const char **file_names;
2785
2786 /* The file names from the line table after being run through
2787 gdb_realpath. These are computed lazily. */
2788 const char **real_names;
2789 };
2790
2791 /* When using the index (and thus not using psymtabs), each CU has an
2792 object of this type. This is used to hold information needed by
2793 the various "quick" methods. */
2794 struct dwarf2_per_cu_quick_data
2795 {
2796 /* The file table. This can be NULL if there was no file table
2797 or it's currently not read in.
2798 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2799 struct quick_file_names *file_names;
2800
2801 /* The corresponding symbol table. This is NULL if symbols for this
2802 CU have not yet been read. */
2803 struct compunit_symtab *compunit_symtab;
2804
2805 /* A temporary mark bit used when iterating over all CUs in
2806 expand_symtabs_matching. */
2807 unsigned int mark : 1;
2808
2809 /* True if we've tried to read the file table and found there isn't one.
2810 There will be no point in trying to read it again next time. */
2811 unsigned int no_file_data : 1;
2812 };
2813
2814 /* Utility hash function for a stmt_list_hash. */
2815
2816 static hashval_t
2817 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2818 {
2819 hashval_t v = 0;
2820
2821 if (stmt_list_hash->dwo_unit != NULL)
2822 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2823 v += to_underlying (stmt_list_hash->line_sect_off);
2824 return v;
2825 }
2826
2827 /* Utility equality function for a stmt_list_hash. */
2828
2829 static int
2830 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2831 const struct stmt_list_hash *rhs)
2832 {
2833 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2834 return 0;
2835 if (lhs->dwo_unit != NULL
2836 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2837 return 0;
2838
2839 return lhs->line_sect_off == rhs->line_sect_off;
2840 }
2841
2842 /* Hash function for a quick_file_names. */
2843
2844 static hashval_t
2845 hash_file_name_entry (const void *e)
2846 {
2847 const struct quick_file_names *file_data
2848 = (const struct quick_file_names *) e;
2849
2850 return hash_stmt_list_entry (&file_data->hash);
2851 }
2852
2853 /* Equality function for a quick_file_names. */
2854
2855 static int
2856 eq_file_name_entry (const void *a, const void *b)
2857 {
2858 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2859 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2860
2861 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2862 }
2863
2864 /* Delete function for a quick_file_names. */
2865
2866 static void
2867 delete_file_name_entry (void *e)
2868 {
2869 struct quick_file_names *file_data = (struct quick_file_names *) e;
2870 int i;
2871
2872 for (i = 0; i < file_data->num_file_names; ++i)
2873 {
2874 xfree ((void*) file_data->file_names[i]);
2875 if (file_data->real_names)
2876 xfree ((void*) file_data->real_names[i]);
2877 }
2878
2879 /* The space for the struct itself lives on objfile_obstack,
2880 so we don't free it here. */
2881 }
2882
2883 /* Create a quick_file_names hash table. */
2884
2885 static htab_t
2886 create_quick_file_names_table (unsigned int nr_initial_entries)
2887 {
2888 return htab_create_alloc (nr_initial_entries,
2889 hash_file_name_entry, eq_file_name_entry,
2890 delete_file_name_entry, xcalloc, xfree);
2891 }
2892
2893 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2894 have to be created afterwards. You should call age_cached_comp_units after
2895 processing PER_CU->CU. dw2_setup must have been already called. */
2896
2897 static void
2898 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2899 {
2900 if (per_cu->is_debug_types)
2901 load_full_type_unit (per_cu);
2902 else
2903 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2904
2905 if (per_cu->cu == NULL)
2906 return; /* Dummy CU. */
2907
2908 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2909 }
2910
2911 /* Read in the symbols for PER_CU. */
2912
2913 static void
2914 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2915 {
2916 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2917
2918 /* Skip type_unit_groups, reading the type units they contain
2919 is handled elsewhere. */
2920 if (IS_TYPE_UNIT_GROUP (per_cu))
2921 return;
2922
2923 /* The destructor of dwarf2_queue_guard frees any entries left on
2924 the queue. After this point we're guaranteed to leave this function
2925 with the dwarf queue empty. */
2926 dwarf2_queue_guard q_guard;
2927
2928 if (dwarf2_per_objfile->using_index
2929 ? per_cu->v.quick->compunit_symtab == NULL
2930 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2931 {
2932 queue_comp_unit (per_cu, language_minimal);
2933 load_cu (per_cu, skip_partial);
2934
2935 /* If we just loaded a CU from a DWO, and we're working with an index
2936 that may badly handle TUs, load all the TUs in that DWO as well.
2937 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2938 if (!per_cu->is_debug_types
2939 && per_cu->cu != NULL
2940 && per_cu->cu->dwo_unit != NULL
2941 && dwarf2_per_objfile->index_table != NULL
2942 && dwarf2_per_objfile->index_table->version <= 7
2943 /* DWP files aren't supported yet. */
2944 && get_dwp_file (dwarf2_per_objfile) == NULL)
2945 queue_and_load_all_dwo_tus (per_cu);
2946 }
2947
2948 process_queue (dwarf2_per_objfile);
2949
2950 /* Age the cache, releasing compilation units that have not
2951 been used recently. */
2952 age_cached_comp_units (dwarf2_per_objfile);
2953 }
2954
2955 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2956 the objfile from which this CU came. Returns the resulting symbol
2957 table. */
2958
2959 static struct compunit_symtab *
2960 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2961 {
2962 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2963
2964 gdb_assert (dwarf2_per_objfile->using_index);
2965 if (!per_cu->v.quick->compunit_symtab)
2966 {
2967 free_cached_comp_units freer (dwarf2_per_objfile);
2968 scoped_restore decrementer = increment_reading_symtab ();
2969 dw2_do_instantiate_symtab (per_cu, skip_partial);
2970 process_cu_includes (dwarf2_per_objfile);
2971 }
2972
2973 return per_cu->v.quick->compunit_symtab;
2974 }
2975
2976 /* See declaration. */
2977
2978 dwarf2_per_cu_data *
2979 dwarf2_per_objfile::get_cutu (int index)
2980 {
2981 if (index >= this->all_comp_units.size ())
2982 {
2983 index -= this->all_comp_units.size ();
2984 gdb_assert (index < this->all_type_units.size ());
2985 return &this->all_type_units[index]->per_cu;
2986 }
2987
2988 return this->all_comp_units[index];
2989 }
2990
2991 /* See declaration. */
2992
2993 dwarf2_per_cu_data *
2994 dwarf2_per_objfile::get_cu (int index)
2995 {
2996 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2997
2998 return this->all_comp_units[index];
2999 }
3000
3001 /* See declaration. */
3002
3003 signatured_type *
3004 dwarf2_per_objfile::get_tu (int index)
3005 {
3006 gdb_assert (index >= 0 && index < this->all_type_units.size ());
3007
3008 return this->all_type_units[index];
3009 }
3010
3011 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3012 objfile_obstack, and constructed with the specified field
3013 values. */
3014
3015 static dwarf2_per_cu_data *
3016 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3017 struct dwarf2_section_info *section,
3018 int is_dwz,
3019 sect_offset sect_off, ULONGEST length)
3020 {
3021 struct objfile *objfile = dwarf2_per_objfile->objfile;
3022 dwarf2_per_cu_data *the_cu
3023 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3024 struct dwarf2_per_cu_data);
3025 the_cu->sect_off = sect_off;
3026 the_cu->length = length;
3027 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3028 the_cu->section = section;
3029 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3030 struct dwarf2_per_cu_quick_data);
3031 the_cu->is_dwz = is_dwz;
3032 return the_cu;
3033 }
3034
3035 /* A helper for create_cus_from_index that handles a given list of
3036 CUs. */
3037
3038 static void
3039 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3040 const gdb_byte *cu_list, offset_type n_elements,
3041 struct dwarf2_section_info *section,
3042 int is_dwz)
3043 {
3044 for (offset_type i = 0; i < n_elements; i += 2)
3045 {
3046 gdb_static_assert (sizeof (ULONGEST) >= 8);
3047
3048 sect_offset sect_off
3049 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3050 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3051 cu_list += 2 * 8;
3052
3053 dwarf2_per_cu_data *per_cu
3054 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3055 sect_off, length);
3056 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3057 }
3058 }
3059
3060 /* Read the CU list from the mapped index, and use it to create all
3061 the CU objects for this objfile. */
3062
3063 static void
3064 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3065 const gdb_byte *cu_list, offset_type cu_list_elements,
3066 const gdb_byte *dwz_list, offset_type dwz_elements)
3067 {
3068 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3069 dwarf2_per_objfile->all_comp_units.reserve
3070 ((cu_list_elements + dwz_elements) / 2);
3071
3072 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3073 &dwarf2_per_objfile->info, 0);
3074
3075 if (dwz_elements == 0)
3076 return;
3077
3078 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3079 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3080 &dwz->info, 1);
3081 }
3082
3083 /* Create the signatured type hash table from the index. */
3084
3085 static void
3086 create_signatured_type_table_from_index
3087 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3088 struct dwarf2_section_info *section,
3089 const gdb_byte *bytes,
3090 offset_type elements)
3091 {
3092 struct objfile *objfile = dwarf2_per_objfile->objfile;
3093
3094 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3095 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3096
3097 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3098
3099 for (offset_type i = 0; i < elements; i += 3)
3100 {
3101 struct signatured_type *sig_type;
3102 ULONGEST signature;
3103 void **slot;
3104 cu_offset type_offset_in_tu;
3105
3106 gdb_static_assert (sizeof (ULONGEST) >= 8);
3107 sect_offset sect_off
3108 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3109 type_offset_in_tu
3110 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3111 BFD_ENDIAN_LITTLE);
3112 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3113 bytes += 3 * 8;
3114
3115 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3116 struct signatured_type);
3117 sig_type->signature = signature;
3118 sig_type->type_offset_in_tu = type_offset_in_tu;
3119 sig_type->per_cu.is_debug_types = 1;
3120 sig_type->per_cu.section = section;
3121 sig_type->per_cu.sect_off = sect_off;
3122 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3123 sig_type->per_cu.v.quick
3124 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3125 struct dwarf2_per_cu_quick_data);
3126
3127 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3128 *slot = sig_type;
3129
3130 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3131 }
3132
3133 dwarf2_per_objfile->signatured_types = sig_types_hash;
3134 }
3135
3136 /* Create the signatured type hash table from .debug_names. */
3137
3138 static void
3139 create_signatured_type_table_from_debug_names
3140 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3141 const mapped_debug_names &map,
3142 struct dwarf2_section_info *section,
3143 struct dwarf2_section_info *abbrev_section)
3144 {
3145 struct objfile *objfile = dwarf2_per_objfile->objfile;
3146
3147 dwarf2_read_section (objfile, section);
3148 dwarf2_read_section (objfile, abbrev_section);
3149
3150 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3151 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3152
3153 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3154
3155 for (uint32_t i = 0; i < map.tu_count; ++i)
3156 {
3157 struct signatured_type *sig_type;
3158 void **slot;
3159
3160 sect_offset sect_off
3161 = (sect_offset) (extract_unsigned_integer
3162 (map.tu_table_reordered + i * map.offset_size,
3163 map.offset_size,
3164 map.dwarf5_byte_order));
3165
3166 comp_unit_head cu_header;
3167 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3168 abbrev_section,
3169 section->buffer + to_underlying (sect_off),
3170 rcuh_kind::TYPE);
3171
3172 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3173 struct signatured_type);
3174 sig_type->signature = cu_header.signature;
3175 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3176 sig_type->per_cu.is_debug_types = 1;
3177 sig_type->per_cu.section = section;
3178 sig_type->per_cu.sect_off = sect_off;
3179 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3180 sig_type->per_cu.v.quick
3181 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3182 struct dwarf2_per_cu_quick_data);
3183
3184 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3185 *slot = sig_type;
3186
3187 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3188 }
3189
3190 dwarf2_per_objfile->signatured_types = sig_types_hash;
3191 }
3192
3193 /* Read the address map data from the mapped index, and use it to
3194 populate the objfile's psymtabs_addrmap. */
3195
3196 static void
3197 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3198 struct mapped_index *index)
3199 {
3200 struct objfile *objfile = dwarf2_per_objfile->objfile;
3201 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3202 const gdb_byte *iter, *end;
3203 struct addrmap *mutable_map;
3204 CORE_ADDR baseaddr;
3205
3206 auto_obstack temp_obstack;
3207
3208 mutable_map = addrmap_create_mutable (&temp_obstack);
3209
3210 iter = index->address_table.data ();
3211 end = iter + index->address_table.size ();
3212
3213 baseaddr = objfile->text_section_offset ();
3214
3215 while (iter < end)
3216 {
3217 ULONGEST hi, lo, cu_index;
3218 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3219 iter += 8;
3220 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3221 iter += 8;
3222 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3223 iter += 4;
3224
3225 if (lo > hi)
3226 {
3227 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3228 hex_string (lo), hex_string (hi));
3229 continue;
3230 }
3231
3232 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3233 {
3234 complaint (_(".gdb_index address table has invalid CU number %u"),
3235 (unsigned) cu_index);
3236 continue;
3237 }
3238
3239 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3240 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3241 addrmap_set_empty (mutable_map, lo, hi - 1,
3242 dwarf2_per_objfile->get_cu (cu_index));
3243 }
3244
3245 objfile->partial_symtabs->psymtabs_addrmap
3246 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3247 }
3248
3249 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3250 populate the objfile's psymtabs_addrmap. */
3251
3252 static void
3253 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3254 struct dwarf2_section_info *section)
3255 {
3256 struct objfile *objfile = dwarf2_per_objfile->objfile;
3257 bfd *abfd = objfile->obfd;
3258 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3259 const CORE_ADDR baseaddr = objfile->text_section_offset ();
3260
3261 auto_obstack temp_obstack;
3262 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3263
3264 std::unordered_map<sect_offset,
3265 dwarf2_per_cu_data *,
3266 gdb::hash_enum<sect_offset>>
3267 debug_info_offset_to_per_cu;
3268 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3269 {
3270 const auto insertpair
3271 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3272 if (!insertpair.second)
3273 {
3274 warning (_("Section .debug_aranges in %s has duplicate "
3275 "debug_info_offset %s, ignoring .debug_aranges."),
3276 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3277 return;
3278 }
3279 }
3280
3281 dwarf2_read_section (objfile, section);
3282
3283 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3284
3285 const gdb_byte *addr = section->buffer;
3286
3287 while (addr < section->buffer + section->size)
3288 {
3289 const gdb_byte *const entry_addr = addr;
3290 unsigned int bytes_read;
3291
3292 const LONGEST entry_length = read_initial_length (abfd, addr,
3293 &bytes_read);
3294 addr += bytes_read;
3295
3296 const gdb_byte *const entry_end = addr + entry_length;
3297 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3298 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3299 if (addr + entry_length > section->buffer + section->size)
3300 {
3301 warning (_("Section .debug_aranges in %s entry at offset %s "
3302 "length %s exceeds section length %s, "
3303 "ignoring .debug_aranges."),
3304 objfile_name (objfile),
3305 plongest (entry_addr - section->buffer),
3306 plongest (bytes_read + entry_length),
3307 pulongest (section->size));
3308 return;
3309 }
3310
3311 /* The version number. */
3312 const uint16_t version = read_2_bytes (abfd, addr);
3313 addr += 2;
3314 if (version != 2)
3315 {
3316 warning (_("Section .debug_aranges in %s entry at offset %s "
3317 "has unsupported version %d, ignoring .debug_aranges."),
3318 objfile_name (objfile),
3319 plongest (entry_addr - section->buffer), version);
3320 return;
3321 }
3322
3323 const uint64_t debug_info_offset
3324 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3325 addr += offset_size;
3326 const auto per_cu_it
3327 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3328 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3329 {
3330 warning (_("Section .debug_aranges in %s entry at offset %s "
3331 "debug_info_offset %s does not exists, "
3332 "ignoring .debug_aranges."),
3333 objfile_name (objfile),
3334 plongest (entry_addr - section->buffer),
3335 pulongest (debug_info_offset));
3336 return;
3337 }
3338 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3339
3340 const uint8_t address_size = *addr++;
3341 if (address_size < 1 || address_size > 8)
3342 {
3343 warning (_("Section .debug_aranges in %s entry at offset %s "
3344 "address_size %u is invalid, ignoring .debug_aranges."),
3345 objfile_name (objfile),
3346 plongest (entry_addr - section->buffer), address_size);
3347 return;
3348 }
3349
3350 const uint8_t segment_selector_size = *addr++;
3351 if (segment_selector_size != 0)
3352 {
3353 warning (_("Section .debug_aranges in %s entry at offset %s "
3354 "segment_selector_size %u is not supported, "
3355 "ignoring .debug_aranges."),
3356 objfile_name (objfile),
3357 plongest (entry_addr - section->buffer),
3358 segment_selector_size);
3359 return;
3360 }
3361
3362 /* Must pad to an alignment boundary that is twice the address
3363 size. It is undocumented by the DWARF standard but GCC does
3364 use it. */
3365 for (size_t padding = ((-(addr - section->buffer))
3366 & (2 * address_size - 1));
3367 padding > 0; padding--)
3368 if (*addr++ != 0)
3369 {
3370 warning (_("Section .debug_aranges in %s entry at offset %s "
3371 "padding is not zero, ignoring .debug_aranges."),
3372 objfile_name (objfile),
3373 plongest (entry_addr - section->buffer));
3374 return;
3375 }
3376
3377 for (;;)
3378 {
3379 if (addr + 2 * address_size > entry_end)
3380 {
3381 warning (_("Section .debug_aranges in %s entry at offset %s "
3382 "address list is not properly terminated, "
3383 "ignoring .debug_aranges."),
3384 objfile_name (objfile),
3385 plongest (entry_addr - section->buffer));
3386 return;
3387 }
3388 ULONGEST start = extract_unsigned_integer (addr, address_size,
3389 dwarf5_byte_order);
3390 addr += address_size;
3391 ULONGEST length = extract_unsigned_integer (addr, address_size,
3392 dwarf5_byte_order);
3393 addr += address_size;
3394 if (start == 0 && length == 0)
3395 break;
3396 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3397 {
3398 /* Symbol was eliminated due to a COMDAT group. */
3399 continue;
3400 }
3401 ULONGEST end = start + length;
3402 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3403 - baseaddr);
3404 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3405 - baseaddr);
3406 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3407 }
3408 }
3409
3410 objfile->partial_symtabs->psymtabs_addrmap
3411 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3412 }
3413
3414 /* Find a slot in the mapped index INDEX for the object named NAME.
3415 If NAME is found, set *VEC_OUT to point to the CU vector in the
3416 constant pool and return true. If NAME cannot be found, return
3417 false. */
3418
3419 static bool
3420 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3421 offset_type **vec_out)
3422 {
3423 offset_type hash;
3424 offset_type slot, step;
3425 int (*cmp) (const char *, const char *);
3426
3427 gdb::unique_xmalloc_ptr<char> without_params;
3428 if (current_language->la_language == language_cplus
3429 || current_language->la_language == language_fortran
3430 || current_language->la_language == language_d)
3431 {
3432 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3433 not contain any. */
3434
3435 if (strchr (name, '(') != NULL)
3436 {
3437 without_params = cp_remove_params (name);
3438
3439 if (without_params != NULL)
3440 name = without_params.get ();
3441 }
3442 }
3443
3444 /* Index version 4 did not support case insensitive searches. But the
3445 indices for case insensitive languages are built in lowercase, therefore
3446 simulate our NAME being searched is also lowercased. */
3447 hash = mapped_index_string_hash ((index->version == 4
3448 && case_sensitivity == case_sensitive_off
3449 ? 5 : index->version),
3450 name);
3451
3452 slot = hash & (index->symbol_table.size () - 1);
3453 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3454 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3455
3456 for (;;)
3457 {
3458 const char *str;
3459
3460 const auto &bucket = index->symbol_table[slot];
3461 if (bucket.name == 0 && bucket.vec == 0)
3462 return false;
3463
3464 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3465 if (!cmp (name, str))
3466 {
3467 *vec_out = (offset_type *) (index->constant_pool
3468 + MAYBE_SWAP (bucket.vec));
3469 return true;
3470 }
3471
3472 slot = (slot + step) & (index->symbol_table.size () - 1);
3473 }
3474 }
3475
3476 /* A helper function that reads the .gdb_index from BUFFER and fills
3477 in MAP. FILENAME is the name of the file containing the data;
3478 it is used for error reporting. DEPRECATED_OK is true if it is
3479 ok to use deprecated sections.
3480
3481 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3482 out parameters that are filled in with information about the CU and
3483 TU lists in the section.
3484
3485 Returns true if all went well, false otherwise. */
3486
3487 static bool
3488 read_gdb_index_from_buffer (struct objfile *objfile,
3489 const char *filename,
3490 bool deprecated_ok,
3491 gdb::array_view<const gdb_byte> buffer,
3492 struct mapped_index *map,
3493 const gdb_byte **cu_list,
3494 offset_type *cu_list_elements,
3495 const gdb_byte **types_list,
3496 offset_type *types_list_elements)
3497 {
3498 const gdb_byte *addr = &buffer[0];
3499
3500 /* Version check. */
3501 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3502 /* Versions earlier than 3 emitted every copy of a psymbol. This
3503 causes the index to behave very poorly for certain requests. Version 3
3504 contained incomplete addrmap. So, it seems better to just ignore such
3505 indices. */
3506 if (version < 4)
3507 {
3508 static int warning_printed = 0;
3509 if (!warning_printed)
3510 {
3511 warning (_("Skipping obsolete .gdb_index section in %s."),
3512 filename);
3513 warning_printed = 1;
3514 }
3515 return 0;
3516 }
3517 /* Index version 4 uses a different hash function than index version
3518 5 and later.
3519
3520 Versions earlier than 6 did not emit psymbols for inlined
3521 functions. Using these files will cause GDB not to be able to
3522 set breakpoints on inlined functions by name, so we ignore these
3523 indices unless the user has done
3524 "set use-deprecated-index-sections on". */
3525 if (version < 6 && !deprecated_ok)
3526 {
3527 static int warning_printed = 0;
3528 if (!warning_printed)
3529 {
3530 warning (_("\
3531 Skipping deprecated .gdb_index section in %s.\n\
3532 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3533 to use the section anyway."),
3534 filename);
3535 warning_printed = 1;
3536 }
3537 return 0;
3538 }
3539 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3540 of the TU (for symbols coming from TUs),
3541 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3542 Plus gold-generated indices can have duplicate entries for global symbols,
3543 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3544 These are just performance bugs, and we can't distinguish gdb-generated
3545 indices from gold-generated ones, so issue no warning here. */
3546
3547 /* Indexes with higher version than the one supported by GDB may be no
3548 longer backward compatible. */
3549 if (version > 8)
3550 return 0;
3551
3552 map->version = version;
3553
3554 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3555
3556 int i = 0;
3557 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3558 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3559 / 8);
3560 ++i;
3561
3562 *types_list = addr + MAYBE_SWAP (metadata[i]);
3563 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3564 - MAYBE_SWAP (metadata[i]))
3565 / 8);
3566 ++i;
3567
3568 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3569 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3570 map->address_table
3571 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3572 ++i;
3573
3574 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3575 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3576 map->symbol_table
3577 = gdb::array_view<mapped_index::symbol_table_slot>
3578 ((mapped_index::symbol_table_slot *) symbol_table,
3579 (mapped_index::symbol_table_slot *) symbol_table_end);
3580
3581 ++i;
3582 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3583
3584 return 1;
3585 }
3586
3587 /* Callback types for dwarf2_read_gdb_index. */
3588
3589 typedef gdb::function_view
3590 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3591 get_gdb_index_contents_ftype;
3592 typedef gdb::function_view
3593 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3594 get_gdb_index_contents_dwz_ftype;
3595
3596 /* Read .gdb_index. If everything went ok, initialize the "quick"
3597 elements of all the CUs and return 1. Otherwise, return 0. */
3598
3599 static int
3600 dwarf2_read_gdb_index
3601 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3602 get_gdb_index_contents_ftype get_gdb_index_contents,
3603 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3604 {
3605 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3606 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3607 struct dwz_file *dwz;
3608 struct objfile *objfile = dwarf2_per_objfile->objfile;
3609
3610 gdb::array_view<const gdb_byte> main_index_contents
3611 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3612
3613 if (main_index_contents.empty ())
3614 return 0;
3615
3616 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3617 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3618 use_deprecated_index_sections,
3619 main_index_contents, map.get (), &cu_list,
3620 &cu_list_elements, &types_list,
3621 &types_list_elements))
3622 return 0;
3623
3624 /* Don't use the index if it's empty. */
3625 if (map->symbol_table.empty ())
3626 return 0;
3627
3628 /* If there is a .dwz file, read it so we can get its CU list as
3629 well. */
3630 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3631 if (dwz != NULL)
3632 {
3633 struct mapped_index dwz_map;
3634 const gdb_byte *dwz_types_ignore;
3635 offset_type dwz_types_elements_ignore;
3636
3637 gdb::array_view<const gdb_byte> dwz_index_content
3638 = get_gdb_index_contents_dwz (objfile, dwz);
3639
3640 if (dwz_index_content.empty ())
3641 return 0;
3642
3643 if (!read_gdb_index_from_buffer (objfile,
3644 bfd_get_filename (dwz->dwz_bfd.get ()),
3645 1, dwz_index_content, &dwz_map,
3646 &dwz_list, &dwz_list_elements,
3647 &dwz_types_ignore,
3648 &dwz_types_elements_ignore))
3649 {
3650 warning (_("could not read '.gdb_index' section from %s; skipping"),
3651 bfd_get_filename (dwz->dwz_bfd.get ()));
3652 return 0;
3653 }
3654 }
3655
3656 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3657 dwz_list, dwz_list_elements);
3658
3659 if (types_list_elements)
3660 {
3661 /* We can only handle a single .debug_types when we have an
3662 index. */
3663 if (dwarf2_per_objfile->types.size () != 1)
3664 return 0;
3665
3666 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3667
3668 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3669 types_list, types_list_elements);
3670 }
3671
3672 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3673
3674 dwarf2_per_objfile->index_table = std::move (map);
3675 dwarf2_per_objfile->using_index = 1;
3676 dwarf2_per_objfile->quick_file_names_table =
3677 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3678
3679 return 1;
3680 }
3681
3682 /* die_reader_func for dw2_get_file_names. */
3683
3684 static void
3685 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3686 const gdb_byte *info_ptr,
3687 struct die_info *comp_unit_die,
3688 int has_children)
3689 {
3690 struct dwarf2_cu *cu = reader->cu;
3691 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3692 struct dwarf2_per_objfile *dwarf2_per_objfile
3693 = cu->per_cu->dwarf2_per_objfile;
3694 struct objfile *objfile = dwarf2_per_objfile->objfile;
3695 struct dwarf2_per_cu_data *lh_cu;
3696 struct attribute *attr;
3697 void **slot;
3698 struct quick_file_names *qfn;
3699
3700 gdb_assert (! this_cu->is_debug_types);
3701
3702 /* Our callers never want to match partial units -- instead they
3703 will match the enclosing full CU. */
3704 if (comp_unit_die->tag == DW_TAG_partial_unit)
3705 {
3706 this_cu->v.quick->no_file_data = 1;
3707 return;
3708 }
3709
3710 lh_cu = this_cu;
3711 slot = NULL;
3712
3713 line_header_up lh;
3714 sect_offset line_offset {};
3715
3716 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3717 if (attr != nullptr)
3718 {
3719 struct quick_file_names find_entry;
3720
3721 line_offset = (sect_offset) DW_UNSND (attr);
3722
3723 /* We may have already read in this line header (TU line header sharing).
3724 If we have we're done. */
3725 find_entry.hash.dwo_unit = cu->dwo_unit;
3726 find_entry.hash.line_sect_off = line_offset;
3727 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3728 &find_entry, INSERT);
3729 if (*slot != NULL)
3730 {
3731 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3732 return;
3733 }
3734
3735 lh = dwarf_decode_line_header (line_offset, cu);
3736 }
3737 if (lh == NULL)
3738 {
3739 lh_cu->v.quick->no_file_data = 1;
3740 return;
3741 }
3742
3743 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3744 qfn->hash.dwo_unit = cu->dwo_unit;
3745 qfn->hash.line_sect_off = line_offset;
3746 gdb_assert (slot != NULL);
3747 *slot = qfn;
3748
3749 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3750
3751 int offset = 0;
3752 if (strcmp (fnd.name, "<unknown>") != 0)
3753 ++offset;
3754
3755 qfn->num_file_names = offset + lh->file_names_size ();
3756 qfn->file_names =
3757 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3758 if (offset != 0)
3759 qfn->file_names[0] = xstrdup (fnd.name);
3760 for (int i = 0; i < lh->file_names_size (); ++i)
3761 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3762 qfn->real_names = NULL;
3763
3764 lh_cu->v.quick->file_names = qfn;
3765 }
3766
3767 /* A helper for the "quick" functions which attempts to read the line
3768 table for THIS_CU. */
3769
3770 static struct quick_file_names *
3771 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3772 {
3773 /* This should never be called for TUs. */
3774 gdb_assert (! this_cu->is_debug_types);
3775 /* Nor type unit groups. */
3776 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3777
3778 if (this_cu->v.quick->file_names != NULL)
3779 return this_cu->v.quick->file_names;
3780 /* If we know there is no line data, no point in looking again. */
3781 if (this_cu->v.quick->no_file_data)
3782 return NULL;
3783
3784 cutu_reader reader (this_cu);
3785 if (!reader.dummy_p)
3786 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3787 reader.has_children);
3788
3789 if (this_cu->v.quick->no_file_data)
3790 return NULL;
3791 return this_cu->v.quick->file_names;
3792 }
3793
3794 /* A helper for the "quick" functions which computes and caches the
3795 real path for a given file name from the line table. */
3796
3797 static const char *
3798 dw2_get_real_path (struct objfile *objfile,
3799 struct quick_file_names *qfn, int index)
3800 {
3801 if (qfn->real_names == NULL)
3802 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3803 qfn->num_file_names, const char *);
3804
3805 if (qfn->real_names[index] == NULL)
3806 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3807
3808 return qfn->real_names[index];
3809 }
3810
3811 static struct symtab *
3812 dw2_find_last_source_symtab (struct objfile *objfile)
3813 {
3814 struct dwarf2_per_objfile *dwarf2_per_objfile
3815 = get_dwarf2_per_objfile (objfile);
3816 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3817 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3818
3819 if (cust == NULL)
3820 return NULL;
3821
3822 return compunit_primary_filetab (cust);
3823 }
3824
3825 /* Traversal function for dw2_forget_cached_source_info. */
3826
3827 static int
3828 dw2_free_cached_file_names (void **slot, void *info)
3829 {
3830 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3831
3832 if (file_data->real_names)
3833 {
3834 int i;
3835
3836 for (i = 0; i < file_data->num_file_names; ++i)
3837 {
3838 xfree ((void*) file_data->real_names[i]);
3839 file_data->real_names[i] = NULL;
3840 }
3841 }
3842
3843 return 1;
3844 }
3845
3846 static void
3847 dw2_forget_cached_source_info (struct objfile *objfile)
3848 {
3849 struct dwarf2_per_objfile *dwarf2_per_objfile
3850 = get_dwarf2_per_objfile (objfile);
3851
3852 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3853 dw2_free_cached_file_names, NULL);
3854 }
3855
3856 /* Helper function for dw2_map_symtabs_matching_filename that expands
3857 the symtabs and calls the iterator. */
3858
3859 static int
3860 dw2_map_expand_apply (struct objfile *objfile,
3861 struct dwarf2_per_cu_data *per_cu,
3862 const char *name, const char *real_path,
3863 gdb::function_view<bool (symtab *)> callback)
3864 {
3865 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3866
3867 /* Don't visit already-expanded CUs. */
3868 if (per_cu->v.quick->compunit_symtab)
3869 return 0;
3870
3871 /* This may expand more than one symtab, and we want to iterate over
3872 all of them. */
3873 dw2_instantiate_symtab (per_cu, false);
3874
3875 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3876 last_made, callback);
3877 }
3878
3879 /* Implementation of the map_symtabs_matching_filename method. */
3880
3881 static bool
3882 dw2_map_symtabs_matching_filename
3883 (struct objfile *objfile, const char *name, const char *real_path,
3884 gdb::function_view<bool (symtab *)> callback)
3885 {
3886 const char *name_basename = lbasename (name);
3887 struct dwarf2_per_objfile *dwarf2_per_objfile
3888 = get_dwarf2_per_objfile (objfile);
3889
3890 /* The rule is CUs specify all the files, including those used by
3891 any TU, so there's no need to scan TUs here. */
3892
3893 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3894 {
3895 /* We only need to look at symtabs not already expanded. */
3896 if (per_cu->v.quick->compunit_symtab)
3897 continue;
3898
3899 quick_file_names *file_data = dw2_get_file_names (per_cu);
3900 if (file_data == NULL)
3901 continue;
3902
3903 for (int j = 0; j < file_data->num_file_names; ++j)
3904 {
3905 const char *this_name = file_data->file_names[j];
3906 const char *this_real_name;
3907
3908 if (compare_filenames_for_search (this_name, name))
3909 {
3910 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3911 callback))
3912 return true;
3913 continue;
3914 }
3915
3916 /* Before we invoke realpath, which can get expensive when many
3917 files are involved, do a quick comparison of the basenames. */
3918 if (! basenames_may_differ
3919 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3920 continue;
3921
3922 this_real_name = dw2_get_real_path (objfile, file_data, j);
3923 if (compare_filenames_for_search (this_real_name, name))
3924 {
3925 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3926 callback))
3927 return true;
3928 continue;
3929 }
3930
3931 if (real_path != NULL)
3932 {
3933 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3934 gdb_assert (IS_ABSOLUTE_PATH (name));
3935 if (this_real_name != NULL
3936 && FILENAME_CMP (real_path, this_real_name) == 0)
3937 {
3938 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3939 callback))
3940 return true;
3941 continue;
3942 }
3943 }
3944 }
3945 }
3946
3947 return false;
3948 }
3949
3950 /* Struct used to manage iterating over all CUs looking for a symbol. */
3951
3952 struct dw2_symtab_iterator
3953 {
3954 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3955 struct dwarf2_per_objfile *dwarf2_per_objfile;
3956 /* If set, only look for symbols that match that block. Valid values are
3957 GLOBAL_BLOCK and STATIC_BLOCK. */
3958 gdb::optional<block_enum> block_index;
3959 /* The kind of symbol we're looking for. */
3960 domain_enum domain;
3961 /* The list of CUs from the index entry of the symbol,
3962 or NULL if not found. */
3963 offset_type *vec;
3964 /* The next element in VEC to look at. */
3965 int next;
3966 /* The number of elements in VEC, or zero if there is no match. */
3967 int length;
3968 /* Have we seen a global version of the symbol?
3969 If so we can ignore all further global instances.
3970 This is to work around gold/15646, inefficient gold-generated
3971 indices. */
3972 int global_seen;
3973 };
3974
3975 /* Initialize the index symtab iterator ITER. */
3976
3977 static void
3978 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3979 struct dwarf2_per_objfile *dwarf2_per_objfile,
3980 gdb::optional<block_enum> block_index,
3981 domain_enum domain,
3982 const char *name)
3983 {
3984 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3985 iter->block_index = block_index;
3986 iter->domain = domain;
3987 iter->next = 0;
3988 iter->global_seen = 0;
3989
3990 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3991
3992 /* index is NULL if OBJF_READNOW. */
3993 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3994 iter->length = MAYBE_SWAP (*iter->vec);
3995 else
3996 {
3997 iter->vec = NULL;
3998 iter->length = 0;
3999 }
4000 }
4001
4002 /* Return the next matching CU or NULL if there are no more. */
4003
4004 static struct dwarf2_per_cu_data *
4005 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4006 {
4007 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4008
4009 for ( ; iter->next < iter->length; ++iter->next)
4010 {
4011 offset_type cu_index_and_attrs =
4012 MAYBE_SWAP (iter->vec[iter->next + 1]);
4013 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4014 gdb_index_symbol_kind symbol_kind =
4015 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4016 /* Only check the symbol attributes if they're present.
4017 Indices prior to version 7 don't record them,
4018 and indices >= 7 may elide them for certain symbols
4019 (gold does this). */
4020 int attrs_valid =
4021 (dwarf2_per_objfile->index_table->version >= 7
4022 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4023
4024 /* Don't crash on bad data. */
4025 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4026 + dwarf2_per_objfile->all_type_units.size ()))
4027 {
4028 complaint (_(".gdb_index entry has bad CU index"
4029 " [in module %s]"),
4030 objfile_name (dwarf2_per_objfile->objfile));
4031 continue;
4032 }
4033
4034 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4035
4036 /* Skip if already read in. */
4037 if (per_cu->v.quick->compunit_symtab)
4038 continue;
4039
4040 /* Check static vs global. */
4041 if (attrs_valid)
4042 {
4043 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4044
4045 if (iter->block_index.has_value ())
4046 {
4047 bool want_static = *iter->block_index == STATIC_BLOCK;
4048
4049 if (is_static != want_static)
4050 continue;
4051 }
4052
4053 /* Work around gold/15646. */
4054 if (!is_static && iter->global_seen)
4055 continue;
4056 if (!is_static)
4057 iter->global_seen = 1;
4058 }
4059
4060 /* Only check the symbol's kind if it has one. */
4061 if (attrs_valid)
4062 {
4063 switch (iter->domain)
4064 {
4065 case VAR_DOMAIN:
4066 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4067 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4068 /* Some types are also in VAR_DOMAIN. */
4069 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4070 continue;
4071 break;
4072 case STRUCT_DOMAIN:
4073 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4074 continue;
4075 break;
4076 case LABEL_DOMAIN:
4077 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4078 continue;
4079 break;
4080 case MODULE_DOMAIN:
4081 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4082 continue;
4083 break;
4084 default:
4085 break;
4086 }
4087 }
4088
4089 ++iter->next;
4090 return per_cu;
4091 }
4092
4093 return NULL;
4094 }
4095
4096 static struct compunit_symtab *
4097 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4098 const char *name, domain_enum domain)
4099 {
4100 struct compunit_symtab *stab_best = NULL;
4101 struct dwarf2_per_objfile *dwarf2_per_objfile
4102 = get_dwarf2_per_objfile (objfile);
4103
4104 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4105
4106 struct dw2_symtab_iterator iter;
4107 struct dwarf2_per_cu_data *per_cu;
4108
4109 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4110
4111 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4112 {
4113 struct symbol *sym, *with_opaque = NULL;
4114 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4115 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4116 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4117
4118 sym = block_find_symbol (block, name, domain,
4119 block_find_non_opaque_type_preferred,
4120 &with_opaque);
4121
4122 /* Some caution must be observed with overloaded functions
4123 and methods, since the index will not contain any overload
4124 information (but NAME might contain it). */
4125
4126 if (sym != NULL
4127 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4128 return stab;
4129 if (with_opaque != NULL
4130 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4131 stab_best = stab;
4132
4133 /* Keep looking through other CUs. */
4134 }
4135
4136 return stab_best;
4137 }
4138
4139 static void
4140 dw2_print_stats (struct objfile *objfile)
4141 {
4142 struct dwarf2_per_objfile *dwarf2_per_objfile
4143 = get_dwarf2_per_objfile (objfile);
4144 int total = (dwarf2_per_objfile->all_comp_units.size ()
4145 + dwarf2_per_objfile->all_type_units.size ());
4146 int count = 0;
4147
4148 for (int i = 0; i < total; ++i)
4149 {
4150 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4151
4152 if (!per_cu->v.quick->compunit_symtab)
4153 ++count;
4154 }
4155 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4156 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4157 }
4158
4159 /* This dumps minimal information about the index.
4160 It is called via "mt print objfiles".
4161 One use is to verify .gdb_index has been loaded by the
4162 gdb.dwarf2/gdb-index.exp testcase. */
4163
4164 static void
4165 dw2_dump (struct objfile *objfile)
4166 {
4167 struct dwarf2_per_objfile *dwarf2_per_objfile
4168 = get_dwarf2_per_objfile (objfile);
4169
4170 gdb_assert (dwarf2_per_objfile->using_index);
4171 printf_filtered (".gdb_index:");
4172 if (dwarf2_per_objfile->index_table != NULL)
4173 {
4174 printf_filtered (" version %d\n",
4175 dwarf2_per_objfile->index_table->version);
4176 }
4177 else
4178 printf_filtered (" faked for \"readnow\"\n");
4179 printf_filtered ("\n");
4180 }
4181
4182 static void
4183 dw2_expand_symtabs_for_function (struct objfile *objfile,
4184 const char *func_name)
4185 {
4186 struct dwarf2_per_objfile *dwarf2_per_objfile
4187 = get_dwarf2_per_objfile (objfile);
4188
4189 struct dw2_symtab_iterator iter;
4190 struct dwarf2_per_cu_data *per_cu;
4191
4192 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4193
4194 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4195 dw2_instantiate_symtab (per_cu, false);
4196
4197 }
4198
4199 static void
4200 dw2_expand_all_symtabs (struct objfile *objfile)
4201 {
4202 struct dwarf2_per_objfile *dwarf2_per_objfile
4203 = get_dwarf2_per_objfile (objfile);
4204 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4205 + dwarf2_per_objfile->all_type_units.size ());
4206
4207 for (int i = 0; i < total_units; ++i)
4208 {
4209 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4210
4211 /* We don't want to directly expand a partial CU, because if we
4212 read it with the wrong language, then assertion failures can
4213 be triggered later on. See PR symtab/23010. So, tell
4214 dw2_instantiate_symtab to skip partial CUs -- any important
4215 partial CU will be read via DW_TAG_imported_unit anyway. */
4216 dw2_instantiate_symtab (per_cu, true);
4217 }
4218 }
4219
4220 static void
4221 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4222 const char *fullname)
4223 {
4224 struct dwarf2_per_objfile *dwarf2_per_objfile
4225 = get_dwarf2_per_objfile (objfile);
4226
4227 /* We don't need to consider type units here.
4228 This is only called for examining code, e.g. expand_line_sal.
4229 There can be an order of magnitude (or more) more type units
4230 than comp units, and we avoid them if we can. */
4231
4232 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4233 {
4234 /* We only need to look at symtabs not already expanded. */
4235 if (per_cu->v.quick->compunit_symtab)
4236 continue;
4237
4238 quick_file_names *file_data = dw2_get_file_names (per_cu);
4239 if (file_data == NULL)
4240 continue;
4241
4242 for (int j = 0; j < file_data->num_file_names; ++j)
4243 {
4244 const char *this_fullname = file_data->file_names[j];
4245
4246 if (filename_cmp (this_fullname, fullname) == 0)
4247 {
4248 dw2_instantiate_symtab (per_cu, false);
4249 break;
4250 }
4251 }
4252 }
4253 }
4254
4255 static void
4256 dw2_map_matching_symbols
4257 (struct objfile *objfile,
4258 const lookup_name_info &name, domain_enum domain,
4259 int global,
4260 gdb::function_view<symbol_found_callback_ftype> callback,
4261 symbol_compare_ftype *ordered_compare)
4262 {
4263 /* Currently unimplemented; used for Ada. The function can be called if the
4264 current language is Ada for a non-Ada objfile using GNU index. As Ada
4265 does not look for non-Ada symbols this function should just return. */
4266 }
4267
4268 /* Starting from a search name, return the string that finds the upper
4269 bound of all strings that start with SEARCH_NAME in a sorted name
4270 list. Returns the empty string to indicate that the upper bound is
4271 the end of the list. */
4272
4273 static std::string
4274 make_sort_after_prefix_name (const char *search_name)
4275 {
4276 /* When looking to complete "func", we find the upper bound of all
4277 symbols that start with "func" by looking for where we'd insert
4278 the closest string that would follow "func" in lexicographical
4279 order. Usually, that's "func"-with-last-character-incremented,
4280 i.e. "fund". Mind non-ASCII characters, though. Usually those
4281 will be UTF-8 multi-byte sequences, but we can't be certain.
4282 Especially mind the 0xff character, which is a valid character in
4283 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4284 rule out compilers allowing it in identifiers. Note that
4285 conveniently, strcmp/strcasecmp are specified to compare
4286 characters interpreted as unsigned char. So what we do is treat
4287 the whole string as a base 256 number composed of a sequence of
4288 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4289 to 0, and carries 1 to the following more-significant position.
4290 If the very first character in SEARCH_NAME ends up incremented
4291 and carries/overflows, then the upper bound is the end of the
4292 list. The string after the empty string is also the empty
4293 string.
4294
4295 Some examples of this operation:
4296
4297 SEARCH_NAME => "+1" RESULT
4298
4299 "abc" => "abd"
4300 "ab\xff" => "ac"
4301 "\xff" "a" "\xff" => "\xff" "b"
4302 "\xff" => ""
4303 "\xff\xff" => ""
4304 "" => ""
4305
4306 Then, with these symbols for example:
4307
4308 func
4309 func1
4310 fund
4311
4312 completing "func" looks for symbols between "func" and
4313 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4314 which finds "func" and "func1", but not "fund".
4315
4316 And with:
4317
4318 funcÿ (Latin1 'ÿ' [0xff])
4319 funcÿ1
4320 fund
4321
4322 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4323 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4324
4325 And with:
4326
4327 ÿÿ (Latin1 'ÿ' [0xff])
4328 ÿÿ1
4329
4330 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4331 the end of the list.
4332 */
4333 std::string after = search_name;
4334 while (!after.empty () && (unsigned char) after.back () == 0xff)
4335 after.pop_back ();
4336 if (!after.empty ())
4337 after.back () = (unsigned char) after.back () + 1;
4338 return after;
4339 }
4340
4341 /* See declaration. */
4342
4343 std::pair<std::vector<name_component>::const_iterator,
4344 std::vector<name_component>::const_iterator>
4345 mapped_index_base::find_name_components_bounds
4346 (const lookup_name_info &lookup_name_without_params, language lang) const
4347 {
4348 auto *name_cmp
4349 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4350
4351 const char *lang_name
4352 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4353
4354 /* Comparison function object for lower_bound that matches against a
4355 given symbol name. */
4356 auto lookup_compare_lower = [&] (const name_component &elem,
4357 const char *name)
4358 {
4359 const char *elem_qualified = this->symbol_name_at (elem.idx);
4360 const char *elem_name = elem_qualified + elem.name_offset;
4361 return name_cmp (elem_name, name) < 0;
4362 };
4363
4364 /* Comparison function object for upper_bound that matches against a
4365 given symbol name. */
4366 auto lookup_compare_upper = [&] (const char *name,
4367 const name_component &elem)
4368 {
4369 const char *elem_qualified = this->symbol_name_at (elem.idx);
4370 const char *elem_name = elem_qualified + elem.name_offset;
4371 return name_cmp (name, elem_name) < 0;
4372 };
4373
4374 auto begin = this->name_components.begin ();
4375 auto end = this->name_components.end ();
4376
4377 /* Find the lower bound. */
4378 auto lower = [&] ()
4379 {
4380 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4381 return begin;
4382 else
4383 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4384 } ();
4385
4386 /* Find the upper bound. */
4387 auto upper = [&] ()
4388 {
4389 if (lookup_name_without_params.completion_mode ())
4390 {
4391 /* In completion mode, we want UPPER to point past all
4392 symbols names that have the same prefix. I.e., with
4393 these symbols, and completing "func":
4394
4395 function << lower bound
4396 function1
4397 other_function << upper bound
4398
4399 We find the upper bound by looking for the insertion
4400 point of "func"-with-last-character-incremented,
4401 i.e. "fund". */
4402 std::string after = make_sort_after_prefix_name (lang_name);
4403 if (after.empty ())
4404 return end;
4405 return std::lower_bound (lower, end, after.c_str (),
4406 lookup_compare_lower);
4407 }
4408 else
4409 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4410 } ();
4411
4412 return {lower, upper};
4413 }
4414
4415 /* See declaration. */
4416
4417 void
4418 mapped_index_base::build_name_components ()
4419 {
4420 if (!this->name_components.empty ())
4421 return;
4422
4423 this->name_components_casing = case_sensitivity;
4424 auto *name_cmp
4425 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4426
4427 /* The code below only knows how to break apart components of C++
4428 symbol names (and other languages that use '::' as
4429 namespace/module separator) and Ada symbol names. */
4430 auto count = this->symbol_name_count ();
4431 for (offset_type idx = 0; idx < count; idx++)
4432 {
4433 if (this->symbol_name_slot_invalid (idx))
4434 continue;
4435
4436 const char *name = this->symbol_name_at (idx);
4437
4438 /* Add each name component to the name component table. */
4439 unsigned int previous_len = 0;
4440
4441 if (strstr (name, "::") != nullptr)
4442 {
4443 for (unsigned int current_len = cp_find_first_component (name);
4444 name[current_len] != '\0';
4445 current_len += cp_find_first_component (name + current_len))
4446 {
4447 gdb_assert (name[current_len] == ':');
4448 this->name_components.push_back ({previous_len, idx});
4449 /* Skip the '::'. */
4450 current_len += 2;
4451 previous_len = current_len;
4452 }
4453 }
4454 else
4455 {
4456 /* Handle the Ada encoded (aka mangled) form here. */
4457 for (const char *iter = strstr (name, "__");
4458 iter != nullptr;
4459 iter = strstr (iter, "__"))
4460 {
4461 this->name_components.push_back ({previous_len, idx});
4462 iter += 2;
4463 previous_len = iter - name;
4464 }
4465 }
4466
4467 this->name_components.push_back ({previous_len, idx});
4468 }
4469
4470 /* Sort name_components elements by name. */
4471 auto name_comp_compare = [&] (const name_component &left,
4472 const name_component &right)
4473 {
4474 const char *left_qualified = this->symbol_name_at (left.idx);
4475 const char *right_qualified = this->symbol_name_at (right.idx);
4476
4477 const char *left_name = left_qualified + left.name_offset;
4478 const char *right_name = right_qualified + right.name_offset;
4479
4480 return name_cmp (left_name, right_name) < 0;
4481 };
4482
4483 std::sort (this->name_components.begin (),
4484 this->name_components.end (),
4485 name_comp_compare);
4486 }
4487
4488 /* Helper for dw2_expand_symtabs_matching that works with a
4489 mapped_index_base instead of the containing objfile. This is split
4490 to a separate function in order to be able to unit test the
4491 name_components matching using a mock mapped_index_base. For each
4492 symbol name that matches, calls MATCH_CALLBACK, passing it the
4493 symbol's index in the mapped_index_base symbol table. */
4494
4495 static void
4496 dw2_expand_symtabs_matching_symbol
4497 (mapped_index_base &index,
4498 const lookup_name_info &lookup_name_in,
4499 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4500 enum search_domain kind,
4501 gdb::function_view<bool (offset_type)> match_callback)
4502 {
4503 lookup_name_info lookup_name_without_params
4504 = lookup_name_in.make_ignore_params ();
4505
4506 /* Build the symbol name component sorted vector, if we haven't
4507 yet. */
4508 index.build_name_components ();
4509
4510 /* The same symbol may appear more than once in the range though.
4511 E.g., if we're looking for symbols that complete "w", and we have
4512 a symbol named "w1::w2", we'll find the two name components for
4513 that same symbol in the range. To be sure we only call the
4514 callback once per symbol, we first collect the symbol name
4515 indexes that matched in a temporary vector and ignore
4516 duplicates. */
4517 std::vector<offset_type> matches;
4518
4519 struct name_and_matcher
4520 {
4521 symbol_name_matcher_ftype *matcher;
4522 const std::string &name;
4523
4524 bool operator== (const name_and_matcher &other) const
4525 {
4526 return matcher == other.matcher && name == other.name;
4527 }
4528 };
4529
4530 /* A vector holding all the different symbol name matchers, for all
4531 languages. */
4532 std::vector<name_and_matcher> matchers;
4533
4534 for (int i = 0; i < nr_languages; i++)
4535 {
4536 enum language lang_e = (enum language) i;
4537
4538 const language_defn *lang = language_def (lang_e);
4539 symbol_name_matcher_ftype *name_matcher
4540 = get_symbol_name_matcher (lang, lookup_name_without_params);
4541
4542 name_and_matcher key {
4543 name_matcher,
4544 lookup_name_without_params.language_lookup_name (lang_e)
4545 };
4546
4547 /* Don't insert the same comparison routine more than once.
4548 Note that we do this linear walk. This is not a problem in
4549 practice because the number of supported languages is
4550 low. */
4551 if (std::find (matchers.begin (), matchers.end (), key)
4552 != matchers.end ())
4553 continue;
4554 matchers.push_back (std::move (key));
4555
4556 auto bounds
4557 = index.find_name_components_bounds (lookup_name_without_params,
4558 lang_e);
4559
4560 /* Now for each symbol name in range, check to see if we have a name
4561 match, and if so, call the MATCH_CALLBACK callback. */
4562
4563 for (; bounds.first != bounds.second; ++bounds.first)
4564 {
4565 const char *qualified = index.symbol_name_at (bounds.first->idx);
4566
4567 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4568 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4569 continue;
4570
4571 matches.push_back (bounds.first->idx);
4572 }
4573 }
4574
4575 std::sort (matches.begin (), matches.end ());
4576
4577 /* Finally call the callback, once per match. */
4578 ULONGEST prev = -1;
4579 for (offset_type idx : matches)
4580 {
4581 if (prev != idx)
4582 {
4583 if (!match_callback (idx))
4584 break;
4585 prev = idx;
4586 }
4587 }
4588
4589 /* Above we use a type wider than idx's for 'prev', since 0 and
4590 (offset_type)-1 are both possible values. */
4591 static_assert (sizeof (prev) > sizeof (offset_type), "");
4592 }
4593
4594 #if GDB_SELF_TEST
4595
4596 namespace selftests { namespace dw2_expand_symtabs_matching {
4597
4598 /* A mock .gdb_index/.debug_names-like name index table, enough to
4599 exercise dw2_expand_symtabs_matching_symbol, which works with the
4600 mapped_index_base interface. Builds an index from the symbol list
4601 passed as parameter to the constructor. */
4602 class mock_mapped_index : public mapped_index_base
4603 {
4604 public:
4605 mock_mapped_index (gdb::array_view<const char *> symbols)
4606 : m_symbol_table (symbols)
4607 {}
4608
4609 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4610
4611 /* Return the number of names in the symbol table. */
4612 size_t symbol_name_count () const override
4613 {
4614 return m_symbol_table.size ();
4615 }
4616
4617 /* Get the name of the symbol at IDX in the symbol table. */
4618 const char *symbol_name_at (offset_type idx) const override
4619 {
4620 return m_symbol_table[idx];
4621 }
4622
4623 private:
4624 gdb::array_view<const char *> m_symbol_table;
4625 };
4626
4627 /* Convenience function that converts a NULL pointer to a "<null>"
4628 string, to pass to print routines. */
4629
4630 static const char *
4631 string_or_null (const char *str)
4632 {
4633 return str != NULL ? str : "<null>";
4634 }
4635
4636 /* Check if a lookup_name_info built from
4637 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4638 index. EXPECTED_LIST is the list of expected matches, in expected
4639 matching order. If no match expected, then an empty list is
4640 specified. Returns true on success. On failure prints a warning
4641 indicating the file:line that failed, and returns false. */
4642
4643 static bool
4644 check_match (const char *file, int line,
4645 mock_mapped_index &mock_index,
4646 const char *name, symbol_name_match_type match_type,
4647 bool completion_mode,
4648 std::initializer_list<const char *> expected_list)
4649 {
4650 lookup_name_info lookup_name (name, match_type, completion_mode);
4651
4652 bool matched = true;
4653
4654 auto mismatch = [&] (const char *expected_str,
4655 const char *got)
4656 {
4657 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4658 "expected=\"%s\", got=\"%s\"\n"),
4659 file, line,
4660 (match_type == symbol_name_match_type::FULL
4661 ? "FULL" : "WILD"),
4662 name, string_or_null (expected_str), string_or_null (got));
4663 matched = false;
4664 };
4665
4666 auto expected_it = expected_list.begin ();
4667 auto expected_end = expected_list.end ();
4668
4669 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4670 NULL, ALL_DOMAIN,
4671 [&] (offset_type idx)
4672 {
4673 const char *matched_name = mock_index.symbol_name_at (idx);
4674 const char *expected_str
4675 = expected_it == expected_end ? NULL : *expected_it++;
4676
4677 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4678 mismatch (expected_str, matched_name);
4679 return true;
4680 });
4681
4682 const char *expected_str
4683 = expected_it == expected_end ? NULL : *expected_it++;
4684 if (expected_str != NULL)
4685 mismatch (expected_str, NULL);
4686
4687 return matched;
4688 }
4689
4690 /* The symbols added to the mock mapped_index for testing (in
4691 canonical form). */
4692 static const char *test_symbols[] = {
4693 "function",
4694 "std::bar",
4695 "std::zfunction",
4696 "std::zfunction2",
4697 "w1::w2",
4698 "ns::foo<char*>",
4699 "ns::foo<int>",
4700 "ns::foo<long>",
4701 "ns2::tmpl<int>::foo2",
4702 "(anonymous namespace)::A::B::C",
4703
4704 /* These are used to check that the increment-last-char in the
4705 matching algorithm for completion doesn't match "t1_fund" when
4706 completing "t1_func". */
4707 "t1_func",
4708 "t1_func1",
4709 "t1_fund",
4710 "t1_fund1",
4711
4712 /* A UTF-8 name with multi-byte sequences to make sure that
4713 cp-name-parser understands this as a single identifier ("função"
4714 is "function" in PT). */
4715 u8"u8função",
4716
4717 /* \377 (0xff) is Latin1 'ÿ'. */
4718 "yfunc\377",
4719
4720 /* \377 (0xff) is Latin1 'ÿ'. */
4721 "\377",
4722 "\377\377123",
4723
4724 /* A name with all sorts of complications. Starts with "z" to make
4725 it easier for the completion tests below. */
4726 #define Z_SYM_NAME \
4727 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4728 "::tuple<(anonymous namespace)::ui*, " \
4729 "std::default_delete<(anonymous namespace)::ui>, void>"
4730
4731 Z_SYM_NAME
4732 };
4733
4734 /* Returns true if the mapped_index_base::find_name_component_bounds
4735 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4736 in completion mode. */
4737
4738 static bool
4739 check_find_bounds_finds (mapped_index_base &index,
4740 const char *search_name,
4741 gdb::array_view<const char *> expected_syms)
4742 {
4743 lookup_name_info lookup_name (search_name,
4744 symbol_name_match_type::FULL, true);
4745
4746 auto bounds = index.find_name_components_bounds (lookup_name,
4747 language_cplus);
4748
4749 size_t distance = std::distance (bounds.first, bounds.second);
4750 if (distance != expected_syms.size ())
4751 return false;
4752
4753 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4754 {
4755 auto nc_elem = bounds.first + exp_elem;
4756 const char *qualified = index.symbol_name_at (nc_elem->idx);
4757 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4758 return false;
4759 }
4760
4761 return true;
4762 }
4763
4764 /* Test the lower-level mapped_index::find_name_component_bounds
4765 method. */
4766
4767 static void
4768 test_mapped_index_find_name_component_bounds ()
4769 {
4770 mock_mapped_index mock_index (test_symbols);
4771
4772 mock_index.build_name_components ();
4773
4774 /* Test the lower-level mapped_index::find_name_component_bounds
4775 method in completion mode. */
4776 {
4777 static const char *expected_syms[] = {
4778 "t1_func",
4779 "t1_func1",
4780 };
4781
4782 SELF_CHECK (check_find_bounds_finds (mock_index,
4783 "t1_func", expected_syms));
4784 }
4785
4786 /* Check that the increment-last-char in the name matching algorithm
4787 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4788 {
4789 static const char *expected_syms1[] = {
4790 "\377",
4791 "\377\377123",
4792 };
4793 SELF_CHECK (check_find_bounds_finds (mock_index,
4794 "\377", expected_syms1));
4795
4796 static const char *expected_syms2[] = {
4797 "\377\377123",
4798 };
4799 SELF_CHECK (check_find_bounds_finds (mock_index,
4800 "\377\377", expected_syms2));
4801 }
4802 }
4803
4804 /* Test dw2_expand_symtabs_matching_symbol. */
4805
4806 static void
4807 test_dw2_expand_symtabs_matching_symbol ()
4808 {
4809 mock_mapped_index mock_index (test_symbols);
4810
4811 /* We let all tests run until the end even if some fails, for debug
4812 convenience. */
4813 bool any_mismatch = false;
4814
4815 /* Create the expected symbols list (an initializer_list). Needed
4816 because lists have commas, and we need to pass them to CHECK,
4817 which is a macro. */
4818 #define EXPECT(...) { __VA_ARGS__ }
4819
4820 /* Wrapper for check_match that passes down the current
4821 __FILE__/__LINE__. */
4822 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4823 any_mismatch |= !check_match (__FILE__, __LINE__, \
4824 mock_index, \
4825 NAME, MATCH_TYPE, COMPLETION_MODE, \
4826 EXPECTED_LIST)
4827
4828 /* Identity checks. */
4829 for (const char *sym : test_symbols)
4830 {
4831 /* Should be able to match all existing symbols. */
4832 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4833 EXPECT (sym));
4834
4835 /* Should be able to match all existing symbols with
4836 parameters. */
4837 std::string with_params = std::string (sym) + "(int)";
4838 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4839 EXPECT (sym));
4840
4841 /* Should be able to match all existing symbols with
4842 parameters and qualifiers. */
4843 with_params = std::string (sym) + " ( int ) const";
4844 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4845 EXPECT (sym));
4846
4847 /* This should really find sym, but cp-name-parser.y doesn't
4848 know about lvalue/rvalue qualifiers yet. */
4849 with_params = std::string (sym) + " ( int ) &&";
4850 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4851 {});
4852 }
4853
4854 /* Check that the name matching algorithm for completion doesn't get
4855 confused with Latin1 'ÿ' / 0xff. */
4856 {
4857 static const char str[] = "\377";
4858 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4859 EXPECT ("\377", "\377\377123"));
4860 }
4861
4862 /* Check that the increment-last-char in the matching algorithm for
4863 completion doesn't match "t1_fund" when completing "t1_func". */
4864 {
4865 static const char str[] = "t1_func";
4866 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4867 EXPECT ("t1_func", "t1_func1"));
4868 }
4869
4870 /* Check that completion mode works at each prefix of the expected
4871 symbol name. */
4872 {
4873 static const char str[] = "function(int)";
4874 size_t len = strlen (str);
4875 std::string lookup;
4876
4877 for (size_t i = 1; i < len; i++)
4878 {
4879 lookup.assign (str, i);
4880 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4881 EXPECT ("function"));
4882 }
4883 }
4884
4885 /* While "w" is a prefix of both components, the match function
4886 should still only be called once. */
4887 {
4888 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4889 EXPECT ("w1::w2"));
4890 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4891 EXPECT ("w1::w2"));
4892 }
4893
4894 /* Same, with a "complicated" symbol. */
4895 {
4896 static const char str[] = Z_SYM_NAME;
4897 size_t len = strlen (str);
4898 std::string lookup;
4899
4900 for (size_t i = 1; i < len; i++)
4901 {
4902 lookup.assign (str, i);
4903 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4904 EXPECT (Z_SYM_NAME));
4905 }
4906 }
4907
4908 /* In FULL mode, an incomplete symbol doesn't match. */
4909 {
4910 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4911 {});
4912 }
4913
4914 /* A complete symbol with parameters matches any overload, since the
4915 index has no overload info. */
4916 {
4917 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4918 EXPECT ("std::zfunction", "std::zfunction2"));
4919 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4920 EXPECT ("std::zfunction", "std::zfunction2"));
4921 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4922 EXPECT ("std::zfunction", "std::zfunction2"));
4923 }
4924
4925 /* Check that whitespace is ignored appropriately. A symbol with a
4926 template argument list. */
4927 {
4928 static const char expected[] = "ns::foo<int>";
4929 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4930 EXPECT (expected));
4931 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4932 EXPECT (expected));
4933 }
4934
4935 /* Check that whitespace is ignored appropriately. A symbol with a
4936 template argument list that includes a pointer. */
4937 {
4938 static const char expected[] = "ns::foo<char*>";
4939 /* Try both completion and non-completion modes. */
4940 static const bool completion_mode[2] = {false, true};
4941 for (size_t i = 0; i < 2; i++)
4942 {
4943 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4944 completion_mode[i], EXPECT (expected));
4945 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4946 completion_mode[i], EXPECT (expected));
4947
4948 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4949 completion_mode[i], EXPECT (expected));
4950 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4951 completion_mode[i], EXPECT (expected));
4952 }
4953 }
4954
4955 {
4956 /* Check method qualifiers are ignored. */
4957 static const char expected[] = "ns::foo<char*>";
4958 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4959 symbol_name_match_type::FULL, true, EXPECT (expected));
4960 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4961 symbol_name_match_type::FULL, true, EXPECT (expected));
4962 CHECK_MATCH ("foo < char * > ( int ) const",
4963 symbol_name_match_type::WILD, true, EXPECT (expected));
4964 CHECK_MATCH ("foo < char * > ( int ) &&",
4965 symbol_name_match_type::WILD, true, EXPECT (expected));
4966 }
4967
4968 /* Test lookup names that don't match anything. */
4969 {
4970 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4971 {});
4972
4973 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4974 {});
4975 }
4976
4977 /* Some wild matching tests, exercising "(anonymous namespace)",
4978 which should not be confused with a parameter list. */
4979 {
4980 static const char *syms[] = {
4981 "A::B::C",
4982 "B::C",
4983 "C",
4984 "A :: B :: C ( int )",
4985 "B :: C ( int )",
4986 "C ( int )",
4987 };
4988
4989 for (const char *s : syms)
4990 {
4991 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4992 EXPECT ("(anonymous namespace)::A::B::C"));
4993 }
4994 }
4995
4996 {
4997 static const char expected[] = "ns2::tmpl<int>::foo2";
4998 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4999 EXPECT (expected));
5000 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5001 EXPECT (expected));
5002 }
5003
5004 SELF_CHECK (!any_mismatch);
5005
5006 #undef EXPECT
5007 #undef CHECK_MATCH
5008 }
5009
5010 static void
5011 run_test ()
5012 {
5013 test_mapped_index_find_name_component_bounds ();
5014 test_dw2_expand_symtabs_matching_symbol ();
5015 }
5016
5017 }} // namespace selftests::dw2_expand_symtabs_matching
5018
5019 #endif /* GDB_SELF_TEST */
5020
5021 /* If FILE_MATCHER is NULL or if PER_CU has
5022 dwarf2_per_cu_quick_data::MARK set (see
5023 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5024 EXPANSION_NOTIFY on it. */
5025
5026 static void
5027 dw2_expand_symtabs_matching_one
5028 (struct dwarf2_per_cu_data *per_cu,
5029 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5030 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5031 {
5032 if (file_matcher == NULL || per_cu->v.quick->mark)
5033 {
5034 bool symtab_was_null
5035 = (per_cu->v.quick->compunit_symtab == NULL);
5036
5037 dw2_instantiate_symtab (per_cu, false);
5038
5039 if (expansion_notify != NULL
5040 && symtab_was_null
5041 && per_cu->v.quick->compunit_symtab != NULL)
5042 expansion_notify (per_cu->v.quick->compunit_symtab);
5043 }
5044 }
5045
5046 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5047 matched, to expand corresponding CUs that were marked. IDX is the
5048 index of the symbol name that matched. */
5049
5050 static void
5051 dw2_expand_marked_cus
5052 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5053 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5054 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5055 search_domain kind)
5056 {
5057 offset_type *vec, vec_len, vec_idx;
5058 bool global_seen = false;
5059 mapped_index &index = *dwarf2_per_objfile->index_table;
5060
5061 vec = (offset_type *) (index.constant_pool
5062 + MAYBE_SWAP (index.symbol_table[idx].vec));
5063 vec_len = MAYBE_SWAP (vec[0]);
5064 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5065 {
5066 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5067 /* This value is only valid for index versions >= 7. */
5068 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5069 gdb_index_symbol_kind symbol_kind =
5070 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5071 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5072 /* Only check the symbol attributes if they're present.
5073 Indices prior to version 7 don't record them,
5074 and indices >= 7 may elide them for certain symbols
5075 (gold does this). */
5076 int attrs_valid =
5077 (index.version >= 7
5078 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5079
5080 /* Work around gold/15646. */
5081 if (attrs_valid)
5082 {
5083 if (!is_static && global_seen)
5084 continue;
5085 if (!is_static)
5086 global_seen = true;
5087 }
5088
5089 /* Only check the symbol's kind if it has one. */
5090 if (attrs_valid)
5091 {
5092 switch (kind)
5093 {
5094 case VARIABLES_DOMAIN:
5095 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5096 continue;
5097 break;
5098 case FUNCTIONS_DOMAIN:
5099 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5100 continue;
5101 break;
5102 case TYPES_DOMAIN:
5103 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5104 continue;
5105 break;
5106 case MODULES_DOMAIN:
5107 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5108 continue;
5109 break;
5110 default:
5111 break;
5112 }
5113 }
5114
5115 /* Don't crash on bad data. */
5116 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5117 + dwarf2_per_objfile->all_type_units.size ()))
5118 {
5119 complaint (_(".gdb_index entry has bad CU index"
5120 " [in module %s]"),
5121 objfile_name (dwarf2_per_objfile->objfile));
5122 continue;
5123 }
5124
5125 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5126 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5127 expansion_notify);
5128 }
5129 }
5130
5131 /* If FILE_MATCHER is non-NULL, set all the
5132 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5133 that match FILE_MATCHER. */
5134
5135 static void
5136 dw_expand_symtabs_matching_file_matcher
5137 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5138 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5139 {
5140 if (file_matcher == NULL)
5141 return;
5142
5143 objfile *const objfile = dwarf2_per_objfile->objfile;
5144
5145 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5146 htab_eq_pointer,
5147 NULL, xcalloc, xfree));
5148 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5149 htab_eq_pointer,
5150 NULL, xcalloc, xfree));
5151
5152 /* The rule is CUs specify all the files, including those used by
5153 any TU, so there's no need to scan TUs here. */
5154
5155 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5156 {
5157 QUIT;
5158
5159 per_cu->v.quick->mark = 0;
5160
5161 /* We only need to look at symtabs not already expanded. */
5162 if (per_cu->v.quick->compunit_symtab)
5163 continue;
5164
5165 quick_file_names *file_data = dw2_get_file_names (per_cu);
5166 if (file_data == NULL)
5167 continue;
5168
5169 if (htab_find (visited_not_found.get (), file_data) != NULL)
5170 continue;
5171 else if (htab_find (visited_found.get (), file_data) != NULL)
5172 {
5173 per_cu->v.quick->mark = 1;
5174 continue;
5175 }
5176
5177 for (int j = 0; j < file_data->num_file_names; ++j)
5178 {
5179 const char *this_real_name;
5180
5181 if (file_matcher (file_data->file_names[j], false))
5182 {
5183 per_cu->v.quick->mark = 1;
5184 break;
5185 }
5186
5187 /* Before we invoke realpath, which can get expensive when many
5188 files are involved, do a quick comparison of the basenames. */
5189 if (!basenames_may_differ
5190 && !file_matcher (lbasename (file_data->file_names[j]),
5191 true))
5192 continue;
5193
5194 this_real_name = dw2_get_real_path (objfile, file_data, j);
5195 if (file_matcher (this_real_name, false))
5196 {
5197 per_cu->v.quick->mark = 1;
5198 break;
5199 }
5200 }
5201
5202 void **slot = htab_find_slot (per_cu->v.quick->mark
5203 ? visited_found.get ()
5204 : visited_not_found.get (),
5205 file_data, INSERT);
5206 *slot = file_data;
5207 }
5208 }
5209
5210 static void
5211 dw2_expand_symtabs_matching
5212 (struct objfile *objfile,
5213 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5214 const lookup_name_info &lookup_name,
5215 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5216 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5217 enum search_domain kind)
5218 {
5219 struct dwarf2_per_objfile *dwarf2_per_objfile
5220 = get_dwarf2_per_objfile (objfile);
5221
5222 /* index_table is NULL if OBJF_READNOW. */
5223 if (!dwarf2_per_objfile->index_table)
5224 return;
5225
5226 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5227
5228 mapped_index &index = *dwarf2_per_objfile->index_table;
5229
5230 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5231 symbol_matcher,
5232 kind, [&] (offset_type idx)
5233 {
5234 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5235 expansion_notify, kind);
5236 return true;
5237 });
5238 }
5239
5240 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5241 symtab. */
5242
5243 static struct compunit_symtab *
5244 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5245 CORE_ADDR pc)
5246 {
5247 int i;
5248
5249 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5250 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5251 return cust;
5252
5253 if (cust->includes == NULL)
5254 return NULL;
5255
5256 for (i = 0; cust->includes[i]; ++i)
5257 {
5258 struct compunit_symtab *s = cust->includes[i];
5259
5260 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5261 if (s != NULL)
5262 return s;
5263 }
5264
5265 return NULL;
5266 }
5267
5268 static struct compunit_symtab *
5269 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5270 struct bound_minimal_symbol msymbol,
5271 CORE_ADDR pc,
5272 struct obj_section *section,
5273 int warn_if_readin)
5274 {
5275 struct dwarf2_per_cu_data *data;
5276 struct compunit_symtab *result;
5277
5278 if (!objfile->partial_symtabs->psymtabs_addrmap)
5279 return NULL;
5280
5281 CORE_ADDR baseaddr = objfile->text_section_offset ();
5282 data = (struct dwarf2_per_cu_data *) addrmap_find
5283 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5284 if (!data)
5285 return NULL;
5286
5287 if (warn_if_readin && data->v.quick->compunit_symtab)
5288 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5289 paddress (get_objfile_arch (objfile), pc));
5290
5291 result
5292 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5293 false),
5294 pc);
5295 gdb_assert (result != NULL);
5296 return result;
5297 }
5298
5299 static void
5300 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5301 void *data, int need_fullname)
5302 {
5303 struct dwarf2_per_objfile *dwarf2_per_objfile
5304 = get_dwarf2_per_objfile (objfile);
5305
5306 if (!dwarf2_per_objfile->filenames_cache)
5307 {
5308 dwarf2_per_objfile->filenames_cache.emplace ();
5309
5310 htab_up visited (htab_create_alloc (10,
5311 htab_hash_pointer, htab_eq_pointer,
5312 NULL, xcalloc, xfree));
5313
5314 /* The rule is CUs specify all the files, including those used
5315 by any TU, so there's no need to scan TUs here. We can
5316 ignore file names coming from already-expanded CUs. */
5317
5318 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5319 {
5320 if (per_cu->v.quick->compunit_symtab)
5321 {
5322 void **slot = htab_find_slot (visited.get (),
5323 per_cu->v.quick->file_names,
5324 INSERT);
5325
5326 *slot = per_cu->v.quick->file_names;
5327 }
5328 }
5329
5330 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5331 {
5332 /* We only need to look at symtabs not already expanded. */
5333 if (per_cu->v.quick->compunit_symtab)
5334 continue;
5335
5336 quick_file_names *file_data = dw2_get_file_names (per_cu);
5337 if (file_data == NULL)
5338 continue;
5339
5340 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5341 if (*slot)
5342 {
5343 /* Already visited. */
5344 continue;
5345 }
5346 *slot = file_data;
5347
5348 for (int j = 0; j < file_data->num_file_names; ++j)
5349 {
5350 const char *filename = file_data->file_names[j];
5351 dwarf2_per_objfile->filenames_cache->seen (filename);
5352 }
5353 }
5354 }
5355
5356 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5357 {
5358 gdb::unique_xmalloc_ptr<char> this_real_name;
5359
5360 if (need_fullname)
5361 this_real_name = gdb_realpath (filename);
5362 (*fun) (filename, this_real_name.get (), data);
5363 });
5364 }
5365
5366 static int
5367 dw2_has_symbols (struct objfile *objfile)
5368 {
5369 return 1;
5370 }
5371
5372 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5373 {
5374 dw2_has_symbols,
5375 dw2_find_last_source_symtab,
5376 dw2_forget_cached_source_info,
5377 dw2_map_symtabs_matching_filename,
5378 dw2_lookup_symbol,
5379 dw2_print_stats,
5380 dw2_dump,
5381 dw2_expand_symtabs_for_function,
5382 dw2_expand_all_symtabs,
5383 dw2_expand_symtabs_with_fullname,
5384 dw2_map_matching_symbols,
5385 dw2_expand_symtabs_matching,
5386 dw2_find_pc_sect_compunit_symtab,
5387 NULL,
5388 dw2_map_symbol_filenames
5389 };
5390
5391 /* DWARF-5 debug_names reader. */
5392
5393 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5394 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5395
5396 /* A helper function that reads the .debug_names section in SECTION
5397 and fills in MAP. FILENAME is the name of the file containing the
5398 section; it is used for error reporting.
5399
5400 Returns true if all went well, false otherwise. */
5401
5402 static bool
5403 read_debug_names_from_section (struct objfile *objfile,
5404 const char *filename,
5405 struct dwarf2_section_info *section,
5406 mapped_debug_names &map)
5407 {
5408 if (dwarf2_section_empty_p (section))
5409 return false;
5410
5411 /* Older elfutils strip versions could keep the section in the main
5412 executable while splitting it for the separate debug info file. */
5413 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5414 return false;
5415
5416 dwarf2_read_section (objfile, section);
5417
5418 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5419
5420 const gdb_byte *addr = section->buffer;
5421
5422 bfd *const abfd = get_section_bfd_owner (section);
5423
5424 unsigned int bytes_read;
5425 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5426 addr += bytes_read;
5427
5428 map.dwarf5_is_dwarf64 = bytes_read != 4;
5429 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5430 if (bytes_read + length != section->size)
5431 {
5432 /* There may be multiple per-CU indices. */
5433 warning (_("Section .debug_names in %s length %s does not match "
5434 "section length %s, ignoring .debug_names."),
5435 filename, plongest (bytes_read + length),
5436 pulongest (section->size));
5437 return false;
5438 }
5439
5440 /* The version number. */
5441 uint16_t version = read_2_bytes (abfd, addr);
5442 addr += 2;
5443 if (version != 5)
5444 {
5445 warning (_("Section .debug_names in %s has unsupported version %d, "
5446 "ignoring .debug_names."),
5447 filename, version);
5448 return false;
5449 }
5450
5451 /* Padding. */
5452 uint16_t padding = read_2_bytes (abfd, addr);
5453 addr += 2;
5454 if (padding != 0)
5455 {
5456 warning (_("Section .debug_names in %s has unsupported padding %d, "
5457 "ignoring .debug_names."),
5458 filename, padding);
5459 return false;
5460 }
5461
5462 /* comp_unit_count - The number of CUs in the CU list. */
5463 map.cu_count = read_4_bytes (abfd, addr);
5464 addr += 4;
5465
5466 /* local_type_unit_count - The number of TUs in the local TU
5467 list. */
5468 map.tu_count = read_4_bytes (abfd, addr);
5469 addr += 4;
5470
5471 /* foreign_type_unit_count - The number of TUs in the foreign TU
5472 list. */
5473 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5474 addr += 4;
5475 if (foreign_tu_count != 0)
5476 {
5477 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5478 "ignoring .debug_names."),
5479 filename, static_cast<unsigned long> (foreign_tu_count));
5480 return false;
5481 }
5482
5483 /* bucket_count - The number of hash buckets in the hash lookup
5484 table. */
5485 map.bucket_count = read_4_bytes (abfd, addr);
5486 addr += 4;
5487
5488 /* name_count - The number of unique names in the index. */
5489 map.name_count = read_4_bytes (abfd, addr);
5490 addr += 4;
5491
5492 /* abbrev_table_size - The size in bytes of the abbreviations
5493 table. */
5494 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5495 addr += 4;
5496
5497 /* augmentation_string_size - The size in bytes of the augmentation
5498 string. This value is rounded up to a multiple of 4. */
5499 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5500 addr += 4;
5501 map.augmentation_is_gdb = ((augmentation_string_size
5502 == sizeof (dwarf5_augmentation))
5503 && memcmp (addr, dwarf5_augmentation,
5504 sizeof (dwarf5_augmentation)) == 0);
5505 augmentation_string_size += (-augmentation_string_size) & 3;
5506 addr += augmentation_string_size;
5507
5508 /* List of CUs */
5509 map.cu_table_reordered = addr;
5510 addr += map.cu_count * map.offset_size;
5511
5512 /* List of Local TUs */
5513 map.tu_table_reordered = addr;
5514 addr += map.tu_count * map.offset_size;
5515
5516 /* Hash Lookup Table */
5517 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5518 addr += map.bucket_count * 4;
5519 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5520 addr += map.name_count * 4;
5521
5522 /* Name Table */
5523 map.name_table_string_offs_reordered = addr;
5524 addr += map.name_count * map.offset_size;
5525 map.name_table_entry_offs_reordered = addr;
5526 addr += map.name_count * map.offset_size;
5527
5528 const gdb_byte *abbrev_table_start = addr;
5529 for (;;)
5530 {
5531 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5532 addr += bytes_read;
5533 if (index_num == 0)
5534 break;
5535
5536 const auto insertpair
5537 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5538 if (!insertpair.second)
5539 {
5540 warning (_("Section .debug_names in %s has duplicate index %s, "
5541 "ignoring .debug_names."),
5542 filename, pulongest (index_num));
5543 return false;
5544 }
5545 mapped_debug_names::index_val &indexval = insertpair.first->second;
5546 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5547 addr += bytes_read;
5548
5549 for (;;)
5550 {
5551 mapped_debug_names::index_val::attr attr;
5552 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5553 addr += bytes_read;
5554 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5555 addr += bytes_read;
5556 if (attr.form == DW_FORM_implicit_const)
5557 {
5558 attr.implicit_const = read_signed_leb128 (abfd, addr,
5559 &bytes_read);
5560 addr += bytes_read;
5561 }
5562 if (attr.dw_idx == 0 && attr.form == 0)
5563 break;
5564 indexval.attr_vec.push_back (std::move (attr));
5565 }
5566 }
5567 if (addr != abbrev_table_start + abbrev_table_size)
5568 {
5569 warning (_("Section .debug_names in %s has abbreviation_table "
5570 "of size %s vs. written as %u, ignoring .debug_names."),
5571 filename, plongest (addr - abbrev_table_start),
5572 abbrev_table_size);
5573 return false;
5574 }
5575 map.entry_pool = addr;
5576
5577 return true;
5578 }
5579
5580 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5581 list. */
5582
5583 static void
5584 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5585 const mapped_debug_names &map,
5586 dwarf2_section_info &section,
5587 bool is_dwz)
5588 {
5589 sect_offset sect_off_prev;
5590 for (uint32_t i = 0; i <= map.cu_count; ++i)
5591 {
5592 sect_offset sect_off_next;
5593 if (i < map.cu_count)
5594 {
5595 sect_off_next
5596 = (sect_offset) (extract_unsigned_integer
5597 (map.cu_table_reordered + i * map.offset_size,
5598 map.offset_size,
5599 map.dwarf5_byte_order));
5600 }
5601 else
5602 sect_off_next = (sect_offset) section.size;
5603 if (i >= 1)
5604 {
5605 const ULONGEST length = sect_off_next - sect_off_prev;
5606 dwarf2_per_cu_data *per_cu
5607 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5608 sect_off_prev, length);
5609 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5610 }
5611 sect_off_prev = sect_off_next;
5612 }
5613 }
5614
5615 /* Read the CU list from the mapped index, and use it to create all
5616 the CU objects for this dwarf2_per_objfile. */
5617
5618 static void
5619 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5620 const mapped_debug_names &map,
5621 const mapped_debug_names &dwz_map)
5622 {
5623 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5624 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5625
5626 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5627 dwarf2_per_objfile->info,
5628 false /* is_dwz */);
5629
5630 if (dwz_map.cu_count == 0)
5631 return;
5632
5633 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5634 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5635 true /* is_dwz */);
5636 }
5637
5638 /* Read .debug_names. If everything went ok, initialize the "quick"
5639 elements of all the CUs and return true. Otherwise, return false. */
5640
5641 static bool
5642 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5643 {
5644 std::unique_ptr<mapped_debug_names> map
5645 (new mapped_debug_names (dwarf2_per_objfile));
5646 mapped_debug_names dwz_map (dwarf2_per_objfile);
5647 struct objfile *objfile = dwarf2_per_objfile->objfile;
5648
5649 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5650 &dwarf2_per_objfile->debug_names,
5651 *map))
5652 return false;
5653
5654 /* Don't use the index if it's empty. */
5655 if (map->name_count == 0)
5656 return false;
5657
5658 /* If there is a .dwz file, read it so we can get its CU list as
5659 well. */
5660 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5661 if (dwz != NULL)
5662 {
5663 if (!read_debug_names_from_section (objfile,
5664 bfd_get_filename (dwz->dwz_bfd.get ()),
5665 &dwz->debug_names, dwz_map))
5666 {
5667 warning (_("could not read '.debug_names' section from %s; skipping"),
5668 bfd_get_filename (dwz->dwz_bfd.get ()));
5669 return false;
5670 }
5671 }
5672
5673 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5674
5675 if (map->tu_count != 0)
5676 {
5677 /* We can only handle a single .debug_types when we have an
5678 index. */
5679 if (dwarf2_per_objfile->types.size () != 1)
5680 return false;
5681
5682 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5683
5684 create_signatured_type_table_from_debug_names
5685 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5686 }
5687
5688 create_addrmap_from_aranges (dwarf2_per_objfile,
5689 &dwarf2_per_objfile->debug_aranges);
5690
5691 dwarf2_per_objfile->debug_names_table = std::move (map);
5692 dwarf2_per_objfile->using_index = 1;
5693 dwarf2_per_objfile->quick_file_names_table =
5694 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5695
5696 return true;
5697 }
5698
5699 /* Type used to manage iterating over all CUs looking for a symbol for
5700 .debug_names. */
5701
5702 class dw2_debug_names_iterator
5703 {
5704 public:
5705 dw2_debug_names_iterator (const mapped_debug_names &map,
5706 gdb::optional<block_enum> block_index,
5707 domain_enum domain,
5708 const char *name)
5709 : m_map (map), m_block_index (block_index), m_domain (domain),
5710 m_addr (find_vec_in_debug_names (map, name))
5711 {}
5712
5713 dw2_debug_names_iterator (const mapped_debug_names &map,
5714 search_domain search, uint32_t namei)
5715 : m_map (map),
5716 m_search (search),
5717 m_addr (find_vec_in_debug_names (map, namei))
5718 {}
5719
5720 dw2_debug_names_iterator (const mapped_debug_names &map,
5721 block_enum block_index, domain_enum domain,
5722 uint32_t namei)
5723 : m_map (map), m_block_index (block_index), m_domain (domain),
5724 m_addr (find_vec_in_debug_names (map, namei))
5725 {}
5726
5727 /* Return the next matching CU or NULL if there are no more. */
5728 dwarf2_per_cu_data *next ();
5729
5730 private:
5731 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5732 const char *name);
5733 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5734 uint32_t namei);
5735
5736 /* The internalized form of .debug_names. */
5737 const mapped_debug_names &m_map;
5738
5739 /* If set, only look for symbols that match that block. Valid values are
5740 GLOBAL_BLOCK and STATIC_BLOCK. */
5741 const gdb::optional<block_enum> m_block_index;
5742
5743 /* The kind of symbol we're looking for. */
5744 const domain_enum m_domain = UNDEF_DOMAIN;
5745 const search_domain m_search = ALL_DOMAIN;
5746
5747 /* The list of CUs from the index entry of the symbol, or NULL if
5748 not found. */
5749 const gdb_byte *m_addr;
5750 };
5751
5752 const char *
5753 mapped_debug_names::namei_to_name (uint32_t namei) const
5754 {
5755 const ULONGEST namei_string_offs
5756 = extract_unsigned_integer ((name_table_string_offs_reordered
5757 + namei * offset_size),
5758 offset_size,
5759 dwarf5_byte_order);
5760 return read_indirect_string_at_offset
5761 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5762 }
5763
5764 /* Find a slot in .debug_names for the object named NAME. If NAME is
5765 found, return pointer to its pool data. If NAME cannot be found,
5766 return NULL. */
5767
5768 const gdb_byte *
5769 dw2_debug_names_iterator::find_vec_in_debug_names
5770 (const mapped_debug_names &map, const char *name)
5771 {
5772 int (*cmp) (const char *, const char *);
5773
5774 gdb::unique_xmalloc_ptr<char> without_params;
5775 if (current_language->la_language == language_cplus
5776 || current_language->la_language == language_fortran
5777 || current_language->la_language == language_d)
5778 {
5779 /* NAME is already canonical. Drop any qualifiers as
5780 .debug_names does not contain any. */
5781
5782 if (strchr (name, '(') != NULL)
5783 {
5784 without_params = cp_remove_params (name);
5785 if (without_params != NULL)
5786 name = without_params.get ();
5787 }
5788 }
5789
5790 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5791
5792 const uint32_t full_hash = dwarf5_djb_hash (name);
5793 uint32_t namei
5794 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5795 (map.bucket_table_reordered
5796 + (full_hash % map.bucket_count)), 4,
5797 map.dwarf5_byte_order);
5798 if (namei == 0)
5799 return NULL;
5800 --namei;
5801 if (namei >= map.name_count)
5802 {
5803 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5804 "[in module %s]"),
5805 namei, map.name_count,
5806 objfile_name (map.dwarf2_per_objfile->objfile));
5807 return NULL;
5808 }
5809
5810 for (;;)
5811 {
5812 const uint32_t namei_full_hash
5813 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5814 (map.hash_table_reordered + namei), 4,
5815 map.dwarf5_byte_order);
5816 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5817 return NULL;
5818
5819 if (full_hash == namei_full_hash)
5820 {
5821 const char *const namei_string = map.namei_to_name (namei);
5822
5823 #if 0 /* An expensive sanity check. */
5824 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5825 {
5826 complaint (_("Wrong .debug_names hash for string at index %u "
5827 "[in module %s]"),
5828 namei, objfile_name (dwarf2_per_objfile->objfile));
5829 return NULL;
5830 }
5831 #endif
5832
5833 if (cmp (namei_string, name) == 0)
5834 {
5835 const ULONGEST namei_entry_offs
5836 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5837 + namei * map.offset_size),
5838 map.offset_size, map.dwarf5_byte_order);
5839 return map.entry_pool + namei_entry_offs;
5840 }
5841 }
5842
5843 ++namei;
5844 if (namei >= map.name_count)
5845 return NULL;
5846 }
5847 }
5848
5849 const gdb_byte *
5850 dw2_debug_names_iterator::find_vec_in_debug_names
5851 (const mapped_debug_names &map, uint32_t namei)
5852 {
5853 if (namei >= map.name_count)
5854 {
5855 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5856 "[in module %s]"),
5857 namei, map.name_count,
5858 objfile_name (map.dwarf2_per_objfile->objfile));
5859 return NULL;
5860 }
5861
5862 const ULONGEST namei_entry_offs
5863 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5864 + namei * map.offset_size),
5865 map.offset_size, map.dwarf5_byte_order);
5866 return map.entry_pool + namei_entry_offs;
5867 }
5868
5869 /* See dw2_debug_names_iterator. */
5870
5871 dwarf2_per_cu_data *
5872 dw2_debug_names_iterator::next ()
5873 {
5874 if (m_addr == NULL)
5875 return NULL;
5876
5877 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5878 struct objfile *objfile = dwarf2_per_objfile->objfile;
5879 bfd *const abfd = objfile->obfd;
5880
5881 again:
5882
5883 unsigned int bytes_read;
5884 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5885 m_addr += bytes_read;
5886 if (abbrev == 0)
5887 return NULL;
5888
5889 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5890 if (indexval_it == m_map.abbrev_map.cend ())
5891 {
5892 complaint (_("Wrong .debug_names undefined abbrev code %s "
5893 "[in module %s]"),
5894 pulongest (abbrev), objfile_name (objfile));
5895 return NULL;
5896 }
5897 const mapped_debug_names::index_val &indexval = indexval_it->second;
5898 enum class symbol_linkage {
5899 unknown,
5900 static_,
5901 extern_,
5902 } symbol_linkage_ = symbol_linkage::unknown;
5903 dwarf2_per_cu_data *per_cu = NULL;
5904 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5905 {
5906 ULONGEST ull;
5907 switch (attr.form)
5908 {
5909 case DW_FORM_implicit_const:
5910 ull = attr.implicit_const;
5911 break;
5912 case DW_FORM_flag_present:
5913 ull = 1;
5914 break;
5915 case DW_FORM_udata:
5916 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5917 m_addr += bytes_read;
5918 break;
5919 default:
5920 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5921 dwarf_form_name (attr.form),
5922 objfile_name (objfile));
5923 return NULL;
5924 }
5925 switch (attr.dw_idx)
5926 {
5927 case DW_IDX_compile_unit:
5928 /* Don't crash on bad data. */
5929 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5930 {
5931 complaint (_(".debug_names entry has bad CU index %s"
5932 " [in module %s]"),
5933 pulongest (ull),
5934 objfile_name (dwarf2_per_objfile->objfile));
5935 continue;
5936 }
5937 per_cu = dwarf2_per_objfile->get_cutu (ull);
5938 break;
5939 case DW_IDX_type_unit:
5940 /* Don't crash on bad data. */
5941 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5942 {
5943 complaint (_(".debug_names entry has bad TU index %s"
5944 " [in module %s]"),
5945 pulongest (ull),
5946 objfile_name (dwarf2_per_objfile->objfile));
5947 continue;
5948 }
5949 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5950 break;
5951 case DW_IDX_GNU_internal:
5952 if (!m_map.augmentation_is_gdb)
5953 break;
5954 symbol_linkage_ = symbol_linkage::static_;
5955 break;
5956 case DW_IDX_GNU_external:
5957 if (!m_map.augmentation_is_gdb)
5958 break;
5959 symbol_linkage_ = symbol_linkage::extern_;
5960 break;
5961 }
5962 }
5963
5964 /* Skip if already read in. */
5965 if (per_cu->v.quick->compunit_symtab)
5966 goto again;
5967
5968 /* Check static vs global. */
5969 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5970 {
5971 const bool want_static = *m_block_index == STATIC_BLOCK;
5972 const bool symbol_is_static =
5973 symbol_linkage_ == symbol_linkage::static_;
5974 if (want_static != symbol_is_static)
5975 goto again;
5976 }
5977
5978 /* Match dw2_symtab_iter_next, symbol_kind
5979 and debug_names::psymbol_tag. */
5980 switch (m_domain)
5981 {
5982 case VAR_DOMAIN:
5983 switch (indexval.dwarf_tag)
5984 {
5985 case DW_TAG_variable:
5986 case DW_TAG_subprogram:
5987 /* Some types are also in VAR_DOMAIN. */
5988 case DW_TAG_typedef:
5989 case DW_TAG_structure_type:
5990 break;
5991 default:
5992 goto again;
5993 }
5994 break;
5995 case STRUCT_DOMAIN:
5996 switch (indexval.dwarf_tag)
5997 {
5998 case DW_TAG_typedef:
5999 case DW_TAG_structure_type:
6000 break;
6001 default:
6002 goto again;
6003 }
6004 break;
6005 case LABEL_DOMAIN:
6006 switch (indexval.dwarf_tag)
6007 {
6008 case 0:
6009 case DW_TAG_variable:
6010 break;
6011 default:
6012 goto again;
6013 }
6014 break;
6015 case MODULE_DOMAIN:
6016 switch (indexval.dwarf_tag)
6017 {
6018 case DW_TAG_module:
6019 break;
6020 default:
6021 goto again;
6022 }
6023 break;
6024 default:
6025 break;
6026 }
6027
6028 /* Match dw2_expand_symtabs_matching, symbol_kind and
6029 debug_names::psymbol_tag. */
6030 switch (m_search)
6031 {
6032 case VARIABLES_DOMAIN:
6033 switch (indexval.dwarf_tag)
6034 {
6035 case DW_TAG_variable:
6036 break;
6037 default:
6038 goto again;
6039 }
6040 break;
6041 case FUNCTIONS_DOMAIN:
6042 switch (indexval.dwarf_tag)
6043 {
6044 case DW_TAG_subprogram:
6045 break;
6046 default:
6047 goto again;
6048 }
6049 break;
6050 case TYPES_DOMAIN:
6051 switch (indexval.dwarf_tag)
6052 {
6053 case DW_TAG_typedef:
6054 case DW_TAG_structure_type:
6055 break;
6056 default:
6057 goto again;
6058 }
6059 break;
6060 case MODULES_DOMAIN:
6061 switch (indexval.dwarf_tag)
6062 {
6063 case DW_TAG_module:
6064 break;
6065 default:
6066 goto again;
6067 }
6068 default:
6069 break;
6070 }
6071
6072 return per_cu;
6073 }
6074
6075 static struct compunit_symtab *
6076 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
6077 const char *name, domain_enum domain)
6078 {
6079 struct dwarf2_per_objfile *dwarf2_per_objfile
6080 = get_dwarf2_per_objfile (objfile);
6081
6082 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6083 if (!mapp)
6084 {
6085 /* index is NULL if OBJF_READNOW. */
6086 return NULL;
6087 }
6088 const auto &map = *mapp;
6089
6090 dw2_debug_names_iterator iter (map, block_index, domain, name);
6091
6092 struct compunit_symtab *stab_best = NULL;
6093 struct dwarf2_per_cu_data *per_cu;
6094 while ((per_cu = iter.next ()) != NULL)
6095 {
6096 struct symbol *sym, *with_opaque = NULL;
6097 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6098 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6099 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6100
6101 sym = block_find_symbol (block, name, domain,
6102 block_find_non_opaque_type_preferred,
6103 &with_opaque);
6104
6105 /* Some caution must be observed with overloaded functions and
6106 methods, since the index will not contain any overload
6107 information (but NAME might contain it). */
6108
6109 if (sym != NULL
6110 && strcmp_iw (sym->search_name (), name) == 0)
6111 return stab;
6112 if (with_opaque != NULL
6113 && strcmp_iw (with_opaque->search_name (), name) == 0)
6114 stab_best = stab;
6115
6116 /* Keep looking through other CUs. */
6117 }
6118
6119 return stab_best;
6120 }
6121
6122 /* This dumps minimal information about .debug_names. It is called
6123 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6124 uses this to verify that .debug_names has been loaded. */
6125
6126 static void
6127 dw2_debug_names_dump (struct objfile *objfile)
6128 {
6129 struct dwarf2_per_objfile *dwarf2_per_objfile
6130 = get_dwarf2_per_objfile (objfile);
6131
6132 gdb_assert (dwarf2_per_objfile->using_index);
6133 printf_filtered (".debug_names:");
6134 if (dwarf2_per_objfile->debug_names_table)
6135 printf_filtered (" exists\n");
6136 else
6137 printf_filtered (" faked for \"readnow\"\n");
6138 printf_filtered ("\n");
6139 }
6140
6141 static void
6142 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6143 const char *func_name)
6144 {
6145 struct dwarf2_per_objfile *dwarf2_per_objfile
6146 = get_dwarf2_per_objfile (objfile);
6147
6148 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6149 if (dwarf2_per_objfile->debug_names_table)
6150 {
6151 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6152
6153 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6154
6155 struct dwarf2_per_cu_data *per_cu;
6156 while ((per_cu = iter.next ()) != NULL)
6157 dw2_instantiate_symtab (per_cu, false);
6158 }
6159 }
6160
6161 static void
6162 dw2_debug_names_map_matching_symbols
6163 (struct objfile *objfile,
6164 const lookup_name_info &name, domain_enum domain,
6165 int global,
6166 gdb::function_view<symbol_found_callback_ftype> callback,
6167 symbol_compare_ftype *ordered_compare)
6168 {
6169 struct dwarf2_per_objfile *dwarf2_per_objfile
6170 = get_dwarf2_per_objfile (objfile);
6171
6172 /* debug_names_table is NULL if OBJF_READNOW. */
6173 if (!dwarf2_per_objfile->debug_names_table)
6174 return;
6175
6176 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6177 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6178
6179 const char *match_name = name.ada ().lookup_name ().c_str ();
6180 auto matcher = [&] (const char *symname)
6181 {
6182 if (ordered_compare == nullptr)
6183 return true;
6184 return ordered_compare (symname, match_name) == 0;
6185 };
6186
6187 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6188 [&] (offset_type namei)
6189 {
6190 /* The name was matched, now expand corresponding CUs that were
6191 marked. */
6192 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6193
6194 struct dwarf2_per_cu_data *per_cu;
6195 while ((per_cu = iter.next ()) != NULL)
6196 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6197 return true;
6198 });
6199
6200 /* It's a shame we couldn't do this inside the
6201 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6202 that have already been expanded. Instead, this loop matches what
6203 the psymtab code does. */
6204 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6205 {
6206 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6207 if (cust != nullptr)
6208 {
6209 const struct block *block
6210 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6211 if (!iterate_over_symbols_terminated (block, name,
6212 domain, callback))
6213 break;
6214 }
6215 }
6216 }
6217
6218 static void
6219 dw2_debug_names_expand_symtabs_matching
6220 (struct objfile *objfile,
6221 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6222 const lookup_name_info &lookup_name,
6223 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6224 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6225 enum search_domain kind)
6226 {
6227 struct dwarf2_per_objfile *dwarf2_per_objfile
6228 = get_dwarf2_per_objfile (objfile);
6229
6230 /* debug_names_table is NULL if OBJF_READNOW. */
6231 if (!dwarf2_per_objfile->debug_names_table)
6232 return;
6233
6234 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6235
6236 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6237
6238 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6239 symbol_matcher,
6240 kind, [&] (offset_type namei)
6241 {
6242 /* The name was matched, now expand corresponding CUs that were
6243 marked. */
6244 dw2_debug_names_iterator iter (map, kind, namei);
6245
6246 struct dwarf2_per_cu_data *per_cu;
6247 while ((per_cu = iter.next ()) != NULL)
6248 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6249 expansion_notify);
6250 return true;
6251 });
6252 }
6253
6254 const struct quick_symbol_functions dwarf2_debug_names_functions =
6255 {
6256 dw2_has_symbols,
6257 dw2_find_last_source_symtab,
6258 dw2_forget_cached_source_info,
6259 dw2_map_symtabs_matching_filename,
6260 dw2_debug_names_lookup_symbol,
6261 dw2_print_stats,
6262 dw2_debug_names_dump,
6263 dw2_debug_names_expand_symtabs_for_function,
6264 dw2_expand_all_symtabs,
6265 dw2_expand_symtabs_with_fullname,
6266 dw2_debug_names_map_matching_symbols,
6267 dw2_debug_names_expand_symtabs_matching,
6268 dw2_find_pc_sect_compunit_symtab,
6269 NULL,
6270 dw2_map_symbol_filenames
6271 };
6272
6273 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6274 to either a dwarf2_per_objfile or dwz_file object. */
6275
6276 template <typename T>
6277 static gdb::array_view<const gdb_byte>
6278 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6279 {
6280 dwarf2_section_info *section = &section_owner->gdb_index;
6281
6282 if (dwarf2_section_empty_p (section))
6283 return {};
6284
6285 /* Older elfutils strip versions could keep the section in the main
6286 executable while splitting it for the separate debug info file. */
6287 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6288 return {};
6289
6290 dwarf2_read_section (obj, section);
6291
6292 /* dwarf2_section_info::size is a bfd_size_type, while
6293 gdb::array_view works with size_t. On 32-bit hosts, with
6294 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6295 is 32-bit. So we need an explicit narrowing conversion here.
6296 This is fine, because it's impossible to allocate or mmap an
6297 array/buffer larger than what size_t can represent. */
6298 return gdb::make_array_view (section->buffer, section->size);
6299 }
6300
6301 /* Lookup the index cache for the contents of the index associated to
6302 DWARF2_OBJ. */
6303
6304 static gdb::array_view<const gdb_byte>
6305 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6306 {
6307 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6308 if (build_id == nullptr)
6309 return {};
6310
6311 return global_index_cache.lookup_gdb_index (build_id,
6312 &dwarf2_obj->index_cache_res);
6313 }
6314
6315 /* Same as the above, but for DWZ. */
6316
6317 static gdb::array_view<const gdb_byte>
6318 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6319 {
6320 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6321 if (build_id == nullptr)
6322 return {};
6323
6324 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6325 }
6326
6327 /* See symfile.h. */
6328
6329 bool
6330 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6331 {
6332 struct dwarf2_per_objfile *dwarf2_per_objfile
6333 = get_dwarf2_per_objfile (objfile);
6334
6335 /* If we're about to read full symbols, don't bother with the
6336 indices. In this case we also don't care if some other debug
6337 format is making psymtabs, because they are all about to be
6338 expanded anyway. */
6339 if ((objfile->flags & OBJF_READNOW))
6340 {
6341 dwarf2_per_objfile->using_index = 1;
6342 create_all_comp_units (dwarf2_per_objfile);
6343 create_all_type_units (dwarf2_per_objfile);
6344 dwarf2_per_objfile->quick_file_names_table
6345 = create_quick_file_names_table
6346 (dwarf2_per_objfile->all_comp_units.size ());
6347
6348 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6349 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6350 {
6351 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6352
6353 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6354 struct dwarf2_per_cu_quick_data);
6355 }
6356
6357 /* Return 1 so that gdb sees the "quick" functions. However,
6358 these functions will be no-ops because we will have expanded
6359 all symtabs. */
6360 *index_kind = dw_index_kind::GDB_INDEX;
6361 return true;
6362 }
6363
6364 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6365 {
6366 *index_kind = dw_index_kind::DEBUG_NAMES;
6367 return true;
6368 }
6369
6370 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6371 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6372 get_gdb_index_contents_from_section<dwz_file>))
6373 {
6374 *index_kind = dw_index_kind::GDB_INDEX;
6375 return true;
6376 }
6377
6378 /* ... otherwise, try to find the index in the index cache. */
6379 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6380 get_gdb_index_contents_from_cache,
6381 get_gdb_index_contents_from_cache_dwz))
6382 {
6383 global_index_cache.hit ();
6384 *index_kind = dw_index_kind::GDB_INDEX;
6385 return true;
6386 }
6387
6388 global_index_cache.miss ();
6389 return false;
6390 }
6391
6392 \f
6393
6394 /* Build a partial symbol table. */
6395
6396 void
6397 dwarf2_build_psymtabs (struct objfile *objfile)
6398 {
6399 struct dwarf2_per_objfile *dwarf2_per_objfile
6400 = get_dwarf2_per_objfile (objfile);
6401
6402 init_psymbol_list (objfile, 1024);
6403
6404 try
6405 {
6406 /* This isn't really ideal: all the data we allocate on the
6407 objfile's obstack is still uselessly kept around. However,
6408 freeing it seems unsafe. */
6409 psymtab_discarder psymtabs (objfile);
6410 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6411 psymtabs.keep ();
6412
6413 /* (maybe) store an index in the cache. */
6414 global_index_cache.store (dwarf2_per_objfile);
6415 }
6416 catch (const gdb_exception_error &except)
6417 {
6418 exception_print (gdb_stderr, except);
6419 }
6420 }
6421
6422 /* Return the total length of the CU described by HEADER. */
6423
6424 static unsigned int
6425 get_cu_length (const struct comp_unit_head *header)
6426 {
6427 return header->initial_length_size + header->length;
6428 }
6429
6430 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6431
6432 static inline bool
6433 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6434 {
6435 sect_offset bottom = cu_header->sect_off;
6436 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6437
6438 return sect_off >= bottom && sect_off < top;
6439 }
6440
6441 /* Find the base address of the compilation unit for range lists and
6442 location lists. It will normally be specified by DW_AT_low_pc.
6443 In DWARF-3 draft 4, the base address could be overridden by
6444 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6445 compilation units with discontinuous ranges. */
6446
6447 static void
6448 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6449 {
6450 struct attribute *attr;
6451
6452 cu->base_known = 0;
6453 cu->base_address = 0;
6454
6455 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6456 if (attr != nullptr)
6457 {
6458 cu->base_address = attr_value_as_address (attr);
6459 cu->base_known = 1;
6460 }
6461 else
6462 {
6463 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6464 if (attr != nullptr)
6465 {
6466 cu->base_address = attr_value_as_address (attr);
6467 cu->base_known = 1;
6468 }
6469 }
6470 }
6471
6472 /* Read in the comp unit header information from the debug_info at info_ptr.
6473 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6474 NOTE: This leaves members offset, first_die_offset to be filled in
6475 by the caller. */
6476
6477 static const gdb_byte *
6478 read_comp_unit_head (struct comp_unit_head *cu_header,
6479 const gdb_byte *info_ptr,
6480 struct dwarf2_section_info *section,
6481 rcuh_kind section_kind)
6482 {
6483 int signed_addr;
6484 unsigned int bytes_read;
6485 const char *filename = get_section_file_name (section);
6486 bfd *abfd = get_section_bfd_owner (section);
6487
6488 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6489 cu_header->initial_length_size = bytes_read;
6490 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6491 info_ptr += bytes_read;
6492 cu_header->version = read_2_bytes (abfd, info_ptr);
6493 if (cu_header->version < 2 || cu_header->version > 5)
6494 error (_("Dwarf Error: wrong version in compilation unit header "
6495 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6496 cu_header->version, filename);
6497 info_ptr += 2;
6498 if (cu_header->version < 5)
6499 switch (section_kind)
6500 {
6501 case rcuh_kind::COMPILE:
6502 cu_header->unit_type = DW_UT_compile;
6503 break;
6504 case rcuh_kind::TYPE:
6505 cu_header->unit_type = DW_UT_type;
6506 break;
6507 default:
6508 internal_error (__FILE__, __LINE__,
6509 _("read_comp_unit_head: invalid section_kind"));
6510 }
6511 else
6512 {
6513 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6514 (read_1_byte (abfd, info_ptr));
6515 info_ptr += 1;
6516 switch (cu_header->unit_type)
6517 {
6518 case DW_UT_compile:
6519 case DW_UT_partial:
6520 case DW_UT_skeleton:
6521 case DW_UT_split_compile:
6522 if (section_kind != rcuh_kind::COMPILE)
6523 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6524 "(is %s, should be %s) [in module %s]"),
6525 dwarf_unit_type_name (cu_header->unit_type),
6526 dwarf_unit_type_name (DW_UT_type), filename);
6527 break;
6528 case DW_UT_type:
6529 case DW_UT_split_type:
6530 section_kind = rcuh_kind::TYPE;
6531 break;
6532 default:
6533 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6534 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6535 "[in module %s]"), cu_header->unit_type,
6536 dwarf_unit_type_name (DW_UT_compile),
6537 dwarf_unit_type_name (DW_UT_skeleton),
6538 dwarf_unit_type_name (DW_UT_split_compile),
6539 dwarf_unit_type_name (DW_UT_type),
6540 dwarf_unit_type_name (DW_UT_split_type), filename);
6541 }
6542
6543 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6544 info_ptr += 1;
6545 }
6546 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6547 cu_header,
6548 &bytes_read);
6549 info_ptr += bytes_read;
6550 if (cu_header->version < 5)
6551 {
6552 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6553 info_ptr += 1;
6554 }
6555 signed_addr = bfd_get_sign_extend_vma (abfd);
6556 if (signed_addr < 0)
6557 internal_error (__FILE__, __LINE__,
6558 _("read_comp_unit_head: dwarf from non elf file"));
6559 cu_header->signed_addr_p = signed_addr;
6560
6561 bool header_has_signature = section_kind == rcuh_kind::TYPE
6562 || cu_header->unit_type == DW_UT_skeleton
6563 || cu_header->unit_type == DW_UT_split_compile;
6564
6565 if (header_has_signature)
6566 {
6567 cu_header->signature = read_8_bytes (abfd, info_ptr);
6568 info_ptr += 8;
6569 }
6570
6571 if (section_kind == rcuh_kind::TYPE)
6572 {
6573 LONGEST type_offset;
6574 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6575 info_ptr += bytes_read;
6576 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6577 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6578 error (_("Dwarf Error: Too big type_offset in compilation unit "
6579 "header (is %s) [in module %s]"), plongest (type_offset),
6580 filename);
6581 }
6582
6583 return info_ptr;
6584 }
6585
6586 /* Helper function that returns the proper abbrev section for
6587 THIS_CU. */
6588
6589 static struct dwarf2_section_info *
6590 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6591 {
6592 struct dwarf2_section_info *abbrev;
6593 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6594
6595 if (this_cu->is_dwz)
6596 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6597 else
6598 abbrev = &dwarf2_per_objfile->abbrev;
6599
6600 return abbrev;
6601 }
6602
6603 /* Subroutine of read_and_check_comp_unit_head and
6604 read_and_check_type_unit_head to simplify them.
6605 Perform various error checking on the header. */
6606
6607 static void
6608 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6609 struct comp_unit_head *header,
6610 struct dwarf2_section_info *section,
6611 struct dwarf2_section_info *abbrev_section)
6612 {
6613 const char *filename = get_section_file_name (section);
6614
6615 if (to_underlying (header->abbrev_sect_off)
6616 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6617 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6618 "(offset %s + 6) [in module %s]"),
6619 sect_offset_str (header->abbrev_sect_off),
6620 sect_offset_str (header->sect_off),
6621 filename);
6622
6623 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6624 avoid potential 32-bit overflow. */
6625 if (((ULONGEST) header->sect_off + get_cu_length (header))
6626 > section->size)
6627 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6628 "(offset %s + 0) [in module %s]"),
6629 header->length, sect_offset_str (header->sect_off),
6630 filename);
6631 }
6632
6633 /* Read in a CU/TU header and perform some basic error checking.
6634 The contents of the header are stored in HEADER.
6635 The result is a pointer to the start of the first DIE. */
6636
6637 static const gdb_byte *
6638 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6639 struct comp_unit_head *header,
6640 struct dwarf2_section_info *section,
6641 struct dwarf2_section_info *abbrev_section,
6642 const gdb_byte *info_ptr,
6643 rcuh_kind section_kind)
6644 {
6645 const gdb_byte *beg_of_comp_unit = info_ptr;
6646
6647 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6648
6649 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6650
6651 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6652
6653 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6654 abbrev_section);
6655
6656 return info_ptr;
6657 }
6658
6659 /* Fetch the abbreviation table offset from a comp or type unit header. */
6660
6661 static sect_offset
6662 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6663 struct dwarf2_section_info *section,
6664 sect_offset sect_off)
6665 {
6666 bfd *abfd = get_section_bfd_owner (section);
6667 const gdb_byte *info_ptr;
6668 unsigned int initial_length_size, offset_size;
6669 uint16_t version;
6670
6671 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6672 info_ptr = section->buffer + to_underlying (sect_off);
6673 read_initial_length (abfd, info_ptr, &initial_length_size);
6674 offset_size = initial_length_size == 4 ? 4 : 8;
6675 info_ptr += initial_length_size;
6676
6677 version = read_2_bytes (abfd, info_ptr);
6678 info_ptr += 2;
6679 if (version >= 5)
6680 {
6681 /* Skip unit type and address size. */
6682 info_ptr += 2;
6683 }
6684
6685 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6686 }
6687
6688 /* Allocate a new partial symtab for file named NAME and mark this new
6689 partial symtab as being an include of PST. */
6690
6691 static void
6692 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6693 struct objfile *objfile)
6694 {
6695 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
6696
6697 if (!IS_ABSOLUTE_PATH (subpst->filename))
6698 {
6699 /* It shares objfile->objfile_obstack. */
6700 subpst->dirname = pst->dirname;
6701 }
6702
6703 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6704 subpst->dependencies[0] = pst;
6705 subpst->number_of_dependencies = 1;
6706
6707 /* No private part is necessary for include psymtabs. This property
6708 can be used to differentiate between such include psymtabs and
6709 the regular ones. */
6710 subpst->per_cu_data = nullptr;
6711 }
6712
6713 /* Read the Line Number Program data and extract the list of files
6714 included by the source file represented by PST. Build an include
6715 partial symtab for each of these included files. */
6716
6717 static void
6718 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6719 struct die_info *die,
6720 dwarf2_psymtab *pst)
6721 {
6722 line_header_up lh;
6723 struct attribute *attr;
6724
6725 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6726 if (attr != nullptr)
6727 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6728 if (lh == NULL)
6729 return; /* No linetable, so no includes. */
6730
6731 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6732 that we pass in the raw text_low here; that is ok because we're
6733 only decoding the line table to make include partial symtabs, and
6734 so the addresses aren't really used. */
6735 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6736 pst->raw_text_low (), 1);
6737 }
6738
6739 static hashval_t
6740 hash_signatured_type (const void *item)
6741 {
6742 const struct signatured_type *sig_type
6743 = (const struct signatured_type *) item;
6744
6745 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6746 return sig_type->signature;
6747 }
6748
6749 static int
6750 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6751 {
6752 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6753 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6754
6755 return lhs->signature == rhs->signature;
6756 }
6757
6758 /* Allocate a hash table for signatured types. */
6759
6760 static htab_t
6761 allocate_signatured_type_table (struct objfile *objfile)
6762 {
6763 return htab_create_alloc_ex (41,
6764 hash_signatured_type,
6765 eq_signatured_type,
6766 NULL,
6767 &objfile->objfile_obstack,
6768 hashtab_obstack_allocate,
6769 dummy_obstack_deallocate);
6770 }
6771
6772 /* A helper function to add a signatured type CU to a table. */
6773
6774 static int
6775 add_signatured_type_cu_to_table (void **slot, void *datum)
6776 {
6777 struct signatured_type *sigt = (struct signatured_type *) *slot;
6778 std::vector<signatured_type *> *all_type_units
6779 = (std::vector<signatured_type *> *) datum;
6780
6781 all_type_units->push_back (sigt);
6782
6783 return 1;
6784 }
6785
6786 /* A helper for create_debug_types_hash_table. Read types from SECTION
6787 and fill them into TYPES_HTAB. It will process only type units,
6788 therefore DW_UT_type. */
6789
6790 static void
6791 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6792 struct dwo_file *dwo_file,
6793 dwarf2_section_info *section, htab_t &types_htab,
6794 rcuh_kind section_kind)
6795 {
6796 struct objfile *objfile = dwarf2_per_objfile->objfile;
6797 struct dwarf2_section_info *abbrev_section;
6798 bfd *abfd;
6799 const gdb_byte *info_ptr, *end_ptr;
6800
6801 abbrev_section = (dwo_file != NULL
6802 ? &dwo_file->sections.abbrev
6803 : &dwarf2_per_objfile->abbrev);
6804
6805 if (dwarf_read_debug)
6806 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6807 get_section_name (section),
6808 get_section_file_name (abbrev_section));
6809
6810 dwarf2_read_section (objfile, section);
6811 info_ptr = section->buffer;
6812
6813 if (info_ptr == NULL)
6814 return;
6815
6816 /* We can't set abfd until now because the section may be empty or
6817 not present, in which case the bfd is unknown. */
6818 abfd = get_section_bfd_owner (section);
6819
6820 /* We don't use cutu_reader here because we don't need to read
6821 any dies: the signature is in the header. */
6822
6823 end_ptr = info_ptr + section->size;
6824 while (info_ptr < end_ptr)
6825 {
6826 struct signatured_type *sig_type;
6827 struct dwo_unit *dwo_tu;
6828 void **slot;
6829 const gdb_byte *ptr = info_ptr;
6830 struct comp_unit_head header;
6831 unsigned int length;
6832
6833 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6834
6835 /* Initialize it due to a false compiler warning. */
6836 header.signature = -1;
6837 header.type_cu_offset_in_tu = (cu_offset) -1;
6838
6839 /* We need to read the type's signature in order to build the hash
6840 table, but we don't need anything else just yet. */
6841
6842 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6843 abbrev_section, ptr, section_kind);
6844
6845 length = get_cu_length (&header);
6846
6847 /* Skip dummy type units. */
6848 if (ptr >= info_ptr + length
6849 || peek_abbrev_code (abfd, ptr) == 0
6850 || header.unit_type != DW_UT_type)
6851 {
6852 info_ptr += length;
6853 continue;
6854 }
6855
6856 if (types_htab == NULL)
6857 {
6858 if (dwo_file)
6859 types_htab = allocate_dwo_unit_table (objfile);
6860 else
6861 types_htab = allocate_signatured_type_table (objfile);
6862 }
6863
6864 if (dwo_file)
6865 {
6866 sig_type = NULL;
6867 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6868 struct dwo_unit);
6869 dwo_tu->dwo_file = dwo_file;
6870 dwo_tu->signature = header.signature;
6871 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6872 dwo_tu->section = section;
6873 dwo_tu->sect_off = sect_off;
6874 dwo_tu->length = length;
6875 }
6876 else
6877 {
6878 /* N.B.: type_offset is not usable if this type uses a DWO file.
6879 The real type_offset is in the DWO file. */
6880 dwo_tu = NULL;
6881 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6882 struct signatured_type);
6883 sig_type->signature = header.signature;
6884 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6885 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6886 sig_type->per_cu.is_debug_types = 1;
6887 sig_type->per_cu.section = section;
6888 sig_type->per_cu.sect_off = sect_off;
6889 sig_type->per_cu.length = length;
6890 }
6891
6892 slot = htab_find_slot (types_htab,
6893 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6894 INSERT);
6895 gdb_assert (slot != NULL);
6896 if (*slot != NULL)
6897 {
6898 sect_offset dup_sect_off;
6899
6900 if (dwo_file)
6901 {
6902 const struct dwo_unit *dup_tu
6903 = (const struct dwo_unit *) *slot;
6904
6905 dup_sect_off = dup_tu->sect_off;
6906 }
6907 else
6908 {
6909 const struct signatured_type *dup_tu
6910 = (const struct signatured_type *) *slot;
6911
6912 dup_sect_off = dup_tu->per_cu.sect_off;
6913 }
6914
6915 complaint (_("debug type entry at offset %s is duplicate to"
6916 " the entry at offset %s, signature %s"),
6917 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6918 hex_string (header.signature));
6919 }
6920 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6921
6922 if (dwarf_read_debug > 1)
6923 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6924 sect_offset_str (sect_off),
6925 hex_string (header.signature));
6926
6927 info_ptr += length;
6928 }
6929 }
6930
6931 /* Create the hash table of all entries in the .debug_types
6932 (or .debug_types.dwo) section(s).
6933 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6934 otherwise it is NULL.
6935
6936 The result is a pointer to the hash table or NULL if there are no types.
6937
6938 Note: This function processes DWO files only, not DWP files. */
6939
6940 static void
6941 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6942 struct dwo_file *dwo_file,
6943 gdb::array_view<dwarf2_section_info> type_sections,
6944 htab_t &types_htab)
6945 {
6946 for (dwarf2_section_info &section : type_sections)
6947 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6948 types_htab, rcuh_kind::TYPE);
6949 }
6950
6951 /* Create the hash table of all entries in the .debug_types section,
6952 and initialize all_type_units.
6953 The result is zero if there is an error (e.g. missing .debug_types section),
6954 otherwise non-zero. */
6955
6956 static int
6957 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6958 {
6959 htab_t types_htab = NULL;
6960
6961 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6962 &dwarf2_per_objfile->info, types_htab,
6963 rcuh_kind::COMPILE);
6964 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6965 dwarf2_per_objfile->types, types_htab);
6966 if (types_htab == NULL)
6967 {
6968 dwarf2_per_objfile->signatured_types = NULL;
6969 return 0;
6970 }
6971
6972 dwarf2_per_objfile->signatured_types = types_htab;
6973
6974 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6975 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6976
6977 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6978 &dwarf2_per_objfile->all_type_units);
6979
6980 return 1;
6981 }
6982
6983 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6984 If SLOT is non-NULL, it is the entry to use in the hash table.
6985 Otherwise we find one. */
6986
6987 static struct signatured_type *
6988 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6989 void **slot)
6990 {
6991 struct objfile *objfile = dwarf2_per_objfile->objfile;
6992
6993 if (dwarf2_per_objfile->all_type_units.size ()
6994 == dwarf2_per_objfile->all_type_units.capacity ())
6995 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6996
6997 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6998 struct signatured_type);
6999
7000 dwarf2_per_objfile->all_type_units.push_back (sig_type);
7001 sig_type->signature = sig;
7002 sig_type->per_cu.is_debug_types = 1;
7003 if (dwarf2_per_objfile->using_index)
7004 {
7005 sig_type->per_cu.v.quick =
7006 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7007 struct dwarf2_per_cu_quick_data);
7008 }
7009
7010 if (slot == NULL)
7011 {
7012 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7013 sig_type, INSERT);
7014 }
7015 gdb_assert (*slot == NULL);
7016 *slot = sig_type;
7017 /* The rest of sig_type must be filled in by the caller. */
7018 return sig_type;
7019 }
7020
7021 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7022 Fill in SIG_ENTRY with DWO_ENTRY. */
7023
7024 static void
7025 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7026 struct signatured_type *sig_entry,
7027 struct dwo_unit *dwo_entry)
7028 {
7029 /* Make sure we're not clobbering something we don't expect to. */
7030 gdb_assert (! sig_entry->per_cu.queued);
7031 gdb_assert (sig_entry->per_cu.cu == NULL);
7032 if (dwarf2_per_objfile->using_index)
7033 {
7034 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7035 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7036 }
7037 else
7038 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7039 gdb_assert (sig_entry->signature == dwo_entry->signature);
7040 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7041 gdb_assert (sig_entry->type_unit_group == NULL);
7042 gdb_assert (sig_entry->dwo_unit == NULL);
7043
7044 sig_entry->per_cu.section = dwo_entry->section;
7045 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7046 sig_entry->per_cu.length = dwo_entry->length;
7047 sig_entry->per_cu.reading_dwo_directly = 1;
7048 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7049 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7050 sig_entry->dwo_unit = dwo_entry;
7051 }
7052
7053 /* Subroutine of lookup_signatured_type.
7054 If we haven't read the TU yet, create the signatured_type data structure
7055 for a TU to be read in directly from a DWO file, bypassing the stub.
7056 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7057 using .gdb_index, then when reading a CU we want to stay in the DWO file
7058 containing that CU. Otherwise we could end up reading several other DWO
7059 files (due to comdat folding) to process the transitive closure of all the
7060 mentioned TUs, and that can be slow. The current DWO file will have every
7061 type signature that it needs.
7062 We only do this for .gdb_index because in the psymtab case we already have
7063 to read all the DWOs to build the type unit groups. */
7064
7065 static struct signatured_type *
7066 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7067 {
7068 struct dwarf2_per_objfile *dwarf2_per_objfile
7069 = cu->per_cu->dwarf2_per_objfile;
7070 struct objfile *objfile = dwarf2_per_objfile->objfile;
7071 struct dwo_file *dwo_file;
7072 struct dwo_unit find_dwo_entry, *dwo_entry;
7073 struct signatured_type find_sig_entry, *sig_entry;
7074 void **slot;
7075
7076 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7077
7078 /* If TU skeletons have been removed then we may not have read in any
7079 TUs yet. */
7080 if (dwarf2_per_objfile->signatured_types == NULL)
7081 {
7082 dwarf2_per_objfile->signatured_types
7083 = allocate_signatured_type_table (objfile);
7084 }
7085
7086 /* We only ever need to read in one copy of a signatured type.
7087 Use the global signatured_types array to do our own comdat-folding
7088 of types. If this is the first time we're reading this TU, and
7089 the TU has an entry in .gdb_index, replace the recorded data from
7090 .gdb_index with this TU. */
7091
7092 find_sig_entry.signature = sig;
7093 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7094 &find_sig_entry, INSERT);
7095 sig_entry = (struct signatured_type *) *slot;
7096
7097 /* We can get here with the TU already read, *or* in the process of being
7098 read. Don't reassign the global entry to point to this DWO if that's
7099 the case. Also note that if the TU is already being read, it may not
7100 have come from a DWO, the program may be a mix of Fission-compiled
7101 code and non-Fission-compiled code. */
7102
7103 /* Have we already tried to read this TU?
7104 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7105 needn't exist in the global table yet). */
7106 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7107 return sig_entry;
7108
7109 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7110 dwo_unit of the TU itself. */
7111 dwo_file = cu->dwo_unit->dwo_file;
7112
7113 /* Ok, this is the first time we're reading this TU. */
7114 if (dwo_file->tus == NULL)
7115 return NULL;
7116 find_dwo_entry.signature = sig;
7117 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7118 if (dwo_entry == NULL)
7119 return NULL;
7120
7121 /* If the global table doesn't have an entry for this TU, add one. */
7122 if (sig_entry == NULL)
7123 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7124
7125 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7126 sig_entry->per_cu.tu_read = 1;
7127 return sig_entry;
7128 }
7129
7130 /* Subroutine of lookup_signatured_type.
7131 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7132 then try the DWP file. If the TU stub (skeleton) has been removed then
7133 it won't be in .gdb_index. */
7134
7135 static struct signatured_type *
7136 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7137 {
7138 struct dwarf2_per_objfile *dwarf2_per_objfile
7139 = cu->per_cu->dwarf2_per_objfile;
7140 struct objfile *objfile = dwarf2_per_objfile->objfile;
7141 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7142 struct dwo_unit *dwo_entry;
7143 struct signatured_type find_sig_entry, *sig_entry;
7144 void **slot;
7145
7146 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7147 gdb_assert (dwp_file != NULL);
7148
7149 /* If TU skeletons have been removed then we may not have read in any
7150 TUs yet. */
7151 if (dwarf2_per_objfile->signatured_types == NULL)
7152 {
7153 dwarf2_per_objfile->signatured_types
7154 = allocate_signatured_type_table (objfile);
7155 }
7156
7157 find_sig_entry.signature = sig;
7158 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7159 &find_sig_entry, INSERT);
7160 sig_entry = (struct signatured_type *) *slot;
7161
7162 /* Have we already tried to read this TU?
7163 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7164 needn't exist in the global table yet). */
7165 if (sig_entry != NULL)
7166 return sig_entry;
7167
7168 if (dwp_file->tus == NULL)
7169 return NULL;
7170 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7171 sig, 1 /* is_debug_types */);
7172 if (dwo_entry == NULL)
7173 return NULL;
7174
7175 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7176 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7177
7178 return sig_entry;
7179 }
7180
7181 /* Lookup a signature based type for DW_FORM_ref_sig8.
7182 Returns NULL if signature SIG is not present in the table.
7183 It is up to the caller to complain about this. */
7184
7185 static struct signatured_type *
7186 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7187 {
7188 struct dwarf2_per_objfile *dwarf2_per_objfile
7189 = cu->per_cu->dwarf2_per_objfile;
7190
7191 if (cu->dwo_unit
7192 && dwarf2_per_objfile->using_index)
7193 {
7194 /* We're in a DWO/DWP file, and we're using .gdb_index.
7195 These cases require special processing. */
7196 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7197 return lookup_dwo_signatured_type (cu, sig);
7198 else
7199 return lookup_dwp_signatured_type (cu, sig);
7200 }
7201 else
7202 {
7203 struct signatured_type find_entry, *entry;
7204
7205 if (dwarf2_per_objfile->signatured_types == NULL)
7206 return NULL;
7207 find_entry.signature = sig;
7208 entry = ((struct signatured_type *)
7209 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7210 return entry;
7211 }
7212 }
7213
7214 /* Return the address base of the compile unit, which, if exists, is stored
7215 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
7216 static gdb::optional<ULONGEST>
7217 lookup_addr_base (struct die_info *comp_unit_die)
7218 {
7219 struct attribute *attr;
7220 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
7221 if (attr == nullptr)
7222 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
7223 if (attr == nullptr)
7224 return gdb::optional<ULONGEST> ();
7225 return DW_UNSND (attr);
7226 }
7227
7228 /* Return range lists base of the compile unit, which, if exists, is stored
7229 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
7230 static ULONGEST
7231 lookup_ranges_base (struct die_info *comp_unit_die)
7232 {
7233 struct attribute *attr;
7234 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
7235 if (attr == nullptr)
7236 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
7237 if (attr == nullptr)
7238 return 0;
7239 return DW_UNSND (attr);
7240 }
7241
7242 /* Low level DIE reading support. */
7243
7244 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7245
7246 static void
7247 init_cu_die_reader (struct die_reader_specs *reader,
7248 struct dwarf2_cu *cu,
7249 struct dwarf2_section_info *section,
7250 struct dwo_file *dwo_file,
7251 struct abbrev_table *abbrev_table)
7252 {
7253 gdb_assert (section->readin && section->buffer != NULL);
7254 reader->abfd = get_section_bfd_owner (section);
7255 reader->cu = cu;
7256 reader->dwo_file = dwo_file;
7257 reader->die_section = section;
7258 reader->buffer = section->buffer;
7259 reader->buffer_end = section->buffer + section->size;
7260 reader->comp_dir = NULL;
7261 reader->abbrev_table = abbrev_table;
7262 }
7263
7264 /* Subroutine of cutu_reader to simplify it.
7265 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7266 There's just a lot of work to do, and cutu_reader is big enough
7267 already.
7268
7269 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7270 from it to the DIE in the DWO. If NULL we are skipping the stub.
7271 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7272 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7273 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7274 STUB_COMP_DIR may be non-NULL.
7275 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7276 are filled in with the info of the DIE from the DWO file.
7277 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7278 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7279 kept around for at least as long as *RESULT_READER.
7280
7281 The result is non-zero if a valid (non-dummy) DIE was found. */
7282
7283 static int
7284 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7285 struct dwo_unit *dwo_unit,
7286 struct die_info *stub_comp_unit_die,
7287 const char *stub_comp_dir,
7288 struct die_reader_specs *result_reader,
7289 const gdb_byte **result_info_ptr,
7290 struct die_info **result_comp_unit_die,
7291 int *result_has_children,
7292 abbrev_table_up *result_dwo_abbrev_table)
7293 {
7294 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7295 struct objfile *objfile = dwarf2_per_objfile->objfile;
7296 struct dwarf2_cu *cu = this_cu->cu;
7297 bfd *abfd;
7298 const gdb_byte *begin_info_ptr, *info_ptr;
7299 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7300 int i,num_extra_attrs;
7301 struct dwarf2_section_info *dwo_abbrev_section;
7302 struct die_info *comp_unit_die;
7303
7304 /* At most one of these may be provided. */
7305 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7306
7307 /* These attributes aren't processed until later:
7308 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7309 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7310 referenced later. However, these attributes are found in the stub
7311 which we won't have later. In order to not impose this complication
7312 on the rest of the code, we read them here and copy them to the
7313 DWO CU/TU die. */
7314
7315 stmt_list = NULL;
7316 low_pc = NULL;
7317 high_pc = NULL;
7318 ranges = NULL;
7319 comp_dir = NULL;
7320
7321 if (stub_comp_unit_die != NULL)
7322 {
7323 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7324 DWO file. */
7325 if (! this_cu->is_debug_types)
7326 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7327 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7328 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7329 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7330 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7331
7332 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
7333
7334 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
7335 here (if needed). We need the value before we can process
7336 DW_AT_ranges. */
7337 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
7338 }
7339 else if (stub_comp_dir != NULL)
7340 {
7341 /* Reconstruct the comp_dir attribute to simplify the code below. */
7342 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7343 comp_dir->name = DW_AT_comp_dir;
7344 comp_dir->form = DW_FORM_string;
7345 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7346 DW_STRING (comp_dir) = stub_comp_dir;
7347 }
7348
7349 /* Set up for reading the DWO CU/TU. */
7350 cu->dwo_unit = dwo_unit;
7351 dwarf2_section_info *section = dwo_unit->section;
7352 dwarf2_read_section (objfile, section);
7353 abfd = get_section_bfd_owner (section);
7354 begin_info_ptr = info_ptr = (section->buffer
7355 + to_underlying (dwo_unit->sect_off));
7356 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7357
7358 if (this_cu->is_debug_types)
7359 {
7360 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7361
7362 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7363 &cu->header, section,
7364 dwo_abbrev_section,
7365 info_ptr, rcuh_kind::TYPE);
7366 /* This is not an assert because it can be caused by bad debug info. */
7367 if (sig_type->signature != cu->header.signature)
7368 {
7369 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7370 " TU at offset %s [in module %s]"),
7371 hex_string (sig_type->signature),
7372 hex_string (cu->header.signature),
7373 sect_offset_str (dwo_unit->sect_off),
7374 bfd_get_filename (abfd));
7375 }
7376 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7377 /* For DWOs coming from DWP files, we don't know the CU length
7378 nor the type's offset in the TU until now. */
7379 dwo_unit->length = get_cu_length (&cu->header);
7380 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7381
7382 /* Establish the type offset that can be used to lookup the type.
7383 For DWO files, we don't know it until now. */
7384 sig_type->type_offset_in_section
7385 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7386 }
7387 else
7388 {
7389 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7390 &cu->header, section,
7391 dwo_abbrev_section,
7392 info_ptr, rcuh_kind::COMPILE);
7393 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7394 /* For DWOs coming from DWP files, we don't know the CU length
7395 until now. */
7396 dwo_unit->length = get_cu_length (&cu->header);
7397 }
7398
7399 *result_dwo_abbrev_table
7400 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7401 cu->header.abbrev_sect_off);
7402 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7403 result_dwo_abbrev_table->get ());
7404
7405 /* Read in the die, but leave space to copy over the attributes
7406 from the stub. This has the benefit of simplifying the rest of
7407 the code - all the work to maintain the illusion of a single
7408 DW_TAG_{compile,type}_unit DIE is done here. */
7409 num_extra_attrs = ((stmt_list != NULL)
7410 + (low_pc != NULL)
7411 + (high_pc != NULL)
7412 + (ranges != NULL)
7413 + (comp_dir != NULL));
7414 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7415 result_has_children, num_extra_attrs);
7416
7417 /* Copy over the attributes from the stub to the DIE we just read in. */
7418 comp_unit_die = *result_comp_unit_die;
7419 i = comp_unit_die->num_attrs;
7420 if (stmt_list != NULL)
7421 comp_unit_die->attrs[i++] = *stmt_list;
7422 if (low_pc != NULL)
7423 comp_unit_die->attrs[i++] = *low_pc;
7424 if (high_pc != NULL)
7425 comp_unit_die->attrs[i++] = *high_pc;
7426 if (ranges != NULL)
7427 comp_unit_die->attrs[i++] = *ranges;
7428 if (comp_dir != NULL)
7429 comp_unit_die->attrs[i++] = *comp_dir;
7430 comp_unit_die->num_attrs += num_extra_attrs;
7431
7432 if (dwarf_die_debug)
7433 {
7434 fprintf_unfiltered (gdb_stdlog,
7435 "Read die from %s@0x%x of %s:\n",
7436 get_section_name (section),
7437 (unsigned) (begin_info_ptr - section->buffer),
7438 bfd_get_filename (abfd));
7439 dump_die (comp_unit_die, dwarf_die_debug);
7440 }
7441
7442 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7443 TUs by skipping the stub and going directly to the entry in the DWO file.
7444 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7445 to get it via circuitous means. Blech. */
7446 if (comp_dir != NULL)
7447 result_reader->comp_dir = DW_STRING (comp_dir);
7448
7449 /* Skip dummy compilation units. */
7450 if (info_ptr >= begin_info_ptr + dwo_unit->length
7451 || peek_abbrev_code (abfd, info_ptr) == 0)
7452 return 0;
7453
7454 *result_info_ptr = info_ptr;
7455 return 1;
7456 }
7457
7458 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7459 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7460 signature is part of the header. */
7461 static gdb::optional<ULONGEST>
7462 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7463 {
7464 if (cu->header.version >= 5)
7465 return cu->header.signature;
7466 struct attribute *attr;
7467 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7468 if (attr == nullptr)
7469 return gdb::optional<ULONGEST> ();
7470 return DW_UNSND (attr);
7471 }
7472
7473 /* Subroutine of cutu_reader to simplify it.
7474 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7475 Returns NULL if the specified DWO unit cannot be found. */
7476
7477 static struct dwo_unit *
7478 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7479 struct die_info *comp_unit_die,
7480 const char *dwo_name)
7481 {
7482 struct dwarf2_cu *cu = this_cu->cu;
7483 struct dwo_unit *dwo_unit;
7484 const char *comp_dir;
7485
7486 gdb_assert (cu != NULL);
7487
7488 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7489 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7490 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7491
7492 if (this_cu->is_debug_types)
7493 {
7494 struct signatured_type *sig_type;
7495
7496 /* Since this_cu is the first member of struct signatured_type,
7497 we can go from a pointer to one to a pointer to the other. */
7498 sig_type = (struct signatured_type *) this_cu;
7499 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7500 }
7501 else
7502 {
7503 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7504 if (!signature.has_value ())
7505 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7506 " [in module %s]"),
7507 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7508 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7509 *signature);
7510 }
7511
7512 return dwo_unit;
7513 }
7514
7515 /* Subroutine of cutu_reader to simplify it.
7516 See it for a description of the parameters.
7517 Read a TU directly from a DWO file, bypassing the stub. */
7518
7519 void
7520 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7521 int use_existing_cu, int keep)
7522 {
7523 struct signatured_type *sig_type;
7524 struct die_reader_specs reader;
7525
7526 /* Verify we can do the following downcast, and that we have the
7527 data we need. */
7528 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7529 sig_type = (struct signatured_type *) this_cu;
7530 gdb_assert (sig_type->dwo_unit != NULL);
7531
7532 if (use_existing_cu && this_cu->cu != NULL)
7533 {
7534 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7535 /* There's no need to do the rereading_dwo_cu handling that
7536 cutu_reader does since we don't read the stub. */
7537 }
7538 else
7539 {
7540 /* If !use_existing_cu, this_cu->cu must be NULL. */
7541 gdb_assert (this_cu->cu == NULL);
7542 m_new_cu.reset (new dwarf2_cu (this_cu));
7543 }
7544
7545 /* A future optimization, if needed, would be to use an existing
7546 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7547 could share abbrev tables. */
7548
7549 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7550 NULL /* stub_comp_unit_die */,
7551 sig_type->dwo_unit->dwo_file->comp_dir,
7552 &reader, &info_ptr,
7553 &comp_unit_die, &has_children,
7554 &m_dwo_abbrev_table) == 0)
7555 {
7556 /* Dummy die. */
7557 dummy_p = true;
7558 }
7559 }
7560
7561 /* Initialize a CU (or TU) and read its DIEs.
7562 If the CU defers to a DWO file, read the DWO file as well.
7563
7564 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7565 Otherwise the table specified in the comp unit header is read in and used.
7566 This is an optimization for when we already have the abbrev table.
7567
7568 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7569 Otherwise, a new CU is allocated with xmalloc.
7570
7571 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7572 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7573 end. */
7574
7575 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7576 struct abbrev_table *abbrev_table,
7577 int use_existing_cu, int keep,
7578 bool skip_partial)
7579 : die_reader_specs {},
7580 m_this_cu (this_cu),
7581 m_keep (keep)
7582 {
7583 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7584 struct objfile *objfile = dwarf2_per_objfile->objfile;
7585 struct dwarf2_section_info *section = this_cu->section;
7586 bfd *abfd = get_section_bfd_owner (section);
7587 struct dwarf2_cu *cu;
7588 const gdb_byte *begin_info_ptr;
7589 struct signatured_type *sig_type = NULL;
7590 struct dwarf2_section_info *abbrev_section;
7591 /* Non-zero if CU currently points to a DWO file and we need to
7592 reread it. When this happens we need to reread the skeleton die
7593 before we can reread the DWO file (this only applies to CUs, not TUs). */
7594 int rereading_dwo_cu = 0;
7595
7596 if (dwarf_die_debug)
7597 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7598 this_cu->is_debug_types ? "type" : "comp",
7599 sect_offset_str (this_cu->sect_off));
7600
7601 if (use_existing_cu)
7602 gdb_assert (keep);
7603
7604 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7605 file (instead of going through the stub), short-circuit all of this. */
7606 if (this_cu->reading_dwo_directly)
7607 {
7608 /* Narrow down the scope of possibilities to have to understand. */
7609 gdb_assert (this_cu->is_debug_types);
7610 gdb_assert (abbrev_table == NULL);
7611 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
7612 return;
7613 }
7614
7615 /* This is cheap if the section is already read in. */
7616 dwarf2_read_section (objfile, section);
7617
7618 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7619
7620 abbrev_section = get_abbrev_section_for_cu (this_cu);
7621
7622 if (use_existing_cu && this_cu->cu != NULL)
7623 {
7624 cu = this_cu->cu;
7625 /* If this CU is from a DWO file we need to start over, we need to
7626 refetch the attributes from the skeleton CU.
7627 This could be optimized by retrieving those attributes from when we
7628 were here the first time: the previous comp_unit_die was stored in
7629 comp_unit_obstack. But there's no data yet that we need this
7630 optimization. */
7631 if (cu->dwo_unit != NULL)
7632 rereading_dwo_cu = 1;
7633 }
7634 else
7635 {
7636 /* If !use_existing_cu, this_cu->cu must be NULL. */
7637 gdb_assert (this_cu->cu == NULL);
7638 m_new_cu.reset (new dwarf2_cu (this_cu));
7639 cu = m_new_cu.get ();
7640 }
7641
7642 /* Get the header. */
7643 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7644 {
7645 /* We already have the header, there's no need to read it in again. */
7646 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7647 }
7648 else
7649 {
7650 if (this_cu->is_debug_types)
7651 {
7652 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7653 &cu->header, section,
7654 abbrev_section, info_ptr,
7655 rcuh_kind::TYPE);
7656
7657 /* Since per_cu is the first member of struct signatured_type,
7658 we can go from a pointer to one to a pointer to the other. */
7659 sig_type = (struct signatured_type *) this_cu;
7660 gdb_assert (sig_type->signature == cu->header.signature);
7661 gdb_assert (sig_type->type_offset_in_tu
7662 == cu->header.type_cu_offset_in_tu);
7663 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7664
7665 /* LENGTH has not been set yet for type units if we're
7666 using .gdb_index. */
7667 this_cu->length = get_cu_length (&cu->header);
7668
7669 /* Establish the type offset that can be used to lookup the type. */
7670 sig_type->type_offset_in_section =
7671 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7672
7673 this_cu->dwarf_version = cu->header.version;
7674 }
7675 else
7676 {
7677 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7678 &cu->header, section,
7679 abbrev_section,
7680 info_ptr,
7681 rcuh_kind::COMPILE);
7682
7683 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7684 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7685 this_cu->dwarf_version = cu->header.version;
7686 }
7687 }
7688
7689 /* Skip dummy compilation units. */
7690 if (info_ptr >= begin_info_ptr + this_cu->length
7691 || peek_abbrev_code (abfd, info_ptr) == 0)
7692 {
7693 dummy_p = true;
7694 return;
7695 }
7696
7697 /* If we don't have them yet, read the abbrevs for this compilation unit.
7698 And if we need to read them now, make sure they're freed when we're
7699 done. */
7700 if (abbrev_table != NULL)
7701 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7702 else
7703 {
7704 m_abbrev_table_holder
7705 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7706 cu->header.abbrev_sect_off);
7707 abbrev_table = m_abbrev_table_holder.get ();
7708 }
7709
7710 /* Read the top level CU/TU die. */
7711 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7712 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
7713
7714 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7715 {
7716 dummy_p = true;
7717 return;
7718 }
7719
7720 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7721 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7722 table from the DWO file and pass the ownership over to us. It will be
7723 referenced from READER, so we must make sure to free it after we're done
7724 with READER.
7725
7726 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7727 DWO CU, that this test will fail (the attribute will not be present). */
7728 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7729 if (dwo_name != nullptr)
7730 {
7731 struct dwo_unit *dwo_unit;
7732 struct die_info *dwo_comp_unit_die;
7733
7734 if (has_children)
7735 {
7736 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7737 " has children (offset %s) [in module %s]"),
7738 sect_offset_str (this_cu->sect_off),
7739 bfd_get_filename (abfd));
7740 }
7741 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7742 if (dwo_unit != NULL)
7743 {
7744 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7745 comp_unit_die, NULL,
7746 this, &info_ptr,
7747 &dwo_comp_unit_die, &has_children,
7748 &m_dwo_abbrev_table) == 0)
7749 {
7750 /* Dummy die. */
7751 dummy_p = true;
7752 return;
7753 }
7754 comp_unit_die = dwo_comp_unit_die;
7755 }
7756 else
7757 {
7758 /* Yikes, we couldn't find the rest of the DIE, we only have
7759 the stub. A complaint has already been logged. There's
7760 not much more we can do except pass on the stub DIE to
7761 die_reader_func. We don't want to throw an error on bad
7762 debug info. */
7763 }
7764 }
7765 }
7766
7767 cutu_reader::~cutu_reader ()
7768 {
7769 /* Done, clean up. */
7770 if (m_new_cu != NULL && m_keep && !dummy_p)
7771 {
7772 struct dwarf2_per_objfile *dwarf2_per_objfile
7773 = m_this_cu->dwarf2_per_objfile;
7774 /* Link this CU into read_in_chain. */
7775 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7776 dwarf2_per_objfile->read_in_chain = m_this_cu;
7777 /* The chain owns it now. */
7778 m_new_cu.release ();
7779 }
7780 }
7781
7782 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7783 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7784 assumed to have already done the lookup to find the DWO file).
7785
7786 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7787 THIS_CU->is_debug_types, but nothing else.
7788
7789 We fill in THIS_CU->length.
7790
7791 THIS_CU->cu is always freed when done.
7792 This is done in order to not leave THIS_CU->cu in a state where we have
7793 to care whether it refers to the "main" CU or the DWO CU.
7794
7795 When parent_cu is passed, it is used to provide a default value for
7796 str_offsets_base and addr_base from the parent. */
7797
7798 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7799 struct dwarf2_cu *parent_cu,
7800 struct dwo_file *dwo_file)
7801 : die_reader_specs {},
7802 m_this_cu (this_cu)
7803 {
7804 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7805 struct objfile *objfile = dwarf2_per_objfile->objfile;
7806 struct dwarf2_section_info *section = this_cu->section;
7807 bfd *abfd = get_section_bfd_owner (section);
7808 struct dwarf2_section_info *abbrev_section;
7809 const gdb_byte *begin_info_ptr, *info_ptr;
7810 int has_children;
7811
7812 if (dwarf_die_debug)
7813 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7814 this_cu->is_debug_types ? "type" : "comp",
7815 sect_offset_str (this_cu->sect_off));
7816
7817 gdb_assert (this_cu->cu == NULL);
7818
7819 abbrev_section = (dwo_file != NULL
7820 ? &dwo_file->sections.abbrev
7821 : get_abbrev_section_for_cu (this_cu));
7822
7823 /* This is cheap if the section is already read in. */
7824 dwarf2_read_section (objfile, section);
7825
7826 m_new_cu.reset (new dwarf2_cu (this_cu));
7827
7828 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7829 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7830 &m_new_cu->header, section,
7831 abbrev_section, info_ptr,
7832 (this_cu->is_debug_types
7833 ? rcuh_kind::TYPE
7834 : rcuh_kind::COMPILE));
7835
7836 if (parent_cu != nullptr)
7837 {
7838 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7839 m_new_cu->addr_base = parent_cu->addr_base;
7840 }
7841 this_cu->length = get_cu_length (&m_new_cu->header);
7842
7843 /* Skip dummy compilation units. */
7844 if (info_ptr >= begin_info_ptr + this_cu->length
7845 || peek_abbrev_code (abfd, info_ptr) == 0)
7846 {
7847 dummy_p = true;
7848 return;
7849 }
7850
7851 m_abbrev_table_holder
7852 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7853 m_new_cu->header.abbrev_sect_off);
7854
7855 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7856 m_abbrev_table_holder.get ());
7857 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
7858 }
7859
7860 \f
7861 /* Type Unit Groups.
7862
7863 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7864 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7865 so that all types coming from the same compilation (.o file) are grouped
7866 together. A future step could be to put the types in the same symtab as
7867 the CU the types ultimately came from. */
7868
7869 static hashval_t
7870 hash_type_unit_group (const void *item)
7871 {
7872 const struct type_unit_group *tu_group
7873 = (const struct type_unit_group *) item;
7874
7875 return hash_stmt_list_entry (&tu_group->hash);
7876 }
7877
7878 static int
7879 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7880 {
7881 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7882 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7883
7884 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7885 }
7886
7887 /* Allocate a hash table for type unit groups. */
7888
7889 static htab_t
7890 allocate_type_unit_groups_table (struct objfile *objfile)
7891 {
7892 return htab_create_alloc_ex (3,
7893 hash_type_unit_group,
7894 eq_type_unit_group,
7895 NULL,
7896 &objfile->objfile_obstack,
7897 hashtab_obstack_allocate,
7898 dummy_obstack_deallocate);
7899 }
7900
7901 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7902 partial symtabs. We combine several TUs per psymtab to not let the size
7903 of any one psymtab grow too big. */
7904 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7905 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7906
7907 /* Helper routine for get_type_unit_group.
7908 Create the type_unit_group object used to hold one or more TUs. */
7909
7910 static struct type_unit_group *
7911 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7912 {
7913 struct dwarf2_per_objfile *dwarf2_per_objfile
7914 = cu->per_cu->dwarf2_per_objfile;
7915 struct objfile *objfile = dwarf2_per_objfile->objfile;
7916 struct dwarf2_per_cu_data *per_cu;
7917 struct type_unit_group *tu_group;
7918
7919 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7920 struct type_unit_group);
7921 per_cu = &tu_group->per_cu;
7922 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7923
7924 if (dwarf2_per_objfile->using_index)
7925 {
7926 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7927 struct dwarf2_per_cu_quick_data);
7928 }
7929 else
7930 {
7931 unsigned int line_offset = to_underlying (line_offset_struct);
7932 dwarf2_psymtab *pst;
7933 std::string name;
7934
7935 /* Give the symtab a useful name for debug purposes. */
7936 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7937 name = string_printf ("<type_units_%d>",
7938 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7939 else
7940 name = string_printf ("<type_units_at_0x%x>", line_offset);
7941
7942 pst = create_partial_symtab (per_cu, name.c_str ());
7943 pst->anonymous = true;
7944 }
7945
7946 tu_group->hash.dwo_unit = cu->dwo_unit;
7947 tu_group->hash.line_sect_off = line_offset_struct;
7948
7949 return tu_group;
7950 }
7951
7952 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7953 STMT_LIST is a DW_AT_stmt_list attribute. */
7954
7955 static struct type_unit_group *
7956 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7957 {
7958 struct dwarf2_per_objfile *dwarf2_per_objfile
7959 = cu->per_cu->dwarf2_per_objfile;
7960 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7961 struct type_unit_group *tu_group;
7962 void **slot;
7963 unsigned int line_offset;
7964 struct type_unit_group type_unit_group_for_lookup;
7965
7966 if (dwarf2_per_objfile->type_unit_groups == NULL)
7967 {
7968 dwarf2_per_objfile->type_unit_groups =
7969 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7970 }
7971
7972 /* Do we need to create a new group, or can we use an existing one? */
7973
7974 if (stmt_list)
7975 {
7976 line_offset = DW_UNSND (stmt_list);
7977 ++tu_stats->nr_symtab_sharers;
7978 }
7979 else
7980 {
7981 /* Ugh, no stmt_list. Rare, but we have to handle it.
7982 We can do various things here like create one group per TU or
7983 spread them over multiple groups to split up the expansion work.
7984 To avoid worst case scenarios (too many groups or too large groups)
7985 we, umm, group them in bunches. */
7986 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7987 | (tu_stats->nr_stmt_less_type_units
7988 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7989 ++tu_stats->nr_stmt_less_type_units;
7990 }
7991
7992 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7993 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7994 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7995 &type_unit_group_for_lookup, INSERT);
7996 if (*slot != NULL)
7997 {
7998 tu_group = (struct type_unit_group *) *slot;
7999 gdb_assert (tu_group != NULL);
8000 }
8001 else
8002 {
8003 sect_offset line_offset_struct = (sect_offset) line_offset;
8004 tu_group = create_type_unit_group (cu, line_offset_struct);
8005 *slot = tu_group;
8006 ++tu_stats->nr_symtabs;
8007 }
8008
8009 return tu_group;
8010 }
8011 \f
8012 /* Partial symbol tables. */
8013
8014 /* Create a psymtab named NAME and assign it to PER_CU.
8015
8016 The caller must fill in the following details:
8017 dirname, textlow, texthigh. */
8018
8019 static dwarf2_psymtab *
8020 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8021 {
8022 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8023 dwarf2_psymtab *pst;
8024
8025 pst = new dwarf2_psymtab (name, objfile, 0);
8026
8027 pst->psymtabs_addrmap_supported = true;
8028
8029 /* This is the glue that links PST into GDB's symbol API. */
8030 pst->per_cu_data = per_cu;
8031 per_cu->v.psymtab = pst;
8032
8033 return pst;
8034 }
8035
8036 /* DIE reader function for process_psymtab_comp_unit. */
8037
8038 static void
8039 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8040 const gdb_byte *info_ptr,
8041 struct die_info *comp_unit_die,
8042 int has_children,
8043 int want_partial_unit,
8044 enum language pretend_language)
8045 {
8046 struct dwarf2_cu *cu = reader->cu;
8047 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8048 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8049 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8050 CORE_ADDR baseaddr;
8051 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8052 dwarf2_psymtab *pst;
8053 enum pc_bounds_kind cu_bounds_kind;
8054 const char *filename;
8055
8056 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
8057 return;
8058
8059 gdb_assert (! per_cu->is_debug_types);
8060
8061 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
8062
8063 /* Allocate a new partial symbol table structure. */
8064 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8065 if (filename == NULL)
8066 filename = "";
8067
8068 pst = create_partial_symtab (per_cu, filename);
8069
8070 /* This must be done before calling dwarf2_build_include_psymtabs. */
8071 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8072
8073 baseaddr = objfile->text_section_offset ();
8074
8075 dwarf2_find_base_address (comp_unit_die, cu);
8076
8077 /* Possibly set the default values of LOWPC and HIGHPC from
8078 `DW_AT_ranges'. */
8079 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8080 &best_highpc, cu, pst);
8081 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8082 {
8083 CORE_ADDR low
8084 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8085 - baseaddr);
8086 CORE_ADDR high
8087 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8088 - baseaddr - 1);
8089 /* Store the contiguous range if it is not empty; it can be
8090 empty for CUs with no code. */
8091 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8092 low, high, pst);
8093 }
8094
8095 /* Check if comp unit has_children.
8096 If so, read the rest of the partial symbols from this comp unit.
8097 If not, there's no more debug_info for this comp unit. */
8098 if (has_children)
8099 {
8100 struct partial_die_info *first_die;
8101 CORE_ADDR lowpc, highpc;
8102
8103 lowpc = ((CORE_ADDR) -1);
8104 highpc = ((CORE_ADDR) 0);
8105
8106 first_die = load_partial_dies (reader, info_ptr, 1);
8107
8108 scan_partial_symbols (first_die, &lowpc, &highpc,
8109 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8110
8111 /* If we didn't find a lowpc, set it to highpc to avoid
8112 complaints from `maint check'. */
8113 if (lowpc == ((CORE_ADDR) -1))
8114 lowpc = highpc;
8115
8116 /* If the compilation unit didn't have an explicit address range,
8117 then use the information extracted from its child dies. */
8118 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8119 {
8120 best_lowpc = lowpc;
8121 best_highpc = highpc;
8122 }
8123 }
8124 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8125 best_lowpc + baseaddr)
8126 - baseaddr);
8127 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8128 best_highpc + baseaddr)
8129 - baseaddr);
8130
8131 end_psymtab_common (objfile, pst);
8132
8133 if (!cu->per_cu->imported_symtabs_empty ())
8134 {
8135 int i;
8136 int len = cu->per_cu->imported_symtabs_size ();
8137
8138 /* Fill in 'dependencies' here; we fill in 'users' in a
8139 post-pass. */
8140 pst->number_of_dependencies = len;
8141 pst->dependencies
8142 = objfile->partial_symtabs->allocate_dependencies (len);
8143 for (i = 0; i < len; ++i)
8144 {
8145 pst->dependencies[i]
8146 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8147 }
8148
8149 cu->per_cu->imported_symtabs_free ();
8150 }
8151
8152 /* Get the list of files included in the current compilation unit,
8153 and build a psymtab for each of them. */
8154 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8155
8156 if (dwarf_read_debug)
8157 fprintf_unfiltered (gdb_stdlog,
8158 "Psymtab for %s unit @%s: %s - %s"
8159 ", %d global, %d static syms\n",
8160 per_cu->is_debug_types ? "type" : "comp",
8161 sect_offset_str (per_cu->sect_off),
8162 paddress (gdbarch, pst->text_low (objfile)),
8163 paddress (gdbarch, pst->text_high (objfile)),
8164 pst->n_global_syms, pst->n_static_syms);
8165 }
8166
8167 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8168 Process compilation unit THIS_CU for a psymtab. */
8169
8170 static void
8171 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8172 int want_partial_unit,
8173 enum language pretend_language)
8174 {
8175 /* If this compilation unit was already read in, free the
8176 cached copy in order to read it in again. This is
8177 necessary because we skipped some symbols when we first
8178 read in the compilation unit (see load_partial_dies).
8179 This problem could be avoided, but the benefit is unclear. */
8180 if (this_cu->cu != NULL)
8181 free_one_cached_comp_unit (this_cu);
8182
8183 cutu_reader reader (this_cu, NULL, 0, 0, false);
8184
8185 if (reader.dummy_p)
8186 {
8187 /* Nothing. */
8188 }
8189 else if (this_cu->is_debug_types)
8190 build_type_psymtabs_reader (&reader, reader.info_ptr, reader.comp_unit_die,
8191 reader.has_children);
8192 else
8193 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
8194 reader.comp_unit_die,
8195 reader.has_children,
8196 want_partial_unit,
8197 pretend_language);
8198
8199 /* Age out any secondary CUs. */
8200 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8201 }
8202
8203 /* Reader function for build_type_psymtabs. */
8204
8205 static void
8206 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8207 const gdb_byte *info_ptr,
8208 struct die_info *type_unit_die,
8209 int has_children)
8210 {
8211 struct dwarf2_per_objfile *dwarf2_per_objfile
8212 = reader->cu->per_cu->dwarf2_per_objfile;
8213 struct objfile *objfile = dwarf2_per_objfile->objfile;
8214 struct dwarf2_cu *cu = reader->cu;
8215 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8216 struct signatured_type *sig_type;
8217 struct type_unit_group *tu_group;
8218 struct attribute *attr;
8219 struct partial_die_info *first_die;
8220 CORE_ADDR lowpc, highpc;
8221 dwarf2_psymtab *pst;
8222
8223 gdb_assert (per_cu->is_debug_types);
8224 sig_type = (struct signatured_type *) per_cu;
8225
8226 if (! has_children)
8227 return;
8228
8229 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8230 tu_group = get_type_unit_group (cu, attr);
8231
8232 if (tu_group->tus == nullptr)
8233 tu_group->tus = new std::vector<signatured_type *>;
8234 tu_group->tus->push_back (sig_type);
8235
8236 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8237 pst = create_partial_symtab (per_cu, "");
8238 pst->anonymous = true;
8239
8240 first_die = load_partial_dies (reader, info_ptr, 1);
8241
8242 lowpc = (CORE_ADDR) -1;
8243 highpc = (CORE_ADDR) 0;
8244 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8245
8246 end_psymtab_common (objfile, pst);
8247 }
8248
8249 /* Struct used to sort TUs by their abbreviation table offset. */
8250
8251 struct tu_abbrev_offset
8252 {
8253 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8254 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8255 {}
8256
8257 signatured_type *sig_type;
8258 sect_offset abbrev_offset;
8259 };
8260
8261 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8262
8263 static bool
8264 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8265 const struct tu_abbrev_offset &b)
8266 {
8267 return a.abbrev_offset < b.abbrev_offset;
8268 }
8269
8270 /* Efficiently read all the type units.
8271 This does the bulk of the work for build_type_psymtabs.
8272
8273 The efficiency is because we sort TUs by the abbrev table they use and
8274 only read each abbrev table once. In one program there are 200K TUs
8275 sharing 8K abbrev tables.
8276
8277 The main purpose of this function is to support building the
8278 dwarf2_per_objfile->type_unit_groups table.
8279 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8280 can collapse the search space by grouping them by stmt_list.
8281 The savings can be significant, in the same program from above the 200K TUs
8282 share 8K stmt_list tables.
8283
8284 FUNC is expected to call get_type_unit_group, which will create the
8285 struct type_unit_group if necessary and add it to
8286 dwarf2_per_objfile->type_unit_groups. */
8287
8288 static void
8289 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8290 {
8291 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8292 abbrev_table_up abbrev_table;
8293 sect_offset abbrev_offset;
8294
8295 /* It's up to the caller to not call us multiple times. */
8296 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8297
8298 if (dwarf2_per_objfile->all_type_units.empty ())
8299 return;
8300
8301 /* TUs typically share abbrev tables, and there can be way more TUs than
8302 abbrev tables. Sort by abbrev table to reduce the number of times we
8303 read each abbrev table in.
8304 Alternatives are to punt or to maintain a cache of abbrev tables.
8305 This is simpler and efficient enough for now.
8306
8307 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8308 symtab to use). Typically TUs with the same abbrev offset have the same
8309 stmt_list value too so in practice this should work well.
8310
8311 The basic algorithm here is:
8312
8313 sort TUs by abbrev table
8314 for each TU with same abbrev table:
8315 read abbrev table if first user
8316 read TU top level DIE
8317 [IWBN if DWO skeletons had DW_AT_stmt_list]
8318 call FUNC */
8319
8320 if (dwarf_read_debug)
8321 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8322
8323 /* Sort in a separate table to maintain the order of all_type_units
8324 for .gdb_index: TU indices directly index all_type_units. */
8325 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8326 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8327
8328 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8329 sorted_by_abbrev.emplace_back
8330 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8331 sig_type->per_cu.section,
8332 sig_type->per_cu.sect_off));
8333
8334 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8335 sort_tu_by_abbrev_offset);
8336
8337 abbrev_offset = (sect_offset) ~(unsigned) 0;
8338
8339 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8340 {
8341 /* Switch to the next abbrev table if necessary. */
8342 if (abbrev_table == NULL
8343 || tu.abbrev_offset != abbrev_offset)
8344 {
8345 abbrev_offset = tu.abbrev_offset;
8346 abbrev_table =
8347 abbrev_table_read_table (dwarf2_per_objfile,
8348 &dwarf2_per_objfile->abbrev,
8349 abbrev_offset);
8350 ++tu_stats->nr_uniq_abbrev_tables;
8351 }
8352
8353 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
8354 0, 0, false);
8355 if (!reader.dummy_p)
8356 build_type_psymtabs_reader (&reader, reader.info_ptr,
8357 reader.comp_unit_die,
8358 reader.has_children);
8359 }
8360 }
8361
8362 /* Print collected type unit statistics. */
8363
8364 static void
8365 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8366 {
8367 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8368
8369 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8370 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8371 dwarf2_per_objfile->all_type_units.size ());
8372 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8373 tu_stats->nr_uniq_abbrev_tables);
8374 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8375 tu_stats->nr_symtabs);
8376 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8377 tu_stats->nr_symtab_sharers);
8378 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8379 tu_stats->nr_stmt_less_type_units);
8380 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8381 tu_stats->nr_all_type_units_reallocs);
8382 }
8383
8384 /* Traversal function for build_type_psymtabs. */
8385
8386 static int
8387 build_type_psymtab_dependencies (void **slot, void *info)
8388 {
8389 struct dwarf2_per_objfile *dwarf2_per_objfile
8390 = (struct dwarf2_per_objfile *) info;
8391 struct objfile *objfile = dwarf2_per_objfile->objfile;
8392 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8393 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8394 dwarf2_psymtab *pst = per_cu->v.psymtab;
8395 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8396 int i;
8397
8398 gdb_assert (len > 0);
8399 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8400
8401 pst->number_of_dependencies = len;
8402 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8403 for (i = 0; i < len; ++i)
8404 {
8405 struct signatured_type *iter = tu_group->tus->at (i);
8406 gdb_assert (iter->per_cu.is_debug_types);
8407 pst->dependencies[i] = iter->per_cu.v.psymtab;
8408 iter->type_unit_group = tu_group;
8409 }
8410
8411 delete tu_group->tus;
8412 tu_group->tus = nullptr;
8413
8414 return 1;
8415 }
8416
8417 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8418 Build partial symbol tables for the .debug_types comp-units. */
8419
8420 static void
8421 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8422 {
8423 if (! create_all_type_units (dwarf2_per_objfile))
8424 return;
8425
8426 build_type_psymtabs_1 (dwarf2_per_objfile);
8427 }
8428
8429 /* Traversal function for process_skeletonless_type_unit.
8430 Read a TU in a DWO file and build partial symbols for it. */
8431
8432 static int
8433 process_skeletonless_type_unit (void **slot, void *info)
8434 {
8435 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8436 struct dwarf2_per_objfile *dwarf2_per_objfile
8437 = (struct dwarf2_per_objfile *) info;
8438 struct signatured_type find_entry, *entry;
8439
8440 /* If this TU doesn't exist in the global table, add it and read it in. */
8441
8442 if (dwarf2_per_objfile->signatured_types == NULL)
8443 {
8444 dwarf2_per_objfile->signatured_types
8445 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8446 }
8447
8448 find_entry.signature = dwo_unit->signature;
8449 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8450 INSERT);
8451 /* If we've already seen this type there's nothing to do. What's happening
8452 is we're doing our own version of comdat-folding here. */
8453 if (*slot != NULL)
8454 return 1;
8455
8456 /* This does the job that create_all_type_units would have done for
8457 this TU. */
8458 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8459 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8460 *slot = entry;
8461
8462 /* This does the job that build_type_psymtabs_1 would have done. */
8463 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
8464 if (!reader.dummy_p)
8465 build_type_psymtabs_reader (&reader, reader.info_ptr,
8466 reader.comp_unit_die, reader.has_children);
8467
8468 return 1;
8469 }
8470
8471 /* Traversal function for process_skeletonless_type_units. */
8472
8473 static int
8474 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8475 {
8476 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8477
8478 if (dwo_file->tus != NULL)
8479 {
8480 htab_traverse_noresize (dwo_file->tus,
8481 process_skeletonless_type_unit, info);
8482 }
8483
8484 return 1;
8485 }
8486
8487 /* Scan all TUs of DWO files, verifying we've processed them.
8488 This is needed in case a TU was emitted without its skeleton.
8489 Note: This can't be done until we know what all the DWO files are. */
8490
8491 static void
8492 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8493 {
8494 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8495 if (get_dwp_file (dwarf2_per_objfile) == NULL
8496 && dwarf2_per_objfile->dwo_files != NULL)
8497 {
8498 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8499 process_dwo_file_for_skeletonless_type_units,
8500 dwarf2_per_objfile);
8501 }
8502 }
8503
8504 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8505
8506 static void
8507 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8508 {
8509 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8510 {
8511 dwarf2_psymtab *pst = per_cu->v.psymtab;
8512
8513 if (pst == NULL)
8514 continue;
8515
8516 for (int j = 0; j < pst->number_of_dependencies; ++j)
8517 {
8518 /* Set the 'user' field only if it is not already set. */
8519 if (pst->dependencies[j]->user == NULL)
8520 pst->dependencies[j]->user = pst;
8521 }
8522 }
8523 }
8524
8525 /* Build the partial symbol table by doing a quick pass through the
8526 .debug_info and .debug_abbrev sections. */
8527
8528 static void
8529 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8530 {
8531 struct objfile *objfile = dwarf2_per_objfile->objfile;
8532
8533 if (dwarf_read_debug)
8534 {
8535 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8536 objfile_name (objfile));
8537 }
8538
8539 dwarf2_per_objfile->reading_partial_symbols = 1;
8540
8541 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8542
8543 /* Any cached compilation units will be linked by the per-objfile
8544 read_in_chain. Make sure to free them when we're done. */
8545 free_cached_comp_units freer (dwarf2_per_objfile);
8546
8547 build_type_psymtabs (dwarf2_per_objfile);
8548
8549 create_all_comp_units (dwarf2_per_objfile);
8550
8551 /* Create a temporary address map on a temporary obstack. We later
8552 copy this to the final obstack. */
8553 auto_obstack temp_obstack;
8554
8555 scoped_restore save_psymtabs_addrmap
8556 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8557 addrmap_create_mutable (&temp_obstack));
8558
8559 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8560 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8561
8562 /* This has to wait until we read the CUs, we need the list of DWOs. */
8563 process_skeletonless_type_units (dwarf2_per_objfile);
8564
8565 /* Now that all TUs have been processed we can fill in the dependencies. */
8566 if (dwarf2_per_objfile->type_unit_groups != NULL)
8567 {
8568 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8569 build_type_psymtab_dependencies, dwarf2_per_objfile);
8570 }
8571
8572 if (dwarf_read_debug)
8573 print_tu_stats (dwarf2_per_objfile);
8574
8575 set_partial_user (dwarf2_per_objfile);
8576
8577 objfile->partial_symtabs->psymtabs_addrmap
8578 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8579 objfile->partial_symtabs->obstack ());
8580 /* At this point we want to keep the address map. */
8581 save_psymtabs_addrmap.release ();
8582
8583 if (dwarf_read_debug)
8584 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8585 objfile_name (objfile));
8586 }
8587
8588 /* Load the partial DIEs for a secondary CU into memory.
8589 This is also used when rereading a primary CU with load_all_dies. */
8590
8591 static void
8592 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8593 {
8594 cutu_reader reader (this_cu, NULL, 1, 1, false);
8595
8596 if (!reader.dummy_p)
8597 {
8598 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8599 language_minimal);
8600
8601 /* Check if comp unit has_children.
8602 If so, read the rest of the partial symbols from this comp unit.
8603 If not, there's no more debug_info for this comp unit. */
8604 if (reader.has_children)
8605 load_partial_dies (&reader, reader.info_ptr, 0);
8606 }
8607 }
8608
8609 static void
8610 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8611 struct dwarf2_section_info *section,
8612 struct dwarf2_section_info *abbrev_section,
8613 unsigned int is_dwz)
8614 {
8615 const gdb_byte *info_ptr;
8616 struct objfile *objfile = dwarf2_per_objfile->objfile;
8617
8618 if (dwarf_read_debug)
8619 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8620 get_section_name (section),
8621 get_section_file_name (section));
8622
8623 dwarf2_read_section (objfile, section);
8624
8625 info_ptr = section->buffer;
8626
8627 while (info_ptr < section->buffer + section->size)
8628 {
8629 struct dwarf2_per_cu_data *this_cu;
8630
8631 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8632
8633 comp_unit_head cu_header;
8634 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8635 abbrev_section, info_ptr,
8636 rcuh_kind::COMPILE);
8637
8638 /* Save the compilation unit for later lookup. */
8639 if (cu_header.unit_type != DW_UT_type)
8640 {
8641 this_cu = XOBNEW (&objfile->objfile_obstack,
8642 struct dwarf2_per_cu_data);
8643 memset (this_cu, 0, sizeof (*this_cu));
8644 }
8645 else
8646 {
8647 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8648 struct signatured_type);
8649 memset (sig_type, 0, sizeof (*sig_type));
8650 sig_type->signature = cu_header.signature;
8651 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8652 this_cu = &sig_type->per_cu;
8653 }
8654 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8655 this_cu->sect_off = sect_off;
8656 this_cu->length = cu_header.length + cu_header.initial_length_size;
8657 this_cu->is_dwz = is_dwz;
8658 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8659 this_cu->section = section;
8660
8661 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8662
8663 info_ptr = info_ptr + this_cu->length;
8664 }
8665 }
8666
8667 /* Create a list of all compilation units in OBJFILE.
8668 This is only done for -readnow and building partial symtabs. */
8669
8670 static void
8671 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8672 {
8673 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8674 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8675 &dwarf2_per_objfile->abbrev, 0);
8676
8677 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8678 if (dwz != NULL)
8679 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8680 1);
8681 }
8682
8683 /* Process all loaded DIEs for compilation unit CU, starting at
8684 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8685 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8686 DW_AT_ranges). See the comments of add_partial_subprogram on how
8687 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8688
8689 static void
8690 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8691 CORE_ADDR *highpc, int set_addrmap,
8692 struct dwarf2_cu *cu)
8693 {
8694 struct partial_die_info *pdi;
8695
8696 /* Now, march along the PDI's, descending into ones which have
8697 interesting children but skipping the children of the other ones,
8698 until we reach the end of the compilation unit. */
8699
8700 pdi = first_die;
8701
8702 while (pdi != NULL)
8703 {
8704 pdi->fixup (cu);
8705
8706 /* Anonymous namespaces or modules have no name but have interesting
8707 children, so we need to look at them. Ditto for anonymous
8708 enums. */
8709
8710 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8711 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8712 || pdi->tag == DW_TAG_imported_unit
8713 || pdi->tag == DW_TAG_inlined_subroutine)
8714 {
8715 switch (pdi->tag)
8716 {
8717 case DW_TAG_subprogram:
8718 case DW_TAG_inlined_subroutine:
8719 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8720 break;
8721 case DW_TAG_constant:
8722 case DW_TAG_variable:
8723 case DW_TAG_typedef:
8724 case DW_TAG_union_type:
8725 if (!pdi->is_declaration)
8726 {
8727 add_partial_symbol (pdi, cu);
8728 }
8729 break;
8730 case DW_TAG_class_type:
8731 case DW_TAG_interface_type:
8732 case DW_TAG_structure_type:
8733 if (!pdi->is_declaration)
8734 {
8735 add_partial_symbol (pdi, cu);
8736 }
8737 if ((cu->language == language_rust
8738 || cu->language == language_cplus) && pdi->has_children)
8739 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8740 set_addrmap, cu);
8741 break;
8742 case DW_TAG_enumeration_type:
8743 if (!pdi->is_declaration)
8744 add_partial_enumeration (pdi, cu);
8745 break;
8746 case DW_TAG_base_type:
8747 case DW_TAG_subrange_type:
8748 /* File scope base type definitions are added to the partial
8749 symbol table. */
8750 add_partial_symbol (pdi, cu);
8751 break;
8752 case DW_TAG_namespace:
8753 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8754 break;
8755 case DW_TAG_module:
8756 if (!pdi->is_declaration)
8757 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8758 break;
8759 case DW_TAG_imported_unit:
8760 {
8761 struct dwarf2_per_cu_data *per_cu;
8762
8763 /* For now we don't handle imported units in type units. */
8764 if (cu->per_cu->is_debug_types)
8765 {
8766 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8767 " supported in type units [in module %s]"),
8768 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8769 }
8770
8771 per_cu = dwarf2_find_containing_comp_unit
8772 (pdi->d.sect_off, pdi->is_dwz,
8773 cu->per_cu->dwarf2_per_objfile);
8774
8775 /* Go read the partial unit, if needed. */
8776 if (per_cu->v.psymtab == NULL)
8777 process_psymtab_comp_unit (per_cu, 1, cu->language);
8778
8779 cu->per_cu->imported_symtabs_push (per_cu);
8780 }
8781 break;
8782 case DW_TAG_imported_declaration:
8783 add_partial_symbol (pdi, cu);
8784 break;
8785 default:
8786 break;
8787 }
8788 }
8789
8790 /* If the die has a sibling, skip to the sibling. */
8791
8792 pdi = pdi->die_sibling;
8793 }
8794 }
8795
8796 /* Functions used to compute the fully scoped name of a partial DIE.
8797
8798 Normally, this is simple. For C++, the parent DIE's fully scoped
8799 name is concatenated with "::" and the partial DIE's name.
8800 Enumerators are an exception; they use the scope of their parent
8801 enumeration type, i.e. the name of the enumeration type is not
8802 prepended to the enumerator.
8803
8804 There are two complexities. One is DW_AT_specification; in this
8805 case "parent" means the parent of the target of the specification,
8806 instead of the direct parent of the DIE. The other is compilers
8807 which do not emit DW_TAG_namespace; in this case we try to guess
8808 the fully qualified name of structure types from their members'
8809 linkage names. This must be done using the DIE's children rather
8810 than the children of any DW_AT_specification target. We only need
8811 to do this for structures at the top level, i.e. if the target of
8812 any DW_AT_specification (if any; otherwise the DIE itself) does not
8813 have a parent. */
8814
8815 /* Compute the scope prefix associated with PDI's parent, in
8816 compilation unit CU. The result will be allocated on CU's
8817 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8818 field. NULL is returned if no prefix is necessary. */
8819 static const char *
8820 partial_die_parent_scope (struct partial_die_info *pdi,
8821 struct dwarf2_cu *cu)
8822 {
8823 const char *grandparent_scope;
8824 struct partial_die_info *parent, *real_pdi;
8825
8826 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8827 then this means the parent of the specification DIE. */
8828
8829 real_pdi = pdi;
8830 while (real_pdi->has_specification)
8831 {
8832 auto res = find_partial_die (real_pdi->spec_offset,
8833 real_pdi->spec_is_dwz, cu);
8834 real_pdi = res.pdi;
8835 cu = res.cu;
8836 }
8837
8838 parent = real_pdi->die_parent;
8839 if (parent == NULL)
8840 return NULL;
8841
8842 if (parent->scope_set)
8843 return parent->scope;
8844
8845 parent->fixup (cu);
8846
8847 grandparent_scope = partial_die_parent_scope (parent, cu);
8848
8849 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8850 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8851 Work around this problem here. */
8852 if (cu->language == language_cplus
8853 && parent->tag == DW_TAG_namespace
8854 && strcmp (parent->name, "::") == 0
8855 && grandparent_scope == NULL)
8856 {
8857 parent->scope = NULL;
8858 parent->scope_set = 1;
8859 return NULL;
8860 }
8861
8862 /* Nested subroutines in Fortran get a prefix. */
8863 if (pdi->tag == DW_TAG_enumerator)
8864 /* Enumerators should not get the name of the enumeration as a prefix. */
8865 parent->scope = grandparent_scope;
8866 else if (parent->tag == DW_TAG_namespace
8867 || parent->tag == DW_TAG_module
8868 || parent->tag == DW_TAG_structure_type
8869 || parent->tag == DW_TAG_class_type
8870 || parent->tag == DW_TAG_interface_type
8871 || parent->tag == DW_TAG_union_type
8872 || parent->tag == DW_TAG_enumeration_type
8873 || (cu->language == language_fortran
8874 && parent->tag == DW_TAG_subprogram
8875 && pdi->tag == DW_TAG_subprogram))
8876 {
8877 if (grandparent_scope == NULL)
8878 parent->scope = parent->name;
8879 else
8880 parent->scope = typename_concat (&cu->comp_unit_obstack,
8881 grandparent_scope,
8882 parent->name, 0, cu);
8883 }
8884 else
8885 {
8886 /* FIXME drow/2004-04-01: What should we be doing with
8887 function-local names? For partial symbols, we should probably be
8888 ignoring them. */
8889 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8890 dwarf_tag_name (parent->tag),
8891 sect_offset_str (pdi->sect_off));
8892 parent->scope = grandparent_scope;
8893 }
8894
8895 parent->scope_set = 1;
8896 return parent->scope;
8897 }
8898
8899 /* Return the fully scoped name associated with PDI, from compilation unit
8900 CU. The result will be allocated with malloc. */
8901
8902 static gdb::unique_xmalloc_ptr<char>
8903 partial_die_full_name (struct partial_die_info *pdi,
8904 struct dwarf2_cu *cu)
8905 {
8906 const char *parent_scope;
8907
8908 /* If this is a template instantiation, we can not work out the
8909 template arguments from partial DIEs. So, unfortunately, we have
8910 to go through the full DIEs. At least any work we do building
8911 types here will be reused if full symbols are loaded later. */
8912 if (pdi->has_template_arguments)
8913 {
8914 pdi->fixup (cu);
8915
8916 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8917 {
8918 struct die_info *die;
8919 struct attribute attr;
8920 struct dwarf2_cu *ref_cu = cu;
8921
8922 /* DW_FORM_ref_addr is using section offset. */
8923 attr.name = (enum dwarf_attribute) 0;
8924 attr.form = DW_FORM_ref_addr;
8925 attr.u.unsnd = to_underlying (pdi->sect_off);
8926 die = follow_die_ref (NULL, &attr, &ref_cu);
8927
8928 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8929 }
8930 }
8931
8932 parent_scope = partial_die_parent_scope (pdi, cu);
8933 if (parent_scope == NULL)
8934 return NULL;
8935 else
8936 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8937 pdi->name, 0, cu));
8938 }
8939
8940 static void
8941 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8942 {
8943 struct dwarf2_per_objfile *dwarf2_per_objfile
8944 = cu->per_cu->dwarf2_per_objfile;
8945 struct objfile *objfile = dwarf2_per_objfile->objfile;
8946 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8947 CORE_ADDR addr = 0;
8948 const char *actual_name = NULL;
8949 CORE_ADDR baseaddr;
8950
8951 baseaddr = objfile->text_section_offset ();
8952
8953 gdb::unique_xmalloc_ptr<char> built_actual_name
8954 = partial_die_full_name (pdi, cu);
8955 if (built_actual_name != NULL)
8956 actual_name = built_actual_name.get ();
8957
8958 if (actual_name == NULL)
8959 actual_name = pdi->name;
8960
8961 switch (pdi->tag)
8962 {
8963 case DW_TAG_inlined_subroutine:
8964 case DW_TAG_subprogram:
8965 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8966 - baseaddr);
8967 if (pdi->is_external
8968 || cu->language == language_ada
8969 || (cu->language == language_fortran
8970 && pdi->die_parent != NULL
8971 && pdi->die_parent->tag == DW_TAG_subprogram))
8972 {
8973 /* Normally, only "external" DIEs are part of the global scope.
8974 But in Ada and Fortran, we want to be able to access nested
8975 procedures globally. So all Ada and Fortran subprograms are
8976 stored in the global scope. */
8977 add_psymbol_to_list (actual_name,
8978 built_actual_name != NULL,
8979 VAR_DOMAIN, LOC_BLOCK,
8980 SECT_OFF_TEXT (objfile),
8981 psymbol_placement::GLOBAL,
8982 addr,
8983 cu->language, objfile);
8984 }
8985 else
8986 {
8987 add_psymbol_to_list (actual_name,
8988 built_actual_name != NULL,
8989 VAR_DOMAIN, LOC_BLOCK,
8990 SECT_OFF_TEXT (objfile),
8991 psymbol_placement::STATIC,
8992 addr, cu->language, objfile);
8993 }
8994
8995 if (pdi->main_subprogram && actual_name != NULL)
8996 set_objfile_main_name (objfile, actual_name, cu->language);
8997 break;
8998 case DW_TAG_constant:
8999 add_psymbol_to_list (actual_name,
9000 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9001 -1, (pdi->is_external
9002 ? psymbol_placement::GLOBAL
9003 : psymbol_placement::STATIC),
9004 0, cu->language, objfile);
9005 break;
9006 case DW_TAG_variable:
9007 if (pdi->d.locdesc)
9008 addr = decode_locdesc (pdi->d.locdesc, cu);
9009
9010 if (pdi->d.locdesc
9011 && addr == 0
9012 && !dwarf2_per_objfile->has_section_at_zero)
9013 {
9014 /* A global or static variable may also have been stripped
9015 out by the linker if unused, in which case its address
9016 will be nullified; do not add such variables into partial
9017 symbol table then. */
9018 }
9019 else if (pdi->is_external)
9020 {
9021 /* Global Variable.
9022 Don't enter into the minimal symbol tables as there is
9023 a minimal symbol table entry from the ELF symbols already.
9024 Enter into partial symbol table if it has a location
9025 descriptor or a type.
9026 If the location descriptor is missing, new_symbol will create
9027 a LOC_UNRESOLVED symbol, the address of the variable will then
9028 be determined from the minimal symbol table whenever the variable
9029 is referenced.
9030 The address for the partial symbol table entry is not
9031 used by GDB, but it comes in handy for debugging partial symbol
9032 table building. */
9033
9034 if (pdi->d.locdesc || pdi->has_type)
9035 add_psymbol_to_list (actual_name,
9036 built_actual_name != NULL,
9037 VAR_DOMAIN, LOC_STATIC,
9038 SECT_OFF_TEXT (objfile),
9039 psymbol_placement::GLOBAL,
9040 addr, cu->language, objfile);
9041 }
9042 else
9043 {
9044 int has_loc = pdi->d.locdesc != NULL;
9045
9046 /* Static Variable. Skip symbols whose value we cannot know (those
9047 without location descriptors or constant values). */
9048 if (!has_loc && !pdi->has_const_value)
9049 return;
9050
9051 add_psymbol_to_list (actual_name,
9052 built_actual_name != NULL,
9053 VAR_DOMAIN, LOC_STATIC,
9054 SECT_OFF_TEXT (objfile),
9055 psymbol_placement::STATIC,
9056 has_loc ? addr : 0,
9057 cu->language, objfile);
9058 }
9059 break;
9060 case DW_TAG_typedef:
9061 case DW_TAG_base_type:
9062 case DW_TAG_subrange_type:
9063 add_psymbol_to_list (actual_name,
9064 built_actual_name != NULL,
9065 VAR_DOMAIN, LOC_TYPEDEF, -1,
9066 psymbol_placement::STATIC,
9067 0, cu->language, objfile);
9068 break;
9069 case DW_TAG_imported_declaration:
9070 case DW_TAG_namespace:
9071 add_psymbol_to_list (actual_name,
9072 built_actual_name != NULL,
9073 VAR_DOMAIN, LOC_TYPEDEF, -1,
9074 psymbol_placement::GLOBAL,
9075 0, cu->language, objfile);
9076 break;
9077 case DW_TAG_module:
9078 /* With Fortran 77 there might be a "BLOCK DATA" module
9079 available without any name. If so, we skip the module as it
9080 doesn't bring any value. */
9081 if (actual_name != nullptr)
9082 add_psymbol_to_list (actual_name,
9083 built_actual_name != NULL,
9084 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9085 psymbol_placement::GLOBAL,
9086 0, cu->language, objfile);
9087 break;
9088 case DW_TAG_class_type:
9089 case DW_TAG_interface_type:
9090 case DW_TAG_structure_type:
9091 case DW_TAG_union_type:
9092 case DW_TAG_enumeration_type:
9093 /* Skip external references. The DWARF standard says in the section
9094 about "Structure, Union, and Class Type Entries": "An incomplete
9095 structure, union or class type is represented by a structure,
9096 union or class entry that does not have a byte size attribute
9097 and that has a DW_AT_declaration attribute." */
9098 if (!pdi->has_byte_size && pdi->is_declaration)
9099 return;
9100
9101 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9102 static vs. global. */
9103 add_psymbol_to_list (actual_name,
9104 built_actual_name != NULL,
9105 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9106 cu->language == language_cplus
9107 ? psymbol_placement::GLOBAL
9108 : psymbol_placement::STATIC,
9109 0, cu->language, objfile);
9110
9111 break;
9112 case DW_TAG_enumerator:
9113 add_psymbol_to_list (actual_name,
9114 built_actual_name != NULL,
9115 VAR_DOMAIN, LOC_CONST, -1,
9116 cu->language == language_cplus
9117 ? psymbol_placement::GLOBAL
9118 : psymbol_placement::STATIC,
9119 0, cu->language, objfile);
9120 break;
9121 default:
9122 break;
9123 }
9124 }
9125
9126 /* Read a partial die corresponding to a namespace; also, add a symbol
9127 corresponding to that namespace to the symbol table. NAMESPACE is
9128 the name of the enclosing namespace. */
9129
9130 static void
9131 add_partial_namespace (struct partial_die_info *pdi,
9132 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9133 int set_addrmap, struct dwarf2_cu *cu)
9134 {
9135 /* Add a symbol for the namespace. */
9136
9137 add_partial_symbol (pdi, cu);
9138
9139 /* Now scan partial symbols in that namespace. */
9140
9141 if (pdi->has_children)
9142 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9143 }
9144
9145 /* Read a partial die corresponding to a Fortran module. */
9146
9147 static void
9148 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9149 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9150 {
9151 /* Add a symbol for the namespace. */
9152
9153 add_partial_symbol (pdi, cu);
9154
9155 /* Now scan partial symbols in that module. */
9156
9157 if (pdi->has_children)
9158 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9159 }
9160
9161 /* Read a partial die corresponding to a subprogram or an inlined
9162 subprogram and create a partial symbol for that subprogram.
9163 When the CU language allows it, this routine also defines a partial
9164 symbol for each nested subprogram that this subprogram contains.
9165 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9166 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9167
9168 PDI may also be a lexical block, in which case we simply search
9169 recursively for subprograms defined inside that lexical block.
9170 Again, this is only performed when the CU language allows this
9171 type of definitions. */
9172
9173 static void
9174 add_partial_subprogram (struct partial_die_info *pdi,
9175 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9176 int set_addrmap, struct dwarf2_cu *cu)
9177 {
9178 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9179 {
9180 if (pdi->has_pc_info)
9181 {
9182 if (pdi->lowpc < *lowpc)
9183 *lowpc = pdi->lowpc;
9184 if (pdi->highpc > *highpc)
9185 *highpc = pdi->highpc;
9186 if (set_addrmap)
9187 {
9188 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9189 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9190 CORE_ADDR baseaddr;
9191 CORE_ADDR this_highpc;
9192 CORE_ADDR this_lowpc;
9193
9194 baseaddr = objfile->text_section_offset ();
9195 this_lowpc
9196 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9197 pdi->lowpc + baseaddr)
9198 - baseaddr);
9199 this_highpc
9200 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9201 pdi->highpc + baseaddr)
9202 - baseaddr);
9203 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9204 this_lowpc, this_highpc - 1,
9205 cu->per_cu->v.psymtab);
9206 }
9207 }
9208
9209 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9210 {
9211 if (!pdi->is_declaration)
9212 /* Ignore subprogram DIEs that do not have a name, they are
9213 illegal. Do not emit a complaint at this point, we will
9214 do so when we convert this psymtab into a symtab. */
9215 if (pdi->name)
9216 add_partial_symbol (pdi, cu);
9217 }
9218 }
9219
9220 if (! pdi->has_children)
9221 return;
9222
9223 if (cu->language == language_ada || cu->language == language_fortran)
9224 {
9225 pdi = pdi->die_child;
9226 while (pdi != NULL)
9227 {
9228 pdi->fixup (cu);
9229 if (pdi->tag == DW_TAG_subprogram
9230 || pdi->tag == DW_TAG_inlined_subroutine
9231 || pdi->tag == DW_TAG_lexical_block)
9232 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9233 pdi = pdi->die_sibling;
9234 }
9235 }
9236 }
9237
9238 /* Read a partial die corresponding to an enumeration type. */
9239
9240 static void
9241 add_partial_enumeration (struct partial_die_info *enum_pdi,
9242 struct dwarf2_cu *cu)
9243 {
9244 struct partial_die_info *pdi;
9245
9246 if (enum_pdi->name != NULL)
9247 add_partial_symbol (enum_pdi, cu);
9248
9249 pdi = enum_pdi->die_child;
9250 while (pdi)
9251 {
9252 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9253 complaint (_("malformed enumerator DIE ignored"));
9254 else
9255 add_partial_symbol (pdi, cu);
9256 pdi = pdi->die_sibling;
9257 }
9258 }
9259
9260 /* Return the initial uleb128 in the die at INFO_PTR. */
9261
9262 static unsigned int
9263 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9264 {
9265 unsigned int bytes_read;
9266
9267 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9268 }
9269
9270 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9271 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9272
9273 Return the corresponding abbrev, or NULL if the number is zero (indicating
9274 an empty DIE). In either case *BYTES_READ will be set to the length of
9275 the initial number. */
9276
9277 static struct abbrev_info *
9278 peek_die_abbrev (const die_reader_specs &reader,
9279 const gdb_byte *info_ptr, unsigned int *bytes_read)
9280 {
9281 dwarf2_cu *cu = reader.cu;
9282 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9283 unsigned int abbrev_number
9284 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9285
9286 if (abbrev_number == 0)
9287 return NULL;
9288
9289 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9290 if (!abbrev)
9291 {
9292 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9293 " at offset %s [in module %s]"),
9294 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9295 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9296 }
9297
9298 return abbrev;
9299 }
9300
9301 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9302 Returns a pointer to the end of a series of DIEs, terminated by an empty
9303 DIE. Any children of the skipped DIEs will also be skipped. */
9304
9305 static const gdb_byte *
9306 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9307 {
9308 while (1)
9309 {
9310 unsigned int bytes_read;
9311 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9312
9313 if (abbrev == NULL)
9314 return info_ptr + bytes_read;
9315 else
9316 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9317 }
9318 }
9319
9320 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9321 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9322 abbrev corresponding to that skipped uleb128 should be passed in
9323 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9324 children. */
9325
9326 static const gdb_byte *
9327 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9328 struct abbrev_info *abbrev)
9329 {
9330 unsigned int bytes_read;
9331 struct attribute attr;
9332 bfd *abfd = reader->abfd;
9333 struct dwarf2_cu *cu = reader->cu;
9334 const gdb_byte *buffer = reader->buffer;
9335 const gdb_byte *buffer_end = reader->buffer_end;
9336 unsigned int form, i;
9337
9338 for (i = 0; i < abbrev->num_attrs; i++)
9339 {
9340 /* The only abbrev we care about is DW_AT_sibling. */
9341 if (abbrev->attrs[i].name == DW_AT_sibling)
9342 {
9343 bool ignored;
9344 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
9345 &ignored);
9346 if (attr.form == DW_FORM_ref_addr)
9347 complaint (_("ignoring absolute DW_AT_sibling"));
9348 else
9349 {
9350 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9351 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9352
9353 if (sibling_ptr < info_ptr)
9354 complaint (_("DW_AT_sibling points backwards"));
9355 else if (sibling_ptr > reader->buffer_end)
9356 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9357 else
9358 return sibling_ptr;
9359 }
9360 }
9361
9362 /* If it isn't DW_AT_sibling, skip this attribute. */
9363 form = abbrev->attrs[i].form;
9364 skip_attribute:
9365 switch (form)
9366 {
9367 case DW_FORM_ref_addr:
9368 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9369 and later it is offset sized. */
9370 if (cu->header.version == 2)
9371 info_ptr += cu->header.addr_size;
9372 else
9373 info_ptr += cu->header.offset_size;
9374 break;
9375 case DW_FORM_GNU_ref_alt:
9376 info_ptr += cu->header.offset_size;
9377 break;
9378 case DW_FORM_addr:
9379 info_ptr += cu->header.addr_size;
9380 break;
9381 case DW_FORM_data1:
9382 case DW_FORM_ref1:
9383 case DW_FORM_flag:
9384 case DW_FORM_strx1:
9385 info_ptr += 1;
9386 break;
9387 case DW_FORM_flag_present:
9388 case DW_FORM_implicit_const:
9389 break;
9390 case DW_FORM_data2:
9391 case DW_FORM_ref2:
9392 case DW_FORM_strx2:
9393 info_ptr += 2;
9394 break;
9395 case DW_FORM_strx3:
9396 info_ptr += 3;
9397 break;
9398 case DW_FORM_data4:
9399 case DW_FORM_ref4:
9400 case DW_FORM_strx4:
9401 info_ptr += 4;
9402 break;
9403 case DW_FORM_data8:
9404 case DW_FORM_ref8:
9405 case DW_FORM_ref_sig8:
9406 info_ptr += 8;
9407 break;
9408 case DW_FORM_data16:
9409 info_ptr += 16;
9410 break;
9411 case DW_FORM_string:
9412 read_direct_string (abfd, info_ptr, &bytes_read);
9413 info_ptr += bytes_read;
9414 break;
9415 case DW_FORM_sec_offset:
9416 case DW_FORM_strp:
9417 case DW_FORM_GNU_strp_alt:
9418 info_ptr += cu->header.offset_size;
9419 break;
9420 case DW_FORM_exprloc:
9421 case DW_FORM_block:
9422 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9423 info_ptr += bytes_read;
9424 break;
9425 case DW_FORM_block1:
9426 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9427 break;
9428 case DW_FORM_block2:
9429 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9430 break;
9431 case DW_FORM_block4:
9432 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9433 break;
9434 case DW_FORM_addrx:
9435 case DW_FORM_strx:
9436 case DW_FORM_sdata:
9437 case DW_FORM_udata:
9438 case DW_FORM_ref_udata:
9439 case DW_FORM_GNU_addr_index:
9440 case DW_FORM_GNU_str_index:
9441 case DW_FORM_rnglistx:
9442 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9443 break;
9444 case DW_FORM_indirect:
9445 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9446 info_ptr += bytes_read;
9447 /* We need to continue parsing from here, so just go back to
9448 the top. */
9449 goto skip_attribute;
9450
9451 default:
9452 error (_("Dwarf Error: Cannot handle %s "
9453 "in DWARF reader [in module %s]"),
9454 dwarf_form_name (form),
9455 bfd_get_filename (abfd));
9456 }
9457 }
9458
9459 if (abbrev->has_children)
9460 return skip_children (reader, info_ptr);
9461 else
9462 return info_ptr;
9463 }
9464
9465 /* Locate ORIG_PDI's sibling.
9466 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9467
9468 static const gdb_byte *
9469 locate_pdi_sibling (const struct die_reader_specs *reader,
9470 struct partial_die_info *orig_pdi,
9471 const gdb_byte *info_ptr)
9472 {
9473 /* Do we know the sibling already? */
9474
9475 if (orig_pdi->sibling)
9476 return orig_pdi->sibling;
9477
9478 /* Are there any children to deal with? */
9479
9480 if (!orig_pdi->has_children)
9481 return info_ptr;
9482
9483 /* Skip the children the long way. */
9484
9485 return skip_children (reader, info_ptr);
9486 }
9487
9488 /* Expand this partial symbol table into a full symbol table. SELF is
9489 not NULL. */
9490
9491 void
9492 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9493 {
9494 struct dwarf2_per_objfile *dwarf2_per_objfile
9495 = get_dwarf2_per_objfile (objfile);
9496
9497 if (readin)
9498 {
9499 warning (_("bug: psymtab for %s is already read in."),
9500 filename);
9501 }
9502 else
9503 {
9504 if (info_verbose)
9505 {
9506 printf_filtered (_("Reading in symbols for %s..."),
9507 filename);
9508 gdb_flush (gdb_stdout);
9509 }
9510
9511 /* If this psymtab is constructed from a debug-only objfile, the
9512 has_section_at_zero flag will not necessarily be correct. We
9513 can get the correct value for this flag by looking at the data
9514 associated with the (presumably stripped) associated objfile. */
9515 if (objfile->separate_debug_objfile_backlink)
9516 {
9517 struct dwarf2_per_objfile *dpo_backlink
9518 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9519
9520 dwarf2_per_objfile->has_section_at_zero
9521 = dpo_backlink->has_section_at_zero;
9522 }
9523
9524 dwarf2_per_objfile->reading_partial_symbols = 0;
9525
9526 psymtab_to_symtab_1 (this);
9527
9528 /* Finish up the debug error message. */
9529 if (info_verbose)
9530 printf_filtered (_("done.\n"));
9531 }
9532
9533 process_cu_includes (dwarf2_per_objfile);
9534 }
9535 \f
9536 /* Reading in full CUs. */
9537
9538 /* Add PER_CU to the queue. */
9539
9540 static void
9541 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9542 enum language pretend_language)
9543 {
9544 struct dwarf2_queue_item *item;
9545
9546 per_cu->queued = 1;
9547 item = XNEW (struct dwarf2_queue_item);
9548 item->per_cu = per_cu;
9549 item->pretend_language = pretend_language;
9550 item->next = NULL;
9551
9552 if (dwarf2_queue == NULL)
9553 dwarf2_queue = item;
9554 else
9555 dwarf2_queue_tail->next = item;
9556
9557 dwarf2_queue_tail = item;
9558 }
9559
9560 /* If PER_CU is not yet queued, add it to the queue.
9561 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9562 dependency.
9563 The result is non-zero if PER_CU was queued, otherwise the result is zero
9564 meaning either PER_CU is already queued or it is already loaded.
9565
9566 N.B. There is an invariant here that if a CU is queued then it is loaded.
9567 The caller is required to load PER_CU if we return non-zero. */
9568
9569 static int
9570 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9571 struct dwarf2_per_cu_data *per_cu,
9572 enum language pretend_language)
9573 {
9574 /* We may arrive here during partial symbol reading, if we need full
9575 DIEs to process an unusual case (e.g. template arguments). Do
9576 not queue PER_CU, just tell our caller to load its DIEs. */
9577 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9578 {
9579 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9580 return 1;
9581 return 0;
9582 }
9583
9584 /* Mark the dependence relation so that we don't flush PER_CU
9585 too early. */
9586 if (dependent_cu != NULL)
9587 dwarf2_add_dependence (dependent_cu, per_cu);
9588
9589 /* If it's already on the queue, we have nothing to do. */
9590 if (per_cu->queued)
9591 return 0;
9592
9593 /* If the compilation unit is already loaded, just mark it as
9594 used. */
9595 if (per_cu->cu != NULL)
9596 {
9597 per_cu->cu->last_used = 0;
9598 return 0;
9599 }
9600
9601 /* Add it to the queue. */
9602 queue_comp_unit (per_cu, pretend_language);
9603
9604 return 1;
9605 }
9606
9607 /* Process the queue. */
9608
9609 static void
9610 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9611 {
9612 struct dwarf2_queue_item *item, *next_item;
9613
9614 if (dwarf_read_debug)
9615 {
9616 fprintf_unfiltered (gdb_stdlog,
9617 "Expanding one or more symtabs of objfile %s ...\n",
9618 objfile_name (dwarf2_per_objfile->objfile));
9619 }
9620
9621 /* The queue starts out with one item, but following a DIE reference
9622 may load a new CU, adding it to the end of the queue. */
9623 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9624 {
9625 if ((dwarf2_per_objfile->using_index
9626 ? !item->per_cu->v.quick->compunit_symtab
9627 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9628 /* Skip dummy CUs. */
9629 && item->per_cu->cu != NULL)
9630 {
9631 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9632 unsigned int debug_print_threshold;
9633 char buf[100];
9634
9635 if (per_cu->is_debug_types)
9636 {
9637 struct signatured_type *sig_type =
9638 (struct signatured_type *) per_cu;
9639
9640 sprintf (buf, "TU %s at offset %s",
9641 hex_string (sig_type->signature),
9642 sect_offset_str (per_cu->sect_off));
9643 /* There can be 100s of TUs.
9644 Only print them in verbose mode. */
9645 debug_print_threshold = 2;
9646 }
9647 else
9648 {
9649 sprintf (buf, "CU at offset %s",
9650 sect_offset_str (per_cu->sect_off));
9651 debug_print_threshold = 1;
9652 }
9653
9654 if (dwarf_read_debug >= debug_print_threshold)
9655 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9656
9657 if (per_cu->is_debug_types)
9658 process_full_type_unit (per_cu, item->pretend_language);
9659 else
9660 process_full_comp_unit (per_cu, item->pretend_language);
9661
9662 if (dwarf_read_debug >= debug_print_threshold)
9663 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9664 }
9665
9666 item->per_cu->queued = 0;
9667 next_item = item->next;
9668 xfree (item);
9669 }
9670
9671 dwarf2_queue_tail = NULL;
9672
9673 if (dwarf_read_debug)
9674 {
9675 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9676 objfile_name (dwarf2_per_objfile->objfile));
9677 }
9678 }
9679
9680 /* Read in full symbols for PST, and anything it depends on. */
9681
9682 static void
9683 psymtab_to_symtab_1 (dwarf2_psymtab *pst)
9684 {
9685 struct dwarf2_per_cu_data *per_cu;
9686 int i;
9687
9688 if (pst->readin)
9689 return;
9690
9691 for (i = 0; i < pst->number_of_dependencies; i++)
9692 if (!pst->dependencies[i]->readin
9693 && pst->dependencies[i]->user == NULL)
9694 {
9695 /* Inform about additional files that need to be read in. */
9696 if (info_verbose)
9697 {
9698 /* FIXME: i18n: Need to make this a single string. */
9699 fputs_filtered (" ", gdb_stdout);
9700 wrap_here ("");
9701 fputs_filtered ("and ", gdb_stdout);
9702 wrap_here ("");
9703 printf_filtered ("%s...", pst->dependencies[i]->filename);
9704 wrap_here (""); /* Flush output. */
9705 gdb_flush (gdb_stdout);
9706 }
9707 psymtab_to_symtab_1 ((dwarf2_psymtab *) pst->dependencies[i]);
9708 }
9709
9710 per_cu = pst->per_cu_data;
9711
9712 if (per_cu == NULL)
9713 {
9714 /* It's an include file, no symbols to read for it.
9715 Everything is in the parent symtab. */
9716 pst->readin = true;
9717 return;
9718 }
9719
9720 dw2_do_instantiate_symtab (per_cu, false);
9721 }
9722
9723 /* Trivial hash function for die_info: the hash value of a DIE
9724 is its offset in .debug_info for this objfile. */
9725
9726 static hashval_t
9727 die_hash (const void *item)
9728 {
9729 const struct die_info *die = (const struct die_info *) item;
9730
9731 return to_underlying (die->sect_off);
9732 }
9733
9734 /* Trivial comparison function for die_info structures: two DIEs
9735 are equal if they have the same offset. */
9736
9737 static int
9738 die_eq (const void *item_lhs, const void *item_rhs)
9739 {
9740 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9741 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9742
9743 return die_lhs->sect_off == die_rhs->sect_off;
9744 }
9745
9746 /* Load the DIEs associated with PER_CU into memory. */
9747
9748 static void
9749 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9750 bool skip_partial,
9751 enum language pretend_language)
9752 {
9753 gdb_assert (! this_cu->is_debug_types);
9754
9755 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9756 if (reader.dummy_p)
9757 return;
9758
9759 struct dwarf2_cu *cu = reader.cu;
9760 const gdb_byte *info_ptr = reader.info_ptr;
9761
9762 gdb_assert (cu->die_hash == NULL);
9763 cu->die_hash =
9764 htab_create_alloc_ex (cu->header.length / 12,
9765 die_hash,
9766 die_eq,
9767 NULL,
9768 &cu->comp_unit_obstack,
9769 hashtab_obstack_allocate,
9770 dummy_obstack_deallocate);
9771
9772 if (reader.has_children)
9773 reader.comp_unit_die->child
9774 = read_die_and_siblings (&reader, reader.info_ptr,
9775 &info_ptr, reader.comp_unit_die);
9776 cu->dies = reader.comp_unit_die;
9777 /* comp_unit_die is not stored in die_hash, no need. */
9778
9779 /* We try not to read any attributes in this function, because not
9780 all CUs needed for references have been loaded yet, and symbol
9781 table processing isn't initialized. But we have to set the CU language,
9782 or we won't be able to build types correctly.
9783 Similarly, if we do not read the producer, we can not apply
9784 producer-specific interpretation. */
9785 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9786 }
9787
9788 /* Add a DIE to the delayed physname list. */
9789
9790 static void
9791 add_to_method_list (struct type *type, int fnfield_index, int index,
9792 const char *name, struct die_info *die,
9793 struct dwarf2_cu *cu)
9794 {
9795 struct delayed_method_info mi;
9796 mi.type = type;
9797 mi.fnfield_index = fnfield_index;
9798 mi.index = index;
9799 mi.name = name;
9800 mi.die = die;
9801 cu->method_list.push_back (mi);
9802 }
9803
9804 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9805 "const" / "volatile". If so, decrements LEN by the length of the
9806 modifier and return true. Otherwise return false. */
9807
9808 template<size_t N>
9809 static bool
9810 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9811 {
9812 size_t mod_len = sizeof (mod) - 1;
9813 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9814 {
9815 len -= mod_len;
9816 return true;
9817 }
9818 return false;
9819 }
9820
9821 /* Compute the physnames of any methods on the CU's method list.
9822
9823 The computation of method physnames is delayed in order to avoid the
9824 (bad) condition that one of the method's formal parameters is of an as yet
9825 incomplete type. */
9826
9827 static void
9828 compute_delayed_physnames (struct dwarf2_cu *cu)
9829 {
9830 /* Only C++ delays computing physnames. */
9831 if (cu->method_list.empty ())
9832 return;
9833 gdb_assert (cu->language == language_cplus);
9834
9835 for (const delayed_method_info &mi : cu->method_list)
9836 {
9837 const char *physname;
9838 struct fn_fieldlist *fn_flp
9839 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9840 physname = dwarf2_physname (mi.name, mi.die, cu);
9841 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9842 = physname ? physname : "";
9843
9844 /* Since there's no tag to indicate whether a method is a
9845 const/volatile overload, extract that information out of the
9846 demangled name. */
9847 if (physname != NULL)
9848 {
9849 size_t len = strlen (physname);
9850
9851 while (1)
9852 {
9853 if (physname[len] == ')') /* shortcut */
9854 break;
9855 else if (check_modifier (physname, len, " const"))
9856 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9857 else if (check_modifier (physname, len, " volatile"))
9858 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9859 else
9860 break;
9861 }
9862 }
9863 }
9864
9865 /* The list is no longer needed. */
9866 cu->method_list.clear ();
9867 }
9868
9869 /* Go objects should be embedded in a DW_TAG_module DIE,
9870 and it's not clear if/how imported objects will appear.
9871 To keep Go support simple until that's worked out,
9872 go back through what we've read and create something usable.
9873 We could do this while processing each DIE, and feels kinda cleaner,
9874 but that way is more invasive.
9875 This is to, for example, allow the user to type "p var" or "b main"
9876 without having to specify the package name, and allow lookups
9877 of module.object to work in contexts that use the expression
9878 parser. */
9879
9880 static void
9881 fixup_go_packaging (struct dwarf2_cu *cu)
9882 {
9883 gdb::unique_xmalloc_ptr<char> package_name;
9884 struct pending *list;
9885 int i;
9886
9887 for (list = *cu->get_builder ()->get_global_symbols ();
9888 list != NULL;
9889 list = list->next)
9890 {
9891 for (i = 0; i < list->nsyms; ++i)
9892 {
9893 struct symbol *sym = list->symbol[i];
9894
9895 if (sym->language () == language_go
9896 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9897 {
9898 gdb::unique_xmalloc_ptr<char> this_package_name
9899 (go_symbol_package_name (sym));
9900
9901 if (this_package_name == NULL)
9902 continue;
9903 if (package_name == NULL)
9904 package_name = std::move (this_package_name);
9905 else
9906 {
9907 struct objfile *objfile
9908 = cu->per_cu->dwarf2_per_objfile->objfile;
9909 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9910 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9911 (symbol_symtab (sym) != NULL
9912 ? symtab_to_filename_for_display
9913 (symbol_symtab (sym))
9914 : objfile_name (objfile)),
9915 this_package_name.get (), package_name.get ());
9916 }
9917 }
9918 }
9919 }
9920
9921 if (package_name != NULL)
9922 {
9923 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9924 const char *saved_package_name
9925 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9926 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9927 saved_package_name);
9928 struct symbol *sym;
9929
9930 sym = allocate_symbol (objfile);
9931 sym->set_language (language_go, &objfile->objfile_obstack);
9932 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9933 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9934 e.g., "main" finds the "main" module and not C's main(). */
9935 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9936 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9937 SYMBOL_TYPE (sym) = type;
9938
9939 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9940 }
9941 }
9942
9943 /* Allocate a fully-qualified name consisting of the two parts on the
9944 obstack. */
9945
9946 static const char *
9947 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9948 {
9949 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9950 }
9951
9952 /* A helper that allocates a struct discriminant_info to attach to a
9953 union type. */
9954
9955 static struct discriminant_info *
9956 alloc_discriminant_info (struct type *type, int discriminant_index,
9957 int default_index)
9958 {
9959 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9960 gdb_assert (discriminant_index == -1
9961 || (discriminant_index >= 0
9962 && discriminant_index < TYPE_NFIELDS (type)));
9963 gdb_assert (default_index == -1
9964 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9965
9966 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9967
9968 struct discriminant_info *disc
9969 = ((struct discriminant_info *)
9970 TYPE_ZALLOC (type,
9971 offsetof (struct discriminant_info, discriminants)
9972 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9973 disc->default_index = default_index;
9974 disc->discriminant_index = discriminant_index;
9975
9976 struct dynamic_prop prop;
9977 prop.kind = PROP_UNDEFINED;
9978 prop.data.baton = disc;
9979
9980 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9981
9982 return disc;
9983 }
9984
9985 /* Some versions of rustc emitted enums in an unusual way.
9986
9987 Ordinary enums were emitted as unions. The first element of each
9988 structure in the union was named "RUST$ENUM$DISR". This element
9989 held the discriminant.
9990
9991 These versions of Rust also implemented the "non-zero"
9992 optimization. When the enum had two values, and one is empty and
9993 the other holds a pointer that cannot be zero, the pointer is used
9994 as the discriminant, with a zero value meaning the empty variant.
9995 Here, the union's first member is of the form
9996 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9997 where the fieldnos are the indices of the fields that should be
9998 traversed in order to find the field (which may be several fields deep)
9999 and the variantname is the name of the variant of the case when the
10000 field is zero.
10001
10002 This function recognizes whether TYPE is of one of these forms,
10003 and, if so, smashes it to be a variant type. */
10004
10005 static void
10006 quirk_rust_enum (struct type *type, struct objfile *objfile)
10007 {
10008 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10009
10010 /* We don't need to deal with empty enums. */
10011 if (TYPE_NFIELDS (type) == 0)
10012 return;
10013
10014 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10015 if (TYPE_NFIELDS (type) == 1
10016 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10017 {
10018 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10019
10020 /* Decode the field name to find the offset of the
10021 discriminant. */
10022 ULONGEST bit_offset = 0;
10023 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10024 while (name[0] >= '0' && name[0] <= '9')
10025 {
10026 char *tail;
10027 unsigned long index = strtoul (name, &tail, 10);
10028 name = tail;
10029 if (*name != '$'
10030 || index >= TYPE_NFIELDS (field_type)
10031 || (TYPE_FIELD_LOC_KIND (field_type, index)
10032 != FIELD_LOC_KIND_BITPOS))
10033 {
10034 complaint (_("Could not parse Rust enum encoding string \"%s\""
10035 "[in module %s]"),
10036 TYPE_FIELD_NAME (type, 0),
10037 objfile_name (objfile));
10038 return;
10039 }
10040 ++name;
10041
10042 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10043 field_type = TYPE_FIELD_TYPE (field_type, index);
10044 }
10045
10046 /* Make a union to hold the variants. */
10047 struct type *union_type = alloc_type (objfile);
10048 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10049 TYPE_NFIELDS (union_type) = 3;
10050 TYPE_FIELDS (union_type)
10051 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10052 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10053 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10054
10055 /* Put the discriminant must at index 0. */
10056 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10057 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10058 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10059 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10060
10061 /* The order of fields doesn't really matter, so put the real
10062 field at index 1 and the data-less field at index 2. */
10063 struct discriminant_info *disc
10064 = alloc_discriminant_info (union_type, 0, 1);
10065 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10066 TYPE_FIELD_NAME (union_type, 1)
10067 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10068 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10069 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10070 TYPE_FIELD_NAME (union_type, 1));
10071
10072 const char *dataless_name
10073 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10074 name);
10075 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10076 dataless_name);
10077 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10078 /* NAME points into the original discriminant name, which
10079 already has the correct lifetime. */
10080 TYPE_FIELD_NAME (union_type, 2) = name;
10081 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10082 disc->discriminants[2] = 0;
10083
10084 /* Smash this type to be a structure type. We have to do this
10085 because the type has already been recorded. */
10086 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10087 TYPE_NFIELDS (type) = 1;
10088 TYPE_FIELDS (type)
10089 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10090
10091 /* Install the variant part. */
10092 TYPE_FIELD_TYPE (type, 0) = union_type;
10093 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10094 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10095 }
10096 /* A union with a single anonymous field is probably an old-style
10097 univariant enum. */
10098 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
10099 {
10100 /* Smash this type to be a structure type. We have to do this
10101 because the type has already been recorded. */
10102 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10103
10104 /* Make a union to hold the variants. */
10105 struct type *union_type = alloc_type (objfile);
10106 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10107 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10108 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10109 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10110 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10111
10112 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10113 const char *variant_name
10114 = rust_last_path_segment (TYPE_NAME (field_type));
10115 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10116 TYPE_NAME (field_type)
10117 = rust_fully_qualify (&objfile->objfile_obstack,
10118 TYPE_NAME (type), variant_name);
10119
10120 /* Install the union in the outer struct type. */
10121 TYPE_NFIELDS (type) = 1;
10122 TYPE_FIELDS (type)
10123 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10124 TYPE_FIELD_TYPE (type, 0) = union_type;
10125 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10126 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10127
10128 alloc_discriminant_info (union_type, -1, 0);
10129 }
10130 else
10131 {
10132 struct type *disr_type = nullptr;
10133 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10134 {
10135 disr_type = TYPE_FIELD_TYPE (type, i);
10136
10137 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10138 {
10139 /* All fields of a true enum will be structs. */
10140 return;
10141 }
10142 else if (TYPE_NFIELDS (disr_type) == 0)
10143 {
10144 /* Could be data-less variant, so keep going. */
10145 disr_type = nullptr;
10146 }
10147 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10148 "RUST$ENUM$DISR") != 0)
10149 {
10150 /* Not a Rust enum. */
10151 return;
10152 }
10153 else
10154 {
10155 /* Found one. */
10156 break;
10157 }
10158 }
10159
10160 /* If we got here without a discriminant, then it's probably
10161 just a union. */
10162 if (disr_type == nullptr)
10163 return;
10164
10165 /* Smash this type to be a structure type. We have to do this
10166 because the type has already been recorded. */
10167 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10168
10169 /* Make a union to hold the variants. */
10170 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10171 struct type *union_type = alloc_type (objfile);
10172 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10173 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10174 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10175 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10176 TYPE_FIELDS (union_type)
10177 = (struct field *) TYPE_ZALLOC (union_type,
10178 (TYPE_NFIELDS (union_type)
10179 * sizeof (struct field)));
10180
10181 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10182 TYPE_NFIELDS (type) * sizeof (struct field));
10183
10184 /* Install the discriminant at index 0 in the union. */
10185 TYPE_FIELD (union_type, 0) = *disr_field;
10186 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10187 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10188
10189 /* Install the union in the outer struct type. */
10190 TYPE_FIELD_TYPE (type, 0) = union_type;
10191 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10192 TYPE_NFIELDS (type) = 1;
10193
10194 /* Set the size and offset of the union type. */
10195 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10196
10197 /* We need a way to find the correct discriminant given a
10198 variant name. For convenience we build a map here. */
10199 struct type *enum_type = FIELD_TYPE (*disr_field);
10200 std::unordered_map<std::string, ULONGEST> discriminant_map;
10201 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10202 {
10203 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10204 {
10205 const char *name
10206 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10207 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10208 }
10209 }
10210
10211 int n_fields = TYPE_NFIELDS (union_type);
10212 struct discriminant_info *disc
10213 = alloc_discriminant_info (union_type, 0, -1);
10214 /* Skip the discriminant here. */
10215 for (int i = 1; i < n_fields; ++i)
10216 {
10217 /* Find the final word in the name of this variant's type.
10218 That name can be used to look up the correct
10219 discriminant. */
10220 const char *variant_name
10221 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10222 i)));
10223
10224 auto iter = discriminant_map.find (variant_name);
10225 if (iter != discriminant_map.end ())
10226 disc->discriminants[i] = iter->second;
10227
10228 /* Remove the discriminant field, if it exists. */
10229 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10230 if (TYPE_NFIELDS (sub_type) > 0)
10231 {
10232 --TYPE_NFIELDS (sub_type);
10233 ++TYPE_FIELDS (sub_type);
10234 }
10235 TYPE_FIELD_NAME (union_type, i) = variant_name;
10236 TYPE_NAME (sub_type)
10237 = rust_fully_qualify (&objfile->objfile_obstack,
10238 TYPE_NAME (type), variant_name);
10239 }
10240 }
10241 }
10242
10243 /* Rewrite some Rust unions to be structures with variants parts. */
10244
10245 static void
10246 rust_union_quirks (struct dwarf2_cu *cu)
10247 {
10248 gdb_assert (cu->language == language_rust);
10249 for (type *type_ : cu->rust_unions)
10250 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10251 /* We don't need this any more. */
10252 cu->rust_unions.clear ();
10253 }
10254
10255 /* Return the symtab for PER_CU. This works properly regardless of
10256 whether we're using the index or psymtabs. */
10257
10258 static struct compunit_symtab *
10259 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10260 {
10261 return (per_cu->dwarf2_per_objfile->using_index
10262 ? per_cu->v.quick->compunit_symtab
10263 : per_cu->v.psymtab->compunit_symtab);
10264 }
10265
10266 /* A helper function for computing the list of all symbol tables
10267 included by PER_CU. */
10268
10269 static void
10270 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10271 htab_t all_children, htab_t all_type_symtabs,
10272 struct dwarf2_per_cu_data *per_cu,
10273 struct compunit_symtab *immediate_parent)
10274 {
10275 void **slot;
10276 struct compunit_symtab *cust;
10277
10278 slot = htab_find_slot (all_children, per_cu, INSERT);
10279 if (*slot != NULL)
10280 {
10281 /* This inclusion and its children have been processed. */
10282 return;
10283 }
10284
10285 *slot = per_cu;
10286 /* Only add a CU if it has a symbol table. */
10287 cust = get_compunit_symtab (per_cu);
10288 if (cust != NULL)
10289 {
10290 /* If this is a type unit only add its symbol table if we haven't
10291 seen it yet (type unit per_cu's can share symtabs). */
10292 if (per_cu->is_debug_types)
10293 {
10294 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10295 if (*slot == NULL)
10296 {
10297 *slot = cust;
10298 result->push_back (cust);
10299 if (cust->user == NULL)
10300 cust->user = immediate_parent;
10301 }
10302 }
10303 else
10304 {
10305 result->push_back (cust);
10306 if (cust->user == NULL)
10307 cust->user = immediate_parent;
10308 }
10309 }
10310
10311 if (!per_cu->imported_symtabs_empty ())
10312 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10313 {
10314 recursively_compute_inclusions (result, all_children,
10315 all_type_symtabs, ptr, cust);
10316 }
10317 }
10318
10319 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10320 PER_CU. */
10321
10322 static void
10323 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10324 {
10325 gdb_assert (! per_cu->is_debug_types);
10326
10327 if (!per_cu->imported_symtabs_empty ())
10328 {
10329 int len;
10330 std::vector<compunit_symtab *> result_symtabs;
10331 htab_t all_children, all_type_symtabs;
10332 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10333
10334 /* If we don't have a symtab, we can just skip this case. */
10335 if (cust == NULL)
10336 return;
10337
10338 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10339 NULL, xcalloc, xfree);
10340 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10341 NULL, xcalloc, xfree);
10342
10343 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10344 {
10345 recursively_compute_inclusions (&result_symtabs, all_children,
10346 all_type_symtabs, ptr, cust);
10347 }
10348
10349 /* Now we have a transitive closure of all the included symtabs. */
10350 len = result_symtabs.size ();
10351 cust->includes
10352 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10353 struct compunit_symtab *, len + 1);
10354 memcpy (cust->includes, result_symtabs.data (),
10355 len * sizeof (compunit_symtab *));
10356 cust->includes[len] = NULL;
10357
10358 htab_delete (all_children);
10359 htab_delete (all_type_symtabs);
10360 }
10361 }
10362
10363 /* Compute the 'includes' field for the symtabs of all the CUs we just
10364 read. */
10365
10366 static void
10367 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10368 {
10369 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10370 {
10371 if (! iter->is_debug_types)
10372 compute_compunit_symtab_includes (iter);
10373 }
10374
10375 dwarf2_per_objfile->just_read_cus.clear ();
10376 }
10377
10378 /* Generate full symbol information for PER_CU, whose DIEs have
10379 already been loaded into memory. */
10380
10381 static void
10382 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10383 enum language pretend_language)
10384 {
10385 struct dwarf2_cu *cu = per_cu->cu;
10386 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10387 struct objfile *objfile = dwarf2_per_objfile->objfile;
10388 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10389 CORE_ADDR lowpc, highpc;
10390 struct compunit_symtab *cust;
10391 CORE_ADDR baseaddr;
10392 struct block *static_block;
10393 CORE_ADDR addr;
10394
10395 baseaddr = objfile->text_section_offset ();
10396
10397 /* Clear the list here in case something was left over. */
10398 cu->method_list.clear ();
10399
10400 cu->language = pretend_language;
10401 cu->language_defn = language_def (cu->language);
10402
10403 /* Do line number decoding in read_file_scope () */
10404 process_die (cu->dies, cu);
10405
10406 /* For now fudge the Go package. */
10407 if (cu->language == language_go)
10408 fixup_go_packaging (cu);
10409
10410 /* Now that we have processed all the DIEs in the CU, all the types
10411 should be complete, and it should now be safe to compute all of the
10412 physnames. */
10413 compute_delayed_physnames (cu);
10414
10415 if (cu->language == language_rust)
10416 rust_union_quirks (cu);
10417
10418 /* Some compilers don't define a DW_AT_high_pc attribute for the
10419 compilation unit. If the DW_AT_high_pc is missing, synthesize
10420 it, by scanning the DIE's below the compilation unit. */
10421 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10422
10423 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10424 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10425
10426 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10427 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10428 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10429 addrmap to help ensure it has an accurate map of pc values belonging to
10430 this comp unit. */
10431 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10432
10433 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10434 SECT_OFF_TEXT (objfile),
10435 0);
10436
10437 if (cust != NULL)
10438 {
10439 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10440
10441 /* Set symtab language to language from DW_AT_language. If the
10442 compilation is from a C file generated by language preprocessors, do
10443 not set the language if it was already deduced by start_subfile. */
10444 if (!(cu->language == language_c
10445 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10446 COMPUNIT_FILETABS (cust)->language = cu->language;
10447
10448 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10449 produce DW_AT_location with location lists but it can be possibly
10450 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10451 there were bugs in prologue debug info, fixed later in GCC-4.5
10452 by "unwind info for epilogues" patch (which is not directly related).
10453
10454 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10455 needed, it would be wrong due to missing DW_AT_producer there.
10456
10457 Still one can confuse GDB by using non-standard GCC compilation
10458 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10459 */
10460 if (cu->has_loclist && gcc_4_minor >= 5)
10461 cust->locations_valid = 1;
10462
10463 if (gcc_4_minor >= 5)
10464 cust->epilogue_unwind_valid = 1;
10465
10466 cust->call_site_htab = cu->call_site_htab;
10467 }
10468
10469 if (dwarf2_per_objfile->using_index)
10470 per_cu->v.quick->compunit_symtab = cust;
10471 else
10472 {
10473 dwarf2_psymtab *pst = per_cu->v.psymtab;
10474 pst->compunit_symtab = cust;
10475 pst->readin = true;
10476 }
10477
10478 /* Push it for inclusion processing later. */
10479 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10480
10481 /* Not needed any more. */
10482 cu->reset_builder ();
10483 }
10484
10485 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10486 already been loaded into memory. */
10487
10488 static void
10489 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10490 enum language pretend_language)
10491 {
10492 struct dwarf2_cu *cu = per_cu->cu;
10493 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10494 struct objfile *objfile = dwarf2_per_objfile->objfile;
10495 struct compunit_symtab *cust;
10496 struct signatured_type *sig_type;
10497
10498 gdb_assert (per_cu->is_debug_types);
10499 sig_type = (struct signatured_type *) per_cu;
10500
10501 /* Clear the list here in case something was left over. */
10502 cu->method_list.clear ();
10503
10504 cu->language = pretend_language;
10505 cu->language_defn = language_def (cu->language);
10506
10507 /* The symbol tables are set up in read_type_unit_scope. */
10508 process_die (cu->dies, cu);
10509
10510 /* For now fudge the Go package. */
10511 if (cu->language == language_go)
10512 fixup_go_packaging (cu);
10513
10514 /* Now that we have processed all the DIEs in the CU, all the types
10515 should be complete, and it should now be safe to compute all of the
10516 physnames. */
10517 compute_delayed_physnames (cu);
10518
10519 if (cu->language == language_rust)
10520 rust_union_quirks (cu);
10521
10522 /* TUs share symbol tables.
10523 If this is the first TU to use this symtab, complete the construction
10524 of it with end_expandable_symtab. Otherwise, complete the addition of
10525 this TU's symbols to the existing symtab. */
10526 if (sig_type->type_unit_group->compunit_symtab == NULL)
10527 {
10528 buildsym_compunit *builder = cu->get_builder ();
10529 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10530 sig_type->type_unit_group->compunit_symtab = cust;
10531
10532 if (cust != NULL)
10533 {
10534 /* Set symtab language to language from DW_AT_language. If the
10535 compilation is from a C file generated by language preprocessors,
10536 do not set the language if it was already deduced by
10537 start_subfile. */
10538 if (!(cu->language == language_c
10539 && COMPUNIT_FILETABS (cust)->language != language_c))
10540 COMPUNIT_FILETABS (cust)->language = cu->language;
10541 }
10542 }
10543 else
10544 {
10545 cu->get_builder ()->augment_type_symtab ();
10546 cust = sig_type->type_unit_group->compunit_symtab;
10547 }
10548
10549 if (dwarf2_per_objfile->using_index)
10550 per_cu->v.quick->compunit_symtab = cust;
10551 else
10552 {
10553 dwarf2_psymtab *pst = per_cu->v.psymtab;
10554 pst->compunit_symtab = cust;
10555 pst->readin = true;
10556 }
10557
10558 /* Not needed any more. */
10559 cu->reset_builder ();
10560 }
10561
10562 /* Process an imported unit DIE. */
10563
10564 static void
10565 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10566 {
10567 struct attribute *attr;
10568
10569 /* For now we don't handle imported units in type units. */
10570 if (cu->per_cu->is_debug_types)
10571 {
10572 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10573 " supported in type units [in module %s]"),
10574 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10575 }
10576
10577 attr = dwarf2_attr (die, DW_AT_import, cu);
10578 if (attr != NULL)
10579 {
10580 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10581 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10582 dwarf2_per_cu_data *per_cu
10583 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10584 cu->per_cu->dwarf2_per_objfile);
10585
10586 /* If necessary, add it to the queue and load its DIEs. */
10587 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10588 load_full_comp_unit (per_cu, false, cu->language);
10589
10590 cu->per_cu->imported_symtabs_push (per_cu);
10591 }
10592 }
10593
10594 /* RAII object that represents a process_die scope: i.e.,
10595 starts/finishes processing a DIE. */
10596 class process_die_scope
10597 {
10598 public:
10599 process_die_scope (die_info *die, dwarf2_cu *cu)
10600 : m_die (die), m_cu (cu)
10601 {
10602 /* We should only be processing DIEs not already in process. */
10603 gdb_assert (!m_die->in_process);
10604 m_die->in_process = true;
10605 }
10606
10607 ~process_die_scope ()
10608 {
10609 m_die->in_process = false;
10610
10611 /* If we're done processing the DIE for the CU that owns the line
10612 header, we don't need the line header anymore. */
10613 if (m_cu->line_header_die_owner == m_die)
10614 {
10615 delete m_cu->line_header;
10616 m_cu->line_header = NULL;
10617 m_cu->line_header_die_owner = NULL;
10618 }
10619 }
10620
10621 private:
10622 die_info *m_die;
10623 dwarf2_cu *m_cu;
10624 };
10625
10626 /* Process a die and its children. */
10627
10628 static void
10629 process_die (struct die_info *die, struct dwarf2_cu *cu)
10630 {
10631 process_die_scope scope (die, cu);
10632
10633 switch (die->tag)
10634 {
10635 case DW_TAG_padding:
10636 break;
10637 case DW_TAG_compile_unit:
10638 case DW_TAG_partial_unit:
10639 read_file_scope (die, cu);
10640 break;
10641 case DW_TAG_type_unit:
10642 read_type_unit_scope (die, cu);
10643 break;
10644 case DW_TAG_subprogram:
10645 /* Nested subprograms in Fortran get a prefix. */
10646 if (cu->language == language_fortran
10647 && die->parent != NULL
10648 && die->parent->tag == DW_TAG_subprogram)
10649 cu->processing_has_namespace_info = true;
10650 /* Fall through. */
10651 case DW_TAG_inlined_subroutine:
10652 read_func_scope (die, cu);
10653 break;
10654 case DW_TAG_lexical_block:
10655 case DW_TAG_try_block:
10656 case DW_TAG_catch_block:
10657 read_lexical_block_scope (die, cu);
10658 break;
10659 case DW_TAG_call_site:
10660 case DW_TAG_GNU_call_site:
10661 read_call_site_scope (die, cu);
10662 break;
10663 case DW_TAG_class_type:
10664 case DW_TAG_interface_type:
10665 case DW_TAG_structure_type:
10666 case DW_TAG_union_type:
10667 process_structure_scope (die, cu);
10668 break;
10669 case DW_TAG_enumeration_type:
10670 process_enumeration_scope (die, cu);
10671 break;
10672
10673 /* These dies have a type, but processing them does not create
10674 a symbol or recurse to process the children. Therefore we can
10675 read them on-demand through read_type_die. */
10676 case DW_TAG_subroutine_type:
10677 case DW_TAG_set_type:
10678 case DW_TAG_array_type:
10679 case DW_TAG_pointer_type:
10680 case DW_TAG_ptr_to_member_type:
10681 case DW_TAG_reference_type:
10682 case DW_TAG_rvalue_reference_type:
10683 case DW_TAG_string_type:
10684 break;
10685
10686 case DW_TAG_base_type:
10687 case DW_TAG_subrange_type:
10688 case DW_TAG_typedef:
10689 /* Add a typedef symbol for the type definition, if it has a
10690 DW_AT_name. */
10691 new_symbol (die, read_type_die (die, cu), cu);
10692 break;
10693 case DW_TAG_common_block:
10694 read_common_block (die, cu);
10695 break;
10696 case DW_TAG_common_inclusion:
10697 break;
10698 case DW_TAG_namespace:
10699 cu->processing_has_namespace_info = true;
10700 read_namespace (die, cu);
10701 break;
10702 case DW_TAG_module:
10703 cu->processing_has_namespace_info = true;
10704 read_module (die, cu);
10705 break;
10706 case DW_TAG_imported_declaration:
10707 cu->processing_has_namespace_info = true;
10708 if (read_namespace_alias (die, cu))
10709 break;
10710 /* The declaration is not a global namespace alias. */
10711 /* Fall through. */
10712 case DW_TAG_imported_module:
10713 cu->processing_has_namespace_info = true;
10714 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10715 || cu->language != language_fortran))
10716 complaint (_("Tag '%s' has unexpected children"),
10717 dwarf_tag_name (die->tag));
10718 read_import_statement (die, cu);
10719 break;
10720
10721 case DW_TAG_imported_unit:
10722 process_imported_unit_die (die, cu);
10723 break;
10724
10725 case DW_TAG_variable:
10726 read_variable (die, cu);
10727 break;
10728
10729 default:
10730 new_symbol (die, NULL, cu);
10731 break;
10732 }
10733 }
10734 \f
10735 /* DWARF name computation. */
10736
10737 /* A helper function for dwarf2_compute_name which determines whether DIE
10738 needs to have the name of the scope prepended to the name listed in the
10739 die. */
10740
10741 static int
10742 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10743 {
10744 struct attribute *attr;
10745
10746 switch (die->tag)
10747 {
10748 case DW_TAG_namespace:
10749 case DW_TAG_typedef:
10750 case DW_TAG_class_type:
10751 case DW_TAG_interface_type:
10752 case DW_TAG_structure_type:
10753 case DW_TAG_union_type:
10754 case DW_TAG_enumeration_type:
10755 case DW_TAG_enumerator:
10756 case DW_TAG_subprogram:
10757 case DW_TAG_inlined_subroutine:
10758 case DW_TAG_member:
10759 case DW_TAG_imported_declaration:
10760 return 1;
10761
10762 case DW_TAG_variable:
10763 case DW_TAG_constant:
10764 /* We only need to prefix "globally" visible variables. These include
10765 any variable marked with DW_AT_external or any variable that
10766 lives in a namespace. [Variables in anonymous namespaces
10767 require prefixing, but they are not DW_AT_external.] */
10768
10769 if (dwarf2_attr (die, DW_AT_specification, cu))
10770 {
10771 struct dwarf2_cu *spec_cu = cu;
10772
10773 return die_needs_namespace (die_specification (die, &spec_cu),
10774 spec_cu);
10775 }
10776
10777 attr = dwarf2_attr (die, DW_AT_external, cu);
10778 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10779 && die->parent->tag != DW_TAG_module)
10780 return 0;
10781 /* A variable in a lexical block of some kind does not need a
10782 namespace, even though in C++ such variables may be external
10783 and have a mangled name. */
10784 if (die->parent->tag == DW_TAG_lexical_block
10785 || die->parent->tag == DW_TAG_try_block
10786 || die->parent->tag == DW_TAG_catch_block
10787 || die->parent->tag == DW_TAG_subprogram)
10788 return 0;
10789 return 1;
10790
10791 default:
10792 return 0;
10793 }
10794 }
10795
10796 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10797 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10798 defined for the given DIE. */
10799
10800 static struct attribute *
10801 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10802 {
10803 struct attribute *attr;
10804
10805 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10806 if (attr == NULL)
10807 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10808
10809 return attr;
10810 }
10811
10812 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10813 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10814 defined for the given DIE. */
10815
10816 static const char *
10817 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10818 {
10819 const char *linkage_name;
10820
10821 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10822 if (linkage_name == NULL)
10823 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10824
10825 return linkage_name;
10826 }
10827
10828 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10829 compute the physname for the object, which include a method's:
10830 - formal parameters (C++),
10831 - receiver type (Go),
10832
10833 The term "physname" is a bit confusing.
10834 For C++, for example, it is the demangled name.
10835 For Go, for example, it's the mangled name.
10836
10837 For Ada, return the DIE's linkage name rather than the fully qualified
10838 name. PHYSNAME is ignored..
10839
10840 The result is allocated on the objfile_obstack and canonicalized. */
10841
10842 static const char *
10843 dwarf2_compute_name (const char *name,
10844 struct die_info *die, struct dwarf2_cu *cu,
10845 int physname)
10846 {
10847 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10848
10849 if (name == NULL)
10850 name = dwarf2_name (die, cu);
10851
10852 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10853 but otherwise compute it by typename_concat inside GDB.
10854 FIXME: Actually this is not really true, or at least not always true.
10855 It's all very confusing. compute_and_set_names doesn't try to demangle
10856 Fortran names because there is no mangling standard. So new_symbol
10857 will set the demangled name to the result of dwarf2_full_name, and it is
10858 the demangled name that GDB uses if it exists. */
10859 if (cu->language == language_ada
10860 || (cu->language == language_fortran && physname))
10861 {
10862 /* For Ada unit, we prefer the linkage name over the name, as
10863 the former contains the exported name, which the user expects
10864 to be able to reference. Ideally, we want the user to be able
10865 to reference this entity using either natural or linkage name,
10866 but we haven't started looking at this enhancement yet. */
10867 const char *linkage_name = dw2_linkage_name (die, cu);
10868
10869 if (linkage_name != NULL)
10870 return linkage_name;
10871 }
10872
10873 /* These are the only languages we know how to qualify names in. */
10874 if (name != NULL
10875 && (cu->language == language_cplus
10876 || cu->language == language_fortran || cu->language == language_d
10877 || cu->language == language_rust))
10878 {
10879 if (die_needs_namespace (die, cu))
10880 {
10881 const char *prefix;
10882 const char *canonical_name = NULL;
10883
10884 string_file buf;
10885
10886 prefix = determine_prefix (die, cu);
10887 if (*prefix != '\0')
10888 {
10889 gdb::unique_xmalloc_ptr<char> prefixed_name
10890 (typename_concat (NULL, prefix, name, physname, cu));
10891
10892 buf.puts (prefixed_name.get ());
10893 }
10894 else
10895 buf.puts (name);
10896
10897 /* Template parameters may be specified in the DIE's DW_AT_name, or
10898 as children with DW_TAG_template_type_param or
10899 DW_TAG_value_type_param. If the latter, add them to the name
10900 here. If the name already has template parameters, then
10901 skip this step; some versions of GCC emit both, and
10902 it is more efficient to use the pre-computed name.
10903
10904 Something to keep in mind about this process: it is very
10905 unlikely, or in some cases downright impossible, to produce
10906 something that will match the mangled name of a function.
10907 If the definition of the function has the same debug info,
10908 we should be able to match up with it anyway. But fallbacks
10909 using the minimal symbol, for instance to find a method
10910 implemented in a stripped copy of libstdc++, will not work.
10911 If we do not have debug info for the definition, we will have to
10912 match them up some other way.
10913
10914 When we do name matching there is a related problem with function
10915 templates; two instantiated function templates are allowed to
10916 differ only by their return types, which we do not add here. */
10917
10918 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10919 {
10920 struct attribute *attr;
10921 struct die_info *child;
10922 int first = 1;
10923
10924 die->building_fullname = 1;
10925
10926 for (child = die->child; child != NULL; child = child->sibling)
10927 {
10928 struct type *type;
10929 LONGEST value;
10930 const gdb_byte *bytes;
10931 struct dwarf2_locexpr_baton *baton;
10932 struct value *v;
10933
10934 if (child->tag != DW_TAG_template_type_param
10935 && child->tag != DW_TAG_template_value_param)
10936 continue;
10937
10938 if (first)
10939 {
10940 buf.puts ("<");
10941 first = 0;
10942 }
10943 else
10944 buf.puts (", ");
10945
10946 attr = dwarf2_attr (child, DW_AT_type, cu);
10947 if (attr == NULL)
10948 {
10949 complaint (_("template parameter missing DW_AT_type"));
10950 buf.puts ("UNKNOWN_TYPE");
10951 continue;
10952 }
10953 type = die_type (child, cu);
10954
10955 if (child->tag == DW_TAG_template_type_param)
10956 {
10957 c_print_type (type, "", &buf, -1, 0, cu->language,
10958 &type_print_raw_options);
10959 continue;
10960 }
10961
10962 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10963 if (attr == NULL)
10964 {
10965 complaint (_("template parameter missing "
10966 "DW_AT_const_value"));
10967 buf.puts ("UNKNOWN_VALUE");
10968 continue;
10969 }
10970
10971 dwarf2_const_value_attr (attr, type, name,
10972 &cu->comp_unit_obstack, cu,
10973 &value, &bytes, &baton);
10974
10975 if (TYPE_NOSIGN (type))
10976 /* GDB prints characters as NUMBER 'CHAR'. If that's
10977 changed, this can use value_print instead. */
10978 c_printchar (value, type, &buf);
10979 else
10980 {
10981 struct value_print_options opts;
10982
10983 if (baton != NULL)
10984 v = dwarf2_evaluate_loc_desc (type, NULL,
10985 baton->data,
10986 baton->size,
10987 baton->per_cu);
10988 else if (bytes != NULL)
10989 {
10990 v = allocate_value (type);
10991 memcpy (value_contents_writeable (v), bytes,
10992 TYPE_LENGTH (type));
10993 }
10994 else
10995 v = value_from_longest (type, value);
10996
10997 /* Specify decimal so that we do not depend on
10998 the radix. */
10999 get_formatted_print_options (&opts, 'd');
11000 opts.raw = 1;
11001 value_print (v, &buf, &opts);
11002 release_value (v);
11003 }
11004 }
11005
11006 die->building_fullname = 0;
11007
11008 if (!first)
11009 {
11010 /* Close the argument list, with a space if necessary
11011 (nested templates). */
11012 if (!buf.empty () && buf.string ().back () == '>')
11013 buf.puts (" >");
11014 else
11015 buf.puts (">");
11016 }
11017 }
11018
11019 /* For C++ methods, append formal parameter type
11020 information, if PHYSNAME. */
11021
11022 if (physname && die->tag == DW_TAG_subprogram
11023 && cu->language == language_cplus)
11024 {
11025 struct type *type = read_type_die (die, cu);
11026
11027 c_type_print_args (type, &buf, 1, cu->language,
11028 &type_print_raw_options);
11029
11030 if (cu->language == language_cplus)
11031 {
11032 /* Assume that an artificial first parameter is
11033 "this", but do not crash if it is not. RealView
11034 marks unnamed (and thus unused) parameters as
11035 artificial; there is no way to differentiate
11036 the two cases. */
11037 if (TYPE_NFIELDS (type) > 0
11038 && TYPE_FIELD_ARTIFICIAL (type, 0)
11039 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11040 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11041 0))))
11042 buf.puts (" const");
11043 }
11044 }
11045
11046 const std::string &intermediate_name = buf.string ();
11047
11048 if (cu->language == language_cplus)
11049 canonical_name
11050 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11051 &objfile->per_bfd->storage_obstack);
11052
11053 /* If we only computed INTERMEDIATE_NAME, or if
11054 INTERMEDIATE_NAME is already canonical, then we need to
11055 copy it to the appropriate obstack. */
11056 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11057 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11058 intermediate_name);
11059 else
11060 name = canonical_name;
11061 }
11062 }
11063
11064 return name;
11065 }
11066
11067 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11068 If scope qualifiers are appropriate they will be added. The result
11069 will be allocated on the storage_obstack, or NULL if the DIE does
11070 not have a name. NAME may either be from a previous call to
11071 dwarf2_name or NULL.
11072
11073 The output string will be canonicalized (if C++). */
11074
11075 static const char *
11076 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11077 {
11078 return dwarf2_compute_name (name, die, cu, 0);
11079 }
11080
11081 /* Construct a physname for the given DIE in CU. NAME may either be
11082 from a previous call to dwarf2_name or NULL. The result will be
11083 allocated on the objfile_objstack or NULL if the DIE does not have a
11084 name.
11085
11086 The output string will be canonicalized (if C++). */
11087
11088 static const char *
11089 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11090 {
11091 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11092 const char *retval, *mangled = NULL, *canon = NULL;
11093 int need_copy = 1;
11094
11095 /* In this case dwarf2_compute_name is just a shortcut not building anything
11096 on its own. */
11097 if (!die_needs_namespace (die, cu))
11098 return dwarf2_compute_name (name, die, cu, 1);
11099
11100 mangled = dw2_linkage_name (die, cu);
11101
11102 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11103 See https://github.com/rust-lang/rust/issues/32925. */
11104 if (cu->language == language_rust && mangled != NULL
11105 && strchr (mangled, '{') != NULL)
11106 mangled = NULL;
11107
11108 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11109 has computed. */
11110 gdb::unique_xmalloc_ptr<char> demangled;
11111 if (mangled != NULL)
11112 {
11113
11114 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11115 {
11116 /* Do nothing (do not demangle the symbol name). */
11117 }
11118 else if (cu->language == language_go)
11119 {
11120 /* This is a lie, but we already lie to the caller new_symbol.
11121 new_symbol assumes we return the mangled name.
11122 This just undoes that lie until things are cleaned up. */
11123 }
11124 else
11125 {
11126 /* Use DMGL_RET_DROP for C++ template functions to suppress
11127 their return type. It is easier for GDB users to search
11128 for such functions as `name(params)' than `long name(params)'.
11129 In such case the minimal symbol names do not match the full
11130 symbol names but for template functions there is never a need
11131 to look up their definition from their declaration so
11132 the only disadvantage remains the minimal symbol variant
11133 `long name(params)' does not have the proper inferior type. */
11134 demangled.reset (gdb_demangle (mangled,
11135 (DMGL_PARAMS | DMGL_ANSI
11136 | DMGL_RET_DROP)));
11137 }
11138 if (demangled)
11139 canon = demangled.get ();
11140 else
11141 {
11142 canon = mangled;
11143 need_copy = 0;
11144 }
11145 }
11146
11147 if (canon == NULL || check_physname)
11148 {
11149 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11150
11151 if (canon != NULL && strcmp (physname, canon) != 0)
11152 {
11153 /* It may not mean a bug in GDB. The compiler could also
11154 compute DW_AT_linkage_name incorrectly. But in such case
11155 GDB would need to be bug-to-bug compatible. */
11156
11157 complaint (_("Computed physname <%s> does not match demangled <%s> "
11158 "(from linkage <%s>) - DIE at %s [in module %s]"),
11159 physname, canon, mangled, sect_offset_str (die->sect_off),
11160 objfile_name (objfile));
11161
11162 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11163 is available here - over computed PHYSNAME. It is safer
11164 against both buggy GDB and buggy compilers. */
11165
11166 retval = canon;
11167 }
11168 else
11169 {
11170 retval = physname;
11171 need_copy = 0;
11172 }
11173 }
11174 else
11175 retval = canon;
11176
11177 if (need_copy)
11178 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11179
11180 return retval;
11181 }
11182
11183 /* Inspect DIE in CU for a namespace alias. If one exists, record
11184 a new symbol for it.
11185
11186 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11187
11188 static int
11189 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11190 {
11191 struct attribute *attr;
11192
11193 /* If the die does not have a name, this is not a namespace
11194 alias. */
11195 attr = dwarf2_attr (die, DW_AT_name, cu);
11196 if (attr != NULL)
11197 {
11198 int num;
11199 struct die_info *d = die;
11200 struct dwarf2_cu *imported_cu = cu;
11201
11202 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11203 keep inspecting DIEs until we hit the underlying import. */
11204 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11205 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11206 {
11207 attr = dwarf2_attr (d, DW_AT_import, cu);
11208 if (attr == NULL)
11209 break;
11210
11211 d = follow_die_ref (d, attr, &imported_cu);
11212 if (d->tag != DW_TAG_imported_declaration)
11213 break;
11214 }
11215
11216 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11217 {
11218 complaint (_("DIE at %s has too many recursively imported "
11219 "declarations"), sect_offset_str (d->sect_off));
11220 return 0;
11221 }
11222
11223 if (attr != NULL)
11224 {
11225 struct type *type;
11226 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11227
11228 type = get_die_type_at_offset (sect_off, cu->per_cu);
11229 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11230 {
11231 /* This declaration is a global namespace alias. Add
11232 a symbol for it whose type is the aliased namespace. */
11233 new_symbol (die, type, cu);
11234 return 1;
11235 }
11236 }
11237 }
11238
11239 return 0;
11240 }
11241
11242 /* Return the using directives repository (global or local?) to use in the
11243 current context for CU.
11244
11245 For Ada, imported declarations can materialize renamings, which *may* be
11246 global. However it is impossible (for now?) in DWARF to distinguish
11247 "external" imported declarations and "static" ones. As all imported
11248 declarations seem to be static in all other languages, make them all CU-wide
11249 global only in Ada. */
11250
11251 static struct using_direct **
11252 using_directives (struct dwarf2_cu *cu)
11253 {
11254 if (cu->language == language_ada
11255 && cu->get_builder ()->outermost_context_p ())
11256 return cu->get_builder ()->get_global_using_directives ();
11257 else
11258 return cu->get_builder ()->get_local_using_directives ();
11259 }
11260
11261 /* Read the import statement specified by the given die and record it. */
11262
11263 static void
11264 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11265 {
11266 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11267 struct attribute *import_attr;
11268 struct die_info *imported_die, *child_die;
11269 struct dwarf2_cu *imported_cu;
11270 const char *imported_name;
11271 const char *imported_name_prefix;
11272 const char *canonical_name;
11273 const char *import_alias;
11274 const char *imported_declaration = NULL;
11275 const char *import_prefix;
11276 std::vector<const char *> excludes;
11277
11278 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11279 if (import_attr == NULL)
11280 {
11281 complaint (_("Tag '%s' has no DW_AT_import"),
11282 dwarf_tag_name (die->tag));
11283 return;
11284 }
11285
11286 imported_cu = cu;
11287 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11288 imported_name = dwarf2_name (imported_die, imported_cu);
11289 if (imported_name == NULL)
11290 {
11291 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11292
11293 The import in the following code:
11294 namespace A
11295 {
11296 typedef int B;
11297 }
11298
11299 int main ()
11300 {
11301 using A::B;
11302 B b;
11303 return b;
11304 }
11305
11306 ...
11307 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11308 <52> DW_AT_decl_file : 1
11309 <53> DW_AT_decl_line : 6
11310 <54> DW_AT_import : <0x75>
11311 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11312 <59> DW_AT_name : B
11313 <5b> DW_AT_decl_file : 1
11314 <5c> DW_AT_decl_line : 2
11315 <5d> DW_AT_type : <0x6e>
11316 ...
11317 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11318 <76> DW_AT_byte_size : 4
11319 <77> DW_AT_encoding : 5 (signed)
11320
11321 imports the wrong die ( 0x75 instead of 0x58 ).
11322 This case will be ignored until the gcc bug is fixed. */
11323 return;
11324 }
11325
11326 /* Figure out the local name after import. */
11327 import_alias = dwarf2_name (die, cu);
11328
11329 /* Figure out where the statement is being imported to. */
11330 import_prefix = determine_prefix (die, cu);
11331
11332 /* Figure out what the scope of the imported die is and prepend it
11333 to the name of the imported die. */
11334 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11335
11336 if (imported_die->tag != DW_TAG_namespace
11337 && imported_die->tag != DW_TAG_module)
11338 {
11339 imported_declaration = imported_name;
11340 canonical_name = imported_name_prefix;
11341 }
11342 else if (strlen (imported_name_prefix) > 0)
11343 canonical_name = obconcat (&objfile->objfile_obstack,
11344 imported_name_prefix,
11345 (cu->language == language_d ? "." : "::"),
11346 imported_name, (char *) NULL);
11347 else
11348 canonical_name = imported_name;
11349
11350 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11351 for (child_die = die->child; child_die && child_die->tag;
11352 child_die = sibling_die (child_die))
11353 {
11354 /* DWARF-4: A Fortran use statement with a “rename list” may be
11355 represented by an imported module entry with an import attribute
11356 referring to the module and owned entries corresponding to those
11357 entities that are renamed as part of being imported. */
11358
11359 if (child_die->tag != DW_TAG_imported_declaration)
11360 {
11361 complaint (_("child DW_TAG_imported_declaration expected "
11362 "- DIE at %s [in module %s]"),
11363 sect_offset_str (child_die->sect_off),
11364 objfile_name (objfile));
11365 continue;
11366 }
11367
11368 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11369 if (import_attr == NULL)
11370 {
11371 complaint (_("Tag '%s' has no DW_AT_import"),
11372 dwarf_tag_name (child_die->tag));
11373 continue;
11374 }
11375
11376 imported_cu = cu;
11377 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11378 &imported_cu);
11379 imported_name = dwarf2_name (imported_die, imported_cu);
11380 if (imported_name == NULL)
11381 {
11382 complaint (_("child DW_TAG_imported_declaration has unknown "
11383 "imported name - DIE at %s [in module %s]"),
11384 sect_offset_str (child_die->sect_off),
11385 objfile_name (objfile));
11386 continue;
11387 }
11388
11389 excludes.push_back (imported_name);
11390
11391 process_die (child_die, cu);
11392 }
11393
11394 add_using_directive (using_directives (cu),
11395 import_prefix,
11396 canonical_name,
11397 import_alias,
11398 imported_declaration,
11399 excludes,
11400 0,
11401 &objfile->objfile_obstack);
11402 }
11403
11404 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11405 types, but gives them a size of zero. Starting with version 14,
11406 ICC is compatible with GCC. */
11407
11408 static bool
11409 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11410 {
11411 if (!cu->checked_producer)
11412 check_producer (cu);
11413
11414 return cu->producer_is_icc_lt_14;
11415 }
11416
11417 /* ICC generates a DW_AT_type for C void functions. This was observed on
11418 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11419 which says that void functions should not have a DW_AT_type. */
11420
11421 static bool
11422 producer_is_icc (struct dwarf2_cu *cu)
11423 {
11424 if (!cu->checked_producer)
11425 check_producer (cu);
11426
11427 return cu->producer_is_icc;
11428 }
11429
11430 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11431 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11432 this, it was first present in GCC release 4.3.0. */
11433
11434 static bool
11435 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11436 {
11437 if (!cu->checked_producer)
11438 check_producer (cu);
11439
11440 return cu->producer_is_gcc_lt_4_3;
11441 }
11442
11443 static file_and_directory
11444 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11445 {
11446 file_and_directory res;
11447
11448 /* Find the filename. Do not use dwarf2_name here, since the filename
11449 is not a source language identifier. */
11450 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11451 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11452
11453 if (res.comp_dir == NULL
11454 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11455 && IS_ABSOLUTE_PATH (res.name))
11456 {
11457 res.comp_dir_storage = ldirname (res.name);
11458 if (!res.comp_dir_storage.empty ())
11459 res.comp_dir = res.comp_dir_storage.c_str ();
11460 }
11461 if (res.comp_dir != NULL)
11462 {
11463 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11464 directory, get rid of it. */
11465 const char *cp = strchr (res.comp_dir, ':');
11466
11467 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11468 res.comp_dir = cp + 1;
11469 }
11470
11471 if (res.name == NULL)
11472 res.name = "<unknown>";
11473
11474 return res;
11475 }
11476
11477 /* Handle DW_AT_stmt_list for a compilation unit.
11478 DIE is the DW_TAG_compile_unit die for CU.
11479 COMP_DIR is the compilation directory. LOWPC is passed to
11480 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11481
11482 static void
11483 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11484 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11485 {
11486 struct dwarf2_per_objfile *dwarf2_per_objfile
11487 = cu->per_cu->dwarf2_per_objfile;
11488 struct objfile *objfile = dwarf2_per_objfile->objfile;
11489 struct attribute *attr;
11490 struct line_header line_header_local;
11491 hashval_t line_header_local_hash;
11492 void **slot;
11493 int decode_mapping;
11494
11495 gdb_assert (! cu->per_cu->is_debug_types);
11496
11497 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11498 if (attr == NULL)
11499 return;
11500
11501 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11502
11503 /* The line header hash table is only created if needed (it exists to
11504 prevent redundant reading of the line table for partial_units).
11505 If we're given a partial_unit, we'll need it. If we're given a
11506 compile_unit, then use the line header hash table if it's already
11507 created, but don't create one just yet. */
11508
11509 if (dwarf2_per_objfile->line_header_hash == NULL
11510 && die->tag == DW_TAG_partial_unit)
11511 {
11512 dwarf2_per_objfile->line_header_hash
11513 = htab_create_alloc_ex (127, line_header_hash_voidp,
11514 line_header_eq_voidp,
11515 free_line_header_voidp,
11516 &objfile->objfile_obstack,
11517 hashtab_obstack_allocate,
11518 dummy_obstack_deallocate);
11519 }
11520
11521 line_header_local.sect_off = line_offset;
11522 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11523 line_header_local_hash = line_header_hash (&line_header_local);
11524 if (dwarf2_per_objfile->line_header_hash != NULL)
11525 {
11526 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11527 &line_header_local,
11528 line_header_local_hash, NO_INSERT);
11529
11530 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11531 is not present in *SLOT (since if there is something in *SLOT then
11532 it will be for a partial_unit). */
11533 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11534 {
11535 gdb_assert (*slot != NULL);
11536 cu->line_header = (struct line_header *) *slot;
11537 return;
11538 }
11539 }
11540
11541 /* dwarf_decode_line_header does not yet provide sufficient information.
11542 We always have to call also dwarf_decode_lines for it. */
11543 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11544 if (lh == NULL)
11545 return;
11546
11547 cu->line_header = lh.release ();
11548 cu->line_header_die_owner = die;
11549
11550 if (dwarf2_per_objfile->line_header_hash == NULL)
11551 slot = NULL;
11552 else
11553 {
11554 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11555 &line_header_local,
11556 line_header_local_hash, INSERT);
11557 gdb_assert (slot != NULL);
11558 }
11559 if (slot != NULL && *slot == NULL)
11560 {
11561 /* This newly decoded line number information unit will be owned
11562 by line_header_hash hash table. */
11563 *slot = cu->line_header;
11564 cu->line_header_die_owner = NULL;
11565 }
11566 else
11567 {
11568 /* We cannot free any current entry in (*slot) as that struct line_header
11569 may be already used by multiple CUs. Create only temporary decoded
11570 line_header for this CU - it may happen at most once for each line
11571 number information unit. And if we're not using line_header_hash
11572 then this is what we want as well. */
11573 gdb_assert (die->tag != DW_TAG_partial_unit);
11574 }
11575 decode_mapping = (die->tag != DW_TAG_partial_unit);
11576 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11577 decode_mapping);
11578
11579 }
11580
11581 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11582
11583 static void
11584 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11585 {
11586 struct dwarf2_per_objfile *dwarf2_per_objfile
11587 = cu->per_cu->dwarf2_per_objfile;
11588 struct objfile *objfile = dwarf2_per_objfile->objfile;
11589 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11590 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11591 CORE_ADDR highpc = ((CORE_ADDR) 0);
11592 struct attribute *attr;
11593 struct die_info *child_die;
11594 CORE_ADDR baseaddr;
11595
11596 prepare_one_comp_unit (cu, die, cu->language);
11597 baseaddr = objfile->text_section_offset ();
11598
11599 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11600
11601 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11602 from finish_block. */
11603 if (lowpc == ((CORE_ADDR) -1))
11604 lowpc = highpc;
11605 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11606
11607 file_and_directory fnd = find_file_and_directory (die, cu);
11608
11609 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11610 standardised yet. As a workaround for the language detection we fall
11611 back to the DW_AT_producer string. */
11612 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11613 cu->language = language_opencl;
11614
11615 /* Similar hack for Go. */
11616 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11617 set_cu_language (DW_LANG_Go, cu);
11618
11619 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11620
11621 /* Decode line number information if present. We do this before
11622 processing child DIEs, so that the line header table is available
11623 for DW_AT_decl_file. */
11624 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11625
11626 /* Process all dies in compilation unit. */
11627 if (die->child != NULL)
11628 {
11629 child_die = die->child;
11630 while (child_die && child_die->tag)
11631 {
11632 process_die (child_die, cu);
11633 child_die = sibling_die (child_die);
11634 }
11635 }
11636
11637 /* Decode macro information, if present. Dwarf 2 macro information
11638 refers to information in the line number info statement program
11639 header, so we can only read it if we've read the header
11640 successfully. */
11641 attr = dwarf2_attr (die, DW_AT_macros, cu);
11642 if (attr == NULL)
11643 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11644 if (attr && cu->line_header)
11645 {
11646 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11647 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11648
11649 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11650 }
11651 else
11652 {
11653 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11654 if (attr && cu->line_header)
11655 {
11656 unsigned int macro_offset = DW_UNSND (attr);
11657
11658 dwarf_decode_macros (cu, macro_offset, 0);
11659 }
11660 }
11661 }
11662
11663 void
11664 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11665 {
11666 struct type_unit_group *tu_group;
11667 int first_time;
11668 struct attribute *attr;
11669 unsigned int i;
11670 struct signatured_type *sig_type;
11671
11672 gdb_assert (per_cu->is_debug_types);
11673 sig_type = (struct signatured_type *) per_cu;
11674
11675 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11676
11677 /* If we're using .gdb_index (includes -readnow) then
11678 per_cu->type_unit_group may not have been set up yet. */
11679 if (sig_type->type_unit_group == NULL)
11680 sig_type->type_unit_group = get_type_unit_group (this, attr);
11681 tu_group = sig_type->type_unit_group;
11682
11683 /* If we've already processed this stmt_list there's no real need to
11684 do it again, we could fake it and just recreate the part we need
11685 (file name,index -> symtab mapping). If data shows this optimization
11686 is useful we can do it then. */
11687 first_time = tu_group->compunit_symtab == NULL;
11688
11689 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11690 debug info. */
11691 line_header_up lh;
11692 if (attr != NULL)
11693 {
11694 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11695 lh = dwarf_decode_line_header (line_offset, this);
11696 }
11697 if (lh == NULL)
11698 {
11699 if (first_time)
11700 start_symtab ("", NULL, 0);
11701 else
11702 {
11703 gdb_assert (tu_group->symtabs == NULL);
11704 gdb_assert (m_builder == nullptr);
11705 struct compunit_symtab *cust = tu_group->compunit_symtab;
11706 m_builder.reset (new struct buildsym_compunit
11707 (COMPUNIT_OBJFILE (cust), "",
11708 COMPUNIT_DIRNAME (cust),
11709 compunit_language (cust),
11710 0, cust));
11711 }
11712 return;
11713 }
11714
11715 line_header = lh.release ();
11716 line_header_die_owner = die;
11717
11718 if (first_time)
11719 {
11720 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11721
11722 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11723 still initializing it, and our caller (a few levels up)
11724 process_full_type_unit still needs to know if this is the first
11725 time. */
11726
11727 tu_group->num_symtabs = line_header->file_names_size ();
11728 tu_group->symtabs = XNEWVEC (struct symtab *,
11729 line_header->file_names_size ());
11730
11731 auto &file_names = line_header->file_names ();
11732 for (i = 0; i < file_names.size (); ++i)
11733 {
11734 file_entry &fe = file_names[i];
11735 dwarf2_start_subfile (this, fe.name,
11736 fe.include_dir (line_header));
11737 buildsym_compunit *b = get_builder ();
11738 if (b->get_current_subfile ()->symtab == NULL)
11739 {
11740 /* NOTE: start_subfile will recognize when it's been
11741 passed a file it has already seen. So we can't
11742 assume there's a simple mapping from
11743 cu->line_header->file_names to subfiles, plus
11744 cu->line_header->file_names may contain dups. */
11745 b->get_current_subfile ()->symtab
11746 = allocate_symtab (cust, b->get_current_subfile ()->name);
11747 }
11748
11749 fe.symtab = b->get_current_subfile ()->symtab;
11750 tu_group->symtabs[i] = fe.symtab;
11751 }
11752 }
11753 else
11754 {
11755 gdb_assert (m_builder == nullptr);
11756 struct compunit_symtab *cust = tu_group->compunit_symtab;
11757 m_builder.reset (new struct buildsym_compunit
11758 (COMPUNIT_OBJFILE (cust), "",
11759 COMPUNIT_DIRNAME (cust),
11760 compunit_language (cust),
11761 0, cust));
11762
11763 auto &file_names = line_header->file_names ();
11764 for (i = 0; i < file_names.size (); ++i)
11765 {
11766 file_entry &fe = file_names[i];
11767 fe.symtab = tu_group->symtabs[i];
11768 }
11769 }
11770
11771 /* The main symtab is allocated last. Type units don't have DW_AT_name
11772 so they don't have a "real" (so to speak) symtab anyway.
11773 There is later code that will assign the main symtab to all symbols
11774 that don't have one. We need to handle the case of a symbol with a
11775 missing symtab (DW_AT_decl_file) anyway. */
11776 }
11777
11778 /* Process DW_TAG_type_unit.
11779 For TUs we want to skip the first top level sibling if it's not the
11780 actual type being defined by this TU. In this case the first top
11781 level sibling is there to provide context only. */
11782
11783 static void
11784 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11785 {
11786 struct die_info *child_die;
11787
11788 prepare_one_comp_unit (cu, die, language_minimal);
11789
11790 /* Initialize (or reinitialize) the machinery for building symtabs.
11791 We do this before processing child DIEs, so that the line header table
11792 is available for DW_AT_decl_file. */
11793 cu->setup_type_unit_groups (die);
11794
11795 if (die->child != NULL)
11796 {
11797 child_die = die->child;
11798 while (child_die && child_die->tag)
11799 {
11800 process_die (child_die, cu);
11801 child_die = sibling_die (child_die);
11802 }
11803 }
11804 }
11805 \f
11806 /* DWO/DWP files.
11807
11808 http://gcc.gnu.org/wiki/DebugFission
11809 http://gcc.gnu.org/wiki/DebugFissionDWP
11810
11811 To simplify handling of both DWO files ("object" files with the DWARF info)
11812 and DWP files (a file with the DWOs packaged up into one file), we treat
11813 DWP files as having a collection of virtual DWO files. */
11814
11815 static hashval_t
11816 hash_dwo_file (const void *item)
11817 {
11818 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11819 hashval_t hash;
11820
11821 hash = htab_hash_string (dwo_file->dwo_name);
11822 if (dwo_file->comp_dir != NULL)
11823 hash += htab_hash_string (dwo_file->comp_dir);
11824 return hash;
11825 }
11826
11827 static int
11828 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11829 {
11830 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11831 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11832
11833 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11834 return 0;
11835 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11836 return lhs->comp_dir == rhs->comp_dir;
11837 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11838 }
11839
11840 /* Allocate a hash table for DWO files. */
11841
11842 static htab_up
11843 allocate_dwo_file_hash_table (struct objfile *objfile)
11844 {
11845 auto delete_dwo_file = [] (void *item)
11846 {
11847 struct dwo_file *dwo_file = (struct dwo_file *) item;
11848
11849 delete dwo_file;
11850 };
11851
11852 return htab_up (htab_create_alloc_ex (41,
11853 hash_dwo_file,
11854 eq_dwo_file,
11855 delete_dwo_file,
11856 &objfile->objfile_obstack,
11857 hashtab_obstack_allocate,
11858 dummy_obstack_deallocate));
11859 }
11860
11861 /* Lookup DWO file DWO_NAME. */
11862
11863 static void **
11864 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11865 const char *dwo_name,
11866 const char *comp_dir)
11867 {
11868 struct dwo_file find_entry;
11869 void **slot;
11870
11871 if (dwarf2_per_objfile->dwo_files == NULL)
11872 dwarf2_per_objfile->dwo_files
11873 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11874
11875 find_entry.dwo_name = dwo_name;
11876 find_entry.comp_dir = comp_dir;
11877 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11878 INSERT);
11879
11880 return slot;
11881 }
11882
11883 static hashval_t
11884 hash_dwo_unit (const void *item)
11885 {
11886 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11887
11888 /* This drops the top 32 bits of the id, but is ok for a hash. */
11889 return dwo_unit->signature;
11890 }
11891
11892 static int
11893 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11894 {
11895 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11896 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11897
11898 /* The signature is assumed to be unique within the DWO file.
11899 So while object file CU dwo_id's always have the value zero,
11900 that's OK, assuming each object file DWO file has only one CU,
11901 and that's the rule for now. */
11902 return lhs->signature == rhs->signature;
11903 }
11904
11905 /* Allocate a hash table for DWO CUs,TUs.
11906 There is one of these tables for each of CUs,TUs for each DWO file. */
11907
11908 static htab_t
11909 allocate_dwo_unit_table (struct objfile *objfile)
11910 {
11911 /* Start out with a pretty small number.
11912 Generally DWO files contain only one CU and maybe some TUs. */
11913 return htab_create_alloc_ex (3,
11914 hash_dwo_unit,
11915 eq_dwo_unit,
11916 NULL,
11917 &objfile->objfile_obstack,
11918 hashtab_obstack_allocate,
11919 dummy_obstack_deallocate);
11920 }
11921
11922 /* die_reader_func for create_dwo_cu. */
11923
11924 static void
11925 create_dwo_cu_reader (const struct die_reader_specs *reader,
11926 const gdb_byte *info_ptr,
11927 struct die_info *comp_unit_die,
11928 int has_children,
11929 struct dwo_file *dwo_file,
11930 struct dwo_unit *dwo_unit)
11931 {
11932 struct dwarf2_cu *cu = reader->cu;
11933 sect_offset sect_off = cu->per_cu->sect_off;
11934 struct dwarf2_section_info *section = cu->per_cu->section;
11935
11936 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11937 if (!signature.has_value ())
11938 {
11939 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11940 " its dwo_id [in module %s]"),
11941 sect_offset_str (sect_off), dwo_file->dwo_name);
11942 return;
11943 }
11944
11945 dwo_unit->dwo_file = dwo_file;
11946 dwo_unit->signature = *signature;
11947 dwo_unit->section = section;
11948 dwo_unit->sect_off = sect_off;
11949 dwo_unit->length = cu->per_cu->length;
11950
11951 if (dwarf_read_debug)
11952 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11953 sect_offset_str (sect_off),
11954 hex_string (dwo_unit->signature));
11955 }
11956
11957 /* Create the dwo_units for the CUs in a DWO_FILE.
11958 Note: This function processes DWO files only, not DWP files. */
11959
11960 static void
11961 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11962 dwarf2_cu *cu, struct dwo_file &dwo_file,
11963 dwarf2_section_info &section, htab_t &cus_htab)
11964 {
11965 struct objfile *objfile = dwarf2_per_objfile->objfile;
11966 const gdb_byte *info_ptr, *end_ptr;
11967
11968 dwarf2_read_section (objfile, &section);
11969 info_ptr = section.buffer;
11970
11971 if (info_ptr == NULL)
11972 return;
11973
11974 if (dwarf_read_debug)
11975 {
11976 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11977 get_section_name (&section),
11978 get_section_file_name (&section));
11979 }
11980
11981 end_ptr = info_ptr + section.size;
11982 while (info_ptr < end_ptr)
11983 {
11984 struct dwarf2_per_cu_data per_cu;
11985 struct dwo_unit read_unit {};
11986 struct dwo_unit *dwo_unit;
11987 void **slot;
11988 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11989
11990 memset (&per_cu, 0, sizeof (per_cu));
11991 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11992 per_cu.is_debug_types = 0;
11993 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11994 per_cu.section = &section;
11995
11996 cutu_reader reader (&per_cu, cu, &dwo_file);
11997 if (!reader.dummy_p)
11998 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11999 reader.has_children, &dwo_file, &read_unit);
12000 info_ptr += per_cu.length;
12001
12002 // If the unit could not be parsed, skip it.
12003 if (read_unit.dwo_file == NULL)
12004 continue;
12005
12006 if (cus_htab == NULL)
12007 cus_htab = allocate_dwo_unit_table (objfile);
12008
12009 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12010 *dwo_unit = read_unit;
12011 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12012 gdb_assert (slot != NULL);
12013 if (*slot != NULL)
12014 {
12015 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12016 sect_offset dup_sect_off = dup_cu->sect_off;
12017
12018 complaint (_("debug cu entry at offset %s is duplicate to"
12019 " the entry at offset %s, signature %s"),
12020 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12021 hex_string (dwo_unit->signature));
12022 }
12023 *slot = (void *)dwo_unit;
12024 }
12025 }
12026
12027 /* DWP file .debug_{cu,tu}_index section format:
12028 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12029
12030 DWP Version 1:
12031
12032 Both index sections have the same format, and serve to map a 64-bit
12033 signature to a set of section numbers. Each section begins with a header,
12034 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12035 indexes, and a pool of 32-bit section numbers. The index sections will be
12036 aligned at 8-byte boundaries in the file.
12037
12038 The index section header consists of:
12039
12040 V, 32 bit version number
12041 -, 32 bits unused
12042 N, 32 bit number of compilation units or type units in the index
12043 M, 32 bit number of slots in the hash table
12044
12045 Numbers are recorded using the byte order of the application binary.
12046
12047 The hash table begins at offset 16 in the section, and consists of an array
12048 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12049 order of the application binary). Unused slots in the hash table are 0.
12050 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12051
12052 The parallel table begins immediately after the hash table
12053 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12054 array of 32-bit indexes (using the byte order of the application binary),
12055 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12056 table contains a 32-bit index into the pool of section numbers. For unused
12057 hash table slots, the corresponding entry in the parallel table will be 0.
12058
12059 The pool of section numbers begins immediately following the hash table
12060 (at offset 16 + 12 * M from the beginning of the section). The pool of
12061 section numbers consists of an array of 32-bit words (using the byte order
12062 of the application binary). Each item in the array is indexed starting
12063 from 0. The hash table entry provides the index of the first section
12064 number in the set. Additional section numbers in the set follow, and the
12065 set is terminated by a 0 entry (section number 0 is not used in ELF).
12066
12067 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12068 section must be the first entry in the set, and the .debug_abbrev.dwo must
12069 be the second entry. Other members of the set may follow in any order.
12070
12071 ---
12072
12073 DWP Version 2:
12074
12075 DWP Version 2 combines all the .debug_info, etc. sections into one,
12076 and the entries in the index tables are now offsets into these sections.
12077 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12078 section.
12079
12080 Index Section Contents:
12081 Header
12082 Hash Table of Signatures dwp_hash_table.hash_table
12083 Parallel Table of Indices dwp_hash_table.unit_table
12084 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12085 Table of Section Sizes dwp_hash_table.v2.sizes
12086
12087 The index section header consists of:
12088
12089 V, 32 bit version number
12090 L, 32 bit number of columns in the table of section offsets
12091 N, 32 bit number of compilation units or type units in the index
12092 M, 32 bit number of slots in the hash table
12093
12094 Numbers are recorded using the byte order of the application binary.
12095
12096 The hash table has the same format as version 1.
12097 The parallel table of indices has the same format as version 1,
12098 except that the entries are origin-1 indices into the table of sections
12099 offsets and the table of section sizes.
12100
12101 The table of offsets begins immediately following the parallel table
12102 (at offset 16 + 12 * M from the beginning of the section). The table is
12103 a two-dimensional array of 32-bit words (using the byte order of the
12104 application binary), with L columns and N+1 rows, in row-major order.
12105 Each row in the array is indexed starting from 0. The first row provides
12106 a key to the remaining rows: each column in this row provides an identifier
12107 for a debug section, and the offsets in the same column of subsequent rows
12108 refer to that section. The section identifiers are:
12109
12110 DW_SECT_INFO 1 .debug_info.dwo
12111 DW_SECT_TYPES 2 .debug_types.dwo
12112 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12113 DW_SECT_LINE 4 .debug_line.dwo
12114 DW_SECT_LOC 5 .debug_loc.dwo
12115 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12116 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12117 DW_SECT_MACRO 8 .debug_macro.dwo
12118
12119 The offsets provided by the CU and TU index sections are the base offsets
12120 for the contributions made by each CU or TU to the corresponding section
12121 in the package file. Each CU and TU header contains an abbrev_offset
12122 field, used to find the abbreviations table for that CU or TU within the
12123 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12124 be interpreted as relative to the base offset given in the index section.
12125 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12126 should be interpreted as relative to the base offset for .debug_line.dwo,
12127 and offsets into other debug sections obtained from DWARF attributes should
12128 also be interpreted as relative to the corresponding base offset.
12129
12130 The table of sizes begins immediately following the table of offsets.
12131 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12132 with L columns and N rows, in row-major order. Each row in the array is
12133 indexed starting from 1 (row 0 is shared by the two tables).
12134
12135 ---
12136
12137 Hash table lookup is handled the same in version 1 and 2:
12138
12139 We assume that N and M will not exceed 2^32 - 1.
12140 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12141
12142 Given a 64-bit compilation unit signature or a type signature S, an entry
12143 in the hash table is located as follows:
12144
12145 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12146 the low-order k bits all set to 1.
12147
12148 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12149
12150 3) If the hash table entry at index H matches the signature, use that
12151 entry. If the hash table entry at index H is unused (all zeroes),
12152 terminate the search: the signature is not present in the table.
12153
12154 4) Let H = (H + H') modulo M. Repeat at Step 3.
12155
12156 Because M > N and H' and M are relatively prime, the search is guaranteed
12157 to stop at an unused slot or find the match. */
12158
12159 /* Create a hash table to map DWO IDs to their CU/TU entry in
12160 .debug_{info,types}.dwo in DWP_FILE.
12161 Returns NULL if there isn't one.
12162 Note: This function processes DWP files only, not DWO files. */
12163
12164 static struct dwp_hash_table *
12165 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12166 struct dwp_file *dwp_file, int is_debug_types)
12167 {
12168 struct objfile *objfile = dwarf2_per_objfile->objfile;
12169 bfd *dbfd = dwp_file->dbfd.get ();
12170 const gdb_byte *index_ptr, *index_end;
12171 struct dwarf2_section_info *index;
12172 uint32_t version, nr_columns, nr_units, nr_slots;
12173 struct dwp_hash_table *htab;
12174
12175 if (is_debug_types)
12176 index = &dwp_file->sections.tu_index;
12177 else
12178 index = &dwp_file->sections.cu_index;
12179
12180 if (dwarf2_section_empty_p (index))
12181 return NULL;
12182 dwarf2_read_section (objfile, index);
12183
12184 index_ptr = index->buffer;
12185 index_end = index_ptr + index->size;
12186
12187 version = read_4_bytes (dbfd, index_ptr);
12188 index_ptr += 4;
12189 if (version == 2)
12190 nr_columns = read_4_bytes (dbfd, index_ptr);
12191 else
12192 nr_columns = 0;
12193 index_ptr += 4;
12194 nr_units = read_4_bytes (dbfd, index_ptr);
12195 index_ptr += 4;
12196 nr_slots = read_4_bytes (dbfd, index_ptr);
12197 index_ptr += 4;
12198
12199 if (version != 1 && version != 2)
12200 {
12201 error (_("Dwarf Error: unsupported DWP file version (%s)"
12202 " [in module %s]"),
12203 pulongest (version), dwp_file->name);
12204 }
12205 if (nr_slots != (nr_slots & -nr_slots))
12206 {
12207 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12208 " is not power of 2 [in module %s]"),
12209 pulongest (nr_slots), dwp_file->name);
12210 }
12211
12212 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12213 htab->version = version;
12214 htab->nr_columns = nr_columns;
12215 htab->nr_units = nr_units;
12216 htab->nr_slots = nr_slots;
12217 htab->hash_table = index_ptr;
12218 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12219
12220 /* Exit early if the table is empty. */
12221 if (nr_slots == 0 || nr_units == 0
12222 || (version == 2 && nr_columns == 0))
12223 {
12224 /* All must be zero. */
12225 if (nr_slots != 0 || nr_units != 0
12226 || (version == 2 && nr_columns != 0))
12227 {
12228 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12229 " all zero [in modules %s]"),
12230 dwp_file->name);
12231 }
12232 return htab;
12233 }
12234
12235 if (version == 1)
12236 {
12237 htab->section_pool.v1.indices =
12238 htab->unit_table + sizeof (uint32_t) * nr_slots;
12239 /* It's harder to decide whether the section is too small in v1.
12240 V1 is deprecated anyway so we punt. */
12241 }
12242 else
12243 {
12244 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12245 int *ids = htab->section_pool.v2.section_ids;
12246 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12247 /* Reverse map for error checking. */
12248 int ids_seen[DW_SECT_MAX + 1];
12249 int i;
12250
12251 if (nr_columns < 2)
12252 {
12253 error (_("Dwarf Error: bad DWP hash table, too few columns"
12254 " in section table [in module %s]"),
12255 dwp_file->name);
12256 }
12257 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12258 {
12259 error (_("Dwarf Error: bad DWP hash table, too many columns"
12260 " in section table [in module %s]"),
12261 dwp_file->name);
12262 }
12263 memset (ids, 255, sizeof_ids);
12264 memset (ids_seen, 255, sizeof (ids_seen));
12265 for (i = 0; i < nr_columns; ++i)
12266 {
12267 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12268
12269 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12270 {
12271 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12272 " in section table [in module %s]"),
12273 id, dwp_file->name);
12274 }
12275 if (ids_seen[id] != -1)
12276 {
12277 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12278 " id %d in section table [in module %s]"),
12279 id, dwp_file->name);
12280 }
12281 ids_seen[id] = i;
12282 ids[i] = id;
12283 }
12284 /* Must have exactly one info or types section. */
12285 if (((ids_seen[DW_SECT_INFO] != -1)
12286 + (ids_seen[DW_SECT_TYPES] != -1))
12287 != 1)
12288 {
12289 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12290 " DWO info/types section [in module %s]"),
12291 dwp_file->name);
12292 }
12293 /* Must have an abbrev section. */
12294 if (ids_seen[DW_SECT_ABBREV] == -1)
12295 {
12296 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12297 " section [in module %s]"),
12298 dwp_file->name);
12299 }
12300 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12301 htab->section_pool.v2.sizes =
12302 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12303 * nr_units * nr_columns);
12304 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12305 * nr_units * nr_columns))
12306 > index_end)
12307 {
12308 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12309 " [in module %s]"),
12310 dwp_file->name);
12311 }
12312 }
12313
12314 return htab;
12315 }
12316
12317 /* Update SECTIONS with the data from SECTP.
12318
12319 This function is like the other "locate" section routines that are
12320 passed to bfd_map_over_sections, but in this context the sections to
12321 read comes from the DWP V1 hash table, not the full ELF section table.
12322
12323 The result is non-zero for success, or zero if an error was found. */
12324
12325 static int
12326 locate_v1_virtual_dwo_sections (asection *sectp,
12327 struct virtual_v1_dwo_sections *sections)
12328 {
12329 const struct dwop_section_names *names = &dwop_section_names;
12330
12331 if (section_is_p (sectp->name, &names->abbrev_dwo))
12332 {
12333 /* There can be only one. */
12334 if (sections->abbrev.s.section != NULL)
12335 return 0;
12336 sections->abbrev.s.section = sectp;
12337 sections->abbrev.size = bfd_section_size (sectp);
12338 }
12339 else if (section_is_p (sectp->name, &names->info_dwo)
12340 || section_is_p (sectp->name, &names->types_dwo))
12341 {
12342 /* There can be only one. */
12343 if (sections->info_or_types.s.section != NULL)
12344 return 0;
12345 sections->info_or_types.s.section = sectp;
12346 sections->info_or_types.size = bfd_section_size (sectp);
12347 }
12348 else if (section_is_p (sectp->name, &names->line_dwo))
12349 {
12350 /* There can be only one. */
12351 if (sections->line.s.section != NULL)
12352 return 0;
12353 sections->line.s.section = sectp;
12354 sections->line.size = bfd_section_size (sectp);
12355 }
12356 else if (section_is_p (sectp->name, &names->loc_dwo))
12357 {
12358 /* There can be only one. */
12359 if (sections->loc.s.section != NULL)
12360 return 0;
12361 sections->loc.s.section = sectp;
12362 sections->loc.size = bfd_section_size (sectp);
12363 }
12364 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12365 {
12366 /* There can be only one. */
12367 if (sections->macinfo.s.section != NULL)
12368 return 0;
12369 sections->macinfo.s.section = sectp;
12370 sections->macinfo.size = bfd_section_size (sectp);
12371 }
12372 else if (section_is_p (sectp->name, &names->macro_dwo))
12373 {
12374 /* There can be only one. */
12375 if (sections->macro.s.section != NULL)
12376 return 0;
12377 sections->macro.s.section = sectp;
12378 sections->macro.size = bfd_section_size (sectp);
12379 }
12380 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12381 {
12382 /* There can be only one. */
12383 if (sections->str_offsets.s.section != NULL)
12384 return 0;
12385 sections->str_offsets.s.section = sectp;
12386 sections->str_offsets.size = bfd_section_size (sectp);
12387 }
12388 else
12389 {
12390 /* No other kind of section is valid. */
12391 return 0;
12392 }
12393
12394 return 1;
12395 }
12396
12397 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12398 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12399 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12400 This is for DWP version 1 files. */
12401
12402 static struct dwo_unit *
12403 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12404 struct dwp_file *dwp_file,
12405 uint32_t unit_index,
12406 const char *comp_dir,
12407 ULONGEST signature, int is_debug_types)
12408 {
12409 struct objfile *objfile = dwarf2_per_objfile->objfile;
12410 const struct dwp_hash_table *dwp_htab =
12411 is_debug_types ? dwp_file->tus : dwp_file->cus;
12412 bfd *dbfd = dwp_file->dbfd.get ();
12413 const char *kind = is_debug_types ? "TU" : "CU";
12414 struct dwo_file *dwo_file;
12415 struct dwo_unit *dwo_unit;
12416 struct virtual_v1_dwo_sections sections;
12417 void **dwo_file_slot;
12418 int i;
12419
12420 gdb_assert (dwp_file->version == 1);
12421
12422 if (dwarf_read_debug)
12423 {
12424 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12425 kind,
12426 pulongest (unit_index), hex_string (signature),
12427 dwp_file->name);
12428 }
12429
12430 /* Fetch the sections of this DWO unit.
12431 Put a limit on the number of sections we look for so that bad data
12432 doesn't cause us to loop forever. */
12433
12434 #define MAX_NR_V1_DWO_SECTIONS \
12435 (1 /* .debug_info or .debug_types */ \
12436 + 1 /* .debug_abbrev */ \
12437 + 1 /* .debug_line */ \
12438 + 1 /* .debug_loc */ \
12439 + 1 /* .debug_str_offsets */ \
12440 + 1 /* .debug_macro or .debug_macinfo */ \
12441 + 1 /* trailing zero */)
12442
12443 memset (&sections, 0, sizeof (sections));
12444
12445 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12446 {
12447 asection *sectp;
12448 uint32_t section_nr =
12449 read_4_bytes (dbfd,
12450 dwp_htab->section_pool.v1.indices
12451 + (unit_index + i) * sizeof (uint32_t));
12452
12453 if (section_nr == 0)
12454 break;
12455 if (section_nr >= dwp_file->num_sections)
12456 {
12457 error (_("Dwarf Error: bad DWP hash table, section number too large"
12458 " [in module %s]"),
12459 dwp_file->name);
12460 }
12461
12462 sectp = dwp_file->elf_sections[section_nr];
12463 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12464 {
12465 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12466 " [in module %s]"),
12467 dwp_file->name);
12468 }
12469 }
12470
12471 if (i < 2
12472 || dwarf2_section_empty_p (&sections.info_or_types)
12473 || dwarf2_section_empty_p (&sections.abbrev))
12474 {
12475 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12476 " [in module %s]"),
12477 dwp_file->name);
12478 }
12479 if (i == MAX_NR_V1_DWO_SECTIONS)
12480 {
12481 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12482 " [in module %s]"),
12483 dwp_file->name);
12484 }
12485
12486 /* It's easier for the rest of the code if we fake a struct dwo_file and
12487 have dwo_unit "live" in that. At least for now.
12488
12489 The DWP file can be made up of a random collection of CUs and TUs.
12490 However, for each CU + set of TUs that came from the same original DWO
12491 file, we can combine them back into a virtual DWO file to save space
12492 (fewer struct dwo_file objects to allocate). Remember that for really
12493 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12494
12495 std::string virtual_dwo_name =
12496 string_printf ("virtual-dwo/%d-%d-%d-%d",
12497 get_section_id (&sections.abbrev),
12498 get_section_id (&sections.line),
12499 get_section_id (&sections.loc),
12500 get_section_id (&sections.str_offsets));
12501 /* Can we use an existing virtual DWO file? */
12502 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12503 virtual_dwo_name.c_str (),
12504 comp_dir);
12505 /* Create one if necessary. */
12506 if (*dwo_file_slot == NULL)
12507 {
12508 if (dwarf_read_debug)
12509 {
12510 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12511 virtual_dwo_name.c_str ());
12512 }
12513 dwo_file = new struct dwo_file;
12514 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12515 virtual_dwo_name);
12516 dwo_file->comp_dir = comp_dir;
12517 dwo_file->sections.abbrev = sections.abbrev;
12518 dwo_file->sections.line = sections.line;
12519 dwo_file->sections.loc = sections.loc;
12520 dwo_file->sections.macinfo = sections.macinfo;
12521 dwo_file->sections.macro = sections.macro;
12522 dwo_file->sections.str_offsets = sections.str_offsets;
12523 /* The "str" section is global to the entire DWP file. */
12524 dwo_file->sections.str = dwp_file->sections.str;
12525 /* The info or types section is assigned below to dwo_unit,
12526 there's no need to record it in dwo_file.
12527 Also, we can't simply record type sections in dwo_file because
12528 we record a pointer into the vector in dwo_unit. As we collect more
12529 types we'll grow the vector and eventually have to reallocate space
12530 for it, invalidating all copies of pointers into the previous
12531 contents. */
12532 *dwo_file_slot = dwo_file;
12533 }
12534 else
12535 {
12536 if (dwarf_read_debug)
12537 {
12538 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12539 virtual_dwo_name.c_str ());
12540 }
12541 dwo_file = (struct dwo_file *) *dwo_file_slot;
12542 }
12543
12544 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12545 dwo_unit->dwo_file = dwo_file;
12546 dwo_unit->signature = signature;
12547 dwo_unit->section =
12548 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12549 *dwo_unit->section = sections.info_or_types;
12550 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12551
12552 return dwo_unit;
12553 }
12554
12555 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12556 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12557 piece within that section used by a TU/CU, return a virtual section
12558 of just that piece. */
12559
12560 static struct dwarf2_section_info
12561 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12562 struct dwarf2_section_info *section,
12563 bfd_size_type offset, bfd_size_type size)
12564 {
12565 struct dwarf2_section_info result;
12566 asection *sectp;
12567
12568 gdb_assert (section != NULL);
12569 gdb_assert (!section->is_virtual);
12570
12571 memset (&result, 0, sizeof (result));
12572 result.s.containing_section = section;
12573 result.is_virtual = true;
12574
12575 if (size == 0)
12576 return result;
12577
12578 sectp = get_section_bfd_section (section);
12579
12580 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12581 bounds of the real section. This is a pretty-rare event, so just
12582 flag an error (easier) instead of a warning and trying to cope. */
12583 if (sectp == NULL
12584 || offset + size > bfd_section_size (sectp))
12585 {
12586 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12587 " in section %s [in module %s]"),
12588 sectp ? bfd_section_name (sectp) : "<unknown>",
12589 objfile_name (dwarf2_per_objfile->objfile));
12590 }
12591
12592 result.virtual_offset = offset;
12593 result.size = size;
12594 return result;
12595 }
12596
12597 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12598 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12599 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12600 This is for DWP version 2 files. */
12601
12602 static struct dwo_unit *
12603 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12604 struct dwp_file *dwp_file,
12605 uint32_t unit_index,
12606 const char *comp_dir,
12607 ULONGEST signature, int is_debug_types)
12608 {
12609 struct objfile *objfile = dwarf2_per_objfile->objfile;
12610 const struct dwp_hash_table *dwp_htab =
12611 is_debug_types ? dwp_file->tus : dwp_file->cus;
12612 bfd *dbfd = dwp_file->dbfd.get ();
12613 const char *kind = is_debug_types ? "TU" : "CU";
12614 struct dwo_file *dwo_file;
12615 struct dwo_unit *dwo_unit;
12616 struct virtual_v2_dwo_sections sections;
12617 void **dwo_file_slot;
12618 int i;
12619
12620 gdb_assert (dwp_file->version == 2);
12621
12622 if (dwarf_read_debug)
12623 {
12624 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12625 kind,
12626 pulongest (unit_index), hex_string (signature),
12627 dwp_file->name);
12628 }
12629
12630 /* Fetch the section offsets of this DWO unit. */
12631
12632 memset (&sections, 0, sizeof (sections));
12633
12634 for (i = 0; i < dwp_htab->nr_columns; ++i)
12635 {
12636 uint32_t offset = read_4_bytes (dbfd,
12637 dwp_htab->section_pool.v2.offsets
12638 + (((unit_index - 1) * dwp_htab->nr_columns
12639 + i)
12640 * sizeof (uint32_t)));
12641 uint32_t size = read_4_bytes (dbfd,
12642 dwp_htab->section_pool.v2.sizes
12643 + (((unit_index - 1) * dwp_htab->nr_columns
12644 + i)
12645 * sizeof (uint32_t)));
12646
12647 switch (dwp_htab->section_pool.v2.section_ids[i])
12648 {
12649 case DW_SECT_INFO:
12650 case DW_SECT_TYPES:
12651 sections.info_or_types_offset = offset;
12652 sections.info_or_types_size = size;
12653 break;
12654 case DW_SECT_ABBREV:
12655 sections.abbrev_offset = offset;
12656 sections.abbrev_size = size;
12657 break;
12658 case DW_SECT_LINE:
12659 sections.line_offset = offset;
12660 sections.line_size = size;
12661 break;
12662 case DW_SECT_LOC:
12663 sections.loc_offset = offset;
12664 sections.loc_size = size;
12665 break;
12666 case DW_SECT_STR_OFFSETS:
12667 sections.str_offsets_offset = offset;
12668 sections.str_offsets_size = size;
12669 break;
12670 case DW_SECT_MACINFO:
12671 sections.macinfo_offset = offset;
12672 sections.macinfo_size = size;
12673 break;
12674 case DW_SECT_MACRO:
12675 sections.macro_offset = offset;
12676 sections.macro_size = size;
12677 break;
12678 }
12679 }
12680
12681 /* It's easier for the rest of the code if we fake a struct dwo_file and
12682 have dwo_unit "live" in that. At least for now.
12683
12684 The DWP file can be made up of a random collection of CUs and TUs.
12685 However, for each CU + set of TUs that came from the same original DWO
12686 file, we can combine them back into a virtual DWO file to save space
12687 (fewer struct dwo_file objects to allocate). Remember that for really
12688 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12689
12690 std::string virtual_dwo_name =
12691 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12692 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12693 (long) (sections.line_size ? sections.line_offset : 0),
12694 (long) (sections.loc_size ? sections.loc_offset : 0),
12695 (long) (sections.str_offsets_size
12696 ? sections.str_offsets_offset : 0));
12697 /* Can we use an existing virtual DWO file? */
12698 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12699 virtual_dwo_name.c_str (),
12700 comp_dir);
12701 /* Create one if necessary. */
12702 if (*dwo_file_slot == NULL)
12703 {
12704 if (dwarf_read_debug)
12705 {
12706 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12707 virtual_dwo_name.c_str ());
12708 }
12709 dwo_file = new struct dwo_file;
12710 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12711 virtual_dwo_name);
12712 dwo_file->comp_dir = comp_dir;
12713 dwo_file->sections.abbrev =
12714 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12715 sections.abbrev_offset, sections.abbrev_size);
12716 dwo_file->sections.line =
12717 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12718 sections.line_offset, sections.line_size);
12719 dwo_file->sections.loc =
12720 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12721 sections.loc_offset, sections.loc_size);
12722 dwo_file->sections.macinfo =
12723 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12724 sections.macinfo_offset, sections.macinfo_size);
12725 dwo_file->sections.macro =
12726 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12727 sections.macro_offset, sections.macro_size);
12728 dwo_file->sections.str_offsets =
12729 create_dwp_v2_section (dwarf2_per_objfile,
12730 &dwp_file->sections.str_offsets,
12731 sections.str_offsets_offset,
12732 sections.str_offsets_size);
12733 /* The "str" section is global to the entire DWP file. */
12734 dwo_file->sections.str = dwp_file->sections.str;
12735 /* The info or types section is assigned below to dwo_unit,
12736 there's no need to record it in dwo_file.
12737 Also, we can't simply record type sections in dwo_file because
12738 we record a pointer into the vector in dwo_unit. As we collect more
12739 types we'll grow the vector and eventually have to reallocate space
12740 for it, invalidating all copies of pointers into the previous
12741 contents. */
12742 *dwo_file_slot = dwo_file;
12743 }
12744 else
12745 {
12746 if (dwarf_read_debug)
12747 {
12748 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12749 virtual_dwo_name.c_str ());
12750 }
12751 dwo_file = (struct dwo_file *) *dwo_file_slot;
12752 }
12753
12754 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12755 dwo_unit->dwo_file = dwo_file;
12756 dwo_unit->signature = signature;
12757 dwo_unit->section =
12758 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12759 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12760 is_debug_types
12761 ? &dwp_file->sections.types
12762 : &dwp_file->sections.info,
12763 sections.info_or_types_offset,
12764 sections.info_or_types_size);
12765 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12766
12767 return dwo_unit;
12768 }
12769
12770 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12771 Returns NULL if the signature isn't found. */
12772
12773 static struct dwo_unit *
12774 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12775 struct dwp_file *dwp_file, const char *comp_dir,
12776 ULONGEST signature, int is_debug_types)
12777 {
12778 const struct dwp_hash_table *dwp_htab =
12779 is_debug_types ? dwp_file->tus : dwp_file->cus;
12780 bfd *dbfd = dwp_file->dbfd.get ();
12781 uint32_t mask = dwp_htab->nr_slots - 1;
12782 uint32_t hash = signature & mask;
12783 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12784 unsigned int i;
12785 void **slot;
12786 struct dwo_unit find_dwo_cu;
12787
12788 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12789 find_dwo_cu.signature = signature;
12790 slot = htab_find_slot (is_debug_types
12791 ? dwp_file->loaded_tus
12792 : dwp_file->loaded_cus,
12793 &find_dwo_cu, INSERT);
12794
12795 if (*slot != NULL)
12796 return (struct dwo_unit *) *slot;
12797
12798 /* Use a for loop so that we don't loop forever on bad debug info. */
12799 for (i = 0; i < dwp_htab->nr_slots; ++i)
12800 {
12801 ULONGEST signature_in_table;
12802
12803 signature_in_table =
12804 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12805 if (signature_in_table == signature)
12806 {
12807 uint32_t unit_index =
12808 read_4_bytes (dbfd,
12809 dwp_htab->unit_table + hash * sizeof (uint32_t));
12810
12811 if (dwp_file->version == 1)
12812 {
12813 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12814 dwp_file, unit_index,
12815 comp_dir, signature,
12816 is_debug_types);
12817 }
12818 else
12819 {
12820 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12821 dwp_file, unit_index,
12822 comp_dir, signature,
12823 is_debug_types);
12824 }
12825 return (struct dwo_unit *) *slot;
12826 }
12827 if (signature_in_table == 0)
12828 return NULL;
12829 hash = (hash + hash2) & mask;
12830 }
12831
12832 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12833 " [in module %s]"),
12834 dwp_file->name);
12835 }
12836
12837 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12838 Open the file specified by FILE_NAME and hand it off to BFD for
12839 preliminary analysis. Return a newly initialized bfd *, which
12840 includes a canonicalized copy of FILE_NAME.
12841 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12842 SEARCH_CWD is true if the current directory is to be searched.
12843 It will be searched before debug-file-directory.
12844 If successful, the file is added to the bfd include table of the
12845 objfile's bfd (see gdb_bfd_record_inclusion).
12846 If unable to find/open the file, return NULL.
12847 NOTE: This function is derived from symfile_bfd_open. */
12848
12849 static gdb_bfd_ref_ptr
12850 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12851 const char *file_name, int is_dwp, int search_cwd)
12852 {
12853 int desc;
12854 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12855 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12856 to debug_file_directory. */
12857 const char *search_path;
12858 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12859
12860 gdb::unique_xmalloc_ptr<char> search_path_holder;
12861 if (search_cwd)
12862 {
12863 if (*debug_file_directory != '\0')
12864 {
12865 search_path_holder.reset (concat (".", dirname_separator_string,
12866 debug_file_directory,
12867 (char *) NULL));
12868 search_path = search_path_holder.get ();
12869 }
12870 else
12871 search_path = ".";
12872 }
12873 else
12874 search_path = debug_file_directory;
12875
12876 openp_flags flags = OPF_RETURN_REALPATH;
12877 if (is_dwp)
12878 flags |= OPF_SEARCH_IN_PATH;
12879
12880 gdb::unique_xmalloc_ptr<char> absolute_name;
12881 desc = openp (search_path, flags, file_name,
12882 O_RDONLY | O_BINARY, &absolute_name);
12883 if (desc < 0)
12884 return NULL;
12885
12886 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12887 gnutarget, desc));
12888 if (sym_bfd == NULL)
12889 return NULL;
12890 bfd_set_cacheable (sym_bfd.get (), 1);
12891
12892 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12893 return NULL;
12894
12895 /* Success. Record the bfd as having been included by the objfile's bfd.
12896 This is important because things like demangled_names_hash lives in the
12897 objfile's per_bfd space and may have references to things like symbol
12898 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12899 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12900
12901 return sym_bfd;
12902 }
12903
12904 /* Try to open DWO file FILE_NAME.
12905 COMP_DIR is the DW_AT_comp_dir attribute.
12906 The result is the bfd handle of the file.
12907 If there is a problem finding or opening the file, return NULL.
12908 Upon success, the canonicalized path of the file is stored in the bfd,
12909 same as symfile_bfd_open. */
12910
12911 static gdb_bfd_ref_ptr
12912 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12913 const char *file_name, const char *comp_dir)
12914 {
12915 if (IS_ABSOLUTE_PATH (file_name))
12916 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12917 0 /*is_dwp*/, 0 /*search_cwd*/);
12918
12919 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12920
12921 if (comp_dir != NULL)
12922 {
12923 gdb::unique_xmalloc_ptr<char> path_to_try
12924 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12925
12926 /* NOTE: If comp_dir is a relative path, this will also try the
12927 search path, which seems useful. */
12928 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12929 path_to_try.get (),
12930 0 /*is_dwp*/,
12931 1 /*search_cwd*/));
12932 if (abfd != NULL)
12933 return abfd;
12934 }
12935
12936 /* That didn't work, try debug-file-directory, which, despite its name,
12937 is a list of paths. */
12938
12939 if (*debug_file_directory == '\0')
12940 return NULL;
12941
12942 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12943 0 /*is_dwp*/, 1 /*search_cwd*/);
12944 }
12945
12946 /* This function is mapped across the sections and remembers the offset and
12947 size of each of the DWO debugging sections we are interested in. */
12948
12949 static void
12950 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12951 {
12952 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12953 const struct dwop_section_names *names = &dwop_section_names;
12954
12955 if (section_is_p (sectp->name, &names->abbrev_dwo))
12956 {
12957 dwo_sections->abbrev.s.section = sectp;
12958 dwo_sections->abbrev.size = bfd_section_size (sectp);
12959 }
12960 else if (section_is_p (sectp->name, &names->info_dwo))
12961 {
12962 dwo_sections->info.s.section = sectp;
12963 dwo_sections->info.size = bfd_section_size (sectp);
12964 }
12965 else if (section_is_p (sectp->name, &names->line_dwo))
12966 {
12967 dwo_sections->line.s.section = sectp;
12968 dwo_sections->line.size = bfd_section_size (sectp);
12969 }
12970 else if (section_is_p (sectp->name, &names->loc_dwo))
12971 {
12972 dwo_sections->loc.s.section = sectp;
12973 dwo_sections->loc.size = bfd_section_size (sectp);
12974 }
12975 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12976 {
12977 dwo_sections->macinfo.s.section = sectp;
12978 dwo_sections->macinfo.size = bfd_section_size (sectp);
12979 }
12980 else if (section_is_p (sectp->name, &names->macro_dwo))
12981 {
12982 dwo_sections->macro.s.section = sectp;
12983 dwo_sections->macro.size = bfd_section_size (sectp);
12984 }
12985 else if (section_is_p (sectp->name, &names->str_dwo))
12986 {
12987 dwo_sections->str.s.section = sectp;
12988 dwo_sections->str.size = bfd_section_size (sectp);
12989 }
12990 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12991 {
12992 dwo_sections->str_offsets.s.section = sectp;
12993 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12994 }
12995 else if (section_is_p (sectp->name, &names->types_dwo))
12996 {
12997 struct dwarf2_section_info type_section;
12998
12999 memset (&type_section, 0, sizeof (type_section));
13000 type_section.s.section = sectp;
13001 type_section.size = bfd_section_size (sectp);
13002 dwo_sections->types.push_back (type_section);
13003 }
13004 }
13005
13006 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13007 by PER_CU. This is for the non-DWP case.
13008 The result is NULL if DWO_NAME can't be found. */
13009
13010 static struct dwo_file *
13011 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13012 const char *dwo_name, const char *comp_dir)
13013 {
13014 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13015
13016 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
13017 if (dbfd == NULL)
13018 {
13019 if (dwarf_read_debug)
13020 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13021 return NULL;
13022 }
13023
13024 dwo_file_up dwo_file (new struct dwo_file);
13025 dwo_file->dwo_name = dwo_name;
13026 dwo_file->comp_dir = comp_dir;
13027 dwo_file->dbfd = std::move (dbfd);
13028
13029 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
13030 &dwo_file->sections);
13031
13032 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
13033 dwo_file->sections.info, dwo_file->cus);
13034
13035 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13036 dwo_file->sections.types, dwo_file->tus);
13037
13038 if (dwarf_read_debug)
13039 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13040
13041 return dwo_file.release ();
13042 }
13043
13044 /* This function is mapped across the sections and remembers the offset and
13045 size of each of the DWP debugging sections common to version 1 and 2 that
13046 we are interested in. */
13047
13048 static void
13049 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13050 void *dwp_file_ptr)
13051 {
13052 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13053 const struct dwop_section_names *names = &dwop_section_names;
13054 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13055
13056 /* Record the ELF section number for later lookup: this is what the
13057 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13058 gdb_assert (elf_section_nr < dwp_file->num_sections);
13059 dwp_file->elf_sections[elf_section_nr] = sectp;
13060
13061 /* Look for specific sections that we need. */
13062 if (section_is_p (sectp->name, &names->str_dwo))
13063 {
13064 dwp_file->sections.str.s.section = sectp;
13065 dwp_file->sections.str.size = bfd_section_size (sectp);
13066 }
13067 else if (section_is_p (sectp->name, &names->cu_index))
13068 {
13069 dwp_file->sections.cu_index.s.section = sectp;
13070 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13071 }
13072 else if (section_is_p (sectp->name, &names->tu_index))
13073 {
13074 dwp_file->sections.tu_index.s.section = sectp;
13075 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13076 }
13077 }
13078
13079 /* This function is mapped across the sections and remembers the offset and
13080 size of each of the DWP version 2 debugging sections that we are interested
13081 in. This is split into a separate function because we don't know if we
13082 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13083
13084 static void
13085 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13086 {
13087 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13088 const struct dwop_section_names *names = &dwop_section_names;
13089 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13090
13091 /* Record the ELF section number for later lookup: this is what the
13092 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13093 gdb_assert (elf_section_nr < dwp_file->num_sections);
13094 dwp_file->elf_sections[elf_section_nr] = sectp;
13095
13096 /* Look for specific sections that we need. */
13097 if (section_is_p (sectp->name, &names->abbrev_dwo))
13098 {
13099 dwp_file->sections.abbrev.s.section = sectp;
13100 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13101 }
13102 else if (section_is_p (sectp->name, &names->info_dwo))
13103 {
13104 dwp_file->sections.info.s.section = sectp;
13105 dwp_file->sections.info.size = bfd_section_size (sectp);
13106 }
13107 else if (section_is_p (sectp->name, &names->line_dwo))
13108 {
13109 dwp_file->sections.line.s.section = sectp;
13110 dwp_file->sections.line.size = bfd_section_size (sectp);
13111 }
13112 else if (section_is_p (sectp->name, &names->loc_dwo))
13113 {
13114 dwp_file->sections.loc.s.section = sectp;
13115 dwp_file->sections.loc.size = bfd_section_size (sectp);
13116 }
13117 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13118 {
13119 dwp_file->sections.macinfo.s.section = sectp;
13120 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13121 }
13122 else if (section_is_p (sectp->name, &names->macro_dwo))
13123 {
13124 dwp_file->sections.macro.s.section = sectp;
13125 dwp_file->sections.macro.size = bfd_section_size (sectp);
13126 }
13127 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13128 {
13129 dwp_file->sections.str_offsets.s.section = sectp;
13130 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13131 }
13132 else if (section_is_p (sectp->name, &names->types_dwo))
13133 {
13134 dwp_file->sections.types.s.section = sectp;
13135 dwp_file->sections.types.size = bfd_section_size (sectp);
13136 }
13137 }
13138
13139 /* Hash function for dwp_file loaded CUs/TUs. */
13140
13141 static hashval_t
13142 hash_dwp_loaded_cutus (const void *item)
13143 {
13144 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13145
13146 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13147 return dwo_unit->signature;
13148 }
13149
13150 /* Equality function for dwp_file loaded CUs/TUs. */
13151
13152 static int
13153 eq_dwp_loaded_cutus (const void *a, const void *b)
13154 {
13155 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13156 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13157
13158 return dua->signature == dub->signature;
13159 }
13160
13161 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13162
13163 static htab_t
13164 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13165 {
13166 return htab_create_alloc_ex (3,
13167 hash_dwp_loaded_cutus,
13168 eq_dwp_loaded_cutus,
13169 NULL,
13170 &objfile->objfile_obstack,
13171 hashtab_obstack_allocate,
13172 dummy_obstack_deallocate);
13173 }
13174
13175 /* Try to open DWP file FILE_NAME.
13176 The result is the bfd handle of the file.
13177 If there is a problem finding or opening the file, return NULL.
13178 Upon success, the canonicalized path of the file is stored in the bfd,
13179 same as symfile_bfd_open. */
13180
13181 static gdb_bfd_ref_ptr
13182 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13183 const char *file_name)
13184 {
13185 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13186 1 /*is_dwp*/,
13187 1 /*search_cwd*/));
13188 if (abfd != NULL)
13189 return abfd;
13190
13191 /* Work around upstream bug 15652.
13192 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13193 [Whether that's a "bug" is debatable, but it is getting in our way.]
13194 We have no real idea where the dwp file is, because gdb's realpath-ing
13195 of the executable's path may have discarded the needed info.
13196 [IWBN if the dwp file name was recorded in the executable, akin to
13197 .gnu_debuglink, but that doesn't exist yet.]
13198 Strip the directory from FILE_NAME and search again. */
13199 if (*debug_file_directory != '\0')
13200 {
13201 /* Don't implicitly search the current directory here.
13202 If the user wants to search "." to handle this case,
13203 it must be added to debug-file-directory. */
13204 return try_open_dwop_file (dwarf2_per_objfile,
13205 lbasename (file_name), 1 /*is_dwp*/,
13206 0 /*search_cwd*/);
13207 }
13208
13209 return NULL;
13210 }
13211
13212 /* Initialize the use of the DWP file for the current objfile.
13213 By convention the name of the DWP file is ${objfile}.dwp.
13214 The result is NULL if it can't be found. */
13215
13216 static std::unique_ptr<struct dwp_file>
13217 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13218 {
13219 struct objfile *objfile = dwarf2_per_objfile->objfile;
13220
13221 /* Try to find first .dwp for the binary file before any symbolic links
13222 resolving. */
13223
13224 /* If the objfile is a debug file, find the name of the real binary
13225 file and get the name of dwp file from there. */
13226 std::string dwp_name;
13227 if (objfile->separate_debug_objfile_backlink != NULL)
13228 {
13229 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13230 const char *backlink_basename = lbasename (backlink->original_name);
13231
13232 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13233 }
13234 else
13235 dwp_name = objfile->original_name;
13236
13237 dwp_name += ".dwp";
13238
13239 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13240 if (dbfd == NULL
13241 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13242 {
13243 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13244 dwp_name = objfile_name (objfile);
13245 dwp_name += ".dwp";
13246 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13247 }
13248
13249 if (dbfd == NULL)
13250 {
13251 if (dwarf_read_debug)
13252 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13253 return std::unique_ptr<dwp_file> ();
13254 }
13255
13256 const char *name = bfd_get_filename (dbfd.get ());
13257 std::unique_ptr<struct dwp_file> dwp_file
13258 (new struct dwp_file (name, std::move (dbfd)));
13259
13260 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13261 dwp_file->elf_sections =
13262 OBSTACK_CALLOC (&objfile->objfile_obstack,
13263 dwp_file->num_sections, asection *);
13264
13265 bfd_map_over_sections (dwp_file->dbfd.get (),
13266 dwarf2_locate_common_dwp_sections,
13267 dwp_file.get ());
13268
13269 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13270 0);
13271
13272 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13273 1);
13274
13275 /* The DWP file version is stored in the hash table. Oh well. */
13276 if (dwp_file->cus && dwp_file->tus
13277 && dwp_file->cus->version != dwp_file->tus->version)
13278 {
13279 /* Technically speaking, we should try to limp along, but this is
13280 pretty bizarre. We use pulongest here because that's the established
13281 portability solution (e.g, we cannot use %u for uint32_t). */
13282 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13283 " TU version %s [in DWP file %s]"),
13284 pulongest (dwp_file->cus->version),
13285 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13286 }
13287
13288 if (dwp_file->cus)
13289 dwp_file->version = dwp_file->cus->version;
13290 else if (dwp_file->tus)
13291 dwp_file->version = dwp_file->tus->version;
13292 else
13293 dwp_file->version = 2;
13294
13295 if (dwp_file->version == 2)
13296 bfd_map_over_sections (dwp_file->dbfd.get (),
13297 dwarf2_locate_v2_dwp_sections,
13298 dwp_file.get ());
13299
13300 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13301 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13302
13303 if (dwarf_read_debug)
13304 {
13305 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13306 fprintf_unfiltered (gdb_stdlog,
13307 " %s CUs, %s TUs\n",
13308 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13309 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13310 }
13311
13312 return dwp_file;
13313 }
13314
13315 /* Wrapper around open_and_init_dwp_file, only open it once. */
13316
13317 static struct dwp_file *
13318 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13319 {
13320 if (! dwarf2_per_objfile->dwp_checked)
13321 {
13322 dwarf2_per_objfile->dwp_file
13323 = open_and_init_dwp_file (dwarf2_per_objfile);
13324 dwarf2_per_objfile->dwp_checked = 1;
13325 }
13326 return dwarf2_per_objfile->dwp_file.get ();
13327 }
13328
13329 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13330 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13331 or in the DWP file for the objfile, referenced by THIS_UNIT.
13332 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13333 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13334
13335 This is called, for example, when wanting to read a variable with a
13336 complex location. Therefore we don't want to do file i/o for every call.
13337 Therefore we don't want to look for a DWO file on every call.
13338 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13339 then we check if we've already seen DWO_NAME, and only THEN do we check
13340 for a DWO file.
13341
13342 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13343 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13344
13345 static struct dwo_unit *
13346 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13347 const char *dwo_name, const char *comp_dir,
13348 ULONGEST signature, int is_debug_types)
13349 {
13350 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13351 struct objfile *objfile = dwarf2_per_objfile->objfile;
13352 const char *kind = is_debug_types ? "TU" : "CU";
13353 void **dwo_file_slot;
13354 struct dwo_file *dwo_file;
13355 struct dwp_file *dwp_file;
13356
13357 /* First see if there's a DWP file.
13358 If we have a DWP file but didn't find the DWO inside it, don't
13359 look for the original DWO file. It makes gdb behave differently
13360 depending on whether one is debugging in the build tree. */
13361
13362 dwp_file = get_dwp_file (dwarf2_per_objfile);
13363 if (dwp_file != NULL)
13364 {
13365 const struct dwp_hash_table *dwp_htab =
13366 is_debug_types ? dwp_file->tus : dwp_file->cus;
13367
13368 if (dwp_htab != NULL)
13369 {
13370 struct dwo_unit *dwo_cutu =
13371 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13372 signature, is_debug_types);
13373
13374 if (dwo_cutu != NULL)
13375 {
13376 if (dwarf_read_debug)
13377 {
13378 fprintf_unfiltered (gdb_stdlog,
13379 "Virtual DWO %s %s found: @%s\n",
13380 kind, hex_string (signature),
13381 host_address_to_string (dwo_cutu));
13382 }
13383 return dwo_cutu;
13384 }
13385 }
13386 }
13387 else
13388 {
13389 /* No DWP file, look for the DWO file. */
13390
13391 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13392 dwo_name, comp_dir);
13393 if (*dwo_file_slot == NULL)
13394 {
13395 /* Read in the file and build a table of the CUs/TUs it contains. */
13396 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13397 }
13398 /* NOTE: This will be NULL if unable to open the file. */
13399 dwo_file = (struct dwo_file *) *dwo_file_slot;
13400
13401 if (dwo_file != NULL)
13402 {
13403 struct dwo_unit *dwo_cutu = NULL;
13404
13405 if (is_debug_types && dwo_file->tus)
13406 {
13407 struct dwo_unit find_dwo_cutu;
13408
13409 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13410 find_dwo_cutu.signature = signature;
13411 dwo_cutu
13412 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13413 }
13414 else if (!is_debug_types && dwo_file->cus)
13415 {
13416 struct dwo_unit find_dwo_cutu;
13417
13418 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13419 find_dwo_cutu.signature = signature;
13420 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13421 &find_dwo_cutu);
13422 }
13423
13424 if (dwo_cutu != NULL)
13425 {
13426 if (dwarf_read_debug)
13427 {
13428 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13429 kind, dwo_name, hex_string (signature),
13430 host_address_to_string (dwo_cutu));
13431 }
13432 return dwo_cutu;
13433 }
13434 }
13435 }
13436
13437 /* We didn't find it. This could mean a dwo_id mismatch, or
13438 someone deleted the DWO/DWP file, or the search path isn't set up
13439 correctly to find the file. */
13440
13441 if (dwarf_read_debug)
13442 {
13443 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13444 kind, dwo_name, hex_string (signature));
13445 }
13446
13447 /* This is a warning and not a complaint because it can be caused by
13448 pilot error (e.g., user accidentally deleting the DWO). */
13449 {
13450 /* Print the name of the DWP file if we looked there, helps the user
13451 better diagnose the problem. */
13452 std::string dwp_text;
13453
13454 if (dwp_file != NULL)
13455 dwp_text = string_printf (" [in DWP file %s]",
13456 lbasename (dwp_file->name));
13457
13458 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13459 " [in module %s]"),
13460 kind, dwo_name, hex_string (signature),
13461 dwp_text.c_str (),
13462 this_unit->is_debug_types ? "TU" : "CU",
13463 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13464 }
13465 return NULL;
13466 }
13467
13468 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13469 See lookup_dwo_cutu_unit for details. */
13470
13471 static struct dwo_unit *
13472 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13473 const char *dwo_name, const char *comp_dir,
13474 ULONGEST signature)
13475 {
13476 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13477 }
13478
13479 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13480 See lookup_dwo_cutu_unit for details. */
13481
13482 static struct dwo_unit *
13483 lookup_dwo_type_unit (struct signatured_type *this_tu,
13484 const char *dwo_name, const char *comp_dir)
13485 {
13486 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13487 }
13488
13489 /* Traversal function for queue_and_load_all_dwo_tus. */
13490
13491 static int
13492 queue_and_load_dwo_tu (void **slot, void *info)
13493 {
13494 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13495 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13496 ULONGEST signature = dwo_unit->signature;
13497 struct signatured_type *sig_type =
13498 lookup_dwo_signatured_type (per_cu->cu, signature);
13499
13500 if (sig_type != NULL)
13501 {
13502 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13503
13504 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13505 a real dependency of PER_CU on SIG_TYPE. That is detected later
13506 while processing PER_CU. */
13507 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13508 load_full_type_unit (sig_cu);
13509 per_cu->imported_symtabs_push (sig_cu);
13510 }
13511
13512 return 1;
13513 }
13514
13515 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13516 The DWO may have the only definition of the type, though it may not be
13517 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13518 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13519
13520 static void
13521 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13522 {
13523 struct dwo_unit *dwo_unit;
13524 struct dwo_file *dwo_file;
13525
13526 gdb_assert (!per_cu->is_debug_types);
13527 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13528 gdb_assert (per_cu->cu != NULL);
13529
13530 dwo_unit = per_cu->cu->dwo_unit;
13531 gdb_assert (dwo_unit != NULL);
13532
13533 dwo_file = dwo_unit->dwo_file;
13534 if (dwo_file->tus != NULL)
13535 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13536 }
13537
13538 /* Read in various DIEs. */
13539
13540 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13541 Inherit only the children of the DW_AT_abstract_origin DIE not being
13542 already referenced by DW_AT_abstract_origin from the children of the
13543 current DIE. */
13544
13545 static void
13546 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13547 {
13548 struct die_info *child_die;
13549 sect_offset *offsetp;
13550 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13551 struct die_info *origin_die;
13552 /* Iterator of the ORIGIN_DIE children. */
13553 struct die_info *origin_child_die;
13554 struct attribute *attr;
13555 struct dwarf2_cu *origin_cu;
13556 struct pending **origin_previous_list_in_scope;
13557
13558 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13559 if (!attr)
13560 return;
13561
13562 /* Note that following die references may follow to a die in a
13563 different cu. */
13564
13565 origin_cu = cu;
13566 origin_die = follow_die_ref (die, attr, &origin_cu);
13567
13568 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13569 symbols in. */
13570 origin_previous_list_in_scope = origin_cu->list_in_scope;
13571 origin_cu->list_in_scope = cu->list_in_scope;
13572
13573 if (die->tag != origin_die->tag
13574 && !(die->tag == DW_TAG_inlined_subroutine
13575 && origin_die->tag == DW_TAG_subprogram))
13576 complaint (_("DIE %s and its abstract origin %s have different tags"),
13577 sect_offset_str (die->sect_off),
13578 sect_offset_str (origin_die->sect_off));
13579
13580 std::vector<sect_offset> offsets;
13581
13582 for (child_die = die->child;
13583 child_die && child_die->tag;
13584 child_die = sibling_die (child_die))
13585 {
13586 struct die_info *child_origin_die;
13587 struct dwarf2_cu *child_origin_cu;
13588
13589 /* We are trying to process concrete instance entries:
13590 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13591 it's not relevant to our analysis here. i.e. detecting DIEs that are
13592 present in the abstract instance but not referenced in the concrete
13593 one. */
13594 if (child_die->tag == DW_TAG_call_site
13595 || child_die->tag == DW_TAG_GNU_call_site)
13596 continue;
13597
13598 /* For each CHILD_DIE, find the corresponding child of
13599 ORIGIN_DIE. If there is more than one layer of
13600 DW_AT_abstract_origin, follow them all; there shouldn't be,
13601 but GCC versions at least through 4.4 generate this (GCC PR
13602 40573). */
13603 child_origin_die = child_die;
13604 child_origin_cu = cu;
13605 while (1)
13606 {
13607 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13608 child_origin_cu);
13609 if (attr == NULL)
13610 break;
13611 child_origin_die = follow_die_ref (child_origin_die, attr,
13612 &child_origin_cu);
13613 }
13614
13615 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13616 counterpart may exist. */
13617 if (child_origin_die != child_die)
13618 {
13619 if (child_die->tag != child_origin_die->tag
13620 && !(child_die->tag == DW_TAG_inlined_subroutine
13621 && child_origin_die->tag == DW_TAG_subprogram))
13622 complaint (_("Child DIE %s and its abstract origin %s have "
13623 "different tags"),
13624 sect_offset_str (child_die->sect_off),
13625 sect_offset_str (child_origin_die->sect_off));
13626 if (child_origin_die->parent != origin_die)
13627 complaint (_("Child DIE %s and its abstract origin %s have "
13628 "different parents"),
13629 sect_offset_str (child_die->sect_off),
13630 sect_offset_str (child_origin_die->sect_off));
13631 else
13632 offsets.push_back (child_origin_die->sect_off);
13633 }
13634 }
13635 std::sort (offsets.begin (), offsets.end ());
13636 sect_offset *offsets_end = offsets.data () + offsets.size ();
13637 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13638 if (offsetp[-1] == *offsetp)
13639 complaint (_("Multiple children of DIE %s refer "
13640 "to DIE %s as their abstract origin"),
13641 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13642
13643 offsetp = offsets.data ();
13644 origin_child_die = origin_die->child;
13645 while (origin_child_die && origin_child_die->tag)
13646 {
13647 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13648 while (offsetp < offsets_end
13649 && *offsetp < origin_child_die->sect_off)
13650 offsetp++;
13651 if (offsetp >= offsets_end
13652 || *offsetp > origin_child_die->sect_off)
13653 {
13654 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13655 Check whether we're already processing ORIGIN_CHILD_DIE.
13656 This can happen with mutually referenced abstract_origins.
13657 PR 16581. */
13658 if (!origin_child_die->in_process)
13659 process_die (origin_child_die, origin_cu);
13660 }
13661 origin_child_die = sibling_die (origin_child_die);
13662 }
13663 origin_cu->list_in_scope = origin_previous_list_in_scope;
13664
13665 if (cu != origin_cu)
13666 compute_delayed_physnames (origin_cu);
13667 }
13668
13669 static void
13670 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13671 {
13672 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13673 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13674 struct context_stack *newobj;
13675 CORE_ADDR lowpc;
13676 CORE_ADDR highpc;
13677 struct die_info *child_die;
13678 struct attribute *attr, *call_line, *call_file;
13679 const char *name;
13680 CORE_ADDR baseaddr;
13681 struct block *block;
13682 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13683 std::vector<struct symbol *> template_args;
13684 struct template_symbol *templ_func = NULL;
13685
13686 if (inlined_func)
13687 {
13688 /* If we do not have call site information, we can't show the
13689 caller of this inlined function. That's too confusing, so
13690 only use the scope for local variables. */
13691 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13692 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13693 if (call_line == NULL || call_file == NULL)
13694 {
13695 read_lexical_block_scope (die, cu);
13696 return;
13697 }
13698 }
13699
13700 baseaddr = objfile->text_section_offset ();
13701
13702 name = dwarf2_name (die, cu);
13703
13704 /* Ignore functions with missing or empty names. These are actually
13705 illegal according to the DWARF standard. */
13706 if (name == NULL)
13707 {
13708 complaint (_("missing name for subprogram DIE at %s"),
13709 sect_offset_str (die->sect_off));
13710 return;
13711 }
13712
13713 /* Ignore functions with missing or invalid low and high pc attributes. */
13714 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13715 <= PC_BOUNDS_INVALID)
13716 {
13717 attr = dwarf2_attr (die, DW_AT_external, cu);
13718 if (!attr || !DW_UNSND (attr))
13719 complaint (_("cannot get low and high bounds "
13720 "for subprogram DIE at %s"),
13721 sect_offset_str (die->sect_off));
13722 return;
13723 }
13724
13725 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13726 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13727
13728 /* If we have any template arguments, then we must allocate a
13729 different sort of symbol. */
13730 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13731 {
13732 if (child_die->tag == DW_TAG_template_type_param
13733 || child_die->tag == DW_TAG_template_value_param)
13734 {
13735 templ_func = allocate_template_symbol (objfile);
13736 templ_func->subclass = SYMBOL_TEMPLATE;
13737 break;
13738 }
13739 }
13740
13741 newobj = cu->get_builder ()->push_context (0, lowpc);
13742 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13743 (struct symbol *) templ_func);
13744
13745 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13746 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13747 cu->language);
13748
13749 /* If there is a location expression for DW_AT_frame_base, record
13750 it. */
13751 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13752 if (attr != nullptr)
13753 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13754
13755 /* If there is a location for the static link, record it. */
13756 newobj->static_link = NULL;
13757 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13758 if (attr != nullptr)
13759 {
13760 newobj->static_link
13761 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13762 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13763 dwarf2_per_cu_addr_type (cu->per_cu));
13764 }
13765
13766 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13767
13768 if (die->child != NULL)
13769 {
13770 child_die = die->child;
13771 while (child_die && child_die->tag)
13772 {
13773 if (child_die->tag == DW_TAG_template_type_param
13774 || child_die->tag == DW_TAG_template_value_param)
13775 {
13776 struct symbol *arg = new_symbol (child_die, NULL, cu);
13777
13778 if (arg != NULL)
13779 template_args.push_back (arg);
13780 }
13781 else
13782 process_die (child_die, cu);
13783 child_die = sibling_die (child_die);
13784 }
13785 }
13786
13787 inherit_abstract_dies (die, cu);
13788
13789 /* If we have a DW_AT_specification, we might need to import using
13790 directives from the context of the specification DIE. See the
13791 comment in determine_prefix. */
13792 if (cu->language == language_cplus
13793 && dwarf2_attr (die, DW_AT_specification, cu))
13794 {
13795 struct dwarf2_cu *spec_cu = cu;
13796 struct die_info *spec_die = die_specification (die, &spec_cu);
13797
13798 while (spec_die)
13799 {
13800 child_die = spec_die->child;
13801 while (child_die && child_die->tag)
13802 {
13803 if (child_die->tag == DW_TAG_imported_module)
13804 process_die (child_die, spec_cu);
13805 child_die = sibling_die (child_die);
13806 }
13807
13808 /* In some cases, GCC generates specification DIEs that
13809 themselves contain DW_AT_specification attributes. */
13810 spec_die = die_specification (spec_die, &spec_cu);
13811 }
13812 }
13813
13814 struct context_stack cstk = cu->get_builder ()->pop_context ();
13815 /* Make a block for the local symbols within. */
13816 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13817 cstk.static_link, lowpc, highpc);
13818
13819 /* For C++, set the block's scope. */
13820 if ((cu->language == language_cplus
13821 || cu->language == language_fortran
13822 || cu->language == language_d
13823 || cu->language == language_rust)
13824 && cu->processing_has_namespace_info)
13825 block_set_scope (block, determine_prefix (die, cu),
13826 &objfile->objfile_obstack);
13827
13828 /* If we have address ranges, record them. */
13829 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13830
13831 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13832
13833 /* Attach template arguments to function. */
13834 if (!template_args.empty ())
13835 {
13836 gdb_assert (templ_func != NULL);
13837
13838 templ_func->n_template_arguments = template_args.size ();
13839 templ_func->template_arguments
13840 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13841 templ_func->n_template_arguments);
13842 memcpy (templ_func->template_arguments,
13843 template_args.data (),
13844 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13845
13846 /* Make sure that the symtab is set on the new symbols. Even
13847 though they don't appear in this symtab directly, other parts
13848 of gdb assume that symbols do, and this is reasonably
13849 true. */
13850 for (symbol *sym : template_args)
13851 symbol_set_symtab (sym, symbol_symtab (templ_func));
13852 }
13853
13854 /* In C++, we can have functions nested inside functions (e.g., when
13855 a function declares a class that has methods). This means that
13856 when we finish processing a function scope, we may need to go
13857 back to building a containing block's symbol lists. */
13858 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13859 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13860
13861 /* If we've finished processing a top-level function, subsequent
13862 symbols go in the file symbol list. */
13863 if (cu->get_builder ()->outermost_context_p ())
13864 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13865 }
13866
13867 /* Process all the DIES contained within a lexical block scope. Start
13868 a new scope, process the dies, and then close the scope. */
13869
13870 static void
13871 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13872 {
13873 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13874 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13875 CORE_ADDR lowpc, highpc;
13876 struct die_info *child_die;
13877 CORE_ADDR baseaddr;
13878
13879 baseaddr = objfile->text_section_offset ();
13880
13881 /* Ignore blocks with missing or invalid low and high pc attributes. */
13882 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13883 as multiple lexical blocks? Handling children in a sane way would
13884 be nasty. Might be easier to properly extend generic blocks to
13885 describe ranges. */
13886 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13887 {
13888 case PC_BOUNDS_NOT_PRESENT:
13889 /* DW_TAG_lexical_block has no attributes, process its children as if
13890 there was no wrapping by that DW_TAG_lexical_block.
13891 GCC does no longer produces such DWARF since GCC r224161. */
13892 for (child_die = die->child;
13893 child_die != NULL && child_die->tag;
13894 child_die = sibling_die (child_die))
13895 process_die (child_die, cu);
13896 return;
13897 case PC_BOUNDS_INVALID:
13898 return;
13899 }
13900 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13901 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13902
13903 cu->get_builder ()->push_context (0, lowpc);
13904 if (die->child != NULL)
13905 {
13906 child_die = die->child;
13907 while (child_die && child_die->tag)
13908 {
13909 process_die (child_die, cu);
13910 child_die = sibling_die (child_die);
13911 }
13912 }
13913 inherit_abstract_dies (die, cu);
13914 struct context_stack cstk = cu->get_builder ()->pop_context ();
13915
13916 if (*cu->get_builder ()->get_local_symbols () != NULL
13917 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13918 {
13919 struct block *block
13920 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13921 cstk.start_addr, highpc);
13922
13923 /* Note that recording ranges after traversing children, as we
13924 do here, means that recording a parent's ranges entails
13925 walking across all its children's ranges as they appear in
13926 the address map, which is quadratic behavior.
13927
13928 It would be nicer to record the parent's ranges before
13929 traversing its children, simply overriding whatever you find
13930 there. But since we don't even decide whether to create a
13931 block until after we've traversed its children, that's hard
13932 to do. */
13933 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13934 }
13935 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13936 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13937 }
13938
13939 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13940
13941 static void
13942 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13943 {
13944 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13945 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13946 CORE_ADDR pc, baseaddr;
13947 struct attribute *attr;
13948 struct call_site *call_site, call_site_local;
13949 void **slot;
13950 int nparams;
13951 struct die_info *child_die;
13952
13953 baseaddr = objfile->text_section_offset ();
13954
13955 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13956 if (attr == NULL)
13957 {
13958 /* This was a pre-DWARF-5 GNU extension alias
13959 for DW_AT_call_return_pc. */
13960 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13961 }
13962 if (!attr)
13963 {
13964 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13965 "DIE %s [in module %s]"),
13966 sect_offset_str (die->sect_off), objfile_name (objfile));
13967 return;
13968 }
13969 pc = attr_value_as_address (attr) + baseaddr;
13970 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13971
13972 if (cu->call_site_htab == NULL)
13973 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13974 NULL, &objfile->objfile_obstack,
13975 hashtab_obstack_allocate, NULL);
13976 call_site_local.pc = pc;
13977 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13978 if (*slot != NULL)
13979 {
13980 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13981 "DIE %s [in module %s]"),
13982 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13983 objfile_name (objfile));
13984 return;
13985 }
13986
13987 /* Count parameters at the caller. */
13988
13989 nparams = 0;
13990 for (child_die = die->child; child_die && child_die->tag;
13991 child_die = sibling_die (child_die))
13992 {
13993 if (child_die->tag != DW_TAG_call_site_parameter
13994 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13995 {
13996 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13997 "DW_TAG_call_site child DIE %s [in module %s]"),
13998 child_die->tag, sect_offset_str (child_die->sect_off),
13999 objfile_name (objfile));
14000 continue;
14001 }
14002
14003 nparams++;
14004 }
14005
14006 call_site
14007 = ((struct call_site *)
14008 obstack_alloc (&objfile->objfile_obstack,
14009 sizeof (*call_site)
14010 + (sizeof (*call_site->parameter) * (nparams - 1))));
14011 *slot = call_site;
14012 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14013 call_site->pc = pc;
14014
14015 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14016 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14017 {
14018 struct die_info *func_die;
14019
14020 /* Skip also over DW_TAG_inlined_subroutine. */
14021 for (func_die = die->parent;
14022 func_die && func_die->tag != DW_TAG_subprogram
14023 && func_die->tag != DW_TAG_subroutine_type;
14024 func_die = func_die->parent);
14025
14026 /* DW_AT_call_all_calls is a superset
14027 of DW_AT_call_all_tail_calls. */
14028 if (func_die
14029 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14030 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14031 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14032 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14033 {
14034 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14035 not complete. But keep CALL_SITE for look ups via call_site_htab,
14036 both the initial caller containing the real return address PC and
14037 the final callee containing the current PC of a chain of tail
14038 calls do not need to have the tail call list complete. But any
14039 function candidate for a virtual tail call frame searched via
14040 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14041 determined unambiguously. */
14042 }
14043 else
14044 {
14045 struct type *func_type = NULL;
14046
14047 if (func_die)
14048 func_type = get_die_type (func_die, cu);
14049 if (func_type != NULL)
14050 {
14051 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14052
14053 /* Enlist this call site to the function. */
14054 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14055 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14056 }
14057 else
14058 complaint (_("Cannot find function owning DW_TAG_call_site "
14059 "DIE %s [in module %s]"),
14060 sect_offset_str (die->sect_off), objfile_name (objfile));
14061 }
14062 }
14063
14064 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14065 if (attr == NULL)
14066 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14067 if (attr == NULL)
14068 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14069 if (attr == NULL)
14070 {
14071 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14072 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14073 }
14074 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14075 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14076 /* Keep NULL DWARF_BLOCK. */;
14077 else if (attr_form_is_block (attr))
14078 {
14079 struct dwarf2_locexpr_baton *dlbaton;
14080
14081 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14082 dlbaton->data = DW_BLOCK (attr)->data;
14083 dlbaton->size = DW_BLOCK (attr)->size;
14084 dlbaton->per_cu = cu->per_cu;
14085
14086 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14087 }
14088 else if (attr_form_is_ref (attr))
14089 {
14090 struct dwarf2_cu *target_cu = cu;
14091 struct die_info *target_die;
14092
14093 target_die = follow_die_ref (die, attr, &target_cu);
14094 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14095 if (die_is_declaration (target_die, target_cu))
14096 {
14097 const char *target_physname;
14098
14099 /* Prefer the mangled name; otherwise compute the demangled one. */
14100 target_physname = dw2_linkage_name (target_die, target_cu);
14101 if (target_physname == NULL)
14102 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14103 if (target_physname == NULL)
14104 complaint (_("DW_AT_call_target target DIE has invalid "
14105 "physname, for referencing DIE %s [in module %s]"),
14106 sect_offset_str (die->sect_off), objfile_name (objfile));
14107 else
14108 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14109 }
14110 else
14111 {
14112 CORE_ADDR lowpc;
14113
14114 /* DW_AT_entry_pc should be preferred. */
14115 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14116 <= PC_BOUNDS_INVALID)
14117 complaint (_("DW_AT_call_target target DIE has invalid "
14118 "low pc, for referencing DIE %s [in module %s]"),
14119 sect_offset_str (die->sect_off), objfile_name (objfile));
14120 else
14121 {
14122 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14123 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14124 }
14125 }
14126 }
14127 else
14128 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14129 "block nor reference, for DIE %s [in module %s]"),
14130 sect_offset_str (die->sect_off), objfile_name (objfile));
14131
14132 call_site->per_cu = cu->per_cu;
14133
14134 for (child_die = die->child;
14135 child_die && child_die->tag;
14136 child_die = sibling_die (child_die))
14137 {
14138 struct call_site_parameter *parameter;
14139 struct attribute *loc, *origin;
14140
14141 if (child_die->tag != DW_TAG_call_site_parameter
14142 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14143 {
14144 /* Already printed the complaint above. */
14145 continue;
14146 }
14147
14148 gdb_assert (call_site->parameter_count < nparams);
14149 parameter = &call_site->parameter[call_site->parameter_count];
14150
14151 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14152 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14153 register is contained in DW_AT_call_value. */
14154
14155 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14156 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14157 if (origin == NULL)
14158 {
14159 /* This was a pre-DWARF-5 GNU extension alias
14160 for DW_AT_call_parameter. */
14161 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14162 }
14163 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14164 {
14165 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14166
14167 sect_offset sect_off
14168 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14169 if (!offset_in_cu_p (&cu->header, sect_off))
14170 {
14171 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14172 binding can be done only inside one CU. Such referenced DIE
14173 therefore cannot be even moved to DW_TAG_partial_unit. */
14174 complaint (_("DW_AT_call_parameter offset is not in CU for "
14175 "DW_TAG_call_site child DIE %s [in module %s]"),
14176 sect_offset_str (child_die->sect_off),
14177 objfile_name (objfile));
14178 continue;
14179 }
14180 parameter->u.param_cu_off
14181 = (cu_offset) (sect_off - cu->header.sect_off);
14182 }
14183 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14184 {
14185 complaint (_("No DW_FORM_block* DW_AT_location for "
14186 "DW_TAG_call_site child DIE %s [in module %s]"),
14187 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14188 continue;
14189 }
14190 else
14191 {
14192 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14193 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14194 if (parameter->u.dwarf_reg != -1)
14195 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14196 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14197 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14198 &parameter->u.fb_offset))
14199 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14200 else
14201 {
14202 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14203 "for DW_FORM_block* DW_AT_location is supported for "
14204 "DW_TAG_call_site child DIE %s "
14205 "[in module %s]"),
14206 sect_offset_str (child_die->sect_off),
14207 objfile_name (objfile));
14208 continue;
14209 }
14210 }
14211
14212 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14213 if (attr == NULL)
14214 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14215 if (!attr_form_is_block (attr))
14216 {
14217 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14218 "DW_TAG_call_site child DIE %s [in module %s]"),
14219 sect_offset_str (child_die->sect_off),
14220 objfile_name (objfile));
14221 continue;
14222 }
14223 parameter->value = DW_BLOCK (attr)->data;
14224 parameter->value_size = DW_BLOCK (attr)->size;
14225
14226 /* Parameters are not pre-cleared by memset above. */
14227 parameter->data_value = NULL;
14228 parameter->data_value_size = 0;
14229 call_site->parameter_count++;
14230
14231 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14232 if (attr == NULL)
14233 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14234 if (attr != nullptr)
14235 {
14236 if (!attr_form_is_block (attr))
14237 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14238 "DW_TAG_call_site child DIE %s [in module %s]"),
14239 sect_offset_str (child_die->sect_off),
14240 objfile_name (objfile));
14241 else
14242 {
14243 parameter->data_value = DW_BLOCK (attr)->data;
14244 parameter->data_value_size = DW_BLOCK (attr)->size;
14245 }
14246 }
14247 }
14248 }
14249
14250 /* Helper function for read_variable. If DIE represents a virtual
14251 table, then return the type of the concrete object that is
14252 associated with the virtual table. Otherwise, return NULL. */
14253
14254 static struct type *
14255 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14256 {
14257 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14258 if (attr == NULL)
14259 return NULL;
14260
14261 /* Find the type DIE. */
14262 struct die_info *type_die = NULL;
14263 struct dwarf2_cu *type_cu = cu;
14264
14265 if (attr_form_is_ref (attr))
14266 type_die = follow_die_ref (die, attr, &type_cu);
14267 if (type_die == NULL)
14268 return NULL;
14269
14270 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14271 return NULL;
14272 return die_containing_type (type_die, type_cu);
14273 }
14274
14275 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14276
14277 static void
14278 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14279 {
14280 struct rust_vtable_symbol *storage = NULL;
14281
14282 if (cu->language == language_rust)
14283 {
14284 struct type *containing_type = rust_containing_type (die, cu);
14285
14286 if (containing_type != NULL)
14287 {
14288 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14289
14290 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
14291 initialize_objfile_symbol (storage);
14292 storage->concrete_type = containing_type;
14293 storage->subclass = SYMBOL_RUST_VTABLE;
14294 }
14295 }
14296
14297 struct symbol *res = new_symbol (die, NULL, cu, storage);
14298 struct attribute *abstract_origin
14299 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14300 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14301 if (res == NULL && loc && abstract_origin)
14302 {
14303 /* We have a variable without a name, but with a location and an abstract
14304 origin. This may be a concrete instance of an abstract variable
14305 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14306 later. */
14307 struct dwarf2_cu *origin_cu = cu;
14308 struct die_info *origin_die
14309 = follow_die_ref (die, abstract_origin, &origin_cu);
14310 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14311 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14312 }
14313 }
14314
14315 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14316 reading .debug_rnglists.
14317 Callback's type should be:
14318 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14319 Return true if the attributes are present and valid, otherwise,
14320 return false. */
14321
14322 template <typename Callback>
14323 static bool
14324 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14325 Callback &&callback)
14326 {
14327 struct dwarf2_per_objfile *dwarf2_per_objfile
14328 = cu->per_cu->dwarf2_per_objfile;
14329 struct objfile *objfile = dwarf2_per_objfile->objfile;
14330 bfd *obfd = objfile->obfd;
14331 /* Base address selection entry. */
14332 CORE_ADDR base;
14333 int found_base;
14334 const gdb_byte *buffer;
14335 CORE_ADDR baseaddr;
14336 bool overflow = false;
14337
14338 found_base = cu->base_known;
14339 base = cu->base_address;
14340
14341 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14342 if (offset >= dwarf2_per_objfile->rnglists.size)
14343 {
14344 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14345 offset);
14346 return false;
14347 }
14348 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14349
14350 baseaddr = objfile->text_section_offset ();
14351
14352 while (1)
14353 {
14354 /* Initialize it due to a false compiler warning. */
14355 CORE_ADDR range_beginning = 0, range_end = 0;
14356 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14357 + dwarf2_per_objfile->rnglists.size);
14358 unsigned int bytes_read;
14359
14360 if (buffer == buf_end)
14361 {
14362 overflow = true;
14363 break;
14364 }
14365 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14366 switch (rlet)
14367 {
14368 case DW_RLE_end_of_list:
14369 break;
14370 case DW_RLE_base_address:
14371 if (buffer + cu->header.addr_size > buf_end)
14372 {
14373 overflow = true;
14374 break;
14375 }
14376 base = read_address (obfd, buffer, cu, &bytes_read);
14377 found_base = 1;
14378 buffer += bytes_read;
14379 break;
14380 case DW_RLE_start_length:
14381 if (buffer + cu->header.addr_size > buf_end)
14382 {
14383 overflow = true;
14384 break;
14385 }
14386 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14387 buffer += bytes_read;
14388 range_end = (range_beginning
14389 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14390 buffer += bytes_read;
14391 if (buffer > buf_end)
14392 {
14393 overflow = true;
14394 break;
14395 }
14396 break;
14397 case DW_RLE_offset_pair:
14398 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14399 buffer += bytes_read;
14400 if (buffer > buf_end)
14401 {
14402 overflow = true;
14403 break;
14404 }
14405 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14406 buffer += bytes_read;
14407 if (buffer > buf_end)
14408 {
14409 overflow = true;
14410 break;
14411 }
14412 break;
14413 case DW_RLE_start_end:
14414 if (buffer + 2 * cu->header.addr_size > buf_end)
14415 {
14416 overflow = true;
14417 break;
14418 }
14419 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14420 buffer += bytes_read;
14421 range_end = read_address (obfd, buffer, cu, &bytes_read);
14422 buffer += bytes_read;
14423 break;
14424 default:
14425 complaint (_("Invalid .debug_rnglists data (no base address)"));
14426 return false;
14427 }
14428 if (rlet == DW_RLE_end_of_list || overflow)
14429 break;
14430 if (rlet == DW_RLE_base_address)
14431 continue;
14432
14433 if (!found_base)
14434 {
14435 /* We have no valid base address for the ranges
14436 data. */
14437 complaint (_("Invalid .debug_rnglists data (no base address)"));
14438 return false;
14439 }
14440
14441 if (range_beginning > range_end)
14442 {
14443 /* Inverted range entries are invalid. */
14444 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14445 return false;
14446 }
14447
14448 /* Empty range entries have no effect. */
14449 if (range_beginning == range_end)
14450 continue;
14451
14452 range_beginning += base;
14453 range_end += base;
14454
14455 /* A not-uncommon case of bad debug info.
14456 Don't pollute the addrmap with bad data. */
14457 if (range_beginning + baseaddr == 0
14458 && !dwarf2_per_objfile->has_section_at_zero)
14459 {
14460 complaint (_(".debug_rnglists entry has start address of zero"
14461 " [in module %s]"), objfile_name (objfile));
14462 continue;
14463 }
14464
14465 callback (range_beginning, range_end);
14466 }
14467
14468 if (overflow)
14469 {
14470 complaint (_("Offset %d is not terminated "
14471 "for DW_AT_ranges attribute"),
14472 offset);
14473 return false;
14474 }
14475
14476 return true;
14477 }
14478
14479 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14480 Callback's type should be:
14481 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14482 Return 1 if the attributes are present and valid, otherwise, return 0. */
14483
14484 template <typename Callback>
14485 static int
14486 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14487 Callback &&callback)
14488 {
14489 struct dwarf2_per_objfile *dwarf2_per_objfile
14490 = cu->per_cu->dwarf2_per_objfile;
14491 struct objfile *objfile = dwarf2_per_objfile->objfile;
14492 struct comp_unit_head *cu_header = &cu->header;
14493 bfd *obfd = objfile->obfd;
14494 unsigned int addr_size = cu_header->addr_size;
14495 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14496 /* Base address selection entry. */
14497 CORE_ADDR base;
14498 int found_base;
14499 unsigned int dummy;
14500 const gdb_byte *buffer;
14501 CORE_ADDR baseaddr;
14502
14503 if (cu_header->version >= 5)
14504 return dwarf2_rnglists_process (offset, cu, callback);
14505
14506 found_base = cu->base_known;
14507 base = cu->base_address;
14508
14509 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14510 if (offset >= dwarf2_per_objfile->ranges.size)
14511 {
14512 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14513 offset);
14514 return 0;
14515 }
14516 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14517
14518 baseaddr = objfile->text_section_offset ();
14519
14520 while (1)
14521 {
14522 CORE_ADDR range_beginning, range_end;
14523
14524 range_beginning = read_address (obfd, buffer, cu, &dummy);
14525 buffer += addr_size;
14526 range_end = read_address (obfd, buffer, cu, &dummy);
14527 buffer += addr_size;
14528 offset += 2 * addr_size;
14529
14530 /* An end of list marker is a pair of zero addresses. */
14531 if (range_beginning == 0 && range_end == 0)
14532 /* Found the end of list entry. */
14533 break;
14534
14535 /* Each base address selection entry is a pair of 2 values.
14536 The first is the largest possible address, the second is
14537 the base address. Check for a base address here. */
14538 if ((range_beginning & mask) == mask)
14539 {
14540 /* If we found the largest possible address, then we already
14541 have the base address in range_end. */
14542 base = range_end;
14543 found_base = 1;
14544 continue;
14545 }
14546
14547 if (!found_base)
14548 {
14549 /* We have no valid base address for the ranges
14550 data. */
14551 complaint (_("Invalid .debug_ranges data (no base address)"));
14552 return 0;
14553 }
14554
14555 if (range_beginning > range_end)
14556 {
14557 /* Inverted range entries are invalid. */
14558 complaint (_("Invalid .debug_ranges data (inverted range)"));
14559 return 0;
14560 }
14561
14562 /* Empty range entries have no effect. */
14563 if (range_beginning == range_end)
14564 continue;
14565
14566 range_beginning += base;
14567 range_end += base;
14568
14569 /* A not-uncommon case of bad debug info.
14570 Don't pollute the addrmap with bad data. */
14571 if (range_beginning + baseaddr == 0
14572 && !dwarf2_per_objfile->has_section_at_zero)
14573 {
14574 complaint (_(".debug_ranges entry has start address of zero"
14575 " [in module %s]"), objfile_name (objfile));
14576 continue;
14577 }
14578
14579 callback (range_beginning, range_end);
14580 }
14581
14582 return 1;
14583 }
14584
14585 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14586 Return 1 if the attributes are present and valid, otherwise, return 0.
14587 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14588
14589 static int
14590 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14591 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14592 dwarf2_psymtab *ranges_pst)
14593 {
14594 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14595 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14596 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14597 int low_set = 0;
14598 CORE_ADDR low = 0;
14599 CORE_ADDR high = 0;
14600 int retval;
14601
14602 retval = dwarf2_ranges_process (offset, cu,
14603 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14604 {
14605 if (ranges_pst != NULL)
14606 {
14607 CORE_ADDR lowpc;
14608 CORE_ADDR highpc;
14609
14610 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14611 range_beginning + baseaddr)
14612 - baseaddr);
14613 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14614 range_end + baseaddr)
14615 - baseaddr);
14616 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14617 lowpc, highpc - 1, ranges_pst);
14618 }
14619
14620 /* FIXME: This is recording everything as a low-high
14621 segment of consecutive addresses. We should have a
14622 data structure for discontiguous block ranges
14623 instead. */
14624 if (! low_set)
14625 {
14626 low = range_beginning;
14627 high = range_end;
14628 low_set = 1;
14629 }
14630 else
14631 {
14632 if (range_beginning < low)
14633 low = range_beginning;
14634 if (range_end > high)
14635 high = range_end;
14636 }
14637 });
14638 if (!retval)
14639 return 0;
14640
14641 if (! low_set)
14642 /* If the first entry is an end-of-list marker, the range
14643 describes an empty scope, i.e. no instructions. */
14644 return 0;
14645
14646 if (low_return)
14647 *low_return = low;
14648 if (high_return)
14649 *high_return = high;
14650 return 1;
14651 }
14652
14653 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14654 definition for the return value. *LOWPC and *HIGHPC are set iff
14655 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14656
14657 static enum pc_bounds_kind
14658 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14659 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14660 dwarf2_psymtab *pst)
14661 {
14662 struct dwarf2_per_objfile *dwarf2_per_objfile
14663 = cu->per_cu->dwarf2_per_objfile;
14664 struct attribute *attr;
14665 struct attribute *attr_high;
14666 CORE_ADDR low = 0;
14667 CORE_ADDR high = 0;
14668 enum pc_bounds_kind ret;
14669
14670 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14671 if (attr_high)
14672 {
14673 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14674 if (attr != nullptr)
14675 {
14676 low = attr_value_as_address (attr);
14677 high = attr_value_as_address (attr_high);
14678 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14679 high += low;
14680 }
14681 else
14682 /* Found high w/o low attribute. */
14683 return PC_BOUNDS_INVALID;
14684
14685 /* Found consecutive range of addresses. */
14686 ret = PC_BOUNDS_HIGH_LOW;
14687 }
14688 else
14689 {
14690 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14691 if (attr != NULL)
14692 {
14693 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14694 We take advantage of the fact that DW_AT_ranges does not appear
14695 in DW_TAG_compile_unit of DWO files. */
14696 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14697 unsigned int ranges_offset = (DW_UNSND (attr)
14698 + (need_ranges_base
14699 ? cu->ranges_base
14700 : 0));
14701
14702 /* Value of the DW_AT_ranges attribute is the offset in the
14703 .debug_ranges section. */
14704 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14705 return PC_BOUNDS_INVALID;
14706 /* Found discontinuous range of addresses. */
14707 ret = PC_BOUNDS_RANGES;
14708 }
14709 else
14710 return PC_BOUNDS_NOT_PRESENT;
14711 }
14712
14713 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14714 if (high <= low)
14715 return PC_BOUNDS_INVALID;
14716
14717 /* When using the GNU linker, .gnu.linkonce. sections are used to
14718 eliminate duplicate copies of functions and vtables and such.
14719 The linker will arbitrarily choose one and discard the others.
14720 The AT_*_pc values for such functions refer to local labels in
14721 these sections. If the section from that file was discarded, the
14722 labels are not in the output, so the relocs get a value of 0.
14723 If this is a discarded function, mark the pc bounds as invalid,
14724 so that GDB will ignore it. */
14725 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14726 return PC_BOUNDS_INVALID;
14727
14728 *lowpc = low;
14729 if (highpc)
14730 *highpc = high;
14731 return ret;
14732 }
14733
14734 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14735 its low and high PC addresses. Do nothing if these addresses could not
14736 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14737 and HIGHPC to the high address if greater than HIGHPC. */
14738
14739 static void
14740 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14741 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14742 struct dwarf2_cu *cu)
14743 {
14744 CORE_ADDR low, high;
14745 struct die_info *child = die->child;
14746
14747 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14748 {
14749 *lowpc = std::min (*lowpc, low);
14750 *highpc = std::max (*highpc, high);
14751 }
14752
14753 /* If the language does not allow nested subprograms (either inside
14754 subprograms or lexical blocks), we're done. */
14755 if (cu->language != language_ada)
14756 return;
14757
14758 /* Check all the children of the given DIE. If it contains nested
14759 subprograms, then check their pc bounds. Likewise, we need to
14760 check lexical blocks as well, as they may also contain subprogram
14761 definitions. */
14762 while (child && child->tag)
14763 {
14764 if (child->tag == DW_TAG_subprogram
14765 || child->tag == DW_TAG_lexical_block)
14766 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14767 child = sibling_die (child);
14768 }
14769 }
14770
14771 /* Get the low and high pc's represented by the scope DIE, and store
14772 them in *LOWPC and *HIGHPC. If the correct values can't be
14773 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14774
14775 static void
14776 get_scope_pc_bounds (struct die_info *die,
14777 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14778 struct dwarf2_cu *cu)
14779 {
14780 CORE_ADDR best_low = (CORE_ADDR) -1;
14781 CORE_ADDR best_high = (CORE_ADDR) 0;
14782 CORE_ADDR current_low, current_high;
14783
14784 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14785 >= PC_BOUNDS_RANGES)
14786 {
14787 best_low = current_low;
14788 best_high = current_high;
14789 }
14790 else
14791 {
14792 struct die_info *child = die->child;
14793
14794 while (child && child->tag)
14795 {
14796 switch (child->tag) {
14797 case DW_TAG_subprogram:
14798 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14799 break;
14800 case DW_TAG_namespace:
14801 case DW_TAG_module:
14802 /* FIXME: carlton/2004-01-16: Should we do this for
14803 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14804 that current GCC's always emit the DIEs corresponding
14805 to definitions of methods of classes as children of a
14806 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14807 the DIEs giving the declarations, which could be
14808 anywhere). But I don't see any reason why the
14809 standards says that they have to be there. */
14810 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14811
14812 if (current_low != ((CORE_ADDR) -1))
14813 {
14814 best_low = std::min (best_low, current_low);
14815 best_high = std::max (best_high, current_high);
14816 }
14817 break;
14818 default:
14819 /* Ignore. */
14820 break;
14821 }
14822
14823 child = sibling_die (child);
14824 }
14825 }
14826
14827 *lowpc = best_low;
14828 *highpc = best_high;
14829 }
14830
14831 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14832 in DIE. */
14833
14834 static void
14835 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14836 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14837 {
14838 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14839 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14840 struct attribute *attr;
14841 struct attribute *attr_high;
14842
14843 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14844 if (attr_high)
14845 {
14846 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14847 if (attr != nullptr)
14848 {
14849 CORE_ADDR low = attr_value_as_address (attr);
14850 CORE_ADDR high = attr_value_as_address (attr_high);
14851
14852 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14853 high += low;
14854
14855 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14856 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14857 cu->get_builder ()->record_block_range (block, low, high - 1);
14858 }
14859 }
14860
14861 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14862 if (attr != nullptr)
14863 {
14864 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14865 We take advantage of the fact that DW_AT_ranges does not appear
14866 in DW_TAG_compile_unit of DWO files. */
14867 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14868
14869 /* The value of the DW_AT_ranges attribute is the offset of the
14870 address range list in the .debug_ranges section. */
14871 unsigned long offset = (DW_UNSND (attr)
14872 + (need_ranges_base ? cu->ranges_base : 0));
14873
14874 std::vector<blockrange> blockvec;
14875 dwarf2_ranges_process (offset, cu,
14876 [&] (CORE_ADDR start, CORE_ADDR end)
14877 {
14878 start += baseaddr;
14879 end += baseaddr;
14880 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14881 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14882 cu->get_builder ()->record_block_range (block, start, end - 1);
14883 blockvec.emplace_back (start, end);
14884 });
14885
14886 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14887 }
14888 }
14889
14890 /* Check whether the producer field indicates either of GCC < 4.6, or the
14891 Intel C/C++ compiler, and cache the result in CU. */
14892
14893 static void
14894 check_producer (struct dwarf2_cu *cu)
14895 {
14896 int major, minor;
14897
14898 if (cu->producer == NULL)
14899 {
14900 /* For unknown compilers expect their behavior is DWARF version
14901 compliant.
14902
14903 GCC started to support .debug_types sections by -gdwarf-4 since
14904 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14905 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14906 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14907 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14908 }
14909 else if (producer_is_gcc (cu->producer, &major, &minor))
14910 {
14911 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14912 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14913 }
14914 else if (producer_is_icc (cu->producer, &major, &minor))
14915 {
14916 cu->producer_is_icc = true;
14917 cu->producer_is_icc_lt_14 = major < 14;
14918 }
14919 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14920 cu->producer_is_codewarrior = true;
14921 else
14922 {
14923 /* For other non-GCC compilers, expect their behavior is DWARF version
14924 compliant. */
14925 }
14926
14927 cu->checked_producer = true;
14928 }
14929
14930 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14931 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14932 during 4.6.0 experimental. */
14933
14934 static bool
14935 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14936 {
14937 if (!cu->checked_producer)
14938 check_producer (cu);
14939
14940 return cu->producer_is_gxx_lt_4_6;
14941 }
14942
14943
14944 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14945 with incorrect is_stmt attributes. */
14946
14947 static bool
14948 producer_is_codewarrior (struct dwarf2_cu *cu)
14949 {
14950 if (!cu->checked_producer)
14951 check_producer (cu);
14952
14953 return cu->producer_is_codewarrior;
14954 }
14955
14956 /* Return the default accessibility type if it is not overridden by
14957 DW_AT_accessibility. */
14958
14959 static enum dwarf_access_attribute
14960 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14961 {
14962 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14963 {
14964 /* The default DWARF 2 accessibility for members is public, the default
14965 accessibility for inheritance is private. */
14966
14967 if (die->tag != DW_TAG_inheritance)
14968 return DW_ACCESS_public;
14969 else
14970 return DW_ACCESS_private;
14971 }
14972 else
14973 {
14974 /* DWARF 3+ defines the default accessibility a different way. The same
14975 rules apply now for DW_TAG_inheritance as for the members and it only
14976 depends on the container kind. */
14977
14978 if (die->parent->tag == DW_TAG_class_type)
14979 return DW_ACCESS_private;
14980 else
14981 return DW_ACCESS_public;
14982 }
14983 }
14984
14985 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14986 offset. If the attribute was not found return 0, otherwise return
14987 1. If it was found but could not properly be handled, set *OFFSET
14988 to 0. */
14989
14990 static int
14991 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14992 LONGEST *offset)
14993 {
14994 struct attribute *attr;
14995
14996 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14997 if (attr != NULL)
14998 {
14999 *offset = 0;
15000
15001 /* Note that we do not check for a section offset first here.
15002 This is because DW_AT_data_member_location is new in DWARF 4,
15003 so if we see it, we can assume that a constant form is really
15004 a constant and not a section offset. */
15005 if (attr_form_is_constant (attr))
15006 *offset = dwarf2_get_attr_constant_value (attr, 0);
15007 else if (attr_form_is_section_offset (attr))
15008 dwarf2_complex_location_expr_complaint ();
15009 else if (attr_form_is_block (attr))
15010 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15011 else
15012 dwarf2_complex_location_expr_complaint ();
15013
15014 return 1;
15015 }
15016
15017 return 0;
15018 }
15019
15020 /* Add an aggregate field to the field list. */
15021
15022 static void
15023 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15024 struct dwarf2_cu *cu)
15025 {
15026 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15027 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15028 struct nextfield *new_field;
15029 struct attribute *attr;
15030 struct field *fp;
15031 const char *fieldname = "";
15032
15033 if (die->tag == DW_TAG_inheritance)
15034 {
15035 fip->baseclasses.emplace_back ();
15036 new_field = &fip->baseclasses.back ();
15037 }
15038 else
15039 {
15040 fip->fields.emplace_back ();
15041 new_field = &fip->fields.back ();
15042 }
15043
15044 fip->nfields++;
15045
15046 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15047 if (attr != nullptr)
15048 new_field->accessibility = DW_UNSND (attr);
15049 else
15050 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15051 if (new_field->accessibility != DW_ACCESS_public)
15052 fip->non_public_fields = 1;
15053
15054 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15055 if (attr != nullptr)
15056 new_field->virtuality = DW_UNSND (attr);
15057 else
15058 new_field->virtuality = DW_VIRTUALITY_none;
15059
15060 fp = &new_field->field;
15061
15062 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15063 {
15064 LONGEST offset;
15065
15066 /* Data member other than a C++ static data member. */
15067
15068 /* Get type of field. */
15069 fp->type = die_type (die, cu);
15070
15071 SET_FIELD_BITPOS (*fp, 0);
15072
15073 /* Get bit size of field (zero if none). */
15074 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15075 if (attr != nullptr)
15076 {
15077 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15078 }
15079 else
15080 {
15081 FIELD_BITSIZE (*fp) = 0;
15082 }
15083
15084 /* Get bit offset of field. */
15085 if (handle_data_member_location (die, cu, &offset))
15086 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15087 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15088 if (attr != nullptr)
15089 {
15090 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15091 {
15092 /* For big endian bits, the DW_AT_bit_offset gives the
15093 additional bit offset from the MSB of the containing
15094 anonymous object to the MSB of the field. We don't
15095 have to do anything special since we don't need to
15096 know the size of the anonymous object. */
15097 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15098 }
15099 else
15100 {
15101 /* For little endian bits, compute the bit offset to the
15102 MSB of the anonymous object, subtract off the number of
15103 bits from the MSB of the field to the MSB of the
15104 object, and then subtract off the number of bits of
15105 the field itself. The result is the bit offset of
15106 the LSB of the field. */
15107 int anonymous_size;
15108 int bit_offset = DW_UNSND (attr);
15109
15110 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15111 if (attr != nullptr)
15112 {
15113 /* The size of the anonymous object containing
15114 the bit field is explicit, so use the
15115 indicated size (in bytes). */
15116 anonymous_size = DW_UNSND (attr);
15117 }
15118 else
15119 {
15120 /* The size of the anonymous object containing
15121 the bit field must be inferred from the type
15122 attribute of the data member containing the
15123 bit field. */
15124 anonymous_size = TYPE_LENGTH (fp->type);
15125 }
15126 SET_FIELD_BITPOS (*fp,
15127 (FIELD_BITPOS (*fp)
15128 + anonymous_size * bits_per_byte
15129 - bit_offset - FIELD_BITSIZE (*fp)));
15130 }
15131 }
15132 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15133 if (attr != NULL)
15134 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15135 + dwarf2_get_attr_constant_value (attr, 0)));
15136
15137 /* Get name of field. */
15138 fieldname = dwarf2_name (die, cu);
15139 if (fieldname == NULL)
15140 fieldname = "";
15141
15142 /* The name is already allocated along with this objfile, so we don't
15143 need to duplicate it for the type. */
15144 fp->name = fieldname;
15145
15146 /* Change accessibility for artificial fields (e.g. virtual table
15147 pointer or virtual base class pointer) to private. */
15148 if (dwarf2_attr (die, DW_AT_artificial, cu))
15149 {
15150 FIELD_ARTIFICIAL (*fp) = 1;
15151 new_field->accessibility = DW_ACCESS_private;
15152 fip->non_public_fields = 1;
15153 }
15154 }
15155 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15156 {
15157 /* C++ static member. */
15158
15159 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15160 is a declaration, but all versions of G++ as of this writing
15161 (so through at least 3.2.1) incorrectly generate
15162 DW_TAG_variable tags. */
15163
15164 const char *physname;
15165
15166 /* Get name of field. */
15167 fieldname = dwarf2_name (die, cu);
15168 if (fieldname == NULL)
15169 return;
15170
15171 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15172 if (attr
15173 /* Only create a symbol if this is an external value.
15174 new_symbol checks this and puts the value in the global symbol
15175 table, which we want. If it is not external, new_symbol
15176 will try to put the value in cu->list_in_scope which is wrong. */
15177 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15178 {
15179 /* A static const member, not much different than an enum as far as
15180 we're concerned, except that we can support more types. */
15181 new_symbol (die, NULL, cu);
15182 }
15183
15184 /* Get physical name. */
15185 physname = dwarf2_physname (fieldname, die, cu);
15186
15187 /* The name is already allocated along with this objfile, so we don't
15188 need to duplicate it for the type. */
15189 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15190 FIELD_TYPE (*fp) = die_type (die, cu);
15191 FIELD_NAME (*fp) = fieldname;
15192 }
15193 else if (die->tag == DW_TAG_inheritance)
15194 {
15195 LONGEST offset;
15196
15197 /* C++ base class field. */
15198 if (handle_data_member_location (die, cu, &offset))
15199 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15200 FIELD_BITSIZE (*fp) = 0;
15201 FIELD_TYPE (*fp) = die_type (die, cu);
15202 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15203 }
15204 else if (die->tag == DW_TAG_variant_part)
15205 {
15206 /* process_structure_scope will treat this DIE as a union. */
15207 process_structure_scope (die, cu);
15208
15209 /* The variant part is relative to the start of the enclosing
15210 structure. */
15211 SET_FIELD_BITPOS (*fp, 0);
15212 fp->type = get_die_type (die, cu);
15213 fp->artificial = 1;
15214 fp->name = "<<variant>>";
15215
15216 /* Normally a DW_TAG_variant_part won't have a size, but our
15217 representation requires one, so set it to the maximum of the
15218 child sizes, being sure to account for the offset at which
15219 each child is seen. */
15220 if (TYPE_LENGTH (fp->type) == 0)
15221 {
15222 unsigned max = 0;
15223 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15224 {
15225 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15226 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15227 if (len > max)
15228 max = len;
15229 }
15230 TYPE_LENGTH (fp->type) = max;
15231 }
15232 }
15233 else
15234 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15235 }
15236
15237 /* Can the type given by DIE define another type? */
15238
15239 static bool
15240 type_can_define_types (const struct die_info *die)
15241 {
15242 switch (die->tag)
15243 {
15244 case DW_TAG_typedef:
15245 case DW_TAG_class_type:
15246 case DW_TAG_structure_type:
15247 case DW_TAG_union_type:
15248 case DW_TAG_enumeration_type:
15249 return true;
15250
15251 default:
15252 return false;
15253 }
15254 }
15255
15256 /* Add a type definition defined in the scope of the FIP's class. */
15257
15258 static void
15259 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15260 struct dwarf2_cu *cu)
15261 {
15262 struct decl_field fp;
15263 memset (&fp, 0, sizeof (fp));
15264
15265 gdb_assert (type_can_define_types (die));
15266
15267 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15268 fp.name = dwarf2_name (die, cu);
15269 fp.type = read_type_die (die, cu);
15270
15271 /* Save accessibility. */
15272 enum dwarf_access_attribute accessibility;
15273 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15274 if (attr != NULL)
15275 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15276 else
15277 accessibility = dwarf2_default_access_attribute (die, cu);
15278 switch (accessibility)
15279 {
15280 case DW_ACCESS_public:
15281 /* The assumed value if neither private nor protected. */
15282 break;
15283 case DW_ACCESS_private:
15284 fp.is_private = 1;
15285 break;
15286 case DW_ACCESS_protected:
15287 fp.is_protected = 1;
15288 break;
15289 default:
15290 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15291 }
15292
15293 if (die->tag == DW_TAG_typedef)
15294 fip->typedef_field_list.push_back (fp);
15295 else
15296 fip->nested_types_list.push_back (fp);
15297 }
15298
15299 /* Create the vector of fields, and attach it to the type. */
15300
15301 static void
15302 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15303 struct dwarf2_cu *cu)
15304 {
15305 int nfields = fip->nfields;
15306
15307 /* Record the field count, allocate space for the array of fields,
15308 and create blank accessibility bitfields if necessary. */
15309 TYPE_NFIELDS (type) = nfields;
15310 TYPE_FIELDS (type) = (struct field *)
15311 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15312
15313 if (fip->non_public_fields && cu->language != language_ada)
15314 {
15315 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15316
15317 TYPE_FIELD_PRIVATE_BITS (type) =
15318 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15319 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15320
15321 TYPE_FIELD_PROTECTED_BITS (type) =
15322 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15323 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15324
15325 TYPE_FIELD_IGNORE_BITS (type) =
15326 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15327 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15328 }
15329
15330 /* If the type has baseclasses, allocate and clear a bit vector for
15331 TYPE_FIELD_VIRTUAL_BITS. */
15332 if (!fip->baseclasses.empty () && cu->language != language_ada)
15333 {
15334 int num_bytes = B_BYTES (fip->baseclasses.size ());
15335 unsigned char *pointer;
15336
15337 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15338 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15339 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15340 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15341 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15342 }
15343
15344 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15345 {
15346 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15347
15348 for (int index = 0; index < nfields; ++index)
15349 {
15350 struct nextfield &field = fip->fields[index];
15351
15352 if (field.variant.is_discriminant)
15353 di->discriminant_index = index;
15354 else if (field.variant.default_branch)
15355 di->default_index = index;
15356 else
15357 di->discriminants[index] = field.variant.discriminant_value;
15358 }
15359 }
15360
15361 /* Copy the saved-up fields into the field vector. */
15362 for (int i = 0; i < nfields; ++i)
15363 {
15364 struct nextfield &field
15365 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15366 : fip->fields[i - fip->baseclasses.size ()]);
15367
15368 TYPE_FIELD (type, i) = field.field;
15369 switch (field.accessibility)
15370 {
15371 case DW_ACCESS_private:
15372 if (cu->language != language_ada)
15373 SET_TYPE_FIELD_PRIVATE (type, i);
15374 break;
15375
15376 case DW_ACCESS_protected:
15377 if (cu->language != language_ada)
15378 SET_TYPE_FIELD_PROTECTED (type, i);
15379 break;
15380
15381 case DW_ACCESS_public:
15382 break;
15383
15384 default:
15385 /* Unknown accessibility. Complain and treat it as public. */
15386 {
15387 complaint (_("unsupported accessibility %d"),
15388 field.accessibility);
15389 }
15390 break;
15391 }
15392 if (i < fip->baseclasses.size ())
15393 {
15394 switch (field.virtuality)
15395 {
15396 case DW_VIRTUALITY_virtual:
15397 case DW_VIRTUALITY_pure_virtual:
15398 if (cu->language == language_ada)
15399 error (_("unexpected virtuality in component of Ada type"));
15400 SET_TYPE_FIELD_VIRTUAL (type, i);
15401 break;
15402 }
15403 }
15404 }
15405 }
15406
15407 /* Return true if this member function is a constructor, false
15408 otherwise. */
15409
15410 static int
15411 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15412 {
15413 const char *fieldname;
15414 const char *type_name;
15415 int len;
15416
15417 if (die->parent == NULL)
15418 return 0;
15419
15420 if (die->parent->tag != DW_TAG_structure_type
15421 && die->parent->tag != DW_TAG_union_type
15422 && die->parent->tag != DW_TAG_class_type)
15423 return 0;
15424
15425 fieldname = dwarf2_name (die, cu);
15426 type_name = dwarf2_name (die->parent, cu);
15427 if (fieldname == NULL || type_name == NULL)
15428 return 0;
15429
15430 len = strlen (fieldname);
15431 return (strncmp (fieldname, type_name, len) == 0
15432 && (type_name[len] == '\0' || type_name[len] == '<'));
15433 }
15434
15435 /* Check if the given VALUE is a recognized enum
15436 dwarf_defaulted_attribute constant according to DWARF5 spec,
15437 Table 7.24. */
15438
15439 static bool
15440 is_valid_DW_AT_defaulted (ULONGEST value)
15441 {
15442 switch (value)
15443 {
15444 case DW_DEFAULTED_no:
15445 case DW_DEFAULTED_in_class:
15446 case DW_DEFAULTED_out_of_class:
15447 return true;
15448 }
15449
15450 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15451 return false;
15452 }
15453
15454 /* Add a member function to the proper fieldlist. */
15455
15456 static void
15457 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15458 struct type *type, struct dwarf2_cu *cu)
15459 {
15460 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15461 struct attribute *attr;
15462 int i;
15463 struct fnfieldlist *flp = nullptr;
15464 struct fn_field *fnp;
15465 const char *fieldname;
15466 struct type *this_type;
15467 enum dwarf_access_attribute accessibility;
15468
15469 if (cu->language == language_ada)
15470 error (_("unexpected member function in Ada type"));
15471
15472 /* Get name of member function. */
15473 fieldname = dwarf2_name (die, cu);
15474 if (fieldname == NULL)
15475 return;
15476
15477 /* Look up member function name in fieldlist. */
15478 for (i = 0; i < fip->fnfieldlists.size (); i++)
15479 {
15480 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15481 {
15482 flp = &fip->fnfieldlists[i];
15483 break;
15484 }
15485 }
15486
15487 /* Create a new fnfieldlist if necessary. */
15488 if (flp == nullptr)
15489 {
15490 fip->fnfieldlists.emplace_back ();
15491 flp = &fip->fnfieldlists.back ();
15492 flp->name = fieldname;
15493 i = fip->fnfieldlists.size () - 1;
15494 }
15495
15496 /* Create a new member function field and add it to the vector of
15497 fnfieldlists. */
15498 flp->fnfields.emplace_back ();
15499 fnp = &flp->fnfields.back ();
15500
15501 /* Delay processing of the physname until later. */
15502 if (cu->language == language_cplus)
15503 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15504 die, cu);
15505 else
15506 {
15507 const char *physname = dwarf2_physname (fieldname, die, cu);
15508 fnp->physname = physname ? physname : "";
15509 }
15510
15511 fnp->type = alloc_type (objfile);
15512 this_type = read_type_die (die, cu);
15513 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15514 {
15515 int nparams = TYPE_NFIELDS (this_type);
15516
15517 /* TYPE is the domain of this method, and THIS_TYPE is the type
15518 of the method itself (TYPE_CODE_METHOD). */
15519 smash_to_method_type (fnp->type, type,
15520 TYPE_TARGET_TYPE (this_type),
15521 TYPE_FIELDS (this_type),
15522 TYPE_NFIELDS (this_type),
15523 TYPE_VARARGS (this_type));
15524
15525 /* Handle static member functions.
15526 Dwarf2 has no clean way to discern C++ static and non-static
15527 member functions. G++ helps GDB by marking the first
15528 parameter for non-static member functions (which is the this
15529 pointer) as artificial. We obtain this information from
15530 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15531 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15532 fnp->voffset = VOFFSET_STATIC;
15533 }
15534 else
15535 complaint (_("member function type missing for '%s'"),
15536 dwarf2_full_name (fieldname, die, cu));
15537
15538 /* Get fcontext from DW_AT_containing_type if present. */
15539 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15540 fnp->fcontext = die_containing_type (die, cu);
15541
15542 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15543 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15544
15545 /* Get accessibility. */
15546 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15547 if (attr != nullptr)
15548 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15549 else
15550 accessibility = dwarf2_default_access_attribute (die, cu);
15551 switch (accessibility)
15552 {
15553 case DW_ACCESS_private:
15554 fnp->is_private = 1;
15555 break;
15556 case DW_ACCESS_protected:
15557 fnp->is_protected = 1;
15558 break;
15559 }
15560
15561 /* Check for artificial methods. */
15562 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15563 if (attr && DW_UNSND (attr) != 0)
15564 fnp->is_artificial = 1;
15565
15566 /* Check for defaulted methods. */
15567 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15568 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15569 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15570
15571 /* Check for deleted methods. */
15572 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15573 if (attr != nullptr && DW_UNSND (attr) != 0)
15574 fnp->is_deleted = 1;
15575
15576 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15577
15578 /* Get index in virtual function table if it is a virtual member
15579 function. For older versions of GCC, this is an offset in the
15580 appropriate virtual table, as specified by DW_AT_containing_type.
15581 For everyone else, it is an expression to be evaluated relative
15582 to the object address. */
15583
15584 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15585 if (attr != nullptr)
15586 {
15587 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15588 {
15589 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15590 {
15591 /* Old-style GCC. */
15592 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15593 }
15594 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15595 || (DW_BLOCK (attr)->size > 1
15596 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15597 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15598 {
15599 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15600 if ((fnp->voffset % cu->header.addr_size) != 0)
15601 dwarf2_complex_location_expr_complaint ();
15602 else
15603 fnp->voffset /= cu->header.addr_size;
15604 fnp->voffset += 2;
15605 }
15606 else
15607 dwarf2_complex_location_expr_complaint ();
15608
15609 if (!fnp->fcontext)
15610 {
15611 /* If there is no `this' field and no DW_AT_containing_type,
15612 we cannot actually find a base class context for the
15613 vtable! */
15614 if (TYPE_NFIELDS (this_type) == 0
15615 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15616 {
15617 complaint (_("cannot determine context for virtual member "
15618 "function \"%s\" (offset %s)"),
15619 fieldname, sect_offset_str (die->sect_off));
15620 }
15621 else
15622 {
15623 fnp->fcontext
15624 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15625 }
15626 }
15627 }
15628 else if (attr_form_is_section_offset (attr))
15629 {
15630 dwarf2_complex_location_expr_complaint ();
15631 }
15632 else
15633 {
15634 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15635 fieldname);
15636 }
15637 }
15638 else
15639 {
15640 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15641 if (attr && DW_UNSND (attr))
15642 {
15643 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15644 complaint (_("Member function \"%s\" (offset %s) is virtual "
15645 "but the vtable offset is not specified"),
15646 fieldname, sect_offset_str (die->sect_off));
15647 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15648 TYPE_CPLUS_DYNAMIC (type) = 1;
15649 }
15650 }
15651 }
15652
15653 /* Create the vector of member function fields, and attach it to the type. */
15654
15655 static void
15656 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15657 struct dwarf2_cu *cu)
15658 {
15659 if (cu->language == language_ada)
15660 error (_("unexpected member functions in Ada type"));
15661
15662 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15663 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15664 TYPE_ALLOC (type,
15665 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15666
15667 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15668 {
15669 struct fnfieldlist &nf = fip->fnfieldlists[i];
15670 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15671
15672 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15673 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15674 fn_flp->fn_fields = (struct fn_field *)
15675 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15676
15677 for (int k = 0; k < nf.fnfields.size (); ++k)
15678 fn_flp->fn_fields[k] = nf.fnfields[k];
15679 }
15680
15681 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15682 }
15683
15684 /* Returns non-zero if NAME is the name of a vtable member in CU's
15685 language, zero otherwise. */
15686 static int
15687 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15688 {
15689 static const char vptr[] = "_vptr";
15690
15691 /* Look for the C++ form of the vtable. */
15692 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15693 return 1;
15694
15695 return 0;
15696 }
15697
15698 /* GCC outputs unnamed structures that are really pointers to member
15699 functions, with the ABI-specified layout. If TYPE describes
15700 such a structure, smash it into a member function type.
15701
15702 GCC shouldn't do this; it should just output pointer to member DIEs.
15703 This is GCC PR debug/28767. */
15704
15705 static void
15706 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15707 {
15708 struct type *pfn_type, *self_type, *new_type;
15709
15710 /* Check for a structure with no name and two children. */
15711 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15712 return;
15713
15714 /* Check for __pfn and __delta members. */
15715 if (TYPE_FIELD_NAME (type, 0) == NULL
15716 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15717 || TYPE_FIELD_NAME (type, 1) == NULL
15718 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15719 return;
15720
15721 /* Find the type of the method. */
15722 pfn_type = TYPE_FIELD_TYPE (type, 0);
15723 if (pfn_type == NULL
15724 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15725 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15726 return;
15727
15728 /* Look for the "this" argument. */
15729 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15730 if (TYPE_NFIELDS (pfn_type) == 0
15731 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15732 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15733 return;
15734
15735 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15736 new_type = alloc_type (objfile);
15737 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15738 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15739 TYPE_VARARGS (pfn_type));
15740 smash_to_methodptr_type (type, new_type);
15741 }
15742
15743 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15744 appropriate error checking and issuing complaints if there is a
15745 problem. */
15746
15747 static ULONGEST
15748 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15749 {
15750 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15751
15752 if (attr == nullptr)
15753 return 0;
15754
15755 if (!attr_form_is_constant (attr))
15756 {
15757 complaint (_("DW_AT_alignment must have constant form"
15758 " - DIE at %s [in module %s]"),
15759 sect_offset_str (die->sect_off),
15760 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15761 return 0;
15762 }
15763
15764 ULONGEST align;
15765 if (attr->form == DW_FORM_sdata)
15766 {
15767 LONGEST val = DW_SND (attr);
15768 if (val < 0)
15769 {
15770 complaint (_("DW_AT_alignment value must not be negative"
15771 " - DIE at %s [in module %s]"),
15772 sect_offset_str (die->sect_off),
15773 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15774 return 0;
15775 }
15776 align = val;
15777 }
15778 else
15779 align = DW_UNSND (attr);
15780
15781 if (align == 0)
15782 {
15783 complaint (_("DW_AT_alignment value must not be zero"
15784 " - DIE at %s [in module %s]"),
15785 sect_offset_str (die->sect_off),
15786 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15787 return 0;
15788 }
15789 if ((align & (align - 1)) != 0)
15790 {
15791 complaint (_("DW_AT_alignment value must be a power of 2"
15792 " - DIE at %s [in module %s]"),
15793 sect_offset_str (die->sect_off),
15794 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15795 return 0;
15796 }
15797
15798 return align;
15799 }
15800
15801 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15802 the alignment for TYPE. */
15803
15804 static void
15805 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15806 struct type *type)
15807 {
15808 if (!set_type_align (type, get_alignment (cu, die)))
15809 complaint (_("DW_AT_alignment value too large"
15810 " - DIE at %s [in module %s]"),
15811 sect_offset_str (die->sect_off),
15812 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15813 }
15814
15815 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15816 constant for a type, according to DWARF5 spec, Table 5.5. */
15817
15818 static bool
15819 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15820 {
15821 switch (value)
15822 {
15823 case DW_CC_normal:
15824 case DW_CC_pass_by_reference:
15825 case DW_CC_pass_by_value:
15826 return true;
15827
15828 default:
15829 complaint (_("unrecognized DW_AT_calling_convention value "
15830 "(%s) for a type"), pulongest (value));
15831 return false;
15832 }
15833 }
15834
15835 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15836 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15837 also according to GNU-specific values (see include/dwarf2.h). */
15838
15839 static bool
15840 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15841 {
15842 switch (value)
15843 {
15844 case DW_CC_normal:
15845 case DW_CC_program:
15846 case DW_CC_nocall:
15847 return true;
15848
15849 case DW_CC_GNU_renesas_sh:
15850 case DW_CC_GNU_borland_fastcall_i386:
15851 case DW_CC_GDB_IBM_OpenCL:
15852 return true;
15853
15854 default:
15855 complaint (_("unrecognized DW_AT_calling_convention value "
15856 "(%s) for a subroutine"), pulongest (value));
15857 return false;
15858 }
15859 }
15860
15861 /* Called when we find the DIE that starts a structure or union scope
15862 (definition) to create a type for the structure or union. Fill in
15863 the type's name and general properties; the members will not be
15864 processed until process_structure_scope. A symbol table entry for
15865 the type will also not be done until process_structure_scope (assuming
15866 the type has a name).
15867
15868 NOTE: we need to call these functions regardless of whether or not the
15869 DIE has a DW_AT_name attribute, since it might be an anonymous
15870 structure or union. This gets the type entered into our set of
15871 user defined types. */
15872
15873 static struct type *
15874 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15875 {
15876 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15877 struct type *type;
15878 struct attribute *attr;
15879 const char *name;
15880
15881 /* If the definition of this type lives in .debug_types, read that type.
15882 Don't follow DW_AT_specification though, that will take us back up
15883 the chain and we want to go down. */
15884 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15885 if (attr != nullptr)
15886 {
15887 type = get_DW_AT_signature_type (die, attr, cu);
15888
15889 /* The type's CU may not be the same as CU.
15890 Ensure TYPE is recorded with CU in die_type_hash. */
15891 return set_die_type (die, type, cu);
15892 }
15893
15894 type = alloc_type (objfile);
15895 INIT_CPLUS_SPECIFIC (type);
15896
15897 name = dwarf2_name (die, cu);
15898 if (name != NULL)
15899 {
15900 if (cu->language == language_cplus
15901 || cu->language == language_d
15902 || cu->language == language_rust)
15903 {
15904 const char *full_name = dwarf2_full_name (name, die, cu);
15905
15906 /* dwarf2_full_name might have already finished building the DIE's
15907 type. If so, there is no need to continue. */
15908 if (get_die_type (die, cu) != NULL)
15909 return get_die_type (die, cu);
15910
15911 TYPE_NAME (type) = full_name;
15912 }
15913 else
15914 {
15915 /* The name is already allocated along with this objfile, so
15916 we don't need to duplicate it for the type. */
15917 TYPE_NAME (type) = name;
15918 }
15919 }
15920
15921 if (die->tag == DW_TAG_structure_type)
15922 {
15923 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15924 }
15925 else if (die->tag == DW_TAG_union_type)
15926 {
15927 TYPE_CODE (type) = TYPE_CODE_UNION;
15928 }
15929 else if (die->tag == DW_TAG_variant_part)
15930 {
15931 TYPE_CODE (type) = TYPE_CODE_UNION;
15932 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15933 }
15934 else
15935 {
15936 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15937 }
15938
15939 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15940 TYPE_DECLARED_CLASS (type) = 1;
15941
15942 /* Store the calling convention in the type if it's available in
15943 the die. Otherwise the calling convention remains set to
15944 the default value DW_CC_normal. */
15945 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15946 if (attr != nullptr
15947 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15948 {
15949 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15950 TYPE_CPLUS_CALLING_CONVENTION (type)
15951 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15952 }
15953
15954 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15955 if (attr != nullptr)
15956 {
15957 if (attr_form_is_constant (attr))
15958 TYPE_LENGTH (type) = DW_UNSND (attr);
15959 else
15960 {
15961 /* For the moment, dynamic type sizes are not supported
15962 by GDB's struct type. The actual size is determined
15963 on-demand when resolving the type of a given object,
15964 so set the type's length to zero for now. Otherwise,
15965 we record an expression as the length, and that expression
15966 could lead to a very large value, which could eventually
15967 lead to us trying to allocate that much memory when creating
15968 a value of that type. */
15969 TYPE_LENGTH (type) = 0;
15970 }
15971 }
15972 else
15973 {
15974 TYPE_LENGTH (type) = 0;
15975 }
15976
15977 maybe_set_alignment (cu, die, type);
15978
15979 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15980 {
15981 /* ICC<14 does not output the required DW_AT_declaration on
15982 incomplete types, but gives them a size of zero. */
15983 TYPE_STUB (type) = 1;
15984 }
15985 else
15986 TYPE_STUB_SUPPORTED (type) = 1;
15987
15988 if (die_is_declaration (die, cu))
15989 TYPE_STUB (type) = 1;
15990 else if (attr == NULL && die->child == NULL
15991 && producer_is_realview (cu->producer))
15992 /* RealView does not output the required DW_AT_declaration
15993 on incomplete types. */
15994 TYPE_STUB (type) = 1;
15995
15996 /* We need to add the type field to the die immediately so we don't
15997 infinitely recurse when dealing with pointers to the structure
15998 type within the structure itself. */
15999 set_die_type (die, type, cu);
16000
16001 /* set_die_type should be already done. */
16002 set_descriptive_type (type, die, cu);
16003
16004 return type;
16005 }
16006
16007 /* A helper for process_structure_scope that handles a single member
16008 DIE. */
16009
16010 static void
16011 handle_struct_member_die (struct die_info *child_die, struct type *type,
16012 struct field_info *fi,
16013 std::vector<struct symbol *> *template_args,
16014 struct dwarf2_cu *cu)
16015 {
16016 if (child_die->tag == DW_TAG_member
16017 || child_die->tag == DW_TAG_variable
16018 || child_die->tag == DW_TAG_variant_part)
16019 {
16020 /* NOTE: carlton/2002-11-05: A C++ static data member
16021 should be a DW_TAG_member that is a declaration, but
16022 all versions of G++ as of this writing (so through at
16023 least 3.2.1) incorrectly generate DW_TAG_variable
16024 tags for them instead. */
16025 dwarf2_add_field (fi, child_die, cu);
16026 }
16027 else if (child_die->tag == DW_TAG_subprogram)
16028 {
16029 /* Rust doesn't have member functions in the C++ sense.
16030 However, it does emit ordinary functions as children
16031 of a struct DIE. */
16032 if (cu->language == language_rust)
16033 read_func_scope (child_die, cu);
16034 else
16035 {
16036 /* C++ member function. */
16037 dwarf2_add_member_fn (fi, child_die, type, cu);
16038 }
16039 }
16040 else if (child_die->tag == DW_TAG_inheritance)
16041 {
16042 /* C++ base class field. */
16043 dwarf2_add_field (fi, child_die, cu);
16044 }
16045 else if (type_can_define_types (child_die))
16046 dwarf2_add_type_defn (fi, child_die, cu);
16047 else if (child_die->tag == DW_TAG_template_type_param
16048 || child_die->tag == DW_TAG_template_value_param)
16049 {
16050 struct symbol *arg = new_symbol (child_die, NULL, cu);
16051
16052 if (arg != NULL)
16053 template_args->push_back (arg);
16054 }
16055 else if (child_die->tag == DW_TAG_variant)
16056 {
16057 /* In a variant we want to get the discriminant and also add a
16058 field for our sole member child. */
16059 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16060
16061 for (die_info *variant_child = child_die->child;
16062 variant_child != NULL;
16063 variant_child = sibling_die (variant_child))
16064 {
16065 if (variant_child->tag == DW_TAG_member)
16066 {
16067 handle_struct_member_die (variant_child, type, fi,
16068 template_args, cu);
16069 /* Only handle the one. */
16070 break;
16071 }
16072 }
16073
16074 /* We don't handle this but we might as well report it if we see
16075 it. */
16076 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16077 complaint (_("DW_AT_discr_list is not supported yet"
16078 " - DIE at %s [in module %s]"),
16079 sect_offset_str (child_die->sect_off),
16080 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16081
16082 /* The first field was just added, so we can stash the
16083 discriminant there. */
16084 gdb_assert (!fi->fields.empty ());
16085 if (discr == NULL)
16086 fi->fields.back ().variant.default_branch = true;
16087 else
16088 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16089 }
16090 }
16091
16092 /* Finish creating a structure or union type, including filling in
16093 its members and creating a symbol for it. */
16094
16095 static void
16096 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16097 {
16098 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16099 struct die_info *child_die;
16100 struct type *type;
16101
16102 type = get_die_type (die, cu);
16103 if (type == NULL)
16104 type = read_structure_type (die, cu);
16105
16106 /* When reading a DW_TAG_variant_part, we need to notice when we
16107 read the discriminant member, so we can record it later in the
16108 discriminant_info. */
16109 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16110 sect_offset discr_offset {};
16111 bool has_template_parameters = false;
16112
16113 if (is_variant_part)
16114 {
16115 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16116 if (discr == NULL)
16117 {
16118 /* Maybe it's a univariant form, an extension we support.
16119 In this case arrange not to check the offset. */
16120 is_variant_part = false;
16121 }
16122 else if (attr_form_is_ref (discr))
16123 {
16124 struct dwarf2_cu *target_cu = cu;
16125 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16126
16127 discr_offset = target_die->sect_off;
16128 }
16129 else
16130 {
16131 complaint (_("DW_AT_discr does not have DIE reference form"
16132 " - DIE at %s [in module %s]"),
16133 sect_offset_str (die->sect_off),
16134 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16135 is_variant_part = false;
16136 }
16137 }
16138
16139 if (die->child != NULL && ! die_is_declaration (die, cu))
16140 {
16141 struct field_info fi;
16142 std::vector<struct symbol *> template_args;
16143
16144 child_die = die->child;
16145
16146 while (child_die && child_die->tag)
16147 {
16148 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16149
16150 if (is_variant_part && discr_offset == child_die->sect_off)
16151 fi.fields.back ().variant.is_discriminant = true;
16152
16153 child_die = sibling_die (child_die);
16154 }
16155
16156 /* Attach template arguments to type. */
16157 if (!template_args.empty ())
16158 {
16159 has_template_parameters = true;
16160 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16161 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16162 TYPE_TEMPLATE_ARGUMENTS (type)
16163 = XOBNEWVEC (&objfile->objfile_obstack,
16164 struct symbol *,
16165 TYPE_N_TEMPLATE_ARGUMENTS (type));
16166 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16167 template_args.data (),
16168 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16169 * sizeof (struct symbol *)));
16170 }
16171
16172 /* Attach fields and member functions to the type. */
16173 if (fi.nfields)
16174 dwarf2_attach_fields_to_type (&fi, type, cu);
16175 if (!fi.fnfieldlists.empty ())
16176 {
16177 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16178
16179 /* Get the type which refers to the base class (possibly this
16180 class itself) which contains the vtable pointer for the current
16181 class from the DW_AT_containing_type attribute. This use of
16182 DW_AT_containing_type is a GNU extension. */
16183
16184 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16185 {
16186 struct type *t = die_containing_type (die, cu);
16187
16188 set_type_vptr_basetype (type, t);
16189 if (type == t)
16190 {
16191 int i;
16192
16193 /* Our own class provides vtbl ptr. */
16194 for (i = TYPE_NFIELDS (t) - 1;
16195 i >= TYPE_N_BASECLASSES (t);
16196 --i)
16197 {
16198 const char *fieldname = TYPE_FIELD_NAME (t, i);
16199
16200 if (is_vtable_name (fieldname, cu))
16201 {
16202 set_type_vptr_fieldno (type, i);
16203 break;
16204 }
16205 }
16206
16207 /* Complain if virtual function table field not found. */
16208 if (i < TYPE_N_BASECLASSES (t))
16209 complaint (_("virtual function table pointer "
16210 "not found when defining class '%s'"),
16211 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16212 }
16213 else
16214 {
16215 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16216 }
16217 }
16218 else if (cu->producer
16219 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16220 {
16221 /* The IBM XLC compiler does not provide direct indication
16222 of the containing type, but the vtable pointer is
16223 always named __vfp. */
16224
16225 int i;
16226
16227 for (i = TYPE_NFIELDS (type) - 1;
16228 i >= TYPE_N_BASECLASSES (type);
16229 --i)
16230 {
16231 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16232 {
16233 set_type_vptr_fieldno (type, i);
16234 set_type_vptr_basetype (type, type);
16235 break;
16236 }
16237 }
16238 }
16239 }
16240
16241 /* Copy fi.typedef_field_list linked list elements content into the
16242 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16243 if (!fi.typedef_field_list.empty ())
16244 {
16245 int count = fi.typedef_field_list.size ();
16246
16247 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16248 TYPE_TYPEDEF_FIELD_ARRAY (type)
16249 = ((struct decl_field *)
16250 TYPE_ALLOC (type,
16251 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16252 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16253
16254 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16255 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16256 }
16257
16258 /* Copy fi.nested_types_list linked list elements content into the
16259 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16260 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16261 {
16262 int count = fi.nested_types_list.size ();
16263
16264 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16265 TYPE_NESTED_TYPES_ARRAY (type)
16266 = ((struct decl_field *)
16267 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16268 TYPE_NESTED_TYPES_COUNT (type) = count;
16269
16270 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16271 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16272 }
16273 }
16274
16275 quirk_gcc_member_function_pointer (type, objfile);
16276 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16277 cu->rust_unions.push_back (type);
16278
16279 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16280 snapshots) has been known to create a die giving a declaration
16281 for a class that has, as a child, a die giving a definition for a
16282 nested class. So we have to process our children even if the
16283 current die is a declaration. Normally, of course, a declaration
16284 won't have any children at all. */
16285
16286 child_die = die->child;
16287
16288 while (child_die != NULL && child_die->tag)
16289 {
16290 if (child_die->tag == DW_TAG_member
16291 || child_die->tag == DW_TAG_variable
16292 || child_die->tag == DW_TAG_inheritance
16293 || child_die->tag == DW_TAG_template_value_param
16294 || child_die->tag == DW_TAG_template_type_param)
16295 {
16296 /* Do nothing. */
16297 }
16298 else
16299 process_die (child_die, cu);
16300
16301 child_die = sibling_die (child_die);
16302 }
16303
16304 /* Do not consider external references. According to the DWARF standard,
16305 these DIEs are identified by the fact that they have no byte_size
16306 attribute, and a declaration attribute. */
16307 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16308 || !die_is_declaration (die, cu))
16309 {
16310 struct symbol *sym = new_symbol (die, type, cu);
16311
16312 if (has_template_parameters)
16313 {
16314 struct symtab *symtab;
16315 if (sym != nullptr)
16316 symtab = symbol_symtab (sym);
16317 else if (cu->line_header != nullptr)
16318 {
16319 /* Any related symtab will do. */
16320 symtab
16321 = cu->line_header->file_names ()[0].symtab;
16322 }
16323 else
16324 {
16325 symtab = nullptr;
16326 complaint (_("could not find suitable "
16327 "symtab for template parameter"
16328 " - DIE at %s [in module %s]"),
16329 sect_offset_str (die->sect_off),
16330 objfile_name (objfile));
16331 }
16332
16333 if (symtab != nullptr)
16334 {
16335 /* Make sure that the symtab is set on the new symbols.
16336 Even though they don't appear in this symtab directly,
16337 other parts of gdb assume that symbols do, and this is
16338 reasonably true. */
16339 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16340 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16341 }
16342 }
16343 }
16344 }
16345
16346 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16347 update TYPE using some information only available in DIE's children. */
16348
16349 static void
16350 update_enumeration_type_from_children (struct die_info *die,
16351 struct type *type,
16352 struct dwarf2_cu *cu)
16353 {
16354 struct die_info *child_die;
16355 int unsigned_enum = 1;
16356 int flag_enum = 1;
16357 ULONGEST mask = 0;
16358
16359 auto_obstack obstack;
16360
16361 for (child_die = die->child;
16362 child_die != NULL && child_die->tag;
16363 child_die = sibling_die (child_die))
16364 {
16365 struct attribute *attr;
16366 LONGEST value;
16367 const gdb_byte *bytes;
16368 struct dwarf2_locexpr_baton *baton;
16369 const char *name;
16370
16371 if (child_die->tag != DW_TAG_enumerator)
16372 continue;
16373
16374 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16375 if (attr == NULL)
16376 continue;
16377
16378 name = dwarf2_name (child_die, cu);
16379 if (name == NULL)
16380 name = "<anonymous enumerator>";
16381
16382 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16383 &value, &bytes, &baton);
16384 if (value < 0)
16385 {
16386 unsigned_enum = 0;
16387 flag_enum = 0;
16388 }
16389 else if ((mask & value) != 0)
16390 flag_enum = 0;
16391 else
16392 mask |= value;
16393
16394 /* If we already know that the enum type is neither unsigned, nor
16395 a flag type, no need to look at the rest of the enumerates. */
16396 if (!unsigned_enum && !flag_enum)
16397 break;
16398 }
16399
16400 if (unsigned_enum)
16401 TYPE_UNSIGNED (type) = 1;
16402 if (flag_enum)
16403 TYPE_FLAG_ENUM (type) = 1;
16404 }
16405
16406 /* Given a DW_AT_enumeration_type die, set its type. We do not
16407 complete the type's fields yet, or create any symbols. */
16408
16409 static struct type *
16410 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16411 {
16412 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16413 struct type *type;
16414 struct attribute *attr;
16415 const char *name;
16416
16417 /* If the definition of this type lives in .debug_types, read that type.
16418 Don't follow DW_AT_specification though, that will take us back up
16419 the chain and we want to go down. */
16420 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16421 if (attr != nullptr)
16422 {
16423 type = get_DW_AT_signature_type (die, attr, cu);
16424
16425 /* The type's CU may not be the same as CU.
16426 Ensure TYPE is recorded with CU in die_type_hash. */
16427 return set_die_type (die, type, cu);
16428 }
16429
16430 type = alloc_type (objfile);
16431
16432 TYPE_CODE (type) = TYPE_CODE_ENUM;
16433 name = dwarf2_full_name (NULL, die, cu);
16434 if (name != NULL)
16435 TYPE_NAME (type) = name;
16436
16437 attr = dwarf2_attr (die, DW_AT_type, cu);
16438 if (attr != NULL)
16439 {
16440 struct type *underlying_type = die_type (die, cu);
16441
16442 TYPE_TARGET_TYPE (type) = underlying_type;
16443 }
16444
16445 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16446 if (attr != nullptr)
16447 {
16448 TYPE_LENGTH (type) = DW_UNSND (attr);
16449 }
16450 else
16451 {
16452 TYPE_LENGTH (type) = 0;
16453 }
16454
16455 maybe_set_alignment (cu, die, type);
16456
16457 /* The enumeration DIE can be incomplete. In Ada, any type can be
16458 declared as private in the package spec, and then defined only
16459 inside the package body. Such types are known as Taft Amendment
16460 Types. When another package uses such a type, an incomplete DIE
16461 may be generated by the compiler. */
16462 if (die_is_declaration (die, cu))
16463 TYPE_STUB (type) = 1;
16464
16465 /* Finish the creation of this type by using the enum's children.
16466 We must call this even when the underlying type has been provided
16467 so that we can determine if we're looking at a "flag" enum. */
16468 update_enumeration_type_from_children (die, type, cu);
16469
16470 /* If this type has an underlying type that is not a stub, then we
16471 may use its attributes. We always use the "unsigned" attribute
16472 in this situation, because ordinarily we guess whether the type
16473 is unsigned -- but the guess can be wrong and the underlying type
16474 can tell us the reality. However, we defer to a local size
16475 attribute if one exists, because this lets the compiler override
16476 the underlying type if needed. */
16477 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16478 {
16479 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16480 if (TYPE_LENGTH (type) == 0)
16481 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16482 if (TYPE_RAW_ALIGN (type) == 0
16483 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16484 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16485 }
16486
16487 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16488
16489 return set_die_type (die, type, cu);
16490 }
16491
16492 /* Given a pointer to a die which begins an enumeration, process all
16493 the dies that define the members of the enumeration, and create the
16494 symbol for the enumeration type.
16495
16496 NOTE: We reverse the order of the element list. */
16497
16498 static void
16499 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16500 {
16501 struct type *this_type;
16502
16503 this_type = get_die_type (die, cu);
16504 if (this_type == NULL)
16505 this_type = read_enumeration_type (die, cu);
16506
16507 if (die->child != NULL)
16508 {
16509 struct die_info *child_die;
16510 struct symbol *sym;
16511 std::vector<struct field> fields;
16512 const char *name;
16513
16514 child_die = die->child;
16515 while (child_die && child_die->tag)
16516 {
16517 if (child_die->tag != DW_TAG_enumerator)
16518 {
16519 process_die (child_die, cu);
16520 }
16521 else
16522 {
16523 name = dwarf2_name (child_die, cu);
16524 if (name)
16525 {
16526 sym = new_symbol (child_die, this_type, cu);
16527
16528 fields.emplace_back ();
16529 struct field &field = fields.back ();
16530
16531 FIELD_NAME (field) = sym->linkage_name ();
16532 FIELD_TYPE (field) = NULL;
16533 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16534 FIELD_BITSIZE (field) = 0;
16535 }
16536 }
16537
16538 child_die = sibling_die (child_die);
16539 }
16540
16541 if (!fields.empty ())
16542 {
16543 TYPE_NFIELDS (this_type) = fields.size ();
16544 TYPE_FIELDS (this_type) = (struct field *)
16545 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16546 memcpy (TYPE_FIELDS (this_type), fields.data (),
16547 sizeof (struct field) * fields.size ());
16548 }
16549 }
16550
16551 /* If we are reading an enum from a .debug_types unit, and the enum
16552 is a declaration, and the enum is not the signatured type in the
16553 unit, then we do not want to add a symbol for it. Adding a
16554 symbol would in some cases obscure the true definition of the
16555 enum, giving users an incomplete type when the definition is
16556 actually available. Note that we do not want to do this for all
16557 enums which are just declarations, because C++0x allows forward
16558 enum declarations. */
16559 if (cu->per_cu->is_debug_types
16560 && die_is_declaration (die, cu))
16561 {
16562 struct signatured_type *sig_type;
16563
16564 sig_type = (struct signatured_type *) cu->per_cu;
16565 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16566 if (sig_type->type_offset_in_section != die->sect_off)
16567 return;
16568 }
16569
16570 new_symbol (die, this_type, cu);
16571 }
16572
16573 /* Extract all information from a DW_TAG_array_type DIE and put it in
16574 the DIE's type field. For now, this only handles one dimensional
16575 arrays. */
16576
16577 static struct type *
16578 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16579 {
16580 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16581 struct die_info *child_die;
16582 struct type *type;
16583 struct type *element_type, *range_type, *index_type;
16584 struct attribute *attr;
16585 const char *name;
16586 struct dynamic_prop *byte_stride_prop = NULL;
16587 unsigned int bit_stride = 0;
16588
16589 element_type = die_type (die, cu);
16590
16591 /* The die_type call above may have already set the type for this DIE. */
16592 type = get_die_type (die, cu);
16593 if (type)
16594 return type;
16595
16596 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16597 if (attr != NULL)
16598 {
16599 int stride_ok;
16600 struct type *prop_type
16601 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16602
16603 byte_stride_prop
16604 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16605 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16606 prop_type);
16607 if (!stride_ok)
16608 {
16609 complaint (_("unable to read array DW_AT_byte_stride "
16610 " - DIE at %s [in module %s]"),
16611 sect_offset_str (die->sect_off),
16612 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16613 /* Ignore this attribute. We will likely not be able to print
16614 arrays of this type correctly, but there is little we can do
16615 to help if we cannot read the attribute's value. */
16616 byte_stride_prop = NULL;
16617 }
16618 }
16619
16620 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16621 if (attr != NULL)
16622 bit_stride = DW_UNSND (attr);
16623
16624 /* Irix 6.2 native cc creates array types without children for
16625 arrays with unspecified length. */
16626 if (die->child == NULL)
16627 {
16628 index_type = objfile_type (objfile)->builtin_int;
16629 range_type = create_static_range_type (NULL, index_type, 0, -1);
16630 type = create_array_type_with_stride (NULL, element_type, range_type,
16631 byte_stride_prop, bit_stride);
16632 return set_die_type (die, type, cu);
16633 }
16634
16635 std::vector<struct type *> range_types;
16636 child_die = die->child;
16637 while (child_die && child_die->tag)
16638 {
16639 if (child_die->tag == DW_TAG_subrange_type)
16640 {
16641 struct type *child_type = read_type_die (child_die, cu);
16642
16643 if (child_type != NULL)
16644 {
16645 /* The range type was succesfully read. Save it for the
16646 array type creation. */
16647 range_types.push_back (child_type);
16648 }
16649 }
16650 child_die = sibling_die (child_die);
16651 }
16652
16653 /* Dwarf2 dimensions are output from left to right, create the
16654 necessary array types in backwards order. */
16655
16656 type = element_type;
16657
16658 if (read_array_order (die, cu) == DW_ORD_col_major)
16659 {
16660 int i = 0;
16661
16662 while (i < range_types.size ())
16663 type = create_array_type_with_stride (NULL, type, range_types[i++],
16664 byte_stride_prop, bit_stride);
16665 }
16666 else
16667 {
16668 size_t ndim = range_types.size ();
16669 while (ndim-- > 0)
16670 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16671 byte_stride_prop, bit_stride);
16672 }
16673
16674 /* Understand Dwarf2 support for vector types (like they occur on
16675 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16676 array type. This is not part of the Dwarf2/3 standard yet, but a
16677 custom vendor extension. The main difference between a regular
16678 array and the vector variant is that vectors are passed by value
16679 to functions. */
16680 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16681 if (attr != nullptr)
16682 make_vector_type (type);
16683
16684 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16685 implementation may choose to implement triple vectors using this
16686 attribute. */
16687 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16688 if (attr != nullptr)
16689 {
16690 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16691 TYPE_LENGTH (type) = DW_UNSND (attr);
16692 else
16693 complaint (_("DW_AT_byte_size for array type smaller "
16694 "than the total size of elements"));
16695 }
16696
16697 name = dwarf2_name (die, cu);
16698 if (name)
16699 TYPE_NAME (type) = name;
16700
16701 maybe_set_alignment (cu, die, type);
16702
16703 /* Install the type in the die. */
16704 set_die_type (die, type, cu);
16705
16706 /* set_die_type should be already done. */
16707 set_descriptive_type (type, die, cu);
16708
16709 return type;
16710 }
16711
16712 static enum dwarf_array_dim_ordering
16713 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16714 {
16715 struct attribute *attr;
16716
16717 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16718
16719 if (attr != nullptr)
16720 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16721
16722 /* GNU F77 is a special case, as at 08/2004 array type info is the
16723 opposite order to the dwarf2 specification, but data is still
16724 laid out as per normal fortran.
16725
16726 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16727 version checking. */
16728
16729 if (cu->language == language_fortran
16730 && cu->producer && strstr (cu->producer, "GNU F77"))
16731 {
16732 return DW_ORD_row_major;
16733 }
16734
16735 switch (cu->language_defn->la_array_ordering)
16736 {
16737 case array_column_major:
16738 return DW_ORD_col_major;
16739 case array_row_major:
16740 default:
16741 return DW_ORD_row_major;
16742 };
16743 }
16744
16745 /* Extract all information from a DW_TAG_set_type DIE and put it in
16746 the DIE's type field. */
16747
16748 static struct type *
16749 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16750 {
16751 struct type *domain_type, *set_type;
16752 struct attribute *attr;
16753
16754 domain_type = die_type (die, cu);
16755
16756 /* The die_type call above may have already set the type for this DIE. */
16757 set_type = get_die_type (die, cu);
16758 if (set_type)
16759 return set_type;
16760
16761 set_type = create_set_type (NULL, domain_type);
16762
16763 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16764 if (attr != nullptr)
16765 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16766
16767 maybe_set_alignment (cu, die, set_type);
16768
16769 return set_die_type (die, set_type, cu);
16770 }
16771
16772 /* A helper for read_common_block that creates a locexpr baton.
16773 SYM is the symbol which we are marking as computed.
16774 COMMON_DIE is the DIE for the common block.
16775 COMMON_LOC is the location expression attribute for the common
16776 block itself.
16777 MEMBER_LOC is the location expression attribute for the particular
16778 member of the common block that we are processing.
16779 CU is the CU from which the above come. */
16780
16781 static void
16782 mark_common_block_symbol_computed (struct symbol *sym,
16783 struct die_info *common_die,
16784 struct attribute *common_loc,
16785 struct attribute *member_loc,
16786 struct dwarf2_cu *cu)
16787 {
16788 struct dwarf2_per_objfile *dwarf2_per_objfile
16789 = cu->per_cu->dwarf2_per_objfile;
16790 struct objfile *objfile = dwarf2_per_objfile->objfile;
16791 struct dwarf2_locexpr_baton *baton;
16792 gdb_byte *ptr;
16793 unsigned int cu_off;
16794 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16795 LONGEST offset = 0;
16796
16797 gdb_assert (common_loc && member_loc);
16798 gdb_assert (attr_form_is_block (common_loc));
16799 gdb_assert (attr_form_is_block (member_loc)
16800 || attr_form_is_constant (member_loc));
16801
16802 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16803 baton->per_cu = cu->per_cu;
16804 gdb_assert (baton->per_cu);
16805
16806 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16807
16808 if (attr_form_is_constant (member_loc))
16809 {
16810 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16811 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16812 }
16813 else
16814 baton->size += DW_BLOCK (member_loc)->size;
16815
16816 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16817 baton->data = ptr;
16818
16819 *ptr++ = DW_OP_call4;
16820 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16821 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16822 ptr += 4;
16823
16824 if (attr_form_is_constant (member_loc))
16825 {
16826 *ptr++ = DW_OP_addr;
16827 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16828 ptr += cu->header.addr_size;
16829 }
16830 else
16831 {
16832 /* We have to copy the data here, because DW_OP_call4 will only
16833 use a DW_AT_location attribute. */
16834 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16835 ptr += DW_BLOCK (member_loc)->size;
16836 }
16837
16838 *ptr++ = DW_OP_plus;
16839 gdb_assert (ptr - baton->data == baton->size);
16840
16841 SYMBOL_LOCATION_BATON (sym) = baton;
16842 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16843 }
16844
16845 /* Create appropriate locally-scoped variables for all the
16846 DW_TAG_common_block entries. Also create a struct common_block
16847 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16848 is used to separate the common blocks name namespace from regular
16849 variable names. */
16850
16851 static void
16852 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16853 {
16854 struct attribute *attr;
16855
16856 attr = dwarf2_attr (die, DW_AT_location, cu);
16857 if (attr != nullptr)
16858 {
16859 /* Support the .debug_loc offsets. */
16860 if (attr_form_is_block (attr))
16861 {
16862 /* Ok. */
16863 }
16864 else if (attr_form_is_section_offset (attr))
16865 {
16866 dwarf2_complex_location_expr_complaint ();
16867 attr = NULL;
16868 }
16869 else
16870 {
16871 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16872 "common block member");
16873 attr = NULL;
16874 }
16875 }
16876
16877 if (die->child != NULL)
16878 {
16879 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16880 struct die_info *child_die;
16881 size_t n_entries = 0, size;
16882 struct common_block *common_block;
16883 struct symbol *sym;
16884
16885 for (child_die = die->child;
16886 child_die && child_die->tag;
16887 child_die = sibling_die (child_die))
16888 ++n_entries;
16889
16890 size = (sizeof (struct common_block)
16891 + (n_entries - 1) * sizeof (struct symbol *));
16892 common_block
16893 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16894 size);
16895 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16896 common_block->n_entries = 0;
16897
16898 for (child_die = die->child;
16899 child_die && child_die->tag;
16900 child_die = sibling_die (child_die))
16901 {
16902 /* Create the symbol in the DW_TAG_common_block block in the current
16903 symbol scope. */
16904 sym = new_symbol (child_die, NULL, cu);
16905 if (sym != NULL)
16906 {
16907 struct attribute *member_loc;
16908
16909 common_block->contents[common_block->n_entries++] = sym;
16910
16911 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16912 cu);
16913 if (member_loc)
16914 {
16915 /* GDB has handled this for a long time, but it is
16916 not specified by DWARF. It seems to have been
16917 emitted by gfortran at least as recently as:
16918 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16919 complaint (_("Variable in common block has "
16920 "DW_AT_data_member_location "
16921 "- DIE at %s [in module %s]"),
16922 sect_offset_str (child_die->sect_off),
16923 objfile_name (objfile));
16924
16925 if (attr_form_is_section_offset (member_loc))
16926 dwarf2_complex_location_expr_complaint ();
16927 else if (attr_form_is_constant (member_loc)
16928 || attr_form_is_block (member_loc))
16929 {
16930 if (attr != nullptr)
16931 mark_common_block_symbol_computed (sym, die, attr,
16932 member_loc, cu);
16933 }
16934 else
16935 dwarf2_complex_location_expr_complaint ();
16936 }
16937 }
16938 }
16939
16940 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16941 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16942 }
16943 }
16944
16945 /* Create a type for a C++ namespace. */
16946
16947 static struct type *
16948 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16949 {
16950 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16951 const char *previous_prefix, *name;
16952 int is_anonymous;
16953 struct type *type;
16954
16955 /* For extensions, reuse the type of the original namespace. */
16956 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16957 {
16958 struct die_info *ext_die;
16959 struct dwarf2_cu *ext_cu = cu;
16960
16961 ext_die = dwarf2_extension (die, &ext_cu);
16962 type = read_type_die (ext_die, ext_cu);
16963
16964 /* EXT_CU may not be the same as CU.
16965 Ensure TYPE is recorded with CU in die_type_hash. */
16966 return set_die_type (die, type, cu);
16967 }
16968
16969 name = namespace_name (die, &is_anonymous, cu);
16970
16971 /* Now build the name of the current namespace. */
16972
16973 previous_prefix = determine_prefix (die, cu);
16974 if (previous_prefix[0] != '\0')
16975 name = typename_concat (&objfile->objfile_obstack,
16976 previous_prefix, name, 0, cu);
16977
16978 /* Create the type. */
16979 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16980
16981 return set_die_type (die, type, cu);
16982 }
16983
16984 /* Read a namespace scope. */
16985
16986 static void
16987 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16988 {
16989 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16990 int is_anonymous;
16991
16992 /* Add a symbol associated to this if we haven't seen the namespace
16993 before. Also, add a using directive if it's an anonymous
16994 namespace. */
16995
16996 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16997 {
16998 struct type *type;
16999
17000 type = read_type_die (die, cu);
17001 new_symbol (die, type, cu);
17002
17003 namespace_name (die, &is_anonymous, cu);
17004 if (is_anonymous)
17005 {
17006 const char *previous_prefix = determine_prefix (die, cu);
17007
17008 std::vector<const char *> excludes;
17009 add_using_directive (using_directives (cu),
17010 previous_prefix, TYPE_NAME (type), NULL,
17011 NULL, excludes, 0, &objfile->objfile_obstack);
17012 }
17013 }
17014
17015 if (die->child != NULL)
17016 {
17017 struct die_info *child_die = die->child;
17018
17019 while (child_die && child_die->tag)
17020 {
17021 process_die (child_die, cu);
17022 child_die = sibling_die (child_die);
17023 }
17024 }
17025 }
17026
17027 /* Read a Fortran module as type. This DIE can be only a declaration used for
17028 imported module. Still we need that type as local Fortran "use ... only"
17029 declaration imports depend on the created type in determine_prefix. */
17030
17031 static struct type *
17032 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17033 {
17034 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17035 const char *module_name;
17036 struct type *type;
17037
17038 module_name = dwarf2_name (die, cu);
17039 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17040
17041 return set_die_type (die, type, cu);
17042 }
17043
17044 /* Read a Fortran module. */
17045
17046 static void
17047 read_module (struct die_info *die, struct dwarf2_cu *cu)
17048 {
17049 struct die_info *child_die = die->child;
17050 struct type *type;
17051
17052 type = read_type_die (die, cu);
17053 new_symbol (die, type, cu);
17054
17055 while (child_die && child_die->tag)
17056 {
17057 process_die (child_die, cu);
17058 child_die = sibling_die (child_die);
17059 }
17060 }
17061
17062 /* Return the name of the namespace represented by DIE. Set
17063 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17064 namespace. */
17065
17066 static const char *
17067 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17068 {
17069 struct die_info *current_die;
17070 const char *name = NULL;
17071
17072 /* Loop through the extensions until we find a name. */
17073
17074 for (current_die = die;
17075 current_die != NULL;
17076 current_die = dwarf2_extension (die, &cu))
17077 {
17078 /* We don't use dwarf2_name here so that we can detect the absence
17079 of a name -> anonymous namespace. */
17080 name = dwarf2_string_attr (die, DW_AT_name, cu);
17081
17082 if (name != NULL)
17083 break;
17084 }
17085
17086 /* Is it an anonymous namespace? */
17087
17088 *is_anonymous = (name == NULL);
17089 if (*is_anonymous)
17090 name = CP_ANONYMOUS_NAMESPACE_STR;
17091
17092 return name;
17093 }
17094
17095 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17096 the user defined type vector. */
17097
17098 static struct type *
17099 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17100 {
17101 struct gdbarch *gdbarch
17102 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17103 struct comp_unit_head *cu_header = &cu->header;
17104 struct type *type;
17105 struct attribute *attr_byte_size;
17106 struct attribute *attr_address_class;
17107 int byte_size, addr_class;
17108 struct type *target_type;
17109
17110 target_type = die_type (die, cu);
17111
17112 /* The die_type call above may have already set the type for this DIE. */
17113 type = get_die_type (die, cu);
17114 if (type)
17115 return type;
17116
17117 type = lookup_pointer_type (target_type);
17118
17119 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17120 if (attr_byte_size)
17121 byte_size = DW_UNSND (attr_byte_size);
17122 else
17123 byte_size = cu_header->addr_size;
17124
17125 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17126 if (attr_address_class)
17127 addr_class = DW_UNSND (attr_address_class);
17128 else
17129 addr_class = DW_ADDR_none;
17130
17131 ULONGEST alignment = get_alignment (cu, die);
17132
17133 /* If the pointer size, alignment, or address class is different
17134 than the default, create a type variant marked as such and set
17135 the length accordingly. */
17136 if (TYPE_LENGTH (type) != byte_size
17137 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17138 && alignment != TYPE_RAW_ALIGN (type))
17139 || addr_class != DW_ADDR_none)
17140 {
17141 if (gdbarch_address_class_type_flags_p (gdbarch))
17142 {
17143 int type_flags;
17144
17145 type_flags = gdbarch_address_class_type_flags
17146 (gdbarch, byte_size, addr_class);
17147 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17148 == 0);
17149 type = make_type_with_address_space (type, type_flags);
17150 }
17151 else if (TYPE_LENGTH (type) != byte_size)
17152 {
17153 complaint (_("invalid pointer size %d"), byte_size);
17154 }
17155 else if (TYPE_RAW_ALIGN (type) != alignment)
17156 {
17157 complaint (_("Invalid DW_AT_alignment"
17158 " - DIE at %s [in module %s]"),
17159 sect_offset_str (die->sect_off),
17160 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17161 }
17162 else
17163 {
17164 /* Should we also complain about unhandled address classes? */
17165 }
17166 }
17167
17168 TYPE_LENGTH (type) = byte_size;
17169 set_type_align (type, alignment);
17170 return set_die_type (die, type, cu);
17171 }
17172
17173 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17174 the user defined type vector. */
17175
17176 static struct type *
17177 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17178 {
17179 struct type *type;
17180 struct type *to_type;
17181 struct type *domain;
17182
17183 to_type = die_type (die, cu);
17184 domain = die_containing_type (die, cu);
17185
17186 /* The calls above may have already set the type for this DIE. */
17187 type = get_die_type (die, cu);
17188 if (type)
17189 return type;
17190
17191 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17192 type = lookup_methodptr_type (to_type);
17193 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17194 {
17195 struct type *new_type
17196 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17197
17198 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17199 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17200 TYPE_VARARGS (to_type));
17201 type = lookup_methodptr_type (new_type);
17202 }
17203 else
17204 type = lookup_memberptr_type (to_type, domain);
17205
17206 return set_die_type (die, type, cu);
17207 }
17208
17209 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17210 the user defined type vector. */
17211
17212 static struct type *
17213 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17214 enum type_code refcode)
17215 {
17216 struct comp_unit_head *cu_header = &cu->header;
17217 struct type *type, *target_type;
17218 struct attribute *attr;
17219
17220 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17221
17222 target_type = die_type (die, cu);
17223
17224 /* The die_type call above may have already set the type for this DIE. */
17225 type = get_die_type (die, cu);
17226 if (type)
17227 return type;
17228
17229 type = lookup_reference_type (target_type, refcode);
17230 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17231 if (attr != nullptr)
17232 {
17233 TYPE_LENGTH (type) = DW_UNSND (attr);
17234 }
17235 else
17236 {
17237 TYPE_LENGTH (type) = cu_header->addr_size;
17238 }
17239 maybe_set_alignment (cu, die, type);
17240 return set_die_type (die, type, cu);
17241 }
17242
17243 /* Add the given cv-qualifiers to the element type of the array. GCC
17244 outputs DWARF type qualifiers that apply to an array, not the
17245 element type. But GDB relies on the array element type to carry
17246 the cv-qualifiers. This mimics section 6.7.3 of the C99
17247 specification. */
17248
17249 static struct type *
17250 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17251 struct type *base_type, int cnst, int voltl)
17252 {
17253 struct type *el_type, *inner_array;
17254
17255 base_type = copy_type (base_type);
17256 inner_array = base_type;
17257
17258 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17259 {
17260 TYPE_TARGET_TYPE (inner_array) =
17261 copy_type (TYPE_TARGET_TYPE (inner_array));
17262 inner_array = TYPE_TARGET_TYPE (inner_array);
17263 }
17264
17265 el_type = TYPE_TARGET_TYPE (inner_array);
17266 cnst |= TYPE_CONST (el_type);
17267 voltl |= TYPE_VOLATILE (el_type);
17268 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17269
17270 return set_die_type (die, base_type, cu);
17271 }
17272
17273 static struct type *
17274 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17275 {
17276 struct type *base_type, *cv_type;
17277
17278 base_type = die_type (die, cu);
17279
17280 /* The die_type call above may have already set the type for this DIE. */
17281 cv_type = get_die_type (die, cu);
17282 if (cv_type)
17283 return cv_type;
17284
17285 /* In case the const qualifier is applied to an array type, the element type
17286 is so qualified, not the array type (section 6.7.3 of C99). */
17287 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17288 return add_array_cv_type (die, cu, base_type, 1, 0);
17289
17290 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17291 return set_die_type (die, cv_type, cu);
17292 }
17293
17294 static struct type *
17295 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17296 {
17297 struct type *base_type, *cv_type;
17298
17299 base_type = die_type (die, cu);
17300
17301 /* The die_type call above may have already set the type for this DIE. */
17302 cv_type = get_die_type (die, cu);
17303 if (cv_type)
17304 return cv_type;
17305
17306 /* In case the volatile qualifier is applied to an array type, the
17307 element type is so qualified, not the array type (section 6.7.3
17308 of C99). */
17309 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17310 return add_array_cv_type (die, cu, base_type, 0, 1);
17311
17312 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17313 return set_die_type (die, cv_type, cu);
17314 }
17315
17316 /* Handle DW_TAG_restrict_type. */
17317
17318 static struct type *
17319 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17320 {
17321 struct type *base_type, *cv_type;
17322
17323 base_type = die_type (die, cu);
17324
17325 /* The die_type call above may have already set the type for this DIE. */
17326 cv_type = get_die_type (die, cu);
17327 if (cv_type)
17328 return cv_type;
17329
17330 cv_type = make_restrict_type (base_type);
17331 return set_die_type (die, cv_type, cu);
17332 }
17333
17334 /* Handle DW_TAG_atomic_type. */
17335
17336 static struct type *
17337 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17338 {
17339 struct type *base_type, *cv_type;
17340
17341 base_type = die_type (die, cu);
17342
17343 /* The die_type call above may have already set the type for this DIE. */
17344 cv_type = get_die_type (die, cu);
17345 if (cv_type)
17346 return cv_type;
17347
17348 cv_type = make_atomic_type (base_type);
17349 return set_die_type (die, cv_type, cu);
17350 }
17351
17352 /* Extract all information from a DW_TAG_string_type DIE and add to
17353 the user defined type vector. It isn't really a user defined type,
17354 but it behaves like one, with other DIE's using an AT_user_def_type
17355 attribute to reference it. */
17356
17357 static struct type *
17358 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17359 {
17360 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17361 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17362 struct type *type, *range_type, *index_type, *char_type;
17363 struct attribute *attr;
17364 struct dynamic_prop prop;
17365 bool length_is_constant = true;
17366 LONGEST length;
17367
17368 /* There are a couple of places where bit sizes might be made use of
17369 when parsing a DW_TAG_string_type, however, no producer that we know
17370 of make use of these. Handling bit sizes that are a multiple of the
17371 byte size is easy enough, but what about other bit sizes? Lets deal
17372 with that problem when we have to. Warn about these attributes being
17373 unsupported, then parse the type and ignore them like we always
17374 have. */
17375 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17376 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17377 {
17378 static bool warning_printed = false;
17379 if (!warning_printed)
17380 {
17381 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17382 "currently supported on DW_TAG_string_type."));
17383 warning_printed = true;
17384 }
17385 }
17386
17387 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17388 if (attr != nullptr && !attr_form_is_constant (attr))
17389 {
17390 /* The string length describes the location at which the length of
17391 the string can be found. The size of the length field can be
17392 specified with one of the attributes below. */
17393 struct type *prop_type;
17394 struct attribute *len
17395 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17396 if (len == nullptr)
17397 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17398 if (len != nullptr && attr_form_is_constant (len))
17399 {
17400 /* Pass 0 as the default as we know this attribute is constant
17401 and the default value will not be returned. */
17402 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17403 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17404 }
17405 else
17406 {
17407 /* If the size is not specified then we assume it is the size of
17408 an address on this target. */
17409 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17410 }
17411
17412 /* Convert the attribute into a dynamic property. */
17413 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17414 length = 1;
17415 else
17416 length_is_constant = false;
17417 }
17418 else if (attr != nullptr)
17419 {
17420 /* This DW_AT_string_length just contains the length with no
17421 indirection. There's no need to create a dynamic property in this
17422 case. Pass 0 for the default value as we know it will not be
17423 returned in this case. */
17424 length = dwarf2_get_attr_constant_value (attr, 0);
17425 }
17426 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17427 {
17428 /* We don't currently support non-constant byte sizes for strings. */
17429 length = dwarf2_get_attr_constant_value (attr, 1);
17430 }
17431 else
17432 {
17433 /* Use 1 as a fallback length if we have nothing else. */
17434 length = 1;
17435 }
17436
17437 index_type = objfile_type (objfile)->builtin_int;
17438 if (length_is_constant)
17439 range_type = create_static_range_type (NULL, index_type, 1, length);
17440 else
17441 {
17442 struct dynamic_prop low_bound;
17443
17444 low_bound.kind = PROP_CONST;
17445 low_bound.data.const_val = 1;
17446 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17447 }
17448 char_type = language_string_char_type (cu->language_defn, gdbarch);
17449 type = create_string_type (NULL, char_type, range_type);
17450
17451 return set_die_type (die, type, cu);
17452 }
17453
17454 /* Assuming that DIE corresponds to a function, returns nonzero
17455 if the function is prototyped. */
17456
17457 static int
17458 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17459 {
17460 struct attribute *attr;
17461
17462 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17463 if (attr && (DW_UNSND (attr) != 0))
17464 return 1;
17465
17466 /* The DWARF standard implies that the DW_AT_prototyped attribute
17467 is only meaningful for C, but the concept also extends to other
17468 languages that allow unprototyped functions (Eg: Objective C).
17469 For all other languages, assume that functions are always
17470 prototyped. */
17471 if (cu->language != language_c
17472 && cu->language != language_objc
17473 && cu->language != language_opencl)
17474 return 1;
17475
17476 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17477 prototyped and unprototyped functions; default to prototyped,
17478 since that is more common in modern code (and RealView warns
17479 about unprototyped functions). */
17480 if (producer_is_realview (cu->producer))
17481 return 1;
17482
17483 return 0;
17484 }
17485
17486 /* Handle DIES due to C code like:
17487
17488 struct foo
17489 {
17490 int (*funcp)(int a, long l);
17491 int b;
17492 };
17493
17494 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17495
17496 static struct type *
17497 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17498 {
17499 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17500 struct type *type; /* Type that this function returns. */
17501 struct type *ftype; /* Function that returns above type. */
17502 struct attribute *attr;
17503
17504 type = die_type (die, cu);
17505
17506 /* The die_type call above may have already set the type for this DIE. */
17507 ftype = get_die_type (die, cu);
17508 if (ftype)
17509 return ftype;
17510
17511 ftype = lookup_function_type (type);
17512
17513 if (prototyped_function_p (die, cu))
17514 TYPE_PROTOTYPED (ftype) = 1;
17515
17516 /* Store the calling convention in the type if it's available in
17517 the subroutine die. Otherwise set the calling convention to
17518 the default value DW_CC_normal. */
17519 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17520 if (attr != nullptr
17521 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17522 TYPE_CALLING_CONVENTION (ftype)
17523 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17524 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17525 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17526 else
17527 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17528
17529 /* Record whether the function returns normally to its caller or not
17530 if the DWARF producer set that information. */
17531 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17532 if (attr && (DW_UNSND (attr) != 0))
17533 TYPE_NO_RETURN (ftype) = 1;
17534
17535 /* We need to add the subroutine type to the die immediately so
17536 we don't infinitely recurse when dealing with parameters
17537 declared as the same subroutine type. */
17538 set_die_type (die, ftype, cu);
17539
17540 if (die->child != NULL)
17541 {
17542 struct type *void_type = objfile_type (objfile)->builtin_void;
17543 struct die_info *child_die;
17544 int nparams, iparams;
17545
17546 /* Count the number of parameters.
17547 FIXME: GDB currently ignores vararg functions, but knows about
17548 vararg member functions. */
17549 nparams = 0;
17550 child_die = die->child;
17551 while (child_die && child_die->tag)
17552 {
17553 if (child_die->tag == DW_TAG_formal_parameter)
17554 nparams++;
17555 else if (child_die->tag == DW_TAG_unspecified_parameters)
17556 TYPE_VARARGS (ftype) = 1;
17557 child_die = sibling_die (child_die);
17558 }
17559
17560 /* Allocate storage for parameters and fill them in. */
17561 TYPE_NFIELDS (ftype) = nparams;
17562 TYPE_FIELDS (ftype) = (struct field *)
17563 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17564
17565 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17566 even if we error out during the parameters reading below. */
17567 for (iparams = 0; iparams < nparams; iparams++)
17568 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17569
17570 iparams = 0;
17571 child_die = die->child;
17572 while (child_die && child_die->tag)
17573 {
17574 if (child_die->tag == DW_TAG_formal_parameter)
17575 {
17576 struct type *arg_type;
17577
17578 /* DWARF version 2 has no clean way to discern C++
17579 static and non-static member functions. G++ helps
17580 GDB by marking the first parameter for non-static
17581 member functions (which is the this pointer) as
17582 artificial. We pass this information to
17583 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17584
17585 DWARF version 3 added DW_AT_object_pointer, which GCC
17586 4.5 does not yet generate. */
17587 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17588 if (attr != nullptr)
17589 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17590 else
17591 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17592 arg_type = die_type (child_die, cu);
17593
17594 /* RealView does not mark THIS as const, which the testsuite
17595 expects. GCC marks THIS as const in method definitions,
17596 but not in the class specifications (GCC PR 43053). */
17597 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17598 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17599 {
17600 int is_this = 0;
17601 struct dwarf2_cu *arg_cu = cu;
17602 const char *name = dwarf2_name (child_die, cu);
17603
17604 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17605 if (attr != nullptr)
17606 {
17607 /* If the compiler emits this, use it. */
17608 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17609 is_this = 1;
17610 }
17611 else if (name && strcmp (name, "this") == 0)
17612 /* Function definitions will have the argument names. */
17613 is_this = 1;
17614 else if (name == NULL && iparams == 0)
17615 /* Declarations may not have the names, so like
17616 elsewhere in GDB, assume an artificial first
17617 argument is "this". */
17618 is_this = 1;
17619
17620 if (is_this)
17621 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17622 arg_type, 0);
17623 }
17624
17625 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17626 iparams++;
17627 }
17628 child_die = sibling_die (child_die);
17629 }
17630 }
17631
17632 return ftype;
17633 }
17634
17635 static struct type *
17636 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17637 {
17638 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17639 const char *name = NULL;
17640 struct type *this_type, *target_type;
17641
17642 name = dwarf2_full_name (NULL, die, cu);
17643 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17644 TYPE_TARGET_STUB (this_type) = 1;
17645 set_die_type (die, this_type, cu);
17646 target_type = die_type (die, cu);
17647 if (target_type != this_type)
17648 TYPE_TARGET_TYPE (this_type) = target_type;
17649 else
17650 {
17651 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17652 spec and cause infinite loops in GDB. */
17653 complaint (_("Self-referential DW_TAG_typedef "
17654 "- DIE at %s [in module %s]"),
17655 sect_offset_str (die->sect_off), objfile_name (objfile));
17656 TYPE_TARGET_TYPE (this_type) = NULL;
17657 }
17658 return this_type;
17659 }
17660
17661 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17662 (which may be different from NAME) to the architecture back-end to allow
17663 it to guess the correct format if necessary. */
17664
17665 static struct type *
17666 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17667 const char *name_hint, enum bfd_endian byte_order)
17668 {
17669 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17670 const struct floatformat **format;
17671 struct type *type;
17672
17673 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17674 if (format)
17675 type = init_float_type (objfile, bits, name, format, byte_order);
17676 else
17677 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17678
17679 return type;
17680 }
17681
17682 /* Allocate an integer type of size BITS and name NAME. */
17683
17684 static struct type *
17685 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17686 int bits, int unsigned_p, const char *name)
17687 {
17688 struct type *type;
17689
17690 /* Versions of Intel's C Compiler generate an integer type called "void"
17691 instead of using DW_TAG_unspecified_type. This has been seen on
17692 at least versions 14, 17, and 18. */
17693 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17694 && strcmp (name, "void") == 0)
17695 type = objfile_type (objfile)->builtin_void;
17696 else
17697 type = init_integer_type (objfile, bits, unsigned_p, name);
17698
17699 return type;
17700 }
17701
17702 /* Initialise and return a floating point type of size BITS suitable for
17703 use as a component of a complex number. The NAME_HINT is passed through
17704 when initialising the floating point type and is the name of the complex
17705 type.
17706
17707 As DWARF doesn't currently provide an explicit name for the components
17708 of a complex number, but it can be helpful to have these components
17709 named, we try to select a suitable name based on the size of the
17710 component. */
17711 static struct type *
17712 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17713 struct objfile *objfile,
17714 int bits, const char *name_hint,
17715 enum bfd_endian byte_order)
17716 {
17717 gdbarch *gdbarch = get_objfile_arch (objfile);
17718 struct type *tt = nullptr;
17719
17720 /* Try to find a suitable floating point builtin type of size BITS.
17721 We're going to use the name of this type as the name for the complex
17722 target type that we are about to create. */
17723 switch (cu->language)
17724 {
17725 case language_fortran:
17726 switch (bits)
17727 {
17728 case 32:
17729 tt = builtin_f_type (gdbarch)->builtin_real;
17730 break;
17731 case 64:
17732 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17733 break;
17734 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17735 case 128:
17736 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17737 break;
17738 }
17739 break;
17740 default:
17741 switch (bits)
17742 {
17743 case 32:
17744 tt = builtin_type (gdbarch)->builtin_float;
17745 break;
17746 case 64:
17747 tt = builtin_type (gdbarch)->builtin_double;
17748 break;
17749 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17750 case 128:
17751 tt = builtin_type (gdbarch)->builtin_long_double;
17752 break;
17753 }
17754 break;
17755 }
17756
17757 /* If the type we found doesn't match the size we were looking for, then
17758 pretend we didn't find a type at all, the complex target type we
17759 create will then be nameless. */
17760 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17761 tt = nullptr;
17762
17763 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17764 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17765 }
17766
17767 /* Find a representation of a given base type and install
17768 it in the TYPE field of the die. */
17769
17770 static struct type *
17771 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17772 {
17773 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17774 struct type *type;
17775 struct attribute *attr;
17776 int encoding = 0, bits = 0;
17777 const char *name;
17778 gdbarch *arch;
17779
17780 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17781 if (attr != nullptr)
17782 encoding = DW_UNSND (attr);
17783 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17784 if (attr != nullptr)
17785 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17786 name = dwarf2_name (die, cu);
17787 if (!name)
17788 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17789
17790 arch = get_objfile_arch (objfile);
17791 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17792
17793 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17794 if (attr)
17795 {
17796 int endianity = DW_UNSND (attr);
17797
17798 switch (endianity)
17799 {
17800 case DW_END_big:
17801 byte_order = BFD_ENDIAN_BIG;
17802 break;
17803 case DW_END_little:
17804 byte_order = BFD_ENDIAN_LITTLE;
17805 break;
17806 default:
17807 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17808 break;
17809 }
17810 }
17811
17812 switch (encoding)
17813 {
17814 case DW_ATE_address:
17815 /* Turn DW_ATE_address into a void * pointer. */
17816 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17817 type = init_pointer_type (objfile, bits, name, type);
17818 break;
17819 case DW_ATE_boolean:
17820 type = init_boolean_type (objfile, bits, 1, name);
17821 break;
17822 case DW_ATE_complex_float:
17823 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17824 byte_order);
17825 type = init_complex_type (objfile, name, type);
17826 break;
17827 case DW_ATE_decimal_float:
17828 type = init_decfloat_type (objfile, bits, name);
17829 break;
17830 case DW_ATE_float:
17831 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17832 break;
17833 case DW_ATE_signed:
17834 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17835 break;
17836 case DW_ATE_unsigned:
17837 if (cu->language == language_fortran
17838 && name
17839 && startswith (name, "character("))
17840 type = init_character_type (objfile, bits, 1, name);
17841 else
17842 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17843 break;
17844 case DW_ATE_signed_char:
17845 if (cu->language == language_ada || cu->language == language_m2
17846 || cu->language == language_pascal
17847 || cu->language == language_fortran)
17848 type = init_character_type (objfile, bits, 0, name);
17849 else
17850 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17851 break;
17852 case DW_ATE_unsigned_char:
17853 if (cu->language == language_ada || cu->language == language_m2
17854 || cu->language == language_pascal
17855 || cu->language == language_fortran
17856 || cu->language == language_rust)
17857 type = init_character_type (objfile, bits, 1, name);
17858 else
17859 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17860 break;
17861 case DW_ATE_UTF:
17862 {
17863 if (bits == 16)
17864 type = builtin_type (arch)->builtin_char16;
17865 else if (bits == 32)
17866 type = builtin_type (arch)->builtin_char32;
17867 else
17868 {
17869 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17870 bits);
17871 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17872 }
17873 return set_die_type (die, type, cu);
17874 }
17875 break;
17876
17877 default:
17878 complaint (_("unsupported DW_AT_encoding: '%s'"),
17879 dwarf_type_encoding_name (encoding));
17880 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17881 break;
17882 }
17883
17884 if (name && strcmp (name, "char") == 0)
17885 TYPE_NOSIGN (type) = 1;
17886
17887 maybe_set_alignment (cu, die, type);
17888
17889 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17890
17891 return set_die_type (die, type, cu);
17892 }
17893
17894 /* Parse dwarf attribute if it's a block, reference or constant and put the
17895 resulting value of the attribute into struct bound_prop.
17896 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17897
17898 static int
17899 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17900 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17901 struct type *default_type)
17902 {
17903 struct dwarf2_property_baton *baton;
17904 struct obstack *obstack
17905 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17906
17907 gdb_assert (default_type != NULL);
17908
17909 if (attr == NULL || prop == NULL)
17910 return 0;
17911
17912 if (attr_form_is_block (attr))
17913 {
17914 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17915 baton->property_type = default_type;
17916 baton->locexpr.per_cu = cu->per_cu;
17917 baton->locexpr.size = DW_BLOCK (attr)->size;
17918 baton->locexpr.data = DW_BLOCK (attr)->data;
17919 switch (attr->name)
17920 {
17921 case DW_AT_string_length:
17922 baton->locexpr.is_reference = true;
17923 break;
17924 default:
17925 baton->locexpr.is_reference = false;
17926 break;
17927 }
17928 prop->data.baton = baton;
17929 prop->kind = PROP_LOCEXPR;
17930 gdb_assert (prop->data.baton != NULL);
17931 }
17932 else if (attr_form_is_ref (attr))
17933 {
17934 struct dwarf2_cu *target_cu = cu;
17935 struct die_info *target_die;
17936 struct attribute *target_attr;
17937
17938 target_die = follow_die_ref (die, attr, &target_cu);
17939 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17940 if (target_attr == NULL)
17941 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17942 target_cu);
17943 if (target_attr == NULL)
17944 return 0;
17945
17946 switch (target_attr->name)
17947 {
17948 case DW_AT_location:
17949 if (attr_form_is_section_offset (target_attr))
17950 {
17951 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17952 baton->property_type = die_type (target_die, target_cu);
17953 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17954 prop->data.baton = baton;
17955 prop->kind = PROP_LOCLIST;
17956 gdb_assert (prop->data.baton != NULL);
17957 }
17958 else if (attr_form_is_block (target_attr))
17959 {
17960 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17961 baton->property_type = die_type (target_die, target_cu);
17962 baton->locexpr.per_cu = cu->per_cu;
17963 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17964 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17965 baton->locexpr.is_reference = true;
17966 prop->data.baton = baton;
17967 prop->kind = PROP_LOCEXPR;
17968 gdb_assert (prop->data.baton != NULL);
17969 }
17970 else
17971 {
17972 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17973 "dynamic property");
17974 return 0;
17975 }
17976 break;
17977 case DW_AT_data_member_location:
17978 {
17979 LONGEST offset;
17980
17981 if (!handle_data_member_location (target_die, target_cu,
17982 &offset))
17983 return 0;
17984
17985 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17986 baton->property_type = read_type_die (target_die->parent,
17987 target_cu);
17988 baton->offset_info.offset = offset;
17989 baton->offset_info.type = die_type (target_die, target_cu);
17990 prop->data.baton = baton;
17991 prop->kind = PROP_ADDR_OFFSET;
17992 break;
17993 }
17994 }
17995 }
17996 else if (attr_form_is_constant (attr))
17997 {
17998 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17999 prop->kind = PROP_CONST;
18000 }
18001 else
18002 {
18003 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18004 dwarf2_name (die, cu));
18005 return 0;
18006 }
18007
18008 return 1;
18009 }
18010
18011 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
18012 UNSIGNED_P controls if the integer is unsigned or not. */
18013
18014 static struct type *
18015 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
18016 int size_in_bytes, bool unsigned_p)
18017 {
18018 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
18019 struct type *int_type;
18020
18021 /* Helper macro to examine the various builtin types. */
18022 #define TRY_TYPE(F) \
18023 int_type = (unsigned_p \
18024 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18025 : objfile_type (objfile)->builtin_ ## F); \
18026 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18027 return int_type
18028
18029 TRY_TYPE (char);
18030 TRY_TYPE (short);
18031 TRY_TYPE (int);
18032 TRY_TYPE (long);
18033 TRY_TYPE (long_long);
18034
18035 #undef TRY_TYPE
18036
18037 gdb_assert_not_reached ("unable to find suitable integer type");
18038 }
18039
18040 /* Find an integer type the same size as the address size given in the
18041 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18042 is unsigned or not. */
18043
18044 static struct type *
18045 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18046 bool unsigned_p)
18047 {
18048 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18049 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18050 }
18051
18052 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18053 present (which is valid) then compute the default type based on the
18054 compilation units address size. */
18055
18056 static struct type *
18057 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18058 {
18059 struct type *index_type = die_type (die, cu);
18060
18061 /* Dwarf-2 specifications explicitly allows to create subrange types
18062 without specifying a base type.
18063 In that case, the base type must be set to the type of
18064 the lower bound, upper bound or count, in that order, if any of these
18065 three attributes references an object that has a type.
18066 If no base type is found, the Dwarf-2 specifications say that
18067 a signed integer type of size equal to the size of an address should
18068 be used.
18069 For the following C code: `extern char gdb_int [];'
18070 GCC produces an empty range DIE.
18071 FIXME: muller/2010-05-28: Possible references to object for low bound,
18072 high bound or count are not yet handled by this code. */
18073 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
18074 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18075
18076 return index_type;
18077 }
18078
18079 /* Read the given DW_AT_subrange DIE. */
18080
18081 static struct type *
18082 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18083 {
18084 struct type *base_type, *orig_base_type;
18085 struct type *range_type;
18086 struct attribute *attr;
18087 struct dynamic_prop low, high;
18088 int low_default_is_valid;
18089 int high_bound_is_count = 0;
18090 const char *name;
18091 ULONGEST negative_mask;
18092
18093 orig_base_type = read_subrange_index_type (die, cu);
18094
18095 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18096 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18097 creating the range type, but we use the result of check_typedef
18098 when examining properties of the type. */
18099 base_type = check_typedef (orig_base_type);
18100
18101 /* The die_type call above may have already set the type for this DIE. */
18102 range_type = get_die_type (die, cu);
18103 if (range_type)
18104 return range_type;
18105
18106 low.kind = PROP_CONST;
18107 high.kind = PROP_CONST;
18108 high.data.const_val = 0;
18109
18110 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18111 omitting DW_AT_lower_bound. */
18112 switch (cu->language)
18113 {
18114 case language_c:
18115 case language_cplus:
18116 low.data.const_val = 0;
18117 low_default_is_valid = 1;
18118 break;
18119 case language_fortran:
18120 low.data.const_val = 1;
18121 low_default_is_valid = 1;
18122 break;
18123 case language_d:
18124 case language_objc:
18125 case language_rust:
18126 low.data.const_val = 0;
18127 low_default_is_valid = (cu->header.version >= 4);
18128 break;
18129 case language_ada:
18130 case language_m2:
18131 case language_pascal:
18132 low.data.const_val = 1;
18133 low_default_is_valid = (cu->header.version >= 4);
18134 break;
18135 default:
18136 low.data.const_val = 0;
18137 low_default_is_valid = 0;
18138 break;
18139 }
18140
18141 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18142 if (attr != nullptr)
18143 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18144 else if (!low_default_is_valid)
18145 complaint (_("Missing DW_AT_lower_bound "
18146 "- DIE at %s [in module %s]"),
18147 sect_offset_str (die->sect_off),
18148 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18149
18150 struct attribute *attr_ub, *attr_count;
18151 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18152 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18153 {
18154 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18155 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18156 {
18157 /* If bounds are constant do the final calculation here. */
18158 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18159 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18160 else
18161 high_bound_is_count = 1;
18162 }
18163 else
18164 {
18165 if (attr_ub != NULL)
18166 complaint (_("Unresolved DW_AT_upper_bound "
18167 "- DIE at %s [in module %s]"),
18168 sect_offset_str (die->sect_off),
18169 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18170 if (attr_count != NULL)
18171 complaint (_("Unresolved DW_AT_count "
18172 "- DIE at %s [in module %s]"),
18173 sect_offset_str (die->sect_off),
18174 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18175 }
18176 }
18177
18178 LONGEST bias = 0;
18179 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18180 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18181 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18182
18183 /* Normally, the DWARF producers are expected to use a signed
18184 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18185 But this is unfortunately not always the case, as witnessed
18186 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18187 is used instead. To work around that ambiguity, we treat
18188 the bounds as signed, and thus sign-extend their values, when
18189 the base type is signed. */
18190 negative_mask =
18191 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18192 if (low.kind == PROP_CONST
18193 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18194 low.data.const_val |= negative_mask;
18195 if (high.kind == PROP_CONST
18196 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18197 high.data.const_val |= negative_mask;
18198
18199 /* Check for bit and byte strides. */
18200 struct dynamic_prop byte_stride_prop;
18201 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18202 if (attr_byte_stride != nullptr)
18203 {
18204 struct type *prop_type
18205 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18206 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18207 prop_type);
18208 }
18209
18210 struct dynamic_prop bit_stride_prop;
18211 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18212 if (attr_bit_stride != nullptr)
18213 {
18214 /* It only makes sense to have either a bit or byte stride. */
18215 if (attr_byte_stride != nullptr)
18216 {
18217 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18218 "- DIE at %s [in module %s]"),
18219 sect_offset_str (die->sect_off),
18220 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18221 attr_bit_stride = nullptr;
18222 }
18223 else
18224 {
18225 struct type *prop_type
18226 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18227 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18228 prop_type);
18229 }
18230 }
18231
18232 if (attr_byte_stride != nullptr
18233 || attr_bit_stride != nullptr)
18234 {
18235 bool byte_stride_p = (attr_byte_stride != nullptr);
18236 struct dynamic_prop *stride
18237 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18238
18239 range_type
18240 = create_range_type_with_stride (NULL, orig_base_type, &low,
18241 &high, bias, stride, byte_stride_p);
18242 }
18243 else
18244 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18245
18246 if (high_bound_is_count)
18247 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18248
18249 /* Ada expects an empty array on no boundary attributes. */
18250 if (attr == NULL && cu->language != language_ada)
18251 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18252
18253 name = dwarf2_name (die, cu);
18254 if (name)
18255 TYPE_NAME (range_type) = name;
18256
18257 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18258 if (attr != nullptr)
18259 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18260
18261 maybe_set_alignment (cu, die, range_type);
18262
18263 set_die_type (die, range_type, cu);
18264
18265 /* set_die_type should be already done. */
18266 set_descriptive_type (range_type, die, cu);
18267
18268 return range_type;
18269 }
18270
18271 static struct type *
18272 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18273 {
18274 struct type *type;
18275
18276 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18277 NULL);
18278 TYPE_NAME (type) = dwarf2_name (die, cu);
18279
18280 /* In Ada, an unspecified type is typically used when the description
18281 of the type is deferred to a different unit. When encountering
18282 such a type, we treat it as a stub, and try to resolve it later on,
18283 when needed. */
18284 if (cu->language == language_ada)
18285 TYPE_STUB (type) = 1;
18286
18287 return set_die_type (die, type, cu);
18288 }
18289
18290 /* Read a single die and all its descendents. Set the die's sibling
18291 field to NULL; set other fields in the die correctly, and set all
18292 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18293 location of the info_ptr after reading all of those dies. PARENT
18294 is the parent of the die in question. */
18295
18296 static struct die_info *
18297 read_die_and_children (const struct die_reader_specs *reader,
18298 const gdb_byte *info_ptr,
18299 const gdb_byte **new_info_ptr,
18300 struct die_info *parent)
18301 {
18302 struct die_info *die;
18303 const gdb_byte *cur_ptr;
18304 int has_children;
18305
18306 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18307 if (die == NULL)
18308 {
18309 *new_info_ptr = cur_ptr;
18310 return NULL;
18311 }
18312 store_in_ref_table (die, reader->cu);
18313
18314 if (has_children)
18315 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18316 else
18317 {
18318 die->child = NULL;
18319 *new_info_ptr = cur_ptr;
18320 }
18321
18322 die->sibling = NULL;
18323 die->parent = parent;
18324 return die;
18325 }
18326
18327 /* Read a die, all of its descendents, and all of its siblings; set
18328 all of the fields of all of the dies correctly. Arguments are as
18329 in read_die_and_children. */
18330
18331 static struct die_info *
18332 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18333 const gdb_byte *info_ptr,
18334 const gdb_byte **new_info_ptr,
18335 struct die_info *parent)
18336 {
18337 struct die_info *first_die, *last_sibling;
18338 const gdb_byte *cur_ptr;
18339
18340 cur_ptr = info_ptr;
18341 first_die = last_sibling = NULL;
18342
18343 while (1)
18344 {
18345 struct die_info *die
18346 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18347
18348 if (die == NULL)
18349 {
18350 *new_info_ptr = cur_ptr;
18351 return first_die;
18352 }
18353
18354 if (!first_die)
18355 first_die = die;
18356 else
18357 last_sibling->sibling = die;
18358
18359 last_sibling = die;
18360 }
18361 }
18362
18363 /* Read a die, all of its descendents, and all of its siblings; set
18364 all of the fields of all of the dies correctly. Arguments are as
18365 in read_die_and_children.
18366 This the main entry point for reading a DIE and all its children. */
18367
18368 static struct die_info *
18369 read_die_and_siblings (const struct die_reader_specs *reader,
18370 const gdb_byte *info_ptr,
18371 const gdb_byte **new_info_ptr,
18372 struct die_info *parent)
18373 {
18374 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18375 new_info_ptr, parent);
18376
18377 if (dwarf_die_debug)
18378 {
18379 fprintf_unfiltered (gdb_stdlog,
18380 "Read die from %s@0x%x of %s:\n",
18381 get_section_name (reader->die_section),
18382 (unsigned) (info_ptr - reader->die_section->buffer),
18383 bfd_get_filename (reader->abfd));
18384 dump_die (die, dwarf_die_debug);
18385 }
18386
18387 return die;
18388 }
18389
18390 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18391 attributes.
18392 The caller is responsible for filling in the extra attributes
18393 and updating (*DIEP)->num_attrs.
18394 Set DIEP to point to a newly allocated die with its information,
18395 except for its child, sibling, and parent fields.
18396 Set HAS_CHILDREN to tell whether the die has children or not. */
18397
18398 static const gdb_byte *
18399 read_full_die_1 (const struct die_reader_specs *reader,
18400 struct die_info **diep, const gdb_byte *info_ptr,
18401 int *has_children, int num_extra_attrs)
18402 {
18403 unsigned int abbrev_number, bytes_read, i;
18404 struct abbrev_info *abbrev;
18405 struct die_info *die;
18406 struct dwarf2_cu *cu = reader->cu;
18407 bfd *abfd = reader->abfd;
18408
18409 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18410 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18411 info_ptr += bytes_read;
18412 if (!abbrev_number)
18413 {
18414 *diep = NULL;
18415 *has_children = 0;
18416 return info_ptr;
18417 }
18418
18419 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18420 if (!abbrev)
18421 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18422 abbrev_number,
18423 bfd_get_filename (abfd));
18424
18425 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18426 die->sect_off = sect_off;
18427 die->tag = abbrev->tag;
18428 die->abbrev = abbrev_number;
18429
18430 /* Make the result usable.
18431 The caller needs to update num_attrs after adding the extra
18432 attributes. */
18433 die->num_attrs = abbrev->num_attrs;
18434
18435 std::vector<int> indexes_that_need_reprocess;
18436 for (i = 0; i < abbrev->num_attrs; ++i)
18437 {
18438 bool need_reprocess;
18439 info_ptr =
18440 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18441 info_ptr, &need_reprocess);
18442 if (need_reprocess)
18443 indexes_that_need_reprocess.push_back (i);
18444 }
18445
18446 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18447 if (attr != nullptr)
18448 cu->str_offsets_base = DW_UNSND (attr);
18449
18450 auto maybe_addr_base = lookup_addr_base(die);
18451 if (maybe_addr_base.has_value ())
18452 cu->addr_base = *maybe_addr_base;
18453 for (int index : indexes_that_need_reprocess)
18454 read_attribute_reprocess (reader, &die->attrs[index]);
18455 *diep = die;
18456 *has_children = abbrev->has_children;
18457 return info_ptr;
18458 }
18459
18460 /* Read a die and all its attributes.
18461 Set DIEP to point to a newly allocated die with its information,
18462 except for its child, sibling, and parent fields.
18463 Set HAS_CHILDREN to tell whether the die has children or not. */
18464
18465 static const gdb_byte *
18466 read_full_die (const struct die_reader_specs *reader,
18467 struct die_info **diep, const gdb_byte *info_ptr,
18468 int *has_children)
18469 {
18470 const gdb_byte *result;
18471
18472 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18473
18474 if (dwarf_die_debug)
18475 {
18476 fprintf_unfiltered (gdb_stdlog,
18477 "Read die from %s@0x%x of %s:\n",
18478 get_section_name (reader->die_section),
18479 (unsigned) (info_ptr - reader->die_section->buffer),
18480 bfd_get_filename (reader->abfd));
18481 dump_die (*diep, dwarf_die_debug);
18482 }
18483
18484 return result;
18485 }
18486 \f
18487 /* Abbreviation tables.
18488
18489 In DWARF version 2, the description of the debugging information is
18490 stored in a separate .debug_abbrev section. Before we read any
18491 dies from a section we read in all abbreviations and install them
18492 in a hash table. */
18493
18494 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18495
18496 struct abbrev_info *
18497 abbrev_table::alloc_abbrev ()
18498 {
18499 struct abbrev_info *abbrev;
18500
18501 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18502 memset (abbrev, 0, sizeof (struct abbrev_info));
18503
18504 return abbrev;
18505 }
18506
18507 /* Add an abbreviation to the table. */
18508
18509 void
18510 abbrev_table::add_abbrev (unsigned int abbrev_number,
18511 struct abbrev_info *abbrev)
18512 {
18513 unsigned int hash_number;
18514
18515 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18516 abbrev->next = m_abbrevs[hash_number];
18517 m_abbrevs[hash_number] = abbrev;
18518 }
18519
18520 /* Look up an abbrev in the table.
18521 Returns NULL if the abbrev is not found. */
18522
18523 struct abbrev_info *
18524 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18525 {
18526 unsigned int hash_number;
18527 struct abbrev_info *abbrev;
18528
18529 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18530 abbrev = m_abbrevs[hash_number];
18531
18532 while (abbrev)
18533 {
18534 if (abbrev->number == abbrev_number)
18535 return abbrev;
18536 abbrev = abbrev->next;
18537 }
18538 return NULL;
18539 }
18540
18541 /* Read in an abbrev table. */
18542
18543 static abbrev_table_up
18544 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18545 struct dwarf2_section_info *section,
18546 sect_offset sect_off)
18547 {
18548 struct objfile *objfile = dwarf2_per_objfile->objfile;
18549 bfd *abfd = get_section_bfd_owner (section);
18550 const gdb_byte *abbrev_ptr;
18551 struct abbrev_info *cur_abbrev;
18552 unsigned int abbrev_number, bytes_read, abbrev_name;
18553 unsigned int abbrev_form;
18554 std::vector<struct attr_abbrev> cur_attrs;
18555
18556 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18557
18558 dwarf2_read_section (objfile, section);
18559 abbrev_ptr = section->buffer + to_underlying (sect_off);
18560 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18561 abbrev_ptr += bytes_read;
18562
18563 /* Loop until we reach an abbrev number of 0. */
18564 while (abbrev_number)
18565 {
18566 cur_attrs.clear ();
18567 cur_abbrev = abbrev_table->alloc_abbrev ();
18568
18569 /* read in abbrev header */
18570 cur_abbrev->number = abbrev_number;
18571 cur_abbrev->tag
18572 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18573 abbrev_ptr += bytes_read;
18574 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18575 abbrev_ptr += 1;
18576
18577 /* now read in declarations */
18578 for (;;)
18579 {
18580 LONGEST implicit_const;
18581
18582 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18583 abbrev_ptr += bytes_read;
18584 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18585 abbrev_ptr += bytes_read;
18586 if (abbrev_form == DW_FORM_implicit_const)
18587 {
18588 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18589 &bytes_read);
18590 abbrev_ptr += bytes_read;
18591 }
18592 else
18593 {
18594 /* Initialize it due to a false compiler warning. */
18595 implicit_const = -1;
18596 }
18597
18598 if (abbrev_name == 0)
18599 break;
18600
18601 cur_attrs.emplace_back ();
18602 struct attr_abbrev &cur_attr = cur_attrs.back ();
18603 cur_attr.name = (enum dwarf_attribute) abbrev_name;
18604 cur_attr.form = (enum dwarf_form) abbrev_form;
18605 cur_attr.implicit_const = implicit_const;
18606 ++cur_abbrev->num_attrs;
18607 }
18608
18609 cur_abbrev->attrs =
18610 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18611 cur_abbrev->num_attrs);
18612 memcpy (cur_abbrev->attrs, cur_attrs.data (),
18613 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18614
18615 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18616
18617 /* Get next abbreviation.
18618 Under Irix6 the abbreviations for a compilation unit are not
18619 always properly terminated with an abbrev number of 0.
18620 Exit loop if we encounter an abbreviation which we have
18621 already read (which means we are about to read the abbreviations
18622 for the next compile unit) or if the end of the abbreviation
18623 table is reached. */
18624 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18625 break;
18626 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18627 abbrev_ptr += bytes_read;
18628 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18629 break;
18630 }
18631
18632 return abbrev_table;
18633 }
18634
18635 /* Returns nonzero if TAG represents a type that we might generate a partial
18636 symbol for. */
18637
18638 static int
18639 is_type_tag_for_partial (int tag)
18640 {
18641 switch (tag)
18642 {
18643 #if 0
18644 /* Some types that would be reasonable to generate partial symbols for,
18645 that we don't at present. */
18646 case DW_TAG_array_type:
18647 case DW_TAG_file_type:
18648 case DW_TAG_ptr_to_member_type:
18649 case DW_TAG_set_type:
18650 case DW_TAG_string_type:
18651 case DW_TAG_subroutine_type:
18652 #endif
18653 case DW_TAG_base_type:
18654 case DW_TAG_class_type:
18655 case DW_TAG_interface_type:
18656 case DW_TAG_enumeration_type:
18657 case DW_TAG_structure_type:
18658 case DW_TAG_subrange_type:
18659 case DW_TAG_typedef:
18660 case DW_TAG_union_type:
18661 return 1;
18662 default:
18663 return 0;
18664 }
18665 }
18666
18667 /* Load all DIEs that are interesting for partial symbols into memory. */
18668
18669 static struct partial_die_info *
18670 load_partial_dies (const struct die_reader_specs *reader,
18671 const gdb_byte *info_ptr, int building_psymtab)
18672 {
18673 struct dwarf2_cu *cu = reader->cu;
18674 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18675 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18676 unsigned int bytes_read;
18677 unsigned int load_all = 0;
18678 int nesting_level = 1;
18679
18680 parent_die = NULL;
18681 last_die = NULL;
18682
18683 gdb_assert (cu->per_cu != NULL);
18684 if (cu->per_cu->load_all_dies)
18685 load_all = 1;
18686
18687 cu->partial_dies
18688 = htab_create_alloc_ex (cu->header.length / 12,
18689 partial_die_hash,
18690 partial_die_eq,
18691 NULL,
18692 &cu->comp_unit_obstack,
18693 hashtab_obstack_allocate,
18694 dummy_obstack_deallocate);
18695
18696 while (1)
18697 {
18698 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18699
18700 /* A NULL abbrev means the end of a series of children. */
18701 if (abbrev == NULL)
18702 {
18703 if (--nesting_level == 0)
18704 return first_die;
18705
18706 info_ptr += bytes_read;
18707 last_die = parent_die;
18708 parent_die = parent_die->die_parent;
18709 continue;
18710 }
18711
18712 /* Check for template arguments. We never save these; if
18713 they're seen, we just mark the parent, and go on our way. */
18714 if (parent_die != NULL
18715 && cu->language == language_cplus
18716 && (abbrev->tag == DW_TAG_template_type_param
18717 || abbrev->tag == DW_TAG_template_value_param))
18718 {
18719 parent_die->has_template_arguments = 1;
18720
18721 if (!load_all)
18722 {
18723 /* We don't need a partial DIE for the template argument. */
18724 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18725 continue;
18726 }
18727 }
18728
18729 /* We only recurse into c++ subprograms looking for template arguments.
18730 Skip their other children. */
18731 if (!load_all
18732 && cu->language == language_cplus
18733 && parent_die != NULL
18734 && parent_die->tag == DW_TAG_subprogram)
18735 {
18736 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18737 continue;
18738 }
18739
18740 /* Check whether this DIE is interesting enough to save. Normally
18741 we would not be interested in members here, but there may be
18742 later variables referencing them via DW_AT_specification (for
18743 static members). */
18744 if (!load_all
18745 && !is_type_tag_for_partial (abbrev->tag)
18746 && abbrev->tag != DW_TAG_constant
18747 && abbrev->tag != DW_TAG_enumerator
18748 && abbrev->tag != DW_TAG_subprogram
18749 && abbrev->tag != DW_TAG_inlined_subroutine
18750 && abbrev->tag != DW_TAG_lexical_block
18751 && abbrev->tag != DW_TAG_variable
18752 && abbrev->tag != DW_TAG_namespace
18753 && abbrev->tag != DW_TAG_module
18754 && abbrev->tag != DW_TAG_member
18755 && abbrev->tag != DW_TAG_imported_unit
18756 && abbrev->tag != DW_TAG_imported_declaration)
18757 {
18758 /* Otherwise we skip to the next sibling, if any. */
18759 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18760 continue;
18761 }
18762
18763 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18764 abbrev);
18765
18766 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18767
18768 /* This two-pass algorithm for processing partial symbols has a
18769 high cost in cache pressure. Thus, handle some simple cases
18770 here which cover the majority of C partial symbols. DIEs
18771 which neither have specification tags in them, nor could have
18772 specification tags elsewhere pointing at them, can simply be
18773 processed and discarded.
18774
18775 This segment is also optional; scan_partial_symbols and
18776 add_partial_symbol will handle these DIEs if we chain
18777 them in normally. When compilers which do not emit large
18778 quantities of duplicate debug information are more common,
18779 this code can probably be removed. */
18780
18781 /* Any complete simple types at the top level (pretty much all
18782 of them, for a language without namespaces), can be processed
18783 directly. */
18784 if (parent_die == NULL
18785 && pdi.has_specification == 0
18786 && pdi.is_declaration == 0
18787 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18788 || pdi.tag == DW_TAG_base_type
18789 || pdi.tag == DW_TAG_subrange_type))
18790 {
18791 if (building_psymtab && pdi.name != NULL)
18792 add_psymbol_to_list (pdi.name, false,
18793 VAR_DOMAIN, LOC_TYPEDEF, -1,
18794 psymbol_placement::STATIC,
18795 0, cu->language, objfile);
18796 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18797 continue;
18798 }
18799
18800 /* The exception for DW_TAG_typedef with has_children above is
18801 a workaround of GCC PR debug/47510. In the case of this complaint
18802 type_name_or_error will error on such types later.
18803
18804 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18805 it could not find the child DIEs referenced later, this is checked
18806 above. In correct DWARF DW_TAG_typedef should have no children. */
18807
18808 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18809 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18810 "- DIE at %s [in module %s]"),
18811 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18812
18813 /* If we're at the second level, and we're an enumerator, and
18814 our parent has no specification (meaning possibly lives in a
18815 namespace elsewhere), then we can add the partial symbol now
18816 instead of queueing it. */
18817 if (pdi.tag == DW_TAG_enumerator
18818 && parent_die != NULL
18819 && parent_die->die_parent == NULL
18820 && parent_die->tag == DW_TAG_enumeration_type
18821 && parent_die->has_specification == 0)
18822 {
18823 if (pdi.name == NULL)
18824 complaint (_("malformed enumerator DIE ignored"));
18825 else if (building_psymtab)
18826 add_psymbol_to_list (pdi.name, false,
18827 VAR_DOMAIN, LOC_CONST, -1,
18828 cu->language == language_cplus
18829 ? psymbol_placement::GLOBAL
18830 : psymbol_placement::STATIC,
18831 0, cu->language, objfile);
18832
18833 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18834 continue;
18835 }
18836
18837 struct partial_die_info *part_die
18838 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18839
18840 /* We'll save this DIE so link it in. */
18841 part_die->die_parent = parent_die;
18842 part_die->die_sibling = NULL;
18843 part_die->die_child = NULL;
18844
18845 if (last_die && last_die == parent_die)
18846 last_die->die_child = part_die;
18847 else if (last_die)
18848 last_die->die_sibling = part_die;
18849
18850 last_die = part_die;
18851
18852 if (first_die == NULL)
18853 first_die = part_die;
18854
18855 /* Maybe add the DIE to the hash table. Not all DIEs that we
18856 find interesting need to be in the hash table, because we
18857 also have the parent/sibling/child chains; only those that we
18858 might refer to by offset later during partial symbol reading.
18859
18860 For now this means things that might have be the target of a
18861 DW_AT_specification, DW_AT_abstract_origin, or
18862 DW_AT_extension. DW_AT_extension will refer only to
18863 namespaces; DW_AT_abstract_origin refers to functions (and
18864 many things under the function DIE, but we do not recurse
18865 into function DIEs during partial symbol reading) and
18866 possibly variables as well; DW_AT_specification refers to
18867 declarations. Declarations ought to have the DW_AT_declaration
18868 flag. It happens that GCC forgets to put it in sometimes, but
18869 only for functions, not for types.
18870
18871 Adding more things than necessary to the hash table is harmless
18872 except for the performance cost. Adding too few will result in
18873 wasted time in find_partial_die, when we reread the compilation
18874 unit with load_all_dies set. */
18875
18876 if (load_all
18877 || abbrev->tag == DW_TAG_constant
18878 || abbrev->tag == DW_TAG_subprogram
18879 || abbrev->tag == DW_TAG_variable
18880 || abbrev->tag == DW_TAG_namespace
18881 || part_die->is_declaration)
18882 {
18883 void **slot;
18884
18885 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18886 to_underlying (part_die->sect_off),
18887 INSERT);
18888 *slot = part_die;
18889 }
18890
18891 /* For some DIEs we want to follow their children (if any). For C
18892 we have no reason to follow the children of structures; for other
18893 languages we have to, so that we can get at method physnames
18894 to infer fully qualified class names, for DW_AT_specification,
18895 and for C++ template arguments. For C++, we also look one level
18896 inside functions to find template arguments (if the name of the
18897 function does not already contain the template arguments).
18898
18899 For Ada and Fortran, we need to scan the children of subprograms
18900 and lexical blocks as well because these languages allow the
18901 definition of nested entities that could be interesting for the
18902 debugger, such as nested subprograms for instance. */
18903 if (last_die->has_children
18904 && (load_all
18905 || last_die->tag == DW_TAG_namespace
18906 || last_die->tag == DW_TAG_module
18907 || last_die->tag == DW_TAG_enumeration_type
18908 || (cu->language == language_cplus
18909 && last_die->tag == DW_TAG_subprogram
18910 && (last_die->name == NULL
18911 || strchr (last_die->name, '<') == NULL))
18912 || (cu->language != language_c
18913 && (last_die->tag == DW_TAG_class_type
18914 || last_die->tag == DW_TAG_interface_type
18915 || last_die->tag == DW_TAG_structure_type
18916 || last_die->tag == DW_TAG_union_type))
18917 || ((cu->language == language_ada
18918 || cu->language == language_fortran)
18919 && (last_die->tag == DW_TAG_subprogram
18920 || last_die->tag == DW_TAG_lexical_block))))
18921 {
18922 nesting_level++;
18923 parent_die = last_die;
18924 continue;
18925 }
18926
18927 /* Otherwise we skip to the next sibling, if any. */
18928 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18929
18930 /* Back to the top, do it again. */
18931 }
18932 }
18933
18934 partial_die_info::partial_die_info (sect_offset sect_off_,
18935 struct abbrev_info *abbrev)
18936 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18937 {
18938 }
18939
18940 /* Read a minimal amount of information into the minimal die structure.
18941 INFO_PTR should point just after the initial uleb128 of a DIE. */
18942
18943 const gdb_byte *
18944 partial_die_info::read (const struct die_reader_specs *reader,
18945 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18946 {
18947 struct dwarf2_cu *cu = reader->cu;
18948 struct dwarf2_per_objfile *dwarf2_per_objfile
18949 = cu->per_cu->dwarf2_per_objfile;
18950 unsigned int i;
18951 int has_low_pc_attr = 0;
18952 int has_high_pc_attr = 0;
18953 int high_pc_relative = 0;
18954
18955 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
18956 for (i = 0; i < abbrev.num_attrs; ++i)
18957 {
18958 bool need_reprocess;
18959 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18960 info_ptr, &need_reprocess);
18961 /* String and address offsets that need to do the reprocessing have
18962 already been read at this point, so there is no need to wait until
18963 the loop terminates to do the reprocessing. */
18964 if (need_reprocess)
18965 read_attribute_reprocess (reader, &attr_vec[i]);
18966 attribute &attr = attr_vec[i];
18967 /* Store the data if it is of an attribute we want to keep in a
18968 partial symbol table. */
18969 switch (attr.name)
18970 {
18971 case DW_AT_name:
18972 switch (tag)
18973 {
18974 case DW_TAG_compile_unit:
18975 case DW_TAG_partial_unit:
18976 case DW_TAG_type_unit:
18977 /* Compilation units have a DW_AT_name that is a filename, not
18978 a source language identifier. */
18979 case DW_TAG_enumeration_type:
18980 case DW_TAG_enumerator:
18981 /* These tags always have simple identifiers already; no need
18982 to canonicalize them. */
18983 name = DW_STRING (&attr);
18984 break;
18985 default:
18986 {
18987 struct objfile *objfile = dwarf2_per_objfile->objfile;
18988
18989 name
18990 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18991 &objfile->per_bfd->storage_obstack);
18992 }
18993 break;
18994 }
18995 break;
18996 case DW_AT_linkage_name:
18997 case DW_AT_MIPS_linkage_name:
18998 /* Note that both forms of linkage name might appear. We
18999 assume they will be the same, and we only store the last
19000 one we see. */
19001 linkage_name = DW_STRING (&attr);
19002 break;
19003 case DW_AT_low_pc:
19004 has_low_pc_attr = 1;
19005 lowpc = attr_value_as_address (&attr);
19006 break;
19007 case DW_AT_high_pc:
19008 has_high_pc_attr = 1;
19009 highpc = attr_value_as_address (&attr);
19010 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19011 high_pc_relative = 1;
19012 break;
19013 case DW_AT_location:
19014 /* Support the .debug_loc offsets. */
19015 if (attr_form_is_block (&attr))
19016 {
19017 d.locdesc = DW_BLOCK (&attr);
19018 }
19019 else if (attr_form_is_section_offset (&attr))
19020 {
19021 dwarf2_complex_location_expr_complaint ();
19022 }
19023 else
19024 {
19025 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19026 "partial symbol information");
19027 }
19028 break;
19029 case DW_AT_external:
19030 is_external = DW_UNSND (&attr);
19031 break;
19032 case DW_AT_declaration:
19033 is_declaration = DW_UNSND (&attr);
19034 break;
19035 case DW_AT_type:
19036 has_type = 1;
19037 break;
19038 case DW_AT_abstract_origin:
19039 case DW_AT_specification:
19040 case DW_AT_extension:
19041 has_specification = 1;
19042 spec_offset = dwarf2_get_ref_die_offset (&attr);
19043 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19044 || cu->per_cu->is_dwz);
19045 break;
19046 case DW_AT_sibling:
19047 /* Ignore absolute siblings, they might point outside of
19048 the current compile unit. */
19049 if (attr.form == DW_FORM_ref_addr)
19050 complaint (_("ignoring absolute DW_AT_sibling"));
19051 else
19052 {
19053 const gdb_byte *buffer = reader->buffer;
19054 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19055 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19056
19057 if (sibling_ptr < info_ptr)
19058 complaint (_("DW_AT_sibling points backwards"));
19059 else if (sibling_ptr > reader->buffer_end)
19060 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19061 else
19062 sibling = sibling_ptr;
19063 }
19064 break;
19065 case DW_AT_byte_size:
19066 has_byte_size = 1;
19067 break;
19068 case DW_AT_const_value:
19069 has_const_value = 1;
19070 break;
19071 case DW_AT_calling_convention:
19072 /* DWARF doesn't provide a way to identify a program's source-level
19073 entry point. DW_AT_calling_convention attributes are only meant
19074 to describe functions' calling conventions.
19075
19076 However, because it's a necessary piece of information in
19077 Fortran, and before DWARF 4 DW_CC_program was the only
19078 piece of debugging information whose definition refers to
19079 a 'main program' at all, several compilers marked Fortran
19080 main programs with DW_CC_program --- even when those
19081 functions use the standard calling conventions.
19082
19083 Although DWARF now specifies a way to provide this
19084 information, we support this practice for backward
19085 compatibility. */
19086 if (DW_UNSND (&attr) == DW_CC_program
19087 && cu->language == language_fortran)
19088 main_subprogram = 1;
19089 break;
19090 case DW_AT_inline:
19091 if (DW_UNSND (&attr) == DW_INL_inlined
19092 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19093 may_be_inlined = 1;
19094 break;
19095
19096 case DW_AT_import:
19097 if (tag == DW_TAG_imported_unit)
19098 {
19099 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19100 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19101 || cu->per_cu->is_dwz);
19102 }
19103 break;
19104
19105 case DW_AT_main_subprogram:
19106 main_subprogram = DW_UNSND (&attr);
19107 break;
19108
19109 case DW_AT_ranges:
19110 {
19111 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19112 but that requires a full DIE, so instead we just
19113 reimplement it. */
19114 int need_ranges_base = tag != DW_TAG_compile_unit;
19115 unsigned int ranges_offset = (DW_UNSND (&attr)
19116 + (need_ranges_base
19117 ? cu->ranges_base
19118 : 0));
19119
19120 /* Value of the DW_AT_ranges attribute is the offset in the
19121 .debug_ranges section. */
19122 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19123 nullptr))
19124 has_pc_info = 1;
19125 }
19126 break;
19127
19128 default:
19129 break;
19130 }
19131 }
19132
19133 /* For Ada, if both the name and the linkage name appear, we prefer
19134 the latter. This lets "catch exception" work better, regardless
19135 of the order in which the name and linkage name were emitted.
19136 Really, though, this is just a workaround for the fact that gdb
19137 doesn't store both the name and the linkage name. */
19138 if (cu->language == language_ada && linkage_name != nullptr)
19139 name = linkage_name;
19140
19141 if (high_pc_relative)
19142 highpc += lowpc;
19143
19144 if (has_low_pc_attr && has_high_pc_attr)
19145 {
19146 /* When using the GNU linker, .gnu.linkonce. sections are used to
19147 eliminate duplicate copies of functions and vtables and such.
19148 The linker will arbitrarily choose one and discard the others.
19149 The AT_*_pc values for such functions refer to local labels in
19150 these sections. If the section from that file was discarded, the
19151 labels are not in the output, so the relocs get a value of 0.
19152 If this is a discarded function, mark the pc bounds as invalid,
19153 so that GDB will ignore it. */
19154 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19155 {
19156 struct objfile *objfile = dwarf2_per_objfile->objfile;
19157 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19158
19159 complaint (_("DW_AT_low_pc %s is zero "
19160 "for DIE at %s [in module %s]"),
19161 paddress (gdbarch, lowpc),
19162 sect_offset_str (sect_off),
19163 objfile_name (objfile));
19164 }
19165 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19166 else if (lowpc >= highpc)
19167 {
19168 struct objfile *objfile = dwarf2_per_objfile->objfile;
19169 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19170
19171 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19172 "for DIE at %s [in module %s]"),
19173 paddress (gdbarch, lowpc),
19174 paddress (gdbarch, highpc),
19175 sect_offset_str (sect_off),
19176 objfile_name (objfile));
19177 }
19178 else
19179 has_pc_info = 1;
19180 }
19181
19182 return info_ptr;
19183 }
19184
19185 /* Find a cached partial DIE at OFFSET in CU. */
19186
19187 struct partial_die_info *
19188 dwarf2_cu::find_partial_die (sect_offset sect_off)
19189 {
19190 struct partial_die_info *lookup_die = NULL;
19191 struct partial_die_info part_die (sect_off);
19192
19193 lookup_die = ((struct partial_die_info *)
19194 htab_find_with_hash (partial_dies, &part_die,
19195 to_underlying (sect_off)));
19196
19197 return lookup_die;
19198 }
19199
19200 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19201 except in the case of .debug_types DIEs which do not reference
19202 outside their CU (they do however referencing other types via
19203 DW_FORM_ref_sig8). */
19204
19205 static const struct cu_partial_die_info
19206 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19207 {
19208 struct dwarf2_per_objfile *dwarf2_per_objfile
19209 = cu->per_cu->dwarf2_per_objfile;
19210 struct objfile *objfile = dwarf2_per_objfile->objfile;
19211 struct dwarf2_per_cu_data *per_cu = NULL;
19212 struct partial_die_info *pd = NULL;
19213
19214 if (offset_in_dwz == cu->per_cu->is_dwz
19215 && offset_in_cu_p (&cu->header, sect_off))
19216 {
19217 pd = cu->find_partial_die (sect_off);
19218 if (pd != NULL)
19219 return { cu, pd };
19220 /* We missed recording what we needed.
19221 Load all dies and try again. */
19222 per_cu = cu->per_cu;
19223 }
19224 else
19225 {
19226 /* TUs don't reference other CUs/TUs (except via type signatures). */
19227 if (cu->per_cu->is_debug_types)
19228 {
19229 error (_("Dwarf Error: Type Unit at offset %s contains"
19230 " external reference to offset %s [in module %s].\n"),
19231 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19232 bfd_get_filename (objfile->obfd));
19233 }
19234 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19235 dwarf2_per_objfile);
19236
19237 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19238 load_partial_comp_unit (per_cu);
19239
19240 per_cu->cu->last_used = 0;
19241 pd = per_cu->cu->find_partial_die (sect_off);
19242 }
19243
19244 /* If we didn't find it, and not all dies have been loaded,
19245 load them all and try again. */
19246
19247 if (pd == NULL && per_cu->load_all_dies == 0)
19248 {
19249 per_cu->load_all_dies = 1;
19250
19251 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19252 THIS_CU->cu may already be in use. So we can't just free it and
19253 replace its DIEs with the ones we read in. Instead, we leave those
19254 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19255 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19256 set. */
19257 load_partial_comp_unit (per_cu);
19258
19259 pd = per_cu->cu->find_partial_die (sect_off);
19260 }
19261
19262 if (pd == NULL)
19263 internal_error (__FILE__, __LINE__,
19264 _("could not find partial DIE %s "
19265 "in cache [from module %s]\n"),
19266 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19267 return { per_cu->cu, pd };
19268 }
19269
19270 /* See if we can figure out if the class lives in a namespace. We do
19271 this by looking for a member function; its demangled name will
19272 contain namespace info, if there is any. */
19273
19274 static void
19275 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19276 struct dwarf2_cu *cu)
19277 {
19278 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19279 what template types look like, because the demangler
19280 frequently doesn't give the same name as the debug info. We
19281 could fix this by only using the demangled name to get the
19282 prefix (but see comment in read_structure_type). */
19283
19284 struct partial_die_info *real_pdi;
19285 struct partial_die_info *child_pdi;
19286
19287 /* If this DIE (this DIE's specification, if any) has a parent, then
19288 we should not do this. We'll prepend the parent's fully qualified
19289 name when we create the partial symbol. */
19290
19291 real_pdi = struct_pdi;
19292 while (real_pdi->has_specification)
19293 {
19294 auto res = find_partial_die (real_pdi->spec_offset,
19295 real_pdi->spec_is_dwz, cu);
19296 real_pdi = res.pdi;
19297 cu = res.cu;
19298 }
19299
19300 if (real_pdi->die_parent != NULL)
19301 return;
19302
19303 for (child_pdi = struct_pdi->die_child;
19304 child_pdi != NULL;
19305 child_pdi = child_pdi->die_sibling)
19306 {
19307 if (child_pdi->tag == DW_TAG_subprogram
19308 && child_pdi->linkage_name != NULL)
19309 {
19310 gdb::unique_xmalloc_ptr<char> actual_class_name
19311 (language_class_name_from_physname (cu->language_defn,
19312 child_pdi->linkage_name));
19313 if (actual_class_name != NULL)
19314 {
19315 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19316 struct_pdi->name
19317 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19318 actual_class_name.get ());
19319 }
19320 break;
19321 }
19322 }
19323 }
19324
19325 void
19326 partial_die_info::fixup (struct dwarf2_cu *cu)
19327 {
19328 /* Once we've fixed up a die, there's no point in doing so again.
19329 This also avoids a memory leak if we were to call
19330 guess_partial_die_structure_name multiple times. */
19331 if (fixup_called)
19332 return;
19333
19334 /* If we found a reference attribute and the DIE has no name, try
19335 to find a name in the referred to DIE. */
19336
19337 if (name == NULL && has_specification)
19338 {
19339 struct partial_die_info *spec_die;
19340
19341 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19342 spec_die = res.pdi;
19343 cu = res.cu;
19344
19345 spec_die->fixup (cu);
19346
19347 if (spec_die->name)
19348 {
19349 name = spec_die->name;
19350
19351 /* Copy DW_AT_external attribute if it is set. */
19352 if (spec_die->is_external)
19353 is_external = spec_die->is_external;
19354 }
19355 }
19356
19357 /* Set default names for some unnamed DIEs. */
19358
19359 if (name == NULL && tag == DW_TAG_namespace)
19360 name = CP_ANONYMOUS_NAMESPACE_STR;
19361
19362 /* If there is no parent die to provide a namespace, and there are
19363 children, see if we can determine the namespace from their linkage
19364 name. */
19365 if (cu->language == language_cplus
19366 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19367 && die_parent == NULL
19368 && has_children
19369 && (tag == DW_TAG_class_type
19370 || tag == DW_TAG_structure_type
19371 || tag == DW_TAG_union_type))
19372 guess_partial_die_structure_name (this, cu);
19373
19374 /* GCC might emit a nameless struct or union that has a linkage
19375 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19376 if (name == NULL
19377 && (tag == DW_TAG_class_type
19378 || tag == DW_TAG_interface_type
19379 || tag == DW_TAG_structure_type
19380 || tag == DW_TAG_union_type)
19381 && linkage_name != NULL)
19382 {
19383 gdb::unique_xmalloc_ptr<char> demangled
19384 (gdb_demangle (linkage_name, DMGL_TYPES));
19385 if (demangled != nullptr)
19386 {
19387 const char *base;
19388
19389 /* Strip any leading namespaces/classes, keep only the base name.
19390 DW_AT_name for named DIEs does not contain the prefixes. */
19391 base = strrchr (demangled.get (), ':');
19392 if (base && base > demangled.get () && base[-1] == ':')
19393 base++;
19394 else
19395 base = demangled.get ();
19396
19397 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19398 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19399 }
19400 }
19401
19402 fixup_called = 1;
19403 }
19404
19405 /* Process the attributes that had to be skipped in the first round. These
19406 attributes are the ones that need str_offsets_base or addr_base attributes.
19407 They could not have been processed in the first round, because at the time
19408 the values of str_offsets_base or addr_base may not have been known. */
19409 void read_attribute_reprocess (const struct die_reader_specs *reader,
19410 struct attribute *attr)
19411 {
19412 struct dwarf2_cu *cu = reader->cu;
19413 switch (attr->form)
19414 {
19415 case DW_FORM_addrx:
19416 case DW_FORM_GNU_addr_index:
19417 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19418 break;
19419 case DW_FORM_strx:
19420 case DW_FORM_strx1:
19421 case DW_FORM_strx2:
19422 case DW_FORM_strx3:
19423 case DW_FORM_strx4:
19424 case DW_FORM_GNU_str_index:
19425 {
19426 unsigned int str_index = DW_UNSND (attr);
19427 if (reader->dwo_file != NULL)
19428 {
19429 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19430 DW_STRING_IS_CANONICAL (attr) = 0;
19431 }
19432 else
19433 {
19434 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19435 DW_STRING_IS_CANONICAL (attr) = 0;
19436 }
19437 break;
19438 }
19439 default:
19440 gdb_assert_not_reached (_("Unexpected DWARF form."));
19441 }
19442 }
19443
19444 /* Read an attribute value described by an attribute form. */
19445
19446 static const gdb_byte *
19447 read_attribute_value (const struct die_reader_specs *reader,
19448 struct attribute *attr, unsigned form,
19449 LONGEST implicit_const, const gdb_byte *info_ptr,
19450 bool *need_reprocess)
19451 {
19452 struct dwarf2_cu *cu = reader->cu;
19453 struct dwarf2_per_objfile *dwarf2_per_objfile
19454 = cu->per_cu->dwarf2_per_objfile;
19455 struct objfile *objfile = dwarf2_per_objfile->objfile;
19456 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19457 bfd *abfd = reader->abfd;
19458 struct comp_unit_head *cu_header = &cu->header;
19459 unsigned int bytes_read;
19460 struct dwarf_block *blk;
19461 *need_reprocess = false;
19462
19463 attr->form = (enum dwarf_form) form;
19464 switch (form)
19465 {
19466 case DW_FORM_ref_addr:
19467 if (cu->header.version == 2)
19468 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19469 else
19470 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19471 &cu->header, &bytes_read);
19472 info_ptr += bytes_read;
19473 break;
19474 case DW_FORM_GNU_ref_alt:
19475 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19476 info_ptr += bytes_read;
19477 break;
19478 case DW_FORM_addr:
19479 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19480 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19481 info_ptr += bytes_read;
19482 break;
19483 case DW_FORM_block2:
19484 blk = dwarf_alloc_block (cu);
19485 blk->size = read_2_bytes (abfd, info_ptr);
19486 info_ptr += 2;
19487 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19488 info_ptr += blk->size;
19489 DW_BLOCK (attr) = blk;
19490 break;
19491 case DW_FORM_block4:
19492 blk = dwarf_alloc_block (cu);
19493 blk->size = read_4_bytes (abfd, info_ptr);
19494 info_ptr += 4;
19495 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19496 info_ptr += blk->size;
19497 DW_BLOCK (attr) = blk;
19498 break;
19499 case DW_FORM_data2:
19500 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19501 info_ptr += 2;
19502 break;
19503 case DW_FORM_data4:
19504 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19505 info_ptr += 4;
19506 break;
19507 case DW_FORM_data8:
19508 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19509 info_ptr += 8;
19510 break;
19511 case DW_FORM_data16:
19512 blk = dwarf_alloc_block (cu);
19513 blk->size = 16;
19514 blk->data = read_n_bytes (abfd, info_ptr, 16);
19515 info_ptr += 16;
19516 DW_BLOCK (attr) = blk;
19517 break;
19518 case DW_FORM_sec_offset:
19519 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19520 info_ptr += bytes_read;
19521 break;
19522 case DW_FORM_string:
19523 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19524 DW_STRING_IS_CANONICAL (attr) = 0;
19525 info_ptr += bytes_read;
19526 break;
19527 case DW_FORM_strp:
19528 if (!cu->per_cu->is_dwz)
19529 {
19530 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19531 abfd, info_ptr, cu_header,
19532 &bytes_read);
19533 DW_STRING_IS_CANONICAL (attr) = 0;
19534 info_ptr += bytes_read;
19535 break;
19536 }
19537 /* FALLTHROUGH */
19538 case DW_FORM_line_strp:
19539 if (!cu->per_cu->is_dwz)
19540 {
19541 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19542 abfd, info_ptr,
19543 cu_header, &bytes_read);
19544 DW_STRING_IS_CANONICAL (attr) = 0;
19545 info_ptr += bytes_read;
19546 break;
19547 }
19548 /* FALLTHROUGH */
19549 case DW_FORM_GNU_strp_alt:
19550 {
19551 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19552 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19553 &bytes_read);
19554
19555 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19556 dwz, str_offset);
19557 DW_STRING_IS_CANONICAL (attr) = 0;
19558 info_ptr += bytes_read;
19559 }
19560 break;
19561 case DW_FORM_exprloc:
19562 case DW_FORM_block:
19563 blk = dwarf_alloc_block (cu);
19564 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19565 info_ptr += bytes_read;
19566 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19567 info_ptr += blk->size;
19568 DW_BLOCK (attr) = blk;
19569 break;
19570 case DW_FORM_block1:
19571 blk = dwarf_alloc_block (cu);
19572 blk->size = read_1_byte (abfd, info_ptr);
19573 info_ptr += 1;
19574 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19575 info_ptr += blk->size;
19576 DW_BLOCK (attr) = blk;
19577 break;
19578 case DW_FORM_data1:
19579 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19580 info_ptr += 1;
19581 break;
19582 case DW_FORM_flag:
19583 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19584 info_ptr += 1;
19585 break;
19586 case DW_FORM_flag_present:
19587 DW_UNSND (attr) = 1;
19588 break;
19589 case DW_FORM_sdata:
19590 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19591 info_ptr += bytes_read;
19592 break;
19593 case DW_FORM_udata:
19594 case DW_FORM_rnglistx:
19595 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19596 info_ptr += bytes_read;
19597 break;
19598 case DW_FORM_ref1:
19599 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19600 + read_1_byte (abfd, info_ptr));
19601 info_ptr += 1;
19602 break;
19603 case DW_FORM_ref2:
19604 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19605 + read_2_bytes (abfd, info_ptr));
19606 info_ptr += 2;
19607 break;
19608 case DW_FORM_ref4:
19609 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19610 + read_4_bytes (abfd, info_ptr));
19611 info_ptr += 4;
19612 break;
19613 case DW_FORM_ref8:
19614 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19615 + read_8_bytes (abfd, info_ptr));
19616 info_ptr += 8;
19617 break;
19618 case DW_FORM_ref_sig8:
19619 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19620 info_ptr += 8;
19621 break;
19622 case DW_FORM_ref_udata:
19623 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19624 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19625 info_ptr += bytes_read;
19626 break;
19627 case DW_FORM_indirect:
19628 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19629 info_ptr += bytes_read;
19630 if (form == DW_FORM_implicit_const)
19631 {
19632 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19633 info_ptr += bytes_read;
19634 }
19635 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19636 info_ptr, need_reprocess);
19637 break;
19638 case DW_FORM_implicit_const:
19639 DW_SND (attr) = implicit_const;
19640 break;
19641 case DW_FORM_addrx:
19642 case DW_FORM_GNU_addr_index:
19643 *need_reprocess = true;
19644 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19645 info_ptr += bytes_read;
19646 break;
19647 case DW_FORM_strx:
19648 case DW_FORM_strx1:
19649 case DW_FORM_strx2:
19650 case DW_FORM_strx3:
19651 case DW_FORM_strx4:
19652 case DW_FORM_GNU_str_index:
19653 {
19654 ULONGEST str_index;
19655 if (form == DW_FORM_strx1)
19656 {
19657 str_index = read_1_byte (abfd, info_ptr);
19658 info_ptr += 1;
19659 }
19660 else if (form == DW_FORM_strx2)
19661 {
19662 str_index = read_2_bytes (abfd, info_ptr);
19663 info_ptr += 2;
19664 }
19665 else if (form == DW_FORM_strx3)
19666 {
19667 str_index = read_3_bytes (abfd, info_ptr);
19668 info_ptr += 3;
19669 }
19670 else if (form == DW_FORM_strx4)
19671 {
19672 str_index = read_4_bytes (abfd, info_ptr);
19673 info_ptr += 4;
19674 }
19675 else
19676 {
19677 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19678 info_ptr += bytes_read;
19679 }
19680 *need_reprocess = true;
19681 DW_UNSND (attr) = str_index;
19682 }
19683 break;
19684 default:
19685 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19686 dwarf_form_name (form),
19687 bfd_get_filename (abfd));
19688 }
19689
19690 /* Super hack. */
19691 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19692 attr->form = DW_FORM_GNU_ref_alt;
19693
19694 /* We have seen instances where the compiler tried to emit a byte
19695 size attribute of -1 which ended up being encoded as an unsigned
19696 0xffffffff. Although 0xffffffff is technically a valid size value,
19697 an object of this size seems pretty unlikely so we can relatively
19698 safely treat these cases as if the size attribute was invalid and
19699 treat them as zero by default. */
19700 if (attr->name == DW_AT_byte_size
19701 && form == DW_FORM_data4
19702 && DW_UNSND (attr) >= 0xffffffff)
19703 {
19704 complaint
19705 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19706 hex_string (DW_UNSND (attr)));
19707 DW_UNSND (attr) = 0;
19708 }
19709
19710 return info_ptr;
19711 }
19712
19713 /* Read an attribute described by an abbreviated attribute. */
19714
19715 static const gdb_byte *
19716 read_attribute (const struct die_reader_specs *reader,
19717 struct attribute *attr, struct attr_abbrev *abbrev,
19718 const gdb_byte *info_ptr, bool *need_reprocess)
19719 {
19720 attr->name = abbrev->name;
19721 return read_attribute_value (reader, attr, abbrev->form,
19722 abbrev->implicit_const, info_ptr,
19723 need_reprocess);
19724 }
19725
19726 /* Read dwarf information from a buffer. */
19727
19728 static unsigned int
19729 read_1_byte (bfd *abfd, const gdb_byte *buf)
19730 {
19731 return bfd_get_8 (abfd, buf);
19732 }
19733
19734 static int
19735 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19736 {
19737 return bfd_get_signed_8 (abfd, buf);
19738 }
19739
19740 static unsigned int
19741 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19742 {
19743 return bfd_get_16 (abfd, buf);
19744 }
19745
19746 static int
19747 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19748 {
19749 return bfd_get_signed_16 (abfd, buf);
19750 }
19751
19752 static unsigned int
19753 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19754 {
19755 unsigned int result = 0;
19756 for (int i = 0; i < 3; ++i)
19757 {
19758 unsigned char byte = bfd_get_8 (abfd, buf);
19759 buf++;
19760 result |= ((unsigned int) byte << (i * 8));
19761 }
19762 return result;
19763 }
19764
19765 static unsigned int
19766 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19767 {
19768 return bfd_get_32 (abfd, buf);
19769 }
19770
19771 static int
19772 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19773 {
19774 return bfd_get_signed_32 (abfd, buf);
19775 }
19776
19777 static ULONGEST
19778 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19779 {
19780 return bfd_get_64 (abfd, buf);
19781 }
19782
19783 static CORE_ADDR
19784 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19785 unsigned int *bytes_read)
19786 {
19787 struct comp_unit_head *cu_header = &cu->header;
19788 CORE_ADDR retval = 0;
19789
19790 if (cu_header->signed_addr_p)
19791 {
19792 switch (cu_header->addr_size)
19793 {
19794 case 2:
19795 retval = bfd_get_signed_16 (abfd, buf);
19796 break;
19797 case 4:
19798 retval = bfd_get_signed_32 (abfd, buf);
19799 break;
19800 case 8:
19801 retval = bfd_get_signed_64 (abfd, buf);
19802 break;
19803 default:
19804 internal_error (__FILE__, __LINE__,
19805 _("read_address: bad switch, signed [in module %s]"),
19806 bfd_get_filename (abfd));
19807 }
19808 }
19809 else
19810 {
19811 switch (cu_header->addr_size)
19812 {
19813 case 2:
19814 retval = bfd_get_16 (abfd, buf);
19815 break;
19816 case 4:
19817 retval = bfd_get_32 (abfd, buf);
19818 break;
19819 case 8:
19820 retval = bfd_get_64 (abfd, buf);
19821 break;
19822 default:
19823 internal_error (__FILE__, __LINE__,
19824 _("read_address: bad switch, "
19825 "unsigned [in module %s]"),
19826 bfd_get_filename (abfd));
19827 }
19828 }
19829
19830 *bytes_read = cu_header->addr_size;
19831 return retval;
19832 }
19833
19834 /* Read the initial length from a section. The (draft) DWARF 3
19835 specification allows the initial length to take up either 4 bytes
19836 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19837 bytes describe the length and all offsets will be 8 bytes in length
19838 instead of 4.
19839
19840 An older, non-standard 64-bit format is also handled by this
19841 function. The older format in question stores the initial length
19842 as an 8-byte quantity without an escape value. Lengths greater
19843 than 2^32 aren't very common which means that the initial 4 bytes
19844 is almost always zero. Since a length value of zero doesn't make
19845 sense for the 32-bit format, this initial zero can be considered to
19846 be an escape value which indicates the presence of the older 64-bit
19847 format. As written, the code can't detect (old format) lengths
19848 greater than 4GB. If it becomes necessary to handle lengths
19849 somewhat larger than 4GB, we could allow other small values (such
19850 as the non-sensical values of 1, 2, and 3) to also be used as
19851 escape values indicating the presence of the old format.
19852
19853 The value returned via bytes_read should be used to increment the
19854 relevant pointer after calling read_initial_length().
19855
19856 [ Note: read_initial_length() and read_offset() are based on the
19857 document entitled "DWARF Debugging Information Format", revision
19858 3, draft 8, dated November 19, 2001. This document was obtained
19859 from:
19860
19861 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19862
19863 This document is only a draft and is subject to change. (So beware.)
19864
19865 Details regarding the older, non-standard 64-bit format were
19866 determined empirically by examining 64-bit ELF files produced by
19867 the SGI toolchain on an IRIX 6.5 machine.
19868
19869 - Kevin, July 16, 2002
19870 ] */
19871
19872 static LONGEST
19873 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19874 {
19875 LONGEST length = bfd_get_32 (abfd, buf);
19876
19877 if (length == 0xffffffff)
19878 {
19879 length = bfd_get_64 (abfd, buf + 4);
19880 *bytes_read = 12;
19881 }
19882 else if (length == 0)
19883 {
19884 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19885 length = bfd_get_64 (abfd, buf);
19886 *bytes_read = 8;
19887 }
19888 else
19889 {
19890 *bytes_read = 4;
19891 }
19892
19893 return length;
19894 }
19895
19896 /* Cover function for read_initial_length.
19897 Returns the length of the object at BUF, and stores the size of the
19898 initial length in *BYTES_READ and stores the size that offsets will be in
19899 *OFFSET_SIZE.
19900 If the initial length size is not equivalent to that specified in
19901 CU_HEADER then issue a complaint.
19902 This is useful when reading non-comp-unit headers. */
19903
19904 static LONGEST
19905 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19906 const struct comp_unit_head *cu_header,
19907 unsigned int *bytes_read,
19908 unsigned int *offset_size)
19909 {
19910 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19911
19912 gdb_assert (cu_header->initial_length_size == 4
19913 || cu_header->initial_length_size == 8
19914 || cu_header->initial_length_size == 12);
19915
19916 if (cu_header->initial_length_size != *bytes_read)
19917 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19918
19919 *offset_size = (*bytes_read == 4) ? 4 : 8;
19920 return length;
19921 }
19922
19923 /* Read an offset from the data stream. The size of the offset is
19924 given by cu_header->offset_size. */
19925
19926 static LONGEST
19927 read_offset (bfd *abfd, const gdb_byte *buf,
19928 const struct comp_unit_head *cu_header,
19929 unsigned int *bytes_read)
19930 {
19931 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19932
19933 *bytes_read = cu_header->offset_size;
19934 return offset;
19935 }
19936
19937 /* Read an offset from the data stream. */
19938
19939 static LONGEST
19940 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19941 {
19942 LONGEST retval = 0;
19943
19944 switch (offset_size)
19945 {
19946 case 4:
19947 retval = bfd_get_32 (abfd, buf);
19948 break;
19949 case 8:
19950 retval = bfd_get_64 (abfd, buf);
19951 break;
19952 default:
19953 internal_error (__FILE__, __LINE__,
19954 _("read_offset_1: bad switch [in module %s]"),
19955 bfd_get_filename (abfd));
19956 }
19957
19958 return retval;
19959 }
19960
19961 static const gdb_byte *
19962 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19963 {
19964 /* If the size of a host char is 8 bits, we can return a pointer
19965 to the buffer, otherwise we have to copy the data to a buffer
19966 allocated on the temporary obstack. */
19967 gdb_assert (HOST_CHAR_BIT == 8);
19968 return buf;
19969 }
19970
19971 static const char *
19972 read_direct_string (bfd *abfd, const gdb_byte *buf,
19973 unsigned int *bytes_read_ptr)
19974 {
19975 /* If the size of a host char is 8 bits, we can return a pointer
19976 to the string, otherwise we have to copy the string to a buffer
19977 allocated on the temporary obstack. */
19978 gdb_assert (HOST_CHAR_BIT == 8);
19979 if (*buf == '\0')
19980 {
19981 *bytes_read_ptr = 1;
19982 return NULL;
19983 }
19984 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19985 return (const char *) buf;
19986 }
19987
19988 /* Return pointer to string at section SECT offset STR_OFFSET with error
19989 reporting strings FORM_NAME and SECT_NAME. */
19990
19991 static const char *
19992 read_indirect_string_at_offset_from (struct objfile *objfile,
19993 bfd *abfd, LONGEST str_offset,
19994 struct dwarf2_section_info *sect,
19995 const char *form_name,
19996 const char *sect_name)
19997 {
19998 dwarf2_read_section (objfile, sect);
19999 if (sect->buffer == NULL)
20000 error (_("%s used without %s section [in module %s]"),
20001 form_name, sect_name, bfd_get_filename (abfd));
20002 if (str_offset >= sect->size)
20003 error (_("%s pointing outside of %s section [in module %s]"),
20004 form_name, sect_name, bfd_get_filename (abfd));
20005 gdb_assert (HOST_CHAR_BIT == 8);
20006 if (sect->buffer[str_offset] == '\0')
20007 return NULL;
20008 return (const char *) (sect->buffer + str_offset);
20009 }
20010
20011 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20012
20013 static const char *
20014 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20015 bfd *abfd, LONGEST str_offset)
20016 {
20017 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20018 abfd, str_offset,
20019 &dwarf2_per_objfile->str,
20020 "DW_FORM_strp", ".debug_str");
20021 }
20022
20023 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
20024
20025 static const char *
20026 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20027 bfd *abfd, LONGEST str_offset)
20028 {
20029 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20030 abfd, str_offset,
20031 &dwarf2_per_objfile->line_str,
20032 "DW_FORM_line_strp",
20033 ".debug_line_str");
20034 }
20035
20036 /* Read a string at offset STR_OFFSET in the .debug_str section from
20037 the .dwz file DWZ. Throw an error if the offset is too large. If
20038 the string consists of a single NUL byte, return NULL; otherwise
20039 return a pointer to the string. */
20040
20041 static const char *
20042 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20043 LONGEST str_offset)
20044 {
20045 dwarf2_read_section (objfile, &dwz->str);
20046
20047 if (dwz->str.buffer == NULL)
20048 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20049 "section [in module %s]"),
20050 bfd_get_filename (dwz->dwz_bfd.get ()));
20051 if (str_offset >= dwz->str.size)
20052 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20053 ".debug_str section [in module %s]"),
20054 bfd_get_filename (dwz->dwz_bfd.get ()));
20055 gdb_assert (HOST_CHAR_BIT == 8);
20056 if (dwz->str.buffer[str_offset] == '\0')
20057 return NULL;
20058 return (const char *) (dwz->str.buffer + str_offset);
20059 }
20060
20061 /* Return pointer to string at .debug_str offset as read from BUF.
20062 BUF is assumed to be in a compilation unit described by CU_HEADER.
20063 Return *BYTES_READ_PTR count of bytes read from BUF. */
20064
20065 static const char *
20066 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20067 const gdb_byte *buf,
20068 const struct comp_unit_head *cu_header,
20069 unsigned int *bytes_read_ptr)
20070 {
20071 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20072
20073 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
20074 }
20075
20076 /* Return pointer to string at .debug_line_str offset as read from BUF.
20077 BUF is assumed to be in a compilation unit described by CU_HEADER.
20078 Return *BYTES_READ_PTR count of bytes read from BUF. */
20079
20080 static const char *
20081 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20082 bfd *abfd, const gdb_byte *buf,
20083 const struct comp_unit_head *cu_header,
20084 unsigned int *bytes_read_ptr)
20085 {
20086 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20087
20088 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20089 str_offset);
20090 }
20091
20092 ULONGEST
20093 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
20094 unsigned int *bytes_read_ptr)
20095 {
20096 ULONGEST result;
20097 unsigned int num_read;
20098 int shift;
20099 unsigned char byte;
20100
20101 result = 0;
20102 shift = 0;
20103 num_read = 0;
20104 while (1)
20105 {
20106 byte = bfd_get_8 (abfd, buf);
20107 buf++;
20108 num_read++;
20109 result |= ((ULONGEST) (byte & 127) << shift);
20110 if ((byte & 128) == 0)
20111 {
20112 break;
20113 }
20114 shift += 7;
20115 }
20116 *bytes_read_ptr = num_read;
20117 return result;
20118 }
20119
20120 static LONGEST
20121 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20122 unsigned int *bytes_read_ptr)
20123 {
20124 ULONGEST result;
20125 int shift, num_read;
20126 unsigned char byte;
20127
20128 result = 0;
20129 shift = 0;
20130 num_read = 0;
20131 while (1)
20132 {
20133 byte = bfd_get_8 (abfd, buf);
20134 buf++;
20135 num_read++;
20136 result |= ((ULONGEST) (byte & 127) << shift);
20137 shift += 7;
20138 if ((byte & 128) == 0)
20139 {
20140 break;
20141 }
20142 }
20143 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
20144 result |= -(((ULONGEST) 1) << shift);
20145 *bytes_read_ptr = num_read;
20146 return result;
20147 }
20148
20149 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20150 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20151 ADDR_SIZE is the size of addresses from the CU header. */
20152
20153 static CORE_ADDR
20154 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20155 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
20156 int addr_size)
20157 {
20158 struct objfile *objfile = dwarf2_per_objfile->objfile;
20159 bfd *abfd = objfile->obfd;
20160 const gdb_byte *info_ptr;
20161 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20162
20163 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20164 if (dwarf2_per_objfile->addr.buffer == NULL)
20165 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20166 objfile_name (objfile));
20167 if (addr_base_or_zero + addr_index * addr_size
20168 >= dwarf2_per_objfile->addr.size)
20169 error (_("DW_FORM_addr_index pointing outside of "
20170 ".debug_addr section [in module %s]"),
20171 objfile_name (objfile));
20172 info_ptr = (dwarf2_per_objfile->addr.buffer
20173 + addr_base_or_zero + addr_index * addr_size);
20174 if (addr_size == 4)
20175 return bfd_get_32 (abfd, info_ptr);
20176 else
20177 return bfd_get_64 (abfd, info_ptr);
20178 }
20179
20180 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20181
20182 static CORE_ADDR
20183 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20184 {
20185 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20186 cu->addr_base, cu->header.addr_size);
20187 }
20188
20189 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20190
20191 static CORE_ADDR
20192 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20193 unsigned int *bytes_read)
20194 {
20195 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20196 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20197
20198 return read_addr_index (cu, addr_index);
20199 }
20200
20201 /* Given an index in .debug_addr, fetch the value.
20202 NOTE: This can be called during dwarf expression evaluation,
20203 long after the debug information has been read, and thus per_cu->cu
20204 may no longer exist. */
20205
20206 CORE_ADDR
20207 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20208 unsigned int addr_index)
20209 {
20210 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20211 struct dwarf2_cu *cu = per_cu->cu;
20212 gdb::optional<ULONGEST> addr_base;
20213 int addr_size;
20214
20215 /* We need addr_base and addr_size.
20216 If we don't have PER_CU->cu, we have to get it.
20217 Nasty, but the alternative is storing the needed info in PER_CU,
20218 which at this point doesn't seem justified: it's not clear how frequently
20219 it would get used and it would increase the size of every PER_CU.
20220 Entry points like dwarf2_per_cu_addr_size do a similar thing
20221 so we're not in uncharted territory here.
20222 Alas we need to be a bit more complicated as addr_base is contained
20223 in the DIE.
20224
20225 We don't need to read the entire CU(/TU).
20226 We just need the header and top level die.
20227
20228 IWBN to use the aging mechanism to let us lazily later discard the CU.
20229 For now we skip this optimization. */
20230
20231 if (cu != NULL)
20232 {
20233 addr_base = cu->addr_base;
20234 addr_size = cu->header.addr_size;
20235 }
20236 else
20237 {
20238 cutu_reader reader (per_cu, NULL, 0, 0, false);
20239 addr_base = reader.cu->addr_base;
20240 addr_size = reader.cu->header.addr_size;
20241 }
20242
20243 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20244 addr_size);
20245 }
20246
20247 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20248 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20249 DWO file. */
20250
20251 static const char *
20252 read_str_index (struct dwarf2_cu *cu,
20253 struct dwarf2_section_info *str_section,
20254 struct dwarf2_section_info *str_offsets_section,
20255 ULONGEST str_offsets_base, ULONGEST str_index)
20256 {
20257 struct dwarf2_per_objfile *dwarf2_per_objfile
20258 = cu->per_cu->dwarf2_per_objfile;
20259 struct objfile *objfile = dwarf2_per_objfile->objfile;
20260 const char *objf_name = objfile_name (objfile);
20261 bfd *abfd = objfile->obfd;
20262 const gdb_byte *info_ptr;
20263 ULONGEST str_offset;
20264 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20265
20266 dwarf2_read_section (objfile, str_section);
20267 dwarf2_read_section (objfile, str_offsets_section);
20268 if (str_section->buffer == NULL)
20269 error (_("%s used without %s section"
20270 " in CU at offset %s [in module %s]"),
20271 form_name, get_section_name (str_section),
20272 sect_offset_str (cu->header.sect_off), objf_name);
20273 if (str_offsets_section->buffer == NULL)
20274 error (_("%s used without %s section"
20275 " in CU at offset %s [in module %s]"),
20276 form_name, get_section_name (str_section),
20277 sect_offset_str (cu->header.sect_off), objf_name);
20278 info_ptr = (str_offsets_section->buffer
20279 + str_offsets_base
20280 + str_index * cu->header.offset_size);
20281 if (cu->header.offset_size == 4)
20282 str_offset = bfd_get_32 (abfd, info_ptr);
20283 else
20284 str_offset = bfd_get_64 (abfd, info_ptr);
20285 if (str_offset >= str_section->size)
20286 error (_("Offset from %s pointing outside of"
20287 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20288 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20289 return (const char *) (str_section->buffer + str_offset);
20290 }
20291
20292 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20293
20294 static const char *
20295 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20296 {
20297 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20298 ? reader->cu->header.addr_size : 0;
20299 return read_str_index (reader->cu,
20300 &reader->dwo_file->sections.str,
20301 &reader->dwo_file->sections.str_offsets,
20302 str_offsets_base, str_index);
20303 }
20304
20305 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20306
20307 static const char *
20308 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20309 {
20310 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20311 const char *objf_name = objfile_name (objfile);
20312 static const char form_name[] = "DW_FORM_GNU_str_index";
20313 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20314
20315 if (!cu->str_offsets_base.has_value ())
20316 error (_("%s used in Fission stub without %s"
20317 " in CU at offset 0x%lx [in module %s]"),
20318 form_name, str_offsets_attr_name,
20319 (long) cu->header.offset_size, objf_name);
20320
20321 return read_str_index (cu,
20322 &cu->per_cu->dwarf2_per_objfile->str,
20323 &cu->per_cu->dwarf2_per_objfile->str_offsets,
20324 *cu->str_offsets_base, str_index);
20325 }
20326
20327 /* Return the length of an LEB128 number in BUF. */
20328
20329 static int
20330 leb128_size (const gdb_byte *buf)
20331 {
20332 const gdb_byte *begin = buf;
20333 gdb_byte byte;
20334
20335 while (1)
20336 {
20337 byte = *buf++;
20338 if ((byte & 128) == 0)
20339 return buf - begin;
20340 }
20341 }
20342
20343 static void
20344 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20345 {
20346 switch (lang)
20347 {
20348 case DW_LANG_C89:
20349 case DW_LANG_C99:
20350 case DW_LANG_C11:
20351 case DW_LANG_C:
20352 case DW_LANG_UPC:
20353 cu->language = language_c;
20354 break;
20355 case DW_LANG_Java:
20356 case DW_LANG_C_plus_plus:
20357 case DW_LANG_C_plus_plus_11:
20358 case DW_LANG_C_plus_plus_14:
20359 cu->language = language_cplus;
20360 break;
20361 case DW_LANG_D:
20362 cu->language = language_d;
20363 break;
20364 case DW_LANG_Fortran77:
20365 case DW_LANG_Fortran90:
20366 case DW_LANG_Fortran95:
20367 case DW_LANG_Fortran03:
20368 case DW_LANG_Fortran08:
20369 cu->language = language_fortran;
20370 break;
20371 case DW_LANG_Go:
20372 cu->language = language_go;
20373 break;
20374 case DW_LANG_Mips_Assembler:
20375 cu->language = language_asm;
20376 break;
20377 case DW_LANG_Ada83:
20378 case DW_LANG_Ada95:
20379 cu->language = language_ada;
20380 break;
20381 case DW_LANG_Modula2:
20382 cu->language = language_m2;
20383 break;
20384 case DW_LANG_Pascal83:
20385 cu->language = language_pascal;
20386 break;
20387 case DW_LANG_ObjC:
20388 cu->language = language_objc;
20389 break;
20390 case DW_LANG_Rust:
20391 case DW_LANG_Rust_old:
20392 cu->language = language_rust;
20393 break;
20394 case DW_LANG_Cobol74:
20395 case DW_LANG_Cobol85:
20396 default:
20397 cu->language = language_minimal;
20398 break;
20399 }
20400 cu->language_defn = language_def (cu->language);
20401 }
20402
20403 /* Return the named attribute or NULL if not there. */
20404
20405 static struct attribute *
20406 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20407 {
20408 for (;;)
20409 {
20410 unsigned int i;
20411 struct attribute *spec = NULL;
20412
20413 for (i = 0; i < die->num_attrs; ++i)
20414 {
20415 if (die->attrs[i].name == name)
20416 return &die->attrs[i];
20417 if (die->attrs[i].name == DW_AT_specification
20418 || die->attrs[i].name == DW_AT_abstract_origin)
20419 spec = &die->attrs[i];
20420 }
20421
20422 if (!spec)
20423 break;
20424
20425 die = follow_die_ref (die, spec, &cu);
20426 }
20427
20428 return NULL;
20429 }
20430
20431 /* Return the named attribute or NULL if not there,
20432 but do not follow DW_AT_specification, etc.
20433 This is for use in contexts where we're reading .debug_types dies.
20434 Following DW_AT_specification, DW_AT_abstract_origin will take us
20435 back up the chain, and we want to go down. */
20436
20437 static struct attribute *
20438 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20439 {
20440 unsigned int i;
20441
20442 for (i = 0; i < die->num_attrs; ++i)
20443 if (die->attrs[i].name == name)
20444 return &die->attrs[i];
20445
20446 return NULL;
20447 }
20448
20449 /* Return the string associated with a string-typed attribute, or NULL if it
20450 is either not found or is of an incorrect type. */
20451
20452 static const char *
20453 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20454 {
20455 struct attribute *attr;
20456 const char *str = NULL;
20457
20458 attr = dwarf2_attr (die, name, cu);
20459
20460 if (attr != NULL)
20461 {
20462 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20463 || attr->form == DW_FORM_string
20464 || attr->form == DW_FORM_strx
20465 || attr->form == DW_FORM_strx1
20466 || attr->form == DW_FORM_strx2
20467 || attr->form == DW_FORM_strx3
20468 || attr->form == DW_FORM_strx4
20469 || attr->form == DW_FORM_GNU_str_index
20470 || attr->form == DW_FORM_GNU_strp_alt)
20471 str = DW_STRING (attr);
20472 else
20473 complaint (_("string type expected for attribute %s for "
20474 "DIE at %s in module %s"),
20475 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20476 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20477 }
20478
20479 return str;
20480 }
20481
20482 /* Return the dwo name or NULL if not present. If present, it is in either
20483 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20484 static const char *
20485 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20486 {
20487 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20488 if (dwo_name == nullptr)
20489 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20490 return dwo_name;
20491 }
20492
20493 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20494 and holds a non-zero value. This function should only be used for
20495 DW_FORM_flag or DW_FORM_flag_present attributes. */
20496
20497 static int
20498 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20499 {
20500 struct attribute *attr = dwarf2_attr (die, name, cu);
20501
20502 return (attr && DW_UNSND (attr));
20503 }
20504
20505 static int
20506 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20507 {
20508 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20509 which value is non-zero. However, we have to be careful with
20510 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20511 (via dwarf2_flag_true_p) follows this attribute. So we may
20512 end up accidently finding a declaration attribute that belongs
20513 to a different DIE referenced by the specification attribute,
20514 even though the given DIE does not have a declaration attribute. */
20515 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20516 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20517 }
20518
20519 /* Return the die giving the specification for DIE, if there is
20520 one. *SPEC_CU is the CU containing DIE on input, and the CU
20521 containing the return value on output. If there is no
20522 specification, but there is an abstract origin, that is
20523 returned. */
20524
20525 static struct die_info *
20526 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20527 {
20528 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20529 *spec_cu);
20530
20531 if (spec_attr == NULL)
20532 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20533
20534 if (spec_attr == NULL)
20535 return NULL;
20536 else
20537 return follow_die_ref (die, spec_attr, spec_cu);
20538 }
20539
20540 /* Stub for free_line_header to match void * callback types. */
20541
20542 static void
20543 free_line_header_voidp (void *arg)
20544 {
20545 struct line_header *lh = (struct line_header *) arg;
20546
20547 delete lh;
20548 }
20549
20550 void
20551 line_header::add_include_dir (const char *include_dir)
20552 {
20553 if (dwarf_line_debug >= 2)
20554 {
20555 size_t new_size;
20556 if (version >= 5)
20557 new_size = m_include_dirs.size ();
20558 else
20559 new_size = m_include_dirs.size () + 1;
20560 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20561 new_size, include_dir);
20562 }
20563 m_include_dirs.push_back (include_dir);
20564 }
20565
20566 void
20567 line_header::add_file_name (const char *name,
20568 dir_index d_index,
20569 unsigned int mod_time,
20570 unsigned int length)
20571 {
20572 if (dwarf_line_debug >= 2)
20573 {
20574 size_t new_size;
20575 if (version >= 5)
20576 new_size = file_names_size ();
20577 else
20578 new_size = file_names_size () + 1;
20579 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20580 new_size, name);
20581 }
20582 m_file_names.emplace_back (name, d_index, mod_time, length);
20583 }
20584
20585 /* A convenience function to find the proper .debug_line section for a CU. */
20586
20587 static struct dwarf2_section_info *
20588 get_debug_line_section (struct dwarf2_cu *cu)
20589 {
20590 struct dwarf2_section_info *section;
20591 struct dwarf2_per_objfile *dwarf2_per_objfile
20592 = cu->per_cu->dwarf2_per_objfile;
20593
20594 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20595 DWO file. */
20596 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20597 section = &cu->dwo_unit->dwo_file->sections.line;
20598 else if (cu->per_cu->is_dwz)
20599 {
20600 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20601
20602 section = &dwz->line;
20603 }
20604 else
20605 section = &dwarf2_per_objfile->line;
20606
20607 return section;
20608 }
20609
20610 /* Read directory or file name entry format, starting with byte of
20611 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20612 entries count and the entries themselves in the described entry
20613 format. */
20614
20615 static void
20616 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20617 bfd *abfd, const gdb_byte **bufp,
20618 struct line_header *lh,
20619 const struct comp_unit_head *cu_header,
20620 void (*callback) (struct line_header *lh,
20621 const char *name,
20622 dir_index d_index,
20623 unsigned int mod_time,
20624 unsigned int length))
20625 {
20626 gdb_byte format_count, formati;
20627 ULONGEST data_count, datai;
20628 const gdb_byte *buf = *bufp;
20629 const gdb_byte *format_header_data;
20630 unsigned int bytes_read;
20631
20632 format_count = read_1_byte (abfd, buf);
20633 buf += 1;
20634 format_header_data = buf;
20635 for (formati = 0; formati < format_count; formati++)
20636 {
20637 read_unsigned_leb128 (abfd, buf, &bytes_read);
20638 buf += bytes_read;
20639 read_unsigned_leb128 (abfd, buf, &bytes_read);
20640 buf += bytes_read;
20641 }
20642
20643 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20644 buf += bytes_read;
20645 for (datai = 0; datai < data_count; datai++)
20646 {
20647 const gdb_byte *format = format_header_data;
20648 struct file_entry fe;
20649
20650 for (formati = 0; formati < format_count; formati++)
20651 {
20652 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20653 format += bytes_read;
20654
20655 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20656 format += bytes_read;
20657
20658 gdb::optional<const char *> string;
20659 gdb::optional<unsigned int> uint;
20660
20661 switch (form)
20662 {
20663 case DW_FORM_string:
20664 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20665 buf += bytes_read;
20666 break;
20667
20668 case DW_FORM_line_strp:
20669 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20670 abfd, buf,
20671 cu_header,
20672 &bytes_read));
20673 buf += bytes_read;
20674 break;
20675
20676 case DW_FORM_data1:
20677 uint.emplace (read_1_byte (abfd, buf));
20678 buf += 1;
20679 break;
20680
20681 case DW_FORM_data2:
20682 uint.emplace (read_2_bytes (abfd, buf));
20683 buf += 2;
20684 break;
20685
20686 case DW_FORM_data4:
20687 uint.emplace (read_4_bytes (abfd, buf));
20688 buf += 4;
20689 break;
20690
20691 case DW_FORM_data8:
20692 uint.emplace (read_8_bytes (abfd, buf));
20693 buf += 8;
20694 break;
20695
20696 case DW_FORM_data16:
20697 /* This is used for MD5, but file_entry does not record MD5s. */
20698 buf += 16;
20699 break;
20700
20701 case DW_FORM_udata:
20702 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20703 buf += bytes_read;
20704 break;
20705
20706 case DW_FORM_block:
20707 /* It is valid only for DW_LNCT_timestamp which is ignored by
20708 current GDB. */
20709 break;
20710 }
20711
20712 switch (content_type)
20713 {
20714 case DW_LNCT_path:
20715 if (string.has_value ())
20716 fe.name = *string;
20717 break;
20718 case DW_LNCT_directory_index:
20719 if (uint.has_value ())
20720 fe.d_index = (dir_index) *uint;
20721 break;
20722 case DW_LNCT_timestamp:
20723 if (uint.has_value ())
20724 fe.mod_time = *uint;
20725 break;
20726 case DW_LNCT_size:
20727 if (uint.has_value ())
20728 fe.length = *uint;
20729 break;
20730 case DW_LNCT_MD5:
20731 break;
20732 default:
20733 complaint (_("Unknown format content type %s"),
20734 pulongest (content_type));
20735 }
20736 }
20737
20738 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20739 }
20740
20741 *bufp = buf;
20742 }
20743
20744 /* Read the statement program header starting at OFFSET in
20745 .debug_line, or .debug_line.dwo. Return a pointer
20746 to a struct line_header, allocated using xmalloc.
20747 Returns NULL if there is a problem reading the header, e.g., if it
20748 has a version we don't understand.
20749
20750 NOTE: the strings in the include directory and file name tables of
20751 the returned object point into the dwarf line section buffer,
20752 and must not be freed. */
20753
20754 static line_header_up
20755 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20756 {
20757 const gdb_byte *line_ptr;
20758 unsigned int bytes_read, offset_size;
20759 int i;
20760 const char *cur_dir, *cur_file;
20761 struct dwarf2_section_info *section;
20762 bfd *abfd;
20763 struct dwarf2_per_objfile *dwarf2_per_objfile
20764 = cu->per_cu->dwarf2_per_objfile;
20765
20766 section = get_debug_line_section (cu);
20767 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20768 if (section->buffer == NULL)
20769 {
20770 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20771 complaint (_("missing .debug_line.dwo section"));
20772 else
20773 complaint (_("missing .debug_line section"));
20774 return 0;
20775 }
20776
20777 /* We can't do this until we know the section is non-empty.
20778 Only then do we know we have such a section. */
20779 abfd = get_section_bfd_owner (section);
20780
20781 /* Make sure that at least there's room for the total_length field.
20782 That could be 12 bytes long, but we're just going to fudge that. */
20783 if (to_underlying (sect_off) + 4 >= section->size)
20784 {
20785 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20786 return 0;
20787 }
20788
20789 line_header_up lh (new line_header ());
20790
20791 lh->sect_off = sect_off;
20792 lh->offset_in_dwz = cu->per_cu->is_dwz;
20793
20794 line_ptr = section->buffer + to_underlying (sect_off);
20795
20796 /* Read in the header. */
20797 lh->total_length =
20798 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20799 &bytes_read, &offset_size);
20800 line_ptr += bytes_read;
20801
20802 const gdb_byte *start_here = line_ptr;
20803
20804 if (line_ptr + lh->total_length > (section->buffer + section->size))
20805 {
20806 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20807 return 0;
20808 }
20809 lh->statement_program_end = start_here + lh->total_length;
20810 lh->version = read_2_bytes (abfd, line_ptr);
20811 line_ptr += 2;
20812 if (lh->version > 5)
20813 {
20814 /* This is a version we don't understand. The format could have
20815 changed in ways we don't handle properly so just punt. */
20816 complaint (_("unsupported version in .debug_line section"));
20817 return NULL;
20818 }
20819 if (lh->version >= 5)
20820 {
20821 gdb_byte segment_selector_size;
20822
20823 /* Skip address size. */
20824 read_1_byte (abfd, line_ptr);
20825 line_ptr += 1;
20826
20827 segment_selector_size = read_1_byte (abfd, line_ptr);
20828 line_ptr += 1;
20829 if (segment_selector_size != 0)
20830 {
20831 complaint (_("unsupported segment selector size %u "
20832 "in .debug_line section"),
20833 segment_selector_size);
20834 return NULL;
20835 }
20836 }
20837 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20838 line_ptr += offset_size;
20839 lh->statement_program_start = line_ptr + lh->header_length;
20840 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20841 line_ptr += 1;
20842 if (lh->version >= 4)
20843 {
20844 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20845 line_ptr += 1;
20846 }
20847 else
20848 lh->maximum_ops_per_instruction = 1;
20849
20850 if (lh->maximum_ops_per_instruction == 0)
20851 {
20852 lh->maximum_ops_per_instruction = 1;
20853 complaint (_("invalid maximum_ops_per_instruction "
20854 "in `.debug_line' section"));
20855 }
20856
20857 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20858 line_ptr += 1;
20859 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20860 line_ptr += 1;
20861 lh->line_range = read_1_byte (abfd, line_ptr);
20862 line_ptr += 1;
20863 lh->opcode_base = read_1_byte (abfd, line_ptr);
20864 line_ptr += 1;
20865 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20866
20867 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20868 for (i = 1; i < lh->opcode_base; ++i)
20869 {
20870 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20871 line_ptr += 1;
20872 }
20873
20874 if (lh->version >= 5)
20875 {
20876 /* Read directory table. */
20877 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20878 &cu->header,
20879 [] (struct line_header *header, const char *name,
20880 dir_index d_index, unsigned int mod_time,
20881 unsigned int length)
20882 {
20883 header->add_include_dir (name);
20884 });
20885
20886 /* Read file name table. */
20887 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20888 &cu->header,
20889 [] (struct line_header *header, const char *name,
20890 dir_index d_index, unsigned int mod_time,
20891 unsigned int length)
20892 {
20893 header->add_file_name (name, d_index, mod_time, length);
20894 });
20895 }
20896 else
20897 {
20898 /* Read directory table. */
20899 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20900 {
20901 line_ptr += bytes_read;
20902 lh->add_include_dir (cur_dir);
20903 }
20904 line_ptr += bytes_read;
20905
20906 /* Read file name table. */
20907 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20908 {
20909 unsigned int mod_time, length;
20910 dir_index d_index;
20911
20912 line_ptr += bytes_read;
20913 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20914 line_ptr += bytes_read;
20915 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20916 line_ptr += bytes_read;
20917 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20918 line_ptr += bytes_read;
20919
20920 lh->add_file_name (cur_file, d_index, mod_time, length);
20921 }
20922 line_ptr += bytes_read;
20923 }
20924
20925 if (line_ptr > (section->buffer + section->size))
20926 complaint (_("line number info header doesn't "
20927 "fit in `.debug_line' section"));
20928
20929 return lh;
20930 }
20931
20932 /* Subroutine of dwarf_decode_lines to simplify it.
20933 Return the file name of the psymtab for the given file_entry.
20934 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20935 If space for the result is malloc'd, *NAME_HOLDER will be set.
20936 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20937
20938 static const char *
20939 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20940 const dwarf2_psymtab *pst,
20941 const char *comp_dir,
20942 gdb::unique_xmalloc_ptr<char> *name_holder)
20943 {
20944 const char *include_name = fe.name;
20945 const char *include_name_to_compare = include_name;
20946 const char *pst_filename;
20947 int file_is_pst;
20948
20949 const char *dir_name = fe.include_dir (lh);
20950
20951 gdb::unique_xmalloc_ptr<char> hold_compare;
20952 if (!IS_ABSOLUTE_PATH (include_name)
20953 && (dir_name != NULL || comp_dir != NULL))
20954 {
20955 /* Avoid creating a duplicate psymtab for PST.
20956 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20957 Before we do the comparison, however, we need to account
20958 for DIR_NAME and COMP_DIR.
20959 First prepend dir_name (if non-NULL). If we still don't
20960 have an absolute path prepend comp_dir (if non-NULL).
20961 However, the directory we record in the include-file's
20962 psymtab does not contain COMP_DIR (to match the
20963 corresponding symtab(s)).
20964
20965 Example:
20966
20967 bash$ cd /tmp
20968 bash$ gcc -g ./hello.c
20969 include_name = "hello.c"
20970 dir_name = "."
20971 DW_AT_comp_dir = comp_dir = "/tmp"
20972 DW_AT_name = "./hello.c"
20973
20974 */
20975
20976 if (dir_name != NULL)
20977 {
20978 name_holder->reset (concat (dir_name, SLASH_STRING,
20979 include_name, (char *) NULL));
20980 include_name = name_holder->get ();
20981 include_name_to_compare = include_name;
20982 }
20983 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20984 {
20985 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20986 include_name, (char *) NULL));
20987 include_name_to_compare = hold_compare.get ();
20988 }
20989 }
20990
20991 pst_filename = pst->filename;
20992 gdb::unique_xmalloc_ptr<char> copied_name;
20993 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20994 {
20995 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20996 pst_filename, (char *) NULL));
20997 pst_filename = copied_name.get ();
20998 }
20999
21000 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21001
21002 if (file_is_pst)
21003 return NULL;
21004 return include_name;
21005 }
21006
21007 /* State machine to track the state of the line number program. */
21008
21009 class lnp_state_machine
21010 {
21011 public:
21012 /* Initialize a machine state for the start of a line number
21013 program. */
21014 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21015 bool record_lines_p);
21016
21017 file_entry *current_file ()
21018 {
21019 /* lh->file_names is 0-based, but the file name numbers in the
21020 statement program are 1-based. */
21021 return m_line_header->file_name_at (m_file);
21022 }
21023
21024 /* Record the line in the state machine. END_SEQUENCE is true if
21025 we're processing the end of a sequence. */
21026 void record_line (bool end_sequence);
21027
21028 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
21029 nop-out rest of the lines in this sequence. */
21030 void check_line_address (struct dwarf2_cu *cu,
21031 const gdb_byte *line_ptr,
21032 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21033
21034 void handle_set_discriminator (unsigned int discriminator)
21035 {
21036 m_discriminator = discriminator;
21037 m_line_has_non_zero_discriminator |= discriminator != 0;
21038 }
21039
21040 /* Handle DW_LNE_set_address. */
21041 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21042 {
21043 m_op_index = 0;
21044 address += baseaddr;
21045 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21046 }
21047
21048 /* Handle DW_LNS_advance_pc. */
21049 void handle_advance_pc (CORE_ADDR adjust);
21050
21051 /* Handle a special opcode. */
21052 void handle_special_opcode (unsigned char op_code);
21053
21054 /* Handle DW_LNS_advance_line. */
21055 void handle_advance_line (int line_delta)
21056 {
21057 advance_line (line_delta);
21058 }
21059
21060 /* Handle DW_LNS_set_file. */
21061 void handle_set_file (file_name_index file);
21062
21063 /* Handle DW_LNS_negate_stmt. */
21064 void handle_negate_stmt ()
21065 {
21066 m_is_stmt = !m_is_stmt;
21067 }
21068
21069 /* Handle DW_LNS_const_add_pc. */
21070 void handle_const_add_pc ();
21071
21072 /* Handle DW_LNS_fixed_advance_pc. */
21073 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21074 {
21075 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21076 m_op_index = 0;
21077 }
21078
21079 /* Handle DW_LNS_copy. */
21080 void handle_copy ()
21081 {
21082 record_line (false);
21083 m_discriminator = 0;
21084 }
21085
21086 /* Handle DW_LNE_end_sequence. */
21087 void handle_end_sequence ()
21088 {
21089 m_currently_recording_lines = true;
21090 }
21091
21092 private:
21093 /* Advance the line by LINE_DELTA. */
21094 void advance_line (int line_delta)
21095 {
21096 m_line += line_delta;
21097
21098 if (line_delta != 0)
21099 m_line_has_non_zero_discriminator = m_discriminator != 0;
21100 }
21101
21102 struct dwarf2_cu *m_cu;
21103
21104 gdbarch *m_gdbarch;
21105
21106 /* True if we're recording lines.
21107 Otherwise we're building partial symtabs and are just interested in
21108 finding include files mentioned by the line number program. */
21109 bool m_record_lines_p;
21110
21111 /* The line number header. */
21112 line_header *m_line_header;
21113
21114 /* These are part of the standard DWARF line number state machine,
21115 and initialized according to the DWARF spec. */
21116
21117 unsigned char m_op_index = 0;
21118 /* The line table index of the current file. */
21119 file_name_index m_file = 1;
21120 unsigned int m_line = 1;
21121
21122 /* These are initialized in the constructor. */
21123
21124 CORE_ADDR m_address;
21125 bool m_is_stmt;
21126 unsigned int m_discriminator;
21127
21128 /* Additional bits of state we need to track. */
21129
21130 /* The last file that we called dwarf2_start_subfile for.
21131 This is only used for TLLs. */
21132 unsigned int m_last_file = 0;
21133 /* The last file a line number was recorded for. */
21134 struct subfile *m_last_subfile = NULL;
21135
21136 /* When true, record the lines we decode. */
21137 bool m_currently_recording_lines = false;
21138
21139 /* The last line number that was recorded, used to coalesce
21140 consecutive entries for the same line. This can happen, for
21141 example, when discriminators are present. PR 17276. */
21142 unsigned int m_last_line = 0;
21143 bool m_line_has_non_zero_discriminator = false;
21144 };
21145
21146 void
21147 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21148 {
21149 CORE_ADDR addr_adj = (((m_op_index + adjust)
21150 / m_line_header->maximum_ops_per_instruction)
21151 * m_line_header->minimum_instruction_length);
21152 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21153 m_op_index = ((m_op_index + adjust)
21154 % m_line_header->maximum_ops_per_instruction);
21155 }
21156
21157 void
21158 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21159 {
21160 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21161 CORE_ADDR addr_adj = (((m_op_index
21162 + (adj_opcode / m_line_header->line_range))
21163 / m_line_header->maximum_ops_per_instruction)
21164 * m_line_header->minimum_instruction_length);
21165 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21166 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21167 % m_line_header->maximum_ops_per_instruction);
21168
21169 int line_delta = (m_line_header->line_base
21170 + (adj_opcode % m_line_header->line_range));
21171 advance_line (line_delta);
21172 record_line (false);
21173 m_discriminator = 0;
21174 }
21175
21176 void
21177 lnp_state_machine::handle_set_file (file_name_index file)
21178 {
21179 m_file = file;
21180
21181 const file_entry *fe = current_file ();
21182 if (fe == NULL)
21183 dwarf2_debug_line_missing_file_complaint ();
21184 else if (m_record_lines_p)
21185 {
21186 const char *dir = fe->include_dir (m_line_header);
21187
21188 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21189 m_line_has_non_zero_discriminator = m_discriminator != 0;
21190 dwarf2_start_subfile (m_cu, fe->name, dir);
21191 }
21192 }
21193
21194 void
21195 lnp_state_machine::handle_const_add_pc ()
21196 {
21197 CORE_ADDR adjust
21198 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21199
21200 CORE_ADDR addr_adj
21201 = (((m_op_index + adjust)
21202 / m_line_header->maximum_ops_per_instruction)
21203 * m_line_header->minimum_instruction_length);
21204
21205 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21206 m_op_index = ((m_op_index + adjust)
21207 % m_line_header->maximum_ops_per_instruction);
21208 }
21209
21210 /* Return non-zero if we should add LINE to the line number table.
21211 LINE is the line to add, LAST_LINE is the last line that was added,
21212 LAST_SUBFILE is the subfile for LAST_LINE.
21213 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21214 had a non-zero discriminator.
21215
21216 We have to be careful in the presence of discriminators.
21217 E.g., for this line:
21218
21219 for (i = 0; i < 100000; i++);
21220
21221 clang can emit four line number entries for that one line,
21222 each with a different discriminator.
21223 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21224
21225 However, we want gdb to coalesce all four entries into one.
21226 Otherwise the user could stepi into the middle of the line and
21227 gdb would get confused about whether the pc really was in the
21228 middle of the line.
21229
21230 Things are further complicated by the fact that two consecutive
21231 line number entries for the same line is a heuristic used by gcc
21232 to denote the end of the prologue. So we can't just discard duplicate
21233 entries, we have to be selective about it. The heuristic we use is
21234 that we only collapse consecutive entries for the same line if at least
21235 one of those entries has a non-zero discriminator. PR 17276.
21236
21237 Note: Addresses in the line number state machine can never go backwards
21238 within one sequence, thus this coalescing is ok. */
21239
21240 static int
21241 dwarf_record_line_p (struct dwarf2_cu *cu,
21242 unsigned int line, unsigned int last_line,
21243 int line_has_non_zero_discriminator,
21244 struct subfile *last_subfile)
21245 {
21246 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21247 return 1;
21248 if (line != last_line)
21249 return 1;
21250 /* Same line for the same file that we've seen already.
21251 As a last check, for pr 17276, only record the line if the line
21252 has never had a non-zero discriminator. */
21253 if (!line_has_non_zero_discriminator)
21254 return 1;
21255 return 0;
21256 }
21257
21258 /* Use the CU's builder to record line number LINE beginning at
21259 address ADDRESS in the line table of subfile SUBFILE. */
21260
21261 static void
21262 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21263 unsigned int line, CORE_ADDR address,
21264 struct dwarf2_cu *cu)
21265 {
21266 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21267
21268 if (dwarf_line_debug)
21269 {
21270 fprintf_unfiltered (gdb_stdlog,
21271 "Recording line %u, file %s, address %s\n",
21272 line, lbasename (subfile->name),
21273 paddress (gdbarch, address));
21274 }
21275
21276 if (cu != nullptr)
21277 cu->get_builder ()->record_line (subfile, line, addr);
21278 }
21279
21280 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21281 Mark the end of a set of line number records.
21282 The arguments are the same as for dwarf_record_line_1.
21283 If SUBFILE is NULL the request is ignored. */
21284
21285 static void
21286 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21287 CORE_ADDR address, struct dwarf2_cu *cu)
21288 {
21289 if (subfile == NULL)
21290 return;
21291
21292 if (dwarf_line_debug)
21293 {
21294 fprintf_unfiltered (gdb_stdlog,
21295 "Finishing current line, file %s, address %s\n",
21296 lbasename (subfile->name),
21297 paddress (gdbarch, address));
21298 }
21299
21300 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
21301 }
21302
21303 void
21304 lnp_state_machine::record_line (bool end_sequence)
21305 {
21306 if (dwarf_line_debug)
21307 {
21308 fprintf_unfiltered (gdb_stdlog,
21309 "Processing actual line %u: file %u,"
21310 " address %s, is_stmt %u, discrim %u%s\n",
21311 m_line, m_file,
21312 paddress (m_gdbarch, m_address),
21313 m_is_stmt, m_discriminator,
21314 (end_sequence ? "\t(end sequence)" : ""));
21315 }
21316
21317 file_entry *fe = current_file ();
21318
21319 if (fe == NULL)
21320 dwarf2_debug_line_missing_file_complaint ();
21321 /* For now we ignore lines not starting on an instruction boundary.
21322 But not when processing end_sequence for compatibility with the
21323 previous version of the code. */
21324 else if (m_op_index == 0 || end_sequence)
21325 {
21326 fe->included_p = 1;
21327 if (m_record_lines_p
21328 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
21329 {
21330 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21331 || end_sequence)
21332 {
21333 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21334 m_currently_recording_lines ? m_cu : nullptr);
21335 }
21336
21337 if (!end_sequence)
21338 {
21339 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21340 m_line_has_non_zero_discriminator,
21341 m_last_subfile))
21342 {
21343 buildsym_compunit *builder = m_cu->get_builder ();
21344 dwarf_record_line_1 (m_gdbarch,
21345 builder->get_current_subfile (),
21346 m_line, m_address,
21347 m_currently_recording_lines ? m_cu : nullptr);
21348 }
21349 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21350 m_last_line = m_line;
21351 }
21352 }
21353 }
21354 }
21355
21356 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21357 line_header *lh, bool record_lines_p)
21358 {
21359 m_cu = cu;
21360 m_gdbarch = arch;
21361 m_record_lines_p = record_lines_p;
21362 m_line_header = lh;
21363
21364 m_currently_recording_lines = true;
21365
21366 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21367 was a line entry for it so that the backend has a chance to adjust it
21368 and also record it in case it needs it. This is currently used by MIPS
21369 code, cf. `mips_adjust_dwarf2_line'. */
21370 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21371 m_is_stmt = lh->default_is_stmt;
21372 m_discriminator = 0;
21373 }
21374
21375 void
21376 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21377 const gdb_byte *line_ptr,
21378 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21379 {
21380 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21381 the pc range of the CU. However, we restrict the test to only ADDRESS
21382 values of zero to preserve GDB's previous behaviour which is to handle
21383 the specific case of a function being GC'd by the linker. */
21384
21385 if (address == 0 && address < unrelocated_lowpc)
21386 {
21387 /* This line table is for a function which has been
21388 GCd by the linker. Ignore it. PR gdb/12528 */
21389
21390 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21391 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21392
21393 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21394 line_offset, objfile_name (objfile));
21395 m_currently_recording_lines = false;
21396 /* Note: m_currently_recording_lines is left as false until we see
21397 DW_LNE_end_sequence. */
21398 }
21399 }
21400
21401 /* Subroutine of dwarf_decode_lines to simplify it.
21402 Process the line number information in LH.
21403 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21404 program in order to set included_p for every referenced header. */
21405
21406 static void
21407 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21408 const int decode_for_pst_p, CORE_ADDR lowpc)
21409 {
21410 const gdb_byte *line_ptr, *extended_end;
21411 const gdb_byte *line_end;
21412 unsigned int bytes_read, extended_len;
21413 unsigned char op_code, extended_op;
21414 CORE_ADDR baseaddr;
21415 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21416 bfd *abfd = objfile->obfd;
21417 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21418 /* True if we're recording line info (as opposed to building partial
21419 symtabs and just interested in finding include files mentioned by
21420 the line number program). */
21421 bool record_lines_p = !decode_for_pst_p;
21422
21423 baseaddr = objfile->text_section_offset ();
21424
21425 line_ptr = lh->statement_program_start;
21426 line_end = lh->statement_program_end;
21427
21428 /* Read the statement sequences until there's nothing left. */
21429 while (line_ptr < line_end)
21430 {
21431 /* The DWARF line number program state machine. Reset the state
21432 machine at the start of each sequence. */
21433 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21434 bool end_sequence = false;
21435
21436 if (record_lines_p)
21437 {
21438 /* Start a subfile for the current file of the state
21439 machine. */
21440 const file_entry *fe = state_machine.current_file ();
21441
21442 if (fe != NULL)
21443 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21444 }
21445
21446 /* Decode the table. */
21447 while (line_ptr < line_end && !end_sequence)
21448 {
21449 op_code = read_1_byte (abfd, line_ptr);
21450 line_ptr += 1;
21451
21452 if (op_code >= lh->opcode_base)
21453 {
21454 /* Special opcode. */
21455 state_machine.handle_special_opcode (op_code);
21456 }
21457 else switch (op_code)
21458 {
21459 case DW_LNS_extended_op:
21460 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21461 &bytes_read);
21462 line_ptr += bytes_read;
21463 extended_end = line_ptr + extended_len;
21464 extended_op = read_1_byte (abfd, line_ptr);
21465 line_ptr += 1;
21466 switch (extended_op)
21467 {
21468 case DW_LNE_end_sequence:
21469 state_machine.handle_end_sequence ();
21470 end_sequence = true;
21471 break;
21472 case DW_LNE_set_address:
21473 {
21474 CORE_ADDR address
21475 = read_address (abfd, line_ptr, cu, &bytes_read);
21476 line_ptr += bytes_read;
21477
21478 state_machine.check_line_address (cu, line_ptr,
21479 lowpc - baseaddr, address);
21480 state_machine.handle_set_address (baseaddr, address);
21481 }
21482 break;
21483 case DW_LNE_define_file:
21484 {
21485 const char *cur_file;
21486 unsigned int mod_time, length;
21487 dir_index dindex;
21488
21489 cur_file = read_direct_string (abfd, line_ptr,
21490 &bytes_read);
21491 line_ptr += bytes_read;
21492 dindex = (dir_index)
21493 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21494 line_ptr += bytes_read;
21495 mod_time =
21496 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21497 line_ptr += bytes_read;
21498 length =
21499 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21500 line_ptr += bytes_read;
21501 lh->add_file_name (cur_file, dindex, mod_time, length);
21502 }
21503 break;
21504 case DW_LNE_set_discriminator:
21505 {
21506 /* The discriminator is not interesting to the
21507 debugger; just ignore it. We still need to
21508 check its value though:
21509 if there are consecutive entries for the same
21510 (non-prologue) line we want to coalesce them.
21511 PR 17276. */
21512 unsigned int discr
21513 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21514 line_ptr += bytes_read;
21515
21516 state_machine.handle_set_discriminator (discr);
21517 }
21518 break;
21519 default:
21520 complaint (_("mangled .debug_line section"));
21521 return;
21522 }
21523 /* Make sure that we parsed the extended op correctly. If e.g.
21524 we expected a different address size than the producer used,
21525 we may have read the wrong number of bytes. */
21526 if (line_ptr != extended_end)
21527 {
21528 complaint (_("mangled .debug_line section"));
21529 return;
21530 }
21531 break;
21532 case DW_LNS_copy:
21533 state_machine.handle_copy ();
21534 break;
21535 case DW_LNS_advance_pc:
21536 {
21537 CORE_ADDR adjust
21538 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21539 line_ptr += bytes_read;
21540
21541 state_machine.handle_advance_pc (adjust);
21542 }
21543 break;
21544 case DW_LNS_advance_line:
21545 {
21546 int line_delta
21547 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21548 line_ptr += bytes_read;
21549
21550 state_machine.handle_advance_line (line_delta);
21551 }
21552 break;
21553 case DW_LNS_set_file:
21554 {
21555 file_name_index file
21556 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21557 &bytes_read);
21558 line_ptr += bytes_read;
21559
21560 state_machine.handle_set_file (file);
21561 }
21562 break;
21563 case DW_LNS_set_column:
21564 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21565 line_ptr += bytes_read;
21566 break;
21567 case DW_LNS_negate_stmt:
21568 state_machine.handle_negate_stmt ();
21569 break;
21570 case DW_LNS_set_basic_block:
21571 break;
21572 /* Add to the address register of the state machine the
21573 address increment value corresponding to special opcode
21574 255. I.e., this value is scaled by the minimum
21575 instruction length since special opcode 255 would have
21576 scaled the increment. */
21577 case DW_LNS_const_add_pc:
21578 state_machine.handle_const_add_pc ();
21579 break;
21580 case DW_LNS_fixed_advance_pc:
21581 {
21582 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21583 line_ptr += 2;
21584
21585 state_machine.handle_fixed_advance_pc (addr_adj);
21586 }
21587 break;
21588 default:
21589 {
21590 /* Unknown standard opcode, ignore it. */
21591 int i;
21592
21593 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21594 {
21595 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21596 line_ptr += bytes_read;
21597 }
21598 }
21599 }
21600 }
21601
21602 if (!end_sequence)
21603 dwarf2_debug_line_missing_end_sequence_complaint ();
21604
21605 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21606 in which case we still finish recording the last line). */
21607 state_machine.record_line (true);
21608 }
21609 }
21610
21611 /* Decode the Line Number Program (LNP) for the given line_header
21612 structure and CU. The actual information extracted and the type
21613 of structures created from the LNP depends on the value of PST.
21614
21615 1. If PST is NULL, then this procedure uses the data from the program
21616 to create all necessary symbol tables, and their linetables.
21617
21618 2. If PST is not NULL, this procedure reads the program to determine
21619 the list of files included by the unit represented by PST, and
21620 builds all the associated partial symbol tables.
21621
21622 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21623 It is used for relative paths in the line table.
21624 NOTE: When processing partial symtabs (pst != NULL),
21625 comp_dir == pst->dirname.
21626
21627 NOTE: It is important that psymtabs have the same file name (via strcmp)
21628 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21629 symtab we don't use it in the name of the psymtabs we create.
21630 E.g. expand_line_sal requires this when finding psymtabs to expand.
21631 A good testcase for this is mb-inline.exp.
21632
21633 LOWPC is the lowest address in CU (or 0 if not known).
21634
21635 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21636 for its PC<->lines mapping information. Otherwise only the filename
21637 table is read in. */
21638
21639 static void
21640 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21641 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21642 CORE_ADDR lowpc, int decode_mapping)
21643 {
21644 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21645 const int decode_for_pst_p = (pst != NULL);
21646
21647 if (decode_mapping)
21648 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21649
21650 if (decode_for_pst_p)
21651 {
21652 /* Now that we're done scanning the Line Header Program, we can
21653 create the psymtab of each included file. */
21654 for (auto &file_entry : lh->file_names ())
21655 if (file_entry.included_p == 1)
21656 {
21657 gdb::unique_xmalloc_ptr<char> name_holder;
21658 const char *include_name =
21659 psymtab_include_file_name (lh, file_entry, pst,
21660 comp_dir, &name_holder);
21661 if (include_name != NULL)
21662 dwarf2_create_include_psymtab (include_name, pst, objfile);
21663 }
21664 }
21665 else
21666 {
21667 /* Make sure a symtab is created for every file, even files
21668 which contain only variables (i.e. no code with associated
21669 line numbers). */
21670 buildsym_compunit *builder = cu->get_builder ();
21671 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21672
21673 for (auto &fe : lh->file_names ())
21674 {
21675 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21676 if (builder->get_current_subfile ()->symtab == NULL)
21677 {
21678 builder->get_current_subfile ()->symtab
21679 = allocate_symtab (cust,
21680 builder->get_current_subfile ()->name);
21681 }
21682 fe.symtab = builder->get_current_subfile ()->symtab;
21683 }
21684 }
21685 }
21686
21687 /* Start a subfile for DWARF. FILENAME is the name of the file and
21688 DIRNAME the name of the source directory which contains FILENAME
21689 or NULL if not known.
21690 This routine tries to keep line numbers from identical absolute and
21691 relative file names in a common subfile.
21692
21693 Using the `list' example from the GDB testsuite, which resides in
21694 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21695 of /srcdir/list0.c yields the following debugging information for list0.c:
21696
21697 DW_AT_name: /srcdir/list0.c
21698 DW_AT_comp_dir: /compdir
21699 files.files[0].name: list0.h
21700 files.files[0].dir: /srcdir
21701 files.files[1].name: list0.c
21702 files.files[1].dir: /srcdir
21703
21704 The line number information for list0.c has to end up in a single
21705 subfile, so that `break /srcdir/list0.c:1' works as expected.
21706 start_subfile will ensure that this happens provided that we pass the
21707 concatenation of files.files[1].dir and files.files[1].name as the
21708 subfile's name. */
21709
21710 static void
21711 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21712 const char *dirname)
21713 {
21714 gdb::unique_xmalloc_ptr<char> copy;
21715
21716 /* In order not to lose the line information directory,
21717 we concatenate it to the filename when it makes sense.
21718 Note that the Dwarf3 standard says (speaking of filenames in line
21719 information): ``The directory index is ignored for file names
21720 that represent full path names''. Thus ignoring dirname in the
21721 `else' branch below isn't an issue. */
21722
21723 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21724 {
21725 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21726 filename = copy.get ();
21727 }
21728
21729 cu->get_builder ()->start_subfile (filename);
21730 }
21731
21732 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21733 buildsym_compunit constructor. */
21734
21735 struct compunit_symtab *
21736 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21737 CORE_ADDR low_pc)
21738 {
21739 gdb_assert (m_builder == nullptr);
21740
21741 m_builder.reset (new struct buildsym_compunit
21742 (per_cu->dwarf2_per_objfile->objfile,
21743 name, comp_dir, language, low_pc));
21744
21745 list_in_scope = get_builder ()->get_file_symbols ();
21746
21747 get_builder ()->record_debugformat ("DWARF 2");
21748 get_builder ()->record_producer (producer);
21749
21750 processing_has_namespace_info = false;
21751
21752 return get_builder ()->get_compunit_symtab ();
21753 }
21754
21755 static void
21756 var_decode_location (struct attribute *attr, struct symbol *sym,
21757 struct dwarf2_cu *cu)
21758 {
21759 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21760 struct comp_unit_head *cu_header = &cu->header;
21761
21762 /* NOTE drow/2003-01-30: There used to be a comment and some special
21763 code here to turn a symbol with DW_AT_external and a
21764 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21765 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21766 with some versions of binutils) where shared libraries could have
21767 relocations against symbols in their debug information - the
21768 minimal symbol would have the right address, but the debug info
21769 would not. It's no longer necessary, because we will explicitly
21770 apply relocations when we read in the debug information now. */
21771
21772 /* A DW_AT_location attribute with no contents indicates that a
21773 variable has been optimized away. */
21774 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21775 {
21776 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21777 return;
21778 }
21779
21780 /* Handle one degenerate form of location expression specially, to
21781 preserve GDB's previous behavior when section offsets are
21782 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21783 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21784
21785 if (attr_form_is_block (attr)
21786 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21787 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21788 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21789 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21790 && (DW_BLOCK (attr)->size
21791 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21792 {
21793 unsigned int dummy;
21794
21795 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21796 SET_SYMBOL_VALUE_ADDRESS (sym,
21797 read_address (objfile->obfd,
21798 DW_BLOCK (attr)->data + 1,
21799 cu, &dummy));
21800 else
21801 SET_SYMBOL_VALUE_ADDRESS
21802 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21803 &dummy));
21804 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21805 fixup_symbol_section (sym, objfile);
21806 SET_SYMBOL_VALUE_ADDRESS
21807 (sym,
21808 SYMBOL_VALUE_ADDRESS (sym)
21809 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21810 return;
21811 }
21812
21813 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21814 expression evaluator, and use LOC_COMPUTED only when necessary
21815 (i.e. when the value of a register or memory location is
21816 referenced, or a thread-local block, etc.). Then again, it might
21817 not be worthwhile. I'm assuming that it isn't unless performance
21818 or memory numbers show me otherwise. */
21819
21820 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21821
21822 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21823 cu->has_loclist = true;
21824 }
21825
21826 /* Given a pointer to a DWARF information entry, figure out if we need
21827 to make a symbol table entry for it, and if so, create a new entry
21828 and return a pointer to it.
21829 If TYPE is NULL, determine symbol type from the die, otherwise
21830 used the passed type.
21831 If SPACE is not NULL, use it to hold the new symbol. If it is
21832 NULL, allocate a new symbol on the objfile's obstack. */
21833
21834 static struct symbol *
21835 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21836 struct symbol *space)
21837 {
21838 struct dwarf2_per_objfile *dwarf2_per_objfile
21839 = cu->per_cu->dwarf2_per_objfile;
21840 struct objfile *objfile = dwarf2_per_objfile->objfile;
21841 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21842 struct symbol *sym = NULL;
21843 const char *name;
21844 struct attribute *attr = NULL;
21845 struct attribute *attr2 = NULL;
21846 CORE_ADDR baseaddr;
21847 struct pending **list_to_add = NULL;
21848
21849 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21850
21851 baseaddr = objfile->text_section_offset ();
21852
21853 name = dwarf2_name (die, cu);
21854 if (name)
21855 {
21856 const char *linkagename;
21857 int suppress_add = 0;
21858
21859 if (space)
21860 sym = space;
21861 else
21862 sym = allocate_symbol (objfile);
21863 OBJSTAT (objfile, n_syms++);
21864
21865 /* Cache this symbol's name and the name's demangled form (if any). */
21866 sym->set_language (cu->language, &objfile->objfile_obstack);
21867 linkagename = dwarf2_physname (name, die, cu);
21868 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21869
21870 /* Fortran does not have mangling standard and the mangling does differ
21871 between gfortran, iFort etc. */
21872 if (cu->language == language_fortran
21873 && symbol_get_demangled_name (sym) == NULL)
21874 symbol_set_demangled_name (sym,
21875 dwarf2_full_name (name, die, cu),
21876 NULL);
21877
21878 /* Default assumptions.
21879 Use the passed type or decode it from the die. */
21880 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21881 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21882 if (type != NULL)
21883 SYMBOL_TYPE (sym) = type;
21884 else
21885 SYMBOL_TYPE (sym) = die_type (die, cu);
21886 attr = dwarf2_attr (die,
21887 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21888 cu);
21889 if (attr != nullptr)
21890 {
21891 SYMBOL_LINE (sym) = DW_UNSND (attr);
21892 }
21893
21894 attr = dwarf2_attr (die,
21895 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21896 cu);
21897 if (attr != nullptr)
21898 {
21899 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21900 struct file_entry *fe;
21901
21902 if (cu->line_header != NULL)
21903 fe = cu->line_header->file_name_at (file_index);
21904 else
21905 fe = NULL;
21906
21907 if (fe == NULL)
21908 complaint (_("file index out of range"));
21909 else
21910 symbol_set_symtab (sym, fe->symtab);
21911 }
21912
21913 switch (die->tag)
21914 {
21915 case DW_TAG_label:
21916 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21917 if (attr != nullptr)
21918 {
21919 CORE_ADDR addr;
21920
21921 addr = attr_value_as_address (attr);
21922 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21923 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21924 }
21925 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21926 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21927 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21928 add_symbol_to_list (sym, cu->list_in_scope);
21929 break;
21930 case DW_TAG_subprogram:
21931 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21932 finish_block. */
21933 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21934 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21935 if ((attr2 && (DW_UNSND (attr2) != 0))
21936 || cu->language == language_ada
21937 || cu->language == language_fortran)
21938 {
21939 /* Subprograms marked external are stored as a global symbol.
21940 Ada and Fortran subprograms, whether marked external or
21941 not, are always stored as a global symbol, because we want
21942 to be able to access them globally. For instance, we want
21943 to be able to break on a nested subprogram without having
21944 to specify the context. */
21945 list_to_add = cu->get_builder ()->get_global_symbols ();
21946 }
21947 else
21948 {
21949 list_to_add = cu->list_in_scope;
21950 }
21951 break;
21952 case DW_TAG_inlined_subroutine:
21953 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21954 finish_block. */
21955 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21956 SYMBOL_INLINED (sym) = 1;
21957 list_to_add = cu->list_in_scope;
21958 break;
21959 case DW_TAG_template_value_param:
21960 suppress_add = 1;
21961 /* Fall through. */
21962 case DW_TAG_constant:
21963 case DW_TAG_variable:
21964 case DW_TAG_member:
21965 /* Compilation with minimal debug info may result in
21966 variables with missing type entries. Change the
21967 misleading `void' type to something sensible. */
21968 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21969 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21970
21971 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21972 /* In the case of DW_TAG_member, we should only be called for
21973 static const members. */
21974 if (die->tag == DW_TAG_member)
21975 {
21976 /* dwarf2_add_field uses die_is_declaration,
21977 so we do the same. */
21978 gdb_assert (die_is_declaration (die, cu));
21979 gdb_assert (attr);
21980 }
21981 if (attr != nullptr)
21982 {
21983 dwarf2_const_value (attr, sym, cu);
21984 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21985 if (!suppress_add)
21986 {
21987 if (attr2 && (DW_UNSND (attr2) != 0))
21988 list_to_add = cu->get_builder ()->get_global_symbols ();
21989 else
21990 list_to_add = cu->list_in_scope;
21991 }
21992 break;
21993 }
21994 attr = dwarf2_attr (die, DW_AT_location, cu);
21995 if (attr != nullptr)
21996 {
21997 var_decode_location (attr, sym, cu);
21998 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21999
22000 /* Fortran explicitly imports any global symbols to the local
22001 scope by DW_TAG_common_block. */
22002 if (cu->language == language_fortran && die->parent
22003 && die->parent->tag == DW_TAG_common_block)
22004 attr2 = NULL;
22005
22006 if (SYMBOL_CLASS (sym) == LOC_STATIC
22007 && SYMBOL_VALUE_ADDRESS (sym) == 0
22008 && !dwarf2_per_objfile->has_section_at_zero)
22009 {
22010 /* When a static variable is eliminated by the linker,
22011 the corresponding debug information is not stripped
22012 out, but the variable address is set to null;
22013 do not add such variables into symbol table. */
22014 }
22015 else if (attr2 && (DW_UNSND (attr2) != 0))
22016 {
22017 if (SYMBOL_CLASS (sym) == LOC_STATIC
22018 && (objfile->flags & OBJF_MAINLINE) == 0
22019 && dwarf2_per_objfile->can_copy)
22020 {
22021 /* A global static variable might be subject to
22022 copy relocation. We first check for a local
22023 minsym, though, because maybe the symbol was
22024 marked hidden, in which case this would not
22025 apply. */
22026 bound_minimal_symbol found
22027 = (lookup_minimal_symbol_linkage
22028 (sym->linkage_name (), objfile));
22029 if (found.minsym != nullptr)
22030 sym->maybe_copied = 1;
22031 }
22032
22033 /* A variable with DW_AT_external is never static,
22034 but it may be block-scoped. */
22035 list_to_add
22036 = ((cu->list_in_scope
22037 == cu->get_builder ()->get_file_symbols ())
22038 ? cu->get_builder ()->get_global_symbols ()
22039 : cu->list_in_scope);
22040 }
22041 else
22042 list_to_add = cu->list_in_scope;
22043 }
22044 else
22045 {
22046 /* We do not know the address of this symbol.
22047 If it is an external symbol and we have type information
22048 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22049 The address of the variable will then be determined from
22050 the minimal symbol table whenever the variable is
22051 referenced. */
22052 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22053
22054 /* Fortran explicitly imports any global symbols to the local
22055 scope by DW_TAG_common_block. */
22056 if (cu->language == language_fortran && die->parent
22057 && die->parent->tag == DW_TAG_common_block)
22058 {
22059 /* SYMBOL_CLASS doesn't matter here because
22060 read_common_block is going to reset it. */
22061 if (!suppress_add)
22062 list_to_add = cu->list_in_scope;
22063 }
22064 else if (attr2 && (DW_UNSND (attr2) != 0)
22065 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22066 {
22067 /* A variable with DW_AT_external is never static, but it
22068 may be block-scoped. */
22069 list_to_add
22070 = ((cu->list_in_scope
22071 == cu->get_builder ()->get_file_symbols ())
22072 ? cu->get_builder ()->get_global_symbols ()
22073 : cu->list_in_scope);
22074
22075 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22076 }
22077 else if (!die_is_declaration (die, cu))
22078 {
22079 /* Use the default LOC_OPTIMIZED_OUT class. */
22080 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22081 if (!suppress_add)
22082 list_to_add = cu->list_in_scope;
22083 }
22084 }
22085 break;
22086 case DW_TAG_formal_parameter:
22087 {
22088 /* If we are inside a function, mark this as an argument. If
22089 not, we might be looking at an argument to an inlined function
22090 when we do not have enough information to show inlined frames;
22091 pretend it's a local variable in that case so that the user can
22092 still see it. */
22093 struct context_stack *curr
22094 = cu->get_builder ()->get_current_context_stack ();
22095 if (curr != nullptr && curr->name != nullptr)
22096 SYMBOL_IS_ARGUMENT (sym) = 1;
22097 attr = dwarf2_attr (die, DW_AT_location, cu);
22098 if (attr != nullptr)
22099 {
22100 var_decode_location (attr, sym, cu);
22101 }
22102 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22103 if (attr != nullptr)
22104 {
22105 dwarf2_const_value (attr, sym, cu);
22106 }
22107
22108 list_to_add = cu->list_in_scope;
22109 }
22110 break;
22111 case DW_TAG_unspecified_parameters:
22112 /* From varargs functions; gdb doesn't seem to have any
22113 interest in this information, so just ignore it for now.
22114 (FIXME?) */
22115 break;
22116 case DW_TAG_template_type_param:
22117 suppress_add = 1;
22118 /* Fall through. */
22119 case DW_TAG_class_type:
22120 case DW_TAG_interface_type:
22121 case DW_TAG_structure_type:
22122 case DW_TAG_union_type:
22123 case DW_TAG_set_type:
22124 case DW_TAG_enumeration_type:
22125 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22126 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22127
22128 {
22129 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22130 really ever be static objects: otherwise, if you try
22131 to, say, break of a class's method and you're in a file
22132 which doesn't mention that class, it won't work unless
22133 the check for all static symbols in lookup_symbol_aux
22134 saves you. See the OtherFileClass tests in
22135 gdb.c++/namespace.exp. */
22136
22137 if (!suppress_add)
22138 {
22139 buildsym_compunit *builder = cu->get_builder ();
22140 list_to_add
22141 = (cu->list_in_scope == builder->get_file_symbols ()
22142 && cu->language == language_cplus
22143 ? builder->get_global_symbols ()
22144 : cu->list_in_scope);
22145
22146 /* The semantics of C++ state that "struct foo {
22147 ... }" also defines a typedef for "foo". */
22148 if (cu->language == language_cplus
22149 || cu->language == language_ada
22150 || cu->language == language_d
22151 || cu->language == language_rust)
22152 {
22153 /* The symbol's name is already allocated along
22154 with this objfile, so we don't need to
22155 duplicate it for the type. */
22156 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
22157 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
22158 }
22159 }
22160 }
22161 break;
22162 case DW_TAG_typedef:
22163 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22164 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22165 list_to_add = cu->list_in_scope;
22166 break;
22167 case DW_TAG_base_type:
22168 case DW_TAG_subrange_type:
22169 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22170 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22171 list_to_add = cu->list_in_scope;
22172 break;
22173 case DW_TAG_enumerator:
22174 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22175 if (attr != nullptr)
22176 {
22177 dwarf2_const_value (attr, sym, cu);
22178 }
22179 {
22180 /* NOTE: carlton/2003-11-10: See comment above in the
22181 DW_TAG_class_type, etc. block. */
22182
22183 list_to_add
22184 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22185 && cu->language == language_cplus
22186 ? cu->get_builder ()->get_global_symbols ()
22187 : cu->list_in_scope);
22188 }
22189 break;
22190 case DW_TAG_imported_declaration:
22191 case DW_TAG_namespace:
22192 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22193 list_to_add = cu->get_builder ()->get_global_symbols ();
22194 break;
22195 case DW_TAG_module:
22196 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22197 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22198 list_to_add = cu->get_builder ()->get_global_symbols ();
22199 break;
22200 case DW_TAG_common_block:
22201 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22202 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22203 add_symbol_to_list (sym, cu->list_in_scope);
22204 break;
22205 default:
22206 /* Not a tag we recognize. Hopefully we aren't processing
22207 trash data, but since we must specifically ignore things
22208 we don't recognize, there is nothing else we should do at
22209 this point. */
22210 complaint (_("unsupported tag: '%s'"),
22211 dwarf_tag_name (die->tag));
22212 break;
22213 }
22214
22215 if (suppress_add)
22216 {
22217 sym->hash_next = objfile->template_symbols;
22218 objfile->template_symbols = sym;
22219 list_to_add = NULL;
22220 }
22221
22222 if (list_to_add != NULL)
22223 add_symbol_to_list (sym, list_to_add);
22224
22225 /* For the benefit of old versions of GCC, check for anonymous
22226 namespaces based on the demangled name. */
22227 if (!cu->processing_has_namespace_info
22228 && cu->language == language_cplus)
22229 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22230 }
22231 return (sym);
22232 }
22233
22234 /* Given an attr with a DW_FORM_dataN value in host byte order,
22235 zero-extend it as appropriate for the symbol's type. The DWARF
22236 standard (v4) is not entirely clear about the meaning of using
22237 DW_FORM_dataN for a constant with a signed type, where the type is
22238 wider than the data. The conclusion of a discussion on the DWARF
22239 list was that this is unspecified. We choose to always zero-extend
22240 because that is the interpretation long in use by GCC. */
22241
22242 static gdb_byte *
22243 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22244 struct dwarf2_cu *cu, LONGEST *value, int bits)
22245 {
22246 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22247 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22248 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22249 LONGEST l = DW_UNSND (attr);
22250
22251 if (bits < sizeof (*value) * 8)
22252 {
22253 l &= ((LONGEST) 1 << bits) - 1;
22254 *value = l;
22255 }
22256 else if (bits == sizeof (*value) * 8)
22257 *value = l;
22258 else
22259 {
22260 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22261 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22262 return bytes;
22263 }
22264
22265 return NULL;
22266 }
22267
22268 /* Read a constant value from an attribute. Either set *VALUE, or if
22269 the value does not fit in *VALUE, set *BYTES - either already
22270 allocated on the objfile obstack, or newly allocated on OBSTACK,
22271 or, set *BATON, if we translated the constant to a location
22272 expression. */
22273
22274 static void
22275 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22276 const char *name, struct obstack *obstack,
22277 struct dwarf2_cu *cu,
22278 LONGEST *value, const gdb_byte **bytes,
22279 struct dwarf2_locexpr_baton **baton)
22280 {
22281 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22282 struct comp_unit_head *cu_header = &cu->header;
22283 struct dwarf_block *blk;
22284 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22285 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22286
22287 *value = 0;
22288 *bytes = NULL;
22289 *baton = NULL;
22290
22291 switch (attr->form)
22292 {
22293 case DW_FORM_addr:
22294 case DW_FORM_addrx:
22295 case DW_FORM_GNU_addr_index:
22296 {
22297 gdb_byte *data;
22298
22299 if (TYPE_LENGTH (type) != cu_header->addr_size)
22300 dwarf2_const_value_length_mismatch_complaint (name,
22301 cu_header->addr_size,
22302 TYPE_LENGTH (type));
22303 /* Symbols of this form are reasonably rare, so we just
22304 piggyback on the existing location code rather than writing
22305 a new implementation of symbol_computed_ops. */
22306 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22307 (*baton)->per_cu = cu->per_cu;
22308 gdb_assert ((*baton)->per_cu);
22309
22310 (*baton)->size = 2 + cu_header->addr_size;
22311 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22312 (*baton)->data = data;
22313
22314 data[0] = DW_OP_addr;
22315 store_unsigned_integer (&data[1], cu_header->addr_size,
22316 byte_order, DW_ADDR (attr));
22317 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22318 }
22319 break;
22320 case DW_FORM_string:
22321 case DW_FORM_strp:
22322 case DW_FORM_strx:
22323 case DW_FORM_GNU_str_index:
22324 case DW_FORM_GNU_strp_alt:
22325 /* DW_STRING is already allocated on the objfile obstack, point
22326 directly to it. */
22327 *bytes = (const gdb_byte *) DW_STRING (attr);
22328 break;
22329 case DW_FORM_block1:
22330 case DW_FORM_block2:
22331 case DW_FORM_block4:
22332 case DW_FORM_block:
22333 case DW_FORM_exprloc:
22334 case DW_FORM_data16:
22335 blk = DW_BLOCK (attr);
22336 if (TYPE_LENGTH (type) != blk->size)
22337 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22338 TYPE_LENGTH (type));
22339 *bytes = blk->data;
22340 break;
22341
22342 /* The DW_AT_const_value attributes are supposed to carry the
22343 symbol's value "represented as it would be on the target
22344 architecture." By the time we get here, it's already been
22345 converted to host endianness, so we just need to sign- or
22346 zero-extend it as appropriate. */
22347 case DW_FORM_data1:
22348 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22349 break;
22350 case DW_FORM_data2:
22351 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22352 break;
22353 case DW_FORM_data4:
22354 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22355 break;
22356 case DW_FORM_data8:
22357 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22358 break;
22359
22360 case DW_FORM_sdata:
22361 case DW_FORM_implicit_const:
22362 *value = DW_SND (attr);
22363 break;
22364
22365 case DW_FORM_udata:
22366 *value = DW_UNSND (attr);
22367 break;
22368
22369 default:
22370 complaint (_("unsupported const value attribute form: '%s'"),
22371 dwarf_form_name (attr->form));
22372 *value = 0;
22373 break;
22374 }
22375 }
22376
22377
22378 /* Copy constant value from an attribute to a symbol. */
22379
22380 static void
22381 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22382 struct dwarf2_cu *cu)
22383 {
22384 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22385 LONGEST value;
22386 const gdb_byte *bytes;
22387 struct dwarf2_locexpr_baton *baton;
22388
22389 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22390 sym->print_name (),
22391 &objfile->objfile_obstack, cu,
22392 &value, &bytes, &baton);
22393
22394 if (baton != NULL)
22395 {
22396 SYMBOL_LOCATION_BATON (sym) = baton;
22397 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22398 }
22399 else if (bytes != NULL)
22400 {
22401 SYMBOL_VALUE_BYTES (sym) = bytes;
22402 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22403 }
22404 else
22405 {
22406 SYMBOL_VALUE (sym) = value;
22407 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22408 }
22409 }
22410
22411 /* Return the type of the die in question using its DW_AT_type attribute. */
22412
22413 static struct type *
22414 die_type (struct die_info *die, struct dwarf2_cu *cu)
22415 {
22416 struct attribute *type_attr;
22417
22418 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22419 if (!type_attr)
22420 {
22421 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22422 /* A missing DW_AT_type represents a void type. */
22423 return objfile_type (objfile)->builtin_void;
22424 }
22425
22426 return lookup_die_type (die, type_attr, cu);
22427 }
22428
22429 /* True iff CU's producer generates GNAT Ada auxiliary information
22430 that allows to find parallel types through that information instead
22431 of having to do expensive parallel lookups by type name. */
22432
22433 static int
22434 need_gnat_info (struct dwarf2_cu *cu)
22435 {
22436 /* Assume that the Ada compiler was GNAT, which always produces
22437 the auxiliary information. */
22438 return (cu->language == language_ada);
22439 }
22440
22441 /* Return the auxiliary type of the die in question using its
22442 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22443 attribute is not present. */
22444
22445 static struct type *
22446 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22447 {
22448 struct attribute *type_attr;
22449
22450 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22451 if (!type_attr)
22452 return NULL;
22453
22454 return lookup_die_type (die, type_attr, cu);
22455 }
22456
22457 /* If DIE has a descriptive_type attribute, then set the TYPE's
22458 descriptive type accordingly. */
22459
22460 static void
22461 set_descriptive_type (struct type *type, struct die_info *die,
22462 struct dwarf2_cu *cu)
22463 {
22464 struct type *descriptive_type = die_descriptive_type (die, cu);
22465
22466 if (descriptive_type)
22467 {
22468 ALLOCATE_GNAT_AUX_TYPE (type);
22469 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22470 }
22471 }
22472
22473 /* Return the containing type of the die in question using its
22474 DW_AT_containing_type attribute. */
22475
22476 static struct type *
22477 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22478 {
22479 struct attribute *type_attr;
22480 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22481
22482 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22483 if (!type_attr)
22484 error (_("Dwarf Error: Problem turning containing type into gdb type "
22485 "[in module %s]"), objfile_name (objfile));
22486
22487 return lookup_die_type (die, type_attr, cu);
22488 }
22489
22490 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22491
22492 static struct type *
22493 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22494 {
22495 struct dwarf2_per_objfile *dwarf2_per_objfile
22496 = cu->per_cu->dwarf2_per_objfile;
22497 struct objfile *objfile = dwarf2_per_objfile->objfile;
22498 char *saved;
22499
22500 std::string message
22501 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22502 objfile_name (objfile),
22503 sect_offset_str (cu->header.sect_off),
22504 sect_offset_str (die->sect_off));
22505 saved = obstack_strdup (&objfile->objfile_obstack, message);
22506
22507 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22508 }
22509
22510 /* Look up the type of DIE in CU using its type attribute ATTR.
22511 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22512 DW_AT_containing_type.
22513 If there is no type substitute an error marker. */
22514
22515 static struct type *
22516 lookup_die_type (struct die_info *die, const struct attribute *attr,
22517 struct dwarf2_cu *cu)
22518 {
22519 struct dwarf2_per_objfile *dwarf2_per_objfile
22520 = cu->per_cu->dwarf2_per_objfile;
22521 struct objfile *objfile = dwarf2_per_objfile->objfile;
22522 struct type *this_type;
22523
22524 gdb_assert (attr->name == DW_AT_type
22525 || attr->name == DW_AT_GNAT_descriptive_type
22526 || attr->name == DW_AT_containing_type);
22527
22528 /* First see if we have it cached. */
22529
22530 if (attr->form == DW_FORM_GNU_ref_alt)
22531 {
22532 struct dwarf2_per_cu_data *per_cu;
22533 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22534
22535 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22536 dwarf2_per_objfile);
22537 this_type = get_die_type_at_offset (sect_off, per_cu);
22538 }
22539 else if (attr_form_is_ref (attr))
22540 {
22541 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22542
22543 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22544 }
22545 else if (attr->form == DW_FORM_ref_sig8)
22546 {
22547 ULONGEST signature = DW_SIGNATURE (attr);
22548
22549 return get_signatured_type (die, signature, cu);
22550 }
22551 else
22552 {
22553 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22554 " at %s [in module %s]"),
22555 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22556 objfile_name (objfile));
22557 return build_error_marker_type (cu, die);
22558 }
22559
22560 /* If not cached we need to read it in. */
22561
22562 if (this_type == NULL)
22563 {
22564 struct die_info *type_die = NULL;
22565 struct dwarf2_cu *type_cu = cu;
22566
22567 if (attr_form_is_ref (attr))
22568 type_die = follow_die_ref (die, attr, &type_cu);
22569 if (type_die == NULL)
22570 return build_error_marker_type (cu, die);
22571 /* If we find the type now, it's probably because the type came
22572 from an inter-CU reference and the type's CU got expanded before
22573 ours. */
22574 this_type = read_type_die (type_die, type_cu);
22575 }
22576
22577 /* If we still don't have a type use an error marker. */
22578
22579 if (this_type == NULL)
22580 return build_error_marker_type (cu, die);
22581
22582 return this_type;
22583 }
22584
22585 /* Return the type in DIE, CU.
22586 Returns NULL for invalid types.
22587
22588 This first does a lookup in die_type_hash,
22589 and only reads the die in if necessary.
22590
22591 NOTE: This can be called when reading in partial or full symbols. */
22592
22593 static struct type *
22594 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22595 {
22596 struct type *this_type;
22597
22598 this_type = get_die_type (die, cu);
22599 if (this_type)
22600 return this_type;
22601
22602 return read_type_die_1 (die, cu);
22603 }
22604
22605 /* Read the type in DIE, CU.
22606 Returns NULL for invalid types. */
22607
22608 static struct type *
22609 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22610 {
22611 struct type *this_type = NULL;
22612
22613 switch (die->tag)
22614 {
22615 case DW_TAG_class_type:
22616 case DW_TAG_interface_type:
22617 case DW_TAG_structure_type:
22618 case DW_TAG_union_type:
22619 this_type = read_structure_type (die, cu);
22620 break;
22621 case DW_TAG_enumeration_type:
22622 this_type = read_enumeration_type (die, cu);
22623 break;
22624 case DW_TAG_subprogram:
22625 case DW_TAG_subroutine_type:
22626 case DW_TAG_inlined_subroutine:
22627 this_type = read_subroutine_type (die, cu);
22628 break;
22629 case DW_TAG_array_type:
22630 this_type = read_array_type (die, cu);
22631 break;
22632 case DW_TAG_set_type:
22633 this_type = read_set_type (die, cu);
22634 break;
22635 case DW_TAG_pointer_type:
22636 this_type = read_tag_pointer_type (die, cu);
22637 break;
22638 case DW_TAG_ptr_to_member_type:
22639 this_type = read_tag_ptr_to_member_type (die, cu);
22640 break;
22641 case DW_TAG_reference_type:
22642 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22643 break;
22644 case DW_TAG_rvalue_reference_type:
22645 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22646 break;
22647 case DW_TAG_const_type:
22648 this_type = read_tag_const_type (die, cu);
22649 break;
22650 case DW_TAG_volatile_type:
22651 this_type = read_tag_volatile_type (die, cu);
22652 break;
22653 case DW_TAG_restrict_type:
22654 this_type = read_tag_restrict_type (die, cu);
22655 break;
22656 case DW_TAG_string_type:
22657 this_type = read_tag_string_type (die, cu);
22658 break;
22659 case DW_TAG_typedef:
22660 this_type = read_typedef (die, cu);
22661 break;
22662 case DW_TAG_subrange_type:
22663 this_type = read_subrange_type (die, cu);
22664 break;
22665 case DW_TAG_base_type:
22666 this_type = read_base_type (die, cu);
22667 break;
22668 case DW_TAG_unspecified_type:
22669 this_type = read_unspecified_type (die, cu);
22670 break;
22671 case DW_TAG_namespace:
22672 this_type = read_namespace_type (die, cu);
22673 break;
22674 case DW_TAG_module:
22675 this_type = read_module_type (die, cu);
22676 break;
22677 case DW_TAG_atomic_type:
22678 this_type = read_tag_atomic_type (die, cu);
22679 break;
22680 default:
22681 complaint (_("unexpected tag in read_type_die: '%s'"),
22682 dwarf_tag_name (die->tag));
22683 break;
22684 }
22685
22686 return this_type;
22687 }
22688
22689 /* See if we can figure out if the class lives in a namespace. We do
22690 this by looking for a member function; its demangled name will
22691 contain namespace info, if there is any.
22692 Return the computed name or NULL.
22693 Space for the result is allocated on the objfile's obstack.
22694 This is the full-die version of guess_partial_die_structure_name.
22695 In this case we know DIE has no useful parent. */
22696
22697 static const char *
22698 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22699 {
22700 struct die_info *spec_die;
22701 struct dwarf2_cu *spec_cu;
22702 struct die_info *child;
22703 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22704
22705 spec_cu = cu;
22706 spec_die = die_specification (die, &spec_cu);
22707 if (spec_die != NULL)
22708 {
22709 die = spec_die;
22710 cu = spec_cu;
22711 }
22712
22713 for (child = die->child;
22714 child != NULL;
22715 child = child->sibling)
22716 {
22717 if (child->tag == DW_TAG_subprogram)
22718 {
22719 const char *linkage_name = dw2_linkage_name (child, cu);
22720
22721 if (linkage_name != NULL)
22722 {
22723 gdb::unique_xmalloc_ptr<char> actual_name
22724 (language_class_name_from_physname (cu->language_defn,
22725 linkage_name));
22726 const char *name = NULL;
22727
22728 if (actual_name != NULL)
22729 {
22730 const char *die_name = dwarf2_name (die, cu);
22731
22732 if (die_name != NULL
22733 && strcmp (die_name, actual_name.get ()) != 0)
22734 {
22735 /* Strip off the class name from the full name.
22736 We want the prefix. */
22737 int die_name_len = strlen (die_name);
22738 int actual_name_len = strlen (actual_name.get ());
22739 const char *ptr = actual_name.get ();
22740
22741 /* Test for '::' as a sanity check. */
22742 if (actual_name_len > die_name_len + 2
22743 && ptr[actual_name_len - die_name_len - 1] == ':')
22744 name = obstack_strndup (
22745 &objfile->per_bfd->storage_obstack,
22746 ptr, actual_name_len - die_name_len - 2);
22747 }
22748 }
22749 return name;
22750 }
22751 }
22752 }
22753
22754 return NULL;
22755 }
22756
22757 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22758 prefix part in such case. See
22759 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22760
22761 static const char *
22762 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22763 {
22764 struct attribute *attr;
22765 const char *base;
22766
22767 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22768 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22769 return NULL;
22770
22771 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22772 return NULL;
22773
22774 attr = dw2_linkage_name_attr (die, cu);
22775 if (attr == NULL || DW_STRING (attr) == NULL)
22776 return NULL;
22777
22778 /* dwarf2_name had to be already called. */
22779 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22780
22781 /* Strip the base name, keep any leading namespaces/classes. */
22782 base = strrchr (DW_STRING (attr), ':');
22783 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22784 return "";
22785
22786 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22787 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22788 DW_STRING (attr),
22789 &base[-1] - DW_STRING (attr));
22790 }
22791
22792 /* Return the name of the namespace/class that DIE is defined within,
22793 or "" if we can't tell. The caller should not xfree the result.
22794
22795 For example, if we're within the method foo() in the following
22796 code:
22797
22798 namespace N {
22799 class C {
22800 void foo () {
22801 }
22802 };
22803 }
22804
22805 then determine_prefix on foo's die will return "N::C". */
22806
22807 static const char *
22808 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22809 {
22810 struct dwarf2_per_objfile *dwarf2_per_objfile
22811 = cu->per_cu->dwarf2_per_objfile;
22812 struct die_info *parent, *spec_die;
22813 struct dwarf2_cu *spec_cu;
22814 struct type *parent_type;
22815 const char *retval;
22816
22817 if (cu->language != language_cplus
22818 && cu->language != language_fortran && cu->language != language_d
22819 && cu->language != language_rust)
22820 return "";
22821
22822 retval = anonymous_struct_prefix (die, cu);
22823 if (retval)
22824 return retval;
22825
22826 /* We have to be careful in the presence of DW_AT_specification.
22827 For example, with GCC 3.4, given the code
22828
22829 namespace N {
22830 void foo() {
22831 // Definition of N::foo.
22832 }
22833 }
22834
22835 then we'll have a tree of DIEs like this:
22836
22837 1: DW_TAG_compile_unit
22838 2: DW_TAG_namespace // N
22839 3: DW_TAG_subprogram // declaration of N::foo
22840 4: DW_TAG_subprogram // definition of N::foo
22841 DW_AT_specification // refers to die #3
22842
22843 Thus, when processing die #4, we have to pretend that we're in
22844 the context of its DW_AT_specification, namely the contex of die
22845 #3. */
22846 spec_cu = cu;
22847 spec_die = die_specification (die, &spec_cu);
22848 if (spec_die == NULL)
22849 parent = die->parent;
22850 else
22851 {
22852 parent = spec_die->parent;
22853 cu = spec_cu;
22854 }
22855
22856 if (parent == NULL)
22857 return "";
22858 else if (parent->building_fullname)
22859 {
22860 const char *name;
22861 const char *parent_name;
22862
22863 /* It has been seen on RealView 2.2 built binaries,
22864 DW_TAG_template_type_param types actually _defined_ as
22865 children of the parent class:
22866
22867 enum E {};
22868 template class <class Enum> Class{};
22869 Class<enum E> class_e;
22870
22871 1: DW_TAG_class_type (Class)
22872 2: DW_TAG_enumeration_type (E)
22873 3: DW_TAG_enumerator (enum1:0)
22874 3: DW_TAG_enumerator (enum2:1)
22875 ...
22876 2: DW_TAG_template_type_param
22877 DW_AT_type DW_FORM_ref_udata (E)
22878
22879 Besides being broken debug info, it can put GDB into an
22880 infinite loop. Consider:
22881
22882 When we're building the full name for Class<E>, we'll start
22883 at Class, and go look over its template type parameters,
22884 finding E. We'll then try to build the full name of E, and
22885 reach here. We're now trying to build the full name of E,
22886 and look over the parent DIE for containing scope. In the
22887 broken case, if we followed the parent DIE of E, we'd again
22888 find Class, and once again go look at its template type
22889 arguments, etc., etc. Simply don't consider such parent die
22890 as source-level parent of this die (it can't be, the language
22891 doesn't allow it), and break the loop here. */
22892 name = dwarf2_name (die, cu);
22893 parent_name = dwarf2_name (parent, cu);
22894 complaint (_("template param type '%s' defined within parent '%s'"),
22895 name ? name : "<unknown>",
22896 parent_name ? parent_name : "<unknown>");
22897 return "";
22898 }
22899 else
22900 switch (parent->tag)
22901 {
22902 case DW_TAG_namespace:
22903 parent_type = read_type_die (parent, cu);
22904 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22905 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22906 Work around this problem here. */
22907 if (cu->language == language_cplus
22908 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22909 return "";
22910 /* We give a name to even anonymous namespaces. */
22911 return TYPE_NAME (parent_type);
22912 case DW_TAG_class_type:
22913 case DW_TAG_interface_type:
22914 case DW_TAG_structure_type:
22915 case DW_TAG_union_type:
22916 case DW_TAG_module:
22917 parent_type = read_type_die (parent, cu);
22918 if (TYPE_NAME (parent_type) != NULL)
22919 return TYPE_NAME (parent_type);
22920 else
22921 /* An anonymous structure is only allowed non-static data
22922 members; no typedefs, no member functions, et cetera.
22923 So it does not need a prefix. */
22924 return "";
22925 case DW_TAG_compile_unit:
22926 case DW_TAG_partial_unit:
22927 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22928 if (cu->language == language_cplus
22929 && !dwarf2_per_objfile->types.empty ()
22930 && die->child != NULL
22931 && (die->tag == DW_TAG_class_type
22932 || die->tag == DW_TAG_structure_type
22933 || die->tag == DW_TAG_union_type))
22934 {
22935 const char *name = guess_full_die_structure_name (die, cu);
22936 if (name != NULL)
22937 return name;
22938 }
22939 return "";
22940 case DW_TAG_subprogram:
22941 /* Nested subroutines in Fortran get a prefix with the name
22942 of the parent's subroutine. */
22943 if (cu->language == language_fortran)
22944 {
22945 if ((die->tag == DW_TAG_subprogram)
22946 && (dwarf2_name (parent, cu) != NULL))
22947 return dwarf2_name (parent, cu);
22948 }
22949 return determine_prefix (parent, cu);
22950 case DW_TAG_enumeration_type:
22951 parent_type = read_type_die (parent, cu);
22952 if (TYPE_DECLARED_CLASS (parent_type))
22953 {
22954 if (TYPE_NAME (parent_type) != NULL)
22955 return TYPE_NAME (parent_type);
22956 return "";
22957 }
22958 /* Fall through. */
22959 default:
22960 return determine_prefix (parent, cu);
22961 }
22962 }
22963
22964 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22965 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22966 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22967 an obconcat, otherwise allocate storage for the result. The CU argument is
22968 used to determine the language and hence, the appropriate separator. */
22969
22970 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22971
22972 static char *
22973 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22974 int physname, struct dwarf2_cu *cu)
22975 {
22976 const char *lead = "";
22977 const char *sep;
22978
22979 if (suffix == NULL || suffix[0] == '\0'
22980 || prefix == NULL || prefix[0] == '\0')
22981 sep = "";
22982 else if (cu->language == language_d)
22983 {
22984 /* For D, the 'main' function could be defined in any module, but it
22985 should never be prefixed. */
22986 if (strcmp (suffix, "D main") == 0)
22987 {
22988 prefix = "";
22989 sep = "";
22990 }
22991 else
22992 sep = ".";
22993 }
22994 else if (cu->language == language_fortran && physname)
22995 {
22996 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22997 DW_AT_MIPS_linkage_name is preferred and used instead. */
22998
22999 lead = "__";
23000 sep = "_MOD_";
23001 }
23002 else
23003 sep = "::";
23004
23005 if (prefix == NULL)
23006 prefix = "";
23007 if (suffix == NULL)
23008 suffix = "";
23009
23010 if (obs == NULL)
23011 {
23012 char *retval
23013 = ((char *)
23014 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23015
23016 strcpy (retval, lead);
23017 strcat (retval, prefix);
23018 strcat (retval, sep);
23019 strcat (retval, suffix);
23020 return retval;
23021 }
23022 else
23023 {
23024 /* We have an obstack. */
23025 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23026 }
23027 }
23028
23029 /* Return sibling of die, NULL if no sibling. */
23030
23031 static struct die_info *
23032 sibling_die (struct die_info *die)
23033 {
23034 return die->sibling;
23035 }
23036
23037 /* Get name of a die, return NULL if not found. */
23038
23039 static const char *
23040 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23041 struct obstack *obstack)
23042 {
23043 if (name && cu->language == language_cplus)
23044 {
23045 std::string canon_name = cp_canonicalize_string (name);
23046
23047 if (!canon_name.empty ())
23048 {
23049 if (canon_name != name)
23050 name = obstack_strdup (obstack, canon_name);
23051 }
23052 }
23053
23054 return name;
23055 }
23056
23057 /* Get name of a die, return NULL if not found.
23058 Anonymous namespaces are converted to their magic string. */
23059
23060 static const char *
23061 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23062 {
23063 struct attribute *attr;
23064 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23065
23066 attr = dwarf2_attr (die, DW_AT_name, cu);
23067 if ((!attr || !DW_STRING (attr))
23068 && die->tag != DW_TAG_namespace
23069 && die->tag != DW_TAG_class_type
23070 && die->tag != DW_TAG_interface_type
23071 && die->tag != DW_TAG_structure_type
23072 && die->tag != DW_TAG_union_type)
23073 return NULL;
23074
23075 switch (die->tag)
23076 {
23077 case DW_TAG_compile_unit:
23078 case DW_TAG_partial_unit:
23079 /* Compilation units have a DW_AT_name that is a filename, not
23080 a source language identifier. */
23081 case DW_TAG_enumeration_type:
23082 case DW_TAG_enumerator:
23083 /* These tags always have simple identifiers already; no need
23084 to canonicalize them. */
23085 return DW_STRING (attr);
23086
23087 case DW_TAG_namespace:
23088 if (attr != NULL && DW_STRING (attr) != NULL)
23089 return DW_STRING (attr);
23090 return CP_ANONYMOUS_NAMESPACE_STR;
23091
23092 case DW_TAG_class_type:
23093 case DW_TAG_interface_type:
23094 case DW_TAG_structure_type:
23095 case DW_TAG_union_type:
23096 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23097 structures or unions. These were of the form "._%d" in GCC 4.1,
23098 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23099 and GCC 4.4. We work around this problem by ignoring these. */
23100 if (attr && DW_STRING (attr)
23101 && (startswith (DW_STRING (attr), "._")
23102 || startswith (DW_STRING (attr), "<anonymous")))
23103 return NULL;
23104
23105 /* GCC might emit a nameless typedef that has a linkage name. See
23106 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23107 if (!attr || DW_STRING (attr) == NULL)
23108 {
23109 attr = dw2_linkage_name_attr (die, cu);
23110 if (attr == NULL || DW_STRING (attr) == NULL)
23111 return NULL;
23112
23113 /* Avoid demangling DW_STRING (attr) the second time on a second
23114 call for the same DIE. */
23115 if (!DW_STRING_IS_CANONICAL (attr))
23116 {
23117 gdb::unique_xmalloc_ptr<char> demangled
23118 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
23119
23120 const char *base;
23121
23122 /* FIXME: we already did this for the partial symbol... */
23123 DW_STRING (attr)
23124 = obstack_strdup (&objfile->per_bfd->storage_obstack,
23125 demangled.get ());
23126 DW_STRING_IS_CANONICAL (attr) = 1;
23127
23128 /* Strip any leading namespaces/classes, keep only the base name.
23129 DW_AT_name for named DIEs does not contain the prefixes. */
23130 base = strrchr (DW_STRING (attr), ':');
23131 if (base && base > DW_STRING (attr) && base[-1] == ':')
23132 return &base[1];
23133 else
23134 return DW_STRING (attr);
23135 }
23136 }
23137 break;
23138
23139 default:
23140 break;
23141 }
23142
23143 if (!DW_STRING_IS_CANONICAL (attr))
23144 {
23145 DW_STRING (attr)
23146 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
23147 &objfile->per_bfd->storage_obstack);
23148 DW_STRING_IS_CANONICAL (attr) = 1;
23149 }
23150 return DW_STRING (attr);
23151 }
23152
23153 /* Return the die that this die in an extension of, or NULL if there
23154 is none. *EXT_CU is the CU containing DIE on input, and the CU
23155 containing the return value on output. */
23156
23157 static struct die_info *
23158 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23159 {
23160 struct attribute *attr;
23161
23162 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23163 if (attr == NULL)
23164 return NULL;
23165
23166 return follow_die_ref (die, attr, ext_cu);
23167 }
23168
23169 /* A convenience function that returns an "unknown" DWARF name,
23170 including the value of V. STR is the name of the entity being
23171 printed, e.g., "TAG". */
23172
23173 static const char *
23174 dwarf_unknown (const char *str, unsigned v)
23175 {
23176 char *cell = get_print_cell ();
23177 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23178 return cell;
23179 }
23180
23181 /* Convert a DIE tag into its string name. */
23182
23183 static const char *
23184 dwarf_tag_name (unsigned tag)
23185 {
23186 const char *name = get_DW_TAG_name (tag);
23187
23188 if (name == NULL)
23189 return dwarf_unknown ("TAG", tag);
23190
23191 return name;
23192 }
23193
23194 /* Convert a DWARF attribute code into its string name. */
23195
23196 static const char *
23197 dwarf_attr_name (unsigned attr)
23198 {
23199 const char *name;
23200
23201 #ifdef MIPS /* collides with DW_AT_HP_block_index */
23202 if (attr == DW_AT_MIPS_fde)
23203 return "DW_AT_MIPS_fde";
23204 #else
23205 if (attr == DW_AT_HP_block_index)
23206 return "DW_AT_HP_block_index";
23207 #endif
23208
23209 name = get_DW_AT_name (attr);
23210
23211 if (name == NULL)
23212 return dwarf_unknown ("AT", attr);
23213
23214 return name;
23215 }
23216
23217 /* Convert a unit type to corresponding DW_UT name. */
23218
23219 static const char *
23220 dwarf_unit_type_name (int unit_type) {
23221 switch (unit_type)
23222 {
23223 case 0x01:
23224 return "DW_UT_compile (0x01)";
23225 case 0x02:
23226 return "DW_UT_type (0x02)";
23227 case 0x03:
23228 return "DW_UT_partial (0x03)";
23229 case 0x04:
23230 return "DW_UT_skeleton (0x04)";
23231 case 0x05:
23232 return "DW_UT_split_compile (0x05)";
23233 case 0x06:
23234 return "DW_UT_split_type (0x06)";
23235 case 0x80:
23236 return "DW_UT_lo_user (0x80)";
23237 case 0xff:
23238 return "DW_UT_hi_user (0xff)";
23239 default:
23240 return nullptr;
23241 }
23242 }
23243
23244 /* Convert a DWARF value form code into its string name. */
23245
23246 static const char *
23247 dwarf_form_name (unsigned form)
23248 {
23249 const char *name = get_DW_FORM_name (form);
23250
23251 if (name == NULL)
23252 return dwarf_unknown ("FORM", form);
23253
23254 return name;
23255 }
23256
23257 static const char *
23258 dwarf_bool_name (unsigned mybool)
23259 {
23260 if (mybool)
23261 return "TRUE";
23262 else
23263 return "FALSE";
23264 }
23265
23266 /* Convert a DWARF type code into its string name. */
23267
23268 static const char *
23269 dwarf_type_encoding_name (unsigned enc)
23270 {
23271 const char *name = get_DW_ATE_name (enc);
23272
23273 if (name == NULL)
23274 return dwarf_unknown ("ATE", enc);
23275
23276 return name;
23277 }
23278
23279 static void
23280 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23281 {
23282 unsigned int i;
23283
23284 print_spaces (indent, f);
23285 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23286 dwarf_tag_name (die->tag), die->abbrev,
23287 sect_offset_str (die->sect_off));
23288
23289 if (die->parent != NULL)
23290 {
23291 print_spaces (indent, f);
23292 fprintf_unfiltered (f, " parent at offset: %s\n",
23293 sect_offset_str (die->parent->sect_off));
23294 }
23295
23296 print_spaces (indent, f);
23297 fprintf_unfiltered (f, " has children: %s\n",
23298 dwarf_bool_name (die->child != NULL));
23299
23300 print_spaces (indent, f);
23301 fprintf_unfiltered (f, " attributes:\n");
23302
23303 for (i = 0; i < die->num_attrs; ++i)
23304 {
23305 print_spaces (indent, f);
23306 fprintf_unfiltered (f, " %s (%s) ",
23307 dwarf_attr_name (die->attrs[i].name),
23308 dwarf_form_name (die->attrs[i].form));
23309
23310 switch (die->attrs[i].form)
23311 {
23312 case DW_FORM_addr:
23313 case DW_FORM_addrx:
23314 case DW_FORM_GNU_addr_index:
23315 fprintf_unfiltered (f, "address: ");
23316 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23317 break;
23318 case DW_FORM_block2:
23319 case DW_FORM_block4:
23320 case DW_FORM_block:
23321 case DW_FORM_block1:
23322 fprintf_unfiltered (f, "block: size %s",
23323 pulongest (DW_BLOCK (&die->attrs[i])->size));
23324 break;
23325 case DW_FORM_exprloc:
23326 fprintf_unfiltered (f, "expression: size %s",
23327 pulongest (DW_BLOCK (&die->attrs[i])->size));
23328 break;
23329 case DW_FORM_data16:
23330 fprintf_unfiltered (f, "constant of 16 bytes");
23331 break;
23332 case DW_FORM_ref_addr:
23333 fprintf_unfiltered (f, "ref address: ");
23334 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23335 break;
23336 case DW_FORM_GNU_ref_alt:
23337 fprintf_unfiltered (f, "alt ref address: ");
23338 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23339 break;
23340 case DW_FORM_ref1:
23341 case DW_FORM_ref2:
23342 case DW_FORM_ref4:
23343 case DW_FORM_ref8:
23344 case DW_FORM_ref_udata:
23345 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23346 (long) (DW_UNSND (&die->attrs[i])));
23347 break;
23348 case DW_FORM_data1:
23349 case DW_FORM_data2:
23350 case DW_FORM_data4:
23351 case DW_FORM_data8:
23352 case DW_FORM_udata:
23353 case DW_FORM_sdata:
23354 fprintf_unfiltered (f, "constant: %s",
23355 pulongest (DW_UNSND (&die->attrs[i])));
23356 break;
23357 case DW_FORM_sec_offset:
23358 fprintf_unfiltered (f, "section offset: %s",
23359 pulongest (DW_UNSND (&die->attrs[i])));
23360 break;
23361 case DW_FORM_ref_sig8:
23362 fprintf_unfiltered (f, "signature: %s",
23363 hex_string (DW_SIGNATURE (&die->attrs[i])));
23364 break;
23365 case DW_FORM_string:
23366 case DW_FORM_strp:
23367 case DW_FORM_line_strp:
23368 case DW_FORM_strx:
23369 case DW_FORM_GNU_str_index:
23370 case DW_FORM_GNU_strp_alt:
23371 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23372 DW_STRING (&die->attrs[i])
23373 ? DW_STRING (&die->attrs[i]) : "",
23374 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23375 break;
23376 case DW_FORM_flag:
23377 if (DW_UNSND (&die->attrs[i]))
23378 fprintf_unfiltered (f, "flag: TRUE");
23379 else
23380 fprintf_unfiltered (f, "flag: FALSE");
23381 break;
23382 case DW_FORM_flag_present:
23383 fprintf_unfiltered (f, "flag: TRUE");
23384 break;
23385 case DW_FORM_indirect:
23386 /* The reader will have reduced the indirect form to
23387 the "base form" so this form should not occur. */
23388 fprintf_unfiltered (f,
23389 "unexpected attribute form: DW_FORM_indirect");
23390 break;
23391 case DW_FORM_implicit_const:
23392 fprintf_unfiltered (f, "constant: %s",
23393 plongest (DW_SND (&die->attrs[i])));
23394 break;
23395 default:
23396 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23397 die->attrs[i].form);
23398 break;
23399 }
23400 fprintf_unfiltered (f, "\n");
23401 }
23402 }
23403
23404 static void
23405 dump_die_for_error (struct die_info *die)
23406 {
23407 dump_die_shallow (gdb_stderr, 0, die);
23408 }
23409
23410 static void
23411 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23412 {
23413 int indent = level * 4;
23414
23415 gdb_assert (die != NULL);
23416
23417 if (level >= max_level)
23418 return;
23419
23420 dump_die_shallow (f, indent, die);
23421
23422 if (die->child != NULL)
23423 {
23424 print_spaces (indent, f);
23425 fprintf_unfiltered (f, " Children:");
23426 if (level + 1 < max_level)
23427 {
23428 fprintf_unfiltered (f, "\n");
23429 dump_die_1 (f, level + 1, max_level, die->child);
23430 }
23431 else
23432 {
23433 fprintf_unfiltered (f,
23434 " [not printed, max nesting level reached]\n");
23435 }
23436 }
23437
23438 if (die->sibling != NULL && level > 0)
23439 {
23440 dump_die_1 (f, level, max_level, die->sibling);
23441 }
23442 }
23443
23444 /* This is called from the pdie macro in gdbinit.in.
23445 It's not static so gcc will keep a copy callable from gdb. */
23446
23447 void
23448 dump_die (struct die_info *die, int max_level)
23449 {
23450 dump_die_1 (gdb_stdlog, 0, max_level, die);
23451 }
23452
23453 static void
23454 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23455 {
23456 void **slot;
23457
23458 slot = htab_find_slot_with_hash (cu->die_hash, die,
23459 to_underlying (die->sect_off),
23460 INSERT);
23461
23462 *slot = die;
23463 }
23464
23465 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23466 required kind. */
23467
23468 static sect_offset
23469 dwarf2_get_ref_die_offset (const struct attribute *attr)
23470 {
23471 if (attr_form_is_ref (attr))
23472 return (sect_offset) DW_UNSND (attr);
23473
23474 complaint (_("unsupported die ref attribute form: '%s'"),
23475 dwarf_form_name (attr->form));
23476 return {};
23477 }
23478
23479 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23480 * the value held by the attribute is not constant. */
23481
23482 static LONGEST
23483 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23484 {
23485 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23486 return DW_SND (attr);
23487 else if (attr->form == DW_FORM_udata
23488 || attr->form == DW_FORM_data1
23489 || attr->form == DW_FORM_data2
23490 || attr->form == DW_FORM_data4
23491 || attr->form == DW_FORM_data8)
23492 return DW_UNSND (attr);
23493 else
23494 {
23495 /* For DW_FORM_data16 see attr_form_is_constant. */
23496 complaint (_("Attribute value is not a constant (%s)"),
23497 dwarf_form_name (attr->form));
23498 return default_value;
23499 }
23500 }
23501
23502 /* Follow reference or signature attribute ATTR of SRC_DIE.
23503 On entry *REF_CU is the CU of SRC_DIE.
23504 On exit *REF_CU is the CU of the result. */
23505
23506 static struct die_info *
23507 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23508 struct dwarf2_cu **ref_cu)
23509 {
23510 struct die_info *die;
23511
23512 if (attr_form_is_ref (attr))
23513 die = follow_die_ref (src_die, attr, ref_cu);
23514 else if (attr->form == DW_FORM_ref_sig8)
23515 die = follow_die_sig (src_die, attr, ref_cu);
23516 else
23517 {
23518 dump_die_for_error (src_die);
23519 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23520 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23521 }
23522
23523 return die;
23524 }
23525
23526 /* Follow reference OFFSET.
23527 On entry *REF_CU is the CU of the source die referencing OFFSET.
23528 On exit *REF_CU is the CU of the result.
23529 Returns NULL if OFFSET is invalid. */
23530
23531 static struct die_info *
23532 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23533 struct dwarf2_cu **ref_cu)
23534 {
23535 struct die_info temp_die;
23536 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23537 struct dwarf2_per_objfile *dwarf2_per_objfile
23538 = cu->per_cu->dwarf2_per_objfile;
23539
23540 gdb_assert (cu->per_cu != NULL);
23541
23542 target_cu = cu;
23543
23544 if (cu->per_cu->is_debug_types)
23545 {
23546 /* .debug_types CUs cannot reference anything outside their CU.
23547 If they need to, they have to reference a signatured type via
23548 DW_FORM_ref_sig8. */
23549 if (!offset_in_cu_p (&cu->header, sect_off))
23550 return NULL;
23551 }
23552 else if (offset_in_dwz != cu->per_cu->is_dwz
23553 || !offset_in_cu_p (&cu->header, sect_off))
23554 {
23555 struct dwarf2_per_cu_data *per_cu;
23556
23557 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23558 dwarf2_per_objfile);
23559
23560 /* If necessary, add it to the queue and load its DIEs. */
23561 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23562 load_full_comp_unit (per_cu, false, cu->language);
23563
23564 target_cu = per_cu->cu;
23565 }
23566 else if (cu->dies == NULL)
23567 {
23568 /* We're loading full DIEs during partial symbol reading. */
23569 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23570 load_full_comp_unit (cu->per_cu, false, language_minimal);
23571 }
23572
23573 *ref_cu = target_cu;
23574 temp_die.sect_off = sect_off;
23575
23576 if (target_cu != cu)
23577 target_cu->ancestor = cu;
23578
23579 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23580 &temp_die,
23581 to_underlying (sect_off));
23582 }
23583
23584 /* Follow reference attribute ATTR of SRC_DIE.
23585 On entry *REF_CU is the CU of SRC_DIE.
23586 On exit *REF_CU is the CU of the result. */
23587
23588 static struct die_info *
23589 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23590 struct dwarf2_cu **ref_cu)
23591 {
23592 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23593 struct dwarf2_cu *cu = *ref_cu;
23594 struct die_info *die;
23595
23596 die = follow_die_offset (sect_off,
23597 (attr->form == DW_FORM_GNU_ref_alt
23598 || cu->per_cu->is_dwz),
23599 ref_cu);
23600 if (!die)
23601 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23602 "at %s [in module %s]"),
23603 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23604 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23605
23606 return die;
23607 }
23608
23609 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23610 Returned value is intended for DW_OP_call*. Returned
23611 dwarf2_locexpr_baton->data has lifetime of
23612 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23613
23614 struct dwarf2_locexpr_baton
23615 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23616 struct dwarf2_per_cu_data *per_cu,
23617 CORE_ADDR (*get_frame_pc) (void *baton),
23618 void *baton, bool resolve_abstract_p)
23619 {
23620 struct dwarf2_cu *cu;
23621 struct die_info *die;
23622 struct attribute *attr;
23623 struct dwarf2_locexpr_baton retval;
23624 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23625 struct objfile *objfile = dwarf2_per_objfile->objfile;
23626
23627 if (per_cu->cu == NULL)
23628 load_cu (per_cu, false);
23629 cu = per_cu->cu;
23630 if (cu == NULL)
23631 {
23632 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23633 Instead just throw an error, not much else we can do. */
23634 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23635 sect_offset_str (sect_off), objfile_name (objfile));
23636 }
23637
23638 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23639 if (!die)
23640 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23641 sect_offset_str (sect_off), objfile_name (objfile));
23642
23643 attr = dwarf2_attr (die, DW_AT_location, cu);
23644 if (!attr && resolve_abstract_p
23645 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23646 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23647 {
23648 CORE_ADDR pc = (*get_frame_pc) (baton);
23649 CORE_ADDR baseaddr = objfile->text_section_offset ();
23650 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23651
23652 for (const auto &cand_off
23653 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23654 {
23655 struct dwarf2_cu *cand_cu = cu;
23656 struct die_info *cand
23657 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23658 if (!cand
23659 || !cand->parent
23660 || cand->parent->tag != DW_TAG_subprogram)
23661 continue;
23662
23663 CORE_ADDR pc_low, pc_high;
23664 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23665 if (pc_low == ((CORE_ADDR) -1))
23666 continue;
23667 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23668 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23669 if (!(pc_low <= pc && pc < pc_high))
23670 continue;
23671
23672 die = cand;
23673 attr = dwarf2_attr (die, DW_AT_location, cu);
23674 break;
23675 }
23676 }
23677
23678 if (!attr)
23679 {
23680 /* DWARF: "If there is no such attribute, then there is no effect.".
23681 DATA is ignored if SIZE is 0. */
23682
23683 retval.data = NULL;
23684 retval.size = 0;
23685 }
23686 else if (attr_form_is_section_offset (attr))
23687 {
23688 struct dwarf2_loclist_baton loclist_baton;
23689 CORE_ADDR pc = (*get_frame_pc) (baton);
23690 size_t size;
23691
23692 fill_in_loclist_baton (cu, &loclist_baton, attr);
23693
23694 retval.data = dwarf2_find_location_expression (&loclist_baton,
23695 &size, pc);
23696 retval.size = size;
23697 }
23698 else
23699 {
23700 if (!attr_form_is_block (attr))
23701 error (_("Dwarf Error: DIE at %s referenced in module %s "
23702 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23703 sect_offset_str (sect_off), objfile_name (objfile));
23704
23705 retval.data = DW_BLOCK (attr)->data;
23706 retval.size = DW_BLOCK (attr)->size;
23707 }
23708 retval.per_cu = cu->per_cu;
23709
23710 age_cached_comp_units (dwarf2_per_objfile);
23711
23712 return retval;
23713 }
23714
23715 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23716 offset. */
23717
23718 struct dwarf2_locexpr_baton
23719 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23720 struct dwarf2_per_cu_data *per_cu,
23721 CORE_ADDR (*get_frame_pc) (void *baton),
23722 void *baton)
23723 {
23724 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23725
23726 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23727 }
23728
23729 /* Write a constant of a given type as target-ordered bytes into
23730 OBSTACK. */
23731
23732 static const gdb_byte *
23733 write_constant_as_bytes (struct obstack *obstack,
23734 enum bfd_endian byte_order,
23735 struct type *type,
23736 ULONGEST value,
23737 LONGEST *len)
23738 {
23739 gdb_byte *result;
23740
23741 *len = TYPE_LENGTH (type);
23742 result = (gdb_byte *) obstack_alloc (obstack, *len);
23743 store_unsigned_integer (result, *len, byte_order, value);
23744
23745 return result;
23746 }
23747
23748 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23749 pointer to the constant bytes and set LEN to the length of the
23750 data. If memory is needed, allocate it on OBSTACK. If the DIE
23751 does not have a DW_AT_const_value, return NULL. */
23752
23753 const gdb_byte *
23754 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23755 struct dwarf2_per_cu_data *per_cu,
23756 struct obstack *obstack,
23757 LONGEST *len)
23758 {
23759 struct dwarf2_cu *cu;
23760 struct die_info *die;
23761 struct attribute *attr;
23762 const gdb_byte *result = NULL;
23763 struct type *type;
23764 LONGEST value;
23765 enum bfd_endian byte_order;
23766 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23767
23768 if (per_cu->cu == NULL)
23769 load_cu (per_cu, false);
23770 cu = per_cu->cu;
23771 if (cu == NULL)
23772 {
23773 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23774 Instead just throw an error, not much else we can do. */
23775 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23776 sect_offset_str (sect_off), objfile_name (objfile));
23777 }
23778
23779 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23780 if (!die)
23781 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23782 sect_offset_str (sect_off), objfile_name (objfile));
23783
23784 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23785 if (attr == NULL)
23786 return NULL;
23787
23788 byte_order = (bfd_big_endian (objfile->obfd)
23789 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23790
23791 switch (attr->form)
23792 {
23793 case DW_FORM_addr:
23794 case DW_FORM_addrx:
23795 case DW_FORM_GNU_addr_index:
23796 {
23797 gdb_byte *tem;
23798
23799 *len = cu->header.addr_size;
23800 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23801 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23802 result = tem;
23803 }
23804 break;
23805 case DW_FORM_string:
23806 case DW_FORM_strp:
23807 case DW_FORM_strx:
23808 case DW_FORM_GNU_str_index:
23809 case DW_FORM_GNU_strp_alt:
23810 /* DW_STRING is already allocated on the objfile obstack, point
23811 directly to it. */
23812 result = (const gdb_byte *) DW_STRING (attr);
23813 *len = strlen (DW_STRING (attr));
23814 break;
23815 case DW_FORM_block1:
23816 case DW_FORM_block2:
23817 case DW_FORM_block4:
23818 case DW_FORM_block:
23819 case DW_FORM_exprloc:
23820 case DW_FORM_data16:
23821 result = DW_BLOCK (attr)->data;
23822 *len = DW_BLOCK (attr)->size;
23823 break;
23824
23825 /* The DW_AT_const_value attributes are supposed to carry the
23826 symbol's value "represented as it would be on the target
23827 architecture." By the time we get here, it's already been
23828 converted to host endianness, so we just need to sign- or
23829 zero-extend it as appropriate. */
23830 case DW_FORM_data1:
23831 type = die_type (die, cu);
23832 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23833 if (result == NULL)
23834 result = write_constant_as_bytes (obstack, byte_order,
23835 type, value, len);
23836 break;
23837 case DW_FORM_data2:
23838 type = die_type (die, cu);
23839 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23840 if (result == NULL)
23841 result = write_constant_as_bytes (obstack, byte_order,
23842 type, value, len);
23843 break;
23844 case DW_FORM_data4:
23845 type = die_type (die, cu);
23846 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23847 if (result == NULL)
23848 result = write_constant_as_bytes (obstack, byte_order,
23849 type, value, len);
23850 break;
23851 case DW_FORM_data8:
23852 type = die_type (die, cu);
23853 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23854 if (result == NULL)
23855 result = write_constant_as_bytes (obstack, byte_order,
23856 type, value, len);
23857 break;
23858
23859 case DW_FORM_sdata:
23860 case DW_FORM_implicit_const:
23861 type = die_type (die, cu);
23862 result = write_constant_as_bytes (obstack, byte_order,
23863 type, DW_SND (attr), len);
23864 break;
23865
23866 case DW_FORM_udata:
23867 type = die_type (die, cu);
23868 result = write_constant_as_bytes (obstack, byte_order,
23869 type, DW_UNSND (attr), len);
23870 break;
23871
23872 default:
23873 complaint (_("unsupported const value attribute form: '%s'"),
23874 dwarf_form_name (attr->form));
23875 break;
23876 }
23877
23878 return result;
23879 }
23880
23881 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23882 valid type for this die is found. */
23883
23884 struct type *
23885 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23886 struct dwarf2_per_cu_data *per_cu)
23887 {
23888 struct dwarf2_cu *cu;
23889 struct die_info *die;
23890
23891 if (per_cu->cu == NULL)
23892 load_cu (per_cu, false);
23893 cu = per_cu->cu;
23894 if (!cu)
23895 return NULL;
23896
23897 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23898 if (!die)
23899 return NULL;
23900
23901 return die_type (die, cu);
23902 }
23903
23904 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23905 PER_CU. */
23906
23907 struct type *
23908 dwarf2_get_die_type (cu_offset die_offset,
23909 struct dwarf2_per_cu_data *per_cu)
23910 {
23911 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23912 return get_die_type_at_offset (die_offset_sect, per_cu);
23913 }
23914
23915 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23916 On entry *REF_CU is the CU of SRC_DIE.
23917 On exit *REF_CU is the CU of the result.
23918 Returns NULL if the referenced DIE isn't found. */
23919
23920 static struct die_info *
23921 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23922 struct dwarf2_cu **ref_cu)
23923 {
23924 struct die_info temp_die;
23925 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23926 struct die_info *die;
23927
23928 /* While it might be nice to assert sig_type->type == NULL here,
23929 we can get here for DW_AT_imported_declaration where we need
23930 the DIE not the type. */
23931
23932 /* If necessary, add it to the queue and load its DIEs. */
23933
23934 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23935 read_signatured_type (sig_type);
23936
23937 sig_cu = sig_type->per_cu.cu;
23938 gdb_assert (sig_cu != NULL);
23939 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23940 temp_die.sect_off = sig_type->type_offset_in_section;
23941 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23942 to_underlying (temp_die.sect_off));
23943 if (die)
23944 {
23945 struct dwarf2_per_objfile *dwarf2_per_objfile
23946 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23947
23948 /* For .gdb_index version 7 keep track of included TUs.
23949 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23950 if (dwarf2_per_objfile->index_table != NULL
23951 && dwarf2_per_objfile->index_table->version <= 7)
23952 {
23953 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23954 }
23955
23956 *ref_cu = sig_cu;
23957 if (sig_cu != cu)
23958 sig_cu->ancestor = cu;
23959
23960 return die;
23961 }
23962
23963 return NULL;
23964 }
23965
23966 /* Follow signatured type referenced by ATTR in SRC_DIE.
23967 On entry *REF_CU is the CU of SRC_DIE.
23968 On exit *REF_CU is the CU of the result.
23969 The result is the DIE of the type.
23970 If the referenced type cannot be found an error is thrown. */
23971
23972 static struct die_info *
23973 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23974 struct dwarf2_cu **ref_cu)
23975 {
23976 ULONGEST signature = DW_SIGNATURE (attr);
23977 struct signatured_type *sig_type;
23978 struct die_info *die;
23979
23980 gdb_assert (attr->form == DW_FORM_ref_sig8);
23981
23982 sig_type = lookup_signatured_type (*ref_cu, signature);
23983 /* sig_type will be NULL if the signatured type is missing from
23984 the debug info. */
23985 if (sig_type == NULL)
23986 {
23987 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23988 " from DIE at %s [in module %s]"),
23989 hex_string (signature), sect_offset_str (src_die->sect_off),
23990 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23991 }
23992
23993 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23994 if (die == NULL)
23995 {
23996 dump_die_for_error (src_die);
23997 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23998 " from DIE at %s [in module %s]"),
23999 hex_string (signature), sect_offset_str (src_die->sect_off),
24000 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
24001 }
24002
24003 return die;
24004 }
24005
24006 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24007 reading in and processing the type unit if necessary. */
24008
24009 static struct type *
24010 get_signatured_type (struct die_info *die, ULONGEST signature,
24011 struct dwarf2_cu *cu)
24012 {
24013 struct dwarf2_per_objfile *dwarf2_per_objfile
24014 = cu->per_cu->dwarf2_per_objfile;
24015 struct signatured_type *sig_type;
24016 struct dwarf2_cu *type_cu;
24017 struct die_info *type_die;
24018 struct type *type;
24019
24020 sig_type = lookup_signatured_type (cu, signature);
24021 /* sig_type will be NULL if the signatured type is missing from
24022 the debug info. */
24023 if (sig_type == NULL)
24024 {
24025 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24026 " from DIE at %s [in module %s]"),
24027 hex_string (signature), sect_offset_str (die->sect_off),
24028 objfile_name (dwarf2_per_objfile->objfile));
24029 return build_error_marker_type (cu, die);
24030 }
24031
24032 /* If we already know the type we're done. */
24033 if (sig_type->type != NULL)
24034 return sig_type->type;
24035
24036 type_cu = cu;
24037 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24038 if (type_die != NULL)
24039 {
24040 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24041 is created. This is important, for example, because for c++ classes
24042 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24043 type = read_type_die (type_die, type_cu);
24044 if (type == NULL)
24045 {
24046 complaint (_("Dwarf Error: Cannot build signatured type %s"
24047 " referenced from DIE at %s [in module %s]"),
24048 hex_string (signature), sect_offset_str (die->sect_off),
24049 objfile_name (dwarf2_per_objfile->objfile));
24050 type = build_error_marker_type (cu, die);
24051 }
24052 }
24053 else
24054 {
24055 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24056 " from DIE at %s [in module %s]"),
24057 hex_string (signature), sect_offset_str (die->sect_off),
24058 objfile_name (dwarf2_per_objfile->objfile));
24059 type = build_error_marker_type (cu, die);
24060 }
24061 sig_type->type = type;
24062
24063 return type;
24064 }
24065
24066 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24067 reading in and processing the type unit if necessary. */
24068
24069 static struct type *
24070 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24071 struct dwarf2_cu *cu) /* ARI: editCase function */
24072 {
24073 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24074 if (attr_form_is_ref (attr))
24075 {
24076 struct dwarf2_cu *type_cu = cu;
24077 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24078
24079 return read_type_die (type_die, type_cu);
24080 }
24081 else if (attr->form == DW_FORM_ref_sig8)
24082 {
24083 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24084 }
24085 else
24086 {
24087 struct dwarf2_per_objfile *dwarf2_per_objfile
24088 = cu->per_cu->dwarf2_per_objfile;
24089
24090 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24091 " at %s [in module %s]"),
24092 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24093 objfile_name (dwarf2_per_objfile->objfile));
24094 return build_error_marker_type (cu, die);
24095 }
24096 }
24097
24098 /* Load the DIEs associated with type unit PER_CU into memory. */
24099
24100 static void
24101 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
24102 {
24103 struct signatured_type *sig_type;
24104
24105 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24106 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24107
24108 /* We have the per_cu, but we need the signatured_type.
24109 Fortunately this is an easy translation. */
24110 gdb_assert (per_cu->is_debug_types);
24111 sig_type = (struct signatured_type *) per_cu;
24112
24113 gdb_assert (per_cu->cu == NULL);
24114
24115 read_signatured_type (sig_type);
24116
24117 gdb_assert (per_cu->cu != NULL);
24118 }
24119
24120 /* Read in a signatured type and build its CU and DIEs.
24121 If the type is a stub for the real type in a DWO file,
24122 read in the real type from the DWO file as well. */
24123
24124 static void
24125 read_signatured_type (struct signatured_type *sig_type)
24126 {
24127 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24128
24129 gdb_assert (per_cu->is_debug_types);
24130 gdb_assert (per_cu->cu == NULL);
24131
24132 cutu_reader reader (per_cu, NULL, 0, 1, false);
24133
24134 if (!reader.dummy_p)
24135 {
24136 struct dwarf2_cu *cu = reader.cu;
24137 const gdb_byte *info_ptr = reader.info_ptr;
24138
24139 gdb_assert (cu->die_hash == NULL);
24140 cu->die_hash =
24141 htab_create_alloc_ex (cu->header.length / 12,
24142 die_hash,
24143 die_eq,
24144 NULL,
24145 &cu->comp_unit_obstack,
24146 hashtab_obstack_allocate,
24147 dummy_obstack_deallocate);
24148
24149 if (reader.has_children)
24150 reader.comp_unit_die->child
24151 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24152 reader.comp_unit_die);
24153 cu->dies = reader.comp_unit_die;
24154 /* comp_unit_die is not stored in die_hash, no need. */
24155
24156 /* We try not to read any attributes in this function, because
24157 not all CUs needed for references have been loaded yet, and
24158 symbol table processing isn't initialized. But we have to
24159 set the CU language, or we won't be able to build types
24160 correctly. Similarly, if we do not read the producer, we can
24161 not apply producer-specific interpretation. */
24162 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24163 }
24164
24165 sig_type->per_cu.tu_read = 1;
24166 }
24167
24168 /* Decode simple location descriptions.
24169 Given a pointer to a dwarf block that defines a location, compute
24170 the location and return the value.
24171
24172 NOTE drow/2003-11-18: This function is called in two situations
24173 now: for the address of static or global variables (partial symbols
24174 only) and for offsets into structures which are expected to be
24175 (more or less) constant. The partial symbol case should go away,
24176 and only the constant case should remain. That will let this
24177 function complain more accurately. A few special modes are allowed
24178 without complaint for global variables (for instance, global
24179 register values and thread-local values).
24180
24181 A location description containing no operations indicates that the
24182 object is optimized out. The return value is 0 for that case.
24183 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24184 callers will only want a very basic result and this can become a
24185 complaint.
24186
24187 Note that stack[0] is unused except as a default error return. */
24188
24189 static CORE_ADDR
24190 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
24191 {
24192 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
24193 size_t i;
24194 size_t size = blk->size;
24195 const gdb_byte *data = blk->data;
24196 CORE_ADDR stack[64];
24197 int stacki;
24198 unsigned int bytes_read, unsnd;
24199 gdb_byte op;
24200
24201 i = 0;
24202 stacki = 0;
24203 stack[stacki] = 0;
24204 stack[++stacki] = 0;
24205
24206 while (i < size)
24207 {
24208 op = data[i++];
24209 switch (op)
24210 {
24211 case DW_OP_lit0:
24212 case DW_OP_lit1:
24213 case DW_OP_lit2:
24214 case DW_OP_lit3:
24215 case DW_OP_lit4:
24216 case DW_OP_lit5:
24217 case DW_OP_lit6:
24218 case DW_OP_lit7:
24219 case DW_OP_lit8:
24220 case DW_OP_lit9:
24221 case DW_OP_lit10:
24222 case DW_OP_lit11:
24223 case DW_OP_lit12:
24224 case DW_OP_lit13:
24225 case DW_OP_lit14:
24226 case DW_OP_lit15:
24227 case DW_OP_lit16:
24228 case DW_OP_lit17:
24229 case DW_OP_lit18:
24230 case DW_OP_lit19:
24231 case DW_OP_lit20:
24232 case DW_OP_lit21:
24233 case DW_OP_lit22:
24234 case DW_OP_lit23:
24235 case DW_OP_lit24:
24236 case DW_OP_lit25:
24237 case DW_OP_lit26:
24238 case DW_OP_lit27:
24239 case DW_OP_lit28:
24240 case DW_OP_lit29:
24241 case DW_OP_lit30:
24242 case DW_OP_lit31:
24243 stack[++stacki] = op - DW_OP_lit0;
24244 break;
24245
24246 case DW_OP_reg0:
24247 case DW_OP_reg1:
24248 case DW_OP_reg2:
24249 case DW_OP_reg3:
24250 case DW_OP_reg4:
24251 case DW_OP_reg5:
24252 case DW_OP_reg6:
24253 case DW_OP_reg7:
24254 case DW_OP_reg8:
24255 case DW_OP_reg9:
24256 case DW_OP_reg10:
24257 case DW_OP_reg11:
24258 case DW_OP_reg12:
24259 case DW_OP_reg13:
24260 case DW_OP_reg14:
24261 case DW_OP_reg15:
24262 case DW_OP_reg16:
24263 case DW_OP_reg17:
24264 case DW_OP_reg18:
24265 case DW_OP_reg19:
24266 case DW_OP_reg20:
24267 case DW_OP_reg21:
24268 case DW_OP_reg22:
24269 case DW_OP_reg23:
24270 case DW_OP_reg24:
24271 case DW_OP_reg25:
24272 case DW_OP_reg26:
24273 case DW_OP_reg27:
24274 case DW_OP_reg28:
24275 case DW_OP_reg29:
24276 case DW_OP_reg30:
24277 case DW_OP_reg31:
24278 stack[++stacki] = op - DW_OP_reg0;
24279 if (i < size)
24280 dwarf2_complex_location_expr_complaint ();
24281 break;
24282
24283 case DW_OP_regx:
24284 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24285 i += bytes_read;
24286 stack[++stacki] = unsnd;
24287 if (i < size)
24288 dwarf2_complex_location_expr_complaint ();
24289 break;
24290
24291 case DW_OP_addr:
24292 stack[++stacki] = read_address (objfile->obfd, &data[i],
24293 cu, &bytes_read);
24294 i += bytes_read;
24295 break;
24296
24297 case DW_OP_const1u:
24298 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24299 i += 1;
24300 break;
24301
24302 case DW_OP_const1s:
24303 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24304 i += 1;
24305 break;
24306
24307 case DW_OP_const2u:
24308 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24309 i += 2;
24310 break;
24311
24312 case DW_OP_const2s:
24313 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24314 i += 2;
24315 break;
24316
24317 case DW_OP_const4u:
24318 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24319 i += 4;
24320 break;
24321
24322 case DW_OP_const4s:
24323 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24324 i += 4;
24325 break;
24326
24327 case DW_OP_const8u:
24328 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24329 i += 8;
24330 break;
24331
24332 case DW_OP_constu:
24333 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24334 &bytes_read);
24335 i += bytes_read;
24336 break;
24337
24338 case DW_OP_consts:
24339 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24340 i += bytes_read;
24341 break;
24342
24343 case DW_OP_dup:
24344 stack[stacki + 1] = stack[stacki];
24345 stacki++;
24346 break;
24347
24348 case DW_OP_plus:
24349 stack[stacki - 1] += stack[stacki];
24350 stacki--;
24351 break;
24352
24353 case DW_OP_plus_uconst:
24354 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24355 &bytes_read);
24356 i += bytes_read;
24357 break;
24358
24359 case DW_OP_minus:
24360 stack[stacki - 1] -= stack[stacki];
24361 stacki--;
24362 break;
24363
24364 case DW_OP_deref:
24365 /* If we're not the last op, then we definitely can't encode
24366 this using GDB's address_class enum. This is valid for partial
24367 global symbols, although the variable's address will be bogus
24368 in the psymtab. */
24369 if (i < size)
24370 dwarf2_complex_location_expr_complaint ();
24371 break;
24372
24373 case DW_OP_GNU_push_tls_address:
24374 case DW_OP_form_tls_address:
24375 /* The top of the stack has the offset from the beginning
24376 of the thread control block at which the variable is located. */
24377 /* Nothing should follow this operator, so the top of stack would
24378 be returned. */
24379 /* This is valid for partial global symbols, but the variable's
24380 address will be bogus in the psymtab. Make it always at least
24381 non-zero to not look as a variable garbage collected by linker
24382 which have DW_OP_addr 0. */
24383 if (i < size)
24384 dwarf2_complex_location_expr_complaint ();
24385 stack[stacki]++;
24386 break;
24387
24388 case DW_OP_GNU_uninit:
24389 break;
24390
24391 case DW_OP_addrx:
24392 case DW_OP_GNU_addr_index:
24393 case DW_OP_GNU_const_index:
24394 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24395 &bytes_read);
24396 i += bytes_read;
24397 break;
24398
24399 default:
24400 {
24401 const char *name = get_DW_OP_name (op);
24402
24403 if (name)
24404 complaint (_("unsupported stack op: '%s'"),
24405 name);
24406 else
24407 complaint (_("unsupported stack op: '%02x'"),
24408 op);
24409 }
24410
24411 return (stack[stacki]);
24412 }
24413
24414 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24415 outside of the allocated space. Also enforce minimum>0. */
24416 if (stacki >= ARRAY_SIZE (stack) - 1)
24417 {
24418 complaint (_("location description stack overflow"));
24419 return 0;
24420 }
24421
24422 if (stacki <= 0)
24423 {
24424 complaint (_("location description stack underflow"));
24425 return 0;
24426 }
24427 }
24428 return (stack[stacki]);
24429 }
24430
24431 /* memory allocation interface */
24432
24433 static struct dwarf_block *
24434 dwarf_alloc_block (struct dwarf2_cu *cu)
24435 {
24436 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24437 }
24438
24439 static struct die_info *
24440 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24441 {
24442 struct die_info *die;
24443 size_t size = sizeof (struct die_info);
24444
24445 if (num_attrs > 1)
24446 size += (num_attrs - 1) * sizeof (struct attribute);
24447
24448 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24449 memset (die, 0, sizeof (struct die_info));
24450 return (die);
24451 }
24452
24453 \f
24454 /* Macro support. */
24455
24456 /* Return file name relative to the compilation directory of file number I in
24457 *LH's file name table. The result is allocated using xmalloc; the caller is
24458 responsible for freeing it. */
24459
24460 static char *
24461 file_file_name (int file, struct line_header *lh)
24462 {
24463 /* Is the file number a valid index into the line header's file name
24464 table? Remember that file numbers start with one, not zero. */
24465 if (lh->is_valid_file_index (file))
24466 {
24467 const file_entry *fe = lh->file_name_at (file);
24468
24469 if (!IS_ABSOLUTE_PATH (fe->name))
24470 {
24471 const char *dir = fe->include_dir (lh);
24472 if (dir != NULL)
24473 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
24474 }
24475 return xstrdup (fe->name);
24476 }
24477 else
24478 {
24479 /* The compiler produced a bogus file number. We can at least
24480 record the macro definitions made in the file, even if we
24481 won't be able to find the file by name. */
24482 char fake_name[80];
24483
24484 xsnprintf (fake_name, sizeof (fake_name),
24485 "<bad macro file number %d>", file);
24486
24487 complaint (_("bad file number in macro information (%d)"),
24488 file);
24489
24490 return xstrdup (fake_name);
24491 }
24492 }
24493
24494 /* Return the full name of file number I in *LH's file name table.
24495 Use COMP_DIR as the name of the current directory of the
24496 compilation. The result is allocated using xmalloc; the caller is
24497 responsible for freeing it. */
24498 static char *
24499 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24500 {
24501 /* Is the file number a valid index into the line header's file name
24502 table? Remember that file numbers start with one, not zero. */
24503 if (lh->is_valid_file_index (file))
24504 {
24505 char *relative = file_file_name (file, lh);
24506
24507 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24508 return relative;
24509 return reconcat (relative, comp_dir, SLASH_STRING,
24510 relative, (char *) NULL);
24511 }
24512 else
24513 return file_file_name (file, lh);
24514 }
24515
24516
24517 static struct macro_source_file *
24518 macro_start_file (struct dwarf2_cu *cu,
24519 int file, int line,
24520 struct macro_source_file *current_file,
24521 struct line_header *lh)
24522 {
24523 /* File name relative to the compilation directory of this source file. */
24524 char *file_name = file_file_name (file, lh);
24525
24526 if (! current_file)
24527 {
24528 /* Note: We don't create a macro table for this compilation unit
24529 at all until we actually get a filename. */
24530 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24531
24532 /* If we have no current file, then this must be the start_file
24533 directive for the compilation unit's main source file. */
24534 current_file = macro_set_main (macro_table, file_name);
24535 macro_define_special (macro_table);
24536 }
24537 else
24538 current_file = macro_include (current_file, line, file_name);
24539
24540 xfree (file_name);
24541
24542 return current_file;
24543 }
24544
24545 static const char *
24546 consume_improper_spaces (const char *p, const char *body)
24547 {
24548 if (*p == ' ')
24549 {
24550 complaint (_("macro definition contains spaces "
24551 "in formal argument list:\n`%s'"),
24552 body);
24553
24554 while (*p == ' ')
24555 p++;
24556 }
24557
24558 return p;
24559 }
24560
24561
24562 static void
24563 parse_macro_definition (struct macro_source_file *file, int line,
24564 const char *body)
24565 {
24566 const char *p;
24567
24568 /* The body string takes one of two forms. For object-like macro
24569 definitions, it should be:
24570
24571 <macro name> " " <definition>
24572
24573 For function-like macro definitions, it should be:
24574
24575 <macro name> "() " <definition>
24576 or
24577 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24578
24579 Spaces may appear only where explicitly indicated, and in the
24580 <definition>.
24581
24582 The Dwarf 2 spec says that an object-like macro's name is always
24583 followed by a space, but versions of GCC around March 2002 omit
24584 the space when the macro's definition is the empty string.
24585
24586 The Dwarf 2 spec says that there should be no spaces between the
24587 formal arguments in a function-like macro's formal argument list,
24588 but versions of GCC around March 2002 include spaces after the
24589 commas. */
24590
24591
24592 /* Find the extent of the macro name. The macro name is terminated
24593 by either a space or null character (for an object-like macro) or
24594 an opening paren (for a function-like macro). */
24595 for (p = body; *p; p++)
24596 if (*p == ' ' || *p == '(')
24597 break;
24598
24599 if (*p == ' ' || *p == '\0')
24600 {
24601 /* It's an object-like macro. */
24602 int name_len = p - body;
24603 std::string name (body, name_len);
24604 const char *replacement;
24605
24606 if (*p == ' ')
24607 replacement = body + name_len + 1;
24608 else
24609 {
24610 dwarf2_macro_malformed_definition_complaint (body);
24611 replacement = body + name_len;
24612 }
24613
24614 macro_define_object (file, line, name.c_str (), replacement);
24615 }
24616 else if (*p == '(')
24617 {
24618 /* It's a function-like macro. */
24619 std::string name (body, p - body);
24620 int argc = 0;
24621 int argv_size = 1;
24622 char **argv = XNEWVEC (char *, argv_size);
24623
24624 p++;
24625
24626 p = consume_improper_spaces (p, body);
24627
24628 /* Parse the formal argument list. */
24629 while (*p && *p != ')')
24630 {
24631 /* Find the extent of the current argument name. */
24632 const char *arg_start = p;
24633
24634 while (*p && *p != ',' && *p != ')' && *p != ' ')
24635 p++;
24636
24637 if (! *p || p == arg_start)
24638 dwarf2_macro_malformed_definition_complaint (body);
24639 else
24640 {
24641 /* Make sure argv has room for the new argument. */
24642 if (argc >= argv_size)
24643 {
24644 argv_size *= 2;
24645 argv = XRESIZEVEC (char *, argv, argv_size);
24646 }
24647
24648 argv[argc++] = savestring (arg_start, p - arg_start);
24649 }
24650
24651 p = consume_improper_spaces (p, body);
24652
24653 /* Consume the comma, if present. */
24654 if (*p == ',')
24655 {
24656 p++;
24657
24658 p = consume_improper_spaces (p, body);
24659 }
24660 }
24661
24662 if (*p == ')')
24663 {
24664 p++;
24665
24666 if (*p == ' ')
24667 /* Perfectly formed definition, no complaints. */
24668 macro_define_function (file, line, name.c_str (),
24669 argc, (const char **) argv,
24670 p + 1);
24671 else if (*p == '\0')
24672 {
24673 /* Complain, but do define it. */
24674 dwarf2_macro_malformed_definition_complaint (body);
24675 macro_define_function (file, line, name.c_str (),
24676 argc, (const char **) argv,
24677 p);
24678 }
24679 else
24680 /* Just complain. */
24681 dwarf2_macro_malformed_definition_complaint (body);
24682 }
24683 else
24684 /* Just complain. */
24685 dwarf2_macro_malformed_definition_complaint (body);
24686
24687 {
24688 int i;
24689
24690 for (i = 0; i < argc; i++)
24691 xfree (argv[i]);
24692 }
24693 xfree (argv);
24694 }
24695 else
24696 dwarf2_macro_malformed_definition_complaint (body);
24697 }
24698
24699 /* Skip some bytes from BYTES according to the form given in FORM.
24700 Returns the new pointer. */
24701
24702 static const gdb_byte *
24703 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24704 enum dwarf_form form,
24705 unsigned int offset_size,
24706 struct dwarf2_section_info *section)
24707 {
24708 unsigned int bytes_read;
24709
24710 switch (form)
24711 {
24712 case DW_FORM_data1:
24713 case DW_FORM_flag:
24714 ++bytes;
24715 break;
24716
24717 case DW_FORM_data2:
24718 bytes += 2;
24719 break;
24720
24721 case DW_FORM_data4:
24722 bytes += 4;
24723 break;
24724
24725 case DW_FORM_data8:
24726 bytes += 8;
24727 break;
24728
24729 case DW_FORM_data16:
24730 bytes += 16;
24731 break;
24732
24733 case DW_FORM_string:
24734 read_direct_string (abfd, bytes, &bytes_read);
24735 bytes += bytes_read;
24736 break;
24737
24738 case DW_FORM_sec_offset:
24739 case DW_FORM_strp:
24740 case DW_FORM_GNU_strp_alt:
24741 bytes += offset_size;
24742 break;
24743
24744 case DW_FORM_block:
24745 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24746 bytes += bytes_read;
24747 break;
24748
24749 case DW_FORM_block1:
24750 bytes += 1 + read_1_byte (abfd, bytes);
24751 break;
24752 case DW_FORM_block2:
24753 bytes += 2 + read_2_bytes (abfd, bytes);
24754 break;
24755 case DW_FORM_block4:
24756 bytes += 4 + read_4_bytes (abfd, bytes);
24757 break;
24758
24759 case DW_FORM_addrx:
24760 case DW_FORM_sdata:
24761 case DW_FORM_strx:
24762 case DW_FORM_udata:
24763 case DW_FORM_GNU_addr_index:
24764 case DW_FORM_GNU_str_index:
24765 bytes = gdb_skip_leb128 (bytes, buffer_end);
24766 if (bytes == NULL)
24767 {
24768 dwarf2_section_buffer_overflow_complaint (section);
24769 return NULL;
24770 }
24771 break;
24772
24773 case DW_FORM_implicit_const:
24774 break;
24775
24776 default:
24777 {
24778 complaint (_("invalid form 0x%x in `%s'"),
24779 form, get_section_name (section));
24780 return NULL;
24781 }
24782 }
24783
24784 return bytes;
24785 }
24786
24787 /* A helper for dwarf_decode_macros that handles skipping an unknown
24788 opcode. Returns an updated pointer to the macro data buffer; or,
24789 on error, issues a complaint and returns NULL. */
24790
24791 static const gdb_byte *
24792 skip_unknown_opcode (unsigned int opcode,
24793 const gdb_byte **opcode_definitions,
24794 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24795 bfd *abfd,
24796 unsigned int offset_size,
24797 struct dwarf2_section_info *section)
24798 {
24799 unsigned int bytes_read, i;
24800 unsigned long arg;
24801 const gdb_byte *defn;
24802
24803 if (opcode_definitions[opcode] == NULL)
24804 {
24805 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24806 opcode);
24807 return NULL;
24808 }
24809
24810 defn = opcode_definitions[opcode];
24811 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24812 defn += bytes_read;
24813
24814 for (i = 0; i < arg; ++i)
24815 {
24816 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24817 (enum dwarf_form) defn[i], offset_size,
24818 section);
24819 if (mac_ptr == NULL)
24820 {
24821 /* skip_form_bytes already issued the complaint. */
24822 return NULL;
24823 }
24824 }
24825
24826 return mac_ptr;
24827 }
24828
24829 /* A helper function which parses the header of a macro section.
24830 If the macro section is the extended (for now called "GNU") type,
24831 then this updates *OFFSET_SIZE. Returns a pointer to just after
24832 the header, or issues a complaint and returns NULL on error. */
24833
24834 static const gdb_byte *
24835 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24836 bfd *abfd,
24837 const gdb_byte *mac_ptr,
24838 unsigned int *offset_size,
24839 int section_is_gnu)
24840 {
24841 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24842
24843 if (section_is_gnu)
24844 {
24845 unsigned int version, flags;
24846
24847 version = read_2_bytes (abfd, mac_ptr);
24848 if (version != 4 && version != 5)
24849 {
24850 complaint (_("unrecognized version `%d' in .debug_macro section"),
24851 version);
24852 return NULL;
24853 }
24854 mac_ptr += 2;
24855
24856 flags = read_1_byte (abfd, mac_ptr);
24857 ++mac_ptr;
24858 *offset_size = (flags & 1) ? 8 : 4;
24859
24860 if ((flags & 2) != 0)
24861 /* We don't need the line table offset. */
24862 mac_ptr += *offset_size;
24863
24864 /* Vendor opcode descriptions. */
24865 if ((flags & 4) != 0)
24866 {
24867 unsigned int i, count;
24868
24869 count = read_1_byte (abfd, mac_ptr);
24870 ++mac_ptr;
24871 for (i = 0; i < count; ++i)
24872 {
24873 unsigned int opcode, bytes_read;
24874 unsigned long arg;
24875
24876 opcode = read_1_byte (abfd, mac_ptr);
24877 ++mac_ptr;
24878 opcode_definitions[opcode] = mac_ptr;
24879 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24880 mac_ptr += bytes_read;
24881 mac_ptr += arg;
24882 }
24883 }
24884 }
24885
24886 return mac_ptr;
24887 }
24888
24889 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24890 including DW_MACRO_import. */
24891
24892 static void
24893 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24894 bfd *abfd,
24895 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24896 struct macro_source_file *current_file,
24897 struct line_header *lh,
24898 struct dwarf2_section_info *section,
24899 int section_is_gnu, int section_is_dwz,
24900 unsigned int offset_size,
24901 htab_t include_hash)
24902 {
24903 struct dwarf2_per_objfile *dwarf2_per_objfile
24904 = cu->per_cu->dwarf2_per_objfile;
24905 struct objfile *objfile = dwarf2_per_objfile->objfile;
24906 enum dwarf_macro_record_type macinfo_type;
24907 int at_commandline;
24908 const gdb_byte *opcode_definitions[256];
24909
24910 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24911 &offset_size, section_is_gnu);
24912 if (mac_ptr == NULL)
24913 {
24914 /* We already issued a complaint. */
24915 return;
24916 }
24917
24918 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24919 GDB is still reading the definitions from command line. First
24920 DW_MACINFO_start_file will need to be ignored as it was already executed
24921 to create CURRENT_FILE for the main source holding also the command line
24922 definitions. On first met DW_MACINFO_start_file this flag is reset to
24923 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24924
24925 at_commandline = 1;
24926
24927 do
24928 {
24929 /* Do we at least have room for a macinfo type byte? */
24930 if (mac_ptr >= mac_end)
24931 {
24932 dwarf2_section_buffer_overflow_complaint (section);
24933 break;
24934 }
24935
24936 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24937 mac_ptr++;
24938
24939 /* Note that we rely on the fact that the corresponding GNU and
24940 DWARF constants are the same. */
24941 DIAGNOSTIC_PUSH
24942 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24943 switch (macinfo_type)
24944 {
24945 /* A zero macinfo type indicates the end of the macro
24946 information. */
24947 case 0:
24948 break;
24949
24950 case DW_MACRO_define:
24951 case DW_MACRO_undef:
24952 case DW_MACRO_define_strp:
24953 case DW_MACRO_undef_strp:
24954 case DW_MACRO_define_sup:
24955 case DW_MACRO_undef_sup:
24956 {
24957 unsigned int bytes_read;
24958 int line;
24959 const char *body;
24960 int is_define;
24961
24962 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24963 mac_ptr += bytes_read;
24964
24965 if (macinfo_type == DW_MACRO_define
24966 || macinfo_type == DW_MACRO_undef)
24967 {
24968 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24969 mac_ptr += bytes_read;
24970 }
24971 else
24972 {
24973 LONGEST str_offset;
24974
24975 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24976 mac_ptr += offset_size;
24977
24978 if (macinfo_type == DW_MACRO_define_sup
24979 || macinfo_type == DW_MACRO_undef_sup
24980 || section_is_dwz)
24981 {
24982 struct dwz_file *dwz
24983 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24984
24985 body = read_indirect_string_from_dwz (objfile,
24986 dwz, str_offset);
24987 }
24988 else
24989 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24990 abfd, str_offset);
24991 }
24992
24993 is_define = (macinfo_type == DW_MACRO_define
24994 || macinfo_type == DW_MACRO_define_strp
24995 || macinfo_type == DW_MACRO_define_sup);
24996 if (! current_file)
24997 {
24998 /* DWARF violation as no main source is present. */
24999 complaint (_("debug info with no main source gives macro %s "
25000 "on line %d: %s"),
25001 is_define ? _("definition") : _("undefinition"),
25002 line, body);
25003 break;
25004 }
25005 if ((line == 0 && !at_commandline)
25006 || (line != 0 && at_commandline))
25007 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
25008 at_commandline ? _("command-line") : _("in-file"),
25009 is_define ? _("definition") : _("undefinition"),
25010 line == 0 ? _("zero") : _("non-zero"), line, body);
25011
25012 if (body == NULL)
25013 {
25014 /* Fedora's rpm-build's "debugedit" binary
25015 corrupted .debug_macro sections.
25016
25017 For more info, see
25018 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
25019 complaint (_("debug info gives %s invalid macro %s "
25020 "without body (corrupted?) at line %d "
25021 "on file %s"),
25022 at_commandline ? _("command-line") : _("in-file"),
25023 is_define ? _("definition") : _("undefinition"),
25024 line, current_file->filename);
25025 }
25026 else if (is_define)
25027 parse_macro_definition (current_file, line, body);
25028 else
25029 {
25030 gdb_assert (macinfo_type == DW_MACRO_undef
25031 || macinfo_type == DW_MACRO_undef_strp
25032 || macinfo_type == DW_MACRO_undef_sup);
25033 macro_undef (current_file, line, body);
25034 }
25035 }
25036 break;
25037
25038 case DW_MACRO_start_file:
25039 {
25040 unsigned int bytes_read;
25041 int line, file;
25042
25043 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25044 mac_ptr += bytes_read;
25045 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25046 mac_ptr += bytes_read;
25047
25048 if ((line == 0 && !at_commandline)
25049 || (line != 0 && at_commandline))
25050 complaint (_("debug info gives source %d included "
25051 "from %s at %s line %d"),
25052 file, at_commandline ? _("command-line") : _("file"),
25053 line == 0 ? _("zero") : _("non-zero"), line);
25054
25055 if (at_commandline)
25056 {
25057 /* This DW_MACRO_start_file was executed in the
25058 pass one. */
25059 at_commandline = 0;
25060 }
25061 else
25062 current_file = macro_start_file (cu, file, line, current_file,
25063 lh);
25064 }
25065 break;
25066
25067 case DW_MACRO_end_file:
25068 if (! current_file)
25069 complaint (_("macro debug info has an unmatched "
25070 "`close_file' directive"));
25071 else
25072 {
25073 current_file = current_file->included_by;
25074 if (! current_file)
25075 {
25076 enum dwarf_macro_record_type next_type;
25077
25078 /* GCC circa March 2002 doesn't produce the zero
25079 type byte marking the end of the compilation
25080 unit. Complain if it's not there, but exit no
25081 matter what. */
25082
25083 /* Do we at least have room for a macinfo type byte? */
25084 if (mac_ptr >= mac_end)
25085 {
25086 dwarf2_section_buffer_overflow_complaint (section);
25087 return;
25088 }
25089
25090 /* We don't increment mac_ptr here, so this is just
25091 a look-ahead. */
25092 next_type
25093 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25094 mac_ptr);
25095 if (next_type != 0)
25096 complaint (_("no terminating 0-type entry for "
25097 "macros in `.debug_macinfo' section"));
25098
25099 return;
25100 }
25101 }
25102 break;
25103
25104 case DW_MACRO_import:
25105 case DW_MACRO_import_sup:
25106 {
25107 LONGEST offset;
25108 void **slot;
25109 bfd *include_bfd = abfd;
25110 struct dwarf2_section_info *include_section = section;
25111 const gdb_byte *include_mac_end = mac_end;
25112 int is_dwz = section_is_dwz;
25113 const gdb_byte *new_mac_ptr;
25114
25115 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25116 mac_ptr += offset_size;
25117
25118 if (macinfo_type == DW_MACRO_import_sup)
25119 {
25120 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
25121
25122 dwarf2_read_section (objfile, &dwz->macro);
25123
25124 include_section = &dwz->macro;
25125 include_bfd = get_section_bfd_owner (include_section);
25126 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25127 is_dwz = 1;
25128 }
25129
25130 new_mac_ptr = include_section->buffer + offset;
25131 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25132
25133 if (*slot != NULL)
25134 {
25135 /* This has actually happened; see
25136 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
25137 complaint (_("recursive DW_MACRO_import in "
25138 ".debug_macro section"));
25139 }
25140 else
25141 {
25142 *slot = (void *) new_mac_ptr;
25143
25144 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
25145 include_mac_end, current_file, lh,
25146 section, section_is_gnu, is_dwz,
25147 offset_size, include_hash);
25148
25149 htab_remove_elt (include_hash, (void *) new_mac_ptr);
25150 }
25151 }
25152 break;
25153
25154 case DW_MACINFO_vendor_ext:
25155 if (!section_is_gnu)
25156 {
25157 unsigned int bytes_read;
25158
25159 /* This reads the constant, but since we don't recognize
25160 any vendor extensions, we ignore it. */
25161 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25162 mac_ptr += bytes_read;
25163 read_direct_string (abfd, mac_ptr, &bytes_read);
25164 mac_ptr += bytes_read;
25165
25166 /* We don't recognize any vendor extensions. */
25167 break;
25168 }
25169 /* FALLTHROUGH */
25170
25171 default:
25172 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25173 mac_ptr, mac_end, abfd, offset_size,
25174 section);
25175 if (mac_ptr == NULL)
25176 return;
25177 break;
25178 }
25179 DIAGNOSTIC_POP
25180 } while (macinfo_type != 0);
25181 }
25182
25183 static void
25184 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
25185 int section_is_gnu)
25186 {
25187 struct dwarf2_per_objfile *dwarf2_per_objfile
25188 = cu->per_cu->dwarf2_per_objfile;
25189 struct objfile *objfile = dwarf2_per_objfile->objfile;
25190 struct line_header *lh = cu->line_header;
25191 bfd *abfd;
25192 const gdb_byte *mac_ptr, *mac_end;
25193 struct macro_source_file *current_file = 0;
25194 enum dwarf_macro_record_type macinfo_type;
25195 unsigned int offset_size = cu->header.offset_size;
25196 const gdb_byte *opcode_definitions[256];
25197 void **slot;
25198 struct dwarf2_section_info *section;
25199 const char *section_name;
25200
25201 if (cu->dwo_unit != NULL)
25202 {
25203 if (section_is_gnu)
25204 {
25205 section = &cu->dwo_unit->dwo_file->sections.macro;
25206 section_name = ".debug_macro.dwo";
25207 }
25208 else
25209 {
25210 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25211 section_name = ".debug_macinfo.dwo";
25212 }
25213 }
25214 else
25215 {
25216 if (section_is_gnu)
25217 {
25218 section = &dwarf2_per_objfile->macro;
25219 section_name = ".debug_macro";
25220 }
25221 else
25222 {
25223 section = &dwarf2_per_objfile->macinfo;
25224 section_name = ".debug_macinfo";
25225 }
25226 }
25227
25228 dwarf2_read_section (objfile, section);
25229 if (section->buffer == NULL)
25230 {
25231 complaint (_("missing %s section"), section_name);
25232 return;
25233 }
25234 abfd = get_section_bfd_owner (section);
25235
25236 /* First pass: Find the name of the base filename.
25237 This filename is needed in order to process all macros whose definition
25238 (or undefinition) comes from the command line. These macros are defined
25239 before the first DW_MACINFO_start_file entry, and yet still need to be
25240 associated to the base file.
25241
25242 To determine the base file name, we scan the macro definitions until we
25243 reach the first DW_MACINFO_start_file entry. We then initialize
25244 CURRENT_FILE accordingly so that any macro definition found before the
25245 first DW_MACINFO_start_file can still be associated to the base file. */
25246
25247 mac_ptr = section->buffer + offset;
25248 mac_end = section->buffer + section->size;
25249
25250 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25251 &offset_size, section_is_gnu);
25252 if (mac_ptr == NULL)
25253 {
25254 /* We already issued a complaint. */
25255 return;
25256 }
25257
25258 do
25259 {
25260 /* Do we at least have room for a macinfo type byte? */
25261 if (mac_ptr >= mac_end)
25262 {
25263 /* Complaint is printed during the second pass as GDB will probably
25264 stop the first pass earlier upon finding
25265 DW_MACINFO_start_file. */
25266 break;
25267 }
25268
25269 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25270 mac_ptr++;
25271
25272 /* Note that we rely on the fact that the corresponding GNU and
25273 DWARF constants are the same. */
25274 DIAGNOSTIC_PUSH
25275 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25276 switch (macinfo_type)
25277 {
25278 /* A zero macinfo type indicates the end of the macro
25279 information. */
25280 case 0:
25281 break;
25282
25283 case DW_MACRO_define:
25284 case DW_MACRO_undef:
25285 /* Only skip the data by MAC_PTR. */
25286 {
25287 unsigned int bytes_read;
25288
25289 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25290 mac_ptr += bytes_read;
25291 read_direct_string (abfd, mac_ptr, &bytes_read);
25292 mac_ptr += bytes_read;
25293 }
25294 break;
25295
25296 case DW_MACRO_start_file:
25297 {
25298 unsigned int bytes_read;
25299 int line, file;
25300
25301 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25302 mac_ptr += bytes_read;
25303 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25304 mac_ptr += bytes_read;
25305
25306 current_file = macro_start_file (cu, file, line, current_file, lh);
25307 }
25308 break;
25309
25310 case DW_MACRO_end_file:
25311 /* No data to skip by MAC_PTR. */
25312 break;
25313
25314 case DW_MACRO_define_strp:
25315 case DW_MACRO_undef_strp:
25316 case DW_MACRO_define_sup:
25317 case DW_MACRO_undef_sup:
25318 {
25319 unsigned int bytes_read;
25320
25321 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25322 mac_ptr += bytes_read;
25323 mac_ptr += offset_size;
25324 }
25325 break;
25326
25327 case DW_MACRO_import:
25328 case DW_MACRO_import_sup:
25329 /* Note that, according to the spec, a transparent include
25330 chain cannot call DW_MACRO_start_file. So, we can just
25331 skip this opcode. */
25332 mac_ptr += offset_size;
25333 break;
25334
25335 case DW_MACINFO_vendor_ext:
25336 /* Only skip the data by MAC_PTR. */
25337 if (!section_is_gnu)
25338 {
25339 unsigned int bytes_read;
25340
25341 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25342 mac_ptr += bytes_read;
25343 read_direct_string (abfd, mac_ptr, &bytes_read);
25344 mac_ptr += bytes_read;
25345 }
25346 /* FALLTHROUGH */
25347
25348 default:
25349 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25350 mac_ptr, mac_end, abfd, offset_size,
25351 section);
25352 if (mac_ptr == NULL)
25353 return;
25354 break;
25355 }
25356 DIAGNOSTIC_POP
25357 } while (macinfo_type != 0 && current_file == NULL);
25358
25359 /* Second pass: Process all entries.
25360
25361 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25362 command-line macro definitions/undefinitions. This flag is unset when we
25363 reach the first DW_MACINFO_start_file entry. */
25364
25365 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25366 htab_eq_pointer,
25367 NULL, xcalloc, xfree));
25368 mac_ptr = section->buffer + offset;
25369 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25370 *slot = (void *) mac_ptr;
25371 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25372 current_file, lh, section,
25373 section_is_gnu, 0, offset_size,
25374 include_hash.get ());
25375 }
25376
25377 /* Check if the attribute's form is a DW_FORM_block*
25378 if so return true else false. */
25379
25380 static int
25381 attr_form_is_block (const struct attribute *attr)
25382 {
25383 return (attr == NULL ? 0 :
25384 attr->form == DW_FORM_block1
25385 || attr->form == DW_FORM_block2
25386 || attr->form == DW_FORM_block4
25387 || attr->form == DW_FORM_block
25388 || attr->form == DW_FORM_exprloc);
25389 }
25390
25391 /* Return non-zero if ATTR's value is a section offset --- classes
25392 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25393 You may use DW_UNSND (attr) to retrieve such offsets.
25394
25395 Section 7.5.4, "Attribute Encodings", explains that no attribute
25396 may have a value that belongs to more than one of these classes; it
25397 would be ambiguous if we did, because we use the same forms for all
25398 of them. */
25399
25400 static int
25401 attr_form_is_section_offset (const struct attribute *attr)
25402 {
25403 return (attr->form == DW_FORM_data4
25404 || attr->form == DW_FORM_data8
25405 || attr->form == DW_FORM_sec_offset);
25406 }
25407
25408 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25409 zero otherwise. When this function returns true, you can apply
25410 dwarf2_get_attr_constant_value to it.
25411
25412 However, note that for some attributes you must check
25413 attr_form_is_section_offset before using this test. DW_FORM_data4
25414 and DW_FORM_data8 are members of both the constant class, and of
25415 the classes that contain offsets into other debug sections
25416 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25417 that, if an attribute's can be either a constant or one of the
25418 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25419 taken as section offsets, not constants.
25420
25421 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25422 cannot handle that. */
25423
25424 static int
25425 attr_form_is_constant (const struct attribute *attr)
25426 {
25427 switch (attr->form)
25428 {
25429 case DW_FORM_sdata:
25430 case DW_FORM_udata:
25431 case DW_FORM_data1:
25432 case DW_FORM_data2:
25433 case DW_FORM_data4:
25434 case DW_FORM_data8:
25435 case DW_FORM_implicit_const:
25436 return 1;
25437 default:
25438 return 0;
25439 }
25440 }
25441
25442
25443 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25444 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25445
25446 static int
25447 attr_form_is_ref (const struct attribute *attr)
25448 {
25449 switch (attr->form)
25450 {
25451 case DW_FORM_ref_addr:
25452 case DW_FORM_ref1:
25453 case DW_FORM_ref2:
25454 case DW_FORM_ref4:
25455 case DW_FORM_ref8:
25456 case DW_FORM_ref_udata:
25457 case DW_FORM_GNU_ref_alt:
25458 return 1;
25459 default:
25460 return 0;
25461 }
25462 }
25463
25464 /* Return the .debug_loc section to use for CU.
25465 For DWO files use .debug_loc.dwo. */
25466
25467 static struct dwarf2_section_info *
25468 cu_debug_loc_section (struct dwarf2_cu *cu)
25469 {
25470 struct dwarf2_per_objfile *dwarf2_per_objfile
25471 = cu->per_cu->dwarf2_per_objfile;
25472
25473 if (cu->dwo_unit)
25474 {
25475 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25476
25477 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25478 }
25479 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25480 : &dwarf2_per_objfile->loc);
25481 }
25482
25483 /* A helper function that fills in a dwarf2_loclist_baton. */
25484
25485 static void
25486 fill_in_loclist_baton (struct dwarf2_cu *cu,
25487 struct dwarf2_loclist_baton *baton,
25488 const struct attribute *attr)
25489 {
25490 struct dwarf2_per_objfile *dwarf2_per_objfile
25491 = cu->per_cu->dwarf2_per_objfile;
25492 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25493
25494 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25495
25496 baton->per_cu = cu->per_cu;
25497 gdb_assert (baton->per_cu);
25498 /* We don't know how long the location list is, but make sure we
25499 don't run off the edge of the section. */
25500 baton->size = section->size - DW_UNSND (attr);
25501 baton->data = section->buffer + DW_UNSND (attr);
25502 baton->base_address = cu->base_address;
25503 baton->from_dwo = cu->dwo_unit != NULL;
25504 }
25505
25506 static void
25507 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25508 struct dwarf2_cu *cu, int is_block)
25509 {
25510 struct dwarf2_per_objfile *dwarf2_per_objfile
25511 = cu->per_cu->dwarf2_per_objfile;
25512 struct objfile *objfile = dwarf2_per_objfile->objfile;
25513 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25514
25515 if (attr_form_is_section_offset (attr)
25516 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25517 the section. If so, fall through to the complaint in the
25518 other branch. */
25519 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25520 {
25521 struct dwarf2_loclist_baton *baton;
25522
25523 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25524
25525 fill_in_loclist_baton (cu, baton, attr);
25526
25527 if (cu->base_known == 0)
25528 complaint (_("Location list used without "
25529 "specifying the CU base address."));
25530
25531 SYMBOL_ACLASS_INDEX (sym) = (is_block
25532 ? dwarf2_loclist_block_index
25533 : dwarf2_loclist_index);
25534 SYMBOL_LOCATION_BATON (sym) = baton;
25535 }
25536 else
25537 {
25538 struct dwarf2_locexpr_baton *baton;
25539
25540 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25541 baton->per_cu = cu->per_cu;
25542 gdb_assert (baton->per_cu);
25543
25544 if (attr_form_is_block (attr))
25545 {
25546 /* Note that we're just copying the block's data pointer
25547 here, not the actual data. We're still pointing into the
25548 info_buffer for SYM's objfile; right now we never release
25549 that buffer, but when we do clean up properly this may
25550 need to change. */
25551 baton->size = DW_BLOCK (attr)->size;
25552 baton->data = DW_BLOCK (attr)->data;
25553 }
25554 else
25555 {
25556 dwarf2_invalid_attrib_class_complaint ("location description",
25557 sym->natural_name ());
25558 baton->size = 0;
25559 }
25560
25561 SYMBOL_ACLASS_INDEX (sym) = (is_block
25562 ? dwarf2_locexpr_block_index
25563 : dwarf2_locexpr_index);
25564 SYMBOL_LOCATION_BATON (sym) = baton;
25565 }
25566 }
25567
25568 /* Return the OBJFILE associated with the compilation unit CU. If CU
25569 came from a separate debuginfo file, then the master objfile is
25570 returned. */
25571
25572 struct objfile *
25573 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25574 {
25575 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25576
25577 /* Return the master objfile, so that we can report and look up the
25578 correct file containing this variable. */
25579 if (objfile->separate_debug_objfile_backlink)
25580 objfile = objfile->separate_debug_objfile_backlink;
25581
25582 return objfile;
25583 }
25584
25585 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25586 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25587 CU_HEADERP first. */
25588
25589 static const struct comp_unit_head *
25590 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25591 struct dwarf2_per_cu_data *per_cu)
25592 {
25593 const gdb_byte *info_ptr;
25594
25595 if (per_cu->cu)
25596 return &per_cu->cu->header;
25597
25598 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25599
25600 memset (cu_headerp, 0, sizeof (*cu_headerp));
25601 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25602 rcuh_kind::COMPILE);
25603
25604 return cu_headerp;
25605 }
25606
25607 /* Return the address size given in the compilation unit header for CU. */
25608
25609 int
25610 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25611 {
25612 struct comp_unit_head cu_header_local;
25613 const struct comp_unit_head *cu_headerp;
25614
25615 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25616
25617 return cu_headerp->addr_size;
25618 }
25619
25620 /* Return the offset size given in the compilation unit header for CU. */
25621
25622 int
25623 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25624 {
25625 struct comp_unit_head cu_header_local;
25626 const struct comp_unit_head *cu_headerp;
25627
25628 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25629
25630 return cu_headerp->offset_size;
25631 }
25632
25633 /* See its dwarf2loc.h declaration. */
25634
25635 int
25636 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25637 {
25638 struct comp_unit_head cu_header_local;
25639 const struct comp_unit_head *cu_headerp;
25640
25641 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25642
25643 if (cu_headerp->version == 2)
25644 return cu_headerp->addr_size;
25645 else
25646 return cu_headerp->offset_size;
25647 }
25648
25649 /* Return the text offset of the CU. The returned offset comes from
25650 this CU's objfile. If this objfile came from a separate debuginfo
25651 file, then the offset may be different from the corresponding
25652 offset in the parent objfile. */
25653
25654 CORE_ADDR
25655 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25656 {
25657 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
25658 }
25659
25660 /* Return a type that is a generic pointer type, the size of which matches
25661 the address size given in the compilation unit header for PER_CU. */
25662 static struct type *
25663 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25664 {
25665 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25666 struct type *void_type = objfile_type (objfile)->builtin_void;
25667 struct type *addr_type = lookup_pointer_type (void_type);
25668 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25669
25670 if (TYPE_LENGTH (addr_type) == addr_size)
25671 return addr_type;
25672
25673 addr_type
25674 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25675 return addr_type;
25676 }
25677
25678 /* Return DWARF version number of PER_CU. */
25679
25680 short
25681 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25682 {
25683 return per_cu->dwarf_version;
25684 }
25685
25686 /* Locate the .debug_info compilation unit from CU's objfile which contains
25687 the DIE at OFFSET. Raises an error on failure. */
25688
25689 static struct dwarf2_per_cu_data *
25690 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25691 unsigned int offset_in_dwz,
25692 struct dwarf2_per_objfile *dwarf2_per_objfile)
25693 {
25694 struct dwarf2_per_cu_data *this_cu;
25695 int low, high;
25696
25697 low = 0;
25698 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25699 while (high > low)
25700 {
25701 struct dwarf2_per_cu_data *mid_cu;
25702 int mid = low + (high - low) / 2;
25703
25704 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25705 if (mid_cu->is_dwz > offset_in_dwz
25706 || (mid_cu->is_dwz == offset_in_dwz
25707 && mid_cu->sect_off + mid_cu->length >= sect_off))
25708 high = mid;
25709 else
25710 low = mid + 1;
25711 }
25712 gdb_assert (low == high);
25713 this_cu = dwarf2_per_objfile->all_comp_units[low];
25714 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25715 {
25716 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25717 error (_("Dwarf Error: could not find partial DIE containing "
25718 "offset %s [in module %s]"),
25719 sect_offset_str (sect_off),
25720 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25721
25722 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25723 <= sect_off);
25724 return dwarf2_per_objfile->all_comp_units[low-1];
25725 }
25726 else
25727 {
25728 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25729 && sect_off >= this_cu->sect_off + this_cu->length)
25730 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25731 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25732 return this_cu;
25733 }
25734 }
25735
25736 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25737
25738 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25739 : per_cu (per_cu_),
25740 mark (false),
25741 has_loclist (false),
25742 checked_producer (false),
25743 producer_is_gxx_lt_4_6 (false),
25744 producer_is_gcc_lt_4_3 (false),
25745 producer_is_icc (false),
25746 producer_is_icc_lt_14 (false),
25747 producer_is_codewarrior (false),
25748 processing_has_namespace_info (false)
25749 {
25750 per_cu->cu = this;
25751 }
25752
25753 /* Destroy a dwarf2_cu. */
25754
25755 dwarf2_cu::~dwarf2_cu ()
25756 {
25757 per_cu->cu = NULL;
25758 }
25759
25760 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25761
25762 static void
25763 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25764 enum language pretend_language)
25765 {
25766 struct attribute *attr;
25767
25768 /* Set the language we're debugging. */
25769 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25770 if (attr != nullptr)
25771 set_cu_language (DW_UNSND (attr), cu);
25772 else
25773 {
25774 cu->language = pretend_language;
25775 cu->language_defn = language_def (cu->language);
25776 }
25777
25778 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25779 }
25780
25781 /* Increase the age counter on each cached compilation unit, and free
25782 any that are too old. */
25783
25784 static void
25785 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25786 {
25787 struct dwarf2_per_cu_data *per_cu, **last_chain;
25788
25789 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25790 per_cu = dwarf2_per_objfile->read_in_chain;
25791 while (per_cu != NULL)
25792 {
25793 per_cu->cu->last_used ++;
25794 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25795 dwarf2_mark (per_cu->cu);
25796 per_cu = per_cu->cu->read_in_chain;
25797 }
25798
25799 per_cu = dwarf2_per_objfile->read_in_chain;
25800 last_chain = &dwarf2_per_objfile->read_in_chain;
25801 while (per_cu != NULL)
25802 {
25803 struct dwarf2_per_cu_data *next_cu;
25804
25805 next_cu = per_cu->cu->read_in_chain;
25806
25807 if (!per_cu->cu->mark)
25808 {
25809 delete per_cu->cu;
25810 *last_chain = next_cu;
25811 }
25812 else
25813 last_chain = &per_cu->cu->read_in_chain;
25814
25815 per_cu = next_cu;
25816 }
25817 }
25818
25819 /* Remove a single compilation unit from the cache. */
25820
25821 static void
25822 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25823 {
25824 struct dwarf2_per_cu_data *per_cu, **last_chain;
25825 struct dwarf2_per_objfile *dwarf2_per_objfile
25826 = target_per_cu->dwarf2_per_objfile;
25827
25828 per_cu = dwarf2_per_objfile->read_in_chain;
25829 last_chain = &dwarf2_per_objfile->read_in_chain;
25830 while (per_cu != NULL)
25831 {
25832 struct dwarf2_per_cu_data *next_cu;
25833
25834 next_cu = per_cu->cu->read_in_chain;
25835
25836 if (per_cu == target_per_cu)
25837 {
25838 delete per_cu->cu;
25839 per_cu->cu = NULL;
25840 *last_chain = next_cu;
25841 break;
25842 }
25843 else
25844 last_chain = &per_cu->cu->read_in_chain;
25845
25846 per_cu = next_cu;
25847 }
25848 }
25849
25850 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25851 We store these in a hash table separate from the DIEs, and preserve them
25852 when the DIEs are flushed out of cache.
25853
25854 The CU "per_cu" pointer is needed because offset alone is not enough to
25855 uniquely identify the type. A file may have multiple .debug_types sections,
25856 or the type may come from a DWO file. Furthermore, while it's more logical
25857 to use per_cu->section+offset, with Fission the section with the data is in
25858 the DWO file but we don't know that section at the point we need it.
25859 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25860 because we can enter the lookup routine, get_die_type_at_offset, from
25861 outside this file, and thus won't necessarily have PER_CU->cu.
25862 Fortunately, PER_CU is stable for the life of the objfile. */
25863
25864 struct dwarf2_per_cu_offset_and_type
25865 {
25866 const struct dwarf2_per_cu_data *per_cu;
25867 sect_offset sect_off;
25868 struct type *type;
25869 };
25870
25871 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25872
25873 static hashval_t
25874 per_cu_offset_and_type_hash (const void *item)
25875 {
25876 const struct dwarf2_per_cu_offset_and_type *ofs
25877 = (const struct dwarf2_per_cu_offset_and_type *) item;
25878
25879 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25880 }
25881
25882 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25883
25884 static int
25885 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25886 {
25887 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25888 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25889 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25890 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25891
25892 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25893 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25894 }
25895
25896 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25897 table if necessary. For convenience, return TYPE.
25898
25899 The DIEs reading must have careful ordering to:
25900 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25901 reading current DIE.
25902 * Not trying to dereference contents of still incompletely read in types
25903 while reading in other DIEs.
25904 * Enable referencing still incompletely read in types just by a pointer to
25905 the type without accessing its fields.
25906
25907 Therefore caller should follow these rules:
25908 * Try to fetch any prerequisite types we may need to build this DIE type
25909 before building the type and calling set_die_type.
25910 * After building type call set_die_type for current DIE as soon as
25911 possible before fetching more types to complete the current type.
25912 * Make the type as complete as possible before fetching more types. */
25913
25914 static struct type *
25915 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25916 {
25917 struct dwarf2_per_objfile *dwarf2_per_objfile
25918 = cu->per_cu->dwarf2_per_objfile;
25919 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25920 struct objfile *objfile = dwarf2_per_objfile->objfile;
25921 struct attribute *attr;
25922 struct dynamic_prop prop;
25923
25924 /* For Ada types, make sure that the gnat-specific data is always
25925 initialized (if not already set). There are a few types where
25926 we should not be doing so, because the type-specific area is
25927 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25928 where the type-specific area is used to store the floatformat).
25929 But this is not a problem, because the gnat-specific information
25930 is actually not needed for these types. */
25931 if (need_gnat_info (cu)
25932 && TYPE_CODE (type) != TYPE_CODE_FUNC
25933 && TYPE_CODE (type) != TYPE_CODE_FLT
25934 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25935 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25936 && TYPE_CODE (type) != TYPE_CODE_METHOD
25937 && !HAVE_GNAT_AUX_INFO (type))
25938 INIT_GNAT_SPECIFIC (type);
25939
25940 /* Read DW_AT_allocated and set in type. */
25941 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25942 if (attr_form_is_block (attr))
25943 {
25944 struct type *prop_type
25945 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25946 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25947 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25948 }
25949 else if (attr != NULL)
25950 {
25951 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25952 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25953 sect_offset_str (die->sect_off));
25954 }
25955
25956 /* Read DW_AT_associated and set in type. */
25957 attr = dwarf2_attr (die, DW_AT_associated, cu);
25958 if (attr_form_is_block (attr))
25959 {
25960 struct type *prop_type
25961 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25962 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25963 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25964 }
25965 else if (attr != NULL)
25966 {
25967 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25968 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25969 sect_offset_str (die->sect_off));
25970 }
25971
25972 /* Read DW_AT_data_location and set in type. */
25973 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25974 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25975 dwarf2_per_cu_addr_type (cu->per_cu)))
25976 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25977
25978 if (dwarf2_per_objfile->die_type_hash == NULL)
25979 {
25980 dwarf2_per_objfile->die_type_hash =
25981 htab_create_alloc_ex (127,
25982 per_cu_offset_and_type_hash,
25983 per_cu_offset_and_type_eq,
25984 NULL,
25985 &objfile->objfile_obstack,
25986 hashtab_obstack_allocate,
25987 dummy_obstack_deallocate);
25988 }
25989
25990 ofs.per_cu = cu->per_cu;
25991 ofs.sect_off = die->sect_off;
25992 ofs.type = type;
25993 slot = (struct dwarf2_per_cu_offset_and_type **)
25994 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
25995 if (*slot)
25996 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25997 sect_offset_str (die->sect_off));
25998 *slot = XOBNEW (&objfile->objfile_obstack,
25999 struct dwarf2_per_cu_offset_and_type);
26000 **slot = ofs;
26001 return type;
26002 }
26003
26004 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
26005 or return NULL if the die does not have a saved type. */
26006
26007 static struct type *
26008 get_die_type_at_offset (sect_offset sect_off,
26009 struct dwarf2_per_cu_data *per_cu)
26010 {
26011 struct dwarf2_per_cu_offset_and_type *slot, ofs;
26012 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
26013
26014 if (dwarf2_per_objfile->die_type_hash == NULL)
26015 return NULL;
26016
26017 ofs.per_cu = per_cu;
26018 ofs.sect_off = sect_off;
26019 slot = ((struct dwarf2_per_cu_offset_and_type *)
26020 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
26021 if (slot)
26022 return slot->type;
26023 else
26024 return NULL;
26025 }
26026
26027 /* Look up the type for DIE in CU in die_type_hash,
26028 or return NULL if DIE does not have a saved type. */
26029
26030 static struct type *
26031 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
26032 {
26033 return get_die_type_at_offset (die->sect_off, cu->per_cu);
26034 }
26035
26036 /* Add a dependence relationship from CU to REF_PER_CU. */
26037
26038 static void
26039 dwarf2_add_dependence (struct dwarf2_cu *cu,
26040 struct dwarf2_per_cu_data *ref_per_cu)
26041 {
26042 void **slot;
26043
26044 if (cu->dependencies == NULL)
26045 cu->dependencies
26046 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26047 NULL, &cu->comp_unit_obstack,
26048 hashtab_obstack_allocate,
26049 dummy_obstack_deallocate);
26050
26051 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26052 if (*slot == NULL)
26053 *slot = ref_per_cu;
26054 }
26055
26056 /* Subroutine of dwarf2_mark to pass to htab_traverse.
26057 Set the mark field in every compilation unit in the
26058 cache that we must keep because we are keeping CU. */
26059
26060 static int
26061 dwarf2_mark_helper (void **slot, void *data)
26062 {
26063 struct dwarf2_per_cu_data *per_cu;
26064
26065 per_cu = (struct dwarf2_per_cu_data *) *slot;
26066
26067 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26068 reading of the chain. As such dependencies remain valid it is not much
26069 useful to track and undo them during QUIT cleanups. */
26070 if (per_cu->cu == NULL)
26071 return 1;
26072
26073 if (per_cu->cu->mark)
26074 return 1;
26075 per_cu->cu->mark = true;
26076
26077 if (per_cu->cu->dependencies != NULL)
26078 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26079
26080 return 1;
26081 }
26082
26083 /* Set the mark field in CU and in every other compilation unit in the
26084 cache that we must keep because we are keeping CU. */
26085
26086 static void
26087 dwarf2_mark (struct dwarf2_cu *cu)
26088 {
26089 if (cu->mark)
26090 return;
26091 cu->mark = true;
26092 if (cu->dependencies != NULL)
26093 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
26094 }
26095
26096 static void
26097 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26098 {
26099 while (per_cu)
26100 {
26101 per_cu->cu->mark = false;
26102 per_cu = per_cu->cu->read_in_chain;
26103 }
26104 }
26105
26106 /* Trivial hash function for partial_die_info: the hash value of a DIE
26107 is its offset in .debug_info for this objfile. */
26108
26109 static hashval_t
26110 partial_die_hash (const void *item)
26111 {
26112 const struct partial_die_info *part_die
26113 = (const struct partial_die_info *) item;
26114
26115 return to_underlying (part_die->sect_off);
26116 }
26117
26118 /* Trivial comparison function for partial_die_info structures: two DIEs
26119 are equal if they have the same offset. */
26120
26121 static int
26122 partial_die_eq (const void *item_lhs, const void *item_rhs)
26123 {
26124 const struct partial_die_info *part_die_lhs
26125 = (const struct partial_die_info *) item_lhs;
26126 const struct partial_die_info *part_die_rhs
26127 = (const struct partial_die_info *) item_rhs;
26128
26129 return part_die_lhs->sect_off == part_die_rhs->sect_off;
26130 }
26131
26132 struct cmd_list_element *set_dwarf_cmdlist;
26133 struct cmd_list_element *show_dwarf_cmdlist;
26134
26135 static void
26136 set_dwarf_cmd (const char *args, int from_tty)
26137 {
26138 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
26139 gdb_stdout);
26140 }
26141
26142 static void
26143 show_dwarf_cmd (const char *args, int from_tty)
26144 {
26145 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
26146 }
26147
26148 bool dwarf_always_disassemble;
26149
26150 static void
26151 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26152 struct cmd_list_element *c, const char *value)
26153 {
26154 fprintf_filtered (file,
26155 _("Whether to always disassemble "
26156 "DWARF expressions is %s.\n"),
26157 value);
26158 }
26159
26160 static void
26161 show_check_physname (struct ui_file *file, int from_tty,
26162 struct cmd_list_element *c, const char *value)
26163 {
26164 fprintf_filtered (file,
26165 _("Whether to check \"physname\" is %s.\n"),
26166 value);
26167 }
26168
26169 void _initialize_dwarf2_read ();
26170 void
26171 _initialize_dwarf2_read ()
26172 {
26173 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26174 Set DWARF specific variables.\n\
26175 Configure DWARF variables such as the cache size."),
26176 &set_dwarf_cmdlist, "maintenance set dwarf ",
26177 0/*allow-unknown*/, &maintenance_set_cmdlist);
26178
26179 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26180 Show DWARF specific variables.\n\
26181 Show DWARF variables such as the cache size."),
26182 &show_dwarf_cmdlist, "maintenance show dwarf ",
26183 0/*allow-unknown*/, &maintenance_show_cmdlist);
26184
26185 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26186 &dwarf_max_cache_age, _("\
26187 Set the upper bound on the age of cached DWARF compilation units."), _("\
26188 Show the upper bound on the age of cached DWARF compilation units."), _("\
26189 A higher limit means that cached compilation units will be stored\n\
26190 in memory longer, and more total memory will be used. Zero disables\n\
26191 caching, which can slow down startup."),
26192 NULL,
26193 show_dwarf_max_cache_age,
26194 &set_dwarf_cmdlist,
26195 &show_dwarf_cmdlist);
26196
26197 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26198 &dwarf_always_disassemble, _("\
26199 Set whether `info address' always disassembles DWARF expressions."), _("\
26200 Show whether `info address' always disassembles DWARF expressions."), _("\
26201 When enabled, DWARF expressions are always printed in an assembly-like\n\
26202 syntax. When disabled, expressions will be printed in a more\n\
26203 conversational style, when possible."),
26204 NULL,
26205 show_dwarf_always_disassemble,
26206 &set_dwarf_cmdlist,
26207 &show_dwarf_cmdlist);
26208
26209 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26210 Set debugging of the DWARF reader."), _("\
26211 Show debugging of the DWARF reader."), _("\
26212 When enabled (non-zero), debugging messages are printed during DWARF\n\
26213 reading and symtab expansion. A value of 1 (one) provides basic\n\
26214 information. A value greater than 1 provides more verbose information."),
26215 NULL,
26216 NULL,
26217 &setdebuglist, &showdebuglist);
26218
26219 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26220 Set debugging of the DWARF DIE reader."), _("\
26221 Show debugging of the DWARF DIE reader."), _("\
26222 When enabled (non-zero), DIEs are dumped after they are read in.\n\
26223 The value is the maximum depth to print."),
26224 NULL,
26225 NULL,
26226 &setdebuglist, &showdebuglist);
26227
26228 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26229 Set debugging of the dwarf line reader."), _("\
26230 Show debugging of the dwarf line reader."), _("\
26231 When enabled (non-zero), line number entries are dumped as they are read in.\n\
26232 A value of 1 (one) provides basic information.\n\
26233 A value greater than 1 provides more verbose information."),
26234 NULL,
26235 NULL,
26236 &setdebuglist, &showdebuglist);
26237
26238 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26239 Set cross-checking of \"physname\" code against demangler."), _("\
26240 Show cross-checking of \"physname\" code against demangler."), _("\
26241 When enabled, GDB's internal \"physname\" code is checked against\n\
26242 the demangler."),
26243 NULL, show_check_physname,
26244 &setdebuglist, &showdebuglist);
26245
26246 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26247 no_class, &use_deprecated_index_sections, _("\
26248 Set whether to use deprecated gdb_index sections."), _("\
26249 Show whether to use deprecated gdb_index sections."), _("\
26250 When enabled, deprecated .gdb_index sections are used anyway.\n\
26251 Normally they are ignored either because of a missing feature or\n\
26252 performance issue.\n\
26253 Warning: This option must be enabled before gdb reads the file."),
26254 NULL,
26255 NULL,
26256 &setlist, &showlist);
26257
26258 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26259 &dwarf2_locexpr_funcs);
26260 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26261 &dwarf2_loclist_funcs);
26262
26263 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26264 &dwarf2_block_frame_base_locexpr_funcs);
26265 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26266 &dwarf2_block_frame_base_loclist_funcs);
26267
26268 #if GDB_SELF_TEST
26269 selftests::register_test ("dw2_expand_symtabs_matching",
26270 selftests::dw2_expand_symtabs_matching::run_test);
26271 #endif
26272 }
This page took 0.709824 seconds and 3 git commands to generate.