Replace init_cutu_and_read_dies with a class
[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 struct partial_symtab *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 dwarf2_read_symtab (struct partial_symtab *,
1517 struct objfile *);
1518
1519 static void psymtab_to_symtab_1 (struct partial_symtab *);
1520
1521 static abbrev_table_up abbrev_table_read_table
1522 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1523 sect_offset);
1524
1525 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1526
1527 static struct partial_die_info *load_partial_dies
1528 (const struct die_reader_specs *, const gdb_byte *, int);
1529
1530 /* A pair of partial_die_info and compilation unit. */
1531 struct cu_partial_die_info
1532 {
1533 /* The compilation unit of the partial_die_info. */
1534 struct dwarf2_cu *cu;
1535 /* A partial_die_info. */
1536 struct partial_die_info *pdi;
1537
1538 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1539 : cu (cu),
1540 pdi (pdi)
1541 { /* Nothing. */ }
1542
1543 private:
1544 cu_partial_die_info () = delete;
1545 };
1546
1547 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1548 struct dwarf2_cu *);
1549
1550 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1551 struct attribute *, struct attr_abbrev *,
1552 const gdb_byte *, bool *need_reprocess);
1553
1554 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1555 struct attribute *attr);
1556
1557 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1558
1559 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1560
1561 static int read_1_signed_byte (bfd *, const gdb_byte *);
1562
1563 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1564
1565 /* Read the next three bytes (little-endian order) as an unsigned integer. */
1566 static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1567
1568 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1569
1570 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1571
1572 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1573 unsigned int *);
1574
1575 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1576
1577 static LONGEST read_checked_initial_length_and_offset
1578 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1579 unsigned int *, unsigned int *);
1580
1581 static LONGEST read_offset (bfd *, const gdb_byte *,
1582 const struct comp_unit_head *,
1583 unsigned int *);
1584
1585 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1586
1587 static sect_offset read_abbrev_offset
1588 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1589 struct dwarf2_section_info *, sect_offset);
1590
1591 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1592
1593 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1594
1595 static const char *read_indirect_string
1596 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1597 const struct comp_unit_head *, unsigned int *);
1598
1599 static const char *read_indirect_line_string
1600 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1601 const struct comp_unit_head *, unsigned int *);
1602
1603 static const char *read_indirect_string_at_offset
1604 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1605 LONGEST str_offset);
1606
1607 static const char *read_indirect_string_from_dwz
1608 (struct objfile *objfile, struct dwz_file *, LONGEST);
1609
1610 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1611
1612 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1613 const gdb_byte *,
1614 unsigned int *);
1615
1616 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1617 ULONGEST str_index);
1618
1619 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1620 ULONGEST str_index);
1621
1622 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1623
1624 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1625 struct dwarf2_cu *);
1626
1627 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1628 unsigned int);
1629
1630 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1631 struct dwarf2_cu *cu);
1632
1633 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1634
1635 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1636 struct dwarf2_cu *cu);
1637
1638 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1639
1640 static struct die_info *die_specification (struct die_info *die,
1641 struct dwarf2_cu **);
1642
1643 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1644 struct dwarf2_cu *cu);
1645
1646 static void dwarf_decode_lines (struct line_header *, const char *,
1647 struct dwarf2_cu *, struct partial_symtab *,
1648 CORE_ADDR, int decode_mapping);
1649
1650 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1651 const char *);
1652
1653 static struct symbol *new_symbol (struct die_info *, struct type *,
1654 struct dwarf2_cu *, struct symbol * = NULL);
1655
1656 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1657 struct dwarf2_cu *);
1658
1659 static void dwarf2_const_value_attr (const struct attribute *attr,
1660 struct type *type,
1661 const char *name,
1662 struct obstack *obstack,
1663 struct dwarf2_cu *cu, LONGEST *value,
1664 const gdb_byte **bytes,
1665 struct dwarf2_locexpr_baton **baton);
1666
1667 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1668
1669 static int need_gnat_info (struct dwarf2_cu *);
1670
1671 static struct type *die_descriptive_type (struct die_info *,
1672 struct dwarf2_cu *);
1673
1674 static void set_descriptive_type (struct type *, struct die_info *,
1675 struct dwarf2_cu *);
1676
1677 static struct type *die_containing_type (struct die_info *,
1678 struct dwarf2_cu *);
1679
1680 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1681 struct dwarf2_cu *);
1682
1683 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1684
1685 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1686
1687 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1688
1689 static char *typename_concat (struct obstack *obs, const char *prefix,
1690 const char *suffix, int physname,
1691 struct dwarf2_cu *cu);
1692
1693 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1694
1695 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1696
1697 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1698
1699 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1700
1701 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1702
1703 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1704
1705 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1706 struct dwarf2_cu *, struct partial_symtab *);
1707
1708 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1709 values. Keep the items ordered with increasing constraints compliance. */
1710 enum pc_bounds_kind
1711 {
1712 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1713 PC_BOUNDS_NOT_PRESENT,
1714
1715 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1716 were present but they do not form a valid range of PC addresses. */
1717 PC_BOUNDS_INVALID,
1718
1719 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1720 PC_BOUNDS_RANGES,
1721
1722 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1723 PC_BOUNDS_HIGH_LOW,
1724 };
1725
1726 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1727 CORE_ADDR *, CORE_ADDR *,
1728 struct dwarf2_cu *,
1729 struct partial_symtab *);
1730
1731 static void get_scope_pc_bounds (struct die_info *,
1732 CORE_ADDR *, CORE_ADDR *,
1733 struct dwarf2_cu *);
1734
1735 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1736 CORE_ADDR, struct dwarf2_cu *);
1737
1738 static void dwarf2_add_field (struct field_info *, struct die_info *,
1739 struct dwarf2_cu *);
1740
1741 static void dwarf2_attach_fields_to_type (struct field_info *,
1742 struct type *, struct dwarf2_cu *);
1743
1744 static void dwarf2_add_member_fn (struct field_info *,
1745 struct die_info *, struct type *,
1746 struct dwarf2_cu *);
1747
1748 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1749 struct type *,
1750 struct dwarf2_cu *);
1751
1752 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1753
1754 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1755
1756 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1757
1758 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1759
1760 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1761
1762 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1763
1764 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1765
1766 static struct type *read_module_type (struct die_info *die,
1767 struct dwarf2_cu *cu);
1768
1769 static const char *namespace_name (struct die_info *die,
1770 int *is_anonymous, struct dwarf2_cu *);
1771
1772 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1773
1774 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1775
1776 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1777 struct dwarf2_cu *);
1778
1779 static struct die_info *read_die_and_siblings_1
1780 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1781 struct die_info *);
1782
1783 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1784 const gdb_byte *info_ptr,
1785 const gdb_byte **new_info_ptr,
1786 struct die_info *parent);
1787
1788 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1789 struct die_info **, const gdb_byte *,
1790 int *, int);
1791
1792 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1793 struct die_info **, const gdb_byte *,
1794 int *);
1795
1796 static void process_die (struct die_info *, struct dwarf2_cu *);
1797
1798 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1799 struct obstack *);
1800
1801 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1802
1803 static const char *dwarf2_full_name (const char *name,
1804 struct die_info *die,
1805 struct dwarf2_cu *cu);
1806
1807 static const char *dwarf2_physname (const char *name, struct die_info *die,
1808 struct dwarf2_cu *cu);
1809
1810 static struct die_info *dwarf2_extension (struct die_info *die,
1811 struct dwarf2_cu **);
1812
1813 static const char *dwarf_tag_name (unsigned int);
1814
1815 static const char *dwarf_attr_name (unsigned int);
1816
1817 static const char *dwarf_unit_type_name (int unit_type);
1818
1819 static const char *dwarf_form_name (unsigned int);
1820
1821 static const char *dwarf_bool_name (unsigned int);
1822
1823 static const char *dwarf_type_encoding_name (unsigned int);
1824
1825 static struct die_info *sibling_die (struct die_info *);
1826
1827 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1828
1829 static void dump_die_for_error (struct die_info *);
1830
1831 static void dump_die_1 (struct ui_file *, int level, int max_level,
1832 struct die_info *);
1833
1834 /*static*/ void dump_die (struct die_info *, int max_level);
1835
1836 static void store_in_ref_table (struct die_info *,
1837 struct dwarf2_cu *);
1838
1839 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1840
1841 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1842
1843 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1844 const struct attribute *,
1845 struct dwarf2_cu **);
1846
1847 static struct die_info *follow_die_ref (struct die_info *,
1848 const struct attribute *,
1849 struct dwarf2_cu **);
1850
1851 static struct die_info *follow_die_sig (struct die_info *,
1852 const struct attribute *,
1853 struct dwarf2_cu **);
1854
1855 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1856 struct dwarf2_cu *);
1857
1858 static struct type *get_DW_AT_signature_type (struct die_info *,
1859 const struct attribute *,
1860 struct dwarf2_cu *);
1861
1862 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1863
1864 static void read_signatured_type (struct signatured_type *);
1865
1866 static int attr_to_dynamic_prop (const struct attribute *attr,
1867 struct die_info *die, struct dwarf2_cu *cu,
1868 struct dynamic_prop *prop, struct type *type);
1869
1870 /* memory allocation interface */
1871
1872 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1873
1874 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1875
1876 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1877
1878 static int attr_form_is_block (const struct attribute *);
1879
1880 static int attr_form_is_section_offset (const struct attribute *);
1881
1882 static int attr_form_is_constant (const struct attribute *);
1883
1884 static int attr_form_is_ref (const struct attribute *);
1885
1886 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1887 struct dwarf2_loclist_baton *baton,
1888 const struct attribute *attr);
1889
1890 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1891 struct symbol *sym,
1892 struct dwarf2_cu *cu,
1893 int is_block);
1894
1895 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1896 const gdb_byte *info_ptr,
1897 struct abbrev_info *abbrev);
1898
1899 static hashval_t partial_die_hash (const void *item);
1900
1901 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1902
1903 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1904 (sect_offset sect_off, unsigned int offset_in_dwz,
1905 struct dwarf2_per_objfile *dwarf2_per_objfile);
1906
1907 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1908 struct die_info *comp_unit_die,
1909 enum language pretend_language);
1910
1911 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1912
1913 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1914
1915 static struct type *set_die_type (struct die_info *, struct type *,
1916 struct dwarf2_cu *);
1917
1918 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1919
1920 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1921
1922 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1923 enum language);
1924
1925 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1926 enum language);
1927
1928 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1929 enum language);
1930
1931 static void dwarf2_add_dependence (struct dwarf2_cu *,
1932 struct dwarf2_per_cu_data *);
1933
1934 static void dwarf2_mark (struct dwarf2_cu *);
1935
1936 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1937
1938 static struct type *get_die_type_at_offset (sect_offset,
1939 struct dwarf2_per_cu_data *);
1940
1941 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1942
1943 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1944 enum language pretend_language);
1945
1946 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1947
1948 static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1949 static struct type *dwarf2_per_cu_addr_sized_int_type
1950 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1951 static struct type *dwarf2_per_cu_int_type
1952 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1953 bool unsigned_p);
1954
1955 /* Class, the destructor of which frees all allocated queue entries. This
1956 will only have work to do if an error was thrown while processing the
1957 dwarf. If no error was thrown then the queue entries should have all
1958 been processed, and freed, as we went along. */
1959
1960 class dwarf2_queue_guard
1961 {
1962 public:
1963 dwarf2_queue_guard () = default;
1964
1965 /* Free any entries remaining on the queue. There should only be
1966 entries left if we hit an error while processing the dwarf. */
1967 ~dwarf2_queue_guard ()
1968 {
1969 struct dwarf2_queue_item *item, *last;
1970
1971 item = dwarf2_queue;
1972 while (item)
1973 {
1974 /* Anything still marked queued is likely to be in an
1975 inconsistent state, so discard it. */
1976 if (item->per_cu->queued)
1977 {
1978 if (item->per_cu->cu != NULL)
1979 free_one_cached_comp_unit (item->per_cu);
1980 item->per_cu->queued = 0;
1981 }
1982
1983 last = item;
1984 item = item->next;
1985 xfree (last);
1986 }
1987
1988 dwarf2_queue = dwarf2_queue_tail = NULL;
1989 }
1990 };
1991
1992 /* The return type of find_file_and_directory. Note, the enclosed
1993 string pointers are only valid while this object is valid. */
1994
1995 struct file_and_directory
1996 {
1997 /* The filename. This is never NULL. */
1998 const char *name;
1999
2000 /* The compilation directory. NULL if not known. If we needed to
2001 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
2002 points directly to the DW_AT_comp_dir string attribute owned by
2003 the obstack that owns the DIE. */
2004 const char *comp_dir;
2005
2006 /* If we needed to build a new string for comp_dir, this is what
2007 owns the storage. */
2008 std::string comp_dir_storage;
2009 };
2010
2011 static file_and_directory find_file_and_directory (struct die_info *die,
2012 struct dwarf2_cu *cu);
2013
2014 static char *file_full_name (int file, struct line_header *lh,
2015 const char *comp_dir);
2016
2017 /* Expected enum dwarf_unit_type for read_comp_unit_head. */
2018 enum class rcuh_kind { COMPILE, TYPE };
2019
2020 static const gdb_byte *read_and_check_comp_unit_head
2021 (struct dwarf2_per_objfile* dwarf2_per_objfile,
2022 struct comp_unit_head *header,
2023 struct dwarf2_section_info *section,
2024 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
2025 rcuh_kind section_kind);
2026
2027 static htab_t allocate_signatured_type_table (struct objfile *objfile);
2028
2029 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
2030
2031 static struct dwo_unit *lookup_dwo_unit_in_dwp
2032 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2033 struct dwp_file *dwp_file, const char *comp_dir,
2034 ULONGEST signature, int is_debug_types);
2035
2036 static struct dwp_file *get_dwp_file
2037 (struct dwarf2_per_objfile *dwarf2_per_objfile);
2038
2039 static struct dwo_unit *lookup_dwo_comp_unit
2040 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
2041
2042 static struct dwo_unit *lookup_dwo_type_unit
2043 (struct signatured_type *, const char *, const char *);
2044
2045 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2046
2047 /* A unique pointer to a dwo_file. */
2048
2049 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
2050
2051 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
2052
2053 static void check_producer (struct dwarf2_cu *cu);
2054
2055 static void free_line_header_voidp (void *arg);
2056 \f
2057 /* Various complaints about symbol reading that don't abort the process. */
2058
2059 static void
2060 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2061 {
2062 complaint (_("statement list doesn't fit in .debug_line section"));
2063 }
2064
2065 static void
2066 dwarf2_debug_line_missing_file_complaint (void)
2067 {
2068 complaint (_(".debug_line section has line data without a file"));
2069 }
2070
2071 static void
2072 dwarf2_debug_line_missing_end_sequence_complaint (void)
2073 {
2074 complaint (_(".debug_line section has line "
2075 "program sequence without an end"));
2076 }
2077
2078 static void
2079 dwarf2_complex_location_expr_complaint (void)
2080 {
2081 complaint (_("location expression too complex"));
2082 }
2083
2084 static void
2085 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2086 int arg3)
2087 {
2088 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
2089 arg1, arg2, arg3);
2090 }
2091
2092 static void
2093 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2094 {
2095 complaint (_("debug info runs off end of %s section"
2096 " [in module %s]"),
2097 get_section_name (section),
2098 get_section_file_name (section));
2099 }
2100
2101 static void
2102 dwarf2_macro_malformed_definition_complaint (const char *arg1)
2103 {
2104 complaint (_("macro debug info contains a "
2105 "malformed macro definition:\n`%s'"),
2106 arg1);
2107 }
2108
2109 static void
2110 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2111 {
2112 complaint (_("invalid attribute class or form for '%s' in '%s'"),
2113 arg1, arg2);
2114 }
2115
2116 /* Hash function for line_header_hash. */
2117
2118 static hashval_t
2119 line_header_hash (const struct line_header *ofs)
2120 {
2121 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
2122 }
2123
2124 /* Hash function for htab_create_alloc_ex for line_header_hash. */
2125
2126 static hashval_t
2127 line_header_hash_voidp (const void *item)
2128 {
2129 const struct line_header *ofs = (const struct line_header *) item;
2130
2131 return line_header_hash (ofs);
2132 }
2133
2134 /* Equality function for line_header_hash. */
2135
2136 static int
2137 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2138 {
2139 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2140 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
2141
2142 return (ofs_lhs->sect_off == ofs_rhs->sect_off
2143 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2144 }
2145
2146 \f
2147
2148 /* Read the given attribute value as an address, taking the attribute's
2149 form into account. */
2150
2151 static CORE_ADDR
2152 attr_value_as_address (struct attribute *attr)
2153 {
2154 CORE_ADDR addr;
2155
2156 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2157 && attr->form != DW_FORM_GNU_addr_index)
2158 {
2159 /* Aside from a few clearly defined exceptions, attributes that
2160 contain an address must always be in DW_FORM_addr form.
2161 Unfortunately, some compilers happen to be violating this
2162 requirement by encoding addresses using other forms, such
2163 as DW_FORM_data4 for example. For those broken compilers,
2164 we try to do our best, without any guarantee of success,
2165 to interpret the address correctly. It would also be nice
2166 to generate a complaint, but that would require us to maintain
2167 a list of legitimate cases where a non-address form is allowed,
2168 as well as update callers to pass in at least the CU's DWARF
2169 version. This is more overhead than what we're willing to
2170 expand for a pretty rare case. */
2171 addr = DW_UNSND (attr);
2172 }
2173 else
2174 addr = DW_ADDR (attr);
2175
2176 return addr;
2177 }
2178
2179 /* See declaration. */
2180
2181 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2182 const dwarf2_debug_sections *names,
2183 bool can_copy_)
2184 : objfile (objfile_),
2185 can_copy (can_copy_)
2186 {
2187 if (names == NULL)
2188 names = &dwarf2_elf_names;
2189
2190 bfd *obfd = objfile->obfd;
2191
2192 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2193 locate_sections (obfd, sec, *names);
2194 }
2195
2196 dwarf2_per_objfile::~dwarf2_per_objfile ()
2197 {
2198 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2199 free_cached_comp_units ();
2200
2201 if (quick_file_names_table)
2202 htab_delete (quick_file_names_table);
2203
2204 if (line_header_hash)
2205 htab_delete (line_header_hash);
2206
2207 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2208 per_cu->imported_symtabs_free ();
2209
2210 for (signatured_type *sig_type : all_type_units)
2211 sig_type->per_cu.imported_symtabs_free ();
2212
2213 /* Everything else should be on the objfile obstack. */
2214 }
2215
2216 /* See declaration. */
2217
2218 void
2219 dwarf2_per_objfile::free_cached_comp_units ()
2220 {
2221 dwarf2_per_cu_data *per_cu = read_in_chain;
2222 dwarf2_per_cu_data **last_chain = &read_in_chain;
2223 while (per_cu != NULL)
2224 {
2225 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2226
2227 delete per_cu->cu;
2228 *last_chain = next_cu;
2229 per_cu = next_cu;
2230 }
2231 }
2232
2233 /* A helper class that calls free_cached_comp_units on
2234 destruction. */
2235
2236 class free_cached_comp_units
2237 {
2238 public:
2239
2240 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2241 : m_per_objfile (per_objfile)
2242 {
2243 }
2244
2245 ~free_cached_comp_units ()
2246 {
2247 m_per_objfile->free_cached_comp_units ();
2248 }
2249
2250 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2251
2252 private:
2253
2254 dwarf2_per_objfile *m_per_objfile;
2255 };
2256
2257 /* Try to locate the sections we need for DWARF 2 debugging
2258 information and return true if we have enough to do something.
2259 NAMES points to the dwarf2 section names, or is NULL if the standard
2260 ELF names are used. CAN_COPY is true for formats where symbol
2261 interposition is possible and so symbol values must follow copy
2262 relocation rules. */
2263
2264 int
2265 dwarf2_has_info (struct objfile *objfile,
2266 const struct dwarf2_debug_sections *names,
2267 bool can_copy)
2268 {
2269 if (objfile->flags & OBJF_READNEVER)
2270 return 0;
2271
2272 struct dwarf2_per_objfile *dwarf2_per_objfile
2273 = get_dwarf2_per_objfile (objfile);
2274
2275 if (dwarf2_per_objfile == NULL)
2276 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2277 names,
2278 can_copy);
2279
2280 return (!dwarf2_per_objfile->info.is_virtual
2281 && dwarf2_per_objfile->info.s.section != NULL
2282 && !dwarf2_per_objfile->abbrev.is_virtual
2283 && dwarf2_per_objfile->abbrev.s.section != NULL);
2284 }
2285
2286 /* Return the containing section of virtual section SECTION. */
2287
2288 static struct dwarf2_section_info *
2289 get_containing_section (const struct dwarf2_section_info *section)
2290 {
2291 gdb_assert (section->is_virtual);
2292 return section->s.containing_section;
2293 }
2294
2295 /* Return the bfd owner of SECTION. */
2296
2297 static struct bfd *
2298 get_section_bfd_owner (const struct dwarf2_section_info *section)
2299 {
2300 if (section->is_virtual)
2301 {
2302 section = get_containing_section (section);
2303 gdb_assert (!section->is_virtual);
2304 }
2305 return section->s.section->owner;
2306 }
2307
2308 /* Return the bfd section of SECTION.
2309 Returns NULL if the section is not present. */
2310
2311 static asection *
2312 get_section_bfd_section (const struct dwarf2_section_info *section)
2313 {
2314 if (section->is_virtual)
2315 {
2316 section = get_containing_section (section);
2317 gdb_assert (!section->is_virtual);
2318 }
2319 return section->s.section;
2320 }
2321
2322 /* Return the name of SECTION. */
2323
2324 static const char *
2325 get_section_name (const struct dwarf2_section_info *section)
2326 {
2327 asection *sectp = get_section_bfd_section (section);
2328
2329 gdb_assert (sectp != NULL);
2330 return bfd_section_name (sectp);
2331 }
2332
2333 /* Return the name of the file SECTION is in. */
2334
2335 static const char *
2336 get_section_file_name (const struct dwarf2_section_info *section)
2337 {
2338 bfd *abfd = get_section_bfd_owner (section);
2339
2340 return bfd_get_filename (abfd);
2341 }
2342
2343 /* Return the id of SECTION.
2344 Returns 0 if SECTION doesn't exist. */
2345
2346 static int
2347 get_section_id (const struct dwarf2_section_info *section)
2348 {
2349 asection *sectp = get_section_bfd_section (section);
2350
2351 if (sectp == NULL)
2352 return 0;
2353 return sectp->id;
2354 }
2355
2356 /* Return the flags of SECTION.
2357 SECTION (or containing section if this is a virtual section) must exist. */
2358
2359 static int
2360 get_section_flags (const struct dwarf2_section_info *section)
2361 {
2362 asection *sectp = get_section_bfd_section (section);
2363
2364 gdb_assert (sectp != NULL);
2365 return bfd_section_flags (sectp);
2366 }
2367
2368 /* When loading sections, we look either for uncompressed section or for
2369 compressed section names. */
2370
2371 static int
2372 section_is_p (const char *section_name,
2373 const struct dwarf2_section_names *names)
2374 {
2375 if (names->normal != NULL
2376 && strcmp (section_name, names->normal) == 0)
2377 return 1;
2378 if (names->compressed != NULL
2379 && strcmp (section_name, names->compressed) == 0)
2380 return 1;
2381 return 0;
2382 }
2383
2384 /* See declaration. */
2385
2386 void
2387 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2388 const dwarf2_debug_sections &names)
2389 {
2390 flagword aflag = bfd_section_flags (sectp);
2391
2392 if ((aflag & SEC_HAS_CONTENTS) == 0)
2393 {
2394 }
2395 else if (elf_section_data (sectp)->this_hdr.sh_size
2396 > bfd_get_file_size (abfd))
2397 {
2398 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2399 warning (_("Discarding section %s which has a section size (%s"
2400 ") larger than the file size [in module %s]"),
2401 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2402 bfd_get_filename (abfd));
2403 }
2404 else if (section_is_p (sectp->name, &names.info))
2405 {
2406 this->info.s.section = sectp;
2407 this->info.size = bfd_section_size (sectp);
2408 }
2409 else if (section_is_p (sectp->name, &names.abbrev))
2410 {
2411 this->abbrev.s.section = sectp;
2412 this->abbrev.size = bfd_section_size (sectp);
2413 }
2414 else if (section_is_p (sectp->name, &names.line))
2415 {
2416 this->line.s.section = sectp;
2417 this->line.size = bfd_section_size (sectp);
2418 }
2419 else if (section_is_p (sectp->name, &names.loc))
2420 {
2421 this->loc.s.section = sectp;
2422 this->loc.size = bfd_section_size (sectp);
2423 }
2424 else if (section_is_p (sectp->name, &names.loclists))
2425 {
2426 this->loclists.s.section = sectp;
2427 this->loclists.size = bfd_section_size (sectp);
2428 }
2429 else if (section_is_p (sectp->name, &names.macinfo))
2430 {
2431 this->macinfo.s.section = sectp;
2432 this->macinfo.size = bfd_section_size (sectp);
2433 }
2434 else if (section_is_p (sectp->name, &names.macro))
2435 {
2436 this->macro.s.section = sectp;
2437 this->macro.size = bfd_section_size (sectp);
2438 }
2439 else if (section_is_p (sectp->name, &names.str))
2440 {
2441 this->str.s.section = sectp;
2442 this->str.size = bfd_section_size (sectp);
2443 }
2444 else if (section_is_p (sectp->name, &names.str_offsets))
2445 {
2446 this->str_offsets.s.section = sectp;
2447 this->str_offsets.size = bfd_section_size (sectp);
2448 }
2449 else if (section_is_p (sectp->name, &names.line_str))
2450 {
2451 this->line_str.s.section = sectp;
2452 this->line_str.size = bfd_section_size (sectp);
2453 }
2454 else if (section_is_p (sectp->name, &names.addr))
2455 {
2456 this->addr.s.section = sectp;
2457 this->addr.size = bfd_section_size (sectp);
2458 }
2459 else if (section_is_p (sectp->name, &names.frame))
2460 {
2461 this->frame.s.section = sectp;
2462 this->frame.size = bfd_section_size (sectp);
2463 }
2464 else if (section_is_p (sectp->name, &names.eh_frame))
2465 {
2466 this->eh_frame.s.section = sectp;
2467 this->eh_frame.size = bfd_section_size (sectp);
2468 }
2469 else if (section_is_p (sectp->name, &names.ranges))
2470 {
2471 this->ranges.s.section = sectp;
2472 this->ranges.size = bfd_section_size (sectp);
2473 }
2474 else if (section_is_p (sectp->name, &names.rnglists))
2475 {
2476 this->rnglists.s.section = sectp;
2477 this->rnglists.size = bfd_section_size (sectp);
2478 }
2479 else if (section_is_p (sectp->name, &names.types))
2480 {
2481 struct dwarf2_section_info type_section;
2482
2483 memset (&type_section, 0, sizeof (type_section));
2484 type_section.s.section = sectp;
2485 type_section.size = bfd_section_size (sectp);
2486
2487 this->types.push_back (type_section);
2488 }
2489 else if (section_is_p (sectp->name, &names.gdb_index))
2490 {
2491 this->gdb_index.s.section = sectp;
2492 this->gdb_index.size = bfd_section_size (sectp);
2493 }
2494 else if (section_is_p (sectp->name, &names.debug_names))
2495 {
2496 this->debug_names.s.section = sectp;
2497 this->debug_names.size = bfd_section_size (sectp);
2498 }
2499 else if (section_is_p (sectp->name, &names.debug_aranges))
2500 {
2501 this->debug_aranges.s.section = sectp;
2502 this->debug_aranges.size = bfd_section_size (sectp);
2503 }
2504
2505 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2506 && bfd_section_vma (sectp) == 0)
2507 this->has_section_at_zero = true;
2508 }
2509
2510 /* A helper function that decides whether a section is empty,
2511 or not present. */
2512
2513 static int
2514 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
2515 {
2516 if (section->is_virtual)
2517 return section->size == 0;
2518 return section->s.section == NULL || section->size == 0;
2519 }
2520
2521 /* See dwarf2read.h. */
2522
2523 void
2524 dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
2525 {
2526 asection *sectp;
2527 bfd *abfd;
2528 gdb_byte *buf, *retbuf;
2529
2530 if (info->readin)
2531 return;
2532 info->buffer = NULL;
2533 info->readin = true;
2534
2535 if (dwarf2_section_empty_p (info))
2536 return;
2537
2538 sectp = get_section_bfd_section (info);
2539
2540 /* If this is a virtual section we need to read in the real one first. */
2541 if (info->is_virtual)
2542 {
2543 struct dwarf2_section_info *containing_section =
2544 get_containing_section (info);
2545
2546 gdb_assert (sectp != NULL);
2547 if ((sectp->flags & SEC_RELOC) != 0)
2548 {
2549 error (_("Dwarf Error: DWP format V2 with relocations is not"
2550 " supported in section %s [in module %s]"),
2551 get_section_name (info), get_section_file_name (info));
2552 }
2553 dwarf2_read_section (objfile, containing_section);
2554 /* Other code should have already caught virtual sections that don't
2555 fit. */
2556 gdb_assert (info->virtual_offset + info->size
2557 <= containing_section->size);
2558 /* If the real section is empty or there was a problem reading the
2559 section we shouldn't get here. */
2560 gdb_assert (containing_section->buffer != NULL);
2561 info->buffer = containing_section->buffer + info->virtual_offset;
2562 return;
2563 }
2564
2565 /* If the section has relocations, we must read it ourselves.
2566 Otherwise we attach it to the BFD. */
2567 if ((sectp->flags & SEC_RELOC) == 0)
2568 {
2569 info->buffer = gdb_bfd_map_section (sectp, &info->size);
2570 return;
2571 }
2572
2573 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
2574 info->buffer = buf;
2575
2576 /* When debugging .o files, we may need to apply relocations; see
2577 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2578 We never compress sections in .o files, so we only need to
2579 try this when the section is not compressed. */
2580 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
2581 if (retbuf != NULL)
2582 {
2583 info->buffer = retbuf;
2584 return;
2585 }
2586
2587 abfd = get_section_bfd_owner (info);
2588 gdb_assert (abfd != NULL);
2589
2590 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2591 || bfd_bread (buf, info->size, abfd) != info->size)
2592 {
2593 error (_("Dwarf Error: Can't read DWARF data"
2594 " in section %s [in module %s]"),
2595 bfd_section_name (sectp), bfd_get_filename (abfd));
2596 }
2597 }
2598
2599 /* A helper function that returns the size of a section in a safe way.
2600 If you are positive that the section has been read before using the
2601 size, then it is safe to refer to the dwarf2_section_info object's
2602 "size" field directly. In other cases, you must call this
2603 function, because for compressed sections the size field is not set
2604 correctly until the section has been read. */
2605
2606 static bfd_size_type
2607 dwarf2_section_size (struct objfile *objfile,
2608 struct dwarf2_section_info *info)
2609 {
2610 if (!info->readin)
2611 dwarf2_read_section (objfile, info);
2612 return info->size;
2613 }
2614
2615 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2616 SECTION_NAME. */
2617
2618 void
2619 dwarf2_get_section_info (struct objfile *objfile,
2620 enum dwarf2_section_enum sect,
2621 asection **sectp, const gdb_byte **bufp,
2622 bfd_size_type *sizep)
2623 {
2624 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2625 struct dwarf2_section_info *info;
2626
2627 /* We may see an objfile without any DWARF, in which case we just
2628 return nothing. */
2629 if (data == NULL)
2630 {
2631 *sectp = NULL;
2632 *bufp = NULL;
2633 *sizep = 0;
2634 return;
2635 }
2636 switch (sect)
2637 {
2638 case DWARF2_DEBUG_FRAME:
2639 info = &data->frame;
2640 break;
2641 case DWARF2_EH_FRAME:
2642 info = &data->eh_frame;
2643 break;
2644 default:
2645 gdb_assert_not_reached ("unexpected section");
2646 }
2647
2648 dwarf2_read_section (objfile, info);
2649
2650 *sectp = get_section_bfd_section (info);
2651 *bufp = info->buffer;
2652 *sizep = info->size;
2653 }
2654
2655 /* A helper function to find the sections for a .dwz file. */
2656
2657 static void
2658 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2659 {
2660 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2661
2662 /* Note that we only support the standard ELF names, because .dwz
2663 is ELF-only (at the time of writing). */
2664 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2665 {
2666 dwz_file->abbrev.s.section = sectp;
2667 dwz_file->abbrev.size = bfd_section_size (sectp);
2668 }
2669 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2670 {
2671 dwz_file->info.s.section = sectp;
2672 dwz_file->info.size = bfd_section_size (sectp);
2673 }
2674 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2675 {
2676 dwz_file->str.s.section = sectp;
2677 dwz_file->str.size = bfd_section_size (sectp);
2678 }
2679 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2680 {
2681 dwz_file->line.s.section = sectp;
2682 dwz_file->line.size = bfd_section_size (sectp);
2683 }
2684 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2685 {
2686 dwz_file->macro.s.section = sectp;
2687 dwz_file->macro.size = bfd_section_size (sectp);
2688 }
2689 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2690 {
2691 dwz_file->gdb_index.s.section = sectp;
2692 dwz_file->gdb_index.size = bfd_section_size (sectp);
2693 }
2694 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2695 {
2696 dwz_file->debug_names.s.section = sectp;
2697 dwz_file->debug_names.size = bfd_section_size (sectp);
2698 }
2699 }
2700
2701 /* See dwarf2read.h. */
2702
2703 struct dwz_file *
2704 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2705 {
2706 const char *filename;
2707 bfd_size_type buildid_len_arg;
2708 size_t buildid_len;
2709 bfd_byte *buildid;
2710
2711 if (dwarf2_per_objfile->dwz_file != NULL)
2712 return dwarf2_per_objfile->dwz_file.get ();
2713
2714 bfd_set_error (bfd_error_no_error);
2715 gdb::unique_xmalloc_ptr<char> data
2716 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2717 &buildid_len_arg, &buildid));
2718 if (data == NULL)
2719 {
2720 if (bfd_get_error () == bfd_error_no_error)
2721 return NULL;
2722 error (_("could not read '.gnu_debugaltlink' section: %s"),
2723 bfd_errmsg (bfd_get_error ()));
2724 }
2725
2726 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2727
2728 buildid_len = (size_t) buildid_len_arg;
2729
2730 filename = data.get ();
2731
2732 std::string abs_storage;
2733 if (!IS_ABSOLUTE_PATH (filename))
2734 {
2735 gdb::unique_xmalloc_ptr<char> abs
2736 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2737
2738 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2739 filename = abs_storage.c_str ();
2740 }
2741
2742 /* First try the file name given in the section. If that doesn't
2743 work, try to use the build-id instead. */
2744 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2745 if (dwz_bfd != NULL)
2746 {
2747 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2748 dwz_bfd.reset (nullptr);
2749 }
2750
2751 if (dwz_bfd == NULL)
2752 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2753
2754 if (dwz_bfd == NULL)
2755 error (_("could not find '.gnu_debugaltlink' file for %s"),
2756 objfile_name (dwarf2_per_objfile->objfile));
2757
2758 std::unique_ptr<struct dwz_file> result
2759 (new struct dwz_file (std::move (dwz_bfd)));
2760
2761 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2762 result.get ());
2763
2764 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2765 result->dwz_bfd.get ());
2766 dwarf2_per_objfile->dwz_file = std::move (result);
2767 return dwarf2_per_objfile->dwz_file.get ();
2768 }
2769 \f
2770 /* DWARF quick_symbols_functions support. */
2771
2772 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2773 unique line tables, so we maintain a separate table of all .debug_line
2774 derived entries to support the sharing.
2775 All the quick functions need is the list of file names. We discard the
2776 line_header when we're done and don't need to record it here. */
2777 struct quick_file_names
2778 {
2779 /* The data used to construct the hash key. */
2780 struct stmt_list_hash hash;
2781
2782 /* The number of entries in file_names, real_names. */
2783 unsigned int num_file_names;
2784
2785 /* The file names from the line table, after being run through
2786 file_full_name. */
2787 const char **file_names;
2788
2789 /* The file names from the line table after being run through
2790 gdb_realpath. These are computed lazily. */
2791 const char **real_names;
2792 };
2793
2794 /* When using the index (and thus not using psymtabs), each CU has an
2795 object of this type. This is used to hold information needed by
2796 the various "quick" methods. */
2797 struct dwarf2_per_cu_quick_data
2798 {
2799 /* The file table. This can be NULL if there was no file table
2800 or it's currently not read in.
2801 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2802 struct quick_file_names *file_names;
2803
2804 /* The corresponding symbol table. This is NULL if symbols for this
2805 CU have not yet been read. */
2806 struct compunit_symtab *compunit_symtab;
2807
2808 /* A temporary mark bit used when iterating over all CUs in
2809 expand_symtabs_matching. */
2810 unsigned int mark : 1;
2811
2812 /* True if we've tried to read the file table and found there isn't one.
2813 There will be no point in trying to read it again next time. */
2814 unsigned int no_file_data : 1;
2815 };
2816
2817 /* Utility hash function for a stmt_list_hash. */
2818
2819 static hashval_t
2820 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2821 {
2822 hashval_t v = 0;
2823
2824 if (stmt_list_hash->dwo_unit != NULL)
2825 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2826 v += to_underlying (stmt_list_hash->line_sect_off);
2827 return v;
2828 }
2829
2830 /* Utility equality function for a stmt_list_hash. */
2831
2832 static int
2833 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2834 const struct stmt_list_hash *rhs)
2835 {
2836 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2837 return 0;
2838 if (lhs->dwo_unit != NULL
2839 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2840 return 0;
2841
2842 return lhs->line_sect_off == rhs->line_sect_off;
2843 }
2844
2845 /* Hash function for a quick_file_names. */
2846
2847 static hashval_t
2848 hash_file_name_entry (const void *e)
2849 {
2850 const struct quick_file_names *file_data
2851 = (const struct quick_file_names *) e;
2852
2853 return hash_stmt_list_entry (&file_data->hash);
2854 }
2855
2856 /* Equality function for a quick_file_names. */
2857
2858 static int
2859 eq_file_name_entry (const void *a, const void *b)
2860 {
2861 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2862 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2863
2864 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2865 }
2866
2867 /* Delete function for a quick_file_names. */
2868
2869 static void
2870 delete_file_name_entry (void *e)
2871 {
2872 struct quick_file_names *file_data = (struct quick_file_names *) e;
2873 int i;
2874
2875 for (i = 0; i < file_data->num_file_names; ++i)
2876 {
2877 xfree ((void*) file_data->file_names[i]);
2878 if (file_data->real_names)
2879 xfree ((void*) file_data->real_names[i]);
2880 }
2881
2882 /* The space for the struct itself lives on objfile_obstack,
2883 so we don't free it here. */
2884 }
2885
2886 /* Create a quick_file_names hash table. */
2887
2888 static htab_t
2889 create_quick_file_names_table (unsigned int nr_initial_entries)
2890 {
2891 return htab_create_alloc (nr_initial_entries,
2892 hash_file_name_entry, eq_file_name_entry,
2893 delete_file_name_entry, xcalloc, xfree);
2894 }
2895
2896 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2897 have to be created afterwards. You should call age_cached_comp_units after
2898 processing PER_CU->CU. dw2_setup must have been already called. */
2899
2900 static void
2901 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2902 {
2903 if (per_cu->is_debug_types)
2904 load_full_type_unit (per_cu);
2905 else
2906 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2907
2908 if (per_cu->cu == NULL)
2909 return; /* Dummy CU. */
2910
2911 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2912 }
2913
2914 /* Read in the symbols for PER_CU. */
2915
2916 static void
2917 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2918 {
2919 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2920
2921 /* Skip type_unit_groups, reading the type units they contain
2922 is handled elsewhere. */
2923 if (IS_TYPE_UNIT_GROUP (per_cu))
2924 return;
2925
2926 /* The destructor of dwarf2_queue_guard frees any entries left on
2927 the queue. After this point we're guaranteed to leave this function
2928 with the dwarf queue empty. */
2929 dwarf2_queue_guard q_guard;
2930
2931 if (dwarf2_per_objfile->using_index
2932 ? per_cu->v.quick->compunit_symtab == NULL
2933 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2934 {
2935 queue_comp_unit (per_cu, language_minimal);
2936 load_cu (per_cu, skip_partial);
2937
2938 /* If we just loaded a CU from a DWO, and we're working with an index
2939 that may badly handle TUs, load all the TUs in that DWO as well.
2940 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2941 if (!per_cu->is_debug_types
2942 && per_cu->cu != NULL
2943 && per_cu->cu->dwo_unit != NULL
2944 && dwarf2_per_objfile->index_table != NULL
2945 && dwarf2_per_objfile->index_table->version <= 7
2946 /* DWP files aren't supported yet. */
2947 && get_dwp_file (dwarf2_per_objfile) == NULL)
2948 queue_and_load_all_dwo_tus (per_cu);
2949 }
2950
2951 process_queue (dwarf2_per_objfile);
2952
2953 /* Age the cache, releasing compilation units that have not
2954 been used recently. */
2955 age_cached_comp_units (dwarf2_per_objfile);
2956 }
2957
2958 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2959 the objfile from which this CU came. Returns the resulting symbol
2960 table. */
2961
2962 static struct compunit_symtab *
2963 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2964 {
2965 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2966
2967 gdb_assert (dwarf2_per_objfile->using_index);
2968 if (!per_cu->v.quick->compunit_symtab)
2969 {
2970 free_cached_comp_units freer (dwarf2_per_objfile);
2971 scoped_restore decrementer = increment_reading_symtab ();
2972 dw2_do_instantiate_symtab (per_cu, skip_partial);
2973 process_cu_includes (dwarf2_per_objfile);
2974 }
2975
2976 return per_cu->v.quick->compunit_symtab;
2977 }
2978
2979 /* See declaration. */
2980
2981 dwarf2_per_cu_data *
2982 dwarf2_per_objfile::get_cutu (int index)
2983 {
2984 if (index >= this->all_comp_units.size ())
2985 {
2986 index -= this->all_comp_units.size ();
2987 gdb_assert (index < this->all_type_units.size ());
2988 return &this->all_type_units[index]->per_cu;
2989 }
2990
2991 return this->all_comp_units[index];
2992 }
2993
2994 /* See declaration. */
2995
2996 dwarf2_per_cu_data *
2997 dwarf2_per_objfile::get_cu (int index)
2998 {
2999 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
3000
3001 return this->all_comp_units[index];
3002 }
3003
3004 /* See declaration. */
3005
3006 signatured_type *
3007 dwarf2_per_objfile::get_tu (int index)
3008 {
3009 gdb_assert (index >= 0 && index < this->all_type_units.size ());
3010
3011 return this->all_type_units[index];
3012 }
3013
3014 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
3015 objfile_obstack, and constructed with the specified field
3016 values. */
3017
3018 static dwarf2_per_cu_data *
3019 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3020 struct dwarf2_section_info *section,
3021 int is_dwz,
3022 sect_offset sect_off, ULONGEST length)
3023 {
3024 struct objfile *objfile = dwarf2_per_objfile->objfile;
3025 dwarf2_per_cu_data *the_cu
3026 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3027 struct dwarf2_per_cu_data);
3028 the_cu->sect_off = sect_off;
3029 the_cu->length = length;
3030 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
3031 the_cu->section = section;
3032 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3033 struct dwarf2_per_cu_quick_data);
3034 the_cu->is_dwz = is_dwz;
3035 return the_cu;
3036 }
3037
3038 /* A helper for create_cus_from_index that handles a given list of
3039 CUs. */
3040
3041 static void
3042 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
3043 const gdb_byte *cu_list, offset_type n_elements,
3044 struct dwarf2_section_info *section,
3045 int is_dwz)
3046 {
3047 for (offset_type i = 0; i < n_elements; i += 2)
3048 {
3049 gdb_static_assert (sizeof (ULONGEST) >= 8);
3050
3051 sect_offset sect_off
3052 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3053 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
3054 cu_list += 2 * 8;
3055
3056 dwarf2_per_cu_data *per_cu
3057 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3058 sect_off, length);
3059 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
3060 }
3061 }
3062
3063 /* Read the CU list from the mapped index, and use it to create all
3064 the CU objects for this objfile. */
3065
3066 static void
3067 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3068 const gdb_byte *cu_list, offset_type cu_list_elements,
3069 const gdb_byte *dwz_list, offset_type dwz_elements)
3070 {
3071 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3072 dwarf2_per_objfile->all_comp_units.reserve
3073 ((cu_list_elements + dwz_elements) / 2);
3074
3075 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
3076 &dwarf2_per_objfile->info, 0);
3077
3078 if (dwz_elements == 0)
3079 return;
3080
3081 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3082 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
3083 &dwz->info, 1);
3084 }
3085
3086 /* Create the signatured type hash table from the index. */
3087
3088 static void
3089 create_signatured_type_table_from_index
3090 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3091 struct dwarf2_section_info *section,
3092 const gdb_byte *bytes,
3093 offset_type elements)
3094 {
3095 struct objfile *objfile = dwarf2_per_objfile->objfile;
3096
3097 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3098 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
3099
3100 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3101
3102 for (offset_type i = 0; i < elements; i += 3)
3103 {
3104 struct signatured_type *sig_type;
3105 ULONGEST signature;
3106 void **slot;
3107 cu_offset type_offset_in_tu;
3108
3109 gdb_static_assert (sizeof (ULONGEST) >= 8);
3110 sect_offset sect_off
3111 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3112 type_offset_in_tu
3113 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3114 BFD_ENDIAN_LITTLE);
3115 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3116 bytes += 3 * 8;
3117
3118 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3119 struct signatured_type);
3120 sig_type->signature = signature;
3121 sig_type->type_offset_in_tu = type_offset_in_tu;
3122 sig_type->per_cu.is_debug_types = 1;
3123 sig_type->per_cu.section = section;
3124 sig_type->per_cu.sect_off = sect_off;
3125 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3126 sig_type->per_cu.v.quick
3127 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3128 struct dwarf2_per_cu_quick_data);
3129
3130 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3131 *slot = sig_type;
3132
3133 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3134 }
3135
3136 dwarf2_per_objfile->signatured_types = sig_types_hash;
3137 }
3138
3139 /* Create the signatured type hash table from .debug_names. */
3140
3141 static void
3142 create_signatured_type_table_from_debug_names
3143 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3144 const mapped_debug_names &map,
3145 struct dwarf2_section_info *section,
3146 struct dwarf2_section_info *abbrev_section)
3147 {
3148 struct objfile *objfile = dwarf2_per_objfile->objfile;
3149
3150 dwarf2_read_section (objfile, section);
3151 dwarf2_read_section (objfile, abbrev_section);
3152
3153 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3154 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
3155
3156 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3157
3158 for (uint32_t i = 0; i < map.tu_count; ++i)
3159 {
3160 struct signatured_type *sig_type;
3161 void **slot;
3162
3163 sect_offset sect_off
3164 = (sect_offset) (extract_unsigned_integer
3165 (map.tu_table_reordered + i * map.offset_size,
3166 map.offset_size,
3167 map.dwarf5_byte_order));
3168
3169 comp_unit_head cu_header;
3170 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3171 abbrev_section,
3172 section->buffer + to_underlying (sect_off),
3173 rcuh_kind::TYPE);
3174
3175 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3176 struct signatured_type);
3177 sig_type->signature = cu_header.signature;
3178 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3179 sig_type->per_cu.is_debug_types = 1;
3180 sig_type->per_cu.section = section;
3181 sig_type->per_cu.sect_off = sect_off;
3182 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3183 sig_type->per_cu.v.quick
3184 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3185 struct dwarf2_per_cu_quick_data);
3186
3187 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3188 *slot = sig_type;
3189
3190 dwarf2_per_objfile->all_type_units.push_back (sig_type);
3191 }
3192
3193 dwarf2_per_objfile->signatured_types = sig_types_hash;
3194 }
3195
3196 /* Read the address map data from the mapped index, and use it to
3197 populate the objfile's psymtabs_addrmap. */
3198
3199 static void
3200 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3201 struct mapped_index *index)
3202 {
3203 struct objfile *objfile = dwarf2_per_objfile->objfile;
3204 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3205 const gdb_byte *iter, *end;
3206 struct addrmap *mutable_map;
3207 CORE_ADDR baseaddr;
3208
3209 auto_obstack temp_obstack;
3210
3211 mutable_map = addrmap_create_mutable (&temp_obstack);
3212
3213 iter = index->address_table.data ();
3214 end = iter + index->address_table.size ();
3215
3216 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
3217
3218 while (iter < end)
3219 {
3220 ULONGEST hi, lo, cu_index;
3221 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3222 iter += 8;
3223 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3224 iter += 8;
3225 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3226 iter += 4;
3227
3228 if (lo > hi)
3229 {
3230 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
3231 hex_string (lo), hex_string (hi));
3232 continue;
3233 }
3234
3235 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
3236 {
3237 complaint (_(".gdb_index address table has invalid CU number %u"),
3238 (unsigned) cu_index);
3239 continue;
3240 }
3241
3242 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3243 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
3244 addrmap_set_empty (mutable_map, lo, hi - 1,
3245 dwarf2_per_objfile->get_cu (cu_index));
3246 }
3247
3248 objfile->partial_symtabs->psymtabs_addrmap
3249 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3250 }
3251
3252 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
3253 populate the objfile's psymtabs_addrmap. */
3254
3255 static void
3256 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
3257 struct dwarf2_section_info *section)
3258 {
3259 struct objfile *objfile = dwarf2_per_objfile->objfile;
3260 bfd *abfd = objfile->obfd;
3261 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3262 const CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
3263
3264 auto_obstack temp_obstack;
3265 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3266
3267 std::unordered_map<sect_offset,
3268 dwarf2_per_cu_data *,
3269 gdb::hash_enum<sect_offset>>
3270 debug_info_offset_to_per_cu;
3271 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3272 {
3273 const auto insertpair
3274 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3275 if (!insertpair.second)
3276 {
3277 warning (_("Section .debug_aranges in %s has duplicate "
3278 "debug_info_offset %s, ignoring .debug_aranges."),
3279 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
3280 return;
3281 }
3282 }
3283
3284 dwarf2_read_section (objfile, section);
3285
3286 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3287
3288 const gdb_byte *addr = section->buffer;
3289
3290 while (addr < section->buffer + section->size)
3291 {
3292 const gdb_byte *const entry_addr = addr;
3293 unsigned int bytes_read;
3294
3295 const LONGEST entry_length = read_initial_length (abfd, addr,
3296 &bytes_read);
3297 addr += bytes_read;
3298
3299 const gdb_byte *const entry_end = addr + entry_length;
3300 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3301 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3302 if (addr + entry_length > section->buffer + section->size)
3303 {
3304 warning (_("Section .debug_aranges in %s entry at offset %s "
3305 "length %s exceeds section length %s, "
3306 "ignoring .debug_aranges."),
3307 objfile_name (objfile),
3308 plongest (entry_addr - section->buffer),
3309 plongest (bytes_read + entry_length),
3310 pulongest (section->size));
3311 return;
3312 }
3313
3314 /* The version number. */
3315 const uint16_t version = read_2_bytes (abfd, addr);
3316 addr += 2;
3317 if (version != 2)
3318 {
3319 warning (_("Section .debug_aranges in %s entry at offset %s "
3320 "has unsupported version %d, ignoring .debug_aranges."),
3321 objfile_name (objfile),
3322 plongest (entry_addr - section->buffer), version);
3323 return;
3324 }
3325
3326 const uint64_t debug_info_offset
3327 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3328 addr += offset_size;
3329 const auto per_cu_it
3330 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3331 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3332 {
3333 warning (_("Section .debug_aranges in %s entry at offset %s "
3334 "debug_info_offset %s does not exists, "
3335 "ignoring .debug_aranges."),
3336 objfile_name (objfile),
3337 plongest (entry_addr - section->buffer),
3338 pulongest (debug_info_offset));
3339 return;
3340 }
3341 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3342
3343 const uint8_t address_size = *addr++;
3344 if (address_size < 1 || address_size > 8)
3345 {
3346 warning (_("Section .debug_aranges in %s entry at offset %s "
3347 "address_size %u is invalid, ignoring .debug_aranges."),
3348 objfile_name (objfile),
3349 plongest (entry_addr - section->buffer), address_size);
3350 return;
3351 }
3352
3353 const uint8_t segment_selector_size = *addr++;
3354 if (segment_selector_size != 0)
3355 {
3356 warning (_("Section .debug_aranges in %s entry at offset %s "
3357 "segment_selector_size %u is not supported, "
3358 "ignoring .debug_aranges."),
3359 objfile_name (objfile),
3360 plongest (entry_addr - section->buffer),
3361 segment_selector_size);
3362 return;
3363 }
3364
3365 /* Must pad to an alignment boundary that is twice the address
3366 size. It is undocumented by the DWARF standard but GCC does
3367 use it. */
3368 for (size_t padding = ((-(addr - section->buffer))
3369 & (2 * address_size - 1));
3370 padding > 0; padding--)
3371 if (*addr++ != 0)
3372 {
3373 warning (_("Section .debug_aranges in %s entry at offset %s "
3374 "padding is not zero, ignoring .debug_aranges."),
3375 objfile_name (objfile),
3376 plongest (entry_addr - section->buffer));
3377 return;
3378 }
3379
3380 for (;;)
3381 {
3382 if (addr + 2 * address_size > entry_end)
3383 {
3384 warning (_("Section .debug_aranges in %s entry at offset %s "
3385 "address list is not properly terminated, "
3386 "ignoring .debug_aranges."),
3387 objfile_name (objfile),
3388 plongest (entry_addr - section->buffer));
3389 return;
3390 }
3391 ULONGEST start = extract_unsigned_integer (addr, address_size,
3392 dwarf5_byte_order);
3393 addr += address_size;
3394 ULONGEST length = extract_unsigned_integer (addr, address_size,
3395 dwarf5_byte_order);
3396 addr += address_size;
3397 if (start == 0 && length == 0)
3398 break;
3399 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3400 {
3401 /* Symbol was eliminated due to a COMDAT group. */
3402 continue;
3403 }
3404 ULONGEST end = start + length;
3405 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3406 - baseaddr);
3407 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3408 - baseaddr);
3409 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3410 }
3411 }
3412
3413 objfile->partial_symtabs->psymtabs_addrmap
3414 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3415 }
3416
3417 /* Find a slot in the mapped index INDEX for the object named NAME.
3418 If NAME is found, set *VEC_OUT to point to the CU vector in the
3419 constant pool and return true. If NAME cannot be found, return
3420 false. */
3421
3422 static bool
3423 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3424 offset_type **vec_out)
3425 {
3426 offset_type hash;
3427 offset_type slot, step;
3428 int (*cmp) (const char *, const char *);
3429
3430 gdb::unique_xmalloc_ptr<char> without_params;
3431 if (current_language->la_language == language_cplus
3432 || current_language->la_language == language_fortran
3433 || current_language->la_language == language_d)
3434 {
3435 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3436 not contain any. */
3437
3438 if (strchr (name, '(') != NULL)
3439 {
3440 without_params = cp_remove_params (name);
3441
3442 if (without_params != NULL)
3443 name = without_params.get ();
3444 }
3445 }
3446
3447 /* Index version 4 did not support case insensitive searches. But the
3448 indices for case insensitive languages are built in lowercase, therefore
3449 simulate our NAME being searched is also lowercased. */
3450 hash = mapped_index_string_hash ((index->version == 4
3451 && case_sensitivity == case_sensitive_off
3452 ? 5 : index->version),
3453 name);
3454
3455 slot = hash & (index->symbol_table.size () - 1);
3456 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3457 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3458
3459 for (;;)
3460 {
3461 const char *str;
3462
3463 const auto &bucket = index->symbol_table[slot];
3464 if (bucket.name == 0 && bucket.vec == 0)
3465 return false;
3466
3467 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3468 if (!cmp (name, str))
3469 {
3470 *vec_out = (offset_type *) (index->constant_pool
3471 + MAYBE_SWAP (bucket.vec));
3472 return true;
3473 }
3474
3475 slot = (slot + step) & (index->symbol_table.size () - 1);
3476 }
3477 }
3478
3479 /* A helper function that reads the .gdb_index from BUFFER and fills
3480 in MAP. FILENAME is the name of the file containing the data;
3481 it is used for error reporting. DEPRECATED_OK is true if it is
3482 ok to use deprecated sections.
3483
3484 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3485 out parameters that are filled in with information about the CU and
3486 TU lists in the section.
3487
3488 Returns true if all went well, false otherwise. */
3489
3490 static bool
3491 read_gdb_index_from_buffer (struct objfile *objfile,
3492 const char *filename,
3493 bool deprecated_ok,
3494 gdb::array_view<const gdb_byte> buffer,
3495 struct mapped_index *map,
3496 const gdb_byte **cu_list,
3497 offset_type *cu_list_elements,
3498 const gdb_byte **types_list,
3499 offset_type *types_list_elements)
3500 {
3501 const gdb_byte *addr = &buffer[0];
3502
3503 /* Version check. */
3504 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3505 /* Versions earlier than 3 emitted every copy of a psymbol. This
3506 causes the index to behave very poorly for certain requests. Version 3
3507 contained incomplete addrmap. So, it seems better to just ignore such
3508 indices. */
3509 if (version < 4)
3510 {
3511 static int warning_printed = 0;
3512 if (!warning_printed)
3513 {
3514 warning (_("Skipping obsolete .gdb_index section in %s."),
3515 filename);
3516 warning_printed = 1;
3517 }
3518 return 0;
3519 }
3520 /* Index version 4 uses a different hash function than index version
3521 5 and later.
3522
3523 Versions earlier than 6 did not emit psymbols for inlined
3524 functions. Using these files will cause GDB not to be able to
3525 set breakpoints on inlined functions by name, so we ignore these
3526 indices unless the user has done
3527 "set use-deprecated-index-sections on". */
3528 if (version < 6 && !deprecated_ok)
3529 {
3530 static int warning_printed = 0;
3531 if (!warning_printed)
3532 {
3533 warning (_("\
3534 Skipping deprecated .gdb_index section in %s.\n\
3535 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3536 to use the section anyway."),
3537 filename);
3538 warning_printed = 1;
3539 }
3540 return 0;
3541 }
3542 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3543 of the TU (for symbols coming from TUs),
3544 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3545 Plus gold-generated indices can have duplicate entries for global symbols,
3546 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3547 These are just performance bugs, and we can't distinguish gdb-generated
3548 indices from gold-generated ones, so issue no warning here. */
3549
3550 /* Indexes with higher version than the one supported by GDB may be no
3551 longer backward compatible. */
3552 if (version > 8)
3553 return 0;
3554
3555 map->version = version;
3556
3557 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3558
3559 int i = 0;
3560 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3561 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3562 / 8);
3563 ++i;
3564
3565 *types_list = addr + MAYBE_SWAP (metadata[i]);
3566 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3567 - MAYBE_SWAP (metadata[i]))
3568 / 8);
3569 ++i;
3570
3571 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3572 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3573 map->address_table
3574 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3575 ++i;
3576
3577 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3578 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3579 map->symbol_table
3580 = gdb::array_view<mapped_index::symbol_table_slot>
3581 ((mapped_index::symbol_table_slot *) symbol_table,
3582 (mapped_index::symbol_table_slot *) symbol_table_end);
3583
3584 ++i;
3585 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3586
3587 return 1;
3588 }
3589
3590 /* Callback types for dwarf2_read_gdb_index. */
3591
3592 typedef gdb::function_view
3593 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3594 get_gdb_index_contents_ftype;
3595 typedef gdb::function_view
3596 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3597 get_gdb_index_contents_dwz_ftype;
3598
3599 /* Read .gdb_index. If everything went ok, initialize the "quick"
3600 elements of all the CUs and return 1. Otherwise, return 0. */
3601
3602 static int
3603 dwarf2_read_gdb_index
3604 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3605 get_gdb_index_contents_ftype get_gdb_index_contents,
3606 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3607 {
3608 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3609 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3610 struct dwz_file *dwz;
3611 struct objfile *objfile = dwarf2_per_objfile->objfile;
3612
3613 gdb::array_view<const gdb_byte> main_index_contents
3614 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3615
3616 if (main_index_contents.empty ())
3617 return 0;
3618
3619 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3620 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3621 use_deprecated_index_sections,
3622 main_index_contents, map.get (), &cu_list,
3623 &cu_list_elements, &types_list,
3624 &types_list_elements))
3625 return 0;
3626
3627 /* Don't use the index if it's empty. */
3628 if (map->symbol_table.empty ())
3629 return 0;
3630
3631 /* If there is a .dwz file, read it so we can get its CU list as
3632 well. */
3633 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3634 if (dwz != NULL)
3635 {
3636 struct mapped_index dwz_map;
3637 const gdb_byte *dwz_types_ignore;
3638 offset_type dwz_types_elements_ignore;
3639
3640 gdb::array_view<const gdb_byte> dwz_index_content
3641 = get_gdb_index_contents_dwz (objfile, dwz);
3642
3643 if (dwz_index_content.empty ())
3644 return 0;
3645
3646 if (!read_gdb_index_from_buffer (objfile,
3647 bfd_get_filename (dwz->dwz_bfd.get ()),
3648 1, dwz_index_content, &dwz_map,
3649 &dwz_list, &dwz_list_elements,
3650 &dwz_types_ignore,
3651 &dwz_types_elements_ignore))
3652 {
3653 warning (_("could not read '.gdb_index' section from %s; skipping"),
3654 bfd_get_filename (dwz->dwz_bfd.get ()));
3655 return 0;
3656 }
3657 }
3658
3659 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3660 dwz_list, dwz_list_elements);
3661
3662 if (types_list_elements)
3663 {
3664 /* We can only handle a single .debug_types when we have an
3665 index. */
3666 if (dwarf2_per_objfile->types.size () != 1)
3667 return 0;
3668
3669 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3670
3671 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3672 types_list, types_list_elements);
3673 }
3674
3675 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3676
3677 dwarf2_per_objfile->index_table = std::move (map);
3678 dwarf2_per_objfile->using_index = 1;
3679 dwarf2_per_objfile->quick_file_names_table =
3680 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3681
3682 return 1;
3683 }
3684
3685 /* die_reader_func for dw2_get_file_names. */
3686
3687 static void
3688 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3689 const gdb_byte *info_ptr,
3690 struct die_info *comp_unit_die,
3691 int has_children)
3692 {
3693 struct dwarf2_cu *cu = reader->cu;
3694 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3695 struct dwarf2_per_objfile *dwarf2_per_objfile
3696 = cu->per_cu->dwarf2_per_objfile;
3697 struct objfile *objfile = dwarf2_per_objfile->objfile;
3698 struct dwarf2_per_cu_data *lh_cu;
3699 struct attribute *attr;
3700 void **slot;
3701 struct quick_file_names *qfn;
3702
3703 gdb_assert (! this_cu->is_debug_types);
3704
3705 /* Our callers never want to match partial units -- instead they
3706 will match the enclosing full CU. */
3707 if (comp_unit_die->tag == DW_TAG_partial_unit)
3708 {
3709 this_cu->v.quick->no_file_data = 1;
3710 return;
3711 }
3712
3713 lh_cu = this_cu;
3714 slot = NULL;
3715
3716 line_header_up lh;
3717 sect_offset line_offset {};
3718
3719 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3720 if (attr != nullptr)
3721 {
3722 struct quick_file_names find_entry;
3723
3724 line_offset = (sect_offset) DW_UNSND (attr);
3725
3726 /* We may have already read in this line header (TU line header sharing).
3727 If we have we're done. */
3728 find_entry.hash.dwo_unit = cu->dwo_unit;
3729 find_entry.hash.line_sect_off = line_offset;
3730 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3731 &find_entry, INSERT);
3732 if (*slot != NULL)
3733 {
3734 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3735 return;
3736 }
3737
3738 lh = dwarf_decode_line_header (line_offset, cu);
3739 }
3740 if (lh == NULL)
3741 {
3742 lh_cu->v.quick->no_file_data = 1;
3743 return;
3744 }
3745
3746 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3747 qfn->hash.dwo_unit = cu->dwo_unit;
3748 qfn->hash.line_sect_off = line_offset;
3749 gdb_assert (slot != NULL);
3750 *slot = qfn;
3751
3752 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3753
3754 int offset = 0;
3755 if (strcmp (fnd.name, "<unknown>") != 0)
3756 ++offset;
3757
3758 qfn->num_file_names = offset + lh->file_names_size ();
3759 qfn->file_names =
3760 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3761 if (offset != 0)
3762 qfn->file_names[0] = xstrdup (fnd.name);
3763 for (int i = 0; i < lh->file_names_size (); ++i)
3764 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
3765 qfn->real_names = NULL;
3766
3767 lh_cu->v.quick->file_names = qfn;
3768 }
3769
3770 /* A helper for the "quick" functions which attempts to read the line
3771 table for THIS_CU. */
3772
3773 static struct quick_file_names *
3774 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3775 {
3776 /* This should never be called for TUs. */
3777 gdb_assert (! this_cu->is_debug_types);
3778 /* Nor type unit groups. */
3779 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
3780
3781 if (this_cu->v.quick->file_names != NULL)
3782 return this_cu->v.quick->file_names;
3783 /* If we know there is no line data, no point in looking again. */
3784 if (this_cu->v.quick->no_file_data)
3785 return NULL;
3786
3787 cutu_reader reader (this_cu);
3788 if (!reader.dummy_p)
3789 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3790 reader.has_children);
3791
3792 if (this_cu->v.quick->no_file_data)
3793 return NULL;
3794 return this_cu->v.quick->file_names;
3795 }
3796
3797 /* A helper for the "quick" functions which computes and caches the
3798 real path for a given file name from the line table. */
3799
3800 static const char *
3801 dw2_get_real_path (struct objfile *objfile,
3802 struct quick_file_names *qfn, int index)
3803 {
3804 if (qfn->real_names == NULL)
3805 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3806 qfn->num_file_names, const char *);
3807
3808 if (qfn->real_names[index] == NULL)
3809 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3810
3811 return qfn->real_names[index];
3812 }
3813
3814 static struct symtab *
3815 dw2_find_last_source_symtab (struct objfile *objfile)
3816 {
3817 struct dwarf2_per_objfile *dwarf2_per_objfile
3818 = get_dwarf2_per_objfile (objfile);
3819 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3820 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3821
3822 if (cust == NULL)
3823 return NULL;
3824
3825 return compunit_primary_filetab (cust);
3826 }
3827
3828 /* Traversal function for dw2_forget_cached_source_info. */
3829
3830 static int
3831 dw2_free_cached_file_names (void **slot, void *info)
3832 {
3833 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3834
3835 if (file_data->real_names)
3836 {
3837 int i;
3838
3839 for (i = 0; i < file_data->num_file_names; ++i)
3840 {
3841 xfree ((void*) file_data->real_names[i]);
3842 file_data->real_names[i] = NULL;
3843 }
3844 }
3845
3846 return 1;
3847 }
3848
3849 static void
3850 dw2_forget_cached_source_info (struct objfile *objfile)
3851 {
3852 struct dwarf2_per_objfile *dwarf2_per_objfile
3853 = get_dwarf2_per_objfile (objfile);
3854
3855 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3856 dw2_free_cached_file_names, NULL);
3857 }
3858
3859 /* Helper function for dw2_map_symtabs_matching_filename that expands
3860 the symtabs and calls the iterator. */
3861
3862 static int
3863 dw2_map_expand_apply (struct objfile *objfile,
3864 struct dwarf2_per_cu_data *per_cu,
3865 const char *name, const char *real_path,
3866 gdb::function_view<bool (symtab *)> callback)
3867 {
3868 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3869
3870 /* Don't visit already-expanded CUs. */
3871 if (per_cu->v.quick->compunit_symtab)
3872 return 0;
3873
3874 /* This may expand more than one symtab, and we want to iterate over
3875 all of them. */
3876 dw2_instantiate_symtab (per_cu, false);
3877
3878 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3879 last_made, callback);
3880 }
3881
3882 /* Implementation of the map_symtabs_matching_filename method. */
3883
3884 static bool
3885 dw2_map_symtabs_matching_filename
3886 (struct objfile *objfile, const char *name, const char *real_path,
3887 gdb::function_view<bool (symtab *)> callback)
3888 {
3889 const char *name_basename = lbasename (name);
3890 struct dwarf2_per_objfile *dwarf2_per_objfile
3891 = get_dwarf2_per_objfile (objfile);
3892
3893 /* The rule is CUs specify all the files, including those used by
3894 any TU, so there's no need to scan TUs here. */
3895
3896 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3897 {
3898 /* We only need to look at symtabs not already expanded. */
3899 if (per_cu->v.quick->compunit_symtab)
3900 continue;
3901
3902 quick_file_names *file_data = dw2_get_file_names (per_cu);
3903 if (file_data == NULL)
3904 continue;
3905
3906 for (int j = 0; j < file_data->num_file_names; ++j)
3907 {
3908 const char *this_name = file_data->file_names[j];
3909 const char *this_real_name;
3910
3911 if (compare_filenames_for_search (this_name, name))
3912 {
3913 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3914 callback))
3915 return true;
3916 continue;
3917 }
3918
3919 /* Before we invoke realpath, which can get expensive when many
3920 files are involved, do a quick comparison of the basenames. */
3921 if (! basenames_may_differ
3922 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3923 continue;
3924
3925 this_real_name = dw2_get_real_path (objfile, file_data, j);
3926 if (compare_filenames_for_search (this_real_name, name))
3927 {
3928 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3929 callback))
3930 return true;
3931 continue;
3932 }
3933
3934 if (real_path != NULL)
3935 {
3936 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3937 gdb_assert (IS_ABSOLUTE_PATH (name));
3938 if (this_real_name != NULL
3939 && FILENAME_CMP (real_path, this_real_name) == 0)
3940 {
3941 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3942 callback))
3943 return true;
3944 continue;
3945 }
3946 }
3947 }
3948 }
3949
3950 return false;
3951 }
3952
3953 /* Struct used to manage iterating over all CUs looking for a symbol. */
3954
3955 struct dw2_symtab_iterator
3956 {
3957 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3958 struct dwarf2_per_objfile *dwarf2_per_objfile;
3959 /* If set, only look for symbols that match that block. Valid values are
3960 GLOBAL_BLOCK and STATIC_BLOCK. */
3961 gdb::optional<block_enum> block_index;
3962 /* The kind of symbol we're looking for. */
3963 domain_enum domain;
3964 /* The list of CUs from the index entry of the symbol,
3965 or NULL if not found. */
3966 offset_type *vec;
3967 /* The next element in VEC to look at. */
3968 int next;
3969 /* The number of elements in VEC, or zero if there is no match. */
3970 int length;
3971 /* Have we seen a global version of the symbol?
3972 If so we can ignore all further global instances.
3973 This is to work around gold/15646, inefficient gold-generated
3974 indices. */
3975 int global_seen;
3976 };
3977
3978 /* Initialize the index symtab iterator ITER. */
3979
3980 static void
3981 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3982 struct dwarf2_per_objfile *dwarf2_per_objfile,
3983 gdb::optional<block_enum> block_index,
3984 domain_enum domain,
3985 const char *name)
3986 {
3987 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3988 iter->block_index = block_index;
3989 iter->domain = domain;
3990 iter->next = 0;
3991 iter->global_seen = 0;
3992
3993 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3994
3995 /* index is NULL if OBJF_READNOW. */
3996 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3997 iter->length = MAYBE_SWAP (*iter->vec);
3998 else
3999 {
4000 iter->vec = NULL;
4001 iter->length = 0;
4002 }
4003 }
4004
4005 /* Return the next matching CU or NULL if there are no more. */
4006
4007 static struct dwarf2_per_cu_data *
4008 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
4009 {
4010 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
4011
4012 for ( ; iter->next < iter->length; ++iter->next)
4013 {
4014 offset_type cu_index_and_attrs =
4015 MAYBE_SWAP (iter->vec[iter->next + 1]);
4016 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4017 gdb_index_symbol_kind symbol_kind =
4018 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4019 /* Only check the symbol attributes if they're present.
4020 Indices prior to version 7 don't record them,
4021 and indices >= 7 may elide them for certain symbols
4022 (gold does this). */
4023 int attrs_valid =
4024 (dwarf2_per_objfile->index_table->version >= 7
4025 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4026
4027 /* Don't crash on bad data. */
4028 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4029 + dwarf2_per_objfile->all_type_units.size ()))
4030 {
4031 complaint (_(".gdb_index entry has bad CU index"
4032 " [in module %s]"),
4033 objfile_name (dwarf2_per_objfile->objfile));
4034 continue;
4035 }
4036
4037 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4038
4039 /* Skip if already read in. */
4040 if (per_cu->v.quick->compunit_symtab)
4041 continue;
4042
4043 /* Check static vs global. */
4044 if (attrs_valid)
4045 {
4046 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4047
4048 if (iter->block_index.has_value ())
4049 {
4050 bool want_static = *iter->block_index == STATIC_BLOCK;
4051
4052 if (is_static != want_static)
4053 continue;
4054 }
4055
4056 /* Work around gold/15646. */
4057 if (!is_static && iter->global_seen)
4058 continue;
4059 if (!is_static)
4060 iter->global_seen = 1;
4061 }
4062
4063 /* Only check the symbol's kind if it has one. */
4064 if (attrs_valid)
4065 {
4066 switch (iter->domain)
4067 {
4068 case VAR_DOMAIN:
4069 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4070 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4071 /* Some types are also in VAR_DOMAIN. */
4072 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4073 continue;
4074 break;
4075 case STRUCT_DOMAIN:
4076 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4077 continue;
4078 break;
4079 case LABEL_DOMAIN:
4080 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4081 continue;
4082 break;
4083 case MODULE_DOMAIN:
4084 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4085 continue;
4086 break;
4087 default:
4088 break;
4089 }
4090 }
4091
4092 ++iter->next;
4093 return per_cu;
4094 }
4095
4096 return NULL;
4097 }
4098
4099 static struct compunit_symtab *
4100 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
4101 const char *name, domain_enum domain)
4102 {
4103 struct compunit_symtab *stab_best = NULL;
4104 struct dwarf2_per_objfile *dwarf2_per_objfile
4105 = get_dwarf2_per_objfile (objfile);
4106
4107 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4108
4109 struct dw2_symtab_iterator iter;
4110 struct dwarf2_per_cu_data *per_cu;
4111
4112 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
4113
4114 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4115 {
4116 struct symbol *sym, *with_opaque = NULL;
4117 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
4118 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4119 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
4120
4121 sym = block_find_symbol (block, name, domain,
4122 block_find_non_opaque_type_preferred,
4123 &with_opaque);
4124
4125 /* Some caution must be observed with overloaded functions
4126 and methods, since the index will not contain any overload
4127 information (but NAME might contain it). */
4128
4129 if (sym != NULL
4130 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4131 return stab;
4132 if (with_opaque != NULL
4133 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4134 stab_best = stab;
4135
4136 /* Keep looking through other CUs. */
4137 }
4138
4139 return stab_best;
4140 }
4141
4142 static void
4143 dw2_print_stats (struct objfile *objfile)
4144 {
4145 struct dwarf2_per_objfile *dwarf2_per_objfile
4146 = get_dwarf2_per_objfile (objfile);
4147 int total = (dwarf2_per_objfile->all_comp_units.size ()
4148 + dwarf2_per_objfile->all_type_units.size ());
4149 int count = 0;
4150
4151 for (int i = 0; i < total; ++i)
4152 {
4153 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4154
4155 if (!per_cu->v.quick->compunit_symtab)
4156 ++count;
4157 }
4158 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
4159 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4160 }
4161
4162 /* This dumps minimal information about the index.
4163 It is called via "mt print objfiles".
4164 One use is to verify .gdb_index has been loaded by the
4165 gdb.dwarf2/gdb-index.exp testcase. */
4166
4167 static void
4168 dw2_dump (struct objfile *objfile)
4169 {
4170 struct dwarf2_per_objfile *dwarf2_per_objfile
4171 = get_dwarf2_per_objfile (objfile);
4172
4173 gdb_assert (dwarf2_per_objfile->using_index);
4174 printf_filtered (".gdb_index:");
4175 if (dwarf2_per_objfile->index_table != NULL)
4176 {
4177 printf_filtered (" version %d\n",
4178 dwarf2_per_objfile->index_table->version);
4179 }
4180 else
4181 printf_filtered (" faked for \"readnow\"\n");
4182 printf_filtered ("\n");
4183 }
4184
4185 static void
4186 dw2_expand_symtabs_for_function (struct objfile *objfile,
4187 const char *func_name)
4188 {
4189 struct dwarf2_per_objfile *dwarf2_per_objfile
4190 = get_dwarf2_per_objfile (objfile);
4191
4192 struct dw2_symtab_iterator iter;
4193 struct dwarf2_per_cu_data *per_cu;
4194
4195 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
4196
4197 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4198 dw2_instantiate_symtab (per_cu, false);
4199
4200 }
4201
4202 static void
4203 dw2_expand_all_symtabs (struct objfile *objfile)
4204 {
4205 struct dwarf2_per_objfile *dwarf2_per_objfile
4206 = get_dwarf2_per_objfile (objfile);
4207 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
4208 + dwarf2_per_objfile->all_type_units.size ());
4209
4210 for (int i = 0; i < total_units; ++i)
4211 {
4212 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
4213
4214 /* We don't want to directly expand a partial CU, because if we
4215 read it with the wrong language, then assertion failures can
4216 be triggered later on. See PR symtab/23010. So, tell
4217 dw2_instantiate_symtab to skip partial CUs -- any important
4218 partial CU will be read via DW_TAG_imported_unit anyway. */
4219 dw2_instantiate_symtab (per_cu, true);
4220 }
4221 }
4222
4223 static void
4224 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4225 const char *fullname)
4226 {
4227 struct dwarf2_per_objfile *dwarf2_per_objfile
4228 = get_dwarf2_per_objfile (objfile);
4229
4230 /* We don't need to consider type units here.
4231 This is only called for examining code, e.g. expand_line_sal.
4232 There can be an order of magnitude (or more) more type units
4233 than comp units, and we avoid them if we can. */
4234
4235 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4236 {
4237 /* We only need to look at symtabs not already expanded. */
4238 if (per_cu->v.quick->compunit_symtab)
4239 continue;
4240
4241 quick_file_names *file_data = dw2_get_file_names (per_cu);
4242 if (file_data == NULL)
4243 continue;
4244
4245 for (int j = 0; j < file_data->num_file_names; ++j)
4246 {
4247 const char *this_fullname = file_data->file_names[j];
4248
4249 if (filename_cmp (this_fullname, fullname) == 0)
4250 {
4251 dw2_instantiate_symtab (per_cu, false);
4252 break;
4253 }
4254 }
4255 }
4256 }
4257
4258 static void
4259 dw2_map_matching_symbols
4260 (struct objfile *objfile,
4261 const lookup_name_info &name, domain_enum domain,
4262 int global,
4263 gdb::function_view<symbol_found_callback_ftype> callback,
4264 symbol_compare_ftype *ordered_compare)
4265 {
4266 /* Currently unimplemented; used for Ada. The function can be called if the
4267 current language is Ada for a non-Ada objfile using GNU index. As Ada
4268 does not look for non-Ada symbols this function should just return. */
4269 }
4270
4271 /* Starting from a search name, return the string that finds the upper
4272 bound of all strings that start with SEARCH_NAME in a sorted name
4273 list. Returns the empty string to indicate that the upper bound is
4274 the end of the list. */
4275
4276 static std::string
4277 make_sort_after_prefix_name (const char *search_name)
4278 {
4279 /* When looking to complete "func", we find the upper bound of all
4280 symbols that start with "func" by looking for where we'd insert
4281 the closest string that would follow "func" in lexicographical
4282 order. Usually, that's "func"-with-last-character-incremented,
4283 i.e. "fund". Mind non-ASCII characters, though. Usually those
4284 will be UTF-8 multi-byte sequences, but we can't be certain.
4285 Especially mind the 0xff character, which is a valid character in
4286 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4287 rule out compilers allowing it in identifiers. Note that
4288 conveniently, strcmp/strcasecmp are specified to compare
4289 characters interpreted as unsigned char. So what we do is treat
4290 the whole string as a base 256 number composed of a sequence of
4291 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4292 to 0, and carries 1 to the following more-significant position.
4293 If the very first character in SEARCH_NAME ends up incremented
4294 and carries/overflows, then the upper bound is the end of the
4295 list. The string after the empty string is also the empty
4296 string.
4297
4298 Some examples of this operation:
4299
4300 SEARCH_NAME => "+1" RESULT
4301
4302 "abc" => "abd"
4303 "ab\xff" => "ac"
4304 "\xff" "a" "\xff" => "\xff" "b"
4305 "\xff" => ""
4306 "\xff\xff" => ""
4307 "" => ""
4308
4309 Then, with these symbols for example:
4310
4311 func
4312 func1
4313 fund
4314
4315 completing "func" looks for symbols between "func" and
4316 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4317 which finds "func" and "func1", but not "fund".
4318
4319 And with:
4320
4321 funcÿ (Latin1 'ÿ' [0xff])
4322 funcÿ1
4323 fund
4324
4325 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4326 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4327
4328 And with:
4329
4330 ÿÿ (Latin1 'ÿ' [0xff])
4331 ÿÿ1
4332
4333 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4334 the end of the list.
4335 */
4336 std::string after = search_name;
4337 while (!after.empty () && (unsigned char) after.back () == 0xff)
4338 after.pop_back ();
4339 if (!after.empty ())
4340 after.back () = (unsigned char) after.back () + 1;
4341 return after;
4342 }
4343
4344 /* See declaration. */
4345
4346 std::pair<std::vector<name_component>::const_iterator,
4347 std::vector<name_component>::const_iterator>
4348 mapped_index_base::find_name_components_bounds
4349 (const lookup_name_info &lookup_name_without_params, language lang) const
4350 {
4351 auto *name_cmp
4352 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4353
4354 const char *lang_name
4355 = lookup_name_without_params.language_lookup_name (lang).c_str ();
4356
4357 /* Comparison function object for lower_bound that matches against a
4358 given symbol name. */
4359 auto lookup_compare_lower = [&] (const name_component &elem,
4360 const char *name)
4361 {
4362 const char *elem_qualified = this->symbol_name_at (elem.idx);
4363 const char *elem_name = elem_qualified + elem.name_offset;
4364 return name_cmp (elem_name, name) < 0;
4365 };
4366
4367 /* Comparison function object for upper_bound that matches against a
4368 given symbol name. */
4369 auto lookup_compare_upper = [&] (const char *name,
4370 const name_component &elem)
4371 {
4372 const char *elem_qualified = this->symbol_name_at (elem.idx);
4373 const char *elem_name = elem_qualified + elem.name_offset;
4374 return name_cmp (name, elem_name) < 0;
4375 };
4376
4377 auto begin = this->name_components.begin ();
4378 auto end = this->name_components.end ();
4379
4380 /* Find the lower bound. */
4381 auto lower = [&] ()
4382 {
4383 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4384 return begin;
4385 else
4386 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4387 } ();
4388
4389 /* Find the upper bound. */
4390 auto upper = [&] ()
4391 {
4392 if (lookup_name_without_params.completion_mode ())
4393 {
4394 /* In completion mode, we want UPPER to point past all
4395 symbols names that have the same prefix. I.e., with
4396 these symbols, and completing "func":
4397
4398 function << lower bound
4399 function1
4400 other_function << upper bound
4401
4402 We find the upper bound by looking for the insertion
4403 point of "func"-with-last-character-incremented,
4404 i.e. "fund". */
4405 std::string after = make_sort_after_prefix_name (lang_name);
4406 if (after.empty ())
4407 return end;
4408 return std::lower_bound (lower, end, after.c_str (),
4409 lookup_compare_lower);
4410 }
4411 else
4412 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4413 } ();
4414
4415 return {lower, upper};
4416 }
4417
4418 /* See declaration. */
4419
4420 void
4421 mapped_index_base::build_name_components ()
4422 {
4423 if (!this->name_components.empty ())
4424 return;
4425
4426 this->name_components_casing = case_sensitivity;
4427 auto *name_cmp
4428 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4429
4430 /* The code below only knows how to break apart components of C++
4431 symbol names (and other languages that use '::' as
4432 namespace/module separator) and Ada symbol names. */
4433 auto count = this->symbol_name_count ();
4434 for (offset_type idx = 0; idx < count; idx++)
4435 {
4436 if (this->symbol_name_slot_invalid (idx))
4437 continue;
4438
4439 const char *name = this->symbol_name_at (idx);
4440
4441 /* Add each name component to the name component table. */
4442 unsigned int previous_len = 0;
4443
4444 if (strstr (name, "::") != nullptr)
4445 {
4446 for (unsigned int current_len = cp_find_first_component (name);
4447 name[current_len] != '\0';
4448 current_len += cp_find_first_component (name + current_len))
4449 {
4450 gdb_assert (name[current_len] == ':');
4451 this->name_components.push_back ({previous_len, idx});
4452 /* Skip the '::'. */
4453 current_len += 2;
4454 previous_len = current_len;
4455 }
4456 }
4457 else
4458 {
4459 /* Handle the Ada encoded (aka mangled) form here. */
4460 for (const char *iter = strstr (name, "__");
4461 iter != nullptr;
4462 iter = strstr (iter, "__"))
4463 {
4464 this->name_components.push_back ({previous_len, idx});
4465 iter += 2;
4466 previous_len = iter - name;
4467 }
4468 }
4469
4470 this->name_components.push_back ({previous_len, idx});
4471 }
4472
4473 /* Sort name_components elements by name. */
4474 auto name_comp_compare = [&] (const name_component &left,
4475 const name_component &right)
4476 {
4477 const char *left_qualified = this->symbol_name_at (left.idx);
4478 const char *right_qualified = this->symbol_name_at (right.idx);
4479
4480 const char *left_name = left_qualified + left.name_offset;
4481 const char *right_name = right_qualified + right.name_offset;
4482
4483 return name_cmp (left_name, right_name) < 0;
4484 };
4485
4486 std::sort (this->name_components.begin (),
4487 this->name_components.end (),
4488 name_comp_compare);
4489 }
4490
4491 /* Helper for dw2_expand_symtabs_matching that works with a
4492 mapped_index_base instead of the containing objfile. This is split
4493 to a separate function in order to be able to unit test the
4494 name_components matching using a mock mapped_index_base. For each
4495 symbol name that matches, calls MATCH_CALLBACK, passing it the
4496 symbol's index in the mapped_index_base symbol table. */
4497
4498 static void
4499 dw2_expand_symtabs_matching_symbol
4500 (mapped_index_base &index,
4501 const lookup_name_info &lookup_name_in,
4502 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4503 enum search_domain kind,
4504 gdb::function_view<bool (offset_type)> match_callback)
4505 {
4506 lookup_name_info lookup_name_without_params
4507 = lookup_name_in.make_ignore_params ();
4508
4509 /* Build the symbol name component sorted vector, if we haven't
4510 yet. */
4511 index.build_name_components ();
4512
4513 /* The same symbol may appear more than once in the range though.
4514 E.g., if we're looking for symbols that complete "w", and we have
4515 a symbol named "w1::w2", we'll find the two name components for
4516 that same symbol in the range. To be sure we only call the
4517 callback once per symbol, we first collect the symbol name
4518 indexes that matched in a temporary vector and ignore
4519 duplicates. */
4520 std::vector<offset_type> matches;
4521
4522 struct name_and_matcher
4523 {
4524 symbol_name_matcher_ftype *matcher;
4525 const std::string &name;
4526
4527 bool operator== (const name_and_matcher &other) const
4528 {
4529 return matcher == other.matcher && name == other.name;
4530 }
4531 };
4532
4533 /* A vector holding all the different symbol name matchers, for all
4534 languages. */
4535 std::vector<name_and_matcher> matchers;
4536
4537 for (int i = 0; i < nr_languages; i++)
4538 {
4539 enum language lang_e = (enum language) i;
4540
4541 const language_defn *lang = language_def (lang_e);
4542 symbol_name_matcher_ftype *name_matcher
4543 = get_symbol_name_matcher (lang, lookup_name_without_params);
4544
4545 name_and_matcher key {
4546 name_matcher,
4547 lookup_name_without_params.language_lookup_name (lang_e)
4548 };
4549
4550 /* Don't insert the same comparison routine more than once.
4551 Note that we do this linear walk. This is not a problem in
4552 practice because the number of supported languages is
4553 low. */
4554 if (std::find (matchers.begin (), matchers.end (), key)
4555 != matchers.end ())
4556 continue;
4557 matchers.push_back (std::move (key));
4558
4559 auto bounds
4560 = index.find_name_components_bounds (lookup_name_without_params,
4561 lang_e);
4562
4563 /* Now for each symbol name in range, check to see if we have a name
4564 match, and if so, call the MATCH_CALLBACK callback. */
4565
4566 for (; bounds.first != bounds.second; ++bounds.first)
4567 {
4568 const char *qualified = index.symbol_name_at (bounds.first->idx);
4569
4570 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4571 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4572 continue;
4573
4574 matches.push_back (bounds.first->idx);
4575 }
4576 }
4577
4578 std::sort (matches.begin (), matches.end ());
4579
4580 /* Finally call the callback, once per match. */
4581 ULONGEST prev = -1;
4582 for (offset_type idx : matches)
4583 {
4584 if (prev != idx)
4585 {
4586 if (!match_callback (idx))
4587 break;
4588 prev = idx;
4589 }
4590 }
4591
4592 /* Above we use a type wider than idx's for 'prev', since 0 and
4593 (offset_type)-1 are both possible values. */
4594 static_assert (sizeof (prev) > sizeof (offset_type), "");
4595 }
4596
4597 #if GDB_SELF_TEST
4598
4599 namespace selftests { namespace dw2_expand_symtabs_matching {
4600
4601 /* A mock .gdb_index/.debug_names-like name index table, enough to
4602 exercise dw2_expand_symtabs_matching_symbol, which works with the
4603 mapped_index_base interface. Builds an index from the symbol list
4604 passed as parameter to the constructor. */
4605 class mock_mapped_index : public mapped_index_base
4606 {
4607 public:
4608 mock_mapped_index (gdb::array_view<const char *> symbols)
4609 : m_symbol_table (symbols)
4610 {}
4611
4612 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4613
4614 /* Return the number of names in the symbol table. */
4615 size_t symbol_name_count () const override
4616 {
4617 return m_symbol_table.size ();
4618 }
4619
4620 /* Get the name of the symbol at IDX in the symbol table. */
4621 const char *symbol_name_at (offset_type idx) const override
4622 {
4623 return m_symbol_table[idx];
4624 }
4625
4626 private:
4627 gdb::array_view<const char *> m_symbol_table;
4628 };
4629
4630 /* Convenience function that converts a NULL pointer to a "<null>"
4631 string, to pass to print routines. */
4632
4633 static const char *
4634 string_or_null (const char *str)
4635 {
4636 return str != NULL ? str : "<null>";
4637 }
4638
4639 /* Check if a lookup_name_info built from
4640 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4641 index. EXPECTED_LIST is the list of expected matches, in expected
4642 matching order. If no match expected, then an empty list is
4643 specified. Returns true on success. On failure prints a warning
4644 indicating the file:line that failed, and returns false. */
4645
4646 static bool
4647 check_match (const char *file, int line,
4648 mock_mapped_index &mock_index,
4649 const char *name, symbol_name_match_type match_type,
4650 bool completion_mode,
4651 std::initializer_list<const char *> expected_list)
4652 {
4653 lookup_name_info lookup_name (name, match_type, completion_mode);
4654
4655 bool matched = true;
4656
4657 auto mismatch = [&] (const char *expected_str,
4658 const char *got)
4659 {
4660 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4661 "expected=\"%s\", got=\"%s\"\n"),
4662 file, line,
4663 (match_type == symbol_name_match_type::FULL
4664 ? "FULL" : "WILD"),
4665 name, string_or_null (expected_str), string_or_null (got));
4666 matched = false;
4667 };
4668
4669 auto expected_it = expected_list.begin ();
4670 auto expected_end = expected_list.end ();
4671
4672 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4673 NULL, ALL_DOMAIN,
4674 [&] (offset_type idx)
4675 {
4676 const char *matched_name = mock_index.symbol_name_at (idx);
4677 const char *expected_str
4678 = expected_it == expected_end ? NULL : *expected_it++;
4679
4680 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4681 mismatch (expected_str, matched_name);
4682 return true;
4683 });
4684
4685 const char *expected_str
4686 = expected_it == expected_end ? NULL : *expected_it++;
4687 if (expected_str != NULL)
4688 mismatch (expected_str, NULL);
4689
4690 return matched;
4691 }
4692
4693 /* The symbols added to the mock mapped_index for testing (in
4694 canonical form). */
4695 static const char *test_symbols[] = {
4696 "function",
4697 "std::bar",
4698 "std::zfunction",
4699 "std::zfunction2",
4700 "w1::w2",
4701 "ns::foo<char*>",
4702 "ns::foo<int>",
4703 "ns::foo<long>",
4704 "ns2::tmpl<int>::foo2",
4705 "(anonymous namespace)::A::B::C",
4706
4707 /* These are used to check that the increment-last-char in the
4708 matching algorithm for completion doesn't match "t1_fund" when
4709 completing "t1_func". */
4710 "t1_func",
4711 "t1_func1",
4712 "t1_fund",
4713 "t1_fund1",
4714
4715 /* A UTF-8 name with multi-byte sequences to make sure that
4716 cp-name-parser understands this as a single identifier ("função"
4717 is "function" in PT). */
4718 u8"u8função",
4719
4720 /* \377 (0xff) is Latin1 'ÿ'. */
4721 "yfunc\377",
4722
4723 /* \377 (0xff) is Latin1 'ÿ'. */
4724 "\377",
4725 "\377\377123",
4726
4727 /* A name with all sorts of complications. Starts with "z" to make
4728 it easier for the completion tests below. */
4729 #define Z_SYM_NAME \
4730 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4731 "::tuple<(anonymous namespace)::ui*, " \
4732 "std::default_delete<(anonymous namespace)::ui>, void>"
4733
4734 Z_SYM_NAME
4735 };
4736
4737 /* Returns true if the mapped_index_base::find_name_component_bounds
4738 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4739 in completion mode. */
4740
4741 static bool
4742 check_find_bounds_finds (mapped_index_base &index,
4743 const char *search_name,
4744 gdb::array_view<const char *> expected_syms)
4745 {
4746 lookup_name_info lookup_name (search_name,
4747 symbol_name_match_type::FULL, true);
4748
4749 auto bounds = index.find_name_components_bounds (lookup_name,
4750 language_cplus);
4751
4752 size_t distance = std::distance (bounds.first, bounds.second);
4753 if (distance != expected_syms.size ())
4754 return false;
4755
4756 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4757 {
4758 auto nc_elem = bounds.first + exp_elem;
4759 const char *qualified = index.symbol_name_at (nc_elem->idx);
4760 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4761 return false;
4762 }
4763
4764 return true;
4765 }
4766
4767 /* Test the lower-level mapped_index::find_name_component_bounds
4768 method. */
4769
4770 static void
4771 test_mapped_index_find_name_component_bounds ()
4772 {
4773 mock_mapped_index mock_index (test_symbols);
4774
4775 mock_index.build_name_components ();
4776
4777 /* Test the lower-level mapped_index::find_name_component_bounds
4778 method in completion mode. */
4779 {
4780 static const char *expected_syms[] = {
4781 "t1_func",
4782 "t1_func1",
4783 };
4784
4785 SELF_CHECK (check_find_bounds_finds (mock_index,
4786 "t1_func", expected_syms));
4787 }
4788
4789 /* Check that the increment-last-char in the name matching algorithm
4790 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4791 {
4792 static const char *expected_syms1[] = {
4793 "\377",
4794 "\377\377123",
4795 };
4796 SELF_CHECK (check_find_bounds_finds (mock_index,
4797 "\377", expected_syms1));
4798
4799 static const char *expected_syms2[] = {
4800 "\377\377123",
4801 };
4802 SELF_CHECK (check_find_bounds_finds (mock_index,
4803 "\377\377", expected_syms2));
4804 }
4805 }
4806
4807 /* Test dw2_expand_symtabs_matching_symbol. */
4808
4809 static void
4810 test_dw2_expand_symtabs_matching_symbol ()
4811 {
4812 mock_mapped_index mock_index (test_symbols);
4813
4814 /* We let all tests run until the end even if some fails, for debug
4815 convenience. */
4816 bool any_mismatch = false;
4817
4818 /* Create the expected symbols list (an initializer_list). Needed
4819 because lists have commas, and we need to pass them to CHECK,
4820 which is a macro. */
4821 #define EXPECT(...) { __VA_ARGS__ }
4822
4823 /* Wrapper for check_match that passes down the current
4824 __FILE__/__LINE__. */
4825 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4826 any_mismatch |= !check_match (__FILE__, __LINE__, \
4827 mock_index, \
4828 NAME, MATCH_TYPE, COMPLETION_MODE, \
4829 EXPECTED_LIST)
4830
4831 /* Identity checks. */
4832 for (const char *sym : test_symbols)
4833 {
4834 /* Should be able to match all existing symbols. */
4835 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4836 EXPECT (sym));
4837
4838 /* Should be able to match all existing symbols with
4839 parameters. */
4840 std::string with_params = std::string (sym) + "(int)";
4841 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4842 EXPECT (sym));
4843
4844 /* Should be able to match all existing symbols with
4845 parameters and qualifiers. */
4846 with_params = std::string (sym) + " ( int ) const";
4847 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4848 EXPECT (sym));
4849
4850 /* This should really find sym, but cp-name-parser.y doesn't
4851 know about lvalue/rvalue qualifiers yet. */
4852 with_params = std::string (sym) + " ( int ) &&";
4853 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4854 {});
4855 }
4856
4857 /* Check that the name matching algorithm for completion doesn't get
4858 confused with Latin1 'ÿ' / 0xff. */
4859 {
4860 static const char str[] = "\377";
4861 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4862 EXPECT ("\377", "\377\377123"));
4863 }
4864
4865 /* Check that the increment-last-char in the matching algorithm for
4866 completion doesn't match "t1_fund" when completing "t1_func". */
4867 {
4868 static const char str[] = "t1_func";
4869 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4870 EXPECT ("t1_func", "t1_func1"));
4871 }
4872
4873 /* Check that completion mode works at each prefix of the expected
4874 symbol name. */
4875 {
4876 static const char str[] = "function(int)";
4877 size_t len = strlen (str);
4878 std::string lookup;
4879
4880 for (size_t i = 1; i < len; i++)
4881 {
4882 lookup.assign (str, i);
4883 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4884 EXPECT ("function"));
4885 }
4886 }
4887
4888 /* While "w" is a prefix of both components, the match function
4889 should still only be called once. */
4890 {
4891 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4892 EXPECT ("w1::w2"));
4893 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4894 EXPECT ("w1::w2"));
4895 }
4896
4897 /* Same, with a "complicated" symbol. */
4898 {
4899 static const char str[] = Z_SYM_NAME;
4900 size_t len = strlen (str);
4901 std::string lookup;
4902
4903 for (size_t i = 1; i < len; i++)
4904 {
4905 lookup.assign (str, i);
4906 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4907 EXPECT (Z_SYM_NAME));
4908 }
4909 }
4910
4911 /* In FULL mode, an incomplete symbol doesn't match. */
4912 {
4913 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4914 {});
4915 }
4916
4917 /* A complete symbol with parameters matches any overload, since the
4918 index has no overload info. */
4919 {
4920 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4921 EXPECT ("std::zfunction", "std::zfunction2"));
4922 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4923 EXPECT ("std::zfunction", "std::zfunction2"));
4924 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4925 EXPECT ("std::zfunction", "std::zfunction2"));
4926 }
4927
4928 /* Check that whitespace is ignored appropriately. A symbol with a
4929 template argument list. */
4930 {
4931 static const char expected[] = "ns::foo<int>";
4932 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4933 EXPECT (expected));
4934 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4935 EXPECT (expected));
4936 }
4937
4938 /* Check that whitespace is ignored appropriately. A symbol with a
4939 template argument list that includes a pointer. */
4940 {
4941 static const char expected[] = "ns::foo<char*>";
4942 /* Try both completion and non-completion modes. */
4943 static const bool completion_mode[2] = {false, true};
4944 for (size_t i = 0; i < 2; i++)
4945 {
4946 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4947 completion_mode[i], EXPECT (expected));
4948 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4949 completion_mode[i], EXPECT (expected));
4950
4951 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4952 completion_mode[i], EXPECT (expected));
4953 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4954 completion_mode[i], EXPECT (expected));
4955 }
4956 }
4957
4958 {
4959 /* Check method qualifiers are ignored. */
4960 static const char expected[] = "ns::foo<char*>";
4961 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4962 symbol_name_match_type::FULL, true, EXPECT (expected));
4963 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4964 symbol_name_match_type::FULL, true, EXPECT (expected));
4965 CHECK_MATCH ("foo < char * > ( int ) const",
4966 symbol_name_match_type::WILD, true, EXPECT (expected));
4967 CHECK_MATCH ("foo < char * > ( int ) &&",
4968 symbol_name_match_type::WILD, true, EXPECT (expected));
4969 }
4970
4971 /* Test lookup names that don't match anything. */
4972 {
4973 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4974 {});
4975
4976 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4977 {});
4978 }
4979
4980 /* Some wild matching tests, exercising "(anonymous namespace)",
4981 which should not be confused with a parameter list. */
4982 {
4983 static const char *syms[] = {
4984 "A::B::C",
4985 "B::C",
4986 "C",
4987 "A :: B :: C ( int )",
4988 "B :: C ( int )",
4989 "C ( int )",
4990 };
4991
4992 for (const char *s : syms)
4993 {
4994 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4995 EXPECT ("(anonymous namespace)::A::B::C"));
4996 }
4997 }
4998
4999 {
5000 static const char expected[] = "ns2::tmpl<int>::foo2";
5001 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
5002 EXPECT (expected));
5003 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
5004 EXPECT (expected));
5005 }
5006
5007 SELF_CHECK (!any_mismatch);
5008
5009 #undef EXPECT
5010 #undef CHECK_MATCH
5011 }
5012
5013 static void
5014 run_test ()
5015 {
5016 test_mapped_index_find_name_component_bounds ();
5017 test_dw2_expand_symtabs_matching_symbol ();
5018 }
5019
5020 }} // namespace selftests::dw2_expand_symtabs_matching
5021
5022 #endif /* GDB_SELF_TEST */
5023
5024 /* If FILE_MATCHER is NULL or if PER_CU has
5025 dwarf2_per_cu_quick_data::MARK set (see
5026 dw_expand_symtabs_matching_file_matcher), expand the CU and call
5027 EXPANSION_NOTIFY on it. */
5028
5029 static void
5030 dw2_expand_symtabs_matching_one
5031 (struct dwarf2_per_cu_data *per_cu,
5032 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5033 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
5034 {
5035 if (file_matcher == NULL || per_cu->v.quick->mark)
5036 {
5037 bool symtab_was_null
5038 = (per_cu->v.quick->compunit_symtab == NULL);
5039
5040 dw2_instantiate_symtab (per_cu, false);
5041
5042 if (expansion_notify != NULL
5043 && symtab_was_null
5044 && per_cu->v.quick->compunit_symtab != NULL)
5045 expansion_notify (per_cu->v.quick->compunit_symtab);
5046 }
5047 }
5048
5049 /* Helper for dw2_expand_matching symtabs. Called on each symbol
5050 matched, to expand corresponding CUs that were marked. IDX is the
5051 index of the symbol name that matched. */
5052
5053 static void
5054 dw2_expand_marked_cus
5055 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
5056 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5057 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5058 search_domain kind)
5059 {
5060 offset_type *vec, vec_len, vec_idx;
5061 bool global_seen = false;
5062 mapped_index &index = *dwarf2_per_objfile->index_table;
5063
5064 vec = (offset_type *) (index.constant_pool
5065 + MAYBE_SWAP (index.symbol_table[idx].vec));
5066 vec_len = MAYBE_SWAP (vec[0]);
5067 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5068 {
5069 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5070 /* This value is only valid for index versions >= 7. */
5071 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5072 gdb_index_symbol_kind symbol_kind =
5073 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5074 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5075 /* Only check the symbol attributes if they're present.
5076 Indices prior to version 7 don't record them,
5077 and indices >= 7 may elide them for certain symbols
5078 (gold does this). */
5079 int attrs_valid =
5080 (index.version >= 7
5081 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5082
5083 /* Work around gold/15646. */
5084 if (attrs_valid)
5085 {
5086 if (!is_static && global_seen)
5087 continue;
5088 if (!is_static)
5089 global_seen = true;
5090 }
5091
5092 /* Only check the symbol's kind if it has one. */
5093 if (attrs_valid)
5094 {
5095 switch (kind)
5096 {
5097 case VARIABLES_DOMAIN:
5098 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5099 continue;
5100 break;
5101 case FUNCTIONS_DOMAIN:
5102 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
5103 continue;
5104 break;
5105 case TYPES_DOMAIN:
5106 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5107 continue;
5108 break;
5109 case MODULES_DOMAIN:
5110 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
5111 continue;
5112 break;
5113 default:
5114 break;
5115 }
5116 }
5117
5118 /* Don't crash on bad data. */
5119 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
5120 + dwarf2_per_objfile->all_type_units.size ()))
5121 {
5122 complaint (_(".gdb_index entry has bad CU index"
5123 " [in module %s]"),
5124 objfile_name (dwarf2_per_objfile->objfile));
5125 continue;
5126 }
5127
5128 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
5129 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5130 expansion_notify);
5131 }
5132 }
5133
5134 /* If FILE_MATCHER is non-NULL, set all the
5135 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5136 that match FILE_MATCHER. */
5137
5138 static void
5139 dw_expand_symtabs_matching_file_matcher
5140 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5141 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
5142 {
5143 if (file_matcher == NULL)
5144 return;
5145
5146 objfile *const objfile = dwarf2_per_objfile->objfile;
5147
5148 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5149 htab_eq_pointer,
5150 NULL, xcalloc, xfree));
5151 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
5152 htab_eq_pointer,
5153 NULL, xcalloc, xfree));
5154
5155 /* The rule is CUs specify all the files, including those used by
5156 any TU, so there's no need to scan TUs here. */
5157
5158 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5159 {
5160 QUIT;
5161
5162 per_cu->v.quick->mark = 0;
5163
5164 /* We only need to look at symtabs not already expanded. */
5165 if (per_cu->v.quick->compunit_symtab)
5166 continue;
5167
5168 quick_file_names *file_data = dw2_get_file_names (per_cu);
5169 if (file_data == NULL)
5170 continue;
5171
5172 if (htab_find (visited_not_found.get (), file_data) != NULL)
5173 continue;
5174 else if (htab_find (visited_found.get (), file_data) != NULL)
5175 {
5176 per_cu->v.quick->mark = 1;
5177 continue;
5178 }
5179
5180 for (int j = 0; j < file_data->num_file_names; ++j)
5181 {
5182 const char *this_real_name;
5183
5184 if (file_matcher (file_data->file_names[j], false))
5185 {
5186 per_cu->v.quick->mark = 1;
5187 break;
5188 }
5189
5190 /* Before we invoke realpath, which can get expensive when many
5191 files are involved, do a quick comparison of the basenames. */
5192 if (!basenames_may_differ
5193 && !file_matcher (lbasename (file_data->file_names[j]),
5194 true))
5195 continue;
5196
5197 this_real_name = dw2_get_real_path (objfile, file_data, j);
5198 if (file_matcher (this_real_name, false))
5199 {
5200 per_cu->v.quick->mark = 1;
5201 break;
5202 }
5203 }
5204
5205 void **slot = htab_find_slot (per_cu->v.quick->mark
5206 ? visited_found.get ()
5207 : visited_not_found.get (),
5208 file_data, INSERT);
5209 *slot = file_data;
5210 }
5211 }
5212
5213 static void
5214 dw2_expand_symtabs_matching
5215 (struct objfile *objfile,
5216 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5217 const lookup_name_info &lookup_name,
5218 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5219 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5220 enum search_domain kind)
5221 {
5222 struct dwarf2_per_objfile *dwarf2_per_objfile
5223 = get_dwarf2_per_objfile (objfile);
5224
5225 /* index_table is NULL if OBJF_READNOW. */
5226 if (!dwarf2_per_objfile->index_table)
5227 return;
5228
5229 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5230
5231 mapped_index &index = *dwarf2_per_objfile->index_table;
5232
5233 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5234 symbol_matcher,
5235 kind, [&] (offset_type idx)
5236 {
5237 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
5238 expansion_notify, kind);
5239 return true;
5240 });
5241 }
5242
5243 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5244 symtab. */
5245
5246 static struct compunit_symtab *
5247 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5248 CORE_ADDR pc)
5249 {
5250 int i;
5251
5252 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5253 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5254 return cust;
5255
5256 if (cust->includes == NULL)
5257 return NULL;
5258
5259 for (i = 0; cust->includes[i]; ++i)
5260 {
5261 struct compunit_symtab *s = cust->includes[i];
5262
5263 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5264 if (s != NULL)
5265 return s;
5266 }
5267
5268 return NULL;
5269 }
5270
5271 static struct compunit_symtab *
5272 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5273 struct bound_minimal_symbol msymbol,
5274 CORE_ADDR pc,
5275 struct obj_section *section,
5276 int warn_if_readin)
5277 {
5278 struct dwarf2_per_cu_data *data;
5279 struct compunit_symtab *result;
5280
5281 if (!objfile->partial_symtabs->psymtabs_addrmap)
5282 return NULL;
5283
5284 CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
5285 data = (struct dwarf2_per_cu_data *) addrmap_find
5286 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5287 if (!data)
5288 return NULL;
5289
5290 if (warn_if_readin && data->v.quick->compunit_symtab)
5291 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5292 paddress (get_objfile_arch (objfile), pc));
5293
5294 result
5295 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5296 false),
5297 pc);
5298 gdb_assert (result != NULL);
5299 return result;
5300 }
5301
5302 static void
5303 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5304 void *data, int need_fullname)
5305 {
5306 struct dwarf2_per_objfile *dwarf2_per_objfile
5307 = get_dwarf2_per_objfile (objfile);
5308
5309 if (!dwarf2_per_objfile->filenames_cache)
5310 {
5311 dwarf2_per_objfile->filenames_cache.emplace ();
5312
5313 htab_up visited (htab_create_alloc (10,
5314 htab_hash_pointer, htab_eq_pointer,
5315 NULL, xcalloc, xfree));
5316
5317 /* The rule is CUs specify all the files, including those used
5318 by any TU, so there's no need to scan TUs here. We can
5319 ignore file names coming from already-expanded CUs. */
5320
5321 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5322 {
5323 if (per_cu->v.quick->compunit_symtab)
5324 {
5325 void **slot = htab_find_slot (visited.get (),
5326 per_cu->v.quick->file_names,
5327 INSERT);
5328
5329 *slot = per_cu->v.quick->file_names;
5330 }
5331 }
5332
5333 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5334 {
5335 /* We only need to look at symtabs not already expanded. */
5336 if (per_cu->v.quick->compunit_symtab)
5337 continue;
5338
5339 quick_file_names *file_data = dw2_get_file_names (per_cu);
5340 if (file_data == NULL)
5341 continue;
5342
5343 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5344 if (*slot)
5345 {
5346 /* Already visited. */
5347 continue;
5348 }
5349 *slot = file_data;
5350
5351 for (int j = 0; j < file_data->num_file_names; ++j)
5352 {
5353 const char *filename = file_data->file_names[j];
5354 dwarf2_per_objfile->filenames_cache->seen (filename);
5355 }
5356 }
5357 }
5358
5359 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5360 {
5361 gdb::unique_xmalloc_ptr<char> this_real_name;
5362
5363 if (need_fullname)
5364 this_real_name = gdb_realpath (filename);
5365 (*fun) (filename, this_real_name.get (), data);
5366 });
5367 }
5368
5369 static int
5370 dw2_has_symbols (struct objfile *objfile)
5371 {
5372 return 1;
5373 }
5374
5375 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5376 {
5377 dw2_has_symbols,
5378 dw2_find_last_source_symtab,
5379 dw2_forget_cached_source_info,
5380 dw2_map_symtabs_matching_filename,
5381 dw2_lookup_symbol,
5382 dw2_print_stats,
5383 dw2_dump,
5384 dw2_expand_symtabs_for_function,
5385 dw2_expand_all_symtabs,
5386 dw2_expand_symtabs_with_fullname,
5387 dw2_map_matching_symbols,
5388 dw2_expand_symtabs_matching,
5389 dw2_find_pc_sect_compunit_symtab,
5390 NULL,
5391 dw2_map_symbol_filenames
5392 };
5393
5394 /* DWARF-5 debug_names reader. */
5395
5396 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5397 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5398
5399 /* A helper function that reads the .debug_names section in SECTION
5400 and fills in MAP. FILENAME is the name of the file containing the
5401 section; it is used for error reporting.
5402
5403 Returns true if all went well, false otherwise. */
5404
5405 static bool
5406 read_debug_names_from_section (struct objfile *objfile,
5407 const char *filename,
5408 struct dwarf2_section_info *section,
5409 mapped_debug_names &map)
5410 {
5411 if (dwarf2_section_empty_p (section))
5412 return false;
5413
5414 /* Older elfutils strip versions could keep the section in the main
5415 executable while splitting it for the separate debug info file. */
5416 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5417 return false;
5418
5419 dwarf2_read_section (objfile, section);
5420
5421 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5422
5423 const gdb_byte *addr = section->buffer;
5424
5425 bfd *const abfd = get_section_bfd_owner (section);
5426
5427 unsigned int bytes_read;
5428 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5429 addr += bytes_read;
5430
5431 map.dwarf5_is_dwarf64 = bytes_read != 4;
5432 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5433 if (bytes_read + length != section->size)
5434 {
5435 /* There may be multiple per-CU indices. */
5436 warning (_("Section .debug_names in %s length %s does not match "
5437 "section length %s, ignoring .debug_names."),
5438 filename, plongest (bytes_read + length),
5439 pulongest (section->size));
5440 return false;
5441 }
5442
5443 /* The version number. */
5444 uint16_t version = read_2_bytes (abfd, addr);
5445 addr += 2;
5446 if (version != 5)
5447 {
5448 warning (_("Section .debug_names in %s has unsupported version %d, "
5449 "ignoring .debug_names."),
5450 filename, version);
5451 return false;
5452 }
5453
5454 /* Padding. */
5455 uint16_t padding = read_2_bytes (abfd, addr);
5456 addr += 2;
5457 if (padding != 0)
5458 {
5459 warning (_("Section .debug_names in %s has unsupported padding %d, "
5460 "ignoring .debug_names."),
5461 filename, padding);
5462 return false;
5463 }
5464
5465 /* comp_unit_count - The number of CUs in the CU list. */
5466 map.cu_count = read_4_bytes (abfd, addr);
5467 addr += 4;
5468
5469 /* local_type_unit_count - The number of TUs in the local TU
5470 list. */
5471 map.tu_count = read_4_bytes (abfd, addr);
5472 addr += 4;
5473
5474 /* foreign_type_unit_count - The number of TUs in the foreign TU
5475 list. */
5476 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5477 addr += 4;
5478 if (foreign_tu_count != 0)
5479 {
5480 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5481 "ignoring .debug_names."),
5482 filename, static_cast<unsigned long> (foreign_tu_count));
5483 return false;
5484 }
5485
5486 /* bucket_count - The number of hash buckets in the hash lookup
5487 table. */
5488 map.bucket_count = read_4_bytes (abfd, addr);
5489 addr += 4;
5490
5491 /* name_count - The number of unique names in the index. */
5492 map.name_count = read_4_bytes (abfd, addr);
5493 addr += 4;
5494
5495 /* abbrev_table_size - The size in bytes of the abbreviations
5496 table. */
5497 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5498 addr += 4;
5499
5500 /* augmentation_string_size - The size in bytes of the augmentation
5501 string. This value is rounded up to a multiple of 4. */
5502 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5503 addr += 4;
5504 map.augmentation_is_gdb = ((augmentation_string_size
5505 == sizeof (dwarf5_augmentation))
5506 && memcmp (addr, dwarf5_augmentation,
5507 sizeof (dwarf5_augmentation)) == 0);
5508 augmentation_string_size += (-augmentation_string_size) & 3;
5509 addr += augmentation_string_size;
5510
5511 /* List of CUs */
5512 map.cu_table_reordered = addr;
5513 addr += map.cu_count * map.offset_size;
5514
5515 /* List of Local TUs */
5516 map.tu_table_reordered = addr;
5517 addr += map.tu_count * map.offset_size;
5518
5519 /* Hash Lookup Table */
5520 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5521 addr += map.bucket_count * 4;
5522 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5523 addr += map.name_count * 4;
5524
5525 /* Name Table */
5526 map.name_table_string_offs_reordered = addr;
5527 addr += map.name_count * map.offset_size;
5528 map.name_table_entry_offs_reordered = addr;
5529 addr += map.name_count * map.offset_size;
5530
5531 const gdb_byte *abbrev_table_start = addr;
5532 for (;;)
5533 {
5534 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5535 addr += bytes_read;
5536 if (index_num == 0)
5537 break;
5538
5539 const auto insertpair
5540 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5541 if (!insertpair.second)
5542 {
5543 warning (_("Section .debug_names in %s has duplicate index %s, "
5544 "ignoring .debug_names."),
5545 filename, pulongest (index_num));
5546 return false;
5547 }
5548 mapped_debug_names::index_val &indexval = insertpair.first->second;
5549 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5550 addr += bytes_read;
5551
5552 for (;;)
5553 {
5554 mapped_debug_names::index_val::attr attr;
5555 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5556 addr += bytes_read;
5557 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5558 addr += bytes_read;
5559 if (attr.form == DW_FORM_implicit_const)
5560 {
5561 attr.implicit_const = read_signed_leb128 (abfd, addr,
5562 &bytes_read);
5563 addr += bytes_read;
5564 }
5565 if (attr.dw_idx == 0 && attr.form == 0)
5566 break;
5567 indexval.attr_vec.push_back (std::move (attr));
5568 }
5569 }
5570 if (addr != abbrev_table_start + abbrev_table_size)
5571 {
5572 warning (_("Section .debug_names in %s has abbreviation_table "
5573 "of size %s vs. written as %u, ignoring .debug_names."),
5574 filename, plongest (addr - abbrev_table_start),
5575 abbrev_table_size);
5576 return false;
5577 }
5578 map.entry_pool = addr;
5579
5580 return true;
5581 }
5582
5583 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5584 list. */
5585
5586 static void
5587 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5588 const mapped_debug_names &map,
5589 dwarf2_section_info &section,
5590 bool is_dwz)
5591 {
5592 sect_offset sect_off_prev;
5593 for (uint32_t i = 0; i <= map.cu_count; ++i)
5594 {
5595 sect_offset sect_off_next;
5596 if (i < map.cu_count)
5597 {
5598 sect_off_next
5599 = (sect_offset) (extract_unsigned_integer
5600 (map.cu_table_reordered + i * map.offset_size,
5601 map.offset_size,
5602 map.dwarf5_byte_order));
5603 }
5604 else
5605 sect_off_next = (sect_offset) section.size;
5606 if (i >= 1)
5607 {
5608 const ULONGEST length = sect_off_next - sect_off_prev;
5609 dwarf2_per_cu_data *per_cu
5610 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5611 sect_off_prev, length);
5612 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5613 }
5614 sect_off_prev = sect_off_next;
5615 }
5616 }
5617
5618 /* Read the CU list from the mapped index, and use it to create all
5619 the CU objects for this dwarf2_per_objfile. */
5620
5621 static void
5622 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5623 const mapped_debug_names &map,
5624 const mapped_debug_names &dwz_map)
5625 {
5626 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5627 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5628
5629 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5630 dwarf2_per_objfile->info,
5631 false /* is_dwz */);
5632
5633 if (dwz_map.cu_count == 0)
5634 return;
5635
5636 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5637 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5638 true /* is_dwz */);
5639 }
5640
5641 /* Read .debug_names. If everything went ok, initialize the "quick"
5642 elements of all the CUs and return true. Otherwise, return false. */
5643
5644 static bool
5645 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5646 {
5647 std::unique_ptr<mapped_debug_names> map
5648 (new mapped_debug_names (dwarf2_per_objfile));
5649 mapped_debug_names dwz_map (dwarf2_per_objfile);
5650 struct objfile *objfile = dwarf2_per_objfile->objfile;
5651
5652 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5653 &dwarf2_per_objfile->debug_names,
5654 *map))
5655 return false;
5656
5657 /* Don't use the index if it's empty. */
5658 if (map->name_count == 0)
5659 return false;
5660
5661 /* If there is a .dwz file, read it so we can get its CU list as
5662 well. */
5663 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5664 if (dwz != NULL)
5665 {
5666 if (!read_debug_names_from_section (objfile,
5667 bfd_get_filename (dwz->dwz_bfd.get ()),
5668 &dwz->debug_names, dwz_map))
5669 {
5670 warning (_("could not read '.debug_names' section from %s; skipping"),
5671 bfd_get_filename (dwz->dwz_bfd.get ()));
5672 return false;
5673 }
5674 }
5675
5676 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5677
5678 if (map->tu_count != 0)
5679 {
5680 /* We can only handle a single .debug_types when we have an
5681 index. */
5682 if (dwarf2_per_objfile->types.size () != 1)
5683 return false;
5684
5685 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5686
5687 create_signatured_type_table_from_debug_names
5688 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5689 }
5690
5691 create_addrmap_from_aranges (dwarf2_per_objfile,
5692 &dwarf2_per_objfile->debug_aranges);
5693
5694 dwarf2_per_objfile->debug_names_table = std::move (map);
5695 dwarf2_per_objfile->using_index = 1;
5696 dwarf2_per_objfile->quick_file_names_table =
5697 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5698
5699 return true;
5700 }
5701
5702 /* Type used to manage iterating over all CUs looking for a symbol for
5703 .debug_names. */
5704
5705 class dw2_debug_names_iterator
5706 {
5707 public:
5708 dw2_debug_names_iterator (const mapped_debug_names &map,
5709 gdb::optional<block_enum> block_index,
5710 domain_enum domain,
5711 const char *name)
5712 : m_map (map), m_block_index (block_index), m_domain (domain),
5713 m_addr (find_vec_in_debug_names (map, name))
5714 {}
5715
5716 dw2_debug_names_iterator (const mapped_debug_names &map,
5717 search_domain search, uint32_t namei)
5718 : m_map (map),
5719 m_search (search),
5720 m_addr (find_vec_in_debug_names (map, namei))
5721 {}
5722
5723 dw2_debug_names_iterator (const mapped_debug_names &map,
5724 block_enum block_index, domain_enum domain,
5725 uint32_t namei)
5726 : m_map (map), m_block_index (block_index), m_domain (domain),
5727 m_addr (find_vec_in_debug_names (map, namei))
5728 {}
5729
5730 /* Return the next matching CU or NULL if there are no more. */
5731 dwarf2_per_cu_data *next ();
5732
5733 private:
5734 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5735 const char *name);
5736 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5737 uint32_t namei);
5738
5739 /* The internalized form of .debug_names. */
5740 const mapped_debug_names &m_map;
5741
5742 /* If set, only look for symbols that match that block. Valid values are
5743 GLOBAL_BLOCK and STATIC_BLOCK. */
5744 const gdb::optional<block_enum> m_block_index;
5745
5746 /* The kind of symbol we're looking for. */
5747 const domain_enum m_domain = UNDEF_DOMAIN;
5748 const search_domain m_search = ALL_DOMAIN;
5749
5750 /* The list of CUs from the index entry of the symbol, or NULL if
5751 not found. */
5752 const gdb_byte *m_addr;
5753 };
5754
5755 const char *
5756 mapped_debug_names::namei_to_name (uint32_t namei) const
5757 {
5758 const ULONGEST namei_string_offs
5759 = extract_unsigned_integer ((name_table_string_offs_reordered
5760 + namei * offset_size),
5761 offset_size,
5762 dwarf5_byte_order);
5763 return read_indirect_string_at_offset
5764 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
5765 }
5766
5767 /* Find a slot in .debug_names for the object named NAME. If NAME is
5768 found, return pointer to its pool data. If NAME cannot be found,
5769 return NULL. */
5770
5771 const gdb_byte *
5772 dw2_debug_names_iterator::find_vec_in_debug_names
5773 (const mapped_debug_names &map, const char *name)
5774 {
5775 int (*cmp) (const char *, const char *);
5776
5777 gdb::unique_xmalloc_ptr<char> without_params;
5778 if (current_language->la_language == language_cplus
5779 || current_language->la_language == language_fortran
5780 || current_language->la_language == language_d)
5781 {
5782 /* NAME is already canonical. Drop any qualifiers as
5783 .debug_names does not contain any. */
5784
5785 if (strchr (name, '(') != NULL)
5786 {
5787 without_params = cp_remove_params (name);
5788 if (without_params != NULL)
5789 name = without_params.get ();
5790 }
5791 }
5792
5793 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5794
5795 const uint32_t full_hash = dwarf5_djb_hash (name);
5796 uint32_t namei
5797 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5798 (map.bucket_table_reordered
5799 + (full_hash % map.bucket_count)), 4,
5800 map.dwarf5_byte_order);
5801 if (namei == 0)
5802 return NULL;
5803 --namei;
5804 if (namei >= map.name_count)
5805 {
5806 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5807 "[in module %s]"),
5808 namei, map.name_count,
5809 objfile_name (map.dwarf2_per_objfile->objfile));
5810 return NULL;
5811 }
5812
5813 for (;;)
5814 {
5815 const uint32_t namei_full_hash
5816 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5817 (map.hash_table_reordered + namei), 4,
5818 map.dwarf5_byte_order);
5819 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5820 return NULL;
5821
5822 if (full_hash == namei_full_hash)
5823 {
5824 const char *const namei_string = map.namei_to_name (namei);
5825
5826 #if 0 /* An expensive sanity check. */
5827 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5828 {
5829 complaint (_("Wrong .debug_names hash for string at index %u "
5830 "[in module %s]"),
5831 namei, objfile_name (dwarf2_per_objfile->objfile));
5832 return NULL;
5833 }
5834 #endif
5835
5836 if (cmp (namei_string, name) == 0)
5837 {
5838 const ULONGEST namei_entry_offs
5839 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5840 + namei * map.offset_size),
5841 map.offset_size, map.dwarf5_byte_order);
5842 return map.entry_pool + namei_entry_offs;
5843 }
5844 }
5845
5846 ++namei;
5847 if (namei >= map.name_count)
5848 return NULL;
5849 }
5850 }
5851
5852 const gdb_byte *
5853 dw2_debug_names_iterator::find_vec_in_debug_names
5854 (const mapped_debug_names &map, uint32_t namei)
5855 {
5856 if (namei >= map.name_count)
5857 {
5858 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5859 "[in module %s]"),
5860 namei, map.name_count,
5861 objfile_name (map.dwarf2_per_objfile->objfile));
5862 return NULL;
5863 }
5864
5865 const ULONGEST namei_entry_offs
5866 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5867 + namei * map.offset_size),
5868 map.offset_size, map.dwarf5_byte_order);
5869 return map.entry_pool + namei_entry_offs;
5870 }
5871
5872 /* See dw2_debug_names_iterator. */
5873
5874 dwarf2_per_cu_data *
5875 dw2_debug_names_iterator::next ()
5876 {
5877 if (m_addr == NULL)
5878 return NULL;
5879
5880 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5881 struct objfile *objfile = dwarf2_per_objfile->objfile;
5882 bfd *const abfd = objfile->obfd;
5883
5884 again:
5885
5886 unsigned int bytes_read;
5887 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5888 m_addr += bytes_read;
5889 if (abbrev == 0)
5890 return NULL;
5891
5892 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5893 if (indexval_it == m_map.abbrev_map.cend ())
5894 {
5895 complaint (_("Wrong .debug_names undefined abbrev code %s "
5896 "[in module %s]"),
5897 pulongest (abbrev), objfile_name (objfile));
5898 return NULL;
5899 }
5900 const mapped_debug_names::index_val &indexval = indexval_it->second;
5901 enum class symbol_linkage {
5902 unknown,
5903 static_,
5904 extern_,
5905 } symbol_linkage_ = symbol_linkage::unknown;
5906 dwarf2_per_cu_data *per_cu = NULL;
5907 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5908 {
5909 ULONGEST ull;
5910 switch (attr.form)
5911 {
5912 case DW_FORM_implicit_const:
5913 ull = attr.implicit_const;
5914 break;
5915 case DW_FORM_flag_present:
5916 ull = 1;
5917 break;
5918 case DW_FORM_udata:
5919 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5920 m_addr += bytes_read;
5921 break;
5922 default:
5923 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5924 dwarf_form_name (attr.form),
5925 objfile_name (objfile));
5926 return NULL;
5927 }
5928 switch (attr.dw_idx)
5929 {
5930 case DW_IDX_compile_unit:
5931 /* Don't crash on bad data. */
5932 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5933 {
5934 complaint (_(".debug_names entry has bad CU index %s"
5935 " [in module %s]"),
5936 pulongest (ull),
5937 objfile_name (dwarf2_per_objfile->objfile));
5938 continue;
5939 }
5940 per_cu = dwarf2_per_objfile->get_cutu (ull);
5941 break;
5942 case DW_IDX_type_unit:
5943 /* Don't crash on bad data. */
5944 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5945 {
5946 complaint (_(".debug_names entry has bad TU index %s"
5947 " [in module %s]"),
5948 pulongest (ull),
5949 objfile_name (dwarf2_per_objfile->objfile));
5950 continue;
5951 }
5952 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5953 break;
5954 case DW_IDX_GNU_internal:
5955 if (!m_map.augmentation_is_gdb)
5956 break;
5957 symbol_linkage_ = symbol_linkage::static_;
5958 break;
5959 case DW_IDX_GNU_external:
5960 if (!m_map.augmentation_is_gdb)
5961 break;
5962 symbol_linkage_ = symbol_linkage::extern_;
5963 break;
5964 }
5965 }
5966
5967 /* Skip if already read in. */
5968 if (per_cu->v.quick->compunit_symtab)
5969 goto again;
5970
5971 /* Check static vs global. */
5972 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5973 {
5974 const bool want_static = *m_block_index == STATIC_BLOCK;
5975 const bool symbol_is_static =
5976 symbol_linkage_ == symbol_linkage::static_;
5977 if (want_static != symbol_is_static)
5978 goto again;
5979 }
5980
5981 /* Match dw2_symtab_iter_next, symbol_kind
5982 and debug_names::psymbol_tag. */
5983 switch (m_domain)
5984 {
5985 case VAR_DOMAIN:
5986 switch (indexval.dwarf_tag)
5987 {
5988 case DW_TAG_variable:
5989 case DW_TAG_subprogram:
5990 /* Some types are also in VAR_DOMAIN. */
5991 case DW_TAG_typedef:
5992 case DW_TAG_structure_type:
5993 break;
5994 default:
5995 goto again;
5996 }
5997 break;
5998 case STRUCT_DOMAIN:
5999 switch (indexval.dwarf_tag)
6000 {
6001 case DW_TAG_typedef:
6002 case DW_TAG_structure_type:
6003 break;
6004 default:
6005 goto again;
6006 }
6007 break;
6008 case LABEL_DOMAIN:
6009 switch (indexval.dwarf_tag)
6010 {
6011 case 0:
6012 case DW_TAG_variable:
6013 break;
6014 default:
6015 goto again;
6016 }
6017 break;
6018 case MODULE_DOMAIN:
6019 switch (indexval.dwarf_tag)
6020 {
6021 case DW_TAG_module:
6022 break;
6023 default:
6024 goto again;
6025 }
6026 break;
6027 default:
6028 break;
6029 }
6030
6031 /* Match dw2_expand_symtabs_matching, symbol_kind and
6032 debug_names::psymbol_tag. */
6033 switch (m_search)
6034 {
6035 case VARIABLES_DOMAIN:
6036 switch (indexval.dwarf_tag)
6037 {
6038 case DW_TAG_variable:
6039 break;
6040 default:
6041 goto again;
6042 }
6043 break;
6044 case FUNCTIONS_DOMAIN:
6045 switch (indexval.dwarf_tag)
6046 {
6047 case DW_TAG_subprogram:
6048 break;
6049 default:
6050 goto again;
6051 }
6052 break;
6053 case TYPES_DOMAIN:
6054 switch (indexval.dwarf_tag)
6055 {
6056 case DW_TAG_typedef:
6057 case DW_TAG_structure_type:
6058 break;
6059 default:
6060 goto again;
6061 }
6062 break;
6063 case MODULES_DOMAIN:
6064 switch (indexval.dwarf_tag)
6065 {
6066 case DW_TAG_module:
6067 break;
6068 default:
6069 goto again;
6070 }
6071 default:
6072 break;
6073 }
6074
6075 return per_cu;
6076 }
6077
6078 static struct compunit_symtab *
6079 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
6080 const char *name, domain_enum domain)
6081 {
6082 struct dwarf2_per_objfile *dwarf2_per_objfile
6083 = get_dwarf2_per_objfile (objfile);
6084
6085 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6086 if (!mapp)
6087 {
6088 /* index is NULL if OBJF_READNOW. */
6089 return NULL;
6090 }
6091 const auto &map = *mapp;
6092
6093 dw2_debug_names_iterator iter (map, block_index, domain, name);
6094
6095 struct compunit_symtab *stab_best = NULL;
6096 struct dwarf2_per_cu_data *per_cu;
6097 while ((per_cu = iter.next ()) != NULL)
6098 {
6099 struct symbol *sym, *with_opaque = NULL;
6100 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
6101 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6102 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
6103
6104 sym = block_find_symbol (block, name, domain,
6105 block_find_non_opaque_type_preferred,
6106 &with_opaque);
6107
6108 /* Some caution must be observed with overloaded functions and
6109 methods, since the index will not contain any overload
6110 information (but NAME might contain it). */
6111
6112 if (sym != NULL
6113 && strcmp_iw (sym->search_name (), name) == 0)
6114 return stab;
6115 if (with_opaque != NULL
6116 && strcmp_iw (with_opaque->search_name (), name) == 0)
6117 stab_best = stab;
6118
6119 /* Keep looking through other CUs. */
6120 }
6121
6122 return stab_best;
6123 }
6124
6125 /* This dumps minimal information about .debug_names. It is called
6126 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6127 uses this to verify that .debug_names has been loaded. */
6128
6129 static void
6130 dw2_debug_names_dump (struct objfile *objfile)
6131 {
6132 struct dwarf2_per_objfile *dwarf2_per_objfile
6133 = get_dwarf2_per_objfile (objfile);
6134
6135 gdb_assert (dwarf2_per_objfile->using_index);
6136 printf_filtered (".debug_names:");
6137 if (dwarf2_per_objfile->debug_names_table)
6138 printf_filtered (" exists\n");
6139 else
6140 printf_filtered (" faked for \"readnow\"\n");
6141 printf_filtered ("\n");
6142 }
6143
6144 static void
6145 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6146 const char *func_name)
6147 {
6148 struct dwarf2_per_objfile *dwarf2_per_objfile
6149 = get_dwarf2_per_objfile (objfile);
6150
6151 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6152 if (dwarf2_per_objfile->debug_names_table)
6153 {
6154 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6155
6156 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
6157
6158 struct dwarf2_per_cu_data *per_cu;
6159 while ((per_cu = iter.next ()) != NULL)
6160 dw2_instantiate_symtab (per_cu, false);
6161 }
6162 }
6163
6164 static void
6165 dw2_debug_names_map_matching_symbols
6166 (struct objfile *objfile,
6167 const lookup_name_info &name, domain_enum domain,
6168 int global,
6169 gdb::function_view<symbol_found_callback_ftype> callback,
6170 symbol_compare_ftype *ordered_compare)
6171 {
6172 struct dwarf2_per_objfile *dwarf2_per_objfile
6173 = get_dwarf2_per_objfile (objfile);
6174
6175 /* debug_names_table is NULL if OBJF_READNOW. */
6176 if (!dwarf2_per_objfile->debug_names_table)
6177 return;
6178
6179 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6180 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6181
6182 const char *match_name = name.ada ().lookup_name ().c_str ();
6183 auto matcher = [&] (const char *symname)
6184 {
6185 if (ordered_compare == nullptr)
6186 return true;
6187 return ordered_compare (symname, match_name) == 0;
6188 };
6189
6190 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6191 [&] (offset_type namei)
6192 {
6193 /* The name was matched, now expand corresponding CUs that were
6194 marked. */
6195 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6196
6197 struct dwarf2_per_cu_data *per_cu;
6198 while ((per_cu = iter.next ()) != NULL)
6199 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6200 return true;
6201 });
6202
6203 /* It's a shame we couldn't do this inside the
6204 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6205 that have already been expanded. Instead, this loop matches what
6206 the psymtab code does. */
6207 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6208 {
6209 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6210 if (cust != nullptr)
6211 {
6212 const struct block *block
6213 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6214 if (!iterate_over_symbols_terminated (block, name,
6215 domain, callback))
6216 break;
6217 }
6218 }
6219 }
6220
6221 static void
6222 dw2_debug_names_expand_symtabs_matching
6223 (struct objfile *objfile,
6224 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6225 const lookup_name_info &lookup_name,
6226 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6227 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6228 enum search_domain kind)
6229 {
6230 struct dwarf2_per_objfile *dwarf2_per_objfile
6231 = get_dwarf2_per_objfile (objfile);
6232
6233 /* debug_names_table is NULL if OBJF_READNOW. */
6234 if (!dwarf2_per_objfile->debug_names_table)
6235 return;
6236
6237 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
6238
6239 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6240
6241 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6242 symbol_matcher,
6243 kind, [&] (offset_type namei)
6244 {
6245 /* The name was matched, now expand corresponding CUs that were
6246 marked. */
6247 dw2_debug_names_iterator iter (map, kind, namei);
6248
6249 struct dwarf2_per_cu_data *per_cu;
6250 while ((per_cu = iter.next ()) != NULL)
6251 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6252 expansion_notify);
6253 return true;
6254 });
6255 }
6256
6257 const struct quick_symbol_functions dwarf2_debug_names_functions =
6258 {
6259 dw2_has_symbols,
6260 dw2_find_last_source_symtab,
6261 dw2_forget_cached_source_info,
6262 dw2_map_symtabs_matching_filename,
6263 dw2_debug_names_lookup_symbol,
6264 dw2_print_stats,
6265 dw2_debug_names_dump,
6266 dw2_debug_names_expand_symtabs_for_function,
6267 dw2_expand_all_symtabs,
6268 dw2_expand_symtabs_with_fullname,
6269 dw2_debug_names_map_matching_symbols,
6270 dw2_debug_names_expand_symtabs_matching,
6271 dw2_find_pc_sect_compunit_symtab,
6272 NULL,
6273 dw2_map_symbol_filenames
6274 };
6275
6276 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6277 to either a dwarf2_per_objfile or dwz_file object. */
6278
6279 template <typename T>
6280 static gdb::array_view<const gdb_byte>
6281 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6282 {
6283 dwarf2_section_info *section = &section_owner->gdb_index;
6284
6285 if (dwarf2_section_empty_p (section))
6286 return {};
6287
6288 /* Older elfutils strip versions could keep the section in the main
6289 executable while splitting it for the separate debug info file. */
6290 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6291 return {};
6292
6293 dwarf2_read_section (obj, section);
6294
6295 /* dwarf2_section_info::size is a bfd_size_type, while
6296 gdb::array_view works with size_t. On 32-bit hosts, with
6297 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6298 is 32-bit. So we need an explicit narrowing conversion here.
6299 This is fine, because it's impossible to allocate or mmap an
6300 array/buffer larger than what size_t can represent. */
6301 return gdb::make_array_view (section->buffer, section->size);
6302 }
6303
6304 /* Lookup the index cache for the contents of the index associated to
6305 DWARF2_OBJ. */
6306
6307 static gdb::array_view<const gdb_byte>
6308 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6309 {
6310 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6311 if (build_id == nullptr)
6312 return {};
6313
6314 return global_index_cache.lookup_gdb_index (build_id,
6315 &dwarf2_obj->index_cache_res);
6316 }
6317
6318 /* Same as the above, but for DWZ. */
6319
6320 static gdb::array_view<const gdb_byte>
6321 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6322 {
6323 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6324 if (build_id == nullptr)
6325 return {};
6326
6327 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6328 }
6329
6330 /* See symfile.h. */
6331
6332 bool
6333 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6334 {
6335 struct dwarf2_per_objfile *dwarf2_per_objfile
6336 = get_dwarf2_per_objfile (objfile);
6337
6338 /* If we're about to read full symbols, don't bother with the
6339 indices. In this case we also don't care if some other debug
6340 format is making psymtabs, because they are all about to be
6341 expanded anyway. */
6342 if ((objfile->flags & OBJF_READNOW))
6343 {
6344 dwarf2_per_objfile->using_index = 1;
6345 create_all_comp_units (dwarf2_per_objfile);
6346 create_all_type_units (dwarf2_per_objfile);
6347 dwarf2_per_objfile->quick_file_names_table
6348 = create_quick_file_names_table
6349 (dwarf2_per_objfile->all_comp_units.size ());
6350
6351 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
6352 + dwarf2_per_objfile->all_type_units.size ()); ++i)
6353 {
6354 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
6355
6356 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6357 struct dwarf2_per_cu_quick_data);
6358 }
6359
6360 /* Return 1 so that gdb sees the "quick" functions. However,
6361 these functions will be no-ops because we will have expanded
6362 all symtabs. */
6363 *index_kind = dw_index_kind::GDB_INDEX;
6364 return true;
6365 }
6366
6367 if (dwarf2_read_debug_names (dwarf2_per_objfile))
6368 {
6369 *index_kind = dw_index_kind::DEBUG_NAMES;
6370 return true;
6371 }
6372
6373 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6374 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6375 get_gdb_index_contents_from_section<dwz_file>))
6376 {
6377 *index_kind = dw_index_kind::GDB_INDEX;
6378 return true;
6379 }
6380
6381 /* ... otherwise, try to find the index in the index cache. */
6382 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6383 get_gdb_index_contents_from_cache,
6384 get_gdb_index_contents_from_cache_dwz))
6385 {
6386 global_index_cache.hit ();
6387 *index_kind = dw_index_kind::GDB_INDEX;
6388 return true;
6389 }
6390
6391 global_index_cache.miss ();
6392 return false;
6393 }
6394
6395 \f
6396
6397 /* Build a partial symbol table. */
6398
6399 void
6400 dwarf2_build_psymtabs (struct objfile *objfile)
6401 {
6402 struct dwarf2_per_objfile *dwarf2_per_objfile
6403 = get_dwarf2_per_objfile (objfile);
6404
6405 init_psymbol_list (objfile, 1024);
6406
6407 try
6408 {
6409 /* This isn't really ideal: all the data we allocate on the
6410 objfile's obstack is still uselessly kept around. However,
6411 freeing it seems unsafe. */
6412 psymtab_discarder psymtabs (objfile);
6413 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6414 psymtabs.keep ();
6415
6416 /* (maybe) store an index in the cache. */
6417 global_index_cache.store (dwarf2_per_objfile);
6418 }
6419 catch (const gdb_exception_error &except)
6420 {
6421 exception_print (gdb_stderr, except);
6422 }
6423 }
6424
6425 /* Return the total length of the CU described by HEADER. */
6426
6427 static unsigned int
6428 get_cu_length (const struct comp_unit_head *header)
6429 {
6430 return header->initial_length_size + header->length;
6431 }
6432
6433 /* Return TRUE if SECT_OFF is within CU_HEADER. */
6434
6435 static inline bool
6436 offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
6437 {
6438 sect_offset bottom = cu_header->sect_off;
6439 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
6440
6441 return sect_off >= bottom && sect_off < top;
6442 }
6443
6444 /* Find the base address of the compilation unit for range lists and
6445 location lists. It will normally be specified by DW_AT_low_pc.
6446 In DWARF-3 draft 4, the base address could be overridden by
6447 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6448 compilation units with discontinuous ranges. */
6449
6450 static void
6451 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6452 {
6453 struct attribute *attr;
6454
6455 cu->base_known = 0;
6456 cu->base_address = 0;
6457
6458 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6459 if (attr != nullptr)
6460 {
6461 cu->base_address = attr_value_as_address (attr);
6462 cu->base_known = 1;
6463 }
6464 else
6465 {
6466 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6467 if (attr != nullptr)
6468 {
6469 cu->base_address = attr_value_as_address (attr);
6470 cu->base_known = 1;
6471 }
6472 }
6473 }
6474
6475 /* Read in the comp unit header information from the debug_info at info_ptr.
6476 Use rcuh_kind::COMPILE as the default type if not known by the caller.
6477 NOTE: This leaves members offset, first_die_offset to be filled in
6478 by the caller. */
6479
6480 static const gdb_byte *
6481 read_comp_unit_head (struct comp_unit_head *cu_header,
6482 const gdb_byte *info_ptr,
6483 struct dwarf2_section_info *section,
6484 rcuh_kind section_kind)
6485 {
6486 int signed_addr;
6487 unsigned int bytes_read;
6488 const char *filename = get_section_file_name (section);
6489 bfd *abfd = get_section_bfd_owner (section);
6490
6491 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6492 cu_header->initial_length_size = bytes_read;
6493 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
6494 info_ptr += bytes_read;
6495 cu_header->version = read_2_bytes (abfd, info_ptr);
6496 if (cu_header->version < 2 || cu_header->version > 5)
6497 error (_("Dwarf Error: wrong version in compilation unit header "
6498 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6499 cu_header->version, filename);
6500 info_ptr += 2;
6501 if (cu_header->version < 5)
6502 switch (section_kind)
6503 {
6504 case rcuh_kind::COMPILE:
6505 cu_header->unit_type = DW_UT_compile;
6506 break;
6507 case rcuh_kind::TYPE:
6508 cu_header->unit_type = DW_UT_type;
6509 break;
6510 default:
6511 internal_error (__FILE__, __LINE__,
6512 _("read_comp_unit_head: invalid section_kind"));
6513 }
6514 else
6515 {
6516 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6517 (read_1_byte (abfd, info_ptr));
6518 info_ptr += 1;
6519 switch (cu_header->unit_type)
6520 {
6521 case DW_UT_compile:
6522 case DW_UT_partial:
6523 case DW_UT_skeleton:
6524 case DW_UT_split_compile:
6525 if (section_kind != rcuh_kind::COMPILE)
6526 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6527 "(is %s, should be %s) [in module %s]"),
6528 dwarf_unit_type_name (cu_header->unit_type),
6529 dwarf_unit_type_name (DW_UT_type), filename);
6530 break;
6531 case DW_UT_type:
6532 case DW_UT_split_type:
6533 section_kind = rcuh_kind::TYPE;
6534 break;
6535 default:
6536 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6537 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6538 "[in module %s]"), cu_header->unit_type,
6539 dwarf_unit_type_name (DW_UT_compile),
6540 dwarf_unit_type_name (DW_UT_skeleton),
6541 dwarf_unit_type_name (DW_UT_split_compile),
6542 dwarf_unit_type_name (DW_UT_type),
6543 dwarf_unit_type_name (DW_UT_split_type), filename);
6544 }
6545
6546 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6547 info_ptr += 1;
6548 }
6549 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6550 cu_header,
6551 &bytes_read);
6552 info_ptr += bytes_read;
6553 if (cu_header->version < 5)
6554 {
6555 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6556 info_ptr += 1;
6557 }
6558 signed_addr = bfd_get_sign_extend_vma (abfd);
6559 if (signed_addr < 0)
6560 internal_error (__FILE__, __LINE__,
6561 _("read_comp_unit_head: dwarf from non elf file"));
6562 cu_header->signed_addr_p = signed_addr;
6563
6564 bool header_has_signature = section_kind == rcuh_kind::TYPE
6565 || cu_header->unit_type == DW_UT_skeleton
6566 || cu_header->unit_type == DW_UT_split_compile;
6567
6568 if (header_has_signature)
6569 {
6570 cu_header->signature = read_8_bytes (abfd, info_ptr);
6571 info_ptr += 8;
6572 }
6573
6574 if (section_kind == rcuh_kind::TYPE)
6575 {
6576 LONGEST type_offset;
6577 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6578 info_ptr += bytes_read;
6579 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6580 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
6581 error (_("Dwarf Error: Too big type_offset in compilation unit "
6582 "header (is %s) [in module %s]"), plongest (type_offset),
6583 filename);
6584 }
6585
6586 return info_ptr;
6587 }
6588
6589 /* Helper function that returns the proper abbrev section for
6590 THIS_CU. */
6591
6592 static struct dwarf2_section_info *
6593 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6594 {
6595 struct dwarf2_section_info *abbrev;
6596 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6597
6598 if (this_cu->is_dwz)
6599 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
6600 else
6601 abbrev = &dwarf2_per_objfile->abbrev;
6602
6603 return abbrev;
6604 }
6605
6606 /* Subroutine of read_and_check_comp_unit_head and
6607 read_and_check_type_unit_head to simplify them.
6608 Perform various error checking on the header. */
6609
6610 static void
6611 error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6612 struct comp_unit_head *header,
6613 struct dwarf2_section_info *section,
6614 struct dwarf2_section_info *abbrev_section)
6615 {
6616 const char *filename = get_section_file_name (section);
6617
6618 if (to_underlying (header->abbrev_sect_off)
6619 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
6620 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6621 "(offset %s + 6) [in module %s]"),
6622 sect_offset_str (header->abbrev_sect_off),
6623 sect_offset_str (header->sect_off),
6624 filename);
6625
6626 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
6627 avoid potential 32-bit overflow. */
6628 if (((ULONGEST) header->sect_off + get_cu_length (header))
6629 > section->size)
6630 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
6631 "(offset %s + 0) [in module %s]"),
6632 header->length, sect_offset_str (header->sect_off),
6633 filename);
6634 }
6635
6636 /* Read in a CU/TU header and perform some basic error checking.
6637 The contents of the header are stored in HEADER.
6638 The result is a pointer to the start of the first DIE. */
6639
6640 static const gdb_byte *
6641 read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6642 struct comp_unit_head *header,
6643 struct dwarf2_section_info *section,
6644 struct dwarf2_section_info *abbrev_section,
6645 const gdb_byte *info_ptr,
6646 rcuh_kind section_kind)
6647 {
6648 const gdb_byte *beg_of_comp_unit = info_ptr;
6649
6650 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
6651
6652 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
6653
6654 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
6655
6656 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6657 abbrev_section);
6658
6659 return info_ptr;
6660 }
6661
6662 /* Fetch the abbreviation table offset from a comp or type unit header. */
6663
6664 static sect_offset
6665 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6666 struct dwarf2_section_info *section,
6667 sect_offset sect_off)
6668 {
6669 bfd *abfd = get_section_bfd_owner (section);
6670 const gdb_byte *info_ptr;
6671 unsigned int initial_length_size, offset_size;
6672 uint16_t version;
6673
6674 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
6675 info_ptr = section->buffer + to_underlying (sect_off);
6676 read_initial_length (abfd, info_ptr, &initial_length_size);
6677 offset_size = initial_length_size == 4 ? 4 : 8;
6678 info_ptr += initial_length_size;
6679
6680 version = read_2_bytes (abfd, info_ptr);
6681 info_ptr += 2;
6682 if (version >= 5)
6683 {
6684 /* Skip unit type and address size. */
6685 info_ptr += 2;
6686 }
6687
6688 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
6689 }
6690
6691 /* Allocate a new partial symtab for file named NAME and mark this new
6692 partial symtab as being an include of PST. */
6693
6694 static void
6695 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
6696 struct objfile *objfile)
6697 {
6698 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6699
6700 if (!IS_ABSOLUTE_PATH (subpst->filename))
6701 {
6702 /* It shares objfile->objfile_obstack. */
6703 subpst->dirname = pst->dirname;
6704 }
6705
6706 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6707 subpst->dependencies[0] = pst;
6708 subpst->number_of_dependencies = 1;
6709
6710 subpst->read_symtab = pst->read_symtab;
6711
6712 /* No private part is necessary for include psymtabs. This property
6713 can be used to differentiate between such include psymtabs and
6714 the regular ones. */
6715 subpst->read_symtab_private = NULL;
6716 }
6717
6718 /* Read the Line Number Program data and extract the list of files
6719 included by the source file represented by PST. Build an include
6720 partial symtab for each of these included files. */
6721
6722 static void
6723 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6724 struct die_info *die,
6725 struct partial_symtab *pst)
6726 {
6727 line_header_up lh;
6728 struct attribute *attr;
6729
6730 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6731 if (attr != nullptr)
6732 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6733 if (lh == NULL)
6734 return; /* No linetable, so no includes. */
6735
6736 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6737 that we pass in the raw text_low here; that is ok because we're
6738 only decoding the line table to make include partial symtabs, and
6739 so the addresses aren't really used. */
6740 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6741 pst->raw_text_low (), 1);
6742 }
6743
6744 static hashval_t
6745 hash_signatured_type (const void *item)
6746 {
6747 const struct signatured_type *sig_type
6748 = (const struct signatured_type *) item;
6749
6750 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6751 return sig_type->signature;
6752 }
6753
6754 static int
6755 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6756 {
6757 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6758 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6759
6760 return lhs->signature == rhs->signature;
6761 }
6762
6763 /* Allocate a hash table for signatured types. */
6764
6765 static htab_t
6766 allocate_signatured_type_table (struct objfile *objfile)
6767 {
6768 return htab_create_alloc_ex (41,
6769 hash_signatured_type,
6770 eq_signatured_type,
6771 NULL,
6772 &objfile->objfile_obstack,
6773 hashtab_obstack_allocate,
6774 dummy_obstack_deallocate);
6775 }
6776
6777 /* A helper function to add a signatured type CU to a table. */
6778
6779 static int
6780 add_signatured_type_cu_to_table (void **slot, void *datum)
6781 {
6782 struct signatured_type *sigt = (struct signatured_type *) *slot;
6783 std::vector<signatured_type *> *all_type_units
6784 = (std::vector<signatured_type *> *) datum;
6785
6786 all_type_units->push_back (sigt);
6787
6788 return 1;
6789 }
6790
6791 /* A helper for create_debug_types_hash_table. Read types from SECTION
6792 and fill them into TYPES_HTAB. It will process only type units,
6793 therefore DW_UT_type. */
6794
6795 static void
6796 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6797 struct dwo_file *dwo_file,
6798 dwarf2_section_info *section, htab_t &types_htab,
6799 rcuh_kind section_kind)
6800 {
6801 struct objfile *objfile = dwarf2_per_objfile->objfile;
6802 struct dwarf2_section_info *abbrev_section;
6803 bfd *abfd;
6804 const gdb_byte *info_ptr, *end_ptr;
6805
6806 abbrev_section = (dwo_file != NULL
6807 ? &dwo_file->sections.abbrev
6808 : &dwarf2_per_objfile->abbrev);
6809
6810 if (dwarf_read_debug)
6811 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6812 get_section_name (section),
6813 get_section_file_name (abbrev_section));
6814
6815 dwarf2_read_section (objfile, section);
6816 info_ptr = section->buffer;
6817
6818 if (info_ptr == NULL)
6819 return;
6820
6821 /* We can't set abfd until now because the section may be empty or
6822 not present, in which case the bfd is unknown. */
6823 abfd = get_section_bfd_owner (section);
6824
6825 /* We don't use cutu_reader here because we don't need to read
6826 any dies: the signature is in the header. */
6827
6828 end_ptr = info_ptr + section->size;
6829 while (info_ptr < end_ptr)
6830 {
6831 struct signatured_type *sig_type;
6832 struct dwo_unit *dwo_tu;
6833 void **slot;
6834 const gdb_byte *ptr = info_ptr;
6835 struct comp_unit_head header;
6836 unsigned int length;
6837
6838 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6839
6840 /* Initialize it due to a false compiler warning. */
6841 header.signature = -1;
6842 header.type_cu_offset_in_tu = (cu_offset) -1;
6843
6844 /* We need to read the type's signature in order to build the hash
6845 table, but we don't need anything else just yet. */
6846
6847 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6848 abbrev_section, ptr, section_kind);
6849
6850 length = get_cu_length (&header);
6851
6852 /* Skip dummy type units. */
6853 if (ptr >= info_ptr + length
6854 || peek_abbrev_code (abfd, ptr) == 0
6855 || header.unit_type != DW_UT_type)
6856 {
6857 info_ptr += length;
6858 continue;
6859 }
6860
6861 if (types_htab == NULL)
6862 {
6863 if (dwo_file)
6864 types_htab = allocate_dwo_unit_table (objfile);
6865 else
6866 types_htab = allocate_signatured_type_table (objfile);
6867 }
6868
6869 if (dwo_file)
6870 {
6871 sig_type = NULL;
6872 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6873 struct dwo_unit);
6874 dwo_tu->dwo_file = dwo_file;
6875 dwo_tu->signature = header.signature;
6876 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6877 dwo_tu->section = section;
6878 dwo_tu->sect_off = sect_off;
6879 dwo_tu->length = length;
6880 }
6881 else
6882 {
6883 /* N.B.: type_offset is not usable if this type uses a DWO file.
6884 The real type_offset is in the DWO file. */
6885 dwo_tu = NULL;
6886 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6887 struct signatured_type);
6888 sig_type->signature = header.signature;
6889 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6890 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6891 sig_type->per_cu.is_debug_types = 1;
6892 sig_type->per_cu.section = section;
6893 sig_type->per_cu.sect_off = sect_off;
6894 sig_type->per_cu.length = length;
6895 }
6896
6897 slot = htab_find_slot (types_htab,
6898 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6899 INSERT);
6900 gdb_assert (slot != NULL);
6901 if (*slot != NULL)
6902 {
6903 sect_offset dup_sect_off;
6904
6905 if (dwo_file)
6906 {
6907 const struct dwo_unit *dup_tu
6908 = (const struct dwo_unit *) *slot;
6909
6910 dup_sect_off = dup_tu->sect_off;
6911 }
6912 else
6913 {
6914 const struct signatured_type *dup_tu
6915 = (const struct signatured_type *) *slot;
6916
6917 dup_sect_off = dup_tu->per_cu.sect_off;
6918 }
6919
6920 complaint (_("debug type entry at offset %s is duplicate to"
6921 " the entry at offset %s, signature %s"),
6922 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6923 hex_string (header.signature));
6924 }
6925 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6926
6927 if (dwarf_read_debug > 1)
6928 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6929 sect_offset_str (sect_off),
6930 hex_string (header.signature));
6931
6932 info_ptr += length;
6933 }
6934 }
6935
6936 /* Create the hash table of all entries in the .debug_types
6937 (or .debug_types.dwo) section(s).
6938 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6939 otherwise it is NULL.
6940
6941 The result is a pointer to the hash table or NULL if there are no types.
6942
6943 Note: This function processes DWO files only, not DWP files. */
6944
6945 static void
6946 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6947 struct dwo_file *dwo_file,
6948 gdb::array_view<dwarf2_section_info> type_sections,
6949 htab_t &types_htab)
6950 {
6951 for (dwarf2_section_info &section : type_sections)
6952 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6953 types_htab, rcuh_kind::TYPE);
6954 }
6955
6956 /* Create the hash table of all entries in the .debug_types section,
6957 and initialize all_type_units.
6958 The result is zero if there is an error (e.g. missing .debug_types section),
6959 otherwise non-zero. */
6960
6961 static int
6962 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6963 {
6964 htab_t types_htab = NULL;
6965
6966 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6967 &dwarf2_per_objfile->info, types_htab,
6968 rcuh_kind::COMPILE);
6969 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6970 dwarf2_per_objfile->types, types_htab);
6971 if (types_htab == NULL)
6972 {
6973 dwarf2_per_objfile->signatured_types = NULL;
6974 return 0;
6975 }
6976
6977 dwarf2_per_objfile->signatured_types = types_htab;
6978
6979 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6980 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6981
6982 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6983 &dwarf2_per_objfile->all_type_units);
6984
6985 return 1;
6986 }
6987
6988 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6989 If SLOT is non-NULL, it is the entry to use in the hash table.
6990 Otherwise we find one. */
6991
6992 static struct signatured_type *
6993 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6994 void **slot)
6995 {
6996 struct objfile *objfile = dwarf2_per_objfile->objfile;
6997
6998 if (dwarf2_per_objfile->all_type_units.size ()
6999 == dwarf2_per_objfile->all_type_units.capacity ())
7000 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
7001
7002 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7003 struct signatured_type);
7004
7005 dwarf2_per_objfile->all_type_units.push_back (sig_type);
7006 sig_type->signature = sig;
7007 sig_type->per_cu.is_debug_types = 1;
7008 if (dwarf2_per_objfile->using_index)
7009 {
7010 sig_type->per_cu.v.quick =
7011 OBSTACK_ZALLOC (&objfile->objfile_obstack,
7012 struct dwarf2_per_cu_quick_data);
7013 }
7014
7015 if (slot == NULL)
7016 {
7017 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7018 sig_type, INSERT);
7019 }
7020 gdb_assert (*slot == NULL);
7021 *slot = sig_type;
7022 /* The rest of sig_type must be filled in by the caller. */
7023 return sig_type;
7024 }
7025
7026 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
7027 Fill in SIG_ENTRY with DWO_ENTRY. */
7028
7029 static void
7030 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
7031 struct signatured_type *sig_entry,
7032 struct dwo_unit *dwo_entry)
7033 {
7034 /* Make sure we're not clobbering something we don't expect to. */
7035 gdb_assert (! sig_entry->per_cu.queued);
7036 gdb_assert (sig_entry->per_cu.cu == NULL);
7037 if (dwarf2_per_objfile->using_index)
7038 {
7039 gdb_assert (sig_entry->per_cu.v.quick != NULL);
7040 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
7041 }
7042 else
7043 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
7044 gdb_assert (sig_entry->signature == dwo_entry->signature);
7045 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
7046 gdb_assert (sig_entry->type_unit_group == NULL);
7047 gdb_assert (sig_entry->dwo_unit == NULL);
7048
7049 sig_entry->per_cu.section = dwo_entry->section;
7050 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7051 sig_entry->per_cu.length = dwo_entry->length;
7052 sig_entry->per_cu.reading_dwo_directly = 1;
7053 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
7054 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
7055 sig_entry->dwo_unit = dwo_entry;
7056 }
7057
7058 /* Subroutine of lookup_signatured_type.
7059 If we haven't read the TU yet, create the signatured_type data structure
7060 for a TU to be read in directly from a DWO file, bypassing the stub.
7061 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7062 using .gdb_index, then when reading a CU we want to stay in the DWO file
7063 containing that CU. Otherwise we could end up reading several other DWO
7064 files (due to comdat folding) to process the transitive closure of all the
7065 mentioned TUs, and that can be slow. The current DWO file will have every
7066 type signature that it needs.
7067 We only do this for .gdb_index because in the psymtab case we already have
7068 to read all the DWOs to build the type unit groups. */
7069
7070 static struct signatured_type *
7071 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7072 {
7073 struct dwarf2_per_objfile *dwarf2_per_objfile
7074 = cu->per_cu->dwarf2_per_objfile;
7075 struct objfile *objfile = dwarf2_per_objfile->objfile;
7076 struct dwo_file *dwo_file;
7077 struct dwo_unit find_dwo_entry, *dwo_entry;
7078 struct signatured_type find_sig_entry, *sig_entry;
7079 void **slot;
7080
7081 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7082
7083 /* If TU skeletons have been removed then we may not have read in any
7084 TUs yet. */
7085 if (dwarf2_per_objfile->signatured_types == NULL)
7086 {
7087 dwarf2_per_objfile->signatured_types
7088 = allocate_signatured_type_table (objfile);
7089 }
7090
7091 /* We only ever need to read in one copy of a signatured type.
7092 Use the global signatured_types array to do our own comdat-folding
7093 of types. If this is the first time we're reading this TU, and
7094 the TU has an entry in .gdb_index, replace the recorded data from
7095 .gdb_index with this TU. */
7096
7097 find_sig_entry.signature = sig;
7098 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7099 &find_sig_entry, INSERT);
7100 sig_entry = (struct signatured_type *) *slot;
7101
7102 /* We can get here with the TU already read, *or* in the process of being
7103 read. Don't reassign the global entry to point to this DWO if that's
7104 the case. Also note that if the TU is already being read, it may not
7105 have come from a DWO, the program may be a mix of Fission-compiled
7106 code and non-Fission-compiled code. */
7107
7108 /* Have we already tried to read this TU?
7109 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7110 needn't exist in the global table yet). */
7111 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
7112 return sig_entry;
7113
7114 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7115 dwo_unit of the TU itself. */
7116 dwo_file = cu->dwo_unit->dwo_file;
7117
7118 /* Ok, this is the first time we're reading this TU. */
7119 if (dwo_file->tus == NULL)
7120 return NULL;
7121 find_dwo_entry.signature = sig;
7122 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
7123 if (dwo_entry == NULL)
7124 return NULL;
7125
7126 /* If the global table doesn't have an entry for this TU, add one. */
7127 if (sig_entry == NULL)
7128 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7129
7130 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7131 sig_entry->per_cu.tu_read = 1;
7132 return sig_entry;
7133 }
7134
7135 /* Subroutine of lookup_signatured_type.
7136 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
7137 then try the DWP file. If the TU stub (skeleton) has been removed then
7138 it won't be in .gdb_index. */
7139
7140 static struct signatured_type *
7141 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7142 {
7143 struct dwarf2_per_objfile *dwarf2_per_objfile
7144 = cu->per_cu->dwarf2_per_objfile;
7145 struct objfile *objfile = dwarf2_per_objfile->objfile;
7146 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
7147 struct dwo_unit *dwo_entry;
7148 struct signatured_type find_sig_entry, *sig_entry;
7149 void **slot;
7150
7151 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7152 gdb_assert (dwp_file != NULL);
7153
7154 /* If TU skeletons have been removed then we may not have read in any
7155 TUs yet. */
7156 if (dwarf2_per_objfile->signatured_types == NULL)
7157 {
7158 dwarf2_per_objfile->signatured_types
7159 = allocate_signatured_type_table (objfile);
7160 }
7161
7162 find_sig_entry.signature = sig;
7163 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7164 &find_sig_entry, INSERT);
7165 sig_entry = (struct signatured_type *) *slot;
7166
7167 /* Have we already tried to read this TU?
7168 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7169 needn't exist in the global table yet). */
7170 if (sig_entry != NULL)
7171 return sig_entry;
7172
7173 if (dwp_file->tus == NULL)
7174 return NULL;
7175 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
7176 sig, 1 /* is_debug_types */);
7177 if (dwo_entry == NULL)
7178 return NULL;
7179
7180 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7181 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
7182
7183 return sig_entry;
7184 }
7185
7186 /* Lookup a signature based type for DW_FORM_ref_sig8.
7187 Returns NULL if signature SIG is not present in the table.
7188 It is up to the caller to complain about this. */
7189
7190 static struct signatured_type *
7191 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7192 {
7193 struct dwarf2_per_objfile *dwarf2_per_objfile
7194 = cu->per_cu->dwarf2_per_objfile;
7195
7196 if (cu->dwo_unit
7197 && dwarf2_per_objfile->using_index)
7198 {
7199 /* We're in a DWO/DWP file, and we're using .gdb_index.
7200 These cases require special processing. */
7201 if (get_dwp_file (dwarf2_per_objfile) == NULL)
7202 return lookup_dwo_signatured_type (cu, sig);
7203 else
7204 return lookup_dwp_signatured_type (cu, sig);
7205 }
7206 else
7207 {
7208 struct signatured_type find_entry, *entry;
7209
7210 if (dwarf2_per_objfile->signatured_types == NULL)
7211 return NULL;
7212 find_entry.signature = sig;
7213 entry = ((struct signatured_type *)
7214 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
7215 return entry;
7216 }
7217 }
7218
7219 /* Return the address base of the compile unit, which, if exists, is stored
7220 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
7221 static gdb::optional<ULONGEST>
7222 lookup_addr_base (struct die_info *comp_unit_die)
7223 {
7224 struct attribute *attr;
7225 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
7226 if (attr == nullptr)
7227 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
7228 if (attr == nullptr)
7229 return gdb::optional<ULONGEST> ();
7230 return DW_UNSND (attr);
7231 }
7232
7233 /* Return range lists base of the compile unit, which, if exists, is stored
7234 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
7235 static ULONGEST
7236 lookup_ranges_base (struct die_info *comp_unit_die)
7237 {
7238 struct attribute *attr;
7239 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
7240 if (attr == nullptr)
7241 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
7242 if (attr == nullptr)
7243 return 0;
7244 return DW_UNSND (attr);
7245 }
7246
7247 /* Low level DIE reading support. */
7248
7249 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7250
7251 static void
7252 init_cu_die_reader (struct die_reader_specs *reader,
7253 struct dwarf2_cu *cu,
7254 struct dwarf2_section_info *section,
7255 struct dwo_file *dwo_file,
7256 struct abbrev_table *abbrev_table)
7257 {
7258 gdb_assert (section->readin && section->buffer != NULL);
7259 reader->abfd = get_section_bfd_owner (section);
7260 reader->cu = cu;
7261 reader->dwo_file = dwo_file;
7262 reader->die_section = section;
7263 reader->buffer = section->buffer;
7264 reader->buffer_end = section->buffer + section->size;
7265 reader->comp_dir = NULL;
7266 reader->abbrev_table = abbrev_table;
7267 }
7268
7269 /* Subroutine of cutu_reader to simplify it.
7270 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7271 There's just a lot of work to do, and cutu_reader is big enough
7272 already.
7273
7274 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7275 from it to the DIE in the DWO. If NULL we are skipping the stub.
7276 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7277 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
7278 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7279 STUB_COMP_DIR may be non-NULL.
7280 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7281 are filled in with the info of the DIE from the DWO file.
7282 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7283 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7284 kept around for at least as long as *RESULT_READER.
7285
7286 The result is non-zero if a valid (non-dummy) DIE was found. */
7287
7288 static int
7289 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7290 struct dwo_unit *dwo_unit,
7291 struct die_info *stub_comp_unit_die,
7292 const char *stub_comp_dir,
7293 struct die_reader_specs *result_reader,
7294 const gdb_byte **result_info_ptr,
7295 struct die_info **result_comp_unit_die,
7296 int *result_has_children,
7297 abbrev_table_up *result_dwo_abbrev_table)
7298 {
7299 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7300 struct objfile *objfile = dwarf2_per_objfile->objfile;
7301 struct dwarf2_cu *cu = this_cu->cu;
7302 bfd *abfd;
7303 const gdb_byte *begin_info_ptr, *info_ptr;
7304 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7305 int i,num_extra_attrs;
7306 struct dwarf2_section_info *dwo_abbrev_section;
7307 struct die_info *comp_unit_die;
7308
7309 /* At most one of these may be provided. */
7310 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
7311
7312 /* These attributes aren't processed until later:
7313 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
7314 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7315 referenced later. However, these attributes are found in the stub
7316 which we won't have later. In order to not impose this complication
7317 on the rest of the code, we read them here and copy them to the
7318 DWO CU/TU die. */
7319
7320 stmt_list = NULL;
7321 low_pc = NULL;
7322 high_pc = NULL;
7323 ranges = NULL;
7324 comp_dir = NULL;
7325
7326 if (stub_comp_unit_die != NULL)
7327 {
7328 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7329 DWO file. */
7330 if (! this_cu->is_debug_types)
7331 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7332 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7333 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7334 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7335 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7336
7337 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
7338
7339 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
7340 here (if needed). We need the value before we can process
7341 DW_AT_ranges. */
7342 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
7343 }
7344 else if (stub_comp_dir != NULL)
7345 {
7346 /* Reconstruct the comp_dir attribute to simplify the code below. */
7347 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
7348 comp_dir->name = DW_AT_comp_dir;
7349 comp_dir->form = DW_FORM_string;
7350 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7351 DW_STRING (comp_dir) = stub_comp_dir;
7352 }
7353
7354 /* Set up for reading the DWO CU/TU. */
7355 cu->dwo_unit = dwo_unit;
7356 dwarf2_section_info *section = dwo_unit->section;
7357 dwarf2_read_section (objfile, section);
7358 abfd = get_section_bfd_owner (section);
7359 begin_info_ptr = info_ptr = (section->buffer
7360 + to_underlying (dwo_unit->sect_off));
7361 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7362
7363 if (this_cu->is_debug_types)
7364 {
7365 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7366
7367 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7368 &cu->header, section,
7369 dwo_abbrev_section,
7370 info_ptr, rcuh_kind::TYPE);
7371 /* This is not an assert because it can be caused by bad debug info. */
7372 if (sig_type->signature != cu->header.signature)
7373 {
7374 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7375 " TU at offset %s [in module %s]"),
7376 hex_string (sig_type->signature),
7377 hex_string (cu->header.signature),
7378 sect_offset_str (dwo_unit->sect_off),
7379 bfd_get_filename (abfd));
7380 }
7381 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7382 /* For DWOs coming from DWP files, we don't know the CU length
7383 nor the type's offset in the TU until now. */
7384 dwo_unit->length = get_cu_length (&cu->header);
7385 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7386
7387 /* Establish the type offset that can be used to lookup the type.
7388 For DWO files, we don't know it until now. */
7389 sig_type->type_offset_in_section
7390 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7391 }
7392 else
7393 {
7394 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7395 &cu->header, section,
7396 dwo_abbrev_section,
7397 info_ptr, rcuh_kind::COMPILE);
7398 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7399 /* For DWOs coming from DWP files, we don't know the CU length
7400 until now. */
7401 dwo_unit->length = get_cu_length (&cu->header);
7402 }
7403
7404 *result_dwo_abbrev_table
7405 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7406 cu->header.abbrev_sect_off);
7407 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7408 result_dwo_abbrev_table->get ());
7409
7410 /* Read in the die, but leave space to copy over the attributes
7411 from the stub. This has the benefit of simplifying the rest of
7412 the code - all the work to maintain the illusion of a single
7413 DW_TAG_{compile,type}_unit DIE is done here. */
7414 num_extra_attrs = ((stmt_list != NULL)
7415 + (low_pc != NULL)
7416 + (high_pc != NULL)
7417 + (ranges != NULL)
7418 + (comp_dir != NULL));
7419 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7420 result_has_children, num_extra_attrs);
7421
7422 /* Copy over the attributes from the stub to the DIE we just read in. */
7423 comp_unit_die = *result_comp_unit_die;
7424 i = comp_unit_die->num_attrs;
7425 if (stmt_list != NULL)
7426 comp_unit_die->attrs[i++] = *stmt_list;
7427 if (low_pc != NULL)
7428 comp_unit_die->attrs[i++] = *low_pc;
7429 if (high_pc != NULL)
7430 comp_unit_die->attrs[i++] = *high_pc;
7431 if (ranges != NULL)
7432 comp_unit_die->attrs[i++] = *ranges;
7433 if (comp_dir != NULL)
7434 comp_unit_die->attrs[i++] = *comp_dir;
7435 comp_unit_die->num_attrs += num_extra_attrs;
7436
7437 if (dwarf_die_debug)
7438 {
7439 fprintf_unfiltered (gdb_stdlog,
7440 "Read die from %s@0x%x of %s:\n",
7441 get_section_name (section),
7442 (unsigned) (begin_info_ptr - section->buffer),
7443 bfd_get_filename (abfd));
7444 dump_die (comp_unit_die, dwarf_die_debug);
7445 }
7446
7447 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7448 TUs by skipping the stub and going directly to the entry in the DWO file.
7449 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7450 to get it via circuitous means. Blech. */
7451 if (comp_dir != NULL)
7452 result_reader->comp_dir = DW_STRING (comp_dir);
7453
7454 /* Skip dummy compilation units. */
7455 if (info_ptr >= begin_info_ptr + dwo_unit->length
7456 || peek_abbrev_code (abfd, info_ptr) == 0)
7457 return 0;
7458
7459 *result_info_ptr = info_ptr;
7460 return 1;
7461 }
7462
7463 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7464 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7465 signature is part of the header. */
7466 static gdb::optional<ULONGEST>
7467 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7468 {
7469 if (cu->header.version >= 5)
7470 return cu->header.signature;
7471 struct attribute *attr;
7472 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7473 if (attr == nullptr)
7474 return gdb::optional<ULONGEST> ();
7475 return DW_UNSND (attr);
7476 }
7477
7478 /* Subroutine of cutu_reader to simplify it.
7479 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7480 Returns NULL if the specified DWO unit cannot be found. */
7481
7482 static struct dwo_unit *
7483 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7484 struct die_info *comp_unit_die,
7485 const char *dwo_name)
7486 {
7487 struct dwarf2_cu *cu = this_cu->cu;
7488 struct dwo_unit *dwo_unit;
7489 const char *comp_dir;
7490
7491 gdb_assert (cu != NULL);
7492
7493 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7494 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7495 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7496
7497 if (this_cu->is_debug_types)
7498 {
7499 struct signatured_type *sig_type;
7500
7501 /* Since this_cu is the first member of struct signatured_type,
7502 we can go from a pointer to one to a pointer to the other. */
7503 sig_type = (struct signatured_type *) this_cu;
7504 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7505 }
7506 else
7507 {
7508 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7509 if (!signature.has_value ())
7510 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7511 " [in module %s]"),
7512 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
7513 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7514 *signature);
7515 }
7516
7517 return dwo_unit;
7518 }
7519
7520 /* Subroutine of cutu_reader to simplify it.
7521 See it for a description of the parameters.
7522 Read a TU directly from a DWO file, bypassing the stub. */
7523
7524 void
7525 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7526 int use_existing_cu, int keep)
7527 {
7528 struct signatured_type *sig_type;
7529 struct die_reader_specs reader;
7530
7531 /* Verify we can do the following downcast, and that we have the
7532 data we need. */
7533 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7534 sig_type = (struct signatured_type *) this_cu;
7535 gdb_assert (sig_type->dwo_unit != NULL);
7536
7537 if (use_existing_cu && this_cu->cu != NULL)
7538 {
7539 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
7540 /* There's no need to do the rereading_dwo_cu handling that
7541 cutu_reader does since we don't read the stub. */
7542 }
7543 else
7544 {
7545 /* If !use_existing_cu, this_cu->cu must be NULL. */
7546 gdb_assert (this_cu->cu == NULL);
7547 m_new_cu.reset (new dwarf2_cu (this_cu));
7548 }
7549
7550 /* A future optimization, if needed, would be to use an existing
7551 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7552 could share abbrev tables. */
7553
7554 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
7555 NULL /* stub_comp_unit_die */,
7556 sig_type->dwo_unit->dwo_file->comp_dir,
7557 &reader, &info_ptr,
7558 &comp_unit_die, &has_children,
7559 &m_dwo_abbrev_table) == 0)
7560 {
7561 /* Dummy die. */
7562 dummy_p = true;
7563 }
7564 }
7565
7566 /* Initialize a CU (or TU) and read its DIEs.
7567 If the CU defers to a DWO file, read the DWO file as well.
7568
7569 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7570 Otherwise the table specified in the comp unit header is read in and used.
7571 This is an optimization for when we already have the abbrev table.
7572
7573 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7574 Otherwise, a new CU is allocated with xmalloc.
7575
7576 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7577 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7578 end. */
7579
7580 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7581 struct abbrev_table *abbrev_table,
7582 int use_existing_cu, int keep,
7583 bool skip_partial)
7584 : die_reader_specs {},
7585 m_this_cu (this_cu),
7586 m_keep (keep)
7587 {
7588 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7589 struct objfile *objfile = dwarf2_per_objfile->objfile;
7590 struct dwarf2_section_info *section = this_cu->section;
7591 bfd *abfd = get_section_bfd_owner (section);
7592 struct dwarf2_cu *cu;
7593 const gdb_byte *begin_info_ptr;
7594 struct signatured_type *sig_type = NULL;
7595 struct dwarf2_section_info *abbrev_section;
7596 /* Non-zero if CU currently points to a DWO file and we need to
7597 reread it. When this happens we need to reread the skeleton die
7598 before we can reread the DWO file (this only applies to CUs, not TUs). */
7599 int rereading_dwo_cu = 0;
7600
7601 if (dwarf_die_debug)
7602 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7603 this_cu->is_debug_types ? "type" : "comp",
7604 sect_offset_str (this_cu->sect_off));
7605
7606 if (use_existing_cu)
7607 gdb_assert (keep);
7608
7609 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7610 file (instead of going through the stub), short-circuit all of this. */
7611 if (this_cu->reading_dwo_directly)
7612 {
7613 /* Narrow down the scope of possibilities to have to understand. */
7614 gdb_assert (this_cu->is_debug_types);
7615 gdb_assert (abbrev_table == NULL);
7616 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
7617 return;
7618 }
7619
7620 /* This is cheap if the section is already read in. */
7621 dwarf2_read_section (objfile, section);
7622
7623 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7624
7625 abbrev_section = get_abbrev_section_for_cu (this_cu);
7626
7627 if (use_existing_cu && this_cu->cu != NULL)
7628 {
7629 cu = this_cu->cu;
7630 /* If this CU is from a DWO file we need to start over, we need to
7631 refetch the attributes from the skeleton CU.
7632 This could be optimized by retrieving those attributes from when we
7633 were here the first time: the previous comp_unit_die was stored in
7634 comp_unit_obstack. But there's no data yet that we need this
7635 optimization. */
7636 if (cu->dwo_unit != NULL)
7637 rereading_dwo_cu = 1;
7638 }
7639 else
7640 {
7641 /* If !use_existing_cu, this_cu->cu must be NULL. */
7642 gdb_assert (this_cu->cu == NULL);
7643 m_new_cu.reset (new dwarf2_cu (this_cu));
7644 cu = m_new_cu.get ();
7645 }
7646
7647 /* Get the header. */
7648 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7649 {
7650 /* We already have the header, there's no need to read it in again. */
7651 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7652 }
7653 else
7654 {
7655 if (this_cu->is_debug_types)
7656 {
7657 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7658 &cu->header, section,
7659 abbrev_section, info_ptr,
7660 rcuh_kind::TYPE);
7661
7662 /* Since per_cu is the first member of struct signatured_type,
7663 we can go from a pointer to one to a pointer to the other. */
7664 sig_type = (struct signatured_type *) this_cu;
7665 gdb_assert (sig_type->signature == cu->header.signature);
7666 gdb_assert (sig_type->type_offset_in_tu
7667 == cu->header.type_cu_offset_in_tu);
7668 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7669
7670 /* LENGTH has not been set yet for type units if we're
7671 using .gdb_index. */
7672 this_cu->length = get_cu_length (&cu->header);
7673
7674 /* Establish the type offset that can be used to lookup the type. */
7675 sig_type->type_offset_in_section =
7676 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7677
7678 this_cu->dwarf_version = cu->header.version;
7679 }
7680 else
7681 {
7682 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7683 &cu->header, section,
7684 abbrev_section,
7685 info_ptr,
7686 rcuh_kind::COMPILE);
7687
7688 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7689 gdb_assert (this_cu->length == get_cu_length (&cu->header));
7690 this_cu->dwarf_version = cu->header.version;
7691 }
7692 }
7693
7694 /* Skip dummy compilation units. */
7695 if (info_ptr >= begin_info_ptr + this_cu->length
7696 || peek_abbrev_code (abfd, info_ptr) == 0)
7697 {
7698 dummy_p = true;
7699 return;
7700 }
7701
7702 /* If we don't have them yet, read the abbrevs for this compilation unit.
7703 And if we need to read them now, make sure they're freed when we're
7704 done. */
7705 if (abbrev_table != NULL)
7706 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7707 else
7708 {
7709 m_abbrev_table_holder
7710 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7711 cu->header.abbrev_sect_off);
7712 abbrev_table = m_abbrev_table_holder.get ();
7713 }
7714
7715 /* Read the top level CU/TU die. */
7716 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7717 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
7718
7719 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7720 {
7721 dummy_p = true;
7722 return;
7723 }
7724
7725 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7726 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7727 table from the DWO file and pass the ownership over to us. It will be
7728 referenced from READER, so we must make sure to free it after we're done
7729 with READER.
7730
7731 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7732 DWO CU, that this test will fail (the attribute will not be present). */
7733 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7734 if (dwo_name != nullptr)
7735 {
7736 struct dwo_unit *dwo_unit;
7737 struct die_info *dwo_comp_unit_die;
7738
7739 if (has_children)
7740 {
7741 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7742 " has children (offset %s) [in module %s]"),
7743 sect_offset_str (this_cu->sect_off),
7744 bfd_get_filename (abfd));
7745 }
7746 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7747 if (dwo_unit != NULL)
7748 {
7749 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7750 comp_unit_die, NULL,
7751 this, &info_ptr,
7752 &dwo_comp_unit_die, &has_children,
7753 &m_dwo_abbrev_table) == 0)
7754 {
7755 /* Dummy die. */
7756 dummy_p = true;
7757 return;
7758 }
7759 comp_unit_die = dwo_comp_unit_die;
7760 }
7761 else
7762 {
7763 /* Yikes, we couldn't find the rest of the DIE, we only have
7764 the stub. A complaint has already been logged. There's
7765 not much more we can do except pass on the stub DIE to
7766 die_reader_func. We don't want to throw an error on bad
7767 debug info. */
7768 }
7769 }
7770 }
7771
7772 cutu_reader::~cutu_reader ()
7773 {
7774 /* Done, clean up. */
7775 if (m_new_cu != NULL && m_keep && !dummy_p)
7776 {
7777 struct dwarf2_per_objfile *dwarf2_per_objfile
7778 = m_this_cu->dwarf2_per_objfile;
7779 /* Link this CU into read_in_chain. */
7780 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7781 dwarf2_per_objfile->read_in_chain = m_this_cu;
7782 /* The chain owns it now. */
7783 m_new_cu.release ();
7784 }
7785 }
7786
7787 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7788 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7789 assumed to have already done the lookup to find the DWO file).
7790
7791 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7792 THIS_CU->is_debug_types, but nothing else.
7793
7794 We fill in THIS_CU->length.
7795
7796 THIS_CU->cu is always freed when done.
7797 This is done in order to not leave THIS_CU->cu in a state where we have
7798 to care whether it refers to the "main" CU or the DWO CU.
7799
7800 When parent_cu is passed, it is used to provide a default value for
7801 str_offsets_base and addr_base from the parent. */
7802
7803 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7804 struct dwarf2_cu *parent_cu,
7805 struct dwo_file *dwo_file)
7806 : die_reader_specs {},
7807 m_this_cu (this_cu)
7808 {
7809 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
7810 struct objfile *objfile = dwarf2_per_objfile->objfile;
7811 struct dwarf2_section_info *section = this_cu->section;
7812 bfd *abfd = get_section_bfd_owner (section);
7813 struct dwarf2_section_info *abbrev_section;
7814 const gdb_byte *begin_info_ptr, *info_ptr;
7815 int has_children;
7816
7817 if (dwarf_die_debug)
7818 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7819 this_cu->is_debug_types ? "type" : "comp",
7820 sect_offset_str (this_cu->sect_off));
7821
7822 gdb_assert (this_cu->cu == NULL);
7823
7824 abbrev_section = (dwo_file != NULL
7825 ? &dwo_file->sections.abbrev
7826 : get_abbrev_section_for_cu (this_cu));
7827
7828 /* This is cheap if the section is already read in. */
7829 dwarf2_read_section (objfile, section);
7830
7831 m_new_cu.reset (new dwarf2_cu (this_cu));
7832
7833 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7834 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7835 &m_new_cu->header, section,
7836 abbrev_section, info_ptr,
7837 (this_cu->is_debug_types
7838 ? rcuh_kind::TYPE
7839 : rcuh_kind::COMPILE));
7840
7841 if (parent_cu != nullptr)
7842 {
7843 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7844 m_new_cu->addr_base = parent_cu->addr_base;
7845 }
7846 this_cu->length = get_cu_length (&m_new_cu->header);
7847
7848 /* Skip dummy compilation units. */
7849 if (info_ptr >= begin_info_ptr + this_cu->length
7850 || peek_abbrev_code (abfd, info_ptr) == 0)
7851 {
7852 dummy_p = true;
7853 return;
7854 }
7855
7856 m_abbrev_table_holder
7857 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7858 m_new_cu->header.abbrev_sect_off);
7859
7860 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7861 m_abbrev_table_holder.get ());
7862 info_ptr = read_full_die (this, &comp_unit_die, info_ptr, &has_children);
7863 }
7864
7865 \f
7866 /* Type Unit Groups.
7867
7868 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7869 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7870 so that all types coming from the same compilation (.o file) are grouped
7871 together. A future step could be to put the types in the same symtab as
7872 the CU the types ultimately came from. */
7873
7874 static hashval_t
7875 hash_type_unit_group (const void *item)
7876 {
7877 const struct type_unit_group *tu_group
7878 = (const struct type_unit_group *) item;
7879
7880 return hash_stmt_list_entry (&tu_group->hash);
7881 }
7882
7883 static int
7884 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7885 {
7886 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7887 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7888
7889 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7890 }
7891
7892 /* Allocate a hash table for type unit groups. */
7893
7894 static htab_t
7895 allocate_type_unit_groups_table (struct objfile *objfile)
7896 {
7897 return htab_create_alloc_ex (3,
7898 hash_type_unit_group,
7899 eq_type_unit_group,
7900 NULL,
7901 &objfile->objfile_obstack,
7902 hashtab_obstack_allocate,
7903 dummy_obstack_deallocate);
7904 }
7905
7906 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7907 partial symtabs. We combine several TUs per psymtab to not let the size
7908 of any one psymtab grow too big. */
7909 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7910 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7911
7912 /* Helper routine for get_type_unit_group.
7913 Create the type_unit_group object used to hold one or more TUs. */
7914
7915 static struct type_unit_group *
7916 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7917 {
7918 struct dwarf2_per_objfile *dwarf2_per_objfile
7919 = cu->per_cu->dwarf2_per_objfile;
7920 struct objfile *objfile = dwarf2_per_objfile->objfile;
7921 struct dwarf2_per_cu_data *per_cu;
7922 struct type_unit_group *tu_group;
7923
7924 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7925 struct type_unit_group);
7926 per_cu = &tu_group->per_cu;
7927 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7928
7929 if (dwarf2_per_objfile->using_index)
7930 {
7931 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7932 struct dwarf2_per_cu_quick_data);
7933 }
7934 else
7935 {
7936 unsigned int line_offset = to_underlying (line_offset_struct);
7937 struct partial_symtab *pst;
7938 std::string name;
7939
7940 /* Give the symtab a useful name for debug purposes. */
7941 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7942 name = string_printf ("<type_units_%d>",
7943 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7944 else
7945 name = string_printf ("<type_units_at_0x%x>", line_offset);
7946
7947 pst = create_partial_symtab (per_cu, name.c_str ());
7948 pst->anonymous = 1;
7949 }
7950
7951 tu_group->hash.dwo_unit = cu->dwo_unit;
7952 tu_group->hash.line_sect_off = line_offset_struct;
7953
7954 return tu_group;
7955 }
7956
7957 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7958 STMT_LIST is a DW_AT_stmt_list attribute. */
7959
7960 static struct type_unit_group *
7961 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7962 {
7963 struct dwarf2_per_objfile *dwarf2_per_objfile
7964 = cu->per_cu->dwarf2_per_objfile;
7965 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7966 struct type_unit_group *tu_group;
7967 void **slot;
7968 unsigned int line_offset;
7969 struct type_unit_group type_unit_group_for_lookup;
7970
7971 if (dwarf2_per_objfile->type_unit_groups == NULL)
7972 {
7973 dwarf2_per_objfile->type_unit_groups =
7974 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
7975 }
7976
7977 /* Do we need to create a new group, or can we use an existing one? */
7978
7979 if (stmt_list)
7980 {
7981 line_offset = DW_UNSND (stmt_list);
7982 ++tu_stats->nr_symtab_sharers;
7983 }
7984 else
7985 {
7986 /* Ugh, no stmt_list. Rare, but we have to handle it.
7987 We can do various things here like create one group per TU or
7988 spread them over multiple groups to split up the expansion work.
7989 To avoid worst case scenarios (too many groups or too large groups)
7990 we, umm, group them in bunches. */
7991 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7992 | (tu_stats->nr_stmt_less_type_units
7993 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7994 ++tu_stats->nr_stmt_less_type_units;
7995 }
7996
7997 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7998 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7999 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
8000 &type_unit_group_for_lookup, INSERT);
8001 if (*slot != NULL)
8002 {
8003 tu_group = (struct type_unit_group *) *slot;
8004 gdb_assert (tu_group != NULL);
8005 }
8006 else
8007 {
8008 sect_offset line_offset_struct = (sect_offset) line_offset;
8009 tu_group = create_type_unit_group (cu, line_offset_struct);
8010 *slot = tu_group;
8011 ++tu_stats->nr_symtabs;
8012 }
8013
8014 return tu_group;
8015 }
8016 \f
8017 /* Partial symbol tables. */
8018
8019 /* Create a psymtab named NAME and assign it to PER_CU.
8020
8021 The caller must fill in the following details:
8022 dirname, textlow, texthigh. */
8023
8024 static struct partial_symtab *
8025 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
8026 {
8027 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
8028 struct partial_symtab *pst;
8029
8030 pst = start_psymtab_common (objfile, name, 0);
8031
8032 pst->psymtabs_addrmap_supported = 1;
8033
8034 /* This is the glue that links PST into GDB's symbol API. */
8035 pst->read_symtab_private = per_cu;
8036 pst->read_symtab = dwarf2_read_symtab;
8037 per_cu->v.psymtab = pst;
8038
8039 return pst;
8040 }
8041
8042 /* DIE reader function for process_psymtab_comp_unit. */
8043
8044 static void
8045 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
8046 const gdb_byte *info_ptr,
8047 struct die_info *comp_unit_die,
8048 int has_children,
8049 int want_partial_unit,
8050 enum language pretend_language)
8051 {
8052 struct dwarf2_cu *cu = reader->cu;
8053 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8054 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8055 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8056 CORE_ADDR baseaddr;
8057 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8058 struct partial_symtab *pst;
8059 enum pc_bounds_kind cu_bounds_kind;
8060 const char *filename;
8061
8062 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
8063 return;
8064
8065 gdb_assert (! per_cu->is_debug_types);
8066
8067 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
8068
8069 /* Allocate a new partial symbol table structure. */
8070 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8071 if (filename == NULL)
8072 filename = "";
8073
8074 pst = create_partial_symtab (per_cu, filename);
8075
8076 /* This must be done before calling dwarf2_build_include_psymtabs. */
8077 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
8078
8079 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
8080
8081 dwarf2_find_base_address (comp_unit_die, cu);
8082
8083 /* Possibly set the default values of LOWPC and HIGHPC from
8084 `DW_AT_ranges'. */
8085 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8086 &best_highpc, cu, pst);
8087 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
8088 {
8089 CORE_ADDR low
8090 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8091 - baseaddr);
8092 CORE_ADDR high
8093 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8094 - baseaddr - 1);
8095 /* Store the contiguous range if it is not empty; it can be
8096 empty for CUs with no code. */
8097 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8098 low, high, pst);
8099 }
8100
8101 /* Check if comp unit has_children.
8102 If so, read the rest of the partial symbols from this comp unit.
8103 If not, there's no more debug_info for this comp unit. */
8104 if (has_children)
8105 {
8106 struct partial_die_info *first_die;
8107 CORE_ADDR lowpc, highpc;
8108
8109 lowpc = ((CORE_ADDR) -1);
8110 highpc = ((CORE_ADDR) 0);
8111
8112 first_die = load_partial_dies (reader, info_ptr, 1);
8113
8114 scan_partial_symbols (first_die, &lowpc, &highpc,
8115 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
8116
8117 /* If we didn't find a lowpc, set it to highpc to avoid
8118 complaints from `maint check'. */
8119 if (lowpc == ((CORE_ADDR) -1))
8120 lowpc = highpc;
8121
8122 /* If the compilation unit didn't have an explicit address range,
8123 then use the information extracted from its child dies. */
8124 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
8125 {
8126 best_lowpc = lowpc;
8127 best_highpc = highpc;
8128 }
8129 }
8130 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
8131 best_lowpc + baseaddr)
8132 - baseaddr);
8133 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
8134 best_highpc + baseaddr)
8135 - baseaddr);
8136
8137 end_psymtab_common (objfile, pst);
8138
8139 if (!cu->per_cu->imported_symtabs_empty ())
8140 {
8141 int i;
8142 int len = cu->per_cu->imported_symtabs_size ();
8143
8144 /* Fill in 'dependencies' here; we fill in 'users' in a
8145 post-pass. */
8146 pst->number_of_dependencies = len;
8147 pst->dependencies
8148 = objfile->partial_symtabs->allocate_dependencies (len);
8149 for (i = 0; i < len; ++i)
8150 {
8151 pst->dependencies[i]
8152 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8153 }
8154
8155 cu->per_cu->imported_symtabs_free ();
8156 }
8157
8158 /* Get the list of files included in the current compilation unit,
8159 and build a psymtab for each of them. */
8160 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8161
8162 if (dwarf_read_debug)
8163 fprintf_unfiltered (gdb_stdlog,
8164 "Psymtab for %s unit @%s: %s - %s"
8165 ", %d global, %d static syms\n",
8166 per_cu->is_debug_types ? "type" : "comp",
8167 sect_offset_str (per_cu->sect_off),
8168 paddress (gdbarch, pst->text_low (objfile)),
8169 paddress (gdbarch, pst->text_high (objfile)),
8170 pst->n_global_syms, pst->n_static_syms);
8171 }
8172
8173 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8174 Process compilation unit THIS_CU for a psymtab. */
8175
8176 static void
8177 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
8178 int want_partial_unit,
8179 enum language pretend_language)
8180 {
8181 /* If this compilation unit was already read in, free the
8182 cached copy in order to read it in again. This is
8183 necessary because we skipped some symbols when we first
8184 read in the compilation unit (see load_partial_dies).
8185 This problem could be avoided, but the benefit is unclear. */
8186 if (this_cu->cu != NULL)
8187 free_one_cached_comp_unit (this_cu);
8188
8189 cutu_reader reader (this_cu, NULL, 0, 0, false);
8190
8191 if (reader.dummy_p)
8192 {
8193 /* Nothing. */
8194 }
8195 else if (this_cu->is_debug_types)
8196 build_type_psymtabs_reader (&reader, reader.info_ptr, reader.comp_unit_die,
8197 reader.has_children);
8198 else
8199 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
8200 reader.comp_unit_die,
8201 reader.has_children,
8202 want_partial_unit,
8203 pretend_language);
8204
8205 /* Age out any secondary CUs. */
8206 age_cached_comp_units (this_cu->dwarf2_per_objfile);
8207 }
8208
8209 /* Reader function for build_type_psymtabs. */
8210
8211 static void
8212 build_type_psymtabs_reader (const struct die_reader_specs *reader,
8213 const gdb_byte *info_ptr,
8214 struct die_info *type_unit_die,
8215 int has_children)
8216 {
8217 struct dwarf2_per_objfile *dwarf2_per_objfile
8218 = reader->cu->per_cu->dwarf2_per_objfile;
8219 struct objfile *objfile = dwarf2_per_objfile->objfile;
8220 struct dwarf2_cu *cu = reader->cu;
8221 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8222 struct signatured_type *sig_type;
8223 struct type_unit_group *tu_group;
8224 struct attribute *attr;
8225 struct partial_die_info *first_die;
8226 CORE_ADDR lowpc, highpc;
8227 struct partial_symtab *pst;
8228
8229 gdb_assert (per_cu->is_debug_types);
8230 sig_type = (struct signatured_type *) per_cu;
8231
8232 if (! has_children)
8233 return;
8234
8235 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
8236 tu_group = get_type_unit_group (cu, attr);
8237
8238 if (tu_group->tus == nullptr)
8239 tu_group->tus = new std::vector<signatured_type *>;
8240 tu_group->tus->push_back (sig_type);
8241
8242 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8243 pst = create_partial_symtab (per_cu, "");
8244 pst->anonymous = 1;
8245
8246 first_die = load_partial_dies (reader, info_ptr, 1);
8247
8248 lowpc = (CORE_ADDR) -1;
8249 highpc = (CORE_ADDR) 0;
8250 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8251
8252 end_psymtab_common (objfile, pst);
8253 }
8254
8255 /* Struct used to sort TUs by their abbreviation table offset. */
8256
8257 struct tu_abbrev_offset
8258 {
8259 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8260 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8261 {}
8262
8263 signatured_type *sig_type;
8264 sect_offset abbrev_offset;
8265 };
8266
8267 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
8268
8269 static bool
8270 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8271 const struct tu_abbrev_offset &b)
8272 {
8273 return a.abbrev_offset < b.abbrev_offset;
8274 }
8275
8276 /* Efficiently read all the type units.
8277 This does the bulk of the work for build_type_psymtabs.
8278
8279 The efficiency is because we sort TUs by the abbrev table they use and
8280 only read each abbrev table once. In one program there are 200K TUs
8281 sharing 8K abbrev tables.
8282
8283 The main purpose of this function is to support building the
8284 dwarf2_per_objfile->type_unit_groups table.
8285 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8286 can collapse the search space by grouping them by stmt_list.
8287 The savings can be significant, in the same program from above the 200K TUs
8288 share 8K stmt_list tables.
8289
8290 FUNC is expected to call get_type_unit_group, which will create the
8291 struct type_unit_group if necessary and add it to
8292 dwarf2_per_objfile->type_unit_groups. */
8293
8294 static void
8295 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
8296 {
8297 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8298 abbrev_table_up abbrev_table;
8299 sect_offset abbrev_offset;
8300
8301 /* It's up to the caller to not call us multiple times. */
8302 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8303
8304 if (dwarf2_per_objfile->all_type_units.empty ())
8305 return;
8306
8307 /* TUs typically share abbrev tables, and there can be way more TUs than
8308 abbrev tables. Sort by abbrev table to reduce the number of times we
8309 read each abbrev table in.
8310 Alternatives are to punt or to maintain a cache of abbrev tables.
8311 This is simpler and efficient enough for now.
8312
8313 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8314 symtab to use). Typically TUs with the same abbrev offset have the same
8315 stmt_list value too so in practice this should work well.
8316
8317 The basic algorithm here is:
8318
8319 sort TUs by abbrev table
8320 for each TU with same abbrev table:
8321 read abbrev table if first user
8322 read TU top level DIE
8323 [IWBN if DWO skeletons had DW_AT_stmt_list]
8324 call FUNC */
8325
8326 if (dwarf_read_debug)
8327 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8328
8329 /* Sort in a separate table to maintain the order of all_type_units
8330 for .gdb_index: TU indices directly index all_type_units. */
8331 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8332 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8333
8334 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8335 sorted_by_abbrev.emplace_back
8336 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8337 sig_type->per_cu.section,
8338 sig_type->per_cu.sect_off));
8339
8340 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8341 sort_tu_by_abbrev_offset);
8342
8343 abbrev_offset = (sect_offset) ~(unsigned) 0;
8344
8345 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
8346 {
8347 /* Switch to the next abbrev table if necessary. */
8348 if (abbrev_table == NULL
8349 || tu.abbrev_offset != abbrev_offset)
8350 {
8351 abbrev_offset = tu.abbrev_offset;
8352 abbrev_table =
8353 abbrev_table_read_table (dwarf2_per_objfile,
8354 &dwarf2_per_objfile->abbrev,
8355 abbrev_offset);
8356 ++tu_stats->nr_uniq_abbrev_tables;
8357 }
8358
8359 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
8360 0, 0, false);
8361 if (!reader.dummy_p)
8362 build_type_psymtabs_reader (&reader, reader.info_ptr,
8363 reader.comp_unit_die,
8364 reader.has_children);
8365 }
8366 }
8367
8368 /* Print collected type unit statistics. */
8369
8370 static void
8371 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
8372 {
8373 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8374
8375 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
8376 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8377 dwarf2_per_objfile->all_type_units.size ());
8378 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8379 tu_stats->nr_uniq_abbrev_tables);
8380 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8381 tu_stats->nr_symtabs);
8382 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8383 tu_stats->nr_symtab_sharers);
8384 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8385 tu_stats->nr_stmt_less_type_units);
8386 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8387 tu_stats->nr_all_type_units_reallocs);
8388 }
8389
8390 /* Traversal function for build_type_psymtabs. */
8391
8392 static int
8393 build_type_psymtab_dependencies (void **slot, void *info)
8394 {
8395 struct dwarf2_per_objfile *dwarf2_per_objfile
8396 = (struct dwarf2_per_objfile *) info;
8397 struct objfile *objfile = dwarf2_per_objfile->objfile;
8398 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8399 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8400 struct partial_symtab *pst = per_cu->v.psymtab;
8401 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8402 int i;
8403
8404 gdb_assert (len > 0);
8405 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
8406
8407 pst->number_of_dependencies = len;
8408 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8409 for (i = 0; i < len; ++i)
8410 {
8411 struct signatured_type *iter = tu_group->tus->at (i);
8412 gdb_assert (iter->per_cu.is_debug_types);
8413 pst->dependencies[i] = iter->per_cu.v.psymtab;
8414 iter->type_unit_group = tu_group;
8415 }
8416
8417 delete tu_group->tus;
8418 tu_group->tus = nullptr;
8419
8420 return 1;
8421 }
8422
8423 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8424 Build partial symbol tables for the .debug_types comp-units. */
8425
8426 static void
8427 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
8428 {
8429 if (! create_all_type_units (dwarf2_per_objfile))
8430 return;
8431
8432 build_type_psymtabs_1 (dwarf2_per_objfile);
8433 }
8434
8435 /* Traversal function for process_skeletonless_type_unit.
8436 Read a TU in a DWO file and build partial symbols for it. */
8437
8438 static int
8439 process_skeletonless_type_unit (void **slot, void *info)
8440 {
8441 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8442 struct dwarf2_per_objfile *dwarf2_per_objfile
8443 = (struct dwarf2_per_objfile *) info;
8444 struct signatured_type find_entry, *entry;
8445
8446 /* If this TU doesn't exist in the global table, add it and read it in. */
8447
8448 if (dwarf2_per_objfile->signatured_types == NULL)
8449 {
8450 dwarf2_per_objfile->signatured_types
8451 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
8452 }
8453
8454 find_entry.signature = dwo_unit->signature;
8455 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8456 INSERT);
8457 /* If we've already seen this type there's nothing to do. What's happening
8458 is we're doing our own version of comdat-folding here. */
8459 if (*slot != NULL)
8460 return 1;
8461
8462 /* This does the job that create_all_type_units would have done for
8463 this TU. */
8464 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8465 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
8466 *slot = entry;
8467
8468 /* This does the job that build_type_psymtabs_1 would have done. */
8469 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
8470 if (!reader.dummy_p)
8471 build_type_psymtabs_reader (&reader, reader.info_ptr,
8472 reader.comp_unit_die, reader.has_children);
8473
8474 return 1;
8475 }
8476
8477 /* Traversal function for process_skeletonless_type_units. */
8478
8479 static int
8480 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8481 {
8482 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8483
8484 if (dwo_file->tus != NULL)
8485 {
8486 htab_traverse_noresize (dwo_file->tus,
8487 process_skeletonless_type_unit, info);
8488 }
8489
8490 return 1;
8491 }
8492
8493 /* Scan all TUs of DWO files, verifying we've processed them.
8494 This is needed in case a TU was emitted without its skeleton.
8495 Note: This can't be done until we know what all the DWO files are. */
8496
8497 static void
8498 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8499 {
8500 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8501 if (get_dwp_file (dwarf2_per_objfile) == NULL
8502 && dwarf2_per_objfile->dwo_files != NULL)
8503 {
8504 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
8505 process_dwo_file_for_skeletonless_type_units,
8506 dwarf2_per_objfile);
8507 }
8508 }
8509
8510 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8511
8512 static void
8513 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
8514 {
8515 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8516 {
8517 struct partial_symtab *pst = per_cu->v.psymtab;
8518
8519 if (pst == NULL)
8520 continue;
8521
8522 for (int j = 0; j < pst->number_of_dependencies; ++j)
8523 {
8524 /* Set the 'user' field only if it is not already set. */
8525 if (pst->dependencies[j]->user == NULL)
8526 pst->dependencies[j]->user = pst;
8527 }
8528 }
8529 }
8530
8531 /* Build the partial symbol table by doing a quick pass through the
8532 .debug_info and .debug_abbrev sections. */
8533
8534 static void
8535 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
8536 {
8537 struct objfile *objfile = dwarf2_per_objfile->objfile;
8538
8539 if (dwarf_read_debug)
8540 {
8541 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
8542 objfile_name (objfile));
8543 }
8544
8545 dwarf2_per_objfile->reading_partial_symbols = 1;
8546
8547 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
8548
8549 /* Any cached compilation units will be linked by the per-objfile
8550 read_in_chain. Make sure to free them when we're done. */
8551 free_cached_comp_units freer (dwarf2_per_objfile);
8552
8553 build_type_psymtabs (dwarf2_per_objfile);
8554
8555 create_all_comp_units (dwarf2_per_objfile);
8556
8557 /* Create a temporary address map on a temporary obstack. We later
8558 copy this to the final obstack. */
8559 auto_obstack temp_obstack;
8560
8561 scoped_restore save_psymtabs_addrmap
8562 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8563 addrmap_create_mutable (&temp_obstack));
8564
8565 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8566 process_psymtab_comp_unit (per_cu, 0, language_minimal);
8567
8568 /* This has to wait until we read the CUs, we need the list of DWOs. */
8569 process_skeletonless_type_units (dwarf2_per_objfile);
8570
8571 /* Now that all TUs have been processed we can fill in the dependencies. */
8572 if (dwarf2_per_objfile->type_unit_groups != NULL)
8573 {
8574 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
8575 build_type_psymtab_dependencies, dwarf2_per_objfile);
8576 }
8577
8578 if (dwarf_read_debug)
8579 print_tu_stats (dwarf2_per_objfile);
8580
8581 set_partial_user (dwarf2_per_objfile);
8582
8583 objfile->partial_symtabs->psymtabs_addrmap
8584 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8585 objfile->partial_symtabs->obstack ());
8586 /* At this point we want to keep the address map. */
8587 save_psymtabs_addrmap.release ();
8588
8589 if (dwarf_read_debug)
8590 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
8591 objfile_name (objfile));
8592 }
8593
8594 /* Load the partial DIEs for a secondary CU into memory.
8595 This is also used when rereading a primary CU with load_all_dies. */
8596
8597 static void
8598 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8599 {
8600 cutu_reader reader (this_cu, NULL, 1, 1, false);
8601
8602 if (!reader.dummy_p)
8603 {
8604 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8605 language_minimal);
8606
8607 /* Check if comp unit has_children.
8608 If so, read the rest of the partial symbols from this comp unit.
8609 If not, there's no more debug_info for this comp unit. */
8610 if (reader.has_children)
8611 load_partial_dies (&reader, reader.info_ptr, 0);
8612 }
8613 }
8614
8615 static void
8616 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8617 struct dwarf2_section_info *section,
8618 struct dwarf2_section_info *abbrev_section,
8619 unsigned int is_dwz)
8620 {
8621 const gdb_byte *info_ptr;
8622 struct objfile *objfile = dwarf2_per_objfile->objfile;
8623
8624 if (dwarf_read_debug)
8625 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8626 get_section_name (section),
8627 get_section_file_name (section));
8628
8629 dwarf2_read_section (objfile, section);
8630
8631 info_ptr = section->buffer;
8632
8633 while (info_ptr < section->buffer + section->size)
8634 {
8635 struct dwarf2_per_cu_data *this_cu;
8636
8637 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8638
8639 comp_unit_head cu_header;
8640 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8641 abbrev_section, info_ptr,
8642 rcuh_kind::COMPILE);
8643
8644 /* Save the compilation unit for later lookup. */
8645 if (cu_header.unit_type != DW_UT_type)
8646 {
8647 this_cu = XOBNEW (&objfile->objfile_obstack,
8648 struct dwarf2_per_cu_data);
8649 memset (this_cu, 0, sizeof (*this_cu));
8650 }
8651 else
8652 {
8653 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8654 struct signatured_type);
8655 memset (sig_type, 0, sizeof (*sig_type));
8656 sig_type->signature = cu_header.signature;
8657 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8658 this_cu = &sig_type->per_cu;
8659 }
8660 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8661 this_cu->sect_off = sect_off;
8662 this_cu->length = cu_header.length + cu_header.initial_length_size;
8663 this_cu->is_dwz = is_dwz;
8664 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8665 this_cu->section = section;
8666
8667 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
8668
8669 info_ptr = info_ptr + this_cu->length;
8670 }
8671 }
8672
8673 /* Create a list of all compilation units in OBJFILE.
8674 This is only done for -readnow and building partial symtabs. */
8675
8676 static void
8677 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8678 {
8679 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
8680 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
8681 &dwarf2_per_objfile->abbrev, 0);
8682
8683 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8684 if (dwz != NULL)
8685 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8686 1);
8687 }
8688
8689 /* Process all loaded DIEs for compilation unit CU, starting at
8690 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8691 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8692 DW_AT_ranges). See the comments of add_partial_subprogram on how
8693 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8694
8695 static void
8696 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8697 CORE_ADDR *highpc, int set_addrmap,
8698 struct dwarf2_cu *cu)
8699 {
8700 struct partial_die_info *pdi;
8701
8702 /* Now, march along the PDI's, descending into ones which have
8703 interesting children but skipping the children of the other ones,
8704 until we reach the end of the compilation unit. */
8705
8706 pdi = first_die;
8707
8708 while (pdi != NULL)
8709 {
8710 pdi->fixup (cu);
8711
8712 /* Anonymous namespaces or modules have no name but have interesting
8713 children, so we need to look at them. Ditto for anonymous
8714 enums. */
8715
8716 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8717 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8718 || pdi->tag == DW_TAG_imported_unit
8719 || pdi->tag == DW_TAG_inlined_subroutine)
8720 {
8721 switch (pdi->tag)
8722 {
8723 case DW_TAG_subprogram:
8724 case DW_TAG_inlined_subroutine:
8725 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8726 break;
8727 case DW_TAG_constant:
8728 case DW_TAG_variable:
8729 case DW_TAG_typedef:
8730 case DW_TAG_union_type:
8731 if (!pdi->is_declaration)
8732 {
8733 add_partial_symbol (pdi, cu);
8734 }
8735 break;
8736 case DW_TAG_class_type:
8737 case DW_TAG_interface_type:
8738 case DW_TAG_structure_type:
8739 if (!pdi->is_declaration)
8740 {
8741 add_partial_symbol (pdi, cu);
8742 }
8743 if ((cu->language == language_rust
8744 || cu->language == language_cplus) && pdi->has_children)
8745 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8746 set_addrmap, cu);
8747 break;
8748 case DW_TAG_enumeration_type:
8749 if (!pdi->is_declaration)
8750 add_partial_enumeration (pdi, cu);
8751 break;
8752 case DW_TAG_base_type:
8753 case DW_TAG_subrange_type:
8754 /* File scope base type definitions are added to the partial
8755 symbol table. */
8756 add_partial_symbol (pdi, cu);
8757 break;
8758 case DW_TAG_namespace:
8759 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8760 break;
8761 case DW_TAG_module:
8762 if (!pdi->is_declaration)
8763 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8764 break;
8765 case DW_TAG_imported_unit:
8766 {
8767 struct dwarf2_per_cu_data *per_cu;
8768
8769 /* For now we don't handle imported units in type units. */
8770 if (cu->per_cu->is_debug_types)
8771 {
8772 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8773 " supported in type units [in module %s]"),
8774 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
8775 }
8776
8777 per_cu = dwarf2_find_containing_comp_unit
8778 (pdi->d.sect_off, pdi->is_dwz,
8779 cu->per_cu->dwarf2_per_objfile);
8780
8781 /* Go read the partial unit, if needed. */
8782 if (per_cu->v.psymtab == NULL)
8783 process_psymtab_comp_unit (per_cu, 1, cu->language);
8784
8785 cu->per_cu->imported_symtabs_push (per_cu);
8786 }
8787 break;
8788 case DW_TAG_imported_declaration:
8789 add_partial_symbol (pdi, cu);
8790 break;
8791 default:
8792 break;
8793 }
8794 }
8795
8796 /* If the die has a sibling, skip to the sibling. */
8797
8798 pdi = pdi->die_sibling;
8799 }
8800 }
8801
8802 /* Functions used to compute the fully scoped name of a partial DIE.
8803
8804 Normally, this is simple. For C++, the parent DIE's fully scoped
8805 name is concatenated with "::" and the partial DIE's name.
8806 Enumerators are an exception; they use the scope of their parent
8807 enumeration type, i.e. the name of the enumeration type is not
8808 prepended to the enumerator.
8809
8810 There are two complexities. One is DW_AT_specification; in this
8811 case "parent" means the parent of the target of the specification,
8812 instead of the direct parent of the DIE. The other is compilers
8813 which do not emit DW_TAG_namespace; in this case we try to guess
8814 the fully qualified name of structure types from their members'
8815 linkage names. This must be done using the DIE's children rather
8816 than the children of any DW_AT_specification target. We only need
8817 to do this for structures at the top level, i.e. if the target of
8818 any DW_AT_specification (if any; otherwise the DIE itself) does not
8819 have a parent. */
8820
8821 /* Compute the scope prefix associated with PDI's parent, in
8822 compilation unit CU. The result will be allocated on CU's
8823 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8824 field. NULL is returned if no prefix is necessary. */
8825 static const char *
8826 partial_die_parent_scope (struct partial_die_info *pdi,
8827 struct dwarf2_cu *cu)
8828 {
8829 const char *grandparent_scope;
8830 struct partial_die_info *parent, *real_pdi;
8831
8832 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8833 then this means the parent of the specification DIE. */
8834
8835 real_pdi = pdi;
8836 while (real_pdi->has_specification)
8837 {
8838 auto res = find_partial_die (real_pdi->spec_offset,
8839 real_pdi->spec_is_dwz, cu);
8840 real_pdi = res.pdi;
8841 cu = res.cu;
8842 }
8843
8844 parent = real_pdi->die_parent;
8845 if (parent == NULL)
8846 return NULL;
8847
8848 if (parent->scope_set)
8849 return parent->scope;
8850
8851 parent->fixup (cu);
8852
8853 grandparent_scope = partial_die_parent_scope (parent, cu);
8854
8855 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8856 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8857 Work around this problem here. */
8858 if (cu->language == language_cplus
8859 && parent->tag == DW_TAG_namespace
8860 && strcmp (parent->name, "::") == 0
8861 && grandparent_scope == NULL)
8862 {
8863 parent->scope = NULL;
8864 parent->scope_set = 1;
8865 return NULL;
8866 }
8867
8868 /* Nested subroutines in Fortran get a prefix. */
8869 if (pdi->tag == DW_TAG_enumerator)
8870 /* Enumerators should not get the name of the enumeration as a prefix. */
8871 parent->scope = grandparent_scope;
8872 else if (parent->tag == DW_TAG_namespace
8873 || parent->tag == DW_TAG_module
8874 || parent->tag == DW_TAG_structure_type
8875 || parent->tag == DW_TAG_class_type
8876 || parent->tag == DW_TAG_interface_type
8877 || parent->tag == DW_TAG_union_type
8878 || parent->tag == DW_TAG_enumeration_type
8879 || (cu->language == language_fortran
8880 && parent->tag == DW_TAG_subprogram
8881 && pdi->tag == DW_TAG_subprogram))
8882 {
8883 if (grandparent_scope == NULL)
8884 parent->scope = parent->name;
8885 else
8886 parent->scope = typename_concat (&cu->comp_unit_obstack,
8887 grandparent_scope,
8888 parent->name, 0, cu);
8889 }
8890 else
8891 {
8892 /* FIXME drow/2004-04-01: What should we be doing with
8893 function-local names? For partial symbols, we should probably be
8894 ignoring them. */
8895 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8896 dwarf_tag_name (parent->tag),
8897 sect_offset_str (pdi->sect_off));
8898 parent->scope = grandparent_scope;
8899 }
8900
8901 parent->scope_set = 1;
8902 return parent->scope;
8903 }
8904
8905 /* Return the fully scoped name associated with PDI, from compilation unit
8906 CU. The result will be allocated with malloc. */
8907
8908 static gdb::unique_xmalloc_ptr<char>
8909 partial_die_full_name (struct partial_die_info *pdi,
8910 struct dwarf2_cu *cu)
8911 {
8912 const char *parent_scope;
8913
8914 /* If this is a template instantiation, we can not work out the
8915 template arguments from partial DIEs. So, unfortunately, we have
8916 to go through the full DIEs. At least any work we do building
8917 types here will be reused if full symbols are loaded later. */
8918 if (pdi->has_template_arguments)
8919 {
8920 pdi->fixup (cu);
8921
8922 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8923 {
8924 struct die_info *die;
8925 struct attribute attr;
8926 struct dwarf2_cu *ref_cu = cu;
8927
8928 /* DW_FORM_ref_addr is using section offset. */
8929 attr.name = (enum dwarf_attribute) 0;
8930 attr.form = DW_FORM_ref_addr;
8931 attr.u.unsnd = to_underlying (pdi->sect_off);
8932 die = follow_die_ref (NULL, &attr, &ref_cu);
8933
8934 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8935 }
8936 }
8937
8938 parent_scope = partial_die_parent_scope (pdi, cu);
8939 if (parent_scope == NULL)
8940 return NULL;
8941 else
8942 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8943 pdi->name, 0, cu));
8944 }
8945
8946 static void
8947 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8948 {
8949 struct dwarf2_per_objfile *dwarf2_per_objfile
8950 = cu->per_cu->dwarf2_per_objfile;
8951 struct objfile *objfile = dwarf2_per_objfile->objfile;
8952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8953 CORE_ADDR addr = 0;
8954 const char *actual_name = NULL;
8955 CORE_ADDR baseaddr;
8956
8957 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
8958
8959 gdb::unique_xmalloc_ptr<char> built_actual_name
8960 = partial_die_full_name (pdi, cu);
8961 if (built_actual_name != NULL)
8962 actual_name = built_actual_name.get ();
8963
8964 if (actual_name == NULL)
8965 actual_name = pdi->name;
8966
8967 switch (pdi->tag)
8968 {
8969 case DW_TAG_inlined_subroutine:
8970 case DW_TAG_subprogram:
8971 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8972 - baseaddr);
8973 if (pdi->is_external
8974 || cu->language == language_ada
8975 || (cu->language == language_fortran
8976 && pdi->die_parent != NULL
8977 && pdi->die_parent->tag == DW_TAG_subprogram))
8978 {
8979 /* Normally, only "external" DIEs are part of the global scope.
8980 But in Ada and Fortran, we want to be able to access nested
8981 procedures globally. So all Ada and Fortran subprograms are
8982 stored in the global scope. */
8983 add_psymbol_to_list (actual_name,
8984 built_actual_name != NULL,
8985 VAR_DOMAIN, LOC_BLOCK,
8986 SECT_OFF_TEXT (objfile),
8987 psymbol_placement::GLOBAL,
8988 addr,
8989 cu->language, objfile);
8990 }
8991 else
8992 {
8993 add_psymbol_to_list (actual_name,
8994 built_actual_name != NULL,
8995 VAR_DOMAIN, LOC_BLOCK,
8996 SECT_OFF_TEXT (objfile),
8997 psymbol_placement::STATIC,
8998 addr, cu->language, objfile);
8999 }
9000
9001 if (pdi->main_subprogram && actual_name != NULL)
9002 set_objfile_main_name (objfile, actual_name, cu->language);
9003 break;
9004 case DW_TAG_constant:
9005 add_psymbol_to_list (actual_name,
9006 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
9007 -1, (pdi->is_external
9008 ? psymbol_placement::GLOBAL
9009 : psymbol_placement::STATIC),
9010 0, cu->language, objfile);
9011 break;
9012 case DW_TAG_variable:
9013 if (pdi->d.locdesc)
9014 addr = decode_locdesc (pdi->d.locdesc, cu);
9015
9016 if (pdi->d.locdesc
9017 && addr == 0
9018 && !dwarf2_per_objfile->has_section_at_zero)
9019 {
9020 /* A global or static variable may also have been stripped
9021 out by the linker if unused, in which case its address
9022 will be nullified; do not add such variables into partial
9023 symbol table then. */
9024 }
9025 else if (pdi->is_external)
9026 {
9027 /* Global Variable.
9028 Don't enter into the minimal symbol tables as there is
9029 a minimal symbol table entry from the ELF symbols already.
9030 Enter into partial symbol table if it has a location
9031 descriptor or a type.
9032 If the location descriptor is missing, new_symbol will create
9033 a LOC_UNRESOLVED symbol, the address of the variable will then
9034 be determined from the minimal symbol table whenever the variable
9035 is referenced.
9036 The address for the partial symbol table entry is not
9037 used by GDB, but it comes in handy for debugging partial symbol
9038 table building. */
9039
9040 if (pdi->d.locdesc || pdi->has_type)
9041 add_psymbol_to_list (actual_name,
9042 built_actual_name != NULL,
9043 VAR_DOMAIN, LOC_STATIC,
9044 SECT_OFF_TEXT (objfile),
9045 psymbol_placement::GLOBAL,
9046 addr, cu->language, objfile);
9047 }
9048 else
9049 {
9050 int has_loc = pdi->d.locdesc != NULL;
9051
9052 /* Static Variable. Skip symbols whose value we cannot know (those
9053 without location descriptors or constant values). */
9054 if (!has_loc && !pdi->has_const_value)
9055 return;
9056
9057 add_psymbol_to_list (actual_name,
9058 built_actual_name != NULL,
9059 VAR_DOMAIN, LOC_STATIC,
9060 SECT_OFF_TEXT (objfile),
9061 psymbol_placement::STATIC,
9062 has_loc ? addr : 0,
9063 cu->language, objfile);
9064 }
9065 break;
9066 case DW_TAG_typedef:
9067 case DW_TAG_base_type:
9068 case DW_TAG_subrange_type:
9069 add_psymbol_to_list (actual_name,
9070 built_actual_name != NULL,
9071 VAR_DOMAIN, LOC_TYPEDEF, -1,
9072 psymbol_placement::STATIC,
9073 0, cu->language, objfile);
9074 break;
9075 case DW_TAG_imported_declaration:
9076 case DW_TAG_namespace:
9077 add_psymbol_to_list (actual_name,
9078 built_actual_name != NULL,
9079 VAR_DOMAIN, LOC_TYPEDEF, -1,
9080 psymbol_placement::GLOBAL,
9081 0, cu->language, objfile);
9082 break;
9083 case DW_TAG_module:
9084 /* With Fortran 77 there might be a "BLOCK DATA" module
9085 available without any name. If so, we skip the module as it
9086 doesn't bring any value. */
9087 if (actual_name != nullptr)
9088 add_psymbol_to_list (actual_name,
9089 built_actual_name != NULL,
9090 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9091 psymbol_placement::GLOBAL,
9092 0, cu->language, objfile);
9093 break;
9094 case DW_TAG_class_type:
9095 case DW_TAG_interface_type:
9096 case DW_TAG_structure_type:
9097 case DW_TAG_union_type:
9098 case DW_TAG_enumeration_type:
9099 /* Skip external references. The DWARF standard says in the section
9100 about "Structure, Union, and Class Type Entries": "An incomplete
9101 structure, union or class type is represented by a structure,
9102 union or class entry that does not have a byte size attribute
9103 and that has a DW_AT_declaration attribute." */
9104 if (!pdi->has_byte_size && pdi->is_declaration)
9105 return;
9106
9107 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9108 static vs. global. */
9109 add_psymbol_to_list (actual_name,
9110 built_actual_name != NULL,
9111 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9112 cu->language == language_cplus
9113 ? psymbol_placement::GLOBAL
9114 : psymbol_placement::STATIC,
9115 0, cu->language, objfile);
9116
9117 break;
9118 case DW_TAG_enumerator:
9119 add_psymbol_to_list (actual_name,
9120 built_actual_name != NULL,
9121 VAR_DOMAIN, LOC_CONST, -1,
9122 cu->language == language_cplus
9123 ? psymbol_placement::GLOBAL
9124 : psymbol_placement::STATIC,
9125 0, cu->language, objfile);
9126 break;
9127 default:
9128 break;
9129 }
9130 }
9131
9132 /* Read a partial die corresponding to a namespace; also, add a symbol
9133 corresponding to that namespace to the symbol table. NAMESPACE is
9134 the name of the enclosing namespace. */
9135
9136 static void
9137 add_partial_namespace (struct partial_die_info *pdi,
9138 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9139 int set_addrmap, struct dwarf2_cu *cu)
9140 {
9141 /* Add a symbol for the namespace. */
9142
9143 add_partial_symbol (pdi, cu);
9144
9145 /* Now scan partial symbols in that namespace. */
9146
9147 if (pdi->has_children)
9148 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9149 }
9150
9151 /* Read a partial die corresponding to a Fortran module. */
9152
9153 static void
9154 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
9155 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
9156 {
9157 /* Add a symbol for the namespace. */
9158
9159 add_partial_symbol (pdi, cu);
9160
9161 /* Now scan partial symbols in that module. */
9162
9163 if (pdi->has_children)
9164 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
9165 }
9166
9167 /* Read a partial die corresponding to a subprogram or an inlined
9168 subprogram and create a partial symbol for that subprogram.
9169 When the CU language allows it, this routine also defines a partial
9170 symbol for each nested subprogram that this subprogram contains.
9171 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9172 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
9173
9174 PDI may also be a lexical block, in which case we simply search
9175 recursively for subprograms defined inside that lexical block.
9176 Again, this is only performed when the CU language allows this
9177 type of definitions. */
9178
9179 static void
9180 add_partial_subprogram (struct partial_die_info *pdi,
9181 CORE_ADDR *lowpc, CORE_ADDR *highpc,
9182 int set_addrmap, struct dwarf2_cu *cu)
9183 {
9184 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
9185 {
9186 if (pdi->has_pc_info)
9187 {
9188 if (pdi->lowpc < *lowpc)
9189 *lowpc = pdi->lowpc;
9190 if (pdi->highpc > *highpc)
9191 *highpc = pdi->highpc;
9192 if (set_addrmap)
9193 {
9194 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9195 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9196 CORE_ADDR baseaddr;
9197 CORE_ADDR this_highpc;
9198 CORE_ADDR this_lowpc;
9199
9200 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
9201 this_lowpc
9202 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9203 pdi->lowpc + baseaddr)
9204 - baseaddr);
9205 this_highpc
9206 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9207 pdi->highpc + baseaddr)
9208 - baseaddr);
9209 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
9210 this_lowpc, this_highpc - 1,
9211 cu->per_cu->v.psymtab);
9212 }
9213 }
9214
9215 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9216 {
9217 if (!pdi->is_declaration)
9218 /* Ignore subprogram DIEs that do not have a name, they are
9219 illegal. Do not emit a complaint at this point, we will
9220 do so when we convert this psymtab into a symtab. */
9221 if (pdi->name)
9222 add_partial_symbol (pdi, cu);
9223 }
9224 }
9225
9226 if (! pdi->has_children)
9227 return;
9228
9229 if (cu->language == language_ada || cu->language == language_fortran)
9230 {
9231 pdi = pdi->die_child;
9232 while (pdi != NULL)
9233 {
9234 pdi->fixup (cu);
9235 if (pdi->tag == DW_TAG_subprogram
9236 || pdi->tag == DW_TAG_inlined_subroutine
9237 || pdi->tag == DW_TAG_lexical_block)
9238 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
9239 pdi = pdi->die_sibling;
9240 }
9241 }
9242 }
9243
9244 /* Read a partial die corresponding to an enumeration type. */
9245
9246 static void
9247 add_partial_enumeration (struct partial_die_info *enum_pdi,
9248 struct dwarf2_cu *cu)
9249 {
9250 struct partial_die_info *pdi;
9251
9252 if (enum_pdi->name != NULL)
9253 add_partial_symbol (enum_pdi, cu);
9254
9255 pdi = enum_pdi->die_child;
9256 while (pdi)
9257 {
9258 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
9259 complaint (_("malformed enumerator DIE ignored"));
9260 else
9261 add_partial_symbol (pdi, cu);
9262 pdi = pdi->die_sibling;
9263 }
9264 }
9265
9266 /* Return the initial uleb128 in the die at INFO_PTR. */
9267
9268 static unsigned int
9269 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
9270 {
9271 unsigned int bytes_read;
9272
9273 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9274 }
9275
9276 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9277 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9278
9279 Return the corresponding abbrev, or NULL if the number is zero (indicating
9280 an empty DIE). In either case *BYTES_READ will be set to the length of
9281 the initial number. */
9282
9283 static struct abbrev_info *
9284 peek_die_abbrev (const die_reader_specs &reader,
9285 const gdb_byte *info_ptr, unsigned int *bytes_read)
9286 {
9287 dwarf2_cu *cu = reader.cu;
9288 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
9289 unsigned int abbrev_number
9290 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
9291
9292 if (abbrev_number == 0)
9293 return NULL;
9294
9295 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
9296 if (!abbrev)
9297 {
9298 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9299 " at offset %s [in module %s]"),
9300 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9301 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
9302 }
9303
9304 return abbrev;
9305 }
9306
9307 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9308 Returns a pointer to the end of a series of DIEs, terminated by an empty
9309 DIE. Any children of the skipped DIEs will also be skipped. */
9310
9311 static const gdb_byte *
9312 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
9313 {
9314 while (1)
9315 {
9316 unsigned int bytes_read;
9317 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9318
9319 if (abbrev == NULL)
9320 return info_ptr + bytes_read;
9321 else
9322 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
9323 }
9324 }
9325
9326 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9327 INFO_PTR should point just after the initial uleb128 of a DIE, and the
9328 abbrev corresponding to that skipped uleb128 should be passed in
9329 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9330 children. */
9331
9332 static const gdb_byte *
9333 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
9334 struct abbrev_info *abbrev)
9335 {
9336 unsigned int bytes_read;
9337 struct attribute attr;
9338 bfd *abfd = reader->abfd;
9339 struct dwarf2_cu *cu = reader->cu;
9340 const gdb_byte *buffer = reader->buffer;
9341 const gdb_byte *buffer_end = reader->buffer_end;
9342 unsigned int form, i;
9343
9344 for (i = 0; i < abbrev->num_attrs; i++)
9345 {
9346 /* The only abbrev we care about is DW_AT_sibling. */
9347 if (abbrev->attrs[i].name == DW_AT_sibling)
9348 {
9349 bool ignored;
9350 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
9351 &ignored);
9352 if (attr.form == DW_FORM_ref_addr)
9353 complaint (_("ignoring absolute DW_AT_sibling"));
9354 else
9355 {
9356 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9357 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9358
9359 if (sibling_ptr < info_ptr)
9360 complaint (_("DW_AT_sibling points backwards"));
9361 else if (sibling_ptr > reader->buffer_end)
9362 dwarf2_section_buffer_overflow_complaint (reader->die_section);
9363 else
9364 return sibling_ptr;
9365 }
9366 }
9367
9368 /* If it isn't DW_AT_sibling, skip this attribute. */
9369 form = abbrev->attrs[i].form;
9370 skip_attribute:
9371 switch (form)
9372 {
9373 case DW_FORM_ref_addr:
9374 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9375 and later it is offset sized. */
9376 if (cu->header.version == 2)
9377 info_ptr += cu->header.addr_size;
9378 else
9379 info_ptr += cu->header.offset_size;
9380 break;
9381 case DW_FORM_GNU_ref_alt:
9382 info_ptr += cu->header.offset_size;
9383 break;
9384 case DW_FORM_addr:
9385 info_ptr += cu->header.addr_size;
9386 break;
9387 case DW_FORM_data1:
9388 case DW_FORM_ref1:
9389 case DW_FORM_flag:
9390 case DW_FORM_strx1:
9391 info_ptr += 1;
9392 break;
9393 case DW_FORM_flag_present:
9394 case DW_FORM_implicit_const:
9395 break;
9396 case DW_FORM_data2:
9397 case DW_FORM_ref2:
9398 case DW_FORM_strx2:
9399 info_ptr += 2;
9400 break;
9401 case DW_FORM_strx3:
9402 info_ptr += 3;
9403 break;
9404 case DW_FORM_data4:
9405 case DW_FORM_ref4:
9406 case DW_FORM_strx4:
9407 info_ptr += 4;
9408 break;
9409 case DW_FORM_data8:
9410 case DW_FORM_ref8:
9411 case DW_FORM_ref_sig8:
9412 info_ptr += 8;
9413 break;
9414 case DW_FORM_data16:
9415 info_ptr += 16;
9416 break;
9417 case DW_FORM_string:
9418 read_direct_string (abfd, info_ptr, &bytes_read);
9419 info_ptr += bytes_read;
9420 break;
9421 case DW_FORM_sec_offset:
9422 case DW_FORM_strp:
9423 case DW_FORM_GNU_strp_alt:
9424 info_ptr += cu->header.offset_size;
9425 break;
9426 case DW_FORM_exprloc:
9427 case DW_FORM_block:
9428 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9429 info_ptr += bytes_read;
9430 break;
9431 case DW_FORM_block1:
9432 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9433 break;
9434 case DW_FORM_block2:
9435 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9436 break;
9437 case DW_FORM_block4:
9438 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9439 break;
9440 case DW_FORM_addrx:
9441 case DW_FORM_strx:
9442 case DW_FORM_sdata:
9443 case DW_FORM_udata:
9444 case DW_FORM_ref_udata:
9445 case DW_FORM_GNU_addr_index:
9446 case DW_FORM_GNU_str_index:
9447 case DW_FORM_rnglistx:
9448 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9449 break;
9450 case DW_FORM_indirect:
9451 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9452 info_ptr += bytes_read;
9453 /* We need to continue parsing from here, so just go back to
9454 the top. */
9455 goto skip_attribute;
9456
9457 default:
9458 error (_("Dwarf Error: Cannot handle %s "
9459 "in DWARF reader [in module %s]"),
9460 dwarf_form_name (form),
9461 bfd_get_filename (abfd));
9462 }
9463 }
9464
9465 if (abbrev->has_children)
9466 return skip_children (reader, info_ptr);
9467 else
9468 return info_ptr;
9469 }
9470
9471 /* Locate ORIG_PDI's sibling.
9472 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9473
9474 static const gdb_byte *
9475 locate_pdi_sibling (const struct die_reader_specs *reader,
9476 struct partial_die_info *orig_pdi,
9477 const gdb_byte *info_ptr)
9478 {
9479 /* Do we know the sibling already? */
9480
9481 if (orig_pdi->sibling)
9482 return orig_pdi->sibling;
9483
9484 /* Are there any children to deal with? */
9485
9486 if (!orig_pdi->has_children)
9487 return info_ptr;
9488
9489 /* Skip the children the long way. */
9490
9491 return skip_children (reader, info_ptr);
9492 }
9493
9494 /* Expand this partial symbol table into a full symbol table. SELF is
9495 not NULL. */
9496
9497 static void
9498 dwarf2_read_symtab (struct partial_symtab *self,
9499 struct objfile *objfile)
9500 {
9501 struct dwarf2_per_objfile *dwarf2_per_objfile
9502 = get_dwarf2_per_objfile (objfile);
9503
9504 if (self->readin)
9505 {
9506 warning (_("bug: psymtab for %s is already read in."),
9507 self->filename);
9508 }
9509 else
9510 {
9511 if (info_verbose)
9512 {
9513 printf_filtered (_("Reading in symbols for %s..."),
9514 self->filename);
9515 gdb_flush (gdb_stdout);
9516 }
9517
9518 /* If this psymtab is constructed from a debug-only objfile, the
9519 has_section_at_zero flag will not necessarily be correct. We
9520 can get the correct value for this flag by looking at the data
9521 associated with the (presumably stripped) associated objfile. */
9522 if (objfile->separate_debug_objfile_backlink)
9523 {
9524 struct dwarf2_per_objfile *dpo_backlink
9525 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9526
9527 dwarf2_per_objfile->has_section_at_zero
9528 = dpo_backlink->has_section_at_zero;
9529 }
9530
9531 dwarf2_per_objfile->reading_partial_symbols = 0;
9532
9533 psymtab_to_symtab_1 (self);
9534
9535 /* Finish up the debug error message. */
9536 if (info_verbose)
9537 printf_filtered (_("done.\n"));
9538 }
9539
9540 process_cu_includes (dwarf2_per_objfile);
9541 }
9542 \f
9543 /* Reading in full CUs. */
9544
9545 /* Add PER_CU to the queue. */
9546
9547 static void
9548 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9549 enum language pretend_language)
9550 {
9551 struct dwarf2_queue_item *item;
9552
9553 per_cu->queued = 1;
9554 item = XNEW (struct dwarf2_queue_item);
9555 item->per_cu = per_cu;
9556 item->pretend_language = pretend_language;
9557 item->next = NULL;
9558
9559 if (dwarf2_queue == NULL)
9560 dwarf2_queue = item;
9561 else
9562 dwarf2_queue_tail->next = item;
9563
9564 dwarf2_queue_tail = item;
9565 }
9566
9567 /* If PER_CU is not yet queued, add it to the queue.
9568 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9569 dependency.
9570 The result is non-zero if PER_CU was queued, otherwise the result is zero
9571 meaning either PER_CU is already queued or it is already loaded.
9572
9573 N.B. There is an invariant here that if a CU is queued then it is loaded.
9574 The caller is required to load PER_CU if we return non-zero. */
9575
9576 static int
9577 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9578 struct dwarf2_per_cu_data *per_cu,
9579 enum language pretend_language)
9580 {
9581 /* We may arrive here during partial symbol reading, if we need full
9582 DIEs to process an unusual case (e.g. template arguments). Do
9583 not queue PER_CU, just tell our caller to load its DIEs. */
9584 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
9585 {
9586 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9587 return 1;
9588 return 0;
9589 }
9590
9591 /* Mark the dependence relation so that we don't flush PER_CU
9592 too early. */
9593 if (dependent_cu != NULL)
9594 dwarf2_add_dependence (dependent_cu, per_cu);
9595
9596 /* If it's already on the queue, we have nothing to do. */
9597 if (per_cu->queued)
9598 return 0;
9599
9600 /* If the compilation unit is already loaded, just mark it as
9601 used. */
9602 if (per_cu->cu != NULL)
9603 {
9604 per_cu->cu->last_used = 0;
9605 return 0;
9606 }
9607
9608 /* Add it to the queue. */
9609 queue_comp_unit (per_cu, pretend_language);
9610
9611 return 1;
9612 }
9613
9614 /* Process the queue. */
9615
9616 static void
9617 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
9618 {
9619 struct dwarf2_queue_item *item, *next_item;
9620
9621 if (dwarf_read_debug)
9622 {
9623 fprintf_unfiltered (gdb_stdlog,
9624 "Expanding one or more symtabs of objfile %s ...\n",
9625 objfile_name (dwarf2_per_objfile->objfile));
9626 }
9627
9628 /* The queue starts out with one item, but following a DIE reference
9629 may load a new CU, adding it to the end of the queue. */
9630 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9631 {
9632 if ((dwarf2_per_objfile->using_index
9633 ? !item->per_cu->v.quick->compunit_symtab
9634 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9635 /* Skip dummy CUs. */
9636 && item->per_cu->cu != NULL)
9637 {
9638 struct dwarf2_per_cu_data *per_cu = item->per_cu;
9639 unsigned int debug_print_threshold;
9640 char buf[100];
9641
9642 if (per_cu->is_debug_types)
9643 {
9644 struct signatured_type *sig_type =
9645 (struct signatured_type *) per_cu;
9646
9647 sprintf (buf, "TU %s at offset %s",
9648 hex_string (sig_type->signature),
9649 sect_offset_str (per_cu->sect_off));
9650 /* There can be 100s of TUs.
9651 Only print them in verbose mode. */
9652 debug_print_threshold = 2;
9653 }
9654 else
9655 {
9656 sprintf (buf, "CU at offset %s",
9657 sect_offset_str (per_cu->sect_off));
9658 debug_print_threshold = 1;
9659 }
9660
9661 if (dwarf_read_debug >= debug_print_threshold)
9662 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9663
9664 if (per_cu->is_debug_types)
9665 process_full_type_unit (per_cu, item->pretend_language);
9666 else
9667 process_full_comp_unit (per_cu, item->pretend_language);
9668
9669 if (dwarf_read_debug >= debug_print_threshold)
9670 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9671 }
9672
9673 item->per_cu->queued = 0;
9674 next_item = item->next;
9675 xfree (item);
9676 }
9677
9678 dwarf2_queue_tail = NULL;
9679
9680 if (dwarf_read_debug)
9681 {
9682 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9683 objfile_name (dwarf2_per_objfile->objfile));
9684 }
9685 }
9686
9687 /* Read in full symbols for PST, and anything it depends on. */
9688
9689 static void
9690 psymtab_to_symtab_1 (struct partial_symtab *pst)
9691 {
9692 struct dwarf2_per_cu_data *per_cu;
9693 int i;
9694
9695 if (pst->readin)
9696 return;
9697
9698 for (i = 0; i < pst->number_of_dependencies; i++)
9699 if (!pst->dependencies[i]->readin
9700 && pst->dependencies[i]->user == NULL)
9701 {
9702 /* Inform about additional files that need to be read in. */
9703 if (info_verbose)
9704 {
9705 /* FIXME: i18n: Need to make this a single string. */
9706 fputs_filtered (" ", gdb_stdout);
9707 wrap_here ("");
9708 fputs_filtered ("and ", gdb_stdout);
9709 wrap_here ("");
9710 printf_filtered ("%s...", pst->dependencies[i]->filename);
9711 wrap_here (""); /* Flush output. */
9712 gdb_flush (gdb_stdout);
9713 }
9714 psymtab_to_symtab_1 (pst->dependencies[i]);
9715 }
9716
9717 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
9718
9719 if (per_cu == NULL)
9720 {
9721 /* It's an include file, no symbols to read for it.
9722 Everything is in the parent symtab. */
9723 pst->readin = 1;
9724 return;
9725 }
9726
9727 dw2_do_instantiate_symtab (per_cu, false);
9728 }
9729
9730 /* Trivial hash function for die_info: the hash value of a DIE
9731 is its offset in .debug_info for this objfile. */
9732
9733 static hashval_t
9734 die_hash (const void *item)
9735 {
9736 const struct die_info *die = (const struct die_info *) item;
9737
9738 return to_underlying (die->sect_off);
9739 }
9740
9741 /* Trivial comparison function for die_info structures: two DIEs
9742 are equal if they have the same offset. */
9743
9744 static int
9745 die_eq (const void *item_lhs, const void *item_rhs)
9746 {
9747 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9748 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9749
9750 return die_lhs->sect_off == die_rhs->sect_off;
9751 }
9752
9753 /* Load the DIEs associated with PER_CU into memory. */
9754
9755 static void
9756 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9757 bool skip_partial,
9758 enum language pretend_language)
9759 {
9760 gdb_assert (! this_cu->is_debug_types);
9761
9762 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9763 if (reader.dummy_p)
9764 return;
9765
9766 struct dwarf2_cu *cu = reader.cu;
9767 const gdb_byte *info_ptr = reader.info_ptr;
9768
9769 gdb_assert (cu->die_hash == NULL);
9770 cu->die_hash =
9771 htab_create_alloc_ex (cu->header.length / 12,
9772 die_hash,
9773 die_eq,
9774 NULL,
9775 &cu->comp_unit_obstack,
9776 hashtab_obstack_allocate,
9777 dummy_obstack_deallocate);
9778
9779 if (reader.has_children)
9780 reader.comp_unit_die->child
9781 = read_die_and_siblings (&reader, reader.info_ptr,
9782 &info_ptr, reader.comp_unit_die);
9783 cu->dies = reader.comp_unit_die;
9784 /* comp_unit_die is not stored in die_hash, no need. */
9785
9786 /* We try not to read any attributes in this function, because not
9787 all CUs needed for references have been loaded yet, and symbol
9788 table processing isn't initialized. But we have to set the CU language,
9789 or we won't be able to build types correctly.
9790 Similarly, if we do not read the producer, we can not apply
9791 producer-specific interpretation. */
9792 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9793 }
9794
9795 /* Add a DIE to the delayed physname list. */
9796
9797 static void
9798 add_to_method_list (struct type *type, int fnfield_index, int index,
9799 const char *name, struct die_info *die,
9800 struct dwarf2_cu *cu)
9801 {
9802 struct delayed_method_info mi;
9803 mi.type = type;
9804 mi.fnfield_index = fnfield_index;
9805 mi.index = index;
9806 mi.name = name;
9807 mi.die = die;
9808 cu->method_list.push_back (mi);
9809 }
9810
9811 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9812 "const" / "volatile". If so, decrements LEN by the length of the
9813 modifier and return true. Otherwise return false. */
9814
9815 template<size_t N>
9816 static bool
9817 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9818 {
9819 size_t mod_len = sizeof (mod) - 1;
9820 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9821 {
9822 len -= mod_len;
9823 return true;
9824 }
9825 return false;
9826 }
9827
9828 /* Compute the physnames of any methods on the CU's method list.
9829
9830 The computation of method physnames is delayed in order to avoid the
9831 (bad) condition that one of the method's formal parameters is of an as yet
9832 incomplete type. */
9833
9834 static void
9835 compute_delayed_physnames (struct dwarf2_cu *cu)
9836 {
9837 /* Only C++ delays computing physnames. */
9838 if (cu->method_list.empty ())
9839 return;
9840 gdb_assert (cu->language == language_cplus);
9841
9842 for (const delayed_method_info &mi : cu->method_list)
9843 {
9844 const char *physname;
9845 struct fn_fieldlist *fn_flp
9846 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9847 physname = dwarf2_physname (mi.name, mi.die, cu);
9848 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9849 = physname ? physname : "";
9850
9851 /* Since there's no tag to indicate whether a method is a
9852 const/volatile overload, extract that information out of the
9853 demangled name. */
9854 if (physname != NULL)
9855 {
9856 size_t len = strlen (physname);
9857
9858 while (1)
9859 {
9860 if (physname[len] == ')') /* shortcut */
9861 break;
9862 else if (check_modifier (physname, len, " const"))
9863 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9864 else if (check_modifier (physname, len, " volatile"))
9865 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9866 else
9867 break;
9868 }
9869 }
9870 }
9871
9872 /* The list is no longer needed. */
9873 cu->method_list.clear ();
9874 }
9875
9876 /* Go objects should be embedded in a DW_TAG_module DIE,
9877 and it's not clear if/how imported objects will appear.
9878 To keep Go support simple until that's worked out,
9879 go back through what we've read and create something usable.
9880 We could do this while processing each DIE, and feels kinda cleaner,
9881 but that way is more invasive.
9882 This is to, for example, allow the user to type "p var" or "b main"
9883 without having to specify the package name, and allow lookups
9884 of module.object to work in contexts that use the expression
9885 parser. */
9886
9887 static void
9888 fixup_go_packaging (struct dwarf2_cu *cu)
9889 {
9890 gdb::unique_xmalloc_ptr<char> package_name;
9891 struct pending *list;
9892 int i;
9893
9894 for (list = *cu->get_builder ()->get_global_symbols ();
9895 list != NULL;
9896 list = list->next)
9897 {
9898 for (i = 0; i < list->nsyms; ++i)
9899 {
9900 struct symbol *sym = list->symbol[i];
9901
9902 if (sym->language () == language_go
9903 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9904 {
9905 gdb::unique_xmalloc_ptr<char> this_package_name
9906 (go_symbol_package_name (sym));
9907
9908 if (this_package_name == NULL)
9909 continue;
9910 if (package_name == NULL)
9911 package_name = std::move (this_package_name);
9912 else
9913 {
9914 struct objfile *objfile
9915 = cu->per_cu->dwarf2_per_objfile->objfile;
9916 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9917 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9918 (symbol_symtab (sym) != NULL
9919 ? symtab_to_filename_for_display
9920 (symbol_symtab (sym))
9921 : objfile_name (objfile)),
9922 this_package_name.get (), package_name.get ());
9923 }
9924 }
9925 }
9926 }
9927
9928 if (package_name != NULL)
9929 {
9930 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9931 const char *saved_package_name
9932 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
9933 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9934 saved_package_name);
9935 struct symbol *sym;
9936
9937 sym = allocate_symbol (objfile);
9938 sym->set_language (language_go, &objfile->objfile_obstack);
9939 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9940 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9941 e.g., "main" finds the "main" module and not C's main(). */
9942 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9943 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9944 SYMBOL_TYPE (sym) = type;
9945
9946 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9947 }
9948 }
9949
9950 /* Allocate a fully-qualified name consisting of the two parts on the
9951 obstack. */
9952
9953 static const char *
9954 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9955 {
9956 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9957 }
9958
9959 /* A helper that allocates a struct discriminant_info to attach to a
9960 union type. */
9961
9962 static struct discriminant_info *
9963 alloc_discriminant_info (struct type *type, int discriminant_index,
9964 int default_index)
9965 {
9966 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9967 gdb_assert (discriminant_index == -1
9968 || (discriminant_index >= 0
9969 && discriminant_index < TYPE_NFIELDS (type)));
9970 gdb_assert (default_index == -1
9971 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9972
9973 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9974
9975 struct discriminant_info *disc
9976 = ((struct discriminant_info *)
9977 TYPE_ZALLOC (type,
9978 offsetof (struct discriminant_info, discriminants)
9979 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9980 disc->default_index = default_index;
9981 disc->discriminant_index = discriminant_index;
9982
9983 struct dynamic_prop prop;
9984 prop.kind = PROP_UNDEFINED;
9985 prop.data.baton = disc;
9986
9987 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9988
9989 return disc;
9990 }
9991
9992 /* Some versions of rustc emitted enums in an unusual way.
9993
9994 Ordinary enums were emitted as unions. The first element of each
9995 structure in the union was named "RUST$ENUM$DISR". This element
9996 held the discriminant.
9997
9998 These versions of Rust also implemented the "non-zero"
9999 optimization. When the enum had two values, and one is empty and
10000 the other holds a pointer that cannot be zero, the pointer is used
10001 as the discriminant, with a zero value meaning the empty variant.
10002 Here, the union's first member is of the form
10003 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
10004 where the fieldnos are the indices of the fields that should be
10005 traversed in order to find the field (which may be several fields deep)
10006 and the variantname is the name of the variant of the case when the
10007 field is zero.
10008
10009 This function recognizes whether TYPE is of one of these forms,
10010 and, if so, smashes it to be a variant type. */
10011
10012 static void
10013 quirk_rust_enum (struct type *type, struct objfile *objfile)
10014 {
10015 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10016
10017 /* We don't need to deal with empty enums. */
10018 if (TYPE_NFIELDS (type) == 0)
10019 return;
10020
10021 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10022 if (TYPE_NFIELDS (type) == 1
10023 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10024 {
10025 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10026
10027 /* Decode the field name to find the offset of the
10028 discriminant. */
10029 ULONGEST bit_offset = 0;
10030 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10031 while (name[0] >= '0' && name[0] <= '9')
10032 {
10033 char *tail;
10034 unsigned long index = strtoul (name, &tail, 10);
10035 name = tail;
10036 if (*name != '$'
10037 || index >= TYPE_NFIELDS (field_type)
10038 || (TYPE_FIELD_LOC_KIND (field_type, index)
10039 != FIELD_LOC_KIND_BITPOS))
10040 {
10041 complaint (_("Could not parse Rust enum encoding string \"%s\""
10042 "[in module %s]"),
10043 TYPE_FIELD_NAME (type, 0),
10044 objfile_name (objfile));
10045 return;
10046 }
10047 ++name;
10048
10049 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10050 field_type = TYPE_FIELD_TYPE (field_type, index);
10051 }
10052
10053 /* Make a union to hold the variants. */
10054 struct type *union_type = alloc_type (objfile);
10055 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10056 TYPE_NFIELDS (union_type) = 3;
10057 TYPE_FIELDS (union_type)
10058 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10059 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10060 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10061
10062 /* Put the discriminant must at index 0. */
10063 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10064 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10065 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10066 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10067
10068 /* The order of fields doesn't really matter, so put the real
10069 field at index 1 and the data-less field at index 2. */
10070 struct discriminant_info *disc
10071 = alloc_discriminant_info (union_type, 0, 1);
10072 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10073 TYPE_FIELD_NAME (union_type, 1)
10074 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10075 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10076 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10077 TYPE_FIELD_NAME (union_type, 1));
10078
10079 const char *dataless_name
10080 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10081 name);
10082 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10083 dataless_name);
10084 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10085 /* NAME points into the original discriminant name, which
10086 already has the correct lifetime. */
10087 TYPE_FIELD_NAME (union_type, 2) = name;
10088 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10089 disc->discriminants[2] = 0;
10090
10091 /* Smash this type to be a structure type. We have to do this
10092 because the type has already been recorded. */
10093 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10094 TYPE_NFIELDS (type) = 1;
10095 TYPE_FIELDS (type)
10096 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10097
10098 /* Install the variant part. */
10099 TYPE_FIELD_TYPE (type, 0) = union_type;
10100 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10101 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10102 }
10103 /* A union with a single anonymous field is probably an old-style
10104 univariant enum. */
10105 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
10106 {
10107 /* Smash this type to be a structure type. We have to do this
10108 because the type has already been recorded. */
10109 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10110
10111 /* Make a union to hold the variants. */
10112 struct type *union_type = alloc_type (objfile);
10113 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10114 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10115 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10116 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10117 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10118
10119 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10120 const char *variant_name
10121 = rust_last_path_segment (TYPE_NAME (field_type));
10122 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10123 TYPE_NAME (field_type)
10124 = rust_fully_qualify (&objfile->objfile_obstack,
10125 TYPE_NAME (type), variant_name);
10126
10127 /* Install the union in the outer struct type. */
10128 TYPE_NFIELDS (type) = 1;
10129 TYPE_FIELDS (type)
10130 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10131 TYPE_FIELD_TYPE (type, 0) = union_type;
10132 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10133 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10134
10135 alloc_discriminant_info (union_type, -1, 0);
10136 }
10137 else
10138 {
10139 struct type *disr_type = nullptr;
10140 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10141 {
10142 disr_type = TYPE_FIELD_TYPE (type, i);
10143
10144 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10145 {
10146 /* All fields of a true enum will be structs. */
10147 return;
10148 }
10149 else if (TYPE_NFIELDS (disr_type) == 0)
10150 {
10151 /* Could be data-less variant, so keep going. */
10152 disr_type = nullptr;
10153 }
10154 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10155 "RUST$ENUM$DISR") != 0)
10156 {
10157 /* Not a Rust enum. */
10158 return;
10159 }
10160 else
10161 {
10162 /* Found one. */
10163 break;
10164 }
10165 }
10166
10167 /* If we got here without a discriminant, then it's probably
10168 just a union. */
10169 if (disr_type == nullptr)
10170 return;
10171
10172 /* Smash this type to be a structure type. We have to do this
10173 because the type has already been recorded. */
10174 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10175
10176 /* Make a union to hold the variants. */
10177 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10178 struct type *union_type = alloc_type (objfile);
10179 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10180 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10181 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
10182 set_type_align (union_type, TYPE_RAW_ALIGN (type));
10183 TYPE_FIELDS (union_type)
10184 = (struct field *) TYPE_ZALLOC (union_type,
10185 (TYPE_NFIELDS (union_type)
10186 * sizeof (struct field)));
10187
10188 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10189 TYPE_NFIELDS (type) * sizeof (struct field));
10190
10191 /* Install the discriminant at index 0 in the union. */
10192 TYPE_FIELD (union_type, 0) = *disr_field;
10193 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10194 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10195
10196 /* Install the union in the outer struct type. */
10197 TYPE_FIELD_TYPE (type, 0) = union_type;
10198 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10199 TYPE_NFIELDS (type) = 1;
10200
10201 /* Set the size and offset of the union type. */
10202 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10203
10204 /* We need a way to find the correct discriminant given a
10205 variant name. For convenience we build a map here. */
10206 struct type *enum_type = FIELD_TYPE (*disr_field);
10207 std::unordered_map<std::string, ULONGEST> discriminant_map;
10208 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10209 {
10210 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10211 {
10212 const char *name
10213 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10214 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10215 }
10216 }
10217
10218 int n_fields = TYPE_NFIELDS (union_type);
10219 struct discriminant_info *disc
10220 = alloc_discriminant_info (union_type, 0, -1);
10221 /* Skip the discriminant here. */
10222 for (int i = 1; i < n_fields; ++i)
10223 {
10224 /* Find the final word in the name of this variant's type.
10225 That name can be used to look up the correct
10226 discriminant. */
10227 const char *variant_name
10228 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10229 i)));
10230
10231 auto iter = discriminant_map.find (variant_name);
10232 if (iter != discriminant_map.end ())
10233 disc->discriminants[i] = iter->second;
10234
10235 /* Remove the discriminant field, if it exists. */
10236 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
10237 if (TYPE_NFIELDS (sub_type) > 0)
10238 {
10239 --TYPE_NFIELDS (sub_type);
10240 ++TYPE_FIELDS (sub_type);
10241 }
10242 TYPE_FIELD_NAME (union_type, i) = variant_name;
10243 TYPE_NAME (sub_type)
10244 = rust_fully_qualify (&objfile->objfile_obstack,
10245 TYPE_NAME (type), variant_name);
10246 }
10247 }
10248 }
10249
10250 /* Rewrite some Rust unions to be structures with variants parts. */
10251
10252 static void
10253 rust_union_quirks (struct dwarf2_cu *cu)
10254 {
10255 gdb_assert (cu->language == language_rust);
10256 for (type *type_ : cu->rust_unions)
10257 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
10258 /* We don't need this any more. */
10259 cu->rust_unions.clear ();
10260 }
10261
10262 /* Return the symtab for PER_CU. This works properly regardless of
10263 whether we're using the index or psymtabs. */
10264
10265 static struct compunit_symtab *
10266 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
10267 {
10268 return (per_cu->dwarf2_per_objfile->using_index
10269 ? per_cu->v.quick->compunit_symtab
10270 : per_cu->v.psymtab->compunit_symtab);
10271 }
10272
10273 /* A helper function for computing the list of all symbol tables
10274 included by PER_CU. */
10275
10276 static void
10277 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
10278 htab_t all_children, htab_t all_type_symtabs,
10279 struct dwarf2_per_cu_data *per_cu,
10280 struct compunit_symtab *immediate_parent)
10281 {
10282 void **slot;
10283 struct compunit_symtab *cust;
10284
10285 slot = htab_find_slot (all_children, per_cu, INSERT);
10286 if (*slot != NULL)
10287 {
10288 /* This inclusion and its children have been processed. */
10289 return;
10290 }
10291
10292 *slot = per_cu;
10293 /* Only add a CU if it has a symbol table. */
10294 cust = get_compunit_symtab (per_cu);
10295 if (cust != NULL)
10296 {
10297 /* If this is a type unit only add its symbol table if we haven't
10298 seen it yet (type unit per_cu's can share symtabs). */
10299 if (per_cu->is_debug_types)
10300 {
10301 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
10302 if (*slot == NULL)
10303 {
10304 *slot = cust;
10305 result->push_back (cust);
10306 if (cust->user == NULL)
10307 cust->user = immediate_parent;
10308 }
10309 }
10310 else
10311 {
10312 result->push_back (cust);
10313 if (cust->user == NULL)
10314 cust->user = immediate_parent;
10315 }
10316 }
10317
10318 if (!per_cu->imported_symtabs_empty ())
10319 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10320 {
10321 recursively_compute_inclusions (result, all_children,
10322 all_type_symtabs, ptr, cust);
10323 }
10324 }
10325
10326 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10327 PER_CU. */
10328
10329 static void
10330 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
10331 {
10332 gdb_assert (! per_cu->is_debug_types);
10333
10334 if (!per_cu->imported_symtabs_empty ())
10335 {
10336 int len;
10337 std::vector<compunit_symtab *> result_symtabs;
10338 htab_t all_children, all_type_symtabs;
10339 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
10340
10341 /* If we don't have a symtab, we can just skip this case. */
10342 if (cust == NULL)
10343 return;
10344
10345 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10346 NULL, xcalloc, xfree);
10347 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10348 NULL, xcalloc, xfree);
10349
10350 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10351 {
10352 recursively_compute_inclusions (&result_symtabs, all_children,
10353 all_type_symtabs, ptr, cust);
10354 }
10355
10356 /* Now we have a transitive closure of all the included symtabs. */
10357 len = result_symtabs.size ();
10358 cust->includes
10359 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
10360 struct compunit_symtab *, len + 1);
10361 memcpy (cust->includes, result_symtabs.data (),
10362 len * sizeof (compunit_symtab *));
10363 cust->includes[len] = NULL;
10364
10365 htab_delete (all_children);
10366 htab_delete (all_type_symtabs);
10367 }
10368 }
10369
10370 /* Compute the 'includes' field for the symtabs of all the CUs we just
10371 read. */
10372
10373 static void
10374 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
10375 {
10376 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
10377 {
10378 if (! iter->is_debug_types)
10379 compute_compunit_symtab_includes (iter);
10380 }
10381
10382 dwarf2_per_objfile->just_read_cus.clear ();
10383 }
10384
10385 /* Generate full symbol information for PER_CU, whose DIEs have
10386 already been loaded into memory. */
10387
10388 static void
10389 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10390 enum language pretend_language)
10391 {
10392 struct dwarf2_cu *cu = per_cu->cu;
10393 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10394 struct objfile *objfile = dwarf2_per_objfile->objfile;
10395 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10396 CORE_ADDR lowpc, highpc;
10397 struct compunit_symtab *cust;
10398 CORE_ADDR baseaddr;
10399 struct block *static_block;
10400 CORE_ADDR addr;
10401
10402 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
10403
10404 /* Clear the list here in case something was left over. */
10405 cu->method_list.clear ();
10406
10407 cu->language = pretend_language;
10408 cu->language_defn = language_def (cu->language);
10409
10410 /* Do line number decoding in read_file_scope () */
10411 process_die (cu->dies, cu);
10412
10413 /* For now fudge the Go package. */
10414 if (cu->language == language_go)
10415 fixup_go_packaging (cu);
10416
10417 /* Now that we have processed all the DIEs in the CU, all the types
10418 should be complete, and it should now be safe to compute all of the
10419 physnames. */
10420 compute_delayed_physnames (cu);
10421
10422 if (cu->language == language_rust)
10423 rust_union_quirks (cu);
10424
10425 /* Some compilers don't define a DW_AT_high_pc attribute for the
10426 compilation unit. If the DW_AT_high_pc is missing, synthesize
10427 it, by scanning the DIE's below the compilation unit. */
10428 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10429
10430 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10431 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10432
10433 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10434 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10435 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10436 addrmap to help ensure it has an accurate map of pc values belonging to
10437 this comp unit. */
10438 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10439
10440 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10441 SECT_OFF_TEXT (objfile),
10442 0);
10443
10444 if (cust != NULL)
10445 {
10446 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10447
10448 /* Set symtab language to language from DW_AT_language. If the
10449 compilation is from a C file generated by language preprocessors, do
10450 not set the language if it was already deduced by start_subfile. */
10451 if (!(cu->language == language_c
10452 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10453 COMPUNIT_FILETABS (cust)->language = cu->language;
10454
10455 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10456 produce DW_AT_location with location lists but it can be possibly
10457 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10458 there were bugs in prologue debug info, fixed later in GCC-4.5
10459 by "unwind info for epilogues" patch (which is not directly related).
10460
10461 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10462 needed, it would be wrong due to missing DW_AT_producer there.
10463
10464 Still one can confuse GDB by using non-standard GCC compilation
10465 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10466 */
10467 if (cu->has_loclist && gcc_4_minor >= 5)
10468 cust->locations_valid = 1;
10469
10470 if (gcc_4_minor >= 5)
10471 cust->epilogue_unwind_valid = 1;
10472
10473 cust->call_site_htab = cu->call_site_htab;
10474 }
10475
10476 if (dwarf2_per_objfile->using_index)
10477 per_cu->v.quick->compunit_symtab = cust;
10478 else
10479 {
10480 struct partial_symtab *pst = per_cu->v.psymtab;
10481 pst->compunit_symtab = cust;
10482 pst->readin = 1;
10483 }
10484
10485 /* Push it for inclusion processing later. */
10486 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
10487
10488 /* Not needed any more. */
10489 cu->reset_builder ();
10490 }
10491
10492 /* Generate full symbol information for type unit PER_CU, whose DIEs have
10493 already been loaded into memory. */
10494
10495 static void
10496 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10497 enum language pretend_language)
10498 {
10499 struct dwarf2_cu *cu = per_cu->cu;
10500 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10501 struct objfile *objfile = dwarf2_per_objfile->objfile;
10502 struct compunit_symtab *cust;
10503 struct signatured_type *sig_type;
10504
10505 gdb_assert (per_cu->is_debug_types);
10506 sig_type = (struct signatured_type *) per_cu;
10507
10508 /* Clear the list here in case something was left over. */
10509 cu->method_list.clear ();
10510
10511 cu->language = pretend_language;
10512 cu->language_defn = language_def (cu->language);
10513
10514 /* The symbol tables are set up in read_type_unit_scope. */
10515 process_die (cu->dies, cu);
10516
10517 /* For now fudge the Go package. */
10518 if (cu->language == language_go)
10519 fixup_go_packaging (cu);
10520
10521 /* Now that we have processed all the DIEs in the CU, all the types
10522 should be complete, and it should now be safe to compute all of the
10523 physnames. */
10524 compute_delayed_physnames (cu);
10525
10526 if (cu->language == language_rust)
10527 rust_union_quirks (cu);
10528
10529 /* TUs share symbol tables.
10530 If this is the first TU to use this symtab, complete the construction
10531 of it with end_expandable_symtab. Otherwise, complete the addition of
10532 this TU's symbols to the existing symtab. */
10533 if (sig_type->type_unit_group->compunit_symtab == NULL)
10534 {
10535 buildsym_compunit *builder = cu->get_builder ();
10536 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10537 sig_type->type_unit_group->compunit_symtab = cust;
10538
10539 if (cust != NULL)
10540 {
10541 /* Set symtab language to language from DW_AT_language. If the
10542 compilation is from a C file generated by language preprocessors,
10543 do not set the language if it was already deduced by
10544 start_subfile. */
10545 if (!(cu->language == language_c
10546 && COMPUNIT_FILETABS (cust)->language != language_c))
10547 COMPUNIT_FILETABS (cust)->language = cu->language;
10548 }
10549 }
10550 else
10551 {
10552 cu->get_builder ()->augment_type_symtab ();
10553 cust = sig_type->type_unit_group->compunit_symtab;
10554 }
10555
10556 if (dwarf2_per_objfile->using_index)
10557 per_cu->v.quick->compunit_symtab = cust;
10558 else
10559 {
10560 struct partial_symtab *pst = per_cu->v.psymtab;
10561 pst->compunit_symtab = cust;
10562 pst->readin = 1;
10563 }
10564
10565 /* Not needed any more. */
10566 cu->reset_builder ();
10567 }
10568
10569 /* Process an imported unit DIE. */
10570
10571 static void
10572 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10573 {
10574 struct attribute *attr;
10575
10576 /* For now we don't handle imported units in type units. */
10577 if (cu->per_cu->is_debug_types)
10578 {
10579 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10580 " supported in type units [in module %s]"),
10581 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
10582 }
10583
10584 attr = dwarf2_attr (die, DW_AT_import, cu);
10585 if (attr != NULL)
10586 {
10587 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10588 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10589 dwarf2_per_cu_data *per_cu
10590 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
10591 cu->per_cu->dwarf2_per_objfile);
10592
10593 /* If necessary, add it to the queue and load its DIEs. */
10594 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
10595 load_full_comp_unit (per_cu, false, cu->language);
10596
10597 cu->per_cu->imported_symtabs_push (per_cu);
10598 }
10599 }
10600
10601 /* RAII object that represents a process_die scope: i.e.,
10602 starts/finishes processing a DIE. */
10603 class process_die_scope
10604 {
10605 public:
10606 process_die_scope (die_info *die, dwarf2_cu *cu)
10607 : m_die (die), m_cu (cu)
10608 {
10609 /* We should only be processing DIEs not already in process. */
10610 gdb_assert (!m_die->in_process);
10611 m_die->in_process = true;
10612 }
10613
10614 ~process_die_scope ()
10615 {
10616 m_die->in_process = false;
10617
10618 /* If we're done processing the DIE for the CU that owns the line
10619 header, we don't need the line header anymore. */
10620 if (m_cu->line_header_die_owner == m_die)
10621 {
10622 delete m_cu->line_header;
10623 m_cu->line_header = NULL;
10624 m_cu->line_header_die_owner = NULL;
10625 }
10626 }
10627
10628 private:
10629 die_info *m_die;
10630 dwarf2_cu *m_cu;
10631 };
10632
10633 /* Process a die and its children. */
10634
10635 static void
10636 process_die (struct die_info *die, struct dwarf2_cu *cu)
10637 {
10638 process_die_scope scope (die, cu);
10639
10640 switch (die->tag)
10641 {
10642 case DW_TAG_padding:
10643 break;
10644 case DW_TAG_compile_unit:
10645 case DW_TAG_partial_unit:
10646 read_file_scope (die, cu);
10647 break;
10648 case DW_TAG_type_unit:
10649 read_type_unit_scope (die, cu);
10650 break;
10651 case DW_TAG_subprogram:
10652 /* Nested subprograms in Fortran get a prefix. */
10653 if (cu->language == language_fortran
10654 && die->parent != NULL
10655 && die->parent->tag == DW_TAG_subprogram)
10656 cu->processing_has_namespace_info = true;
10657 /* Fall through. */
10658 case DW_TAG_inlined_subroutine:
10659 read_func_scope (die, cu);
10660 break;
10661 case DW_TAG_lexical_block:
10662 case DW_TAG_try_block:
10663 case DW_TAG_catch_block:
10664 read_lexical_block_scope (die, cu);
10665 break;
10666 case DW_TAG_call_site:
10667 case DW_TAG_GNU_call_site:
10668 read_call_site_scope (die, cu);
10669 break;
10670 case DW_TAG_class_type:
10671 case DW_TAG_interface_type:
10672 case DW_TAG_structure_type:
10673 case DW_TAG_union_type:
10674 process_structure_scope (die, cu);
10675 break;
10676 case DW_TAG_enumeration_type:
10677 process_enumeration_scope (die, cu);
10678 break;
10679
10680 /* These dies have a type, but processing them does not create
10681 a symbol or recurse to process the children. Therefore we can
10682 read them on-demand through read_type_die. */
10683 case DW_TAG_subroutine_type:
10684 case DW_TAG_set_type:
10685 case DW_TAG_array_type:
10686 case DW_TAG_pointer_type:
10687 case DW_TAG_ptr_to_member_type:
10688 case DW_TAG_reference_type:
10689 case DW_TAG_rvalue_reference_type:
10690 case DW_TAG_string_type:
10691 break;
10692
10693 case DW_TAG_base_type:
10694 case DW_TAG_subrange_type:
10695 case DW_TAG_typedef:
10696 /* Add a typedef symbol for the type definition, if it has a
10697 DW_AT_name. */
10698 new_symbol (die, read_type_die (die, cu), cu);
10699 break;
10700 case DW_TAG_common_block:
10701 read_common_block (die, cu);
10702 break;
10703 case DW_TAG_common_inclusion:
10704 break;
10705 case DW_TAG_namespace:
10706 cu->processing_has_namespace_info = true;
10707 read_namespace (die, cu);
10708 break;
10709 case DW_TAG_module:
10710 cu->processing_has_namespace_info = true;
10711 read_module (die, cu);
10712 break;
10713 case DW_TAG_imported_declaration:
10714 cu->processing_has_namespace_info = true;
10715 if (read_namespace_alias (die, cu))
10716 break;
10717 /* The declaration is not a global namespace alias. */
10718 /* Fall through. */
10719 case DW_TAG_imported_module:
10720 cu->processing_has_namespace_info = true;
10721 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10722 || cu->language != language_fortran))
10723 complaint (_("Tag '%s' has unexpected children"),
10724 dwarf_tag_name (die->tag));
10725 read_import_statement (die, cu);
10726 break;
10727
10728 case DW_TAG_imported_unit:
10729 process_imported_unit_die (die, cu);
10730 break;
10731
10732 case DW_TAG_variable:
10733 read_variable (die, cu);
10734 break;
10735
10736 default:
10737 new_symbol (die, NULL, cu);
10738 break;
10739 }
10740 }
10741 \f
10742 /* DWARF name computation. */
10743
10744 /* A helper function for dwarf2_compute_name which determines whether DIE
10745 needs to have the name of the scope prepended to the name listed in the
10746 die. */
10747
10748 static int
10749 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10750 {
10751 struct attribute *attr;
10752
10753 switch (die->tag)
10754 {
10755 case DW_TAG_namespace:
10756 case DW_TAG_typedef:
10757 case DW_TAG_class_type:
10758 case DW_TAG_interface_type:
10759 case DW_TAG_structure_type:
10760 case DW_TAG_union_type:
10761 case DW_TAG_enumeration_type:
10762 case DW_TAG_enumerator:
10763 case DW_TAG_subprogram:
10764 case DW_TAG_inlined_subroutine:
10765 case DW_TAG_member:
10766 case DW_TAG_imported_declaration:
10767 return 1;
10768
10769 case DW_TAG_variable:
10770 case DW_TAG_constant:
10771 /* We only need to prefix "globally" visible variables. These include
10772 any variable marked with DW_AT_external or any variable that
10773 lives in a namespace. [Variables in anonymous namespaces
10774 require prefixing, but they are not DW_AT_external.] */
10775
10776 if (dwarf2_attr (die, DW_AT_specification, cu))
10777 {
10778 struct dwarf2_cu *spec_cu = cu;
10779
10780 return die_needs_namespace (die_specification (die, &spec_cu),
10781 spec_cu);
10782 }
10783
10784 attr = dwarf2_attr (die, DW_AT_external, cu);
10785 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10786 && die->parent->tag != DW_TAG_module)
10787 return 0;
10788 /* A variable in a lexical block of some kind does not need a
10789 namespace, even though in C++ such variables may be external
10790 and have a mangled name. */
10791 if (die->parent->tag == DW_TAG_lexical_block
10792 || die->parent->tag == DW_TAG_try_block
10793 || die->parent->tag == DW_TAG_catch_block
10794 || die->parent->tag == DW_TAG_subprogram)
10795 return 0;
10796 return 1;
10797
10798 default:
10799 return 0;
10800 }
10801 }
10802
10803 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10804 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10805 defined for the given DIE. */
10806
10807 static struct attribute *
10808 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10809 {
10810 struct attribute *attr;
10811
10812 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10813 if (attr == NULL)
10814 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10815
10816 return attr;
10817 }
10818
10819 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10820 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10821 defined for the given DIE. */
10822
10823 static const char *
10824 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10825 {
10826 const char *linkage_name;
10827
10828 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10829 if (linkage_name == NULL)
10830 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10831
10832 return linkage_name;
10833 }
10834
10835 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10836 compute the physname for the object, which include a method's:
10837 - formal parameters (C++),
10838 - receiver type (Go),
10839
10840 The term "physname" is a bit confusing.
10841 For C++, for example, it is the demangled name.
10842 For Go, for example, it's the mangled name.
10843
10844 For Ada, return the DIE's linkage name rather than the fully qualified
10845 name. PHYSNAME is ignored..
10846
10847 The result is allocated on the objfile_obstack and canonicalized. */
10848
10849 static const char *
10850 dwarf2_compute_name (const char *name,
10851 struct die_info *die, struct dwarf2_cu *cu,
10852 int physname)
10853 {
10854 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10855
10856 if (name == NULL)
10857 name = dwarf2_name (die, cu);
10858
10859 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10860 but otherwise compute it by typename_concat inside GDB.
10861 FIXME: Actually this is not really true, or at least not always true.
10862 It's all very confusing. compute_and_set_names doesn't try to demangle
10863 Fortran names because there is no mangling standard. So new_symbol
10864 will set the demangled name to the result of dwarf2_full_name, and it is
10865 the demangled name that GDB uses if it exists. */
10866 if (cu->language == language_ada
10867 || (cu->language == language_fortran && physname))
10868 {
10869 /* For Ada unit, we prefer the linkage name over the name, as
10870 the former contains the exported name, which the user expects
10871 to be able to reference. Ideally, we want the user to be able
10872 to reference this entity using either natural or linkage name,
10873 but we haven't started looking at this enhancement yet. */
10874 const char *linkage_name = dw2_linkage_name (die, cu);
10875
10876 if (linkage_name != NULL)
10877 return linkage_name;
10878 }
10879
10880 /* These are the only languages we know how to qualify names in. */
10881 if (name != NULL
10882 && (cu->language == language_cplus
10883 || cu->language == language_fortran || cu->language == language_d
10884 || cu->language == language_rust))
10885 {
10886 if (die_needs_namespace (die, cu))
10887 {
10888 const char *prefix;
10889 const char *canonical_name = NULL;
10890
10891 string_file buf;
10892
10893 prefix = determine_prefix (die, cu);
10894 if (*prefix != '\0')
10895 {
10896 gdb::unique_xmalloc_ptr<char> prefixed_name
10897 (typename_concat (NULL, prefix, name, physname, cu));
10898
10899 buf.puts (prefixed_name.get ());
10900 }
10901 else
10902 buf.puts (name);
10903
10904 /* Template parameters may be specified in the DIE's DW_AT_name, or
10905 as children with DW_TAG_template_type_param or
10906 DW_TAG_value_type_param. If the latter, add them to the name
10907 here. If the name already has template parameters, then
10908 skip this step; some versions of GCC emit both, and
10909 it is more efficient to use the pre-computed name.
10910
10911 Something to keep in mind about this process: it is very
10912 unlikely, or in some cases downright impossible, to produce
10913 something that will match the mangled name of a function.
10914 If the definition of the function has the same debug info,
10915 we should be able to match up with it anyway. But fallbacks
10916 using the minimal symbol, for instance to find a method
10917 implemented in a stripped copy of libstdc++, will not work.
10918 If we do not have debug info for the definition, we will have to
10919 match them up some other way.
10920
10921 When we do name matching there is a related problem with function
10922 templates; two instantiated function templates are allowed to
10923 differ only by their return types, which we do not add here. */
10924
10925 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10926 {
10927 struct attribute *attr;
10928 struct die_info *child;
10929 int first = 1;
10930
10931 die->building_fullname = 1;
10932
10933 for (child = die->child; child != NULL; child = child->sibling)
10934 {
10935 struct type *type;
10936 LONGEST value;
10937 const gdb_byte *bytes;
10938 struct dwarf2_locexpr_baton *baton;
10939 struct value *v;
10940
10941 if (child->tag != DW_TAG_template_type_param
10942 && child->tag != DW_TAG_template_value_param)
10943 continue;
10944
10945 if (first)
10946 {
10947 buf.puts ("<");
10948 first = 0;
10949 }
10950 else
10951 buf.puts (", ");
10952
10953 attr = dwarf2_attr (child, DW_AT_type, cu);
10954 if (attr == NULL)
10955 {
10956 complaint (_("template parameter missing DW_AT_type"));
10957 buf.puts ("UNKNOWN_TYPE");
10958 continue;
10959 }
10960 type = die_type (child, cu);
10961
10962 if (child->tag == DW_TAG_template_type_param)
10963 {
10964 c_print_type (type, "", &buf, -1, 0, cu->language,
10965 &type_print_raw_options);
10966 continue;
10967 }
10968
10969 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10970 if (attr == NULL)
10971 {
10972 complaint (_("template parameter missing "
10973 "DW_AT_const_value"));
10974 buf.puts ("UNKNOWN_VALUE");
10975 continue;
10976 }
10977
10978 dwarf2_const_value_attr (attr, type, name,
10979 &cu->comp_unit_obstack, cu,
10980 &value, &bytes, &baton);
10981
10982 if (TYPE_NOSIGN (type))
10983 /* GDB prints characters as NUMBER 'CHAR'. If that's
10984 changed, this can use value_print instead. */
10985 c_printchar (value, type, &buf);
10986 else
10987 {
10988 struct value_print_options opts;
10989
10990 if (baton != NULL)
10991 v = dwarf2_evaluate_loc_desc (type, NULL,
10992 baton->data,
10993 baton->size,
10994 baton->per_cu);
10995 else if (bytes != NULL)
10996 {
10997 v = allocate_value (type);
10998 memcpy (value_contents_writeable (v), bytes,
10999 TYPE_LENGTH (type));
11000 }
11001 else
11002 v = value_from_longest (type, value);
11003
11004 /* Specify decimal so that we do not depend on
11005 the radix. */
11006 get_formatted_print_options (&opts, 'd');
11007 opts.raw = 1;
11008 value_print (v, &buf, &opts);
11009 release_value (v);
11010 }
11011 }
11012
11013 die->building_fullname = 0;
11014
11015 if (!first)
11016 {
11017 /* Close the argument list, with a space if necessary
11018 (nested templates). */
11019 if (!buf.empty () && buf.string ().back () == '>')
11020 buf.puts (" >");
11021 else
11022 buf.puts (">");
11023 }
11024 }
11025
11026 /* For C++ methods, append formal parameter type
11027 information, if PHYSNAME. */
11028
11029 if (physname && die->tag == DW_TAG_subprogram
11030 && cu->language == language_cplus)
11031 {
11032 struct type *type = read_type_die (die, cu);
11033
11034 c_type_print_args (type, &buf, 1, cu->language,
11035 &type_print_raw_options);
11036
11037 if (cu->language == language_cplus)
11038 {
11039 /* Assume that an artificial first parameter is
11040 "this", but do not crash if it is not. RealView
11041 marks unnamed (and thus unused) parameters as
11042 artificial; there is no way to differentiate
11043 the two cases. */
11044 if (TYPE_NFIELDS (type) > 0
11045 && TYPE_FIELD_ARTIFICIAL (type, 0)
11046 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
11047 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11048 0))))
11049 buf.puts (" const");
11050 }
11051 }
11052
11053 const std::string &intermediate_name = buf.string ();
11054
11055 if (cu->language == language_cplus)
11056 canonical_name
11057 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
11058 &objfile->per_bfd->storage_obstack);
11059
11060 /* If we only computed INTERMEDIATE_NAME, or if
11061 INTERMEDIATE_NAME is already canonical, then we need to
11062 copy it to the appropriate obstack. */
11063 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
11064 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11065 intermediate_name);
11066 else
11067 name = canonical_name;
11068 }
11069 }
11070
11071 return name;
11072 }
11073
11074 /* Return the fully qualified name of DIE, based on its DW_AT_name.
11075 If scope qualifiers are appropriate they will be added. The result
11076 will be allocated on the storage_obstack, or NULL if the DIE does
11077 not have a name. NAME may either be from a previous call to
11078 dwarf2_name or NULL.
11079
11080 The output string will be canonicalized (if C++). */
11081
11082 static const char *
11083 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11084 {
11085 return dwarf2_compute_name (name, die, cu, 0);
11086 }
11087
11088 /* Construct a physname for the given DIE in CU. NAME may either be
11089 from a previous call to dwarf2_name or NULL. The result will be
11090 allocated on the objfile_objstack or NULL if the DIE does not have a
11091 name.
11092
11093 The output string will be canonicalized (if C++). */
11094
11095 static const char *
11096 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
11097 {
11098 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11099 const char *retval, *mangled = NULL, *canon = NULL;
11100 int need_copy = 1;
11101
11102 /* In this case dwarf2_compute_name is just a shortcut not building anything
11103 on its own. */
11104 if (!die_needs_namespace (die, cu))
11105 return dwarf2_compute_name (name, die, cu, 1);
11106
11107 mangled = dw2_linkage_name (die, cu);
11108
11109 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11110 See https://github.com/rust-lang/rust/issues/32925. */
11111 if (cu->language == language_rust && mangled != NULL
11112 && strchr (mangled, '{') != NULL)
11113 mangled = NULL;
11114
11115 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11116 has computed. */
11117 gdb::unique_xmalloc_ptr<char> demangled;
11118 if (mangled != NULL)
11119 {
11120
11121 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11122 {
11123 /* Do nothing (do not demangle the symbol name). */
11124 }
11125 else if (cu->language == language_go)
11126 {
11127 /* This is a lie, but we already lie to the caller new_symbol.
11128 new_symbol assumes we return the mangled name.
11129 This just undoes that lie until things are cleaned up. */
11130 }
11131 else
11132 {
11133 /* Use DMGL_RET_DROP for C++ template functions to suppress
11134 their return type. It is easier for GDB users to search
11135 for such functions as `name(params)' than `long name(params)'.
11136 In such case the minimal symbol names do not match the full
11137 symbol names but for template functions there is never a need
11138 to look up their definition from their declaration so
11139 the only disadvantage remains the minimal symbol variant
11140 `long name(params)' does not have the proper inferior type. */
11141 demangled.reset (gdb_demangle (mangled,
11142 (DMGL_PARAMS | DMGL_ANSI
11143 | DMGL_RET_DROP)));
11144 }
11145 if (demangled)
11146 canon = demangled.get ();
11147 else
11148 {
11149 canon = mangled;
11150 need_copy = 0;
11151 }
11152 }
11153
11154 if (canon == NULL || check_physname)
11155 {
11156 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11157
11158 if (canon != NULL && strcmp (physname, canon) != 0)
11159 {
11160 /* It may not mean a bug in GDB. The compiler could also
11161 compute DW_AT_linkage_name incorrectly. But in such case
11162 GDB would need to be bug-to-bug compatible. */
11163
11164 complaint (_("Computed physname <%s> does not match demangled <%s> "
11165 "(from linkage <%s>) - DIE at %s [in module %s]"),
11166 physname, canon, mangled, sect_offset_str (die->sect_off),
11167 objfile_name (objfile));
11168
11169 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11170 is available here - over computed PHYSNAME. It is safer
11171 against both buggy GDB and buggy compilers. */
11172
11173 retval = canon;
11174 }
11175 else
11176 {
11177 retval = physname;
11178 need_copy = 0;
11179 }
11180 }
11181 else
11182 retval = canon;
11183
11184 if (need_copy)
11185 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
11186
11187 return retval;
11188 }
11189
11190 /* Inspect DIE in CU for a namespace alias. If one exists, record
11191 a new symbol for it.
11192
11193 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11194
11195 static int
11196 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11197 {
11198 struct attribute *attr;
11199
11200 /* If the die does not have a name, this is not a namespace
11201 alias. */
11202 attr = dwarf2_attr (die, DW_AT_name, cu);
11203 if (attr != NULL)
11204 {
11205 int num;
11206 struct die_info *d = die;
11207 struct dwarf2_cu *imported_cu = cu;
11208
11209 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11210 keep inspecting DIEs until we hit the underlying import. */
11211 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
11212 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11213 {
11214 attr = dwarf2_attr (d, DW_AT_import, cu);
11215 if (attr == NULL)
11216 break;
11217
11218 d = follow_die_ref (d, attr, &imported_cu);
11219 if (d->tag != DW_TAG_imported_declaration)
11220 break;
11221 }
11222
11223 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11224 {
11225 complaint (_("DIE at %s has too many recursively imported "
11226 "declarations"), sect_offset_str (d->sect_off));
11227 return 0;
11228 }
11229
11230 if (attr != NULL)
11231 {
11232 struct type *type;
11233 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
11234
11235 type = get_die_type_at_offset (sect_off, cu->per_cu);
11236 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11237 {
11238 /* This declaration is a global namespace alias. Add
11239 a symbol for it whose type is the aliased namespace. */
11240 new_symbol (die, type, cu);
11241 return 1;
11242 }
11243 }
11244 }
11245
11246 return 0;
11247 }
11248
11249 /* Return the using directives repository (global or local?) to use in the
11250 current context for CU.
11251
11252 For Ada, imported declarations can materialize renamings, which *may* be
11253 global. However it is impossible (for now?) in DWARF to distinguish
11254 "external" imported declarations and "static" ones. As all imported
11255 declarations seem to be static in all other languages, make them all CU-wide
11256 global only in Ada. */
11257
11258 static struct using_direct **
11259 using_directives (struct dwarf2_cu *cu)
11260 {
11261 if (cu->language == language_ada
11262 && cu->get_builder ()->outermost_context_p ())
11263 return cu->get_builder ()->get_global_using_directives ();
11264 else
11265 return cu->get_builder ()->get_local_using_directives ();
11266 }
11267
11268 /* Read the import statement specified by the given die and record it. */
11269
11270 static void
11271 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11272 {
11273 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
11274 struct attribute *import_attr;
11275 struct die_info *imported_die, *child_die;
11276 struct dwarf2_cu *imported_cu;
11277 const char *imported_name;
11278 const char *imported_name_prefix;
11279 const char *canonical_name;
11280 const char *import_alias;
11281 const char *imported_declaration = NULL;
11282 const char *import_prefix;
11283 std::vector<const char *> excludes;
11284
11285 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11286 if (import_attr == NULL)
11287 {
11288 complaint (_("Tag '%s' has no DW_AT_import"),
11289 dwarf_tag_name (die->tag));
11290 return;
11291 }
11292
11293 imported_cu = cu;
11294 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11295 imported_name = dwarf2_name (imported_die, imported_cu);
11296 if (imported_name == NULL)
11297 {
11298 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11299
11300 The import in the following code:
11301 namespace A
11302 {
11303 typedef int B;
11304 }
11305
11306 int main ()
11307 {
11308 using A::B;
11309 B b;
11310 return b;
11311 }
11312
11313 ...
11314 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11315 <52> DW_AT_decl_file : 1
11316 <53> DW_AT_decl_line : 6
11317 <54> DW_AT_import : <0x75>
11318 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11319 <59> DW_AT_name : B
11320 <5b> DW_AT_decl_file : 1
11321 <5c> DW_AT_decl_line : 2
11322 <5d> DW_AT_type : <0x6e>
11323 ...
11324 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11325 <76> DW_AT_byte_size : 4
11326 <77> DW_AT_encoding : 5 (signed)
11327
11328 imports the wrong die ( 0x75 instead of 0x58 ).
11329 This case will be ignored until the gcc bug is fixed. */
11330 return;
11331 }
11332
11333 /* Figure out the local name after import. */
11334 import_alias = dwarf2_name (die, cu);
11335
11336 /* Figure out where the statement is being imported to. */
11337 import_prefix = determine_prefix (die, cu);
11338
11339 /* Figure out what the scope of the imported die is and prepend it
11340 to the name of the imported die. */
11341 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11342
11343 if (imported_die->tag != DW_TAG_namespace
11344 && imported_die->tag != DW_TAG_module)
11345 {
11346 imported_declaration = imported_name;
11347 canonical_name = imported_name_prefix;
11348 }
11349 else if (strlen (imported_name_prefix) > 0)
11350 canonical_name = obconcat (&objfile->objfile_obstack,
11351 imported_name_prefix,
11352 (cu->language == language_d ? "." : "::"),
11353 imported_name, (char *) NULL);
11354 else
11355 canonical_name = imported_name;
11356
11357 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11358 for (child_die = die->child; child_die && child_die->tag;
11359 child_die = sibling_die (child_die))
11360 {
11361 /* DWARF-4: A Fortran use statement with a “rename list” may be
11362 represented by an imported module entry with an import attribute
11363 referring to the module and owned entries corresponding to those
11364 entities that are renamed as part of being imported. */
11365
11366 if (child_die->tag != DW_TAG_imported_declaration)
11367 {
11368 complaint (_("child DW_TAG_imported_declaration expected "
11369 "- DIE at %s [in module %s]"),
11370 sect_offset_str (child_die->sect_off),
11371 objfile_name (objfile));
11372 continue;
11373 }
11374
11375 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11376 if (import_attr == NULL)
11377 {
11378 complaint (_("Tag '%s' has no DW_AT_import"),
11379 dwarf_tag_name (child_die->tag));
11380 continue;
11381 }
11382
11383 imported_cu = cu;
11384 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11385 &imported_cu);
11386 imported_name = dwarf2_name (imported_die, imported_cu);
11387 if (imported_name == NULL)
11388 {
11389 complaint (_("child DW_TAG_imported_declaration has unknown "
11390 "imported name - DIE at %s [in module %s]"),
11391 sect_offset_str (child_die->sect_off),
11392 objfile_name (objfile));
11393 continue;
11394 }
11395
11396 excludes.push_back (imported_name);
11397
11398 process_die (child_die, cu);
11399 }
11400
11401 add_using_directive (using_directives (cu),
11402 import_prefix,
11403 canonical_name,
11404 import_alias,
11405 imported_declaration,
11406 excludes,
11407 0,
11408 &objfile->objfile_obstack);
11409 }
11410
11411 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11412 types, but gives them a size of zero. Starting with version 14,
11413 ICC is compatible with GCC. */
11414
11415 static bool
11416 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11417 {
11418 if (!cu->checked_producer)
11419 check_producer (cu);
11420
11421 return cu->producer_is_icc_lt_14;
11422 }
11423
11424 /* ICC generates a DW_AT_type for C void functions. This was observed on
11425 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11426 which says that void functions should not have a DW_AT_type. */
11427
11428 static bool
11429 producer_is_icc (struct dwarf2_cu *cu)
11430 {
11431 if (!cu->checked_producer)
11432 check_producer (cu);
11433
11434 return cu->producer_is_icc;
11435 }
11436
11437 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11438 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11439 this, it was first present in GCC release 4.3.0. */
11440
11441 static bool
11442 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11443 {
11444 if (!cu->checked_producer)
11445 check_producer (cu);
11446
11447 return cu->producer_is_gcc_lt_4_3;
11448 }
11449
11450 static file_and_directory
11451 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11452 {
11453 file_and_directory res;
11454
11455 /* Find the filename. Do not use dwarf2_name here, since the filename
11456 is not a source language identifier. */
11457 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11458 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11459
11460 if (res.comp_dir == NULL
11461 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11462 && IS_ABSOLUTE_PATH (res.name))
11463 {
11464 res.comp_dir_storage = ldirname (res.name);
11465 if (!res.comp_dir_storage.empty ())
11466 res.comp_dir = res.comp_dir_storage.c_str ();
11467 }
11468 if (res.comp_dir != NULL)
11469 {
11470 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11471 directory, get rid of it. */
11472 const char *cp = strchr (res.comp_dir, ':');
11473
11474 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11475 res.comp_dir = cp + 1;
11476 }
11477
11478 if (res.name == NULL)
11479 res.name = "<unknown>";
11480
11481 return res;
11482 }
11483
11484 /* Handle DW_AT_stmt_list for a compilation unit.
11485 DIE is the DW_TAG_compile_unit die for CU.
11486 COMP_DIR is the compilation directory. LOWPC is passed to
11487 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11488
11489 static void
11490 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11491 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11492 {
11493 struct dwarf2_per_objfile *dwarf2_per_objfile
11494 = cu->per_cu->dwarf2_per_objfile;
11495 struct objfile *objfile = dwarf2_per_objfile->objfile;
11496 struct attribute *attr;
11497 struct line_header line_header_local;
11498 hashval_t line_header_local_hash;
11499 void **slot;
11500 int decode_mapping;
11501
11502 gdb_assert (! cu->per_cu->is_debug_types);
11503
11504 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11505 if (attr == NULL)
11506 return;
11507
11508 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11509
11510 /* The line header hash table is only created if needed (it exists to
11511 prevent redundant reading of the line table for partial_units).
11512 If we're given a partial_unit, we'll need it. If we're given a
11513 compile_unit, then use the line header hash table if it's already
11514 created, but don't create one just yet. */
11515
11516 if (dwarf2_per_objfile->line_header_hash == NULL
11517 && die->tag == DW_TAG_partial_unit)
11518 {
11519 dwarf2_per_objfile->line_header_hash
11520 = htab_create_alloc_ex (127, line_header_hash_voidp,
11521 line_header_eq_voidp,
11522 free_line_header_voidp,
11523 &objfile->objfile_obstack,
11524 hashtab_obstack_allocate,
11525 dummy_obstack_deallocate);
11526 }
11527
11528 line_header_local.sect_off = line_offset;
11529 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11530 line_header_local_hash = line_header_hash (&line_header_local);
11531 if (dwarf2_per_objfile->line_header_hash != NULL)
11532 {
11533 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11534 &line_header_local,
11535 line_header_local_hash, NO_INSERT);
11536
11537 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11538 is not present in *SLOT (since if there is something in *SLOT then
11539 it will be for a partial_unit). */
11540 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11541 {
11542 gdb_assert (*slot != NULL);
11543 cu->line_header = (struct line_header *) *slot;
11544 return;
11545 }
11546 }
11547
11548 /* dwarf_decode_line_header does not yet provide sufficient information.
11549 We always have to call also dwarf_decode_lines for it. */
11550 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11551 if (lh == NULL)
11552 return;
11553
11554 cu->line_header = lh.release ();
11555 cu->line_header_die_owner = die;
11556
11557 if (dwarf2_per_objfile->line_header_hash == NULL)
11558 slot = NULL;
11559 else
11560 {
11561 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11562 &line_header_local,
11563 line_header_local_hash, INSERT);
11564 gdb_assert (slot != NULL);
11565 }
11566 if (slot != NULL && *slot == NULL)
11567 {
11568 /* This newly decoded line number information unit will be owned
11569 by line_header_hash hash table. */
11570 *slot = cu->line_header;
11571 cu->line_header_die_owner = NULL;
11572 }
11573 else
11574 {
11575 /* We cannot free any current entry in (*slot) as that struct line_header
11576 may be already used by multiple CUs. Create only temporary decoded
11577 line_header for this CU - it may happen at most once for each line
11578 number information unit. And if we're not using line_header_hash
11579 then this is what we want as well. */
11580 gdb_assert (die->tag != DW_TAG_partial_unit);
11581 }
11582 decode_mapping = (die->tag != DW_TAG_partial_unit);
11583 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11584 decode_mapping);
11585
11586 }
11587
11588 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11589
11590 static void
11591 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11592 {
11593 struct dwarf2_per_objfile *dwarf2_per_objfile
11594 = cu->per_cu->dwarf2_per_objfile;
11595 struct objfile *objfile = dwarf2_per_objfile->objfile;
11596 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11597 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11598 CORE_ADDR highpc = ((CORE_ADDR) 0);
11599 struct attribute *attr;
11600 struct die_info *child_die;
11601 CORE_ADDR baseaddr;
11602
11603 prepare_one_comp_unit (cu, die, cu->language);
11604 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
11605
11606 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11607
11608 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11609 from finish_block. */
11610 if (lowpc == ((CORE_ADDR) -1))
11611 lowpc = highpc;
11612 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11613
11614 file_and_directory fnd = find_file_and_directory (die, cu);
11615
11616 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11617 standardised yet. As a workaround for the language detection we fall
11618 back to the DW_AT_producer string. */
11619 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11620 cu->language = language_opencl;
11621
11622 /* Similar hack for Go. */
11623 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11624 set_cu_language (DW_LANG_Go, cu);
11625
11626 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11627
11628 /* Decode line number information if present. We do this before
11629 processing child DIEs, so that the line header table is available
11630 for DW_AT_decl_file. */
11631 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11632
11633 /* Process all dies in compilation unit. */
11634 if (die->child != NULL)
11635 {
11636 child_die = die->child;
11637 while (child_die && child_die->tag)
11638 {
11639 process_die (child_die, cu);
11640 child_die = sibling_die (child_die);
11641 }
11642 }
11643
11644 /* Decode macro information, if present. Dwarf 2 macro information
11645 refers to information in the line number info statement program
11646 header, so we can only read it if we've read the header
11647 successfully. */
11648 attr = dwarf2_attr (die, DW_AT_macros, cu);
11649 if (attr == NULL)
11650 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11651 if (attr && cu->line_header)
11652 {
11653 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11654 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11655
11656 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11657 }
11658 else
11659 {
11660 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11661 if (attr && cu->line_header)
11662 {
11663 unsigned int macro_offset = DW_UNSND (attr);
11664
11665 dwarf_decode_macros (cu, macro_offset, 0);
11666 }
11667 }
11668 }
11669
11670 void
11671 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11672 {
11673 struct type_unit_group *tu_group;
11674 int first_time;
11675 struct attribute *attr;
11676 unsigned int i;
11677 struct signatured_type *sig_type;
11678
11679 gdb_assert (per_cu->is_debug_types);
11680 sig_type = (struct signatured_type *) per_cu;
11681
11682 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11683
11684 /* If we're using .gdb_index (includes -readnow) then
11685 per_cu->type_unit_group may not have been set up yet. */
11686 if (sig_type->type_unit_group == NULL)
11687 sig_type->type_unit_group = get_type_unit_group (this, attr);
11688 tu_group = sig_type->type_unit_group;
11689
11690 /* If we've already processed this stmt_list there's no real need to
11691 do it again, we could fake it and just recreate the part we need
11692 (file name,index -> symtab mapping). If data shows this optimization
11693 is useful we can do it then. */
11694 first_time = tu_group->compunit_symtab == NULL;
11695
11696 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11697 debug info. */
11698 line_header_up lh;
11699 if (attr != NULL)
11700 {
11701 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11702 lh = dwarf_decode_line_header (line_offset, this);
11703 }
11704 if (lh == NULL)
11705 {
11706 if (first_time)
11707 start_symtab ("", NULL, 0);
11708 else
11709 {
11710 gdb_assert (tu_group->symtabs == NULL);
11711 gdb_assert (m_builder == nullptr);
11712 struct compunit_symtab *cust = tu_group->compunit_symtab;
11713 m_builder.reset (new struct buildsym_compunit
11714 (COMPUNIT_OBJFILE (cust), "",
11715 COMPUNIT_DIRNAME (cust),
11716 compunit_language (cust),
11717 0, cust));
11718 }
11719 return;
11720 }
11721
11722 line_header = lh.release ();
11723 line_header_die_owner = die;
11724
11725 if (first_time)
11726 {
11727 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11728
11729 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11730 still initializing it, and our caller (a few levels up)
11731 process_full_type_unit still needs to know if this is the first
11732 time. */
11733
11734 tu_group->num_symtabs = line_header->file_names_size ();
11735 tu_group->symtabs = XNEWVEC (struct symtab *,
11736 line_header->file_names_size ());
11737
11738 auto &file_names = line_header->file_names ();
11739 for (i = 0; i < file_names.size (); ++i)
11740 {
11741 file_entry &fe = file_names[i];
11742 dwarf2_start_subfile (this, fe.name,
11743 fe.include_dir (line_header));
11744 buildsym_compunit *b = get_builder ();
11745 if (b->get_current_subfile ()->symtab == NULL)
11746 {
11747 /* NOTE: start_subfile will recognize when it's been
11748 passed a file it has already seen. So we can't
11749 assume there's a simple mapping from
11750 cu->line_header->file_names to subfiles, plus
11751 cu->line_header->file_names may contain dups. */
11752 b->get_current_subfile ()->symtab
11753 = allocate_symtab (cust, b->get_current_subfile ()->name);
11754 }
11755
11756 fe.symtab = b->get_current_subfile ()->symtab;
11757 tu_group->symtabs[i] = fe.symtab;
11758 }
11759 }
11760 else
11761 {
11762 gdb_assert (m_builder == nullptr);
11763 struct compunit_symtab *cust = tu_group->compunit_symtab;
11764 m_builder.reset (new struct buildsym_compunit
11765 (COMPUNIT_OBJFILE (cust), "",
11766 COMPUNIT_DIRNAME (cust),
11767 compunit_language (cust),
11768 0, cust));
11769
11770 auto &file_names = line_header->file_names ();
11771 for (i = 0; i < file_names.size (); ++i)
11772 {
11773 file_entry &fe = file_names[i];
11774 fe.symtab = tu_group->symtabs[i];
11775 }
11776 }
11777
11778 /* The main symtab is allocated last. Type units don't have DW_AT_name
11779 so they don't have a "real" (so to speak) symtab anyway.
11780 There is later code that will assign the main symtab to all symbols
11781 that don't have one. We need to handle the case of a symbol with a
11782 missing symtab (DW_AT_decl_file) anyway. */
11783 }
11784
11785 /* Process DW_TAG_type_unit.
11786 For TUs we want to skip the first top level sibling if it's not the
11787 actual type being defined by this TU. In this case the first top
11788 level sibling is there to provide context only. */
11789
11790 static void
11791 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11792 {
11793 struct die_info *child_die;
11794
11795 prepare_one_comp_unit (cu, die, language_minimal);
11796
11797 /* Initialize (or reinitialize) the machinery for building symtabs.
11798 We do this before processing child DIEs, so that the line header table
11799 is available for DW_AT_decl_file. */
11800 cu->setup_type_unit_groups (die);
11801
11802 if (die->child != NULL)
11803 {
11804 child_die = die->child;
11805 while (child_die && child_die->tag)
11806 {
11807 process_die (child_die, cu);
11808 child_die = sibling_die (child_die);
11809 }
11810 }
11811 }
11812 \f
11813 /* DWO/DWP files.
11814
11815 http://gcc.gnu.org/wiki/DebugFission
11816 http://gcc.gnu.org/wiki/DebugFissionDWP
11817
11818 To simplify handling of both DWO files ("object" files with the DWARF info)
11819 and DWP files (a file with the DWOs packaged up into one file), we treat
11820 DWP files as having a collection of virtual DWO files. */
11821
11822 static hashval_t
11823 hash_dwo_file (const void *item)
11824 {
11825 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11826 hashval_t hash;
11827
11828 hash = htab_hash_string (dwo_file->dwo_name);
11829 if (dwo_file->comp_dir != NULL)
11830 hash += htab_hash_string (dwo_file->comp_dir);
11831 return hash;
11832 }
11833
11834 static int
11835 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11836 {
11837 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11838 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11839
11840 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11841 return 0;
11842 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11843 return lhs->comp_dir == rhs->comp_dir;
11844 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11845 }
11846
11847 /* Allocate a hash table for DWO files. */
11848
11849 static htab_up
11850 allocate_dwo_file_hash_table (struct objfile *objfile)
11851 {
11852 auto delete_dwo_file = [] (void *item)
11853 {
11854 struct dwo_file *dwo_file = (struct dwo_file *) item;
11855
11856 delete dwo_file;
11857 };
11858
11859 return htab_up (htab_create_alloc_ex (41,
11860 hash_dwo_file,
11861 eq_dwo_file,
11862 delete_dwo_file,
11863 &objfile->objfile_obstack,
11864 hashtab_obstack_allocate,
11865 dummy_obstack_deallocate));
11866 }
11867
11868 /* Lookup DWO file DWO_NAME. */
11869
11870 static void **
11871 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11872 const char *dwo_name,
11873 const char *comp_dir)
11874 {
11875 struct dwo_file find_entry;
11876 void **slot;
11877
11878 if (dwarf2_per_objfile->dwo_files == NULL)
11879 dwarf2_per_objfile->dwo_files
11880 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
11881
11882 find_entry.dwo_name = dwo_name;
11883 find_entry.comp_dir = comp_dir;
11884 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11885 INSERT);
11886
11887 return slot;
11888 }
11889
11890 static hashval_t
11891 hash_dwo_unit (const void *item)
11892 {
11893 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11894
11895 /* This drops the top 32 bits of the id, but is ok for a hash. */
11896 return dwo_unit->signature;
11897 }
11898
11899 static int
11900 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11901 {
11902 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11903 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11904
11905 /* The signature is assumed to be unique within the DWO file.
11906 So while object file CU dwo_id's always have the value zero,
11907 that's OK, assuming each object file DWO file has only one CU,
11908 and that's the rule for now. */
11909 return lhs->signature == rhs->signature;
11910 }
11911
11912 /* Allocate a hash table for DWO CUs,TUs.
11913 There is one of these tables for each of CUs,TUs for each DWO file. */
11914
11915 static htab_t
11916 allocate_dwo_unit_table (struct objfile *objfile)
11917 {
11918 /* Start out with a pretty small number.
11919 Generally DWO files contain only one CU and maybe some TUs. */
11920 return htab_create_alloc_ex (3,
11921 hash_dwo_unit,
11922 eq_dwo_unit,
11923 NULL,
11924 &objfile->objfile_obstack,
11925 hashtab_obstack_allocate,
11926 dummy_obstack_deallocate);
11927 }
11928
11929 /* die_reader_func for create_dwo_cu. */
11930
11931 static void
11932 create_dwo_cu_reader (const struct die_reader_specs *reader,
11933 const gdb_byte *info_ptr,
11934 struct die_info *comp_unit_die,
11935 int has_children,
11936 struct dwo_file *dwo_file,
11937 struct dwo_unit *dwo_unit)
11938 {
11939 struct dwarf2_cu *cu = reader->cu;
11940 sect_offset sect_off = cu->per_cu->sect_off;
11941 struct dwarf2_section_info *section = cu->per_cu->section;
11942
11943 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11944 if (!signature.has_value ())
11945 {
11946 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11947 " its dwo_id [in module %s]"),
11948 sect_offset_str (sect_off), dwo_file->dwo_name);
11949 return;
11950 }
11951
11952 dwo_unit->dwo_file = dwo_file;
11953 dwo_unit->signature = *signature;
11954 dwo_unit->section = section;
11955 dwo_unit->sect_off = sect_off;
11956 dwo_unit->length = cu->per_cu->length;
11957
11958 if (dwarf_read_debug)
11959 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11960 sect_offset_str (sect_off),
11961 hex_string (dwo_unit->signature));
11962 }
11963
11964 /* Create the dwo_units for the CUs in a DWO_FILE.
11965 Note: This function processes DWO files only, not DWP files. */
11966
11967 static void
11968 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11969 dwarf2_cu *cu, struct dwo_file &dwo_file,
11970 dwarf2_section_info &section, htab_t &cus_htab)
11971 {
11972 struct objfile *objfile = dwarf2_per_objfile->objfile;
11973 const gdb_byte *info_ptr, *end_ptr;
11974
11975 dwarf2_read_section (objfile, &section);
11976 info_ptr = section.buffer;
11977
11978 if (info_ptr == NULL)
11979 return;
11980
11981 if (dwarf_read_debug)
11982 {
11983 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11984 get_section_name (&section),
11985 get_section_file_name (&section));
11986 }
11987
11988 end_ptr = info_ptr + section.size;
11989 while (info_ptr < end_ptr)
11990 {
11991 struct dwarf2_per_cu_data per_cu;
11992 struct dwo_unit read_unit {};
11993 struct dwo_unit *dwo_unit;
11994 void **slot;
11995 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11996
11997 memset (&per_cu, 0, sizeof (per_cu));
11998 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11999 per_cu.is_debug_types = 0;
12000 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12001 per_cu.section = &section;
12002
12003 cutu_reader reader (&per_cu, cu, &dwo_file);
12004 if (!reader.dummy_p)
12005 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
12006 reader.has_children, &dwo_file, &read_unit);
12007 info_ptr += per_cu.length;
12008
12009 // If the unit could not be parsed, skip it.
12010 if (read_unit.dwo_file == NULL)
12011 continue;
12012
12013 if (cus_htab == NULL)
12014 cus_htab = allocate_dwo_unit_table (objfile);
12015
12016 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12017 *dwo_unit = read_unit;
12018 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12019 gdb_assert (slot != NULL);
12020 if (*slot != NULL)
12021 {
12022 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12023 sect_offset dup_sect_off = dup_cu->sect_off;
12024
12025 complaint (_("debug cu entry at offset %s is duplicate to"
12026 " the entry at offset %s, signature %s"),
12027 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
12028 hex_string (dwo_unit->signature));
12029 }
12030 *slot = (void *)dwo_unit;
12031 }
12032 }
12033
12034 /* DWP file .debug_{cu,tu}_index section format:
12035 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12036
12037 DWP Version 1:
12038
12039 Both index sections have the same format, and serve to map a 64-bit
12040 signature to a set of section numbers. Each section begins with a header,
12041 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12042 indexes, and a pool of 32-bit section numbers. The index sections will be
12043 aligned at 8-byte boundaries in the file.
12044
12045 The index section header consists of:
12046
12047 V, 32 bit version number
12048 -, 32 bits unused
12049 N, 32 bit number of compilation units or type units in the index
12050 M, 32 bit number of slots in the hash table
12051
12052 Numbers are recorded using the byte order of the application binary.
12053
12054 The hash table begins at offset 16 in the section, and consists of an array
12055 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12056 order of the application binary). Unused slots in the hash table are 0.
12057 (We rely on the extreme unlikeliness of a signature being exactly 0.)
12058
12059 The parallel table begins immediately after the hash table
12060 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12061 array of 32-bit indexes (using the byte order of the application binary),
12062 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12063 table contains a 32-bit index into the pool of section numbers. For unused
12064 hash table slots, the corresponding entry in the parallel table will be 0.
12065
12066 The pool of section numbers begins immediately following the hash table
12067 (at offset 16 + 12 * M from the beginning of the section). The pool of
12068 section numbers consists of an array of 32-bit words (using the byte order
12069 of the application binary). Each item in the array is indexed starting
12070 from 0. The hash table entry provides the index of the first section
12071 number in the set. Additional section numbers in the set follow, and the
12072 set is terminated by a 0 entry (section number 0 is not used in ELF).
12073
12074 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12075 section must be the first entry in the set, and the .debug_abbrev.dwo must
12076 be the second entry. Other members of the set may follow in any order.
12077
12078 ---
12079
12080 DWP Version 2:
12081
12082 DWP Version 2 combines all the .debug_info, etc. sections into one,
12083 and the entries in the index tables are now offsets into these sections.
12084 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12085 section.
12086
12087 Index Section Contents:
12088 Header
12089 Hash Table of Signatures dwp_hash_table.hash_table
12090 Parallel Table of Indices dwp_hash_table.unit_table
12091 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12092 Table of Section Sizes dwp_hash_table.v2.sizes
12093
12094 The index section header consists of:
12095
12096 V, 32 bit version number
12097 L, 32 bit number of columns in the table of section offsets
12098 N, 32 bit number of compilation units or type units in the index
12099 M, 32 bit number of slots in the hash table
12100
12101 Numbers are recorded using the byte order of the application binary.
12102
12103 The hash table has the same format as version 1.
12104 The parallel table of indices has the same format as version 1,
12105 except that the entries are origin-1 indices into the table of sections
12106 offsets and the table of section sizes.
12107
12108 The table of offsets begins immediately following the parallel table
12109 (at offset 16 + 12 * M from the beginning of the section). The table is
12110 a two-dimensional array of 32-bit words (using the byte order of the
12111 application binary), with L columns and N+1 rows, in row-major order.
12112 Each row in the array is indexed starting from 0. The first row provides
12113 a key to the remaining rows: each column in this row provides an identifier
12114 for a debug section, and the offsets in the same column of subsequent rows
12115 refer to that section. The section identifiers are:
12116
12117 DW_SECT_INFO 1 .debug_info.dwo
12118 DW_SECT_TYPES 2 .debug_types.dwo
12119 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12120 DW_SECT_LINE 4 .debug_line.dwo
12121 DW_SECT_LOC 5 .debug_loc.dwo
12122 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12123 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12124 DW_SECT_MACRO 8 .debug_macro.dwo
12125
12126 The offsets provided by the CU and TU index sections are the base offsets
12127 for the contributions made by each CU or TU to the corresponding section
12128 in the package file. Each CU and TU header contains an abbrev_offset
12129 field, used to find the abbreviations table for that CU or TU within the
12130 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12131 be interpreted as relative to the base offset given in the index section.
12132 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12133 should be interpreted as relative to the base offset for .debug_line.dwo,
12134 and offsets into other debug sections obtained from DWARF attributes should
12135 also be interpreted as relative to the corresponding base offset.
12136
12137 The table of sizes begins immediately following the table of offsets.
12138 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12139 with L columns and N rows, in row-major order. Each row in the array is
12140 indexed starting from 1 (row 0 is shared by the two tables).
12141
12142 ---
12143
12144 Hash table lookup is handled the same in version 1 and 2:
12145
12146 We assume that N and M will not exceed 2^32 - 1.
12147 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12148
12149 Given a 64-bit compilation unit signature or a type signature S, an entry
12150 in the hash table is located as follows:
12151
12152 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12153 the low-order k bits all set to 1.
12154
12155 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
12156
12157 3) If the hash table entry at index H matches the signature, use that
12158 entry. If the hash table entry at index H is unused (all zeroes),
12159 terminate the search: the signature is not present in the table.
12160
12161 4) Let H = (H + H') modulo M. Repeat at Step 3.
12162
12163 Because M > N and H' and M are relatively prime, the search is guaranteed
12164 to stop at an unused slot or find the match. */
12165
12166 /* Create a hash table to map DWO IDs to their CU/TU entry in
12167 .debug_{info,types}.dwo in DWP_FILE.
12168 Returns NULL if there isn't one.
12169 Note: This function processes DWP files only, not DWO files. */
12170
12171 static struct dwp_hash_table *
12172 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12173 struct dwp_file *dwp_file, int is_debug_types)
12174 {
12175 struct objfile *objfile = dwarf2_per_objfile->objfile;
12176 bfd *dbfd = dwp_file->dbfd.get ();
12177 const gdb_byte *index_ptr, *index_end;
12178 struct dwarf2_section_info *index;
12179 uint32_t version, nr_columns, nr_units, nr_slots;
12180 struct dwp_hash_table *htab;
12181
12182 if (is_debug_types)
12183 index = &dwp_file->sections.tu_index;
12184 else
12185 index = &dwp_file->sections.cu_index;
12186
12187 if (dwarf2_section_empty_p (index))
12188 return NULL;
12189 dwarf2_read_section (objfile, index);
12190
12191 index_ptr = index->buffer;
12192 index_end = index_ptr + index->size;
12193
12194 version = read_4_bytes (dbfd, index_ptr);
12195 index_ptr += 4;
12196 if (version == 2)
12197 nr_columns = read_4_bytes (dbfd, index_ptr);
12198 else
12199 nr_columns = 0;
12200 index_ptr += 4;
12201 nr_units = read_4_bytes (dbfd, index_ptr);
12202 index_ptr += 4;
12203 nr_slots = read_4_bytes (dbfd, index_ptr);
12204 index_ptr += 4;
12205
12206 if (version != 1 && version != 2)
12207 {
12208 error (_("Dwarf Error: unsupported DWP file version (%s)"
12209 " [in module %s]"),
12210 pulongest (version), dwp_file->name);
12211 }
12212 if (nr_slots != (nr_slots & -nr_slots))
12213 {
12214 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
12215 " is not power of 2 [in module %s]"),
12216 pulongest (nr_slots), dwp_file->name);
12217 }
12218
12219 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
12220 htab->version = version;
12221 htab->nr_columns = nr_columns;
12222 htab->nr_units = nr_units;
12223 htab->nr_slots = nr_slots;
12224 htab->hash_table = index_ptr;
12225 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
12226
12227 /* Exit early if the table is empty. */
12228 if (nr_slots == 0 || nr_units == 0
12229 || (version == 2 && nr_columns == 0))
12230 {
12231 /* All must be zero. */
12232 if (nr_slots != 0 || nr_units != 0
12233 || (version == 2 && nr_columns != 0))
12234 {
12235 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
12236 " all zero [in modules %s]"),
12237 dwp_file->name);
12238 }
12239 return htab;
12240 }
12241
12242 if (version == 1)
12243 {
12244 htab->section_pool.v1.indices =
12245 htab->unit_table + sizeof (uint32_t) * nr_slots;
12246 /* It's harder to decide whether the section is too small in v1.
12247 V1 is deprecated anyway so we punt. */
12248 }
12249 else
12250 {
12251 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12252 int *ids = htab->section_pool.v2.section_ids;
12253 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
12254 /* Reverse map for error checking. */
12255 int ids_seen[DW_SECT_MAX + 1];
12256 int i;
12257
12258 if (nr_columns < 2)
12259 {
12260 error (_("Dwarf Error: bad DWP hash table, too few columns"
12261 " in section table [in module %s]"),
12262 dwp_file->name);
12263 }
12264 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12265 {
12266 error (_("Dwarf Error: bad DWP hash table, too many columns"
12267 " in section table [in module %s]"),
12268 dwp_file->name);
12269 }
12270 memset (ids, 255, sizeof_ids);
12271 memset (ids_seen, 255, sizeof (ids_seen));
12272 for (i = 0; i < nr_columns; ++i)
12273 {
12274 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12275
12276 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12277 {
12278 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12279 " in section table [in module %s]"),
12280 id, dwp_file->name);
12281 }
12282 if (ids_seen[id] != -1)
12283 {
12284 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12285 " id %d in section table [in module %s]"),
12286 id, dwp_file->name);
12287 }
12288 ids_seen[id] = i;
12289 ids[i] = id;
12290 }
12291 /* Must have exactly one info or types section. */
12292 if (((ids_seen[DW_SECT_INFO] != -1)
12293 + (ids_seen[DW_SECT_TYPES] != -1))
12294 != 1)
12295 {
12296 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12297 " DWO info/types section [in module %s]"),
12298 dwp_file->name);
12299 }
12300 /* Must have an abbrev section. */
12301 if (ids_seen[DW_SECT_ABBREV] == -1)
12302 {
12303 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12304 " section [in module %s]"),
12305 dwp_file->name);
12306 }
12307 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12308 htab->section_pool.v2.sizes =
12309 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12310 * nr_units * nr_columns);
12311 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12312 * nr_units * nr_columns))
12313 > index_end)
12314 {
12315 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12316 " [in module %s]"),
12317 dwp_file->name);
12318 }
12319 }
12320
12321 return htab;
12322 }
12323
12324 /* Update SECTIONS with the data from SECTP.
12325
12326 This function is like the other "locate" section routines that are
12327 passed to bfd_map_over_sections, but in this context the sections to
12328 read comes from the DWP V1 hash table, not the full ELF section table.
12329
12330 The result is non-zero for success, or zero if an error was found. */
12331
12332 static int
12333 locate_v1_virtual_dwo_sections (asection *sectp,
12334 struct virtual_v1_dwo_sections *sections)
12335 {
12336 const struct dwop_section_names *names = &dwop_section_names;
12337
12338 if (section_is_p (sectp->name, &names->abbrev_dwo))
12339 {
12340 /* There can be only one. */
12341 if (sections->abbrev.s.section != NULL)
12342 return 0;
12343 sections->abbrev.s.section = sectp;
12344 sections->abbrev.size = bfd_section_size (sectp);
12345 }
12346 else if (section_is_p (sectp->name, &names->info_dwo)
12347 || section_is_p (sectp->name, &names->types_dwo))
12348 {
12349 /* There can be only one. */
12350 if (sections->info_or_types.s.section != NULL)
12351 return 0;
12352 sections->info_or_types.s.section = sectp;
12353 sections->info_or_types.size = bfd_section_size (sectp);
12354 }
12355 else if (section_is_p (sectp->name, &names->line_dwo))
12356 {
12357 /* There can be only one. */
12358 if (sections->line.s.section != NULL)
12359 return 0;
12360 sections->line.s.section = sectp;
12361 sections->line.size = bfd_section_size (sectp);
12362 }
12363 else if (section_is_p (sectp->name, &names->loc_dwo))
12364 {
12365 /* There can be only one. */
12366 if (sections->loc.s.section != NULL)
12367 return 0;
12368 sections->loc.s.section = sectp;
12369 sections->loc.size = bfd_section_size (sectp);
12370 }
12371 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12372 {
12373 /* There can be only one. */
12374 if (sections->macinfo.s.section != NULL)
12375 return 0;
12376 sections->macinfo.s.section = sectp;
12377 sections->macinfo.size = bfd_section_size (sectp);
12378 }
12379 else if (section_is_p (sectp->name, &names->macro_dwo))
12380 {
12381 /* There can be only one. */
12382 if (sections->macro.s.section != NULL)
12383 return 0;
12384 sections->macro.s.section = sectp;
12385 sections->macro.size = bfd_section_size (sectp);
12386 }
12387 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12388 {
12389 /* There can be only one. */
12390 if (sections->str_offsets.s.section != NULL)
12391 return 0;
12392 sections->str_offsets.s.section = sectp;
12393 sections->str_offsets.size = bfd_section_size (sectp);
12394 }
12395 else
12396 {
12397 /* No other kind of section is valid. */
12398 return 0;
12399 }
12400
12401 return 1;
12402 }
12403
12404 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12405 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12406 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12407 This is for DWP version 1 files. */
12408
12409 static struct dwo_unit *
12410 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12411 struct dwp_file *dwp_file,
12412 uint32_t unit_index,
12413 const char *comp_dir,
12414 ULONGEST signature, int is_debug_types)
12415 {
12416 struct objfile *objfile = dwarf2_per_objfile->objfile;
12417 const struct dwp_hash_table *dwp_htab =
12418 is_debug_types ? dwp_file->tus : dwp_file->cus;
12419 bfd *dbfd = dwp_file->dbfd.get ();
12420 const char *kind = is_debug_types ? "TU" : "CU";
12421 struct dwo_file *dwo_file;
12422 struct dwo_unit *dwo_unit;
12423 struct virtual_v1_dwo_sections sections;
12424 void **dwo_file_slot;
12425 int i;
12426
12427 gdb_assert (dwp_file->version == 1);
12428
12429 if (dwarf_read_debug)
12430 {
12431 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
12432 kind,
12433 pulongest (unit_index), hex_string (signature),
12434 dwp_file->name);
12435 }
12436
12437 /* Fetch the sections of this DWO unit.
12438 Put a limit on the number of sections we look for so that bad data
12439 doesn't cause us to loop forever. */
12440
12441 #define MAX_NR_V1_DWO_SECTIONS \
12442 (1 /* .debug_info or .debug_types */ \
12443 + 1 /* .debug_abbrev */ \
12444 + 1 /* .debug_line */ \
12445 + 1 /* .debug_loc */ \
12446 + 1 /* .debug_str_offsets */ \
12447 + 1 /* .debug_macro or .debug_macinfo */ \
12448 + 1 /* trailing zero */)
12449
12450 memset (&sections, 0, sizeof (sections));
12451
12452 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12453 {
12454 asection *sectp;
12455 uint32_t section_nr =
12456 read_4_bytes (dbfd,
12457 dwp_htab->section_pool.v1.indices
12458 + (unit_index + i) * sizeof (uint32_t));
12459
12460 if (section_nr == 0)
12461 break;
12462 if (section_nr >= dwp_file->num_sections)
12463 {
12464 error (_("Dwarf Error: bad DWP hash table, section number too large"
12465 " [in module %s]"),
12466 dwp_file->name);
12467 }
12468
12469 sectp = dwp_file->elf_sections[section_nr];
12470 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12471 {
12472 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12473 " [in module %s]"),
12474 dwp_file->name);
12475 }
12476 }
12477
12478 if (i < 2
12479 || dwarf2_section_empty_p (&sections.info_or_types)
12480 || dwarf2_section_empty_p (&sections.abbrev))
12481 {
12482 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12483 " [in module %s]"),
12484 dwp_file->name);
12485 }
12486 if (i == MAX_NR_V1_DWO_SECTIONS)
12487 {
12488 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12489 " [in module %s]"),
12490 dwp_file->name);
12491 }
12492
12493 /* It's easier for the rest of the code if we fake a struct dwo_file and
12494 have dwo_unit "live" in that. At least for now.
12495
12496 The DWP file can be made up of a random collection of CUs and TUs.
12497 However, for each CU + set of TUs that came from the same original DWO
12498 file, we can combine them back into a virtual DWO file to save space
12499 (fewer struct dwo_file objects to allocate). Remember that for really
12500 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12501
12502 std::string virtual_dwo_name =
12503 string_printf ("virtual-dwo/%d-%d-%d-%d",
12504 get_section_id (&sections.abbrev),
12505 get_section_id (&sections.line),
12506 get_section_id (&sections.loc),
12507 get_section_id (&sections.str_offsets));
12508 /* Can we use an existing virtual DWO file? */
12509 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12510 virtual_dwo_name.c_str (),
12511 comp_dir);
12512 /* Create one if necessary. */
12513 if (*dwo_file_slot == NULL)
12514 {
12515 if (dwarf_read_debug)
12516 {
12517 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12518 virtual_dwo_name.c_str ());
12519 }
12520 dwo_file = new struct dwo_file;
12521 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12522 virtual_dwo_name);
12523 dwo_file->comp_dir = comp_dir;
12524 dwo_file->sections.abbrev = sections.abbrev;
12525 dwo_file->sections.line = sections.line;
12526 dwo_file->sections.loc = sections.loc;
12527 dwo_file->sections.macinfo = sections.macinfo;
12528 dwo_file->sections.macro = sections.macro;
12529 dwo_file->sections.str_offsets = sections.str_offsets;
12530 /* The "str" section is global to the entire DWP file. */
12531 dwo_file->sections.str = dwp_file->sections.str;
12532 /* The info or types section is assigned below to dwo_unit,
12533 there's no need to record it in dwo_file.
12534 Also, we can't simply record type sections in dwo_file because
12535 we record a pointer into the vector in dwo_unit. As we collect more
12536 types we'll grow the vector and eventually have to reallocate space
12537 for it, invalidating all copies of pointers into the previous
12538 contents. */
12539 *dwo_file_slot = dwo_file;
12540 }
12541 else
12542 {
12543 if (dwarf_read_debug)
12544 {
12545 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12546 virtual_dwo_name.c_str ());
12547 }
12548 dwo_file = (struct dwo_file *) *dwo_file_slot;
12549 }
12550
12551 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12552 dwo_unit->dwo_file = dwo_file;
12553 dwo_unit->signature = signature;
12554 dwo_unit->section =
12555 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12556 *dwo_unit->section = sections.info_or_types;
12557 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12558
12559 return dwo_unit;
12560 }
12561
12562 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12563 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12564 piece within that section used by a TU/CU, return a virtual section
12565 of just that piece. */
12566
12567 static struct dwarf2_section_info
12568 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12569 struct dwarf2_section_info *section,
12570 bfd_size_type offset, bfd_size_type size)
12571 {
12572 struct dwarf2_section_info result;
12573 asection *sectp;
12574
12575 gdb_assert (section != NULL);
12576 gdb_assert (!section->is_virtual);
12577
12578 memset (&result, 0, sizeof (result));
12579 result.s.containing_section = section;
12580 result.is_virtual = true;
12581
12582 if (size == 0)
12583 return result;
12584
12585 sectp = get_section_bfd_section (section);
12586
12587 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12588 bounds of the real section. This is a pretty-rare event, so just
12589 flag an error (easier) instead of a warning and trying to cope. */
12590 if (sectp == NULL
12591 || offset + size > bfd_section_size (sectp))
12592 {
12593 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12594 " in section %s [in module %s]"),
12595 sectp ? bfd_section_name (sectp) : "<unknown>",
12596 objfile_name (dwarf2_per_objfile->objfile));
12597 }
12598
12599 result.virtual_offset = offset;
12600 result.size = size;
12601 return result;
12602 }
12603
12604 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12605 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12606 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12607 This is for DWP version 2 files. */
12608
12609 static struct dwo_unit *
12610 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12611 struct dwp_file *dwp_file,
12612 uint32_t unit_index,
12613 const char *comp_dir,
12614 ULONGEST signature, int is_debug_types)
12615 {
12616 struct objfile *objfile = dwarf2_per_objfile->objfile;
12617 const struct dwp_hash_table *dwp_htab =
12618 is_debug_types ? dwp_file->tus : dwp_file->cus;
12619 bfd *dbfd = dwp_file->dbfd.get ();
12620 const char *kind = is_debug_types ? "TU" : "CU";
12621 struct dwo_file *dwo_file;
12622 struct dwo_unit *dwo_unit;
12623 struct virtual_v2_dwo_sections sections;
12624 void **dwo_file_slot;
12625 int i;
12626
12627 gdb_assert (dwp_file->version == 2);
12628
12629 if (dwarf_read_debug)
12630 {
12631 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12632 kind,
12633 pulongest (unit_index), hex_string (signature),
12634 dwp_file->name);
12635 }
12636
12637 /* Fetch the section offsets of this DWO unit. */
12638
12639 memset (&sections, 0, sizeof (sections));
12640
12641 for (i = 0; i < dwp_htab->nr_columns; ++i)
12642 {
12643 uint32_t offset = read_4_bytes (dbfd,
12644 dwp_htab->section_pool.v2.offsets
12645 + (((unit_index - 1) * dwp_htab->nr_columns
12646 + i)
12647 * sizeof (uint32_t)));
12648 uint32_t size = read_4_bytes (dbfd,
12649 dwp_htab->section_pool.v2.sizes
12650 + (((unit_index - 1) * dwp_htab->nr_columns
12651 + i)
12652 * sizeof (uint32_t)));
12653
12654 switch (dwp_htab->section_pool.v2.section_ids[i])
12655 {
12656 case DW_SECT_INFO:
12657 case DW_SECT_TYPES:
12658 sections.info_or_types_offset = offset;
12659 sections.info_or_types_size = size;
12660 break;
12661 case DW_SECT_ABBREV:
12662 sections.abbrev_offset = offset;
12663 sections.abbrev_size = size;
12664 break;
12665 case DW_SECT_LINE:
12666 sections.line_offset = offset;
12667 sections.line_size = size;
12668 break;
12669 case DW_SECT_LOC:
12670 sections.loc_offset = offset;
12671 sections.loc_size = size;
12672 break;
12673 case DW_SECT_STR_OFFSETS:
12674 sections.str_offsets_offset = offset;
12675 sections.str_offsets_size = size;
12676 break;
12677 case DW_SECT_MACINFO:
12678 sections.macinfo_offset = offset;
12679 sections.macinfo_size = size;
12680 break;
12681 case DW_SECT_MACRO:
12682 sections.macro_offset = offset;
12683 sections.macro_size = size;
12684 break;
12685 }
12686 }
12687
12688 /* It's easier for the rest of the code if we fake a struct dwo_file and
12689 have dwo_unit "live" in that. At least for now.
12690
12691 The DWP file can be made up of a random collection of CUs and TUs.
12692 However, for each CU + set of TUs that came from the same original DWO
12693 file, we can combine them back into a virtual DWO file to save space
12694 (fewer struct dwo_file objects to allocate). Remember that for really
12695 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12696
12697 std::string virtual_dwo_name =
12698 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12699 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12700 (long) (sections.line_size ? sections.line_offset : 0),
12701 (long) (sections.loc_size ? sections.loc_offset : 0),
12702 (long) (sections.str_offsets_size
12703 ? sections.str_offsets_offset : 0));
12704 /* Can we use an existing virtual DWO file? */
12705 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12706 virtual_dwo_name.c_str (),
12707 comp_dir);
12708 /* Create one if necessary. */
12709 if (*dwo_file_slot == NULL)
12710 {
12711 if (dwarf_read_debug)
12712 {
12713 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12714 virtual_dwo_name.c_str ());
12715 }
12716 dwo_file = new struct dwo_file;
12717 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12718 virtual_dwo_name);
12719 dwo_file->comp_dir = comp_dir;
12720 dwo_file->sections.abbrev =
12721 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12722 sections.abbrev_offset, sections.abbrev_size);
12723 dwo_file->sections.line =
12724 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12725 sections.line_offset, sections.line_size);
12726 dwo_file->sections.loc =
12727 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12728 sections.loc_offset, sections.loc_size);
12729 dwo_file->sections.macinfo =
12730 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12731 sections.macinfo_offset, sections.macinfo_size);
12732 dwo_file->sections.macro =
12733 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12734 sections.macro_offset, sections.macro_size);
12735 dwo_file->sections.str_offsets =
12736 create_dwp_v2_section (dwarf2_per_objfile,
12737 &dwp_file->sections.str_offsets,
12738 sections.str_offsets_offset,
12739 sections.str_offsets_size);
12740 /* The "str" section is global to the entire DWP file. */
12741 dwo_file->sections.str = dwp_file->sections.str;
12742 /* The info or types section is assigned below to dwo_unit,
12743 there's no need to record it in dwo_file.
12744 Also, we can't simply record type sections in dwo_file because
12745 we record a pointer into the vector in dwo_unit. As we collect more
12746 types we'll grow the vector and eventually have to reallocate space
12747 for it, invalidating all copies of pointers into the previous
12748 contents. */
12749 *dwo_file_slot = dwo_file;
12750 }
12751 else
12752 {
12753 if (dwarf_read_debug)
12754 {
12755 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12756 virtual_dwo_name.c_str ());
12757 }
12758 dwo_file = (struct dwo_file *) *dwo_file_slot;
12759 }
12760
12761 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12762 dwo_unit->dwo_file = dwo_file;
12763 dwo_unit->signature = signature;
12764 dwo_unit->section =
12765 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
12766 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12767 is_debug_types
12768 ? &dwp_file->sections.types
12769 : &dwp_file->sections.info,
12770 sections.info_or_types_offset,
12771 sections.info_or_types_size);
12772 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12773
12774 return dwo_unit;
12775 }
12776
12777 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12778 Returns NULL if the signature isn't found. */
12779
12780 static struct dwo_unit *
12781 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12782 struct dwp_file *dwp_file, const char *comp_dir,
12783 ULONGEST signature, int is_debug_types)
12784 {
12785 const struct dwp_hash_table *dwp_htab =
12786 is_debug_types ? dwp_file->tus : dwp_file->cus;
12787 bfd *dbfd = dwp_file->dbfd.get ();
12788 uint32_t mask = dwp_htab->nr_slots - 1;
12789 uint32_t hash = signature & mask;
12790 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12791 unsigned int i;
12792 void **slot;
12793 struct dwo_unit find_dwo_cu;
12794
12795 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12796 find_dwo_cu.signature = signature;
12797 slot = htab_find_slot (is_debug_types
12798 ? dwp_file->loaded_tus
12799 : dwp_file->loaded_cus,
12800 &find_dwo_cu, INSERT);
12801
12802 if (*slot != NULL)
12803 return (struct dwo_unit *) *slot;
12804
12805 /* Use a for loop so that we don't loop forever on bad debug info. */
12806 for (i = 0; i < dwp_htab->nr_slots; ++i)
12807 {
12808 ULONGEST signature_in_table;
12809
12810 signature_in_table =
12811 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12812 if (signature_in_table == signature)
12813 {
12814 uint32_t unit_index =
12815 read_4_bytes (dbfd,
12816 dwp_htab->unit_table + hash * sizeof (uint32_t));
12817
12818 if (dwp_file->version == 1)
12819 {
12820 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12821 dwp_file, unit_index,
12822 comp_dir, signature,
12823 is_debug_types);
12824 }
12825 else
12826 {
12827 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12828 dwp_file, unit_index,
12829 comp_dir, signature,
12830 is_debug_types);
12831 }
12832 return (struct dwo_unit *) *slot;
12833 }
12834 if (signature_in_table == 0)
12835 return NULL;
12836 hash = (hash + hash2) & mask;
12837 }
12838
12839 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12840 " [in module %s]"),
12841 dwp_file->name);
12842 }
12843
12844 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12845 Open the file specified by FILE_NAME and hand it off to BFD for
12846 preliminary analysis. Return a newly initialized bfd *, which
12847 includes a canonicalized copy of FILE_NAME.
12848 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12849 SEARCH_CWD is true if the current directory is to be searched.
12850 It will be searched before debug-file-directory.
12851 If successful, the file is added to the bfd include table of the
12852 objfile's bfd (see gdb_bfd_record_inclusion).
12853 If unable to find/open the file, return NULL.
12854 NOTE: This function is derived from symfile_bfd_open. */
12855
12856 static gdb_bfd_ref_ptr
12857 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12858 const char *file_name, int is_dwp, int search_cwd)
12859 {
12860 int desc;
12861 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12862 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12863 to debug_file_directory. */
12864 const char *search_path;
12865 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12866
12867 gdb::unique_xmalloc_ptr<char> search_path_holder;
12868 if (search_cwd)
12869 {
12870 if (*debug_file_directory != '\0')
12871 {
12872 search_path_holder.reset (concat (".", dirname_separator_string,
12873 debug_file_directory,
12874 (char *) NULL));
12875 search_path = search_path_holder.get ();
12876 }
12877 else
12878 search_path = ".";
12879 }
12880 else
12881 search_path = debug_file_directory;
12882
12883 openp_flags flags = OPF_RETURN_REALPATH;
12884 if (is_dwp)
12885 flags |= OPF_SEARCH_IN_PATH;
12886
12887 gdb::unique_xmalloc_ptr<char> absolute_name;
12888 desc = openp (search_path, flags, file_name,
12889 O_RDONLY | O_BINARY, &absolute_name);
12890 if (desc < 0)
12891 return NULL;
12892
12893 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12894 gnutarget, desc));
12895 if (sym_bfd == NULL)
12896 return NULL;
12897 bfd_set_cacheable (sym_bfd.get (), 1);
12898
12899 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12900 return NULL;
12901
12902 /* Success. Record the bfd as having been included by the objfile's bfd.
12903 This is important because things like demangled_names_hash lives in the
12904 objfile's per_bfd space and may have references to things like symbol
12905 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12906 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12907
12908 return sym_bfd;
12909 }
12910
12911 /* Try to open DWO file FILE_NAME.
12912 COMP_DIR is the DW_AT_comp_dir attribute.
12913 The result is the bfd handle of the file.
12914 If there is a problem finding or opening the file, return NULL.
12915 Upon success, the canonicalized path of the file is stored in the bfd,
12916 same as symfile_bfd_open. */
12917
12918 static gdb_bfd_ref_ptr
12919 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12920 const char *file_name, const char *comp_dir)
12921 {
12922 if (IS_ABSOLUTE_PATH (file_name))
12923 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12924 0 /*is_dwp*/, 0 /*search_cwd*/);
12925
12926 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12927
12928 if (comp_dir != NULL)
12929 {
12930 gdb::unique_xmalloc_ptr<char> path_to_try
12931 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12932
12933 /* NOTE: If comp_dir is a relative path, this will also try the
12934 search path, which seems useful. */
12935 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12936 path_to_try.get (),
12937 0 /*is_dwp*/,
12938 1 /*search_cwd*/));
12939 if (abfd != NULL)
12940 return abfd;
12941 }
12942
12943 /* That didn't work, try debug-file-directory, which, despite its name,
12944 is a list of paths. */
12945
12946 if (*debug_file_directory == '\0')
12947 return NULL;
12948
12949 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12950 0 /*is_dwp*/, 1 /*search_cwd*/);
12951 }
12952
12953 /* This function is mapped across the sections and remembers the offset and
12954 size of each of the DWO debugging sections we are interested in. */
12955
12956 static void
12957 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12958 {
12959 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12960 const struct dwop_section_names *names = &dwop_section_names;
12961
12962 if (section_is_p (sectp->name, &names->abbrev_dwo))
12963 {
12964 dwo_sections->abbrev.s.section = sectp;
12965 dwo_sections->abbrev.size = bfd_section_size (sectp);
12966 }
12967 else if (section_is_p (sectp->name, &names->info_dwo))
12968 {
12969 dwo_sections->info.s.section = sectp;
12970 dwo_sections->info.size = bfd_section_size (sectp);
12971 }
12972 else if (section_is_p (sectp->name, &names->line_dwo))
12973 {
12974 dwo_sections->line.s.section = sectp;
12975 dwo_sections->line.size = bfd_section_size (sectp);
12976 }
12977 else if (section_is_p (sectp->name, &names->loc_dwo))
12978 {
12979 dwo_sections->loc.s.section = sectp;
12980 dwo_sections->loc.size = bfd_section_size (sectp);
12981 }
12982 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12983 {
12984 dwo_sections->macinfo.s.section = sectp;
12985 dwo_sections->macinfo.size = bfd_section_size (sectp);
12986 }
12987 else if (section_is_p (sectp->name, &names->macro_dwo))
12988 {
12989 dwo_sections->macro.s.section = sectp;
12990 dwo_sections->macro.size = bfd_section_size (sectp);
12991 }
12992 else if (section_is_p (sectp->name, &names->str_dwo))
12993 {
12994 dwo_sections->str.s.section = sectp;
12995 dwo_sections->str.size = bfd_section_size (sectp);
12996 }
12997 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12998 {
12999 dwo_sections->str_offsets.s.section = sectp;
13000 dwo_sections->str_offsets.size = bfd_section_size (sectp);
13001 }
13002 else if (section_is_p (sectp->name, &names->types_dwo))
13003 {
13004 struct dwarf2_section_info type_section;
13005
13006 memset (&type_section, 0, sizeof (type_section));
13007 type_section.s.section = sectp;
13008 type_section.size = bfd_section_size (sectp);
13009 dwo_sections->types.push_back (type_section);
13010 }
13011 }
13012
13013 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
13014 by PER_CU. This is for the non-DWP case.
13015 The result is NULL if DWO_NAME can't be found. */
13016
13017 static struct dwo_file *
13018 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13019 const char *dwo_name, const char *comp_dir)
13020 {
13021 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
13022
13023 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
13024 if (dbfd == NULL)
13025 {
13026 if (dwarf_read_debug)
13027 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13028 return NULL;
13029 }
13030
13031 dwo_file_up dwo_file (new struct dwo_file);
13032 dwo_file->dwo_name = dwo_name;
13033 dwo_file->comp_dir = comp_dir;
13034 dwo_file->dbfd = std::move (dbfd);
13035
13036 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
13037 &dwo_file->sections);
13038
13039 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
13040 dwo_file->sections.info, dwo_file->cus);
13041
13042 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
13043 dwo_file->sections.types, dwo_file->tus);
13044
13045 if (dwarf_read_debug)
13046 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13047
13048 return dwo_file.release ();
13049 }
13050
13051 /* This function is mapped across the sections and remembers the offset and
13052 size of each of the DWP debugging sections common to version 1 and 2 that
13053 we are interested in. */
13054
13055 static void
13056 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13057 void *dwp_file_ptr)
13058 {
13059 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13060 const struct dwop_section_names *names = &dwop_section_names;
13061 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13062
13063 /* Record the ELF section number for later lookup: this is what the
13064 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13065 gdb_assert (elf_section_nr < dwp_file->num_sections);
13066 dwp_file->elf_sections[elf_section_nr] = sectp;
13067
13068 /* Look for specific sections that we need. */
13069 if (section_is_p (sectp->name, &names->str_dwo))
13070 {
13071 dwp_file->sections.str.s.section = sectp;
13072 dwp_file->sections.str.size = bfd_section_size (sectp);
13073 }
13074 else if (section_is_p (sectp->name, &names->cu_index))
13075 {
13076 dwp_file->sections.cu_index.s.section = sectp;
13077 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13078 }
13079 else if (section_is_p (sectp->name, &names->tu_index))
13080 {
13081 dwp_file->sections.tu_index.s.section = sectp;
13082 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13083 }
13084 }
13085
13086 /* This function is mapped across the sections and remembers the offset and
13087 size of each of the DWP version 2 debugging sections that we are interested
13088 in. This is split into a separate function because we don't know if we
13089 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13090
13091 static void
13092 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13093 {
13094 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13095 const struct dwop_section_names *names = &dwop_section_names;
13096 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13097
13098 /* Record the ELF section number for later lookup: this is what the
13099 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13100 gdb_assert (elf_section_nr < dwp_file->num_sections);
13101 dwp_file->elf_sections[elf_section_nr] = sectp;
13102
13103 /* Look for specific sections that we need. */
13104 if (section_is_p (sectp->name, &names->abbrev_dwo))
13105 {
13106 dwp_file->sections.abbrev.s.section = sectp;
13107 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13108 }
13109 else if (section_is_p (sectp->name, &names->info_dwo))
13110 {
13111 dwp_file->sections.info.s.section = sectp;
13112 dwp_file->sections.info.size = bfd_section_size (sectp);
13113 }
13114 else if (section_is_p (sectp->name, &names->line_dwo))
13115 {
13116 dwp_file->sections.line.s.section = sectp;
13117 dwp_file->sections.line.size = bfd_section_size (sectp);
13118 }
13119 else if (section_is_p (sectp->name, &names->loc_dwo))
13120 {
13121 dwp_file->sections.loc.s.section = sectp;
13122 dwp_file->sections.loc.size = bfd_section_size (sectp);
13123 }
13124 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13125 {
13126 dwp_file->sections.macinfo.s.section = sectp;
13127 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13128 }
13129 else if (section_is_p (sectp->name, &names->macro_dwo))
13130 {
13131 dwp_file->sections.macro.s.section = sectp;
13132 dwp_file->sections.macro.size = bfd_section_size (sectp);
13133 }
13134 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13135 {
13136 dwp_file->sections.str_offsets.s.section = sectp;
13137 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13138 }
13139 else if (section_is_p (sectp->name, &names->types_dwo))
13140 {
13141 dwp_file->sections.types.s.section = sectp;
13142 dwp_file->sections.types.size = bfd_section_size (sectp);
13143 }
13144 }
13145
13146 /* Hash function for dwp_file loaded CUs/TUs. */
13147
13148 static hashval_t
13149 hash_dwp_loaded_cutus (const void *item)
13150 {
13151 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13152
13153 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13154 return dwo_unit->signature;
13155 }
13156
13157 /* Equality function for dwp_file loaded CUs/TUs. */
13158
13159 static int
13160 eq_dwp_loaded_cutus (const void *a, const void *b)
13161 {
13162 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13163 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13164
13165 return dua->signature == dub->signature;
13166 }
13167
13168 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13169
13170 static htab_t
13171 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13172 {
13173 return htab_create_alloc_ex (3,
13174 hash_dwp_loaded_cutus,
13175 eq_dwp_loaded_cutus,
13176 NULL,
13177 &objfile->objfile_obstack,
13178 hashtab_obstack_allocate,
13179 dummy_obstack_deallocate);
13180 }
13181
13182 /* Try to open DWP file FILE_NAME.
13183 The result is the bfd handle of the file.
13184 If there is a problem finding or opening the file, return NULL.
13185 Upon success, the canonicalized path of the file is stored in the bfd,
13186 same as symfile_bfd_open. */
13187
13188 static gdb_bfd_ref_ptr
13189 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13190 const char *file_name)
13191 {
13192 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13193 1 /*is_dwp*/,
13194 1 /*search_cwd*/));
13195 if (abfd != NULL)
13196 return abfd;
13197
13198 /* Work around upstream bug 15652.
13199 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13200 [Whether that's a "bug" is debatable, but it is getting in our way.]
13201 We have no real idea where the dwp file is, because gdb's realpath-ing
13202 of the executable's path may have discarded the needed info.
13203 [IWBN if the dwp file name was recorded in the executable, akin to
13204 .gnu_debuglink, but that doesn't exist yet.]
13205 Strip the directory from FILE_NAME and search again. */
13206 if (*debug_file_directory != '\0')
13207 {
13208 /* Don't implicitly search the current directory here.
13209 If the user wants to search "." to handle this case,
13210 it must be added to debug-file-directory. */
13211 return try_open_dwop_file (dwarf2_per_objfile,
13212 lbasename (file_name), 1 /*is_dwp*/,
13213 0 /*search_cwd*/);
13214 }
13215
13216 return NULL;
13217 }
13218
13219 /* Initialize the use of the DWP file for the current objfile.
13220 By convention the name of the DWP file is ${objfile}.dwp.
13221 The result is NULL if it can't be found. */
13222
13223 static std::unique_ptr<struct dwp_file>
13224 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13225 {
13226 struct objfile *objfile = dwarf2_per_objfile->objfile;
13227
13228 /* Try to find first .dwp for the binary file before any symbolic links
13229 resolving. */
13230
13231 /* If the objfile is a debug file, find the name of the real binary
13232 file and get the name of dwp file from there. */
13233 std::string dwp_name;
13234 if (objfile->separate_debug_objfile_backlink != NULL)
13235 {
13236 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13237 const char *backlink_basename = lbasename (backlink->original_name);
13238
13239 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13240 }
13241 else
13242 dwp_name = objfile->original_name;
13243
13244 dwp_name += ".dwp";
13245
13246 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
13247 if (dbfd == NULL
13248 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13249 {
13250 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13251 dwp_name = objfile_name (objfile);
13252 dwp_name += ".dwp";
13253 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
13254 }
13255
13256 if (dbfd == NULL)
13257 {
13258 if (dwarf_read_debug)
13259 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
13260 return std::unique_ptr<dwp_file> ();
13261 }
13262
13263 const char *name = bfd_get_filename (dbfd.get ());
13264 std::unique_ptr<struct dwp_file> dwp_file
13265 (new struct dwp_file (name, std::move (dbfd)));
13266
13267 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13268 dwp_file->elf_sections =
13269 OBSTACK_CALLOC (&objfile->objfile_obstack,
13270 dwp_file->num_sections, asection *);
13271
13272 bfd_map_over_sections (dwp_file->dbfd.get (),
13273 dwarf2_locate_common_dwp_sections,
13274 dwp_file.get ());
13275
13276 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13277 0);
13278
13279 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13280 1);
13281
13282 /* The DWP file version is stored in the hash table. Oh well. */
13283 if (dwp_file->cus && dwp_file->tus
13284 && dwp_file->cus->version != dwp_file->tus->version)
13285 {
13286 /* Technically speaking, we should try to limp along, but this is
13287 pretty bizarre. We use pulongest here because that's the established
13288 portability solution (e.g, we cannot use %u for uint32_t). */
13289 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13290 " TU version %s [in DWP file %s]"),
13291 pulongest (dwp_file->cus->version),
13292 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13293 }
13294
13295 if (dwp_file->cus)
13296 dwp_file->version = dwp_file->cus->version;
13297 else if (dwp_file->tus)
13298 dwp_file->version = dwp_file->tus->version;
13299 else
13300 dwp_file->version = 2;
13301
13302 if (dwp_file->version == 2)
13303 bfd_map_over_sections (dwp_file->dbfd.get (),
13304 dwarf2_locate_v2_dwp_sections,
13305 dwp_file.get ());
13306
13307 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13308 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
13309
13310 if (dwarf_read_debug)
13311 {
13312 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13313 fprintf_unfiltered (gdb_stdlog,
13314 " %s CUs, %s TUs\n",
13315 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13316 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13317 }
13318
13319 return dwp_file;
13320 }
13321
13322 /* Wrapper around open_and_init_dwp_file, only open it once. */
13323
13324 static struct dwp_file *
13325 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
13326 {
13327 if (! dwarf2_per_objfile->dwp_checked)
13328 {
13329 dwarf2_per_objfile->dwp_file
13330 = open_and_init_dwp_file (dwarf2_per_objfile);
13331 dwarf2_per_objfile->dwp_checked = 1;
13332 }
13333 return dwarf2_per_objfile->dwp_file.get ();
13334 }
13335
13336 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13337 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13338 or in the DWP file for the objfile, referenced by THIS_UNIT.
13339 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13340 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13341
13342 This is called, for example, when wanting to read a variable with a
13343 complex location. Therefore we don't want to do file i/o for every call.
13344 Therefore we don't want to look for a DWO file on every call.
13345 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13346 then we check if we've already seen DWO_NAME, and only THEN do we check
13347 for a DWO file.
13348
13349 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13350 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13351
13352 static struct dwo_unit *
13353 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13354 const char *dwo_name, const char *comp_dir,
13355 ULONGEST signature, int is_debug_types)
13356 {
13357 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
13358 struct objfile *objfile = dwarf2_per_objfile->objfile;
13359 const char *kind = is_debug_types ? "TU" : "CU";
13360 void **dwo_file_slot;
13361 struct dwo_file *dwo_file;
13362 struct dwp_file *dwp_file;
13363
13364 /* First see if there's a DWP file.
13365 If we have a DWP file but didn't find the DWO inside it, don't
13366 look for the original DWO file. It makes gdb behave differently
13367 depending on whether one is debugging in the build tree. */
13368
13369 dwp_file = get_dwp_file (dwarf2_per_objfile);
13370 if (dwp_file != NULL)
13371 {
13372 const struct dwp_hash_table *dwp_htab =
13373 is_debug_types ? dwp_file->tus : dwp_file->cus;
13374
13375 if (dwp_htab != NULL)
13376 {
13377 struct dwo_unit *dwo_cutu =
13378 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
13379 signature, is_debug_types);
13380
13381 if (dwo_cutu != NULL)
13382 {
13383 if (dwarf_read_debug)
13384 {
13385 fprintf_unfiltered (gdb_stdlog,
13386 "Virtual DWO %s %s found: @%s\n",
13387 kind, hex_string (signature),
13388 host_address_to_string (dwo_cutu));
13389 }
13390 return dwo_cutu;
13391 }
13392 }
13393 }
13394 else
13395 {
13396 /* No DWP file, look for the DWO file. */
13397
13398 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13399 dwo_name, comp_dir);
13400 if (*dwo_file_slot == NULL)
13401 {
13402 /* Read in the file and build a table of the CUs/TUs it contains. */
13403 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
13404 }
13405 /* NOTE: This will be NULL if unable to open the file. */
13406 dwo_file = (struct dwo_file *) *dwo_file_slot;
13407
13408 if (dwo_file != NULL)
13409 {
13410 struct dwo_unit *dwo_cutu = NULL;
13411
13412 if (is_debug_types && dwo_file->tus)
13413 {
13414 struct dwo_unit find_dwo_cutu;
13415
13416 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13417 find_dwo_cutu.signature = signature;
13418 dwo_cutu
13419 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
13420 }
13421 else if (!is_debug_types && dwo_file->cus)
13422 {
13423 struct dwo_unit find_dwo_cutu;
13424
13425 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13426 find_dwo_cutu.signature = signature;
13427 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13428 &find_dwo_cutu);
13429 }
13430
13431 if (dwo_cutu != NULL)
13432 {
13433 if (dwarf_read_debug)
13434 {
13435 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13436 kind, dwo_name, hex_string (signature),
13437 host_address_to_string (dwo_cutu));
13438 }
13439 return dwo_cutu;
13440 }
13441 }
13442 }
13443
13444 /* We didn't find it. This could mean a dwo_id mismatch, or
13445 someone deleted the DWO/DWP file, or the search path isn't set up
13446 correctly to find the file. */
13447
13448 if (dwarf_read_debug)
13449 {
13450 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13451 kind, dwo_name, hex_string (signature));
13452 }
13453
13454 /* This is a warning and not a complaint because it can be caused by
13455 pilot error (e.g., user accidentally deleting the DWO). */
13456 {
13457 /* Print the name of the DWP file if we looked there, helps the user
13458 better diagnose the problem. */
13459 std::string dwp_text;
13460
13461 if (dwp_file != NULL)
13462 dwp_text = string_printf (" [in DWP file %s]",
13463 lbasename (dwp_file->name));
13464
13465 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13466 " [in module %s]"),
13467 kind, dwo_name, hex_string (signature),
13468 dwp_text.c_str (),
13469 this_unit->is_debug_types ? "TU" : "CU",
13470 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
13471 }
13472 return NULL;
13473 }
13474
13475 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13476 See lookup_dwo_cutu_unit for details. */
13477
13478 static struct dwo_unit *
13479 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13480 const char *dwo_name, const char *comp_dir,
13481 ULONGEST signature)
13482 {
13483 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13484 }
13485
13486 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13487 See lookup_dwo_cutu_unit for details. */
13488
13489 static struct dwo_unit *
13490 lookup_dwo_type_unit (struct signatured_type *this_tu,
13491 const char *dwo_name, const char *comp_dir)
13492 {
13493 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13494 }
13495
13496 /* Traversal function for queue_and_load_all_dwo_tus. */
13497
13498 static int
13499 queue_and_load_dwo_tu (void **slot, void *info)
13500 {
13501 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13502 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13503 ULONGEST signature = dwo_unit->signature;
13504 struct signatured_type *sig_type =
13505 lookup_dwo_signatured_type (per_cu->cu, signature);
13506
13507 if (sig_type != NULL)
13508 {
13509 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13510
13511 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13512 a real dependency of PER_CU on SIG_TYPE. That is detected later
13513 while processing PER_CU. */
13514 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13515 load_full_type_unit (sig_cu);
13516 per_cu->imported_symtabs_push (sig_cu);
13517 }
13518
13519 return 1;
13520 }
13521
13522 /* Queue all TUs contained in the DWO of PER_CU to be read in.
13523 The DWO may have the only definition of the type, though it may not be
13524 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13525 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13526
13527 static void
13528 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13529 {
13530 struct dwo_unit *dwo_unit;
13531 struct dwo_file *dwo_file;
13532
13533 gdb_assert (!per_cu->is_debug_types);
13534 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
13535 gdb_assert (per_cu->cu != NULL);
13536
13537 dwo_unit = per_cu->cu->dwo_unit;
13538 gdb_assert (dwo_unit != NULL);
13539
13540 dwo_file = dwo_unit->dwo_file;
13541 if (dwo_file->tus != NULL)
13542 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13543 }
13544
13545 /* Read in various DIEs. */
13546
13547 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13548 Inherit only the children of the DW_AT_abstract_origin DIE not being
13549 already referenced by DW_AT_abstract_origin from the children of the
13550 current DIE. */
13551
13552 static void
13553 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13554 {
13555 struct die_info *child_die;
13556 sect_offset *offsetp;
13557 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13558 struct die_info *origin_die;
13559 /* Iterator of the ORIGIN_DIE children. */
13560 struct die_info *origin_child_die;
13561 struct attribute *attr;
13562 struct dwarf2_cu *origin_cu;
13563 struct pending **origin_previous_list_in_scope;
13564
13565 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13566 if (!attr)
13567 return;
13568
13569 /* Note that following die references may follow to a die in a
13570 different cu. */
13571
13572 origin_cu = cu;
13573 origin_die = follow_die_ref (die, attr, &origin_cu);
13574
13575 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13576 symbols in. */
13577 origin_previous_list_in_scope = origin_cu->list_in_scope;
13578 origin_cu->list_in_scope = cu->list_in_scope;
13579
13580 if (die->tag != origin_die->tag
13581 && !(die->tag == DW_TAG_inlined_subroutine
13582 && origin_die->tag == DW_TAG_subprogram))
13583 complaint (_("DIE %s and its abstract origin %s have different tags"),
13584 sect_offset_str (die->sect_off),
13585 sect_offset_str (origin_die->sect_off));
13586
13587 std::vector<sect_offset> offsets;
13588
13589 for (child_die = die->child;
13590 child_die && child_die->tag;
13591 child_die = sibling_die (child_die))
13592 {
13593 struct die_info *child_origin_die;
13594 struct dwarf2_cu *child_origin_cu;
13595
13596 /* We are trying to process concrete instance entries:
13597 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13598 it's not relevant to our analysis here. i.e. detecting DIEs that are
13599 present in the abstract instance but not referenced in the concrete
13600 one. */
13601 if (child_die->tag == DW_TAG_call_site
13602 || child_die->tag == DW_TAG_GNU_call_site)
13603 continue;
13604
13605 /* For each CHILD_DIE, find the corresponding child of
13606 ORIGIN_DIE. If there is more than one layer of
13607 DW_AT_abstract_origin, follow them all; there shouldn't be,
13608 but GCC versions at least through 4.4 generate this (GCC PR
13609 40573). */
13610 child_origin_die = child_die;
13611 child_origin_cu = cu;
13612 while (1)
13613 {
13614 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13615 child_origin_cu);
13616 if (attr == NULL)
13617 break;
13618 child_origin_die = follow_die_ref (child_origin_die, attr,
13619 &child_origin_cu);
13620 }
13621
13622 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13623 counterpart may exist. */
13624 if (child_origin_die != child_die)
13625 {
13626 if (child_die->tag != child_origin_die->tag
13627 && !(child_die->tag == DW_TAG_inlined_subroutine
13628 && child_origin_die->tag == DW_TAG_subprogram))
13629 complaint (_("Child DIE %s and its abstract origin %s have "
13630 "different tags"),
13631 sect_offset_str (child_die->sect_off),
13632 sect_offset_str (child_origin_die->sect_off));
13633 if (child_origin_die->parent != origin_die)
13634 complaint (_("Child DIE %s and its abstract origin %s have "
13635 "different parents"),
13636 sect_offset_str (child_die->sect_off),
13637 sect_offset_str (child_origin_die->sect_off));
13638 else
13639 offsets.push_back (child_origin_die->sect_off);
13640 }
13641 }
13642 std::sort (offsets.begin (), offsets.end ());
13643 sect_offset *offsets_end = offsets.data () + offsets.size ();
13644 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13645 if (offsetp[-1] == *offsetp)
13646 complaint (_("Multiple children of DIE %s refer "
13647 "to DIE %s as their abstract origin"),
13648 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13649
13650 offsetp = offsets.data ();
13651 origin_child_die = origin_die->child;
13652 while (origin_child_die && origin_child_die->tag)
13653 {
13654 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13655 while (offsetp < offsets_end
13656 && *offsetp < origin_child_die->sect_off)
13657 offsetp++;
13658 if (offsetp >= offsets_end
13659 || *offsetp > origin_child_die->sect_off)
13660 {
13661 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13662 Check whether we're already processing ORIGIN_CHILD_DIE.
13663 This can happen with mutually referenced abstract_origins.
13664 PR 16581. */
13665 if (!origin_child_die->in_process)
13666 process_die (origin_child_die, origin_cu);
13667 }
13668 origin_child_die = sibling_die (origin_child_die);
13669 }
13670 origin_cu->list_in_scope = origin_previous_list_in_scope;
13671
13672 if (cu != origin_cu)
13673 compute_delayed_physnames (origin_cu);
13674 }
13675
13676 static void
13677 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13678 {
13679 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13680 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13681 struct context_stack *newobj;
13682 CORE_ADDR lowpc;
13683 CORE_ADDR highpc;
13684 struct die_info *child_die;
13685 struct attribute *attr, *call_line, *call_file;
13686 const char *name;
13687 CORE_ADDR baseaddr;
13688 struct block *block;
13689 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13690 std::vector<struct symbol *> template_args;
13691 struct template_symbol *templ_func = NULL;
13692
13693 if (inlined_func)
13694 {
13695 /* If we do not have call site information, we can't show the
13696 caller of this inlined function. That's too confusing, so
13697 only use the scope for local variables. */
13698 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13699 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13700 if (call_line == NULL || call_file == NULL)
13701 {
13702 read_lexical_block_scope (die, cu);
13703 return;
13704 }
13705 }
13706
13707 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
13708
13709 name = dwarf2_name (die, cu);
13710
13711 /* Ignore functions with missing or empty names. These are actually
13712 illegal according to the DWARF standard. */
13713 if (name == NULL)
13714 {
13715 complaint (_("missing name for subprogram DIE at %s"),
13716 sect_offset_str (die->sect_off));
13717 return;
13718 }
13719
13720 /* Ignore functions with missing or invalid low and high pc attributes. */
13721 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13722 <= PC_BOUNDS_INVALID)
13723 {
13724 attr = dwarf2_attr (die, DW_AT_external, cu);
13725 if (!attr || !DW_UNSND (attr))
13726 complaint (_("cannot get low and high bounds "
13727 "for subprogram DIE at %s"),
13728 sect_offset_str (die->sect_off));
13729 return;
13730 }
13731
13732 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13733 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13734
13735 /* If we have any template arguments, then we must allocate a
13736 different sort of symbol. */
13737 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13738 {
13739 if (child_die->tag == DW_TAG_template_type_param
13740 || child_die->tag == DW_TAG_template_value_param)
13741 {
13742 templ_func = allocate_template_symbol (objfile);
13743 templ_func->subclass = SYMBOL_TEMPLATE;
13744 break;
13745 }
13746 }
13747
13748 newobj = cu->get_builder ()->push_context (0, lowpc);
13749 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13750 (struct symbol *) templ_func);
13751
13752 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13753 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13754 cu->language);
13755
13756 /* If there is a location expression for DW_AT_frame_base, record
13757 it. */
13758 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13759 if (attr != nullptr)
13760 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13761
13762 /* If there is a location for the static link, record it. */
13763 newobj->static_link = NULL;
13764 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13765 if (attr != nullptr)
13766 {
13767 newobj->static_link
13768 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13769 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13770 dwarf2_per_cu_addr_type (cu->per_cu));
13771 }
13772
13773 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13774
13775 if (die->child != NULL)
13776 {
13777 child_die = die->child;
13778 while (child_die && child_die->tag)
13779 {
13780 if (child_die->tag == DW_TAG_template_type_param
13781 || child_die->tag == DW_TAG_template_value_param)
13782 {
13783 struct symbol *arg = new_symbol (child_die, NULL, cu);
13784
13785 if (arg != NULL)
13786 template_args.push_back (arg);
13787 }
13788 else
13789 process_die (child_die, cu);
13790 child_die = sibling_die (child_die);
13791 }
13792 }
13793
13794 inherit_abstract_dies (die, cu);
13795
13796 /* If we have a DW_AT_specification, we might need to import using
13797 directives from the context of the specification DIE. See the
13798 comment in determine_prefix. */
13799 if (cu->language == language_cplus
13800 && dwarf2_attr (die, DW_AT_specification, cu))
13801 {
13802 struct dwarf2_cu *spec_cu = cu;
13803 struct die_info *spec_die = die_specification (die, &spec_cu);
13804
13805 while (spec_die)
13806 {
13807 child_die = spec_die->child;
13808 while (child_die && child_die->tag)
13809 {
13810 if (child_die->tag == DW_TAG_imported_module)
13811 process_die (child_die, spec_cu);
13812 child_die = sibling_die (child_die);
13813 }
13814
13815 /* In some cases, GCC generates specification DIEs that
13816 themselves contain DW_AT_specification attributes. */
13817 spec_die = die_specification (spec_die, &spec_cu);
13818 }
13819 }
13820
13821 struct context_stack cstk = cu->get_builder ()->pop_context ();
13822 /* Make a block for the local symbols within. */
13823 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13824 cstk.static_link, lowpc, highpc);
13825
13826 /* For C++, set the block's scope. */
13827 if ((cu->language == language_cplus
13828 || cu->language == language_fortran
13829 || cu->language == language_d
13830 || cu->language == language_rust)
13831 && cu->processing_has_namespace_info)
13832 block_set_scope (block, determine_prefix (die, cu),
13833 &objfile->objfile_obstack);
13834
13835 /* If we have address ranges, record them. */
13836 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13837
13838 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13839
13840 /* Attach template arguments to function. */
13841 if (!template_args.empty ())
13842 {
13843 gdb_assert (templ_func != NULL);
13844
13845 templ_func->n_template_arguments = template_args.size ();
13846 templ_func->template_arguments
13847 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13848 templ_func->n_template_arguments);
13849 memcpy (templ_func->template_arguments,
13850 template_args.data (),
13851 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13852
13853 /* Make sure that the symtab is set on the new symbols. Even
13854 though they don't appear in this symtab directly, other parts
13855 of gdb assume that symbols do, and this is reasonably
13856 true. */
13857 for (symbol *sym : template_args)
13858 symbol_set_symtab (sym, symbol_symtab (templ_func));
13859 }
13860
13861 /* In C++, we can have functions nested inside functions (e.g., when
13862 a function declares a class that has methods). This means that
13863 when we finish processing a function scope, we may need to go
13864 back to building a containing block's symbol lists. */
13865 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13866 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13867
13868 /* If we've finished processing a top-level function, subsequent
13869 symbols go in the file symbol list. */
13870 if (cu->get_builder ()->outermost_context_p ())
13871 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13872 }
13873
13874 /* Process all the DIES contained within a lexical block scope. Start
13875 a new scope, process the dies, and then close the scope. */
13876
13877 static void
13878 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13879 {
13880 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13881 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13882 CORE_ADDR lowpc, highpc;
13883 struct die_info *child_die;
13884 CORE_ADDR baseaddr;
13885
13886 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
13887
13888 /* Ignore blocks with missing or invalid low and high pc attributes. */
13889 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13890 as multiple lexical blocks? Handling children in a sane way would
13891 be nasty. Might be easier to properly extend generic blocks to
13892 describe ranges. */
13893 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13894 {
13895 case PC_BOUNDS_NOT_PRESENT:
13896 /* DW_TAG_lexical_block has no attributes, process its children as if
13897 there was no wrapping by that DW_TAG_lexical_block.
13898 GCC does no longer produces such DWARF since GCC r224161. */
13899 for (child_die = die->child;
13900 child_die != NULL && child_die->tag;
13901 child_die = sibling_die (child_die))
13902 process_die (child_die, cu);
13903 return;
13904 case PC_BOUNDS_INVALID:
13905 return;
13906 }
13907 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13908 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13909
13910 cu->get_builder ()->push_context (0, lowpc);
13911 if (die->child != NULL)
13912 {
13913 child_die = die->child;
13914 while (child_die && child_die->tag)
13915 {
13916 process_die (child_die, cu);
13917 child_die = sibling_die (child_die);
13918 }
13919 }
13920 inherit_abstract_dies (die, cu);
13921 struct context_stack cstk = cu->get_builder ()->pop_context ();
13922
13923 if (*cu->get_builder ()->get_local_symbols () != NULL
13924 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13925 {
13926 struct block *block
13927 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13928 cstk.start_addr, highpc);
13929
13930 /* Note that recording ranges after traversing children, as we
13931 do here, means that recording a parent's ranges entails
13932 walking across all its children's ranges as they appear in
13933 the address map, which is quadratic behavior.
13934
13935 It would be nicer to record the parent's ranges before
13936 traversing its children, simply overriding whatever you find
13937 there. But since we don't even decide whether to create a
13938 block until after we've traversed its children, that's hard
13939 to do. */
13940 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13941 }
13942 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13943 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13944 }
13945
13946 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13947
13948 static void
13949 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13950 {
13951 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13953 CORE_ADDR pc, baseaddr;
13954 struct attribute *attr;
13955 struct call_site *call_site, call_site_local;
13956 void **slot;
13957 int nparams;
13958 struct die_info *child_die;
13959
13960 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
13961
13962 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13963 if (attr == NULL)
13964 {
13965 /* This was a pre-DWARF-5 GNU extension alias
13966 for DW_AT_call_return_pc. */
13967 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13968 }
13969 if (!attr)
13970 {
13971 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13972 "DIE %s [in module %s]"),
13973 sect_offset_str (die->sect_off), objfile_name (objfile));
13974 return;
13975 }
13976 pc = attr_value_as_address (attr) + baseaddr;
13977 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13978
13979 if (cu->call_site_htab == NULL)
13980 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13981 NULL, &objfile->objfile_obstack,
13982 hashtab_obstack_allocate, NULL);
13983 call_site_local.pc = pc;
13984 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13985 if (*slot != NULL)
13986 {
13987 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13988 "DIE %s [in module %s]"),
13989 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13990 objfile_name (objfile));
13991 return;
13992 }
13993
13994 /* Count parameters at the caller. */
13995
13996 nparams = 0;
13997 for (child_die = die->child; child_die && child_die->tag;
13998 child_die = sibling_die (child_die))
13999 {
14000 if (child_die->tag != DW_TAG_call_site_parameter
14001 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14002 {
14003 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
14004 "DW_TAG_call_site child DIE %s [in module %s]"),
14005 child_die->tag, sect_offset_str (child_die->sect_off),
14006 objfile_name (objfile));
14007 continue;
14008 }
14009
14010 nparams++;
14011 }
14012
14013 call_site
14014 = ((struct call_site *)
14015 obstack_alloc (&objfile->objfile_obstack,
14016 sizeof (*call_site)
14017 + (sizeof (*call_site->parameter) * (nparams - 1))));
14018 *slot = call_site;
14019 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14020 call_site->pc = pc;
14021
14022 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14023 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14024 {
14025 struct die_info *func_die;
14026
14027 /* Skip also over DW_TAG_inlined_subroutine. */
14028 for (func_die = die->parent;
14029 func_die && func_die->tag != DW_TAG_subprogram
14030 && func_die->tag != DW_TAG_subroutine_type;
14031 func_die = func_die->parent);
14032
14033 /* DW_AT_call_all_calls is a superset
14034 of DW_AT_call_all_tail_calls. */
14035 if (func_die
14036 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14037 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14038 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14039 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14040 {
14041 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14042 not complete. But keep CALL_SITE for look ups via call_site_htab,
14043 both the initial caller containing the real return address PC and
14044 the final callee containing the current PC of a chain of tail
14045 calls do not need to have the tail call list complete. But any
14046 function candidate for a virtual tail call frame searched via
14047 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14048 determined unambiguously. */
14049 }
14050 else
14051 {
14052 struct type *func_type = NULL;
14053
14054 if (func_die)
14055 func_type = get_die_type (func_die, cu);
14056 if (func_type != NULL)
14057 {
14058 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14059
14060 /* Enlist this call site to the function. */
14061 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14062 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14063 }
14064 else
14065 complaint (_("Cannot find function owning DW_TAG_call_site "
14066 "DIE %s [in module %s]"),
14067 sect_offset_str (die->sect_off), objfile_name (objfile));
14068 }
14069 }
14070
14071 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14072 if (attr == NULL)
14073 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14074 if (attr == NULL)
14075 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14076 if (attr == NULL)
14077 {
14078 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14079 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14080 }
14081 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14082 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14083 /* Keep NULL DWARF_BLOCK. */;
14084 else if (attr_form_is_block (attr))
14085 {
14086 struct dwarf2_locexpr_baton *dlbaton;
14087
14088 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14089 dlbaton->data = DW_BLOCK (attr)->data;
14090 dlbaton->size = DW_BLOCK (attr)->size;
14091 dlbaton->per_cu = cu->per_cu;
14092
14093 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14094 }
14095 else if (attr_form_is_ref (attr))
14096 {
14097 struct dwarf2_cu *target_cu = cu;
14098 struct die_info *target_die;
14099
14100 target_die = follow_die_ref (die, attr, &target_cu);
14101 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
14102 if (die_is_declaration (target_die, target_cu))
14103 {
14104 const char *target_physname;
14105
14106 /* Prefer the mangled name; otherwise compute the demangled one. */
14107 target_physname = dw2_linkage_name (target_die, target_cu);
14108 if (target_physname == NULL)
14109 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14110 if (target_physname == NULL)
14111 complaint (_("DW_AT_call_target target DIE has invalid "
14112 "physname, for referencing DIE %s [in module %s]"),
14113 sect_offset_str (die->sect_off), objfile_name (objfile));
14114 else
14115 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14116 }
14117 else
14118 {
14119 CORE_ADDR lowpc;
14120
14121 /* DW_AT_entry_pc should be preferred. */
14122 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14123 <= PC_BOUNDS_INVALID)
14124 complaint (_("DW_AT_call_target target DIE has invalid "
14125 "low pc, for referencing DIE %s [in module %s]"),
14126 sect_offset_str (die->sect_off), objfile_name (objfile));
14127 else
14128 {
14129 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14130 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14131 }
14132 }
14133 }
14134 else
14135 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14136 "block nor reference, for DIE %s [in module %s]"),
14137 sect_offset_str (die->sect_off), objfile_name (objfile));
14138
14139 call_site->per_cu = cu->per_cu;
14140
14141 for (child_die = die->child;
14142 child_die && child_die->tag;
14143 child_die = sibling_die (child_die))
14144 {
14145 struct call_site_parameter *parameter;
14146 struct attribute *loc, *origin;
14147
14148 if (child_die->tag != DW_TAG_call_site_parameter
14149 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14150 {
14151 /* Already printed the complaint above. */
14152 continue;
14153 }
14154
14155 gdb_assert (call_site->parameter_count < nparams);
14156 parameter = &call_site->parameter[call_site->parameter_count];
14157
14158 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14159 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14160 register is contained in DW_AT_call_value. */
14161
14162 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14163 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14164 if (origin == NULL)
14165 {
14166 /* This was a pre-DWARF-5 GNU extension alias
14167 for DW_AT_call_parameter. */
14168 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14169 }
14170 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
14171 {
14172 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14173
14174 sect_offset sect_off
14175 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14176 if (!offset_in_cu_p (&cu->header, sect_off))
14177 {
14178 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14179 binding can be done only inside one CU. Such referenced DIE
14180 therefore cannot be even moved to DW_TAG_partial_unit. */
14181 complaint (_("DW_AT_call_parameter offset is not in CU for "
14182 "DW_TAG_call_site child DIE %s [in module %s]"),
14183 sect_offset_str (child_die->sect_off),
14184 objfile_name (objfile));
14185 continue;
14186 }
14187 parameter->u.param_cu_off
14188 = (cu_offset) (sect_off - cu->header.sect_off);
14189 }
14190 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
14191 {
14192 complaint (_("No DW_FORM_block* DW_AT_location for "
14193 "DW_TAG_call_site child DIE %s [in module %s]"),
14194 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14195 continue;
14196 }
14197 else
14198 {
14199 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14200 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14201 if (parameter->u.dwarf_reg != -1)
14202 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14203 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14204 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14205 &parameter->u.fb_offset))
14206 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14207 else
14208 {
14209 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14210 "for DW_FORM_block* DW_AT_location is supported for "
14211 "DW_TAG_call_site child DIE %s "
14212 "[in module %s]"),
14213 sect_offset_str (child_die->sect_off),
14214 objfile_name (objfile));
14215 continue;
14216 }
14217 }
14218
14219 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14220 if (attr == NULL)
14221 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14222 if (!attr_form_is_block (attr))
14223 {
14224 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14225 "DW_TAG_call_site child DIE %s [in module %s]"),
14226 sect_offset_str (child_die->sect_off),
14227 objfile_name (objfile));
14228 continue;
14229 }
14230 parameter->value = DW_BLOCK (attr)->data;
14231 parameter->value_size = DW_BLOCK (attr)->size;
14232
14233 /* Parameters are not pre-cleared by memset above. */
14234 parameter->data_value = NULL;
14235 parameter->data_value_size = 0;
14236 call_site->parameter_count++;
14237
14238 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14239 if (attr == NULL)
14240 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14241 if (attr != nullptr)
14242 {
14243 if (!attr_form_is_block (attr))
14244 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14245 "DW_TAG_call_site child DIE %s [in module %s]"),
14246 sect_offset_str (child_die->sect_off),
14247 objfile_name (objfile));
14248 else
14249 {
14250 parameter->data_value = DW_BLOCK (attr)->data;
14251 parameter->data_value_size = DW_BLOCK (attr)->size;
14252 }
14253 }
14254 }
14255 }
14256
14257 /* Helper function for read_variable. If DIE represents a virtual
14258 table, then return the type of the concrete object that is
14259 associated with the virtual table. Otherwise, return NULL. */
14260
14261 static struct type *
14262 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14263 {
14264 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14265 if (attr == NULL)
14266 return NULL;
14267
14268 /* Find the type DIE. */
14269 struct die_info *type_die = NULL;
14270 struct dwarf2_cu *type_cu = cu;
14271
14272 if (attr_form_is_ref (attr))
14273 type_die = follow_die_ref (die, attr, &type_cu);
14274 if (type_die == NULL)
14275 return NULL;
14276
14277 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14278 return NULL;
14279 return die_containing_type (type_die, type_cu);
14280 }
14281
14282 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14283
14284 static void
14285 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14286 {
14287 struct rust_vtable_symbol *storage = NULL;
14288
14289 if (cu->language == language_rust)
14290 {
14291 struct type *containing_type = rust_containing_type (die, cu);
14292
14293 if (containing_type != NULL)
14294 {
14295 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14296
14297 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
14298 initialize_objfile_symbol (storage);
14299 storage->concrete_type = containing_type;
14300 storage->subclass = SYMBOL_RUST_VTABLE;
14301 }
14302 }
14303
14304 struct symbol *res = new_symbol (die, NULL, cu, storage);
14305 struct attribute *abstract_origin
14306 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14307 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14308 if (res == NULL && loc && abstract_origin)
14309 {
14310 /* We have a variable without a name, but with a location and an abstract
14311 origin. This may be a concrete instance of an abstract variable
14312 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14313 later. */
14314 struct dwarf2_cu *origin_cu = cu;
14315 struct die_info *origin_die
14316 = follow_die_ref (die, abstract_origin, &origin_cu);
14317 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
14318 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
14319 }
14320 }
14321
14322 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14323 reading .debug_rnglists.
14324 Callback's type should be:
14325 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14326 Return true if the attributes are present and valid, otherwise,
14327 return false. */
14328
14329 template <typename Callback>
14330 static bool
14331 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14332 Callback &&callback)
14333 {
14334 struct dwarf2_per_objfile *dwarf2_per_objfile
14335 = cu->per_cu->dwarf2_per_objfile;
14336 struct objfile *objfile = dwarf2_per_objfile->objfile;
14337 bfd *obfd = objfile->obfd;
14338 /* Base address selection entry. */
14339 CORE_ADDR base;
14340 int found_base;
14341 const gdb_byte *buffer;
14342 CORE_ADDR baseaddr;
14343 bool overflow = false;
14344
14345 found_base = cu->base_known;
14346 base = cu->base_address;
14347
14348 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14349 if (offset >= dwarf2_per_objfile->rnglists.size)
14350 {
14351 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14352 offset);
14353 return false;
14354 }
14355 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14356
14357 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
14358
14359 while (1)
14360 {
14361 /* Initialize it due to a false compiler warning. */
14362 CORE_ADDR range_beginning = 0, range_end = 0;
14363 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14364 + dwarf2_per_objfile->rnglists.size);
14365 unsigned int bytes_read;
14366
14367 if (buffer == buf_end)
14368 {
14369 overflow = true;
14370 break;
14371 }
14372 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14373 switch (rlet)
14374 {
14375 case DW_RLE_end_of_list:
14376 break;
14377 case DW_RLE_base_address:
14378 if (buffer + cu->header.addr_size > buf_end)
14379 {
14380 overflow = true;
14381 break;
14382 }
14383 base = read_address (obfd, buffer, cu, &bytes_read);
14384 found_base = 1;
14385 buffer += bytes_read;
14386 break;
14387 case DW_RLE_start_length:
14388 if (buffer + cu->header.addr_size > buf_end)
14389 {
14390 overflow = true;
14391 break;
14392 }
14393 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14394 buffer += bytes_read;
14395 range_end = (range_beginning
14396 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14397 buffer += bytes_read;
14398 if (buffer > buf_end)
14399 {
14400 overflow = true;
14401 break;
14402 }
14403 break;
14404 case DW_RLE_offset_pair:
14405 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14406 buffer += bytes_read;
14407 if (buffer > buf_end)
14408 {
14409 overflow = true;
14410 break;
14411 }
14412 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14413 buffer += bytes_read;
14414 if (buffer > buf_end)
14415 {
14416 overflow = true;
14417 break;
14418 }
14419 break;
14420 case DW_RLE_start_end:
14421 if (buffer + 2 * cu->header.addr_size > buf_end)
14422 {
14423 overflow = true;
14424 break;
14425 }
14426 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14427 buffer += bytes_read;
14428 range_end = read_address (obfd, buffer, cu, &bytes_read);
14429 buffer += bytes_read;
14430 break;
14431 default:
14432 complaint (_("Invalid .debug_rnglists data (no base address)"));
14433 return false;
14434 }
14435 if (rlet == DW_RLE_end_of_list || overflow)
14436 break;
14437 if (rlet == DW_RLE_base_address)
14438 continue;
14439
14440 if (!found_base)
14441 {
14442 /* We have no valid base address for the ranges
14443 data. */
14444 complaint (_("Invalid .debug_rnglists data (no base address)"));
14445 return false;
14446 }
14447
14448 if (range_beginning > range_end)
14449 {
14450 /* Inverted range entries are invalid. */
14451 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14452 return false;
14453 }
14454
14455 /* Empty range entries have no effect. */
14456 if (range_beginning == range_end)
14457 continue;
14458
14459 range_beginning += base;
14460 range_end += base;
14461
14462 /* A not-uncommon case of bad debug info.
14463 Don't pollute the addrmap with bad data. */
14464 if (range_beginning + baseaddr == 0
14465 && !dwarf2_per_objfile->has_section_at_zero)
14466 {
14467 complaint (_(".debug_rnglists entry has start address of zero"
14468 " [in module %s]"), objfile_name (objfile));
14469 continue;
14470 }
14471
14472 callback (range_beginning, range_end);
14473 }
14474
14475 if (overflow)
14476 {
14477 complaint (_("Offset %d is not terminated "
14478 "for DW_AT_ranges attribute"),
14479 offset);
14480 return false;
14481 }
14482
14483 return true;
14484 }
14485
14486 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14487 Callback's type should be:
14488 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14489 Return 1 if the attributes are present and valid, otherwise, return 0. */
14490
14491 template <typename Callback>
14492 static int
14493 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
14494 Callback &&callback)
14495 {
14496 struct dwarf2_per_objfile *dwarf2_per_objfile
14497 = cu->per_cu->dwarf2_per_objfile;
14498 struct objfile *objfile = dwarf2_per_objfile->objfile;
14499 struct comp_unit_head *cu_header = &cu->header;
14500 bfd *obfd = objfile->obfd;
14501 unsigned int addr_size = cu_header->addr_size;
14502 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14503 /* Base address selection entry. */
14504 CORE_ADDR base;
14505 int found_base;
14506 unsigned int dummy;
14507 const gdb_byte *buffer;
14508 CORE_ADDR baseaddr;
14509
14510 if (cu_header->version >= 5)
14511 return dwarf2_rnglists_process (offset, cu, callback);
14512
14513 found_base = cu->base_known;
14514 base = cu->base_address;
14515
14516 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
14517 if (offset >= dwarf2_per_objfile->ranges.size)
14518 {
14519 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14520 offset);
14521 return 0;
14522 }
14523 buffer = dwarf2_per_objfile->ranges.buffer + offset;
14524
14525 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
14526
14527 while (1)
14528 {
14529 CORE_ADDR range_beginning, range_end;
14530
14531 range_beginning = read_address (obfd, buffer, cu, &dummy);
14532 buffer += addr_size;
14533 range_end = read_address (obfd, buffer, cu, &dummy);
14534 buffer += addr_size;
14535 offset += 2 * addr_size;
14536
14537 /* An end of list marker is a pair of zero addresses. */
14538 if (range_beginning == 0 && range_end == 0)
14539 /* Found the end of list entry. */
14540 break;
14541
14542 /* Each base address selection entry is a pair of 2 values.
14543 The first is the largest possible address, the second is
14544 the base address. Check for a base address here. */
14545 if ((range_beginning & mask) == mask)
14546 {
14547 /* If we found the largest possible address, then we already
14548 have the base address in range_end. */
14549 base = range_end;
14550 found_base = 1;
14551 continue;
14552 }
14553
14554 if (!found_base)
14555 {
14556 /* We have no valid base address for the ranges
14557 data. */
14558 complaint (_("Invalid .debug_ranges data (no base address)"));
14559 return 0;
14560 }
14561
14562 if (range_beginning > range_end)
14563 {
14564 /* Inverted range entries are invalid. */
14565 complaint (_("Invalid .debug_ranges data (inverted range)"));
14566 return 0;
14567 }
14568
14569 /* Empty range entries have no effect. */
14570 if (range_beginning == range_end)
14571 continue;
14572
14573 range_beginning += base;
14574 range_end += base;
14575
14576 /* A not-uncommon case of bad debug info.
14577 Don't pollute the addrmap with bad data. */
14578 if (range_beginning + baseaddr == 0
14579 && !dwarf2_per_objfile->has_section_at_zero)
14580 {
14581 complaint (_(".debug_ranges entry has start address of zero"
14582 " [in module %s]"), objfile_name (objfile));
14583 continue;
14584 }
14585
14586 callback (range_beginning, range_end);
14587 }
14588
14589 return 1;
14590 }
14591
14592 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14593 Return 1 if the attributes are present and valid, otherwise, return 0.
14594 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14595
14596 static int
14597 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14598 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14599 struct partial_symtab *ranges_pst)
14600 {
14601 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14602 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14603 const CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
14604 int low_set = 0;
14605 CORE_ADDR low = 0;
14606 CORE_ADDR high = 0;
14607 int retval;
14608
14609 retval = dwarf2_ranges_process (offset, cu,
14610 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14611 {
14612 if (ranges_pst != NULL)
14613 {
14614 CORE_ADDR lowpc;
14615 CORE_ADDR highpc;
14616
14617 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14618 range_beginning + baseaddr)
14619 - baseaddr);
14620 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14621 range_end + baseaddr)
14622 - baseaddr);
14623 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14624 lowpc, highpc - 1, ranges_pst);
14625 }
14626
14627 /* FIXME: This is recording everything as a low-high
14628 segment of consecutive addresses. We should have a
14629 data structure for discontiguous block ranges
14630 instead. */
14631 if (! low_set)
14632 {
14633 low = range_beginning;
14634 high = range_end;
14635 low_set = 1;
14636 }
14637 else
14638 {
14639 if (range_beginning < low)
14640 low = range_beginning;
14641 if (range_end > high)
14642 high = range_end;
14643 }
14644 });
14645 if (!retval)
14646 return 0;
14647
14648 if (! low_set)
14649 /* If the first entry is an end-of-list marker, the range
14650 describes an empty scope, i.e. no instructions. */
14651 return 0;
14652
14653 if (low_return)
14654 *low_return = low;
14655 if (high_return)
14656 *high_return = high;
14657 return 1;
14658 }
14659
14660 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14661 definition for the return value. *LOWPC and *HIGHPC are set iff
14662 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14663
14664 static enum pc_bounds_kind
14665 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14666 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14667 struct partial_symtab *pst)
14668 {
14669 struct dwarf2_per_objfile *dwarf2_per_objfile
14670 = cu->per_cu->dwarf2_per_objfile;
14671 struct attribute *attr;
14672 struct attribute *attr_high;
14673 CORE_ADDR low = 0;
14674 CORE_ADDR high = 0;
14675 enum pc_bounds_kind ret;
14676
14677 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14678 if (attr_high)
14679 {
14680 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14681 if (attr != nullptr)
14682 {
14683 low = attr_value_as_address (attr);
14684 high = attr_value_as_address (attr_high);
14685 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14686 high += low;
14687 }
14688 else
14689 /* Found high w/o low attribute. */
14690 return PC_BOUNDS_INVALID;
14691
14692 /* Found consecutive range of addresses. */
14693 ret = PC_BOUNDS_HIGH_LOW;
14694 }
14695 else
14696 {
14697 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14698 if (attr != NULL)
14699 {
14700 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14701 We take advantage of the fact that DW_AT_ranges does not appear
14702 in DW_TAG_compile_unit of DWO files. */
14703 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14704 unsigned int ranges_offset = (DW_UNSND (attr)
14705 + (need_ranges_base
14706 ? cu->ranges_base
14707 : 0));
14708
14709 /* Value of the DW_AT_ranges attribute is the offset in the
14710 .debug_ranges section. */
14711 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14712 return PC_BOUNDS_INVALID;
14713 /* Found discontinuous range of addresses. */
14714 ret = PC_BOUNDS_RANGES;
14715 }
14716 else
14717 return PC_BOUNDS_NOT_PRESENT;
14718 }
14719
14720 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14721 if (high <= low)
14722 return PC_BOUNDS_INVALID;
14723
14724 /* When using the GNU linker, .gnu.linkonce. sections are used to
14725 eliminate duplicate copies of functions and vtables and such.
14726 The linker will arbitrarily choose one and discard the others.
14727 The AT_*_pc values for such functions refer to local labels in
14728 these sections. If the section from that file was discarded, the
14729 labels are not in the output, so the relocs get a value of 0.
14730 If this is a discarded function, mark the pc bounds as invalid,
14731 so that GDB will ignore it. */
14732 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
14733 return PC_BOUNDS_INVALID;
14734
14735 *lowpc = low;
14736 if (highpc)
14737 *highpc = high;
14738 return ret;
14739 }
14740
14741 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14742 its low and high PC addresses. Do nothing if these addresses could not
14743 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14744 and HIGHPC to the high address if greater than HIGHPC. */
14745
14746 static void
14747 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14748 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14749 struct dwarf2_cu *cu)
14750 {
14751 CORE_ADDR low, high;
14752 struct die_info *child = die->child;
14753
14754 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14755 {
14756 *lowpc = std::min (*lowpc, low);
14757 *highpc = std::max (*highpc, high);
14758 }
14759
14760 /* If the language does not allow nested subprograms (either inside
14761 subprograms or lexical blocks), we're done. */
14762 if (cu->language != language_ada)
14763 return;
14764
14765 /* Check all the children of the given DIE. If it contains nested
14766 subprograms, then check their pc bounds. Likewise, we need to
14767 check lexical blocks as well, as they may also contain subprogram
14768 definitions. */
14769 while (child && child->tag)
14770 {
14771 if (child->tag == DW_TAG_subprogram
14772 || child->tag == DW_TAG_lexical_block)
14773 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14774 child = sibling_die (child);
14775 }
14776 }
14777
14778 /* Get the low and high pc's represented by the scope DIE, and store
14779 them in *LOWPC and *HIGHPC. If the correct values can't be
14780 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14781
14782 static void
14783 get_scope_pc_bounds (struct die_info *die,
14784 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14785 struct dwarf2_cu *cu)
14786 {
14787 CORE_ADDR best_low = (CORE_ADDR) -1;
14788 CORE_ADDR best_high = (CORE_ADDR) 0;
14789 CORE_ADDR current_low, current_high;
14790
14791 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14792 >= PC_BOUNDS_RANGES)
14793 {
14794 best_low = current_low;
14795 best_high = current_high;
14796 }
14797 else
14798 {
14799 struct die_info *child = die->child;
14800
14801 while (child && child->tag)
14802 {
14803 switch (child->tag) {
14804 case DW_TAG_subprogram:
14805 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14806 break;
14807 case DW_TAG_namespace:
14808 case DW_TAG_module:
14809 /* FIXME: carlton/2004-01-16: Should we do this for
14810 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14811 that current GCC's always emit the DIEs corresponding
14812 to definitions of methods of classes as children of a
14813 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14814 the DIEs giving the declarations, which could be
14815 anywhere). But I don't see any reason why the
14816 standards says that they have to be there. */
14817 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14818
14819 if (current_low != ((CORE_ADDR) -1))
14820 {
14821 best_low = std::min (best_low, current_low);
14822 best_high = std::max (best_high, current_high);
14823 }
14824 break;
14825 default:
14826 /* Ignore. */
14827 break;
14828 }
14829
14830 child = sibling_die (child);
14831 }
14832 }
14833
14834 *lowpc = best_low;
14835 *highpc = best_high;
14836 }
14837
14838 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14839 in DIE. */
14840
14841 static void
14842 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14843 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14844 {
14845 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14846 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14847 struct attribute *attr;
14848 struct attribute *attr_high;
14849
14850 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14851 if (attr_high)
14852 {
14853 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14854 if (attr != nullptr)
14855 {
14856 CORE_ADDR low = attr_value_as_address (attr);
14857 CORE_ADDR high = attr_value_as_address (attr_high);
14858
14859 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14860 high += low;
14861
14862 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14863 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14864 cu->get_builder ()->record_block_range (block, low, high - 1);
14865 }
14866 }
14867
14868 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14869 if (attr != nullptr)
14870 {
14871 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14872 We take advantage of the fact that DW_AT_ranges does not appear
14873 in DW_TAG_compile_unit of DWO files. */
14874 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14875
14876 /* The value of the DW_AT_ranges attribute is the offset of the
14877 address range list in the .debug_ranges section. */
14878 unsigned long offset = (DW_UNSND (attr)
14879 + (need_ranges_base ? cu->ranges_base : 0));
14880
14881 std::vector<blockrange> blockvec;
14882 dwarf2_ranges_process (offset, cu,
14883 [&] (CORE_ADDR start, CORE_ADDR end)
14884 {
14885 start += baseaddr;
14886 end += baseaddr;
14887 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14888 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14889 cu->get_builder ()->record_block_range (block, start, end - 1);
14890 blockvec.emplace_back (start, end);
14891 });
14892
14893 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14894 }
14895 }
14896
14897 /* Check whether the producer field indicates either of GCC < 4.6, or the
14898 Intel C/C++ compiler, and cache the result in CU. */
14899
14900 static void
14901 check_producer (struct dwarf2_cu *cu)
14902 {
14903 int major, minor;
14904
14905 if (cu->producer == NULL)
14906 {
14907 /* For unknown compilers expect their behavior is DWARF version
14908 compliant.
14909
14910 GCC started to support .debug_types sections by -gdwarf-4 since
14911 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14912 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14913 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14914 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14915 }
14916 else if (producer_is_gcc (cu->producer, &major, &minor))
14917 {
14918 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14919 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14920 }
14921 else if (producer_is_icc (cu->producer, &major, &minor))
14922 {
14923 cu->producer_is_icc = true;
14924 cu->producer_is_icc_lt_14 = major < 14;
14925 }
14926 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14927 cu->producer_is_codewarrior = true;
14928 else
14929 {
14930 /* For other non-GCC compilers, expect their behavior is DWARF version
14931 compliant. */
14932 }
14933
14934 cu->checked_producer = true;
14935 }
14936
14937 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14938 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14939 during 4.6.0 experimental. */
14940
14941 static bool
14942 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14943 {
14944 if (!cu->checked_producer)
14945 check_producer (cu);
14946
14947 return cu->producer_is_gxx_lt_4_6;
14948 }
14949
14950
14951 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14952 with incorrect is_stmt attributes. */
14953
14954 static bool
14955 producer_is_codewarrior (struct dwarf2_cu *cu)
14956 {
14957 if (!cu->checked_producer)
14958 check_producer (cu);
14959
14960 return cu->producer_is_codewarrior;
14961 }
14962
14963 /* Return the default accessibility type if it is not overridden by
14964 DW_AT_accessibility. */
14965
14966 static enum dwarf_access_attribute
14967 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14968 {
14969 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14970 {
14971 /* The default DWARF 2 accessibility for members is public, the default
14972 accessibility for inheritance is private. */
14973
14974 if (die->tag != DW_TAG_inheritance)
14975 return DW_ACCESS_public;
14976 else
14977 return DW_ACCESS_private;
14978 }
14979 else
14980 {
14981 /* DWARF 3+ defines the default accessibility a different way. The same
14982 rules apply now for DW_TAG_inheritance as for the members and it only
14983 depends on the container kind. */
14984
14985 if (die->parent->tag == DW_TAG_class_type)
14986 return DW_ACCESS_private;
14987 else
14988 return DW_ACCESS_public;
14989 }
14990 }
14991
14992 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14993 offset. If the attribute was not found return 0, otherwise return
14994 1. If it was found but could not properly be handled, set *OFFSET
14995 to 0. */
14996
14997 static int
14998 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14999 LONGEST *offset)
15000 {
15001 struct attribute *attr;
15002
15003 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15004 if (attr != NULL)
15005 {
15006 *offset = 0;
15007
15008 /* Note that we do not check for a section offset first here.
15009 This is because DW_AT_data_member_location is new in DWARF 4,
15010 so if we see it, we can assume that a constant form is really
15011 a constant and not a section offset. */
15012 if (attr_form_is_constant (attr))
15013 *offset = dwarf2_get_attr_constant_value (attr, 0);
15014 else if (attr_form_is_section_offset (attr))
15015 dwarf2_complex_location_expr_complaint ();
15016 else if (attr_form_is_block (attr))
15017 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15018 else
15019 dwarf2_complex_location_expr_complaint ();
15020
15021 return 1;
15022 }
15023
15024 return 0;
15025 }
15026
15027 /* Add an aggregate field to the field list. */
15028
15029 static void
15030 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15031 struct dwarf2_cu *cu)
15032 {
15033 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15034 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15035 struct nextfield *new_field;
15036 struct attribute *attr;
15037 struct field *fp;
15038 const char *fieldname = "";
15039
15040 if (die->tag == DW_TAG_inheritance)
15041 {
15042 fip->baseclasses.emplace_back ();
15043 new_field = &fip->baseclasses.back ();
15044 }
15045 else
15046 {
15047 fip->fields.emplace_back ();
15048 new_field = &fip->fields.back ();
15049 }
15050
15051 fip->nfields++;
15052
15053 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15054 if (attr != nullptr)
15055 new_field->accessibility = DW_UNSND (attr);
15056 else
15057 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
15058 if (new_field->accessibility != DW_ACCESS_public)
15059 fip->non_public_fields = 1;
15060
15061 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15062 if (attr != nullptr)
15063 new_field->virtuality = DW_UNSND (attr);
15064 else
15065 new_field->virtuality = DW_VIRTUALITY_none;
15066
15067 fp = &new_field->field;
15068
15069 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15070 {
15071 LONGEST offset;
15072
15073 /* Data member other than a C++ static data member. */
15074
15075 /* Get type of field. */
15076 fp->type = die_type (die, cu);
15077
15078 SET_FIELD_BITPOS (*fp, 0);
15079
15080 /* Get bit size of field (zero if none). */
15081 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15082 if (attr != nullptr)
15083 {
15084 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15085 }
15086 else
15087 {
15088 FIELD_BITSIZE (*fp) = 0;
15089 }
15090
15091 /* Get bit offset of field. */
15092 if (handle_data_member_location (die, cu, &offset))
15093 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15094 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15095 if (attr != nullptr)
15096 {
15097 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15098 {
15099 /* For big endian bits, the DW_AT_bit_offset gives the
15100 additional bit offset from the MSB of the containing
15101 anonymous object to the MSB of the field. We don't
15102 have to do anything special since we don't need to
15103 know the size of the anonymous object. */
15104 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
15105 }
15106 else
15107 {
15108 /* For little endian bits, compute the bit offset to the
15109 MSB of the anonymous object, subtract off the number of
15110 bits from the MSB of the field to the MSB of the
15111 object, and then subtract off the number of bits of
15112 the field itself. The result is the bit offset of
15113 the LSB of the field. */
15114 int anonymous_size;
15115 int bit_offset = DW_UNSND (attr);
15116
15117 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15118 if (attr != nullptr)
15119 {
15120 /* The size of the anonymous object containing
15121 the bit field is explicit, so use the
15122 indicated size (in bytes). */
15123 anonymous_size = DW_UNSND (attr);
15124 }
15125 else
15126 {
15127 /* The size of the anonymous object containing
15128 the bit field must be inferred from the type
15129 attribute of the data member containing the
15130 bit field. */
15131 anonymous_size = TYPE_LENGTH (fp->type);
15132 }
15133 SET_FIELD_BITPOS (*fp,
15134 (FIELD_BITPOS (*fp)
15135 + anonymous_size * bits_per_byte
15136 - bit_offset - FIELD_BITSIZE (*fp)));
15137 }
15138 }
15139 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15140 if (attr != NULL)
15141 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15142 + dwarf2_get_attr_constant_value (attr, 0)));
15143
15144 /* Get name of field. */
15145 fieldname = dwarf2_name (die, cu);
15146 if (fieldname == NULL)
15147 fieldname = "";
15148
15149 /* The name is already allocated along with this objfile, so we don't
15150 need to duplicate it for the type. */
15151 fp->name = fieldname;
15152
15153 /* Change accessibility for artificial fields (e.g. virtual table
15154 pointer or virtual base class pointer) to private. */
15155 if (dwarf2_attr (die, DW_AT_artificial, cu))
15156 {
15157 FIELD_ARTIFICIAL (*fp) = 1;
15158 new_field->accessibility = DW_ACCESS_private;
15159 fip->non_public_fields = 1;
15160 }
15161 }
15162 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15163 {
15164 /* C++ static member. */
15165
15166 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15167 is a declaration, but all versions of G++ as of this writing
15168 (so through at least 3.2.1) incorrectly generate
15169 DW_TAG_variable tags. */
15170
15171 const char *physname;
15172
15173 /* Get name of field. */
15174 fieldname = dwarf2_name (die, cu);
15175 if (fieldname == NULL)
15176 return;
15177
15178 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15179 if (attr
15180 /* Only create a symbol if this is an external value.
15181 new_symbol checks this and puts the value in the global symbol
15182 table, which we want. If it is not external, new_symbol
15183 will try to put the value in cu->list_in_scope which is wrong. */
15184 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15185 {
15186 /* A static const member, not much different than an enum as far as
15187 we're concerned, except that we can support more types. */
15188 new_symbol (die, NULL, cu);
15189 }
15190
15191 /* Get physical name. */
15192 physname = dwarf2_physname (fieldname, die, cu);
15193
15194 /* The name is already allocated along with this objfile, so we don't
15195 need to duplicate it for the type. */
15196 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15197 FIELD_TYPE (*fp) = die_type (die, cu);
15198 FIELD_NAME (*fp) = fieldname;
15199 }
15200 else if (die->tag == DW_TAG_inheritance)
15201 {
15202 LONGEST offset;
15203
15204 /* C++ base class field. */
15205 if (handle_data_member_location (die, cu, &offset))
15206 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
15207 FIELD_BITSIZE (*fp) = 0;
15208 FIELD_TYPE (*fp) = die_type (die, cu);
15209 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
15210 }
15211 else if (die->tag == DW_TAG_variant_part)
15212 {
15213 /* process_structure_scope will treat this DIE as a union. */
15214 process_structure_scope (die, cu);
15215
15216 /* The variant part is relative to the start of the enclosing
15217 structure. */
15218 SET_FIELD_BITPOS (*fp, 0);
15219 fp->type = get_die_type (die, cu);
15220 fp->artificial = 1;
15221 fp->name = "<<variant>>";
15222
15223 /* Normally a DW_TAG_variant_part won't have a size, but our
15224 representation requires one, so set it to the maximum of the
15225 child sizes, being sure to account for the offset at which
15226 each child is seen. */
15227 if (TYPE_LENGTH (fp->type) == 0)
15228 {
15229 unsigned max = 0;
15230 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15231 {
15232 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
15233 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
15234 if (len > max)
15235 max = len;
15236 }
15237 TYPE_LENGTH (fp->type) = max;
15238 }
15239 }
15240 else
15241 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15242 }
15243
15244 /* Can the type given by DIE define another type? */
15245
15246 static bool
15247 type_can_define_types (const struct die_info *die)
15248 {
15249 switch (die->tag)
15250 {
15251 case DW_TAG_typedef:
15252 case DW_TAG_class_type:
15253 case DW_TAG_structure_type:
15254 case DW_TAG_union_type:
15255 case DW_TAG_enumeration_type:
15256 return true;
15257
15258 default:
15259 return false;
15260 }
15261 }
15262
15263 /* Add a type definition defined in the scope of the FIP's class. */
15264
15265 static void
15266 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15267 struct dwarf2_cu *cu)
15268 {
15269 struct decl_field fp;
15270 memset (&fp, 0, sizeof (fp));
15271
15272 gdb_assert (type_can_define_types (die));
15273
15274 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15275 fp.name = dwarf2_name (die, cu);
15276 fp.type = read_type_die (die, cu);
15277
15278 /* Save accessibility. */
15279 enum dwarf_access_attribute accessibility;
15280 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15281 if (attr != NULL)
15282 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15283 else
15284 accessibility = dwarf2_default_access_attribute (die, cu);
15285 switch (accessibility)
15286 {
15287 case DW_ACCESS_public:
15288 /* The assumed value if neither private nor protected. */
15289 break;
15290 case DW_ACCESS_private:
15291 fp.is_private = 1;
15292 break;
15293 case DW_ACCESS_protected:
15294 fp.is_protected = 1;
15295 break;
15296 default:
15297 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
15298 }
15299
15300 if (die->tag == DW_TAG_typedef)
15301 fip->typedef_field_list.push_back (fp);
15302 else
15303 fip->nested_types_list.push_back (fp);
15304 }
15305
15306 /* Create the vector of fields, and attach it to the type. */
15307
15308 static void
15309 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15310 struct dwarf2_cu *cu)
15311 {
15312 int nfields = fip->nfields;
15313
15314 /* Record the field count, allocate space for the array of fields,
15315 and create blank accessibility bitfields if necessary. */
15316 TYPE_NFIELDS (type) = nfields;
15317 TYPE_FIELDS (type) = (struct field *)
15318 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
15319
15320 if (fip->non_public_fields && cu->language != language_ada)
15321 {
15322 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15323
15324 TYPE_FIELD_PRIVATE_BITS (type) =
15325 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15326 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15327
15328 TYPE_FIELD_PROTECTED_BITS (type) =
15329 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15330 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15331
15332 TYPE_FIELD_IGNORE_BITS (type) =
15333 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15334 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15335 }
15336
15337 /* If the type has baseclasses, allocate and clear a bit vector for
15338 TYPE_FIELD_VIRTUAL_BITS. */
15339 if (!fip->baseclasses.empty () && cu->language != language_ada)
15340 {
15341 int num_bytes = B_BYTES (fip->baseclasses.size ());
15342 unsigned char *pointer;
15343
15344 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15345 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15346 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15347 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15348 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15349 }
15350
15351 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15352 {
15353 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15354
15355 for (int index = 0; index < nfields; ++index)
15356 {
15357 struct nextfield &field = fip->fields[index];
15358
15359 if (field.variant.is_discriminant)
15360 di->discriminant_index = index;
15361 else if (field.variant.default_branch)
15362 di->default_index = index;
15363 else
15364 di->discriminants[index] = field.variant.discriminant_value;
15365 }
15366 }
15367
15368 /* Copy the saved-up fields into the field vector. */
15369 for (int i = 0; i < nfields; ++i)
15370 {
15371 struct nextfield &field
15372 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15373 : fip->fields[i - fip->baseclasses.size ()]);
15374
15375 TYPE_FIELD (type, i) = field.field;
15376 switch (field.accessibility)
15377 {
15378 case DW_ACCESS_private:
15379 if (cu->language != language_ada)
15380 SET_TYPE_FIELD_PRIVATE (type, i);
15381 break;
15382
15383 case DW_ACCESS_protected:
15384 if (cu->language != language_ada)
15385 SET_TYPE_FIELD_PROTECTED (type, i);
15386 break;
15387
15388 case DW_ACCESS_public:
15389 break;
15390
15391 default:
15392 /* Unknown accessibility. Complain and treat it as public. */
15393 {
15394 complaint (_("unsupported accessibility %d"),
15395 field.accessibility);
15396 }
15397 break;
15398 }
15399 if (i < fip->baseclasses.size ())
15400 {
15401 switch (field.virtuality)
15402 {
15403 case DW_VIRTUALITY_virtual:
15404 case DW_VIRTUALITY_pure_virtual:
15405 if (cu->language == language_ada)
15406 error (_("unexpected virtuality in component of Ada type"));
15407 SET_TYPE_FIELD_VIRTUAL (type, i);
15408 break;
15409 }
15410 }
15411 }
15412 }
15413
15414 /* Return true if this member function is a constructor, false
15415 otherwise. */
15416
15417 static int
15418 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15419 {
15420 const char *fieldname;
15421 const char *type_name;
15422 int len;
15423
15424 if (die->parent == NULL)
15425 return 0;
15426
15427 if (die->parent->tag != DW_TAG_structure_type
15428 && die->parent->tag != DW_TAG_union_type
15429 && die->parent->tag != DW_TAG_class_type)
15430 return 0;
15431
15432 fieldname = dwarf2_name (die, cu);
15433 type_name = dwarf2_name (die->parent, cu);
15434 if (fieldname == NULL || type_name == NULL)
15435 return 0;
15436
15437 len = strlen (fieldname);
15438 return (strncmp (fieldname, type_name, len) == 0
15439 && (type_name[len] == '\0' || type_name[len] == '<'));
15440 }
15441
15442 /* Check if the given VALUE is a recognized enum
15443 dwarf_defaulted_attribute constant according to DWARF5 spec,
15444 Table 7.24. */
15445
15446 static bool
15447 is_valid_DW_AT_defaulted (ULONGEST value)
15448 {
15449 switch (value)
15450 {
15451 case DW_DEFAULTED_no:
15452 case DW_DEFAULTED_in_class:
15453 case DW_DEFAULTED_out_of_class:
15454 return true;
15455 }
15456
15457 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15458 return false;
15459 }
15460
15461 /* Add a member function to the proper fieldlist. */
15462
15463 static void
15464 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15465 struct type *type, struct dwarf2_cu *cu)
15466 {
15467 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15468 struct attribute *attr;
15469 int i;
15470 struct fnfieldlist *flp = nullptr;
15471 struct fn_field *fnp;
15472 const char *fieldname;
15473 struct type *this_type;
15474 enum dwarf_access_attribute accessibility;
15475
15476 if (cu->language == language_ada)
15477 error (_("unexpected member function in Ada type"));
15478
15479 /* Get name of member function. */
15480 fieldname = dwarf2_name (die, cu);
15481 if (fieldname == NULL)
15482 return;
15483
15484 /* Look up member function name in fieldlist. */
15485 for (i = 0; i < fip->fnfieldlists.size (); i++)
15486 {
15487 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15488 {
15489 flp = &fip->fnfieldlists[i];
15490 break;
15491 }
15492 }
15493
15494 /* Create a new fnfieldlist if necessary. */
15495 if (flp == nullptr)
15496 {
15497 fip->fnfieldlists.emplace_back ();
15498 flp = &fip->fnfieldlists.back ();
15499 flp->name = fieldname;
15500 i = fip->fnfieldlists.size () - 1;
15501 }
15502
15503 /* Create a new member function field and add it to the vector of
15504 fnfieldlists. */
15505 flp->fnfields.emplace_back ();
15506 fnp = &flp->fnfields.back ();
15507
15508 /* Delay processing of the physname until later. */
15509 if (cu->language == language_cplus)
15510 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15511 die, cu);
15512 else
15513 {
15514 const char *physname = dwarf2_physname (fieldname, die, cu);
15515 fnp->physname = physname ? physname : "";
15516 }
15517
15518 fnp->type = alloc_type (objfile);
15519 this_type = read_type_die (die, cu);
15520 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
15521 {
15522 int nparams = TYPE_NFIELDS (this_type);
15523
15524 /* TYPE is the domain of this method, and THIS_TYPE is the type
15525 of the method itself (TYPE_CODE_METHOD). */
15526 smash_to_method_type (fnp->type, type,
15527 TYPE_TARGET_TYPE (this_type),
15528 TYPE_FIELDS (this_type),
15529 TYPE_NFIELDS (this_type),
15530 TYPE_VARARGS (this_type));
15531
15532 /* Handle static member functions.
15533 Dwarf2 has no clean way to discern C++ static and non-static
15534 member functions. G++ helps GDB by marking the first
15535 parameter for non-static member functions (which is the this
15536 pointer) as artificial. We obtain this information from
15537 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15538 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15539 fnp->voffset = VOFFSET_STATIC;
15540 }
15541 else
15542 complaint (_("member function type missing for '%s'"),
15543 dwarf2_full_name (fieldname, die, cu));
15544
15545 /* Get fcontext from DW_AT_containing_type if present. */
15546 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15547 fnp->fcontext = die_containing_type (die, cu);
15548
15549 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15550 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15551
15552 /* Get accessibility. */
15553 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15554 if (attr != nullptr)
15555 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15556 else
15557 accessibility = dwarf2_default_access_attribute (die, cu);
15558 switch (accessibility)
15559 {
15560 case DW_ACCESS_private:
15561 fnp->is_private = 1;
15562 break;
15563 case DW_ACCESS_protected:
15564 fnp->is_protected = 1;
15565 break;
15566 }
15567
15568 /* Check for artificial methods. */
15569 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15570 if (attr && DW_UNSND (attr) != 0)
15571 fnp->is_artificial = 1;
15572
15573 /* Check for defaulted methods. */
15574 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15575 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15576 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15577
15578 /* Check for deleted methods. */
15579 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15580 if (attr != nullptr && DW_UNSND (attr) != 0)
15581 fnp->is_deleted = 1;
15582
15583 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15584
15585 /* Get index in virtual function table if it is a virtual member
15586 function. For older versions of GCC, this is an offset in the
15587 appropriate virtual table, as specified by DW_AT_containing_type.
15588 For everyone else, it is an expression to be evaluated relative
15589 to the object address. */
15590
15591 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15592 if (attr != nullptr)
15593 {
15594 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
15595 {
15596 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15597 {
15598 /* Old-style GCC. */
15599 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15600 }
15601 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15602 || (DW_BLOCK (attr)->size > 1
15603 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15604 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15605 {
15606 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15607 if ((fnp->voffset % cu->header.addr_size) != 0)
15608 dwarf2_complex_location_expr_complaint ();
15609 else
15610 fnp->voffset /= cu->header.addr_size;
15611 fnp->voffset += 2;
15612 }
15613 else
15614 dwarf2_complex_location_expr_complaint ();
15615
15616 if (!fnp->fcontext)
15617 {
15618 /* If there is no `this' field and no DW_AT_containing_type,
15619 we cannot actually find a base class context for the
15620 vtable! */
15621 if (TYPE_NFIELDS (this_type) == 0
15622 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15623 {
15624 complaint (_("cannot determine context for virtual member "
15625 "function \"%s\" (offset %s)"),
15626 fieldname, sect_offset_str (die->sect_off));
15627 }
15628 else
15629 {
15630 fnp->fcontext
15631 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15632 }
15633 }
15634 }
15635 else if (attr_form_is_section_offset (attr))
15636 {
15637 dwarf2_complex_location_expr_complaint ();
15638 }
15639 else
15640 {
15641 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15642 fieldname);
15643 }
15644 }
15645 else
15646 {
15647 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15648 if (attr && DW_UNSND (attr))
15649 {
15650 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15651 complaint (_("Member function \"%s\" (offset %s) is virtual "
15652 "but the vtable offset is not specified"),
15653 fieldname, sect_offset_str (die->sect_off));
15654 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15655 TYPE_CPLUS_DYNAMIC (type) = 1;
15656 }
15657 }
15658 }
15659
15660 /* Create the vector of member function fields, and attach it to the type. */
15661
15662 static void
15663 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15664 struct dwarf2_cu *cu)
15665 {
15666 if (cu->language == language_ada)
15667 error (_("unexpected member functions in Ada type"));
15668
15669 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15670 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15671 TYPE_ALLOC (type,
15672 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15673
15674 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15675 {
15676 struct fnfieldlist &nf = fip->fnfieldlists[i];
15677 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15678
15679 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15680 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15681 fn_flp->fn_fields = (struct fn_field *)
15682 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15683
15684 for (int k = 0; k < nf.fnfields.size (); ++k)
15685 fn_flp->fn_fields[k] = nf.fnfields[k];
15686 }
15687
15688 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15689 }
15690
15691 /* Returns non-zero if NAME is the name of a vtable member in CU's
15692 language, zero otherwise. */
15693 static int
15694 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15695 {
15696 static const char vptr[] = "_vptr";
15697
15698 /* Look for the C++ form of the vtable. */
15699 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15700 return 1;
15701
15702 return 0;
15703 }
15704
15705 /* GCC outputs unnamed structures that are really pointers to member
15706 functions, with the ABI-specified layout. If TYPE describes
15707 such a structure, smash it into a member function type.
15708
15709 GCC shouldn't do this; it should just output pointer to member DIEs.
15710 This is GCC PR debug/28767. */
15711
15712 static void
15713 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15714 {
15715 struct type *pfn_type, *self_type, *new_type;
15716
15717 /* Check for a structure with no name and two children. */
15718 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15719 return;
15720
15721 /* Check for __pfn and __delta members. */
15722 if (TYPE_FIELD_NAME (type, 0) == NULL
15723 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15724 || TYPE_FIELD_NAME (type, 1) == NULL
15725 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15726 return;
15727
15728 /* Find the type of the method. */
15729 pfn_type = TYPE_FIELD_TYPE (type, 0);
15730 if (pfn_type == NULL
15731 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15732 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
15733 return;
15734
15735 /* Look for the "this" argument. */
15736 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15737 if (TYPE_NFIELDS (pfn_type) == 0
15738 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15739 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
15740 return;
15741
15742 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15743 new_type = alloc_type (objfile);
15744 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15745 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15746 TYPE_VARARGS (pfn_type));
15747 smash_to_methodptr_type (type, new_type);
15748 }
15749
15750 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15751 appropriate error checking and issuing complaints if there is a
15752 problem. */
15753
15754 static ULONGEST
15755 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15756 {
15757 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15758
15759 if (attr == nullptr)
15760 return 0;
15761
15762 if (!attr_form_is_constant (attr))
15763 {
15764 complaint (_("DW_AT_alignment must have constant form"
15765 " - DIE at %s [in module %s]"),
15766 sect_offset_str (die->sect_off),
15767 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15768 return 0;
15769 }
15770
15771 ULONGEST align;
15772 if (attr->form == DW_FORM_sdata)
15773 {
15774 LONGEST val = DW_SND (attr);
15775 if (val < 0)
15776 {
15777 complaint (_("DW_AT_alignment value must not be negative"
15778 " - DIE at %s [in module %s]"),
15779 sect_offset_str (die->sect_off),
15780 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15781 return 0;
15782 }
15783 align = val;
15784 }
15785 else
15786 align = DW_UNSND (attr);
15787
15788 if (align == 0)
15789 {
15790 complaint (_("DW_AT_alignment value must not be zero"
15791 " - DIE at %s [in module %s]"),
15792 sect_offset_str (die->sect_off),
15793 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15794 return 0;
15795 }
15796 if ((align & (align - 1)) != 0)
15797 {
15798 complaint (_("DW_AT_alignment value must be a power of 2"
15799 " - DIE at %s [in module %s]"),
15800 sect_offset_str (die->sect_off),
15801 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15802 return 0;
15803 }
15804
15805 return align;
15806 }
15807
15808 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15809 the alignment for TYPE. */
15810
15811 static void
15812 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15813 struct type *type)
15814 {
15815 if (!set_type_align (type, get_alignment (cu, die)))
15816 complaint (_("DW_AT_alignment value too large"
15817 " - DIE at %s [in module %s]"),
15818 sect_offset_str (die->sect_off),
15819 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15820 }
15821
15822 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15823 constant for a type, according to DWARF5 spec, Table 5.5. */
15824
15825 static bool
15826 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15827 {
15828 switch (value)
15829 {
15830 case DW_CC_normal:
15831 case DW_CC_pass_by_reference:
15832 case DW_CC_pass_by_value:
15833 return true;
15834
15835 default:
15836 complaint (_("unrecognized DW_AT_calling_convention value "
15837 "(%s) for a type"), pulongest (value));
15838 return false;
15839 }
15840 }
15841
15842 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15843 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15844 also according to GNU-specific values (see include/dwarf2.h). */
15845
15846 static bool
15847 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15848 {
15849 switch (value)
15850 {
15851 case DW_CC_normal:
15852 case DW_CC_program:
15853 case DW_CC_nocall:
15854 return true;
15855
15856 case DW_CC_GNU_renesas_sh:
15857 case DW_CC_GNU_borland_fastcall_i386:
15858 case DW_CC_GDB_IBM_OpenCL:
15859 return true;
15860
15861 default:
15862 complaint (_("unrecognized DW_AT_calling_convention value "
15863 "(%s) for a subroutine"), pulongest (value));
15864 return false;
15865 }
15866 }
15867
15868 /* Called when we find the DIE that starts a structure or union scope
15869 (definition) to create a type for the structure or union. Fill in
15870 the type's name and general properties; the members will not be
15871 processed until process_structure_scope. A symbol table entry for
15872 the type will also not be done until process_structure_scope (assuming
15873 the type has a name).
15874
15875 NOTE: we need to call these functions regardless of whether or not the
15876 DIE has a DW_AT_name attribute, since it might be an anonymous
15877 structure or union. This gets the type entered into our set of
15878 user defined types. */
15879
15880 static struct type *
15881 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15882 {
15883 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15884 struct type *type;
15885 struct attribute *attr;
15886 const char *name;
15887
15888 /* If the definition of this type lives in .debug_types, read that type.
15889 Don't follow DW_AT_specification though, that will take us back up
15890 the chain and we want to go down. */
15891 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
15892 if (attr != nullptr)
15893 {
15894 type = get_DW_AT_signature_type (die, attr, cu);
15895
15896 /* The type's CU may not be the same as CU.
15897 Ensure TYPE is recorded with CU in die_type_hash. */
15898 return set_die_type (die, type, cu);
15899 }
15900
15901 type = alloc_type (objfile);
15902 INIT_CPLUS_SPECIFIC (type);
15903
15904 name = dwarf2_name (die, cu);
15905 if (name != NULL)
15906 {
15907 if (cu->language == language_cplus
15908 || cu->language == language_d
15909 || cu->language == language_rust)
15910 {
15911 const char *full_name = dwarf2_full_name (name, die, cu);
15912
15913 /* dwarf2_full_name might have already finished building the DIE's
15914 type. If so, there is no need to continue. */
15915 if (get_die_type (die, cu) != NULL)
15916 return get_die_type (die, cu);
15917
15918 TYPE_NAME (type) = full_name;
15919 }
15920 else
15921 {
15922 /* The name is already allocated along with this objfile, so
15923 we don't need to duplicate it for the type. */
15924 TYPE_NAME (type) = name;
15925 }
15926 }
15927
15928 if (die->tag == DW_TAG_structure_type)
15929 {
15930 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15931 }
15932 else if (die->tag == DW_TAG_union_type)
15933 {
15934 TYPE_CODE (type) = TYPE_CODE_UNION;
15935 }
15936 else if (die->tag == DW_TAG_variant_part)
15937 {
15938 TYPE_CODE (type) = TYPE_CODE_UNION;
15939 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15940 }
15941 else
15942 {
15943 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15944 }
15945
15946 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15947 TYPE_DECLARED_CLASS (type) = 1;
15948
15949 /* Store the calling convention in the type if it's available in
15950 the die. Otherwise the calling convention remains set to
15951 the default value DW_CC_normal. */
15952 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15953 if (attr != nullptr
15954 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15955 {
15956 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15957 TYPE_CPLUS_CALLING_CONVENTION (type)
15958 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15959 }
15960
15961 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15962 if (attr != nullptr)
15963 {
15964 if (attr_form_is_constant (attr))
15965 TYPE_LENGTH (type) = DW_UNSND (attr);
15966 else
15967 {
15968 /* For the moment, dynamic type sizes are not supported
15969 by GDB's struct type. The actual size is determined
15970 on-demand when resolving the type of a given object,
15971 so set the type's length to zero for now. Otherwise,
15972 we record an expression as the length, and that expression
15973 could lead to a very large value, which could eventually
15974 lead to us trying to allocate that much memory when creating
15975 a value of that type. */
15976 TYPE_LENGTH (type) = 0;
15977 }
15978 }
15979 else
15980 {
15981 TYPE_LENGTH (type) = 0;
15982 }
15983
15984 maybe_set_alignment (cu, die, type);
15985
15986 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15987 {
15988 /* ICC<14 does not output the required DW_AT_declaration on
15989 incomplete types, but gives them a size of zero. */
15990 TYPE_STUB (type) = 1;
15991 }
15992 else
15993 TYPE_STUB_SUPPORTED (type) = 1;
15994
15995 if (die_is_declaration (die, cu))
15996 TYPE_STUB (type) = 1;
15997 else if (attr == NULL && die->child == NULL
15998 && producer_is_realview (cu->producer))
15999 /* RealView does not output the required DW_AT_declaration
16000 on incomplete types. */
16001 TYPE_STUB (type) = 1;
16002
16003 /* We need to add the type field to the die immediately so we don't
16004 infinitely recurse when dealing with pointers to the structure
16005 type within the structure itself. */
16006 set_die_type (die, type, cu);
16007
16008 /* set_die_type should be already done. */
16009 set_descriptive_type (type, die, cu);
16010
16011 return type;
16012 }
16013
16014 /* A helper for process_structure_scope that handles a single member
16015 DIE. */
16016
16017 static void
16018 handle_struct_member_die (struct die_info *child_die, struct type *type,
16019 struct field_info *fi,
16020 std::vector<struct symbol *> *template_args,
16021 struct dwarf2_cu *cu)
16022 {
16023 if (child_die->tag == DW_TAG_member
16024 || child_die->tag == DW_TAG_variable
16025 || child_die->tag == DW_TAG_variant_part)
16026 {
16027 /* NOTE: carlton/2002-11-05: A C++ static data member
16028 should be a DW_TAG_member that is a declaration, but
16029 all versions of G++ as of this writing (so through at
16030 least 3.2.1) incorrectly generate DW_TAG_variable
16031 tags for them instead. */
16032 dwarf2_add_field (fi, child_die, cu);
16033 }
16034 else if (child_die->tag == DW_TAG_subprogram)
16035 {
16036 /* Rust doesn't have member functions in the C++ sense.
16037 However, it does emit ordinary functions as children
16038 of a struct DIE. */
16039 if (cu->language == language_rust)
16040 read_func_scope (child_die, cu);
16041 else
16042 {
16043 /* C++ member function. */
16044 dwarf2_add_member_fn (fi, child_die, type, cu);
16045 }
16046 }
16047 else if (child_die->tag == DW_TAG_inheritance)
16048 {
16049 /* C++ base class field. */
16050 dwarf2_add_field (fi, child_die, cu);
16051 }
16052 else if (type_can_define_types (child_die))
16053 dwarf2_add_type_defn (fi, child_die, cu);
16054 else if (child_die->tag == DW_TAG_template_type_param
16055 || child_die->tag == DW_TAG_template_value_param)
16056 {
16057 struct symbol *arg = new_symbol (child_die, NULL, cu);
16058
16059 if (arg != NULL)
16060 template_args->push_back (arg);
16061 }
16062 else if (child_die->tag == DW_TAG_variant)
16063 {
16064 /* In a variant we want to get the discriminant and also add a
16065 field for our sole member child. */
16066 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
16067
16068 for (die_info *variant_child = child_die->child;
16069 variant_child != NULL;
16070 variant_child = sibling_die (variant_child))
16071 {
16072 if (variant_child->tag == DW_TAG_member)
16073 {
16074 handle_struct_member_die (variant_child, type, fi,
16075 template_args, cu);
16076 /* Only handle the one. */
16077 break;
16078 }
16079 }
16080
16081 /* We don't handle this but we might as well report it if we see
16082 it. */
16083 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
16084 complaint (_("DW_AT_discr_list is not supported yet"
16085 " - DIE at %s [in module %s]"),
16086 sect_offset_str (child_die->sect_off),
16087 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16088
16089 /* The first field was just added, so we can stash the
16090 discriminant there. */
16091 gdb_assert (!fi->fields.empty ());
16092 if (discr == NULL)
16093 fi->fields.back ().variant.default_branch = true;
16094 else
16095 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
16096 }
16097 }
16098
16099 /* Finish creating a structure or union type, including filling in
16100 its members and creating a symbol for it. */
16101
16102 static void
16103 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16104 {
16105 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16106 struct die_info *child_die;
16107 struct type *type;
16108
16109 type = get_die_type (die, cu);
16110 if (type == NULL)
16111 type = read_structure_type (die, cu);
16112
16113 /* When reading a DW_TAG_variant_part, we need to notice when we
16114 read the discriminant member, so we can record it later in the
16115 discriminant_info. */
16116 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16117 sect_offset discr_offset {};
16118 bool has_template_parameters = false;
16119
16120 if (is_variant_part)
16121 {
16122 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16123 if (discr == NULL)
16124 {
16125 /* Maybe it's a univariant form, an extension we support.
16126 In this case arrange not to check the offset. */
16127 is_variant_part = false;
16128 }
16129 else if (attr_form_is_ref (discr))
16130 {
16131 struct dwarf2_cu *target_cu = cu;
16132 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16133
16134 discr_offset = target_die->sect_off;
16135 }
16136 else
16137 {
16138 complaint (_("DW_AT_discr does not have DIE reference form"
16139 " - DIE at %s [in module %s]"),
16140 sect_offset_str (die->sect_off),
16141 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16142 is_variant_part = false;
16143 }
16144 }
16145
16146 if (die->child != NULL && ! die_is_declaration (die, cu))
16147 {
16148 struct field_info fi;
16149 std::vector<struct symbol *> template_args;
16150
16151 child_die = die->child;
16152
16153 while (child_die && child_die->tag)
16154 {
16155 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16156
16157 if (is_variant_part && discr_offset == child_die->sect_off)
16158 fi.fields.back ().variant.is_discriminant = true;
16159
16160 child_die = sibling_die (child_die);
16161 }
16162
16163 /* Attach template arguments to type. */
16164 if (!template_args.empty ())
16165 {
16166 has_template_parameters = true;
16167 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16168 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16169 TYPE_TEMPLATE_ARGUMENTS (type)
16170 = XOBNEWVEC (&objfile->objfile_obstack,
16171 struct symbol *,
16172 TYPE_N_TEMPLATE_ARGUMENTS (type));
16173 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16174 template_args.data (),
16175 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16176 * sizeof (struct symbol *)));
16177 }
16178
16179 /* Attach fields and member functions to the type. */
16180 if (fi.nfields)
16181 dwarf2_attach_fields_to_type (&fi, type, cu);
16182 if (!fi.fnfieldlists.empty ())
16183 {
16184 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16185
16186 /* Get the type which refers to the base class (possibly this
16187 class itself) which contains the vtable pointer for the current
16188 class from the DW_AT_containing_type attribute. This use of
16189 DW_AT_containing_type is a GNU extension. */
16190
16191 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16192 {
16193 struct type *t = die_containing_type (die, cu);
16194
16195 set_type_vptr_basetype (type, t);
16196 if (type == t)
16197 {
16198 int i;
16199
16200 /* Our own class provides vtbl ptr. */
16201 for (i = TYPE_NFIELDS (t) - 1;
16202 i >= TYPE_N_BASECLASSES (t);
16203 --i)
16204 {
16205 const char *fieldname = TYPE_FIELD_NAME (t, i);
16206
16207 if (is_vtable_name (fieldname, cu))
16208 {
16209 set_type_vptr_fieldno (type, i);
16210 break;
16211 }
16212 }
16213
16214 /* Complain if virtual function table field not found. */
16215 if (i < TYPE_N_BASECLASSES (t))
16216 complaint (_("virtual function table pointer "
16217 "not found when defining class '%s'"),
16218 TYPE_NAME (type) ? TYPE_NAME (type) : "");
16219 }
16220 else
16221 {
16222 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16223 }
16224 }
16225 else if (cu->producer
16226 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16227 {
16228 /* The IBM XLC compiler does not provide direct indication
16229 of the containing type, but the vtable pointer is
16230 always named __vfp. */
16231
16232 int i;
16233
16234 for (i = TYPE_NFIELDS (type) - 1;
16235 i >= TYPE_N_BASECLASSES (type);
16236 --i)
16237 {
16238 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16239 {
16240 set_type_vptr_fieldno (type, i);
16241 set_type_vptr_basetype (type, type);
16242 break;
16243 }
16244 }
16245 }
16246 }
16247
16248 /* Copy fi.typedef_field_list linked list elements content into the
16249 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16250 if (!fi.typedef_field_list.empty ())
16251 {
16252 int count = fi.typedef_field_list.size ();
16253
16254 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16255 TYPE_TYPEDEF_FIELD_ARRAY (type)
16256 = ((struct decl_field *)
16257 TYPE_ALLOC (type,
16258 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16259 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16260
16261 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16262 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16263 }
16264
16265 /* Copy fi.nested_types_list linked list elements content into the
16266 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16267 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16268 {
16269 int count = fi.nested_types_list.size ();
16270
16271 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16272 TYPE_NESTED_TYPES_ARRAY (type)
16273 = ((struct decl_field *)
16274 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16275 TYPE_NESTED_TYPES_COUNT (type) = count;
16276
16277 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16278 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16279 }
16280 }
16281
16282 quirk_gcc_member_function_pointer (type, objfile);
16283 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16284 cu->rust_unions.push_back (type);
16285
16286 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16287 snapshots) has been known to create a die giving a declaration
16288 for a class that has, as a child, a die giving a definition for a
16289 nested class. So we have to process our children even if the
16290 current die is a declaration. Normally, of course, a declaration
16291 won't have any children at all. */
16292
16293 child_die = die->child;
16294
16295 while (child_die != NULL && child_die->tag)
16296 {
16297 if (child_die->tag == DW_TAG_member
16298 || child_die->tag == DW_TAG_variable
16299 || child_die->tag == DW_TAG_inheritance
16300 || child_die->tag == DW_TAG_template_value_param
16301 || child_die->tag == DW_TAG_template_type_param)
16302 {
16303 /* Do nothing. */
16304 }
16305 else
16306 process_die (child_die, cu);
16307
16308 child_die = sibling_die (child_die);
16309 }
16310
16311 /* Do not consider external references. According to the DWARF standard,
16312 these DIEs are identified by the fact that they have no byte_size
16313 attribute, and a declaration attribute. */
16314 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16315 || !die_is_declaration (die, cu))
16316 {
16317 struct symbol *sym = new_symbol (die, type, cu);
16318
16319 if (has_template_parameters)
16320 {
16321 struct symtab *symtab;
16322 if (sym != nullptr)
16323 symtab = symbol_symtab (sym);
16324 else if (cu->line_header != nullptr)
16325 {
16326 /* Any related symtab will do. */
16327 symtab
16328 = cu->line_header->file_names ()[0].symtab;
16329 }
16330 else
16331 {
16332 symtab = nullptr;
16333 complaint (_("could not find suitable "
16334 "symtab for template parameter"
16335 " - DIE at %s [in module %s]"),
16336 sect_offset_str (die->sect_off),
16337 objfile_name (objfile));
16338 }
16339
16340 if (symtab != nullptr)
16341 {
16342 /* Make sure that the symtab is set on the new symbols.
16343 Even though they don't appear in this symtab directly,
16344 other parts of gdb assume that symbols do, and this is
16345 reasonably true. */
16346 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16347 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16348 }
16349 }
16350 }
16351 }
16352
16353 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
16354 update TYPE using some information only available in DIE's children. */
16355
16356 static void
16357 update_enumeration_type_from_children (struct die_info *die,
16358 struct type *type,
16359 struct dwarf2_cu *cu)
16360 {
16361 struct die_info *child_die;
16362 int unsigned_enum = 1;
16363 int flag_enum = 1;
16364 ULONGEST mask = 0;
16365
16366 auto_obstack obstack;
16367
16368 for (child_die = die->child;
16369 child_die != NULL && child_die->tag;
16370 child_die = sibling_die (child_die))
16371 {
16372 struct attribute *attr;
16373 LONGEST value;
16374 const gdb_byte *bytes;
16375 struct dwarf2_locexpr_baton *baton;
16376 const char *name;
16377
16378 if (child_die->tag != DW_TAG_enumerator)
16379 continue;
16380
16381 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16382 if (attr == NULL)
16383 continue;
16384
16385 name = dwarf2_name (child_die, cu);
16386 if (name == NULL)
16387 name = "<anonymous enumerator>";
16388
16389 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16390 &value, &bytes, &baton);
16391 if (value < 0)
16392 {
16393 unsigned_enum = 0;
16394 flag_enum = 0;
16395 }
16396 else if ((mask & value) != 0)
16397 flag_enum = 0;
16398 else
16399 mask |= value;
16400
16401 /* If we already know that the enum type is neither unsigned, nor
16402 a flag type, no need to look at the rest of the enumerates. */
16403 if (!unsigned_enum && !flag_enum)
16404 break;
16405 }
16406
16407 if (unsigned_enum)
16408 TYPE_UNSIGNED (type) = 1;
16409 if (flag_enum)
16410 TYPE_FLAG_ENUM (type) = 1;
16411 }
16412
16413 /* Given a DW_AT_enumeration_type die, set its type. We do not
16414 complete the type's fields yet, or create any symbols. */
16415
16416 static struct type *
16417 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16418 {
16419 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16420 struct type *type;
16421 struct attribute *attr;
16422 const char *name;
16423
16424 /* If the definition of this type lives in .debug_types, read that type.
16425 Don't follow DW_AT_specification though, that will take us back up
16426 the chain and we want to go down. */
16427 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
16428 if (attr != nullptr)
16429 {
16430 type = get_DW_AT_signature_type (die, attr, cu);
16431
16432 /* The type's CU may not be the same as CU.
16433 Ensure TYPE is recorded with CU in die_type_hash. */
16434 return set_die_type (die, type, cu);
16435 }
16436
16437 type = alloc_type (objfile);
16438
16439 TYPE_CODE (type) = TYPE_CODE_ENUM;
16440 name = dwarf2_full_name (NULL, die, cu);
16441 if (name != NULL)
16442 TYPE_NAME (type) = name;
16443
16444 attr = dwarf2_attr (die, DW_AT_type, cu);
16445 if (attr != NULL)
16446 {
16447 struct type *underlying_type = die_type (die, cu);
16448
16449 TYPE_TARGET_TYPE (type) = underlying_type;
16450 }
16451
16452 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16453 if (attr != nullptr)
16454 {
16455 TYPE_LENGTH (type) = DW_UNSND (attr);
16456 }
16457 else
16458 {
16459 TYPE_LENGTH (type) = 0;
16460 }
16461
16462 maybe_set_alignment (cu, die, type);
16463
16464 /* The enumeration DIE can be incomplete. In Ada, any type can be
16465 declared as private in the package spec, and then defined only
16466 inside the package body. Such types are known as Taft Amendment
16467 Types. When another package uses such a type, an incomplete DIE
16468 may be generated by the compiler. */
16469 if (die_is_declaration (die, cu))
16470 TYPE_STUB (type) = 1;
16471
16472 /* Finish the creation of this type by using the enum's children.
16473 We must call this even when the underlying type has been provided
16474 so that we can determine if we're looking at a "flag" enum. */
16475 update_enumeration_type_from_children (die, type, cu);
16476
16477 /* If this type has an underlying type that is not a stub, then we
16478 may use its attributes. We always use the "unsigned" attribute
16479 in this situation, because ordinarily we guess whether the type
16480 is unsigned -- but the guess can be wrong and the underlying type
16481 can tell us the reality. However, we defer to a local size
16482 attribute if one exists, because this lets the compiler override
16483 the underlying type if needed. */
16484 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16485 {
16486 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16487 if (TYPE_LENGTH (type) == 0)
16488 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
16489 if (TYPE_RAW_ALIGN (type) == 0
16490 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16491 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
16492 }
16493
16494 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16495
16496 return set_die_type (die, type, cu);
16497 }
16498
16499 /* Given a pointer to a die which begins an enumeration, process all
16500 the dies that define the members of the enumeration, and create the
16501 symbol for the enumeration type.
16502
16503 NOTE: We reverse the order of the element list. */
16504
16505 static void
16506 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16507 {
16508 struct type *this_type;
16509
16510 this_type = get_die_type (die, cu);
16511 if (this_type == NULL)
16512 this_type = read_enumeration_type (die, cu);
16513
16514 if (die->child != NULL)
16515 {
16516 struct die_info *child_die;
16517 struct symbol *sym;
16518 std::vector<struct field> fields;
16519 const char *name;
16520
16521 child_die = die->child;
16522 while (child_die && child_die->tag)
16523 {
16524 if (child_die->tag != DW_TAG_enumerator)
16525 {
16526 process_die (child_die, cu);
16527 }
16528 else
16529 {
16530 name = dwarf2_name (child_die, cu);
16531 if (name)
16532 {
16533 sym = new_symbol (child_die, this_type, cu);
16534
16535 fields.emplace_back ();
16536 struct field &field = fields.back ();
16537
16538 FIELD_NAME (field) = sym->linkage_name ();
16539 FIELD_TYPE (field) = NULL;
16540 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
16541 FIELD_BITSIZE (field) = 0;
16542 }
16543 }
16544
16545 child_die = sibling_die (child_die);
16546 }
16547
16548 if (!fields.empty ())
16549 {
16550 TYPE_NFIELDS (this_type) = fields.size ();
16551 TYPE_FIELDS (this_type) = (struct field *)
16552 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
16553 memcpy (TYPE_FIELDS (this_type), fields.data (),
16554 sizeof (struct field) * fields.size ());
16555 }
16556 }
16557
16558 /* If we are reading an enum from a .debug_types unit, and the enum
16559 is a declaration, and the enum is not the signatured type in the
16560 unit, then we do not want to add a symbol for it. Adding a
16561 symbol would in some cases obscure the true definition of the
16562 enum, giving users an incomplete type when the definition is
16563 actually available. Note that we do not want to do this for all
16564 enums which are just declarations, because C++0x allows forward
16565 enum declarations. */
16566 if (cu->per_cu->is_debug_types
16567 && die_is_declaration (die, cu))
16568 {
16569 struct signatured_type *sig_type;
16570
16571 sig_type = (struct signatured_type *) cu->per_cu;
16572 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16573 if (sig_type->type_offset_in_section != die->sect_off)
16574 return;
16575 }
16576
16577 new_symbol (die, this_type, cu);
16578 }
16579
16580 /* Extract all information from a DW_TAG_array_type DIE and put it in
16581 the DIE's type field. For now, this only handles one dimensional
16582 arrays. */
16583
16584 static struct type *
16585 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16586 {
16587 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16588 struct die_info *child_die;
16589 struct type *type;
16590 struct type *element_type, *range_type, *index_type;
16591 struct attribute *attr;
16592 const char *name;
16593 struct dynamic_prop *byte_stride_prop = NULL;
16594 unsigned int bit_stride = 0;
16595
16596 element_type = die_type (die, cu);
16597
16598 /* The die_type call above may have already set the type for this DIE. */
16599 type = get_die_type (die, cu);
16600 if (type)
16601 return type;
16602
16603 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16604 if (attr != NULL)
16605 {
16606 int stride_ok;
16607 struct type *prop_type
16608 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
16609
16610 byte_stride_prop
16611 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16612 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16613 prop_type);
16614 if (!stride_ok)
16615 {
16616 complaint (_("unable to read array DW_AT_byte_stride "
16617 " - DIE at %s [in module %s]"),
16618 sect_offset_str (die->sect_off),
16619 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16620 /* Ignore this attribute. We will likely not be able to print
16621 arrays of this type correctly, but there is little we can do
16622 to help if we cannot read the attribute's value. */
16623 byte_stride_prop = NULL;
16624 }
16625 }
16626
16627 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16628 if (attr != NULL)
16629 bit_stride = DW_UNSND (attr);
16630
16631 /* Irix 6.2 native cc creates array types without children for
16632 arrays with unspecified length. */
16633 if (die->child == NULL)
16634 {
16635 index_type = objfile_type (objfile)->builtin_int;
16636 range_type = create_static_range_type (NULL, index_type, 0, -1);
16637 type = create_array_type_with_stride (NULL, element_type, range_type,
16638 byte_stride_prop, bit_stride);
16639 return set_die_type (die, type, cu);
16640 }
16641
16642 std::vector<struct type *> range_types;
16643 child_die = die->child;
16644 while (child_die && child_die->tag)
16645 {
16646 if (child_die->tag == DW_TAG_subrange_type)
16647 {
16648 struct type *child_type = read_type_die (child_die, cu);
16649
16650 if (child_type != NULL)
16651 {
16652 /* The range type was succesfully read. Save it for the
16653 array type creation. */
16654 range_types.push_back (child_type);
16655 }
16656 }
16657 child_die = sibling_die (child_die);
16658 }
16659
16660 /* Dwarf2 dimensions are output from left to right, create the
16661 necessary array types in backwards order. */
16662
16663 type = element_type;
16664
16665 if (read_array_order (die, cu) == DW_ORD_col_major)
16666 {
16667 int i = 0;
16668
16669 while (i < range_types.size ())
16670 type = create_array_type_with_stride (NULL, type, range_types[i++],
16671 byte_stride_prop, bit_stride);
16672 }
16673 else
16674 {
16675 size_t ndim = range_types.size ();
16676 while (ndim-- > 0)
16677 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16678 byte_stride_prop, bit_stride);
16679 }
16680
16681 /* Understand Dwarf2 support for vector types (like they occur on
16682 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16683 array type. This is not part of the Dwarf2/3 standard yet, but a
16684 custom vendor extension. The main difference between a regular
16685 array and the vector variant is that vectors are passed by value
16686 to functions. */
16687 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16688 if (attr != nullptr)
16689 make_vector_type (type);
16690
16691 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16692 implementation may choose to implement triple vectors using this
16693 attribute. */
16694 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16695 if (attr != nullptr)
16696 {
16697 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16698 TYPE_LENGTH (type) = DW_UNSND (attr);
16699 else
16700 complaint (_("DW_AT_byte_size for array type smaller "
16701 "than the total size of elements"));
16702 }
16703
16704 name = dwarf2_name (die, cu);
16705 if (name)
16706 TYPE_NAME (type) = name;
16707
16708 maybe_set_alignment (cu, die, type);
16709
16710 /* Install the type in the die. */
16711 set_die_type (die, type, cu);
16712
16713 /* set_die_type should be already done. */
16714 set_descriptive_type (type, die, cu);
16715
16716 return type;
16717 }
16718
16719 static enum dwarf_array_dim_ordering
16720 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16721 {
16722 struct attribute *attr;
16723
16724 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16725
16726 if (attr != nullptr)
16727 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16728
16729 /* GNU F77 is a special case, as at 08/2004 array type info is the
16730 opposite order to the dwarf2 specification, but data is still
16731 laid out as per normal fortran.
16732
16733 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16734 version checking. */
16735
16736 if (cu->language == language_fortran
16737 && cu->producer && strstr (cu->producer, "GNU F77"))
16738 {
16739 return DW_ORD_row_major;
16740 }
16741
16742 switch (cu->language_defn->la_array_ordering)
16743 {
16744 case array_column_major:
16745 return DW_ORD_col_major;
16746 case array_row_major:
16747 default:
16748 return DW_ORD_row_major;
16749 };
16750 }
16751
16752 /* Extract all information from a DW_TAG_set_type DIE and put it in
16753 the DIE's type field. */
16754
16755 static struct type *
16756 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16757 {
16758 struct type *domain_type, *set_type;
16759 struct attribute *attr;
16760
16761 domain_type = die_type (die, cu);
16762
16763 /* The die_type call above may have already set the type for this DIE. */
16764 set_type = get_die_type (die, cu);
16765 if (set_type)
16766 return set_type;
16767
16768 set_type = create_set_type (NULL, domain_type);
16769
16770 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16771 if (attr != nullptr)
16772 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16773
16774 maybe_set_alignment (cu, die, set_type);
16775
16776 return set_die_type (die, set_type, cu);
16777 }
16778
16779 /* A helper for read_common_block that creates a locexpr baton.
16780 SYM is the symbol which we are marking as computed.
16781 COMMON_DIE is the DIE for the common block.
16782 COMMON_LOC is the location expression attribute for the common
16783 block itself.
16784 MEMBER_LOC is the location expression attribute for the particular
16785 member of the common block that we are processing.
16786 CU is the CU from which the above come. */
16787
16788 static void
16789 mark_common_block_symbol_computed (struct symbol *sym,
16790 struct die_info *common_die,
16791 struct attribute *common_loc,
16792 struct attribute *member_loc,
16793 struct dwarf2_cu *cu)
16794 {
16795 struct dwarf2_per_objfile *dwarf2_per_objfile
16796 = cu->per_cu->dwarf2_per_objfile;
16797 struct objfile *objfile = dwarf2_per_objfile->objfile;
16798 struct dwarf2_locexpr_baton *baton;
16799 gdb_byte *ptr;
16800 unsigned int cu_off;
16801 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16802 LONGEST offset = 0;
16803
16804 gdb_assert (common_loc && member_loc);
16805 gdb_assert (attr_form_is_block (common_loc));
16806 gdb_assert (attr_form_is_block (member_loc)
16807 || attr_form_is_constant (member_loc));
16808
16809 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16810 baton->per_cu = cu->per_cu;
16811 gdb_assert (baton->per_cu);
16812
16813 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16814
16815 if (attr_form_is_constant (member_loc))
16816 {
16817 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16818 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16819 }
16820 else
16821 baton->size += DW_BLOCK (member_loc)->size;
16822
16823 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16824 baton->data = ptr;
16825
16826 *ptr++ = DW_OP_call4;
16827 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16828 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16829 ptr += 4;
16830
16831 if (attr_form_is_constant (member_loc))
16832 {
16833 *ptr++ = DW_OP_addr;
16834 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16835 ptr += cu->header.addr_size;
16836 }
16837 else
16838 {
16839 /* We have to copy the data here, because DW_OP_call4 will only
16840 use a DW_AT_location attribute. */
16841 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16842 ptr += DW_BLOCK (member_loc)->size;
16843 }
16844
16845 *ptr++ = DW_OP_plus;
16846 gdb_assert (ptr - baton->data == baton->size);
16847
16848 SYMBOL_LOCATION_BATON (sym) = baton;
16849 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16850 }
16851
16852 /* Create appropriate locally-scoped variables for all the
16853 DW_TAG_common_block entries. Also create a struct common_block
16854 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16855 is used to separate the common blocks name namespace from regular
16856 variable names. */
16857
16858 static void
16859 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16860 {
16861 struct attribute *attr;
16862
16863 attr = dwarf2_attr (die, DW_AT_location, cu);
16864 if (attr != nullptr)
16865 {
16866 /* Support the .debug_loc offsets. */
16867 if (attr_form_is_block (attr))
16868 {
16869 /* Ok. */
16870 }
16871 else if (attr_form_is_section_offset (attr))
16872 {
16873 dwarf2_complex_location_expr_complaint ();
16874 attr = NULL;
16875 }
16876 else
16877 {
16878 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16879 "common block member");
16880 attr = NULL;
16881 }
16882 }
16883
16884 if (die->child != NULL)
16885 {
16886 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16887 struct die_info *child_die;
16888 size_t n_entries = 0, size;
16889 struct common_block *common_block;
16890 struct symbol *sym;
16891
16892 for (child_die = die->child;
16893 child_die && child_die->tag;
16894 child_die = sibling_die (child_die))
16895 ++n_entries;
16896
16897 size = (sizeof (struct common_block)
16898 + (n_entries - 1) * sizeof (struct symbol *));
16899 common_block
16900 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16901 size);
16902 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16903 common_block->n_entries = 0;
16904
16905 for (child_die = die->child;
16906 child_die && child_die->tag;
16907 child_die = sibling_die (child_die))
16908 {
16909 /* Create the symbol in the DW_TAG_common_block block in the current
16910 symbol scope. */
16911 sym = new_symbol (child_die, NULL, cu);
16912 if (sym != NULL)
16913 {
16914 struct attribute *member_loc;
16915
16916 common_block->contents[common_block->n_entries++] = sym;
16917
16918 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16919 cu);
16920 if (member_loc)
16921 {
16922 /* GDB has handled this for a long time, but it is
16923 not specified by DWARF. It seems to have been
16924 emitted by gfortran at least as recently as:
16925 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16926 complaint (_("Variable in common block has "
16927 "DW_AT_data_member_location "
16928 "- DIE at %s [in module %s]"),
16929 sect_offset_str (child_die->sect_off),
16930 objfile_name (objfile));
16931
16932 if (attr_form_is_section_offset (member_loc))
16933 dwarf2_complex_location_expr_complaint ();
16934 else if (attr_form_is_constant (member_loc)
16935 || attr_form_is_block (member_loc))
16936 {
16937 if (attr != nullptr)
16938 mark_common_block_symbol_computed (sym, die, attr,
16939 member_loc, cu);
16940 }
16941 else
16942 dwarf2_complex_location_expr_complaint ();
16943 }
16944 }
16945 }
16946
16947 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16948 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16949 }
16950 }
16951
16952 /* Create a type for a C++ namespace. */
16953
16954 static struct type *
16955 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16956 {
16957 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16958 const char *previous_prefix, *name;
16959 int is_anonymous;
16960 struct type *type;
16961
16962 /* For extensions, reuse the type of the original namespace. */
16963 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16964 {
16965 struct die_info *ext_die;
16966 struct dwarf2_cu *ext_cu = cu;
16967
16968 ext_die = dwarf2_extension (die, &ext_cu);
16969 type = read_type_die (ext_die, ext_cu);
16970
16971 /* EXT_CU may not be the same as CU.
16972 Ensure TYPE is recorded with CU in die_type_hash. */
16973 return set_die_type (die, type, cu);
16974 }
16975
16976 name = namespace_name (die, &is_anonymous, cu);
16977
16978 /* Now build the name of the current namespace. */
16979
16980 previous_prefix = determine_prefix (die, cu);
16981 if (previous_prefix[0] != '\0')
16982 name = typename_concat (&objfile->objfile_obstack,
16983 previous_prefix, name, 0, cu);
16984
16985 /* Create the type. */
16986 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16987
16988 return set_die_type (die, type, cu);
16989 }
16990
16991 /* Read a namespace scope. */
16992
16993 static void
16994 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16995 {
16996 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16997 int is_anonymous;
16998
16999 /* Add a symbol associated to this if we haven't seen the namespace
17000 before. Also, add a using directive if it's an anonymous
17001 namespace. */
17002
17003 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17004 {
17005 struct type *type;
17006
17007 type = read_type_die (die, cu);
17008 new_symbol (die, type, cu);
17009
17010 namespace_name (die, &is_anonymous, cu);
17011 if (is_anonymous)
17012 {
17013 const char *previous_prefix = determine_prefix (die, cu);
17014
17015 std::vector<const char *> excludes;
17016 add_using_directive (using_directives (cu),
17017 previous_prefix, TYPE_NAME (type), NULL,
17018 NULL, excludes, 0, &objfile->objfile_obstack);
17019 }
17020 }
17021
17022 if (die->child != NULL)
17023 {
17024 struct die_info *child_die = die->child;
17025
17026 while (child_die && child_die->tag)
17027 {
17028 process_die (child_die, cu);
17029 child_die = sibling_die (child_die);
17030 }
17031 }
17032 }
17033
17034 /* Read a Fortran module as type. This DIE can be only a declaration used for
17035 imported module. Still we need that type as local Fortran "use ... only"
17036 declaration imports depend on the created type in determine_prefix. */
17037
17038 static struct type *
17039 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17040 {
17041 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17042 const char *module_name;
17043 struct type *type;
17044
17045 module_name = dwarf2_name (die, cu);
17046 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17047
17048 return set_die_type (die, type, cu);
17049 }
17050
17051 /* Read a Fortran module. */
17052
17053 static void
17054 read_module (struct die_info *die, struct dwarf2_cu *cu)
17055 {
17056 struct die_info *child_die = die->child;
17057 struct type *type;
17058
17059 type = read_type_die (die, cu);
17060 new_symbol (die, type, cu);
17061
17062 while (child_die && child_die->tag)
17063 {
17064 process_die (child_die, cu);
17065 child_die = sibling_die (child_die);
17066 }
17067 }
17068
17069 /* Return the name of the namespace represented by DIE. Set
17070 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17071 namespace. */
17072
17073 static const char *
17074 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17075 {
17076 struct die_info *current_die;
17077 const char *name = NULL;
17078
17079 /* Loop through the extensions until we find a name. */
17080
17081 for (current_die = die;
17082 current_die != NULL;
17083 current_die = dwarf2_extension (die, &cu))
17084 {
17085 /* We don't use dwarf2_name here so that we can detect the absence
17086 of a name -> anonymous namespace. */
17087 name = dwarf2_string_attr (die, DW_AT_name, cu);
17088
17089 if (name != NULL)
17090 break;
17091 }
17092
17093 /* Is it an anonymous namespace? */
17094
17095 *is_anonymous = (name == NULL);
17096 if (*is_anonymous)
17097 name = CP_ANONYMOUS_NAMESPACE_STR;
17098
17099 return name;
17100 }
17101
17102 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17103 the user defined type vector. */
17104
17105 static struct type *
17106 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17107 {
17108 struct gdbarch *gdbarch
17109 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
17110 struct comp_unit_head *cu_header = &cu->header;
17111 struct type *type;
17112 struct attribute *attr_byte_size;
17113 struct attribute *attr_address_class;
17114 int byte_size, addr_class;
17115 struct type *target_type;
17116
17117 target_type = die_type (die, cu);
17118
17119 /* The die_type call above may have already set the type for this DIE. */
17120 type = get_die_type (die, cu);
17121 if (type)
17122 return type;
17123
17124 type = lookup_pointer_type (target_type);
17125
17126 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17127 if (attr_byte_size)
17128 byte_size = DW_UNSND (attr_byte_size);
17129 else
17130 byte_size = cu_header->addr_size;
17131
17132 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17133 if (attr_address_class)
17134 addr_class = DW_UNSND (attr_address_class);
17135 else
17136 addr_class = DW_ADDR_none;
17137
17138 ULONGEST alignment = get_alignment (cu, die);
17139
17140 /* If the pointer size, alignment, or address class is different
17141 than the default, create a type variant marked as such and set
17142 the length accordingly. */
17143 if (TYPE_LENGTH (type) != byte_size
17144 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17145 && alignment != TYPE_RAW_ALIGN (type))
17146 || addr_class != DW_ADDR_none)
17147 {
17148 if (gdbarch_address_class_type_flags_p (gdbarch))
17149 {
17150 int type_flags;
17151
17152 type_flags = gdbarch_address_class_type_flags
17153 (gdbarch, byte_size, addr_class);
17154 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17155 == 0);
17156 type = make_type_with_address_space (type, type_flags);
17157 }
17158 else if (TYPE_LENGTH (type) != byte_size)
17159 {
17160 complaint (_("invalid pointer size %d"), byte_size);
17161 }
17162 else if (TYPE_RAW_ALIGN (type) != alignment)
17163 {
17164 complaint (_("Invalid DW_AT_alignment"
17165 " - DIE at %s [in module %s]"),
17166 sect_offset_str (die->sect_off),
17167 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17168 }
17169 else
17170 {
17171 /* Should we also complain about unhandled address classes? */
17172 }
17173 }
17174
17175 TYPE_LENGTH (type) = byte_size;
17176 set_type_align (type, alignment);
17177 return set_die_type (die, type, cu);
17178 }
17179
17180 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17181 the user defined type vector. */
17182
17183 static struct type *
17184 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17185 {
17186 struct type *type;
17187 struct type *to_type;
17188 struct type *domain;
17189
17190 to_type = die_type (die, cu);
17191 domain = die_containing_type (die, cu);
17192
17193 /* The calls above may have already set the type for this DIE. */
17194 type = get_die_type (die, cu);
17195 if (type)
17196 return type;
17197
17198 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17199 type = lookup_methodptr_type (to_type);
17200 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17201 {
17202 struct type *new_type
17203 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
17204
17205 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17206 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17207 TYPE_VARARGS (to_type));
17208 type = lookup_methodptr_type (new_type);
17209 }
17210 else
17211 type = lookup_memberptr_type (to_type, domain);
17212
17213 return set_die_type (die, type, cu);
17214 }
17215
17216 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17217 the user defined type vector. */
17218
17219 static struct type *
17220 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17221 enum type_code refcode)
17222 {
17223 struct comp_unit_head *cu_header = &cu->header;
17224 struct type *type, *target_type;
17225 struct attribute *attr;
17226
17227 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17228
17229 target_type = die_type (die, cu);
17230
17231 /* The die_type call above may have already set the type for this DIE. */
17232 type = get_die_type (die, cu);
17233 if (type)
17234 return type;
17235
17236 type = lookup_reference_type (target_type, refcode);
17237 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17238 if (attr != nullptr)
17239 {
17240 TYPE_LENGTH (type) = DW_UNSND (attr);
17241 }
17242 else
17243 {
17244 TYPE_LENGTH (type) = cu_header->addr_size;
17245 }
17246 maybe_set_alignment (cu, die, type);
17247 return set_die_type (die, type, cu);
17248 }
17249
17250 /* Add the given cv-qualifiers to the element type of the array. GCC
17251 outputs DWARF type qualifiers that apply to an array, not the
17252 element type. But GDB relies on the array element type to carry
17253 the cv-qualifiers. This mimics section 6.7.3 of the C99
17254 specification. */
17255
17256 static struct type *
17257 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17258 struct type *base_type, int cnst, int voltl)
17259 {
17260 struct type *el_type, *inner_array;
17261
17262 base_type = copy_type (base_type);
17263 inner_array = base_type;
17264
17265 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17266 {
17267 TYPE_TARGET_TYPE (inner_array) =
17268 copy_type (TYPE_TARGET_TYPE (inner_array));
17269 inner_array = TYPE_TARGET_TYPE (inner_array);
17270 }
17271
17272 el_type = TYPE_TARGET_TYPE (inner_array);
17273 cnst |= TYPE_CONST (el_type);
17274 voltl |= TYPE_VOLATILE (el_type);
17275 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17276
17277 return set_die_type (die, base_type, cu);
17278 }
17279
17280 static struct type *
17281 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17282 {
17283 struct type *base_type, *cv_type;
17284
17285 base_type = die_type (die, cu);
17286
17287 /* The die_type call above may have already set the type for this DIE. */
17288 cv_type = get_die_type (die, cu);
17289 if (cv_type)
17290 return cv_type;
17291
17292 /* In case the const qualifier is applied to an array type, the element type
17293 is so qualified, not the array type (section 6.7.3 of C99). */
17294 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17295 return add_array_cv_type (die, cu, base_type, 1, 0);
17296
17297 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17298 return set_die_type (die, cv_type, cu);
17299 }
17300
17301 static struct type *
17302 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17303 {
17304 struct type *base_type, *cv_type;
17305
17306 base_type = die_type (die, cu);
17307
17308 /* The die_type call above may have already set the type for this DIE. */
17309 cv_type = get_die_type (die, cu);
17310 if (cv_type)
17311 return cv_type;
17312
17313 /* In case the volatile qualifier is applied to an array type, the
17314 element type is so qualified, not the array type (section 6.7.3
17315 of C99). */
17316 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17317 return add_array_cv_type (die, cu, base_type, 0, 1);
17318
17319 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17320 return set_die_type (die, cv_type, cu);
17321 }
17322
17323 /* Handle DW_TAG_restrict_type. */
17324
17325 static struct type *
17326 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17327 {
17328 struct type *base_type, *cv_type;
17329
17330 base_type = die_type (die, cu);
17331
17332 /* The die_type call above may have already set the type for this DIE. */
17333 cv_type = get_die_type (die, cu);
17334 if (cv_type)
17335 return cv_type;
17336
17337 cv_type = make_restrict_type (base_type);
17338 return set_die_type (die, cv_type, cu);
17339 }
17340
17341 /* Handle DW_TAG_atomic_type. */
17342
17343 static struct type *
17344 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17345 {
17346 struct type *base_type, *cv_type;
17347
17348 base_type = die_type (die, cu);
17349
17350 /* The die_type call above may have already set the type for this DIE. */
17351 cv_type = get_die_type (die, cu);
17352 if (cv_type)
17353 return cv_type;
17354
17355 cv_type = make_atomic_type (base_type);
17356 return set_die_type (die, cv_type, cu);
17357 }
17358
17359 /* Extract all information from a DW_TAG_string_type DIE and add to
17360 the user defined type vector. It isn't really a user defined type,
17361 but it behaves like one, with other DIE's using an AT_user_def_type
17362 attribute to reference it. */
17363
17364 static struct type *
17365 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17366 {
17367 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17368 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17369 struct type *type, *range_type, *index_type, *char_type;
17370 struct attribute *attr;
17371 struct dynamic_prop prop;
17372 bool length_is_constant = true;
17373 LONGEST length;
17374
17375 /* There are a couple of places where bit sizes might be made use of
17376 when parsing a DW_TAG_string_type, however, no producer that we know
17377 of make use of these. Handling bit sizes that are a multiple of the
17378 byte size is easy enough, but what about other bit sizes? Lets deal
17379 with that problem when we have to. Warn about these attributes being
17380 unsupported, then parse the type and ignore them like we always
17381 have. */
17382 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17383 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17384 {
17385 static bool warning_printed = false;
17386 if (!warning_printed)
17387 {
17388 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17389 "currently supported on DW_TAG_string_type."));
17390 warning_printed = true;
17391 }
17392 }
17393
17394 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17395 if (attr != nullptr && !attr_form_is_constant (attr))
17396 {
17397 /* The string length describes the location at which the length of
17398 the string can be found. The size of the length field can be
17399 specified with one of the attributes below. */
17400 struct type *prop_type;
17401 struct attribute *len
17402 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17403 if (len == nullptr)
17404 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17405 if (len != nullptr && attr_form_is_constant (len))
17406 {
17407 /* Pass 0 as the default as we know this attribute is constant
17408 and the default value will not be returned. */
17409 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
17410 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
17411 }
17412 else
17413 {
17414 /* If the size is not specified then we assume it is the size of
17415 an address on this target. */
17416 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
17417 }
17418
17419 /* Convert the attribute into a dynamic property. */
17420 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17421 length = 1;
17422 else
17423 length_is_constant = false;
17424 }
17425 else if (attr != nullptr)
17426 {
17427 /* This DW_AT_string_length just contains the length with no
17428 indirection. There's no need to create a dynamic property in this
17429 case. Pass 0 for the default value as we know it will not be
17430 returned in this case. */
17431 length = dwarf2_get_attr_constant_value (attr, 0);
17432 }
17433 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17434 {
17435 /* We don't currently support non-constant byte sizes for strings. */
17436 length = dwarf2_get_attr_constant_value (attr, 1);
17437 }
17438 else
17439 {
17440 /* Use 1 as a fallback length if we have nothing else. */
17441 length = 1;
17442 }
17443
17444 index_type = objfile_type (objfile)->builtin_int;
17445 if (length_is_constant)
17446 range_type = create_static_range_type (NULL, index_type, 1, length);
17447 else
17448 {
17449 struct dynamic_prop low_bound;
17450
17451 low_bound.kind = PROP_CONST;
17452 low_bound.data.const_val = 1;
17453 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17454 }
17455 char_type = language_string_char_type (cu->language_defn, gdbarch);
17456 type = create_string_type (NULL, char_type, range_type);
17457
17458 return set_die_type (die, type, cu);
17459 }
17460
17461 /* Assuming that DIE corresponds to a function, returns nonzero
17462 if the function is prototyped. */
17463
17464 static int
17465 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17466 {
17467 struct attribute *attr;
17468
17469 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17470 if (attr && (DW_UNSND (attr) != 0))
17471 return 1;
17472
17473 /* The DWARF standard implies that the DW_AT_prototyped attribute
17474 is only meaningful for C, but the concept also extends to other
17475 languages that allow unprototyped functions (Eg: Objective C).
17476 For all other languages, assume that functions are always
17477 prototyped. */
17478 if (cu->language != language_c
17479 && cu->language != language_objc
17480 && cu->language != language_opencl)
17481 return 1;
17482
17483 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17484 prototyped and unprototyped functions; default to prototyped,
17485 since that is more common in modern code (and RealView warns
17486 about unprototyped functions). */
17487 if (producer_is_realview (cu->producer))
17488 return 1;
17489
17490 return 0;
17491 }
17492
17493 /* Handle DIES due to C code like:
17494
17495 struct foo
17496 {
17497 int (*funcp)(int a, long l);
17498 int b;
17499 };
17500
17501 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17502
17503 static struct type *
17504 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17505 {
17506 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17507 struct type *type; /* Type that this function returns. */
17508 struct type *ftype; /* Function that returns above type. */
17509 struct attribute *attr;
17510
17511 type = die_type (die, cu);
17512
17513 /* The die_type call above may have already set the type for this DIE. */
17514 ftype = get_die_type (die, cu);
17515 if (ftype)
17516 return ftype;
17517
17518 ftype = lookup_function_type (type);
17519
17520 if (prototyped_function_p (die, cu))
17521 TYPE_PROTOTYPED (ftype) = 1;
17522
17523 /* Store the calling convention in the type if it's available in
17524 the subroutine die. Otherwise set the calling convention to
17525 the default value DW_CC_normal. */
17526 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17527 if (attr != nullptr
17528 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17529 TYPE_CALLING_CONVENTION (ftype)
17530 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17531 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17532 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17533 else
17534 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17535
17536 /* Record whether the function returns normally to its caller or not
17537 if the DWARF producer set that information. */
17538 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17539 if (attr && (DW_UNSND (attr) != 0))
17540 TYPE_NO_RETURN (ftype) = 1;
17541
17542 /* We need to add the subroutine type to the die immediately so
17543 we don't infinitely recurse when dealing with parameters
17544 declared as the same subroutine type. */
17545 set_die_type (die, ftype, cu);
17546
17547 if (die->child != NULL)
17548 {
17549 struct type *void_type = objfile_type (objfile)->builtin_void;
17550 struct die_info *child_die;
17551 int nparams, iparams;
17552
17553 /* Count the number of parameters.
17554 FIXME: GDB currently ignores vararg functions, but knows about
17555 vararg member functions. */
17556 nparams = 0;
17557 child_die = die->child;
17558 while (child_die && child_die->tag)
17559 {
17560 if (child_die->tag == DW_TAG_formal_parameter)
17561 nparams++;
17562 else if (child_die->tag == DW_TAG_unspecified_parameters)
17563 TYPE_VARARGS (ftype) = 1;
17564 child_die = sibling_die (child_die);
17565 }
17566
17567 /* Allocate storage for parameters and fill them in. */
17568 TYPE_NFIELDS (ftype) = nparams;
17569 TYPE_FIELDS (ftype) = (struct field *)
17570 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
17571
17572 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17573 even if we error out during the parameters reading below. */
17574 for (iparams = 0; iparams < nparams; iparams++)
17575 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17576
17577 iparams = 0;
17578 child_die = die->child;
17579 while (child_die && child_die->tag)
17580 {
17581 if (child_die->tag == DW_TAG_formal_parameter)
17582 {
17583 struct type *arg_type;
17584
17585 /* DWARF version 2 has no clean way to discern C++
17586 static and non-static member functions. G++ helps
17587 GDB by marking the first parameter for non-static
17588 member functions (which is the this pointer) as
17589 artificial. We pass this information to
17590 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17591
17592 DWARF version 3 added DW_AT_object_pointer, which GCC
17593 4.5 does not yet generate. */
17594 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17595 if (attr != nullptr)
17596 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17597 else
17598 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17599 arg_type = die_type (child_die, cu);
17600
17601 /* RealView does not mark THIS as const, which the testsuite
17602 expects. GCC marks THIS as const in method definitions,
17603 but not in the class specifications (GCC PR 43053). */
17604 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17605 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17606 {
17607 int is_this = 0;
17608 struct dwarf2_cu *arg_cu = cu;
17609 const char *name = dwarf2_name (child_die, cu);
17610
17611 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17612 if (attr != nullptr)
17613 {
17614 /* If the compiler emits this, use it. */
17615 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17616 is_this = 1;
17617 }
17618 else if (name && strcmp (name, "this") == 0)
17619 /* Function definitions will have the argument names. */
17620 is_this = 1;
17621 else if (name == NULL && iparams == 0)
17622 /* Declarations may not have the names, so like
17623 elsewhere in GDB, assume an artificial first
17624 argument is "this". */
17625 is_this = 1;
17626
17627 if (is_this)
17628 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17629 arg_type, 0);
17630 }
17631
17632 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17633 iparams++;
17634 }
17635 child_die = sibling_die (child_die);
17636 }
17637 }
17638
17639 return ftype;
17640 }
17641
17642 static struct type *
17643 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17644 {
17645 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17646 const char *name = NULL;
17647 struct type *this_type, *target_type;
17648
17649 name = dwarf2_full_name (NULL, die, cu);
17650 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17651 TYPE_TARGET_STUB (this_type) = 1;
17652 set_die_type (die, this_type, cu);
17653 target_type = die_type (die, cu);
17654 if (target_type != this_type)
17655 TYPE_TARGET_TYPE (this_type) = target_type;
17656 else
17657 {
17658 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17659 spec and cause infinite loops in GDB. */
17660 complaint (_("Self-referential DW_TAG_typedef "
17661 "- DIE at %s [in module %s]"),
17662 sect_offset_str (die->sect_off), objfile_name (objfile));
17663 TYPE_TARGET_TYPE (this_type) = NULL;
17664 }
17665 return this_type;
17666 }
17667
17668 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17669 (which may be different from NAME) to the architecture back-end to allow
17670 it to guess the correct format if necessary. */
17671
17672 static struct type *
17673 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17674 const char *name_hint, enum bfd_endian byte_order)
17675 {
17676 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17677 const struct floatformat **format;
17678 struct type *type;
17679
17680 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17681 if (format)
17682 type = init_float_type (objfile, bits, name, format, byte_order);
17683 else
17684 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17685
17686 return type;
17687 }
17688
17689 /* Allocate an integer type of size BITS and name NAME. */
17690
17691 static struct type *
17692 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17693 int bits, int unsigned_p, const char *name)
17694 {
17695 struct type *type;
17696
17697 /* Versions of Intel's C Compiler generate an integer type called "void"
17698 instead of using DW_TAG_unspecified_type. This has been seen on
17699 at least versions 14, 17, and 18. */
17700 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17701 && strcmp (name, "void") == 0)
17702 type = objfile_type (objfile)->builtin_void;
17703 else
17704 type = init_integer_type (objfile, bits, unsigned_p, name);
17705
17706 return type;
17707 }
17708
17709 /* Initialise and return a floating point type of size BITS suitable for
17710 use as a component of a complex number. The NAME_HINT is passed through
17711 when initialising the floating point type and is the name of the complex
17712 type.
17713
17714 As DWARF doesn't currently provide an explicit name for the components
17715 of a complex number, but it can be helpful to have these components
17716 named, we try to select a suitable name based on the size of the
17717 component. */
17718 static struct type *
17719 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17720 struct objfile *objfile,
17721 int bits, const char *name_hint,
17722 enum bfd_endian byte_order)
17723 {
17724 gdbarch *gdbarch = get_objfile_arch (objfile);
17725 struct type *tt = nullptr;
17726
17727 /* Try to find a suitable floating point builtin type of size BITS.
17728 We're going to use the name of this type as the name for the complex
17729 target type that we are about to create. */
17730 switch (cu->language)
17731 {
17732 case language_fortran:
17733 switch (bits)
17734 {
17735 case 32:
17736 tt = builtin_f_type (gdbarch)->builtin_real;
17737 break;
17738 case 64:
17739 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17740 break;
17741 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17742 case 128:
17743 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17744 break;
17745 }
17746 break;
17747 default:
17748 switch (bits)
17749 {
17750 case 32:
17751 tt = builtin_type (gdbarch)->builtin_float;
17752 break;
17753 case 64:
17754 tt = builtin_type (gdbarch)->builtin_double;
17755 break;
17756 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17757 case 128:
17758 tt = builtin_type (gdbarch)->builtin_long_double;
17759 break;
17760 }
17761 break;
17762 }
17763
17764 /* If the type we found doesn't match the size we were looking for, then
17765 pretend we didn't find a type at all, the complex target type we
17766 create will then be nameless. */
17767 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17768 tt = nullptr;
17769
17770 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17771 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17772 }
17773
17774 /* Find a representation of a given base type and install
17775 it in the TYPE field of the die. */
17776
17777 static struct type *
17778 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17779 {
17780 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17781 struct type *type;
17782 struct attribute *attr;
17783 int encoding = 0, bits = 0;
17784 const char *name;
17785 gdbarch *arch;
17786
17787 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17788 if (attr != nullptr)
17789 encoding = DW_UNSND (attr);
17790 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17791 if (attr != nullptr)
17792 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17793 name = dwarf2_name (die, cu);
17794 if (!name)
17795 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17796
17797 arch = get_objfile_arch (objfile);
17798 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17799
17800 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17801 if (attr)
17802 {
17803 int endianity = DW_UNSND (attr);
17804
17805 switch (endianity)
17806 {
17807 case DW_END_big:
17808 byte_order = BFD_ENDIAN_BIG;
17809 break;
17810 case DW_END_little:
17811 byte_order = BFD_ENDIAN_LITTLE;
17812 break;
17813 default:
17814 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17815 break;
17816 }
17817 }
17818
17819 switch (encoding)
17820 {
17821 case DW_ATE_address:
17822 /* Turn DW_ATE_address into a void * pointer. */
17823 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17824 type = init_pointer_type (objfile, bits, name, type);
17825 break;
17826 case DW_ATE_boolean:
17827 type = init_boolean_type (objfile, bits, 1, name);
17828 break;
17829 case DW_ATE_complex_float:
17830 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17831 byte_order);
17832 type = init_complex_type (objfile, name, type);
17833 break;
17834 case DW_ATE_decimal_float:
17835 type = init_decfloat_type (objfile, bits, name);
17836 break;
17837 case DW_ATE_float:
17838 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17839 break;
17840 case DW_ATE_signed:
17841 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17842 break;
17843 case DW_ATE_unsigned:
17844 if (cu->language == language_fortran
17845 && name
17846 && startswith (name, "character("))
17847 type = init_character_type (objfile, bits, 1, name);
17848 else
17849 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17850 break;
17851 case DW_ATE_signed_char:
17852 if (cu->language == language_ada || cu->language == language_m2
17853 || cu->language == language_pascal
17854 || cu->language == language_fortran)
17855 type = init_character_type (objfile, bits, 0, name);
17856 else
17857 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17858 break;
17859 case DW_ATE_unsigned_char:
17860 if (cu->language == language_ada || cu->language == language_m2
17861 || cu->language == language_pascal
17862 || cu->language == language_fortran
17863 || cu->language == language_rust)
17864 type = init_character_type (objfile, bits, 1, name);
17865 else
17866 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17867 break;
17868 case DW_ATE_UTF:
17869 {
17870 if (bits == 16)
17871 type = builtin_type (arch)->builtin_char16;
17872 else if (bits == 32)
17873 type = builtin_type (arch)->builtin_char32;
17874 else
17875 {
17876 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17877 bits);
17878 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17879 }
17880 return set_die_type (die, type, cu);
17881 }
17882 break;
17883
17884 default:
17885 complaint (_("unsupported DW_AT_encoding: '%s'"),
17886 dwarf_type_encoding_name (encoding));
17887 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17888 break;
17889 }
17890
17891 if (name && strcmp (name, "char") == 0)
17892 TYPE_NOSIGN (type) = 1;
17893
17894 maybe_set_alignment (cu, die, type);
17895
17896 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17897
17898 return set_die_type (die, type, cu);
17899 }
17900
17901 /* Parse dwarf attribute if it's a block, reference or constant and put the
17902 resulting value of the attribute into struct bound_prop.
17903 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17904
17905 static int
17906 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17907 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17908 struct type *default_type)
17909 {
17910 struct dwarf2_property_baton *baton;
17911 struct obstack *obstack
17912 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17913
17914 gdb_assert (default_type != NULL);
17915
17916 if (attr == NULL || prop == NULL)
17917 return 0;
17918
17919 if (attr_form_is_block (attr))
17920 {
17921 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17922 baton->property_type = default_type;
17923 baton->locexpr.per_cu = cu->per_cu;
17924 baton->locexpr.size = DW_BLOCK (attr)->size;
17925 baton->locexpr.data = DW_BLOCK (attr)->data;
17926 switch (attr->name)
17927 {
17928 case DW_AT_string_length:
17929 baton->locexpr.is_reference = true;
17930 break;
17931 default:
17932 baton->locexpr.is_reference = false;
17933 break;
17934 }
17935 prop->data.baton = baton;
17936 prop->kind = PROP_LOCEXPR;
17937 gdb_assert (prop->data.baton != NULL);
17938 }
17939 else if (attr_form_is_ref (attr))
17940 {
17941 struct dwarf2_cu *target_cu = cu;
17942 struct die_info *target_die;
17943 struct attribute *target_attr;
17944
17945 target_die = follow_die_ref (die, attr, &target_cu);
17946 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17947 if (target_attr == NULL)
17948 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17949 target_cu);
17950 if (target_attr == NULL)
17951 return 0;
17952
17953 switch (target_attr->name)
17954 {
17955 case DW_AT_location:
17956 if (attr_form_is_section_offset (target_attr))
17957 {
17958 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17959 baton->property_type = die_type (target_die, target_cu);
17960 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17961 prop->data.baton = baton;
17962 prop->kind = PROP_LOCLIST;
17963 gdb_assert (prop->data.baton != NULL);
17964 }
17965 else if (attr_form_is_block (target_attr))
17966 {
17967 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17968 baton->property_type = die_type (target_die, target_cu);
17969 baton->locexpr.per_cu = cu->per_cu;
17970 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17971 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17972 baton->locexpr.is_reference = true;
17973 prop->data.baton = baton;
17974 prop->kind = PROP_LOCEXPR;
17975 gdb_assert (prop->data.baton != NULL);
17976 }
17977 else
17978 {
17979 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17980 "dynamic property");
17981 return 0;
17982 }
17983 break;
17984 case DW_AT_data_member_location:
17985 {
17986 LONGEST offset;
17987
17988 if (!handle_data_member_location (target_die, target_cu,
17989 &offset))
17990 return 0;
17991
17992 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17993 baton->property_type = read_type_die (target_die->parent,
17994 target_cu);
17995 baton->offset_info.offset = offset;
17996 baton->offset_info.type = die_type (target_die, target_cu);
17997 prop->data.baton = baton;
17998 prop->kind = PROP_ADDR_OFFSET;
17999 break;
18000 }
18001 }
18002 }
18003 else if (attr_form_is_constant (attr))
18004 {
18005 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
18006 prop->kind = PROP_CONST;
18007 }
18008 else
18009 {
18010 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18011 dwarf2_name (die, cu));
18012 return 0;
18013 }
18014
18015 return 1;
18016 }
18017
18018 /* Find an integer type SIZE_IN_BYTES bytes in size and return it.
18019 UNSIGNED_P controls if the integer is unsigned or not. */
18020
18021 static struct type *
18022 dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
18023 int size_in_bytes, bool unsigned_p)
18024 {
18025 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
18026 struct type *int_type;
18027
18028 /* Helper macro to examine the various builtin types. */
18029 #define TRY_TYPE(F) \
18030 int_type = (unsigned_p \
18031 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18032 : objfile_type (objfile)->builtin_ ## F); \
18033 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18034 return int_type
18035
18036 TRY_TYPE (char);
18037 TRY_TYPE (short);
18038 TRY_TYPE (int);
18039 TRY_TYPE (long);
18040 TRY_TYPE (long_long);
18041
18042 #undef TRY_TYPE
18043
18044 gdb_assert_not_reached ("unable to find suitable integer type");
18045 }
18046
18047 /* Find an integer type the same size as the address size given in the
18048 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
18049 is unsigned or not. */
18050
18051 static struct type *
18052 dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
18053 bool unsigned_p)
18054 {
18055 int addr_size = dwarf2_per_cu_addr_size (per_cu);
18056 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
18057 }
18058
18059 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18060 present (which is valid) then compute the default type based on the
18061 compilation units address size. */
18062
18063 static struct type *
18064 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18065 {
18066 struct type *index_type = die_type (die, cu);
18067
18068 /* Dwarf-2 specifications explicitly allows to create subrange types
18069 without specifying a base type.
18070 In that case, the base type must be set to the type of
18071 the lower bound, upper bound or count, in that order, if any of these
18072 three attributes references an object that has a type.
18073 If no base type is found, the Dwarf-2 specifications say that
18074 a signed integer type of size equal to the size of an address should
18075 be used.
18076 For the following C code: `extern char gdb_int [];'
18077 GCC produces an empty range DIE.
18078 FIXME: muller/2010-05-28: Possible references to object for low bound,
18079 high bound or count are not yet handled by this code. */
18080 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
18081 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18082
18083 return index_type;
18084 }
18085
18086 /* Read the given DW_AT_subrange DIE. */
18087
18088 static struct type *
18089 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18090 {
18091 struct type *base_type, *orig_base_type;
18092 struct type *range_type;
18093 struct attribute *attr;
18094 struct dynamic_prop low, high;
18095 int low_default_is_valid;
18096 int high_bound_is_count = 0;
18097 const char *name;
18098 ULONGEST negative_mask;
18099
18100 orig_base_type = read_subrange_index_type (die, cu);
18101
18102 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18103 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18104 creating the range type, but we use the result of check_typedef
18105 when examining properties of the type. */
18106 base_type = check_typedef (orig_base_type);
18107
18108 /* The die_type call above may have already set the type for this DIE. */
18109 range_type = get_die_type (die, cu);
18110 if (range_type)
18111 return range_type;
18112
18113 low.kind = PROP_CONST;
18114 high.kind = PROP_CONST;
18115 high.data.const_val = 0;
18116
18117 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18118 omitting DW_AT_lower_bound. */
18119 switch (cu->language)
18120 {
18121 case language_c:
18122 case language_cplus:
18123 low.data.const_val = 0;
18124 low_default_is_valid = 1;
18125 break;
18126 case language_fortran:
18127 low.data.const_val = 1;
18128 low_default_is_valid = 1;
18129 break;
18130 case language_d:
18131 case language_objc:
18132 case language_rust:
18133 low.data.const_val = 0;
18134 low_default_is_valid = (cu->header.version >= 4);
18135 break;
18136 case language_ada:
18137 case language_m2:
18138 case language_pascal:
18139 low.data.const_val = 1;
18140 low_default_is_valid = (cu->header.version >= 4);
18141 break;
18142 default:
18143 low.data.const_val = 0;
18144 low_default_is_valid = 0;
18145 break;
18146 }
18147
18148 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18149 if (attr != nullptr)
18150 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18151 else if (!low_default_is_valid)
18152 complaint (_("Missing DW_AT_lower_bound "
18153 "- DIE at %s [in module %s]"),
18154 sect_offset_str (die->sect_off),
18155 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18156
18157 struct attribute *attr_ub, *attr_count;
18158 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18159 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18160 {
18161 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18162 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18163 {
18164 /* If bounds are constant do the final calculation here. */
18165 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
18166 high.data.const_val = low.data.const_val + high.data.const_val - 1;
18167 else
18168 high_bound_is_count = 1;
18169 }
18170 else
18171 {
18172 if (attr_ub != NULL)
18173 complaint (_("Unresolved DW_AT_upper_bound "
18174 "- DIE at %s [in module %s]"),
18175 sect_offset_str (die->sect_off),
18176 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18177 if (attr_count != NULL)
18178 complaint (_("Unresolved DW_AT_count "
18179 "- DIE at %s [in module %s]"),
18180 sect_offset_str (die->sect_off),
18181 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18182 }
18183 }
18184
18185 LONGEST bias = 0;
18186 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18187 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18188 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18189
18190 /* Normally, the DWARF producers are expected to use a signed
18191 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18192 But this is unfortunately not always the case, as witnessed
18193 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18194 is used instead. To work around that ambiguity, we treat
18195 the bounds as signed, and thus sign-extend their values, when
18196 the base type is signed. */
18197 negative_mask =
18198 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18199 if (low.kind == PROP_CONST
18200 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18201 low.data.const_val |= negative_mask;
18202 if (high.kind == PROP_CONST
18203 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18204 high.data.const_val |= negative_mask;
18205
18206 /* Check for bit and byte strides. */
18207 struct dynamic_prop byte_stride_prop;
18208 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18209 if (attr_byte_stride != nullptr)
18210 {
18211 struct type *prop_type
18212 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18213 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18214 prop_type);
18215 }
18216
18217 struct dynamic_prop bit_stride_prop;
18218 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18219 if (attr_bit_stride != nullptr)
18220 {
18221 /* It only makes sense to have either a bit or byte stride. */
18222 if (attr_byte_stride != nullptr)
18223 {
18224 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18225 "- DIE at %s [in module %s]"),
18226 sect_offset_str (die->sect_off),
18227 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18228 attr_bit_stride = nullptr;
18229 }
18230 else
18231 {
18232 struct type *prop_type
18233 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
18234 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18235 prop_type);
18236 }
18237 }
18238
18239 if (attr_byte_stride != nullptr
18240 || attr_bit_stride != nullptr)
18241 {
18242 bool byte_stride_p = (attr_byte_stride != nullptr);
18243 struct dynamic_prop *stride
18244 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18245
18246 range_type
18247 = create_range_type_with_stride (NULL, orig_base_type, &low,
18248 &high, bias, stride, byte_stride_p);
18249 }
18250 else
18251 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18252
18253 if (high_bound_is_count)
18254 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18255
18256 /* Ada expects an empty array on no boundary attributes. */
18257 if (attr == NULL && cu->language != language_ada)
18258 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
18259
18260 name = dwarf2_name (die, cu);
18261 if (name)
18262 TYPE_NAME (range_type) = name;
18263
18264 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18265 if (attr != nullptr)
18266 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18267
18268 maybe_set_alignment (cu, die, range_type);
18269
18270 set_die_type (die, range_type, cu);
18271
18272 /* set_die_type should be already done. */
18273 set_descriptive_type (range_type, die, cu);
18274
18275 return range_type;
18276 }
18277
18278 static struct type *
18279 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18280 {
18281 struct type *type;
18282
18283 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18284 NULL);
18285 TYPE_NAME (type) = dwarf2_name (die, cu);
18286
18287 /* In Ada, an unspecified type is typically used when the description
18288 of the type is deferred to a different unit. When encountering
18289 such a type, we treat it as a stub, and try to resolve it later on,
18290 when needed. */
18291 if (cu->language == language_ada)
18292 TYPE_STUB (type) = 1;
18293
18294 return set_die_type (die, type, cu);
18295 }
18296
18297 /* Read a single die and all its descendents. Set the die's sibling
18298 field to NULL; set other fields in the die correctly, and set all
18299 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18300 location of the info_ptr after reading all of those dies. PARENT
18301 is the parent of the die in question. */
18302
18303 static struct die_info *
18304 read_die_and_children (const struct die_reader_specs *reader,
18305 const gdb_byte *info_ptr,
18306 const gdb_byte **new_info_ptr,
18307 struct die_info *parent)
18308 {
18309 struct die_info *die;
18310 const gdb_byte *cur_ptr;
18311 int has_children;
18312
18313 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
18314 if (die == NULL)
18315 {
18316 *new_info_ptr = cur_ptr;
18317 return NULL;
18318 }
18319 store_in_ref_table (die, reader->cu);
18320
18321 if (has_children)
18322 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18323 else
18324 {
18325 die->child = NULL;
18326 *new_info_ptr = cur_ptr;
18327 }
18328
18329 die->sibling = NULL;
18330 die->parent = parent;
18331 return die;
18332 }
18333
18334 /* Read a die, all of its descendents, and all of its siblings; set
18335 all of the fields of all of the dies correctly. Arguments are as
18336 in read_die_and_children. */
18337
18338 static struct die_info *
18339 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18340 const gdb_byte *info_ptr,
18341 const gdb_byte **new_info_ptr,
18342 struct die_info *parent)
18343 {
18344 struct die_info *first_die, *last_sibling;
18345 const gdb_byte *cur_ptr;
18346
18347 cur_ptr = info_ptr;
18348 first_die = last_sibling = NULL;
18349
18350 while (1)
18351 {
18352 struct die_info *die
18353 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18354
18355 if (die == NULL)
18356 {
18357 *new_info_ptr = cur_ptr;
18358 return first_die;
18359 }
18360
18361 if (!first_die)
18362 first_die = die;
18363 else
18364 last_sibling->sibling = die;
18365
18366 last_sibling = die;
18367 }
18368 }
18369
18370 /* Read a die, all of its descendents, and all of its siblings; set
18371 all of the fields of all of the dies correctly. Arguments are as
18372 in read_die_and_children.
18373 This the main entry point for reading a DIE and all its children. */
18374
18375 static struct die_info *
18376 read_die_and_siblings (const struct die_reader_specs *reader,
18377 const gdb_byte *info_ptr,
18378 const gdb_byte **new_info_ptr,
18379 struct die_info *parent)
18380 {
18381 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18382 new_info_ptr, parent);
18383
18384 if (dwarf_die_debug)
18385 {
18386 fprintf_unfiltered (gdb_stdlog,
18387 "Read die from %s@0x%x of %s:\n",
18388 get_section_name (reader->die_section),
18389 (unsigned) (info_ptr - reader->die_section->buffer),
18390 bfd_get_filename (reader->abfd));
18391 dump_die (die, dwarf_die_debug);
18392 }
18393
18394 return die;
18395 }
18396
18397 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18398 attributes.
18399 The caller is responsible for filling in the extra attributes
18400 and updating (*DIEP)->num_attrs.
18401 Set DIEP to point to a newly allocated die with its information,
18402 except for its child, sibling, and parent fields.
18403 Set HAS_CHILDREN to tell whether the die has children or not. */
18404
18405 static const gdb_byte *
18406 read_full_die_1 (const struct die_reader_specs *reader,
18407 struct die_info **diep, const gdb_byte *info_ptr,
18408 int *has_children, int num_extra_attrs)
18409 {
18410 unsigned int abbrev_number, bytes_read, i;
18411 struct abbrev_info *abbrev;
18412 struct die_info *die;
18413 struct dwarf2_cu *cu = reader->cu;
18414 bfd *abfd = reader->abfd;
18415
18416 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18417 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18418 info_ptr += bytes_read;
18419 if (!abbrev_number)
18420 {
18421 *diep = NULL;
18422 *has_children = 0;
18423 return info_ptr;
18424 }
18425
18426 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18427 if (!abbrev)
18428 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18429 abbrev_number,
18430 bfd_get_filename (abfd));
18431
18432 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18433 die->sect_off = sect_off;
18434 die->tag = abbrev->tag;
18435 die->abbrev = abbrev_number;
18436
18437 /* Make the result usable.
18438 The caller needs to update num_attrs after adding the extra
18439 attributes. */
18440 die->num_attrs = abbrev->num_attrs;
18441
18442 std::vector<int> indexes_that_need_reprocess;
18443 for (i = 0; i < abbrev->num_attrs; ++i)
18444 {
18445 bool need_reprocess;
18446 info_ptr =
18447 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18448 info_ptr, &need_reprocess);
18449 if (need_reprocess)
18450 indexes_that_need_reprocess.push_back (i);
18451 }
18452
18453 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
18454 if (attr != nullptr)
18455 cu->str_offsets_base = DW_UNSND (attr);
18456
18457 auto maybe_addr_base = lookup_addr_base(die);
18458 if (maybe_addr_base.has_value ())
18459 cu->addr_base = *maybe_addr_base;
18460 for (int index : indexes_that_need_reprocess)
18461 read_attribute_reprocess (reader, &die->attrs[index]);
18462 *diep = die;
18463 *has_children = abbrev->has_children;
18464 return info_ptr;
18465 }
18466
18467 /* Read a die and all its attributes.
18468 Set DIEP to point to a newly allocated die with its information,
18469 except for its child, sibling, and parent fields.
18470 Set HAS_CHILDREN to tell whether the die has children or not. */
18471
18472 static const gdb_byte *
18473 read_full_die (const struct die_reader_specs *reader,
18474 struct die_info **diep, const gdb_byte *info_ptr,
18475 int *has_children)
18476 {
18477 const gdb_byte *result;
18478
18479 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18480
18481 if (dwarf_die_debug)
18482 {
18483 fprintf_unfiltered (gdb_stdlog,
18484 "Read die from %s@0x%x of %s:\n",
18485 get_section_name (reader->die_section),
18486 (unsigned) (info_ptr - reader->die_section->buffer),
18487 bfd_get_filename (reader->abfd));
18488 dump_die (*diep, dwarf_die_debug);
18489 }
18490
18491 return result;
18492 }
18493 \f
18494 /* Abbreviation tables.
18495
18496 In DWARF version 2, the description of the debugging information is
18497 stored in a separate .debug_abbrev section. Before we read any
18498 dies from a section we read in all abbreviations and install them
18499 in a hash table. */
18500
18501 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18502
18503 struct abbrev_info *
18504 abbrev_table::alloc_abbrev ()
18505 {
18506 struct abbrev_info *abbrev;
18507
18508 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
18509 memset (abbrev, 0, sizeof (struct abbrev_info));
18510
18511 return abbrev;
18512 }
18513
18514 /* Add an abbreviation to the table. */
18515
18516 void
18517 abbrev_table::add_abbrev (unsigned int abbrev_number,
18518 struct abbrev_info *abbrev)
18519 {
18520 unsigned int hash_number;
18521
18522 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18523 abbrev->next = m_abbrevs[hash_number];
18524 m_abbrevs[hash_number] = abbrev;
18525 }
18526
18527 /* Look up an abbrev in the table.
18528 Returns NULL if the abbrev is not found. */
18529
18530 struct abbrev_info *
18531 abbrev_table::lookup_abbrev (unsigned int abbrev_number)
18532 {
18533 unsigned int hash_number;
18534 struct abbrev_info *abbrev;
18535
18536 hash_number = abbrev_number % ABBREV_HASH_SIZE;
18537 abbrev = m_abbrevs[hash_number];
18538
18539 while (abbrev)
18540 {
18541 if (abbrev->number == abbrev_number)
18542 return abbrev;
18543 abbrev = abbrev->next;
18544 }
18545 return NULL;
18546 }
18547
18548 /* Read in an abbrev table. */
18549
18550 static abbrev_table_up
18551 abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18552 struct dwarf2_section_info *section,
18553 sect_offset sect_off)
18554 {
18555 struct objfile *objfile = dwarf2_per_objfile->objfile;
18556 bfd *abfd = get_section_bfd_owner (section);
18557 const gdb_byte *abbrev_ptr;
18558 struct abbrev_info *cur_abbrev;
18559 unsigned int abbrev_number, bytes_read, abbrev_name;
18560 unsigned int abbrev_form;
18561 std::vector<struct attr_abbrev> cur_attrs;
18562
18563 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
18564
18565 dwarf2_read_section (objfile, section);
18566 abbrev_ptr = section->buffer + to_underlying (sect_off);
18567 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18568 abbrev_ptr += bytes_read;
18569
18570 /* Loop until we reach an abbrev number of 0. */
18571 while (abbrev_number)
18572 {
18573 cur_attrs.clear ();
18574 cur_abbrev = abbrev_table->alloc_abbrev ();
18575
18576 /* read in abbrev header */
18577 cur_abbrev->number = abbrev_number;
18578 cur_abbrev->tag
18579 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18580 abbrev_ptr += bytes_read;
18581 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18582 abbrev_ptr += 1;
18583
18584 /* now read in declarations */
18585 for (;;)
18586 {
18587 LONGEST implicit_const;
18588
18589 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18590 abbrev_ptr += bytes_read;
18591 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18592 abbrev_ptr += bytes_read;
18593 if (abbrev_form == DW_FORM_implicit_const)
18594 {
18595 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18596 &bytes_read);
18597 abbrev_ptr += bytes_read;
18598 }
18599 else
18600 {
18601 /* Initialize it due to a false compiler warning. */
18602 implicit_const = -1;
18603 }
18604
18605 if (abbrev_name == 0)
18606 break;
18607
18608 cur_attrs.emplace_back ();
18609 struct attr_abbrev &cur_attr = cur_attrs.back ();
18610 cur_attr.name = (enum dwarf_attribute) abbrev_name;
18611 cur_attr.form = (enum dwarf_form) abbrev_form;
18612 cur_attr.implicit_const = implicit_const;
18613 ++cur_abbrev->num_attrs;
18614 }
18615
18616 cur_abbrev->attrs =
18617 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18618 cur_abbrev->num_attrs);
18619 memcpy (cur_abbrev->attrs, cur_attrs.data (),
18620 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18621
18622 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
18623
18624 /* Get next abbreviation.
18625 Under Irix6 the abbreviations for a compilation unit are not
18626 always properly terminated with an abbrev number of 0.
18627 Exit loop if we encounter an abbreviation which we have
18628 already read (which means we are about to read the abbreviations
18629 for the next compile unit) or if the end of the abbreviation
18630 table is reached. */
18631 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
18632 break;
18633 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18634 abbrev_ptr += bytes_read;
18635 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
18636 break;
18637 }
18638
18639 return abbrev_table;
18640 }
18641
18642 /* Returns nonzero if TAG represents a type that we might generate a partial
18643 symbol for. */
18644
18645 static int
18646 is_type_tag_for_partial (int tag)
18647 {
18648 switch (tag)
18649 {
18650 #if 0
18651 /* Some types that would be reasonable to generate partial symbols for,
18652 that we don't at present. */
18653 case DW_TAG_array_type:
18654 case DW_TAG_file_type:
18655 case DW_TAG_ptr_to_member_type:
18656 case DW_TAG_set_type:
18657 case DW_TAG_string_type:
18658 case DW_TAG_subroutine_type:
18659 #endif
18660 case DW_TAG_base_type:
18661 case DW_TAG_class_type:
18662 case DW_TAG_interface_type:
18663 case DW_TAG_enumeration_type:
18664 case DW_TAG_structure_type:
18665 case DW_TAG_subrange_type:
18666 case DW_TAG_typedef:
18667 case DW_TAG_union_type:
18668 return 1;
18669 default:
18670 return 0;
18671 }
18672 }
18673
18674 /* Load all DIEs that are interesting for partial symbols into memory. */
18675
18676 static struct partial_die_info *
18677 load_partial_dies (const struct die_reader_specs *reader,
18678 const gdb_byte *info_ptr, int building_psymtab)
18679 {
18680 struct dwarf2_cu *cu = reader->cu;
18681 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18682 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18683 unsigned int bytes_read;
18684 unsigned int load_all = 0;
18685 int nesting_level = 1;
18686
18687 parent_die = NULL;
18688 last_die = NULL;
18689
18690 gdb_assert (cu->per_cu != NULL);
18691 if (cu->per_cu->load_all_dies)
18692 load_all = 1;
18693
18694 cu->partial_dies
18695 = htab_create_alloc_ex (cu->header.length / 12,
18696 partial_die_hash,
18697 partial_die_eq,
18698 NULL,
18699 &cu->comp_unit_obstack,
18700 hashtab_obstack_allocate,
18701 dummy_obstack_deallocate);
18702
18703 while (1)
18704 {
18705 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18706
18707 /* A NULL abbrev means the end of a series of children. */
18708 if (abbrev == NULL)
18709 {
18710 if (--nesting_level == 0)
18711 return first_die;
18712
18713 info_ptr += bytes_read;
18714 last_die = parent_die;
18715 parent_die = parent_die->die_parent;
18716 continue;
18717 }
18718
18719 /* Check for template arguments. We never save these; if
18720 they're seen, we just mark the parent, and go on our way. */
18721 if (parent_die != NULL
18722 && cu->language == language_cplus
18723 && (abbrev->tag == DW_TAG_template_type_param
18724 || abbrev->tag == DW_TAG_template_value_param))
18725 {
18726 parent_die->has_template_arguments = 1;
18727
18728 if (!load_all)
18729 {
18730 /* We don't need a partial DIE for the template argument. */
18731 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18732 continue;
18733 }
18734 }
18735
18736 /* We only recurse into c++ subprograms looking for template arguments.
18737 Skip their other children. */
18738 if (!load_all
18739 && cu->language == language_cplus
18740 && parent_die != NULL
18741 && parent_die->tag == DW_TAG_subprogram)
18742 {
18743 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18744 continue;
18745 }
18746
18747 /* Check whether this DIE is interesting enough to save. Normally
18748 we would not be interested in members here, but there may be
18749 later variables referencing them via DW_AT_specification (for
18750 static members). */
18751 if (!load_all
18752 && !is_type_tag_for_partial (abbrev->tag)
18753 && abbrev->tag != DW_TAG_constant
18754 && abbrev->tag != DW_TAG_enumerator
18755 && abbrev->tag != DW_TAG_subprogram
18756 && abbrev->tag != DW_TAG_inlined_subroutine
18757 && abbrev->tag != DW_TAG_lexical_block
18758 && abbrev->tag != DW_TAG_variable
18759 && abbrev->tag != DW_TAG_namespace
18760 && abbrev->tag != DW_TAG_module
18761 && abbrev->tag != DW_TAG_member
18762 && abbrev->tag != DW_TAG_imported_unit
18763 && abbrev->tag != DW_TAG_imported_declaration)
18764 {
18765 /* Otherwise we skip to the next sibling, if any. */
18766 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18767 continue;
18768 }
18769
18770 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18771 abbrev);
18772
18773 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18774
18775 /* This two-pass algorithm for processing partial symbols has a
18776 high cost in cache pressure. Thus, handle some simple cases
18777 here which cover the majority of C partial symbols. DIEs
18778 which neither have specification tags in them, nor could have
18779 specification tags elsewhere pointing at them, can simply be
18780 processed and discarded.
18781
18782 This segment is also optional; scan_partial_symbols and
18783 add_partial_symbol will handle these DIEs if we chain
18784 them in normally. When compilers which do not emit large
18785 quantities of duplicate debug information are more common,
18786 this code can probably be removed. */
18787
18788 /* Any complete simple types at the top level (pretty much all
18789 of them, for a language without namespaces), can be processed
18790 directly. */
18791 if (parent_die == NULL
18792 && pdi.has_specification == 0
18793 && pdi.is_declaration == 0
18794 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18795 || pdi.tag == DW_TAG_base_type
18796 || pdi.tag == DW_TAG_subrange_type))
18797 {
18798 if (building_psymtab && pdi.name != NULL)
18799 add_psymbol_to_list (pdi.name, false,
18800 VAR_DOMAIN, LOC_TYPEDEF, -1,
18801 psymbol_placement::STATIC,
18802 0, cu->language, objfile);
18803 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18804 continue;
18805 }
18806
18807 /* The exception for DW_TAG_typedef with has_children above is
18808 a workaround of GCC PR debug/47510. In the case of this complaint
18809 type_name_or_error will error on such types later.
18810
18811 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18812 it could not find the child DIEs referenced later, this is checked
18813 above. In correct DWARF DW_TAG_typedef should have no children. */
18814
18815 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18816 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18817 "- DIE at %s [in module %s]"),
18818 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18819
18820 /* If we're at the second level, and we're an enumerator, and
18821 our parent has no specification (meaning possibly lives in a
18822 namespace elsewhere), then we can add the partial symbol now
18823 instead of queueing it. */
18824 if (pdi.tag == DW_TAG_enumerator
18825 && parent_die != NULL
18826 && parent_die->die_parent == NULL
18827 && parent_die->tag == DW_TAG_enumeration_type
18828 && parent_die->has_specification == 0)
18829 {
18830 if (pdi.name == NULL)
18831 complaint (_("malformed enumerator DIE ignored"));
18832 else if (building_psymtab)
18833 add_psymbol_to_list (pdi.name, false,
18834 VAR_DOMAIN, LOC_CONST, -1,
18835 cu->language == language_cplus
18836 ? psymbol_placement::GLOBAL
18837 : psymbol_placement::STATIC,
18838 0, cu->language, objfile);
18839
18840 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18841 continue;
18842 }
18843
18844 struct partial_die_info *part_die
18845 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18846
18847 /* We'll save this DIE so link it in. */
18848 part_die->die_parent = parent_die;
18849 part_die->die_sibling = NULL;
18850 part_die->die_child = NULL;
18851
18852 if (last_die && last_die == parent_die)
18853 last_die->die_child = part_die;
18854 else if (last_die)
18855 last_die->die_sibling = part_die;
18856
18857 last_die = part_die;
18858
18859 if (first_die == NULL)
18860 first_die = part_die;
18861
18862 /* Maybe add the DIE to the hash table. Not all DIEs that we
18863 find interesting need to be in the hash table, because we
18864 also have the parent/sibling/child chains; only those that we
18865 might refer to by offset later during partial symbol reading.
18866
18867 For now this means things that might have be the target of a
18868 DW_AT_specification, DW_AT_abstract_origin, or
18869 DW_AT_extension. DW_AT_extension will refer only to
18870 namespaces; DW_AT_abstract_origin refers to functions (and
18871 many things under the function DIE, but we do not recurse
18872 into function DIEs during partial symbol reading) and
18873 possibly variables as well; DW_AT_specification refers to
18874 declarations. Declarations ought to have the DW_AT_declaration
18875 flag. It happens that GCC forgets to put it in sometimes, but
18876 only for functions, not for types.
18877
18878 Adding more things than necessary to the hash table is harmless
18879 except for the performance cost. Adding too few will result in
18880 wasted time in find_partial_die, when we reread the compilation
18881 unit with load_all_dies set. */
18882
18883 if (load_all
18884 || abbrev->tag == DW_TAG_constant
18885 || abbrev->tag == DW_TAG_subprogram
18886 || abbrev->tag == DW_TAG_variable
18887 || abbrev->tag == DW_TAG_namespace
18888 || part_die->is_declaration)
18889 {
18890 void **slot;
18891
18892 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18893 to_underlying (part_die->sect_off),
18894 INSERT);
18895 *slot = part_die;
18896 }
18897
18898 /* For some DIEs we want to follow their children (if any). For C
18899 we have no reason to follow the children of structures; for other
18900 languages we have to, so that we can get at method physnames
18901 to infer fully qualified class names, for DW_AT_specification,
18902 and for C++ template arguments. For C++, we also look one level
18903 inside functions to find template arguments (if the name of the
18904 function does not already contain the template arguments).
18905
18906 For Ada and Fortran, we need to scan the children of subprograms
18907 and lexical blocks as well because these languages allow the
18908 definition of nested entities that could be interesting for the
18909 debugger, such as nested subprograms for instance. */
18910 if (last_die->has_children
18911 && (load_all
18912 || last_die->tag == DW_TAG_namespace
18913 || last_die->tag == DW_TAG_module
18914 || last_die->tag == DW_TAG_enumeration_type
18915 || (cu->language == language_cplus
18916 && last_die->tag == DW_TAG_subprogram
18917 && (last_die->name == NULL
18918 || strchr (last_die->name, '<') == NULL))
18919 || (cu->language != language_c
18920 && (last_die->tag == DW_TAG_class_type
18921 || last_die->tag == DW_TAG_interface_type
18922 || last_die->tag == DW_TAG_structure_type
18923 || last_die->tag == DW_TAG_union_type))
18924 || ((cu->language == language_ada
18925 || cu->language == language_fortran)
18926 && (last_die->tag == DW_TAG_subprogram
18927 || last_die->tag == DW_TAG_lexical_block))))
18928 {
18929 nesting_level++;
18930 parent_die = last_die;
18931 continue;
18932 }
18933
18934 /* Otherwise we skip to the next sibling, if any. */
18935 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18936
18937 /* Back to the top, do it again. */
18938 }
18939 }
18940
18941 partial_die_info::partial_die_info (sect_offset sect_off_,
18942 struct abbrev_info *abbrev)
18943 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18944 {
18945 }
18946
18947 /* Read a minimal amount of information into the minimal die structure.
18948 INFO_PTR should point just after the initial uleb128 of a DIE. */
18949
18950 const gdb_byte *
18951 partial_die_info::read (const struct die_reader_specs *reader,
18952 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18953 {
18954 struct dwarf2_cu *cu = reader->cu;
18955 struct dwarf2_per_objfile *dwarf2_per_objfile
18956 = cu->per_cu->dwarf2_per_objfile;
18957 unsigned int i;
18958 int has_low_pc_attr = 0;
18959 int has_high_pc_attr = 0;
18960 int high_pc_relative = 0;
18961
18962 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
18963 for (i = 0; i < abbrev.num_attrs; ++i)
18964 {
18965 bool need_reprocess;
18966 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18967 info_ptr, &need_reprocess);
18968 /* String and address offsets that need to do the reprocessing have
18969 already been read at this point, so there is no need to wait until
18970 the loop terminates to do the reprocessing. */
18971 if (need_reprocess)
18972 read_attribute_reprocess (reader, &attr_vec[i]);
18973 attribute &attr = attr_vec[i];
18974 /* Store the data if it is of an attribute we want to keep in a
18975 partial symbol table. */
18976 switch (attr.name)
18977 {
18978 case DW_AT_name:
18979 switch (tag)
18980 {
18981 case DW_TAG_compile_unit:
18982 case DW_TAG_partial_unit:
18983 case DW_TAG_type_unit:
18984 /* Compilation units have a DW_AT_name that is a filename, not
18985 a source language identifier. */
18986 case DW_TAG_enumeration_type:
18987 case DW_TAG_enumerator:
18988 /* These tags always have simple identifiers already; no need
18989 to canonicalize them. */
18990 name = DW_STRING (&attr);
18991 break;
18992 default:
18993 {
18994 struct objfile *objfile = dwarf2_per_objfile->objfile;
18995
18996 name
18997 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18998 &objfile->per_bfd->storage_obstack);
18999 }
19000 break;
19001 }
19002 break;
19003 case DW_AT_linkage_name:
19004 case DW_AT_MIPS_linkage_name:
19005 /* Note that both forms of linkage name might appear. We
19006 assume they will be the same, and we only store the last
19007 one we see. */
19008 linkage_name = DW_STRING (&attr);
19009 break;
19010 case DW_AT_low_pc:
19011 has_low_pc_attr = 1;
19012 lowpc = attr_value_as_address (&attr);
19013 break;
19014 case DW_AT_high_pc:
19015 has_high_pc_attr = 1;
19016 highpc = attr_value_as_address (&attr);
19017 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
19018 high_pc_relative = 1;
19019 break;
19020 case DW_AT_location:
19021 /* Support the .debug_loc offsets. */
19022 if (attr_form_is_block (&attr))
19023 {
19024 d.locdesc = DW_BLOCK (&attr);
19025 }
19026 else if (attr_form_is_section_offset (&attr))
19027 {
19028 dwarf2_complex_location_expr_complaint ();
19029 }
19030 else
19031 {
19032 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19033 "partial symbol information");
19034 }
19035 break;
19036 case DW_AT_external:
19037 is_external = DW_UNSND (&attr);
19038 break;
19039 case DW_AT_declaration:
19040 is_declaration = DW_UNSND (&attr);
19041 break;
19042 case DW_AT_type:
19043 has_type = 1;
19044 break;
19045 case DW_AT_abstract_origin:
19046 case DW_AT_specification:
19047 case DW_AT_extension:
19048 has_specification = 1;
19049 spec_offset = dwarf2_get_ref_die_offset (&attr);
19050 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19051 || cu->per_cu->is_dwz);
19052 break;
19053 case DW_AT_sibling:
19054 /* Ignore absolute siblings, they might point outside of
19055 the current compile unit. */
19056 if (attr.form == DW_FORM_ref_addr)
19057 complaint (_("ignoring absolute DW_AT_sibling"));
19058 else
19059 {
19060 const gdb_byte *buffer = reader->buffer;
19061 sect_offset off = dwarf2_get_ref_die_offset (&attr);
19062 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19063
19064 if (sibling_ptr < info_ptr)
19065 complaint (_("DW_AT_sibling points backwards"));
19066 else if (sibling_ptr > reader->buffer_end)
19067 dwarf2_section_buffer_overflow_complaint (reader->die_section);
19068 else
19069 sibling = sibling_ptr;
19070 }
19071 break;
19072 case DW_AT_byte_size:
19073 has_byte_size = 1;
19074 break;
19075 case DW_AT_const_value:
19076 has_const_value = 1;
19077 break;
19078 case DW_AT_calling_convention:
19079 /* DWARF doesn't provide a way to identify a program's source-level
19080 entry point. DW_AT_calling_convention attributes are only meant
19081 to describe functions' calling conventions.
19082
19083 However, because it's a necessary piece of information in
19084 Fortran, and before DWARF 4 DW_CC_program was the only
19085 piece of debugging information whose definition refers to
19086 a 'main program' at all, several compilers marked Fortran
19087 main programs with DW_CC_program --- even when those
19088 functions use the standard calling conventions.
19089
19090 Although DWARF now specifies a way to provide this
19091 information, we support this practice for backward
19092 compatibility. */
19093 if (DW_UNSND (&attr) == DW_CC_program
19094 && cu->language == language_fortran)
19095 main_subprogram = 1;
19096 break;
19097 case DW_AT_inline:
19098 if (DW_UNSND (&attr) == DW_INL_inlined
19099 || DW_UNSND (&attr) == DW_INL_declared_inlined)
19100 may_be_inlined = 1;
19101 break;
19102
19103 case DW_AT_import:
19104 if (tag == DW_TAG_imported_unit)
19105 {
19106 d.sect_off = dwarf2_get_ref_die_offset (&attr);
19107 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19108 || cu->per_cu->is_dwz);
19109 }
19110 break;
19111
19112 case DW_AT_main_subprogram:
19113 main_subprogram = DW_UNSND (&attr);
19114 break;
19115
19116 case DW_AT_ranges:
19117 {
19118 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19119 but that requires a full DIE, so instead we just
19120 reimplement it. */
19121 int need_ranges_base = tag != DW_TAG_compile_unit;
19122 unsigned int ranges_offset = (DW_UNSND (&attr)
19123 + (need_ranges_base
19124 ? cu->ranges_base
19125 : 0));
19126
19127 /* Value of the DW_AT_ranges attribute is the offset in the
19128 .debug_ranges section. */
19129 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19130 nullptr))
19131 has_pc_info = 1;
19132 }
19133 break;
19134
19135 default:
19136 break;
19137 }
19138 }
19139
19140 /* For Ada, if both the name and the linkage name appear, we prefer
19141 the latter. This lets "catch exception" work better, regardless
19142 of the order in which the name and linkage name were emitted.
19143 Really, though, this is just a workaround for the fact that gdb
19144 doesn't store both the name and the linkage name. */
19145 if (cu->language == language_ada && linkage_name != nullptr)
19146 name = linkage_name;
19147
19148 if (high_pc_relative)
19149 highpc += lowpc;
19150
19151 if (has_low_pc_attr && has_high_pc_attr)
19152 {
19153 /* When using the GNU linker, .gnu.linkonce. sections are used to
19154 eliminate duplicate copies of functions and vtables and such.
19155 The linker will arbitrarily choose one and discard the others.
19156 The AT_*_pc values for such functions refer to local labels in
19157 these sections. If the section from that file was discarded, the
19158 labels are not in the output, so the relocs get a value of 0.
19159 If this is a discarded function, mark the pc bounds as invalid,
19160 so that GDB will ignore it. */
19161 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
19162 {
19163 struct objfile *objfile = dwarf2_per_objfile->objfile;
19164 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19165
19166 complaint (_("DW_AT_low_pc %s is zero "
19167 "for DIE at %s [in module %s]"),
19168 paddress (gdbarch, lowpc),
19169 sect_offset_str (sect_off),
19170 objfile_name (objfile));
19171 }
19172 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19173 else if (lowpc >= highpc)
19174 {
19175 struct objfile *objfile = dwarf2_per_objfile->objfile;
19176 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19177
19178 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19179 "for DIE at %s [in module %s]"),
19180 paddress (gdbarch, lowpc),
19181 paddress (gdbarch, highpc),
19182 sect_offset_str (sect_off),
19183 objfile_name (objfile));
19184 }
19185 else
19186 has_pc_info = 1;
19187 }
19188
19189 return info_ptr;
19190 }
19191
19192 /* Find a cached partial DIE at OFFSET in CU. */
19193
19194 struct partial_die_info *
19195 dwarf2_cu::find_partial_die (sect_offset sect_off)
19196 {
19197 struct partial_die_info *lookup_die = NULL;
19198 struct partial_die_info part_die (sect_off);
19199
19200 lookup_die = ((struct partial_die_info *)
19201 htab_find_with_hash (partial_dies, &part_die,
19202 to_underlying (sect_off)));
19203
19204 return lookup_die;
19205 }
19206
19207 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19208 except in the case of .debug_types DIEs which do not reference
19209 outside their CU (they do however referencing other types via
19210 DW_FORM_ref_sig8). */
19211
19212 static const struct cu_partial_die_info
19213 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19214 {
19215 struct dwarf2_per_objfile *dwarf2_per_objfile
19216 = cu->per_cu->dwarf2_per_objfile;
19217 struct objfile *objfile = dwarf2_per_objfile->objfile;
19218 struct dwarf2_per_cu_data *per_cu = NULL;
19219 struct partial_die_info *pd = NULL;
19220
19221 if (offset_in_dwz == cu->per_cu->is_dwz
19222 && offset_in_cu_p (&cu->header, sect_off))
19223 {
19224 pd = cu->find_partial_die (sect_off);
19225 if (pd != NULL)
19226 return { cu, pd };
19227 /* We missed recording what we needed.
19228 Load all dies and try again. */
19229 per_cu = cu->per_cu;
19230 }
19231 else
19232 {
19233 /* TUs don't reference other CUs/TUs (except via type signatures). */
19234 if (cu->per_cu->is_debug_types)
19235 {
19236 error (_("Dwarf Error: Type Unit at offset %s contains"
19237 " external reference to offset %s [in module %s].\n"),
19238 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19239 bfd_get_filename (objfile->obfd));
19240 }
19241 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19242 dwarf2_per_objfile);
19243
19244 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19245 load_partial_comp_unit (per_cu);
19246
19247 per_cu->cu->last_used = 0;
19248 pd = per_cu->cu->find_partial_die (sect_off);
19249 }
19250
19251 /* If we didn't find it, and not all dies have been loaded,
19252 load them all and try again. */
19253
19254 if (pd == NULL && per_cu->load_all_dies == 0)
19255 {
19256 per_cu->load_all_dies = 1;
19257
19258 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19259 THIS_CU->cu may already be in use. So we can't just free it and
19260 replace its DIEs with the ones we read in. Instead, we leave those
19261 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19262 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19263 set. */
19264 load_partial_comp_unit (per_cu);
19265
19266 pd = per_cu->cu->find_partial_die (sect_off);
19267 }
19268
19269 if (pd == NULL)
19270 internal_error (__FILE__, __LINE__,
19271 _("could not find partial DIE %s "
19272 "in cache [from module %s]\n"),
19273 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19274 return { per_cu->cu, pd };
19275 }
19276
19277 /* See if we can figure out if the class lives in a namespace. We do
19278 this by looking for a member function; its demangled name will
19279 contain namespace info, if there is any. */
19280
19281 static void
19282 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19283 struct dwarf2_cu *cu)
19284 {
19285 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19286 what template types look like, because the demangler
19287 frequently doesn't give the same name as the debug info. We
19288 could fix this by only using the demangled name to get the
19289 prefix (but see comment in read_structure_type). */
19290
19291 struct partial_die_info *real_pdi;
19292 struct partial_die_info *child_pdi;
19293
19294 /* If this DIE (this DIE's specification, if any) has a parent, then
19295 we should not do this. We'll prepend the parent's fully qualified
19296 name when we create the partial symbol. */
19297
19298 real_pdi = struct_pdi;
19299 while (real_pdi->has_specification)
19300 {
19301 auto res = find_partial_die (real_pdi->spec_offset,
19302 real_pdi->spec_is_dwz, cu);
19303 real_pdi = res.pdi;
19304 cu = res.cu;
19305 }
19306
19307 if (real_pdi->die_parent != NULL)
19308 return;
19309
19310 for (child_pdi = struct_pdi->die_child;
19311 child_pdi != NULL;
19312 child_pdi = child_pdi->die_sibling)
19313 {
19314 if (child_pdi->tag == DW_TAG_subprogram
19315 && child_pdi->linkage_name != NULL)
19316 {
19317 gdb::unique_xmalloc_ptr<char> actual_class_name
19318 (language_class_name_from_physname (cu->language_defn,
19319 child_pdi->linkage_name));
19320 if (actual_class_name != NULL)
19321 {
19322 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19323 struct_pdi->name
19324 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19325 actual_class_name.get ());
19326 }
19327 break;
19328 }
19329 }
19330 }
19331
19332 void
19333 partial_die_info::fixup (struct dwarf2_cu *cu)
19334 {
19335 /* Once we've fixed up a die, there's no point in doing so again.
19336 This also avoids a memory leak if we were to call
19337 guess_partial_die_structure_name multiple times. */
19338 if (fixup_called)
19339 return;
19340
19341 /* If we found a reference attribute and the DIE has no name, try
19342 to find a name in the referred to DIE. */
19343
19344 if (name == NULL && has_specification)
19345 {
19346 struct partial_die_info *spec_die;
19347
19348 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19349 spec_die = res.pdi;
19350 cu = res.cu;
19351
19352 spec_die->fixup (cu);
19353
19354 if (spec_die->name)
19355 {
19356 name = spec_die->name;
19357
19358 /* Copy DW_AT_external attribute if it is set. */
19359 if (spec_die->is_external)
19360 is_external = spec_die->is_external;
19361 }
19362 }
19363
19364 /* Set default names for some unnamed DIEs. */
19365
19366 if (name == NULL && tag == DW_TAG_namespace)
19367 name = CP_ANONYMOUS_NAMESPACE_STR;
19368
19369 /* If there is no parent die to provide a namespace, and there are
19370 children, see if we can determine the namespace from their linkage
19371 name. */
19372 if (cu->language == language_cplus
19373 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
19374 && die_parent == NULL
19375 && has_children
19376 && (tag == DW_TAG_class_type
19377 || tag == DW_TAG_structure_type
19378 || tag == DW_TAG_union_type))
19379 guess_partial_die_structure_name (this, cu);
19380
19381 /* GCC might emit a nameless struct or union that has a linkage
19382 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19383 if (name == NULL
19384 && (tag == DW_TAG_class_type
19385 || tag == DW_TAG_interface_type
19386 || tag == DW_TAG_structure_type
19387 || tag == DW_TAG_union_type)
19388 && linkage_name != NULL)
19389 {
19390 gdb::unique_xmalloc_ptr<char> demangled
19391 (gdb_demangle (linkage_name, DMGL_TYPES));
19392 if (demangled != nullptr)
19393 {
19394 const char *base;
19395
19396 /* Strip any leading namespaces/classes, keep only the base name.
19397 DW_AT_name for named DIEs does not contain the prefixes. */
19398 base = strrchr (demangled.get (), ':');
19399 if (base && base > demangled.get () && base[-1] == ':')
19400 base++;
19401 else
19402 base = demangled.get ();
19403
19404 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19405 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
19406 }
19407 }
19408
19409 fixup_called = 1;
19410 }
19411
19412 /* Process the attributes that had to be skipped in the first round. These
19413 attributes are the ones that need str_offsets_base or addr_base attributes.
19414 They could not have been processed in the first round, because at the time
19415 the values of str_offsets_base or addr_base may not have been known. */
19416 void read_attribute_reprocess (const struct die_reader_specs *reader,
19417 struct attribute *attr)
19418 {
19419 struct dwarf2_cu *cu = reader->cu;
19420 switch (attr->form)
19421 {
19422 case DW_FORM_addrx:
19423 case DW_FORM_GNU_addr_index:
19424 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19425 break;
19426 case DW_FORM_strx:
19427 case DW_FORM_strx1:
19428 case DW_FORM_strx2:
19429 case DW_FORM_strx3:
19430 case DW_FORM_strx4:
19431 case DW_FORM_GNU_str_index:
19432 {
19433 unsigned int str_index = DW_UNSND (attr);
19434 if (reader->dwo_file != NULL)
19435 {
19436 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19437 DW_STRING_IS_CANONICAL (attr) = 0;
19438 }
19439 else
19440 {
19441 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19442 DW_STRING_IS_CANONICAL (attr) = 0;
19443 }
19444 break;
19445 }
19446 default:
19447 gdb_assert_not_reached (_("Unexpected DWARF form."));
19448 }
19449 }
19450
19451 /* Read an attribute value described by an attribute form. */
19452
19453 static const gdb_byte *
19454 read_attribute_value (const struct die_reader_specs *reader,
19455 struct attribute *attr, unsigned form,
19456 LONGEST implicit_const, const gdb_byte *info_ptr,
19457 bool *need_reprocess)
19458 {
19459 struct dwarf2_cu *cu = reader->cu;
19460 struct dwarf2_per_objfile *dwarf2_per_objfile
19461 = cu->per_cu->dwarf2_per_objfile;
19462 struct objfile *objfile = dwarf2_per_objfile->objfile;
19463 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19464 bfd *abfd = reader->abfd;
19465 struct comp_unit_head *cu_header = &cu->header;
19466 unsigned int bytes_read;
19467 struct dwarf_block *blk;
19468 *need_reprocess = false;
19469
19470 attr->form = (enum dwarf_form) form;
19471 switch (form)
19472 {
19473 case DW_FORM_ref_addr:
19474 if (cu->header.version == 2)
19475 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19476 else
19477 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19478 &cu->header, &bytes_read);
19479 info_ptr += bytes_read;
19480 break;
19481 case DW_FORM_GNU_ref_alt:
19482 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19483 info_ptr += bytes_read;
19484 break;
19485 case DW_FORM_addr:
19486 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
19487 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19488 info_ptr += bytes_read;
19489 break;
19490 case DW_FORM_block2:
19491 blk = dwarf_alloc_block (cu);
19492 blk->size = read_2_bytes (abfd, info_ptr);
19493 info_ptr += 2;
19494 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19495 info_ptr += blk->size;
19496 DW_BLOCK (attr) = blk;
19497 break;
19498 case DW_FORM_block4:
19499 blk = dwarf_alloc_block (cu);
19500 blk->size = read_4_bytes (abfd, info_ptr);
19501 info_ptr += 4;
19502 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19503 info_ptr += blk->size;
19504 DW_BLOCK (attr) = blk;
19505 break;
19506 case DW_FORM_data2:
19507 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19508 info_ptr += 2;
19509 break;
19510 case DW_FORM_data4:
19511 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19512 info_ptr += 4;
19513 break;
19514 case DW_FORM_data8:
19515 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19516 info_ptr += 8;
19517 break;
19518 case DW_FORM_data16:
19519 blk = dwarf_alloc_block (cu);
19520 blk->size = 16;
19521 blk->data = read_n_bytes (abfd, info_ptr, 16);
19522 info_ptr += 16;
19523 DW_BLOCK (attr) = blk;
19524 break;
19525 case DW_FORM_sec_offset:
19526 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19527 info_ptr += bytes_read;
19528 break;
19529 case DW_FORM_string:
19530 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19531 DW_STRING_IS_CANONICAL (attr) = 0;
19532 info_ptr += bytes_read;
19533 break;
19534 case DW_FORM_strp:
19535 if (!cu->per_cu->is_dwz)
19536 {
19537 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19538 abfd, info_ptr, cu_header,
19539 &bytes_read);
19540 DW_STRING_IS_CANONICAL (attr) = 0;
19541 info_ptr += bytes_read;
19542 break;
19543 }
19544 /* FALLTHROUGH */
19545 case DW_FORM_line_strp:
19546 if (!cu->per_cu->is_dwz)
19547 {
19548 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19549 abfd, info_ptr,
19550 cu_header, &bytes_read);
19551 DW_STRING_IS_CANONICAL (attr) = 0;
19552 info_ptr += bytes_read;
19553 break;
19554 }
19555 /* FALLTHROUGH */
19556 case DW_FORM_GNU_strp_alt:
19557 {
19558 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19559 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19560 &bytes_read);
19561
19562 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19563 dwz, str_offset);
19564 DW_STRING_IS_CANONICAL (attr) = 0;
19565 info_ptr += bytes_read;
19566 }
19567 break;
19568 case DW_FORM_exprloc:
19569 case DW_FORM_block:
19570 blk = dwarf_alloc_block (cu);
19571 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19572 info_ptr += bytes_read;
19573 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19574 info_ptr += blk->size;
19575 DW_BLOCK (attr) = blk;
19576 break;
19577 case DW_FORM_block1:
19578 blk = dwarf_alloc_block (cu);
19579 blk->size = read_1_byte (abfd, info_ptr);
19580 info_ptr += 1;
19581 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19582 info_ptr += blk->size;
19583 DW_BLOCK (attr) = blk;
19584 break;
19585 case DW_FORM_data1:
19586 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19587 info_ptr += 1;
19588 break;
19589 case DW_FORM_flag:
19590 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19591 info_ptr += 1;
19592 break;
19593 case DW_FORM_flag_present:
19594 DW_UNSND (attr) = 1;
19595 break;
19596 case DW_FORM_sdata:
19597 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19598 info_ptr += bytes_read;
19599 break;
19600 case DW_FORM_udata:
19601 case DW_FORM_rnglistx:
19602 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19603 info_ptr += bytes_read;
19604 break;
19605 case DW_FORM_ref1:
19606 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19607 + read_1_byte (abfd, info_ptr));
19608 info_ptr += 1;
19609 break;
19610 case DW_FORM_ref2:
19611 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19612 + read_2_bytes (abfd, info_ptr));
19613 info_ptr += 2;
19614 break;
19615 case DW_FORM_ref4:
19616 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19617 + read_4_bytes (abfd, info_ptr));
19618 info_ptr += 4;
19619 break;
19620 case DW_FORM_ref8:
19621 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19622 + read_8_bytes (abfd, info_ptr));
19623 info_ptr += 8;
19624 break;
19625 case DW_FORM_ref_sig8:
19626 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19627 info_ptr += 8;
19628 break;
19629 case DW_FORM_ref_udata:
19630 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19631 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19632 info_ptr += bytes_read;
19633 break;
19634 case DW_FORM_indirect:
19635 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19636 info_ptr += bytes_read;
19637 if (form == DW_FORM_implicit_const)
19638 {
19639 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19640 info_ptr += bytes_read;
19641 }
19642 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19643 info_ptr, need_reprocess);
19644 break;
19645 case DW_FORM_implicit_const:
19646 DW_SND (attr) = implicit_const;
19647 break;
19648 case DW_FORM_addrx:
19649 case DW_FORM_GNU_addr_index:
19650 *need_reprocess = true;
19651 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19652 info_ptr += bytes_read;
19653 break;
19654 case DW_FORM_strx:
19655 case DW_FORM_strx1:
19656 case DW_FORM_strx2:
19657 case DW_FORM_strx3:
19658 case DW_FORM_strx4:
19659 case DW_FORM_GNU_str_index:
19660 {
19661 ULONGEST str_index;
19662 if (form == DW_FORM_strx1)
19663 {
19664 str_index = read_1_byte (abfd, info_ptr);
19665 info_ptr += 1;
19666 }
19667 else if (form == DW_FORM_strx2)
19668 {
19669 str_index = read_2_bytes (abfd, info_ptr);
19670 info_ptr += 2;
19671 }
19672 else if (form == DW_FORM_strx3)
19673 {
19674 str_index = read_3_bytes (abfd, info_ptr);
19675 info_ptr += 3;
19676 }
19677 else if (form == DW_FORM_strx4)
19678 {
19679 str_index = read_4_bytes (abfd, info_ptr);
19680 info_ptr += 4;
19681 }
19682 else
19683 {
19684 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19685 info_ptr += bytes_read;
19686 }
19687 *need_reprocess = true;
19688 DW_UNSND (attr) = str_index;
19689 }
19690 break;
19691 default:
19692 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19693 dwarf_form_name (form),
19694 bfd_get_filename (abfd));
19695 }
19696
19697 /* Super hack. */
19698 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
19699 attr->form = DW_FORM_GNU_ref_alt;
19700
19701 /* We have seen instances where the compiler tried to emit a byte
19702 size attribute of -1 which ended up being encoded as an unsigned
19703 0xffffffff. Although 0xffffffff is technically a valid size value,
19704 an object of this size seems pretty unlikely so we can relatively
19705 safely treat these cases as if the size attribute was invalid and
19706 treat them as zero by default. */
19707 if (attr->name == DW_AT_byte_size
19708 && form == DW_FORM_data4
19709 && DW_UNSND (attr) >= 0xffffffff)
19710 {
19711 complaint
19712 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19713 hex_string (DW_UNSND (attr)));
19714 DW_UNSND (attr) = 0;
19715 }
19716
19717 return info_ptr;
19718 }
19719
19720 /* Read an attribute described by an abbreviated attribute. */
19721
19722 static const gdb_byte *
19723 read_attribute (const struct die_reader_specs *reader,
19724 struct attribute *attr, struct attr_abbrev *abbrev,
19725 const gdb_byte *info_ptr, bool *need_reprocess)
19726 {
19727 attr->name = abbrev->name;
19728 return read_attribute_value (reader, attr, abbrev->form,
19729 abbrev->implicit_const, info_ptr,
19730 need_reprocess);
19731 }
19732
19733 /* Read dwarf information from a buffer. */
19734
19735 static unsigned int
19736 read_1_byte (bfd *abfd, const gdb_byte *buf)
19737 {
19738 return bfd_get_8 (abfd, buf);
19739 }
19740
19741 static int
19742 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
19743 {
19744 return bfd_get_signed_8 (abfd, buf);
19745 }
19746
19747 static unsigned int
19748 read_2_bytes (bfd *abfd, const gdb_byte *buf)
19749 {
19750 return bfd_get_16 (abfd, buf);
19751 }
19752
19753 static int
19754 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
19755 {
19756 return bfd_get_signed_16 (abfd, buf);
19757 }
19758
19759 static unsigned int
19760 read_3_bytes (bfd *abfd, const gdb_byte *buf)
19761 {
19762 unsigned int result = 0;
19763 for (int i = 0; i < 3; ++i)
19764 {
19765 unsigned char byte = bfd_get_8 (abfd, buf);
19766 buf++;
19767 result |= ((unsigned int) byte << (i * 8));
19768 }
19769 return result;
19770 }
19771
19772 static unsigned int
19773 read_4_bytes (bfd *abfd, const gdb_byte *buf)
19774 {
19775 return bfd_get_32 (abfd, buf);
19776 }
19777
19778 static int
19779 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
19780 {
19781 return bfd_get_signed_32 (abfd, buf);
19782 }
19783
19784 static ULONGEST
19785 read_8_bytes (bfd *abfd, const gdb_byte *buf)
19786 {
19787 return bfd_get_64 (abfd, buf);
19788 }
19789
19790 static CORE_ADDR
19791 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
19792 unsigned int *bytes_read)
19793 {
19794 struct comp_unit_head *cu_header = &cu->header;
19795 CORE_ADDR retval = 0;
19796
19797 if (cu_header->signed_addr_p)
19798 {
19799 switch (cu_header->addr_size)
19800 {
19801 case 2:
19802 retval = bfd_get_signed_16 (abfd, buf);
19803 break;
19804 case 4:
19805 retval = bfd_get_signed_32 (abfd, buf);
19806 break;
19807 case 8:
19808 retval = bfd_get_signed_64 (abfd, buf);
19809 break;
19810 default:
19811 internal_error (__FILE__, __LINE__,
19812 _("read_address: bad switch, signed [in module %s]"),
19813 bfd_get_filename (abfd));
19814 }
19815 }
19816 else
19817 {
19818 switch (cu_header->addr_size)
19819 {
19820 case 2:
19821 retval = bfd_get_16 (abfd, buf);
19822 break;
19823 case 4:
19824 retval = bfd_get_32 (abfd, buf);
19825 break;
19826 case 8:
19827 retval = bfd_get_64 (abfd, buf);
19828 break;
19829 default:
19830 internal_error (__FILE__, __LINE__,
19831 _("read_address: bad switch, "
19832 "unsigned [in module %s]"),
19833 bfd_get_filename (abfd));
19834 }
19835 }
19836
19837 *bytes_read = cu_header->addr_size;
19838 return retval;
19839 }
19840
19841 /* Read the initial length from a section. The (draft) DWARF 3
19842 specification allows the initial length to take up either 4 bytes
19843 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19844 bytes describe the length and all offsets will be 8 bytes in length
19845 instead of 4.
19846
19847 An older, non-standard 64-bit format is also handled by this
19848 function. The older format in question stores the initial length
19849 as an 8-byte quantity without an escape value. Lengths greater
19850 than 2^32 aren't very common which means that the initial 4 bytes
19851 is almost always zero. Since a length value of zero doesn't make
19852 sense for the 32-bit format, this initial zero can be considered to
19853 be an escape value which indicates the presence of the older 64-bit
19854 format. As written, the code can't detect (old format) lengths
19855 greater than 4GB. If it becomes necessary to handle lengths
19856 somewhat larger than 4GB, we could allow other small values (such
19857 as the non-sensical values of 1, 2, and 3) to also be used as
19858 escape values indicating the presence of the old format.
19859
19860 The value returned via bytes_read should be used to increment the
19861 relevant pointer after calling read_initial_length().
19862
19863 [ Note: read_initial_length() and read_offset() are based on the
19864 document entitled "DWARF Debugging Information Format", revision
19865 3, draft 8, dated November 19, 2001. This document was obtained
19866 from:
19867
19868 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
19869
19870 This document is only a draft and is subject to change. (So beware.)
19871
19872 Details regarding the older, non-standard 64-bit format were
19873 determined empirically by examining 64-bit ELF files produced by
19874 the SGI toolchain on an IRIX 6.5 machine.
19875
19876 - Kevin, July 16, 2002
19877 ] */
19878
19879 static LONGEST
19880 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
19881 {
19882 LONGEST length = bfd_get_32 (abfd, buf);
19883
19884 if (length == 0xffffffff)
19885 {
19886 length = bfd_get_64 (abfd, buf + 4);
19887 *bytes_read = 12;
19888 }
19889 else if (length == 0)
19890 {
19891 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
19892 length = bfd_get_64 (abfd, buf);
19893 *bytes_read = 8;
19894 }
19895 else
19896 {
19897 *bytes_read = 4;
19898 }
19899
19900 return length;
19901 }
19902
19903 /* Cover function for read_initial_length.
19904 Returns the length of the object at BUF, and stores the size of the
19905 initial length in *BYTES_READ and stores the size that offsets will be in
19906 *OFFSET_SIZE.
19907 If the initial length size is not equivalent to that specified in
19908 CU_HEADER then issue a complaint.
19909 This is useful when reading non-comp-unit headers. */
19910
19911 static LONGEST
19912 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
19913 const struct comp_unit_head *cu_header,
19914 unsigned int *bytes_read,
19915 unsigned int *offset_size)
19916 {
19917 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19918
19919 gdb_assert (cu_header->initial_length_size == 4
19920 || cu_header->initial_length_size == 8
19921 || cu_header->initial_length_size == 12);
19922
19923 if (cu_header->initial_length_size != *bytes_read)
19924 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
19925
19926 *offset_size = (*bytes_read == 4) ? 4 : 8;
19927 return length;
19928 }
19929
19930 /* Read an offset from the data stream. The size of the offset is
19931 given by cu_header->offset_size. */
19932
19933 static LONGEST
19934 read_offset (bfd *abfd, const gdb_byte *buf,
19935 const struct comp_unit_head *cu_header,
19936 unsigned int *bytes_read)
19937 {
19938 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
19939
19940 *bytes_read = cu_header->offset_size;
19941 return offset;
19942 }
19943
19944 /* Read an offset from the data stream. */
19945
19946 static LONGEST
19947 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
19948 {
19949 LONGEST retval = 0;
19950
19951 switch (offset_size)
19952 {
19953 case 4:
19954 retval = bfd_get_32 (abfd, buf);
19955 break;
19956 case 8:
19957 retval = bfd_get_64 (abfd, buf);
19958 break;
19959 default:
19960 internal_error (__FILE__, __LINE__,
19961 _("read_offset_1: bad switch [in module %s]"),
19962 bfd_get_filename (abfd));
19963 }
19964
19965 return retval;
19966 }
19967
19968 static const gdb_byte *
19969 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
19970 {
19971 /* If the size of a host char is 8 bits, we can return a pointer
19972 to the buffer, otherwise we have to copy the data to a buffer
19973 allocated on the temporary obstack. */
19974 gdb_assert (HOST_CHAR_BIT == 8);
19975 return buf;
19976 }
19977
19978 static const char *
19979 read_direct_string (bfd *abfd, const gdb_byte *buf,
19980 unsigned int *bytes_read_ptr)
19981 {
19982 /* If the size of a host char is 8 bits, we can return a pointer
19983 to the string, otherwise we have to copy the string to a buffer
19984 allocated on the temporary obstack. */
19985 gdb_assert (HOST_CHAR_BIT == 8);
19986 if (*buf == '\0')
19987 {
19988 *bytes_read_ptr = 1;
19989 return NULL;
19990 }
19991 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19992 return (const char *) buf;
19993 }
19994
19995 /* Return pointer to string at section SECT offset STR_OFFSET with error
19996 reporting strings FORM_NAME and SECT_NAME. */
19997
19998 static const char *
19999 read_indirect_string_at_offset_from (struct objfile *objfile,
20000 bfd *abfd, LONGEST str_offset,
20001 struct dwarf2_section_info *sect,
20002 const char *form_name,
20003 const char *sect_name)
20004 {
20005 dwarf2_read_section (objfile, sect);
20006 if (sect->buffer == NULL)
20007 error (_("%s used without %s section [in module %s]"),
20008 form_name, sect_name, bfd_get_filename (abfd));
20009 if (str_offset >= sect->size)
20010 error (_("%s pointing outside of %s section [in module %s]"),
20011 form_name, sect_name, bfd_get_filename (abfd));
20012 gdb_assert (HOST_CHAR_BIT == 8);
20013 if (sect->buffer[str_offset] == '\0')
20014 return NULL;
20015 return (const char *) (sect->buffer + str_offset);
20016 }
20017
20018 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20019
20020 static const char *
20021 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20022 bfd *abfd, LONGEST str_offset)
20023 {
20024 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20025 abfd, str_offset,
20026 &dwarf2_per_objfile->str,
20027 "DW_FORM_strp", ".debug_str");
20028 }
20029
20030 /* Return pointer to string at .debug_line_str offset STR_OFFSET. */
20031
20032 static const char *
20033 read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
20034 bfd *abfd, LONGEST str_offset)
20035 {
20036 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
20037 abfd, str_offset,
20038 &dwarf2_per_objfile->line_str,
20039 "DW_FORM_line_strp",
20040 ".debug_line_str");
20041 }
20042
20043 /* Read a string at offset STR_OFFSET in the .debug_str section from
20044 the .dwz file DWZ. Throw an error if the offset is too large. If
20045 the string consists of a single NUL byte, return NULL; otherwise
20046 return a pointer to the string. */
20047
20048 static const char *
20049 read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
20050 LONGEST str_offset)
20051 {
20052 dwarf2_read_section (objfile, &dwz->str);
20053
20054 if (dwz->str.buffer == NULL)
20055 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
20056 "section [in module %s]"),
20057 bfd_get_filename (dwz->dwz_bfd.get ()));
20058 if (str_offset >= dwz->str.size)
20059 error (_("DW_FORM_GNU_strp_alt pointing outside of "
20060 ".debug_str section [in module %s]"),
20061 bfd_get_filename (dwz->dwz_bfd.get ()));
20062 gdb_assert (HOST_CHAR_BIT == 8);
20063 if (dwz->str.buffer[str_offset] == '\0')
20064 return NULL;
20065 return (const char *) (dwz->str.buffer + str_offset);
20066 }
20067
20068 /* Return pointer to string at .debug_str offset as read from BUF.
20069 BUF is assumed to be in a compilation unit described by CU_HEADER.
20070 Return *BYTES_READ_PTR count of bytes read from BUF. */
20071
20072 static const char *
20073 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
20074 const gdb_byte *buf,
20075 const struct comp_unit_head *cu_header,
20076 unsigned int *bytes_read_ptr)
20077 {
20078 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20079
20080 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
20081 }
20082
20083 /* Return pointer to string at .debug_line_str offset as read from BUF.
20084 BUF is assumed to be in a compilation unit described by CU_HEADER.
20085 Return *BYTES_READ_PTR count of bytes read from BUF. */
20086
20087 static const char *
20088 read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
20089 bfd *abfd, const gdb_byte *buf,
20090 const struct comp_unit_head *cu_header,
20091 unsigned int *bytes_read_ptr)
20092 {
20093 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
20094
20095 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
20096 str_offset);
20097 }
20098
20099 ULONGEST
20100 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
20101 unsigned int *bytes_read_ptr)
20102 {
20103 ULONGEST result;
20104 unsigned int num_read;
20105 int shift;
20106 unsigned char byte;
20107
20108 result = 0;
20109 shift = 0;
20110 num_read = 0;
20111 while (1)
20112 {
20113 byte = bfd_get_8 (abfd, buf);
20114 buf++;
20115 num_read++;
20116 result |= ((ULONGEST) (byte & 127) << shift);
20117 if ((byte & 128) == 0)
20118 {
20119 break;
20120 }
20121 shift += 7;
20122 }
20123 *bytes_read_ptr = num_read;
20124 return result;
20125 }
20126
20127 static LONGEST
20128 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
20129 unsigned int *bytes_read_ptr)
20130 {
20131 ULONGEST result;
20132 int shift, num_read;
20133 unsigned char byte;
20134
20135 result = 0;
20136 shift = 0;
20137 num_read = 0;
20138 while (1)
20139 {
20140 byte = bfd_get_8 (abfd, buf);
20141 buf++;
20142 num_read++;
20143 result |= ((ULONGEST) (byte & 127) << shift);
20144 shift += 7;
20145 if ((byte & 128) == 0)
20146 {
20147 break;
20148 }
20149 }
20150 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
20151 result |= -(((ULONGEST) 1) << shift);
20152 *bytes_read_ptr = num_read;
20153 return result;
20154 }
20155
20156 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20157 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20158 ADDR_SIZE is the size of addresses from the CU header. */
20159
20160 static CORE_ADDR
20161 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
20162 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
20163 int addr_size)
20164 {
20165 struct objfile *objfile = dwarf2_per_objfile->objfile;
20166 bfd *abfd = objfile->obfd;
20167 const gdb_byte *info_ptr;
20168 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20169
20170 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
20171 if (dwarf2_per_objfile->addr.buffer == NULL)
20172 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20173 objfile_name (objfile));
20174 if (addr_base_or_zero + addr_index * addr_size
20175 >= dwarf2_per_objfile->addr.size)
20176 error (_("DW_FORM_addr_index pointing outside of "
20177 ".debug_addr section [in module %s]"),
20178 objfile_name (objfile));
20179 info_ptr = (dwarf2_per_objfile->addr.buffer
20180 + addr_base_or_zero + addr_index * addr_size);
20181 if (addr_size == 4)
20182 return bfd_get_32 (abfd, info_ptr);
20183 else
20184 return bfd_get_64 (abfd, info_ptr);
20185 }
20186
20187 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20188
20189 static CORE_ADDR
20190 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20191 {
20192 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
20193 cu->addr_base, cu->header.addr_size);
20194 }
20195
20196 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20197
20198 static CORE_ADDR
20199 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20200 unsigned int *bytes_read)
20201 {
20202 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
20203 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20204
20205 return read_addr_index (cu, addr_index);
20206 }
20207
20208 /* Given an index in .debug_addr, fetch the value.
20209 NOTE: This can be called during dwarf expression evaluation,
20210 long after the debug information has been read, and thus per_cu->cu
20211 may no longer exist. */
20212
20213 CORE_ADDR
20214 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
20215 unsigned int addr_index)
20216 {
20217 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
20218 struct dwarf2_cu *cu = per_cu->cu;
20219 gdb::optional<ULONGEST> addr_base;
20220 int addr_size;
20221
20222 /* We need addr_base and addr_size.
20223 If we don't have PER_CU->cu, we have to get it.
20224 Nasty, but the alternative is storing the needed info in PER_CU,
20225 which at this point doesn't seem justified: it's not clear how frequently
20226 it would get used and it would increase the size of every PER_CU.
20227 Entry points like dwarf2_per_cu_addr_size do a similar thing
20228 so we're not in uncharted territory here.
20229 Alas we need to be a bit more complicated as addr_base is contained
20230 in the DIE.
20231
20232 We don't need to read the entire CU(/TU).
20233 We just need the header and top level die.
20234
20235 IWBN to use the aging mechanism to let us lazily later discard the CU.
20236 For now we skip this optimization. */
20237
20238 if (cu != NULL)
20239 {
20240 addr_base = cu->addr_base;
20241 addr_size = cu->header.addr_size;
20242 }
20243 else
20244 {
20245 cutu_reader reader (per_cu, NULL, 0, 0, false);
20246 addr_base = reader.cu->addr_base;
20247 addr_size = reader.cu->header.addr_size;
20248 }
20249
20250 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20251 addr_size);
20252 }
20253
20254 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20255 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20256 DWO file. */
20257
20258 static const char *
20259 read_str_index (struct dwarf2_cu *cu,
20260 struct dwarf2_section_info *str_section,
20261 struct dwarf2_section_info *str_offsets_section,
20262 ULONGEST str_offsets_base, ULONGEST str_index)
20263 {
20264 struct dwarf2_per_objfile *dwarf2_per_objfile
20265 = cu->per_cu->dwarf2_per_objfile;
20266 struct objfile *objfile = dwarf2_per_objfile->objfile;
20267 const char *objf_name = objfile_name (objfile);
20268 bfd *abfd = objfile->obfd;
20269 const gdb_byte *info_ptr;
20270 ULONGEST str_offset;
20271 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20272
20273 dwarf2_read_section (objfile, str_section);
20274 dwarf2_read_section (objfile, str_offsets_section);
20275 if (str_section->buffer == NULL)
20276 error (_("%s used without %s section"
20277 " in CU at offset %s [in module %s]"),
20278 form_name, get_section_name (str_section),
20279 sect_offset_str (cu->header.sect_off), objf_name);
20280 if (str_offsets_section->buffer == NULL)
20281 error (_("%s used without %s section"
20282 " in CU at offset %s [in module %s]"),
20283 form_name, get_section_name (str_section),
20284 sect_offset_str (cu->header.sect_off), objf_name);
20285 info_ptr = (str_offsets_section->buffer
20286 + str_offsets_base
20287 + str_index * cu->header.offset_size);
20288 if (cu->header.offset_size == 4)
20289 str_offset = bfd_get_32 (abfd, info_ptr);
20290 else
20291 str_offset = bfd_get_64 (abfd, info_ptr);
20292 if (str_offset >= str_section->size)
20293 error (_("Offset from %s pointing outside of"
20294 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20295 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20296 return (const char *) (str_section->buffer + str_offset);
20297 }
20298
20299 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20300
20301 static const char *
20302 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20303 {
20304 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20305 ? reader->cu->header.addr_size : 0;
20306 return read_str_index (reader->cu,
20307 &reader->dwo_file->sections.str,
20308 &reader->dwo_file->sections.str_offsets,
20309 str_offsets_base, str_index);
20310 }
20311
20312 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20313
20314 static const char *
20315 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20316 {
20317 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20318 const char *objf_name = objfile_name (objfile);
20319 static const char form_name[] = "DW_FORM_GNU_str_index";
20320 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20321
20322 if (!cu->str_offsets_base.has_value ())
20323 error (_("%s used in Fission stub without %s"
20324 " in CU at offset 0x%lx [in module %s]"),
20325 form_name, str_offsets_attr_name,
20326 (long) cu->header.offset_size, objf_name);
20327
20328 return read_str_index (cu,
20329 &cu->per_cu->dwarf2_per_objfile->str,
20330 &cu->per_cu->dwarf2_per_objfile->str_offsets,
20331 *cu->str_offsets_base, str_index);
20332 }
20333
20334 /* Return the length of an LEB128 number in BUF. */
20335
20336 static int
20337 leb128_size (const gdb_byte *buf)
20338 {
20339 const gdb_byte *begin = buf;
20340 gdb_byte byte;
20341
20342 while (1)
20343 {
20344 byte = *buf++;
20345 if ((byte & 128) == 0)
20346 return buf - begin;
20347 }
20348 }
20349
20350 static void
20351 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20352 {
20353 switch (lang)
20354 {
20355 case DW_LANG_C89:
20356 case DW_LANG_C99:
20357 case DW_LANG_C11:
20358 case DW_LANG_C:
20359 case DW_LANG_UPC:
20360 cu->language = language_c;
20361 break;
20362 case DW_LANG_Java:
20363 case DW_LANG_C_plus_plus:
20364 case DW_LANG_C_plus_plus_11:
20365 case DW_LANG_C_plus_plus_14:
20366 cu->language = language_cplus;
20367 break;
20368 case DW_LANG_D:
20369 cu->language = language_d;
20370 break;
20371 case DW_LANG_Fortran77:
20372 case DW_LANG_Fortran90:
20373 case DW_LANG_Fortran95:
20374 case DW_LANG_Fortran03:
20375 case DW_LANG_Fortran08:
20376 cu->language = language_fortran;
20377 break;
20378 case DW_LANG_Go:
20379 cu->language = language_go;
20380 break;
20381 case DW_LANG_Mips_Assembler:
20382 cu->language = language_asm;
20383 break;
20384 case DW_LANG_Ada83:
20385 case DW_LANG_Ada95:
20386 cu->language = language_ada;
20387 break;
20388 case DW_LANG_Modula2:
20389 cu->language = language_m2;
20390 break;
20391 case DW_LANG_Pascal83:
20392 cu->language = language_pascal;
20393 break;
20394 case DW_LANG_ObjC:
20395 cu->language = language_objc;
20396 break;
20397 case DW_LANG_Rust:
20398 case DW_LANG_Rust_old:
20399 cu->language = language_rust;
20400 break;
20401 case DW_LANG_Cobol74:
20402 case DW_LANG_Cobol85:
20403 default:
20404 cu->language = language_minimal;
20405 break;
20406 }
20407 cu->language_defn = language_def (cu->language);
20408 }
20409
20410 /* Return the named attribute or NULL if not there. */
20411
20412 static struct attribute *
20413 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20414 {
20415 for (;;)
20416 {
20417 unsigned int i;
20418 struct attribute *spec = NULL;
20419
20420 for (i = 0; i < die->num_attrs; ++i)
20421 {
20422 if (die->attrs[i].name == name)
20423 return &die->attrs[i];
20424 if (die->attrs[i].name == DW_AT_specification
20425 || die->attrs[i].name == DW_AT_abstract_origin)
20426 spec = &die->attrs[i];
20427 }
20428
20429 if (!spec)
20430 break;
20431
20432 die = follow_die_ref (die, spec, &cu);
20433 }
20434
20435 return NULL;
20436 }
20437
20438 /* Return the named attribute or NULL if not there,
20439 but do not follow DW_AT_specification, etc.
20440 This is for use in contexts where we're reading .debug_types dies.
20441 Following DW_AT_specification, DW_AT_abstract_origin will take us
20442 back up the chain, and we want to go down. */
20443
20444 static struct attribute *
20445 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
20446 {
20447 unsigned int i;
20448
20449 for (i = 0; i < die->num_attrs; ++i)
20450 if (die->attrs[i].name == name)
20451 return &die->attrs[i];
20452
20453 return NULL;
20454 }
20455
20456 /* Return the string associated with a string-typed attribute, or NULL if it
20457 is either not found or is of an incorrect type. */
20458
20459 static const char *
20460 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20461 {
20462 struct attribute *attr;
20463 const char *str = NULL;
20464
20465 attr = dwarf2_attr (die, name, cu);
20466
20467 if (attr != NULL)
20468 {
20469 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
20470 || attr->form == DW_FORM_string
20471 || attr->form == DW_FORM_strx
20472 || attr->form == DW_FORM_strx1
20473 || attr->form == DW_FORM_strx2
20474 || attr->form == DW_FORM_strx3
20475 || attr->form == DW_FORM_strx4
20476 || attr->form == DW_FORM_GNU_str_index
20477 || attr->form == DW_FORM_GNU_strp_alt)
20478 str = DW_STRING (attr);
20479 else
20480 complaint (_("string type expected for attribute %s for "
20481 "DIE at %s in module %s"),
20482 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20483 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
20484 }
20485
20486 return str;
20487 }
20488
20489 /* Return the dwo name or NULL if not present. If present, it is in either
20490 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20491 static const char *
20492 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20493 {
20494 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20495 if (dwo_name == nullptr)
20496 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20497 return dwo_name;
20498 }
20499
20500 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20501 and holds a non-zero value. This function should only be used for
20502 DW_FORM_flag or DW_FORM_flag_present attributes. */
20503
20504 static int
20505 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20506 {
20507 struct attribute *attr = dwarf2_attr (die, name, cu);
20508
20509 return (attr && DW_UNSND (attr));
20510 }
20511
20512 static int
20513 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20514 {
20515 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20516 which value is non-zero. However, we have to be careful with
20517 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20518 (via dwarf2_flag_true_p) follows this attribute. So we may
20519 end up accidently finding a declaration attribute that belongs
20520 to a different DIE referenced by the specification attribute,
20521 even though the given DIE does not have a declaration attribute. */
20522 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20523 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20524 }
20525
20526 /* Return the die giving the specification for DIE, if there is
20527 one. *SPEC_CU is the CU containing DIE on input, and the CU
20528 containing the return value on output. If there is no
20529 specification, but there is an abstract origin, that is
20530 returned. */
20531
20532 static struct die_info *
20533 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20534 {
20535 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20536 *spec_cu);
20537
20538 if (spec_attr == NULL)
20539 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20540
20541 if (spec_attr == NULL)
20542 return NULL;
20543 else
20544 return follow_die_ref (die, spec_attr, spec_cu);
20545 }
20546
20547 /* Stub for free_line_header to match void * callback types. */
20548
20549 static void
20550 free_line_header_voidp (void *arg)
20551 {
20552 struct line_header *lh = (struct line_header *) arg;
20553
20554 delete lh;
20555 }
20556
20557 void
20558 line_header::add_include_dir (const char *include_dir)
20559 {
20560 if (dwarf_line_debug >= 2)
20561 {
20562 size_t new_size;
20563 if (version >= 5)
20564 new_size = m_include_dirs.size ();
20565 else
20566 new_size = m_include_dirs.size () + 1;
20567 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20568 new_size, include_dir);
20569 }
20570 m_include_dirs.push_back (include_dir);
20571 }
20572
20573 void
20574 line_header::add_file_name (const char *name,
20575 dir_index d_index,
20576 unsigned int mod_time,
20577 unsigned int length)
20578 {
20579 if (dwarf_line_debug >= 2)
20580 {
20581 size_t new_size;
20582 if (version >= 5)
20583 new_size = file_names_size ();
20584 else
20585 new_size = file_names_size () + 1;
20586 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20587 new_size, name);
20588 }
20589 m_file_names.emplace_back (name, d_index, mod_time, length);
20590 }
20591
20592 /* A convenience function to find the proper .debug_line section for a CU. */
20593
20594 static struct dwarf2_section_info *
20595 get_debug_line_section (struct dwarf2_cu *cu)
20596 {
20597 struct dwarf2_section_info *section;
20598 struct dwarf2_per_objfile *dwarf2_per_objfile
20599 = cu->per_cu->dwarf2_per_objfile;
20600
20601 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20602 DWO file. */
20603 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20604 section = &cu->dwo_unit->dwo_file->sections.line;
20605 else if (cu->per_cu->is_dwz)
20606 {
20607 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
20608
20609 section = &dwz->line;
20610 }
20611 else
20612 section = &dwarf2_per_objfile->line;
20613
20614 return section;
20615 }
20616
20617 /* Read directory or file name entry format, starting with byte of
20618 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20619 entries count and the entries themselves in the described entry
20620 format. */
20621
20622 static void
20623 read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20624 bfd *abfd, const gdb_byte **bufp,
20625 struct line_header *lh,
20626 const struct comp_unit_head *cu_header,
20627 void (*callback) (struct line_header *lh,
20628 const char *name,
20629 dir_index d_index,
20630 unsigned int mod_time,
20631 unsigned int length))
20632 {
20633 gdb_byte format_count, formati;
20634 ULONGEST data_count, datai;
20635 const gdb_byte *buf = *bufp;
20636 const gdb_byte *format_header_data;
20637 unsigned int bytes_read;
20638
20639 format_count = read_1_byte (abfd, buf);
20640 buf += 1;
20641 format_header_data = buf;
20642 for (formati = 0; formati < format_count; formati++)
20643 {
20644 read_unsigned_leb128 (abfd, buf, &bytes_read);
20645 buf += bytes_read;
20646 read_unsigned_leb128 (abfd, buf, &bytes_read);
20647 buf += bytes_read;
20648 }
20649
20650 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20651 buf += bytes_read;
20652 for (datai = 0; datai < data_count; datai++)
20653 {
20654 const gdb_byte *format = format_header_data;
20655 struct file_entry fe;
20656
20657 for (formati = 0; formati < format_count; formati++)
20658 {
20659 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
20660 format += bytes_read;
20661
20662 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
20663 format += bytes_read;
20664
20665 gdb::optional<const char *> string;
20666 gdb::optional<unsigned int> uint;
20667
20668 switch (form)
20669 {
20670 case DW_FORM_string:
20671 string.emplace (read_direct_string (abfd, buf, &bytes_read));
20672 buf += bytes_read;
20673 break;
20674
20675 case DW_FORM_line_strp:
20676 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20677 abfd, buf,
20678 cu_header,
20679 &bytes_read));
20680 buf += bytes_read;
20681 break;
20682
20683 case DW_FORM_data1:
20684 uint.emplace (read_1_byte (abfd, buf));
20685 buf += 1;
20686 break;
20687
20688 case DW_FORM_data2:
20689 uint.emplace (read_2_bytes (abfd, buf));
20690 buf += 2;
20691 break;
20692
20693 case DW_FORM_data4:
20694 uint.emplace (read_4_bytes (abfd, buf));
20695 buf += 4;
20696 break;
20697
20698 case DW_FORM_data8:
20699 uint.emplace (read_8_bytes (abfd, buf));
20700 buf += 8;
20701 break;
20702
20703 case DW_FORM_data16:
20704 /* This is used for MD5, but file_entry does not record MD5s. */
20705 buf += 16;
20706 break;
20707
20708 case DW_FORM_udata:
20709 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
20710 buf += bytes_read;
20711 break;
20712
20713 case DW_FORM_block:
20714 /* It is valid only for DW_LNCT_timestamp which is ignored by
20715 current GDB. */
20716 break;
20717 }
20718
20719 switch (content_type)
20720 {
20721 case DW_LNCT_path:
20722 if (string.has_value ())
20723 fe.name = *string;
20724 break;
20725 case DW_LNCT_directory_index:
20726 if (uint.has_value ())
20727 fe.d_index = (dir_index) *uint;
20728 break;
20729 case DW_LNCT_timestamp:
20730 if (uint.has_value ())
20731 fe.mod_time = *uint;
20732 break;
20733 case DW_LNCT_size:
20734 if (uint.has_value ())
20735 fe.length = *uint;
20736 break;
20737 case DW_LNCT_MD5:
20738 break;
20739 default:
20740 complaint (_("Unknown format content type %s"),
20741 pulongest (content_type));
20742 }
20743 }
20744
20745 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
20746 }
20747
20748 *bufp = buf;
20749 }
20750
20751 /* Read the statement program header starting at OFFSET in
20752 .debug_line, or .debug_line.dwo. Return a pointer
20753 to a struct line_header, allocated using xmalloc.
20754 Returns NULL if there is a problem reading the header, e.g., if it
20755 has a version we don't understand.
20756
20757 NOTE: the strings in the include directory and file name tables of
20758 the returned object point into the dwarf line section buffer,
20759 and must not be freed. */
20760
20761 static line_header_up
20762 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20763 {
20764 const gdb_byte *line_ptr;
20765 unsigned int bytes_read, offset_size;
20766 int i;
20767 const char *cur_dir, *cur_file;
20768 struct dwarf2_section_info *section;
20769 bfd *abfd;
20770 struct dwarf2_per_objfile *dwarf2_per_objfile
20771 = cu->per_cu->dwarf2_per_objfile;
20772
20773 section = get_debug_line_section (cu);
20774 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20775 if (section->buffer == NULL)
20776 {
20777 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20778 complaint (_("missing .debug_line.dwo section"));
20779 else
20780 complaint (_("missing .debug_line section"));
20781 return 0;
20782 }
20783
20784 /* We can't do this until we know the section is non-empty.
20785 Only then do we know we have such a section. */
20786 abfd = get_section_bfd_owner (section);
20787
20788 /* Make sure that at least there's room for the total_length field.
20789 That could be 12 bytes long, but we're just going to fudge that. */
20790 if (to_underlying (sect_off) + 4 >= section->size)
20791 {
20792 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20793 return 0;
20794 }
20795
20796 line_header_up lh (new line_header ());
20797
20798 lh->sect_off = sect_off;
20799 lh->offset_in_dwz = cu->per_cu->is_dwz;
20800
20801 line_ptr = section->buffer + to_underlying (sect_off);
20802
20803 /* Read in the header. */
20804 lh->total_length =
20805 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20806 &bytes_read, &offset_size);
20807 line_ptr += bytes_read;
20808
20809 const gdb_byte *start_here = line_ptr;
20810
20811 if (line_ptr + lh->total_length > (section->buffer + section->size))
20812 {
20813 dwarf2_statement_list_fits_in_line_number_section_complaint ();
20814 return 0;
20815 }
20816 lh->statement_program_end = start_here + lh->total_length;
20817 lh->version = read_2_bytes (abfd, line_ptr);
20818 line_ptr += 2;
20819 if (lh->version > 5)
20820 {
20821 /* This is a version we don't understand. The format could have
20822 changed in ways we don't handle properly so just punt. */
20823 complaint (_("unsupported version in .debug_line section"));
20824 return NULL;
20825 }
20826 if (lh->version >= 5)
20827 {
20828 gdb_byte segment_selector_size;
20829
20830 /* Skip address size. */
20831 read_1_byte (abfd, line_ptr);
20832 line_ptr += 1;
20833
20834 segment_selector_size = read_1_byte (abfd, line_ptr);
20835 line_ptr += 1;
20836 if (segment_selector_size != 0)
20837 {
20838 complaint (_("unsupported segment selector size %u "
20839 "in .debug_line section"),
20840 segment_selector_size);
20841 return NULL;
20842 }
20843 }
20844 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20845 line_ptr += offset_size;
20846 lh->statement_program_start = line_ptr + lh->header_length;
20847 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20848 line_ptr += 1;
20849 if (lh->version >= 4)
20850 {
20851 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20852 line_ptr += 1;
20853 }
20854 else
20855 lh->maximum_ops_per_instruction = 1;
20856
20857 if (lh->maximum_ops_per_instruction == 0)
20858 {
20859 lh->maximum_ops_per_instruction = 1;
20860 complaint (_("invalid maximum_ops_per_instruction "
20861 "in `.debug_line' section"));
20862 }
20863
20864 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20865 line_ptr += 1;
20866 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20867 line_ptr += 1;
20868 lh->line_range = read_1_byte (abfd, line_ptr);
20869 line_ptr += 1;
20870 lh->opcode_base = read_1_byte (abfd, line_ptr);
20871 line_ptr += 1;
20872 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
20873
20874 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20875 for (i = 1; i < lh->opcode_base; ++i)
20876 {
20877 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20878 line_ptr += 1;
20879 }
20880
20881 if (lh->version >= 5)
20882 {
20883 /* Read directory table. */
20884 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20885 &cu->header,
20886 [] (struct line_header *header, const char *name,
20887 dir_index d_index, unsigned int mod_time,
20888 unsigned int length)
20889 {
20890 header->add_include_dir (name);
20891 });
20892
20893 /* Read file name table. */
20894 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20895 &cu->header,
20896 [] (struct line_header *header, const char *name,
20897 dir_index d_index, unsigned int mod_time,
20898 unsigned int length)
20899 {
20900 header->add_file_name (name, d_index, mod_time, length);
20901 });
20902 }
20903 else
20904 {
20905 /* Read directory table. */
20906 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20907 {
20908 line_ptr += bytes_read;
20909 lh->add_include_dir (cur_dir);
20910 }
20911 line_ptr += bytes_read;
20912
20913 /* Read file name table. */
20914 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20915 {
20916 unsigned int mod_time, length;
20917 dir_index d_index;
20918
20919 line_ptr += bytes_read;
20920 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20921 line_ptr += bytes_read;
20922 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20923 line_ptr += bytes_read;
20924 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20925 line_ptr += bytes_read;
20926
20927 lh->add_file_name (cur_file, d_index, mod_time, length);
20928 }
20929 line_ptr += bytes_read;
20930 }
20931
20932 if (line_ptr > (section->buffer + section->size))
20933 complaint (_("line number info header doesn't "
20934 "fit in `.debug_line' section"));
20935
20936 return lh;
20937 }
20938
20939 /* Subroutine of dwarf_decode_lines to simplify it.
20940 Return the file name of the psymtab for the given file_entry.
20941 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20942 If space for the result is malloc'd, *NAME_HOLDER will be set.
20943 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20944
20945 static const char *
20946 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20947 const struct partial_symtab *pst,
20948 const char *comp_dir,
20949 gdb::unique_xmalloc_ptr<char> *name_holder)
20950 {
20951 const char *include_name = fe.name;
20952 const char *include_name_to_compare = include_name;
20953 const char *pst_filename;
20954 int file_is_pst;
20955
20956 const char *dir_name = fe.include_dir (lh);
20957
20958 gdb::unique_xmalloc_ptr<char> hold_compare;
20959 if (!IS_ABSOLUTE_PATH (include_name)
20960 && (dir_name != NULL || comp_dir != NULL))
20961 {
20962 /* Avoid creating a duplicate psymtab for PST.
20963 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20964 Before we do the comparison, however, we need to account
20965 for DIR_NAME and COMP_DIR.
20966 First prepend dir_name (if non-NULL). If we still don't
20967 have an absolute path prepend comp_dir (if non-NULL).
20968 However, the directory we record in the include-file's
20969 psymtab does not contain COMP_DIR (to match the
20970 corresponding symtab(s)).
20971
20972 Example:
20973
20974 bash$ cd /tmp
20975 bash$ gcc -g ./hello.c
20976 include_name = "hello.c"
20977 dir_name = "."
20978 DW_AT_comp_dir = comp_dir = "/tmp"
20979 DW_AT_name = "./hello.c"
20980
20981 */
20982
20983 if (dir_name != NULL)
20984 {
20985 name_holder->reset (concat (dir_name, SLASH_STRING,
20986 include_name, (char *) NULL));
20987 include_name = name_holder->get ();
20988 include_name_to_compare = include_name;
20989 }
20990 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20991 {
20992 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20993 include_name, (char *) NULL));
20994 include_name_to_compare = hold_compare.get ();
20995 }
20996 }
20997
20998 pst_filename = pst->filename;
20999 gdb::unique_xmalloc_ptr<char> copied_name;
21000 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21001 {
21002 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21003 pst_filename, (char *) NULL));
21004 pst_filename = copied_name.get ();
21005 }
21006
21007 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21008
21009 if (file_is_pst)
21010 return NULL;
21011 return include_name;
21012 }
21013
21014 /* State machine to track the state of the line number program. */
21015
21016 class lnp_state_machine
21017 {
21018 public:
21019 /* Initialize a machine state for the start of a line number
21020 program. */
21021 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21022 bool record_lines_p);
21023
21024 file_entry *current_file ()
21025 {
21026 /* lh->file_names is 0-based, but the file name numbers in the
21027 statement program are 1-based. */
21028 return m_line_header->file_name_at (m_file);
21029 }
21030
21031 /* Record the line in the state machine. END_SEQUENCE is true if
21032 we're processing the end of a sequence. */
21033 void record_line (bool end_sequence);
21034
21035 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
21036 nop-out rest of the lines in this sequence. */
21037 void check_line_address (struct dwarf2_cu *cu,
21038 const gdb_byte *line_ptr,
21039 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21040
21041 void handle_set_discriminator (unsigned int discriminator)
21042 {
21043 m_discriminator = discriminator;
21044 m_line_has_non_zero_discriminator |= discriminator != 0;
21045 }
21046
21047 /* Handle DW_LNE_set_address. */
21048 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21049 {
21050 m_op_index = 0;
21051 address += baseaddr;
21052 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21053 }
21054
21055 /* Handle DW_LNS_advance_pc. */
21056 void handle_advance_pc (CORE_ADDR adjust);
21057
21058 /* Handle a special opcode. */
21059 void handle_special_opcode (unsigned char op_code);
21060
21061 /* Handle DW_LNS_advance_line. */
21062 void handle_advance_line (int line_delta)
21063 {
21064 advance_line (line_delta);
21065 }
21066
21067 /* Handle DW_LNS_set_file. */
21068 void handle_set_file (file_name_index file);
21069
21070 /* Handle DW_LNS_negate_stmt. */
21071 void handle_negate_stmt ()
21072 {
21073 m_is_stmt = !m_is_stmt;
21074 }
21075
21076 /* Handle DW_LNS_const_add_pc. */
21077 void handle_const_add_pc ();
21078
21079 /* Handle DW_LNS_fixed_advance_pc. */
21080 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21081 {
21082 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21083 m_op_index = 0;
21084 }
21085
21086 /* Handle DW_LNS_copy. */
21087 void handle_copy ()
21088 {
21089 record_line (false);
21090 m_discriminator = 0;
21091 }
21092
21093 /* Handle DW_LNE_end_sequence. */
21094 void handle_end_sequence ()
21095 {
21096 m_currently_recording_lines = true;
21097 }
21098
21099 private:
21100 /* Advance the line by LINE_DELTA. */
21101 void advance_line (int line_delta)
21102 {
21103 m_line += line_delta;
21104
21105 if (line_delta != 0)
21106 m_line_has_non_zero_discriminator = m_discriminator != 0;
21107 }
21108
21109 struct dwarf2_cu *m_cu;
21110
21111 gdbarch *m_gdbarch;
21112
21113 /* True if we're recording lines.
21114 Otherwise we're building partial symtabs and are just interested in
21115 finding include files mentioned by the line number program. */
21116 bool m_record_lines_p;
21117
21118 /* The line number header. */
21119 line_header *m_line_header;
21120
21121 /* These are part of the standard DWARF line number state machine,
21122 and initialized according to the DWARF spec. */
21123
21124 unsigned char m_op_index = 0;
21125 /* The line table index of the current file. */
21126 file_name_index m_file = 1;
21127 unsigned int m_line = 1;
21128
21129 /* These are initialized in the constructor. */
21130
21131 CORE_ADDR m_address;
21132 bool m_is_stmt;
21133 unsigned int m_discriminator;
21134
21135 /* Additional bits of state we need to track. */
21136
21137 /* The last file that we called dwarf2_start_subfile for.
21138 This is only used for TLLs. */
21139 unsigned int m_last_file = 0;
21140 /* The last file a line number was recorded for. */
21141 struct subfile *m_last_subfile = NULL;
21142
21143 /* When true, record the lines we decode. */
21144 bool m_currently_recording_lines = false;
21145
21146 /* The last line number that was recorded, used to coalesce
21147 consecutive entries for the same line. This can happen, for
21148 example, when discriminators are present. PR 17276. */
21149 unsigned int m_last_line = 0;
21150 bool m_line_has_non_zero_discriminator = false;
21151 };
21152
21153 void
21154 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21155 {
21156 CORE_ADDR addr_adj = (((m_op_index + adjust)
21157 / m_line_header->maximum_ops_per_instruction)
21158 * m_line_header->minimum_instruction_length);
21159 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21160 m_op_index = ((m_op_index + adjust)
21161 % m_line_header->maximum_ops_per_instruction);
21162 }
21163
21164 void
21165 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21166 {
21167 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21168 CORE_ADDR addr_adj = (((m_op_index
21169 + (adj_opcode / m_line_header->line_range))
21170 / m_line_header->maximum_ops_per_instruction)
21171 * m_line_header->minimum_instruction_length);
21172 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21173 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
21174 % m_line_header->maximum_ops_per_instruction);
21175
21176 int line_delta = (m_line_header->line_base
21177 + (adj_opcode % m_line_header->line_range));
21178 advance_line (line_delta);
21179 record_line (false);
21180 m_discriminator = 0;
21181 }
21182
21183 void
21184 lnp_state_machine::handle_set_file (file_name_index file)
21185 {
21186 m_file = file;
21187
21188 const file_entry *fe = current_file ();
21189 if (fe == NULL)
21190 dwarf2_debug_line_missing_file_complaint ();
21191 else if (m_record_lines_p)
21192 {
21193 const char *dir = fe->include_dir (m_line_header);
21194
21195 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21196 m_line_has_non_zero_discriminator = m_discriminator != 0;
21197 dwarf2_start_subfile (m_cu, fe->name, dir);
21198 }
21199 }
21200
21201 void
21202 lnp_state_machine::handle_const_add_pc ()
21203 {
21204 CORE_ADDR adjust
21205 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21206
21207 CORE_ADDR addr_adj
21208 = (((m_op_index + adjust)
21209 / m_line_header->maximum_ops_per_instruction)
21210 * m_line_header->minimum_instruction_length);
21211
21212 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21213 m_op_index = ((m_op_index + adjust)
21214 % m_line_header->maximum_ops_per_instruction);
21215 }
21216
21217 /* Return non-zero if we should add LINE to the line number table.
21218 LINE is the line to add, LAST_LINE is the last line that was added,
21219 LAST_SUBFILE is the subfile for LAST_LINE.
21220 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21221 had a non-zero discriminator.
21222
21223 We have to be careful in the presence of discriminators.
21224 E.g., for this line:
21225
21226 for (i = 0; i < 100000; i++);
21227
21228 clang can emit four line number entries for that one line,
21229 each with a different discriminator.
21230 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21231
21232 However, we want gdb to coalesce all four entries into one.
21233 Otherwise the user could stepi into the middle of the line and
21234 gdb would get confused about whether the pc really was in the
21235 middle of the line.
21236
21237 Things are further complicated by the fact that two consecutive
21238 line number entries for the same line is a heuristic used by gcc
21239 to denote the end of the prologue. So we can't just discard duplicate
21240 entries, we have to be selective about it. The heuristic we use is
21241 that we only collapse consecutive entries for the same line if at least
21242 one of those entries has a non-zero discriminator. PR 17276.
21243
21244 Note: Addresses in the line number state machine can never go backwards
21245 within one sequence, thus this coalescing is ok. */
21246
21247 static int
21248 dwarf_record_line_p (struct dwarf2_cu *cu,
21249 unsigned int line, unsigned int last_line,
21250 int line_has_non_zero_discriminator,
21251 struct subfile *last_subfile)
21252 {
21253 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21254 return 1;
21255 if (line != last_line)
21256 return 1;
21257 /* Same line for the same file that we've seen already.
21258 As a last check, for pr 17276, only record the line if the line
21259 has never had a non-zero discriminator. */
21260 if (!line_has_non_zero_discriminator)
21261 return 1;
21262 return 0;
21263 }
21264
21265 /* Use the CU's builder to record line number LINE beginning at
21266 address ADDRESS in the line table of subfile SUBFILE. */
21267
21268 static void
21269 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21270 unsigned int line, CORE_ADDR address,
21271 struct dwarf2_cu *cu)
21272 {
21273 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21274
21275 if (dwarf_line_debug)
21276 {
21277 fprintf_unfiltered (gdb_stdlog,
21278 "Recording line %u, file %s, address %s\n",
21279 line, lbasename (subfile->name),
21280 paddress (gdbarch, address));
21281 }
21282
21283 if (cu != nullptr)
21284 cu->get_builder ()->record_line (subfile, line, addr);
21285 }
21286
21287 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21288 Mark the end of a set of line number records.
21289 The arguments are the same as for dwarf_record_line_1.
21290 If SUBFILE is NULL the request is ignored. */
21291
21292 static void
21293 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21294 CORE_ADDR address, struct dwarf2_cu *cu)
21295 {
21296 if (subfile == NULL)
21297 return;
21298
21299 if (dwarf_line_debug)
21300 {
21301 fprintf_unfiltered (gdb_stdlog,
21302 "Finishing current line, file %s, address %s\n",
21303 lbasename (subfile->name),
21304 paddress (gdbarch, address));
21305 }
21306
21307 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
21308 }
21309
21310 void
21311 lnp_state_machine::record_line (bool end_sequence)
21312 {
21313 if (dwarf_line_debug)
21314 {
21315 fprintf_unfiltered (gdb_stdlog,
21316 "Processing actual line %u: file %u,"
21317 " address %s, is_stmt %u, discrim %u\n",
21318 m_line, m_file,
21319 paddress (m_gdbarch, m_address),
21320 m_is_stmt, m_discriminator);
21321 }
21322
21323 file_entry *fe = current_file ();
21324
21325 if (fe == NULL)
21326 dwarf2_debug_line_missing_file_complaint ();
21327 /* For now we ignore lines not starting on an instruction boundary.
21328 But not when processing end_sequence for compatibility with the
21329 previous version of the code. */
21330 else if (m_op_index == 0 || end_sequence)
21331 {
21332 fe->included_p = 1;
21333 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
21334 {
21335 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
21336 || end_sequence)
21337 {
21338 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21339 m_currently_recording_lines ? m_cu : nullptr);
21340 }
21341
21342 if (!end_sequence)
21343 {
21344 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21345 m_line_has_non_zero_discriminator,
21346 m_last_subfile))
21347 {
21348 buildsym_compunit *builder = m_cu->get_builder ();
21349 dwarf_record_line_1 (m_gdbarch,
21350 builder->get_current_subfile (),
21351 m_line, m_address,
21352 m_currently_recording_lines ? m_cu : nullptr);
21353 }
21354 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21355 m_last_line = m_line;
21356 }
21357 }
21358 }
21359 }
21360
21361 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21362 line_header *lh, bool record_lines_p)
21363 {
21364 m_cu = cu;
21365 m_gdbarch = arch;
21366 m_record_lines_p = record_lines_p;
21367 m_line_header = lh;
21368
21369 m_currently_recording_lines = true;
21370
21371 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21372 was a line entry for it so that the backend has a chance to adjust it
21373 and also record it in case it needs it. This is currently used by MIPS
21374 code, cf. `mips_adjust_dwarf2_line'. */
21375 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21376 m_is_stmt = lh->default_is_stmt;
21377 m_discriminator = 0;
21378 }
21379
21380 void
21381 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21382 const gdb_byte *line_ptr,
21383 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21384 {
21385 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21386 the pc range of the CU. However, we restrict the test to only ADDRESS
21387 values of zero to preserve GDB's previous behaviour which is to handle
21388 the specific case of a function being GC'd by the linker. */
21389
21390 if (address == 0 && address < unrelocated_lowpc)
21391 {
21392 /* This line table is for a function which has been
21393 GCd by the linker. Ignore it. PR gdb/12528 */
21394
21395 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21396 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21397
21398 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21399 line_offset, objfile_name (objfile));
21400 m_currently_recording_lines = false;
21401 /* Note: m_currently_recording_lines is left as false until we see
21402 DW_LNE_end_sequence. */
21403 }
21404 }
21405
21406 /* Subroutine of dwarf_decode_lines to simplify it.
21407 Process the line number information in LH.
21408 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21409 program in order to set included_p for every referenced header. */
21410
21411 static void
21412 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21413 const int decode_for_pst_p, CORE_ADDR lowpc)
21414 {
21415 const gdb_byte *line_ptr, *extended_end;
21416 const gdb_byte *line_end;
21417 unsigned int bytes_read, extended_len;
21418 unsigned char op_code, extended_op;
21419 CORE_ADDR baseaddr;
21420 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21421 bfd *abfd = objfile->obfd;
21422 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21423 /* True if we're recording line info (as opposed to building partial
21424 symtabs and just interested in finding include files mentioned by
21425 the line number program). */
21426 bool record_lines_p = !decode_for_pst_p;
21427
21428 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
21429
21430 line_ptr = lh->statement_program_start;
21431 line_end = lh->statement_program_end;
21432
21433 /* Read the statement sequences until there's nothing left. */
21434 while (line_ptr < line_end)
21435 {
21436 /* The DWARF line number program state machine. Reset the state
21437 machine at the start of each sequence. */
21438 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21439 bool end_sequence = false;
21440
21441 if (record_lines_p)
21442 {
21443 /* Start a subfile for the current file of the state
21444 machine. */
21445 const file_entry *fe = state_machine.current_file ();
21446
21447 if (fe != NULL)
21448 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21449 }
21450
21451 /* Decode the table. */
21452 while (line_ptr < line_end && !end_sequence)
21453 {
21454 op_code = read_1_byte (abfd, line_ptr);
21455 line_ptr += 1;
21456
21457 if (op_code >= lh->opcode_base)
21458 {
21459 /* Special opcode. */
21460 state_machine.handle_special_opcode (op_code);
21461 }
21462 else switch (op_code)
21463 {
21464 case DW_LNS_extended_op:
21465 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21466 &bytes_read);
21467 line_ptr += bytes_read;
21468 extended_end = line_ptr + extended_len;
21469 extended_op = read_1_byte (abfd, line_ptr);
21470 line_ptr += 1;
21471 switch (extended_op)
21472 {
21473 case DW_LNE_end_sequence:
21474 state_machine.handle_end_sequence ();
21475 end_sequence = true;
21476 break;
21477 case DW_LNE_set_address:
21478 {
21479 CORE_ADDR address
21480 = read_address (abfd, line_ptr, cu, &bytes_read);
21481 line_ptr += bytes_read;
21482
21483 state_machine.check_line_address (cu, line_ptr,
21484 lowpc - baseaddr, address);
21485 state_machine.handle_set_address (baseaddr, address);
21486 }
21487 break;
21488 case DW_LNE_define_file:
21489 {
21490 const char *cur_file;
21491 unsigned int mod_time, length;
21492 dir_index dindex;
21493
21494 cur_file = read_direct_string (abfd, line_ptr,
21495 &bytes_read);
21496 line_ptr += bytes_read;
21497 dindex = (dir_index)
21498 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21499 line_ptr += bytes_read;
21500 mod_time =
21501 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21502 line_ptr += bytes_read;
21503 length =
21504 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21505 line_ptr += bytes_read;
21506 lh->add_file_name (cur_file, dindex, mod_time, length);
21507 }
21508 break;
21509 case DW_LNE_set_discriminator:
21510 {
21511 /* The discriminator is not interesting to the
21512 debugger; just ignore it. We still need to
21513 check its value though:
21514 if there are consecutive entries for the same
21515 (non-prologue) line we want to coalesce them.
21516 PR 17276. */
21517 unsigned int discr
21518 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21519 line_ptr += bytes_read;
21520
21521 state_machine.handle_set_discriminator (discr);
21522 }
21523 break;
21524 default:
21525 complaint (_("mangled .debug_line section"));
21526 return;
21527 }
21528 /* Make sure that we parsed the extended op correctly. If e.g.
21529 we expected a different address size than the producer used,
21530 we may have read the wrong number of bytes. */
21531 if (line_ptr != extended_end)
21532 {
21533 complaint (_("mangled .debug_line section"));
21534 return;
21535 }
21536 break;
21537 case DW_LNS_copy:
21538 state_machine.handle_copy ();
21539 break;
21540 case DW_LNS_advance_pc:
21541 {
21542 CORE_ADDR adjust
21543 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21544 line_ptr += bytes_read;
21545
21546 state_machine.handle_advance_pc (adjust);
21547 }
21548 break;
21549 case DW_LNS_advance_line:
21550 {
21551 int line_delta
21552 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21553 line_ptr += bytes_read;
21554
21555 state_machine.handle_advance_line (line_delta);
21556 }
21557 break;
21558 case DW_LNS_set_file:
21559 {
21560 file_name_index file
21561 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21562 &bytes_read);
21563 line_ptr += bytes_read;
21564
21565 state_machine.handle_set_file (file);
21566 }
21567 break;
21568 case DW_LNS_set_column:
21569 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21570 line_ptr += bytes_read;
21571 break;
21572 case DW_LNS_negate_stmt:
21573 state_machine.handle_negate_stmt ();
21574 break;
21575 case DW_LNS_set_basic_block:
21576 break;
21577 /* Add to the address register of the state machine the
21578 address increment value corresponding to special opcode
21579 255. I.e., this value is scaled by the minimum
21580 instruction length since special opcode 255 would have
21581 scaled the increment. */
21582 case DW_LNS_const_add_pc:
21583 state_machine.handle_const_add_pc ();
21584 break;
21585 case DW_LNS_fixed_advance_pc:
21586 {
21587 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21588 line_ptr += 2;
21589
21590 state_machine.handle_fixed_advance_pc (addr_adj);
21591 }
21592 break;
21593 default:
21594 {
21595 /* Unknown standard opcode, ignore it. */
21596 int i;
21597
21598 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21599 {
21600 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21601 line_ptr += bytes_read;
21602 }
21603 }
21604 }
21605 }
21606
21607 if (!end_sequence)
21608 dwarf2_debug_line_missing_end_sequence_complaint ();
21609
21610 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21611 in which case we still finish recording the last line). */
21612 state_machine.record_line (true);
21613 }
21614 }
21615
21616 /* Decode the Line Number Program (LNP) for the given line_header
21617 structure and CU. The actual information extracted and the type
21618 of structures created from the LNP depends on the value of PST.
21619
21620 1. If PST is NULL, then this procedure uses the data from the program
21621 to create all necessary symbol tables, and their linetables.
21622
21623 2. If PST is not NULL, this procedure reads the program to determine
21624 the list of files included by the unit represented by PST, and
21625 builds all the associated partial symbol tables.
21626
21627 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21628 It is used for relative paths in the line table.
21629 NOTE: When processing partial symtabs (pst != NULL),
21630 comp_dir == pst->dirname.
21631
21632 NOTE: It is important that psymtabs have the same file name (via strcmp)
21633 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21634 symtab we don't use it in the name of the psymtabs we create.
21635 E.g. expand_line_sal requires this when finding psymtabs to expand.
21636 A good testcase for this is mb-inline.exp.
21637
21638 LOWPC is the lowest address in CU (or 0 if not known).
21639
21640 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21641 for its PC<->lines mapping information. Otherwise only the filename
21642 table is read in. */
21643
21644 static void
21645 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21646 struct dwarf2_cu *cu, struct partial_symtab *pst,
21647 CORE_ADDR lowpc, int decode_mapping)
21648 {
21649 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21650 const int decode_for_pst_p = (pst != NULL);
21651
21652 if (decode_mapping)
21653 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21654
21655 if (decode_for_pst_p)
21656 {
21657 /* Now that we're done scanning the Line Header Program, we can
21658 create the psymtab of each included file. */
21659 for (auto &file_entry : lh->file_names ())
21660 if (file_entry.included_p == 1)
21661 {
21662 gdb::unique_xmalloc_ptr<char> name_holder;
21663 const char *include_name =
21664 psymtab_include_file_name (lh, file_entry, pst,
21665 comp_dir, &name_holder);
21666 if (include_name != NULL)
21667 dwarf2_create_include_psymtab (include_name, pst, objfile);
21668 }
21669 }
21670 else
21671 {
21672 /* Make sure a symtab is created for every file, even files
21673 which contain only variables (i.e. no code with associated
21674 line numbers). */
21675 buildsym_compunit *builder = cu->get_builder ();
21676 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21677
21678 for (auto &fe : lh->file_names ())
21679 {
21680 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21681 if (builder->get_current_subfile ()->symtab == NULL)
21682 {
21683 builder->get_current_subfile ()->symtab
21684 = allocate_symtab (cust,
21685 builder->get_current_subfile ()->name);
21686 }
21687 fe.symtab = builder->get_current_subfile ()->symtab;
21688 }
21689 }
21690 }
21691
21692 /* Start a subfile for DWARF. FILENAME is the name of the file and
21693 DIRNAME the name of the source directory which contains FILENAME
21694 or NULL if not known.
21695 This routine tries to keep line numbers from identical absolute and
21696 relative file names in a common subfile.
21697
21698 Using the `list' example from the GDB testsuite, which resides in
21699 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21700 of /srcdir/list0.c yields the following debugging information for list0.c:
21701
21702 DW_AT_name: /srcdir/list0.c
21703 DW_AT_comp_dir: /compdir
21704 files.files[0].name: list0.h
21705 files.files[0].dir: /srcdir
21706 files.files[1].name: list0.c
21707 files.files[1].dir: /srcdir
21708
21709 The line number information for list0.c has to end up in a single
21710 subfile, so that `break /srcdir/list0.c:1' works as expected.
21711 start_subfile will ensure that this happens provided that we pass the
21712 concatenation of files.files[1].dir and files.files[1].name as the
21713 subfile's name. */
21714
21715 static void
21716 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21717 const char *dirname)
21718 {
21719 gdb::unique_xmalloc_ptr<char> copy;
21720
21721 /* In order not to lose the line information directory,
21722 we concatenate it to the filename when it makes sense.
21723 Note that the Dwarf3 standard says (speaking of filenames in line
21724 information): ``The directory index is ignored for file names
21725 that represent full path names''. Thus ignoring dirname in the
21726 `else' branch below isn't an issue. */
21727
21728 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21729 {
21730 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21731 filename = copy.get ();
21732 }
21733
21734 cu->get_builder ()->start_subfile (filename);
21735 }
21736
21737 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21738 buildsym_compunit constructor. */
21739
21740 struct compunit_symtab *
21741 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21742 CORE_ADDR low_pc)
21743 {
21744 gdb_assert (m_builder == nullptr);
21745
21746 m_builder.reset (new struct buildsym_compunit
21747 (per_cu->dwarf2_per_objfile->objfile,
21748 name, comp_dir, language, low_pc));
21749
21750 list_in_scope = get_builder ()->get_file_symbols ();
21751
21752 get_builder ()->record_debugformat ("DWARF 2");
21753 get_builder ()->record_producer (producer);
21754
21755 processing_has_namespace_info = false;
21756
21757 return get_builder ()->get_compunit_symtab ();
21758 }
21759
21760 static void
21761 var_decode_location (struct attribute *attr, struct symbol *sym,
21762 struct dwarf2_cu *cu)
21763 {
21764 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21765 struct comp_unit_head *cu_header = &cu->header;
21766
21767 /* NOTE drow/2003-01-30: There used to be a comment and some special
21768 code here to turn a symbol with DW_AT_external and a
21769 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21770 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21771 with some versions of binutils) where shared libraries could have
21772 relocations against symbols in their debug information - the
21773 minimal symbol would have the right address, but the debug info
21774 would not. It's no longer necessary, because we will explicitly
21775 apply relocations when we read in the debug information now. */
21776
21777 /* A DW_AT_location attribute with no contents indicates that a
21778 variable has been optimized away. */
21779 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21780 {
21781 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21782 return;
21783 }
21784
21785 /* Handle one degenerate form of location expression specially, to
21786 preserve GDB's previous behavior when section offsets are
21787 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21788 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21789
21790 if (attr_form_is_block (attr)
21791 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21792 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21793 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21794 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
21795 && (DW_BLOCK (attr)->size
21796 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
21797 {
21798 unsigned int dummy;
21799
21800 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21801 SET_SYMBOL_VALUE_ADDRESS (sym,
21802 read_address (objfile->obfd,
21803 DW_BLOCK (attr)->data + 1,
21804 cu, &dummy));
21805 else
21806 SET_SYMBOL_VALUE_ADDRESS
21807 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21808 &dummy));
21809 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21810 fixup_symbol_section (sym, objfile);
21811 SET_SYMBOL_VALUE_ADDRESS
21812 (sym,
21813 SYMBOL_VALUE_ADDRESS (sym)
21814 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21815 return;
21816 }
21817
21818 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21819 expression evaluator, and use LOC_COMPUTED only when necessary
21820 (i.e. when the value of a register or memory location is
21821 referenced, or a thread-local block, etc.). Then again, it might
21822 not be worthwhile. I'm assuming that it isn't unless performance
21823 or memory numbers show me otherwise. */
21824
21825 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21826
21827 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21828 cu->has_loclist = true;
21829 }
21830
21831 /* Given a pointer to a DWARF information entry, figure out if we need
21832 to make a symbol table entry for it, and if so, create a new entry
21833 and return a pointer to it.
21834 If TYPE is NULL, determine symbol type from the die, otherwise
21835 used the passed type.
21836 If SPACE is not NULL, use it to hold the new symbol. If it is
21837 NULL, allocate a new symbol on the objfile's obstack. */
21838
21839 static struct symbol *
21840 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21841 struct symbol *space)
21842 {
21843 struct dwarf2_per_objfile *dwarf2_per_objfile
21844 = cu->per_cu->dwarf2_per_objfile;
21845 struct objfile *objfile = dwarf2_per_objfile->objfile;
21846 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21847 struct symbol *sym = NULL;
21848 const char *name;
21849 struct attribute *attr = NULL;
21850 struct attribute *attr2 = NULL;
21851 CORE_ADDR baseaddr;
21852 struct pending **list_to_add = NULL;
21853
21854 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21855
21856 baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
21857
21858 name = dwarf2_name (die, cu);
21859 if (name)
21860 {
21861 const char *linkagename;
21862 int suppress_add = 0;
21863
21864 if (space)
21865 sym = space;
21866 else
21867 sym = allocate_symbol (objfile);
21868 OBJSTAT (objfile, n_syms++);
21869
21870 /* Cache this symbol's name and the name's demangled form (if any). */
21871 sym->set_language (cu->language, &objfile->objfile_obstack);
21872 linkagename = dwarf2_physname (name, die, cu);
21873 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
21874
21875 /* Fortran does not have mangling standard and the mangling does differ
21876 between gfortran, iFort etc. */
21877 if (cu->language == language_fortran
21878 && symbol_get_demangled_name (sym) == NULL)
21879 symbol_set_demangled_name (sym,
21880 dwarf2_full_name (name, die, cu),
21881 NULL);
21882
21883 /* Default assumptions.
21884 Use the passed type or decode it from the die. */
21885 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21886 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21887 if (type != NULL)
21888 SYMBOL_TYPE (sym) = type;
21889 else
21890 SYMBOL_TYPE (sym) = die_type (die, cu);
21891 attr = dwarf2_attr (die,
21892 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21893 cu);
21894 if (attr != nullptr)
21895 {
21896 SYMBOL_LINE (sym) = DW_UNSND (attr);
21897 }
21898
21899 attr = dwarf2_attr (die,
21900 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21901 cu);
21902 if (attr != nullptr)
21903 {
21904 file_name_index file_index = (file_name_index) DW_UNSND (attr);
21905 struct file_entry *fe;
21906
21907 if (cu->line_header != NULL)
21908 fe = cu->line_header->file_name_at (file_index);
21909 else
21910 fe = NULL;
21911
21912 if (fe == NULL)
21913 complaint (_("file index out of range"));
21914 else
21915 symbol_set_symtab (sym, fe->symtab);
21916 }
21917
21918 switch (die->tag)
21919 {
21920 case DW_TAG_label:
21921 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21922 if (attr != nullptr)
21923 {
21924 CORE_ADDR addr;
21925
21926 addr = attr_value_as_address (attr);
21927 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21928 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21929 }
21930 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21931 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21932 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21933 add_symbol_to_list (sym, cu->list_in_scope);
21934 break;
21935 case DW_TAG_subprogram:
21936 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21937 finish_block. */
21938 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21939 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21940 if ((attr2 && (DW_UNSND (attr2) != 0))
21941 || cu->language == language_ada
21942 || cu->language == language_fortran)
21943 {
21944 /* Subprograms marked external are stored as a global symbol.
21945 Ada and Fortran subprograms, whether marked external or
21946 not, are always stored as a global symbol, because we want
21947 to be able to access them globally. For instance, we want
21948 to be able to break on a nested subprogram without having
21949 to specify the context. */
21950 list_to_add = cu->get_builder ()->get_global_symbols ();
21951 }
21952 else
21953 {
21954 list_to_add = cu->list_in_scope;
21955 }
21956 break;
21957 case DW_TAG_inlined_subroutine:
21958 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21959 finish_block. */
21960 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21961 SYMBOL_INLINED (sym) = 1;
21962 list_to_add = cu->list_in_scope;
21963 break;
21964 case DW_TAG_template_value_param:
21965 suppress_add = 1;
21966 /* Fall through. */
21967 case DW_TAG_constant:
21968 case DW_TAG_variable:
21969 case DW_TAG_member:
21970 /* Compilation with minimal debug info may result in
21971 variables with missing type entries. Change the
21972 misleading `void' type to something sensible. */
21973 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
21974 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21975
21976 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21977 /* In the case of DW_TAG_member, we should only be called for
21978 static const members. */
21979 if (die->tag == DW_TAG_member)
21980 {
21981 /* dwarf2_add_field uses die_is_declaration,
21982 so we do the same. */
21983 gdb_assert (die_is_declaration (die, cu));
21984 gdb_assert (attr);
21985 }
21986 if (attr != nullptr)
21987 {
21988 dwarf2_const_value (attr, sym, cu);
21989 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21990 if (!suppress_add)
21991 {
21992 if (attr2 && (DW_UNSND (attr2) != 0))
21993 list_to_add = cu->get_builder ()->get_global_symbols ();
21994 else
21995 list_to_add = cu->list_in_scope;
21996 }
21997 break;
21998 }
21999 attr = dwarf2_attr (die, DW_AT_location, cu);
22000 if (attr != nullptr)
22001 {
22002 var_decode_location (attr, sym, cu);
22003 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22004
22005 /* Fortran explicitly imports any global symbols to the local
22006 scope by DW_TAG_common_block. */
22007 if (cu->language == language_fortran && die->parent
22008 && die->parent->tag == DW_TAG_common_block)
22009 attr2 = NULL;
22010
22011 if (SYMBOL_CLASS (sym) == LOC_STATIC
22012 && SYMBOL_VALUE_ADDRESS (sym) == 0
22013 && !dwarf2_per_objfile->has_section_at_zero)
22014 {
22015 /* When a static variable is eliminated by the linker,
22016 the corresponding debug information is not stripped
22017 out, but the variable address is set to null;
22018 do not add such variables into symbol table. */
22019 }
22020 else if (attr2 && (DW_UNSND (attr2) != 0))
22021 {
22022 if (SYMBOL_CLASS (sym) == LOC_STATIC
22023 && (objfile->flags & OBJF_MAINLINE) == 0
22024 && dwarf2_per_objfile->can_copy)
22025 {
22026 /* A global static variable might be subject to
22027 copy relocation. We first check for a local
22028 minsym, though, because maybe the symbol was
22029 marked hidden, in which case this would not
22030 apply. */
22031 bound_minimal_symbol found
22032 = (lookup_minimal_symbol_linkage
22033 (sym->linkage_name (), objfile));
22034 if (found.minsym != nullptr)
22035 sym->maybe_copied = 1;
22036 }
22037
22038 /* A variable with DW_AT_external is never static,
22039 but it may be block-scoped. */
22040 list_to_add
22041 = ((cu->list_in_scope
22042 == cu->get_builder ()->get_file_symbols ())
22043 ? cu->get_builder ()->get_global_symbols ()
22044 : cu->list_in_scope);
22045 }
22046 else
22047 list_to_add = cu->list_in_scope;
22048 }
22049 else
22050 {
22051 /* We do not know the address of this symbol.
22052 If it is an external symbol and we have type information
22053 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22054 The address of the variable will then be determined from
22055 the minimal symbol table whenever the variable is
22056 referenced. */
22057 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22058
22059 /* Fortran explicitly imports any global symbols to the local
22060 scope by DW_TAG_common_block. */
22061 if (cu->language == language_fortran && die->parent
22062 && die->parent->tag == DW_TAG_common_block)
22063 {
22064 /* SYMBOL_CLASS doesn't matter here because
22065 read_common_block is going to reset it. */
22066 if (!suppress_add)
22067 list_to_add = cu->list_in_scope;
22068 }
22069 else if (attr2 && (DW_UNSND (attr2) != 0)
22070 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22071 {
22072 /* A variable with DW_AT_external is never static, but it
22073 may be block-scoped. */
22074 list_to_add
22075 = ((cu->list_in_scope
22076 == cu->get_builder ()->get_file_symbols ())
22077 ? cu->get_builder ()->get_global_symbols ()
22078 : cu->list_in_scope);
22079
22080 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22081 }
22082 else if (!die_is_declaration (die, cu))
22083 {
22084 /* Use the default LOC_OPTIMIZED_OUT class. */
22085 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22086 if (!suppress_add)
22087 list_to_add = cu->list_in_scope;
22088 }
22089 }
22090 break;
22091 case DW_TAG_formal_parameter:
22092 {
22093 /* If we are inside a function, mark this as an argument. If
22094 not, we might be looking at an argument to an inlined function
22095 when we do not have enough information to show inlined frames;
22096 pretend it's a local variable in that case so that the user can
22097 still see it. */
22098 struct context_stack *curr
22099 = cu->get_builder ()->get_current_context_stack ();
22100 if (curr != nullptr && curr->name != nullptr)
22101 SYMBOL_IS_ARGUMENT (sym) = 1;
22102 attr = dwarf2_attr (die, DW_AT_location, cu);
22103 if (attr != nullptr)
22104 {
22105 var_decode_location (attr, sym, cu);
22106 }
22107 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22108 if (attr != nullptr)
22109 {
22110 dwarf2_const_value (attr, sym, cu);
22111 }
22112
22113 list_to_add = cu->list_in_scope;
22114 }
22115 break;
22116 case DW_TAG_unspecified_parameters:
22117 /* From varargs functions; gdb doesn't seem to have any
22118 interest in this information, so just ignore it for now.
22119 (FIXME?) */
22120 break;
22121 case DW_TAG_template_type_param:
22122 suppress_add = 1;
22123 /* Fall through. */
22124 case DW_TAG_class_type:
22125 case DW_TAG_interface_type:
22126 case DW_TAG_structure_type:
22127 case DW_TAG_union_type:
22128 case DW_TAG_set_type:
22129 case DW_TAG_enumeration_type:
22130 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22131 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22132
22133 {
22134 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22135 really ever be static objects: otherwise, if you try
22136 to, say, break of a class's method and you're in a file
22137 which doesn't mention that class, it won't work unless
22138 the check for all static symbols in lookup_symbol_aux
22139 saves you. See the OtherFileClass tests in
22140 gdb.c++/namespace.exp. */
22141
22142 if (!suppress_add)
22143 {
22144 buildsym_compunit *builder = cu->get_builder ();
22145 list_to_add
22146 = (cu->list_in_scope == builder->get_file_symbols ()
22147 && cu->language == language_cplus
22148 ? builder->get_global_symbols ()
22149 : cu->list_in_scope);
22150
22151 /* The semantics of C++ state that "struct foo {
22152 ... }" also defines a typedef for "foo". */
22153 if (cu->language == language_cplus
22154 || cu->language == language_ada
22155 || cu->language == language_d
22156 || cu->language == language_rust)
22157 {
22158 /* The symbol's name is already allocated along
22159 with this objfile, so we don't need to
22160 duplicate it for the type. */
22161 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
22162 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
22163 }
22164 }
22165 }
22166 break;
22167 case DW_TAG_typedef:
22168 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22169 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22170 list_to_add = cu->list_in_scope;
22171 break;
22172 case DW_TAG_base_type:
22173 case DW_TAG_subrange_type:
22174 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22175 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22176 list_to_add = cu->list_in_scope;
22177 break;
22178 case DW_TAG_enumerator:
22179 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22180 if (attr != nullptr)
22181 {
22182 dwarf2_const_value (attr, sym, cu);
22183 }
22184 {
22185 /* NOTE: carlton/2003-11-10: See comment above in the
22186 DW_TAG_class_type, etc. block. */
22187
22188 list_to_add
22189 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22190 && cu->language == language_cplus
22191 ? cu->get_builder ()->get_global_symbols ()
22192 : cu->list_in_scope);
22193 }
22194 break;
22195 case DW_TAG_imported_declaration:
22196 case DW_TAG_namespace:
22197 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22198 list_to_add = cu->get_builder ()->get_global_symbols ();
22199 break;
22200 case DW_TAG_module:
22201 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22202 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22203 list_to_add = cu->get_builder ()->get_global_symbols ();
22204 break;
22205 case DW_TAG_common_block:
22206 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22207 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22208 add_symbol_to_list (sym, cu->list_in_scope);
22209 break;
22210 default:
22211 /* Not a tag we recognize. Hopefully we aren't processing
22212 trash data, but since we must specifically ignore things
22213 we don't recognize, there is nothing else we should do at
22214 this point. */
22215 complaint (_("unsupported tag: '%s'"),
22216 dwarf_tag_name (die->tag));
22217 break;
22218 }
22219
22220 if (suppress_add)
22221 {
22222 sym->hash_next = objfile->template_symbols;
22223 objfile->template_symbols = sym;
22224 list_to_add = NULL;
22225 }
22226
22227 if (list_to_add != NULL)
22228 add_symbol_to_list (sym, list_to_add);
22229
22230 /* For the benefit of old versions of GCC, check for anonymous
22231 namespaces based on the demangled name. */
22232 if (!cu->processing_has_namespace_info
22233 && cu->language == language_cplus)
22234 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22235 }
22236 return (sym);
22237 }
22238
22239 /* Given an attr with a DW_FORM_dataN value in host byte order,
22240 zero-extend it as appropriate for the symbol's type. The DWARF
22241 standard (v4) is not entirely clear about the meaning of using
22242 DW_FORM_dataN for a constant with a signed type, where the type is
22243 wider than the data. The conclusion of a discussion on the DWARF
22244 list was that this is unspecified. We choose to always zero-extend
22245 because that is the interpretation long in use by GCC. */
22246
22247 static gdb_byte *
22248 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22249 struct dwarf2_cu *cu, LONGEST *value, int bits)
22250 {
22251 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22252 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22253 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22254 LONGEST l = DW_UNSND (attr);
22255
22256 if (bits < sizeof (*value) * 8)
22257 {
22258 l &= ((LONGEST) 1 << bits) - 1;
22259 *value = l;
22260 }
22261 else if (bits == sizeof (*value) * 8)
22262 *value = l;
22263 else
22264 {
22265 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22266 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22267 return bytes;
22268 }
22269
22270 return NULL;
22271 }
22272
22273 /* Read a constant value from an attribute. Either set *VALUE, or if
22274 the value does not fit in *VALUE, set *BYTES - either already
22275 allocated on the objfile obstack, or newly allocated on OBSTACK,
22276 or, set *BATON, if we translated the constant to a location
22277 expression. */
22278
22279 static void
22280 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22281 const char *name, struct obstack *obstack,
22282 struct dwarf2_cu *cu,
22283 LONGEST *value, const gdb_byte **bytes,
22284 struct dwarf2_locexpr_baton **baton)
22285 {
22286 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22287 struct comp_unit_head *cu_header = &cu->header;
22288 struct dwarf_block *blk;
22289 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22290 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22291
22292 *value = 0;
22293 *bytes = NULL;
22294 *baton = NULL;
22295
22296 switch (attr->form)
22297 {
22298 case DW_FORM_addr:
22299 case DW_FORM_addrx:
22300 case DW_FORM_GNU_addr_index:
22301 {
22302 gdb_byte *data;
22303
22304 if (TYPE_LENGTH (type) != cu_header->addr_size)
22305 dwarf2_const_value_length_mismatch_complaint (name,
22306 cu_header->addr_size,
22307 TYPE_LENGTH (type));
22308 /* Symbols of this form are reasonably rare, so we just
22309 piggyback on the existing location code rather than writing
22310 a new implementation of symbol_computed_ops. */
22311 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22312 (*baton)->per_cu = cu->per_cu;
22313 gdb_assert ((*baton)->per_cu);
22314
22315 (*baton)->size = 2 + cu_header->addr_size;
22316 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22317 (*baton)->data = data;
22318
22319 data[0] = DW_OP_addr;
22320 store_unsigned_integer (&data[1], cu_header->addr_size,
22321 byte_order, DW_ADDR (attr));
22322 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22323 }
22324 break;
22325 case DW_FORM_string:
22326 case DW_FORM_strp:
22327 case DW_FORM_strx:
22328 case DW_FORM_GNU_str_index:
22329 case DW_FORM_GNU_strp_alt:
22330 /* DW_STRING is already allocated on the objfile obstack, point
22331 directly to it. */
22332 *bytes = (const gdb_byte *) DW_STRING (attr);
22333 break;
22334 case DW_FORM_block1:
22335 case DW_FORM_block2:
22336 case DW_FORM_block4:
22337 case DW_FORM_block:
22338 case DW_FORM_exprloc:
22339 case DW_FORM_data16:
22340 blk = DW_BLOCK (attr);
22341 if (TYPE_LENGTH (type) != blk->size)
22342 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22343 TYPE_LENGTH (type));
22344 *bytes = blk->data;
22345 break;
22346
22347 /* The DW_AT_const_value attributes are supposed to carry the
22348 symbol's value "represented as it would be on the target
22349 architecture." By the time we get here, it's already been
22350 converted to host endianness, so we just need to sign- or
22351 zero-extend it as appropriate. */
22352 case DW_FORM_data1:
22353 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22354 break;
22355 case DW_FORM_data2:
22356 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22357 break;
22358 case DW_FORM_data4:
22359 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22360 break;
22361 case DW_FORM_data8:
22362 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22363 break;
22364
22365 case DW_FORM_sdata:
22366 case DW_FORM_implicit_const:
22367 *value = DW_SND (attr);
22368 break;
22369
22370 case DW_FORM_udata:
22371 *value = DW_UNSND (attr);
22372 break;
22373
22374 default:
22375 complaint (_("unsupported const value attribute form: '%s'"),
22376 dwarf_form_name (attr->form));
22377 *value = 0;
22378 break;
22379 }
22380 }
22381
22382
22383 /* Copy constant value from an attribute to a symbol. */
22384
22385 static void
22386 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22387 struct dwarf2_cu *cu)
22388 {
22389 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22390 LONGEST value;
22391 const gdb_byte *bytes;
22392 struct dwarf2_locexpr_baton *baton;
22393
22394 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22395 sym->print_name (),
22396 &objfile->objfile_obstack, cu,
22397 &value, &bytes, &baton);
22398
22399 if (baton != NULL)
22400 {
22401 SYMBOL_LOCATION_BATON (sym) = baton;
22402 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22403 }
22404 else if (bytes != NULL)
22405 {
22406 SYMBOL_VALUE_BYTES (sym) = bytes;
22407 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22408 }
22409 else
22410 {
22411 SYMBOL_VALUE (sym) = value;
22412 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22413 }
22414 }
22415
22416 /* Return the type of the die in question using its DW_AT_type attribute. */
22417
22418 static struct type *
22419 die_type (struct die_info *die, struct dwarf2_cu *cu)
22420 {
22421 struct attribute *type_attr;
22422
22423 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22424 if (!type_attr)
22425 {
22426 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22427 /* A missing DW_AT_type represents a void type. */
22428 return objfile_type (objfile)->builtin_void;
22429 }
22430
22431 return lookup_die_type (die, type_attr, cu);
22432 }
22433
22434 /* True iff CU's producer generates GNAT Ada auxiliary information
22435 that allows to find parallel types through that information instead
22436 of having to do expensive parallel lookups by type name. */
22437
22438 static int
22439 need_gnat_info (struct dwarf2_cu *cu)
22440 {
22441 /* Assume that the Ada compiler was GNAT, which always produces
22442 the auxiliary information. */
22443 return (cu->language == language_ada);
22444 }
22445
22446 /* Return the auxiliary type of the die in question using its
22447 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22448 attribute is not present. */
22449
22450 static struct type *
22451 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22452 {
22453 struct attribute *type_attr;
22454
22455 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22456 if (!type_attr)
22457 return NULL;
22458
22459 return lookup_die_type (die, type_attr, cu);
22460 }
22461
22462 /* If DIE has a descriptive_type attribute, then set the TYPE's
22463 descriptive type accordingly. */
22464
22465 static void
22466 set_descriptive_type (struct type *type, struct die_info *die,
22467 struct dwarf2_cu *cu)
22468 {
22469 struct type *descriptive_type = die_descriptive_type (die, cu);
22470
22471 if (descriptive_type)
22472 {
22473 ALLOCATE_GNAT_AUX_TYPE (type);
22474 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22475 }
22476 }
22477
22478 /* Return the containing type of the die in question using its
22479 DW_AT_containing_type attribute. */
22480
22481 static struct type *
22482 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22483 {
22484 struct attribute *type_attr;
22485 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22486
22487 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22488 if (!type_attr)
22489 error (_("Dwarf Error: Problem turning containing type into gdb type "
22490 "[in module %s]"), objfile_name (objfile));
22491
22492 return lookup_die_type (die, type_attr, cu);
22493 }
22494
22495 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22496
22497 static struct type *
22498 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22499 {
22500 struct dwarf2_per_objfile *dwarf2_per_objfile
22501 = cu->per_cu->dwarf2_per_objfile;
22502 struct objfile *objfile = dwarf2_per_objfile->objfile;
22503 char *saved;
22504
22505 std::string message
22506 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22507 objfile_name (objfile),
22508 sect_offset_str (cu->header.sect_off),
22509 sect_offset_str (die->sect_off));
22510 saved = obstack_strdup (&objfile->objfile_obstack, message);
22511
22512 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22513 }
22514
22515 /* Look up the type of DIE in CU using its type attribute ATTR.
22516 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22517 DW_AT_containing_type.
22518 If there is no type substitute an error marker. */
22519
22520 static struct type *
22521 lookup_die_type (struct die_info *die, const struct attribute *attr,
22522 struct dwarf2_cu *cu)
22523 {
22524 struct dwarf2_per_objfile *dwarf2_per_objfile
22525 = cu->per_cu->dwarf2_per_objfile;
22526 struct objfile *objfile = dwarf2_per_objfile->objfile;
22527 struct type *this_type;
22528
22529 gdb_assert (attr->name == DW_AT_type
22530 || attr->name == DW_AT_GNAT_descriptive_type
22531 || attr->name == DW_AT_containing_type);
22532
22533 /* First see if we have it cached. */
22534
22535 if (attr->form == DW_FORM_GNU_ref_alt)
22536 {
22537 struct dwarf2_per_cu_data *per_cu;
22538 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22539
22540 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22541 dwarf2_per_objfile);
22542 this_type = get_die_type_at_offset (sect_off, per_cu);
22543 }
22544 else if (attr_form_is_ref (attr))
22545 {
22546 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
22547
22548 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
22549 }
22550 else if (attr->form == DW_FORM_ref_sig8)
22551 {
22552 ULONGEST signature = DW_SIGNATURE (attr);
22553
22554 return get_signatured_type (die, signature, cu);
22555 }
22556 else
22557 {
22558 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22559 " at %s [in module %s]"),
22560 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22561 objfile_name (objfile));
22562 return build_error_marker_type (cu, die);
22563 }
22564
22565 /* If not cached we need to read it in. */
22566
22567 if (this_type == NULL)
22568 {
22569 struct die_info *type_die = NULL;
22570 struct dwarf2_cu *type_cu = cu;
22571
22572 if (attr_form_is_ref (attr))
22573 type_die = follow_die_ref (die, attr, &type_cu);
22574 if (type_die == NULL)
22575 return build_error_marker_type (cu, die);
22576 /* If we find the type now, it's probably because the type came
22577 from an inter-CU reference and the type's CU got expanded before
22578 ours. */
22579 this_type = read_type_die (type_die, type_cu);
22580 }
22581
22582 /* If we still don't have a type use an error marker. */
22583
22584 if (this_type == NULL)
22585 return build_error_marker_type (cu, die);
22586
22587 return this_type;
22588 }
22589
22590 /* Return the type in DIE, CU.
22591 Returns NULL for invalid types.
22592
22593 This first does a lookup in die_type_hash,
22594 and only reads the die in if necessary.
22595
22596 NOTE: This can be called when reading in partial or full symbols. */
22597
22598 static struct type *
22599 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22600 {
22601 struct type *this_type;
22602
22603 this_type = get_die_type (die, cu);
22604 if (this_type)
22605 return this_type;
22606
22607 return read_type_die_1 (die, cu);
22608 }
22609
22610 /* Read the type in DIE, CU.
22611 Returns NULL for invalid types. */
22612
22613 static struct type *
22614 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22615 {
22616 struct type *this_type = NULL;
22617
22618 switch (die->tag)
22619 {
22620 case DW_TAG_class_type:
22621 case DW_TAG_interface_type:
22622 case DW_TAG_structure_type:
22623 case DW_TAG_union_type:
22624 this_type = read_structure_type (die, cu);
22625 break;
22626 case DW_TAG_enumeration_type:
22627 this_type = read_enumeration_type (die, cu);
22628 break;
22629 case DW_TAG_subprogram:
22630 case DW_TAG_subroutine_type:
22631 case DW_TAG_inlined_subroutine:
22632 this_type = read_subroutine_type (die, cu);
22633 break;
22634 case DW_TAG_array_type:
22635 this_type = read_array_type (die, cu);
22636 break;
22637 case DW_TAG_set_type:
22638 this_type = read_set_type (die, cu);
22639 break;
22640 case DW_TAG_pointer_type:
22641 this_type = read_tag_pointer_type (die, cu);
22642 break;
22643 case DW_TAG_ptr_to_member_type:
22644 this_type = read_tag_ptr_to_member_type (die, cu);
22645 break;
22646 case DW_TAG_reference_type:
22647 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22648 break;
22649 case DW_TAG_rvalue_reference_type:
22650 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22651 break;
22652 case DW_TAG_const_type:
22653 this_type = read_tag_const_type (die, cu);
22654 break;
22655 case DW_TAG_volatile_type:
22656 this_type = read_tag_volatile_type (die, cu);
22657 break;
22658 case DW_TAG_restrict_type:
22659 this_type = read_tag_restrict_type (die, cu);
22660 break;
22661 case DW_TAG_string_type:
22662 this_type = read_tag_string_type (die, cu);
22663 break;
22664 case DW_TAG_typedef:
22665 this_type = read_typedef (die, cu);
22666 break;
22667 case DW_TAG_subrange_type:
22668 this_type = read_subrange_type (die, cu);
22669 break;
22670 case DW_TAG_base_type:
22671 this_type = read_base_type (die, cu);
22672 break;
22673 case DW_TAG_unspecified_type:
22674 this_type = read_unspecified_type (die, cu);
22675 break;
22676 case DW_TAG_namespace:
22677 this_type = read_namespace_type (die, cu);
22678 break;
22679 case DW_TAG_module:
22680 this_type = read_module_type (die, cu);
22681 break;
22682 case DW_TAG_atomic_type:
22683 this_type = read_tag_atomic_type (die, cu);
22684 break;
22685 default:
22686 complaint (_("unexpected tag in read_type_die: '%s'"),
22687 dwarf_tag_name (die->tag));
22688 break;
22689 }
22690
22691 return this_type;
22692 }
22693
22694 /* See if we can figure out if the class lives in a namespace. We do
22695 this by looking for a member function; its demangled name will
22696 contain namespace info, if there is any.
22697 Return the computed name or NULL.
22698 Space for the result is allocated on the objfile's obstack.
22699 This is the full-die version of guess_partial_die_structure_name.
22700 In this case we know DIE has no useful parent. */
22701
22702 static const char *
22703 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22704 {
22705 struct die_info *spec_die;
22706 struct dwarf2_cu *spec_cu;
22707 struct die_info *child;
22708 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22709
22710 spec_cu = cu;
22711 spec_die = die_specification (die, &spec_cu);
22712 if (spec_die != NULL)
22713 {
22714 die = spec_die;
22715 cu = spec_cu;
22716 }
22717
22718 for (child = die->child;
22719 child != NULL;
22720 child = child->sibling)
22721 {
22722 if (child->tag == DW_TAG_subprogram)
22723 {
22724 const char *linkage_name = dw2_linkage_name (child, cu);
22725
22726 if (linkage_name != NULL)
22727 {
22728 gdb::unique_xmalloc_ptr<char> actual_name
22729 (language_class_name_from_physname (cu->language_defn,
22730 linkage_name));
22731 const char *name = NULL;
22732
22733 if (actual_name != NULL)
22734 {
22735 const char *die_name = dwarf2_name (die, cu);
22736
22737 if (die_name != NULL
22738 && strcmp (die_name, actual_name.get ()) != 0)
22739 {
22740 /* Strip off the class name from the full name.
22741 We want the prefix. */
22742 int die_name_len = strlen (die_name);
22743 int actual_name_len = strlen (actual_name.get ());
22744 const char *ptr = actual_name.get ();
22745
22746 /* Test for '::' as a sanity check. */
22747 if (actual_name_len > die_name_len + 2
22748 && ptr[actual_name_len - die_name_len - 1] == ':')
22749 name = obstack_strndup (
22750 &objfile->per_bfd->storage_obstack,
22751 ptr, actual_name_len - die_name_len - 2);
22752 }
22753 }
22754 return name;
22755 }
22756 }
22757 }
22758
22759 return NULL;
22760 }
22761
22762 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22763 prefix part in such case. See
22764 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22765
22766 static const char *
22767 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22768 {
22769 struct attribute *attr;
22770 const char *base;
22771
22772 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22773 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22774 return NULL;
22775
22776 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22777 return NULL;
22778
22779 attr = dw2_linkage_name_attr (die, cu);
22780 if (attr == NULL || DW_STRING (attr) == NULL)
22781 return NULL;
22782
22783 /* dwarf2_name had to be already called. */
22784 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22785
22786 /* Strip the base name, keep any leading namespaces/classes. */
22787 base = strrchr (DW_STRING (attr), ':');
22788 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22789 return "";
22790
22791 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22792 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22793 DW_STRING (attr),
22794 &base[-1] - DW_STRING (attr));
22795 }
22796
22797 /* Return the name of the namespace/class that DIE is defined within,
22798 or "" if we can't tell. The caller should not xfree the result.
22799
22800 For example, if we're within the method foo() in the following
22801 code:
22802
22803 namespace N {
22804 class C {
22805 void foo () {
22806 }
22807 };
22808 }
22809
22810 then determine_prefix on foo's die will return "N::C". */
22811
22812 static const char *
22813 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22814 {
22815 struct dwarf2_per_objfile *dwarf2_per_objfile
22816 = cu->per_cu->dwarf2_per_objfile;
22817 struct die_info *parent, *spec_die;
22818 struct dwarf2_cu *spec_cu;
22819 struct type *parent_type;
22820 const char *retval;
22821
22822 if (cu->language != language_cplus
22823 && cu->language != language_fortran && cu->language != language_d
22824 && cu->language != language_rust)
22825 return "";
22826
22827 retval = anonymous_struct_prefix (die, cu);
22828 if (retval)
22829 return retval;
22830
22831 /* We have to be careful in the presence of DW_AT_specification.
22832 For example, with GCC 3.4, given the code
22833
22834 namespace N {
22835 void foo() {
22836 // Definition of N::foo.
22837 }
22838 }
22839
22840 then we'll have a tree of DIEs like this:
22841
22842 1: DW_TAG_compile_unit
22843 2: DW_TAG_namespace // N
22844 3: DW_TAG_subprogram // declaration of N::foo
22845 4: DW_TAG_subprogram // definition of N::foo
22846 DW_AT_specification // refers to die #3
22847
22848 Thus, when processing die #4, we have to pretend that we're in
22849 the context of its DW_AT_specification, namely the contex of die
22850 #3. */
22851 spec_cu = cu;
22852 spec_die = die_specification (die, &spec_cu);
22853 if (spec_die == NULL)
22854 parent = die->parent;
22855 else
22856 {
22857 parent = spec_die->parent;
22858 cu = spec_cu;
22859 }
22860
22861 if (parent == NULL)
22862 return "";
22863 else if (parent->building_fullname)
22864 {
22865 const char *name;
22866 const char *parent_name;
22867
22868 /* It has been seen on RealView 2.2 built binaries,
22869 DW_TAG_template_type_param types actually _defined_ as
22870 children of the parent class:
22871
22872 enum E {};
22873 template class <class Enum> Class{};
22874 Class<enum E> class_e;
22875
22876 1: DW_TAG_class_type (Class)
22877 2: DW_TAG_enumeration_type (E)
22878 3: DW_TAG_enumerator (enum1:0)
22879 3: DW_TAG_enumerator (enum2:1)
22880 ...
22881 2: DW_TAG_template_type_param
22882 DW_AT_type DW_FORM_ref_udata (E)
22883
22884 Besides being broken debug info, it can put GDB into an
22885 infinite loop. Consider:
22886
22887 When we're building the full name for Class<E>, we'll start
22888 at Class, and go look over its template type parameters,
22889 finding E. We'll then try to build the full name of E, and
22890 reach here. We're now trying to build the full name of E,
22891 and look over the parent DIE for containing scope. In the
22892 broken case, if we followed the parent DIE of E, we'd again
22893 find Class, and once again go look at its template type
22894 arguments, etc., etc. Simply don't consider such parent die
22895 as source-level parent of this die (it can't be, the language
22896 doesn't allow it), and break the loop here. */
22897 name = dwarf2_name (die, cu);
22898 parent_name = dwarf2_name (parent, cu);
22899 complaint (_("template param type '%s' defined within parent '%s'"),
22900 name ? name : "<unknown>",
22901 parent_name ? parent_name : "<unknown>");
22902 return "";
22903 }
22904 else
22905 switch (parent->tag)
22906 {
22907 case DW_TAG_namespace:
22908 parent_type = read_type_die (parent, cu);
22909 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22910 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22911 Work around this problem here. */
22912 if (cu->language == language_cplus
22913 && strcmp (TYPE_NAME (parent_type), "::") == 0)
22914 return "";
22915 /* We give a name to even anonymous namespaces. */
22916 return TYPE_NAME (parent_type);
22917 case DW_TAG_class_type:
22918 case DW_TAG_interface_type:
22919 case DW_TAG_structure_type:
22920 case DW_TAG_union_type:
22921 case DW_TAG_module:
22922 parent_type = read_type_die (parent, cu);
22923 if (TYPE_NAME (parent_type) != NULL)
22924 return TYPE_NAME (parent_type);
22925 else
22926 /* An anonymous structure is only allowed non-static data
22927 members; no typedefs, no member functions, et cetera.
22928 So it does not need a prefix. */
22929 return "";
22930 case DW_TAG_compile_unit:
22931 case DW_TAG_partial_unit:
22932 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22933 if (cu->language == language_cplus
22934 && !dwarf2_per_objfile->types.empty ()
22935 && die->child != NULL
22936 && (die->tag == DW_TAG_class_type
22937 || die->tag == DW_TAG_structure_type
22938 || die->tag == DW_TAG_union_type))
22939 {
22940 const char *name = guess_full_die_structure_name (die, cu);
22941 if (name != NULL)
22942 return name;
22943 }
22944 return "";
22945 case DW_TAG_subprogram:
22946 /* Nested subroutines in Fortran get a prefix with the name
22947 of the parent's subroutine. */
22948 if (cu->language == language_fortran)
22949 {
22950 if ((die->tag == DW_TAG_subprogram)
22951 && (dwarf2_name (parent, cu) != NULL))
22952 return dwarf2_name (parent, cu);
22953 }
22954 return determine_prefix (parent, cu);
22955 case DW_TAG_enumeration_type:
22956 parent_type = read_type_die (parent, cu);
22957 if (TYPE_DECLARED_CLASS (parent_type))
22958 {
22959 if (TYPE_NAME (parent_type) != NULL)
22960 return TYPE_NAME (parent_type);
22961 return "";
22962 }
22963 /* Fall through. */
22964 default:
22965 return determine_prefix (parent, cu);
22966 }
22967 }
22968
22969 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22970 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22971 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22972 an obconcat, otherwise allocate storage for the result. The CU argument is
22973 used to determine the language and hence, the appropriate separator. */
22974
22975 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22976
22977 static char *
22978 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22979 int physname, struct dwarf2_cu *cu)
22980 {
22981 const char *lead = "";
22982 const char *sep;
22983
22984 if (suffix == NULL || suffix[0] == '\0'
22985 || prefix == NULL || prefix[0] == '\0')
22986 sep = "";
22987 else if (cu->language == language_d)
22988 {
22989 /* For D, the 'main' function could be defined in any module, but it
22990 should never be prefixed. */
22991 if (strcmp (suffix, "D main") == 0)
22992 {
22993 prefix = "";
22994 sep = "";
22995 }
22996 else
22997 sep = ".";
22998 }
22999 else if (cu->language == language_fortran && physname)
23000 {
23001 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23002 DW_AT_MIPS_linkage_name is preferred and used instead. */
23003
23004 lead = "__";
23005 sep = "_MOD_";
23006 }
23007 else
23008 sep = "::";
23009
23010 if (prefix == NULL)
23011 prefix = "";
23012 if (suffix == NULL)
23013 suffix = "";
23014
23015 if (obs == NULL)
23016 {
23017 char *retval
23018 = ((char *)
23019 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23020
23021 strcpy (retval, lead);
23022 strcat (retval, prefix);
23023 strcat (retval, sep);
23024 strcat (retval, suffix);
23025 return retval;
23026 }
23027 else
23028 {
23029 /* We have an obstack. */
23030 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23031 }
23032 }
23033
23034 /* Return sibling of die, NULL if no sibling. */
23035
23036 static struct die_info *
23037 sibling_die (struct die_info *die)
23038 {
23039 return die->sibling;
23040 }
23041
23042 /* Get name of a die, return NULL if not found. */
23043
23044 static const char *
23045 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23046 struct obstack *obstack)
23047 {
23048 if (name && cu->language == language_cplus)
23049 {
23050 std::string canon_name = cp_canonicalize_string (name);
23051
23052 if (!canon_name.empty ())
23053 {
23054 if (canon_name != name)
23055 name = obstack_strdup (obstack, canon_name);
23056 }
23057 }
23058
23059 return name;
23060 }
23061
23062 /* Get name of a die, return NULL if not found.
23063 Anonymous namespaces are converted to their magic string. */
23064
23065 static const char *
23066 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23067 {
23068 struct attribute *attr;
23069 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
23070
23071 attr = dwarf2_attr (die, DW_AT_name, cu);
23072 if ((!attr || !DW_STRING (attr))
23073 && die->tag != DW_TAG_namespace
23074 && die->tag != DW_TAG_class_type
23075 && die->tag != DW_TAG_interface_type
23076 && die->tag != DW_TAG_structure_type
23077 && die->tag != DW_TAG_union_type)
23078 return NULL;
23079
23080 switch (die->tag)
23081 {
23082 case DW_TAG_compile_unit:
23083 case DW_TAG_partial_unit:
23084 /* Compilation units have a DW_AT_name that is a filename, not
23085 a source language identifier. */
23086 case DW_TAG_enumeration_type:
23087 case DW_TAG_enumerator:
23088 /* These tags always have simple identifiers already; no need
23089 to canonicalize them. */
23090 return DW_STRING (attr);
23091
23092 case DW_TAG_namespace:
23093 if (attr != NULL && DW_STRING (attr) != NULL)
23094 return DW_STRING (attr);
23095 return CP_ANONYMOUS_NAMESPACE_STR;
23096
23097 case DW_TAG_class_type:
23098 case DW_TAG_interface_type:
23099 case DW_TAG_structure_type:
23100 case DW_TAG_union_type:
23101 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23102 structures or unions. These were of the form "._%d" in GCC 4.1,
23103 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23104 and GCC 4.4. We work around this problem by ignoring these. */
23105 if (attr && DW_STRING (attr)
23106 && (startswith (DW_STRING (attr), "._")
23107 || startswith (DW_STRING (attr), "<anonymous")))
23108 return NULL;
23109
23110 /* GCC might emit a nameless typedef that has a linkage name. See
23111 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23112 if (!attr || DW_STRING (attr) == NULL)
23113 {
23114 attr = dw2_linkage_name_attr (die, cu);
23115 if (attr == NULL || DW_STRING (attr) == NULL)
23116 return NULL;
23117
23118 /* Avoid demangling DW_STRING (attr) the second time on a second
23119 call for the same DIE. */
23120 if (!DW_STRING_IS_CANONICAL (attr))
23121 {
23122 gdb::unique_xmalloc_ptr<char> demangled
23123 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
23124
23125 const char *base;
23126
23127 /* FIXME: we already did this for the partial symbol... */
23128 DW_STRING (attr)
23129 = obstack_strdup (&objfile->per_bfd->storage_obstack,
23130 demangled.get ());
23131 DW_STRING_IS_CANONICAL (attr) = 1;
23132
23133 /* Strip any leading namespaces/classes, keep only the base name.
23134 DW_AT_name for named DIEs does not contain the prefixes. */
23135 base = strrchr (DW_STRING (attr), ':');
23136 if (base && base > DW_STRING (attr) && base[-1] == ':')
23137 return &base[1];
23138 else
23139 return DW_STRING (attr);
23140 }
23141 }
23142 break;
23143
23144 default:
23145 break;
23146 }
23147
23148 if (!DW_STRING_IS_CANONICAL (attr))
23149 {
23150 DW_STRING (attr)
23151 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
23152 &objfile->per_bfd->storage_obstack);
23153 DW_STRING_IS_CANONICAL (attr) = 1;
23154 }
23155 return DW_STRING (attr);
23156 }
23157
23158 /* Return the die that this die in an extension of, or NULL if there
23159 is none. *EXT_CU is the CU containing DIE on input, and the CU
23160 containing the return value on output. */
23161
23162 static struct die_info *
23163 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23164 {
23165 struct attribute *attr;
23166
23167 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23168 if (attr == NULL)
23169 return NULL;
23170
23171 return follow_die_ref (die, attr, ext_cu);
23172 }
23173
23174 /* A convenience function that returns an "unknown" DWARF name,
23175 including the value of V. STR is the name of the entity being
23176 printed, e.g., "TAG". */
23177
23178 static const char *
23179 dwarf_unknown (const char *str, unsigned v)
23180 {
23181 char *cell = get_print_cell ();
23182 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
23183 return cell;
23184 }
23185
23186 /* Convert a DIE tag into its string name. */
23187
23188 static const char *
23189 dwarf_tag_name (unsigned tag)
23190 {
23191 const char *name = get_DW_TAG_name (tag);
23192
23193 if (name == NULL)
23194 return dwarf_unknown ("TAG", tag);
23195
23196 return name;
23197 }
23198
23199 /* Convert a DWARF attribute code into its string name. */
23200
23201 static const char *
23202 dwarf_attr_name (unsigned attr)
23203 {
23204 const char *name;
23205
23206 #ifdef MIPS /* collides with DW_AT_HP_block_index */
23207 if (attr == DW_AT_MIPS_fde)
23208 return "DW_AT_MIPS_fde";
23209 #else
23210 if (attr == DW_AT_HP_block_index)
23211 return "DW_AT_HP_block_index";
23212 #endif
23213
23214 name = get_DW_AT_name (attr);
23215
23216 if (name == NULL)
23217 return dwarf_unknown ("AT", attr);
23218
23219 return name;
23220 }
23221
23222 /* Convert a unit type to corresponding DW_UT name. */
23223
23224 static const char *
23225 dwarf_unit_type_name (int unit_type) {
23226 switch (unit_type)
23227 {
23228 case 0x01:
23229 return "DW_UT_compile (0x01)";
23230 case 0x02:
23231 return "DW_UT_type (0x02)";
23232 case 0x03:
23233 return "DW_UT_partial (0x03)";
23234 case 0x04:
23235 return "DW_UT_skeleton (0x04)";
23236 case 0x05:
23237 return "DW_UT_split_compile (0x05)";
23238 case 0x06:
23239 return "DW_UT_split_type (0x06)";
23240 case 0x80:
23241 return "DW_UT_lo_user (0x80)";
23242 case 0xff:
23243 return "DW_UT_hi_user (0xff)";
23244 default:
23245 return nullptr;
23246 }
23247 }
23248
23249 /* Convert a DWARF value form code into its string name. */
23250
23251 static const char *
23252 dwarf_form_name (unsigned form)
23253 {
23254 const char *name = get_DW_FORM_name (form);
23255
23256 if (name == NULL)
23257 return dwarf_unknown ("FORM", form);
23258
23259 return name;
23260 }
23261
23262 static const char *
23263 dwarf_bool_name (unsigned mybool)
23264 {
23265 if (mybool)
23266 return "TRUE";
23267 else
23268 return "FALSE";
23269 }
23270
23271 /* Convert a DWARF type code into its string name. */
23272
23273 static const char *
23274 dwarf_type_encoding_name (unsigned enc)
23275 {
23276 const char *name = get_DW_ATE_name (enc);
23277
23278 if (name == NULL)
23279 return dwarf_unknown ("ATE", enc);
23280
23281 return name;
23282 }
23283
23284 static void
23285 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23286 {
23287 unsigned int i;
23288
23289 print_spaces (indent, f);
23290 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23291 dwarf_tag_name (die->tag), die->abbrev,
23292 sect_offset_str (die->sect_off));
23293
23294 if (die->parent != NULL)
23295 {
23296 print_spaces (indent, f);
23297 fprintf_unfiltered (f, " parent at offset: %s\n",
23298 sect_offset_str (die->parent->sect_off));
23299 }
23300
23301 print_spaces (indent, f);
23302 fprintf_unfiltered (f, " has children: %s\n",
23303 dwarf_bool_name (die->child != NULL));
23304
23305 print_spaces (indent, f);
23306 fprintf_unfiltered (f, " attributes:\n");
23307
23308 for (i = 0; i < die->num_attrs; ++i)
23309 {
23310 print_spaces (indent, f);
23311 fprintf_unfiltered (f, " %s (%s) ",
23312 dwarf_attr_name (die->attrs[i].name),
23313 dwarf_form_name (die->attrs[i].form));
23314
23315 switch (die->attrs[i].form)
23316 {
23317 case DW_FORM_addr:
23318 case DW_FORM_addrx:
23319 case DW_FORM_GNU_addr_index:
23320 fprintf_unfiltered (f, "address: ");
23321 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
23322 break;
23323 case DW_FORM_block2:
23324 case DW_FORM_block4:
23325 case DW_FORM_block:
23326 case DW_FORM_block1:
23327 fprintf_unfiltered (f, "block: size %s",
23328 pulongest (DW_BLOCK (&die->attrs[i])->size));
23329 break;
23330 case DW_FORM_exprloc:
23331 fprintf_unfiltered (f, "expression: size %s",
23332 pulongest (DW_BLOCK (&die->attrs[i])->size));
23333 break;
23334 case DW_FORM_data16:
23335 fprintf_unfiltered (f, "constant of 16 bytes");
23336 break;
23337 case DW_FORM_ref_addr:
23338 fprintf_unfiltered (f, "ref address: ");
23339 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23340 break;
23341 case DW_FORM_GNU_ref_alt:
23342 fprintf_unfiltered (f, "alt ref address: ");
23343 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23344 break;
23345 case DW_FORM_ref1:
23346 case DW_FORM_ref2:
23347 case DW_FORM_ref4:
23348 case DW_FORM_ref8:
23349 case DW_FORM_ref_udata:
23350 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23351 (long) (DW_UNSND (&die->attrs[i])));
23352 break;
23353 case DW_FORM_data1:
23354 case DW_FORM_data2:
23355 case DW_FORM_data4:
23356 case DW_FORM_data8:
23357 case DW_FORM_udata:
23358 case DW_FORM_sdata:
23359 fprintf_unfiltered (f, "constant: %s",
23360 pulongest (DW_UNSND (&die->attrs[i])));
23361 break;
23362 case DW_FORM_sec_offset:
23363 fprintf_unfiltered (f, "section offset: %s",
23364 pulongest (DW_UNSND (&die->attrs[i])));
23365 break;
23366 case DW_FORM_ref_sig8:
23367 fprintf_unfiltered (f, "signature: %s",
23368 hex_string (DW_SIGNATURE (&die->attrs[i])));
23369 break;
23370 case DW_FORM_string:
23371 case DW_FORM_strp:
23372 case DW_FORM_line_strp:
23373 case DW_FORM_strx:
23374 case DW_FORM_GNU_str_index:
23375 case DW_FORM_GNU_strp_alt:
23376 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23377 DW_STRING (&die->attrs[i])
23378 ? DW_STRING (&die->attrs[i]) : "",
23379 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
23380 break;
23381 case DW_FORM_flag:
23382 if (DW_UNSND (&die->attrs[i]))
23383 fprintf_unfiltered (f, "flag: TRUE");
23384 else
23385 fprintf_unfiltered (f, "flag: FALSE");
23386 break;
23387 case DW_FORM_flag_present:
23388 fprintf_unfiltered (f, "flag: TRUE");
23389 break;
23390 case DW_FORM_indirect:
23391 /* The reader will have reduced the indirect form to
23392 the "base form" so this form should not occur. */
23393 fprintf_unfiltered (f,
23394 "unexpected attribute form: DW_FORM_indirect");
23395 break;
23396 case DW_FORM_implicit_const:
23397 fprintf_unfiltered (f, "constant: %s",
23398 plongest (DW_SND (&die->attrs[i])));
23399 break;
23400 default:
23401 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23402 die->attrs[i].form);
23403 break;
23404 }
23405 fprintf_unfiltered (f, "\n");
23406 }
23407 }
23408
23409 static void
23410 dump_die_for_error (struct die_info *die)
23411 {
23412 dump_die_shallow (gdb_stderr, 0, die);
23413 }
23414
23415 static void
23416 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23417 {
23418 int indent = level * 4;
23419
23420 gdb_assert (die != NULL);
23421
23422 if (level >= max_level)
23423 return;
23424
23425 dump_die_shallow (f, indent, die);
23426
23427 if (die->child != NULL)
23428 {
23429 print_spaces (indent, f);
23430 fprintf_unfiltered (f, " Children:");
23431 if (level + 1 < max_level)
23432 {
23433 fprintf_unfiltered (f, "\n");
23434 dump_die_1 (f, level + 1, max_level, die->child);
23435 }
23436 else
23437 {
23438 fprintf_unfiltered (f,
23439 " [not printed, max nesting level reached]\n");
23440 }
23441 }
23442
23443 if (die->sibling != NULL && level > 0)
23444 {
23445 dump_die_1 (f, level, max_level, die->sibling);
23446 }
23447 }
23448
23449 /* This is called from the pdie macro in gdbinit.in.
23450 It's not static so gcc will keep a copy callable from gdb. */
23451
23452 void
23453 dump_die (struct die_info *die, int max_level)
23454 {
23455 dump_die_1 (gdb_stdlog, 0, max_level, die);
23456 }
23457
23458 static void
23459 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23460 {
23461 void **slot;
23462
23463 slot = htab_find_slot_with_hash (cu->die_hash, die,
23464 to_underlying (die->sect_off),
23465 INSERT);
23466
23467 *slot = die;
23468 }
23469
23470 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23471 required kind. */
23472
23473 static sect_offset
23474 dwarf2_get_ref_die_offset (const struct attribute *attr)
23475 {
23476 if (attr_form_is_ref (attr))
23477 return (sect_offset) DW_UNSND (attr);
23478
23479 complaint (_("unsupported die ref attribute form: '%s'"),
23480 dwarf_form_name (attr->form));
23481 return {};
23482 }
23483
23484 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23485 * the value held by the attribute is not constant. */
23486
23487 static LONGEST
23488 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
23489 {
23490 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
23491 return DW_SND (attr);
23492 else if (attr->form == DW_FORM_udata
23493 || attr->form == DW_FORM_data1
23494 || attr->form == DW_FORM_data2
23495 || attr->form == DW_FORM_data4
23496 || attr->form == DW_FORM_data8)
23497 return DW_UNSND (attr);
23498 else
23499 {
23500 /* For DW_FORM_data16 see attr_form_is_constant. */
23501 complaint (_("Attribute value is not a constant (%s)"),
23502 dwarf_form_name (attr->form));
23503 return default_value;
23504 }
23505 }
23506
23507 /* Follow reference or signature attribute ATTR of SRC_DIE.
23508 On entry *REF_CU is the CU of SRC_DIE.
23509 On exit *REF_CU is the CU of the result. */
23510
23511 static struct die_info *
23512 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23513 struct dwarf2_cu **ref_cu)
23514 {
23515 struct die_info *die;
23516
23517 if (attr_form_is_ref (attr))
23518 die = follow_die_ref (src_die, attr, ref_cu);
23519 else if (attr->form == DW_FORM_ref_sig8)
23520 die = follow_die_sig (src_die, attr, ref_cu);
23521 else
23522 {
23523 dump_die_for_error (src_die);
23524 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23525 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23526 }
23527
23528 return die;
23529 }
23530
23531 /* Follow reference OFFSET.
23532 On entry *REF_CU is the CU of the source die referencing OFFSET.
23533 On exit *REF_CU is the CU of the result.
23534 Returns NULL if OFFSET is invalid. */
23535
23536 static struct die_info *
23537 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23538 struct dwarf2_cu **ref_cu)
23539 {
23540 struct die_info temp_die;
23541 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23542 struct dwarf2_per_objfile *dwarf2_per_objfile
23543 = cu->per_cu->dwarf2_per_objfile;
23544
23545 gdb_assert (cu->per_cu != NULL);
23546
23547 target_cu = cu;
23548
23549 if (cu->per_cu->is_debug_types)
23550 {
23551 /* .debug_types CUs cannot reference anything outside their CU.
23552 If they need to, they have to reference a signatured type via
23553 DW_FORM_ref_sig8. */
23554 if (!offset_in_cu_p (&cu->header, sect_off))
23555 return NULL;
23556 }
23557 else if (offset_in_dwz != cu->per_cu->is_dwz
23558 || !offset_in_cu_p (&cu->header, sect_off))
23559 {
23560 struct dwarf2_per_cu_data *per_cu;
23561
23562 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23563 dwarf2_per_objfile);
23564
23565 /* If necessary, add it to the queue and load its DIEs. */
23566 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
23567 load_full_comp_unit (per_cu, false, cu->language);
23568
23569 target_cu = per_cu->cu;
23570 }
23571 else if (cu->dies == NULL)
23572 {
23573 /* We're loading full DIEs during partial symbol reading. */
23574 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
23575 load_full_comp_unit (cu->per_cu, false, language_minimal);
23576 }
23577
23578 *ref_cu = target_cu;
23579 temp_die.sect_off = sect_off;
23580
23581 if (target_cu != cu)
23582 target_cu->ancestor = cu;
23583
23584 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23585 &temp_die,
23586 to_underlying (sect_off));
23587 }
23588
23589 /* Follow reference attribute ATTR of SRC_DIE.
23590 On entry *REF_CU is the CU of SRC_DIE.
23591 On exit *REF_CU is the CU of the result. */
23592
23593 static struct die_info *
23594 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23595 struct dwarf2_cu **ref_cu)
23596 {
23597 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
23598 struct dwarf2_cu *cu = *ref_cu;
23599 struct die_info *die;
23600
23601 die = follow_die_offset (sect_off,
23602 (attr->form == DW_FORM_GNU_ref_alt
23603 || cu->per_cu->is_dwz),
23604 ref_cu);
23605 if (!die)
23606 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23607 "at %s [in module %s]"),
23608 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23609 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
23610
23611 return die;
23612 }
23613
23614 /* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
23615 Returned value is intended for DW_OP_call*. Returned
23616 dwarf2_locexpr_baton->data has lifetime of
23617 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
23618
23619 struct dwarf2_locexpr_baton
23620 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23621 struct dwarf2_per_cu_data *per_cu,
23622 CORE_ADDR (*get_frame_pc) (void *baton),
23623 void *baton, bool resolve_abstract_p)
23624 {
23625 struct dwarf2_cu *cu;
23626 struct die_info *die;
23627 struct attribute *attr;
23628 struct dwarf2_locexpr_baton retval;
23629 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23630 struct objfile *objfile = dwarf2_per_objfile->objfile;
23631
23632 if (per_cu->cu == NULL)
23633 load_cu (per_cu, false);
23634 cu = per_cu->cu;
23635 if (cu == NULL)
23636 {
23637 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23638 Instead just throw an error, not much else we can do. */
23639 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23640 sect_offset_str (sect_off), objfile_name (objfile));
23641 }
23642
23643 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23644 if (!die)
23645 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23646 sect_offset_str (sect_off), objfile_name (objfile));
23647
23648 attr = dwarf2_attr (die, DW_AT_location, cu);
23649 if (!attr && resolve_abstract_p
23650 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
23651 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23652 {
23653 CORE_ADDR pc = (*get_frame_pc) (baton);
23654 CORE_ADDR baseaddr = objfile->section_offsets[SECT_OFF_TEXT (objfile)];
23655 struct gdbarch *gdbarch = get_objfile_arch (objfile);
23656
23657 for (const auto &cand_off
23658 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
23659 {
23660 struct dwarf2_cu *cand_cu = cu;
23661 struct die_info *cand
23662 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23663 if (!cand
23664 || !cand->parent
23665 || cand->parent->tag != DW_TAG_subprogram)
23666 continue;
23667
23668 CORE_ADDR pc_low, pc_high;
23669 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23670 if (pc_low == ((CORE_ADDR) -1))
23671 continue;
23672 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23673 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23674 if (!(pc_low <= pc && pc < pc_high))
23675 continue;
23676
23677 die = cand;
23678 attr = dwarf2_attr (die, DW_AT_location, cu);
23679 break;
23680 }
23681 }
23682
23683 if (!attr)
23684 {
23685 /* DWARF: "If there is no such attribute, then there is no effect.".
23686 DATA is ignored if SIZE is 0. */
23687
23688 retval.data = NULL;
23689 retval.size = 0;
23690 }
23691 else if (attr_form_is_section_offset (attr))
23692 {
23693 struct dwarf2_loclist_baton loclist_baton;
23694 CORE_ADDR pc = (*get_frame_pc) (baton);
23695 size_t size;
23696
23697 fill_in_loclist_baton (cu, &loclist_baton, attr);
23698
23699 retval.data = dwarf2_find_location_expression (&loclist_baton,
23700 &size, pc);
23701 retval.size = size;
23702 }
23703 else
23704 {
23705 if (!attr_form_is_block (attr))
23706 error (_("Dwarf Error: DIE at %s referenced in module %s "
23707 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23708 sect_offset_str (sect_off), objfile_name (objfile));
23709
23710 retval.data = DW_BLOCK (attr)->data;
23711 retval.size = DW_BLOCK (attr)->size;
23712 }
23713 retval.per_cu = cu->per_cu;
23714
23715 age_cached_comp_units (dwarf2_per_objfile);
23716
23717 return retval;
23718 }
23719
23720 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23721 offset. */
23722
23723 struct dwarf2_locexpr_baton
23724 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23725 struct dwarf2_per_cu_data *per_cu,
23726 CORE_ADDR (*get_frame_pc) (void *baton),
23727 void *baton)
23728 {
23729 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23730
23731 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
23732 }
23733
23734 /* Write a constant of a given type as target-ordered bytes into
23735 OBSTACK. */
23736
23737 static const gdb_byte *
23738 write_constant_as_bytes (struct obstack *obstack,
23739 enum bfd_endian byte_order,
23740 struct type *type,
23741 ULONGEST value,
23742 LONGEST *len)
23743 {
23744 gdb_byte *result;
23745
23746 *len = TYPE_LENGTH (type);
23747 result = (gdb_byte *) obstack_alloc (obstack, *len);
23748 store_unsigned_integer (result, *len, byte_order, value);
23749
23750 return result;
23751 }
23752
23753 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23754 pointer to the constant bytes and set LEN to the length of the
23755 data. If memory is needed, allocate it on OBSTACK. If the DIE
23756 does not have a DW_AT_const_value, return NULL. */
23757
23758 const gdb_byte *
23759 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23760 struct dwarf2_per_cu_data *per_cu,
23761 struct obstack *obstack,
23762 LONGEST *len)
23763 {
23764 struct dwarf2_cu *cu;
23765 struct die_info *die;
23766 struct attribute *attr;
23767 const gdb_byte *result = NULL;
23768 struct type *type;
23769 LONGEST value;
23770 enum bfd_endian byte_order;
23771 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
23772
23773 if (per_cu->cu == NULL)
23774 load_cu (per_cu, false);
23775 cu = per_cu->cu;
23776 if (cu == NULL)
23777 {
23778 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23779 Instead just throw an error, not much else we can do. */
23780 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23781 sect_offset_str (sect_off), objfile_name (objfile));
23782 }
23783
23784 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23785 if (!die)
23786 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23787 sect_offset_str (sect_off), objfile_name (objfile));
23788
23789 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23790 if (attr == NULL)
23791 return NULL;
23792
23793 byte_order = (bfd_big_endian (objfile->obfd)
23794 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23795
23796 switch (attr->form)
23797 {
23798 case DW_FORM_addr:
23799 case DW_FORM_addrx:
23800 case DW_FORM_GNU_addr_index:
23801 {
23802 gdb_byte *tem;
23803
23804 *len = cu->header.addr_size;
23805 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23806 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23807 result = tem;
23808 }
23809 break;
23810 case DW_FORM_string:
23811 case DW_FORM_strp:
23812 case DW_FORM_strx:
23813 case DW_FORM_GNU_str_index:
23814 case DW_FORM_GNU_strp_alt:
23815 /* DW_STRING is already allocated on the objfile obstack, point
23816 directly to it. */
23817 result = (const gdb_byte *) DW_STRING (attr);
23818 *len = strlen (DW_STRING (attr));
23819 break;
23820 case DW_FORM_block1:
23821 case DW_FORM_block2:
23822 case DW_FORM_block4:
23823 case DW_FORM_block:
23824 case DW_FORM_exprloc:
23825 case DW_FORM_data16:
23826 result = DW_BLOCK (attr)->data;
23827 *len = DW_BLOCK (attr)->size;
23828 break;
23829
23830 /* The DW_AT_const_value attributes are supposed to carry the
23831 symbol's value "represented as it would be on the target
23832 architecture." By the time we get here, it's already been
23833 converted to host endianness, so we just need to sign- or
23834 zero-extend it as appropriate. */
23835 case DW_FORM_data1:
23836 type = die_type (die, cu);
23837 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23838 if (result == NULL)
23839 result = write_constant_as_bytes (obstack, byte_order,
23840 type, value, len);
23841 break;
23842 case DW_FORM_data2:
23843 type = die_type (die, cu);
23844 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23845 if (result == NULL)
23846 result = write_constant_as_bytes (obstack, byte_order,
23847 type, value, len);
23848 break;
23849 case DW_FORM_data4:
23850 type = die_type (die, cu);
23851 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23852 if (result == NULL)
23853 result = write_constant_as_bytes (obstack, byte_order,
23854 type, value, len);
23855 break;
23856 case DW_FORM_data8:
23857 type = die_type (die, cu);
23858 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23859 if (result == NULL)
23860 result = write_constant_as_bytes (obstack, byte_order,
23861 type, value, len);
23862 break;
23863
23864 case DW_FORM_sdata:
23865 case DW_FORM_implicit_const:
23866 type = die_type (die, cu);
23867 result = write_constant_as_bytes (obstack, byte_order,
23868 type, DW_SND (attr), len);
23869 break;
23870
23871 case DW_FORM_udata:
23872 type = die_type (die, cu);
23873 result = write_constant_as_bytes (obstack, byte_order,
23874 type, DW_UNSND (attr), len);
23875 break;
23876
23877 default:
23878 complaint (_("unsupported const value attribute form: '%s'"),
23879 dwarf_form_name (attr->form));
23880 break;
23881 }
23882
23883 return result;
23884 }
23885
23886 /* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23887 valid type for this die is found. */
23888
23889 struct type *
23890 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23891 struct dwarf2_per_cu_data *per_cu)
23892 {
23893 struct dwarf2_cu *cu;
23894 struct die_info *die;
23895
23896 if (per_cu->cu == NULL)
23897 load_cu (per_cu, false);
23898 cu = per_cu->cu;
23899 if (!cu)
23900 return NULL;
23901
23902 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23903 if (!die)
23904 return NULL;
23905
23906 return die_type (die, cu);
23907 }
23908
23909 /* Return the type of the DIE at DIE_OFFSET in the CU named by
23910 PER_CU. */
23911
23912 struct type *
23913 dwarf2_get_die_type (cu_offset die_offset,
23914 struct dwarf2_per_cu_data *per_cu)
23915 {
23916 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23917 return get_die_type_at_offset (die_offset_sect, per_cu);
23918 }
23919
23920 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23921 On entry *REF_CU is the CU of SRC_DIE.
23922 On exit *REF_CU is the CU of the result.
23923 Returns NULL if the referenced DIE isn't found. */
23924
23925 static struct die_info *
23926 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23927 struct dwarf2_cu **ref_cu)
23928 {
23929 struct die_info temp_die;
23930 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23931 struct die_info *die;
23932
23933 /* While it might be nice to assert sig_type->type == NULL here,
23934 we can get here for DW_AT_imported_declaration where we need
23935 the DIE not the type. */
23936
23937 /* If necessary, add it to the queue and load its DIEs. */
23938
23939 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
23940 read_signatured_type (sig_type);
23941
23942 sig_cu = sig_type->per_cu.cu;
23943 gdb_assert (sig_cu != NULL);
23944 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23945 temp_die.sect_off = sig_type->type_offset_in_section;
23946 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23947 to_underlying (temp_die.sect_off));
23948 if (die)
23949 {
23950 struct dwarf2_per_objfile *dwarf2_per_objfile
23951 = (*ref_cu)->per_cu->dwarf2_per_objfile;
23952
23953 /* For .gdb_index version 7 keep track of included TUs.
23954 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23955 if (dwarf2_per_objfile->index_table != NULL
23956 && dwarf2_per_objfile->index_table->version <= 7)
23957 {
23958 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23959 }
23960
23961 *ref_cu = sig_cu;
23962 if (sig_cu != cu)
23963 sig_cu->ancestor = cu;
23964
23965 return die;
23966 }
23967
23968 return NULL;
23969 }
23970
23971 /* Follow signatured type referenced by ATTR in SRC_DIE.
23972 On entry *REF_CU is the CU of SRC_DIE.
23973 On exit *REF_CU is the CU of the result.
23974 The result is the DIE of the type.
23975 If the referenced type cannot be found an error is thrown. */
23976
23977 static struct die_info *
23978 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23979 struct dwarf2_cu **ref_cu)
23980 {
23981 ULONGEST signature = DW_SIGNATURE (attr);
23982 struct signatured_type *sig_type;
23983 struct die_info *die;
23984
23985 gdb_assert (attr->form == DW_FORM_ref_sig8);
23986
23987 sig_type = lookup_signatured_type (*ref_cu, signature);
23988 /* sig_type will be NULL if the signatured type is missing from
23989 the debug info. */
23990 if (sig_type == NULL)
23991 {
23992 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23993 " from DIE at %s [in module %s]"),
23994 hex_string (signature), sect_offset_str (src_die->sect_off),
23995 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
23996 }
23997
23998 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23999 if (die == NULL)
24000 {
24001 dump_die_for_error (src_die);
24002 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24003 " from DIE at %s [in module %s]"),
24004 hex_string (signature), sect_offset_str (src_die->sect_off),
24005 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
24006 }
24007
24008 return die;
24009 }
24010
24011 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24012 reading in and processing the type unit if necessary. */
24013
24014 static struct type *
24015 get_signatured_type (struct die_info *die, ULONGEST signature,
24016 struct dwarf2_cu *cu)
24017 {
24018 struct dwarf2_per_objfile *dwarf2_per_objfile
24019 = cu->per_cu->dwarf2_per_objfile;
24020 struct signatured_type *sig_type;
24021 struct dwarf2_cu *type_cu;
24022 struct die_info *type_die;
24023 struct type *type;
24024
24025 sig_type = lookup_signatured_type (cu, signature);
24026 /* sig_type will be NULL if the signatured type is missing from
24027 the debug info. */
24028 if (sig_type == NULL)
24029 {
24030 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24031 " from DIE at %s [in module %s]"),
24032 hex_string (signature), sect_offset_str (die->sect_off),
24033 objfile_name (dwarf2_per_objfile->objfile));
24034 return build_error_marker_type (cu, die);
24035 }
24036
24037 /* If we already know the type we're done. */
24038 if (sig_type->type != NULL)
24039 return sig_type->type;
24040
24041 type_cu = cu;
24042 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24043 if (type_die != NULL)
24044 {
24045 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24046 is created. This is important, for example, because for c++ classes
24047 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24048 type = read_type_die (type_die, type_cu);
24049 if (type == NULL)
24050 {
24051 complaint (_("Dwarf Error: Cannot build signatured type %s"
24052 " referenced from DIE at %s [in module %s]"),
24053 hex_string (signature), sect_offset_str (die->sect_off),
24054 objfile_name (dwarf2_per_objfile->objfile));
24055 type = build_error_marker_type (cu, die);
24056 }
24057 }
24058 else
24059 {
24060 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24061 " from DIE at %s [in module %s]"),
24062 hex_string (signature), sect_offset_str (die->sect_off),
24063 objfile_name (dwarf2_per_objfile->objfile));
24064 type = build_error_marker_type (cu, die);
24065 }
24066 sig_type->type = type;
24067
24068 return type;
24069 }
24070
24071 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24072 reading in and processing the type unit if necessary. */
24073
24074 static struct type *
24075 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24076 struct dwarf2_cu *cu) /* ARI: editCase function */
24077 {
24078 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24079 if (attr_form_is_ref (attr))
24080 {
24081 struct dwarf2_cu *type_cu = cu;
24082 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24083
24084 return read_type_die (type_die, type_cu);
24085 }
24086 else if (attr->form == DW_FORM_ref_sig8)
24087 {
24088 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
24089 }
24090 else
24091 {
24092 struct dwarf2_per_objfile *dwarf2_per_objfile
24093 = cu->per_cu->dwarf2_per_objfile;
24094
24095 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24096 " at %s [in module %s]"),
24097 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24098 objfile_name (dwarf2_per_objfile->objfile));
24099 return build_error_marker_type (cu, die);
24100 }
24101 }
24102
24103 /* Load the DIEs associated with type unit PER_CU into memory. */
24104
24105 static void
24106 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
24107 {
24108 struct signatured_type *sig_type;
24109
24110 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24111 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
24112
24113 /* We have the per_cu, but we need the signatured_type.
24114 Fortunately this is an easy translation. */
24115 gdb_assert (per_cu->is_debug_types);
24116 sig_type = (struct signatured_type *) per_cu;
24117
24118 gdb_assert (per_cu->cu == NULL);
24119
24120 read_signatured_type (sig_type);
24121
24122 gdb_assert (per_cu->cu != NULL);
24123 }
24124
24125 /* Read in a signatured type and build its CU and DIEs.
24126 If the type is a stub for the real type in a DWO file,
24127 read in the real type from the DWO file as well. */
24128
24129 static void
24130 read_signatured_type (struct signatured_type *sig_type)
24131 {
24132 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24133
24134 gdb_assert (per_cu->is_debug_types);
24135 gdb_assert (per_cu->cu == NULL);
24136
24137 cutu_reader reader (per_cu, NULL, 0, 1, false);
24138
24139 if (!reader.dummy_p)
24140 {
24141 struct dwarf2_cu *cu = reader.cu;
24142 const gdb_byte *info_ptr = reader.info_ptr;
24143
24144 gdb_assert (cu->die_hash == NULL);
24145 cu->die_hash =
24146 htab_create_alloc_ex (cu->header.length / 12,
24147 die_hash,
24148 die_eq,
24149 NULL,
24150 &cu->comp_unit_obstack,
24151 hashtab_obstack_allocate,
24152 dummy_obstack_deallocate);
24153
24154 if (reader.has_children)
24155 reader.comp_unit_die->child
24156 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24157 reader.comp_unit_die);
24158 cu->dies = reader.comp_unit_die;
24159 /* comp_unit_die is not stored in die_hash, no need. */
24160
24161 /* We try not to read any attributes in this function, because
24162 not all CUs needed for references have been loaded yet, and
24163 symbol table processing isn't initialized. But we have to
24164 set the CU language, or we won't be able to build types
24165 correctly. Similarly, if we do not read the producer, we can
24166 not apply producer-specific interpretation. */
24167 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24168 }
24169
24170 sig_type->per_cu.tu_read = 1;
24171 }
24172
24173 /* Decode simple location descriptions.
24174 Given a pointer to a dwarf block that defines a location, compute
24175 the location and return the value.
24176
24177 NOTE drow/2003-11-18: This function is called in two situations
24178 now: for the address of static or global variables (partial symbols
24179 only) and for offsets into structures which are expected to be
24180 (more or less) constant. The partial symbol case should go away,
24181 and only the constant case should remain. That will let this
24182 function complain more accurately. A few special modes are allowed
24183 without complaint for global variables (for instance, global
24184 register values and thread-local values).
24185
24186 A location description containing no operations indicates that the
24187 object is optimized out. The return value is 0 for that case.
24188 FIXME drow/2003-11-16: No callers check for this case any more; soon all
24189 callers will only want a very basic result and this can become a
24190 complaint.
24191
24192 Note that stack[0] is unused except as a default error return. */
24193
24194 static CORE_ADDR
24195 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
24196 {
24197 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
24198 size_t i;
24199 size_t size = blk->size;
24200 const gdb_byte *data = blk->data;
24201 CORE_ADDR stack[64];
24202 int stacki;
24203 unsigned int bytes_read, unsnd;
24204 gdb_byte op;
24205
24206 i = 0;
24207 stacki = 0;
24208 stack[stacki] = 0;
24209 stack[++stacki] = 0;
24210
24211 while (i < size)
24212 {
24213 op = data[i++];
24214 switch (op)
24215 {
24216 case DW_OP_lit0:
24217 case DW_OP_lit1:
24218 case DW_OP_lit2:
24219 case DW_OP_lit3:
24220 case DW_OP_lit4:
24221 case DW_OP_lit5:
24222 case DW_OP_lit6:
24223 case DW_OP_lit7:
24224 case DW_OP_lit8:
24225 case DW_OP_lit9:
24226 case DW_OP_lit10:
24227 case DW_OP_lit11:
24228 case DW_OP_lit12:
24229 case DW_OP_lit13:
24230 case DW_OP_lit14:
24231 case DW_OP_lit15:
24232 case DW_OP_lit16:
24233 case DW_OP_lit17:
24234 case DW_OP_lit18:
24235 case DW_OP_lit19:
24236 case DW_OP_lit20:
24237 case DW_OP_lit21:
24238 case DW_OP_lit22:
24239 case DW_OP_lit23:
24240 case DW_OP_lit24:
24241 case DW_OP_lit25:
24242 case DW_OP_lit26:
24243 case DW_OP_lit27:
24244 case DW_OP_lit28:
24245 case DW_OP_lit29:
24246 case DW_OP_lit30:
24247 case DW_OP_lit31:
24248 stack[++stacki] = op - DW_OP_lit0;
24249 break;
24250
24251 case DW_OP_reg0:
24252 case DW_OP_reg1:
24253 case DW_OP_reg2:
24254 case DW_OP_reg3:
24255 case DW_OP_reg4:
24256 case DW_OP_reg5:
24257 case DW_OP_reg6:
24258 case DW_OP_reg7:
24259 case DW_OP_reg8:
24260 case DW_OP_reg9:
24261 case DW_OP_reg10:
24262 case DW_OP_reg11:
24263 case DW_OP_reg12:
24264 case DW_OP_reg13:
24265 case DW_OP_reg14:
24266 case DW_OP_reg15:
24267 case DW_OP_reg16:
24268 case DW_OP_reg17:
24269 case DW_OP_reg18:
24270 case DW_OP_reg19:
24271 case DW_OP_reg20:
24272 case DW_OP_reg21:
24273 case DW_OP_reg22:
24274 case DW_OP_reg23:
24275 case DW_OP_reg24:
24276 case DW_OP_reg25:
24277 case DW_OP_reg26:
24278 case DW_OP_reg27:
24279 case DW_OP_reg28:
24280 case DW_OP_reg29:
24281 case DW_OP_reg30:
24282 case DW_OP_reg31:
24283 stack[++stacki] = op - DW_OP_reg0;
24284 if (i < size)
24285 dwarf2_complex_location_expr_complaint ();
24286 break;
24287
24288 case DW_OP_regx:
24289 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24290 i += bytes_read;
24291 stack[++stacki] = unsnd;
24292 if (i < size)
24293 dwarf2_complex_location_expr_complaint ();
24294 break;
24295
24296 case DW_OP_addr:
24297 stack[++stacki] = read_address (objfile->obfd, &data[i],
24298 cu, &bytes_read);
24299 i += bytes_read;
24300 break;
24301
24302 case DW_OP_const1u:
24303 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24304 i += 1;
24305 break;
24306
24307 case DW_OP_const1s:
24308 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24309 i += 1;
24310 break;
24311
24312 case DW_OP_const2u:
24313 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24314 i += 2;
24315 break;
24316
24317 case DW_OP_const2s:
24318 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24319 i += 2;
24320 break;
24321
24322 case DW_OP_const4u:
24323 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24324 i += 4;
24325 break;
24326
24327 case DW_OP_const4s:
24328 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24329 i += 4;
24330 break;
24331
24332 case DW_OP_const8u:
24333 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24334 i += 8;
24335 break;
24336
24337 case DW_OP_constu:
24338 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24339 &bytes_read);
24340 i += bytes_read;
24341 break;
24342
24343 case DW_OP_consts:
24344 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24345 i += bytes_read;
24346 break;
24347
24348 case DW_OP_dup:
24349 stack[stacki + 1] = stack[stacki];
24350 stacki++;
24351 break;
24352
24353 case DW_OP_plus:
24354 stack[stacki - 1] += stack[stacki];
24355 stacki--;
24356 break;
24357
24358 case DW_OP_plus_uconst:
24359 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24360 &bytes_read);
24361 i += bytes_read;
24362 break;
24363
24364 case DW_OP_minus:
24365 stack[stacki - 1] -= stack[stacki];
24366 stacki--;
24367 break;
24368
24369 case DW_OP_deref:
24370 /* If we're not the last op, then we definitely can't encode
24371 this using GDB's address_class enum. This is valid for partial
24372 global symbols, although the variable's address will be bogus
24373 in the psymtab. */
24374 if (i < size)
24375 dwarf2_complex_location_expr_complaint ();
24376 break;
24377
24378 case DW_OP_GNU_push_tls_address:
24379 case DW_OP_form_tls_address:
24380 /* The top of the stack has the offset from the beginning
24381 of the thread control block at which the variable is located. */
24382 /* Nothing should follow this operator, so the top of stack would
24383 be returned. */
24384 /* This is valid for partial global symbols, but the variable's
24385 address will be bogus in the psymtab. Make it always at least
24386 non-zero to not look as a variable garbage collected by linker
24387 which have DW_OP_addr 0. */
24388 if (i < size)
24389 dwarf2_complex_location_expr_complaint ();
24390 stack[stacki]++;
24391 break;
24392
24393 case DW_OP_GNU_uninit:
24394 break;
24395
24396 case DW_OP_addrx:
24397 case DW_OP_GNU_addr_index:
24398 case DW_OP_GNU_const_index:
24399 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24400 &bytes_read);
24401 i += bytes_read;
24402 break;
24403
24404 default:
24405 {
24406 const char *name = get_DW_OP_name (op);
24407
24408 if (name)
24409 complaint (_("unsupported stack op: '%s'"),
24410 name);
24411 else
24412 complaint (_("unsupported stack op: '%02x'"),
24413 op);
24414 }
24415
24416 return (stack[stacki]);
24417 }
24418
24419 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24420 outside of the allocated space. Also enforce minimum>0. */
24421 if (stacki >= ARRAY_SIZE (stack) - 1)
24422 {
24423 complaint (_("location description stack overflow"));
24424 return 0;
24425 }
24426
24427 if (stacki <= 0)
24428 {
24429 complaint (_("location description stack underflow"));
24430 return 0;
24431 }
24432 }
24433 return (stack[stacki]);
24434 }
24435
24436 /* memory allocation interface */
24437
24438 static struct dwarf_block *
24439 dwarf_alloc_block (struct dwarf2_cu *cu)
24440 {
24441 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24442 }
24443
24444 static struct die_info *
24445 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24446 {
24447 struct die_info *die;
24448 size_t size = sizeof (struct die_info);
24449
24450 if (num_attrs > 1)
24451 size += (num_attrs - 1) * sizeof (struct attribute);
24452
24453 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24454 memset (die, 0, sizeof (struct die_info));
24455 return (die);
24456 }
24457
24458 \f
24459 /* Macro support. */
24460
24461 /* Return file name relative to the compilation directory of file number I in
24462 *LH's file name table. The result is allocated using xmalloc; the caller is
24463 responsible for freeing it. */
24464
24465 static char *
24466 file_file_name (int file, struct line_header *lh)
24467 {
24468 /* Is the file number a valid index into the line header's file name
24469 table? Remember that file numbers start with one, not zero. */
24470 if (lh->is_valid_file_index (file))
24471 {
24472 const file_entry *fe = lh->file_name_at (file);
24473
24474 if (!IS_ABSOLUTE_PATH (fe->name))
24475 {
24476 const char *dir = fe->include_dir (lh);
24477 if (dir != NULL)
24478 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
24479 }
24480 return xstrdup (fe->name);
24481 }
24482 else
24483 {
24484 /* The compiler produced a bogus file number. We can at least
24485 record the macro definitions made in the file, even if we
24486 won't be able to find the file by name. */
24487 char fake_name[80];
24488
24489 xsnprintf (fake_name, sizeof (fake_name),
24490 "<bad macro file number %d>", file);
24491
24492 complaint (_("bad file number in macro information (%d)"),
24493 file);
24494
24495 return xstrdup (fake_name);
24496 }
24497 }
24498
24499 /* Return the full name of file number I in *LH's file name table.
24500 Use COMP_DIR as the name of the current directory of the
24501 compilation. The result is allocated using xmalloc; the caller is
24502 responsible for freeing it. */
24503 static char *
24504 file_full_name (int file, struct line_header *lh, const char *comp_dir)
24505 {
24506 /* Is the file number a valid index into the line header's file name
24507 table? Remember that file numbers start with one, not zero. */
24508 if (lh->is_valid_file_index (file))
24509 {
24510 char *relative = file_file_name (file, lh);
24511
24512 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24513 return relative;
24514 return reconcat (relative, comp_dir, SLASH_STRING,
24515 relative, (char *) NULL);
24516 }
24517 else
24518 return file_file_name (file, lh);
24519 }
24520
24521
24522 static struct macro_source_file *
24523 macro_start_file (struct dwarf2_cu *cu,
24524 int file, int line,
24525 struct macro_source_file *current_file,
24526 struct line_header *lh)
24527 {
24528 /* File name relative to the compilation directory of this source file. */
24529 char *file_name = file_file_name (file, lh);
24530
24531 if (! current_file)
24532 {
24533 /* Note: We don't create a macro table for this compilation unit
24534 at all until we actually get a filename. */
24535 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
24536
24537 /* If we have no current file, then this must be the start_file
24538 directive for the compilation unit's main source file. */
24539 current_file = macro_set_main (macro_table, file_name);
24540 macro_define_special (macro_table);
24541 }
24542 else
24543 current_file = macro_include (current_file, line, file_name);
24544
24545 xfree (file_name);
24546
24547 return current_file;
24548 }
24549
24550 static const char *
24551 consume_improper_spaces (const char *p, const char *body)
24552 {
24553 if (*p == ' ')
24554 {
24555 complaint (_("macro definition contains spaces "
24556 "in formal argument list:\n`%s'"),
24557 body);
24558
24559 while (*p == ' ')
24560 p++;
24561 }
24562
24563 return p;
24564 }
24565
24566
24567 static void
24568 parse_macro_definition (struct macro_source_file *file, int line,
24569 const char *body)
24570 {
24571 const char *p;
24572
24573 /* The body string takes one of two forms. For object-like macro
24574 definitions, it should be:
24575
24576 <macro name> " " <definition>
24577
24578 For function-like macro definitions, it should be:
24579
24580 <macro name> "() " <definition>
24581 or
24582 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24583
24584 Spaces may appear only where explicitly indicated, and in the
24585 <definition>.
24586
24587 The Dwarf 2 spec says that an object-like macro's name is always
24588 followed by a space, but versions of GCC around March 2002 omit
24589 the space when the macro's definition is the empty string.
24590
24591 The Dwarf 2 spec says that there should be no spaces between the
24592 formal arguments in a function-like macro's formal argument list,
24593 but versions of GCC around March 2002 include spaces after the
24594 commas. */
24595
24596
24597 /* Find the extent of the macro name. The macro name is terminated
24598 by either a space or null character (for an object-like macro) or
24599 an opening paren (for a function-like macro). */
24600 for (p = body; *p; p++)
24601 if (*p == ' ' || *p == '(')
24602 break;
24603
24604 if (*p == ' ' || *p == '\0')
24605 {
24606 /* It's an object-like macro. */
24607 int name_len = p - body;
24608 std::string name (body, name_len);
24609 const char *replacement;
24610
24611 if (*p == ' ')
24612 replacement = body + name_len + 1;
24613 else
24614 {
24615 dwarf2_macro_malformed_definition_complaint (body);
24616 replacement = body + name_len;
24617 }
24618
24619 macro_define_object (file, line, name.c_str (), replacement);
24620 }
24621 else if (*p == '(')
24622 {
24623 /* It's a function-like macro. */
24624 std::string name (body, p - body);
24625 int argc = 0;
24626 int argv_size = 1;
24627 char **argv = XNEWVEC (char *, argv_size);
24628
24629 p++;
24630
24631 p = consume_improper_spaces (p, body);
24632
24633 /* Parse the formal argument list. */
24634 while (*p && *p != ')')
24635 {
24636 /* Find the extent of the current argument name. */
24637 const char *arg_start = p;
24638
24639 while (*p && *p != ',' && *p != ')' && *p != ' ')
24640 p++;
24641
24642 if (! *p || p == arg_start)
24643 dwarf2_macro_malformed_definition_complaint (body);
24644 else
24645 {
24646 /* Make sure argv has room for the new argument. */
24647 if (argc >= argv_size)
24648 {
24649 argv_size *= 2;
24650 argv = XRESIZEVEC (char *, argv, argv_size);
24651 }
24652
24653 argv[argc++] = savestring (arg_start, p - arg_start);
24654 }
24655
24656 p = consume_improper_spaces (p, body);
24657
24658 /* Consume the comma, if present. */
24659 if (*p == ',')
24660 {
24661 p++;
24662
24663 p = consume_improper_spaces (p, body);
24664 }
24665 }
24666
24667 if (*p == ')')
24668 {
24669 p++;
24670
24671 if (*p == ' ')
24672 /* Perfectly formed definition, no complaints. */
24673 macro_define_function (file, line, name.c_str (),
24674 argc, (const char **) argv,
24675 p + 1);
24676 else if (*p == '\0')
24677 {
24678 /* Complain, but do define it. */
24679 dwarf2_macro_malformed_definition_complaint (body);
24680 macro_define_function (file, line, name.c_str (),
24681 argc, (const char **) argv,
24682 p);
24683 }
24684 else
24685 /* Just complain. */
24686 dwarf2_macro_malformed_definition_complaint (body);
24687 }
24688 else
24689 /* Just complain. */
24690 dwarf2_macro_malformed_definition_complaint (body);
24691
24692 {
24693 int i;
24694
24695 for (i = 0; i < argc; i++)
24696 xfree (argv[i]);
24697 }
24698 xfree (argv);
24699 }
24700 else
24701 dwarf2_macro_malformed_definition_complaint (body);
24702 }
24703
24704 /* Skip some bytes from BYTES according to the form given in FORM.
24705 Returns the new pointer. */
24706
24707 static const gdb_byte *
24708 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
24709 enum dwarf_form form,
24710 unsigned int offset_size,
24711 struct dwarf2_section_info *section)
24712 {
24713 unsigned int bytes_read;
24714
24715 switch (form)
24716 {
24717 case DW_FORM_data1:
24718 case DW_FORM_flag:
24719 ++bytes;
24720 break;
24721
24722 case DW_FORM_data2:
24723 bytes += 2;
24724 break;
24725
24726 case DW_FORM_data4:
24727 bytes += 4;
24728 break;
24729
24730 case DW_FORM_data8:
24731 bytes += 8;
24732 break;
24733
24734 case DW_FORM_data16:
24735 bytes += 16;
24736 break;
24737
24738 case DW_FORM_string:
24739 read_direct_string (abfd, bytes, &bytes_read);
24740 bytes += bytes_read;
24741 break;
24742
24743 case DW_FORM_sec_offset:
24744 case DW_FORM_strp:
24745 case DW_FORM_GNU_strp_alt:
24746 bytes += offset_size;
24747 break;
24748
24749 case DW_FORM_block:
24750 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24751 bytes += bytes_read;
24752 break;
24753
24754 case DW_FORM_block1:
24755 bytes += 1 + read_1_byte (abfd, bytes);
24756 break;
24757 case DW_FORM_block2:
24758 bytes += 2 + read_2_bytes (abfd, bytes);
24759 break;
24760 case DW_FORM_block4:
24761 bytes += 4 + read_4_bytes (abfd, bytes);
24762 break;
24763
24764 case DW_FORM_addrx:
24765 case DW_FORM_sdata:
24766 case DW_FORM_strx:
24767 case DW_FORM_udata:
24768 case DW_FORM_GNU_addr_index:
24769 case DW_FORM_GNU_str_index:
24770 bytes = gdb_skip_leb128 (bytes, buffer_end);
24771 if (bytes == NULL)
24772 {
24773 dwarf2_section_buffer_overflow_complaint (section);
24774 return NULL;
24775 }
24776 break;
24777
24778 case DW_FORM_implicit_const:
24779 break;
24780
24781 default:
24782 {
24783 complaint (_("invalid form 0x%x in `%s'"),
24784 form, get_section_name (section));
24785 return NULL;
24786 }
24787 }
24788
24789 return bytes;
24790 }
24791
24792 /* A helper for dwarf_decode_macros that handles skipping an unknown
24793 opcode. Returns an updated pointer to the macro data buffer; or,
24794 on error, issues a complaint and returns NULL. */
24795
24796 static const gdb_byte *
24797 skip_unknown_opcode (unsigned int opcode,
24798 const gdb_byte **opcode_definitions,
24799 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24800 bfd *abfd,
24801 unsigned int offset_size,
24802 struct dwarf2_section_info *section)
24803 {
24804 unsigned int bytes_read, i;
24805 unsigned long arg;
24806 const gdb_byte *defn;
24807
24808 if (opcode_definitions[opcode] == NULL)
24809 {
24810 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
24811 opcode);
24812 return NULL;
24813 }
24814
24815 defn = opcode_definitions[opcode];
24816 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24817 defn += bytes_read;
24818
24819 for (i = 0; i < arg; ++i)
24820 {
24821 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24822 (enum dwarf_form) defn[i], offset_size,
24823 section);
24824 if (mac_ptr == NULL)
24825 {
24826 /* skip_form_bytes already issued the complaint. */
24827 return NULL;
24828 }
24829 }
24830
24831 return mac_ptr;
24832 }
24833
24834 /* A helper function which parses the header of a macro section.
24835 If the macro section is the extended (for now called "GNU") type,
24836 then this updates *OFFSET_SIZE. Returns a pointer to just after
24837 the header, or issues a complaint and returns NULL on error. */
24838
24839 static const gdb_byte *
24840 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
24841 bfd *abfd,
24842 const gdb_byte *mac_ptr,
24843 unsigned int *offset_size,
24844 int section_is_gnu)
24845 {
24846 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
24847
24848 if (section_is_gnu)
24849 {
24850 unsigned int version, flags;
24851
24852 version = read_2_bytes (abfd, mac_ptr);
24853 if (version != 4 && version != 5)
24854 {
24855 complaint (_("unrecognized version `%d' in .debug_macro section"),
24856 version);
24857 return NULL;
24858 }
24859 mac_ptr += 2;
24860
24861 flags = read_1_byte (abfd, mac_ptr);
24862 ++mac_ptr;
24863 *offset_size = (flags & 1) ? 8 : 4;
24864
24865 if ((flags & 2) != 0)
24866 /* We don't need the line table offset. */
24867 mac_ptr += *offset_size;
24868
24869 /* Vendor opcode descriptions. */
24870 if ((flags & 4) != 0)
24871 {
24872 unsigned int i, count;
24873
24874 count = read_1_byte (abfd, mac_ptr);
24875 ++mac_ptr;
24876 for (i = 0; i < count; ++i)
24877 {
24878 unsigned int opcode, bytes_read;
24879 unsigned long arg;
24880
24881 opcode = read_1_byte (abfd, mac_ptr);
24882 ++mac_ptr;
24883 opcode_definitions[opcode] = mac_ptr;
24884 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24885 mac_ptr += bytes_read;
24886 mac_ptr += arg;
24887 }
24888 }
24889 }
24890
24891 return mac_ptr;
24892 }
24893
24894 /* A helper for dwarf_decode_macros that handles the GNU extensions,
24895 including DW_MACRO_import. */
24896
24897 static void
24898 dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
24899 bfd *abfd,
24900 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
24901 struct macro_source_file *current_file,
24902 struct line_header *lh,
24903 struct dwarf2_section_info *section,
24904 int section_is_gnu, int section_is_dwz,
24905 unsigned int offset_size,
24906 htab_t include_hash)
24907 {
24908 struct dwarf2_per_objfile *dwarf2_per_objfile
24909 = cu->per_cu->dwarf2_per_objfile;
24910 struct objfile *objfile = dwarf2_per_objfile->objfile;
24911 enum dwarf_macro_record_type macinfo_type;
24912 int at_commandline;
24913 const gdb_byte *opcode_definitions[256];
24914
24915 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24916 &offset_size, section_is_gnu);
24917 if (mac_ptr == NULL)
24918 {
24919 /* We already issued a complaint. */
24920 return;
24921 }
24922
24923 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24924 GDB is still reading the definitions from command line. First
24925 DW_MACINFO_start_file will need to be ignored as it was already executed
24926 to create CURRENT_FILE for the main source holding also the command line
24927 definitions. On first met DW_MACINFO_start_file this flag is reset to
24928 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24929
24930 at_commandline = 1;
24931
24932 do
24933 {
24934 /* Do we at least have room for a macinfo type byte? */
24935 if (mac_ptr >= mac_end)
24936 {
24937 dwarf2_section_buffer_overflow_complaint (section);
24938 break;
24939 }
24940
24941 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
24942 mac_ptr++;
24943
24944 /* Note that we rely on the fact that the corresponding GNU and
24945 DWARF constants are the same. */
24946 DIAGNOSTIC_PUSH
24947 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
24948 switch (macinfo_type)
24949 {
24950 /* A zero macinfo type indicates the end of the macro
24951 information. */
24952 case 0:
24953 break;
24954
24955 case DW_MACRO_define:
24956 case DW_MACRO_undef:
24957 case DW_MACRO_define_strp:
24958 case DW_MACRO_undef_strp:
24959 case DW_MACRO_define_sup:
24960 case DW_MACRO_undef_sup:
24961 {
24962 unsigned int bytes_read;
24963 int line;
24964 const char *body;
24965 int is_define;
24966
24967 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24968 mac_ptr += bytes_read;
24969
24970 if (macinfo_type == DW_MACRO_define
24971 || macinfo_type == DW_MACRO_undef)
24972 {
24973 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24974 mac_ptr += bytes_read;
24975 }
24976 else
24977 {
24978 LONGEST str_offset;
24979
24980 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24981 mac_ptr += offset_size;
24982
24983 if (macinfo_type == DW_MACRO_define_sup
24984 || macinfo_type == DW_MACRO_undef_sup
24985 || section_is_dwz)
24986 {
24987 struct dwz_file *dwz
24988 = dwarf2_get_dwz_file (dwarf2_per_objfile);
24989
24990 body = read_indirect_string_from_dwz (objfile,
24991 dwz, str_offset);
24992 }
24993 else
24994 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24995 abfd, str_offset);
24996 }
24997
24998 is_define = (macinfo_type == DW_MACRO_define
24999 || macinfo_type == DW_MACRO_define_strp
25000 || macinfo_type == DW_MACRO_define_sup);
25001 if (! current_file)
25002 {
25003 /* DWARF violation as no main source is present. */
25004 complaint (_("debug info with no main source gives macro %s "
25005 "on line %d: %s"),
25006 is_define ? _("definition") : _("undefinition"),
25007 line, body);
25008 break;
25009 }
25010 if ((line == 0 && !at_commandline)
25011 || (line != 0 && at_commandline))
25012 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
25013 at_commandline ? _("command-line") : _("in-file"),
25014 is_define ? _("definition") : _("undefinition"),
25015 line == 0 ? _("zero") : _("non-zero"), line, body);
25016
25017 if (body == NULL)
25018 {
25019 /* Fedora's rpm-build's "debugedit" binary
25020 corrupted .debug_macro sections.
25021
25022 For more info, see
25023 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
25024 complaint (_("debug info gives %s invalid macro %s "
25025 "without body (corrupted?) at line %d "
25026 "on file %s"),
25027 at_commandline ? _("command-line") : _("in-file"),
25028 is_define ? _("definition") : _("undefinition"),
25029 line, current_file->filename);
25030 }
25031 else if (is_define)
25032 parse_macro_definition (current_file, line, body);
25033 else
25034 {
25035 gdb_assert (macinfo_type == DW_MACRO_undef
25036 || macinfo_type == DW_MACRO_undef_strp
25037 || macinfo_type == DW_MACRO_undef_sup);
25038 macro_undef (current_file, line, body);
25039 }
25040 }
25041 break;
25042
25043 case DW_MACRO_start_file:
25044 {
25045 unsigned int bytes_read;
25046 int line, file;
25047
25048 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25049 mac_ptr += bytes_read;
25050 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25051 mac_ptr += bytes_read;
25052
25053 if ((line == 0 && !at_commandline)
25054 || (line != 0 && at_commandline))
25055 complaint (_("debug info gives source %d included "
25056 "from %s at %s line %d"),
25057 file, at_commandline ? _("command-line") : _("file"),
25058 line == 0 ? _("zero") : _("non-zero"), line);
25059
25060 if (at_commandline)
25061 {
25062 /* This DW_MACRO_start_file was executed in the
25063 pass one. */
25064 at_commandline = 0;
25065 }
25066 else
25067 current_file = macro_start_file (cu, file, line, current_file,
25068 lh);
25069 }
25070 break;
25071
25072 case DW_MACRO_end_file:
25073 if (! current_file)
25074 complaint (_("macro debug info has an unmatched "
25075 "`close_file' directive"));
25076 else
25077 {
25078 current_file = current_file->included_by;
25079 if (! current_file)
25080 {
25081 enum dwarf_macro_record_type next_type;
25082
25083 /* GCC circa March 2002 doesn't produce the zero
25084 type byte marking the end of the compilation
25085 unit. Complain if it's not there, but exit no
25086 matter what. */
25087
25088 /* Do we at least have room for a macinfo type byte? */
25089 if (mac_ptr >= mac_end)
25090 {
25091 dwarf2_section_buffer_overflow_complaint (section);
25092 return;
25093 }
25094
25095 /* We don't increment mac_ptr here, so this is just
25096 a look-ahead. */
25097 next_type
25098 = (enum dwarf_macro_record_type) read_1_byte (abfd,
25099 mac_ptr);
25100 if (next_type != 0)
25101 complaint (_("no terminating 0-type entry for "
25102 "macros in `.debug_macinfo' section"));
25103
25104 return;
25105 }
25106 }
25107 break;
25108
25109 case DW_MACRO_import:
25110 case DW_MACRO_import_sup:
25111 {
25112 LONGEST offset;
25113 void **slot;
25114 bfd *include_bfd = abfd;
25115 struct dwarf2_section_info *include_section = section;
25116 const gdb_byte *include_mac_end = mac_end;
25117 int is_dwz = section_is_dwz;
25118 const gdb_byte *new_mac_ptr;
25119
25120 offset = read_offset_1 (abfd, mac_ptr, offset_size);
25121 mac_ptr += offset_size;
25122
25123 if (macinfo_type == DW_MACRO_import_sup)
25124 {
25125 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
25126
25127 dwarf2_read_section (objfile, &dwz->macro);
25128
25129 include_section = &dwz->macro;
25130 include_bfd = get_section_bfd_owner (include_section);
25131 include_mac_end = dwz->macro.buffer + dwz->macro.size;
25132 is_dwz = 1;
25133 }
25134
25135 new_mac_ptr = include_section->buffer + offset;
25136 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
25137
25138 if (*slot != NULL)
25139 {
25140 /* This has actually happened; see
25141 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
25142 complaint (_("recursive DW_MACRO_import in "
25143 ".debug_macro section"));
25144 }
25145 else
25146 {
25147 *slot = (void *) new_mac_ptr;
25148
25149 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
25150 include_mac_end, current_file, lh,
25151 section, section_is_gnu, is_dwz,
25152 offset_size, include_hash);
25153
25154 htab_remove_elt (include_hash, (void *) new_mac_ptr);
25155 }
25156 }
25157 break;
25158
25159 case DW_MACINFO_vendor_ext:
25160 if (!section_is_gnu)
25161 {
25162 unsigned int bytes_read;
25163
25164 /* This reads the constant, but since we don't recognize
25165 any vendor extensions, we ignore it. */
25166 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25167 mac_ptr += bytes_read;
25168 read_direct_string (abfd, mac_ptr, &bytes_read);
25169 mac_ptr += bytes_read;
25170
25171 /* We don't recognize any vendor extensions. */
25172 break;
25173 }
25174 /* FALLTHROUGH */
25175
25176 default:
25177 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25178 mac_ptr, mac_end, abfd, offset_size,
25179 section);
25180 if (mac_ptr == NULL)
25181 return;
25182 break;
25183 }
25184 DIAGNOSTIC_POP
25185 } while (macinfo_type != 0);
25186 }
25187
25188 static void
25189 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
25190 int section_is_gnu)
25191 {
25192 struct dwarf2_per_objfile *dwarf2_per_objfile
25193 = cu->per_cu->dwarf2_per_objfile;
25194 struct objfile *objfile = dwarf2_per_objfile->objfile;
25195 struct line_header *lh = cu->line_header;
25196 bfd *abfd;
25197 const gdb_byte *mac_ptr, *mac_end;
25198 struct macro_source_file *current_file = 0;
25199 enum dwarf_macro_record_type macinfo_type;
25200 unsigned int offset_size = cu->header.offset_size;
25201 const gdb_byte *opcode_definitions[256];
25202 void **slot;
25203 struct dwarf2_section_info *section;
25204 const char *section_name;
25205
25206 if (cu->dwo_unit != NULL)
25207 {
25208 if (section_is_gnu)
25209 {
25210 section = &cu->dwo_unit->dwo_file->sections.macro;
25211 section_name = ".debug_macro.dwo";
25212 }
25213 else
25214 {
25215 section = &cu->dwo_unit->dwo_file->sections.macinfo;
25216 section_name = ".debug_macinfo.dwo";
25217 }
25218 }
25219 else
25220 {
25221 if (section_is_gnu)
25222 {
25223 section = &dwarf2_per_objfile->macro;
25224 section_name = ".debug_macro";
25225 }
25226 else
25227 {
25228 section = &dwarf2_per_objfile->macinfo;
25229 section_name = ".debug_macinfo";
25230 }
25231 }
25232
25233 dwarf2_read_section (objfile, section);
25234 if (section->buffer == NULL)
25235 {
25236 complaint (_("missing %s section"), section_name);
25237 return;
25238 }
25239 abfd = get_section_bfd_owner (section);
25240
25241 /* First pass: Find the name of the base filename.
25242 This filename is needed in order to process all macros whose definition
25243 (or undefinition) comes from the command line. These macros are defined
25244 before the first DW_MACINFO_start_file entry, and yet still need to be
25245 associated to the base file.
25246
25247 To determine the base file name, we scan the macro definitions until we
25248 reach the first DW_MACINFO_start_file entry. We then initialize
25249 CURRENT_FILE accordingly so that any macro definition found before the
25250 first DW_MACINFO_start_file can still be associated to the base file. */
25251
25252 mac_ptr = section->buffer + offset;
25253 mac_end = section->buffer + section->size;
25254
25255 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25256 &offset_size, section_is_gnu);
25257 if (mac_ptr == NULL)
25258 {
25259 /* We already issued a complaint. */
25260 return;
25261 }
25262
25263 do
25264 {
25265 /* Do we at least have room for a macinfo type byte? */
25266 if (mac_ptr >= mac_end)
25267 {
25268 /* Complaint is printed during the second pass as GDB will probably
25269 stop the first pass earlier upon finding
25270 DW_MACINFO_start_file. */
25271 break;
25272 }
25273
25274 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
25275 mac_ptr++;
25276
25277 /* Note that we rely on the fact that the corresponding GNU and
25278 DWARF constants are the same. */
25279 DIAGNOSTIC_PUSH
25280 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
25281 switch (macinfo_type)
25282 {
25283 /* A zero macinfo type indicates the end of the macro
25284 information. */
25285 case 0:
25286 break;
25287
25288 case DW_MACRO_define:
25289 case DW_MACRO_undef:
25290 /* Only skip the data by MAC_PTR. */
25291 {
25292 unsigned int bytes_read;
25293
25294 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25295 mac_ptr += bytes_read;
25296 read_direct_string (abfd, mac_ptr, &bytes_read);
25297 mac_ptr += bytes_read;
25298 }
25299 break;
25300
25301 case DW_MACRO_start_file:
25302 {
25303 unsigned int bytes_read;
25304 int line, file;
25305
25306 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25307 mac_ptr += bytes_read;
25308 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25309 mac_ptr += bytes_read;
25310
25311 current_file = macro_start_file (cu, file, line, current_file, lh);
25312 }
25313 break;
25314
25315 case DW_MACRO_end_file:
25316 /* No data to skip by MAC_PTR. */
25317 break;
25318
25319 case DW_MACRO_define_strp:
25320 case DW_MACRO_undef_strp:
25321 case DW_MACRO_define_sup:
25322 case DW_MACRO_undef_sup:
25323 {
25324 unsigned int bytes_read;
25325
25326 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25327 mac_ptr += bytes_read;
25328 mac_ptr += offset_size;
25329 }
25330 break;
25331
25332 case DW_MACRO_import:
25333 case DW_MACRO_import_sup:
25334 /* Note that, according to the spec, a transparent include
25335 chain cannot call DW_MACRO_start_file. So, we can just
25336 skip this opcode. */
25337 mac_ptr += offset_size;
25338 break;
25339
25340 case DW_MACINFO_vendor_ext:
25341 /* Only skip the data by MAC_PTR. */
25342 if (!section_is_gnu)
25343 {
25344 unsigned int bytes_read;
25345
25346 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25347 mac_ptr += bytes_read;
25348 read_direct_string (abfd, mac_ptr, &bytes_read);
25349 mac_ptr += bytes_read;
25350 }
25351 /* FALLTHROUGH */
25352
25353 default:
25354 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
25355 mac_ptr, mac_end, abfd, offset_size,
25356 section);
25357 if (mac_ptr == NULL)
25358 return;
25359 break;
25360 }
25361 DIAGNOSTIC_POP
25362 } while (macinfo_type != 0 && current_file == NULL);
25363
25364 /* Second pass: Process all entries.
25365
25366 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25367 command-line macro definitions/undefinitions. This flag is unset when we
25368 reach the first DW_MACINFO_start_file entry. */
25369
25370 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25371 htab_eq_pointer,
25372 NULL, xcalloc, xfree));
25373 mac_ptr = section->buffer + offset;
25374 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
25375 *slot = (void *) mac_ptr;
25376 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
25377 current_file, lh, section,
25378 section_is_gnu, 0, offset_size,
25379 include_hash.get ());
25380 }
25381
25382 /* Check if the attribute's form is a DW_FORM_block*
25383 if so return true else false. */
25384
25385 static int
25386 attr_form_is_block (const struct attribute *attr)
25387 {
25388 return (attr == NULL ? 0 :
25389 attr->form == DW_FORM_block1
25390 || attr->form == DW_FORM_block2
25391 || attr->form == DW_FORM_block4
25392 || attr->form == DW_FORM_block
25393 || attr->form == DW_FORM_exprloc);
25394 }
25395
25396 /* Return non-zero if ATTR's value is a section offset --- classes
25397 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25398 You may use DW_UNSND (attr) to retrieve such offsets.
25399
25400 Section 7.5.4, "Attribute Encodings", explains that no attribute
25401 may have a value that belongs to more than one of these classes; it
25402 would be ambiguous if we did, because we use the same forms for all
25403 of them. */
25404
25405 static int
25406 attr_form_is_section_offset (const struct attribute *attr)
25407 {
25408 return (attr->form == DW_FORM_data4
25409 || attr->form == DW_FORM_data8
25410 || attr->form == DW_FORM_sec_offset);
25411 }
25412
25413 /* Return non-zero if ATTR's value falls in the 'constant' class, or
25414 zero otherwise. When this function returns true, you can apply
25415 dwarf2_get_attr_constant_value to it.
25416
25417 However, note that for some attributes you must check
25418 attr_form_is_section_offset before using this test. DW_FORM_data4
25419 and DW_FORM_data8 are members of both the constant class, and of
25420 the classes that contain offsets into other debug sections
25421 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25422 that, if an attribute's can be either a constant or one of the
25423 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
25424 taken as section offsets, not constants.
25425
25426 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25427 cannot handle that. */
25428
25429 static int
25430 attr_form_is_constant (const struct attribute *attr)
25431 {
25432 switch (attr->form)
25433 {
25434 case DW_FORM_sdata:
25435 case DW_FORM_udata:
25436 case DW_FORM_data1:
25437 case DW_FORM_data2:
25438 case DW_FORM_data4:
25439 case DW_FORM_data8:
25440 case DW_FORM_implicit_const:
25441 return 1;
25442 default:
25443 return 0;
25444 }
25445 }
25446
25447
25448 /* DW_ADDR is always stored already as sect_offset; despite for the forms
25449 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25450
25451 static int
25452 attr_form_is_ref (const struct attribute *attr)
25453 {
25454 switch (attr->form)
25455 {
25456 case DW_FORM_ref_addr:
25457 case DW_FORM_ref1:
25458 case DW_FORM_ref2:
25459 case DW_FORM_ref4:
25460 case DW_FORM_ref8:
25461 case DW_FORM_ref_udata:
25462 case DW_FORM_GNU_ref_alt:
25463 return 1;
25464 default:
25465 return 0;
25466 }
25467 }
25468
25469 /* Return the .debug_loc section to use for CU.
25470 For DWO files use .debug_loc.dwo. */
25471
25472 static struct dwarf2_section_info *
25473 cu_debug_loc_section (struct dwarf2_cu *cu)
25474 {
25475 struct dwarf2_per_objfile *dwarf2_per_objfile
25476 = cu->per_cu->dwarf2_per_objfile;
25477
25478 if (cu->dwo_unit)
25479 {
25480 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25481
25482 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25483 }
25484 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25485 : &dwarf2_per_objfile->loc);
25486 }
25487
25488 /* A helper function that fills in a dwarf2_loclist_baton. */
25489
25490 static void
25491 fill_in_loclist_baton (struct dwarf2_cu *cu,
25492 struct dwarf2_loclist_baton *baton,
25493 const struct attribute *attr)
25494 {
25495 struct dwarf2_per_objfile *dwarf2_per_objfile
25496 = cu->per_cu->dwarf2_per_objfile;
25497 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25498
25499 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
25500
25501 baton->per_cu = cu->per_cu;
25502 gdb_assert (baton->per_cu);
25503 /* We don't know how long the location list is, but make sure we
25504 don't run off the edge of the section. */
25505 baton->size = section->size - DW_UNSND (attr);
25506 baton->data = section->buffer + DW_UNSND (attr);
25507 baton->base_address = cu->base_address;
25508 baton->from_dwo = cu->dwo_unit != NULL;
25509 }
25510
25511 static void
25512 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
25513 struct dwarf2_cu *cu, int is_block)
25514 {
25515 struct dwarf2_per_objfile *dwarf2_per_objfile
25516 = cu->per_cu->dwarf2_per_objfile;
25517 struct objfile *objfile = dwarf2_per_objfile->objfile;
25518 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25519
25520 if (attr_form_is_section_offset (attr)
25521 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
25522 the section. If so, fall through to the complaint in the
25523 other branch. */
25524 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
25525 {
25526 struct dwarf2_loclist_baton *baton;
25527
25528 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
25529
25530 fill_in_loclist_baton (cu, baton, attr);
25531
25532 if (cu->base_known == 0)
25533 complaint (_("Location list used without "
25534 "specifying the CU base address."));
25535
25536 SYMBOL_ACLASS_INDEX (sym) = (is_block
25537 ? dwarf2_loclist_block_index
25538 : dwarf2_loclist_index);
25539 SYMBOL_LOCATION_BATON (sym) = baton;
25540 }
25541 else
25542 {
25543 struct dwarf2_locexpr_baton *baton;
25544
25545 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
25546 baton->per_cu = cu->per_cu;
25547 gdb_assert (baton->per_cu);
25548
25549 if (attr_form_is_block (attr))
25550 {
25551 /* Note that we're just copying the block's data pointer
25552 here, not the actual data. We're still pointing into the
25553 info_buffer for SYM's objfile; right now we never release
25554 that buffer, but when we do clean up properly this may
25555 need to change. */
25556 baton->size = DW_BLOCK (attr)->size;
25557 baton->data = DW_BLOCK (attr)->data;
25558 }
25559 else
25560 {
25561 dwarf2_invalid_attrib_class_complaint ("location description",
25562 sym->natural_name ());
25563 baton->size = 0;
25564 }
25565
25566 SYMBOL_ACLASS_INDEX (sym) = (is_block
25567 ? dwarf2_locexpr_block_index
25568 : dwarf2_locexpr_index);
25569 SYMBOL_LOCATION_BATON (sym) = baton;
25570 }
25571 }
25572
25573 /* Return the OBJFILE associated with the compilation unit CU. If CU
25574 came from a separate debuginfo file, then the master objfile is
25575 returned. */
25576
25577 struct objfile *
25578 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25579 {
25580 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25581
25582 /* Return the master objfile, so that we can report and look up the
25583 correct file containing this variable. */
25584 if (objfile->separate_debug_objfile_backlink)
25585 objfile = objfile->separate_debug_objfile_backlink;
25586
25587 return objfile;
25588 }
25589
25590 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25591 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25592 CU_HEADERP first. */
25593
25594 static const struct comp_unit_head *
25595 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25596 struct dwarf2_per_cu_data *per_cu)
25597 {
25598 const gdb_byte *info_ptr;
25599
25600 if (per_cu->cu)
25601 return &per_cu->cu->header;
25602
25603 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
25604
25605 memset (cu_headerp, 0, sizeof (*cu_headerp));
25606 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25607 rcuh_kind::COMPILE);
25608
25609 return cu_headerp;
25610 }
25611
25612 /* Return the address size given in the compilation unit header for CU. */
25613
25614 int
25615 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25616 {
25617 struct comp_unit_head cu_header_local;
25618 const struct comp_unit_head *cu_headerp;
25619
25620 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25621
25622 return cu_headerp->addr_size;
25623 }
25624
25625 /* Return the offset size given in the compilation unit header for CU. */
25626
25627 int
25628 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25629 {
25630 struct comp_unit_head cu_header_local;
25631 const struct comp_unit_head *cu_headerp;
25632
25633 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25634
25635 return cu_headerp->offset_size;
25636 }
25637
25638 /* See its dwarf2loc.h declaration. */
25639
25640 int
25641 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25642 {
25643 struct comp_unit_head cu_header_local;
25644 const struct comp_unit_head *cu_headerp;
25645
25646 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25647
25648 if (cu_headerp->version == 2)
25649 return cu_headerp->addr_size;
25650 else
25651 return cu_headerp->offset_size;
25652 }
25653
25654 /* Return the text offset of the CU. The returned offset comes from
25655 this CU's objfile. If this objfile came from a separate debuginfo
25656 file, then the offset may be different from the corresponding
25657 offset in the parent objfile. */
25658
25659 CORE_ADDR
25660 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25661 {
25662 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25663
25664 return objfile->section_offsets[SECT_OFF_TEXT (objfile)];
25665 }
25666
25667 /* Return a type that is a generic pointer type, the size of which matches
25668 the address size given in the compilation unit header for PER_CU. */
25669 static struct type *
25670 dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25671 {
25672 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25673 struct type *void_type = objfile_type (objfile)->builtin_void;
25674 struct type *addr_type = lookup_pointer_type (void_type);
25675 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25676
25677 if (TYPE_LENGTH (addr_type) == addr_size)
25678 return addr_type;
25679
25680 addr_type
25681 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25682 return addr_type;
25683 }
25684
25685 /* Return DWARF version number of PER_CU. */
25686
25687 short
25688 dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25689 {
25690 return per_cu->dwarf_version;
25691 }
25692
25693 /* Locate the .debug_info compilation unit from CU's objfile which contains
25694 the DIE at OFFSET. Raises an error on failure. */
25695
25696 static struct dwarf2_per_cu_data *
25697 dwarf2_find_containing_comp_unit (sect_offset sect_off,
25698 unsigned int offset_in_dwz,
25699 struct dwarf2_per_objfile *dwarf2_per_objfile)
25700 {
25701 struct dwarf2_per_cu_data *this_cu;
25702 int low, high;
25703
25704 low = 0;
25705 high = dwarf2_per_objfile->all_comp_units.size () - 1;
25706 while (high > low)
25707 {
25708 struct dwarf2_per_cu_data *mid_cu;
25709 int mid = low + (high - low) / 2;
25710
25711 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
25712 if (mid_cu->is_dwz > offset_in_dwz
25713 || (mid_cu->is_dwz == offset_in_dwz
25714 && mid_cu->sect_off + mid_cu->length >= sect_off))
25715 high = mid;
25716 else
25717 low = mid + 1;
25718 }
25719 gdb_assert (low == high);
25720 this_cu = dwarf2_per_objfile->all_comp_units[low];
25721 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
25722 {
25723 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
25724 error (_("Dwarf Error: could not find partial DIE containing "
25725 "offset %s [in module %s]"),
25726 sect_offset_str (sect_off),
25727 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
25728
25729 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25730 <= sect_off);
25731 return dwarf2_per_objfile->all_comp_units[low-1];
25732 }
25733 else
25734 {
25735 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
25736 && sect_off >= this_cu->sect_off + this_cu->length)
25737 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25738 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25739 return this_cu;
25740 }
25741 }
25742
25743 /* Initialize dwarf2_cu CU, owned by PER_CU. */
25744
25745 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25746 : per_cu (per_cu_),
25747 mark (false),
25748 has_loclist (false),
25749 checked_producer (false),
25750 producer_is_gxx_lt_4_6 (false),
25751 producer_is_gcc_lt_4_3 (false),
25752 producer_is_icc (false),
25753 producer_is_icc_lt_14 (false),
25754 producer_is_codewarrior (false),
25755 processing_has_namespace_info (false)
25756 {
25757 per_cu->cu = this;
25758 }
25759
25760 /* Destroy a dwarf2_cu. */
25761
25762 dwarf2_cu::~dwarf2_cu ()
25763 {
25764 per_cu->cu = NULL;
25765 }
25766
25767 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25768
25769 static void
25770 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25771 enum language pretend_language)
25772 {
25773 struct attribute *attr;
25774
25775 /* Set the language we're debugging. */
25776 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25777 if (attr != nullptr)
25778 set_cu_language (DW_UNSND (attr), cu);
25779 else
25780 {
25781 cu->language = pretend_language;
25782 cu->language_defn = language_def (cu->language);
25783 }
25784
25785 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25786 }
25787
25788 /* Increase the age counter on each cached compilation unit, and free
25789 any that are too old. */
25790
25791 static void
25792 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
25793 {
25794 struct dwarf2_per_cu_data *per_cu, **last_chain;
25795
25796 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25797 per_cu = dwarf2_per_objfile->read_in_chain;
25798 while (per_cu != NULL)
25799 {
25800 per_cu->cu->last_used ++;
25801 if (per_cu->cu->last_used <= dwarf_max_cache_age)
25802 dwarf2_mark (per_cu->cu);
25803 per_cu = per_cu->cu->read_in_chain;
25804 }
25805
25806 per_cu = dwarf2_per_objfile->read_in_chain;
25807 last_chain = &dwarf2_per_objfile->read_in_chain;
25808 while (per_cu != NULL)
25809 {
25810 struct dwarf2_per_cu_data *next_cu;
25811
25812 next_cu = per_cu->cu->read_in_chain;
25813
25814 if (!per_cu->cu->mark)
25815 {
25816 delete per_cu->cu;
25817 *last_chain = next_cu;
25818 }
25819 else
25820 last_chain = &per_cu->cu->read_in_chain;
25821
25822 per_cu = next_cu;
25823 }
25824 }
25825
25826 /* Remove a single compilation unit from the cache. */
25827
25828 static void
25829 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
25830 {
25831 struct dwarf2_per_cu_data *per_cu, **last_chain;
25832 struct dwarf2_per_objfile *dwarf2_per_objfile
25833 = target_per_cu->dwarf2_per_objfile;
25834
25835 per_cu = dwarf2_per_objfile->read_in_chain;
25836 last_chain = &dwarf2_per_objfile->read_in_chain;
25837 while (per_cu != NULL)
25838 {
25839 struct dwarf2_per_cu_data *next_cu;
25840
25841 next_cu = per_cu->cu->read_in_chain;
25842
25843 if (per_cu == target_per_cu)
25844 {
25845 delete per_cu->cu;
25846 per_cu->cu = NULL;
25847 *last_chain = next_cu;
25848 break;
25849 }
25850 else
25851 last_chain = &per_cu->cu->read_in_chain;
25852
25853 per_cu = next_cu;
25854 }
25855 }
25856
25857 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25858 We store these in a hash table separate from the DIEs, and preserve them
25859 when the DIEs are flushed out of cache.
25860
25861 The CU "per_cu" pointer is needed because offset alone is not enough to
25862 uniquely identify the type. A file may have multiple .debug_types sections,
25863 or the type may come from a DWO file. Furthermore, while it's more logical
25864 to use per_cu->section+offset, with Fission the section with the data is in
25865 the DWO file but we don't know that section at the point we need it.
25866 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25867 because we can enter the lookup routine, get_die_type_at_offset, from
25868 outside this file, and thus won't necessarily have PER_CU->cu.
25869 Fortunately, PER_CU is stable for the life of the objfile. */
25870
25871 struct dwarf2_per_cu_offset_and_type
25872 {
25873 const struct dwarf2_per_cu_data *per_cu;
25874 sect_offset sect_off;
25875 struct type *type;
25876 };
25877
25878 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25879
25880 static hashval_t
25881 per_cu_offset_and_type_hash (const void *item)
25882 {
25883 const struct dwarf2_per_cu_offset_and_type *ofs
25884 = (const struct dwarf2_per_cu_offset_and_type *) item;
25885
25886 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25887 }
25888
25889 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25890
25891 static int
25892 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25893 {
25894 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25895 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25896 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25897 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25898
25899 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25900 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25901 }
25902
25903 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25904 table if necessary. For convenience, return TYPE.
25905
25906 The DIEs reading must have careful ordering to:
25907 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25908 reading current DIE.
25909 * Not trying to dereference contents of still incompletely read in types
25910 while reading in other DIEs.
25911 * Enable referencing still incompletely read in types just by a pointer to
25912 the type without accessing its fields.
25913
25914 Therefore caller should follow these rules:
25915 * Try to fetch any prerequisite types we may need to build this DIE type
25916 before building the type and calling set_die_type.
25917 * After building type call set_die_type for current DIE as soon as
25918 possible before fetching more types to complete the current type.
25919 * Make the type as complete as possible before fetching more types. */
25920
25921 static struct type *
25922 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25923 {
25924 struct dwarf2_per_objfile *dwarf2_per_objfile
25925 = cu->per_cu->dwarf2_per_objfile;
25926 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25927 struct objfile *objfile = dwarf2_per_objfile->objfile;
25928 struct attribute *attr;
25929 struct dynamic_prop prop;
25930
25931 /* For Ada types, make sure that the gnat-specific data is always
25932 initialized (if not already set). There are a few types where
25933 we should not be doing so, because the type-specific area is
25934 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25935 where the type-specific area is used to store the floatformat).
25936 But this is not a problem, because the gnat-specific information
25937 is actually not needed for these types. */
25938 if (need_gnat_info (cu)
25939 && TYPE_CODE (type) != TYPE_CODE_FUNC
25940 && TYPE_CODE (type) != TYPE_CODE_FLT
25941 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25942 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25943 && TYPE_CODE (type) != TYPE_CODE_METHOD
25944 && !HAVE_GNAT_AUX_INFO (type))
25945 INIT_GNAT_SPECIFIC (type);
25946
25947 /* Read DW_AT_allocated and set in type. */
25948 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25949 if (attr_form_is_block (attr))
25950 {
25951 struct type *prop_type
25952 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25953 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25954 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
25955 }
25956 else if (attr != NULL)
25957 {
25958 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
25959 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25960 sect_offset_str (die->sect_off));
25961 }
25962
25963 /* Read DW_AT_associated and set in type. */
25964 attr = dwarf2_attr (die, DW_AT_associated, cu);
25965 if (attr_form_is_block (attr))
25966 {
25967 struct type *prop_type
25968 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25969 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25970 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
25971 }
25972 else if (attr != NULL)
25973 {
25974 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
25975 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
25976 sect_offset_str (die->sect_off));
25977 }
25978
25979 /* Read DW_AT_data_location and set in type. */
25980 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25981 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25982 dwarf2_per_cu_addr_type (cu->per_cu)))
25983 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
25984
25985 if (dwarf2_per_objfile->die_type_hash == NULL)
25986 {
25987 dwarf2_per_objfile->die_type_hash =
25988 htab_create_alloc_ex (127,
25989 per_cu_offset_and_type_hash,
25990 per_cu_offset_and_type_eq,
25991 NULL,
25992 &objfile->objfile_obstack,
25993 hashtab_obstack_allocate,
25994 dummy_obstack_deallocate);
25995 }
25996
25997 ofs.per_cu = cu->per_cu;
25998 ofs.sect_off = die->sect_off;
25999 ofs.type = type;
26000 slot = (struct dwarf2_per_cu_offset_and_type **)
26001 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
26002 if (*slot)
26003 complaint (_("A problem internal to GDB: DIE %s has type already set"),
26004 sect_offset_str (die->sect_off));
26005 *slot = XOBNEW (&objfile->objfile_obstack,
26006 struct dwarf2_per_cu_offset_and_type);
26007 **slot = ofs;
26008 return type;
26009 }
26010
26011 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
26012 or return NULL if the die does not have a saved type. */
26013
26014 static struct type *
26015 get_die_type_at_offset (sect_offset sect_off,
26016 struct dwarf2_per_cu_data *per_cu)
26017 {
26018 struct dwarf2_per_cu_offset_and_type *slot, ofs;
26019 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
26020
26021 if (dwarf2_per_objfile->die_type_hash == NULL)
26022 return NULL;
26023
26024 ofs.per_cu = per_cu;
26025 ofs.sect_off = sect_off;
26026 slot = ((struct dwarf2_per_cu_offset_and_type *)
26027 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
26028 if (slot)
26029 return slot->type;
26030 else
26031 return NULL;
26032 }
26033
26034 /* Look up the type for DIE in CU in die_type_hash,
26035 or return NULL if DIE does not have a saved type. */
26036
26037 static struct type *
26038 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
26039 {
26040 return get_die_type_at_offset (die->sect_off, cu->per_cu);
26041 }
26042
26043 /* Add a dependence relationship from CU to REF_PER_CU. */
26044
26045 static void
26046 dwarf2_add_dependence (struct dwarf2_cu *cu,
26047 struct dwarf2_per_cu_data *ref_per_cu)
26048 {
26049 void **slot;
26050
26051 if (cu->dependencies == NULL)
26052 cu->dependencies
26053 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
26054 NULL, &cu->comp_unit_obstack,
26055 hashtab_obstack_allocate,
26056 dummy_obstack_deallocate);
26057
26058 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
26059 if (*slot == NULL)
26060 *slot = ref_per_cu;
26061 }
26062
26063 /* Subroutine of dwarf2_mark to pass to htab_traverse.
26064 Set the mark field in every compilation unit in the
26065 cache that we must keep because we are keeping CU. */
26066
26067 static int
26068 dwarf2_mark_helper (void **slot, void *data)
26069 {
26070 struct dwarf2_per_cu_data *per_cu;
26071
26072 per_cu = (struct dwarf2_per_cu_data *) *slot;
26073
26074 /* cu->dependencies references may not yet have been ever read if QUIT aborts
26075 reading of the chain. As such dependencies remain valid it is not much
26076 useful to track and undo them during QUIT cleanups. */
26077 if (per_cu->cu == NULL)
26078 return 1;
26079
26080 if (per_cu->cu->mark)
26081 return 1;
26082 per_cu->cu->mark = true;
26083
26084 if (per_cu->cu->dependencies != NULL)
26085 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
26086
26087 return 1;
26088 }
26089
26090 /* Set the mark field in CU and in every other compilation unit in the
26091 cache that we must keep because we are keeping CU. */
26092
26093 static void
26094 dwarf2_mark (struct dwarf2_cu *cu)
26095 {
26096 if (cu->mark)
26097 return;
26098 cu->mark = true;
26099 if (cu->dependencies != NULL)
26100 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
26101 }
26102
26103 static void
26104 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
26105 {
26106 while (per_cu)
26107 {
26108 per_cu->cu->mark = false;
26109 per_cu = per_cu->cu->read_in_chain;
26110 }
26111 }
26112
26113 /* Trivial hash function for partial_die_info: the hash value of a DIE
26114 is its offset in .debug_info for this objfile. */
26115
26116 static hashval_t
26117 partial_die_hash (const void *item)
26118 {
26119 const struct partial_die_info *part_die
26120 = (const struct partial_die_info *) item;
26121
26122 return to_underlying (part_die->sect_off);
26123 }
26124
26125 /* Trivial comparison function for partial_die_info structures: two DIEs
26126 are equal if they have the same offset. */
26127
26128 static int
26129 partial_die_eq (const void *item_lhs, const void *item_rhs)
26130 {
26131 const struct partial_die_info *part_die_lhs
26132 = (const struct partial_die_info *) item_lhs;
26133 const struct partial_die_info *part_die_rhs
26134 = (const struct partial_die_info *) item_rhs;
26135
26136 return part_die_lhs->sect_off == part_die_rhs->sect_off;
26137 }
26138
26139 struct cmd_list_element *set_dwarf_cmdlist;
26140 struct cmd_list_element *show_dwarf_cmdlist;
26141
26142 static void
26143 set_dwarf_cmd (const char *args, int from_tty)
26144 {
26145 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
26146 gdb_stdout);
26147 }
26148
26149 static void
26150 show_dwarf_cmd (const char *args, int from_tty)
26151 {
26152 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
26153 }
26154
26155 bool dwarf_always_disassemble;
26156
26157 static void
26158 show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
26159 struct cmd_list_element *c, const char *value)
26160 {
26161 fprintf_filtered (file,
26162 _("Whether to always disassemble "
26163 "DWARF expressions is %s.\n"),
26164 value);
26165 }
26166
26167 static void
26168 show_check_physname (struct ui_file *file, int from_tty,
26169 struct cmd_list_element *c, const char *value)
26170 {
26171 fprintf_filtered (file,
26172 _("Whether to check \"physname\" is %s.\n"),
26173 value);
26174 }
26175
26176 void _initialize_dwarf2_read ();
26177 void
26178 _initialize_dwarf2_read ()
26179 {
26180 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
26181 Set DWARF specific variables.\n\
26182 Configure DWARF variables such as the cache size."),
26183 &set_dwarf_cmdlist, "maintenance set dwarf ",
26184 0/*allow-unknown*/, &maintenance_set_cmdlist);
26185
26186 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
26187 Show DWARF specific variables.\n\
26188 Show DWARF variables such as the cache size."),
26189 &show_dwarf_cmdlist, "maintenance show dwarf ",
26190 0/*allow-unknown*/, &maintenance_show_cmdlist);
26191
26192 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
26193 &dwarf_max_cache_age, _("\
26194 Set the upper bound on the age of cached DWARF compilation units."), _("\
26195 Show the upper bound on the age of cached DWARF compilation units."), _("\
26196 A higher limit means that cached compilation units will be stored\n\
26197 in memory longer, and more total memory will be used. Zero disables\n\
26198 caching, which can slow down startup."),
26199 NULL,
26200 show_dwarf_max_cache_age,
26201 &set_dwarf_cmdlist,
26202 &show_dwarf_cmdlist);
26203
26204 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
26205 &dwarf_always_disassemble, _("\
26206 Set whether `info address' always disassembles DWARF expressions."), _("\
26207 Show whether `info address' always disassembles DWARF expressions."), _("\
26208 When enabled, DWARF expressions are always printed in an assembly-like\n\
26209 syntax. When disabled, expressions will be printed in a more\n\
26210 conversational style, when possible."),
26211 NULL,
26212 show_dwarf_always_disassemble,
26213 &set_dwarf_cmdlist,
26214 &show_dwarf_cmdlist);
26215
26216 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
26217 Set debugging of the DWARF reader."), _("\
26218 Show debugging of the DWARF reader."), _("\
26219 When enabled (non-zero), debugging messages are printed during DWARF\n\
26220 reading and symtab expansion. A value of 1 (one) provides basic\n\
26221 information. A value greater than 1 provides more verbose information."),
26222 NULL,
26223 NULL,
26224 &setdebuglist, &showdebuglist);
26225
26226 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
26227 Set debugging of the DWARF DIE reader."), _("\
26228 Show debugging of the DWARF DIE reader."), _("\
26229 When enabled (non-zero), DIEs are dumped after they are read in.\n\
26230 The value is the maximum depth to print."),
26231 NULL,
26232 NULL,
26233 &setdebuglist, &showdebuglist);
26234
26235 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
26236 Set debugging of the dwarf line reader."), _("\
26237 Show debugging of the dwarf line reader."), _("\
26238 When enabled (non-zero), line number entries are dumped as they are read in.\n\
26239 A value of 1 (one) provides basic information.\n\
26240 A value greater than 1 provides more verbose information."),
26241 NULL,
26242 NULL,
26243 &setdebuglist, &showdebuglist);
26244
26245 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
26246 Set cross-checking of \"physname\" code against demangler."), _("\
26247 Show cross-checking of \"physname\" code against demangler."), _("\
26248 When enabled, GDB's internal \"physname\" code is checked against\n\
26249 the demangler."),
26250 NULL, show_check_physname,
26251 &setdebuglist, &showdebuglist);
26252
26253 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26254 no_class, &use_deprecated_index_sections, _("\
26255 Set whether to use deprecated gdb_index sections."), _("\
26256 Show whether to use deprecated gdb_index sections."), _("\
26257 When enabled, deprecated .gdb_index sections are used anyway.\n\
26258 Normally they are ignored either because of a missing feature or\n\
26259 performance issue.\n\
26260 Warning: This option must be enabled before gdb reads the file."),
26261 NULL,
26262 NULL,
26263 &setlist, &showlist);
26264
26265 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26266 &dwarf2_locexpr_funcs);
26267 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26268 &dwarf2_loclist_funcs);
26269
26270 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26271 &dwarf2_block_frame_base_locexpr_funcs);
26272 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26273 &dwarf2_block_frame_base_loclist_funcs);
26274
26275 #if GDB_SELF_TEST
26276 selftests::register_test ("dw2_expand_symtabs_matching",
26277 selftests::dw2_expand_symtabs_matching::run_test);
26278 #endif
26279 }
This page took 0.634128 seconds and 4 git commands to generate.